Fix parser memory leak in cilk_simd_fn_info
[official-gcc.git] / gcc / cp / parser.c
blob6583d4ca7db41fc96e40af718bd0ce6212f06609
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2015 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, PRAGMA_NONE, 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 /* Prototypes. */
183 static cp_lexer *cp_lexer_new_main
184 (void);
185 static cp_lexer *cp_lexer_new_from_tokens
186 (cp_token_cache *tokens);
187 static void cp_lexer_destroy
188 (cp_lexer *);
189 static int cp_lexer_saving_tokens
190 (const cp_lexer *);
191 static cp_token *cp_lexer_token_at
192 (cp_lexer *, cp_token_position);
193 static void cp_lexer_get_preprocessor_token
194 (cp_lexer *, cp_token *);
195 static inline cp_token *cp_lexer_peek_token
196 (cp_lexer *);
197 static cp_token *cp_lexer_peek_nth_token
198 (cp_lexer *, size_t);
199 static inline bool cp_lexer_next_token_is
200 (cp_lexer *, enum cpp_ttype);
201 static bool cp_lexer_next_token_is_not
202 (cp_lexer *, enum cpp_ttype);
203 static bool cp_lexer_next_token_is_keyword
204 (cp_lexer *, enum rid);
205 static cp_token *cp_lexer_consume_token
206 (cp_lexer *);
207 static void cp_lexer_purge_token
208 (cp_lexer *);
209 static void cp_lexer_purge_tokens_after
210 (cp_lexer *, cp_token_position);
211 static void cp_lexer_save_tokens
212 (cp_lexer *);
213 static void cp_lexer_commit_tokens
214 (cp_lexer *);
215 static void cp_lexer_rollback_tokens
216 (cp_lexer *);
217 static void cp_lexer_print_token
218 (FILE *, cp_token *);
219 static inline bool cp_lexer_debugging_p
220 (cp_lexer *);
221 static void cp_lexer_start_debugging
222 (cp_lexer *) ATTRIBUTE_UNUSED;
223 static void cp_lexer_stop_debugging
224 (cp_lexer *) ATTRIBUTE_UNUSED;
226 static cp_token_cache *cp_token_cache_new
227 (cp_token *, cp_token *);
229 static void cp_parser_initial_pragma
230 (cp_token *);
232 static tree cp_literal_operator_id
233 (const char *);
235 static void cp_parser_cilk_simd
236 (cp_parser *, cp_token *);
237 static tree cp_parser_cilk_for
238 (cp_parser *, tree);
239 static bool cp_parser_omp_declare_reduction_exprs
240 (tree, cp_parser *);
241 static tree cp_parser_cilk_simd_vectorlength
242 (cp_parser *, tree, bool);
243 static void cp_finalize_oacc_routine
244 (cp_parser *, tree, bool);
246 /* Manifest constants. */
247 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
248 #define CP_SAVED_TOKEN_STACK 5
250 /* Variables. */
252 /* The stream to which debugging output should be written. */
253 static FILE *cp_lexer_debug_stream;
255 /* Nonzero if we are parsing an unevaluated operand: an operand to
256 sizeof, typeof, or alignof. */
257 int cp_unevaluated_operand;
259 /* Dump up to NUM tokens in BUFFER to FILE starting with token
260 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
261 first token in BUFFER. If NUM is 0, dump all the tokens. If
262 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
263 highlighted by surrounding it in [[ ]]. */
265 static void
266 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
267 cp_token *start_token, unsigned num,
268 cp_token *curr_token)
270 unsigned i, nprinted;
271 cp_token *token;
272 bool do_print;
274 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
276 if (buffer == NULL)
277 return;
279 if (num == 0)
280 num = buffer->length ();
282 if (start_token == NULL)
283 start_token = buffer->address ();
285 if (start_token > buffer->address ())
287 cp_lexer_print_token (file, &(*buffer)[0]);
288 fprintf (file, " ... ");
291 do_print = false;
292 nprinted = 0;
293 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
295 if (token == start_token)
296 do_print = true;
298 if (!do_print)
299 continue;
301 nprinted++;
302 if (token == curr_token)
303 fprintf (file, "[[");
305 cp_lexer_print_token (file, token);
307 if (token == curr_token)
308 fprintf (file, "]]");
310 switch (token->type)
312 case CPP_SEMICOLON:
313 case CPP_OPEN_BRACE:
314 case CPP_CLOSE_BRACE:
315 case CPP_EOF:
316 fputc ('\n', file);
317 break;
319 default:
320 fputc (' ', file);
324 if (i == num && i < buffer->length ())
326 fprintf (file, " ... ");
327 cp_lexer_print_token (file, &buffer->last ());
330 fprintf (file, "\n");
334 /* Dump all tokens in BUFFER to stderr. */
336 void
337 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
339 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
342 DEBUG_FUNCTION void
343 debug (vec<cp_token, va_gc> &ref)
345 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
348 DEBUG_FUNCTION void
349 debug (vec<cp_token, va_gc> *ptr)
351 if (ptr)
352 debug (*ptr);
353 else
354 fprintf (stderr, "<nil>\n");
358 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
359 description for T. */
361 static void
362 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
364 if (t)
366 fprintf (file, "%s: ", desc);
367 print_node_brief (file, "", t, 0);
372 /* Dump parser context C to FILE. */
374 static void
375 cp_debug_print_context (FILE *file, cp_parser_context *c)
377 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
378 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
379 print_node_brief (file, "", c->object_type, 0);
380 fprintf (file, "}\n");
384 /* Print the stack of parsing contexts to FILE starting with FIRST. */
386 static void
387 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
389 unsigned i;
390 cp_parser_context *c;
392 fprintf (file, "Parsing context stack:\n");
393 for (i = 0, c = first; c; c = c->next, i++)
395 fprintf (file, "\t#%u: ", i);
396 cp_debug_print_context (file, c);
401 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
403 static void
404 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
406 if (flag)
407 fprintf (file, "%s: true\n", desc);
411 /* Print an unparsed function entry UF to FILE. */
413 static void
414 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
416 unsigned i;
417 cp_default_arg_entry *default_arg_fn;
418 tree fn;
420 fprintf (file, "\tFunctions with default args:\n");
421 for (i = 0;
422 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
423 i++)
425 fprintf (file, "\t\tClass type: ");
426 print_node_brief (file, "", default_arg_fn->class_type, 0);
427 fprintf (file, "\t\tDeclaration: ");
428 print_node_brief (file, "", default_arg_fn->decl, 0);
429 fprintf (file, "\n");
432 fprintf (file, "\n\tFunctions with definitions that require "
433 "post-processing\n\t\t");
434 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
436 print_node_brief (file, "", fn, 0);
437 fprintf (file, " ");
439 fprintf (file, "\n");
441 fprintf (file, "\n\tNon-static data members with initializers that require "
442 "post-processing\n\t\t");
443 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
445 print_node_brief (file, "", fn, 0);
446 fprintf (file, " ");
448 fprintf (file, "\n");
452 /* Print the stack of unparsed member functions S to FILE. */
454 static void
455 cp_debug_print_unparsed_queues (FILE *file,
456 vec<cp_unparsed_functions_entry, va_gc> *s)
458 unsigned i;
459 cp_unparsed_functions_entry *uf;
461 fprintf (file, "Unparsed functions\n");
462 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
464 fprintf (file, "#%u:\n", i);
465 cp_debug_print_unparsed_function (file, uf);
470 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
471 the given PARSER. If FILE is NULL, the output is printed on stderr. */
473 static void
474 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
476 cp_token *next_token, *first_token, *start_token;
478 if (file == NULL)
479 file = stderr;
481 next_token = parser->lexer->next_token;
482 first_token = parser->lexer->buffer->address ();
483 start_token = (next_token > first_token + window_size / 2)
484 ? next_token - window_size / 2
485 : first_token;
486 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
487 next_token);
491 /* Dump debugging information for the given PARSER. If FILE is NULL,
492 the output is printed on stderr. */
494 void
495 cp_debug_parser (FILE *file, cp_parser *parser)
497 const size_t window_size = 20;
498 cp_token *token;
499 expanded_location eloc;
501 if (file == NULL)
502 file = stderr;
504 fprintf (file, "Parser state\n\n");
505 fprintf (file, "Number of tokens: %u\n",
506 vec_safe_length (parser->lexer->buffer));
507 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
508 cp_debug_print_tree_if_set (file, "Object scope",
509 parser->object_scope);
510 cp_debug_print_tree_if_set (file, "Qualifying scope",
511 parser->qualifying_scope);
512 cp_debug_print_context_stack (file, parser->context);
513 cp_debug_print_flag (file, "Allow GNU extensions",
514 parser->allow_gnu_extensions_p);
515 cp_debug_print_flag (file, "'>' token is greater-than",
516 parser->greater_than_is_operator_p);
517 cp_debug_print_flag (file, "Default args allowed in current "
518 "parameter list", parser->default_arg_ok_p);
519 cp_debug_print_flag (file, "Parsing integral constant-expression",
520 parser->integral_constant_expression_p);
521 cp_debug_print_flag (file, "Allow non-constant expression in current "
522 "constant-expression",
523 parser->allow_non_integral_constant_expression_p);
524 cp_debug_print_flag (file, "Seen non-constant expression",
525 parser->non_integral_constant_expression_p);
526 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
527 "current context",
528 parser->local_variables_forbidden_p);
529 cp_debug_print_flag (file, "In unbraced linkage specification",
530 parser->in_unbraced_linkage_specification_p);
531 cp_debug_print_flag (file, "Parsing a declarator",
532 parser->in_declarator_p);
533 cp_debug_print_flag (file, "In template argument list",
534 parser->in_template_argument_list_p);
535 cp_debug_print_flag (file, "Parsing an iteration statement",
536 parser->in_statement & IN_ITERATION_STMT);
537 cp_debug_print_flag (file, "Parsing a switch statement",
538 parser->in_statement & IN_SWITCH_STMT);
539 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
540 parser->in_statement & IN_OMP_BLOCK);
541 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
542 parser->in_statement & IN_CILK_SIMD_FOR);
543 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
544 parser->in_statement & IN_OMP_FOR);
545 cp_debug_print_flag (file, "Parsing an if statement",
546 parser->in_statement & IN_IF_STMT);
547 cp_debug_print_flag (file, "Parsing a type-id in an expression "
548 "context", parser->in_type_id_in_expr_p);
549 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
550 parser->implicit_extern_c);
551 cp_debug_print_flag (file, "String expressions should be translated "
552 "to execution character set",
553 parser->translate_strings_p);
554 cp_debug_print_flag (file, "Parsing function body outside of a "
555 "local class", parser->in_function_body);
556 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
557 parser->colon_corrects_to_scope_p);
558 cp_debug_print_flag (file, "Colon doesn't start a class definition",
559 parser->colon_doesnt_start_class_def_p);
560 if (parser->type_definition_forbidden_message)
561 fprintf (file, "Error message for forbidden type definitions: %s\n",
562 parser->type_definition_forbidden_message);
563 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
564 fprintf (file, "Number of class definitions in progress: %u\n",
565 parser->num_classes_being_defined);
566 fprintf (file, "Number of template parameter lists for the current "
567 "declaration: %u\n", parser->num_template_parameter_lists);
568 cp_debug_parser_tokens (file, parser, window_size);
569 token = parser->lexer->next_token;
570 fprintf (file, "Next token to parse:\n");
571 fprintf (file, "\tToken: ");
572 cp_lexer_print_token (file, token);
573 eloc = expand_location (token->location);
574 fprintf (file, "\n\tFile: %s\n", eloc.file);
575 fprintf (file, "\tLine: %d\n", eloc.line);
576 fprintf (file, "\tColumn: %d\n", eloc.column);
579 DEBUG_FUNCTION void
580 debug (cp_parser &ref)
582 cp_debug_parser (stderr, &ref);
585 DEBUG_FUNCTION void
586 debug (cp_parser *ptr)
588 if (ptr)
589 debug (*ptr);
590 else
591 fprintf (stderr, "<nil>\n");
594 /* Allocate memory for a new lexer object and return it. */
596 static cp_lexer *
597 cp_lexer_alloc (void)
599 cp_lexer *lexer;
601 c_common_no_more_pch ();
603 /* Allocate the memory. */
604 lexer = ggc_cleared_alloc<cp_lexer> ();
606 /* Initially we are not debugging. */
607 lexer->debugging_p = false;
609 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
611 /* Create the buffer. */
612 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
614 return lexer;
618 /* Create a new main C++ lexer, the lexer that gets tokens from the
619 preprocessor. */
621 static cp_lexer *
622 cp_lexer_new_main (void)
624 cp_lexer *lexer;
625 cp_token token;
627 /* It's possible that parsing the first pragma will load a PCH file,
628 which is a GC collection point. So we have to do that before
629 allocating any memory. */
630 cp_parser_initial_pragma (&token);
632 lexer = cp_lexer_alloc ();
634 /* Put the first token in the buffer. */
635 lexer->buffer->quick_push (token);
637 /* Get the remaining tokens from the preprocessor. */
638 while (token.type != CPP_EOF)
640 cp_lexer_get_preprocessor_token (lexer, &token);
641 vec_safe_push (lexer->buffer, token);
644 lexer->last_token = lexer->buffer->address ()
645 + lexer->buffer->length ()
646 - 1;
647 lexer->next_token = lexer->buffer->length ()
648 ? lexer->buffer->address ()
649 : &eof_token;
651 /* Subsequent preprocessor diagnostics should use compiler
652 diagnostic functions to get the compiler source location. */
653 done_lexing = true;
655 gcc_assert (!lexer->next_token->purged_p);
656 return lexer;
659 /* Create a new lexer whose token stream is primed with the tokens in
660 CACHE. When these tokens are exhausted, no new tokens will be read. */
662 static cp_lexer *
663 cp_lexer_new_from_tokens (cp_token_cache *cache)
665 cp_token *first = cache->first;
666 cp_token *last = cache->last;
667 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
669 /* We do not own the buffer. */
670 lexer->buffer = NULL;
671 lexer->next_token = first == last ? &eof_token : first;
672 lexer->last_token = last;
674 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
676 /* Initially we are not debugging. */
677 lexer->debugging_p = false;
679 gcc_assert (!lexer->next_token->purged_p);
680 return lexer;
683 /* Frees all resources associated with LEXER. */
685 static void
686 cp_lexer_destroy (cp_lexer *lexer)
688 vec_free (lexer->buffer);
689 lexer->saved_tokens.release ();
690 ggc_free (lexer);
693 /* Returns nonzero if debugging information should be output. */
695 static inline bool
696 cp_lexer_debugging_p (cp_lexer *lexer)
698 return lexer->debugging_p;
702 static inline cp_token_position
703 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
705 gcc_assert (!previous_p || lexer->next_token != &eof_token);
707 return lexer->next_token - previous_p;
710 static inline cp_token *
711 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
713 return pos;
716 static inline void
717 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
719 lexer->next_token = cp_lexer_token_at (lexer, pos);
722 static inline cp_token_position
723 cp_lexer_previous_token_position (cp_lexer *lexer)
725 if (lexer->next_token == &eof_token)
726 return lexer->last_token - 1;
727 else
728 return cp_lexer_token_position (lexer, true);
731 static inline cp_token *
732 cp_lexer_previous_token (cp_lexer *lexer)
734 cp_token_position tp = cp_lexer_previous_token_position (lexer);
736 return cp_lexer_token_at (lexer, tp);
739 /* nonzero if we are presently saving tokens. */
741 static inline int
742 cp_lexer_saving_tokens (const cp_lexer* lexer)
744 return lexer->saved_tokens.length () != 0;
747 /* Store the next token from the preprocessor in *TOKEN. Return true
748 if we reach EOF. If LEXER is NULL, assume we are handling an
749 initial #pragma pch_preprocess, and thus want the lexer to return
750 processed strings. */
752 static void
753 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
755 static int is_extern_c = 0;
757 /* Get a new token from the preprocessor. */
758 token->type
759 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
760 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
761 token->keyword = RID_MAX;
762 token->pragma_kind = PRAGMA_NONE;
763 token->purged_p = false;
764 token->error_reported = false;
766 /* On some systems, some header files are surrounded by an
767 implicit extern "C" block. Set a flag in the token if it
768 comes from such a header. */
769 is_extern_c += pending_lang_change;
770 pending_lang_change = 0;
771 token->implicit_extern_c = is_extern_c > 0;
773 /* Check to see if this token is a keyword. */
774 if (token->type == CPP_NAME)
776 if (C_IS_RESERVED_WORD (token->u.value))
778 /* Mark this token as a keyword. */
779 token->type = CPP_KEYWORD;
780 /* Record which keyword. */
781 token->keyword = C_RID_CODE (token->u.value);
783 else
785 if (warn_cxx11_compat
786 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
787 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
789 /* Warn about the C++0x keyword (but still treat it as
790 an identifier). */
791 warning (OPT_Wc__11_compat,
792 "identifier %qE is a keyword in C++11",
793 token->u.value);
795 /* Clear out the C_RID_CODE so we don't warn about this
796 particular identifier-turned-keyword again. */
797 C_SET_RID_CODE (token->u.value, RID_MAX);
800 token->keyword = RID_MAX;
803 else if (token->type == CPP_AT_NAME)
805 /* This only happens in Objective-C++; it must be a keyword. */
806 token->type = CPP_KEYWORD;
807 switch (C_RID_CODE (token->u.value))
809 /* Replace 'class' with '@class', 'private' with '@private',
810 etc. This prevents confusion with the C++ keyword
811 'class', and makes the tokens consistent with other
812 Objective-C 'AT' keywords. For example '@class' is
813 reported as RID_AT_CLASS which is consistent with
814 '@synchronized', which is reported as
815 RID_AT_SYNCHRONIZED.
817 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
818 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
819 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
820 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
821 case RID_THROW: token->keyword = RID_AT_THROW; break;
822 case RID_TRY: token->keyword = RID_AT_TRY; break;
823 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
824 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
825 default: token->keyword = C_RID_CODE (token->u.value);
828 else if (token->type == CPP_PRAGMA)
830 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
831 token->pragma_kind = ((enum pragma_kind)
832 TREE_INT_CST_LOW (token->u.value));
833 token->u.value = NULL_TREE;
837 /* Update the globals input_location and the input file stack from TOKEN. */
838 static inline void
839 cp_lexer_set_source_position_from_token (cp_token *token)
841 if (token->type != CPP_EOF)
843 input_location = token->location;
847 /* Update the globals input_location and the input file stack from LEXER. */
848 static inline void
849 cp_lexer_set_source_position (cp_lexer *lexer)
851 cp_token *token = cp_lexer_peek_token (lexer);
852 cp_lexer_set_source_position_from_token (token);
855 /* Return a pointer to the next token in the token stream, but do not
856 consume it. */
858 static inline cp_token *
859 cp_lexer_peek_token (cp_lexer *lexer)
861 if (cp_lexer_debugging_p (lexer))
863 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
864 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
865 putc ('\n', cp_lexer_debug_stream);
867 return lexer->next_token;
870 /* Return true if the next token has the indicated TYPE. */
872 static inline bool
873 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
875 return cp_lexer_peek_token (lexer)->type == type;
878 /* Return true if the next token does not have the indicated TYPE. */
880 static inline bool
881 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
883 return !cp_lexer_next_token_is (lexer, type);
886 /* Return true if the next token is the indicated KEYWORD. */
888 static inline bool
889 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
891 return cp_lexer_peek_token (lexer)->keyword == keyword;
894 static inline bool
895 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
897 return cp_lexer_peek_nth_token (lexer, n)->type == type;
900 static inline bool
901 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
903 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
906 /* Return true if the next token is not the indicated KEYWORD. */
908 static inline bool
909 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
911 return cp_lexer_peek_token (lexer)->keyword != keyword;
914 /* Return true if the next token is a keyword for a decl-specifier. */
916 static bool
917 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
919 cp_token *token;
921 token = cp_lexer_peek_token (lexer);
922 switch (token->keyword)
924 /* auto specifier: storage-class-specifier in C++,
925 simple-type-specifier in C++0x. */
926 case RID_AUTO:
927 /* Storage classes. */
928 case RID_REGISTER:
929 case RID_STATIC:
930 case RID_EXTERN:
931 case RID_MUTABLE:
932 case RID_THREAD:
933 /* Elaborated type specifiers. */
934 case RID_ENUM:
935 case RID_CLASS:
936 case RID_STRUCT:
937 case RID_UNION:
938 case RID_TYPENAME:
939 /* Simple type specifiers. */
940 case RID_CHAR:
941 case RID_CHAR16:
942 case RID_CHAR32:
943 case RID_WCHAR:
944 case RID_BOOL:
945 case RID_SHORT:
946 case RID_INT:
947 case RID_LONG:
948 case RID_SIGNED:
949 case RID_UNSIGNED:
950 case RID_FLOAT:
951 case RID_DOUBLE:
952 case RID_VOID:
953 /* GNU extensions. */
954 case RID_ATTRIBUTE:
955 case RID_TYPEOF:
956 /* C++0x extensions. */
957 case RID_DECLTYPE:
958 case RID_UNDERLYING_TYPE:
959 return true;
961 default:
962 if (token->keyword >= RID_FIRST_INT_N
963 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
964 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
965 return true;
966 return false;
970 /* Returns TRUE iff the token T begins a decltype type. */
972 static bool
973 token_is_decltype (cp_token *t)
975 return (t->keyword == RID_DECLTYPE
976 || t->type == CPP_DECLTYPE);
979 /* Returns TRUE iff the next token begins a decltype type. */
981 static bool
982 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
984 cp_token *t = cp_lexer_peek_token (lexer);
985 return token_is_decltype (t);
988 /* Return a pointer to the Nth token in the token stream. If N is 1,
989 then this is precisely equivalent to cp_lexer_peek_token (except
990 that it is not inline). One would like to disallow that case, but
991 there is one case (cp_parser_nth_token_starts_template_id) where
992 the caller passes a variable for N and it might be 1. */
994 static cp_token *
995 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
997 cp_token *token;
999 /* N is 1-based, not zero-based. */
1000 gcc_assert (n > 0);
1002 if (cp_lexer_debugging_p (lexer))
1003 fprintf (cp_lexer_debug_stream,
1004 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1006 --n;
1007 token = lexer->next_token;
1008 gcc_assert (!n || token != &eof_token);
1009 while (n != 0)
1011 ++token;
1012 if (token == lexer->last_token)
1014 token = &eof_token;
1015 break;
1018 if (!token->purged_p)
1019 --n;
1022 if (cp_lexer_debugging_p (lexer))
1024 cp_lexer_print_token (cp_lexer_debug_stream, token);
1025 putc ('\n', cp_lexer_debug_stream);
1028 return token;
1031 /* Return the next token, and advance the lexer's next_token pointer
1032 to point to the next non-purged token. */
1034 static cp_token *
1035 cp_lexer_consume_token (cp_lexer* lexer)
1037 cp_token *token = lexer->next_token;
1039 gcc_assert (token != &eof_token);
1040 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1044 lexer->next_token++;
1045 if (lexer->next_token == lexer->last_token)
1047 lexer->next_token = &eof_token;
1048 break;
1052 while (lexer->next_token->purged_p);
1054 cp_lexer_set_source_position_from_token (token);
1056 /* Provide debugging output. */
1057 if (cp_lexer_debugging_p (lexer))
1059 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1060 cp_lexer_print_token (cp_lexer_debug_stream, token);
1061 putc ('\n', cp_lexer_debug_stream);
1064 return token;
1067 /* Permanently remove the next token from the token stream, and
1068 advance the next_token pointer to refer to the next non-purged
1069 token. */
1071 static void
1072 cp_lexer_purge_token (cp_lexer *lexer)
1074 cp_token *tok = lexer->next_token;
1076 gcc_assert (tok != &eof_token);
1077 tok->purged_p = true;
1078 tok->location = UNKNOWN_LOCATION;
1079 tok->u.value = NULL_TREE;
1080 tok->keyword = RID_MAX;
1084 tok++;
1085 if (tok == lexer->last_token)
1087 tok = &eof_token;
1088 break;
1091 while (tok->purged_p);
1092 lexer->next_token = tok;
1095 /* Permanently remove all tokens after TOK, up to, but not
1096 including, the token that will be returned next by
1097 cp_lexer_peek_token. */
1099 static void
1100 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1102 cp_token *peek = lexer->next_token;
1104 if (peek == &eof_token)
1105 peek = lexer->last_token;
1107 gcc_assert (tok < peek);
1109 for ( tok += 1; tok != peek; tok += 1)
1111 tok->purged_p = true;
1112 tok->location = UNKNOWN_LOCATION;
1113 tok->u.value = NULL_TREE;
1114 tok->keyword = RID_MAX;
1118 /* Begin saving tokens. All tokens consumed after this point will be
1119 preserved. */
1121 static void
1122 cp_lexer_save_tokens (cp_lexer* lexer)
1124 /* Provide debugging output. */
1125 if (cp_lexer_debugging_p (lexer))
1126 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1128 lexer->saved_tokens.safe_push (lexer->next_token);
1131 /* Commit to the portion of the token stream most recently saved. */
1133 static void
1134 cp_lexer_commit_tokens (cp_lexer* lexer)
1136 /* Provide debugging output. */
1137 if (cp_lexer_debugging_p (lexer))
1138 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1140 lexer->saved_tokens.pop ();
1143 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1144 to the token stream. Stop saving tokens. */
1146 static void
1147 cp_lexer_rollback_tokens (cp_lexer* lexer)
1149 /* Provide debugging output. */
1150 if (cp_lexer_debugging_p (lexer))
1151 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1153 lexer->next_token = lexer->saved_tokens.pop ();
1156 /* RAII wrapper around the above functions, with sanity checking. Creating
1157 a variable saves tokens, which are committed when the variable is
1158 destroyed unless they are explicitly rolled back by calling the rollback
1159 member function. */
1161 struct saved_token_sentinel
1163 cp_lexer *lexer;
1164 unsigned len;
1165 bool commit;
1166 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1168 len = lexer->saved_tokens.length ();
1169 cp_lexer_save_tokens (lexer);
1171 void rollback ()
1173 cp_lexer_rollback_tokens (lexer);
1174 commit = false;
1176 ~saved_token_sentinel()
1178 if (commit)
1179 cp_lexer_commit_tokens (lexer);
1180 gcc_assert (lexer->saved_tokens.length () == len);
1184 /* Print a representation of the TOKEN on the STREAM. */
1186 static void
1187 cp_lexer_print_token (FILE * stream, cp_token *token)
1189 /* We don't use cpp_type2name here because the parser defines
1190 a few tokens of its own. */
1191 static const char *const token_names[] = {
1192 /* cpplib-defined token types */
1193 #define OP(e, s) #e,
1194 #define TK(e, s) #e,
1195 TTYPE_TABLE
1196 #undef OP
1197 #undef TK
1198 /* C++ parser token types - see "Manifest constants", above. */
1199 "KEYWORD",
1200 "TEMPLATE_ID",
1201 "NESTED_NAME_SPECIFIER",
1204 /* For some tokens, print the associated data. */
1205 switch (token->type)
1207 case CPP_KEYWORD:
1208 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1209 For example, `struct' is mapped to an INTEGER_CST. */
1210 if (!identifier_p (token->u.value))
1211 break;
1212 /* else fall through */
1213 case CPP_NAME:
1214 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1215 break;
1217 case CPP_STRING:
1218 case CPP_STRING16:
1219 case CPP_STRING32:
1220 case CPP_WSTRING:
1221 case CPP_UTF8STRING:
1222 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1223 break;
1225 case CPP_NUMBER:
1226 print_generic_expr (stream, token->u.value, 0);
1227 break;
1229 default:
1230 /* If we have a name for the token, print it out. Otherwise, we
1231 simply give the numeric code. */
1232 if (token->type < ARRAY_SIZE(token_names))
1233 fputs (token_names[token->type], stream);
1234 else
1235 fprintf (stream, "[%d]", token->type);
1236 break;
1240 DEBUG_FUNCTION void
1241 debug (cp_token &ref)
1243 cp_lexer_print_token (stderr, &ref);
1244 fprintf (stderr, "\n");
1247 DEBUG_FUNCTION void
1248 debug (cp_token *ptr)
1250 if (ptr)
1251 debug (*ptr);
1252 else
1253 fprintf (stderr, "<nil>\n");
1257 /* Start emitting debugging information. */
1259 static void
1260 cp_lexer_start_debugging (cp_lexer* lexer)
1262 lexer->debugging_p = true;
1263 cp_lexer_debug_stream = stderr;
1266 /* Stop emitting debugging information. */
1268 static void
1269 cp_lexer_stop_debugging (cp_lexer* lexer)
1271 lexer->debugging_p = false;
1272 cp_lexer_debug_stream = NULL;
1275 /* Create a new cp_token_cache, representing a range of tokens. */
1277 static cp_token_cache *
1278 cp_token_cache_new (cp_token *first, cp_token *last)
1280 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1281 cache->first = first;
1282 cache->last = last;
1283 return cache;
1286 /* Diagnose if #pragma omp declare simd isn't followed immediately
1287 by function declaration or definition. */
1289 static inline void
1290 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1292 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1294 error ("%<#pragma omp declare simd%> not immediately followed by "
1295 "function declaration or definition");
1296 parser->omp_declare_simd = NULL;
1300 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1301 and put that into "omp declare simd" attribute. */
1303 static inline void
1304 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1306 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1308 if (fndecl == error_mark_node)
1310 parser->omp_declare_simd = NULL;
1311 return;
1313 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1315 cp_ensure_no_omp_declare_simd (parser);
1316 return;
1321 /* Diagnose if #pragma acc routine isn't followed immediately by function
1322 declaration or definition. */
1324 static inline void
1325 cp_ensure_no_oacc_routine (cp_parser *parser)
1327 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1329 tree clauses = parser->oacc_routine->clauses;
1330 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses));
1332 error_at (loc, "%<#pragma oacc routine%> not followed by function "
1333 "declaration or definition");
1334 parser->oacc_routine = NULL;
1338 /* Decl-specifiers. */
1340 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1342 static void
1343 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1345 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1348 /* Declarators. */
1350 /* Nothing other than the parser should be creating declarators;
1351 declarators are a semi-syntactic representation of C++ entities.
1352 Other parts of the front end that need to create entities (like
1353 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1355 static cp_declarator *make_call_declarator
1356 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1357 static cp_declarator *make_array_declarator
1358 (cp_declarator *, tree);
1359 static cp_declarator *make_pointer_declarator
1360 (cp_cv_quals, cp_declarator *, tree);
1361 static cp_declarator *make_reference_declarator
1362 (cp_cv_quals, cp_declarator *, bool, tree);
1363 static cp_declarator *make_ptrmem_declarator
1364 (cp_cv_quals, tree, cp_declarator *, tree);
1366 /* An erroneous declarator. */
1367 static cp_declarator *cp_error_declarator;
1369 /* The obstack on which declarators and related data structures are
1370 allocated. */
1371 static struct obstack declarator_obstack;
1373 /* Alloc BYTES from the declarator memory pool. */
1375 static inline void *
1376 alloc_declarator (size_t bytes)
1378 return obstack_alloc (&declarator_obstack, bytes);
1381 /* Allocate a declarator of the indicated KIND. Clear fields that are
1382 common to all declarators. */
1384 static cp_declarator *
1385 make_declarator (cp_declarator_kind kind)
1387 cp_declarator *declarator;
1389 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1390 declarator->kind = kind;
1391 declarator->attributes = NULL_TREE;
1392 declarator->std_attributes = NULL_TREE;
1393 declarator->declarator = NULL;
1394 declarator->parameter_pack_p = false;
1395 declarator->id_loc = UNKNOWN_LOCATION;
1397 return declarator;
1400 /* Make a declarator for a generalized identifier. If
1401 QUALIFYING_SCOPE is non-NULL, the identifier is
1402 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1403 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1404 is, if any. */
1406 static cp_declarator *
1407 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1408 special_function_kind sfk)
1410 cp_declarator *declarator;
1412 /* It is valid to write:
1414 class C { void f(); };
1415 typedef C D;
1416 void D::f();
1418 The standard is not clear about whether `typedef const C D' is
1419 legal; as of 2002-09-15 the committee is considering that
1420 question. EDG 3.0 allows that syntax. Therefore, we do as
1421 well. */
1422 if (qualifying_scope && TYPE_P (qualifying_scope))
1423 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1425 gcc_assert (identifier_p (unqualified_name)
1426 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1427 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1429 declarator = make_declarator (cdk_id);
1430 declarator->u.id.qualifying_scope = qualifying_scope;
1431 declarator->u.id.unqualified_name = unqualified_name;
1432 declarator->u.id.sfk = sfk;
1434 return declarator;
1437 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1438 of modifiers such as const or volatile to apply to the pointer
1439 type, represented as identifiers. ATTRIBUTES represent the attributes that
1440 appertain to the pointer or reference. */
1442 cp_declarator *
1443 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1444 tree attributes)
1446 cp_declarator *declarator;
1448 declarator = make_declarator (cdk_pointer);
1449 declarator->declarator = target;
1450 declarator->u.pointer.qualifiers = cv_qualifiers;
1451 declarator->u.pointer.class_type = NULL_TREE;
1452 if (target)
1454 declarator->id_loc = target->id_loc;
1455 declarator->parameter_pack_p = target->parameter_pack_p;
1456 target->parameter_pack_p = false;
1458 else
1459 declarator->parameter_pack_p = false;
1461 declarator->std_attributes = attributes;
1463 return declarator;
1466 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1467 represent the attributes that appertain to the pointer or
1468 reference. */
1470 cp_declarator *
1471 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1472 bool rvalue_ref, tree attributes)
1474 cp_declarator *declarator;
1476 declarator = make_declarator (cdk_reference);
1477 declarator->declarator = target;
1478 declarator->u.reference.qualifiers = cv_qualifiers;
1479 declarator->u.reference.rvalue_ref = rvalue_ref;
1480 if (target)
1482 declarator->id_loc = target->id_loc;
1483 declarator->parameter_pack_p = target->parameter_pack_p;
1484 target->parameter_pack_p = false;
1486 else
1487 declarator->parameter_pack_p = false;
1489 declarator->std_attributes = attributes;
1491 return declarator;
1494 /* Like make_pointer_declarator -- but for a pointer to a non-static
1495 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1496 appertain to the pointer or reference. */
1498 cp_declarator *
1499 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1500 cp_declarator *pointee,
1501 tree attributes)
1503 cp_declarator *declarator;
1505 declarator = make_declarator (cdk_ptrmem);
1506 declarator->declarator = pointee;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = class_type;
1510 if (pointee)
1512 declarator->parameter_pack_p = pointee->parameter_pack_p;
1513 pointee->parameter_pack_p = false;
1515 else
1516 declarator->parameter_pack_p = false;
1518 declarator->std_attributes = attributes;
1520 return declarator;
1523 /* Make a declarator for the function given by TARGET, with the
1524 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1525 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1526 indicates what exceptions can be thrown. */
1528 cp_declarator *
1529 make_call_declarator (cp_declarator *target,
1530 tree parms,
1531 cp_cv_quals cv_qualifiers,
1532 cp_virt_specifiers virt_specifiers,
1533 cp_ref_qualifier ref_qualifier,
1534 tree tx_qualifier,
1535 tree exception_specification,
1536 tree late_return_type,
1537 tree requires_clause)
1539 cp_declarator *declarator;
1541 declarator = make_declarator (cdk_function);
1542 declarator->declarator = target;
1543 declarator->u.function.parameters = parms;
1544 declarator->u.function.qualifiers = cv_qualifiers;
1545 declarator->u.function.virt_specifiers = virt_specifiers;
1546 declarator->u.function.ref_qualifier = ref_qualifier;
1547 declarator->u.function.tx_qualifier = tx_qualifier;
1548 declarator->u.function.exception_specification = exception_specification;
1549 declarator->u.function.late_return_type = late_return_type;
1550 declarator->u.function.requires_clause = requires_clause;
1551 if (target)
1553 declarator->id_loc = target->id_loc;
1554 declarator->parameter_pack_p = target->parameter_pack_p;
1555 target->parameter_pack_p = false;
1557 else
1558 declarator->parameter_pack_p = false;
1560 return declarator;
1563 /* Make a declarator for an array of BOUNDS elements, each of which is
1564 defined by ELEMENT. */
1566 cp_declarator *
1567 make_array_declarator (cp_declarator *element, tree bounds)
1569 cp_declarator *declarator;
1571 declarator = make_declarator (cdk_array);
1572 declarator->declarator = element;
1573 declarator->u.array.bounds = bounds;
1574 if (element)
1576 declarator->id_loc = element->id_loc;
1577 declarator->parameter_pack_p = element->parameter_pack_p;
1578 element->parameter_pack_p = false;
1580 else
1581 declarator->parameter_pack_p = false;
1583 return declarator;
1586 /* Determine whether the declarator we've seen so far can be a
1587 parameter pack, when followed by an ellipsis. */
1588 static bool
1589 declarator_can_be_parameter_pack (cp_declarator *declarator)
1591 if (declarator && declarator->parameter_pack_p)
1592 /* We already saw an ellipsis. */
1593 return false;
1595 /* Search for a declarator name, or any other declarator that goes
1596 after the point where the ellipsis could appear in a parameter
1597 pack. If we find any of these, then this declarator can not be
1598 made into a parameter pack. */
1599 bool found = false;
1600 while (declarator && !found)
1602 switch ((int)declarator->kind)
1604 case cdk_id:
1605 case cdk_array:
1606 found = true;
1607 break;
1609 case cdk_error:
1610 return true;
1612 default:
1613 declarator = declarator->declarator;
1614 break;
1618 return !found;
1621 cp_parameter_declarator *no_parameters;
1623 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1624 DECLARATOR and DEFAULT_ARGUMENT. */
1626 cp_parameter_declarator *
1627 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1628 cp_declarator *declarator,
1629 tree default_argument,
1630 bool template_parameter_pack_p = false)
1632 cp_parameter_declarator *parameter;
1634 parameter = ((cp_parameter_declarator *)
1635 alloc_declarator (sizeof (cp_parameter_declarator)));
1636 parameter->next = NULL;
1637 if (decl_specifiers)
1638 parameter->decl_specifiers = *decl_specifiers;
1639 else
1640 clear_decl_specs (&parameter->decl_specifiers);
1641 parameter->declarator = declarator;
1642 parameter->default_argument = default_argument;
1643 parameter->template_parameter_pack_p = template_parameter_pack_p;
1645 return parameter;
1648 /* Returns true iff DECLARATOR is a declaration for a function. */
1650 static bool
1651 function_declarator_p (const cp_declarator *declarator)
1653 while (declarator)
1655 if (declarator->kind == cdk_function
1656 && declarator->declarator->kind == cdk_id)
1657 return true;
1658 if (declarator->kind == cdk_id
1659 || declarator->kind == cdk_error)
1660 return false;
1661 declarator = declarator->declarator;
1663 return false;
1666 /* The parser. */
1668 /* Overview
1669 --------
1671 A cp_parser parses the token stream as specified by the C++
1672 grammar. Its job is purely parsing, not semantic analysis. For
1673 example, the parser breaks the token stream into declarators,
1674 expressions, statements, and other similar syntactic constructs.
1675 It does not check that the types of the expressions on either side
1676 of an assignment-statement are compatible, or that a function is
1677 not declared with a parameter of type `void'.
1679 The parser invokes routines elsewhere in the compiler to perform
1680 semantic analysis and to build up the abstract syntax tree for the
1681 code processed.
1683 The parser (and the template instantiation code, which is, in a
1684 way, a close relative of parsing) are the only parts of the
1685 compiler that should be calling push_scope and pop_scope, or
1686 related functions. The parser (and template instantiation code)
1687 keeps track of what scope is presently active; everything else
1688 should simply honor that. (The code that generates static
1689 initializers may also need to set the scope, in order to check
1690 access control correctly when emitting the initializers.)
1692 Methodology
1693 -----------
1695 The parser is of the standard recursive-descent variety. Upcoming
1696 tokens in the token stream are examined in order to determine which
1697 production to use when parsing a non-terminal. Some C++ constructs
1698 require arbitrary look ahead to disambiguate. For example, it is
1699 impossible, in the general case, to tell whether a statement is an
1700 expression or declaration without scanning the entire statement.
1701 Therefore, the parser is capable of "parsing tentatively." When the
1702 parser is not sure what construct comes next, it enters this mode.
1703 Then, while we attempt to parse the construct, the parser queues up
1704 error messages, rather than issuing them immediately, and saves the
1705 tokens it consumes. If the construct is parsed successfully, the
1706 parser "commits", i.e., it issues any queued error messages and
1707 the tokens that were being preserved are permanently discarded.
1708 If, however, the construct is not parsed successfully, the parser
1709 rolls back its state completely so that it can resume parsing using
1710 a different alternative.
1712 Future Improvements
1713 -------------------
1715 The performance of the parser could probably be improved substantially.
1716 We could often eliminate the need to parse tentatively by looking ahead
1717 a little bit. In some places, this approach might not entirely eliminate
1718 the need to parse tentatively, but it might still speed up the average
1719 case. */
1721 /* Flags that are passed to some parsing functions. These values can
1722 be bitwise-ored together. */
1724 enum
1726 /* No flags. */
1727 CP_PARSER_FLAGS_NONE = 0x0,
1728 /* The construct is optional. If it is not present, then no error
1729 should be issued. */
1730 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1731 /* When parsing a type-specifier, treat user-defined type-names
1732 as non-type identifiers. */
1733 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1734 /* When parsing a type-specifier, do not try to parse a class-specifier
1735 or enum-specifier. */
1736 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1737 /* When parsing a decl-specifier-seq, only allow type-specifier or
1738 constexpr. */
1739 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1742 /* This type is used for parameters and variables which hold
1743 combinations of the above flags. */
1744 typedef int cp_parser_flags;
1746 /* The different kinds of declarators we want to parse. */
1748 enum cp_parser_declarator_kind
1750 /* We want an abstract declarator. */
1751 CP_PARSER_DECLARATOR_ABSTRACT,
1752 /* We want a named declarator. */
1753 CP_PARSER_DECLARATOR_NAMED,
1754 /* We don't mind, but the name must be an unqualified-id. */
1755 CP_PARSER_DECLARATOR_EITHER
1758 /* The precedence values used to parse binary expressions. The minimum value
1759 of PREC must be 1, because zero is reserved to quickly discriminate
1760 binary operators from other tokens. */
1762 enum cp_parser_prec
1764 PREC_NOT_OPERATOR,
1765 PREC_LOGICAL_OR_EXPRESSION,
1766 PREC_LOGICAL_AND_EXPRESSION,
1767 PREC_INCLUSIVE_OR_EXPRESSION,
1768 PREC_EXCLUSIVE_OR_EXPRESSION,
1769 PREC_AND_EXPRESSION,
1770 PREC_EQUALITY_EXPRESSION,
1771 PREC_RELATIONAL_EXPRESSION,
1772 PREC_SHIFT_EXPRESSION,
1773 PREC_ADDITIVE_EXPRESSION,
1774 PREC_MULTIPLICATIVE_EXPRESSION,
1775 PREC_PM_EXPRESSION,
1776 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1779 /* A mapping from a token type to a corresponding tree node type, with a
1780 precedence value. */
1782 struct cp_parser_binary_operations_map_node
1784 /* The token type. */
1785 enum cpp_ttype token_type;
1786 /* The corresponding tree code. */
1787 enum tree_code tree_type;
1788 /* The precedence of this operator. */
1789 enum cp_parser_prec prec;
1792 struct cp_parser_expression_stack_entry
1794 /* Left hand side of the binary operation we are currently
1795 parsing. */
1796 tree lhs;
1797 /* Original tree code for left hand side, if it was a binary
1798 expression itself (used for -Wparentheses). */
1799 enum tree_code lhs_type;
1800 /* Tree code for the binary operation we are parsing. */
1801 enum tree_code tree_type;
1802 /* Precedence of the binary operation we are parsing. */
1803 enum cp_parser_prec prec;
1804 /* Location of the binary operation we are parsing. */
1805 location_t loc;
1808 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1809 entries because precedence levels on the stack are monotonically
1810 increasing. */
1811 typedef struct cp_parser_expression_stack_entry
1812 cp_parser_expression_stack[NUM_PREC_VALUES];
1814 /* Prototypes. */
1816 /* Constructors and destructors. */
1818 static cp_parser_context *cp_parser_context_new
1819 (cp_parser_context *);
1821 /* Class variables. */
1823 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1825 /* The operator-precedence table used by cp_parser_binary_expression.
1826 Transformed into an associative array (binops_by_token) by
1827 cp_parser_new. */
1829 static const cp_parser_binary_operations_map_node binops[] = {
1830 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1831 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1833 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1834 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1835 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1837 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1838 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1840 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1841 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1843 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1844 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1845 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1846 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1848 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1849 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1851 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1853 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1855 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1857 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1859 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1862 /* The same as binops, but initialized by cp_parser_new so that
1863 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1864 for speed. */
1865 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1867 /* Constructors and destructors. */
1869 /* Construct a new context. The context below this one on the stack
1870 is given by NEXT. */
1872 static cp_parser_context *
1873 cp_parser_context_new (cp_parser_context* next)
1875 cp_parser_context *context;
1877 /* Allocate the storage. */
1878 if (cp_parser_context_free_list != NULL)
1880 /* Pull the first entry from the free list. */
1881 context = cp_parser_context_free_list;
1882 cp_parser_context_free_list = context->next;
1883 memset (context, 0, sizeof (*context));
1885 else
1886 context = ggc_cleared_alloc<cp_parser_context> ();
1888 /* No errors have occurred yet in this context. */
1889 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1890 /* If this is not the bottommost context, copy information that we
1891 need from the previous context. */
1892 if (next)
1894 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1895 expression, then we are parsing one in this context, too. */
1896 context->object_type = next->object_type;
1897 /* Thread the stack. */
1898 context->next = next;
1901 return context;
1904 /* Managing the unparsed function queues. */
1906 #define unparsed_funs_with_default_args \
1907 parser->unparsed_queues->last ().funs_with_default_args
1908 #define unparsed_funs_with_definitions \
1909 parser->unparsed_queues->last ().funs_with_definitions
1910 #define unparsed_nsdmis \
1911 parser->unparsed_queues->last ().nsdmis
1912 #define unparsed_classes \
1913 parser->unparsed_queues->last ().classes
1915 static void
1916 push_unparsed_function_queues (cp_parser *parser)
1918 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1919 vec_safe_push (parser->unparsed_queues, e);
1922 static void
1923 pop_unparsed_function_queues (cp_parser *parser)
1925 release_tree_vector (unparsed_funs_with_definitions);
1926 parser->unparsed_queues->pop ();
1929 /* Prototypes. */
1931 /* Constructors and destructors. */
1933 static cp_parser *cp_parser_new
1934 (void);
1936 /* Routines to parse various constructs.
1938 Those that return `tree' will return the error_mark_node (rather
1939 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1940 Sometimes, they will return an ordinary node if error-recovery was
1941 attempted, even though a parse error occurred. So, to check
1942 whether or not a parse error occurred, you should always use
1943 cp_parser_error_occurred. If the construct is optional (indicated
1944 either by an `_opt' in the name of the function that does the
1945 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1946 the construct is not present. */
1948 /* Lexical conventions [gram.lex] */
1950 static tree cp_parser_identifier
1951 (cp_parser *);
1952 static tree cp_parser_string_literal
1953 (cp_parser *, bool, bool, bool);
1954 static tree cp_parser_userdef_char_literal
1955 (cp_parser *);
1956 static tree cp_parser_userdef_string_literal
1957 (tree);
1958 static tree cp_parser_userdef_numeric_literal
1959 (cp_parser *);
1961 /* Basic concepts [gram.basic] */
1963 static bool cp_parser_translation_unit
1964 (cp_parser *);
1966 /* Expressions [gram.expr] */
1968 static tree cp_parser_primary_expression
1969 (cp_parser *, bool, bool, bool, cp_id_kind *);
1970 static tree cp_parser_id_expression
1971 (cp_parser *, bool, bool, bool *, bool, bool);
1972 static tree cp_parser_unqualified_id
1973 (cp_parser *, bool, bool, bool, bool);
1974 static tree cp_parser_nested_name_specifier_opt
1975 (cp_parser *, bool, bool, bool, bool);
1976 static tree cp_parser_nested_name_specifier
1977 (cp_parser *, bool, bool, bool, bool);
1978 static tree cp_parser_qualifying_entity
1979 (cp_parser *, bool, bool, bool, bool, bool);
1980 static tree cp_parser_postfix_expression
1981 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1982 static tree cp_parser_postfix_open_square_expression
1983 (cp_parser *, tree, bool, bool);
1984 static tree cp_parser_postfix_dot_deref_expression
1985 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1986 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1987 (cp_parser *, int, bool, bool, bool *);
1988 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1989 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1990 static void cp_parser_pseudo_destructor_name
1991 (cp_parser *, tree, tree *, tree *);
1992 static tree cp_parser_unary_expression
1993 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1994 static enum tree_code cp_parser_unary_operator
1995 (cp_token *);
1996 static tree cp_parser_new_expression
1997 (cp_parser *);
1998 static vec<tree, va_gc> *cp_parser_new_placement
1999 (cp_parser *);
2000 static tree cp_parser_new_type_id
2001 (cp_parser *, tree *);
2002 static cp_declarator *cp_parser_new_declarator_opt
2003 (cp_parser *);
2004 static cp_declarator *cp_parser_direct_new_declarator
2005 (cp_parser *);
2006 static vec<tree, va_gc> *cp_parser_new_initializer
2007 (cp_parser *);
2008 static tree cp_parser_delete_expression
2009 (cp_parser *);
2010 static tree cp_parser_cast_expression
2011 (cp_parser *, bool, bool, bool, cp_id_kind *);
2012 static tree cp_parser_binary_expression
2013 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2014 static tree cp_parser_question_colon_clause
2015 (cp_parser *, tree);
2016 static tree cp_parser_assignment_expression
2017 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2018 static enum tree_code cp_parser_assignment_operator_opt
2019 (cp_parser *);
2020 static tree cp_parser_expression
2021 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2022 static tree cp_parser_constant_expression
2023 (cp_parser *, bool = false, bool * = NULL);
2024 static tree cp_parser_builtin_offsetof
2025 (cp_parser *);
2026 static tree cp_parser_lambda_expression
2027 (cp_parser *);
2028 static void cp_parser_lambda_introducer
2029 (cp_parser *, tree);
2030 static bool cp_parser_lambda_declarator_opt
2031 (cp_parser *, tree);
2032 static void cp_parser_lambda_body
2033 (cp_parser *, tree);
2035 /* Statements [gram.stmt.stmt] */
2037 static void cp_parser_statement
2038 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2039 static void cp_parser_label_for_labeled_statement
2040 (cp_parser *, tree);
2041 static tree cp_parser_expression_statement
2042 (cp_parser *, tree);
2043 static tree cp_parser_compound_statement
2044 (cp_parser *, tree, int, bool);
2045 static void cp_parser_statement_seq_opt
2046 (cp_parser *, tree);
2047 static tree cp_parser_selection_statement
2048 (cp_parser *, bool *, vec<tree> *);
2049 static tree cp_parser_condition
2050 (cp_parser *);
2051 static tree cp_parser_iteration_statement
2052 (cp_parser *, bool);
2053 static bool cp_parser_for_init_statement
2054 (cp_parser *, tree *decl);
2055 static tree cp_parser_for
2056 (cp_parser *, bool);
2057 static tree cp_parser_c_for
2058 (cp_parser *, tree, tree, bool);
2059 static tree cp_parser_range_for
2060 (cp_parser *, tree, tree, tree, bool);
2061 static void do_range_for_auto_deduction
2062 (tree, tree);
2063 static tree cp_parser_perform_range_for_lookup
2064 (tree, tree *, tree *);
2065 static tree cp_parser_range_for_member_function
2066 (tree, tree);
2067 static tree cp_parser_jump_statement
2068 (cp_parser *);
2069 static void cp_parser_declaration_statement
2070 (cp_parser *);
2072 static tree cp_parser_implicitly_scoped_statement
2073 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2074 static void cp_parser_already_scoped_statement
2075 (cp_parser *, const token_indent_info &);
2077 /* Declarations [gram.dcl.dcl] */
2079 static void cp_parser_declaration_seq_opt
2080 (cp_parser *);
2081 static void cp_parser_declaration
2082 (cp_parser *);
2083 static void cp_parser_block_declaration
2084 (cp_parser *, bool);
2085 static void cp_parser_simple_declaration
2086 (cp_parser *, bool, tree *);
2087 static void cp_parser_decl_specifier_seq
2088 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2089 static tree cp_parser_storage_class_specifier_opt
2090 (cp_parser *);
2091 static tree cp_parser_function_specifier_opt
2092 (cp_parser *, cp_decl_specifier_seq *);
2093 static tree cp_parser_type_specifier
2094 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2095 int *, bool *);
2096 static tree cp_parser_simple_type_specifier
2097 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2098 static tree cp_parser_type_name
2099 (cp_parser *, bool);
2100 static tree cp_parser_type_name
2101 (cp_parser *);
2102 static tree cp_parser_nonclass_name
2103 (cp_parser* parser);
2104 static tree cp_parser_elaborated_type_specifier
2105 (cp_parser *, bool, bool);
2106 static tree cp_parser_enum_specifier
2107 (cp_parser *);
2108 static void cp_parser_enumerator_list
2109 (cp_parser *, tree);
2110 static void cp_parser_enumerator_definition
2111 (cp_parser *, tree);
2112 static tree cp_parser_namespace_name
2113 (cp_parser *);
2114 static void cp_parser_namespace_definition
2115 (cp_parser *);
2116 static void cp_parser_namespace_body
2117 (cp_parser *);
2118 static tree cp_parser_qualified_namespace_specifier
2119 (cp_parser *);
2120 static void cp_parser_namespace_alias_definition
2121 (cp_parser *);
2122 static bool cp_parser_using_declaration
2123 (cp_parser *, bool);
2124 static void cp_parser_using_directive
2125 (cp_parser *);
2126 static tree cp_parser_alias_declaration
2127 (cp_parser *);
2128 static void cp_parser_asm_definition
2129 (cp_parser *);
2130 static void cp_parser_linkage_specification
2131 (cp_parser *);
2132 static void cp_parser_static_assert
2133 (cp_parser *, bool);
2134 static tree cp_parser_decltype
2135 (cp_parser *);
2137 /* Declarators [gram.dcl.decl] */
2139 static tree cp_parser_init_declarator
2140 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2141 bool, bool, int, bool *, tree *, location_t *);
2142 static cp_declarator *cp_parser_declarator
2143 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2144 static cp_declarator *cp_parser_direct_declarator
2145 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2146 static enum tree_code cp_parser_ptr_operator
2147 (cp_parser *, tree *, cp_cv_quals *, tree *);
2148 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2149 (cp_parser *);
2150 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2151 (cp_parser *);
2152 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2153 (cp_parser *);
2154 static tree cp_parser_tx_qualifier_opt
2155 (cp_parser *);
2156 static tree cp_parser_late_return_type_opt
2157 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2158 static tree cp_parser_declarator_id
2159 (cp_parser *, bool);
2160 static tree cp_parser_type_id
2161 (cp_parser *);
2162 static tree cp_parser_template_type_arg
2163 (cp_parser *);
2164 static tree cp_parser_trailing_type_id (cp_parser *);
2165 static tree cp_parser_type_id_1
2166 (cp_parser *, bool, bool);
2167 static void cp_parser_type_specifier_seq
2168 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2169 static tree cp_parser_parameter_declaration_clause
2170 (cp_parser *);
2171 static tree cp_parser_parameter_declaration_list
2172 (cp_parser *, bool *);
2173 static cp_parameter_declarator *cp_parser_parameter_declaration
2174 (cp_parser *, bool, bool *);
2175 static tree cp_parser_default_argument
2176 (cp_parser *, bool);
2177 static void cp_parser_function_body
2178 (cp_parser *, bool);
2179 static tree cp_parser_initializer
2180 (cp_parser *, bool *, bool *);
2181 static tree cp_parser_initializer_clause
2182 (cp_parser *, bool *);
2183 static tree cp_parser_braced_list
2184 (cp_parser*, bool*);
2185 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2186 (cp_parser *, bool *);
2188 static bool cp_parser_ctor_initializer_opt_and_function_body
2189 (cp_parser *, bool);
2191 static tree cp_parser_late_parsing_omp_declare_simd
2192 (cp_parser *, tree);
2194 static tree cp_parser_late_parsing_cilk_simd_fn_info
2195 (cp_parser *, tree);
2197 static tree cp_parser_late_parsing_oacc_routine
2198 (cp_parser *, tree);
2200 static tree synthesize_implicit_template_parm
2201 (cp_parser *, tree);
2202 static tree finish_fully_implicit_template
2203 (cp_parser *, tree);
2205 /* Classes [gram.class] */
2207 static tree cp_parser_class_name
2208 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2209 static tree cp_parser_class_specifier
2210 (cp_parser *);
2211 static tree cp_parser_class_head
2212 (cp_parser *, bool *);
2213 static enum tag_types cp_parser_class_key
2214 (cp_parser *);
2215 static void cp_parser_type_parameter_key
2216 (cp_parser* parser);
2217 static void cp_parser_member_specification_opt
2218 (cp_parser *);
2219 static void cp_parser_member_declaration
2220 (cp_parser *);
2221 static tree cp_parser_pure_specifier
2222 (cp_parser *);
2223 static tree cp_parser_constant_initializer
2224 (cp_parser *);
2226 /* Derived classes [gram.class.derived] */
2228 static tree cp_parser_base_clause
2229 (cp_parser *);
2230 static tree cp_parser_base_specifier
2231 (cp_parser *);
2233 /* Special member functions [gram.special] */
2235 static tree cp_parser_conversion_function_id
2236 (cp_parser *);
2237 static tree cp_parser_conversion_type_id
2238 (cp_parser *);
2239 static cp_declarator *cp_parser_conversion_declarator_opt
2240 (cp_parser *);
2241 static bool cp_parser_ctor_initializer_opt
2242 (cp_parser *);
2243 static void cp_parser_mem_initializer_list
2244 (cp_parser *);
2245 static tree cp_parser_mem_initializer
2246 (cp_parser *);
2247 static tree cp_parser_mem_initializer_id
2248 (cp_parser *);
2250 /* Overloading [gram.over] */
2252 static tree cp_parser_operator_function_id
2253 (cp_parser *);
2254 static tree cp_parser_operator
2255 (cp_parser *);
2257 /* Templates [gram.temp] */
2259 static void cp_parser_template_declaration
2260 (cp_parser *, bool);
2261 static tree cp_parser_template_parameter_list
2262 (cp_parser *);
2263 static tree cp_parser_template_parameter
2264 (cp_parser *, bool *, bool *);
2265 static tree cp_parser_type_parameter
2266 (cp_parser *, bool *);
2267 static tree cp_parser_template_id
2268 (cp_parser *, bool, bool, enum tag_types, bool);
2269 static tree cp_parser_template_name
2270 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2271 static tree cp_parser_template_argument_list
2272 (cp_parser *);
2273 static tree cp_parser_template_argument
2274 (cp_parser *);
2275 static void cp_parser_explicit_instantiation
2276 (cp_parser *);
2277 static void cp_parser_explicit_specialization
2278 (cp_parser *);
2280 /* Exception handling [gram.exception] */
2282 static tree cp_parser_try_block
2283 (cp_parser *);
2284 static bool cp_parser_function_try_block
2285 (cp_parser *);
2286 static void cp_parser_handler_seq
2287 (cp_parser *);
2288 static void cp_parser_handler
2289 (cp_parser *);
2290 static tree cp_parser_exception_declaration
2291 (cp_parser *);
2292 static tree cp_parser_throw_expression
2293 (cp_parser *);
2294 static tree cp_parser_exception_specification_opt
2295 (cp_parser *);
2296 static tree cp_parser_type_id_list
2297 (cp_parser *);
2299 /* GNU Extensions */
2301 static tree cp_parser_asm_specification_opt
2302 (cp_parser *);
2303 static tree cp_parser_asm_operand_list
2304 (cp_parser *);
2305 static tree cp_parser_asm_clobber_list
2306 (cp_parser *);
2307 static tree cp_parser_asm_label_list
2308 (cp_parser *);
2309 static bool cp_next_tokens_can_be_attribute_p
2310 (cp_parser *);
2311 static bool cp_next_tokens_can_be_gnu_attribute_p
2312 (cp_parser *);
2313 static bool cp_next_tokens_can_be_std_attribute_p
2314 (cp_parser *);
2315 static bool cp_nth_tokens_can_be_std_attribute_p
2316 (cp_parser *, size_t);
2317 static bool cp_nth_tokens_can_be_gnu_attribute_p
2318 (cp_parser *, size_t);
2319 static bool cp_nth_tokens_can_be_attribute_p
2320 (cp_parser *, size_t);
2321 static tree cp_parser_attributes_opt
2322 (cp_parser *);
2323 static tree cp_parser_gnu_attributes_opt
2324 (cp_parser *);
2325 static tree cp_parser_gnu_attribute_list
2326 (cp_parser *);
2327 static tree cp_parser_std_attribute
2328 (cp_parser *);
2329 static tree cp_parser_std_attribute_spec
2330 (cp_parser *);
2331 static tree cp_parser_std_attribute_spec_seq
2332 (cp_parser *);
2333 static bool cp_parser_extension_opt
2334 (cp_parser *, int *);
2335 static void cp_parser_label_declaration
2336 (cp_parser *);
2338 /* Concept Extensions */
2340 static tree cp_parser_requires_clause
2341 (cp_parser *);
2342 static tree cp_parser_requires_clause_opt
2343 (cp_parser *);
2344 static tree cp_parser_requires_expression
2345 (cp_parser *);
2346 static tree cp_parser_requirement_parameter_list
2347 (cp_parser *);
2348 static tree cp_parser_requirement_body
2349 (cp_parser *);
2350 static tree cp_parser_requirement_list
2351 (cp_parser *);
2352 static tree cp_parser_requirement
2353 (cp_parser *);
2354 static tree cp_parser_simple_requirement
2355 (cp_parser *);
2356 static tree cp_parser_compound_requirement
2357 (cp_parser *);
2358 static tree cp_parser_type_requirement
2359 (cp_parser *);
2360 static tree cp_parser_nested_requirement
2361 (cp_parser *);
2363 /* Transactional Memory Extensions */
2365 static tree cp_parser_transaction
2366 (cp_parser *, cp_token *);
2367 static tree cp_parser_transaction_expression
2368 (cp_parser *, enum rid);
2369 static bool cp_parser_function_transaction
2370 (cp_parser *, enum rid);
2371 static tree cp_parser_transaction_cancel
2372 (cp_parser *);
2374 enum pragma_context {
2375 pragma_external,
2376 pragma_member,
2377 pragma_objc_icode,
2378 pragma_stmt,
2379 pragma_compound
2381 static bool cp_parser_pragma
2382 (cp_parser *, enum pragma_context);
2384 /* Objective-C++ Productions */
2386 static tree cp_parser_objc_message_receiver
2387 (cp_parser *);
2388 static tree cp_parser_objc_message_args
2389 (cp_parser *);
2390 static tree cp_parser_objc_message_expression
2391 (cp_parser *);
2392 static tree cp_parser_objc_encode_expression
2393 (cp_parser *);
2394 static tree cp_parser_objc_defs_expression
2395 (cp_parser *);
2396 static tree cp_parser_objc_protocol_expression
2397 (cp_parser *);
2398 static tree cp_parser_objc_selector_expression
2399 (cp_parser *);
2400 static tree cp_parser_objc_expression
2401 (cp_parser *);
2402 static bool cp_parser_objc_selector_p
2403 (enum cpp_ttype);
2404 static tree cp_parser_objc_selector
2405 (cp_parser *);
2406 static tree cp_parser_objc_protocol_refs_opt
2407 (cp_parser *);
2408 static void cp_parser_objc_declaration
2409 (cp_parser *, tree);
2410 static tree cp_parser_objc_statement
2411 (cp_parser *);
2412 static bool cp_parser_objc_valid_prefix_attributes
2413 (cp_parser *, tree *);
2414 static void cp_parser_objc_at_property_declaration
2415 (cp_parser *) ;
2416 static void cp_parser_objc_at_synthesize_declaration
2417 (cp_parser *) ;
2418 static void cp_parser_objc_at_dynamic_declaration
2419 (cp_parser *) ;
2420 static tree cp_parser_objc_struct_declaration
2421 (cp_parser *) ;
2423 /* Utility Routines */
2425 static tree cp_parser_lookup_name
2426 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2427 static tree cp_parser_lookup_name_simple
2428 (cp_parser *, tree, location_t);
2429 static tree cp_parser_maybe_treat_template_as_class
2430 (tree, bool);
2431 static bool cp_parser_check_declarator_template_parameters
2432 (cp_parser *, cp_declarator *, location_t);
2433 static bool cp_parser_check_template_parameters
2434 (cp_parser *, unsigned, location_t, cp_declarator *);
2435 static tree cp_parser_simple_cast_expression
2436 (cp_parser *);
2437 static tree cp_parser_global_scope_opt
2438 (cp_parser *, bool);
2439 static bool cp_parser_constructor_declarator_p
2440 (cp_parser *, bool);
2441 static tree cp_parser_function_definition_from_specifiers_and_declarator
2442 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2443 static tree cp_parser_function_definition_after_declarator
2444 (cp_parser *, bool);
2445 static bool cp_parser_template_declaration_after_export
2446 (cp_parser *, bool);
2447 static void cp_parser_perform_template_parameter_access_checks
2448 (vec<deferred_access_check, va_gc> *);
2449 static tree cp_parser_single_declaration
2450 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2451 static tree cp_parser_functional_cast
2452 (cp_parser *, tree);
2453 static tree cp_parser_save_member_function_body
2454 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2455 static tree cp_parser_save_nsdmi
2456 (cp_parser *);
2457 static tree cp_parser_enclosed_template_argument_list
2458 (cp_parser *);
2459 static void cp_parser_save_default_args
2460 (cp_parser *, tree);
2461 static void cp_parser_late_parsing_for_member
2462 (cp_parser *, tree);
2463 static tree cp_parser_late_parse_one_default_arg
2464 (cp_parser *, tree, tree, tree);
2465 static void cp_parser_late_parsing_nsdmi
2466 (cp_parser *, tree);
2467 static void cp_parser_late_parsing_default_args
2468 (cp_parser *, tree);
2469 static tree cp_parser_sizeof_operand
2470 (cp_parser *, enum rid);
2471 static tree cp_parser_trait_expr
2472 (cp_parser *, enum rid);
2473 static bool cp_parser_declares_only_class_p
2474 (cp_parser *);
2475 static void cp_parser_set_storage_class
2476 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2477 static void cp_parser_set_decl_spec_type
2478 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2479 static void set_and_check_decl_spec_loc
2480 (cp_decl_specifier_seq *decl_specs,
2481 cp_decl_spec ds, cp_token *);
2482 static bool cp_parser_friend_p
2483 (const cp_decl_specifier_seq *);
2484 static void cp_parser_required_error
2485 (cp_parser *, required_token, bool);
2486 static cp_token *cp_parser_require
2487 (cp_parser *, enum cpp_ttype, required_token);
2488 static cp_token *cp_parser_require_keyword
2489 (cp_parser *, enum rid, required_token);
2490 static bool cp_parser_token_starts_function_definition_p
2491 (cp_token *);
2492 static bool cp_parser_next_token_starts_class_definition_p
2493 (cp_parser *);
2494 static bool cp_parser_next_token_ends_template_argument_p
2495 (cp_parser *);
2496 static bool cp_parser_nth_token_starts_template_argument_list_p
2497 (cp_parser *, size_t);
2498 static enum tag_types cp_parser_token_is_class_key
2499 (cp_token *);
2500 static enum tag_types cp_parser_token_is_type_parameter_key
2501 (cp_token *);
2502 static void cp_parser_check_class_key
2503 (enum tag_types, tree type);
2504 static void cp_parser_check_access_in_redeclaration
2505 (tree type, location_t location);
2506 static bool cp_parser_optional_template_keyword
2507 (cp_parser *);
2508 static void cp_parser_pre_parsed_nested_name_specifier
2509 (cp_parser *);
2510 static bool cp_parser_cache_group
2511 (cp_parser *, enum cpp_ttype, unsigned);
2512 static tree cp_parser_cache_defarg
2513 (cp_parser *parser, bool nsdmi);
2514 static void cp_parser_parse_tentatively
2515 (cp_parser *);
2516 static void cp_parser_commit_to_tentative_parse
2517 (cp_parser *);
2518 static void cp_parser_commit_to_topmost_tentative_parse
2519 (cp_parser *);
2520 static void cp_parser_abort_tentative_parse
2521 (cp_parser *);
2522 static bool cp_parser_parse_definitely
2523 (cp_parser *);
2524 static inline bool cp_parser_parsing_tentatively
2525 (cp_parser *);
2526 static bool cp_parser_uncommitted_to_tentative_parse_p
2527 (cp_parser *);
2528 static void cp_parser_error
2529 (cp_parser *, const char *);
2530 static void cp_parser_name_lookup_error
2531 (cp_parser *, tree, tree, name_lookup_error, location_t);
2532 static bool cp_parser_simulate_error
2533 (cp_parser *);
2534 static bool cp_parser_check_type_definition
2535 (cp_parser *);
2536 static void cp_parser_check_for_definition_in_return_type
2537 (cp_declarator *, tree, location_t type_location);
2538 static void cp_parser_check_for_invalid_template_id
2539 (cp_parser *, tree, enum tag_types, location_t location);
2540 static bool cp_parser_non_integral_constant_expression
2541 (cp_parser *, non_integral_constant);
2542 static void cp_parser_diagnose_invalid_type_name
2543 (cp_parser *, tree, location_t);
2544 static bool cp_parser_parse_and_diagnose_invalid_type_name
2545 (cp_parser *);
2546 static int cp_parser_skip_to_closing_parenthesis
2547 (cp_parser *, bool, bool, bool);
2548 static void cp_parser_skip_to_end_of_statement
2549 (cp_parser *);
2550 static void cp_parser_consume_semicolon_at_end_of_statement
2551 (cp_parser *);
2552 static void cp_parser_skip_to_end_of_block_or_statement
2553 (cp_parser *);
2554 static bool cp_parser_skip_to_closing_brace
2555 (cp_parser *);
2556 static void cp_parser_skip_to_end_of_template_parameter_list
2557 (cp_parser *);
2558 static void cp_parser_skip_to_pragma_eol
2559 (cp_parser*, cp_token *);
2560 static bool cp_parser_error_occurred
2561 (cp_parser *);
2562 static bool cp_parser_allow_gnu_extensions_p
2563 (cp_parser *);
2564 static bool cp_parser_is_pure_string_literal
2565 (cp_token *);
2566 static bool cp_parser_is_string_literal
2567 (cp_token *);
2568 static bool cp_parser_is_keyword
2569 (cp_token *, enum rid);
2570 static tree cp_parser_make_typename_type
2571 (cp_parser *, tree, location_t location);
2572 static cp_declarator * cp_parser_make_indirect_declarator
2573 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2574 static bool cp_parser_compound_literal_p
2575 (cp_parser *);
2576 static bool cp_parser_array_designator_p
2577 (cp_parser *);
2578 static bool cp_parser_skip_to_closing_square_bracket
2579 (cp_parser *);
2581 /* Concept-related syntactic transformations */
2583 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2584 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2586 // -------------------------------------------------------------------------- //
2587 // Unevaluated Operand Guard
2589 // Implementation of an RAII helper for unevaluated operand parsing.
2590 cp_unevaluated::cp_unevaluated ()
2592 ++cp_unevaluated_operand;
2593 ++c_inhibit_evaluation_warnings;
2596 cp_unevaluated::~cp_unevaluated ()
2598 --c_inhibit_evaluation_warnings;
2599 --cp_unevaluated_operand;
2602 // -------------------------------------------------------------------------- //
2603 // Tentative Parsing
2605 /* Returns nonzero if we are parsing tentatively. */
2607 static inline bool
2608 cp_parser_parsing_tentatively (cp_parser* parser)
2610 return parser->context->next != NULL;
2613 /* Returns nonzero if TOKEN is a string literal. */
2615 static bool
2616 cp_parser_is_pure_string_literal (cp_token* token)
2618 return (token->type == CPP_STRING ||
2619 token->type == CPP_STRING16 ||
2620 token->type == CPP_STRING32 ||
2621 token->type == CPP_WSTRING ||
2622 token->type == CPP_UTF8STRING);
2625 /* Returns nonzero if TOKEN is a string literal
2626 of a user-defined string literal. */
2628 static bool
2629 cp_parser_is_string_literal (cp_token* token)
2631 return (cp_parser_is_pure_string_literal (token) ||
2632 token->type == CPP_STRING_USERDEF ||
2633 token->type == CPP_STRING16_USERDEF ||
2634 token->type == CPP_STRING32_USERDEF ||
2635 token->type == CPP_WSTRING_USERDEF ||
2636 token->type == CPP_UTF8STRING_USERDEF);
2639 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2641 static bool
2642 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2644 return token->keyword == keyword;
2647 /* If not parsing tentatively, issue a diagnostic of the form
2648 FILE:LINE: MESSAGE before TOKEN
2649 where TOKEN is the next token in the input stream. MESSAGE
2650 (specified by the caller) is usually of the form "expected
2651 OTHER-TOKEN". */
2653 static void
2654 cp_parser_error (cp_parser* parser, const char* gmsgid)
2656 if (!cp_parser_simulate_error (parser))
2658 cp_token *token = cp_lexer_peek_token (parser->lexer);
2659 /* This diagnostic makes more sense if it is tagged to the line
2660 of the token we just peeked at. */
2661 cp_lexer_set_source_position_from_token (token);
2663 if (token->type == CPP_PRAGMA)
2665 error_at (token->location,
2666 "%<#pragma%> is not allowed here");
2667 cp_parser_skip_to_pragma_eol (parser, token);
2668 return;
2671 c_parse_error (gmsgid,
2672 /* Because c_parser_error does not understand
2673 CPP_KEYWORD, keywords are treated like
2674 identifiers. */
2675 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2676 token->u.value, token->flags);
2680 /* Issue an error about name-lookup failing. NAME is the
2681 IDENTIFIER_NODE DECL is the result of
2682 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2683 the thing that we hoped to find. */
2685 static void
2686 cp_parser_name_lookup_error (cp_parser* parser,
2687 tree name,
2688 tree decl,
2689 name_lookup_error desired,
2690 location_t location)
2692 /* If name lookup completely failed, tell the user that NAME was not
2693 declared. */
2694 if (decl == error_mark_node)
2696 if (parser->scope && parser->scope != global_namespace)
2697 error_at (location, "%<%E::%E%> has not been declared",
2698 parser->scope, name);
2699 else if (parser->scope == global_namespace)
2700 error_at (location, "%<::%E%> has not been declared", name);
2701 else if (parser->object_scope
2702 && !CLASS_TYPE_P (parser->object_scope))
2703 error_at (location, "request for member %qE in non-class type %qT",
2704 name, parser->object_scope);
2705 else if (parser->object_scope)
2706 error_at (location, "%<%T::%E%> has not been declared",
2707 parser->object_scope, name);
2708 else
2709 error_at (location, "%qE has not been declared", name);
2711 else if (parser->scope && parser->scope != global_namespace)
2713 switch (desired)
2715 case NLE_TYPE:
2716 error_at (location, "%<%E::%E%> is not a type",
2717 parser->scope, name);
2718 break;
2719 case NLE_CXX98:
2720 error_at (location, "%<%E::%E%> is not a class or namespace",
2721 parser->scope, name);
2722 break;
2723 case NLE_NOT_CXX98:
2724 error_at (location,
2725 "%<%E::%E%> is not a class, namespace, or enumeration",
2726 parser->scope, name);
2727 break;
2728 default:
2729 gcc_unreachable ();
2733 else if (parser->scope == global_namespace)
2735 switch (desired)
2737 case NLE_TYPE:
2738 error_at (location, "%<::%E%> is not a type", name);
2739 break;
2740 case NLE_CXX98:
2741 error_at (location, "%<::%E%> is not a class or namespace", name);
2742 break;
2743 case NLE_NOT_CXX98:
2744 error_at (location,
2745 "%<::%E%> is not a class, namespace, or enumeration",
2746 name);
2747 break;
2748 default:
2749 gcc_unreachable ();
2752 else
2754 switch (desired)
2756 case NLE_TYPE:
2757 error_at (location, "%qE is not a type", name);
2758 break;
2759 case NLE_CXX98:
2760 error_at (location, "%qE is not a class or namespace", name);
2761 break;
2762 case NLE_NOT_CXX98:
2763 error_at (location,
2764 "%qE is not a class, namespace, or enumeration", name);
2765 break;
2766 default:
2767 gcc_unreachable ();
2772 /* If we are parsing tentatively, remember that an error has occurred
2773 during this tentative parse. Returns true if the error was
2774 simulated; false if a message should be issued by the caller. */
2776 static bool
2777 cp_parser_simulate_error (cp_parser* parser)
2779 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2781 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2782 return true;
2784 return false;
2787 /* This function is called when a type is defined. If type
2788 definitions are forbidden at this point, an error message is
2789 issued. */
2791 static bool
2792 cp_parser_check_type_definition (cp_parser* parser)
2794 /* If types are forbidden here, issue a message. */
2795 if (parser->type_definition_forbidden_message)
2797 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2798 in the message need to be interpreted. */
2799 error (parser->type_definition_forbidden_message);
2800 return false;
2802 return true;
2805 /* This function is called when the DECLARATOR is processed. The TYPE
2806 was a type defined in the decl-specifiers. If it is invalid to
2807 define a type in the decl-specifiers for DECLARATOR, an error is
2808 issued. TYPE_LOCATION is the location of TYPE and is used
2809 for error reporting. */
2811 static void
2812 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2813 tree type, location_t type_location)
2815 /* [dcl.fct] forbids type definitions in return types.
2816 Unfortunately, it's not easy to know whether or not we are
2817 processing a return type until after the fact. */
2818 while (declarator
2819 && (declarator->kind == cdk_pointer
2820 || declarator->kind == cdk_reference
2821 || declarator->kind == cdk_ptrmem))
2822 declarator = declarator->declarator;
2823 if (declarator
2824 && declarator->kind == cdk_function)
2826 error_at (type_location,
2827 "new types may not be defined in a return type");
2828 inform (type_location,
2829 "(perhaps a semicolon is missing after the definition of %qT)",
2830 type);
2834 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2835 "<" in any valid C++ program. If the next token is indeed "<",
2836 issue a message warning the user about what appears to be an
2837 invalid attempt to form a template-id. LOCATION is the location
2838 of the type-specifier (TYPE) */
2840 static void
2841 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2842 tree type,
2843 enum tag_types tag_type,
2844 location_t location)
2846 cp_token_position start = 0;
2848 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2850 if (TYPE_P (type))
2851 error_at (location, "%qT is not a template", type);
2852 else if (identifier_p (type))
2854 if (tag_type != none_type)
2855 error_at (location, "%qE is not a class template", type);
2856 else
2857 error_at (location, "%qE is not a template", type);
2859 else
2860 error_at (location, "invalid template-id");
2861 /* Remember the location of the invalid "<". */
2862 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2863 start = cp_lexer_token_position (parser->lexer, true);
2864 /* Consume the "<". */
2865 cp_lexer_consume_token (parser->lexer);
2866 /* Parse the template arguments. */
2867 cp_parser_enclosed_template_argument_list (parser);
2868 /* Permanently remove the invalid template arguments so that
2869 this error message is not issued again. */
2870 if (start)
2871 cp_lexer_purge_tokens_after (parser->lexer, start);
2875 /* If parsing an integral constant-expression, issue an error message
2876 about the fact that THING appeared and return true. Otherwise,
2877 return false. In either case, set
2878 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2880 static bool
2881 cp_parser_non_integral_constant_expression (cp_parser *parser,
2882 non_integral_constant thing)
2884 parser->non_integral_constant_expression_p = true;
2885 if (parser->integral_constant_expression_p)
2887 if (!parser->allow_non_integral_constant_expression_p)
2889 const char *msg = NULL;
2890 switch (thing)
2892 case NIC_FLOAT:
2893 error ("floating-point literal "
2894 "cannot appear in a constant-expression");
2895 return true;
2896 case NIC_CAST:
2897 error ("a cast to a type other than an integral or "
2898 "enumeration type cannot appear in a "
2899 "constant-expression");
2900 return true;
2901 case NIC_TYPEID:
2902 error ("%<typeid%> operator "
2903 "cannot appear in a constant-expression");
2904 return true;
2905 case NIC_NCC:
2906 error ("non-constant compound literals "
2907 "cannot appear in a constant-expression");
2908 return true;
2909 case NIC_FUNC_CALL:
2910 error ("a function call "
2911 "cannot appear in a constant-expression");
2912 return true;
2913 case NIC_INC:
2914 error ("an increment "
2915 "cannot appear in a constant-expression");
2916 return true;
2917 case NIC_DEC:
2918 error ("an decrement "
2919 "cannot appear in a constant-expression");
2920 return true;
2921 case NIC_ARRAY_REF:
2922 error ("an array reference "
2923 "cannot appear in a constant-expression");
2924 return true;
2925 case NIC_ADDR_LABEL:
2926 error ("the address of a label "
2927 "cannot appear in a constant-expression");
2928 return true;
2929 case NIC_OVERLOADED:
2930 error ("calls to overloaded operators "
2931 "cannot appear in a constant-expression");
2932 return true;
2933 case NIC_ASSIGNMENT:
2934 error ("an assignment cannot appear in a constant-expression");
2935 return true;
2936 case NIC_COMMA:
2937 error ("a comma operator "
2938 "cannot appear in a constant-expression");
2939 return true;
2940 case NIC_CONSTRUCTOR:
2941 error ("a call to a constructor "
2942 "cannot appear in a constant-expression");
2943 return true;
2944 case NIC_TRANSACTION:
2945 error ("a transaction expression "
2946 "cannot appear in a constant-expression");
2947 return true;
2948 case NIC_THIS:
2949 msg = "this";
2950 break;
2951 case NIC_FUNC_NAME:
2952 msg = "__FUNCTION__";
2953 break;
2954 case NIC_PRETTY_FUNC:
2955 msg = "__PRETTY_FUNCTION__";
2956 break;
2957 case NIC_C99_FUNC:
2958 msg = "__func__";
2959 break;
2960 case NIC_VA_ARG:
2961 msg = "va_arg";
2962 break;
2963 case NIC_ARROW:
2964 msg = "->";
2965 break;
2966 case NIC_POINT:
2967 msg = ".";
2968 break;
2969 case NIC_STAR:
2970 msg = "*";
2971 break;
2972 case NIC_ADDR:
2973 msg = "&";
2974 break;
2975 case NIC_PREINCREMENT:
2976 msg = "++";
2977 break;
2978 case NIC_PREDECREMENT:
2979 msg = "--";
2980 break;
2981 case NIC_NEW:
2982 msg = "new";
2983 break;
2984 case NIC_DEL:
2985 msg = "delete";
2986 break;
2987 default:
2988 gcc_unreachable ();
2990 if (msg)
2991 error ("%qs cannot appear in a constant-expression", msg);
2992 return true;
2995 return false;
2998 /* Emit a diagnostic for an invalid type name. This function commits
2999 to the current active tentative parse, if any. (Otherwise, the
3000 problematic construct might be encountered again later, resulting
3001 in duplicate error messages.) LOCATION is the location of ID. */
3003 static void
3004 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3005 location_t location)
3007 tree decl, ambiguous_decls;
3008 cp_parser_commit_to_tentative_parse (parser);
3009 /* Try to lookup the identifier. */
3010 decl = cp_parser_lookup_name (parser, id, none_type,
3011 /*is_template=*/false,
3012 /*is_namespace=*/false,
3013 /*check_dependency=*/true,
3014 &ambiguous_decls, location);
3015 if (ambiguous_decls)
3016 /* If the lookup was ambiguous, an error will already have
3017 been issued. */
3018 return;
3019 /* If the lookup found a template-name, it means that the user forgot
3020 to specify an argument list. Emit a useful error message. */
3021 if (DECL_TYPE_TEMPLATE_P (decl))
3023 error_at (location,
3024 "invalid use of template-name %qE without an argument list",
3025 decl);
3026 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3028 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3029 error_at (location, "invalid use of destructor %qD as a type", id);
3030 else if (TREE_CODE (decl) == TYPE_DECL)
3031 /* Something like 'unsigned A a;' */
3032 error_at (location, "invalid combination of multiple type-specifiers");
3033 else if (!parser->scope)
3035 /* Issue an error message. */
3036 error_at (location, "%qE does not name a type", id);
3037 /* If we're in a template class, it's possible that the user was
3038 referring to a type from a base class. For example:
3040 template <typename T> struct A { typedef T X; };
3041 template <typename T> struct B : public A<T> { X x; };
3043 The user should have said "typename A<T>::X". */
3044 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3045 inform (location, "C++11 %<constexpr%> only available with "
3046 "-std=c++11 or -std=gnu++11");
3047 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3048 inform (location, "C++11 %<noexcept%> only available with "
3049 "-std=c++11 or -std=gnu++11");
3050 else if (cxx_dialect < cxx11
3051 && TREE_CODE (id) == IDENTIFIER_NODE
3052 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3053 inform (location, "C++11 %<thread_local%> only available with "
3054 "-std=c++11 or -std=gnu++11");
3055 else if (processing_template_decl && current_class_type
3056 && TYPE_BINFO (current_class_type))
3058 tree b;
3060 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3062 b = TREE_CHAIN (b))
3064 tree base_type = BINFO_TYPE (b);
3065 if (CLASS_TYPE_P (base_type)
3066 && dependent_type_p (base_type))
3068 tree field;
3069 /* Go from a particular instantiation of the
3070 template (which will have an empty TYPE_FIELDs),
3071 to the main version. */
3072 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3073 for (field = TYPE_FIELDS (base_type);
3074 field;
3075 field = DECL_CHAIN (field))
3076 if (TREE_CODE (field) == TYPE_DECL
3077 && DECL_NAME (field) == id)
3079 inform (location,
3080 "(perhaps %<typename %T::%E%> was intended)",
3081 BINFO_TYPE (b), id);
3082 break;
3084 if (field)
3085 break;
3090 /* Here we diagnose qualified-ids where the scope is actually correct,
3091 but the identifier does not resolve to a valid type name. */
3092 else if (parser->scope != error_mark_node)
3094 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3096 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3097 error_at (location_of (id),
3098 "%qE in namespace %qE does not name a template type",
3099 id, parser->scope);
3100 else
3101 error_at (location_of (id),
3102 "%qE in namespace %qE does not name a type",
3103 id, parser->scope);
3104 if (DECL_P (decl))
3105 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3107 else if (CLASS_TYPE_P (parser->scope)
3108 && constructor_name_p (id, parser->scope))
3110 /* A<T>::A<T>() */
3111 error_at (location, "%<%T::%E%> names the constructor, not"
3112 " the type", parser->scope, id);
3113 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3114 error_at (location, "and %qT has no template constructors",
3115 parser->scope);
3117 else if (TYPE_P (parser->scope)
3118 && dependent_scope_p (parser->scope))
3119 error_at (location, "need %<typename%> before %<%T::%E%> because "
3120 "%qT is a dependent scope",
3121 parser->scope, id, parser->scope);
3122 else if (TYPE_P (parser->scope))
3124 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3125 error_at (location_of (id),
3126 "%qE in %q#T does not name a template type",
3127 id, parser->scope);
3128 else
3129 error_at (location_of (id),
3130 "%qE in %q#T does not name a type",
3131 id, parser->scope);
3132 if (DECL_P (decl))
3133 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3135 else
3136 gcc_unreachable ();
3140 /* Check for a common situation where a type-name should be present,
3141 but is not, and issue a sensible error message. Returns true if an
3142 invalid type-name was detected.
3144 The situation handled by this function are variable declarations of the
3145 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3146 Usually, `ID' should name a type, but if we got here it means that it
3147 does not. We try to emit the best possible error message depending on
3148 how exactly the id-expression looks like. */
3150 static bool
3151 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3153 tree id;
3154 cp_token *token = cp_lexer_peek_token (parser->lexer);
3156 /* Avoid duplicate error about ambiguous lookup. */
3157 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3159 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3160 if (next->type == CPP_NAME && next->error_reported)
3161 goto out;
3164 cp_parser_parse_tentatively (parser);
3165 id = cp_parser_id_expression (parser,
3166 /*template_keyword_p=*/false,
3167 /*check_dependency_p=*/true,
3168 /*template_p=*/NULL,
3169 /*declarator_p=*/true,
3170 /*optional_p=*/false);
3171 /* If the next token is a (, this is a function with no explicit return
3172 type, i.e. constructor, destructor or conversion op. */
3173 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3174 || TREE_CODE (id) == TYPE_DECL)
3176 cp_parser_abort_tentative_parse (parser);
3177 return false;
3179 if (!cp_parser_parse_definitely (parser))
3180 return false;
3182 /* Emit a diagnostic for the invalid type. */
3183 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3184 out:
3185 /* If we aren't in the middle of a declarator (i.e. in a
3186 parameter-declaration-clause), skip to the end of the declaration;
3187 there's no point in trying to process it. */
3188 if (!parser->in_declarator_p)
3189 cp_parser_skip_to_end_of_block_or_statement (parser);
3190 return true;
3193 /* Consume tokens up to, and including, the next non-nested closing `)'.
3194 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3195 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3196 found an unnested token of that type. */
3198 static int
3199 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3200 bool recovering,
3201 cpp_ttype or_ttype,
3202 bool consume_paren)
3204 unsigned paren_depth = 0;
3205 unsigned brace_depth = 0;
3206 unsigned square_depth = 0;
3208 if (recovering && or_ttype == CPP_EOF
3209 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3210 return 0;
3212 while (true)
3214 cp_token * token = cp_lexer_peek_token (parser->lexer);
3216 /* Have we found what we're looking for before the closing paren? */
3217 if (token->type == or_ttype && or_ttype != CPP_EOF
3218 && !brace_depth && !paren_depth && !square_depth)
3219 return -1;
3221 switch (token->type)
3223 case CPP_EOF:
3224 case CPP_PRAGMA_EOL:
3225 /* If we've run out of tokens, then there is no closing `)'. */
3226 return 0;
3228 /* This is good for lambda expression capture-lists. */
3229 case CPP_OPEN_SQUARE:
3230 ++square_depth;
3231 break;
3232 case CPP_CLOSE_SQUARE:
3233 if (!square_depth--)
3234 return 0;
3235 break;
3237 case CPP_SEMICOLON:
3238 /* This matches the processing in skip_to_end_of_statement. */
3239 if (!brace_depth)
3240 return 0;
3241 break;
3243 case CPP_OPEN_BRACE:
3244 ++brace_depth;
3245 break;
3246 case CPP_CLOSE_BRACE:
3247 if (!brace_depth--)
3248 return 0;
3249 break;
3251 case CPP_OPEN_PAREN:
3252 if (!brace_depth)
3253 ++paren_depth;
3254 break;
3256 case CPP_CLOSE_PAREN:
3257 if (!brace_depth && !paren_depth--)
3259 if (consume_paren)
3260 cp_lexer_consume_token (parser->lexer);
3261 return 1;
3263 break;
3265 default:
3266 break;
3269 /* Consume the token. */
3270 cp_lexer_consume_token (parser->lexer);
3274 /* Consume tokens up to, and including, the next non-nested closing `)'.
3275 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3276 are doing error recovery. Returns -1 if OR_COMMA is true and we
3277 found an unnested token of that type. */
3279 static int
3280 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3281 bool recovering,
3282 bool or_comma,
3283 bool consume_paren)
3285 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3286 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3287 ttype, consume_paren);
3290 /* Consume tokens until we reach the end of the current statement.
3291 Normally, that will be just before consuming a `;'. However, if a
3292 non-nested `}' comes first, then we stop before consuming that. */
3294 static void
3295 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3297 unsigned nesting_depth = 0;
3299 /* Unwind generic function template scope if necessary. */
3300 if (parser->fully_implicit_function_template_p)
3301 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3303 while (true)
3305 cp_token *token = cp_lexer_peek_token (parser->lexer);
3307 switch (token->type)
3309 case CPP_EOF:
3310 case CPP_PRAGMA_EOL:
3311 /* If we've run out of tokens, stop. */
3312 return;
3314 case CPP_SEMICOLON:
3315 /* If the next token is a `;', we have reached the end of the
3316 statement. */
3317 if (!nesting_depth)
3318 return;
3319 break;
3321 case CPP_CLOSE_BRACE:
3322 /* If this is a non-nested '}', stop before consuming it.
3323 That way, when confronted with something like:
3325 { 3 + }
3327 we stop before consuming the closing '}', even though we
3328 have not yet reached a `;'. */
3329 if (nesting_depth == 0)
3330 return;
3332 /* If it is the closing '}' for a block that we have
3333 scanned, stop -- but only after consuming the token.
3334 That way given:
3336 void f g () { ... }
3337 typedef int I;
3339 we will stop after the body of the erroneously declared
3340 function, but before consuming the following `typedef'
3341 declaration. */
3342 if (--nesting_depth == 0)
3344 cp_lexer_consume_token (parser->lexer);
3345 return;
3348 case CPP_OPEN_BRACE:
3349 ++nesting_depth;
3350 break;
3352 default:
3353 break;
3356 /* Consume the token. */
3357 cp_lexer_consume_token (parser->lexer);
3361 /* This function is called at the end of a statement or declaration.
3362 If the next token is a semicolon, it is consumed; otherwise, error
3363 recovery is attempted. */
3365 static void
3366 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3368 /* Look for the trailing `;'. */
3369 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3371 /* If there is additional (erroneous) input, skip to the end of
3372 the statement. */
3373 cp_parser_skip_to_end_of_statement (parser);
3374 /* If the next token is now a `;', consume it. */
3375 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3376 cp_lexer_consume_token (parser->lexer);
3380 /* Skip tokens until we have consumed an entire block, or until we
3381 have consumed a non-nested `;'. */
3383 static void
3384 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3386 int nesting_depth = 0;
3388 /* Unwind generic function template scope if necessary. */
3389 if (parser->fully_implicit_function_template_p)
3390 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3392 while (nesting_depth >= 0)
3394 cp_token *token = cp_lexer_peek_token (parser->lexer);
3396 switch (token->type)
3398 case CPP_EOF:
3399 case CPP_PRAGMA_EOL:
3400 /* If we've run out of tokens, stop. */
3401 return;
3403 case CPP_SEMICOLON:
3404 /* Stop if this is an unnested ';'. */
3405 if (!nesting_depth)
3406 nesting_depth = -1;
3407 break;
3409 case CPP_CLOSE_BRACE:
3410 /* Stop if this is an unnested '}', or closes the outermost
3411 nesting level. */
3412 nesting_depth--;
3413 if (nesting_depth < 0)
3414 return;
3415 if (!nesting_depth)
3416 nesting_depth = -1;
3417 break;
3419 case CPP_OPEN_BRACE:
3420 /* Nest. */
3421 nesting_depth++;
3422 break;
3424 default:
3425 break;
3428 /* Consume the token. */
3429 cp_lexer_consume_token (parser->lexer);
3433 /* Skip tokens until a non-nested closing curly brace is the next
3434 token, or there are no more tokens. Return true in the first case,
3435 false otherwise. */
3437 static bool
3438 cp_parser_skip_to_closing_brace (cp_parser *parser)
3440 unsigned nesting_depth = 0;
3442 while (true)
3444 cp_token *token = cp_lexer_peek_token (parser->lexer);
3446 switch (token->type)
3448 case CPP_EOF:
3449 case CPP_PRAGMA_EOL:
3450 /* If we've run out of tokens, stop. */
3451 return false;
3453 case CPP_CLOSE_BRACE:
3454 /* If the next token is a non-nested `}', then we have reached
3455 the end of the current block. */
3456 if (nesting_depth-- == 0)
3457 return true;
3458 break;
3460 case CPP_OPEN_BRACE:
3461 /* If it the next token is a `{', then we are entering a new
3462 block. Consume the entire block. */
3463 ++nesting_depth;
3464 break;
3466 default:
3467 break;
3470 /* Consume the token. */
3471 cp_lexer_consume_token (parser->lexer);
3475 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3476 parameter is the PRAGMA token, allowing us to purge the entire pragma
3477 sequence. */
3479 static void
3480 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3482 cp_token *token;
3484 parser->lexer->in_pragma = false;
3487 token = cp_lexer_consume_token (parser->lexer);
3488 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3490 /* Ensure that the pragma is not parsed again. */
3491 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3494 /* Require pragma end of line, resyncing with it as necessary. The
3495 arguments are as for cp_parser_skip_to_pragma_eol. */
3497 static void
3498 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3500 parser->lexer->in_pragma = false;
3501 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3502 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3505 /* This is a simple wrapper around make_typename_type. When the id is
3506 an unresolved identifier node, we can provide a superior diagnostic
3507 using cp_parser_diagnose_invalid_type_name. */
3509 static tree
3510 cp_parser_make_typename_type (cp_parser *parser, tree id,
3511 location_t id_location)
3513 tree result;
3514 if (identifier_p (id))
3516 result = make_typename_type (parser->scope, id, typename_type,
3517 /*complain=*/tf_none);
3518 if (result == error_mark_node)
3519 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3520 return result;
3522 return make_typename_type (parser->scope, id, typename_type, tf_error);
3525 /* This is a wrapper around the
3526 make_{pointer,ptrmem,reference}_declarator functions that decides
3527 which one to call based on the CODE and CLASS_TYPE arguments. The
3528 CODE argument should be one of the values returned by
3529 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3530 appertain to the pointer or reference. */
3532 static cp_declarator *
3533 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3534 cp_cv_quals cv_qualifiers,
3535 cp_declarator *target,
3536 tree attributes)
3538 if (code == ERROR_MARK)
3539 return cp_error_declarator;
3541 if (code == INDIRECT_REF)
3542 if (class_type == NULL_TREE)
3543 return make_pointer_declarator (cv_qualifiers, target, attributes);
3544 else
3545 return make_ptrmem_declarator (cv_qualifiers, class_type,
3546 target, attributes);
3547 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3548 return make_reference_declarator (cv_qualifiers, target,
3549 false, attributes);
3550 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3551 return make_reference_declarator (cv_qualifiers, target,
3552 true, attributes);
3553 gcc_unreachable ();
3556 /* Create a new C++ parser. */
3558 static cp_parser *
3559 cp_parser_new (void)
3561 cp_parser *parser;
3562 cp_lexer *lexer;
3563 unsigned i;
3565 /* cp_lexer_new_main is called before doing GC allocation because
3566 cp_lexer_new_main might load a PCH file. */
3567 lexer = cp_lexer_new_main ();
3569 /* Initialize the binops_by_token so that we can get the tree
3570 directly from the token. */
3571 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3572 binops_by_token[binops[i].token_type] = binops[i];
3574 parser = ggc_cleared_alloc<cp_parser> ();
3575 parser->lexer = lexer;
3576 parser->context = cp_parser_context_new (NULL);
3578 /* For now, we always accept GNU extensions. */
3579 parser->allow_gnu_extensions_p = 1;
3581 /* The `>' token is a greater-than operator, not the end of a
3582 template-id. */
3583 parser->greater_than_is_operator_p = true;
3585 parser->default_arg_ok_p = true;
3587 /* We are not parsing a constant-expression. */
3588 parser->integral_constant_expression_p = false;
3589 parser->allow_non_integral_constant_expression_p = false;
3590 parser->non_integral_constant_expression_p = false;
3592 /* Local variable names are not forbidden. */
3593 parser->local_variables_forbidden_p = false;
3595 /* We are not processing an `extern "C"' declaration. */
3596 parser->in_unbraced_linkage_specification_p = false;
3598 /* We are not processing a declarator. */
3599 parser->in_declarator_p = false;
3601 /* We are not processing a template-argument-list. */
3602 parser->in_template_argument_list_p = false;
3604 /* We are not in an iteration statement. */
3605 parser->in_statement = 0;
3607 /* We are not in a switch statement. */
3608 parser->in_switch_statement_p = false;
3610 /* We are not parsing a type-id inside an expression. */
3611 parser->in_type_id_in_expr_p = false;
3613 /* Declarations aren't implicitly extern "C". */
3614 parser->implicit_extern_c = false;
3616 /* String literals should be translated to the execution character set. */
3617 parser->translate_strings_p = true;
3619 /* We are not parsing a function body. */
3620 parser->in_function_body = false;
3622 /* We can correct until told otherwise. */
3623 parser->colon_corrects_to_scope_p = true;
3625 /* The unparsed function queue is empty. */
3626 push_unparsed_function_queues (parser);
3628 /* There are no classes being defined. */
3629 parser->num_classes_being_defined = 0;
3631 /* No template parameters apply. */
3632 parser->num_template_parameter_lists = 0;
3634 /* Not declaring an implicit function template. */
3635 parser->auto_is_implicit_function_template_parm_p = false;
3636 parser->fully_implicit_function_template_p = false;
3637 parser->implicit_template_parms = 0;
3638 parser->implicit_template_scope = 0;
3640 /* Active OpenACC routine clauses. */
3641 parser->oacc_routine = NULL;
3643 /* Allow constrained-type-specifiers. */
3644 parser->prevent_constrained_type_specifiers = 0;
3646 return parser;
3649 /* Create a cp_lexer structure which will emit the tokens in CACHE
3650 and push it onto the parser's lexer stack. This is used for delayed
3651 parsing of in-class method bodies and default arguments, and should
3652 not be confused with tentative parsing. */
3653 static void
3654 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3656 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3657 lexer->next = parser->lexer;
3658 parser->lexer = lexer;
3660 /* Move the current source position to that of the first token in the
3661 new lexer. */
3662 cp_lexer_set_source_position_from_token (lexer->next_token);
3665 /* Pop the top lexer off the parser stack. This is never used for the
3666 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3667 static void
3668 cp_parser_pop_lexer (cp_parser *parser)
3670 cp_lexer *lexer = parser->lexer;
3671 parser->lexer = lexer->next;
3672 cp_lexer_destroy (lexer);
3674 /* Put the current source position back where it was before this
3675 lexer was pushed. */
3676 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3679 /* Lexical conventions [gram.lex] */
3681 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3682 identifier. */
3684 static tree
3685 cp_parser_identifier (cp_parser* parser)
3687 cp_token *token;
3689 /* Look for the identifier. */
3690 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3691 /* Return the value. */
3692 return token ? token->u.value : error_mark_node;
3695 /* Parse a sequence of adjacent string constants. Returns a
3696 TREE_STRING representing the combined, nul-terminated string
3697 constant. If TRANSLATE is true, translate the string to the
3698 execution character set. If WIDE_OK is true, a wide string is
3699 invalid here.
3701 C++98 [lex.string] says that if a narrow string literal token is
3702 adjacent to a wide string literal token, the behavior is undefined.
3703 However, C99 6.4.5p4 says that this results in a wide string literal.
3704 We follow C99 here, for consistency with the C front end.
3706 This code is largely lifted from lex_string() in c-lex.c.
3708 FUTURE: ObjC++ will need to handle @-strings here. */
3709 static tree
3710 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3711 bool lookup_udlit = true)
3713 tree value;
3714 size_t count;
3715 struct obstack str_ob;
3716 cpp_string str, istr, *strs;
3717 cp_token *tok;
3718 enum cpp_ttype type, curr_type;
3719 int have_suffix_p = 0;
3720 tree string_tree;
3721 tree suffix_id = NULL_TREE;
3722 bool curr_tok_is_userdef_p = false;
3724 tok = cp_lexer_peek_token (parser->lexer);
3725 if (!cp_parser_is_string_literal (tok))
3727 cp_parser_error (parser, "expected string-literal");
3728 return error_mark_node;
3731 if (cpp_userdef_string_p (tok->type))
3733 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3734 curr_type = cpp_userdef_string_remove_type (tok->type);
3735 curr_tok_is_userdef_p = true;
3737 else
3739 string_tree = tok->u.value;
3740 curr_type = tok->type;
3742 type = curr_type;
3744 /* Try to avoid the overhead of creating and destroying an obstack
3745 for the common case of just one string. */
3746 if (!cp_parser_is_string_literal
3747 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3749 cp_lexer_consume_token (parser->lexer);
3751 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3752 str.len = TREE_STRING_LENGTH (string_tree);
3753 count = 1;
3755 if (curr_tok_is_userdef_p)
3757 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3758 have_suffix_p = 1;
3759 curr_type = cpp_userdef_string_remove_type (tok->type);
3761 else
3762 curr_type = tok->type;
3764 strs = &str;
3766 else
3768 gcc_obstack_init (&str_ob);
3769 count = 0;
3773 cp_lexer_consume_token (parser->lexer);
3774 count++;
3775 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3776 str.len = TREE_STRING_LENGTH (string_tree);
3778 if (curr_tok_is_userdef_p)
3780 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3781 if (have_suffix_p == 0)
3783 suffix_id = curr_suffix_id;
3784 have_suffix_p = 1;
3786 else if (have_suffix_p == 1
3787 && curr_suffix_id != suffix_id)
3789 error ("inconsistent user-defined literal suffixes"
3790 " %qD and %qD in string literal",
3791 suffix_id, curr_suffix_id);
3792 have_suffix_p = -1;
3794 curr_type = cpp_userdef_string_remove_type (tok->type);
3796 else
3797 curr_type = tok->type;
3799 if (type != curr_type)
3801 if (type == CPP_STRING)
3802 type = curr_type;
3803 else if (curr_type != CPP_STRING)
3804 error_at (tok->location,
3805 "unsupported non-standard concatenation "
3806 "of string literals");
3809 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3811 tok = cp_lexer_peek_token (parser->lexer);
3812 if (cpp_userdef_string_p (tok->type))
3814 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3815 curr_type = cpp_userdef_string_remove_type (tok->type);
3816 curr_tok_is_userdef_p = true;
3818 else
3820 string_tree = tok->u.value;
3821 curr_type = tok->type;
3822 curr_tok_is_userdef_p = false;
3825 while (cp_parser_is_string_literal (tok));
3827 strs = (cpp_string *) obstack_finish (&str_ob);
3830 if (type != CPP_STRING && !wide_ok)
3832 cp_parser_error (parser, "a wide string is invalid in this context");
3833 type = CPP_STRING;
3836 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3837 (parse_in, strs, count, &istr, type))
3839 value = build_string (istr.len, (const char *)istr.text);
3840 free (CONST_CAST (unsigned char *, istr.text));
3842 switch (type)
3844 default:
3845 case CPP_STRING:
3846 case CPP_UTF8STRING:
3847 TREE_TYPE (value) = char_array_type_node;
3848 break;
3849 case CPP_STRING16:
3850 TREE_TYPE (value) = char16_array_type_node;
3851 break;
3852 case CPP_STRING32:
3853 TREE_TYPE (value) = char32_array_type_node;
3854 break;
3855 case CPP_WSTRING:
3856 TREE_TYPE (value) = wchar_array_type_node;
3857 break;
3860 value = fix_string_type (value);
3862 if (have_suffix_p)
3864 tree literal = build_userdef_literal (suffix_id, value,
3865 OT_NONE, NULL_TREE);
3866 if (lookup_udlit)
3867 value = cp_parser_userdef_string_literal (literal);
3868 else
3869 value = literal;
3872 else
3873 /* cpp_interpret_string has issued an error. */
3874 value = error_mark_node;
3876 if (count > 1)
3877 obstack_free (&str_ob, 0);
3879 return value;
3882 /* Look up a literal operator with the name and the exact arguments. */
3884 static tree
3885 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3887 tree decl, fns;
3888 decl = lookup_name (name);
3889 if (!decl || !is_overloaded_fn (decl))
3890 return error_mark_node;
3892 for (fns = decl; fns; fns = OVL_NEXT (fns))
3894 unsigned int ix;
3895 bool found = true;
3896 tree fn = OVL_CURRENT (fns);
3897 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3898 if (parmtypes != NULL_TREE)
3900 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3901 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3903 tree tparm = TREE_VALUE (parmtypes);
3904 tree targ = TREE_TYPE ((*args)[ix]);
3905 bool ptr = TYPE_PTR_P (tparm);
3906 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3907 if ((ptr || arr || !same_type_p (tparm, targ))
3908 && (!ptr || !arr
3909 || !same_type_p (TREE_TYPE (tparm),
3910 TREE_TYPE (targ))))
3911 found = false;
3913 if (found
3914 && ix == vec_safe_length (args)
3915 /* May be this should be sufficient_parms_p instead,
3916 depending on how exactly should user-defined literals
3917 work in presence of default arguments on the literal
3918 operator parameters. */
3919 && parmtypes == void_list_node)
3920 return decl;
3924 return error_mark_node;
3927 /* Parse a user-defined char constant. Returns a call to a user-defined
3928 literal operator taking the character as an argument. */
3930 static tree
3931 cp_parser_userdef_char_literal (cp_parser *parser)
3933 cp_token *token = cp_lexer_consume_token (parser->lexer);
3934 tree literal = token->u.value;
3935 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3936 tree value = USERDEF_LITERAL_VALUE (literal);
3937 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3938 tree decl, result;
3940 /* Build up a call to the user-defined operator */
3941 /* Lookup the name we got back from the id-expression. */
3942 vec<tree, va_gc> *args = make_tree_vector ();
3943 vec_safe_push (args, value);
3944 decl = lookup_literal_operator (name, args);
3945 if (!decl || decl == error_mark_node)
3947 error ("unable to find character literal operator %qD with %qT argument",
3948 name, TREE_TYPE (value));
3949 release_tree_vector (args);
3950 return error_mark_node;
3952 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3953 release_tree_vector (args);
3954 return result;
3957 /* A subroutine of cp_parser_userdef_numeric_literal to
3958 create a char... template parameter pack from a string node. */
3960 static tree
3961 make_char_string_pack (tree value)
3963 tree charvec;
3964 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3965 const char *str = TREE_STRING_POINTER (value);
3966 int i, len = TREE_STRING_LENGTH (value) - 1;
3967 tree argvec = make_tree_vec (1);
3969 /* Fill in CHARVEC with all of the parameters. */
3970 charvec = make_tree_vec (len);
3971 for (i = 0; i < len; ++i)
3972 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3974 /* Build the argument packs. */
3975 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3976 TREE_TYPE (argpack) = char_type_node;
3978 TREE_VEC_ELT (argvec, 0) = argpack;
3980 return argvec;
3983 /* A subroutine of cp_parser_userdef_numeric_literal to
3984 create a char... template parameter pack from a string node. */
3986 static tree
3987 make_string_pack (tree value)
3989 tree charvec;
3990 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3991 const unsigned char *str
3992 = (const unsigned char *) TREE_STRING_POINTER (value);
3993 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3994 int len = TREE_STRING_LENGTH (value) / sz - 1;
3995 tree argvec = make_tree_vec (2);
3997 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3998 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4000 /* First template parm is character type. */
4001 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4003 /* Fill in CHARVEC with all of the parameters. */
4004 charvec = make_tree_vec (len);
4005 for (int i = 0; i < len; ++i)
4006 TREE_VEC_ELT (charvec, i)
4007 = double_int_to_tree (str_char_type_node,
4008 double_int::from_buffer (str + i * sz, sz));
4010 /* Build the argument packs. */
4011 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4012 TREE_TYPE (argpack) = str_char_type_node;
4014 TREE_VEC_ELT (argvec, 1) = argpack;
4016 return argvec;
4019 /* Parse a user-defined numeric constant. returns a call to a user-defined
4020 literal operator. */
4022 static tree
4023 cp_parser_userdef_numeric_literal (cp_parser *parser)
4025 cp_token *token = cp_lexer_consume_token (parser->lexer);
4026 tree literal = token->u.value;
4027 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4028 tree value = USERDEF_LITERAL_VALUE (literal);
4029 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4030 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4031 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4032 tree decl, result;
4033 vec<tree, va_gc> *args;
4035 /* Look for a literal operator taking the exact type of numeric argument
4036 as the literal value. */
4037 args = make_tree_vector ();
4038 vec_safe_push (args, value);
4039 decl = lookup_literal_operator (name, args);
4040 if (decl && decl != error_mark_node)
4042 result = finish_call_expr (decl, &args, false, true,
4043 tf_warning_or_error);
4045 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4047 warning_at (token->location, OPT_Woverflow,
4048 "integer literal exceeds range of %qT type",
4049 long_long_unsigned_type_node);
4051 else
4053 if (overflow > 0)
4054 warning_at (token->location, OPT_Woverflow,
4055 "floating literal exceeds range of %qT type",
4056 long_double_type_node);
4057 else if (overflow < 0)
4058 warning_at (token->location, OPT_Woverflow,
4059 "floating literal truncated to zero");
4062 release_tree_vector (args);
4063 return result;
4065 release_tree_vector (args);
4067 /* If the numeric argument didn't work, look for a raw literal
4068 operator taking a const char* argument consisting of the number
4069 in string format. */
4070 args = make_tree_vector ();
4071 vec_safe_push (args, num_string);
4072 decl = lookup_literal_operator (name, args);
4073 if (decl && decl != error_mark_node)
4075 result = finish_call_expr (decl, &args, false, true,
4076 tf_warning_or_error);
4077 release_tree_vector (args);
4078 return result;
4080 release_tree_vector (args);
4082 /* If the raw literal didn't work, look for a non-type template
4083 function with parameter pack char.... Call the function with
4084 template parameter characters representing the number. */
4085 args = make_tree_vector ();
4086 decl = lookup_literal_operator (name, args);
4087 if (decl && decl != error_mark_node)
4089 tree tmpl_args = make_char_string_pack (num_string);
4090 decl = lookup_template_function (decl, tmpl_args);
4091 result = finish_call_expr (decl, &args, false, true,
4092 tf_warning_or_error);
4093 release_tree_vector (args);
4094 return result;
4097 release_tree_vector (args);
4099 error ("unable to find numeric literal operator %qD", name);
4100 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4101 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4102 "to enable more built-in suffixes");
4103 return error_mark_node;
4106 /* Parse a user-defined string constant. Returns a call to a user-defined
4107 literal operator taking a character pointer and the length of the string
4108 as arguments. */
4110 static tree
4111 cp_parser_userdef_string_literal (tree literal)
4113 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4114 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4115 tree value = USERDEF_LITERAL_VALUE (literal);
4116 int len = TREE_STRING_LENGTH (value)
4117 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4118 tree decl, result;
4119 vec<tree, va_gc> *args;
4121 /* Build up a call to the user-defined operator. */
4122 /* Lookup the name we got back from the id-expression. */
4123 args = make_tree_vector ();
4124 vec_safe_push (args, value);
4125 vec_safe_push (args, build_int_cst (size_type_node, len));
4126 decl = lookup_literal_operator (name, args);
4128 if (decl && decl != error_mark_node)
4130 result = finish_call_expr (decl, &args, false, true,
4131 tf_warning_or_error);
4132 release_tree_vector (args);
4133 return result;
4135 release_tree_vector (args);
4137 /* Look for a template function with typename parameter CharT
4138 and parameter pack CharT... Call the function with
4139 template parameter characters representing the string. */
4140 args = make_tree_vector ();
4141 decl = lookup_literal_operator (name, args);
4142 if (decl && decl != error_mark_node)
4144 tree tmpl_args = make_string_pack (value);
4145 decl = lookup_template_function (decl, tmpl_args);
4146 result = finish_call_expr (decl, &args, false, true,
4147 tf_warning_or_error);
4148 release_tree_vector (args);
4149 return result;
4151 release_tree_vector (args);
4153 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4154 name, TREE_TYPE (value), size_type_node);
4155 return error_mark_node;
4159 /* Basic concepts [gram.basic] */
4161 /* Parse a translation-unit.
4163 translation-unit:
4164 declaration-seq [opt]
4166 Returns TRUE if all went well. */
4168 static bool
4169 cp_parser_translation_unit (cp_parser* parser)
4171 /* The address of the first non-permanent object on the declarator
4172 obstack. */
4173 static void *declarator_obstack_base;
4175 bool success;
4177 /* Create the declarator obstack, if necessary. */
4178 if (!cp_error_declarator)
4180 gcc_obstack_init (&declarator_obstack);
4181 /* Create the error declarator. */
4182 cp_error_declarator = make_declarator (cdk_error);
4183 /* Create the empty parameter list. */
4184 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4185 /* Remember where the base of the declarator obstack lies. */
4186 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4189 cp_parser_declaration_seq_opt (parser);
4191 /* If there are no tokens left then all went well. */
4192 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4194 /* Get rid of the token array; we don't need it any more. */
4195 cp_lexer_destroy (parser->lexer);
4196 parser->lexer = NULL;
4198 /* This file might have been a context that's implicitly extern
4199 "C". If so, pop the lang context. (Only relevant for PCH.) */
4200 if (parser->implicit_extern_c)
4202 pop_lang_context ();
4203 parser->implicit_extern_c = false;
4206 /* Finish up. */
4207 finish_translation_unit ();
4209 success = true;
4211 else
4213 cp_parser_error (parser, "expected declaration");
4214 success = false;
4217 /* Make sure the declarator obstack was fully cleaned up. */
4218 gcc_assert (obstack_next_free (&declarator_obstack)
4219 == declarator_obstack_base);
4221 /* All went well. */
4222 return success;
4225 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4226 decltype context. */
4228 static inline tsubst_flags_t
4229 complain_flags (bool decltype_p)
4231 tsubst_flags_t complain = tf_warning_or_error;
4232 if (decltype_p)
4233 complain |= tf_decltype;
4234 return complain;
4237 /* We're about to parse a collection of statements. If we're currently
4238 parsing tentatively, set up a firewall so that any nested
4239 cp_parser_commit_to_tentative_parse won't affect the current context. */
4241 static cp_token_position
4242 cp_parser_start_tentative_firewall (cp_parser *parser)
4244 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4245 return 0;
4247 cp_parser_parse_tentatively (parser);
4248 cp_parser_commit_to_topmost_tentative_parse (parser);
4249 return cp_lexer_token_position (parser->lexer, false);
4252 /* We've finished parsing the collection of statements. Wrap up the
4253 firewall and replace the relevant tokens with the parsed form. */
4255 static void
4256 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4257 tree expr)
4259 if (!start)
4260 return;
4262 /* Finish the firewall level. */
4263 cp_parser_parse_definitely (parser);
4264 /* And remember the result of the parse for when we try again. */
4265 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4266 token->type = CPP_PREPARSED_EXPR;
4267 token->u.value = expr;
4268 token->keyword = RID_MAX;
4269 cp_lexer_purge_tokens_after (parser->lexer, start);
4272 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4273 enclosing parentheses. */
4275 static tree
4276 cp_parser_statement_expr (cp_parser *parser)
4278 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4280 /* Consume the '('. */
4281 cp_lexer_consume_token (parser->lexer);
4282 /* Start the statement-expression. */
4283 tree expr = begin_stmt_expr ();
4284 /* Parse the compound-statement. */
4285 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4286 /* Finish up. */
4287 expr = finish_stmt_expr (expr, false);
4288 /* Consume the ')'. */
4289 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4290 cp_parser_skip_to_end_of_statement (parser);
4292 cp_parser_end_tentative_firewall (parser, start, expr);
4293 return expr;
4296 /* Expressions [gram.expr] */
4298 /* Parse a fold-operator.
4300 fold-operator:
4301 - * / % ^ & | = < > << >>
4302 = -= *= /= %= ^= &= |= <<= >>=
4303 == != <= >= && || , .* ->*
4305 This returns the tree code corresponding to the matched operator
4306 as an int. When the current token matches a compound assignment
4307 opertor, the resulting tree code is the negative value of the
4308 non-assignment operator. */
4310 static int
4311 cp_parser_fold_operator (cp_token *token)
4313 switch (token->type)
4315 case CPP_PLUS: return PLUS_EXPR;
4316 case CPP_MINUS: return MINUS_EXPR;
4317 case CPP_MULT: return MULT_EXPR;
4318 case CPP_DIV: return TRUNC_DIV_EXPR;
4319 case CPP_MOD: return TRUNC_MOD_EXPR;
4320 case CPP_XOR: return BIT_XOR_EXPR;
4321 case CPP_AND: return BIT_AND_EXPR;
4322 case CPP_OR: return BIT_IOR_EXPR;
4323 case CPP_LSHIFT: return LSHIFT_EXPR;
4324 case CPP_RSHIFT: return RSHIFT_EXPR;
4326 case CPP_EQ: return -NOP_EXPR;
4327 case CPP_PLUS_EQ: return -PLUS_EXPR;
4328 case CPP_MINUS_EQ: return -MINUS_EXPR;
4329 case CPP_MULT_EQ: return -MULT_EXPR;
4330 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4331 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4332 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4333 case CPP_AND_EQ: return -BIT_AND_EXPR;
4334 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4335 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4336 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4338 case CPP_EQ_EQ: return EQ_EXPR;
4339 case CPP_NOT_EQ: return NE_EXPR;
4340 case CPP_LESS: return LT_EXPR;
4341 case CPP_GREATER: return GT_EXPR;
4342 case CPP_LESS_EQ: return LE_EXPR;
4343 case CPP_GREATER_EQ: return GE_EXPR;
4345 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4346 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4348 case CPP_COMMA: return COMPOUND_EXPR;
4350 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4351 case CPP_DEREF_STAR: return MEMBER_REF;
4353 default: return ERROR_MARK;
4357 /* Returns true if CODE indicates a binary expression, which is not allowed in
4358 the LHS of a fold-expression. More codes will need to be added to use this
4359 function in other contexts. */
4361 static bool
4362 is_binary_op (tree_code code)
4364 switch (code)
4366 case PLUS_EXPR:
4367 case POINTER_PLUS_EXPR:
4368 case MINUS_EXPR:
4369 case MULT_EXPR:
4370 case TRUNC_DIV_EXPR:
4371 case TRUNC_MOD_EXPR:
4372 case BIT_XOR_EXPR:
4373 case BIT_AND_EXPR:
4374 case BIT_IOR_EXPR:
4375 case LSHIFT_EXPR:
4376 case RSHIFT_EXPR:
4378 case MODOP_EXPR:
4380 case EQ_EXPR:
4381 case NE_EXPR:
4382 case LE_EXPR:
4383 case GE_EXPR:
4384 case LT_EXPR:
4385 case GT_EXPR:
4387 case TRUTH_ANDIF_EXPR:
4388 case TRUTH_ORIF_EXPR:
4390 case COMPOUND_EXPR:
4392 case DOTSTAR_EXPR:
4393 case MEMBER_REF:
4394 return true;
4396 default:
4397 return false;
4401 /* If the next token is a suitable fold operator, consume it and return as
4402 the function above. */
4404 static int
4405 cp_parser_fold_operator (cp_parser *parser)
4407 cp_token* token = cp_lexer_peek_token (parser->lexer);
4408 int code = cp_parser_fold_operator (token);
4409 if (code != ERROR_MARK)
4410 cp_lexer_consume_token (parser->lexer);
4411 return code;
4414 /* Parse a fold-expression.
4416 fold-expression:
4417 ( ... folding-operator cast-expression)
4418 ( cast-expression folding-operator ... )
4419 ( cast-expression folding operator ... folding-operator cast-expression)
4421 Note that the '(' and ')' are matched in primary expression. */
4423 static tree
4424 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4426 cp_id_kind pidk;
4428 // Left fold.
4429 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4431 cp_lexer_consume_token (parser->lexer);
4432 int op = cp_parser_fold_operator (parser);
4433 if (op == ERROR_MARK)
4435 cp_parser_error (parser, "expected binary operator");
4436 return error_mark_node;
4439 tree expr = cp_parser_cast_expression (parser, false, false,
4440 false, &pidk);
4441 if (expr == error_mark_node)
4442 return error_mark_node;
4443 return finish_left_unary_fold_expr (expr, op);
4446 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4447 int op = cp_parser_fold_operator (parser);
4448 if (op == ERROR_MARK)
4450 cp_parser_error (parser, "expected binary operator");
4451 return error_mark_node;
4454 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4456 cp_parser_error (parser, "expected ...");
4457 return error_mark_node;
4459 cp_lexer_consume_token (parser->lexer);
4461 /* The operands of a fold-expression are cast-expressions, so binary or
4462 conditional expressions are not allowed. We check this here to avoid
4463 tentative parsing. */
4464 if (is_binary_op (TREE_CODE (expr1)))
4465 error_at (location_of (expr1),
4466 "binary expression in operand of fold-expression");
4467 else if (TREE_CODE (expr1) == COND_EXPR)
4468 error_at (location_of (expr1),
4469 "conditional expression in operand of fold-expression");
4471 // Right fold.
4472 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4473 return finish_right_unary_fold_expr (expr1, op);
4475 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4477 cp_parser_error (parser, "mismatched operator in fold-expression");
4478 return error_mark_node;
4480 cp_lexer_consume_token (parser->lexer);
4482 // Binary left or right fold.
4483 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4484 if (expr2 == error_mark_node)
4485 return error_mark_node;
4486 return finish_binary_fold_expr (expr1, expr2, op);
4489 /* Parse a primary-expression.
4491 primary-expression:
4492 literal
4493 this
4494 ( expression )
4495 id-expression
4496 lambda-expression (C++11)
4498 GNU Extensions:
4500 primary-expression:
4501 ( compound-statement )
4502 __builtin_va_arg ( assignment-expression , type-id )
4503 __builtin_offsetof ( type-id , offsetof-expression )
4505 C++ Extensions:
4506 __has_nothrow_assign ( type-id )
4507 __has_nothrow_constructor ( type-id )
4508 __has_nothrow_copy ( type-id )
4509 __has_trivial_assign ( type-id )
4510 __has_trivial_constructor ( type-id )
4511 __has_trivial_copy ( type-id )
4512 __has_trivial_destructor ( type-id )
4513 __has_virtual_destructor ( type-id )
4514 __is_abstract ( type-id )
4515 __is_base_of ( type-id , type-id )
4516 __is_class ( type-id )
4517 __is_empty ( type-id )
4518 __is_enum ( type-id )
4519 __is_final ( type-id )
4520 __is_literal_type ( type-id )
4521 __is_pod ( type-id )
4522 __is_polymorphic ( type-id )
4523 __is_std_layout ( type-id )
4524 __is_trivial ( type-id )
4525 __is_union ( type-id )
4527 Objective-C++ Extension:
4529 primary-expression:
4530 objc-expression
4532 literal:
4533 __null
4535 ADDRESS_P is true iff this expression was immediately preceded by
4536 "&" and therefore might denote a pointer-to-member. CAST_P is true
4537 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4538 true iff this expression is a template argument.
4540 Returns a representation of the expression. Upon return, *IDK
4541 indicates what kind of id-expression (if any) was present. */
4543 static tree
4544 cp_parser_primary_expression (cp_parser *parser,
4545 bool address_p,
4546 bool cast_p,
4547 bool template_arg_p,
4548 bool decltype_p,
4549 cp_id_kind *idk)
4551 cp_token *token = NULL;
4553 /* Assume the primary expression is not an id-expression. */
4554 *idk = CP_ID_KIND_NONE;
4556 /* Peek at the next token. */
4557 token = cp_lexer_peek_token (parser->lexer);
4558 switch ((int) token->type)
4560 /* literal:
4561 integer-literal
4562 character-literal
4563 floating-literal
4564 string-literal
4565 boolean-literal
4566 pointer-literal
4567 user-defined-literal */
4568 case CPP_CHAR:
4569 case CPP_CHAR16:
4570 case CPP_CHAR32:
4571 case CPP_WCHAR:
4572 case CPP_UTF8CHAR:
4573 case CPP_NUMBER:
4574 case CPP_PREPARSED_EXPR:
4575 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4576 return cp_parser_userdef_numeric_literal (parser);
4577 token = cp_lexer_consume_token (parser->lexer);
4578 if (TREE_CODE (token->u.value) == FIXED_CST)
4580 error_at (token->location,
4581 "fixed-point types not supported in C++");
4582 return error_mark_node;
4584 /* Floating-point literals are only allowed in an integral
4585 constant expression if they are cast to an integral or
4586 enumeration type. */
4587 if (TREE_CODE (token->u.value) == REAL_CST
4588 && parser->integral_constant_expression_p
4589 && pedantic)
4591 /* CAST_P will be set even in invalid code like "int(2.7 +
4592 ...)". Therefore, we have to check that the next token
4593 is sure to end the cast. */
4594 if (cast_p)
4596 cp_token *next_token;
4598 next_token = cp_lexer_peek_token (parser->lexer);
4599 if (/* The comma at the end of an
4600 enumerator-definition. */
4601 next_token->type != CPP_COMMA
4602 /* The curly brace at the end of an enum-specifier. */
4603 && next_token->type != CPP_CLOSE_BRACE
4604 /* The end of a statement. */
4605 && next_token->type != CPP_SEMICOLON
4606 /* The end of the cast-expression. */
4607 && next_token->type != CPP_CLOSE_PAREN
4608 /* The end of an array bound. */
4609 && next_token->type != CPP_CLOSE_SQUARE
4610 /* The closing ">" in a template-argument-list. */
4611 && (next_token->type != CPP_GREATER
4612 || parser->greater_than_is_operator_p)
4613 /* C++0x only: A ">>" treated like two ">" tokens,
4614 in a template-argument-list. */
4615 && (next_token->type != CPP_RSHIFT
4616 || (cxx_dialect == cxx98)
4617 || parser->greater_than_is_operator_p))
4618 cast_p = false;
4621 /* If we are within a cast, then the constraint that the
4622 cast is to an integral or enumeration type will be
4623 checked at that point. If we are not within a cast, then
4624 this code is invalid. */
4625 if (!cast_p)
4626 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4628 return token->u.value;
4630 case CPP_CHAR_USERDEF:
4631 case CPP_CHAR16_USERDEF:
4632 case CPP_CHAR32_USERDEF:
4633 case CPP_WCHAR_USERDEF:
4634 case CPP_UTF8CHAR_USERDEF:
4635 return cp_parser_userdef_char_literal (parser);
4637 case CPP_STRING:
4638 case CPP_STRING16:
4639 case CPP_STRING32:
4640 case CPP_WSTRING:
4641 case CPP_UTF8STRING:
4642 case CPP_STRING_USERDEF:
4643 case CPP_STRING16_USERDEF:
4644 case CPP_STRING32_USERDEF:
4645 case CPP_WSTRING_USERDEF:
4646 case CPP_UTF8STRING_USERDEF:
4647 /* ??? Should wide strings be allowed when parser->translate_strings_p
4648 is false (i.e. in attributes)? If not, we can kill the third
4649 argument to cp_parser_string_literal. */
4650 return cp_parser_string_literal (parser,
4651 parser->translate_strings_p,
4652 true);
4654 case CPP_OPEN_PAREN:
4655 /* If we see `( { ' then we are looking at the beginning of
4656 a GNU statement-expression. */
4657 if (cp_parser_allow_gnu_extensions_p (parser)
4658 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4660 /* Statement-expressions are not allowed by the standard. */
4661 pedwarn (token->location, OPT_Wpedantic,
4662 "ISO C++ forbids braced-groups within expressions");
4664 /* And they're not allowed outside of a function-body; you
4665 cannot, for example, write:
4667 int i = ({ int j = 3; j + 1; });
4669 at class or namespace scope. */
4670 if (!parser->in_function_body
4671 || parser->in_template_argument_list_p)
4673 error_at (token->location,
4674 "statement-expressions are not allowed outside "
4675 "functions nor in template-argument lists");
4676 cp_parser_skip_to_end_of_block_or_statement (parser);
4677 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4678 cp_lexer_consume_token (parser->lexer);
4679 return error_mark_node;
4681 else
4682 return cp_parser_statement_expr (parser);
4684 /* Otherwise it's a normal parenthesized expression. */
4686 tree expr;
4687 bool saved_greater_than_is_operator_p;
4689 /* Consume the `('. */
4690 cp_lexer_consume_token (parser->lexer);
4691 /* Within a parenthesized expression, a `>' token is always
4692 the greater-than operator. */
4693 saved_greater_than_is_operator_p
4694 = parser->greater_than_is_operator_p;
4695 parser->greater_than_is_operator_p = true;
4697 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4698 /* Left fold expression. */
4699 expr = NULL_TREE;
4700 else
4701 /* Parse the parenthesized expression. */
4702 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4704 token = cp_lexer_peek_token (parser->lexer);
4705 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4707 expr = cp_parser_fold_expression (parser, expr);
4708 if (expr != error_mark_node
4709 && cxx_dialect < cxx1z
4710 && !in_system_header_at (input_location))
4711 pedwarn (input_location, 0, "fold-expressions only available "
4712 "with -std=c++1z or -std=gnu++1z");
4714 else
4715 /* Let the front end know that this expression was
4716 enclosed in parentheses. This matters in case, for
4717 example, the expression is of the form `A::B', since
4718 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4719 not. */
4720 expr = finish_parenthesized_expr (expr);
4722 /* DR 705: Wrapping an unqualified name in parentheses
4723 suppresses arg-dependent lookup. We want to pass back
4724 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4725 (c++/37862), but none of the others. */
4726 if (*idk != CP_ID_KIND_QUALIFIED)
4727 *idk = CP_ID_KIND_NONE;
4729 /* The `>' token might be the end of a template-id or
4730 template-parameter-list now. */
4731 parser->greater_than_is_operator_p
4732 = saved_greater_than_is_operator_p;
4733 /* Consume the `)'. */
4734 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4735 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4736 cp_parser_skip_to_end_of_statement (parser);
4738 return expr;
4741 case CPP_OPEN_SQUARE:
4743 if (c_dialect_objc ())
4745 /* We might have an Objective-C++ message. */
4746 cp_parser_parse_tentatively (parser);
4747 tree msg = cp_parser_objc_message_expression (parser);
4748 /* If that works out, we're done ... */
4749 if (cp_parser_parse_definitely (parser))
4750 return msg;
4751 /* ... else, fall though to see if it's a lambda. */
4753 tree lam = cp_parser_lambda_expression (parser);
4754 /* Don't warn about a failed tentative parse. */
4755 if (cp_parser_error_occurred (parser))
4756 return error_mark_node;
4757 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4758 return lam;
4761 case CPP_OBJC_STRING:
4762 if (c_dialect_objc ())
4763 /* We have an Objective-C++ string literal. */
4764 return cp_parser_objc_expression (parser);
4765 cp_parser_error (parser, "expected primary-expression");
4766 return error_mark_node;
4768 case CPP_KEYWORD:
4769 switch (token->keyword)
4771 /* These two are the boolean literals. */
4772 case RID_TRUE:
4773 cp_lexer_consume_token (parser->lexer);
4774 return boolean_true_node;
4775 case RID_FALSE:
4776 cp_lexer_consume_token (parser->lexer);
4777 return boolean_false_node;
4779 /* The `__null' literal. */
4780 case RID_NULL:
4781 cp_lexer_consume_token (parser->lexer);
4782 return null_node;
4784 /* The `nullptr' literal. */
4785 case RID_NULLPTR:
4786 cp_lexer_consume_token (parser->lexer);
4787 return nullptr_node;
4789 /* Recognize the `this' keyword. */
4790 case RID_THIS:
4791 cp_lexer_consume_token (parser->lexer);
4792 if (parser->local_variables_forbidden_p)
4794 error_at (token->location,
4795 "%<this%> may not be used in this context");
4796 return error_mark_node;
4798 /* Pointers cannot appear in constant-expressions. */
4799 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4800 return error_mark_node;
4801 return finish_this_expr ();
4803 /* The `operator' keyword can be the beginning of an
4804 id-expression. */
4805 case RID_OPERATOR:
4806 goto id_expression;
4808 case RID_FUNCTION_NAME:
4809 case RID_PRETTY_FUNCTION_NAME:
4810 case RID_C99_FUNCTION_NAME:
4812 non_integral_constant name;
4814 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4815 __func__ are the names of variables -- but they are
4816 treated specially. Therefore, they are handled here,
4817 rather than relying on the generic id-expression logic
4818 below. Grammatically, these names are id-expressions.
4820 Consume the token. */
4821 token = cp_lexer_consume_token (parser->lexer);
4823 switch (token->keyword)
4825 case RID_FUNCTION_NAME:
4826 name = NIC_FUNC_NAME;
4827 break;
4828 case RID_PRETTY_FUNCTION_NAME:
4829 name = NIC_PRETTY_FUNC;
4830 break;
4831 case RID_C99_FUNCTION_NAME:
4832 name = NIC_C99_FUNC;
4833 break;
4834 default:
4835 gcc_unreachable ();
4838 if (cp_parser_non_integral_constant_expression (parser, name))
4839 return error_mark_node;
4841 /* Look up the name. */
4842 return finish_fname (token->u.value);
4845 case RID_VA_ARG:
4847 tree expression;
4848 tree type;
4849 source_location type_location;
4851 /* The `__builtin_va_arg' construct is used to handle
4852 `va_arg'. Consume the `__builtin_va_arg' token. */
4853 cp_lexer_consume_token (parser->lexer);
4854 /* Look for the opening `('. */
4855 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4856 /* Now, parse the assignment-expression. */
4857 expression = cp_parser_assignment_expression (parser);
4858 /* Look for the `,'. */
4859 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4860 type_location = cp_lexer_peek_token (parser->lexer)->location;
4861 /* Parse the type-id. */
4862 type = cp_parser_type_id (parser);
4863 /* Look for the closing `)'. */
4864 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4865 /* Using `va_arg' in a constant-expression is not
4866 allowed. */
4867 if (cp_parser_non_integral_constant_expression (parser,
4868 NIC_VA_ARG))
4869 return error_mark_node;
4870 return build_x_va_arg (type_location, expression, type);
4873 case RID_OFFSETOF:
4874 return cp_parser_builtin_offsetof (parser);
4876 case RID_HAS_NOTHROW_ASSIGN:
4877 case RID_HAS_NOTHROW_CONSTRUCTOR:
4878 case RID_HAS_NOTHROW_COPY:
4879 case RID_HAS_TRIVIAL_ASSIGN:
4880 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4881 case RID_HAS_TRIVIAL_COPY:
4882 case RID_HAS_TRIVIAL_DESTRUCTOR:
4883 case RID_HAS_VIRTUAL_DESTRUCTOR:
4884 case RID_IS_ABSTRACT:
4885 case RID_IS_BASE_OF:
4886 case RID_IS_CLASS:
4887 case RID_IS_EMPTY:
4888 case RID_IS_ENUM:
4889 case RID_IS_FINAL:
4890 case RID_IS_LITERAL_TYPE:
4891 case RID_IS_POD:
4892 case RID_IS_POLYMORPHIC:
4893 case RID_IS_SAME_AS:
4894 case RID_IS_STD_LAYOUT:
4895 case RID_IS_TRIVIAL:
4896 case RID_IS_TRIVIALLY_ASSIGNABLE:
4897 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4898 case RID_IS_TRIVIALLY_COPYABLE:
4899 case RID_IS_UNION:
4900 return cp_parser_trait_expr (parser, token->keyword);
4902 // C++ concepts
4903 case RID_REQUIRES:
4904 return cp_parser_requires_expression (parser);
4906 /* Objective-C++ expressions. */
4907 case RID_AT_ENCODE:
4908 case RID_AT_PROTOCOL:
4909 case RID_AT_SELECTOR:
4910 return cp_parser_objc_expression (parser);
4912 case RID_TEMPLATE:
4913 if (parser->in_function_body
4914 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4915 == CPP_LESS))
4917 error_at (token->location,
4918 "a template declaration cannot appear at block scope");
4919 cp_parser_skip_to_end_of_block_or_statement (parser);
4920 return error_mark_node;
4922 default:
4923 cp_parser_error (parser, "expected primary-expression");
4924 return error_mark_node;
4927 /* An id-expression can start with either an identifier, a
4928 `::' as the beginning of a qualified-id, or the "operator"
4929 keyword. */
4930 case CPP_NAME:
4931 case CPP_SCOPE:
4932 case CPP_TEMPLATE_ID:
4933 case CPP_NESTED_NAME_SPECIFIER:
4935 tree id_expression;
4936 tree decl;
4937 const char *error_msg;
4938 bool template_p;
4939 bool done;
4940 cp_token *id_expr_token;
4942 id_expression:
4943 /* Parse the id-expression. */
4944 id_expression
4945 = cp_parser_id_expression (parser,
4946 /*template_keyword_p=*/false,
4947 /*check_dependency_p=*/true,
4948 &template_p,
4949 /*declarator_p=*/false,
4950 /*optional_p=*/false);
4951 if (id_expression == error_mark_node)
4952 return error_mark_node;
4953 id_expr_token = token;
4954 token = cp_lexer_peek_token (parser->lexer);
4955 done = (token->type != CPP_OPEN_SQUARE
4956 && token->type != CPP_OPEN_PAREN
4957 && token->type != CPP_DOT
4958 && token->type != CPP_DEREF
4959 && token->type != CPP_PLUS_PLUS
4960 && token->type != CPP_MINUS_MINUS);
4961 /* If we have a template-id, then no further lookup is
4962 required. If the template-id was for a template-class, we
4963 will sometimes have a TYPE_DECL at this point. */
4964 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4965 || TREE_CODE (id_expression) == TYPE_DECL)
4966 decl = id_expression;
4967 /* Look up the name. */
4968 else
4970 tree ambiguous_decls;
4972 /* If we already know that this lookup is ambiguous, then
4973 we've already issued an error message; there's no reason
4974 to check again. */
4975 if (id_expr_token->type == CPP_NAME
4976 && id_expr_token->error_reported)
4978 cp_parser_simulate_error (parser);
4979 return error_mark_node;
4982 decl = cp_parser_lookup_name (parser, id_expression,
4983 none_type,
4984 template_p,
4985 /*is_namespace=*/false,
4986 /*check_dependency=*/true,
4987 &ambiguous_decls,
4988 id_expr_token->location);
4989 /* If the lookup was ambiguous, an error will already have
4990 been issued. */
4991 if (ambiguous_decls)
4992 return error_mark_node;
4994 /* In Objective-C++, we may have an Objective-C 2.0
4995 dot-syntax for classes here. */
4996 if (c_dialect_objc ()
4997 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4998 && TREE_CODE (decl) == TYPE_DECL
4999 && objc_is_class_name (decl))
5001 tree component;
5002 cp_lexer_consume_token (parser->lexer);
5003 component = cp_parser_identifier (parser);
5004 if (component == error_mark_node)
5005 return error_mark_node;
5007 return objc_build_class_component_ref (id_expression, component);
5010 /* In Objective-C++, an instance variable (ivar) may be preferred
5011 to whatever cp_parser_lookup_name() found. */
5012 decl = objc_lookup_ivar (decl, id_expression);
5014 /* If name lookup gives us a SCOPE_REF, then the
5015 qualifying scope was dependent. */
5016 if (TREE_CODE (decl) == SCOPE_REF)
5018 /* At this point, we do not know if DECL is a valid
5019 integral constant expression. We assume that it is
5020 in fact such an expression, so that code like:
5022 template <int N> struct A {
5023 int a[B<N>::i];
5026 is accepted. At template-instantiation time, we
5027 will check that B<N>::i is actually a constant. */
5028 return decl;
5030 /* Check to see if DECL is a local variable in a context
5031 where that is forbidden. */
5032 if (parser->local_variables_forbidden_p
5033 && local_variable_p (decl))
5035 /* It might be that we only found DECL because we are
5036 trying to be generous with pre-ISO scoping rules.
5037 For example, consider:
5039 int i;
5040 void g() {
5041 for (int i = 0; i < 10; ++i) {}
5042 extern void f(int j = i);
5045 Here, name look up will originally find the out
5046 of scope `i'. We need to issue a warning message,
5047 but then use the global `i'. */
5048 decl = check_for_out_of_scope_variable (decl);
5049 if (local_variable_p (decl))
5051 error_at (id_expr_token->location,
5052 "local variable %qD may not appear in this context",
5053 decl);
5054 return error_mark_node;
5059 decl = (finish_id_expression
5060 (id_expression, decl, parser->scope,
5061 idk,
5062 parser->integral_constant_expression_p,
5063 parser->allow_non_integral_constant_expression_p,
5064 &parser->non_integral_constant_expression_p,
5065 template_p, done, address_p,
5066 template_arg_p,
5067 &error_msg,
5068 id_expr_token->location));
5069 if (error_msg)
5070 cp_parser_error (parser, error_msg);
5071 return decl;
5074 /* Anything else is an error. */
5075 default:
5076 cp_parser_error (parser, "expected primary-expression");
5077 return error_mark_node;
5081 static inline tree
5082 cp_parser_primary_expression (cp_parser *parser,
5083 bool address_p,
5084 bool cast_p,
5085 bool template_arg_p,
5086 cp_id_kind *idk)
5088 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5089 /*decltype*/false, idk);
5092 /* Parse an id-expression.
5094 id-expression:
5095 unqualified-id
5096 qualified-id
5098 qualified-id:
5099 :: [opt] nested-name-specifier template [opt] unqualified-id
5100 :: identifier
5101 :: operator-function-id
5102 :: template-id
5104 Return a representation of the unqualified portion of the
5105 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5106 a `::' or nested-name-specifier.
5108 Often, if the id-expression was a qualified-id, the caller will
5109 want to make a SCOPE_REF to represent the qualified-id. This
5110 function does not do this in order to avoid wastefully creating
5111 SCOPE_REFs when they are not required.
5113 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5114 `template' keyword.
5116 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5117 uninstantiated templates.
5119 If *TEMPLATE_P is non-NULL, it is set to true iff the
5120 `template' keyword is used to explicitly indicate that the entity
5121 named is a template.
5123 If DECLARATOR_P is true, the id-expression is appearing as part of
5124 a declarator, rather than as part of an expression. */
5126 static tree
5127 cp_parser_id_expression (cp_parser *parser,
5128 bool template_keyword_p,
5129 bool check_dependency_p,
5130 bool *template_p,
5131 bool declarator_p,
5132 bool optional_p)
5134 bool global_scope_p;
5135 bool nested_name_specifier_p;
5137 /* Assume the `template' keyword was not used. */
5138 if (template_p)
5139 *template_p = template_keyword_p;
5141 /* Look for the optional `::' operator. */
5142 global_scope_p
5143 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5144 != NULL_TREE);
5145 /* Look for the optional nested-name-specifier. */
5146 nested_name_specifier_p
5147 = (cp_parser_nested_name_specifier_opt (parser,
5148 /*typename_keyword_p=*/false,
5149 check_dependency_p,
5150 /*type_p=*/false,
5151 declarator_p)
5152 != NULL_TREE);
5153 /* If there is a nested-name-specifier, then we are looking at
5154 the first qualified-id production. */
5155 if (nested_name_specifier_p)
5157 tree saved_scope;
5158 tree saved_object_scope;
5159 tree saved_qualifying_scope;
5160 tree unqualified_id;
5161 bool is_template;
5163 /* See if the next token is the `template' keyword. */
5164 if (!template_p)
5165 template_p = &is_template;
5166 *template_p = cp_parser_optional_template_keyword (parser);
5167 /* Name lookup we do during the processing of the
5168 unqualified-id might obliterate SCOPE. */
5169 saved_scope = parser->scope;
5170 saved_object_scope = parser->object_scope;
5171 saved_qualifying_scope = parser->qualifying_scope;
5172 /* Process the final unqualified-id. */
5173 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5174 check_dependency_p,
5175 declarator_p,
5176 /*optional_p=*/false);
5177 /* Restore the SAVED_SCOPE for our caller. */
5178 parser->scope = saved_scope;
5179 parser->object_scope = saved_object_scope;
5180 parser->qualifying_scope = saved_qualifying_scope;
5182 return unqualified_id;
5184 /* Otherwise, if we are in global scope, then we are looking at one
5185 of the other qualified-id productions. */
5186 else if (global_scope_p)
5188 cp_token *token;
5189 tree id;
5191 /* Peek at the next token. */
5192 token = cp_lexer_peek_token (parser->lexer);
5194 /* If it's an identifier, and the next token is not a "<", then
5195 we can avoid the template-id case. This is an optimization
5196 for this common case. */
5197 if (token->type == CPP_NAME
5198 && !cp_parser_nth_token_starts_template_argument_list_p
5199 (parser, 2))
5200 return cp_parser_identifier (parser);
5202 cp_parser_parse_tentatively (parser);
5203 /* Try a template-id. */
5204 id = cp_parser_template_id (parser,
5205 /*template_keyword_p=*/false,
5206 /*check_dependency_p=*/true,
5207 none_type,
5208 declarator_p);
5209 /* If that worked, we're done. */
5210 if (cp_parser_parse_definitely (parser))
5211 return id;
5213 /* Peek at the next token. (Changes in the token buffer may
5214 have invalidated the pointer obtained above.) */
5215 token = cp_lexer_peek_token (parser->lexer);
5217 switch (token->type)
5219 case CPP_NAME:
5220 return cp_parser_identifier (parser);
5222 case CPP_KEYWORD:
5223 if (token->keyword == RID_OPERATOR)
5224 return cp_parser_operator_function_id (parser);
5225 /* Fall through. */
5227 default:
5228 cp_parser_error (parser, "expected id-expression");
5229 return error_mark_node;
5232 else
5233 return cp_parser_unqualified_id (parser, template_keyword_p,
5234 /*check_dependency_p=*/true,
5235 declarator_p,
5236 optional_p);
5239 /* Parse an unqualified-id.
5241 unqualified-id:
5242 identifier
5243 operator-function-id
5244 conversion-function-id
5245 ~ class-name
5246 template-id
5248 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5249 keyword, in a construct like `A::template ...'.
5251 Returns a representation of unqualified-id. For the `identifier'
5252 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5253 production a BIT_NOT_EXPR is returned; the operand of the
5254 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5255 other productions, see the documentation accompanying the
5256 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5257 names are looked up in uninstantiated templates. If DECLARATOR_P
5258 is true, the unqualified-id is appearing as part of a declarator,
5259 rather than as part of an expression. */
5261 static tree
5262 cp_parser_unqualified_id (cp_parser* parser,
5263 bool template_keyword_p,
5264 bool check_dependency_p,
5265 bool declarator_p,
5266 bool optional_p)
5268 cp_token *token;
5270 /* Peek at the next token. */
5271 token = cp_lexer_peek_token (parser->lexer);
5273 switch ((int) token->type)
5275 case CPP_NAME:
5277 tree id;
5279 /* We don't know yet whether or not this will be a
5280 template-id. */
5281 cp_parser_parse_tentatively (parser);
5282 /* Try a template-id. */
5283 id = cp_parser_template_id (parser, template_keyword_p,
5284 check_dependency_p,
5285 none_type,
5286 declarator_p);
5287 /* If it worked, we're done. */
5288 if (cp_parser_parse_definitely (parser))
5289 return id;
5290 /* Otherwise, it's an ordinary identifier. */
5291 return cp_parser_identifier (parser);
5294 case CPP_TEMPLATE_ID:
5295 return cp_parser_template_id (parser, template_keyword_p,
5296 check_dependency_p,
5297 none_type,
5298 declarator_p);
5300 case CPP_COMPL:
5302 tree type_decl;
5303 tree qualifying_scope;
5304 tree object_scope;
5305 tree scope;
5306 bool done;
5308 /* Consume the `~' token. */
5309 cp_lexer_consume_token (parser->lexer);
5310 /* Parse the class-name. The standard, as written, seems to
5311 say that:
5313 template <typename T> struct S { ~S (); };
5314 template <typename T> S<T>::~S() {}
5316 is invalid, since `~' must be followed by a class-name, but
5317 `S<T>' is dependent, and so not known to be a class.
5318 That's not right; we need to look in uninstantiated
5319 templates. A further complication arises from:
5321 template <typename T> void f(T t) {
5322 t.T::~T();
5325 Here, it is not possible to look up `T' in the scope of `T'
5326 itself. We must look in both the current scope, and the
5327 scope of the containing complete expression.
5329 Yet another issue is:
5331 struct S {
5332 int S;
5333 ~S();
5336 S::~S() {}
5338 The standard does not seem to say that the `S' in `~S'
5339 should refer to the type `S' and not the data member
5340 `S::S'. */
5342 /* DR 244 says that we look up the name after the "~" in the
5343 same scope as we looked up the qualifying name. That idea
5344 isn't fully worked out; it's more complicated than that. */
5345 scope = parser->scope;
5346 object_scope = parser->object_scope;
5347 qualifying_scope = parser->qualifying_scope;
5349 /* Check for invalid scopes. */
5350 if (scope == error_mark_node)
5352 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5353 cp_lexer_consume_token (parser->lexer);
5354 return error_mark_node;
5356 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5358 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5359 error_at (token->location,
5360 "scope %qT before %<~%> is not a class-name",
5361 scope);
5362 cp_parser_simulate_error (parser);
5363 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5364 cp_lexer_consume_token (parser->lexer);
5365 return error_mark_node;
5367 gcc_assert (!scope || TYPE_P (scope));
5369 /* If the name is of the form "X::~X" it's OK even if X is a
5370 typedef. */
5371 token = cp_lexer_peek_token (parser->lexer);
5372 if (scope
5373 && token->type == CPP_NAME
5374 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5375 != CPP_LESS)
5376 && (token->u.value == TYPE_IDENTIFIER (scope)
5377 || (CLASS_TYPE_P (scope)
5378 && constructor_name_p (token->u.value, scope))))
5380 cp_lexer_consume_token (parser->lexer);
5381 return build_nt (BIT_NOT_EXPR, scope);
5384 /* ~auto means the destructor of whatever the object is. */
5385 if (cp_parser_is_keyword (token, RID_AUTO))
5387 if (cxx_dialect < cxx14)
5388 pedwarn (input_location, 0,
5389 "%<~auto%> only available with "
5390 "-std=c++14 or -std=gnu++14");
5391 cp_lexer_consume_token (parser->lexer);
5392 return build_nt (BIT_NOT_EXPR, make_auto ());
5395 /* If there was an explicit qualification (S::~T), first look
5396 in the scope given by the qualification (i.e., S).
5398 Note: in the calls to cp_parser_class_name below we pass
5399 typename_type so that lookup finds the injected-class-name
5400 rather than the constructor. */
5401 done = false;
5402 type_decl = NULL_TREE;
5403 if (scope)
5405 cp_parser_parse_tentatively (parser);
5406 type_decl = cp_parser_class_name (parser,
5407 /*typename_keyword_p=*/false,
5408 /*template_keyword_p=*/false,
5409 typename_type,
5410 /*check_dependency=*/false,
5411 /*class_head_p=*/false,
5412 declarator_p);
5413 if (cp_parser_parse_definitely (parser))
5414 done = true;
5416 /* In "N::S::~S", look in "N" as well. */
5417 if (!done && scope && qualifying_scope)
5419 cp_parser_parse_tentatively (parser);
5420 parser->scope = qualifying_scope;
5421 parser->object_scope = NULL_TREE;
5422 parser->qualifying_scope = NULL_TREE;
5423 type_decl
5424 = cp_parser_class_name (parser,
5425 /*typename_keyword_p=*/false,
5426 /*template_keyword_p=*/false,
5427 typename_type,
5428 /*check_dependency=*/false,
5429 /*class_head_p=*/false,
5430 declarator_p);
5431 if (cp_parser_parse_definitely (parser))
5432 done = true;
5434 /* In "p->S::~T", look in the scope given by "*p" as well. */
5435 else if (!done && object_scope)
5437 cp_parser_parse_tentatively (parser);
5438 parser->scope = object_scope;
5439 parser->object_scope = NULL_TREE;
5440 parser->qualifying_scope = NULL_TREE;
5441 type_decl
5442 = cp_parser_class_name (parser,
5443 /*typename_keyword_p=*/false,
5444 /*template_keyword_p=*/false,
5445 typename_type,
5446 /*check_dependency=*/false,
5447 /*class_head_p=*/false,
5448 declarator_p);
5449 if (cp_parser_parse_definitely (parser))
5450 done = true;
5452 /* Look in the surrounding context. */
5453 if (!done)
5455 parser->scope = NULL_TREE;
5456 parser->object_scope = NULL_TREE;
5457 parser->qualifying_scope = NULL_TREE;
5458 if (processing_template_decl)
5459 cp_parser_parse_tentatively (parser);
5460 type_decl
5461 = cp_parser_class_name (parser,
5462 /*typename_keyword_p=*/false,
5463 /*template_keyword_p=*/false,
5464 typename_type,
5465 /*check_dependency=*/false,
5466 /*class_head_p=*/false,
5467 declarator_p);
5468 if (processing_template_decl
5469 && ! cp_parser_parse_definitely (parser))
5471 /* We couldn't find a type with this name. If we're parsing
5472 tentatively, fail and try something else. */
5473 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5475 cp_parser_simulate_error (parser);
5476 return error_mark_node;
5478 /* Otherwise, accept it and check for a match at instantiation
5479 time. */
5480 type_decl = cp_parser_identifier (parser);
5481 if (type_decl != error_mark_node)
5482 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5483 return type_decl;
5486 /* If an error occurred, assume that the name of the
5487 destructor is the same as the name of the qualifying
5488 class. That allows us to keep parsing after running
5489 into ill-formed destructor names. */
5490 if (type_decl == error_mark_node && scope)
5491 return build_nt (BIT_NOT_EXPR, scope);
5492 else if (type_decl == error_mark_node)
5493 return error_mark_node;
5495 /* Check that destructor name and scope match. */
5496 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5498 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5499 error_at (token->location,
5500 "declaration of %<~%T%> as member of %qT",
5501 type_decl, scope);
5502 cp_parser_simulate_error (parser);
5503 return error_mark_node;
5506 /* [class.dtor]
5508 A typedef-name that names a class shall not be used as the
5509 identifier in the declarator for a destructor declaration. */
5510 if (declarator_p
5511 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5512 && !DECL_SELF_REFERENCE_P (type_decl)
5513 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5514 error_at (token->location,
5515 "typedef-name %qD used as destructor declarator",
5516 type_decl);
5518 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5521 case CPP_KEYWORD:
5522 if (token->keyword == RID_OPERATOR)
5524 tree id;
5526 /* This could be a template-id, so we try that first. */
5527 cp_parser_parse_tentatively (parser);
5528 /* Try a template-id. */
5529 id = cp_parser_template_id (parser, template_keyword_p,
5530 /*check_dependency_p=*/true,
5531 none_type,
5532 declarator_p);
5533 /* If that worked, we're done. */
5534 if (cp_parser_parse_definitely (parser))
5535 return id;
5536 /* We still don't know whether we're looking at an
5537 operator-function-id or a conversion-function-id. */
5538 cp_parser_parse_tentatively (parser);
5539 /* Try an operator-function-id. */
5540 id = cp_parser_operator_function_id (parser);
5541 /* If that didn't work, try a conversion-function-id. */
5542 if (!cp_parser_parse_definitely (parser))
5543 id = cp_parser_conversion_function_id (parser);
5544 else if (UDLIT_OPER_P (id))
5546 /* 17.6.3.3.5 */
5547 const char *name = UDLIT_OP_SUFFIX (id);
5548 if (name[0] != '_' && !in_system_header_at (input_location)
5549 && declarator_p)
5550 warning (0, "literal operator suffixes not preceded by %<_%>"
5551 " are reserved for future standardization");
5554 return id;
5556 /* Fall through. */
5558 default:
5559 if (optional_p)
5560 return NULL_TREE;
5561 cp_parser_error (parser, "expected unqualified-id");
5562 return error_mark_node;
5566 /* Parse an (optional) nested-name-specifier.
5568 nested-name-specifier: [C++98]
5569 class-or-namespace-name :: nested-name-specifier [opt]
5570 class-or-namespace-name :: template nested-name-specifier [opt]
5572 nested-name-specifier: [C++0x]
5573 type-name ::
5574 namespace-name ::
5575 nested-name-specifier identifier ::
5576 nested-name-specifier template [opt] simple-template-id ::
5578 PARSER->SCOPE should be set appropriately before this function is
5579 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5580 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5581 in name lookups.
5583 Sets PARSER->SCOPE to the class (TYPE) or namespace
5584 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5585 it unchanged if there is no nested-name-specifier. Returns the new
5586 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5588 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5589 part of a declaration and/or decl-specifier. */
5591 static tree
5592 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5593 bool typename_keyword_p,
5594 bool check_dependency_p,
5595 bool type_p,
5596 bool is_declaration)
5598 bool success = false;
5599 cp_token_position start = 0;
5600 cp_token *token;
5602 /* Remember where the nested-name-specifier starts. */
5603 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5605 start = cp_lexer_token_position (parser->lexer, false);
5606 push_deferring_access_checks (dk_deferred);
5609 while (true)
5611 tree new_scope;
5612 tree old_scope;
5613 tree saved_qualifying_scope;
5614 bool template_keyword_p;
5616 /* Spot cases that cannot be the beginning of a
5617 nested-name-specifier. */
5618 token = cp_lexer_peek_token (parser->lexer);
5620 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5621 the already parsed nested-name-specifier. */
5622 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5624 /* Grab the nested-name-specifier and continue the loop. */
5625 cp_parser_pre_parsed_nested_name_specifier (parser);
5626 /* If we originally encountered this nested-name-specifier
5627 with IS_DECLARATION set to false, we will not have
5628 resolved TYPENAME_TYPEs, so we must do so here. */
5629 if (is_declaration
5630 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5632 new_scope = resolve_typename_type (parser->scope,
5633 /*only_current_p=*/false);
5634 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5635 parser->scope = new_scope;
5637 success = true;
5638 continue;
5641 /* Spot cases that cannot be the beginning of a
5642 nested-name-specifier. On the second and subsequent times
5643 through the loop, we look for the `template' keyword. */
5644 if (success && token->keyword == RID_TEMPLATE)
5646 /* A template-id can start a nested-name-specifier. */
5647 else if (token->type == CPP_TEMPLATE_ID)
5649 /* DR 743: decltype can be used in a nested-name-specifier. */
5650 else if (token_is_decltype (token))
5652 else
5654 /* If the next token is not an identifier, then it is
5655 definitely not a type-name or namespace-name. */
5656 if (token->type != CPP_NAME)
5657 break;
5658 /* If the following token is neither a `<' (to begin a
5659 template-id), nor a `::', then we are not looking at a
5660 nested-name-specifier. */
5661 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5663 if (token->type == CPP_COLON
5664 && parser->colon_corrects_to_scope_p
5665 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5667 error_at (token->location,
5668 "found %<:%> in nested-name-specifier, expected %<::%>");
5669 token->type = CPP_SCOPE;
5672 if (token->type != CPP_SCOPE
5673 && !cp_parser_nth_token_starts_template_argument_list_p
5674 (parser, 2))
5675 break;
5678 /* The nested-name-specifier is optional, so we parse
5679 tentatively. */
5680 cp_parser_parse_tentatively (parser);
5682 /* Look for the optional `template' keyword, if this isn't the
5683 first time through the loop. */
5684 if (success)
5685 template_keyword_p = cp_parser_optional_template_keyword (parser);
5686 else
5687 template_keyword_p = false;
5689 /* Save the old scope since the name lookup we are about to do
5690 might destroy it. */
5691 old_scope = parser->scope;
5692 saved_qualifying_scope = parser->qualifying_scope;
5693 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5694 look up names in "X<T>::I" in order to determine that "Y" is
5695 a template. So, if we have a typename at this point, we make
5696 an effort to look through it. */
5697 if (is_declaration
5698 && !typename_keyword_p
5699 && parser->scope
5700 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5701 parser->scope = resolve_typename_type (parser->scope,
5702 /*only_current_p=*/false);
5703 /* Parse the qualifying entity. */
5704 new_scope
5705 = cp_parser_qualifying_entity (parser,
5706 typename_keyword_p,
5707 template_keyword_p,
5708 check_dependency_p,
5709 type_p,
5710 is_declaration);
5711 /* Look for the `::' token. */
5712 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5714 /* If we found what we wanted, we keep going; otherwise, we're
5715 done. */
5716 if (!cp_parser_parse_definitely (parser))
5718 bool error_p = false;
5720 /* Restore the OLD_SCOPE since it was valid before the
5721 failed attempt at finding the last
5722 class-or-namespace-name. */
5723 parser->scope = old_scope;
5724 parser->qualifying_scope = saved_qualifying_scope;
5726 /* If the next token is a decltype, and the one after that is a
5727 `::', then the decltype has failed to resolve to a class or
5728 enumeration type. Give this error even when parsing
5729 tentatively since it can't possibly be valid--and we're going
5730 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5731 won't get another chance.*/
5732 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5733 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5734 == CPP_SCOPE))
5736 token = cp_lexer_consume_token (parser->lexer);
5737 error_at (token->location, "decltype evaluates to %qT, "
5738 "which is not a class or enumeration type",
5739 token->u.value);
5740 parser->scope = error_mark_node;
5741 error_p = true;
5742 /* As below. */
5743 success = true;
5744 cp_lexer_consume_token (parser->lexer);
5747 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5748 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5750 /* If we have a non-type template-id followed by ::, it can't
5751 possibly be valid. */
5752 token = cp_lexer_peek_token (parser->lexer);
5753 tree tid = token->u.tree_check_value->value;
5754 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5755 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5757 tree tmpl = NULL_TREE;
5758 if (is_overloaded_fn (tid))
5760 tree fns = get_fns (tid);
5761 if (!OVL_CHAIN (fns))
5762 tmpl = OVL_CURRENT (fns);
5763 error_at (token->location, "function template-id %qD "
5764 "in nested-name-specifier", tid);
5766 else
5768 /* Variable template. */
5769 tmpl = TREE_OPERAND (tid, 0);
5770 gcc_assert (variable_template_p (tmpl));
5771 error_at (token->location, "variable template-id %qD "
5772 "in nested-name-specifier", tid);
5774 if (tmpl)
5775 inform (DECL_SOURCE_LOCATION (tmpl),
5776 "%qD declared here", tmpl);
5778 parser->scope = error_mark_node;
5779 error_p = true;
5780 /* As below. */
5781 success = true;
5782 cp_lexer_consume_token (parser->lexer);
5783 cp_lexer_consume_token (parser->lexer);
5787 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5788 break;
5789 /* If the next token is an identifier, and the one after
5790 that is a `::', then any valid interpretation would have
5791 found a class-or-namespace-name. */
5792 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5793 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5794 == CPP_SCOPE)
5795 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5796 != CPP_COMPL))
5798 token = cp_lexer_consume_token (parser->lexer);
5799 if (!error_p)
5801 if (!token->error_reported)
5803 tree decl;
5804 tree ambiguous_decls;
5806 decl = cp_parser_lookup_name (parser, token->u.value,
5807 none_type,
5808 /*is_template=*/false,
5809 /*is_namespace=*/false,
5810 /*check_dependency=*/true,
5811 &ambiguous_decls,
5812 token->location);
5813 if (TREE_CODE (decl) == TEMPLATE_DECL)
5814 error_at (token->location,
5815 "%qD used without template parameters",
5816 decl);
5817 else if (ambiguous_decls)
5819 // cp_parser_lookup_name has the same diagnostic,
5820 // thus make sure to emit it at most once.
5821 if (cp_parser_uncommitted_to_tentative_parse_p
5822 (parser))
5824 error_at (token->location,
5825 "reference to %qD is ambiguous",
5826 token->u.value);
5827 print_candidates (ambiguous_decls);
5829 decl = error_mark_node;
5831 else
5833 if (cxx_dialect != cxx98)
5834 cp_parser_name_lookup_error
5835 (parser, token->u.value, decl, NLE_NOT_CXX98,
5836 token->location);
5837 else
5838 cp_parser_name_lookup_error
5839 (parser, token->u.value, decl, NLE_CXX98,
5840 token->location);
5843 parser->scope = error_mark_node;
5844 error_p = true;
5845 /* Treat this as a successful nested-name-specifier
5846 due to:
5848 [basic.lookup.qual]
5850 If the name found is not a class-name (clause
5851 _class_) or namespace-name (_namespace.def_), the
5852 program is ill-formed. */
5853 success = true;
5855 cp_lexer_consume_token (parser->lexer);
5857 break;
5859 /* We've found one valid nested-name-specifier. */
5860 success = true;
5861 /* Name lookup always gives us a DECL. */
5862 if (TREE_CODE (new_scope) == TYPE_DECL)
5863 new_scope = TREE_TYPE (new_scope);
5864 /* Uses of "template" must be followed by actual templates. */
5865 if (template_keyword_p
5866 && !(CLASS_TYPE_P (new_scope)
5867 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5868 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5869 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5870 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5871 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5872 == TEMPLATE_ID_EXPR)))
5873 permerror (input_location, TYPE_P (new_scope)
5874 ? G_("%qT is not a template")
5875 : G_("%qD is not a template"),
5876 new_scope);
5877 /* If it is a class scope, try to complete it; we are about to
5878 be looking up names inside the class. */
5879 if (TYPE_P (new_scope)
5880 /* Since checking types for dependency can be expensive,
5881 avoid doing it if the type is already complete. */
5882 && !COMPLETE_TYPE_P (new_scope)
5883 /* Do not try to complete dependent types. */
5884 && !dependent_type_p (new_scope))
5886 new_scope = complete_type (new_scope);
5887 /* If it is a typedef to current class, use the current
5888 class instead, as the typedef won't have any names inside
5889 it yet. */
5890 if (!COMPLETE_TYPE_P (new_scope)
5891 && currently_open_class (new_scope))
5892 new_scope = TYPE_MAIN_VARIANT (new_scope);
5894 /* Make sure we look in the right scope the next time through
5895 the loop. */
5896 parser->scope = new_scope;
5899 /* If parsing tentatively, replace the sequence of tokens that makes
5900 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5901 token. That way, should we re-parse the token stream, we will
5902 not have to repeat the effort required to do the parse, nor will
5903 we issue duplicate error messages. */
5904 if (success && start)
5906 cp_token *token;
5908 token = cp_lexer_token_at (parser->lexer, start);
5909 /* Reset the contents of the START token. */
5910 token->type = CPP_NESTED_NAME_SPECIFIER;
5911 /* Retrieve any deferred checks. Do not pop this access checks yet
5912 so the memory will not be reclaimed during token replacing below. */
5913 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5914 token->u.tree_check_value->value = parser->scope;
5915 token->u.tree_check_value->checks = get_deferred_access_checks ();
5916 token->u.tree_check_value->qualifying_scope =
5917 parser->qualifying_scope;
5918 token->keyword = RID_MAX;
5920 /* Purge all subsequent tokens. */
5921 cp_lexer_purge_tokens_after (parser->lexer, start);
5924 if (start)
5925 pop_to_parent_deferring_access_checks ();
5927 return success ? parser->scope : NULL_TREE;
5930 /* Parse a nested-name-specifier. See
5931 cp_parser_nested_name_specifier_opt for details. This function
5932 behaves identically, except that it will an issue an error if no
5933 nested-name-specifier is present. */
5935 static tree
5936 cp_parser_nested_name_specifier (cp_parser *parser,
5937 bool typename_keyword_p,
5938 bool check_dependency_p,
5939 bool type_p,
5940 bool is_declaration)
5942 tree scope;
5944 /* Look for the nested-name-specifier. */
5945 scope = cp_parser_nested_name_specifier_opt (parser,
5946 typename_keyword_p,
5947 check_dependency_p,
5948 type_p,
5949 is_declaration);
5950 /* If it was not present, issue an error message. */
5951 if (!scope)
5953 cp_parser_error (parser, "expected nested-name-specifier");
5954 parser->scope = NULL_TREE;
5957 return scope;
5960 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5961 this is either a class-name or a namespace-name (which corresponds
5962 to the class-or-namespace-name production in the grammar). For
5963 C++0x, it can also be a type-name that refers to an enumeration
5964 type or a simple-template-id.
5966 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5967 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5968 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5969 TYPE_P is TRUE iff the next name should be taken as a class-name,
5970 even the same name is declared to be another entity in the same
5971 scope.
5973 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5974 specified by the class-or-namespace-name. If neither is found the
5975 ERROR_MARK_NODE is returned. */
5977 static tree
5978 cp_parser_qualifying_entity (cp_parser *parser,
5979 bool typename_keyword_p,
5980 bool template_keyword_p,
5981 bool check_dependency_p,
5982 bool type_p,
5983 bool is_declaration)
5985 tree saved_scope;
5986 tree saved_qualifying_scope;
5987 tree saved_object_scope;
5988 tree scope;
5989 bool only_class_p;
5990 bool successful_parse_p;
5992 /* DR 743: decltype can appear in a nested-name-specifier. */
5993 if (cp_lexer_next_token_is_decltype (parser->lexer))
5995 scope = cp_parser_decltype (parser);
5996 if (TREE_CODE (scope) != ENUMERAL_TYPE
5997 && !MAYBE_CLASS_TYPE_P (scope))
5999 cp_parser_simulate_error (parser);
6000 return error_mark_node;
6002 if (TYPE_NAME (scope))
6003 scope = TYPE_NAME (scope);
6004 return scope;
6007 /* Before we try to parse the class-name, we must save away the
6008 current PARSER->SCOPE since cp_parser_class_name will destroy
6009 it. */
6010 saved_scope = parser->scope;
6011 saved_qualifying_scope = parser->qualifying_scope;
6012 saved_object_scope = parser->object_scope;
6013 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6014 there is no need to look for a namespace-name. */
6015 only_class_p = template_keyword_p
6016 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6017 if (!only_class_p)
6018 cp_parser_parse_tentatively (parser);
6019 scope = cp_parser_class_name (parser,
6020 typename_keyword_p,
6021 template_keyword_p,
6022 type_p ? class_type : none_type,
6023 check_dependency_p,
6024 /*class_head_p=*/false,
6025 is_declaration,
6026 /*enum_ok=*/cxx_dialect > cxx98);
6027 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6028 /* If that didn't work, try for a namespace-name. */
6029 if (!only_class_p && !successful_parse_p)
6031 /* Restore the saved scope. */
6032 parser->scope = saved_scope;
6033 parser->qualifying_scope = saved_qualifying_scope;
6034 parser->object_scope = saved_object_scope;
6035 /* If we are not looking at an identifier followed by the scope
6036 resolution operator, then this is not part of a
6037 nested-name-specifier. (Note that this function is only used
6038 to parse the components of a nested-name-specifier.) */
6039 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6040 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6041 return error_mark_node;
6042 scope = cp_parser_namespace_name (parser);
6045 return scope;
6048 /* Return true if we are looking at a compound-literal, false otherwise. */
6050 static bool
6051 cp_parser_compound_literal_p (cp_parser *parser)
6053 /* Consume the `('. */
6054 cp_lexer_consume_token (parser->lexer);
6056 cp_lexer_save_tokens (parser->lexer);
6058 /* Skip tokens until the next token is a closing parenthesis.
6059 If we find the closing `)', and the next token is a `{', then
6060 we are looking at a compound-literal. */
6061 bool compound_literal_p
6062 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6063 /*consume_paren=*/true)
6064 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6066 /* Roll back the tokens we skipped. */
6067 cp_lexer_rollback_tokens (parser->lexer);
6069 return compound_literal_p;
6072 /* Parse a postfix-expression.
6074 postfix-expression:
6075 primary-expression
6076 postfix-expression [ expression ]
6077 postfix-expression ( expression-list [opt] )
6078 simple-type-specifier ( expression-list [opt] )
6079 typename :: [opt] nested-name-specifier identifier
6080 ( expression-list [opt] )
6081 typename :: [opt] nested-name-specifier template [opt] template-id
6082 ( expression-list [opt] )
6083 postfix-expression . template [opt] id-expression
6084 postfix-expression -> template [opt] id-expression
6085 postfix-expression . pseudo-destructor-name
6086 postfix-expression -> pseudo-destructor-name
6087 postfix-expression ++
6088 postfix-expression --
6089 dynamic_cast < type-id > ( expression )
6090 static_cast < type-id > ( expression )
6091 reinterpret_cast < type-id > ( expression )
6092 const_cast < type-id > ( expression )
6093 typeid ( expression )
6094 typeid ( type-id )
6096 GNU Extension:
6098 postfix-expression:
6099 ( type-id ) { initializer-list , [opt] }
6101 This extension is a GNU version of the C99 compound-literal
6102 construct. (The C99 grammar uses `type-name' instead of `type-id',
6103 but they are essentially the same concept.)
6105 If ADDRESS_P is true, the postfix expression is the operand of the
6106 `&' operator. CAST_P is true if this expression is the target of a
6107 cast.
6109 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6110 class member access expressions [expr.ref].
6112 Returns a representation of the expression. */
6114 static tree
6115 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6116 bool member_access_only_p, bool decltype_p,
6117 cp_id_kind * pidk_return)
6119 cp_token *token;
6120 location_t loc;
6121 enum rid keyword;
6122 cp_id_kind idk = CP_ID_KIND_NONE;
6123 tree postfix_expression = NULL_TREE;
6124 bool is_member_access = false;
6125 int saved_in_statement = -1;
6127 /* Peek at the next token. */
6128 token = cp_lexer_peek_token (parser->lexer);
6129 loc = token->location;
6130 /* Some of the productions are determined by keywords. */
6131 keyword = token->keyword;
6132 switch (keyword)
6134 case RID_DYNCAST:
6135 case RID_STATCAST:
6136 case RID_REINTCAST:
6137 case RID_CONSTCAST:
6139 tree type;
6140 tree expression;
6141 const char *saved_message;
6142 bool saved_in_type_id_in_expr_p;
6144 /* All of these can be handled in the same way from the point
6145 of view of parsing. Begin by consuming the token
6146 identifying the cast. */
6147 cp_lexer_consume_token (parser->lexer);
6149 /* New types cannot be defined in the cast. */
6150 saved_message = parser->type_definition_forbidden_message;
6151 parser->type_definition_forbidden_message
6152 = G_("types may not be defined in casts");
6154 /* Look for the opening `<'. */
6155 cp_parser_require (parser, CPP_LESS, RT_LESS);
6156 /* Parse the type to which we are casting. */
6157 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6158 parser->in_type_id_in_expr_p = true;
6159 type = cp_parser_type_id (parser);
6160 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6161 /* Look for the closing `>'. */
6162 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6163 /* Restore the old message. */
6164 parser->type_definition_forbidden_message = saved_message;
6166 bool saved_greater_than_is_operator_p
6167 = parser->greater_than_is_operator_p;
6168 parser->greater_than_is_operator_p = true;
6170 /* And the expression which is being cast. */
6171 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6172 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6173 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6175 parser->greater_than_is_operator_p
6176 = saved_greater_than_is_operator_p;
6178 /* Only type conversions to integral or enumeration types
6179 can be used in constant-expressions. */
6180 if (!cast_valid_in_integral_constant_expression_p (type)
6181 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6182 return error_mark_node;
6184 switch (keyword)
6186 case RID_DYNCAST:
6187 postfix_expression
6188 = build_dynamic_cast (type, expression, tf_warning_or_error);
6189 break;
6190 case RID_STATCAST:
6191 postfix_expression
6192 = build_static_cast (type, expression, tf_warning_or_error);
6193 break;
6194 case RID_REINTCAST:
6195 postfix_expression
6196 = build_reinterpret_cast (type, expression,
6197 tf_warning_or_error);
6198 break;
6199 case RID_CONSTCAST:
6200 postfix_expression
6201 = build_const_cast (type, expression, tf_warning_or_error);
6202 break;
6203 default:
6204 gcc_unreachable ();
6207 break;
6209 case RID_TYPEID:
6211 tree type;
6212 const char *saved_message;
6213 bool saved_in_type_id_in_expr_p;
6215 /* Consume the `typeid' token. */
6216 cp_lexer_consume_token (parser->lexer);
6217 /* Look for the `(' token. */
6218 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6219 /* Types cannot be defined in a `typeid' expression. */
6220 saved_message = parser->type_definition_forbidden_message;
6221 parser->type_definition_forbidden_message
6222 = G_("types may not be defined in a %<typeid%> expression");
6223 /* We can't be sure yet whether we're looking at a type-id or an
6224 expression. */
6225 cp_parser_parse_tentatively (parser);
6226 /* Try a type-id first. */
6227 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6228 parser->in_type_id_in_expr_p = true;
6229 type = cp_parser_type_id (parser);
6230 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6231 /* Look for the `)' token. Otherwise, we can't be sure that
6232 we're not looking at an expression: consider `typeid (int
6233 (3))', for example. */
6234 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6235 /* If all went well, simply lookup the type-id. */
6236 if (cp_parser_parse_definitely (parser))
6237 postfix_expression = get_typeid (type, tf_warning_or_error);
6238 /* Otherwise, fall back to the expression variant. */
6239 else
6241 tree expression;
6243 /* Look for an expression. */
6244 expression = cp_parser_expression (parser, & idk);
6245 /* Compute its typeid. */
6246 postfix_expression = build_typeid (expression, tf_warning_or_error);
6247 /* Look for the `)' token. */
6248 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6250 /* Restore the saved message. */
6251 parser->type_definition_forbidden_message = saved_message;
6252 /* `typeid' may not appear in an integral constant expression. */
6253 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6254 return error_mark_node;
6256 break;
6258 case RID_TYPENAME:
6260 tree type;
6261 /* The syntax permitted here is the same permitted for an
6262 elaborated-type-specifier. */
6263 ++parser->prevent_constrained_type_specifiers;
6264 type = cp_parser_elaborated_type_specifier (parser,
6265 /*is_friend=*/false,
6266 /*is_declaration=*/false);
6267 --parser->prevent_constrained_type_specifiers;
6268 postfix_expression = cp_parser_functional_cast (parser, type);
6270 break;
6272 case RID_CILK_SPAWN:
6274 cp_lexer_consume_token (parser->lexer);
6275 token = cp_lexer_peek_token (parser->lexer);
6276 if (token->type == CPP_SEMICOLON)
6278 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6279 "an expression");
6280 postfix_expression = error_mark_node;
6281 break;
6283 else if (!current_function_decl)
6285 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6286 "inside a function");
6287 postfix_expression = error_mark_node;
6288 break;
6290 else
6292 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6293 saved_in_statement = parser->in_statement;
6294 parser->in_statement |= IN_CILK_SPAWN;
6296 cfun->calls_cilk_spawn = 1;
6297 postfix_expression =
6298 cp_parser_postfix_expression (parser, false, false,
6299 false, false, &idk);
6300 if (!flag_cilkplus)
6302 error_at (token->location, "-fcilkplus must be enabled to use"
6303 " %<_Cilk_spawn%>");
6304 cfun->calls_cilk_spawn = 0;
6306 else if (saved_in_statement & IN_CILK_SPAWN)
6308 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6309 "are not permitted");
6310 postfix_expression = error_mark_node;
6311 cfun->calls_cilk_spawn = 0;
6313 else
6315 postfix_expression = build_cilk_spawn (token->location,
6316 postfix_expression);
6317 if (postfix_expression != error_mark_node)
6318 SET_EXPR_LOCATION (postfix_expression, input_location);
6319 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6321 break;
6324 case RID_BUILTIN_SHUFFLE:
6326 vec<tree, va_gc> *vec;
6327 unsigned int i;
6328 tree p;
6330 cp_lexer_consume_token (parser->lexer);
6331 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6332 /*cast_p=*/false, /*allow_expansion_p=*/true,
6333 /*non_constant_p=*/NULL);
6334 if (vec == NULL)
6335 return error_mark_node;
6337 FOR_EACH_VEC_ELT (*vec, i, p)
6338 mark_exp_read (p);
6340 if (vec->length () == 2)
6341 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6342 tf_warning_or_error);
6343 else if (vec->length () == 3)
6344 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6345 tf_warning_or_error);
6346 else
6348 error_at (loc, "wrong number of arguments to "
6349 "%<__builtin_shuffle%>");
6350 return error_mark_node;
6352 break;
6355 default:
6357 tree type;
6359 /* If the next thing is a simple-type-specifier, we may be
6360 looking at a functional cast. We could also be looking at
6361 an id-expression. So, we try the functional cast, and if
6362 that doesn't work we fall back to the primary-expression. */
6363 cp_parser_parse_tentatively (parser);
6364 /* Look for the simple-type-specifier. */
6365 ++parser->prevent_constrained_type_specifiers;
6366 type = cp_parser_simple_type_specifier (parser,
6367 /*decl_specs=*/NULL,
6368 CP_PARSER_FLAGS_NONE);
6369 --parser->prevent_constrained_type_specifiers;
6370 /* Parse the cast itself. */
6371 if (!cp_parser_error_occurred (parser))
6372 postfix_expression
6373 = cp_parser_functional_cast (parser, type);
6374 /* If that worked, we're done. */
6375 if (cp_parser_parse_definitely (parser))
6376 break;
6378 /* If the functional-cast didn't work out, try a
6379 compound-literal. */
6380 if (cp_parser_allow_gnu_extensions_p (parser)
6381 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6383 tree initializer = NULL_TREE;
6385 cp_parser_parse_tentatively (parser);
6387 /* Avoid calling cp_parser_type_id pointlessly, see comment
6388 in cp_parser_cast_expression about c++/29234. */
6389 if (!cp_parser_compound_literal_p (parser))
6390 cp_parser_simulate_error (parser);
6391 else
6393 /* Parse the type. */
6394 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6395 parser->in_type_id_in_expr_p = true;
6396 type = cp_parser_type_id (parser);
6397 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6398 /* Look for the `)'. */
6399 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6402 /* If things aren't going well, there's no need to
6403 keep going. */
6404 if (!cp_parser_error_occurred (parser))
6406 bool non_constant_p;
6407 /* Parse the brace-enclosed initializer list. */
6408 initializer = cp_parser_braced_list (parser,
6409 &non_constant_p);
6411 /* If that worked, we're definitely looking at a
6412 compound-literal expression. */
6413 if (cp_parser_parse_definitely (parser))
6415 /* Warn the user that a compound literal is not
6416 allowed in standard C++. */
6417 pedwarn (input_location, OPT_Wpedantic,
6418 "ISO C++ forbids compound-literals");
6419 /* For simplicity, we disallow compound literals in
6420 constant-expressions. We could
6421 allow compound literals of integer type, whose
6422 initializer was a constant, in constant
6423 expressions. Permitting that usage, as a further
6424 extension, would not change the meaning of any
6425 currently accepted programs. (Of course, as
6426 compound literals are not part of ISO C++, the
6427 standard has nothing to say.) */
6428 if (cp_parser_non_integral_constant_expression (parser,
6429 NIC_NCC))
6431 postfix_expression = error_mark_node;
6432 break;
6434 /* Form the representation of the compound-literal. */
6435 postfix_expression
6436 = finish_compound_literal (type, initializer,
6437 tf_warning_or_error);
6438 break;
6442 /* It must be a primary-expression. */
6443 postfix_expression
6444 = cp_parser_primary_expression (parser, address_p, cast_p,
6445 /*template_arg_p=*/false,
6446 decltype_p,
6447 &idk);
6449 break;
6452 /* Note that we don't need to worry about calling build_cplus_new on a
6453 class-valued CALL_EXPR in decltype when it isn't the end of the
6454 postfix-expression; unary_complex_lvalue will take care of that for
6455 all these cases. */
6457 /* Keep looping until the postfix-expression is complete. */
6458 while (true)
6460 if (idk == CP_ID_KIND_UNQUALIFIED
6461 && identifier_p (postfix_expression)
6462 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6463 /* It is not a Koenig lookup function call. */
6464 postfix_expression
6465 = unqualified_name_lookup_error (postfix_expression);
6467 /* Peek at the next token. */
6468 token = cp_lexer_peek_token (parser->lexer);
6470 switch (token->type)
6472 case CPP_OPEN_SQUARE:
6473 if (cp_next_tokens_can_be_std_attribute_p (parser))
6475 cp_parser_error (parser,
6476 "two consecutive %<[%> shall "
6477 "only introduce an attribute");
6478 return error_mark_node;
6480 postfix_expression
6481 = cp_parser_postfix_open_square_expression (parser,
6482 postfix_expression,
6483 false,
6484 decltype_p);
6485 idk = CP_ID_KIND_NONE;
6486 is_member_access = false;
6487 break;
6489 case CPP_OPEN_PAREN:
6490 /* postfix-expression ( expression-list [opt] ) */
6492 bool koenig_p;
6493 bool is_builtin_constant_p;
6494 bool saved_integral_constant_expression_p = false;
6495 bool saved_non_integral_constant_expression_p = false;
6496 tsubst_flags_t complain = complain_flags (decltype_p);
6497 vec<tree, va_gc> *args;
6499 is_member_access = false;
6501 is_builtin_constant_p
6502 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6503 if (is_builtin_constant_p)
6505 /* The whole point of __builtin_constant_p is to allow
6506 non-constant expressions to appear as arguments. */
6507 saved_integral_constant_expression_p
6508 = parser->integral_constant_expression_p;
6509 saved_non_integral_constant_expression_p
6510 = parser->non_integral_constant_expression_p;
6511 parser->integral_constant_expression_p = false;
6513 args = (cp_parser_parenthesized_expression_list
6514 (parser, non_attr,
6515 /*cast_p=*/false, /*allow_expansion_p=*/true,
6516 /*non_constant_p=*/NULL));
6517 if (is_builtin_constant_p)
6519 parser->integral_constant_expression_p
6520 = saved_integral_constant_expression_p;
6521 parser->non_integral_constant_expression_p
6522 = saved_non_integral_constant_expression_p;
6525 if (args == NULL)
6527 postfix_expression = error_mark_node;
6528 break;
6531 /* Function calls are not permitted in
6532 constant-expressions. */
6533 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6534 && cp_parser_non_integral_constant_expression (parser,
6535 NIC_FUNC_CALL))
6537 postfix_expression = error_mark_node;
6538 release_tree_vector (args);
6539 break;
6542 koenig_p = false;
6543 if (idk == CP_ID_KIND_UNQUALIFIED
6544 || idk == CP_ID_KIND_TEMPLATE_ID)
6546 if (identifier_p (postfix_expression))
6548 if (!args->is_empty ())
6550 koenig_p = true;
6551 if (!any_type_dependent_arguments_p (args))
6552 postfix_expression
6553 = perform_koenig_lookup (postfix_expression, args,
6554 complain);
6556 else
6557 postfix_expression
6558 = unqualified_fn_lookup_error (postfix_expression);
6560 /* We do not perform argument-dependent lookup if
6561 normal lookup finds a non-function, in accordance
6562 with the expected resolution of DR 218. */
6563 else if (!args->is_empty ()
6564 && is_overloaded_fn (postfix_expression))
6566 tree fn = get_first_fn (postfix_expression);
6567 fn = STRIP_TEMPLATE (fn);
6569 /* Do not do argument dependent lookup if regular
6570 lookup finds a member function or a block-scope
6571 function declaration. [basic.lookup.argdep]/3 */
6572 if (!DECL_FUNCTION_MEMBER_P (fn)
6573 && !DECL_LOCAL_FUNCTION_P (fn))
6575 koenig_p = true;
6576 if (!any_type_dependent_arguments_p (args))
6577 postfix_expression
6578 = perform_koenig_lookup (postfix_expression, args,
6579 complain);
6584 if (warn_memset_transposed_args)
6586 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6587 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6588 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6589 && vec_safe_length (args) == 3
6590 && TREE_CODE ((*args)[2]) == INTEGER_CST
6591 && integer_zerop ((*args)[2])
6592 && !(TREE_CODE ((*args)[1]) == INTEGER_CST
6593 && integer_zerop ((*args)[1])))
6594 warning (OPT_Wmemset_transposed_args,
6595 "%<memset%> used with constant zero length "
6596 "parameter; this could be due to transposed "
6597 "parameters");
6600 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6602 tree instance = TREE_OPERAND (postfix_expression, 0);
6603 tree fn = TREE_OPERAND (postfix_expression, 1);
6605 if (processing_template_decl
6606 && (type_dependent_expression_p (instance)
6607 || (!BASELINK_P (fn)
6608 && TREE_CODE (fn) != FIELD_DECL)
6609 || type_dependent_expression_p (fn)
6610 || any_type_dependent_arguments_p (args)))
6612 postfix_expression
6613 = build_nt_call_vec (postfix_expression, args);
6614 release_tree_vector (args);
6615 break;
6618 if (BASELINK_P (fn))
6620 postfix_expression
6621 = (build_new_method_call
6622 (instance, fn, &args, NULL_TREE,
6623 (idk == CP_ID_KIND_QUALIFIED
6624 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6625 : LOOKUP_NORMAL),
6626 /*fn_p=*/NULL,
6627 complain));
6629 else
6630 postfix_expression
6631 = finish_call_expr (postfix_expression, &args,
6632 /*disallow_virtual=*/false,
6633 /*koenig_p=*/false,
6634 complain);
6636 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6637 || TREE_CODE (postfix_expression) == MEMBER_REF
6638 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6639 postfix_expression = (build_offset_ref_call_from_tree
6640 (postfix_expression, &args,
6641 complain));
6642 else if (idk == CP_ID_KIND_QUALIFIED)
6643 /* A call to a static class member, or a namespace-scope
6644 function. */
6645 postfix_expression
6646 = finish_call_expr (postfix_expression, &args,
6647 /*disallow_virtual=*/true,
6648 koenig_p,
6649 complain);
6650 else
6651 /* All other function calls. */
6652 postfix_expression
6653 = finish_call_expr (postfix_expression, &args,
6654 /*disallow_virtual=*/false,
6655 koenig_p,
6656 complain);
6658 protected_set_expr_location (postfix_expression, token->location);
6660 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6661 idk = CP_ID_KIND_NONE;
6663 release_tree_vector (args);
6665 break;
6667 case CPP_DOT:
6668 case CPP_DEREF:
6669 /* postfix-expression . template [opt] id-expression
6670 postfix-expression . pseudo-destructor-name
6671 postfix-expression -> template [opt] id-expression
6672 postfix-expression -> pseudo-destructor-name */
6674 /* Consume the `.' or `->' operator. */
6675 cp_lexer_consume_token (parser->lexer);
6677 postfix_expression
6678 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6679 postfix_expression,
6680 false, &idk, loc);
6682 is_member_access = true;
6683 break;
6685 case CPP_PLUS_PLUS:
6686 /* postfix-expression ++ */
6687 /* Consume the `++' token. */
6688 cp_lexer_consume_token (parser->lexer);
6689 /* Generate a representation for the complete expression. */
6690 postfix_expression
6691 = finish_increment_expr (postfix_expression,
6692 POSTINCREMENT_EXPR);
6693 /* Increments may not appear in constant-expressions. */
6694 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6695 postfix_expression = error_mark_node;
6696 idk = CP_ID_KIND_NONE;
6697 is_member_access = false;
6698 break;
6700 case CPP_MINUS_MINUS:
6701 /* postfix-expression -- */
6702 /* Consume the `--' token. */
6703 cp_lexer_consume_token (parser->lexer);
6704 /* Generate a representation for the complete expression. */
6705 postfix_expression
6706 = finish_increment_expr (postfix_expression,
6707 POSTDECREMENT_EXPR);
6708 /* Decrements may not appear in constant-expressions. */
6709 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6710 postfix_expression = error_mark_node;
6711 idk = CP_ID_KIND_NONE;
6712 is_member_access = false;
6713 break;
6715 default:
6716 if (pidk_return != NULL)
6717 * pidk_return = idk;
6718 if (member_access_only_p)
6719 return is_member_access? postfix_expression : error_mark_node;
6720 else
6721 return postfix_expression;
6725 /* We should never get here. */
6726 gcc_unreachable ();
6727 return error_mark_node;
6730 /* This function parses Cilk Plus array notations. If a normal array expr. is
6731 parsed then the array index is passed back to the caller through *INIT_INDEX
6732 and the function returns a NULL_TREE. If array notation expr. is parsed,
6733 then *INIT_INDEX is ignored by the caller and the function returns
6734 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6735 error_mark_node. */
6737 static tree
6738 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6739 tree array_value)
6741 cp_token *token = NULL;
6742 tree length_index, stride = NULL_TREE, value_tree, array_type;
6743 if (!array_value || array_value == error_mark_node)
6745 cp_parser_skip_to_end_of_statement (parser);
6746 return error_mark_node;
6749 array_type = TREE_TYPE (array_value);
6751 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6752 parser->colon_corrects_to_scope_p = false;
6753 token = cp_lexer_peek_token (parser->lexer);
6755 if (!token)
6757 cp_parser_error (parser, "expected %<:%> or numeral");
6758 return error_mark_node;
6760 else if (token->type == CPP_COLON)
6762 /* Consume the ':'. */
6763 cp_lexer_consume_token (parser->lexer);
6765 /* If we are here, then we have a case like this A[:]. */
6766 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6768 cp_parser_error (parser, "expected %<]%>");
6769 cp_parser_skip_to_end_of_statement (parser);
6770 return error_mark_node;
6772 *init_index = NULL_TREE;
6773 stride = NULL_TREE;
6774 length_index = NULL_TREE;
6776 else
6778 /* If we are here, then there are three valid possibilities:
6779 1. ARRAY [ EXP ]
6780 2. ARRAY [ EXP : EXP ]
6781 3. ARRAY [ EXP : EXP : EXP ] */
6783 *init_index = cp_parser_expression (parser);
6784 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6786 /* This indicates that we have a normal array expression. */
6787 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6788 return NULL_TREE;
6791 /* Consume the ':'. */
6792 cp_lexer_consume_token (parser->lexer);
6793 length_index = cp_parser_expression (parser);
6794 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6796 cp_lexer_consume_token (parser->lexer);
6797 stride = cp_parser_expression (parser);
6800 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6802 if (*init_index == error_mark_node || length_index == error_mark_node
6803 || stride == error_mark_node || array_type == error_mark_node)
6805 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6806 cp_lexer_consume_token (parser->lexer);
6807 return error_mark_node;
6809 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6811 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6812 length_index, stride, array_type);
6813 return value_tree;
6816 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6817 by cp_parser_builtin_offsetof. We're looking for
6819 postfix-expression [ expression ]
6820 postfix-expression [ braced-init-list ] (C++11)
6822 FOR_OFFSETOF is set if we're being called in that context, which
6823 changes how we deal with integer constant expressions. */
6825 static tree
6826 cp_parser_postfix_open_square_expression (cp_parser *parser,
6827 tree postfix_expression,
6828 bool for_offsetof,
6829 bool decltype_p)
6831 tree index = NULL_TREE;
6832 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6833 bool saved_greater_than_is_operator_p;
6835 /* Consume the `[' token. */
6836 cp_lexer_consume_token (parser->lexer);
6838 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6839 parser->greater_than_is_operator_p = true;
6841 /* Parse the index expression. */
6842 /* ??? For offsetof, there is a question of what to allow here. If
6843 offsetof is not being used in an integral constant expression context,
6844 then we *could* get the right answer by computing the value at runtime.
6845 If we are in an integral constant expression context, then we might
6846 could accept any constant expression; hard to say without analysis.
6847 Rather than open the barn door too wide right away, allow only integer
6848 constant expressions here. */
6849 if (for_offsetof)
6850 index = cp_parser_constant_expression (parser);
6851 else
6853 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6855 bool expr_nonconst_p;
6856 cp_lexer_set_source_position (parser->lexer);
6857 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6858 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6859 if (flag_cilkplus
6860 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6862 error_at (cp_lexer_peek_token (parser->lexer)->location,
6863 "braced list index is not allowed with array "
6864 "notation");
6865 cp_parser_skip_to_end_of_statement (parser);
6866 return error_mark_node;
6869 else if (flag_cilkplus)
6871 /* Here are have these two options:
6872 ARRAY[EXP : EXP] - Array notation expr with default
6873 stride of 1.
6874 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6875 stride. */
6876 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6877 postfix_expression);
6878 if (an_exp)
6879 return an_exp;
6881 else
6882 index = cp_parser_expression (parser);
6885 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6887 /* Look for the closing `]'. */
6888 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6890 /* Build the ARRAY_REF. */
6891 postfix_expression = grok_array_decl (loc, postfix_expression,
6892 index, decltype_p);
6894 /* When not doing offsetof, array references are not permitted in
6895 constant-expressions. */
6896 if (!for_offsetof
6897 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6898 postfix_expression = error_mark_node;
6900 return postfix_expression;
6903 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6904 by cp_parser_builtin_offsetof. We're looking for
6906 postfix-expression . template [opt] id-expression
6907 postfix-expression . pseudo-destructor-name
6908 postfix-expression -> template [opt] id-expression
6909 postfix-expression -> pseudo-destructor-name
6911 FOR_OFFSETOF is set if we're being called in that context. That sorta
6912 limits what of the above we'll actually accept, but nevermind.
6913 TOKEN_TYPE is the "." or "->" token, which will already have been
6914 removed from the stream. */
6916 static tree
6917 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6918 enum cpp_ttype token_type,
6919 tree postfix_expression,
6920 bool for_offsetof, cp_id_kind *idk,
6921 location_t location)
6923 tree name;
6924 bool dependent_p;
6925 bool pseudo_destructor_p;
6926 tree scope = NULL_TREE;
6928 /* If this is a `->' operator, dereference the pointer. */
6929 if (token_type == CPP_DEREF)
6930 postfix_expression = build_x_arrow (location, postfix_expression,
6931 tf_warning_or_error);
6932 /* Check to see whether or not the expression is type-dependent. */
6933 dependent_p = type_dependent_expression_p (postfix_expression);
6934 /* The identifier following the `->' or `.' is not qualified. */
6935 parser->scope = NULL_TREE;
6936 parser->qualifying_scope = NULL_TREE;
6937 parser->object_scope = NULL_TREE;
6938 *idk = CP_ID_KIND_NONE;
6940 /* Enter the scope corresponding to the type of the object
6941 given by the POSTFIX_EXPRESSION. */
6942 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6944 scope = TREE_TYPE (postfix_expression);
6945 /* According to the standard, no expression should ever have
6946 reference type. Unfortunately, we do not currently match
6947 the standard in this respect in that our internal representation
6948 of an expression may have reference type even when the standard
6949 says it does not. Therefore, we have to manually obtain the
6950 underlying type here. */
6951 scope = non_reference (scope);
6952 /* The type of the POSTFIX_EXPRESSION must be complete. */
6953 if (scope == unknown_type_node)
6955 error_at (location, "%qE does not have class type",
6956 postfix_expression);
6957 scope = NULL_TREE;
6959 /* Unlike the object expression in other contexts, *this is not
6960 required to be of complete type for purposes of class member
6961 access (5.2.5) outside the member function body. */
6962 else if (postfix_expression != current_class_ref
6963 && !(processing_template_decl && scope == current_class_type))
6964 scope = complete_type_or_else (scope, NULL_TREE);
6965 /* Let the name lookup machinery know that we are processing a
6966 class member access expression. */
6967 parser->context->object_type = scope;
6968 /* If something went wrong, we want to be able to discern that case,
6969 as opposed to the case where there was no SCOPE due to the type
6970 of expression being dependent. */
6971 if (!scope)
6972 scope = error_mark_node;
6973 /* If the SCOPE was erroneous, make the various semantic analysis
6974 functions exit quickly -- and without issuing additional error
6975 messages. */
6976 if (scope == error_mark_node)
6977 postfix_expression = error_mark_node;
6980 /* Assume this expression is not a pseudo-destructor access. */
6981 pseudo_destructor_p = false;
6983 /* If the SCOPE is a scalar type, then, if this is a valid program,
6984 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6985 is type dependent, it can be pseudo-destructor-name or something else.
6986 Try to parse it as pseudo-destructor-name first. */
6987 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6989 tree s;
6990 tree type;
6992 cp_parser_parse_tentatively (parser);
6993 /* Parse the pseudo-destructor-name. */
6994 s = NULL_TREE;
6995 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6996 &s, &type);
6997 if (dependent_p
6998 && (cp_parser_error_occurred (parser)
6999 || !SCALAR_TYPE_P (type)))
7000 cp_parser_abort_tentative_parse (parser);
7001 else if (cp_parser_parse_definitely (parser))
7003 pseudo_destructor_p = true;
7004 postfix_expression
7005 = finish_pseudo_destructor_expr (postfix_expression,
7006 s, type, location);
7010 if (!pseudo_destructor_p)
7012 /* If the SCOPE is not a scalar type, we are looking at an
7013 ordinary class member access expression, rather than a
7014 pseudo-destructor-name. */
7015 bool template_p;
7016 cp_token *token = cp_lexer_peek_token (parser->lexer);
7017 /* Parse the id-expression. */
7018 name = (cp_parser_id_expression
7019 (parser,
7020 cp_parser_optional_template_keyword (parser),
7021 /*check_dependency_p=*/true,
7022 &template_p,
7023 /*declarator_p=*/false,
7024 /*optional_p=*/false));
7025 /* In general, build a SCOPE_REF if the member name is qualified.
7026 However, if the name was not dependent and has already been
7027 resolved; there is no need to build the SCOPE_REF. For example;
7029 struct X { void f(); };
7030 template <typename T> void f(T* t) { t->X::f(); }
7032 Even though "t" is dependent, "X::f" is not and has been resolved
7033 to a BASELINK; there is no need to include scope information. */
7035 /* But we do need to remember that there was an explicit scope for
7036 virtual function calls. */
7037 if (parser->scope)
7038 *idk = CP_ID_KIND_QUALIFIED;
7040 /* If the name is a template-id that names a type, we will get a
7041 TYPE_DECL here. That is invalid code. */
7042 if (TREE_CODE (name) == TYPE_DECL)
7044 error_at (token->location, "invalid use of %qD", name);
7045 postfix_expression = error_mark_node;
7047 else
7049 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7051 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7053 error_at (token->location, "%<%D::%D%> is not a class member",
7054 parser->scope, name);
7055 postfix_expression = error_mark_node;
7057 else
7058 name = build_qualified_name (/*type=*/NULL_TREE,
7059 parser->scope,
7060 name,
7061 template_p);
7062 parser->scope = NULL_TREE;
7063 parser->qualifying_scope = NULL_TREE;
7064 parser->object_scope = NULL_TREE;
7066 if (parser->scope && name && BASELINK_P (name))
7067 adjust_result_of_qualified_name_lookup
7068 (name, parser->scope, scope);
7069 postfix_expression
7070 = finish_class_member_access_expr (postfix_expression, name,
7071 template_p,
7072 tf_warning_or_error);
7076 /* We no longer need to look up names in the scope of the object on
7077 the left-hand side of the `.' or `->' operator. */
7078 parser->context->object_type = NULL_TREE;
7080 /* Outside of offsetof, these operators may not appear in
7081 constant-expressions. */
7082 if (!for_offsetof
7083 && (cp_parser_non_integral_constant_expression
7084 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7085 postfix_expression = error_mark_node;
7087 return postfix_expression;
7090 /* Parse a parenthesized expression-list.
7092 expression-list:
7093 assignment-expression
7094 expression-list, assignment-expression
7096 attribute-list:
7097 expression-list
7098 identifier
7099 identifier, expression-list
7101 CAST_P is true if this expression is the target of a cast.
7103 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7104 argument pack.
7106 Returns a vector of trees. Each element is a representation of an
7107 assignment-expression. NULL is returned if the ( and or ) are
7108 missing. An empty, but allocated, vector is returned on no
7109 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7110 if we are parsing an attribute list for an attribute that wants a
7111 plain identifier argument, normal_attr for an attribute that wants
7112 an expression, or non_attr if we aren't parsing an attribute list. If
7113 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7114 not all of the expressions in the list were constant. */
7116 static vec<tree, va_gc> *
7117 cp_parser_parenthesized_expression_list (cp_parser* parser,
7118 int is_attribute_list,
7119 bool cast_p,
7120 bool allow_expansion_p,
7121 bool *non_constant_p)
7123 vec<tree, va_gc> *expression_list;
7124 bool fold_expr_p = is_attribute_list != non_attr;
7125 tree identifier = NULL_TREE;
7126 bool saved_greater_than_is_operator_p;
7128 /* Assume all the expressions will be constant. */
7129 if (non_constant_p)
7130 *non_constant_p = false;
7132 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7133 return NULL;
7135 expression_list = make_tree_vector ();
7137 /* Within a parenthesized expression, a `>' token is always
7138 the greater-than operator. */
7139 saved_greater_than_is_operator_p
7140 = parser->greater_than_is_operator_p;
7141 parser->greater_than_is_operator_p = true;
7143 /* Consume expressions until there are no more. */
7144 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7145 while (true)
7147 tree expr;
7149 /* At the beginning of attribute lists, check to see if the
7150 next token is an identifier. */
7151 if (is_attribute_list == id_attr
7152 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7154 cp_token *token;
7156 /* Consume the identifier. */
7157 token = cp_lexer_consume_token (parser->lexer);
7158 /* Save the identifier. */
7159 identifier = token->u.value;
7161 else
7163 bool expr_non_constant_p;
7165 /* Parse the next assignment-expression. */
7166 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7168 /* A braced-init-list. */
7169 cp_lexer_set_source_position (parser->lexer);
7170 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7171 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7172 if (non_constant_p && expr_non_constant_p)
7173 *non_constant_p = true;
7175 else if (non_constant_p)
7177 expr = (cp_parser_constant_expression
7178 (parser, /*allow_non_constant_p=*/true,
7179 &expr_non_constant_p));
7180 if (expr_non_constant_p)
7181 *non_constant_p = true;
7183 else
7184 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7185 cast_p);
7187 if (fold_expr_p)
7188 expr = instantiate_non_dependent_expr (expr);
7190 /* If we have an ellipsis, then this is an expression
7191 expansion. */
7192 if (allow_expansion_p
7193 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7195 /* Consume the `...'. */
7196 cp_lexer_consume_token (parser->lexer);
7198 /* Build the argument pack. */
7199 expr = make_pack_expansion (expr);
7202 /* Add it to the list. We add error_mark_node
7203 expressions to the list, so that we can still tell if
7204 the correct form for a parenthesized expression-list
7205 is found. That gives better errors. */
7206 vec_safe_push (expression_list, expr);
7208 if (expr == error_mark_node)
7209 goto skip_comma;
7212 /* After the first item, attribute lists look the same as
7213 expression lists. */
7214 is_attribute_list = non_attr;
7216 get_comma:;
7217 /* If the next token isn't a `,', then we are done. */
7218 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7219 break;
7221 /* Otherwise, consume the `,' and keep going. */
7222 cp_lexer_consume_token (parser->lexer);
7225 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7227 int ending;
7229 skip_comma:;
7230 /* We try and resync to an unnested comma, as that will give the
7231 user better diagnostics. */
7232 ending = cp_parser_skip_to_closing_parenthesis (parser,
7233 /*recovering=*/true,
7234 /*or_comma=*/true,
7235 /*consume_paren=*/true);
7236 if (ending < 0)
7237 goto get_comma;
7238 if (!ending)
7240 parser->greater_than_is_operator_p
7241 = saved_greater_than_is_operator_p;
7242 return NULL;
7246 parser->greater_than_is_operator_p
7247 = saved_greater_than_is_operator_p;
7249 if (identifier)
7250 vec_safe_insert (expression_list, 0, identifier);
7252 return expression_list;
7255 /* Parse a pseudo-destructor-name.
7257 pseudo-destructor-name:
7258 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7259 :: [opt] nested-name-specifier template template-id :: ~ type-name
7260 :: [opt] nested-name-specifier [opt] ~ type-name
7262 If either of the first two productions is used, sets *SCOPE to the
7263 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7264 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7265 or ERROR_MARK_NODE if the parse fails. */
7267 static void
7268 cp_parser_pseudo_destructor_name (cp_parser* parser,
7269 tree object,
7270 tree* scope,
7271 tree* type)
7273 bool nested_name_specifier_p;
7275 /* Handle ~auto. */
7276 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7277 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7278 && !type_dependent_expression_p (object))
7280 if (cxx_dialect < cxx14)
7281 pedwarn (input_location, 0,
7282 "%<~auto%> only available with "
7283 "-std=c++14 or -std=gnu++14");
7284 cp_lexer_consume_token (parser->lexer);
7285 cp_lexer_consume_token (parser->lexer);
7286 *scope = NULL_TREE;
7287 *type = TREE_TYPE (object);
7288 return;
7291 /* Assume that things will not work out. */
7292 *type = error_mark_node;
7294 /* Look for the optional `::' operator. */
7295 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7296 /* Look for the optional nested-name-specifier. */
7297 nested_name_specifier_p
7298 = (cp_parser_nested_name_specifier_opt (parser,
7299 /*typename_keyword_p=*/false,
7300 /*check_dependency_p=*/true,
7301 /*type_p=*/false,
7302 /*is_declaration=*/false)
7303 != NULL_TREE);
7304 /* Now, if we saw a nested-name-specifier, we might be doing the
7305 second production. */
7306 if (nested_name_specifier_p
7307 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7309 /* Consume the `template' keyword. */
7310 cp_lexer_consume_token (parser->lexer);
7311 /* Parse the template-id. */
7312 cp_parser_template_id (parser,
7313 /*template_keyword_p=*/true,
7314 /*check_dependency_p=*/false,
7315 class_type,
7316 /*is_declaration=*/true);
7317 /* Look for the `::' token. */
7318 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7320 /* If the next token is not a `~', then there might be some
7321 additional qualification. */
7322 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7324 /* At this point, we're looking for "type-name :: ~". The type-name
7325 must not be a class-name, since this is a pseudo-destructor. So,
7326 it must be either an enum-name, or a typedef-name -- both of which
7327 are just identifiers. So, we peek ahead to check that the "::"
7328 and "~" tokens are present; if they are not, then we can avoid
7329 calling type_name. */
7330 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7331 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7332 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7334 cp_parser_error (parser, "non-scalar type");
7335 return;
7338 /* Look for the type-name. */
7339 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7340 if (*scope == error_mark_node)
7341 return;
7343 /* Look for the `::' token. */
7344 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7346 else
7347 *scope = NULL_TREE;
7349 /* Look for the `~'. */
7350 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7352 /* Once we see the ~, this has to be a pseudo-destructor. */
7353 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7354 cp_parser_commit_to_topmost_tentative_parse (parser);
7356 /* Look for the type-name again. We are not responsible for
7357 checking that it matches the first type-name. */
7358 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7361 /* Parse a unary-expression.
7363 unary-expression:
7364 postfix-expression
7365 ++ cast-expression
7366 -- cast-expression
7367 unary-operator cast-expression
7368 sizeof unary-expression
7369 sizeof ( type-id )
7370 alignof ( type-id ) [C++0x]
7371 new-expression
7372 delete-expression
7374 GNU Extensions:
7376 unary-expression:
7377 __extension__ cast-expression
7378 __alignof__ unary-expression
7379 __alignof__ ( type-id )
7380 alignof unary-expression [C++0x]
7381 __real__ cast-expression
7382 __imag__ cast-expression
7383 && identifier
7384 sizeof ( type-id ) { initializer-list , [opt] }
7385 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7386 __alignof__ ( type-id ) { initializer-list , [opt] }
7388 ADDRESS_P is true iff the unary-expression is appearing as the
7389 operand of the `&' operator. CAST_P is true if this expression is
7390 the target of a cast.
7392 Returns a representation of the expression. */
7394 static tree
7395 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7396 bool address_p, bool cast_p, bool decltype_p)
7398 cp_token *token;
7399 enum tree_code unary_operator;
7401 /* Peek at the next token. */
7402 token = cp_lexer_peek_token (parser->lexer);
7403 /* Some keywords give away the kind of expression. */
7404 if (token->type == CPP_KEYWORD)
7406 enum rid keyword = token->keyword;
7408 switch (keyword)
7410 case RID_ALIGNOF:
7411 case RID_SIZEOF:
7413 tree operand, ret;
7414 enum tree_code op;
7415 location_t first_loc;
7417 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7418 /* Consume the token. */
7419 cp_lexer_consume_token (parser->lexer);
7420 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7421 /* Parse the operand. */
7422 operand = cp_parser_sizeof_operand (parser, keyword);
7424 if (TYPE_P (operand))
7425 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7426 else
7428 /* ISO C++ defines alignof only with types, not with
7429 expressions. So pedwarn if alignof is used with a non-
7430 type expression. However, __alignof__ is ok. */
7431 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7432 pedwarn (token->location, OPT_Wpedantic,
7433 "ISO C++ does not allow %<alignof%> "
7434 "with a non-type");
7436 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7438 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7439 SIZEOF_EXPR with the original operand. */
7440 if (op == SIZEOF_EXPR && ret != error_mark_node)
7442 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7444 if (!processing_template_decl && TYPE_P (operand))
7446 ret = build_min (SIZEOF_EXPR, size_type_node,
7447 build1 (NOP_EXPR, operand,
7448 error_mark_node));
7449 SIZEOF_EXPR_TYPE_P (ret) = 1;
7451 else
7452 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7453 TREE_SIDE_EFFECTS (ret) = 0;
7454 TREE_READONLY (ret) = 1;
7456 SET_EXPR_LOCATION (ret, first_loc);
7458 return ret;
7461 case RID_NEW:
7462 return cp_parser_new_expression (parser);
7464 case RID_DELETE:
7465 return cp_parser_delete_expression (parser);
7467 case RID_EXTENSION:
7469 /* The saved value of the PEDANTIC flag. */
7470 int saved_pedantic;
7471 tree expr;
7473 /* Save away the PEDANTIC flag. */
7474 cp_parser_extension_opt (parser, &saved_pedantic);
7475 /* Parse the cast-expression. */
7476 expr = cp_parser_simple_cast_expression (parser);
7477 /* Restore the PEDANTIC flag. */
7478 pedantic = saved_pedantic;
7480 return expr;
7483 case RID_REALPART:
7484 case RID_IMAGPART:
7486 tree expression;
7488 /* Consume the `__real__' or `__imag__' token. */
7489 cp_lexer_consume_token (parser->lexer);
7490 /* Parse the cast-expression. */
7491 expression = cp_parser_simple_cast_expression (parser);
7492 /* Create the complete representation. */
7493 return build_x_unary_op (token->location,
7494 (keyword == RID_REALPART
7495 ? REALPART_EXPR : IMAGPART_EXPR),
7496 expression,
7497 tf_warning_or_error);
7499 break;
7501 case RID_TRANSACTION_ATOMIC:
7502 case RID_TRANSACTION_RELAXED:
7503 return cp_parser_transaction_expression (parser, keyword);
7505 case RID_NOEXCEPT:
7507 tree expr;
7508 const char *saved_message;
7509 bool saved_integral_constant_expression_p;
7510 bool saved_non_integral_constant_expression_p;
7511 bool saved_greater_than_is_operator_p;
7513 cp_lexer_consume_token (parser->lexer);
7514 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7516 saved_message = parser->type_definition_forbidden_message;
7517 parser->type_definition_forbidden_message
7518 = G_("types may not be defined in %<noexcept%> expressions");
7520 saved_integral_constant_expression_p
7521 = parser->integral_constant_expression_p;
7522 saved_non_integral_constant_expression_p
7523 = parser->non_integral_constant_expression_p;
7524 parser->integral_constant_expression_p = false;
7526 saved_greater_than_is_operator_p
7527 = parser->greater_than_is_operator_p;
7528 parser->greater_than_is_operator_p = true;
7530 ++cp_unevaluated_operand;
7531 ++c_inhibit_evaluation_warnings;
7532 ++cp_noexcept_operand;
7533 expr = cp_parser_expression (parser);
7534 --cp_noexcept_operand;
7535 --c_inhibit_evaluation_warnings;
7536 --cp_unevaluated_operand;
7538 parser->greater_than_is_operator_p
7539 = saved_greater_than_is_operator_p;
7541 parser->integral_constant_expression_p
7542 = saved_integral_constant_expression_p;
7543 parser->non_integral_constant_expression_p
7544 = saved_non_integral_constant_expression_p;
7546 parser->type_definition_forbidden_message = saved_message;
7548 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7549 return finish_noexcept_expr (expr, tf_warning_or_error);
7552 default:
7553 break;
7557 /* Look for the `:: new' and `:: delete', which also signal the
7558 beginning of a new-expression, or delete-expression,
7559 respectively. If the next token is `::', then it might be one of
7560 these. */
7561 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7563 enum rid keyword;
7565 /* See if the token after the `::' is one of the keywords in
7566 which we're interested. */
7567 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7568 /* If it's `new', we have a new-expression. */
7569 if (keyword == RID_NEW)
7570 return cp_parser_new_expression (parser);
7571 /* Similarly, for `delete'. */
7572 else if (keyword == RID_DELETE)
7573 return cp_parser_delete_expression (parser);
7576 /* Look for a unary operator. */
7577 unary_operator = cp_parser_unary_operator (token);
7578 /* The `++' and `--' operators can be handled similarly, even though
7579 they are not technically unary-operators in the grammar. */
7580 if (unary_operator == ERROR_MARK)
7582 if (token->type == CPP_PLUS_PLUS)
7583 unary_operator = PREINCREMENT_EXPR;
7584 else if (token->type == CPP_MINUS_MINUS)
7585 unary_operator = PREDECREMENT_EXPR;
7586 /* Handle the GNU address-of-label extension. */
7587 else if (cp_parser_allow_gnu_extensions_p (parser)
7588 && token->type == CPP_AND_AND)
7590 tree identifier;
7591 tree expression;
7592 location_t loc = token->location;
7594 /* Consume the '&&' token. */
7595 cp_lexer_consume_token (parser->lexer);
7596 /* Look for the identifier. */
7597 identifier = cp_parser_identifier (parser);
7598 /* Create an expression representing the address. */
7599 expression = finish_label_address_expr (identifier, loc);
7600 if (cp_parser_non_integral_constant_expression (parser,
7601 NIC_ADDR_LABEL))
7602 expression = error_mark_node;
7603 return expression;
7606 if (unary_operator != ERROR_MARK)
7608 tree cast_expression;
7609 tree expression = error_mark_node;
7610 non_integral_constant non_constant_p = NIC_NONE;
7611 location_t loc = token->location;
7612 tsubst_flags_t complain = complain_flags (decltype_p);
7614 /* Consume the operator token. */
7615 token = cp_lexer_consume_token (parser->lexer);
7616 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
7618 /* Parse the cast-expression. */
7619 cast_expression
7620 = cp_parser_cast_expression (parser,
7621 unary_operator == ADDR_EXPR,
7622 /*cast_p=*/false,
7623 /*decltype*/false,
7624 pidk);
7625 /* Now, build an appropriate representation. */
7626 switch (unary_operator)
7628 case INDIRECT_REF:
7629 non_constant_p = NIC_STAR;
7630 expression = build_x_indirect_ref (loc, cast_expression,
7631 RO_UNARY_STAR,
7632 complain);
7633 break;
7635 case ADDR_EXPR:
7636 non_constant_p = NIC_ADDR;
7637 /* Fall through. */
7638 case BIT_NOT_EXPR:
7639 expression = build_x_unary_op (loc, unary_operator,
7640 cast_expression,
7641 complain);
7642 break;
7644 case PREINCREMENT_EXPR:
7645 case PREDECREMENT_EXPR:
7646 non_constant_p = unary_operator == PREINCREMENT_EXPR
7647 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7648 /* Fall through. */
7649 case NEGATE_EXPR:
7650 /* Immediately fold negation of a constant, unless the constant is 0
7651 (since -0 == 0) or it would overflow. */
7652 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
7653 && CONSTANT_CLASS_P (cast_expression)
7654 && !integer_zerop (cast_expression)
7655 && !TREE_OVERFLOW (cast_expression))
7657 tree folded = fold_build1 (unary_operator,
7658 TREE_TYPE (cast_expression),
7659 cast_expression);
7660 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
7662 expression = folded;
7663 break;
7666 /* Fall through. */
7667 case UNARY_PLUS_EXPR:
7668 case TRUTH_NOT_EXPR:
7669 expression = finish_unary_op_expr (loc, unary_operator,
7670 cast_expression, complain);
7671 break;
7673 default:
7674 gcc_unreachable ();
7677 if (non_constant_p != NIC_NONE
7678 && cp_parser_non_integral_constant_expression (parser,
7679 non_constant_p))
7680 expression = error_mark_node;
7682 return expression;
7685 return cp_parser_postfix_expression (parser, address_p, cast_p,
7686 /*member_access_only_p=*/false,
7687 decltype_p,
7688 pidk);
7691 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7692 unary-operator, the corresponding tree code is returned. */
7694 static enum tree_code
7695 cp_parser_unary_operator (cp_token* token)
7697 switch (token->type)
7699 case CPP_MULT:
7700 return INDIRECT_REF;
7702 case CPP_AND:
7703 return ADDR_EXPR;
7705 case CPP_PLUS:
7706 return UNARY_PLUS_EXPR;
7708 case CPP_MINUS:
7709 return NEGATE_EXPR;
7711 case CPP_NOT:
7712 return TRUTH_NOT_EXPR;
7714 case CPP_COMPL:
7715 return BIT_NOT_EXPR;
7717 default:
7718 return ERROR_MARK;
7722 /* Parse a new-expression.
7724 new-expression:
7725 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7726 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7728 Returns a representation of the expression. */
7730 static tree
7731 cp_parser_new_expression (cp_parser* parser)
7733 bool global_scope_p;
7734 vec<tree, va_gc> *placement;
7735 tree type;
7736 vec<tree, va_gc> *initializer;
7737 tree nelts = NULL_TREE;
7738 tree ret;
7740 /* Look for the optional `::' operator. */
7741 global_scope_p
7742 = (cp_parser_global_scope_opt (parser,
7743 /*current_scope_valid_p=*/false)
7744 != NULL_TREE);
7745 /* Look for the `new' operator. */
7746 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7747 /* There's no easy way to tell a new-placement from the
7748 `( type-id )' construct. */
7749 cp_parser_parse_tentatively (parser);
7750 /* Look for a new-placement. */
7751 placement = cp_parser_new_placement (parser);
7752 /* If that didn't work out, there's no new-placement. */
7753 if (!cp_parser_parse_definitely (parser))
7755 if (placement != NULL)
7756 release_tree_vector (placement);
7757 placement = NULL;
7760 /* If the next token is a `(', then we have a parenthesized
7761 type-id. */
7762 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7764 cp_token *token;
7765 const char *saved_message = parser->type_definition_forbidden_message;
7767 /* Consume the `('. */
7768 cp_lexer_consume_token (parser->lexer);
7770 /* Parse the type-id. */
7771 parser->type_definition_forbidden_message
7772 = G_("types may not be defined in a new-expression");
7773 type = cp_parser_type_id (parser);
7774 parser->type_definition_forbidden_message = saved_message;
7776 /* Look for the closing `)'. */
7777 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7778 token = cp_lexer_peek_token (parser->lexer);
7779 /* There should not be a direct-new-declarator in this production,
7780 but GCC used to allowed this, so we check and emit a sensible error
7781 message for this case. */
7782 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7784 error_at (token->location,
7785 "array bound forbidden after parenthesized type-id");
7786 inform (token->location,
7787 "try removing the parentheses around the type-id");
7788 cp_parser_direct_new_declarator (parser);
7791 /* Otherwise, there must be a new-type-id. */
7792 else
7793 type = cp_parser_new_type_id (parser, &nelts);
7795 /* If the next token is a `(' or '{', then we have a new-initializer. */
7796 cp_token *token = cp_lexer_peek_token (parser->lexer);
7797 if (token->type == CPP_OPEN_PAREN
7798 || token->type == CPP_OPEN_BRACE)
7799 initializer = cp_parser_new_initializer (parser);
7800 else
7801 initializer = NULL;
7803 /* A new-expression may not appear in an integral constant
7804 expression. */
7805 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7806 ret = error_mark_node;
7807 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
7808 of a new-type-id or type-id of a new-expression, the new-expression shall
7809 contain a new-initializer of the form ( assignment-expression )".
7810 Additionally, consistently with the spirit of DR 1467, we want to accept
7811 'new auto { 2 }' too. */
7812 else if (type_uses_auto (type)
7813 && (vec_safe_length (initializer) != 1
7814 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
7815 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
7817 error_at (token->location,
7818 "initialization of new-expression for type %<auto%> "
7819 "requires exactly one element");
7820 ret = error_mark_node;
7822 else
7824 /* Create a representation of the new-expression. */
7825 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7826 tf_warning_or_error);
7829 if (placement != NULL)
7830 release_tree_vector (placement);
7831 if (initializer != NULL)
7832 release_tree_vector (initializer);
7834 return ret;
7837 /* Parse a new-placement.
7839 new-placement:
7840 ( expression-list )
7842 Returns the same representation as for an expression-list. */
7844 static vec<tree, va_gc> *
7845 cp_parser_new_placement (cp_parser* parser)
7847 vec<tree, va_gc> *expression_list;
7849 /* Parse the expression-list. */
7850 expression_list = (cp_parser_parenthesized_expression_list
7851 (parser, non_attr, /*cast_p=*/false,
7852 /*allow_expansion_p=*/true,
7853 /*non_constant_p=*/NULL));
7855 if (expression_list && expression_list->is_empty ())
7856 error ("expected expression-list or type-id");
7858 return expression_list;
7861 /* Parse a new-type-id.
7863 new-type-id:
7864 type-specifier-seq new-declarator [opt]
7866 Returns the TYPE allocated. If the new-type-id indicates an array
7867 type, *NELTS is set to the number of elements in the last array
7868 bound; the TYPE will not include the last array bound. */
7870 static tree
7871 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7873 cp_decl_specifier_seq type_specifier_seq;
7874 cp_declarator *new_declarator;
7875 cp_declarator *declarator;
7876 cp_declarator *outer_declarator;
7877 const char *saved_message;
7879 /* The type-specifier sequence must not contain type definitions.
7880 (It cannot contain declarations of new types either, but if they
7881 are not definitions we will catch that because they are not
7882 complete.) */
7883 saved_message = parser->type_definition_forbidden_message;
7884 parser->type_definition_forbidden_message
7885 = G_("types may not be defined in a new-type-id");
7886 /* Parse the type-specifier-seq. */
7887 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7888 /*is_trailing_return=*/false,
7889 &type_specifier_seq);
7890 /* Restore the old message. */
7891 parser->type_definition_forbidden_message = saved_message;
7893 if (type_specifier_seq.type == error_mark_node)
7894 return error_mark_node;
7896 /* Parse the new-declarator. */
7897 new_declarator = cp_parser_new_declarator_opt (parser);
7899 /* Determine the number of elements in the last array dimension, if
7900 any. */
7901 *nelts = NULL_TREE;
7902 /* Skip down to the last array dimension. */
7903 declarator = new_declarator;
7904 outer_declarator = NULL;
7905 while (declarator && (declarator->kind == cdk_pointer
7906 || declarator->kind == cdk_ptrmem))
7908 outer_declarator = declarator;
7909 declarator = declarator->declarator;
7911 while (declarator
7912 && declarator->kind == cdk_array
7913 && declarator->declarator
7914 && declarator->declarator->kind == cdk_array)
7916 outer_declarator = declarator;
7917 declarator = declarator->declarator;
7920 if (declarator && declarator->kind == cdk_array)
7922 *nelts = declarator->u.array.bounds;
7923 if (*nelts == error_mark_node)
7924 *nelts = integer_one_node;
7926 if (outer_declarator)
7927 outer_declarator->declarator = declarator->declarator;
7928 else
7929 new_declarator = NULL;
7932 return groktypename (&type_specifier_seq, new_declarator, false);
7935 /* Parse an (optional) new-declarator.
7937 new-declarator:
7938 ptr-operator new-declarator [opt]
7939 direct-new-declarator
7941 Returns the declarator. */
7943 static cp_declarator *
7944 cp_parser_new_declarator_opt (cp_parser* parser)
7946 enum tree_code code;
7947 tree type, std_attributes = NULL_TREE;
7948 cp_cv_quals cv_quals;
7950 /* We don't know if there's a ptr-operator next, or not. */
7951 cp_parser_parse_tentatively (parser);
7952 /* Look for a ptr-operator. */
7953 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7954 /* If that worked, look for more new-declarators. */
7955 if (cp_parser_parse_definitely (parser))
7957 cp_declarator *declarator;
7959 /* Parse another optional declarator. */
7960 declarator = cp_parser_new_declarator_opt (parser);
7962 declarator = cp_parser_make_indirect_declarator
7963 (code, type, cv_quals, declarator, std_attributes);
7965 return declarator;
7968 /* If the next token is a `[', there is a direct-new-declarator. */
7969 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7970 return cp_parser_direct_new_declarator (parser);
7972 return NULL;
7975 /* Parse a direct-new-declarator.
7977 direct-new-declarator:
7978 [ expression ]
7979 direct-new-declarator [constant-expression]
7983 static cp_declarator *
7984 cp_parser_direct_new_declarator (cp_parser* parser)
7986 cp_declarator *declarator = NULL;
7988 while (true)
7990 tree expression;
7991 cp_token *token;
7993 /* Look for the opening `['. */
7994 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7996 token = cp_lexer_peek_token (parser->lexer);
7997 expression = cp_parser_expression (parser);
7998 /* The standard requires that the expression have integral
7999 type. DR 74 adds enumeration types. We believe that the
8000 real intent is that these expressions be handled like the
8001 expression in a `switch' condition, which also allows
8002 classes with a single conversion to integral or
8003 enumeration type. */
8004 if (!processing_template_decl)
8006 expression
8007 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8008 expression,
8009 /*complain=*/true);
8010 if (!expression)
8012 error_at (token->location,
8013 "expression in new-declarator must have integral "
8014 "or enumeration type");
8015 expression = error_mark_node;
8019 /* Look for the closing `]'. */
8020 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8022 /* Add this bound to the declarator. */
8023 declarator = make_array_declarator (declarator, expression);
8025 /* If the next token is not a `[', then there are no more
8026 bounds. */
8027 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8028 break;
8031 return declarator;
8034 /* Parse a new-initializer.
8036 new-initializer:
8037 ( expression-list [opt] )
8038 braced-init-list
8040 Returns a representation of the expression-list. */
8042 static vec<tree, va_gc> *
8043 cp_parser_new_initializer (cp_parser* parser)
8045 vec<tree, va_gc> *expression_list;
8047 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8049 tree t;
8050 bool expr_non_constant_p;
8051 cp_lexer_set_source_position (parser->lexer);
8052 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8053 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8054 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8055 expression_list = make_tree_vector_single (t);
8057 else
8058 expression_list = (cp_parser_parenthesized_expression_list
8059 (parser, non_attr, /*cast_p=*/false,
8060 /*allow_expansion_p=*/true,
8061 /*non_constant_p=*/NULL));
8063 return expression_list;
8066 /* Parse a delete-expression.
8068 delete-expression:
8069 :: [opt] delete cast-expression
8070 :: [opt] delete [ ] cast-expression
8072 Returns a representation of the expression. */
8074 static tree
8075 cp_parser_delete_expression (cp_parser* parser)
8077 bool global_scope_p;
8078 bool array_p;
8079 tree expression;
8081 /* Look for the optional `::' operator. */
8082 global_scope_p
8083 = (cp_parser_global_scope_opt (parser,
8084 /*current_scope_valid_p=*/false)
8085 != NULL_TREE);
8086 /* Look for the `delete' keyword. */
8087 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8088 /* See if the array syntax is in use. */
8089 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8091 /* Consume the `[' token. */
8092 cp_lexer_consume_token (parser->lexer);
8093 /* Look for the `]' token. */
8094 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8095 /* Remember that this is the `[]' construct. */
8096 array_p = true;
8098 else
8099 array_p = false;
8101 /* Parse the cast-expression. */
8102 expression = cp_parser_simple_cast_expression (parser);
8104 /* A delete-expression may not appear in an integral constant
8105 expression. */
8106 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8107 return error_mark_node;
8109 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8110 tf_warning_or_error);
8113 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8114 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8115 0 otherwise. */
8117 static int
8118 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8120 cp_token *token = cp_lexer_peek_token (parser->lexer);
8121 switch (token->type)
8123 case CPP_COMMA:
8124 case CPP_SEMICOLON:
8125 case CPP_QUERY:
8126 case CPP_COLON:
8127 case CPP_CLOSE_SQUARE:
8128 case CPP_CLOSE_PAREN:
8129 case CPP_CLOSE_BRACE:
8130 case CPP_OPEN_BRACE:
8131 case CPP_DOT:
8132 case CPP_DOT_STAR:
8133 case CPP_DEREF:
8134 case CPP_DEREF_STAR:
8135 case CPP_DIV:
8136 case CPP_MOD:
8137 case CPP_LSHIFT:
8138 case CPP_RSHIFT:
8139 case CPP_LESS:
8140 case CPP_GREATER:
8141 case CPP_LESS_EQ:
8142 case CPP_GREATER_EQ:
8143 case CPP_EQ_EQ:
8144 case CPP_NOT_EQ:
8145 case CPP_EQ:
8146 case CPP_MULT_EQ:
8147 case CPP_DIV_EQ:
8148 case CPP_MOD_EQ:
8149 case CPP_PLUS_EQ:
8150 case CPP_MINUS_EQ:
8151 case CPP_RSHIFT_EQ:
8152 case CPP_LSHIFT_EQ:
8153 case CPP_AND_EQ:
8154 case CPP_XOR_EQ:
8155 case CPP_OR_EQ:
8156 case CPP_XOR:
8157 case CPP_OR:
8158 case CPP_OR_OR:
8159 case CPP_EOF:
8160 case CPP_ELLIPSIS:
8161 return 0;
8163 case CPP_OPEN_PAREN:
8164 /* In ((type ()) () the last () isn't a valid cast-expression,
8165 so the whole must be parsed as postfix-expression. */
8166 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8167 != CPP_CLOSE_PAREN;
8169 case CPP_OPEN_SQUARE:
8170 /* '[' may start a primary-expression in obj-c++ and in C++11,
8171 as a lambda-expression, eg, '(void)[]{}'. */
8172 if (cxx_dialect >= cxx11)
8173 return -1;
8174 return c_dialect_objc ();
8176 case CPP_PLUS_PLUS:
8177 case CPP_MINUS_MINUS:
8178 /* '++' and '--' may or may not start a cast-expression:
8180 struct T { void operator++(int); };
8181 void f() { (T())++; }
8185 int a;
8186 (int)++a; */
8187 return -1;
8189 default:
8190 return 1;
8194 /* Parse a cast-expression.
8196 cast-expression:
8197 unary-expression
8198 ( type-id ) cast-expression
8200 ADDRESS_P is true iff the unary-expression is appearing as the
8201 operand of the `&' operator. CAST_P is true if this expression is
8202 the target of a cast.
8204 Returns a representation of the expression. */
8206 static tree
8207 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8208 bool decltype_p, cp_id_kind * pidk)
8210 /* If it's a `(', then we might be looking at a cast. */
8211 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8213 tree type = NULL_TREE;
8214 tree expr = NULL_TREE;
8215 int cast_expression = 0;
8216 const char *saved_message;
8218 /* There's no way to know yet whether or not this is a cast.
8219 For example, `(int (3))' is a unary-expression, while `(int)
8220 3' is a cast. So, we resort to parsing tentatively. */
8221 cp_parser_parse_tentatively (parser);
8222 /* Types may not be defined in a cast. */
8223 saved_message = parser->type_definition_forbidden_message;
8224 parser->type_definition_forbidden_message
8225 = G_("types may not be defined in casts");
8226 /* Consume the `('. */
8227 cp_lexer_consume_token (parser->lexer);
8228 /* A very tricky bit is that `(struct S) { 3 }' is a
8229 compound-literal (which we permit in C++ as an extension).
8230 But, that construct is not a cast-expression -- it is a
8231 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8232 is legal; if the compound-literal were a cast-expression,
8233 you'd need an extra set of parentheses.) But, if we parse
8234 the type-id, and it happens to be a class-specifier, then we
8235 will commit to the parse at that point, because we cannot
8236 undo the action that is done when creating a new class. So,
8237 then we cannot back up and do a postfix-expression.
8239 Another tricky case is the following (c++/29234):
8241 struct S { void operator () (); };
8243 void foo ()
8245 ( S()() );
8248 As a type-id we parse the parenthesized S()() as a function
8249 returning a function, groktypename complains and we cannot
8250 back up in this case either.
8252 Therefore, we scan ahead to the closing `)', and check to see
8253 if the tokens after the `)' can start a cast-expression. Otherwise
8254 we are dealing with an unary-expression, a postfix-expression
8255 or something else.
8257 Yet another tricky case, in C++11, is the following (c++/54891):
8259 (void)[]{};
8261 The issue is that usually, besides the case of lambda-expressions,
8262 the parenthesized type-id cannot be followed by '[', and, eg, we
8263 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8264 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8265 we don't commit, we try a cast-expression, then an unary-expression.
8267 Save tokens so that we can put them back. */
8268 cp_lexer_save_tokens (parser->lexer);
8270 /* We may be looking at a cast-expression. */
8271 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8272 /*consume_paren=*/true))
8273 cast_expression
8274 = cp_parser_tokens_start_cast_expression (parser);
8276 /* Roll back the tokens we skipped. */
8277 cp_lexer_rollback_tokens (parser->lexer);
8278 /* If we aren't looking at a cast-expression, simulate an error so
8279 that the call to cp_parser_error_occurred below returns true. */
8280 if (!cast_expression)
8281 cp_parser_simulate_error (parser);
8282 else
8284 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8285 parser->in_type_id_in_expr_p = true;
8286 /* Look for the type-id. */
8287 type = cp_parser_type_id (parser);
8288 /* Look for the closing `)'. */
8289 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8290 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8293 /* Restore the saved message. */
8294 parser->type_definition_forbidden_message = saved_message;
8296 /* At this point this can only be either a cast or a
8297 parenthesized ctor such as `(T ())' that looks like a cast to
8298 function returning T. */
8299 if (!cp_parser_error_occurred (parser))
8301 /* Only commit if the cast-expression doesn't start with
8302 '++', '--', or '[' in C++11. */
8303 if (cast_expression > 0)
8304 cp_parser_commit_to_topmost_tentative_parse (parser);
8306 expr = cp_parser_cast_expression (parser,
8307 /*address_p=*/false,
8308 /*cast_p=*/true,
8309 /*decltype_p=*/false,
8310 pidk);
8312 if (cp_parser_parse_definitely (parser))
8314 /* Warn about old-style casts, if so requested. */
8315 if (warn_old_style_cast
8316 && !in_system_header_at (input_location)
8317 && !VOID_TYPE_P (type)
8318 && current_lang_name != lang_name_c)
8319 warning (OPT_Wold_style_cast, "use of old-style cast");
8321 /* Only type conversions to integral or enumeration types
8322 can be used in constant-expressions. */
8323 if (!cast_valid_in_integral_constant_expression_p (type)
8324 && cp_parser_non_integral_constant_expression (parser,
8325 NIC_CAST))
8326 return error_mark_node;
8328 /* Perform the cast. */
8329 expr = build_c_cast (input_location, type, expr);
8330 return expr;
8333 else
8334 cp_parser_abort_tentative_parse (parser);
8337 /* If we get here, then it's not a cast, so it must be a
8338 unary-expression. */
8339 return cp_parser_unary_expression (parser, pidk, address_p,
8340 cast_p, decltype_p);
8343 /* Parse a binary expression of the general form:
8345 pm-expression:
8346 cast-expression
8347 pm-expression .* cast-expression
8348 pm-expression ->* cast-expression
8350 multiplicative-expression:
8351 pm-expression
8352 multiplicative-expression * pm-expression
8353 multiplicative-expression / pm-expression
8354 multiplicative-expression % pm-expression
8356 additive-expression:
8357 multiplicative-expression
8358 additive-expression + multiplicative-expression
8359 additive-expression - multiplicative-expression
8361 shift-expression:
8362 additive-expression
8363 shift-expression << additive-expression
8364 shift-expression >> additive-expression
8366 relational-expression:
8367 shift-expression
8368 relational-expression < shift-expression
8369 relational-expression > shift-expression
8370 relational-expression <= shift-expression
8371 relational-expression >= shift-expression
8373 GNU Extension:
8375 relational-expression:
8376 relational-expression <? shift-expression
8377 relational-expression >? shift-expression
8379 equality-expression:
8380 relational-expression
8381 equality-expression == relational-expression
8382 equality-expression != relational-expression
8384 and-expression:
8385 equality-expression
8386 and-expression & equality-expression
8388 exclusive-or-expression:
8389 and-expression
8390 exclusive-or-expression ^ and-expression
8392 inclusive-or-expression:
8393 exclusive-or-expression
8394 inclusive-or-expression | exclusive-or-expression
8396 logical-and-expression:
8397 inclusive-or-expression
8398 logical-and-expression && inclusive-or-expression
8400 logical-or-expression:
8401 logical-and-expression
8402 logical-or-expression || logical-and-expression
8404 All these are implemented with a single function like:
8406 binary-expression:
8407 simple-cast-expression
8408 binary-expression <token> binary-expression
8410 CAST_P is true if this expression is the target of a cast.
8412 The binops_by_token map is used to get the tree codes for each <token> type.
8413 binary-expressions are associated according to a precedence table. */
8415 #define TOKEN_PRECEDENCE(token) \
8416 (((token->type == CPP_GREATER \
8417 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8418 && !parser->greater_than_is_operator_p) \
8419 ? PREC_NOT_OPERATOR \
8420 : binops_by_token[token->type].prec)
8422 static tree
8423 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8424 bool no_toplevel_fold_p,
8425 bool decltype_p,
8426 enum cp_parser_prec prec,
8427 cp_id_kind * pidk)
8429 cp_parser_expression_stack stack;
8430 cp_parser_expression_stack_entry *sp = &stack[0];
8431 cp_parser_expression_stack_entry current;
8432 tree rhs;
8433 cp_token *token;
8434 enum tree_code rhs_type;
8435 enum cp_parser_prec new_prec, lookahead_prec;
8436 tree overload;
8438 /* Parse the first expression. */
8439 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8440 ? TRUTH_NOT_EXPR : ERROR_MARK);
8441 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8442 cast_p, decltype_p, pidk);
8443 current.prec = prec;
8445 if (cp_parser_error_occurred (parser))
8446 return error_mark_node;
8448 for (;;)
8450 /* Get an operator token. */
8451 token = cp_lexer_peek_token (parser->lexer);
8453 if (warn_cxx11_compat
8454 && token->type == CPP_RSHIFT
8455 && !parser->greater_than_is_operator_p)
8457 if (warning_at (token->location, OPT_Wc__11_compat,
8458 "%<>>%> operator is treated"
8459 " as two right angle brackets in C++11"))
8460 inform (token->location,
8461 "suggest parentheses around %<>>%> expression");
8464 new_prec = TOKEN_PRECEDENCE (token);
8465 if (new_prec != PREC_NOT_OPERATOR
8466 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8467 /* This is a fold-expression; handle it later. */
8468 new_prec = PREC_NOT_OPERATOR;
8470 /* Popping an entry off the stack means we completed a subexpression:
8471 - either we found a token which is not an operator (`>' where it is not
8472 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8473 will happen repeatedly;
8474 - or, we found an operator which has lower priority. This is the case
8475 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8476 parsing `3 * 4'. */
8477 if (new_prec <= current.prec)
8479 if (sp == stack)
8480 break;
8481 else
8482 goto pop;
8485 get_rhs:
8486 current.tree_type = binops_by_token[token->type].tree_type;
8487 current.loc = token->location;
8489 /* We used the operator token. */
8490 cp_lexer_consume_token (parser->lexer);
8492 /* For "false && x" or "true || x", x will never be executed;
8493 disable warnings while evaluating it. */
8494 if (current.tree_type == TRUTH_ANDIF_EXPR)
8495 c_inhibit_evaluation_warnings +=
8496 cp_fully_fold (current.lhs) == truthvalue_false_node;
8497 else if (current.tree_type == TRUTH_ORIF_EXPR)
8498 c_inhibit_evaluation_warnings +=
8499 cp_fully_fold (current.lhs) == truthvalue_true_node;
8501 /* Extract another operand. It may be the RHS of this expression
8502 or the LHS of a new, higher priority expression. */
8503 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8504 ? TRUTH_NOT_EXPR : ERROR_MARK);
8505 rhs = cp_parser_simple_cast_expression (parser);
8507 /* Get another operator token. Look up its precedence to avoid
8508 building a useless (immediately popped) stack entry for common
8509 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8510 token = cp_lexer_peek_token (parser->lexer);
8511 lookahead_prec = TOKEN_PRECEDENCE (token);
8512 if (lookahead_prec != PREC_NOT_OPERATOR
8513 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8514 lookahead_prec = PREC_NOT_OPERATOR;
8515 if (lookahead_prec > new_prec)
8517 /* ... and prepare to parse the RHS of the new, higher priority
8518 expression. Since precedence levels on the stack are
8519 monotonically increasing, we do not have to care about
8520 stack overflows. */
8521 *sp = current;
8522 ++sp;
8523 current.lhs = rhs;
8524 current.lhs_type = rhs_type;
8525 current.prec = new_prec;
8526 new_prec = lookahead_prec;
8527 goto get_rhs;
8529 pop:
8530 lookahead_prec = new_prec;
8531 /* If the stack is not empty, we have parsed into LHS the right side
8532 (`4' in the example above) of an expression we had suspended.
8533 We can use the information on the stack to recover the LHS (`3')
8534 from the stack together with the tree code (`MULT_EXPR'), and
8535 the precedence of the higher level subexpression
8536 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8537 which will be used to actually build the additive expression. */
8538 rhs = current.lhs;
8539 rhs_type = current.lhs_type;
8540 --sp;
8541 current = *sp;
8544 /* Undo the disabling of warnings done above. */
8545 if (current.tree_type == TRUTH_ANDIF_EXPR)
8546 c_inhibit_evaluation_warnings -=
8547 cp_fully_fold (current.lhs) == truthvalue_false_node;
8548 else if (current.tree_type == TRUTH_ORIF_EXPR)
8549 c_inhibit_evaluation_warnings -=
8550 cp_fully_fold (current.lhs) == truthvalue_true_node;
8552 if (warn_logical_not_paren
8553 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8554 && current.lhs_type == TRUTH_NOT_EXPR
8555 /* Avoid warning for !!x == y. */
8556 && (TREE_CODE (current.lhs) != NE_EXPR
8557 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8558 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8559 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8560 /* Avoid warning for !b == y where b is boolean. */
8561 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8562 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8563 != BOOLEAN_TYPE))))
8564 /* Avoid warning for !!b == y where b is boolean. */
8565 && (!DECL_P (current.lhs)
8566 || TREE_TYPE (current.lhs) == NULL_TREE
8567 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8568 warn_logical_not_parentheses (current.loc, current.tree_type,
8569 maybe_constant_value (rhs));
8571 overload = NULL;
8572 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8573 ERROR_MARK for everything that is not a binary expression.
8574 This makes warn_about_parentheses miss some warnings that
8575 involve unary operators. For unary expressions we should
8576 pass the correct tree_code unless the unary expression was
8577 surrounded by parentheses.
8579 if (no_toplevel_fold_p
8580 && lookahead_prec <= current.prec
8581 && sp == stack)
8582 current.lhs = build2 (current.tree_type,
8583 TREE_CODE_CLASS (current.tree_type)
8584 == tcc_comparison
8585 ? boolean_type_node : TREE_TYPE (current.lhs),
8586 current.lhs, rhs);
8587 else
8588 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8589 current.lhs, current.lhs_type,
8590 rhs, rhs_type, &overload,
8591 complain_flags (decltype_p));
8592 current.lhs_type = current.tree_type;
8593 protected_set_expr_location (current.lhs, current.loc);
8595 /* If the binary operator required the use of an overloaded operator,
8596 then this expression cannot be an integral constant-expression.
8597 An overloaded operator can be used even if both operands are
8598 otherwise permissible in an integral constant-expression if at
8599 least one of the operands is of enumeration type. */
8601 if (overload
8602 && cp_parser_non_integral_constant_expression (parser,
8603 NIC_OVERLOADED))
8604 return error_mark_node;
8607 return current.lhs;
8610 static tree
8611 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8612 bool no_toplevel_fold_p,
8613 enum cp_parser_prec prec,
8614 cp_id_kind * pidk)
8616 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8617 /*decltype*/false, prec, pidk);
8620 /* Parse the `? expression : assignment-expression' part of a
8621 conditional-expression. The LOGICAL_OR_EXPR is the
8622 logical-or-expression that started the conditional-expression.
8623 Returns a representation of the entire conditional-expression.
8625 This routine is used by cp_parser_assignment_expression.
8627 ? expression : assignment-expression
8629 GNU Extensions:
8631 ? : assignment-expression */
8633 static tree
8634 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8636 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
8637 tree assignment_expr;
8638 struct cp_token *token;
8639 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8641 /* Consume the `?' token. */
8642 cp_lexer_consume_token (parser->lexer);
8643 token = cp_lexer_peek_token (parser->lexer);
8644 if (cp_parser_allow_gnu_extensions_p (parser)
8645 && token->type == CPP_COLON)
8647 pedwarn (token->location, OPT_Wpedantic,
8648 "ISO C++ does not allow ?: with omitted middle operand");
8649 /* Implicit true clause. */
8650 expr = NULL_TREE;
8651 c_inhibit_evaluation_warnings +=
8652 folded_logical_or_expr == truthvalue_true_node;
8653 warn_for_omitted_condop (token->location, logical_or_expr);
8655 else
8657 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8658 parser->colon_corrects_to_scope_p = false;
8659 /* Parse the expression. */
8660 c_inhibit_evaluation_warnings +=
8661 folded_logical_or_expr == truthvalue_false_node;
8662 expr = cp_parser_expression (parser);
8663 c_inhibit_evaluation_warnings +=
8664 ((folded_logical_or_expr == truthvalue_true_node)
8665 - (folded_logical_or_expr == truthvalue_false_node));
8666 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8669 /* The next token should be a `:'. */
8670 cp_parser_require (parser, CPP_COLON, RT_COLON);
8671 /* Parse the assignment-expression. */
8672 assignment_expr = cp_parser_assignment_expression (parser);
8673 c_inhibit_evaluation_warnings -=
8674 folded_logical_or_expr == truthvalue_true_node;
8676 /* Build the conditional-expression. */
8677 return build_x_conditional_expr (loc, logical_or_expr,
8678 expr,
8679 assignment_expr,
8680 tf_warning_or_error);
8683 /* Parse an assignment-expression.
8685 assignment-expression:
8686 conditional-expression
8687 logical-or-expression assignment-operator assignment_expression
8688 throw-expression
8690 CAST_P is true if this expression is the target of a cast.
8691 DECLTYPE_P is true if this expression is the operand of decltype.
8693 Returns a representation for the expression. */
8695 static tree
8696 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8697 bool cast_p, bool decltype_p)
8699 tree expr;
8701 /* If the next token is the `throw' keyword, then we're looking at
8702 a throw-expression. */
8703 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8704 expr = cp_parser_throw_expression (parser);
8705 /* Otherwise, it must be that we are looking at a
8706 logical-or-expression. */
8707 else
8709 /* Parse the binary expressions (logical-or-expression). */
8710 expr = cp_parser_binary_expression (parser, cast_p, false,
8711 decltype_p,
8712 PREC_NOT_OPERATOR, pidk);
8713 /* If the next token is a `?' then we're actually looking at a
8714 conditional-expression. */
8715 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8716 return cp_parser_question_colon_clause (parser, expr);
8717 else
8719 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8721 /* If it's an assignment-operator, we're using the second
8722 production. */
8723 enum tree_code assignment_operator
8724 = cp_parser_assignment_operator_opt (parser);
8725 if (assignment_operator != ERROR_MARK)
8727 bool non_constant_p;
8728 location_t saved_input_location;
8730 /* Parse the right-hand side of the assignment. */
8731 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8733 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8734 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8736 /* An assignment may not appear in a
8737 constant-expression. */
8738 if (cp_parser_non_integral_constant_expression (parser,
8739 NIC_ASSIGNMENT))
8740 return error_mark_node;
8741 /* Build the assignment expression. Its default
8742 location is the location of the '=' token. */
8743 saved_input_location = input_location;
8744 input_location = loc;
8745 expr = build_x_modify_expr (loc, expr,
8746 assignment_operator,
8747 rhs,
8748 complain_flags (decltype_p));
8749 input_location = saved_input_location;
8754 return expr;
8757 /* Parse an (optional) assignment-operator.
8759 assignment-operator: one of
8760 = *= /= %= += -= >>= <<= &= ^= |=
8762 GNU Extension:
8764 assignment-operator: one of
8765 <?= >?=
8767 If the next token is an assignment operator, the corresponding tree
8768 code is returned, and the token is consumed. For example, for
8769 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8770 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8771 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8772 operator, ERROR_MARK is returned. */
8774 static enum tree_code
8775 cp_parser_assignment_operator_opt (cp_parser* parser)
8777 enum tree_code op;
8778 cp_token *token;
8780 /* Peek at the next token. */
8781 token = cp_lexer_peek_token (parser->lexer);
8783 switch (token->type)
8785 case CPP_EQ:
8786 op = NOP_EXPR;
8787 break;
8789 case CPP_MULT_EQ:
8790 op = MULT_EXPR;
8791 break;
8793 case CPP_DIV_EQ:
8794 op = TRUNC_DIV_EXPR;
8795 break;
8797 case CPP_MOD_EQ:
8798 op = TRUNC_MOD_EXPR;
8799 break;
8801 case CPP_PLUS_EQ:
8802 op = PLUS_EXPR;
8803 break;
8805 case CPP_MINUS_EQ:
8806 op = MINUS_EXPR;
8807 break;
8809 case CPP_RSHIFT_EQ:
8810 op = RSHIFT_EXPR;
8811 break;
8813 case CPP_LSHIFT_EQ:
8814 op = LSHIFT_EXPR;
8815 break;
8817 case CPP_AND_EQ:
8818 op = BIT_AND_EXPR;
8819 break;
8821 case CPP_XOR_EQ:
8822 op = BIT_XOR_EXPR;
8823 break;
8825 case CPP_OR_EQ:
8826 op = BIT_IOR_EXPR;
8827 break;
8829 default:
8830 /* Nothing else is an assignment operator. */
8831 op = ERROR_MARK;
8834 /* An operator followed by ... is a fold-expression, handled elsewhere. */
8835 if (op != ERROR_MARK
8836 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8837 op = ERROR_MARK;
8839 /* If it was an assignment operator, consume it. */
8840 if (op != ERROR_MARK)
8841 cp_lexer_consume_token (parser->lexer);
8843 return op;
8846 /* Parse an expression.
8848 expression:
8849 assignment-expression
8850 expression , assignment-expression
8852 CAST_P is true if this expression is the target of a cast.
8853 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8854 except possibly parenthesized or on the RHS of a comma (N3276).
8856 Returns a representation of the expression. */
8858 static tree
8859 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8860 bool cast_p, bool decltype_p)
8862 tree expression = NULL_TREE;
8863 location_t loc = UNKNOWN_LOCATION;
8865 while (true)
8867 tree assignment_expression;
8869 /* Parse the next assignment-expression. */
8870 assignment_expression
8871 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8873 /* We don't create a temporary for a call that is the immediate operand
8874 of decltype or on the RHS of a comma. But when we see a comma, we
8875 need to create a temporary for a call on the LHS. */
8876 if (decltype_p && !processing_template_decl
8877 && TREE_CODE (assignment_expression) == CALL_EXPR
8878 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8879 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8880 assignment_expression
8881 = build_cplus_new (TREE_TYPE (assignment_expression),
8882 assignment_expression, tf_warning_or_error);
8884 /* If this is the first assignment-expression, we can just
8885 save it away. */
8886 if (!expression)
8887 expression = assignment_expression;
8888 else
8889 expression = build_x_compound_expr (loc, expression,
8890 assignment_expression,
8891 complain_flags (decltype_p));
8892 /* If the next token is not a comma, or we're in a fold-expression, then
8893 we are done with the expression. */
8894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8895 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8896 break;
8897 /* Consume the `,'. */
8898 loc = cp_lexer_peek_token (parser->lexer)->location;
8899 cp_lexer_consume_token (parser->lexer);
8900 /* A comma operator cannot appear in a constant-expression. */
8901 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8902 expression = error_mark_node;
8905 return expression;
8908 /* Parse a constant-expression.
8910 constant-expression:
8911 conditional-expression
8913 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8914 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8915 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8916 is false, NON_CONSTANT_P should be NULL. */
8918 static tree
8919 cp_parser_constant_expression (cp_parser* parser,
8920 bool allow_non_constant_p,
8921 bool *non_constant_p)
8923 bool saved_integral_constant_expression_p;
8924 bool saved_allow_non_integral_constant_expression_p;
8925 bool saved_non_integral_constant_expression_p;
8926 tree expression;
8928 /* It might seem that we could simply parse the
8929 conditional-expression, and then check to see if it were
8930 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8931 one that the compiler can figure out is constant, possibly after
8932 doing some simplifications or optimizations. The standard has a
8933 precise definition of constant-expression, and we must honor
8934 that, even though it is somewhat more restrictive.
8936 For example:
8938 int i[(2, 3)];
8940 is not a legal declaration, because `(2, 3)' is not a
8941 constant-expression. The `,' operator is forbidden in a
8942 constant-expression. However, GCC's constant-folding machinery
8943 will fold this operation to an INTEGER_CST for `3'. */
8945 /* Save the old settings. */
8946 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8947 saved_allow_non_integral_constant_expression_p
8948 = parser->allow_non_integral_constant_expression_p;
8949 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8950 /* We are now parsing a constant-expression. */
8951 parser->integral_constant_expression_p = true;
8952 parser->allow_non_integral_constant_expression_p
8953 = (allow_non_constant_p || cxx_dialect >= cxx11);
8954 parser->non_integral_constant_expression_p = false;
8955 /* Although the grammar says "conditional-expression", we parse an
8956 "assignment-expression", which also permits "throw-expression"
8957 and the use of assignment operators. In the case that
8958 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8959 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8960 actually essential that we look for an assignment-expression.
8961 For example, cp_parser_initializer_clauses uses this function to
8962 determine whether a particular assignment-expression is in fact
8963 constant. */
8964 expression = cp_parser_assignment_expression (parser);
8965 /* Restore the old settings. */
8966 parser->integral_constant_expression_p
8967 = saved_integral_constant_expression_p;
8968 parser->allow_non_integral_constant_expression_p
8969 = saved_allow_non_integral_constant_expression_p;
8970 if (cxx_dialect >= cxx11)
8972 /* Require an rvalue constant expression here; that's what our
8973 callers expect. Reference constant expressions are handled
8974 separately in e.g. cp_parser_template_argument. */
8975 bool is_const = potential_rvalue_constant_expression (expression);
8976 parser->non_integral_constant_expression_p = !is_const;
8977 if (!is_const && !allow_non_constant_p)
8978 require_potential_rvalue_constant_expression (expression);
8980 if (allow_non_constant_p)
8981 *non_constant_p = parser->non_integral_constant_expression_p;
8982 parser->non_integral_constant_expression_p
8983 = saved_non_integral_constant_expression_p;
8985 return expression;
8988 /* Parse __builtin_offsetof.
8990 offsetof-expression:
8991 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8993 offsetof-member-designator:
8994 id-expression
8995 | offsetof-member-designator "." id-expression
8996 | offsetof-member-designator "[" expression "]"
8997 | offsetof-member-designator "->" id-expression */
8999 static tree
9000 cp_parser_builtin_offsetof (cp_parser *parser)
9002 int save_ice_p, save_non_ice_p;
9003 tree type, expr;
9004 cp_id_kind dummy;
9005 cp_token *token;
9007 /* We're about to accept non-integral-constant things, but will
9008 definitely yield an integral constant expression. Save and
9009 restore these values around our local parsing. */
9010 save_ice_p = parser->integral_constant_expression_p;
9011 save_non_ice_p = parser->non_integral_constant_expression_p;
9013 /* Consume the "__builtin_offsetof" token. */
9014 cp_lexer_consume_token (parser->lexer);
9015 /* Consume the opening `('. */
9016 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9017 /* Parse the type-id. */
9018 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9019 type = cp_parser_type_id (parser);
9020 /* Look for the `,'. */
9021 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9022 token = cp_lexer_peek_token (parser->lexer);
9024 /* Build the (type *)null that begins the traditional offsetof macro. */
9025 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9026 tf_warning_or_error);
9028 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9029 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9030 true, &dummy, token->location);
9031 while (true)
9033 token = cp_lexer_peek_token (parser->lexer);
9034 switch (token->type)
9036 case CPP_OPEN_SQUARE:
9037 /* offsetof-member-designator "[" expression "]" */
9038 expr = cp_parser_postfix_open_square_expression (parser, expr,
9039 true, false);
9040 break;
9042 case CPP_DEREF:
9043 /* offsetof-member-designator "->" identifier */
9044 expr = grok_array_decl (token->location, expr,
9045 integer_zero_node, false);
9046 /* FALLTHRU */
9048 case CPP_DOT:
9049 /* offsetof-member-designator "." identifier */
9050 cp_lexer_consume_token (parser->lexer);
9051 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9052 expr, true, &dummy,
9053 token->location);
9054 break;
9056 case CPP_CLOSE_PAREN:
9057 /* Consume the ")" token. */
9058 cp_lexer_consume_token (parser->lexer);
9059 goto success;
9061 default:
9062 /* Error. We know the following require will fail, but
9063 that gives the proper error message. */
9064 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9065 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9066 expr = error_mark_node;
9067 goto failure;
9071 success:
9072 expr = finish_offsetof (expr, loc);
9074 failure:
9075 parser->integral_constant_expression_p = save_ice_p;
9076 parser->non_integral_constant_expression_p = save_non_ice_p;
9078 return expr;
9081 /* Parse a trait expression.
9083 Returns a representation of the expression, the underlying type
9084 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9086 static tree
9087 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9089 cp_trait_kind kind;
9090 tree type1, type2 = NULL_TREE;
9091 bool binary = false;
9092 bool variadic = false;
9094 switch (keyword)
9096 case RID_HAS_NOTHROW_ASSIGN:
9097 kind = CPTK_HAS_NOTHROW_ASSIGN;
9098 break;
9099 case RID_HAS_NOTHROW_CONSTRUCTOR:
9100 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9101 break;
9102 case RID_HAS_NOTHROW_COPY:
9103 kind = CPTK_HAS_NOTHROW_COPY;
9104 break;
9105 case RID_HAS_TRIVIAL_ASSIGN:
9106 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9107 break;
9108 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9109 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9110 break;
9111 case RID_HAS_TRIVIAL_COPY:
9112 kind = CPTK_HAS_TRIVIAL_COPY;
9113 break;
9114 case RID_HAS_TRIVIAL_DESTRUCTOR:
9115 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9116 break;
9117 case RID_HAS_VIRTUAL_DESTRUCTOR:
9118 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9119 break;
9120 case RID_IS_ABSTRACT:
9121 kind = CPTK_IS_ABSTRACT;
9122 break;
9123 case RID_IS_BASE_OF:
9124 kind = CPTK_IS_BASE_OF;
9125 binary = true;
9126 break;
9127 case RID_IS_CLASS:
9128 kind = CPTK_IS_CLASS;
9129 break;
9130 case RID_IS_EMPTY:
9131 kind = CPTK_IS_EMPTY;
9132 break;
9133 case RID_IS_ENUM:
9134 kind = CPTK_IS_ENUM;
9135 break;
9136 case RID_IS_FINAL:
9137 kind = CPTK_IS_FINAL;
9138 break;
9139 case RID_IS_LITERAL_TYPE:
9140 kind = CPTK_IS_LITERAL_TYPE;
9141 break;
9142 case RID_IS_POD:
9143 kind = CPTK_IS_POD;
9144 break;
9145 case RID_IS_POLYMORPHIC:
9146 kind = CPTK_IS_POLYMORPHIC;
9147 break;
9148 case RID_IS_SAME_AS:
9149 kind = CPTK_IS_SAME_AS;
9150 binary = true;
9151 break;
9152 case RID_IS_STD_LAYOUT:
9153 kind = CPTK_IS_STD_LAYOUT;
9154 break;
9155 case RID_IS_TRIVIAL:
9156 kind = CPTK_IS_TRIVIAL;
9157 break;
9158 case RID_IS_TRIVIALLY_ASSIGNABLE:
9159 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9160 binary = true;
9161 break;
9162 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9163 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9164 variadic = true;
9165 break;
9166 case RID_IS_TRIVIALLY_COPYABLE:
9167 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9168 break;
9169 case RID_IS_UNION:
9170 kind = CPTK_IS_UNION;
9171 break;
9172 case RID_UNDERLYING_TYPE:
9173 kind = CPTK_UNDERLYING_TYPE;
9174 break;
9175 case RID_BASES:
9176 kind = CPTK_BASES;
9177 break;
9178 case RID_DIRECT_BASES:
9179 kind = CPTK_DIRECT_BASES;
9180 break;
9181 default:
9182 gcc_unreachable ();
9185 /* Consume the token. */
9186 cp_lexer_consume_token (parser->lexer);
9188 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9190 type1 = cp_parser_type_id (parser);
9192 if (type1 == error_mark_node)
9193 return error_mark_node;
9195 if (binary)
9197 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9199 type2 = cp_parser_type_id (parser);
9201 if (type2 == error_mark_node)
9202 return error_mark_node;
9204 else if (variadic)
9206 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9208 cp_lexer_consume_token (parser->lexer);
9209 tree elt = cp_parser_type_id (parser);
9210 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9212 cp_lexer_consume_token (parser->lexer);
9213 elt = make_pack_expansion (elt);
9215 if (elt == error_mark_node)
9216 return error_mark_node;
9217 type2 = tree_cons (NULL_TREE, elt, type2);
9221 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9223 /* Complete the trait expression, which may mean either processing
9224 the trait expr now or saving it for template instantiation. */
9225 switch(kind)
9227 case CPTK_UNDERLYING_TYPE:
9228 return finish_underlying_type (type1);
9229 case CPTK_BASES:
9230 return finish_bases (type1, false);
9231 case CPTK_DIRECT_BASES:
9232 return finish_bases (type1, true);
9233 default:
9234 return finish_trait_expr (kind, type1, type2);
9238 /* Lambdas that appear in variable initializer or default argument scope
9239 get that in their mangling, so we need to record it. We might as well
9240 use the count for function and namespace scopes as well. */
9241 static GTY(()) tree lambda_scope;
9242 static GTY(()) int lambda_count;
9243 struct GTY(()) tree_int
9245 tree t;
9246 int i;
9248 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9250 static void
9251 start_lambda_scope (tree decl)
9253 tree_int ti;
9254 gcc_assert (decl);
9255 /* Once we're inside a function, we ignore other scopes and just push
9256 the function again so that popping works properly. */
9257 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9258 decl = current_function_decl;
9259 ti.t = lambda_scope;
9260 ti.i = lambda_count;
9261 vec_safe_push (lambda_scope_stack, ti);
9262 if (lambda_scope != decl)
9264 /* Don't reset the count if we're still in the same function. */
9265 lambda_scope = decl;
9266 lambda_count = 0;
9270 static void
9271 record_lambda_scope (tree lambda)
9273 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9274 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9277 static void
9278 finish_lambda_scope (void)
9280 tree_int *p = &lambda_scope_stack->last ();
9281 if (lambda_scope != p->t)
9283 lambda_scope = p->t;
9284 lambda_count = p->i;
9286 lambda_scope_stack->pop ();
9289 /* Parse a lambda expression.
9291 lambda-expression:
9292 lambda-introducer lambda-declarator [opt] compound-statement
9294 Returns a representation of the expression. */
9296 static tree
9297 cp_parser_lambda_expression (cp_parser* parser)
9299 tree lambda_expr = build_lambda_expr ();
9300 tree type;
9301 bool ok = true;
9302 cp_token *token = cp_lexer_peek_token (parser->lexer);
9303 cp_token_position start = 0;
9305 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9307 if (cp_unevaluated_operand)
9309 if (!token->error_reported)
9311 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9312 "lambda-expression in unevaluated context");
9313 token->error_reported = true;
9315 ok = false;
9317 else if (parser->in_template_argument_list_p)
9319 if (!token->error_reported)
9321 error_at (token->location, "lambda-expression in template-argument");
9322 token->error_reported = true;
9324 ok = false;
9327 /* We may be in the middle of deferred access check. Disable
9328 it now. */
9329 push_deferring_access_checks (dk_no_deferred);
9331 cp_parser_lambda_introducer (parser, lambda_expr);
9333 type = begin_lambda_type (lambda_expr);
9334 if (type == error_mark_node)
9335 return error_mark_node;
9337 record_lambda_scope (lambda_expr);
9339 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9340 determine_visibility (TYPE_NAME (type));
9342 /* Now that we've started the type, add the capture fields for any
9343 explicit captures. */
9344 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9347 /* Inside the class, surrounding template-parameter-lists do not apply. */
9348 unsigned int saved_num_template_parameter_lists
9349 = parser->num_template_parameter_lists;
9350 unsigned char in_statement = parser->in_statement;
9351 bool in_switch_statement_p = parser->in_switch_statement_p;
9352 bool fully_implicit_function_template_p
9353 = parser->fully_implicit_function_template_p;
9354 tree implicit_template_parms = parser->implicit_template_parms;
9355 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9356 bool auto_is_implicit_function_template_parm_p
9357 = parser->auto_is_implicit_function_template_parm_p;
9359 parser->num_template_parameter_lists = 0;
9360 parser->in_statement = 0;
9361 parser->in_switch_statement_p = false;
9362 parser->fully_implicit_function_template_p = false;
9363 parser->implicit_template_parms = 0;
9364 parser->implicit_template_scope = 0;
9365 parser->auto_is_implicit_function_template_parm_p = false;
9367 /* By virtue of defining a local class, a lambda expression has access to
9368 the private variables of enclosing classes. */
9370 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9372 if (ok)
9374 if (!cp_parser_error_occurred (parser)
9375 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9376 && cp_parser_start_tentative_firewall (parser))
9377 start = token;
9378 cp_parser_lambda_body (parser, lambda_expr);
9380 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9382 if (cp_parser_skip_to_closing_brace (parser))
9383 cp_lexer_consume_token (parser->lexer);
9386 /* The capture list was built up in reverse order; fix that now. */
9387 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9388 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9390 if (ok)
9391 maybe_add_lambda_conv_op (type);
9393 type = finish_struct (type, /*attributes=*/NULL_TREE);
9395 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9396 parser->in_statement = in_statement;
9397 parser->in_switch_statement_p = in_switch_statement_p;
9398 parser->fully_implicit_function_template_p
9399 = fully_implicit_function_template_p;
9400 parser->implicit_template_parms = implicit_template_parms;
9401 parser->implicit_template_scope = implicit_template_scope;
9402 parser->auto_is_implicit_function_template_parm_p
9403 = auto_is_implicit_function_template_parm_p;
9406 pop_deferring_access_checks ();
9408 /* This field is only used during parsing of the lambda. */
9409 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9411 /* This lambda shouldn't have any proxies left at this point. */
9412 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9413 /* And now that we're done, push proxies for an enclosing lambda. */
9414 insert_pending_capture_proxies ();
9416 if (ok)
9417 lambda_expr = build_lambda_object (lambda_expr);
9418 else
9419 lambda_expr = error_mark_node;
9421 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9423 return lambda_expr;
9426 /* Parse the beginning of a lambda expression.
9428 lambda-introducer:
9429 [ lambda-capture [opt] ]
9431 LAMBDA_EXPR is the current representation of the lambda expression. */
9433 static void
9434 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9436 /* Need commas after the first capture. */
9437 bool first = true;
9439 /* Eat the leading `['. */
9440 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9442 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9443 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9444 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9445 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9446 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9447 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9449 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9451 cp_lexer_consume_token (parser->lexer);
9452 first = false;
9455 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9457 cp_token* capture_token;
9458 tree capture_id;
9459 tree capture_init_expr;
9460 cp_id_kind idk = CP_ID_KIND_NONE;
9461 bool explicit_init_p = false;
9463 enum capture_kind_type
9465 BY_COPY,
9466 BY_REFERENCE
9468 enum capture_kind_type capture_kind = BY_COPY;
9470 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9472 error ("expected end of capture-list");
9473 return;
9476 if (first)
9477 first = false;
9478 else
9479 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9481 /* Possibly capture `this'. */
9482 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9484 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9485 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9486 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9487 "with by-copy capture default");
9488 cp_lexer_consume_token (parser->lexer);
9489 add_capture (lambda_expr,
9490 /*id=*/this_identifier,
9491 /*initializer=*/finish_this_expr(),
9492 /*by_reference_p=*/false,
9493 explicit_init_p);
9494 continue;
9497 /* Remember whether we want to capture as a reference or not. */
9498 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9500 capture_kind = BY_REFERENCE;
9501 cp_lexer_consume_token (parser->lexer);
9504 /* Get the identifier. */
9505 capture_token = cp_lexer_peek_token (parser->lexer);
9506 capture_id = cp_parser_identifier (parser);
9508 if (capture_id == error_mark_node)
9509 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9510 delimiters, but I modified this to stop on unnested ']' as well. It
9511 was already changed to stop on unnested '}', so the
9512 "closing_parenthesis" name is no more misleading with my change. */
9514 cp_parser_skip_to_closing_parenthesis (parser,
9515 /*recovering=*/true,
9516 /*or_comma=*/true,
9517 /*consume_paren=*/true);
9518 break;
9521 /* Find the initializer for this capture. */
9522 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9523 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9524 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9526 bool direct, non_constant;
9527 /* An explicit initializer exists. */
9528 if (cxx_dialect < cxx14)
9529 pedwarn (input_location, 0,
9530 "lambda capture initializers "
9531 "only available with -std=c++14 or -std=gnu++14");
9532 capture_init_expr = cp_parser_initializer (parser, &direct,
9533 &non_constant);
9534 explicit_init_p = true;
9535 if (capture_init_expr == NULL_TREE)
9537 error ("empty initializer for lambda init-capture");
9538 capture_init_expr = error_mark_node;
9541 else
9543 const char* error_msg;
9545 /* Turn the identifier into an id-expression. */
9546 capture_init_expr
9547 = cp_parser_lookup_name_simple (parser, capture_id,
9548 capture_token->location);
9550 if (capture_init_expr == error_mark_node)
9552 unqualified_name_lookup_error (capture_id);
9553 continue;
9555 else if (DECL_P (capture_init_expr)
9556 && (!VAR_P (capture_init_expr)
9557 && TREE_CODE (capture_init_expr) != PARM_DECL))
9559 error_at (capture_token->location,
9560 "capture of non-variable %qD ",
9561 capture_init_expr);
9562 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9563 "%q#D declared here", capture_init_expr);
9564 continue;
9566 if (VAR_P (capture_init_expr)
9567 && decl_storage_duration (capture_init_expr) != dk_auto)
9569 if (pedwarn (capture_token->location, 0, "capture of variable "
9570 "%qD with non-automatic storage duration",
9571 capture_init_expr))
9572 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9573 "%q#D declared here", capture_init_expr);
9574 continue;
9577 capture_init_expr
9578 = finish_id_expression
9579 (capture_id,
9580 capture_init_expr,
9581 parser->scope,
9582 &idk,
9583 /*integral_constant_expression_p=*/false,
9584 /*allow_non_integral_constant_expression_p=*/false,
9585 /*non_integral_constant_expression_p=*/NULL,
9586 /*template_p=*/false,
9587 /*done=*/true,
9588 /*address_p=*/false,
9589 /*template_arg_p=*/false,
9590 &error_msg,
9591 capture_token->location);
9593 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9595 cp_lexer_consume_token (parser->lexer);
9596 capture_init_expr = make_pack_expansion (capture_init_expr);
9598 else
9599 check_for_bare_parameter_packs (capture_init_expr);
9602 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9603 && !explicit_init_p)
9605 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9606 && capture_kind == BY_COPY)
9607 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9608 "of %qD redundant with by-copy capture default",
9609 capture_id);
9610 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9611 && capture_kind == BY_REFERENCE)
9612 pedwarn (capture_token->location, 0, "explicit by-reference "
9613 "capture of %qD redundant with by-reference capture "
9614 "default", capture_id);
9617 add_capture (lambda_expr,
9618 capture_id,
9619 capture_init_expr,
9620 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9621 explicit_init_p);
9624 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9627 /* Parse the (optional) middle of a lambda expression.
9629 lambda-declarator:
9630 < template-parameter-list [opt] >
9631 ( parameter-declaration-clause [opt] )
9632 attribute-specifier [opt]
9633 mutable [opt]
9634 exception-specification [opt]
9635 lambda-return-type-clause [opt]
9637 LAMBDA_EXPR is the current representation of the lambda expression. */
9639 static bool
9640 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9642 /* 5.1.1.4 of the standard says:
9643 If a lambda-expression does not include a lambda-declarator, it is as if
9644 the lambda-declarator were ().
9645 This means an empty parameter list, no attributes, and no exception
9646 specification. */
9647 tree param_list = void_list_node;
9648 tree attributes = NULL_TREE;
9649 tree exception_spec = NULL_TREE;
9650 tree template_param_list = NULL_TREE;
9651 tree tx_qual = NULL_TREE;
9653 /* The template-parameter-list is optional, but must begin with
9654 an opening angle if present. */
9655 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9657 if (cxx_dialect < cxx14)
9658 pedwarn (parser->lexer->next_token->location, 0,
9659 "lambda templates are only available with "
9660 "-std=c++14 or -std=gnu++14");
9662 cp_lexer_consume_token (parser->lexer);
9664 template_param_list = cp_parser_template_parameter_list (parser);
9666 cp_parser_skip_to_end_of_template_parameter_list (parser);
9668 /* We just processed one more parameter list. */
9669 ++parser->num_template_parameter_lists;
9672 /* The parameter-declaration-clause is optional (unless
9673 template-parameter-list was given), but must begin with an
9674 opening parenthesis if present. */
9675 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9677 cp_lexer_consume_token (parser->lexer);
9679 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9681 /* Parse parameters. */
9682 param_list = cp_parser_parameter_declaration_clause (parser);
9684 /* Default arguments shall not be specified in the
9685 parameter-declaration-clause of a lambda-declarator. */
9686 for (tree t = param_list; t; t = TREE_CHAIN (t))
9687 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9688 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9689 "default argument specified for lambda parameter");
9691 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9693 attributes = cp_parser_attributes_opt (parser);
9695 /* Parse optional `mutable' keyword. */
9696 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9698 cp_lexer_consume_token (parser->lexer);
9699 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9702 tx_qual = cp_parser_tx_qualifier_opt (parser);
9704 /* Parse optional exception specification. */
9705 exception_spec = cp_parser_exception_specification_opt (parser);
9707 /* Parse optional trailing return type. */
9708 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9710 cp_lexer_consume_token (parser->lexer);
9711 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9712 = cp_parser_trailing_type_id (parser);
9715 /* The function parameters must be in scope all the way until after the
9716 trailing-return-type in case of decltype. */
9717 pop_bindings_and_leave_scope ();
9719 else if (template_param_list != NULL_TREE) // generate diagnostic
9720 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9722 /* Create the function call operator.
9724 Messing with declarators like this is no uglier than building up the
9725 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9726 other code. */
9728 cp_decl_specifier_seq return_type_specs;
9729 cp_declarator* declarator;
9730 tree fco;
9731 int quals;
9732 void *p;
9734 clear_decl_specs (&return_type_specs);
9735 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9736 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9737 else
9738 /* Maybe we will deduce the return type later. */
9739 return_type_specs.type = make_auto ();
9741 p = obstack_alloc (&declarator_obstack, 0);
9743 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9744 sfk_none);
9746 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9747 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9748 declarator = make_call_declarator (declarator, param_list, quals,
9749 VIRT_SPEC_UNSPECIFIED,
9750 REF_QUAL_NONE,
9751 tx_qual,
9752 exception_spec,
9753 /*late_return_type=*/NULL_TREE,
9754 /*requires_clause*/NULL_TREE);
9755 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9757 fco = grokmethod (&return_type_specs,
9758 declarator,
9759 attributes);
9760 if (fco != error_mark_node)
9762 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9763 DECL_ARTIFICIAL (fco) = 1;
9764 /* Give the object parameter a different name. */
9765 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9766 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9767 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9769 if (template_param_list)
9771 fco = finish_member_template_decl (fco);
9772 finish_template_decl (template_param_list);
9773 --parser->num_template_parameter_lists;
9775 else if (parser->fully_implicit_function_template_p)
9776 fco = finish_fully_implicit_template (parser, fco);
9778 finish_member_declaration (fco);
9780 obstack_free (&declarator_obstack, p);
9782 return (fco != error_mark_node);
9786 /* Parse the body of a lambda expression, which is simply
9788 compound-statement
9790 but which requires special handling.
9791 LAMBDA_EXPR is the current representation of the lambda expression. */
9793 static void
9794 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9796 bool nested = (current_function_decl != NULL_TREE);
9797 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9798 if (nested)
9799 push_function_context ();
9800 else
9801 /* Still increment function_depth so that we don't GC in the
9802 middle of an expression. */
9803 ++function_depth;
9804 vec<tree> omp_privatization_save;
9805 save_omp_privatization_clauses (omp_privatization_save);
9806 /* Clear this in case we're in the middle of a default argument. */
9807 parser->local_variables_forbidden_p = false;
9809 /* Finish the function call operator
9810 - class_specifier
9811 + late_parsing_for_member
9812 + function_definition_after_declarator
9813 + ctor_initializer_opt_and_function_body */
9815 tree fco = lambda_function (lambda_expr);
9816 tree body;
9817 bool done = false;
9818 tree compound_stmt;
9819 tree cap;
9821 /* Let the front end know that we are going to be defining this
9822 function. */
9823 start_preparsed_function (fco,
9824 NULL_TREE,
9825 SF_PRE_PARSED | SF_INCLASS_INLINE);
9827 start_lambda_scope (fco);
9828 body = begin_function_body ();
9830 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9831 goto out;
9833 /* Push the proxies for any explicit captures. */
9834 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9835 cap = TREE_CHAIN (cap))
9836 build_capture_proxy (TREE_PURPOSE (cap));
9838 compound_stmt = begin_compound_stmt (0);
9840 /* 5.1.1.4 of the standard says:
9841 If a lambda-expression does not include a trailing-return-type, it
9842 is as if the trailing-return-type denotes the following type:
9843 * if the compound-statement is of the form
9844 { return attribute-specifier [opt] expression ; }
9845 the type of the returned expression after lvalue-to-rvalue
9846 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9847 (_conv.array_ 4.2), and function-to-pointer conversion
9848 (_conv.func_ 4.3);
9849 * otherwise, void. */
9851 /* In a lambda that has neither a lambda-return-type-clause
9852 nor a deducible form, errors should be reported for return statements
9853 in the body. Since we used void as the placeholder return type, parsing
9854 the body as usual will give such desired behavior. */
9855 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9856 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9857 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9859 tree expr = NULL_TREE;
9860 cp_id_kind idk = CP_ID_KIND_NONE;
9862 /* Parse tentatively in case there's more after the initial return
9863 statement. */
9864 cp_parser_parse_tentatively (parser);
9866 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9868 expr = cp_parser_expression (parser, &idk);
9870 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9871 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9873 if (cp_parser_parse_definitely (parser))
9875 if (!processing_template_decl)
9877 tree type = lambda_return_type (expr);
9878 apply_deduced_return_type (fco, type);
9879 if (type == error_mark_node)
9880 expr = error_mark_node;
9883 /* Will get error here if type not deduced yet. */
9884 finish_return_stmt (expr);
9886 done = true;
9890 if (!done)
9892 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9893 cp_parser_label_declaration (parser);
9894 cp_parser_statement_seq_opt (parser, NULL_TREE);
9895 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9898 finish_compound_stmt (compound_stmt);
9900 out:
9901 finish_function_body (body);
9902 finish_lambda_scope ();
9904 /* Finish the function and generate code for it if necessary. */
9905 tree fn = finish_function (/*inline*/2);
9907 /* Only expand if the call op is not a template. */
9908 if (!DECL_TEMPLATE_INFO (fco))
9909 expand_or_defer_fn (fn);
9912 restore_omp_privatization_clauses (omp_privatization_save);
9913 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9914 if (nested)
9915 pop_function_context();
9916 else
9917 --function_depth;
9920 /* Statements [gram.stmt.stmt] */
9922 /* Parse a statement.
9924 statement:
9925 labeled-statement
9926 expression-statement
9927 compound-statement
9928 selection-statement
9929 iteration-statement
9930 jump-statement
9931 declaration-statement
9932 try-block
9934 C++11:
9936 statement:
9937 labeled-statement
9938 attribute-specifier-seq (opt) expression-statement
9939 attribute-specifier-seq (opt) compound-statement
9940 attribute-specifier-seq (opt) selection-statement
9941 attribute-specifier-seq (opt) iteration-statement
9942 attribute-specifier-seq (opt) jump-statement
9943 declaration-statement
9944 attribute-specifier-seq (opt) try-block
9946 TM Extension:
9948 statement:
9949 atomic-statement
9951 IN_COMPOUND is true when the statement is nested inside a
9952 cp_parser_compound_statement; this matters for certain pragmas.
9954 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9955 is a (possibly labeled) if statement which is not enclosed in braces
9956 and has an else clause. This is used to implement -Wparentheses.
9958 CHAIN is a vector of if-else-if conditions. */
9960 static void
9961 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9962 bool in_compound, bool *if_p, vec<tree> *chain)
9964 tree statement, std_attrs = NULL_TREE;
9965 cp_token *token;
9966 location_t statement_location, attrs_location;
9968 restart:
9969 if (if_p != NULL)
9970 *if_p = false;
9971 /* There is no statement yet. */
9972 statement = NULL_TREE;
9974 saved_token_sentinel saved_tokens (parser->lexer);
9975 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9976 if (c_dialect_objc ())
9977 /* In obj-c++, seeing '[[' might be the either the beginning of
9978 c++11 attributes, or a nested objc-message-expression. So
9979 let's parse the c++11 attributes tentatively. */
9980 cp_parser_parse_tentatively (parser);
9981 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9982 if (c_dialect_objc ())
9984 if (!cp_parser_parse_definitely (parser))
9985 std_attrs = NULL_TREE;
9988 /* Peek at the next token. */
9989 token = cp_lexer_peek_token (parser->lexer);
9990 /* Remember the location of the first token in the statement. */
9991 statement_location = token->location;
9992 /* If this is a keyword, then that will often determine what kind of
9993 statement we have. */
9994 if (token->type == CPP_KEYWORD)
9996 enum rid keyword = token->keyword;
9998 switch (keyword)
10000 case RID_CASE:
10001 case RID_DEFAULT:
10002 /* Looks like a labeled-statement with a case label.
10003 Parse the label, and then use tail recursion to parse
10004 the statement. */
10005 cp_parser_label_for_labeled_statement (parser, std_attrs);
10006 goto restart;
10008 case RID_IF:
10009 case RID_SWITCH:
10010 statement = cp_parser_selection_statement (parser, if_p, chain);
10011 break;
10013 case RID_WHILE:
10014 case RID_DO:
10015 case RID_FOR:
10016 statement = cp_parser_iteration_statement (parser, false);
10017 break;
10019 case RID_CILK_FOR:
10020 if (!flag_cilkplus)
10022 error_at (cp_lexer_peek_token (parser->lexer)->location,
10023 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10024 cp_lexer_consume_token (parser->lexer);
10025 statement = error_mark_node;
10027 else
10028 statement = cp_parser_cilk_for (parser, integer_zero_node);
10029 break;
10031 case RID_BREAK:
10032 case RID_CONTINUE:
10033 case RID_RETURN:
10034 case RID_GOTO:
10035 statement = cp_parser_jump_statement (parser);
10036 break;
10038 case RID_CILK_SYNC:
10039 cp_lexer_consume_token (parser->lexer);
10040 if (flag_cilkplus)
10042 tree sync_expr = build_cilk_sync ();
10043 SET_EXPR_LOCATION (sync_expr,
10044 token->location);
10045 statement = finish_expr_stmt (sync_expr);
10047 else
10049 error_at (token->location, "-fcilkplus must be enabled to use"
10050 " %<_Cilk_sync%>");
10051 statement = error_mark_node;
10053 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10054 break;
10056 /* Objective-C++ exception-handling constructs. */
10057 case RID_AT_TRY:
10058 case RID_AT_CATCH:
10059 case RID_AT_FINALLY:
10060 case RID_AT_SYNCHRONIZED:
10061 case RID_AT_THROW:
10062 statement = cp_parser_objc_statement (parser);
10063 break;
10065 case RID_TRY:
10066 statement = cp_parser_try_block (parser);
10067 break;
10069 case RID_NAMESPACE:
10070 /* This must be a namespace alias definition. */
10071 cp_parser_declaration_statement (parser);
10072 return;
10074 case RID_TRANSACTION_ATOMIC:
10075 case RID_TRANSACTION_RELAXED:
10076 case RID_SYNCHRONIZED:
10077 case RID_ATOMIC_NOEXCEPT:
10078 case RID_ATOMIC_CANCEL:
10079 statement = cp_parser_transaction (parser, token);
10080 break;
10081 case RID_TRANSACTION_CANCEL:
10082 statement = cp_parser_transaction_cancel (parser);
10083 break;
10085 default:
10086 /* It might be a keyword like `int' that can start a
10087 declaration-statement. */
10088 break;
10091 else if (token->type == CPP_NAME)
10093 /* If the next token is a `:', then we are looking at a
10094 labeled-statement. */
10095 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10096 if (token->type == CPP_COLON)
10098 /* Looks like a labeled-statement with an ordinary label.
10099 Parse the label, and then use tail recursion to parse
10100 the statement. */
10102 cp_parser_label_for_labeled_statement (parser, std_attrs);
10103 goto restart;
10106 /* Anything that starts with a `{' must be a compound-statement. */
10107 else if (token->type == CPP_OPEN_BRACE)
10108 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10109 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10110 a statement all its own. */
10111 else if (token->type == CPP_PRAGMA)
10113 /* Only certain OpenMP pragmas are attached to statements, and thus
10114 are considered statements themselves. All others are not. In
10115 the context of a compound, accept the pragma as a "statement" and
10116 return so that we can check for a close brace. Otherwise we
10117 require a real statement and must go back and read one. */
10118 if (in_compound)
10119 cp_parser_pragma (parser, pragma_compound);
10120 else if (!cp_parser_pragma (parser, pragma_stmt))
10121 goto restart;
10122 return;
10124 else if (token->type == CPP_EOF)
10126 cp_parser_error (parser, "expected statement");
10127 return;
10130 /* Everything else must be a declaration-statement or an
10131 expression-statement. Try for the declaration-statement
10132 first, unless we are looking at a `;', in which case we know that
10133 we have an expression-statement. */
10134 if (!statement)
10136 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10138 if (std_attrs != NULL_TREE)
10140 /* Attributes should be parsed as part of the the
10141 declaration, so let's un-parse them. */
10142 saved_tokens.rollback();
10143 std_attrs = NULL_TREE;
10146 cp_parser_parse_tentatively (parser);
10147 /* Try to parse the declaration-statement. */
10148 cp_parser_declaration_statement (parser);
10149 /* If that worked, we're done. */
10150 if (cp_parser_parse_definitely (parser))
10151 return;
10153 /* Look for an expression-statement instead. */
10154 statement = cp_parser_expression_statement (parser, in_statement_expr);
10157 /* Set the line number for the statement. */
10158 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10159 SET_EXPR_LOCATION (statement, statement_location);
10161 /* Note that for now, we don't do anything with c++11 statements
10162 parsed at this level. */
10163 if (std_attrs != NULL_TREE)
10164 warning_at (attrs_location,
10165 OPT_Wattributes,
10166 "attributes at the beginning of statement are ignored");
10169 /* Parse the label for a labeled-statement, i.e.
10171 identifier :
10172 case constant-expression :
10173 default :
10175 GNU Extension:
10176 case constant-expression ... constant-expression : statement
10178 When a label is parsed without errors, the label is added to the
10179 parse tree by the finish_* functions, so this function doesn't
10180 have to return the label. */
10182 static void
10183 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10185 cp_token *token;
10186 tree label = NULL_TREE;
10187 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10189 /* The next token should be an identifier. */
10190 token = cp_lexer_peek_token (parser->lexer);
10191 if (token->type != CPP_NAME
10192 && token->type != CPP_KEYWORD)
10194 cp_parser_error (parser, "expected labeled-statement");
10195 return;
10198 parser->colon_corrects_to_scope_p = false;
10199 switch (token->keyword)
10201 case RID_CASE:
10203 tree expr, expr_hi;
10204 cp_token *ellipsis;
10206 /* Consume the `case' token. */
10207 cp_lexer_consume_token (parser->lexer);
10208 /* Parse the constant-expression. */
10209 expr = cp_parser_constant_expression (parser);
10210 if (check_for_bare_parameter_packs (expr))
10211 expr = error_mark_node;
10213 ellipsis = cp_lexer_peek_token (parser->lexer);
10214 if (ellipsis->type == CPP_ELLIPSIS)
10216 /* Consume the `...' token. */
10217 cp_lexer_consume_token (parser->lexer);
10218 expr_hi = cp_parser_constant_expression (parser);
10219 if (check_for_bare_parameter_packs (expr_hi))
10220 expr_hi = error_mark_node;
10222 /* We don't need to emit warnings here, as the common code
10223 will do this for us. */
10225 else
10226 expr_hi = NULL_TREE;
10228 if (parser->in_switch_statement_p)
10229 finish_case_label (token->location, expr, expr_hi);
10230 else
10231 error_at (token->location,
10232 "case label %qE not within a switch statement",
10233 expr);
10235 break;
10237 case RID_DEFAULT:
10238 /* Consume the `default' token. */
10239 cp_lexer_consume_token (parser->lexer);
10241 if (parser->in_switch_statement_p)
10242 finish_case_label (token->location, NULL_TREE, NULL_TREE);
10243 else
10244 error_at (token->location, "case label not within a switch statement");
10245 break;
10247 default:
10248 /* Anything else must be an ordinary label. */
10249 label = finish_label_stmt (cp_parser_identifier (parser));
10250 break;
10253 /* Require the `:' token. */
10254 cp_parser_require (parser, CPP_COLON, RT_COLON);
10256 /* An ordinary label may optionally be followed by attributes.
10257 However, this is only permitted if the attributes are then
10258 followed by a semicolon. This is because, for backward
10259 compatibility, when parsing
10260 lab: __attribute__ ((unused)) int i;
10261 we want the attribute to attach to "i", not "lab". */
10262 if (label != NULL_TREE
10263 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10265 tree attrs;
10266 cp_parser_parse_tentatively (parser);
10267 attrs = cp_parser_gnu_attributes_opt (parser);
10268 if (attrs == NULL_TREE
10269 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10270 cp_parser_abort_tentative_parse (parser);
10271 else if (!cp_parser_parse_definitely (parser))
10273 else
10274 attributes = chainon (attributes, attrs);
10277 if (attributes != NULL_TREE)
10278 cplus_decl_attributes (&label, attributes, 0);
10280 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10283 /* Parse an expression-statement.
10285 expression-statement:
10286 expression [opt] ;
10288 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10289 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10290 indicates whether this expression-statement is part of an
10291 expression statement. */
10293 static tree
10294 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10296 tree statement = NULL_TREE;
10297 cp_token *token = cp_lexer_peek_token (parser->lexer);
10299 /* If the next token is a ';', then there is no expression
10300 statement. */
10301 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10303 statement = cp_parser_expression (parser);
10304 if (statement == error_mark_node
10305 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10307 cp_parser_skip_to_end_of_block_or_statement (parser);
10308 return error_mark_node;
10312 /* Give a helpful message for "A<T>::type t;" and the like. */
10313 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10314 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10316 if (TREE_CODE (statement) == SCOPE_REF)
10317 error_at (token->location, "need %<typename%> before %qE because "
10318 "%qT is a dependent scope",
10319 statement, TREE_OPERAND (statement, 0));
10320 else if (is_overloaded_fn (statement)
10321 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10323 /* A::A a; */
10324 tree fn = get_first_fn (statement);
10325 error_at (token->location,
10326 "%<%T::%D%> names the constructor, not the type",
10327 DECL_CONTEXT (fn), DECL_NAME (fn));
10331 /* Consume the final `;'. */
10332 cp_parser_consume_semicolon_at_end_of_statement (parser);
10334 if (in_statement_expr
10335 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10336 /* This is the final expression statement of a statement
10337 expression. */
10338 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10339 else if (statement)
10340 statement = finish_expr_stmt (statement);
10342 return statement;
10345 /* Parse a compound-statement.
10347 compound-statement:
10348 { statement-seq [opt] }
10350 GNU extension:
10352 compound-statement:
10353 { label-declaration-seq [opt] statement-seq [opt] }
10355 label-declaration-seq:
10356 label-declaration
10357 label-declaration-seq label-declaration
10359 Returns a tree representing the statement. */
10361 static tree
10362 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10363 int bcs_flags, bool function_body)
10365 tree compound_stmt;
10367 /* Consume the `{'. */
10368 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10369 return error_mark_node;
10370 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10371 && !function_body && cxx_dialect < cxx14)
10372 pedwarn (input_location, OPT_Wpedantic,
10373 "compound-statement in constexpr function");
10374 /* Begin the compound-statement. */
10375 compound_stmt = begin_compound_stmt (bcs_flags);
10376 /* If the next keyword is `__label__' we have a label declaration. */
10377 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10378 cp_parser_label_declaration (parser);
10379 /* Parse an (optional) statement-seq. */
10380 cp_parser_statement_seq_opt (parser, in_statement_expr);
10381 /* Finish the compound-statement. */
10382 finish_compound_stmt (compound_stmt);
10383 /* Consume the `}'. */
10384 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10386 return compound_stmt;
10389 /* Parse an (optional) statement-seq.
10391 statement-seq:
10392 statement
10393 statement-seq [opt] statement */
10395 static void
10396 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10398 /* Scan statements until there aren't any more. */
10399 while (true)
10401 cp_token *token = cp_lexer_peek_token (parser->lexer);
10403 /* If we are looking at a `}', then we have run out of
10404 statements; the same is true if we have reached the end
10405 of file, or have stumbled upon a stray '@end'. */
10406 if (token->type == CPP_CLOSE_BRACE
10407 || token->type == CPP_EOF
10408 || token->type == CPP_PRAGMA_EOL
10409 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10410 break;
10412 /* If we are in a compound statement and find 'else' then
10413 something went wrong. */
10414 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10416 if (parser->in_statement & IN_IF_STMT)
10417 break;
10418 else
10420 token = cp_lexer_consume_token (parser->lexer);
10421 error_at (token->location, "%<else%> without a previous %<if%>");
10425 /* Parse the statement. */
10426 cp_parser_statement (parser, in_statement_expr, true, NULL);
10430 /* Parse a selection-statement.
10432 selection-statement:
10433 if ( condition ) statement
10434 if ( condition ) statement else statement
10435 switch ( condition ) statement
10437 Returns the new IF_STMT or SWITCH_STMT.
10439 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10440 is a (possibly labeled) if statement which is not enclosed in
10441 braces and has an else clause. This is used to implement
10442 -Wparentheses.
10444 CHAIN is a vector of if-else-if conditions. This is used to implement
10445 -Wduplicated-cond. */
10447 static tree
10448 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10449 vec<tree> *chain)
10451 cp_token *token;
10452 enum rid keyword;
10453 token_indent_info guard_tinfo;
10455 if (if_p != NULL)
10456 *if_p = false;
10458 /* Peek at the next token. */
10459 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10460 guard_tinfo = get_token_indent_info (token);
10462 /* See what kind of keyword it is. */
10463 keyword = token->keyword;
10464 switch (keyword)
10466 case RID_IF:
10467 case RID_SWITCH:
10469 tree statement;
10470 tree condition;
10472 /* Look for the `('. */
10473 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10475 cp_parser_skip_to_end_of_statement (parser);
10476 return error_mark_node;
10479 /* Begin the selection-statement. */
10480 if (keyword == RID_IF)
10481 statement = begin_if_stmt ();
10482 else
10483 statement = begin_switch_stmt ();
10485 /* Parse the condition. */
10486 condition = cp_parser_condition (parser);
10487 /* Look for the `)'. */
10488 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10489 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10490 /*consume_paren=*/true);
10492 if (keyword == RID_IF)
10494 bool nested_if;
10495 unsigned char in_statement;
10497 /* Add the condition. */
10498 finish_if_stmt_cond (condition, statement);
10500 if (warn_duplicated_cond)
10501 warn_duplicated_cond_add_or_warn (token->location, condition,
10502 &chain);
10504 /* Parse the then-clause. */
10505 in_statement = parser->in_statement;
10506 parser->in_statement |= IN_IF_STMT;
10507 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10508 guard_tinfo);
10509 parser->in_statement = in_statement;
10511 finish_then_clause (statement);
10513 /* If the next token is `else', parse the else-clause. */
10514 if (cp_lexer_next_token_is_keyword (parser->lexer,
10515 RID_ELSE))
10517 guard_tinfo
10518 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10519 /* Consume the `else' keyword. */
10520 cp_lexer_consume_token (parser->lexer);
10521 if (warn_duplicated_cond)
10523 if (cp_lexer_next_token_is_keyword (parser->lexer,
10524 RID_IF)
10525 && chain == NULL)
10527 /* We've got "if (COND) else if (COND2)". Start
10528 the condition chain and add COND as the first
10529 element. */
10530 chain = new vec<tree> ();
10531 if (!CONSTANT_CLASS_P (condition)
10532 && !TREE_SIDE_EFFECTS (condition))
10534 /* Wrap it in a NOP_EXPR so that we can set the
10535 location of the condition. */
10536 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
10537 condition);
10538 SET_EXPR_LOCATION (e, token->location);
10539 chain->safe_push (e);
10542 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
10543 RID_IF))
10545 /* This is if-else without subsequent if. Zap the
10546 condition chain; we would have already warned at
10547 this point. */
10548 delete chain;
10549 chain = NULL;
10552 begin_else_clause (statement);
10553 /* Parse the else-clause. */
10554 cp_parser_implicitly_scoped_statement (parser, NULL,
10555 guard_tinfo, chain);
10557 finish_else_clause (statement);
10559 /* If we are currently parsing a then-clause, then
10560 IF_P will not be NULL. We set it to true to
10561 indicate that this if statement has an else clause.
10562 This may trigger the Wparentheses warning below
10563 when we get back up to the parent if statement. */
10564 if (if_p != NULL)
10565 *if_p = true;
10567 else
10569 /* This if statement does not have an else clause. If
10570 NESTED_IF is true, then the then-clause is an if
10571 statement which does have an else clause. We warn
10572 about the potential ambiguity. */
10573 if (nested_if)
10574 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10575 "suggest explicit braces to avoid ambiguous"
10576 " %<else%>");
10577 if (warn_duplicated_cond)
10579 /* We don't need the condition chain anymore. */
10580 delete chain;
10581 chain = NULL;
10585 /* Now we're all done with the if-statement. */
10586 finish_if_stmt (statement);
10588 else
10590 bool in_switch_statement_p;
10591 unsigned char in_statement;
10593 /* Add the condition. */
10594 finish_switch_cond (condition, statement);
10596 /* Parse the body of the switch-statement. */
10597 in_switch_statement_p = parser->in_switch_statement_p;
10598 in_statement = parser->in_statement;
10599 parser->in_switch_statement_p = true;
10600 parser->in_statement |= IN_SWITCH_STMT;
10601 cp_parser_implicitly_scoped_statement (parser, NULL,
10602 guard_tinfo);
10603 parser->in_switch_statement_p = in_switch_statement_p;
10604 parser->in_statement = in_statement;
10606 /* Now we're all done with the switch-statement. */
10607 finish_switch_stmt (statement);
10610 return statement;
10612 break;
10614 default:
10615 cp_parser_error (parser, "expected selection-statement");
10616 return error_mark_node;
10620 /* Parse a condition.
10622 condition:
10623 expression
10624 type-specifier-seq declarator = initializer-clause
10625 type-specifier-seq declarator braced-init-list
10627 GNU Extension:
10629 condition:
10630 type-specifier-seq declarator asm-specification [opt]
10631 attributes [opt] = assignment-expression
10633 Returns the expression that should be tested. */
10635 static tree
10636 cp_parser_condition (cp_parser* parser)
10638 cp_decl_specifier_seq type_specifiers;
10639 const char *saved_message;
10640 int declares_class_or_enum;
10642 /* Try the declaration first. */
10643 cp_parser_parse_tentatively (parser);
10644 /* New types are not allowed in the type-specifier-seq for a
10645 condition. */
10646 saved_message = parser->type_definition_forbidden_message;
10647 parser->type_definition_forbidden_message
10648 = G_("types may not be defined in conditions");
10649 /* Parse the type-specifier-seq. */
10650 cp_parser_decl_specifier_seq (parser,
10651 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10652 &type_specifiers,
10653 &declares_class_or_enum);
10654 /* Restore the saved message. */
10655 parser->type_definition_forbidden_message = saved_message;
10656 /* If all is well, we might be looking at a declaration. */
10657 if (!cp_parser_error_occurred (parser))
10659 tree decl;
10660 tree asm_specification;
10661 tree attributes;
10662 cp_declarator *declarator;
10663 tree initializer = NULL_TREE;
10665 /* Parse the declarator. */
10666 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10667 /*ctor_dtor_or_conv_p=*/NULL,
10668 /*parenthesized_p=*/NULL,
10669 /*member_p=*/false,
10670 /*friend_p=*/false);
10671 /* Parse the attributes. */
10672 attributes = cp_parser_attributes_opt (parser);
10673 /* Parse the asm-specification. */
10674 asm_specification = cp_parser_asm_specification_opt (parser);
10675 /* If the next token is not an `=' or '{', then we might still be
10676 looking at an expression. For example:
10678 if (A(a).x)
10680 looks like a decl-specifier-seq and a declarator -- but then
10681 there is no `=', so this is an expression. */
10682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10683 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10684 cp_parser_simulate_error (parser);
10686 /* If we did see an `=' or '{', then we are looking at a declaration
10687 for sure. */
10688 if (cp_parser_parse_definitely (parser))
10690 tree pushed_scope;
10691 bool non_constant_p;
10692 bool flags = LOOKUP_ONLYCONVERTING;
10694 /* Create the declaration. */
10695 decl = start_decl (declarator, &type_specifiers,
10696 /*initialized_p=*/true,
10697 attributes, /*prefix_attributes=*/NULL_TREE,
10698 &pushed_scope);
10700 /* Parse the initializer. */
10701 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10703 initializer = cp_parser_braced_list (parser, &non_constant_p);
10704 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10705 flags = 0;
10707 else
10709 /* Consume the `='. */
10710 cp_parser_require (parser, CPP_EQ, RT_EQ);
10711 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10713 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10714 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10716 /* Process the initializer. */
10717 cp_finish_decl (decl,
10718 initializer, !non_constant_p,
10719 asm_specification,
10720 flags);
10722 if (pushed_scope)
10723 pop_scope (pushed_scope);
10725 return convert_from_reference (decl);
10728 /* If we didn't even get past the declarator successfully, we are
10729 definitely not looking at a declaration. */
10730 else
10731 cp_parser_abort_tentative_parse (parser);
10733 /* Otherwise, we are looking at an expression. */
10734 return cp_parser_expression (parser);
10737 /* Parses a for-statement or range-for-statement until the closing ')',
10738 not included. */
10740 static tree
10741 cp_parser_for (cp_parser *parser, bool ivdep)
10743 tree init, scope, decl;
10744 bool is_range_for;
10746 /* Begin the for-statement. */
10747 scope = begin_for_scope (&init);
10749 /* Parse the initialization. */
10750 is_range_for = cp_parser_for_init_statement (parser, &decl);
10752 if (is_range_for)
10753 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10754 else
10755 return cp_parser_c_for (parser, scope, init, ivdep);
10758 static tree
10759 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10761 /* Normal for loop */
10762 tree condition = NULL_TREE;
10763 tree expression = NULL_TREE;
10764 tree stmt;
10766 stmt = begin_for_stmt (scope, init);
10767 /* The for-init-statement has already been parsed in
10768 cp_parser_for_init_statement, so no work is needed here. */
10769 finish_for_init_stmt (stmt);
10771 /* If there's a condition, process it. */
10772 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10773 condition = cp_parser_condition (parser);
10774 else if (ivdep)
10776 cp_parser_error (parser, "missing loop condition in loop with "
10777 "%<GCC ivdep%> pragma");
10778 condition = error_mark_node;
10780 finish_for_cond (condition, stmt, ivdep);
10781 /* Look for the `;'. */
10782 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10784 /* If there's an expression, process it. */
10785 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10786 expression = cp_parser_expression (parser);
10787 finish_for_expr (expression, stmt);
10789 return stmt;
10792 /* Tries to parse a range-based for-statement:
10794 range-based-for:
10795 decl-specifier-seq declarator : expression
10797 The decl-specifier-seq declarator and the `:' are already parsed by
10798 cp_parser_for_init_statement. If processing_template_decl it returns a
10799 newly created RANGE_FOR_STMT; if not, it is converted to a
10800 regular FOR_STMT. */
10802 static tree
10803 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10804 bool ivdep)
10806 tree stmt, range_expr;
10808 /* Get the range declaration momentarily out of the way so that
10809 the range expression doesn't clash with it. */
10810 if (range_decl != error_mark_node)
10811 pop_binding (DECL_NAME (range_decl), range_decl);
10813 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10815 bool expr_non_constant_p;
10816 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10818 else
10819 range_expr = cp_parser_expression (parser);
10821 /* Put the range declaration back into scope. */
10822 if (range_decl != error_mark_node)
10823 push_binding (DECL_NAME (range_decl), range_decl, current_binding_level);
10825 /* If in template, STMT is converted to a normal for-statement
10826 at instantiation. If not, it is done just ahead. */
10827 if (processing_template_decl)
10829 if (check_for_bare_parameter_packs (range_expr))
10830 range_expr = error_mark_node;
10831 stmt = begin_range_for_stmt (scope, init);
10832 if (ivdep)
10833 RANGE_FOR_IVDEP (stmt) = 1;
10834 finish_range_for_decl (stmt, range_decl, range_expr);
10835 if (!type_dependent_expression_p (range_expr)
10836 /* do_auto_deduction doesn't mess with template init-lists. */
10837 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10838 do_range_for_auto_deduction (range_decl, range_expr);
10840 else
10842 stmt = begin_for_stmt (scope, init);
10843 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10845 return stmt;
10848 /* Subroutine of cp_convert_range_for: given the initializer expression,
10849 builds up the range temporary. */
10851 static tree
10852 build_range_temp (tree range_expr)
10854 tree range_type, range_temp;
10856 /* Find out the type deduced by the declaration
10857 `auto &&__range = range_expr'. */
10858 range_type = cp_build_reference_type (make_auto (), true);
10859 range_type = do_auto_deduction (range_type, range_expr,
10860 type_uses_auto (range_type));
10862 /* Create the __range variable. */
10863 range_temp = build_decl (input_location, VAR_DECL,
10864 get_identifier ("__for_range"), range_type);
10865 TREE_USED (range_temp) = 1;
10866 DECL_ARTIFICIAL (range_temp) = 1;
10868 return range_temp;
10871 /* Used by cp_parser_range_for in template context: we aren't going to
10872 do a full conversion yet, but we still need to resolve auto in the
10873 type of the for-range-declaration if present. This is basically
10874 a shortcut version of cp_convert_range_for. */
10876 static void
10877 do_range_for_auto_deduction (tree decl, tree range_expr)
10879 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10880 if (auto_node)
10882 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10883 range_temp = convert_from_reference (build_range_temp (range_expr));
10884 iter_type = (cp_parser_perform_range_for_lookup
10885 (range_temp, &begin_dummy, &end_dummy));
10886 if (iter_type)
10888 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10889 iter_type);
10890 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10891 tf_warning_or_error);
10892 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10893 iter_decl, auto_node);
10898 /* Converts a range-based for-statement into a normal
10899 for-statement, as per the definition.
10901 for (RANGE_DECL : RANGE_EXPR)
10902 BLOCK
10904 should be equivalent to:
10907 auto &&__range = RANGE_EXPR;
10908 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10909 __begin != __end;
10910 ++__begin)
10912 RANGE_DECL = *__begin;
10913 BLOCK
10917 If RANGE_EXPR is an array:
10918 BEGIN_EXPR = __range
10919 END_EXPR = __range + ARRAY_SIZE(__range)
10920 Else if RANGE_EXPR has a member 'begin' or 'end':
10921 BEGIN_EXPR = __range.begin()
10922 END_EXPR = __range.end()
10923 Else:
10924 BEGIN_EXPR = begin(__range)
10925 END_EXPR = end(__range);
10927 If __range has a member 'begin' but not 'end', or vice versa, we must
10928 still use the second alternative (it will surely fail, however).
10929 When calling begin()/end() in the third alternative we must use
10930 argument dependent lookup, but always considering 'std' as an associated
10931 namespace. */
10933 tree
10934 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10935 bool ivdep)
10937 tree begin, end;
10938 tree iter_type, begin_expr, end_expr;
10939 tree condition, expression;
10941 if (range_decl == error_mark_node || range_expr == error_mark_node)
10942 /* If an error happened previously do nothing or else a lot of
10943 unhelpful errors would be issued. */
10944 begin_expr = end_expr = iter_type = error_mark_node;
10945 else
10947 tree range_temp;
10949 if (VAR_P (range_expr)
10950 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10951 /* Can't bind a reference to an array of runtime bound. */
10952 range_temp = range_expr;
10953 else
10955 range_temp = build_range_temp (range_expr);
10956 pushdecl (range_temp);
10957 cp_finish_decl (range_temp, range_expr,
10958 /*is_constant_init*/false, NULL_TREE,
10959 LOOKUP_ONLYCONVERTING);
10960 range_temp = convert_from_reference (range_temp);
10962 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10963 &begin_expr, &end_expr);
10966 /* The new for initialization statement. */
10967 begin = build_decl (input_location, VAR_DECL,
10968 get_identifier ("__for_begin"), iter_type);
10969 TREE_USED (begin) = 1;
10970 DECL_ARTIFICIAL (begin) = 1;
10971 pushdecl (begin);
10972 cp_finish_decl (begin, begin_expr,
10973 /*is_constant_init*/false, NULL_TREE,
10974 LOOKUP_ONLYCONVERTING);
10976 end = build_decl (input_location, VAR_DECL,
10977 get_identifier ("__for_end"), iter_type);
10978 TREE_USED (end) = 1;
10979 DECL_ARTIFICIAL (end) = 1;
10980 pushdecl (end);
10981 cp_finish_decl (end, end_expr,
10982 /*is_constant_init*/false, NULL_TREE,
10983 LOOKUP_ONLYCONVERTING);
10985 finish_for_init_stmt (statement);
10987 /* The new for condition. */
10988 condition = build_x_binary_op (input_location, NE_EXPR,
10989 begin, ERROR_MARK,
10990 end, ERROR_MARK,
10991 NULL, tf_warning_or_error);
10992 finish_for_cond (condition, statement, ivdep);
10994 /* The new increment expression. */
10995 expression = finish_unary_op_expr (input_location,
10996 PREINCREMENT_EXPR, begin,
10997 tf_warning_or_error);
10998 finish_for_expr (expression, statement);
11000 /* The declaration is initialized with *__begin inside the loop body. */
11001 cp_finish_decl (range_decl,
11002 build_x_indirect_ref (input_location, begin, RO_NULL,
11003 tf_warning_or_error),
11004 /*is_constant_init*/false, NULL_TREE,
11005 LOOKUP_ONLYCONVERTING);
11007 return statement;
11010 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11011 We need to solve both at the same time because the method used
11012 depends on the existence of members begin or end.
11013 Returns the type deduced for the iterator expression. */
11015 static tree
11016 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11018 if (error_operand_p (range))
11020 *begin = *end = error_mark_node;
11021 return error_mark_node;
11024 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11026 error ("range-based %<for%> expression of type %qT "
11027 "has incomplete type", TREE_TYPE (range));
11028 *begin = *end = error_mark_node;
11029 return error_mark_node;
11031 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11033 /* If RANGE is an array, we will use pointer arithmetic. */
11034 *begin = range;
11035 *end = build_binary_op (input_location, PLUS_EXPR,
11036 range,
11037 array_type_nelts_top (TREE_TYPE (range)),
11039 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
11041 else
11043 /* If it is not an array, we must do a bit of magic. */
11044 tree id_begin, id_end;
11045 tree member_begin, member_end;
11047 *begin = *end = error_mark_node;
11049 id_begin = get_identifier ("begin");
11050 id_end = get_identifier ("end");
11051 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11052 /*protect=*/2, /*want_type=*/false,
11053 tf_warning_or_error);
11054 member_end = lookup_member (TREE_TYPE (range), id_end,
11055 /*protect=*/2, /*want_type=*/false,
11056 tf_warning_or_error);
11058 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11060 /* Use the member functions. */
11061 if (member_begin != NULL_TREE)
11062 *begin = cp_parser_range_for_member_function (range, id_begin);
11063 else
11064 error ("range-based %<for%> expression of type %qT has an "
11065 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11067 if (member_end != NULL_TREE)
11068 *end = cp_parser_range_for_member_function (range, id_end);
11069 else
11070 error ("range-based %<for%> expression of type %qT has a "
11071 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11073 else
11075 /* Use global functions with ADL. */
11076 vec<tree, va_gc> *vec;
11077 vec = make_tree_vector ();
11079 vec_safe_push (vec, range);
11081 member_begin = perform_koenig_lookup (id_begin, vec,
11082 tf_warning_or_error);
11083 *begin = finish_call_expr (member_begin, &vec, false, true,
11084 tf_warning_or_error);
11085 member_end = perform_koenig_lookup (id_end, vec,
11086 tf_warning_or_error);
11087 *end = finish_call_expr (member_end, &vec, false, true,
11088 tf_warning_or_error);
11090 release_tree_vector (vec);
11093 /* Last common checks. */
11094 if (*begin == error_mark_node || *end == error_mark_node)
11096 /* If one of the expressions is an error do no more checks. */
11097 *begin = *end = error_mark_node;
11098 return error_mark_node;
11100 else if (type_dependent_expression_p (*begin)
11101 || type_dependent_expression_p (*end))
11102 /* Can happen, when, eg, in a template context, Koenig lookup
11103 can't resolve begin/end (c++/58503). */
11104 return NULL_TREE;
11105 else
11107 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11108 /* The unqualified type of the __begin and __end temporaries should
11109 be the same, as required by the multiple auto declaration. */
11110 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11111 error ("inconsistent begin/end types in range-based %<for%> "
11112 "statement: %qT and %qT",
11113 TREE_TYPE (*begin), TREE_TYPE (*end));
11114 return iter_type;
11119 /* Helper function for cp_parser_perform_range_for_lookup.
11120 Builds a tree for RANGE.IDENTIFIER(). */
11122 static tree
11123 cp_parser_range_for_member_function (tree range, tree identifier)
11125 tree member, res;
11126 vec<tree, va_gc> *vec;
11128 member = finish_class_member_access_expr (range, identifier,
11129 false, tf_warning_or_error);
11130 if (member == error_mark_node)
11131 return error_mark_node;
11133 vec = make_tree_vector ();
11134 res = finish_call_expr (member, &vec,
11135 /*disallow_virtual=*/false,
11136 /*koenig_p=*/false,
11137 tf_warning_or_error);
11138 release_tree_vector (vec);
11139 return res;
11142 /* Parse an iteration-statement.
11144 iteration-statement:
11145 while ( condition ) statement
11146 do statement while ( expression ) ;
11147 for ( for-init-statement condition [opt] ; expression [opt] )
11148 statement
11150 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11152 static tree
11153 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
11155 cp_token *token;
11156 enum rid keyword;
11157 tree statement;
11158 unsigned char in_statement;
11159 token_indent_info guard_tinfo;
11161 /* Peek at the next token. */
11162 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11163 if (!token)
11164 return error_mark_node;
11166 guard_tinfo = get_token_indent_info (token);
11168 /* Remember whether or not we are already within an iteration
11169 statement. */
11170 in_statement = parser->in_statement;
11172 /* See what kind of keyword it is. */
11173 keyword = token->keyword;
11174 switch (keyword)
11176 case RID_WHILE:
11178 tree condition;
11180 /* Begin the while-statement. */
11181 statement = begin_while_stmt ();
11182 /* Look for the `('. */
11183 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11184 /* Parse the condition. */
11185 condition = cp_parser_condition (parser);
11186 finish_while_stmt_cond (condition, statement, ivdep);
11187 /* Look for the `)'. */
11188 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11189 /* Parse the dependent statement. */
11190 parser->in_statement = IN_ITERATION_STMT;
11191 cp_parser_already_scoped_statement (parser, guard_tinfo);
11192 parser->in_statement = in_statement;
11193 /* We're done with the while-statement. */
11194 finish_while_stmt (statement);
11196 break;
11198 case RID_DO:
11200 tree expression;
11202 /* Begin the do-statement. */
11203 statement = begin_do_stmt ();
11204 /* Parse the body of the do-statement. */
11205 parser->in_statement = IN_ITERATION_STMT;
11206 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11207 parser->in_statement = in_statement;
11208 finish_do_body (statement);
11209 /* Look for the `while' keyword. */
11210 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11211 /* Look for the `('. */
11212 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11213 /* Parse the expression. */
11214 expression = cp_parser_expression (parser);
11215 /* We're done with the do-statement. */
11216 finish_do_stmt (expression, statement, ivdep);
11217 /* Look for the `)'. */
11218 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11219 /* Look for the `;'. */
11220 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11222 break;
11224 case RID_FOR:
11226 /* Look for the `('. */
11227 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11229 statement = cp_parser_for (parser, ivdep);
11231 /* Look for the `)'. */
11232 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11234 /* Parse the body of the for-statement. */
11235 parser->in_statement = IN_ITERATION_STMT;
11236 cp_parser_already_scoped_statement (parser, guard_tinfo);
11237 parser->in_statement = in_statement;
11239 /* We're done with the for-statement. */
11240 finish_for_stmt (statement);
11242 break;
11244 default:
11245 cp_parser_error (parser, "expected iteration-statement");
11246 statement = error_mark_node;
11247 break;
11250 return statement;
11253 /* Parse a for-init-statement or the declarator of a range-based-for.
11254 Returns true if a range-based-for declaration is seen.
11256 for-init-statement:
11257 expression-statement
11258 simple-declaration */
11260 static bool
11261 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11263 /* If the next token is a `;', then we have an empty
11264 expression-statement. Grammatically, this is also a
11265 simple-declaration, but an invalid one, because it does not
11266 declare anything. Therefore, if we did not handle this case
11267 specially, we would issue an error message about an invalid
11268 declaration. */
11269 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11271 bool is_range_for = false;
11272 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11274 /* A colon is used in range-based for. */
11275 parser->colon_corrects_to_scope_p = false;
11277 /* We're going to speculatively look for a declaration, falling back
11278 to an expression, if necessary. */
11279 cp_parser_parse_tentatively (parser);
11280 /* Parse the declaration. */
11281 cp_parser_simple_declaration (parser,
11282 /*function_definition_allowed_p=*/false,
11283 decl);
11284 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11285 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11287 /* It is a range-for, consume the ':' */
11288 cp_lexer_consume_token (parser->lexer);
11289 is_range_for = true;
11290 if (cxx_dialect < cxx11)
11292 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11293 "range-based %<for%> loops only available with "
11294 "-std=c++11 or -std=gnu++11");
11295 *decl = error_mark_node;
11298 else
11299 /* The ';' is not consumed yet because we told
11300 cp_parser_simple_declaration not to. */
11301 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11303 if (cp_parser_parse_definitely (parser))
11304 return is_range_for;
11305 /* If the tentative parse failed, then we shall need to look for an
11306 expression-statement. */
11308 /* If we are here, it is an expression-statement. */
11309 cp_parser_expression_statement (parser, NULL_TREE);
11310 return false;
11313 /* Parse a jump-statement.
11315 jump-statement:
11316 break ;
11317 continue ;
11318 return expression [opt] ;
11319 return braced-init-list ;
11320 goto identifier ;
11322 GNU extension:
11324 jump-statement:
11325 goto * expression ;
11327 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11329 static tree
11330 cp_parser_jump_statement (cp_parser* parser)
11332 tree statement = error_mark_node;
11333 cp_token *token;
11334 enum rid keyword;
11335 unsigned char in_statement;
11337 /* Peek at the next token. */
11338 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11339 if (!token)
11340 return error_mark_node;
11342 /* See what kind of keyword it is. */
11343 keyword = token->keyword;
11344 switch (keyword)
11346 case RID_BREAK:
11347 in_statement = parser->in_statement & ~IN_IF_STMT;
11348 switch (in_statement)
11350 case 0:
11351 error_at (token->location, "break statement not within loop or switch");
11352 break;
11353 default:
11354 gcc_assert ((in_statement & IN_SWITCH_STMT)
11355 || in_statement == IN_ITERATION_STMT);
11356 statement = finish_break_stmt ();
11357 if (in_statement == IN_ITERATION_STMT)
11358 break_maybe_infinite_loop ();
11359 break;
11360 case IN_OMP_BLOCK:
11361 error_at (token->location, "invalid exit from OpenMP structured block");
11362 break;
11363 case IN_OMP_FOR:
11364 error_at (token->location, "break statement used with OpenMP for loop");
11365 break;
11366 case IN_CILK_SIMD_FOR:
11367 error_at (token->location, "break statement used with Cilk Plus for loop");
11368 break;
11370 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11371 break;
11373 case RID_CONTINUE:
11374 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11376 case 0:
11377 error_at (token->location, "continue statement not within a loop");
11378 break;
11379 case IN_CILK_SIMD_FOR:
11380 error_at (token->location,
11381 "continue statement within %<#pragma simd%> loop body");
11382 /* Fall through. */
11383 case IN_ITERATION_STMT:
11384 case IN_OMP_FOR:
11385 statement = finish_continue_stmt ();
11386 break;
11387 case IN_OMP_BLOCK:
11388 error_at (token->location, "invalid exit from OpenMP structured block");
11389 break;
11390 default:
11391 gcc_unreachable ();
11393 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11394 break;
11396 case RID_RETURN:
11398 tree expr;
11399 bool expr_non_constant_p;
11401 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11403 cp_lexer_set_source_position (parser->lexer);
11404 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11405 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11407 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11408 expr = cp_parser_expression (parser);
11409 else
11410 /* If the next token is a `;', then there is no
11411 expression. */
11412 expr = NULL_TREE;
11413 /* Build the return-statement. */
11414 statement = finish_return_stmt (expr);
11415 /* Look for the final `;'. */
11416 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11418 break;
11420 case RID_GOTO:
11421 if (parser->in_function_body
11422 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11424 error ("%<goto%> in %<constexpr%> function");
11425 cp_function_chain->invalid_constexpr = true;
11428 /* Create the goto-statement. */
11429 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11431 /* Issue a warning about this use of a GNU extension. */
11432 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11433 /* Consume the '*' token. */
11434 cp_lexer_consume_token (parser->lexer);
11435 /* Parse the dependent expression. */
11436 finish_goto_stmt (cp_parser_expression (parser));
11438 else
11439 finish_goto_stmt (cp_parser_identifier (parser));
11440 /* Look for the final `;'. */
11441 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11442 break;
11444 default:
11445 cp_parser_error (parser, "expected jump-statement");
11446 break;
11449 return statement;
11452 /* Parse a declaration-statement.
11454 declaration-statement:
11455 block-declaration */
11457 static void
11458 cp_parser_declaration_statement (cp_parser* parser)
11460 void *p;
11462 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11463 p = obstack_alloc (&declarator_obstack, 0);
11465 /* Parse the block-declaration. */
11466 cp_parser_block_declaration (parser, /*statement_p=*/true);
11468 /* Free any declarators allocated. */
11469 obstack_free (&declarator_obstack, p);
11472 /* Some dependent statements (like `if (cond) statement'), are
11473 implicitly in their own scope. In other words, if the statement is
11474 a single statement (as opposed to a compound-statement), it is
11475 none-the-less treated as if it were enclosed in braces. Any
11476 declarations appearing in the dependent statement are out of scope
11477 after control passes that point. This function parses a statement,
11478 but ensures that is in its own scope, even if it is not a
11479 compound-statement.
11481 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11482 is a (possibly labeled) if statement which is not enclosed in
11483 braces and has an else clause. This is used to implement
11484 -Wparentheses.
11486 CHAIN is a vector of if-else-if conditions. This is used to implement
11487 -Wduplicated-cond.
11489 Returns the new statement. */
11491 static tree
11492 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11493 const token_indent_info &guard_tinfo,
11494 vec<tree> *chain)
11496 tree statement;
11497 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11498 token_indent_info body_tinfo
11499 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11501 if (if_p != NULL)
11502 *if_p = false;
11504 /* Mark if () ; with a special NOP_EXPR. */
11505 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11507 cp_lexer_consume_token (parser->lexer);
11508 statement = add_stmt (build_empty_stmt (body_loc));
11510 if (guard_tinfo.keyword == RID_IF
11511 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11512 warning_at (body_loc, OPT_Wempty_body,
11513 "suggest braces around empty body in an %<if%> statement");
11514 else if (guard_tinfo.keyword == RID_ELSE)
11515 warning_at (body_loc, OPT_Wempty_body,
11516 "suggest braces around empty body in an %<else%> statement");
11518 /* if a compound is opened, we simply parse the statement directly. */
11519 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11520 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11521 /* If the token is not a `{', then we must take special action. */
11522 else
11524 /* Create a compound-statement. */
11525 statement = begin_compound_stmt (0);
11526 /* Parse the dependent-statement. */
11527 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
11528 /* Finish the dummy compound-statement. */
11529 finish_compound_stmt (statement);
11532 token_indent_info next_tinfo
11533 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11534 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11536 /* Return the statement. */
11537 return statement;
11540 /* For some dependent statements (like `while (cond) statement'), we
11541 have already created a scope. Therefore, even if the dependent
11542 statement is a compound-statement, we do not want to create another
11543 scope. */
11545 static void
11546 cp_parser_already_scoped_statement (cp_parser* parser,
11547 const token_indent_info &guard_tinfo)
11549 /* If the token is a `{', then we must take special action. */
11550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11552 token_indent_info body_tinfo
11553 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11555 cp_parser_statement (parser, NULL_TREE, false, NULL);
11556 token_indent_info next_tinfo
11557 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11558 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11560 else
11562 /* Avoid calling cp_parser_compound_statement, so that we
11563 don't create a new scope. Do everything else by hand. */
11564 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11565 /* If the next keyword is `__label__' we have a label declaration. */
11566 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11567 cp_parser_label_declaration (parser);
11568 /* Parse an (optional) statement-seq. */
11569 cp_parser_statement_seq_opt (parser, NULL_TREE);
11570 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11574 /* Declarations [gram.dcl.dcl] */
11576 /* Parse an optional declaration-sequence.
11578 declaration-seq:
11579 declaration
11580 declaration-seq declaration */
11582 static void
11583 cp_parser_declaration_seq_opt (cp_parser* parser)
11585 while (true)
11587 cp_token *token;
11589 token = cp_lexer_peek_token (parser->lexer);
11591 if (token->type == CPP_CLOSE_BRACE
11592 || token->type == CPP_EOF
11593 || token->type == CPP_PRAGMA_EOL)
11594 break;
11596 if (token->type == CPP_SEMICOLON)
11598 /* A declaration consisting of a single semicolon is
11599 invalid. Allow it unless we're being pedantic. */
11600 cp_lexer_consume_token (parser->lexer);
11601 if (!in_system_header_at (input_location))
11602 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11603 continue;
11606 /* If we're entering or exiting a region that's implicitly
11607 extern "C", modify the lang context appropriately. */
11608 if (!parser->implicit_extern_c && token->implicit_extern_c)
11610 push_lang_context (lang_name_c);
11611 parser->implicit_extern_c = true;
11613 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11615 pop_lang_context ();
11616 parser->implicit_extern_c = false;
11619 if (token->type == CPP_PRAGMA)
11621 /* A top-level declaration can consist solely of a #pragma.
11622 A nested declaration cannot, so this is done here and not
11623 in cp_parser_declaration. (A #pragma at block scope is
11624 handled in cp_parser_statement.) */
11625 cp_parser_pragma (parser, pragma_external);
11626 continue;
11629 /* Parse the declaration itself. */
11630 cp_parser_declaration (parser);
11634 /* Parse a declaration.
11636 declaration:
11637 block-declaration
11638 function-definition
11639 template-declaration
11640 explicit-instantiation
11641 explicit-specialization
11642 linkage-specification
11643 namespace-definition
11645 GNU extension:
11647 declaration:
11648 __extension__ declaration */
11650 static void
11651 cp_parser_declaration (cp_parser* parser)
11653 cp_token token1;
11654 cp_token token2;
11655 int saved_pedantic;
11656 void *p;
11657 tree attributes = NULL_TREE;
11659 /* Check for the `__extension__' keyword. */
11660 if (cp_parser_extension_opt (parser, &saved_pedantic))
11662 /* Parse the qualified declaration. */
11663 cp_parser_declaration (parser);
11664 /* Restore the PEDANTIC flag. */
11665 pedantic = saved_pedantic;
11667 return;
11670 /* Try to figure out what kind of declaration is present. */
11671 token1 = *cp_lexer_peek_token (parser->lexer);
11673 if (token1.type != CPP_EOF)
11674 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11675 else
11677 token2.type = CPP_EOF;
11678 token2.keyword = RID_MAX;
11681 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11682 p = obstack_alloc (&declarator_obstack, 0);
11684 /* If the next token is `extern' and the following token is a string
11685 literal, then we have a linkage specification. */
11686 if (token1.keyword == RID_EXTERN
11687 && cp_parser_is_pure_string_literal (&token2))
11688 cp_parser_linkage_specification (parser);
11689 /* If the next token is `template', then we have either a template
11690 declaration, an explicit instantiation, or an explicit
11691 specialization. */
11692 else if (token1.keyword == RID_TEMPLATE)
11694 /* `template <>' indicates a template specialization. */
11695 if (token2.type == CPP_LESS
11696 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11697 cp_parser_explicit_specialization (parser);
11698 /* `template <' indicates a template declaration. */
11699 else if (token2.type == CPP_LESS)
11700 cp_parser_template_declaration (parser, /*member_p=*/false);
11701 /* Anything else must be an explicit instantiation. */
11702 else
11703 cp_parser_explicit_instantiation (parser);
11705 /* If the next token is `export', then we have a template
11706 declaration. */
11707 else if (token1.keyword == RID_EXPORT)
11708 cp_parser_template_declaration (parser, /*member_p=*/false);
11709 /* If the next token is `extern', 'static' or 'inline' and the one
11710 after that is `template', we have a GNU extended explicit
11711 instantiation directive. */
11712 else if (cp_parser_allow_gnu_extensions_p (parser)
11713 && (token1.keyword == RID_EXTERN
11714 || token1.keyword == RID_STATIC
11715 || token1.keyword == RID_INLINE)
11716 && token2.keyword == RID_TEMPLATE)
11717 cp_parser_explicit_instantiation (parser);
11718 /* If the next token is `namespace', check for a named or unnamed
11719 namespace definition. */
11720 else if (token1.keyword == RID_NAMESPACE
11721 && (/* A named namespace definition. */
11722 (token2.type == CPP_NAME
11723 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11724 != CPP_EQ))
11725 || (token2.type == CPP_OPEN_SQUARE
11726 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
11727 == CPP_OPEN_SQUARE)
11728 /* An unnamed namespace definition. */
11729 || token2.type == CPP_OPEN_BRACE
11730 || token2.keyword == RID_ATTRIBUTE))
11731 cp_parser_namespace_definition (parser);
11732 /* An inline (associated) namespace definition. */
11733 else if (token1.keyword == RID_INLINE
11734 && token2.keyword == RID_NAMESPACE)
11735 cp_parser_namespace_definition (parser);
11736 /* Objective-C++ declaration/definition. */
11737 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11738 cp_parser_objc_declaration (parser, NULL_TREE);
11739 else if (c_dialect_objc ()
11740 && token1.keyword == RID_ATTRIBUTE
11741 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11742 cp_parser_objc_declaration (parser, attributes);
11743 /* At this point we may have a template declared by a concept
11744 introduction. */
11745 else if (flag_concepts
11746 && cp_parser_template_declaration_after_export (parser,
11747 /*member_p=*/false))
11748 /* We did. */;
11749 else
11750 /* Try to parse a block-declaration, or a function-definition. */
11751 cp_parser_block_declaration (parser, /*statement_p=*/false);
11753 /* Free any declarators allocated. */
11754 obstack_free (&declarator_obstack, p);
11757 /* Parse a block-declaration.
11759 block-declaration:
11760 simple-declaration
11761 asm-definition
11762 namespace-alias-definition
11763 using-declaration
11764 using-directive
11766 GNU Extension:
11768 block-declaration:
11769 __extension__ block-declaration
11771 C++0x Extension:
11773 block-declaration:
11774 static_assert-declaration
11776 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11777 part of a declaration-statement. */
11779 static void
11780 cp_parser_block_declaration (cp_parser *parser,
11781 bool statement_p)
11783 cp_token *token1;
11784 int saved_pedantic;
11786 /* Check for the `__extension__' keyword. */
11787 if (cp_parser_extension_opt (parser, &saved_pedantic))
11789 /* Parse the qualified declaration. */
11790 cp_parser_block_declaration (parser, statement_p);
11791 /* Restore the PEDANTIC flag. */
11792 pedantic = saved_pedantic;
11794 return;
11797 /* Peek at the next token to figure out which kind of declaration is
11798 present. */
11799 token1 = cp_lexer_peek_token (parser->lexer);
11801 /* If the next keyword is `asm', we have an asm-definition. */
11802 if (token1->keyword == RID_ASM)
11804 if (statement_p)
11805 cp_parser_commit_to_tentative_parse (parser);
11806 cp_parser_asm_definition (parser);
11808 /* If the next keyword is `namespace', we have a
11809 namespace-alias-definition. */
11810 else if (token1->keyword == RID_NAMESPACE)
11811 cp_parser_namespace_alias_definition (parser);
11812 /* If the next keyword is `using', we have a
11813 using-declaration, a using-directive, or an alias-declaration. */
11814 else if (token1->keyword == RID_USING)
11816 cp_token *token2;
11818 if (statement_p)
11819 cp_parser_commit_to_tentative_parse (parser);
11820 /* If the token after `using' is `namespace', then we have a
11821 using-directive. */
11822 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11823 if (token2->keyword == RID_NAMESPACE)
11824 cp_parser_using_directive (parser);
11825 /* If the second token after 'using' is '=', then we have an
11826 alias-declaration. */
11827 else if (cxx_dialect >= cxx11
11828 && token2->type == CPP_NAME
11829 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11830 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11831 cp_parser_alias_declaration (parser);
11832 /* Otherwise, it's a using-declaration. */
11833 else
11834 cp_parser_using_declaration (parser,
11835 /*access_declaration_p=*/false);
11837 /* If the next keyword is `__label__' we have a misplaced label
11838 declaration. */
11839 else if (token1->keyword == RID_LABEL)
11841 cp_lexer_consume_token (parser->lexer);
11842 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11843 cp_parser_skip_to_end_of_statement (parser);
11844 /* If the next token is now a `;', consume it. */
11845 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11846 cp_lexer_consume_token (parser->lexer);
11848 /* If the next token is `static_assert' we have a static assertion. */
11849 else if (token1->keyword == RID_STATIC_ASSERT)
11850 cp_parser_static_assert (parser, /*member_p=*/false);
11851 /* Anything else must be a simple-declaration. */
11852 else
11853 cp_parser_simple_declaration (parser, !statement_p,
11854 /*maybe_range_for_decl*/NULL);
11857 /* Parse a simple-declaration.
11859 simple-declaration:
11860 decl-specifier-seq [opt] init-declarator-list [opt] ;
11862 init-declarator-list:
11863 init-declarator
11864 init-declarator-list , init-declarator
11866 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11867 function-definition as a simple-declaration.
11869 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11870 parsed declaration if it is an uninitialized single declarator not followed
11871 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11872 if present, will not be consumed. */
11874 static void
11875 cp_parser_simple_declaration (cp_parser* parser,
11876 bool function_definition_allowed_p,
11877 tree *maybe_range_for_decl)
11879 cp_decl_specifier_seq decl_specifiers;
11880 int declares_class_or_enum;
11881 bool saw_declarator;
11882 location_t comma_loc = UNKNOWN_LOCATION;
11883 location_t init_loc = UNKNOWN_LOCATION;
11885 if (maybe_range_for_decl)
11886 *maybe_range_for_decl = NULL_TREE;
11888 /* Defer access checks until we know what is being declared; the
11889 checks for names appearing in the decl-specifier-seq should be
11890 done as if we were in the scope of the thing being declared. */
11891 push_deferring_access_checks (dk_deferred);
11893 /* Parse the decl-specifier-seq. We have to keep track of whether
11894 or not the decl-specifier-seq declares a named class or
11895 enumeration type, since that is the only case in which the
11896 init-declarator-list is allowed to be empty.
11898 [dcl.dcl]
11900 In a simple-declaration, the optional init-declarator-list can be
11901 omitted only when declaring a class or enumeration, that is when
11902 the decl-specifier-seq contains either a class-specifier, an
11903 elaborated-type-specifier, or an enum-specifier. */
11904 cp_parser_decl_specifier_seq (parser,
11905 CP_PARSER_FLAGS_OPTIONAL,
11906 &decl_specifiers,
11907 &declares_class_or_enum);
11908 /* We no longer need to defer access checks. */
11909 stop_deferring_access_checks ();
11911 /* In a block scope, a valid declaration must always have a
11912 decl-specifier-seq. By not trying to parse declarators, we can
11913 resolve the declaration/expression ambiguity more quickly. */
11914 if (!function_definition_allowed_p
11915 && !decl_specifiers.any_specifiers_p)
11917 cp_parser_error (parser, "expected declaration");
11918 goto done;
11921 /* If the next two tokens are both identifiers, the code is
11922 erroneous. The usual cause of this situation is code like:
11924 T t;
11926 where "T" should name a type -- but does not. */
11927 if (!decl_specifiers.any_type_specifiers_p
11928 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11930 /* If parsing tentatively, we should commit; we really are
11931 looking at a declaration. */
11932 cp_parser_commit_to_tentative_parse (parser);
11933 /* Give up. */
11934 goto done;
11937 /* If we have seen at least one decl-specifier, and the next token
11938 is not a parenthesis, then we must be looking at a declaration.
11939 (After "int (" we might be looking at a functional cast.) */
11940 if (decl_specifiers.any_specifiers_p
11941 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11942 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11943 && !cp_parser_error_occurred (parser))
11944 cp_parser_commit_to_tentative_parse (parser);
11946 /* Keep going until we hit the `;' at the end of the simple
11947 declaration. */
11948 saw_declarator = false;
11949 while (cp_lexer_next_token_is_not (parser->lexer,
11950 CPP_SEMICOLON))
11952 cp_token *token;
11953 bool function_definition_p;
11954 tree decl;
11956 if (saw_declarator)
11958 /* If we are processing next declarator, comma is expected */
11959 token = cp_lexer_peek_token (parser->lexer);
11960 gcc_assert (token->type == CPP_COMMA);
11961 cp_lexer_consume_token (parser->lexer);
11962 if (maybe_range_for_decl)
11964 *maybe_range_for_decl = error_mark_node;
11965 if (comma_loc == UNKNOWN_LOCATION)
11966 comma_loc = token->location;
11969 else
11970 saw_declarator = true;
11972 /* Parse the init-declarator. */
11973 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11974 /*checks=*/NULL,
11975 function_definition_allowed_p,
11976 /*member_p=*/false,
11977 declares_class_or_enum,
11978 &function_definition_p,
11979 maybe_range_for_decl,
11980 &init_loc);
11981 /* If an error occurred while parsing tentatively, exit quickly.
11982 (That usually happens when in the body of a function; each
11983 statement is treated as a declaration-statement until proven
11984 otherwise.) */
11985 if (cp_parser_error_occurred (parser))
11986 goto done;
11987 /* Handle function definitions specially. */
11988 if (function_definition_p)
11990 /* If the next token is a `,', then we are probably
11991 processing something like:
11993 void f() {}, *p;
11995 which is erroneous. */
11996 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11998 cp_token *token = cp_lexer_peek_token (parser->lexer);
11999 error_at (token->location,
12000 "mixing"
12001 " declarations and function-definitions is forbidden");
12003 /* Otherwise, we're done with the list of declarators. */
12004 else
12006 pop_deferring_access_checks ();
12007 return;
12010 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12011 *maybe_range_for_decl = decl;
12012 /* The next token should be either a `,' or a `;'. */
12013 token = cp_lexer_peek_token (parser->lexer);
12014 /* If it's a `,', there are more declarators to come. */
12015 if (token->type == CPP_COMMA)
12016 /* will be consumed next time around */;
12017 /* If it's a `;', we are done. */
12018 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12019 break;
12020 /* Anything else is an error. */
12021 else
12023 /* If we have already issued an error message we don't need
12024 to issue another one. */
12025 if ((decl != error_mark_node
12026 && DECL_INITIAL (decl) != error_mark_node)
12027 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12028 cp_parser_error (parser, "expected %<,%> or %<;%>");
12029 /* Skip tokens until we reach the end of the statement. */
12030 cp_parser_skip_to_end_of_statement (parser);
12031 /* If the next token is now a `;', consume it. */
12032 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12033 cp_lexer_consume_token (parser->lexer);
12034 goto done;
12036 /* After the first time around, a function-definition is not
12037 allowed -- even if it was OK at first. For example:
12039 int i, f() {}
12041 is not valid. */
12042 function_definition_allowed_p = false;
12045 /* Issue an error message if no declarators are present, and the
12046 decl-specifier-seq does not itself declare a class or
12047 enumeration: [dcl.dcl]/3. */
12048 if (!saw_declarator)
12050 if (cp_parser_declares_only_class_p (parser))
12052 if (!declares_class_or_enum
12053 && decl_specifiers.type
12054 && OVERLOAD_TYPE_P (decl_specifiers.type))
12055 /* Ensure an error is issued anyway when finish_decltype_type,
12056 called via cp_parser_decl_specifier_seq, returns a class or
12057 an enumeration (c++/51786). */
12058 decl_specifiers.type = NULL_TREE;
12059 shadow_tag (&decl_specifiers);
12061 /* Perform any deferred access checks. */
12062 perform_deferred_access_checks (tf_warning_or_error);
12065 /* Consume the `;'. */
12066 if (!maybe_range_for_decl)
12067 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12068 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12070 if (init_loc != UNKNOWN_LOCATION)
12071 error_at (init_loc, "initializer in range-based %<for%> loop");
12072 if (comma_loc != UNKNOWN_LOCATION)
12073 error_at (comma_loc,
12074 "multiple declarations in range-based %<for%> loop");
12077 done:
12078 pop_deferring_access_checks ();
12081 /* Parse a decl-specifier-seq.
12083 decl-specifier-seq:
12084 decl-specifier-seq [opt] decl-specifier
12085 decl-specifier attribute-specifier-seq [opt] (C++11)
12087 decl-specifier:
12088 storage-class-specifier
12089 type-specifier
12090 function-specifier
12091 friend
12092 typedef
12094 GNU Extension:
12096 decl-specifier:
12097 attributes
12099 Concepts Extension:
12101 decl-specifier:
12102 concept
12104 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12106 The parser flags FLAGS is used to control type-specifier parsing.
12108 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12109 flags:
12111 1: one of the decl-specifiers is an elaborated-type-specifier
12112 (i.e., a type declaration)
12113 2: one of the decl-specifiers is an enum-specifier or a
12114 class-specifier (i.e., a type definition)
12118 static void
12119 cp_parser_decl_specifier_seq (cp_parser* parser,
12120 cp_parser_flags flags,
12121 cp_decl_specifier_seq *decl_specs,
12122 int* declares_class_or_enum)
12124 bool constructor_possible_p = !parser->in_declarator_p;
12125 bool found_decl_spec = false;
12126 cp_token *start_token = NULL;
12127 cp_decl_spec ds;
12129 /* Clear DECL_SPECS. */
12130 clear_decl_specs (decl_specs);
12132 /* Assume no class or enumeration type is declared. */
12133 *declares_class_or_enum = 0;
12135 /* Keep reading specifiers until there are no more to read. */
12136 while (true)
12138 bool constructor_p;
12139 cp_token *token;
12140 ds = ds_last;
12142 /* Peek at the next token. */
12143 token = cp_lexer_peek_token (parser->lexer);
12145 /* Save the first token of the decl spec list for error
12146 reporting. */
12147 if (!start_token)
12148 start_token = token;
12149 /* Handle attributes. */
12150 if (cp_next_tokens_can_be_attribute_p (parser))
12152 /* Parse the attributes. */
12153 tree attrs = cp_parser_attributes_opt (parser);
12155 /* In a sequence of declaration specifiers, c++11 attributes
12156 appertain to the type that precede them. In that case
12157 [dcl.spec]/1 says:
12159 The attribute-specifier-seq affects the type only for
12160 the declaration it appears in, not other declarations
12161 involving the same type.
12163 But for now let's force the user to position the
12164 attribute either at the beginning of the declaration or
12165 after the declarator-id, which would clearly mean that it
12166 applies to the declarator. */
12167 if (cxx11_attribute_p (attrs))
12169 if (!found_decl_spec)
12170 /* The c++11 attribute is at the beginning of the
12171 declaration. It appertains to the entity being
12172 declared. */;
12173 else
12175 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12177 /* This is an attribute following a
12178 class-specifier. */
12179 if (decl_specs->type_definition_p)
12180 warn_misplaced_attr_for_class_type (token->location,
12181 decl_specs->type);
12182 attrs = NULL_TREE;
12184 else
12186 decl_specs->std_attributes
12187 = chainon (decl_specs->std_attributes,
12188 attrs);
12189 if (decl_specs->locations[ds_std_attribute] == 0)
12190 decl_specs->locations[ds_std_attribute] = token->location;
12192 continue;
12196 decl_specs->attributes
12197 = chainon (decl_specs->attributes,
12198 attrs);
12199 if (decl_specs->locations[ds_attribute] == 0)
12200 decl_specs->locations[ds_attribute] = token->location;
12201 continue;
12203 /* Assume we will find a decl-specifier keyword. */
12204 found_decl_spec = true;
12205 /* If the next token is an appropriate keyword, we can simply
12206 add it to the list. */
12207 switch (token->keyword)
12209 /* decl-specifier:
12210 friend
12211 constexpr */
12212 case RID_FRIEND:
12213 if (!at_class_scope_p ())
12215 error_at (token->location, "%<friend%> used outside of class");
12216 cp_lexer_purge_token (parser->lexer);
12218 else
12220 ds = ds_friend;
12221 /* Consume the token. */
12222 cp_lexer_consume_token (parser->lexer);
12224 break;
12226 case RID_CONSTEXPR:
12227 ds = ds_constexpr;
12228 cp_lexer_consume_token (parser->lexer);
12229 break;
12231 case RID_CONCEPT:
12232 ds = ds_concept;
12233 cp_lexer_consume_token (parser->lexer);
12234 break;
12236 /* function-specifier:
12237 inline
12238 virtual
12239 explicit */
12240 case RID_INLINE:
12241 case RID_VIRTUAL:
12242 case RID_EXPLICIT:
12243 cp_parser_function_specifier_opt (parser, decl_specs);
12244 break;
12246 /* decl-specifier:
12247 typedef */
12248 case RID_TYPEDEF:
12249 ds = ds_typedef;
12250 /* Consume the token. */
12251 cp_lexer_consume_token (parser->lexer);
12252 /* A constructor declarator cannot appear in a typedef. */
12253 constructor_possible_p = false;
12254 /* The "typedef" keyword can only occur in a declaration; we
12255 may as well commit at this point. */
12256 cp_parser_commit_to_tentative_parse (parser);
12258 if (decl_specs->storage_class != sc_none)
12259 decl_specs->conflicting_specifiers_p = true;
12260 break;
12262 /* storage-class-specifier:
12263 auto
12264 register
12265 static
12266 extern
12267 mutable
12269 GNU Extension:
12270 thread */
12271 case RID_AUTO:
12272 if (cxx_dialect == cxx98)
12274 /* Consume the token. */
12275 cp_lexer_consume_token (parser->lexer);
12277 /* Complain about `auto' as a storage specifier, if
12278 we're complaining about C++0x compatibility. */
12279 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12280 " changes meaning in C++11; please remove it");
12282 /* Set the storage class anyway. */
12283 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12284 token);
12286 else
12287 /* C++0x auto type-specifier. */
12288 found_decl_spec = false;
12289 break;
12291 case RID_REGISTER:
12292 case RID_STATIC:
12293 case RID_EXTERN:
12294 case RID_MUTABLE:
12295 /* Consume the token. */
12296 cp_lexer_consume_token (parser->lexer);
12297 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12298 token);
12299 break;
12300 case RID_THREAD:
12301 /* Consume the token. */
12302 ds = ds_thread;
12303 cp_lexer_consume_token (parser->lexer);
12304 break;
12306 default:
12307 /* We did not yet find a decl-specifier yet. */
12308 found_decl_spec = false;
12309 break;
12312 if (found_decl_spec
12313 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12314 && token->keyword != RID_CONSTEXPR)
12315 error ("decl-specifier invalid in condition");
12317 if (ds != ds_last)
12318 set_and_check_decl_spec_loc (decl_specs, ds, token);
12320 /* Constructors are a special case. The `S' in `S()' is not a
12321 decl-specifier; it is the beginning of the declarator. */
12322 constructor_p
12323 = (!found_decl_spec
12324 && constructor_possible_p
12325 && (cp_parser_constructor_declarator_p
12326 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12328 /* If we don't have a DECL_SPEC yet, then we must be looking at
12329 a type-specifier. */
12330 if (!found_decl_spec && !constructor_p)
12332 int decl_spec_declares_class_or_enum;
12333 bool is_cv_qualifier;
12334 tree type_spec;
12336 type_spec
12337 = cp_parser_type_specifier (parser, flags,
12338 decl_specs,
12339 /*is_declaration=*/true,
12340 &decl_spec_declares_class_or_enum,
12341 &is_cv_qualifier);
12342 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12344 /* If this type-specifier referenced a user-defined type
12345 (a typedef, class-name, etc.), then we can't allow any
12346 more such type-specifiers henceforth.
12348 [dcl.spec]
12350 The longest sequence of decl-specifiers that could
12351 possibly be a type name is taken as the
12352 decl-specifier-seq of a declaration. The sequence shall
12353 be self-consistent as described below.
12355 [dcl.type]
12357 As a general rule, at most one type-specifier is allowed
12358 in the complete decl-specifier-seq of a declaration. The
12359 only exceptions are the following:
12361 -- const or volatile can be combined with any other
12362 type-specifier.
12364 -- signed or unsigned can be combined with char, long,
12365 short, or int.
12367 -- ..
12369 Example:
12371 typedef char* Pc;
12372 void g (const int Pc);
12374 Here, Pc is *not* part of the decl-specifier seq; it's
12375 the declarator. Therefore, once we see a type-specifier
12376 (other than a cv-qualifier), we forbid any additional
12377 user-defined types. We *do* still allow things like `int
12378 int' to be considered a decl-specifier-seq, and issue the
12379 error message later. */
12380 if (type_spec && !is_cv_qualifier)
12381 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12382 /* A constructor declarator cannot follow a type-specifier. */
12383 if (type_spec)
12385 constructor_possible_p = false;
12386 found_decl_spec = true;
12387 if (!is_cv_qualifier)
12388 decl_specs->any_type_specifiers_p = true;
12392 /* If we still do not have a DECL_SPEC, then there are no more
12393 decl-specifiers. */
12394 if (!found_decl_spec)
12395 break;
12397 decl_specs->any_specifiers_p = true;
12398 /* After we see one decl-specifier, further decl-specifiers are
12399 always optional. */
12400 flags |= CP_PARSER_FLAGS_OPTIONAL;
12403 /* Don't allow a friend specifier with a class definition. */
12404 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12405 && (*declares_class_or_enum & 2))
12406 error_at (decl_specs->locations[ds_friend],
12407 "class definition may not be declared a friend");
12410 /* Parse an (optional) storage-class-specifier.
12412 storage-class-specifier:
12413 auto
12414 register
12415 static
12416 extern
12417 mutable
12419 GNU Extension:
12421 storage-class-specifier:
12422 thread
12424 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12426 static tree
12427 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12429 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12431 case RID_AUTO:
12432 if (cxx_dialect != cxx98)
12433 return NULL_TREE;
12434 /* Fall through for C++98. */
12436 case RID_REGISTER:
12437 case RID_STATIC:
12438 case RID_EXTERN:
12439 case RID_MUTABLE:
12440 case RID_THREAD:
12441 /* Consume the token. */
12442 return cp_lexer_consume_token (parser->lexer)->u.value;
12444 default:
12445 return NULL_TREE;
12449 /* Parse an (optional) function-specifier.
12451 function-specifier:
12452 inline
12453 virtual
12454 explicit
12456 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12457 Updates DECL_SPECS, if it is non-NULL. */
12459 static tree
12460 cp_parser_function_specifier_opt (cp_parser* parser,
12461 cp_decl_specifier_seq *decl_specs)
12463 cp_token *token = cp_lexer_peek_token (parser->lexer);
12464 switch (token->keyword)
12466 case RID_INLINE:
12467 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12468 break;
12470 case RID_VIRTUAL:
12471 /* 14.5.2.3 [temp.mem]
12473 A member function template shall not be virtual. */
12474 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12475 error_at (token->location, "templates may not be %<virtual%>");
12476 else
12477 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12478 break;
12480 case RID_EXPLICIT:
12481 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12482 break;
12484 default:
12485 return NULL_TREE;
12488 /* Consume the token. */
12489 return cp_lexer_consume_token (parser->lexer)->u.value;
12492 /* Parse a linkage-specification.
12494 linkage-specification:
12495 extern string-literal { declaration-seq [opt] }
12496 extern string-literal declaration */
12498 static void
12499 cp_parser_linkage_specification (cp_parser* parser)
12501 tree linkage;
12503 /* Look for the `extern' keyword. */
12504 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12506 /* Look for the string-literal. */
12507 linkage = cp_parser_string_literal (parser, false, false);
12509 /* Transform the literal into an identifier. If the literal is a
12510 wide-character string, or contains embedded NULs, then we can't
12511 handle it as the user wants. */
12512 if (strlen (TREE_STRING_POINTER (linkage))
12513 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12515 cp_parser_error (parser, "invalid linkage-specification");
12516 /* Assume C++ linkage. */
12517 linkage = lang_name_cplusplus;
12519 else
12520 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12522 /* We're now using the new linkage. */
12523 push_lang_context (linkage);
12525 /* If the next token is a `{', then we're using the first
12526 production. */
12527 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12529 cp_ensure_no_omp_declare_simd (parser);
12530 cp_ensure_no_oacc_routine (parser);
12532 /* Consume the `{' token. */
12533 cp_lexer_consume_token (parser->lexer);
12534 /* Parse the declarations. */
12535 cp_parser_declaration_seq_opt (parser);
12536 /* Look for the closing `}'. */
12537 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12539 /* Otherwise, there's just one declaration. */
12540 else
12542 bool saved_in_unbraced_linkage_specification_p;
12544 saved_in_unbraced_linkage_specification_p
12545 = parser->in_unbraced_linkage_specification_p;
12546 parser->in_unbraced_linkage_specification_p = true;
12547 cp_parser_declaration (parser);
12548 parser->in_unbraced_linkage_specification_p
12549 = saved_in_unbraced_linkage_specification_p;
12552 /* We're done with the linkage-specification. */
12553 pop_lang_context ();
12556 /* Parse a static_assert-declaration.
12558 static_assert-declaration:
12559 static_assert ( constant-expression , string-literal ) ;
12560 static_assert ( constant-expression ) ; (C++1Z)
12562 If MEMBER_P, this static_assert is a class member. */
12564 static void
12565 cp_parser_static_assert(cp_parser *parser, bool member_p)
12567 tree condition;
12568 tree message;
12569 cp_token *token;
12570 location_t saved_loc;
12571 bool dummy;
12573 /* Peek at the `static_assert' token so we can keep track of exactly
12574 where the static assertion started. */
12575 token = cp_lexer_peek_token (parser->lexer);
12576 saved_loc = token->location;
12578 /* Look for the `static_assert' keyword. */
12579 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12580 RT_STATIC_ASSERT))
12581 return;
12583 /* We know we are in a static assertion; commit to any tentative
12584 parse. */
12585 if (cp_parser_parsing_tentatively (parser))
12586 cp_parser_commit_to_tentative_parse (parser);
12588 /* Parse the `(' starting the static assertion condition. */
12589 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12591 /* Parse the constant-expression. Allow a non-constant expression
12592 here in order to give better diagnostics in finish_static_assert. */
12593 condition =
12594 cp_parser_constant_expression (parser,
12595 /*allow_non_constant_p=*/true,
12596 /*non_constant_p=*/&dummy);
12598 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12600 if (cxx_dialect < cxx1z)
12601 pedwarn (input_location, OPT_Wpedantic,
12602 "static_assert without a message "
12603 "only available with -std=c++1z or -std=gnu++1z");
12604 /* Eat the ')' */
12605 cp_lexer_consume_token (parser->lexer);
12606 message = build_string (1, "");
12607 TREE_TYPE (message) = char_array_type_node;
12608 fix_string_type (message);
12610 else
12612 /* Parse the separating `,'. */
12613 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12615 /* Parse the string-literal message. */
12616 message = cp_parser_string_literal (parser,
12617 /*translate=*/false,
12618 /*wide_ok=*/true);
12620 /* A `)' completes the static assertion. */
12621 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12622 cp_parser_skip_to_closing_parenthesis (parser,
12623 /*recovering=*/true,
12624 /*or_comma=*/false,
12625 /*consume_paren=*/true);
12628 /* A semicolon terminates the declaration. */
12629 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12631 /* Complete the static assertion, which may mean either processing
12632 the static assert now or saving it for template instantiation. */
12633 finish_static_assert (condition, message, saved_loc, member_p);
12636 /* Parse the expression in decltype ( expression ). */
12638 static tree
12639 cp_parser_decltype_expr (cp_parser *parser,
12640 bool &id_expression_or_member_access_p)
12642 cp_token *id_expr_start_token;
12643 tree expr;
12645 /* First, try parsing an id-expression. */
12646 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12647 cp_parser_parse_tentatively (parser);
12648 expr = cp_parser_id_expression (parser,
12649 /*template_keyword_p=*/false,
12650 /*check_dependency_p=*/true,
12651 /*template_p=*/NULL,
12652 /*declarator_p=*/false,
12653 /*optional_p=*/false);
12655 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12657 bool non_integral_constant_expression_p = false;
12658 tree id_expression = expr;
12659 cp_id_kind idk;
12660 const char *error_msg;
12662 if (identifier_p (expr))
12663 /* Lookup the name we got back from the id-expression. */
12664 expr = cp_parser_lookup_name_simple (parser, expr,
12665 id_expr_start_token->location);
12667 if (expr
12668 && expr != error_mark_node
12669 && TREE_CODE (expr) != TYPE_DECL
12670 && (TREE_CODE (expr) != BIT_NOT_EXPR
12671 || !TYPE_P (TREE_OPERAND (expr, 0)))
12672 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12674 /* Complete lookup of the id-expression. */
12675 expr = (finish_id_expression
12676 (id_expression, expr, parser->scope, &idk,
12677 /*integral_constant_expression_p=*/false,
12678 /*allow_non_integral_constant_expression_p=*/true,
12679 &non_integral_constant_expression_p,
12680 /*template_p=*/false,
12681 /*done=*/true,
12682 /*address_p=*/false,
12683 /*template_arg_p=*/false,
12684 &error_msg,
12685 id_expr_start_token->location));
12687 if (expr == error_mark_node)
12688 /* We found an id-expression, but it was something that we
12689 should not have found. This is an error, not something
12690 we can recover from, so note that we found an
12691 id-expression and we'll recover as gracefully as
12692 possible. */
12693 id_expression_or_member_access_p = true;
12696 if (expr
12697 && expr != error_mark_node
12698 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12699 /* We have an id-expression. */
12700 id_expression_or_member_access_p = true;
12703 if (!id_expression_or_member_access_p)
12705 /* Abort the id-expression parse. */
12706 cp_parser_abort_tentative_parse (parser);
12708 /* Parsing tentatively, again. */
12709 cp_parser_parse_tentatively (parser);
12711 /* Parse a class member access. */
12712 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12713 /*cast_p=*/false, /*decltype*/true,
12714 /*member_access_only_p=*/true, NULL);
12716 if (expr
12717 && expr != error_mark_node
12718 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12719 /* We have an id-expression. */
12720 id_expression_or_member_access_p = true;
12723 if (id_expression_or_member_access_p)
12724 /* We have parsed the complete id-expression or member access. */
12725 cp_parser_parse_definitely (parser);
12726 else
12728 /* Abort our attempt to parse an id-expression or member access
12729 expression. */
12730 cp_parser_abort_tentative_parse (parser);
12732 /* Parse a full expression. */
12733 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12734 /*decltype_p=*/true);
12737 return expr;
12740 /* Parse a `decltype' type. Returns the type.
12742 simple-type-specifier:
12743 decltype ( expression )
12744 C++14 proposal:
12745 decltype ( auto ) */
12747 static tree
12748 cp_parser_decltype (cp_parser *parser)
12750 tree expr;
12751 bool id_expression_or_member_access_p = false;
12752 const char *saved_message;
12753 bool saved_integral_constant_expression_p;
12754 bool saved_non_integral_constant_expression_p;
12755 bool saved_greater_than_is_operator_p;
12756 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12758 if (start_token->type == CPP_DECLTYPE)
12760 /* Already parsed. */
12761 cp_lexer_consume_token (parser->lexer);
12762 return start_token->u.value;
12765 /* Look for the `decltype' token. */
12766 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12767 return error_mark_node;
12769 /* Parse the opening `('. */
12770 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12771 return error_mark_node;
12773 /* decltype (auto) */
12774 if (cxx_dialect >= cxx14
12775 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12777 cp_lexer_consume_token (parser->lexer);
12778 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12779 return error_mark_node;
12780 expr = make_decltype_auto ();
12781 AUTO_IS_DECLTYPE (expr) = true;
12782 goto rewrite;
12785 /* Types cannot be defined in a `decltype' expression. Save away the
12786 old message. */
12787 saved_message = parser->type_definition_forbidden_message;
12789 /* And create the new one. */
12790 parser->type_definition_forbidden_message
12791 = G_("types may not be defined in %<decltype%> expressions");
12793 /* The restrictions on constant-expressions do not apply inside
12794 decltype expressions. */
12795 saved_integral_constant_expression_p
12796 = parser->integral_constant_expression_p;
12797 saved_non_integral_constant_expression_p
12798 = parser->non_integral_constant_expression_p;
12799 parser->integral_constant_expression_p = false;
12801 /* Within a parenthesized expression, a `>' token is always
12802 the greater-than operator. */
12803 saved_greater_than_is_operator_p
12804 = parser->greater_than_is_operator_p;
12805 parser->greater_than_is_operator_p = true;
12807 /* Do not actually evaluate the expression. */
12808 ++cp_unevaluated_operand;
12810 /* Do not warn about problems with the expression. */
12811 ++c_inhibit_evaluation_warnings;
12813 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12815 /* Go back to evaluating expressions. */
12816 --cp_unevaluated_operand;
12817 --c_inhibit_evaluation_warnings;
12819 /* The `>' token might be the end of a template-id or
12820 template-parameter-list now. */
12821 parser->greater_than_is_operator_p
12822 = saved_greater_than_is_operator_p;
12824 /* Restore the old message and the integral constant expression
12825 flags. */
12826 parser->type_definition_forbidden_message = saved_message;
12827 parser->integral_constant_expression_p
12828 = saved_integral_constant_expression_p;
12829 parser->non_integral_constant_expression_p
12830 = saved_non_integral_constant_expression_p;
12832 /* Parse to the closing `)'. */
12833 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12835 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12836 /*consume_paren=*/true);
12837 return error_mark_node;
12840 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12841 tf_warning_or_error);
12843 rewrite:
12844 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12845 it again. */
12846 start_token->type = CPP_DECLTYPE;
12847 start_token->u.value = expr;
12848 start_token->keyword = RID_MAX;
12849 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12851 return expr;
12854 /* Special member functions [gram.special] */
12856 /* Parse a conversion-function-id.
12858 conversion-function-id:
12859 operator conversion-type-id
12861 Returns an IDENTIFIER_NODE representing the operator. */
12863 static tree
12864 cp_parser_conversion_function_id (cp_parser* parser)
12866 tree type;
12867 tree saved_scope;
12868 tree saved_qualifying_scope;
12869 tree saved_object_scope;
12870 tree pushed_scope = NULL_TREE;
12872 /* Look for the `operator' token. */
12873 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12874 return error_mark_node;
12875 /* When we parse the conversion-type-id, the current scope will be
12876 reset. However, we need that information in able to look up the
12877 conversion function later, so we save it here. */
12878 saved_scope = parser->scope;
12879 saved_qualifying_scope = parser->qualifying_scope;
12880 saved_object_scope = parser->object_scope;
12881 /* We must enter the scope of the class so that the names of
12882 entities declared within the class are available in the
12883 conversion-type-id. For example, consider:
12885 struct S {
12886 typedef int I;
12887 operator I();
12890 S::operator I() { ... }
12892 In order to see that `I' is a type-name in the definition, we
12893 must be in the scope of `S'. */
12894 if (saved_scope)
12895 pushed_scope = push_scope (saved_scope);
12896 /* Parse the conversion-type-id. */
12897 type = cp_parser_conversion_type_id (parser);
12898 /* Leave the scope of the class, if any. */
12899 if (pushed_scope)
12900 pop_scope (pushed_scope);
12901 /* Restore the saved scope. */
12902 parser->scope = saved_scope;
12903 parser->qualifying_scope = saved_qualifying_scope;
12904 parser->object_scope = saved_object_scope;
12905 /* If the TYPE is invalid, indicate failure. */
12906 if (type == error_mark_node)
12907 return error_mark_node;
12908 return mangle_conv_op_name_for_type (type);
12911 /* Parse a conversion-type-id:
12913 conversion-type-id:
12914 type-specifier-seq conversion-declarator [opt]
12916 Returns the TYPE specified. */
12918 static tree
12919 cp_parser_conversion_type_id (cp_parser* parser)
12921 tree attributes;
12922 cp_decl_specifier_seq type_specifiers;
12923 cp_declarator *declarator;
12924 tree type_specified;
12925 const char *saved_message;
12927 /* Parse the attributes. */
12928 attributes = cp_parser_attributes_opt (parser);
12930 saved_message = parser->type_definition_forbidden_message;
12931 parser->type_definition_forbidden_message
12932 = G_("types may not be defined in a conversion-type-id");
12934 /* Parse the type-specifiers. */
12935 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12936 /*is_trailing_return=*/false,
12937 &type_specifiers);
12939 parser->type_definition_forbidden_message = saved_message;
12941 /* If that didn't work, stop. */
12942 if (type_specifiers.type == error_mark_node)
12943 return error_mark_node;
12944 /* Parse the conversion-declarator. */
12945 declarator = cp_parser_conversion_declarator_opt (parser);
12947 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12948 /*initialized=*/0, &attributes);
12949 if (attributes)
12950 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12952 /* Don't give this error when parsing tentatively. This happens to
12953 work because we always parse this definitively once. */
12954 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12955 && type_uses_auto (type_specified))
12957 if (cxx_dialect < cxx14)
12959 error ("invalid use of %<auto%> in conversion operator");
12960 return error_mark_node;
12962 else if (template_parm_scope_p ())
12963 warning (0, "use of %<auto%> in member template "
12964 "conversion operator can never be deduced");
12967 return type_specified;
12970 /* Parse an (optional) conversion-declarator.
12972 conversion-declarator:
12973 ptr-operator conversion-declarator [opt]
12977 static cp_declarator *
12978 cp_parser_conversion_declarator_opt (cp_parser* parser)
12980 enum tree_code code;
12981 tree class_type, std_attributes = NULL_TREE;
12982 cp_cv_quals cv_quals;
12984 /* We don't know if there's a ptr-operator next, or not. */
12985 cp_parser_parse_tentatively (parser);
12986 /* Try the ptr-operator. */
12987 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12988 &std_attributes);
12989 /* If it worked, look for more conversion-declarators. */
12990 if (cp_parser_parse_definitely (parser))
12992 cp_declarator *declarator;
12994 /* Parse another optional declarator. */
12995 declarator = cp_parser_conversion_declarator_opt (parser);
12997 declarator = cp_parser_make_indirect_declarator
12998 (code, class_type, cv_quals, declarator, std_attributes);
13000 return declarator;
13003 return NULL;
13006 /* Parse an (optional) ctor-initializer.
13008 ctor-initializer:
13009 : mem-initializer-list
13011 Returns TRUE iff the ctor-initializer was actually present. */
13013 static bool
13014 cp_parser_ctor_initializer_opt (cp_parser* parser)
13016 /* If the next token is not a `:', then there is no
13017 ctor-initializer. */
13018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13020 /* Do default initialization of any bases and members. */
13021 if (DECL_CONSTRUCTOR_P (current_function_decl))
13022 finish_mem_initializers (NULL_TREE);
13024 return false;
13027 /* Consume the `:' token. */
13028 cp_lexer_consume_token (parser->lexer);
13029 /* And the mem-initializer-list. */
13030 cp_parser_mem_initializer_list (parser);
13032 return true;
13035 /* Parse a mem-initializer-list.
13037 mem-initializer-list:
13038 mem-initializer ... [opt]
13039 mem-initializer ... [opt] , mem-initializer-list */
13041 static void
13042 cp_parser_mem_initializer_list (cp_parser* parser)
13044 tree mem_initializer_list = NULL_TREE;
13045 tree target_ctor = error_mark_node;
13046 cp_token *token = cp_lexer_peek_token (parser->lexer);
13048 /* Let the semantic analysis code know that we are starting the
13049 mem-initializer-list. */
13050 if (!DECL_CONSTRUCTOR_P (current_function_decl))
13051 error_at (token->location,
13052 "only constructors take member initializers");
13054 /* Loop through the list. */
13055 while (true)
13057 tree mem_initializer;
13059 token = cp_lexer_peek_token (parser->lexer);
13060 /* Parse the mem-initializer. */
13061 mem_initializer = cp_parser_mem_initializer (parser);
13062 /* If the next token is a `...', we're expanding member initializers. */
13063 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13065 /* Consume the `...'. */
13066 cp_lexer_consume_token (parser->lexer);
13068 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13069 can be expanded but members cannot. */
13070 if (mem_initializer != error_mark_node
13071 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13073 error_at (token->location,
13074 "cannot expand initializer for member %<%D%>",
13075 TREE_PURPOSE (mem_initializer));
13076 mem_initializer = error_mark_node;
13079 /* Construct the pack expansion type. */
13080 if (mem_initializer != error_mark_node)
13081 mem_initializer = make_pack_expansion (mem_initializer);
13083 if (target_ctor != error_mark_node
13084 && mem_initializer != error_mark_node)
13086 error ("mem-initializer for %qD follows constructor delegation",
13087 TREE_PURPOSE (mem_initializer));
13088 mem_initializer = error_mark_node;
13090 /* Look for a target constructor. */
13091 if (mem_initializer != error_mark_node
13092 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13093 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13095 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13096 if (mem_initializer_list)
13098 error ("constructor delegation follows mem-initializer for %qD",
13099 TREE_PURPOSE (mem_initializer_list));
13100 mem_initializer = error_mark_node;
13102 target_ctor = mem_initializer;
13104 /* Add it to the list, unless it was erroneous. */
13105 if (mem_initializer != error_mark_node)
13107 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13108 mem_initializer_list = mem_initializer;
13110 /* If the next token is not a `,', we're done. */
13111 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13112 break;
13113 /* Consume the `,' token. */
13114 cp_lexer_consume_token (parser->lexer);
13117 /* Perform semantic analysis. */
13118 if (DECL_CONSTRUCTOR_P (current_function_decl))
13119 finish_mem_initializers (mem_initializer_list);
13122 /* Parse a mem-initializer.
13124 mem-initializer:
13125 mem-initializer-id ( expression-list [opt] )
13126 mem-initializer-id braced-init-list
13128 GNU extension:
13130 mem-initializer:
13131 ( expression-list [opt] )
13133 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13134 class) or FIELD_DECL (for a non-static data member) to initialize;
13135 the TREE_VALUE is the expression-list. An empty initialization
13136 list is represented by void_list_node. */
13138 static tree
13139 cp_parser_mem_initializer (cp_parser* parser)
13141 tree mem_initializer_id;
13142 tree expression_list;
13143 tree member;
13144 cp_token *token = cp_lexer_peek_token (parser->lexer);
13146 /* Find out what is being initialized. */
13147 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13149 permerror (token->location,
13150 "anachronistic old-style base class initializer");
13151 mem_initializer_id = NULL_TREE;
13153 else
13155 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13156 if (mem_initializer_id == error_mark_node)
13157 return mem_initializer_id;
13159 member = expand_member_init (mem_initializer_id);
13160 if (member && !DECL_P (member))
13161 in_base_initializer = 1;
13163 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13165 bool expr_non_constant_p;
13166 cp_lexer_set_source_position (parser->lexer);
13167 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13168 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13169 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13170 expression_list = build_tree_list (NULL_TREE, expression_list);
13172 else
13174 vec<tree, va_gc> *vec;
13175 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13176 /*cast_p=*/false,
13177 /*allow_expansion_p=*/true,
13178 /*non_constant_p=*/NULL);
13179 if (vec == NULL)
13180 return error_mark_node;
13181 expression_list = build_tree_list_vec (vec);
13182 release_tree_vector (vec);
13185 if (expression_list == error_mark_node)
13186 return error_mark_node;
13187 if (!expression_list)
13188 expression_list = void_type_node;
13190 in_base_initializer = 0;
13192 return member ? build_tree_list (member, expression_list) : error_mark_node;
13195 /* Parse a mem-initializer-id.
13197 mem-initializer-id:
13198 :: [opt] nested-name-specifier [opt] class-name
13199 decltype-specifier (C++11)
13200 identifier
13202 Returns a TYPE indicating the class to be initialized for the first
13203 production (and the second in C++11). Returns an IDENTIFIER_NODE
13204 indicating the data member to be initialized for the last production. */
13206 static tree
13207 cp_parser_mem_initializer_id (cp_parser* parser)
13209 bool global_scope_p;
13210 bool nested_name_specifier_p;
13211 bool template_p = false;
13212 tree id;
13214 cp_token *token = cp_lexer_peek_token (parser->lexer);
13216 /* `typename' is not allowed in this context ([temp.res]). */
13217 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13219 error_at (token->location,
13220 "keyword %<typename%> not allowed in this context (a qualified "
13221 "member initializer is implicitly a type)");
13222 cp_lexer_consume_token (parser->lexer);
13224 /* Look for the optional `::' operator. */
13225 global_scope_p
13226 = (cp_parser_global_scope_opt (parser,
13227 /*current_scope_valid_p=*/false)
13228 != NULL_TREE);
13229 /* Look for the optional nested-name-specifier. The simplest way to
13230 implement:
13232 [temp.res]
13234 The keyword `typename' is not permitted in a base-specifier or
13235 mem-initializer; in these contexts a qualified name that
13236 depends on a template-parameter is implicitly assumed to be a
13237 type name.
13239 is to assume that we have seen the `typename' keyword at this
13240 point. */
13241 nested_name_specifier_p
13242 = (cp_parser_nested_name_specifier_opt (parser,
13243 /*typename_keyword_p=*/true,
13244 /*check_dependency_p=*/true,
13245 /*type_p=*/true,
13246 /*is_declaration=*/true)
13247 != NULL_TREE);
13248 if (nested_name_specifier_p)
13249 template_p = cp_parser_optional_template_keyword (parser);
13250 /* If there is a `::' operator or a nested-name-specifier, then we
13251 are definitely looking for a class-name. */
13252 if (global_scope_p || nested_name_specifier_p)
13253 return cp_parser_class_name (parser,
13254 /*typename_keyword_p=*/true,
13255 /*template_keyword_p=*/template_p,
13256 typename_type,
13257 /*check_dependency_p=*/true,
13258 /*class_head_p=*/false,
13259 /*is_declaration=*/true);
13260 /* Otherwise, we could also be looking for an ordinary identifier. */
13261 cp_parser_parse_tentatively (parser);
13262 if (cp_lexer_next_token_is_decltype (parser->lexer))
13263 /* Try a decltype-specifier. */
13264 id = cp_parser_decltype (parser);
13265 else
13266 /* Otherwise, try a class-name. */
13267 id = cp_parser_class_name (parser,
13268 /*typename_keyword_p=*/true,
13269 /*template_keyword_p=*/false,
13270 none_type,
13271 /*check_dependency_p=*/true,
13272 /*class_head_p=*/false,
13273 /*is_declaration=*/true);
13274 /* If we found one, we're done. */
13275 if (cp_parser_parse_definitely (parser))
13276 return id;
13277 /* Otherwise, look for an ordinary identifier. */
13278 return cp_parser_identifier (parser);
13281 /* Overloading [gram.over] */
13283 /* Parse an operator-function-id.
13285 operator-function-id:
13286 operator operator
13288 Returns an IDENTIFIER_NODE for the operator which is a
13289 human-readable spelling of the identifier, e.g., `operator +'. */
13291 static tree
13292 cp_parser_operator_function_id (cp_parser* parser)
13294 /* Look for the `operator' keyword. */
13295 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13296 return error_mark_node;
13297 /* And then the name of the operator itself. */
13298 return cp_parser_operator (parser);
13301 /* Return an identifier node for a user-defined literal operator.
13302 The suffix identifier is chained to the operator name identifier. */
13304 static tree
13305 cp_literal_operator_id (const char* name)
13307 tree identifier;
13308 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13309 + strlen (name) + 10);
13310 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13311 identifier = get_identifier (buffer);
13313 return identifier;
13316 /* Parse an operator.
13318 operator:
13319 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13320 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13321 || ++ -- , ->* -> () []
13323 GNU Extensions:
13325 operator:
13326 <? >? <?= >?=
13328 Returns an IDENTIFIER_NODE for the operator which is a
13329 human-readable spelling of the identifier, e.g., `operator +'. */
13331 static tree
13332 cp_parser_operator (cp_parser* parser)
13334 tree id = NULL_TREE;
13335 cp_token *token;
13336 bool utf8 = false;
13338 /* Peek at the next token. */
13339 token = cp_lexer_peek_token (parser->lexer);
13340 /* Figure out which operator we have. */
13341 switch (token->type)
13343 case CPP_KEYWORD:
13345 enum tree_code op;
13347 /* The keyword should be either `new' or `delete'. */
13348 if (token->keyword == RID_NEW)
13349 op = NEW_EXPR;
13350 else if (token->keyword == RID_DELETE)
13351 op = DELETE_EXPR;
13352 else
13353 break;
13355 /* Consume the `new' or `delete' token. */
13356 cp_lexer_consume_token (parser->lexer);
13358 /* Peek at the next token. */
13359 token = cp_lexer_peek_token (parser->lexer);
13360 /* If it's a `[' token then this is the array variant of the
13361 operator. */
13362 if (token->type == CPP_OPEN_SQUARE)
13364 /* Consume the `[' token. */
13365 cp_lexer_consume_token (parser->lexer);
13366 /* Look for the `]' token. */
13367 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13368 id = ansi_opname (op == NEW_EXPR
13369 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13371 /* Otherwise, we have the non-array variant. */
13372 else
13373 id = ansi_opname (op);
13375 return id;
13378 case CPP_PLUS:
13379 id = ansi_opname (PLUS_EXPR);
13380 break;
13382 case CPP_MINUS:
13383 id = ansi_opname (MINUS_EXPR);
13384 break;
13386 case CPP_MULT:
13387 id = ansi_opname (MULT_EXPR);
13388 break;
13390 case CPP_DIV:
13391 id = ansi_opname (TRUNC_DIV_EXPR);
13392 break;
13394 case CPP_MOD:
13395 id = ansi_opname (TRUNC_MOD_EXPR);
13396 break;
13398 case CPP_XOR:
13399 id = ansi_opname (BIT_XOR_EXPR);
13400 break;
13402 case CPP_AND:
13403 id = ansi_opname (BIT_AND_EXPR);
13404 break;
13406 case CPP_OR:
13407 id = ansi_opname (BIT_IOR_EXPR);
13408 break;
13410 case CPP_COMPL:
13411 id = ansi_opname (BIT_NOT_EXPR);
13412 break;
13414 case CPP_NOT:
13415 id = ansi_opname (TRUTH_NOT_EXPR);
13416 break;
13418 case CPP_EQ:
13419 id = ansi_assopname (NOP_EXPR);
13420 break;
13422 case CPP_LESS:
13423 id = ansi_opname (LT_EXPR);
13424 break;
13426 case CPP_GREATER:
13427 id = ansi_opname (GT_EXPR);
13428 break;
13430 case CPP_PLUS_EQ:
13431 id = ansi_assopname (PLUS_EXPR);
13432 break;
13434 case CPP_MINUS_EQ:
13435 id = ansi_assopname (MINUS_EXPR);
13436 break;
13438 case CPP_MULT_EQ:
13439 id = ansi_assopname (MULT_EXPR);
13440 break;
13442 case CPP_DIV_EQ:
13443 id = ansi_assopname (TRUNC_DIV_EXPR);
13444 break;
13446 case CPP_MOD_EQ:
13447 id = ansi_assopname (TRUNC_MOD_EXPR);
13448 break;
13450 case CPP_XOR_EQ:
13451 id = ansi_assopname (BIT_XOR_EXPR);
13452 break;
13454 case CPP_AND_EQ:
13455 id = ansi_assopname (BIT_AND_EXPR);
13456 break;
13458 case CPP_OR_EQ:
13459 id = ansi_assopname (BIT_IOR_EXPR);
13460 break;
13462 case CPP_LSHIFT:
13463 id = ansi_opname (LSHIFT_EXPR);
13464 break;
13466 case CPP_RSHIFT:
13467 id = ansi_opname (RSHIFT_EXPR);
13468 break;
13470 case CPP_LSHIFT_EQ:
13471 id = ansi_assopname (LSHIFT_EXPR);
13472 break;
13474 case CPP_RSHIFT_EQ:
13475 id = ansi_assopname (RSHIFT_EXPR);
13476 break;
13478 case CPP_EQ_EQ:
13479 id = ansi_opname (EQ_EXPR);
13480 break;
13482 case CPP_NOT_EQ:
13483 id = ansi_opname (NE_EXPR);
13484 break;
13486 case CPP_LESS_EQ:
13487 id = ansi_opname (LE_EXPR);
13488 break;
13490 case CPP_GREATER_EQ:
13491 id = ansi_opname (GE_EXPR);
13492 break;
13494 case CPP_AND_AND:
13495 id = ansi_opname (TRUTH_ANDIF_EXPR);
13496 break;
13498 case CPP_OR_OR:
13499 id = ansi_opname (TRUTH_ORIF_EXPR);
13500 break;
13502 case CPP_PLUS_PLUS:
13503 id = ansi_opname (POSTINCREMENT_EXPR);
13504 break;
13506 case CPP_MINUS_MINUS:
13507 id = ansi_opname (PREDECREMENT_EXPR);
13508 break;
13510 case CPP_COMMA:
13511 id = ansi_opname (COMPOUND_EXPR);
13512 break;
13514 case CPP_DEREF_STAR:
13515 id = ansi_opname (MEMBER_REF);
13516 break;
13518 case CPP_DEREF:
13519 id = ansi_opname (COMPONENT_REF);
13520 break;
13522 case CPP_OPEN_PAREN:
13523 /* Consume the `('. */
13524 cp_lexer_consume_token (parser->lexer);
13525 /* Look for the matching `)'. */
13526 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13527 return ansi_opname (CALL_EXPR);
13529 case CPP_OPEN_SQUARE:
13530 /* Consume the `['. */
13531 cp_lexer_consume_token (parser->lexer);
13532 /* Look for the matching `]'. */
13533 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13534 return ansi_opname (ARRAY_REF);
13536 case CPP_UTF8STRING:
13537 case CPP_UTF8STRING_USERDEF:
13538 utf8 = true;
13539 case CPP_STRING:
13540 case CPP_WSTRING:
13541 case CPP_STRING16:
13542 case CPP_STRING32:
13543 case CPP_STRING_USERDEF:
13544 case CPP_WSTRING_USERDEF:
13545 case CPP_STRING16_USERDEF:
13546 case CPP_STRING32_USERDEF:
13548 tree str, string_tree;
13549 int sz, len;
13551 if (cxx_dialect == cxx98)
13552 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13554 /* Consume the string. */
13555 str = cp_parser_string_literal (parser, /*translate=*/true,
13556 /*wide_ok=*/true, /*lookup_udlit=*/false);
13557 if (str == error_mark_node)
13558 return error_mark_node;
13559 else if (TREE_CODE (str) == USERDEF_LITERAL)
13561 string_tree = USERDEF_LITERAL_VALUE (str);
13562 id = USERDEF_LITERAL_SUFFIX_ID (str);
13564 else
13566 string_tree = str;
13567 /* Look for the suffix identifier. */
13568 token = cp_lexer_peek_token (parser->lexer);
13569 if (token->type == CPP_NAME)
13570 id = cp_parser_identifier (parser);
13571 else if (token->type == CPP_KEYWORD)
13573 error ("unexpected keyword;"
13574 " remove space between quotes and suffix identifier");
13575 return error_mark_node;
13577 else
13579 error ("expected suffix identifier");
13580 return error_mark_node;
13583 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13584 (TREE_TYPE (TREE_TYPE (string_tree))));
13585 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13586 if (len != 0)
13588 error ("expected empty string after %<operator%> keyword");
13589 return error_mark_node;
13591 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13592 != char_type_node)
13594 error ("invalid encoding prefix in literal operator");
13595 return error_mark_node;
13597 if (id != error_mark_node)
13599 const char *name = IDENTIFIER_POINTER (id);
13600 id = cp_literal_operator_id (name);
13602 return id;
13605 default:
13606 /* Anything else is an error. */
13607 break;
13610 /* If we have selected an identifier, we need to consume the
13611 operator token. */
13612 if (id)
13613 cp_lexer_consume_token (parser->lexer);
13614 /* Otherwise, no valid operator name was present. */
13615 else
13617 cp_parser_error (parser, "expected operator");
13618 id = error_mark_node;
13621 return id;
13624 /* Parse a template-declaration.
13626 template-declaration:
13627 export [opt] template < template-parameter-list > declaration
13629 If MEMBER_P is TRUE, this template-declaration occurs within a
13630 class-specifier.
13632 The grammar rule given by the standard isn't correct. What
13633 is really meant is:
13635 template-declaration:
13636 export [opt] template-parameter-list-seq
13637 decl-specifier-seq [opt] init-declarator [opt] ;
13638 export [opt] template-parameter-list-seq
13639 function-definition
13641 template-parameter-list-seq:
13642 template-parameter-list-seq [opt]
13643 template < template-parameter-list >
13645 Concept Extensions:
13647 template-parameter-list-seq:
13648 template < template-parameter-list > requires-clause [opt]
13650 requires-clause:
13651 requires logical-or-expression */
13653 static void
13654 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13656 /* Check for `export'. */
13657 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13659 /* Consume the `export' token. */
13660 cp_lexer_consume_token (parser->lexer);
13661 /* Warn that we do not support `export'. */
13662 warning (0, "keyword %<export%> not implemented, and will be ignored");
13665 cp_parser_template_declaration_after_export (parser, member_p);
13668 /* Parse a template-parameter-list.
13670 template-parameter-list:
13671 template-parameter
13672 template-parameter-list , template-parameter
13674 Returns a TREE_LIST. Each node represents a template parameter.
13675 The nodes are connected via their TREE_CHAINs. */
13677 static tree
13678 cp_parser_template_parameter_list (cp_parser* parser)
13680 tree parameter_list = NULL_TREE;
13682 begin_template_parm_list ();
13684 /* The loop below parses the template parms. We first need to know
13685 the total number of template parms to be able to compute proper
13686 canonical types of each dependent type. So after the loop, when
13687 we know the total number of template parms,
13688 end_template_parm_list computes the proper canonical types and
13689 fixes up the dependent types accordingly. */
13690 while (true)
13692 tree parameter;
13693 bool is_non_type;
13694 bool is_parameter_pack;
13695 location_t parm_loc;
13697 /* Parse the template-parameter. */
13698 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13699 parameter = cp_parser_template_parameter (parser,
13700 &is_non_type,
13701 &is_parameter_pack);
13702 /* Add it to the list. */
13703 if (parameter != error_mark_node)
13704 parameter_list = process_template_parm (parameter_list,
13705 parm_loc,
13706 parameter,
13707 is_non_type,
13708 is_parameter_pack);
13709 else
13711 tree err_parm = build_tree_list (parameter, parameter);
13712 parameter_list = chainon (parameter_list, err_parm);
13715 /* If the next token is not a `,', we're done. */
13716 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13717 break;
13718 /* Otherwise, consume the `,' token. */
13719 cp_lexer_consume_token (parser->lexer);
13722 return end_template_parm_list (parameter_list);
13725 /* Parse a introduction-list.
13727 introduction-list:
13728 introduced-parameter
13729 introduction-list , introduced-parameter
13731 introduced-parameter:
13732 ...[opt] identifier
13734 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
13735 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
13736 WILDCARD_DECL will also have DECL_NAME set and token location in
13737 DECL_SOURCE_LOCATION. */
13739 static tree
13740 cp_parser_introduction_list (cp_parser *parser)
13742 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
13744 while (true)
13746 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
13747 if (is_pack)
13748 cp_lexer_consume_token (parser->lexer);
13750 /* Build placeholder. */
13751 tree parm = build_nt (WILDCARD_DECL);
13752 DECL_SOURCE_LOCATION (parm)
13753 = cp_lexer_peek_token (parser->lexer)->location;
13754 DECL_NAME (parm) = cp_parser_identifier (parser);
13755 WILDCARD_PACK_P (parm) = is_pack;
13756 vec_safe_push (introduction_vec, parm);
13758 /* If the next token is not a `,', we're done. */
13759 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13760 break;
13761 /* Otherwise, consume the `,' token. */
13762 cp_lexer_consume_token (parser->lexer);
13765 /* Convert the vec into a TREE_VEC. */
13766 tree introduction_list = make_tree_vec (introduction_vec->length ());
13767 unsigned int n;
13768 tree parm;
13769 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
13770 TREE_VEC_ELT (introduction_list, n) = parm;
13772 release_tree_vector (introduction_vec);
13773 return introduction_list;
13776 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
13777 is an abstract declarator. */
13779 static inline cp_declarator*
13780 get_id_declarator (cp_declarator *declarator)
13782 cp_declarator *d = declarator;
13783 while (d && d->kind != cdk_id)
13784 d = d->declarator;
13785 return d;
13788 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
13789 is an abstract declarator. */
13791 static inline tree
13792 get_unqualified_id (cp_declarator *declarator)
13794 declarator = get_id_declarator (declarator);
13795 if (declarator)
13796 return declarator->u.id.unqualified_name;
13797 else
13798 return NULL_TREE;
13801 /* Returns true if DECL represents a constrained-parameter. */
13803 static inline bool
13804 is_constrained_parameter (tree decl)
13806 return (decl
13807 && TREE_CODE (decl) == TYPE_DECL
13808 && CONSTRAINED_PARM_CONCEPT (decl)
13809 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
13812 /* Returns true if PARM declares a constrained-parameter. */
13814 static inline bool
13815 is_constrained_parameter (cp_parameter_declarator *parm)
13817 return is_constrained_parameter (parm->decl_specifiers.type);
13820 /* Check that the type parameter is only a declarator-id, and that its
13821 type is not cv-qualified. */
13823 bool
13824 cp_parser_check_constrained_type_parm (cp_parser *parser,
13825 cp_parameter_declarator *parm)
13827 if (!parm->declarator)
13828 return true;
13830 if (parm->declarator->kind != cdk_id)
13832 cp_parser_error (parser, "invalid constrained type parameter");
13833 return false;
13836 /* Don't allow cv-qualified type parameters. */
13837 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
13838 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
13840 cp_parser_error (parser, "cv-qualified type parameter");
13841 return false;
13844 return true;
13847 /* Finish parsing/processing a template type parameter and checking
13848 various restrictions. */
13850 static inline tree
13851 cp_parser_constrained_type_template_parm (cp_parser *parser,
13852 tree id,
13853 cp_parameter_declarator* parmdecl)
13855 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
13856 return finish_template_type_parm (class_type_node, id);
13857 else
13858 return error_mark_node;
13861 static tree
13862 finish_constrained_template_template_parm (tree proto, tree id)
13864 /* FIXME: This should probably be copied, and we may need to adjust
13865 the template parameter depths. */
13866 tree saved_parms = current_template_parms;
13867 begin_template_parm_list ();
13868 current_template_parms = DECL_TEMPLATE_PARMS (proto);
13869 end_template_parm_list ();
13871 tree parm = finish_template_template_parm (class_type_node, id);
13872 current_template_parms = saved_parms;
13874 return parm;
13877 /* Finish parsing/processing a template template parameter by borrowing
13878 the template parameter list from the prototype parameter. */
13880 static tree
13881 cp_parser_constrained_template_template_parm (cp_parser *parser,
13882 tree proto,
13883 tree id,
13884 cp_parameter_declarator *parmdecl)
13886 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
13887 return error_mark_node;
13888 return finish_constrained_template_template_parm (proto, id);
13891 /* Create a new non-type template parameter from the given PARM
13892 declarator. */
13894 static tree
13895 constrained_non_type_template_parm (bool *is_non_type,
13896 cp_parameter_declarator *parm)
13898 *is_non_type = true;
13899 cp_declarator *decl = parm->declarator;
13900 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
13901 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
13902 return grokdeclarator (decl, specs, TPARM, 0, NULL);
13905 /* Build a constrained template parameter based on the PARMDECL
13906 declarator. The type of PARMDECL is the constrained type, which
13907 refers to the prototype template parameter that ultimately
13908 specifies the type of the declared parameter. */
13910 static tree
13911 finish_constrained_parameter (cp_parser *parser,
13912 cp_parameter_declarator *parmdecl,
13913 bool *is_non_type,
13914 bool *is_parameter_pack)
13916 tree decl = parmdecl->decl_specifiers.type;
13917 tree id = get_unqualified_id (parmdecl->declarator);
13918 tree def = parmdecl->default_argument;
13919 tree proto = DECL_INITIAL (decl);
13921 /* A template parameter constrained by a variadic concept shall also
13922 be declared as a template parameter pack. */
13923 bool is_variadic = template_parameter_pack_p (proto);
13924 if (is_variadic && !*is_parameter_pack)
13925 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
13927 /* Build the parameter. Return an error if the declarator was invalid. */
13928 tree parm;
13929 if (TREE_CODE (proto) == TYPE_DECL)
13930 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
13931 else if (TREE_CODE (proto) == TEMPLATE_DECL)
13932 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
13933 parmdecl);
13934 else
13935 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
13936 if (parm == error_mark_node)
13937 return error_mark_node;
13939 /* Finish the parameter decl and create a node attaching the
13940 default argument and constraint. */
13941 parm = build_tree_list (def, parm);
13942 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
13944 return parm;
13947 /* Returns true if the parsed type actually represents the declaration
13948 of a type template-parameter. */
13950 static inline bool
13951 declares_constrained_type_template_parameter (tree type)
13953 return (is_constrained_parameter (type)
13954 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
13958 /* Returns true if the parsed type actually represents the declaration of
13959 a template template-parameter. */
13961 static bool
13962 declares_constrained_template_template_parameter (tree type)
13964 return (is_constrained_parameter (type)
13965 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
13968 /* Parse a default argument for a type template-parameter.
13969 Note that diagnostics are handled in cp_parser_template_parameter. */
13971 static tree
13972 cp_parser_default_type_template_argument (cp_parser *parser)
13974 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
13976 /* Consume the `=' token. */
13977 cp_lexer_consume_token (parser->lexer);
13979 /* Parse the default-argument. */
13980 push_deferring_access_checks (dk_no_deferred);
13981 tree default_argument = cp_parser_type_id (parser);
13982 pop_deferring_access_checks ();
13984 return default_argument;
13987 /* Parse a default argument for a template template-parameter. */
13989 static tree
13990 cp_parser_default_template_template_argument (cp_parser *parser)
13992 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
13994 bool is_template;
13996 /* Consume the `='. */
13997 cp_lexer_consume_token (parser->lexer);
13998 /* Parse the id-expression. */
13999 push_deferring_access_checks (dk_no_deferred);
14000 /* save token before parsing the id-expression, for error
14001 reporting */
14002 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14003 tree default_argument
14004 = cp_parser_id_expression (parser,
14005 /*template_keyword_p=*/false,
14006 /*check_dependency_p=*/true,
14007 /*template_p=*/&is_template,
14008 /*declarator_p=*/false,
14009 /*optional_p=*/false);
14010 if (TREE_CODE (default_argument) == TYPE_DECL)
14011 /* If the id-expression was a template-id that refers to
14012 a template-class, we already have the declaration here,
14013 so no further lookup is needed. */
14015 else
14016 /* Look up the name. */
14017 default_argument
14018 = cp_parser_lookup_name (parser, default_argument,
14019 none_type,
14020 /*is_template=*/is_template,
14021 /*is_namespace=*/false,
14022 /*check_dependency=*/true,
14023 /*ambiguous_decls=*/NULL,
14024 token->location);
14025 /* See if the default argument is valid. */
14026 default_argument = check_template_template_default_arg (default_argument);
14027 pop_deferring_access_checks ();
14028 return default_argument;
14031 /* Parse a template-parameter.
14033 template-parameter:
14034 type-parameter
14035 parameter-declaration
14037 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14038 the parameter. The TREE_PURPOSE is the default value, if any.
14039 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14040 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14041 set to true iff this parameter is a parameter pack. */
14043 static tree
14044 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14045 bool *is_parameter_pack)
14047 cp_token *token;
14048 cp_parameter_declarator *parameter_declarator;
14049 tree parm;
14051 /* Assume it is a type parameter or a template parameter. */
14052 *is_non_type = false;
14053 /* Assume it not a parameter pack. */
14054 *is_parameter_pack = false;
14055 /* Peek at the next token. */
14056 token = cp_lexer_peek_token (parser->lexer);
14057 /* If it is `class' or `template', we have a type-parameter. */
14058 if (token->keyword == RID_TEMPLATE)
14059 return cp_parser_type_parameter (parser, is_parameter_pack);
14060 /* If it is `class' or `typename' we do not know yet whether it is a
14061 type parameter or a non-type parameter. Consider:
14063 template <typename T, typename T::X X> ...
14067 template <class C, class D*> ...
14069 Here, the first parameter is a type parameter, and the second is
14070 a non-type parameter. We can tell by looking at the token after
14071 the identifier -- if it is a `,', `=', or `>' then we have a type
14072 parameter. */
14073 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14075 /* Peek at the token after `class' or `typename'. */
14076 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14077 /* If it's an ellipsis, we have a template type parameter
14078 pack. */
14079 if (token->type == CPP_ELLIPSIS)
14080 return cp_parser_type_parameter (parser, is_parameter_pack);
14081 /* If it's an identifier, skip it. */
14082 if (token->type == CPP_NAME)
14083 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14084 /* Now, see if the token looks like the end of a template
14085 parameter. */
14086 if (token->type == CPP_COMMA
14087 || token->type == CPP_EQ
14088 || token->type == CPP_GREATER)
14089 return cp_parser_type_parameter (parser, is_parameter_pack);
14092 /* Otherwise, it is a non-type parameter or a constrained parameter.
14094 [temp.param]
14096 When parsing a default template-argument for a non-type
14097 template-parameter, the first non-nested `>' is taken as the end
14098 of the template parameter-list rather than a greater-than
14099 operator. */
14100 parameter_declarator
14101 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14102 /*parenthesized_p=*/NULL);
14104 if (!parameter_declarator)
14105 return error_mark_node;
14107 /* If the parameter declaration is marked as a parameter pack, set
14108 *IS_PARAMETER_PACK to notify the caller. */
14109 if (parameter_declarator->template_parameter_pack_p)
14110 *is_parameter_pack = true;
14112 if (parameter_declarator->default_argument)
14114 /* Can happen in some cases of erroneous input (c++/34892). */
14115 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14116 /* Consume the `...' for better error recovery. */
14117 cp_lexer_consume_token (parser->lexer);
14120 // The parameter may have been constrained.
14121 if (is_constrained_parameter (parameter_declarator))
14122 return finish_constrained_parameter (parser,
14123 parameter_declarator,
14124 is_non_type,
14125 is_parameter_pack);
14127 // Now we're sure that the parameter is a non-type parameter.
14128 *is_non_type = true;
14130 parm = grokdeclarator (parameter_declarator->declarator,
14131 &parameter_declarator->decl_specifiers,
14132 TPARM, /*initialized=*/0,
14133 /*attrlist=*/NULL);
14134 if (parm == error_mark_node)
14135 return error_mark_node;
14137 return build_tree_list (parameter_declarator->default_argument, parm);
14140 /* Parse a type-parameter.
14142 type-parameter:
14143 class identifier [opt]
14144 class identifier [opt] = type-id
14145 typename identifier [opt]
14146 typename identifier [opt] = type-id
14147 template < template-parameter-list > class identifier [opt]
14148 template < template-parameter-list > class identifier [opt]
14149 = id-expression
14151 GNU Extension (variadic templates):
14153 type-parameter:
14154 class ... identifier [opt]
14155 typename ... identifier [opt]
14157 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14158 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14159 the declaration of the parameter.
14161 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14163 static tree
14164 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14166 cp_token *token;
14167 tree parameter;
14169 /* Look for a keyword to tell us what kind of parameter this is. */
14170 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14171 if (!token)
14172 return error_mark_node;
14174 switch (token->keyword)
14176 case RID_CLASS:
14177 case RID_TYPENAME:
14179 tree identifier;
14180 tree default_argument;
14182 /* If the next token is an ellipsis, we have a template
14183 argument pack. */
14184 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14186 /* Consume the `...' token. */
14187 cp_lexer_consume_token (parser->lexer);
14188 maybe_warn_variadic_templates ();
14190 *is_parameter_pack = true;
14193 /* If the next token is an identifier, then it names the
14194 parameter. */
14195 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14196 identifier = cp_parser_identifier (parser);
14197 else
14198 identifier = NULL_TREE;
14200 /* Create the parameter. */
14201 parameter = finish_template_type_parm (class_type_node, identifier);
14203 /* If the next token is an `=', we have a default argument. */
14204 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14206 default_argument
14207 = cp_parser_default_type_template_argument (parser);
14209 /* Template parameter packs cannot have default
14210 arguments. */
14211 if (*is_parameter_pack)
14213 if (identifier)
14214 error_at (token->location,
14215 "template parameter pack %qD cannot have a "
14216 "default argument", identifier);
14217 else
14218 error_at (token->location,
14219 "template parameter packs cannot have "
14220 "default arguments");
14221 default_argument = NULL_TREE;
14223 else if (check_for_bare_parameter_packs (default_argument))
14224 default_argument = error_mark_node;
14226 else
14227 default_argument = NULL_TREE;
14229 /* Create the combined representation of the parameter and the
14230 default argument. */
14231 parameter = build_tree_list (default_argument, parameter);
14233 break;
14235 case RID_TEMPLATE:
14237 tree identifier;
14238 tree default_argument;
14240 /* Look for the `<'. */
14241 cp_parser_require (parser, CPP_LESS, RT_LESS);
14242 /* Parse the template-parameter-list. */
14243 cp_parser_template_parameter_list (parser);
14244 /* Look for the `>'. */
14245 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14247 // If template requirements are present, parse them.
14248 if (flag_concepts)
14250 tree reqs = get_shorthand_constraints (current_template_parms);
14251 if (tree r = cp_parser_requires_clause_opt (parser))
14252 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
14253 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14256 /* Look for the `class' or 'typename' keywords. */
14257 cp_parser_type_parameter_key (parser);
14258 /* If the next token is an ellipsis, we have a template
14259 argument pack. */
14260 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14262 /* Consume the `...' token. */
14263 cp_lexer_consume_token (parser->lexer);
14264 maybe_warn_variadic_templates ();
14266 *is_parameter_pack = true;
14268 /* If the next token is an `=', then there is a
14269 default-argument. If the next token is a `>', we are at
14270 the end of the parameter-list. If the next token is a `,',
14271 then we are at the end of this parameter. */
14272 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14273 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14274 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14276 identifier = cp_parser_identifier (parser);
14277 /* Treat invalid names as if the parameter were nameless. */
14278 if (identifier == error_mark_node)
14279 identifier = NULL_TREE;
14281 else
14282 identifier = NULL_TREE;
14284 /* Create the template parameter. */
14285 parameter = finish_template_template_parm (class_type_node,
14286 identifier);
14288 /* If the next token is an `=', then there is a
14289 default-argument. */
14290 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14292 default_argument
14293 = cp_parser_default_template_template_argument (parser);
14295 /* Template parameter packs cannot have default
14296 arguments. */
14297 if (*is_parameter_pack)
14299 if (identifier)
14300 error_at (token->location,
14301 "template parameter pack %qD cannot "
14302 "have a default argument",
14303 identifier);
14304 else
14305 error_at (token->location, "template parameter packs cannot "
14306 "have default arguments");
14307 default_argument = NULL_TREE;
14310 else
14311 default_argument = NULL_TREE;
14313 /* Create the combined representation of the parameter and the
14314 default argument. */
14315 parameter = build_tree_list (default_argument, parameter);
14317 break;
14319 default:
14320 gcc_unreachable ();
14321 break;
14324 return parameter;
14327 /* Parse a template-id.
14329 template-id:
14330 template-name < template-argument-list [opt] >
14332 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14333 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14334 returned. Otherwise, if the template-name names a function, or set
14335 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14336 names a class, returns a TYPE_DECL for the specialization.
14338 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14339 uninstantiated templates. */
14341 static tree
14342 cp_parser_template_id (cp_parser *parser,
14343 bool template_keyword_p,
14344 bool check_dependency_p,
14345 enum tag_types tag_type,
14346 bool is_declaration)
14348 int i;
14349 tree templ;
14350 tree arguments;
14351 tree template_id;
14352 cp_token_position start_of_id = 0;
14353 deferred_access_check *chk;
14354 vec<deferred_access_check, va_gc> *access_check;
14355 cp_token *next_token = NULL, *next_token_2 = NULL;
14356 bool is_identifier;
14358 /* If the next token corresponds to a template-id, there is no need
14359 to reparse it. */
14360 next_token = cp_lexer_peek_token (parser->lexer);
14361 if (next_token->type == CPP_TEMPLATE_ID)
14363 struct tree_check *check_value;
14365 /* Get the stored value. */
14366 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
14367 /* Perform any access checks that were deferred. */
14368 access_check = check_value->checks;
14369 if (access_check)
14371 FOR_EACH_VEC_ELT (*access_check, i, chk)
14372 perform_or_defer_access_check (chk->binfo,
14373 chk->decl,
14374 chk->diag_decl,
14375 tf_warning_or_error);
14377 /* Return the stored value. */
14378 return check_value->value;
14381 /* Avoid performing name lookup if there is no possibility of
14382 finding a template-id. */
14383 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14384 || (next_token->type == CPP_NAME
14385 && !cp_parser_nth_token_starts_template_argument_list_p
14386 (parser, 2)))
14388 cp_parser_error (parser, "expected template-id");
14389 return error_mark_node;
14392 /* Remember where the template-id starts. */
14393 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14394 start_of_id = cp_lexer_token_position (parser->lexer, false);
14396 push_deferring_access_checks (dk_deferred);
14398 /* Parse the template-name. */
14399 is_identifier = false;
14400 templ = cp_parser_template_name (parser, template_keyword_p,
14401 check_dependency_p,
14402 is_declaration,
14403 tag_type,
14404 &is_identifier);
14405 if (templ == error_mark_node || is_identifier)
14407 pop_deferring_access_checks ();
14408 return templ;
14411 /* If we find the sequence `[:' after a template-name, it's probably
14412 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14413 parse correctly the argument list. */
14414 next_token = cp_lexer_peek_token (parser->lexer);
14415 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14416 if (next_token->type == CPP_OPEN_SQUARE
14417 && next_token->flags & DIGRAPH
14418 && next_token_2->type == CPP_COLON
14419 && !(next_token_2->flags & PREV_WHITE))
14421 cp_parser_parse_tentatively (parser);
14422 /* Change `:' into `::'. */
14423 next_token_2->type = CPP_SCOPE;
14424 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14425 CPP_LESS. */
14426 cp_lexer_consume_token (parser->lexer);
14428 /* Parse the arguments. */
14429 arguments = cp_parser_enclosed_template_argument_list (parser);
14430 if (!cp_parser_parse_definitely (parser))
14432 /* If we couldn't parse an argument list, then we revert our changes
14433 and return simply an error. Maybe this is not a template-id
14434 after all. */
14435 next_token_2->type = CPP_COLON;
14436 cp_parser_error (parser, "expected %<<%>");
14437 pop_deferring_access_checks ();
14438 return error_mark_node;
14440 /* Otherwise, emit an error about the invalid digraph, but continue
14441 parsing because we got our argument list. */
14442 if (permerror (next_token->location,
14443 "%<<::%> cannot begin a template-argument list"))
14445 static bool hint = false;
14446 inform (next_token->location,
14447 "%<<:%> is an alternate spelling for %<[%>."
14448 " Insert whitespace between %<<%> and %<::%>");
14449 if (!hint && !flag_permissive)
14451 inform (next_token->location, "(if you use %<-fpermissive%> "
14452 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14453 "accept your code)");
14454 hint = true;
14458 else
14460 /* Look for the `<' that starts the template-argument-list. */
14461 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14463 pop_deferring_access_checks ();
14464 return error_mark_node;
14466 /* Parse the arguments. */
14467 arguments = cp_parser_enclosed_template_argument_list (parser);
14470 /* Build a representation of the specialization. */
14471 if (identifier_p (templ))
14472 template_id = build_min_nt_loc (next_token->location,
14473 TEMPLATE_ID_EXPR,
14474 templ, arguments);
14475 else if (DECL_TYPE_TEMPLATE_P (templ)
14476 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14478 bool entering_scope;
14479 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14480 template (rather than some instantiation thereof) only if
14481 is not nested within some other construct. For example, in
14482 "template <typename T> void f(T) { A<T>::", A<T> is just an
14483 instantiation of A. */
14484 entering_scope = (template_parm_scope_p ()
14485 && cp_lexer_next_token_is (parser->lexer,
14486 CPP_SCOPE));
14487 template_id
14488 = finish_template_type (templ, arguments, entering_scope);
14490 /* A template-like identifier may be a partial concept id. */
14491 else if (flag_concepts
14492 && (template_id = (cp_parser_maybe_partial_concept_id
14493 (parser, templ, arguments))))
14494 return template_id;
14495 else if (variable_template_p (templ))
14497 template_id = lookup_template_variable (templ, arguments);
14498 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14499 SET_EXPR_LOCATION (template_id, next_token->location);
14501 else
14503 /* If it's not a class-template or a template-template, it should be
14504 a function-template. */
14505 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14506 || TREE_CODE (templ) == OVERLOAD
14507 || BASELINK_P (templ)));
14509 template_id = lookup_template_function (templ, arguments);
14510 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14511 SET_EXPR_LOCATION (template_id, next_token->location);
14514 /* If parsing tentatively, replace the sequence of tokens that makes
14515 up the template-id with a CPP_TEMPLATE_ID token. That way,
14516 should we re-parse the token stream, we will not have to repeat
14517 the effort required to do the parse, nor will we issue duplicate
14518 error messages about problems during instantiation of the
14519 template. */
14520 if (start_of_id
14521 /* Don't do this if we had a parse error in a declarator; re-parsing
14522 might succeed if a name changes meaning (60361). */
14523 && !(cp_parser_error_occurred (parser)
14524 && cp_parser_parsing_tentatively (parser)
14525 && parser->in_declarator_p))
14527 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14529 /* Reset the contents of the START_OF_ID token. */
14530 token->type = CPP_TEMPLATE_ID;
14531 /* Retrieve any deferred checks. Do not pop this access checks yet
14532 so the memory will not be reclaimed during token replacing below. */
14533 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14534 token->u.tree_check_value->value = template_id;
14535 token->u.tree_check_value->checks = get_deferred_access_checks ();
14536 token->keyword = RID_MAX;
14538 /* Purge all subsequent tokens. */
14539 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
14541 /* ??? Can we actually assume that, if template_id ==
14542 error_mark_node, we will have issued a diagnostic to the
14543 user, as opposed to simply marking the tentative parse as
14544 failed? */
14545 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
14546 error_at (token->location, "parse error in template argument list");
14549 pop_to_parent_deferring_access_checks ();
14550 return template_id;
14553 /* Parse a template-name.
14555 template-name:
14556 identifier
14558 The standard should actually say:
14560 template-name:
14561 identifier
14562 operator-function-id
14564 A defect report has been filed about this issue.
14566 A conversion-function-id cannot be a template name because they cannot
14567 be part of a template-id. In fact, looking at this code:
14569 a.operator K<int>()
14571 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
14572 It is impossible to call a templated conversion-function-id with an
14573 explicit argument list, since the only allowed template parameter is
14574 the type to which it is converting.
14576 If TEMPLATE_KEYWORD_P is true, then we have just seen the
14577 `template' keyword, in a construction like:
14579 T::template f<3>()
14581 In that case `f' is taken to be a template-name, even though there
14582 is no way of knowing for sure.
14584 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
14585 name refers to a set of overloaded functions, at least one of which
14586 is a template, or an IDENTIFIER_NODE with the name of the template,
14587 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
14588 names are looked up inside uninstantiated templates. */
14590 static tree
14591 cp_parser_template_name (cp_parser* parser,
14592 bool template_keyword_p,
14593 bool check_dependency_p,
14594 bool is_declaration,
14595 enum tag_types tag_type,
14596 bool *is_identifier)
14598 tree identifier;
14599 tree decl;
14600 tree fns;
14601 cp_token *token = cp_lexer_peek_token (parser->lexer);
14603 /* If the next token is `operator', then we have either an
14604 operator-function-id or a conversion-function-id. */
14605 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
14607 /* We don't know whether we're looking at an
14608 operator-function-id or a conversion-function-id. */
14609 cp_parser_parse_tentatively (parser);
14610 /* Try an operator-function-id. */
14611 identifier = cp_parser_operator_function_id (parser);
14612 /* If that didn't work, try a conversion-function-id. */
14613 if (!cp_parser_parse_definitely (parser))
14615 cp_parser_error (parser, "expected template-name");
14616 return error_mark_node;
14619 /* Look for the identifier. */
14620 else
14621 identifier = cp_parser_identifier (parser);
14623 /* If we didn't find an identifier, we don't have a template-id. */
14624 if (identifier == error_mark_node)
14625 return error_mark_node;
14627 /* If the name immediately followed the `template' keyword, then it
14628 is a template-name. However, if the next token is not `<', then
14629 we do not treat it as a template-name, since it is not being used
14630 as part of a template-id. This enables us to handle constructs
14631 like:
14633 template <typename T> struct S { S(); };
14634 template <typename T> S<T>::S();
14636 correctly. We would treat `S' as a template -- if it were `S<T>'
14637 -- but we do not if there is no `<'. */
14639 if (processing_template_decl
14640 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
14642 /* In a declaration, in a dependent context, we pretend that the
14643 "template" keyword was present in order to improve error
14644 recovery. For example, given:
14646 template <typename T> void f(T::X<int>);
14648 we want to treat "X<int>" as a template-id. */
14649 if (is_declaration
14650 && !template_keyword_p
14651 && parser->scope && TYPE_P (parser->scope)
14652 && check_dependency_p
14653 && dependent_scope_p (parser->scope)
14654 /* Do not do this for dtors (or ctors), since they never
14655 need the template keyword before their name. */
14656 && !constructor_name_p (identifier, parser->scope))
14658 cp_token_position start = 0;
14660 /* Explain what went wrong. */
14661 error_at (token->location, "non-template %qD used as template",
14662 identifier);
14663 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14664 parser->scope, identifier);
14665 /* If parsing tentatively, find the location of the "<" token. */
14666 if (cp_parser_simulate_error (parser))
14667 start = cp_lexer_token_position (parser->lexer, true);
14668 /* Parse the template arguments so that we can issue error
14669 messages about them. */
14670 cp_lexer_consume_token (parser->lexer);
14671 cp_parser_enclosed_template_argument_list (parser);
14672 /* Skip tokens until we find a good place from which to
14673 continue parsing. */
14674 cp_parser_skip_to_closing_parenthesis (parser,
14675 /*recovering=*/true,
14676 /*or_comma=*/true,
14677 /*consume_paren=*/false);
14678 /* If parsing tentatively, permanently remove the
14679 template argument list. That will prevent duplicate
14680 error messages from being issued about the missing
14681 "template" keyword. */
14682 if (start)
14683 cp_lexer_purge_tokens_after (parser->lexer, start);
14684 if (is_identifier)
14685 *is_identifier = true;
14686 return identifier;
14689 /* If the "template" keyword is present, then there is generally
14690 no point in doing name-lookup, so we just return IDENTIFIER.
14691 But, if the qualifying scope is non-dependent then we can
14692 (and must) do name-lookup normally. */
14693 if (template_keyword_p
14694 && (!parser->scope
14695 || (TYPE_P (parser->scope)
14696 && dependent_type_p (parser->scope))))
14697 return identifier;
14700 /* Look up the name. */
14701 decl = cp_parser_lookup_name (parser, identifier,
14702 tag_type,
14703 /*is_template=*/true,
14704 /*is_namespace=*/false,
14705 check_dependency_p,
14706 /*ambiguous_decls=*/NULL,
14707 token->location);
14709 decl = strip_using_decl (decl);
14711 /* If DECL is a template, then the name was a template-name. */
14712 if (TREE_CODE (decl) == TEMPLATE_DECL)
14714 if (TREE_DEPRECATED (decl)
14715 && deprecated_state != DEPRECATED_SUPPRESS)
14716 warn_deprecated_use (decl, NULL_TREE);
14718 else
14720 tree fn = NULL_TREE;
14722 /* The standard does not explicitly indicate whether a name that
14723 names a set of overloaded declarations, some of which are
14724 templates, is a template-name. However, such a name should
14725 be a template-name; otherwise, there is no way to form a
14726 template-id for the overloaded templates. */
14727 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14728 if (TREE_CODE (fns) == OVERLOAD)
14729 for (fn = fns; fn; fn = OVL_NEXT (fn))
14730 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14731 break;
14733 if (!fn)
14735 /* The name does not name a template. */
14736 cp_parser_error (parser, "expected template-name");
14737 return error_mark_node;
14741 /* If DECL is dependent, and refers to a function, then just return
14742 its name; we will look it up again during template instantiation. */
14743 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14745 tree scope = ovl_scope (decl);
14746 if (TYPE_P (scope) && dependent_type_p (scope))
14747 return identifier;
14750 return decl;
14753 /* Parse a template-argument-list.
14755 template-argument-list:
14756 template-argument ... [opt]
14757 template-argument-list , template-argument ... [opt]
14759 Returns a TREE_VEC containing the arguments. */
14761 static tree
14762 cp_parser_template_argument_list (cp_parser* parser)
14764 tree fixed_args[10];
14765 unsigned n_args = 0;
14766 unsigned alloced = 10;
14767 tree *arg_ary = fixed_args;
14768 tree vec;
14769 bool saved_in_template_argument_list_p;
14770 bool saved_ice_p;
14771 bool saved_non_ice_p;
14773 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14774 parser->in_template_argument_list_p = true;
14775 /* Even if the template-id appears in an integral
14776 constant-expression, the contents of the argument list do
14777 not. */
14778 saved_ice_p = parser->integral_constant_expression_p;
14779 parser->integral_constant_expression_p = false;
14780 saved_non_ice_p = parser->non_integral_constant_expression_p;
14781 parser->non_integral_constant_expression_p = false;
14783 /* Parse the arguments. */
14786 tree argument;
14788 if (n_args)
14789 /* Consume the comma. */
14790 cp_lexer_consume_token (parser->lexer);
14792 /* Parse the template-argument. */
14793 argument = cp_parser_template_argument (parser);
14795 /* If the next token is an ellipsis, we're expanding a template
14796 argument pack. */
14797 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14799 if (argument == error_mark_node)
14801 cp_token *token = cp_lexer_peek_token (parser->lexer);
14802 error_at (token->location,
14803 "expected parameter pack before %<...%>");
14805 /* Consume the `...' token. */
14806 cp_lexer_consume_token (parser->lexer);
14808 /* Make the argument into a TYPE_PACK_EXPANSION or
14809 EXPR_PACK_EXPANSION. */
14810 argument = make_pack_expansion (argument);
14813 if (n_args == alloced)
14815 alloced *= 2;
14817 if (arg_ary == fixed_args)
14819 arg_ary = XNEWVEC (tree, alloced);
14820 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14822 else
14823 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14825 arg_ary[n_args++] = argument;
14827 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14829 vec = make_tree_vec (n_args);
14831 while (n_args--)
14832 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14834 if (arg_ary != fixed_args)
14835 free (arg_ary);
14836 parser->non_integral_constant_expression_p = saved_non_ice_p;
14837 parser->integral_constant_expression_p = saved_ice_p;
14838 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14839 if (CHECKING_P)
14840 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14841 return vec;
14844 /* Parse a template-argument.
14846 template-argument:
14847 assignment-expression
14848 type-id
14849 id-expression
14851 The representation is that of an assignment-expression, type-id, or
14852 id-expression -- except that the qualified id-expression is
14853 evaluated, so that the value returned is either a DECL or an
14854 OVERLOAD.
14856 Although the standard says "assignment-expression", it forbids
14857 throw-expressions or assignments in the template argument.
14858 Therefore, we use "conditional-expression" instead. */
14860 static tree
14861 cp_parser_template_argument (cp_parser* parser)
14863 tree argument;
14864 bool template_p;
14865 bool address_p;
14866 bool maybe_type_id = false;
14867 cp_token *token = NULL, *argument_start_token = NULL;
14868 location_t loc = 0;
14869 cp_id_kind idk;
14871 /* There's really no way to know what we're looking at, so we just
14872 try each alternative in order.
14874 [temp.arg]
14876 In a template-argument, an ambiguity between a type-id and an
14877 expression is resolved to a type-id, regardless of the form of
14878 the corresponding template-parameter.
14880 Therefore, we try a type-id first. */
14881 cp_parser_parse_tentatively (parser);
14882 argument = cp_parser_template_type_arg (parser);
14883 /* If there was no error parsing the type-id but the next token is a
14884 '>>', our behavior depends on which dialect of C++ we're
14885 parsing. In C++98, we probably found a typo for '> >'. But there
14886 are type-id which are also valid expressions. For instance:
14888 struct X { int operator >> (int); };
14889 template <int V> struct Foo {};
14890 Foo<X () >> 5> r;
14892 Here 'X()' is a valid type-id of a function type, but the user just
14893 wanted to write the expression "X() >> 5". Thus, we remember that we
14894 found a valid type-id, but we still try to parse the argument as an
14895 expression to see what happens.
14897 In C++0x, the '>>' will be considered two separate '>'
14898 tokens. */
14899 if (!cp_parser_error_occurred (parser)
14900 && cxx_dialect == cxx98
14901 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14903 maybe_type_id = true;
14904 cp_parser_abort_tentative_parse (parser);
14906 else
14908 /* If the next token isn't a `,' or a `>', then this argument wasn't
14909 really finished. This means that the argument is not a valid
14910 type-id. */
14911 if (!cp_parser_next_token_ends_template_argument_p (parser))
14912 cp_parser_error (parser, "expected template-argument");
14913 /* If that worked, we're done. */
14914 if (cp_parser_parse_definitely (parser))
14915 return argument;
14917 /* We're still not sure what the argument will be. */
14918 cp_parser_parse_tentatively (parser);
14919 /* Try a template. */
14920 argument_start_token = cp_lexer_peek_token (parser->lexer);
14921 argument = cp_parser_id_expression (parser,
14922 /*template_keyword_p=*/false,
14923 /*check_dependency_p=*/true,
14924 &template_p,
14925 /*declarator_p=*/false,
14926 /*optional_p=*/false);
14927 /* If the next token isn't a `,' or a `>', then this argument wasn't
14928 really finished. */
14929 if (!cp_parser_next_token_ends_template_argument_p (parser))
14930 cp_parser_error (parser, "expected template-argument");
14931 if (!cp_parser_error_occurred (parser))
14933 /* Figure out what is being referred to. If the id-expression
14934 was for a class template specialization, then we will have a
14935 TYPE_DECL at this point. There is no need to do name lookup
14936 at this point in that case. */
14937 if (TREE_CODE (argument) != TYPE_DECL)
14938 argument = cp_parser_lookup_name (parser, argument,
14939 none_type,
14940 /*is_template=*/template_p,
14941 /*is_namespace=*/false,
14942 /*check_dependency=*/true,
14943 /*ambiguous_decls=*/NULL,
14944 argument_start_token->location);
14945 /* Handle a constrained-type-specifier for a non-type template
14946 parameter. */
14947 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
14948 argument = decl;
14949 else if (TREE_CODE (argument) != TEMPLATE_DECL
14950 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14951 cp_parser_error (parser, "expected template-name");
14953 if (cp_parser_parse_definitely (parser))
14955 if (TREE_DEPRECATED (argument))
14956 warn_deprecated_use (argument, NULL_TREE);
14957 return argument;
14959 /* It must be a non-type argument. In C++17 any constant-expression is
14960 allowed. */
14961 if (cxx_dialect > cxx14)
14962 goto general_expr;
14964 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
14966 -- an integral constant-expression of integral or enumeration
14967 type; or
14969 -- the name of a non-type template-parameter; or
14971 -- the name of an object or function with external linkage...
14973 -- the address of an object or function with external linkage...
14975 -- a pointer to member... */
14976 /* Look for a non-type template parameter. */
14977 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14979 cp_parser_parse_tentatively (parser);
14980 argument = cp_parser_primary_expression (parser,
14981 /*address_p=*/false,
14982 /*cast_p=*/false,
14983 /*template_arg_p=*/true,
14984 &idk);
14985 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14986 || !cp_parser_next_token_ends_template_argument_p (parser))
14987 cp_parser_simulate_error (parser);
14988 if (cp_parser_parse_definitely (parser))
14989 return argument;
14992 /* If the next token is "&", the argument must be the address of an
14993 object or function with external linkage. */
14994 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14995 if (address_p)
14997 loc = cp_lexer_peek_token (parser->lexer)->location;
14998 cp_lexer_consume_token (parser->lexer);
15000 /* See if we might have an id-expression. */
15001 token = cp_lexer_peek_token (parser->lexer);
15002 if (token->type == CPP_NAME
15003 || token->keyword == RID_OPERATOR
15004 || token->type == CPP_SCOPE
15005 || token->type == CPP_TEMPLATE_ID
15006 || token->type == CPP_NESTED_NAME_SPECIFIER)
15008 cp_parser_parse_tentatively (parser);
15009 argument = cp_parser_primary_expression (parser,
15010 address_p,
15011 /*cast_p=*/false,
15012 /*template_arg_p=*/true,
15013 &idk);
15014 if (cp_parser_error_occurred (parser)
15015 || !cp_parser_next_token_ends_template_argument_p (parser))
15016 cp_parser_abort_tentative_parse (parser);
15017 else
15019 tree probe;
15021 if (INDIRECT_REF_P (argument))
15023 /* Strip the dereference temporarily. */
15024 gcc_assert (REFERENCE_REF_P (argument));
15025 argument = TREE_OPERAND (argument, 0);
15028 /* If we're in a template, we represent a qualified-id referring
15029 to a static data member as a SCOPE_REF even if the scope isn't
15030 dependent so that we can check access control later. */
15031 probe = argument;
15032 if (TREE_CODE (probe) == SCOPE_REF)
15033 probe = TREE_OPERAND (probe, 1);
15034 if (VAR_P (probe))
15036 /* A variable without external linkage might still be a
15037 valid constant-expression, so no error is issued here
15038 if the external-linkage check fails. */
15039 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15040 cp_parser_simulate_error (parser);
15042 else if (is_overloaded_fn (argument))
15043 /* All overloaded functions are allowed; if the external
15044 linkage test does not pass, an error will be issued
15045 later. */
15047 else if (address_p
15048 && (TREE_CODE (argument) == OFFSET_REF
15049 || TREE_CODE (argument) == SCOPE_REF))
15050 /* A pointer-to-member. */
15052 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15054 else
15055 cp_parser_simulate_error (parser);
15057 if (cp_parser_parse_definitely (parser))
15059 if (address_p)
15060 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15061 tf_warning_or_error);
15062 else
15063 argument = convert_from_reference (argument);
15064 return argument;
15068 /* If the argument started with "&", there are no other valid
15069 alternatives at this point. */
15070 if (address_p)
15072 cp_parser_error (parser, "invalid non-type template argument");
15073 return error_mark_node;
15076 general_expr:
15077 /* If the argument wasn't successfully parsed as a type-id followed
15078 by '>>', the argument can only be a constant expression now.
15079 Otherwise, we try parsing the constant-expression tentatively,
15080 because the argument could really be a type-id. */
15081 if (maybe_type_id)
15082 cp_parser_parse_tentatively (parser);
15083 argument = cp_parser_constant_expression (parser);
15085 if (!maybe_type_id)
15086 return argument;
15087 if (!cp_parser_next_token_ends_template_argument_p (parser))
15088 cp_parser_error (parser, "expected template-argument");
15089 if (cp_parser_parse_definitely (parser))
15090 return argument;
15091 /* We did our best to parse the argument as a non type-id, but that
15092 was the only alternative that matched (albeit with a '>' after
15093 it). We can assume it's just a typo from the user, and a
15094 diagnostic will then be issued. */
15095 return cp_parser_template_type_arg (parser);
15098 /* Parse an explicit-instantiation.
15100 explicit-instantiation:
15101 template declaration
15103 Although the standard says `declaration', what it really means is:
15105 explicit-instantiation:
15106 template decl-specifier-seq [opt] declarator [opt] ;
15108 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15109 supposed to be allowed. A defect report has been filed about this
15110 issue.
15112 GNU Extension:
15114 explicit-instantiation:
15115 storage-class-specifier template
15116 decl-specifier-seq [opt] declarator [opt] ;
15117 function-specifier template
15118 decl-specifier-seq [opt] declarator [opt] ; */
15120 static void
15121 cp_parser_explicit_instantiation (cp_parser* parser)
15123 int declares_class_or_enum;
15124 cp_decl_specifier_seq decl_specifiers;
15125 tree extension_specifier = NULL_TREE;
15127 timevar_push (TV_TEMPLATE_INST);
15129 /* Look for an (optional) storage-class-specifier or
15130 function-specifier. */
15131 if (cp_parser_allow_gnu_extensions_p (parser))
15133 extension_specifier
15134 = cp_parser_storage_class_specifier_opt (parser);
15135 if (!extension_specifier)
15136 extension_specifier
15137 = cp_parser_function_specifier_opt (parser,
15138 /*decl_specs=*/NULL);
15141 /* Look for the `template' keyword. */
15142 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15143 /* Let the front end know that we are processing an explicit
15144 instantiation. */
15145 begin_explicit_instantiation ();
15146 /* [temp.explicit] says that we are supposed to ignore access
15147 control while processing explicit instantiation directives. */
15148 push_deferring_access_checks (dk_no_check);
15149 /* Parse a decl-specifier-seq. */
15150 cp_parser_decl_specifier_seq (parser,
15151 CP_PARSER_FLAGS_OPTIONAL,
15152 &decl_specifiers,
15153 &declares_class_or_enum);
15154 /* If there was exactly one decl-specifier, and it declared a class,
15155 and there's no declarator, then we have an explicit type
15156 instantiation. */
15157 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15159 tree type;
15161 type = check_tag_decl (&decl_specifiers,
15162 /*explicit_type_instantiation_p=*/true);
15163 /* Turn access control back on for names used during
15164 template instantiation. */
15165 pop_deferring_access_checks ();
15166 if (type)
15167 do_type_instantiation (type, extension_specifier,
15168 /*complain=*/tf_error);
15170 else
15172 cp_declarator *declarator;
15173 tree decl;
15175 /* Parse the declarator. */
15176 declarator
15177 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15178 /*ctor_dtor_or_conv_p=*/NULL,
15179 /*parenthesized_p=*/NULL,
15180 /*member_p=*/false,
15181 /*friend_p=*/false);
15182 if (declares_class_or_enum & 2)
15183 cp_parser_check_for_definition_in_return_type (declarator,
15184 decl_specifiers.type,
15185 decl_specifiers.locations[ds_type_spec]);
15186 if (declarator != cp_error_declarator)
15188 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15189 permerror (decl_specifiers.locations[ds_inline],
15190 "explicit instantiation shall not use"
15191 " %<inline%> specifier");
15192 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15193 permerror (decl_specifiers.locations[ds_constexpr],
15194 "explicit instantiation shall not use"
15195 " %<constexpr%> specifier");
15197 decl = grokdeclarator (declarator, &decl_specifiers,
15198 NORMAL, 0, &decl_specifiers.attributes);
15199 /* Turn access control back on for names used during
15200 template instantiation. */
15201 pop_deferring_access_checks ();
15202 /* Do the explicit instantiation. */
15203 do_decl_instantiation (decl, extension_specifier);
15205 else
15207 pop_deferring_access_checks ();
15208 /* Skip the body of the explicit instantiation. */
15209 cp_parser_skip_to_end_of_statement (parser);
15212 /* We're done with the instantiation. */
15213 end_explicit_instantiation ();
15215 cp_parser_consume_semicolon_at_end_of_statement (parser);
15217 timevar_pop (TV_TEMPLATE_INST);
15220 /* Parse an explicit-specialization.
15222 explicit-specialization:
15223 template < > declaration
15225 Although the standard says `declaration', what it really means is:
15227 explicit-specialization:
15228 template <> decl-specifier [opt] init-declarator [opt] ;
15229 template <> function-definition
15230 template <> explicit-specialization
15231 template <> template-declaration */
15233 static void
15234 cp_parser_explicit_specialization (cp_parser* parser)
15236 bool need_lang_pop;
15237 cp_token *token = cp_lexer_peek_token (parser->lexer);
15239 /* Look for the `template' keyword. */
15240 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15241 /* Look for the `<'. */
15242 cp_parser_require (parser, CPP_LESS, RT_LESS);
15243 /* Look for the `>'. */
15244 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15245 /* We have processed another parameter list. */
15246 ++parser->num_template_parameter_lists;
15247 /* [temp]
15249 A template ... explicit specialization ... shall not have C
15250 linkage. */
15251 if (current_lang_name == lang_name_c)
15253 error_at (token->location, "template specialization with C linkage");
15254 /* Give it C++ linkage to avoid confusing other parts of the
15255 front end. */
15256 push_lang_context (lang_name_cplusplus);
15257 need_lang_pop = true;
15259 else
15260 need_lang_pop = false;
15261 /* Let the front end know that we are beginning a specialization. */
15262 if (!begin_specialization ())
15264 end_specialization ();
15265 return;
15268 /* If the next keyword is `template', we need to figure out whether
15269 or not we're looking a template-declaration. */
15270 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15272 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15273 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15274 cp_parser_template_declaration_after_export (parser,
15275 /*member_p=*/false);
15276 else
15277 cp_parser_explicit_specialization (parser);
15279 else
15280 /* Parse the dependent declaration. */
15281 cp_parser_single_declaration (parser,
15282 /*checks=*/NULL,
15283 /*member_p=*/false,
15284 /*explicit_specialization_p=*/true,
15285 /*friend_p=*/NULL);
15286 /* We're done with the specialization. */
15287 end_specialization ();
15288 /* For the erroneous case of a template with C linkage, we pushed an
15289 implicit C++ linkage scope; exit that scope now. */
15290 if (need_lang_pop)
15291 pop_lang_context ();
15292 /* We're done with this parameter list. */
15293 --parser->num_template_parameter_lists;
15296 /* Parse a type-specifier.
15298 type-specifier:
15299 simple-type-specifier
15300 class-specifier
15301 enum-specifier
15302 elaborated-type-specifier
15303 cv-qualifier
15305 GNU Extension:
15307 type-specifier:
15308 __complex__
15310 Returns a representation of the type-specifier. For a
15311 class-specifier, enum-specifier, or elaborated-type-specifier, a
15312 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15314 The parser flags FLAGS is used to control type-specifier parsing.
15316 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15317 in a decl-specifier-seq.
15319 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15320 class-specifier, enum-specifier, or elaborated-type-specifier, then
15321 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15322 if a type is declared; 2 if it is defined. Otherwise, it is set to
15323 zero.
15325 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15326 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15327 is set to FALSE. */
15329 static tree
15330 cp_parser_type_specifier (cp_parser* parser,
15331 cp_parser_flags flags,
15332 cp_decl_specifier_seq *decl_specs,
15333 bool is_declaration,
15334 int* declares_class_or_enum,
15335 bool* is_cv_qualifier)
15337 tree type_spec = NULL_TREE;
15338 cp_token *token;
15339 enum rid keyword;
15340 cp_decl_spec ds = ds_last;
15342 /* Assume this type-specifier does not declare a new type. */
15343 if (declares_class_or_enum)
15344 *declares_class_or_enum = 0;
15345 /* And that it does not specify a cv-qualifier. */
15346 if (is_cv_qualifier)
15347 *is_cv_qualifier = false;
15348 /* Peek at the next token. */
15349 token = cp_lexer_peek_token (parser->lexer);
15351 /* If we're looking at a keyword, we can use that to guide the
15352 production we choose. */
15353 keyword = token->keyword;
15354 switch (keyword)
15356 case RID_ENUM:
15357 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15358 goto elaborated_type_specifier;
15360 /* Look for the enum-specifier. */
15361 type_spec = cp_parser_enum_specifier (parser);
15362 /* If that worked, we're done. */
15363 if (type_spec)
15365 if (declares_class_or_enum)
15366 *declares_class_or_enum = 2;
15367 if (decl_specs)
15368 cp_parser_set_decl_spec_type (decl_specs,
15369 type_spec,
15370 token,
15371 /*type_definition_p=*/true);
15372 return type_spec;
15374 else
15375 goto elaborated_type_specifier;
15377 /* Any of these indicate either a class-specifier, or an
15378 elaborated-type-specifier. */
15379 case RID_CLASS:
15380 case RID_STRUCT:
15381 case RID_UNION:
15382 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15383 goto elaborated_type_specifier;
15385 /* Parse tentatively so that we can back up if we don't find a
15386 class-specifier. */
15387 cp_parser_parse_tentatively (parser);
15388 /* Look for the class-specifier. */
15389 type_spec = cp_parser_class_specifier (parser);
15390 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15391 /* If that worked, we're done. */
15392 if (cp_parser_parse_definitely (parser))
15394 if (declares_class_or_enum)
15395 *declares_class_or_enum = 2;
15396 if (decl_specs)
15397 cp_parser_set_decl_spec_type (decl_specs,
15398 type_spec,
15399 token,
15400 /*type_definition_p=*/true);
15401 return type_spec;
15404 /* Fall through. */
15405 elaborated_type_specifier:
15406 /* We're declaring (not defining) a class or enum. */
15407 if (declares_class_or_enum)
15408 *declares_class_or_enum = 1;
15410 /* Fall through. */
15411 case RID_TYPENAME:
15412 /* Look for an elaborated-type-specifier. */
15413 type_spec
15414 = (cp_parser_elaborated_type_specifier
15415 (parser,
15416 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15417 is_declaration));
15418 if (decl_specs)
15419 cp_parser_set_decl_spec_type (decl_specs,
15420 type_spec,
15421 token,
15422 /*type_definition_p=*/false);
15423 return type_spec;
15425 case RID_CONST:
15426 ds = ds_const;
15427 if (is_cv_qualifier)
15428 *is_cv_qualifier = true;
15429 break;
15431 case RID_VOLATILE:
15432 ds = ds_volatile;
15433 if (is_cv_qualifier)
15434 *is_cv_qualifier = true;
15435 break;
15437 case RID_RESTRICT:
15438 ds = ds_restrict;
15439 if (is_cv_qualifier)
15440 *is_cv_qualifier = true;
15441 break;
15443 case RID_COMPLEX:
15444 /* The `__complex__' keyword is a GNU extension. */
15445 ds = ds_complex;
15446 break;
15448 default:
15449 break;
15452 /* Handle simple keywords. */
15453 if (ds != ds_last)
15455 if (decl_specs)
15457 set_and_check_decl_spec_loc (decl_specs, ds, token);
15458 decl_specs->any_specifiers_p = true;
15460 return cp_lexer_consume_token (parser->lexer)->u.value;
15463 /* If we do not already have a type-specifier, assume we are looking
15464 at a simple-type-specifier. */
15465 type_spec = cp_parser_simple_type_specifier (parser,
15466 decl_specs,
15467 flags);
15469 /* If we didn't find a type-specifier, and a type-specifier was not
15470 optional in this context, issue an error message. */
15471 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15473 cp_parser_error (parser, "expected type specifier");
15474 return error_mark_node;
15477 return type_spec;
15480 /* Parse a simple-type-specifier.
15482 simple-type-specifier:
15483 :: [opt] nested-name-specifier [opt] type-name
15484 :: [opt] nested-name-specifier template template-id
15485 char
15486 wchar_t
15487 bool
15488 short
15490 long
15491 signed
15492 unsigned
15493 float
15494 double
15495 void
15497 C++0x Extension:
15499 simple-type-specifier:
15500 auto
15501 decltype ( expression )
15502 char16_t
15503 char32_t
15504 __underlying_type ( type-id )
15506 GNU Extension:
15508 simple-type-specifier:
15509 __int128
15510 __typeof__ unary-expression
15511 __typeof__ ( type-id )
15512 __typeof__ ( type-id ) { initializer-list , [opt] }
15514 Concepts Extension:
15516 simple-type-specifier:
15517 constrained-type-specifier
15519 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15520 appropriately updated. */
15522 static tree
15523 cp_parser_simple_type_specifier (cp_parser* parser,
15524 cp_decl_specifier_seq *decl_specs,
15525 cp_parser_flags flags)
15527 tree type = NULL_TREE;
15528 cp_token *token;
15529 int idx;
15531 /* Peek at the next token. */
15532 token = cp_lexer_peek_token (parser->lexer);
15534 /* If we're looking at a keyword, things are easy. */
15535 switch (token->keyword)
15537 case RID_CHAR:
15538 if (decl_specs)
15539 decl_specs->explicit_char_p = true;
15540 type = char_type_node;
15541 break;
15542 case RID_CHAR16:
15543 type = char16_type_node;
15544 break;
15545 case RID_CHAR32:
15546 type = char32_type_node;
15547 break;
15548 case RID_WCHAR:
15549 type = wchar_type_node;
15550 break;
15551 case RID_BOOL:
15552 type = boolean_type_node;
15553 break;
15554 case RID_SHORT:
15555 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
15556 type = short_integer_type_node;
15557 break;
15558 case RID_INT:
15559 if (decl_specs)
15560 decl_specs->explicit_int_p = true;
15561 type = integer_type_node;
15562 break;
15563 case RID_INT_N_0:
15564 case RID_INT_N_1:
15565 case RID_INT_N_2:
15566 case RID_INT_N_3:
15567 idx = token->keyword - RID_INT_N_0;
15568 if (! int_n_enabled_p [idx])
15569 break;
15570 if (decl_specs)
15572 decl_specs->explicit_intN_p = true;
15573 decl_specs->int_n_idx = idx;
15575 type = int_n_trees [idx].signed_type;
15576 break;
15577 case RID_LONG:
15578 if (decl_specs)
15579 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
15580 type = long_integer_type_node;
15581 break;
15582 case RID_SIGNED:
15583 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
15584 type = integer_type_node;
15585 break;
15586 case RID_UNSIGNED:
15587 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
15588 type = unsigned_type_node;
15589 break;
15590 case RID_FLOAT:
15591 type = float_type_node;
15592 break;
15593 case RID_DOUBLE:
15594 type = double_type_node;
15595 break;
15596 case RID_VOID:
15597 type = void_type_node;
15598 break;
15600 case RID_AUTO:
15601 maybe_warn_cpp0x (CPP0X_AUTO);
15602 if (parser->auto_is_implicit_function_template_parm_p)
15604 /* The 'auto' might be the placeholder return type for a function decl
15605 with trailing return type. */
15606 bool have_trailing_return_fn_decl = false;
15607 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15608 == CPP_OPEN_PAREN)
15610 cp_parser_parse_tentatively (parser);
15611 cp_lexer_consume_token (parser->lexer);
15612 cp_lexer_consume_token (parser->lexer);
15613 if (cp_parser_skip_to_closing_parenthesis (parser,
15614 /*recovering*/false,
15615 /*or_comma*/false,
15616 /*consume_paren*/true))
15617 have_trailing_return_fn_decl
15618 = cp_lexer_next_token_is (parser->lexer, CPP_DEREF);
15619 cp_parser_abort_tentative_parse (parser);
15622 if (have_trailing_return_fn_decl)
15624 type = make_auto ();
15625 break;
15628 if (cxx_dialect >= cxx14)
15630 type = synthesize_implicit_template_parm (parser, NULL_TREE);
15631 type = TREE_TYPE (type);
15633 else
15634 type = error_mark_node;
15636 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
15638 if (cxx_dialect < cxx14)
15639 error_at (token->location,
15640 "use of %<auto%> in lambda parameter declaration "
15641 "only available with "
15642 "-std=c++14 or -std=gnu++14");
15644 else if (cxx_dialect < cxx14)
15645 error_at (token->location,
15646 "use of %<auto%> in parameter declaration "
15647 "only available with "
15648 "-std=c++14 or -std=gnu++14");
15649 else
15650 pedwarn (token->location, OPT_Wpedantic,
15651 "ISO C++ forbids use of %<auto%> in parameter "
15652 "declaration");
15654 else
15655 type = make_auto ();
15656 break;
15658 case RID_DECLTYPE:
15659 /* Since DR 743, decltype can either be a simple-type-specifier by
15660 itself or begin a nested-name-specifier. Parsing it will replace
15661 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
15662 handling below decide what to do. */
15663 cp_parser_decltype (parser);
15664 cp_lexer_set_token_position (parser->lexer, token);
15665 break;
15667 case RID_TYPEOF:
15668 /* Consume the `typeof' token. */
15669 cp_lexer_consume_token (parser->lexer);
15670 /* Parse the operand to `typeof'. */
15671 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
15672 /* If it is not already a TYPE, take its type. */
15673 if (!TYPE_P (type))
15674 type = finish_typeof (type);
15676 if (decl_specs)
15677 cp_parser_set_decl_spec_type (decl_specs, type,
15678 token,
15679 /*type_definition_p=*/false);
15681 return type;
15683 case RID_UNDERLYING_TYPE:
15684 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
15685 if (decl_specs)
15686 cp_parser_set_decl_spec_type (decl_specs, type,
15687 token,
15688 /*type_definition_p=*/false);
15690 return type;
15692 case RID_BASES:
15693 case RID_DIRECT_BASES:
15694 type = cp_parser_trait_expr (parser, token->keyword);
15695 if (decl_specs)
15696 cp_parser_set_decl_spec_type (decl_specs, type,
15697 token,
15698 /*type_definition_p=*/false);
15699 return type;
15700 default:
15701 break;
15704 /* If token is an already-parsed decltype not followed by ::,
15705 it's a simple-type-specifier. */
15706 if (token->type == CPP_DECLTYPE
15707 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15709 type = token->u.value;
15710 if (decl_specs)
15712 cp_parser_set_decl_spec_type (decl_specs, type,
15713 token,
15714 /*type_definition_p=*/false);
15715 /* Remember that we are handling a decltype in order to
15716 implement the resolution of DR 1510 when the argument
15717 isn't instantiation dependent. */
15718 decl_specs->decltype_p = true;
15720 cp_lexer_consume_token (parser->lexer);
15721 return type;
15724 /* If the type-specifier was for a built-in type, we're done. */
15725 if (type)
15727 /* Record the type. */
15728 if (decl_specs
15729 && (token->keyword != RID_SIGNED
15730 && token->keyword != RID_UNSIGNED
15731 && token->keyword != RID_SHORT
15732 && token->keyword != RID_LONG))
15733 cp_parser_set_decl_spec_type (decl_specs,
15734 type,
15735 token,
15736 /*type_definition_p=*/false);
15737 if (decl_specs)
15738 decl_specs->any_specifiers_p = true;
15740 /* Consume the token. */
15741 cp_lexer_consume_token (parser->lexer);
15743 if (type == error_mark_node)
15744 return error_mark_node;
15746 /* There is no valid C++ program where a non-template type is
15747 followed by a "<". That usually indicates that the user thought
15748 that the type was a template. */
15749 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15750 token->location);
15752 return TYPE_NAME (type);
15755 /* The type-specifier must be a user-defined type. */
15756 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15758 bool qualified_p;
15759 bool global_p;
15761 /* Don't gobble tokens or issue error messages if this is an
15762 optional type-specifier. */
15763 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15764 cp_parser_parse_tentatively (parser);
15766 /* Look for the optional `::' operator. */
15767 global_p
15768 = (cp_parser_global_scope_opt (parser,
15769 /*current_scope_valid_p=*/false)
15770 != NULL_TREE);
15771 /* Look for the nested-name specifier. */
15772 qualified_p
15773 = (cp_parser_nested_name_specifier_opt (parser,
15774 /*typename_keyword_p=*/false,
15775 /*check_dependency_p=*/true,
15776 /*type_p=*/false,
15777 /*is_declaration=*/false)
15778 != NULL_TREE);
15779 token = cp_lexer_peek_token (parser->lexer);
15780 /* If we have seen a nested-name-specifier, and the next token
15781 is `template', then we are using the template-id production. */
15782 if (parser->scope
15783 && cp_parser_optional_template_keyword (parser))
15785 /* Look for the template-id. */
15786 type = cp_parser_template_id (parser,
15787 /*template_keyword_p=*/true,
15788 /*check_dependency_p=*/true,
15789 none_type,
15790 /*is_declaration=*/false);
15791 /* If the template-id did not name a type, we are out of
15792 luck. */
15793 if (TREE_CODE (type) != TYPE_DECL)
15795 cp_parser_error (parser, "expected template-id for type");
15796 type = NULL_TREE;
15799 /* Otherwise, look for a type-name. */
15800 else
15801 type = cp_parser_type_name (parser);
15802 /* Keep track of all name-lookups performed in class scopes. */
15803 if (type
15804 && !global_p
15805 && !qualified_p
15806 && TREE_CODE (type) == TYPE_DECL
15807 && identifier_p (DECL_NAME (type)))
15808 maybe_note_name_used_in_class (DECL_NAME (type), type);
15809 /* If it didn't work out, we don't have a TYPE. */
15810 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15811 && !cp_parser_parse_definitely (parser))
15812 type = NULL_TREE;
15813 if (type && decl_specs)
15814 cp_parser_set_decl_spec_type (decl_specs, type,
15815 token,
15816 /*type_definition_p=*/false);
15819 /* If we didn't get a type-name, issue an error message. */
15820 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15822 cp_parser_error (parser, "expected type-name");
15823 return error_mark_node;
15826 if (type && type != error_mark_node)
15828 /* See if TYPE is an Objective-C type, and if so, parse and
15829 accept any protocol references following it. Do this before
15830 the cp_parser_check_for_invalid_template_id() call, because
15831 Objective-C types can be followed by '<...>' which would
15832 enclose protocol names rather than template arguments, and so
15833 everything is fine. */
15834 if (c_dialect_objc () && !parser->scope
15835 && (objc_is_id (type) || objc_is_class_name (type)))
15837 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15838 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15840 /* Clobber the "unqualified" type previously entered into
15841 DECL_SPECS with the new, improved protocol-qualified version. */
15842 if (decl_specs)
15843 decl_specs->type = qual_type;
15845 return qual_type;
15848 /* There is no valid C++ program where a non-template type is
15849 followed by a "<". That usually indicates that the user
15850 thought that the type was a template. */
15851 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15852 none_type,
15853 token->location);
15856 return type;
15859 /* Parse a type-name.
15861 type-name:
15862 class-name
15863 enum-name
15864 typedef-name
15865 simple-template-id [in c++0x]
15867 enum-name:
15868 identifier
15870 typedef-name:
15871 identifier
15873 Concepts:
15875 type-name:
15876 concept-name
15877 partial-concept-id
15879 concept-name:
15880 identifier
15882 Returns a TYPE_DECL for the type. */
15884 static tree
15885 cp_parser_type_name (cp_parser* parser)
15887 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
15890 /* See above. */
15891 static tree
15892 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
15894 tree type_decl;
15896 /* We can't know yet whether it is a class-name or not. */
15897 cp_parser_parse_tentatively (parser);
15898 /* Try a class-name. */
15899 type_decl = cp_parser_class_name (parser,
15900 typename_keyword_p,
15901 /*template_keyword_p=*/false,
15902 none_type,
15903 /*check_dependency_p=*/true,
15904 /*class_head_p=*/false,
15905 /*is_declaration=*/false);
15906 /* If it's not a class-name, keep looking. */
15907 if (!cp_parser_parse_definitely (parser))
15909 if (cxx_dialect < cxx11)
15910 /* It must be a typedef-name or an enum-name. */
15911 return cp_parser_nonclass_name (parser);
15913 cp_parser_parse_tentatively (parser);
15914 /* It is either a simple-template-id representing an
15915 instantiation of an alias template... */
15916 type_decl = cp_parser_template_id (parser,
15917 /*template_keyword_p=*/false,
15918 /*check_dependency_p=*/true,
15919 none_type,
15920 /*is_declaration=*/false);
15921 /* Note that this must be an instantiation of an alias template
15922 because [temp.names]/6 says:
15924 A template-id that names an alias template specialization
15925 is a type-name.
15927 Whereas [temp.names]/7 says:
15929 A simple-template-id that names a class template
15930 specialization is a class-name.
15932 With concepts, this could also be a partial-concept-id that
15933 declares a non-type template parameter. */
15934 if (type_decl != NULL_TREE
15935 && TREE_CODE (type_decl) == TYPE_DECL
15936 && TYPE_DECL_ALIAS_P (type_decl))
15937 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15938 else if (is_constrained_parameter (type_decl))
15939 /* Don't do anything. */ ;
15940 else
15941 cp_parser_simulate_error (parser);
15943 if (!cp_parser_parse_definitely (parser))
15944 /* ... Or a typedef-name or an enum-name. */
15945 return cp_parser_nonclass_name (parser);
15948 return type_decl;
15951 /* Check if DECL and ARGS can form a constrained-type-specifier.
15952 If ARGS is non-null, we try to form a concept check of the
15953 form DECL<?, ARGS> where ? is a wildcard that matches any
15954 kind of template argument. If ARGS is NULL, then we try to
15955 form a concept check of the form DECL<?>. */
15957 static tree
15958 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
15959 tree decl, tree args)
15961 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
15963 /* If we a constrained-type-specifier cannot be deduced. */
15964 if (parser->prevent_constrained_type_specifiers)
15965 return NULL_TREE;
15967 /* A constrained type specifier can only be found in an
15968 overload set or as a reference to a template declaration.
15970 FIXME: This might be masking a bug. It's possible that
15971 that the deduction below is causing template specializations
15972 to be formed with the wildcard as an argument. */
15973 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
15974 return NULL_TREE;
15976 /* Try to build a call expression that evaluates the
15977 concept. This can fail if the overload set refers
15978 only to non-templates. */
15979 tree placeholder = build_nt (WILDCARD_DECL);
15980 tree check = build_concept_check (decl, placeholder, args);
15981 if (check == error_mark_node)
15982 return NULL_TREE;
15984 /* Deduce the checked constraint and the prototype parameter.
15986 FIXME: In certain cases, failure to deduce should be a
15987 diagnosable error. */
15988 tree conc;
15989 tree proto;
15990 if (!deduce_constrained_parameter (check, conc, proto))
15991 return NULL_TREE;
15993 /* In template parameter scope, this results in a constrained
15994 parameter. Return a descriptor of that parm. */
15995 if (processing_template_parmlist)
15996 return build_constrained_parameter (conc, proto, args);
15998 /* In a parameter-declaration-clause, constrained-type
15999 specifiers result in invented template parameters. */
16000 if (parser->auto_is_implicit_function_template_parm_p)
16002 tree x = build_constrained_parameter (conc, proto, args);
16003 return synthesize_implicit_template_parm (parser, x);
16005 else
16007 /* Otherwise, we're in a context where the constrained
16008 type name is deduced and the constraint applies
16009 after deduction. */
16010 return make_constrained_auto (conc, args);
16013 return NULL_TREE;
16016 /* If DECL refers to a concept, return a TYPE_DECL representing
16017 the result of using the constrained type specifier in the
16018 current context. DECL refers to a concept if
16020 - it is an overload set containing a function concept taking a single
16021 type argument, or
16023 - it is a variable concept taking a single type argument. */
16025 static tree
16026 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16028 if (flag_concepts
16029 && (TREE_CODE (decl) == OVERLOAD
16030 || BASELINK_P (decl)
16031 || variable_concept_p (decl)))
16032 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16033 else
16034 return NULL_TREE;
16037 /* Check if DECL and ARGS form a partial-concept-id. If so,
16038 assign ID to the resulting constrained placeholder.
16040 Returns true if the partial-concept-id designates a placeholder
16041 and false otherwise. Note that *id is set to NULL_TREE in
16042 this case. */
16044 static tree
16045 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16047 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16050 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16051 or a concept-name.
16053 enum-name:
16054 identifier
16056 typedef-name:
16057 identifier
16059 concept-name:
16060 identifier
16062 Returns a TYPE_DECL for the type. */
16064 static tree
16065 cp_parser_nonclass_name (cp_parser* parser)
16067 tree type_decl;
16068 tree identifier;
16070 cp_token *token = cp_lexer_peek_token (parser->lexer);
16071 identifier = cp_parser_identifier (parser);
16072 if (identifier == error_mark_node)
16073 return error_mark_node;
16075 /* Look up the type-name. */
16076 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16078 type_decl = strip_using_decl (type_decl);
16080 /* If we found an overload set, then it may refer to a concept-name. */
16081 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16082 type_decl = decl;
16084 if (TREE_CODE (type_decl) != TYPE_DECL
16085 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16087 /* See if this is an Objective-C type. */
16088 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16089 tree type = objc_get_protocol_qualified_type (identifier, protos);
16090 if (type)
16091 type_decl = TYPE_NAME (type);
16094 /* Issue an error if we did not find a type-name. */
16095 if (TREE_CODE (type_decl) != TYPE_DECL
16096 /* In Objective-C, we have the complication that class names are
16097 normally type names and start declarations (eg, the
16098 "NSObject" in "NSObject *object;"), but can be used in an
16099 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16100 is an expression. So, a classname followed by a dot is not a
16101 valid type-name. */
16102 || (objc_is_class_name (TREE_TYPE (type_decl))
16103 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16105 if (!cp_parser_simulate_error (parser))
16106 cp_parser_name_lookup_error (parser, identifier, type_decl,
16107 NLE_TYPE, token->location);
16108 return error_mark_node;
16110 /* Remember that the name was used in the definition of the
16111 current class so that we can check later to see if the
16112 meaning would have been different after the class was
16113 entirely defined. */
16114 else if (type_decl != error_mark_node
16115 && !parser->scope)
16116 maybe_note_name_used_in_class (identifier, type_decl);
16118 return type_decl;
16121 /* Parse an elaborated-type-specifier. Note that the grammar given
16122 here incorporates the resolution to DR68.
16124 elaborated-type-specifier:
16125 class-key :: [opt] nested-name-specifier [opt] identifier
16126 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16127 enum-key :: [opt] nested-name-specifier [opt] identifier
16128 typename :: [opt] nested-name-specifier identifier
16129 typename :: [opt] nested-name-specifier template [opt]
16130 template-id
16132 GNU extension:
16134 elaborated-type-specifier:
16135 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16136 class-key attributes :: [opt] nested-name-specifier [opt]
16137 template [opt] template-id
16138 enum attributes :: [opt] nested-name-specifier [opt] identifier
16140 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16141 declared `friend'. If IS_DECLARATION is TRUE, then this
16142 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16143 something is being declared.
16145 Returns the TYPE specified. */
16147 static tree
16148 cp_parser_elaborated_type_specifier (cp_parser* parser,
16149 bool is_friend,
16150 bool is_declaration)
16152 enum tag_types tag_type;
16153 tree identifier;
16154 tree type = NULL_TREE;
16155 tree attributes = NULL_TREE;
16156 tree globalscope;
16157 cp_token *token = NULL;
16159 /* See if we're looking at the `enum' keyword. */
16160 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16162 /* Consume the `enum' token. */
16163 cp_lexer_consume_token (parser->lexer);
16164 /* Remember that it's an enumeration type. */
16165 tag_type = enum_type;
16166 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16167 enums) is used here. */
16168 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16169 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16171 pedwarn (input_location, 0, "elaborated-type-specifier "
16172 "for a scoped enum must not use the %<%D%> keyword",
16173 cp_lexer_peek_token (parser->lexer)->u.value);
16174 /* Consume the `struct' or `class' and parse it anyway. */
16175 cp_lexer_consume_token (parser->lexer);
16177 /* Parse the attributes. */
16178 attributes = cp_parser_attributes_opt (parser);
16180 /* Or, it might be `typename'. */
16181 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16182 RID_TYPENAME))
16184 /* Consume the `typename' token. */
16185 cp_lexer_consume_token (parser->lexer);
16186 /* Remember that it's a `typename' type. */
16187 tag_type = typename_type;
16189 /* Otherwise it must be a class-key. */
16190 else
16192 tag_type = cp_parser_class_key (parser);
16193 if (tag_type == none_type)
16194 return error_mark_node;
16195 /* Parse the attributes. */
16196 attributes = cp_parser_attributes_opt (parser);
16199 /* Look for the `::' operator. */
16200 globalscope = cp_parser_global_scope_opt (parser,
16201 /*current_scope_valid_p=*/false);
16202 /* Look for the nested-name-specifier. */
16203 if (tag_type == typename_type && !globalscope)
16205 if (!cp_parser_nested_name_specifier (parser,
16206 /*typename_keyword_p=*/true,
16207 /*check_dependency_p=*/true,
16208 /*type_p=*/true,
16209 is_declaration))
16210 return error_mark_node;
16212 else
16213 /* Even though `typename' is not present, the proposed resolution
16214 to Core Issue 180 says that in `class A<T>::B', `B' should be
16215 considered a type-name, even if `A<T>' is dependent. */
16216 cp_parser_nested_name_specifier_opt (parser,
16217 /*typename_keyword_p=*/true,
16218 /*check_dependency_p=*/true,
16219 /*type_p=*/true,
16220 is_declaration);
16221 /* For everything but enumeration types, consider a template-id.
16222 For an enumeration type, consider only a plain identifier. */
16223 if (tag_type != enum_type)
16225 bool template_p = false;
16226 tree decl;
16228 /* Allow the `template' keyword. */
16229 template_p = cp_parser_optional_template_keyword (parser);
16230 /* If we didn't see `template', we don't know if there's a
16231 template-id or not. */
16232 if (!template_p)
16233 cp_parser_parse_tentatively (parser);
16234 /* Parse the template-id. */
16235 token = cp_lexer_peek_token (parser->lexer);
16236 decl = cp_parser_template_id (parser, template_p,
16237 /*check_dependency_p=*/true,
16238 tag_type,
16239 is_declaration);
16240 /* If we didn't find a template-id, look for an ordinary
16241 identifier. */
16242 if (!template_p && !cp_parser_parse_definitely (parser))
16244 /* We can get here when cp_parser_template_id, called by
16245 cp_parser_class_name with tag_type == none_type, succeeds
16246 and caches a BASELINK. Then, when called again here,
16247 instead of failing and returning an error_mark_node
16248 returns it (see template/typename17.C in C++11).
16249 ??? Could we diagnose this earlier? */
16250 else if (tag_type == typename_type && BASELINK_P (decl))
16252 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16253 type = error_mark_node;
16255 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16256 in effect, then we must assume that, upon instantiation, the
16257 template will correspond to a class. */
16258 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16259 && tag_type == typename_type)
16260 type = make_typename_type (parser->scope, decl,
16261 typename_type,
16262 /*complain=*/tf_error);
16263 /* If the `typename' keyword is in effect and DECL is not a type
16264 decl, then type is non existent. */
16265 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16267 else if (TREE_CODE (decl) == TYPE_DECL)
16268 type = check_elaborated_type_specifier (tag_type, decl,
16269 /*allow_template_p=*/true);
16270 else if (decl == error_mark_node)
16271 type = error_mark_node;
16274 if (!type)
16276 token = cp_lexer_peek_token (parser->lexer);
16277 identifier = cp_parser_identifier (parser);
16279 if (identifier == error_mark_node)
16281 parser->scope = NULL_TREE;
16282 return error_mark_node;
16285 /* For a `typename', we needn't call xref_tag. */
16286 if (tag_type == typename_type
16287 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16288 return cp_parser_make_typename_type (parser, identifier,
16289 token->location);
16291 /* Template parameter lists apply only if we are not within a
16292 function parameter list. */
16293 bool template_parm_lists_apply
16294 = parser->num_template_parameter_lists;
16295 if (template_parm_lists_apply)
16296 for (cp_binding_level *s = current_binding_level;
16297 s && s->kind != sk_template_parms;
16298 s = s->level_chain)
16299 if (s->kind == sk_function_parms)
16300 template_parm_lists_apply = false;
16302 /* Look up a qualified name in the usual way. */
16303 if (parser->scope)
16305 tree decl;
16306 tree ambiguous_decls;
16308 decl = cp_parser_lookup_name (parser, identifier,
16309 tag_type,
16310 /*is_template=*/false,
16311 /*is_namespace=*/false,
16312 /*check_dependency=*/true,
16313 &ambiguous_decls,
16314 token->location);
16316 /* If the lookup was ambiguous, an error will already have been
16317 issued. */
16318 if (ambiguous_decls)
16319 return error_mark_node;
16321 /* If we are parsing friend declaration, DECL may be a
16322 TEMPLATE_DECL tree node here. However, we need to check
16323 whether this TEMPLATE_DECL results in valid code. Consider
16324 the following example:
16326 namespace N {
16327 template <class T> class C {};
16329 class X {
16330 template <class T> friend class N::C; // #1, valid code
16332 template <class T> class Y {
16333 friend class N::C; // #2, invalid code
16336 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16337 name lookup of `N::C'. We see that friend declaration must
16338 be template for the code to be valid. Note that
16339 processing_template_decl does not work here since it is
16340 always 1 for the above two cases. */
16342 decl = (cp_parser_maybe_treat_template_as_class
16343 (decl, /*tag_name_p=*/is_friend
16344 && template_parm_lists_apply));
16346 if (TREE_CODE (decl) != TYPE_DECL)
16348 cp_parser_diagnose_invalid_type_name (parser,
16349 identifier,
16350 token->location);
16351 return error_mark_node;
16354 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16356 bool allow_template = (template_parm_lists_apply
16357 || DECL_SELF_REFERENCE_P (decl));
16358 type = check_elaborated_type_specifier (tag_type, decl,
16359 allow_template);
16361 if (type == error_mark_node)
16362 return error_mark_node;
16365 /* Forward declarations of nested types, such as
16367 class C1::C2;
16368 class C1::C2::C3;
16370 are invalid unless all components preceding the final '::'
16371 are complete. If all enclosing types are complete, these
16372 declarations become merely pointless.
16374 Invalid forward declarations of nested types are errors
16375 caught elsewhere in parsing. Those that are pointless arrive
16376 here. */
16378 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16379 && !is_friend && !processing_explicit_instantiation)
16380 warning (0, "declaration %qD does not declare anything", decl);
16382 type = TREE_TYPE (decl);
16384 else
16386 /* An elaborated-type-specifier sometimes introduces a new type and
16387 sometimes names an existing type. Normally, the rule is that it
16388 introduces a new type only if there is not an existing type of
16389 the same name already in scope. For example, given:
16391 struct S {};
16392 void f() { struct S s; }
16394 the `struct S' in the body of `f' is the same `struct S' as in
16395 the global scope; the existing definition is used. However, if
16396 there were no global declaration, this would introduce a new
16397 local class named `S'.
16399 An exception to this rule applies to the following code:
16401 namespace N { struct S; }
16403 Here, the elaborated-type-specifier names a new type
16404 unconditionally; even if there is already an `S' in the
16405 containing scope this declaration names a new type.
16406 This exception only applies if the elaborated-type-specifier
16407 forms the complete declaration:
16409 [class.name]
16411 A declaration consisting solely of `class-key identifier ;' is
16412 either a redeclaration of the name in the current scope or a
16413 forward declaration of the identifier as a class name. It
16414 introduces the name into the current scope.
16416 We are in this situation precisely when the next token is a `;'.
16418 An exception to the exception is that a `friend' declaration does
16419 *not* name a new type; i.e., given:
16421 struct S { friend struct T; };
16423 `T' is not a new type in the scope of `S'.
16425 Also, `new struct S' or `sizeof (struct S)' never results in the
16426 definition of a new type; a new type can only be declared in a
16427 declaration context. */
16429 tag_scope ts;
16430 bool template_p;
16432 if (is_friend)
16433 /* Friends have special name lookup rules. */
16434 ts = ts_within_enclosing_non_class;
16435 else if (is_declaration
16436 && cp_lexer_next_token_is (parser->lexer,
16437 CPP_SEMICOLON))
16438 /* This is a `class-key identifier ;' */
16439 ts = ts_current;
16440 else
16441 ts = ts_global;
16443 template_p =
16444 (template_parm_lists_apply
16445 && (cp_parser_next_token_starts_class_definition_p (parser)
16446 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16447 /* An unqualified name was used to reference this type, so
16448 there were no qualifying templates. */
16449 if (template_parm_lists_apply
16450 && !cp_parser_check_template_parameters (parser,
16451 /*num_templates=*/0,
16452 token->location,
16453 /*declarator=*/NULL))
16454 return error_mark_node;
16455 type = xref_tag (tag_type, identifier, ts, template_p);
16459 if (type == error_mark_node)
16460 return error_mark_node;
16462 /* Allow attributes on forward declarations of classes. */
16463 if (attributes)
16465 if (TREE_CODE (type) == TYPENAME_TYPE)
16466 warning (OPT_Wattributes,
16467 "attributes ignored on uninstantiated type");
16468 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16469 && ! processing_explicit_instantiation)
16470 warning (OPT_Wattributes,
16471 "attributes ignored on template instantiation");
16472 else if (is_declaration && cp_parser_declares_only_class_p (parser))
16473 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16474 else
16475 warning (OPT_Wattributes,
16476 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16479 if (tag_type != enum_type)
16481 /* Indicate whether this class was declared as a `class' or as a
16482 `struct'. */
16483 if (TREE_CODE (type) == RECORD_TYPE)
16484 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16485 cp_parser_check_class_key (tag_type, type);
16488 /* A "<" cannot follow an elaborated type specifier. If that
16489 happens, the user was probably trying to form a template-id. */
16490 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16491 token->location);
16493 return type;
16496 /* Parse an enum-specifier.
16498 enum-specifier:
16499 enum-head { enumerator-list [opt] }
16500 enum-head { enumerator-list , } [C++0x]
16502 enum-head:
16503 enum-key identifier [opt] enum-base [opt]
16504 enum-key nested-name-specifier identifier enum-base [opt]
16506 enum-key:
16507 enum
16508 enum class [C++0x]
16509 enum struct [C++0x]
16511 enum-base: [C++0x]
16512 : type-specifier-seq
16514 opaque-enum-specifier:
16515 enum-key identifier enum-base [opt] ;
16517 GNU Extensions:
16518 enum-key attributes[opt] identifier [opt] enum-base [opt]
16519 { enumerator-list [opt] }attributes[opt]
16520 enum-key attributes[opt] identifier [opt] enum-base [opt]
16521 { enumerator-list, }attributes[opt] [C++0x]
16523 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16524 if the token stream isn't an enum-specifier after all. */
16526 static tree
16527 cp_parser_enum_specifier (cp_parser* parser)
16529 tree identifier;
16530 tree type = NULL_TREE;
16531 tree prev_scope;
16532 tree nested_name_specifier = NULL_TREE;
16533 tree attributes;
16534 bool scoped_enum_p = false;
16535 bool has_underlying_type = false;
16536 bool nested_being_defined = false;
16537 bool new_value_list = false;
16538 bool is_new_type = false;
16539 bool is_anonymous = false;
16540 tree underlying_type = NULL_TREE;
16541 cp_token *type_start_token = NULL;
16542 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
16544 parser->colon_corrects_to_scope_p = false;
16546 /* Parse tentatively so that we can back up if we don't find a
16547 enum-specifier. */
16548 cp_parser_parse_tentatively (parser);
16550 /* Caller guarantees that the current token is 'enum', an identifier
16551 possibly follows, and the token after that is an opening brace.
16552 If we don't have an identifier, fabricate an anonymous name for
16553 the enumeration being defined. */
16554 cp_lexer_consume_token (parser->lexer);
16556 /* Parse the "class" or "struct", which indicates a scoped
16557 enumeration type in C++0x. */
16558 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16559 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16561 if (cxx_dialect < cxx11)
16562 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
16564 /* Consume the `struct' or `class' token. */
16565 cp_lexer_consume_token (parser->lexer);
16567 scoped_enum_p = true;
16570 attributes = cp_parser_attributes_opt (parser);
16572 /* Clear the qualification. */
16573 parser->scope = NULL_TREE;
16574 parser->qualifying_scope = NULL_TREE;
16575 parser->object_scope = NULL_TREE;
16577 /* Figure out in what scope the declaration is being placed. */
16578 prev_scope = current_scope ();
16580 type_start_token = cp_lexer_peek_token (parser->lexer);
16582 push_deferring_access_checks (dk_no_check);
16583 nested_name_specifier
16584 = cp_parser_nested_name_specifier_opt (parser,
16585 /*typename_keyword_p=*/true,
16586 /*check_dependency_p=*/false,
16587 /*type_p=*/false,
16588 /*is_declaration=*/false);
16590 if (nested_name_specifier)
16592 tree name;
16594 identifier = cp_parser_identifier (parser);
16595 name = cp_parser_lookup_name (parser, identifier,
16596 enum_type,
16597 /*is_template=*/false,
16598 /*is_namespace=*/false,
16599 /*check_dependency=*/true,
16600 /*ambiguous_decls=*/NULL,
16601 input_location);
16602 if (name && name != error_mark_node)
16604 type = TREE_TYPE (name);
16605 if (TREE_CODE (type) == TYPENAME_TYPE)
16607 /* Are template enums allowed in ISO? */
16608 if (template_parm_scope_p ())
16609 pedwarn (type_start_token->location, OPT_Wpedantic,
16610 "%qD is an enumeration template", name);
16611 /* ignore a typename reference, for it will be solved by name
16612 in start_enum. */
16613 type = NULL_TREE;
16616 else if (nested_name_specifier == error_mark_node)
16617 /* We already issued an error. */;
16618 else
16620 error_at (type_start_token->location,
16621 "%qD does not name an enumeration in %qT",
16622 identifier, nested_name_specifier);
16623 nested_name_specifier = error_mark_node;
16626 else
16628 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16629 identifier = cp_parser_identifier (parser);
16630 else
16632 identifier = make_anon_name ();
16633 is_anonymous = true;
16634 if (scoped_enum_p)
16635 error_at (type_start_token->location,
16636 "anonymous scoped enum is not allowed");
16639 pop_deferring_access_checks ();
16641 /* Check for the `:' that denotes a specified underlying type in C++0x.
16642 Note that a ':' could also indicate a bitfield width, however. */
16643 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16645 cp_decl_specifier_seq type_specifiers;
16647 /* Consume the `:'. */
16648 cp_lexer_consume_token (parser->lexer);
16650 /* Parse the type-specifier-seq. */
16651 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16652 /*is_trailing_return=*/false,
16653 &type_specifiers);
16655 /* At this point this is surely not elaborated type specifier. */
16656 if (!cp_parser_parse_definitely (parser))
16657 return NULL_TREE;
16659 if (cxx_dialect < cxx11)
16660 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
16662 has_underlying_type = true;
16664 /* If that didn't work, stop. */
16665 if (type_specifiers.type != error_mark_node)
16667 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
16668 /*initialized=*/0, NULL);
16669 if (underlying_type == error_mark_node
16670 || check_for_bare_parameter_packs (underlying_type))
16671 underlying_type = NULL_TREE;
16675 /* Look for the `{' but don't consume it yet. */
16676 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16678 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
16680 cp_parser_error (parser, "expected %<{%>");
16681 if (has_underlying_type)
16683 type = NULL_TREE;
16684 goto out;
16687 /* An opaque-enum-specifier must have a ';' here. */
16688 if ((scoped_enum_p || underlying_type)
16689 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16691 cp_parser_error (parser, "expected %<;%> or %<{%>");
16692 if (has_underlying_type)
16694 type = NULL_TREE;
16695 goto out;
16700 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
16701 return NULL_TREE;
16703 if (nested_name_specifier)
16705 if (CLASS_TYPE_P (nested_name_specifier))
16707 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
16708 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
16709 push_scope (nested_name_specifier);
16711 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16713 push_nested_namespace (nested_name_specifier);
16717 /* Issue an error message if type-definitions are forbidden here. */
16718 if (!cp_parser_check_type_definition (parser))
16719 type = error_mark_node;
16720 else
16721 /* Create the new type. We do this before consuming the opening
16722 brace so the enum will be recorded as being on the line of its
16723 tag (or the 'enum' keyword, if there is no tag). */
16724 type = start_enum (identifier, type, underlying_type,
16725 scoped_enum_p, &is_new_type);
16727 /* If the next token is not '{' it is an opaque-enum-specifier or an
16728 elaborated-type-specifier. */
16729 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16731 timevar_push (TV_PARSE_ENUM);
16732 if (nested_name_specifier
16733 && nested_name_specifier != error_mark_node)
16735 /* The following catches invalid code such as:
16736 enum class S<int>::E { A, B, C }; */
16737 if (!processing_specialization
16738 && CLASS_TYPE_P (nested_name_specifier)
16739 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
16740 error_at (type_start_token->location, "cannot add an enumerator "
16741 "list to a template instantiation");
16743 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
16745 error_at (type_start_token->location,
16746 "%<%T::%E%> has not been declared",
16747 TYPE_CONTEXT (nested_name_specifier),
16748 nested_name_specifier);
16749 type = error_mark_node;
16751 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
16752 && !CLASS_TYPE_P (nested_name_specifier))
16754 error_at (type_start_token->location, "nested name specifier "
16755 "%qT for enum declaration does not name a class "
16756 "or namespace", nested_name_specifier);
16757 type = error_mark_node;
16759 /* If that scope does not contain the scope in which the
16760 class was originally declared, the program is invalid. */
16761 else if (prev_scope && !is_ancestor (prev_scope,
16762 nested_name_specifier))
16764 if (at_namespace_scope_p ())
16765 error_at (type_start_token->location,
16766 "declaration of %qD in namespace %qD which does not "
16767 "enclose %qD",
16768 type, prev_scope, nested_name_specifier);
16769 else
16770 error_at (type_start_token->location,
16771 "declaration of %qD in %qD which does not "
16772 "enclose %qD",
16773 type, prev_scope, nested_name_specifier);
16774 type = error_mark_node;
16778 if (scoped_enum_p)
16779 begin_scope (sk_scoped_enum, type);
16781 /* Consume the opening brace. */
16782 cp_lexer_consume_token (parser->lexer);
16784 if (type == error_mark_node)
16785 ; /* Nothing to add */
16786 else if (OPAQUE_ENUM_P (type)
16787 || (cxx_dialect > cxx98 && processing_specialization))
16789 new_value_list = true;
16790 SET_OPAQUE_ENUM_P (type, false);
16791 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16793 else
16795 error_at (type_start_token->location,
16796 "multiple definition of %q#T", type);
16797 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
16798 "previous definition here");
16799 type = error_mark_node;
16802 if (type == error_mark_node)
16803 cp_parser_skip_to_end_of_block_or_statement (parser);
16804 /* If the next token is not '}', then there are some enumerators. */
16805 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16807 if (is_anonymous && !scoped_enum_p)
16808 pedwarn (type_start_token->location, OPT_Wpedantic,
16809 "ISO C++ forbids empty anonymous enum");
16811 else
16812 cp_parser_enumerator_list (parser, type);
16814 /* Consume the final '}'. */
16815 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16817 if (scoped_enum_p)
16818 finish_scope ();
16819 timevar_pop (TV_PARSE_ENUM);
16821 else
16823 /* If a ';' follows, then it is an opaque-enum-specifier
16824 and additional restrictions apply. */
16825 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16827 if (is_anonymous)
16828 error_at (type_start_token->location,
16829 "opaque-enum-specifier without name");
16830 else if (nested_name_specifier)
16831 error_at (type_start_token->location,
16832 "opaque-enum-specifier must use a simple identifier");
16836 /* Look for trailing attributes to apply to this enumeration, and
16837 apply them if appropriate. */
16838 if (cp_parser_allow_gnu_extensions_p (parser))
16840 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16841 trailing_attr = chainon (trailing_attr, attributes);
16842 cplus_decl_attributes (&type,
16843 trailing_attr,
16844 (int) ATTR_FLAG_TYPE_IN_PLACE);
16847 /* Finish up the enumeration. */
16848 if (type != error_mark_node)
16850 if (new_value_list)
16851 finish_enum_value_list (type);
16852 if (is_new_type)
16853 finish_enum (type);
16856 if (nested_name_specifier)
16858 if (CLASS_TYPE_P (nested_name_specifier))
16860 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16861 pop_scope (nested_name_specifier);
16863 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16865 pop_nested_namespace (nested_name_specifier);
16868 out:
16869 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16870 return type;
16873 /* Parse an enumerator-list. The enumerators all have the indicated
16874 TYPE.
16876 enumerator-list:
16877 enumerator-definition
16878 enumerator-list , enumerator-definition */
16880 static void
16881 cp_parser_enumerator_list (cp_parser* parser, tree type)
16883 while (true)
16885 /* Parse an enumerator-definition. */
16886 cp_parser_enumerator_definition (parser, type);
16888 /* If the next token is not a ',', we've reached the end of
16889 the list. */
16890 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16891 break;
16892 /* Otherwise, consume the `,' and keep going. */
16893 cp_lexer_consume_token (parser->lexer);
16894 /* If the next token is a `}', there is a trailing comma. */
16895 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16897 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16898 pedwarn (input_location, OPT_Wpedantic,
16899 "comma at end of enumerator list");
16900 break;
16905 /* Parse an enumerator-definition. The enumerator has the indicated
16906 TYPE.
16908 enumerator-definition:
16909 enumerator
16910 enumerator = constant-expression
16912 enumerator:
16913 identifier
16915 GNU Extensions:
16917 enumerator-definition:
16918 enumerator attributes [opt]
16919 enumerator attributes [opt] = constant-expression */
16921 static void
16922 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16924 tree identifier;
16925 tree value;
16926 location_t loc;
16928 /* Save the input location because we are interested in the location
16929 of the identifier and not the location of the explicit value. */
16930 loc = cp_lexer_peek_token (parser->lexer)->location;
16932 /* Look for the identifier. */
16933 identifier = cp_parser_identifier (parser);
16934 if (identifier == error_mark_node)
16935 return;
16937 /* Parse any specified attributes. */
16938 tree attrs = cp_parser_attributes_opt (parser);
16940 /* If the next token is an '=', then there is an explicit value. */
16941 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16943 /* Consume the `=' token. */
16944 cp_lexer_consume_token (parser->lexer);
16945 /* Parse the value. */
16946 value = cp_parser_constant_expression (parser);
16948 else
16949 value = NULL_TREE;
16951 /* If we are processing a template, make sure the initializer of the
16952 enumerator doesn't contain any bare template parameter pack. */
16953 if (check_for_bare_parameter_packs (value))
16954 value = error_mark_node;
16956 /* Create the enumerator. */
16957 build_enumerator (identifier, value, type, attrs, loc);
16960 /* Parse a namespace-name.
16962 namespace-name:
16963 original-namespace-name
16964 namespace-alias
16966 Returns the NAMESPACE_DECL for the namespace. */
16968 static tree
16969 cp_parser_namespace_name (cp_parser* parser)
16971 tree identifier;
16972 tree namespace_decl;
16974 cp_token *token = cp_lexer_peek_token (parser->lexer);
16976 /* Get the name of the namespace. */
16977 identifier = cp_parser_identifier (parser);
16978 if (identifier == error_mark_node)
16979 return error_mark_node;
16981 /* Look up the identifier in the currently active scope. Look only
16982 for namespaces, due to:
16984 [basic.lookup.udir]
16986 When looking up a namespace-name in a using-directive or alias
16987 definition, only namespace names are considered.
16989 And:
16991 [basic.lookup.qual]
16993 During the lookup of a name preceding the :: scope resolution
16994 operator, object, function, and enumerator names are ignored.
16996 (Note that cp_parser_qualifying_entity only calls this
16997 function if the token after the name is the scope resolution
16998 operator.) */
16999 namespace_decl = cp_parser_lookup_name (parser, identifier,
17000 none_type,
17001 /*is_template=*/false,
17002 /*is_namespace=*/true,
17003 /*check_dependency=*/true,
17004 /*ambiguous_decls=*/NULL,
17005 token->location);
17006 /* If it's not a namespace, issue an error. */
17007 if (namespace_decl == error_mark_node
17008 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17010 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17011 error_at (token->location, "%qD is not a namespace-name", identifier);
17012 cp_parser_error (parser, "expected namespace-name");
17013 namespace_decl = error_mark_node;
17016 return namespace_decl;
17019 /* Parse a namespace-definition.
17021 namespace-definition:
17022 named-namespace-definition
17023 unnamed-namespace-definition
17025 named-namespace-definition:
17026 original-namespace-definition
17027 extension-namespace-definition
17029 original-namespace-definition:
17030 namespace identifier { namespace-body }
17032 extension-namespace-definition:
17033 namespace original-namespace-name { namespace-body }
17035 unnamed-namespace-definition:
17036 namespace { namespace-body } */
17038 static void
17039 cp_parser_namespace_definition (cp_parser* parser)
17041 tree identifier, attribs;
17042 bool has_visibility;
17043 bool is_inline;
17044 cp_token* token;
17045 int nested_definition_count = 0;
17047 cp_ensure_no_omp_declare_simd (parser);
17048 cp_ensure_no_oacc_routine (parser);
17049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17051 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17052 is_inline = true;
17053 cp_lexer_consume_token (parser->lexer);
17055 else
17056 is_inline = false;
17058 /* Look for the `namespace' keyword. */
17059 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17061 /* Parse any specified attributes before the identifier. */
17062 attribs = cp_parser_attributes_opt (parser);
17064 /* Get the name of the namespace. We do not attempt to distinguish
17065 between an original-namespace-definition and an
17066 extension-namespace-definition at this point. The semantic
17067 analysis routines are responsible for that. */
17068 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17069 identifier = cp_parser_identifier (parser);
17070 else
17071 identifier = NULL_TREE;
17073 /* Parse any specified attributes after the identifier. */
17074 tree post_ident_attribs = cp_parser_attributes_opt (parser);
17075 if (post_ident_attribs)
17077 if (attribs)
17078 attribs = chainon (attribs, post_ident_attribs);
17079 else
17080 attribs = post_ident_attribs;
17083 /* Start the namespace. */
17084 push_namespace (identifier);
17086 /* Parse any nested namespace definition. */
17087 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17089 if (attribs)
17090 error_at (token->location, "a nested namespace definition cannot have attributes");
17091 if (cxx_dialect < cxx1z)
17092 pedwarn (input_location, OPT_Wpedantic,
17093 "nested namespace definitions only available with "
17094 "-std=c++1z or -std=gnu++1z");
17095 if (is_inline)
17096 error_at (token->location, "a nested namespace definition cannot be inline");
17097 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17099 cp_lexer_consume_token (parser->lexer);
17100 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17101 identifier = cp_parser_identifier (parser);
17102 else
17104 cp_parser_error (parser, "nested identifier required");
17105 break;
17107 ++nested_definition_count;
17108 push_namespace (identifier);
17112 /* Look for the `{' to validate starting the namespace. */
17113 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17115 /* "inline namespace" is equivalent to a stub namespace definition
17116 followed by a strong using directive. */
17117 if (is_inline)
17119 tree name_space = current_namespace;
17120 /* Set up namespace association. */
17121 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17122 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17123 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17124 /* Import the contents of the inline namespace. */
17125 pop_namespace ();
17126 do_using_directive (name_space);
17127 push_namespace (identifier);
17130 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17132 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17134 /* Parse the body of the namespace. */
17135 cp_parser_namespace_body (parser);
17137 if (has_visibility)
17138 pop_visibility (1);
17140 /* Finish the nested namespace definitions. */
17141 while (nested_definition_count--)
17142 pop_namespace ();
17144 /* Finish the namespace. */
17145 pop_namespace ();
17146 /* Look for the final `}'. */
17147 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17150 /* Parse a namespace-body.
17152 namespace-body:
17153 declaration-seq [opt] */
17155 static void
17156 cp_parser_namespace_body (cp_parser* parser)
17158 cp_parser_declaration_seq_opt (parser);
17161 /* Parse a namespace-alias-definition.
17163 namespace-alias-definition:
17164 namespace identifier = qualified-namespace-specifier ; */
17166 static void
17167 cp_parser_namespace_alias_definition (cp_parser* parser)
17169 tree identifier;
17170 tree namespace_specifier;
17172 cp_token *token = cp_lexer_peek_token (parser->lexer);
17174 /* Look for the `namespace' keyword. */
17175 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17176 /* Look for the identifier. */
17177 identifier = cp_parser_identifier (parser);
17178 if (identifier == error_mark_node)
17179 return;
17180 /* Look for the `=' token. */
17181 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17182 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17184 error_at (token->location, "%<namespace%> definition is not allowed here");
17185 /* Skip the definition. */
17186 cp_lexer_consume_token (parser->lexer);
17187 if (cp_parser_skip_to_closing_brace (parser))
17188 cp_lexer_consume_token (parser->lexer);
17189 return;
17191 cp_parser_require (parser, CPP_EQ, RT_EQ);
17192 /* Look for the qualified-namespace-specifier. */
17193 namespace_specifier
17194 = cp_parser_qualified_namespace_specifier (parser);
17195 /* Look for the `;' token. */
17196 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17198 /* Register the alias in the symbol table. */
17199 do_namespace_alias (identifier, namespace_specifier);
17202 /* Parse a qualified-namespace-specifier.
17204 qualified-namespace-specifier:
17205 :: [opt] nested-name-specifier [opt] namespace-name
17207 Returns a NAMESPACE_DECL corresponding to the specified
17208 namespace. */
17210 static tree
17211 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17213 /* Look for the optional `::'. */
17214 cp_parser_global_scope_opt (parser,
17215 /*current_scope_valid_p=*/false);
17217 /* Look for the optional nested-name-specifier. */
17218 cp_parser_nested_name_specifier_opt (parser,
17219 /*typename_keyword_p=*/false,
17220 /*check_dependency_p=*/true,
17221 /*type_p=*/false,
17222 /*is_declaration=*/true);
17224 return cp_parser_namespace_name (parser);
17227 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17228 access declaration.
17230 using-declaration:
17231 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17232 using :: unqualified-id ;
17234 access-declaration:
17235 qualified-id ;
17239 static bool
17240 cp_parser_using_declaration (cp_parser* parser,
17241 bool access_declaration_p)
17243 cp_token *token;
17244 bool typename_p = false;
17245 bool global_scope_p;
17246 tree decl;
17247 tree identifier;
17248 tree qscope;
17249 int oldcount = errorcount;
17250 cp_token *diag_token = NULL;
17252 if (access_declaration_p)
17254 diag_token = cp_lexer_peek_token (parser->lexer);
17255 cp_parser_parse_tentatively (parser);
17257 else
17259 /* Look for the `using' keyword. */
17260 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17262 /* Peek at the next token. */
17263 token = cp_lexer_peek_token (parser->lexer);
17264 /* See if it's `typename'. */
17265 if (token->keyword == RID_TYPENAME)
17267 /* Remember that we've seen it. */
17268 typename_p = true;
17269 /* Consume the `typename' token. */
17270 cp_lexer_consume_token (parser->lexer);
17274 /* Look for the optional global scope qualification. */
17275 global_scope_p
17276 = (cp_parser_global_scope_opt (parser,
17277 /*current_scope_valid_p=*/false)
17278 != NULL_TREE);
17280 /* If we saw `typename', or didn't see `::', then there must be a
17281 nested-name-specifier present. */
17282 if (typename_p || !global_scope_p)
17284 qscope = cp_parser_nested_name_specifier (parser, typename_p,
17285 /*check_dependency_p=*/true,
17286 /*type_p=*/false,
17287 /*is_declaration=*/true);
17288 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17290 cp_parser_skip_to_end_of_block_or_statement (parser);
17291 return false;
17294 /* Otherwise, we could be in either of the two productions. In that
17295 case, treat the nested-name-specifier as optional. */
17296 else
17297 qscope = cp_parser_nested_name_specifier_opt (parser,
17298 /*typename_keyword_p=*/false,
17299 /*check_dependency_p=*/true,
17300 /*type_p=*/false,
17301 /*is_declaration=*/true);
17302 if (!qscope)
17303 qscope = global_namespace;
17304 else if (UNSCOPED_ENUM_P (qscope))
17305 qscope = CP_TYPE_CONTEXT (qscope);
17307 if (access_declaration_p && cp_parser_error_occurred (parser))
17308 /* Something has already gone wrong; there's no need to parse
17309 further. Since an error has occurred, the return value of
17310 cp_parser_parse_definitely will be false, as required. */
17311 return cp_parser_parse_definitely (parser);
17313 token = cp_lexer_peek_token (parser->lexer);
17314 /* Parse the unqualified-id. */
17315 identifier = cp_parser_unqualified_id (parser,
17316 /*template_keyword_p=*/false,
17317 /*check_dependency_p=*/true,
17318 /*declarator_p=*/true,
17319 /*optional_p=*/false);
17321 if (access_declaration_p)
17323 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17324 cp_parser_simulate_error (parser);
17325 if (!cp_parser_parse_definitely (parser))
17326 return false;
17329 /* The function we call to handle a using-declaration is different
17330 depending on what scope we are in. */
17331 if (qscope == error_mark_node || identifier == error_mark_node)
17333 else if (!identifier_p (identifier)
17334 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17335 /* [namespace.udecl]
17337 A using declaration shall not name a template-id. */
17338 error_at (token->location,
17339 "a template-id may not appear in a using-declaration");
17340 else
17342 if (at_class_scope_p ())
17344 /* Create the USING_DECL. */
17345 decl = do_class_using_decl (parser->scope, identifier);
17347 if (decl && typename_p)
17348 USING_DECL_TYPENAME_P (decl) = 1;
17350 if (check_for_bare_parameter_packs (decl))
17352 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17353 return false;
17355 else
17356 /* Add it to the list of members in this class. */
17357 finish_member_declaration (decl);
17359 else
17361 decl = cp_parser_lookup_name_simple (parser,
17362 identifier,
17363 token->location);
17364 if (decl == error_mark_node)
17365 cp_parser_name_lookup_error (parser, identifier,
17366 decl, NLE_NULL,
17367 token->location);
17368 else if (check_for_bare_parameter_packs (decl))
17370 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17371 return false;
17373 else if (!at_namespace_scope_p ())
17374 do_local_using_decl (decl, qscope, identifier);
17375 else
17376 do_toplevel_using_decl (decl, qscope, identifier);
17380 /* Look for the final `;'. */
17381 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17383 if (access_declaration_p && errorcount == oldcount)
17384 warning_at (diag_token->location, OPT_Wdeprecated,
17385 "access declarations are deprecated "
17386 "in favour of using-declarations; "
17387 "suggestion: add the %<using%> keyword");
17389 return true;
17392 /* Parse an alias-declaration.
17394 alias-declaration:
17395 using identifier attribute-specifier-seq [opt] = type-id */
17397 static tree
17398 cp_parser_alias_declaration (cp_parser* parser)
17400 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17401 location_t id_location;
17402 cp_declarator *declarator;
17403 cp_decl_specifier_seq decl_specs;
17404 bool member_p;
17405 const char *saved_message = NULL;
17407 /* Look for the `using' keyword. */
17408 cp_token *using_token
17409 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17410 if (using_token == NULL)
17411 return error_mark_node;
17413 id_location = cp_lexer_peek_token (parser->lexer)->location;
17414 id = cp_parser_identifier (parser);
17415 if (id == error_mark_node)
17416 return error_mark_node;
17418 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17419 attributes = cp_parser_attributes_opt (parser);
17420 if (attributes == error_mark_node)
17421 return error_mark_node;
17423 cp_parser_require (parser, CPP_EQ, RT_EQ);
17425 if (cp_parser_error_occurred (parser))
17426 return error_mark_node;
17428 cp_parser_commit_to_tentative_parse (parser);
17430 /* Now we are going to parse the type-id of the declaration. */
17433 [dcl.type]/3 says:
17435 "A type-specifier-seq shall not define a class or enumeration
17436 unless it appears in the type-id of an alias-declaration (7.1.3) that
17437 is not the declaration of a template-declaration."
17439 In other words, if we currently are in an alias template, the
17440 type-id should not define a type.
17442 So let's set parser->type_definition_forbidden_message in that
17443 case; cp_parser_check_type_definition (called by
17444 cp_parser_class_specifier) will then emit an error if a type is
17445 defined in the type-id. */
17446 if (parser->num_template_parameter_lists)
17448 saved_message = parser->type_definition_forbidden_message;
17449 parser->type_definition_forbidden_message =
17450 G_("types may not be defined in alias template declarations");
17453 type = cp_parser_type_id (parser);
17455 /* Restore the error message if need be. */
17456 if (parser->num_template_parameter_lists)
17457 parser->type_definition_forbidden_message = saved_message;
17459 if (type == error_mark_node
17460 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17462 cp_parser_skip_to_end_of_block_or_statement (parser);
17463 return error_mark_node;
17466 /* A typedef-name can also be introduced by an alias-declaration. The
17467 identifier following the using keyword becomes a typedef-name. It has
17468 the same semantics as if it were introduced by the typedef
17469 specifier. In particular, it does not define a new type and it shall
17470 not appear in the type-id. */
17472 clear_decl_specs (&decl_specs);
17473 decl_specs.type = type;
17474 if (attributes != NULL_TREE)
17476 decl_specs.attributes = attributes;
17477 set_and_check_decl_spec_loc (&decl_specs,
17478 ds_attribute,
17479 attrs_token);
17481 set_and_check_decl_spec_loc (&decl_specs,
17482 ds_typedef,
17483 using_token);
17484 set_and_check_decl_spec_loc (&decl_specs,
17485 ds_alias,
17486 using_token);
17488 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17489 declarator->id_loc = id_location;
17491 member_p = at_class_scope_p ();
17492 if (member_p)
17493 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17494 NULL_TREE, attributes);
17495 else
17496 decl = start_decl (declarator, &decl_specs, 0,
17497 attributes, NULL_TREE, &pushed_scope);
17498 if (decl == error_mark_node)
17499 return decl;
17501 // Attach constraints to the alias declaration.
17502 if (flag_concepts && current_template_parms)
17504 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
17505 tree constr = build_constraints (reqs, NULL_TREE);
17506 set_constraints (decl, constr);
17509 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
17511 if (pushed_scope)
17512 pop_scope (pushed_scope);
17514 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17515 added into the symbol table; otherwise, return the TYPE_DECL. */
17516 if (DECL_LANG_SPECIFIC (decl)
17517 && DECL_TEMPLATE_INFO (decl)
17518 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
17520 decl = DECL_TI_TEMPLATE (decl);
17521 if (member_p)
17522 check_member_template (decl);
17525 return decl;
17528 /* Parse a using-directive.
17530 using-directive:
17531 using namespace :: [opt] nested-name-specifier [opt]
17532 namespace-name ; */
17534 static void
17535 cp_parser_using_directive (cp_parser* parser)
17537 tree namespace_decl;
17538 tree attribs;
17540 /* Look for the `using' keyword. */
17541 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17542 /* And the `namespace' keyword. */
17543 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17544 /* Look for the optional `::' operator. */
17545 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17546 /* And the optional nested-name-specifier. */
17547 cp_parser_nested_name_specifier_opt (parser,
17548 /*typename_keyword_p=*/false,
17549 /*check_dependency_p=*/true,
17550 /*type_p=*/false,
17551 /*is_declaration=*/true);
17552 /* Get the namespace being used. */
17553 namespace_decl = cp_parser_namespace_name (parser);
17554 /* And any specified attributes. */
17555 attribs = cp_parser_attributes_opt (parser);
17556 /* Update the symbol table. */
17557 parse_using_directive (namespace_decl, attribs);
17558 /* Look for the final `;'. */
17559 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17562 /* Parse an asm-definition.
17564 asm-definition:
17565 asm ( string-literal ) ;
17567 GNU Extension:
17569 asm-definition:
17570 asm volatile [opt] ( string-literal ) ;
17571 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
17572 asm volatile [opt] ( string-literal : asm-operand-list [opt]
17573 : asm-operand-list [opt] ) ;
17574 asm volatile [opt] ( string-literal : asm-operand-list [opt]
17575 : asm-operand-list [opt]
17576 : asm-clobber-list [opt] ) ;
17577 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
17578 : asm-clobber-list [opt]
17579 : asm-goto-list ) ; */
17581 static void
17582 cp_parser_asm_definition (cp_parser* parser)
17584 tree string;
17585 tree outputs = NULL_TREE;
17586 tree inputs = NULL_TREE;
17587 tree clobbers = NULL_TREE;
17588 tree labels = NULL_TREE;
17589 tree asm_stmt;
17590 bool volatile_p = false;
17591 bool extended_p = false;
17592 bool invalid_inputs_p = false;
17593 bool invalid_outputs_p = false;
17594 bool goto_p = false;
17595 required_token missing = RT_NONE;
17597 /* Look for the `asm' keyword. */
17598 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
17600 if (parser->in_function_body
17601 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
17603 error ("%<asm%> in %<constexpr%> function");
17604 cp_function_chain->invalid_constexpr = true;
17607 /* See if the next token is `volatile'. */
17608 if (cp_parser_allow_gnu_extensions_p (parser)
17609 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
17611 /* Remember that we saw the `volatile' keyword. */
17612 volatile_p = true;
17613 /* Consume the token. */
17614 cp_lexer_consume_token (parser->lexer);
17616 if (cp_parser_allow_gnu_extensions_p (parser)
17617 && parser->in_function_body
17618 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
17620 /* Remember that we saw the `goto' keyword. */
17621 goto_p = true;
17622 /* Consume the token. */
17623 cp_lexer_consume_token (parser->lexer);
17625 /* Look for the opening `('. */
17626 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
17627 return;
17628 /* Look for the string. */
17629 string = cp_parser_string_literal (parser, false, false);
17630 if (string == error_mark_node)
17632 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17633 /*consume_paren=*/true);
17634 return;
17637 /* If we're allowing GNU extensions, check for the extended assembly
17638 syntax. Unfortunately, the `:' tokens need not be separated by
17639 a space in C, and so, for compatibility, we tolerate that here
17640 too. Doing that means that we have to treat the `::' operator as
17641 two `:' tokens. */
17642 if (cp_parser_allow_gnu_extensions_p (parser)
17643 && parser->in_function_body
17644 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
17645 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
17647 bool inputs_p = false;
17648 bool clobbers_p = false;
17649 bool labels_p = false;
17651 /* The extended syntax was used. */
17652 extended_p = true;
17654 /* Look for outputs. */
17655 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17657 /* Consume the `:'. */
17658 cp_lexer_consume_token (parser->lexer);
17659 /* Parse the output-operands. */
17660 if (cp_lexer_next_token_is_not (parser->lexer,
17661 CPP_COLON)
17662 && cp_lexer_next_token_is_not (parser->lexer,
17663 CPP_SCOPE)
17664 && cp_lexer_next_token_is_not (parser->lexer,
17665 CPP_CLOSE_PAREN)
17666 && !goto_p)
17668 outputs = cp_parser_asm_operand_list (parser);
17669 if (outputs == error_mark_node)
17670 invalid_outputs_p = true;
17673 /* If the next token is `::', there are no outputs, and the
17674 next token is the beginning of the inputs. */
17675 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17676 /* The inputs are coming next. */
17677 inputs_p = true;
17679 /* Look for inputs. */
17680 if (inputs_p
17681 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17683 /* Consume the `:' or `::'. */
17684 cp_lexer_consume_token (parser->lexer);
17685 /* Parse the output-operands. */
17686 if (cp_lexer_next_token_is_not (parser->lexer,
17687 CPP_COLON)
17688 && cp_lexer_next_token_is_not (parser->lexer,
17689 CPP_SCOPE)
17690 && cp_lexer_next_token_is_not (parser->lexer,
17691 CPP_CLOSE_PAREN))
17693 inputs = cp_parser_asm_operand_list (parser);
17694 if (inputs == error_mark_node)
17695 invalid_inputs_p = true;
17698 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17699 /* The clobbers are coming next. */
17700 clobbers_p = true;
17702 /* Look for clobbers. */
17703 if (clobbers_p
17704 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17706 clobbers_p = true;
17707 /* Consume the `:' or `::'. */
17708 cp_lexer_consume_token (parser->lexer);
17709 /* Parse the clobbers. */
17710 if (cp_lexer_next_token_is_not (parser->lexer,
17711 CPP_COLON)
17712 && cp_lexer_next_token_is_not (parser->lexer,
17713 CPP_CLOSE_PAREN))
17714 clobbers = cp_parser_asm_clobber_list (parser);
17716 else if (goto_p
17717 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17718 /* The labels are coming next. */
17719 labels_p = true;
17721 /* Look for labels. */
17722 if (labels_p
17723 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
17725 labels_p = true;
17726 /* Consume the `:' or `::'. */
17727 cp_lexer_consume_token (parser->lexer);
17728 /* Parse the labels. */
17729 labels = cp_parser_asm_label_list (parser);
17732 if (goto_p && !labels_p)
17733 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
17735 else if (goto_p)
17736 missing = RT_COLON_SCOPE;
17738 /* Look for the closing `)'. */
17739 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
17740 missing ? missing : RT_CLOSE_PAREN))
17741 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17742 /*consume_paren=*/true);
17743 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17745 if (!invalid_inputs_p && !invalid_outputs_p)
17747 /* Create the ASM_EXPR. */
17748 if (parser->in_function_body)
17750 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
17751 inputs, clobbers, labels);
17752 /* If the extended syntax was not used, mark the ASM_EXPR. */
17753 if (!extended_p)
17755 tree temp = asm_stmt;
17756 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
17757 temp = TREE_OPERAND (temp, 0);
17759 ASM_INPUT_P (temp) = 1;
17762 else
17763 symtab->finalize_toplevel_asm (string);
17767 /* Declarators [gram.dcl.decl] */
17769 /* Parse an init-declarator.
17771 init-declarator:
17772 declarator initializer [opt]
17774 GNU Extension:
17776 init-declarator:
17777 declarator asm-specification [opt] attributes [opt] initializer [opt]
17779 function-definition:
17780 decl-specifier-seq [opt] declarator ctor-initializer [opt]
17781 function-body
17782 decl-specifier-seq [opt] declarator function-try-block
17784 GNU Extension:
17786 function-definition:
17787 __extension__ function-definition
17789 TM Extension:
17791 function-definition:
17792 decl-specifier-seq [opt] declarator function-transaction-block
17794 The DECL_SPECIFIERS apply to this declarator. Returns a
17795 representation of the entity declared. If MEMBER_P is TRUE, then
17796 this declarator appears in a class scope. The new DECL created by
17797 this declarator is returned.
17799 The CHECKS are access checks that should be performed once we know
17800 what entity is being declared (and, therefore, what classes have
17801 befriended it).
17803 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
17804 for a function-definition here as well. If the declarator is a
17805 declarator for a function-definition, *FUNCTION_DEFINITION_P will
17806 be TRUE upon return. By that point, the function-definition will
17807 have been completely parsed.
17809 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
17810 is FALSE.
17812 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
17813 parsed declaration if it is an uninitialized single declarator not followed
17814 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
17815 if present, will not be consumed. If returned, this declarator will be
17816 created with SD_INITIALIZED but will not call cp_finish_decl.
17818 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
17819 and there is an initializer, the pointed location_t is set to the
17820 location of the '=' or `(', or '{' in C++11 token introducing the
17821 initializer. */
17823 static tree
17824 cp_parser_init_declarator (cp_parser* parser,
17825 cp_decl_specifier_seq *decl_specifiers,
17826 vec<deferred_access_check, va_gc> *checks,
17827 bool function_definition_allowed_p,
17828 bool member_p,
17829 int declares_class_or_enum,
17830 bool* function_definition_p,
17831 tree* maybe_range_for_decl,
17832 location_t* init_loc)
17834 cp_token *token = NULL, *asm_spec_start_token = NULL,
17835 *attributes_start_token = NULL;
17836 cp_declarator *declarator;
17837 tree prefix_attributes;
17838 tree attributes = NULL;
17839 tree asm_specification;
17840 tree initializer;
17841 tree decl = NULL_TREE;
17842 tree scope;
17843 int is_initialized;
17844 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
17845 initialized with "= ..", CPP_OPEN_PAREN if initialized with
17846 "(...)". */
17847 enum cpp_ttype initialization_kind;
17848 bool is_direct_init = false;
17849 bool is_non_constant_init;
17850 int ctor_dtor_or_conv_p;
17851 bool friend_p = cp_parser_friend_p (decl_specifiers);
17852 tree pushed_scope = NULL_TREE;
17853 bool range_for_decl_p = false;
17854 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17855 location_t tmp_init_loc = UNKNOWN_LOCATION;
17857 /* Gather the attributes that were provided with the
17858 decl-specifiers. */
17859 prefix_attributes = decl_specifiers->attributes;
17861 /* Assume that this is not the declarator for a function
17862 definition. */
17863 if (function_definition_p)
17864 *function_definition_p = false;
17866 /* Default arguments are only permitted for function parameters. */
17867 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
17868 parser->default_arg_ok_p = false;
17870 /* Defer access checks while parsing the declarator; we cannot know
17871 what names are accessible until we know what is being
17872 declared. */
17873 resume_deferring_access_checks ();
17875 /* Parse the declarator. */
17876 token = cp_lexer_peek_token (parser->lexer);
17877 declarator
17878 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17879 &ctor_dtor_or_conv_p,
17880 /*parenthesized_p=*/NULL,
17881 member_p, friend_p);
17882 /* Gather up the deferred checks. */
17883 stop_deferring_access_checks ();
17885 parser->default_arg_ok_p = saved_default_arg_ok_p;
17887 /* If the DECLARATOR was erroneous, there's no need to go
17888 further. */
17889 if (declarator == cp_error_declarator)
17890 return error_mark_node;
17892 /* Check that the number of template-parameter-lists is OK. */
17893 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17894 token->location))
17895 return error_mark_node;
17897 if (declares_class_or_enum & 2)
17898 cp_parser_check_for_definition_in_return_type (declarator,
17899 decl_specifiers->type,
17900 decl_specifiers->locations[ds_type_spec]);
17902 /* Figure out what scope the entity declared by the DECLARATOR is
17903 located in. `grokdeclarator' sometimes changes the scope, so
17904 we compute it now. */
17905 scope = get_scope_of_declarator (declarator);
17907 /* Perform any lookups in the declared type which were thought to be
17908 dependent, but are not in the scope of the declarator. */
17909 decl_specifiers->type
17910 = maybe_update_decl_type (decl_specifiers->type, scope);
17912 /* If we're allowing GNU extensions, look for an
17913 asm-specification. */
17914 if (cp_parser_allow_gnu_extensions_p (parser))
17916 /* Look for an asm-specification. */
17917 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17918 asm_specification = cp_parser_asm_specification_opt (parser);
17920 else
17921 asm_specification = NULL_TREE;
17923 /* Look for attributes. */
17924 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17925 attributes = cp_parser_attributes_opt (parser);
17927 /* Peek at the next token. */
17928 token = cp_lexer_peek_token (parser->lexer);
17930 bool bogus_implicit_tmpl = false;
17932 if (function_declarator_p (declarator))
17934 /* Check to see if the token indicates the start of a
17935 function-definition. */
17936 if (cp_parser_token_starts_function_definition_p (token))
17938 if (!function_definition_allowed_p)
17940 /* If a function-definition should not appear here, issue an
17941 error message. */
17942 cp_parser_error (parser,
17943 "a function-definition is not allowed here");
17944 return error_mark_node;
17947 location_t func_brace_location
17948 = cp_lexer_peek_token (parser->lexer)->location;
17950 /* Neither attributes nor an asm-specification are allowed
17951 on a function-definition. */
17952 if (asm_specification)
17953 error_at (asm_spec_start_token->location,
17954 "an asm-specification is not allowed "
17955 "on a function-definition");
17956 if (attributes)
17957 error_at (attributes_start_token->location,
17958 "attributes are not allowed "
17959 "on a function-definition");
17960 /* This is a function-definition. */
17961 *function_definition_p = true;
17963 /* Parse the function definition. */
17964 if (member_p)
17965 decl = cp_parser_save_member_function_body (parser,
17966 decl_specifiers,
17967 declarator,
17968 prefix_attributes);
17969 else
17970 decl =
17971 (cp_parser_function_definition_from_specifiers_and_declarator
17972 (parser, decl_specifiers, prefix_attributes, declarator));
17974 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17976 /* This is where the prologue starts... */
17977 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17978 = func_brace_location;
17981 return decl;
17984 else if (parser->fully_implicit_function_template_p)
17986 /* A non-template declaration involving a function parameter list
17987 containing an implicit template parameter will be made into a
17988 template. If the resulting declaration is not going to be an
17989 actual function then finish the template scope here to prevent it.
17990 An error message will be issued once we have a decl to talk about.
17992 FIXME probably we should do type deduction rather than create an
17993 implicit template, but the standard currently doesn't allow it. */
17994 bogus_implicit_tmpl = true;
17995 finish_fully_implicit_template (parser, NULL_TREE);
17998 /* [dcl.dcl]
18000 Only in function declarations for constructors, destructors, and
18001 type conversions can the decl-specifier-seq be omitted.
18003 We explicitly postpone this check past the point where we handle
18004 function-definitions because we tolerate function-definitions
18005 that are missing their return types in some modes. */
18006 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18008 cp_parser_error (parser,
18009 "expected constructor, destructor, or type conversion");
18010 return error_mark_node;
18013 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18014 if (token->type == CPP_EQ
18015 || token->type == CPP_OPEN_PAREN
18016 || token->type == CPP_OPEN_BRACE)
18018 is_initialized = SD_INITIALIZED;
18019 initialization_kind = token->type;
18020 if (maybe_range_for_decl)
18021 *maybe_range_for_decl = error_mark_node;
18022 tmp_init_loc = token->location;
18023 if (init_loc && *init_loc == UNKNOWN_LOCATION)
18024 *init_loc = tmp_init_loc;
18026 if (token->type == CPP_EQ
18027 && function_declarator_p (declarator))
18029 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18030 if (t2->keyword == RID_DEFAULT)
18031 is_initialized = SD_DEFAULTED;
18032 else if (t2->keyword == RID_DELETE)
18033 is_initialized = SD_DELETED;
18036 else
18038 /* If the init-declarator isn't initialized and isn't followed by a
18039 `,' or `;', it's not a valid init-declarator. */
18040 if (token->type != CPP_COMMA
18041 && token->type != CPP_SEMICOLON)
18043 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18044 range_for_decl_p = true;
18045 else
18047 if (!maybe_range_for_decl)
18048 cp_parser_error (parser, "expected initializer");
18049 return error_mark_node;
18052 is_initialized = SD_UNINITIALIZED;
18053 initialization_kind = CPP_EOF;
18056 /* Because start_decl has side-effects, we should only call it if we
18057 know we're going ahead. By this point, we know that we cannot
18058 possibly be looking at any other construct. */
18059 cp_parser_commit_to_tentative_parse (parser);
18061 /* Enter the newly declared entry in the symbol table. If we're
18062 processing a declaration in a class-specifier, we wait until
18063 after processing the initializer. */
18064 if (!member_p)
18066 if (parser->in_unbraced_linkage_specification_p)
18067 decl_specifiers->storage_class = sc_extern;
18068 decl = start_decl (declarator, decl_specifiers,
18069 range_for_decl_p? SD_INITIALIZED : is_initialized,
18070 attributes, prefix_attributes, &pushed_scope);
18071 cp_finalize_omp_declare_simd (parser, decl);
18072 cp_finalize_oacc_routine (parser, decl, false);
18073 /* Adjust location of decl if declarator->id_loc is more appropriate:
18074 set, and decl wasn't merged with another decl, in which case its
18075 location would be different from input_location, and more accurate. */
18076 if (DECL_P (decl)
18077 && declarator->id_loc != UNKNOWN_LOCATION
18078 && DECL_SOURCE_LOCATION (decl) == input_location)
18079 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18081 else if (scope)
18082 /* Enter the SCOPE. That way unqualified names appearing in the
18083 initializer will be looked up in SCOPE. */
18084 pushed_scope = push_scope (scope);
18086 /* Perform deferred access control checks, now that we know in which
18087 SCOPE the declared entity resides. */
18088 if (!member_p && decl)
18090 tree saved_current_function_decl = NULL_TREE;
18092 /* If the entity being declared is a function, pretend that we
18093 are in its scope. If it is a `friend', it may have access to
18094 things that would not otherwise be accessible. */
18095 if (TREE_CODE (decl) == FUNCTION_DECL)
18097 saved_current_function_decl = current_function_decl;
18098 current_function_decl = decl;
18101 /* Perform access checks for template parameters. */
18102 cp_parser_perform_template_parameter_access_checks (checks);
18104 /* Perform the access control checks for the declarator and the
18105 decl-specifiers. */
18106 perform_deferred_access_checks (tf_warning_or_error);
18108 /* Restore the saved value. */
18109 if (TREE_CODE (decl) == FUNCTION_DECL)
18110 current_function_decl = saved_current_function_decl;
18113 /* Parse the initializer. */
18114 initializer = NULL_TREE;
18115 is_direct_init = false;
18116 is_non_constant_init = true;
18117 if (is_initialized)
18119 if (function_declarator_p (declarator))
18121 if (initialization_kind == CPP_EQ)
18122 initializer = cp_parser_pure_specifier (parser);
18123 else
18125 /* If the declaration was erroneous, we don't really
18126 know what the user intended, so just silently
18127 consume the initializer. */
18128 if (decl != error_mark_node)
18129 error_at (tmp_init_loc, "initializer provided for function");
18130 cp_parser_skip_to_closing_parenthesis (parser,
18131 /*recovering=*/true,
18132 /*or_comma=*/false,
18133 /*consume_paren=*/true);
18136 else
18138 /* We want to record the extra mangling scope for in-class
18139 initializers of class members and initializers of static data
18140 member templates. The former involves deferring
18141 parsing of the initializer until end of class as with default
18142 arguments. So right here we only handle the latter. */
18143 if (!member_p && processing_template_decl)
18144 start_lambda_scope (decl);
18145 initializer = cp_parser_initializer (parser,
18146 &is_direct_init,
18147 &is_non_constant_init);
18148 if (!member_p && processing_template_decl)
18149 finish_lambda_scope ();
18150 if (initializer == error_mark_node)
18151 cp_parser_skip_to_end_of_statement (parser);
18155 /* The old parser allows attributes to appear after a parenthesized
18156 initializer. Mark Mitchell proposed removing this functionality
18157 on the GCC mailing lists on 2002-08-13. This parser accepts the
18158 attributes -- but ignores them. */
18159 if (cp_parser_allow_gnu_extensions_p (parser)
18160 && initialization_kind == CPP_OPEN_PAREN)
18161 if (cp_parser_attributes_opt (parser))
18162 warning (OPT_Wattributes,
18163 "attributes after parenthesized initializer ignored");
18165 /* And now complain about a non-function implicit template. */
18166 if (bogus_implicit_tmpl && decl != error_mark_node)
18167 error_at (DECL_SOURCE_LOCATION (decl),
18168 "non-function %qD declared as implicit template", decl);
18170 /* For an in-class declaration, use `grokfield' to create the
18171 declaration. */
18172 if (member_p)
18174 if (pushed_scope)
18176 pop_scope (pushed_scope);
18177 pushed_scope = NULL_TREE;
18179 decl = grokfield (declarator, decl_specifiers,
18180 initializer, !is_non_constant_init,
18181 /*asmspec=*/NULL_TREE,
18182 chainon (attributes, prefix_attributes));
18183 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18184 cp_parser_save_default_args (parser, decl);
18185 cp_finalize_omp_declare_simd (parser, decl);
18186 cp_finalize_oacc_routine (parser, decl, false);
18189 /* Finish processing the declaration. But, skip member
18190 declarations. */
18191 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18193 cp_finish_decl (decl,
18194 initializer, !is_non_constant_init,
18195 asm_specification,
18196 /* If the initializer is in parentheses, then this is
18197 a direct-initialization, which means that an
18198 `explicit' constructor is OK. Otherwise, an
18199 `explicit' constructor cannot be used. */
18200 ((is_direct_init || !is_initialized)
18201 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18203 else if ((cxx_dialect != cxx98) && friend_p
18204 && decl && TREE_CODE (decl) == FUNCTION_DECL)
18205 /* Core issue #226 (C++0x only): A default template-argument
18206 shall not be specified in a friend class template
18207 declaration. */
18208 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
18209 /*is_partial=*/false, /*is_friend_decl=*/1);
18211 if (!friend_p && pushed_scope)
18212 pop_scope (pushed_scope);
18214 if (function_declarator_p (declarator)
18215 && parser->fully_implicit_function_template_p)
18217 if (member_p)
18218 decl = finish_fully_implicit_template (parser, decl);
18219 else
18220 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18223 return decl;
18226 /* Parse a declarator.
18228 declarator:
18229 direct-declarator
18230 ptr-operator declarator
18232 abstract-declarator:
18233 ptr-operator abstract-declarator [opt]
18234 direct-abstract-declarator
18236 GNU Extensions:
18238 declarator:
18239 attributes [opt] direct-declarator
18240 attributes [opt] ptr-operator declarator
18242 abstract-declarator:
18243 attributes [opt] ptr-operator abstract-declarator [opt]
18244 attributes [opt] direct-abstract-declarator
18246 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18247 detect constructor, destructor or conversion operators. It is set
18248 to -1 if the declarator is a name, and +1 if it is a
18249 function. Otherwise it is set to zero. Usually you just want to
18250 test for >0, but internally the negative value is used.
18252 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18253 a decl-specifier-seq unless it declares a constructor, destructor,
18254 or conversion. It might seem that we could check this condition in
18255 semantic analysis, rather than parsing, but that makes it difficult
18256 to handle something like `f()'. We want to notice that there are
18257 no decl-specifiers, and therefore realize that this is an
18258 expression, not a declaration.)
18260 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18261 the declarator is a direct-declarator of the form "(...)".
18263 MEMBER_P is true iff this declarator is a member-declarator.
18265 FRIEND_P is true iff this declarator is a friend. */
18267 static cp_declarator *
18268 cp_parser_declarator (cp_parser* parser,
18269 cp_parser_declarator_kind dcl_kind,
18270 int* ctor_dtor_or_conv_p,
18271 bool* parenthesized_p,
18272 bool member_p, bool friend_p)
18274 cp_declarator *declarator;
18275 enum tree_code code;
18276 cp_cv_quals cv_quals;
18277 tree class_type;
18278 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18280 /* Assume this is not a constructor, destructor, or type-conversion
18281 operator. */
18282 if (ctor_dtor_or_conv_p)
18283 *ctor_dtor_or_conv_p = 0;
18285 if (cp_parser_allow_gnu_extensions_p (parser))
18286 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18288 /* Check for the ptr-operator production. */
18289 cp_parser_parse_tentatively (parser);
18290 /* Parse the ptr-operator. */
18291 code = cp_parser_ptr_operator (parser,
18292 &class_type,
18293 &cv_quals,
18294 &std_attributes);
18296 /* If that worked, then we have a ptr-operator. */
18297 if (cp_parser_parse_definitely (parser))
18299 /* If a ptr-operator was found, then this declarator was not
18300 parenthesized. */
18301 if (parenthesized_p)
18302 *parenthesized_p = true;
18303 /* The dependent declarator is optional if we are parsing an
18304 abstract-declarator. */
18305 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18306 cp_parser_parse_tentatively (parser);
18308 /* Parse the dependent declarator. */
18309 declarator = cp_parser_declarator (parser, dcl_kind,
18310 /*ctor_dtor_or_conv_p=*/NULL,
18311 /*parenthesized_p=*/NULL,
18312 /*member_p=*/false,
18313 friend_p);
18315 /* If we are parsing an abstract-declarator, we must handle the
18316 case where the dependent declarator is absent. */
18317 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18318 && !cp_parser_parse_definitely (parser))
18319 declarator = NULL;
18321 declarator = cp_parser_make_indirect_declarator
18322 (code, class_type, cv_quals, declarator, std_attributes);
18324 /* Everything else is a direct-declarator. */
18325 else
18327 if (parenthesized_p)
18328 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18329 CPP_OPEN_PAREN);
18330 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18331 ctor_dtor_or_conv_p,
18332 member_p, friend_p);
18335 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18336 declarator->attributes = gnu_attributes;
18337 return declarator;
18340 /* Parse a direct-declarator or direct-abstract-declarator.
18342 direct-declarator:
18343 declarator-id
18344 direct-declarator ( parameter-declaration-clause )
18345 cv-qualifier-seq [opt]
18346 ref-qualifier [opt]
18347 exception-specification [opt]
18348 direct-declarator [ constant-expression [opt] ]
18349 ( declarator )
18351 direct-abstract-declarator:
18352 direct-abstract-declarator [opt]
18353 ( parameter-declaration-clause )
18354 cv-qualifier-seq [opt]
18355 ref-qualifier [opt]
18356 exception-specification [opt]
18357 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18358 ( abstract-declarator )
18360 Returns a representation of the declarator. DCL_KIND is
18361 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18362 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18363 we are parsing a direct-declarator. It is
18364 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18365 of ambiguity we prefer an abstract declarator, as per
18366 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18367 as for cp_parser_declarator. */
18369 static cp_declarator *
18370 cp_parser_direct_declarator (cp_parser* parser,
18371 cp_parser_declarator_kind dcl_kind,
18372 int* ctor_dtor_or_conv_p,
18373 bool member_p, bool friend_p)
18375 cp_token *token;
18376 cp_declarator *declarator = NULL;
18377 tree scope = NULL_TREE;
18378 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18379 bool saved_in_declarator_p = parser->in_declarator_p;
18380 bool first = true;
18381 tree pushed_scope = NULL_TREE;
18383 while (true)
18385 /* Peek at the next token. */
18386 token = cp_lexer_peek_token (parser->lexer);
18387 if (token->type == CPP_OPEN_PAREN)
18389 /* This is either a parameter-declaration-clause, or a
18390 parenthesized declarator. When we know we are parsing a
18391 named declarator, it must be a parenthesized declarator
18392 if FIRST is true. For instance, `(int)' is a
18393 parameter-declaration-clause, with an omitted
18394 direct-abstract-declarator. But `((*))', is a
18395 parenthesized abstract declarator. Finally, when T is a
18396 template parameter `(T)' is a
18397 parameter-declaration-clause, and not a parenthesized
18398 named declarator.
18400 We first try and parse a parameter-declaration-clause,
18401 and then try a nested declarator (if FIRST is true).
18403 It is not an error for it not to be a
18404 parameter-declaration-clause, even when FIRST is
18405 false. Consider,
18407 int i (int);
18408 int i (3);
18410 The first is the declaration of a function while the
18411 second is the definition of a variable, including its
18412 initializer.
18414 Having seen only the parenthesis, we cannot know which of
18415 these two alternatives should be selected. Even more
18416 complex are examples like:
18418 int i (int (a));
18419 int i (int (3));
18421 The former is a function-declaration; the latter is a
18422 variable initialization.
18424 Thus again, we try a parameter-declaration-clause, and if
18425 that fails, we back out and return. */
18427 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18429 tree params;
18430 bool is_declarator = false;
18432 /* In a member-declarator, the only valid interpretation
18433 of a parenthesis is the start of a
18434 parameter-declaration-clause. (It is invalid to
18435 initialize a static data member with a parenthesized
18436 initializer; only the "=" form of initialization is
18437 permitted.) */
18438 if (!member_p)
18439 cp_parser_parse_tentatively (parser);
18441 /* Consume the `('. */
18442 cp_lexer_consume_token (parser->lexer);
18443 if (first)
18445 /* If this is going to be an abstract declarator, we're
18446 in a declarator and we can't have default args. */
18447 parser->default_arg_ok_p = false;
18448 parser->in_declarator_p = true;
18451 begin_scope (sk_function_parms, NULL_TREE);
18453 /* Parse the parameter-declaration-clause. */
18454 params = cp_parser_parameter_declaration_clause (parser);
18456 /* Consume the `)'. */
18457 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18459 /* If all went well, parse the cv-qualifier-seq,
18460 ref-qualifier and the exception-specification. */
18461 if (member_p || cp_parser_parse_definitely (parser))
18463 cp_cv_quals cv_quals;
18464 cp_virt_specifiers virt_specifiers;
18465 cp_ref_qualifier ref_qual;
18466 tree exception_specification;
18467 tree late_return;
18468 tree attrs;
18469 bool memfn = (member_p || (pushed_scope
18470 && CLASS_TYPE_P (pushed_scope)));
18472 is_declarator = true;
18474 if (ctor_dtor_or_conv_p)
18475 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
18476 first = false;
18478 /* Parse the cv-qualifier-seq. */
18479 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18480 /* Parse the ref-qualifier. */
18481 ref_qual = cp_parser_ref_qualifier_opt (parser);
18482 /* Parse the tx-qualifier. */
18483 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
18484 /* And the exception-specification. */
18485 exception_specification
18486 = cp_parser_exception_specification_opt (parser);
18488 attrs = cp_parser_std_attribute_spec_seq (parser);
18490 /* In here, we handle cases where attribute is used after
18491 the function declaration. For example:
18492 void func (int x) __attribute__((vector(..))); */
18493 if (flag_cilkplus
18494 && cp_next_tokens_can_be_gnu_attribute_p (parser))
18496 cp_parser_parse_tentatively (parser);
18497 tree attr = cp_parser_gnu_attributes_opt (parser);
18498 if (cp_lexer_next_token_is_not (parser->lexer,
18499 CPP_SEMICOLON)
18500 && cp_lexer_next_token_is_not (parser->lexer,
18501 CPP_OPEN_BRACE))
18502 cp_parser_abort_tentative_parse (parser);
18503 else if (!cp_parser_parse_definitely (parser))
18505 else
18506 attrs = chainon (attr, attrs);
18508 tree requires_clause = NULL_TREE;
18509 late_return = (cp_parser_late_return_type_opt
18510 (parser, declarator, requires_clause,
18511 memfn ? cv_quals : -1));
18513 /* Parse the virt-specifier-seq. */
18514 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18516 /* Create the function-declarator. */
18517 declarator = make_call_declarator (declarator,
18518 params,
18519 cv_quals,
18520 virt_specifiers,
18521 ref_qual,
18522 tx_qual,
18523 exception_specification,
18524 late_return,
18525 requires_clause);
18526 declarator->std_attributes = attrs;
18527 /* Any subsequent parameter lists are to do with
18528 return type, so are not those of the declared
18529 function. */
18530 parser->default_arg_ok_p = false;
18533 /* Remove the function parms from scope. */
18534 pop_bindings_and_leave_scope ();
18536 if (is_declarator)
18537 /* Repeat the main loop. */
18538 continue;
18541 /* If this is the first, we can try a parenthesized
18542 declarator. */
18543 if (first)
18545 bool saved_in_type_id_in_expr_p;
18547 parser->default_arg_ok_p = saved_default_arg_ok_p;
18548 parser->in_declarator_p = saved_in_declarator_p;
18550 /* Consume the `('. */
18551 cp_lexer_consume_token (parser->lexer);
18552 /* Parse the nested declarator. */
18553 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18554 parser->in_type_id_in_expr_p = true;
18555 declarator
18556 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
18557 /*parenthesized_p=*/NULL,
18558 member_p, friend_p);
18559 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18560 first = false;
18561 /* Expect a `)'. */
18562 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
18563 declarator = cp_error_declarator;
18564 if (declarator == cp_error_declarator)
18565 break;
18567 goto handle_declarator;
18569 /* Otherwise, we must be done. */
18570 else
18571 break;
18573 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18574 && token->type == CPP_OPEN_SQUARE
18575 && !cp_next_tokens_can_be_attribute_p (parser))
18577 /* Parse an array-declarator. */
18578 tree bounds, attrs;
18580 if (ctor_dtor_or_conv_p)
18581 *ctor_dtor_or_conv_p = 0;
18583 first = false;
18584 parser->default_arg_ok_p = false;
18585 parser->in_declarator_p = true;
18586 /* Consume the `['. */
18587 cp_lexer_consume_token (parser->lexer);
18588 /* Peek at the next token. */
18589 token = cp_lexer_peek_token (parser->lexer);
18590 /* If the next token is `]', then there is no
18591 constant-expression. */
18592 if (token->type != CPP_CLOSE_SQUARE)
18594 bool non_constant_p;
18595 bounds
18596 = cp_parser_constant_expression (parser,
18597 /*allow_non_constant=*/true,
18598 &non_constant_p);
18599 if (!non_constant_p)
18600 /* OK */;
18601 else if (error_operand_p (bounds))
18602 /* Already gave an error. */;
18603 else if (!parser->in_function_body
18604 || current_binding_level->kind == sk_function_parms)
18606 /* Normally, the array bound must be an integral constant
18607 expression. However, as an extension, we allow VLAs
18608 in function scopes as long as they aren't part of a
18609 parameter declaration. */
18610 cp_parser_error (parser,
18611 "array bound is not an integer constant");
18612 bounds = error_mark_node;
18614 else if (processing_template_decl
18615 && !type_dependent_expression_p (bounds))
18617 /* Remember this wasn't a constant-expression. */
18618 bounds = build_nop (TREE_TYPE (bounds), bounds);
18619 TREE_SIDE_EFFECTS (bounds) = 1;
18622 else
18623 bounds = NULL_TREE;
18624 /* Look for the closing `]'. */
18625 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
18627 declarator = cp_error_declarator;
18628 break;
18631 attrs = cp_parser_std_attribute_spec_seq (parser);
18632 declarator = make_array_declarator (declarator, bounds);
18633 declarator->std_attributes = attrs;
18635 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
18638 tree qualifying_scope;
18639 tree unqualified_name;
18640 tree attrs;
18641 special_function_kind sfk;
18642 bool abstract_ok;
18643 bool pack_expansion_p = false;
18644 cp_token *declarator_id_start_token;
18646 /* Parse a declarator-id */
18647 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
18648 if (abstract_ok)
18650 cp_parser_parse_tentatively (parser);
18652 /* If we see an ellipsis, we should be looking at a
18653 parameter pack. */
18654 if (token->type == CPP_ELLIPSIS)
18656 /* Consume the `...' */
18657 cp_lexer_consume_token (parser->lexer);
18659 pack_expansion_p = true;
18663 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
18664 unqualified_name
18665 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
18666 qualifying_scope = parser->scope;
18667 if (abstract_ok)
18669 bool okay = false;
18671 if (!unqualified_name && pack_expansion_p)
18673 /* Check whether an error occurred. */
18674 okay = !cp_parser_error_occurred (parser);
18676 /* We already consumed the ellipsis to mark a
18677 parameter pack, but we have no way to report it,
18678 so abort the tentative parse. We will be exiting
18679 immediately anyway. */
18680 cp_parser_abort_tentative_parse (parser);
18682 else
18683 okay = cp_parser_parse_definitely (parser);
18685 if (!okay)
18686 unqualified_name = error_mark_node;
18687 else if (unqualified_name
18688 && (qualifying_scope
18689 || (!identifier_p (unqualified_name))))
18691 cp_parser_error (parser, "expected unqualified-id");
18692 unqualified_name = error_mark_node;
18696 if (!unqualified_name)
18697 return NULL;
18698 if (unqualified_name == error_mark_node)
18700 declarator = cp_error_declarator;
18701 pack_expansion_p = false;
18702 declarator->parameter_pack_p = false;
18703 break;
18706 attrs = cp_parser_std_attribute_spec_seq (parser);
18708 if (qualifying_scope && at_namespace_scope_p ()
18709 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
18711 /* In the declaration of a member of a template class
18712 outside of the class itself, the SCOPE will sometimes
18713 be a TYPENAME_TYPE. For example, given:
18715 template <typename T>
18716 int S<T>::R::i = 3;
18718 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
18719 this context, we must resolve S<T>::R to an ordinary
18720 type, rather than a typename type.
18722 The reason we normally avoid resolving TYPENAME_TYPEs
18723 is that a specialization of `S' might render
18724 `S<T>::R' not a type. However, if `S' is
18725 specialized, then this `i' will not be used, so there
18726 is no harm in resolving the types here. */
18727 tree type;
18729 /* Resolve the TYPENAME_TYPE. */
18730 type = resolve_typename_type (qualifying_scope,
18731 /*only_current_p=*/false);
18732 /* If that failed, the declarator is invalid. */
18733 if (TREE_CODE (type) == TYPENAME_TYPE)
18735 if (typedef_variant_p (type))
18736 error_at (declarator_id_start_token->location,
18737 "cannot define member of dependent typedef "
18738 "%qT", type);
18739 else
18740 error_at (declarator_id_start_token->location,
18741 "%<%T::%E%> is not a type",
18742 TYPE_CONTEXT (qualifying_scope),
18743 TYPE_IDENTIFIER (qualifying_scope));
18745 qualifying_scope = type;
18748 sfk = sfk_none;
18750 if (unqualified_name)
18752 tree class_type;
18754 if (qualifying_scope
18755 && CLASS_TYPE_P (qualifying_scope))
18756 class_type = qualifying_scope;
18757 else
18758 class_type = current_class_type;
18760 if (TREE_CODE (unqualified_name) == TYPE_DECL)
18762 tree name_type = TREE_TYPE (unqualified_name);
18763 if (class_type && same_type_p (name_type, class_type))
18765 if (qualifying_scope
18766 && CLASSTYPE_USE_TEMPLATE (name_type))
18768 error_at (declarator_id_start_token->location,
18769 "invalid use of constructor as a template");
18770 inform (declarator_id_start_token->location,
18771 "use %<%T::%D%> instead of %<%T::%D%> to "
18772 "name the constructor in a qualified name",
18773 class_type,
18774 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
18775 class_type, name_type);
18776 declarator = cp_error_declarator;
18777 break;
18779 else
18780 unqualified_name = constructor_name (class_type);
18782 else
18784 /* We do not attempt to print the declarator
18785 here because we do not have enough
18786 information about its original syntactic
18787 form. */
18788 cp_parser_error (parser, "invalid declarator");
18789 declarator = cp_error_declarator;
18790 break;
18794 if (class_type)
18796 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
18797 sfk = sfk_destructor;
18798 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
18799 sfk = sfk_conversion;
18800 else if (/* There's no way to declare a constructor
18801 for an anonymous type, even if the type
18802 got a name for linkage purposes. */
18803 !TYPE_WAS_ANONYMOUS (class_type)
18804 /* Handle correctly (c++/19200):
18806 struct S {
18807 struct T{};
18808 friend void S(T);
18811 and also:
18813 namespace N {
18814 void S();
18817 struct S {
18818 friend void N::S();
18819 }; */
18820 && !(friend_p
18821 && class_type != qualifying_scope)
18822 && constructor_name_p (unqualified_name,
18823 class_type))
18825 unqualified_name = constructor_name (class_type);
18826 sfk = sfk_constructor;
18828 else if (is_overloaded_fn (unqualified_name)
18829 && DECL_CONSTRUCTOR_P (get_first_fn
18830 (unqualified_name)))
18831 sfk = sfk_constructor;
18833 if (ctor_dtor_or_conv_p && sfk != sfk_none)
18834 *ctor_dtor_or_conv_p = -1;
18837 declarator = make_id_declarator (qualifying_scope,
18838 unqualified_name,
18839 sfk);
18840 declarator->std_attributes = attrs;
18841 declarator->id_loc = token->location;
18842 declarator->parameter_pack_p = pack_expansion_p;
18844 if (pack_expansion_p)
18845 maybe_warn_variadic_templates ();
18848 handle_declarator:;
18849 scope = get_scope_of_declarator (declarator);
18850 if (scope)
18852 /* Any names that appear after the declarator-id for a
18853 member are looked up in the containing scope. */
18854 if (at_function_scope_p ())
18856 /* But declarations with qualified-ids can't appear in a
18857 function. */
18858 cp_parser_error (parser, "qualified-id in declaration");
18859 declarator = cp_error_declarator;
18860 break;
18862 pushed_scope = push_scope (scope);
18864 parser->in_declarator_p = true;
18865 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
18866 || (declarator && declarator->kind == cdk_id))
18867 /* Default args are only allowed on function
18868 declarations. */
18869 parser->default_arg_ok_p = saved_default_arg_ok_p;
18870 else
18871 parser->default_arg_ok_p = false;
18873 first = false;
18875 /* We're done. */
18876 else
18877 break;
18880 /* For an abstract declarator, we might wind up with nothing at this
18881 point. That's an error; the declarator is not optional. */
18882 if (!declarator)
18883 cp_parser_error (parser, "expected declarator");
18885 /* If we entered a scope, we must exit it now. */
18886 if (pushed_scope)
18887 pop_scope (pushed_scope);
18889 parser->default_arg_ok_p = saved_default_arg_ok_p;
18890 parser->in_declarator_p = saved_in_declarator_p;
18892 return declarator;
18895 /* Parse a ptr-operator.
18897 ptr-operator:
18898 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18899 * cv-qualifier-seq [opt]
18901 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18902 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18904 GNU Extension:
18906 ptr-operator:
18907 & cv-qualifier-seq [opt]
18909 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18910 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18911 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18912 filled in with the TYPE containing the member. *CV_QUALS is
18913 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18914 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18915 Note that the tree codes returned by this function have nothing
18916 to do with the types of trees that will be eventually be created
18917 to represent the pointer or reference type being parsed. They are
18918 just constants with suggestive names. */
18919 static enum tree_code
18920 cp_parser_ptr_operator (cp_parser* parser,
18921 tree* type,
18922 cp_cv_quals *cv_quals,
18923 tree *attributes)
18925 enum tree_code code = ERROR_MARK;
18926 cp_token *token;
18927 tree attrs = NULL_TREE;
18929 /* Assume that it's not a pointer-to-member. */
18930 *type = NULL_TREE;
18931 /* And that there are no cv-qualifiers. */
18932 *cv_quals = TYPE_UNQUALIFIED;
18934 /* Peek at the next token. */
18935 token = cp_lexer_peek_token (parser->lexer);
18937 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18938 if (token->type == CPP_MULT)
18939 code = INDIRECT_REF;
18940 else if (token->type == CPP_AND)
18941 code = ADDR_EXPR;
18942 else if ((cxx_dialect != cxx98) &&
18943 token->type == CPP_AND_AND) /* C++0x only */
18944 code = NON_LVALUE_EXPR;
18946 if (code != ERROR_MARK)
18948 /* Consume the `*', `&' or `&&'. */
18949 cp_lexer_consume_token (parser->lexer);
18951 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18952 `&', if we are allowing GNU extensions. (The only qualifier
18953 that can legally appear after `&' is `restrict', but that is
18954 enforced during semantic analysis. */
18955 if (code == INDIRECT_REF
18956 || cp_parser_allow_gnu_extensions_p (parser))
18957 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18959 attrs = cp_parser_std_attribute_spec_seq (parser);
18960 if (attributes != NULL)
18961 *attributes = attrs;
18963 else
18965 /* Try the pointer-to-member case. */
18966 cp_parser_parse_tentatively (parser);
18967 /* Look for the optional `::' operator. */
18968 cp_parser_global_scope_opt (parser,
18969 /*current_scope_valid_p=*/false);
18970 /* Look for the nested-name specifier. */
18971 token = cp_lexer_peek_token (parser->lexer);
18972 cp_parser_nested_name_specifier (parser,
18973 /*typename_keyword_p=*/false,
18974 /*check_dependency_p=*/true,
18975 /*type_p=*/false,
18976 /*is_declaration=*/false);
18977 /* If we found it, and the next token is a `*', then we are
18978 indeed looking at a pointer-to-member operator. */
18979 if (!cp_parser_error_occurred (parser)
18980 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18982 /* Indicate that the `*' operator was used. */
18983 code = INDIRECT_REF;
18985 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18986 error_at (token->location, "%qD is a namespace", parser->scope);
18987 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18988 error_at (token->location, "cannot form pointer to member of "
18989 "non-class %q#T", parser->scope);
18990 else
18992 /* The type of which the member is a member is given by the
18993 current SCOPE. */
18994 *type = parser->scope;
18995 /* The next name will not be qualified. */
18996 parser->scope = NULL_TREE;
18997 parser->qualifying_scope = NULL_TREE;
18998 parser->object_scope = NULL_TREE;
18999 /* Look for optional c++11 attributes. */
19000 attrs = cp_parser_std_attribute_spec_seq (parser);
19001 if (attributes != NULL)
19002 *attributes = attrs;
19003 /* Look for the optional cv-qualifier-seq. */
19004 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19007 /* If that didn't work we don't have a ptr-operator. */
19008 if (!cp_parser_parse_definitely (parser))
19009 cp_parser_error (parser, "expected ptr-operator");
19012 return code;
19015 /* Parse an (optional) cv-qualifier-seq.
19017 cv-qualifier-seq:
19018 cv-qualifier cv-qualifier-seq [opt]
19020 cv-qualifier:
19021 const
19022 volatile
19024 GNU Extension:
19026 cv-qualifier:
19027 __restrict__
19029 Returns a bitmask representing the cv-qualifiers. */
19031 static cp_cv_quals
19032 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19034 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19036 while (true)
19038 cp_token *token;
19039 cp_cv_quals cv_qualifier;
19041 /* Peek at the next token. */
19042 token = cp_lexer_peek_token (parser->lexer);
19043 /* See if it's a cv-qualifier. */
19044 switch (token->keyword)
19046 case RID_CONST:
19047 cv_qualifier = TYPE_QUAL_CONST;
19048 break;
19050 case RID_VOLATILE:
19051 cv_qualifier = TYPE_QUAL_VOLATILE;
19052 break;
19054 case RID_RESTRICT:
19055 cv_qualifier = TYPE_QUAL_RESTRICT;
19056 break;
19058 default:
19059 cv_qualifier = TYPE_UNQUALIFIED;
19060 break;
19063 if (!cv_qualifier)
19064 break;
19066 if (cv_quals & cv_qualifier)
19068 error_at (token->location, "duplicate cv-qualifier");
19069 cp_lexer_purge_token (parser->lexer);
19071 else
19073 cp_lexer_consume_token (parser->lexer);
19074 cv_quals |= cv_qualifier;
19078 return cv_quals;
19081 /* Parse an (optional) ref-qualifier
19083 ref-qualifier:
19087 Returns cp_ref_qualifier representing ref-qualifier. */
19089 static cp_ref_qualifier
19090 cp_parser_ref_qualifier_opt (cp_parser* parser)
19092 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19094 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19095 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19096 return ref_qual;
19098 while (true)
19100 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19101 cp_token *token = cp_lexer_peek_token (parser->lexer);
19103 switch (token->type)
19105 case CPP_AND:
19106 curr_ref_qual = REF_QUAL_LVALUE;
19107 break;
19109 case CPP_AND_AND:
19110 curr_ref_qual = REF_QUAL_RVALUE;
19111 break;
19113 default:
19114 curr_ref_qual = REF_QUAL_NONE;
19115 break;
19118 if (!curr_ref_qual)
19119 break;
19120 else if (ref_qual)
19122 error_at (token->location, "multiple ref-qualifiers");
19123 cp_lexer_purge_token (parser->lexer);
19125 else
19127 ref_qual = curr_ref_qual;
19128 cp_lexer_consume_token (parser->lexer);
19132 return ref_qual;
19135 /* Parse an optional tx-qualifier.
19137 tx-qualifier:
19138 transaction_safe
19139 transaction_safe_dynamic */
19141 static tree
19142 cp_parser_tx_qualifier_opt (cp_parser *parser)
19144 cp_token *token = cp_lexer_peek_token (parser->lexer);
19145 if (token->type == CPP_NAME)
19147 tree name = token->u.value;
19148 const char *p = IDENTIFIER_POINTER (name);
19149 const int len = strlen ("transaction_safe");
19150 if (!strncmp (p, "transaction_safe", len))
19152 p += len;
19153 if (*p == '\0'
19154 || !strcmp (p, "_dynamic"))
19156 cp_lexer_consume_token (parser->lexer);
19157 if (!flag_tm)
19159 error ("%E requires %<-fgnu-tm%>", name);
19160 return NULL_TREE;
19162 else
19163 return name;
19167 return NULL_TREE;
19170 /* Parse an (optional) virt-specifier-seq.
19172 virt-specifier-seq:
19173 virt-specifier virt-specifier-seq [opt]
19175 virt-specifier:
19176 override
19177 final
19179 Returns a bitmask representing the virt-specifiers. */
19181 static cp_virt_specifiers
19182 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19184 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19186 while (true)
19188 cp_token *token;
19189 cp_virt_specifiers virt_specifier;
19191 /* Peek at the next token. */
19192 token = cp_lexer_peek_token (parser->lexer);
19193 /* See if it's a virt-specifier-qualifier. */
19194 if (token->type != CPP_NAME)
19195 break;
19196 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19198 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19199 virt_specifier = VIRT_SPEC_OVERRIDE;
19201 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19203 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19204 virt_specifier = VIRT_SPEC_FINAL;
19206 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19208 virt_specifier = VIRT_SPEC_FINAL;
19210 else
19211 break;
19213 if (virt_specifiers & virt_specifier)
19215 error_at (token->location, "duplicate virt-specifier");
19216 cp_lexer_purge_token (parser->lexer);
19218 else
19220 cp_lexer_consume_token (parser->lexer);
19221 virt_specifiers |= virt_specifier;
19224 return virt_specifiers;
19227 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19228 is in scope even though it isn't real. */
19230 void
19231 inject_this_parameter (tree ctype, cp_cv_quals quals)
19233 tree this_parm;
19235 if (current_class_ptr)
19237 /* We don't clear this between NSDMIs. Is it already what we want? */
19238 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19239 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19240 && cp_type_quals (type) == quals)
19241 return;
19244 this_parm = build_this_parm (ctype, quals);
19245 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19246 current_class_ptr = NULL_TREE;
19247 current_class_ref
19248 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19249 current_class_ptr = this_parm;
19252 /* Return true iff our current scope is a non-static data member
19253 initializer. */
19255 bool
19256 parsing_nsdmi (void)
19258 /* We recognize NSDMI context by the context-less 'this' pointer set up
19259 by the function above. */
19260 if (current_class_ptr
19261 && TREE_CODE (current_class_ptr) == PARM_DECL
19262 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19263 return true;
19264 return false;
19267 /* Parse a late-specified return type, if any. This is not a separate
19268 non-terminal, but part of a function declarator, which looks like
19270 -> trailing-type-specifier-seq abstract-declarator(opt)
19272 Returns the type indicated by the type-id.
19274 In addition to this, parse any queued up omp declare simd
19275 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19277 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19278 function. */
19280 static tree
19281 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19282 tree& requires_clause, cp_cv_quals quals)
19284 cp_token *token;
19285 tree type = NULL_TREE;
19286 bool declare_simd_p = (parser->omp_declare_simd
19287 && declarator
19288 && declarator->kind == cdk_id);
19290 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
19291 && declarator && declarator->kind == cdk_id);
19293 bool oacc_routine_p = (parser->oacc_routine
19294 && declarator
19295 && declarator->kind == cdk_id);
19297 /* Peek at the next token. */
19298 token = cp_lexer_peek_token (parser->lexer);
19299 /* A late-specified return type is indicated by an initial '->'. */
19300 if (token->type != CPP_DEREF
19301 && token->keyword != RID_REQUIRES
19302 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
19303 return NULL_TREE;
19305 tree save_ccp = current_class_ptr;
19306 tree save_ccr = current_class_ref;
19307 if (quals >= 0)
19309 /* DR 1207: 'this' is in scope in the trailing return type. */
19310 inject_this_parameter (current_class_type, quals);
19313 if (token->type == CPP_DEREF)
19315 /* Consume the ->. */
19316 cp_lexer_consume_token (parser->lexer);
19318 type = cp_parser_trailing_type_id (parser);
19321 /* Function declarations may be followed by a trailing
19322 requires-clause. */
19323 requires_clause = cp_parser_requires_clause_opt (parser);
19325 if (cilk_simd_fn_vector_p)
19326 declarator->std_attributes
19327 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19328 declarator->std_attributes);
19329 if (declare_simd_p)
19330 declarator->std_attributes
19331 = cp_parser_late_parsing_omp_declare_simd (parser,
19332 declarator->std_attributes);
19333 if (oacc_routine_p)
19334 declarator->std_attributes
19335 = cp_parser_late_parsing_oacc_routine (parser,
19336 declarator->std_attributes);
19338 if (quals >= 0)
19340 current_class_ptr = save_ccp;
19341 current_class_ref = save_ccr;
19344 return type;
19347 /* Parse a declarator-id.
19349 declarator-id:
19350 id-expression
19351 :: [opt] nested-name-specifier [opt] type-name
19353 In the `id-expression' case, the value returned is as for
19354 cp_parser_id_expression if the id-expression was an unqualified-id.
19355 If the id-expression was a qualified-id, then a SCOPE_REF is
19356 returned. The first operand is the scope (either a NAMESPACE_DECL
19357 or TREE_TYPE), but the second is still just a representation of an
19358 unqualified-id. */
19360 static tree
19361 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19363 tree id;
19364 /* The expression must be an id-expression. Assume that qualified
19365 names are the names of types so that:
19367 template <class T>
19368 int S<T>::R::i = 3;
19370 will work; we must treat `S<T>::R' as the name of a type.
19371 Similarly, assume that qualified names are templates, where
19372 required, so that:
19374 template <class T>
19375 int S<T>::R<T>::i = 3;
19377 will work, too. */
19378 id = cp_parser_id_expression (parser,
19379 /*template_keyword_p=*/false,
19380 /*check_dependency_p=*/false,
19381 /*template_p=*/NULL,
19382 /*declarator_p=*/true,
19383 optional_p);
19384 if (id && BASELINK_P (id))
19385 id = BASELINK_FUNCTIONS (id);
19386 return id;
19389 /* Parse a type-id.
19391 type-id:
19392 type-specifier-seq abstract-declarator [opt]
19394 Returns the TYPE specified. */
19396 static tree
19397 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19398 bool is_trailing_return)
19400 cp_decl_specifier_seq type_specifier_seq;
19401 cp_declarator *abstract_declarator;
19403 /* Parse the type-specifier-seq. */
19404 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19405 is_trailing_return,
19406 &type_specifier_seq);
19407 if (type_specifier_seq.type == error_mark_node)
19408 return error_mark_node;
19410 /* There might or might not be an abstract declarator. */
19411 cp_parser_parse_tentatively (parser);
19412 /* Look for the declarator. */
19413 abstract_declarator
19414 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19415 /*parenthesized_p=*/NULL,
19416 /*member_p=*/false,
19417 /*friend_p=*/false);
19418 /* Check to see if there really was a declarator. */
19419 if (!cp_parser_parse_definitely (parser))
19420 abstract_declarator = NULL;
19422 if (type_specifier_seq.type
19423 /* The concepts TS allows 'auto' as a type-id. */
19424 && !flag_concepts
19425 /* None of the valid uses of 'auto' in C++14 involve the type-id
19426 nonterminal, but it is valid in a trailing-return-type. */
19427 && !(cxx_dialect >= cxx14 && is_trailing_return)
19428 && type_uses_auto (type_specifier_seq.type))
19430 /* A type-id with type 'auto' is only ok if the abstract declarator
19431 is a function declarator with a late-specified return type.
19433 A type-id with 'auto' is also valid in a trailing-return-type
19434 in a compound-requirement. */
19435 if (abstract_declarator
19436 && abstract_declarator->kind == cdk_function
19437 && abstract_declarator->u.function.late_return_type)
19438 /* OK */;
19439 else if (parser->in_result_type_constraint_p)
19440 /* OK */;
19441 else
19443 error ("invalid use of %<auto%>");
19444 return error_mark_node;
19448 return groktypename (&type_specifier_seq, abstract_declarator,
19449 is_template_arg);
19452 static tree
19453 cp_parser_type_id (cp_parser *parser)
19455 return cp_parser_type_id_1 (parser, false, false);
19458 static tree
19459 cp_parser_template_type_arg (cp_parser *parser)
19461 tree r;
19462 const char *saved_message = parser->type_definition_forbidden_message;
19463 parser->type_definition_forbidden_message
19464 = G_("types may not be defined in template arguments");
19465 r = cp_parser_type_id_1 (parser, true, false);
19466 parser->type_definition_forbidden_message = saved_message;
19467 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
19469 error ("invalid use of %<auto%> in template argument");
19470 r = error_mark_node;
19472 return r;
19475 static tree
19476 cp_parser_trailing_type_id (cp_parser *parser)
19478 return cp_parser_type_id_1 (parser, false, true);
19481 /* Parse a type-specifier-seq.
19483 type-specifier-seq:
19484 type-specifier type-specifier-seq [opt]
19486 GNU extension:
19488 type-specifier-seq:
19489 attributes type-specifier-seq [opt]
19491 If IS_DECLARATION is true, we are at the start of a "condition" or
19492 exception-declaration, so we might be followed by a declarator-id.
19494 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
19495 i.e. we've just seen "->".
19497 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
19499 static void
19500 cp_parser_type_specifier_seq (cp_parser* parser,
19501 bool is_declaration,
19502 bool is_trailing_return,
19503 cp_decl_specifier_seq *type_specifier_seq)
19505 bool seen_type_specifier = false;
19506 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
19507 cp_token *start_token = NULL;
19509 /* Clear the TYPE_SPECIFIER_SEQ. */
19510 clear_decl_specs (type_specifier_seq);
19512 /* In the context of a trailing return type, enum E { } is an
19513 elaborated-type-specifier followed by a function-body, not an
19514 enum-specifier. */
19515 if (is_trailing_return)
19516 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
19518 /* Parse the type-specifiers and attributes. */
19519 while (true)
19521 tree type_specifier;
19522 bool is_cv_qualifier;
19524 /* Check for attributes first. */
19525 if (cp_next_tokens_can_be_attribute_p (parser))
19527 type_specifier_seq->attributes =
19528 chainon (type_specifier_seq->attributes,
19529 cp_parser_attributes_opt (parser));
19530 continue;
19533 /* record the token of the beginning of the type specifier seq,
19534 for error reporting purposes*/
19535 if (!start_token)
19536 start_token = cp_lexer_peek_token (parser->lexer);
19538 /* Look for the type-specifier. */
19539 type_specifier = cp_parser_type_specifier (parser,
19540 flags,
19541 type_specifier_seq,
19542 /*is_declaration=*/false,
19543 NULL,
19544 &is_cv_qualifier);
19545 if (!type_specifier)
19547 /* If the first type-specifier could not be found, this is not a
19548 type-specifier-seq at all. */
19549 if (!seen_type_specifier)
19551 /* Set in_declarator_p to avoid skipping to the semicolon. */
19552 int in_decl = parser->in_declarator_p;
19553 parser->in_declarator_p = true;
19555 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
19556 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
19557 cp_parser_error (parser, "expected type-specifier");
19559 parser->in_declarator_p = in_decl;
19561 type_specifier_seq->type = error_mark_node;
19562 return;
19564 /* If subsequent type-specifiers could not be found, the
19565 type-specifier-seq is complete. */
19566 break;
19569 seen_type_specifier = true;
19570 /* The standard says that a condition can be:
19572 type-specifier-seq declarator = assignment-expression
19574 However, given:
19576 struct S {};
19577 if (int S = ...)
19579 we should treat the "S" as a declarator, not as a
19580 type-specifier. The standard doesn't say that explicitly for
19581 type-specifier-seq, but it does say that for
19582 decl-specifier-seq in an ordinary declaration. Perhaps it
19583 would be clearer just to allow a decl-specifier-seq here, and
19584 then add a semantic restriction that if any decl-specifiers
19585 that are not type-specifiers appear, the program is invalid. */
19586 if (is_declaration && !is_cv_qualifier)
19587 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
19591 /* Return whether the function currently being declared has an associated
19592 template parameter list. */
19594 static bool
19595 function_being_declared_is_template_p (cp_parser* parser)
19597 if (!current_template_parms || processing_template_parmlist)
19598 return false;
19600 if (parser->implicit_template_scope)
19601 return true;
19603 if (at_class_scope_p ()
19604 && TYPE_BEING_DEFINED (current_class_type))
19605 return parser->num_template_parameter_lists != 0;
19607 return ((int) parser->num_template_parameter_lists > template_class_depth
19608 (current_class_type));
19611 /* Parse a parameter-declaration-clause.
19613 parameter-declaration-clause:
19614 parameter-declaration-list [opt] ... [opt]
19615 parameter-declaration-list , ...
19617 Returns a representation for the parameter declarations. A return
19618 value of NULL indicates a parameter-declaration-clause consisting
19619 only of an ellipsis. */
19621 static tree
19622 cp_parser_parameter_declaration_clause (cp_parser* parser)
19624 tree parameters;
19625 cp_token *token;
19626 bool ellipsis_p;
19627 bool is_error;
19629 struct cleanup {
19630 cp_parser* parser;
19631 int auto_is_implicit_function_template_parm_p;
19632 ~cleanup() {
19633 parser->auto_is_implicit_function_template_parm_p
19634 = auto_is_implicit_function_template_parm_p;
19636 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
19638 (void) cleanup;
19640 if (!processing_specialization
19641 && !processing_template_parmlist
19642 && !processing_explicit_instantiation)
19643 if (!current_function_decl
19644 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
19645 parser->auto_is_implicit_function_template_parm_p = true;
19647 /* Peek at the next token. */
19648 token = cp_lexer_peek_token (parser->lexer);
19649 /* Check for trivial parameter-declaration-clauses. */
19650 if (token->type == CPP_ELLIPSIS)
19652 /* Consume the `...' token. */
19653 cp_lexer_consume_token (parser->lexer);
19654 return NULL_TREE;
19656 else if (token->type == CPP_CLOSE_PAREN)
19657 /* There are no parameters. */
19659 #ifndef NO_IMPLICIT_EXTERN_C
19660 if (in_system_header_at (input_location)
19661 && current_class_type == NULL
19662 && current_lang_name == lang_name_c)
19663 return NULL_TREE;
19664 else
19665 #endif
19666 return void_list_node;
19668 /* Check for `(void)', too, which is a special case. */
19669 else if (token->keyword == RID_VOID
19670 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19671 == CPP_CLOSE_PAREN))
19673 /* Consume the `void' token. */
19674 cp_lexer_consume_token (parser->lexer);
19675 /* There are no parameters. */
19676 return void_list_node;
19679 /* Parse the parameter-declaration-list. */
19680 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
19681 /* If a parse error occurred while parsing the
19682 parameter-declaration-list, then the entire
19683 parameter-declaration-clause is erroneous. */
19684 if (is_error)
19685 return NULL;
19687 /* Peek at the next token. */
19688 token = cp_lexer_peek_token (parser->lexer);
19689 /* If it's a `,', the clause should terminate with an ellipsis. */
19690 if (token->type == CPP_COMMA)
19692 /* Consume the `,'. */
19693 cp_lexer_consume_token (parser->lexer);
19694 /* Expect an ellipsis. */
19695 ellipsis_p
19696 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
19698 /* It might also be `...' if the optional trailing `,' was
19699 omitted. */
19700 else if (token->type == CPP_ELLIPSIS)
19702 /* Consume the `...' token. */
19703 cp_lexer_consume_token (parser->lexer);
19704 /* And remember that we saw it. */
19705 ellipsis_p = true;
19707 else
19708 ellipsis_p = false;
19710 /* Finish the parameter list. */
19711 if (!ellipsis_p)
19712 parameters = chainon (parameters, void_list_node);
19714 return parameters;
19717 /* Parse a parameter-declaration-list.
19719 parameter-declaration-list:
19720 parameter-declaration
19721 parameter-declaration-list , parameter-declaration
19723 Returns a representation of the parameter-declaration-list, as for
19724 cp_parser_parameter_declaration_clause. However, the
19725 `void_list_node' is never appended to the list. Upon return,
19726 *IS_ERROR will be true iff an error occurred. */
19728 static tree
19729 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
19731 tree parameters = NULL_TREE;
19732 tree *tail = &parameters;
19733 bool saved_in_unbraced_linkage_specification_p;
19734 int index = 0;
19736 /* Assume all will go well. */
19737 *is_error = false;
19738 /* The special considerations that apply to a function within an
19739 unbraced linkage specifications do not apply to the parameters
19740 to the function. */
19741 saved_in_unbraced_linkage_specification_p
19742 = parser->in_unbraced_linkage_specification_p;
19743 parser->in_unbraced_linkage_specification_p = false;
19745 /* Look for more parameters. */
19746 while (true)
19748 cp_parameter_declarator *parameter;
19749 tree decl = error_mark_node;
19750 bool parenthesized_p = false;
19751 int template_parm_idx = (function_being_declared_is_template_p (parser)?
19752 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
19753 (current_template_parms)) : 0);
19755 /* Parse the parameter. */
19756 parameter
19757 = cp_parser_parameter_declaration (parser,
19758 /*template_parm_p=*/false,
19759 &parenthesized_p);
19761 /* We don't know yet if the enclosing context is deprecated, so wait
19762 and warn in grokparms if appropriate. */
19763 deprecated_state = DEPRECATED_SUPPRESS;
19765 if (parameter)
19767 /* If a function parameter pack was specified and an implicit template
19768 parameter was introduced during cp_parser_parameter_declaration,
19769 change any implicit parameters introduced into packs. */
19770 if (parser->implicit_template_parms
19771 && parameter->declarator
19772 && parameter->declarator->parameter_pack_p)
19774 int latest_template_parm_idx = TREE_VEC_LENGTH
19775 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
19777 if (latest_template_parm_idx != template_parm_idx)
19778 parameter->decl_specifiers.type = convert_generic_types_to_packs
19779 (parameter->decl_specifiers.type,
19780 template_parm_idx, latest_template_parm_idx);
19783 decl = grokdeclarator (parameter->declarator,
19784 &parameter->decl_specifiers,
19785 PARM,
19786 parameter->default_argument != NULL_TREE,
19787 &parameter->decl_specifiers.attributes);
19790 deprecated_state = DEPRECATED_NORMAL;
19792 /* If a parse error occurred parsing the parameter declaration,
19793 then the entire parameter-declaration-list is erroneous. */
19794 if (decl == error_mark_node)
19796 *is_error = true;
19797 parameters = error_mark_node;
19798 break;
19801 if (parameter->decl_specifiers.attributes)
19802 cplus_decl_attributes (&decl,
19803 parameter->decl_specifiers.attributes,
19805 if (DECL_NAME (decl))
19806 decl = pushdecl (decl);
19808 if (decl != error_mark_node)
19810 retrofit_lang_decl (decl);
19811 DECL_PARM_INDEX (decl) = ++index;
19812 DECL_PARM_LEVEL (decl) = function_parm_depth ();
19815 /* Add the new parameter to the list. */
19816 *tail = build_tree_list (parameter->default_argument, decl);
19817 tail = &TREE_CHAIN (*tail);
19819 /* Peek at the next token. */
19820 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
19821 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
19822 /* These are for Objective-C++ */
19823 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19824 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19825 /* The parameter-declaration-list is complete. */
19826 break;
19827 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19829 cp_token *token;
19831 /* Peek at the next token. */
19832 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19833 /* If it's an ellipsis, then the list is complete. */
19834 if (token->type == CPP_ELLIPSIS)
19835 break;
19836 /* Otherwise, there must be more parameters. Consume the
19837 `,'. */
19838 cp_lexer_consume_token (parser->lexer);
19839 /* When parsing something like:
19841 int i(float f, double d)
19843 we can tell after seeing the declaration for "f" that we
19844 are not looking at an initialization of a variable "i",
19845 but rather at the declaration of a function "i".
19847 Due to the fact that the parsing of template arguments
19848 (as specified to a template-id) requires backtracking we
19849 cannot use this technique when inside a template argument
19850 list. */
19851 if (!parser->in_template_argument_list_p
19852 && !parser->in_type_id_in_expr_p
19853 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19854 /* However, a parameter-declaration of the form
19855 "float(f)" (which is a valid declaration of a
19856 parameter "f") can also be interpreted as an
19857 expression (the conversion of "f" to "float"). */
19858 && !parenthesized_p)
19859 cp_parser_commit_to_tentative_parse (parser);
19861 else
19863 cp_parser_error (parser, "expected %<,%> or %<...%>");
19864 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19865 cp_parser_skip_to_closing_parenthesis (parser,
19866 /*recovering=*/true,
19867 /*or_comma=*/false,
19868 /*consume_paren=*/false);
19869 break;
19873 parser->in_unbraced_linkage_specification_p
19874 = saved_in_unbraced_linkage_specification_p;
19876 /* Reset implicit_template_scope if we are about to leave the function
19877 parameter list that introduced it. Note that for out-of-line member
19878 definitions, there will be one or more class scopes before we get to
19879 the template parameter scope. */
19881 if (cp_binding_level *its = parser->implicit_template_scope)
19882 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
19884 while (maybe_its->kind == sk_class)
19885 maybe_its = maybe_its->level_chain;
19886 if (maybe_its == its)
19888 parser->implicit_template_parms = 0;
19889 parser->implicit_template_scope = 0;
19893 return parameters;
19896 /* Parse a parameter declaration.
19898 parameter-declaration:
19899 decl-specifier-seq ... [opt] declarator
19900 decl-specifier-seq declarator = assignment-expression
19901 decl-specifier-seq ... [opt] abstract-declarator [opt]
19902 decl-specifier-seq abstract-declarator [opt] = assignment-expression
19904 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
19905 declares a template parameter. (In that case, a non-nested `>'
19906 token encountered during the parsing of the assignment-expression
19907 is not interpreted as a greater-than operator.)
19909 Returns a representation of the parameter, or NULL if an error
19910 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
19911 true iff the declarator is of the form "(p)". */
19913 static cp_parameter_declarator *
19914 cp_parser_parameter_declaration (cp_parser *parser,
19915 bool template_parm_p,
19916 bool *parenthesized_p)
19918 int declares_class_or_enum;
19919 cp_decl_specifier_seq decl_specifiers;
19920 cp_declarator *declarator;
19921 tree default_argument;
19922 cp_token *token = NULL, *declarator_token_start = NULL;
19923 const char *saved_message;
19924 bool template_parameter_pack_p = false;
19926 /* In a template parameter, `>' is not an operator.
19928 [temp.param]
19930 When parsing a default template-argument for a non-type
19931 template-parameter, the first non-nested `>' is taken as the end
19932 of the template parameter-list rather than a greater-than
19933 operator. */
19935 /* Type definitions may not appear in parameter types. */
19936 saved_message = parser->type_definition_forbidden_message;
19937 parser->type_definition_forbidden_message
19938 = G_("types may not be defined in parameter types");
19940 /* Parse the declaration-specifiers. */
19941 cp_parser_decl_specifier_seq (parser,
19942 CP_PARSER_FLAGS_NONE,
19943 &decl_specifiers,
19944 &declares_class_or_enum);
19946 /* Complain about missing 'typename' or other invalid type names. */
19947 if (!decl_specifiers.any_type_specifiers_p
19948 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19949 decl_specifiers.type = error_mark_node;
19951 /* If an error occurred, there's no reason to attempt to parse the
19952 rest of the declaration. */
19953 if (cp_parser_error_occurred (parser))
19955 parser->type_definition_forbidden_message = saved_message;
19956 return NULL;
19959 /* Peek at the next token. */
19960 token = cp_lexer_peek_token (parser->lexer);
19962 /* If the next token is a `)', `,', `=', `>', or `...', then there
19963 is no declarator. However, when variadic templates are enabled,
19964 there may be a declarator following `...'. */
19965 if (token->type == CPP_CLOSE_PAREN
19966 || token->type == CPP_COMMA
19967 || token->type == CPP_EQ
19968 || token->type == CPP_GREATER)
19970 declarator = NULL;
19971 if (parenthesized_p)
19972 *parenthesized_p = false;
19974 /* Otherwise, there should be a declarator. */
19975 else
19977 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19978 parser->default_arg_ok_p = false;
19980 /* After seeing a decl-specifier-seq, if the next token is not a
19981 "(", there is no possibility that the code is a valid
19982 expression. Therefore, if parsing tentatively, we commit at
19983 this point. */
19984 if (!parser->in_template_argument_list_p
19985 /* In an expression context, having seen:
19987 (int((char ...
19989 we cannot be sure whether we are looking at a
19990 function-type (taking a "char" as a parameter) or a cast
19991 of some object of type "char" to "int". */
19992 && !parser->in_type_id_in_expr_p
19993 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19994 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19995 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19996 cp_parser_commit_to_tentative_parse (parser);
19997 /* Parse the declarator. */
19998 declarator_token_start = token;
19999 declarator = cp_parser_declarator (parser,
20000 CP_PARSER_DECLARATOR_EITHER,
20001 /*ctor_dtor_or_conv_p=*/NULL,
20002 parenthesized_p,
20003 /*member_p=*/false,
20004 /*friend_p=*/false);
20005 parser->default_arg_ok_p = saved_default_arg_ok_p;
20006 /* After the declarator, allow more attributes. */
20007 decl_specifiers.attributes
20008 = chainon (decl_specifiers.attributes,
20009 cp_parser_attributes_opt (parser));
20011 /* If the declarator is a template parameter pack, remember that and
20012 clear the flag in the declarator itself so we don't get errors
20013 from grokdeclarator. */
20014 if (template_parm_p && declarator && declarator->parameter_pack_p)
20016 declarator->parameter_pack_p = false;
20017 template_parameter_pack_p = true;
20021 /* If the next token is an ellipsis, and we have not seen a declarator
20022 name, and if either the type of the declarator contains parameter
20023 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20024 for, eg, abbreviated integral type names), then we actually have a
20025 parameter pack expansion expression. Otherwise, leave the ellipsis
20026 for a C-style variadic function. */
20027 token = cp_lexer_peek_token (parser->lexer);
20028 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20030 tree type = decl_specifiers.type;
20032 if (type && DECL_P (type))
20033 type = TREE_TYPE (type);
20035 if (((type
20036 && TREE_CODE (type) != TYPE_PACK_EXPANSION
20037 && (template_parm_p || uses_parameter_packs (type)))
20038 || (!type && template_parm_p))
20039 && declarator_can_be_parameter_pack (declarator))
20041 /* Consume the `...'. */
20042 cp_lexer_consume_token (parser->lexer);
20043 maybe_warn_variadic_templates ();
20045 /* Build a pack expansion type */
20046 if (template_parm_p)
20047 template_parameter_pack_p = true;
20048 else if (declarator)
20049 declarator->parameter_pack_p = true;
20050 else
20051 decl_specifiers.type = make_pack_expansion (type);
20055 /* The restriction on defining new types applies only to the type
20056 of the parameter, not to the default argument. */
20057 parser->type_definition_forbidden_message = saved_message;
20059 /* If the next token is `=', then process a default argument. */
20060 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20062 tree type = decl_specifiers.type;
20063 token = cp_lexer_peek_token (parser->lexer);
20064 /* If we are defining a class, then the tokens that make up the
20065 default argument must be saved and processed later. */
20066 if (!template_parm_p && at_class_scope_p ()
20067 && TYPE_BEING_DEFINED (current_class_type)
20068 && !LAMBDA_TYPE_P (current_class_type))
20069 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20071 // A constrained-type-specifier may declare a type template-parameter.
20072 else if (declares_constrained_type_template_parameter (type))
20073 default_argument
20074 = cp_parser_default_type_template_argument (parser);
20076 // A constrained-type-specifier may declare a template-template-parameter.
20077 else if (declares_constrained_template_template_parameter (type))
20078 default_argument
20079 = cp_parser_default_template_template_argument (parser);
20081 /* Outside of a class definition, we can just parse the
20082 assignment-expression. */
20083 else
20084 default_argument
20085 = cp_parser_default_argument (parser, template_parm_p);
20087 if (!parser->default_arg_ok_p)
20089 permerror (token->location,
20090 "default arguments are only "
20091 "permitted for function parameters");
20093 else if ((declarator && declarator->parameter_pack_p)
20094 || template_parameter_pack_p
20095 || (decl_specifiers.type
20096 && PACK_EXPANSION_P (decl_specifiers.type)))
20098 /* Find the name of the parameter pack. */
20099 cp_declarator *id_declarator = declarator;
20100 while (id_declarator && id_declarator->kind != cdk_id)
20101 id_declarator = id_declarator->declarator;
20103 if (id_declarator && id_declarator->kind == cdk_id)
20104 error_at (declarator_token_start->location,
20105 template_parm_p
20106 ? G_("template parameter pack %qD "
20107 "cannot have a default argument")
20108 : G_("parameter pack %qD cannot have "
20109 "a default argument"),
20110 id_declarator->u.id.unqualified_name);
20111 else
20112 error_at (declarator_token_start->location,
20113 template_parm_p
20114 ? G_("template parameter pack cannot have "
20115 "a default argument")
20116 : G_("parameter pack cannot have a "
20117 "default argument"));
20119 default_argument = NULL_TREE;
20122 else
20123 default_argument = NULL_TREE;
20125 return make_parameter_declarator (&decl_specifiers,
20126 declarator,
20127 default_argument,
20128 template_parameter_pack_p);
20131 /* Parse a default argument and return it.
20133 TEMPLATE_PARM_P is true if this is a default argument for a
20134 non-type template parameter. */
20135 static tree
20136 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20138 tree default_argument = NULL_TREE;
20139 bool saved_greater_than_is_operator_p;
20140 bool saved_local_variables_forbidden_p;
20141 bool non_constant_p, is_direct_init;
20143 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20144 set correctly. */
20145 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20146 parser->greater_than_is_operator_p = !template_parm_p;
20147 /* Local variable names (and the `this' keyword) may not
20148 appear in a default argument. */
20149 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20150 parser->local_variables_forbidden_p = true;
20151 /* Parse the assignment-expression. */
20152 if (template_parm_p)
20153 push_deferring_access_checks (dk_no_deferred);
20154 tree saved_class_ptr = NULL_TREE;
20155 tree saved_class_ref = NULL_TREE;
20156 /* The "this" pointer is not valid in a default argument. */
20157 if (cfun)
20159 saved_class_ptr = current_class_ptr;
20160 cp_function_chain->x_current_class_ptr = NULL_TREE;
20161 saved_class_ref = current_class_ref;
20162 cp_function_chain->x_current_class_ref = NULL_TREE;
20164 default_argument
20165 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20166 /* Restore the "this" pointer. */
20167 if (cfun)
20169 cp_function_chain->x_current_class_ptr = saved_class_ptr;
20170 cp_function_chain->x_current_class_ref = saved_class_ref;
20172 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20173 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20174 if (template_parm_p)
20175 pop_deferring_access_checks ();
20176 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20177 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20179 return default_argument;
20182 /* Parse a function-body.
20184 function-body:
20185 compound_statement */
20187 static void
20188 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20190 cp_parser_compound_statement (parser, NULL, (in_function_try_block
20191 ? BCS_TRY_BLOCK : BCS_NORMAL),
20192 true);
20195 /* Parse a ctor-initializer-opt followed by a function-body. Return
20196 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20197 is true we are parsing a function-try-block. */
20199 static bool
20200 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20201 bool in_function_try_block)
20203 tree body, list;
20204 bool ctor_initializer_p;
20205 const bool check_body_p =
20206 DECL_CONSTRUCTOR_P (current_function_decl)
20207 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20208 tree last = NULL;
20210 /* Begin the function body. */
20211 body = begin_function_body ();
20212 /* Parse the optional ctor-initializer. */
20213 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20215 /* If we're parsing a constexpr constructor definition, we need
20216 to check that the constructor body is indeed empty. However,
20217 before we get to cp_parser_function_body lot of junk has been
20218 generated, so we can't just check that we have an empty block.
20219 Rather we take a snapshot of the outermost block, and check whether
20220 cp_parser_function_body changed its state. */
20221 if (check_body_p)
20223 list = cur_stmt_list;
20224 if (STATEMENT_LIST_TAIL (list))
20225 last = STATEMENT_LIST_TAIL (list)->stmt;
20227 /* Parse the function-body. */
20228 cp_parser_function_body (parser, in_function_try_block);
20229 if (check_body_p)
20230 check_constexpr_ctor_body (last, list, /*complain=*/true);
20231 /* Finish the function body. */
20232 finish_function_body (body);
20234 return ctor_initializer_p;
20237 /* Parse an initializer.
20239 initializer:
20240 = initializer-clause
20241 ( expression-list )
20243 Returns an expression representing the initializer. If no
20244 initializer is present, NULL_TREE is returned.
20246 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20247 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20248 set to TRUE if there is no initializer present. If there is an
20249 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20250 is set to true; otherwise it is set to false. */
20252 static tree
20253 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20254 bool* non_constant_p)
20256 cp_token *token;
20257 tree init;
20259 /* Peek at the next token. */
20260 token = cp_lexer_peek_token (parser->lexer);
20262 /* Let our caller know whether or not this initializer was
20263 parenthesized. */
20264 *is_direct_init = (token->type != CPP_EQ);
20265 /* Assume that the initializer is constant. */
20266 *non_constant_p = false;
20268 if (token->type == CPP_EQ)
20270 /* Consume the `='. */
20271 cp_lexer_consume_token (parser->lexer);
20272 /* Parse the initializer-clause. */
20273 init = cp_parser_initializer_clause (parser, non_constant_p);
20275 else if (token->type == CPP_OPEN_PAREN)
20277 vec<tree, va_gc> *vec;
20278 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20279 /*cast_p=*/false,
20280 /*allow_expansion_p=*/true,
20281 non_constant_p);
20282 if (vec == NULL)
20283 return error_mark_node;
20284 init = build_tree_list_vec (vec);
20285 release_tree_vector (vec);
20287 else if (token->type == CPP_OPEN_BRACE)
20289 cp_lexer_set_source_position (parser->lexer);
20290 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20291 init = cp_parser_braced_list (parser, non_constant_p);
20292 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20294 else
20296 /* Anything else is an error. */
20297 cp_parser_error (parser, "expected initializer");
20298 init = error_mark_node;
20301 return init;
20304 /* Parse an initializer-clause.
20306 initializer-clause:
20307 assignment-expression
20308 braced-init-list
20310 Returns an expression representing the initializer.
20312 If the `assignment-expression' production is used the value
20313 returned is simply a representation for the expression.
20315 Otherwise, calls cp_parser_braced_list. */
20317 static tree
20318 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20320 tree initializer;
20322 /* Assume the expression is constant. */
20323 *non_constant_p = false;
20325 /* If it is not a `{', then we are looking at an
20326 assignment-expression. */
20327 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20329 initializer
20330 = cp_parser_constant_expression (parser,
20331 /*allow_non_constant_p=*/true,
20332 non_constant_p);
20334 else
20335 initializer = cp_parser_braced_list (parser, non_constant_p);
20337 return initializer;
20340 /* Parse a brace-enclosed initializer list.
20342 braced-init-list:
20343 { initializer-list , [opt] }
20346 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20347 the elements of the initializer-list (or NULL, if the last
20348 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20349 NULL_TREE. There is no way to detect whether or not the optional
20350 trailing `,' was provided. NON_CONSTANT_P is as for
20351 cp_parser_initializer. */
20353 static tree
20354 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20356 tree initializer;
20358 /* Consume the `{' token. */
20359 cp_lexer_consume_token (parser->lexer);
20360 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20361 initializer = make_node (CONSTRUCTOR);
20362 /* If it's not a `}', then there is a non-trivial initializer. */
20363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20365 /* Parse the initializer list. */
20366 CONSTRUCTOR_ELTS (initializer)
20367 = cp_parser_initializer_list (parser, non_constant_p);
20368 /* A trailing `,' token is allowed. */
20369 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20370 cp_lexer_consume_token (parser->lexer);
20372 else
20373 *non_constant_p = false;
20374 /* Now, there should be a trailing `}'. */
20375 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20376 TREE_TYPE (initializer) = init_list_type_node;
20377 return initializer;
20380 /* Consume tokens up to, and including, the next non-nested closing `]'.
20381 Returns true iff we found a closing `]'. */
20383 static bool
20384 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20386 unsigned square_depth = 0;
20388 while (true)
20390 cp_token * token = cp_lexer_peek_token (parser->lexer);
20392 switch (token->type)
20394 case CPP_EOF:
20395 case CPP_PRAGMA_EOL:
20396 /* If we've run out of tokens, then there is no closing `]'. */
20397 return false;
20399 case CPP_OPEN_SQUARE:
20400 ++square_depth;
20401 break;
20403 case CPP_CLOSE_SQUARE:
20404 if (!square_depth--)
20406 cp_lexer_consume_token (parser->lexer);
20407 return true;
20409 break;
20411 default:
20412 break;
20415 /* Consume the token. */
20416 cp_lexer_consume_token (parser->lexer);
20420 /* Return true if we are looking at an array-designator, false otherwise. */
20422 static bool
20423 cp_parser_array_designator_p (cp_parser *parser)
20425 /* Consume the `['. */
20426 cp_lexer_consume_token (parser->lexer);
20428 cp_lexer_save_tokens (parser->lexer);
20430 /* Skip tokens until the next token is a closing square bracket.
20431 If we find the closing `]', and the next token is a `=', then
20432 we are looking at an array designator. */
20433 bool array_designator_p
20434 = (cp_parser_skip_to_closing_square_bracket (parser)
20435 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20437 /* Roll back the tokens we skipped. */
20438 cp_lexer_rollback_tokens (parser->lexer);
20440 return array_designator_p;
20443 /* Parse an initializer-list.
20445 initializer-list:
20446 initializer-clause ... [opt]
20447 initializer-list , initializer-clause ... [opt]
20449 GNU Extension:
20451 initializer-list:
20452 designation initializer-clause ...[opt]
20453 initializer-list , designation initializer-clause ...[opt]
20455 designation:
20456 . identifier =
20457 identifier :
20458 [ constant-expression ] =
20460 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20461 for the initializer. If the INDEX of the elt is non-NULL, it is the
20462 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20463 as for cp_parser_initializer. */
20465 static vec<constructor_elt, va_gc> *
20466 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
20468 vec<constructor_elt, va_gc> *v = NULL;
20470 /* Assume all of the expressions are constant. */
20471 *non_constant_p = false;
20473 /* Parse the rest of the list. */
20474 while (true)
20476 cp_token *token;
20477 tree designator;
20478 tree initializer;
20479 bool clause_non_constant_p;
20481 /* If the next token is an identifier and the following one is a
20482 colon, we are looking at the GNU designated-initializer
20483 syntax. */
20484 if (cp_parser_allow_gnu_extensions_p (parser)
20485 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
20486 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
20488 /* Warn the user that they are using an extension. */
20489 pedwarn (input_location, OPT_Wpedantic,
20490 "ISO C++ does not allow designated initializers");
20491 /* Consume the identifier. */
20492 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20493 /* Consume the `:'. */
20494 cp_lexer_consume_token (parser->lexer);
20496 /* Also handle the C99 syntax, '. id ='. */
20497 else if (cp_parser_allow_gnu_extensions_p (parser)
20498 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
20499 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
20500 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
20502 /* Warn the user that they are using an extension. */
20503 pedwarn (input_location, OPT_Wpedantic,
20504 "ISO C++ does not allow C99 designated initializers");
20505 /* Consume the `.'. */
20506 cp_lexer_consume_token (parser->lexer);
20507 /* Consume the identifier. */
20508 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20509 /* Consume the `='. */
20510 cp_lexer_consume_token (parser->lexer);
20512 /* Also handle C99 array designators, '[ const ] ='. */
20513 else if (cp_parser_allow_gnu_extensions_p (parser)
20514 && !c_dialect_objc ()
20515 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20517 /* In C++11, [ could start a lambda-introducer. */
20518 bool non_const = false;
20520 cp_parser_parse_tentatively (parser);
20522 if (!cp_parser_array_designator_p (parser))
20524 cp_parser_simulate_error (parser);
20525 designator = NULL_TREE;
20527 else
20529 designator = cp_parser_constant_expression (parser, true,
20530 &non_const);
20531 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20532 cp_parser_require (parser, CPP_EQ, RT_EQ);
20535 if (!cp_parser_parse_definitely (parser))
20536 designator = NULL_TREE;
20537 else if (non_const)
20538 require_potential_rvalue_constant_expression (designator);
20540 else
20541 designator = NULL_TREE;
20543 /* Parse the initializer. */
20544 initializer = cp_parser_initializer_clause (parser,
20545 &clause_non_constant_p);
20546 /* If any clause is non-constant, so is the entire initializer. */
20547 if (clause_non_constant_p)
20548 *non_constant_p = true;
20550 /* If we have an ellipsis, this is an initializer pack
20551 expansion. */
20552 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20554 /* Consume the `...'. */
20555 cp_lexer_consume_token (parser->lexer);
20557 /* Turn the initializer into an initializer expansion. */
20558 initializer = make_pack_expansion (initializer);
20561 /* Add it to the vector. */
20562 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
20564 /* If the next token is not a comma, we have reached the end of
20565 the list. */
20566 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20567 break;
20569 /* Peek at the next token. */
20570 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20571 /* If the next token is a `}', then we're still done. An
20572 initializer-clause can have a trailing `,' after the
20573 initializer-list and before the closing `}'. */
20574 if (token->type == CPP_CLOSE_BRACE)
20575 break;
20577 /* Consume the `,' token. */
20578 cp_lexer_consume_token (parser->lexer);
20581 return v;
20584 /* Classes [gram.class] */
20586 /* Parse a class-name.
20588 class-name:
20589 identifier
20590 template-id
20592 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
20593 to indicate that names looked up in dependent types should be
20594 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
20595 keyword has been used to indicate that the name that appears next
20596 is a template. TAG_TYPE indicates the explicit tag given before
20597 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
20598 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
20599 is the class being defined in a class-head. If ENUM_OK is TRUE,
20600 enum-names are also accepted.
20602 Returns the TYPE_DECL representing the class. */
20604 static tree
20605 cp_parser_class_name (cp_parser *parser,
20606 bool typename_keyword_p,
20607 bool template_keyword_p,
20608 enum tag_types tag_type,
20609 bool check_dependency_p,
20610 bool class_head_p,
20611 bool is_declaration,
20612 bool enum_ok)
20614 tree decl;
20615 tree scope;
20616 bool typename_p;
20617 cp_token *token;
20618 tree identifier = NULL_TREE;
20620 /* All class-names start with an identifier. */
20621 token = cp_lexer_peek_token (parser->lexer);
20622 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
20624 cp_parser_error (parser, "expected class-name");
20625 return error_mark_node;
20628 /* PARSER->SCOPE can be cleared when parsing the template-arguments
20629 to a template-id, so we save it here. */
20630 scope = parser->scope;
20631 if (scope == error_mark_node)
20632 return error_mark_node;
20634 /* Any name names a type if we're following the `typename' keyword
20635 in a qualified name where the enclosing scope is type-dependent. */
20636 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
20637 && dependent_type_p (scope));
20638 /* Handle the common case (an identifier, but not a template-id)
20639 efficiently. */
20640 if (token->type == CPP_NAME
20641 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
20643 cp_token *identifier_token;
20644 bool ambiguous_p;
20646 /* Look for the identifier. */
20647 identifier_token = cp_lexer_peek_token (parser->lexer);
20648 ambiguous_p = identifier_token->error_reported;
20649 identifier = cp_parser_identifier (parser);
20650 /* If the next token isn't an identifier, we are certainly not
20651 looking at a class-name. */
20652 if (identifier == error_mark_node)
20653 decl = error_mark_node;
20654 /* If we know this is a type-name, there's no need to look it
20655 up. */
20656 else if (typename_p)
20657 decl = identifier;
20658 else
20660 tree ambiguous_decls;
20661 /* If we already know that this lookup is ambiguous, then
20662 we've already issued an error message; there's no reason
20663 to check again. */
20664 if (ambiguous_p)
20666 cp_parser_simulate_error (parser);
20667 return error_mark_node;
20669 /* If the next token is a `::', then the name must be a type
20670 name.
20672 [basic.lookup.qual]
20674 During the lookup for a name preceding the :: scope
20675 resolution operator, object, function, and enumerator
20676 names are ignored. */
20677 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20678 tag_type = typename_type;
20679 /* Look up the name. */
20680 decl = cp_parser_lookup_name (parser, identifier,
20681 tag_type,
20682 /*is_template=*/false,
20683 /*is_namespace=*/false,
20684 check_dependency_p,
20685 &ambiguous_decls,
20686 identifier_token->location);
20687 if (ambiguous_decls)
20689 if (cp_parser_parsing_tentatively (parser))
20690 cp_parser_simulate_error (parser);
20691 return error_mark_node;
20695 else
20697 /* Try a template-id. */
20698 decl = cp_parser_template_id (parser, template_keyword_p,
20699 check_dependency_p,
20700 tag_type,
20701 is_declaration);
20702 if (decl == error_mark_node)
20703 return error_mark_node;
20706 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
20708 /* If this is a typename, create a TYPENAME_TYPE. */
20709 if (typename_p && decl != error_mark_node)
20711 decl = make_typename_type (scope, decl, typename_type,
20712 /*complain=*/tf_error);
20713 if (decl != error_mark_node)
20714 decl = TYPE_NAME (decl);
20717 decl = strip_using_decl (decl);
20719 /* Check to see that it is really the name of a class. */
20720 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20721 && identifier_p (TREE_OPERAND (decl, 0))
20722 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20723 /* Situations like this:
20725 template <typename T> struct A {
20726 typename T::template X<int>::I i;
20729 are problematic. Is `T::template X<int>' a class-name? The
20730 standard does not seem to be definitive, but there is no other
20731 valid interpretation of the following `::'. Therefore, those
20732 names are considered class-names. */
20734 decl = make_typename_type (scope, decl, tag_type, tf_error);
20735 if (decl != error_mark_node)
20736 decl = TYPE_NAME (decl);
20738 else if (TREE_CODE (decl) != TYPE_DECL
20739 || TREE_TYPE (decl) == error_mark_node
20740 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
20741 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
20742 /* In Objective-C 2.0, a classname followed by '.' starts a
20743 dot-syntax expression, and it's not a type-name. */
20744 || (c_dialect_objc ()
20745 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
20746 && objc_is_class_name (decl)))
20747 decl = error_mark_node;
20749 if (decl == error_mark_node)
20750 cp_parser_error (parser, "expected class-name");
20751 else if (identifier && !parser->scope)
20752 maybe_note_name_used_in_class (identifier, decl);
20754 return decl;
20757 /* Parse a class-specifier.
20759 class-specifier:
20760 class-head { member-specification [opt] }
20762 Returns the TREE_TYPE representing the class. */
20764 static tree
20765 cp_parser_class_specifier_1 (cp_parser* parser)
20767 tree type;
20768 tree attributes = NULL_TREE;
20769 bool nested_name_specifier_p;
20770 unsigned saved_num_template_parameter_lists;
20771 bool saved_in_function_body;
20772 unsigned char in_statement;
20773 bool in_switch_statement_p;
20774 bool saved_in_unbraced_linkage_specification_p;
20775 tree old_scope = NULL_TREE;
20776 tree scope = NULL_TREE;
20777 cp_token *closing_brace;
20779 push_deferring_access_checks (dk_no_deferred);
20781 /* Parse the class-head. */
20782 type = cp_parser_class_head (parser,
20783 &nested_name_specifier_p);
20784 /* If the class-head was a semantic disaster, skip the entire body
20785 of the class. */
20786 if (!type)
20788 cp_parser_skip_to_end_of_block_or_statement (parser);
20789 pop_deferring_access_checks ();
20790 return error_mark_node;
20793 /* Look for the `{'. */
20794 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
20796 pop_deferring_access_checks ();
20797 return error_mark_node;
20800 cp_ensure_no_omp_declare_simd (parser);
20801 cp_ensure_no_oacc_routine (parser);
20803 /* Issue an error message if type-definitions are forbidden here. */
20804 cp_parser_check_type_definition (parser);
20805 /* Remember that we are defining one more class. */
20806 ++parser->num_classes_being_defined;
20807 /* Inside the class, surrounding template-parameter-lists do not
20808 apply. */
20809 saved_num_template_parameter_lists
20810 = parser->num_template_parameter_lists;
20811 parser->num_template_parameter_lists = 0;
20812 /* We are not in a function body. */
20813 saved_in_function_body = parser->in_function_body;
20814 parser->in_function_body = false;
20815 /* Or in a loop. */
20816 in_statement = parser->in_statement;
20817 parser->in_statement = 0;
20818 /* Or in a switch. */
20819 in_switch_statement_p = parser->in_switch_statement_p;
20820 parser->in_switch_statement_p = false;
20821 /* We are not immediately inside an extern "lang" block. */
20822 saved_in_unbraced_linkage_specification_p
20823 = parser->in_unbraced_linkage_specification_p;
20824 parser->in_unbraced_linkage_specification_p = false;
20826 // Associate constraints with the type.
20827 if (flag_concepts)
20828 type = associate_classtype_constraints (type);
20830 /* Start the class. */
20831 if (nested_name_specifier_p)
20833 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
20834 old_scope = push_inner_scope (scope);
20836 type = begin_class_definition (type);
20838 if (type == error_mark_node)
20839 /* If the type is erroneous, skip the entire body of the class. */
20840 cp_parser_skip_to_closing_brace (parser);
20841 else
20842 /* Parse the member-specification. */
20843 cp_parser_member_specification_opt (parser);
20845 /* Look for the trailing `}'. */
20846 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20847 /* Look for trailing attributes to apply to this class. */
20848 if (cp_parser_allow_gnu_extensions_p (parser))
20849 attributes = cp_parser_gnu_attributes_opt (parser);
20850 if (type != error_mark_node)
20851 type = finish_struct (type, attributes);
20852 if (nested_name_specifier_p)
20853 pop_inner_scope (old_scope, scope);
20855 /* We've finished a type definition. Check for the common syntax
20856 error of forgetting a semicolon after the definition. We need to
20857 be careful, as we can't just check for not-a-semicolon and be done
20858 with it; the user might have typed:
20860 class X { } c = ...;
20861 class X { } *p = ...;
20863 and so forth. Instead, enumerate all the possible tokens that
20864 might follow this production; if we don't see one of them, then
20865 complain and silently insert the semicolon. */
20867 cp_token *token = cp_lexer_peek_token (parser->lexer);
20868 bool want_semicolon = true;
20870 if (cp_next_tokens_can_be_std_attribute_p (parser))
20871 /* Don't try to parse c++11 attributes here. As per the
20872 grammar, that should be a task for
20873 cp_parser_decl_specifier_seq. */
20874 want_semicolon = false;
20876 switch (token->type)
20878 case CPP_NAME:
20879 case CPP_SEMICOLON:
20880 case CPP_MULT:
20881 case CPP_AND:
20882 case CPP_OPEN_PAREN:
20883 case CPP_CLOSE_PAREN:
20884 case CPP_COMMA:
20885 want_semicolon = false;
20886 break;
20888 /* While it's legal for type qualifiers and storage class
20889 specifiers to follow type definitions in the grammar, only
20890 compiler testsuites contain code like that. Assume that if
20891 we see such code, then what we're really seeing is a case
20892 like:
20894 class X { }
20895 const <type> var = ...;
20899 class Y { }
20900 static <type> func (...) ...
20902 i.e. the qualifier or specifier applies to the next
20903 declaration. To do so, however, we need to look ahead one
20904 more token to see if *that* token is a type specifier.
20906 This code could be improved to handle:
20908 class Z { }
20909 static const <type> var = ...; */
20910 case CPP_KEYWORD:
20911 if (keyword_is_decl_specifier (token->keyword))
20913 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
20915 /* Handling user-defined types here would be nice, but very
20916 tricky. */
20917 want_semicolon
20918 = (lookahead->type == CPP_KEYWORD
20919 && keyword_begins_type_specifier (lookahead->keyword));
20921 break;
20922 default:
20923 break;
20926 /* If we don't have a type, then something is very wrong and we
20927 shouldn't try to do anything clever. Likewise for not seeing the
20928 closing brace. */
20929 if (closing_brace && TYPE_P (type) && want_semicolon)
20931 cp_token_position prev
20932 = cp_lexer_previous_token_position (parser->lexer);
20933 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
20934 location_t loc = prev_token->location;
20936 if (CLASSTYPE_DECLARED_CLASS (type))
20937 error_at (loc, "expected %<;%> after class definition");
20938 else if (TREE_CODE (type) == RECORD_TYPE)
20939 error_at (loc, "expected %<;%> after struct definition");
20940 else if (TREE_CODE (type) == UNION_TYPE)
20941 error_at (loc, "expected %<;%> after union definition");
20942 else
20943 gcc_unreachable ();
20945 /* Unget one token and smash it to look as though we encountered
20946 a semicolon in the input stream. */
20947 cp_lexer_set_token_position (parser->lexer, prev);
20948 token = cp_lexer_peek_token (parser->lexer);
20949 token->type = CPP_SEMICOLON;
20950 token->keyword = RID_MAX;
20954 /* If this class is not itself within the scope of another class,
20955 then we need to parse the bodies of all of the queued function
20956 definitions. Note that the queued functions defined in a class
20957 are not always processed immediately following the
20958 class-specifier for that class. Consider:
20960 struct A {
20961 struct B { void f() { sizeof (A); } };
20964 If `f' were processed before the processing of `A' were
20965 completed, there would be no way to compute the size of `A'.
20966 Note that the nesting we are interested in here is lexical --
20967 not the semantic nesting given by TYPE_CONTEXT. In particular,
20968 for:
20970 struct A { struct B; };
20971 struct A::B { void f() { } };
20973 there is no need to delay the parsing of `A::B::f'. */
20974 if (--parser->num_classes_being_defined == 0)
20976 tree decl;
20977 tree class_type = NULL_TREE;
20978 tree pushed_scope = NULL_TREE;
20979 unsigned ix;
20980 cp_default_arg_entry *e;
20981 tree save_ccp, save_ccr;
20983 /* In a first pass, parse default arguments to the functions.
20984 Then, in a second pass, parse the bodies of the functions.
20985 This two-phased approach handles cases like:
20987 struct S {
20988 void f() { g(); }
20989 void g(int i = 3);
20993 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
20995 decl = e->decl;
20996 /* If there are default arguments that have not yet been processed,
20997 take care of them now. */
20998 if (class_type != e->class_type)
21000 if (pushed_scope)
21001 pop_scope (pushed_scope);
21002 class_type = e->class_type;
21003 pushed_scope = push_scope (class_type);
21005 /* Make sure that any template parameters are in scope. */
21006 maybe_begin_member_template_processing (decl);
21007 /* Parse the default argument expressions. */
21008 cp_parser_late_parsing_default_args (parser, decl);
21009 /* Remove any template parameters from the symbol table. */
21010 maybe_end_member_template_processing ();
21012 vec_safe_truncate (unparsed_funs_with_default_args, 0);
21013 /* Now parse any NSDMIs. */
21014 save_ccp = current_class_ptr;
21015 save_ccr = current_class_ref;
21016 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21018 if (class_type != DECL_CONTEXT (decl))
21020 if (pushed_scope)
21021 pop_scope (pushed_scope);
21022 class_type = DECL_CONTEXT (decl);
21023 pushed_scope = push_scope (class_type);
21025 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21026 cp_parser_late_parsing_nsdmi (parser, decl);
21028 vec_safe_truncate (unparsed_nsdmis, 0);
21029 current_class_ptr = save_ccp;
21030 current_class_ref = save_ccr;
21031 if (pushed_scope)
21032 pop_scope (pushed_scope);
21034 /* Now do some post-NSDMI bookkeeping. */
21035 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21036 after_nsdmi_defaulted_late_checks (class_type);
21037 vec_safe_truncate (unparsed_classes, 0);
21038 after_nsdmi_defaulted_late_checks (type);
21040 /* Now parse the body of the functions. */
21041 if (flag_openmp)
21043 /* OpenMP UDRs need to be parsed before all other functions. */
21044 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21045 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21046 cp_parser_late_parsing_for_member (parser, decl);
21047 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21048 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21049 cp_parser_late_parsing_for_member (parser, decl);
21051 else
21052 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21053 cp_parser_late_parsing_for_member (parser, decl);
21054 vec_safe_truncate (unparsed_funs_with_definitions, 0);
21056 else
21057 vec_safe_push (unparsed_classes, type);
21059 /* Put back any saved access checks. */
21060 pop_deferring_access_checks ();
21062 /* Restore saved state. */
21063 parser->in_switch_statement_p = in_switch_statement_p;
21064 parser->in_statement = in_statement;
21065 parser->in_function_body = saved_in_function_body;
21066 parser->num_template_parameter_lists
21067 = saved_num_template_parameter_lists;
21068 parser->in_unbraced_linkage_specification_p
21069 = saved_in_unbraced_linkage_specification_p;
21071 return type;
21074 static tree
21075 cp_parser_class_specifier (cp_parser* parser)
21077 tree ret;
21078 timevar_push (TV_PARSE_STRUCT);
21079 ret = cp_parser_class_specifier_1 (parser);
21080 timevar_pop (TV_PARSE_STRUCT);
21081 return ret;
21084 /* Parse a class-head.
21086 class-head:
21087 class-key identifier [opt] base-clause [opt]
21088 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21089 class-key nested-name-specifier [opt] template-id
21090 base-clause [opt]
21092 class-virt-specifier:
21093 final
21095 GNU Extensions:
21096 class-key attributes identifier [opt] base-clause [opt]
21097 class-key attributes nested-name-specifier identifier base-clause [opt]
21098 class-key attributes nested-name-specifier [opt] template-id
21099 base-clause [opt]
21101 Upon return BASES is initialized to the list of base classes (or
21102 NULL, if there are none) in the same form returned by
21103 cp_parser_base_clause.
21105 Returns the TYPE of the indicated class. Sets
21106 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21107 involving a nested-name-specifier was used, and FALSE otherwise.
21109 Returns error_mark_node if this is not a class-head.
21111 Returns NULL_TREE if the class-head is syntactically valid, but
21112 semantically invalid in a way that means we should skip the entire
21113 body of the class. */
21115 static tree
21116 cp_parser_class_head (cp_parser* parser,
21117 bool* nested_name_specifier_p)
21119 tree nested_name_specifier;
21120 enum tag_types class_key;
21121 tree id = NULL_TREE;
21122 tree type = NULL_TREE;
21123 tree attributes;
21124 tree bases;
21125 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21126 bool template_id_p = false;
21127 bool qualified_p = false;
21128 bool invalid_nested_name_p = false;
21129 bool invalid_explicit_specialization_p = false;
21130 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21131 tree pushed_scope = NULL_TREE;
21132 unsigned num_templates;
21133 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21134 /* Assume no nested-name-specifier will be present. */
21135 *nested_name_specifier_p = false;
21136 /* Assume no template parameter lists will be used in defining the
21137 type. */
21138 num_templates = 0;
21139 parser->colon_corrects_to_scope_p = false;
21141 /* Look for the class-key. */
21142 class_key = cp_parser_class_key (parser);
21143 if (class_key == none_type)
21144 return error_mark_node;
21146 /* Parse the attributes. */
21147 attributes = cp_parser_attributes_opt (parser);
21149 /* If the next token is `::', that is invalid -- but sometimes
21150 people do try to write:
21152 struct ::S {};
21154 Handle this gracefully by accepting the extra qualifier, and then
21155 issuing an error about it later if this really is a
21156 class-head. If it turns out just to be an elaborated type
21157 specifier, remain silent. */
21158 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21159 qualified_p = true;
21161 push_deferring_access_checks (dk_no_check);
21163 /* Determine the name of the class. Begin by looking for an
21164 optional nested-name-specifier. */
21165 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21166 nested_name_specifier
21167 = cp_parser_nested_name_specifier_opt (parser,
21168 /*typename_keyword_p=*/false,
21169 /*check_dependency_p=*/false,
21170 /*type_p=*/true,
21171 /*is_declaration=*/false);
21172 /* If there was a nested-name-specifier, then there *must* be an
21173 identifier. */
21174 if (nested_name_specifier)
21176 type_start_token = cp_lexer_peek_token (parser->lexer);
21177 /* Although the grammar says `identifier', it really means
21178 `class-name' or `template-name'. You are only allowed to
21179 define a class that has already been declared with this
21180 syntax.
21182 The proposed resolution for Core Issue 180 says that wherever
21183 you see `class T::X' you should treat `X' as a type-name.
21185 It is OK to define an inaccessible class; for example:
21187 class A { class B; };
21188 class A::B {};
21190 We do not know if we will see a class-name, or a
21191 template-name. We look for a class-name first, in case the
21192 class-name is a template-id; if we looked for the
21193 template-name first we would stop after the template-name. */
21194 cp_parser_parse_tentatively (parser);
21195 type = cp_parser_class_name (parser,
21196 /*typename_keyword_p=*/false,
21197 /*template_keyword_p=*/false,
21198 class_type,
21199 /*check_dependency_p=*/false,
21200 /*class_head_p=*/true,
21201 /*is_declaration=*/false);
21202 /* If that didn't work, ignore the nested-name-specifier. */
21203 if (!cp_parser_parse_definitely (parser))
21205 invalid_nested_name_p = true;
21206 type_start_token = cp_lexer_peek_token (parser->lexer);
21207 id = cp_parser_identifier (parser);
21208 if (id == error_mark_node)
21209 id = NULL_TREE;
21211 /* If we could not find a corresponding TYPE, treat this
21212 declaration like an unqualified declaration. */
21213 if (type == error_mark_node)
21214 nested_name_specifier = NULL_TREE;
21215 /* Otherwise, count the number of templates used in TYPE and its
21216 containing scopes. */
21217 else
21219 tree scope;
21221 for (scope = TREE_TYPE (type);
21222 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21223 scope = get_containing_scope (scope))
21224 if (TYPE_P (scope)
21225 && CLASS_TYPE_P (scope)
21226 && CLASSTYPE_TEMPLATE_INFO (scope)
21227 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21228 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21229 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21230 ++num_templates;
21233 /* Otherwise, the identifier is optional. */
21234 else
21236 /* We don't know whether what comes next is a template-id,
21237 an identifier, or nothing at all. */
21238 cp_parser_parse_tentatively (parser);
21239 /* Check for a template-id. */
21240 type_start_token = cp_lexer_peek_token (parser->lexer);
21241 id = cp_parser_template_id (parser,
21242 /*template_keyword_p=*/false,
21243 /*check_dependency_p=*/true,
21244 class_key,
21245 /*is_declaration=*/true);
21246 /* If that didn't work, it could still be an identifier. */
21247 if (!cp_parser_parse_definitely (parser))
21249 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21251 type_start_token = cp_lexer_peek_token (parser->lexer);
21252 id = cp_parser_identifier (parser);
21254 else
21255 id = NULL_TREE;
21257 else
21259 template_id_p = true;
21260 ++num_templates;
21264 pop_deferring_access_checks ();
21266 if (id)
21268 cp_parser_check_for_invalid_template_id (parser, id,
21269 class_key,
21270 type_start_token->location);
21272 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21274 /* If it's not a `:' or a `{' then we can't really be looking at a
21275 class-head, since a class-head only appears as part of a
21276 class-specifier. We have to detect this situation before calling
21277 xref_tag, since that has irreversible side-effects. */
21278 if (!cp_parser_next_token_starts_class_definition_p (parser))
21280 cp_parser_error (parser, "expected %<{%> or %<:%>");
21281 type = error_mark_node;
21282 goto out;
21285 /* At this point, we're going ahead with the class-specifier, even
21286 if some other problem occurs. */
21287 cp_parser_commit_to_tentative_parse (parser);
21288 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21290 cp_parser_error (parser,
21291 "cannot specify %<override%> for a class");
21292 type = error_mark_node;
21293 goto out;
21295 /* Issue the error about the overly-qualified name now. */
21296 if (qualified_p)
21298 cp_parser_error (parser,
21299 "global qualification of class name is invalid");
21300 type = error_mark_node;
21301 goto out;
21303 else if (invalid_nested_name_p)
21305 cp_parser_error (parser,
21306 "qualified name does not name a class");
21307 type = error_mark_node;
21308 goto out;
21310 else if (nested_name_specifier)
21312 tree scope;
21314 /* Reject typedef-names in class heads. */
21315 if (!DECL_IMPLICIT_TYPEDEF_P (type))
21317 error_at (type_start_token->location,
21318 "invalid class name in declaration of %qD",
21319 type);
21320 type = NULL_TREE;
21321 goto done;
21324 /* Figure out in what scope the declaration is being placed. */
21325 scope = current_scope ();
21326 /* If that scope does not contain the scope in which the
21327 class was originally declared, the program is invalid. */
21328 if (scope && !is_ancestor (scope, nested_name_specifier))
21330 if (at_namespace_scope_p ())
21331 error_at (type_start_token->location,
21332 "declaration of %qD in namespace %qD which does not "
21333 "enclose %qD",
21334 type, scope, nested_name_specifier);
21335 else
21336 error_at (type_start_token->location,
21337 "declaration of %qD in %qD which does not enclose %qD",
21338 type, scope, nested_name_specifier);
21339 type = NULL_TREE;
21340 goto done;
21342 /* [dcl.meaning]
21344 A declarator-id shall not be qualified except for the
21345 definition of a ... nested class outside of its class
21346 ... [or] the definition or explicit instantiation of a
21347 class member of a namespace outside of its namespace. */
21348 if (scope == nested_name_specifier)
21350 permerror (nested_name_specifier_token_start->location,
21351 "extra qualification not allowed");
21352 nested_name_specifier = NULL_TREE;
21353 num_templates = 0;
21356 /* An explicit-specialization must be preceded by "template <>". If
21357 it is not, try to recover gracefully. */
21358 if (at_namespace_scope_p ()
21359 && parser->num_template_parameter_lists == 0
21360 && template_id_p)
21362 error_at (type_start_token->location,
21363 "an explicit specialization must be preceded by %<template <>%>");
21364 invalid_explicit_specialization_p = true;
21365 /* Take the same action that would have been taken by
21366 cp_parser_explicit_specialization. */
21367 ++parser->num_template_parameter_lists;
21368 begin_specialization ();
21370 /* There must be no "return" statements between this point and the
21371 end of this function; set "type "to the correct return value and
21372 use "goto done;" to return. */
21373 /* Make sure that the right number of template parameters were
21374 present. */
21375 if (!cp_parser_check_template_parameters (parser, num_templates,
21376 type_start_token->location,
21377 /*declarator=*/NULL))
21379 /* If something went wrong, there is no point in even trying to
21380 process the class-definition. */
21381 type = NULL_TREE;
21382 goto done;
21385 /* Look up the type. */
21386 if (template_id_p)
21388 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21389 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21390 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21392 error_at (type_start_token->location,
21393 "function template %qD redeclared as a class template", id);
21394 type = error_mark_node;
21396 else
21398 type = TREE_TYPE (id);
21399 type = maybe_process_partial_specialization (type);
21401 if (nested_name_specifier)
21402 pushed_scope = push_scope (nested_name_specifier);
21404 else if (nested_name_specifier)
21406 tree class_type;
21408 /* Given:
21410 template <typename T> struct S { struct T };
21411 template <typename T> struct S<T>::T { };
21413 we will get a TYPENAME_TYPE when processing the definition of
21414 `S::T'. We need to resolve it to the actual type before we
21415 try to define it. */
21416 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21418 class_type = resolve_typename_type (TREE_TYPE (type),
21419 /*only_current_p=*/false);
21420 if (TREE_CODE (class_type) != TYPENAME_TYPE)
21421 type = TYPE_NAME (class_type);
21422 else
21424 cp_parser_error (parser, "could not resolve typename type");
21425 type = error_mark_node;
21429 if (maybe_process_partial_specialization (TREE_TYPE (type))
21430 == error_mark_node)
21432 type = NULL_TREE;
21433 goto done;
21436 class_type = current_class_type;
21437 /* Enter the scope indicated by the nested-name-specifier. */
21438 pushed_scope = push_scope (nested_name_specifier);
21439 /* Get the canonical version of this type. */
21440 type = TYPE_MAIN_DECL (TREE_TYPE (type));
21441 /* Call push_template_decl if it seems like we should be defining a
21442 template either from the template headers or the type we're
21443 defining, so that we diagnose both extra and missing headers. */
21444 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21445 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21446 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21448 type = push_template_decl (type);
21449 if (type == error_mark_node)
21451 type = NULL_TREE;
21452 goto done;
21456 type = TREE_TYPE (type);
21457 *nested_name_specifier_p = true;
21459 else /* The name is not a nested name. */
21461 /* If the class was unnamed, create a dummy name. */
21462 if (!id)
21463 id = make_anon_name ();
21464 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
21465 parser->num_template_parameter_lists);
21468 /* Indicate whether this class was declared as a `class' or as a
21469 `struct'. */
21470 if (TREE_CODE (type) == RECORD_TYPE)
21471 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
21472 cp_parser_check_class_key (class_key, type);
21474 /* If this type was already complete, and we see another definition,
21475 that's an error. */
21476 if (type != error_mark_node && COMPLETE_TYPE_P (type))
21478 error_at (type_start_token->location, "redefinition of %q#T",
21479 type);
21480 error_at (type_start_token->location, "previous definition of %q+#T",
21481 type);
21482 type = NULL_TREE;
21483 goto done;
21485 else if (type == error_mark_node)
21486 type = NULL_TREE;
21488 if (type)
21490 /* Apply attributes now, before any use of the class as a template
21491 argument in its base list. */
21492 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
21493 fixup_attribute_variants (type);
21496 /* We will have entered the scope containing the class; the names of
21497 base classes should be looked up in that context. For example:
21499 struct A { struct B {}; struct C; };
21500 struct A::C : B {};
21502 is valid. */
21504 /* Get the list of base-classes, if there is one. */
21505 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21507 /* PR59482: enter the class scope so that base-specifiers are looked
21508 up correctly. */
21509 if (type)
21510 pushclass (type);
21511 bases = cp_parser_base_clause (parser);
21512 /* PR59482: get out of the previously pushed class scope so that the
21513 subsequent pops pop the right thing. */
21514 if (type)
21515 popclass ();
21517 else
21518 bases = NULL_TREE;
21520 /* If we're really defining a class, process the base classes.
21521 If they're invalid, fail. */
21522 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21523 && !xref_basetypes (type, bases))
21524 type = NULL_TREE;
21526 done:
21527 /* Leave the scope given by the nested-name-specifier. We will
21528 enter the class scope itself while processing the members. */
21529 if (pushed_scope)
21530 pop_scope (pushed_scope);
21532 if (invalid_explicit_specialization_p)
21534 end_specialization ();
21535 --parser->num_template_parameter_lists;
21538 if (type)
21539 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21540 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
21541 CLASSTYPE_FINAL (type) = 1;
21542 out:
21543 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21544 return type;
21547 /* Parse a class-key.
21549 class-key:
21550 class
21551 struct
21552 union
21554 Returns the kind of class-key specified, or none_type to indicate
21555 error. */
21557 static enum tag_types
21558 cp_parser_class_key (cp_parser* parser)
21560 cp_token *token;
21561 enum tag_types tag_type;
21563 /* Look for the class-key. */
21564 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
21565 if (!token)
21566 return none_type;
21568 /* Check to see if the TOKEN is a class-key. */
21569 tag_type = cp_parser_token_is_class_key (token);
21570 if (!tag_type)
21571 cp_parser_error (parser, "expected class-key");
21572 return tag_type;
21575 /* Parse a type-parameter-key.
21577 type-parameter-key:
21578 class
21579 typename
21582 static void
21583 cp_parser_type_parameter_key (cp_parser* parser)
21585 /* Look for the type-parameter-key. */
21586 enum tag_types tag_type = none_type;
21587 cp_token *token = cp_lexer_peek_token (parser->lexer);
21588 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
21590 cp_lexer_consume_token (parser->lexer);
21591 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
21592 /* typename is not allowed in a template template parameter
21593 by the standard until C++1Z. */
21594 pedwarn (token->location, OPT_Wpedantic,
21595 "ISO C++ forbids typename key in template template parameter;"
21596 " use -std=c++1z or -std=gnu++1z");
21598 else
21599 cp_parser_error (parser, "expected %<class%> or %<typename%>");
21601 return;
21604 /* Parse an (optional) member-specification.
21606 member-specification:
21607 member-declaration member-specification [opt]
21608 access-specifier : member-specification [opt] */
21610 static void
21611 cp_parser_member_specification_opt (cp_parser* parser)
21613 while (true)
21615 cp_token *token;
21616 enum rid keyword;
21618 /* Peek at the next token. */
21619 token = cp_lexer_peek_token (parser->lexer);
21620 /* If it's a `}', or EOF then we've seen all the members. */
21621 if (token->type == CPP_CLOSE_BRACE
21622 || token->type == CPP_EOF
21623 || token->type == CPP_PRAGMA_EOL)
21624 break;
21626 /* See if this token is a keyword. */
21627 keyword = token->keyword;
21628 switch (keyword)
21630 case RID_PUBLIC:
21631 case RID_PROTECTED:
21632 case RID_PRIVATE:
21633 /* Consume the access-specifier. */
21634 cp_lexer_consume_token (parser->lexer);
21635 /* Remember which access-specifier is active. */
21636 current_access_specifier = token->u.value;
21637 /* Look for the `:'. */
21638 cp_parser_require (parser, CPP_COLON, RT_COLON);
21639 break;
21641 default:
21642 /* Accept #pragmas at class scope. */
21643 if (token->type == CPP_PRAGMA)
21645 cp_parser_pragma (parser, pragma_member);
21646 break;
21649 /* Otherwise, the next construction must be a
21650 member-declaration. */
21651 cp_parser_member_declaration (parser);
21656 /* Parse a member-declaration.
21658 member-declaration:
21659 decl-specifier-seq [opt] member-declarator-list [opt] ;
21660 function-definition ; [opt]
21661 :: [opt] nested-name-specifier template [opt] unqualified-id ;
21662 using-declaration
21663 template-declaration
21664 alias-declaration
21666 member-declarator-list:
21667 member-declarator
21668 member-declarator-list , member-declarator
21670 member-declarator:
21671 declarator pure-specifier [opt]
21672 declarator constant-initializer [opt]
21673 identifier [opt] : constant-expression
21675 GNU Extensions:
21677 member-declaration:
21678 __extension__ member-declaration
21680 member-declarator:
21681 declarator attributes [opt] pure-specifier [opt]
21682 declarator attributes [opt] constant-initializer [opt]
21683 identifier [opt] attributes [opt] : constant-expression
21685 C++0x Extensions:
21687 member-declaration:
21688 static_assert-declaration */
21690 static void
21691 cp_parser_member_declaration (cp_parser* parser)
21693 cp_decl_specifier_seq decl_specifiers;
21694 tree prefix_attributes;
21695 tree decl;
21696 int declares_class_or_enum;
21697 bool friend_p;
21698 cp_token *token = NULL;
21699 cp_token *decl_spec_token_start = NULL;
21700 cp_token *initializer_token_start = NULL;
21701 int saved_pedantic;
21702 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21704 /* Check for the `__extension__' keyword. */
21705 if (cp_parser_extension_opt (parser, &saved_pedantic))
21707 /* Recurse. */
21708 cp_parser_member_declaration (parser);
21709 /* Restore the old value of the PEDANTIC flag. */
21710 pedantic = saved_pedantic;
21712 return;
21715 /* Check for a template-declaration. */
21716 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21718 /* An explicit specialization here is an error condition, and we
21719 expect the specialization handler to detect and report this. */
21720 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
21721 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
21722 cp_parser_explicit_specialization (parser);
21723 else
21724 cp_parser_template_declaration (parser, /*member_p=*/true);
21726 return;
21728 /* Check for a template introduction. */
21729 else if (cp_parser_template_declaration_after_export (parser, true))
21730 return;
21732 /* Check for a using-declaration. */
21733 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21735 if (cxx_dialect < cxx11)
21737 /* Parse the using-declaration. */
21738 cp_parser_using_declaration (parser,
21739 /*access_declaration_p=*/false);
21740 return;
21742 else
21744 tree decl;
21745 bool alias_decl_expected;
21746 cp_parser_parse_tentatively (parser);
21747 decl = cp_parser_alias_declaration (parser);
21748 /* Note that if we actually see the '=' token after the
21749 identifier, cp_parser_alias_declaration commits the
21750 tentative parse. In that case, we really expect an
21751 alias-declaration. Otherwise, we expect a using
21752 declaration. */
21753 alias_decl_expected =
21754 !cp_parser_uncommitted_to_tentative_parse_p (parser);
21755 cp_parser_parse_definitely (parser);
21757 if (alias_decl_expected)
21758 finish_member_declaration (decl);
21759 else
21760 cp_parser_using_declaration (parser,
21761 /*access_declaration_p=*/false);
21762 return;
21766 /* Check for @defs. */
21767 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
21769 tree ivar, member;
21770 tree ivar_chains = cp_parser_objc_defs_expression (parser);
21771 ivar = ivar_chains;
21772 while (ivar)
21774 member = ivar;
21775 ivar = TREE_CHAIN (member);
21776 TREE_CHAIN (member) = NULL_TREE;
21777 finish_member_declaration (member);
21779 return;
21782 /* If the next token is `static_assert' we have a static assertion. */
21783 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
21785 cp_parser_static_assert (parser, /*member_p=*/true);
21786 return;
21789 parser->colon_corrects_to_scope_p = false;
21791 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
21792 goto out;
21794 /* Parse the decl-specifier-seq. */
21795 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21796 cp_parser_decl_specifier_seq (parser,
21797 CP_PARSER_FLAGS_OPTIONAL,
21798 &decl_specifiers,
21799 &declares_class_or_enum);
21800 /* Check for an invalid type-name. */
21801 if (!decl_specifiers.any_type_specifiers_p
21802 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21803 goto out;
21804 /* If there is no declarator, then the decl-specifier-seq should
21805 specify a type. */
21806 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21808 /* If there was no decl-specifier-seq, and the next token is a
21809 `;', then we have something like:
21811 struct S { ; };
21813 [class.mem]
21815 Each member-declaration shall declare at least one member
21816 name of the class. */
21817 if (!decl_specifiers.any_specifiers_p)
21819 cp_token *token = cp_lexer_peek_token (parser->lexer);
21820 if (!in_system_header_at (token->location))
21821 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
21823 else
21825 tree type;
21827 /* See if this declaration is a friend. */
21828 friend_p = cp_parser_friend_p (&decl_specifiers);
21829 /* If there were decl-specifiers, check to see if there was
21830 a class-declaration. */
21831 type = check_tag_decl (&decl_specifiers,
21832 /*explicit_type_instantiation_p=*/false);
21833 /* Nested classes have already been added to the class, but
21834 a `friend' needs to be explicitly registered. */
21835 if (friend_p)
21837 /* If the `friend' keyword was present, the friend must
21838 be introduced with a class-key. */
21839 if (!declares_class_or_enum && cxx_dialect < cxx11)
21840 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
21841 "in C++03 a class-key must be used "
21842 "when declaring a friend");
21843 /* In this case:
21845 template <typename T> struct A {
21846 friend struct A<T>::B;
21849 A<T>::B will be represented by a TYPENAME_TYPE, and
21850 therefore not recognized by check_tag_decl. */
21851 if (!type)
21853 type = decl_specifiers.type;
21854 if (type && TREE_CODE (type) == TYPE_DECL)
21855 type = TREE_TYPE (type);
21857 if (!type || !TYPE_P (type))
21858 error_at (decl_spec_token_start->location,
21859 "friend declaration does not name a class or "
21860 "function");
21861 else
21862 make_friend_class (current_class_type, type,
21863 /*complain=*/true);
21865 /* If there is no TYPE, an error message will already have
21866 been issued. */
21867 else if (!type || type == error_mark_node)
21869 /* An anonymous aggregate has to be handled specially; such
21870 a declaration really declares a data member (with a
21871 particular type), as opposed to a nested class. */
21872 else if (ANON_AGGR_TYPE_P (type))
21874 /* C++11 9.5/6. */
21875 if (decl_specifiers.storage_class != sc_none)
21876 error_at (decl_spec_token_start->location,
21877 "a storage class on an anonymous aggregate "
21878 "in class scope is not allowed");
21880 /* Remove constructors and such from TYPE, now that we
21881 know it is an anonymous aggregate. */
21882 fixup_anonymous_aggr (type);
21883 /* And make the corresponding data member. */
21884 decl = build_decl (decl_spec_token_start->location,
21885 FIELD_DECL, NULL_TREE, type);
21886 /* Add it to the class. */
21887 finish_member_declaration (decl);
21889 else
21890 cp_parser_check_access_in_redeclaration
21891 (TYPE_NAME (type),
21892 decl_spec_token_start->location);
21895 else
21897 bool assume_semicolon = false;
21899 /* Clear attributes from the decl_specifiers but keep them
21900 around as prefix attributes that apply them to the entity
21901 being declared. */
21902 prefix_attributes = decl_specifiers.attributes;
21903 decl_specifiers.attributes = NULL_TREE;
21905 /* See if these declarations will be friends. */
21906 friend_p = cp_parser_friend_p (&decl_specifiers);
21908 /* Keep going until we hit the `;' at the end of the
21909 declaration. */
21910 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21912 tree attributes = NULL_TREE;
21913 tree first_attribute;
21915 /* Peek at the next token. */
21916 token = cp_lexer_peek_token (parser->lexer);
21918 /* Check for a bitfield declaration. */
21919 if (token->type == CPP_COLON
21920 || (token->type == CPP_NAME
21921 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
21922 == CPP_COLON))
21924 tree identifier;
21925 tree width;
21927 /* Get the name of the bitfield. Note that we cannot just
21928 check TOKEN here because it may have been invalidated by
21929 the call to cp_lexer_peek_nth_token above. */
21930 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
21931 identifier = cp_parser_identifier (parser);
21932 else
21933 identifier = NULL_TREE;
21935 /* Consume the `:' token. */
21936 cp_lexer_consume_token (parser->lexer);
21937 /* Get the width of the bitfield. */
21938 width
21939 = cp_parser_constant_expression (parser);
21941 /* Look for attributes that apply to the bitfield. */
21942 attributes = cp_parser_attributes_opt (parser);
21943 /* Remember which attributes are prefix attributes and
21944 which are not. */
21945 first_attribute = attributes;
21946 /* Combine the attributes. */
21947 attributes = chainon (prefix_attributes, attributes);
21949 /* Create the bitfield declaration. */
21950 decl = grokbitfield (identifier
21951 ? make_id_declarator (NULL_TREE,
21952 identifier,
21953 sfk_none)
21954 : NULL,
21955 &decl_specifiers,
21956 width,
21957 attributes);
21959 else
21961 cp_declarator *declarator;
21962 tree initializer;
21963 tree asm_specification;
21964 int ctor_dtor_or_conv_p;
21966 /* Parse the declarator. */
21967 declarator
21968 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21969 &ctor_dtor_or_conv_p,
21970 /*parenthesized_p=*/NULL,
21971 /*member_p=*/true,
21972 friend_p);
21974 /* If something went wrong parsing the declarator, make sure
21975 that we at least consume some tokens. */
21976 if (declarator == cp_error_declarator)
21978 /* Skip to the end of the statement. */
21979 cp_parser_skip_to_end_of_statement (parser);
21980 /* If the next token is not a semicolon, that is
21981 probably because we just skipped over the body of
21982 a function. So, we consume a semicolon if
21983 present, but do not issue an error message if it
21984 is not present. */
21985 if (cp_lexer_next_token_is (parser->lexer,
21986 CPP_SEMICOLON))
21987 cp_lexer_consume_token (parser->lexer);
21988 goto out;
21991 if (declares_class_or_enum & 2)
21992 cp_parser_check_for_definition_in_return_type
21993 (declarator, decl_specifiers.type,
21994 decl_specifiers.locations[ds_type_spec]);
21996 /* Look for an asm-specification. */
21997 asm_specification = cp_parser_asm_specification_opt (parser);
21998 /* Look for attributes that apply to the declaration. */
21999 attributes = cp_parser_attributes_opt (parser);
22000 /* Remember which attributes are prefix attributes and
22001 which are not. */
22002 first_attribute = attributes;
22003 /* Combine the attributes. */
22004 attributes = chainon (prefix_attributes, attributes);
22006 /* If it's an `=', then we have a constant-initializer or a
22007 pure-specifier. It is not correct to parse the
22008 initializer before registering the member declaration
22009 since the member declaration should be in scope while
22010 its initializer is processed. However, the rest of the
22011 front end does not yet provide an interface that allows
22012 us to handle this correctly. */
22013 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22015 /* In [class.mem]:
22017 A pure-specifier shall be used only in the declaration of
22018 a virtual function.
22020 A member-declarator can contain a constant-initializer
22021 only if it declares a static member of integral or
22022 enumeration type.
22024 Therefore, if the DECLARATOR is for a function, we look
22025 for a pure-specifier; otherwise, we look for a
22026 constant-initializer. When we call `grokfield', it will
22027 perform more stringent semantics checks. */
22028 initializer_token_start = cp_lexer_peek_token (parser->lexer);
22029 if (function_declarator_p (declarator)
22030 || (decl_specifiers.type
22031 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22032 && declarator->kind == cdk_id
22033 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22034 == FUNCTION_TYPE)))
22035 initializer = cp_parser_pure_specifier (parser);
22036 else if (decl_specifiers.storage_class != sc_static)
22037 initializer = cp_parser_save_nsdmi (parser);
22038 else if (cxx_dialect >= cxx11)
22040 bool nonconst;
22041 /* Don't require a constant rvalue in C++11, since we
22042 might want a reference constant. We'll enforce
22043 constancy later. */
22044 cp_lexer_consume_token (parser->lexer);
22045 /* Parse the initializer. */
22046 initializer = cp_parser_initializer_clause (parser,
22047 &nonconst);
22049 else
22050 /* Parse the initializer. */
22051 initializer = cp_parser_constant_initializer (parser);
22053 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22054 && !function_declarator_p (declarator))
22056 bool x;
22057 if (decl_specifiers.storage_class != sc_static)
22058 initializer = cp_parser_save_nsdmi (parser);
22059 else
22060 initializer = cp_parser_initializer (parser, &x, &x);
22062 /* Otherwise, there is no initializer. */
22063 else
22064 initializer = NULL_TREE;
22066 /* See if we are probably looking at a function
22067 definition. We are certainly not looking at a
22068 member-declarator. Calling `grokfield' has
22069 side-effects, so we must not do it unless we are sure
22070 that we are looking at a member-declarator. */
22071 if (cp_parser_token_starts_function_definition_p
22072 (cp_lexer_peek_token (parser->lexer)))
22074 /* The grammar does not allow a pure-specifier to be
22075 used when a member function is defined. (It is
22076 possible that this fact is an oversight in the
22077 standard, since a pure function may be defined
22078 outside of the class-specifier. */
22079 if (initializer && initializer_token_start)
22080 error_at (initializer_token_start->location,
22081 "pure-specifier on function-definition");
22082 decl = cp_parser_save_member_function_body (parser,
22083 &decl_specifiers,
22084 declarator,
22085 attributes);
22086 if (parser->fully_implicit_function_template_p)
22087 decl = finish_fully_implicit_template (parser, decl);
22088 /* If the member was not a friend, declare it here. */
22089 if (!friend_p)
22090 finish_member_declaration (decl);
22091 /* Peek at the next token. */
22092 token = cp_lexer_peek_token (parser->lexer);
22093 /* If the next token is a semicolon, consume it. */
22094 if (token->type == CPP_SEMICOLON)
22095 cp_lexer_consume_token (parser->lexer);
22096 goto out;
22098 else
22099 if (declarator->kind == cdk_function)
22100 declarator->id_loc = token->location;
22101 /* Create the declaration. */
22102 decl = grokfield (declarator, &decl_specifiers,
22103 initializer, /*init_const_expr_p=*/true,
22104 asm_specification, attributes);
22105 if (parser->fully_implicit_function_template_p)
22107 if (friend_p)
22108 finish_fully_implicit_template (parser, 0);
22109 else
22110 decl = finish_fully_implicit_template (parser, decl);
22114 cp_finalize_omp_declare_simd (parser, decl);
22115 cp_finalize_oacc_routine (parser, decl, false);
22117 /* Reset PREFIX_ATTRIBUTES. */
22118 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22119 attributes = TREE_CHAIN (attributes);
22120 if (attributes)
22121 TREE_CHAIN (attributes) = NULL_TREE;
22123 /* If there is any qualification still in effect, clear it
22124 now; we will be starting fresh with the next declarator. */
22125 parser->scope = NULL_TREE;
22126 parser->qualifying_scope = NULL_TREE;
22127 parser->object_scope = NULL_TREE;
22128 /* If it's a `,', then there are more declarators. */
22129 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22131 cp_lexer_consume_token (parser->lexer);
22132 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22134 cp_token *token = cp_lexer_previous_token (parser->lexer);
22135 error_at (token->location,
22136 "stray %<,%> at end of member declaration");
22139 /* If the next token isn't a `;', then we have a parse error. */
22140 else if (cp_lexer_next_token_is_not (parser->lexer,
22141 CPP_SEMICOLON))
22143 /* The next token might be a ways away from where the
22144 actual semicolon is missing. Find the previous token
22145 and use that for our error position. */
22146 cp_token *token = cp_lexer_previous_token (parser->lexer);
22147 error_at (token->location,
22148 "expected %<;%> at end of member declaration");
22150 /* Assume that the user meant to provide a semicolon. If
22151 we were to cp_parser_skip_to_end_of_statement, we might
22152 skip to a semicolon inside a member function definition
22153 and issue nonsensical error messages. */
22154 assume_semicolon = true;
22157 if (decl)
22159 /* Add DECL to the list of members. */
22160 if (!friend_p
22161 /* Explicitly include, eg, NSDMIs, for better error
22162 recovery (c++/58650). */
22163 || !DECL_DECLARES_FUNCTION_P (decl))
22164 finish_member_declaration (decl);
22166 if (TREE_CODE (decl) == FUNCTION_DECL)
22167 cp_parser_save_default_args (parser, decl);
22168 else if (TREE_CODE (decl) == FIELD_DECL
22169 && !DECL_C_BIT_FIELD (decl)
22170 && DECL_INITIAL (decl))
22171 /* Add DECL to the queue of NSDMI to be parsed later. */
22172 vec_safe_push (unparsed_nsdmis, decl);
22175 if (assume_semicolon)
22176 goto out;
22180 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22181 out:
22182 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22185 /* Parse a pure-specifier.
22187 pure-specifier:
22190 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22191 Otherwise, ERROR_MARK_NODE is returned. */
22193 static tree
22194 cp_parser_pure_specifier (cp_parser* parser)
22196 cp_token *token;
22198 /* Look for the `=' token. */
22199 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22200 return error_mark_node;
22201 /* Look for the `0' token. */
22202 token = cp_lexer_peek_token (parser->lexer);
22204 if (token->type == CPP_EOF
22205 || token->type == CPP_PRAGMA_EOL)
22206 return error_mark_node;
22208 cp_lexer_consume_token (parser->lexer);
22210 /* Accept = default or = delete in c++0x mode. */
22211 if (token->keyword == RID_DEFAULT
22212 || token->keyword == RID_DELETE)
22214 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22215 return token->u.value;
22218 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22219 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22221 cp_parser_error (parser,
22222 "invalid pure specifier (only %<= 0%> is allowed)");
22223 cp_parser_skip_to_end_of_statement (parser);
22224 return error_mark_node;
22226 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22228 error_at (token->location, "templates may not be %<virtual%>");
22229 return error_mark_node;
22232 return integer_zero_node;
22235 /* Parse a constant-initializer.
22237 constant-initializer:
22238 = constant-expression
22240 Returns a representation of the constant-expression. */
22242 static tree
22243 cp_parser_constant_initializer (cp_parser* parser)
22245 /* Look for the `=' token. */
22246 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22247 return error_mark_node;
22249 /* It is invalid to write:
22251 struct S { static const int i = { 7 }; };
22254 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22256 cp_parser_error (parser,
22257 "a brace-enclosed initializer is not allowed here");
22258 /* Consume the opening brace. */
22259 cp_lexer_consume_token (parser->lexer);
22260 /* Skip the initializer. */
22261 cp_parser_skip_to_closing_brace (parser);
22262 /* Look for the trailing `}'. */
22263 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22265 return error_mark_node;
22268 return cp_parser_constant_expression (parser);
22271 /* Derived classes [gram.class.derived] */
22273 /* Parse a base-clause.
22275 base-clause:
22276 : base-specifier-list
22278 base-specifier-list:
22279 base-specifier ... [opt]
22280 base-specifier-list , base-specifier ... [opt]
22282 Returns a TREE_LIST representing the base-classes, in the order in
22283 which they were declared. The representation of each node is as
22284 described by cp_parser_base_specifier.
22286 In the case that no bases are specified, this function will return
22287 NULL_TREE, not ERROR_MARK_NODE. */
22289 static tree
22290 cp_parser_base_clause (cp_parser* parser)
22292 tree bases = NULL_TREE;
22294 /* Look for the `:' that begins the list. */
22295 cp_parser_require (parser, CPP_COLON, RT_COLON);
22297 /* Scan the base-specifier-list. */
22298 while (true)
22300 cp_token *token;
22301 tree base;
22302 bool pack_expansion_p = false;
22304 /* Look for the base-specifier. */
22305 base = cp_parser_base_specifier (parser);
22306 /* Look for the (optional) ellipsis. */
22307 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22309 /* Consume the `...'. */
22310 cp_lexer_consume_token (parser->lexer);
22312 pack_expansion_p = true;
22315 /* Add BASE to the front of the list. */
22316 if (base && base != error_mark_node)
22318 if (pack_expansion_p)
22319 /* Make this a pack expansion type. */
22320 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22322 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22324 TREE_CHAIN (base) = bases;
22325 bases = base;
22328 /* Peek at the next token. */
22329 token = cp_lexer_peek_token (parser->lexer);
22330 /* If it's not a comma, then the list is complete. */
22331 if (token->type != CPP_COMMA)
22332 break;
22333 /* Consume the `,'. */
22334 cp_lexer_consume_token (parser->lexer);
22337 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22338 base class had a qualified name. However, the next name that
22339 appears is certainly not qualified. */
22340 parser->scope = NULL_TREE;
22341 parser->qualifying_scope = NULL_TREE;
22342 parser->object_scope = NULL_TREE;
22344 return nreverse (bases);
22347 /* Parse a base-specifier.
22349 base-specifier:
22350 :: [opt] nested-name-specifier [opt] class-name
22351 virtual access-specifier [opt] :: [opt] nested-name-specifier
22352 [opt] class-name
22353 access-specifier virtual [opt] :: [opt] nested-name-specifier
22354 [opt] class-name
22356 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22357 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22358 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22359 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22361 static tree
22362 cp_parser_base_specifier (cp_parser* parser)
22364 cp_token *token;
22365 bool done = false;
22366 bool virtual_p = false;
22367 bool duplicate_virtual_error_issued_p = false;
22368 bool duplicate_access_error_issued_p = false;
22369 bool class_scope_p, template_p;
22370 tree access = access_default_node;
22371 tree type;
22373 /* Process the optional `virtual' and `access-specifier'. */
22374 while (!done)
22376 /* Peek at the next token. */
22377 token = cp_lexer_peek_token (parser->lexer);
22378 /* Process `virtual'. */
22379 switch (token->keyword)
22381 case RID_VIRTUAL:
22382 /* If `virtual' appears more than once, issue an error. */
22383 if (virtual_p && !duplicate_virtual_error_issued_p)
22385 cp_parser_error (parser,
22386 "%<virtual%> specified more than once in base-specified");
22387 duplicate_virtual_error_issued_p = true;
22390 virtual_p = true;
22392 /* Consume the `virtual' token. */
22393 cp_lexer_consume_token (parser->lexer);
22395 break;
22397 case RID_PUBLIC:
22398 case RID_PROTECTED:
22399 case RID_PRIVATE:
22400 /* If more than one access specifier appears, issue an
22401 error. */
22402 if (access != access_default_node
22403 && !duplicate_access_error_issued_p)
22405 cp_parser_error (parser,
22406 "more than one access specifier in base-specified");
22407 duplicate_access_error_issued_p = true;
22410 access = ridpointers[(int) token->keyword];
22412 /* Consume the access-specifier. */
22413 cp_lexer_consume_token (parser->lexer);
22415 break;
22417 default:
22418 done = true;
22419 break;
22422 /* It is not uncommon to see programs mechanically, erroneously, use
22423 the 'typename' keyword to denote (dependent) qualified types
22424 as base classes. */
22425 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22427 token = cp_lexer_peek_token (parser->lexer);
22428 if (!processing_template_decl)
22429 error_at (token->location,
22430 "keyword %<typename%> not allowed outside of templates");
22431 else
22432 error_at (token->location,
22433 "keyword %<typename%> not allowed in this context "
22434 "(the base class is implicitly a type)");
22435 cp_lexer_consume_token (parser->lexer);
22438 /* Look for the optional `::' operator. */
22439 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22440 /* Look for the nested-name-specifier. The simplest way to
22441 implement:
22443 [temp.res]
22445 The keyword `typename' is not permitted in a base-specifier or
22446 mem-initializer; in these contexts a qualified name that
22447 depends on a template-parameter is implicitly assumed to be a
22448 type name.
22450 is to pretend that we have seen the `typename' keyword at this
22451 point. */
22452 cp_parser_nested_name_specifier_opt (parser,
22453 /*typename_keyword_p=*/true,
22454 /*check_dependency_p=*/true,
22455 typename_type,
22456 /*is_declaration=*/true);
22457 /* If the base class is given by a qualified name, assume that names
22458 we see are type names or templates, as appropriate. */
22459 class_scope_p = (parser->scope && TYPE_P (parser->scope));
22460 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
22462 if (!parser->scope
22463 && cp_lexer_next_token_is_decltype (parser->lexer))
22464 /* DR 950 allows decltype as a base-specifier. */
22465 type = cp_parser_decltype (parser);
22466 else
22468 /* Otherwise, look for the class-name. */
22469 type = cp_parser_class_name (parser,
22470 class_scope_p,
22471 template_p,
22472 typename_type,
22473 /*check_dependency_p=*/true,
22474 /*class_head_p=*/false,
22475 /*is_declaration=*/true);
22476 type = TREE_TYPE (type);
22479 if (type == error_mark_node)
22480 return error_mark_node;
22482 return finish_base_specifier (type, access, virtual_p);
22485 /* Exception handling [gram.exception] */
22487 /* Parse an (optional) noexcept-specification.
22489 noexcept-specification:
22490 noexcept ( constant-expression ) [opt]
22492 If no noexcept-specification is present, returns NULL_TREE.
22493 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
22494 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
22495 there are no parentheses. CONSUMED_EXPR will be set accordingly.
22496 Otherwise, returns a noexcept specification unless RETURN_COND is true,
22497 in which case a boolean condition is returned instead. */
22499 static tree
22500 cp_parser_noexcept_specification_opt (cp_parser* parser,
22501 bool require_constexpr,
22502 bool* consumed_expr,
22503 bool return_cond)
22505 cp_token *token;
22506 const char *saved_message;
22508 /* Peek at the next token. */
22509 token = cp_lexer_peek_token (parser->lexer);
22511 /* Is it a noexcept-specification? */
22512 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
22514 tree expr;
22515 cp_lexer_consume_token (parser->lexer);
22517 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
22519 cp_lexer_consume_token (parser->lexer);
22521 if (require_constexpr)
22523 /* Types may not be defined in an exception-specification. */
22524 saved_message = parser->type_definition_forbidden_message;
22525 parser->type_definition_forbidden_message
22526 = G_("types may not be defined in an exception-specification");
22528 expr = cp_parser_constant_expression (parser);
22530 /* Restore the saved message. */
22531 parser->type_definition_forbidden_message = saved_message;
22533 else
22535 expr = cp_parser_expression (parser);
22536 *consumed_expr = true;
22539 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22541 else
22543 expr = boolean_true_node;
22544 if (!require_constexpr)
22545 *consumed_expr = false;
22548 /* We cannot build a noexcept-spec right away because this will check
22549 that expr is a constexpr. */
22550 if (!return_cond)
22551 return build_noexcept_spec (expr, tf_warning_or_error);
22552 else
22553 return expr;
22555 else
22556 return NULL_TREE;
22559 /* Parse an (optional) exception-specification.
22561 exception-specification:
22562 throw ( type-id-list [opt] )
22564 Returns a TREE_LIST representing the exception-specification. The
22565 TREE_VALUE of each node is a type. */
22567 static tree
22568 cp_parser_exception_specification_opt (cp_parser* parser)
22570 cp_token *token;
22571 tree type_id_list;
22572 const char *saved_message;
22574 /* Peek at the next token. */
22575 token = cp_lexer_peek_token (parser->lexer);
22577 /* Is it a noexcept-specification? */
22578 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
22579 false);
22580 if (type_id_list != NULL_TREE)
22581 return type_id_list;
22583 /* If it's not `throw', then there's no exception-specification. */
22584 if (!cp_parser_is_keyword (token, RID_THROW))
22585 return NULL_TREE;
22587 #if 0
22588 /* Enable this once a lot of code has transitioned to noexcept? */
22589 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
22590 warning (OPT_Wdeprecated, "dynamic exception specifications are "
22591 "deprecated in C++0x; use %<noexcept%> instead");
22592 #endif
22594 /* Consume the `throw'. */
22595 cp_lexer_consume_token (parser->lexer);
22597 /* Look for the `('. */
22598 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22600 /* Peek at the next token. */
22601 token = cp_lexer_peek_token (parser->lexer);
22602 /* If it's not a `)', then there is a type-id-list. */
22603 if (token->type != CPP_CLOSE_PAREN)
22605 /* Types may not be defined in an exception-specification. */
22606 saved_message = parser->type_definition_forbidden_message;
22607 parser->type_definition_forbidden_message
22608 = G_("types may not be defined in an exception-specification");
22609 /* Parse the type-id-list. */
22610 type_id_list = cp_parser_type_id_list (parser);
22611 /* Restore the saved message. */
22612 parser->type_definition_forbidden_message = saved_message;
22614 else
22615 type_id_list = empty_except_spec;
22617 /* Look for the `)'. */
22618 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22620 return type_id_list;
22623 /* Parse an (optional) type-id-list.
22625 type-id-list:
22626 type-id ... [opt]
22627 type-id-list , type-id ... [opt]
22629 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
22630 in the order that the types were presented. */
22632 static tree
22633 cp_parser_type_id_list (cp_parser* parser)
22635 tree types = NULL_TREE;
22637 while (true)
22639 cp_token *token;
22640 tree type;
22642 /* Get the next type-id. */
22643 type = cp_parser_type_id (parser);
22644 /* Parse the optional ellipsis. */
22645 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22647 /* Consume the `...'. */
22648 cp_lexer_consume_token (parser->lexer);
22650 /* Turn the type into a pack expansion expression. */
22651 type = make_pack_expansion (type);
22653 /* Add it to the list. */
22654 types = add_exception_specifier (types, type, /*complain=*/1);
22655 /* Peek at the next token. */
22656 token = cp_lexer_peek_token (parser->lexer);
22657 /* If it is not a `,', we are done. */
22658 if (token->type != CPP_COMMA)
22659 break;
22660 /* Consume the `,'. */
22661 cp_lexer_consume_token (parser->lexer);
22664 return nreverse (types);
22667 /* Parse a try-block.
22669 try-block:
22670 try compound-statement handler-seq */
22672 static tree
22673 cp_parser_try_block (cp_parser* parser)
22675 tree try_block;
22677 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
22678 if (parser->in_function_body
22679 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
22680 error ("%<try%> in %<constexpr%> function");
22682 try_block = begin_try_block ();
22683 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
22684 finish_try_block (try_block);
22685 cp_parser_handler_seq (parser);
22686 finish_handler_sequence (try_block);
22688 return try_block;
22691 /* Parse a function-try-block.
22693 function-try-block:
22694 try ctor-initializer [opt] function-body handler-seq */
22696 static bool
22697 cp_parser_function_try_block (cp_parser* parser)
22699 tree compound_stmt;
22700 tree try_block;
22701 bool ctor_initializer_p;
22703 /* Look for the `try' keyword. */
22704 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
22705 return false;
22706 /* Let the rest of the front end know where we are. */
22707 try_block = begin_function_try_block (&compound_stmt);
22708 /* Parse the function-body. */
22709 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22710 (parser, /*in_function_try_block=*/true);
22711 /* We're done with the `try' part. */
22712 finish_function_try_block (try_block);
22713 /* Parse the handlers. */
22714 cp_parser_handler_seq (parser);
22715 /* We're done with the handlers. */
22716 finish_function_handler_sequence (try_block, compound_stmt);
22718 return ctor_initializer_p;
22721 /* Parse a handler-seq.
22723 handler-seq:
22724 handler handler-seq [opt] */
22726 static void
22727 cp_parser_handler_seq (cp_parser* parser)
22729 while (true)
22731 cp_token *token;
22733 /* Parse the handler. */
22734 cp_parser_handler (parser);
22735 /* Peek at the next token. */
22736 token = cp_lexer_peek_token (parser->lexer);
22737 /* If it's not `catch' then there are no more handlers. */
22738 if (!cp_parser_is_keyword (token, RID_CATCH))
22739 break;
22743 /* Parse a handler.
22745 handler:
22746 catch ( exception-declaration ) compound-statement */
22748 static void
22749 cp_parser_handler (cp_parser* parser)
22751 tree handler;
22752 tree declaration;
22754 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
22755 handler = begin_handler ();
22756 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22757 declaration = cp_parser_exception_declaration (parser);
22758 finish_handler_parms (declaration, handler);
22759 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22760 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
22761 finish_handler (handler);
22764 /* Parse an exception-declaration.
22766 exception-declaration:
22767 type-specifier-seq declarator
22768 type-specifier-seq abstract-declarator
22769 type-specifier-seq
22772 Returns a VAR_DECL for the declaration, or NULL_TREE if the
22773 ellipsis variant is used. */
22775 static tree
22776 cp_parser_exception_declaration (cp_parser* parser)
22778 cp_decl_specifier_seq type_specifiers;
22779 cp_declarator *declarator;
22780 const char *saved_message;
22782 /* If it's an ellipsis, it's easy to handle. */
22783 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22785 /* Consume the `...' token. */
22786 cp_lexer_consume_token (parser->lexer);
22787 return NULL_TREE;
22790 /* Types may not be defined in exception-declarations. */
22791 saved_message = parser->type_definition_forbidden_message;
22792 parser->type_definition_forbidden_message
22793 = G_("types may not be defined in exception-declarations");
22795 /* Parse the type-specifier-seq. */
22796 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22797 /*is_trailing_return=*/false,
22798 &type_specifiers);
22799 /* If it's a `)', then there is no declarator. */
22800 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22801 declarator = NULL;
22802 else
22803 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
22804 /*ctor_dtor_or_conv_p=*/NULL,
22805 /*parenthesized_p=*/NULL,
22806 /*member_p=*/false,
22807 /*friend_p=*/false);
22809 /* Restore the saved message. */
22810 parser->type_definition_forbidden_message = saved_message;
22812 if (!type_specifiers.any_specifiers_p)
22813 return error_mark_node;
22815 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
22818 /* Parse a throw-expression.
22820 throw-expression:
22821 throw assignment-expression [opt]
22823 Returns a THROW_EXPR representing the throw-expression. */
22825 static tree
22826 cp_parser_throw_expression (cp_parser* parser)
22828 tree expression;
22829 cp_token* token;
22831 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
22832 token = cp_lexer_peek_token (parser->lexer);
22833 /* Figure out whether or not there is an assignment-expression
22834 following the "throw" keyword. */
22835 if (token->type == CPP_COMMA
22836 || token->type == CPP_SEMICOLON
22837 || token->type == CPP_CLOSE_PAREN
22838 || token->type == CPP_CLOSE_SQUARE
22839 || token->type == CPP_CLOSE_BRACE
22840 || token->type == CPP_COLON)
22841 expression = NULL_TREE;
22842 else
22843 expression = cp_parser_assignment_expression (parser);
22845 return build_throw (expression);
22848 /* GNU Extensions */
22850 /* Parse an (optional) asm-specification.
22852 asm-specification:
22853 asm ( string-literal )
22855 If the asm-specification is present, returns a STRING_CST
22856 corresponding to the string-literal. Otherwise, returns
22857 NULL_TREE. */
22859 static tree
22860 cp_parser_asm_specification_opt (cp_parser* parser)
22862 cp_token *token;
22863 tree asm_specification;
22865 /* Peek at the next token. */
22866 token = cp_lexer_peek_token (parser->lexer);
22867 /* If the next token isn't the `asm' keyword, then there's no
22868 asm-specification. */
22869 if (!cp_parser_is_keyword (token, RID_ASM))
22870 return NULL_TREE;
22872 /* Consume the `asm' token. */
22873 cp_lexer_consume_token (parser->lexer);
22874 /* Look for the `('. */
22875 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22877 /* Look for the string-literal. */
22878 asm_specification = cp_parser_string_literal (parser, false, false);
22880 /* Look for the `)'. */
22881 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22883 return asm_specification;
22886 /* Parse an asm-operand-list.
22888 asm-operand-list:
22889 asm-operand
22890 asm-operand-list , asm-operand
22892 asm-operand:
22893 string-literal ( expression )
22894 [ string-literal ] string-literal ( expression )
22896 Returns a TREE_LIST representing the operands. The TREE_VALUE of
22897 each node is the expression. The TREE_PURPOSE is itself a
22898 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
22899 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
22900 is a STRING_CST for the string literal before the parenthesis. Returns
22901 ERROR_MARK_NODE if any of the operands are invalid. */
22903 static tree
22904 cp_parser_asm_operand_list (cp_parser* parser)
22906 tree asm_operands = NULL_TREE;
22907 bool invalid_operands = false;
22909 while (true)
22911 tree string_literal;
22912 tree expression;
22913 tree name;
22915 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22917 /* Consume the `[' token. */
22918 cp_lexer_consume_token (parser->lexer);
22919 /* Read the operand name. */
22920 name = cp_parser_identifier (parser);
22921 if (name != error_mark_node)
22922 name = build_string (IDENTIFIER_LENGTH (name),
22923 IDENTIFIER_POINTER (name));
22924 /* Look for the closing `]'. */
22925 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22927 else
22928 name = NULL_TREE;
22929 /* Look for the string-literal. */
22930 string_literal = cp_parser_string_literal (parser, false, false);
22932 /* Look for the `('. */
22933 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22934 /* Parse the expression. */
22935 expression = cp_parser_expression (parser);
22936 /* Look for the `)'. */
22937 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22939 if (name == error_mark_node
22940 || string_literal == error_mark_node
22941 || expression == error_mark_node)
22942 invalid_operands = true;
22944 /* Add this operand to the list. */
22945 asm_operands = tree_cons (build_tree_list (name, string_literal),
22946 expression,
22947 asm_operands);
22948 /* If the next token is not a `,', there are no more
22949 operands. */
22950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22951 break;
22952 /* Consume the `,'. */
22953 cp_lexer_consume_token (parser->lexer);
22956 return invalid_operands ? error_mark_node : nreverse (asm_operands);
22959 /* Parse an asm-clobber-list.
22961 asm-clobber-list:
22962 string-literal
22963 asm-clobber-list , string-literal
22965 Returns a TREE_LIST, indicating the clobbers in the order that they
22966 appeared. The TREE_VALUE of each node is a STRING_CST. */
22968 static tree
22969 cp_parser_asm_clobber_list (cp_parser* parser)
22971 tree clobbers = NULL_TREE;
22973 while (true)
22975 tree string_literal;
22977 /* Look for the string literal. */
22978 string_literal = cp_parser_string_literal (parser, false, false);
22979 /* Add it to the list. */
22980 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
22981 /* If the next token is not a `,', then the list is
22982 complete. */
22983 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22984 break;
22985 /* Consume the `,' token. */
22986 cp_lexer_consume_token (parser->lexer);
22989 return clobbers;
22992 /* Parse an asm-label-list.
22994 asm-label-list:
22995 identifier
22996 asm-label-list , identifier
22998 Returns a TREE_LIST, indicating the labels in the order that they
22999 appeared. The TREE_VALUE of each node is a label. */
23001 static tree
23002 cp_parser_asm_label_list (cp_parser* parser)
23004 tree labels = NULL_TREE;
23006 while (true)
23008 tree identifier, label, name;
23010 /* Look for the identifier. */
23011 identifier = cp_parser_identifier (parser);
23012 if (!error_operand_p (identifier))
23014 label = lookup_label (identifier);
23015 if (TREE_CODE (label) == LABEL_DECL)
23017 TREE_USED (label) = 1;
23018 check_goto (label);
23019 name = build_string (IDENTIFIER_LENGTH (identifier),
23020 IDENTIFIER_POINTER (identifier));
23021 labels = tree_cons (name, label, labels);
23024 /* If the next token is not a `,', then the list is
23025 complete. */
23026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23027 break;
23028 /* Consume the `,' token. */
23029 cp_lexer_consume_token (parser->lexer);
23032 return nreverse (labels);
23035 /* Return TRUE iff the next tokens in the stream are possibly the
23036 beginning of a GNU extension attribute. */
23038 static bool
23039 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23041 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23044 /* Return TRUE iff the next tokens in the stream are possibly the
23045 beginning of a standard C++-11 attribute specifier. */
23047 static bool
23048 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23050 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23053 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23054 beginning of a standard C++-11 attribute specifier. */
23056 static bool
23057 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23059 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23061 return (cxx_dialect >= cxx11
23062 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23063 || (token->type == CPP_OPEN_SQUARE
23064 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23065 && token->type == CPP_OPEN_SQUARE)));
23068 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23069 beginning of a GNU extension attribute. */
23071 static bool
23072 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23074 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23076 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23079 /* Return true iff the next tokens can be the beginning of either a
23080 GNU attribute list, or a standard C++11 attribute sequence. */
23082 static bool
23083 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23085 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23086 || cp_next_tokens_can_be_std_attribute_p (parser));
23089 /* Return true iff the next Nth tokens can be the beginning of either
23090 a GNU attribute list, or a standard C++11 attribute sequence. */
23092 static bool
23093 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23095 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23096 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23099 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23100 of GNU attributes, or return NULL. */
23102 static tree
23103 cp_parser_attributes_opt (cp_parser *parser)
23105 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23106 return cp_parser_gnu_attributes_opt (parser);
23107 return cp_parser_std_attribute_spec_seq (parser);
23110 #define CILK_SIMD_FN_CLAUSE_MASK \
23111 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23112 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23113 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23114 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23115 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23117 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23118 vector [(<clauses>)] */
23120 static void
23121 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23123 bool first_p = parser->cilk_simd_fn_info == NULL;
23124 cp_token *token = v_token;
23125 if (first_p)
23127 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23128 parser->cilk_simd_fn_info->error_seen = false;
23129 parser->cilk_simd_fn_info->fndecl_seen = false;
23130 parser->cilk_simd_fn_info->tokens = vNULL;
23132 int paren_scope = 0;
23133 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23135 cp_lexer_consume_token (parser->lexer);
23136 v_token = cp_lexer_peek_token (parser->lexer);
23137 paren_scope++;
23139 while (paren_scope > 0)
23141 token = cp_lexer_peek_token (parser->lexer);
23142 if (token->type == CPP_OPEN_PAREN)
23143 paren_scope++;
23144 else if (token->type == CPP_CLOSE_PAREN)
23145 paren_scope--;
23146 /* Do not push the last ')' */
23147 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23148 cp_lexer_consume_token (parser->lexer);
23151 token->type = CPP_PRAGMA_EOL;
23152 parser->lexer->next_token = token;
23153 cp_lexer_consume_token (parser->lexer);
23155 struct cp_token_cache *cp
23156 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23157 parser->cilk_simd_fn_info->tokens.safe_push (cp);
23160 /* Parse an (optional) series of attributes.
23162 attributes:
23163 attributes attribute
23165 attribute:
23166 __attribute__ (( attribute-list [opt] ))
23168 The return value is as for cp_parser_gnu_attribute_list. */
23170 static tree
23171 cp_parser_gnu_attributes_opt (cp_parser* parser)
23173 tree attributes = NULL_TREE;
23175 while (true)
23177 cp_token *token;
23178 tree attribute_list;
23179 bool ok = true;
23181 /* Peek at the next token. */
23182 token = cp_lexer_peek_token (parser->lexer);
23183 /* If it's not `__attribute__', then we're done. */
23184 if (token->keyword != RID_ATTRIBUTE)
23185 break;
23187 /* Consume the `__attribute__' keyword. */
23188 cp_lexer_consume_token (parser->lexer);
23189 /* Look for the two `(' tokens. */
23190 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23191 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23193 /* Peek at the next token. */
23194 token = cp_lexer_peek_token (parser->lexer);
23195 if (token->type != CPP_CLOSE_PAREN)
23196 /* Parse the attribute-list. */
23197 attribute_list = cp_parser_gnu_attribute_list (parser);
23198 else
23199 /* If the next token is a `)', then there is no attribute
23200 list. */
23201 attribute_list = NULL;
23203 /* Look for the two `)' tokens. */
23204 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23205 ok = false;
23206 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23207 ok = false;
23208 if (!ok)
23209 cp_parser_skip_to_end_of_statement (parser);
23211 /* Add these new attributes to the list. */
23212 attributes = chainon (attributes, attribute_list);
23215 return attributes;
23218 /* Parse a GNU attribute-list.
23220 attribute-list:
23221 attribute
23222 attribute-list , attribute
23224 attribute:
23225 identifier
23226 identifier ( identifier )
23227 identifier ( identifier , expression-list )
23228 identifier ( expression-list )
23230 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23231 to an attribute. The TREE_PURPOSE of each node is the identifier
23232 indicating which attribute is in use. The TREE_VALUE represents
23233 the arguments, if any. */
23235 static tree
23236 cp_parser_gnu_attribute_list (cp_parser* parser)
23238 tree attribute_list = NULL_TREE;
23239 bool save_translate_strings_p = parser->translate_strings_p;
23241 parser->translate_strings_p = false;
23242 while (true)
23244 cp_token *token;
23245 tree identifier;
23246 tree attribute;
23248 /* Look for the identifier. We also allow keywords here; for
23249 example `__attribute__ ((const))' is legal. */
23250 token = cp_lexer_peek_token (parser->lexer);
23251 if (token->type == CPP_NAME
23252 || token->type == CPP_KEYWORD)
23254 tree arguments = NULL_TREE;
23256 /* Consume the token, but save it since we need it for the
23257 SIMD enabled function parsing. */
23258 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23260 /* Save away the identifier that indicates which attribute
23261 this is. */
23262 identifier = (token->type == CPP_KEYWORD)
23263 /* For keywords, use the canonical spelling, not the
23264 parsed identifier. */
23265 ? ridpointers[(int) token->keyword]
23266 : id_token->u.value;
23268 attribute = build_tree_list (identifier, NULL_TREE);
23270 /* Peek at the next token. */
23271 token = cp_lexer_peek_token (parser->lexer);
23272 /* If it's an `(', then parse the attribute arguments. */
23273 if (token->type == CPP_OPEN_PAREN)
23275 vec<tree, va_gc> *vec;
23276 int attr_flag = (attribute_takes_identifier_p (identifier)
23277 ? id_attr : normal_attr);
23278 if (is_cilkplus_vector_p (identifier))
23280 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23281 continue;
23283 else
23284 vec = cp_parser_parenthesized_expression_list
23285 (parser, attr_flag, /*cast_p=*/false,
23286 /*allow_expansion_p=*/false,
23287 /*non_constant_p=*/NULL);
23288 if (vec == NULL)
23289 arguments = error_mark_node;
23290 else
23292 arguments = build_tree_list_vec (vec);
23293 release_tree_vector (vec);
23295 /* Save the arguments away. */
23296 TREE_VALUE (attribute) = arguments;
23298 else if (is_cilkplus_vector_p (identifier))
23300 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23301 continue;
23304 if (arguments != error_mark_node)
23306 /* Add this attribute to the list. */
23307 TREE_CHAIN (attribute) = attribute_list;
23308 attribute_list = attribute;
23311 token = cp_lexer_peek_token (parser->lexer);
23313 /* Now, look for more attributes. If the next token isn't a
23314 `,', we're done. */
23315 if (token->type != CPP_COMMA)
23316 break;
23318 /* Consume the comma and keep going. */
23319 cp_lexer_consume_token (parser->lexer);
23321 parser->translate_strings_p = save_translate_strings_p;
23323 /* We built up the list in reverse order. */
23324 return nreverse (attribute_list);
23327 /* Parse a standard C++11 attribute.
23329 The returned representation is a TREE_LIST which TREE_PURPOSE is
23330 the scoped name of the attribute, and the TREE_VALUE is its
23331 arguments list.
23333 Note that the scoped name of the attribute is itself a TREE_LIST
23334 which TREE_PURPOSE is the namespace of the attribute, and
23335 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23336 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23337 and which TREE_PURPOSE is directly the attribute name.
23339 Clients of the attribute code should use get_attribute_namespace
23340 and get_attribute_name to get the actual namespace and name of
23341 attributes, regardless of their being GNU or C++11 attributes.
23343 attribute:
23344 attribute-token attribute-argument-clause [opt]
23346 attribute-token:
23347 identifier
23348 attribute-scoped-token
23350 attribute-scoped-token:
23351 attribute-namespace :: identifier
23353 attribute-namespace:
23354 identifier
23356 attribute-argument-clause:
23357 ( balanced-token-seq )
23359 balanced-token-seq:
23360 balanced-token [opt]
23361 balanced-token-seq balanced-token
23363 balanced-token:
23364 ( balanced-token-seq )
23365 [ balanced-token-seq ]
23366 { balanced-token-seq }. */
23368 static tree
23369 cp_parser_std_attribute (cp_parser *parser)
23371 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23372 cp_token *token;
23374 /* First, parse name of the attribute, a.k.a attribute-token. */
23376 token = cp_lexer_peek_token (parser->lexer);
23377 if (token->type == CPP_NAME)
23378 attr_id = token->u.value;
23379 else if (token->type == CPP_KEYWORD)
23380 attr_id = ridpointers[(int) token->keyword];
23381 else if (token->flags & NAMED_OP)
23382 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23384 if (attr_id == NULL_TREE)
23385 return NULL_TREE;
23387 cp_lexer_consume_token (parser->lexer);
23389 token = cp_lexer_peek_token (parser->lexer);
23390 if (token->type == CPP_SCOPE)
23392 /* We are seeing a scoped attribute token. */
23394 cp_lexer_consume_token (parser->lexer);
23395 attr_ns = attr_id;
23397 token = cp_lexer_consume_token (parser->lexer);
23398 if (token->type == CPP_NAME)
23399 attr_id = token->u.value;
23400 else if (token->type == CPP_KEYWORD)
23401 attr_id = ridpointers[(int) token->keyword];
23402 else
23404 error_at (token->location,
23405 "expected an identifier for the attribute name");
23406 return error_mark_node;
23408 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23409 NULL_TREE);
23410 token = cp_lexer_peek_token (parser->lexer);
23412 else
23414 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23415 NULL_TREE);
23416 /* C++11 noreturn attribute is equivalent to GNU's. */
23417 if (is_attribute_p ("noreturn", attr_id))
23418 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23419 /* C++14 deprecated attribute is equivalent to GNU's. */
23420 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23422 if (cxx_dialect == cxx11)
23423 pedwarn (token->location, OPT_Wpedantic,
23424 "%<deprecated%> is a C++14 feature;"
23425 " use %<gnu::deprecated%>");
23426 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23428 /* Transactional Memory TS optimize_for_synchronized attribute is
23429 equivalent to GNU transaction_callable. */
23430 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
23431 TREE_PURPOSE (attribute)
23432 = get_identifier ("transaction_callable");
23433 /* Transactional Memory attributes are GNU attributes. */
23434 else if (tm_attr_to_mask (attr_id))
23435 TREE_PURPOSE (attribute) = attr_id;
23438 /* Now parse the optional argument clause of the attribute. */
23440 if (token->type != CPP_OPEN_PAREN)
23441 return attribute;
23444 vec<tree, va_gc> *vec;
23445 int attr_flag = normal_attr;
23447 if (attr_ns == get_identifier ("gnu")
23448 && attribute_takes_identifier_p (attr_id))
23449 /* A GNU attribute that takes an identifier in parameter. */
23450 attr_flag = id_attr;
23452 vec = cp_parser_parenthesized_expression_list
23453 (parser, attr_flag, /*cast_p=*/false,
23454 /*allow_expansion_p=*/true,
23455 /*non_constant_p=*/NULL);
23456 if (vec == NULL)
23457 arguments = error_mark_node;
23458 else
23460 arguments = build_tree_list_vec (vec);
23461 release_tree_vector (vec);
23464 if (arguments == error_mark_node)
23465 attribute = error_mark_node;
23466 else
23467 TREE_VALUE (attribute) = arguments;
23470 return attribute;
23473 /* Check that the attribute ATTRIBUTE appears at most once in the
23474 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
23475 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
23476 isn't implemented yet in GCC. */
23478 static void
23479 cp_parser_check_std_attribute (tree attributes, tree attribute)
23481 if (attributes)
23483 tree name = get_attribute_name (attribute);
23484 if (is_attribute_p ("noreturn", name)
23485 && lookup_attribute ("noreturn", attributes))
23486 error ("attribute noreturn can appear at most once "
23487 "in an attribute-list");
23488 else if (is_attribute_p ("deprecated", name)
23489 && lookup_attribute ("deprecated", attributes))
23490 error ("attribute deprecated can appear at most once "
23491 "in an attribute-list");
23495 /* Parse a list of standard C++-11 attributes.
23497 attribute-list:
23498 attribute [opt]
23499 attribute-list , attribute[opt]
23500 attribute ...
23501 attribute-list , attribute ...
23504 static tree
23505 cp_parser_std_attribute_list (cp_parser *parser)
23507 tree attributes = NULL_TREE, attribute = NULL_TREE;
23508 cp_token *token = NULL;
23510 while (true)
23512 attribute = cp_parser_std_attribute (parser);
23513 if (attribute == error_mark_node)
23514 break;
23515 if (attribute != NULL_TREE)
23517 cp_parser_check_std_attribute (attributes, attribute);
23518 TREE_CHAIN (attribute) = attributes;
23519 attributes = attribute;
23521 token = cp_lexer_peek_token (parser->lexer);
23522 if (token->type == CPP_ELLIPSIS)
23524 cp_lexer_consume_token (parser->lexer);
23525 TREE_VALUE (attribute)
23526 = make_pack_expansion (TREE_VALUE (attribute));
23527 token = cp_lexer_peek_token (parser->lexer);
23529 if (token->type != CPP_COMMA)
23530 break;
23531 cp_lexer_consume_token (parser->lexer);
23533 attributes = nreverse (attributes);
23534 return attributes;
23537 /* Parse a standard C++-11 attribute specifier.
23539 attribute-specifier:
23540 [ [ attribute-list ] ]
23541 alignment-specifier
23543 alignment-specifier:
23544 alignas ( type-id ... [opt] )
23545 alignas ( alignment-expression ... [opt] ). */
23547 static tree
23548 cp_parser_std_attribute_spec (cp_parser *parser)
23550 tree attributes = NULL_TREE;
23551 cp_token *token = cp_lexer_peek_token (parser->lexer);
23553 if (token->type == CPP_OPEN_SQUARE
23554 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
23556 cp_lexer_consume_token (parser->lexer);
23557 cp_lexer_consume_token (parser->lexer);
23559 attributes = cp_parser_std_attribute_list (parser);
23561 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
23562 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23563 cp_parser_skip_to_end_of_statement (parser);
23564 else
23565 /* Warn about parsing c++11 attribute in non-c++1 mode, only
23566 when we are sure that we have actually parsed them. */
23567 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
23569 else
23571 tree alignas_expr;
23573 /* Look for an alignment-specifier. */
23575 token = cp_lexer_peek_token (parser->lexer);
23577 if (token->type != CPP_KEYWORD
23578 || token->keyword != RID_ALIGNAS)
23579 return NULL_TREE;
23581 cp_lexer_consume_token (parser->lexer);
23582 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
23584 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
23586 cp_parser_error (parser, "expected %<(%>");
23587 return error_mark_node;
23590 cp_parser_parse_tentatively (parser);
23591 alignas_expr = cp_parser_type_id (parser);
23593 if (!cp_parser_parse_definitely (parser))
23595 gcc_assert (alignas_expr == error_mark_node
23596 || alignas_expr == NULL_TREE);
23598 alignas_expr =
23599 cp_parser_assignment_expression (parser);
23600 if (alignas_expr == error_mark_node)
23601 cp_parser_skip_to_end_of_statement (parser);
23602 if (alignas_expr == NULL_TREE
23603 || alignas_expr == error_mark_node)
23604 return alignas_expr;
23607 alignas_expr = cxx_alignas_expr (alignas_expr);
23608 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
23610 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23612 cp_lexer_consume_token (parser->lexer);
23613 alignas_expr = make_pack_expansion (alignas_expr);
23616 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
23618 cp_parser_error (parser, "expected %<)%>");
23619 return error_mark_node;
23622 /* Build the C++-11 representation of an 'aligned'
23623 attribute. */
23624 attributes =
23625 build_tree_list (build_tree_list (get_identifier ("gnu"),
23626 get_identifier ("aligned")),
23627 alignas_expr);
23630 return attributes;
23633 /* Parse a standard C++-11 attribute-specifier-seq.
23635 attribute-specifier-seq:
23636 attribute-specifier-seq [opt] attribute-specifier
23639 static tree
23640 cp_parser_std_attribute_spec_seq (cp_parser *parser)
23642 tree attr_specs = NULL;
23644 while (true)
23646 tree attr_spec = cp_parser_std_attribute_spec (parser);
23647 if (attr_spec == NULL_TREE)
23648 break;
23649 if (attr_spec == error_mark_node)
23650 return error_mark_node;
23652 TREE_CHAIN (attr_spec) = attr_specs;
23653 attr_specs = attr_spec;
23656 attr_specs = nreverse (attr_specs);
23657 return attr_specs;
23660 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
23661 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
23662 current value of the PEDANTIC flag, regardless of whether or not
23663 the `__extension__' keyword is present. The caller is responsible
23664 for restoring the value of the PEDANTIC flag. */
23666 static bool
23667 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
23669 /* Save the old value of the PEDANTIC flag. */
23670 *saved_pedantic = pedantic;
23672 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
23674 /* Consume the `__extension__' token. */
23675 cp_lexer_consume_token (parser->lexer);
23676 /* We're not being pedantic while the `__extension__' keyword is
23677 in effect. */
23678 pedantic = 0;
23680 return true;
23683 return false;
23686 /* Parse a label declaration.
23688 label-declaration:
23689 __label__ label-declarator-seq ;
23691 label-declarator-seq:
23692 identifier , label-declarator-seq
23693 identifier */
23695 static void
23696 cp_parser_label_declaration (cp_parser* parser)
23698 /* Look for the `__label__' keyword. */
23699 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
23701 while (true)
23703 tree identifier;
23705 /* Look for an identifier. */
23706 identifier = cp_parser_identifier (parser);
23707 /* If we failed, stop. */
23708 if (identifier == error_mark_node)
23709 break;
23710 /* Declare it as a label. */
23711 finish_label_decl (identifier);
23712 /* If the next token is a `;', stop. */
23713 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23714 break;
23715 /* Look for the `,' separating the label declarations. */
23716 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
23719 /* Look for the final `;'. */
23720 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23723 // -------------------------------------------------------------------------- //
23724 // Requires Clause
23726 // Parse a requires clause.
23728 // requires-clause:
23729 // 'requires' logical-or-expression
23731 // The required logical-or-expression must be a constant expression. Note
23732 // that we don't check that the expression is constepxr here. We defer until
23733 // we analyze constraints and then, we only check atomic constraints.
23734 static tree
23735 cp_parser_requires_clause (cp_parser *parser)
23737 // Parse the requires clause so that it is not automatically folded.
23738 ++processing_template_decl;
23739 tree expr = cp_parser_binary_expression (parser, false, false,
23740 PREC_NOT_OPERATOR, NULL);
23741 --processing_template_decl;
23742 return expr;
23745 // Optionally parse a requires clause:
23746 static tree
23747 cp_parser_requires_clause_opt (cp_parser *parser)
23749 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
23750 return NULL_TREE;
23751 cp_lexer_consume_token (parser->lexer);
23752 return cp_parser_requires_clause (parser);
23756 /*---------------------------------------------------------------------------
23757 Requires expressions
23758 ---------------------------------------------------------------------------*/
23760 /* Parse a requires expression
23762 requirement-expression:
23763 'requires' requirement-parameter-list [opt] requirement-body */
23764 static tree
23765 cp_parser_requires_expression (cp_parser *parser)
23767 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
23768 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
23770 /* A requires-expression shall appear only within a concept
23771 definition or a requires-clause.
23773 TODO: Implement this diagnostic correctly. */
23774 if (!processing_template_decl)
23776 error_at (loc, "a requires expression cannot appear outside a template");
23777 cp_parser_skip_to_end_of_statement (parser);
23778 return error_mark_node;
23781 tree parms, reqs;
23783 /* Local parameters are delared as variables within the scope
23784 of the expression. They are not visible past the end of
23785 the expression. Expressions within the requires-expression
23786 are unevaluated. */
23787 struct scope_sentinel
23789 scope_sentinel ()
23791 ++cp_unevaluated_operand;
23792 begin_scope (sk_block, NULL_TREE);
23795 ~scope_sentinel ()
23797 pop_bindings_and_leave_scope ();
23798 --cp_unevaluated_operand;
23800 } s;
23802 /* Parse the optional parameter list. */
23803 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23805 parms = cp_parser_requirement_parameter_list (parser);
23806 if (parms == error_mark_node)
23807 return error_mark_node;
23809 else
23810 parms = NULL_TREE;
23812 /* Parse the requirement body. */
23813 reqs = cp_parser_requirement_body (parser);
23814 if (reqs == error_mark_node)
23815 return error_mark_node;
23818 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
23819 the parm chain. */
23820 grokparms (parms, &parms);
23821 return finish_requires_expr (parms, reqs);
23824 /* Parse a parameterized requirement.
23826 requirement-parameter-list:
23827 '(' parameter-declaration-clause ')' */
23828 static tree
23829 cp_parser_requirement_parameter_list (cp_parser *parser)
23831 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23832 return error_mark_node;
23834 tree parms = cp_parser_parameter_declaration_clause (parser);
23836 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23837 return error_mark_node;
23839 return parms;
23842 /* Parse the body of a requirement.
23844 requirement-body:
23845 '{' requirement-list '}' */
23846 static tree
23847 cp_parser_requirement_body (cp_parser *parser)
23849 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23850 return error_mark_node;
23852 tree reqs = cp_parser_requirement_list (parser);
23854 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
23855 return error_mark_node;
23857 return reqs;
23860 /* Parse a list of requirements.
23862 requirement-list:
23863 requirement
23864 requirement-list ';' requirement[opt] */
23865 static tree
23866 cp_parser_requirement_list (cp_parser *parser)
23868 tree result = NULL_TREE;
23869 while (true)
23871 tree req = cp_parser_requirement (parser);
23872 if (req == error_mark_node)
23873 return error_mark_node;
23875 result = tree_cons (NULL_TREE, req, result);
23877 /* If we see a semi-colon, consume it. */
23878 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23879 cp_lexer_consume_token (parser->lexer);
23881 /* Stop processing at the end of the list. */
23882 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23883 break;
23886 /* Reverse the order of requirements so they are analyzed in
23887 declaration order. */
23888 return nreverse (result);
23891 /* Parse a syntactic requirement or type requirement.
23893 requirement:
23894 simple-requirement
23895 compound-requirement
23896 type-requirement
23897 nested-requirement */
23898 static tree
23899 cp_parser_requirement (cp_parser *parser)
23901 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23902 return cp_parser_compound_requirement (parser);
23903 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23904 return cp_parser_type_requirement (parser);
23905 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
23906 return cp_parser_nested_requirement (parser);
23907 else
23908 return cp_parser_simple_requirement (parser);
23911 /* Parse a simple requirement.
23913 simple-requirement:
23914 expression ';' */
23915 static tree
23916 cp_parser_simple_requirement (cp_parser *parser)
23918 tree expr = cp_parser_expression (parser, NULL, false, false);
23919 if (!expr || expr == error_mark_node)
23920 return error_mark_node;
23922 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
23923 return error_mark_node;
23925 return finish_simple_requirement (expr);
23928 /* Parse a type requirement
23930 type-requirement
23931 nested-name-specifier [opt] required-type-name ';'
23933 required-type-name:
23934 type-name
23935 'template' [opt] simple-template-id */
23936 static tree
23937 cp_parser_type_requirement (cp_parser *parser)
23939 cp_lexer_consume_token (parser->lexer);
23941 // Save the scope before parsing name specifiers.
23942 tree saved_scope = parser->scope;
23943 tree saved_object_scope = parser->object_scope;
23944 tree saved_qualifying_scope = parser->qualifying_scope;
23945 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
23946 cp_parser_nested_name_specifier_opt (parser,
23947 /*typename_keyword_p=*/true,
23948 /*check_dependency_p=*/false,
23949 /*type_p=*/true,
23950 /*is_declaration=*/false);
23952 tree type;
23953 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23955 cp_lexer_consume_token (parser->lexer);
23956 type = cp_parser_template_id (parser,
23957 /*template_keyword_p=*/true,
23958 /*check_dependency=*/false,
23959 /*tag_type=*/none_type,
23960 /*is_declaration=*/false);
23961 type = make_typename_type (parser->scope, type, typename_type,
23962 /*complain=*/tf_error);
23964 else
23965 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
23967 if (TREE_CODE (type) == TYPE_DECL)
23968 type = TREE_TYPE (type);
23970 parser->scope = saved_scope;
23971 parser->object_scope = saved_object_scope;
23972 parser->qualifying_scope = saved_qualifying_scope;
23974 if (type == error_mark_node)
23975 cp_parser_skip_to_end_of_statement (parser);
23977 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
23978 return error_mark_node;
23979 if (type == error_mark_node)
23980 return error_mark_node;
23982 return finish_type_requirement (type);
23985 /* Parse a compound requirement
23987 compound-requirement:
23988 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
23989 static tree
23990 cp_parser_compound_requirement (cp_parser *parser)
23992 /* Parse an expression enclosed in '{ }'s. */
23993 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23994 return error_mark_node;
23996 tree expr = cp_parser_expression (parser, NULL, false, false);
23997 if (!expr || expr == error_mark_node)
23998 return error_mark_node;
24000 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24001 return error_mark_node;
24003 /* Parse the optional noexcept. */
24004 bool noexcept_p = false;
24005 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24007 cp_lexer_consume_token (parser->lexer);
24008 noexcept_p = true;
24011 /* Parse the optional trailing return type. */
24012 tree type = NULL_TREE;
24013 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24015 cp_lexer_consume_token (parser->lexer);
24016 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24017 parser->in_result_type_constraint_p = true;
24018 type = cp_parser_trailing_type_id (parser);
24019 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24020 if (type == error_mark_node)
24021 return error_mark_node;
24024 return finish_compound_requirement (expr, type, noexcept_p);
24027 /* Parse a nested requirement. This is the same as a requires clause.
24029 nested-requirement:
24030 requires-clause */
24031 static tree
24032 cp_parser_nested_requirement (cp_parser *parser)
24034 cp_lexer_consume_token (parser->lexer);
24035 tree req = cp_parser_requires_clause (parser);
24036 if (req == error_mark_node)
24037 return error_mark_node;
24038 return finish_nested_requirement (req);
24041 /* Support Functions */
24043 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24044 NAME should have one of the representations used for an
24045 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24046 is returned. If PARSER->SCOPE is a dependent type, then a
24047 SCOPE_REF is returned.
24049 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24050 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24051 was formed. Abstractly, such entities should not be passed to this
24052 function, because they do not need to be looked up, but it is
24053 simpler to check for this special case here, rather than at the
24054 call-sites.
24056 In cases not explicitly covered above, this function returns a
24057 DECL, OVERLOAD, or baselink representing the result of the lookup.
24058 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24059 is returned.
24061 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24062 (e.g., "struct") that was used. In that case bindings that do not
24063 refer to types are ignored.
24065 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24066 ignored.
24068 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24069 are ignored.
24071 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24072 types.
24074 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24075 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24076 NULL_TREE otherwise. */
24078 static tree
24079 cp_parser_lookup_name (cp_parser *parser, tree name,
24080 enum tag_types tag_type,
24081 bool is_template,
24082 bool is_namespace,
24083 bool check_dependency,
24084 tree *ambiguous_decls,
24085 location_t name_location)
24087 tree decl;
24088 tree object_type = parser->context->object_type;
24090 /* Assume that the lookup will be unambiguous. */
24091 if (ambiguous_decls)
24092 *ambiguous_decls = NULL_TREE;
24094 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24095 no longer valid. Note that if we are parsing tentatively, and
24096 the parse fails, OBJECT_TYPE will be automatically restored. */
24097 parser->context->object_type = NULL_TREE;
24099 if (name == error_mark_node)
24100 return error_mark_node;
24102 /* A template-id has already been resolved; there is no lookup to
24103 do. */
24104 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24105 return name;
24106 if (BASELINK_P (name))
24108 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24109 == TEMPLATE_ID_EXPR);
24110 return name;
24113 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24114 it should already have been checked to make sure that the name
24115 used matches the type being destroyed. */
24116 if (TREE_CODE (name) == BIT_NOT_EXPR)
24118 tree type;
24120 /* Figure out to which type this destructor applies. */
24121 if (parser->scope)
24122 type = parser->scope;
24123 else if (object_type)
24124 type = object_type;
24125 else
24126 type = current_class_type;
24127 /* If that's not a class type, there is no destructor. */
24128 if (!type || !CLASS_TYPE_P (type))
24129 return error_mark_node;
24130 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24131 lazily_declare_fn (sfk_destructor, type);
24132 if (!CLASSTYPE_DESTRUCTORS (type))
24133 return error_mark_node;
24134 /* If it was a class type, return the destructor. */
24135 return CLASSTYPE_DESTRUCTORS (type);
24138 /* By this point, the NAME should be an ordinary identifier. If
24139 the id-expression was a qualified name, the qualifying scope is
24140 stored in PARSER->SCOPE at this point. */
24141 gcc_assert (identifier_p (name));
24143 /* Perform the lookup. */
24144 if (parser->scope)
24146 bool dependent_p;
24148 if (parser->scope == error_mark_node)
24149 return error_mark_node;
24151 /* If the SCOPE is dependent, the lookup must be deferred until
24152 the template is instantiated -- unless we are explicitly
24153 looking up names in uninstantiated templates. Even then, we
24154 cannot look up the name if the scope is not a class type; it
24155 might, for example, be a template type parameter. */
24156 dependent_p = (TYPE_P (parser->scope)
24157 && dependent_scope_p (parser->scope));
24158 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24159 && dependent_p)
24160 /* Defer lookup. */
24161 decl = error_mark_node;
24162 else
24164 tree pushed_scope = NULL_TREE;
24166 /* If PARSER->SCOPE is a dependent type, then it must be a
24167 class type, and we must not be checking dependencies;
24168 otherwise, we would have processed this lookup above. So
24169 that PARSER->SCOPE is not considered a dependent base by
24170 lookup_member, we must enter the scope here. */
24171 if (dependent_p)
24172 pushed_scope = push_scope (parser->scope);
24174 /* If the PARSER->SCOPE is a template specialization, it
24175 may be instantiated during name lookup. In that case,
24176 errors may be issued. Even if we rollback the current
24177 tentative parse, those errors are valid. */
24178 decl = lookup_qualified_name (parser->scope, name,
24179 tag_type != none_type,
24180 /*complain=*/true);
24182 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24183 lookup result and the nested-name-specifier nominates a class C:
24184 * if the name specified after the nested-name-specifier, when
24185 looked up in C, is the injected-class-name of C (Clause 9), or
24186 * if the name specified after the nested-name-specifier is the
24187 same as the identifier or the simple-template-id's template-
24188 name in the last component of the nested-name-specifier,
24189 the name is instead considered to name the constructor of
24190 class C. [ Note: for example, the constructor is not an
24191 acceptable lookup result in an elaborated-type-specifier so
24192 the constructor would not be used in place of the
24193 injected-class-name. --end note ] Such a constructor name
24194 shall be used only in the declarator-id of a declaration that
24195 names a constructor or in a using-declaration. */
24196 if (tag_type == none_type
24197 && DECL_SELF_REFERENCE_P (decl)
24198 && same_type_p (DECL_CONTEXT (decl), parser->scope))
24199 decl = lookup_qualified_name (parser->scope, ctor_identifier,
24200 tag_type != none_type,
24201 /*complain=*/true);
24203 /* If we have a single function from a using decl, pull it out. */
24204 if (TREE_CODE (decl) == OVERLOAD
24205 && !really_overloaded_fn (decl))
24206 decl = OVL_FUNCTION (decl);
24208 if (pushed_scope)
24209 pop_scope (pushed_scope);
24212 /* If the scope is a dependent type and either we deferred lookup or
24213 we did lookup but didn't find the name, rememeber the name. */
24214 if (decl == error_mark_node && TYPE_P (parser->scope)
24215 && dependent_type_p (parser->scope))
24217 if (tag_type)
24219 tree type;
24221 /* The resolution to Core Issue 180 says that `struct
24222 A::B' should be considered a type-name, even if `A'
24223 is dependent. */
24224 type = make_typename_type (parser->scope, name, tag_type,
24225 /*complain=*/tf_error);
24226 if (type != error_mark_node)
24227 decl = TYPE_NAME (type);
24229 else if (is_template
24230 && (cp_parser_next_token_ends_template_argument_p (parser)
24231 || cp_lexer_next_token_is (parser->lexer,
24232 CPP_CLOSE_PAREN)))
24233 decl = make_unbound_class_template (parser->scope,
24234 name, NULL_TREE,
24235 /*complain=*/tf_error);
24236 else
24237 decl = build_qualified_name (/*type=*/NULL_TREE,
24238 parser->scope, name,
24239 is_template);
24241 parser->qualifying_scope = parser->scope;
24242 parser->object_scope = NULL_TREE;
24244 else if (object_type)
24246 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24247 OBJECT_TYPE is not a class. */
24248 if (CLASS_TYPE_P (object_type))
24249 /* If the OBJECT_TYPE is a template specialization, it may
24250 be instantiated during name lookup. In that case, errors
24251 may be issued. Even if we rollback the current tentative
24252 parse, those errors are valid. */
24253 decl = lookup_member (object_type,
24254 name,
24255 /*protect=*/0,
24256 tag_type != none_type,
24257 tf_warning_or_error);
24258 else
24259 decl = NULL_TREE;
24261 if (!decl)
24262 /* Look it up in the enclosing context. */
24263 decl = lookup_name_real (name, tag_type != none_type,
24264 /*nonclass=*/0,
24265 /*block_p=*/true, is_namespace, 0);
24266 parser->object_scope = object_type;
24267 parser->qualifying_scope = NULL_TREE;
24269 else
24271 decl = lookup_name_real (name, tag_type != none_type,
24272 /*nonclass=*/0,
24273 /*block_p=*/true, is_namespace, 0);
24274 parser->qualifying_scope = NULL_TREE;
24275 parser->object_scope = NULL_TREE;
24278 /* If the lookup failed, let our caller know. */
24279 if (!decl || decl == error_mark_node)
24280 return error_mark_node;
24282 /* Pull out the template from an injected-class-name (or multiple). */
24283 if (is_template)
24284 decl = maybe_get_template_decl_from_type_decl (decl);
24286 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24287 if (TREE_CODE (decl) == TREE_LIST)
24289 if (ambiguous_decls)
24290 *ambiguous_decls = decl;
24291 /* The error message we have to print is too complicated for
24292 cp_parser_error, so we incorporate its actions directly. */
24293 if (!cp_parser_simulate_error (parser))
24295 error_at (name_location, "reference to %qD is ambiguous",
24296 name);
24297 print_candidates (decl);
24299 return error_mark_node;
24302 gcc_assert (DECL_P (decl)
24303 || TREE_CODE (decl) == OVERLOAD
24304 || TREE_CODE (decl) == SCOPE_REF
24305 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24306 || BASELINK_P (decl));
24308 /* If we have resolved the name of a member declaration, check to
24309 see if the declaration is accessible. When the name resolves to
24310 set of overloaded functions, accessibility is checked when
24311 overload resolution is done.
24313 During an explicit instantiation, access is not checked at all,
24314 as per [temp.explicit]. */
24315 if (DECL_P (decl))
24316 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24318 maybe_record_typedef_use (decl);
24320 return decl;
24323 /* Like cp_parser_lookup_name, but for use in the typical case where
24324 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24325 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24327 static tree
24328 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24330 return cp_parser_lookup_name (parser, name,
24331 none_type,
24332 /*is_template=*/false,
24333 /*is_namespace=*/false,
24334 /*check_dependency=*/true,
24335 /*ambiguous_decls=*/NULL,
24336 location);
24339 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24340 the current context, return the TYPE_DECL. If TAG_NAME_P is
24341 true, the DECL indicates the class being defined in a class-head,
24342 or declared in an elaborated-type-specifier.
24344 Otherwise, return DECL. */
24346 static tree
24347 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24349 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24350 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24352 struct A {
24353 template <typename T> struct B;
24356 template <typename T> struct A::B {};
24358 Similarly, in an elaborated-type-specifier:
24360 namespace N { struct X{}; }
24362 struct A {
24363 template <typename T> friend struct N::X;
24366 However, if the DECL refers to a class type, and we are in
24367 the scope of the class, then the name lookup automatically
24368 finds the TYPE_DECL created by build_self_reference rather
24369 than a TEMPLATE_DECL. For example, in:
24371 template <class T> struct S {
24372 S s;
24375 there is no need to handle such case. */
24377 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24378 return DECL_TEMPLATE_RESULT (decl);
24380 return decl;
24383 /* If too many, or too few, template-parameter lists apply to the
24384 declarator, issue an error message. Returns TRUE if all went well,
24385 and FALSE otherwise. */
24387 static bool
24388 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24389 cp_declarator *declarator,
24390 location_t declarator_location)
24392 switch (declarator->kind)
24394 case cdk_id:
24396 unsigned num_templates = 0;
24397 tree scope = declarator->u.id.qualifying_scope;
24399 if (scope)
24400 num_templates = num_template_headers_for_class (scope);
24401 else if (TREE_CODE (declarator->u.id.unqualified_name)
24402 == TEMPLATE_ID_EXPR)
24403 /* If the DECLARATOR has the form `X<y>' then it uses one
24404 additional level of template parameters. */
24405 ++num_templates;
24407 return cp_parser_check_template_parameters
24408 (parser, num_templates, declarator_location, declarator);
24411 case cdk_function:
24412 case cdk_array:
24413 case cdk_pointer:
24414 case cdk_reference:
24415 case cdk_ptrmem:
24416 return (cp_parser_check_declarator_template_parameters
24417 (parser, declarator->declarator, declarator_location));
24419 case cdk_error:
24420 return true;
24422 default:
24423 gcc_unreachable ();
24425 return false;
24428 /* NUM_TEMPLATES were used in the current declaration. If that is
24429 invalid, return FALSE and issue an error messages. Otherwise,
24430 return TRUE. If DECLARATOR is non-NULL, then we are checking a
24431 declarator and we can print more accurate diagnostics. */
24433 static bool
24434 cp_parser_check_template_parameters (cp_parser* parser,
24435 unsigned num_templates,
24436 location_t location,
24437 cp_declarator *declarator)
24439 /* If there are the same number of template classes and parameter
24440 lists, that's OK. */
24441 if (parser->num_template_parameter_lists == num_templates)
24442 return true;
24443 /* If there are more, but only one more, then we are referring to a
24444 member template. That's OK too. */
24445 if (parser->num_template_parameter_lists == num_templates + 1)
24446 return true;
24447 /* If there are more template classes than parameter lists, we have
24448 something like:
24450 template <class T> void S<T>::R<T>::f (); */
24451 if (parser->num_template_parameter_lists < num_templates)
24453 if (declarator && !current_function_decl)
24454 error_at (location, "specializing member %<%T::%E%> "
24455 "requires %<template<>%> syntax",
24456 declarator->u.id.qualifying_scope,
24457 declarator->u.id.unqualified_name);
24458 else if (declarator)
24459 error_at (location, "invalid declaration of %<%T::%E%>",
24460 declarator->u.id.qualifying_scope,
24461 declarator->u.id.unqualified_name);
24462 else
24463 error_at (location, "too few template-parameter-lists");
24464 return false;
24466 /* Otherwise, there are too many template parameter lists. We have
24467 something like:
24469 template <class T> template <class U> void S::f(); */
24470 error_at (location, "too many template-parameter-lists");
24471 return false;
24474 /* Parse an optional `::' token indicating that the following name is
24475 from the global namespace. If so, PARSER->SCOPE is set to the
24476 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
24477 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
24478 Returns the new value of PARSER->SCOPE, if the `::' token is
24479 present, and NULL_TREE otherwise. */
24481 static tree
24482 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
24484 cp_token *token;
24486 /* Peek at the next token. */
24487 token = cp_lexer_peek_token (parser->lexer);
24488 /* If we're looking at a `::' token then we're starting from the
24489 global namespace, not our current location. */
24490 if (token->type == CPP_SCOPE)
24492 /* Consume the `::' token. */
24493 cp_lexer_consume_token (parser->lexer);
24494 /* Set the SCOPE so that we know where to start the lookup. */
24495 parser->scope = global_namespace;
24496 parser->qualifying_scope = global_namespace;
24497 parser->object_scope = NULL_TREE;
24499 return parser->scope;
24501 else if (!current_scope_valid_p)
24503 parser->scope = NULL_TREE;
24504 parser->qualifying_scope = NULL_TREE;
24505 parser->object_scope = NULL_TREE;
24508 return NULL_TREE;
24511 /* Returns TRUE if the upcoming token sequence is the start of a
24512 constructor declarator. If FRIEND_P is true, the declarator is
24513 preceded by the `friend' specifier. */
24515 static bool
24516 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
24518 bool constructor_p;
24519 bool outside_class_specifier_p;
24520 tree nested_name_specifier;
24521 cp_token *next_token;
24523 /* The common case is that this is not a constructor declarator, so
24524 try to avoid doing lots of work if at all possible. It's not
24525 valid declare a constructor at function scope. */
24526 if (parser->in_function_body)
24527 return false;
24528 /* And only certain tokens can begin a constructor declarator. */
24529 next_token = cp_lexer_peek_token (parser->lexer);
24530 if (next_token->type != CPP_NAME
24531 && next_token->type != CPP_SCOPE
24532 && next_token->type != CPP_NESTED_NAME_SPECIFIER
24533 && next_token->type != CPP_TEMPLATE_ID)
24534 return false;
24536 /* Parse tentatively; we are going to roll back all of the tokens
24537 consumed here. */
24538 cp_parser_parse_tentatively (parser);
24539 /* Assume that we are looking at a constructor declarator. */
24540 constructor_p = true;
24542 /* Look for the optional `::' operator. */
24543 cp_parser_global_scope_opt (parser,
24544 /*current_scope_valid_p=*/false);
24545 /* Look for the nested-name-specifier. */
24546 nested_name_specifier
24547 = (cp_parser_nested_name_specifier_opt (parser,
24548 /*typename_keyword_p=*/false,
24549 /*check_dependency_p=*/false,
24550 /*type_p=*/false,
24551 /*is_declaration=*/false));
24553 outside_class_specifier_p = (!at_class_scope_p ()
24554 || !TYPE_BEING_DEFINED (current_class_type)
24555 || friend_p);
24557 /* Outside of a class-specifier, there must be a
24558 nested-name-specifier. */
24559 if (!nested_name_specifier && outside_class_specifier_p)
24560 constructor_p = false;
24561 else if (nested_name_specifier == error_mark_node)
24562 constructor_p = false;
24564 /* If we have a class scope, this is easy; DR 147 says that S::S always
24565 names the constructor, and no other qualified name could. */
24566 if (constructor_p && nested_name_specifier
24567 && CLASS_TYPE_P (nested_name_specifier))
24569 tree id = cp_parser_unqualified_id (parser,
24570 /*template_keyword_p=*/false,
24571 /*check_dependency_p=*/false,
24572 /*declarator_p=*/true,
24573 /*optional_p=*/false);
24574 if (is_overloaded_fn (id))
24575 id = DECL_NAME (get_first_fn (id));
24576 if (!constructor_name_p (id, nested_name_specifier))
24577 constructor_p = false;
24579 /* If we still think that this might be a constructor-declarator,
24580 look for a class-name. */
24581 else if (constructor_p)
24583 /* If we have:
24585 template <typename T> struct S {
24586 S();
24589 we must recognize that the nested `S' names a class. */
24590 tree type_decl;
24591 type_decl = cp_parser_class_name (parser,
24592 /*typename_keyword_p=*/false,
24593 /*template_keyword_p=*/false,
24594 none_type,
24595 /*check_dependency_p=*/false,
24596 /*class_head_p=*/false,
24597 /*is_declaration=*/false);
24598 /* If there was no class-name, then this is not a constructor.
24599 Otherwise, if we are in a class-specifier and we aren't
24600 handling a friend declaration, check that its type matches
24601 current_class_type (c++/38313). Note: error_mark_node
24602 is left alone for error recovery purposes. */
24603 constructor_p = (!cp_parser_error_occurred (parser)
24604 && (outside_class_specifier_p
24605 || type_decl == error_mark_node
24606 || same_type_p (current_class_type,
24607 TREE_TYPE (type_decl))));
24609 /* If we're still considering a constructor, we have to see a `(',
24610 to begin the parameter-declaration-clause, followed by either a
24611 `)', an `...', or a decl-specifier. We need to check for a
24612 type-specifier to avoid being fooled into thinking that:
24614 S (f) (int);
24616 is a constructor. (It is actually a function named `f' that
24617 takes one parameter (of type `int') and returns a value of type
24618 `S'. */
24619 if (constructor_p
24620 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24621 constructor_p = false;
24623 if (constructor_p
24624 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
24625 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
24626 /* A parameter declaration begins with a decl-specifier,
24627 which is either the "attribute" keyword, a storage class
24628 specifier, or (usually) a type-specifier. */
24629 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
24631 tree type;
24632 tree pushed_scope = NULL_TREE;
24633 unsigned saved_num_template_parameter_lists;
24635 /* Names appearing in the type-specifier should be looked up
24636 in the scope of the class. */
24637 if (current_class_type)
24638 type = NULL_TREE;
24639 else
24641 type = TREE_TYPE (type_decl);
24642 if (TREE_CODE (type) == TYPENAME_TYPE)
24644 type = resolve_typename_type (type,
24645 /*only_current_p=*/false);
24646 if (TREE_CODE (type) == TYPENAME_TYPE)
24648 cp_parser_abort_tentative_parse (parser);
24649 return false;
24652 pushed_scope = push_scope (type);
24655 /* Inside the constructor parameter list, surrounding
24656 template-parameter-lists do not apply. */
24657 saved_num_template_parameter_lists
24658 = parser->num_template_parameter_lists;
24659 parser->num_template_parameter_lists = 0;
24661 /* Look for the type-specifier. */
24662 cp_parser_type_specifier (parser,
24663 CP_PARSER_FLAGS_NONE,
24664 /*decl_specs=*/NULL,
24665 /*is_declarator=*/true,
24666 /*declares_class_or_enum=*/NULL,
24667 /*is_cv_qualifier=*/NULL);
24669 parser->num_template_parameter_lists
24670 = saved_num_template_parameter_lists;
24672 /* Leave the scope of the class. */
24673 if (pushed_scope)
24674 pop_scope (pushed_scope);
24676 constructor_p = !cp_parser_error_occurred (parser);
24680 /* We did not really want to consume any tokens. */
24681 cp_parser_abort_tentative_parse (parser);
24683 return constructor_p;
24686 /* Parse the definition of the function given by the DECL_SPECIFIERS,
24687 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
24688 they must be performed once we are in the scope of the function.
24690 Returns the function defined. */
24692 static tree
24693 cp_parser_function_definition_from_specifiers_and_declarator
24694 (cp_parser* parser,
24695 cp_decl_specifier_seq *decl_specifiers,
24696 tree attributes,
24697 const cp_declarator *declarator)
24699 tree fn;
24700 bool success_p;
24702 /* Begin the function-definition. */
24703 success_p = start_function (decl_specifiers, declarator, attributes);
24705 /* The things we're about to see are not directly qualified by any
24706 template headers we've seen thus far. */
24707 reset_specialization ();
24709 /* If there were names looked up in the decl-specifier-seq that we
24710 did not check, check them now. We must wait until we are in the
24711 scope of the function to perform the checks, since the function
24712 might be a friend. */
24713 perform_deferred_access_checks (tf_warning_or_error);
24715 if (success_p)
24717 cp_finalize_omp_declare_simd (parser, current_function_decl);
24718 parser->omp_declare_simd = NULL;
24719 cp_finalize_oacc_routine (parser, current_function_decl, true);
24720 parser->oacc_routine = NULL;
24723 if (!success_p)
24725 /* Skip the entire function. */
24726 cp_parser_skip_to_end_of_block_or_statement (parser);
24727 fn = error_mark_node;
24729 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
24731 /* Seen already, skip it. An error message has already been output. */
24732 cp_parser_skip_to_end_of_block_or_statement (parser);
24733 fn = current_function_decl;
24734 current_function_decl = NULL_TREE;
24735 /* If this is a function from a class, pop the nested class. */
24736 if (current_class_name)
24737 pop_nested_class ();
24739 else
24741 timevar_id_t tv;
24742 if (DECL_DECLARED_INLINE_P (current_function_decl))
24743 tv = TV_PARSE_INLINE;
24744 else
24745 tv = TV_PARSE_FUNC;
24746 timevar_push (tv);
24747 fn = cp_parser_function_definition_after_declarator (parser,
24748 /*inline_p=*/false);
24749 timevar_pop (tv);
24752 return fn;
24755 /* Parse the part of a function-definition that follows the
24756 declarator. INLINE_P is TRUE iff this function is an inline
24757 function defined within a class-specifier.
24759 Returns the function defined. */
24761 static tree
24762 cp_parser_function_definition_after_declarator (cp_parser* parser,
24763 bool inline_p)
24765 tree fn;
24766 bool ctor_initializer_p = false;
24767 bool saved_in_unbraced_linkage_specification_p;
24768 bool saved_in_function_body;
24769 unsigned saved_num_template_parameter_lists;
24770 cp_token *token;
24771 bool fully_implicit_function_template_p
24772 = parser->fully_implicit_function_template_p;
24773 parser->fully_implicit_function_template_p = false;
24774 tree implicit_template_parms
24775 = parser->implicit_template_parms;
24776 parser->implicit_template_parms = 0;
24777 cp_binding_level* implicit_template_scope
24778 = parser->implicit_template_scope;
24779 parser->implicit_template_scope = 0;
24781 saved_in_function_body = parser->in_function_body;
24782 parser->in_function_body = true;
24783 /* If the next token is `return', then the code may be trying to
24784 make use of the "named return value" extension that G++ used to
24785 support. */
24786 token = cp_lexer_peek_token (parser->lexer);
24787 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
24789 /* Consume the `return' keyword. */
24790 cp_lexer_consume_token (parser->lexer);
24791 /* Look for the identifier that indicates what value is to be
24792 returned. */
24793 cp_parser_identifier (parser);
24794 /* Issue an error message. */
24795 error_at (token->location,
24796 "named return values are no longer supported");
24797 /* Skip tokens until we reach the start of the function body. */
24798 while (true)
24800 cp_token *token = cp_lexer_peek_token (parser->lexer);
24801 if (token->type == CPP_OPEN_BRACE
24802 || token->type == CPP_EOF
24803 || token->type == CPP_PRAGMA_EOL)
24804 break;
24805 cp_lexer_consume_token (parser->lexer);
24808 /* The `extern' in `extern "C" void f () { ... }' does not apply to
24809 anything declared inside `f'. */
24810 saved_in_unbraced_linkage_specification_p
24811 = parser->in_unbraced_linkage_specification_p;
24812 parser->in_unbraced_linkage_specification_p = false;
24813 /* Inside the function, surrounding template-parameter-lists do not
24814 apply. */
24815 saved_num_template_parameter_lists
24816 = parser->num_template_parameter_lists;
24817 parser->num_template_parameter_lists = 0;
24819 start_lambda_scope (current_function_decl);
24821 /* If the next token is `try', `__transaction_atomic', or
24822 `__transaction_relaxed`, then we are looking at either function-try-block
24823 or function-transaction-block. Note that all of these include the
24824 function-body. */
24825 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
24826 ctor_initializer_p = cp_parser_function_transaction (parser,
24827 RID_TRANSACTION_ATOMIC);
24828 else if (cp_lexer_next_token_is_keyword (parser->lexer,
24829 RID_TRANSACTION_RELAXED))
24830 ctor_initializer_p = cp_parser_function_transaction (parser,
24831 RID_TRANSACTION_RELAXED);
24832 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
24833 ctor_initializer_p = cp_parser_function_try_block (parser);
24834 else
24835 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24836 (parser, /*in_function_try_block=*/false);
24838 finish_lambda_scope ();
24840 /* Finish the function. */
24841 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
24842 (inline_p ? 2 : 0));
24843 /* Generate code for it, if necessary. */
24844 expand_or_defer_fn (fn);
24845 /* Restore the saved values. */
24846 parser->in_unbraced_linkage_specification_p
24847 = saved_in_unbraced_linkage_specification_p;
24848 parser->num_template_parameter_lists
24849 = saved_num_template_parameter_lists;
24850 parser->in_function_body = saved_in_function_body;
24852 parser->fully_implicit_function_template_p
24853 = fully_implicit_function_template_p;
24854 parser->implicit_template_parms
24855 = implicit_template_parms;
24856 parser->implicit_template_scope
24857 = implicit_template_scope;
24859 if (parser->fully_implicit_function_template_p)
24860 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
24862 return fn;
24865 /* Parse a template-declaration body (following argument list). */
24867 static void
24868 cp_parser_template_declaration_after_parameters (cp_parser* parser,
24869 tree parameter_list,
24870 bool member_p)
24872 tree decl = NULL_TREE;
24873 bool friend_p = false;
24875 /* We just processed one more parameter list. */
24876 ++parser->num_template_parameter_lists;
24878 /* Get the deferred access checks from the parameter list. These
24879 will be checked once we know what is being declared, as for a
24880 member template the checks must be performed in the scope of the
24881 class containing the member. */
24882 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
24884 /* Tentatively parse for a new template parameter list, which can either be
24885 the template keyword or a template introduction. */
24886 if (cp_parser_template_declaration_after_export (parser, member_p))
24887 /* OK */;
24888 else if (cxx_dialect >= cxx11
24889 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24890 decl = cp_parser_alias_declaration (parser);
24891 else
24893 /* There are no access checks when parsing a template, as we do not
24894 know if a specialization will be a friend. */
24895 push_deferring_access_checks (dk_no_check);
24896 cp_token *token = cp_lexer_peek_token (parser->lexer);
24897 decl = cp_parser_single_declaration (parser,
24898 checks,
24899 member_p,
24900 /*explicit_specialization_p=*/false,
24901 &friend_p);
24902 pop_deferring_access_checks ();
24904 /* If this is a member template declaration, let the front
24905 end know. */
24906 if (member_p && !friend_p && decl)
24908 if (TREE_CODE (decl) == TYPE_DECL)
24909 cp_parser_check_access_in_redeclaration (decl, token->location);
24911 decl = finish_member_template_decl (decl);
24913 else if (friend_p && decl
24914 && DECL_DECLARES_TYPE_P (decl))
24915 make_friend_class (current_class_type, TREE_TYPE (decl),
24916 /*complain=*/true);
24918 /* We are done with the current parameter list. */
24919 --parser->num_template_parameter_lists;
24921 pop_deferring_access_checks ();
24923 /* Finish up. */
24924 finish_template_decl (parameter_list);
24926 /* Check the template arguments for a literal operator template. */
24927 if (decl
24928 && DECL_DECLARES_FUNCTION_P (decl)
24929 && UDLIT_OPER_P (DECL_NAME (decl)))
24931 bool ok = true;
24932 if (parameter_list == NULL_TREE)
24933 ok = false;
24934 else
24936 int num_parms = TREE_VEC_LENGTH (parameter_list);
24937 if (num_parms == 1)
24939 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
24940 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
24941 if (TREE_TYPE (parm) != char_type_node
24942 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
24943 ok = false;
24945 else if (num_parms == 2 && cxx_dialect >= cxx14)
24947 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
24948 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
24949 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
24950 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
24951 if (TREE_TYPE (parm) != TREE_TYPE (type)
24952 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
24953 ok = false;
24955 else
24956 ok = false;
24958 if (!ok)
24960 if (cxx_dialect >= cxx14)
24961 error ("literal operator template %qD has invalid parameter list."
24962 " Expected non-type template argument pack <char...>"
24963 " or <typename CharT, CharT...>",
24964 decl);
24965 else
24966 error ("literal operator template %qD has invalid parameter list."
24967 " Expected non-type template argument pack <char...>",
24968 decl);
24972 /* Register member declarations. */
24973 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
24974 finish_member_declaration (decl);
24975 /* If DECL is a function template, we must return to parse it later.
24976 (Even though there is no definition, there might be default
24977 arguments that need handling.) */
24978 if (member_p && decl
24979 && DECL_DECLARES_FUNCTION_P (decl))
24980 vec_safe_push (unparsed_funs_with_definitions, decl);
24983 /* Parse a template introduction header for a template-declaration. Returns
24984 false if tentative parse fails. */
24986 static bool
24987 cp_parser_template_introduction (cp_parser* parser, bool member_p)
24989 cp_parser_parse_tentatively (parser);
24991 tree saved_scope = parser->scope;
24992 tree saved_object_scope = parser->object_scope;
24993 tree saved_qualifying_scope = parser->qualifying_scope;
24995 /* Look for the optional `::' operator. */
24996 cp_parser_global_scope_opt (parser,
24997 /*current_scope_valid_p=*/false);
24998 /* Look for the nested-name-specifier. */
24999 cp_parser_nested_name_specifier_opt (parser,
25000 /*typename_keyword_p=*/false,
25001 /*check_dependency_p=*/true,
25002 /*type_p=*/false,
25003 /*is_declaration=*/false);
25005 cp_token *token = cp_lexer_peek_token (parser->lexer);
25006 tree concept_name = cp_parser_identifier (parser);
25008 /* Look up the concept for which we will be matching
25009 template parameters. */
25010 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25011 token->location);
25012 parser->scope = saved_scope;
25013 parser->object_scope = saved_object_scope;
25014 parser->qualifying_scope = saved_qualifying_scope;
25016 if (concept_name == error_mark_node)
25017 cp_parser_simulate_error (parser);
25019 /* Look for opening brace for introduction. */
25020 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25022 if (!cp_parser_parse_definitely (parser))
25023 return false;
25025 push_deferring_access_checks (dk_deferred);
25027 /* Build vector of placeholder parameters and grab
25028 matching identifiers. */
25029 tree introduction_list = cp_parser_introduction_list (parser);
25031 /* The introduction-list shall not be empty. */
25032 int nargs = TREE_VEC_LENGTH (introduction_list);
25033 if (nargs == 0)
25035 error ("empty introduction-list");
25036 return true;
25039 /* Look for closing brace for introduction. */
25040 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25041 return true;
25043 if (tmpl_decl == error_mark_node)
25045 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25046 token->location);
25047 return true;
25050 /* Build and associate the constraint. */
25051 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25052 if (parms && parms != error_mark_node)
25054 cp_parser_template_declaration_after_parameters (parser, parms,
25055 member_p);
25056 return true;
25059 error_at (token->location, "no matching concept for template-introduction");
25060 return true;
25063 /* Parse a normal template-declaration following the template keyword. */
25065 static void
25066 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25068 tree parameter_list;
25069 bool need_lang_pop;
25070 location_t location = input_location;
25072 /* Look for the `<' token. */
25073 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25074 return;
25075 if (at_class_scope_p () && current_function_decl)
25077 /* 14.5.2.2 [temp.mem]
25079 A local class shall not have member templates. */
25080 error_at (location,
25081 "invalid declaration of member template in local class");
25082 cp_parser_skip_to_end_of_block_or_statement (parser);
25083 return;
25085 /* [temp]
25087 A template ... shall not have C linkage. */
25088 if (current_lang_name == lang_name_c)
25090 error_at (location, "template with C linkage");
25091 /* Give it C++ linkage to avoid confusing other parts of the
25092 front end. */
25093 push_lang_context (lang_name_cplusplus);
25094 need_lang_pop = true;
25096 else
25097 need_lang_pop = false;
25099 /* We cannot perform access checks on the template parameter
25100 declarations until we know what is being declared, just as we
25101 cannot check the decl-specifier list. */
25102 push_deferring_access_checks (dk_deferred);
25104 /* If the next token is `>', then we have an invalid
25105 specialization. Rather than complain about an invalid template
25106 parameter, issue an error message here. */
25107 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25109 cp_parser_error (parser, "invalid explicit specialization");
25110 begin_specialization ();
25111 parameter_list = NULL_TREE;
25113 else
25115 /* Parse the template parameters. */
25116 parameter_list = cp_parser_template_parameter_list (parser);
25119 /* Look for the `>'. */
25120 cp_parser_skip_to_end_of_template_parameter_list (parser);
25122 /* Manage template requirements */
25123 if (flag_concepts)
25125 tree reqs = get_shorthand_constraints (current_template_parms);
25126 if (tree r = cp_parser_requires_clause_opt (parser))
25127 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
25128 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25131 cp_parser_template_declaration_after_parameters (parser, parameter_list,
25132 member_p);
25134 /* For the erroneous case of a template with C linkage, we pushed an
25135 implicit C++ linkage scope; exit that scope now. */
25136 if (need_lang_pop)
25137 pop_lang_context ();
25140 /* Parse a template-declaration, assuming that the `export' (and
25141 `extern') keywords, if present, has already been scanned. MEMBER_P
25142 is as for cp_parser_template_declaration. */
25144 static bool
25145 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25147 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25149 cp_lexer_consume_token (parser->lexer);
25150 cp_parser_explicit_template_declaration (parser, member_p);
25151 return true;
25153 else if (flag_concepts)
25154 return cp_parser_template_introduction (parser, member_p);
25156 return false;
25159 /* Perform the deferred access checks from a template-parameter-list.
25160 CHECKS is a TREE_LIST of access checks, as returned by
25161 get_deferred_access_checks. */
25163 static void
25164 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25166 ++processing_template_parmlist;
25167 perform_access_checks (checks, tf_warning_or_error);
25168 --processing_template_parmlist;
25171 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25172 `function-definition' sequence that follows a template header.
25173 If MEMBER_P is true, this declaration appears in a class scope.
25175 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25176 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25178 static tree
25179 cp_parser_single_declaration (cp_parser* parser,
25180 vec<deferred_access_check, va_gc> *checks,
25181 bool member_p,
25182 bool explicit_specialization_p,
25183 bool* friend_p)
25185 int declares_class_or_enum;
25186 tree decl = NULL_TREE;
25187 cp_decl_specifier_seq decl_specifiers;
25188 bool function_definition_p = false;
25189 cp_token *decl_spec_token_start;
25191 /* This function is only used when processing a template
25192 declaration. */
25193 gcc_assert (innermost_scope_kind () == sk_template_parms
25194 || innermost_scope_kind () == sk_template_spec);
25196 /* Defer access checks until we know what is being declared. */
25197 push_deferring_access_checks (dk_deferred);
25199 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25200 alternative. */
25201 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25202 cp_parser_decl_specifier_seq (parser,
25203 CP_PARSER_FLAGS_OPTIONAL,
25204 &decl_specifiers,
25205 &declares_class_or_enum);
25206 if (friend_p)
25207 *friend_p = cp_parser_friend_p (&decl_specifiers);
25209 /* There are no template typedefs. */
25210 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25212 error_at (decl_spec_token_start->location,
25213 "template declaration of %<typedef%>");
25214 decl = error_mark_node;
25217 /* Gather up the access checks that occurred the
25218 decl-specifier-seq. */
25219 stop_deferring_access_checks ();
25221 /* Check for the declaration of a template class. */
25222 if (declares_class_or_enum)
25224 if (cp_parser_declares_only_class_p (parser))
25226 // If this is a declaration, but not a definition, associate
25227 // any constraints with the type declaration. Constraints
25228 // are associated with definitions in cp_parser_class_specifier.
25229 if (declares_class_or_enum == 1)
25230 associate_classtype_constraints (decl_specifiers.type);
25232 decl = shadow_tag (&decl_specifiers);
25234 /* In this case:
25236 struct C {
25237 friend template <typename T> struct A<T>::B;
25240 A<T>::B will be represented by a TYPENAME_TYPE, and
25241 therefore not recognized by shadow_tag. */
25242 if (friend_p && *friend_p
25243 && !decl
25244 && decl_specifiers.type
25245 && TYPE_P (decl_specifiers.type))
25246 decl = decl_specifiers.type;
25248 if (decl && decl != error_mark_node)
25249 decl = TYPE_NAME (decl);
25250 else
25251 decl = error_mark_node;
25253 /* Perform access checks for template parameters. */
25254 cp_parser_perform_template_parameter_access_checks (checks);
25258 /* Complain about missing 'typename' or other invalid type names. */
25259 if (!decl_specifiers.any_type_specifiers_p
25260 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25262 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25263 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25264 the rest of this declaration. */
25265 decl = error_mark_node;
25266 goto out;
25269 /* If it's not a template class, try for a template function. If
25270 the next token is a `;', then this declaration does not declare
25271 anything. But, if there were errors in the decl-specifiers, then
25272 the error might well have come from an attempted class-specifier.
25273 In that case, there's no need to warn about a missing declarator. */
25274 if (!decl
25275 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25276 || decl_specifiers.type != error_mark_node))
25278 decl = cp_parser_init_declarator (parser,
25279 &decl_specifiers,
25280 checks,
25281 /*function_definition_allowed_p=*/true,
25282 member_p,
25283 declares_class_or_enum,
25284 &function_definition_p,
25285 NULL, NULL);
25287 /* 7.1.1-1 [dcl.stc]
25289 A storage-class-specifier shall not be specified in an explicit
25290 specialization... */
25291 if (decl
25292 && explicit_specialization_p
25293 && decl_specifiers.storage_class != sc_none)
25295 error_at (decl_spec_token_start->location,
25296 "explicit template specialization cannot have a storage class");
25297 decl = error_mark_node;
25300 if (decl && VAR_P (decl))
25301 check_template_variable (decl);
25304 /* Look for a trailing `;' after the declaration. */
25305 if (!function_definition_p
25306 && (decl == error_mark_node
25307 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25308 cp_parser_skip_to_end_of_block_or_statement (parser);
25310 out:
25311 pop_deferring_access_checks ();
25313 /* Clear any current qualification; whatever comes next is the start
25314 of something new. */
25315 parser->scope = NULL_TREE;
25316 parser->qualifying_scope = NULL_TREE;
25317 parser->object_scope = NULL_TREE;
25319 return decl;
25322 /* Parse a cast-expression that is not the operand of a unary "&". */
25324 static tree
25325 cp_parser_simple_cast_expression (cp_parser *parser)
25327 return cp_parser_cast_expression (parser, /*address_p=*/false,
25328 /*cast_p=*/false, /*decltype*/false, NULL);
25331 /* Parse a functional cast to TYPE. Returns an expression
25332 representing the cast. */
25334 static tree
25335 cp_parser_functional_cast (cp_parser* parser, tree type)
25337 vec<tree, va_gc> *vec;
25338 tree expression_list;
25339 tree cast;
25340 bool nonconst_p;
25342 if (!type)
25343 type = error_mark_node;
25345 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25347 cp_lexer_set_source_position (parser->lexer);
25348 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25349 expression_list = cp_parser_braced_list (parser, &nonconst_p);
25350 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25351 if (TREE_CODE (type) == TYPE_DECL)
25352 type = TREE_TYPE (type);
25353 return finish_compound_literal (type, expression_list,
25354 tf_warning_or_error);
25358 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25359 /*cast_p=*/true,
25360 /*allow_expansion_p=*/true,
25361 /*non_constant_p=*/NULL);
25362 if (vec == NULL)
25363 expression_list = error_mark_node;
25364 else
25366 expression_list = build_tree_list_vec (vec);
25367 release_tree_vector (vec);
25370 cast = build_functional_cast (type, expression_list,
25371 tf_warning_or_error);
25372 /* [expr.const]/1: In an integral constant expression "only type
25373 conversions to integral or enumeration type can be used". */
25374 if (TREE_CODE (type) == TYPE_DECL)
25375 type = TREE_TYPE (type);
25376 if (cast != error_mark_node
25377 && !cast_valid_in_integral_constant_expression_p (type)
25378 && cp_parser_non_integral_constant_expression (parser,
25379 NIC_CONSTRUCTOR))
25380 return error_mark_node;
25381 return cast;
25384 /* Save the tokens that make up the body of a member function defined
25385 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
25386 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
25387 specifiers applied to the declaration. Returns the FUNCTION_DECL
25388 for the member function. */
25390 static tree
25391 cp_parser_save_member_function_body (cp_parser* parser,
25392 cp_decl_specifier_seq *decl_specifiers,
25393 cp_declarator *declarator,
25394 tree attributes)
25396 cp_token *first;
25397 cp_token *last;
25398 tree fn;
25400 /* Create the FUNCTION_DECL. */
25401 fn = grokmethod (decl_specifiers, declarator, attributes);
25402 cp_finalize_omp_declare_simd (parser, fn);
25403 cp_finalize_oacc_routine (parser, fn, true);
25404 /* If something went badly wrong, bail out now. */
25405 if (fn == error_mark_node)
25407 /* If there's a function-body, skip it. */
25408 if (cp_parser_token_starts_function_definition_p
25409 (cp_lexer_peek_token (parser->lexer)))
25410 cp_parser_skip_to_end_of_block_or_statement (parser);
25411 return error_mark_node;
25414 /* Remember it, if there default args to post process. */
25415 cp_parser_save_default_args (parser, fn);
25417 /* Save away the tokens that make up the body of the
25418 function. */
25419 first = parser->lexer->next_token;
25420 /* Handle function try blocks. */
25421 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25422 cp_lexer_consume_token (parser->lexer);
25423 /* We can have braced-init-list mem-initializers before the fn body. */
25424 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
25426 cp_lexer_consume_token (parser->lexer);
25427 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25429 /* cache_group will stop after an un-nested { } pair, too. */
25430 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
25431 break;
25433 /* variadic mem-inits have ... after the ')'. */
25434 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25435 cp_lexer_consume_token (parser->lexer);
25438 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25439 /* Handle function try blocks. */
25440 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
25441 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25442 last = parser->lexer->next_token;
25444 /* Save away the inline definition; we will process it when the
25445 class is complete. */
25446 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
25447 DECL_PENDING_INLINE_P (fn) = 1;
25449 /* We need to know that this was defined in the class, so that
25450 friend templates are handled correctly. */
25451 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
25453 /* Add FN to the queue of functions to be parsed later. */
25454 vec_safe_push (unparsed_funs_with_definitions, fn);
25456 return fn;
25459 /* Save the tokens that make up the in-class initializer for a non-static
25460 data member. Returns a DEFAULT_ARG. */
25462 static tree
25463 cp_parser_save_nsdmi (cp_parser* parser)
25465 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
25468 /* Parse a template-argument-list, as well as the trailing ">" (but
25469 not the opening "<"). See cp_parser_template_argument_list for the
25470 return value. */
25472 static tree
25473 cp_parser_enclosed_template_argument_list (cp_parser* parser)
25475 tree arguments;
25476 tree saved_scope;
25477 tree saved_qualifying_scope;
25478 tree saved_object_scope;
25479 bool saved_greater_than_is_operator_p;
25480 int saved_unevaluated_operand;
25481 int saved_inhibit_evaluation_warnings;
25483 /* [temp.names]
25485 When parsing a template-id, the first non-nested `>' is taken as
25486 the end of the template-argument-list rather than a greater-than
25487 operator. */
25488 saved_greater_than_is_operator_p
25489 = parser->greater_than_is_operator_p;
25490 parser->greater_than_is_operator_p = false;
25491 /* Parsing the argument list may modify SCOPE, so we save it
25492 here. */
25493 saved_scope = parser->scope;
25494 saved_qualifying_scope = parser->qualifying_scope;
25495 saved_object_scope = parser->object_scope;
25496 /* We need to evaluate the template arguments, even though this
25497 template-id may be nested within a "sizeof". */
25498 saved_unevaluated_operand = cp_unevaluated_operand;
25499 cp_unevaluated_operand = 0;
25500 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25501 c_inhibit_evaluation_warnings = 0;
25502 /* Parse the template-argument-list itself. */
25503 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
25504 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
25505 arguments = NULL_TREE;
25506 else
25507 arguments = cp_parser_template_argument_list (parser);
25508 /* Look for the `>' that ends the template-argument-list. If we find
25509 a '>>' instead, it's probably just a typo. */
25510 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
25512 if (cxx_dialect != cxx98)
25514 /* In C++0x, a `>>' in a template argument list or cast
25515 expression is considered to be two separate `>'
25516 tokens. So, change the current token to a `>', but don't
25517 consume it: it will be consumed later when the outer
25518 template argument list (or cast expression) is parsed.
25519 Note that this replacement of `>' for `>>' is necessary
25520 even if we are parsing tentatively: in the tentative
25521 case, after calling
25522 cp_parser_enclosed_template_argument_list we will always
25523 throw away all of the template arguments and the first
25524 closing `>', either because the template argument list
25525 was erroneous or because we are replacing those tokens
25526 with a CPP_TEMPLATE_ID token. The second `>' (which will
25527 not have been thrown away) is needed either to close an
25528 outer template argument list or to complete a new-style
25529 cast. */
25530 cp_token *token = cp_lexer_peek_token (parser->lexer);
25531 token->type = CPP_GREATER;
25533 else if (!saved_greater_than_is_operator_p)
25535 /* If we're in a nested template argument list, the '>>' has
25536 to be a typo for '> >'. We emit the error message, but we
25537 continue parsing and we push a '>' as next token, so that
25538 the argument list will be parsed correctly. Note that the
25539 global source location is still on the token before the
25540 '>>', so we need to say explicitly where we want it. */
25541 cp_token *token = cp_lexer_peek_token (parser->lexer);
25542 error_at (token->location, "%<>>%> should be %<> >%> "
25543 "within a nested template argument list");
25545 token->type = CPP_GREATER;
25547 else
25549 /* If this is not a nested template argument list, the '>>'
25550 is a typo for '>'. Emit an error message and continue.
25551 Same deal about the token location, but here we can get it
25552 right by consuming the '>>' before issuing the diagnostic. */
25553 cp_token *token = cp_lexer_consume_token (parser->lexer);
25554 error_at (token->location,
25555 "spurious %<>>%>, use %<>%> to terminate "
25556 "a template argument list");
25559 else
25560 cp_parser_skip_to_end_of_template_parameter_list (parser);
25561 /* The `>' token might be a greater-than operator again now. */
25562 parser->greater_than_is_operator_p
25563 = saved_greater_than_is_operator_p;
25564 /* Restore the SAVED_SCOPE. */
25565 parser->scope = saved_scope;
25566 parser->qualifying_scope = saved_qualifying_scope;
25567 parser->object_scope = saved_object_scope;
25568 cp_unevaluated_operand = saved_unevaluated_operand;
25569 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25571 return arguments;
25574 /* MEMBER_FUNCTION is a member function, or a friend. If default
25575 arguments, or the body of the function have not yet been parsed,
25576 parse them now. */
25578 static void
25579 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
25581 timevar_push (TV_PARSE_INMETH);
25582 /* If this member is a template, get the underlying
25583 FUNCTION_DECL. */
25584 if (DECL_FUNCTION_TEMPLATE_P (member_function))
25585 member_function = DECL_TEMPLATE_RESULT (member_function);
25587 /* There should not be any class definitions in progress at this
25588 point; the bodies of members are only parsed outside of all class
25589 definitions. */
25590 gcc_assert (parser->num_classes_being_defined == 0);
25591 /* While we're parsing the member functions we might encounter more
25592 classes. We want to handle them right away, but we don't want
25593 them getting mixed up with functions that are currently in the
25594 queue. */
25595 push_unparsed_function_queues (parser);
25597 /* Make sure that any template parameters are in scope. */
25598 maybe_begin_member_template_processing (member_function);
25600 /* If the body of the function has not yet been parsed, parse it
25601 now. */
25602 if (DECL_PENDING_INLINE_P (member_function))
25604 tree function_scope;
25605 cp_token_cache *tokens;
25607 /* The function is no longer pending; we are processing it. */
25608 tokens = DECL_PENDING_INLINE_INFO (member_function);
25609 DECL_PENDING_INLINE_INFO (member_function) = NULL;
25610 DECL_PENDING_INLINE_P (member_function) = 0;
25612 /* If this is a local class, enter the scope of the containing
25613 function. */
25614 function_scope = current_function_decl;
25615 if (function_scope)
25616 push_function_context ();
25618 /* Push the body of the function onto the lexer stack. */
25619 cp_parser_push_lexer_for_tokens (parser, tokens);
25621 /* Let the front end know that we going to be defining this
25622 function. */
25623 start_preparsed_function (member_function, NULL_TREE,
25624 SF_PRE_PARSED | SF_INCLASS_INLINE);
25626 /* Don't do access checking if it is a templated function. */
25627 if (processing_template_decl)
25628 push_deferring_access_checks (dk_no_check);
25630 /* #pragma omp declare reduction needs special parsing. */
25631 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
25633 parser->lexer->in_pragma = true;
25634 cp_parser_omp_declare_reduction_exprs (member_function, parser);
25635 finish_function (/*inline*/2);
25636 cp_check_omp_declare_reduction (member_function);
25638 else
25639 /* Now, parse the body of the function. */
25640 cp_parser_function_definition_after_declarator (parser,
25641 /*inline_p=*/true);
25643 if (processing_template_decl)
25644 pop_deferring_access_checks ();
25646 /* Leave the scope of the containing function. */
25647 if (function_scope)
25648 pop_function_context ();
25649 cp_parser_pop_lexer (parser);
25652 /* Remove any template parameters from the symbol table. */
25653 maybe_end_member_template_processing ();
25655 /* Restore the queue. */
25656 pop_unparsed_function_queues (parser);
25657 timevar_pop (TV_PARSE_INMETH);
25660 /* If DECL contains any default args, remember it on the unparsed
25661 functions queue. */
25663 static void
25664 cp_parser_save_default_args (cp_parser* parser, tree decl)
25666 tree probe;
25668 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
25669 probe;
25670 probe = TREE_CHAIN (probe))
25671 if (TREE_PURPOSE (probe))
25673 cp_default_arg_entry entry = {current_class_type, decl};
25674 vec_safe_push (unparsed_funs_with_default_args, entry);
25675 break;
25679 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
25680 which is either a FIELD_DECL or PARM_DECL. Parse it and return
25681 the result. For a PARM_DECL, PARMTYPE is the corresponding type
25682 from the parameter-type-list. */
25684 static tree
25685 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
25686 tree default_arg, tree parmtype)
25688 cp_token_cache *tokens;
25689 tree parsed_arg;
25690 bool dummy;
25692 if (default_arg == error_mark_node)
25693 return error_mark_node;
25695 /* Push the saved tokens for the default argument onto the parser's
25696 lexer stack. */
25697 tokens = DEFARG_TOKENS (default_arg);
25698 cp_parser_push_lexer_for_tokens (parser, tokens);
25700 start_lambda_scope (decl);
25702 /* Parse the default argument. */
25703 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
25704 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
25705 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25707 finish_lambda_scope ();
25709 if (parsed_arg == error_mark_node)
25710 cp_parser_skip_to_end_of_statement (parser);
25712 if (!processing_template_decl)
25714 /* In a non-template class, check conversions now. In a template,
25715 we'll wait and instantiate these as needed. */
25716 if (TREE_CODE (decl) == PARM_DECL)
25717 parsed_arg = check_default_argument (parmtype, parsed_arg,
25718 tf_warning_or_error);
25719 else
25720 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
25723 /* If the token stream has not been completely used up, then
25724 there was extra junk after the end of the default
25725 argument. */
25726 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
25728 if (TREE_CODE (decl) == PARM_DECL)
25729 cp_parser_error (parser, "expected %<,%>");
25730 else
25731 cp_parser_error (parser, "expected %<;%>");
25734 /* Revert to the main lexer. */
25735 cp_parser_pop_lexer (parser);
25737 return parsed_arg;
25740 /* FIELD is a non-static data member with an initializer which we saved for
25741 later; parse it now. */
25743 static void
25744 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
25746 tree def;
25748 maybe_begin_member_template_processing (field);
25750 push_unparsed_function_queues (parser);
25751 def = cp_parser_late_parse_one_default_arg (parser, field,
25752 DECL_INITIAL (field),
25753 NULL_TREE);
25754 pop_unparsed_function_queues (parser);
25756 maybe_end_member_template_processing ();
25758 DECL_INITIAL (field) = def;
25761 /* FN is a FUNCTION_DECL which may contains a parameter with an
25762 unparsed DEFAULT_ARG. Parse the default args now. This function
25763 assumes that the current scope is the scope in which the default
25764 argument should be processed. */
25766 static void
25767 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
25769 bool saved_local_variables_forbidden_p;
25770 tree parm, parmdecl;
25772 /* While we're parsing the default args, we might (due to the
25773 statement expression extension) encounter more classes. We want
25774 to handle them right away, but we don't want them getting mixed
25775 up with default args that are currently in the queue. */
25776 push_unparsed_function_queues (parser);
25778 /* Local variable names (and the `this' keyword) may not appear
25779 in a default argument. */
25780 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25781 parser->local_variables_forbidden_p = true;
25783 push_defarg_context (fn);
25785 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
25786 parmdecl = DECL_ARGUMENTS (fn);
25787 parm && parm != void_list_node;
25788 parm = TREE_CHAIN (parm),
25789 parmdecl = DECL_CHAIN (parmdecl))
25791 tree default_arg = TREE_PURPOSE (parm);
25792 tree parsed_arg;
25793 vec<tree, va_gc> *insts;
25794 tree copy;
25795 unsigned ix;
25797 if (!default_arg)
25798 continue;
25800 if (TREE_CODE (default_arg) != DEFAULT_ARG)
25801 /* This can happen for a friend declaration for a function
25802 already declared with default arguments. */
25803 continue;
25805 parsed_arg
25806 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
25807 default_arg,
25808 TREE_VALUE (parm));
25809 if (parsed_arg == error_mark_node)
25811 continue;
25814 TREE_PURPOSE (parm) = parsed_arg;
25816 /* Update any instantiations we've already created. */
25817 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
25818 vec_safe_iterate (insts, ix, &copy); ix++)
25819 TREE_PURPOSE (copy) = parsed_arg;
25822 pop_defarg_context ();
25824 /* Make sure no default arg is missing. */
25825 check_default_args (fn);
25827 /* Restore the state of local_variables_forbidden_p. */
25828 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25830 /* Restore the queue. */
25831 pop_unparsed_function_queues (parser);
25834 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
25836 sizeof ... ( identifier )
25838 where the 'sizeof' token has already been consumed. */
25840 static tree
25841 cp_parser_sizeof_pack (cp_parser *parser)
25843 /* Consume the `...'. */
25844 cp_lexer_consume_token (parser->lexer);
25845 maybe_warn_variadic_templates ();
25847 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
25848 if (paren)
25849 cp_lexer_consume_token (parser->lexer);
25850 else
25851 permerror (cp_lexer_peek_token (parser->lexer)->location,
25852 "%<sizeof...%> argument must be surrounded by parentheses");
25854 cp_token *token = cp_lexer_peek_token (parser->lexer);
25855 tree name = cp_parser_identifier (parser);
25856 if (name == error_mark_node)
25857 return error_mark_node;
25858 /* The name is not qualified. */
25859 parser->scope = NULL_TREE;
25860 parser->qualifying_scope = NULL_TREE;
25861 parser->object_scope = NULL_TREE;
25862 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
25863 if (expr == error_mark_node)
25864 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
25865 token->location);
25866 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
25867 expr = TREE_TYPE (expr);
25868 else if (TREE_CODE (expr) == CONST_DECL)
25869 expr = DECL_INITIAL (expr);
25870 expr = make_pack_expansion (expr);
25871 PACK_EXPANSION_SIZEOF_P (expr) = true;
25873 if (paren)
25874 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25876 return expr;
25879 /* Parse the operand of `sizeof' (or a similar operator). Returns
25880 either a TYPE or an expression, depending on the form of the
25881 input. The KEYWORD indicates which kind of expression we have
25882 encountered. */
25884 static tree
25885 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
25887 tree expr = NULL_TREE;
25888 const char *saved_message;
25889 char *tmp;
25890 bool saved_integral_constant_expression_p;
25891 bool saved_non_integral_constant_expression_p;
25893 /* If it's a `...', then we are computing the length of a parameter
25894 pack. */
25895 if (keyword == RID_SIZEOF
25896 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25897 return cp_parser_sizeof_pack (parser);
25899 /* Types cannot be defined in a `sizeof' expression. Save away the
25900 old message. */
25901 saved_message = parser->type_definition_forbidden_message;
25902 /* And create the new one. */
25903 tmp = concat ("types may not be defined in %<",
25904 IDENTIFIER_POINTER (ridpointers[keyword]),
25905 "%> expressions", NULL);
25906 parser->type_definition_forbidden_message = tmp;
25908 /* The restrictions on constant-expressions do not apply inside
25909 sizeof expressions. */
25910 saved_integral_constant_expression_p
25911 = parser->integral_constant_expression_p;
25912 saved_non_integral_constant_expression_p
25913 = parser->non_integral_constant_expression_p;
25914 parser->integral_constant_expression_p = false;
25916 /* Do not actually evaluate the expression. */
25917 ++cp_unevaluated_operand;
25918 ++c_inhibit_evaluation_warnings;
25919 /* If it's a `(', then we might be looking at the type-id
25920 construction. */
25921 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25923 tree type = NULL_TREE;
25925 /* We can't be sure yet whether we're looking at a type-id or an
25926 expression. */
25927 cp_parser_parse_tentatively (parser);
25928 /* Note: as a GNU Extension, compound literals are considered
25929 postfix-expressions as they are in C99, so they are valid
25930 arguments to sizeof. See comment in cp_parser_cast_expression
25931 for details. */
25932 if (cp_parser_compound_literal_p (parser))
25933 cp_parser_simulate_error (parser);
25934 else
25936 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
25937 parser->in_type_id_in_expr_p = true;
25938 /* Look for the type-id. */
25939 type = cp_parser_type_id (parser);
25940 /* Look for the closing `)'. */
25941 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25942 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
25945 /* If all went well, then we're done. */
25946 if (cp_parser_parse_definitely (parser))
25948 cp_decl_specifier_seq decl_specs;
25950 /* Build a trivial decl-specifier-seq. */
25951 clear_decl_specs (&decl_specs);
25952 decl_specs.type = type;
25954 /* Call grokdeclarator to figure out what type this is. */
25955 expr = grokdeclarator (NULL,
25956 &decl_specs,
25957 TYPENAME,
25958 /*initialized=*/0,
25959 /*attrlist=*/NULL);
25963 /* If the type-id production did not work out, then we must be
25964 looking at the unary-expression production. */
25965 if (!expr)
25966 expr = cp_parser_unary_expression (parser);
25968 /* Go back to evaluating expressions. */
25969 --cp_unevaluated_operand;
25970 --c_inhibit_evaluation_warnings;
25972 /* Free the message we created. */
25973 free (tmp);
25974 /* And restore the old one. */
25975 parser->type_definition_forbidden_message = saved_message;
25976 parser->integral_constant_expression_p
25977 = saved_integral_constant_expression_p;
25978 parser->non_integral_constant_expression_p
25979 = saved_non_integral_constant_expression_p;
25981 return expr;
25984 /* If the current declaration has no declarator, return true. */
25986 static bool
25987 cp_parser_declares_only_class_p (cp_parser *parser)
25989 /* If the next token is a `;' or a `,' then there is no
25990 declarator. */
25991 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25992 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
25995 /* Update the DECL_SPECS to reflect the storage class indicated by
25996 KEYWORD. */
25998 static void
25999 cp_parser_set_storage_class (cp_parser *parser,
26000 cp_decl_specifier_seq *decl_specs,
26001 enum rid keyword,
26002 cp_token *token)
26004 cp_storage_class storage_class;
26006 if (parser->in_unbraced_linkage_specification_p)
26008 error_at (token->location, "invalid use of %qD in linkage specification",
26009 ridpointers[keyword]);
26010 return;
26012 else if (decl_specs->storage_class != sc_none)
26014 decl_specs->conflicting_specifiers_p = true;
26015 return;
26018 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26019 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26020 && decl_specs->gnu_thread_keyword_p)
26022 pedwarn (decl_specs->locations[ds_thread], 0,
26023 "%<__thread%> before %qD", ridpointers[keyword]);
26026 switch (keyword)
26028 case RID_AUTO:
26029 storage_class = sc_auto;
26030 break;
26031 case RID_REGISTER:
26032 storage_class = sc_register;
26033 break;
26034 case RID_STATIC:
26035 storage_class = sc_static;
26036 break;
26037 case RID_EXTERN:
26038 storage_class = sc_extern;
26039 break;
26040 case RID_MUTABLE:
26041 storage_class = sc_mutable;
26042 break;
26043 default:
26044 gcc_unreachable ();
26046 decl_specs->storage_class = storage_class;
26047 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26049 /* A storage class specifier cannot be applied alongside a typedef
26050 specifier. If there is a typedef specifier present then set
26051 conflicting_specifiers_p which will trigger an error later
26052 on in grokdeclarator. */
26053 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26054 decl_specs->conflicting_specifiers_p = true;
26057 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26058 is true, the type is a class or enum definition. */
26060 static void
26061 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26062 tree type_spec,
26063 cp_token *token,
26064 bool type_definition_p)
26066 decl_specs->any_specifiers_p = true;
26068 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26069 (with, for example, in "typedef int wchar_t;") we remember that
26070 this is what happened. In system headers, we ignore these
26071 declarations so that G++ can work with system headers that are not
26072 C++-safe. */
26073 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26074 && !type_definition_p
26075 && (type_spec == boolean_type_node
26076 || type_spec == char16_type_node
26077 || type_spec == char32_type_node
26078 || type_spec == wchar_type_node)
26079 && (decl_specs->type
26080 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26081 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26082 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26083 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26085 decl_specs->redefined_builtin_type = type_spec;
26086 set_and_check_decl_spec_loc (decl_specs,
26087 ds_redefined_builtin_type_spec,
26088 token);
26089 if (!decl_specs->type)
26091 decl_specs->type = type_spec;
26092 decl_specs->type_definition_p = false;
26093 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26096 else if (decl_specs->type)
26097 decl_specs->multiple_types_p = true;
26098 else
26100 decl_specs->type = type_spec;
26101 decl_specs->type_definition_p = type_definition_p;
26102 decl_specs->redefined_builtin_type = NULL_TREE;
26103 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26107 /* True iff TOKEN is the GNU keyword __thread. */
26109 static bool
26110 token_is__thread (cp_token *token)
26112 gcc_assert (token->keyword == RID_THREAD);
26113 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26116 /* Set the location for a declarator specifier and check if it is
26117 duplicated.
26119 DECL_SPECS is the sequence of declarator specifiers onto which to
26120 set the location.
26122 DS is the single declarator specifier to set which location is to
26123 be set onto the existing sequence of declarators.
26125 LOCATION is the location for the declarator specifier to
26126 consider. */
26128 static void
26129 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26130 cp_decl_spec ds, cp_token *token)
26132 gcc_assert (ds < ds_last);
26134 if (decl_specs == NULL)
26135 return;
26137 source_location location = token->location;
26139 if (decl_specs->locations[ds] == 0)
26141 decl_specs->locations[ds] = location;
26142 if (ds == ds_thread)
26143 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26145 else
26147 if (ds == ds_long)
26149 if (decl_specs->locations[ds_long_long] != 0)
26150 error_at (location,
26151 "%<long long long%> is too long for GCC");
26152 else
26154 decl_specs->locations[ds_long_long] = location;
26155 pedwarn_cxx98 (location,
26156 OPT_Wlong_long,
26157 "ISO C++ 1998 does not support %<long long%>");
26160 else if (ds == ds_thread)
26162 bool gnu = token_is__thread (token);
26163 if (gnu != decl_specs->gnu_thread_keyword_p)
26164 error_at (location,
26165 "both %<__thread%> and %<thread_local%> specified");
26166 else
26167 error_at (location, "duplicate %qD", token->u.value);
26169 else
26171 static const char *const decl_spec_names[] = {
26172 "signed",
26173 "unsigned",
26174 "short",
26175 "long",
26176 "const",
26177 "volatile",
26178 "restrict",
26179 "inline",
26180 "virtual",
26181 "explicit",
26182 "friend",
26183 "typedef",
26184 "using",
26185 "constexpr",
26186 "__complex"
26188 error_at (location,
26189 "duplicate %qs", decl_spec_names[ds]);
26194 /* Return true iff the declarator specifier DS is present in the
26195 sequence of declarator specifiers DECL_SPECS. */
26197 bool
26198 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26199 cp_decl_spec ds)
26201 gcc_assert (ds < ds_last);
26203 if (decl_specs == NULL)
26204 return false;
26206 return decl_specs->locations[ds] != 0;
26209 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26210 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26212 static bool
26213 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26215 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26218 /* Issue an error message indicating that TOKEN_DESC was expected.
26219 If KEYWORD is true, it indicated this function is called by
26220 cp_parser_require_keword and the required token can only be
26221 a indicated keyword. */
26223 static void
26224 cp_parser_required_error (cp_parser *parser,
26225 required_token token_desc,
26226 bool keyword)
26228 switch (token_desc)
26230 case RT_NEW:
26231 cp_parser_error (parser, "expected %<new%>");
26232 return;
26233 case RT_DELETE:
26234 cp_parser_error (parser, "expected %<delete%>");
26235 return;
26236 case RT_RETURN:
26237 cp_parser_error (parser, "expected %<return%>");
26238 return;
26239 case RT_WHILE:
26240 cp_parser_error (parser, "expected %<while%>");
26241 return;
26242 case RT_EXTERN:
26243 cp_parser_error (parser, "expected %<extern%>");
26244 return;
26245 case RT_STATIC_ASSERT:
26246 cp_parser_error (parser, "expected %<static_assert%>");
26247 return;
26248 case RT_DECLTYPE:
26249 cp_parser_error (parser, "expected %<decltype%>");
26250 return;
26251 case RT_OPERATOR:
26252 cp_parser_error (parser, "expected %<operator%>");
26253 return;
26254 case RT_CLASS:
26255 cp_parser_error (parser, "expected %<class%>");
26256 return;
26257 case RT_TEMPLATE:
26258 cp_parser_error (parser, "expected %<template%>");
26259 return;
26260 case RT_NAMESPACE:
26261 cp_parser_error (parser, "expected %<namespace%>");
26262 return;
26263 case RT_USING:
26264 cp_parser_error (parser, "expected %<using%>");
26265 return;
26266 case RT_ASM:
26267 cp_parser_error (parser, "expected %<asm%>");
26268 return;
26269 case RT_TRY:
26270 cp_parser_error (parser, "expected %<try%>");
26271 return;
26272 case RT_CATCH:
26273 cp_parser_error (parser, "expected %<catch%>");
26274 return;
26275 case RT_THROW:
26276 cp_parser_error (parser, "expected %<throw%>");
26277 return;
26278 case RT_LABEL:
26279 cp_parser_error (parser, "expected %<__label__%>");
26280 return;
26281 case RT_AT_TRY:
26282 cp_parser_error (parser, "expected %<@try%>");
26283 return;
26284 case RT_AT_SYNCHRONIZED:
26285 cp_parser_error (parser, "expected %<@synchronized%>");
26286 return;
26287 case RT_AT_THROW:
26288 cp_parser_error (parser, "expected %<@throw%>");
26289 return;
26290 case RT_TRANSACTION_ATOMIC:
26291 cp_parser_error (parser, "expected %<__transaction_atomic%>");
26292 return;
26293 case RT_TRANSACTION_RELAXED:
26294 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26295 return;
26296 default:
26297 break;
26299 if (!keyword)
26301 switch (token_desc)
26303 case RT_SEMICOLON:
26304 cp_parser_error (parser, "expected %<;%>");
26305 return;
26306 case RT_OPEN_PAREN:
26307 cp_parser_error (parser, "expected %<(%>");
26308 return;
26309 case RT_CLOSE_BRACE:
26310 cp_parser_error (parser, "expected %<}%>");
26311 return;
26312 case RT_OPEN_BRACE:
26313 cp_parser_error (parser, "expected %<{%>");
26314 return;
26315 case RT_CLOSE_SQUARE:
26316 cp_parser_error (parser, "expected %<]%>");
26317 return;
26318 case RT_OPEN_SQUARE:
26319 cp_parser_error (parser, "expected %<[%>");
26320 return;
26321 case RT_COMMA:
26322 cp_parser_error (parser, "expected %<,%>");
26323 return;
26324 case RT_SCOPE:
26325 cp_parser_error (parser, "expected %<::%>");
26326 return;
26327 case RT_LESS:
26328 cp_parser_error (parser, "expected %<<%>");
26329 return;
26330 case RT_GREATER:
26331 cp_parser_error (parser, "expected %<>%>");
26332 return;
26333 case RT_EQ:
26334 cp_parser_error (parser, "expected %<=%>");
26335 return;
26336 case RT_ELLIPSIS:
26337 cp_parser_error (parser, "expected %<...%>");
26338 return;
26339 case RT_MULT:
26340 cp_parser_error (parser, "expected %<*%>");
26341 return;
26342 case RT_COMPL:
26343 cp_parser_error (parser, "expected %<~%>");
26344 return;
26345 case RT_COLON:
26346 cp_parser_error (parser, "expected %<:%>");
26347 return;
26348 case RT_COLON_SCOPE:
26349 cp_parser_error (parser, "expected %<:%> or %<::%>");
26350 return;
26351 case RT_CLOSE_PAREN:
26352 cp_parser_error (parser, "expected %<)%>");
26353 return;
26354 case RT_COMMA_CLOSE_PAREN:
26355 cp_parser_error (parser, "expected %<,%> or %<)%>");
26356 return;
26357 case RT_PRAGMA_EOL:
26358 cp_parser_error (parser, "expected end of line");
26359 return;
26360 case RT_NAME:
26361 cp_parser_error (parser, "expected identifier");
26362 return;
26363 case RT_SELECT:
26364 cp_parser_error (parser, "expected selection-statement");
26365 return;
26366 case RT_INTERATION:
26367 cp_parser_error (parser, "expected iteration-statement");
26368 return;
26369 case RT_JUMP:
26370 cp_parser_error (parser, "expected jump-statement");
26371 return;
26372 case RT_CLASS_KEY:
26373 cp_parser_error (parser, "expected class-key");
26374 return;
26375 case RT_CLASS_TYPENAME_TEMPLATE:
26376 cp_parser_error (parser,
26377 "expected %<class%>, %<typename%>, or %<template%>");
26378 return;
26379 default:
26380 gcc_unreachable ();
26383 else
26384 gcc_unreachable ();
26389 /* If the next token is of the indicated TYPE, consume it. Otherwise,
26390 issue an error message indicating that TOKEN_DESC was expected.
26392 Returns the token consumed, if the token had the appropriate type.
26393 Otherwise, returns NULL. */
26395 static cp_token *
26396 cp_parser_require (cp_parser* parser,
26397 enum cpp_ttype type,
26398 required_token token_desc)
26400 if (cp_lexer_next_token_is (parser->lexer, type))
26401 return cp_lexer_consume_token (parser->lexer);
26402 else
26404 /* Output the MESSAGE -- unless we're parsing tentatively. */
26405 if (!cp_parser_simulate_error (parser))
26406 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
26407 return NULL;
26411 /* An error message is produced if the next token is not '>'.
26412 All further tokens are skipped until the desired token is
26413 found or '{', '}', ';' or an unbalanced ')' or ']'. */
26415 static void
26416 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
26418 /* Current level of '< ... >'. */
26419 unsigned level = 0;
26420 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
26421 unsigned nesting_depth = 0;
26423 /* Are we ready, yet? If not, issue error message. */
26424 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
26425 return;
26427 /* Skip tokens until the desired token is found. */
26428 while (true)
26430 /* Peek at the next token. */
26431 switch (cp_lexer_peek_token (parser->lexer)->type)
26433 case CPP_LESS:
26434 if (!nesting_depth)
26435 ++level;
26436 break;
26438 case CPP_RSHIFT:
26439 if (cxx_dialect == cxx98)
26440 /* C++0x views the `>>' operator as two `>' tokens, but
26441 C++98 does not. */
26442 break;
26443 else if (!nesting_depth && level-- == 0)
26445 /* We've hit a `>>' where the first `>' closes the
26446 template argument list, and the second `>' is
26447 spurious. Just consume the `>>' and stop; we've
26448 already produced at least one error. */
26449 cp_lexer_consume_token (parser->lexer);
26450 return;
26452 /* Fall through for C++0x, so we handle the second `>' in
26453 the `>>'. */
26455 case CPP_GREATER:
26456 if (!nesting_depth && level-- == 0)
26458 /* We've reached the token we want, consume it and stop. */
26459 cp_lexer_consume_token (parser->lexer);
26460 return;
26462 break;
26464 case CPP_OPEN_PAREN:
26465 case CPP_OPEN_SQUARE:
26466 ++nesting_depth;
26467 break;
26469 case CPP_CLOSE_PAREN:
26470 case CPP_CLOSE_SQUARE:
26471 if (nesting_depth-- == 0)
26472 return;
26473 break;
26475 case CPP_EOF:
26476 case CPP_PRAGMA_EOL:
26477 case CPP_SEMICOLON:
26478 case CPP_OPEN_BRACE:
26479 case CPP_CLOSE_BRACE:
26480 /* The '>' was probably forgotten, don't look further. */
26481 return;
26483 default:
26484 break;
26487 /* Consume this token. */
26488 cp_lexer_consume_token (parser->lexer);
26492 /* If the next token is the indicated keyword, consume it. Otherwise,
26493 issue an error message indicating that TOKEN_DESC was expected.
26495 Returns the token consumed, if the token had the appropriate type.
26496 Otherwise, returns NULL. */
26498 static cp_token *
26499 cp_parser_require_keyword (cp_parser* parser,
26500 enum rid keyword,
26501 required_token token_desc)
26503 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
26505 if (token && token->keyword != keyword)
26507 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
26508 return NULL;
26511 return token;
26514 /* Returns TRUE iff TOKEN is a token that can begin the body of a
26515 function-definition. */
26517 static bool
26518 cp_parser_token_starts_function_definition_p (cp_token* token)
26520 return (/* An ordinary function-body begins with an `{'. */
26521 token->type == CPP_OPEN_BRACE
26522 /* A ctor-initializer begins with a `:'. */
26523 || token->type == CPP_COLON
26524 /* A function-try-block begins with `try'. */
26525 || token->keyword == RID_TRY
26526 /* A function-transaction-block begins with `__transaction_atomic'
26527 or `__transaction_relaxed'. */
26528 || token->keyword == RID_TRANSACTION_ATOMIC
26529 || token->keyword == RID_TRANSACTION_RELAXED
26530 /* The named return value extension begins with `return'. */
26531 || token->keyword == RID_RETURN);
26534 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
26535 definition. */
26537 static bool
26538 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
26540 cp_token *token;
26542 token = cp_lexer_peek_token (parser->lexer);
26543 return (token->type == CPP_OPEN_BRACE
26544 || (token->type == CPP_COLON
26545 && !parser->colon_doesnt_start_class_def_p));
26548 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
26549 C++0x) ending a template-argument. */
26551 static bool
26552 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
26554 cp_token *token;
26556 token = cp_lexer_peek_token (parser->lexer);
26557 return (token->type == CPP_COMMA
26558 || token->type == CPP_GREATER
26559 || token->type == CPP_ELLIPSIS
26560 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
26563 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
26564 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
26566 static bool
26567 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
26568 size_t n)
26570 cp_token *token;
26572 token = cp_lexer_peek_nth_token (parser->lexer, n);
26573 if (token->type == CPP_LESS)
26574 return true;
26575 /* Check for the sequence `<::' in the original code. It would be lexed as
26576 `[:', where `[' is a digraph, and there is no whitespace before
26577 `:'. */
26578 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
26580 cp_token *token2;
26581 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
26582 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
26583 return true;
26585 return false;
26588 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
26589 or none_type otherwise. */
26591 static enum tag_types
26592 cp_parser_token_is_class_key (cp_token* token)
26594 switch (token->keyword)
26596 case RID_CLASS:
26597 return class_type;
26598 case RID_STRUCT:
26599 return record_type;
26600 case RID_UNION:
26601 return union_type;
26603 default:
26604 return none_type;
26608 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
26609 or none_type otherwise or if the token is null. */
26611 static enum tag_types
26612 cp_parser_token_is_type_parameter_key (cp_token* token)
26614 if (!token)
26615 return none_type;
26617 switch (token->keyword)
26619 case RID_CLASS:
26620 return class_type;
26621 case RID_TYPENAME:
26622 return typename_type;
26624 default:
26625 return none_type;
26629 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
26631 static void
26632 cp_parser_check_class_key (enum tag_types class_key, tree type)
26634 if (type == error_mark_node)
26635 return;
26636 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
26638 if (permerror (input_location, "%qs tag used in naming %q#T",
26639 class_key == union_type ? "union"
26640 : class_key == record_type ? "struct" : "class",
26641 type))
26642 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
26643 "%q#T was previously declared here", type);
26647 /* Issue an error message if DECL is redeclared with different
26648 access than its original declaration [class.access.spec/3].
26649 This applies to nested classes and nested class templates.
26650 [class.mem/1]. */
26652 static void
26653 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
26655 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
26656 return;
26658 if ((TREE_PRIVATE (decl)
26659 != (current_access_specifier == access_private_node))
26660 || (TREE_PROTECTED (decl)
26661 != (current_access_specifier == access_protected_node)))
26662 error_at (location, "%qD redeclared with different access", decl);
26665 /* Look for the `template' keyword, as a syntactic disambiguator.
26666 Return TRUE iff it is present, in which case it will be
26667 consumed. */
26669 static bool
26670 cp_parser_optional_template_keyword (cp_parser *parser)
26672 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26674 /* In C++98 the `template' keyword can only be used within templates;
26675 outside templates the parser can always figure out what is a
26676 template and what is not. In C++11, per the resolution of DR 468,
26677 `template' is allowed in cases where it is not strictly necessary. */
26678 if (!processing_template_decl
26679 && pedantic && cxx_dialect == cxx98)
26681 cp_token *token = cp_lexer_peek_token (parser->lexer);
26682 pedwarn (token->location, OPT_Wpedantic,
26683 "in C++98 %<template%> (as a disambiguator) is only "
26684 "allowed within templates");
26685 /* If this part of the token stream is rescanned, the same
26686 error message would be generated. So, we purge the token
26687 from the stream. */
26688 cp_lexer_purge_token (parser->lexer);
26689 return false;
26691 else
26693 /* Consume the `template' keyword. */
26694 cp_lexer_consume_token (parser->lexer);
26695 return true;
26698 return false;
26701 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
26702 set PARSER->SCOPE, and perform other related actions. */
26704 static void
26705 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
26707 int i;
26708 struct tree_check *check_value;
26709 deferred_access_check *chk;
26710 vec<deferred_access_check, va_gc> *checks;
26712 /* Get the stored value. */
26713 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
26714 /* Perform any access checks that were deferred. */
26715 checks = check_value->checks;
26716 if (checks)
26718 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
26719 perform_or_defer_access_check (chk->binfo,
26720 chk->decl,
26721 chk->diag_decl, tf_warning_or_error);
26723 /* Set the scope from the stored value. */
26724 parser->scope = check_value->value;
26725 parser->qualifying_scope = check_value->qualifying_scope;
26726 parser->object_scope = NULL_TREE;
26729 /* Consume tokens up through a non-nested END token. Returns TRUE if we
26730 encounter the end of a block before what we were looking for. */
26732 static bool
26733 cp_parser_cache_group (cp_parser *parser,
26734 enum cpp_ttype end,
26735 unsigned depth)
26737 while (true)
26739 cp_token *token = cp_lexer_peek_token (parser->lexer);
26741 /* Abort a parenthesized expression if we encounter a semicolon. */
26742 if ((end == CPP_CLOSE_PAREN || depth == 0)
26743 && token->type == CPP_SEMICOLON)
26744 return true;
26745 /* If we've reached the end of the file, stop. */
26746 if (token->type == CPP_EOF
26747 || (end != CPP_PRAGMA_EOL
26748 && token->type == CPP_PRAGMA_EOL))
26749 return true;
26750 if (token->type == CPP_CLOSE_BRACE && depth == 0)
26751 /* We've hit the end of an enclosing block, so there's been some
26752 kind of syntax error. */
26753 return true;
26755 /* Consume the token. */
26756 cp_lexer_consume_token (parser->lexer);
26757 /* See if it starts a new group. */
26758 if (token->type == CPP_OPEN_BRACE)
26760 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
26761 /* In theory this should probably check end == '}', but
26762 cp_parser_save_member_function_body needs it to exit
26763 after either '}' or ')' when called with ')'. */
26764 if (depth == 0)
26765 return false;
26767 else if (token->type == CPP_OPEN_PAREN)
26769 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
26770 if (depth == 0 && end == CPP_CLOSE_PAREN)
26771 return false;
26773 else if (token->type == CPP_PRAGMA)
26774 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
26775 else if (token->type == end)
26776 return false;
26780 /* Like above, for caching a default argument or NSDMI. Both of these are
26781 terminated by a non-nested comma, but it can be unclear whether or not a
26782 comma is nested in a template argument list unless we do more parsing.
26783 In order to handle this ambiguity, when we encounter a ',' after a '<'
26784 we try to parse what follows as a parameter-declaration-list (in the
26785 case of a default argument) or a member-declarator (in the case of an
26786 NSDMI). If that succeeds, then we stop caching. */
26788 static tree
26789 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
26791 unsigned depth = 0;
26792 int maybe_template_id = 0;
26793 cp_token *first_token;
26794 cp_token *token;
26795 tree default_argument;
26797 /* Add tokens until we have processed the entire default
26798 argument. We add the range [first_token, token). */
26799 first_token = cp_lexer_peek_token (parser->lexer);
26800 if (first_token->type == CPP_OPEN_BRACE)
26802 /* For list-initialization, this is straightforward. */
26803 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26804 token = cp_lexer_peek_token (parser->lexer);
26806 else while (true)
26808 bool done = false;
26810 /* Peek at the next token. */
26811 token = cp_lexer_peek_token (parser->lexer);
26812 /* What we do depends on what token we have. */
26813 switch (token->type)
26815 /* In valid code, a default argument must be
26816 immediately followed by a `,' `)', or `...'. */
26817 case CPP_COMMA:
26818 if (depth == 0 && maybe_template_id)
26820 /* If we've seen a '<', we might be in a
26821 template-argument-list. Until Core issue 325 is
26822 resolved, we don't know how this situation ought
26823 to be handled, so try to DTRT. We check whether
26824 what comes after the comma is a valid parameter
26825 declaration list. If it is, then the comma ends
26826 the default argument; otherwise the default
26827 argument continues. */
26828 bool error = false;
26829 cp_token *peek;
26831 /* Set ITALP so cp_parser_parameter_declaration_list
26832 doesn't decide to commit to this parse. */
26833 bool saved_italp = parser->in_template_argument_list_p;
26834 parser->in_template_argument_list_p = true;
26836 cp_parser_parse_tentatively (parser);
26838 if (nsdmi)
26840 /* Parse declarators until we reach a non-comma or
26841 somthing that cannot be an initializer.
26842 Just checking whether we're looking at a single
26843 declarator is insufficient. Consider:
26844 int var = tuple<T,U>::x;
26845 The template parameter 'U' looks exactly like a
26846 declarator. */
26849 int ctor_dtor_or_conv_p;
26850 cp_lexer_consume_token (parser->lexer);
26851 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26852 &ctor_dtor_or_conv_p,
26853 /*parenthesized_p=*/NULL,
26854 /*member_p=*/true,
26855 /*friend_p=*/false);
26856 peek = cp_lexer_peek_token (parser->lexer);
26857 if (cp_parser_error_occurred (parser))
26858 break;
26860 while (peek->type == CPP_COMMA);
26861 /* If we met an '=' or ';' then the original comma
26862 was the end of the NSDMI. Otherwise assume
26863 we're still in the NSDMI. */
26864 error = (peek->type != CPP_EQ
26865 && peek->type != CPP_SEMICOLON);
26867 else
26869 cp_lexer_consume_token (parser->lexer);
26870 begin_scope (sk_function_parms, NULL_TREE);
26871 cp_parser_parameter_declaration_list (parser, &error);
26872 pop_bindings_and_leave_scope ();
26874 if (!cp_parser_error_occurred (parser) && !error)
26875 done = true;
26876 cp_parser_abort_tentative_parse (parser);
26878 parser->in_template_argument_list_p = saved_italp;
26879 break;
26881 case CPP_CLOSE_PAREN:
26882 case CPP_ELLIPSIS:
26883 /* If we run into a non-nested `;', `}', or `]',
26884 then the code is invalid -- but the default
26885 argument is certainly over. */
26886 case CPP_SEMICOLON:
26887 case CPP_CLOSE_BRACE:
26888 case CPP_CLOSE_SQUARE:
26889 if (depth == 0
26890 /* Handle correctly int n = sizeof ... ( p ); */
26891 && token->type != CPP_ELLIPSIS)
26892 done = true;
26893 /* Update DEPTH, if necessary. */
26894 else if (token->type == CPP_CLOSE_PAREN
26895 || token->type == CPP_CLOSE_BRACE
26896 || token->type == CPP_CLOSE_SQUARE)
26897 --depth;
26898 break;
26900 case CPP_OPEN_PAREN:
26901 case CPP_OPEN_SQUARE:
26902 case CPP_OPEN_BRACE:
26903 ++depth;
26904 break;
26906 case CPP_LESS:
26907 if (depth == 0)
26908 /* This might be the comparison operator, or it might
26909 start a template argument list. */
26910 ++maybe_template_id;
26911 break;
26913 case CPP_RSHIFT:
26914 if (cxx_dialect == cxx98)
26915 break;
26916 /* Fall through for C++0x, which treats the `>>'
26917 operator like two `>' tokens in certain
26918 cases. */
26920 case CPP_GREATER:
26921 if (depth == 0)
26923 /* This might be an operator, or it might close a
26924 template argument list. But if a previous '<'
26925 started a template argument list, this will have
26926 closed it, so we can't be in one anymore. */
26927 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
26928 if (maybe_template_id < 0)
26929 maybe_template_id = 0;
26931 break;
26933 /* If we run out of tokens, issue an error message. */
26934 case CPP_EOF:
26935 case CPP_PRAGMA_EOL:
26936 error_at (token->location, "file ends in default argument");
26937 done = true;
26938 break;
26940 case CPP_NAME:
26941 case CPP_SCOPE:
26942 /* In these cases, we should look for template-ids.
26943 For example, if the default argument is
26944 `X<int, double>()', we need to do name lookup to
26945 figure out whether or not `X' is a template; if
26946 so, the `,' does not end the default argument.
26948 That is not yet done. */
26949 break;
26951 default:
26952 break;
26955 /* If we've reached the end, stop. */
26956 if (done)
26957 break;
26959 /* Add the token to the token block. */
26960 token = cp_lexer_consume_token (parser->lexer);
26963 /* Create a DEFAULT_ARG to represent the unparsed default
26964 argument. */
26965 default_argument = make_node (DEFAULT_ARG);
26966 DEFARG_TOKENS (default_argument)
26967 = cp_token_cache_new (first_token, token);
26968 DEFARG_INSTANTIATIONS (default_argument) = NULL;
26970 return default_argument;
26973 /* Begin parsing tentatively. We always save tokens while parsing
26974 tentatively so that if the tentative parsing fails we can restore the
26975 tokens. */
26977 static void
26978 cp_parser_parse_tentatively (cp_parser* parser)
26980 /* Enter a new parsing context. */
26981 parser->context = cp_parser_context_new (parser->context);
26982 /* Begin saving tokens. */
26983 cp_lexer_save_tokens (parser->lexer);
26984 /* In order to avoid repetitive access control error messages,
26985 access checks are queued up until we are no longer parsing
26986 tentatively. */
26987 push_deferring_access_checks (dk_deferred);
26990 /* Commit to the currently active tentative parse. */
26992 static void
26993 cp_parser_commit_to_tentative_parse (cp_parser* parser)
26995 cp_parser_context *context;
26996 cp_lexer *lexer;
26998 /* Mark all of the levels as committed. */
26999 lexer = parser->lexer;
27000 for (context = parser->context; context->next; context = context->next)
27002 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27003 break;
27004 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27005 while (!cp_lexer_saving_tokens (lexer))
27006 lexer = lexer->next;
27007 cp_lexer_commit_tokens (lexer);
27011 /* Commit to the topmost currently active tentative parse.
27013 Note that this function shouldn't be called when there are
27014 irreversible side-effects while in a tentative state. For
27015 example, we shouldn't create a permanent entry in the symbol
27016 table, or issue an error message that might not apply if the
27017 tentative parse is aborted. */
27019 static void
27020 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27022 cp_parser_context *context = parser->context;
27023 cp_lexer *lexer = parser->lexer;
27025 if (context)
27027 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27028 return;
27029 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27031 while (!cp_lexer_saving_tokens (lexer))
27032 lexer = lexer->next;
27033 cp_lexer_commit_tokens (lexer);
27037 /* Abort the currently active tentative parse. All consumed tokens
27038 will be rolled back, and no diagnostics will be issued. */
27040 static void
27041 cp_parser_abort_tentative_parse (cp_parser* parser)
27043 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27044 || errorcount > 0);
27045 cp_parser_simulate_error (parser);
27046 /* Now, pretend that we want to see if the construct was
27047 successfully parsed. */
27048 cp_parser_parse_definitely (parser);
27051 /* Stop parsing tentatively. If a parse error has occurred, restore the
27052 token stream. Otherwise, commit to the tokens we have consumed.
27053 Returns true if no error occurred; false otherwise. */
27055 static bool
27056 cp_parser_parse_definitely (cp_parser* parser)
27058 bool error_occurred;
27059 cp_parser_context *context;
27061 /* Remember whether or not an error occurred, since we are about to
27062 destroy that information. */
27063 error_occurred = cp_parser_error_occurred (parser);
27064 /* Remove the topmost context from the stack. */
27065 context = parser->context;
27066 parser->context = context->next;
27067 /* If no parse errors occurred, commit to the tentative parse. */
27068 if (!error_occurred)
27070 /* Commit to the tokens read tentatively, unless that was
27071 already done. */
27072 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27073 cp_lexer_commit_tokens (parser->lexer);
27075 pop_to_parent_deferring_access_checks ();
27077 /* Otherwise, if errors occurred, roll back our state so that things
27078 are just as they were before we began the tentative parse. */
27079 else
27081 cp_lexer_rollback_tokens (parser->lexer);
27082 pop_deferring_access_checks ();
27084 /* Add the context to the front of the free list. */
27085 context->next = cp_parser_context_free_list;
27086 cp_parser_context_free_list = context;
27088 return !error_occurred;
27091 /* Returns true if we are parsing tentatively and are not committed to
27092 this tentative parse. */
27094 static bool
27095 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27097 return (cp_parser_parsing_tentatively (parser)
27098 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27101 /* Returns nonzero iff an error has occurred during the most recent
27102 tentative parse. */
27104 static bool
27105 cp_parser_error_occurred (cp_parser* parser)
27107 return (cp_parser_parsing_tentatively (parser)
27108 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27111 /* Returns nonzero if GNU extensions are allowed. */
27113 static bool
27114 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27116 return parser->allow_gnu_extensions_p;
27119 /* Objective-C++ Productions */
27122 /* Parse an Objective-C expression, which feeds into a primary-expression
27123 above.
27125 objc-expression:
27126 objc-message-expression
27127 objc-string-literal
27128 objc-encode-expression
27129 objc-protocol-expression
27130 objc-selector-expression
27132 Returns a tree representation of the expression. */
27134 static tree
27135 cp_parser_objc_expression (cp_parser* parser)
27137 /* Try to figure out what kind of declaration is present. */
27138 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27140 switch (kwd->type)
27142 case CPP_OPEN_SQUARE:
27143 return cp_parser_objc_message_expression (parser);
27145 case CPP_OBJC_STRING:
27146 kwd = cp_lexer_consume_token (parser->lexer);
27147 return objc_build_string_object (kwd->u.value);
27149 case CPP_KEYWORD:
27150 switch (kwd->keyword)
27152 case RID_AT_ENCODE:
27153 return cp_parser_objc_encode_expression (parser);
27155 case RID_AT_PROTOCOL:
27156 return cp_parser_objc_protocol_expression (parser);
27158 case RID_AT_SELECTOR:
27159 return cp_parser_objc_selector_expression (parser);
27161 default:
27162 break;
27164 default:
27165 error_at (kwd->location,
27166 "misplaced %<@%D%> Objective-C++ construct",
27167 kwd->u.value);
27168 cp_parser_skip_to_end_of_block_or_statement (parser);
27171 return error_mark_node;
27174 /* Parse an Objective-C message expression.
27176 objc-message-expression:
27177 [ objc-message-receiver objc-message-args ]
27179 Returns a representation of an Objective-C message. */
27181 static tree
27182 cp_parser_objc_message_expression (cp_parser* parser)
27184 tree receiver, messageargs;
27186 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
27187 receiver = cp_parser_objc_message_receiver (parser);
27188 messageargs = cp_parser_objc_message_args (parser);
27189 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27191 return objc_build_message_expr (receiver, messageargs);
27194 /* Parse an objc-message-receiver.
27196 objc-message-receiver:
27197 expression
27198 simple-type-specifier
27200 Returns a representation of the type or expression. */
27202 static tree
27203 cp_parser_objc_message_receiver (cp_parser* parser)
27205 tree rcv;
27207 /* An Objective-C message receiver may be either (1) a type
27208 or (2) an expression. */
27209 cp_parser_parse_tentatively (parser);
27210 rcv = cp_parser_expression (parser);
27212 /* If that worked out, fine. */
27213 if (cp_parser_parse_definitely (parser))
27214 return rcv;
27216 cp_parser_parse_tentatively (parser);
27217 rcv = cp_parser_simple_type_specifier (parser,
27218 /*decl_specs=*/NULL,
27219 CP_PARSER_FLAGS_NONE);
27221 if (cp_parser_parse_definitely (parser))
27222 return objc_get_class_reference (rcv);
27224 cp_parser_error (parser, "objective-c++ message receiver expected");
27225 return error_mark_node;
27228 /* Parse the arguments and selectors comprising an Objective-C message.
27230 objc-message-args:
27231 objc-selector
27232 objc-selector-args
27233 objc-selector-args , objc-comma-args
27235 objc-selector-args:
27236 objc-selector [opt] : assignment-expression
27237 objc-selector-args objc-selector [opt] : assignment-expression
27239 objc-comma-args:
27240 assignment-expression
27241 objc-comma-args , assignment-expression
27243 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27244 selector arguments and TREE_VALUE containing a list of comma
27245 arguments. */
27247 static tree
27248 cp_parser_objc_message_args (cp_parser* parser)
27250 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27251 bool maybe_unary_selector_p = true;
27252 cp_token *token = cp_lexer_peek_token (parser->lexer);
27254 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27256 tree selector = NULL_TREE, arg;
27258 if (token->type != CPP_COLON)
27259 selector = cp_parser_objc_selector (parser);
27261 /* Detect if we have a unary selector. */
27262 if (maybe_unary_selector_p
27263 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27264 return build_tree_list (selector, NULL_TREE);
27266 maybe_unary_selector_p = false;
27267 cp_parser_require (parser, CPP_COLON, RT_COLON);
27268 arg = cp_parser_assignment_expression (parser);
27270 sel_args
27271 = chainon (sel_args,
27272 build_tree_list (selector, arg));
27274 token = cp_lexer_peek_token (parser->lexer);
27277 /* Handle non-selector arguments, if any. */
27278 while (token->type == CPP_COMMA)
27280 tree arg;
27282 cp_lexer_consume_token (parser->lexer);
27283 arg = cp_parser_assignment_expression (parser);
27285 addl_args
27286 = chainon (addl_args,
27287 build_tree_list (NULL_TREE, arg));
27289 token = cp_lexer_peek_token (parser->lexer);
27292 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27294 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27295 return build_tree_list (error_mark_node, error_mark_node);
27298 return build_tree_list (sel_args, addl_args);
27301 /* Parse an Objective-C encode expression.
27303 objc-encode-expression:
27304 @encode objc-typename
27306 Returns an encoded representation of the type argument. */
27308 static tree
27309 cp_parser_objc_encode_expression (cp_parser* parser)
27311 tree type;
27312 cp_token *token;
27314 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
27315 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27316 token = cp_lexer_peek_token (parser->lexer);
27317 type = complete_type (cp_parser_type_id (parser));
27318 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27320 if (!type)
27322 error_at (token->location,
27323 "%<@encode%> must specify a type as an argument");
27324 return error_mark_node;
27327 /* This happens if we find @encode(T) (where T is a template
27328 typename or something dependent on a template typename) when
27329 parsing a template. In that case, we can't compile it
27330 immediately, but we rather create an AT_ENCODE_EXPR which will
27331 need to be instantiated when the template is used.
27333 if (dependent_type_p (type))
27335 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
27336 TREE_READONLY (value) = 1;
27337 return value;
27340 return objc_build_encode_expr (type);
27343 /* Parse an Objective-C @defs expression. */
27345 static tree
27346 cp_parser_objc_defs_expression (cp_parser *parser)
27348 tree name;
27350 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
27351 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27352 name = cp_parser_identifier (parser);
27353 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27355 return objc_get_class_ivars (name);
27358 /* Parse an Objective-C protocol expression.
27360 objc-protocol-expression:
27361 @protocol ( identifier )
27363 Returns a representation of the protocol expression. */
27365 static tree
27366 cp_parser_objc_protocol_expression (cp_parser* parser)
27368 tree proto;
27370 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
27371 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27372 proto = cp_parser_identifier (parser);
27373 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27375 return objc_build_protocol_expr (proto);
27378 /* Parse an Objective-C selector expression.
27380 objc-selector-expression:
27381 @selector ( objc-method-signature )
27383 objc-method-signature:
27384 objc-selector
27385 objc-selector-seq
27387 objc-selector-seq:
27388 objc-selector :
27389 objc-selector-seq objc-selector :
27391 Returns a representation of the method selector. */
27393 static tree
27394 cp_parser_objc_selector_expression (cp_parser* parser)
27396 tree sel_seq = NULL_TREE;
27397 bool maybe_unary_selector_p = true;
27398 cp_token *token;
27399 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27401 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
27402 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27403 token = cp_lexer_peek_token (parser->lexer);
27405 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
27406 || token->type == CPP_SCOPE)
27408 tree selector = NULL_TREE;
27410 if (token->type != CPP_COLON
27411 || token->type == CPP_SCOPE)
27412 selector = cp_parser_objc_selector (parser);
27414 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
27415 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
27417 /* Detect if we have a unary selector. */
27418 if (maybe_unary_selector_p)
27420 sel_seq = selector;
27421 goto finish_selector;
27423 else
27425 cp_parser_error (parser, "expected %<:%>");
27428 maybe_unary_selector_p = false;
27429 token = cp_lexer_consume_token (parser->lexer);
27431 if (token->type == CPP_SCOPE)
27433 sel_seq
27434 = chainon (sel_seq,
27435 build_tree_list (selector, NULL_TREE));
27436 sel_seq
27437 = chainon (sel_seq,
27438 build_tree_list (NULL_TREE, NULL_TREE));
27440 else
27441 sel_seq
27442 = chainon (sel_seq,
27443 build_tree_list (selector, NULL_TREE));
27445 token = cp_lexer_peek_token (parser->lexer);
27448 finish_selector:
27449 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27451 return objc_build_selector_expr (loc, sel_seq);
27454 /* Parse a list of identifiers.
27456 objc-identifier-list:
27457 identifier
27458 objc-identifier-list , identifier
27460 Returns a TREE_LIST of identifier nodes. */
27462 static tree
27463 cp_parser_objc_identifier_list (cp_parser* parser)
27465 tree identifier;
27466 tree list;
27467 cp_token *sep;
27469 identifier = cp_parser_identifier (parser);
27470 if (identifier == error_mark_node)
27471 return error_mark_node;
27473 list = build_tree_list (NULL_TREE, identifier);
27474 sep = cp_lexer_peek_token (parser->lexer);
27476 while (sep->type == CPP_COMMA)
27478 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27479 identifier = cp_parser_identifier (parser);
27480 if (identifier == error_mark_node)
27481 return list;
27483 list = chainon (list, build_tree_list (NULL_TREE,
27484 identifier));
27485 sep = cp_lexer_peek_token (parser->lexer);
27488 return list;
27491 /* Parse an Objective-C alias declaration.
27493 objc-alias-declaration:
27494 @compatibility_alias identifier identifier ;
27496 This function registers the alias mapping with the Objective-C front end.
27497 It returns nothing. */
27499 static void
27500 cp_parser_objc_alias_declaration (cp_parser* parser)
27502 tree alias, orig;
27504 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
27505 alias = cp_parser_identifier (parser);
27506 orig = cp_parser_identifier (parser);
27507 objc_declare_alias (alias, orig);
27508 cp_parser_consume_semicolon_at_end_of_statement (parser);
27511 /* Parse an Objective-C class forward-declaration.
27513 objc-class-declaration:
27514 @class objc-identifier-list ;
27516 The function registers the forward declarations with the Objective-C
27517 front end. It returns nothing. */
27519 static void
27520 cp_parser_objc_class_declaration (cp_parser* parser)
27522 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
27523 while (true)
27525 tree id;
27527 id = cp_parser_identifier (parser);
27528 if (id == error_mark_node)
27529 break;
27531 objc_declare_class (id);
27533 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27534 cp_lexer_consume_token (parser->lexer);
27535 else
27536 break;
27538 cp_parser_consume_semicolon_at_end_of_statement (parser);
27541 /* Parse a list of Objective-C protocol references.
27543 objc-protocol-refs-opt:
27544 objc-protocol-refs [opt]
27546 objc-protocol-refs:
27547 < objc-identifier-list >
27549 Returns a TREE_LIST of identifiers, if any. */
27551 static tree
27552 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
27554 tree protorefs = NULL_TREE;
27556 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
27558 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
27559 protorefs = cp_parser_objc_identifier_list (parser);
27560 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
27563 return protorefs;
27566 /* Parse a Objective-C visibility specification. */
27568 static void
27569 cp_parser_objc_visibility_spec (cp_parser* parser)
27571 cp_token *vis = cp_lexer_peek_token (parser->lexer);
27573 switch (vis->keyword)
27575 case RID_AT_PRIVATE:
27576 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
27577 break;
27578 case RID_AT_PROTECTED:
27579 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
27580 break;
27581 case RID_AT_PUBLIC:
27582 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
27583 break;
27584 case RID_AT_PACKAGE:
27585 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
27586 break;
27587 default:
27588 return;
27591 /* Eat '@private'/'@protected'/'@public'. */
27592 cp_lexer_consume_token (parser->lexer);
27595 /* Parse an Objective-C method type. Return 'true' if it is a class
27596 (+) method, and 'false' if it is an instance (-) method. */
27598 static inline bool
27599 cp_parser_objc_method_type (cp_parser* parser)
27601 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
27602 return true;
27603 else
27604 return false;
27607 /* Parse an Objective-C protocol qualifier. */
27609 static tree
27610 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
27612 tree quals = NULL_TREE, node;
27613 cp_token *token = cp_lexer_peek_token (parser->lexer);
27615 node = token->u.value;
27617 while (node && identifier_p (node)
27618 && (node == ridpointers [(int) RID_IN]
27619 || node == ridpointers [(int) RID_OUT]
27620 || node == ridpointers [(int) RID_INOUT]
27621 || node == ridpointers [(int) RID_BYCOPY]
27622 || node == ridpointers [(int) RID_BYREF]
27623 || node == ridpointers [(int) RID_ONEWAY]))
27625 quals = tree_cons (NULL_TREE, node, quals);
27626 cp_lexer_consume_token (parser->lexer);
27627 token = cp_lexer_peek_token (parser->lexer);
27628 node = token->u.value;
27631 return quals;
27634 /* Parse an Objective-C typename. */
27636 static tree
27637 cp_parser_objc_typename (cp_parser* parser)
27639 tree type_name = NULL_TREE;
27641 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27643 tree proto_quals, cp_type = NULL_TREE;
27645 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
27646 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
27648 /* An ObjC type name may consist of just protocol qualifiers, in which
27649 case the type shall default to 'id'. */
27650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27652 cp_type = cp_parser_type_id (parser);
27654 /* If the type could not be parsed, an error has already
27655 been produced. For error recovery, behave as if it had
27656 not been specified, which will use the default type
27657 'id'. */
27658 if (cp_type == error_mark_node)
27660 cp_type = NULL_TREE;
27661 /* We need to skip to the closing parenthesis as
27662 cp_parser_type_id() does not seem to do it for
27663 us. */
27664 cp_parser_skip_to_closing_parenthesis (parser,
27665 /*recovering=*/true,
27666 /*or_comma=*/false,
27667 /*consume_paren=*/false);
27671 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27672 type_name = build_tree_list (proto_quals, cp_type);
27675 return type_name;
27678 /* Check to see if TYPE refers to an Objective-C selector name. */
27680 static bool
27681 cp_parser_objc_selector_p (enum cpp_ttype type)
27683 return (type == CPP_NAME || type == CPP_KEYWORD
27684 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
27685 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
27686 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
27687 || type == CPP_XOR || type == CPP_XOR_EQ);
27690 /* Parse an Objective-C selector. */
27692 static tree
27693 cp_parser_objc_selector (cp_parser* parser)
27695 cp_token *token = cp_lexer_consume_token (parser->lexer);
27697 if (!cp_parser_objc_selector_p (token->type))
27699 error_at (token->location, "invalid Objective-C++ selector name");
27700 return error_mark_node;
27703 /* C++ operator names are allowed to appear in ObjC selectors. */
27704 switch (token->type)
27706 case CPP_AND_AND: return get_identifier ("and");
27707 case CPP_AND_EQ: return get_identifier ("and_eq");
27708 case CPP_AND: return get_identifier ("bitand");
27709 case CPP_OR: return get_identifier ("bitor");
27710 case CPP_COMPL: return get_identifier ("compl");
27711 case CPP_NOT: return get_identifier ("not");
27712 case CPP_NOT_EQ: return get_identifier ("not_eq");
27713 case CPP_OR_OR: return get_identifier ("or");
27714 case CPP_OR_EQ: return get_identifier ("or_eq");
27715 case CPP_XOR: return get_identifier ("xor");
27716 case CPP_XOR_EQ: return get_identifier ("xor_eq");
27717 default: return token->u.value;
27721 /* Parse an Objective-C params list. */
27723 static tree
27724 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
27726 tree params = NULL_TREE;
27727 bool maybe_unary_selector_p = true;
27728 cp_token *token = cp_lexer_peek_token (parser->lexer);
27730 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27732 tree selector = NULL_TREE, type_name, identifier;
27733 tree parm_attr = NULL_TREE;
27735 if (token->keyword == RID_ATTRIBUTE)
27736 break;
27738 if (token->type != CPP_COLON)
27739 selector = cp_parser_objc_selector (parser);
27741 /* Detect if we have a unary selector. */
27742 if (maybe_unary_selector_p
27743 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27745 params = selector; /* Might be followed by attributes. */
27746 break;
27749 maybe_unary_selector_p = false;
27750 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27752 /* Something went quite wrong. There should be a colon
27753 here, but there is not. Stop parsing parameters. */
27754 break;
27756 type_name = cp_parser_objc_typename (parser);
27757 /* New ObjC allows attributes on parameters too. */
27758 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27759 parm_attr = cp_parser_attributes_opt (parser);
27760 identifier = cp_parser_identifier (parser);
27762 params
27763 = chainon (params,
27764 objc_build_keyword_decl (selector,
27765 type_name,
27766 identifier,
27767 parm_attr));
27769 token = cp_lexer_peek_token (parser->lexer);
27772 if (params == NULL_TREE)
27774 cp_parser_error (parser, "objective-c++ method declaration is expected");
27775 return error_mark_node;
27778 /* We allow tail attributes for the method. */
27779 if (token->keyword == RID_ATTRIBUTE)
27781 *attributes = cp_parser_attributes_opt (parser);
27782 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27783 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27784 return params;
27785 cp_parser_error (parser,
27786 "method attributes must be specified at the end");
27787 return error_mark_node;
27790 if (params == NULL_TREE)
27792 cp_parser_error (parser, "objective-c++ method declaration is expected");
27793 return error_mark_node;
27795 return params;
27798 /* Parse the non-keyword Objective-C params. */
27800 static tree
27801 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
27802 tree* attributes)
27804 tree params = make_node (TREE_LIST);
27805 cp_token *token = cp_lexer_peek_token (parser->lexer);
27806 *ellipsisp = false; /* Initially, assume no ellipsis. */
27808 while (token->type == CPP_COMMA)
27810 cp_parameter_declarator *parmdecl;
27811 tree parm;
27813 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27814 token = cp_lexer_peek_token (parser->lexer);
27816 if (token->type == CPP_ELLIPSIS)
27818 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
27819 *ellipsisp = true;
27820 token = cp_lexer_peek_token (parser->lexer);
27821 break;
27824 /* TODO: parse attributes for tail parameters. */
27825 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
27826 parm = grokdeclarator (parmdecl->declarator,
27827 &parmdecl->decl_specifiers,
27828 PARM, /*initialized=*/0,
27829 /*attrlist=*/NULL);
27831 chainon (params, build_tree_list (NULL_TREE, parm));
27832 token = cp_lexer_peek_token (parser->lexer);
27835 /* We allow tail attributes for the method. */
27836 if (token->keyword == RID_ATTRIBUTE)
27838 if (*attributes == NULL_TREE)
27840 *attributes = cp_parser_attributes_opt (parser);
27841 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27842 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27843 return params;
27845 else
27846 /* We have an error, but parse the attributes, so that we can
27847 carry on. */
27848 *attributes = cp_parser_attributes_opt (parser);
27850 cp_parser_error (parser,
27851 "method attributes must be specified at the end");
27852 return error_mark_node;
27855 return params;
27858 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
27860 static void
27861 cp_parser_objc_interstitial_code (cp_parser* parser)
27863 cp_token *token = cp_lexer_peek_token (parser->lexer);
27865 /* If the next token is `extern' and the following token is a string
27866 literal, then we have a linkage specification. */
27867 if (token->keyword == RID_EXTERN
27868 && cp_parser_is_pure_string_literal
27869 (cp_lexer_peek_nth_token (parser->lexer, 2)))
27870 cp_parser_linkage_specification (parser);
27871 /* Handle #pragma, if any. */
27872 else if (token->type == CPP_PRAGMA)
27873 cp_parser_pragma (parser, pragma_objc_icode);
27874 /* Allow stray semicolons. */
27875 else if (token->type == CPP_SEMICOLON)
27876 cp_lexer_consume_token (parser->lexer);
27877 /* Mark methods as optional or required, when building protocols. */
27878 else if (token->keyword == RID_AT_OPTIONAL)
27880 cp_lexer_consume_token (parser->lexer);
27881 objc_set_method_opt (true);
27883 else if (token->keyword == RID_AT_REQUIRED)
27885 cp_lexer_consume_token (parser->lexer);
27886 objc_set_method_opt (false);
27888 else if (token->keyword == RID_NAMESPACE)
27889 cp_parser_namespace_definition (parser);
27890 /* Other stray characters must generate errors. */
27891 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
27893 cp_lexer_consume_token (parser->lexer);
27894 error ("stray %qs between Objective-C++ methods",
27895 token->type == CPP_OPEN_BRACE ? "{" : "}");
27897 /* Finally, try to parse a block-declaration, or a function-definition. */
27898 else
27899 cp_parser_block_declaration (parser, /*statement_p=*/false);
27902 /* Parse a method signature. */
27904 static tree
27905 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
27907 tree rettype, kwdparms, optparms;
27908 bool ellipsis = false;
27909 bool is_class_method;
27911 is_class_method = cp_parser_objc_method_type (parser);
27912 rettype = cp_parser_objc_typename (parser);
27913 *attributes = NULL_TREE;
27914 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
27915 if (kwdparms == error_mark_node)
27916 return error_mark_node;
27917 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
27918 if (optparms == error_mark_node)
27919 return error_mark_node;
27921 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
27924 static bool
27925 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
27927 tree tattr;
27928 cp_lexer_save_tokens (parser->lexer);
27929 tattr = cp_parser_attributes_opt (parser);
27930 gcc_assert (tattr) ;
27932 /* If the attributes are followed by a method introducer, this is not allowed.
27933 Dump the attributes and flag the situation. */
27934 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
27935 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
27936 return true;
27938 /* Otherwise, the attributes introduce some interstitial code, possibly so
27939 rewind to allow that check. */
27940 cp_lexer_rollback_tokens (parser->lexer);
27941 return false;
27944 /* Parse an Objective-C method prototype list. */
27946 static void
27947 cp_parser_objc_method_prototype_list (cp_parser* parser)
27949 cp_token *token = cp_lexer_peek_token (parser->lexer);
27951 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
27953 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
27955 tree attributes, sig;
27956 bool is_class_method;
27957 if (token->type == CPP_PLUS)
27958 is_class_method = true;
27959 else
27960 is_class_method = false;
27961 sig = cp_parser_objc_method_signature (parser, &attributes);
27962 if (sig == error_mark_node)
27964 cp_parser_skip_to_end_of_block_or_statement (parser);
27965 token = cp_lexer_peek_token (parser->lexer);
27966 continue;
27968 objc_add_method_declaration (is_class_method, sig, attributes);
27969 cp_parser_consume_semicolon_at_end_of_statement (parser);
27971 else if (token->keyword == RID_AT_PROPERTY)
27972 cp_parser_objc_at_property_declaration (parser);
27973 else if (token->keyword == RID_ATTRIBUTE
27974 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
27975 warning_at (cp_lexer_peek_token (parser->lexer)->location,
27976 OPT_Wattributes,
27977 "prefix attributes are ignored for methods");
27978 else
27979 /* Allow for interspersed non-ObjC++ code. */
27980 cp_parser_objc_interstitial_code (parser);
27982 token = cp_lexer_peek_token (parser->lexer);
27985 if (token->type != CPP_EOF)
27986 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
27987 else
27988 cp_parser_error (parser, "expected %<@end%>");
27990 objc_finish_interface ();
27993 /* Parse an Objective-C method definition list. */
27995 static void
27996 cp_parser_objc_method_definition_list (cp_parser* parser)
27998 cp_token *token = cp_lexer_peek_token (parser->lexer);
28000 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28002 tree meth;
28004 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28006 cp_token *ptk;
28007 tree sig, attribute;
28008 bool is_class_method;
28009 if (token->type == CPP_PLUS)
28010 is_class_method = true;
28011 else
28012 is_class_method = false;
28013 push_deferring_access_checks (dk_deferred);
28014 sig = cp_parser_objc_method_signature (parser, &attribute);
28015 if (sig == error_mark_node)
28017 cp_parser_skip_to_end_of_block_or_statement (parser);
28018 token = cp_lexer_peek_token (parser->lexer);
28019 continue;
28021 objc_start_method_definition (is_class_method, sig, attribute,
28022 NULL_TREE);
28024 /* For historical reasons, we accept an optional semicolon. */
28025 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28026 cp_lexer_consume_token (parser->lexer);
28028 ptk = cp_lexer_peek_token (parser->lexer);
28029 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
28030 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28032 perform_deferred_access_checks (tf_warning_or_error);
28033 stop_deferring_access_checks ();
28034 meth = cp_parser_function_definition_after_declarator (parser,
28035 false);
28036 pop_deferring_access_checks ();
28037 objc_finish_method_definition (meth);
28040 /* The following case will be removed once @synthesize is
28041 completely implemented. */
28042 else if (token->keyword == RID_AT_PROPERTY)
28043 cp_parser_objc_at_property_declaration (parser);
28044 else if (token->keyword == RID_AT_SYNTHESIZE)
28045 cp_parser_objc_at_synthesize_declaration (parser);
28046 else if (token->keyword == RID_AT_DYNAMIC)
28047 cp_parser_objc_at_dynamic_declaration (parser);
28048 else if (token->keyword == RID_ATTRIBUTE
28049 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28050 warning_at (token->location, OPT_Wattributes,
28051 "prefix attributes are ignored for methods");
28052 else
28053 /* Allow for interspersed non-ObjC++ code. */
28054 cp_parser_objc_interstitial_code (parser);
28056 token = cp_lexer_peek_token (parser->lexer);
28059 if (token->type != CPP_EOF)
28060 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28061 else
28062 cp_parser_error (parser, "expected %<@end%>");
28064 objc_finish_implementation ();
28067 /* Parse Objective-C ivars. */
28069 static void
28070 cp_parser_objc_class_ivars (cp_parser* parser)
28072 cp_token *token = cp_lexer_peek_token (parser->lexer);
28074 if (token->type != CPP_OPEN_BRACE)
28075 return; /* No ivars specified. */
28077 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
28078 token = cp_lexer_peek_token (parser->lexer);
28080 while (token->type != CPP_CLOSE_BRACE
28081 && token->keyword != RID_AT_END && token->type != CPP_EOF)
28083 cp_decl_specifier_seq declspecs;
28084 int decl_class_or_enum_p;
28085 tree prefix_attributes;
28087 cp_parser_objc_visibility_spec (parser);
28089 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28090 break;
28092 cp_parser_decl_specifier_seq (parser,
28093 CP_PARSER_FLAGS_OPTIONAL,
28094 &declspecs,
28095 &decl_class_or_enum_p);
28097 /* auto, register, static, extern, mutable. */
28098 if (declspecs.storage_class != sc_none)
28100 cp_parser_error (parser, "invalid type for instance variable");
28101 declspecs.storage_class = sc_none;
28104 /* thread_local. */
28105 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28107 cp_parser_error (parser, "invalid type for instance variable");
28108 declspecs.locations[ds_thread] = 0;
28111 /* typedef. */
28112 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28114 cp_parser_error (parser, "invalid type for instance variable");
28115 declspecs.locations[ds_typedef] = 0;
28118 prefix_attributes = declspecs.attributes;
28119 declspecs.attributes = NULL_TREE;
28121 /* Keep going until we hit the `;' at the end of the
28122 declaration. */
28123 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28125 tree width = NULL_TREE, attributes, first_attribute, decl;
28126 cp_declarator *declarator = NULL;
28127 int ctor_dtor_or_conv_p;
28129 /* Check for a (possibly unnamed) bitfield declaration. */
28130 token = cp_lexer_peek_token (parser->lexer);
28131 if (token->type == CPP_COLON)
28132 goto eat_colon;
28134 if (token->type == CPP_NAME
28135 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28136 == CPP_COLON))
28138 /* Get the name of the bitfield. */
28139 declarator = make_id_declarator (NULL_TREE,
28140 cp_parser_identifier (parser),
28141 sfk_none);
28143 eat_colon:
28144 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28145 /* Get the width of the bitfield. */
28146 width
28147 = cp_parser_constant_expression (parser);
28149 else
28151 /* Parse the declarator. */
28152 declarator
28153 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28154 &ctor_dtor_or_conv_p,
28155 /*parenthesized_p=*/NULL,
28156 /*member_p=*/false,
28157 /*friend_p=*/false);
28160 /* Look for attributes that apply to the ivar. */
28161 attributes = cp_parser_attributes_opt (parser);
28162 /* Remember which attributes are prefix attributes and
28163 which are not. */
28164 first_attribute = attributes;
28165 /* Combine the attributes. */
28166 attributes = chainon (prefix_attributes, attributes);
28168 if (width)
28169 /* Create the bitfield declaration. */
28170 decl = grokbitfield (declarator, &declspecs,
28171 width,
28172 attributes);
28173 else
28174 decl = grokfield (declarator, &declspecs,
28175 NULL_TREE, /*init_const_expr_p=*/false,
28176 NULL_TREE, attributes);
28178 /* Add the instance variable. */
28179 if (decl != error_mark_node && decl != NULL_TREE)
28180 objc_add_instance_variable (decl);
28182 /* Reset PREFIX_ATTRIBUTES. */
28183 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28184 attributes = TREE_CHAIN (attributes);
28185 if (attributes)
28186 TREE_CHAIN (attributes) = NULL_TREE;
28188 token = cp_lexer_peek_token (parser->lexer);
28190 if (token->type == CPP_COMMA)
28192 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28193 continue;
28195 break;
28198 cp_parser_consume_semicolon_at_end_of_statement (parser);
28199 token = cp_lexer_peek_token (parser->lexer);
28202 if (token->keyword == RID_AT_END)
28203 cp_parser_error (parser, "expected %<}%>");
28205 /* Do not consume the RID_AT_END, so it will be read again as terminating
28206 the @interface of @implementation. */
28207 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28208 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
28210 /* For historical reasons, we accept an optional semicolon. */
28211 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28212 cp_lexer_consume_token (parser->lexer);
28215 /* Parse an Objective-C protocol declaration. */
28217 static void
28218 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28220 tree proto, protorefs;
28221 cp_token *tok;
28223 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28226 tok = cp_lexer_peek_token (parser->lexer);
28227 error_at (tok->location, "identifier expected after %<@protocol%>");
28228 cp_parser_consume_semicolon_at_end_of_statement (parser);
28229 return;
28232 /* See if we have a forward declaration or a definition. */
28233 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28235 /* Try a forward declaration first. */
28236 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28238 while (true)
28240 tree id;
28242 id = cp_parser_identifier (parser);
28243 if (id == error_mark_node)
28244 break;
28246 objc_declare_protocol (id, attributes);
28248 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28249 cp_lexer_consume_token (parser->lexer);
28250 else
28251 break;
28253 cp_parser_consume_semicolon_at_end_of_statement (parser);
28256 /* Ok, we got a full-fledged definition (or at least should). */
28257 else
28259 proto = cp_parser_identifier (parser);
28260 protorefs = cp_parser_objc_protocol_refs_opt (parser);
28261 objc_start_protocol (proto, protorefs, attributes);
28262 cp_parser_objc_method_prototype_list (parser);
28266 /* Parse an Objective-C superclass or category. */
28268 static void
28269 cp_parser_objc_superclass_or_category (cp_parser *parser,
28270 bool iface_p,
28271 tree *super,
28272 tree *categ, bool *is_class_extension)
28274 cp_token *next = cp_lexer_peek_token (parser->lexer);
28276 *super = *categ = NULL_TREE;
28277 *is_class_extension = false;
28278 if (next->type == CPP_COLON)
28280 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28281 *super = cp_parser_identifier (parser);
28283 else if (next->type == CPP_OPEN_PAREN)
28285 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28287 /* If there is no category name, and this is an @interface, we
28288 have a class extension. */
28289 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28291 *categ = NULL_TREE;
28292 *is_class_extension = true;
28294 else
28295 *categ = cp_parser_identifier (parser);
28297 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28301 /* Parse an Objective-C class interface. */
28303 static void
28304 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
28306 tree name, super, categ, protos;
28307 bool is_class_extension;
28309 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
28310 name = cp_parser_identifier (parser);
28311 if (name == error_mark_node)
28313 /* It's hard to recover because even if valid @interface stuff
28314 is to follow, we can't compile it (or validate it) if we
28315 don't even know which class it refers to. Let's assume this
28316 was a stray '@interface' token in the stream and skip it.
28318 return;
28320 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
28321 &is_class_extension);
28322 protos = cp_parser_objc_protocol_refs_opt (parser);
28324 /* We have either a class or a category on our hands. */
28325 if (categ || is_class_extension)
28326 objc_start_category_interface (name, categ, protos, attributes);
28327 else
28329 objc_start_class_interface (name, super, protos, attributes);
28330 /* Handle instance variable declarations, if any. */
28331 cp_parser_objc_class_ivars (parser);
28332 objc_continue_interface ();
28335 cp_parser_objc_method_prototype_list (parser);
28338 /* Parse an Objective-C class implementation. */
28340 static void
28341 cp_parser_objc_class_implementation (cp_parser* parser)
28343 tree name, super, categ;
28344 bool is_class_extension;
28346 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
28347 name = cp_parser_identifier (parser);
28348 if (name == error_mark_node)
28350 /* It's hard to recover because even if valid @implementation
28351 stuff is to follow, we can't compile it (or validate it) if
28352 we don't even know which class it refers to. Let's assume
28353 this was a stray '@implementation' token in the stream and
28354 skip it.
28356 return;
28358 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
28359 &is_class_extension);
28361 /* We have either a class or a category on our hands. */
28362 if (categ)
28363 objc_start_category_implementation (name, categ);
28364 else
28366 objc_start_class_implementation (name, super);
28367 /* Handle instance variable declarations, if any. */
28368 cp_parser_objc_class_ivars (parser);
28369 objc_continue_implementation ();
28372 cp_parser_objc_method_definition_list (parser);
28375 /* Consume the @end token and finish off the implementation. */
28377 static void
28378 cp_parser_objc_end_implementation (cp_parser* parser)
28380 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28381 objc_finish_implementation ();
28384 /* Parse an Objective-C declaration. */
28386 static void
28387 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
28389 /* Try to figure out what kind of declaration is present. */
28390 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28392 if (attributes)
28393 switch (kwd->keyword)
28395 case RID_AT_ALIAS:
28396 case RID_AT_CLASS:
28397 case RID_AT_END:
28398 error_at (kwd->location, "attributes may not be specified before"
28399 " the %<@%D%> Objective-C++ keyword",
28400 kwd->u.value);
28401 attributes = NULL;
28402 break;
28403 case RID_AT_IMPLEMENTATION:
28404 warning_at (kwd->location, OPT_Wattributes,
28405 "prefix attributes are ignored before %<@%D%>",
28406 kwd->u.value);
28407 attributes = NULL;
28408 default:
28409 break;
28412 switch (kwd->keyword)
28414 case RID_AT_ALIAS:
28415 cp_parser_objc_alias_declaration (parser);
28416 break;
28417 case RID_AT_CLASS:
28418 cp_parser_objc_class_declaration (parser);
28419 break;
28420 case RID_AT_PROTOCOL:
28421 cp_parser_objc_protocol_declaration (parser, attributes);
28422 break;
28423 case RID_AT_INTERFACE:
28424 cp_parser_objc_class_interface (parser, attributes);
28425 break;
28426 case RID_AT_IMPLEMENTATION:
28427 cp_parser_objc_class_implementation (parser);
28428 break;
28429 case RID_AT_END:
28430 cp_parser_objc_end_implementation (parser);
28431 break;
28432 default:
28433 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
28434 kwd->u.value);
28435 cp_parser_skip_to_end_of_block_or_statement (parser);
28439 /* Parse an Objective-C try-catch-finally statement.
28441 objc-try-catch-finally-stmt:
28442 @try compound-statement objc-catch-clause-seq [opt]
28443 objc-finally-clause [opt]
28445 objc-catch-clause-seq:
28446 objc-catch-clause objc-catch-clause-seq [opt]
28448 objc-catch-clause:
28449 @catch ( objc-exception-declaration ) compound-statement
28451 objc-finally-clause:
28452 @finally compound-statement
28454 objc-exception-declaration:
28455 parameter-declaration
28456 '...'
28458 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
28460 Returns NULL_TREE.
28462 PS: This function is identical to c_parser_objc_try_catch_finally_statement
28463 for C. Keep them in sync. */
28465 static tree
28466 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
28468 location_t location;
28469 tree stmt;
28471 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
28472 location = cp_lexer_peek_token (parser->lexer)->location;
28473 objc_maybe_warn_exceptions (location);
28474 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
28475 node, lest it get absorbed into the surrounding block. */
28476 stmt = push_stmt_list ();
28477 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28478 objc_begin_try_stmt (location, pop_stmt_list (stmt));
28480 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
28482 cp_parameter_declarator *parm;
28483 tree parameter_declaration = error_mark_node;
28484 bool seen_open_paren = false;
28486 cp_lexer_consume_token (parser->lexer);
28487 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28488 seen_open_paren = true;
28489 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28491 /* We have "@catch (...)" (where the '...' are literally
28492 what is in the code). Skip the '...'.
28493 parameter_declaration is set to NULL_TREE, and
28494 objc_being_catch_clauses() knows that that means
28495 '...'. */
28496 cp_lexer_consume_token (parser->lexer);
28497 parameter_declaration = NULL_TREE;
28499 else
28501 /* We have "@catch (NSException *exception)" or something
28502 like that. Parse the parameter declaration. */
28503 parm = cp_parser_parameter_declaration (parser, false, NULL);
28504 if (parm == NULL)
28505 parameter_declaration = error_mark_node;
28506 else
28507 parameter_declaration = grokdeclarator (parm->declarator,
28508 &parm->decl_specifiers,
28509 PARM, /*initialized=*/0,
28510 /*attrlist=*/NULL);
28512 if (seen_open_paren)
28513 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28514 else
28516 /* If there was no open parenthesis, we are recovering from
28517 an error, and we are trying to figure out what mistake
28518 the user has made. */
28520 /* If there is an immediate closing parenthesis, the user
28521 probably forgot the opening one (ie, they typed "@catch
28522 NSException *e)". Parse the closing parenthesis and keep
28523 going. */
28524 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28525 cp_lexer_consume_token (parser->lexer);
28527 /* If these is no immediate closing parenthesis, the user
28528 probably doesn't know that parenthesis are required at
28529 all (ie, they typed "@catch NSException *e"). So, just
28530 forget about the closing parenthesis and keep going. */
28532 objc_begin_catch_clause (parameter_declaration);
28533 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28534 objc_finish_catch_clause ();
28536 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
28538 cp_lexer_consume_token (parser->lexer);
28539 location = cp_lexer_peek_token (parser->lexer)->location;
28540 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
28541 node, lest it get absorbed into the surrounding block. */
28542 stmt = push_stmt_list ();
28543 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28544 objc_build_finally_clause (location, pop_stmt_list (stmt));
28547 return objc_finish_try_stmt ();
28550 /* Parse an Objective-C synchronized statement.
28552 objc-synchronized-stmt:
28553 @synchronized ( expression ) compound-statement
28555 Returns NULL_TREE. */
28557 static tree
28558 cp_parser_objc_synchronized_statement (cp_parser *parser)
28560 location_t location;
28561 tree lock, stmt;
28563 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
28565 location = cp_lexer_peek_token (parser->lexer)->location;
28566 objc_maybe_warn_exceptions (location);
28567 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28568 lock = cp_parser_expression (parser);
28569 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28571 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
28572 node, lest it get absorbed into the surrounding block. */
28573 stmt = push_stmt_list ();
28574 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28576 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
28579 /* Parse an Objective-C throw statement.
28581 objc-throw-stmt:
28582 @throw assignment-expression [opt] ;
28584 Returns a constructed '@throw' statement. */
28586 static tree
28587 cp_parser_objc_throw_statement (cp_parser *parser)
28589 tree expr = NULL_TREE;
28590 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28592 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
28594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28595 expr = cp_parser_expression (parser);
28597 cp_parser_consume_semicolon_at_end_of_statement (parser);
28599 return objc_build_throw_stmt (loc, expr);
28602 /* Parse an Objective-C statement. */
28604 static tree
28605 cp_parser_objc_statement (cp_parser * parser)
28607 /* Try to figure out what kind of declaration is present. */
28608 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28610 switch (kwd->keyword)
28612 case RID_AT_TRY:
28613 return cp_parser_objc_try_catch_finally_statement (parser);
28614 case RID_AT_SYNCHRONIZED:
28615 return cp_parser_objc_synchronized_statement (parser);
28616 case RID_AT_THROW:
28617 return cp_parser_objc_throw_statement (parser);
28618 default:
28619 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
28620 kwd->u.value);
28621 cp_parser_skip_to_end_of_block_or_statement (parser);
28624 return error_mark_node;
28627 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
28628 look ahead to see if an objc keyword follows the attributes. This
28629 is to detect the use of prefix attributes on ObjC @interface and
28630 @protocol. */
28632 static bool
28633 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
28635 cp_lexer_save_tokens (parser->lexer);
28636 *attrib = cp_parser_attributes_opt (parser);
28637 gcc_assert (*attrib);
28638 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
28640 cp_lexer_commit_tokens (parser->lexer);
28641 return true;
28643 cp_lexer_rollback_tokens (parser->lexer);
28644 return false;
28647 /* This routine is a minimal replacement for
28648 c_parser_struct_declaration () used when parsing the list of
28649 types/names or ObjC++ properties. For example, when parsing the
28650 code
28652 @property (readonly) int a, b, c;
28654 this function is responsible for parsing "int a, int b, int c" and
28655 returning the declarations as CHAIN of DECLs.
28657 TODO: Share this code with cp_parser_objc_class_ivars. It's very
28658 similar parsing. */
28659 static tree
28660 cp_parser_objc_struct_declaration (cp_parser *parser)
28662 tree decls = NULL_TREE;
28663 cp_decl_specifier_seq declspecs;
28664 int decl_class_or_enum_p;
28665 tree prefix_attributes;
28667 cp_parser_decl_specifier_seq (parser,
28668 CP_PARSER_FLAGS_NONE,
28669 &declspecs,
28670 &decl_class_or_enum_p);
28672 if (declspecs.type == error_mark_node)
28673 return error_mark_node;
28675 /* auto, register, static, extern, mutable. */
28676 if (declspecs.storage_class != sc_none)
28678 cp_parser_error (parser, "invalid type for property");
28679 declspecs.storage_class = sc_none;
28682 /* thread_local. */
28683 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28685 cp_parser_error (parser, "invalid type for property");
28686 declspecs.locations[ds_thread] = 0;
28689 /* typedef. */
28690 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28692 cp_parser_error (parser, "invalid type for property");
28693 declspecs.locations[ds_typedef] = 0;
28696 prefix_attributes = declspecs.attributes;
28697 declspecs.attributes = NULL_TREE;
28699 /* Keep going until we hit the `;' at the end of the declaration. */
28700 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28702 tree attributes, first_attribute, decl;
28703 cp_declarator *declarator;
28704 cp_token *token;
28706 /* Parse the declarator. */
28707 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28708 NULL, NULL, false, false);
28710 /* Look for attributes that apply to the ivar. */
28711 attributes = cp_parser_attributes_opt (parser);
28712 /* Remember which attributes are prefix attributes and
28713 which are not. */
28714 first_attribute = attributes;
28715 /* Combine the attributes. */
28716 attributes = chainon (prefix_attributes, attributes);
28718 decl = grokfield (declarator, &declspecs,
28719 NULL_TREE, /*init_const_expr_p=*/false,
28720 NULL_TREE, attributes);
28722 if (decl == error_mark_node || decl == NULL_TREE)
28723 return error_mark_node;
28725 /* Reset PREFIX_ATTRIBUTES. */
28726 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28727 attributes = TREE_CHAIN (attributes);
28728 if (attributes)
28729 TREE_CHAIN (attributes) = NULL_TREE;
28731 DECL_CHAIN (decl) = decls;
28732 decls = decl;
28734 token = cp_lexer_peek_token (parser->lexer);
28735 if (token->type == CPP_COMMA)
28737 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28738 continue;
28740 else
28741 break;
28743 return decls;
28746 /* Parse an Objective-C @property declaration. The syntax is:
28748 objc-property-declaration:
28749 '@property' objc-property-attributes[opt] struct-declaration ;
28751 objc-property-attributes:
28752 '(' objc-property-attribute-list ')'
28754 objc-property-attribute-list:
28755 objc-property-attribute
28756 objc-property-attribute-list, objc-property-attribute
28758 objc-property-attribute
28759 'getter' = identifier
28760 'setter' = identifier
28761 'readonly'
28762 'readwrite'
28763 'assign'
28764 'retain'
28765 'copy'
28766 'nonatomic'
28768 For example:
28769 @property NSString *name;
28770 @property (readonly) id object;
28771 @property (retain, nonatomic, getter=getTheName) id name;
28772 @property int a, b, c;
28774 PS: This function is identical to
28775 c_parser_objc_at_property_declaration for C. Keep them in sync. */
28776 static void
28777 cp_parser_objc_at_property_declaration (cp_parser *parser)
28779 /* The following variables hold the attributes of the properties as
28780 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
28781 seen. When we see an attribute, we set them to 'true' (if they
28782 are boolean properties) or to the identifier (if they have an
28783 argument, ie, for getter and setter). Note that here we only
28784 parse the list of attributes, check the syntax and accumulate the
28785 attributes that we find. objc_add_property_declaration() will
28786 then process the information. */
28787 bool property_assign = false;
28788 bool property_copy = false;
28789 tree property_getter_ident = NULL_TREE;
28790 bool property_nonatomic = false;
28791 bool property_readonly = false;
28792 bool property_readwrite = false;
28793 bool property_retain = false;
28794 tree property_setter_ident = NULL_TREE;
28796 /* 'properties' is the list of properties that we read. Usually a
28797 single one, but maybe more (eg, in "@property int a, b, c;" there
28798 are three). */
28799 tree properties;
28800 location_t loc;
28802 loc = cp_lexer_peek_token (parser->lexer)->location;
28804 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
28806 /* Parse the optional attribute list... */
28807 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28809 /* Eat the '('. */
28810 cp_lexer_consume_token (parser->lexer);
28812 while (true)
28814 bool syntax_error = false;
28815 cp_token *token = cp_lexer_peek_token (parser->lexer);
28816 enum rid keyword;
28818 if (token->type != CPP_NAME)
28820 cp_parser_error (parser, "expected identifier");
28821 break;
28823 keyword = C_RID_CODE (token->u.value);
28824 cp_lexer_consume_token (parser->lexer);
28825 switch (keyword)
28827 case RID_ASSIGN: property_assign = true; break;
28828 case RID_COPY: property_copy = true; break;
28829 case RID_NONATOMIC: property_nonatomic = true; break;
28830 case RID_READONLY: property_readonly = true; break;
28831 case RID_READWRITE: property_readwrite = true; break;
28832 case RID_RETAIN: property_retain = true; break;
28834 case RID_GETTER:
28835 case RID_SETTER:
28836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
28838 if (keyword == RID_GETTER)
28839 cp_parser_error (parser,
28840 "missing %<=%> (after %<getter%> attribute)");
28841 else
28842 cp_parser_error (parser,
28843 "missing %<=%> (after %<setter%> attribute)");
28844 syntax_error = true;
28845 break;
28847 cp_lexer_consume_token (parser->lexer); /* eat the = */
28848 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
28850 cp_parser_error (parser, "expected identifier");
28851 syntax_error = true;
28852 break;
28854 if (keyword == RID_SETTER)
28856 if (property_setter_ident != NULL_TREE)
28858 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
28859 cp_lexer_consume_token (parser->lexer);
28861 else
28862 property_setter_ident = cp_parser_objc_selector (parser);
28863 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28864 cp_parser_error (parser, "setter name must terminate with %<:%>");
28865 else
28866 cp_lexer_consume_token (parser->lexer);
28868 else
28870 if (property_getter_ident != NULL_TREE)
28872 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
28873 cp_lexer_consume_token (parser->lexer);
28875 else
28876 property_getter_ident = cp_parser_objc_selector (parser);
28878 break;
28879 default:
28880 cp_parser_error (parser, "unknown property attribute");
28881 syntax_error = true;
28882 break;
28885 if (syntax_error)
28886 break;
28888 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28889 cp_lexer_consume_token (parser->lexer);
28890 else
28891 break;
28894 /* FIXME: "@property (setter, assign);" will generate a spurious
28895 "error: expected ‘)’ before ‘,’ token". This is because
28896 cp_parser_require, unlike the C counterpart, will produce an
28897 error even if we are in error recovery. */
28898 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28900 cp_parser_skip_to_closing_parenthesis (parser,
28901 /*recovering=*/true,
28902 /*or_comma=*/false,
28903 /*consume_paren=*/true);
28907 /* ... and the property declaration(s). */
28908 properties = cp_parser_objc_struct_declaration (parser);
28910 if (properties == error_mark_node)
28912 cp_parser_skip_to_end_of_statement (parser);
28913 /* If the next token is now a `;', consume it. */
28914 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28915 cp_lexer_consume_token (parser->lexer);
28916 return;
28919 if (properties == NULL_TREE)
28920 cp_parser_error (parser, "expected identifier");
28921 else
28923 /* Comma-separated properties are chained together in
28924 reverse order; add them one by one. */
28925 properties = nreverse (properties);
28927 for (; properties; properties = TREE_CHAIN (properties))
28928 objc_add_property_declaration (loc, copy_node (properties),
28929 property_readonly, property_readwrite,
28930 property_assign, property_retain,
28931 property_copy, property_nonatomic,
28932 property_getter_ident, property_setter_ident);
28935 cp_parser_consume_semicolon_at_end_of_statement (parser);
28938 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
28940 objc-synthesize-declaration:
28941 @synthesize objc-synthesize-identifier-list ;
28943 objc-synthesize-identifier-list:
28944 objc-synthesize-identifier
28945 objc-synthesize-identifier-list, objc-synthesize-identifier
28947 objc-synthesize-identifier
28948 identifier
28949 identifier = identifier
28951 For example:
28952 @synthesize MyProperty;
28953 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
28955 PS: This function is identical to c_parser_objc_at_synthesize_declaration
28956 for C. Keep them in sync.
28958 static void
28959 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
28961 tree list = NULL_TREE;
28962 location_t loc;
28963 loc = cp_lexer_peek_token (parser->lexer)->location;
28965 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
28966 while (true)
28968 tree property, ivar;
28969 property = cp_parser_identifier (parser);
28970 if (property == error_mark_node)
28972 cp_parser_consume_semicolon_at_end_of_statement (parser);
28973 return;
28975 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
28977 cp_lexer_consume_token (parser->lexer);
28978 ivar = cp_parser_identifier (parser);
28979 if (ivar == error_mark_node)
28981 cp_parser_consume_semicolon_at_end_of_statement (parser);
28982 return;
28985 else
28986 ivar = NULL_TREE;
28987 list = chainon (list, build_tree_list (ivar, property));
28988 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28989 cp_lexer_consume_token (parser->lexer);
28990 else
28991 break;
28993 cp_parser_consume_semicolon_at_end_of_statement (parser);
28994 objc_add_synthesize_declaration (loc, list);
28997 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
28999 objc-dynamic-declaration:
29000 @dynamic identifier-list ;
29002 For example:
29003 @dynamic MyProperty;
29004 @dynamic MyProperty, AnotherProperty;
29006 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29007 for C. Keep them in sync.
29009 static void
29010 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29012 tree list = NULL_TREE;
29013 location_t loc;
29014 loc = cp_lexer_peek_token (parser->lexer)->location;
29016 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
29017 while (true)
29019 tree property;
29020 property = cp_parser_identifier (parser);
29021 if (property == error_mark_node)
29023 cp_parser_consume_semicolon_at_end_of_statement (parser);
29024 return;
29026 list = chainon (list, build_tree_list (NULL, property));
29027 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29028 cp_lexer_consume_token (parser->lexer);
29029 else
29030 break;
29032 cp_parser_consume_semicolon_at_end_of_statement (parser);
29033 objc_add_dynamic_declaration (loc, list);
29037 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29039 /* Returns name of the next clause.
29040 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29041 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29042 returned and the token is consumed. */
29044 static pragma_omp_clause
29045 cp_parser_omp_clause_name (cp_parser *parser)
29047 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29050 result = PRAGMA_OACC_CLAUSE_AUTO;
29051 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29052 result = PRAGMA_OMP_CLAUSE_IF;
29053 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29054 result = PRAGMA_OMP_CLAUSE_DEFAULT;
29055 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29056 result = PRAGMA_OACC_CLAUSE_DELETE;
29057 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29058 result = PRAGMA_OMP_CLAUSE_PRIVATE;
29059 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29060 result = PRAGMA_OMP_CLAUSE_FOR;
29061 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29063 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29064 const char *p = IDENTIFIER_POINTER (id);
29066 switch (p[0])
29068 case 'a':
29069 if (!strcmp ("aligned", p))
29070 result = PRAGMA_OMP_CLAUSE_ALIGNED;
29071 else if (!strcmp ("async", p))
29072 result = PRAGMA_OACC_CLAUSE_ASYNC;
29073 break;
29074 case 'c':
29075 if (!strcmp ("collapse", p))
29076 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29077 else if (!strcmp ("copy", p))
29078 result = PRAGMA_OACC_CLAUSE_COPY;
29079 else if (!strcmp ("copyin", p))
29080 result = PRAGMA_OMP_CLAUSE_COPYIN;
29081 else if (!strcmp ("copyout", p))
29082 result = PRAGMA_OACC_CLAUSE_COPYOUT;
29083 else if (!strcmp ("copyprivate", p))
29084 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29085 else if (!strcmp ("create", p))
29086 result = PRAGMA_OACC_CLAUSE_CREATE;
29087 break;
29088 case 'd':
29089 if (!strcmp ("defaultmap", p))
29090 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29091 else if (!strcmp ("depend", p))
29092 result = PRAGMA_OMP_CLAUSE_DEPEND;
29093 else if (!strcmp ("device", p))
29094 result = PRAGMA_OMP_CLAUSE_DEVICE;
29095 else if (!strcmp ("deviceptr", p))
29096 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29097 else if (!strcmp ("device_resident", p))
29098 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
29099 else if (!strcmp ("dist_schedule", p))
29100 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29101 break;
29102 case 'f':
29103 if (!strcmp ("final", p))
29104 result = PRAGMA_OMP_CLAUSE_FINAL;
29105 else if (!strcmp ("firstprivate", p))
29106 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29107 else if (!strcmp ("from", p))
29108 result = PRAGMA_OMP_CLAUSE_FROM;
29109 break;
29110 case 'g':
29111 if (!strcmp ("gang", p))
29112 result = PRAGMA_OACC_CLAUSE_GANG;
29113 else if (!strcmp ("grainsize", p))
29114 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29115 break;
29116 case 'h':
29117 if (!strcmp ("hint", p))
29118 result = PRAGMA_OMP_CLAUSE_HINT;
29119 else if (!strcmp ("host", p))
29120 result = PRAGMA_OACC_CLAUSE_HOST;
29121 break;
29122 case 'i':
29123 if (!strcmp ("inbranch", p))
29124 result = PRAGMA_OMP_CLAUSE_INBRANCH;
29125 else if (!strcmp ("independent", p))
29126 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29127 else if (!strcmp ("is_device_ptr", p))
29128 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29129 break;
29130 case 'l':
29131 if (!strcmp ("lastprivate", p))
29132 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29133 else if (!strcmp ("linear", p))
29134 result = PRAGMA_OMP_CLAUSE_LINEAR;
29135 else if (!strcmp ("link", p))
29136 result = PRAGMA_OMP_CLAUSE_LINK;
29137 break;
29138 case 'm':
29139 if (!strcmp ("map", p))
29140 result = PRAGMA_OMP_CLAUSE_MAP;
29141 else if (!strcmp ("mergeable", p))
29142 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29143 else if (flag_cilkplus && !strcmp ("mask", p))
29144 result = PRAGMA_CILK_CLAUSE_MASK;
29145 break;
29146 case 'n':
29147 if (!strcmp ("nogroup", p))
29148 result = PRAGMA_OMP_CLAUSE_NOGROUP;
29149 else if (!strcmp ("notinbranch", p))
29150 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29151 else if (!strcmp ("nowait", p))
29152 result = PRAGMA_OMP_CLAUSE_NOWAIT;
29153 else if (flag_cilkplus && !strcmp ("nomask", p))
29154 result = PRAGMA_CILK_CLAUSE_NOMASK;
29155 else if (!strcmp ("num_gangs", p))
29156 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29157 else if (!strcmp ("num_tasks", p))
29158 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29159 else if (!strcmp ("num_teams", p))
29160 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29161 else if (!strcmp ("num_threads", p))
29162 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
29163 else if (!strcmp ("num_workers", p))
29164 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
29165 break;
29166 case 'o':
29167 if (!strcmp ("ordered", p))
29168 result = PRAGMA_OMP_CLAUSE_ORDERED;
29169 break;
29170 case 'p':
29171 if (!strcmp ("parallel", p))
29172 result = PRAGMA_OMP_CLAUSE_PARALLEL;
29173 else if (!strcmp ("present", p))
29174 result = PRAGMA_OACC_CLAUSE_PRESENT;
29175 else if (!strcmp ("present_or_copy", p)
29176 || !strcmp ("pcopy", p))
29177 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29178 else if (!strcmp ("present_or_copyin", p)
29179 || !strcmp ("pcopyin", p))
29180 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29181 else if (!strcmp ("present_or_copyout", p)
29182 || !strcmp ("pcopyout", p))
29183 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29184 else if (!strcmp ("present_or_create", p)
29185 || !strcmp ("pcreate", p))
29186 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29187 else if (!strcmp ("priority", p))
29188 result = PRAGMA_OMP_CLAUSE_PRIORITY;
29189 else if (!strcmp ("proc_bind", p))
29190 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29191 break;
29192 case 'r':
29193 if (!strcmp ("reduction", p))
29194 result = PRAGMA_OMP_CLAUSE_REDUCTION;
29195 break;
29196 case 's':
29197 if (!strcmp ("safelen", p))
29198 result = PRAGMA_OMP_CLAUSE_SAFELEN;
29199 else if (!strcmp ("schedule", p))
29200 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29201 else if (!strcmp ("sections", p))
29202 result = PRAGMA_OMP_CLAUSE_SECTIONS;
29203 else if (!strcmp ("self", p))
29204 result = PRAGMA_OACC_CLAUSE_SELF;
29205 else if (!strcmp ("seq", p))
29206 result = PRAGMA_OACC_CLAUSE_SEQ;
29207 else if (!strcmp ("shared", p))
29208 result = PRAGMA_OMP_CLAUSE_SHARED;
29209 else if (!strcmp ("simd", p))
29210 result = PRAGMA_OMP_CLAUSE_SIMD;
29211 else if (!strcmp ("simdlen", p))
29212 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29213 break;
29214 case 't':
29215 if (!strcmp ("taskgroup", p))
29216 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29217 else if (!strcmp ("thread_limit", p))
29218 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29219 else if (!strcmp ("threads", p))
29220 result = PRAGMA_OMP_CLAUSE_THREADS;
29221 else if (!strcmp ("tile", p))
29222 result = PRAGMA_OACC_CLAUSE_TILE;
29223 else if (!strcmp ("to", p))
29224 result = PRAGMA_OMP_CLAUSE_TO;
29225 break;
29226 case 'u':
29227 if (!strcmp ("uniform", p))
29228 result = PRAGMA_OMP_CLAUSE_UNIFORM;
29229 else if (!strcmp ("untied", p))
29230 result = PRAGMA_OMP_CLAUSE_UNTIED;
29231 else if (!strcmp ("use_device_ptr", p))
29232 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
29233 break;
29234 case 'v':
29235 if (!strcmp ("vector", p))
29236 result = PRAGMA_OACC_CLAUSE_VECTOR;
29237 else if (!strcmp ("vector_length", p))
29238 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29239 else if (flag_cilkplus && !strcmp ("vectorlength", p))
29240 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29241 break;
29242 case 'w':
29243 if (!strcmp ("wait", p))
29244 result = PRAGMA_OACC_CLAUSE_WAIT;
29245 else if (!strcmp ("worker", p))
29246 result = PRAGMA_OACC_CLAUSE_WORKER;
29247 break;
29251 if (result != PRAGMA_OMP_CLAUSE_NONE)
29252 cp_lexer_consume_token (parser->lexer);
29254 return result;
29257 /* Validate that a clause of the given type does not already exist. */
29259 static void
29260 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29261 const char *name, location_t location)
29263 tree c;
29265 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29266 if (OMP_CLAUSE_CODE (c) == code)
29268 error_at (location, "too many %qs clauses", name);
29269 break;
29273 /* OpenMP 2.5:
29274 variable-list:
29275 identifier
29276 variable-list , identifier
29278 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29279 colon). An opening parenthesis will have been consumed by the caller.
29281 If KIND is nonzero, create the appropriate node and install the decl
29282 in OMP_CLAUSE_DECL and add the node to the head of the list.
29284 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29285 return the list created.
29287 COLON can be NULL if only closing parenthesis should end the list,
29288 or pointer to bool which will receive false if the list is terminated
29289 by closing parenthesis or true if the list is terminated by colon. */
29291 static tree
29292 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
29293 tree list, bool *colon)
29295 cp_token *token;
29296 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29297 if (colon)
29299 parser->colon_corrects_to_scope_p = false;
29300 *colon = false;
29302 while (1)
29304 tree name, decl;
29306 token = cp_lexer_peek_token (parser->lexer);
29307 if (kind != 0
29308 && current_class_ptr
29309 && cp_parser_is_keyword (token, RID_THIS))
29311 decl = finish_this_expr ();
29312 if (TREE_CODE (decl) == NON_LVALUE_EXPR
29313 || CONVERT_EXPR_P (decl))
29314 decl = TREE_OPERAND (decl, 0);
29315 cp_lexer_consume_token (parser->lexer);
29317 else
29319 name = cp_parser_id_expression (parser, /*template_p=*/false,
29320 /*check_dependency_p=*/true,
29321 /*template_p=*/NULL,
29322 /*declarator_p=*/false,
29323 /*optional_p=*/false);
29324 if (name == error_mark_node)
29325 goto skip_comma;
29327 decl = cp_parser_lookup_name_simple (parser, name, token->location);
29328 if (decl == error_mark_node)
29329 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
29330 token->location);
29332 if (decl == error_mark_node)
29334 else if (kind != 0)
29336 switch (kind)
29338 case OMP_CLAUSE__CACHE_:
29339 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
29341 error_at (token->location, "expected %<[%>");
29342 decl = error_mark_node;
29343 break;
29345 /* FALLTHROUGH. */
29346 case OMP_CLAUSE_MAP:
29347 case OMP_CLAUSE_FROM:
29348 case OMP_CLAUSE_TO:
29349 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
29351 location_t loc
29352 = cp_lexer_peek_token (parser->lexer)->location;
29353 cp_id_kind idk = CP_ID_KIND_NONE;
29354 cp_lexer_consume_token (parser->lexer);
29355 decl
29356 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
29357 decl, false,
29358 &idk, loc);
29360 /* FALLTHROUGH. */
29361 case OMP_CLAUSE_DEPEND:
29362 case OMP_CLAUSE_REDUCTION:
29363 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
29365 tree low_bound = NULL_TREE, length = NULL_TREE;
29367 parser->colon_corrects_to_scope_p = false;
29368 cp_lexer_consume_token (parser->lexer);
29369 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29370 low_bound = cp_parser_expression (parser);
29371 if (!colon)
29372 parser->colon_corrects_to_scope_p
29373 = saved_colon_corrects_to_scope_p;
29374 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
29375 length = integer_one_node;
29376 else
29378 /* Look for `:'. */
29379 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29380 goto skip_comma;
29381 if (!cp_lexer_next_token_is (parser->lexer,
29382 CPP_CLOSE_SQUARE))
29383 length = cp_parser_expression (parser);
29385 /* Look for the closing `]'. */
29386 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
29387 RT_CLOSE_SQUARE))
29388 goto skip_comma;
29390 if (kind == OMP_CLAUSE__CACHE_)
29392 if (TREE_CODE (low_bound) != INTEGER_CST
29393 && !TREE_READONLY (low_bound))
29395 error_at (token->location,
29396 "%qD is not a constant", low_bound);
29397 decl = error_mark_node;
29400 if (TREE_CODE (length) != INTEGER_CST
29401 && !TREE_READONLY (length))
29403 error_at (token->location,
29404 "%qD is not a constant", length);
29405 decl = error_mark_node;
29409 decl = tree_cons (low_bound, length, decl);
29411 break;
29412 default:
29413 break;
29416 tree u = build_omp_clause (token->location, kind);
29417 OMP_CLAUSE_DECL (u) = decl;
29418 OMP_CLAUSE_CHAIN (u) = list;
29419 list = u;
29421 else
29422 list = tree_cons (decl, NULL_TREE, list);
29424 get_comma:
29425 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29426 break;
29427 cp_lexer_consume_token (parser->lexer);
29430 if (colon)
29431 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
29433 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29435 *colon = true;
29436 cp_parser_require (parser, CPP_COLON, RT_COLON);
29437 return list;
29440 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29442 int ending;
29444 /* Try to resync to an unnested comma. Copied from
29445 cp_parser_parenthesized_expression_list. */
29446 skip_comma:
29447 if (colon)
29448 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
29449 ending = cp_parser_skip_to_closing_parenthesis (parser,
29450 /*recovering=*/true,
29451 /*or_comma=*/true,
29452 /*consume_paren=*/true);
29453 if (ending < 0)
29454 goto get_comma;
29457 return list;
29460 /* Similarly, but expect leading and trailing parenthesis. This is a very
29461 common case for omp clauses. */
29463 static tree
29464 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
29466 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29467 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
29468 return list;
29471 /* OpenACC 2.0:
29472 copy ( variable-list )
29473 copyin ( variable-list )
29474 copyout ( variable-list )
29475 create ( variable-list )
29476 delete ( variable-list )
29477 present ( variable-list )
29478 present_or_copy ( variable-list )
29479 pcopy ( variable-list )
29480 present_or_copyin ( variable-list )
29481 pcopyin ( variable-list )
29482 present_or_copyout ( variable-list )
29483 pcopyout ( variable-list )
29484 present_or_create ( variable-list )
29485 pcreate ( variable-list ) */
29487 static tree
29488 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
29489 tree list)
29491 enum gomp_map_kind kind;
29492 switch (c_kind)
29494 case PRAGMA_OACC_CLAUSE_COPY:
29495 kind = GOMP_MAP_FORCE_TOFROM;
29496 break;
29497 case PRAGMA_OACC_CLAUSE_COPYIN:
29498 kind = GOMP_MAP_FORCE_TO;
29499 break;
29500 case PRAGMA_OACC_CLAUSE_COPYOUT:
29501 kind = GOMP_MAP_FORCE_FROM;
29502 break;
29503 case PRAGMA_OACC_CLAUSE_CREATE:
29504 kind = GOMP_MAP_FORCE_ALLOC;
29505 break;
29506 case PRAGMA_OACC_CLAUSE_DELETE:
29507 kind = GOMP_MAP_FORCE_DEALLOC;
29508 break;
29509 case PRAGMA_OACC_CLAUSE_DEVICE:
29510 kind = GOMP_MAP_FORCE_TO;
29511 break;
29512 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
29513 kind = GOMP_MAP_DEVICE_RESIDENT;
29514 break;
29515 case PRAGMA_OACC_CLAUSE_HOST:
29516 case PRAGMA_OACC_CLAUSE_SELF:
29517 kind = GOMP_MAP_FORCE_FROM;
29518 break;
29519 case PRAGMA_OACC_CLAUSE_LINK:
29520 kind = GOMP_MAP_LINK;
29521 break;
29522 case PRAGMA_OACC_CLAUSE_PRESENT:
29523 kind = GOMP_MAP_FORCE_PRESENT;
29524 break;
29525 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29526 kind = GOMP_MAP_TOFROM;
29527 break;
29528 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29529 kind = GOMP_MAP_TO;
29530 break;
29531 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29532 kind = GOMP_MAP_FROM;
29533 break;
29534 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29535 kind = GOMP_MAP_ALLOC;
29536 break;
29537 default:
29538 gcc_unreachable ();
29540 tree nl, c;
29541 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
29543 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
29544 OMP_CLAUSE_SET_MAP_KIND (c, kind);
29546 return nl;
29549 /* OpenACC 2.0:
29550 deviceptr ( variable-list ) */
29552 static tree
29553 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
29555 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29556 tree vars, t;
29558 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
29559 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
29560 variable-list must only allow for pointer variables. */
29561 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29562 for (t = vars; t; t = TREE_CHAIN (t))
29564 tree v = TREE_PURPOSE (t);
29566 /* FIXME diagnostics: Ideally we should keep individual
29567 locations for all the variables in the var list to make the
29568 following errors more precise. Perhaps
29569 c_parser_omp_var_list_parens should construct a list of
29570 locations to go along with the var list. */
29572 if (!VAR_P (v))
29573 error_at (loc, "%qD is not a variable", v);
29574 else if (TREE_TYPE (v) == error_mark_node)
29576 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
29577 error_at (loc, "%qD is not a pointer variable", v);
29579 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
29580 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
29581 OMP_CLAUSE_DECL (u) = v;
29582 OMP_CLAUSE_CHAIN (u) = list;
29583 list = u;
29586 return list;
29589 /* OpenACC 2.0:
29590 auto
29591 independent
29592 nohost
29593 seq */
29595 static tree
29596 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
29597 enum omp_clause_code code,
29598 tree list, location_t location)
29600 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
29601 tree c = build_omp_clause (location, code);
29602 OMP_CLAUSE_CHAIN (c) = list;
29603 return c;
29606 /* OpenACC:
29607 num_gangs ( expression )
29608 num_workers ( expression )
29609 vector_length ( expression ) */
29611 static tree
29612 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
29613 const char *str, tree list)
29615 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29617 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29618 return list;
29620 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
29622 if (t == error_mark_node
29623 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29625 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29626 /*or_comma=*/false,
29627 /*consume_paren=*/true);
29628 return list;
29631 check_no_duplicate_clause (list, code, str, loc);
29633 tree c = build_omp_clause (loc, code);
29634 OMP_CLAUSE_OPERAND (c, 0) = t;
29635 OMP_CLAUSE_CHAIN (c) = list;
29636 return c;
29639 /* OpenACC:
29641 gang [( gang-arg-list )]
29642 worker [( [num:] int-expr )]
29643 vector [( [length:] int-expr )]
29645 where gang-arg is one of:
29647 [num:] int-expr
29648 static: size-expr
29650 and size-expr may be:
29653 int-expr
29656 static tree
29657 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
29658 const char *str, tree list)
29660 const char *id = "num";
29661 cp_lexer *lexer = parser->lexer;
29662 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
29663 location_t loc = cp_lexer_peek_token (lexer)->location;
29665 if (kind == OMP_CLAUSE_VECTOR)
29666 id = "length";
29668 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
29670 cp_lexer_consume_token (lexer);
29674 cp_token *next = cp_lexer_peek_token (lexer);
29675 int idx = 0;
29677 /* Gang static argument. */
29678 if (kind == OMP_CLAUSE_GANG
29679 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
29681 cp_lexer_consume_token (lexer);
29683 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29684 goto cleanup_error;
29686 idx = 1;
29687 if (ops[idx] != NULL)
29689 cp_parser_error (parser, "too many %<static%> arguments");
29690 goto cleanup_error;
29693 /* Check for the '*' argument. */
29694 if (cp_lexer_next_token_is (lexer, CPP_MULT)
29695 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
29696 || cp_lexer_nth_token_is (parser->lexer, 2,
29697 CPP_CLOSE_PAREN)))
29699 cp_lexer_consume_token (lexer);
29700 ops[idx] = integer_minus_one_node;
29702 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
29704 cp_lexer_consume_token (lexer);
29705 continue;
29707 else break;
29710 /* Worker num: argument and vector length: arguments. */
29711 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
29712 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
29713 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
29715 cp_lexer_consume_token (lexer); /* id */
29716 cp_lexer_consume_token (lexer); /* ':' */
29719 /* Now collect the actual argument. */
29720 if (ops[idx] != NULL_TREE)
29722 cp_parser_error (parser, "unexpected argument");
29723 goto cleanup_error;
29726 tree expr = cp_parser_assignment_expression (parser, NULL, false,
29727 false);
29728 if (expr == error_mark_node)
29729 goto cleanup_error;
29731 mark_exp_read (expr);
29732 ops[idx] = expr;
29734 if (kind == OMP_CLAUSE_GANG
29735 && cp_lexer_next_token_is (lexer, CPP_COMMA))
29737 cp_lexer_consume_token (lexer);
29738 continue;
29740 break;
29742 while (1);
29744 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29745 goto cleanup_error;
29748 check_no_duplicate_clause (list, kind, str, loc);
29750 c = build_omp_clause (loc, kind);
29752 if (ops[1])
29753 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
29755 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
29756 OMP_CLAUSE_CHAIN (c) = list;
29758 return c;
29760 cleanup_error:
29761 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
29762 return list;
29765 /* OpenACC 2.0:
29766 tile ( size-expr-list ) */
29768 static tree
29769 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
29771 tree c, expr = error_mark_node;
29772 tree tile = NULL_TREE;
29774 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
29776 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29777 return list;
29781 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
29782 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
29783 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
29785 cp_lexer_consume_token (parser->lexer);
29786 expr = integer_minus_one_node;
29788 else
29789 expr = cp_parser_assignment_expression (parser, NULL, false, false);
29791 if (expr == error_mark_node)
29792 return list;
29794 tile = tree_cons (NULL_TREE, expr, tile);
29796 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29797 cp_lexer_consume_token (parser->lexer);
29799 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
29801 /* Consume the trailing ')'. */
29802 cp_lexer_consume_token (parser->lexer);
29804 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
29805 tile = nreverse (tile);
29806 OMP_CLAUSE_TILE_LIST (c) = tile;
29807 OMP_CLAUSE_CHAIN (c) = list;
29808 return c;
29811 /* OpenACC 2.0
29812 Parse wait clause or directive parameters. */
29814 static tree
29815 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
29817 vec<tree, va_gc> *args;
29818 tree t, args_tree;
29820 args = cp_parser_parenthesized_expression_list (parser, non_attr,
29821 /*cast_p=*/false,
29822 /*allow_expansion_p=*/true,
29823 /*non_constant_p=*/NULL);
29825 if (args == NULL || args->length () == 0)
29827 cp_parser_error (parser, "expected integer expression before ')'");
29828 if (args != NULL)
29829 release_tree_vector (args);
29830 return list;
29833 args_tree = build_tree_list_vec (args);
29835 release_tree_vector (args);
29837 for (t = args_tree; t; t = TREE_CHAIN (t))
29839 tree targ = TREE_VALUE (t);
29841 if (targ != error_mark_node)
29843 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
29844 error ("%<wait%> expression must be integral");
29845 else
29847 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
29849 mark_rvalue_use (targ);
29850 OMP_CLAUSE_DECL (c) = targ;
29851 OMP_CLAUSE_CHAIN (c) = list;
29852 list = c;
29857 return list;
29860 /* OpenACC:
29861 wait ( int-expr-list ) */
29863 static tree
29864 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
29866 location_t location = cp_lexer_peek_token (parser->lexer)->location;
29868 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
29869 return list;
29871 list = cp_parser_oacc_wait_list (parser, location, list);
29873 return list;
29876 /* OpenMP 3.0:
29877 collapse ( constant-expression ) */
29879 static tree
29880 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
29882 tree c, num;
29883 location_t loc;
29884 HOST_WIDE_INT n;
29886 loc = cp_lexer_peek_token (parser->lexer)->location;
29887 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29888 return list;
29890 num = cp_parser_constant_expression (parser);
29892 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29893 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29894 /*or_comma=*/false,
29895 /*consume_paren=*/true);
29897 if (num == error_mark_node)
29898 return list;
29899 num = fold_non_dependent_expr (num);
29900 if (!tree_fits_shwi_p (num)
29901 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
29902 || (n = tree_to_shwi (num)) <= 0
29903 || (int) n != n)
29905 error_at (loc, "collapse argument needs positive constant integer expression");
29906 return list;
29909 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
29910 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
29911 OMP_CLAUSE_CHAIN (c) = list;
29912 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
29914 return c;
29917 /* OpenMP 2.5:
29918 default ( shared | none )
29920 OpenACC 2.0
29921 default (none) */
29923 static tree
29924 cp_parser_omp_clause_default (cp_parser *parser, tree list,
29925 location_t location, bool is_oacc)
29927 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
29928 tree c;
29930 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29931 return list;
29932 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29934 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29935 const char *p = IDENTIFIER_POINTER (id);
29937 switch (p[0])
29939 case 'n':
29940 if (strcmp ("none", p) != 0)
29941 goto invalid_kind;
29942 kind = OMP_CLAUSE_DEFAULT_NONE;
29943 break;
29945 case 's':
29946 if (strcmp ("shared", p) != 0 || is_oacc)
29947 goto invalid_kind;
29948 kind = OMP_CLAUSE_DEFAULT_SHARED;
29949 break;
29951 default:
29952 goto invalid_kind;
29955 cp_lexer_consume_token (parser->lexer);
29957 else
29959 invalid_kind:
29960 if (is_oacc)
29961 cp_parser_error (parser, "expected %<none%>");
29962 else
29963 cp_parser_error (parser, "expected %<none%> or %<shared%>");
29966 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29967 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29968 /*or_comma=*/false,
29969 /*consume_paren=*/true);
29971 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
29972 return list;
29974 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
29975 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
29976 OMP_CLAUSE_CHAIN (c) = list;
29977 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
29979 return c;
29982 /* OpenMP 3.1:
29983 final ( expression ) */
29985 static tree
29986 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
29988 tree t, c;
29990 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29991 return list;
29993 t = cp_parser_condition (parser);
29995 if (t == error_mark_node
29996 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29997 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29998 /*or_comma=*/false,
29999 /*consume_paren=*/true);
30001 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30003 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30004 OMP_CLAUSE_FINAL_EXPR (c) = t;
30005 OMP_CLAUSE_CHAIN (c) = list;
30007 return c;
30010 /* OpenMP 2.5:
30011 if ( expression )
30013 OpenMP 4.5:
30014 if ( directive-name-modifier : expression )
30016 directive-name-modifier:
30017 parallel | task | taskloop | target data | target | target update
30018 | target enter data | target exit data */
30020 static tree
30021 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30022 bool is_omp)
30024 tree t, c;
30025 enum tree_code if_modifier = ERROR_MARK;
30027 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30028 return list;
30030 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30032 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30033 const char *p = IDENTIFIER_POINTER (id);
30034 int n = 2;
30036 if (strcmp ("parallel", p) == 0)
30037 if_modifier = OMP_PARALLEL;
30038 else if (strcmp ("task", p) == 0)
30039 if_modifier = OMP_TASK;
30040 else if (strcmp ("taskloop", p) == 0)
30041 if_modifier = OMP_TASKLOOP;
30042 else if (strcmp ("target", p) == 0)
30044 if_modifier = OMP_TARGET;
30045 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30047 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30048 p = IDENTIFIER_POINTER (id);
30049 if (strcmp ("data", p) == 0)
30050 if_modifier = OMP_TARGET_DATA;
30051 else if (strcmp ("update", p) == 0)
30052 if_modifier = OMP_TARGET_UPDATE;
30053 else if (strcmp ("enter", p) == 0)
30054 if_modifier = OMP_TARGET_ENTER_DATA;
30055 else if (strcmp ("exit", p) == 0)
30056 if_modifier = OMP_TARGET_EXIT_DATA;
30057 if (if_modifier != OMP_TARGET)
30058 n = 3;
30059 else
30061 location_t loc
30062 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30063 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30064 "or %<exit%>");
30065 if_modifier = ERROR_MARK;
30067 if (if_modifier == OMP_TARGET_ENTER_DATA
30068 || if_modifier == OMP_TARGET_EXIT_DATA)
30070 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30072 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30073 p = IDENTIFIER_POINTER (id);
30074 if (strcmp ("data", p) == 0)
30075 n = 4;
30077 if (n != 4)
30079 location_t loc
30080 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30081 error_at (loc, "expected %<data%>");
30082 if_modifier = ERROR_MARK;
30087 if (if_modifier != ERROR_MARK)
30089 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30091 while (n-- > 0)
30092 cp_lexer_consume_token (parser->lexer);
30094 else
30096 if (n > 2)
30098 location_t loc
30099 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30100 error_at (loc, "expected %<:%>");
30102 if_modifier = ERROR_MARK;
30107 t = cp_parser_condition (parser);
30109 if (t == error_mark_node
30110 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30111 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30112 /*or_comma=*/false,
30113 /*consume_paren=*/true);
30115 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30116 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30118 if (if_modifier != ERROR_MARK
30119 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30121 const char *p = NULL;
30122 switch (if_modifier)
30124 case OMP_PARALLEL: p = "parallel"; break;
30125 case OMP_TASK: p = "task"; break;
30126 case OMP_TASKLOOP: p = "taskloop"; break;
30127 case OMP_TARGET_DATA: p = "target data"; break;
30128 case OMP_TARGET: p = "target"; break;
30129 case OMP_TARGET_UPDATE: p = "target update"; break;
30130 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30131 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30132 default: gcc_unreachable ();
30134 error_at (location, "too many %<if%> clauses with %qs modifier",
30136 return list;
30138 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30140 if (!is_omp)
30141 error_at (location, "too many %<if%> clauses");
30142 else
30143 error_at (location, "too many %<if%> clauses without modifier");
30144 return list;
30146 else if (if_modifier == ERROR_MARK
30147 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30149 error_at (location, "if any %<if%> clause has modifier, then all "
30150 "%<if%> clauses have to use modifier");
30151 return list;
30155 c = build_omp_clause (location, OMP_CLAUSE_IF);
30156 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30157 OMP_CLAUSE_IF_EXPR (c) = t;
30158 OMP_CLAUSE_CHAIN (c) = list;
30160 return c;
30163 /* OpenMP 3.1:
30164 mergeable */
30166 static tree
30167 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30168 tree list, location_t location)
30170 tree c;
30172 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30173 location);
30175 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30176 OMP_CLAUSE_CHAIN (c) = list;
30177 return c;
30180 /* OpenMP 2.5:
30181 nowait */
30183 static tree
30184 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30185 tree list, location_t location)
30187 tree c;
30189 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
30191 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
30192 OMP_CLAUSE_CHAIN (c) = list;
30193 return c;
30196 /* OpenMP 2.5:
30197 num_threads ( expression ) */
30199 static tree
30200 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
30201 location_t location)
30203 tree t, c;
30205 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30206 return list;
30208 t = cp_parser_expression (parser);
30210 if (t == error_mark_node
30211 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30212 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30213 /*or_comma=*/false,
30214 /*consume_paren=*/true);
30216 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
30217 "num_threads", location);
30219 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
30220 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
30221 OMP_CLAUSE_CHAIN (c) = list;
30223 return c;
30226 /* OpenMP 4.5:
30227 num_tasks ( expression ) */
30229 static tree
30230 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
30231 location_t location)
30233 tree t, c;
30235 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30236 return list;
30238 t = cp_parser_expression (parser);
30240 if (t == error_mark_node
30241 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30242 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30243 /*or_comma=*/false,
30244 /*consume_paren=*/true);
30246 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
30247 "num_tasks", location);
30249 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
30250 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
30251 OMP_CLAUSE_CHAIN (c) = list;
30253 return c;
30256 /* OpenMP 4.5:
30257 grainsize ( expression ) */
30259 static tree
30260 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
30261 location_t location)
30263 tree t, c;
30265 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30266 return list;
30268 t = cp_parser_expression (parser);
30270 if (t == error_mark_node
30271 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30272 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30273 /*or_comma=*/false,
30274 /*consume_paren=*/true);
30276 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
30277 "grainsize", location);
30279 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
30280 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
30281 OMP_CLAUSE_CHAIN (c) = list;
30283 return c;
30286 /* OpenMP 4.5:
30287 priority ( expression ) */
30289 static tree
30290 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
30291 location_t location)
30293 tree t, c;
30295 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30296 return list;
30298 t = cp_parser_expression (parser);
30300 if (t == error_mark_node
30301 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30302 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30303 /*or_comma=*/false,
30304 /*consume_paren=*/true);
30306 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
30307 "priority", location);
30309 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
30310 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
30311 OMP_CLAUSE_CHAIN (c) = list;
30313 return c;
30316 /* OpenMP 4.5:
30317 hint ( expression ) */
30319 static tree
30320 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
30321 location_t location)
30323 tree t, c;
30325 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30326 return list;
30328 t = cp_parser_expression (parser);
30330 if (t == error_mark_node
30331 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30332 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30333 /*or_comma=*/false,
30334 /*consume_paren=*/true);
30336 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
30338 c = build_omp_clause (location, OMP_CLAUSE_HINT);
30339 OMP_CLAUSE_HINT_EXPR (c) = t;
30340 OMP_CLAUSE_CHAIN (c) = list;
30342 return c;
30345 /* OpenMP 4.5:
30346 defaultmap ( tofrom : scalar ) */
30348 static tree
30349 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
30350 location_t location)
30352 tree c, id;
30353 const char *p;
30355 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30356 return list;
30358 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30360 cp_parser_error (parser, "expected %<tofrom%>");
30361 goto out_err;
30363 id = cp_lexer_peek_token (parser->lexer)->u.value;
30364 p = IDENTIFIER_POINTER (id);
30365 if (strcmp (p, "tofrom") != 0)
30367 cp_parser_error (parser, "expected %<tofrom%>");
30368 goto out_err;
30370 cp_lexer_consume_token (parser->lexer);
30371 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30372 goto out_err;
30374 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30376 cp_parser_error (parser, "expected %<scalar%>");
30377 goto out_err;
30379 id = cp_lexer_peek_token (parser->lexer)->u.value;
30380 p = IDENTIFIER_POINTER (id);
30381 if (strcmp (p, "scalar") != 0)
30383 cp_parser_error (parser, "expected %<scalar%>");
30384 goto out_err;
30386 cp_lexer_consume_token (parser->lexer);
30387 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30388 goto out_err;
30390 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
30391 location);
30393 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
30394 OMP_CLAUSE_CHAIN (c) = list;
30395 return c;
30397 out_err:
30398 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30399 /*or_comma=*/false,
30400 /*consume_paren=*/true);
30401 return list;
30404 /* OpenMP 2.5:
30405 ordered
30407 OpenMP 4.5:
30408 ordered ( constant-expression ) */
30410 static tree
30411 cp_parser_omp_clause_ordered (cp_parser *parser,
30412 tree list, location_t location)
30414 tree c, num = NULL_TREE;
30415 HOST_WIDE_INT n;
30417 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
30418 "ordered", location);
30420 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30422 cp_lexer_consume_token (parser->lexer);
30424 num = cp_parser_constant_expression (parser);
30426 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30427 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30428 /*or_comma=*/false,
30429 /*consume_paren=*/true);
30431 if (num == error_mark_node)
30432 return list;
30433 num = fold_non_dependent_expr (num);
30434 if (!tree_fits_shwi_p (num)
30435 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30436 || (n = tree_to_shwi (num)) <= 0
30437 || (int) n != n)
30439 error_at (location,
30440 "ordered argument needs positive constant integer "
30441 "expression");
30442 return list;
30446 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
30447 OMP_CLAUSE_ORDERED_EXPR (c) = num;
30448 OMP_CLAUSE_CHAIN (c) = list;
30449 return c;
30452 /* OpenMP 2.5:
30453 reduction ( reduction-operator : variable-list )
30455 reduction-operator:
30456 One of: + * - & ^ | && ||
30458 OpenMP 3.1:
30460 reduction-operator:
30461 One of: + * - & ^ | && || min max
30463 OpenMP 4.0:
30465 reduction-operator:
30466 One of: + * - & ^ | && ||
30467 id-expression */
30469 static tree
30470 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
30472 enum tree_code code = ERROR_MARK;
30473 tree nlist, c, id = NULL_TREE;
30475 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30476 return list;
30478 switch (cp_lexer_peek_token (parser->lexer)->type)
30480 case CPP_PLUS: code = PLUS_EXPR; break;
30481 case CPP_MULT: code = MULT_EXPR; break;
30482 case CPP_MINUS: code = MINUS_EXPR; break;
30483 case CPP_AND: code = BIT_AND_EXPR; break;
30484 case CPP_XOR: code = BIT_XOR_EXPR; break;
30485 case CPP_OR: code = BIT_IOR_EXPR; break;
30486 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
30487 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
30488 default: break;
30491 if (code != ERROR_MARK)
30492 cp_lexer_consume_token (parser->lexer);
30493 else
30495 bool saved_colon_corrects_to_scope_p;
30496 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30497 parser->colon_corrects_to_scope_p = false;
30498 id = cp_parser_id_expression (parser, /*template_p=*/false,
30499 /*check_dependency_p=*/true,
30500 /*template_p=*/NULL,
30501 /*declarator_p=*/false,
30502 /*optional_p=*/false);
30503 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30504 if (identifier_p (id))
30506 const char *p = IDENTIFIER_POINTER (id);
30508 if (strcmp (p, "min") == 0)
30509 code = MIN_EXPR;
30510 else if (strcmp (p, "max") == 0)
30511 code = MAX_EXPR;
30512 else if (id == ansi_opname (PLUS_EXPR))
30513 code = PLUS_EXPR;
30514 else if (id == ansi_opname (MULT_EXPR))
30515 code = MULT_EXPR;
30516 else if (id == ansi_opname (MINUS_EXPR))
30517 code = MINUS_EXPR;
30518 else if (id == ansi_opname (BIT_AND_EXPR))
30519 code = BIT_AND_EXPR;
30520 else if (id == ansi_opname (BIT_IOR_EXPR))
30521 code = BIT_IOR_EXPR;
30522 else if (id == ansi_opname (BIT_XOR_EXPR))
30523 code = BIT_XOR_EXPR;
30524 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
30525 code = TRUTH_ANDIF_EXPR;
30526 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
30527 code = TRUTH_ORIF_EXPR;
30528 id = omp_reduction_id (code, id, NULL_TREE);
30529 tree scope = parser->scope;
30530 if (scope)
30531 id = build_qualified_name (NULL_TREE, scope, id, false);
30532 parser->scope = NULL_TREE;
30533 parser->qualifying_scope = NULL_TREE;
30534 parser->object_scope = NULL_TREE;
30536 else
30538 error ("invalid reduction-identifier");
30539 resync_fail:
30540 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30541 /*or_comma=*/false,
30542 /*consume_paren=*/true);
30543 return list;
30547 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30548 goto resync_fail;
30550 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
30551 NULL);
30552 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
30554 OMP_CLAUSE_REDUCTION_CODE (c) = code;
30555 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
30558 return nlist;
30561 /* OpenMP 2.5:
30562 schedule ( schedule-kind )
30563 schedule ( schedule-kind , expression )
30565 schedule-kind:
30566 static | dynamic | guided | runtime | auto
30568 OpenMP 4.5:
30569 schedule ( schedule-modifier : schedule-kind )
30570 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
30572 schedule-modifier:
30573 simd
30574 monotonic
30575 nonmonotonic */
30577 static tree
30578 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
30580 tree c, t;
30581 int modifiers = 0, nmodifiers = 0;
30583 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30584 return list;
30586 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
30588 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30590 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30591 const char *p = IDENTIFIER_POINTER (id);
30592 if (strcmp ("simd", p) == 0)
30593 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
30594 else if (strcmp ("monotonic", p) == 0)
30595 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
30596 else if (strcmp ("nonmonotonic", p) == 0)
30597 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
30598 else
30599 break;
30600 cp_lexer_consume_token (parser->lexer);
30601 if (nmodifiers++ == 0
30602 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30603 cp_lexer_consume_token (parser->lexer);
30604 else
30606 cp_parser_require (parser, CPP_COLON, RT_COLON);
30607 break;
30611 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30613 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30614 const char *p = IDENTIFIER_POINTER (id);
30616 switch (p[0])
30618 case 'd':
30619 if (strcmp ("dynamic", p) != 0)
30620 goto invalid_kind;
30621 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
30622 break;
30624 case 'g':
30625 if (strcmp ("guided", p) != 0)
30626 goto invalid_kind;
30627 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
30628 break;
30630 case 'r':
30631 if (strcmp ("runtime", p) != 0)
30632 goto invalid_kind;
30633 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
30634 break;
30636 default:
30637 goto invalid_kind;
30640 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
30641 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
30642 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30643 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
30644 else
30645 goto invalid_kind;
30646 cp_lexer_consume_token (parser->lexer);
30648 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
30649 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
30650 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
30651 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
30653 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
30654 "specified");
30655 modifiers = 0;
30658 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30660 cp_token *token;
30661 cp_lexer_consume_token (parser->lexer);
30663 token = cp_lexer_peek_token (parser->lexer);
30664 t = cp_parser_assignment_expression (parser);
30666 if (t == error_mark_node)
30667 goto resync_fail;
30668 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
30669 error_at (token->location, "schedule %<runtime%> does not take "
30670 "a %<chunk_size%> parameter");
30671 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
30672 error_at (token->location, "schedule %<auto%> does not take "
30673 "a %<chunk_size%> parameter");
30674 else
30675 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
30677 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30678 goto resync_fail;
30680 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
30681 goto resync_fail;
30683 OMP_CLAUSE_SCHEDULE_KIND (c)
30684 = (enum omp_clause_schedule_kind)
30685 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
30687 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
30688 OMP_CLAUSE_CHAIN (c) = list;
30689 return c;
30691 invalid_kind:
30692 cp_parser_error (parser, "invalid schedule kind");
30693 resync_fail:
30694 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30695 /*or_comma=*/false,
30696 /*consume_paren=*/true);
30697 return list;
30700 /* OpenMP 3.0:
30701 untied */
30703 static tree
30704 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
30705 tree list, location_t location)
30707 tree c;
30709 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
30711 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
30712 OMP_CLAUSE_CHAIN (c) = list;
30713 return c;
30716 /* OpenMP 4.0:
30717 inbranch
30718 notinbranch */
30720 static tree
30721 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
30722 tree list, location_t location)
30724 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30725 tree c = build_omp_clause (location, code);
30726 OMP_CLAUSE_CHAIN (c) = list;
30727 return c;
30730 /* OpenMP 4.0:
30731 parallel
30733 sections
30734 taskgroup */
30736 static tree
30737 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
30738 enum omp_clause_code code,
30739 tree list, location_t location)
30741 tree c = build_omp_clause (location, code);
30742 OMP_CLAUSE_CHAIN (c) = list;
30743 return c;
30746 /* OpenMP 4.5:
30747 nogroup */
30749 static tree
30750 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
30751 tree list, location_t location)
30753 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
30754 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
30755 OMP_CLAUSE_CHAIN (c) = list;
30756 return c;
30759 /* OpenMP 4.5:
30760 simd
30761 threads */
30763 static tree
30764 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
30765 enum omp_clause_code code,
30766 tree list, location_t location)
30768 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30769 tree c = build_omp_clause (location, code);
30770 OMP_CLAUSE_CHAIN (c) = list;
30771 return c;
30774 /* OpenMP 4.0:
30775 num_teams ( expression ) */
30777 static tree
30778 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
30779 location_t location)
30781 tree t, c;
30783 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30784 return list;
30786 t = cp_parser_expression (parser);
30788 if (t == error_mark_node
30789 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30790 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30791 /*or_comma=*/false,
30792 /*consume_paren=*/true);
30794 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
30795 "num_teams", location);
30797 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
30798 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
30799 OMP_CLAUSE_CHAIN (c) = list;
30801 return c;
30804 /* OpenMP 4.0:
30805 thread_limit ( expression ) */
30807 static tree
30808 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
30809 location_t location)
30811 tree t, c;
30813 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30814 return list;
30816 t = cp_parser_expression (parser);
30818 if (t == error_mark_node
30819 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30820 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30821 /*or_comma=*/false,
30822 /*consume_paren=*/true);
30824 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
30825 "thread_limit", location);
30827 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
30828 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
30829 OMP_CLAUSE_CHAIN (c) = list;
30831 return c;
30834 /* OpenMP 4.0:
30835 aligned ( variable-list )
30836 aligned ( variable-list : constant-expression ) */
30838 static tree
30839 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
30841 tree nlist, c, alignment = NULL_TREE;
30842 bool colon;
30844 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30845 return list;
30847 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
30848 &colon);
30850 if (colon)
30852 alignment = cp_parser_constant_expression (parser);
30854 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30855 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30856 /*or_comma=*/false,
30857 /*consume_paren=*/true);
30859 if (alignment == error_mark_node)
30860 alignment = NULL_TREE;
30863 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
30864 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
30866 return nlist;
30869 /* OpenMP 4.0:
30870 linear ( variable-list )
30871 linear ( variable-list : expression )
30873 OpenMP 4.5:
30874 linear ( modifier ( variable-list ) )
30875 linear ( modifier ( variable-list ) : expression ) */
30877 static tree
30878 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
30879 bool is_cilk_simd_fn, bool declare_simd)
30881 tree nlist, c, step = integer_one_node;
30882 bool colon;
30883 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
30885 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30886 return list;
30888 if (!is_cilk_simd_fn
30889 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30891 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30892 const char *p = IDENTIFIER_POINTER (id);
30894 if (strcmp ("ref", p) == 0)
30895 kind = OMP_CLAUSE_LINEAR_REF;
30896 else if (strcmp ("val", p) == 0)
30897 kind = OMP_CLAUSE_LINEAR_VAL;
30898 else if (strcmp ("uval", p) == 0)
30899 kind = OMP_CLAUSE_LINEAR_UVAL;
30900 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
30901 cp_lexer_consume_token (parser->lexer);
30902 else
30903 kind = OMP_CLAUSE_LINEAR_DEFAULT;
30906 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
30907 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
30908 &colon);
30909 else
30911 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
30912 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
30913 if (colon)
30914 cp_parser_require (parser, CPP_COLON, RT_COLON);
30915 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30916 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30917 /*or_comma=*/false,
30918 /*consume_paren=*/true);
30921 if (colon)
30923 step = NULL_TREE;
30924 if (declare_simd
30925 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
30926 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
30928 cp_token *token = cp_lexer_peek_token (parser->lexer);
30929 cp_parser_parse_tentatively (parser);
30930 step = cp_parser_id_expression (parser, /*template_p=*/false,
30931 /*check_dependency_p=*/true,
30932 /*template_p=*/NULL,
30933 /*declarator_p=*/false,
30934 /*optional_p=*/false);
30935 if (step != error_mark_node)
30936 step = cp_parser_lookup_name_simple (parser, step, token->location);
30937 if (step == error_mark_node)
30939 step = NULL_TREE;
30940 cp_parser_abort_tentative_parse (parser);
30942 else if (!cp_parser_parse_definitely (parser))
30943 step = NULL_TREE;
30945 if (!step)
30946 step = cp_parser_expression (parser);
30948 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
30950 sorry ("using parameters for %<linear%> step is not supported yet");
30951 step = integer_one_node;
30953 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30954 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30955 /*or_comma=*/false,
30956 /*consume_paren=*/true);
30958 if (step == error_mark_node)
30959 return list;
30962 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
30964 OMP_CLAUSE_LINEAR_STEP (c) = step;
30965 OMP_CLAUSE_LINEAR_KIND (c) = kind;
30968 return nlist;
30971 /* OpenMP 4.0:
30972 safelen ( constant-expression ) */
30974 static tree
30975 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
30976 location_t location)
30978 tree t, c;
30980 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30981 return list;
30983 t = cp_parser_constant_expression (parser);
30985 if (t == error_mark_node
30986 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30987 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30988 /*or_comma=*/false,
30989 /*consume_paren=*/true);
30991 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
30993 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
30994 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
30995 OMP_CLAUSE_CHAIN (c) = list;
30997 return c;
31000 /* OpenMP 4.0:
31001 simdlen ( constant-expression ) */
31003 static tree
31004 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31005 location_t location)
31007 tree t, c;
31009 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31010 return list;
31012 t = cp_parser_constant_expression (parser);
31014 if (t == error_mark_node
31015 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31016 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31017 /*or_comma=*/false,
31018 /*consume_paren=*/true);
31020 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31022 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31023 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31024 OMP_CLAUSE_CHAIN (c) = list;
31026 return c;
31029 /* OpenMP 4.5:
31030 vec:
31031 identifier [+/- integer]
31032 vec , identifier [+/- integer]
31035 static tree
31036 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31037 tree list)
31039 tree vec = NULL;
31041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31043 cp_parser_error (parser, "expected identifier");
31044 return list;
31047 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31049 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31050 tree t, identifier = cp_parser_identifier (parser);
31051 tree addend = NULL;
31053 if (identifier == error_mark_node)
31054 t = error_mark_node;
31055 else
31057 t = cp_parser_lookup_name_simple
31058 (parser, identifier,
31059 cp_lexer_peek_token (parser->lexer)->location);
31060 if (t == error_mark_node)
31061 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31062 id_loc);
31065 bool neg = false;
31066 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31067 neg = true;
31068 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31070 addend = integer_zero_node;
31071 goto add_to_vector;
31073 cp_lexer_consume_token (parser->lexer);
31075 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31077 cp_parser_error (parser, "expected integer");
31078 return list;
31081 addend = cp_lexer_peek_token (parser->lexer)->u.value;
31082 if (TREE_CODE (addend) != INTEGER_CST)
31084 cp_parser_error (parser, "expected integer");
31085 return list;
31087 cp_lexer_consume_token (parser->lexer);
31089 add_to_vector:
31090 if (t != error_mark_node)
31092 vec = tree_cons (addend, t, vec);
31093 if (neg)
31094 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31098 break;
31100 cp_lexer_consume_token (parser->lexer);
31103 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31105 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31106 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31107 OMP_CLAUSE_DECL (u) = nreverse (vec);
31108 OMP_CLAUSE_CHAIN (u) = list;
31109 return u;
31111 return list;
31114 /* OpenMP 4.0:
31115 depend ( depend-kind : variable-list )
31117 depend-kind:
31118 in | out | inout
31120 OpenMP 4.5:
31121 depend ( source )
31123 depend ( sink : vec ) */
31125 static tree
31126 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31128 tree nlist, c;
31129 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31131 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31132 return list;
31134 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31136 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31137 const char *p = IDENTIFIER_POINTER (id);
31139 if (strcmp ("in", p) == 0)
31140 kind = OMP_CLAUSE_DEPEND_IN;
31141 else if (strcmp ("inout", p) == 0)
31142 kind = OMP_CLAUSE_DEPEND_INOUT;
31143 else if (strcmp ("out", p) == 0)
31144 kind = OMP_CLAUSE_DEPEND_OUT;
31145 else if (strcmp ("source", p) == 0)
31146 kind = OMP_CLAUSE_DEPEND_SOURCE;
31147 else if (strcmp ("sink", p) == 0)
31148 kind = OMP_CLAUSE_DEPEND_SINK;
31149 else
31150 goto invalid_kind;
31152 else
31153 goto invalid_kind;
31155 cp_lexer_consume_token (parser->lexer);
31157 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31159 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31160 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31161 OMP_CLAUSE_DECL (c) = NULL_TREE;
31162 OMP_CLAUSE_CHAIN (c) = list;
31163 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31164 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31165 /*or_comma=*/false,
31166 /*consume_paren=*/true);
31167 return c;
31170 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31171 goto resync_fail;
31173 if (kind == OMP_CLAUSE_DEPEND_SINK)
31174 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31175 else
31177 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31178 list, NULL);
31180 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31181 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31183 return nlist;
31185 invalid_kind:
31186 cp_parser_error (parser, "invalid depend kind");
31187 resync_fail:
31188 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31189 /*or_comma=*/false,
31190 /*consume_paren=*/true);
31191 return list;
31194 /* OpenMP 4.0:
31195 map ( map-kind : variable-list )
31196 map ( variable-list )
31198 map-kind:
31199 alloc | to | from | tofrom
31201 OpenMP 4.5:
31202 map-kind:
31203 alloc | to | from | tofrom | release | delete
31205 map ( always [,] map-kind: variable-list ) */
31207 static tree
31208 cp_parser_omp_clause_map (cp_parser *parser, tree list)
31210 tree nlist, c;
31211 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
31212 bool always = false;
31214 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31215 return list;
31217 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31219 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31220 const char *p = IDENTIFIER_POINTER (id);
31222 if (strcmp ("always", p) == 0)
31224 int nth = 2;
31225 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
31226 nth++;
31227 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
31228 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
31229 == RID_DELETE))
31230 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
31231 == CPP_COLON))
31233 always = true;
31234 cp_lexer_consume_token (parser->lexer);
31235 if (nth == 3)
31236 cp_lexer_consume_token (parser->lexer);
31241 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31242 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31244 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31245 const char *p = IDENTIFIER_POINTER (id);
31247 if (strcmp ("alloc", p) == 0)
31248 kind = GOMP_MAP_ALLOC;
31249 else if (strcmp ("to", p) == 0)
31250 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
31251 else if (strcmp ("from", p) == 0)
31252 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
31253 else if (strcmp ("tofrom", p) == 0)
31254 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
31255 else if (strcmp ("release", p) == 0)
31256 kind = GOMP_MAP_RELEASE;
31257 else
31259 cp_parser_error (parser, "invalid map kind");
31260 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31261 /*or_comma=*/false,
31262 /*consume_paren=*/true);
31263 return list;
31265 cp_lexer_consume_token (parser->lexer);
31266 cp_lexer_consume_token (parser->lexer);
31268 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
31269 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31271 kind = GOMP_MAP_DELETE;
31272 cp_lexer_consume_token (parser->lexer);
31273 cp_lexer_consume_token (parser->lexer);
31276 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
31277 NULL);
31279 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31280 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31282 return nlist;
31285 /* OpenMP 4.0:
31286 device ( expression ) */
31288 static tree
31289 cp_parser_omp_clause_device (cp_parser *parser, tree list,
31290 location_t location)
31292 tree t, c;
31294 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31295 return list;
31297 t = cp_parser_expression (parser);
31299 if (t == error_mark_node
31300 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31301 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31302 /*or_comma=*/false,
31303 /*consume_paren=*/true);
31305 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
31306 "device", location);
31308 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
31309 OMP_CLAUSE_DEVICE_ID (c) = t;
31310 OMP_CLAUSE_CHAIN (c) = list;
31312 return c;
31315 /* OpenMP 4.0:
31316 dist_schedule ( static )
31317 dist_schedule ( static , expression ) */
31319 static tree
31320 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
31321 location_t location)
31323 tree c, t;
31325 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31326 return list;
31328 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
31330 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31331 goto invalid_kind;
31332 cp_lexer_consume_token (parser->lexer);
31334 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31336 cp_lexer_consume_token (parser->lexer);
31338 t = cp_parser_assignment_expression (parser);
31340 if (t == error_mark_node)
31341 goto resync_fail;
31342 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
31344 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31345 goto resync_fail;
31347 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31348 goto resync_fail;
31350 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
31351 location);
31352 OMP_CLAUSE_CHAIN (c) = list;
31353 return c;
31355 invalid_kind:
31356 cp_parser_error (parser, "invalid dist_schedule kind");
31357 resync_fail:
31358 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31359 /*or_comma=*/false,
31360 /*consume_paren=*/true);
31361 return list;
31364 /* OpenMP 4.0:
31365 proc_bind ( proc-bind-kind )
31367 proc-bind-kind:
31368 master | close | spread */
31370 static tree
31371 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
31372 location_t location)
31374 tree c;
31375 enum omp_clause_proc_bind_kind kind;
31377 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31378 return list;
31380 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31382 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31383 const char *p = IDENTIFIER_POINTER (id);
31385 if (strcmp ("master", p) == 0)
31386 kind = OMP_CLAUSE_PROC_BIND_MASTER;
31387 else if (strcmp ("close", p) == 0)
31388 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
31389 else if (strcmp ("spread", p) == 0)
31390 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
31391 else
31392 goto invalid_kind;
31394 else
31395 goto invalid_kind;
31397 cp_lexer_consume_token (parser->lexer);
31398 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31399 goto resync_fail;
31401 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
31402 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
31403 location);
31404 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
31405 OMP_CLAUSE_CHAIN (c) = list;
31406 return c;
31408 invalid_kind:
31409 cp_parser_error (parser, "invalid depend kind");
31410 resync_fail:
31411 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31412 /*or_comma=*/false,
31413 /*consume_paren=*/true);
31414 return list;
31417 /* OpenACC:
31418 async [( int-expr )] */
31420 static tree
31421 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
31423 tree c, t;
31424 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31426 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
31428 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31430 cp_lexer_consume_token (parser->lexer);
31432 t = cp_parser_expression (parser);
31433 if (t == error_mark_node
31434 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31435 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31436 /*or_comma=*/false,
31437 /*consume_paren=*/true);
31440 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
31442 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
31443 OMP_CLAUSE_ASYNC_EXPR (c) = t;
31444 OMP_CLAUSE_CHAIN (c) = list;
31445 list = c;
31447 return list;
31450 /* Parse all OpenACC clauses. The set clauses allowed by the directive
31451 is a bitmask in MASK. Return the list of clauses found. */
31453 static tree
31454 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
31455 const char *where, cp_token *pragma_tok,
31456 bool finish_p = true)
31458 tree clauses = NULL;
31459 bool first = true;
31461 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31463 location_t here;
31464 pragma_omp_clause c_kind;
31465 omp_clause_code code;
31466 const char *c_name;
31467 tree prev = clauses;
31469 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31470 cp_lexer_consume_token (parser->lexer);
31472 here = cp_lexer_peek_token (parser->lexer)->location;
31473 c_kind = cp_parser_omp_clause_name (parser);
31475 switch (c_kind)
31477 case PRAGMA_OACC_CLAUSE_ASYNC:
31478 clauses = cp_parser_oacc_clause_async (parser, clauses);
31479 c_name = "async";
31480 break;
31481 case PRAGMA_OACC_CLAUSE_AUTO:
31482 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
31483 clauses, here);
31484 c_name = "auto";
31485 break;
31486 case PRAGMA_OACC_CLAUSE_COLLAPSE:
31487 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
31488 c_name = "collapse";
31489 break;
31490 case PRAGMA_OACC_CLAUSE_COPY:
31491 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31492 c_name = "copy";
31493 break;
31494 case PRAGMA_OACC_CLAUSE_COPYIN:
31495 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31496 c_name = "copyin";
31497 break;
31498 case PRAGMA_OACC_CLAUSE_COPYOUT:
31499 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31500 c_name = "copyout";
31501 break;
31502 case PRAGMA_OACC_CLAUSE_CREATE:
31503 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31504 c_name = "create";
31505 break;
31506 case PRAGMA_OACC_CLAUSE_DELETE:
31507 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31508 c_name = "delete";
31509 break;
31510 case PRAGMA_OMP_CLAUSE_DEFAULT:
31511 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
31512 c_name = "default";
31513 break;
31514 case PRAGMA_OACC_CLAUSE_DEVICE:
31515 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31516 c_name = "device";
31517 break;
31518 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
31519 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
31520 c_name = "deviceptr";
31521 break;
31522 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31523 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31524 c_name = "device_resident";
31525 break;
31526 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
31527 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
31528 clauses);
31529 c_name = "firstprivate";
31530 break;
31531 case PRAGMA_OACC_CLAUSE_GANG:
31532 c_name = "gang";
31533 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
31534 c_name, clauses);
31535 break;
31536 case PRAGMA_OACC_CLAUSE_HOST:
31537 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31538 c_name = "host";
31539 break;
31540 case PRAGMA_OACC_CLAUSE_IF:
31541 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
31542 c_name = "if";
31543 break;
31544 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
31545 clauses = cp_parser_oacc_simple_clause (parser,
31546 OMP_CLAUSE_INDEPENDENT,
31547 clauses, here);
31548 c_name = "independent";
31549 break;
31550 case PRAGMA_OACC_CLAUSE_LINK:
31551 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31552 c_name = "link";
31553 break;
31554 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
31555 code = OMP_CLAUSE_NUM_GANGS;
31556 c_name = "num_gangs";
31557 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
31558 clauses);
31559 break;
31560 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
31561 c_name = "num_workers";
31562 code = OMP_CLAUSE_NUM_WORKERS;
31563 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
31564 clauses);
31565 break;
31566 case PRAGMA_OACC_CLAUSE_PRESENT:
31567 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31568 c_name = "present";
31569 break;
31570 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31571 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31572 c_name = "present_or_copy";
31573 break;
31574 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31575 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31576 c_name = "present_or_copyin";
31577 break;
31578 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31579 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31580 c_name = "present_or_copyout";
31581 break;
31582 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31583 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31584 c_name = "present_or_create";
31585 break;
31586 case PRAGMA_OACC_CLAUSE_PRIVATE:
31587 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
31588 clauses);
31589 c_name = "private";
31590 break;
31591 case PRAGMA_OACC_CLAUSE_REDUCTION:
31592 clauses = cp_parser_omp_clause_reduction (parser, clauses);
31593 c_name = "reduction";
31594 break;
31595 case PRAGMA_OACC_CLAUSE_SELF:
31596 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31597 c_name = "self";
31598 break;
31599 case PRAGMA_OACC_CLAUSE_SEQ:
31600 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
31601 clauses, here);
31602 c_name = "seq";
31603 break;
31604 case PRAGMA_OACC_CLAUSE_TILE:
31605 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
31606 c_name = "tile";
31607 break;
31608 case PRAGMA_OACC_CLAUSE_VECTOR:
31609 c_name = "vector";
31610 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
31611 c_name, clauses);
31612 break;
31613 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
31614 c_name = "vector_length";
31615 code = OMP_CLAUSE_VECTOR_LENGTH;
31616 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
31617 clauses);
31618 break;
31619 case PRAGMA_OACC_CLAUSE_WAIT:
31620 clauses = cp_parser_oacc_clause_wait (parser, clauses);
31621 c_name = "wait";
31622 break;
31623 case PRAGMA_OACC_CLAUSE_WORKER:
31624 c_name = "worker";
31625 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
31626 c_name, clauses);
31627 break;
31628 default:
31629 cp_parser_error (parser, "expected %<#pragma acc%> clause");
31630 goto saw_error;
31633 first = false;
31635 if (((mask >> c_kind) & 1) == 0)
31637 /* Remove the invalid clause(s) from the list to avoid
31638 confusing the rest of the compiler. */
31639 clauses = prev;
31640 error_at (here, "%qs is not valid for %qs", c_name, where);
31644 saw_error:
31645 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31647 if (finish_p)
31648 return finish_omp_clauses (clauses, false);
31650 return clauses;
31653 /* Parse all OpenMP clauses. The set clauses allowed by the directive
31654 is a bitmask in MASK. Return the list of clauses found; the result
31655 of clause default goes in *pdefault. */
31657 static tree
31658 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
31659 const char *where, cp_token *pragma_tok,
31660 bool finish_p = true)
31662 tree clauses = NULL;
31663 bool first = true;
31664 cp_token *token = NULL;
31666 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31668 pragma_omp_clause c_kind;
31669 const char *c_name;
31670 tree prev = clauses;
31672 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31673 cp_lexer_consume_token (parser->lexer);
31675 token = cp_lexer_peek_token (parser->lexer);
31676 c_kind = cp_parser_omp_clause_name (parser);
31678 switch (c_kind)
31680 case PRAGMA_OMP_CLAUSE_COLLAPSE:
31681 clauses = cp_parser_omp_clause_collapse (parser, clauses,
31682 token->location);
31683 c_name = "collapse";
31684 break;
31685 case PRAGMA_OMP_CLAUSE_COPYIN:
31686 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
31687 c_name = "copyin";
31688 break;
31689 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
31690 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
31691 clauses);
31692 c_name = "copyprivate";
31693 break;
31694 case PRAGMA_OMP_CLAUSE_DEFAULT:
31695 clauses = cp_parser_omp_clause_default (parser, clauses,
31696 token->location, false);
31697 c_name = "default";
31698 break;
31699 case PRAGMA_OMP_CLAUSE_FINAL:
31700 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
31701 c_name = "final";
31702 break;
31703 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
31704 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
31705 clauses);
31706 c_name = "firstprivate";
31707 break;
31708 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
31709 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
31710 token->location);
31711 c_name = "grainsize";
31712 break;
31713 case PRAGMA_OMP_CLAUSE_HINT:
31714 clauses = cp_parser_omp_clause_hint (parser, clauses,
31715 token->location);
31716 c_name = "hint";
31717 break;
31718 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
31719 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
31720 token->location);
31721 c_name = "defaultmap";
31722 break;
31723 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
31724 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
31725 clauses);
31726 c_name = "use_device_ptr";
31727 break;
31728 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
31729 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
31730 clauses);
31731 c_name = "is_device_ptr";
31732 break;
31733 case PRAGMA_OMP_CLAUSE_IF:
31734 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
31735 true);
31736 c_name = "if";
31737 break;
31738 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
31739 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
31740 clauses);
31741 c_name = "lastprivate";
31742 break;
31743 case PRAGMA_OMP_CLAUSE_MERGEABLE:
31744 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
31745 token->location);
31746 c_name = "mergeable";
31747 break;
31748 case PRAGMA_OMP_CLAUSE_NOWAIT:
31749 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
31750 c_name = "nowait";
31751 break;
31752 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
31753 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
31754 token->location);
31755 c_name = "num_tasks";
31756 break;
31757 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
31758 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
31759 token->location);
31760 c_name = "num_threads";
31761 break;
31762 case PRAGMA_OMP_CLAUSE_ORDERED:
31763 clauses = cp_parser_omp_clause_ordered (parser, clauses,
31764 token->location);
31765 c_name = "ordered";
31766 break;
31767 case PRAGMA_OMP_CLAUSE_PRIORITY:
31768 clauses = cp_parser_omp_clause_priority (parser, clauses,
31769 token->location);
31770 c_name = "priority";
31771 break;
31772 case PRAGMA_OMP_CLAUSE_PRIVATE:
31773 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
31774 clauses);
31775 c_name = "private";
31776 break;
31777 case PRAGMA_OMP_CLAUSE_REDUCTION:
31778 clauses = cp_parser_omp_clause_reduction (parser, clauses);
31779 c_name = "reduction";
31780 break;
31781 case PRAGMA_OMP_CLAUSE_SCHEDULE:
31782 clauses = cp_parser_omp_clause_schedule (parser, clauses,
31783 token->location);
31784 c_name = "schedule";
31785 break;
31786 case PRAGMA_OMP_CLAUSE_SHARED:
31787 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
31788 clauses);
31789 c_name = "shared";
31790 break;
31791 case PRAGMA_OMP_CLAUSE_UNTIED:
31792 clauses = cp_parser_omp_clause_untied (parser, clauses,
31793 token->location);
31794 c_name = "untied";
31795 break;
31796 case PRAGMA_OMP_CLAUSE_INBRANCH:
31797 case PRAGMA_CILK_CLAUSE_MASK:
31798 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
31799 clauses, token->location);
31800 c_name = "inbranch";
31801 break;
31802 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
31803 case PRAGMA_CILK_CLAUSE_NOMASK:
31804 clauses = cp_parser_omp_clause_branch (parser,
31805 OMP_CLAUSE_NOTINBRANCH,
31806 clauses, token->location);
31807 c_name = "notinbranch";
31808 break;
31809 case PRAGMA_OMP_CLAUSE_PARALLEL:
31810 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
31811 clauses, token->location);
31812 c_name = "parallel";
31813 if (!first)
31815 clause_not_first:
31816 error_at (token->location, "%qs must be the first clause of %qs",
31817 c_name, where);
31818 clauses = prev;
31820 break;
31821 case PRAGMA_OMP_CLAUSE_FOR:
31822 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
31823 clauses, token->location);
31824 c_name = "for";
31825 if (!first)
31826 goto clause_not_first;
31827 break;
31828 case PRAGMA_OMP_CLAUSE_SECTIONS:
31829 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
31830 clauses, token->location);
31831 c_name = "sections";
31832 if (!first)
31833 goto clause_not_first;
31834 break;
31835 case PRAGMA_OMP_CLAUSE_TASKGROUP:
31836 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
31837 clauses, token->location);
31838 c_name = "taskgroup";
31839 if (!first)
31840 goto clause_not_first;
31841 break;
31842 case PRAGMA_OMP_CLAUSE_LINK:
31843 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
31844 c_name = "to";
31845 break;
31846 case PRAGMA_OMP_CLAUSE_TO:
31847 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
31848 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
31849 clauses);
31850 else
31851 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
31852 c_name = "to";
31853 break;
31854 case PRAGMA_OMP_CLAUSE_FROM:
31855 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
31856 c_name = "from";
31857 break;
31858 case PRAGMA_OMP_CLAUSE_UNIFORM:
31859 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
31860 clauses);
31861 c_name = "uniform";
31862 break;
31863 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
31864 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
31865 token->location);
31866 c_name = "num_teams";
31867 break;
31868 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
31869 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
31870 token->location);
31871 c_name = "thread_limit";
31872 break;
31873 case PRAGMA_OMP_CLAUSE_ALIGNED:
31874 clauses = cp_parser_omp_clause_aligned (parser, clauses);
31875 c_name = "aligned";
31876 break;
31877 case PRAGMA_OMP_CLAUSE_LINEAR:
31879 bool cilk_simd_fn = false, declare_simd = false;
31880 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
31881 cilk_simd_fn = true;
31882 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
31883 declare_simd = true;
31884 clauses = cp_parser_omp_clause_linear (parser, clauses,
31885 cilk_simd_fn, declare_simd);
31887 c_name = "linear";
31888 break;
31889 case PRAGMA_OMP_CLAUSE_DEPEND:
31890 clauses = cp_parser_omp_clause_depend (parser, clauses,
31891 token->location);
31892 c_name = "depend";
31893 break;
31894 case PRAGMA_OMP_CLAUSE_MAP:
31895 clauses = cp_parser_omp_clause_map (parser, clauses);
31896 c_name = "map";
31897 break;
31898 case PRAGMA_OMP_CLAUSE_DEVICE:
31899 clauses = cp_parser_omp_clause_device (parser, clauses,
31900 token->location);
31901 c_name = "device";
31902 break;
31903 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
31904 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
31905 token->location);
31906 c_name = "dist_schedule";
31907 break;
31908 case PRAGMA_OMP_CLAUSE_PROC_BIND:
31909 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
31910 token->location);
31911 c_name = "proc_bind";
31912 break;
31913 case PRAGMA_OMP_CLAUSE_SAFELEN:
31914 clauses = cp_parser_omp_clause_safelen (parser, clauses,
31915 token->location);
31916 c_name = "safelen";
31917 break;
31918 case PRAGMA_OMP_CLAUSE_SIMDLEN:
31919 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
31920 token->location);
31921 c_name = "simdlen";
31922 break;
31923 case PRAGMA_OMP_CLAUSE_NOGROUP:
31924 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
31925 token->location);
31926 c_name = "nogroup";
31927 break;
31928 case PRAGMA_OMP_CLAUSE_THREADS:
31929 clauses
31930 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
31931 clauses, token->location);
31932 c_name = "threads";
31933 break;
31934 case PRAGMA_OMP_CLAUSE_SIMD:
31935 clauses
31936 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
31937 clauses, token->location);
31938 c_name = "simd";
31939 break;
31940 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
31941 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
31942 c_name = "simdlen";
31943 break;
31944 default:
31945 cp_parser_error (parser, "expected %<#pragma omp%> clause");
31946 goto saw_error;
31949 first = false;
31951 if (((mask >> c_kind) & 1) == 0)
31953 /* Remove the invalid clause(s) from the list to avoid
31954 confusing the rest of the compiler. */
31955 clauses = prev;
31956 error_at (token->location, "%qs is not valid for %qs", c_name, where);
31959 saw_error:
31960 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
31961 no reason to skip to the end. */
31962 if (!(flag_cilkplus && pragma_tok == NULL))
31963 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31964 if (finish_p)
31966 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
31967 return finish_omp_clauses (clauses, false, true);
31968 else
31969 return finish_omp_clauses (clauses, true);
31971 return clauses;
31974 /* OpenMP 2.5:
31975 structured-block:
31976 statement
31978 In practice, we're also interested in adding the statement to an
31979 outer node. So it is convenient if we work around the fact that
31980 cp_parser_statement calls add_stmt. */
31982 static unsigned
31983 cp_parser_begin_omp_structured_block (cp_parser *parser)
31985 unsigned save = parser->in_statement;
31987 /* Only move the values to IN_OMP_BLOCK if they weren't false.
31988 This preserves the "not within loop or switch" style error messages
31989 for nonsense cases like
31990 void foo() {
31991 #pragma omp single
31992 break;
31995 if (parser->in_statement)
31996 parser->in_statement = IN_OMP_BLOCK;
31998 return save;
32001 static void
32002 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32004 parser->in_statement = save;
32007 static tree
32008 cp_parser_omp_structured_block (cp_parser *parser)
32010 tree stmt = begin_omp_structured_block ();
32011 unsigned int save = cp_parser_begin_omp_structured_block (parser);
32013 cp_parser_statement (parser, NULL_TREE, false, NULL);
32015 cp_parser_end_omp_structured_block (parser, save);
32016 return finish_omp_structured_block (stmt);
32019 /* OpenMP 2.5:
32020 # pragma omp atomic new-line
32021 expression-stmt
32023 expression-stmt:
32024 x binop= expr | x++ | ++x | x-- | --x
32025 binop:
32026 +, *, -, /, &, ^, |, <<, >>
32028 where x is an lvalue expression with scalar type.
32030 OpenMP 3.1:
32031 # pragma omp atomic new-line
32032 update-stmt
32034 # pragma omp atomic read new-line
32035 read-stmt
32037 # pragma omp atomic write new-line
32038 write-stmt
32040 # pragma omp atomic update new-line
32041 update-stmt
32043 # pragma omp atomic capture new-line
32044 capture-stmt
32046 # pragma omp atomic capture new-line
32047 capture-block
32049 read-stmt:
32050 v = x
32051 write-stmt:
32052 x = expr
32053 update-stmt:
32054 expression-stmt | x = x binop expr
32055 capture-stmt:
32056 v = expression-stmt
32057 capture-block:
32058 { v = x; update-stmt; } | { update-stmt; v = x; }
32060 OpenMP 4.0:
32061 update-stmt:
32062 expression-stmt | x = x binop expr | x = expr binop x
32063 capture-stmt:
32064 v = update-stmt
32065 capture-block:
32066 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32068 where x and v are lvalue expressions with scalar type. */
32070 static void
32071 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32073 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32074 tree rhs1 = NULL_TREE, orig_lhs;
32075 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32076 bool structured_block = false;
32077 bool seq_cst = false;
32079 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32081 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32082 const char *p = IDENTIFIER_POINTER (id);
32084 if (!strcmp (p, "seq_cst"))
32086 seq_cst = true;
32087 cp_lexer_consume_token (parser->lexer);
32088 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32089 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32090 cp_lexer_consume_token (parser->lexer);
32093 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32095 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32096 const char *p = IDENTIFIER_POINTER (id);
32098 if (!strcmp (p, "read"))
32099 code = OMP_ATOMIC_READ;
32100 else if (!strcmp (p, "write"))
32101 code = NOP_EXPR;
32102 else if (!strcmp (p, "update"))
32103 code = OMP_ATOMIC;
32104 else if (!strcmp (p, "capture"))
32105 code = OMP_ATOMIC_CAPTURE_NEW;
32106 else
32107 p = NULL;
32108 if (p)
32109 cp_lexer_consume_token (parser->lexer);
32111 if (!seq_cst)
32113 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32114 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32115 cp_lexer_consume_token (parser->lexer);
32117 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32119 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32120 const char *p = IDENTIFIER_POINTER (id);
32122 if (!strcmp (p, "seq_cst"))
32124 seq_cst = true;
32125 cp_lexer_consume_token (parser->lexer);
32129 cp_parser_require_pragma_eol (parser, pragma_tok);
32131 switch (code)
32133 case OMP_ATOMIC_READ:
32134 case NOP_EXPR: /* atomic write */
32135 v = cp_parser_unary_expression (parser);
32136 if (v == error_mark_node)
32137 goto saw_error;
32138 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32139 goto saw_error;
32140 if (code == NOP_EXPR)
32141 lhs = cp_parser_expression (parser);
32142 else
32143 lhs = cp_parser_unary_expression (parser);
32144 if (lhs == error_mark_node)
32145 goto saw_error;
32146 if (code == NOP_EXPR)
32148 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32149 opcode. */
32150 code = OMP_ATOMIC;
32151 rhs = lhs;
32152 lhs = v;
32153 v = NULL_TREE;
32155 goto done;
32156 case OMP_ATOMIC_CAPTURE_NEW:
32157 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32159 cp_lexer_consume_token (parser->lexer);
32160 structured_block = true;
32162 else
32164 v = cp_parser_unary_expression (parser);
32165 if (v == error_mark_node)
32166 goto saw_error;
32167 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32168 goto saw_error;
32170 default:
32171 break;
32174 restart:
32175 lhs = cp_parser_unary_expression (parser);
32176 orig_lhs = lhs;
32177 switch (TREE_CODE (lhs))
32179 case ERROR_MARK:
32180 goto saw_error;
32182 case POSTINCREMENT_EXPR:
32183 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32184 code = OMP_ATOMIC_CAPTURE_OLD;
32185 /* FALLTHROUGH */
32186 case PREINCREMENT_EXPR:
32187 lhs = TREE_OPERAND (lhs, 0);
32188 opcode = PLUS_EXPR;
32189 rhs = integer_one_node;
32190 break;
32192 case POSTDECREMENT_EXPR:
32193 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32194 code = OMP_ATOMIC_CAPTURE_OLD;
32195 /* FALLTHROUGH */
32196 case PREDECREMENT_EXPR:
32197 lhs = TREE_OPERAND (lhs, 0);
32198 opcode = MINUS_EXPR;
32199 rhs = integer_one_node;
32200 break;
32202 case COMPOUND_EXPR:
32203 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
32204 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
32205 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
32206 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
32207 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32208 (TREE_OPERAND (lhs, 1), 0), 0)))
32209 == BOOLEAN_TYPE)
32210 /* Undo effects of boolean_increment for post {in,de}crement. */
32211 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
32212 /* FALLTHRU */
32213 case MODIFY_EXPR:
32214 if (TREE_CODE (lhs) == MODIFY_EXPR
32215 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
32217 /* Undo effects of boolean_increment. */
32218 if (integer_onep (TREE_OPERAND (lhs, 1)))
32220 /* This is pre or post increment. */
32221 rhs = TREE_OPERAND (lhs, 1);
32222 lhs = TREE_OPERAND (lhs, 0);
32223 opcode = NOP_EXPR;
32224 if (code == OMP_ATOMIC_CAPTURE_NEW
32225 && !structured_block
32226 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
32227 code = OMP_ATOMIC_CAPTURE_OLD;
32228 break;
32231 /* FALLTHRU */
32232 default:
32233 switch (cp_lexer_peek_token (parser->lexer)->type)
32235 case CPP_MULT_EQ:
32236 opcode = MULT_EXPR;
32237 break;
32238 case CPP_DIV_EQ:
32239 opcode = TRUNC_DIV_EXPR;
32240 break;
32241 case CPP_PLUS_EQ:
32242 opcode = PLUS_EXPR;
32243 break;
32244 case CPP_MINUS_EQ:
32245 opcode = MINUS_EXPR;
32246 break;
32247 case CPP_LSHIFT_EQ:
32248 opcode = LSHIFT_EXPR;
32249 break;
32250 case CPP_RSHIFT_EQ:
32251 opcode = RSHIFT_EXPR;
32252 break;
32253 case CPP_AND_EQ:
32254 opcode = BIT_AND_EXPR;
32255 break;
32256 case CPP_OR_EQ:
32257 opcode = BIT_IOR_EXPR;
32258 break;
32259 case CPP_XOR_EQ:
32260 opcode = BIT_XOR_EXPR;
32261 break;
32262 case CPP_EQ:
32263 enum cp_parser_prec oprec;
32264 cp_token *token;
32265 cp_lexer_consume_token (parser->lexer);
32266 cp_parser_parse_tentatively (parser);
32267 rhs1 = cp_parser_simple_cast_expression (parser);
32268 if (rhs1 == error_mark_node)
32270 cp_parser_abort_tentative_parse (parser);
32271 cp_parser_simple_cast_expression (parser);
32272 goto saw_error;
32274 token = cp_lexer_peek_token (parser->lexer);
32275 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
32277 cp_parser_abort_tentative_parse (parser);
32278 cp_parser_parse_tentatively (parser);
32279 rhs = cp_parser_binary_expression (parser, false, true,
32280 PREC_NOT_OPERATOR, NULL);
32281 if (rhs == error_mark_node)
32283 cp_parser_abort_tentative_parse (parser);
32284 cp_parser_binary_expression (parser, false, true,
32285 PREC_NOT_OPERATOR, NULL);
32286 goto saw_error;
32288 switch (TREE_CODE (rhs))
32290 case MULT_EXPR:
32291 case TRUNC_DIV_EXPR:
32292 case RDIV_EXPR:
32293 case PLUS_EXPR:
32294 case MINUS_EXPR:
32295 case LSHIFT_EXPR:
32296 case RSHIFT_EXPR:
32297 case BIT_AND_EXPR:
32298 case BIT_IOR_EXPR:
32299 case BIT_XOR_EXPR:
32300 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
32302 if (cp_parser_parse_definitely (parser))
32304 opcode = TREE_CODE (rhs);
32305 rhs1 = TREE_OPERAND (rhs, 0);
32306 rhs = TREE_OPERAND (rhs, 1);
32307 goto stmt_done;
32309 else
32310 goto saw_error;
32312 break;
32313 default:
32314 break;
32316 cp_parser_abort_tentative_parse (parser);
32317 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
32319 rhs = cp_parser_expression (parser);
32320 if (rhs == error_mark_node)
32321 goto saw_error;
32322 opcode = NOP_EXPR;
32323 rhs1 = NULL_TREE;
32324 goto stmt_done;
32326 cp_parser_error (parser,
32327 "invalid form of %<#pragma omp atomic%>");
32328 goto saw_error;
32330 if (!cp_parser_parse_definitely (parser))
32331 goto saw_error;
32332 switch (token->type)
32334 case CPP_SEMICOLON:
32335 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32337 code = OMP_ATOMIC_CAPTURE_OLD;
32338 v = lhs;
32339 lhs = NULL_TREE;
32340 lhs1 = rhs1;
32341 rhs1 = NULL_TREE;
32342 cp_lexer_consume_token (parser->lexer);
32343 goto restart;
32345 else if (structured_block)
32347 opcode = NOP_EXPR;
32348 rhs = rhs1;
32349 rhs1 = NULL_TREE;
32350 goto stmt_done;
32352 cp_parser_error (parser,
32353 "invalid form of %<#pragma omp atomic%>");
32354 goto saw_error;
32355 case CPP_MULT:
32356 opcode = MULT_EXPR;
32357 break;
32358 case CPP_DIV:
32359 opcode = TRUNC_DIV_EXPR;
32360 break;
32361 case CPP_PLUS:
32362 opcode = PLUS_EXPR;
32363 break;
32364 case CPP_MINUS:
32365 opcode = MINUS_EXPR;
32366 break;
32367 case CPP_LSHIFT:
32368 opcode = LSHIFT_EXPR;
32369 break;
32370 case CPP_RSHIFT:
32371 opcode = RSHIFT_EXPR;
32372 break;
32373 case CPP_AND:
32374 opcode = BIT_AND_EXPR;
32375 break;
32376 case CPP_OR:
32377 opcode = BIT_IOR_EXPR;
32378 break;
32379 case CPP_XOR:
32380 opcode = BIT_XOR_EXPR;
32381 break;
32382 default:
32383 cp_parser_error (parser,
32384 "invalid operator for %<#pragma omp atomic%>");
32385 goto saw_error;
32387 oprec = TOKEN_PRECEDENCE (token);
32388 gcc_assert (oprec != PREC_NOT_OPERATOR);
32389 if (commutative_tree_code (opcode))
32390 oprec = (enum cp_parser_prec) (oprec - 1);
32391 cp_lexer_consume_token (parser->lexer);
32392 rhs = cp_parser_binary_expression (parser, false, false,
32393 oprec, NULL);
32394 if (rhs == error_mark_node)
32395 goto saw_error;
32396 goto stmt_done;
32397 /* FALLTHROUGH */
32398 default:
32399 cp_parser_error (parser,
32400 "invalid operator for %<#pragma omp atomic%>");
32401 goto saw_error;
32403 cp_lexer_consume_token (parser->lexer);
32405 rhs = cp_parser_expression (parser);
32406 if (rhs == error_mark_node)
32407 goto saw_error;
32408 break;
32410 stmt_done:
32411 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32413 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
32414 goto saw_error;
32415 v = cp_parser_unary_expression (parser);
32416 if (v == error_mark_node)
32417 goto saw_error;
32418 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32419 goto saw_error;
32420 lhs1 = cp_parser_unary_expression (parser);
32421 if (lhs1 == error_mark_node)
32422 goto saw_error;
32424 if (structured_block)
32426 cp_parser_consume_semicolon_at_end_of_statement (parser);
32427 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
32429 done:
32430 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
32431 if (!structured_block)
32432 cp_parser_consume_semicolon_at_end_of_statement (parser);
32433 return;
32435 saw_error:
32436 cp_parser_skip_to_end_of_block_or_statement (parser);
32437 if (structured_block)
32439 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
32440 cp_lexer_consume_token (parser->lexer);
32441 else if (code == OMP_ATOMIC_CAPTURE_NEW)
32443 cp_parser_skip_to_end_of_block_or_statement (parser);
32444 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
32445 cp_lexer_consume_token (parser->lexer);
32451 /* OpenMP 2.5:
32452 # pragma omp barrier new-line */
32454 static void
32455 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
32457 cp_parser_require_pragma_eol (parser, pragma_tok);
32458 finish_omp_barrier ();
32461 /* OpenMP 2.5:
32462 # pragma omp critical [(name)] new-line
32463 structured-block
32465 OpenMP 4.5:
32466 # pragma omp critical [(name) [hint(expression)]] new-line
32467 structured-block */
32469 #define OMP_CRITICAL_CLAUSE_MASK \
32470 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
32472 static tree
32473 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
32475 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
32477 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32479 cp_lexer_consume_token (parser->lexer);
32481 name = cp_parser_identifier (parser);
32483 if (name == error_mark_node
32484 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32485 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32486 /*or_comma=*/false,
32487 /*consume_paren=*/true);
32488 if (name == error_mark_node)
32489 name = NULL;
32491 clauses = cp_parser_omp_all_clauses (parser,
32492 OMP_CRITICAL_CLAUSE_MASK,
32493 "#pragma omp critical", pragma_tok);
32495 else
32496 cp_parser_require_pragma_eol (parser, pragma_tok);
32498 stmt = cp_parser_omp_structured_block (parser);
32499 return c_finish_omp_critical (input_location, stmt, name, clauses);
32502 /* OpenMP 2.5:
32503 # pragma omp flush flush-vars[opt] new-line
32505 flush-vars:
32506 ( variable-list ) */
32508 static void
32509 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
32511 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32512 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32513 cp_parser_require_pragma_eol (parser, pragma_tok);
32515 finish_omp_flush ();
32518 /* Helper function, to parse omp for increment expression. */
32520 static tree
32521 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
32523 tree cond = cp_parser_binary_expression (parser, false, true,
32524 PREC_NOT_OPERATOR, NULL);
32525 if (cond == error_mark_node
32526 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
32528 cp_parser_skip_to_end_of_statement (parser);
32529 return error_mark_node;
32532 switch (TREE_CODE (cond))
32534 case GT_EXPR:
32535 case GE_EXPR:
32536 case LT_EXPR:
32537 case LE_EXPR:
32538 break;
32539 case NE_EXPR:
32540 if (code == CILK_SIMD || code == CILK_FOR)
32541 break;
32542 /* Fall through: OpenMP disallows NE_EXPR. */
32543 default:
32544 return error_mark_node;
32547 /* If decl is an iterator, preserve LHS and RHS of the relational
32548 expr until finish_omp_for. */
32549 if (decl
32550 && (type_dependent_expression_p (decl)
32551 || CLASS_TYPE_P (TREE_TYPE (decl))))
32552 return cond;
32554 return build_x_binary_op (input_location, TREE_CODE (cond),
32555 TREE_OPERAND (cond, 0), ERROR_MARK,
32556 TREE_OPERAND (cond, 1), ERROR_MARK,
32557 /*overload=*/NULL, tf_warning_or_error);
32560 /* Helper function, to parse omp for increment expression. */
32562 static tree
32563 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
32565 cp_token *token = cp_lexer_peek_token (parser->lexer);
32566 enum tree_code op;
32567 tree lhs, rhs;
32568 cp_id_kind idk;
32569 bool decl_first;
32571 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
32573 op = (token->type == CPP_PLUS_PLUS
32574 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
32575 cp_lexer_consume_token (parser->lexer);
32576 lhs = cp_parser_simple_cast_expression (parser);
32577 if (lhs != decl
32578 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
32579 return error_mark_node;
32580 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
32583 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
32584 if (lhs != decl
32585 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
32586 return error_mark_node;
32588 token = cp_lexer_peek_token (parser->lexer);
32589 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
32591 op = (token->type == CPP_PLUS_PLUS
32592 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
32593 cp_lexer_consume_token (parser->lexer);
32594 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
32597 op = cp_parser_assignment_operator_opt (parser);
32598 if (op == ERROR_MARK)
32599 return error_mark_node;
32601 if (op != NOP_EXPR)
32603 rhs = cp_parser_assignment_expression (parser);
32604 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
32605 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
32608 lhs = cp_parser_binary_expression (parser, false, false,
32609 PREC_ADDITIVE_EXPRESSION, NULL);
32610 token = cp_lexer_peek_token (parser->lexer);
32611 decl_first = (lhs == decl
32612 || (processing_template_decl && cp_tree_equal (lhs, decl)));
32613 if (decl_first)
32614 lhs = NULL_TREE;
32615 if (token->type != CPP_PLUS
32616 && token->type != CPP_MINUS)
32617 return error_mark_node;
32621 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
32622 cp_lexer_consume_token (parser->lexer);
32623 rhs = cp_parser_binary_expression (parser, false, false,
32624 PREC_ADDITIVE_EXPRESSION, NULL);
32625 token = cp_lexer_peek_token (parser->lexer);
32626 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
32628 if (lhs == NULL_TREE)
32630 if (op == PLUS_EXPR)
32631 lhs = rhs;
32632 else
32633 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
32634 tf_warning_or_error);
32636 else
32637 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
32638 ERROR_MARK, NULL, tf_warning_or_error);
32641 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
32643 if (!decl_first)
32645 if ((rhs != decl
32646 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
32647 || op == MINUS_EXPR)
32648 return error_mark_node;
32649 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
32651 else
32652 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
32654 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
32657 /* Parse the initialization statement of either an OpenMP for loop or
32658 a Cilk Plus for loop.
32660 Return true if the resulting construct should have an
32661 OMP_CLAUSE_PRIVATE added to it. */
32663 static tree
32664 cp_parser_omp_for_loop_init (cp_parser *parser,
32665 enum tree_code code,
32666 tree &this_pre_body,
32667 vec<tree, va_gc> *for_block,
32668 tree &init,
32669 tree &orig_init,
32670 tree &decl,
32671 tree &real_decl)
32673 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32674 return NULL_TREE;
32676 tree add_private_clause = NULL_TREE;
32678 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
32680 init-expr:
32681 var = lb
32682 integer-type var = lb
32683 random-access-iterator-type var = lb
32684 pointer-type var = lb
32686 cp_decl_specifier_seq type_specifiers;
32688 /* First, try to parse as an initialized declaration. See
32689 cp_parser_condition, from whence the bulk of this is copied. */
32691 cp_parser_parse_tentatively (parser);
32692 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
32693 /*is_trailing_return=*/false,
32694 &type_specifiers);
32695 if (cp_parser_parse_definitely (parser))
32697 /* If parsing a type specifier seq succeeded, then this
32698 MUST be a initialized declaration. */
32699 tree asm_specification, attributes;
32700 cp_declarator *declarator;
32702 declarator = cp_parser_declarator (parser,
32703 CP_PARSER_DECLARATOR_NAMED,
32704 /*ctor_dtor_or_conv_p=*/NULL,
32705 /*parenthesized_p=*/NULL,
32706 /*member_p=*/false,
32707 /*friend_p=*/false);
32708 attributes = cp_parser_attributes_opt (parser);
32709 asm_specification = cp_parser_asm_specification_opt (parser);
32711 if (declarator == cp_error_declarator)
32712 cp_parser_skip_to_end_of_statement (parser);
32714 else
32716 tree pushed_scope, auto_node;
32718 decl = start_decl (declarator, &type_specifiers,
32719 SD_INITIALIZED, attributes,
32720 /*prefix_attributes=*/NULL_TREE,
32721 &pushed_scope);
32723 auto_node = type_uses_auto (TREE_TYPE (decl));
32724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
32726 if (cp_lexer_next_token_is (parser->lexer,
32727 CPP_OPEN_PAREN))
32729 if (code != CILK_SIMD && code != CILK_FOR)
32730 error ("parenthesized initialization is not allowed in "
32731 "OpenMP %<for%> loop");
32732 else
32733 error ("parenthesized initialization is "
32734 "not allowed in for-loop");
32736 else
32737 /* Trigger an error. */
32738 cp_parser_require (parser, CPP_EQ, RT_EQ);
32740 init = error_mark_node;
32741 cp_parser_skip_to_end_of_statement (parser);
32743 else if (CLASS_TYPE_P (TREE_TYPE (decl))
32744 || type_dependent_expression_p (decl)
32745 || auto_node)
32747 bool is_direct_init, is_non_constant_init;
32749 init = cp_parser_initializer (parser,
32750 &is_direct_init,
32751 &is_non_constant_init);
32753 if (auto_node)
32755 TREE_TYPE (decl)
32756 = do_auto_deduction (TREE_TYPE (decl), init,
32757 auto_node);
32759 if (!CLASS_TYPE_P (TREE_TYPE (decl))
32760 && !type_dependent_expression_p (decl))
32761 goto non_class;
32764 cp_finish_decl (decl, init, !is_non_constant_init,
32765 asm_specification,
32766 LOOKUP_ONLYCONVERTING);
32767 orig_init = init;
32768 if (CLASS_TYPE_P (TREE_TYPE (decl)))
32770 vec_safe_push (for_block, this_pre_body);
32771 init = NULL_TREE;
32773 else
32774 init = pop_stmt_list (this_pre_body);
32775 this_pre_body = NULL_TREE;
32777 else
32779 /* Consume '='. */
32780 cp_lexer_consume_token (parser->lexer);
32781 init = cp_parser_assignment_expression (parser);
32783 non_class:
32784 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
32785 init = error_mark_node;
32786 else
32787 cp_finish_decl (decl, NULL_TREE,
32788 /*init_const_expr_p=*/false,
32789 asm_specification,
32790 LOOKUP_ONLYCONVERTING);
32793 if (pushed_scope)
32794 pop_scope (pushed_scope);
32797 else
32799 cp_id_kind idk;
32800 /* If parsing a type specifier sequence failed, then
32801 this MUST be a simple expression. */
32802 if (code == CILK_FOR)
32803 error ("%<_Cilk_for%> allows expression instead of declaration only "
32804 "in C, not in C++");
32805 cp_parser_parse_tentatively (parser);
32806 decl = cp_parser_primary_expression (parser, false, false,
32807 false, &idk);
32808 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
32809 if (!cp_parser_error_occurred (parser)
32810 && decl
32811 && (TREE_CODE (decl) == COMPONENT_REF
32812 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
32814 cp_parser_abort_tentative_parse (parser);
32815 cp_parser_parse_tentatively (parser);
32816 cp_token *token = cp_lexer_peek_token (parser->lexer);
32817 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
32818 /*check_dependency_p=*/true,
32819 /*template_p=*/NULL,
32820 /*declarator_p=*/false,
32821 /*optional_p=*/false);
32822 if (name != error_mark_node
32823 && last_tok == cp_lexer_peek_token (parser->lexer))
32825 decl = cp_parser_lookup_name_simple (parser, name,
32826 token->location);
32827 if (TREE_CODE (decl) == FIELD_DECL)
32828 add_private_clause = omp_privatize_field (decl, false);
32830 cp_parser_abort_tentative_parse (parser);
32831 cp_parser_parse_tentatively (parser);
32832 decl = cp_parser_primary_expression (parser, false, false,
32833 false, &idk);
32835 if (!cp_parser_error_occurred (parser)
32836 && decl
32837 && DECL_P (decl)
32838 && CLASS_TYPE_P (TREE_TYPE (decl)))
32840 tree rhs;
32842 cp_parser_parse_definitely (parser);
32843 cp_parser_require (parser, CPP_EQ, RT_EQ);
32844 rhs = cp_parser_assignment_expression (parser);
32845 orig_init = rhs;
32846 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
32847 decl, NOP_EXPR,
32848 rhs,
32849 tf_warning_or_error));
32850 if (!add_private_clause)
32851 add_private_clause = decl;
32853 else
32855 decl = NULL;
32856 cp_parser_abort_tentative_parse (parser);
32857 init = cp_parser_expression (parser);
32858 if (init)
32860 if (TREE_CODE (init) == MODIFY_EXPR
32861 || TREE_CODE (init) == MODOP_EXPR)
32862 real_decl = TREE_OPERAND (init, 0);
32866 return add_private_clause;
32869 /* Parse the restricted form of the for statement allowed by OpenMP. */
32871 static tree
32872 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
32873 tree *cclauses)
32875 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
32876 tree real_decl, initv, condv, incrv, declv;
32877 tree this_pre_body, cl, ordered_cl = NULL_TREE;
32878 location_t loc_first;
32879 bool collapse_err = false;
32880 int i, collapse = 1, ordered = 0, count, nbraces = 0;
32881 vec<tree, va_gc> *for_block = make_tree_vector ();
32882 auto_vec<tree, 4> orig_inits;
32884 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
32885 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
32886 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
32887 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
32888 && OMP_CLAUSE_ORDERED_EXPR (cl))
32890 ordered_cl = cl;
32891 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
32894 if (ordered && ordered < collapse)
32896 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
32897 "%<ordered%> clause parameter is less than %<collapse%>");
32898 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
32899 = build_int_cst (NULL_TREE, collapse);
32900 ordered = collapse;
32902 if (ordered)
32904 for (tree *pc = &clauses; *pc; )
32905 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
32907 error_at (OMP_CLAUSE_LOCATION (*pc),
32908 "%<linear%> clause may not be specified together "
32909 "with %<ordered%> clause with a parameter");
32910 *pc = OMP_CLAUSE_CHAIN (*pc);
32912 else
32913 pc = &OMP_CLAUSE_CHAIN (*pc);
32916 gcc_assert (collapse >= 1 && ordered >= 0);
32917 count = ordered ? ordered : collapse;
32919 declv = make_tree_vec (count);
32920 initv = make_tree_vec (count);
32921 condv = make_tree_vec (count);
32922 incrv = make_tree_vec (count);
32924 loc_first = cp_lexer_peek_token (parser->lexer)->location;
32926 for (i = 0; i < count; i++)
32928 int bracecount = 0;
32929 tree add_private_clause = NULL_TREE;
32930 location_t loc;
32932 if (code != CILK_FOR
32933 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32935 cp_parser_error (parser, "for statement expected");
32936 return NULL;
32938 if (code == CILK_FOR
32939 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32941 cp_parser_error (parser, "_Cilk_for statement expected");
32942 return NULL;
32944 loc = cp_lexer_consume_token (parser->lexer)->location;
32946 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32947 return NULL;
32949 init = orig_init = decl = real_decl = NULL;
32950 this_pre_body = push_stmt_list ();
32952 add_private_clause
32953 = cp_parser_omp_for_loop_init (parser, code,
32954 this_pre_body, for_block,
32955 init, orig_init, decl, real_decl);
32957 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32958 if (this_pre_body)
32960 this_pre_body = pop_stmt_list (this_pre_body);
32961 if (pre_body)
32963 tree t = pre_body;
32964 pre_body = push_stmt_list ();
32965 add_stmt (t);
32966 add_stmt (this_pre_body);
32967 pre_body = pop_stmt_list (pre_body);
32969 else
32970 pre_body = this_pre_body;
32973 if (decl)
32974 real_decl = decl;
32975 if (cclauses != NULL
32976 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
32977 && real_decl != NULL_TREE)
32979 tree *c;
32980 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
32981 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
32982 && OMP_CLAUSE_DECL (*c) == real_decl)
32984 error_at (loc, "iteration variable %qD"
32985 " should not be firstprivate", real_decl);
32986 *c = OMP_CLAUSE_CHAIN (*c);
32988 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
32989 && OMP_CLAUSE_DECL (*c) == real_decl)
32991 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
32992 tree l = *c;
32993 *c = OMP_CLAUSE_CHAIN (*c);
32994 if (code == OMP_SIMD)
32996 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
32997 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
32999 else
33001 OMP_CLAUSE_CHAIN (l) = clauses;
33002 clauses = l;
33004 add_private_clause = NULL_TREE;
33006 else
33008 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33009 && OMP_CLAUSE_DECL (*c) == real_decl)
33010 add_private_clause = NULL_TREE;
33011 c = &OMP_CLAUSE_CHAIN (*c);
33015 if (add_private_clause)
33017 tree c;
33018 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33020 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33021 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33022 && OMP_CLAUSE_DECL (c) == decl)
33023 break;
33024 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33025 && OMP_CLAUSE_DECL (c) == decl)
33026 error_at (loc, "iteration variable %qD "
33027 "should not be firstprivate",
33028 decl);
33029 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33030 && OMP_CLAUSE_DECL (c) == decl)
33031 error_at (loc, "iteration variable %qD should not be reduction",
33032 decl);
33034 if (c == NULL)
33036 if (code != OMP_SIMD)
33037 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33038 else if (collapse == 1)
33039 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33040 else
33041 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33042 OMP_CLAUSE_DECL (c) = add_private_clause;
33043 c = finish_omp_clauses (c, true);
33044 if (c)
33046 OMP_CLAUSE_CHAIN (c) = clauses;
33047 clauses = c;
33048 /* For linear, signal that we need to fill up
33049 the so far unknown linear step. */
33050 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33051 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33056 cond = NULL;
33057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33058 cond = cp_parser_omp_for_cond (parser, decl, code);
33059 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33061 incr = NULL;
33062 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33064 /* If decl is an iterator, preserve the operator on decl
33065 until finish_omp_for. */
33066 if (real_decl
33067 && ((processing_template_decl
33068 && (TREE_TYPE (real_decl) == NULL_TREE
33069 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33070 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33071 incr = cp_parser_omp_for_incr (parser, real_decl);
33072 else
33073 incr = cp_parser_expression (parser);
33074 if (!EXPR_HAS_LOCATION (incr))
33075 protected_set_expr_location (incr, input_location);
33078 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33079 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33080 /*or_comma=*/false,
33081 /*consume_paren=*/true);
33083 TREE_VEC_ELT (declv, i) = decl;
33084 TREE_VEC_ELT (initv, i) = init;
33085 TREE_VEC_ELT (condv, i) = cond;
33086 TREE_VEC_ELT (incrv, i) = incr;
33087 if (orig_init)
33089 orig_inits.safe_grow_cleared (i + 1);
33090 orig_inits[i] = orig_init;
33093 if (i == count - 1)
33094 break;
33096 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33097 in between the collapsed for loops to be still considered perfectly
33098 nested. Hopefully the final version clarifies this.
33099 For now handle (multiple) {'s and empty statements. */
33100 cp_parser_parse_tentatively (parser);
33103 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33104 break;
33105 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33107 cp_lexer_consume_token (parser->lexer);
33108 bracecount++;
33110 else if (bracecount
33111 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33112 cp_lexer_consume_token (parser->lexer);
33113 else
33115 loc = cp_lexer_peek_token (parser->lexer)->location;
33116 error_at (loc, "not enough collapsed for loops");
33117 collapse_err = true;
33118 cp_parser_abort_tentative_parse (parser);
33119 declv = NULL_TREE;
33120 break;
33123 while (1);
33125 if (declv)
33127 cp_parser_parse_definitely (parser);
33128 nbraces += bracecount;
33132 /* Note that we saved the original contents of this flag when we entered
33133 the structured block, and so we don't need to re-save it here. */
33134 if (code == CILK_SIMD || code == CILK_FOR)
33135 parser->in_statement = IN_CILK_SIMD_FOR;
33136 else
33137 parser->in_statement = IN_OMP_FOR;
33139 /* Note that the grammar doesn't call for a structured block here,
33140 though the loop as a whole is a structured block. */
33141 body = push_stmt_list ();
33142 cp_parser_statement (parser, NULL_TREE, false, NULL);
33143 body = pop_stmt_list (body);
33145 if (declv == NULL_TREE)
33146 ret = NULL_TREE;
33147 else
33148 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33149 body, pre_body, &orig_inits, clauses);
33151 while (nbraces)
33153 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33155 cp_lexer_consume_token (parser->lexer);
33156 nbraces--;
33158 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33159 cp_lexer_consume_token (parser->lexer);
33160 else
33162 if (!collapse_err)
33164 error_at (cp_lexer_peek_token (parser->lexer)->location,
33165 "collapsed loops not perfectly nested");
33167 collapse_err = true;
33168 cp_parser_statement_seq_opt (parser, NULL);
33169 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33170 break;
33174 while (!for_block->is_empty ())
33175 add_stmt (pop_stmt_list (for_block->pop ()));
33176 release_tree_vector (for_block);
33178 return ret;
33181 /* Helper function for OpenMP parsing, split clauses and call
33182 finish_omp_clauses on each of the set of clauses afterwards. */
33184 static void
33185 cp_omp_split_clauses (location_t loc, enum tree_code code,
33186 omp_clause_mask mask, tree clauses, tree *cclauses)
33188 int i;
33189 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
33190 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
33191 if (cclauses[i])
33192 cclauses[i] = finish_omp_clauses (cclauses[i], true);
33195 /* OpenMP 4.0:
33196 #pragma omp simd simd-clause[optseq] new-line
33197 for-loop */
33199 #define OMP_SIMD_CLAUSE_MASK \
33200 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
33201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33209 static tree
33210 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
33211 char *p_name, omp_clause_mask mask, tree *cclauses)
33213 tree clauses, sb, ret;
33214 unsigned int save;
33215 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33217 strcat (p_name, " simd");
33218 mask |= OMP_SIMD_CLAUSE_MASK;
33220 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33221 cclauses == NULL);
33222 if (cclauses)
33224 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
33225 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
33226 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
33227 OMP_CLAUSE_ORDERED);
33228 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
33230 error_at (OMP_CLAUSE_LOCATION (c),
33231 "%<ordered%> clause with parameter may not be specified "
33232 "on %qs construct", p_name);
33233 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
33237 sb = begin_omp_structured_block ();
33238 save = cp_parser_begin_omp_structured_block (parser);
33240 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
33242 cp_parser_end_omp_structured_block (parser, save);
33243 add_stmt (finish_omp_structured_block (sb));
33245 return ret;
33248 /* OpenMP 2.5:
33249 #pragma omp for for-clause[optseq] new-line
33250 for-loop
33252 OpenMP 4.0:
33253 #pragma omp for simd for-simd-clause[optseq] new-line
33254 for-loop */
33256 #define OMP_FOR_CLAUSE_MASK \
33257 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
33263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
33264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
33265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33267 static tree
33268 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
33269 char *p_name, omp_clause_mask mask, tree *cclauses)
33271 tree clauses, sb, ret;
33272 unsigned int save;
33273 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33275 strcat (p_name, " for");
33276 mask |= OMP_FOR_CLAUSE_MASK;
33277 if (cclauses)
33278 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33279 /* Composite distribute parallel for{, simd} disallows ordered clause. */
33280 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33281 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
33283 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33285 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33286 const char *p = IDENTIFIER_POINTER (id);
33288 if (strcmp (p, "simd") == 0)
33290 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33291 if (cclauses == NULL)
33292 cclauses = cclauses_buf;
33294 cp_lexer_consume_token (parser->lexer);
33295 if (!flag_openmp) /* flag_openmp_simd */
33296 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33297 cclauses);
33298 sb = begin_omp_structured_block ();
33299 save = cp_parser_begin_omp_structured_block (parser);
33300 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33301 cclauses);
33302 cp_parser_end_omp_structured_block (parser, save);
33303 tree body = finish_omp_structured_block (sb);
33304 if (ret == NULL)
33305 return ret;
33306 ret = make_node (OMP_FOR);
33307 TREE_TYPE (ret) = void_type_node;
33308 OMP_FOR_BODY (ret) = body;
33309 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33310 SET_EXPR_LOCATION (ret, loc);
33311 add_stmt (ret);
33312 return ret;
33315 if (!flag_openmp) /* flag_openmp_simd */
33317 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33318 return NULL_TREE;
33321 /* Composite distribute parallel for disallows linear clause. */
33322 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33323 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
33325 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33326 cclauses == NULL);
33327 if (cclauses)
33329 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
33330 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33333 sb = begin_omp_structured_block ();
33334 save = cp_parser_begin_omp_structured_block (parser);
33336 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
33338 cp_parser_end_omp_structured_block (parser, save);
33339 add_stmt (finish_omp_structured_block (sb));
33341 return ret;
33344 /* OpenMP 2.5:
33345 # pragma omp master new-line
33346 structured-block */
33348 static tree
33349 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
33351 cp_parser_require_pragma_eol (parser, pragma_tok);
33352 return c_finish_omp_master (input_location,
33353 cp_parser_omp_structured_block (parser));
33356 /* OpenMP 2.5:
33357 # pragma omp ordered new-line
33358 structured-block
33360 OpenMP 4.5:
33361 # pragma omp ordered ordered-clauses new-line
33362 structured-block */
33364 #define OMP_ORDERED_CLAUSE_MASK \
33365 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
33366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
33368 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
33369 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
33371 static bool
33372 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
33373 enum pragma_context context)
33375 location_t loc = pragma_tok->location;
33377 if (context != pragma_stmt && context != pragma_compound)
33379 cp_parser_error (parser, "expected declaration specifiers");
33380 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33381 return false;
33384 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33386 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33387 const char *p = IDENTIFIER_POINTER (id);
33389 if (strcmp (p, "depend") == 0)
33391 if (context == pragma_stmt)
33393 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
33394 "%<depend%> clause may only be used in compound "
33395 "statements");
33396 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33397 return false;
33399 tree clauses
33400 = cp_parser_omp_all_clauses (parser,
33401 OMP_ORDERED_DEPEND_CLAUSE_MASK,
33402 "#pragma omp ordered", pragma_tok);
33403 c_finish_omp_ordered (loc, clauses, NULL_TREE);
33404 return false;
33408 tree clauses
33409 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
33410 "#pragma omp ordered", pragma_tok);
33411 c_finish_omp_ordered (loc, clauses,
33412 cp_parser_omp_structured_block (parser));
33413 return true;
33416 /* OpenMP 2.5:
33418 section-scope:
33419 { section-sequence }
33421 section-sequence:
33422 section-directive[opt] structured-block
33423 section-sequence section-directive structured-block */
33425 static tree
33426 cp_parser_omp_sections_scope (cp_parser *parser)
33428 tree stmt, substmt;
33429 bool error_suppress = false;
33430 cp_token *tok;
33432 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
33433 return NULL_TREE;
33435 stmt = push_stmt_list ();
33437 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
33439 substmt = cp_parser_omp_structured_block (parser);
33440 substmt = build1 (OMP_SECTION, void_type_node, substmt);
33441 add_stmt (substmt);
33444 while (1)
33446 tok = cp_lexer_peek_token (parser->lexer);
33447 if (tok->type == CPP_CLOSE_BRACE)
33448 break;
33449 if (tok->type == CPP_EOF)
33450 break;
33452 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
33454 cp_lexer_consume_token (parser->lexer);
33455 cp_parser_require_pragma_eol (parser, tok);
33456 error_suppress = false;
33458 else if (!error_suppress)
33460 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
33461 error_suppress = true;
33464 substmt = cp_parser_omp_structured_block (parser);
33465 substmt = build1 (OMP_SECTION, void_type_node, substmt);
33466 add_stmt (substmt);
33468 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33470 substmt = pop_stmt_list (stmt);
33472 stmt = make_node (OMP_SECTIONS);
33473 TREE_TYPE (stmt) = void_type_node;
33474 OMP_SECTIONS_BODY (stmt) = substmt;
33476 add_stmt (stmt);
33477 return stmt;
33480 /* OpenMP 2.5:
33481 # pragma omp sections sections-clause[optseq] newline
33482 sections-scope */
33484 #define OMP_SECTIONS_CLAUSE_MASK \
33485 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
33491 static tree
33492 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
33493 char *p_name, omp_clause_mask mask, tree *cclauses)
33495 tree clauses, ret;
33496 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33498 strcat (p_name, " sections");
33499 mask |= OMP_SECTIONS_CLAUSE_MASK;
33500 if (cclauses)
33501 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33503 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33504 cclauses == NULL);
33505 if (cclauses)
33507 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
33508 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
33511 ret = cp_parser_omp_sections_scope (parser);
33512 if (ret)
33513 OMP_SECTIONS_CLAUSES (ret) = clauses;
33515 return ret;
33518 /* OpenMP 2.5:
33519 # pragma omp parallel parallel-clause[optseq] new-line
33520 structured-block
33521 # pragma omp parallel for parallel-for-clause[optseq] new-line
33522 structured-block
33523 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
33524 structured-block
33526 OpenMP 4.0:
33527 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
33528 structured-block */
33530 #define OMP_PARALLEL_CLAUSE_MASK \
33531 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
33535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
33537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
33539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
33541 static tree
33542 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
33543 char *p_name, omp_clause_mask mask, tree *cclauses)
33545 tree stmt, clauses, block;
33546 unsigned int save;
33547 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33549 strcat (p_name, " parallel");
33550 mask |= OMP_PARALLEL_CLAUSE_MASK;
33551 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
33552 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
33553 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
33554 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
33556 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33558 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33559 if (cclauses == NULL)
33560 cclauses = cclauses_buf;
33562 cp_lexer_consume_token (parser->lexer);
33563 if (!flag_openmp) /* flag_openmp_simd */
33564 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
33565 block = begin_omp_parallel ();
33566 save = cp_parser_begin_omp_structured_block (parser);
33567 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
33568 cp_parser_end_omp_structured_block (parser, save);
33569 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
33570 block);
33571 if (ret == NULL_TREE)
33572 return ret;
33573 OMP_PARALLEL_COMBINED (stmt) = 1;
33574 return stmt;
33576 /* When combined with distribute, parallel has to be followed by for.
33577 #pragma omp target parallel is allowed though. */
33578 else if (cclauses
33579 && (mask & (OMP_CLAUSE_MASK_1
33580 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33582 error_at (loc, "expected %<for%> after %qs", p_name);
33583 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33584 return NULL_TREE;
33586 else if (!flag_openmp) /* flag_openmp_simd */
33588 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33589 return NULL_TREE;
33591 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33593 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33594 const char *p = IDENTIFIER_POINTER (id);
33595 if (strcmp (p, "sections") == 0)
33597 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33598 cclauses = cclauses_buf;
33600 cp_lexer_consume_token (parser->lexer);
33601 block = begin_omp_parallel ();
33602 save = cp_parser_begin_omp_structured_block (parser);
33603 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
33604 cp_parser_end_omp_structured_block (parser, save);
33605 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
33606 block);
33607 OMP_PARALLEL_COMBINED (stmt) = 1;
33608 return stmt;
33612 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
33613 if (cclauses)
33615 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
33616 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
33619 block = begin_omp_parallel ();
33620 save = cp_parser_begin_omp_structured_block (parser);
33621 cp_parser_statement (parser, NULL_TREE, false, NULL);
33622 cp_parser_end_omp_structured_block (parser, save);
33623 stmt = finish_omp_parallel (clauses, block);
33624 return stmt;
33627 /* OpenMP 2.5:
33628 # pragma omp single single-clause[optseq] new-line
33629 structured-block */
33631 #define OMP_SINGLE_CLAUSE_MASK \
33632 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
33635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
33637 static tree
33638 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
33640 tree stmt = make_node (OMP_SINGLE);
33641 TREE_TYPE (stmt) = void_type_node;
33643 OMP_SINGLE_CLAUSES (stmt)
33644 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
33645 "#pragma omp single", pragma_tok);
33646 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
33648 return add_stmt (stmt);
33651 /* OpenMP 3.0:
33652 # pragma omp task task-clause[optseq] new-line
33653 structured-block */
33655 #define OMP_TASK_CLAUSE_MASK \
33656 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
33658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
33659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
33663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
33664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
33665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
33667 static tree
33668 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
33670 tree clauses, block;
33671 unsigned int save;
33673 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
33674 "#pragma omp task", pragma_tok);
33675 block = begin_omp_task ();
33676 save = cp_parser_begin_omp_structured_block (parser);
33677 cp_parser_statement (parser, NULL_TREE, false, NULL);
33678 cp_parser_end_omp_structured_block (parser, save);
33679 return finish_omp_task (clauses, block);
33682 /* OpenMP 3.0:
33683 # pragma omp taskwait new-line */
33685 static void
33686 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
33688 cp_parser_require_pragma_eol (parser, pragma_tok);
33689 finish_omp_taskwait ();
33692 /* OpenMP 3.1:
33693 # pragma omp taskyield new-line */
33695 static void
33696 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
33698 cp_parser_require_pragma_eol (parser, pragma_tok);
33699 finish_omp_taskyield ();
33702 /* OpenMP 4.0:
33703 # pragma omp taskgroup new-line
33704 structured-block */
33706 static tree
33707 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
33709 cp_parser_require_pragma_eol (parser, pragma_tok);
33710 return c_finish_omp_taskgroup (input_location,
33711 cp_parser_omp_structured_block (parser));
33715 /* OpenMP 2.5:
33716 # pragma omp threadprivate (variable-list) */
33718 static void
33719 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
33721 tree vars;
33723 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33724 cp_parser_require_pragma_eol (parser, pragma_tok);
33726 finish_omp_threadprivate (vars);
33729 /* OpenMP 4.0:
33730 # pragma omp cancel cancel-clause[optseq] new-line */
33732 #define OMP_CANCEL_CLAUSE_MASK \
33733 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
33734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
33735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
33736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
33737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
33739 static void
33740 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
33742 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
33743 "#pragma omp cancel", pragma_tok);
33744 finish_omp_cancel (clauses);
33747 /* OpenMP 4.0:
33748 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
33750 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
33751 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
33752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
33753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
33754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
33756 static void
33757 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
33759 tree clauses;
33760 bool point_seen = false;
33762 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33764 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33765 const char *p = IDENTIFIER_POINTER (id);
33767 if (strcmp (p, "point") == 0)
33769 cp_lexer_consume_token (parser->lexer);
33770 point_seen = true;
33773 if (!point_seen)
33775 cp_parser_error (parser, "expected %<point%>");
33776 cp_parser_require_pragma_eol (parser, pragma_tok);
33777 return;
33780 clauses = cp_parser_omp_all_clauses (parser,
33781 OMP_CANCELLATION_POINT_CLAUSE_MASK,
33782 "#pragma omp cancellation point",
33783 pragma_tok);
33784 finish_omp_cancellation_point (clauses);
33787 /* OpenMP 4.0:
33788 #pragma omp distribute distribute-clause[optseq] new-line
33789 for-loop */
33791 #define OMP_DISTRIBUTE_CLAUSE_MASK \
33792 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
33796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33798 static tree
33799 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
33800 char *p_name, omp_clause_mask mask, tree *cclauses)
33802 tree clauses, sb, ret;
33803 unsigned int save;
33804 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33806 strcat (p_name, " distribute");
33807 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
33809 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33811 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33812 const char *p = IDENTIFIER_POINTER (id);
33813 bool simd = false;
33814 bool parallel = false;
33816 if (strcmp (p, "simd") == 0)
33817 simd = true;
33818 else
33819 parallel = strcmp (p, "parallel") == 0;
33820 if (parallel || simd)
33822 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33823 if (cclauses == NULL)
33824 cclauses = cclauses_buf;
33825 cp_lexer_consume_token (parser->lexer);
33826 if (!flag_openmp) /* flag_openmp_simd */
33828 if (simd)
33829 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33830 cclauses);
33831 else
33832 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
33833 cclauses);
33835 sb = begin_omp_structured_block ();
33836 save = cp_parser_begin_omp_structured_block (parser);
33837 if (simd)
33838 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33839 cclauses);
33840 else
33841 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
33842 cclauses);
33843 cp_parser_end_omp_structured_block (parser, save);
33844 tree body = finish_omp_structured_block (sb);
33845 if (ret == NULL)
33846 return ret;
33847 ret = make_node (OMP_DISTRIBUTE);
33848 TREE_TYPE (ret) = void_type_node;
33849 OMP_FOR_BODY (ret) = body;
33850 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
33851 SET_EXPR_LOCATION (ret, loc);
33852 add_stmt (ret);
33853 return ret;
33856 if (!flag_openmp) /* flag_openmp_simd */
33858 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33859 return NULL_TREE;
33862 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33863 cclauses == NULL);
33864 if (cclauses)
33866 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
33867 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
33870 sb = begin_omp_structured_block ();
33871 save = cp_parser_begin_omp_structured_block (parser);
33873 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
33875 cp_parser_end_omp_structured_block (parser, save);
33876 add_stmt (finish_omp_structured_block (sb));
33878 return ret;
33881 /* OpenMP 4.0:
33882 # pragma omp teams teams-clause[optseq] new-line
33883 structured-block */
33885 #define OMP_TEAMS_CLAUSE_MASK \
33886 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
33891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
33892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
33894 static tree
33895 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
33896 char *p_name, omp_clause_mask mask, tree *cclauses)
33898 tree clauses, sb, ret;
33899 unsigned int save;
33900 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33902 strcat (p_name, " teams");
33903 mask |= OMP_TEAMS_CLAUSE_MASK;
33905 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33907 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33908 const char *p = IDENTIFIER_POINTER (id);
33909 if (strcmp (p, "distribute") == 0)
33911 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33912 if (cclauses == NULL)
33913 cclauses = cclauses_buf;
33915 cp_lexer_consume_token (parser->lexer);
33916 if (!flag_openmp) /* flag_openmp_simd */
33917 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
33918 cclauses);
33919 sb = begin_omp_structured_block ();
33920 save = cp_parser_begin_omp_structured_block (parser);
33921 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
33922 cclauses);
33923 cp_parser_end_omp_structured_block (parser, save);
33924 tree body = finish_omp_structured_block (sb);
33925 if (ret == NULL)
33926 return ret;
33927 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
33928 ret = make_node (OMP_TEAMS);
33929 TREE_TYPE (ret) = void_type_node;
33930 OMP_TEAMS_CLAUSES (ret) = clauses;
33931 OMP_TEAMS_BODY (ret) = body;
33932 OMP_TEAMS_COMBINED (ret) = 1;
33933 return add_stmt (ret);
33936 if (!flag_openmp) /* flag_openmp_simd */
33938 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33939 return NULL_TREE;
33942 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33943 cclauses == NULL);
33944 if (cclauses)
33946 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
33947 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
33950 tree stmt = make_node (OMP_TEAMS);
33951 TREE_TYPE (stmt) = void_type_node;
33952 OMP_TEAMS_CLAUSES (stmt) = clauses;
33953 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
33955 return add_stmt (stmt);
33958 /* OpenMP 4.0:
33959 # pragma omp target data target-data-clause[optseq] new-line
33960 structured-block */
33962 #define OMP_TARGET_DATA_CLAUSE_MASK \
33963 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
33964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
33965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
33968 static tree
33969 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
33971 tree clauses
33972 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
33973 "#pragma omp target data", pragma_tok);
33974 int map_seen = 0;
33975 for (tree *pc = &clauses; *pc;)
33977 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
33978 switch (OMP_CLAUSE_MAP_KIND (*pc))
33980 case GOMP_MAP_TO:
33981 case GOMP_MAP_ALWAYS_TO:
33982 case GOMP_MAP_FROM:
33983 case GOMP_MAP_ALWAYS_FROM:
33984 case GOMP_MAP_TOFROM:
33985 case GOMP_MAP_ALWAYS_TOFROM:
33986 case GOMP_MAP_ALLOC:
33987 map_seen = 3;
33988 break;
33989 case GOMP_MAP_FIRSTPRIVATE_POINTER:
33990 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
33991 case GOMP_MAP_ALWAYS_POINTER:
33992 break;
33993 default:
33994 map_seen |= 1;
33995 error_at (OMP_CLAUSE_LOCATION (*pc),
33996 "%<#pragma omp target data%> with map-type other "
33997 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
33998 "on %<map%> clause");
33999 *pc = OMP_CLAUSE_CHAIN (*pc);
34000 continue;
34002 pc = &OMP_CLAUSE_CHAIN (*pc);
34005 if (map_seen != 3)
34007 if (map_seen == 0)
34008 error_at (pragma_tok->location,
34009 "%<#pragma omp target data%> must contain at least "
34010 "one %<map%> clause");
34011 return NULL_TREE;
34014 tree stmt = make_node (OMP_TARGET_DATA);
34015 TREE_TYPE (stmt) = void_type_node;
34016 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34018 keep_next_level (true);
34019 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
34021 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34022 return add_stmt (stmt);
34025 /* OpenMP 4.5:
34026 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34027 structured-block */
34029 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34030 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34036 static tree
34037 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34038 enum pragma_context context)
34040 bool data_seen = false;
34041 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34043 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34044 const char *p = IDENTIFIER_POINTER (id);
34046 if (strcmp (p, "data") == 0)
34048 cp_lexer_consume_token (parser->lexer);
34049 data_seen = true;
34052 if (!data_seen)
34054 cp_parser_error (parser, "expected %<data%>");
34055 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34056 return NULL_TREE;
34059 if (context == pragma_stmt)
34061 error_at (pragma_tok->location,
34062 "%<#pragma omp target enter data%> may only be "
34063 "used in compound statements");
34064 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34065 return NULL_TREE;
34068 tree clauses
34069 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34070 "#pragma omp target enter data", pragma_tok);
34071 int map_seen = 0;
34072 for (tree *pc = &clauses; *pc;)
34074 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34075 switch (OMP_CLAUSE_MAP_KIND (*pc))
34077 case GOMP_MAP_TO:
34078 case GOMP_MAP_ALWAYS_TO:
34079 case GOMP_MAP_ALLOC:
34080 map_seen = 3;
34081 break;
34082 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34083 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34084 case GOMP_MAP_ALWAYS_POINTER:
34085 break;
34086 default:
34087 map_seen |= 1;
34088 error_at (OMP_CLAUSE_LOCATION (*pc),
34089 "%<#pragma omp target enter data%> with map-type other "
34090 "than %<to%> or %<alloc%> on %<map%> clause");
34091 *pc = OMP_CLAUSE_CHAIN (*pc);
34092 continue;
34094 pc = &OMP_CLAUSE_CHAIN (*pc);
34097 if (map_seen != 3)
34099 if (map_seen == 0)
34100 error_at (pragma_tok->location,
34101 "%<#pragma omp target enter data%> must contain at least "
34102 "one %<map%> clause");
34103 return NULL_TREE;
34106 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34107 TREE_TYPE (stmt) = void_type_node;
34108 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34109 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34110 return add_stmt (stmt);
34113 /* OpenMP 4.5:
34114 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34115 structured-block */
34117 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34118 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34124 static tree
34125 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
34126 enum pragma_context context)
34128 bool data_seen = false;
34129 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34131 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34132 const char *p = IDENTIFIER_POINTER (id);
34134 if (strcmp (p, "data") == 0)
34136 cp_lexer_consume_token (parser->lexer);
34137 data_seen = true;
34140 if (!data_seen)
34142 cp_parser_error (parser, "expected %<data%>");
34143 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34144 return NULL_TREE;
34147 if (context == pragma_stmt)
34149 error_at (pragma_tok->location,
34150 "%<#pragma omp target exit data%> may only be "
34151 "used in compound statements");
34152 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34153 return NULL_TREE;
34156 tree clauses
34157 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
34158 "#pragma omp target exit data", pragma_tok);
34159 int map_seen = 0;
34160 for (tree *pc = &clauses; *pc;)
34162 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34163 switch (OMP_CLAUSE_MAP_KIND (*pc))
34165 case GOMP_MAP_FROM:
34166 case GOMP_MAP_ALWAYS_FROM:
34167 case GOMP_MAP_RELEASE:
34168 case GOMP_MAP_DELETE:
34169 map_seen = 3;
34170 break;
34171 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34172 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34173 case GOMP_MAP_ALWAYS_POINTER:
34174 break;
34175 default:
34176 map_seen |= 1;
34177 error_at (OMP_CLAUSE_LOCATION (*pc),
34178 "%<#pragma omp target exit data%> with map-type other "
34179 "than %<from%>, %<release%> or %<delete%> on %<map%>"
34180 " clause");
34181 *pc = OMP_CLAUSE_CHAIN (*pc);
34182 continue;
34184 pc = &OMP_CLAUSE_CHAIN (*pc);
34187 if (map_seen != 3)
34189 if (map_seen == 0)
34190 error_at (pragma_tok->location,
34191 "%<#pragma omp target exit data%> must contain at least "
34192 "one %<map%> clause");
34193 return NULL_TREE;
34196 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
34197 TREE_TYPE (stmt) = void_type_node;
34198 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
34199 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34200 return add_stmt (stmt);
34203 /* OpenMP 4.0:
34204 # pragma omp target update target-update-clause[optseq] new-line */
34206 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
34207 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
34208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34214 static bool
34215 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
34216 enum pragma_context context)
34218 if (context == pragma_stmt)
34220 error_at (pragma_tok->location,
34221 "%<#pragma omp target update%> may only be "
34222 "used in compound statements");
34223 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34224 return false;
34227 tree clauses
34228 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
34229 "#pragma omp target update", pragma_tok);
34230 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
34231 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
34233 error_at (pragma_tok->location,
34234 "%<#pragma omp target update%> must contain at least one "
34235 "%<from%> or %<to%> clauses");
34236 return false;
34239 tree stmt = make_node (OMP_TARGET_UPDATE);
34240 TREE_TYPE (stmt) = void_type_node;
34241 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
34242 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34243 add_stmt (stmt);
34244 return false;
34247 /* OpenMP 4.0:
34248 # pragma omp target target-clause[optseq] new-line
34249 structured-block */
34251 #define OMP_TARGET_CLAUSE_MASK \
34252 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
34260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34262 static bool
34263 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
34264 enum pragma_context context)
34266 tree *pc = NULL, stmt;
34268 if (context != pragma_stmt && context != pragma_compound)
34270 cp_parser_error (parser, "expected declaration specifiers");
34271 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34272 return false;
34275 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34277 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34278 const char *p = IDENTIFIER_POINTER (id);
34279 enum tree_code ccode = ERROR_MARK;
34281 if (strcmp (p, "teams") == 0)
34282 ccode = OMP_TEAMS;
34283 else if (strcmp (p, "parallel") == 0)
34284 ccode = OMP_PARALLEL;
34285 else if (strcmp (p, "simd") == 0)
34286 ccode = OMP_SIMD;
34287 if (ccode != ERROR_MARK)
34289 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
34290 char p_name[sizeof ("#pragma omp target teams distribute "
34291 "parallel for simd")];
34293 cp_lexer_consume_token (parser->lexer);
34294 strcpy (p_name, "#pragma omp target");
34295 if (!flag_openmp) /* flag_openmp_simd */
34297 tree stmt;
34298 switch (ccode)
34300 case OMP_TEAMS:
34301 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
34302 OMP_TARGET_CLAUSE_MASK,
34303 cclauses);
34304 break;
34305 case OMP_PARALLEL:
34306 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34307 OMP_TARGET_CLAUSE_MASK,
34308 cclauses);
34309 break;
34310 case OMP_SIMD:
34311 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
34312 OMP_TARGET_CLAUSE_MASK,
34313 cclauses);
34314 break;
34315 default:
34316 gcc_unreachable ();
34318 return stmt != NULL_TREE;
34320 keep_next_level (true);
34321 tree sb = begin_omp_structured_block (), ret;
34322 unsigned save = cp_parser_begin_omp_structured_block (parser);
34323 switch (ccode)
34325 case OMP_TEAMS:
34326 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
34327 OMP_TARGET_CLAUSE_MASK, cclauses);
34328 break;
34329 case OMP_PARALLEL:
34330 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34331 OMP_TARGET_CLAUSE_MASK, cclauses);
34332 break;
34333 case OMP_SIMD:
34334 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
34335 OMP_TARGET_CLAUSE_MASK, cclauses);
34336 break;
34337 default:
34338 gcc_unreachable ();
34340 cp_parser_end_omp_structured_block (parser, save);
34341 tree body = finish_omp_structured_block (sb);
34342 if (ret == NULL_TREE)
34343 return false;
34344 if (ccode == OMP_TEAMS && !processing_template_decl)
34346 /* For combined target teams, ensure the num_teams and
34347 thread_limit clause expressions are evaluated on the host,
34348 before entering the target construct. */
34349 tree c;
34350 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34351 c; c = OMP_CLAUSE_CHAIN (c))
34352 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
34353 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
34354 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
34356 tree expr = OMP_CLAUSE_OPERAND (c, 0);
34357 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
34358 if (expr == error_mark_node)
34359 continue;
34360 tree tmp = TARGET_EXPR_SLOT (expr);
34361 add_stmt (expr);
34362 OMP_CLAUSE_OPERAND (c, 0) = expr;
34363 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
34364 OMP_CLAUSE_FIRSTPRIVATE);
34365 OMP_CLAUSE_DECL (tc) = tmp;
34366 OMP_CLAUSE_CHAIN (tc)
34367 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
34368 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
34371 tree stmt = make_node (OMP_TARGET);
34372 TREE_TYPE (stmt) = void_type_node;
34373 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
34374 OMP_TARGET_BODY (stmt) = body;
34375 OMP_TARGET_COMBINED (stmt) = 1;
34376 add_stmt (stmt);
34377 pc = &OMP_TARGET_CLAUSES (stmt);
34378 goto check_clauses;
34380 else if (!flag_openmp) /* flag_openmp_simd */
34382 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34383 return false;
34385 else if (strcmp (p, "data") == 0)
34387 cp_lexer_consume_token (parser->lexer);
34388 cp_parser_omp_target_data (parser, pragma_tok);
34389 return true;
34391 else if (strcmp (p, "enter") == 0)
34393 cp_lexer_consume_token (parser->lexer);
34394 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
34395 return false;
34397 else if (strcmp (p, "exit") == 0)
34399 cp_lexer_consume_token (parser->lexer);
34400 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
34401 return false;
34403 else if (strcmp (p, "update") == 0)
34405 cp_lexer_consume_token (parser->lexer);
34406 return cp_parser_omp_target_update (parser, pragma_tok, context);
34410 stmt = make_node (OMP_TARGET);
34411 TREE_TYPE (stmt) = void_type_node;
34413 OMP_TARGET_CLAUSES (stmt)
34414 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
34415 "#pragma omp target", pragma_tok);
34416 pc = &OMP_TARGET_CLAUSES (stmt);
34417 keep_next_level (true);
34418 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
34420 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34421 add_stmt (stmt);
34423 check_clauses:
34424 while (*pc)
34426 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34427 switch (OMP_CLAUSE_MAP_KIND (*pc))
34429 case GOMP_MAP_TO:
34430 case GOMP_MAP_ALWAYS_TO:
34431 case GOMP_MAP_FROM:
34432 case GOMP_MAP_ALWAYS_FROM:
34433 case GOMP_MAP_TOFROM:
34434 case GOMP_MAP_ALWAYS_TOFROM:
34435 case GOMP_MAP_ALLOC:
34436 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34437 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34438 case GOMP_MAP_ALWAYS_POINTER:
34439 break;
34440 default:
34441 error_at (OMP_CLAUSE_LOCATION (*pc),
34442 "%<#pragma omp target%> with map-type other "
34443 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34444 "on %<map%> clause");
34445 *pc = OMP_CLAUSE_CHAIN (*pc);
34446 continue;
34448 pc = &OMP_CLAUSE_CHAIN (*pc);
34450 return true;
34453 /* OpenACC 2.0:
34454 # pragma acc cache (variable-list) new-line
34457 static tree
34458 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
34460 tree stmt, clauses;
34462 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
34463 clauses = finish_omp_clauses (clauses, false);
34465 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
34467 stmt = make_node (OACC_CACHE);
34468 TREE_TYPE (stmt) = void_type_node;
34469 OACC_CACHE_CLAUSES (stmt) = clauses;
34470 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34471 add_stmt (stmt);
34473 return stmt;
34476 /* OpenACC 2.0:
34477 # pragma acc data oacc-data-clause[optseq] new-line
34478 structured-block */
34480 #define OACC_DATA_CLAUSE_MASK \
34481 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
34493 static tree
34494 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
34496 tree stmt, clauses, block;
34497 unsigned int save;
34499 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
34500 "#pragma acc data", pragma_tok);
34502 block = begin_omp_parallel ();
34503 save = cp_parser_begin_omp_structured_block (parser);
34504 cp_parser_statement (parser, NULL_TREE, false, NULL);
34505 cp_parser_end_omp_structured_block (parser, save);
34506 stmt = finish_oacc_data (clauses, block);
34507 return stmt;
34510 /* OpenACC 2.0:
34511 # pragma acc declare oacc-data-clause[optseq] new-line
34514 #define OACC_DECLARE_CLAUSE_MASK \
34515 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
34521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
34522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
34528 static tree
34529 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
34531 tree clauses, stmt;
34532 bool error = false;
34534 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
34535 "#pragma acc declare", pragma_tok, true);
34538 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
34540 error_at (pragma_tok->location,
34541 "no valid clauses specified in %<#pragma acc declare%>");
34542 return NULL_TREE;
34545 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
34547 location_t loc = OMP_CLAUSE_LOCATION (t);
34548 tree decl = OMP_CLAUSE_DECL (t);
34549 if (!DECL_P (decl))
34551 error_at (loc, "array section in %<#pragma acc declare%>");
34552 error = true;
34553 continue;
34555 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
34556 switch (OMP_CLAUSE_MAP_KIND (t))
34558 case GOMP_MAP_FORCE_ALLOC:
34559 case GOMP_MAP_FORCE_TO:
34560 case GOMP_MAP_FORCE_DEVICEPTR:
34561 case GOMP_MAP_DEVICE_RESIDENT:
34562 break;
34564 case GOMP_MAP_POINTER:
34565 /* Generated by c_finish_omp_clauses from array sections;
34566 avoid spurious diagnostics. */
34567 break;
34569 case GOMP_MAP_LINK:
34570 if (!global_bindings_p ()
34571 && (TREE_STATIC (decl)
34572 || !DECL_EXTERNAL (decl)))
34574 error_at (loc,
34575 "%qD must be a global variable in"
34576 "%<#pragma acc declare link%>",
34577 decl);
34578 error = true;
34579 continue;
34581 break;
34583 default:
34584 if (global_bindings_p ())
34586 error_at (loc, "invalid OpenACC clause at file scope");
34587 error = true;
34588 continue;
34590 if (DECL_EXTERNAL (decl))
34592 error_at (loc,
34593 "invalid use of %<extern%> variable %qD "
34594 "in %<#pragma acc declare%>", decl);
34595 error = true;
34596 continue;
34598 else if (TREE_PUBLIC (decl))
34600 error_at (loc,
34601 "invalid use of %<global%> variable %qD "
34602 "in %<#pragma acc declare%>", decl);
34603 error = true;
34604 continue;
34606 break;
34609 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
34610 || lookup_attribute ("omp declare target link",
34611 DECL_ATTRIBUTES (decl)))
34613 error_at (loc, "variable %qD used more than once with "
34614 "%<#pragma acc declare%>", decl);
34615 error = true;
34616 continue;
34619 if (!error)
34621 tree id;
34623 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
34624 id = get_identifier ("omp declare target link");
34625 else
34626 id = get_identifier ("omp declare target");
34628 DECL_ATTRIBUTES (decl)
34629 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
34630 if (global_bindings_p ())
34632 symtab_node *node = symtab_node::get (decl);
34633 if (node != NULL)
34635 node->offloadable = 1;
34636 if (ENABLE_OFFLOADING)
34638 g->have_offload = true;
34639 if (is_a <varpool_node *> (node))
34641 vec_safe_push (offload_vars, decl);
34642 node->force_output = 1;
34650 if (error || global_bindings_p ())
34651 return NULL_TREE;
34653 stmt = make_node (OACC_DECLARE);
34654 TREE_TYPE (stmt) = void_type_node;
34655 OACC_DECLARE_CLAUSES (stmt) = clauses;
34656 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34658 add_stmt (stmt);
34660 return NULL_TREE;
34663 /* OpenACC 2.0:
34664 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
34668 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
34670 LOC is the location of the #pragma token.
34673 #define OACC_ENTER_DATA_CLAUSE_MASK \
34674 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
34680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34682 #define OACC_EXIT_DATA_CLAUSE_MASK \
34683 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
34687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34689 static tree
34690 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
34691 bool enter)
34693 tree stmt, clauses;
34695 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
34696 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34698 cp_parser_error (parser, enter
34699 ? "expected %<data%> in %<#pragma acc enter data%>"
34700 : "expected %<data%> in %<#pragma acc exit data%>");
34701 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34702 return NULL_TREE;
34705 const char *p =
34706 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
34707 if (strcmp (p, "data") != 0)
34709 cp_parser_error (parser, "invalid pragma");
34710 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34711 return NULL_TREE;
34714 cp_lexer_consume_token (parser->lexer);
34716 if (enter)
34717 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
34718 "#pragma acc enter data", pragma_tok);
34719 else
34720 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
34721 "#pragma acc exit data", pragma_tok);
34723 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
34725 error_at (pragma_tok->location,
34726 "%<#pragma acc enter data%> has no data movement clause");
34727 return NULL_TREE;
34730 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
34731 TREE_TYPE (stmt) = void_type_node;
34732 OMP_STANDALONE_CLAUSES (stmt) = clauses;
34733 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34734 add_stmt (stmt);
34735 return stmt;
34738 /* OpenACC 2.0:
34739 # pragma acc loop oacc-loop-clause[optseq] new-line
34740 structured-block */
34742 #define OACC_LOOP_CLAUSE_MASK \
34743 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
34744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
34745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
34746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
34747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
34748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
34749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
34750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
34751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
34752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
34754 static tree
34755 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
34756 omp_clause_mask mask, tree *cclauses)
34758 strcat (p_name, " loop");
34759 mask |= OACC_LOOP_CLAUSE_MASK;
34761 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
34762 cclauses == NULL);
34763 if (cclauses)
34765 clauses = c_oacc_split_loop_clauses (clauses, cclauses);
34766 if (*cclauses)
34767 finish_omp_clauses (*cclauses, false);
34768 if (clauses)
34769 finish_omp_clauses (clauses, false);
34772 tree block = begin_omp_structured_block ();
34773 int save = cp_parser_begin_omp_structured_block (parser);
34774 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
34775 cp_parser_end_omp_structured_block (parser, save);
34776 add_stmt (finish_omp_structured_block (block));
34778 return stmt;
34781 /* OpenACC 2.0:
34782 # pragma acc kernels oacc-kernels-clause[optseq] new-line
34783 structured-block
34787 # pragma acc parallel oacc-parallel-clause[optseq] new-line
34788 structured-block
34791 #define OACC_KERNELS_CLAUSE_MASK \
34792 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
34798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
34805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34807 #define OACC_PARALLEL_CLAUSE_MASK \
34808 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
34814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
34816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
34818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
34819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
34824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
34825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
34826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
34827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34829 static tree
34830 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
34831 char *p_name)
34833 omp_clause_mask mask;
34834 enum tree_code code;
34835 switch (pragma_tok->pragma_kind)
34837 case PRAGMA_OACC_KERNELS:
34838 strcat (p_name, " kernels");
34839 mask = OACC_KERNELS_CLAUSE_MASK;
34840 code = OACC_KERNELS;
34841 break;
34842 case PRAGMA_OACC_PARALLEL:
34843 strcat (p_name, " parallel");
34844 mask = OACC_PARALLEL_CLAUSE_MASK;
34845 code = OACC_PARALLEL;
34846 break;
34847 default:
34848 gcc_unreachable ();
34851 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34853 const char *p
34854 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
34855 if (strcmp (p, "loop") == 0)
34857 cp_lexer_consume_token (parser->lexer);
34858 mask |= OACC_LOOP_CLAUSE_MASK;
34860 tree block = begin_omp_parallel ();
34861 tree clauses;
34862 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses);
34863 return finish_omp_construct (code, block, clauses);
34867 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
34869 tree block = begin_omp_parallel ();
34870 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34871 cp_parser_statement (parser, NULL_TREE, false, NULL);
34872 cp_parser_end_omp_structured_block (parser, save);
34873 return finish_omp_construct (code, block, clauses);
34876 /* OpenACC 2.0:
34877 # pragma acc update oacc-update-clause[optseq] new-line
34880 #define OACC_UPDATE_CLAUSE_MASK \
34881 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
34883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
34884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
34886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
34888 static tree
34889 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
34891 tree stmt, clauses;
34893 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
34894 "#pragma acc update", pragma_tok);
34896 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
34898 error_at (pragma_tok->location,
34899 "%<#pragma acc update%> must contain at least one "
34900 "%<device%> or %<host%> or %<self%> clause");
34901 return NULL_TREE;
34904 stmt = make_node (OACC_UPDATE);
34905 TREE_TYPE (stmt) = void_type_node;
34906 OACC_UPDATE_CLAUSES (stmt) = clauses;
34907 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34908 add_stmt (stmt);
34909 return stmt;
34912 /* OpenACC 2.0:
34913 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
34915 LOC is the location of the #pragma token.
34918 #define OACC_WAIT_CLAUSE_MASK \
34919 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
34921 static tree
34922 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
34924 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
34925 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34927 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34928 list = cp_parser_oacc_wait_list (parser, loc, list);
34930 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
34931 "#pragma acc wait", pragma_tok);
34933 stmt = c_finish_oacc_wait (loc, list, clauses);
34935 return stmt;
34938 /* OpenMP 4.0:
34939 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
34941 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
34942 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
34946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
34947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
34949 static void
34950 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
34951 enum pragma_context context)
34953 bool first_p = parser->omp_declare_simd == NULL;
34954 cp_omp_declare_simd_data data;
34955 if (first_p)
34957 data.error_seen = false;
34958 data.fndecl_seen = false;
34959 data.tokens = vNULL;
34960 parser->omp_declare_simd = &data;
34962 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
34963 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
34964 cp_lexer_consume_token (parser->lexer);
34965 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34966 parser->omp_declare_simd->error_seen = true;
34967 cp_parser_require_pragma_eol (parser, pragma_tok);
34968 struct cp_token_cache *cp
34969 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
34970 parser->omp_declare_simd->tokens.safe_push (cp);
34971 if (first_p)
34973 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
34974 cp_parser_pragma (parser, context);
34975 switch (context)
34977 case pragma_external:
34978 cp_parser_declaration (parser);
34979 break;
34980 case pragma_member:
34981 cp_parser_member_declaration (parser);
34982 break;
34983 case pragma_objc_icode:
34984 cp_parser_block_declaration (parser, /*statement_p=*/false);
34985 break;
34986 default:
34987 cp_parser_declaration_statement (parser);
34988 break;
34990 if (parser->omp_declare_simd
34991 && !parser->omp_declare_simd->error_seen
34992 && !parser->omp_declare_simd->fndecl_seen)
34993 error_at (pragma_tok->location,
34994 "%<#pragma omp declare simd%> not immediately followed by "
34995 "function declaration or definition");
34996 data.tokens.release ();
34997 parser->omp_declare_simd = NULL;
35001 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
35002 This function is modelled similar to the late parsing of omp declare
35003 simd. */
35005 static tree
35006 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
35008 struct cp_token_cache *ce;
35009 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
35010 int ii = 0;
35012 if (parser->omp_declare_simd != NULL
35013 || lookup_attribute ("simd", attrs))
35015 error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35016 "used in the same function marked as a Cilk Plus SIMD-enabled "
35017 " function");
35018 parser->cilk_simd_fn_info->tokens.release ();
35019 XDELETE (parser->cilk_simd_fn_info);
35020 parser->cilk_simd_fn_info = NULL;
35021 return attrs;
35023 if (!info->error_seen && info->fndecl_seen)
35025 error ("vector attribute not immediately followed by a single function"
35026 " declaration or definition");
35027 info->error_seen = true;
35029 if (info->error_seen)
35030 return attrs;
35032 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
35034 tree c, cl;
35036 cp_parser_push_lexer_for_tokens (parser, ce);
35037 parser->lexer->in_pragma = true;
35038 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
35039 "SIMD-enabled functions attribute",
35040 NULL);
35041 cp_parser_pop_lexer (parser);
35042 if (cl)
35043 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35045 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
35046 TREE_CHAIN (c) = attrs;
35047 attrs = c;
35049 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35050 TREE_CHAIN (c) = attrs;
35051 if (processing_template_decl)
35052 ATTR_IS_DEPENDENT (c) = 1;
35053 attrs = c;
35055 info->fndecl_seen = true;
35056 parser->cilk_simd_fn_info->tokens.release ();
35057 XDELETE (parser->cilk_simd_fn_info);
35058 parser->cilk_simd_fn_info = NULL;
35059 return attrs;
35062 /* Finalize #pragma omp declare simd clauses after direct declarator has
35063 been parsed, and put that into "omp declare simd" attribute. */
35065 static tree
35066 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
35068 struct cp_token_cache *ce;
35069 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
35070 int i;
35072 if (!data->error_seen && data->fndecl_seen)
35074 error ("%<#pragma omp declare simd%> not immediately followed by "
35075 "a single function declaration or definition");
35076 data->error_seen = true;
35077 return attrs;
35079 if (data->error_seen)
35080 return attrs;
35082 FOR_EACH_VEC_ELT (data->tokens, i, ce)
35084 tree c, cl;
35086 cp_parser_push_lexer_for_tokens (parser, ce);
35087 parser->lexer->in_pragma = true;
35088 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35089 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35090 cp_lexer_consume_token (parser->lexer);
35091 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
35092 "#pragma omp declare simd", pragma_tok);
35093 cp_parser_pop_lexer (parser);
35094 if (cl)
35095 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35096 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35097 TREE_CHAIN (c) = attrs;
35098 if (processing_template_decl)
35099 ATTR_IS_DEPENDENT (c) = 1;
35100 attrs = c;
35103 data->fndecl_seen = true;
35104 return attrs;
35108 /* OpenMP 4.0:
35109 # pragma omp declare target new-line
35110 declarations and definitions
35111 # pragma omp end declare target new-line
35113 OpenMP 4.5:
35114 # pragma omp declare target ( extended-list ) new-line
35116 # pragma omp declare target declare-target-clauses[seq] new-line */
35118 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
35119 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35122 static void
35123 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
35125 tree clauses = NULL_TREE;
35126 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35127 clauses
35128 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
35129 "#pragma omp declare target", pragma_tok);
35130 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35132 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35133 clauses);
35134 clauses = finish_omp_clauses (clauses, true);
35135 cp_parser_require_pragma_eol (parser, pragma_tok);
35137 else
35139 cp_parser_require_pragma_eol (parser, pragma_tok);
35140 scope_chain->omp_declare_target_attribute++;
35141 return;
35143 if (scope_chain->omp_declare_target_attribute)
35144 error_at (pragma_tok->location,
35145 "%<#pragma omp declare target%> with clauses in between "
35146 "%<#pragma omp declare target%> without clauses and "
35147 "%<#pragma omp end declare target%>");
35148 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
35150 tree t = OMP_CLAUSE_DECL (c), id;
35151 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
35152 tree at2 = lookup_attribute ("omp declare target link",
35153 DECL_ATTRIBUTES (t));
35154 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
35156 id = get_identifier ("omp declare target link");
35157 std::swap (at1, at2);
35159 else
35160 id = get_identifier ("omp declare target");
35161 if (at2)
35163 error_at (OMP_CLAUSE_LOCATION (c),
35164 "%qD specified both in declare target %<link%> and %<to%>"
35165 " clauses", t);
35166 continue;
35168 if (!at1)
35170 symtab_node *node = symtab_node::get (t);
35171 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
35172 if (node != NULL)
35174 node->offloadable = 1;
35175 if (ENABLE_OFFLOADING)
35177 g->have_offload = true;
35178 if (is_a <varpool_node *> (node))
35180 vec_safe_push (offload_vars, t);
35181 node->force_output = 1;
35189 static void
35190 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
35192 const char *p = "";
35193 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35195 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35196 p = IDENTIFIER_POINTER (id);
35198 if (strcmp (p, "declare") == 0)
35200 cp_lexer_consume_token (parser->lexer);
35201 p = "";
35202 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35204 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35205 p = IDENTIFIER_POINTER (id);
35207 if (strcmp (p, "target") == 0)
35208 cp_lexer_consume_token (parser->lexer);
35209 else
35211 cp_parser_error (parser, "expected %<target%>");
35212 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35213 return;
35216 else
35218 cp_parser_error (parser, "expected %<declare%>");
35219 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35220 return;
35222 cp_parser_require_pragma_eol (parser, pragma_tok);
35223 if (!scope_chain->omp_declare_target_attribute)
35224 error_at (pragma_tok->location,
35225 "%<#pragma omp end declare target%> without corresponding "
35226 "%<#pragma omp declare target%>");
35227 else
35228 scope_chain->omp_declare_target_attribute--;
35231 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
35232 expression and optional initializer clause of
35233 #pragma omp declare reduction. We store the expression(s) as
35234 either 3, 6 or 7 special statements inside of the artificial function's
35235 body. The first two statements are DECL_EXPRs for the artificial
35236 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35237 expression that uses those variables.
35238 If there was any INITIALIZER clause, this is followed by further statements,
35239 the fourth and fifth statements are DECL_EXPRs for the artificial
35240 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
35241 constructor variant (first token after open paren is not omp_priv),
35242 then the sixth statement is a statement with the function call expression
35243 that uses the OMP_PRIV and optionally OMP_ORIG variable.
35244 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35245 to initialize the OMP_PRIV artificial variable and there is seventh
35246 statement, a DECL_EXPR of the OMP_PRIV statement again. */
35248 static bool
35249 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
35251 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
35252 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
35253 type = TREE_TYPE (type);
35254 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
35255 DECL_ARTIFICIAL (omp_out) = 1;
35256 pushdecl (omp_out);
35257 add_decl_expr (omp_out);
35258 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
35259 DECL_ARTIFICIAL (omp_in) = 1;
35260 pushdecl (omp_in);
35261 add_decl_expr (omp_in);
35262 tree combiner;
35263 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
35265 keep_next_level (true);
35266 tree block = begin_omp_structured_block ();
35267 combiner = cp_parser_expression (parser);
35268 finish_expr_stmt (combiner);
35269 block = finish_omp_structured_block (block);
35270 add_stmt (block);
35272 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35273 return false;
35275 const char *p = "";
35276 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35278 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35279 p = IDENTIFIER_POINTER (id);
35282 if (strcmp (p, "initializer") == 0)
35284 cp_lexer_consume_token (parser->lexer);
35285 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35286 return false;
35288 p = "";
35289 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35291 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35292 p = IDENTIFIER_POINTER (id);
35295 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
35296 DECL_ARTIFICIAL (omp_priv) = 1;
35297 pushdecl (omp_priv);
35298 add_decl_expr (omp_priv);
35299 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
35300 DECL_ARTIFICIAL (omp_orig) = 1;
35301 pushdecl (omp_orig);
35302 add_decl_expr (omp_orig);
35304 keep_next_level (true);
35305 block = begin_omp_structured_block ();
35307 bool ctor = false;
35308 if (strcmp (p, "omp_priv") == 0)
35310 bool is_direct_init, is_non_constant_init;
35311 ctor = true;
35312 cp_lexer_consume_token (parser->lexer);
35313 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
35314 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
35315 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35316 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
35317 == CPP_CLOSE_PAREN
35318 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
35319 == CPP_CLOSE_PAREN))
35321 finish_omp_structured_block (block);
35322 error ("invalid initializer clause");
35323 return false;
35325 initializer = cp_parser_initializer (parser, &is_direct_init,
35326 &is_non_constant_init);
35327 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
35328 NULL_TREE, LOOKUP_ONLYCONVERTING);
35330 else
35332 cp_parser_parse_tentatively (parser);
35333 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
35334 /*check_dependency_p=*/true,
35335 /*template_p=*/NULL,
35336 /*declarator_p=*/false,
35337 /*optional_p=*/false);
35338 vec<tree, va_gc> *args;
35339 if (fn_name == error_mark_node
35340 || cp_parser_error_occurred (parser)
35341 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35342 || ((args = cp_parser_parenthesized_expression_list
35343 (parser, non_attr, /*cast_p=*/false,
35344 /*allow_expansion_p=*/true,
35345 /*non_constant_p=*/NULL)),
35346 cp_parser_error_occurred (parser)))
35348 finish_omp_structured_block (block);
35349 cp_parser_abort_tentative_parse (parser);
35350 cp_parser_error (parser, "expected id-expression (arguments)");
35351 return false;
35353 unsigned int i;
35354 tree arg;
35355 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
35356 if (arg == omp_priv
35357 || (TREE_CODE (arg) == ADDR_EXPR
35358 && TREE_OPERAND (arg, 0) == omp_priv))
35359 break;
35360 cp_parser_abort_tentative_parse (parser);
35361 if (arg == NULL_TREE)
35362 error ("one of the initializer call arguments should be %<omp_priv%>"
35363 " or %<&omp_priv%>");
35364 initializer = cp_parser_postfix_expression (parser, false, false, false,
35365 false, NULL);
35366 finish_expr_stmt (initializer);
35369 block = finish_omp_structured_block (block);
35370 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
35371 add_stmt (block);
35373 if (ctor)
35374 add_decl_expr (omp_orig);
35376 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35377 return false;
35380 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
35381 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
35383 return true;
35386 /* OpenMP 4.0
35387 #pragma omp declare reduction (reduction-id : typename-list : expression) \
35388 initializer-clause[opt] new-line
35390 initializer-clause:
35391 initializer (omp_priv initializer)
35392 initializer (function-name (argument-list)) */
35394 static void
35395 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
35396 enum pragma_context)
35398 auto_vec<tree> types;
35399 enum tree_code reduc_code = ERROR_MARK;
35400 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
35401 unsigned int i;
35402 cp_token *first_token;
35403 cp_token_cache *cp;
35404 int errs;
35405 void *p;
35407 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
35408 p = obstack_alloc (&declarator_obstack, 0);
35410 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35411 goto fail;
35413 switch (cp_lexer_peek_token (parser->lexer)->type)
35415 case CPP_PLUS:
35416 reduc_code = PLUS_EXPR;
35417 break;
35418 case CPP_MULT:
35419 reduc_code = MULT_EXPR;
35420 break;
35421 case CPP_MINUS:
35422 reduc_code = MINUS_EXPR;
35423 break;
35424 case CPP_AND:
35425 reduc_code = BIT_AND_EXPR;
35426 break;
35427 case CPP_XOR:
35428 reduc_code = BIT_XOR_EXPR;
35429 break;
35430 case CPP_OR:
35431 reduc_code = BIT_IOR_EXPR;
35432 break;
35433 case CPP_AND_AND:
35434 reduc_code = TRUTH_ANDIF_EXPR;
35435 break;
35436 case CPP_OR_OR:
35437 reduc_code = TRUTH_ORIF_EXPR;
35438 break;
35439 case CPP_NAME:
35440 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
35441 break;
35442 default:
35443 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
35444 "%<|%>, %<&&%>, %<||%> or identifier");
35445 goto fail;
35448 if (reduc_code != ERROR_MARK)
35449 cp_lexer_consume_token (parser->lexer);
35451 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
35452 if (reduc_id == error_mark_node)
35453 goto fail;
35455 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35456 goto fail;
35458 /* Types may not be defined in declare reduction type list. */
35459 const char *saved_message;
35460 saved_message = parser->type_definition_forbidden_message;
35461 parser->type_definition_forbidden_message
35462 = G_("types may not be defined in declare reduction type list");
35463 bool saved_colon_corrects_to_scope_p;
35464 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
35465 parser->colon_corrects_to_scope_p = false;
35466 bool saved_colon_doesnt_start_class_def_p;
35467 saved_colon_doesnt_start_class_def_p
35468 = parser->colon_doesnt_start_class_def_p;
35469 parser->colon_doesnt_start_class_def_p = true;
35471 while (true)
35473 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35474 type = cp_parser_type_id (parser);
35475 if (type == error_mark_node)
35477 else if (ARITHMETIC_TYPE_P (type)
35478 && (orig_reduc_id == NULL_TREE
35479 || (TREE_CODE (type) != COMPLEX_TYPE
35480 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
35481 "min") == 0
35482 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
35483 "max") == 0))))
35484 error_at (loc, "predeclared arithmetic type %qT in "
35485 "%<#pragma omp declare reduction%>", type);
35486 else if (TREE_CODE (type) == FUNCTION_TYPE
35487 || TREE_CODE (type) == METHOD_TYPE
35488 || TREE_CODE (type) == ARRAY_TYPE)
35489 error_at (loc, "function or array type %qT in "
35490 "%<#pragma omp declare reduction%>", type);
35491 else if (TREE_CODE (type) == REFERENCE_TYPE)
35492 error_at (loc, "reference type %qT in "
35493 "%<#pragma omp declare reduction%>", type);
35494 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
35495 error_at (loc, "const, volatile or __restrict qualified type %qT in "
35496 "%<#pragma omp declare reduction%>", type);
35497 else
35498 types.safe_push (type);
35500 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35501 cp_lexer_consume_token (parser->lexer);
35502 else
35503 break;
35506 /* Restore the saved message. */
35507 parser->type_definition_forbidden_message = saved_message;
35508 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
35509 parser->colon_doesnt_start_class_def_p
35510 = saved_colon_doesnt_start_class_def_p;
35512 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
35513 || types.is_empty ())
35515 fail:
35516 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35517 goto done;
35520 first_token = cp_lexer_peek_token (parser->lexer);
35521 cp = NULL;
35522 errs = errorcount;
35523 FOR_EACH_VEC_ELT (types, i, type)
35525 tree fntype
35526 = build_function_type_list (void_type_node,
35527 cp_build_reference_type (type, false),
35528 NULL_TREE);
35529 tree this_reduc_id = reduc_id;
35530 if (!dependent_type_p (type))
35531 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
35532 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
35533 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
35534 DECL_ARTIFICIAL (fndecl) = 1;
35535 DECL_EXTERNAL (fndecl) = 1;
35536 DECL_DECLARED_INLINE_P (fndecl) = 1;
35537 DECL_IGNORED_P (fndecl) = 1;
35538 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
35539 DECL_ATTRIBUTES (fndecl)
35540 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
35541 DECL_ATTRIBUTES (fndecl));
35542 if (processing_template_decl)
35543 fndecl = push_template_decl (fndecl);
35544 bool block_scope = false;
35545 tree block = NULL_TREE;
35546 if (current_function_decl)
35548 block_scope = true;
35549 DECL_CONTEXT (fndecl) = global_namespace;
35550 if (!processing_template_decl)
35551 pushdecl (fndecl);
35553 else if (current_class_type)
35555 if (cp == NULL)
35557 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35558 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35559 cp_lexer_consume_token (parser->lexer);
35560 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35561 goto fail;
35562 cp = cp_token_cache_new (first_token,
35563 cp_lexer_peek_nth_token (parser->lexer,
35564 2));
35566 DECL_STATIC_FUNCTION_P (fndecl) = 1;
35567 finish_member_declaration (fndecl);
35568 DECL_PENDING_INLINE_INFO (fndecl) = cp;
35569 DECL_PENDING_INLINE_P (fndecl) = 1;
35570 vec_safe_push (unparsed_funs_with_definitions, fndecl);
35571 continue;
35573 else
35575 DECL_CONTEXT (fndecl) = current_namespace;
35576 pushdecl (fndecl);
35578 if (!block_scope)
35579 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
35580 else
35581 block = begin_omp_structured_block ();
35582 if (cp)
35584 cp_parser_push_lexer_for_tokens (parser, cp);
35585 parser->lexer->in_pragma = true;
35587 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
35589 if (!block_scope)
35590 finish_function (0);
35591 else
35592 DECL_CONTEXT (fndecl) = current_function_decl;
35593 if (cp)
35594 cp_parser_pop_lexer (parser);
35595 goto fail;
35597 if (cp)
35598 cp_parser_pop_lexer (parser);
35599 if (!block_scope)
35600 finish_function (0);
35601 else
35603 DECL_CONTEXT (fndecl) = current_function_decl;
35604 block = finish_omp_structured_block (block);
35605 if (TREE_CODE (block) == BIND_EXPR)
35606 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
35607 else if (TREE_CODE (block) == STATEMENT_LIST)
35608 DECL_SAVED_TREE (fndecl) = block;
35609 if (processing_template_decl)
35610 add_decl_expr (fndecl);
35612 cp_check_omp_declare_reduction (fndecl);
35613 if (cp == NULL && types.length () > 1)
35614 cp = cp_token_cache_new (first_token,
35615 cp_lexer_peek_nth_token (parser->lexer, 2));
35616 if (errs != errorcount)
35617 break;
35620 cp_parser_require_pragma_eol (parser, pragma_tok);
35622 done:
35623 /* Free any declarators allocated. */
35624 obstack_free (&declarator_obstack, p);
35627 /* OpenMP 4.0
35628 #pragma omp declare simd declare-simd-clauses[optseq] new-line
35629 #pragma omp declare reduction (reduction-id : typename-list : expression) \
35630 initializer-clause[opt] new-line
35631 #pragma omp declare target new-line */
35633 static void
35634 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
35635 enum pragma_context context)
35637 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35639 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35640 const char *p = IDENTIFIER_POINTER (id);
35642 if (strcmp (p, "simd") == 0)
35644 cp_lexer_consume_token (parser->lexer);
35645 cp_parser_omp_declare_simd (parser, pragma_tok,
35646 context);
35647 return;
35649 cp_ensure_no_omp_declare_simd (parser);
35650 if (strcmp (p, "reduction") == 0)
35652 cp_lexer_consume_token (parser->lexer);
35653 cp_parser_omp_declare_reduction (parser, pragma_tok,
35654 context);
35655 return;
35657 if (!flag_openmp) /* flag_openmp_simd */
35659 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35660 return;
35662 if (strcmp (p, "target") == 0)
35664 cp_lexer_consume_token (parser->lexer);
35665 cp_parser_omp_declare_target (parser, pragma_tok);
35666 return;
35669 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
35670 "or %<target%>");
35671 cp_parser_require_pragma_eol (parser, pragma_tok);
35674 /* OpenMP 4.5:
35675 #pragma omp taskloop taskloop-clause[optseq] new-line
35676 for-loop
35678 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
35679 for-loop */
35681 #define OMP_TASKLOOP_CLAUSE_MASK \
35682 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
35688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
35689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
35690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
35695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35697 static tree
35698 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
35699 char *p_name, omp_clause_mask mask, tree *cclauses)
35701 tree clauses, sb, ret;
35702 unsigned int save;
35703 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35705 strcat (p_name, " taskloop");
35706 mask |= OMP_TASKLOOP_CLAUSE_MASK;
35708 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35710 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35711 const char *p = IDENTIFIER_POINTER (id);
35713 if (strcmp (p, "simd") == 0)
35715 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35716 if (cclauses == NULL)
35717 cclauses = cclauses_buf;
35719 cp_lexer_consume_token (parser->lexer);
35720 if (!flag_openmp) /* flag_openmp_simd */
35721 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35722 cclauses);
35723 sb = begin_omp_structured_block ();
35724 save = cp_parser_begin_omp_structured_block (parser);
35725 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35726 cclauses);
35727 cp_parser_end_omp_structured_block (parser, save);
35728 tree body = finish_omp_structured_block (sb);
35729 if (ret == NULL)
35730 return ret;
35731 ret = make_node (OMP_TASKLOOP);
35732 TREE_TYPE (ret) = void_type_node;
35733 OMP_FOR_BODY (ret) = body;
35734 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
35735 SET_EXPR_LOCATION (ret, loc);
35736 add_stmt (ret);
35737 return ret;
35740 if (!flag_openmp) /* flag_openmp_simd */
35742 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35743 return NULL_TREE;
35746 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35747 cclauses == NULL);
35748 if (cclauses)
35750 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
35751 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
35754 sb = begin_omp_structured_block ();
35755 save = cp_parser_begin_omp_structured_block (parser);
35757 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses);
35759 cp_parser_end_omp_structured_block (parser, save);
35760 add_stmt (finish_omp_structured_block (sb));
35762 return ret;
35766 /* OpenACC 2.0:
35767 # pragma acc routine oacc-routine-clause[optseq] new-line
35768 function-definition
35770 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
35773 #define OACC_ROUTINE_CLAUSE_MASK \
35774 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
35780 /* Parse the OpenACC routine pragma. This has an optional '( name )'
35781 component, which must resolve to a declared namespace-scope
35782 function. The clauses are either processed directly (for a named
35783 function), or defered until the immediatley following declaration
35784 is parsed. */
35786 static void
35787 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
35788 enum pragma_context context)
35790 bool first_p = parser->oacc_routine == NULL;
35791 location_t loc = pragma_tok->location;
35792 cp_omp_declare_simd_data data;
35793 if (first_p)
35795 data.error_seen = false;
35796 data.fndecl_seen = false;
35797 data.tokens = vNULL;
35798 data.clauses = NULL_TREE;
35799 parser->oacc_routine = &data;
35802 tree decl = NULL_TREE;
35803 /* Create a dummy claue, to record location. */
35804 tree c_head = build_omp_clause (pragma_tok->location, OMP_CLAUSE_SEQ);
35806 if (context != pragma_external)
35808 cp_parser_error (parser, "%<#pragma acc routine%> not at file scope");
35809 parser->oacc_routine->error_seen = true;
35810 parser->oacc_routine = NULL;
35811 return;
35814 /* Look for optional '( name )'. */
35815 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35817 if (!first_p)
35819 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35820 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35821 cp_lexer_consume_token (parser->lexer);
35822 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35823 parser->oacc_routine->error_seen = true;
35824 cp_parser_require_pragma_eol (parser, pragma_tok);
35826 error_at (OMP_CLAUSE_LOCATION (parser->oacc_routine->clauses),
35827 "%<#pragma oacc routine%> not followed by a single "
35828 "function declaration or definition");
35830 parser->oacc_routine->error_seen = true;
35831 return;
35834 cp_lexer_consume_token (parser->lexer);
35835 cp_token *token = cp_lexer_peek_token (parser->lexer);
35837 /* We parse the name as an id-expression. If it resolves to
35838 anything other than a non-overloaded function at namespace
35839 scope, it's an error. */
35840 tree id = cp_parser_id_expression (parser,
35841 /*template_keyword_p=*/false,
35842 /*check_dependency_p=*/false,
35843 /*template_p=*/NULL,
35844 /*declarator_p=*/false,
35845 /*optional_p=*/false);
35846 decl = cp_parser_lookup_name_simple (parser, id, token->location);
35847 if (id != error_mark_node && decl == error_mark_node)
35848 cp_parser_name_lookup_error (parser, id, decl, NLE_NULL,
35849 token->location);
35851 if (decl == error_mark_node
35852 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35854 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35855 parser->oacc_routine = NULL;
35856 return;
35859 /* Build a chain of clauses. */
35860 parser->lexer->in_pragma = true;
35861 tree clauses = NULL_TREE;
35862 clauses = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
35863 "#pragma acc routine",
35864 cp_lexer_peek_token
35865 (parser->lexer));
35867 /* Force clauses to be non-null, by attaching context to it. */
35868 clauses = tree_cons (c_head, clauses, NULL_TREE);
35870 if (decl && is_overloaded_fn (decl)
35871 && (TREE_CODE (decl) != FUNCTION_DECL
35872 || DECL_FUNCTION_TEMPLATE_P (decl)))
35874 error_at (loc, "%<#pragma acc routine%> names a set of overloads");
35875 parser->oacc_routine = NULL;
35876 return;
35879 /* Perhaps we should use the same rule as declarations in different
35880 namespaces? */
35881 if (!DECL_NAMESPACE_SCOPE_P (decl))
35883 error_at (loc, "%<#pragma acc routine%> does not refer to a "
35884 "namespace scope function");
35885 parser->oacc_routine = NULL;
35886 return;
35889 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
35891 error_at (loc,
35892 "%<#pragma acc routine%> does not refer to a function");
35893 parser->oacc_routine = NULL;
35894 return;
35897 data.clauses = clauses;
35899 cp_finalize_oacc_routine (parser, decl, false);
35900 data.tokens.release ();
35901 parser->oacc_routine = NULL;
35903 else
35905 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35906 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35907 cp_lexer_consume_token (parser->lexer);
35908 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35909 parser->oacc_routine->error_seen = true;
35910 cp_parser_require_pragma_eol (parser, pragma_tok);
35912 struct cp_token_cache *cp
35913 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35914 parser->oacc_routine->tokens.safe_push (cp);
35916 if (first_p)
35917 parser->oacc_routine->clauses = c_head;
35919 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35920 cp_parser_pragma (parser, context);
35922 if (first_p)
35924 /* Create an empty list of clauses. */
35925 parser->oacc_routine->clauses = tree_cons (c_head, NULL_TREE,
35926 NULL_TREE);
35927 cp_parser_declaration (parser);
35929 if (parser->oacc_routine
35930 && !parser->oacc_routine->error_seen
35931 && !parser->oacc_routine->fndecl_seen)
35932 error_at (loc, "%<#pragma acc routine%> not followed by "
35933 "function declaration or definition");
35935 data.tokens.release ();
35936 parser->oacc_routine = NULL;
35941 /* Finalize #pragma acc routine clauses after direct declarator has
35942 been parsed, and put that into "oacc routine" attribute. */
35944 static tree
35945 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
35947 struct cp_token_cache *ce;
35948 cp_omp_declare_simd_data *data = parser->oacc_routine;
35949 tree cl, clauses = parser->oacc_routine->clauses;
35950 location_t loc;
35952 loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
35954 if ((!data->error_seen && data->fndecl_seen)
35955 || data->tokens.length () != 1)
35957 error_at (loc, "%<#pragma oacc routine%> not followed by a single "
35958 "function declaration or definition");
35959 data->error_seen = true;
35960 return attrs;
35962 if (data->error_seen)
35963 return attrs;
35965 ce = data->tokens[0];
35967 cp_parser_push_lexer_for_tokens (parser, ce);
35968 parser->lexer->in_pragma = true;
35969 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35971 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35972 cl = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
35973 "#pragma oacc routine", pragma_tok);
35974 cp_parser_pop_lexer (parser);
35976 tree c_head = build_omp_clause (loc, OMP_CLAUSE_SEQ);
35978 /* Force clauses to be non-null, by attaching context to it. */
35979 parser->oacc_routine->clauses = tree_cons (c_head, cl, NULL_TREE);
35981 data->fndecl_seen = true;
35982 return attrs;
35985 /* Apply any saved OpenACC routine clauses to a just-parsed
35986 declaration. */
35988 static void
35989 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
35991 if (__builtin_expect (parser->oacc_routine != NULL, 0))
35993 tree clauses = parser->oacc_routine->clauses;
35994 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
35996 if (parser->oacc_routine->error_seen)
35997 return;
35999 if (fndecl == error_mark_node)
36001 parser->oacc_routine = NULL;
36002 return;
36005 if (TREE_CODE (fndecl) != FUNCTION_DECL)
36007 cp_ensure_no_oacc_routine (parser);
36008 return;
36011 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL)
36013 error_at (loc,
36014 "%<#pragma acc routine%> not followed by single function");
36015 parser->oacc_routine = NULL;
36018 if (get_oacc_fn_attrib (fndecl))
36020 error_at (loc, "%<#pragma acc routine%> already applied to %D",
36021 fndecl);
36022 parser->oacc_routine = NULL;
36025 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
36027 error_at (loc, "%<#pragma acc routine%> must be applied before %s",
36028 TREE_USED (fndecl) ? "use" : "definition");
36029 parser->oacc_routine = NULL;
36032 /* Process for function attrib */
36033 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
36034 replace_oacc_fn_attrib (fndecl, dims);
36036 /* Add an "omp target" attribute. */
36037 DECL_ATTRIBUTES (fndecl)
36038 = tree_cons (get_identifier ("omp declare target"),
36039 NULL_TREE, DECL_ATTRIBUTES (fndecl));
36043 /* Main entry point to OpenMP statement pragmas. */
36045 static void
36046 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
36048 tree stmt;
36049 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
36050 omp_clause_mask mask (0);
36052 switch (pragma_tok->pragma_kind)
36054 case PRAGMA_OACC_ATOMIC:
36055 cp_parser_omp_atomic (parser, pragma_tok);
36056 return;
36057 case PRAGMA_OACC_CACHE:
36058 stmt = cp_parser_oacc_cache (parser, pragma_tok);
36059 break;
36060 case PRAGMA_OACC_DATA:
36061 stmt = cp_parser_oacc_data (parser, pragma_tok);
36062 break;
36063 case PRAGMA_OACC_ENTER_DATA:
36064 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
36065 break;
36066 case PRAGMA_OACC_EXIT_DATA:
36067 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
36068 break;
36069 case PRAGMA_OACC_KERNELS:
36070 case PRAGMA_OACC_PARALLEL:
36071 strcpy (p_name, "#pragma acc");
36072 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name);
36073 break;
36074 case PRAGMA_OACC_LOOP:
36075 strcpy (p_name, "#pragma acc");
36076 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL);
36077 break;
36078 case PRAGMA_OACC_UPDATE:
36079 stmt = cp_parser_oacc_update (parser, pragma_tok);
36080 break;
36081 case PRAGMA_OACC_WAIT:
36082 stmt = cp_parser_oacc_wait (parser, pragma_tok);
36083 break;
36084 case PRAGMA_OMP_ATOMIC:
36085 cp_parser_omp_atomic (parser, pragma_tok);
36086 return;
36087 case PRAGMA_OMP_CRITICAL:
36088 stmt = cp_parser_omp_critical (parser, pragma_tok);
36089 break;
36090 case PRAGMA_OMP_DISTRIBUTE:
36091 strcpy (p_name, "#pragma omp");
36092 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
36093 break;
36094 case PRAGMA_OMP_FOR:
36095 strcpy (p_name, "#pragma omp");
36096 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
36097 break;
36098 case PRAGMA_OMP_MASTER:
36099 stmt = cp_parser_omp_master (parser, pragma_tok);
36100 break;
36101 case PRAGMA_OMP_PARALLEL:
36102 strcpy (p_name, "#pragma omp");
36103 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
36104 break;
36105 case PRAGMA_OMP_SECTIONS:
36106 strcpy (p_name, "#pragma omp");
36107 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
36108 break;
36109 case PRAGMA_OMP_SIMD:
36110 strcpy (p_name, "#pragma omp");
36111 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
36112 break;
36113 case PRAGMA_OMP_SINGLE:
36114 stmt = cp_parser_omp_single (parser, pragma_tok);
36115 break;
36116 case PRAGMA_OMP_TASK:
36117 stmt = cp_parser_omp_task (parser, pragma_tok);
36118 break;
36119 case PRAGMA_OMP_TASKGROUP:
36120 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
36121 break;
36122 case PRAGMA_OMP_TASKLOOP:
36123 strcpy (p_name, "#pragma omp");
36124 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL);
36125 break;
36126 case PRAGMA_OMP_TEAMS:
36127 strcpy (p_name, "#pragma omp");
36128 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
36129 break;
36130 default:
36131 gcc_unreachable ();
36134 protected_set_expr_location (stmt, pragma_tok->location);
36137 /* Transactional Memory parsing routines. */
36139 /* Parse a transaction attribute.
36141 txn-attribute:
36142 attribute
36143 [ [ identifier ] ]
36145 We use this instead of cp_parser_attributes_opt for transactions to avoid
36146 the pedwarn in C++98 mode. */
36148 static tree
36149 cp_parser_txn_attribute_opt (cp_parser *parser)
36151 cp_token *token;
36152 tree attr_name, attr = NULL;
36154 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36155 return cp_parser_attributes_opt (parser);
36157 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
36158 return NULL_TREE;
36159 cp_lexer_consume_token (parser->lexer);
36160 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
36161 goto error1;
36163 token = cp_lexer_peek_token (parser->lexer);
36164 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
36166 token = cp_lexer_consume_token (parser->lexer);
36168 attr_name = (token->type == CPP_KEYWORD
36169 /* For keywords, use the canonical spelling,
36170 not the parsed identifier. */
36171 ? ridpointers[(int) token->keyword]
36172 : token->u.value);
36173 attr = build_tree_list (attr_name, NULL_TREE);
36175 else
36176 cp_parser_error (parser, "expected identifier");
36178 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36179 error1:
36180 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36181 return attr;
36184 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36186 transaction-statement:
36187 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36188 compound-statement
36189 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36192 static tree
36193 cp_parser_transaction (cp_parser *parser, cp_token *token)
36195 unsigned char old_in = parser->in_transaction;
36196 unsigned char this_in = 1, new_in;
36197 enum rid keyword = token->keyword;
36198 tree stmt, attrs, noex;
36200 cp_lexer_consume_token (parser->lexer);
36202 if (keyword == RID_TRANSACTION_RELAXED
36203 || keyword == RID_SYNCHRONIZED)
36204 this_in |= TM_STMT_ATTR_RELAXED;
36205 else
36207 attrs = cp_parser_txn_attribute_opt (parser);
36208 if (attrs)
36209 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36212 /* Parse a noexcept specification. */
36213 if (keyword == RID_ATOMIC_NOEXCEPT)
36214 noex = boolean_true_node;
36215 else if (keyword == RID_ATOMIC_CANCEL)
36217 /* cancel-and-throw is unimplemented. */
36218 sorry ("atomic_cancel");
36219 noex = NULL_TREE;
36221 else
36222 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
36224 /* Keep track if we're in the lexical scope of an outer transaction. */
36225 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
36227 stmt = begin_transaction_stmt (token->location, NULL, this_in);
36229 parser->in_transaction = new_in;
36230 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
36231 parser->in_transaction = old_in;
36233 finish_transaction_stmt (stmt, NULL, this_in, noex);
36235 return stmt;
36238 /* Parse a __transaction_atomic or __transaction_relaxed expression.
36240 transaction-expression:
36241 __transaction_atomic txn-noexcept-spec[opt] ( expression )
36242 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
36245 static tree
36246 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
36248 unsigned char old_in = parser->in_transaction;
36249 unsigned char this_in = 1;
36250 cp_token *token;
36251 tree expr, noex;
36252 bool noex_expr;
36254 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36255 || keyword == RID_TRANSACTION_RELAXED);
36257 if (!flag_tm)
36258 error (keyword == RID_TRANSACTION_RELAXED
36259 ? G_("%<__transaction_relaxed%> without transactional memory "
36260 "support enabled")
36261 : G_("%<__transaction_atomic%> without transactional memory "
36262 "support enabled"));
36264 token = cp_parser_require_keyword (parser, keyword,
36265 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36266 : RT_TRANSACTION_RELAXED));
36267 gcc_assert (token != NULL);
36269 if (keyword == RID_TRANSACTION_RELAXED)
36270 this_in |= TM_STMT_ATTR_RELAXED;
36272 /* Set this early. This might mean that we allow transaction_cancel in
36273 an expression that we find out later actually has to be a constexpr.
36274 However, we expect that cxx_constant_value will be able to deal with
36275 this; also, if the noexcept has no constexpr, then what we parse next
36276 really is a transaction's body. */
36277 parser->in_transaction = this_in;
36279 /* Parse a noexcept specification. */
36280 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
36281 true);
36283 if (!noex || !noex_expr
36284 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36286 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
36288 expr = cp_parser_expression (parser);
36289 expr = finish_parenthesized_expr (expr);
36291 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
36293 else
36295 /* The only expression that is available got parsed for the noexcept
36296 already. noexcept is true then. */
36297 expr = noex;
36298 noex = boolean_true_node;
36301 expr = build_transaction_expr (token->location, expr, this_in, noex);
36302 parser->in_transaction = old_in;
36304 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
36305 return error_mark_node;
36307 return (flag_tm ? expr : error_mark_node);
36310 /* Parse a function-transaction-block.
36312 function-transaction-block:
36313 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
36314 function-body
36315 __transaction_atomic txn-attribute[opt] function-try-block
36316 __transaction_relaxed ctor-initializer[opt] function-body
36317 __transaction_relaxed function-try-block
36320 static bool
36321 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
36323 unsigned char old_in = parser->in_transaction;
36324 unsigned char new_in = 1;
36325 tree compound_stmt, stmt, attrs;
36326 bool ctor_initializer_p;
36327 cp_token *token;
36329 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36330 || keyword == RID_TRANSACTION_RELAXED);
36331 token = cp_parser_require_keyword (parser, keyword,
36332 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36333 : RT_TRANSACTION_RELAXED));
36334 gcc_assert (token != NULL);
36336 if (keyword == RID_TRANSACTION_RELAXED)
36337 new_in |= TM_STMT_ATTR_RELAXED;
36338 else
36340 attrs = cp_parser_txn_attribute_opt (parser);
36341 if (attrs)
36342 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36345 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
36347 parser->in_transaction = new_in;
36349 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
36350 ctor_initializer_p = cp_parser_function_try_block (parser);
36351 else
36352 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
36353 (parser, /*in_function_try_block=*/false);
36355 parser->in_transaction = old_in;
36357 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
36359 return ctor_initializer_p;
36362 /* Parse a __transaction_cancel statement.
36364 cancel-statement:
36365 __transaction_cancel txn-attribute[opt] ;
36366 __transaction_cancel txn-attribute[opt] throw-expression ;
36368 ??? Cancel and throw is not yet implemented. */
36370 static tree
36371 cp_parser_transaction_cancel (cp_parser *parser)
36373 cp_token *token;
36374 bool is_outer = false;
36375 tree stmt, attrs;
36377 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
36378 RT_TRANSACTION_CANCEL);
36379 gcc_assert (token != NULL);
36381 attrs = cp_parser_txn_attribute_opt (parser);
36382 if (attrs)
36383 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
36385 /* ??? Parse cancel-and-throw here. */
36387 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36389 if (!flag_tm)
36391 error_at (token->location, "%<__transaction_cancel%> without "
36392 "transactional memory support enabled");
36393 return error_mark_node;
36395 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
36397 error_at (token->location, "%<__transaction_cancel%> within a "
36398 "%<__transaction_relaxed%>");
36399 return error_mark_node;
36401 else if (is_outer)
36403 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
36404 && !is_tm_may_cancel_outer (current_function_decl))
36406 error_at (token->location, "outer %<__transaction_cancel%> not "
36407 "within outer %<__transaction_atomic%>");
36408 error_at (token->location,
36409 " or a %<transaction_may_cancel_outer%> function");
36410 return error_mark_node;
36413 else if (parser->in_transaction == 0)
36415 error_at (token->location, "%<__transaction_cancel%> not within "
36416 "%<__transaction_atomic%>");
36417 return error_mark_node;
36420 stmt = build_tm_abort_call (token->location, is_outer);
36421 add_stmt (stmt);
36423 return stmt;
36426 /* The parser. */
36428 static GTY (()) cp_parser *the_parser;
36431 /* Special handling for the first token or line in the file. The first
36432 thing in the file might be #pragma GCC pch_preprocess, which loads a
36433 PCH file, which is a GC collection point. So we need to handle this
36434 first pragma without benefit of an existing lexer structure.
36436 Always returns one token to the caller in *FIRST_TOKEN. This is
36437 either the true first token of the file, or the first token after
36438 the initial pragma. */
36440 static void
36441 cp_parser_initial_pragma (cp_token *first_token)
36443 tree name = NULL;
36445 cp_lexer_get_preprocessor_token (NULL, first_token);
36446 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
36447 return;
36449 cp_lexer_get_preprocessor_token (NULL, first_token);
36450 if (first_token->type == CPP_STRING)
36452 name = first_token->u.value;
36454 cp_lexer_get_preprocessor_token (NULL, first_token);
36455 if (first_token->type != CPP_PRAGMA_EOL)
36456 error_at (first_token->location,
36457 "junk at end of %<#pragma GCC pch_preprocess%>");
36459 else
36460 error_at (first_token->location, "expected string literal");
36462 /* Skip to the end of the pragma. */
36463 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
36464 cp_lexer_get_preprocessor_token (NULL, first_token);
36466 /* Now actually load the PCH file. */
36467 if (name)
36468 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
36470 /* Read one more token to return to our caller. We have to do this
36471 after reading the PCH file in, since its pointers have to be
36472 live. */
36473 cp_lexer_get_preprocessor_token (NULL, first_token);
36476 /* Parses the grainsize pragma for the _Cilk_for statement.
36477 Syntax:
36478 #pragma cilk grainsize = <VALUE>. */
36480 static void
36481 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
36483 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
36485 tree exp = cp_parser_binary_expression (parser, false, false,
36486 PREC_NOT_OPERATOR, NULL);
36487 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36488 if (!exp || exp == error_mark_node)
36490 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
36491 return;
36494 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
36495 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
36496 cp_parser_cilk_for (parser, exp);
36497 else
36498 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
36499 "%<#pragma cilk grainsize%> is not followed by "
36500 "%<_Cilk_for%>");
36501 return;
36503 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36506 /* Normal parsing of a pragma token. Here we can (and must) use the
36507 regular lexer. */
36509 static bool
36510 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
36512 cp_token *pragma_tok;
36513 unsigned int id;
36514 tree stmt;
36515 bool ret;
36517 pragma_tok = cp_lexer_consume_token (parser->lexer);
36518 gcc_assert (pragma_tok->type == CPP_PRAGMA);
36519 parser->lexer->in_pragma = true;
36521 id = pragma_tok->pragma_kind;
36522 if (id != PRAGMA_OMP_DECLARE_REDUCTION && id != PRAGMA_OACC_ROUTINE)
36523 cp_ensure_no_omp_declare_simd (parser);
36524 switch (id)
36526 case PRAGMA_GCC_PCH_PREPROCESS:
36527 error_at (pragma_tok->location,
36528 "%<#pragma GCC pch_preprocess%> must be first");
36529 break;
36531 case PRAGMA_OMP_BARRIER:
36532 switch (context)
36534 case pragma_compound:
36535 cp_parser_omp_barrier (parser, pragma_tok);
36536 return false;
36537 case pragma_stmt:
36538 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
36539 "used in compound statements");
36540 break;
36541 default:
36542 goto bad_stmt;
36544 break;
36546 case PRAGMA_OMP_FLUSH:
36547 switch (context)
36549 case pragma_compound:
36550 cp_parser_omp_flush (parser, pragma_tok);
36551 return false;
36552 case pragma_stmt:
36553 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
36554 "used in compound statements");
36555 break;
36556 default:
36557 goto bad_stmt;
36559 break;
36561 case PRAGMA_OMP_TASKWAIT:
36562 switch (context)
36564 case pragma_compound:
36565 cp_parser_omp_taskwait (parser, pragma_tok);
36566 return false;
36567 case pragma_stmt:
36568 error_at (pragma_tok->location,
36569 "%<#pragma omp taskwait%> may only be "
36570 "used in compound statements");
36571 break;
36572 default:
36573 goto bad_stmt;
36575 break;
36577 case PRAGMA_OMP_TASKYIELD:
36578 switch (context)
36580 case pragma_compound:
36581 cp_parser_omp_taskyield (parser, pragma_tok);
36582 return false;
36583 case pragma_stmt:
36584 error_at (pragma_tok->location,
36585 "%<#pragma omp taskyield%> may only be "
36586 "used in compound statements");
36587 break;
36588 default:
36589 goto bad_stmt;
36591 break;
36593 case PRAGMA_OMP_CANCEL:
36594 switch (context)
36596 case pragma_compound:
36597 cp_parser_omp_cancel (parser, pragma_tok);
36598 return false;
36599 case pragma_stmt:
36600 error_at (pragma_tok->location,
36601 "%<#pragma omp cancel%> may only be "
36602 "used in compound statements");
36603 break;
36604 default:
36605 goto bad_stmt;
36607 break;
36609 case PRAGMA_OMP_CANCELLATION_POINT:
36610 switch (context)
36612 case pragma_compound:
36613 cp_parser_omp_cancellation_point (parser, pragma_tok);
36614 return false;
36615 case pragma_stmt:
36616 error_at (pragma_tok->location,
36617 "%<#pragma omp cancellation point%> may only be "
36618 "used in compound statements");
36619 break;
36620 default:
36621 goto bad_stmt;
36623 break;
36625 case PRAGMA_OMP_THREADPRIVATE:
36626 cp_parser_omp_threadprivate (parser, pragma_tok);
36627 return false;
36629 case PRAGMA_OMP_DECLARE_REDUCTION:
36630 cp_parser_omp_declare (parser, pragma_tok, context);
36631 return false;
36633 case PRAGMA_OACC_DECLARE:
36634 cp_parser_oacc_declare (parser, pragma_tok);
36635 return false;
36637 case PRAGMA_OACC_ROUTINE:
36638 cp_parser_oacc_routine (parser, pragma_tok, context);
36639 return false;
36641 case PRAGMA_OACC_ATOMIC:
36642 case PRAGMA_OACC_CACHE:
36643 case PRAGMA_OACC_DATA:
36644 case PRAGMA_OACC_ENTER_DATA:
36645 case PRAGMA_OACC_EXIT_DATA:
36646 case PRAGMA_OACC_KERNELS:
36647 case PRAGMA_OACC_PARALLEL:
36648 case PRAGMA_OACC_LOOP:
36649 case PRAGMA_OACC_UPDATE:
36650 case PRAGMA_OACC_WAIT:
36651 case PRAGMA_OMP_ATOMIC:
36652 case PRAGMA_OMP_CRITICAL:
36653 case PRAGMA_OMP_DISTRIBUTE:
36654 case PRAGMA_OMP_FOR:
36655 case PRAGMA_OMP_MASTER:
36656 case PRAGMA_OMP_PARALLEL:
36657 case PRAGMA_OMP_SECTIONS:
36658 case PRAGMA_OMP_SIMD:
36659 case PRAGMA_OMP_SINGLE:
36660 case PRAGMA_OMP_TASK:
36661 case PRAGMA_OMP_TASKGROUP:
36662 case PRAGMA_OMP_TASKLOOP:
36663 case PRAGMA_OMP_TEAMS:
36664 if (context != pragma_stmt && context != pragma_compound)
36665 goto bad_stmt;
36666 stmt = push_omp_privatization_clauses (false);
36667 cp_parser_omp_construct (parser, pragma_tok);
36668 pop_omp_privatization_clauses (stmt);
36669 return true;
36671 case PRAGMA_OMP_ORDERED:
36672 stmt = push_omp_privatization_clauses (false);
36673 ret = cp_parser_omp_ordered (parser, pragma_tok, context);
36674 pop_omp_privatization_clauses (stmt);
36675 return ret;
36677 case PRAGMA_OMP_TARGET:
36678 stmt = push_omp_privatization_clauses (false);
36679 ret = cp_parser_omp_target (parser, pragma_tok, context);
36680 pop_omp_privatization_clauses (stmt);
36681 return ret;
36683 case PRAGMA_OMP_END_DECLARE_TARGET:
36684 cp_parser_omp_end_declare_target (parser, pragma_tok);
36685 return false;
36687 case PRAGMA_OMP_SECTION:
36688 error_at (pragma_tok->location,
36689 "%<#pragma omp section%> may only be used in "
36690 "%<#pragma omp sections%> construct");
36691 break;
36693 case PRAGMA_IVDEP:
36695 if (context == pragma_external)
36697 error_at (pragma_tok->location,
36698 "%<#pragma GCC ivdep%> must be inside a function");
36699 break;
36701 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36702 cp_token *tok;
36703 tok = cp_lexer_peek_token (the_parser->lexer);
36704 if (tok->type != CPP_KEYWORD
36705 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
36706 && tok->keyword != RID_DO))
36708 cp_parser_error (parser, "for, while or do statement expected");
36709 return false;
36711 cp_parser_iteration_statement (parser, true);
36712 return true;
36715 case PRAGMA_CILK_SIMD:
36716 if (context == pragma_external)
36718 error_at (pragma_tok->location,
36719 "%<#pragma simd%> must be inside a function");
36720 break;
36722 stmt = push_omp_privatization_clauses (false);
36723 cp_parser_cilk_simd (parser, pragma_tok);
36724 pop_omp_privatization_clauses (stmt);
36725 return true;
36727 case PRAGMA_CILK_GRAINSIZE:
36728 if (context == pragma_external)
36730 error_at (pragma_tok->location,
36731 "%<#pragma cilk grainsize%> must be inside a function");
36732 break;
36735 /* Ignore the pragma if Cilk Plus is not enabled. */
36736 if (flag_cilkplus)
36738 cp_parser_cilk_grainsize (parser, pragma_tok);
36739 return true;
36741 else
36743 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
36744 "%<#pragma cilk grainsize%>");
36745 break;
36748 default:
36749 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
36750 c_invoke_pragma_handler (id);
36751 break;
36753 bad_stmt:
36754 cp_parser_error (parser, "expected declaration specifiers");
36755 break;
36758 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36759 return false;
36762 /* The interface the pragma parsers have to the lexer. */
36764 enum cpp_ttype
36765 pragma_lex (tree *value, location_t *loc)
36767 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
36768 enum cpp_ttype ret = tok->type;
36770 *value = tok->u.value;
36771 if (loc)
36772 *loc = tok->location;
36774 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
36775 ret = CPP_EOF;
36776 else if (ret == CPP_STRING)
36777 *value = cp_parser_string_literal (the_parser, false, false);
36778 else
36780 if (ret == CPP_KEYWORD)
36781 ret = CPP_NAME;
36782 cp_lexer_consume_token (the_parser->lexer);
36785 return ret;
36789 /* External interface. */
36791 /* Parse one entire translation unit. */
36793 void
36794 c_parse_file (void)
36796 static bool already_called = false;
36798 if (already_called)
36799 fatal_error (input_location,
36800 "inter-module optimizations not implemented for C++");
36801 already_called = true;
36803 the_parser = cp_parser_new ();
36804 push_deferring_access_checks (flag_access_control
36805 ? dk_no_deferred : dk_no_check);
36806 cp_parser_translation_unit (the_parser);
36807 the_parser = NULL;
36810 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
36811 vectorlength clause:
36812 Syntax:
36813 vectorlength ( constant-expression ) */
36815 static tree
36816 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
36817 bool is_simd_fn)
36819 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36820 tree expr;
36821 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
36822 safelen clause. Thus, vectorlength is represented as OMP 4.0
36823 safelen. For SIMD-enabled function it is represented by OMP 4.0
36824 simdlen. */
36825 if (!is_simd_fn)
36826 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
36827 loc);
36828 else
36829 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
36830 loc);
36832 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36833 return error_mark_node;
36835 expr = cp_parser_constant_expression (parser);
36836 expr = maybe_constant_value (expr);
36838 /* If expr == error_mark_node, then don't emit any errors nor
36839 create a clause. if any of the above functions returns
36840 error mark node then they would have emitted an error message. */
36841 if (expr == error_mark_node)
36843 else if (!TREE_TYPE (expr)
36844 || !TREE_CONSTANT (expr)
36845 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
36846 error_at (loc, "vectorlength must be an integer constant");
36847 else if (TREE_CONSTANT (expr)
36848 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
36849 error_at (loc, "vectorlength must be a power of 2");
36850 else
36852 tree c;
36853 if (!is_simd_fn)
36855 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
36856 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
36857 OMP_CLAUSE_CHAIN (c) = clauses;
36858 clauses = c;
36860 else
36862 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
36863 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
36864 OMP_CLAUSE_CHAIN (c) = clauses;
36865 clauses = c;
36869 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36870 return error_mark_node;
36871 return clauses;
36874 /* Handles the Cilk Plus #pragma simd linear clause.
36875 Syntax:
36876 linear ( simd-linear-variable-list )
36878 simd-linear-variable-list:
36879 simd-linear-variable
36880 simd-linear-variable-list , simd-linear-variable
36882 simd-linear-variable:
36883 id-expression
36884 id-expression : simd-linear-step
36886 simd-linear-step:
36887 conditional-expression */
36889 static tree
36890 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
36892 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36894 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36895 return clauses;
36896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36898 cp_parser_error (parser, "expected identifier");
36899 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
36900 return error_mark_node;
36903 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36904 parser->colon_corrects_to_scope_p = false;
36905 while (1)
36907 cp_token *token = cp_lexer_peek_token (parser->lexer);
36908 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36910 cp_parser_error (parser, "expected variable-name");
36911 clauses = error_mark_node;
36912 break;
36915 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
36916 false, false);
36917 tree decl = cp_parser_lookup_name_simple (parser, var_name,
36918 token->location);
36919 if (decl == error_mark_node)
36921 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
36922 token->location);
36923 clauses = error_mark_node;
36925 else
36927 tree e = NULL_TREE;
36928 tree step_size = integer_one_node;
36930 /* If present, parse the linear step. Otherwise, assume the default
36931 value of 1. */
36932 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
36934 cp_lexer_consume_token (parser->lexer);
36936 e = cp_parser_assignment_expression (parser);
36937 e = maybe_constant_value (e);
36939 if (e == error_mark_node)
36941 /* If an error has occurred, then the whole pragma is
36942 considered ill-formed. Thus, no reason to keep
36943 parsing. */
36944 clauses = error_mark_node;
36945 break;
36947 else if (type_dependent_expression_p (e)
36948 || value_dependent_expression_p (e)
36949 || (TREE_TYPE (e)
36950 && INTEGRAL_TYPE_P (TREE_TYPE (e))
36951 && (TREE_CONSTANT (e)
36952 || DECL_P (e))))
36953 step_size = e;
36954 else
36955 cp_parser_error (parser,
36956 "step size must be an integer constant "
36957 "expression or an integer variable");
36960 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
36961 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
36962 OMP_CLAUSE_DECL (l) = decl;
36963 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
36964 OMP_CLAUSE_CHAIN (l) = clauses;
36965 clauses = l;
36967 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36968 cp_lexer_consume_token (parser->lexer);
36969 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36970 break;
36971 else
36973 error_at (cp_lexer_peek_token (parser->lexer)->location,
36974 "expected %<,%> or %<)%> after %qE", decl);
36975 clauses = error_mark_node;
36976 break;
36979 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36980 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
36981 return clauses;
36984 /* Returns the name of the next clause. If the clause is not
36985 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
36986 token is not consumed. Otherwise, the appropriate enum from the
36987 pragma_simd_clause is returned and the token is consumed. */
36989 static pragma_omp_clause
36990 cp_parser_cilk_simd_clause_name (cp_parser *parser)
36992 pragma_omp_clause clause_type;
36993 cp_token *token = cp_lexer_peek_token (parser->lexer);
36995 if (token->keyword == RID_PRIVATE)
36996 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
36997 else if (!token->u.value || token->type != CPP_NAME)
36998 return PRAGMA_CILK_CLAUSE_NONE;
36999 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37000 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
37001 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
37002 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
37003 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
37004 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
37005 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
37006 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
37007 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
37008 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
37009 else
37010 return PRAGMA_CILK_CLAUSE_NONE;
37012 cp_lexer_consume_token (parser->lexer);
37013 return clause_type;
37016 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
37018 static tree
37019 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
37021 tree clauses = NULL_TREE;
37023 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37024 && clauses != error_mark_node)
37026 pragma_omp_clause c_kind;
37027 c_kind = cp_parser_cilk_simd_clause_name (parser);
37028 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
37029 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
37030 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
37031 clauses = cp_parser_cilk_simd_linear (parser, clauses);
37032 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
37033 /* Use the OpenMP 4.0 equivalent function. */
37034 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
37035 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
37036 /* Use the OpenMP 4.0 equivalent function. */
37037 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37038 clauses);
37039 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
37040 /* Use the OMP 4.0 equivalent function. */
37041 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
37042 clauses);
37043 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
37044 /* Use the OMP 4.0 equivalent function. */
37045 clauses = cp_parser_omp_clause_reduction (parser, clauses);
37046 else
37048 clauses = error_mark_node;
37049 cp_parser_error (parser, "expected %<#pragma simd%> clause");
37050 break;
37054 cp_parser_skip_to_pragma_eol (parser, pragma_token);
37056 if (clauses == error_mark_node)
37057 return error_mark_node;
37058 else
37059 return c_finish_cilk_clauses (clauses);
37062 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
37064 static void
37065 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
37067 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
37069 if (clauses == error_mark_node)
37070 return;
37072 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
37074 error_at (cp_lexer_peek_token (parser->lexer)->location,
37075 "for statement expected");
37076 return;
37079 tree sb = begin_omp_structured_block ();
37080 int save = cp_parser_begin_omp_structured_block (parser);
37081 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
37082 if (ret)
37083 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
37084 cp_parser_end_omp_structured_block (parser, save);
37085 add_stmt (finish_omp_structured_block (sb));
37088 /* Main entry-point for parsing Cilk Plus _Cilk_for
37089 loops. The return value is error_mark_node
37090 when errors happen and CILK_FOR tree on success. */
37092 static tree
37093 cp_parser_cilk_for (cp_parser *parser, tree grain)
37095 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
37096 gcc_unreachable ();
37098 tree sb = begin_omp_structured_block ();
37099 int save = cp_parser_begin_omp_structured_block (parser);
37101 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
37102 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
37103 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
37104 clauses = finish_omp_clauses (clauses, false);
37106 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
37107 if (ret)
37108 cpp_validate_cilk_plus_loop (ret);
37109 else
37110 ret = error_mark_node;
37112 cp_parser_end_omp_structured_block (parser, save);
37113 add_stmt (finish_omp_structured_block (sb));
37114 return ret;
37117 /* Create an identifier for a generic parameter type (a synthesized
37118 template parameter implied by `auto' or a concept identifier). */
37120 static GTY(()) int generic_parm_count;
37121 static tree
37122 make_generic_type_name ()
37124 char buf[32];
37125 sprintf (buf, "auto:%d", ++generic_parm_count);
37126 return get_identifier (buf);
37129 /* Predicate that behaves as is_auto_or_concept but matches the parent
37130 node of the generic type rather than the generic type itself. This
37131 allows for type transformation in add_implicit_template_parms. */
37133 static inline bool
37134 tree_type_is_auto_or_concept (const_tree t)
37136 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
37139 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37140 (creating a new template parameter list if necessary). Returns the newly
37141 created template type parm. */
37143 static tree
37144 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
37146 gcc_assert (current_binding_level->kind == sk_function_parms);
37148 /* Before committing to modifying any scope, if we're in an
37149 implicit template scope, and we're trying to synthesize a
37150 constrained parameter, try to find a previous parameter with
37151 the same name. This is the same-type rule for abbreviated
37152 function templates. */
37153 if (parser->implicit_template_scope && constr)
37155 tree t = parser->implicit_template_parms;
37156 while (t)
37158 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
37160 tree d = TREE_VALUE (t);
37161 if (TREE_CODE (d) == PARM_DECL)
37162 /* Return the TEMPLATE_PARM_INDEX. */
37163 d = DECL_INITIAL (d);
37164 return d;
37166 t = TREE_CHAIN (t);
37170 /* We are either continuing a function template that already contains implicit
37171 template parameters, creating a new fully-implicit function template, or
37172 extending an existing explicit function template with implicit template
37173 parameters. */
37175 cp_binding_level *const entry_scope = current_binding_level;
37177 bool become_template = false;
37178 cp_binding_level *parent_scope = 0;
37180 if (parser->implicit_template_scope)
37182 gcc_assert (parser->implicit_template_parms);
37184 current_binding_level = parser->implicit_template_scope;
37186 else
37188 /* Roll back to the existing template parameter scope (in the case of
37189 extending an explicit function template) or introduce a new template
37190 parameter scope ahead of the function parameter scope (or class scope
37191 in the case of out-of-line member definitions). The function scope is
37192 added back after template parameter synthesis below. */
37194 cp_binding_level *scope = entry_scope;
37196 while (scope->kind == sk_function_parms)
37198 parent_scope = scope;
37199 scope = scope->level_chain;
37201 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
37203 /* If not defining a class, then any class scope is a scope level in
37204 an out-of-line member definition. In this case simply wind back
37205 beyond the first such scope to inject the template parameter list.
37206 Otherwise wind back to the class being defined. The latter can
37207 occur in class member friend declarations such as:
37209 class A {
37210 void foo (auto);
37212 class B {
37213 friend void A::foo (auto);
37216 The template parameter list synthesized for the friend declaration
37217 must be injected in the scope of 'B'. This can also occur in
37218 erroneous cases such as:
37220 struct A {
37221 struct B {
37222 void foo (auto);
37224 void B::foo (auto) {}
37227 Here the attempted definition of 'B::foo' within 'A' is ill-formed
37228 but, nevertheless, the template parameter list synthesized for the
37229 declarator should be injected into the scope of 'A' as if the
37230 ill-formed template was specified explicitly. */
37232 while (scope->kind == sk_class && !scope->defining_class_p)
37234 parent_scope = scope;
37235 scope = scope->level_chain;
37239 current_binding_level = scope;
37241 if (scope->kind != sk_template_parms
37242 || !function_being_declared_is_template_p (parser))
37244 /* Introduce a new template parameter list for implicit template
37245 parameters. */
37247 become_template = true;
37249 parser->implicit_template_scope
37250 = begin_scope (sk_template_parms, NULL);
37252 ++processing_template_decl;
37254 parser->fully_implicit_function_template_p = true;
37255 ++parser->num_template_parameter_lists;
37257 else
37259 /* Synthesize implicit template parameters at the end of the explicit
37260 template parameter list. */
37262 gcc_assert (current_template_parms);
37264 parser->implicit_template_scope = scope;
37266 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
37267 parser->implicit_template_parms
37268 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
37272 /* Synthesize a new template parameter and track the current template
37273 parameter chain with implicit_template_parms. */
37275 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
37276 tree synth_id = make_generic_type_name ();
37277 tree synth_tmpl_parm;
37278 bool non_type = false;
37280 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
37281 synth_tmpl_parm
37282 = finish_template_type_parm (class_type_node, synth_id);
37283 else if (TREE_CODE (proto) == TEMPLATE_DECL)
37284 synth_tmpl_parm
37285 = finish_constrained_template_template_parm (proto, synth_id);
37286 else
37288 synth_tmpl_parm = copy_decl (proto);
37289 DECL_NAME (synth_tmpl_parm) = synth_id;
37290 non_type = true;
37293 // Attach the constraint to the parm before processing.
37294 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
37295 TREE_TYPE (node) = constr;
37296 tree new_parm
37297 = process_template_parm (parser->implicit_template_parms,
37298 input_location,
37299 node,
37300 /*non_type=*/non_type,
37301 /*param_pack=*/false);
37303 // Chain the new parameter to the list of implicit parameters.
37304 if (parser->implicit_template_parms)
37305 parser->implicit_template_parms
37306 = TREE_CHAIN (parser->implicit_template_parms);
37307 else
37308 parser->implicit_template_parms = new_parm;
37310 tree new_decl = getdecls ();
37311 if (non_type)
37312 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
37313 new_decl = DECL_INITIAL (new_decl);
37315 /* If creating a fully implicit function template, start the new implicit
37316 template parameter list with this synthesized type, otherwise grow the
37317 current template parameter list. */
37319 if (become_template)
37321 parent_scope->level_chain = current_binding_level;
37323 tree new_parms = make_tree_vec (1);
37324 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
37325 current_template_parms = tree_cons (size_int (processing_template_decl),
37326 new_parms, current_template_parms);
37328 else
37330 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
37331 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
37332 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
37333 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
37336 // If the new parameter was constrained, we need to add that to the
37337 // constraints in the template parameter list.
37338 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
37340 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
37341 reqs = conjoin_constraints (reqs, req);
37342 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
37345 current_binding_level = entry_scope;
37347 return new_decl;
37350 /* Finish the declaration of a fully implicit function template. Such a
37351 template has no explicit template parameter list so has not been through the
37352 normal template head and tail processing. synthesize_implicit_template_parm
37353 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
37354 provided if the declaration is a class member such that its template
37355 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
37356 form is returned. Otherwise NULL_TREE is returned. */
37358 static tree
37359 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
37361 gcc_assert (parser->fully_implicit_function_template_p);
37363 if (member_decl_opt && member_decl_opt != error_mark_node
37364 && DECL_VIRTUAL_P (member_decl_opt))
37366 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
37367 "implicit templates may not be %<virtual%>");
37368 DECL_VIRTUAL_P (member_decl_opt) = false;
37371 if (member_decl_opt)
37372 member_decl_opt = finish_member_template_decl (member_decl_opt);
37373 end_template_decl ();
37375 parser->fully_implicit_function_template_p = false;
37376 --parser->num_template_parameter_lists;
37378 return member_decl_opt;
37381 #include "gt-cp-parser.h"