gcc/
[official-gcc.git] / gcc / cp / parser.c
blob772369493e96a0bb2ee1542f545f761b687f01ca
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 "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "alias.h"
28 #include "tree.h"
29 #include "print-tree.h"
30 #include "stringpool.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "cp-tree.h"
34 #include "intl.h"
35 #include "c-family/c-pragma.h"
36 #include "decl.h"
37 #include "flags.h"
38 #include "diagnostic-core.h"
39 #include "target.h"
40 #include "hard-reg-set.h"
41 #include "function.h"
42 #include "cgraph.h"
43 #include "c-family/c-common.h"
44 #include "c-family/c-objc.h"
45 #include "plugin.h"
46 #include "tree-pretty-print.h"
47 #include "parser.h"
48 #include "type-utils.h"
49 #include "omp-low.h"
50 #include "gomp-constants.h"
53 /* The lexer. */
55 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
56 and c-lex.c) and the C++ parser. */
58 static cp_token eof_token =
60 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
63 /* The various kinds of non integral constant we encounter. */
64 typedef enum non_integral_constant {
65 NIC_NONE,
66 /* floating-point literal */
67 NIC_FLOAT,
68 /* %<this%> */
69 NIC_THIS,
70 /* %<__FUNCTION__%> */
71 NIC_FUNC_NAME,
72 /* %<__PRETTY_FUNCTION__%> */
73 NIC_PRETTY_FUNC,
74 /* %<__func__%> */
75 NIC_C99_FUNC,
76 /* "%<va_arg%> */
77 NIC_VA_ARG,
78 /* a cast */
79 NIC_CAST,
80 /* %<typeid%> operator */
81 NIC_TYPEID,
82 /* non-constant compound literals */
83 NIC_NCC,
84 /* a function call */
85 NIC_FUNC_CALL,
86 /* an increment */
87 NIC_INC,
88 /* an decrement */
89 NIC_DEC,
90 /* an array reference */
91 NIC_ARRAY_REF,
92 /* %<->%> */
93 NIC_ARROW,
94 /* %<.%> */
95 NIC_POINT,
96 /* the address of a label */
97 NIC_ADDR_LABEL,
98 /* %<*%> */
99 NIC_STAR,
100 /* %<&%> */
101 NIC_ADDR,
102 /* %<++%> */
103 NIC_PREINCREMENT,
104 /* %<--%> */
105 NIC_PREDECREMENT,
106 /* %<new%> */
107 NIC_NEW,
108 /* %<delete%> */
109 NIC_DEL,
110 /* calls to overloaded operators */
111 NIC_OVERLOADED,
112 /* an assignment */
113 NIC_ASSIGNMENT,
114 /* a comma operator */
115 NIC_COMMA,
116 /* a call to a constructor */
117 NIC_CONSTRUCTOR,
118 /* a transaction expression */
119 NIC_TRANSACTION
120 } non_integral_constant;
122 /* The various kinds of errors about name-lookup failing. */
123 typedef enum name_lookup_error {
124 /* NULL */
125 NLE_NULL,
126 /* is not a type */
127 NLE_TYPE,
128 /* is not a class or namespace */
129 NLE_CXX98,
130 /* is not a class, namespace, or enumeration */
131 NLE_NOT_CXX98
132 } name_lookup_error;
134 /* The various kinds of required token */
135 typedef enum required_token {
136 RT_NONE,
137 RT_SEMICOLON, /* ';' */
138 RT_OPEN_PAREN, /* '(' */
139 RT_CLOSE_BRACE, /* '}' */
140 RT_OPEN_BRACE, /* '{' */
141 RT_CLOSE_SQUARE, /* ']' */
142 RT_OPEN_SQUARE, /* '[' */
143 RT_COMMA, /* ',' */
144 RT_SCOPE, /* '::' */
145 RT_LESS, /* '<' */
146 RT_GREATER, /* '>' */
147 RT_EQ, /* '=' */
148 RT_ELLIPSIS, /* '...' */
149 RT_MULT, /* '*' */
150 RT_COMPL, /* '~' */
151 RT_COLON, /* ':' */
152 RT_COLON_SCOPE, /* ':' or '::' */
153 RT_CLOSE_PAREN, /* ')' */
154 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
155 RT_PRAGMA_EOL, /* end of line */
156 RT_NAME, /* identifier */
158 /* The type is CPP_KEYWORD */
159 RT_NEW, /* new */
160 RT_DELETE, /* delete */
161 RT_RETURN, /* return */
162 RT_WHILE, /* while */
163 RT_EXTERN, /* extern */
164 RT_STATIC_ASSERT, /* static_assert */
165 RT_DECLTYPE, /* decltype */
166 RT_OPERATOR, /* operator */
167 RT_CLASS, /* class */
168 RT_TEMPLATE, /* template */
169 RT_NAMESPACE, /* namespace */
170 RT_USING, /* using */
171 RT_ASM, /* asm */
172 RT_TRY, /* try */
173 RT_CATCH, /* catch */
174 RT_THROW, /* throw */
175 RT_LABEL, /* __label__ */
176 RT_AT_TRY, /* @try */
177 RT_AT_SYNCHRONIZED, /* @synchronized */
178 RT_AT_THROW, /* @throw */
180 RT_SELECT, /* selection-statement */
181 RT_INTERATION, /* iteration-statement */
182 RT_JUMP, /* jump-statement */
183 RT_CLASS_KEY, /* class-key */
184 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
185 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
186 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
187 RT_TRANSACTION_CANCEL /* __transaction_cancel */
188 } required_token;
190 /* Prototypes. */
192 static cp_lexer *cp_lexer_new_main
193 (void);
194 static cp_lexer *cp_lexer_new_from_tokens
195 (cp_token_cache *tokens);
196 static void cp_lexer_destroy
197 (cp_lexer *);
198 static int cp_lexer_saving_tokens
199 (const cp_lexer *);
200 static cp_token *cp_lexer_token_at
201 (cp_lexer *, cp_token_position);
202 static void cp_lexer_get_preprocessor_token
203 (cp_lexer *, cp_token *);
204 static inline cp_token *cp_lexer_peek_token
205 (cp_lexer *);
206 static cp_token *cp_lexer_peek_nth_token
207 (cp_lexer *, size_t);
208 static inline bool cp_lexer_next_token_is
209 (cp_lexer *, enum cpp_ttype);
210 static bool cp_lexer_next_token_is_not
211 (cp_lexer *, enum cpp_ttype);
212 static bool cp_lexer_next_token_is_keyword
213 (cp_lexer *, enum rid);
214 static cp_token *cp_lexer_consume_token
215 (cp_lexer *);
216 static void cp_lexer_purge_token
217 (cp_lexer *);
218 static void cp_lexer_purge_tokens_after
219 (cp_lexer *, cp_token_position);
220 static void cp_lexer_save_tokens
221 (cp_lexer *);
222 static void cp_lexer_commit_tokens
223 (cp_lexer *);
224 static void cp_lexer_rollback_tokens
225 (cp_lexer *);
226 static void cp_lexer_print_token
227 (FILE *, cp_token *);
228 static inline bool cp_lexer_debugging_p
229 (cp_lexer *);
230 static void cp_lexer_start_debugging
231 (cp_lexer *) ATTRIBUTE_UNUSED;
232 static void cp_lexer_stop_debugging
233 (cp_lexer *) ATTRIBUTE_UNUSED;
235 static cp_token_cache *cp_token_cache_new
236 (cp_token *, cp_token *);
238 static void cp_parser_initial_pragma
239 (cp_token *);
241 static tree cp_literal_operator_id
242 (const char *);
244 static void cp_parser_cilk_simd
245 (cp_parser *, cp_token *);
246 static tree cp_parser_cilk_for
247 (cp_parser *, tree);
248 static bool cp_parser_omp_declare_reduction_exprs
249 (tree, cp_parser *);
250 static tree cp_parser_cilk_simd_vectorlength
251 (cp_parser *, tree, bool);
253 /* Manifest constants. */
254 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
255 #define CP_SAVED_TOKEN_STACK 5
257 /* Variables. */
259 /* The stream to which debugging output should be written. */
260 static FILE *cp_lexer_debug_stream;
262 /* Nonzero if we are parsing an unevaluated operand: an operand to
263 sizeof, typeof, or alignof. */
264 int cp_unevaluated_operand;
266 /* Dump up to NUM tokens in BUFFER to FILE starting with token
267 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
268 first token in BUFFER. If NUM is 0, dump all the tokens. If
269 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
270 highlighted by surrounding it in [[ ]]. */
272 static void
273 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
274 cp_token *start_token, unsigned num,
275 cp_token *curr_token)
277 unsigned i, nprinted;
278 cp_token *token;
279 bool do_print;
281 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
283 if (buffer == NULL)
284 return;
286 if (num == 0)
287 num = buffer->length ();
289 if (start_token == NULL)
290 start_token = buffer->address ();
292 if (start_token > buffer->address ())
294 cp_lexer_print_token (file, &(*buffer)[0]);
295 fprintf (file, " ... ");
298 do_print = false;
299 nprinted = 0;
300 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
302 if (token == start_token)
303 do_print = true;
305 if (!do_print)
306 continue;
308 nprinted++;
309 if (token == curr_token)
310 fprintf (file, "[[");
312 cp_lexer_print_token (file, token);
314 if (token == curr_token)
315 fprintf (file, "]]");
317 switch (token->type)
319 case CPP_SEMICOLON:
320 case CPP_OPEN_BRACE:
321 case CPP_CLOSE_BRACE:
322 case CPP_EOF:
323 fputc ('\n', file);
324 break;
326 default:
327 fputc (' ', file);
331 if (i == num && i < buffer->length ())
333 fprintf (file, " ... ");
334 cp_lexer_print_token (file, &buffer->last ());
337 fprintf (file, "\n");
341 /* Dump all tokens in BUFFER to stderr. */
343 void
344 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
346 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
349 DEBUG_FUNCTION void
350 debug (vec<cp_token, va_gc> &ref)
352 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
355 DEBUG_FUNCTION void
356 debug (vec<cp_token, va_gc> *ptr)
358 if (ptr)
359 debug (*ptr);
360 else
361 fprintf (stderr, "<nil>\n");
365 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
366 description for T. */
368 static void
369 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
371 if (t)
373 fprintf (file, "%s: ", desc);
374 print_node_brief (file, "", t, 0);
379 /* Dump parser context C to FILE. */
381 static void
382 cp_debug_print_context (FILE *file, cp_parser_context *c)
384 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
385 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
386 print_node_brief (file, "", c->object_type, 0);
387 fprintf (file, "}\n");
391 /* Print the stack of parsing contexts to FILE starting with FIRST. */
393 static void
394 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
396 unsigned i;
397 cp_parser_context *c;
399 fprintf (file, "Parsing context stack:\n");
400 for (i = 0, c = first; c; c = c->next, i++)
402 fprintf (file, "\t#%u: ", i);
403 cp_debug_print_context (file, c);
408 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
410 static void
411 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
413 if (flag)
414 fprintf (file, "%s: true\n", desc);
418 /* Print an unparsed function entry UF to FILE. */
420 static void
421 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
423 unsigned i;
424 cp_default_arg_entry *default_arg_fn;
425 tree fn;
427 fprintf (file, "\tFunctions with default args:\n");
428 for (i = 0;
429 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
430 i++)
432 fprintf (file, "\t\tClass type: ");
433 print_node_brief (file, "", default_arg_fn->class_type, 0);
434 fprintf (file, "\t\tDeclaration: ");
435 print_node_brief (file, "", default_arg_fn->decl, 0);
436 fprintf (file, "\n");
439 fprintf (file, "\n\tFunctions with definitions that require "
440 "post-processing\n\t\t");
441 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
443 print_node_brief (file, "", fn, 0);
444 fprintf (file, " ");
446 fprintf (file, "\n");
448 fprintf (file, "\n\tNon-static data members with initializers that require "
449 "post-processing\n\t\t");
450 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
452 print_node_brief (file, "", fn, 0);
453 fprintf (file, " ");
455 fprintf (file, "\n");
459 /* Print the stack of unparsed member functions S to FILE. */
461 static void
462 cp_debug_print_unparsed_queues (FILE *file,
463 vec<cp_unparsed_functions_entry, va_gc> *s)
465 unsigned i;
466 cp_unparsed_functions_entry *uf;
468 fprintf (file, "Unparsed functions\n");
469 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
471 fprintf (file, "#%u:\n", i);
472 cp_debug_print_unparsed_function (file, uf);
477 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
478 the given PARSER. If FILE is NULL, the output is printed on stderr. */
480 static void
481 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
483 cp_token *next_token, *first_token, *start_token;
485 if (file == NULL)
486 file = stderr;
488 next_token = parser->lexer->next_token;
489 first_token = parser->lexer->buffer->address ();
490 start_token = (next_token > first_token + window_size / 2)
491 ? next_token - window_size / 2
492 : first_token;
493 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
494 next_token);
498 /* Dump debugging information for the given PARSER. If FILE is NULL,
499 the output is printed on stderr. */
501 void
502 cp_debug_parser (FILE *file, cp_parser *parser)
504 const size_t window_size = 20;
505 cp_token *token;
506 expanded_location eloc;
508 if (file == NULL)
509 file = stderr;
511 fprintf (file, "Parser state\n\n");
512 fprintf (file, "Number of tokens: %u\n",
513 vec_safe_length (parser->lexer->buffer));
514 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
515 cp_debug_print_tree_if_set (file, "Object scope",
516 parser->object_scope);
517 cp_debug_print_tree_if_set (file, "Qualifying scope",
518 parser->qualifying_scope);
519 cp_debug_print_context_stack (file, parser->context);
520 cp_debug_print_flag (file, "Allow GNU extensions",
521 parser->allow_gnu_extensions_p);
522 cp_debug_print_flag (file, "'>' token is greater-than",
523 parser->greater_than_is_operator_p);
524 cp_debug_print_flag (file, "Default args allowed in current "
525 "parameter list", parser->default_arg_ok_p);
526 cp_debug_print_flag (file, "Parsing integral constant-expression",
527 parser->integral_constant_expression_p);
528 cp_debug_print_flag (file, "Allow non-constant expression in current "
529 "constant-expression",
530 parser->allow_non_integral_constant_expression_p);
531 cp_debug_print_flag (file, "Seen non-constant expression",
532 parser->non_integral_constant_expression_p);
533 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
534 "current context",
535 parser->local_variables_forbidden_p);
536 cp_debug_print_flag (file, "In unbraced linkage specification",
537 parser->in_unbraced_linkage_specification_p);
538 cp_debug_print_flag (file, "Parsing a declarator",
539 parser->in_declarator_p);
540 cp_debug_print_flag (file, "In template argument list",
541 parser->in_template_argument_list_p);
542 cp_debug_print_flag (file, "Parsing an iteration statement",
543 parser->in_statement & IN_ITERATION_STMT);
544 cp_debug_print_flag (file, "Parsing a switch statement",
545 parser->in_statement & IN_SWITCH_STMT);
546 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
547 parser->in_statement & IN_OMP_BLOCK);
548 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
549 parser->in_statement & IN_CILK_SIMD_FOR);
550 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
551 parser->in_statement & IN_OMP_FOR);
552 cp_debug_print_flag (file, "Parsing an if statement",
553 parser->in_statement & IN_IF_STMT);
554 cp_debug_print_flag (file, "Parsing a type-id in an expression "
555 "context", parser->in_type_id_in_expr_p);
556 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
557 parser->implicit_extern_c);
558 cp_debug_print_flag (file, "String expressions should be translated "
559 "to execution character set",
560 parser->translate_strings_p);
561 cp_debug_print_flag (file, "Parsing function body outside of a "
562 "local class", parser->in_function_body);
563 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
564 parser->colon_corrects_to_scope_p);
565 cp_debug_print_flag (file, "Colon doesn't start a class definition",
566 parser->colon_doesnt_start_class_def_p);
567 if (parser->type_definition_forbidden_message)
568 fprintf (file, "Error message for forbidden type definitions: %s\n",
569 parser->type_definition_forbidden_message);
570 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
571 fprintf (file, "Number of class definitions in progress: %u\n",
572 parser->num_classes_being_defined);
573 fprintf (file, "Number of template parameter lists for the current "
574 "declaration: %u\n", parser->num_template_parameter_lists);
575 cp_debug_parser_tokens (file, parser, window_size);
576 token = parser->lexer->next_token;
577 fprintf (file, "Next token to parse:\n");
578 fprintf (file, "\tToken: ");
579 cp_lexer_print_token (file, token);
580 eloc = expand_location (token->location);
581 fprintf (file, "\n\tFile: %s\n", eloc.file);
582 fprintf (file, "\tLine: %d\n", eloc.line);
583 fprintf (file, "\tColumn: %d\n", eloc.column);
586 DEBUG_FUNCTION void
587 debug (cp_parser &ref)
589 cp_debug_parser (stderr, &ref);
592 DEBUG_FUNCTION void
593 debug (cp_parser *ptr)
595 if (ptr)
596 debug (*ptr);
597 else
598 fprintf (stderr, "<nil>\n");
601 /* Allocate memory for a new lexer object and return it. */
603 static cp_lexer *
604 cp_lexer_alloc (void)
606 cp_lexer *lexer;
608 c_common_no_more_pch ();
610 /* Allocate the memory. */
611 lexer = ggc_cleared_alloc<cp_lexer> ();
613 /* Initially we are not debugging. */
614 lexer->debugging_p = false;
616 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
618 /* Create the buffer. */
619 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
621 return lexer;
625 /* Create a new main C++ lexer, the lexer that gets tokens from the
626 preprocessor. */
628 static cp_lexer *
629 cp_lexer_new_main (void)
631 cp_lexer *lexer;
632 cp_token token;
634 /* It's possible that parsing the first pragma will load a PCH file,
635 which is a GC collection point. So we have to do that before
636 allocating any memory. */
637 cp_parser_initial_pragma (&token);
639 lexer = cp_lexer_alloc ();
641 /* Put the first token in the buffer. */
642 lexer->buffer->quick_push (token);
644 /* Get the remaining tokens from the preprocessor. */
645 while (token.type != CPP_EOF)
647 cp_lexer_get_preprocessor_token (lexer, &token);
648 vec_safe_push (lexer->buffer, token);
651 lexer->last_token = lexer->buffer->address ()
652 + lexer->buffer->length ()
653 - 1;
654 lexer->next_token = lexer->buffer->length ()
655 ? lexer->buffer->address ()
656 : &eof_token;
658 /* Subsequent preprocessor diagnostics should use compiler
659 diagnostic functions to get the compiler source location. */
660 done_lexing = true;
662 gcc_assert (!lexer->next_token->purged_p);
663 return lexer;
666 /* Create a new lexer whose token stream is primed with the tokens in
667 CACHE. When these tokens are exhausted, no new tokens will be read. */
669 static cp_lexer *
670 cp_lexer_new_from_tokens (cp_token_cache *cache)
672 cp_token *first = cache->first;
673 cp_token *last = cache->last;
674 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
676 /* We do not own the buffer. */
677 lexer->buffer = NULL;
678 lexer->next_token = first == last ? &eof_token : first;
679 lexer->last_token = last;
681 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
683 /* Initially we are not debugging. */
684 lexer->debugging_p = false;
686 gcc_assert (!lexer->next_token->purged_p);
687 return lexer;
690 /* Frees all resources associated with LEXER. */
692 static void
693 cp_lexer_destroy (cp_lexer *lexer)
695 vec_free (lexer->buffer);
696 lexer->saved_tokens.release ();
697 ggc_free (lexer);
700 /* Returns nonzero if debugging information should be output. */
702 static inline bool
703 cp_lexer_debugging_p (cp_lexer *lexer)
705 return lexer->debugging_p;
709 static inline cp_token_position
710 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
712 gcc_assert (!previous_p || lexer->next_token != &eof_token);
714 return lexer->next_token - previous_p;
717 static inline cp_token *
718 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
720 return pos;
723 static inline void
724 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
726 lexer->next_token = cp_lexer_token_at (lexer, pos);
729 static inline cp_token_position
730 cp_lexer_previous_token_position (cp_lexer *lexer)
732 if (lexer->next_token == &eof_token)
733 return lexer->last_token - 1;
734 else
735 return cp_lexer_token_position (lexer, true);
738 static inline cp_token *
739 cp_lexer_previous_token (cp_lexer *lexer)
741 cp_token_position tp = cp_lexer_previous_token_position (lexer);
743 return cp_lexer_token_at (lexer, tp);
746 /* nonzero if we are presently saving tokens. */
748 static inline int
749 cp_lexer_saving_tokens (const cp_lexer* lexer)
751 return lexer->saved_tokens.length () != 0;
754 /* Store the next token from the preprocessor in *TOKEN. Return true
755 if we reach EOF. If LEXER is NULL, assume we are handling an
756 initial #pragma pch_preprocess, and thus want the lexer to return
757 processed strings. */
759 static void
760 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
762 static int is_extern_c = 0;
764 /* Get a new token from the preprocessor. */
765 token->type
766 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
767 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
768 token->keyword = RID_MAX;
769 token->pragma_kind = PRAGMA_NONE;
770 token->purged_p = false;
771 token->error_reported = false;
773 /* On some systems, some header files are surrounded by an
774 implicit extern "C" block. Set a flag in the token if it
775 comes from such a header. */
776 is_extern_c += pending_lang_change;
777 pending_lang_change = 0;
778 token->implicit_extern_c = is_extern_c > 0;
780 /* Check to see if this token is a keyword. */
781 if (token->type == CPP_NAME)
783 if (C_IS_RESERVED_WORD (token->u.value))
785 /* Mark this token as a keyword. */
786 token->type = CPP_KEYWORD;
787 /* Record which keyword. */
788 token->keyword = C_RID_CODE (token->u.value);
790 else
792 if (warn_cxx11_compat
793 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
794 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
796 /* Warn about the C++0x keyword (but still treat it as
797 an identifier). */
798 warning (OPT_Wc__11_compat,
799 "identifier %qE is a keyword in C++11",
800 token->u.value);
802 /* Clear out the C_RID_CODE so we don't warn about this
803 particular identifier-turned-keyword again. */
804 C_SET_RID_CODE (token->u.value, RID_MAX);
807 token->keyword = RID_MAX;
810 else if (token->type == CPP_AT_NAME)
812 /* This only happens in Objective-C++; it must be a keyword. */
813 token->type = CPP_KEYWORD;
814 switch (C_RID_CODE (token->u.value))
816 /* Replace 'class' with '@class', 'private' with '@private',
817 etc. This prevents confusion with the C++ keyword
818 'class', and makes the tokens consistent with other
819 Objective-C 'AT' keywords. For example '@class' is
820 reported as RID_AT_CLASS which is consistent with
821 '@synchronized', which is reported as
822 RID_AT_SYNCHRONIZED.
824 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
825 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
826 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
827 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
828 case RID_THROW: token->keyword = RID_AT_THROW; break;
829 case RID_TRY: token->keyword = RID_AT_TRY; break;
830 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
831 default: token->keyword = C_RID_CODE (token->u.value);
834 else if (token->type == CPP_PRAGMA)
836 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
837 token->pragma_kind = ((enum pragma_kind)
838 TREE_INT_CST_LOW (token->u.value));
839 token->u.value = NULL_TREE;
843 /* Update the globals input_location and the input file stack from TOKEN. */
844 static inline void
845 cp_lexer_set_source_position_from_token (cp_token *token)
847 if (token->type != CPP_EOF)
849 input_location = token->location;
853 /* Update the globals input_location and the input file stack from LEXER. */
854 static inline void
855 cp_lexer_set_source_position (cp_lexer *lexer)
857 cp_token *token = cp_lexer_peek_token (lexer);
858 cp_lexer_set_source_position_from_token (token);
861 /* Return a pointer to the next token in the token stream, but do not
862 consume it. */
864 static inline cp_token *
865 cp_lexer_peek_token (cp_lexer *lexer)
867 if (cp_lexer_debugging_p (lexer))
869 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
870 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
871 putc ('\n', cp_lexer_debug_stream);
873 return lexer->next_token;
876 /* Return true if the next token has the indicated TYPE. */
878 static inline bool
879 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
881 return cp_lexer_peek_token (lexer)->type == type;
884 /* Return true if the next token does not have the indicated TYPE. */
886 static inline bool
887 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
889 return !cp_lexer_next_token_is (lexer, type);
892 /* Return true if the next token is the indicated KEYWORD. */
894 static inline bool
895 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
897 return cp_lexer_peek_token (lexer)->keyword == keyword;
900 static inline bool
901 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
903 return cp_lexer_peek_nth_token (lexer, n)->type == type;
906 static inline bool
907 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
909 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
912 /* Return true if the next token is not the indicated KEYWORD. */
914 static inline bool
915 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
917 return cp_lexer_peek_token (lexer)->keyword != keyword;
920 /* Return true if the next token is a keyword for a decl-specifier. */
922 static bool
923 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
925 cp_token *token;
927 token = cp_lexer_peek_token (lexer);
928 switch (token->keyword)
930 /* auto specifier: storage-class-specifier in C++,
931 simple-type-specifier in C++0x. */
932 case RID_AUTO:
933 /* Storage classes. */
934 case RID_REGISTER:
935 case RID_STATIC:
936 case RID_EXTERN:
937 case RID_MUTABLE:
938 case RID_THREAD:
939 /* Elaborated type specifiers. */
940 case RID_ENUM:
941 case RID_CLASS:
942 case RID_STRUCT:
943 case RID_UNION:
944 case RID_TYPENAME:
945 /* Simple type specifiers. */
946 case RID_CHAR:
947 case RID_CHAR16:
948 case RID_CHAR32:
949 case RID_WCHAR:
950 case RID_BOOL:
951 case RID_SHORT:
952 case RID_INT:
953 case RID_LONG:
954 case RID_SIGNED:
955 case RID_UNSIGNED:
956 case RID_FLOAT:
957 case RID_DOUBLE:
958 case RID_VOID:
959 /* GNU extensions. */
960 case RID_ATTRIBUTE:
961 case RID_TYPEOF:
962 /* C++0x extensions. */
963 case RID_DECLTYPE:
964 case RID_UNDERLYING_TYPE:
965 return true;
967 default:
968 if (token->keyword >= RID_FIRST_INT_N
969 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
970 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
971 return true;
972 return false;
976 /* Returns TRUE iff the token T begins a decltype type. */
978 static bool
979 token_is_decltype (cp_token *t)
981 return (t->keyword == RID_DECLTYPE
982 || t->type == CPP_DECLTYPE);
985 /* Returns TRUE iff the next token begins a decltype type. */
987 static bool
988 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
990 cp_token *t = cp_lexer_peek_token (lexer);
991 return token_is_decltype (t);
994 /* Return a pointer to the Nth token in the token stream. If N is 1,
995 then this is precisely equivalent to cp_lexer_peek_token (except
996 that it is not inline). One would like to disallow that case, but
997 there is one case (cp_parser_nth_token_starts_template_id) where
998 the caller passes a variable for N and it might be 1. */
1000 static cp_token *
1001 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1003 cp_token *token;
1005 /* N is 1-based, not zero-based. */
1006 gcc_assert (n > 0);
1008 if (cp_lexer_debugging_p (lexer))
1009 fprintf (cp_lexer_debug_stream,
1010 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1012 --n;
1013 token = lexer->next_token;
1014 gcc_assert (!n || token != &eof_token);
1015 while (n != 0)
1017 ++token;
1018 if (token == lexer->last_token)
1020 token = &eof_token;
1021 break;
1024 if (!token->purged_p)
1025 --n;
1028 if (cp_lexer_debugging_p (lexer))
1030 cp_lexer_print_token (cp_lexer_debug_stream, token);
1031 putc ('\n', cp_lexer_debug_stream);
1034 return token;
1037 /* Return the next token, and advance the lexer's next_token pointer
1038 to point to the next non-purged token. */
1040 static cp_token *
1041 cp_lexer_consume_token (cp_lexer* lexer)
1043 cp_token *token = lexer->next_token;
1045 gcc_assert (token != &eof_token);
1046 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1050 lexer->next_token++;
1051 if (lexer->next_token == lexer->last_token)
1053 lexer->next_token = &eof_token;
1054 break;
1058 while (lexer->next_token->purged_p);
1060 cp_lexer_set_source_position_from_token (token);
1062 /* Provide debugging output. */
1063 if (cp_lexer_debugging_p (lexer))
1065 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1066 cp_lexer_print_token (cp_lexer_debug_stream, token);
1067 putc ('\n', cp_lexer_debug_stream);
1070 return token;
1073 /* Permanently remove the next token from the token stream, and
1074 advance the next_token pointer to refer to the next non-purged
1075 token. */
1077 static void
1078 cp_lexer_purge_token (cp_lexer *lexer)
1080 cp_token *tok = lexer->next_token;
1082 gcc_assert (tok != &eof_token);
1083 tok->purged_p = true;
1084 tok->location = UNKNOWN_LOCATION;
1085 tok->u.value = NULL_TREE;
1086 tok->keyword = RID_MAX;
1090 tok++;
1091 if (tok == lexer->last_token)
1093 tok = &eof_token;
1094 break;
1097 while (tok->purged_p);
1098 lexer->next_token = tok;
1101 /* Permanently remove all tokens after TOK, up to, but not
1102 including, the token that will be returned next by
1103 cp_lexer_peek_token. */
1105 static void
1106 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1108 cp_token *peek = lexer->next_token;
1110 if (peek == &eof_token)
1111 peek = lexer->last_token;
1113 gcc_assert (tok < peek);
1115 for ( tok += 1; tok != peek; tok += 1)
1117 tok->purged_p = true;
1118 tok->location = UNKNOWN_LOCATION;
1119 tok->u.value = NULL_TREE;
1120 tok->keyword = RID_MAX;
1124 /* Begin saving tokens. All tokens consumed after this point will be
1125 preserved. */
1127 static void
1128 cp_lexer_save_tokens (cp_lexer* lexer)
1130 /* Provide debugging output. */
1131 if (cp_lexer_debugging_p (lexer))
1132 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1134 lexer->saved_tokens.safe_push (lexer->next_token);
1137 /* Commit to the portion of the token stream most recently saved. */
1139 static void
1140 cp_lexer_commit_tokens (cp_lexer* lexer)
1142 /* Provide debugging output. */
1143 if (cp_lexer_debugging_p (lexer))
1144 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1146 lexer->saved_tokens.pop ();
1149 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1150 to the token stream. Stop saving tokens. */
1152 static void
1153 cp_lexer_rollback_tokens (cp_lexer* lexer)
1155 /* Provide debugging output. */
1156 if (cp_lexer_debugging_p (lexer))
1157 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1159 lexer->next_token = lexer->saved_tokens.pop ();
1162 /* RAII wrapper around the above functions, with sanity checking. Creating
1163 a variable saves tokens, which are committed when the variable is
1164 destroyed unless they are explicitly rolled back by calling the rollback
1165 member function. */
1167 struct saved_token_sentinel
1169 cp_lexer *lexer;
1170 unsigned len;
1171 bool commit;
1172 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1174 len = lexer->saved_tokens.length ();
1175 cp_lexer_save_tokens (lexer);
1177 void rollback ()
1179 cp_lexer_rollback_tokens (lexer);
1180 commit = false;
1182 ~saved_token_sentinel()
1184 if (commit)
1185 cp_lexer_commit_tokens (lexer);
1186 gcc_assert (lexer->saved_tokens.length () == len);
1190 /* Print a representation of the TOKEN on the STREAM. */
1192 static void
1193 cp_lexer_print_token (FILE * stream, cp_token *token)
1195 /* We don't use cpp_type2name here because the parser defines
1196 a few tokens of its own. */
1197 static const char *const token_names[] = {
1198 /* cpplib-defined token types */
1199 #define OP(e, s) #e,
1200 #define TK(e, s) #e,
1201 TTYPE_TABLE
1202 #undef OP
1203 #undef TK
1204 /* C++ parser token types - see "Manifest constants", above. */
1205 "KEYWORD",
1206 "TEMPLATE_ID",
1207 "NESTED_NAME_SPECIFIER",
1210 /* For some tokens, print the associated data. */
1211 switch (token->type)
1213 case CPP_KEYWORD:
1214 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1215 For example, `struct' is mapped to an INTEGER_CST. */
1216 if (!identifier_p (token->u.value))
1217 break;
1218 /* else fall through */
1219 case CPP_NAME:
1220 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1221 break;
1223 case CPP_STRING:
1224 case CPP_STRING16:
1225 case CPP_STRING32:
1226 case CPP_WSTRING:
1227 case CPP_UTF8STRING:
1228 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1229 break;
1231 case CPP_NUMBER:
1232 print_generic_expr (stream, token->u.value, 0);
1233 break;
1235 default:
1236 /* If we have a name for the token, print it out. Otherwise, we
1237 simply give the numeric code. */
1238 if (token->type < ARRAY_SIZE(token_names))
1239 fputs (token_names[token->type], stream);
1240 else
1241 fprintf (stream, "[%d]", token->type);
1242 break;
1246 DEBUG_FUNCTION void
1247 debug (cp_token &ref)
1249 cp_lexer_print_token (stderr, &ref);
1250 fprintf (stderr, "\n");
1253 DEBUG_FUNCTION void
1254 debug (cp_token *ptr)
1256 if (ptr)
1257 debug (*ptr);
1258 else
1259 fprintf (stderr, "<nil>\n");
1263 /* Start emitting debugging information. */
1265 static void
1266 cp_lexer_start_debugging (cp_lexer* lexer)
1268 lexer->debugging_p = true;
1269 cp_lexer_debug_stream = stderr;
1272 /* Stop emitting debugging information. */
1274 static void
1275 cp_lexer_stop_debugging (cp_lexer* lexer)
1277 lexer->debugging_p = false;
1278 cp_lexer_debug_stream = NULL;
1281 /* Create a new cp_token_cache, representing a range of tokens. */
1283 static cp_token_cache *
1284 cp_token_cache_new (cp_token *first, cp_token *last)
1286 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1287 cache->first = first;
1288 cache->last = last;
1289 return cache;
1292 /* Diagnose if #pragma omp declare simd isn't followed immediately
1293 by function declaration or definition. */
1295 static inline void
1296 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1298 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1300 error ("%<#pragma omp declare simd%> not immediately followed by "
1301 "function declaration or definition");
1302 parser->omp_declare_simd = NULL;
1306 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1307 and put that into "omp declare simd" attribute. */
1309 static inline void
1310 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1312 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1314 if (fndecl == error_mark_node)
1316 parser->omp_declare_simd = NULL;
1317 return;
1319 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1321 cp_ensure_no_omp_declare_simd (parser);
1322 return;
1327 /* Decl-specifiers. */
1329 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1331 static void
1332 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1334 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1337 /* Declarators. */
1339 /* Nothing other than the parser should be creating declarators;
1340 declarators are a semi-syntactic representation of C++ entities.
1341 Other parts of the front end that need to create entities (like
1342 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1344 static cp_declarator *make_call_declarator
1345 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1346 static cp_declarator *make_array_declarator
1347 (cp_declarator *, tree);
1348 static cp_declarator *make_pointer_declarator
1349 (cp_cv_quals, cp_declarator *, tree);
1350 static cp_declarator *make_reference_declarator
1351 (cp_cv_quals, cp_declarator *, bool, tree);
1352 static cp_parameter_declarator *make_parameter_declarator
1353 (cp_decl_specifier_seq *, cp_declarator *, tree);
1354 static cp_declarator *make_ptrmem_declarator
1355 (cp_cv_quals, tree, cp_declarator *, tree);
1357 /* An erroneous declarator. */
1358 static cp_declarator *cp_error_declarator;
1360 /* The obstack on which declarators and related data structures are
1361 allocated. */
1362 static struct obstack declarator_obstack;
1364 /* Alloc BYTES from the declarator memory pool. */
1366 static inline void *
1367 alloc_declarator (size_t bytes)
1369 return obstack_alloc (&declarator_obstack, bytes);
1372 /* Allocate a declarator of the indicated KIND. Clear fields that are
1373 common to all declarators. */
1375 static cp_declarator *
1376 make_declarator (cp_declarator_kind kind)
1378 cp_declarator *declarator;
1380 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1381 declarator->kind = kind;
1382 declarator->attributes = NULL_TREE;
1383 declarator->std_attributes = NULL_TREE;
1384 declarator->declarator = NULL;
1385 declarator->parameter_pack_p = false;
1386 declarator->id_loc = UNKNOWN_LOCATION;
1388 return declarator;
1391 /* Make a declarator for a generalized identifier. If
1392 QUALIFYING_SCOPE is non-NULL, the identifier is
1393 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1394 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1395 is, if any. */
1397 static cp_declarator *
1398 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1399 special_function_kind sfk)
1401 cp_declarator *declarator;
1403 /* It is valid to write:
1405 class C { void f(); };
1406 typedef C D;
1407 void D::f();
1409 The standard is not clear about whether `typedef const C D' is
1410 legal; as of 2002-09-15 the committee is considering that
1411 question. EDG 3.0 allows that syntax. Therefore, we do as
1412 well. */
1413 if (qualifying_scope && TYPE_P (qualifying_scope))
1414 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1416 gcc_assert (identifier_p (unqualified_name)
1417 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1418 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1420 declarator = make_declarator (cdk_id);
1421 declarator->u.id.qualifying_scope = qualifying_scope;
1422 declarator->u.id.unqualified_name = unqualified_name;
1423 declarator->u.id.sfk = sfk;
1425 return declarator;
1428 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1429 of modifiers such as const or volatile to apply to the pointer
1430 type, represented as identifiers. ATTRIBUTES represent the attributes that
1431 appertain to the pointer or reference. */
1433 cp_declarator *
1434 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1435 tree attributes)
1437 cp_declarator *declarator;
1439 declarator = make_declarator (cdk_pointer);
1440 declarator->declarator = target;
1441 declarator->u.pointer.qualifiers = cv_qualifiers;
1442 declarator->u.pointer.class_type = NULL_TREE;
1443 if (target)
1445 declarator->id_loc = target->id_loc;
1446 declarator->parameter_pack_p = target->parameter_pack_p;
1447 target->parameter_pack_p = false;
1449 else
1450 declarator->parameter_pack_p = false;
1452 declarator->std_attributes = attributes;
1454 return declarator;
1457 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1458 represent the attributes that appertain to the pointer or
1459 reference. */
1461 cp_declarator *
1462 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1463 bool rvalue_ref, tree attributes)
1465 cp_declarator *declarator;
1467 declarator = make_declarator (cdk_reference);
1468 declarator->declarator = target;
1469 declarator->u.reference.qualifiers = cv_qualifiers;
1470 declarator->u.reference.rvalue_ref = rvalue_ref;
1471 if (target)
1473 declarator->id_loc = target->id_loc;
1474 declarator->parameter_pack_p = target->parameter_pack_p;
1475 target->parameter_pack_p = false;
1477 else
1478 declarator->parameter_pack_p = false;
1480 declarator->std_attributes = attributes;
1482 return declarator;
1485 /* Like make_pointer_declarator -- but for a pointer to a non-static
1486 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1487 appertain to the pointer or reference. */
1489 cp_declarator *
1490 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1491 cp_declarator *pointee,
1492 tree attributes)
1494 cp_declarator *declarator;
1496 declarator = make_declarator (cdk_ptrmem);
1497 declarator->declarator = pointee;
1498 declarator->u.pointer.qualifiers = cv_qualifiers;
1499 declarator->u.pointer.class_type = class_type;
1501 if (pointee)
1503 declarator->parameter_pack_p = pointee->parameter_pack_p;
1504 pointee->parameter_pack_p = false;
1506 else
1507 declarator->parameter_pack_p = false;
1509 declarator->std_attributes = attributes;
1511 return declarator;
1514 /* Make a declarator for the function given by TARGET, with the
1515 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1516 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1517 indicates what exceptions can be thrown. */
1519 cp_declarator *
1520 make_call_declarator (cp_declarator *target,
1521 tree parms,
1522 cp_cv_quals cv_qualifiers,
1523 cp_virt_specifiers virt_specifiers,
1524 cp_ref_qualifier ref_qualifier,
1525 tree exception_specification,
1526 tree late_return_type)
1528 cp_declarator *declarator;
1530 declarator = make_declarator (cdk_function);
1531 declarator->declarator = target;
1532 declarator->u.function.parameters = parms;
1533 declarator->u.function.qualifiers = cv_qualifiers;
1534 declarator->u.function.virt_specifiers = virt_specifiers;
1535 declarator->u.function.ref_qualifier = ref_qualifier;
1536 declarator->u.function.exception_specification = exception_specification;
1537 declarator->u.function.late_return_type = late_return_type;
1538 if (target)
1540 declarator->id_loc = target->id_loc;
1541 declarator->parameter_pack_p = target->parameter_pack_p;
1542 target->parameter_pack_p = false;
1544 else
1545 declarator->parameter_pack_p = false;
1547 return declarator;
1550 /* Make a declarator for an array of BOUNDS elements, each of which is
1551 defined by ELEMENT. */
1553 cp_declarator *
1554 make_array_declarator (cp_declarator *element, tree bounds)
1556 cp_declarator *declarator;
1558 declarator = make_declarator (cdk_array);
1559 declarator->declarator = element;
1560 declarator->u.array.bounds = bounds;
1561 if (element)
1563 declarator->id_loc = element->id_loc;
1564 declarator->parameter_pack_p = element->parameter_pack_p;
1565 element->parameter_pack_p = false;
1567 else
1568 declarator->parameter_pack_p = false;
1570 return declarator;
1573 /* Determine whether the declarator we've seen so far can be a
1574 parameter pack, when followed by an ellipsis. */
1575 static bool
1576 declarator_can_be_parameter_pack (cp_declarator *declarator)
1578 /* Search for a declarator name, or any other declarator that goes
1579 after the point where the ellipsis could appear in a parameter
1580 pack. If we find any of these, then this declarator can not be
1581 made into a parameter pack. */
1582 bool found = false;
1583 while (declarator && !found)
1585 switch ((int)declarator->kind)
1587 case cdk_id:
1588 case cdk_array:
1589 found = true;
1590 break;
1592 case cdk_error:
1593 return true;
1595 default:
1596 declarator = declarator->declarator;
1597 break;
1601 return !found;
1604 cp_parameter_declarator *no_parameters;
1606 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1607 DECLARATOR and DEFAULT_ARGUMENT. */
1609 cp_parameter_declarator *
1610 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1611 cp_declarator *declarator,
1612 tree default_argument)
1614 cp_parameter_declarator *parameter;
1616 parameter = ((cp_parameter_declarator *)
1617 alloc_declarator (sizeof (cp_parameter_declarator)));
1618 parameter->next = NULL;
1619 if (decl_specifiers)
1620 parameter->decl_specifiers = *decl_specifiers;
1621 else
1622 clear_decl_specs (&parameter->decl_specifiers);
1623 parameter->declarator = declarator;
1624 parameter->default_argument = default_argument;
1625 parameter->ellipsis_p = false;
1627 return parameter;
1630 /* Returns true iff DECLARATOR is a declaration for a function. */
1632 static bool
1633 function_declarator_p (const cp_declarator *declarator)
1635 while (declarator)
1637 if (declarator->kind == cdk_function
1638 && declarator->declarator->kind == cdk_id)
1639 return true;
1640 if (declarator->kind == cdk_id
1641 || declarator->kind == cdk_error)
1642 return false;
1643 declarator = declarator->declarator;
1645 return false;
1648 /* The parser. */
1650 /* Overview
1651 --------
1653 A cp_parser parses the token stream as specified by the C++
1654 grammar. Its job is purely parsing, not semantic analysis. For
1655 example, the parser breaks the token stream into declarators,
1656 expressions, statements, and other similar syntactic constructs.
1657 It does not check that the types of the expressions on either side
1658 of an assignment-statement are compatible, or that a function is
1659 not declared with a parameter of type `void'.
1661 The parser invokes routines elsewhere in the compiler to perform
1662 semantic analysis and to build up the abstract syntax tree for the
1663 code processed.
1665 The parser (and the template instantiation code, which is, in a
1666 way, a close relative of parsing) are the only parts of the
1667 compiler that should be calling push_scope and pop_scope, or
1668 related functions. The parser (and template instantiation code)
1669 keeps track of what scope is presently active; everything else
1670 should simply honor that. (The code that generates static
1671 initializers may also need to set the scope, in order to check
1672 access control correctly when emitting the initializers.)
1674 Methodology
1675 -----------
1677 The parser is of the standard recursive-descent variety. Upcoming
1678 tokens in the token stream are examined in order to determine which
1679 production to use when parsing a non-terminal. Some C++ constructs
1680 require arbitrary look ahead to disambiguate. For example, it is
1681 impossible, in the general case, to tell whether a statement is an
1682 expression or declaration without scanning the entire statement.
1683 Therefore, the parser is capable of "parsing tentatively." When the
1684 parser is not sure what construct comes next, it enters this mode.
1685 Then, while we attempt to parse the construct, the parser queues up
1686 error messages, rather than issuing them immediately, and saves the
1687 tokens it consumes. If the construct is parsed successfully, the
1688 parser "commits", i.e., it issues any queued error messages and
1689 the tokens that were being preserved are permanently discarded.
1690 If, however, the construct is not parsed successfully, the parser
1691 rolls back its state completely so that it can resume parsing using
1692 a different alternative.
1694 Future Improvements
1695 -------------------
1697 The performance of the parser could probably be improved substantially.
1698 We could often eliminate the need to parse tentatively by looking ahead
1699 a little bit. In some places, this approach might not entirely eliminate
1700 the need to parse tentatively, but it might still speed up the average
1701 case. */
1703 /* Flags that are passed to some parsing functions. These values can
1704 be bitwise-ored together. */
1706 enum
1708 /* No flags. */
1709 CP_PARSER_FLAGS_NONE = 0x0,
1710 /* The construct is optional. If it is not present, then no error
1711 should be issued. */
1712 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1713 /* When parsing a type-specifier, treat user-defined type-names
1714 as non-type identifiers. */
1715 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1716 /* When parsing a type-specifier, do not try to parse a class-specifier
1717 or enum-specifier. */
1718 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1719 /* When parsing a decl-specifier-seq, only allow type-specifier or
1720 constexpr. */
1721 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1724 /* This type is used for parameters and variables which hold
1725 combinations of the above flags. */
1726 typedef int cp_parser_flags;
1728 /* The different kinds of declarators we want to parse. */
1730 typedef enum cp_parser_declarator_kind
1732 /* We want an abstract declarator. */
1733 CP_PARSER_DECLARATOR_ABSTRACT,
1734 /* We want a named declarator. */
1735 CP_PARSER_DECLARATOR_NAMED,
1736 /* We don't mind, but the name must be an unqualified-id. */
1737 CP_PARSER_DECLARATOR_EITHER
1738 } cp_parser_declarator_kind;
1740 /* The precedence values used to parse binary expressions. The minimum value
1741 of PREC must be 1, because zero is reserved to quickly discriminate
1742 binary operators from other tokens. */
1744 enum cp_parser_prec
1746 PREC_NOT_OPERATOR,
1747 PREC_LOGICAL_OR_EXPRESSION,
1748 PREC_LOGICAL_AND_EXPRESSION,
1749 PREC_INCLUSIVE_OR_EXPRESSION,
1750 PREC_EXCLUSIVE_OR_EXPRESSION,
1751 PREC_AND_EXPRESSION,
1752 PREC_EQUALITY_EXPRESSION,
1753 PREC_RELATIONAL_EXPRESSION,
1754 PREC_SHIFT_EXPRESSION,
1755 PREC_ADDITIVE_EXPRESSION,
1756 PREC_MULTIPLICATIVE_EXPRESSION,
1757 PREC_PM_EXPRESSION,
1758 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1761 /* A mapping from a token type to a corresponding tree node type, with a
1762 precedence value. */
1764 typedef struct cp_parser_binary_operations_map_node
1766 /* The token type. */
1767 enum cpp_ttype token_type;
1768 /* The corresponding tree code. */
1769 enum tree_code tree_type;
1770 /* The precedence of this operator. */
1771 enum cp_parser_prec prec;
1772 } cp_parser_binary_operations_map_node;
1774 typedef struct cp_parser_expression_stack_entry
1776 /* Left hand side of the binary operation we are currently
1777 parsing. */
1778 tree lhs;
1779 /* Original tree code for left hand side, if it was a binary
1780 expression itself (used for -Wparentheses). */
1781 enum tree_code lhs_type;
1782 /* Tree code for the binary operation we are parsing. */
1783 enum tree_code tree_type;
1784 /* Precedence of the binary operation we are parsing. */
1785 enum cp_parser_prec prec;
1786 /* Location of the binary operation we are parsing. */
1787 location_t loc;
1788 } cp_parser_expression_stack_entry;
1790 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1791 entries because precedence levels on the stack are monotonically
1792 increasing. */
1793 typedef struct cp_parser_expression_stack_entry
1794 cp_parser_expression_stack[NUM_PREC_VALUES];
1796 /* Prototypes. */
1798 /* Constructors and destructors. */
1800 static cp_parser_context *cp_parser_context_new
1801 (cp_parser_context *);
1803 /* Class variables. */
1805 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1807 /* The operator-precedence table used by cp_parser_binary_expression.
1808 Transformed into an associative array (binops_by_token) by
1809 cp_parser_new. */
1811 static const cp_parser_binary_operations_map_node binops[] = {
1812 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1813 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1815 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1816 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1817 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1819 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1820 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1822 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1823 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1825 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1826 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1827 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1828 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1830 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1831 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1833 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1835 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1837 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1839 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1841 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1844 /* The same as binops, but initialized by cp_parser_new so that
1845 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1846 for speed. */
1847 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1849 /* Constructors and destructors. */
1851 /* Construct a new context. The context below this one on the stack
1852 is given by NEXT. */
1854 static cp_parser_context *
1855 cp_parser_context_new (cp_parser_context* next)
1857 cp_parser_context *context;
1859 /* Allocate the storage. */
1860 if (cp_parser_context_free_list != NULL)
1862 /* Pull the first entry from the free list. */
1863 context = cp_parser_context_free_list;
1864 cp_parser_context_free_list = context->next;
1865 memset (context, 0, sizeof (*context));
1867 else
1868 context = ggc_cleared_alloc<cp_parser_context> ();
1870 /* No errors have occurred yet in this context. */
1871 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1872 /* If this is not the bottommost context, copy information that we
1873 need from the previous context. */
1874 if (next)
1876 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1877 expression, then we are parsing one in this context, too. */
1878 context->object_type = next->object_type;
1879 /* Thread the stack. */
1880 context->next = next;
1883 return context;
1886 /* Managing the unparsed function queues. */
1888 #define unparsed_funs_with_default_args \
1889 parser->unparsed_queues->last ().funs_with_default_args
1890 #define unparsed_funs_with_definitions \
1891 parser->unparsed_queues->last ().funs_with_definitions
1892 #define unparsed_nsdmis \
1893 parser->unparsed_queues->last ().nsdmis
1894 #define unparsed_classes \
1895 parser->unparsed_queues->last ().classes
1897 static void
1898 push_unparsed_function_queues (cp_parser *parser)
1900 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1901 vec_safe_push (parser->unparsed_queues, e);
1904 static void
1905 pop_unparsed_function_queues (cp_parser *parser)
1907 release_tree_vector (unparsed_funs_with_definitions);
1908 parser->unparsed_queues->pop ();
1911 /* Prototypes. */
1913 /* Constructors and destructors. */
1915 static cp_parser *cp_parser_new
1916 (void);
1918 /* Routines to parse various constructs.
1920 Those that return `tree' will return the error_mark_node (rather
1921 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1922 Sometimes, they will return an ordinary node if error-recovery was
1923 attempted, even though a parse error occurred. So, to check
1924 whether or not a parse error occurred, you should always use
1925 cp_parser_error_occurred. If the construct is optional (indicated
1926 either by an `_opt' in the name of the function that does the
1927 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1928 the construct is not present. */
1930 /* Lexical conventions [gram.lex] */
1932 static tree cp_parser_identifier
1933 (cp_parser *);
1934 static tree cp_parser_string_literal
1935 (cp_parser *, bool, bool, bool);
1936 static tree cp_parser_userdef_char_literal
1937 (cp_parser *);
1938 static tree cp_parser_userdef_string_literal
1939 (tree);
1940 static tree cp_parser_userdef_numeric_literal
1941 (cp_parser *);
1943 /* Basic concepts [gram.basic] */
1945 static bool cp_parser_translation_unit
1946 (cp_parser *);
1948 /* Expressions [gram.expr] */
1950 static tree cp_parser_primary_expression
1951 (cp_parser *, bool, bool, bool, cp_id_kind *);
1952 static tree cp_parser_id_expression
1953 (cp_parser *, bool, bool, bool *, bool, bool);
1954 static tree cp_parser_unqualified_id
1955 (cp_parser *, bool, bool, bool, bool);
1956 static tree cp_parser_nested_name_specifier_opt
1957 (cp_parser *, bool, bool, bool, bool);
1958 static tree cp_parser_nested_name_specifier
1959 (cp_parser *, bool, bool, bool, bool);
1960 static tree cp_parser_qualifying_entity
1961 (cp_parser *, bool, bool, bool, bool, bool);
1962 static tree cp_parser_postfix_expression
1963 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1964 static tree cp_parser_postfix_open_square_expression
1965 (cp_parser *, tree, bool, bool);
1966 static tree cp_parser_postfix_dot_deref_expression
1967 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1968 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1969 (cp_parser *, int, bool, bool, bool *, bool = false);
1970 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1971 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1972 static void cp_parser_pseudo_destructor_name
1973 (cp_parser *, tree, tree *, tree *);
1974 static tree cp_parser_unary_expression
1975 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1976 static enum tree_code cp_parser_unary_operator
1977 (cp_token *);
1978 static tree cp_parser_new_expression
1979 (cp_parser *);
1980 static vec<tree, va_gc> *cp_parser_new_placement
1981 (cp_parser *);
1982 static tree cp_parser_new_type_id
1983 (cp_parser *, tree *);
1984 static cp_declarator *cp_parser_new_declarator_opt
1985 (cp_parser *);
1986 static cp_declarator *cp_parser_direct_new_declarator
1987 (cp_parser *);
1988 static vec<tree, va_gc> *cp_parser_new_initializer
1989 (cp_parser *);
1990 static tree cp_parser_delete_expression
1991 (cp_parser *);
1992 static tree cp_parser_cast_expression
1993 (cp_parser *, bool, bool, bool, cp_id_kind *);
1994 static tree cp_parser_binary_expression
1995 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1996 static tree cp_parser_question_colon_clause
1997 (cp_parser *, tree);
1998 static tree cp_parser_assignment_expression
1999 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2000 static enum tree_code cp_parser_assignment_operator_opt
2001 (cp_parser *);
2002 static tree cp_parser_expression
2003 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2004 static tree cp_parser_constant_expression
2005 (cp_parser *, bool = false, bool * = NULL);
2006 static tree cp_parser_builtin_offsetof
2007 (cp_parser *);
2008 static tree cp_parser_lambda_expression
2009 (cp_parser *);
2010 static void cp_parser_lambda_introducer
2011 (cp_parser *, tree);
2012 static bool cp_parser_lambda_declarator_opt
2013 (cp_parser *, tree);
2014 static void cp_parser_lambda_body
2015 (cp_parser *, tree);
2017 /* Statements [gram.stmt.stmt] */
2019 static void cp_parser_statement
2020 (cp_parser *, tree, bool, bool *);
2021 static void cp_parser_label_for_labeled_statement
2022 (cp_parser *, tree);
2023 static tree cp_parser_expression_statement
2024 (cp_parser *, tree);
2025 static tree cp_parser_compound_statement
2026 (cp_parser *, tree, bool, bool);
2027 static void cp_parser_statement_seq_opt
2028 (cp_parser *, tree);
2029 static tree cp_parser_selection_statement
2030 (cp_parser *, bool *);
2031 static tree cp_parser_condition
2032 (cp_parser *);
2033 static tree cp_parser_iteration_statement
2034 (cp_parser *, bool);
2035 static bool cp_parser_for_init_statement
2036 (cp_parser *, tree *decl);
2037 static tree cp_parser_for
2038 (cp_parser *, bool);
2039 static tree cp_parser_c_for
2040 (cp_parser *, tree, tree, bool);
2041 static tree cp_parser_range_for
2042 (cp_parser *, tree, tree, tree, bool);
2043 static void do_range_for_auto_deduction
2044 (tree, tree);
2045 static tree cp_parser_perform_range_for_lookup
2046 (tree, tree *, tree *);
2047 static tree cp_parser_range_for_member_function
2048 (tree, tree);
2049 static tree cp_parser_jump_statement
2050 (cp_parser *);
2051 static void cp_parser_declaration_statement
2052 (cp_parser *);
2054 static tree cp_parser_implicitly_scoped_statement
2055 (cp_parser *, bool *, location_t, const char *);
2056 static void cp_parser_already_scoped_statement
2057 (cp_parser *, location_t, const char *);
2059 /* Declarations [gram.dcl.dcl] */
2061 static void cp_parser_declaration_seq_opt
2062 (cp_parser *);
2063 static void cp_parser_declaration
2064 (cp_parser *);
2065 static void cp_parser_block_declaration
2066 (cp_parser *, bool);
2067 static void cp_parser_simple_declaration
2068 (cp_parser *, bool, tree *);
2069 static void cp_parser_decl_specifier_seq
2070 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2071 static tree cp_parser_storage_class_specifier_opt
2072 (cp_parser *);
2073 static tree cp_parser_function_specifier_opt
2074 (cp_parser *, cp_decl_specifier_seq *);
2075 static tree cp_parser_type_specifier
2076 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2077 int *, bool *);
2078 static tree cp_parser_simple_type_specifier
2079 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2080 static tree cp_parser_type_name
2081 (cp_parser *);
2082 static tree cp_parser_nonclass_name
2083 (cp_parser* parser);
2084 static tree cp_parser_elaborated_type_specifier
2085 (cp_parser *, bool, bool);
2086 static tree cp_parser_enum_specifier
2087 (cp_parser *);
2088 static void cp_parser_enumerator_list
2089 (cp_parser *, tree);
2090 static void cp_parser_enumerator_definition
2091 (cp_parser *, tree);
2092 static tree cp_parser_namespace_name
2093 (cp_parser *);
2094 static void cp_parser_namespace_definition
2095 (cp_parser *);
2096 static void cp_parser_namespace_body
2097 (cp_parser *);
2098 static tree cp_parser_qualified_namespace_specifier
2099 (cp_parser *);
2100 static void cp_parser_namespace_alias_definition
2101 (cp_parser *);
2102 static bool cp_parser_using_declaration
2103 (cp_parser *, bool);
2104 static void cp_parser_using_directive
2105 (cp_parser *);
2106 static tree cp_parser_alias_declaration
2107 (cp_parser *);
2108 static void cp_parser_asm_definition
2109 (cp_parser *);
2110 static void cp_parser_linkage_specification
2111 (cp_parser *);
2112 static void cp_parser_static_assert
2113 (cp_parser *, bool);
2114 static tree cp_parser_decltype
2115 (cp_parser *);
2117 /* Declarators [gram.dcl.decl] */
2119 static tree cp_parser_init_declarator
2120 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2121 bool, bool, int, bool *, tree *, location_t *);
2122 static cp_declarator *cp_parser_declarator
2123 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2124 static cp_declarator *cp_parser_direct_declarator
2125 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2126 static enum tree_code cp_parser_ptr_operator
2127 (cp_parser *, tree *, cp_cv_quals *, tree *);
2128 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2129 (cp_parser *);
2130 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2131 (cp_parser *);
2132 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2133 (cp_parser *);
2134 static tree cp_parser_late_return_type_opt
2135 (cp_parser *, cp_declarator *, cp_cv_quals);
2136 static tree cp_parser_declarator_id
2137 (cp_parser *, bool);
2138 static tree cp_parser_type_id
2139 (cp_parser *);
2140 static tree cp_parser_template_type_arg
2141 (cp_parser *);
2142 static tree cp_parser_trailing_type_id (cp_parser *);
2143 static tree cp_parser_type_id_1
2144 (cp_parser *, bool, bool);
2145 static void cp_parser_type_specifier_seq
2146 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2147 static tree cp_parser_parameter_declaration_clause
2148 (cp_parser *);
2149 static tree cp_parser_parameter_declaration_list
2150 (cp_parser *, bool *);
2151 static cp_parameter_declarator *cp_parser_parameter_declaration
2152 (cp_parser *, bool, bool *);
2153 static tree cp_parser_default_argument
2154 (cp_parser *, bool);
2155 static void cp_parser_function_body
2156 (cp_parser *, bool);
2157 static tree cp_parser_initializer
2158 (cp_parser *, bool *, bool *);
2159 static tree cp_parser_initializer_clause
2160 (cp_parser *, bool *);
2161 static tree cp_parser_braced_list
2162 (cp_parser*, bool*);
2163 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2164 (cp_parser *, bool *);
2166 static bool cp_parser_ctor_initializer_opt_and_function_body
2167 (cp_parser *, bool);
2169 static tree cp_parser_late_parsing_omp_declare_simd
2170 (cp_parser *, tree);
2172 static tree cp_parser_late_parsing_cilk_simd_fn_info
2173 (cp_parser *, tree);
2175 static tree synthesize_implicit_template_parm
2176 (cp_parser *);
2177 static tree finish_fully_implicit_template
2178 (cp_parser *, tree);
2180 /* Classes [gram.class] */
2182 static tree cp_parser_class_name
2183 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2184 static tree cp_parser_class_specifier
2185 (cp_parser *);
2186 static tree cp_parser_class_head
2187 (cp_parser *, bool *);
2188 static enum tag_types cp_parser_class_key
2189 (cp_parser *);
2190 static void cp_parser_type_parameter_key
2191 (cp_parser* parser);
2192 static void cp_parser_member_specification_opt
2193 (cp_parser *);
2194 static void cp_parser_member_declaration
2195 (cp_parser *);
2196 static tree cp_parser_pure_specifier
2197 (cp_parser *);
2198 static tree cp_parser_constant_initializer
2199 (cp_parser *);
2201 /* Derived classes [gram.class.derived] */
2203 static tree cp_parser_base_clause
2204 (cp_parser *);
2205 static tree cp_parser_base_specifier
2206 (cp_parser *);
2208 /* Special member functions [gram.special] */
2210 static tree cp_parser_conversion_function_id
2211 (cp_parser *);
2212 static tree cp_parser_conversion_type_id
2213 (cp_parser *);
2214 static cp_declarator *cp_parser_conversion_declarator_opt
2215 (cp_parser *);
2216 static bool cp_parser_ctor_initializer_opt
2217 (cp_parser *);
2218 static void cp_parser_mem_initializer_list
2219 (cp_parser *);
2220 static tree cp_parser_mem_initializer
2221 (cp_parser *);
2222 static tree cp_parser_mem_initializer_id
2223 (cp_parser *);
2225 /* Overloading [gram.over] */
2227 static tree cp_parser_operator_function_id
2228 (cp_parser *);
2229 static tree cp_parser_operator
2230 (cp_parser *);
2232 /* Templates [gram.temp] */
2234 static void cp_parser_template_declaration
2235 (cp_parser *, bool);
2236 static tree cp_parser_template_parameter_list
2237 (cp_parser *);
2238 static tree cp_parser_template_parameter
2239 (cp_parser *, bool *, bool *);
2240 static tree cp_parser_type_parameter
2241 (cp_parser *, bool *);
2242 static tree cp_parser_template_id
2243 (cp_parser *, bool, bool, enum tag_types, bool);
2244 static tree cp_parser_template_name
2245 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2246 static tree cp_parser_template_argument_list
2247 (cp_parser *);
2248 static tree cp_parser_template_argument
2249 (cp_parser *);
2250 static void cp_parser_explicit_instantiation
2251 (cp_parser *);
2252 static void cp_parser_explicit_specialization
2253 (cp_parser *);
2255 /* Exception handling [gram.exception] */
2257 static tree cp_parser_try_block
2258 (cp_parser *);
2259 static bool cp_parser_function_try_block
2260 (cp_parser *);
2261 static void cp_parser_handler_seq
2262 (cp_parser *);
2263 static void cp_parser_handler
2264 (cp_parser *);
2265 static tree cp_parser_exception_declaration
2266 (cp_parser *);
2267 static tree cp_parser_throw_expression
2268 (cp_parser *);
2269 static tree cp_parser_exception_specification_opt
2270 (cp_parser *);
2271 static tree cp_parser_type_id_list
2272 (cp_parser *);
2274 /* GNU Extensions */
2276 static tree cp_parser_asm_specification_opt
2277 (cp_parser *);
2278 static tree cp_parser_asm_operand_list
2279 (cp_parser *);
2280 static tree cp_parser_asm_clobber_list
2281 (cp_parser *);
2282 static tree cp_parser_asm_label_list
2283 (cp_parser *);
2284 static bool cp_next_tokens_can_be_attribute_p
2285 (cp_parser *);
2286 static bool cp_next_tokens_can_be_gnu_attribute_p
2287 (cp_parser *);
2288 static bool cp_next_tokens_can_be_std_attribute_p
2289 (cp_parser *);
2290 static bool cp_nth_tokens_can_be_std_attribute_p
2291 (cp_parser *, size_t);
2292 static bool cp_nth_tokens_can_be_gnu_attribute_p
2293 (cp_parser *, size_t);
2294 static bool cp_nth_tokens_can_be_attribute_p
2295 (cp_parser *, size_t);
2296 static tree cp_parser_attributes_opt
2297 (cp_parser *);
2298 static tree cp_parser_gnu_attributes_opt
2299 (cp_parser *);
2300 static tree cp_parser_gnu_attribute_list
2301 (cp_parser *);
2302 static tree cp_parser_std_attribute
2303 (cp_parser *);
2304 static tree cp_parser_std_attribute_spec
2305 (cp_parser *);
2306 static tree cp_parser_std_attribute_spec_seq
2307 (cp_parser *);
2308 static bool cp_parser_extension_opt
2309 (cp_parser *, int *);
2310 static void cp_parser_label_declaration
2311 (cp_parser *);
2313 /* Transactional Memory Extensions */
2315 static tree cp_parser_transaction
2316 (cp_parser *, enum rid);
2317 static tree cp_parser_transaction_expression
2318 (cp_parser *, enum rid);
2319 static bool cp_parser_function_transaction
2320 (cp_parser *, enum rid);
2321 static tree cp_parser_transaction_cancel
2322 (cp_parser *);
2324 enum pragma_context {
2325 pragma_external,
2326 pragma_member,
2327 pragma_objc_icode,
2328 pragma_stmt,
2329 pragma_compound
2331 static bool cp_parser_pragma
2332 (cp_parser *, enum pragma_context);
2334 /* Objective-C++ Productions */
2336 static tree cp_parser_objc_message_receiver
2337 (cp_parser *);
2338 static tree cp_parser_objc_message_args
2339 (cp_parser *);
2340 static tree cp_parser_objc_message_expression
2341 (cp_parser *);
2342 static tree cp_parser_objc_encode_expression
2343 (cp_parser *);
2344 static tree cp_parser_objc_defs_expression
2345 (cp_parser *);
2346 static tree cp_parser_objc_protocol_expression
2347 (cp_parser *);
2348 static tree cp_parser_objc_selector_expression
2349 (cp_parser *);
2350 static tree cp_parser_objc_expression
2351 (cp_parser *);
2352 static bool cp_parser_objc_selector_p
2353 (enum cpp_ttype);
2354 static tree cp_parser_objc_selector
2355 (cp_parser *);
2356 static tree cp_parser_objc_protocol_refs_opt
2357 (cp_parser *);
2358 static void cp_parser_objc_declaration
2359 (cp_parser *, tree);
2360 static tree cp_parser_objc_statement
2361 (cp_parser *);
2362 static bool cp_parser_objc_valid_prefix_attributes
2363 (cp_parser *, tree *);
2364 static void cp_parser_objc_at_property_declaration
2365 (cp_parser *) ;
2366 static void cp_parser_objc_at_synthesize_declaration
2367 (cp_parser *) ;
2368 static void cp_parser_objc_at_dynamic_declaration
2369 (cp_parser *) ;
2370 static tree cp_parser_objc_struct_declaration
2371 (cp_parser *) ;
2373 /* Utility Routines */
2375 static tree cp_parser_lookup_name
2376 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2377 static tree cp_parser_lookup_name_simple
2378 (cp_parser *, tree, location_t);
2379 static tree cp_parser_maybe_treat_template_as_class
2380 (tree, bool);
2381 static bool cp_parser_check_declarator_template_parameters
2382 (cp_parser *, cp_declarator *, location_t);
2383 static bool cp_parser_check_template_parameters
2384 (cp_parser *, unsigned, location_t, cp_declarator *);
2385 static tree cp_parser_simple_cast_expression
2386 (cp_parser *);
2387 static tree cp_parser_global_scope_opt
2388 (cp_parser *, bool);
2389 static bool cp_parser_constructor_declarator_p
2390 (cp_parser *, bool);
2391 static tree cp_parser_function_definition_from_specifiers_and_declarator
2392 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2393 static tree cp_parser_function_definition_after_declarator
2394 (cp_parser *, bool);
2395 static void cp_parser_template_declaration_after_export
2396 (cp_parser *, bool);
2397 static void cp_parser_perform_template_parameter_access_checks
2398 (vec<deferred_access_check, va_gc> *);
2399 static tree cp_parser_single_declaration
2400 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2401 static tree cp_parser_functional_cast
2402 (cp_parser *, tree);
2403 static tree cp_parser_save_member_function_body
2404 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2405 static tree cp_parser_save_nsdmi
2406 (cp_parser *);
2407 static tree cp_parser_enclosed_template_argument_list
2408 (cp_parser *);
2409 static void cp_parser_save_default_args
2410 (cp_parser *, tree);
2411 static void cp_parser_late_parsing_for_member
2412 (cp_parser *, tree);
2413 static tree cp_parser_late_parse_one_default_arg
2414 (cp_parser *, tree, tree, tree);
2415 static void cp_parser_late_parsing_nsdmi
2416 (cp_parser *, tree);
2417 static void cp_parser_late_parsing_default_args
2418 (cp_parser *, tree);
2419 static tree cp_parser_sizeof_operand
2420 (cp_parser *, enum rid);
2421 static tree cp_parser_trait_expr
2422 (cp_parser *, enum rid);
2423 static bool cp_parser_declares_only_class_p
2424 (cp_parser *);
2425 static void cp_parser_set_storage_class
2426 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2427 static void cp_parser_set_decl_spec_type
2428 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2429 static void set_and_check_decl_spec_loc
2430 (cp_decl_specifier_seq *decl_specs,
2431 cp_decl_spec ds, cp_token *);
2432 static bool cp_parser_friend_p
2433 (const cp_decl_specifier_seq *);
2434 static void cp_parser_required_error
2435 (cp_parser *, required_token, bool);
2436 static cp_token *cp_parser_require
2437 (cp_parser *, enum cpp_ttype, required_token);
2438 static cp_token *cp_parser_require_keyword
2439 (cp_parser *, enum rid, required_token);
2440 static bool cp_parser_token_starts_function_definition_p
2441 (cp_token *);
2442 static bool cp_parser_next_token_starts_class_definition_p
2443 (cp_parser *);
2444 static bool cp_parser_next_token_ends_template_argument_p
2445 (cp_parser *);
2446 static bool cp_parser_nth_token_starts_template_argument_list_p
2447 (cp_parser *, size_t);
2448 static enum tag_types cp_parser_token_is_class_key
2449 (cp_token *);
2450 static enum tag_types cp_parser_token_is_type_parameter_key
2451 (cp_token *);
2452 static void cp_parser_check_class_key
2453 (enum tag_types, tree type);
2454 static void cp_parser_check_access_in_redeclaration
2455 (tree type, location_t location);
2456 static bool cp_parser_optional_template_keyword
2457 (cp_parser *);
2458 static void cp_parser_pre_parsed_nested_name_specifier
2459 (cp_parser *);
2460 static bool cp_parser_cache_group
2461 (cp_parser *, enum cpp_ttype, unsigned);
2462 static tree cp_parser_cache_defarg
2463 (cp_parser *parser, bool nsdmi);
2464 static void cp_parser_parse_tentatively
2465 (cp_parser *);
2466 static void cp_parser_commit_to_tentative_parse
2467 (cp_parser *);
2468 static void cp_parser_commit_to_topmost_tentative_parse
2469 (cp_parser *);
2470 static void cp_parser_abort_tentative_parse
2471 (cp_parser *);
2472 static bool cp_parser_parse_definitely
2473 (cp_parser *);
2474 static inline bool cp_parser_parsing_tentatively
2475 (cp_parser *);
2476 static bool cp_parser_uncommitted_to_tentative_parse_p
2477 (cp_parser *);
2478 static void cp_parser_error
2479 (cp_parser *, const char *);
2480 static void cp_parser_name_lookup_error
2481 (cp_parser *, tree, tree, name_lookup_error, location_t);
2482 static bool cp_parser_simulate_error
2483 (cp_parser *);
2484 static bool cp_parser_check_type_definition
2485 (cp_parser *);
2486 static void cp_parser_check_for_definition_in_return_type
2487 (cp_declarator *, tree, location_t type_location);
2488 static void cp_parser_check_for_invalid_template_id
2489 (cp_parser *, tree, enum tag_types, location_t location);
2490 static bool cp_parser_non_integral_constant_expression
2491 (cp_parser *, non_integral_constant);
2492 static void cp_parser_diagnose_invalid_type_name
2493 (cp_parser *, tree, location_t);
2494 static bool cp_parser_parse_and_diagnose_invalid_type_name
2495 (cp_parser *);
2496 static int cp_parser_skip_to_closing_parenthesis
2497 (cp_parser *, bool, bool, bool);
2498 static void cp_parser_skip_to_end_of_statement
2499 (cp_parser *);
2500 static void cp_parser_consume_semicolon_at_end_of_statement
2501 (cp_parser *);
2502 static void cp_parser_skip_to_end_of_block_or_statement
2503 (cp_parser *);
2504 static bool cp_parser_skip_to_closing_brace
2505 (cp_parser *);
2506 static void cp_parser_skip_to_end_of_template_parameter_list
2507 (cp_parser *);
2508 static void cp_parser_skip_to_pragma_eol
2509 (cp_parser*, cp_token *);
2510 static bool cp_parser_error_occurred
2511 (cp_parser *);
2512 static bool cp_parser_allow_gnu_extensions_p
2513 (cp_parser *);
2514 static bool cp_parser_is_pure_string_literal
2515 (cp_token *);
2516 static bool cp_parser_is_string_literal
2517 (cp_token *);
2518 static bool cp_parser_is_keyword
2519 (cp_token *, enum rid);
2520 static tree cp_parser_make_typename_type
2521 (cp_parser *, tree, location_t location);
2522 static cp_declarator * cp_parser_make_indirect_declarator
2523 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2524 static bool cp_parser_compound_literal_p
2525 (cp_parser *);
2526 static bool cp_parser_array_designator_p
2527 (cp_parser *);
2528 static bool cp_parser_skip_to_closing_square_bracket
2529 (cp_parser *);
2531 /* Returns nonzero if we are parsing tentatively. */
2533 static inline bool
2534 cp_parser_parsing_tentatively (cp_parser* parser)
2536 return parser->context->next != NULL;
2539 /* Returns nonzero if TOKEN is a string literal. */
2541 static bool
2542 cp_parser_is_pure_string_literal (cp_token* token)
2544 return (token->type == CPP_STRING ||
2545 token->type == CPP_STRING16 ||
2546 token->type == CPP_STRING32 ||
2547 token->type == CPP_WSTRING ||
2548 token->type == CPP_UTF8STRING);
2551 /* Returns nonzero if TOKEN is a string literal
2552 of a user-defined string literal. */
2554 static bool
2555 cp_parser_is_string_literal (cp_token* token)
2557 return (cp_parser_is_pure_string_literal (token) ||
2558 token->type == CPP_STRING_USERDEF ||
2559 token->type == CPP_STRING16_USERDEF ||
2560 token->type == CPP_STRING32_USERDEF ||
2561 token->type == CPP_WSTRING_USERDEF ||
2562 token->type == CPP_UTF8STRING_USERDEF);
2565 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2567 static bool
2568 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2570 return token->keyword == keyword;
2573 /* If not parsing tentatively, issue a diagnostic of the form
2574 FILE:LINE: MESSAGE before TOKEN
2575 where TOKEN is the next token in the input stream. MESSAGE
2576 (specified by the caller) is usually of the form "expected
2577 OTHER-TOKEN". */
2579 static void
2580 cp_parser_error (cp_parser* parser, const char* gmsgid)
2582 if (!cp_parser_simulate_error (parser))
2584 cp_token *token = cp_lexer_peek_token (parser->lexer);
2585 /* This diagnostic makes more sense if it is tagged to the line
2586 of the token we just peeked at. */
2587 cp_lexer_set_source_position_from_token (token);
2589 if (token->type == CPP_PRAGMA)
2591 error_at (token->location,
2592 "%<#pragma%> is not allowed here");
2593 cp_parser_skip_to_pragma_eol (parser, token);
2594 return;
2597 c_parse_error (gmsgid,
2598 /* Because c_parser_error does not understand
2599 CPP_KEYWORD, keywords are treated like
2600 identifiers. */
2601 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2602 token->u.value, token->flags);
2606 /* Issue an error about name-lookup failing. NAME is the
2607 IDENTIFIER_NODE DECL is the result of
2608 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2609 the thing that we hoped to find. */
2611 static void
2612 cp_parser_name_lookup_error (cp_parser* parser,
2613 tree name,
2614 tree decl,
2615 name_lookup_error desired,
2616 location_t location)
2618 /* If name lookup completely failed, tell the user that NAME was not
2619 declared. */
2620 if (decl == error_mark_node)
2622 if (parser->scope && parser->scope != global_namespace)
2623 error_at (location, "%<%E::%E%> has not been declared",
2624 parser->scope, name);
2625 else if (parser->scope == global_namespace)
2626 error_at (location, "%<::%E%> has not been declared", name);
2627 else if (parser->object_scope
2628 && !CLASS_TYPE_P (parser->object_scope))
2629 error_at (location, "request for member %qE in non-class type %qT",
2630 name, parser->object_scope);
2631 else if (parser->object_scope)
2632 error_at (location, "%<%T::%E%> has not been declared",
2633 parser->object_scope, name);
2634 else
2635 error_at (location, "%qE has not been declared", name);
2637 else if (parser->scope && parser->scope != global_namespace)
2639 switch (desired)
2641 case NLE_TYPE:
2642 error_at (location, "%<%E::%E%> is not a type",
2643 parser->scope, name);
2644 break;
2645 case NLE_CXX98:
2646 error_at (location, "%<%E::%E%> is not a class or namespace",
2647 parser->scope, name);
2648 break;
2649 case NLE_NOT_CXX98:
2650 error_at (location,
2651 "%<%E::%E%> is not a class, namespace, or enumeration",
2652 parser->scope, name);
2653 break;
2654 default:
2655 gcc_unreachable ();
2659 else if (parser->scope == global_namespace)
2661 switch (desired)
2663 case NLE_TYPE:
2664 error_at (location, "%<::%E%> is not a type", name);
2665 break;
2666 case NLE_CXX98:
2667 error_at (location, "%<::%E%> is not a class or namespace", name);
2668 break;
2669 case NLE_NOT_CXX98:
2670 error_at (location,
2671 "%<::%E%> is not a class, namespace, or enumeration",
2672 name);
2673 break;
2674 default:
2675 gcc_unreachable ();
2678 else
2680 switch (desired)
2682 case NLE_TYPE:
2683 error_at (location, "%qE is not a type", name);
2684 break;
2685 case NLE_CXX98:
2686 error_at (location, "%qE is not a class or namespace", name);
2687 break;
2688 case NLE_NOT_CXX98:
2689 error_at (location,
2690 "%qE is not a class, namespace, or enumeration", name);
2691 break;
2692 default:
2693 gcc_unreachable ();
2698 /* If we are parsing tentatively, remember that an error has occurred
2699 during this tentative parse. Returns true if the error was
2700 simulated; false if a message should be issued by the caller. */
2702 static bool
2703 cp_parser_simulate_error (cp_parser* parser)
2705 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2707 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2708 return true;
2710 return false;
2713 /* This function is called when a type is defined. If type
2714 definitions are forbidden at this point, an error message is
2715 issued. */
2717 static bool
2718 cp_parser_check_type_definition (cp_parser* parser)
2720 /* If types are forbidden here, issue a message. */
2721 if (parser->type_definition_forbidden_message)
2723 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2724 in the message need to be interpreted. */
2725 error (parser->type_definition_forbidden_message);
2726 return false;
2728 return true;
2731 /* This function is called when the DECLARATOR is processed. The TYPE
2732 was a type defined in the decl-specifiers. If it is invalid to
2733 define a type in the decl-specifiers for DECLARATOR, an error is
2734 issued. TYPE_LOCATION is the location of TYPE and is used
2735 for error reporting. */
2737 static void
2738 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2739 tree type, location_t type_location)
2741 /* [dcl.fct] forbids type definitions in return types.
2742 Unfortunately, it's not easy to know whether or not we are
2743 processing a return type until after the fact. */
2744 while (declarator
2745 && (declarator->kind == cdk_pointer
2746 || declarator->kind == cdk_reference
2747 || declarator->kind == cdk_ptrmem))
2748 declarator = declarator->declarator;
2749 if (declarator
2750 && declarator->kind == cdk_function)
2752 error_at (type_location,
2753 "new types may not be defined in a return type");
2754 inform (type_location,
2755 "(perhaps a semicolon is missing after the definition of %qT)",
2756 type);
2760 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2761 "<" in any valid C++ program. If the next token is indeed "<",
2762 issue a message warning the user about what appears to be an
2763 invalid attempt to form a template-id. LOCATION is the location
2764 of the type-specifier (TYPE) */
2766 static void
2767 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2768 tree type,
2769 enum tag_types tag_type,
2770 location_t location)
2772 cp_token_position start = 0;
2774 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2776 if (TYPE_P (type))
2777 error_at (location, "%qT is not a template", type);
2778 else if (identifier_p (type))
2780 if (tag_type != none_type)
2781 error_at (location, "%qE is not a class template", type);
2782 else
2783 error_at (location, "%qE is not a template", type);
2785 else
2786 error_at (location, "invalid template-id");
2787 /* Remember the location of the invalid "<". */
2788 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2789 start = cp_lexer_token_position (parser->lexer, true);
2790 /* Consume the "<". */
2791 cp_lexer_consume_token (parser->lexer);
2792 /* Parse the template arguments. */
2793 cp_parser_enclosed_template_argument_list (parser);
2794 /* Permanently remove the invalid template arguments so that
2795 this error message is not issued again. */
2796 if (start)
2797 cp_lexer_purge_tokens_after (parser->lexer, start);
2801 /* If parsing an integral constant-expression, issue an error message
2802 about the fact that THING appeared and return true. Otherwise,
2803 return false. In either case, set
2804 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2806 static bool
2807 cp_parser_non_integral_constant_expression (cp_parser *parser,
2808 non_integral_constant thing)
2810 parser->non_integral_constant_expression_p = true;
2811 if (parser->integral_constant_expression_p)
2813 if (!parser->allow_non_integral_constant_expression_p)
2815 const char *msg = NULL;
2816 switch (thing)
2818 case NIC_FLOAT:
2819 error ("floating-point literal "
2820 "cannot appear in a constant-expression");
2821 return true;
2822 case NIC_CAST:
2823 error ("a cast to a type other than an integral or "
2824 "enumeration type cannot appear in a "
2825 "constant-expression");
2826 return true;
2827 case NIC_TYPEID:
2828 error ("%<typeid%> operator "
2829 "cannot appear in a constant-expression");
2830 return true;
2831 case NIC_NCC:
2832 error ("non-constant compound literals "
2833 "cannot appear in a constant-expression");
2834 return true;
2835 case NIC_FUNC_CALL:
2836 error ("a function call "
2837 "cannot appear in a constant-expression");
2838 return true;
2839 case NIC_INC:
2840 error ("an increment "
2841 "cannot appear in a constant-expression");
2842 return true;
2843 case NIC_DEC:
2844 error ("an decrement "
2845 "cannot appear in a constant-expression");
2846 return true;
2847 case NIC_ARRAY_REF:
2848 error ("an array reference "
2849 "cannot appear in a constant-expression");
2850 return true;
2851 case NIC_ADDR_LABEL:
2852 error ("the address of a label "
2853 "cannot appear in a constant-expression");
2854 return true;
2855 case NIC_OVERLOADED:
2856 error ("calls to overloaded operators "
2857 "cannot appear in a constant-expression");
2858 return true;
2859 case NIC_ASSIGNMENT:
2860 error ("an assignment cannot appear in a constant-expression");
2861 return true;
2862 case NIC_COMMA:
2863 error ("a comma operator "
2864 "cannot appear in a constant-expression");
2865 return true;
2866 case NIC_CONSTRUCTOR:
2867 error ("a call to a constructor "
2868 "cannot appear in a constant-expression");
2869 return true;
2870 case NIC_TRANSACTION:
2871 error ("a transaction expression "
2872 "cannot appear in a constant-expression");
2873 return true;
2874 case NIC_THIS:
2875 msg = "this";
2876 break;
2877 case NIC_FUNC_NAME:
2878 msg = "__FUNCTION__";
2879 break;
2880 case NIC_PRETTY_FUNC:
2881 msg = "__PRETTY_FUNCTION__";
2882 break;
2883 case NIC_C99_FUNC:
2884 msg = "__func__";
2885 break;
2886 case NIC_VA_ARG:
2887 msg = "va_arg";
2888 break;
2889 case NIC_ARROW:
2890 msg = "->";
2891 break;
2892 case NIC_POINT:
2893 msg = ".";
2894 break;
2895 case NIC_STAR:
2896 msg = "*";
2897 break;
2898 case NIC_ADDR:
2899 msg = "&";
2900 break;
2901 case NIC_PREINCREMENT:
2902 msg = "++";
2903 break;
2904 case NIC_PREDECREMENT:
2905 msg = "--";
2906 break;
2907 case NIC_NEW:
2908 msg = "new";
2909 break;
2910 case NIC_DEL:
2911 msg = "delete";
2912 break;
2913 default:
2914 gcc_unreachable ();
2916 if (msg)
2917 error ("%qs cannot appear in a constant-expression", msg);
2918 return true;
2921 return false;
2924 /* Emit a diagnostic for an invalid type name. This function commits
2925 to the current active tentative parse, if any. (Otherwise, the
2926 problematic construct might be encountered again later, resulting
2927 in duplicate error messages.) LOCATION is the location of ID. */
2929 static void
2930 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2931 location_t location)
2933 tree decl, ambiguous_decls;
2934 cp_parser_commit_to_tentative_parse (parser);
2935 /* Try to lookup the identifier. */
2936 decl = cp_parser_lookup_name (parser, id, none_type,
2937 /*is_template=*/false,
2938 /*is_namespace=*/false,
2939 /*check_dependency=*/true,
2940 &ambiguous_decls, location);
2941 if (ambiguous_decls)
2942 /* If the lookup was ambiguous, an error will already have
2943 been issued. */
2944 return;
2945 /* If the lookup found a template-name, it means that the user forgot
2946 to specify an argument list. Emit a useful error message. */
2947 if (DECL_TYPE_TEMPLATE_P (decl))
2949 error_at (location,
2950 "invalid use of template-name %qE without an argument list",
2951 decl);
2952 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
2954 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2955 error_at (location, "invalid use of destructor %qD as a type", id);
2956 else if (TREE_CODE (decl) == TYPE_DECL)
2957 /* Something like 'unsigned A a;' */
2958 error_at (location, "invalid combination of multiple type-specifiers");
2959 else if (!parser->scope)
2961 /* Issue an error message. */
2962 error_at (location, "%qE does not name a type", id);
2963 /* If we're in a template class, it's possible that the user was
2964 referring to a type from a base class. For example:
2966 template <typename T> struct A { typedef T X; };
2967 template <typename T> struct B : public A<T> { X x; };
2969 The user should have said "typename A<T>::X". */
2970 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2971 inform (location, "C++11 %<constexpr%> only available with "
2972 "-std=c++11 or -std=gnu++11");
2973 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2974 inform (location, "C++11 %<noexcept%> only available with "
2975 "-std=c++11 or -std=gnu++11");
2976 else if (cxx_dialect < cxx11
2977 && TREE_CODE (id) == IDENTIFIER_NODE
2978 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2979 inform (location, "C++11 %<thread_local%> only available with "
2980 "-std=c++11 or -std=gnu++11");
2981 else if (processing_template_decl && current_class_type
2982 && TYPE_BINFO (current_class_type))
2984 tree b;
2986 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2988 b = TREE_CHAIN (b))
2990 tree base_type = BINFO_TYPE (b);
2991 if (CLASS_TYPE_P (base_type)
2992 && dependent_type_p (base_type))
2994 tree field;
2995 /* Go from a particular instantiation of the
2996 template (which will have an empty TYPE_FIELDs),
2997 to the main version. */
2998 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2999 for (field = TYPE_FIELDS (base_type);
3000 field;
3001 field = DECL_CHAIN (field))
3002 if (TREE_CODE (field) == TYPE_DECL
3003 && DECL_NAME (field) == id)
3005 inform (location,
3006 "(perhaps %<typename %T::%E%> was intended)",
3007 BINFO_TYPE (b), id);
3008 break;
3010 if (field)
3011 break;
3016 /* Here we diagnose qualified-ids where the scope is actually correct,
3017 but the identifier does not resolve to a valid type name. */
3018 else if (parser->scope != error_mark_node)
3020 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3022 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3023 error_at (location_of (id),
3024 "%qE in namespace %qE does not name a template type",
3025 id, parser->scope);
3026 else
3027 error_at (location_of (id),
3028 "%qE in namespace %qE does not name a type",
3029 id, parser->scope);
3030 if (DECL_P (decl))
3031 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3033 else if (CLASS_TYPE_P (parser->scope)
3034 && constructor_name_p (id, parser->scope))
3036 /* A<T>::A<T>() */
3037 error_at (location, "%<%T::%E%> names the constructor, not"
3038 " the type", parser->scope, id);
3039 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3040 error_at (location, "and %qT has no template constructors",
3041 parser->scope);
3043 else if (TYPE_P (parser->scope)
3044 && dependent_scope_p (parser->scope))
3045 error_at (location, "need %<typename%> before %<%T::%E%> because "
3046 "%qT is a dependent scope",
3047 parser->scope, id, parser->scope);
3048 else if (TYPE_P (parser->scope))
3050 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3051 error_at (location_of (id),
3052 "%qE in %q#T does not name a template type",
3053 id, parser->scope);
3054 else
3055 error_at (location_of (id),
3056 "%qE in %q#T does not name a type",
3057 id, parser->scope);
3058 if (DECL_P (decl))
3059 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3061 else
3062 gcc_unreachable ();
3066 /* Check for a common situation where a type-name should be present,
3067 but is not, and issue a sensible error message. Returns true if an
3068 invalid type-name was detected.
3070 The situation handled by this function are variable declarations of the
3071 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3072 Usually, `ID' should name a type, but if we got here it means that it
3073 does not. We try to emit the best possible error message depending on
3074 how exactly the id-expression looks like. */
3076 static bool
3077 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3079 tree id;
3080 cp_token *token = cp_lexer_peek_token (parser->lexer);
3082 /* Avoid duplicate error about ambiguous lookup. */
3083 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3085 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3086 if (next->type == CPP_NAME && next->error_reported)
3087 goto out;
3090 cp_parser_parse_tentatively (parser);
3091 id = cp_parser_id_expression (parser,
3092 /*template_keyword_p=*/false,
3093 /*check_dependency_p=*/true,
3094 /*template_p=*/NULL,
3095 /*declarator_p=*/true,
3096 /*optional_p=*/false);
3097 /* If the next token is a (, this is a function with no explicit return
3098 type, i.e. constructor, destructor or conversion op. */
3099 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3100 || TREE_CODE (id) == TYPE_DECL)
3102 cp_parser_abort_tentative_parse (parser);
3103 return false;
3105 if (!cp_parser_parse_definitely (parser))
3106 return false;
3108 /* Emit a diagnostic for the invalid type. */
3109 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3110 out:
3111 /* If we aren't in the middle of a declarator (i.e. in a
3112 parameter-declaration-clause), skip to the end of the declaration;
3113 there's no point in trying to process it. */
3114 if (!parser->in_declarator_p)
3115 cp_parser_skip_to_end_of_block_or_statement (parser);
3116 return true;
3119 /* Consume tokens up to, and including, the next non-nested closing `)'.
3120 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3121 are doing error recovery. Returns -1 if OR_COMMA is true and we
3122 found an unnested comma. */
3124 static int
3125 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3126 bool recovering,
3127 bool or_comma,
3128 bool consume_paren)
3130 unsigned paren_depth = 0;
3131 unsigned brace_depth = 0;
3132 unsigned square_depth = 0;
3134 if (recovering && !or_comma
3135 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3136 return 0;
3138 while (true)
3140 cp_token * token = cp_lexer_peek_token (parser->lexer);
3142 switch (token->type)
3144 case CPP_EOF:
3145 case CPP_PRAGMA_EOL:
3146 /* If we've run out of tokens, then there is no closing `)'. */
3147 return 0;
3149 /* This is good for lambda expression capture-lists. */
3150 case CPP_OPEN_SQUARE:
3151 ++square_depth;
3152 break;
3153 case CPP_CLOSE_SQUARE:
3154 if (!square_depth--)
3155 return 0;
3156 break;
3158 case CPP_SEMICOLON:
3159 /* This matches the processing in skip_to_end_of_statement. */
3160 if (!brace_depth)
3161 return 0;
3162 break;
3164 case CPP_OPEN_BRACE:
3165 ++brace_depth;
3166 break;
3167 case CPP_CLOSE_BRACE:
3168 if (!brace_depth--)
3169 return 0;
3170 break;
3172 case CPP_COMMA:
3173 if (recovering && or_comma && !brace_depth && !paren_depth
3174 && !square_depth)
3175 return -1;
3176 break;
3178 case CPP_OPEN_PAREN:
3179 if (!brace_depth)
3180 ++paren_depth;
3181 break;
3183 case CPP_CLOSE_PAREN:
3184 if (!brace_depth && !paren_depth--)
3186 if (consume_paren)
3187 cp_lexer_consume_token (parser->lexer);
3188 return 1;
3190 break;
3192 default:
3193 break;
3196 /* Consume the token. */
3197 cp_lexer_consume_token (parser->lexer);
3201 /* Consume tokens until we reach the end of the current statement.
3202 Normally, that will be just before consuming a `;'. However, if a
3203 non-nested `}' comes first, then we stop before consuming that. */
3205 static void
3206 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3208 unsigned nesting_depth = 0;
3210 /* Unwind generic function template scope if necessary. */
3211 if (parser->fully_implicit_function_template_p)
3212 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3214 while (true)
3216 cp_token *token = cp_lexer_peek_token (parser->lexer);
3218 switch (token->type)
3220 case CPP_EOF:
3221 case CPP_PRAGMA_EOL:
3222 /* If we've run out of tokens, stop. */
3223 return;
3225 case CPP_SEMICOLON:
3226 /* If the next token is a `;', we have reached the end of the
3227 statement. */
3228 if (!nesting_depth)
3229 return;
3230 break;
3232 case CPP_CLOSE_BRACE:
3233 /* If this is a non-nested '}', stop before consuming it.
3234 That way, when confronted with something like:
3236 { 3 + }
3238 we stop before consuming the closing '}', even though we
3239 have not yet reached a `;'. */
3240 if (nesting_depth == 0)
3241 return;
3243 /* If it is the closing '}' for a block that we have
3244 scanned, stop -- but only after consuming the token.
3245 That way given:
3247 void f g () { ... }
3248 typedef int I;
3250 we will stop after the body of the erroneously declared
3251 function, but before consuming the following `typedef'
3252 declaration. */
3253 if (--nesting_depth == 0)
3255 cp_lexer_consume_token (parser->lexer);
3256 return;
3259 case CPP_OPEN_BRACE:
3260 ++nesting_depth;
3261 break;
3263 default:
3264 break;
3267 /* Consume the token. */
3268 cp_lexer_consume_token (parser->lexer);
3272 /* This function is called at the end of a statement or declaration.
3273 If the next token is a semicolon, it is consumed; otherwise, error
3274 recovery is attempted. */
3276 static void
3277 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3279 /* Look for the trailing `;'. */
3280 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3282 /* If there is additional (erroneous) input, skip to the end of
3283 the statement. */
3284 cp_parser_skip_to_end_of_statement (parser);
3285 /* If the next token is now a `;', consume it. */
3286 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3287 cp_lexer_consume_token (parser->lexer);
3291 /* Skip tokens until we have consumed an entire block, or until we
3292 have consumed a non-nested `;'. */
3294 static void
3295 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3297 int 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 (nesting_depth >= 0)
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 /* Stop if this is an unnested ';'. */
3316 if (!nesting_depth)
3317 nesting_depth = -1;
3318 break;
3320 case CPP_CLOSE_BRACE:
3321 /* Stop if this is an unnested '}', or closes the outermost
3322 nesting level. */
3323 nesting_depth--;
3324 if (nesting_depth < 0)
3325 return;
3326 if (!nesting_depth)
3327 nesting_depth = -1;
3328 break;
3330 case CPP_OPEN_BRACE:
3331 /* Nest. */
3332 nesting_depth++;
3333 break;
3335 default:
3336 break;
3339 /* Consume the token. */
3340 cp_lexer_consume_token (parser->lexer);
3344 /* Skip tokens until a non-nested closing curly brace is the next
3345 token, or there are no more tokens. Return true in the first case,
3346 false otherwise. */
3348 static bool
3349 cp_parser_skip_to_closing_brace (cp_parser *parser)
3351 unsigned nesting_depth = 0;
3353 while (true)
3355 cp_token *token = cp_lexer_peek_token (parser->lexer);
3357 switch (token->type)
3359 case CPP_EOF:
3360 case CPP_PRAGMA_EOL:
3361 /* If we've run out of tokens, stop. */
3362 return false;
3364 case CPP_CLOSE_BRACE:
3365 /* If the next token is a non-nested `}', then we have reached
3366 the end of the current block. */
3367 if (nesting_depth-- == 0)
3368 return true;
3369 break;
3371 case CPP_OPEN_BRACE:
3372 /* If it the next token is a `{', then we are entering a new
3373 block. Consume the entire block. */
3374 ++nesting_depth;
3375 break;
3377 default:
3378 break;
3381 /* Consume the token. */
3382 cp_lexer_consume_token (parser->lexer);
3386 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3387 parameter is the PRAGMA token, allowing us to purge the entire pragma
3388 sequence. */
3390 static void
3391 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3393 cp_token *token;
3395 parser->lexer->in_pragma = false;
3398 token = cp_lexer_consume_token (parser->lexer);
3399 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3401 /* Ensure that the pragma is not parsed again. */
3402 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3405 /* Require pragma end of line, resyncing with it as necessary. The
3406 arguments are as for cp_parser_skip_to_pragma_eol. */
3408 static void
3409 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3411 parser->lexer->in_pragma = false;
3412 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3413 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3416 /* This is a simple wrapper around make_typename_type. When the id is
3417 an unresolved identifier node, we can provide a superior diagnostic
3418 using cp_parser_diagnose_invalid_type_name. */
3420 static tree
3421 cp_parser_make_typename_type (cp_parser *parser, tree id,
3422 location_t id_location)
3424 tree result;
3425 if (identifier_p (id))
3427 result = make_typename_type (parser->scope, id, typename_type,
3428 /*complain=*/tf_none);
3429 if (result == error_mark_node)
3430 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3431 return result;
3433 return make_typename_type (parser->scope, id, typename_type, tf_error);
3436 /* This is a wrapper around the
3437 make_{pointer,ptrmem,reference}_declarator functions that decides
3438 which one to call based on the CODE and CLASS_TYPE arguments. The
3439 CODE argument should be one of the values returned by
3440 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3441 appertain to the pointer or reference. */
3443 static cp_declarator *
3444 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3445 cp_cv_quals cv_qualifiers,
3446 cp_declarator *target,
3447 tree attributes)
3449 if (code == ERROR_MARK)
3450 return cp_error_declarator;
3452 if (code == INDIRECT_REF)
3453 if (class_type == NULL_TREE)
3454 return make_pointer_declarator (cv_qualifiers, target, attributes);
3455 else
3456 return make_ptrmem_declarator (cv_qualifiers, class_type,
3457 target, attributes);
3458 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3459 return make_reference_declarator (cv_qualifiers, target,
3460 false, attributes);
3461 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3462 return make_reference_declarator (cv_qualifiers, target,
3463 true, attributes);
3464 gcc_unreachable ();
3467 /* Create a new C++ parser. */
3469 static cp_parser *
3470 cp_parser_new (void)
3472 cp_parser *parser;
3473 cp_lexer *lexer;
3474 unsigned i;
3476 /* cp_lexer_new_main is called before doing GC allocation because
3477 cp_lexer_new_main might load a PCH file. */
3478 lexer = cp_lexer_new_main ();
3480 /* Initialize the binops_by_token so that we can get the tree
3481 directly from the token. */
3482 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3483 binops_by_token[binops[i].token_type] = binops[i];
3485 parser = ggc_cleared_alloc<cp_parser> ();
3486 parser->lexer = lexer;
3487 parser->context = cp_parser_context_new (NULL);
3489 /* For now, we always accept GNU extensions. */
3490 parser->allow_gnu_extensions_p = 1;
3492 /* The `>' token is a greater-than operator, not the end of a
3493 template-id. */
3494 parser->greater_than_is_operator_p = true;
3496 parser->default_arg_ok_p = true;
3498 /* We are not parsing a constant-expression. */
3499 parser->integral_constant_expression_p = false;
3500 parser->allow_non_integral_constant_expression_p = false;
3501 parser->non_integral_constant_expression_p = false;
3503 /* Local variable names are not forbidden. */
3504 parser->local_variables_forbidden_p = false;
3506 /* We are not processing an `extern "C"' declaration. */
3507 parser->in_unbraced_linkage_specification_p = false;
3509 /* We are not processing a declarator. */
3510 parser->in_declarator_p = false;
3512 /* We are not processing a template-argument-list. */
3513 parser->in_template_argument_list_p = false;
3515 /* We are not in an iteration statement. */
3516 parser->in_statement = 0;
3518 /* We are not in a switch statement. */
3519 parser->in_switch_statement_p = false;
3521 /* We are not parsing a type-id inside an expression. */
3522 parser->in_type_id_in_expr_p = false;
3524 /* Declarations aren't implicitly extern "C". */
3525 parser->implicit_extern_c = false;
3527 /* String literals should be translated to the execution character set. */
3528 parser->translate_strings_p = true;
3530 /* We are not parsing a function body. */
3531 parser->in_function_body = false;
3533 /* We can correct until told otherwise. */
3534 parser->colon_corrects_to_scope_p = true;
3536 /* The unparsed function queue is empty. */
3537 push_unparsed_function_queues (parser);
3539 /* There are no classes being defined. */
3540 parser->num_classes_being_defined = 0;
3542 /* No template parameters apply. */
3543 parser->num_template_parameter_lists = 0;
3545 /* Not declaring an implicit function template. */
3546 parser->auto_is_implicit_function_template_parm_p = false;
3547 parser->fully_implicit_function_template_p = false;
3548 parser->implicit_template_parms = 0;
3549 parser->implicit_template_scope = 0;
3551 return parser;
3554 /* Create a cp_lexer structure which will emit the tokens in CACHE
3555 and push it onto the parser's lexer stack. This is used for delayed
3556 parsing of in-class method bodies and default arguments, and should
3557 not be confused with tentative parsing. */
3558 static void
3559 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3561 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3562 lexer->next = parser->lexer;
3563 parser->lexer = lexer;
3565 /* Move the current source position to that of the first token in the
3566 new lexer. */
3567 cp_lexer_set_source_position_from_token (lexer->next_token);
3570 /* Pop the top lexer off the parser stack. This is never used for the
3571 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3572 static void
3573 cp_parser_pop_lexer (cp_parser *parser)
3575 cp_lexer *lexer = parser->lexer;
3576 parser->lexer = lexer->next;
3577 cp_lexer_destroy (lexer);
3579 /* Put the current source position back where it was before this
3580 lexer was pushed. */
3581 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3584 /* Lexical conventions [gram.lex] */
3586 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3587 identifier. */
3589 static tree
3590 cp_parser_identifier (cp_parser* parser)
3592 cp_token *token;
3594 /* Look for the identifier. */
3595 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3596 /* Return the value. */
3597 return token ? token->u.value : error_mark_node;
3600 /* Parse a sequence of adjacent string constants. Returns a
3601 TREE_STRING representing the combined, nul-terminated string
3602 constant. If TRANSLATE is true, translate the string to the
3603 execution character set. If WIDE_OK is true, a wide string is
3604 invalid here.
3606 C++98 [lex.string] says that if a narrow string literal token is
3607 adjacent to a wide string literal token, the behavior is undefined.
3608 However, C99 6.4.5p4 says that this results in a wide string literal.
3609 We follow C99 here, for consistency with the C front end.
3611 This code is largely lifted from lex_string() in c-lex.c.
3613 FUTURE: ObjC++ will need to handle @-strings here. */
3614 static tree
3615 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3616 bool lookup_udlit = true)
3618 tree value;
3619 size_t count;
3620 struct obstack str_ob;
3621 cpp_string str, istr, *strs;
3622 cp_token *tok;
3623 enum cpp_ttype type, curr_type;
3624 int have_suffix_p = 0;
3625 tree string_tree;
3626 tree suffix_id = NULL_TREE;
3627 bool curr_tok_is_userdef_p = false;
3629 tok = cp_lexer_peek_token (parser->lexer);
3630 if (!cp_parser_is_string_literal (tok))
3632 cp_parser_error (parser, "expected string-literal");
3633 return error_mark_node;
3636 if (cpp_userdef_string_p (tok->type))
3638 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3639 curr_type = cpp_userdef_string_remove_type (tok->type);
3640 curr_tok_is_userdef_p = true;
3642 else
3644 string_tree = tok->u.value;
3645 curr_type = tok->type;
3647 type = curr_type;
3649 /* Try to avoid the overhead of creating and destroying an obstack
3650 for the common case of just one string. */
3651 if (!cp_parser_is_string_literal
3652 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3654 cp_lexer_consume_token (parser->lexer);
3656 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3657 str.len = TREE_STRING_LENGTH (string_tree);
3658 count = 1;
3660 if (curr_tok_is_userdef_p)
3662 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3663 have_suffix_p = 1;
3664 curr_type = cpp_userdef_string_remove_type (tok->type);
3666 else
3667 curr_type = tok->type;
3669 strs = &str;
3671 else
3673 gcc_obstack_init (&str_ob);
3674 count = 0;
3678 cp_lexer_consume_token (parser->lexer);
3679 count++;
3680 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3681 str.len = TREE_STRING_LENGTH (string_tree);
3683 if (curr_tok_is_userdef_p)
3685 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3686 if (have_suffix_p == 0)
3688 suffix_id = curr_suffix_id;
3689 have_suffix_p = 1;
3691 else if (have_suffix_p == 1
3692 && curr_suffix_id != suffix_id)
3694 error ("inconsistent user-defined literal suffixes"
3695 " %qD and %qD in string literal",
3696 suffix_id, curr_suffix_id);
3697 have_suffix_p = -1;
3699 curr_type = cpp_userdef_string_remove_type (tok->type);
3701 else
3702 curr_type = tok->type;
3704 if (type != curr_type)
3706 if (type == CPP_STRING)
3707 type = curr_type;
3708 else if (curr_type != CPP_STRING)
3709 error_at (tok->location,
3710 "unsupported non-standard concatenation "
3711 "of string literals");
3714 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3716 tok = cp_lexer_peek_token (parser->lexer);
3717 if (cpp_userdef_string_p (tok->type))
3719 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3720 curr_type = cpp_userdef_string_remove_type (tok->type);
3721 curr_tok_is_userdef_p = true;
3723 else
3725 string_tree = tok->u.value;
3726 curr_type = tok->type;
3727 curr_tok_is_userdef_p = false;
3730 while (cp_parser_is_string_literal (tok));
3732 strs = (cpp_string *) obstack_finish (&str_ob);
3735 if (type != CPP_STRING && !wide_ok)
3737 cp_parser_error (parser, "a wide string is invalid in this context");
3738 type = CPP_STRING;
3741 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3742 (parse_in, strs, count, &istr, type))
3744 value = build_string (istr.len, (const char *)istr.text);
3745 free (CONST_CAST (unsigned char *, istr.text));
3747 switch (type)
3749 default:
3750 case CPP_STRING:
3751 case CPP_UTF8STRING:
3752 TREE_TYPE (value) = char_array_type_node;
3753 break;
3754 case CPP_STRING16:
3755 TREE_TYPE (value) = char16_array_type_node;
3756 break;
3757 case CPP_STRING32:
3758 TREE_TYPE (value) = char32_array_type_node;
3759 break;
3760 case CPP_WSTRING:
3761 TREE_TYPE (value) = wchar_array_type_node;
3762 break;
3765 value = fix_string_type (value);
3767 if (have_suffix_p)
3769 tree literal = build_userdef_literal (suffix_id, value,
3770 OT_NONE, NULL_TREE);
3771 if (lookup_udlit)
3772 value = cp_parser_userdef_string_literal (literal);
3773 else
3774 value = literal;
3777 else
3778 /* cpp_interpret_string has issued an error. */
3779 value = error_mark_node;
3781 if (count > 1)
3782 obstack_free (&str_ob, 0);
3784 return value;
3787 /* Look up a literal operator with the name and the exact arguments. */
3789 static tree
3790 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3792 tree decl, fns;
3793 decl = lookup_name (name);
3794 if (!decl || !is_overloaded_fn (decl))
3795 return error_mark_node;
3797 for (fns = decl; fns; fns = OVL_NEXT (fns))
3799 unsigned int ix;
3800 bool found = true;
3801 tree fn = OVL_CURRENT (fns);
3802 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3803 if (parmtypes != NULL_TREE)
3805 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3806 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3808 tree tparm = TREE_VALUE (parmtypes);
3809 tree targ = TREE_TYPE ((*args)[ix]);
3810 bool ptr = TYPE_PTR_P (tparm);
3811 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3812 if ((ptr || arr || !same_type_p (tparm, targ))
3813 && (!ptr || !arr
3814 || !same_type_p (TREE_TYPE (tparm),
3815 TREE_TYPE (targ))))
3816 found = false;
3818 if (found
3819 && ix == vec_safe_length (args)
3820 /* May be this should be sufficient_parms_p instead,
3821 depending on how exactly should user-defined literals
3822 work in presence of default arguments on the literal
3823 operator parameters. */
3824 && parmtypes == void_list_node)
3825 return decl;
3829 return error_mark_node;
3832 /* Parse a user-defined char constant. Returns a call to a user-defined
3833 literal operator taking the character as an argument. */
3835 static tree
3836 cp_parser_userdef_char_literal (cp_parser *parser)
3838 cp_token *token = cp_lexer_consume_token (parser->lexer);
3839 tree literal = token->u.value;
3840 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3841 tree value = USERDEF_LITERAL_VALUE (literal);
3842 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3843 tree decl, result;
3845 /* Build up a call to the user-defined operator */
3846 /* Lookup the name we got back from the id-expression. */
3847 vec<tree, va_gc> *args = make_tree_vector ();
3848 vec_safe_push (args, value);
3849 decl = lookup_literal_operator (name, args);
3850 if (!decl || decl == error_mark_node)
3852 error ("unable to find character literal operator %qD with %qT argument",
3853 name, TREE_TYPE (value));
3854 release_tree_vector (args);
3855 return error_mark_node;
3857 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3858 release_tree_vector (args);
3859 return result;
3862 /* A subroutine of cp_parser_userdef_numeric_literal to
3863 create a char... template parameter pack from a string node. */
3865 static tree
3866 make_char_string_pack (tree value)
3868 tree charvec;
3869 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3870 const char *str = TREE_STRING_POINTER (value);
3871 int i, len = TREE_STRING_LENGTH (value) - 1;
3872 tree argvec = make_tree_vec (1);
3874 /* Fill in CHARVEC with all of the parameters. */
3875 charvec = make_tree_vec (len);
3876 for (i = 0; i < len; ++i)
3877 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3879 /* Build the argument packs. */
3880 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3881 TREE_TYPE (argpack) = char_type_node;
3883 TREE_VEC_ELT (argvec, 0) = argpack;
3885 return argvec;
3888 /* A subroutine of cp_parser_userdef_numeric_literal to
3889 create a char... template parameter pack from a string node. */
3891 static tree
3892 make_string_pack (tree value)
3894 tree charvec;
3895 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3896 const unsigned char *str
3897 = (const unsigned char *) TREE_STRING_POINTER (value);
3898 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3899 int len = TREE_STRING_LENGTH (value) / sz - 1;
3900 tree argvec = make_tree_vec (2);
3902 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3903 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3905 /* First template parm is character type. */
3906 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3908 /* Fill in CHARVEC with all of the parameters. */
3909 charvec = make_tree_vec (len);
3910 for (int i = 0; i < len; ++i)
3911 TREE_VEC_ELT (charvec, i)
3912 = double_int_to_tree (str_char_type_node,
3913 double_int::from_buffer (str + i * sz, sz));
3915 /* Build the argument packs. */
3916 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3917 TREE_TYPE (argpack) = str_char_type_node;
3919 TREE_VEC_ELT (argvec, 1) = argpack;
3921 return argvec;
3924 /* Parse a user-defined numeric constant. returns a call to a user-defined
3925 literal operator. */
3927 static tree
3928 cp_parser_userdef_numeric_literal (cp_parser *parser)
3930 cp_token *token = cp_lexer_consume_token (parser->lexer);
3931 tree literal = token->u.value;
3932 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3933 tree value = USERDEF_LITERAL_VALUE (literal);
3934 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3935 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3936 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3937 tree decl, result;
3938 vec<tree, va_gc> *args;
3940 /* Look for a literal operator taking the exact type of numeric argument
3941 as the literal value. */
3942 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 result = finish_call_expr (decl, &args, false, true,
3948 tf_warning_or_error);
3950 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3952 warning_at (token->location, OPT_Woverflow,
3953 "integer literal exceeds range of %qT type",
3954 long_long_unsigned_type_node);
3956 else
3958 if (overflow > 0)
3959 warning_at (token->location, OPT_Woverflow,
3960 "floating literal exceeds range of %qT type",
3961 long_double_type_node);
3962 else if (overflow < 0)
3963 warning_at (token->location, OPT_Woverflow,
3964 "floating literal truncated to zero");
3967 release_tree_vector (args);
3968 return result;
3970 release_tree_vector (args);
3972 /* If the numeric argument didn't work, look for a raw literal
3973 operator taking a const char* argument consisting of the number
3974 in string format. */
3975 args = make_tree_vector ();
3976 vec_safe_push (args, num_string);
3977 decl = lookup_literal_operator (name, args);
3978 if (decl && decl != error_mark_node)
3980 result = finish_call_expr (decl, &args, false, true,
3981 tf_warning_or_error);
3982 release_tree_vector (args);
3983 return result;
3985 release_tree_vector (args);
3987 /* If the raw literal didn't work, look for a non-type template
3988 function with parameter pack char.... Call the function with
3989 template parameter characters representing the number. */
3990 args = make_tree_vector ();
3991 decl = lookup_literal_operator (name, args);
3992 if (decl && decl != error_mark_node)
3994 tree tmpl_args = make_char_string_pack (num_string);
3995 decl = lookup_template_function (decl, tmpl_args);
3996 result = finish_call_expr (decl, &args, false, true,
3997 tf_warning_or_error);
3998 release_tree_vector (args);
3999 return result;
4002 release_tree_vector (args);
4004 error ("unable to find numeric literal operator %qD", name);
4005 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4006 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4007 "to enable more built-in suffixes");
4008 return error_mark_node;
4011 /* Parse a user-defined string constant. Returns a call to a user-defined
4012 literal operator taking a character pointer and the length of the string
4013 as arguments. */
4015 static tree
4016 cp_parser_userdef_string_literal (tree literal)
4018 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4019 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4020 tree value = USERDEF_LITERAL_VALUE (literal);
4021 int len = TREE_STRING_LENGTH (value)
4022 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4023 tree decl, result;
4024 vec<tree, va_gc> *args;
4026 /* Build up a call to the user-defined operator. */
4027 /* Lookup the name we got back from the id-expression. */
4028 args = make_tree_vector ();
4029 vec_safe_push (args, value);
4030 vec_safe_push (args, build_int_cst (size_type_node, len));
4031 decl = lookup_literal_operator (name, args);
4033 if (decl && decl != error_mark_node)
4035 result = finish_call_expr (decl, &args, false, true,
4036 tf_warning_or_error);
4037 release_tree_vector (args);
4038 return result;
4040 release_tree_vector (args);
4042 /* Look for a template function with typename parameter CharT
4043 and parameter pack CharT... Call the function with
4044 template parameter characters representing the string. */
4045 args = make_tree_vector ();
4046 decl = lookup_literal_operator (name, args);
4047 if (decl && decl != error_mark_node)
4049 tree tmpl_args = make_string_pack (value);
4050 decl = lookup_template_function (decl, tmpl_args);
4051 result = finish_call_expr (decl, &args, false, true,
4052 tf_warning_or_error);
4053 release_tree_vector (args);
4054 return result;
4056 release_tree_vector (args);
4058 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4059 name, TREE_TYPE (value), size_type_node);
4060 return error_mark_node;
4064 /* Basic concepts [gram.basic] */
4066 /* Parse a translation-unit.
4068 translation-unit:
4069 declaration-seq [opt]
4071 Returns TRUE if all went well. */
4073 static bool
4074 cp_parser_translation_unit (cp_parser* parser)
4076 /* The address of the first non-permanent object on the declarator
4077 obstack. */
4078 static void *declarator_obstack_base;
4080 bool success;
4082 /* Create the declarator obstack, if necessary. */
4083 if (!cp_error_declarator)
4085 gcc_obstack_init (&declarator_obstack);
4086 /* Create the error declarator. */
4087 cp_error_declarator = make_declarator (cdk_error);
4088 /* Create the empty parameter list. */
4089 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4090 /* Remember where the base of the declarator obstack lies. */
4091 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4094 cp_parser_declaration_seq_opt (parser);
4096 /* If there are no tokens left then all went well. */
4097 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4099 /* Get rid of the token array; we don't need it any more. */
4100 cp_lexer_destroy (parser->lexer);
4101 parser->lexer = NULL;
4103 /* This file might have been a context that's implicitly extern
4104 "C". If so, pop the lang context. (Only relevant for PCH.) */
4105 if (parser->implicit_extern_c)
4107 pop_lang_context ();
4108 parser->implicit_extern_c = false;
4111 /* Finish up. */
4112 finish_translation_unit ();
4114 success = true;
4116 else
4118 cp_parser_error (parser, "expected declaration");
4119 success = false;
4122 /* Make sure the declarator obstack was fully cleaned up. */
4123 gcc_assert (obstack_next_free (&declarator_obstack)
4124 == declarator_obstack_base);
4126 /* All went well. */
4127 return success;
4130 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4131 decltype context. */
4133 static inline tsubst_flags_t
4134 complain_flags (bool decltype_p)
4136 tsubst_flags_t complain = tf_warning_or_error;
4137 if (decltype_p)
4138 complain |= tf_decltype;
4139 return complain;
4142 /* We're about to parse a collection of statements. If we're currently
4143 parsing tentatively, set up a firewall so that any nested
4144 cp_parser_commit_to_tentative_parse won't affect the current context. */
4146 static cp_token_position
4147 cp_parser_start_tentative_firewall (cp_parser *parser)
4149 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4150 return 0;
4152 cp_parser_parse_tentatively (parser);
4153 cp_parser_commit_to_topmost_tentative_parse (parser);
4154 return cp_lexer_token_position (parser->lexer, false);
4157 /* We've finished parsing the collection of statements. Wrap up the
4158 firewall and replace the relevant tokens with the parsed form. */
4160 static void
4161 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4162 tree expr)
4164 if (!start)
4165 return;
4167 /* Finish the firewall level. */
4168 cp_parser_parse_definitely (parser);
4169 /* And remember the result of the parse for when we try again. */
4170 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4171 token->type = CPP_PREPARSED_EXPR;
4172 token->u.value = expr;
4173 token->keyword = RID_MAX;
4174 cp_lexer_purge_tokens_after (parser->lexer, start);
4177 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4178 enclosing parentheses. */
4180 static tree
4181 cp_parser_statement_expr (cp_parser *parser)
4183 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4185 /* Consume the '('. */
4186 cp_lexer_consume_token (parser->lexer);
4187 /* Start the statement-expression. */
4188 tree expr = begin_stmt_expr ();
4189 /* Parse the compound-statement. */
4190 cp_parser_compound_statement (parser, expr, false, false);
4191 /* Finish up. */
4192 expr = finish_stmt_expr (expr, false);
4193 /* Consume the ')'. */
4194 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4195 cp_parser_skip_to_end_of_statement (parser);
4197 cp_parser_end_tentative_firewall (parser, start, expr);
4198 return expr;
4201 /* Expressions [gram.expr] */
4203 /* Parse a primary-expression.
4205 primary-expression:
4206 literal
4207 this
4208 ( expression )
4209 id-expression
4210 lambda-expression (C++11)
4212 GNU Extensions:
4214 primary-expression:
4215 ( compound-statement )
4216 __builtin_va_arg ( assignment-expression , type-id )
4217 __builtin_offsetof ( type-id , offsetof-expression )
4219 C++ Extensions:
4220 __has_nothrow_assign ( type-id )
4221 __has_nothrow_constructor ( type-id )
4222 __has_nothrow_copy ( type-id )
4223 __has_trivial_assign ( type-id )
4224 __has_trivial_constructor ( type-id )
4225 __has_trivial_copy ( type-id )
4226 __has_trivial_destructor ( type-id )
4227 __has_virtual_destructor ( type-id )
4228 __is_abstract ( type-id )
4229 __is_base_of ( type-id , type-id )
4230 __is_class ( type-id )
4231 __is_empty ( type-id )
4232 __is_enum ( type-id )
4233 __is_final ( type-id )
4234 __is_literal_type ( type-id )
4235 __is_pod ( type-id )
4236 __is_polymorphic ( type-id )
4237 __is_std_layout ( type-id )
4238 __is_trivial ( type-id )
4239 __is_union ( type-id )
4241 Objective-C++ Extension:
4243 primary-expression:
4244 objc-expression
4246 literal:
4247 __null
4249 ADDRESS_P is true iff this expression was immediately preceded by
4250 "&" and therefore might denote a pointer-to-member. CAST_P is true
4251 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4252 true iff this expression is a template argument.
4254 Returns a representation of the expression. Upon return, *IDK
4255 indicates what kind of id-expression (if any) was present. */
4257 static tree
4258 cp_parser_primary_expression (cp_parser *parser,
4259 bool address_p,
4260 bool cast_p,
4261 bool template_arg_p,
4262 bool decltype_p,
4263 cp_id_kind *idk)
4265 cp_token *token = NULL;
4267 /* Assume the primary expression is not an id-expression. */
4268 *idk = CP_ID_KIND_NONE;
4270 /* Peek at the next token. */
4271 token = cp_lexer_peek_token (parser->lexer);
4272 switch ((int) token->type)
4274 /* literal:
4275 integer-literal
4276 character-literal
4277 floating-literal
4278 string-literal
4279 boolean-literal
4280 pointer-literal
4281 user-defined-literal */
4282 case CPP_CHAR:
4283 case CPP_CHAR16:
4284 case CPP_CHAR32:
4285 case CPP_WCHAR:
4286 case CPP_UTF8CHAR:
4287 case CPP_NUMBER:
4288 case CPP_PREPARSED_EXPR:
4289 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4290 return cp_parser_userdef_numeric_literal (parser);
4291 token = cp_lexer_consume_token (parser->lexer);
4292 if (TREE_CODE (token->u.value) == FIXED_CST)
4294 error_at (token->location,
4295 "fixed-point types not supported in C++");
4296 return error_mark_node;
4298 /* Floating-point literals are only allowed in an integral
4299 constant expression if they are cast to an integral or
4300 enumeration type. */
4301 if (TREE_CODE (token->u.value) == REAL_CST
4302 && parser->integral_constant_expression_p
4303 && pedantic)
4305 /* CAST_P will be set even in invalid code like "int(2.7 +
4306 ...)". Therefore, we have to check that the next token
4307 is sure to end the cast. */
4308 if (cast_p)
4310 cp_token *next_token;
4312 next_token = cp_lexer_peek_token (parser->lexer);
4313 if (/* The comma at the end of an
4314 enumerator-definition. */
4315 next_token->type != CPP_COMMA
4316 /* The curly brace at the end of an enum-specifier. */
4317 && next_token->type != CPP_CLOSE_BRACE
4318 /* The end of a statement. */
4319 && next_token->type != CPP_SEMICOLON
4320 /* The end of the cast-expression. */
4321 && next_token->type != CPP_CLOSE_PAREN
4322 /* The end of an array bound. */
4323 && next_token->type != CPP_CLOSE_SQUARE
4324 /* The closing ">" in a template-argument-list. */
4325 && (next_token->type != CPP_GREATER
4326 || parser->greater_than_is_operator_p)
4327 /* C++0x only: A ">>" treated like two ">" tokens,
4328 in a template-argument-list. */
4329 && (next_token->type != CPP_RSHIFT
4330 || (cxx_dialect == cxx98)
4331 || parser->greater_than_is_operator_p))
4332 cast_p = false;
4335 /* If we are within a cast, then the constraint that the
4336 cast is to an integral or enumeration type will be
4337 checked at that point. If we are not within a cast, then
4338 this code is invalid. */
4339 if (!cast_p)
4340 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4342 return token->u.value;
4344 case CPP_CHAR_USERDEF:
4345 case CPP_CHAR16_USERDEF:
4346 case CPP_CHAR32_USERDEF:
4347 case CPP_WCHAR_USERDEF:
4348 case CPP_UTF8CHAR_USERDEF:
4349 return cp_parser_userdef_char_literal (parser);
4351 case CPP_STRING:
4352 case CPP_STRING16:
4353 case CPP_STRING32:
4354 case CPP_WSTRING:
4355 case CPP_UTF8STRING:
4356 case CPP_STRING_USERDEF:
4357 case CPP_STRING16_USERDEF:
4358 case CPP_STRING32_USERDEF:
4359 case CPP_WSTRING_USERDEF:
4360 case CPP_UTF8STRING_USERDEF:
4361 /* ??? Should wide strings be allowed when parser->translate_strings_p
4362 is false (i.e. in attributes)? If not, we can kill the third
4363 argument to cp_parser_string_literal. */
4364 return cp_parser_string_literal (parser,
4365 parser->translate_strings_p,
4366 true);
4368 case CPP_OPEN_PAREN:
4369 /* If we see `( { ' then we are looking at the beginning of
4370 a GNU statement-expression. */
4371 if (cp_parser_allow_gnu_extensions_p (parser)
4372 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4374 /* Statement-expressions are not allowed by the standard. */
4375 pedwarn (token->location, OPT_Wpedantic,
4376 "ISO C++ forbids braced-groups within expressions");
4378 /* And they're not allowed outside of a function-body; you
4379 cannot, for example, write:
4381 int i = ({ int j = 3; j + 1; });
4383 at class or namespace scope. */
4384 if (!parser->in_function_body
4385 || parser->in_template_argument_list_p)
4387 error_at (token->location,
4388 "statement-expressions are not allowed outside "
4389 "functions nor in template-argument lists");
4390 cp_parser_skip_to_end_of_block_or_statement (parser);
4391 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4392 cp_lexer_consume_token (parser->lexer);
4393 return error_mark_node;
4395 else
4396 return cp_parser_statement_expr (parser);
4398 /* Otherwise it's a normal parenthesized expression. */
4400 tree expr;
4401 bool saved_greater_than_is_operator_p;
4403 /* Consume the `('. */
4404 cp_lexer_consume_token (parser->lexer);
4405 /* Within a parenthesized expression, a `>' token is always
4406 the greater-than operator. */
4407 saved_greater_than_is_operator_p
4408 = parser->greater_than_is_operator_p;
4409 parser->greater_than_is_operator_p = true;
4411 /* Parse the parenthesized expression. */
4412 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4413 /* Let the front end know that this expression was
4414 enclosed in parentheses. This matters in case, for
4415 example, the expression is of the form `A::B', since
4416 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4417 not. */
4418 expr = finish_parenthesized_expr (expr);
4419 /* DR 705: Wrapping an unqualified name in parentheses
4420 suppresses arg-dependent lookup. We want to pass back
4421 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4422 (c++/37862), but none of the others. */
4423 if (*idk != CP_ID_KIND_QUALIFIED)
4424 *idk = CP_ID_KIND_NONE;
4426 /* The `>' token might be the end of a template-id or
4427 template-parameter-list now. */
4428 parser->greater_than_is_operator_p
4429 = saved_greater_than_is_operator_p;
4430 /* Consume the `)'. */
4431 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4432 cp_parser_skip_to_end_of_statement (parser);
4434 return expr;
4437 case CPP_OPEN_SQUARE:
4439 if (c_dialect_objc ())
4441 /* We might have an Objective-C++ message. */
4442 cp_parser_parse_tentatively (parser);
4443 tree msg = cp_parser_objc_message_expression (parser);
4444 /* If that works out, we're done ... */
4445 if (cp_parser_parse_definitely (parser))
4446 return msg;
4447 /* ... else, fall though to see if it's a lambda. */
4449 tree lam = cp_parser_lambda_expression (parser);
4450 /* Don't warn about a failed tentative parse. */
4451 if (cp_parser_error_occurred (parser))
4452 return error_mark_node;
4453 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4454 return lam;
4457 case CPP_OBJC_STRING:
4458 if (c_dialect_objc ())
4459 /* We have an Objective-C++ string literal. */
4460 return cp_parser_objc_expression (parser);
4461 cp_parser_error (parser, "expected primary-expression");
4462 return error_mark_node;
4464 case CPP_KEYWORD:
4465 switch (token->keyword)
4467 /* These two are the boolean literals. */
4468 case RID_TRUE:
4469 cp_lexer_consume_token (parser->lexer);
4470 return boolean_true_node;
4471 case RID_FALSE:
4472 cp_lexer_consume_token (parser->lexer);
4473 return boolean_false_node;
4475 /* The `__null' literal. */
4476 case RID_NULL:
4477 cp_lexer_consume_token (parser->lexer);
4478 return null_node;
4480 /* The `nullptr' literal. */
4481 case RID_NULLPTR:
4482 cp_lexer_consume_token (parser->lexer);
4483 return nullptr_node;
4485 /* Recognize the `this' keyword. */
4486 case RID_THIS:
4487 cp_lexer_consume_token (parser->lexer);
4488 if (parser->local_variables_forbidden_p)
4490 error_at (token->location,
4491 "%<this%> may not be used in this context");
4492 return error_mark_node;
4494 /* Pointers cannot appear in constant-expressions. */
4495 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4496 return error_mark_node;
4497 return finish_this_expr ();
4499 /* The `operator' keyword can be the beginning of an
4500 id-expression. */
4501 case RID_OPERATOR:
4502 goto id_expression;
4504 case RID_FUNCTION_NAME:
4505 case RID_PRETTY_FUNCTION_NAME:
4506 case RID_C99_FUNCTION_NAME:
4508 non_integral_constant name;
4510 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4511 __func__ are the names of variables -- but they are
4512 treated specially. Therefore, they are handled here,
4513 rather than relying on the generic id-expression logic
4514 below. Grammatically, these names are id-expressions.
4516 Consume the token. */
4517 token = cp_lexer_consume_token (parser->lexer);
4519 switch (token->keyword)
4521 case RID_FUNCTION_NAME:
4522 name = NIC_FUNC_NAME;
4523 break;
4524 case RID_PRETTY_FUNCTION_NAME:
4525 name = NIC_PRETTY_FUNC;
4526 break;
4527 case RID_C99_FUNCTION_NAME:
4528 name = NIC_C99_FUNC;
4529 break;
4530 default:
4531 gcc_unreachable ();
4534 if (cp_parser_non_integral_constant_expression (parser, name))
4535 return error_mark_node;
4537 /* Look up the name. */
4538 return finish_fname (token->u.value);
4541 case RID_VA_ARG:
4543 tree expression;
4544 tree type;
4545 source_location type_location;
4547 /* The `__builtin_va_arg' construct is used to handle
4548 `va_arg'. Consume the `__builtin_va_arg' token. */
4549 cp_lexer_consume_token (parser->lexer);
4550 /* Look for the opening `('. */
4551 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4552 /* Now, parse the assignment-expression. */
4553 expression = cp_parser_assignment_expression (parser);
4554 /* Look for the `,'. */
4555 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4556 type_location = cp_lexer_peek_token (parser->lexer)->location;
4557 /* Parse the type-id. */
4558 type = cp_parser_type_id (parser);
4559 /* Look for the closing `)'. */
4560 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4561 /* Using `va_arg' in a constant-expression is not
4562 allowed. */
4563 if (cp_parser_non_integral_constant_expression (parser,
4564 NIC_VA_ARG))
4565 return error_mark_node;
4566 return build_x_va_arg (type_location, expression, type);
4569 case RID_OFFSETOF:
4570 return cp_parser_builtin_offsetof (parser);
4572 case RID_HAS_NOTHROW_ASSIGN:
4573 case RID_HAS_NOTHROW_CONSTRUCTOR:
4574 case RID_HAS_NOTHROW_COPY:
4575 case RID_HAS_TRIVIAL_ASSIGN:
4576 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4577 case RID_HAS_TRIVIAL_COPY:
4578 case RID_HAS_TRIVIAL_DESTRUCTOR:
4579 case RID_HAS_VIRTUAL_DESTRUCTOR:
4580 case RID_IS_ABSTRACT:
4581 case RID_IS_BASE_OF:
4582 case RID_IS_CLASS:
4583 case RID_IS_EMPTY:
4584 case RID_IS_ENUM:
4585 case RID_IS_FINAL:
4586 case RID_IS_LITERAL_TYPE:
4587 case RID_IS_POD:
4588 case RID_IS_POLYMORPHIC:
4589 case RID_IS_STD_LAYOUT:
4590 case RID_IS_TRIVIAL:
4591 case RID_IS_TRIVIALLY_ASSIGNABLE:
4592 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4593 case RID_IS_TRIVIALLY_COPYABLE:
4594 case RID_IS_UNION:
4595 return cp_parser_trait_expr (parser, token->keyword);
4597 /* Objective-C++ expressions. */
4598 case RID_AT_ENCODE:
4599 case RID_AT_PROTOCOL:
4600 case RID_AT_SELECTOR:
4601 return cp_parser_objc_expression (parser);
4603 case RID_TEMPLATE:
4604 if (parser->in_function_body
4605 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4606 == CPP_LESS))
4608 error_at (token->location,
4609 "a template declaration cannot appear at block scope");
4610 cp_parser_skip_to_end_of_block_or_statement (parser);
4611 return error_mark_node;
4613 default:
4614 cp_parser_error (parser, "expected primary-expression");
4615 return error_mark_node;
4618 /* An id-expression can start with either an identifier, a
4619 `::' as the beginning of a qualified-id, or the "operator"
4620 keyword. */
4621 case CPP_NAME:
4622 case CPP_SCOPE:
4623 case CPP_TEMPLATE_ID:
4624 case CPP_NESTED_NAME_SPECIFIER:
4626 tree id_expression;
4627 tree decl;
4628 const char *error_msg;
4629 bool template_p;
4630 bool done;
4631 cp_token *id_expr_token;
4633 id_expression:
4634 /* Parse the id-expression. */
4635 id_expression
4636 = cp_parser_id_expression (parser,
4637 /*template_keyword_p=*/false,
4638 /*check_dependency_p=*/true,
4639 &template_p,
4640 /*declarator_p=*/false,
4641 /*optional_p=*/false);
4642 if (id_expression == error_mark_node)
4643 return error_mark_node;
4644 id_expr_token = token;
4645 token = cp_lexer_peek_token (parser->lexer);
4646 done = (token->type != CPP_OPEN_SQUARE
4647 && token->type != CPP_OPEN_PAREN
4648 && token->type != CPP_DOT
4649 && token->type != CPP_DEREF
4650 && token->type != CPP_PLUS_PLUS
4651 && token->type != CPP_MINUS_MINUS);
4652 /* If we have a template-id, then no further lookup is
4653 required. If the template-id was for a template-class, we
4654 will sometimes have a TYPE_DECL at this point. */
4655 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4656 || TREE_CODE (id_expression) == TYPE_DECL)
4657 decl = id_expression;
4658 /* Look up the name. */
4659 else
4661 tree ambiguous_decls;
4663 /* If we already know that this lookup is ambiguous, then
4664 we've already issued an error message; there's no reason
4665 to check again. */
4666 if (id_expr_token->type == CPP_NAME
4667 && id_expr_token->error_reported)
4669 cp_parser_simulate_error (parser);
4670 return error_mark_node;
4673 decl = cp_parser_lookup_name (parser, id_expression,
4674 none_type,
4675 template_p,
4676 /*is_namespace=*/false,
4677 /*check_dependency=*/true,
4678 &ambiguous_decls,
4679 id_expr_token->location);
4680 /* If the lookup was ambiguous, an error will already have
4681 been issued. */
4682 if (ambiguous_decls)
4683 return error_mark_node;
4685 /* In Objective-C++, we may have an Objective-C 2.0
4686 dot-syntax for classes here. */
4687 if (c_dialect_objc ()
4688 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4689 && TREE_CODE (decl) == TYPE_DECL
4690 && objc_is_class_name (decl))
4692 tree component;
4693 cp_lexer_consume_token (parser->lexer);
4694 component = cp_parser_identifier (parser);
4695 if (component == error_mark_node)
4696 return error_mark_node;
4698 return objc_build_class_component_ref (id_expression, component);
4701 /* In Objective-C++, an instance variable (ivar) may be preferred
4702 to whatever cp_parser_lookup_name() found. */
4703 decl = objc_lookup_ivar (decl, id_expression);
4705 /* If name lookup gives us a SCOPE_REF, then the
4706 qualifying scope was dependent. */
4707 if (TREE_CODE (decl) == SCOPE_REF)
4709 /* At this point, we do not know if DECL is a valid
4710 integral constant expression. We assume that it is
4711 in fact such an expression, so that code like:
4713 template <int N> struct A {
4714 int a[B<N>::i];
4717 is accepted. At template-instantiation time, we
4718 will check that B<N>::i is actually a constant. */
4719 return decl;
4721 /* Check to see if DECL is a local variable in a context
4722 where that is forbidden. */
4723 if (parser->local_variables_forbidden_p
4724 && local_variable_p (decl))
4726 /* It might be that we only found DECL because we are
4727 trying to be generous with pre-ISO scoping rules.
4728 For example, consider:
4730 int i;
4731 void g() {
4732 for (int i = 0; i < 10; ++i) {}
4733 extern void f(int j = i);
4736 Here, name look up will originally find the out
4737 of scope `i'. We need to issue a warning message,
4738 but then use the global `i'. */
4739 decl = check_for_out_of_scope_variable (decl);
4740 if (local_variable_p (decl))
4742 error_at (id_expr_token->location,
4743 "local variable %qD may not appear in this context",
4744 decl);
4745 return error_mark_node;
4750 decl = (finish_id_expression
4751 (id_expression, decl, parser->scope,
4752 idk,
4753 parser->integral_constant_expression_p,
4754 parser->allow_non_integral_constant_expression_p,
4755 &parser->non_integral_constant_expression_p,
4756 template_p, done, address_p,
4757 template_arg_p,
4758 &error_msg,
4759 id_expr_token->location));
4760 if (error_msg)
4761 cp_parser_error (parser, error_msg);
4762 return decl;
4765 /* Anything else is an error. */
4766 default:
4767 cp_parser_error (parser, "expected primary-expression");
4768 return error_mark_node;
4772 static inline tree
4773 cp_parser_primary_expression (cp_parser *parser,
4774 bool address_p,
4775 bool cast_p,
4776 bool template_arg_p,
4777 cp_id_kind *idk)
4779 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4780 /*decltype*/false, idk);
4783 /* Parse an id-expression.
4785 id-expression:
4786 unqualified-id
4787 qualified-id
4789 qualified-id:
4790 :: [opt] nested-name-specifier template [opt] unqualified-id
4791 :: identifier
4792 :: operator-function-id
4793 :: template-id
4795 Return a representation of the unqualified portion of the
4796 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4797 a `::' or nested-name-specifier.
4799 Often, if the id-expression was a qualified-id, the caller will
4800 want to make a SCOPE_REF to represent the qualified-id. This
4801 function does not do this in order to avoid wastefully creating
4802 SCOPE_REFs when they are not required.
4804 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4805 `template' keyword.
4807 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4808 uninstantiated templates.
4810 If *TEMPLATE_P is non-NULL, it is set to true iff the
4811 `template' keyword is used to explicitly indicate that the entity
4812 named is a template.
4814 If DECLARATOR_P is true, the id-expression is appearing as part of
4815 a declarator, rather than as part of an expression. */
4817 static tree
4818 cp_parser_id_expression (cp_parser *parser,
4819 bool template_keyword_p,
4820 bool check_dependency_p,
4821 bool *template_p,
4822 bool declarator_p,
4823 bool optional_p)
4825 bool global_scope_p;
4826 bool nested_name_specifier_p;
4828 /* Assume the `template' keyword was not used. */
4829 if (template_p)
4830 *template_p = template_keyword_p;
4832 /* Look for the optional `::' operator. */
4833 global_scope_p
4834 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4835 != NULL_TREE);
4836 /* Look for the optional nested-name-specifier. */
4837 nested_name_specifier_p
4838 = (cp_parser_nested_name_specifier_opt (parser,
4839 /*typename_keyword_p=*/false,
4840 check_dependency_p,
4841 /*type_p=*/false,
4842 declarator_p)
4843 != NULL_TREE);
4844 /* If there is a nested-name-specifier, then we are looking at
4845 the first qualified-id production. */
4846 if (nested_name_specifier_p)
4848 tree saved_scope;
4849 tree saved_object_scope;
4850 tree saved_qualifying_scope;
4851 tree unqualified_id;
4852 bool is_template;
4854 /* See if the next token is the `template' keyword. */
4855 if (!template_p)
4856 template_p = &is_template;
4857 *template_p = cp_parser_optional_template_keyword (parser);
4858 /* Name lookup we do during the processing of the
4859 unqualified-id might obliterate SCOPE. */
4860 saved_scope = parser->scope;
4861 saved_object_scope = parser->object_scope;
4862 saved_qualifying_scope = parser->qualifying_scope;
4863 /* Process the final unqualified-id. */
4864 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4865 check_dependency_p,
4866 declarator_p,
4867 /*optional_p=*/false);
4868 /* Restore the SAVED_SCOPE for our caller. */
4869 parser->scope = saved_scope;
4870 parser->object_scope = saved_object_scope;
4871 parser->qualifying_scope = saved_qualifying_scope;
4873 return unqualified_id;
4875 /* Otherwise, if we are in global scope, then we are looking at one
4876 of the other qualified-id productions. */
4877 else if (global_scope_p)
4879 cp_token *token;
4880 tree id;
4882 /* Peek at the next token. */
4883 token = cp_lexer_peek_token (parser->lexer);
4885 /* If it's an identifier, and the next token is not a "<", then
4886 we can avoid the template-id case. This is an optimization
4887 for this common case. */
4888 if (token->type == CPP_NAME
4889 && !cp_parser_nth_token_starts_template_argument_list_p
4890 (parser, 2))
4891 return cp_parser_identifier (parser);
4893 cp_parser_parse_tentatively (parser);
4894 /* Try a template-id. */
4895 id = cp_parser_template_id (parser,
4896 /*template_keyword_p=*/false,
4897 /*check_dependency_p=*/true,
4898 none_type,
4899 declarator_p);
4900 /* If that worked, we're done. */
4901 if (cp_parser_parse_definitely (parser))
4902 return id;
4904 /* Peek at the next token. (Changes in the token buffer may
4905 have invalidated the pointer obtained above.) */
4906 token = cp_lexer_peek_token (parser->lexer);
4908 switch (token->type)
4910 case CPP_NAME:
4911 return cp_parser_identifier (parser);
4913 case CPP_KEYWORD:
4914 if (token->keyword == RID_OPERATOR)
4915 return cp_parser_operator_function_id (parser);
4916 /* Fall through. */
4918 default:
4919 cp_parser_error (parser, "expected id-expression");
4920 return error_mark_node;
4923 else
4924 return cp_parser_unqualified_id (parser, template_keyword_p,
4925 /*check_dependency_p=*/true,
4926 declarator_p,
4927 optional_p);
4930 /* Parse an unqualified-id.
4932 unqualified-id:
4933 identifier
4934 operator-function-id
4935 conversion-function-id
4936 ~ class-name
4937 template-id
4939 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4940 keyword, in a construct like `A::template ...'.
4942 Returns a representation of unqualified-id. For the `identifier'
4943 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4944 production a BIT_NOT_EXPR is returned; the operand of the
4945 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4946 other productions, see the documentation accompanying the
4947 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4948 names are looked up in uninstantiated templates. If DECLARATOR_P
4949 is true, the unqualified-id is appearing as part of a declarator,
4950 rather than as part of an expression. */
4952 static tree
4953 cp_parser_unqualified_id (cp_parser* parser,
4954 bool template_keyword_p,
4955 bool check_dependency_p,
4956 bool declarator_p,
4957 bool optional_p)
4959 cp_token *token;
4961 /* Peek at the next token. */
4962 token = cp_lexer_peek_token (parser->lexer);
4964 switch ((int) token->type)
4966 case CPP_NAME:
4968 tree id;
4970 /* We don't know yet whether or not this will be a
4971 template-id. */
4972 cp_parser_parse_tentatively (parser);
4973 /* Try a template-id. */
4974 id = cp_parser_template_id (parser, template_keyword_p,
4975 check_dependency_p,
4976 none_type,
4977 declarator_p);
4978 /* If it worked, we're done. */
4979 if (cp_parser_parse_definitely (parser))
4980 return id;
4981 /* Otherwise, it's an ordinary identifier. */
4982 return cp_parser_identifier (parser);
4985 case CPP_TEMPLATE_ID:
4986 return cp_parser_template_id (parser, template_keyword_p,
4987 check_dependency_p,
4988 none_type,
4989 declarator_p);
4991 case CPP_COMPL:
4993 tree type_decl;
4994 tree qualifying_scope;
4995 tree object_scope;
4996 tree scope;
4997 bool done;
4999 /* Consume the `~' token. */
5000 cp_lexer_consume_token (parser->lexer);
5001 /* Parse the class-name. The standard, as written, seems to
5002 say that:
5004 template <typename T> struct S { ~S (); };
5005 template <typename T> S<T>::~S() {}
5007 is invalid, since `~' must be followed by a class-name, but
5008 `S<T>' is dependent, and so not known to be a class.
5009 That's not right; we need to look in uninstantiated
5010 templates. A further complication arises from:
5012 template <typename T> void f(T t) {
5013 t.T::~T();
5016 Here, it is not possible to look up `T' in the scope of `T'
5017 itself. We must look in both the current scope, and the
5018 scope of the containing complete expression.
5020 Yet another issue is:
5022 struct S {
5023 int S;
5024 ~S();
5027 S::~S() {}
5029 The standard does not seem to say that the `S' in `~S'
5030 should refer to the type `S' and not the data member
5031 `S::S'. */
5033 /* DR 244 says that we look up the name after the "~" in the
5034 same scope as we looked up the qualifying name. That idea
5035 isn't fully worked out; it's more complicated than that. */
5036 scope = parser->scope;
5037 object_scope = parser->object_scope;
5038 qualifying_scope = parser->qualifying_scope;
5040 /* Check for invalid scopes. */
5041 if (scope == error_mark_node)
5043 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5044 cp_lexer_consume_token (parser->lexer);
5045 return error_mark_node;
5047 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5049 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5050 error_at (token->location,
5051 "scope %qT before %<~%> is not a class-name",
5052 scope);
5053 cp_parser_simulate_error (parser);
5054 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5055 cp_lexer_consume_token (parser->lexer);
5056 return error_mark_node;
5058 gcc_assert (!scope || TYPE_P (scope));
5060 /* If the name is of the form "X::~X" it's OK even if X is a
5061 typedef. */
5062 token = cp_lexer_peek_token (parser->lexer);
5063 if (scope
5064 && token->type == CPP_NAME
5065 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5066 != CPP_LESS)
5067 && (token->u.value == TYPE_IDENTIFIER (scope)
5068 || (CLASS_TYPE_P (scope)
5069 && constructor_name_p (token->u.value, scope))))
5071 cp_lexer_consume_token (parser->lexer);
5072 return build_nt (BIT_NOT_EXPR, scope);
5075 /* ~auto means the destructor of whatever the object is. */
5076 if (cp_parser_is_keyword (token, RID_AUTO))
5078 if (cxx_dialect < cxx14)
5079 pedwarn (input_location, 0,
5080 "%<~auto%> only available with "
5081 "-std=c++14 or -std=gnu++14");
5082 cp_lexer_consume_token (parser->lexer);
5083 return build_nt (BIT_NOT_EXPR, make_auto ());
5086 /* If there was an explicit qualification (S::~T), first look
5087 in the scope given by the qualification (i.e., S).
5089 Note: in the calls to cp_parser_class_name below we pass
5090 typename_type so that lookup finds the injected-class-name
5091 rather than the constructor. */
5092 done = false;
5093 type_decl = NULL_TREE;
5094 if (scope)
5096 cp_parser_parse_tentatively (parser);
5097 type_decl = cp_parser_class_name (parser,
5098 /*typename_keyword_p=*/false,
5099 /*template_keyword_p=*/false,
5100 typename_type,
5101 /*check_dependency=*/false,
5102 /*class_head_p=*/false,
5103 declarator_p);
5104 if (cp_parser_parse_definitely (parser))
5105 done = true;
5107 /* In "N::S::~S", look in "N" as well. */
5108 if (!done && scope && qualifying_scope)
5110 cp_parser_parse_tentatively (parser);
5111 parser->scope = qualifying_scope;
5112 parser->object_scope = NULL_TREE;
5113 parser->qualifying_scope = NULL_TREE;
5114 type_decl
5115 = cp_parser_class_name (parser,
5116 /*typename_keyword_p=*/false,
5117 /*template_keyword_p=*/false,
5118 typename_type,
5119 /*check_dependency=*/false,
5120 /*class_head_p=*/false,
5121 declarator_p);
5122 if (cp_parser_parse_definitely (parser))
5123 done = true;
5125 /* In "p->S::~T", look in the scope given by "*p" as well. */
5126 else if (!done && object_scope)
5128 cp_parser_parse_tentatively (parser);
5129 parser->scope = object_scope;
5130 parser->object_scope = NULL_TREE;
5131 parser->qualifying_scope = NULL_TREE;
5132 type_decl
5133 = cp_parser_class_name (parser,
5134 /*typename_keyword_p=*/false,
5135 /*template_keyword_p=*/false,
5136 typename_type,
5137 /*check_dependency=*/false,
5138 /*class_head_p=*/false,
5139 declarator_p);
5140 if (cp_parser_parse_definitely (parser))
5141 done = true;
5143 /* Look in the surrounding context. */
5144 if (!done)
5146 parser->scope = NULL_TREE;
5147 parser->object_scope = NULL_TREE;
5148 parser->qualifying_scope = NULL_TREE;
5149 if (processing_template_decl)
5150 cp_parser_parse_tentatively (parser);
5151 type_decl
5152 = cp_parser_class_name (parser,
5153 /*typename_keyword_p=*/false,
5154 /*template_keyword_p=*/false,
5155 typename_type,
5156 /*check_dependency=*/false,
5157 /*class_head_p=*/false,
5158 declarator_p);
5159 if (processing_template_decl
5160 && ! cp_parser_parse_definitely (parser))
5162 /* We couldn't find a type with this name, so just accept
5163 it and check for a match at instantiation time. */
5164 type_decl = cp_parser_identifier (parser);
5165 if (type_decl != error_mark_node)
5166 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5167 return type_decl;
5170 /* If an error occurred, assume that the name of the
5171 destructor is the same as the name of the qualifying
5172 class. That allows us to keep parsing after running
5173 into ill-formed destructor names. */
5174 if (type_decl == error_mark_node && scope)
5175 return build_nt (BIT_NOT_EXPR, scope);
5176 else if (type_decl == error_mark_node)
5177 return error_mark_node;
5179 /* Check that destructor name and scope match. */
5180 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5182 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5183 error_at (token->location,
5184 "declaration of %<~%T%> as member of %qT",
5185 type_decl, scope);
5186 cp_parser_simulate_error (parser);
5187 return error_mark_node;
5190 /* [class.dtor]
5192 A typedef-name that names a class shall not be used as the
5193 identifier in the declarator for a destructor declaration. */
5194 if (declarator_p
5195 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5196 && !DECL_SELF_REFERENCE_P (type_decl)
5197 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5198 error_at (token->location,
5199 "typedef-name %qD used as destructor declarator",
5200 type_decl);
5202 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5205 case CPP_KEYWORD:
5206 if (token->keyword == RID_OPERATOR)
5208 tree id;
5210 /* This could be a template-id, so we try that first. */
5211 cp_parser_parse_tentatively (parser);
5212 /* Try a template-id. */
5213 id = cp_parser_template_id (parser, template_keyword_p,
5214 /*check_dependency_p=*/true,
5215 none_type,
5216 declarator_p);
5217 /* If that worked, we're done. */
5218 if (cp_parser_parse_definitely (parser))
5219 return id;
5220 /* We still don't know whether we're looking at an
5221 operator-function-id or a conversion-function-id. */
5222 cp_parser_parse_tentatively (parser);
5223 /* Try an operator-function-id. */
5224 id = cp_parser_operator_function_id (parser);
5225 /* If that didn't work, try a conversion-function-id. */
5226 if (!cp_parser_parse_definitely (parser))
5227 id = cp_parser_conversion_function_id (parser);
5228 else if (UDLIT_OPER_P (id))
5230 /* 17.6.3.3.5 */
5231 const char *name = UDLIT_OP_SUFFIX (id);
5232 if (name[0] != '_' && !in_system_header_at (input_location)
5233 && declarator_p)
5234 warning (0, "literal operator suffixes not preceded by %<_%>"
5235 " are reserved for future standardization");
5238 return id;
5240 /* Fall through. */
5242 default:
5243 if (optional_p)
5244 return NULL_TREE;
5245 cp_parser_error (parser, "expected unqualified-id");
5246 return error_mark_node;
5250 /* Parse an (optional) nested-name-specifier.
5252 nested-name-specifier: [C++98]
5253 class-or-namespace-name :: nested-name-specifier [opt]
5254 class-or-namespace-name :: template nested-name-specifier [opt]
5256 nested-name-specifier: [C++0x]
5257 type-name ::
5258 namespace-name ::
5259 nested-name-specifier identifier ::
5260 nested-name-specifier template [opt] simple-template-id ::
5262 PARSER->SCOPE should be set appropriately before this function is
5263 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5264 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5265 in name lookups.
5267 Sets PARSER->SCOPE to the class (TYPE) or namespace
5268 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5269 it unchanged if there is no nested-name-specifier. Returns the new
5270 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5272 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5273 part of a declaration and/or decl-specifier. */
5275 static tree
5276 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5277 bool typename_keyword_p,
5278 bool check_dependency_p,
5279 bool type_p,
5280 bool is_declaration)
5282 bool success = false;
5283 cp_token_position start = 0;
5284 cp_token *token;
5286 /* Remember where the nested-name-specifier starts. */
5287 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5289 start = cp_lexer_token_position (parser->lexer, false);
5290 push_deferring_access_checks (dk_deferred);
5293 while (true)
5295 tree new_scope;
5296 tree old_scope;
5297 tree saved_qualifying_scope;
5298 bool template_keyword_p;
5300 /* Spot cases that cannot be the beginning of a
5301 nested-name-specifier. */
5302 token = cp_lexer_peek_token (parser->lexer);
5304 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5305 the already parsed nested-name-specifier. */
5306 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5308 /* Grab the nested-name-specifier and continue the loop. */
5309 cp_parser_pre_parsed_nested_name_specifier (parser);
5310 /* If we originally encountered this nested-name-specifier
5311 with IS_DECLARATION set to false, we will not have
5312 resolved TYPENAME_TYPEs, so we must do so here. */
5313 if (is_declaration
5314 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5316 new_scope = resolve_typename_type (parser->scope,
5317 /*only_current_p=*/false);
5318 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5319 parser->scope = new_scope;
5321 success = true;
5322 continue;
5325 /* Spot cases that cannot be the beginning of a
5326 nested-name-specifier. On the second and subsequent times
5327 through the loop, we look for the `template' keyword. */
5328 if (success && token->keyword == RID_TEMPLATE)
5330 /* A template-id can start a nested-name-specifier. */
5331 else if (token->type == CPP_TEMPLATE_ID)
5333 /* DR 743: decltype can be used in a nested-name-specifier. */
5334 else if (token_is_decltype (token))
5336 else
5338 /* If the next token is not an identifier, then it is
5339 definitely not a type-name or namespace-name. */
5340 if (token->type != CPP_NAME)
5341 break;
5342 /* If the following token is neither a `<' (to begin a
5343 template-id), nor a `::', then we are not looking at a
5344 nested-name-specifier. */
5345 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5347 if (token->type == CPP_COLON
5348 && parser->colon_corrects_to_scope_p
5349 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5351 error_at (token->location,
5352 "found %<:%> in nested-name-specifier, expected %<::%>");
5353 token->type = CPP_SCOPE;
5356 if (token->type != CPP_SCOPE
5357 && !cp_parser_nth_token_starts_template_argument_list_p
5358 (parser, 2))
5359 break;
5362 /* The nested-name-specifier is optional, so we parse
5363 tentatively. */
5364 cp_parser_parse_tentatively (parser);
5366 /* Look for the optional `template' keyword, if this isn't the
5367 first time through the loop. */
5368 if (success)
5369 template_keyword_p = cp_parser_optional_template_keyword (parser);
5370 else
5371 template_keyword_p = false;
5373 /* Save the old scope since the name lookup we are about to do
5374 might destroy it. */
5375 old_scope = parser->scope;
5376 saved_qualifying_scope = parser->qualifying_scope;
5377 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5378 look up names in "X<T>::I" in order to determine that "Y" is
5379 a template. So, if we have a typename at this point, we make
5380 an effort to look through it. */
5381 if (is_declaration
5382 && !typename_keyword_p
5383 && parser->scope
5384 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5385 parser->scope = resolve_typename_type (parser->scope,
5386 /*only_current_p=*/false);
5387 /* Parse the qualifying entity. */
5388 new_scope
5389 = cp_parser_qualifying_entity (parser,
5390 typename_keyword_p,
5391 template_keyword_p,
5392 check_dependency_p,
5393 type_p,
5394 is_declaration);
5395 /* Look for the `::' token. */
5396 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5398 /* If we found what we wanted, we keep going; otherwise, we're
5399 done. */
5400 if (!cp_parser_parse_definitely (parser))
5402 bool error_p = false;
5404 /* Restore the OLD_SCOPE since it was valid before the
5405 failed attempt at finding the last
5406 class-or-namespace-name. */
5407 parser->scope = old_scope;
5408 parser->qualifying_scope = saved_qualifying_scope;
5410 /* If the next token is a decltype, and the one after that is a
5411 `::', then the decltype has failed to resolve to a class or
5412 enumeration type. Give this error even when parsing
5413 tentatively since it can't possibly be valid--and we're going
5414 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5415 won't get another chance.*/
5416 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5417 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5418 == CPP_SCOPE))
5420 token = cp_lexer_consume_token (parser->lexer);
5421 error_at (token->location, "decltype evaluates to %qT, "
5422 "which is not a class or enumeration type",
5423 token->u.value);
5424 parser->scope = error_mark_node;
5425 error_p = true;
5426 /* As below. */
5427 success = true;
5428 cp_lexer_consume_token (parser->lexer);
5431 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5432 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5434 /* If we have a non-type template-id followed by ::, it can't
5435 possibly be valid. */
5436 token = cp_lexer_peek_token (parser->lexer);
5437 tree tid = token->u.tree_check_value->value;
5438 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5439 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5441 tree tmpl = NULL_TREE;
5442 if (is_overloaded_fn (tid))
5444 tree fns = get_fns (tid);
5445 if (!OVL_CHAIN (fns))
5446 tmpl = OVL_CURRENT (fns);
5447 error_at (token->location, "function template-id %qD "
5448 "in nested-name-specifier", tid);
5450 else
5452 /* Variable template. */
5453 tmpl = TREE_OPERAND (tid, 0);
5454 gcc_assert (variable_template_p (tmpl));
5455 error_at (token->location, "variable template-id %qD "
5456 "in nested-name-specifier", tid);
5458 if (tmpl)
5459 inform (DECL_SOURCE_LOCATION (tmpl),
5460 "%qD declared here", tmpl);
5462 parser->scope = error_mark_node;
5463 error_p = true;
5464 /* As below. */
5465 success = true;
5466 cp_lexer_consume_token (parser->lexer);
5467 cp_lexer_consume_token (parser->lexer);
5471 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5472 break;
5473 /* If the next token is an identifier, and the one after
5474 that is a `::', then any valid interpretation would have
5475 found a class-or-namespace-name. */
5476 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5477 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5478 == CPP_SCOPE)
5479 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5480 != CPP_COMPL))
5482 token = cp_lexer_consume_token (parser->lexer);
5483 if (!error_p)
5485 if (!token->error_reported)
5487 tree decl;
5488 tree ambiguous_decls;
5490 decl = cp_parser_lookup_name (parser, token->u.value,
5491 none_type,
5492 /*is_template=*/false,
5493 /*is_namespace=*/false,
5494 /*check_dependency=*/true,
5495 &ambiguous_decls,
5496 token->location);
5497 if (TREE_CODE (decl) == TEMPLATE_DECL)
5498 error_at (token->location,
5499 "%qD used without template parameters",
5500 decl);
5501 else if (ambiguous_decls)
5503 // cp_parser_lookup_name has the same diagnostic,
5504 // thus make sure to emit it at most once.
5505 if (cp_parser_uncommitted_to_tentative_parse_p
5506 (parser))
5508 error_at (token->location,
5509 "reference to %qD is ambiguous",
5510 token->u.value);
5511 print_candidates (ambiguous_decls);
5513 decl = error_mark_node;
5515 else
5517 if (cxx_dialect != cxx98)
5518 cp_parser_name_lookup_error
5519 (parser, token->u.value, decl, NLE_NOT_CXX98,
5520 token->location);
5521 else
5522 cp_parser_name_lookup_error
5523 (parser, token->u.value, decl, NLE_CXX98,
5524 token->location);
5527 parser->scope = error_mark_node;
5528 error_p = true;
5529 /* Treat this as a successful nested-name-specifier
5530 due to:
5532 [basic.lookup.qual]
5534 If the name found is not a class-name (clause
5535 _class_) or namespace-name (_namespace.def_), the
5536 program is ill-formed. */
5537 success = true;
5539 cp_lexer_consume_token (parser->lexer);
5541 break;
5543 /* We've found one valid nested-name-specifier. */
5544 success = true;
5545 /* Name lookup always gives us a DECL. */
5546 if (TREE_CODE (new_scope) == TYPE_DECL)
5547 new_scope = TREE_TYPE (new_scope);
5548 /* Uses of "template" must be followed by actual templates. */
5549 if (template_keyword_p
5550 && !(CLASS_TYPE_P (new_scope)
5551 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5552 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5553 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5554 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5555 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5556 == TEMPLATE_ID_EXPR)))
5557 permerror (input_location, TYPE_P (new_scope)
5558 ? G_("%qT is not a template")
5559 : G_("%qD is not a template"),
5560 new_scope);
5561 /* If it is a class scope, try to complete it; we are about to
5562 be looking up names inside the class. */
5563 if (TYPE_P (new_scope)
5564 /* Since checking types for dependency can be expensive,
5565 avoid doing it if the type is already complete. */
5566 && !COMPLETE_TYPE_P (new_scope)
5567 /* Do not try to complete dependent types. */
5568 && !dependent_type_p (new_scope))
5570 new_scope = complete_type (new_scope);
5571 /* If it is a typedef to current class, use the current
5572 class instead, as the typedef won't have any names inside
5573 it yet. */
5574 if (!COMPLETE_TYPE_P (new_scope)
5575 && currently_open_class (new_scope))
5576 new_scope = TYPE_MAIN_VARIANT (new_scope);
5578 /* Make sure we look in the right scope the next time through
5579 the loop. */
5580 parser->scope = new_scope;
5583 /* If parsing tentatively, replace the sequence of tokens that makes
5584 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5585 token. That way, should we re-parse the token stream, we will
5586 not have to repeat the effort required to do the parse, nor will
5587 we issue duplicate error messages. */
5588 if (success && start)
5590 cp_token *token;
5592 token = cp_lexer_token_at (parser->lexer, start);
5593 /* Reset the contents of the START token. */
5594 token->type = CPP_NESTED_NAME_SPECIFIER;
5595 /* Retrieve any deferred checks. Do not pop this access checks yet
5596 so the memory will not be reclaimed during token replacing below. */
5597 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5598 token->u.tree_check_value->value = parser->scope;
5599 token->u.tree_check_value->checks = get_deferred_access_checks ();
5600 token->u.tree_check_value->qualifying_scope =
5601 parser->qualifying_scope;
5602 token->keyword = RID_MAX;
5604 /* Purge all subsequent tokens. */
5605 cp_lexer_purge_tokens_after (parser->lexer, start);
5608 if (start)
5609 pop_to_parent_deferring_access_checks ();
5611 return success ? parser->scope : NULL_TREE;
5614 /* Parse a nested-name-specifier. See
5615 cp_parser_nested_name_specifier_opt for details. This function
5616 behaves identically, except that it will an issue an error if no
5617 nested-name-specifier is present. */
5619 static tree
5620 cp_parser_nested_name_specifier (cp_parser *parser,
5621 bool typename_keyword_p,
5622 bool check_dependency_p,
5623 bool type_p,
5624 bool is_declaration)
5626 tree scope;
5628 /* Look for the nested-name-specifier. */
5629 scope = cp_parser_nested_name_specifier_opt (parser,
5630 typename_keyword_p,
5631 check_dependency_p,
5632 type_p,
5633 is_declaration);
5634 /* If it was not present, issue an error message. */
5635 if (!scope)
5637 cp_parser_error (parser, "expected nested-name-specifier");
5638 parser->scope = NULL_TREE;
5641 return scope;
5644 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5645 this is either a class-name or a namespace-name (which corresponds
5646 to the class-or-namespace-name production in the grammar). For
5647 C++0x, it can also be a type-name that refers to an enumeration
5648 type or a simple-template-id.
5650 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5651 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5652 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5653 TYPE_P is TRUE iff the next name should be taken as a class-name,
5654 even the same name is declared to be another entity in the same
5655 scope.
5657 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5658 specified by the class-or-namespace-name. If neither is found the
5659 ERROR_MARK_NODE is returned. */
5661 static tree
5662 cp_parser_qualifying_entity (cp_parser *parser,
5663 bool typename_keyword_p,
5664 bool template_keyword_p,
5665 bool check_dependency_p,
5666 bool type_p,
5667 bool is_declaration)
5669 tree saved_scope;
5670 tree saved_qualifying_scope;
5671 tree saved_object_scope;
5672 tree scope;
5673 bool only_class_p;
5674 bool successful_parse_p;
5676 /* DR 743: decltype can appear in a nested-name-specifier. */
5677 if (cp_lexer_next_token_is_decltype (parser->lexer))
5679 scope = cp_parser_decltype (parser);
5680 if (TREE_CODE (scope) != ENUMERAL_TYPE
5681 && !MAYBE_CLASS_TYPE_P (scope))
5683 cp_parser_simulate_error (parser);
5684 return error_mark_node;
5686 if (TYPE_NAME (scope))
5687 scope = TYPE_NAME (scope);
5688 return scope;
5691 /* Before we try to parse the class-name, we must save away the
5692 current PARSER->SCOPE since cp_parser_class_name will destroy
5693 it. */
5694 saved_scope = parser->scope;
5695 saved_qualifying_scope = parser->qualifying_scope;
5696 saved_object_scope = parser->object_scope;
5697 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5698 there is no need to look for a namespace-name. */
5699 only_class_p = template_keyword_p
5700 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5701 if (!only_class_p)
5702 cp_parser_parse_tentatively (parser);
5703 scope = cp_parser_class_name (parser,
5704 typename_keyword_p,
5705 template_keyword_p,
5706 type_p ? class_type : none_type,
5707 check_dependency_p,
5708 /*class_head_p=*/false,
5709 is_declaration,
5710 /*enum_ok=*/cxx_dialect > cxx98);
5711 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5712 /* If that didn't work, try for a namespace-name. */
5713 if (!only_class_p && !successful_parse_p)
5715 /* Restore the saved scope. */
5716 parser->scope = saved_scope;
5717 parser->qualifying_scope = saved_qualifying_scope;
5718 parser->object_scope = saved_object_scope;
5719 /* If we are not looking at an identifier followed by the scope
5720 resolution operator, then this is not part of a
5721 nested-name-specifier. (Note that this function is only used
5722 to parse the components of a nested-name-specifier.) */
5723 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5724 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5725 return error_mark_node;
5726 scope = cp_parser_namespace_name (parser);
5729 return scope;
5732 /* Return true if we are looking at a compound-literal, false otherwise. */
5734 static bool
5735 cp_parser_compound_literal_p (cp_parser *parser)
5737 /* Consume the `('. */
5738 cp_lexer_consume_token (parser->lexer);
5740 cp_lexer_save_tokens (parser->lexer);
5742 /* Skip tokens until the next token is a closing parenthesis.
5743 If we find the closing `)', and the next token is a `{', then
5744 we are looking at a compound-literal. */
5745 bool compound_literal_p
5746 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5747 /*consume_paren=*/true)
5748 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5750 /* Roll back the tokens we skipped. */
5751 cp_lexer_rollback_tokens (parser->lexer);
5753 return compound_literal_p;
5756 /* Parse a postfix-expression.
5758 postfix-expression:
5759 primary-expression
5760 postfix-expression [ expression ]
5761 postfix-expression ( expression-list [opt] )
5762 simple-type-specifier ( expression-list [opt] )
5763 typename :: [opt] nested-name-specifier identifier
5764 ( expression-list [opt] )
5765 typename :: [opt] nested-name-specifier template [opt] template-id
5766 ( expression-list [opt] )
5767 postfix-expression . template [opt] id-expression
5768 postfix-expression -> template [opt] id-expression
5769 postfix-expression . pseudo-destructor-name
5770 postfix-expression -> pseudo-destructor-name
5771 postfix-expression ++
5772 postfix-expression --
5773 dynamic_cast < type-id > ( expression )
5774 static_cast < type-id > ( expression )
5775 reinterpret_cast < type-id > ( expression )
5776 const_cast < type-id > ( expression )
5777 typeid ( expression )
5778 typeid ( type-id )
5780 GNU Extension:
5782 postfix-expression:
5783 ( type-id ) { initializer-list , [opt] }
5785 This extension is a GNU version of the C99 compound-literal
5786 construct. (The C99 grammar uses `type-name' instead of `type-id',
5787 but they are essentially the same concept.)
5789 If ADDRESS_P is true, the postfix expression is the operand of the
5790 `&' operator. CAST_P is true if this expression is the target of a
5791 cast.
5793 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5794 class member access expressions [expr.ref].
5796 Returns a representation of the expression. */
5798 static tree
5799 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5800 bool member_access_only_p, bool decltype_p,
5801 cp_id_kind * pidk_return)
5803 cp_token *token;
5804 location_t loc;
5805 enum rid keyword;
5806 cp_id_kind idk = CP_ID_KIND_NONE;
5807 tree postfix_expression = NULL_TREE;
5808 bool is_member_access = false;
5809 int saved_in_statement = -1;
5811 /* Peek at the next token. */
5812 token = cp_lexer_peek_token (parser->lexer);
5813 loc = token->location;
5814 /* Some of the productions are determined by keywords. */
5815 keyword = token->keyword;
5816 switch (keyword)
5818 case RID_DYNCAST:
5819 case RID_STATCAST:
5820 case RID_REINTCAST:
5821 case RID_CONSTCAST:
5823 tree type;
5824 tree expression;
5825 const char *saved_message;
5826 bool saved_in_type_id_in_expr_p;
5828 /* All of these can be handled in the same way from the point
5829 of view of parsing. Begin by consuming the token
5830 identifying the cast. */
5831 cp_lexer_consume_token (parser->lexer);
5833 /* New types cannot be defined in the cast. */
5834 saved_message = parser->type_definition_forbidden_message;
5835 parser->type_definition_forbidden_message
5836 = G_("types may not be defined in casts");
5838 /* Look for the opening `<'. */
5839 cp_parser_require (parser, CPP_LESS, RT_LESS);
5840 /* Parse the type to which we are casting. */
5841 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5842 parser->in_type_id_in_expr_p = true;
5843 type = cp_parser_type_id (parser);
5844 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5845 /* Look for the closing `>'. */
5846 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5847 /* Restore the old message. */
5848 parser->type_definition_forbidden_message = saved_message;
5850 bool saved_greater_than_is_operator_p
5851 = parser->greater_than_is_operator_p;
5852 parser->greater_than_is_operator_p = true;
5854 /* And the expression which is being cast. */
5855 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5856 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5857 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5859 parser->greater_than_is_operator_p
5860 = saved_greater_than_is_operator_p;
5862 /* Only type conversions to integral or enumeration types
5863 can be used in constant-expressions. */
5864 if (!cast_valid_in_integral_constant_expression_p (type)
5865 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5866 return error_mark_node;
5868 switch (keyword)
5870 case RID_DYNCAST:
5871 postfix_expression
5872 = build_dynamic_cast (type, expression, tf_warning_or_error);
5873 break;
5874 case RID_STATCAST:
5875 postfix_expression
5876 = build_static_cast (type, expression, tf_warning_or_error);
5877 break;
5878 case RID_REINTCAST:
5879 postfix_expression
5880 = build_reinterpret_cast (type, expression,
5881 tf_warning_or_error);
5882 break;
5883 case RID_CONSTCAST:
5884 postfix_expression
5885 = build_const_cast (type, expression, tf_warning_or_error);
5886 break;
5887 default:
5888 gcc_unreachable ();
5891 break;
5893 case RID_TYPEID:
5895 tree type;
5896 const char *saved_message;
5897 bool saved_in_type_id_in_expr_p;
5899 /* Consume the `typeid' token. */
5900 cp_lexer_consume_token (parser->lexer);
5901 /* Look for the `(' token. */
5902 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5903 /* Types cannot be defined in a `typeid' expression. */
5904 saved_message = parser->type_definition_forbidden_message;
5905 parser->type_definition_forbidden_message
5906 = G_("types may not be defined in a %<typeid%> expression");
5907 /* We can't be sure yet whether we're looking at a type-id or an
5908 expression. */
5909 cp_parser_parse_tentatively (parser);
5910 /* Try a type-id first. */
5911 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5912 parser->in_type_id_in_expr_p = true;
5913 type = cp_parser_type_id (parser);
5914 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5915 /* Look for the `)' token. Otherwise, we can't be sure that
5916 we're not looking at an expression: consider `typeid (int
5917 (3))', for example. */
5918 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5919 /* If all went well, simply lookup the type-id. */
5920 if (cp_parser_parse_definitely (parser))
5921 postfix_expression = get_typeid (type, tf_warning_or_error);
5922 /* Otherwise, fall back to the expression variant. */
5923 else
5925 tree expression;
5927 /* Look for an expression. */
5928 expression = cp_parser_expression (parser, & idk);
5929 /* Compute its typeid. */
5930 postfix_expression = build_typeid (expression, tf_warning_or_error);
5931 /* Look for the `)' token. */
5932 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5934 /* Restore the saved message. */
5935 parser->type_definition_forbidden_message = saved_message;
5936 /* `typeid' may not appear in an integral constant expression. */
5937 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5938 return error_mark_node;
5940 break;
5942 case RID_TYPENAME:
5944 tree type;
5945 /* The syntax permitted here is the same permitted for an
5946 elaborated-type-specifier. */
5947 type = cp_parser_elaborated_type_specifier (parser,
5948 /*is_friend=*/false,
5949 /*is_declaration=*/false);
5950 postfix_expression = cp_parser_functional_cast (parser, type);
5952 break;
5954 case RID_CILK_SPAWN:
5956 cp_lexer_consume_token (parser->lexer);
5957 token = cp_lexer_peek_token (parser->lexer);
5958 if (token->type == CPP_SEMICOLON)
5960 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5961 "an expression");
5962 postfix_expression = error_mark_node;
5963 break;
5965 else if (!current_function_decl)
5967 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5968 "inside a function");
5969 postfix_expression = error_mark_node;
5970 break;
5972 else
5974 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5975 saved_in_statement = parser->in_statement;
5976 parser->in_statement |= IN_CILK_SPAWN;
5978 cfun->calls_cilk_spawn = 1;
5979 postfix_expression =
5980 cp_parser_postfix_expression (parser, false, false,
5981 false, false, &idk);
5982 if (!flag_cilkplus)
5984 error_at (token->location, "-fcilkplus must be enabled to use"
5985 " %<_Cilk_spawn%>");
5986 cfun->calls_cilk_spawn = 0;
5988 else if (saved_in_statement & IN_CILK_SPAWN)
5990 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5991 "are not permitted");
5992 postfix_expression = error_mark_node;
5993 cfun->calls_cilk_spawn = 0;
5995 else
5997 postfix_expression = build_cilk_spawn (token->location,
5998 postfix_expression);
5999 if (postfix_expression != error_mark_node)
6000 SET_EXPR_LOCATION (postfix_expression, input_location);
6001 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6003 break;
6006 case RID_BUILTIN_SHUFFLE:
6008 vec<tree, va_gc> *vec;
6009 unsigned int i;
6010 tree p;
6012 cp_lexer_consume_token (parser->lexer);
6013 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6014 /*cast_p=*/false, /*allow_expansion_p=*/true,
6015 /*non_constant_p=*/NULL);
6016 if (vec == NULL)
6017 return error_mark_node;
6019 FOR_EACH_VEC_ELT (*vec, i, p)
6020 mark_exp_read (p);
6022 if (vec->length () == 2)
6023 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6024 tf_warning_or_error);
6025 else if (vec->length () == 3)
6026 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6027 tf_warning_or_error);
6028 else
6030 error_at (loc, "wrong number of arguments to "
6031 "%<__builtin_shuffle%>");
6032 return error_mark_node;
6034 break;
6037 default:
6039 tree type;
6041 /* If the next thing is a simple-type-specifier, we may be
6042 looking at a functional cast. We could also be looking at
6043 an id-expression. So, we try the functional cast, and if
6044 that doesn't work we fall back to the primary-expression. */
6045 cp_parser_parse_tentatively (parser);
6046 /* Look for the simple-type-specifier. */
6047 type = cp_parser_simple_type_specifier (parser,
6048 /*decl_specs=*/NULL,
6049 CP_PARSER_FLAGS_NONE);
6050 /* Parse the cast itself. */
6051 if (!cp_parser_error_occurred (parser))
6052 postfix_expression
6053 = cp_parser_functional_cast (parser, type);
6054 /* If that worked, we're done. */
6055 if (cp_parser_parse_definitely (parser))
6056 break;
6058 /* If the functional-cast didn't work out, try a
6059 compound-literal. */
6060 if (cp_parser_allow_gnu_extensions_p (parser)
6061 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6063 tree initializer = NULL_TREE;
6065 cp_parser_parse_tentatively (parser);
6067 /* Avoid calling cp_parser_type_id pointlessly, see comment
6068 in cp_parser_cast_expression about c++/29234. */
6069 if (!cp_parser_compound_literal_p (parser))
6070 cp_parser_simulate_error (parser);
6071 else
6073 /* Parse the type. */
6074 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6075 parser->in_type_id_in_expr_p = true;
6076 type = cp_parser_type_id (parser);
6077 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6078 /* Look for the `)'. */
6079 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6082 /* If things aren't going well, there's no need to
6083 keep going. */
6084 if (!cp_parser_error_occurred (parser))
6086 bool non_constant_p;
6087 /* Parse the brace-enclosed initializer list. */
6088 initializer = cp_parser_braced_list (parser,
6089 &non_constant_p);
6091 /* If that worked, we're definitely looking at a
6092 compound-literal expression. */
6093 if (cp_parser_parse_definitely (parser))
6095 /* Warn the user that a compound literal is not
6096 allowed in standard C++. */
6097 pedwarn (input_location, OPT_Wpedantic,
6098 "ISO C++ forbids compound-literals");
6099 /* For simplicity, we disallow compound literals in
6100 constant-expressions. We could
6101 allow compound literals of integer type, whose
6102 initializer was a constant, in constant
6103 expressions. Permitting that usage, as a further
6104 extension, would not change the meaning of any
6105 currently accepted programs. (Of course, as
6106 compound literals are not part of ISO C++, the
6107 standard has nothing to say.) */
6108 if (cp_parser_non_integral_constant_expression (parser,
6109 NIC_NCC))
6111 postfix_expression = error_mark_node;
6112 break;
6114 /* Form the representation of the compound-literal. */
6115 postfix_expression
6116 = finish_compound_literal (type, initializer,
6117 tf_warning_or_error);
6118 break;
6122 /* It must be a primary-expression. */
6123 postfix_expression
6124 = cp_parser_primary_expression (parser, address_p, cast_p,
6125 /*template_arg_p=*/false,
6126 decltype_p,
6127 &idk);
6129 break;
6132 /* Note that we don't need to worry about calling build_cplus_new on a
6133 class-valued CALL_EXPR in decltype when it isn't the end of the
6134 postfix-expression; unary_complex_lvalue will take care of that for
6135 all these cases. */
6137 /* Keep looping until the postfix-expression is complete. */
6138 while (true)
6140 if (idk == CP_ID_KIND_UNQUALIFIED
6141 && identifier_p (postfix_expression)
6142 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6143 /* It is not a Koenig lookup function call. */
6144 postfix_expression
6145 = unqualified_name_lookup_error (postfix_expression);
6147 /* Peek at the next token. */
6148 token = cp_lexer_peek_token (parser->lexer);
6150 switch (token->type)
6152 case CPP_OPEN_SQUARE:
6153 if (cp_next_tokens_can_be_std_attribute_p (parser))
6155 cp_parser_error (parser,
6156 "two consecutive %<[%> shall "
6157 "only introduce an attribute");
6158 return error_mark_node;
6160 postfix_expression
6161 = cp_parser_postfix_open_square_expression (parser,
6162 postfix_expression,
6163 false,
6164 decltype_p);
6165 idk = CP_ID_KIND_NONE;
6166 is_member_access = false;
6167 break;
6169 case CPP_OPEN_PAREN:
6170 /* postfix-expression ( expression-list [opt] ) */
6172 bool koenig_p;
6173 bool is_builtin_constant_p;
6174 bool saved_integral_constant_expression_p = false;
6175 bool saved_non_integral_constant_expression_p = false;
6176 tsubst_flags_t complain = complain_flags (decltype_p);
6177 vec<tree, va_gc> *args;
6179 is_member_access = false;
6181 is_builtin_constant_p
6182 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6183 if (is_builtin_constant_p)
6185 /* The whole point of __builtin_constant_p is to allow
6186 non-constant expressions to appear as arguments. */
6187 saved_integral_constant_expression_p
6188 = parser->integral_constant_expression_p;
6189 saved_non_integral_constant_expression_p
6190 = parser->non_integral_constant_expression_p;
6191 parser->integral_constant_expression_p = false;
6193 args = (cp_parser_parenthesized_expression_list
6194 (parser, non_attr,
6195 /*cast_p=*/false, /*allow_expansion_p=*/true,
6196 /*non_constant_p=*/NULL,
6197 /*want_literal_zero_p=*/warn_memset_transposed_args));
6198 if (is_builtin_constant_p)
6200 parser->integral_constant_expression_p
6201 = saved_integral_constant_expression_p;
6202 parser->non_integral_constant_expression_p
6203 = saved_non_integral_constant_expression_p;
6206 if (args == NULL)
6208 postfix_expression = error_mark_node;
6209 break;
6212 /* Function calls are not permitted in
6213 constant-expressions. */
6214 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6215 && cp_parser_non_integral_constant_expression (parser,
6216 NIC_FUNC_CALL))
6218 postfix_expression = error_mark_node;
6219 release_tree_vector (args);
6220 break;
6223 koenig_p = false;
6224 if (idk == CP_ID_KIND_UNQUALIFIED
6225 || idk == CP_ID_KIND_TEMPLATE_ID)
6227 if (identifier_p (postfix_expression))
6229 if (!args->is_empty ())
6231 koenig_p = true;
6232 if (!any_type_dependent_arguments_p (args))
6233 postfix_expression
6234 = perform_koenig_lookup (postfix_expression, args,
6235 complain);
6237 else
6238 postfix_expression
6239 = unqualified_fn_lookup_error (postfix_expression);
6241 /* We do not perform argument-dependent lookup if
6242 normal lookup finds a non-function, in accordance
6243 with the expected resolution of DR 218. */
6244 else if (!args->is_empty ()
6245 && is_overloaded_fn (postfix_expression))
6247 tree fn = get_first_fn (postfix_expression);
6248 fn = STRIP_TEMPLATE (fn);
6250 /* Do not do argument dependent lookup if regular
6251 lookup finds a member function or a block-scope
6252 function declaration. [basic.lookup.argdep]/3 */
6253 if (!DECL_FUNCTION_MEMBER_P (fn)
6254 && !DECL_LOCAL_FUNCTION_P (fn))
6256 koenig_p = true;
6257 if (!any_type_dependent_arguments_p (args))
6258 postfix_expression
6259 = perform_koenig_lookup (postfix_expression, args,
6260 complain);
6265 if (warn_memset_transposed_args)
6267 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6268 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6269 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6270 && vec_safe_length (args) == 3
6271 && integer_zerop ((*args)[2])
6272 && LITERAL_ZERO_P ((*args)[2])
6273 && !(integer_zerop ((*args)[1])
6274 && LITERAL_ZERO_P ((*args)[1])))
6275 warning (OPT_Wmemset_transposed_args,
6276 "%<memset%> used with constant zero length "
6277 "parameter; this could be due to transposed "
6278 "parameters");
6280 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6281 to avoid leaking those into folder and middle-end. */
6282 unsigned int i;
6283 tree arg;
6284 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6285 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6286 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6289 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6291 tree instance = TREE_OPERAND (postfix_expression, 0);
6292 tree fn = TREE_OPERAND (postfix_expression, 1);
6294 if (processing_template_decl
6295 && (type_dependent_expression_p (instance)
6296 || (!BASELINK_P (fn)
6297 && TREE_CODE (fn) != FIELD_DECL)
6298 || type_dependent_expression_p (fn)
6299 || any_type_dependent_arguments_p (args)))
6301 postfix_expression
6302 = build_nt_call_vec (postfix_expression, args);
6303 release_tree_vector (args);
6304 break;
6307 if (BASELINK_P (fn))
6309 postfix_expression
6310 = (build_new_method_call
6311 (instance, fn, &args, NULL_TREE,
6312 (idk == CP_ID_KIND_QUALIFIED
6313 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6314 : LOOKUP_NORMAL),
6315 /*fn_p=*/NULL,
6316 complain));
6318 else
6319 postfix_expression
6320 = finish_call_expr (postfix_expression, &args,
6321 /*disallow_virtual=*/false,
6322 /*koenig_p=*/false,
6323 complain);
6325 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6326 || TREE_CODE (postfix_expression) == MEMBER_REF
6327 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6328 postfix_expression = (build_offset_ref_call_from_tree
6329 (postfix_expression, &args,
6330 complain));
6331 else if (idk == CP_ID_KIND_QUALIFIED)
6332 /* A call to a static class member, or a namespace-scope
6333 function. */
6334 postfix_expression
6335 = finish_call_expr (postfix_expression, &args,
6336 /*disallow_virtual=*/true,
6337 koenig_p,
6338 complain);
6339 else
6340 /* All other function calls. */
6341 postfix_expression
6342 = finish_call_expr (postfix_expression, &args,
6343 /*disallow_virtual=*/false,
6344 koenig_p,
6345 complain);
6347 protected_set_expr_location (postfix_expression, token->location);
6349 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6350 idk = CP_ID_KIND_NONE;
6352 release_tree_vector (args);
6354 break;
6356 case CPP_DOT:
6357 case CPP_DEREF:
6358 /* postfix-expression . template [opt] id-expression
6359 postfix-expression . pseudo-destructor-name
6360 postfix-expression -> template [opt] id-expression
6361 postfix-expression -> pseudo-destructor-name */
6363 /* Consume the `.' or `->' operator. */
6364 cp_lexer_consume_token (parser->lexer);
6366 postfix_expression
6367 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6368 postfix_expression,
6369 false, &idk, loc);
6371 is_member_access = true;
6372 break;
6374 case CPP_PLUS_PLUS:
6375 /* postfix-expression ++ */
6376 /* Consume the `++' token. */
6377 cp_lexer_consume_token (parser->lexer);
6378 /* Generate a representation for the complete expression. */
6379 postfix_expression
6380 = finish_increment_expr (postfix_expression,
6381 POSTINCREMENT_EXPR);
6382 /* Increments may not appear in constant-expressions. */
6383 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6384 postfix_expression = error_mark_node;
6385 idk = CP_ID_KIND_NONE;
6386 is_member_access = false;
6387 break;
6389 case CPP_MINUS_MINUS:
6390 /* postfix-expression -- */
6391 /* Consume the `--' token. */
6392 cp_lexer_consume_token (parser->lexer);
6393 /* Generate a representation for the complete expression. */
6394 postfix_expression
6395 = finish_increment_expr (postfix_expression,
6396 POSTDECREMENT_EXPR);
6397 /* Decrements may not appear in constant-expressions. */
6398 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6399 postfix_expression = error_mark_node;
6400 idk = CP_ID_KIND_NONE;
6401 is_member_access = false;
6402 break;
6404 default:
6405 if (pidk_return != NULL)
6406 * pidk_return = idk;
6407 if (member_access_only_p)
6408 return is_member_access? postfix_expression : error_mark_node;
6409 else
6410 return postfix_expression;
6414 /* We should never get here. */
6415 gcc_unreachable ();
6416 return error_mark_node;
6419 /* This function parses Cilk Plus array notations. If a normal array expr. is
6420 parsed then the array index is passed back to the caller through *INIT_INDEX
6421 and the function returns a NULL_TREE. If array notation expr. is parsed,
6422 then *INIT_INDEX is ignored by the caller and the function returns
6423 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6424 error_mark_node. */
6426 static tree
6427 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6428 tree array_value)
6430 cp_token *token = NULL;
6431 tree length_index, stride = NULL_TREE, value_tree, array_type;
6432 if (!array_value || array_value == error_mark_node)
6434 cp_parser_skip_to_end_of_statement (parser);
6435 return error_mark_node;
6438 array_type = TREE_TYPE (array_value);
6440 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6441 parser->colon_corrects_to_scope_p = false;
6442 token = cp_lexer_peek_token (parser->lexer);
6444 if (!token)
6446 cp_parser_error (parser, "expected %<:%> or numeral");
6447 return error_mark_node;
6449 else if (token->type == CPP_COLON)
6451 /* Consume the ':'. */
6452 cp_lexer_consume_token (parser->lexer);
6454 /* If we are here, then we have a case like this A[:]. */
6455 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6457 cp_parser_error (parser, "expected %<]%>");
6458 cp_parser_skip_to_end_of_statement (parser);
6459 return error_mark_node;
6461 *init_index = NULL_TREE;
6462 stride = NULL_TREE;
6463 length_index = NULL_TREE;
6465 else
6467 /* If we are here, then there are three valid possibilities:
6468 1. ARRAY [ EXP ]
6469 2. ARRAY [ EXP : EXP ]
6470 3. ARRAY [ EXP : EXP : EXP ] */
6472 *init_index = cp_parser_expression (parser);
6473 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6475 /* This indicates that we have a normal array expression. */
6476 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6477 return NULL_TREE;
6480 /* Consume the ':'. */
6481 cp_lexer_consume_token (parser->lexer);
6482 length_index = cp_parser_expression (parser);
6483 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6485 cp_lexer_consume_token (parser->lexer);
6486 stride = cp_parser_expression (parser);
6489 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6491 if (*init_index == error_mark_node || length_index == error_mark_node
6492 || stride == error_mark_node || array_type == error_mark_node)
6494 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6495 cp_lexer_consume_token (parser->lexer);
6496 return error_mark_node;
6498 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6500 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6501 length_index, stride, array_type);
6502 return value_tree;
6505 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6506 by cp_parser_builtin_offsetof. We're looking for
6508 postfix-expression [ expression ]
6509 postfix-expression [ braced-init-list ] (C++11)
6511 FOR_OFFSETOF is set if we're being called in that context, which
6512 changes how we deal with integer constant expressions. */
6514 static tree
6515 cp_parser_postfix_open_square_expression (cp_parser *parser,
6516 tree postfix_expression,
6517 bool for_offsetof,
6518 bool decltype_p)
6520 tree index = NULL_TREE;
6521 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6522 bool saved_greater_than_is_operator_p;
6524 /* Consume the `[' token. */
6525 cp_lexer_consume_token (parser->lexer);
6527 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6528 parser->greater_than_is_operator_p = true;
6530 /* Parse the index expression. */
6531 /* ??? For offsetof, there is a question of what to allow here. If
6532 offsetof is not being used in an integral constant expression context,
6533 then we *could* get the right answer by computing the value at runtime.
6534 If we are in an integral constant expression context, then we might
6535 could accept any constant expression; hard to say without analysis.
6536 Rather than open the barn door too wide right away, allow only integer
6537 constant expressions here. */
6538 if (for_offsetof)
6539 index = cp_parser_constant_expression (parser);
6540 else
6542 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6544 bool expr_nonconst_p;
6545 cp_lexer_set_source_position (parser->lexer);
6546 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6547 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6548 if (flag_cilkplus
6549 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6551 error_at (cp_lexer_peek_token (parser->lexer)->location,
6552 "braced list index is not allowed with array "
6553 "notation");
6554 cp_parser_skip_to_end_of_statement (parser);
6555 return error_mark_node;
6558 else if (flag_cilkplus)
6560 /* Here are have these two options:
6561 ARRAY[EXP : EXP] - Array notation expr with default
6562 stride of 1.
6563 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6564 stride. */
6565 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6566 postfix_expression);
6567 if (an_exp)
6568 return an_exp;
6570 else
6571 index = cp_parser_expression (parser);
6574 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6576 /* Look for the closing `]'. */
6577 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6579 /* Build the ARRAY_REF. */
6580 postfix_expression = grok_array_decl (loc, postfix_expression,
6581 index, decltype_p);
6583 /* When not doing offsetof, array references are not permitted in
6584 constant-expressions. */
6585 if (!for_offsetof
6586 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6587 postfix_expression = error_mark_node;
6589 return postfix_expression;
6592 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6593 by cp_parser_builtin_offsetof. We're looking for
6595 postfix-expression . template [opt] id-expression
6596 postfix-expression . pseudo-destructor-name
6597 postfix-expression -> template [opt] id-expression
6598 postfix-expression -> pseudo-destructor-name
6600 FOR_OFFSETOF is set if we're being called in that context. That sorta
6601 limits what of the above we'll actually accept, but nevermind.
6602 TOKEN_TYPE is the "." or "->" token, which will already have been
6603 removed from the stream. */
6605 static tree
6606 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6607 enum cpp_ttype token_type,
6608 tree postfix_expression,
6609 bool for_offsetof, cp_id_kind *idk,
6610 location_t location)
6612 tree name;
6613 bool dependent_p;
6614 bool pseudo_destructor_p;
6615 tree scope = NULL_TREE;
6617 /* If this is a `->' operator, dereference the pointer. */
6618 if (token_type == CPP_DEREF)
6619 postfix_expression = build_x_arrow (location, postfix_expression,
6620 tf_warning_or_error);
6621 /* Check to see whether or not the expression is type-dependent. */
6622 dependent_p = type_dependent_expression_p (postfix_expression);
6623 /* The identifier following the `->' or `.' is not qualified. */
6624 parser->scope = NULL_TREE;
6625 parser->qualifying_scope = NULL_TREE;
6626 parser->object_scope = NULL_TREE;
6627 *idk = CP_ID_KIND_NONE;
6629 /* Enter the scope corresponding to the type of the object
6630 given by the POSTFIX_EXPRESSION. */
6631 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6633 scope = TREE_TYPE (postfix_expression);
6634 /* According to the standard, no expression should ever have
6635 reference type. Unfortunately, we do not currently match
6636 the standard in this respect in that our internal representation
6637 of an expression may have reference type even when the standard
6638 says it does not. Therefore, we have to manually obtain the
6639 underlying type here. */
6640 scope = non_reference (scope);
6641 /* The type of the POSTFIX_EXPRESSION must be complete. */
6642 if (scope == unknown_type_node)
6644 error_at (location, "%qE does not have class type",
6645 postfix_expression);
6646 scope = NULL_TREE;
6648 /* Unlike the object expression in other contexts, *this is not
6649 required to be of complete type for purposes of class member
6650 access (5.2.5) outside the member function body. */
6651 else if (postfix_expression != current_class_ref
6652 && !(processing_template_decl && scope == current_class_type))
6653 scope = complete_type_or_else (scope, NULL_TREE);
6654 /* Let the name lookup machinery know that we are processing a
6655 class member access expression. */
6656 parser->context->object_type = scope;
6657 /* If something went wrong, we want to be able to discern that case,
6658 as opposed to the case where there was no SCOPE due to the type
6659 of expression being dependent. */
6660 if (!scope)
6661 scope = error_mark_node;
6662 /* If the SCOPE was erroneous, make the various semantic analysis
6663 functions exit quickly -- and without issuing additional error
6664 messages. */
6665 if (scope == error_mark_node)
6666 postfix_expression = error_mark_node;
6669 /* Assume this expression is not a pseudo-destructor access. */
6670 pseudo_destructor_p = false;
6672 /* If the SCOPE is a scalar type, then, if this is a valid program,
6673 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6674 is type dependent, it can be pseudo-destructor-name or something else.
6675 Try to parse it as pseudo-destructor-name first. */
6676 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6678 tree s;
6679 tree type;
6681 cp_parser_parse_tentatively (parser);
6682 /* Parse the pseudo-destructor-name. */
6683 s = NULL_TREE;
6684 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6685 &s, &type);
6686 if (dependent_p
6687 && (cp_parser_error_occurred (parser)
6688 || !SCALAR_TYPE_P (type)))
6689 cp_parser_abort_tentative_parse (parser);
6690 else if (cp_parser_parse_definitely (parser))
6692 pseudo_destructor_p = true;
6693 postfix_expression
6694 = finish_pseudo_destructor_expr (postfix_expression,
6695 s, type, location);
6699 if (!pseudo_destructor_p)
6701 /* If the SCOPE is not a scalar type, we are looking at an
6702 ordinary class member access expression, rather than a
6703 pseudo-destructor-name. */
6704 bool template_p;
6705 cp_token *token = cp_lexer_peek_token (parser->lexer);
6706 /* Parse the id-expression. */
6707 name = (cp_parser_id_expression
6708 (parser,
6709 cp_parser_optional_template_keyword (parser),
6710 /*check_dependency_p=*/true,
6711 &template_p,
6712 /*declarator_p=*/false,
6713 /*optional_p=*/false));
6714 /* In general, build a SCOPE_REF if the member name is qualified.
6715 However, if the name was not dependent and has already been
6716 resolved; there is no need to build the SCOPE_REF. For example;
6718 struct X { void f(); };
6719 template <typename T> void f(T* t) { t->X::f(); }
6721 Even though "t" is dependent, "X::f" is not and has been resolved
6722 to a BASELINK; there is no need to include scope information. */
6724 /* But we do need to remember that there was an explicit scope for
6725 virtual function calls. */
6726 if (parser->scope)
6727 *idk = CP_ID_KIND_QUALIFIED;
6729 /* If the name is a template-id that names a type, we will get a
6730 TYPE_DECL here. That is invalid code. */
6731 if (TREE_CODE (name) == TYPE_DECL)
6733 error_at (token->location, "invalid use of %qD", name);
6734 postfix_expression = error_mark_node;
6736 else
6738 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6740 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6742 error_at (token->location, "%<%D::%D%> is not a class member",
6743 parser->scope, name);
6744 postfix_expression = error_mark_node;
6746 else
6747 name = build_qualified_name (/*type=*/NULL_TREE,
6748 parser->scope,
6749 name,
6750 template_p);
6751 parser->scope = NULL_TREE;
6752 parser->qualifying_scope = NULL_TREE;
6753 parser->object_scope = NULL_TREE;
6755 if (parser->scope && name && BASELINK_P (name))
6756 adjust_result_of_qualified_name_lookup
6757 (name, parser->scope, scope);
6758 postfix_expression
6759 = finish_class_member_access_expr (postfix_expression, name,
6760 template_p,
6761 tf_warning_or_error);
6765 /* We no longer need to look up names in the scope of the object on
6766 the left-hand side of the `.' or `->' operator. */
6767 parser->context->object_type = NULL_TREE;
6769 /* Outside of offsetof, these operators may not appear in
6770 constant-expressions. */
6771 if (!for_offsetof
6772 && (cp_parser_non_integral_constant_expression
6773 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6774 postfix_expression = error_mark_node;
6776 return postfix_expression;
6779 /* Cache of LITERAL_ZERO_P constants. */
6781 static GTY(()) tree literal_zeros[itk_none];
6783 /* Parse a parenthesized expression-list.
6785 expression-list:
6786 assignment-expression
6787 expression-list, assignment-expression
6789 attribute-list:
6790 expression-list
6791 identifier
6792 identifier, expression-list
6794 CAST_P is true if this expression is the target of a cast.
6796 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6797 argument pack.
6799 Returns a vector of trees. Each element is a representation of an
6800 assignment-expression. NULL is returned if the ( and or ) are
6801 missing. An empty, but allocated, vector is returned on no
6802 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6803 if we are parsing an attribute list for an attribute that wants a
6804 plain identifier argument, normal_attr for an attribute that wants
6805 an expression, or non_attr if we aren't parsing an attribute list. If
6806 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6807 not all of the expressions in the list were constant.
6808 WANT_LITERAL_ZERO_P is true if the caller is interested in
6809 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6810 immediately, this can be removed. */
6812 static vec<tree, va_gc> *
6813 cp_parser_parenthesized_expression_list (cp_parser* parser,
6814 int is_attribute_list,
6815 bool cast_p,
6816 bool allow_expansion_p,
6817 bool *non_constant_p,
6818 bool want_literal_zero_p)
6820 vec<tree, va_gc> *expression_list;
6821 bool fold_expr_p = is_attribute_list != non_attr;
6822 tree identifier = NULL_TREE;
6823 bool saved_greater_than_is_operator_p;
6825 /* Assume all the expressions will be constant. */
6826 if (non_constant_p)
6827 *non_constant_p = false;
6829 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6830 return NULL;
6832 expression_list = make_tree_vector ();
6834 /* Within a parenthesized expression, a `>' token is always
6835 the greater-than operator. */
6836 saved_greater_than_is_operator_p
6837 = parser->greater_than_is_operator_p;
6838 parser->greater_than_is_operator_p = true;
6840 /* Consume expressions until there are no more. */
6841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6842 while (true)
6844 tree expr;
6846 /* At the beginning of attribute lists, check to see if the
6847 next token is an identifier. */
6848 if (is_attribute_list == id_attr
6849 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6851 cp_token *token;
6853 /* Consume the identifier. */
6854 token = cp_lexer_consume_token (parser->lexer);
6855 /* Save the identifier. */
6856 identifier = token->u.value;
6858 else
6860 bool expr_non_constant_p;
6862 /* Parse the next assignment-expression. */
6863 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6865 /* A braced-init-list. */
6866 cp_lexer_set_source_position (parser->lexer);
6867 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6868 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6869 if (non_constant_p && expr_non_constant_p)
6870 *non_constant_p = true;
6872 else if (non_constant_p)
6874 expr = (cp_parser_constant_expression
6875 (parser, /*allow_non_constant_p=*/true,
6876 &expr_non_constant_p));
6877 if (expr_non_constant_p)
6878 *non_constant_p = true;
6880 else
6882 expr = NULL_TREE;
6883 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6884 switch (tok->type)
6886 case CPP_NUMBER:
6887 case CPP_CHAR:
6888 case CPP_WCHAR:
6889 case CPP_CHAR16:
6890 case CPP_CHAR32:
6891 case CPP_UTF8CHAR:
6892 /* If a parameter is literal zero alone, remember it
6893 for -Wmemset-transposed-args warning. */
6894 if (integer_zerop (tok->u.value)
6895 && !TREE_OVERFLOW (tok->u.value)
6896 && want_literal_zero_p
6897 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6898 == CPP_COMMA
6899 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6900 == CPP_CLOSE_PAREN))
6902 unsigned int i;
6903 for (i = 0; i < itk_none; ++i)
6904 if (TREE_TYPE (tok->u.value) == integer_types[i])
6905 break;
6906 if (i < itk_none && literal_zeros[i])
6907 expr = literal_zeros[i];
6908 else
6910 expr = copy_node (tok->u.value);
6911 LITERAL_ZERO_P (expr) = 1;
6912 if (i < itk_none)
6913 literal_zeros[i] = expr;
6915 /* Consume the 0 token (or '\0', 0LL etc.). */
6916 cp_lexer_consume_token (parser->lexer);
6918 break;
6919 default:
6920 break;
6922 if (expr == NULL_TREE)
6923 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6924 cast_p);
6927 if (fold_expr_p)
6928 expr = instantiate_non_dependent_expr (expr);
6930 /* If we have an ellipsis, then this is an expression
6931 expansion. */
6932 if (allow_expansion_p
6933 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6935 /* Consume the `...'. */
6936 cp_lexer_consume_token (parser->lexer);
6938 /* Build the argument pack. */
6939 expr = make_pack_expansion (expr);
6942 /* Add it to the list. We add error_mark_node
6943 expressions to the list, so that we can still tell if
6944 the correct form for a parenthesized expression-list
6945 is found. That gives better errors. */
6946 vec_safe_push (expression_list, expr);
6948 if (expr == error_mark_node)
6949 goto skip_comma;
6952 /* After the first item, attribute lists look the same as
6953 expression lists. */
6954 is_attribute_list = non_attr;
6956 get_comma:;
6957 /* If the next token isn't a `,', then we are done. */
6958 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6959 break;
6961 /* Otherwise, consume the `,' and keep going. */
6962 cp_lexer_consume_token (parser->lexer);
6965 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6967 int ending;
6969 skip_comma:;
6970 /* We try and resync to an unnested comma, as that will give the
6971 user better diagnostics. */
6972 ending = cp_parser_skip_to_closing_parenthesis (parser,
6973 /*recovering=*/true,
6974 /*or_comma=*/true,
6975 /*consume_paren=*/true);
6976 if (ending < 0)
6977 goto get_comma;
6978 if (!ending)
6980 parser->greater_than_is_operator_p
6981 = saved_greater_than_is_operator_p;
6982 return NULL;
6986 parser->greater_than_is_operator_p
6987 = saved_greater_than_is_operator_p;
6989 if (identifier)
6990 vec_safe_insert (expression_list, 0, identifier);
6992 return expression_list;
6995 /* Parse a pseudo-destructor-name.
6997 pseudo-destructor-name:
6998 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6999 :: [opt] nested-name-specifier template template-id :: ~ type-name
7000 :: [opt] nested-name-specifier [opt] ~ type-name
7002 If either of the first two productions is used, sets *SCOPE to the
7003 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7004 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7005 or ERROR_MARK_NODE if the parse fails. */
7007 static void
7008 cp_parser_pseudo_destructor_name (cp_parser* parser,
7009 tree object,
7010 tree* scope,
7011 tree* type)
7013 bool nested_name_specifier_p;
7015 /* Handle ~auto. */
7016 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7017 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7018 && !type_dependent_expression_p (object))
7020 if (cxx_dialect < cxx14)
7021 pedwarn (input_location, 0,
7022 "%<~auto%> only available with "
7023 "-std=c++14 or -std=gnu++14");
7024 cp_lexer_consume_token (parser->lexer);
7025 cp_lexer_consume_token (parser->lexer);
7026 *scope = NULL_TREE;
7027 *type = TREE_TYPE (object);
7028 return;
7031 /* Assume that things will not work out. */
7032 *type = error_mark_node;
7034 /* Look for the optional `::' operator. */
7035 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7036 /* Look for the optional nested-name-specifier. */
7037 nested_name_specifier_p
7038 = (cp_parser_nested_name_specifier_opt (parser,
7039 /*typename_keyword_p=*/false,
7040 /*check_dependency_p=*/true,
7041 /*type_p=*/false,
7042 /*is_declaration=*/false)
7043 != NULL_TREE);
7044 /* Now, if we saw a nested-name-specifier, we might be doing the
7045 second production. */
7046 if (nested_name_specifier_p
7047 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7049 /* Consume the `template' keyword. */
7050 cp_lexer_consume_token (parser->lexer);
7051 /* Parse the template-id. */
7052 cp_parser_template_id (parser,
7053 /*template_keyword_p=*/true,
7054 /*check_dependency_p=*/false,
7055 class_type,
7056 /*is_declaration=*/true);
7057 /* Look for the `::' token. */
7058 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7060 /* If the next token is not a `~', then there might be some
7061 additional qualification. */
7062 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7064 /* At this point, we're looking for "type-name :: ~". The type-name
7065 must not be a class-name, since this is a pseudo-destructor. So,
7066 it must be either an enum-name, or a typedef-name -- both of which
7067 are just identifiers. So, we peek ahead to check that the "::"
7068 and "~" tokens are present; if they are not, then we can avoid
7069 calling type_name. */
7070 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7071 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7072 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7074 cp_parser_error (parser, "non-scalar type");
7075 return;
7078 /* Look for the type-name. */
7079 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7080 if (*scope == error_mark_node)
7081 return;
7083 /* Look for the `::' token. */
7084 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7086 else
7087 *scope = NULL_TREE;
7089 /* Look for the `~'. */
7090 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7092 /* Once we see the ~, this has to be a pseudo-destructor. */
7093 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7094 cp_parser_commit_to_topmost_tentative_parse (parser);
7096 /* Look for the type-name again. We are not responsible for
7097 checking that it matches the first type-name. */
7098 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7101 /* Parse a unary-expression.
7103 unary-expression:
7104 postfix-expression
7105 ++ cast-expression
7106 -- cast-expression
7107 unary-operator cast-expression
7108 sizeof unary-expression
7109 sizeof ( type-id )
7110 alignof ( type-id ) [C++0x]
7111 new-expression
7112 delete-expression
7114 GNU Extensions:
7116 unary-expression:
7117 __extension__ cast-expression
7118 __alignof__ unary-expression
7119 __alignof__ ( type-id )
7120 alignof unary-expression [C++0x]
7121 __real__ cast-expression
7122 __imag__ cast-expression
7123 && identifier
7124 sizeof ( type-id ) { initializer-list , [opt] }
7125 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7126 __alignof__ ( type-id ) { initializer-list , [opt] }
7128 ADDRESS_P is true iff the unary-expression is appearing as the
7129 operand of the `&' operator. CAST_P is true if this expression is
7130 the target of a cast.
7132 Returns a representation of the expression. */
7134 static tree
7135 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7136 bool address_p, bool cast_p, bool decltype_p)
7138 cp_token *token;
7139 enum tree_code unary_operator;
7141 /* Peek at the next token. */
7142 token = cp_lexer_peek_token (parser->lexer);
7143 /* Some keywords give away the kind of expression. */
7144 if (token->type == CPP_KEYWORD)
7146 enum rid keyword = token->keyword;
7148 switch (keyword)
7150 case RID_ALIGNOF:
7151 case RID_SIZEOF:
7153 tree operand, ret;
7154 enum tree_code op;
7155 location_t first_loc;
7157 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7158 /* Consume the token. */
7159 cp_lexer_consume_token (parser->lexer);
7160 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7161 /* Parse the operand. */
7162 operand = cp_parser_sizeof_operand (parser, keyword);
7164 if (TYPE_P (operand))
7165 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7166 else
7168 /* ISO C++ defines alignof only with types, not with
7169 expressions. So pedwarn if alignof is used with a non-
7170 type expression. However, __alignof__ is ok. */
7171 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7172 pedwarn (token->location, OPT_Wpedantic,
7173 "ISO C++ does not allow %<alignof%> "
7174 "with a non-type");
7176 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7178 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7179 SIZEOF_EXPR with the original operand. */
7180 if (op == SIZEOF_EXPR && ret != error_mark_node)
7182 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7184 if (!processing_template_decl && TYPE_P (operand))
7186 ret = build_min (SIZEOF_EXPR, size_type_node,
7187 build1 (NOP_EXPR, operand,
7188 error_mark_node));
7189 SIZEOF_EXPR_TYPE_P (ret) = 1;
7191 else
7192 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7193 TREE_SIDE_EFFECTS (ret) = 0;
7194 TREE_READONLY (ret) = 1;
7196 SET_EXPR_LOCATION (ret, first_loc);
7198 return ret;
7201 case RID_NEW:
7202 return cp_parser_new_expression (parser);
7204 case RID_DELETE:
7205 return cp_parser_delete_expression (parser);
7207 case RID_EXTENSION:
7209 /* The saved value of the PEDANTIC flag. */
7210 int saved_pedantic;
7211 tree expr;
7213 /* Save away the PEDANTIC flag. */
7214 cp_parser_extension_opt (parser, &saved_pedantic);
7215 /* Parse the cast-expression. */
7216 expr = cp_parser_simple_cast_expression (parser);
7217 /* Restore the PEDANTIC flag. */
7218 pedantic = saved_pedantic;
7220 return expr;
7223 case RID_REALPART:
7224 case RID_IMAGPART:
7226 tree expression;
7228 /* Consume the `__real__' or `__imag__' token. */
7229 cp_lexer_consume_token (parser->lexer);
7230 /* Parse the cast-expression. */
7231 expression = cp_parser_simple_cast_expression (parser);
7232 /* Create the complete representation. */
7233 return build_x_unary_op (token->location,
7234 (keyword == RID_REALPART
7235 ? REALPART_EXPR : IMAGPART_EXPR),
7236 expression,
7237 tf_warning_or_error);
7239 break;
7241 case RID_TRANSACTION_ATOMIC:
7242 case RID_TRANSACTION_RELAXED:
7243 return cp_parser_transaction_expression (parser, keyword);
7245 case RID_NOEXCEPT:
7247 tree expr;
7248 const char *saved_message;
7249 bool saved_integral_constant_expression_p;
7250 bool saved_non_integral_constant_expression_p;
7251 bool saved_greater_than_is_operator_p;
7253 cp_lexer_consume_token (parser->lexer);
7254 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7256 saved_message = parser->type_definition_forbidden_message;
7257 parser->type_definition_forbidden_message
7258 = G_("types may not be defined in %<noexcept%> expressions");
7260 saved_integral_constant_expression_p
7261 = parser->integral_constant_expression_p;
7262 saved_non_integral_constant_expression_p
7263 = parser->non_integral_constant_expression_p;
7264 parser->integral_constant_expression_p = false;
7266 saved_greater_than_is_operator_p
7267 = parser->greater_than_is_operator_p;
7268 parser->greater_than_is_operator_p = true;
7270 ++cp_unevaluated_operand;
7271 ++c_inhibit_evaluation_warnings;
7272 ++cp_noexcept_operand;
7273 expr = cp_parser_expression (parser);
7274 --cp_noexcept_operand;
7275 --c_inhibit_evaluation_warnings;
7276 --cp_unevaluated_operand;
7278 parser->greater_than_is_operator_p
7279 = saved_greater_than_is_operator_p;
7281 parser->integral_constant_expression_p
7282 = saved_integral_constant_expression_p;
7283 parser->non_integral_constant_expression_p
7284 = saved_non_integral_constant_expression_p;
7286 parser->type_definition_forbidden_message = saved_message;
7288 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7289 return finish_noexcept_expr (expr, tf_warning_or_error);
7292 default:
7293 break;
7297 /* Look for the `:: new' and `:: delete', which also signal the
7298 beginning of a new-expression, or delete-expression,
7299 respectively. If the next token is `::', then it might be one of
7300 these. */
7301 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7303 enum rid keyword;
7305 /* See if the token after the `::' is one of the keywords in
7306 which we're interested. */
7307 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7308 /* If it's `new', we have a new-expression. */
7309 if (keyword == RID_NEW)
7310 return cp_parser_new_expression (parser);
7311 /* Similarly, for `delete'. */
7312 else if (keyword == RID_DELETE)
7313 return cp_parser_delete_expression (parser);
7316 /* Look for a unary operator. */
7317 unary_operator = cp_parser_unary_operator (token);
7318 /* The `++' and `--' operators can be handled similarly, even though
7319 they are not technically unary-operators in the grammar. */
7320 if (unary_operator == ERROR_MARK)
7322 if (token->type == CPP_PLUS_PLUS)
7323 unary_operator = PREINCREMENT_EXPR;
7324 else if (token->type == CPP_MINUS_MINUS)
7325 unary_operator = PREDECREMENT_EXPR;
7326 /* Handle the GNU address-of-label extension. */
7327 else if (cp_parser_allow_gnu_extensions_p (parser)
7328 && token->type == CPP_AND_AND)
7330 tree identifier;
7331 tree expression;
7332 location_t loc = token->location;
7334 /* Consume the '&&' token. */
7335 cp_lexer_consume_token (parser->lexer);
7336 /* Look for the identifier. */
7337 identifier = cp_parser_identifier (parser);
7338 /* Create an expression representing the address. */
7339 expression = finish_label_address_expr (identifier, loc);
7340 if (cp_parser_non_integral_constant_expression (parser,
7341 NIC_ADDR_LABEL))
7342 expression = error_mark_node;
7343 return expression;
7346 if (unary_operator != ERROR_MARK)
7348 tree cast_expression;
7349 tree expression = error_mark_node;
7350 non_integral_constant non_constant_p = NIC_NONE;
7351 location_t loc = token->location;
7352 tsubst_flags_t complain = complain_flags (decltype_p);
7354 /* Consume the operator token. */
7355 token = cp_lexer_consume_token (parser->lexer);
7356 /* Parse the cast-expression. */
7357 cast_expression
7358 = cp_parser_cast_expression (parser,
7359 unary_operator == ADDR_EXPR,
7360 /*cast_p=*/false,
7361 /*decltype*/false,
7362 pidk);
7363 /* Now, build an appropriate representation. */
7364 switch (unary_operator)
7366 case INDIRECT_REF:
7367 non_constant_p = NIC_STAR;
7368 expression = build_x_indirect_ref (loc, cast_expression,
7369 RO_UNARY_STAR,
7370 complain);
7371 break;
7373 case ADDR_EXPR:
7374 non_constant_p = NIC_ADDR;
7375 /* Fall through. */
7376 case BIT_NOT_EXPR:
7377 expression = build_x_unary_op (loc, unary_operator,
7378 cast_expression,
7379 complain);
7380 break;
7382 case PREINCREMENT_EXPR:
7383 case PREDECREMENT_EXPR:
7384 non_constant_p = unary_operator == PREINCREMENT_EXPR
7385 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7386 /* Fall through. */
7387 case UNARY_PLUS_EXPR:
7388 case NEGATE_EXPR:
7389 case TRUTH_NOT_EXPR:
7390 expression = finish_unary_op_expr (loc, unary_operator,
7391 cast_expression, complain);
7392 break;
7394 default:
7395 gcc_unreachable ();
7398 if (non_constant_p != NIC_NONE
7399 && cp_parser_non_integral_constant_expression (parser,
7400 non_constant_p))
7401 expression = error_mark_node;
7403 return expression;
7406 return cp_parser_postfix_expression (parser, address_p, cast_p,
7407 /*member_access_only_p=*/false,
7408 decltype_p,
7409 pidk);
7412 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7413 unary-operator, the corresponding tree code is returned. */
7415 static enum tree_code
7416 cp_parser_unary_operator (cp_token* token)
7418 switch (token->type)
7420 case CPP_MULT:
7421 return INDIRECT_REF;
7423 case CPP_AND:
7424 return ADDR_EXPR;
7426 case CPP_PLUS:
7427 return UNARY_PLUS_EXPR;
7429 case CPP_MINUS:
7430 return NEGATE_EXPR;
7432 case CPP_NOT:
7433 return TRUTH_NOT_EXPR;
7435 case CPP_COMPL:
7436 return BIT_NOT_EXPR;
7438 default:
7439 return ERROR_MARK;
7443 /* Parse a new-expression.
7445 new-expression:
7446 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7447 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7449 Returns a representation of the expression. */
7451 static tree
7452 cp_parser_new_expression (cp_parser* parser)
7454 bool global_scope_p;
7455 vec<tree, va_gc> *placement;
7456 tree type;
7457 vec<tree, va_gc> *initializer;
7458 tree nelts = NULL_TREE;
7459 tree ret;
7461 /* Look for the optional `::' operator. */
7462 global_scope_p
7463 = (cp_parser_global_scope_opt (parser,
7464 /*current_scope_valid_p=*/false)
7465 != NULL_TREE);
7466 /* Look for the `new' operator. */
7467 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7468 /* There's no easy way to tell a new-placement from the
7469 `( type-id )' construct. */
7470 cp_parser_parse_tentatively (parser);
7471 /* Look for a new-placement. */
7472 placement = cp_parser_new_placement (parser);
7473 /* If that didn't work out, there's no new-placement. */
7474 if (!cp_parser_parse_definitely (parser))
7476 if (placement != NULL)
7477 release_tree_vector (placement);
7478 placement = NULL;
7481 /* If the next token is a `(', then we have a parenthesized
7482 type-id. */
7483 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7485 cp_token *token;
7486 const char *saved_message = parser->type_definition_forbidden_message;
7488 /* Consume the `('. */
7489 cp_lexer_consume_token (parser->lexer);
7491 /* Parse the type-id. */
7492 parser->type_definition_forbidden_message
7493 = G_("types may not be defined in a new-expression");
7494 type = cp_parser_type_id (parser);
7495 parser->type_definition_forbidden_message = saved_message;
7497 /* Look for the closing `)'. */
7498 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7499 token = cp_lexer_peek_token (parser->lexer);
7500 /* There should not be a direct-new-declarator in this production,
7501 but GCC used to allowed this, so we check and emit a sensible error
7502 message for this case. */
7503 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7505 error_at (token->location,
7506 "array bound forbidden after parenthesized type-id");
7507 inform (token->location,
7508 "try removing the parentheses around the type-id");
7509 cp_parser_direct_new_declarator (parser);
7512 /* Otherwise, there must be a new-type-id. */
7513 else
7514 type = cp_parser_new_type_id (parser, &nelts);
7516 /* If the next token is a `(' or '{', then we have a new-initializer. */
7517 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7518 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7519 initializer = cp_parser_new_initializer (parser);
7520 else
7521 initializer = NULL;
7523 /* A new-expression may not appear in an integral constant
7524 expression. */
7525 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7526 ret = error_mark_node;
7527 else
7529 /* Create a representation of the new-expression. */
7530 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7531 tf_warning_or_error);
7534 if (placement != NULL)
7535 release_tree_vector (placement);
7536 if (initializer != NULL)
7537 release_tree_vector (initializer);
7539 return ret;
7542 /* Parse a new-placement.
7544 new-placement:
7545 ( expression-list )
7547 Returns the same representation as for an expression-list. */
7549 static vec<tree, va_gc> *
7550 cp_parser_new_placement (cp_parser* parser)
7552 vec<tree, va_gc> *expression_list;
7554 /* Parse the expression-list. */
7555 expression_list = (cp_parser_parenthesized_expression_list
7556 (parser, non_attr, /*cast_p=*/false,
7557 /*allow_expansion_p=*/true,
7558 /*non_constant_p=*/NULL));
7560 if (expression_list && expression_list->is_empty ())
7561 error ("expected expression-list or type-id");
7563 return expression_list;
7566 /* Parse a new-type-id.
7568 new-type-id:
7569 type-specifier-seq new-declarator [opt]
7571 Returns the TYPE allocated. If the new-type-id indicates an array
7572 type, *NELTS is set to the number of elements in the last array
7573 bound; the TYPE will not include the last array bound. */
7575 static tree
7576 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7578 cp_decl_specifier_seq type_specifier_seq;
7579 cp_declarator *new_declarator;
7580 cp_declarator *declarator;
7581 cp_declarator *outer_declarator;
7582 const char *saved_message;
7584 /* The type-specifier sequence must not contain type definitions.
7585 (It cannot contain declarations of new types either, but if they
7586 are not definitions we will catch that because they are not
7587 complete.) */
7588 saved_message = parser->type_definition_forbidden_message;
7589 parser->type_definition_forbidden_message
7590 = G_("types may not be defined in a new-type-id");
7591 /* Parse the type-specifier-seq. */
7592 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7593 /*is_trailing_return=*/false,
7594 &type_specifier_seq);
7595 /* Restore the old message. */
7596 parser->type_definition_forbidden_message = saved_message;
7598 if (type_specifier_seq.type == error_mark_node)
7599 return error_mark_node;
7601 /* Parse the new-declarator. */
7602 new_declarator = cp_parser_new_declarator_opt (parser);
7604 /* Determine the number of elements in the last array dimension, if
7605 any. */
7606 *nelts = NULL_TREE;
7607 /* Skip down to the last array dimension. */
7608 declarator = new_declarator;
7609 outer_declarator = NULL;
7610 while (declarator && (declarator->kind == cdk_pointer
7611 || declarator->kind == cdk_ptrmem))
7613 outer_declarator = declarator;
7614 declarator = declarator->declarator;
7616 while (declarator
7617 && declarator->kind == cdk_array
7618 && declarator->declarator
7619 && declarator->declarator->kind == cdk_array)
7621 outer_declarator = declarator;
7622 declarator = declarator->declarator;
7625 if (declarator && declarator->kind == cdk_array)
7627 *nelts = declarator->u.array.bounds;
7628 if (*nelts == error_mark_node)
7629 *nelts = integer_one_node;
7631 if (outer_declarator)
7632 outer_declarator->declarator = declarator->declarator;
7633 else
7634 new_declarator = NULL;
7637 return groktypename (&type_specifier_seq, new_declarator, false);
7640 /* Parse an (optional) new-declarator.
7642 new-declarator:
7643 ptr-operator new-declarator [opt]
7644 direct-new-declarator
7646 Returns the declarator. */
7648 static cp_declarator *
7649 cp_parser_new_declarator_opt (cp_parser* parser)
7651 enum tree_code code;
7652 tree type, std_attributes = NULL_TREE;
7653 cp_cv_quals cv_quals;
7655 /* We don't know if there's a ptr-operator next, or not. */
7656 cp_parser_parse_tentatively (parser);
7657 /* Look for a ptr-operator. */
7658 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7659 /* If that worked, look for more new-declarators. */
7660 if (cp_parser_parse_definitely (parser))
7662 cp_declarator *declarator;
7664 /* Parse another optional declarator. */
7665 declarator = cp_parser_new_declarator_opt (parser);
7667 declarator = cp_parser_make_indirect_declarator
7668 (code, type, cv_quals, declarator, std_attributes);
7670 return declarator;
7673 /* If the next token is a `[', there is a direct-new-declarator. */
7674 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7675 return cp_parser_direct_new_declarator (parser);
7677 return NULL;
7680 /* Parse a direct-new-declarator.
7682 direct-new-declarator:
7683 [ expression ]
7684 direct-new-declarator [constant-expression]
7688 static cp_declarator *
7689 cp_parser_direct_new_declarator (cp_parser* parser)
7691 cp_declarator *declarator = NULL;
7693 while (true)
7695 tree expression;
7696 cp_token *token;
7698 /* Look for the opening `['. */
7699 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7701 token = cp_lexer_peek_token (parser->lexer);
7702 expression = cp_parser_expression (parser);
7703 /* The standard requires that the expression have integral
7704 type. DR 74 adds enumeration types. We believe that the
7705 real intent is that these expressions be handled like the
7706 expression in a `switch' condition, which also allows
7707 classes with a single conversion to integral or
7708 enumeration type. */
7709 if (!processing_template_decl)
7711 expression
7712 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7713 expression,
7714 /*complain=*/true);
7715 if (!expression)
7717 error_at (token->location,
7718 "expression in new-declarator must have integral "
7719 "or enumeration type");
7720 expression = error_mark_node;
7724 /* Look for the closing `]'. */
7725 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7727 /* Add this bound to the declarator. */
7728 declarator = make_array_declarator (declarator, expression);
7730 /* If the next token is not a `[', then there are no more
7731 bounds. */
7732 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7733 break;
7736 return declarator;
7739 /* Parse a new-initializer.
7741 new-initializer:
7742 ( expression-list [opt] )
7743 braced-init-list
7745 Returns a representation of the expression-list. */
7747 static vec<tree, va_gc> *
7748 cp_parser_new_initializer (cp_parser* parser)
7750 vec<tree, va_gc> *expression_list;
7752 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7754 tree t;
7755 bool expr_non_constant_p;
7756 cp_lexer_set_source_position (parser->lexer);
7757 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7758 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7759 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7760 expression_list = make_tree_vector_single (t);
7762 else
7763 expression_list = (cp_parser_parenthesized_expression_list
7764 (parser, non_attr, /*cast_p=*/false,
7765 /*allow_expansion_p=*/true,
7766 /*non_constant_p=*/NULL));
7768 return expression_list;
7771 /* Parse a delete-expression.
7773 delete-expression:
7774 :: [opt] delete cast-expression
7775 :: [opt] delete [ ] cast-expression
7777 Returns a representation of the expression. */
7779 static tree
7780 cp_parser_delete_expression (cp_parser* parser)
7782 bool global_scope_p;
7783 bool array_p;
7784 tree expression;
7786 /* Look for the optional `::' operator. */
7787 global_scope_p
7788 = (cp_parser_global_scope_opt (parser,
7789 /*current_scope_valid_p=*/false)
7790 != NULL_TREE);
7791 /* Look for the `delete' keyword. */
7792 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7793 /* See if the array syntax is in use. */
7794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7796 /* Consume the `[' token. */
7797 cp_lexer_consume_token (parser->lexer);
7798 /* Look for the `]' token. */
7799 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7800 /* Remember that this is the `[]' construct. */
7801 array_p = true;
7803 else
7804 array_p = false;
7806 /* Parse the cast-expression. */
7807 expression = cp_parser_simple_cast_expression (parser);
7809 /* A delete-expression may not appear in an integral constant
7810 expression. */
7811 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7812 return error_mark_node;
7814 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7815 tf_warning_or_error);
7818 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7819 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7820 0 otherwise. */
7822 static int
7823 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7825 cp_token *token = cp_lexer_peek_token (parser->lexer);
7826 switch (token->type)
7828 case CPP_COMMA:
7829 case CPP_SEMICOLON:
7830 case CPP_QUERY:
7831 case CPP_COLON:
7832 case CPP_CLOSE_SQUARE:
7833 case CPP_CLOSE_PAREN:
7834 case CPP_CLOSE_BRACE:
7835 case CPP_OPEN_BRACE:
7836 case CPP_DOT:
7837 case CPP_DOT_STAR:
7838 case CPP_DEREF:
7839 case CPP_DEREF_STAR:
7840 case CPP_DIV:
7841 case CPP_MOD:
7842 case CPP_LSHIFT:
7843 case CPP_RSHIFT:
7844 case CPP_LESS:
7845 case CPP_GREATER:
7846 case CPP_LESS_EQ:
7847 case CPP_GREATER_EQ:
7848 case CPP_EQ_EQ:
7849 case CPP_NOT_EQ:
7850 case CPP_EQ:
7851 case CPP_MULT_EQ:
7852 case CPP_DIV_EQ:
7853 case CPP_MOD_EQ:
7854 case CPP_PLUS_EQ:
7855 case CPP_MINUS_EQ:
7856 case CPP_RSHIFT_EQ:
7857 case CPP_LSHIFT_EQ:
7858 case CPP_AND_EQ:
7859 case CPP_XOR_EQ:
7860 case CPP_OR_EQ:
7861 case CPP_XOR:
7862 case CPP_OR:
7863 case CPP_OR_OR:
7864 case CPP_EOF:
7865 case CPP_ELLIPSIS:
7866 return 0;
7868 case CPP_OPEN_PAREN:
7869 /* In ((type ()) () the last () isn't a valid cast-expression,
7870 so the whole must be parsed as postfix-expression. */
7871 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7872 != CPP_CLOSE_PAREN;
7874 case CPP_OPEN_SQUARE:
7875 /* '[' may start a primary-expression in obj-c++ and in C++11,
7876 as a lambda-expression, eg, '(void)[]{}'. */
7877 if (cxx_dialect >= cxx11)
7878 return -1;
7879 return c_dialect_objc ();
7881 case CPP_PLUS_PLUS:
7882 case CPP_MINUS_MINUS:
7883 /* '++' and '--' may or may not start a cast-expression:
7885 struct T { void operator++(int); };
7886 void f() { (T())++; }
7890 int a;
7891 (int)++a; */
7892 return -1;
7894 default:
7895 return 1;
7899 /* Parse a cast-expression.
7901 cast-expression:
7902 unary-expression
7903 ( type-id ) cast-expression
7905 ADDRESS_P is true iff the unary-expression is appearing as the
7906 operand of the `&' operator. CAST_P is true if this expression is
7907 the target of a cast.
7909 Returns a representation of the expression. */
7911 static tree
7912 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7913 bool decltype_p, cp_id_kind * pidk)
7915 /* If it's a `(', then we might be looking at a cast. */
7916 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7918 tree type = NULL_TREE;
7919 tree expr = NULL_TREE;
7920 int cast_expression = 0;
7921 const char *saved_message;
7923 /* There's no way to know yet whether or not this is a cast.
7924 For example, `(int (3))' is a unary-expression, while `(int)
7925 3' is a cast. So, we resort to parsing tentatively. */
7926 cp_parser_parse_tentatively (parser);
7927 /* Types may not be defined in a cast. */
7928 saved_message = parser->type_definition_forbidden_message;
7929 parser->type_definition_forbidden_message
7930 = G_("types may not be defined in casts");
7931 /* Consume the `('. */
7932 cp_lexer_consume_token (parser->lexer);
7933 /* A very tricky bit is that `(struct S) { 3 }' is a
7934 compound-literal (which we permit in C++ as an extension).
7935 But, that construct is not a cast-expression -- it is a
7936 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7937 is legal; if the compound-literal were a cast-expression,
7938 you'd need an extra set of parentheses.) But, if we parse
7939 the type-id, and it happens to be a class-specifier, then we
7940 will commit to the parse at that point, because we cannot
7941 undo the action that is done when creating a new class. So,
7942 then we cannot back up and do a postfix-expression.
7944 Another tricky case is the following (c++/29234):
7946 struct S { void operator () (); };
7948 void foo ()
7950 ( S()() );
7953 As a type-id we parse the parenthesized S()() as a function
7954 returning a function, groktypename complains and we cannot
7955 back up in this case either.
7957 Therefore, we scan ahead to the closing `)', and check to see
7958 if the tokens after the `)' can start a cast-expression. Otherwise
7959 we are dealing with an unary-expression, a postfix-expression
7960 or something else.
7962 Yet another tricky case, in C++11, is the following (c++/54891):
7964 (void)[]{};
7966 The issue is that usually, besides the case of lambda-expressions,
7967 the parenthesized type-id cannot be followed by '[', and, eg, we
7968 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7969 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7970 we don't commit, we try a cast-expression, then an unary-expression.
7972 Save tokens so that we can put them back. */
7973 cp_lexer_save_tokens (parser->lexer);
7975 /* We may be looking at a cast-expression. */
7976 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7977 /*consume_paren=*/true))
7978 cast_expression
7979 = cp_parser_tokens_start_cast_expression (parser);
7981 /* Roll back the tokens we skipped. */
7982 cp_lexer_rollback_tokens (parser->lexer);
7983 /* If we aren't looking at a cast-expression, simulate an error so
7984 that the call to cp_parser_error_occurred below returns true. */
7985 if (!cast_expression)
7986 cp_parser_simulate_error (parser);
7987 else
7989 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7990 parser->in_type_id_in_expr_p = true;
7991 /* Look for the type-id. */
7992 type = cp_parser_type_id (parser);
7993 /* Look for the closing `)'. */
7994 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7995 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7998 /* Restore the saved message. */
7999 parser->type_definition_forbidden_message = saved_message;
8001 /* At this point this can only be either a cast or a
8002 parenthesized ctor such as `(T ())' that looks like a cast to
8003 function returning T. */
8004 if (!cp_parser_error_occurred (parser))
8006 /* Only commit if the cast-expression doesn't start with
8007 '++', '--', or '[' in C++11. */
8008 if (cast_expression > 0)
8009 cp_parser_commit_to_topmost_tentative_parse (parser);
8011 expr = cp_parser_cast_expression (parser,
8012 /*address_p=*/false,
8013 /*cast_p=*/true,
8014 /*decltype_p=*/false,
8015 pidk);
8017 if (cp_parser_parse_definitely (parser))
8019 /* Warn about old-style casts, if so requested. */
8020 if (warn_old_style_cast
8021 && !in_system_header_at (input_location)
8022 && !VOID_TYPE_P (type)
8023 && current_lang_name != lang_name_c)
8024 warning (OPT_Wold_style_cast, "use of old-style cast");
8026 /* Only type conversions to integral or enumeration types
8027 can be used in constant-expressions. */
8028 if (!cast_valid_in_integral_constant_expression_p (type)
8029 && cp_parser_non_integral_constant_expression (parser,
8030 NIC_CAST))
8031 return error_mark_node;
8033 /* Perform the cast. */
8034 expr = build_c_cast (input_location, type, expr);
8035 return expr;
8038 else
8039 cp_parser_abort_tentative_parse (parser);
8042 /* If we get here, then it's not a cast, so it must be a
8043 unary-expression. */
8044 return cp_parser_unary_expression (parser, pidk, address_p,
8045 cast_p, decltype_p);
8048 /* Parse a binary expression of the general form:
8050 pm-expression:
8051 cast-expression
8052 pm-expression .* cast-expression
8053 pm-expression ->* cast-expression
8055 multiplicative-expression:
8056 pm-expression
8057 multiplicative-expression * pm-expression
8058 multiplicative-expression / pm-expression
8059 multiplicative-expression % pm-expression
8061 additive-expression:
8062 multiplicative-expression
8063 additive-expression + multiplicative-expression
8064 additive-expression - multiplicative-expression
8066 shift-expression:
8067 additive-expression
8068 shift-expression << additive-expression
8069 shift-expression >> additive-expression
8071 relational-expression:
8072 shift-expression
8073 relational-expression < shift-expression
8074 relational-expression > shift-expression
8075 relational-expression <= shift-expression
8076 relational-expression >= shift-expression
8078 GNU Extension:
8080 relational-expression:
8081 relational-expression <? shift-expression
8082 relational-expression >? shift-expression
8084 equality-expression:
8085 relational-expression
8086 equality-expression == relational-expression
8087 equality-expression != relational-expression
8089 and-expression:
8090 equality-expression
8091 and-expression & equality-expression
8093 exclusive-or-expression:
8094 and-expression
8095 exclusive-or-expression ^ and-expression
8097 inclusive-or-expression:
8098 exclusive-or-expression
8099 inclusive-or-expression | exclusive-or-expression
8101 logical-and-expression:
8102 inclusive-or-expression
8103 logical-and-expression && inclusive-or-expression
8105 logical-or-expression:
8106 logical-and-expression
8107 logical-or-expression || logical-and-expression
8109 All these are implemented with a single function like:
8111 binary-expression:
8112 simple-cast-expression
8113 binary-expression <token> binary-expression
8115 CAST_P is true if this expression is the target of a cast.
8117 The binops_by_token map is used to get the tree codes for each <token> type.
8118 binary-expressions are associated according to a precedence table. */
8120 #define TOKEN_PRECEDENCE(token) \
8121 (((token->type == CPP_GREATER \
8122 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8123 && !parser->greater_than_is_operator_p) \
8124 ? PREC_NOT_OPERATOR \
8125 : binops_by_token[token->type].prec)
8127 static tree
8128 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8129 bool no_toplevel_fold_p,
8130 bool decltype_p,
8131 enum cp_parser_prec prec,
8132 cp_id_kind * pidk)
8134 cp_parser_expression_stack stack;
8135 cp_parser_expression_stack_entry *sp = &stack[0];
8136 cp_parser_expression_stack_entry current;
8137 tree rhs;
8138 cp_token *token;
8139 enum tree_code rhs_type;
8140 enum cp_parser_prec new_prec, lookahead_prec;
8141 tree overload;
8143 /* Parse the first expression. */
8144 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8145 ? TRUTH_NOT_EXPR : ERROR_MARK);
8146 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8147 cast_p, decltype_p, pidk);
8148 current.prec = prec;
8150 if (cp_parser_error_occurred (parser))
8151 return error_mark_node;
8153 for (;;)
8155 /* Get an operator token. */
8156 token = cp_lexer_peek_token (parser->lexer);
8158 if (warn_cxx11_compat
8159 && token->type == CPP_RSHIFT
8160 && !parser->greater_than_is_operator_p)
8162 if (warning_at (token->location, OPT_Wc__11_compat,
8163 "%<>>%> operator is treated"
8164 " as two right angle brackets in C++11"))
8165 inform (token->location,
8166 "suggest parentheses around %<>>%> expression");
8169 new_prec = TOKEN_PRECEDENCE (token);
8171 /* Popping an entry off the stack means we completed a subexpression:
8172 - either we found a token which is not an operator (`>' where it is not
8173 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8174 will happen repeatedly;
8175 - or, we found an operator which has lower priority. This is the case
8176 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8177 parsing `3 * 4'. */
8178 if (new_prec <= current.prec)
8180 if (sp == stack)
8181 break;
8182 else
8183 goto pop;
8186 get_rhs:
8187 current.tree_type = binops_by_token[token->type].tree_type;
8188 current.loc = token->location;
8190 /* We used the operator token. */
8191 cp_lexer_consume_token (parser->lexer);
8193 /* For "false && x" or "true || x", x will never be executed;
8194 disable warnings while evaluating it. */
8195 if (current.tree_type == TRUTH_ANDIF_EXPR)
8196 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8197 else if (current.tree_type == TRUTH_ORIF_EXPR)
8198 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8200 /* Extract another operand. It may be the RHS of this expression
8201 or the LHS of a new, higher priority expression. */
8202 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8203 ? TRUTH_NOT_EXPR : ERROR_MARK);
8204 rhs = cp_parser_simple_cast_expression (parser);
8206 /* Get another operator token. Look up its precedence to avoid
8207 building a useless (immediately popped) stack entry for common
8208 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8209 token = cp_lexer_peek_token (parser->lexer);
8210 lookahead_prec = TOKEN_PRECEDENCE (token);
8211 if (lookahead_prec > new_prec)
8213 /* ... and prepare to parse the RHS of the new, higher priority
8214 expression. Since precedence levels on the stack are
8215 monotonically increasing, we do not have to care about
8216 stack overflows. */
8217 *sp = current;
8218 ++sp;
8219 current.lhs = rhs;
8220 current.lhs_type = rhs_type;
8221 current.prec = new_prec;
8222 new_prec = lookahead_prec;
8223 goto get_rhs;
8225 pop:
8226 lookahead_prec = new_prec;
8227 /* If the stack is not empty, we have parsed into LHS the right side
8228 (`4' in the example above) of an expression we had suspended.
8229 We can use the information on the stack to recover the LHS (`3')
8230 from the stack together with the tree code (`MULT_EXPR'), and
8231 the precedence of the higher level subexpression
8232 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8233 which will be used to actually build the additive expression. */
8234 rhs = current.lhs;
8235 rhs_type = current.lhs_type;
8236 --sp;
8237 current = *sp;
8240 /* Undo the disabling of warnings done above. */
8241 if (current.tree_type == TRUTH_ANDIF_EXPR)
8242 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8243 else if (current.tree_type == TRUTH_ORIF_EXPR)
8244 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8246 if (warn_logical_not_paren
8247 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8248 && current.lhs_type == TRUTH_NOT_EXPR
8249 /* Avoid warning for !!x == y. */
8250 && (TREE_CODE (current.lhs) != NE_EXPR
8251 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8252 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8253 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8254 /* Avoid warning for !b == y where b is boolean. */
8255 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8256 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8257 != BOOLEAN_TYPE))))
8258 /* Avoid warning for !!b == y where b is boolean. */
8259 && (!DECL_P (current.lhs)
8260 || TREE_TYPE (current.lhs) == NULL_TREE
8261 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8262 warn_logical_not_parentheses (current.loc, current.tree_type,
8263 maybe_constant_value (rhs));
8265 overload = NULL;
8266 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8267 ERROR_MARK for everything that is not a binary expression.
8268 This makes warn_about_parentheses miss some warnings that
8269 involve unary operators. For unary expressions we should
8270 pass the correct tree_code unless the unary expression was
8271 surrounded by parentheses.
8273 if (no_toplevel_fold_p
8274 && lookahead_prec <= current.prec
8275 && sp == stack)
8276 current.lhs = build2 (current.tree_type,
8277 TREE_CODE_CLASS (current.tree_type)
8278 == tcc_comparison
8279 ? boolean_type_node : TREE_TYPE (current.lhs),
8280 current.lhs, rhs);
8281 else
8282 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8283 current.lhs, current.lhs_type,
8284 rhs, rhs_type, &overload,
8285 complain_flags (decltype_p));
8286 current.lhs_type = current.tree_type;
8287 if (EXPR_P (current.lhs))
8288 SET_EXPR_LOCATION (current.lhs, current.loc);
8290 /* If the binary operator required the use of an overloaded operator,
8291 then this expression cannot be an integral constant-expression.
8292 An overloaded operator can be used even if both operands are
8293 otherwise permissible in an integral constant-expression if at
8294 least one of the operands is of enumeration type. */
8296 if (overload
8297 && cp_parser_non_integral_constant_expression (parser,
8298 NIC_OVERLOADED))
8299 return error_mark_node;
8302 return current.lhs;
8305 static tree
8306 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8307 bool no_toplevel_fold_p,
8308 enum cp_parser_prec prec,
8309 cp_id_kind * pidk)
8311 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8312 /*decltype*/false, prec, pidk);
8315 /* Parse the `? expression : assignment-expression' part of a
8316 conditional-expression. The LOGICAL_OR_EXPR is the
8317 logical-or-expression that started the conditional-expression.
8318 Returns a representation of the entire conditional-expression.
8320 This routine is used by cp_parser_assignment_expression.
8322 ? expression : assignment-expression
8324 GNU Extensions:
8326 ? : assignment-expression */
8328 static tree
8329 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8331 tree expr;
8332 tree assignment_expr;
8333 struct cp_token *token;
8334 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8336 /* Consume the `?' token. */
8337 cp_lexer_consume_token (parser->lexer);
8338 token = cp_lexer_peek_token (parser->lexer);
8339 if (cp_parser_allow_gnu_extensions_p (parser)
8340 && token->type == CPP_COLON)
8342 pedwarn (token->location, OPT_Wpedantic,
8343 "ISO C++ does not allow ?: with omitted middle operand");
8344 /* Implicit true clause. */
8345 expr = NULL_TREE;
8346 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8347 warn_for_omitted_condop (token->location, logical_or_expr);
8349 else
8351 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8352 parser->colon_corrects_to_scope_p = false;
8353 /* Parse the expression. */
8354 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8355 expr = cp_parser_expression (parser);
8356 c_inhibit_evaluation_warnings +=
8357 ((logical_or_expr == truthvalue_true_node)
8358 - (logical_or_expr == truthvalue_false_node));
8359 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8362 /* The next token should be a `:'. */
8363 cp_parser_require (parser, CPP_COLON, RT_COLON);
8364 /* Parse the assignment-expression. */
8365 assignment_expr = cp_parser_assignment_expression (parser);
8366 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8368 /* Build the conditional-expression. */
8369 return build_x_conditional_expr (loc, logical_or_expr,
8370 expr,
8371 assignment_expr,
8372 tf_warning_or_error);
8375 /* Parse an assignment-expression.
8377 assignment-expression:
8378 conditional-expression
8379 logical-or-expression assignment-operator assignment_expression
8380 throw-expression
8382 CAST_P is true if this expression is the target of a cast.
8383 DECLTYPE_P is true if this expression is the operand of decltype.
8385 Returns a representation for the expression. */
8387 static tree
8388 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8389 bool cast_p, bool decltype_p)
8391 tree expr;
8393 /* If the next token is the `throw' keyword, then we're looking at
8394 a throw-expression. */
8395 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8396 expr = cp_parser_throw_expression (parser);
8397 /* Otherwise, it must be that we are looking at a
8398 logical-or-expression. */
8399 else
8401 /* Parse the binary expressions (logical-or-expression). */
8402 expr = cp_parser_binary_expression (parser, cast_p, false,
8403 decltype_p,
8404 PREC_NOT_OPERATOR, pidk);
8405 /* If the next token is a `?' then we're actually looking at a
8406 conditional-expression. */
8407 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8408 return cp_parser_question_colon_clause (parser, expr);
8409 else
8411 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8413 /* If it's an assignment-operator, we're using the second
8414 production. */
8415 enum tree_code assignment_operator
8416 = cp_parser_assignment_operator_opt (parser);
8417 if (assignment_operator != ERROR_MARK)
8419 bool non_constant_p;
8420 location_t saved_input_location;
8422 /* Parse the right-hand side of the assignment. */
8423 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8425 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8426 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8428 /* An assignment may not appear in a
8429 constant-expression. */
8430 if (cp_parser_non_integral_constant_expression (parser,
8431 NIC_ASSIGNMENT))
8432 return error_mark_node;
8433 /* Build the assignment expression. Its default
8434 location is the location of the '=' token. */
8435 saved_input_location = input_location;
8436 input_location = loc;
8437 expr = build_x_modify_expr (loc, expr,
8438 assignment_operator,
8439 rhs,
8440 complain_flags (decltype_p));
8441 input_location = saved_input_location;
8446 return expr;
8449 /* Parse an (optional) assignment-operator.
8451 assignment-operator: one of
8452 = *= /= %= += -= >>= <<= &= ^= |=
8454 GNU Extension:
8456 assignment-operator: one of
8457 <?= >?=
8459 If the next token is an assignment operator, the corresponding tree
8460 code is returned, and the token is consumed. For example, for
8461 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8462 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8463 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8464 operator, ERROR_MARK is returned. */
8466 static enum tree_code
8467 cp_parser_assignment_operator_opt (cp_parser* parser)
8469 enum tree_code op;
8470 cp_token *token;
8472 /* Peek at the next token. */
8473 token = cp_lexer_peek_token (parser->lexer);
8475 switch (token->type)
8477 case CPP_EQ:
8478 op = NOP_EXPR;
8479 break;
8481 case CPP_MULT_EQ:
8482 op = MULT_EXPR;
8483 break;
8485 case CPP_DIV_EQ:
8486 op = TRUNC_DIV_EXPR;
8487 break;
8489 case CPP_MOD_EQ:
8490 op = TRUNC_MOD_EXPR;
8491 break;
8493 case CPP_PLUS_EQ:
8494 op = PLUS_EXPR;
8495 break;
8497 case CPP_MINUS_EQ:
8498 op = MINUS_EXPR;
8499 break;
8501 case CPP_RSHIFT_EQ:
8502 op = RSHIFT_EXPR;
8503 break;
8505 case CPP_LSHIFT_EQ:
8506 op = LSHIFT_EXPR;
8507 break;
8509 case CPP_AND_EQ:
8510 op = BIT_AND_EXPR;
8511 break;
8513 case CPP_XOR_EQ:
8514 op = BIT_XOR_EXPR;
8515 break;
8517 case CPP_OR_EQ:
8518 op = BIT_IOR_EXPR;
8519 break;
8521 default:
8522 /* Nothing else is an assignment operator. */
8523 op = ERROR_MARK;
8526 /* If it was an assignment operator, consume it. */
8527 if (op != ERROR_MARK)
8528 cp_lexer_consume_token (parser->lexer);
8530 return op;
8533 /* Parse an expression.
8535 expression:
8536 assignment-expression
8537 expression , assignment-expression
8539 CAST_P is true if this expression is the target of a cast.
8540 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8541 except possibly parenthesized or on the RHS of a comma (N3276).
8543 Returns a representation of the expression. */
8545 static tree
8546 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8547 bool cast_p, bool decltype_p)
8549 tree expression = NULL_TREE;
8550 location_t loc = UNKNOWN_LOCATION;
8552 while (true)
8554 tree assignment_expression;
8556 /* Parse the next assignment-expression. */
8557 assignment_expression
8558 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8560 /* We don't create a temporary for a call that is the immediate operand
8561 of decltype or on the RHS of a comma. But when we see a comma, we
8562 need to create a temporary for a call on the LHS. */
8563 if (decltype_p && !processing_template_decl
8564 && TREE_CODE (assignment_expression) == CALL_EXPR
8565 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8566 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8567 assignment_expression
8568 = build_cplus_new (TREE_TYPE (assignment_expression),
8569 assignment_expression, tf_warning_or_error);
8571 /* If this is the first assignment-expression, we can just
8572 save it away. */
8573 if (!expression)
8574 expression = assignment_expression;
8575 else
8576 expression = build_x_compound_expr (loc, expression,
8577 assignment_expression,
8578 complain_flags (decltype_p));
8579 /* If the next token is not a comma, then we are done with the
8580 expression. */
8581 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8582 break;
8583 /* Consume the `,'. */
8584 loc = cp_lexer_peek_token (parser->lexer)->location;
8585 cp_lexer_consume_token (parser->lexer);
8586 /* A comma operator cannot appear in a constant-expression. */
8587 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8588 expression = error_mark_node;
8591 return expression;
8594 /* Parse a constant-expression.
8596 constant-expression:
8597 conditional-expression
8599 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8600 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8601 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8602 is false, NON_CONSTANT_P should be NULL. */
8604 static tree
8605 cp_parser_constant_expression (cp_parser* parser,
8606 bool allow_non_constant_p,
8607 bool *non_constant_p)
8609 bool saved_integral_constant_expression_p;
8610 bool saved_allow_non_integral_constant_expression_p;
8611 bool saved_non_integral_constant_expression_p;
8612 tree expression;
8614 /* It might seem that we could simply parse the
8615 conditional-expression, and then check to see if it were
8616 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8617 one that the compiler can figure out is constant, possibly after
8618 doing some simplifications or optimizations. The standard has a
8619 precise definition of constant-expression, and we must honor
8620 that, even though it is somewhat more restrictive.
8622 For example:
8624 int i[(2, 3)];
8626 is not a legal declaration, because `(2, 3)' is not a
8627 constant-expression. The `,' operator is forbidden in a
8628 constant-expression. However, GCC's constant-folding machinery
8629 will fold this operation to an INTEGER_CST for `3'. */
8631 /* Save the old settings. */
8632 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8633 saved_allow_non_integral_constant_expression_p
8634 = parser->allow_non_integral_constant_expression_p;
8635 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8636 /* We are now parsing a constant-expression. */
8637 parser->integral_constant_expression_p = true;
8638 parser->allow_non_integral_constant_expression_p
8639 = (allow_non_constant_p || cxx_dialect >= cxx11);
8640 parser->non_integral_constant_expression_p = false;
8641 /* Although the grammar says "conditional-expression", we parse an
8642 "assignment-expression", which also permits "throw-expression"
8643 and the use of assignment operators. In the case that
8644 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8645 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8646 actually essential that we look for an assignment-expression.
8647 For example, cp_parser_initializer_clauses uses this function to
8648 determine whether a particular assignment-expression is in fact
8649 constant. */
8650 expression = cp_parser_assignment_expression (parser);
8651 /* Restore the old settings. */
8652 parser->integral_constant_expression_p
8653 = saved_integral_constant_expression_p;
8654 parser->allow_non_integral_constant_expression_p
8655 = saved_allow_non_integral_constant_expression_p;
8656 if (cxx_dialect >= cxx11)
8658 /* Require an rvalue constant expression here; that's what our
8659 callers expect. Reference constant expressions are handled
8660 separately in e.g. cp_parser_template_argument. */
8661 bool is_const = potential_rvalue_constant_expression (expression);
8662 parser->non_integral_constant_expression_p = !is_const;
8663 if (!is_const && !allow_non_constant_p)
8664 require_potential_rvalue_constant_expression (expression);
8666 if (allow_non_constant_p)
8667 *non_constant_p = parser->non_integral_constant_expression_p;
8668 parser->non_integral_constant_expression_p
8669 = saved_non_integral_constant_expression_p;
8671 return expression;
8674 /* Parse __builtin_offsetof.
8676 offsetof-expression:
8677 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8679 offsetof-member-designator:
8680 id-expression
8681 | offsetof-member-designator "." id-expression
8682 | offsetof-member-designator "[" expression "]"
8683 | offsetof-member-designator "->" id-expression */
8685 static tree
8686 cp_parser_builtin_offsetof (cp_parser *parser)
8688 int save_ice_p, save_non_ice_p;
8689 tree type, expr;
8690 cp_id_kind dummy;
8691 cp_token *token;
8693 /* We're about to accept non-integral-constant things, but will
8694 definitely yield an integral constant expression. Save and
8695 restore these values around our local parsing. */
8696 save_ice_p = parser->integral_constant_expression_p;
8697 save_non_ice_p = parser->non_integral_constant_expression_p;
8699 /* Consume the "__builtin_offsetof" token. */
8700 cp_lexer_consume_token (parser->lexer);
8701 /* Consume the opening `('. */
8702 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8703 /* Parse the type-id. */
8704 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8705 type = cp_parser_type_id (parser);
8706 /* Look for the `,'. */
8707 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8708 token = cp_lexer_peek_token (parser->lexer);
8710 /* Build the (type *)null that begins the traditional offsetof macro. */
8711 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8712 tf_warning_or_error);
8714 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8715 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8716 true, &dummy, token->location);
8717 while (true)
8719 token = cp_lexer_peek_token (parser->lexer);
8720 switch (token->type)
8722 case CPP_OPEN_SQUARE:
8723 /* offsetof-member-designator "[" expression "]" */
8724 expr = cp_parser_postfix_open_square_expression (parser, expr,
8725 true, false);
8726 break;
8728 case CPP_DEREF:
8729 /* offsetof-member-designator "->" identifier */
8730 expr = grok_array_decl (token->location, expr,
8731 integer_zero_node, false);
8732 /* FALLTHRU */
8734 case CPP_DOT:
8735 /* offsetof-member-designator "." identifier */
8736 cp_lexer_consume_token (parser->lexer);
8737 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8738 expr, true, &dummy,
8739 token->location);
8740 break;
8742 case CPP_CLOSE_PAREN:
8743 /* Consume the ")" token. */
8744 cp_lexer_consume_token (parser->lexer);
8745 goto success;
8747 default:
8748 /* Error. We know the following require will fail, but
8749 that gives the proper error message. */
8750 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8751 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8752 expr = error_mark_node;
8753 goto failure;
8757 success:
8758 expr = finish_offsetof (expr, loc);
8760 failure:
8761 parser->integral_constant_expression_p = save_ice_p;
8762 parser->non_integral_constant_expression_p = save_non_ice_p;
8764 return expr;
8767 /* Parse a trait expression.
8769 Returns a representation of the expression, the underlying type
8770 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8772 static tree
8773 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8775 cp_trait_kind kind;
8776 tree type1, type2 = NULL_TREE;
8777 bool binary = false;
8778 bool variadic = false;
8780 switch (keyword)
8782 case RID_HAS_NOTHROW_ASSIGN:
8783 kind = CPTK_HAS_NOTHROW_ASSIGN;
8784 break;
8785 case RID_HAS_NOTHROW_CONSTRUCTOR:
8786 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8787 break;
8788 case RID_HAS_NOTHROW_COPY:
8789 kind = CPTK_HAS_NOTHROW_COPY;
8790 break;
8791 case RID_HAS_TRIVIAL_ASSIGN:
8792 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8793 break;
8794 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8795 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8796 break;
8797 case RID_HAS_TRIVIAL_COPY:
8798 kind = CPTK_HAS_TRIVIAL_COPY;
8799 break;
8800 case RID_HAS_TRIVIAL_DESTRUCTOR:
8801 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8802 break;
8803 case RID_HAS_VIRTUAL_DESTRUCTOR:
8804 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8805 break;
8806 case RID_IS_ABSTRACT:
8807 kind = CPTK_IS_ABSTRACT;
8808 break;
8809 case RID_IS_BASE_OF:
8810 kind = CPTK_IS_BASE_OF;
8811 binary = true;
8812 break;
8813 case RID_IS_CLASS:
8814 kind = CPTK_IS_CLASS;
8815 break;
8816 case RID_IS_EMPTY:
8817 kind = CPTK_IS_EMPTY;
8818 break;
8819 case RID_IS_ENUM:
8820 kind = CPTK_IS_ENUM;
8821 break;
8822 case RID_IS_FINAL:
8823 kind = CPTK_IS_FINAL;
8824 break;
8825 case RID_IS_LITERAL_TYPE:
8826 kind = CPTK_IS_LITERAL_TYPE;
8827 break;
8828 case RID_IS_POD:
8829 kind = CPTK_IS_POD;
8830 break;
8831 case RID_IS_POLYMORPHIC:
8832 kind = CPTK_IS_POLYMORPHIC;
8833 break;
8834 case RID_IS_STD_LAYOUT:
8835 kind = CPTK_IS_STD_LAYOUT;
8836 break;
8837 case RID_IS_TRIVIAL:
8838 kind = CPTK_IS_TRIVIAL;
8839 break;
8840 case RID_IS_TRIVIALLY_ASSIGNABLE:
8841 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8842 binary = true;
8843 break;
8844 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8845 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8846 variadic = true;
8847 break;
8848 case RID_IS_TRIVIALLY_COPYABLE:
8849 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8850 break;
8851 case RID_IS_UNION:
8852 kind = CPTK_IS_UNION;
8853 break;
8854 case RID_UNDERLYING_TYPE:
8855 kind = CPTK_UNDERLYING_TYPE;
8856 break;
8857 case RID_BASES:
8858 kind = CPTK_BASES;
8859 break;
8860 case RID_DIRECT_BASES:
8861 kind = CPTK_DIRECT_BASES;
8862 break;
8863 default:
8864 gcc_unreachable ();
8867 /* Consume the token. */
8868 cp_lexer_consume_token (parser->lexer);
8870 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8872 type1 = cp_parser_type_id (parser);
8874 if (type1 == error_mark_node)
8875 return error_mark_node;
8877 if (binary)
8879 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8881 type2 = cp_parser_type_id (parser);
8883 if (type2 == error_mark_node)
8884 return error_mark_node;
8886 else if (variadic)
8888 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8890 cp_lexer_consume_token (parser->lexer);
8891 tree elt = cp_parser_type_id (parser);
8892 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8894 cp_lexer_consume_token (parser->lexer);
8895 elt = make_pack_expansion (elt);
8897 if (elt == error_mark_node)
8898 return error_mark_node;
8899 type2 = tree_cons (NULL_TREE, elt, type2);
8903 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8905 /* Complete the trait expression, which may mean either processing
8906 the trait expr now or saving it for template instantiation. */
8907 switch(kind)
8909 case CPTK_UNDERLYING_TYPE:
8910 return finish_underlying_type (type1);
8911 case CPTK_BASES:
8912 return finish_bases (type1, false);
8913 case CPTK_DIRECT_BASES:
8914 return finish_bases (type1, true);
8915 default:
8916 return finish_trait_expr (kind, type1, type2);
8920 /* Lambdas that appear in variable initializer or default argument scope
8921 get that in their mangling, so we need to record it. We might as well
8922 use the count for function and namespace scopes as well. */
8923 static GTY(()) tree lambda_scope;
8924 static GTY(()) int lambda_count;
8925 typedef struct GTY(()) tree_int
8927 tree t;
8928 int i;
8929 } tree_int;
8930 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8932 static void
8933 start_lambda_scope (tree decl)
8935 tree_int ti;
8936 gcc_assert (decl);
8937 /* Once we're inside a function, we ignore other scopes and just push
8938 the function again so that popping works properly. */
8939 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8940 decl = current_function_decl;
8941 ti.t = lambda_scope;
8942 ti.i = lambda_count;
8943 vec_safe_push (lambda_scope_stack, ti);
8944 if (lambda_scope != decl)
8946 /* Don't reset the count if we're still in the same function. */
8947 lambda_scope = decl;
8948 lambda_count = 0;
8952 static void
8953 record_lambda_scope (tree lambda)
8955 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8956 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8959 static void
8960 finish_lambda_scope (void)
8962 tree_int *p = &lambda_scope_stack->last ();
8963 if (lambda_scope != p->t)
8965 lambda_scope = p->t;
8966 lambda_count = p->i;
8968 lambda_scope_stack->pop ();
8971 /* Parse a lambda expression.
8973 lambda-expression:
8974 lambda-introducer lambda-declarator [opt] compound-statement
8976 Returns a representation of the expression. */
8978 static tree
8979 cp_parser_lambda_expression (cp_parser* parser)
8981 tree lambda_expr = build_lambda_expr ();
8982 tree type;
8983 bool ok = true;
8984 cp_token *token = cp_lexer_peek_token (parser->lexer);
8985 cp_token_position start = 0;
8987 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8989 if (cp_unevaluated_operand)
8991 if (!token->error_reported)
8993 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8994 "lambda-expression in unevaluated context");
8995 token->error_reported = true;
8997 ok = false;
8999 else if (parser->in_template_argument_list_p)
9001 if (!token->error_reported)
9003 error_at (token->location, "lambda-expression in template-argument");
9004 token->error_reported = true;
9006 ok = false;
9009 /* We may be in the middle of deferred access check. Disable
9010 it now. */
9011 push_deferring_access_checks (dk_no_deferred);
9013 cp_parser_lambda_introducer (parser, lambda_expr);
9015 type = begin_lambda_type (lambda_expr);
9016 if (type == error_mark_node)
9017 return error_mark_node;
9019 record_lambda_scope (lambda_expr);
9021 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9022 determine_visibility (TYPE_NAME (type));
9024 /* Now that we've started the type, add the capture fields for any
9025 explicit captures. */
9026 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9029 /* Inside the class, surrounding template-parameter-lists do not apply. */
9030 unsigned int saved_num_template_parameter_lists
9031 = parser->num_template_parameter_lists;
9032 unsigned char in_statement = parser->in_statement;
9033 bool in_switch_statement_p = parser->in_switch_statement_p;
9034 bool fully_implicit_function_template_p
9035 = parser->fully_implicit_function_template_p;
9036 tree implicit_template_parms = parser->implicit_template_parms;
9037 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9038 bool auto_is_implicit_function_template_parm_p
9039 = parser->auto_is_implicit_function_template_parm_p;
9041 parser->num_template_parameter_lists = 0;
9042 parser->in_statement = 0;
9043 parser->in_switch_statement_p = false;
9044 parser->fully_implicit_function_template_p = false;
9045 parser->implicit_template_parms = 0;
9046 parser->implicit_template_scope = 0;
9047 parser->auto_is_implicit_function_template_parm_p = false;
9049 /* By virtue of defining a local class, a lambda expression has access to
9050 the private variables of enclosing classes. */
9052 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9054 if (ok)
9056 if (!cp_parser_error_occurred (parser)
9057 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9058 && cp_parser_start_tentative_firewall (parser))
9059 start = token;
9060 cp_parser_lambda_body (parser, lambda_expr);
9062 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9064 if (cp_parser_skip_to_closing_brace (parser))
9065 cp_lexer_consume_token (parser->lexer);
9068 /* The capture list was built up in reverse order; fix that now. */
9069 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9070 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9072 if (ok)
9073 maybe_add_lambda_conv_op (type);
9075 type = finish_struct (type, /*attributes=*/NULL_TREE);
9077 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9078 parser->in_statement = in_statement;
9079 parser->in_switch_statement_p = in_switch_statement_p;
9080 parser->fully_implicit_function_template_p
9081 = fully_implicit_function_template_p;
9082 parser->implicit_template_parms = implicit_template_parms;
9083 parser->implicit_template_scope = implicit_template_scope;
9084 parser->auto_is_implicit_function_template_parm_p
9085 = auto_is_implicit_function_template_parm_p;
9088 pop_deferring_access_checks ();
9090 /* This field is only used during parsing of the lambda. */
9091 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9093 /* This lambda shouldn't have any proxies left at this point. */
9094 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9095 /* And now that we're done, push proxies for an enclosing lambda. */
9096 insert_pending_capture_proxies ();
9098 if (ok)
9099 lambda_expr = build_lambda_object (lambda_expr);
9100 else
9101 lambda_expr = error_mark_node;
9103 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9105 return lambda_expr;
9108 /* Parse the beginning of a lambda expression.
9110 lambda-introducer:
9111 [ lambda-capture [opt] ]
9113 LAMBDA_EXPR is the current representation of the lambda expression. */
9115 static void
9116 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9118 /* Need commas after the first capture. */
9119 bool first = true;
9121 /* Eat the leading `['. */
9122 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9124 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9125 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9126 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9127 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9128 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9129 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9131 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9133 cp_lexer_consume_token (parser->lexer);
9134 first = false;
9137 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9139 cp_token* capture_token;
9140 tree capture_id;
9141 tree capture_init_expr;
9142 cp_id_kind idk = CP_ID_KIND_NONE;
9143 bool explicit_init_p = false;
9145 enum capture_kind_type
9147 BY_COPY,
9148 BY_REFERENCE
9150 enum capture_kind_type capture_kind = BY_COPY;
9152 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9154 error ("expected end of capture-list");
9155 return;
9158 if (first)
9159 first = false;
9160 else
9161 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9163 /* Possibly capture `this'. */
9164 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9166 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9167 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9168 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9169 "with by-copy capture default");
9170 cp_lexer_consume_token (parser->lexer);
9171 add_capture (lambda_expr,
9172 /*id=*/this_identifier,
9173 /*initializer=*/finish_this_expr(),
9174 /*by_reference_p=*/false,
9175 explicit_init_p);
9176 continue;
9179 /* Remember whether we want to capture as a reference or not. */
9180 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9182 capture_kind = BY_REFERENCE;
9183 cp_lexer_consume_token (parser->lexer);
9186 /* Get the identifier. */
9187 capture_token = cp_lexer_peek_token (parser->lexer);
9188 capture_id = cp_parser_identifier (parser);
9190 if (capture_id == error_mark_node)
9191 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9192 delimiters, but I modified this to stop on unnested ']' as well. It
9193 was already changed to stop on unnested '}', so the
9194 "closing_parenthesis" name is no more misleading with my change. */
9196 cp_parser_skip_to_closing_parenthesis (parser,
9197 /*recovering=*/true,
9198 /*or_comma=*/true,
9199 /*consume_paren=*/true);
9200 break;
9203 /* Find the initializer for this capture. */
9204 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9205 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9206 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9208 bool direct, non_constant;
9209 /* An explicit initializer exists. */
9210 if (cxx_dialect < cxx14)
9211 pedwarn (input_location, 0,
9212 "lambda capture initializers "
9213 "only available with -std=c++14 or -std=gnu++14");
9214 capture_init_expr = cp_parser_initializer (parser, &direct,
9215 &non_constant);
9216 explicit_init_p = true;
9217 if (capture_init_expr == NULL_TREE)
9219 error ("empty initializer for lambda init-capture");
9220 capture_init_expr = error_mark_node;
9223 else
9225 const char* error_msg;
9227 /* Turn the identifier into an id-expression. */
9228 capture_init_expr
9229 = cp_parser_lookup_name_simple (parser, capture_id,
9230 capture_token->location);
9232 if (capture_init_expr == error_mark_node)
9234 unqualified_name_lookup_error (capture_id);
9235 continue;
9237 else if (DECL_P (capture_init_expr)
9238 && (!VAR_P (capture_init_expr)
9239 && TREE_CODE (capture_init_expr) != PARM_DECL))
9241 error_at (capture_token->location,
9242 "capture of non-variable %qD ",
9243 capture_init_expr);
9244 inform (0, "%q+#D declared here", capture_init_expr);
9245 continue;
9247 if (VAR_P (capture_init_expr)
9248 && decl_storage_duration (capture_init_expr) != dk_auto)
9250 if (pedwarn (capture_token->location, 0, "capture of variable "
9251 "%qD with non-automatic storage duration",
9252 capture_init_expr))
9253 inform (0, "%q+#D declared here", capture_init_expr);
9254 continue;
9257 capture_init_expr
9258 = finish_id_expression
9259 (capture_id,
9260 capture_init_expr,
9261 parser->scope,
9262 &idk,
9263 /*integral_constant_expression_p=*/false,
9264 /*allow_non_integral_constant_expression_p=*/false,
9265 /*non_integral_constant_expression_p=*/NULL,
9266 /*template_p=*/false,
9267 /*done=*/true,
9268 /*address_p=*/false,
9269 /*template_arg_p=*/false,
9270 &error_msg,
9271 capture_token->location);
9273 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9275 cp_lexer_consume_token (parser->lexer);
9276 capture_init_expr = make_pack_expansion (capture_init_expr);
9278 else
9279 check_for_bare_parameter_packs (capture_init_expr);
9282 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9283 && !explicit_init_p)
9285 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9286 && capture_kind == BY_COPY)
9287 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9288 "of %qD redundant with by-copy capture default",
9289 capture_id);
9290 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9291 && capture_kind == BY_REFERENCE)
9292 pedwarn (capture_token->location, 0, "explicit by-reference "
9293 "capture of %qD redundant with by-reference capture "
9294 "default", capture_id);
9297 add_capture (lambda_expr,
9298 capture_id,
9299 capture_init_expr,
9300 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9301 explicit_init_p);
9304 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9307 /* Parse the (optional) middle of a lambda expression.
9309 lambda-declarator:
9310 < template-parameter-list [opt] >
9311 ( parameter-declaration-clause [opt] )
9312 attribute-specifier [opt]
9313 mutable [opt]
9314 exception-specification [opt]
9315 lambda-return-type-clause [opt]
9317 LAMBDA_EXPR is the current representation of the lambda expression. */
9319 static bool
9320 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9322 /* 5.1.1.4 of the standard says:
9323 If a lambda-expression does not include a lambda-declarator, it is as if
9324 the lambda-declarator were ().
9325 This means an empty parameter list, no attributes, and no exception
9326 specification. */
9327 tree param_list = void_list_node;
9328 tree attributes = NULL_TREE;
9329 tree exception_spec = NULL_TREE;
9330 tree template_param_list = NULL_TREE;
9332 /* The template-parameter-list is optional, but must begin with
9333 an opening angle if present. */
9334 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9336 if (cxx_dialect < cxx14)
9337 pedwarn (parser->lexer->next_token->location, 0,
9338 "lambda templates are only available with "
9339 "-std=c++14 or -std=gnu++14");
9341 cp_lexer_consume_token (parser->lexer);
9343 template_param_list = cp_parser_template_parameter_list (parser);
9345 cp_parser_skip_to_end_of_template_parameter_list (parser);
9347 /* We just processed one more parameter list. */
9348 ++parser->num_template_parameter_lists;
9351 /* The parameter-declaration-clause is optional (unless
9352 template-parameter-list was given), but must begin with an
9353 opening parenthesis if present. */
9354 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9356 cp_lexer_consume_token (parser->lexer);
9358 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9360 /* Parse parameters. */
9361 param_list = cp_parser_parameter_declaration_clause (parser);
9363 /* Default arguments shall not be specified in the
9364 parameter-declaration-clause of a lambda-declarator. */
9365 for (tree t = param_list; t; t = TREE_CHAIN (t))
9366 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9367 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9368 "default argument specified for lambda parameter");
9370 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9372 attributes = cp_parser_attributes_opt (parser);
9374 /* Parse optional `mutable' keyword. */
9375 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9377 cp_lexer_consume_token (parser->lexer);
9378 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9381 /* Parse optional exception specification. */
9382 exception_spec = cp_parser_exception_specification_opt (parser);
9384 /* Parse optional trailing return type. */
9385 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9387 cp_lexer_consume_token (parser->lexer);
9388 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9389 = cp_parser_trailing_type_id (parser);
9392 /* The function parameters must be in scope all the way until after the
9393 trailing-return-type in case of decltype. */
9394 pop_bindings_and_leave_scope ();
9396 else if (template_param_list != NULL_TREE) // generate diagnostic
9397 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9399 /* Create the function call operator.
9401 Messing with declarators like this is no uglier than building up the
9402 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9403 other code. */
9405 cp_decl_specifier_seq return_type_specs;
9406 cp_declarator* declarator;
9407 tree fco;
9408 int quals;
9409 void *p;
9411 clear_decl_specs (&return_type_specs);
9412 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9413 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9414 else
9415 /* Maybe we will deduce the return type later. */
9416 return_type_specs.type = make_auto ();
9418 p = obstack_alloc (&declarator_obstack, 0);
9420 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9421 sfk_none);
9423 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9424 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9425 declarator = make_call_declarator (declarator, param_list, quals,
9426 VIRT_SPEC_UNSPECIFIED,
9427 REF_QUAL_NONE,
9428 exception_spec,
9429 /*late_return_type=*/NULL_TREE);
9430 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9432 fco = grokmethod (&return_type_specs,
9433 declarator,
9434 attributes);
9435 if (fco != error_mark_node)
9437 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9438 DECL_ARTIFICIAL (fco) = 1;
9439 /* Give the object parameter a different name. */
9440 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9441 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9442 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9444 if (template_param_list)
9446 fco = finish_member_template_decl (fco);
9447 finish_template_decl (template_param_list);
9448 --parser->num_template_parameter_lists;
9450 else if (parser->fully_implicit_function_template_p)
9451 fco = finish_fully_implicit_template (parser, fco);
9453 finish_member_declaration (fco);
9455 obstack_free (&declarator_obstack, p);
9457 return (fco != error_mark_node);
9461 /* Parse the body of a lambda expression, which is simply
9463 compound-statement
9465 but which requires special handling.
9466 LAMBDA_EXPR is the current representation of the lambda expression. */
9468 static void
9469 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9471 bool nested = (current_function_decl != NULL_TREE);
9472 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9473 if (nested)
9474 push_function_context ();
9475 else
9476 /* Still increment function_depth so that we don't GC in the
9477 middle of an expression. */
9478 ++function_depth;
9479 vec<tree> omp_privatization_save;
9480 save_omp_privatization_clauses (omp_privatization_save);
9481 /* Clear this in case we're in the middle of a default argument. */
9482 parser->local_variables_forbidden_p = false;
9484 /* Finish the function call operator
9485 - class_specifier
9486 + late_parsing_for_member
9487 + function_definition_after_declarator
9488 + ctor_initializer_opt_and_function_body */
9490 tree fco = lambda_function (lambda_expr);
9491 tree body;
9492 bool done = false;
9493 tree compound_stmt;
9494 tree cap;
9496 /* Let the front end know that we are going to be defining this
9497 function. */
9498 start_preparsed_function (fco,
9499 NULL_TREE,
9500 SF_PRE_PARSED | SF_INCLASS_INLINE);
9502 start_lambda_scope (fco);
9503 body = begin_function_body ();
9505 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9506 goto out;
9508 /* Push the proxies for any explicit captures. */
9509 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9510 cap = TREE_CHAIN (cap))
9511 build_capture_proxy (TREE_PURPOSE (cap));
9513 compound_stmt = begin_compound_stmt (0);
9515 /* 5.1.1.4 of the standard says:
9516 If a lambda-expression does not include a trailing-return-type, it
9517 is as if the trailing-return-type denotes the following type:
9518 * if the compound-statement is of the form
9519 { return attribute-specifier [opt] expression ; }
9520 the type of the returned expression after lvalue-to-rvalue
9521 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9522 (_conv.array_ 4.2), and function-to-pointer conversion
9523 (_conv.func_ 4.3);
9524 * otherwise, void. */
9526 /* In a lambda that has neither a lambda-return-type-clause
9527 nor a deducible form, errors should be reported for return statements
9528 in the body. Since we used void as the placeholder return type, parsing
9529 the body as usual will give such desired behavior. */
9530 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9531 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9532 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9534 tree expr = NULL_TREE;
9535 cp_id_kind idk = CP_ID_KIND_NONE;
9537 /* Parse tentatively in case there's more after the initial return
9538 statement. */
9539 cp_parser_parse_tentatively (parser);
9541 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9543 expr = cp_parser_expression (parser, &idk);
9545 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9546 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9548 if (cp_parser_parse_definitely (parser))
9550 if (!processing_template_decl)
9551 apply_deduced_return_type (fco, lambda_return_type (expr));
9553 /* Will get error here if type not deduced yet. */
9554 finish_return_stmt (expr);
9556 done = true;
9560 if (!done)
9562 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9563 cp_parser_label_declaration (parser);
9564 cp_parser_statement_seq_opt (parser, NULL_TREE);
9565 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9568 finish_compound_stmt (compound_stmt);
9570 out:
9571 finish_function_body (body);
9572 finish_lambda_scope ();
9574 /* Finish the function and generate code for it if necessary. */
9575 tree fn = finish_function (/*inline*/2);
9577 /* Only expand if the call op is not a template. */
9578 if (!DECL_TEMPLATE_INFO (fco))
9579 expand_or_defer_fn (fn);
9582 restore_omp_privatization_clauses (omp_privatization_save);
9583 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9584 if (nested)
9585 pop_function_context();
9586 else
9587 --function_depth;
9590 /* Statements [gram.stmt.stmt] */
9592 /* Parse a statement.
9594 statement:
9595 labeled-statement
9596 expression-statement
9597 compound-statement
9598 selection-statement
9599 iteration-statement
9600 jump-statement
9601 declaration-statement
9602 try-block
9604 C++11:
9606 statement:
9607 labeled-statement
9608 attribute-specifier-seq (opt) expression-statement
9609 attribute-specifier-seq (opt) compound-statement
9610 attribute-specifier-seq (opt) selection-statement
9611 attribute-specifier-seq (opt) iteration-statement
9612 attribute-specifier-seq (opt) jump-statement
9613 declaration-statement
9614 attribute-specifier-seq (opt) try-block
9616 TM Extension:
9618 statement:
9619 atomic-statement
9621 IN_COMPOUND is true when the statement is nested inside a
9622 cp_parser_compound_statement; this matters for certain pragmas.
9624 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9625 is a (possibly labeled) if statement which is not enclosed in braces
9626 and has an else clause. This is used to implement -Wparentheses. */
9628 static void
9629 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9630 bool in_compound, bool *if_p)
9632 tree statement, std_attrs = NULL_TREE;
9633 cp_token *token;
9634 location_t statement_location, attrs_location;
9636 restart:
9637 if (if_p != NULL)
9638 *if_p = false;
9639 /* There is no statement yet. */
9640 statement = NULL_TREE;
9642 saved_token_sentinel saved_tokens (parser->lexer);
9643 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9644 if (c_dialect_objc ())
9645 /* In obj-c++, seeing '[[' might be the either the beginning of
9646 c++11 attributes, or a nested objc-message-expression. So
9647 let's parse the c++11 attributes tentatively. */
9648 cp_parser_parse_tentatively (parser);
9649 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9650 if (c_dialect_objc ())
9652 if (!cp_parser_parse_definitely (parser))
9653 std_attrs = NULL_TREE;
9656 /* Peek at the next token. */
9657 token = cp_lexer_peek_token (parser->lexer);
9658 /* Remember the location of the first token in the statement. */
9659 statement_location = token->location;
9660 /* If this is a keyword, then that will often determine what kind of
9661 statement we have. */
9662 if (token->type == CPP_KEYWORD)
9664 enum rid keyword = token->keyword;
9666 switch (keyword)
9668 case RID_CASE:
9669 case RID_DEFAULT:
9670 /* Looks like a labeled-statement with a case label.
9671 Parse the label, and then use tail recursion to parse
9672 the statement. */
9673 cp_parser_label_for_labeled_statement (parser, std_attrs);
9674 goto restart;
9676 case RID_IF:
9677 case RID_SWITCH:
9678 statement = cp_parser_selection_statement (parser, if_p);
9679 break;
9681 case RID_WHILE:
9682 case RID_DO:
9683 case RID_FOR:
9684 statement = cp_parser_iteration_statement (parser, false);
9685 break;
9687 case RID_CILK_FOR:
9688 if (!flag_cilkplus)
9690 error_at (cp_lexer_peek_token (parser->lexer)->location,
9691 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9692 cp_lexer_consume_token (parser->lexer);
9693 statement = error_mark_node;
9695 else
9696 statement = cp_parser_cilk_for (parser, integer_zero_node);
9697 break;
9699 case RID_BREAK:
9700 case RID_CONTINUE:
9701 case RID_RETURN:
9702 case RID_GOTO:
9703 statement = cp_parser_jump_statement (parser);
9704 break;
9706 case RID_CILK_SYNC:
9707 cp_lexer_consume_token (parser->lexer);
9708 if (flag_cilkplus)
9710 tree sync_expr = build_cilk_sync ();
9711 SET_EXPR_LOCATION (sync_expr,
9712 token->location);
9713 statement = finish_expr_stmt (sync_expr);
9715 else
9717 error_at (token->location, "-fcilkplus must be enabled to use"
9718 " %<_Cilk_sync%>");
9719 statement = error_mark_node;
9721 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9722 break;
9724 /* Objective-C++ exception-handling constructs. */
9725 case RID_AT_TRY:
9726 case RID_AT_CATCH:
9727 case RID_AT_FINALLY:
9728 case RID_AT_SYNCHRONIZED:
9729 case RID_AT_THROW:
9730 statement = cp_parser_objc_statement (parser);
9731 break;
9733 case RID_TRY:
9734 statement = cp_parser_try_block (parser);
9735 break;
9737 case RID_NAMESPACE:
9738 /* This must be a namespace alias definition. */
9739 cp_parser_declaration_statement (parser);
9740 return;
9742 case RID_TRANSACTION_ATOMIC:
9743 case RID_TRANSACTION_RELAXED:
9744 statement = cp_parser_transaction (parser, keyword);
9745 break;
9746 case RID_TRANSACTION_CANCEL:
9747 statement = cp_parser_transaction_cancel (parser);
9748 break;
9750 default:
9751 /* It might be a keyword like `int' that can start a
9752 declaration-statement. */
9753 break;
9756 else if (token->type == CPP_NAME)
9758 /* If the next token is a `:', then we are looking at a
9759 labeled-statement. */
9760 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9761 if (token->type == CPP_COLON)
9763 /* Looks like a labeled-statement with an ordinary label.
9764 Parse the label, and then use tail recursion to parse
9765 the statement. */
9767 cp_parser_label_for_labeled_statement (parser, std_attrs);
9768 goto restart;
9771 /* Anything that starts with a `{' must be a compound-statement. */
9772 else if (token->type == CPP_OPEN_BRACE)
9773 statement = cp_parser_compound_statement (parser, NULL, false, false);
9774 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9775 a statement all its own. */
9776 else if (token->type == CPP_PRAGMA)
9778 /* Only certain OpenMP pragmas are attached to statements, and thus
9779 are considered statements themselves. All others are not. In
9780 the context of a compound, accept the pragma as a "statement" and
9781 return so that we can check for a close brace. Otherwise we
9782 require a real statement and must go back and read one. */
9783 if (in_compound)
9784 cp_parser_pragma (parser, pragma_compound);
9785 else if (!cp_parser_pragma (parser, pragma_stmt))
9786 goto restart;
9787 return;
9789 else if (token->type == CPP_EOF)
9791 cp_parser_error (parser, "expected statement");
9792 return;
9795 /* Everything else must be a declaration-statement or an
9796 expression-statement. Try for the declaration-statement
9797 first, unless we are looking at a `;', in which case we know that
9798 we have an expression-statement. */
9799 if (!statement)
9801 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9803 if (std_attrs != NULL_TREE)
9805 /* Attributes should be parsed as part of the the
9806 declaration, so let's un-parse them. */
9807 saved_tokens.rollback();
9808 std_attrs = NULL_TREE;
9811 cp_parser_parse_tentatively (parser);
9812 /* Try to parse the declaration-statement. */
9813 cp_parser_declaration_statement (parser);
9814 /* If that worked, we're done. */
9815 if (cp_parser_parse_definitely (parser))
9816 return;
9818 /* Look for an expression-statement instead. */
9819 statement = cp_parser_expression_statement (parser, in_statement_expr);
9822 /* Set the line number for the statement. */
9823 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9824 SET_EXPR_LOCATION (statement, statement_location);
9826 /* Note that for now, we don't do anything with c++11 statements
9827 parsed at this level. */
9828 if (std_attrs != NULL_TREE)
9829 warning_at (attrs_location,
9830 OPT_Wattributes,
9831 "attributes at the beginning of statement are ignored");
9834 /* Parse the label for a labeled-statement, i.e.
9836 identifier :
9837 case constant-expression :
9838 default :
9840 GNU Extension:
9841 case constant-expression ... constant-expression : statement
9843 When a label is parsed without errors, the label is added to the
9844 parse tree by the finish_* functions, so this function doesn't
9845 have to return the label. */
9847 static void
9848 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9850 cp_token *token;
9851 tree label = NULL_TREE;
9852 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9854 /* The next token should be an identifier. */
9855 token = cp_lexer_peek_token (parser->lexer);
9856 if (token->type != CPP_NAME
9857 && token->type != CPP_KEYWORD)
9859 cp_parser_error (parser, "expected labeled-statement");
9860 return;
9863 parser->colon_corrects_to_scope_p = false;
9864 switch (token->keyword)
9866 case RID_CASE:
9868 tree expr, expr_hi;
9869 cp_token *ellipsis;
9871 /* Consume the `case' token. */
9872 cp_lexer_consume_token (parser->lexer);
9873 /* Parse the constant-expression. */
9874 expr = cp_parser_constant_expression (parser);
9875 if (check_for_bare_parameter_packs (expr))
9876 expr = error_mark_node;
9878 ellipsis = cp_lexer_peek_token (parser->lexer);
9879 if (ellipsis->type == CPP_ELLIPSIS)
9881 /* Consume the `...' token. */
9882 cp_lexer_consume_token (parser->lexer);
9883 expr_hi = cp_parser_constant_expression (parser);
9884 if (check_for_bare_parameter_packs (expr_hi))
9885 expr_hi = error_mark_node;
9887 /* We don't need to emit warnings here, as the common code
9888 will do this for us. */
9890 else
9891 expr_hi = NULL_TREE;
9893 if (parser->in_switch_statement_p)
9894 finish_case_label (token->location, expr, expr_hi);
9895 else
9896 error_at (token->location,
9897 "case label %qE not within a switch statement",
9898 expr);
9900 break;
9902 case RID_DEFAULT:
9903 /* Consume the `default' token. */
9904 cp_lexer_consume_token (parser->lexer);
9906 if (parser->in_switch_statement_p)
9907 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9908 else
9909 error_at (token->location, "case label not within a switch statement");
9910 break;
9912 default:
9913 /* Anything else must be an ordinary label. */
9914 label = finish_label_stmt (cp_parser_identifier (parser));
9915 break;
9918 /* Require the `:' token. */
9919 cp_parser_require (parser, CPP_COLON, RT_COLON);
9921 /* An ordinary label may optionally be followed by attributes.
9922 However, this is only permitted if the attributes are then
9923 followed by a semicolon. This is because, for backward
9924 compatibility, when parsing
9925 lab: __attribute__ ((unused)) int i;
9926 we want the attribute to attach to "i", not "lab". */
9927 if (label != NULL_TREE
9928 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9930 tree attrs;
9931 cp_parser_parse_tentatively (parser);
9932 attrs = cp_parser_gnu_attributes_opt (parser);
9933 if (attrs == NULL_TREE
9934 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9935 cp_parser_abort_tentative_parse (parser);
9936 else if (!cp_parser_parse_definitely (parser))
9938 else
9939 attributes = chainon (attributes, attrs);
9942 if (attributes != NULL_TREE)
9943 cplus_decl_attributes (&label, attributes, 0);
9945 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9948 /* Parse an expression-statement.
9950 expression-statement:
9951 expression [opt] ;
9953 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9954 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9955 indicates whether this expression-statement is part of an
9956 expression statement. */
9958 static tree
9959 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9961 tree statement = NULL_TREE;
9962 cp_token *token = cp_lexer_peek_token (parser->lexer);
9964 /* If the next token is a ';', then there is no expression
9965 statement. */
9966 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9968 statement = cp_parser_expression (parser);
9969 if (statement == error_mark_node
9970 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9972 cp_parser_skip_to_end_of_block_or_statement (parser);
9973 return error_mark_node;
9977 /* Give a helpful message for "A<T>::type t;" and the like. */
9978 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9979 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9981 if (TREE_CODE (statement) == SCOPE_REF)
9982 error_at (token->location, "need %<typename%> before %qE because "
9983 "%qT is a dependent scope",
9984 statement, TREE_OPERAND (statement, 0));
9985 else if (is_overloaded_fn (statement)
9986 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9988 /* A::A a; */
9989 tree fn = get_first_fn (statement);
9990 error_at (token->location,
9991 "%<%T::%D%> names the constructor, not the type",
9992 DECL_CONTEXT (fn), DECL_NAME (fn));
9996 /* Consume the final `;'. */
9997 cp_parser_consume_semicolon_at_end_of_statement (parser);
9999 if (in_statement_expr
10000 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10001 /* This is the final expression statement of a statement
10002 expression. */
10003 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10004 else if (statement)
10005 statement = finish_expr_stmt (statement);
10007 return statement;
10010 /* Parse a compound-statement.
10012 compound-statement:
10013 { statement-seq [opt] }
10015 GNU extension:
10017 compound-statement:
10018 { label-declaration-seq [opt] statement-seq [opt] }
10020 label-declaration-seq:
10021 label-declaration
10022 label-declaration-seq label-declaration
10024 Returns a tree representing the statement. */
10026 static tree
10027 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10028 bool in_try, bool function_body)
10030 tree compound_stmt;
10032 /* Consume the `{'. */
10033 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10034 return error_mark_node;
10035 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10036 && !function_body && cxx_dialect < cxx14)
10037 pedwarn (input_location, OPT_Wpedantic,
10038 "compound-statement in constexpr function");
10039 /* Begin the compound-statement. */
10040 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10041 /* If the next keyword is `__label__' we have a label declaration. */
10042 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10043 cp_parser_label_declaration (parser);
10044 /* Parse an (optional) statement-seq. */
10045 cp_parser_statement_seq_opt (parser, in_statement_expr);
10046 /* Finish the compound-statement. */
10047 finish_compound_stmt (compound_stmt);
10048 /* Consume the `}'. */
10049 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10051 return compound_stmt;
10054 /* Parse an (optional) statement-seq.
10056 statement-seq:
10057 statement
10058 statement-seq [opt] statement */
10060 static void
10061 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10063 /* Scan statements until there aren't any more. */
10064 while (true)
10066 cp_token *token = cp_lexer_peek_token (parser->lexer);
10068 /* If we are looking at a `}', then we have run out of
10069 statements; the same is true if we have reached the end
10070 of file, or have stumbled upon a stray '@end'. */
10071 if (token->type == CPP_CLOSE_BRACE
10072 || token->type == CPP_EOF
10073 || token->type == CPP_PRAGMA_EOL
10074 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10075 break;
10077 /* If we are in a compound statement and find 'else' then
10078 something went wrong. */
10079 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10081 if (parser->in_statement & IN_IF_STMT)
10082 break;
10083 else
10085 token = cp_lexer_consume_token (parser->lexer);
10086 error_at (token->location, "%<else%> without a previous %<if%>");
10090 /* Parse the statement. */
10091 cp_parser_statement (parser, in_statement_expr, true, NULL);
10095 /* Parse a selection-statement.
10097 selection-statement:
10098 if ( condition ) statement
10099 if ( condition ) statement else statement
10100 switch ( condition ) statement
10102 Returns the new IF_STMT or SWITCH_STMT.
10104 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10105 is a (possibly labeled) if statement which is not enclosed in
10106 braces and has an else clause. This is used to implement
10107 -Wparentheses. */
10109 static tree
10110 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10112 cp_token *token;
10113 enum rid keyword;
10115 if (if_p != NULL)
10116 *if_p = false;
10118 /* Peek at the next token. */
10119 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10121 /* See what kind of keyword it is. */
10122 keyword = token->keyword;
10123 switch (keyword)
10125 case RID_IF:
10126 case RID_SWITCH:
10128 tree statement;
10129 tree condition;
10131 /* Look for the `('. */
10132 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10134 cp_parser_skip_to_end_of_statement (parser);
10135 return error_mark_node;
10138 /* Begin the selection-statement. */
10139 if (keyword == RID_IF)
10140 statement = begin_if_stmt ();
10141 else
10142 statement = begin_switch_stmt ();
10144 /* Parse the condition. */
10145 condition = cp_parser_condition (parser);
10146 /* Look for the `)'. */
10147 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10148 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10149 /*consume_paren=*/true);
10151 if (keyword == RID_IF)
10153 bool nested_if;
10154 unsigned char in_statement;
10156 /* Add the condition. */
10157 finish_if_stmt_cond (condition, statement);
10159 /* Parse the then-clause. */
10160 in_statement = parser->in_statement;
10161 parser->in_statement |= IN_IF_STMT;
10162 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10164 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10165 add_stmt (build_empty_stmt (loc));
10166 cp_lexer_consume_token (parser->lexer);
10167 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10168 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10169 "empty body in an %<if%> statement");
10170 nested_if = false;
10172 else
10173 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10174 token->location, "if");
10175 parser->in_statement = in_statement;
10177 finish_then_clause (statement);
10179 /* If the next token is `else', parse the else-clause. */
10180 if (cp_lexer_next_token_is_keyword (parser->lexer,
10181 RID_ELSE))
10183 /* Consume the `else' keyword. */
10184 location_t else_tok_loc
10185 = cp_lexer_consume_token (parser->lexer)->location;
10186 begin_else_clause (statement);
10187 /* Parse the else-clause. */
10188 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10190 location_t loc;
10191 loc = cp_lexer_peek_token (parser->lexer)->location;
10192 warning_at (loc,
10193 OPT_Wempty_body, "suggest braces around "
10194 "empty body in an %<else%> statement");
10195 add_stmt (build_empty_stmt (loc));
10196 cp_lexer_consume_token (parser->lexer);
10198 else
10199 cp_parser_implicitly_scoped_statement (parser, NULL,
10200 else_tok_loc, "else");
10202 finish_else_clause (statement);
10204 /* If we are currently parsing a then-clause, then
10205 IF_P will not be NULL. We set it to true to
10206 indicate that this if statement has an else clause.
10207 This may trigger the Wparentheses warning below
10208 when we get back up to the parent if statement. */
10209 if (if_p != NULL)
10210 *if_p = true;
10212 else
10214 /* This if statement does not have an else clause. If
10215 NESTED_IF is true, then the then-clause is an if
10216 statement which does have an else clause. We warn
10217 about the potential ambiguity. */
10218 if (nested_if)
10219 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10220 "suggest explicit braces to avoid ambiguous"
10221 " %<else%>");
10224 /* Now we're all done with the if-statement. */
10225 finish_if_stmt (statement);
10227 else
10229 bool in_switch_statement_p;
10230 unsigned char in_statement;
10232 /* Add the condition. */
10233 finish_switch_cond (condition, statement);
10235 /* Parse the body of the switch-statement. */
10236 in_switch_statement_p = parser->in_switch_statement_p;
10237 in_statement = parser->in_statement;
10238 parser->in_switch_statement_p = true;
10239 parser->in_statement |= IN_SWITCH_STMT;
10240 cp_parser_implicitly_scoped_statement (parser, NULL,
10241 0, "switch");
10242 parser->in_switch_statement_p = in_switch_statement_p;
10243 parser->in_statement = in_statement;
10245 /* Now we're all done with the switch-statement. */
10246 finish_switch_stmt (statement);
10249 return statement;
10251 break;
10253 default:
10254 cp_parser_error (parser, "expected selection-statement");
10255 return error_mark_node;
10259 /* Parse a condition.
10261 condition:
10262 expression
10263 type-specifier-seq declarator = initializer-clause
10264 type-specifier-seq declarator braced-init-list
10266 GNU Extension:
10268 condition:
10269 type-specifier-seq declarator asm-specification [opt]
10270 attributes [opt] = assignment-expression
10272 Returns the expression that should be tested. */
10274 static tree
10275 cp_parser_condition (cp_parser* parser)
10277 cp_decl_specifier_seq type_specifiers;
10278 const char *saved_message;
10279 int declares_class_or_enum;
10281 /* Try the declaration first. */
10282 cp_parser_parse_tentatively (parser);
10283 /* New types are not allowed in the type-specifier-seq for a
10284 condition. */
10285 saved_message = parser->type_definition_forbidden_message;
10286 parser->type_definition_forbidden_message
10287 = G_("types may not be defined in conditions");
10288 /* Parse the type-specifier-seq. */
10289 cp_parser_decl_specifier_seq (parser,
10290 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10291 &type_specifiers,
10292 &declares_class_or_enum);
10293 /* Restore the saved message. */
10294 parser->type_definition_forbidden_message = saved_message;
10295 /* If all is well, we might be looking at a declaration. */
10296 if (!cp_parser_error_occurred (parser))
10298 tree decl;
10299 tree asm_specification;
10300 tree attributes;
10301 cp_declarator *declarator;
10302 tree initializer = NULL_TREE;
10304 /* Parse the declarator. */
10305 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10306 /*ctor_dtor_or_conv_p=*/NULL,
10307 /*parenthesized_p=*/NULL,
10308 /*member_p=*/false,
10309 /*friend_p=*/false);
10310 /* Parse the attributes. */
10311 attributes = cp_parser_attributes_opt (parser);
10312 /* Parse the asm-specification. */
10313 asm_specification = cp_parser_asm_specification_opt (parser);
10314 /* If the next token is not an `=' or '{', then we might still be
10315 looking at an expression. For example:
10317 if (A(a).x)
10319 looks like a decl-specifier-seq and a declarator -- but then
10320 there is no `=', so this is an expression. */
10321 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10322 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10323 cp_parser_simulate_error (parser);
10325 /* If we did see an `=' or '{', then we are looking at a declaration
10326 for sure. */
10327 if (cp_parser_parse_definitely (parser))
10329 tree pushed_scope;
10330 bool non_constant_p;
10331 bool flags = LOOKUP_ONLYCONVERTING;
10333 /* Create the declaration. */
10334 decl = start_decl (declarator, &type_specifiers,
10335 /*initialized_p=*/true,
10336 attributes, /*prefix_attributes=*/NULL_TREE,
10337 &pushed_scope);
10339 /* Parse the initializer. */
10340 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10342 initializer = cp_parser_braced_list (parser, &non_constant_p);
10343 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10344 flags = 0;
10346 else
10348 /* Consume the `='. */
10349 cp_parser_require (parser, CPP_EQ, RT_EQ);
10350 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10352 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10353 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10355 /* Process the initializer. */
10356 cp_finish_decl (decl,
10357 initializer, !non_constant_p,
10358 asm_specification,
10359 flags);
10361 if (pushed_scope)
10362 pop_scope (pushed_scope);
10364 return convert_from_reference (decl);
10367 /* If we didn't even get past the declarator successfully, we are
10368 definitely not looking at a declaration. */
10369 else
10370 cp_parser_abort_tentative_parse (parser);
10372 /* Otherwise, we are looking at an expression. */
10373 return cp_parser_expression (parser);
10376 /* Parses a for-statement or range-for-statement until the closing ')',
10377 not included. */
10379 static tree
10380 cp_parser_for (cp_parser *parser, bool ivdep)
10382 tree init, scope, decl;
10383 bool is_range_for;
10385 /* Begin the for-statement. */
10386 scope = begin_for_scope (&init);
10388 /* Parse the initialization. */
10389 is_range_for = cp_parser_for_init_statement (parser, &decl);
10391 if (is_range_for)
10392 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10393 else
10394 return cp_parser_c_for (parser, scope, init, ivdep);
10397 static tree
10398 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10400 /* Normal for loop */
10401 tree condition = NULL_TREE;
10402 tree expression = NULL_TREE;
10403 tree stmt;
10405 stmt = begin_for_stmt (scope, init);
10406 /* The for-init-statement has already been parsed in
10407 cp_parser_for_init_statement, so no work is needed here. */
10408 finish_for_init_stmt (stmt);
10410 /* If there's a condition, process it. */
10411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10412 condition = cp_parser_condition (parser);
10413 else if (ivdep)
10415 cp_parser_error (parser, "missing loop condition in loop with "
10416 "%<GCC ivdep%> pragma");
10417 condition = error_mark_node;
10419 finish_for_cond (condition, stmt, ivdep);
10420 /* Look for the `;'. */
10421 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10423 /* If there's an expression, process it. */
10424 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10425 expression = cp_parser_expression (parser);
10426 finish_for_expr (expression, stmt);
10428 return stmt;
10431 /* Tries to parse a range-based for-statement:
10433 range-based-for:
10434 decl-specifier-seq declarator : expression
10436 The decl-specifier-seq declarator and the `:' are already parsed by
10437 cp_parser_for_init_statement. If processing_template_decl it returns a
10438 newly created RANGE_FOR_STMT; if not, it is converted to a
10439 regular FOR_STMT. */
10441 static tree
10442 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10443 bool ivdep)
10445 tree stmt, range_expr;
10447 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10449 bool expr_non_constant_p;
10450 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10452 else
10453 range_expr = cp_parser_expression (parser);
10455 /* If in template, STMT is converted to a normal for-statement
10456 at instantiation. If not, it is done just ahead. */
10457 if (processing_template_decl)
10459 if (check_for_bare_parameter_packs (range_expr))
10460 range_expr = error_mark_node;
10461 stmt = begin_range_for_stmt (scope, init);
10462 if (ivdep)
10463 RANGE_FOR_IVDEP (stmt) = 1;
10464 finish_range_for_decl (stmt, range_decl, range_expr);
10465 if (!type_dependent_expression_p (range_expr)
10466 /* do_auto_deduction doesn't mess with template init-lists. */
10467 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10468 do_range_for_auto_deduction (range_decl, range_expr);
10470 else
10472 stmt = begin_for_stmt (scope, init);
10473 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10475 return stmt;
10478 /* Subroutine of cp_convert_range_for: given the initializer expression,
10479 builds up the range temporary. */
10481 static tree
10482 build_range_temp (tree range_expr)
10484 tree range_type, range_temp;
10486 /* Find out the type deduced by the declaration
10487 `auto &&__range = range_expr'. */
10488 range_type = cp_build_reference_type (make_auto (), true);
10489 range_type = do_auto_deduction (range_type, range_expr,
10490 type_uses_auto (range_type));
10492 /* Create the __range variable. */
10493 range_temp = build_decl (input_location, VAR_DECL,
10494 get_identifier ("__for_range"), range_type);
10495 TREE_USED (range_temp) = 1;
10496 DECL_ARTIFICIAL (range_temp) = 1;
10498 return range_temp;
10501 /* Used by cp_parser_range_for in template context: we aren't going to
10502 do a full conversion yet, but we still need to resolve auto in the
10503 type of the for-range-declaration if present. This is basically
10504 a shortcut version of cp_convert_range_for. */
10506 static void
10507 do_range_for_auto_deduction (tree decl, tree range_expr)
10509 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10510 if (auto_node)
10512 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10513 range_temp = convert_from_reference (build_range_temp (range_expr));
10514 iter_type = (cp_parser_perform_range_for_lookup
10515 (range_temp, &begin_dummy, &end_dummy));
10516 if (iter_type)
10518 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10519 iter_type);
10520 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10521 tf_warning_or_error);
10522 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10523 iter_decl, auto_node);
10528 /* Converts a range-based for-statement into a normal
10529 for-statement, as per the definition.
10531 for (RANGE_DECL : RANGE_EXPR)
10532 BLOCK
10534 should be equivalent to:
10537 auto &&__range = RANGE_EXPR;
10538 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10539 __begin != __end;
10540 ++__begin)
10542 RANGE_DECL = *__begin;
10543 BLOCK
10547 If RANGE_EXPR is an array:
10548 BEGIN_EXPR = __range
10549 END_EXPR = __range + ARRAY_SIZE(__range)
10550 Else if RANGE_EXPR has a member 'begin' or 'end':
10551 BEGIN_EXPR = __range.begin()
10552 END_EXPR = __range.end()
10553 Else:
10554 BEGIN_EXPR = begin(__range)
10555 END_EXPR = end(__range);
10557 If __range has a member 'begin' but not 'end', or vice versa, we must
10558 still use the second alternative (it will surely fail, however).
10559 When calling begin()/end() in the third alternative we must use
10560 argument dependent lookup, but always considering 'std' as an associated
10561 namespace. */
10563 tree
10564 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10565 bool ivdep)
10567 tree begin, end;
10568 tree iter_type, begin_expr, end_expr;
10569 tree condition, expression;
10571 if (range_decl == error_mark_node || range_expr == error_mark_node)
10572 /* If an error happened previously do nothing or else a lot of
10573 unhelpful errors would be issued. */
10574 begin_expr = end_expr = iter_type = error_mark_node;
10575 else
10577 tree range_temp;
10579 if (VAR_P (range_expr)
10580 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10581 /* Can't bind a reference to an array of runtime bound. */
10582 range_temp = range_expr;
10583 else
10585 range_temp = build_range_temp (range_expr);
10586 pushdecl (range_temp);
10587 cp_finish_decl (range_temp, range_expr,
10588 /*is_constant_init*/false, NULL_TREE,
10589 LOOKUP_ONLYCONVERTING);
10590 range_temp = convert_from_reference (range_temp);
10592 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10593 &begin_expr, &end_expr);
10596 /* The new for initialization statement. */
10597 begin = build_decl (input_location, VAR_DECL,
10598 get_identifier ("__for_begin"), iter_type);
10599 TREE_USED (begin) = 1;
10600 DECL_ARTIFICIAL (begin) = 1;
10601 pushdecl (begin);
10602 cp_finish_decl (begin, begin_expr,
10603 /*is_constant_init*/false, NULL_TREE,
10604 LOOKUP_ONLYCONVERTING);
10606 end = build_decl (input_location, VAR_DECL,
10607 get_identifier ("__for_end"), iter_type);
10608 TREE_USED (end) = 1;
10609 DECL_ARTIFICIAL (end) = 1;
10610 pushdecl (end);
10611 cp_finish_decl (end, end_expr,
10612 /*is_constant_init*/false, NULL_TREE,
10613 LOOKUP_ONLYCONVERTING);
10615 finish_for_init_stmt (statement);
10617 /* The new for condition. */
10618 condition = build_x_binary_op (input_location, NE_EXPR,
10619 begin, ERROR_MARK,
10620 end, ERROR_MARK,
10621 NULL, tf_warning_or_error);
10622 finish_for_cond (condition, statement, ivdep);
10624 /* The new increment expression. */
10625 expression = finish_unary_op_expr (input_location,
10626 PREINCREMENT_EXPR, begin,
10627 tf_warning_or_error);
10628 finish_for_expr (expression, statement);
10630 /* The declaration is initialized with *__begin inside the loop body. */
10631 cp_finish_decl (range_decl,
10632 build_x_indirect_ref (input_location, begin, RO_NULL,
10633 tf_warning_or_error),
10634 /*is_constant_init*/false, NULL_TREE,
10635 LOOKUP_ONLYCONVERTING);
10637 return statement;
10640 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10641 We need to solve both at the same time because the method used
10642 depends on the existence of members begin or end.
10643 Returns the type deduced for the iterator expression. */
10645 static tree
10646 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10648 if (error_operand_p (range))
10650 *begin = *end = error_mark_node;
10651 return error_mark_node;
10654 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10656 error ("range-based %<for%> expression of type %qT "
10657 "has incomplete type", TREE_TYPE (range));
10658 *begin = *end = error_mark_node;
10659 return error_mark_node;
10661 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10663 /* If RANGE is an array, we will use pointer arithmetic. */
10664 *begin = range;
10665 *end = build_binary_op (input_location, PLUS_EXPR,
10666 range,
10667 array_type_nelts_top (TREE_TYPE (range)),
10669 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10671 else
10673 /* If it is not an array, we must do a bit of magic. */
10674 tree id_begin, id_end;
10675 tree member_begin, member_end;
10677 *begin = *end = error_mark_node;
10679 id_begin = get_identifier ("begin");
10680 id_end = get_identifier ("end");
10681 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10682 /*protect=*/2, /*want_type=*/false,
10683 tf_warning_or_error);
10684 member_end = lookup_member (TREE_TYPE (range), id_end,
10685 /*protect=*/2, /*want_type=*/false,
10686 tf_warning_or_error);
10688 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10690 /* Use the member functions. */
10691 if (member_begin != NULL_TREE)
10692 *begin = cp_parser_range_for_member_function (range, id_begin);
10693 else
10694 error ("range-based %<for%> expression of type %qT has an "
10695 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10697 if (member_end != NULL_TREE)
10698 *end = cp_parser_range_for_member_function (range, id_end);
10699 else
10700 error ("range-based %<for%> expression of type %qT has a "
10701 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10703 else
10705 /* Use global functions with ADL. */
10706 vec<tree, va_gc> *vec;
10707 vec = make_tree_vector ();
10709 vec_safe_push (vec, range);
10711 member_begin = perform_koenig_lookup (id_begin, vec,
10712 tf_warning_or_error);
10713 *begin = finish_call_expr (member_begin, &vec, false, true,
10714 tf_warning_or_error);
10715 member_end = perform_koenig_lookup (id_end, vec,
10716 tf_warning_or_error);
10717 *end = finish_call_expr (member_end, &vec, false, true,
10718 tf_warning_or_error);
10720 release_tree_vector (vec);
10723 /* Last common checks. */
10724 if (*begin == error_mark_node || *end == error_mark_node)
10726 /* If one of the expressions is an error do no more checks. */
10727 *begin = *end = error_mark_node;
10728 return error_mark_node;
10730 else if (type_dependent_expression_p (*begin)
10731 || type_dependent_expression_p (*end))
10732 /* Can happen, when, eg, in a template context, Koenig lookup
10733 can't resolve begin/end (c++/58503). */
10734 return NULL_TREE;
10735 else
10737 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10738 /* The unqualified type of the __begin and __end temporaries should
10739 be the same, as required by the multiple auto declaration. */
10740 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10741 error ("inconsistent begin/end types in range-based %<for%> "
10742 "statement: %qT and %qT",
10743 TREE_TYPE (*begin), TREE_TYPE (*end));
10744 return iter_type;
10749 /* Helper function for cp_parser_perform_range_for_lookup.
10750 Builds a tree for RANGE.IDENTIFIER(). */
10752 static tree
10753 cp_parser_range_for_member_function (tree range, tree identifier)
10755 tree member, res;
10756 vec<tree, va_gc> *vec;
10758 member = finish_class_member_access_expr (range, identifier,
10759 false, tf_warning_or_error);
10760 if (member == error_mark_node)
10761 return error_mark_node;
10763 vec = make_tree_vector ();
10764 res = finish_call_expr (member, &vec,
10765 /*disallow_virtual=*/false,
10766 /*koenig_p=*/false,
10767 tf_warning_or_error);
10768 release_tree_vector (vec);
10769 return res;
10772 /* Parse an iteration-statement.
10774 iteration-statement:
10775 while ( condition ) statement
10776 do statement while ( expression ) ;
10777 for ( for-init-statement condition [opt] ; expression [opt] )
10778 statement
10780 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10782 static tree
10783 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10785 cp_token *token;
10786 location_t tok_loc;
10787 enum rid keyword;
10788 tree statement;
10789 unsigned char in_statement;
10791 /* Peek at the next token. */
10792 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10793 if (!token)
10794 return error_mark_node;
10796 tok_loc = token->location;
10798 /* Remember whether or not we are already within an iteration
10799 statement. */
10800 in_statement = parser->in_statement;
10802 /* See what kind of keyword it is. */
10803 keyword = token->keyword;
10804 switch (keyword)
10806 case RID_WHILE:
10808 tree condition;
10810 /* Begin the while-statement. */
10811 statement = begin_while_stmt ();
10812 /* Look for the `('. */
10813 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10814 /* Parse the condition. */
10815 condition = cp_parser_condition (parser);
10816 finish_while_stmt_cond (condition, statement, ivdep);
10817 /* Look for the `)'. */
10818 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10819 /* Parse the dependent statement. */
10820 parser->in_statement = IN_ITERATION_STMT;
10821 cp_parser_already_scoped_statement (parser, tok_loc, "while");
10822 parser->in_statement = in_statement;
10823 /* We're done with the while-statement. */
10824 finish_while_stmt (statement);
10826 break;
10828 case RID_DO:
10830 tree expression;
10832 /* Begin the do-statement. */
10833 statement = begin_do_stmt ();
10834 /* Parse the body of the do-statement. */
10835 parser->in_statement = IN_ITERATION_STMT;
10836 cp_parser_implicitly_scoped_statement (parser, NULL, 0, "do");
10837 parser->in_statement = in_statement;
10838 finish_do_body (statement);
10839 /* Look for the `while' keyword. */
10840 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10841 /* Look for the `('. */
10842 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10843 /* Parse the expression. */
10844 expression = cp_parser_expression (parser);
10845 /* We're done with the do-statement. */
10846 finish_do_stmt (expression, statement, ivdep);
10847 /* Look for the `)'. */
10848 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10849 /* Look for the `;'. */
10850 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10852 break;
10854 case RID_FOR:
10856 /* Look for the `('. */
10857 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10859 statement = cp_parser_for (parser, ivdep);
10861 /* Look for the `)'. */
10862 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10864 /* Parse the body of the for-statement. */
10865 parser->in_statement = IN_ITERATION_STMT;
10866 cp_parser_already_scoped_statement (parser, tok_loc, "for");
10867 parser->in_statement = in_statement;
10869 /* We're done with the for-statement. */
10870 finish_for_stmt (statement);
10872 break;
10874 default:
10875 cp_parser_error (parser, "expected iteration-statement");
10876 statement = error_mark_node;
10877 break;
10880 return statement;
10883 /* Parse a for-init-statement or the declarator of a range-based-for.
10884 Returns true if a range-based-for declaration is seen.
10886 for-init-statement:
10887 expression-statement
10888 simple-declaration */
10890 static bool
10891 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10893 /* If the next token is a `;', then we have an empty
10894 expression-statement. Grammatically, this is also a
10895 simple-declaration, but an invalid one, because it does not
10896 declare anything. Therefore, if we did not handle this case
10897 specially, we would issue an error message about an invalid
10898 declaration. */
10899 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10901 bool is_range_for = false;
10902 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10904 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10905 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10907 /* N3994 -- for (id : init) ... */
10908 if (cxx_dialect < cxx1z)
10909 pedwarn (input_location, 0, "range-based for loop without a "
10910 "type-specifier only available with "
10911 "-std=c++1z or -std=gnu++1z");
10912 tree name = cp_parser_identifier (parser);
10913 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10914 *decl = build_decl (input_location, VAR_DECL, name, type);
10915 pushdecl (*decl);
10916 cp_lexer_consume_token (parser->lexer);
10917 return true;
10920 /* A colon is used in range-based for. */
10921 parser->colon_corrects_to_scope_p = false;
10923 /* We're going to speculatively look for a declaration, falling back
10924 to an expression, if necessary. */
10925 cp_parser_parse_tentatively (parser);
10926 /* Parse the declaration. */
10927 cp_parser_simple_declaration (parser,
10928 /*function_definition_allowed_p=*/false,
10929 decl);
10930 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10931 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10933 /* It is a range-for, consume the ':' */
10934 cp_lexer_consume_token (parser->lexer);
10935 is_range_for = true;
10936 if (cxx_dialect < cxx11)
10938 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10939 "range-based %<for%> loops only available with "
10940 "-std=c++11 or -std=gnu++11");
10941 *decl = error_mark_node;
10944 else
10945 /* The ';' is not consumed yet because we told
10946 cp_parser_simple_declaration not to. */
10947 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10949 if (cp_parser_parse_definitely (parser))
10950 return is_range_for;
10951 /* If the tentative parse failed, then we shall need to look for an
10952 expression-statement. */
10954 /* If we are here, it is an expression-statement. */
10955 cp_parser_expression_statement (parser, NULL_TREE);
10956 return false;
10959 /* Parse a jump-statement.
10961 jump-statement:
10962 break ;
10963 continue ;
10964 return expression [opt] ;
10965 return braced-init-list ;
10966 goto identifier ;
10968 GNU extension:
10970 jump-statement:
10971 goto * expression ;
10973 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10975 static tree
10976 cp_parser_jump_statement (cp_parser* parser)
10978 tree statement = error_mark_node;
10979 cp_token *token;
10980 enum rid keyword;
10981 unsigned char in_statement;
10983 /* Peek at the next token. */
10984 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10985 if (!token)
10986 return error_mark_node;
10988 /* See what kind of keyword it is. */
10989 keyword = token->keyword;
10990 switch (keyword)
10992 case RID_BREAK:
10993 in_statement = parser->in_statement & ~IN_IF_STMT;
10994 switch (in_statement)
10996 case 0:
10997 error_at (token->location, "break statement not within loop or switch");
10998 break;
10999 default:
11000 gcc_assert ((in_statement & IN_SWITCH_STMT)
11001 || in_statement == IN_ITERATION_STMT);
11002 statement = finish_break_stmt ();
11003 if (in_statement == IN_ITERATION_STMT)
11004 break_maybe_infinite_loop ();
11005 break;
11006 case IN_OMP_BLOCK:
11007 error_at (token->location, "invalid exit from OpenMP structured block");
11008 break;
11009 case IN_OMP_FOR:
11010 error_at (token->location, "break statement used with OpenMP for loop");
11011 break;
11012 case IN_CILK_SIMD_FOR:
11013 error_at (token->location, "break statement used with Cilk Plus for loop");
11014 break;
11016 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11017 break;
11019 case RID_CONTINUE:
11020 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11022 case 0:
11023 error_at (token->location, "continue statement not within a loop");
11024 break;
11025 case IN_CILK_SIMD_FOR:
11026 error_at (token->location,
11027 "continue statement within %<#pragma simd%> loop body");
11028 /* Fall through. */
11029 case IN_ITERATION_STMT:
11030 case IN_OMP_FOR:
11031 statement = finish_continue_stmt ();
11032 break;
11033 case IN_OMP_BLOCK:
11034 error_at (token->location, "invalid exit from OpenMP structured block");
11035 break;
11036 default:
11037 gcc_unreachable ();
11039 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11040 break;
11042 case RID_RETURN:
11044 tree expr;
11045 bool expr_non_constant_p;
11047 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11049 cp_lexer_set_source_position (parser->lexer);
11050 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11051 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11053 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11054 expr = cp_parser_expression (parser);
11055 else
11056 /* If the next token is a `;', then there is no
11057 expression. */
11058 expr = NULL_TREE;
11059 /* Build the return-statement. */
11060 statement = finish_return_stmt (expr);
11061 /* Look for the final `;'. */
11062 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11064 break;
11066 case RID_GOTO:
11067 if (parser->in_function_body
11068 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11070 error ("%<goto%> in %<constexpr%> function");
11071 cp_function_chain->invalid_constexpr = true;
11074 /* Create the goto-statement. */
11075 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11077 /* Issue a warning about this use of a GNU extension. */
11078 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11079 /* Consume the '*' token. */
11080 cp_lexer_consume_token (parser->lexer);
11081 /* Parse the dependent expression. */
11082 finish_goto_stmt (cp_parser_expression (parser));
11084 else
11085 finish_goto_stmt (cp_parser_identifier (parser));
11086 /* Look for the final `;'. */
11087 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11088 break;
11090 default:
11091 cp_parser_error (parser, "expected jump-statement");
11092 break;
11095 return statement;
11098 /* Parse a declaration-statement.
11100 declaration-statement:
11101 block-declaration */
11103 static void
11104 cp_parser_declaration_statement (cp_parser* parser)
11106 void *p;
11108 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11109 p = obstack_alloc (&declarator_obstack, 0);
11111 /* Parse the block-declaration. */
11112 cp_parser_block_declaration (parser, /*statement_p=*/true);
11114 /* Free any declarators allocated. */
11115 obstack_free (&declarator_obstack, p);
11118 /* Some dependent statements (like `if (cond) statement'), are
11119 implicitly in their own scope. In other words, if the statement is
11120 a single statement (as opposed to a compound-statement), it is
11121 none-the-less treated as if it were enclosed in braces. Any
11122 declarations appearing in the dependent statement are out of scope
11123 after control passes that point. This function parses a statement,
11124 but ensures that is in its own scope, even if it is not a
11125 compound-statement.
11127 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11128 is a (possibly labeled) if statement which is not enclosed in
11129 braces and has an else clause. This is used to implement
11130 -Wparentheses.
11132 Returns the new statement. */
11134 static tree
11135 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11136 location_t guard_loc,
11137 const char *guard_kind)
11139 tree statement;
11141 if (if_p != NULL)
11142 *if_p = false;
11144 /* Mark if () ; with a special NOP_EXPR. */
11145 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11147 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11148 cp_lexer_consume_token (parser->lexer);
11149 statement = add_stmt (build_empty_stmt (loc));
11151 /* if a compound is opened, we simply parse the statement directly. */
11152 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11153 statement = cp_parser_compound_statement (parser, NULL, false, false);
11154 /* If the token is not a `{', then we must take special action. */
11155 else
11157 /* Create a compound-statement. */
11158 statement = begin_compound_stmt (0);
11159 /* Parse the dependent-statement. */
11160 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11161 cp_parser_statement (parser, NULL_TREE, false, if_p);
11162 /* Finish the dummy compound-statement. */
11163 finish_compound_stmt (statement);
11164 cp_token *next_tok = cp_lexer_peek_token (parser->lexer);
11165 if (next_tok->keyword != RID_ELSE)
11167 location_t next_stmt_loc = next_tok->location;
11168 warn_for_misleading_indentation (guard_loc, body_loc,
11169 next_stmt_loc, next_tok->type,
11170 guard_kind);
11174 /* Return the statement. */
11175 return statement;
11178 /* For some dependent statements (like `while (cond) statement'), we
11179 have already created a scope. Therefore, even if the dependent
11180 statement is a compound-statement, we do not want to create another
11181 scope. */
11183 static void
11184 cp_parser_already_scoped_statement (cp_parser* parser, location_t guard_loc,
11185 const char *guard_kind)
11187 /* If the token is a `{', then we must take special action. */
11188 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11190 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11191 cp_parser_statement (parser, NULL_TREE, false, NULL);
11192 cp_token *next_tok = cp_lexer_peek_token (parser->lexer);
11193 location_t next_stmt_loc = next_tok->location;
11194 warn_for_misleading_indentation (guard_loc, body_loc,
11195 next_stmt_loc, next_tok->type,
11196 guard_kind);
11198 else
11200 /* Avoid calling cp_parser_compound_statement, so that we
11201 don't create a new scope. Do everything else by hand. */
11202 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11203 /* If the next keyword is `__label__' we have a label declaration. */
11204 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11205 cp_parser_label_declaration (parser);
11206 /* Parse an (optional) statement-seq. */
11207 cp_parser_statement_seq_opt (parser, NULL_TREE);
11208 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11212 /* Declarations [gram.dcl.dcl] */
11214 /* Parse an optional declaration-sequence.
11216 declaration-seq:
11217 declaration
11218 declaration-seq declaration */
11220 static void
11221 cp_parser_declaration_seq_opt (cp_parser* parser)
11223 while (true)
11225 cp_token *token;
11227 token = cp_lexer_peek_token (parser->lexer);
11229 if (token->type == CPP_CLOSE_BRACE
11230 || token->type == CPP_EOF
11231 || token->type == CPP_PRAGMA_EOL)
11232 break;
11234 if (token->type == CPP_SEMICOLON)
11236 /* A declaration consisting of a single semicolon is
11237 invalid. Allow it unless we're being pedantic. */
11238 cp_lexer_consume_token (parser->lexer);
11239 if (!in_system_header_at (input_location))
11240 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11241 continue;
11244 /* If we're entering or exiting a region that's implicitly
11245 extern "C", modify the lang context appropriately. */
11246 if (!parser->implicit_extern_c && token->implicit_extern_c)
11248 push_lang_context (lang_name_c);
11249 parser->implicit_extern_c = true;
11251 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11253 pop_lang_context ();
11254 parser->implicit_extern_c = false;
11257 if (token->type == CPP_PRAGMA)
11259 /* A top-level declaration can consist solely of a #pragma.
11260 A nested declaration cannot, so this is done here and not
11261 in cp_parser_declaration. (A #pragma at block scope is
11262 handled in cp_parser_statement.) */
11263 cp_parser_pragma (parser, pragma_external);
11264 continue;
11267 /* Parse the declaration itself. */
11268 cp_parser_declaration (parser);
11272 /* Parse a declaration.
11274 declaration:
11275 block-declaration
11276 function-definition
11277 template-declaration
11278 explicit-instantiation
11279 explicit-specialization
11280 linkage-specification
11281 namespace-definition
11283 GNU extension:
11285 declaration:
11286 __extension__ declaration */
11288 static void
11289 cp_parser_declaration (cp_parser* parser)
11291 cp_token token1;
11292 cp_token token2;
11293 int saved_pedantic;
11294 void *p;
11295 tree attributes = NULL_TREE;
11297 /* Check for the `__extension__' keyword. */
11298 if (cp_parser_extension_opt (parser, &saved_pedantic))
11300 /* Parse the qualified declaration. */
11301 cp_parser_declaration (parser);
11302 /* Restore the PEDANTIC flag. */
11303 pedantic = saved_pedantic;
11305 return;
11308 /* Try to figure out what kind of declaration is present. */
11309 token1 = *cp_lexer_peek_token (parser->lexer);
11311 if (token1.type != CPP_EOF)
11312 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11313 else
11315 token2.type = CPP_EOF;
11316 token2.keyword = RID_MAX;
11319 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11320 p = obstack_alloc (&declarator_obstack, 0);
11322 /* If the next token is `extern' and the following token is a string
11323 literal, then we have a linkage specification. */
11324 if (token1.keyword == RID_EXTERN
11325 && cp_parser_is_pure_string_literal (&token2))
11326 cp_parser_linkage_specification (parser);
11327 /* If the next token is `template', then we have either a template
11328 declaration, an explicit instantiation, or an explicit
11329 specialization. */
11330 else if (token1.keyword == RID_TEMPLATE)
11332 /* `template <>' indicates a template specialization. */
11333 if (token2.type == CPP_LESS
11334 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11335 cp_parser_explicit_specialization (parser);
11336 /* `template <' indicates a template declaration. */
11337 else if (token2.type == CPP_LESS)
11338 cp_parser_template_declaration (parser, /*member_p=*/false);
11339 /* Anything else must be an explicit instantiation. */
11340 else
11341 cp_parser_explicit_instantiation (parser);
11343 /* If the next token is `export', then we have a template
11344 declaration. */
11345 else if (token1.keyword == RID_EXPORT)
11346 cp_parser_template_declaration (parser, /*member_p=*/false);
11347 /* If the next token is `extern', 'static' or 'inline' and the one
11348 after that is `template', we have a GNU extended explicit
11349 instantiation directive. */
11350 else if (cp_parser_allow_gnu_extensions_p (parser)
11351 && (token1.keyword == RID_EXTERN
11352 || token1.keyword == RID_STATIC
11353 || token1.keyword == RID_INLINE)
11354 && token2.keyword == RID_TEMPLATE)
11355 cp_parser_explicit_instantiation (parser);
11356 /* If the next token is `namespace', check for a named or unnamed
11357 namespace definition. */
11358 else if (token1.keyword == RID_NAMESPACE
11359 && (/* A named namespace definition. */
11360 (token2.type == CPP_NAME
11361 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11362 != CPP_EQ))
11363 /* An unnamed namespace definition. */
11364 || token2.type == CPP_OPEN_BRACE
11365 || token2.keyword == RID_ATTRIBUTE))
11366 cp_parser_namespace_definition (parser);
11367 /* An inline (associated) namespace definition. */
11368 else if (token1.keyword == RID_INLINE
11369 && token2.keyword == RID_NAMESPACE)
11370 cp_parser_namespace_definition (parser);
11371 /* Objective-C++ declaration/definition. */
11372 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11373 cp_parser_objc_declaration (parser, NULL_TREE);
11374 else if (c_dialect_objc ()
11375 && token1.keyword == RID_ATTRIBUTE
11376 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11377 cp_parser_objc_declaration (parser, attributes);
11378 /* We must have either a block declaration or a function
11379 definition. */
11380 else
11381 /* Try to parse a block-declaration, or a function-definition. */
11382 cp_parser_block_declaration (parser, /*statement_p=*/false);
11384 /* Free any declarators allocated. */
11385 obstack_free (&declarator_obstack, p);
11388 /* Parse a block-declaration.
11390 block-declaration:
11391 simple-declaration
11392 asm-definition
11393 namespace-alias-definition
11394 using-declaration
11395 using-directive
11397 GNU Extension:
11399 block-declaration:
11400 __extension__ block-declaration
11402 C++0x Extension:
11404 block-declaration:
11405 static_assert-declaration
11407 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11408 part of a declaration-statement. */
11410 static void
11411 cp_parser_block_declaration (cp_parser *parser,
11412 bool statement_p)
11414 cp_token *token1;
11415 int saved_pedantic;
11417 /* Check for the `__extension__' keyword. */
11418 if (cp_parser_extension_opt (parser, &saved_pedantic))
11420 /* Parse the qualified declaration. */
11421 cp_parser_block_declaration (parser, statement_p);
11422 /* Restore the PEDANTIC flag. */
11423 pedantic = saved_pedantic;
11425 return;
11428 /* Peek at the next token to figure out which kind of declaration is
11429 present. */
11430 token1 = cp_lexer_peek_token (parser->lexer);
11432 /* If the next keyword is `asm', we have an asm-definition. */
11433 if (token1->keyword == RID_ASM)
11435 if (statement_p)
11436 cp_parser_commit_to_tentative_parse (parser);
11437 cp_parser_asm_definition (parser);
11439 /* If the next keyword is `namespace', we have a
11440 namespace-alias-definition. */
11441 else if (token1->keyword == RID_NAMESPACE)
11442 cp_parser_namespace_alias_definition (parser);
11443 /* If the next keyword is `using', we have a
11444 using-declaration, a using-directive, or an alias-declaration. */
11445 else if (token1->keyword == RID_USING)
11447 cp_token *token2;
11449 if (statement_p)
11450 cp_parser_commit_to_tentative_parse (parser);
11451 /* If the token after `using' is `namespace', then we have a
11452 using-directive. */
11453 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11454 if (token2->keyword == RID_NAMESPACE)
11455 cp_parser_using_directive (parser);
11456 /* If the second token after 'using' is '=', then we have an
11457 alias-declaration. */
11458 else if (cxx_dialect >= cxx11
11459 && token2->type == CPP_NAME
11460 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11461 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11462 cp_parser_alias_declaration (parser);
11463 /* Otherwise, it's a using-declaration. */
11464 else
11465 cp_parser_using_declaration (parser,
11466 /*access_declaration_p=*/false);
11468 /* If the next keyword is `__label__' we have a misplaced label
11469 declaration. */
11470 else if (token1->keyword == RID_LABEL)
11472 cp_lexer_consume_token (parser->lexer);
11473 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11474 cp_parser_skip_to_end_of_statement (parser);
11475 /* If the next token is now a `;', consume it. */
11476 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11477 cp_lexer_consume_token (parser->lexer);
11479 /* If the next token is `static_assert' we have a static assertion. */
11480 else if (token1->keyword == RID_STATIC_ASSERT)
11481 cp_parser_static_assert (parser, /*member_p=*/false);
11482 /* Anything else must be a simple-declaration. */
11483 else
11484 cp_parser_simple_declaration (parser, !statement_p,
11485 /*maybe_range_for_decl*/NULL);
11488 /* Parse a simple-declaration.
11490 simple-declaration:
11491 decl-specifier-seq [opt] init-declarator-list [opt] ;
11493 init-declarator-list:
11494 init-declarator
11495 init-declarator-list , init-declarator
11497 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11498 function-definition as a simple-declaration.
11500 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11501 parsed declaration if it is an uninitialized single declarator not followed
11502 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11503 if present, will not be consumed. */
11505 static void
11506 cp_parser_simple_declaration (cp_parser* parser,
11507 bool function_definition_allowed_p,
11508 tree *maybe_range_for_decl)
11510 cp_decl_specifier_seq decl_specifiers;
11511 int declares_class_or_enum;
11512 bool saw_declarator;
11513 location_t comma_loc = UNKNOWN_LOCATION;
11514 location_t init_loc = UNKNOWN_LOCATION;
11516 if (maybe_range_for_decl)
11517 *maybe_range_for_decl = NULL_TREE;
11519 /* Defer access checks until we know what is being declared; the
11520 checks for names appearing in the decl-specifier-seq should be
11521 done as if we were in the scope of the thing being declared. */
11522 push_deferring_access_checks (dk_deferred);
11524 /* Parse the decl-specifier-seq. We have to keep track of whether
11525 or not the decl-specifier-seq declares a named class or
11526 enumeration type, since that is the only case in which the
11527 init-declarator-list is allowed to be empty.
11529 [dcl.dcl]
11531 In a simple-declaration, the optional init-declarator-list can be
11532 omitted only when declaring a class or enumeration, that is when
11533 the decl-specifier-seq contains either a class-specifier, an
11534 elaborated-type-specifier, or an enum-specifier. */
11535 cp_parser_decl_specifier_seq (parser,
11536 CP_PARSER_FLAGS_OPTIONAL,
11537 &decl_specifiers,
11538 &declares_class_or_enum);
11539 /* We no longer need to defer access checks. */
11540 stop_deferring_access_checks ();
11542 /* In a block scope, a valid declaration must always have a
11543 decl-specifier-seq. By not trying to parse declarators, we can
11544 resolve the declaration/expression ambiguity more quickly. */
11545 if (!function_definition_allowed_p
11546 && !decl_specifiers.any_specifiers_p)
11548 cp_parser_error (parser, "expected declaration");
11549 goto done;
11552 /* If the next two tokens are both identifiers, the code is
11553 erroneous. The usual cause of this situation is code like:
11555 T t;
11557 where "T" should name a type -- but does not. */
11558 if (!decl_specifiers.any_type_specifiers_p
11559 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11561 /* If parsing tentatively, we should commit; we really are
11562 looking at a declaration. */
11563 cp_parser_commit_to_tentative_parse (parser);
11564 /* Give up. */
11565 goto done;
11568 /* If we have seen at least one decl-specifier, and the next token
11569 is not a parenthesis, then we must be looking at a declaration.
11570 (After "int (" we might be looking at a functional cast.) */
11571 if (decl_specifiers.any_specifiers_p
11572 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11573 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11574 && !cp_parser_error_occurred (parser))
11575 cp_parser_commit_to_tentative_parse (parser);
11577 /* Keep going until we hit the `;' at the end of the simple
11578 declaration. */
11579 saw_declarator = false;
11580 while (cp_lexer_next_token_is_not (parser->lexer,
11581 CPP_SEMICOLON))
11583 cp_token *token;
11584 bool function_definition_p;
11585 tree decl;
11587 if (saw_declarator)
11589 /* If we are processing next declarator, comma is expected */
11590 token = cp_lexer_peek_token (parser->lexer);
11591 gcc_assert (token->type == CPP_COMMA);
11592 cp_lexer_consume_token (parser->lexer);
11593 if (maybe_range_for_decl)
11595 *maybe_range_for_decl = error_mark_node;
11596 if (comma_loc == UNKNOWN_LOCATION)
11597 comma_loc = token->location;
11600 else
11601 saw_declarator = true;
11603 /* Parse the init-declarator. */
11604 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11605 /*checks=*/NULL,
11606 function_definition_allowed_p,
11607 /*member_p=*/false,
11608 declares_class_or_enum,
11609 &function_definition_p,
11610 maybe_range_for_decl,
11611 &init_loc);
11612 /* If an error occurred while parsing tentatively, exit quickly.
11613 (That usually happens when in the body of a function; each
11614 statement is treated as a declaration-statement until proven
11615 otherwise.) */
11616 if (cp_parser_error_occurred (parser))
11617 goto done;
11618 /* Handle function definitions specially. */
11619 if (function_definition_p)
11621 /* If the next token is a `,', then we are probably
11622 processing something like:
11624 void f() {}, *p;
11626 which is erroneous. */
11627 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11629 cp_token *token = cp_lexer_peek_token (parser->lexer);
11630 error_at (token->location,
11631 "mixing"
11632 " declarations and function-definitions is forbidden");
11634 /* Otherwise, we're done with the list of declarators. */
11635 else
11637 pop_deferring_access_checks ();
11638 return;
11641 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11642 *maybe_range_for_decl = decl;
11643 /* The next token should be either a `,' or a `;'. */
11644 token = cp_lexer_peek_token (parser->lexer);
11645 /* If it's a `,', there are more declarators to come. */
11646 if (token->type == CPP_COMMA)
11647 /* will be consumed next time around */;
11648 /* If it's a `;', we are done. */
11649 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11650 break;
11651 /* Anything else is an error. */
11652 else
11654 /* If we have already issued an error message we don't need
11655 to issue another one. */
11656 if (decl != error_mark_node
11657 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11658 cp_parser_error (parser, "expected %<,%> or %<;%>");
11659 /* Skip tokens until we reach the end of the statement. */
11660 cp_parser_skip_to_end_of_statement (parser);
11661 /* If the next token is now a `;', consume it. */
11662 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11663 cp_lexer_consume_token (parser->lexer);
11664 goto done;
11666 /* After the first time around, a function-definition is not
11667 allowed -- even if it was OK at first. For example:
11669 int i, f() {}
11671 is not valid. */
11672 function_definition_allowed_p = false;
11675 /* Issue an error message if no declarators are present, and the
11676 decl-specifier-seq does not itself declare a class or
11677 enumeration: [dcl.dcl]/3. */
11678 if (!saw_declarator)
11680 if (cp_parser_declares_only_class_p (parser))
11682 if (!declares_class_or_enum
11683 && decl_specifiers.type
11684 && OVERLOAD_TYPE_P (decl_specifiers.type))
11685 /* Ensure an error is issued anyway when finish_decltype_type,
11686 called via cp_parser_decl_specifier_seq, returns a class or
11687 an enumeration (c++/51786). */
11688 decl_specifiers.type = NULL_TREE;
11689 shadow_tag (&decl_specifiers);
11691 /* Perform any deferred access checks. */
11692 perform_deferred_access_checks (tf_warning_or_error);
11695 /* Consume the `;'. */
11696 if (!maybe_range_for_decl)
11697 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11698 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11700 if (init_loc != UNKNOWN_LOCATION)
11701 error_at (init_loc, "initializer in range-based %<for%> loop");
11702 if (comma_loc != UNKNOWN_LOCATION)
11703 error_at (comma_loc,
11704 "multiple declarations in range-based %<for%> loop");
11707 done:
11708 pop_deferring_access_checks ();
11711 /* Parse a decl-specifier-seq.
11713 decl-specifier-seq:
11714 decl-specifier-seq [opt] decl-specifier
11715 decl-specifier attribute-specifier-seq [opt] (C++11)
11717 decl-specifier:
11718 storage-class-specifier
11719 type-specifier
11720 function-specifier
11721 friend
11722 typedef
11724 GNU Extension:
11726 decl-specifier:
11727 attributes
11729 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11731 The parser flags FLAGS is used to control type-specifier parsing.
11733 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11734 flags:
11736 1: one of the decl-specifiers is an elaborated-type-specifier
11737 (i.e., a type declaration)
11738 2: one of the decl-specifiers is an enum-specifier or a
11739 class-specifier (i.e., a type definition)
11743 static void
11744 cp_parser_decl_specifier_seq (cp_parser* parser,
11745 cp_parser_flags flags,
11746 cp_decl_specifier_seq *decl_specs,
11747 int* declares_class_or_enum)
11749 bool constructor_possible_p = !parser->in_declarator_p;
11750 bool found_decl_spec = false;
11751 cp_token *start_token = NULL;
11752 cp_decl_spec ds;
11754 /* Clear DECL_SPECS. */
11755 clear_decl_specs (decl_specs);
11757 /* Assume no class or enumeration type is declared. */
11758 *declares_class_or_enum = 0;
11760 /* Keep reading specifiers until there are no more to read. */
11761 while (true)
11763 bool constructor_p;
11764 cp_token *token;
11765 ds = ds_last;
11767 /* Peek at the next token. */
11768 token = cp_lexer_peek_token (parser->lexer);
11770 /* Save the first token of the decl spec list for error
11771 reporting. */
11772 if (!start_token)
11773 start_token = token;
11774 /* Handle attributes. */
11775 if (cp_next_tokens_can_be_attribute_p (parser))
11777 /* Parse the attributes. */
11778 tree attrs = cp_parser_attributes_opt (parser);
11780 /* In a sequence of declaration specifiers, c++11 attributes
11781 appertain to the type that precede them. In that case
11782 [dcl.spec]/1 says:
11784 The attribute-specifier-seq affects the type only for
11785 the declaration it appears in, not other declarations
11786 involving the same type.
11788 But for now let's force the user to position the
11789 attribute either at the beginning of the declaration or
11790 after the declarator-id, which would clearly mean that it
11791 applies to the declarator. */
11792 if (cxx11_attribute_p (attrs))
11794 if (!found_decl_spec)
11795 /* The c++11 attribute is at the beginning of the
11796 declaration. It appertains to the entity being
11797 declared. */;
11798 else
11800 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11802 /* This is an attribute following a
11803 class-specifier. */
11804 if (decl_specs->type_definition_p)
11805 warn_misplaced_attr_for_class_type (token->location,
11806 decl_specs->type);
11807 attrs = NULL_TREE;
11809 else
11811 decl_specs->std_attributes
11812 = chainon (decl_specs->std_attributes,
11813 attrs);
11814 if (decl_specs->locations[ds_std_attribute] == 0)
11815 decl_specs->locations[ds_std_attribute] = token->location;
11817 continue;
11821 decl_specs->attributes
11822 = chainon (decl_specs->attributes,
11823 attrs);
11824 if (decl_specs->locations[ds_attribute] == 0)
11825 decl_specs->locations[ds_attribute] = token->location;
11826 continue;
11828 /* Assume we will find a decl-specifier keyword. */
11829 found_decl_spec = true;
11830 /* If the next token is an appropriate keyword, we can simply
11831 add it to the list. */
11832 switch (token->keyword)
11834 /* decl-specifier:
11835 friend
11836 constexpr */
11837 case RID_FRIEND:
11838 if (!at_class_scope_p ())
11840 error_at (token->location, "%<friend%> used outside of class");
11841 cp_lexer_purge_token (parser->lexer);
11843 else
11845 ds = ds_friend;
11846 /* Consume the token. */
11847 cp_lexer_consume_token (parser->lexer);
11849 break;
11851 case RID_CONSTEXPR:
11852 ds = ds_constexpr;
11853 cp_lexer_consume_token (parser->lexer);
11854 break;
11856 /* function-specifier:
11857 inline
11858 virtual
11859 explicit */
11860 case RID_INLINE:
11861 case RID_VIRTUAL:
11862 case RID_EXPLICIT:
11863 cp_parser_function_specifier_opt (parser, decl_specs);
11864 break;
11866 /* decl-specifier:
11867 typedef */
11868 case RID_TYPEDEF:
11869 ds = ds_typedef;
11870 /* Consume the token. */
11871 cp_lexer_consume_token (parser->lexer);
11872 /* A constructor declarator cannot appear in a typedef. */
11873 constructor_possible_p = false;
11874 /* The "typedef" keyword can only occur in a declaration; we
11875 may as well commit at this point. */
11876 cp_parser_commit_to_tentative_parse (parser);
11878 if (decl_specs->storage_class != sc_none)
11879 decl_specs->conflicting_specifiers_p = true;
11880 break;
11882 /* storage-class-specifier:
11883 auto
11884 register
11885 static
11886 extern
11887 mutable
11889 GNU Extension:
11890 thread */
11891 case RID_AUTO:
11892 if (cxx_dialect == cxx98)
11894 /* Consume the token. */
11895 cp_lexer_consume_token (parser->lexer);
11897 /* Complain about `auto' as a storage specifier, if
11898 we're complaining about C++0x compatibility. */
11899 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
11900 " changes meaning in C++11; please remove it");
11902 /* Set the storage class anyway. */
11903 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11904 token);
11906 else
11907 /* C++0x auto type-specifier. */
11908 found_decl_spec = false;
11909 break;
11911 case RID_REGISTER:
11912 case RID_STATIC:
11913 case RID_EXTERN:
11914 case RID_MUTABLE:
11915 /* Consume the token. */
11916 cp_lexer_consume_token (parser->lexer);
11917 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11918 token);
11919 break;
11920 case RID_THREAD:
11921 /* Consume the token. */
11922 ds = ds_thread;
11923 cp_lexer_consume_token (parser->lexer);
11924 break;
11926 default:
11927 /* We did not yet find a decl-specifier yet. */
11928 found_decl_spec = false;
11929 break;
11932 if (found_decl_spec
11933 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11934 && token->keyword != RID_CONSTEXPR)
11935 error ("decl-specifier invalid in condition");
11937 if (ds != ds_last)
11938 set_and_check_decl_spec_loc (decl_specs, ds, token);
11940 /* Constructors are a special case. The `S' in `S()' is not a
11941 decl-specifier; it is the beginning of the declarator. */
11942 constructor_p
11943 = (!found_decl_spec
11944 && constructor_possible_p
11945 && (cp_parser_constructor_declarator_p
11946 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11948 /* If we don't have a DECL_SPEC yet, then we must be looking at
11949 a type-specifier. */
11950 if (!found_decl_spec && !constructor_p)
11952 int decl_spec_declares_class_or_enum;
11953 bool is_cv_qualifier;
11954 tree type_spec;
11956 type_spec
11957 = cp_parser_type_specifier (parser, flags,
11958 decl_specs,
11959 /*is_declaration=*/true,
11960 &decl_spec_declares_class_or_enum,
11961 &is_cv_qualifier);
11962 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11964 /* If this type-specifier referenced a user-defined type
11965 (a typedef, class-name, etc.), then we can't allow any
11966 more such type-specifiers henceforth.
11968 [dcl.spec]
11970 The longest sequence of decl-specifiers that could
11971 possibly be a type name is taken as the
11972 decl-specifier-seq of a declaration. The sequence shall
11973 be self-consistent as described below.
11975 [dcl.type]
11977 As a general rule, at most one type-specifier is allowed
11978 in the complete decl-specifier-seq of a declaration. The
11979 only exceptions are the following:
11981 -- const or volatile can be combined with any other
11982 type-specifier.
11984 -- signed or unsigned can be combined with char, long,
11985 short, or int.
11987 -- ..
11989 Example:
11991 typedef char* Pc;
11992 void g (const int Pc);
11994 Here, Pc is *not* part of the decl-specifier seq; it's
11995 the declarator. Therefore, once we see a type-specifier
11996 (other than a cv-qualifier), we forbid any additional
11997 user-defined types. We *do* still allow things like `int
11998 int' to be considered a decl-specifier-seq, and issue the
11999 error message later. */
12000 if (type_spec && !is_cv_qualifier)
12001 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12002 /* A constructor declarator cannot follow a type-specifier. */
12003 if (type_spec)
12005 constructor_possible_p = false;
12006 found_decl_spec = true;
12007 if (!is_cv_qualifier)
12008 decl_specs->any_type_specifiers_p = true;
12012 /* If we still do not have a DECL_SPEC, then there are no more
12013 decl-specifiers. */
12014 if (!found_decl_spec)
12015 break;
12017 decl_specs->any_specifiers_p = true;
12018 /* After we see one decl-specifier, further decl-specifiers are
12019 always optional. */
12020 flags |= CP_PARSER_FLAGS_OPTIONAL;
12023 /* Don't allow a friend specifier with a class definition. */
12024 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12025 && (*declares_class_or_enum & 2))
12026 error_at (decl_specs->locations[ds_friend],
12027 "class definition may not be declared a friend");
12030 /* Parse an (optional) storage-class-specifier.
12032 storage-class-specifier:
12033 auto
12034 register
12035 static
12036 extern
12037 mutable
12039 GNU Extension:
12041 storage-class-specifier:
12042 thread
12044 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12046 static tree
12047 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12049 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12051 case RID_AUTO:
12052 if (cxx_dialect != cxx98)
12053 return NULL_TREE;
12054 /* Fall through for C++98. */
12056 case RID_REGISTER:
12057 case RID_STATIC:
12058 case RID_EXTERN:
12059 case RID_MUTABLE:
12060 case RID_THREAD:
12061 /* Consume the token. */
12062 return cp_lexer_consume_token (parser->lexer)->u.value;
12064 default:
12065 return NULL_TREE;
12069 /* Parse an (optional) function-specifier.
12071 function-specifier:
12072 inline
12073 virtual
12074 explicit
12076 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12077 Updates DECL_SPECS, if it is non-NULL. */
12079 static tree
12080 cp_parser_function_specifier_opt (cp_parser* parser,
12081 cp_decl_specifier_seq *decl_specs)
12083 cp_token *token = cp_lexer_peek_token (parser->lexer);
12084 switch (token->keyword)
12086 case RID_INLINE:
12087 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12088 break;
12090 case RID_VIRTUAL:
12091 /* 14.5.2.3 [temp.mem]
12093 A member function template shall not be virtual. */
12094 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12095 error_at (token->location, "templates may not be %<virtual%>");
12096 else
12097 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12098 break;
12100 case RID_EXPLICIT:
12101 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12102 break;
12104 default:
12105 return NULL_TREE;
12108 /* Consume the token. */
12109 return cp_lexer_consume_token (parser->lexer)->u.value;
12112 /* Parse a linkage-specification.
12114 linkage-specification:
12115 extern string-literal { declaration-seq [opt] }
12116 extern string-literal declaration */
12118 static void
12119 cp_parser_linkage_specification (cp_parser* parser)
12121 tree linkage;
12123 /* Look for the `extern' keyword. */
12124 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12126 /* Look for the string-literal. */
12127 linkage = cp_parser_string_literal (parser, false, false);
12129 /* Transform the literal into an identifier. If the literal is a
12130 wide-character string, or contains embedded NULs, then we can't
12131 handle it as the user wants. */
12132 if (strlen (TREE_STRING_POINTER (linkage))
12133 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12135 cp_parser_error (parser, "invalid linkage-specification");
12136 /* Assume C++ linkage. */
12137 linkage = lang_name_cplusplus;
12139 else
12140 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12142 /* We're now using the new linkage. */
12143 push_lang_context (linkage);
12145 /* If the next token is a `{', then we're using the first
12146 production. */
12147 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12149 cp_ensure_no_omp_declare_simd (parser);
12151 /* Consume the `{' token. */
12152 cp_lexer_consume_token (parser->lexer);
12153 /* Parse the declarations. */
12154 cp_parser_declaration_seq_opt (parser);
12155 /* Look for the closing `}'. */
12156 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12158 /* Otherwise, there's just one declaration. */
12159 else
12161 bool saved_in_unbraced_linkage_specification_p;
12163 saved_in_unbraced_linkage_specification_p
12164 = parser->in_unbraced_linkage_specification_p;
12165 parser->in_unbraced_linkage_specification_p = true;
12166 cp_parser_declaration (parser);
12167 parser->in_unbraced_linkage_specification_p
12168 = saved_in_unbraced_linkage_specification_p;
12171 /* We're done with the linkage-specification. */
12172 pop_lang_context ();
12175 /* Parse a static_assert-declaration.
12177 static_assert-declaration:
12178 static_assert ( constant-expression , string-literal ) ;
12179 static_assert ( constant-expression ) ; (C++1Z)
12181 If MEMBER_P, this static_assert is a class member. */
12183 static void
12184 cp_parser_static_assert(cp_parser *parser, bool member_p)
12186 tree condition;
12187 tree message;
12188 cp_token *token;
12189 location_t saved_loc;
12190 bool dummy;
12192 /* Peek at the `static_assert' token so we can keep track of exactly
12193 where the static assertion started. */
12194 token = cp_lexer_peek_token (parser->lexer);
12195 saved_loc = token->location;
12197 /* Look for the `static_assert' keyword. */
12198 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12199 RT_STATIC_ASSERT))
12200 return;
12202 /* We know we are in a static assertion; commit to any tentative
12203 parse. */
12204 if (cp_parser_parsing_tentatively (parser))
12205 cp_parser_commit_to_tentative_parse (parser);
12207 /* Parse the `(' starting the static assertion condition. */
12208 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12210 /* Parse the constant-expression. Allow a non-constant expression
12211 here in order to give better diagnostics in finish_static_assert. */
12212 condition =
12213 cp_parser_constant_expression (parser,
12214 /*allow_non_constant_p=*/true,
12215 /*non_constant_p=*/&dummy);
12217 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12219 if (cxx_dialect < cxx1z)
12220 pedwarn (input_location, OPT_Wpedantic,
12221 "static_assert without a message "
12222 "only available with -std=c++1z or -std=gnu++1z");
12223 /* Eat the ')' */
12224 cp_lexer_consume_token (parser->lexer);
12225 message = build_string (1, "");
12226 TREE_TYPE (message) = char_array_type_node;
12227 fix_string_type (message);
12229 else
12231 /* Parse the separating `,'. */
12232 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12234 /* Parse the string-literal message. */
12235 message = cp_parser_string_literal (parser,
12236 /*translate=*/false,
12237 /*wide_ok=*/true);
12239 /* A `)' completes the static assertion. */
12240 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12241 cp_parser_skip_to_closing_parenthesis (parser,
12242 /*recovering=*/true,
12243 /*or_comma=*/false,
12244 /*consume_paren=*/true);
12247 /* A semicolon terminates the declaration. */
12248 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12250 /* Complete the static assertion, which may mean either processing
12251 the static assert now or saving it for template instantiation. */
12252 finish_static_assert (condition, message, saved_loc, member_p);
12255 /* Parse the expression in decltype ( expression ). */
12257 static tree
12258 cp_parser_decltype_expr (cp_parser *parser,
12259 bool &id_expression_or_member_access_p)
12261 cp_token *id_expr_start_token;
12262 tree expr;
12264 /* First, try parsing an id-expression. */
12265 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12266 cp_parser_parse_tentatively (parser);
12267 expr = cp_parser_id_expression (parser,
12268 /*template_keyword_p=*/false,
12269 /*check_dependency_p=*/true,
12270 /*template_p=*/NULL,
12271 /*declarator_p=*/false,
12272 /*optional_p=*/false);
12274 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12276 bool non_integral_constant_expression_p = false;
12277 tree id_expression = expr;
12278 cp_id_kind idk;
12279 const char *error_msg;
12281 if (identifier_p (expr))
12282 /* Lookup the name we got back from the id-expression. */
12283 expr = cp_parser_lookup_name_simple (parser, expr,
12284 id_expr_start_token->location);
12286 if (expr
12287 && expr != error_mark_node
12288 && TREE_CODE (expr) != TYPE_DECL
12289 && (TREE_CODE (expr) != BIT_NOT_EXPR
12290 || !TYPE_P (TREE_OPERAND (expr, 0)))
12291 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12293 /* Complete lookup of the id-expression. */
12294 expr = (finish_id_expression
12295 (id_expression, expr, parser->scope, &idk,
12296 /*integral_constant_expression_p=*/false,
12297 /*allow_non_integral_constant_expression_p=*/true,
12298 &non_integral_constant_expression_p,
12299 /*template_p=*/false,
12300 /*done=*/true,
12301 /*address_p=*/false,
12302 /*template_arg_p=*/false,
12303 &error_msg,
12304 id_expr_start_token->location));
12306 if (expr == error_mark_node)
12307 /* We found an id-expression, but it was something that we
12308 should not have found. This is an error, not something
12309 we can recover from, so note that we found an
12310 id-expression and we'll recover as gracefully as
12311 possible. */
12312 id_expression_or_member_access_p = true;
12315 if (expr
12316 && expr != error_mark_node
12317 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12318 /* We have an id-expression. */
12319 id_expression_or_member_access_p = true;
12322 if (!id_expression_or_member_access_p)
12324 /* Abort the id-expression parse. */
12325 cp_parser_abort_tentative_parse (parser);
12327 /* Parsing tentatively, again. */
12328 cp_parser_parse_tentatively (parser);
12330 /* Parse a class member access. */
12331 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12332 /*cast_p=*/false, /*decltype*/true,
12333 /*member_access_only_p=*/true, NULL);
12335 if (expr
12336 && expr != error_mark_node
12337 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12338 /* We have an id-expression. */
12339 id_expression_or_member_access_p = true;
12342 if (id_expression_or_member_access_p)
12343 /* We have parsed the complete id-expression or member access. */
12344 cp_parser_parse_definitely (parser);
12345 else
12347 /* Abort our attempt to parse an id-expression or member access
12348 expression. */
12349 cp_parser_abort_tentative_parse (parser);
12351 /* Parse a full expression. */
12352 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12353 /*decltype_p=*/true);
12356 return expr;
12359 /* Parse a `decltype' type. Returns the type.
12361 simple-type-specifier:
12362 decltype ( expression )
12363 C++14 proposal:
12364 decltype ( auto ) */
12366 static tree
12367 cp_parser_decltype (cp_parser *parser)
12369 tree expr;
12370 bool id_expression_or_member_access_p = false;
12371 const char *saved_message;
12372 bool saved_integral_constant_expression_p;
12373 bool saved_non_integral_constant_expression_p;
12374 bool saved_greater_than_is_operator_p;
12375 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12377 if (start_token->type == CPP_DECLTYPE)
12379 /* Already parsed. */
12380 cp_lexer_consume_token (parser->lexer);
12381 return start_token->u.value;
12384 /* Look for the `decltype' token. */
12385 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12386 return error_mark_node;
12388 /* Parse the opening `('. */
12389 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12390 return error_mark_node;
12392 /* decltype (auto) */
12393 if (cxx_dialect >= cxx14
12394 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12396 cp_lexer_consume_token (parser->lexer);
12397 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12398 return error_mark_node;
12399 expr = make_decltype_auto ();
12400 AUTO_IS_DECLTYPE (expr) = true;
12401 goto rewrite;
12404 /* Types cannot be defined in a `decltype' expression. Save away the
12405 old message. */
12406 saved_message = parser->type_definition_forbidden_message;
12408 /* And create the new one. */
12409 parser->type_definition_forbidden_message
12410 = G_("types may not be defined in %<decltype%> expressions");
12412 /* The restrictions on constant-expressions do not apply inside
12413 decltype expressions. */
12414 saved_integral_constant_expression_p
12415 = parser->integral_constant_expression_p;
12416 saved_non_integral_constant_expression_p
12417 = parser->non_integral_constant_expression_p;
12418 parser->integral_constant_expression_p = false;
12420 /* Within a parenthesized expression, a `>' token is always
12421 the greater-than operator. */
12422 saved_greater_than_is_operator_p
12423 = parser->greater_than_is_operator_p;
12424 parser->greater_than_is_operator_p = true;
12426 /* Do not actually evaluate the expression. */
12427 ++cp_unevaluated_operand;
12429 /* Do not warn about problems with the expression. */
12430 ++c_inhibit_evaluation_warnings;
12432 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12434 /* Go back to evaluating expressions. */
12435 --cp_unevaluated_operand;
12436 --c_inhibit_evaluation_warnings;
12438 /* The `>' token might be the end of a template-id or
12439 template-parameter-list now. */
12440 parser->greater_than_is_operator_p
12441 = saved_greater_than_is_operator_p;
12443 /* Restore the old message and the integral constant expression
12444 flags. */
12445 parser->type_definition_forbidden_message = saved_message;
12446 parser->integral_constant_expression_p
12447 = saved_integral_constant_expression_p;
12448 parser->non_integral_constant_expression_p
12449 = saved_non_integral_constant_expression_p;
12451 /* Parse to the closing `)'. */
12452 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12454 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12455 /*consume_paren=*/true);
12456 return error_mark_node;
12459 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12460 tf_warning_or_error);
12462 rewrite:
12463 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12464 it again. */
12465 start_token->type = CPP_DECLTYPE;
12466 start_token->u.value = expr;
12467 start_token->keyword = RID_MAX;
12468 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12470 return expr;
12473 /* Special member functions [gram.special] */
12475 /* Parse a conversion-function-id.
12477 conversion-function-id:
12478 operator conversion-type-id
12480 Returns an IDENTIFIER_NODE representing the operator. */
12482 static tree
12483 cp_parser_conversion_function_id (cp_parser* parser)
12485 tree type;
12486 tree saved_scope;
12487 tree saved_qualifying_scope;
12488 tree saved_object_scope;
12489 tree pushed_scope = NULL_TREE;
12491 /* Look for the `operator' token. */
12492 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12493 return error_mark_node;
12494 /* When we parse the conversion-type-id, the current scope will be
12495 reset. However, we need that information in able to look up the
12496 conversion function later, so we save it here. */
12497 saved_scope = parser->scope;
12498 saved_qualifying_scope = parser->qualifying_scope;
12499 saved_object_scope = parser->object_scope;
12500 /* We must enter the scope of the class so that the names of
12501 entities declared within the class are available in the
12502 conversion-type-id. For example, consider:
12504 struct S {
12505 typedef int I;
12506 operator I();
12509 S::operator I() { ... }
12511 In order to see that `I' is a type-name in the definition, we
12512 must be in the scope of `S'. */
12513 if (saved_scope)
12514 pushed_scope = push_scope (saved_scope);
12515 /* Parse the conversion-type-id. */
12516 type = cp_parser_conversion_type_id (parser);
12517 /* Leave the scope of the class, if any. */
12518 if (pushed_scope)
12519 pop_scope (pushed_scope);
12520 /* Restore the saved scope. */
12521 parser->scope = saved_scope;
12522 parser->qualifying_scope = saved_qualifying_scope;
12523 parser->object_scope = saved_object_scope;
12524 /* If the TYPE is invalid, indicate failure. */
12525 if (type == error_mark_node)
12526 return error_mark_node;
12527 return mangle_conv_op_name_for_type (type);
12530 /* Parse a conversion-type-id:
12532 conversion-type-id:
12533 type-specifier-seq conversion-declarator [opt]
12535 Returns the TYPE specified. */
12537 static tree
12538 cp_parser_conversion_type_id (cp_parser* parser)
12540 tree attributes;
12541 cp_decl_specifier_seq type_specifiers;
12542 cp_declarator *declarator;
12543 tree type_specified;
12544 const char *saved_message;
12546 /* Parse the attributes. */
12547 attributes = cp_parser_attributes_opt (parser);
12549 saved_message = parser->type_definition_forbidden_message;
12550 parser->type_definition_forbidden_message
12551 = G_("types may not be defined in a conversion-type-id");
12553 /* Parse the type-specifiers. */
12554 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12555 /*is_trailing_return=*/false,
12556 &type_specifiers);
12558 parser->type_definition_forbidden_message = saved_message;
12560 /* If that didn't work, stop. */
12561 if (type_specifiers.type == error_mark_node)
12562 return error_mark_node;
12563 /* Parse the conversion-declarator. */
12564 declarator = cp_parser_conversion_declarator_opt (parser);
12566 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12567 /*initialized=*/0, &attributes);
12568 if (attributes)
12569 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12571 /* Don't give this error when parsing tentatively. This happens to
12572 work because we always parse this definitively once. */
12573 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12574 && type_uses_auto (type_specified))
12576 if (cxx_dialect < cxx14)
12578 error ("invalid use of %<auto%> in conversion operator");
12579 return error_mark_node;
12581 else if (template_parm_scope_p ())
12582 warning (0, "use of %<auto%> in member template "
12583 "conversion operator can never be deduced");
12586 return type_specified;
12589 /* Parse an (optional) conversion-declarator.
12591 conversion-declarator:
12592 ptr-operator conversion-declarator [opt]
12596 static cp_declarator *
12597 cp_parser_conversion_declarator_opt (cp_parser* parser)
12599 enum tree_code code;
12600 tree class_type, std_attributes = NULL_TREE;
12601 cp_cv_quals cv_quals;
12603 /* We don't know if there's a ptr-operator next, or not. */
12604 cp_parser_parse_tentatively (parser);
12605 /* Try the ptr-operator. */
12606 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12607 &std_attributes);
12608 /* If it worked, look for more conversion-declarators. */
12609 if (cp_parser_parse_definitely (parser))
12611 cp_declarator *declarator;
12613 /* Parse another optional declarator. */
12614 declarator = cp_parser_conversion_declarator_opt (parser);
12616 declarator = cp_parser_make_indirect_declarator
12617 (code, class_type, cv_quals, declarator, std_attributes);
12619 return declarator;
12622 return NULL;
12625 /* Parse an (optional) ctor-initializer.
12627 ctor-initializer:
12628 : mem-initializer-list
12630 Returns TRUE iff the ctor-initializer was actually present. */
12632 static bool
12633 cp_parser_ctor_initializer_opt (cp_parser* parser)
12635 /* If the next token is not a `:', then there is no
12636 ctor-initializer. */
12637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12639 /* Do default initialization of any bases and members. */
12640 if (DECL_CONSTRUCTOR_P (current_function_decl))
12641 finish_mem_initializers (NULL_TREE);
12643 return false;
12646 /* Consume the `:' token. */
12647 cp_lexer_consume_token (parser->lexer);
12648 /* And the mem-initializer-list. */
12649 cp_parser_mem_initializer_list (parser);
12651 return true;
12654 /* Parse a mem-initializer-list.
12656 mem-initializer-list:
12657 mem-initializer ... [opt]
12658 mem-initializer ... [opt] , mem-initializer-list */
12660 static void
12661 cp_parser_mem_initializer_list (cp_parser* parser)
12663 tree mem_initializer_list = NULL_TREE;
12664 tree target_ctor = error_mark_node;
12665 cp_token *token = cp_lexer_peek_token (parser->lexer);
12667 /* Let the semantic analysis code know that we are starting the
12668 mem-initializer-list. */
12669 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12670 error_at (token->location,
12671 "only constructors take member initializers");
12673 /* Loop through the list. */
12674 while (true)
12676 tree mem_initializer;
12678 token = cp_lexer_peek_token (parser->lexer);
12679 /* Parse the mem-initializer. */
12680 mem_initializer = cp_parser_mem_initializer (parser);
12681 /* If the next token is a `...', we're expanding member initializers. */
12682 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12684 /* Consume the `...'. */
12685 cp_lexer_consume_token (parser->lexer);
12687 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12688 can be expanded but members cannot. */
12689 if (mem_initializer != error_mark_node
12690 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12692 error_at (token->location,
12693 "cannot expand initializer for member %<%D%>",
12694 TREE_PURPOSE (mem_initializer));
12695 mem_initializer = error_mark_node;
12698 /* Construct the pack expansion type. */
12699 if (mem_initializer != error_mark_node)
12700 mem_initializer = make_pack_expansion (mem_initializer);
12702 if (target_ctor != error_mark_node
12703 && mem_initializer != error_mark_node)
12705 error ("mem-initializer for %qD follows constructor delegation",
12706 TREE_PURPOSE (mem_initializer));
12707 mem_initializer = error_mark_node;
12709 /* Look for a target constructor. */
12710 if (mem_initializer != error_mark_node
12711 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12712 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12714 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12715 if (mem_initializer_list)
12717 error ("constructor delegation follows mem-initializer for %qD",
12718 TREE_PURPOSE (mem_initializer_list));
12719 mem_initializer = error_mark_node;
12721 target_ctor = mem_initializer;
12723 /* Add it to the list, unless it was erroneous. */
12724 if (mem_initializer != error_mark_node)
12726 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12727 mem_initializer_list = mem_initializer;
12729 /* If the next token is not a `,', we're done. */
12730 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12731 break;
12732 /* Consume the `,' token. */
12733 cp_lexer_consume_token (parser->lexer);
12736 /* Perform semantic analysis. */
12737 if (DECL_CONSTRUCTOR_P (current_function_decl))
12738 finish_mem_initializers (mem_initializer_list);
12741 /* Parse a mem-initializer.
12743 mem-initializer:
12744 mem-initializer-id ( expression-list [opt] )
12745 mem-initializer-id braced-init-list
12747 GNU extension:
12749 mem-initializer:
12750 ( expression-list [opt] )
12752 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12753 class) or FIELD_DECL (for a non-static data member) to initialize;
12754 the TREE_VALUE is the expression-list. An empty initialization
12755 list is represented by void_list_node. */
12757 static tree
12758 cp_parser_mem_initializer (cp_parser* parser)
12760 tree mem_initializer_id;
12761 tree expression_list;
12762 tree member;
12763 cp_token *token = cp_lexer_peek_token (parser->lexer);
12765 /* Find out what is being initialized. */
12766 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12768 permerror (token->location,
12769 "anachronistic old-style base class initializer");
12770 mem_initializer_id = NULL_TREE;
12772 else
12774 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12775 if (mem_initializer_id == error_mark_node)
12776 return mem_initializer_id;
12778 member = expand_member_init (mem_initializer_id);
12779 if (member && !DECL_P (member))
12780 in_base_initializer = 1;
12782 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12784 bool expr_non_constant_p;
12785 cp_lexer_set_source_position (parser->lexer);
12786 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12787 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12788 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12789 expression_list = build_tree_list (NULL_TREE, expression_list);
12791 else
12793 vec<tree, va_gc> *vec;
12794 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12795 /*cast_p=*/false,
12796 /*allow_expansion_p=*/true,
12797 /*non_constant_p=*/NULL);
12798 if (vec == NULL)
12799 return error_mark_node;
12800 expression_list = build_tree_list_vec (vec);
12801 release_tree_vector (vec);
12804 if (expression_list == error_mark_node)
12805 return error_mark_node;
12806 if (!expression_list)
12807 expression_list = void_type_node;
12809 in_base_initializer = 0;
12811 return member ? build_tree_list (member, expression_list) : error_mark_node;
12814 /* Parse a mem-initializer-id.
12816 mem-initializer-id:
12817 :: [opt] nested-name-specifier [opt] class-name
12818 decltype-specifier (C++11)
12819 identifier
12821 Returns a TYPE indicating the class to be initialized for the first
12822 production (and the second in C++11). Returns an IDENTIFIER_NODE
12823 indicating the data member to be initialized for the last production. */
12825 static tree
12826 cp_parser_mem_initializer_id (cp_parser* parser)
12828 bool global_scope_p;
12829 bool nested_name_specifier_p;
12830 bool template_p = false;
12831 tree id;
12833 cp_token *token = cp_lexer_peek_token (parser->lexer);
12835 /* `typename' is not allowed in this context ([temp.res]). */
12836 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12838 error_at (token->location,
12839 "keyword %<typename%> not allowed in this context (a qualified "
12840 "member initializer is implicitly a type)");
12841 cp_lexer_consume_token (parser->lexer);
12843 /* Look for the optional `::' operator. */
12844 global_scope_p
12845 = (cp_parser_global_scope_opt (parser,
12846 /*current_scope_valid_p=*/false)
12847 != NULL_TREE);
12848 /* Look for the optional nested-name-specifier. The simplest way to
12849 implement:
12851 [temp.res]
12853 The keyword `typename' is not permitted in a base-specifier or
12854 mem-initializer; in these contexts a qualified name that
12855 depends on a template-parameter is implicitly assumed to be a
12856 type name.
12858 is to assume that we have seen the `typename' keyword at this
12859 point. */
12860 nested_name_specifier_p
12861 = (cp_parser_nested_name_specifier_opt (parser,
12862 /*typename_keyword_p=*/true,
12863 /*check_dependency_p=*/true,
12864 /*type_p=*/true,
12865 /*is_declaration=*/true)
12866 != NULL_TREE);
12867 if (nested_name_specifier_p)
12868 template_p = cp_parser_optional_template_keyword (parser);
12869 /* If there is a `::' operator or a nested-name-specifier, then we
12870 are definitely looking for a class-name. */
12871 if (global_scope_p || nested_name_specifier_p)
12872 return cp_parser_class_name (parser,
12873 /*typename_keyword_p=*/true,
12874 /*template_keyword_p=*/template_p,
12875 typename_type,
12876 /*check_dependency_p=*/true,
12877 /*class_head_p=*/false,
12878 /*is_declaration=*/true);
12879 /* Otherwise, we could also be looking for an ordinary identifier. */
12880 cp_parser_parse_tentatively (parser);
12881 if (cp_lexer_next_token_is_decltype (parser->lexer))
12882 /* Try a decltype-specifier. */
12883 id = cp_parser_decltype (parser);
12884 else
12885 /* Otherwise, try a class-name. */
12886 id = cp_parser_class_name (parser,
12887 /*typename_keyword_p=*/true,
12888 /*template_keyword_p=*/false,
12889 none_type,
12890 /*check_dependency_p=*/true,
12891 /*class_head_p=*/false,
12892 /*is_declaration=*/true);
12893 /* If we found one, we're done. */
12894 if (cp_parser_parse_definitely (parser))
12895 return id;
12896 /* Otherwise, look for an ordinary identifier. */
12897 return cp_parser_identifier (parser);
12900 /* Overloading [gram.over] */
12902 /* Parse an operator-function-id.
12904 operator-function-id:
12905 operator operator
12907 Returns an IDENTIFIER_NODE for the operator which is a
12908 human-readable spelling of the identifier, e.g., `operator +'. */
12910 static tree
12911 cp_parser_operator_function_id (cp_parser* parser)
12913 /* Look for the `operator' keyword. */
12914 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12915 return error_mark_node;
12916 /* And then the name of the operator itself. */
12917 return cp_parser_operator (parser);
12920 /* Return an identifier node for a user-defined literal operator.
12921 The suffix identifier is chained to the operator name identifier. */
12923 static tree
12924 cp_literal_operator_id (const char* name)
12926 tree identifier;
12927 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12928 + strlen (name) + 10);
12929 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12930 identifier = get_identifier (buffer);
12932 return identifier;
12935 /* Parse an operator.
12937 operator:
12938 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12939 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12940 || ++ -- , ->* -> () []
12942 GNU Extensions:
12944 operator:
12945 <? >? <?= >?=
12947 Returns an IDENTIFIER_NODE for the operator which is a
12948 human-readable spelling of the identifier, e.g., `operator +'. */
12950 static tree
12951 cp_parser_operator (cp_parser* parser)
12953 tree id = NULL_TREE;
12954 cp_token *token;
12955 bool utf8 = false;
12957 /* Peek at the next token. */
12958 token = cp_lexer_peek_token (parser->lexer);
12959 /* Figure out which operator we have. */
12960 switch (token->type)
12962 case CPP_KEYWORD:
12964 enum tree_code op;
12966 /* The keyword should be either `new' or `delete'. */
12967 if (token->keyword == RID_NEW)
12968 op = NEW_EXPR;
12969 else if (token->keyword == RID_DELETE)
12970 op = DELETE_EXPR;
12971 else
12972 break;
12974 /* Consume the `new' or `delete' token. */
12975 cp_lexer_consume_token (parser->lexer);
12977 /* Peek at the next token. */
12978 token = cp_lexer_peek_token (parser->lexer);
12979 /* If it's a `[' token then this is the array variant of the
12980 operator. */
12981 if (token->type == CPP_OPEN_SQUARE)
12983 /* Consume the `[' token. */
12984 cp_lexer_consume_token (parser->lexer);
12985 /* Look for the `]' token. */
12986 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12987 id = ansi_opname (op == NEW_EXPR
12988 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12990 /* Otherwise, we have the non-array variant. */
12991 else
12992 id = ansi_opname (op);
12994 return id;
12997 case CPP_PLUS:
12998 id = ansi_opname (PLUS_EXPR);
12999 break;
13001 case CPP_MINUS:
13002 id = ansi_opname (MINUS_EXPR);
13003 break;
13005 case CPP_MULT:
13006 id = ansi_opname (MULT_EXPR);
13007 break;
13009 case CPP_DIV:
13010 id = ansi_opname (TRUNC_DIV_EXPR);
13011 break;
13013 case CPP_MOD:
13014 id = ansi_opname (TRUNC_MOD_EXPR);
13015 break;
13017 case CPP_XOR:
13018 id = ansi_opname (BIT_XOR_EXPR);
13019 break;
13021 case CPP_AND:
13022 id = ansi_opname (BIT_AND_EXPR);
13023 break;
13025 case CPP_OR:
13026 id = ansi_opname (BIT_IOR_EXPR);
13027 break;
13029 case CPP_COMPL:
13030 id = ansi_opname (BIT_NOT_EXPR);
13031 break;
13033 case CPP_NOT:
13034 id = ansi_opname (TRUTH_NOT_EXPR);
13035 break;
13037 case CPP_EQ:
13038 id = ansi_assopname (NOP_EXPR);
13039 break;
13041 case CPP_LESS:
13042 id = ansi_opname (LT_EXPR);
13043 break;
13045 case CPP_GREATER:
13046 id = ansi_opname (GT_EXPR);
13047 break;
13049 case CPP_PLUS_EQ:
13050 id = ansi_assopname (PLUS_EXPR);
13051 break;
13053 case CPP_MINUS_EQ:
13054 id = ansi_assopname (MINUS_EXPR);
13055 break;
13057 case CPP_MULT_EQ:
13058 id = ansi_assopname (MULT_EXPR);
13059 break;
13061 case CPP_DIV_EQ:
13062 id = ansi_assopname (TRUNC_DIV_EXPR);
13063 break;
13065 case CPP_MOD_EQ:
13066 id = ansi_assopname (TRUNC_MOD_EXPR);
13067 break;
13069 case CPP_XOR_EQ:
13070 id = ansi_assopname (BIT_XOR_EXPR);
13071 break;
13073 case CPP_AND_EQ:
13074 id = ansi_assopname (BIT_AND_EXPR);
13075 break;
13077 case CPP_OR_EQ:
13078 id = ansi_assopname (BIT_IOR_EXPR);
13079 break;
13081 case CPP_LSHIFT:
13082 id = ansi_opname (LSHIFT_EXPR);
13083 break;
13085 case CPP_RSHIFT:
13086 id = ansi_opname (RSHIFT_EXPR);
13087 break;
13089 case CPP_LSHIFT_EQ:
13090 id = ansi_assopname (LSHIFT_EXPR);
13091 break;
13093 case CPP_RSHIFT_EQ:
13094 id = ansi_assopname (RSHIFT_EXPR);
13095 break;
13097 case CPP_EQ_EQ:
13098 id = ansi_opname (EQ_EXPR);
13099 break;
13101 case CPP_NOT_EQ:
13102 id = ansi_opname (NE_EXPR);
13103 break;
13105 case CPP_LESS_EQ:
13106 id = ansi_opname (LE_EXPR);
13107 break;
13109 case CPP_GREATER_EQ:
13110 id = ansi_opname (GE_EXPR);
13111 break;
13113 case CPP_AND_AND:
13114 id = ansi_opname (TRUTH_ANDIF_EXPR);
13115 break;
13117 case CPP_OR_OR:
13118 id = ansi_opname (TRUTH_ORIF_EXPR);
13119 break;
13121 case CPP_PLUS_PLUS:
13122 id = ansi_opname (POSTINCREMENT_EXPR);
13123 break;
13125 case CPP_MINUS_MINUS:
13126 id = ansi_opname (PREDECREMENT_EXPR);
13127 break;
13129 case CPP_COMMA:
13130 id = ansi_opname (COMPOUND_EXPR);
13131 break;
13133 case CPP_DEREF_STAR:
13134 id = ansi_opname (MEMBER_REF);
13135 break;
13137 case CPP_DEREF:
13138 id = ansi_opname (COMPONENT_REF);
13139 break;
13141 case CPP_OPEN_PAREN:
13142 /* Consume the `('. */
13143 cp_lexer_consume_token (parser->lexer);
13144 /* Look for the matching `)'. */
13145 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13146 return ansi_opname (CALL_EXPR);
13148 case CPP_OPEN_SQUARE:
13149 /* Consume the `['. */
13150 cp_lexer_consume_token (parser->lexer);
13151 /* Look for the matching `]'. */
13152 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13153 return ansi_opname (ARRAY_REF);
13155 case CPP_UTF8STRING:
13156 case CPP_UTF8STRING_USERDEF:
13157 utf8 = true;
13158 case CPP_STRING:
13159 case CPP_WSTRING:
13160 case CPP_STRING16:
13161 case CPP_STRING32:
13162 case CPP_STRING_USERDEF:
13163 case CPP_WSTRING_USERDEF:
13164 case CPP_STRING16_USERDEF:
13165 case CPP_STRING32_USERDEF:
13167 tree str, string_tree;
13168 int sz, len;
13170 if (cxx_dialect == cxx98)
13171 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13173 /* Consume the string. */
13174 str = cp_parser_string_literal (parser, /*translate=*/true,
13175 /*wide_ok=*/true, /*lookup_udlit=*/false);
13176 if (str == error_mark_node)
13177 return error_mark_node;
13178 else if (TREE_CODE (str) == USERDEF_LITERAL)
13180 string_tree = USERDEF_LITERAL_VALUE (str);
13181 id = USERDEF_LITERAL_SUFFIX_ID (str);
13183 else
13185 string_tree = str;
13186 /* Look for the suffix identifier. */
13187 token = cp_lexer_peek_token (parser->lexer);
13188 if (token->type == CPP_NAME)
13189 id = cp_parser_identifier (parser);
13190 else if (token->type == CPP_KEYWORD)
13192 error ("unexpected keyword;"
13193 " remove space between quotes and suffix identifier");
13194 return error_mark_node;
13196 else
13198 error ("expected suffix identifier");
13199 return error_mark_node;
13202 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13203 (TREE_TYPE (TREE_TYPE (string_tree))));
13204 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13205 if (len != 0)
13207 error ("expected empty string after %<operator%> keyword");
13208 return error_mark_node;
13210 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13211 != char_type_node)
13213 error ("invalid encoding prefix in literal operator");
13214 return error_mark_node;
13216 if (id != error_mark_node)
13218 const char *name = IDENTIFIER_POINTER (id);
13219 id = cp_literal_operator_id (name);
13221 return id;
13224 default:
13225 /* Anything else is an error. */
13226 break;
13229 /* If we have selected an identifier, we need to consume the
13230 operator token. */
13231 if (id)
13232 cp_lexer_consume_token (parser->lexer);
13233 /* Otherwise, no valid operator name was present. */
13234 else
13236 cp_parser_error (parser, "expected operator");
13237 id = error_mark_node;
13240 return id;
13243 /* Parse a template-declaration.
13245 template-declaration:
13246 export [opt] template < template-parameter-list > declaration
13248 If MEMBER_P is TRUE, this template-declaration occurs within a
13249 class-specifier.
13251 The grammar rule given by the standard isn't correct. What
13252 is really meant is:
13254 template-declaration:
13255 export [opt] template-parameter-list-seq
13256 decl-specifier-seq [opt] init-declarator [opt] ;
13257 export [opt] template-parameter-list-seq
13258 function-definition
13260 template-parameter-list-seq:
13261 template-parameter-list-seq [opt]
13262 template < template-parameter-list > */
13264 static void
13265 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13267 /* Check for `export'. */
13268 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13270 /* Consume the `export' token. */
13271 cp_lexer_consume_token (parser->lexer);
13272 /* Warn that we do not support `export'. */
13273 warning (0, "keyword %<export%> not implemented, and will be ignored");
13276 cp_parser_template_declaration_after_export (parser, member_p);
13279 /* Parse a template-parameter-list.
13281 template-parameter-list:
13282 template-parameter
13283 template-parameter-list , template-parameter
13285 Returns a TREE_LIST. Each node represents a template parameter.
13286 The nodes are connected via their TREE_CHAINs. */
13288 static tree
13289 cp_parser_template_parameter_list (cp_parser* parser)
13291 tree parameter_list = NULL_TREE;
13293 begin_template_parm_list ();
13295 /* The loop below parses the template parms. We first need to know
13296 the total number of template parms to be able to compute proper
13297 canonical types of each dependent type. So after the loop, when
13298 we know the total number of template parms,
13299 end_template_parm_list computes the proper canonical types and
13300 fixes up the dependent types accordingly. */
13301 while (true)
13303 tree parameter;
13304 bool is_non_type;
13305 bool is_parameter_pack;
13306 location_t parm_loc;
13308 /* Parse the template-parameter. */
13309 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13310 parameter = cp_parser_template_parameter (parser,
13311 &is_non_type,
13312 &is_parameter_pack);
13313 /* Add it to the list. */
13314 if (parameter != error_mark_node)
13315 parameter_list = process_template_parm (parameter_list,
13316 parm_loc,
13317 parameter,
13318 is_non_type,
13319 is_parameter_pack);
13320 else
13322 tree err_parm = build_tree_list (parameter, parameter);
13323 parameter_list = chainon (parameter_list, err_parm);
13326 /* If the next token is not a `,', we're done. */
13327 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13328 break;
13329 /* Otherwise, consume the `,' token. */
13330 cp_lexer_consume_token (parser->lexer);
13333 return end_template_parm_list (parameter_list);
13336 /* Parse a template-parameter.
13338 template-parameter:
13339 type-parameter
13340 parameter-declaration
13342 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13343 the parameter. The TREE_PURPOSE is the default value, if any.
13344 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13345 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13346 set to true iff this parameter is a parameter pack. */
13348 static tree
13349 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13350 bool *is_parameter_pack)
13352 cp_token *token;
13353 cp_parameter_declarator *parameter_declarator;
13354 cp_declarator *id_declarator;
13355 tree parm;
13357 /* Assume it is a type parameter or a template parameter. */
13358 *is_non_type = false;
13359 /* Assume it not a parameter pack. */
13360 *is_parameter_pack = false;
13361 /* Peek at the next token. */
13362 token = cp_lexer_peek_token (parser->lexer);
13363 /* If it is `class' or `template', we have a type-parameter. */
13364 if (token->keyword == RID_TEMPLATE)
13365 return cp_parser_type_parameter (parser, is_parameter_pack);
13366 /* If it is `class' or `typename' we do not know yet whether it is a
13367 type parameter or a non-type parameter. Consider:
13369 template <typename T, typename T::X X> ...
13373 template <class C, class D*> ...
13375 Here, the first parameter is a type parameter, and the second is
13376 a non-type parameter. We can tell by looking at the token after
13377 the identifier -- if it is a `,', `=', or `>' then we have a type
13378 parameter. */
13379 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13381 /* Peek at the token after `class' or `typename'. */
13382 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13383 /* If it's an ellipsis, we have a template type parameter
13384 pack. */
13385 if (token->type == CPP_ELLIPSIS)
13386 return cp_parser_type_parameter (parser, is_parameter_pack);
13387 /* If it's an identifier, skip it. */
13388 if (token->type == CPP_NAME)
13389 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13390 /* Now, see if the token looks like the end of a template
13391 parameter. */
13392 if (token->type == CPP_COMMA
13393 || token->type == CPP_EQ
13394 || token->type == CPP_GREATER)
13395 return cp_parser_type_parameter (parser, is_parameter_pack);
13398 /* Otherwise, it is a non-type parameter.
13400 [temp.param]
13402 When parsing a default template-argument for a non-type
13403 template-parameter, the first non-nested `>' is taken as the end
13404 of the template parameter-list rather than a greater-than
13405 operator. */
13406 *is_non_type = true;
13407 parameter_declarator
13408 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13409 /*parenthesized_p=*/NULL);
13411 if (!parameter_declarator)
13412 return error_mark_node;
13414 /* If the parameter declaration is marked as a parameter pack, set
13415 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13416 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13417 grokdeclarator. */
13418 if (parameter_declarator->declarator
13419 && parameter_declarator->declarator->parameter_pack_p)
13421 *is_parameter_pack = true;
13422 parameter_declarator->declarator->parameter_pack_p = false;
13425 if (parameter_declarator->default_argument)
13427 /* Can happen in some cases of erroneous input (c++/34892). */
13428 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13429 /* Consume the `...' for better error recovery. */
13430 cp_lexer_consume_token (parser->lexer);
13432 /* If the next token is an ellipsis, and we don't already have it
13433 marked as a parameter pack, then we have a parameter pack (that
13434 has no declarator). */
13435 else if (!*is_parameter_pack
13436 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13437 && (declarator_can_be_parameter_pack
13438 (parameter_declarator->declarator)))
13440 /* Consume the `...'. */
13441 cp_lexer_consume_token (parser->lexer);
13442 maybe_warn_variadic_templates ();
13444 *is_parameter_pack = true;
13446 /* We might end up with a pack expansion as the type of the non-type
13447 template parameter, in which case this is a non-type template
13448 parameter pack. */
13449 else if (parameter_declarator->decl_specifiers.type
13450 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13452 *is_parameter_pack = true;
13453 parameter_declarator->decl_specifiers.type =
13454 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13457 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13459 /* Parameter packs cannot have default arguments. However, a
13460 user may try to do so, so we'll parse them and give an
13461 appropriate diagnostic here. */
13463 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13465 /* Find the name of the parameter pack. */
13466 id_declarator = parameter_declarator->declarator;
13467 while (id_declarator && id_declarator->kind != cdk_id)
13468 id_declarator = id_declarator->declarator;
13470 if (id_declarator && id_declarator->kind == cdk_id)
13471 error_at (start_token->location,
13472 "template parameter pack %qD cannot have a default argument",
13473 id_declarator->u.id.unqualified_name);
13474 else
13475 error_at (start_token->location,
13476 "template parameter pack cannot have a default argument");
13478 /* Parse the default argument, but throw away the result. */
13479 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13482 parm = grokdeclarator (parameter_declarator->declarator,
13483 &parameter_declarator->decl_specifiers,
13484 TPARM, /*initialized=*/0,
13485 /*attrlist=*/NULL);
13486 if (parm == error_mark_node)
13487 return error_mark_node;
13489 return build_tree_list (parameter_declarator->default_argument, parm);
13492 /* Parse a type-parameter.
13494 type-parameter:
13495 class identifier [opt]
13496 class identifier [opt] = type-id
13497 typename identifier [opt]
13498 typename identifier [opt] = type-id
13499 template < template-parameter-list > class identifier [opt]
13500 template < template-parameter-list > class identifier [opt]
13501 = id-expression
13503 GNU Extension (variadic templates):
13505 type-parameter:
13506 class ... identifier [opt]
13507 typename ... identifier [opt]
13509 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13510 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13511 the declaration of the parameter.
13513 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13515 static tree
13516 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13518 cp_token *token;
13519 tree parameter;
13521 /* Look for a keyword to tell us what kind of parameter this is. */
13522 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13523 if (!token)
13524 return error_mark_node;
13526 switch (token->keyword)
13528 case RID_CLASS:
13529 case RID_TYPENAME:
13531 tree identifier;
13532 tree default_argument;
13534 /* If the next token is an ellipsis, we have a template
13535 argument pack. */
13536 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13538 /* Consume the `...' token. */
13539 cp_lexer_consume_token (parser->lexer);
13540 maybe_warn_variadic_templates ();
13542 *is_parameter_pack = true;
13545 /* If the next token is an identifier, then it names the
13546 parameter. */
13547 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13548 identifier = cp_parser_identifier (parser);
13549 else
13550 identifier = NULL_TREE;
13552 /* Create the parameter. */
13553 parameter = finish_template_type_parm (class_type_node, identifier);
13555 /* If the next token is an `=', we have a default argument. */
13556 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13558 /* Consume the `=' token. */
13559 cp_lexer_consume_token (parser->lexer);
13560 /* Parse the default-argument. */
13561 push_deferring_access_checks (dk_no_deferred);
13562 default_argument = cp_parser_type_id (parser);
13564 /* Template parameter packs cannot have default
13565 arguments. */
13566 if (*is_parameter_pack)
13568 if (identifier)
13569 error_at (token->location,
13570 "template parameter pack %qD cannot have a "
13571 "default argument", identifier);
13572 else
13573 error_at (token->location,
13574 "template parameter packs cannot have "
13575 "default arguments");
13576 default_argument = NULL_TREE;
13578 else if (check_for_bare_parameter_packs (default_argument))
13579 default_argument = error_mark_node;
13580 pop_deferring_access_checks ();
13582 else
13583 default_argument = NULL_TREE;
13585 /* Create the combined representation of the parameter and the
13586 default argument. */
13587 parameter = build_tree_list (default_argument, parameter);
13589 break;
13591 case RID_TEMPLATE:
13593 tree identifier;
13594 tree default_argument;
13596 /* Look for the `<'. */
13597 cp_parser_require (parser, CPP_LESS, RT_LESS);
13598 /* Parse the template-parameter-list. */
13599 cp_parser_template_parameter_list (parser);
13600 /* Look for the `>'. */
13601 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13602 /* Look for the `class' or 'typename' keywords. */
13603 cp_parser_type_parameter_key (parser);
13604 /* If the next token is an ellipsis, we have a template
13605 argument pack. */
13606 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13608 /* Consume the `...' token. */
13609 cp_lexer_consume_token (parser->lexer);
13610 maybe_warn_variadic_templates ();
13612 *is_parameter_pack = true;
13614 /* If the next token is an `=', then there is a
13615 default-argument. If the next token is a `>', we are at
13616 the end of the parameter-list. If the next token is a `,',
13617 then we are at the end of this parameter. */
13618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13619 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13620 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13622 identifier = cp_parser_identifier (parser);
13623 /* Treat invalid names as if the parameter were nameless. */
13624 if (identifier == error_mark_node)
13625 identifier = NULL_TREE;
13627 else
13628 identifier = NULL_TREE;
13630 /* Create the template parameter. */
13631 parameter = finish_template_template_parm (class_type_node,
13632 identifier);
13634 /* If the next token is an `=', then there is a
13635 default-argument. */
13636 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13638 bool is_template;
13640 /* Consume the `='. */
13641 cp_lexer_consume_token (parser->lexer);
13642 /* Parse the id-expression. */
13643 push_deferring_access_checks (dk_no_deferred);
13644 /* save token before parsing the id-expression, for error
13645 reporting */
13646 token = cp_lexer_peek_token (parser->lexer);
13647 default_argument
13648 = cp_parser_id_expression (parser,
13649 /*template_keyword_p=*/false,
13650 /*check_dependency_p=*/true,
13651 /*template_p=*/&is_template,
13652 /*declarator_p=*/false,
13653 /*optional_p=*/false);
13654 if (TREE_CODE (default_argument) == TYPE_DECL)
13655 /* If the id-expression was a template-id that refers to
13656 a template-class, we already have the declaration here,
13657 so no further lookup is needed. */
13659 else
13660 /* Look up the name. */
13661 default_argument
13662 = cp_parser_lookup_name (parser, default_argument,
13663 none_type,
13664 /*is_template=*/is_template,
13665 /*is_namespace=*/false,
13666 /*check_dependency=*/true,
13667 /*ambiguous_decls=*/NULL,
13668 token->location);
13669 /* See if the default argument is valid. */
13670 default_argument
13671 = check_template_template_default_arg (default_argument);
13673 /* Template parameter packs cannot have default
13674 arguments. */
13675 if (*is_parameter_pack)
13677 if (identifier)
13678 error_at (token->location,
13679 "template parameter pack %qD cannot "
13680 "have a default argument",
13681 identifier);
13682 else
13683 error_at (token->location, "template parameter packs cannot "
13684 "have default arguments");
13685 default_argument = NULL_TREE;
13687 pop_deferring_access_checks ();
13689 else
13690 default_argument = NULL_TREE;
13692 /* Create the combined representation of the parameter and the
13693 default argument. */
13694 parameter = build_tree_list (default_argument, parameter);
13696 break;
13698 default:
13699 gcc_unreachable ();
13700 break;
13703 return parameter;
13706 /* Parse a template-id.
13708 template-id:
13709 template-name < template-argument-list [opt] >
13711 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13712 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13713 returned. Otherwise, if the template-name names a function, or set
13714 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13715 names a class, returns a TYPE_DECL for the specialization.
13717 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13718 uninstantiated templates. */
13720 static tree
13721 cp_parser_template_id (cp_parser *parser,
13722 bool template_keyword_p,
13723 bool check_dependency_p,
13724 enum tag_types tag_type,
13725 bool is_declaration)
13727 int i;
13728 tree templ;
13729 tree arguments;
13730 tree template_id;
13731 cp_token_position start_of_id = 0;
13732 deferred_access_check *chk;
13733 vec<deferred_access_check, va_gc> *access_check;
13734 cp_token *next_token = NULL, *next_token_2 = NULL;
13735 bool is_identifier;
13737 /* If the next token corresponds to a template-id, there is no need
13738 to reparse it. */
13739 next_token = cp_lexer_peek_token (parser->lexer);
13740 if (next_token->type == CPP_TEMPLATE_ID)
13742 struct tree_check *check_value;
13744 /* Get the stored value. */
13745 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13746 /* Perform any access checks that were deferred. */
13747 access_check = check_value->checks;
13748 if (access_check)
13750 FOR_EACH_VEC_ELT (*access_check, i, chk)
13751 perform_or_defer_access_check (chk->binfo,
13752 chk->decl,
13753 chk->diag_decl,
13754 tf_warning_or_error);
13756 /* Return the stored value. */
13757 return check_value->value;
13760 /* Avoid performing name lookup if there is no possibility of
13761 finding a template-id. */
13762 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13763 || (next_token->type == CPP_NAME
13764 && !cp_parser_nth_token_starts_template_argument_list_p
13765 (parser, 2)))
13767 cp_parser_error (parser, "expected template-id");
13768 return error_mark_node;
13771 /* Remember where the template-id starts. */
13772 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13773 start_of_id = cp_lexer_token_position (parser->lexer, false);
13775 push_deferring_access_checks (dk_deferred);
13777 /* Parse the template-name. */
13778 is_identifier = false;
13779 templ = cp_parser_template_name (parser, template_keyword_p,
13780 check_dependency_p,
13781 is_declaration,
13782 tag_type,
13783 &is_identifier);
13784 if (templ == error_mark_node || is_identifier)
13786 pop_deferring_access_checks ();
13787 return templ;
13790 /* If we find the sequence `[:' after a template-name, it's probably
13791 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13792 parse correctly the argument list. */
13793 next_token = cp_lexer_peek_token (parser->lexer);
13794 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13795 if (next_token->type == CPP_OPEN_SQUARE
13796 && next_token->flags & DIGRAPH
13797 && next_token_2->type == CPP_COLON
13798 && !(next_token_2->flags & PREV_WHITE))
13800 cp_parser_parse_tentatively (parser);
13801 /* Change `:' into `::'. */
13802 next_token_2->type = CPP_SCOPE;
13803 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13804 CPP_LESS. */
13805 cp_lexer_consume_token (parser->lexer);
13807 /* Parse the arguments. */
13808 arguments = cp_parser_enclosed_template_argument_list (parser);
13809 if (!cp_parser_parse_definitely (parser))
13811 /* If we couldn't parse an argument list, then we revert our changes
13812 and return simply an error. Maybe this is not a template-id
13813 after all. */
13814 next_token_2->type = CPP_COLON;
13815 cp_parser_error (parser, "expected %<<%>");
13816 pop_deferring_access_checks ();
13817 return error_mark_node;
13819 /* Otherwise, emit an error about the invalid digraph, but continue
13820 parsing because we got our argument list. */
13821 if (permerror (next_token->location,
13822 "%<<::%> cannot begin a template-argument list"))
13824 static bool hint = false;
13825 inform (next_token->location,
13826 "%<<:%> is an alternate spelling for %<[%>."
13827 " Insert whitespace between %<<%> and %<::%>");
13828 if (!hint && !flag_permissive)
13830 inform (next_token->location, "(if you use %<-fpermissive%> "
13831 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13832 "accept your code)");
13833 hint = true;
13837 else
13839 /* Look for the `<' that starts the template-argument-list. */
13840 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13842 pop_deferring_access_checks ();
13843 return error_mark_node;
13845 /* Parse the arguments. */
13846 arguments = cp_parser_enclosed_template_argument_list (parser);
13849 /* Build a representation of the specialization. */
13850 if (identifier_p (templ))
13851 template_id = build_min_nt_loc (next_token->location,
13852 TEMPLATE_ID_EXPR,
13853 templ, arguments);
13854 else if (DECL_TYPE_TEMPLATE_P (templ)
13855 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13857 bool entering_scope;
13858 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13859 template (rather than some instantiation thereof) only if
13860 is not nested within some other construct. For example, in
13861 "template <typename T> void f(T) { A<T>::", A<T> is just an
13862 instantiation of A. */
13863 entering_scope = (template_parm_scope_p ()
13864 && cp_lexer_next_token_is (parser->lexer,
13865 CPP_SCOPE));
13866 template_id
13867 = finish_template_type (templ, arguments, entering_scope);
13869 else if (variable_template_p (templ))
13871 template_id = lookup_template_variable (templ, arguments);
13873 else
13875 /* If it's not a class-template or a template-template, it should be
13876 a function-template. */
13877 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13878 || TREE_CODE (templ) == OVERLOAD
13879 || BASELINK_P (templ)));
13881 template_id = lookup_template_function (templ, arguments);
13884 /* If parsing tentatively, replace the sequence of tokens that makes
13885 up the template-id with a CPP_TEMPLATE_ID token. That way,
13886 should we re-parse the token stream, we will not have to repeat
13887 the effort required to do the parse, nor will we issue duplicate
13888 error messages about problems during instantiation of the
13889 template. */
13890 if (start_of_id
13891 /* Don't do this if we had a parse error in a declarator; re-parsing
13892 might succeed if a name changes meaning (60361). */
13893 && !(cp_parser_error_occurred (parser)
13894 && cp_parser_parsing_tentatively (parser)
13895 && parser->in_declarator_p))
13897 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13899 /* Reset the contents of the START_OF_ID token. */
13900 token->type = CPP_TEMPLATE_ID;
13901 /* Retrieve any deferred checks. Do not pop this access checks yet
13902 so the memory will not be reclaimed during token replacing below. */
13903 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13904 token->u.tree_check_value->value = template_id;
13905 token->u.tree_check_value->checks = get_deferred_access_checks ();
13906 token->keyword = RID_MAX;
13908 /* Purge all subsequent tokens. */
13909 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13911 /* ??? Can we actually assume that, if template_id ==
13912 error_mark_node, we will have issued a diagnostic to the
13913 user, as opposed to simply marking the tentative parse as
13914 failed? */
13915 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13916 error_at (token->location, "parse error in template argument list");
13919 pop_to_parent_deferring_access_checks ();
13920 return template_id;
13923 /* Parse a template-name.
13925 template-name:
13926 identifier
13928 The standard should actually say:
13930 template-name:
13931 identifier
13932 operator-function-id
13934 A defect report has been filed about this issue.
13936 A conversion-function-id cannot be a template name because they cannot
13937 be part of a template-id. In fact, looking at this code:
13939 a.operator K<int>()
13941 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13942 It is impossible to call a templated conversion-function-id with an
13943 explicit argument list, since the only allowed template parameter is
13944 the type to which it is converting.
13946 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13947 `template' keyword, in a construction like:
13949 T::template f<3>()
13951 In that case `f' is taken to be a template-name, even though there
13952 is no way of knowing for sure.
13954 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13955 name refers to a set of overloaded functions, at least one of which
13956 is a template, or an IDENTIFIER_NODE with the name of the template,
13957 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13958 names are looked up inside uninstantiated templates. */
13960 static tree
13961 cp_parser_template_name (cp_parser* parser,
13962 bool template_keyword_p,
13963 bool check_dependency_p,
13964 bool is_declaration,
13965 enum tag_types tag_type,
13966 bool *is_identifier)
13968 tree identifier;
13969 tree decl;
13970 tree fns;
13971 cp_token *token = cp_lexer_peek_token (parser->lexer);
13973 /* If the next token is `operator', then we have either an
13974 operator-function-id or a conversion-function-id. */
13975 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13977 /* We don't know whether we're looking at an
13978 operator-function-id or a conversion-function-id. */
13979 cp_parser_parse_tentatively (parser);
13980 /* Try an operator-function-id. */
13981 identifier = cp_parser_operator_function_id (parser);
13982 /* If that didn't work, try a conversion-function-id. */
13983 if (!cp_parser_parse_definitely (parser))
13985 cp_parser_error (parser, "expected template-name");
13986 return error_mark_node;
13989 /* Look for the identifier. */
13990 else
13991 identifier = cp_parser_identifier (parser);
13993 /* If we didn't find an identifier, we don't have a template-id. */
13994 if (identifier == error_mark_node)
13995 return error_mark_node;
13997 /* If the name immediately followed the `template' keyword, then it
13998 is a template-name. However, if the next token is not `<', then
13999 we do not treat it as a template-name, since it is not being used
14000 as part of a template-id. This enables us to handle constructs
14001 like:
14003 template <typename T> struct S { S(); };
14004 template <typename T> S<T>::S();
14006 correctly. We would treat `S' as a template -- if it were `S<T>'
14007 -- but we do not if there is no `<'. */
14009 if (processing_template_decl
14010 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
14012 /* In a declaration, in a dependent context, we pretend that the
14013 "template" keyword was present in order to improve error
14014 recovery. For example, given:
14016 template <typename T> void f(T::X<int>);
14018 we want to treat "X<int>" as a template-id. */
14019 if (is_declaration
14020 && !template_keyword_p
14021 && parser->scope && TYPE_P (parser->scope)
14022 && check_dependency_p
14023 && dependent_scope_p (parser->scope)
14024 /* Do not do this for dtors (or ctors), since they never
14025 need the template keyword before their name. */
14026 && !constructor_name_p (identifier, parser->scope))
14028 cp_token_position start = 0;
14030 /* Explain what went wrong. */
14031 error_at (token->location, "non-template %qD used as template",
14032 identifier);
14033 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14034 parser->scope, identifier);
14035 /* If parsing tentatively, find the location of the "<" token. */
14036 if (cp_parser_simulate_error (parser))
14037 start = cp_lexer_token_position (parser->lexer, true);
14038 /* Parse the template arguments so that we can issue error
14039 messages about them. */
14040 cp_lexer_consume_token (parser->lexer);
14041 cp_parser_enclosed_template_argument_list (parser);
14042 /* Skip tokens until we find a good place from which to
14043 continue parsing. */
14044 cp_parser_skip_to_closing_parenthesis (parser,
14045 /*recovering=*/true,
14046 /*or_comma=*/true,
14047 /*consume_paren=*/false);
14048 /* If parsing tentatively, permanently remove the
14049 template argument list. That will prevent duplicate
14050 error messages from being issued about the missing
14051 "template" keyword. */
14052 if (start)
14053 cp_lexer_purge_tokens_after (parser->lexer, start);
14054 if (is_identifier)
14055 *is_identifier = true;
14056 return identifier;
14059 /* If the "template" keyword is present, then there is generally
14060 no point in doing name-lookup, so we just return IDENTIFIER.
14061 But, if the qualifying scope is non-dependent then we can
14062 (and must) do name-lookup normally. */
14063 if (template_keyword_p
14064 && (!parser->scope
14065 || (TYPE_P (parser->scope)
14066 && dependent_type_p (parser->scope))))
14067 return identifier;
14070 /* Look up the name. */
14071 decl = cp_parser_lookup_name (parser, identifier,
14072 tag_type,
14073 /*is_template=*/true,
14074 /*is_namespace=*/false,
14075 check_dependency_p,
14076 /*ambiguous_decls=*/NULL,
14077 token->location);
14079 decl = strip_using_decl (decl);
14081 /* If DECL is a template, then the name was a template-name. */
14082 if (TREE_CODE (decl) == TEMPLATE_DECL)
14084 if (TREE_DEPRECATED (decl)
14085 && deprecated_state != DEPRECATED_SUPPRESS)
14086 warn_deprecated_use (decl, NULL_TREE);
14088 else
14090 tree fn = NULL_TREE;
14092 /* The standard does not explicitly indicate whether a name that
14093 names a set of overloaded declarations, some of which are
14094 templates, is a template-name. However, such a name should
14095 be a template-name; otherwise, there is no way to form a
14096 template-id for the overloaded templates. */
14097 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14098 if (TREE_CODE (fns) == OVERLOAD)
14099 for (fn = fns; fn; fn = OVL_NEXT (fn))
14100 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14101 break;
14103 if (!fn)
14105 /* The name does not name a template. */
14106 cp_parser_error (parser, "expected template-name");
14107 return error_mark_node;
14111 /* If DECL is dependent, and refers to a function, then just return
14112 its name; we will look it up again during template instantiation. */
14113 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14115 tree scope = ovl_scope (decl);
14116 if (TYPE_P (scope) && dependent_type_p (scope))
14117 return identifier;
14120 return decl;
14123 /* Parse a template-argument-list.
14125 template-argument-list:
14126 template-argument ... [opt]
14127 template-argument-list , template-argument ... [opt]
14129 Returns a TREE_VEC containing the arguments. */
14131 static tree
14132 cp_parser_template_argument_list (cp_parser* parser)
14134 tree fixed_args[10];
14135 unsigned n_args = 0;
14136 unsigned alloced = 10;
14137 tree *arg_ary = fixed_args;
14138 tree vec;
14139 bool saved_in_template_argument_list_p;
14140 bool saved_ice_p;
14141 bool saved_non_ice_p;
14143 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14144 parser->in_template_argument_list_p = true;
14145 /* Even if the template-id appears in an integral
14146 constant-expression, the contents of the argument list do
14147 not. */
14148 saved_ice_p = parser->integral_constant_expression_p;
14149 parser->integral_constant_expression_p = false;
14150 saved_non_ice_p = parser->non_integral_constant_expression_p;
14151 parser->non_integral_constant_expression_p = false;
14153 /* Parse the arguments. */
14156 tree argument;
14158 if (n_args)
14159 /* Consume the comma. */
14160 cp_lexer_consume_token (parser->lexer);
14162 /* Parse the template-argument. */
14163 argument = cp_parser_template_argument (parser);
14165 /* If the next token is an ellipsis, we're expanding a template
14166 argument pack. */
14167 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14169 if (argument == error_mark_node)
14171 cp_token *token = cp_lexer_peek_token (parser->lexer);
14172 error_at (token->location,
14173 "expected parameter pack before %<...%>");
14175 /* Consume the `...' token. */
14176 cp_lexer_consume_token (parser->lexer);
14178 /* Make the argument into a TYPE_PACK_EXPANSION or
14179 EXPR_PACK_EXPANSION. */
14180 argument = make_pack_expansion (argument);
14183 if (n_args == alloced)
14185 alloced *= 2;
14187 if (arg_ary == fixed_args)
14189 arg_ary = XNEWVEC (tree, alloced);
14190 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14192 else
14193 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14195 arg_ary[n_args++] = argument;
14197 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14199 vec = make_tree_vec (n_args);
14201 while (n_args--)
14202 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14204 if (arg_ary != fixed_args)
14205 free (arg_ary);
14206 parser->non_integral_constant_expression_p = saved_non_ice_p;
14207 parser->integral_constant_expression_p = saved_ice_p;
14208 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14209 #ifdef ENABLE_CHECKING
14210 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14211 #endif
14212 return vec;
14215 /* Parse a template-argument.
14217 template-argument:
14218 assignment-expression
14219 type-id
14220 id-expression
14222 The representation is that of an assignment-expression, type-id, or
14223 id-expression -- except that the qualified id-expression is
14224 evaluated, so that the value returned is either a DECL or an
14225 OVERLOAD.
14227 Although the standard says "assignment-expression", it forbids
14228 throw-expressions or assignments in the template argument.
14229 Therefore, we use "conditional-expression" instead. */
14231 static tree
14232 cp_parser_template_argument (cp_parser* parser)
14234 tree argument;
14235 bool template_p;
14236 bool address_p;
14237 bool maybe_type_id = false;
14238 cp_token *token = NULL, *argument_start_token = NULL;
14239 location_t loc = 0;
14240 cp_id_kind idk;
14242 /* There's really no way to know what we're looking at, so we just
14243 try each alternative in order.
14245 [temp.arg]
14247 In a template-argument, an ambiguity between a type-id and an
14248 expression is resolved to a type-id, regardless of the form of
14249 the corresponding template-parameter.
14251 Therefore, we try a type-id first. */
14252 cp_parser_parse_tentatively (parser);
14253 argument = cp_parser_template_type_arg (parser);
14254 /* If there was no error parsing the type-id but the next token is a
14255 '>>', our behavior depends on which dialect of C++ we're
14256 parsing. In C++98, we probably found a typo for '> >'. But there
14257 are type-id which are also valid expressions. For instance:
14259 struct X { int operator >> (int); };
14260 template <int V> struct Foo {};
14261 Foo<X () >> 5> r;
14263 Here 'X()' is a valid type-id of a function type, but the user just
14264 wanted to write the expression "X() >> 5". Thus, we remember that we
14265 found a valid type-id, but we still try to parse the argument as an
14266 expression to see what happens.
14268 In C++0x, the '>>' will be considered two separate '>'
14269 tokens. */
14270 if (!cp_parser_error_occurred (parser)
14271 && cxx_dialect == cxx98
14272 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14274 maybe_type_id = true;
14275 cp_parser_abort_tentative_parse (parser);
14277 else
14279 /* If the next token isn't a `,' or a `>', then this argument wasn't
14280 really finished. This means that the argument is not a valid
14281 type-id. */
14282 if (!cp_parser_next_token_ends_template_argument_p (parser))
14283 cp_parser_error (parser, "expected template-argument");
14284 /* If that worked, we're done. */
14285 if (cp_parser_parse_definitely (parser))
14286 return argument;
14288 /* We're still not sure what the argument will be. */
14289 cp_parser_parse_tentatively (parser);
14290 /* Try a template. */
14291 argument_start_token = cp_lexer_peek_token (parser->lexer);
14292 argument = cp_parser_id_expression (parser,
14293 /*template_keyword_p=*/false,
14294 /*check_dependency_p=*/true,
14295 &template_p,
14296 /*declarator_p=*/false,
14297 /*optional_p=*/false);
14298 /* If the next token isn't a `,' or a `>', then this argument wasn't
14299 really finished. */
14300 if (!cp_parser_next_token_ends_template_argument_p (parser))
14301 cp_parser_error (parser, "expected template-argument");
14302 if (!cp_parser_error_occurred (parser))
14304 /* Figure out what is being referred to. If the id-expression
14305 was for a class template specialization, then we will have a
14306 TYPE_DECL at this point. There is no need to do name lookup
14307 at this point in that case. */
14308 if (TREE_CODE (argument) != TYPE_DECL)
14309 argument = cp_parser_lookup_name (parser, argument,
14310 none_type,
14311 /*is_template=*/template_p,
14312 /*is_namespace=*/false,
14313 /*check_dependency=*/true,
14314 /*ambiguous_decls=*/NULL,
14315 argument_start_token->location);
14316 if (TREE_CODE (argument) != TEMPLATE_DECL
14317 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14318 cp_parser_error (parser, "expected template-name");
14320 if (cp_parser_parse_definitely (parser))
14322 if (TREE_DEPRECATED (argument))
14323 warn_deprecated_use (argument, NULL_TREE);
14324 return argument;
14326 /* It must be a non-type argument. There permitted cases are given
14327 in [temp.arg.nontype]:
14329 -- an integral constant-expression of integral or enumeration
14330 type; or
14332 -- the name of a non-type template-parameter; or
14334 -- the name of an object or function with external linkage...
14336 -- the address of an object or function with external linkage...
14338 -- a pointer to member... */
14339 /* Look for a non-type template parameter. */
14340 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14342 cp_parser_parse_tentatively (parser);
14343 argument = cp_parser_primary_expression (parser,
14344 /*address_p=*/false,
14345 /*cast_p=*/false,
14346 /*template_arg_p=*/true,
14347 &idk);
14348 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14349 || !cp_parser_next_token_ends_template_argument_p (parser))
14350 cp_parser_simulate_error (parser);
14351 if (cp_parser_parse_definitely (parser))
14352 return argument;
14355 /* If the next token is "&", the argument must be the address of an
14356 object or function with external linkage. */
14357 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14358 if (address_p)
14360 loc = cp_lexer_peek_token (parser->lexer)->location;
14361 cp_lexer_consume_token (parser->lexer);
14363 /* See if we might have an id-expression. */
14364 token = cp_lexer_peek_token (parser->lexer);
14365 if (token->type == CPP_NAME
14366 || token->keyword == RID_OPERATOR
14367 || token->type == CPP_SCOPE
14368 || token->type == CPP_TEMPLATE_ID
14369 || token->type == CPP_NESTED_NAME_SPECIFIER)
14371 cp_parser_parse_tentatively (parser);
14372 argument = cp_parser_primary_expression (parser,
14373 address_p,
14374 /*cast_p=*/false,
14375 /*template_arg_p=*/true,
14376 &idk);
14377 if (cp_parser_error_occurred (parser)
14378 || !cp_parser_next_token_ends_template_argument_p (parser))
14379 cp_parser_abort_tentative_parse (parser);
14380 else
14382 tree probe;
14384 if (INDIRECT_REF_P (argument))
14386 /* Strip the dereference temporarily. */
14387 gcc_assert (REFERENCE_REF_P (argument));
14388 argument = TREE_OPERAND (argument, 0);
14391 /* If we're in a template, we represent a qualified-id referring
14392 to a static data member as a SCOPE_REF even if the scope isn't
14393 dependent so that we can check access control later. */
14394 probe = argument;
14395 if (TREE_CODE (probe) == SCOPE_REF)
14396 probe = TREE_OPERAND (probe, 1);
14397 if (VAR_P (probe))
14399 /* A variable without external linkage might still be a
14400 valid constant-expression, so no error is issued here
14401 if the external-linkage check fails. */
14402 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14403 cp_parser_simulate_error (parser);
14405 else if (is_overloaded_fn (argument))
14406 /* All overloaded functions are allowed; if the external
14407 linkage test does not pass, an error will be issued
14408 later. */
14410 else if (address_p
14411 && (TREE_CODE (argument) == OFFSET_REF
14412 || TREE_CODE (argument) == SCOPE_REF))
14413 /* A pointer-to-member. */
14415 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14417 else
14418 cp_parser_simulate_error (parser);
14420 if (cp_parser_parse_definitely (parser))
14422 if (address_p)
14423 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14424 tf_warning_or_error);
14425 else
14426 argument = convert_from_reference (argument);
14427 return argument;
14431 /* If the argument started with "&", there are no other valid
14432 alternatives at this point. */
14433 if (address_p)
14435 cp_parser_error (parser, "invalid non-type template argument");
14436 return error_mark_node;
14439 /* If the argument wasn't successfully parsed as a type-id followed
14440 by '>>', the argument can only be a constant expression now.
14441 Otherwise, we try parsing the constant-expression tentatively,
14442 because the argument could really be a type-id. */
14443 if (maybe_type_id)
14444 cp_parser_parse_tentatively (parser);
14445 argument = cp_parser_constant_expression (parser);
14447 if (!maybe_type_id)
14448 return argument;
14449 if (!cp_parser_next_token_ends_template_argument_p (parser))
14450 cp_parser_error (parser, "expected template-argument");
14451 if (cp_parser_parse_definitely (parser))
14452 return argument;
14453 /* We did our best to parse the argument as a non type-id, but that
14454 was the only alternative that matched (albeit with a '>' after
14455 it). We can assume it's just a typo from the user, and a
14456 diagnostic will then be issued. */
14457 return cp_parser_template_type_arg (parser);
14460 /* Parse an explicit-instantiation.
14462 explicit-instantiation:
14463 template declaration
14465 Although the standard says `declaration', what it really means is:
14467 explicit-instantiation:
14468 template decl-specifier-seq [opt] declarator [opt] ;
14470 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14471 supposed to be allowed. A defect report has been filed about this
14472 issue.
14474 GNU Extension:
14476 explicit-instantiation:
14477 storage-class-specifier template
14478 decl-specifier-seq [opt] declarator [opt] ;
14479 function-specifier template
14480 decl-specifier-seq [opt] declarator [opt] ; */
14482 static void
14483 cp_parser_explicit_instantiation (cp_parser* parser)
14485 int declares_class_or_enum;
14486 cp_decl_specifier_seq decl_specifiers;
14487 tree extension_specifier = NULL_TREE;
14489 timevar_push (TV_TEMPLATE_INST);
14491 /* Look for an (optional) storage-class-specifier or
14492 function-specifier. */
14493 if (cp_parser_allow_gnu_extensions_p (parser))
14495 extension_specifier
14496 = cp_parser_storage_class_specifier_opt (parser);
14497 if (!extension_specifier)
14498 extension_specifier
14499 = cp_parser_function_specifier_opt (parser,
14500 /*decl_specs=*/NULL);
14503 /* Look for the `template' keyword. */
14504 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14505 /* Let the front end know that we are processing an explicit
14506 instantiation. */
14507 begin_explicit_instantiation ();
14508 /* [temp.explicit] says that we are supposed to ignore access
14509 control while processing explicit instantiation directives. */
14510 push_deferring_access_checks (dk_no_check);
14511 /* Parse a decl-specifier-seq. */
14512 cp_parser_decl_specifier_seq (parser,
14513 CP_PARSER_FLAGS_OPTIONAL,
14514 &decl_specifiers,
14515 &declares_class_or_enum);
14516 /* If there was exactly one decl-specifier, and it declared a class,
14517 and there's no declarator, then we have an explicit type
14518 instantiation. */
14519 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14521 tree type;
14523 type = check_tag_decl (&decl_specifiers,
14524 /*explicit_type_instantiation_p=*/true);
14525 /* Turn access control back on for names used during
14526 template instantiation. */
14527 pop_deferring_access_checks ();
14528 if (type)
14529 do_type_instantiation (type, extension_specifier,
14530 /*complain=*/tf_error);
14532 else
14534 cp_declarator *declarator;
14535 tree decl;
14537 /* Parse the declarator. */
14538 declarator
14539 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14540 /*ctor_dtor_or_conv_p=*/NULL,
14541 /*parenthesized_p=*/NULL,
14542 /*member_p=*/false,
14543 /*friend_p=*/false);
14544 if (declares_class_or_enum & 2)
14545 cp_parser_check_for_definition_in_return_type (declarator,
14546 decl_specifiers.type,
14547 decl_specifiers.locations[ds_type_spec]);
14548 if (declarator != cp_error_declarator)
14550 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14551 permerror (decl_specifiers.locations[ds_inline],
14552 "explicit instantiation shall not use"
14553 " %<inline%> specifier");
14554 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14555 permerror (decl_specifiers.locations[ds_constexpr],
14556 "explicit instantiation shall not use"
14557 " %<constexpr%> specifier");
14559 decl = grokdeclarator (declarator, &decl_specifiers,
14560 NORMAL, 0, &decl_specifiers.attributes);
14561 /* Turn access control back on for names used during
14562 template instantiation. */
14563 pop_deferring_access_checks ();
14564 /* Do the explicit instantiation. */
14565 do_decl_instantiation (decl, extension_specifier);
14567 else
14569 pop_deferring_access_checks ();
14570 /* Skip the body of the explicit instantiation. */
14571 cp_parser_skip_to_end_of_statement (parser);
14574 /* We're done with the instantiation. */
14575 end_explicit_instantiation ();
14577 cp_parser_consume_semicolon_at_end_of_statement (parser);
14579 timevar_pop (TV_TEMPLATE_INST);
14582 /* Parse an explicit-specialization.
14584 explicit-specialization:
14585 template < > declaration
14587 Although the standard says `declaration', what it really means is:
14589 explicit-specialization:
14590 template <> decl-specifier [opt] init-declarator [opt] ;
14591 template <> function-definition
14592 template <> explicit-specialization
14593 template <> template-declaration */
14595 static void
14596 cp_parser_explicit_specialization (cp_parser* parser)
14598 bool need_lang_pop;
14599 cp_token *token = cp_lexer_peek_token (parser->lexer);
14601 /* Look for the `template' keyword. */
14602 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14603 /* Look for the `<'. */
14604 cp_parser_require (parser, CPP_LESS, RT_LESS);
14605 /* Look for the `>'. */
14606 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14607 /* We have processed another parameter list. */
14608 ++parser->num_template_parameter_lists;
14609 /* [temp]
14611 A template ... explicit specialization ... shall not have C
14612 linkage. */
14613 if (current_lang_name == lang_name_c)
14615 error_at (token->location, "template specialization with C linkage");
14616 /* Give it C++ linkage to avoid confusing other parts of the
14617 front end. */
14618 push_lang_context (lang_name_cplusplus);
14619 need_lang_pop = true;
14621 else
14622 need_lang_pop = false;
14623 /* Let the front end know that we are beginning a specialization. */
14624 if (!begin_specialization ())
14626 end_specialization ();
14627 return;
14630 /* If the next keyword is `template', we need to figure out whether
14631 or not we're looking a template-declaration. */
14632 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14634 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14635 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14636 cp_parser_template_declaration_after_export (parser,
14637 /*member_p=*/false);
14638 else
14639 cp_parser_explicit_specialization (parser);
14641 else
14642 /* Parse the dependent declaration. */
14643 cp_parser_single_declaration (parser,
14644 /*checks=*/NULL,
14645 /*member_p=*/false,
14646 /*explicit_specialization_p=*/true,
14647 /*friend_p=*/NULL);
14648 /* We're done with the specialization. */
14649 end_specialization ();
14650 /* For the erroneous case of a template with C linkage, we pushed an
14651 implicit C++ linkage scope; exit that scope now. */
14652 if (need_lang_pop)
14653 pop_lang_context ();
14654 /* We're done with this parameter list. */
14655 --parser->num_template_parameter_lists;
14658 /* Parse a type-specifier.
14660 type-specifier:
14661 simple-type-specifier
14662 class-specifier
14663 enum-specifier
14664 elaborated-type-specifier
14665 cv-qualifier
14667 GNU Extension:
14669 type-specifier:
14670 __complex__
14672 Returns a representation of the type-specifier. For a
14673 class-specifier, enum-specifier, or elaborated-type-specifier, a
14674 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14676 The parser flags FLAGS is used to control type-specifier parsing.
14678 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14679 in a decl-specifier-seq.
14681 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14682 class-specifier, enum-specifier, or elaborated-type-specifier, then
14683 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14684 if a type is declared; 2 if it is defined. Otherwise, it is set to
14685 zero.
14687 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14688 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14689 is set to FALSE. */
14691 static tree
14692 cp_parser_type_specifier (cp_parser* parser,
14693 cp_parser_flags flags,
14694 cp_decl_specifier_seq *decl_specs,
14695 bool is_declaration,
14696 int* declares_class_or_enum,
14697 bool* is_cv_qualifier)
14699 tree type_spec = NULL_TREE;
14700 cp_token *token;
14701 enum rid keyword;
14702 cp_decl_spec ds = ds_last;
14704 /* Assume this type-specifier does not declare a new type. */
14705 if (declares_class_or_enum)
14706 *declares_class_or_enum = 0;
14707 /* And that it does not specify a cv-qualifier. */
14708 if (is_cv_qualifier)
14709 *is_cv_qualifier = false;
14710 /* Peek at the next token. */
14711 token = cp_lexer_peek_token (parser->lexer);
14713 /* If we're looking at a keyword, we can use that to guide the
14714 production we choose. */
14715 keyword = token->keyword;
14716 switch (keyword)
14718 case RID_ENUM:
14719 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14720 goto elaborated_type_specifier;
14722 /* Look for the enum-specifier. */
14723 type_spec = cp_parser_enum_specifier (parser);
14724 /* If that worked, we're done. */
14725 if (type_spec)
14727 if (declares_class_or_enum)
14728 *declares_class_or_enum = 2;
14729 if (decl_specs)
14730 cp_parser_set_decl_spec_type (decl_specs,
14731 type_spec,
14732 token,
14733 /*type_definition_p=*/true);
14734 return type_spec;
14736 else
14737 goto elaborated_type_specifier;
14739 /* Any of these indicate either a class-specifier, or an
14740 elaborated-type-specifier. */
14741 case RID_CLASS:
14742 case RID_STRUCT:
14743 case RID_UNION:
14744 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14745 goto elaborated_type_specifier;
14747 /* Parse tentatively so that we can back up if we don't find a
14748 class-specifier. */
14749 cp_parser_parse_tentatively (parser);
14750 /* Look for the class-specifier. */
14751 type_spec = cp_parser_class_specifier (parser);
14752 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14753 /* If that worked, we're done. */
14754 if (cp_parser_parse_definitely (parser))
14756 if (declares_class_or_enum)
14757 *declares_class_or_enum = 2;
14758 if (decl_specs)
14759 cp_parser_set_decl_spec_type (decl_specs,
14760 type_spec,
14761 token,
14762 /*type_definition_p=*/true);
14763 return type_spec;
14766 /* Fall through. */
14767 elaborated_type_specifier:
14768 /* We're declaring (not defining) a class or enum. */
14769 if (declares_class_or_enum)
14770 *declares_class_or_enum = 1;
14772 /* Fall through. */
14773 case RID_TYPENAME:
14774 /* Look for an elaborated-type-specifier. */
14775 type_spec
14776 = (cp_parser_elaborated_type_specifier
14777 (parser,
14778 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14779 is_declaration));
14780 if (decl_specs)
14781 cp_parser_set_decl_spec_type (decl_specs,
14782 type_spec,
14783 token,
14784 /*type_definition_p=*/false);
14785 return type_spec;
14787 case RID_CONST:
14788 ds = ds_const;
14789 if (is_cv_qualifier)
14790 *is_cv_qualifier = true;
14791 break;
14793 case RID_VOLATILE:
14794 ds = ds_volatile;
14795 if (is_cv_qualifier)
14796 *is_cv_qualifier = true;
14797 break;
14799 case RID_RESTRICT:
14800 ds = ds_restrict;
14801 if (is_cv_qualifier)
14802 *is_cv_qualifier = true;
14803 break;
14805 case RID_COMPLEX:
14806 /* The `__complex__' keyword is a GNU extension. */
14807 ds = ds_complex;
14808 break;
14810 default:
14811 break;
14814 /* Handle simple keywords. */
14815 if (ds != ds_last)
14817 if (decl_specs)
14819 set_and_check_decl_spec_loc (decl_specs, ds, token);
14820 decl_specs->any_specifiers_p = true;
14822 return cp_lexer_consume_token (parser->lexer)->u.value;
14825 /* If we do not already have a type-specifier, assume we are looking
14826 at a simple-type-specifier. */
14827 type_spec = cp_parser_simple_type_specifier (parser,
14828 decl_specs,
14829 flags);
14831 /* If we didn't find a type-specifier, and a type-specifier was not
14832 optional in this context, issue an error message. */
14833 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14835 cp_parser_error (parser, "expected type specifier");
14836 return error_mark_node;
14839 return type_spec;
14842 /* Parse a simple-type-specifier.
14844 simple-type-specifier:
14845 :: [opt] nested-name-specifier [opt] type-name
14846 :: [opt] nested-name-specifier template template-id
14847 char
14848 wchar_t
14849 bool
14850 short
14852 long
14853 signed
14854 unsigned
14855 float
14856 double
14857 void
14859 C++0x Extension:
14861 simple-type-specifier:
14862 auto
14863 decltype ( expression )
14864 char16_t
14865 char32_t
14866 __underlying_type ( type-id )
14868 GNU Extension:
14870 simple-type-specifier:
14871 __int128
14872 __typeof__ unary-expression
14873 __typeof__ ( type-id )
14874 __typeof__ ( type-id ) { initializer-list , [opt] }
14876 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14877 appropriately updated. */
14879 static tree
14880 cp_parser_simple_type_specifier (cp_parser* parser,
14881 cp_decl_specifier_seq *decl_specs,
14882 cp_parser_flags flags)
14884 tree type = NULL_TREE;
14885 cp_token *token;
14886 int idx;
14888 /* Peek at the next token. */
14889 token = cp_lexer_peek_token (parser->lexer);
14891 /* If we're looking at a keyword, things are easy. */
14892 switch (token->keyword)
14894 case RID_CHAR:
14895 if (decl_specs)
14896 decl_specs->explicit_char_p = true;
14897 type = char_type_node;
14898 break;
14899 case RID_CHAR16:
14900 type = char16_type_node;
14901 break;
14902 case RID_CHAR32:
14903 type = char32_type_node;
14904 break;
14905 case RID_WCHAR:
14906 type = wchar_type_node;
14907 break;
14908 case RID_BOOL:
14909 type = boolean_type_node;
14910 break;
14911 case RID_SHORT:
14912 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14913 type = short_integer_type_node;
14914 break;
14915 case RID_INT:
14916 if (decl_specs)
14917 decl_specs->explicit_int_p = true;
14918 type = integer_type_node;
14919 break;
14920 case RID_INT_N_0:
14921 case RID_INT_N_1:
14922 case RID_INT_N_2:
14923 case RID_INT_N_3:
14924 idx = token->keyword - RID_INT_N_0;
14925 if (! int_n_enabled_p [idx])
14926 break;
14927 if (decl_specs)
14929 decl_specs->explicit_intN_p = true;
14930 decl_specs->int_n_idx = idx;
14932 type = int_n_trees [idx].signed_type;
14933 break;
14934 case RID_LONG:
14935 if (decl_specs)
14936 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14937 type = long_integer_type_node;
14938 break;
14939 case RID_SIGNED:
14940 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14941 type = integer_type_node;
14942 break;
14943 case RID_UNSIGNED:
14944 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14945 type = unsigned_type_node;
14946 break;
14947 case RID_FLOAT:
14948 type = float_type_node;
14949 break;
14950 case RID_DOUBLE:
14951 type = double_type_node;
14952 break;
14953 case RID_VOID:
14954 type = void_type_node;
14955 break;
14957 case RID_AUTO:
14958 maybe_warn_cpp0x (CPP0X_AUTO);
14959 if (parser->auto_is_implicit_function_template_parm_p)
14961 /* The 'auto' might be the placeholder return type for a function decl
14962 with trailing return type. */
14963 bool have_trailing_return_fn_decl = false;
14964 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14965 == CPP_OPEN_PAREN)
14967 cp_parser_parse_tentatively (parser);
14968 cp_lexer_consume_token (parser->lexer);
14969 cp_lexer_consume_token (parser->lexer);
14970 if (cp_parser_skip_to_closing_parenthesis (parser,
14971 /*recovering*/false,
14972 /*or_comma*/false,
14973 /*consume_paren*/true))
14974 have_trailing_return_fn_decl
14975 = cp_lexer_next_token_is (parser->lexer, CPP_DEREF);
14976 cp_parser_abort_tentative_parse (parser);
14979 if (have_trailing_return_fn_decl)
14981 type = make_auto ();
14982 break;
14985 if (cxx_dialect >= cxx14)
14986 type = synthesize_implicit_template_parm (parser);
14987 else
14988 type = error_mark_node;
14990 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14992 if (cxx_dialect < cxx14)
14993 error_at (token->location,
14994 "use of %<auto%> in lambda parameter declaration "
14995 "only available with "
14996 "-std=c++14 or -std=gnu++14");
14998 else if (cxx_dialect < cxx14)
14999 error_at (token->location,
15000 "use of %<auto%> in parameter declaration "
15001 "only available with "
15002 "-std=c++14 or -std=gnu++14");
15003 else
15004 pedwarn (token->location, OPT_Wpedantic,
15005 "ISO C++ forbids use of %<auto%> in parameter "
15006 "declaration");
15008 else
15009 type = make_auto ();
15010 break;
15012 case RID_DECLTYPE:
15013 /* Since DR 743, decltype can either be a simple-type-specifier by
15014 itself or begin a nested-name-specifier. Parsing it will replace
15015 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
15016 handling below decide what to do. */
15017 cp_parser_decltype (parser);
15018 cp_lexer_set_token_position (parser->lexer, token);
15019 break;
15021 case RID_TYPEOF:
15022 /* Consume the `typeof' token. */
15023 cp_lexer_consume_token (parser->lexer);
15024 /* Parse the operand to `typeof'. */
15025 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
15026 /* If it is not already a TYPE, take its type. */
15027 if (!TYPE_P (type))
15028 type = finish_typeof (type);
15030 if (decl_specs)
15031 cp_parser_set_decl_spec_type (decl_specs, type,
15032 token,
15033 /*type_definition_p=*/false);
15035 return type;
15037 case RID_UNDERLYING_TYPE:
15038 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
15039 if (decl_specs)
15040 cp_parser_set_decl_spec_type (decl_specs, type,
15041 token,
15042 /*type_definition_p=*/false);
15044 return type;
15046 case RID_BASES:
15047 case RID_DIRECT_BASES:
15048 type = cp_parser_trait_expr (parser, token->keyword);
15049 if (decl_specs)
15050 cp_parser_set_decl_spec_type (decl_specs, type,
15051 token,
15052 /*type_definition_p=*/false);
15053 return type;
15054 default:
15055 break;
15058 /* If token is an already-parsed decltype not followed by ::,
15059 it's a simple-type-specifier. */
15060 if (token->type == CPP_DECLTYPE
15061 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15063 type = token->u.value;
15064 if (decl_specs)
15066 cp_parser_set_decl_spec_type (decl_specs, type,
15067 token,
15068 /*type_definition_p=*/false);
15069 /* Remember that we are handling a decltype in order to
15070 implement the resolution of DR 1510 when the argument
15071 isn't instantiation dependent. */
15072 decl_specs->decltype_p = true;
15074 cp_lexer_consume_token (parser->lexer);
15075 return type;
15078 /* If the type-specifier was for a built-in type, we're done. */
15079 if (type)
15081 /* Record the type. */
15082 if (decl_specs
15083 && (token->keyword != RID_SIGNED
15084 && token->keyword != RID_UNSIGNED
15085 && token->keyword != RID_SHORT
15086 && token->keyword != RID_LONG))
15087 cp_parser_set_decl_spec_type (decl_specs,
15088 type,
15089 token,
15090 /*type_definition_p=*/false);
15091 if (decl_specs)
15092 decl_specs->any_specifiers_p = true;
15094 /* Consume the token. */
15095 cp_lexer_consume_token (parser->lexer);
15097 if (type == error_mark_node)
15098 return error_mark_node;
15100 /* There is no valid C++ program where a non-template type is
15101 followed by a "<". That usually indicates that the user thought
15102 that the type was a template. */
15103 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15104 token->location);
15106 return TYPE_NAME (type);
15109 /* The type-specifier must be a user-defined type. */
15110 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15112 bool qualified_p;
15113 bool global_p;
15115 /* Don't gobble tokens or issue error messages if this is an
15116 optional type-specifier. */
15117 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15118 cp_parser_parse_tentatively (parser);
15120 /* Look for the optional `::' operator. */
15121 global_p
15122 = (cp_parser_global_scope_opt (parser,
15123 /*current_scope_valid_p=*/false)
15124 != NULL_TREE);
15125 /* Look for the nested-name specifier. */
15126 qualified_p
15127 = (cp_parser_nested_name_specifier_opt (parser,
15128 /*typename_keyword_p=*/false,
15129 /*check_dependency_p=*/true,
15130 /*type_p=*/false,
15131 /*is_declaration=*/false)
15132 != NULL_TREE);
15133 token = cp_lexer_peek_token (parser->lexer);
15134 /* If we have seen a nested-name-specifier, and the next token
15135 is `template', then we are using the template-id production. */
15136 if (parser->scope
15137 && cp_parser_optional_template_keyword (parser))
15139 /* Look for the template-id. */
15140 type = cp_parser_template_id (parser,
15141 /*template_keyword_p=*/true,
15142 /*check_dependency_p=*/true,
15143 none_type,
15144 /*is_declaration=*/false);
15145 /* If the template-id did not name a type, we are out of
15146 luck. */
15147 if (TREE_CODE (type) != TYPE_DECL)
15149 cp_parser_error (parser, "expected template-id for type");
15150 type = NULL_TREE;
15153 /* Otherwise, look for a type-name. */
15154 else
15155 type = cp_parser_type_name (parser);
15156 /* Keep track of all name-lookups performed in class scopes. */
15157 if (type
15158 && !global_p
15159 && !qualified_p
15160 && TREE_CODE (type) == TYPE_DECL
15161 && identifier_p (DECL_NAME (type)))
15162 maybe_note_name_used_in_class (DECL_NAME (type), type);
15163 /* If it didn't work out, we don't have a TYPE. */
15164 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15165 && !cp_parser_parse_definitely (parser))
15166 type = NULL_TREE;
15167 if (type && decl_specs)
15168 cp_parser_set_decl_spec_type (decl_specs, type,
15169 token,
15170 /*type_definition_p=*/false);
15173 /* If we didn't get a type-name, issue an error message. */
15174 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15176 cp_parser_error (parser, "expected type-name");
15177 return error_mark_node;
15180 if (type && type != error_mark_node)
15182 /* See if TYPE is an Objective-C type, and if so, parse and
15183 accept any protocol references following it. Do this before
15184 the cp_parser_check_for_invalid_template_id() call, because
15185 Objective-C types can be followed by '<...>' which would
15186 enclose protocol names rather than template arguments, and so
15187 everything is fine. */
15188 if (c_dialect_objc () && !parser->scope
15189 && (objc_is_id (type) || objc_is_class_name (type)))
15191 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15192 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15194 /* Clobber the "unqualified" type previously entered into
15195 DECL_SPECS with the new, improved protocol-qualified version. */
15196 if (decl_specs)
15197 decl_specs->type = qual_type;
15199 return qual_type;
15202 /* There is no valid C++ program where a non-template type is
15203 followed by a "<". That usually indicates that the user
15204 thought that the type was a template. */
15205 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15206 none_type,
15207 token->location);
15210 return type;
15213 /* Parse a type-name.
15215 type-name:
15216 class-name
15217 enum-name
15218 typedef-name
15219 simple-template-id [in c++0x]
15221 enum-name:
15222 identifier
15224 typedef-name:
15225 identifier
15227 Returns a TYPE_DECL for the type. */
15229 static tree
15230 cp_parser_type_name (cp_parser* parser)
15232 tree type_decl;
15234 /* We can't know yet whether it is a class-name or not. */
15235 cp_parser_parse_tentatively (parser);
15236 /* Try a class-name. */
15237 type_decl = cp_parser_class_name (parser,
15238 /*typename_keyword_p=*/false,
15239 /*template_keyword_p=*/false,
15240 none_type,
15241 /*check_dependency_p=*/true,
15242 /*class_head_p=*/false,
15243 /*is_declaration=*/false);
15244 /* If it's not a class-name, keep looking. */
15245 if (!cp_parser_parse_definitely (parser))
15247 if (cxx_dialect < cxx11)
15248 /* It must be a typedef-name or an enum-name. */
15249 return cp_parser_nonclass_name (parser);
15251 cp_parser_parse_tentatively (parser);
15252 /* It is either a simple-template-id representing an
15253 instantiation of an alias template... */
15254 type_decl = cp_parser_template_id (parser,
15255 /*template_keyword_p=*/false,
15256 /*check_dependency_p=*/true,
15257 none_type,
15258 /*is_declaration=*/false);
15259 /* Note that this must be an instantiation of an alias template
15260 because [temp.names]/6 says:
15262 A template-id that names an alias template specialization
15263 is a type-name.
15265 Whereas [temp.names]/7 says:
15267 A simple-template-id that names a class template
15268 specialization is a class-name. */
15269 if (type_decl != NULL_TREE
15270 && TREE_CODE (type_decl) == TYPE_DECL
15271 && TYPE_DECL_ALIAS_P (type_decl))
15272 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15273 else
15274 cp_parser_simulate_error (parser);
15276 if (!cp_parser_parse_definitely (parser))
15277 /* ... Or a typedef-name or an enum-name. */
15278 return cp_parser_nonclass_name (parser);
15281 return type_decl;
15284 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15286 enum-name:
15287 identifier
15289 typedef-name:
15290 identifier
15292 Returns a TYPE_DECL for the type. */
15294 static tree
15295 cp_parser_nonclass_name (cp_parser* parser)
15297 tree type_decl;
15298 tree identifier;
15300 cp_token *token = cp_lexer_peek_token (parser->lexer);
15301 identifier = cp_parser_identifier (parser);
15302 if (identifier == error_mark_node)
15303 return error_mark_node;
15305 /* Look up the type-name. */
15306 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15308 type_decl = strip_using_decl (type_decl);
15310 if (TREE_CODE (type_decl) != TYPE_DECL
15311 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15313 /* See if this is an Objective-C type. */
15314 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15315 tree type = objc_get_protocol_qualified_type (identifier, protos);
15316 if (type)
15317 type_decl = TYPE_NAME (type);
15320 /* Issue an error if we did not find a type-name. */
15321 if (TREE_CODE (type_decl) != TYPE_DECL
15322 /* In Objective-C, we have the complication that class names are
15323 normally type names and start declarations (eg, the
15324 "NSObject" in "NSObject *object;"), but can be used in an
15325 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15326 is an expression. So, a classname followed by a dot is not a
15327 valid type-name. */
15328 || (objc_is_class_name (TREE_TYPE (type_decl))
15329 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15331 if (!cp_parser_simulate_error (parser))
15332 cp_parser_name_lookup_error (parser, identifier, type_decl,
15333 NLE_TYPE, token->location);
15334 return error_mark_node;
15336 /* Remember that the name was used in the definition of the
15337 current class so that we can check later to see if the
15338 meaning would have been different after the class was
15339 entirely defined. */
15340 else if (type_decl != error_mark_node
15341 && !parser->scope)
15342 maybe_note_name_used_in_class (identifier, type_decl);
15344 return type_decl;
15347 /* Parse an elaborated-type-specifier. Note that the grammar given
15348 here incorporates the resolution to DR68.
15350 elaborated-type-specifier:
15351 class-key :: [opt] nested-name-specifier [opt] identifier
15352 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15353 enum-key :: [opt] nested-name-specifier [opt] identifier
15354 typename :: [opt] nested-name-specifier identifier
15355 typename :: [opt] nested-name-specifier template [opt]
15356 template-id
15358 GNU extension:
15360 elaborated-type-specifier:
15361 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15362 class-key attributes :: [opt] nested-name-specifier [opt]
15363 template [opt] template-id
15364 enum attributes :: [opt] nested-name-specifier [opt] identifier
15366 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15367 declared `friend'. If IS_DECLARATION is TRUE, then this
15368 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15369 something is being declared.
15371 Returns the TYPE specified. */
15373 static tree
15374 cp_parser_elaborated_type_specifier (cp_parser* parser,
15375 bool is_friend,
15376 bool is_declaration)
15378 enum tag_types tag_type;
15379 tree identifier;
15380 tree type = NULL_TREE;
15381 tree attributes = NULL_TREE;
15382 tree globalscope;
15383 cp_token *token = NULL;
15385 /* See if we're looking at the `enum' keyword. */
15386 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15388 /* Consume the `enum' token. */
15389 cp_lexer_consume_token (parser->lexer);
15390 /* Remember that it's an enumeration type. */
15391 tag_type = enum_type;
15392 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15393 enums) is used here. */
15394 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15395 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15397 pedwarn (input_location, 0, "elaborated-type-specifier "
15398 "for a scoped enum must not use the %<%D%> keyword",
15399 cp_lexer_peek_token (parser->lexer)->u.value);
15400 /* Consume the `struct' or `class' and parse it anyway. */
15401 cp_lexer_consume_token (parser->lexer);
15403 /* Parse the attributes. */
15404 attributes = cp_parser_attributes_opt (parser);
15406 /* Or, it might be `typename'. */
15407 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15408 RID_TYPENAME))
15410 /* Consume the `typename' token. */
15411 cp_lexer_consume_token (parser->lexer);
15412 /* Remember that it's a `typename' type. */
15413 tag_type = typename_type;
15415 /* Otherwise it must be a class-key. */
15416 else
15418 tag_type = cp_parser_class_key (parser);
15419 if (tag_type == none_type)
15420 return error_mark_node;
15421 /* Parse the attributes. */
15422 attributes = cp_parser_attributes_opt (parser);
15425 /* Look for the `::' operator. */
15426 globalscope = cp_parser_global_scope_opt (parser,
15427 /*current_scope_valid_p=*/false);
15428 /* Look for the nested-name-specifier. */
15429 if (tag_type == typename_type && !globalscope)
15431 if (!cp_parser_nested_name_specifier (parser,
15432 /*typename_keyword_p=*/true,
15433 /*check_dependency_p=*/true,
15434 /*type_p=*/true,
15435 is_declaration))
15436 return error_mark_node;
15438 else
15439 /* Even though `typename' is not present, the proposed resolution
15440 to Core Issue 180 says that in `class A<T>::B', `B' should be
15441 considered a type-name, even if `A<T>' is dependent. */
15442 cp_parser_nested_name_specifier_opt (parser,
15443 /*typename_keyword_p=*/true,
15444 /*check_dependency_p=*/true,
15445 /*type_p=*/true,
15446 is_declaration);
15447 /* For everything but enumeration types, consider a template-id.
15448 For an enumeration type, consider only a plain identifier. */
15449 if (tag_type != enum_type)
15451 bool template_p = false;
15452 tree decl;
15454 /* Allow the `template' keyword. */
15455 template_p = cp_parser_optional_template_keyword (parser);
15456 /* If we didn't see `template', we don't know if there's a
15457 template-id or not. */
15458 if (!template_p)
15459 cp_parser_parse_tentatively (parser);
15460 /* Parse the template-id. */
15461 token = cp_lexer_peek_token (parser->lexer);
15462 decl = cp_parser_template_id (parser, template_p,
15463 /*check_dependency_p=*/true,
15464 tag_type,
15465 is_declaration);
15466 /* If we didn't find a template-id, look for an ordinary
15467 identifier. */
15468 if (!template_p && !cp_parser_parse_definitely (parser))
15470 /* We can get here when cp_parser_template_id, called by
15471 cp_parser_class_name with tag_type == none_type, succeeds
15472 and caches a BASELINK. Then, when called again here,
15473 instead of failing and returning an error_mark_node
15474 returns it (see template/typename17.C in C++11).
15475 ??? Could we diagnose this earlier? */
15476 else if (tag_type == typename_type && BASELINK_P (decl))
15478 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15479 type = error_mark_node;
15481 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15482 in effect, then we must assume that, upon instantiation, the
15483 template will correspond to a class. */
15484 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15485 && tag_type == typename_type)
15486 type = make_typename_type (parser->scope, decl,
15487 typename_type,
15488 /*complain=*/tf_error);
15489 /* If the `typename' keyword is in effect and DECL is not a type
15490 decl, then type is non existent. */
15491 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15493 else if (TREE_CODE (decl) == TYPE_DECL)
15494 type = check_elaborated_type_specifier (tag_type, decl,
15495 /*allow_template_p=*/true);
15496 else if (decl == error_mark_node)
15497 type = error_mark_node;
15500 if (!type)
15502 token = cp_lexer_peek_token (parser->lexer);
15503 identifier = cp_parser_identifier (parser);
15505 if (identifier == error_mark_node)
15507 parser->scope = NULL_TREE;
15508 return error_mark_node;
15511 /* For a `typename', we needn't call xref_tag. */
15512 if (tag_type == typename_type
15513 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15514 return cp_parser_make_typename_type (parser, identifier,
15515 token->location);
15517 /* Template parameter lists apply only if we are not within a
15518 function parameter list. */
15519 bool template_parm_lists_apply
15520 = parser->num_template_parameter_lists;
15521 if (template_parm_lists_apply)
15522 for (cp_binding_level *s = current_binding_level;
15523 s && s->kind != sk_template_parms;
15524 s = s->level_chain)
15525 if (s->kind == sk_function_parms)
15526 template_parm_lists_apply = false;
15528 /* Look up a qualified name in the usual way. */
15529 if (parser->scope)
15531 tree decl;
15532 tree ambiguous_decls;
15534 decl = cp_parser_lookup_name (parser, identifier,
15535 tag_type,
15536 /*is_template=*/false,
15537 /*is_namespace=*/false,
15538 /*check_dependency=*/true,
15539 &ambiguous_decls,
15540 token->location);
15542 /* If the lookup was ambiguous, an error will already have been
15543 issued. */
15544 if (ambiguous_decls)
15545 return error_mark_node;
15547 /* If we are parsing friend declaration, DECL may be a
15548 TEMPLATE_DECL tree node here. However, we need to check
15549 whether this TEMPLATE_DECL results in valid code. Consider
15550 the following example:
15552 namespace N {
15553 template <class T> class C {};
15555 class X {
15556 template <class T> friend class N::C; // #1, valid code
15558 template <class T> class Y {
15559 friend class N::C; // #2, invalid code
15562 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15563 name lookup of `N::C'. We see that friend declaration must
15564 be template for the code to be valid. Note that
15565 processing_template_decl does not work here since it is
15566 always 1 for the above two cases. */
15568 decl = (cp_parser_maybe_treat_template_as_class
15569 (decl, /*tag_name_p=*/is_friend
15570 && template_parm_lists_apply));
15572 if (TREE_CODE (decl) != TYPE_DECL)
15574 cp_parser_diagnose_invalid_type_name (parser,
15575 identifier,
15576 token->location);
15577 return error_mark_node;
15580 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15582 bool allow_template = (template_parm_lists_apply
15583 || DECL_SELF_REFERENCE_P (decl));
15584 type = check_elaborated_type_specifier (tag_type, decl,
15585 allow_template);
15587 if (type == error_mark_node)
15588 return error_mark_node;
15591 /* Forward declarations of nested types, such as
15593 class C1::C2;
15594 class C1::C2::C3;
15596 are invalid unless all components preceding the final '::'
15597 are complete. If all enclosing types are complete, these
15598 declarations become merely pointless.
15600 Invalid forward declarations of nested types are errors
15601 caught elsewhere in parsing. Those that are pointless arrive
15602 here. */
15604 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15605 && !is_friend && !processing_explicit_instantiation)
15606 warning (0, "declaration %qD does not declare anything", decl);
15608 type = TREE_TYPE (decl);
15610 else
15612 /* An elaborated-type-specifier sometimes introduces a new type and
15613 sometimes names an existing type. Normally, the rule is that it
15614 introduces a new type only if there is not an existing type of
15615 the same name already in scope. For example, given:
15617 struct S {};
15618 void f() { struct S s; }
15620 the `struct S' in the body of `f' is the same `struct S' as in
15621 the global scope; the existing definition is used. However, if
15622 there were no global declaration, this would introduce a new
15623 local class named `S'.
15625 An exception to this rule applies to the following code:
15627 namespace N { struct S; }
15629 Here, the elaborated-type-specifier names a new type
15630 unconditionally; even if there is already an `S' in the
15631 containing scope this declaration names a new type.
15632 This exception only applies if the elaborated-type-specifier
15633 forms the complete declaration:
15635 [class.name]
15637 A declaration consisting solely of `class-key identifier ;' is
15638 either a redeclaration of the name in the current scope or a
15639 forward declaration of the identifier as a class name. It
15640 introduces the name into the current scope.
15642 We are in this situation precisely when the next token is a `;'.
15644 An exception to the exception is that a `friend' declaration does
15645 *not* name a new type; i.e., given:
15647 struct S { friend struct T; };
15649 `T' is not a new type in the scope of `S'.
15651 Also, `new struct S' or `sizeof (struct S)' never results in the
15652 definition of a new type; a new type can only be declared in a
15653 declaration context. */
15655 tag_scope ts;
15656 bool template_p;
15658 if (is_friend)
15659 /* Friends have special name lookup rules. */
15660 ts = ts_within_enclosing_non_class;
15661 else if (is_declaration
15662 && cp_lexer_next_token_is (parser->lexer,
15663 CPP_SEMICOLON))
15664 /* This is a `class-key identifier ;' */
15665 ts = ts_current;
15666 else
15667 ts = ts_global;
15669 template_p =
15670 (template_parm_lists_apply
15671 && (cp_parser_next_token_starts_class_definition_p (parser)
15672 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15673 /* An unqualified name was used to reference this type, so
15674 there were no qualifying templates. */
15675 if (template_parm_lists_apply
15676 && !cp_parser_check_template_parameters (parser,
15677 /*num_templates=*/0,
15678 token->location,
15679 /*declarator=*/NULL))
15680 return error_mark_node;
15681 type = xref_tag (tag_type, identifier, ts, template_p);
15685 if (type == error_mark_node)
15686 return error_mark_node;
15688 /* Allow attributes on forward declarations of classes. */
15689 if (attributes)
15691 if (TREE_CODE (type) == TYPENAME_TYPE)
15692 warning (OPT_Wattributes,
15693 "attributes ignored on uninstantiated type");
15694 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15695 && ! processing_explicit_instantiation)
15696 warning (OPT_Wattributes,
15697 "attributes ignored on template instantiation");
15698 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15699 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15700 else
15701 warning (OPT_Wattributes,
15702 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15705 if (tag_type != enum_type)
15707 /* Indicate whether this class was declared as a `class' or as a
15708 `struct'. */
15709 if (TREE_CODE (type) == RECORD_TYPE)
15710 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15711 cp_parser_check_class_key (tag_type, type);
15714 /* A "<" cannot follow an elaborated type specifier. If that
15715 happens, the user was probably trying to form a template-id. */
15716 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15717 token->location);
15719 return type;
15722 /* Parse an enum-specifier.
15724 enum-specifier:
15725 enum-head { enumerator-list [opt] }
15726 enum-head { enumerator-list , } [C++0x]
15728 enum-head:
15729 enum-key identifier [opt] enum-base [opt]
15730 enum-key nested-name-specifier identifier enum-base [opt]
15732 enum-key:
15733 enum
15734 enum class [C++0x]
15735 enum struct [C++0x]
15737 enum-base: [C++0x]
15738 : type-specifier-seq
15740 opaque-enum-specifier:
15741 enum-key identifier enum-base [opt] ;
15743 GNU Extensions:
15744 enum-key attributes[opt] identifier [opt] enum-base [opt]
15745 { enumerator-list [opt] }attributes[opt]
15746 enum-key attributes[opt] identifier [opt] enum-base [opt]
15747 { enumerator-list, }attributes[opt] [C++0x]
15749 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15750 if the token stream isn't an enum-specifier after all. */
15752 static tree
15753 cp_parser_enum_specifier (cp_parser* parser)
15755 tree identifier;
15756 tree type = NULL_TREE;
15757 tree prev_scope;
15758 tree nested_name_specifier = NULL_TREE;
15759 tree attributes;
15760 bool scoped_enum_p = false;
15761 bool has_underlying_type = false;
15762 bool nested_being_defined = false;
15763 bool new_value_list = false;
15764 bool is_new_type = false;
15765 bool is_anonymous = false;
15766 tree underlying_type = NULL_TREE;
15767 cp_token *type_start_token = NULL;
15768 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15770 parser->colon_corrects_to_scope_p = false;
15772 /* Parse tentatively so that we can back up if we don't find a
15773 enum-specifier. */
15774 cp_parser_parse_tentatively (parser);
15776 /* Caller guarantees that the current token is 'enum', an identifier
15777 possibly follows, and the token after that is an opening brace.
15778 If we don't have an identifier, fabricate an anonymous name for
15779 the enumeration being defined. */
15780 cp_lexer_consume_token (parser->lexer);
15782 /* Parse the "class" or "struct", which indicates a scoped
15783 enumeration type in C++0x. */
15784 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15785 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15787 if (cxx_dialect < cxx11)
15788 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15790 /* Consume the `struct' or `class' token. */
15791 cp_lexer_consume_token (parser->lexer);
15793 scoped_enum_p = true;
15796 attributes = cp_parser_attributes_opt (parser);
15798 /* Clear the qualification. */
15799 parser->scope = NULL_TREE;
15800 parser->qualifying_scope = NULL_TREE;
15801 parser->object_scope = NULL_TREE;
15803 /* Figure out in what scope the declaration is being placed. */
15804 prev_scope = current_scope ();
15806 type_start_token = cp_lexer_peek_token (parser->lexer);
15808 push_deferring_access_checks (dk_no_check);
15809 nested_name_specifier
15810 = cp_parser_nested_name_specifier_opt (parser,
15811 /*typename_keyword_p=*/true,
15812 /*check_dependency_p=*/false,
15813 /*type_p=*/false,
15814 /*is_declaration=*/false);
15816 if (nested_name_specifier)
15818 tree name;
15820 identifier = cp_parser_identifier (parser);
15821 name = cp_parser_lookup_name (parser, identifier,
15822 enum_type,
15823 /*is_template=*/false,
15824 /*is_namespace=*/false,
15825 /*check_dependency=*/true,
15826 /*ambiguous_decls=*/NULL,
15827 input_location);
15828 if (name && name != error_mark_node)
15830 type = TREE_TYPE (name);
15831 if (TREE_CODE (type) == TYPENAME_TYPE)
15833 /* Are template enums allowed in ISO? */
15834 if (template_parm_scope_p ())
15835 pedwarn (type_start_token->location, OPT_Wpedantic,
15836 "%qD is an enumeration template", name);
15837 /* ignore a typename reference, for it will be solved by name
15838 in start_enum. */
15839 type = NULL_TREE;
15842 else if (nested_name_specifier == error_mark_node)
15843 /* We already issued an error. */;
15844 else
15845 error_at (type_start_token->location,
15846 "%qD is not an enumerator-name", identifier);
15848 else
15850 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15851 identifier = cp_parser_identifier (parser);
15852 else
15854 identifier = make_anon_name ();
15855 is_anonymous = true;
15856 if (scoped_enum_p)
15857 error_at (type_start_token->location,
15858 "anonymous scoped enum is not allowed");
15861 pop_deferring_access_checks ();
15863 /* Check for the `:' that denotes a specified underlying type in C++0x.
15864 Note that a ':' could also indicate a bitfield width, however. */
15865 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15867 cp_decl_specifier_seq type_specifiers;
15869 /* Consume the `:'. */
15870 cp_lexer_consume_token (parser->lexer);
15872 /* Parse the type-specifier-seq. */
15873 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15874 /*is_trailing_return=*/false,
15875 &type_specifiers);
15877 /* At this point this is surely not elaborated type specifier. */
15878 if (!cp_parser_parse_definitely (parser))
15879 return NULL_TREE;
15881 if (cxx_dialect < cxx11)
15882 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15884 has_underlying_type = true;
15886 /* If that didn't work, stop. */
15887 if (type_specifiers.type != error_mark_node)
15889 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15890 /*initialized=*/0, NULL);
15891 if (underlying_type == error_mark_node
15892 || check_for_bare_parameter_packs (underlying_type))
15893 underlying_type = NULL_TREE;
15897 /* Look for the `{' but don't consume it yet. */
15898 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15900 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15902 cp_parser_error (parser, "expected %<{%>");
15903 if (has_underlying_type)
15905 type = NULL_TREE;
15906 goto out;
15909 /* An opaque-enum-specifier must have a ';' here. */
15910 if ((scoped_enum_p || underlying_type)
15911 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15913 cp_parser_error (parser, "expected %<;%> or %<{%>");
15914 if (has_underlying_type)
15916 type = NULL_TREE;
15917 goto out;
15922 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15923 return NULL_TREE;
15925 if (nested_name_specifier)
15927 if (CLASS_TYPE_P (nested_name_specifier))
15929 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15930 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15931 push_scope (nested_name_specifier);
15933 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15935 push_nested_namespace (nested_name_specifier);
15939 /* Issue an error message if type-definitions are forbidden here. */
15940 if (!cp_parser_check_type_definition (parser))
15941 type = error_mark_node;
15942 else
15943 /* Create the new type. We do this before consuming the opening
15944 brace so the enum will be recorded as being on the line of its
15945 tag (or the 'enum' keyword, if there is no tag). */
15946 type = start_enum (identifier, type, underlying_type,
15947 scoped_enum_p, &is_new_type);
15949 /* If the next token is not '{' it is an opaque-enum-specifier or an
15950 elaborated-type-specifier. */
15951 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15953 timevar_push (TV_PARSE_ENUM);
15954 if (nested_name_specifier
15955 && nested_name_specifier != error_mark_node)
15957 /* The following catches invalid code such as:
15958 enum class S<int>::E { A, B, C }; */
15959 if (!processing_specialization
15960 && CLASS_TYPE_P (nested_name_specifier)
15961 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15962 error_at (type_start_token->location, "cannot add an enumerator "
15963 "list to a template instantiation");
15965 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15967 error_at (type_start_token->location,
15968 "%<%T::%E%> has not been declared",
15969 TYPE_CONTEXT (nested_name_specifier),
15970 nested_name_specifier);
15971 type = error_mark_node;
15973 /* If that scope does not contain the scope in which the
15974 class was originally declared, the program is invalid. */
15975 else if (prev_scope && !is_ancestor (prev_scope,
15976 nested_name_specifier))
15978 if (at_namespace_scope_p ())
15979 error_at (type_start_token->location,
15980 "declaration of %qD in namespace %qD which does not "
15981 "enclose %qD",
15982 type, prev_scope, nested_name_specifier);
15983 else
15984 error_at (type_start_token->location,
15985 "declaration of %qD in %qD which does not "
15986 "enclose %qD",
15987 type, prev_scope, nested_name_specifier);
15988 type = error_mark_node;
15992 if (scoped_enum_p)
15993 begin_scope (sk_scoped_enum, type);
15995 /* Consume the opening brace. */
15996 cp_lexer_consume_token (parser->lexer);
15998 if (type == error_mark_node)
15999 ; /* Nothing to add */
16000 else if (OPAQUE_ENUM_P (type)
16001 || (cxx_dialect > cxx98 && processing_specialization))
16003 new_value_list = true;
16004 SET_OPAQUE_ENUM_P (type, false);
16005 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16007 else
16009 error_at (type_start_token->location,
16010 "multiple definition of %q#T", type);
16011 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
16012 "previous definition here");
16013 type = error_mark_node;
16016 if (type == error_mark_node)
16017 cp_parser_skip_to_end_of_block_or_statement (parser);
16018 /* If the next token is not '}', then there are some enumerators. */
16019 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16021 if (is_anonymous && !scoped_enum_p)
16022 pedwarn (type_start_token->location, OPT_Wpedantic,
16023 "ISO C++ forbids empty anonymous enum");
16025 else
16026 cp_parser_enumerator_list (parser, type);
16028 /* Consume the final '}'. */
16029 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16031 if (scoped_enum_p)
16032 finish_scope ();
16033 timevar_pop (TV_PARSE_ENUM);
16035 else
16037 /* If a ';' follows, then it is an opaque-enum-specifier
16038 and additional restrictions apply. */
16039 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16041 if (is_anonymous)
16042 error_at (type_start_token->location,
16043 "opaque-enum-specifier without name");
16044 else if (nested_name_specifier)
16045 error_at (type_start_token->location,
16046 "opaque-enum-specifier must use a simple identifier");
16050 /* Look for trailing attributes to apply to this enumeration, and
16051 apply them if appropriate. */
16052 if (cp_parser_allow_gnu_extensions_p (parser))
16054 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16055 trailing_attr = chainon (trailing_attr, attributes);
16056 cplus_decl_attributes (&type,
16057 trailing_attr,
16058 (int) ATTR_FLAG_TYPE_IN_PLACE);
16061 /* Finish up the enumeration. */
16062 if (type != error_mark_node)
16064 if (new_value_list)
16065 finish_enum_value_list (type);
16066 if (is_new_type)
16067 finish_enum (type);
16070 if (nested_name_specifier)
16072 if (CLASS_TYPE_P (nested_name_specifier))
16074 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16075 pop_scope (nested_name_specifier);
16077 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16079 pop_nested_namespace (nested_name_specifier);
16082 out:
16083 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16084 return type;
16087 /* Parse an enumerator-list. The enumerators all have the indicated
16088 TYPE.
16090 enumerator-list:
16091 enumerator-definition
16092 enumerator-list , enumerator-definition */
16094 static void
16095 cp_parser_enumerator_list (cp_parser* parser, tree type)
16097 while (true)
16099 /* Parse an enumerator-definition. */
16100 cp_parser_enumerator_definition (parser, type);
16102 /* If the next token is not a ',', we've reached the end of
16103 the list. */
16104 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16105 break;
16106 /* Otherwise, consume the `,' and keep going. */
16107 cp_lexer_consume_token (parser->lexer);
16108 /* If the next token is a `}', there is a trailing comma. */
16109 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16111 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16112 pedwarn (input_location, OPT_Wpedantic,
16113 "comma at end of enumerator list");
16114 break;
16119 /* Parse an enumerator-definition. The enumerator has the indicated
16120 TYPE.
16122 enumerator-definition:
16123 enumerator
16124 enumerator = constant-expression
16126 enumerator:
16127 identifier
16129 GNU Extensions:
16131 enumerator-definition:
16132 enumerator attributes [opt]
16133 enumerator attributes [opt] = constant-expression */
16135 static void
16136 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16138 tree identifier;
16139 tree value;
16140 location_t loc;
16142 /* Save the input location because we are interested in the location
16143 of the identifier and not the location of the explicit value. */
16144 loc = cp_lexer_peek_token (parser->lexer)->location;
16146 /* Look for the identifier. */
16147 identifier = cp_parser_identifier (parser);
16148 if (identifier == error_mark_node)
16149 return;
16151 /* Parse any specified attributes. */
16152 tree attrs = cp_parser_attributes_opt (parser);
16154 /* If the next token is an '=', then there is an explicit value. */
16155 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16157 /* Consume the `=' token. */
16158 cp_lexer_consume_token (parser->lexer);
16159 /* Parse the value. */
16160 value = cp_parser_constant_expression (parser);
16162 else
16163 value = NULL_TREE;
16165 /* If we are processing a template, make sure the initializer of the
16166 enumerator doesn't contain any bare template parameter pack. */
16167 if (check_for_bare_parameter_packs (value))
16168 value = error_mark_node;
16170 /* Create the enumerator. */
16171 build_enumerator (identifier, value, type, attrs, loc);
16174 /* Parse a namespace-name.
16176 namespace-name:
16177 original-namespace-name
16178 namespace-alias
16180 Returns the NAMESPACE_DECL for the namespace. */
16182 static tree
16183 cp_parser_namespace_name (cp_parser* parser)
16185 tree identifier;
16186 tree namespace_decl;
16188 cp_token *token = cp_lexer_peek_token (parser->lexer);
16190 /* Get the name of the namespace. */
16191 identifier = cp_parser_identifier (parser);
16192 if (identifier == error_mark_node)
16193 return error_mark_node;
16195 /* Look up the identifier in the currently active scope. Look only
16196 for namespaces, due to:
16198 [basic.lookup.udir]
16200 When looking up a namespace-name in a using-directive or alias
16201 definition, only namespace names are considered.
16203 And:
16205 [basic.lookup.qual]
16207 During the lookup of a name preceding the :: scope resolution
16208 operator, object, function, and enumerator names are ignored.
16210 (Note that cp_parser_qualifying_entity only calls this
16211 function if the token after the name is the scope resolution
16212 operator.) */
16213 namespace_decl = cp_parser_lookup_name (parser, identifier,
16214 none_type,
16215 /*is_template=*/false,
16216 /*is_namespace=*/true,
16217 /*check_dependency=*/true,
16218 /*ambiguous_decls=*/NULL,
16219 token->location);
16220 /* If it's not a namespace, issue an error. */
16221 if (namespace_decl == error_mark_node
16222 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16224 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16225 error_at (token->location, "%qD is not a namespace-name", identifier);
16226 cp_parser_error (parser, "expected namespace-name");
16227 namespace_decl = error_mark_node;
16230 return namespace_decl;
16233 /* Parse a namespace-definition.
16235 namespace-definition:
16236 named-namespace-definition
16237 unnamed-namespace-definition
16239 named-namespace-definition:
16240 original-namespace-definition
16241 extension-namespace-definition
16243 original-namespace-definition:
16244 namespace identifier { namespace-body }
16246 extension-namespace-definition:
16247 namespace original-namespace-name { namespace-body }
16249 unnamed-namespace-definition:
16250 namespace { namespace-body } */
16252 static void
16253 cp_parser_namespace_definition (cp_parser* parser)
16255 tree identifier, attribs;
16256 bool has_visibility;
16257 bool is_inline;
16259 cp_ensure_no_omp_declare_simd (parser);
16260 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16262 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16263 is_inline = true;
16264 cp_lexer_consume_token (parser->lexer);
16266 else
16267 is_inline = false;
16269 /* Look for the `namespace' keyword. */
16270 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16272 /* Get the name of the namespace. We do not attempt to distinguish
16273 between an original-namespace-definition and an
16274 extension-namespace-definition at this point. The semantic
16275 analysis routines are responsible for that. */
16276 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16277 identifier = cp_parser_identifier (parser);
16278 else
16279 identifier = NULL_TREE;
16281 /* Parse any specified attributes. */
16282 attribs = cp_parser_attributes_opt (parser);
16284 /* Look for the `{' to start the namespace. */
16285 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16286 /* Start the namespace. */
16287 push_namespace (identifier);
16289 /* "inline namespace" is equivalent to a stub namespace definition
16290 followed by a strong using directive. */
16291 if (is_inline)
16293 tree name_space = current_namespace;
16294 /* Set up namespace association. */
16295 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16296 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16297 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16298 /* Import the contents of the inline namespace. */
16299 pop_namespace ();
16300 do_using_directive (name_space);
16301 push_namespace (identifier);
16304 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16306 /* Parse the body of the namespace. */
16307 cp_parser_namespace_body (parser);
16309 if (has_visibility)
16310 pop_visibility (1);
16312 /* Finish the namespace. */
16313 pop_namespace ();
16314 /* Look for the final `}'. */
16315 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16318 /* Parse a namespace-body.
16320 namespace-body:
16321 declaration-seq [opt] */
16323 static void
16324 cp_parser_namespace_body (cp_parser* parser)
16326 cp_parser_declaration_seq_opt (parser);
16329 /* Parse a namespace-alias-definition.
16331 namespace-alias-definition:
16332 namespace identifier = qualified-namespace-specifier ; */
16334 static void
16335 cp_parser_namespace_alias_definition (cp_parser* parser)
16337 tree identifier;
16338 tree namespace_specifier;
16340 cp_token *token = cp_lexer_peek_token (parser->lexer);
16342 /* Look for the `namespace' keyword. */
16343 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16344 /* Look for the identifier. */
16345 identifier = cp_parser_identifier (parser);
16346 if (identifier == error_mark_node)
16347 return;
16348 /* Look for the `=' token. */
16349 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16350 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16352 error_at (token->location, "%<namespace%> definition is not allowed here");
16353 /* Skip the definition. */
16354 cp_lexer_consume_token (parser->lexer);
16355 if (cp_parser_skip_to_closing_brace (parser))
16356 cp_lexer_consume_token (parser->lexer);
16357 return;
16359 cp_parser_require (parser, CPP_EQ, RT_EQ);
16360 /* Look for the qualified-namespace-specifier. */
16361 namespace_specifier
16362 = cp_parser_qualified_namespace_specifier (parser);
16363 /* Look for the `;' token. */
16364 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16366 /* Register the alias in the symbol table. */
16367 do_namespace_alias (identifier, namespace_specifier);
16370 /* Parse a qualified-namespace-specifier.
16372 qualified-namespace-specifier:
16373 :: [opt] nested-name-specifier [opt] namespace-name
16375 Returns a NAMESPACE_DECL corresponding to the specified
16376 namespace. */
16378 static tree
16379 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16381 /* Look for the optional `::'. */
16382 cp_parser_global_scope_opt (parser,
16383 /*current_scope_valid_p=*/false);
16385 /* Look for the optional nested-name-specifier. */
16386 cp_parser_nested_name_specifier_opt (parser,
16387 /*typename_keyword_p=*/false,
16388 /*check_dependency_p=*/true,
16389 /*type_p=*/false,
16390 /*is_declaration=*/true);
16392 return cp_parser_namespace_name (parser);
16395 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16396 access declaration.
16398 using-declaration:
16399 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16400 using :: unqualified-id ;
16402 access-declaration:
16403 qualified-id ;
16407 static bool
16408 cp_parser_using_declaration (cp_parser* parser,
16409 bool access_declaration_p)
16411 cp_token *token;
16412 bool typename_p = false;
16413 bool global_scope_p;
16414 tree decl;
16415 tree identifier;
16416 tree qscope;
16417 int oldcount = errorcount;
16418 cp_token *diag_token = NULL;
16420 if (access_declaration_p)
16422 diag_token = cp_lexer_peek_token (parser->lexer);
16423 cp_parser_parse_tentatively (parser);
16425 else
16427 /* Look for the `using' keyword. */
16428 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16430 /* Peek at the next token. */
16431 token = cp_lexer_peek_token (parser->lexer);
16432 /* See if it's `typename'. */
16433 if (token->keyword == RID_TYPENAME)
16435 /* Remember that we've seen it. */
16436 typename_p = true;
16437 /* Consume the `typename' token. */
16438 cp_lexer_consume_token (parser->lexer);
16442 /* Look for the optional global scope qualification. */
16443 global_scope_p
16444 = (cp_parser_global_scope_opt (parser,
16445 /*current_scope_valid_p=*/false)
16446 != NULL_TREE);
16448 /* If we saw `typename', or didn't see `::', then there must be a
16449 nested-name-specifier present. */
16450 if (typename_p || !global_scope_p)
16452 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16453 /*check_dependency_p=*/true,
16454 /*type_p=*/false,
16455 /*is_declaration=*/true);
16456 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16458 cp_parser_skip_to_end_of_block_or_statement (parser);
16459 return false;
16462 /* Otherwise, we could be in either of the two productions. In that
16463 case, treat the nested-name-specifier as optional. */
16464 else
16465 qscope = cp_parser_nested_name_specifier_opt (parser,
16466 /*typename_keyword_p=*/false,
16467 /*check_dependency_p=*/true,
16468 /*type_p=*/false,
16469 /*is_declaration=*/true);
16470 if (!qscope)
16471 qscope = global_namespace;
16472 else if (UNSCOPED_ENUM_P (qscope))
16473 qscope = CP_TYPE_CONTEXT (qscope);
16475 if (access_declaration_p && cp_parser_error_occurred (parser))
16476 /* Something has already gone wrong; there's no need to parse
16477 further. Since an error has occurred, the return value of
16478 cp_parser_parse_definitely will be false, as required. */
16479 return cp_parser_parse_definitely (parser);
16481 token = cp_lexer_peek_token (parser->lexer);
16482 /* Parse the unqualified-id. */
16483 identifier = cp_parser_unqualified_id (parser,
16484 /*template_keyword_p=*/false,
16485 /*check_dependency_p=*/true,
16486 /*declarator_p=*/true,
16487 /*optional_p=*/false);
16489 if (access_declaration_p)
16491 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16492 cp_parser_simulate_error (parser);
16493 if (!cp_parser_parse_definitely (parser))
16494 return false;
16497 /* The function we call to handle a using-declaration is different
16498 depending on what scope we are in. */
16499 if (qscope == error_mark_node || identifier == error_mark_node)
16501 else if (!identifier_p (identifier)
16502 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16503 /* [namespace.udecl]
16505 A using declaration shall not name a template-id. */
16506 error_at (token->location,
16507 "a template-id may not appear in a using-declaration");
16508 else
16510 if (at_class_scope_p ())
16512 /* Create the USING_DECL. */
16513 decl = do_class_using_decl (parser->scope, identifier);
16515 if (decl && typename_p)
16516 USING_DECL_TYPENAME_P (decl) = 1;
16518 if (check_for_bare_parameter_packs (decl))
16520 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16521 return false;
16523 else
16524 /* Add it to the list of members in this class. */
16525 finish_member_declaration (decl);
16527 else
16529 decl = cp_parser_lookup_name_simple (parser,
16530 identifier,
16531 token->location);
16532 if (decl == error_mark_node)
16533 cp_parser_name_lookup_error (parser, identifier,
16534 decl, NLE_NULL,
16535 token->location);
16536 else if (check_for_bare_parameter_packs (decl))
16538 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16539 return false;
16541 else if (!at_namespace_scope_p ())
16542 do_local_using_decl (decl, qscope, identifier);
16543 else
16544 do_toplevel_using_decl (decl, qscope, identifier);
16548 /* Look for the final `;'. */
16549 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16551 if (access_declaration_p && errorcount == oldcount)
16552 warning_at (diag_token->location, OPT_Wdeprecated,
16553 "access declarations are deprecated "
16554 "in favour of using-declarations; "
16555 "suggestion: add the %<using%> keyword");
16557 return true;
16560 /* Parse an alias-declaration.
16562 alias-declaration:
16563 using identifier attribute-specifier-seq [opt] = type-id */
16565 static tree
16566 cp_parser_alias_declaration (cp_parser* parser)
16568 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16569 location_t id_location;
16570 cp_declarator *declarator;
16571 cp_decl_specifier_seq decl_specs;
16572 bool member_p;
16573 const char *saved_message = NULL;
16575 /* Look for the `using' keyword. */
16576 cp_token *using_token
16577 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16578 if (using_token == NULL)
16579 return error_mark_node;
16581 id_location = cp_lexer_peek_token (parser->lexer)->location;
16582 id = cp_parser_identifier (parser);
16583 if (id == error_mark_node)
16584 return error_mark_node;
16586 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16587 attributes = cp_parser_attributes_opt (parser);
16588 if (attributes == error_mark_node)
16589 return error_mark_node;
16591 cp_parser_require (parser, CPP_EQ, RT_EQ);
16593 if (cp_parser_error_occurred (parser))
16594 return error_mark_node;
16596 cp_parser_commit_to_tentative_parse (parser);
16598 /* Now we are going to parse the type-id of the declaration. */
16601 [dcl.type]/3 says:
16603 "A type-specifier-seq shall not define a class or enumeration
16604 unless it appears in the type-id of an alias-declaration (7.1.3) that
16605 is not the declaration of a template-declaration."
16607 In other words, if we currently are in an alias template, the
16608 type-id should not define a type.
16610 So let's set parser->type_definition_forbidden_message in that
16611 case; cp_parser_check_type_definition (called by
16612 cp_parser_class_specifier) will then emit an error if a type is
16613 defined in the type-id. */
16614 if (parser->num_template_parameter_lists)
16616 saved_message = parser->type_definition_forbidden_message;
16617 parser->type_definition_forbidden_message =
16618 G_("types may not be defined in alias template declarations");
16621 type = cp_parser_type_id (parser);
16623 /* Restore the error message if need be. */
16624 if (parser->num_template_parameter_lists)
16625 parser->type_definition_forbidden_message = saved_message;
16627 if (type == error_mark_node
16628 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16630 cp_parser_skip_to_end_of_block_or_statement (parser);
16631 return error_mark_node;
16634 /* A typedef-name can also be introduced by an alias-declaration. The
16635 identifier following the using keyword becomes a typedef-name. It has
16636 the same semantics as if it were introduced by the typedef
16637 specifier. In particular, it does not define a new type and it shall
16638 not appear in the type-id. */
16640 clear_decl_specs (&decl_specs);
16641 decl_specs.type = type;
16642 if (attributes != NULL_TREE)
16644 decl_specs.attributes = attributes;
16645 set_and_check_decl_spec_loc (&decl_specs,
16646 ds_attribute,
16647 attrs_token);
16649 set_and_check_decl_spec_loc (&decl_specs,
16650 ds_typedef,
16651 using_token);
16652 set_and_check_decl_spec_loc (&decl_specs,
16653 ds_alias,
16654 using_token);
16656 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16657 declarator->id_loc = id_location;
16659 member_p = at_class_scope_p ();
16660 if (member_p)
16661 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16662 NULL_TREE, attributes);
16663 else
16664 decl = start_decl (declarator, &decl_specs, 0,
16665 attributes, NULL_TREE, &pushed_scope);
16666 if (decl == error_mark_node)
16667 return decl;
16669 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16671 if (pushed_scope)
16672 pop_scope (pushed_scope);
16674 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16675 added into the symbol table; otherwise, return the TYPE_DECL. */
16676 if (DECL_LANG_SPECIFIC (decl)
16677 && DECL_TEMPLATE_INFO (decl)
16678 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16680 decl = DECL_TI_TEMPLATE (decl);
16681 if (member_p)
16682 check_member_template (decl);
16685 return decl;
16688 /* Parse a using-directive.
16690 using-directive:
16691 using namespace :: [opt] nested-name-specifier [opt]
16692 namespace-name ; */
16694 static void
16695 cp_parser_using_directive (cp_parser* parser)
16697 tree namespace_decl;
16698 tree attribs;
16700 /* Look for the `using' keyword. */
16701 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16702 /* And the `namespace' keyword. */
16703 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16704 /* Look for the optional `::' operator. */
16705 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16706 /* And the optional nested-name-specifier. */
16707 cp_parser_nested_name_specifier_opt (parser,
16708 /*typename_keyword_p=*/false,
16709 /*check_dependency_p=*/true,
16710 /*type_p=*/false,
16711 /*is_declaration=*/true);
16712 /* Get the namespace being used. */
16713 namespace_decl = cp_parser_namespace_name (parser);
16714 /* And any specified attributes. */
16715 attribs = cp_parser_attributes_opt (parser);
16716 /* Update the symbol table. */
16717 parse_using_directive (namespace_decl, attribs);
16718 /* Look for the final `;'. */
16719 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16722 /* Parse an asm-definition.
16724 asm-definition:
16725 asm ( string-literal ) ;
16727 GNU Extension:
16729 asm-definition:
16730 asm volatile [opt] ( string-literal ) ;
16731 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16732 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16733 : asm-operand-list [opt] ) ;
16734 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16735 : asm-operand-list [opt]
16736 : asm-clobber-list [opt] ) ;
16737 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16738 : asm-clobber-list [opt]
16739 : asm-goto-list ) ; */
16741 static void
16742 cp_parser_asm_definition (cp_parser* parser)
16744 tree string;
16745 tree outputs = NULL_TREE;
16746 tree inputs = NULL_TREE;
16747 tree clobbers = NULL_TREE;
16748 tree labels = NULL_TREE;
16749 tree asm_stmt;
16750 bool volatile_p = false;
16751 bool extended_p = false;
16752 bool invalid_inputs_p = false;
16753 bool invalid_outputs_p = false;
16754 bool goto_p = false;
16755 required_token missing = RT_NONE;
16757 /* Look for the `asm' keyword. */
16758 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16760 if (parser->in_function_body
16761 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16763 error ("%<asm%> in %<constexpr%> function");
16764 cp_function_chain->invalid_constexpr = true;
16767 /* See if the next token is `volatile'. */
16768 if (cp_parser_allow_gnu_extensions_p (parser)
16769 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16771 /* Remember that we saw the `volatile' keyword. */
16772 volatile_p = true;
16773 /* Consume the token. */
16774 cp_lexer_consume_token (parser->lexer);
16776 if (cp_parser_allow_gnu_extensions_p (parser)
16777 && parser->in_function_body
16778 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16780 /* Remember that we saw the `goto' keyword. */
16781 goto_p = true;
16782 /* Consume the token. */
16783 cp_lexer_consume_token (parser->lexer);
16785 /* Look for the opening `('. */
16786 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16787 return;
16788 /* Look for the string. */
16789 string = cp_parser_string_literal (parser, false, false);
16790 if (string == error_mark_node)
16792 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16793 /*consume_paren=*/true);
16794 return;
16797 /* If we're allowing GNU extensions, check for the extended assembly
16798 syntax. Unfortunately, the `:' tokens need not be separated by
16799 a space in C, and so, for compatibility, we tolerate that here
16800 too. Doing that means that we have to treat the `::' operator as
16801 two `:' tokens. */
16802 if (cp_parser_allow_gnu_extensions_p (parser)
16803 && parser->in_function_body
16804 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16805 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16807 bool inputs_p = false;
16808 bool clobbers_p = false;
16809 bool labels_p = false;
16811 /* The extended syntax was used. */
16812 extended_p = true;
16814 /* Look for outputs. */
16815 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16817 /* Consume the `:'. */
16818 cp_lexer_consume_token (parser->lexer);
16819 /* Parse the output-operands. */
16820 if (cp_lexer_next_token_is_not (parser->lexer,
16821 CPP_COLON)
16822 && cp_lexer_next_token_is_not (parser->lexer,
16823 CPP_SCOPE)
16824 && cp_lexer_next_token_is_not (parser->lexer,
16825 CPP_CLOSE_PAREN)
16826 && !goto_p)
16828 outputs = cp_parser_asm_operand_list (parser);
16829 if (outputs == error_mark_node)
16830 invalid_outputs_p = true;
16833 /* If the next token is `::', there are no outputs, and the
16834 next token is the beginning of the inputs. */
16835 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16836 /* The inputs are coming next. */
16837 inputs_p = true;
16839 /* Look for inputs. */
16840 if (inputs_p
16841 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16843 /* Consume the `:' or `::'. */
16844 cp_lexer_consume_token (parser->lexer);
16845 /* Parse the output-operands. */
16846 if (cp_lexer_next_token_is_not (parser->lexer,
16847 CPP_COLON)
16848 && cp_lexer_next_token_is_not (parser->lexer,
16849 CPP_SCOPE)
16850 && cp_lexer_next_token_is_not (parser->lexer,
16851 CPP_CLOSE_PAREN))
16853 inputs = cp_parser_asm_operand_list (parser);
16854 if (inputs == error_mark_node)
16855 invalid_inputs_p = true;
16858 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16859 /* The clobbers are coming next. */
16860 clobbers_p = true;
16862 /* Look for clobbers. */
16863 if (clobbers_p
16864 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16866 clobbers_p = true;
16867 /* Consume the `:' or `::'. */
16868 cp_lexer_consume_token (parser->lexer);
16869 /* Parse the clobbers. */
16870 if (cp_lexer_next_token_is_not (parser->lexer,
16871 CPP_COLON)
16872 && cp_lexer_next_token_is_not (parser->lexer,
16873 CPP_CLOSE_PAREN))
16874 clobbers = cp_parser_asm_clobber_list (parser);
16876 else if (goto_p
16877 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16878 /* The labels are coming next. */
16879 labels_p = true;
16881 /* Look for labels. */
16882 if (labels_p
16883 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16885 labels_p = true;
16886 /* Consume the `:' or `::'. */
16887 cp_lexer_consume_token (parser->lexer);
16888 /* Parse the labels. */
16889 labels = cp_parser_asm_label_list (parser);
16892 if (goto_p && !labels_p)
16893 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16895 else if (goto_p)
16896 missing = RT_COLON_SCOPE;
16898 /* Look for the closing `)'. */
16899 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16900 missing ? missing : RT_CLOSE_PAREN))
16901 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16902 /*consume_paren=*/true);
16903 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16905 if (!invalid_inputs_p && !invalid_outputs_p)
16907 /* Create the ASM_EXPR. */
16908 if (parser->in_function_body)
16910 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16911 inputs, clobbers, labels);
16912 /* If the extended syntax was not used, mark the ASM_EXPR. */
16913 if (!extended_p)
16915 tree temp = asm_stmt;
16916 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16917 temp = TREE_OPERAND (temp, 0);
16919 ASM_INPUT_P (temp) = 1;
16922 else
16923 symtab->finalize_toplevel_asm (string);
16927 /* Declarators [gram.dcl.decl] */
16929 /* Parse an init-declarator.
16931 init-declarator:
16932 declarator initializer [opt]
16934 GNU Extension:
16936 init-declarator:
16937 declarator asm-specification [opt] attributes [opt] initializer [opt]
16939 function-definition:
16940 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16941 function-body
16942 decl-specifier-seq [opt] declarator function-try-block
16944 GNU Extension:
16946 function-definition:
16947 __extension__ function-definition
16949 TM Extension:
16951 function-definition:
16952 decl-specifier-seq [opt] declarator function-transaction-block
16954 The DECL_SPECIFIERS apply to this declarator. Returns a
16955 representation of the entity declared. If MEMBER_P is TRUE, then
16956 this declarator appears in a class scope. The new DECL created by
16957 this declarator is returned.
16959 The CHECKS are access checks that should be performed once we know
16960 what entity is being declared (and, therefore, what classes have
16961 befriended it).
16963 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16964 for a function-definition here as well. If the declarator is a
16965 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16966 be TRUE upon return. By that point, the function-definition will
16967 have been completely parsed.
16969 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16970 is FALSE.
16972 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16973 parsed declaration if it is an uninitialized single declarator not followed
16974 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16975 if present, will not be consumed. If returned, this declarator will be
16976 created with SD_INITIALIZED but will not call cp_finish_decl.
16978 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16979 and there is an initializer, the pointed location_t is set to the
16980 location of the '=' or `(', or '{' in C++11 token introducing the
16981 initializer. */
16983 static tree
16984 cp_parser_init_declarator (cp_parser* parser,
16985 cp_decl_specifier_seq *decl_specifiers,
16986 vec<deferred_access_check, va_gc> *checks,
16987 bool function_definition_allowed_p,
16988 bool member_p,
16989 int declares_class_or_enum,
16990 bool* function_definition_p,
16991 tree* maybe_range_for_decl,
16992 location_t* init_loc)
16994 cp_token *token = NULL, *asm_spec_start_token = NULL,
16995 *attributes_start_token = NULL;
16996 cp_declarator *declarator;
16997 tree prefix_attributes;
16998 tree attributes = NULL;
16999 tree asm_specification;
17000 tree initializer;
17001 tree decl = NULL_TREE;
17002 tree scope;
17003 int is_initialized;
17004 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
17005 initialized with "= ..", CPP_OPEN_PAREN if initialized with
17006 "(...)". */
17007 enum cpp_ttype initialization_kind;
17008 bool is_direct_init = false;
17009 bool is_non_constant_init;
17010 int ctor_dtor_or_conv_p;
17011 bool friend_p = cp_parser_friend_p (decl_specifiers);
17012 tree pushed_scope = NULL_TREE;
17013 bool range_for_decl_p = false;
17014 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17015 location_t tmp_init_loc = UNKNOWN_LOCATION;
17017 /* Gather the attributes that were provided with the
17018 decl-specifiers. */
17019 prefix_attributes = decl_specifiers->attributes;
17021 /* Assume that this is not the declarator for a function
17022 definition. */
17023 if (function_definition_p)
17024 *function_definition_p = false;
17026 /* Default arguments are only permitted for function parameters. */
17027 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
17028 parser->default_arg_ok_p = false;
17030 /* Defer access checks while parsing the declarator; we cannot know
17031 what names are accessible until we know what is being
17032 declared. */
17033 resume_deferring_access_checks ();
17035 /* Parse the declarator. */
17036 token = cp_lexer_peek_token (parser->lexer);
17037 declarator
17038 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17039 &ctor_dtor_or_conv_p,
17040 /*parenthesized_p=*/NULL,
17041 member_p, friend_p);
17042 /* Gather up the deferred checks. */
17043 stop_deferring_access_checks ();
17045 parser->default_arg_ok_p = saved_default_arg_ok_p;
17047 /* If the DECLARATOR was erroneous, there's no need to go
17048 further. */
17049 if (declarator == cp_error_declarator)
17050 return error_mark_node;
17052 /* Check that the number of template-parameter-lists is OK. */
17053 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17054 token->location))
17055 return error_mark_node;
17057 if (declares_class_or_enum & 2)
17058 cp_parser_check_for_definition_in_return_type (declarator,
17059 decl_specifiers->type,
17060 decl_specifiers->locations[ds_type_spec]);
17062 /* Figure out what scope the entity declared by the DECLARATOR is
17063 located in. `grokdeclarator' sometimes changes the scope, so
17064 we compute it now. */
17065 scope = get_scope_of_declarator (declarator);
17067 /* Perform any lookups in the declared type which were thought to be
17068 dependent, but are not in the scope of the declarator. */
17069 decl_specifiers->type
17070 = maybe_update_decl_type (decl_specifiers->type, scope);
17072 /* If we're allowing GNU extensions, look for an
17073 asm-specification. */
17074 if (cp_parser_allow_gnu_extensions_p (parser))
17076 /* Look for an asm-specification. */
17077 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17078 asm_specification = cp_parser_asm_specification_opt (parser);
17080 else
17081 asm_specification = NULL_TREE;
17083 /* Look for attributes. */
17084 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17085 attributes = cp_parser_attributes_opt (parser);
17087 /* Peek at the next token. */
17088 token = cp_lexer_peek_token (parser->lexer);
17090 bool bogus_implicit_tmpl = false;
17092 if (function_declarator_p (declarator))
17094 /* Check to see if the token indicates the start of a
17095 function-definition. */
17096 if (cp_parser_token_starts_function_definition_p (token))
17098 if (!function_definition_allowed_p)
17100 /* If a function-definition should not appear here, issue an
17101 error message. */
17102 cp_parser_error (parser,
17103 "a function-definition is not allowed here");
17104 return error_mark_node;
17107 location_t func_brace_location
17108 = cp_lexer_peek_token (parser->lexer)->location;
17110 /* Neither attributes nor an asm-specification are allowed
17111 on a function-definition. */
17112 if (asm_specification)
17113 error_at (asm_spec_start_token->location,
17114 "an asm-specification is not allowed "
17115 "on a function-definition");
17116 if (attributes)
17117 error_at (attributes_start_token->location,
17118 "attributes are not allowed "
17119 "on a function-definition");
17120 /* This is a function-definition. */
17121 *function_definition_p = true;
17123 /* Parse the function definition. */
17124 if (member_p)
17125 decl = cp_parser_save_member_function_body (parser,
17126 decl_specifiers,
17127 declarator,
17128 prefix_attributes);
17129 else
17130 decl =
17131 (cp_parser_function_definition_from_specifiers_and_declarator
17132 (parser, decl_specifiers, prefix_attributes, declarator));
17134 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17136 /* This is where the prologue starts... */
17137 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17138 = func_brace_location;
17141 return decl;
17144 else if (parser->fully_implicit_function_template_p)
17146 /* A non-template declaration involving a function parameter list
17147 containing an implicit template parameter will be made into a
17148 template. If the resulting declaration is not going to be an
17149 actual function then finish the template scope here to prevent it.
17150 An error message will be issued once we have a decl to talk about.
17152 FIXME probably we should do type deduction rather than create an
17153 implicit template, but the standard currently doesn't allow it. */
17154 bogus_implicit_tmpl = true;
17155 finish_fully_implicit_template (parser, NULL_TREE);
17158 /* [dcl.dcl]
17160 Only in function declarations for constructors, destructors, and
17161 type conversions can the decl-specifier-seq be omitted.
17163 We explicitly postpone this check past the point where we handle
17164 function-definitions because we tolerate function-definitions
17165 that are missing their return types in some modes. */
17166 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17168 cp_parser_error (parser,
17169 "expected constructor, destructor, or type conversion");
17170 return error_mark_node;
17173 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17174 if (token->type == CPP_EQ
17175 || token->type == CPP_OPEN_PAREN
17176 || token->type == CPP_OPEN_BRACE)
17178 is_initialized = SD_INITIALIZED;
17179 initialization_kind = token->type;
17180 if (maybe_range_for_decl)
17181 *maybe_range_for_decl = error_mark_node;
17182 tmp_init_loc = token->location;
17183 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17184 *init_loc = tmp_init_loc;
17186 if (token->type == CPP_EQ
17187 && function_declarator_p (declarator))
17189 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17190 if (t2->keyword == RID_DEFAULT)
17191 is_initialized = SD_DEFAULTED;
17192 else if (t2->keyword == RID_DELETE)
17193 is_initialized = SD_DELETED;
17196 else
17198 /* If the init-declarator isn't initialized and isn't followed by a
17199 `,' or `;', it's not a valid init-declarator. */
17200 if (token->type != CPP_COMMA
17201 && token->type != CPP_SEMICOLON)
17203 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17204 range_for_decl_p = true;
17205 else
17207 if (!maybe_range_for_decl)
17208 cp_parser_error (parser, "expected initializer");
17209 return error_mark_node;
17212 is_initialized = SD_UNINITIALIZED;
17213 initialization_kind = CPP_EOF;
17216 /* Because start_decl has side-effects, we should only call it if we
17217 know we're going ahead. By this point, we know that we cannot
17218 possibly be looking at any other construct. */
17219 cp_parser_commit_to_tentative_parse (parser);
17221 /* Enter the newly declared entry in the symbol table. If we're
17222 processing a declaration in a class-specifier, we wait until
17223 after processing the initializer. */
17224 if (!member_p)
17226 if (parser->in_unbraced_linkage_specification_p)
17227 decl_specifiers->storage_class = sc_extern;
17228 decl = start_decl (declarator, decl_specifiers,
17229 range_for_decl_p? SD_INITIALIZED : is_initialized,
17230 attributes, prefix_attributes, &pushed_scope);
17231 cp_finalize_omp_declare_simd (parser, decl);
17232 /* Adjust location of decl if declarator->id_loc is more appropriate:
17233 set, and decl wasn't merged with another decl, in which case its
17234 location would be different from input_location, and more accurate. */
17235 if (DECL_P (decl)
17236 && declarator->id_loc != UNKNOWN_LOCATION
17237 && DECL_SOURCE_LOCATION (decl) == input_location)
17238 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17240 else if (scope)
17241 /* Enter the SCOPE. That way unqualified names appearing in the
17242 initializer will be looked up in SCOPE. */
17243 pushed_scope = push_scope (scope);
17245 /* Perform deferred access control checks, now that we know in which
17246 SCOPE the declared entity resides. */
17247 if (!member_p && decl)
17249 tree saved_current_function_decl = NULL_TREE;
17251 /* If the entity being declared is a function, pretend that we
17252 are in its scope. If it is a `friend', it may have access to
17253 things that would not otherwise be accessible. */
17254 if (TREE_CODE (decl) == FUNCTION_DECL)
17256 saved_current_function_decl = current_function_decl;
17257 current_function_decl = decl;
17260 /* Perform access checks for template parameters. */
17261 cp_parser_perform_template_parameter_access_checks (checks);
17263 /* Perform the access control checks for the declarator and the
17264 decl-specifiers. */
17265 perform_deferred_access_checks (tf_warning_or_error);
17267 /* Restore the saved value. */
17268 if (TREE_CODE (decl) == FUNCTION_DECL)
17269 current_function_decl = saved_current_function_decl;
17272 /* Parse the initializer. */
17273 initializer = NULL_TREE;
17274 is_direct_init = false;
17275 is_non_constant_init = true;
17276 if (is_initialized)
17278 if (function_declarator_p (declarator))
17280 if (initialization_kind == CPP_EQ)
17281 initializer = cp_parser_pure_specifier (parser);
17282 else
17284 /* If the declaration was erroneous, we don't really
17285 know what the user intended, so just silently
17286 consume the initializer. */
17287 if (decl != error_mark_node)
17288 error_at (tmp_init_loc, "initializer provided for function");
17289 cp_parser_skip_to_closing_parenthesis (parser,
17290 /*recovering=*/true,
17291 /*or_comma=*/false,
17292 /*consume_paren=*/true);
17295 else
17297 /* We want to record the extra mangling scope for in-class
17298 initializers of class members and initializers of static data
17299 member templates. The former involves deferring
17300 parsing of the initializer until end of class as with default
17301 arguments. So right here we only handle the latter. */
17302 if (!member_p && processing_template_decl)
17303 start_lambda_scope (decl);
17304 initializer = cp_parser_initializer (parser,
17305 &is_direct_init,
17306 &is_non_constant_init);
17307 if (!member_p && processing_template_decl)
17308 finish_lambda_scope ();
17309 if (initializer == error_mark_node)
17310 cp_parser_skip_to_end_of_statement (parser);
17314 /* The old parser allows attributes to appear after a parenthesized
17315 initializer. Mark Mitchell proposed removing this functionality
17316 on the GCC mailing lists on 2002-08-13. This parser accepts the
17317 attributes -- but ignores them. */
17318 if (cp_parser_allow_gnu_extensions_p (parser)
17319 && initialization_kind == CPP_OPEN_PAREN)
17320 if (cp_parser_attributes_opt (parser))
17321 warning (OPT_Wattributes,
17322 "attributes after parenthesized initializer ignored");
17324 /* And now complain about a non-function implicit template. */
17325 if (bogus_implicit_tmpl)
17326 error_at (DECL_SOURCE_LOCATION (decl),
17327 "non-function %qD declared as implicit template", decl);
17329 /* For an in-class declaration, use `grokfield' to create the
17330 declaration. */
17331 if (member_p)
17333 if (pushed_scope)
17335 pop_scope (pushed_scope);
17336 pushed_scope = NULL_TREE;
17338 decl = grokfield (declarator, decl_specifiers,
17339 initializer, !is_non_constant_init,
17340 /*asmspec=*/NULL_TREE,
17341 chainon (attributes, prefix_attributes));
17342 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17343 cp_parser_save_default_args (parser, decl);
17344 cp_finalize_omp_declare_simd (parser, decl);
17347 /* Finish processing the declaration. But, skip member
17348 declarations. */
17349 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17351 cp_finish_decl (decl,
17352 initializer, !is_non_constant_init,
17353 asm_specification,
17354 /* If the initializer is in parentheses, then this is
17355 a direct-initialization, which means that an
17356 `explicit' constructor is OK. Otherwise, an
17357 `explicit' constructor cannot be used. */
17358 ((is_direct_init || !is_initialized)
17359 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17361 else if ((cxx_dialect != cxx98) && friend_p
17362 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17363 /* Core issue #226 (C++0x only): A default template-argument
17364 shall not be specified in a friend class template
17365 declaration. */
17366 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17367 /*is_partial=*/false, /*is_friend_decl=*/1);
17369 if (!friend_p && pushed_scope)
17370 pop_scope (pushed_scope);
17372 if (function_declarator_p (declarator)
17373 && parser->fully_implicit_function_template_p)
17375 if (member_p)
17376 decl = finish_fully_implicit_template (parser, decl);
17377 else
17378 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17381 return decl;
17384 /* Parse a declarator.
17386 declarator:
17387 direct-declarator
17388 ptr-operator declarator
17390 abstract-declarator:
17391 ptr-operator abstract-declarator [opt]
17392 direct-abstract-declarator
17394 GNU Extensions:
17396 declarator:
17397 attributes [opt] direct-declarator
17398 attributes [opt] ptr-operator declarator
17400 abstract-declarator:
17401 attributes [opt] ptr-operator abstract-declarator [opt]
17402 attributes [opt] direct-abstract-declarator
17404 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17405 detect constructor, destructor or conversion operators. It is set
17406 to -1 if the declarator is a name, and +1 if it is a
17407 function. Otherwise it is set to zero. Usually you just want to
17408 test for >0, but internally the negative value is used.
17410 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17411 a decl-specifier-seq unless it declares a constructor, destructor,
17412 or conversion. It might seem that we could check this condition in
17413 semantic analysis, rather than parsing, but that makes it difficult
17414 to handle something like `f()'. We want to notice that there are
17415 no decl-specifiers, and therefore realize that this is an
17416 expression, not a declaration.)
17418 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17419 the declarator is a direct-declarator of the form "(...)".
17421 MEMBER_P is true iff this declarator is a member-declarator.
17423 FRIEND_P is true iff this declarator is a friend. */
17425 static cp_declarator *
17426 cp_parser_declarator (cp_parser* parser,
17427 cp_parser_declarator_kind dcl_kind,
17428 int* ctor_dtor_or_conv_p,
17429 bool* parenthesized_p,
17430 bool member_p, bool friend_p)
17432 cp_declarator *declarator;
17433 enum tree_code code;
17434 cp_cv_quals cv_quals;
17435 tree class_type;
17436 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17438 /* Assume this is not a constructor, destructor, or type-conversion
17439 operator. */
17440 if (ctor_dtor_or_conv_p)
17441 *ctor_dtor_or_conv_p = 0;
17443 if (cp_parser_allow_gnu_extensions_p (parser))
17444 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17446 /* Check for the ptr-operator production. */
17447 cp_parser_parse_tentatively (parser);
17448 /* Parse the ptr-operator. */
17449 code = cp_parser_ptr_operator (parser,
17450 &class_type,
17451 &cv_quals,
17452 &std_attributes);
17454 /* If that worked, then we have a ptr-operator. */
17455 if (cp_parser_parse_definitely (parser))
17457 /* If a ptr-operator was found, then this declarator was not
17458 parenthesized. */
17459 if (parenthesized_p)
17460 *parenthesized_p = true;
17461 /* The dependent declarator is optional if we are parsing an
17462 abstract-declarator. */
17463 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17464 cp_parser_parse_tentatively (parser);
17466 /* Parse the dependent declarator. */
17467 declarator = cp_parser_declarator (parser, dcl_kind,
17468 /*ctor_dtor_or_conv_p=*/NULL,
17469 /*parenthesized_p=*/NULL,
17470 /*member_p=*/false,
17471 friend_p);
17473 /* If we are parsing an abstract-declarator, we must handle the
17474 case where the dependent declarator is absent. */
17475 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17476 && !cp_parser_parse_definitely (parser))
17477 declarator = NULL;
17479 declarator = cp_parser_make_indirect_declarator
17480 (code, class_type, cv_quals, declarator, std_attributes);
17482 /* Everything else is a direct-declarator. */
17483 else
17485 if (parenthesized_p)
17486 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17487 CPP_OPEN_PAREN);
17488 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17489 ctor_dtor_or_conv_p,
17490 member_p, friend_p);
17493 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17494 declarator->attributes = gnu_attributes;
17495 return declarator;
17498 /* Parse a direct-declarator or direct-abstract-declarator.
17500 direct-declarator:
17501 declarator-id
17502 direct-declarator ( parameter-declaration-clause )
17503 cv-qualifier-seq [opt]
17504 ref-qualifier [opt]
17505 exception-specification [opt]
17506 direct-declarator [ constant-expression [opt] ]
17507 ( declarator )
17509 direct-abstract-declarator:
17510 direct-abstract-declarator [opt]
17511 ( parameter-declaration-clause )
17512 cv-qualifier-seq [opt]
17513 ref-qualifier [opt]
17514 exception-specification [opt]
17515 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17516 ( abstract-declarator )
17518 Returns a representation of the declarator. DCL_KIND is
17519 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17520 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17521 we are parsing a direct-declarator. It is
17522 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17523 of ambiguity we prefer an abstract declarator, as per
17524 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17525 as for cp_parser_declarator. */
17527 static cp_declarator *
17528 cp_parser_direct_declarator (cp_parser* parser,
17529 cp_parser_declarator_kind dcl_kind,
17530 int* ctor_dtor_or_conv_p,
17531 bool member_p, bool friend_p)
17533 cp_token *token;
17534 cp_declarator *declarator = NULL;
17535 tree scope = NULL_TREE;
17536 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17537 bool saved_in_declarator_p = parser->in_declarator_p;
17538 bool first = true;
17539 tree pushed_scope = NULL_TREE;
17541 while (true)
17543 /* Peek at the next token. */
17544 token = cp_lexer_peek_token (parser->lexer);
17545 if (token->type == CPP_OPEN_PAREN)
17547 /* This is either a parameter-declaration-clause, or a
17548 parenthesized declarator. When we know we are parsing a
17549 named declarator, it must be a parenthesized declarator
17550 if FIRST is true. For instance, `(int)' is a
17551 parameter-declaration-clause, with an omitted
17552 direct-abstract-declarator. But `((*))', is a
17553 parenthesized abstract declarator. Finally, when T is a
17554 template parameter `(T)' is a
17555 parameter-declaration-clause, and not a parenthesized
17556 named declarator.
17558 We first try and parse a parameter-declaration-clause,
17559 and then try a nested declarator (if FIRST is true).
17561 It is not an error for it not to be a
17562 parameter-declaration-clause, even when FIRST is
17563 false. Consider,
17565 int i (int);
17566 int i (3);
17568 The first is the declaration of a function while the
17569 second is the definition of a variable, including its
17570 initializer.
17572 Having seen only the parenthesis, we cannot know which of
17573 these two alternatives should be selected. Even more
17574 complex are examples like:
17576 int i (int (a));
17577 int i (int (3));
17579 The former is a function-declaration; the latter is a
17580 variable initialization.
17582 Thus again, we try a parameter-declaration-clause, and if
17583 that fails, we back out and return. */
17585 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17587 tree params;
17588 bool is_declarator = false;
17590 /* In a member-declarator, the only valid interpretation
17591 of a parenthesis is the start of a
17592 parameter-declaration-clause. (It is invalid to
17593 initialize a static data member with a parenthesized
17594 initializer; only the "=" form of initialization is
17595 permitted.) */
17596 if (!member_p)
17597 cp_parser_parse_tentatively (parser);
17599 /* Consume the `('. */
17600 cp_lexer_consume_token (parser->lexer);
17601 if (first)
17603 /* If this is going to be an abstract declarator, we're
17604 in a declarator and we can't have default args. */
17605 parser->default_arg_ok_p = false;
17606 parser->in_declarator_p = true;
17609 begin_scope (sk_function_parms, NULL_TREE);
17611 /* Parse the parameter-declaration-clause. */
17612 params = cp_parser_parameter_declaration_clause (parser);
17614 /* Consume the `)'. */
17615 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17617 /* If all went well, parse the cv-qualifier-seq,
17618 ref-qualifier and the exception-specification. */
17619 if (member_p || cp_parser_parse_definitely (parser))
17621 cp_cv_quals cv_quals;
17622 cp_virt_specifiers virt_specifiers;
17623 cp_ref_qualifier ref_qual;
17624 tree exception_specification;
17625 tree late_return;
17626 tree attrs;
17627 bool memfn = (member_p || (pushed_scope
17628 && CLASS_TYPE_P (pushed_scope)));
17630 is_declarator = true;
17632 if (ctor_dtor_or_conv_p)
17633 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17634 first = false;
17636 /* Parse the cv-qualifier-seq. */
17637 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17638 /* Parse the ref-qualifier. */
17639 ref_qual = cp_parser_ref_qualifier_opt (parser);
17640 /* And the exception-specification. */
17641 exception_specification
17642 = cp_parser_exception_specification_opt (parser);
17644 attrs = cp_parser_std_attribute_spec_seq (parser);
17646 /* In here, we handle cases where attribute is used after
17647 the function declaration. For example:
17648 void func (int x) __attribute__((vector(..))); */
17649 if (flag_cilkplus
17650 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17652 cp_parser_parse_tentatively (parser);
17653 tree attr = cp_parser_gnu_attributes_opt (parser);
17654 if (cp_lexer_next_token_is_not (parser->lexer,
17655 CPP_SEMICOLON)
17656 && cp_lexer_next_token_is_not (parser->lexer,
17657 CPP_OPEN_BRACE))
17658 cp_parser_abort_tentative_parse (parser);
17659 else if (!cp_parser_parse_definitely (parser))
17661 else
17662 attrs = chainon (attr, attrs);
17664 late_return = (cp_parser_late_return_type_opt
17665 (parser, declarator,
17666 memfn ? cv_quals : -1));
17669 /* Parse the virt-specifier-seq. */
17670 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17672 /* Create the function-declarator. */
17673 declarator = make_call_declarator (declarator,
17674 params,
17675 cv_quals,
17676 virt_specifiers,
17677 ref_qual,
17678 exception_specification,
17679 late_return);
17680 declarator->std_attributes = attrs;
17681 /* Any subsequent parameter lists are to do with
17682 return type, so are not those of the declared
17683 function. */
17684 parser->default_arg_ok_p = false;
17687 /* Remove the function parms from scope. */
17688 pop_bindings_and_leave_scope ();
17690 if (is_declarator)
17691 /* Repeat the main loop. */
17692 continue;
17695 /* If this is the first, we can try a parenthesized
17696 declarator. */
17697 if (first)
17699 bool saved_in_type_id_in_expr_p;
17701 parser->default_arg_ok_p = saved_default_arg_ok_p;
17702 parser->in_declarator_p = saved_in_declarator_p;
17704 /* Consume the `('. */
17705 cp_lexer_consume_token (parser->lexer);
17706 /* Parse the nested declarator. */
17707 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17708 parser->in_type_id_in_expr_p = true;
17709 declarator
17710 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17711 /*parenthesized_p=*/NULL,
17712 member_p, friend_p);
17713 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17714 first = false;
17715 /* Expect a `)'. */
17716 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17717 declarator = cp_error_declarator;
17718 if (declarator == cp_error_declarator)
17719 break;
17721 goto handle_declarator;
17723 /* Otherwise, we must be done. */
17724 else
17725 break;
17727 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17728 && token->type == CPP_OPEN_SQUARE
17729 && !cp_next_tokens_can_be_attribute_p (parser))
17731 /* Parse an array-declarator. */
17732 tree bounds, attrs;
17734 if (ctor_dtor_or_conv_p)
17735 *ctor_dtor_or_conv_p = 0;
17737 first = false;
17738 parser->default_arg_ok_p = false;
17739 parser->in_declarator_p = true;
17740 /* Consume the `['. */
17741 cp_lexer_consume_token (parser->lexer);
17742 /* Peek at the next token. */
17743 token = cp_lexer_peek_token (parser->lexer);
17744 /* If the next token is `]', then there is no
17745 constant-expression. */
17746 if (token->type != CPP_CLOSE_SQUARE)
17748 bool non_constant_p;
17749 bounds
17750 = cp_parser_constant_expression (parser,
17751 /*allow_non_constant=*/true,
17752 &non_constant_p);
17753 if (!non_constant_p)
17754 /* OK */;
17755 else if (error_operand_p (bounds))
17756 /* Already gave an error. */;
17757 else if (!parser->in_function_body
17758 || current_binding_level->kind == sk_function_parms)
17760 /* Normally, the array bound must be an integral constant
17761 expression. However, as an extension, we allow VLAs
17762 in function scopes as long as they aren't part of a
17763 parameter declaration. */
17764 cp_parser_error (parser,
17765 "array bound is not an integer constant");
17766 bounds = error_mark_node;
17768 else if (processing_template_decl
17769 && !type_dependent_expression_p (bounds))
17771 /* Remember this wasn't a constant-expression. */
17772 bounds = build_nop (TREE_TYPE (bounds), bounds);
17773 TREE_SIDE_EFFECTS (bounds) = 1;
17776 else
17777 bounds = NULL_TREE;
17778 /* Look for the closing `]'. */
17779 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17781 declarator = cp_error_declarator;
17782 break;
17785 attrs = cp_parser_std_attribute_spec_seq (parser);
17786 declarator = make_array_declarator (declarator, bounds);
17787 declarator->std_attributes = attrs;
17789 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17792 tree qualifying_scope;
17793 tree unqualified_name;
17794 tree attrs;
17795 special_function_kind sfk;
17796 bool abstract_ok;
17797 bool pack_expansion_p = false;
17798 cp_token *declarator_id_start_token;
17800 /* Parse a declarator-id */
17801 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17802 if (abstract_ok)
17804 cp_parser_parse_tentatively (parser);
17806 /* If we see an ellipsis, we should be looking at a
17807 parameter pack. */
17808 if (token->type == CPP_ELLIPSIS)
17810 /* Consume the `...' */
17811 cp_lexer_consume_token (parser->lexer);
17813 pack_expansion_p = true;
17817 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17818 unqualified_name
17819 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17820 qualifying_scope = parser->scope;
17821 if (abstract_ok)
17823 bool okay = false;
17825 if (!unqualified_name && pack_expansion_p)
17827 /* Check whether an error occurred. */
17828 okay = !cp_parser_error_occurred (parser);
17830 /* We already consumed the ellipsis to mark a
17831 parameter pack, but we have no way to report it,
17832 so abort the tentative parse. We will be exiting
17833 immediately anyway. */
17834 cp_parser_abort_tentative_parse (parser);
17836 else
17837 okay = cp_parser_parse_definitely (parser);
17839 if (!okay)
17840 unqualified_name = error_mark_node;
17841 else if (unqualified_name
17842 && (qualifying_scope
17843 || (!identifier_p (unqualified_name))))
17845 cp_parser_error (parser, "expected unqualified-id");
17846 unqualified_name = error_mark_node;
17850 if (!unqualified_name)
17851 return NULL;
17852 if (unqualified_name == error_mark_node)
17854 declarator = cp_error_declarator;
17855 pack_expansion_p = false;
17856 declarator->parameter_pack_p = false;
17857 break;
17860 attrs = cp_parser_std_attribute_spec_seq (parser);
17862 if (qualifying_scope && at_namespace_scope_p ()
17863 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17865 /* In the declaration of a member of a template class
17866 outside of the class itself, the SCOPE will sometimes
17867 be a TYPENAME_TYPE. For example, given:
17869 template <typename T>
17870 int S<T>::R::i = 3;
17872 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17873 this context, we must resolve S<T>::R to an ordinary
17874 type, rather than a typename type.
17876 The reason we normally avoid resolving TYPENAME_TYPEs
17877 is that a specialization of `S' might render
17878 `S<T>::R' not a type. However, if `S' is
17879 specialized, then this `i' will not be used, so there
17880 is no harm in resolving the types here. */
17881 tree type;
17883 /* Resolve the TYPENAME_TYPE. */
17884 type = resolve_typename_type (qualifying_scope,
17885 /*only_current_p=*/false);
17886 /* If that failed, the declarator is invalid. */
17887 if (TREE_CODE (type) == TYPENAME_TYPE)
17889 if (typedef_variant_p (type))
17890 error_at (declarator_id_start_token->location,
17891 "cannot define member of dependent typedef "
17892 "%qT", type);
17893 else
17894 error_at (declarator_id_start_token->location,
17895 "%<%T::%E%> is not a type",
17896 TYPE_CONTEXT (qualifying_scope),
17897 TYPE_IDENTIFIER (qualifying_scope));
17899 qualifying_scope = type;
17902 sfk = sfk_none;
17904 if (unqualified_name)
17906 tree class_type;
17908 if (qualifying_scope
17909 && CLASS_TYPE_P (qualifying_scope))
17910 class_type = qualifying_scope;
17911 else
17912 class_type = current_class_type;
17914 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17916 tree name_type = TREE_TYPE (unqualified_name);
17917 if (class_type && same_type_p (name_type, class_type))
17919 if (qualifying_scope
17920 && CLASSTYPE_USE_TEMPLATE (name_type))
17922 error_at (declarator_id_start_token->location,
17923 "invalid use of constructor as a template");
17924 inform (declarator_id_start_token->location,
17925 "use %<%T::%D%> instead of %<%T::%D%> to "
17926 "name the constructor in a qualified name",
17927 class_type,
17928 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17929 class_type, name_type);
17930 declarator = cp_error_declarator;
17931 break;
17933 else
17934 unqualified_name = constructor_name (class_type);
17936 else
17938 /* We do not attempt to print the declarator
17939 here because we do not have enough
17940 information about its original syntactic
17941 form. */
17942 cp_parser_error (parser, "invalid declarator");
17943 declarator = cp_error_declarator;
17944 break;
17948 if (class_type)
17950 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17951 sfk = sfk_destructor;
17952 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17953 sfk = sfk_conversion;
17954 else if (/* There's no way to declare a constructor
17955 for an anonymous type, even if the type
17956 got a name for linkage purposes. */
17957 !TYPE_WAS_ANONYMOUS (class_type)
17958 /* Handle correctly (c++/19200):
17960 struct S {
17961 struct T{};
17962 friend void S(T);
17965 and also:
17967 namespace N {
17968 void S();
17971 struct S {
17972 friend void N::S();
17973 }; */
17974 && !(friend_p
17975 && class_type != qualifying_scope)
17976 && constructor_name_p (unqualified_name,
17977 class_type))
17979 unqualified_name = constructor_name (class_type);
17980 sfk = sfk_constructor;
17982 else if (is_overloaded_fn (unqualified_name)
17983 && DECL_CONSTRUCTOR_P (get_first_fn
17984 (unqualified_name)))
17985 sfk = sfk_constructor;
17987 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17988 *ctor_dtor_or_conv_p = -1;
17991 declarator = make_id_declarator (qualifying_scope,
17992 unqualified_name,
17993 sfk);
17994 declarator->std_attributes = attrs;
17995 declarator->id_loc = token->location;
17996 declarator->parameter_pack_p = pack_expansion_p;
17998 if (pack_expansion_p)
17999 maybe_warn_variadic_templates ();
18002 handle_declarator:;
18003 scope = get_scope_of_declarator (declarator);
18004 if (scope)
18006 /* Any names that appear after the declarator-id for a
18007 member are looked up in the containing scope. */
18008 if (at_function_scope_p ())
18010 /* But declarations with qualified-ids can't appear in a
18011 function. */
18012 cp_parser_error (parser, "qualified-id in declaration");
18013 declarator = cp_error_declarator;
18014 break;
18016 pushed_scope = push_scope (scope);
18018 parser->in_declarator_p = true;
18019 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
18020 || (declarator && declarator->kind == cdk_id))
18021 /* Default args are only allowed on function
18022 declarations. */
18023 parser->default_arg_ok_p = saved_default_arg_ok_p;
18024 else
18025 parser->default_arg_ok_p = false;
18027 first = false;
18029 /* We're done. */
18030 else
18031 break;
18034 /* For an abstract declarator, we might wind up with nothing at this
18035 point. That's an error; the declarator is not optional. */
18036 if (!declarator)
18037 cp_parser_error (parser, "expected declarator");
18039 /* If we entered a scope, we must exit it now. */
18040 if (pushed_scope)
18041 pop_scope (pushed_scope);
18043 parser->default_arg_ok_p = saved_default_arg_ok_p;
18044 parser->in_declarator_p = saved_in_declarator_p;
18046 return declarator;
18049 /* Parse a ptr-operator.
18051 ptr-operator:
18052 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18053 * cv-qualifier-seq [opt]
18055 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18056 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18058 GNU Extension:
18060 ptr-operator:
18061 & cv-qualifier-seq [opt]
18063 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18064 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18065 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18066 filled in with the TYPE containing the member. *CV_QUALS is
18067 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18068 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18069 Note that the tree codes returned by this function have nothing
18070 to do with the types of trees that will be eventually be created
18071 to represent the pointer or reference type being parsed. They are
18072 just constants with suggestive names. */
18073 static enum tree_code
18074 cp_parser_ptr_operator (cp_parser* parser,
18075 tree* type,
18076 cp_cv_quals *cv_quals,
18077 tree *attributes)
18079 enum tree_code code = ERROR_MARK;
18080 cp_token *token;
18081 tree attrs = NULL_TREE;
18083 /* Assume that it's not a pointer-to-member. */
18084 *type = NULL_TREE;
18085 /* And that there are no cv-qualifiers. */
18086 *cv_quals = TYPE_UNQUALIFIED;
18088 /* Peek at the next token. */
18089 token = cp_lexer_peek_token (parser->lexer);
18091 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18092 if (token->type == CPP_MULT)
18093 code = INDIRECT_REF;
18094 else if (token->type == CPP_AND)
18095 code = ADDR_EXPR;
18096 else if ((cxx_dialect != cxx98) &&
18097 token->type == CPP_AND_AND) /* C++0x only */
18098 code = NON_LVALUE_EXPR;
18100 if (code != ERROR_MARK)
18102 /* Consume the `*', `&' or `&&'. */
18103 cp_lexer_consume_token (parser->lexer);
18105 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18106 `&', if we are allowing GNU extensions. (The only qualifier
18107 that can legally appear after `&' is `restrict', but that is
18108 enforced during semantic analysis. */
18109 if (code == INDIRECT_REF
18110 || cp_parser_allow_gnu_extensions_p (parser))
18111 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18113 attrs = cp_parser_std_attribute_spec_seq (parser);
18114 if (attributes != NULL)
18115 *attributes = attrs;
18117 else
18119 /* Try the pointer-to-member case. */
18120 cp_parser_parse_tentatively (parser);
18121 /* Look for the optional `::' operator. */
18122 cp_parser_global_scope_opt (parser,
18123 /*current_scope_valid_p=*/false);
18124 /* Look for the nested-name specifier. */
18125 token = cp_lexer_peek_token (parser->lexer);
18126 cp_parser_nested_name_specifier (parser,
18127 /*typename_keyword_p=*/false,
18128 /*check_dependency_p=*/true,
18129 /*type_p=*/false,
18130 /*is_declaration=*/false);
18131 /* If we found it, and the next token is a `*', then we are
18132 indeed looking at a pointer-to-member operator. */
18133 if (!cp_parser_error_occurred (parser)
18134 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18136 /* Indicate that the `*' operator was used. */
18137 code = INDIRECT_REF;
18139 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18140 error_at (token->location, "%qD is a namespace", parser->scope);
18141 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18142 error_at (token->location, "cannot form pointer to member of "
18143 "non-class %q#T", parser->scope);
18144 else
18146 /* The type of which the member is a member is given by the
18147 current SCOPE. */
18148 *type = parser->scope;
18149 /* The next name will not be qualified. */
18150 parser->scope = NULL_TREE;
18151 parser->qualifying_scope = NULL_TREE;
18152 parser->object_scope = NULL_TREE;
18153 /* Look for optional c++11 attributes. */
18154 attrs = cp_parser_std_attribute_spec_seq (parser);
18155 if (attributes != NULL)
18156 *attributes = attrs;
18157 /* Look for the optional cv-qualifier-seq. */
18158 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18161 /* If that didn't work we don't have a ptr-operator. */
18162 if (!cp_parser_parse_definitely (parser))
18163 cp_parser_error (parser, "expected ptr-operator");
18166 return code;
18169 /* Parse an (optional) cv-qualifier-seq.
18171 cv-qualifier-seq:
18172 cv-qualifier cv-qualifier-seq [opt]
18174 cv-qualifier:
18175 const
18176 volatile
18178 GNU Extension:
18180 cv-qualifier:
18181 __restrict__
18183 Returns a bitmask representing the cv-qualifiers. */
18185 static cp_cv_quals
18186 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18188 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18190 while (true)
18192 cp_token *token;
18193 cp_cv_quals cv_qualifier;
18195 /* Peek at the next token. */
18196 token = cp_lexer_peek_token (parser->lexer);
18197 /* See if it's a cv-qualifier. */
18198 switch (token->keyword)
18200 case RID_CONST:
18201 cv_qualifier = TYPE_QUAL_CONST;
18202 break;
18204 case RID_VOLATILE:
18205 cv_qualifier = TYPE_QUAL_VOLATILE;
18206 break;
18208 case RID_RESTRICT:
18209 cv_qualifier = TYPE_QUAL_RESTRICT;
18210 break;
18212 default:
18213 cv_qualifier = TYPE_UNQUALIFIED;
18214 break;
18217 if (!cv_qualifier)
18218 break;
18220 if (cv_quals & cv_qualifier)
18222 error_at (token->location, "duplicate cv-qualifier");
18223 cp_lexer_purge_token (parser->lexer);
18225 else
18227 cp_lexer_consume_token (parser->lexer);
18228 cv_quals |= cv_qualifier;
18232 return cv_quals;
18235 /* Parse an (optional) ref-qualifier
18237 ref-qualifier:
18241 Returns cp_ref_qualifier representing ref-qualifier. */
18243 static cp_ref_qualifier
18244 cp_parser_ref_qualifier_opt (cp_parser* parser)
18246 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18248 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18249 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18250 return ref_qual;
18252 while (true)
18254 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18255 cp_token *token = cp_lexer_peek_token (parser->lexer);
18257 switch (token->type)
18259 case CPP_AND:
18260 curr_ref_qual = REF_QUAL_LVALUE;
18261 break;
18263 case CPP_AND_AND:
18264 curr_ref_qual = REF_QUAL_RVALUE;
18265 break;
18267 default:
18268 curr_ref_qual = REF_QUAL_NONE;
18269 break;
18272 if (!curr_ref_qual)
18273 break;
18274 else if (ref_qual)
18276 error_at (token->location, "multiple ref-qualifiers");
18277 cp_lexer_purge_token (parser->lexer);
18279 else
18281 ref_qual = curr_ref_qual;
18282 cp_lexer_consume_token (parser->lexer);
18286 return ref_qual;
18289 /* Parse an (optional) virt-specifier-seq.
18291 virt-specifier-seq:
18292 virt-specifier virt-specifier-seq [opt]
18294 virt-specifier:
18295 override
18296 final
18298 Returns a bitmask representing the virt-specifiers. */
18300 static cp_virt_specifiers
18301 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18303 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18305 while (true)
18307 cp_token *token;
18308 cp_virt_specifiers virt_specifier;
18310 /* Peek at the next token. */
18311 token = cp_lexer_peek_token (parser->lexer);
18312 /* See if it's a virt-specifier-qualifier. */
18313 if (token->type != CPP_NAME)
18314 break;
18315 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18317 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18318 virt_specifier = VIRT_SPEC_OVERRIDE;
18320 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18322 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18323 virt_specifier = VIRT_SPEC_FINAL;
18325 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18327 virt_specifier = VIRT_SPEC_FINAL;
18329 else
18330 break;
18332 if (virt_specifiers & virt_specifier)
18334 error_at (token->location, "duplicate virt-specifier");
18335 cp_lexer_purge_token (parser->lexer);
18337 else
18339 cp_lexer_consume_token (parser->lexer);
18340 virt_specifiers |= virt_specifier;
18343 return virt_specifiers;
18346 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18347 is in scope even though it isn't real. */
18349 void
18350 inject_this_parameter (tree ctype, cp_cv_quals quals)
18352 tree this_parm;
18354 if (current_class_ptr)
18356 /* We don't clear this between NSDMIs. Is it already what we want? */
18357 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18358 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18359 && cp_type_quals (type) == quals)
18360 return;
18363 this_parm = build_this_parm (ctype, quals);
18364 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18365 current_class_ptr = NULL_TREE;
18366 current_class_ref
18367 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18368 current_class_ptr = this_parm;
18371 /* Return true iff our current scope is a non-static data member
18372 initializer. */
18374 bool
18375 parsing_nsdmi (void)
18377 /* We recognize NSDMI context by the context-less 'this' pointer set up
18378 by the function above. */
18379 if (current_class_ptr
18380 && TREE_CODE (current_class_ptr) == PARM_DECL
18381 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18382 return true;
18383 return false;
18386 /* Parse a late-specified return type, if any. This is not a separate
18387 non-terminal, but part of a function declarator, which looks like
18389 -> trailing-type-specifier-seq abstract-declarator(opt)
18391 Returns the type indicated by the type-id.
18393 In addition to this this parses any queued up omp declare simd
18394 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18396 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18397 function. */
18399 static tree
18400 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18401 cp_cv_quals quals)
18403 cp_token *token;
18404 tree type = NULL_TREE;
18405 bool declare_simd_p = (parser->omp_declare_simd
18406 && declarator
18407 && declarator->kind == cdk_id);
18409 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18410 && declarator && declarator->kind == cdk_id);
18412 /* Peek at the next token. */
18413 token = cp_lexer_peek_token (parser->lexer);
18414 /* A late-specified return type is indicated by an initial '->'. */
18415 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18416 return NULL_TREE;
18418 tree save_ccp = current_class_ptr;
18419 tree save_ccr = current_class_ref;
18420 if (quals >= 0)
18422 /* DR 1207: 'this' is in scope in the trailing return type. */
18423 inject_this_parameter (current_class_type, quals);
18426 if (token->type == CPP_DEREF)
18428 /* Consume the ->. */
18429 cp_lexer_consume_token (parser->lexer);
18431 type = cp_parser_trailing_type_id (parser);
18434 if (cilk_simd_fn_vector_p)
18435 declarator->std_attributes
18436 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18437 declarator->std_attributes);
18438 if (declare_simd_p)
18439 declarator->std_attributes
18440 = cp_parser_late_parsing_omp_declare_simd (parser,
18441 declarator->std_attributes);
18443 if (quals >= 0)
18445 current_class_ptr = save_ccp;
18446 current_class_ref = save_ccr;
18449 return type;
18452 /* Parse a declarator-id.
18454 declarator-id:
18455 id-expression
18456 :: [opt] nested-name-specifier [opt] type-name
18458 In the `id-expression' case, the value returned is as for
18459 cp_parser_id_expression if the id-expression was an unqualified-id.
18460 If the id-expression was a qualified-id, then a SCOPE_REF is
18461 returned. The first operand is the scope (either a NAMESPACE_DECL
18462 or TREE_TYPE), but the second is still just a representation of an
18463 unqualified-id. */
18465 static tree
18466 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18468 tree id;
18469 /* The expression must be an id-expression. Assume that qualified
18470 names are the names of types so that:
18472 template <class T>
18473 int S<T>::R::i = 3;
18475 will work; we must treat `S<T>::R' as the name of a type.
18476 Similarly, assume that qualified names are templates, where
18477 required, so that:
18479 template <class T>
18480 int S<T>::R<T>::i = 3;
18482 will work, too. */
18483 id = cp_parser_id_expression (parser,
18484 /*template_keyword_p=*/false,
18485 /*check_dependency_p=*/false,
18486 /*template_p=*/NULL,
18487 /*declarator_p=*/true,
18488 optional_p);
18489 if (id && BASELINK_P (id))
18490 id = BASELINK_FUNCTIONS (id);
18491 return id;
18494 /* Parse a type-id.
18496 type-id:
18497 type-specifier-seq abstract-declarator [opt]
18499 Returns the TYPE specified. */
18501 static tree
18502 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18503 bool is_trailing_return)
18505 cp_decl_specifier_seq type_specifier_seq;
18506 cp_declarator *abstract_declarator;
18508 /* Parse the type-specifier-seq. */
18509 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18510 is_trailing_return,
18511 &type_specifier_seq);
18512 if (type_specifier_seq.type == error_mark_node)
18513 return error_mark_node;
18515 /* There might or might not be an abstract declarator. */
18516 cp_parser_parse_tentatively (parser);
18517 /* Look for the declarator. */
18518 abstract_declarator
18519 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18520 /*parenthesized_p=*/NULL,
18521 /*member_p=*/false,
18522 /*friend_p=*/false);
18523 /* Check to see if there really was a declarator. */
18524 if (!cp_parser_parse_definitely (parser))
18525 abstract_declarator = NULL;
18527 if (type_specifier_seq.type
18528 /* None of the valid uses of 'auto' in C++14 involve the type-id
18529 nonterminal, but it is valid in a trailing-return-type. */
18530 && !(cxx_dialect >= cxx14 && is_trailing_return)
18531 && type_uses_auto (type_specifier_seq.type))
18533 /* A type-id with type 'auto' is only ok if the abstract declarator
18534 is a function declarator with a late-specified return type. */
18535 if (abstract_declarator
18536 && abstract_declarator->kind == cdk_function
18537 && abstract_declarator->u.function.late_return_type)
18538 /* OK */;
18539 else
18541 error ("invalid use of %<auto%>");
18542 return error_mark_node;
18546 return groktypename (&type_specifier_seq, abstract_declarator,
18547 is_template_arg);
18550 static tree cp_parser_type_id (cp_parser *parser)
18552 return cp_parser_type_id_1 (parser, false, false);
18555 static tree cp_parser_template_type_arg (cp_parser *parser)
18557 tree r;
18558 const char *saved_message = parser->type_definition_forbidden_message;
18559 parser->type_definition_forbidden_message
18560 = G_("types may not be defined in template arguments");
18561 r = cp_parser_type_id_1 (parser, true, false);
18562 parser->type_definition_forbidden_message = saved_message;
18563 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18565 error ("invalid use of %<auto%> in template argument");
18566 r = error_mark_node;
18568 return r;
18571 static tree cp_parser_trailing_type_id (cp_parser *parser)
18573 return cp_parser_type_id_1 (parser, false, true);
18576 /* Parse a type-specifier-seq.
18578 type-specifier-seq:
18579 type-specifier type-specifier-seq [opt]
18581 GNU extension:
18583 type-specifier-seq:
18584 attributes type-specifier-seq [opt]
18586 If IS_DECLARATION is true, we are at the start of a "condition" or
18587 exception-declaration, so we might be followed by a declarator-id.
18589 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18590 i.e. we've just seen "->".
18592 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18594 static void
18595 cp_parser_type_specifier_seq (cp_parser* parser,
18596 bool is_declaration,
18597 bool is_trailing_return,
18598 cp_decl_specifier_seq *type_specifier_seq)
18600 bool seen_type_specifier = false;
18601 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18602 cp_token *start_token = NULL;
18604 /* Clear the TYPE_SPECIFIER_SEQ. */
18605 clear_decl_specs (type_specifier_seq);
18607 /* In the context of a trailing return type, enum E { } is an
18608 elaborated-type-specifier followed by a function-body, not an
18609 enum-specifier. */
18610 if (is_trailing_return)
18611 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18613 /* Parse the type-specifiers and attributes. */
18614 while (true)
18616 tree type_specifier;
18617 bool is_cv_qualifier;
18619 /* Check for attributes first. */
18620 if (cp_next_tokens_can_be_attribute_p (parser))
18622 type_specifier_seq->attributes =
18623 chainon (type_specifier_seq->attributes,
18624 cp_parser_attributes_opt (parser));
18625 continue;
18628 /* record the token of the beginning of the type specifier seq,
18629 for error reporting purposes*/
18630 if (!start_token)
18631 start_token = cp_lexer_peek_token (parser->lexer);
18633 /* Look for the type-specifier. */
18634 type_specifier = cp_parser_type_specifier (parser,
18635 flags,
18636 type_specifier_seq,
18637 /*is_declaration=*/false,
18638 NULL,
18639 &is_cv_qualifier);
18640 if (!type_specifier)
18642 /* If the first type-specifier could not be found, this is not a
18643 type-specifier-seq at all. */
18644 if (!seen_type_specifier)
18646 /* Set in_declarator_p to avoid skipping to the semicolon. */
18647 int in_decl = parser->in_declarator_p;
18648 parser->in_declarator_p = true;
18650 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18651 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18652 cp_parser_error (parser, "expected type-specifier");
18654 parser->in_declarator_p = in_decl;
18656 type_specifier_seq->type = error_mark_node;
18657 return;
18659 /* If subsequent type-specifiers could not be found, the
18660 type-specifier-seq is complete. */
18661 break;
18664 seen_type_specifier = true;
18665 /* The standard says that a condition can be:
18667 type-specifier-seq declarator = assignment-expression
18669 However, given:
18671 struct S {};
18672 if (int S = ...)
18674 we should treat the "S" as a declarator, not as a
18675 type-specifier. The standard doesn't say that explicitly for
18676 type-specifier-seq, but it does say that for
18677 decl-specifier-seq in an ordinary declaration. Perhaps it
18678 would be clearer just to allow a decl-specifier-seq here, and
18679 then add a semantic restriction that if any decl-specifiers
18680 that are not type-specifiers appear, the program is invalid. */
18681 if (is_declaration && !is_cv_qualifier)
18682 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18686 /* Return whether the function currently being declared has an associated
18687 template parameter list. */
18689 static bool
18690 function_being_declared_is_template_p (cp_parser* parser)
18692 if (!current_template_parms || processing_template_parmlist)
18693 return false;
18695 if (parser->implicit_template_scope)
18696 return true;
18698 if (at_class_scope_p ()
18699 && TYPE_BEING_DEFINED (current_class_type))
18700 return parser->num_template_parameter_lists != 0;
18702 return ((int) parser->num_template_parameter_lists > template_class_depth
18703 (current_class_type));
18706 /* Parse a parameter-declaration-clause.
18708 parameter-declaration-clause:
18709 parameter-declaration-list [opt] ... [opt]
18710 parameter-declaration-list , ...
18712 Returns a representation for the parameter declarations. A return
18713 value of NULL indicates a parameter-declaration-clause consisting
18714 only of an ellipsis. */
18716 static tree
18717 cp_parser_parameter_declaration_clause (cp_parser* parser)
18719 tree parameters;
18720 cp_token *token;
18721 bool ellipsis_p;
18722 bool is_error;
18724 struct cleanup {
18725 cp_parser* parser;
18726 int auto_is_implicit_function_template_parm_p;
18727 ~cleanup() {
18728 parser->auto_is_implicit_function_template_parm_p
18729 = auto_is_implicit_function_template_parm_p;
18731 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18733 (void) cleanup;
18735 if (!processing_specialization
18736 && !processing_template_parmlist
18737 && !processing_explicit_instantiation)
18738 if (!current_function_decl
18739 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18740 parser->auto_is_implicit_function_template_parm_p = true;
18742 /* Peek at the next token. */
18743 token = cp_lexer_peek_token (parser->lexer);
18744 /* Check for trivial parameter-declaration-clauses. */
18745 if (token->type == CPP_ELLIPSIS)
18747 /* Consume the `...' token. */
18748 cp_lexer_consume_token (parser->lexer);
18749 return NULL_TREE;
18751 else if (token->type == CPP_CLOSE_PAREN)
18752 /* There are no parameters. */
18754 #ifndef NO_IMPLICIT_EXTERN_C
18755 if (in_system_header_at (input_location)
18756 && current_class_type == NULL
18757 && current_lang_name == lang_name_c)
18758 return NULL_TREE;
18759 else
18760 #endif
18761 return void_list_node;
18763 /* Check for `(void)', too, which is a special case. */
18764 else if (token->keyword == RID_VOID
18765 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18766 == CPP_CLOSE_PAREN))
18768 /* Consume the `void' token. */
18769 cp_lexer_consume_token (parser->lexer);
18770 /* There are no parameters. */
18771 return void_list_node;
18774 /* Parse the parameter-declaration-list. */
18775 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18776 /* If a parse error occurred while parsing the
18777 parameter-declaration-list, then the entire
18778 parameter-declaration-clause is erroneous. */
18779 if (is_error)
18780 return NULL;
18782 /* Peek at the next token. */
18783 token = cp_lexer_peek_token (parser->lexer);
18784 /* If it's a `,', the clause should terminate with an ellipsis. */
18785 if (token->type == CPP_COMMA)
18787 /* Consume the `,'. */
18788 cp_lexer_consume_token (parser->lexer);
18789 /* Expect an ellipsis. */
18790 ellipsis_p
18791 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18793 /* It might also be `...' if the optional trailing `,' was
18794 omitted. */
18795 else if (token->type == CPP_ELLIPSIS)
18797 /* Consume the `...' token. */
18798 cp_lexer_consume_token (parser->lexer);
18799 /* And remember that we saw it. */
18800 ellipsis_p = true;
18802 else
18803 ellipsis_p = false;
18805 /* Finish the parameter list. */
18806 if (!ellipsis_p)
18807 parameters = chainon (parameters, void_list_node);
18809 return parameters;
18812 /* Parse a parameter-declaration-list.
18814 parameter-declaration-list:
18815 parameter-declaration
18816 parameter-declaration-list , parameter-declaration
18818 Returns a representation of the parameter-declaration-list, as for
18819 cp_parser_parameter_declaration_clause. However, the
18820 `void_list_node' is never appended to the list. Upon return,
18821 *IS_ERROR will be true iff an error occurred. */
18823 static tree
18824 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18826 tree parameters = NULL_TREE;
18827 tree *tail = &parameters;
18828 bool saved_in_unbraced_linkage_specification_p;
18829 int index = 0;
18831 /* Assume all will go well. */
18832 *is_error = false;
18833 /* The special considerations that apply to a function within an
18834 unbraced linkage specifications do not apply to the parameters
18835 to the function. */
18836 saved_in_unbraced_linkage_specification_p
18837 = parser->in_unbraced_linkage_specification_p;
18838 parser->in_unbraced_linkage_specification_p = false;
18840 /* Look for more parameters. */
18841 while (true)
18843 cp_parameter_declarator *parameter;
18844 tree decl = error_mark_node;
18845 bool parenthesized_p = false;
18846 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18847 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18848 (current_template_parms)) : 0);
18850 /* Parse the parameter. */
18851 parameter
18852 = cp_parser_parameter_declaration (parser,
18853 /*template_parm_p=*/false,
18854 &parenthesized_p);
18856 /* We don't know yet if the enclosing context is deprecated, so wait
18857 and warn in grokparms if appropriate. */
18858 deprecated_state = DEPRECATED_SUPPRESS;
18860 if (parameter)
18862 /* If a function parameter pack was specified and an implicit template
18863 parameter was introduced during cp_parser_parameter_declaration,
18864 change any implicit parameters introduced into packs. */
18865 if (parser->implicit_template_parms
18866 && parameter->declarator
18867 && parameter->declarator->parameter_pack_p)
18869 int latest_template_parm_idx = TREE_VEC_LENGTH
18870 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18872 if (latest_template_parm_idx != template_parm_idx)
18873 parameter->decl_specifiers.type = convert_generic_types_to_packs
18874 (parameter->decl_specifiers.type,
18875 template_parm_idx, latest_template_parm_idx);
18878 decl = grokdeclarator (parameter->declarator,
18879 &parameter->decl_specifiers,
18880 PARM,
18881 parameter->default_argument != NULL_TREE,
18882 &parameter->decl_specifiers.attributes);
18885 deprecated_state = DEPRECATED_NORMAL;
18887 /* If a parse error occurred parsing the parameter declaration,
18888 then the entire parameter-declaration-list is erroneous. */
18889 if (decl == error_mark_node)
18891 *is_error = true;
18892 parameters = error_mark_node;
18893 break;
18896 if (parameter->decl_specifiers.attributes)
18897 cplus_decl_attributes (&decl,
18898 parameter->decl_specifiers.attributes,
18900 if (DECL_NAME (decl))
18901 decl = pushdecl (decl);
18903 if (decl != error_mark_node)
18905 retrofit_lang_decl (decl);
18906 DECL_PARM_INDEX (decl) = ++index;
18907 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18910 /* Add the new parameter to the list. */
18911 *tail = build_tree_list (parameter->default_argument, decl);
18912 tail = &TREE_CHAIN (*tail);
18914 /* Peek at the next token. */
18915 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18916 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18917 /* These are for Objective-C++ */
18918 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18919 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18920 /* The parameter-declaration-list is complete. */
18921 break;
18922 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18924 cp_token *token;
18926 /* Peek at the next token. */
18927 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18928 /* If it's an ellipsis, then the list is complete. */
18929 if (token->type == CPP_ELLIPSIS)
18930 break;
18931 /* Otherwise, there must be more parameters. Consume the
18932 `,'. */
18933 cp_lexer_consume_token (parser->lexer);
18934 /* When parsing something like:
18936 int i(float f, double d)
18938 we can tell after seeing the declaration for "f" that we
18939 are not looking at an initialization of a variable "i",
18940 but rather at the declaration of a function "i".
18942 Due to the fact that the parsing of template arguments
18943 (as specified to a template-id) requires backtracking we
18944 cannot use this technique when inside a template argument
18945 list. */
18946 if (!parser->in_template_argument_list_p
18947 && !parser->in_type_id_in_expr_p
18948 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18949 /* However, a parameter-declaration of the form
18950 "float(f)" (which is a valid declaration of a
18951 parameter "f") can also be interpreted as an
18952 expression (the conversion of "f" to "float"). */
18953 && !parenthesized_p)
18954 cp_parser_commit_to_tentative_parse (parser);
18956 else
18958 cp_parser_error (parser, "expected %<,%> or %<...%>");
18959 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18960 cp_parser_skip_to_closing_parenthesis (parser,
18961 /*recovering=*/true,
18962 /*or_comma=*/false,
18963 /*consume_paren=*/false);
18964 break;
18968 parser->in_unbraced_linkage_specification_p
18969 = saved_in_unbraced_linkage_specification_p;
18971 /* Reset implicit_template_scope if we are about to leave the function
18972 parameter list that introduced it. Note that for out-of-line member
18973 definitions, there will be one or more class scopes before we get to
18974 the template parameter scope. */
18976 if (cp_binding_level *its = parser->implicit_template_scope)
18977 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18979 while (maybe_its->kind == sk_class)
18980 maybe_its = maybe_its->level_chain;
18981 if (maybe_its == its)
18983 parser->implicit_template_parms = 0;
18984 parser->implicit_template_scope = 0;
18988 return parameters;
18991 /* Parse a parameter declaration.
18993 parameter-declaration:
18994 decl-specifier-seq ... [opt] declarator
18995 decl-specifier-seq declarator = assignment-expression
18996 decl-specifier-seq ... [opt] abstract-declarator [opt]
18997 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18999 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
19000 declares a template parameter. (In that case, a non-nested `>'
19001 token encountered during the parsing of the assignment-expression
19002 is not interpreted as a greater-than operator.)
19004 Returns a representation of the parameter, or NULL if an error
19005 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
19006 true iff the declarator is of the form "(p)". */
19008 static cp_parameter_declarator *
19009 cp_parser_parameter_declaration (cp_parser *parser,
19010 bool template_parm_p,
19011 bool *parenthesized_p)
19013 int declares_class_or_enum;
19014 cp_decl_specifier_seq decl_specifiers;
19015 cp_declarator *declarator;
19016 tree default_argument;
19017 cp_token *token = NULL, *declarator_token_start = NULL;
19018 const char *saved_message;
19020 /* In a template parameter, `>' is not an operator.
19022 [temp.param]
19024 When parsing a default template-argument for a non-type
19025 template-parameter, the first non-nested `>' is taken as the end
19026 of the template parameter-list rather than a greater-than
19027 operator. */
19029 /* Type definitions may not appear in parameter types. */
19030 saved_message = parser->type_definition_forbidden_message;
19031 parser->type_definition_forbidden_message
19032 = G_("types may not be defined in parameter types");
19034 /* Parse the declaration-specifiers. */
19035 cp_parser_decl_specifier_seq (parser,
19036 CP_PARSER_FLAGS_NONE,
19037 &decl_specifiers,
19038 &declares_class_or_enum);
19040 /* Complain about missing 'typename' or other invalid type names. */
19041 if (!decl_specifiers.any_type_specifiers_p
19042 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19043 decl_specifiers.type = error_mark_node;
19045 /* If an error occurred, there's no reason to attempt to parse the
19046 rest of the declaration. */
19047 if (cp_parser_error_occurred (parser))
19049 parser->type_definition_forbidden_message = saved_message;
19050 return NULL;
19053 /* Peek at the next token. */
19054 token = cp_lexer_peek_token (parser->lexer);
19056 /* If the next token is a `)', `,', `=', `>', or `...', then there
19057 is no declarator. However, when variadic templates are enabled,
19058 there may be a declarator following `...'. */
19059 if (token->type == CPP_CLOSE_PAREN
19060 || token->type == CPP_COMMA
19061 || token->type == CPP_EQ
19062 || token->type == CPP_GREATER)
19064 declarator = NULL;
19065 if (parenthesized_p)
19066 *parenthesized_p = false;
19068 /* Otherwise, there should be a declarator. */
19069 else
19071 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19072 parser->default_arg_ok_p = false;
19074 /* After seeing a decl-specifier-seq, if the next token is not a
19075 "(", there is no possibility that the code is a valid
19076 expression. Therefore, if parsing tentatively, we commit at
19077 this point. */
19078 if (!parser->in_template_argument_list_p
19079 /* In an expression context, having seen:
19081 (int((char ...
19083 we cannot be sure whether we are looking at a
19084 function-type (taking a "char" as a parameter) or a cast
19085 of some object of type "char" to "int". */
19086 && !parser->in_type_id_in_expr_p
19087 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19088 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19089 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19090 cp_parser_commit_to_tentative_parse (parser);
19091 /* Parse the declarator. */
19092 declarator_token_start = token;
19093 declarator = cp_parser_declarator (parser,
19094 CP_PARSER_DECLARATOR_EITHER,
19095 /*ctor_dtor_or_conv_p=*/NULL,
19096 parenthesized_p,
19097 /*member_p=*/false,
19098 /*friend_p=*/false);
19099 parser->default_arg_ok_p = saved_default_arg_ok_p;
19100 /* After the declarator, allow more attributes. */
19101 decl_specifiers.attributes
19102 = chainon (decl_specifiers.attributes,
19103 cp_parser_attributes_opt (parser));
19106 /* If the next token is an ellipsis, and we have not seen a
19107 declarator name, and the type of the declarator contains parameter
19108 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19109 a parameter pack expansion expression. Otherwise, leave the
19110 ellipsis for a C-style variadic function. */
19111 token = cp_lexer_peek_token (parser->lexer);
19112 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19114 tree type = decl_specifiers.type;
19116 if (type && DECL_P (type))
19117 type = TREE_TYPE (type);
19119 if (type
19120 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19121 && declarator_can_be_parameter_pack (declarator)
19122 && (!declarator || !declarator->parameter_pack_p)
19123 && uses_parameter_packs (type))
19125 /* Consume the `...'. */
19126 cp_lexer_consume_token (parser->lexer);
19127 maybe_warn_variadic_templates ();
19129 /* Build a pack expansion type */
19130 if (declarator)
19131 declarator->parameter_pack_p = true;
19132 else
19133 decl_specifiers.type = make_pack_expansion (type);
19137 /* The restriction on defining new types applies only to the type
19138 of the parameter, not to the default argument. */
19139 parser->type_definition_forbidden_message = saved_message;
19141 /* If the next token is `=', then process a default argument. */
19142 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19144 token = cp_lexer_peek_token (parser->lexer);
19145 /* If we are defining a class, then the tokens that make up the
19146 default argument must be saved and processed later. */
19147 if (!template_parm_p && at_class_scope_p ()
19148 && TYPE_BEING_DEFINED (current_class_type)
19149 && !LAMBDA_TYPE_P (current_class_type))
19150 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19151 /* Outside of a class definition, we can just parse the
19152 assignment-expression. */
19153 else
19154 default_argument
19155 = cp_parser_default_argument (parser, template_parm_p);
19157 if (!parser->default_arg_ok_p)
19159 if (flag_permissive)
19160 warning (0, "deprecated use of default argument for parameter of non-function");
19161 else
19163 error_at (token->location,
19164 "default arguments are only "
19165 "permitted for function parameters");
19166 default_argument = NULL_TREE;
19169 else if ((declarator && declarator->parameter_pack_p)
19170 || (decl_specifiers.type
19171 && PACK_EXPANSION_P (decl_specifiers.type)))
19173 /* Find the name of the parameter pack. */
19174 cp_declarator *id_declarator = declarator;
19175 while (id_declarator && id_declarator->kind != cdk_id)
19176 id_declarator = id_declarator->declarator;
19178 if (id_declarator && id_declarator->kind == cdk_id)
19179 error_at (declarator_token_start->location,
19180 template_parm_p
19181 ? G_("template parameter pack %qD "
19182 "cannot have a default argument")
19183 : G_("parameter pack %qD cannot have "
19184 "a default argument"),
19185 id_declarator->u.id.unqualified_name);
19186 else
19187 error_at (declarator_token_start->location,
19188 template_parm_p
19189 ? G_("template parameter pack cannot have "
19190 "a default argument")
19191 : G_("parameter pack cannot have a "
19192 "default argument"));
19194 default_argument = NULL_TREE;
19197 else
19198 default_argument = NULL_TREE;
19200 return make_parameter_declarator (&decl_specifiers,
19201 declarator,
19202 default_argument);
19205 /* Parse a default argument and return it.
19207 TEMPLATE_PARM_P is true if this is a default argument for a
19208 non-type template parameter. */
19209 static tree
19210 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19212 tree default_argument = NULL_TREE;
19213 bool saved_greater_than_is_operator_p;
19214 bool saved_local_variables_forbidden_p;
19215 bool non_constant_p, is_direct_init;
19217 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19218 set correctly. */
19219 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19220 parser->greater_than_is_operator_p = !template_parm_p;
19221 /* Local variable names (and the `this' keyword) may not
19222 appear in a default argument. */
19223 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19224 parser->local_variables_forbidden_p = true;
19225 /* Parse the assignment-expression. */
19226 if (template_parm_p)
19227 push_deferring_access_checks (dk_no_deferred);
19228 tree saved_class_ptr = NULL_TREE;
19229 tree saved_class_ref = NULL_TREE;
19230 /* The "this" pointer is not valid in a default argument. */
19231 if (cfun)
19233 saved_class_ptr = current_class_ptr;
19234 cp_function_chain->x_current_class_ptr = NULL_TREE;
19235 saved_class_ref = current_class_ref;
19236 cp_function_chain->x_current_class_ref = NULL_TREE;
19238 default_argument
19239 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19240 /* Restore the "this" pointer. */
19241 if (cfun)
19243 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19244 cp_function_chain->x_current_class_ref = saved_class_ref;
19246 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19247 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19248 if (template_parm_p)
19249 pop_deferring_access_checks ();
19250 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19251 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19253 return default_argument;
19256 /* Parse a function-body.
19258 function-body:
19259 compound_statement */
19261 static void
19262 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19264 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19267 /* Parse a ctor-initializer-opt followed by a function-body. Return
19268 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19269 is true we are parsing a function-try-block. */
19271 static bool
19272 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19273 bool in_function_try_block)
19275 tree body, list;
19276 bool ctor_initializer_p;
19277 const bool check_body_p =
19278 DECL_CONSTRUCTOR_P (current_function_decl)
19279 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19280 tree last = NULL;
19282 /* Begin the function body. */
19283 body = begin_function_body ();
19284 /* Parse the optional ctor-initializer. */
19285 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19287 /* If we're parsing a constexpr constructor definition, we need
19288 to check that the constructor body is indeed empty. However,
19289 before we get to cp_parser_function_body lot of junk has been
19290 generated, so we can't just check that we have an empty block.
19291 Rather we take a snapshot of the outermost block, and check whether
19292 cp_parser_function_body changed its state. */
19293 if (check_body_p)
19295 list = cur_stmt_list;
19296 if (STATEMENT_LIST_TAIL (list))
19297 last = STATEMENT_LIST_TAIL (list)->stmt;
19299 /* Parse the function-body. */
19300 cp_parser_function_body (parser, in_function_try_block);
19301 if (check_body_p)
19302 check_constexpr_ctor_body (last, list, /*complain=*/true);
19303 /* Finish the function body. */
19304 finish_function_body (body);
19306 return ctor_initializer_p;
19309 /* Parse an initializer.
19311 initializer:
19312 = initializer-clause
19313 ( expression-list )
19315 Returns an expression representing the initializer. If no
19316 initializer is present, NULL_TREE is returned.
19318 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19319 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19320 set to TRUE if there is no initializer present. If there is an
19321 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19322 is set to true; otherwise it is set to false. */
19324 static tree
19325 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19326 bool* non_constant_p)
19328 cp_token *token;
19329 tree init;
19331 /* Peek at the next token. */
19332 token = cp_lexer_peek_token (parser->lexer);
19334 /* Let our caller know whether or not this initializer was
19335 parenthesized. */
19336 *is_direct_init = (token->type != CPP_EQ);
19337 /* Assume that the initializer is constant. */
19338 *non_constant_p = false;
19340 if (token->type == CPP_EQ)
19342 /* Consume the `='. */
19343 cp_lexer_consume_token (parser->lexer);
19344 /* Parse the initializer-clause. */
19345 init = cp_parser_initializer_clause (parser, non_constant_p);
19347 else if (token->type == CPP_OPEN_PAREN)
19349 vec<tree, va_gc> *vec;
19350 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19351 /*cast_p=*/false,
19352 /*allow_expansion_p=*/true,
19353 non_constant_p);
19354 if (vec == NULL)
19355 return error_mark_node;
19356 init = build_tree_list_vec (vec);
19357 release_tree_vector (vec);
19359 else if (token->type == CPP_OPEN_BRACE)
19361 cp_lexer_set_source_position (parser->lexer);
19362 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19363 init = cp_parser_braced_list (parser, non_constant_p);
19364 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19366 else
19368 /* Anything else is an error. */
19369 cp_parser_error (parser, "expected initializer");
19370 init = error_mark_node;
19373 return init;
19376 /* Parse an initializer-clause.
19378 initializer-clause:
19379 assignment-expression
19380 braced-init-list
19382 Returns an expression representing the initializer.
19384 If the `assignment-expression' production is used the value
19385 returned is simply a representation for the expression.
19387 Otherwise, calls cp_parser_braced_list. */
19389 static tree
19390 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19392 tree initializer;
19394 /* Assume the expression is constant. */
19395 *non_constant_p = false;
19397 /* If it is not a `{', then we are looking at an
19398 assignment-expression. */
19399 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19401 initializer
19402 = cp_parser_constant_expression (parser,
19403 /*allow_non_constant_p=*/true,
19404 non_constant_p);
19406 else
19407 initializer = cp_parser_braced_list (parser, non_constant_p);
19409 return initializer;
19412 /* Parse a brace-enclosed initializer list.
19414 braced-init-list:
19415 { initializer-list , [opt] }
19418 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19419 the elements of the initializer-list (or NULL, if the last
19420 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19421 NULL_TREE. There is no way to detect whether or not the optional
19422 trailing `,' was provided. NON_CONSTANT_P is as for
19423 cp_parser_initializer. */
19425 static tree
19426 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19428 tree initializer;
19430 /* Consume the `{' token. */
19431 cp_lexer_consume_token (parser->lexer);
19432 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19433 initializer = make_node (CONSTRUCTOR);
19434 /* If it's not a `}', then there is a non-trivial initializer. */
19435 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19437 /* Parse the initializer list. */
19438 CONSTRUCTOR_ELTS (initializer)
19439 = cp_parser_initializer_list (parser, non_constant_p);
19440 /* A trailing `,' token is allowed. */
19441 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19442 cp_lexer_consume_token (parser->lexer);
19444 else
19445 *non_constant_p = false;
19446 /* Now, there should be a trailing `}'. */
19447 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19448 TREE_TYPE (initializer) = init_list_type_node;
19449 return initializer;
19452 /* Consume tokens up to, and including, the next non-nested closing `]'.
19453 Returns true iff we found a closing `]'. */
19455 static bool
19456 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19458 unsigned square_depth = 0;
19460 while (true)
19462 cp_token * token = cp_lexer_peek_token (parser->lexer);
19464 switch (token->type)
19466 case CPP_EOF:
19467 case CPP_PRAGMA_EOL:
19468 /* If we've run out of tokens, then there is no closing `]'. */
19469 return false;
19471 case CPP_OPEN_SQUARE:
19472 ++square_depth;
19473 break;
19475 case CPP_CLOSE_SQUARE:
19476 if (!square_depth--)
19478 cp_lexer_consume_token (parser->lexer);
19479 return true;
19481 break;
19483 default:
19484 break;
19487 /* Consume the token. */
19488 cp_lexer_consume_token (parser->lexer);
19492 /* Return true if we are looking at an array-designator, false otherwise. */
19494 static bool
19495 cp_parser_array_designator_p (cp_parser *parser)
19497 /* Consume the `['. */
19498 cp_lexer_consume_token (parser->lexer);
19500 cp_lexer_save_tokens (parser->lexer);
19502 /* Skip tokens until the next token is a closing square bracket.
19503 If we find the closing `]', and the next token is a `=', then
19504 we are looking at an array designator. */
19505 bool array_designator_p
19506 = (cp_parser_skip_to_closing_square_bracket (parser)
19507 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19509 /* Roll back the tokens we skipped. */
19510 cp_lexer_rollback_tokens (parser->lexer);
19512 return array_designator_p;
19515 /* Parse an initializer-list.
19517 initializer-list:
19518 initializer-clause ... [opt]
19519 initializer-list , initializer-clause ... [opt]
19521 GNU Extension:
19523 initializer-list:
19524 designation initializer-clause ...[opt]
19525 initializer-list , designation initializer-clause ...[opt]
19527 designation:
19528 . identifier =
19529 identifier :
19530 [ constant-expression ] =
19532 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19533 for the initializer. If the INDEX of the elt is non-NULL, it is the
19534 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19535 as for cp_parser_initializer. */
19537 static vec<constructor_elt, va_gc> *
19538 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19540 vec<constructor_elt, va_gc> *v = NULL;
19542 /* Assume all of the expressions are constant. */
19543 *non_constant_p = false;
19545 /* Parse the rest of the list. */
19546 while (true)
19548 cp_token *token;
19549 tree designator;
19550 tree initializer;
19551 bool clause_non_constant_p;
19553 /* If the next token is an identifier and the following one is a
19554 colon, we are looking at the GNU designated-initializer
19555 syntax. */
19556 if (cp_parser_allow_gnu_extensions_p (parser)
19557 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19558 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19560 /* Warn the user that they are using an extension. */
19561 pedwarn (input_location, OPT_Wpedantic,
19562 "ISO C++ does not allow designated initializers");
19563 /* Consume the identifier. */
19564 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19565 /* Consume the `:'. */
19566 cp_lexer_consume_token (parser->lexer);
19568 /* Also handle the C99 syntax, '. id ='. */
19569 else if (cp_parser_allow_gnu_extensions_p (parser)
19570 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19571 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19572 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19574 /* Warn the user that they are using an extension. */
19575 pedwarn (input_location, OPT_Wpedantic,
19576 "ISO C++ does not allow C99 designated initializers");
19577 /* Consume the `.'. */
19578 cp_lexer_consume_token (parser->lexer);
19579 /* Consume the identifier. */
19580 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19581 /* Consume the `='. */
19582 cp_lexer_consume_token (parser->lexer);
19584 /* Also handle C99 array designators, '[ const ] ='. */
19585 else if (cp_parser_allow_gnu_extensions_p (parser)
19586 && !c_dialect_objc ()
19587 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19589 /* In C++11, [ could start a lambda-introducer. */
19590 bool non_const = false;
19592 cp_parser_parse_tentatively (parser);
19594 if (!cp_parser_array_designator_p (parser))
19596 cp_parser_simulate_error (parser);
19597 designator = NULL_TREE;
19599 else
19601 designator = cp_parser_constant_expression (parser, true,
19602 &non_const);
19603 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19604 cp_parser_require (parser, CPP_EQ, RT_EQ);
19607 if (!cp_parser_parse_definitely (parser))
19608 designator = NULL_TREE;
19609 else if (non_const)
19610 require_potential_rvalue_constant_expression (designator);
19612 else
19613 designator = NULL_TREE;
19615 /* Parse the initializer. */
19616 initializer = cp_parser_initializer_clause (parser,
19617 &clause_non_constant_p);
19618 /* If any clause is non-constant, so is the entire initializer. */
19619 if (clause_non_constant_p)
19620 *non_constant_p = true;
19622 /* If we have an ellipsis, this is an initializer pack
19623 expansion. */
19624 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19626 /* Consume the `...'. */
19627 cp_lexer_consume_token (parser->lexer);
19629 /* Turn the initializer into an initializer expansion. */
19630 initializer = make_pack_expansion (initializer);
19633 /* Add it to the vector. */
19634 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19636 /* If the next token is not a comma, we have reached the end of
19637 the list. */
19638 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19639 break;
19641 /* Peek at the next token. */
19642 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19643 /* If the next token is a `}', then we're still done. An
19644 initializer-clause can have a trailing `,' after the
19645 initializer-list and before the closing `}'. */
19646 if (token->type == CPP_CLOSE_BRACE)
19647 break;
19649 /* Consume the `,' token. */
19650 cp_lexer_consume_token (parser->lexer);
19653 return v;
19656 /* Classes [gram.class] */
19658 /* Parse a class-name.
19660 class-name:
19661 identifier
19662 template-id
19664 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19665 to indicate that names looked up in dependent types should be
19666 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19667 keyword has been used to indicate that the name that appears next
19668 is a template. TAG_TYPE indicates the explicit tag given before
19669 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19670 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19671 is the class being defined in a class-head. If ENUM_OK is TRUE,
19672 enum-names are also accepted.
19674 Returns the TYPE_DECL representing the class. */
19676 static tree
19677 cp_parser_class_name (cp_parser *parser,
19678 bool typename_keyword_p,
19679 bool template_keyword_p,
19680 enum tag_types tag_type,
19681 bool check_dependency_p,
19682 bool class_head_p,
19683 bool is_declaration,
19684 bool enum_ok)
19686 tree decl;
19687 tree scope;
19688 bool typename_p;
19689 cp_token *token;
19690 tree identifier = NULL_TREE;
19692 /* All class-names start with an identifier. */
19693 token = cp_lexer_peek_token (parser->lexer);
19694 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19696 cp_parser_error (parser, "expected class-name");
19697 return error_mark_node;
19700 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19701 to a template-id, so we save it here. */
19702 scope = parser->scope;
19703 if (scope == error_mark_node)
19704 return error_mark_node;
19706 /* Any name names a type if we're following the `typename' keyword
19707 in a qualified name where the enclosing scope is type-dependent. */
19708 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19709 && dependent_type_p (scope));
19710 /* Handle the common case (an identifier, but not a template-id)
19711 efficiently. */
19712 if (token->type == CPP_NAME
19713 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19715 cp_token *identifier_token;
19716 bool ambiguous_p;
19718 /* Look for the identifier. */
19719 identifier_token = cp_lexer_peek_token (parser->lexer);
19720 ambiguous_p = identifier_token->error_reported;
19721 identifier = cp_parser_identifier (parser);
19722 /* If the next token isn't an identifier, we are certainly not
19723 looking at a class-name. */
19724 if (identifier == error_mark_node)
19725 decl = error_mark_node;
19726 /* If we know this is a type-name, there's no need to look it
19727 up. */
19728 else if (typename_p)
19729 decl = identifier;
19730 else
19732 tree ambiguous_decls;
19733 /* If we already know that this lookup is ambiguous, then
19734 we've already issued an error message; there's no reason
19735 to check again. */
19736 if (ambiguous_p)
19738 cp_parser_simulate_error (parser);
19739 return error_mark_node;
19741 /* If the next token is a `::', then the name must be a type
19742 name.
19744 [basic.lookup.qual]
19746 During the lookup for a name preceding the :: scope
19747 resolution operator, object, function, and enumerator
19748 names are ignored. */
19749 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19750 tag_type = typename_type;
19751 /* Look up the name. */
19752 decl = cp_parser_lookup_name (parser, identifier,
19753 tag_type,
19754 /*is_template=*/false,
19755 /*is_namespace=*/false,
19756 check_dependency_p,
19757 &ambiguous_decls,
19758 identifier_token->location);
19759 if (ambiguous_decls)
19761 if (cp_parser_parsing_tentatively (parser))
19762 cp_parser_simulate_error (parser);
19763 return error_mark_node;
19767 else
19769 /* Try a template-id. */
19770 decl = cp_parser_template_id (parser, template_keyword_p,
19771 check_dependency_p,
19772 tag_type,
19773 is_declaration);
19774 if (decl == error_mark_node)
19775 return error_mark_node;
19778 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19780 /* If this is a typename, create a TYPENAME_TYPE. */
19781 if (typename_p && decl != error_mark_node)
19783 decl = make_typename_type (scope, decl, typename_type,
19784 /*complain=*/tf_error);
19785 if (decl != error_mark_node)
19786 decl = TYPE_NAME (decl);
19789 decl = strip_using_decl (decl);
19791 /* Check to see that it is really the name of a class. */
19792 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19793 && identifier_p (TREE_OPERAND (decl, 0))
19794 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19795 /* Situations like this:
19797 template <typename T> struct A {
19798 typename T::template X<int>::I i;
19801 are problematic. Is `T::template X<int>' a class-name? The
19802 standard does not seem to be definitive, but there is no other
19803 valid interpretation of the following `::'. Therefore, those
19804 names are considered class-names. */
19806 decl = make_typename_type (scope, decl, tag_type, tf_error);
19807 if (decl != error_mark_node)
19808 decl = TYPE_NAME (decl);
19810 else if (TREE_CODE (decl) != TYPE_DECL
19811 || TREE_TYPE (decl) == error_mark_node
19812 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19813 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
19814 /* In Objective-C 2.0, a classname followed by '.' starts a
19815 dot-syntax expression, and it's not a type-name. */
19816 || (c_dialect_objc ()
19817 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19818 && objc_is_class_name (decl)))
19819 decl = error_mark_node;
19821 if (decl == error_mark_node)
19822 cp_parser_error (parser, "expected class-name");
19823 else if (identifier && !parser->scope)
19824 maybe_note_name_used_in_class (identifier, decl);
19826 return decl;
19829 /* Parse a class-specifier.
19831 class-specifier:
19832 class-head { member-specification [opt] }
19834 Returns the TREE_TYPE representing the class. */
19836 static tree
19837 cp_parser_class_specifier_1 (cp_parser* parser)
19839 tree type;
19840 tree attributes = NULL_TREE;
19841 bool nested_name_specifier_p;
19842 unsigned saved_num_template_parameter_lists;
19843 bool saved_in_function_body;
19844 unsigned char in_statement;
19845 bool in_switch_statement_p;
19846 bool saved_in_unbraced_linkage_specification_p;
19847 tree old_scope = NULL_TREE;
19848 tree scope = NULL_TREE;
19849 cp_token *closing_brace;
19851 push_deferring_access_checks (dk_no_deferred);
19853 /* Parse the class-head. */
19854 type = cp_parser_class_head (parser,
19855 &nested_name_specifier_p);
19856 /* If the class-head was a semantic disaster, skip the entire body
19857 of the class. */
19858 if (!type)
19860 cp_parser_skip_to_end_of_block_or_statement (parser);
19861 pop_deferring_access_checks ();
19862 return error_mark_node;
19865 /* Look for the `{'. */
19866 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19868 pop_deferring_access_checks ();
19869 return error_mark_node;
19872 cp_ensure_no_omp_declare_simd (parser);
19874 /* Issue an error message if type-definitions are forbidden here. */
19875 cp_parser_check_type_definition (parser);
19876 /* Remember that we are defining one more class. */
19877 ++parser->num_classes_being_defined;
19878 /* Inside the class, surrounding template-parameter-lists do not
19879 apply. */
19880 saved_num_template_parameter_lists
19881 = parser->num_template_parameter_lists;
19882 parser->num_template_parameter_lists = 0;
19883 /* We are not in a function body. */
19884 saved_in_function_body = parser->in_function_body;
19885 parser->in_function_body = false;
19886 /* Or in a loop. */
19887 in_statement = parser->in_statement;
19888 parser->in_statement = 0;
19889 /* Or in a switch. */
19890 in_switch_statement_p = parser->in_switch_statement_p;
19891 parser->in_switch_statement_p = false;
19892 /* We are not immediately inside an extern "lang" block. */
19893 saved_in_unbraced_linkage_specification_p
19894 = parser->in_unbraced_linkage_specification_p;
19895 parser->in_unbraced_linkage_specification_p = false;
19897 /* Start the class. */
19898 if (nested_name_specifier_p)
19900 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19901 old_scope = push_inner_scope (scope);
19903 type = begin_class_definition (type);
19905 if (type == error_mark_node)
19906 /* If the type is erroneous, skip the entire body of the class. */
19907 cp_parser_skip_to_closing_brace (parser);
19908 else
19909 /* Parse the member-specification. */
19910 cp_parser_member_specification_opt (parser);
19912 /* Look for the trailing `}'. */
19913 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19914 /* Look for trailing attributes to apply to this class. */
19915 if (cp_parser_allow_gnu_extensions_p (parser))
19916 attributes = cp_parser_gnu_attributes_opt (parser);
19917 if (type != error_mark_node)
19918 type = finish_struct (type, attributes);
19919 if (nested_name_specifier_p)
19920 pop_inner_scope (old_scope, scope);
19922 /* We've finished a type definition. Check for the common syntax
19923 error of forgetting a semicolon after the definition. We need to
19924 be careful, as we can't just check for not-a-semicolon and be done
19925 with it; the user might have typed:
19927 class X { } c = ...;
19928 class X { } *p = ...;
19930 and so forth. Instead, enumerate all the possible tokens that
19931 might follow this production; if we don't see one of them, then
19932 complain and silently insert the semicolon. */
19934 cp_token *token = cp_lexer_peek_token (parser->lexer);
19935 bool want_semicolon = true;
19937 if (cp_next_tokens_can_be_std_attribute_p (parser))
19938 /* Don't try to parse c++11 attributes here. As per the
19939 grammar, that should be a task for
19940 cp_parser_decl_specifier_seq. */
19941 want_semicolon = false;
19943 switch (token->type)
19945 case CPP_NAME:
19946 case CPP_SEMICOLON:
19947 case CPP_MULT:
19948 case CPP_AND:
19949 case CPP_OPEN_PAREN:
19950 case CPP_CLOSE_PAREN:
19951 case CPP_COMMA:
19952 want_semicolon = false;
19953 break;
19955 /* While it's legal for type qualifiers and storage class
19956 specifiers to follow type definitions in the grammar, only
19957 compiler testsuites contain code like that. Assume that if
19958 we see such code, then what we're really seeing is a case
19959 like:
19961 class X { }
19962 const <type> var = ...;
19966 class Y { }
19967 static <type> func (...) ...
19969 i.e. the qualifier or specifier applies to the next
19970 declaration. To do so, however, we need to look ahead one
19971 more token to see if *that* token is a type specifier.
19973 This code could be improved to handle:
19975 class Z { }
19976 static const <type> var = ...; */
19977 case CPP_KEYWORD:
19978 if (keyword_is_decl_specifier (token->keyword))
19980 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19982 /* Handling user-defined types here would be nice, but very
19983 tricky. */
19984 want_semicolon
19985 = (lookahead->type == CPP_KEYWORD
19986 && keyword_begins_type_specifier (lookahead->keyword));
19988 break;
19989 default:
19990 break;
19993 /* If we don't have a type, then something is very wrong and we
19994 shouldn't try to do anything clever. Likewise for not seeing the
19995 closing brace. */
19996 if (closing_brace && TYPE_P (type) && want_semicolon)
19998 cp_token_position prev
19999 = cp_lexer_previous_token_position (parser->lexer);
20000 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
20001 location_t loc = prev_token->location;
20003 if (CLASSTYPE_DECLARED_CLASS (type))
20004 error_at (loc, "expected %<;%> after class definition");
20005 else if (TREE_CODE (type) == RECORD_TYPE)
20006 error_at (loc, "expected %<;%> after struct definition");
20007 else if (TREE_CODE (type) == UNION_TYPE)
20008 error_at (loc, "expected %<;%> after union definition");
20009 else
20010 gcc_unreachable ();
20012 /* Unget one token and smash it to look as though we encountered
20013 a semicolon in the input stream. */
20014 cp_lexer_set_token_position (parser->lexer, prev);
20015 token = cp_lexer_peek_token (parser->lexer);
20016 token->type = CPP_SEMICOLON;
20017 token->keyword = RID_MAX;
20021 /* If this class is not itself within the scope of another class,
20022 then we need to parse the bodies of all of the queued function
20023 definitions. Note that the queued functions defined in a class
20024 are not always processed immediately following the
20025 class-specifier for that class. Consider:
20027 struct A {
20028 struct B { void f() { sizeof (A); } };
20031 If `f' were processed before the processing of `A' were
20032 completed, there would be no way to compute the size of `A'.
20033 Note that the nesting we are interested in here is lexical --
20034 not the semantic nesting given by TYPE_CONTEXT. In particular,
20035 for:
20037 struct A { struct B; };
20038 struct A::B { void f() { } };
20040 there is no need to delay the parsing of `A::B::f'. */
20041 if (--parser->num_classes_being_defined == 0)
20043 tree decl;
20044 tree class_type = NULL_TREE;
20045 tree pushed_scope = NULL_TREE;
20046 unsigned ix;
20047 cp_default_arg_entry *e;
20048 tree save_ccp, save_ccr;
20050 /* In a first pass, parse default arguments to the functions.
20051 Then, in a second pass, parse the bodies of the functions.
20052 This two-phased approach handles cases like:
20054 struct S {
20055 void f() { g(); }
20056 void g(int i = 3);
20060 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
20062 decl = e->decl;
20063 /* If there are default arguments that have not yet been processed,
20064 take care of them now. */
20065 if (class_type != e->class_type)
20067 if (pushed_scope)
20068 pop_scope (pushed_scope);
20069 class_type = e->class_type;
20070 pushed_scope = push_scope (class_type);
20072 /* Make sure that any template parameters are in scope. */
20073 maybe_begin_member_template_processing (decl);
20074 /* Parse the default argument expressions. */
20075 cp_parser_late_parsing_default_args (parser, decl);
20076 /* Remove any template parameters from the symbol table. */
20077 maybe_end_member_template_processing ();
20079 vec_safe_truncate (unparsed_funs_with_default_args, 0);
20080 /* Now parse any NSDMIs. */
20081 save_ccp = current_class_ptr;
20082 save_ccr = current_class_ref;
20083 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20085 if (class_type != DECL_CONTEXT (decl))
20087 if (pushed_scope)
20088 pop_scope (pushed_scope);
20089 class_type = DECL_CONTEXT (decl);
20090 pushed_scope = push_scope (class_type);
20092 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20093 cp_parser_late_parsing_nsdmi (parser, decl);
20095 vec_safe_truncate (unparsed_nsdmis, 0);
20096 current_class_ptr = save_ccp;
20097 current_class_ref = save_ccr;
20098 if (pushed_scope)
20099 pop_scope (pushed_scope);
20101 /* Now do some post-NSDMI bookkeeping. */
20102 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20103 after_nsdmi_defaulted_late_checks (class_type);
20104 vec_safe_truncate (unparsed_classes, 0);
20105 after_nsdmi_defaulted_late_checks (type);
20107 /* Now parse the body of the functions. */
20108 if (flag_openmp)
20110 /* OpenMP UDRs need to be parsed before all other functions. */
20111 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20112 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20113 cp_parser_late_parsing_for_member (parser, decl);
20114 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20115 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20116 cp_parser_late_parsing_for_member (parser, decl);
20118 else
20119 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20120 cp_parser_late_parsing_for_member (parser, decl);
20121 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20123 else
20124 vec_safe_push (unparsed_classes, type);
20126 /* Put back any saved access checks. */
20127 pop_deferring_access_checks ();
20129 /* Restore saved state. */
20130 parser->in_switch_statement_p = in_switch_statement_p;
20131 parser->in_statement = in_statement;
20132 parser->in_function_body = saved_in_function_body;
20133 parser->num_template_parameter_lists
20134 = saved_num_template_parameter_lists;
20135 parser->in_unbraced_linkage_specification_p
20136 = saved_in_unbraced_linkage_specification_p;
20138 return type;
20141 static tree
20142 cp_parser_class_specifier (cp_parser* parser)
20144 tree ret;
20145 timevar_push (TV_PARSE_STRUCT);
20146 ret = cp_parser_class_specifier_1 (parser);
20147 timevar_pop (TV_PARSE_STRUCT);
20148 return ret;
20151 /* Parse a class-head.
20153 class-head:
20154 class-key identifier [opt] base-clause [opt]
20155 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20156 class-key nested-name-specifier [opt] template-id
20157 base-clause [opt]
20159 class-virt-specifier:
20160 final
20162 GNU Extensions:
20163 class-key attributes identifier [opt] base-clause [opt]
20164 class-key attributes nested-name-specifier identifier base-clause [opt]
20165 class-key attributes nested-name-specifier [opt] template-id
20166 base-clause [opt]
20168 Upon return BASES is initialized to the list of base classes (or
20169 NULL, if there are none) in the same form returned by
20170 cp_parser_base_clause.
20172 Returns the TYPE of the indicated class. Sets
20173 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20174 involving a nested-name-specifier was used, and FALSE otherwise.
20176 Returns error_mark_node if this is not a class-head.
20178 Returns NULL_TREE if the class-head is syntactically valid, but
20179 semantically invalid in a way that means we should skip the entire
20180 body of the class. */
20182 static tree
20183 cp_parser_class_head (cp_parser* parser,
20184 bool* nested_name_specifier_p)
20186 tree nested_name_specifier;
20187 enum tag_types class_key;
20188 tree id = NULL_TREE;
20189 tree type = NULL_TREE;
20190 tree attributes;
20191 tree bases;
20192 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20193 bool template_id_p = false;
20194 bool qualified_p = false;
20195 bool invalid_nested_name_p = false;
20196 bool invalid_explicit_specialization_p = false;
20197 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20198 tree pushed_scope = NULL_TREE;
20199 unsigned num_templates;
20200 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20201 /* Assume no nested-name-specifier will be present. */
20202 *nested_name_specifier_p = false;
20203 /* Assume no template parameter lists will be used in defining the
20204 type. */
20205 num_templates = 0;
20206 parser->colon_corrects_to_scope_p = false;
20208 /* Look for the class-key. */
20209 class_key = cp_parser_class_key (parser);
20210 if (class_key == none_type)
20211 return error_mark_node;
20213 /* Parse the attributes. */
20214 attributes = cp_parser_attributes_opt (parser);
20216 /* If the next token is `::', that is invalid -- but sometimes
20217 people do try to write:
20219 struct ::S {};
20221 Handle this gracefully by accepting the extra qualifier, and then
20222 issuing an error about it later if this really is a
20223 class-head. If it turns out just to be an elaborated type
20224 specifier, remain silent. */
20225 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20226 qualified_p = true;
20228 push_deferring_access_checks (dk_no_check);
20230 /* Determine the name of the class. Begin by looking for an
20231 optional nested-name-specifier. */
20232 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20233 nested_name_specifier
20234 = cp_parser_nested_name_specifier_opt (parser,
20235 /*typename_keyword_p=*/false,
20236 /*check_dependency_p=*/false,
20237 /*type_p=*/true,
20238 /*is_declaration=*/false);
20239 /* If there was a nested-name-specifier, then there *must* be an
20240 identifier. */
20241 if (nested_name_specifier)
20243 type_start_token = cp_lexer_peek_token (parser->lexer);
20244 /* Although the grammar says `identifier', it really means
20245 `class-name' or `template-name'. You are only allowed to
20246 define a class that has already been declared with this
20247 syntax.
20249 The proposed resolution for Core Issue 180 says that wherever
20250 you see `class T::X' you should treat `X' as a type-name.
20252 It is OK to define an inaccessible class; for example:
20254 class A { class B; };
20255 class A::B {};
20257 We do not know if we will see a class-name, or a
20258 template-name. We look for a class-name first, in case the
20259 class-name is a template-id; if we looked for the
20260 template-name first we would stop after the template-name. */
20261 cp_parser_parse_tentatively (parser);
20262 type = cp_parser_class_name (parser,
20263 /*typename_keyword_p=*/false,
20264 /*template_keyword_p=*/false,
20265 class_type,
20266 /*check_dependency_p=*/false,
20267 /*class_head_p=*/true,
20268 /*is_declaration=*/false);
20269 /* If that didn't work, ignore the nested-name-specifier. */
20270 if (!cp_parser_parse_definitely (parser))
20272 invalid_nested_name_p = true;
20273 type_start_token = cp_lexer_peek_token (parser->lexer);
20274 id = cp_parser_identifier (parser);
20275 if (id == error_mark_node)
20276 id = NULL_TREE;
20278 /* If we could not find a corresponding TYPE, treat this
20279 declaration like an unqualified declaration. */
20280 if (type == error_mark_node)
20281 nested_name_specifier = NULL_TREE;
20282 /* Otherwise, count the number of templates used in TYPE and its
20283 containing scopes. */
20284 else
20286 tree scope;
20288 for (scope = TREE_TYPE (type);
20289 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20290 scope = get_containing_scope (scope))
20291 if (TYPE_P (scope)
20292 && CLASS_TYPE_P (scope)
20293 && CLASSTYPE_TEMPLATE_INFO (scope)
20294 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20295 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20296 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20297 ++num_templates;
20300 /* Otherwise, the identifier is optional. */
20301 else
20303 /* We don't know whether what comes next is a template-id,
20304 an identifier, or nothing at all. */
20305 cp_parser_parse_tentatively (parser);
20306 /* Check for a template-id. */
20307 type_start_token = cp_lexer_peek_token (parser->lexer);
20308 id = cp_parser_template_id (parser,
20309 /*template_keyword_p=*/false,
20310 /*check_dependency_p=*/true,
20311 class_key,
20312 /*is_declaration=*/true);
20313 /* If that didn't work, it could still be an identifier. */
20314 if (!cp_parser_parse_definitely (parser))
20316 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20318 type_start_token = cp_lexer_peek_token (parser->lexer);
20319 id = cp_parser_identifier (parser);
20321 else
20322 id = NULL_TREE;
20324 else
20326 template_id_p = true;
20327 ++num_templates;
20331 pop_deferring_access_checks ();
20333 if (id)
20335 cp_parser_check_for_invalid_template_id (parser, id,
20336 class_key,
20337 type_start_token->location);
20339 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20341 /* If it's not a `:' or a `{' then we can't really be looking at a
20342 class-head, since a class-head only appears as part of a
20343 class-specifier. We have to detect this situation before calling
20344 xref_tag, since that has irreversible side-effects. */
20345 if (!cp_parser_next_token_starts_class_definition_p (parser))
20347 cp_parser_error (parser, "expected %<{%> or %<:%>");
20348 type = error_mark_node;
20349 goto out;
20352 /* At this point, we're going ahead with the class-specifier, even
20353 if some other problem occurs. */
20354 cp_parser_commit_to_tentative_parse (parser);
20355 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20357 cp_parser_error (parser,
20358 "cannot specify %<override%> for a class");
20359 type = error_mark_node;
20360 goto out;
20362 /* Issue the error about the overly-qualified name now. */
20363 if (qualified_p)
20365 cp_parser_error (parser,
20366 "global qualification of class name is invalid");
20367 type = error_mark_node;
20368 goto out;
20370 else if (invalid_nested_name_p)
20372 cp_parser_error (parser,
20373 "qualified name does not name a class");
20374 type = error_mark_node;
20375 goto out;
20377 else if (nested_name_specifier)
20379 tree scope;
20381 /* Reject typedef-names in class heads. */
20382 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20384 error_at (type_start_token->location,
20385 "invalid class name in declaration of %qD",
20386 type);
20387 type = NULL_TREE;
20388 goto done;
20391 /* Figure out in what scope the declaration is being placed. */
20392 scope = current_scope ();
20393 /* If that scope does not contain the scope in which the
20394 class was originally declared, the program is invalid. */
20395 if (scope && !is_ancestor (scope, nested_name_specifier))
20397 if (at_namespace_scope_p ())
20398 error_at (type_start_token->location,
20399 "declaration of %qD in namespace %qD which does not "
20400 "enclose %qD",
20401 type, scope, nested_name_specifier);
20402 else
20403 error_at (type_start_token->location,
20404 "declaration of %qD in %qD which does not enclose %qD",
20405 type, scope, nested_name_specifier);
20406 type = NULL_TREE;
20407 goto done;
20409 /* [dcl.meaning]
20411 A declarator-id shall not be qualified except for the
20412 definition of a ... nested class outside of its class
20413 ... [or] the definition or explicit instantiation of a
20414 class member of a namespace outside of its namespace. */
20415 if (scope == nested_name_specifier)
20417 permerror (nested_name_specifier_token_start->location,
20418 "extra qualification not allowed");
20419 nested_name_specifier = NULL_TREE;
20420 num_templates = 0;
20423 /* An explicit-specialization must be preceded by "template <>". If
20424 it is not, try to recover gracefully. */
20425 if (at_namespace_scope_p ()
20426 && parser->num_template_parameter_lists == 0
20427 && template_id_p)
20429 error_at (type_start_token->location,
20430 "an explicit specialization must be preceded by %<template <>%>");
20431 invalid_explicit_specialization_p = true;
20432 /* Take the same action that would have been taken by
20433 cp_parser_explicit_specialization. */
20434 ++parser->num_template_parameter_lists;
20435 begin_specialization ();
20437 /* There must be no "return" statements between this point and the
20438 end of this function; set "type "to the correct return value and
20439 use "goto done;" to return. */
20440 /* Make sure that the right number of template parameters were
20441 present. */
20442 if (!cp_parser_check_template_parameters (parser, num_templates,
20443 type_start_token->location,
20444 /*declarator=*/NULL))
20446 /* If something went wrong, there is no point in even trying to
20447 process the class-definition. */
20448 type = NULL_TREE;
20449 goto done;
20452 /* Look up the type. */
20453 if (template_id_p)
20455 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20456 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20457 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20459 error_at (type_start_token->location,
20460 "function template %qD redeclared as a class template", id);
20461 type = error_mark_node;
20463 else
20465 type = TREE_TYPE (id);
20466 type = maybe_process_partial_specialization (type);
20468 if (nested_name_specifier)
20469 pushed_scope = push_scope (nested_name_specifier);
20471 else if (nested_name_specifier)
20473 tree class_type;
20475 /* Given:
20477 template <typename T> struct S { struct T };
20478 template <typename T> struct S<T>::T { };
20480 we will get a TYPENAME_TYPE when processing the definition of
20481 `S::T'. We need to resolve it to the actual type before we
20482 try to define it. */
20483 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20485 class_type = resolve_typename_type (TREE_TYPE (type),
20486 /*only_current_p=*/false);
20487 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20488 type = TYPE_NAME (class_type);
20489 else
20491 cp_parser_error (parser, "could not resolve typename type");
20492 type = error_mark_node;
20496 if (maybe_process_partial_specialization (TREE_TYPE (type))
20497 == error_mark_node)
20499 type = NULL_TREE;
20500 goto done;
20503 class_type = current_class_type;
20504 /* Enter the scope indicated by the nested-name-specifier. */
20505 pushed_scope = push_scope (nested_name_specifier);
20506 /* Get the canonical version of this type. */
20507 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20508 /* Call push_template_decl if it seems like we should be defining a
20509 template either from the template headers or the type we're
20510 defining, so that we diagnose both extra and missing headers. */
20511 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20512 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20513 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20515 type = push_template_decl (type);
20516 if (type == error_mark_node)
20518 type = NULL_TREE;
20519 goto done;
20523 type = TREE_TYPE (type);
20524 *nested_name_specifier_p = true;
20526 else /* The name is not a nested name. */
20528 /* If the class was unnamed, create a dummy name. */
20529 if (!id)
20530 id = make_anon_name ();
20531 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20532 parser->num_template_parameter_lists);
20535 /* Indicate whether this class was declared as a `class' or as a
20536 `struct'. */
20537 if (TREE_CODE (type) == RECORD_TYPE)
20538 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20539 cp_parser_check_class_key (class_key, type);
20541 /* If this type was already complete, and we see another definition,
20542 that's an error. */
20543 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20545 error_at (type_start_token->location, "redefinition of %q#T",
20546 type);
20547 error_at (type_start_token->location, "previous definition of %q+#T",
20548 type);
20549 type = NULL_TREE;
20550 goto done;
20552 else if (type == error_mark_node)
20553 type = NULL_TREE;
20555 if (type)
20557 /* Apply attributes now, before any use of the class as a template
20558 argument in its base list. */
20559 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20560 fixup_attribute_variants (type);
20563 /* We will have entered the scope containing the class; the names of
20564 base classes should be looked up in that context. For example:
20566 struct A { struct B {}; struct C; };
20567 struct A::C : B {};
20569 is valid. */
20571 /* Get the list of base-classes, if there is one. */
20572 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20574 /* PR59482: enter the class scope so that base-specifiers are looked
20575 up correctly. */
20576 if (type)
20577 pushclass (type);
20578 bases = cp_parser_base_clause (parser);
20579 /* PR59482: get out of the previously pushed class scope so that the
20580 subsequent pops pop the right thing. */
20581 if (type)
20582 popclass ();
20584 else
20585 bases = NULL_TREE;
20587 /* If we're really defining a class, process the base classes.
20588 If they're invalid, fail. */
20589 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20590 && !xref_basetypes (type, bases))
20591 type = NULL_TREE;
20593 done:
20594 /* Leave the scope given by the nested-name-specifier. We will
20595 enter the class scope itself while processing the members. */
20596 if (pushed_scope)
20597 pop_scope (pushed_scope);
20599 if (invalid_explicit_specialization_p)
20601 end_specialization ();
20602 --parser->num_template_parameter_lists;
20605 if (type)
20606 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20607 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20608 CLASSTYPE_FINAL (type) = 1;
20609 out:
20610 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20611 return type;
20614 /* Parse a class-key.
20616 class-key:
20617 class
20618 struct
20619 union
20621 Returns the kind of class-key specified, or none_type to indicate
20622 error. */
20624 static enum tag_types
20625 cp_parser_class_key (cp_parser* parser)
20627 cp_token *token;
20628 enum tag_types tag_type;
20630 /* Look for the class-key. */
20631 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20632 if (!token)
20633 return none_type;
20635 /* Check to see if the TOKEN is a class-key. */
20636 tag_type = cp_parser_token_is_class_key (token);
20637 if (!tag_type)
20638 cp_parser_error (parser, "expected class-key");
20639 return tag_type;
20642 /* Parse a type-parameter-key.
20644 type-parameter-key:
20645 class
20646 typename
20649 static void
20650 cp_parser_type_parameter_key (cp_parser* parser)
20652 /* Look for the type-parameter-key. */
20653 enum tag_types tag_type = none_type;
20654 cp_token *token = cp_lexer_peek_token (parser->lexer);
20655 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20657 cp_lexer_consume_token (parser->lexer);
20658 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20659 /* typename is not allowed in a template template parameter
20660 by the standard until C++1Z. */
20661 pedwarn (token->location, OPT_Wpedantic,
20662 "ISO C++ forbids typename key in template template parameter;"
20663 " use -std=c++1z or -std=gnu++1z");
20665 else
20666 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20668 return;
20671 /* Parse an (optional) member-specification.
20673 member-specification:
20674 member-declaration member-specification [opt]
20675 access-specifier : member-specification [opt] */
20677 static void
20678 cp_parser_member_specification_opt (cp_parser* parser)
20680 while (true)
20682 cp_token *token;
20683 enum rid keyword;
20685 /* Peek at the next token. */
20686 token = cp_lexer_peek_token (parser->lexer);
20687 /* If it's a `}', or EOF then we've seen all the members. */
20688 if (token->type == CPP_CLOSE_BRACE
20689 || token->type == CPP_EOF
20690 || token->type == CPP_PRAGMA_EOL)
20691 break;
20693 /* See if this token is a keyword. */
20694 keyword = token->keyword;
20695 switch (keyword)
20697 case RID_PUBLIC:
20698 case RID_PROTECTED:
20699 case RID_PRIVATE:
20700 /* Consume the access-specifier. */
20701 cp_lexer_consume_token (parser->lexer);
20702 /* Remember which access-specifier is active. */
20703 current_access_specifier = token->u.value;
20704 /* Look for the `:'. */
20705 cp_parser_require (parser, CPP_COLON, RT_COLON);
20706 break;
20708 default:
20709 /* Accept #pragmas at class scope. */
20710 if (token->type == CPP_PRAGMA)
20712 cp_parser_pragma (parser, pragma_member);
20713 break;
20716 /* Otherwise, the next construction must be a
20717 member-declaration. */
20718 cp_parser_member_declaration (parser);
20723 /* Parse a member-declaration.
20725 member-declaration:
20726 decl-specifier-seq [opt] member-declarator-list [opt] ;
20727 function-definition ; [opt]
20728 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20729 using-declaration
20730 template-declaration
20731 alias-declaration
20733 member-declarator-list:
20734 member-declarator
20735 member-declarator-list , member-declarator
20737 member-declarator:
20738 declarator pure-specifier [opt]
20739 declarator constant-initializer [opt]
20740 identifier [opt] : constant-expression
20742 GNU Extensions:
20744 member-declaration:
20745 __extension__ member-declaration
20747 member-declarator:
20748 declarator attributes [opt] pure-specifier [opt]
20749 declarator attributes [opt] constant-initializer [opt]
20750 identifier [opt] attributes [opt] : constant-expression
20752 C++0x Extensions:
20754 member-declaration:
20755 static_assert-declaration */
20757 static void
20758 cp_parser_member_declaration (cp_parser* parser)
20760 cp_decl_specifier_seq decl_specifiers;
20761 tree prefix_attributes;
20762 tree decl;
20763 int declares_class_or_enum;
20764 bool friend_p;
20765 cp_token *token = NULL;
20766 cp_token *decl_spec_token_start = NULL;
20767 cp_token *initializer_token_start = NULL;
20768 int saved_pedantic;
20769 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20771 /* Check for the `__extension__' keyword. */
20772 if (cp_parser_extension_opt (parser, &saved_pedantic))
20774 /* Recurse. */
20775 cp_parser_member_declaration (parser);
20776 /* Restore the old value of the PEDANTIC flag. */
20777 pedantic = saved_pedantic;
20779 return;
20782 /* Check for a template-declaration. */
20783 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20785 /* An explicit specialization here is an error condition, and we
20786 expect the specialization handler to detect and report this. */
20787 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20788 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20789 cp_parser_explicit_specialization (parser);
20790 else
20791 cp_parser_template_declaration (parser, /*member_p=*/true);
20793 return;
20796 /* Check for a using-declaration. */
20797 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20799 if (cxx_dialect < cxx11)
20801 /* Parse the using-declaration. */
20802 cp_parser_using_declaration (parser,
20803 /*access_declaration_p=*/false);
20804 return;
20806 else
20808 tree decl;
20809 bool alias_decl_expected;
20810 cp_parser_parse_tentatively (parser);
20811 decl = cp_parser_alias_declaration (parser);
20812 /* Note that if we actually see the '=' token after the
20813 identifier, cp_parser_alias_declaration commits the
20814 tentative parse. In that case, we really expects an
20815 alias-declaration. Otherwise, we expect a using
20816 declaration. */
20817 alias_decl_expected =
20818 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20819 cp_parser_parse_definitely (parser);
20821 if (alias_decl_expected)
20822 finish_member_declaration (decl);
20823 else
20824 cp_parser_using_declaration (parser,
20825 /*access_declaration_p=*/false);
20826 return;
20830 /* Check for @defs. */
20831 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20833 tree ivar, member;
20834 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20835 ivar = ivar_chains;
20836 while (ivar)
20838 member = ivar;
20839 ivar = TREE_CHAIN (member);
20840 TREE_CHAIN (member) = NULL_TREE;
20841 finish_member_declaration (member);
20843 return;
20846 /* If the next token is `static_assert' we have a static assertion. */
20847 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20849 cp_parser_static_assert (parser, /*member_p=*/true);
20850 return;
20853 parser->colon_corrects_to_scope_p = false;
20855 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20856 goto out;
20858 /* Parse the decl-specifier-seq. */
20859 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20860 cp_parser_decl_specifier_seq (parser,
20861 CP_PARSER_FLAGS_OPTIONAL,
20862 &decl_specifiers,
20863 &declares_class_or_enum);
20864 /* Check for an invalid type-name. */
20865 if (!decl_specifiers.any_type_specifiers_p
20866 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20867 goto out;
20868 /* If there is no declarator, then the decl-specifier-seq should
20869 specify a type. */
20870 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20872 /* If there was no decl-specifier-seq, and the next token is a
20873 `;', then we have something like:
20875 struct S { ; };
20877 [class.mem]
20879 Each member-declaration shall declare at least one member
20880 name of the class. */
20881 if (!decl_specifiers.any_specifiers_p)
20883 cp_token *token = cp_lexer_peek_token (parser->lexer);
20884 if (!in_system_header_at (token->location))
20885 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20887 else
20889 tree type;
20891 /* See if this declaration is a friend. */
20892 friend_p = cp_parser_friend_p (&decl_specifiers);
20893 /* If there were decl-specifiers, check to see if there was
20894 a class-declaration. */
20895 type = check_tag_decl (&decl_specifiers,
20896 /*explicit_type_instantiation_p=*/false);
20897 /* Nested classes have already been added to the class, but
20898 a `friend' needs to be explicitly registered. */
20899 if (friend_p)
20901 /* If the `friend' keyword was present, the friend must
20902 be introduced with a class-key. */
20903 if (!declares_class_or_enum && cxx_dialect < cxx11)
20904 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20905 "in C++03 a class-key must be used "
20906 "when declaring a friend");
20907 /* In this case:
20909 template <typename T> struct A {
20910 friend struct A<T>::B;
20913 A<T>::B will be represented by a TYPENAME_TYPE, and
20914 therefore not recognized by check_tag_decl. */
20915 if (!type)
20917 type = decl_specifiers.type;
20918 if (type && TREE_CODE (type) == TYPE_DECL)
20919 type = TREE_TYPE (type);
20921 if (!type || !TYPE_P (type))
20922 error_at (decl_spec_token_start->location,
20923 "friend declaration does not name a class or "
20924 "function");
20925 else
20926 make_friend_class (current_class_type, type,
20927 /*complain=*/true);
20929 /* If there is no TYPE, an error message will already have
20930 been issued. */
20931 else if (!type || type == error_mark_node)
20933 /* An anonymous aggregate has to be handled specially; such
20934 a declaration really declares a data member (with a
20935 particular type), as opposed to a nested class. */
20936 else if (ANON_AGGR_TYPE_P (type))
20938 /* C++11 9.5/6. */
20939 if (decl_specifiers.storage_class != sc_none)
20940 error_at (decl_spec_token_start->location,
20941 "a storage class on an anonymous aggregate "
20942 "in class scope is not allowed");
20944 /* Remove constructors and such from TYPE, now that we
20945 know it is an anonymous aggregate. */
20946 fixup_anonymous_aggr (type);
20947 /* And make the corresponding data member. */
20948 decl = build_decl (decl_spec_token_start->location,
20949 FIELD_DECL, NULL_TREE, type);
20950 /* Add it to the class. */
20951 finish_member_declaration (decl);
20953 else
20954 cp_parser_check_access_in_redeclaration
20955 (TYPE_NAME (type),
20956 decl_spec_token_start->location);
20959 else
20961 bool assume_semicolon = false;
20963 /* Clear attributes from the decl_specifiers but keep them
20964 around as prefix attributes that apply them to the entity
20965 being declared. */
20966 prefix_attributes = decl_specifiers.attributes;
20967 decl_specifiers.attributes = NULL_TREE;
20969 /* See if these declarations will be friends. */
20970 friend_p = cp_parser_friend_p (&decl_specifiers);
20972 /* Keep going until we hit the `;' at the end of the
20973 declaration. */
20974 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20976 tree attributes = NULL_TREE;
20977 tree first_attribute;
20979 /* Peek at the next token. */
20980 token = cp_lexer_peek_token (parser->lexer);
20982 /* Check for a bitfield declaration. */
20983 if (token->type == CPP_COLON
20984 || (token->type == CPP_NAME
20985 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20986 == CPP_COLON))
20988 tree identifier;
20989 tree width;
20991 /* Get the name of the bitfield. Note that we cannot just
20992 check TOKEN here because it may have been invalidated by
20993 the call to cp_lexer_peek_nth_token above. */
20994 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20995 identifier = cp_parser_identifier (parser);
20996 else
20997 identifier = NULL_TREE;
20999 /* Consume the `:' token. */
21000 cp_lexer_consume_token (parser->lexer);
21001 /* Get the width of the bitfield. */
21002 width
21003 = cp_parser_constant_expression (parser);
21005 /* Look for attributes that apply to the bitfield. */
21006 attributes = cp_parser_attributes_opt (parser);
21007 /* Remember which attributes are prefix attributes and
21008 which are not. */
21009 first_attribute = attributes;
21010 /* Combine the attributes. */
21011 attributes = chainon (prefix_attributes, attributes);
21013 /* Create the bitfield declaration. */
21014 decl = grokbitfield (identifier
21015 ? make_id_declarator (NULL_TREE,
21016 identifier,
21017 sfk_none)
21018 : NULL,
21019 &decl_specifiers,
21020 width,
21021 attributes);
21023 else
21025 cp_declarator *declarator;
21026 tree initializer;
21027 tree asm_specification;
21028 int ctor_dtor_or_conv_p;
21030 /* Parse the declarator. */
21031 declarator
21032 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21033 &ctor_dtor_or_conv_p,
21034 /*parenthesized_p=*/NULL,
21035 /*member_p=*/true,
21036 friend_p);
21038 /* If something went wrong parsing the declarator, make sure
21039 that we at least consume some tokens. */
21040 if (declarator == cp_error_declarator)
21042 /* Skip to the end of the statement. */
21043 cp_parser_skip_to_end_of_statement (parser);
21044 /* If the next token is not a semicolon, that is
21045 probably because we just skipped over the body of
21046 a function. So, we consume a semicolon if
21047 present, but do not issue an error message if it
21048 is not present. */
21049 if (cp_lexer_next_token_is (parser->lexer,
21050 CPP_SEMICOLON))
21051 cp_lexer_consume_token (parser->lexer);
21052 goto out;
21055 if (declares_class_or_enum & 2)
21056 cp_parser_check_for_definition_in_return_type
21057 (declarator, decl_specifiers.type,
21058 decl_specifiers.locations[ds_type_spec]);
21060 /* Look for an asm-specification. */
21061 asm_specification = cp_parser_asm_specification_opt (parser);
21062 /* Look for attributes that apply to the declaration. */
21063 attributes = cp_parser_attributes_opt (parser);
21064 /* Remember which attributes are prefix attributes and
21065 which are not. */
21066 first_attribute = attributes;
21067 /* Combine the attributes. */
21068 attributes = chainon (prefix_attributes, attributes);
21070 /* If it's an `=', then we have a constant-initializer or a
21071 pure-specifier. It is not correct to parse the
21072 initializer before registering the member declaration
21073 since the member declaration should be in scope while
21074 its initializer is processed. However, the rest of the
21075 front end does not yet provide an interface that allows
21076 us to handle this correctly. */
21077 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21079 /* In [class.mem]:
21081 A pure-specifier shall be used only in the declaration of
21082 a virtual function.
21084 A member-declarator can contain a constant-initializer
21085 only if it declares a static member of integral or
21086 enumeration type.
21088 Therefore, if the DECLARATOR is for a function, we look
21089 for a pure-specifier; otherwise, we look for a
21090 constant-initializer. When we call `grokfield', it will
21091 perform more stringent semantics checks. */
21092 initializer_token_start = cp_lexer_peek_token (parser->lexer);
21093 if (function_declarator_p (declarator)
21094 || (decl_specifiers.type
21095 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21096 && declarator->kind == cdk_id
21097 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21098 == FUNCTION_TYPE)))
21099 initializer = cp_parser_pure_specifier (parser);
21100 else if (decl_specifiers.storage_class != sc_static)
21101 initializer = cp_parser_save_nsdmi (parser);
21102 else if (cxx_dialect >= cxx11)
21104 bool nonconst;
21105 /* Don't require a constant rvalue in C++11, since we
21106 might want a reference constant. We'll enforce
21107 constancy later. */
21108 cp_lexer_consume_token (parser->lexer);
21109 /* Parse the initializer. */
21110 initializer = cp_parser_initializer_clause (parser,
21111 &nonconst);
21113 else
21114 /* Parse the initializer. */
21115 initializer = cp_parser_constant_initializer (parser);
21117 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21118 && !function_declarator_p (declarator))
21120 bool x;
21121 if (decl_specifiers.storage_class != sc_static)
21122 initializer = cp_parser_save_nsdmi (parser);
21123 else
21124 initializer = cp_parser_initializer (parser, &x, &x);
21126 /* Otherwise, there is no initializer. */
21127 else
21128 initializer = NULL_TREE;
21130 /* See if we are probably looking at a function
21131 definition. We are certainly not looking at a
21132 member-declarator. Calling `grokfield' has
21133 side-effects, so we must not do it unless we are sure
21134 that we are looking at a member-declarator. */
21135 if (cp_parser_token_starts_function_definition_p
21136 (cp_lexer_peek_token (parser->lexer)))
21138 /* The grammar does not allow a pure-specifier to be
21139 used when a member function is defined. (It is
21140 possible that this fact is an oversight in the
21141 standard, since a pure function may be defined
21142 outside of the class-specifier. */
21143 if (initializer && initializer_token_start)
21144 error_at (initializer_token_start->location,
21145 "pure-specifier on function-definition");
21146 decl = cp_parser_save_member_function_body (parser,
21147 &decl_specifiers,
21148 declarator,
21149 attributes);
21150 if (parser->fully_implicit_function_template_p)
21151 decl = finish_fully_implicit_template (parser, decl);
21152 /* If the member was not a friend, declare it here. */
21153 if (!friend_p)
21154 finish_member_declaration (decl);
21155 /* Peek at the next token. */
21156 token = cp_lexer_peek_token (parser->lexer);
21157 /* If the next token is a semicolon, consume it. */
21158 if (token->type == CPP_SEMICOLON)
21159 cp_lexer_consume_token (parser->lexer);
21160 goto out;
21162 else
21163 if (declarator->kind == cdk_function)
21164 declarator->id_loc = token->location;
21165 /* Create the declaration. */
21166 decl = grokfield (declarator, &decl_specifiers,
21167 initializer, /*init_const_expr_p=*/true,
21168 asm_specification, attributes);
21169 if (parser->fully_implicit_function_template_p)
21171 if (friend_p)
21172 finish_fully_implicit_template (parser, 0);
21173 else
21174 decl = finish_fully_implicit_template (parser, decl);
21178 cp_finalize_omp_declare_simd (parser, decl);
21180 /* Reset PREFIX_ATTRIBUTES. */
21181 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21182 attributes = TREE_CHAIN (attributes);
21183 if (attributes)
21184 TREE_CHAIN (attributes) = NULL_TREE;
21186 /* If there is any qualification still in effect, clear it
21187 now; we will be starting fresh with the next declarator. */
21188 parser->scope = NULL_TREE;
21189 parser->qualifying_scope = NULL_TREE;
21190 parser->object_scope = NULL_TREE;
21191 /* If it's a `,', then there are more declarators. */
21192 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21194 cp_lexer_consume_token (parser->lexer);
21195 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21197 cp_token *token = cp_lexer_previous_token (parser->lexer);
21198 error_at (token->location,
21199 "stray %<,%> at end of member declaration");
21202 /* If the next token isn't a `;', then we have a parse error. */
21203 else if (cp_lexer_next_token_is_not (parser->lexer,
21204 CPP_SEMICOLON))
21206 /* The next token might be a ways away from where the
21207 actual semicolon is missing. Find the previous token
21208 and use that for our error position. */
21209 cp_token *token = cp_lexer_previous_token (parser->lexer);
21210 error_at (token->location,
21211 "expected %<;%> at end of member declaration");
21213 /* Assume that the user meant to provide a semicolon. If
21214 we were to cp_parser_skip_to_end_of_statement, we might
21215 skip to a semicolon inside a member function definition
21216 and issue nonsensical error messages. */
21217 assume_semicolon = true;
21220 if (decl)
21222 /* Add DECL to the list of members. */
21223 if (!friend_p
21224 /* Explicitly include, eg, NSDMIs, for better error
21225 recovery (c++/58650). */
21226 || !DECL_DECLARES_FUNCTION_P (decl))
21227 finish_member_declaration (decl);
21229 if (TREE_CODE (decl) == FUNCTION_DECL)
21230 cp_parser_save_default_args (parser, decl);
21231 else if (TREE_CODE (decl) == FIELD_DECL
21232 && !DECL_C_BIT_FIELD (decl)
21233 && DECL_INITIAL (decl))
21234 /* Add DECL to the queue of NSDMI to be parsed later. */
21235 vec_safe_push (unparsed_nsdmis, decl);
21238 if (assume_semicolon)
21239 goto out;
21243 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21244 out:
21245 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21248 /* Parse a pure-specifier.
21250 pure-specifier:
21253 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21254 Otherwise, ERROR_MARK_NODE is returned. */
21256 static tree
21257 cp_parser_pure_specifier (cp_parser* parser)
21259 cp_token *token;
21261 /* Look for the `=' token. */
21262 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21263 return error_mark_node;
21264 /* Look for the `0' token. */
21265 token = cp_lexer_peek_token (parser->lexer);
21267 if (token->type == CPP_EOF
21268 || token->type == CPP_PRAGMA_EOL)
21269 return error_mark_node;
21271 cp_lexer_consume_token (parser->lexer);
21273 /* Accept = default or = delete in c++0x mode. */
21274 if (token->keyword == RID_DEFAULT
21275 || token->keyword == RID_DELETE)
21277 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21278 return token->u.value;
21281 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21282 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21284 cp_parser_error (parser,
21285 "invalid pure specifier (only %<= 0%> is allowed)");
21286 cp_parser_skip_to_end_of_statement (parser);
21287 return error_mark_node;
21289 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21291 error_at (token->location, "templates may not be %<virtual%>");
21292 return error_mark_node;
21295 return integer_zero_node;
21298 /* Parse a constant-initializer.
21300 constant-initializer:
21301 = constant-expression
21303 Returns a representation of the constant-expression. */
21305 static tree
21306 cp_parser_constant_initializer (cp_parser* parser)
21308 /* Look for the `=' token. */
21309 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21310 return error_mark_node;
21312 /* It is invalid to write:
21314 struct S { static const int i = { 7 }; };
21317 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21319 cp_parser_error (parser,
21320 "a brace-enclosed initializer is not allowed here");
21321 /* Consume the opening brace. */
21322 cp_lexer_consume_token (parser->lexer);
21323 /* Skip the initializer. */
21324 cp_parser_skip_to_closing_brace (parser);
21325 /* Look for the trailing `}'. */
21326 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21328 return error_mark_node;
21331 return cp_parser_constant_expression (parser);
21334 /* Derived classes [gram.class.derived] */
21336 /* Parse a base-clause.
21338 base-clause:
21339 : base-specifier-list
21341 base-specifier-list:
21342 base-specifier ... [opt]
21343 base-specifier-list , base-specifier ... [opt]
21345 Returns a TREE_LIST representing the base-classes, in the order in
21346 which they were declared. The representation of each node is as
21347 described by cp_parser_base_specifier.
21349 In the case that no bases are specified, this function will return
21350 NULL_TREE, not ERROR_MARK_NODE. */
21352 static tree
21353 cp_parser_base_clause (cp_parser* parser)
21355 tree bases = NULL_TREE;
21357 /* Look for the `:' that begins the list. */
21358 cp_parser_require (parser, CPP_COLON, RT_COLON);
21360 /* Scan the base-specifier-list. */
21361 while (true)
21363 cp_token *token;
21364 tree base;
21365 bool pack_expansion_p = false;
21367 /* Look for the base-specifier. */
21368 base = cp_parser_base_specifier (parser);
21369 /* Look for the (optional) ellipsis. */
21370 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21372 /* Consume the `...'. */
21373 cp_lexer_consume_token (parser->lexer);
21375 pack_expansion_p = true;
21378 /* Add BASE to the front of the list. */
21379 if (base && base != error_mark_node)
21381 if (pack_expansion_p)
21382 /* Make this a pack expansion type. */
21383 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21385 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21387 TREE_CHAIN (base) = bases;
21388 bases = base;
21391 /* Peek at the next token. */
21392 token = cp_lexer_peek_token (parser->lexer);
21393 /* If it's not a comma, then the list is complete. */
21394 if (token->type != CPP_COMMA)
21395 break;
21396 /* Consume the `,'. */
21397 cp_lexer_consume_token (parser->lexer);
21400 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21401 base class had a qualified name. However, the next name that
21402 appears is certainly not qualified. */
21403 parser->scope = NULL_TREE;
21404 parser->qualifying_scope = NULL_TREE;
21405 parser->object_scope = NULL_TREE;
21407 return nreverse (bases);
21410 /* Parse a base-specifier.
21412 base-specifier:
21413 :: [opt] nested-name-specifier [opt] class-name
21414 virtual access-specifier [opt] :: [opt] nested-name-specifier
21415 [opt] class-name
21416 access-specifier virtual [opt] :: [opt] nested-name-specifier
21417 [opt] class-name
21419 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21420 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21421 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21422 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21424 static tree
21425 cp_parser_base_specifier (cp_parser* parser)
21427 cp_token *token;
21428 bool done = false;
21429 bool virtual_p = false;
21430 bool duplicate_virtual_error_issued_p = false;
21431 bool duplicate_access_error_issued_p = false;
21432 bool class_scope_p, template_p;
21433 tree access = access_default_node;
21434 tree type;
21436 /* Process the optional `virtual' and `access-specifier'. */
21437 while (!done)
21439 /* Peek at the next token. */
21440 token = cp_lexer_peek_token (parser->lexer);
21441 /* Process `virtual'. */
21442 switch (token->keyword)
21444 case RID_VIRTUAL:
21445 /* If `virtual' appears more than once, issue an error. */
21446 if (virtual_p && !duplicate_virtual_error_issued_p)
21448 cp_parser_error (parser,
21449 "%<virtual%> specified more than once in base-specified");
21450 duplicate_virtual_error_issued_p = true;
21453 virtual_p = true;
21455 /* Consume the `virtual' token. */
21456 cp_lexer_consume_token (parser->lexer);
21458 break;
21460 case RID_PUBLIC:
21461 case RID_PROTECTED:
21462 case RID_PRIVATE:
21463 /* If more than one access specifier appears, issue an
21464 error. */
21465 if (access != access_default_node
21466 && !duplicate_access_error_issued_p)
21468 cp_parser_error (parser,
21469 "more than one access specifier in base-specified");
21470 duplicate_access_error_issued_p = true;
21473 access = ridpointers[(int) token->keyword];
21475 /* Consume the access-specifier. */
21476 cp_lexer_consume_token (parser->lexer);
21478 break;
21480 default:
21481 done = true;
21482 break;
21485 /* It is not uncommon to see programs mechanically, erroneously, use
21486 the 'typename' keyword to denote (dependent) qualified types
21487 as base classes. */
21488 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21490 token = cp_lexer_peek_token (parser->lexer);
21491 if (!processing_template_decl)
21492 error_at (token->location,
21493 "keyword %<typename%> not allowed outside of templates");
21494 else
21495 error_at (token->location,
21496 "keyword %<typename%> not allowed in this context "
21497 "(the base class is implicitly a type)");
21498 cp_lexer_consume_token (parser->lexer);
21501 /* Look for the optional `::' operator. */
21502 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21503 /* Look for the nested-name-specifier. The simplest way to
21504 implement:
21506 [temp.res]
21508 The keyword `typename' is not permitted in a base-specifier or
21509 mem-initializer; in these contexts a qualified name that
21510 depends on a template-parameter is implicitly assumed to be a
21511 type name.
21513 is to pretend that we have seen the `typename' keyword at this
21514 point. */
21515 cp_parser_nested_name_specifier_opt (parser,
21516 /*typename_keyword_p=*/true,
21517 /*check_dependency_p=*/true,
21518 typename_type,
21519 /*is_declaration=*/true);
21520 /* If the base class is given by a qualified name, assume that names
21521 we see are type names or templates, as appropriate. */
21522 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21523 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21525 if (!parser->scope
21526 && cp_lexer_next_token_is_decltype (parser->lexer))
21527 /* DR 950 allows decltype as a base-specifier. */
21528 type = cp_parser_decltype (parser);
21529 else
21531 /* Otherwise, look for the class-name. */
21532 type = cp_parser_class_name (parser,
21533 class_scope_p,
21534 template_p,
21535 typename_type,
21536 /*check_dependency_p=*/true,
21537 /*class_head_p=*/false,
21538 /*is_declaration=*/true);
21539 type = TREE_TYPE (type);
21542 if (type == error_mark_node)
21543 return error_mark_node;
21545 return finish_base_specifier (type, access, virtual_p);
21548 /* Exception handling [gram.exception] */
21550 /* Parse an (optional) noexcept-specification.
21552 noexcept-specification:
21553 noexcept ( constant-expression ) [opt]
21555 If no noexcept-specification is present, returns NULL_TREE.
21556 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21557 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21558 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21559 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21560 in which case a boolean condition is returned instead. */
21562 static tree
21563 cp_parser_noexcept_specification_opt (cp_parser* parser,
21564 bool require_constexpr,
21565 bool* consumed_expr,
21566 bool return_cond)
21568 cp_token *token;
21569 const char *saved_message;
21571 /* Peek at the next token. */
21572 token = cp_lexer_peek_token (parser->lexer);
21574 /* Is it a noexcept-specification? */
21575 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21577 tree expr;
21578 cp_lexer_consume_token (parser->lexer);
21580 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21582 cp_lexer_consume_token (parser->lexer);
21584 if (require_constexpr)
21586 /* Types may not be defined in an exception-specification. */
21587 saved_message = parser->type_definition_forbidden_message;
21588 parser->type_definition_forbidden_message
21589 = G_("types may not be defined in an exception-specification");
21591 expr = cp_parser_constant_expression (parser);
21593 /* Restore the saved message. */
21594 parser->type_definition_forbidden_message = saved_message;
21596 else
21598 expr = cp_parser_expression (parser);
21599 *consumed_expr = true;
21602 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21604 else
21606 expr = boolean_true_node;
21607 if (!require_constexpr)
21608 *consumed_expr = false;
21611 /* We cannot build a noexcept-spec right away because this will check
21612 that expr is a constexpr. */
21613 if (!return_cond)
21614 return build_noexcept_spec (expr, tf_warning_or_error);
21615 else
21616 return expr;
21618 else
21619 return NULL_TREE;
21622 /* Parse an (optional) exception-specification.
21624 exception-specification:
21625 throw ( type-id-list [opt] )
21627 Returns a TREE_LIST representing the exception-specification. The
21628 TREE_VALUE of each node is a type. */
21630 static tree
21631 cp_parser_exception_specification_opt (cp_parser* parser)
21633 cp_token *token;
21634 tree type_id_list;
21635 const char *saved_message;
21637 /* Peek at the next token. */
21638 token = cp_lexer_peek_token (parser->lexer);
21640 /* Is it a noexcept-specification? */
21641 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21642 false);
21643 if (type_id_list != NULL_TREE)
21644 return type_id_list;
21646 /* If it's not `throw', then there's no exception-specification. */
21647 if (!cp_parser_is_keyword (token, RID_THROW))
21648 return NULL_TREE;
21650 #if 0
21651 /* Enable this once a lot of code has transitioned to noexcept? */
21652 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21653 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21654 "deprecated in C++0x; use %<noexcept%> instead");
21655 #endif
21657 /* Consume the `throw'. */
21658 cp_lexer_consume_token (parser->lexer);
21660 /* Look for the `('. */
21661 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21663 /* Peek at the next token. */
21664 token = cp_lexer_peek_token (parser->lexer);
21665 /* If it's not a `)', then there is a type-id-list. */
21666 if (token->type != CPP_CLOSE_PAREN)
21668 /* Types may not be defined in an exception-specification. */
21669 saved_message = parser->type_definition_forbidden_message;
21670 parser->type_definition_forbidden_message
21671 = G_("types may not be defined in an exception-specification");
21672 /* Parse the type-id-list. */
21673 type_id_list = cp_parser_type_id_list (parser);
21674 /* Restore the saved message. */
21675 parser->type_definition_forbidden_message = saved_message;
21677 else
21678 type_id_list = empty_except_spec;
21680 /* Look for the `)'. */
21681 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21683 return type_id_list;
21686 /* Parse an (optional) type-id-list.
21688 type-id-list:
21689 type-id ... [opt]
21690 type-id-list , type-id ... [opt]
21692 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21693 in the order that the types were presented. */
21695 static tree
21696 cp_parser_type_id_list (cp_parser* parser)
21698 tree types = NULL_TREE;
21700 while (true)
21702 cp_token *token;
21703 tree type;
21705 /* Get the next type-id. */
21706 type = cp_parser_type_id (parser);
21707 /* Parse the optional ellipsis. */
21708 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21710 /* Consume the `...'. */
21711 cp_lexer_consume_token (parser->lexer);
21713 /* Turn the type into a pack expansion expression. */
21714 type = make_pack_expansion (type);
21716 /* Add it to the list. */
21717 types = add_exception_specifier (types, type, /*complain=*/1);
21718 /* Peek at the next token. */
21719 token = cp_lexer_peek_token (parser->lexer);
21720 /* If it is not a `,', we are done. */
21721 if (token->type != CPP_COMMA)
21722 break;
21723 /* Consume the `,'. */
21724 cp_lexer_consume_token (parser->lexer);
21727 return nreverse (types);
21730 /* Parse a try-block.
21732 try-block:
21733 try compound-statement handler-seq */
21735 static tree
21736 cp_parser_try_block (cp_parser* parser)
21738 tree try_block;
21740 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21741 if (parser->in_function_body
21742 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21743 error ("%<try%> in %<constexpr%> function");
21745 try_block = begin_try_block ();
21746 cp_parser_compound_statement (parser, NULL, true, false);
21747 finish_try_block (try_block);
21748 cp_parser_handler_seq (parser);
21749 finish_handler_sequence (try_block);
21751 return try_block;
21754 /* Parse a function-try-block.
21756 function-try-block:
21757 try ctor-initializer [opt] function-body handler-seq */
21759 static bool
21760 cp_parser_function_try_block (cp_parser* parser)
21762 tree compound_stmt;
21763 tree try_block;
21764 bool ctor_initializer_p;
21766 /* Look for the `try' keyword. */
21767 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21768 return false;
21769 /* Let the rest of the front end know where we are. */
21770 try_block = begin_function_try_block (&compound_stmt);
21771 /* Parse the function-body. */
21772 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21773 (parser, /*in_function_try_block=*/true);
21774 /* We're done with the `try' part. */
21775 finish_function_try_block (try_block);
21776 /* Parse the handlers. */
21777 cp_parser_handler_seq (parser);
21778 /* We're done with the handlers. */
21779 finish_function_handler_sequence (try_block, compound_stmt);
21781 return ctor_initializer_p;
21784 /* Parse a handler-seq.
21786 handler-seq:
21787 handler handler-seq [opt] */
21789 static void
21790 cp_parser_handler_seq (cp_parser* parser)
21792 while (true)
21794 cp_token *token;
21796 /* Parse the handler. */
21797 cp_parser_handler (parser);
21798 /* Peek at the next token. */
21799 token = cp_lexer_peek_token (parser->lexer);
21800 /* If it's not `catch' then there are no more handlers. */
21801 if (!cp_parser_is_keyword (token, RID_CATCH))
21802 break;
21806 /* Parse a handler.
21808 handler:
21809 catch ( exception-declaration ) compound-statement */
21811 static void
21812 cp_parser_handler (cp_parser* parser)
21814 tree handler;
21815 tree declaration;
21817 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21818 handler = begin_handler ();
21819 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21820 declaration = cp_parser_exception_declaration (parser);
21821 finish_handler_parms (declaration, handler);
21822 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21823 cp_parser_compound_statement (parser, NULL, false, false);
21824 finish_handler (handler);
21827 /* Parse an exception-declaration.
21829 exception-declaration:
21830 type-specifier-seq declarator
21831 type-specifier-seq abstract-declarator
21832 type-specifier-seq
21835 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21836 ellipsis variant is used. */
21838 static tree
21839 cp_parser_exception_declaration (cp_parser* parser)
21841 cp_decl_specifier_seq type_specifiers;
21842 cp_declarator *declarator;
21843 const char *saved_message;
21845 /* If it's an ellipsis, it's easy to handle. */
21846 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21848 /* Consume the `...' token. */
21849 cp_lexer_consume_token (parser->lexer);
21850 return NULL_TREE;
21853 /* Types may not be defined in exception-declarations. */
21854 saved_message = parser->type_definition_forbidden_message;
21855 parser->type_definition_forbidden_message
21856 = G_("types may not be defined in exception-declarations");
21858 /* Parse the type-specifier-seq. */
21859 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21860 /*is_trailing_return=*/false,
21861 &type_specifiers);
21862 /* If it's a `)', then there is no declarator. */
21863 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21864 declarator = NULL;
21865 else
21866 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21867 /*ctor_dtor_or_conv_p=*/NULL,
21868 /*parenthesized_p=*/NULL,
21869 /*member_p=*/false,
21870 /*friend_p=*/false);
21872 /* Restore the saved message. */
21873 parser->type_definition_forbidden_message = saved_message;
21875 if (!type_specifiers.any_specifiers_p)
21876 return error_mark_node;
21878 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21881 /* Parse a throw-expression.
21883 throw-expression:
21884 throw assignment-expression [opt]
21886 Returns a THROW_EXPR representing the throw-expression. */
21888 static tree
21889 cp_parser_throw_expression (cp_parser* parser)
21891 tree expression;
21892 cp_token* token;
21894 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21895 token = cp_lexer_peek_token (parser->lexer);
21896 /* Figure out whether or not there is an assignment-expression
21897 following the "throw" keyword. */
21898 if (token->type == CPP_COMMA
21899 || token->type == CPP_SEMICOLON
21900 || token->type == CPP_CLOSE_PAREN
21901 || token->type == CPP_CLOSE_SQUARE
21902 || token->type == CPP_CLOSE_BRACE
21903 || token->type == CPP_COLON)
21904 expression = NULL_TREE;
21905 else
21906 expression = cp_parser_assignment_expression (parser);
21908 return build_throw (expression);
21911 /* GNU Extensions */
21913 /* Parse an (optional) asm-specification.
21915 asm-specification:
21916 asm ( string-literal )
21918 If the asm-specification is present, returns a STRING_CST
21919 corresponding to the string-literal. Otherwise, returns
21920 NULL_TREE. */
21922 static tree
21923 cp_parser_asm_specification_opt (cp_parser* parser)
21925 cp_token *token;
21926 tree asm_specification;
21928 /* Peek at the next token. */
21929 token = cp_lexer_peek_token (parser->lexer);
21930 /* If the next token isn't the `asm' keyword, then there's no
21931 asm-specification. */
21932 if (!cp_parser_is_keyword (token, RID_ASM))
21933 return NULL_TREE;
21935 /* Consume the `asm' token. */
21936 cp_lexer_consume_token (parser->lexer);
21937 /* Look for the `('. */
21938 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21940 /* Look for the string-literal. */
21941 asm_specification = cp_parser_string_literal (parser, false, false);
21943 /* Look for the `)'. */
21944 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21946 return asm_specification;
21949 /* Parse an asm-operand-list.
21951 asm-operand-list:
21952 asm-operand
21953 asm-operand-list , asm-operand
21955 asm-operand:
21956 string-literal ( expression )
21957 [ string-literal ] string-literal ( expression )
21959 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21960 each node is the expression. The TREE_PURPOSE is itself a
21961 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21962 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21963 is a STRING_CST for the string literal before the parenthesis. Returns
21964 ERROR_MARK_NODE if any of the operands are invalid. */
21966 static tree
21967 cp_parser_asm_operand_list (cp_parser* parser)
21969 tree asm_operands = NULL_TREE;
21970 bool invalid_operands = false;
21972 while (true)
21974 tree string_literal;
21975 tree expression;
21976 tree name;
21978 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21980 /* Consume the `[' token. */
21981 cp_lexer_consume_token (parser->lexer);
21982 /* Read the operand name. */
21983 name = cp_parser_identifier (parser);
21984 if (name != error_mark_node)
21985 name = build_string (IDENTIFIER_LENGTH (name),
21986 IDENTIFIER_POINTER (name));
21987 /* Look for the closing `]'. */
21988 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21990 else
21991 name = NULL_TREE;
21992 /* Look for the string-literal. */
21993 string_literal = cp_parser_string_literal (parser, false, false);
21995 /* Look for the `('. */
21996 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21997 /* Parse the expression. */
21998 expression = cp_parser_expression (parser);
21999 /* Look for the `)'. */
22000 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22002 if (name == error_mark_node
22003 || string_literal == error_mark_node
22004 || expression == error_mark_node)
22005 invalid_operands = true;
22007 /* Add this operand to the list. */
22008 asm_operands = tree_cons (build_tree_list (name, string_literal),
22009 expression,
22010 asm_operands);
22011 /* If the next token is not a `,', there are no more
22012 operands. */
22013 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22014 break;
22015 /* Consume the `,'. */
22016 cp_lexer_consume_token (parser->lexer);
22019 return invalid_operands ? error_mark_node : nreverse (asm_operands);
22022 /* Parse an asm-clobber-list.
22024 asm-clobber-list:
22025 string-literal
22026 asm-clobber-list , string-literal
22028 Returns a TREE_LIST, indicating the clobbers in the order that they
22029 appeared. The TREE_VALUE of each node is a STRING_CST. */
22031 static tree
22032 cp_parser_asm_clobber_list (cp_parser* parser)
22034 tree clobbers = NULL_TREE;
22036 while (true)
22038 tree string_literal;
22040 /* Look for the string literal. */
22041 string_literal = cp_parser_string_literal (parser, false, false);
22042 /* Add it to the list. */
22043 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
22044 /* If the next token is not a `,', then the list is
22045 complete. */
22046 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22047 break;
22048 /* Consume the `,' token. */
22049 cp_lexer_consume_token (parser->lexer);
22052 return clobbers;
22055 /* Parse an asm-label-list.
22057 asm-label-list:
22058 identifier
22059 asm-label-list , identifier
22061 Returns a TREE_LIST, indicating the labels in the order that they
22062 appeared. The TREE_VALUE of each node is a label. */
22064 static tree
22065 cp_parser_asm_label_list (cp_parser* parser)
22067 tree labels = NULL_TREE;
22069 while (true)
22071 tree identifier, label, name;
22073 /* Look for the identifier. */
22074 identifier = cp_parser_identifier (parser);
22075 if (!error_operand_p (identifier))
22077 label = lookup_label (identifier);
22078 if (TREE_CODE (label) == LABEL_DECL)
22080 TREE_USED (label) = 1;
22081 check_goto (label);
22082 name = build_string (IDENTIFIER_LENGTH (identifier),
22083 IDENTIFIER_POINTER (identifier));
22084 labels = tree_cons (name, label, labels);
22087 /* If the next token is not a `,', then the list is
22088 complete. */
22089 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22090 break;
22091 /* Consume the `,' token. */
22092 cp_lexer_consume_token (parser->lexer);
22095 return nreverse (labels);
22098 /* Return TRUE iff the next tokens in the stream are possibly the
22099 beginning of a GNU extension attribute. */
22101 static bool
22102 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22104 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22107 /* Return TRUE iff the next tokens in the stream are possibly the
22108 beginning of a standard C++-11 attribute specifier. */
22110 static bool
22111 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22113 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22116 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22117 beginning of a standard C++-11 attribute specifier. */
22119 static bool
22120 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22122 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22124 return (cxx_dialect >= cxx11
22125 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22126 || (token->type == CPP_OPEN_SQUARE
22127 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22128 && token->type == CPP_OPEN_SQUARE)));
22131 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22132 beginning of a GNU extension attribute. */
22134 static bool
22135 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22137 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22139 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22142 /* Return true iff the next tokens can be the beginning of either a
22143 GNU attribute list, or a standard C++11 attribute sequence. */
22145 static bool
22146 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22148 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22149 || cp_next_tokens_can_be_std_attribute_p (parser));
22152 /* Return true iff the next Nth tokens can be the beginning of either
22153 a GNU attribute list, or a standard C++11 attribute sequence. */
22155 static bool
22156 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22158 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22159 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22162 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22163 of GNU attributes, or return NULL. */
22165 static tree
22166 cp_parser_attributes_opt (cp_parser *parser)
22168 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22169 return cp_parser_gnu_attributes_opt (parser);
22170 return cp_parser_std_attribute_spec_seq (parser);
22173 #define CILK_SIMD_FN_CLAUSE_MASK \
22174 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22175 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22176 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22177 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22178 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22180 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22181 vector [(<clauses>)] */
22183 static void
22184 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22186 bool first_p = parser->cilk_simd_fn_info == NULL;
22187 cp_token *token = v_token;
22188 if (first_p)
22190 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22191 parser->cilk_simd_fn_info->error_seen = false;
22192 parser->cilk_simd_fn_info->fndecl_seen = false;
22193 parser->cilk_simd_fn_info->tokens = vNULL;
22195 int paren_scope = 0;
22196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22198 cp_lexer_consume_token (parser->lexer);
22199 v_token = cp_lexer_peek_token (parser->lexer);
22200 paren_scope++;
22202 while (paren_scope > 0)
22204 token = cp_lexer_peek_token (parser->lexer);
22205 if (token->type == CPP_OPEN_PAREN)
22206 paren_scope++;
22207 else if (token->type == CPP_CLOSE_PAREN)
22208 paren_scope--;
22209 /* Do not push the last ')' */
22210 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22211 cp_lexer_consume_token (parser->lexer);
22214 token->type = CPP_PRAGMA_EOL;
22215 parser->lexer->next_token = token;
22216 cp_lexer_consume_token (parser->lexer);
22218 struct cp_token_cache *cp
22219 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22220 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22223 /* Parse an (optional) series of attributes.
22225 attributes:
22226 attributes attribute
22228 attribute:
22229 __attribute__ (( attribute-list [opt] ))
22231 The return value is as for cp_parser_gnu_attribute_list. */
22233 static tree
22234 cp_parser_gnu_attributes_opt (cp_parser* parser)
22236 tree attributes = NULL_TREE;
22238 while (true)
22240 cp_token *token;
22241 tree attribute_list;
22242 bool ok = true;
22244 /* Peek at the next token. */
22245 token = cp_lexer_peek_token (parser->lexer);
22246 /* If it's not `__attribute__', then we're done. */
22247 if (token->keyword != RID_ATTRIBUTE)
22248 break;
22250 /* Consume the `__attribute__' keyword. */
22251 cp_lexer_consume_token (parser->lexer);
22252 /* Look for the two `(' tokens. */
22253 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22254 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22256 /* Peek at the next token. */
22257 token = cp_lexer_peek_token (parser->lexer);
22258 if (token->type != CPP_CLOSE_PAREN)
22259 /* Parse the attribute-list. */
22260 attribute_list = cp_parser_gnu_attribute_list (parser);
22261 else
22262 /* If the next token is a `)', then there is no attribute
22263 list. */
22264 attribute_list = NULL;
22266 /* Look for the two `)' tokens. */
22267 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22268 ok = false;
22269 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22270 ok = false;
22271 if (!ok)
22272 cp_parser_skip_to_end_of_statement (parser);
22274 /* Add these new attributes to the list. */
22275 attributes = chainon (attributes, attribute_list);
22278 return attributes;
22281 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22282 "__vector" or "__vector__." */
22284 static inline bool
22285 is_cilkplus_vector_p (tree name)
22287 if (flag_cilkplus && is_attribute_p ("vector", name))
22288 return true;
22289 return false;
22292 /* Parse a GNU attribute-list.
22294 attribute-list:
22295 attribute
22296 attribute-list , attribute
22298 attribute:
22299 identifier
22300 identifier ( identifier )
22301 identifier ( identifier , expression-list )
22302 identifier ( expression-list )
22304 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22305 to an attribute. The TREE_PURPOSE of each node is the identifier
22306 indicating which attribute is in use. The TREE_VALUE represents
22307 the arguments, if any. */
22309 static tree
22310 cp_parser_gnu_attribute_list (cp_parser* parser)
22312 tree attribute_list = NULL_TREE;
22313 bool save_translate_strings_p = parser->translate_strings_p;
22315 parser->translate_strings_p = false;
22316 while (true)
22318 cp_token *token;
22319 tree identifier;
22320 tree attribute;
22322 /* Look for the identifier. We also allow keywords here; for
22323 example `__attribute__ ((const))' is legal. */
22324 token = cp_lexer_peek_token (parser->lexer);
22325 if (token->type == CPP_NAME
22326 || token->type == CPP_KEYWORD)
22328 tree arguments = NULL_TREE;
22330 /* Consume the token, but save it since we need it for the
22331 SIMD enabled function parsing. */
22332 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22334 /* Save away the identifier that indicates which attribute
22335 this is. */
22336 identifier = (token->type == CPP_KEYWORD)
22337 /* For keywords, use the canonical spelling, not the
22338 parsed identifier. */
22339 ? ridpointers[(int) token->keyword]
22340 : id_token->u.value;
22342 attribute = build_tree_list (identifier, NULL_TREE);
22344 /* Peek at the next token. */
22345 token = cp_lexer_peek_token (parser->lexer);
22346 /* If it's an `(', then parse the attribute arguments. */
22347 if (token->type == CPP_OPEN_PAREN)
22349 vec<tree, va_gc> *vec;
22350 int attr_flag = (attribute_takes_identifier_p (identifier)
22351 ? id_attr : normal_attr);
22352 if (is_cilkplus_vector_p (identifier))
22354 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22355 continue;
22357 else
22358 vec = cp_parser_parenthesized_expression_list
22359 (parser, attr_flag, /*cast_p=*/false,
22360 /*allow_expansion_p=*/false,
22361 /*non_constant_p=*/NULL);
22362 if (vec == NULL)
22363 arguments = error_mark_node;
22364 else
22366 arguments = build_tree_list_vec (vec);
22367 release_tree_vector (vec);
22369 /* Save the arguments away. */
22370 TREE_VALUE (attribute) = arguments;
22372 else if (is_cilkplus_vector_p (identifier))
22374 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22375 continue;
22378 if (arguments != error_mark_node)
22380 /* Add this attribute to the list. */
22381 TREE_CHAIN (attribute) = attribute_list;
22382 attribute_list = attribute;
22385 token = cp_lexer_peek_token (parser->lexer);
22387 /* Now, look for more attributes. If the next token isn't a
22388 `,', we're done. */
22389 if (token->type != CPP_COMMA)
22390 break;
22392 /* Consume the comma and keep going. */
22393 cp_lexer_consume_token (parser->lexer);
22395 parser->translate_strings_p = save_translate_strings_p;
22397 /* We built up the list in reverse order. */
22398 return nreverse (attribute_list);
22401 /* Parse a standard C++11 attribute.
22403 The returned representation is a TREE_LIST which TREE_PURPOSE is
22404 the scoped name of the attribute, and the TREE_VALUE is its
22405 arguments list.
22407 Note that the scoped name of the attribute is itself a TREE_LIST
22408 which TREE_PURPOSE is the namespace of the attribute, and
22409 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22410 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22411 and which TREE_PURPOSE is directly the attribute name.
22413 Clients of the attribute code should use get_attribute_namespace
22414 and get_attribute_name to get the actual namespace and name of
22415 attributes, regardless of their being GNU or C++11 attributes.
22417 attribute:
22418 attribute-token attribute-argument-clause [opt]
22420 attribute-token:
22421 identifier
22422 attribute-scoped-token
22424 attribute-scoped-token:
22425 attribute-namespace :: identifier
22427 attribute-namespace:
22428 identifier
22430 attribute-argument-clause:
22431 ( balanced-token-seq )
22433 balanced-token-seq:
22434 balanced-token [opt]
22435 balanced-token-seq balanced-token
22437 balanced-token:
22438 ( balanced-token-seq )
22439 [ balanced-token-seq ]
22440 { balanced-token-seq }. */
22442 static tree
22443 cp_parser_std_attribute (cp_parser *parser)
22445 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22446 cp_token *token;
22448 /* First, parse name of the the attribute, a.k.a
22449 attribute-token. */
22451 token = cp_lexer_peek_token (parser->lexer);
22452 if (token->type == CPP_NAME)
22453 attr_id = token->u.value;
22454 else if (token->type == CPP_KEYWORD)
22455 attr_id = ridpointers[(int) token->keyword];
22456 else if (token->flags & NAMED_OP)
22457 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22459 if (attr_id == NULL_TREE)
22460 return NULL_TREE;
22462 cp_lexer_consume_token (parser->lexer);
22464 token = cp_lexer_peek_token (parser->lexer);
22465 if (token->type == CPP_SCOPE)
22467 /* We are seeing a scoped attribute token. */
22469 cp_lexer_consume_token (parser->lexer);
22470 attr_ns = attr_id;
22472 token = cp_lexer_consume_token (parser->lexer);
22473 if (token->type == CPP_NAME)
22474 attr_id = token->u.value;
22475 else if (token->type == CPP_KEYWORD)
22476 attr_id = ridpointers[(int) token->keyword];
22477 else
22479 error_at (token->location,
22480 "expected an identifier for the attribute name");
22481 return error_mark_node;
22483 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22484 NULL_TREE);
22485 token = cp_lexer_peek_token (parser->lexer);
22487 else
22489 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22490 NULL_TREE);
22491 /* C++11 noreturn attribute is equivalent to GNU's. */
22492 if (is_attribute_p ("noreturn", attr_id))
22493 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22494 /* C++14 deprecated attribute is equivalent to GNU's. */
22495 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22497 if (cxx_dialect == cxx11)
22498 pedwarn (token->location, OPT_Wpedantic,
22499 "%<deprecated%> is a C++14 feature;"
22500 " use %<gnu::deprecated%>");
22501 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22505 /* Now parse the optional argument clause of the attribute. */
22507 if (token->type != CPP_OPEN_PAREN)
22508 return attribute;
22511 vec<tree, va_gc> *vec;
22512 int attr_flag = normal_attr;
22514 if (attr_ns == get_identifier ("gnu")
22515 && attribute_takes_identifier_p (attr_id))
22516 /* A GNU attribute that takes an identifier in parameter. */
22517 attr_flag = id_attr;
22519 vec = cp_parser_parenthesized_expression_list
22520 (parser, attr_flag, /*cast_p=*/false,
22521 /*allow_expansion_p=*/true,
22522 /*non_constant_p=*/NULL);
22523 if (vec == NULL)
22524 arguments = error_mark_node;
22525 else
22527 arguments = build_tree_list_vec (vec);
22528 release_tree_vector (vec);
22531 if (arguments == error_mark_node)
22532 attribute = error_mark_node;
22533 else
22534 TREE_VALUE (attribute) = arguments;
22537 return attribute;
22540 /* Check that the attribute ATTRIBUTE appears at most once in the
22541 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
22542 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
22543 isn't implemented yet in GCC. */
22545 static void
22546 cp_parser_check_std_attribute (tree attributes, tree attribute)
22548 if (attributes)
22550 tree name = get_attribute_name (attribute);
22551 if (is_attribute_p ("noreturn", name)
22552 && lookup_attribute ("noreturn", attributes))
22553 error ("attribute noreturn can appear at most once "
22554 "in an attribute-list");
22555 else if (is_attribute_p ("deprecated", name)
22556 && lookup_attribute ("deprecated", attributes))
22557 error ("attribute deprecated can appear at most once "
22558 "in an attribute-list");
22562 /* Parse a list of standard C++-11 attributes.
22564 attribute-list:
22565 attribute [opt]
22566 attribute-list , attribute[opt]
22567 attribute ...
22568 attribute-list , attribute ...
22571 static tree
22572 cp_parser_std_attribute_list (cp_parser *parser)
22574 tree attributes = NULL_TREE, attribute = NULL_TREE;
22575 cp_token *token = NULL;
22577 while (true)
22579 attribute = cp_parser_std_attribute (parser);
22580 if (attribute == error_mark_node)
22581 break;
22582 if (attribute != NULL_TREE)
22584 cp_parser_check_std_attribute (attributes, attribute);
22585 TREE_CHAIN (attribute) = attributes;
22586 attributes = attribute;
22588 token = cp_lexer_peek_token (parser->lexer);
22589 if (token->type == CPP_ELLIPSIS)
22591 cp_lexer_consume_token (parser->lexer);
22592 TREE_VALUE (attribute)
22593 = make_pack_expansion (TREE_VALUE (attribute));
22594 token = cp_lexer_peek_token (parser->lexer);
22596 if (token->type != CPP_COMMA)
22597 break;
22598 cp_lexer_consume_token (parser->lexer);
22600 attributes = nreverse (attributes);
22601 return attributes;
22604 /* Parse a standard C++-11 attribute specifier.
22606 attribute-specifier:
22607 [ [ attribute-list ] ]
22608 alignment-specifier
22610 alignment-specifier:
22611 alignas ( type-id ... [opt] )
22612 alignas ( alignment-expression ... [opt] ). */
22614 static tree
22615 cp_parser_std_attribute_spec (cp_parser *parser)
22617 tree attributes = NULL_TREE;
22618 cp_token *token = cp_lexer_peek_token (parser->lexer);
22620 if (token->type == CPP_OPEN_SQUARE
22621 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22623 cp_lexer_consume_token (parser->lexer);
22624 cp_lexer_consume_token (parser->lexer);
22626 attributes = cp_parser_std_attribute_list (parser);
22628 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22629 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22630 cp_parser_skip_to_end_of_statement (parser);
22631 else
22632 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22633 when we are sure that we have actually parsed them. */
22634 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22636 else
22638 tree alignas_expr;
22640 /* Look for an alignment-specifier. */
22642 token = cp_lexer_peek_token (parser->lexer);
22644 if (token->type != CPP_KEYWORD
22645 || token->keyword != RID_ALIGNAS)
22646 return NULL_TREE;
22648 cp_lexer_consume_token (parser->lexer);
22649 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22651 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22653 cp_parser_error (parser, "expected %<(%>");
22654 return error_mark_node;
22657 cp_parser_parse_tentatively (parser);
22658 alignas_expr = cp_parser_type_id (parser);
22660 if (!cp_parser_parse_definitely (parser))
22662 gcc_assert (alignas_expr == error_mark_node
22663 || alignas_expr == NULL_TREE);
22665 alignas_expr =
22666 cp_parser_assignment_expression (parser);
22667 if (alignas_expr == error_mark_node)
22668 cp_parser_skip_to_end_of_statement (parser);
22669 if (alignas_expr == NULL_TREE
22670 || alignas_expr == error_mark_node)
22671 return alignas_expr;
22674 alignas_expr = cxx_alignas_expr (alignas_expr);
22675 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
22677 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22679 cp_lexer_consume_token (parser->lexer);
22680 alignas_expr = make_pack_expansion (alignas_expr);
22683 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22685 cp_parser_error (parser, "expected %<)%>");
22686 return error_mark_node;
22689 /* Build the C++-11 representation of an 'aligned'
22690 attribute. */
22691 attributes =
22692 build_tree_list (build_tree_list (get_identifier ("gnu"),
22693 get_identifier ("aligned")),
22694 alignas_expr);
22697 return attributes;
22700 /* Parse a standard C++-11 attribute-specifier-seq.
22702 attribute-specifier-seq:
22703 attribute-specifier-seq [opt] attribute-specifier
22706 static tree
22707 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22709 tree attr_specs = NULL;
22711 while (true)
22713 tree attr_spec = cp_parser_std_attribute_spec (parser);
22714 if (attr_spec == NULL_TREE)
22715 break;
22716 if (attr_spec == error_mark_node)
22717 return error_mark_node;
22719 TREE_CHAIN (attr_spec) = attr_specs;
22720 attr_specs = attr_spec;
22723 attr_specs = nreverse (attr_specs);
22724 return attr_specs;
22727 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22728 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22729 current value of the PEDANTIC flag, regardless of whether or not
22730 the `__extension__' keyword is present. The caller is responsible
22731 for restoring the value of the PEDANTIC flag. */
22733 static bool
22734 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22736 /* Save the old value of the PEDANTIC flag. */
22737 *saved_pedantic = pedantic;
22739 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22741 /* Consume the `__extension__' token. */
22742 cp_lexer_consume_token (parser->lexer);
22743 /* We're not being pedantic while the `__extension__' keyword is
22744 in effect. */
22745 pedantic = 0;
22747 return true;
22750 return false;
22753 /* Parse a label declaration.
22755 label-declaration:
22756 __label__ label-declarator-seq ;
22758 label-declarator-seq:
22759 identifier , label-declarator-seq
22760 identifier */
22762 static void
22763 cp_parser_label_declaration (cp_parser* parser)
22765 /* Look for the `__label__' keyword. */
22766 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22768 while (true)
22770 tree identifier;
22772 /* Look for an identifier. */
22773 identifier = cp_parser_identifier (parser);
22774 /* If we failed, stop. */
22775 if (identifier == error_mark_node)
22776 break;
22777 /* Declare it as a label. */
22778 finish_label_decl (identifier);
22779 /* If the next token is a `;', stop. */
22780 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22781 break;
22782 /* Look for the `,' separating the label declarations. */
22783 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22786 /* Look for the final `;'. */
22787 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22790 /* Support Functions */
22792 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22793 NAME should have one of the representations used for an
22794 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22795 is returned. If PARSER->SCOPE is a dependent type, then a
22796 SCOPE_REF is returned.
22798 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22799 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22800 was formed. Abstractly, such entities should not be passed to this
22801 function, because they do not need to be looked up, but it is
22802 simpler to check for this special case here, rather than at the
22803 call-sites.
22805 In cases not explicitly covered above, this function returns a
22806 DECL, OVERLOAD, or baselink representing the result of the lookup.
22807 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22808 is returned.
22810 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22811 (e.g., "struct") that was used. In that case bindings that do not
22812 refer to types are ignored.
22814 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22815 ignored.
22817 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22818 are ignored.
22820 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22821 types.
22823 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22824 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22825 NULL_TREE otherwise. */
22827 static tree
22828 cp_parser_lookup_name (cp_parser *parser, tree name,
22829 enum tag_types tag_type,
22830 bool is_template,
22831 bool is_namespace,
22832 bool check_dependency,
22833 tree *ambiguous_decls,
22834 location_t name_location)
22836 tree decl;
22837 tree object_type = parser->context->object_type;
22839 /* Assume that the lookup will be unambiguous. */
22840 if (ambiguous_decls)
22841 *ambiguous_decls = NULL_TREE;
22843 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22844 no longer valid. Note that if we are parsing tentatively, and
22845 the parse fails, OBJECT_TYPE will be automatically restored. */
22846 parser->context->object_type = NULL_TREE;
22848 if (name == error_mark_node)
22849 return error_mark_node;
22851 /* A template-id has already been resolved; there is no lookup to
22852 do. */
22853 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22854 return name;
22855 if (BASELINK_P (name))
22857 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22858 == TEMPLATE_ID_EXPR);
22859 return name;
22862 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22863 it should already have been checked to make sure that the name
22864 used matches the type being destroyed. */
22865 if (TREE_CODE (name) == BIT_NOT_EXPR)
22867 tree type;
22869 /* Figure out to which type this destructor applies. */
22870 if (parser->scope)
22871 type = parser->scope;
22872 else if (object_type)
22873 type = object_type;
22874 else
22875 type = current_class_type;
22876 /* If that's not a class type, there is no destructor. */
22877 if (!type || !CLASS_TYPE_P (type))
22878 return error_mark_node;
22879 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22880 lazily_declare_fn (sfk_destructor, type);
22881 if (!CLASSTYPE_DESTRUCTORS (type))
22882 return error_mark_node;
22883 /* If it was a class type, return the destructor. */
22884 return CLASSTYPE_DESTRUCTORS (type);
22887 /* By this point, the NAME should be an ordinary identifier. If
22888 the id-expression was a qualified name, the qualifying scope is
22889 stored in PARSER->SCOPE at this point. */
22890 gcc_assert (identifier_p (name));
22892 /* Perform the lookup. */
22893 if (parser->scope)
22895 bool dependent_p;
22897 if (parser->scope == error_mark_node)
22898 return error_mark_node;
22900 /* If the SCOPE is dependent, the lookup must be deferred until
22901 the template is instantiated -- unless we are explicitly
22902 looking up names in uninstantiated templates. Even then, we
22903 cannot look up the name if the scope is not a class type; it
22904 might, for example, be a template type parameter. */
22905 dependent_p = (TYPE_P (parser->scope)
22906 && dependent_scope_p (parser->scope));
22907 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22908 && dependent_p)
22909 /* Defer lookup. */
22910 decl = error_mark_node;
22911 else
22913 tree pushed_scope = NULL_TREE;
22915 /* If PARSER->SCOPE is a dependent type, then it must be a
22916 class type, and we must not be checking dependencies;
22917 otherwise, we would have processed this lookup above. So
22918 that PARSER->SCOPE is not considered a dependent base by
22919 lookup_member, we must enter the scope here. */
22920 if (dependent_p)
22921 pushed_scope = push_scope (parser->scope);
22923 /* If the PARSER->SCOPE is a template specialization, it
22924 may be instantiated during name lookup. In that case,
22925 errors may be issued. Even if we rollback the current
22926 tentative parse, those errors are valid. */
22927 decl = lookup_qualified_name (parser->scope, name,
22928 tag_type != none_type,
22929 /*complain=*/true);
22931 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22932 lookup result and the nested-name-specifier nominates a class C:
22933 * if the name specified after the nested-name-specifier, when
22934 looked up in C, is the injected-class-name of C (Clause 9), or
22935 * if the name specified after the nested-name-specifier is the
22936 same as the identifier or the simple-template-id's template-
22937 name in the last component of the nested-name-specifier,
22938 the name is instead considered to name the constructor of
22939 class C. [ Note: for example, the constructor is not an
22940 acceptable lookup result in an elaborated-type-specifier so
22941 the constructor would not be used in place of the
22942 injected-class-name. --end note ] Such a constructor name
22943 shall be used only in the declarator-id of a declaration that
22944 names a constructor or in a using-declaration. */
22945 if (tag_type == none_type
22946 && DECL_SELF_REFERENCE_P (decl)
22947 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22948 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22949 tag_type != none_type,
22950 /*complain=*/true);
22952 /* If we have a single function from a using decl, pull it out. */
22953 if (TREE_CODE (decl) == OVERLOAD
22954 && !really_overloaded_fn (decl))
22955 decl = OVL_FUNCTION (decl);
22957 if (pushed_scope)
22958 pop_scope (pushed_scope);
22961 /* If the scope is a dependent type and either we deferred lookup or
22962 we did lookup but didn't find the name, rememeber the name. */
22963 if (decl == error_mark_node && TYPE_P (parser->scope)
22964 && dependent_type_p (parser->scope))
22966 if (tag_type)
22968 tree type;
22970 /* The resolution to Core Issue 180 says that `struct
22971 A::B' should be considered a type-name, even if `A'
22972 is dependent. */
22973 type = make_typename_type (parser->scope, name, tag_type,
22974 /*complain=*/tf_error);
22975 if (type != error_mark_node)
22976 decl = TYPE_NAME (type);
22978 else if (is_template
22979 && (cp_parser_next_token_ends_template_argument_p (parser)
22980 || cp_lexer_next_token_is (parser->lexer,
22981 CPP_CLOSE_PAREN)))
22982 decl = make_unbound_class_template (parser->scope,
22983 name, NULL_TREE,
22984 /*complain=*/tf_error);
22985 else
22986 decl = build_qualified_name (/*type=*/NULL_TREE,
22987 parser->scope, name,
22988 is_template);
22990 parser->qualifying_scope = parser->scope;
22991 parser->object_scope = NULL_TREE;
22993 else if (object_type)
22995 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22996 OBJECT_TYPE is not a class. */
22997 if (CLASS_TYPE_P (object_type))
22998 /* If the OBJECT_TYPE is a template specialization, it may
22999 be instantiated during name lookup. In that case, errors
23000 may be issued. Even if we rollback the current tentative
23001 parse, those errors are valid. */
23002 decl = lookup_member (object_type,
23003 name,
23004 /*protect=*/0,
23005 tag_type != none_type,
23006 tf_warning_or_error);
23007 else
23008 decl = NULL_TREE;
23010 if (!decl)
23011 /* Look it up in the enclosing context. */
23012 decl = lookup_name_real (name, tag_type != none_type,
23013 /*nonclass=*/0,
23014 /*block_p=*/true, is_namespace, 0);
23015 parser->object_scope = object_type;
23016 parser->qualifying_scope = NULL_TREE;
23018 else
23020 decl = lookup_name_real (name, tag_type != none_type,
23021 /*nonclass=*/0,
23022 /*block_p=*/true, is_namespace, 0);
23023 parser->qualifying_scope = NULL_TREE;
23024 parser->object_scope = NULL_TREE;
23027 /* If the lookup failed, let our caller know. */
23028 if (!decl || decl == error_mark_node)
23029 return error_mark_node;
23031 /* Pull out the template from an injected-class-name (or multiple). */
23032 if (is_template)
23033 decl = maybe_get_template_decl_from_type_decl (decl);
23035 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
23036 if (TREE_CODE (decl) == TREE_LIST)
23038 if (ambiguous_decls)
23039 *ambiguous_decls = decl;
23040 /* The error message we have to print is too complicated for
23041 cp_parser_error, so we incorporate its actions directly. */
23042 if (!cp_parser_simulate_error (parser))
23044 error_at (name_location, "reference to %qD is ambiguous",
23045 name);
23046 print_candidates (decl);
23048 return error_mark_node;
23051 gcc_assert (DECL_P (decl)
23052 || TREE_CODE (decl) == OVERLOAD
23053 || TREE_CODE (decl) == SCOPE_REF
23054 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
23055 || BASELINK_P (decl));
23057 /* If we have resolved the name of a member declaration, check to
23058 see if the declaration is accessible. When the name resolves to
23059 set of overloaded functions, accessibility is checked when
23060 overload resolution is done.
23062 During an explicit instantiation, access is not checked at all,
23063 as per [temp.explicit]. */
23064 if (DECL_P (decl))
23065 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
23067 maybe_record_typedef_use (decl);
23069 return decl;
23072 /* Like cp_parser_lookup_name, but for use in the typical case where
23073 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
23074 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
23076 static tree
23077 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
23079 return cp_parser_lookup_name (parser, name,
23080 none_type,
23081 /*is_template=*/false,
23082 /*is_namespace=*/false,
23083 /*check_dependency=*/true,
23084 /*ambiguous_decls=*/NULL,
23085 location);
23088 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
23089 the current context, return the TYPE_DECL. If TAG_NAME_P is
23090 true, the DECL indicates the class being defined in a class-head,
23091 or declared in an elaborated-type-specifier.
23093 Otherwise, return DECL. */
23095 static tree
23096 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
23098 /* If the TEMPLATE_DECL is being declared as part of a class-head,
23099 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
23101 struct A {
23102 template <typename T> struct B;
23105 template <typename T> struct A::B {};
23107 Similarly, in an elaborated-type-specifier:
23109 namespace N { struct X{}; }
23111 struct A {
23112 template <typename T> friend struct N::X;
23115 However, if the DECL refers to a class type, and we are in
23116 the scope of the class, then the name lookup automatically
23117 finds the TYPE_DECL created by build_self_reference rather
23118 than a TEMPLATE_DECL. For example, in:
23120 template <class T> struct S {
23121 S s;
23124 there is no need to handle such case. */
23126 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23127 return DECL_TEMPLATE_RESULT (decl);
23129 return decl;
23132 /* If too many, or too few, template-parameter lists apply to the
23133 declarator, issue an error message. Returns TRUE if all went well,
23134 and FALSE otherwise. */
23136 static bool
23137 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23138 cp_declarator *declarator,
23139 location_t declarator_location)
23141 switch (declarator->kind)
23143 case cdk_id:
23145 unsigned num_templates = 0;
23146 tree scope = declarator->u.id.qualifying_scope;
23148 if (scope)
23149 num_templates = num_template_headers_for_class (scope);
23150 else if (TREE_CODE (declarator->u.id.unqualified_name)
23151 == TEMPLATE_ID_EXPR)
23152 /* If the DECLARATOR has the form `X<y>' then it uses one
23153 additional level of template parameters. */
23154 ++num_templates;
23156 return cp_parser_check_template_parameters
23157 (parser, num_templates, declarator_location, declarator);
23160 case cdk_function:
23161 case cdk_array:
23162 case cdk_pointer:
23163 case cdk_reference:
23164 case cdk_ptrmem:
23165 return (cp_parser_check_declarator_template_parameters
23166 (parser, declarator->declarator, declarator_location));
23168 case cdk_error:
23169 return true;
23171 default:
23172 gcc_unreachable ();
23174 return false;
23177 /* NUM_TEMPLATES were used in the current declaration. If that is
23178 invalid, return FALSE and issue an error messages. Otherwise,
23179 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23180 declarator and we can print more accurate diagnostics. */
23182 static bool
23183 cp_parser_check_template_parameters (cp_parser* parser,
23184 unsigned num_templates,
23185 location_t location,
23186 cp_declarator *declarator)
23188 /* If there are the same number of template classes and parameter
23189 lists, that's OK. */
23190 if (parser->num_template_parameter_lists == num_templates)
23191 return true;
23192 /* If there are more, but only one more, then we are referring to a
23193 member template. That's OK too. */
23194 if (parser->num_template_parameter_lists == num_templates + 1)
23195 return true;
23196 /* If there are more template classes than parameter lists, we have
23197 something like:
23199 template <class T> void S<T>::R<T>::f (); */
23200 if (parser->num_template_parameter_lists < num_templates)
23202 if (declarator && !current_function_decl)
23203 error_at (location, "specializing member %<%T::%E%> "
23204 "requires %<template<>%> syntax",
23205 declarator->u.id.qualifying_scope,
23206 declarator->u.id.unqualified_name);
23207 else if (declarator)
23208 error_at (location, "invalid declaration of %<%T::%E%>",
23209 declarator->u.id.qualifying_scope,
23210 declarator->u.id.unqualified_name);
23211 else
23212 error_at (location, "too few template-parameter-lists");
23213 return false;
23215 /* Otherwise, there are too many template parameter lists. We have
23216 something like:
23218 template <class T> template <class U> void S::f(); */
23219 error_at (location, "too many template-parameter-lists");
23220 return false;
23223 /* Parse an optional `::' token indicating that the following name is
23224 from the global namespace. If so, PARSER->SCOPE is set to the
23225 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23226 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23227 Returns the new value of PARSER->SCOPE, if the `::' token is
23228 present, and NULL_TREE otherwise. */
23230 static tree
23231 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23233 cp_token *token;
23235 /* Peek at the next token. */
23236 token = cp_lexer_peek_token (parser->lexer);
23237 /* If we're looking at a `::' token then we're starting from the
23238 global namespace, not our current location. */
23239 if (token->type == CPP_SCOPE)
23241 /* Consume the `::' token. */
23242 cp_lexer_consume_token (parser->lexer);
23243 /* Set the SCOPE so that we know where to start the lookup. */
23244 parser->scope = global_namespace;
23245 parser->qualifying_scope = global_namespace;
23246 parser->object_scope = NULL_TREE;
23248 return parser->scope;
23250 else if (!current_scope_valid_p)
23252 parser->scope = NULL_TREE;
23253 parser->qualifying_scope = NULL_TREE;
23254 parser->object_scope = NULL_TREE;
23257 return NULL_TREE;
23260 /* Returns TRUE if the upcoming token sequence is the start of a
23261 constructor declarator. If FRIEND_P is true, the declarator is
23262 preceded by the `friend' specifier. */
23264 static bool
23265 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23267 bool constructor_p;
23268 bool outside_class_specifier_p;
23269 tree nested_name_specifier;
23270 cp_token *next_token;
23272 /* The common case is that this is not a constructor declarator, so
23273 try to avoid doing lots of work if at all possible. It's not
23274 valid declare a constructor at function scope. */
23275 if (parser->in_function_body)
23276 return false;
23277 /* And only certain tokens can begin a constructor declarator. */
23278 next_token = cp_lexer_peek_token (parser->lexer);
23279 if (next_token->type != CPP_NAME
23280 && next_token->type != CPP_SCOPE
23281 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23282 && next_token->type != CPP_TEMPLATE_ID)
23283 return false;
23285 /* Parse tentatively; we are going to roll back all of the tokens
23286 consumed here. */
23287 cp_parser_parse_tentatively (parser);
23288 /* Assume that we are looking at a constructor declarator. */
23289 constructor_p = true;
23291 /* Look for the optional `::' operator. */
23292 cp_parser_global_scope_opt (parser,
23293 /*current_scope_valid_p=*/false);
23294 /* Look for the nested-name-specifier. */
23295 nested_name_specifier
23296 = (cp_parser_nested_name_specifier_opt (parser,
23297 /*typename_keyword_p=*/false,
23298 /*check_dependency_p=*/false,
23299 /*type_p=*/false,
23300 /*is_declaration=*/false));
23302 outside_class_specifier_p = (!at_class_scope_p ()
23303 || !TYPE_BEING_DEFINED (current_class_type)
23304 || friend_p);
23306 /* Outside of a class-specifier, there must be a
23307 nested-name-specifier. */
23308 if (!nested_name_specifier && outside_class_specifier_p)
23309 constructor_p = false;
23310 else if (nested_name_specifier == error_mark_node)
23311 constructor_p = false;
23313 /* If we have a class scope, this is easy; DR 147 says that S::S always
23314 names the constructor, and no other qualified name could. */
23315 if (constructor_p && nested_name_specifier
23316 && CLASS_TYPE_P (nested_name_specifier))
23318 tree id = cp_parser_unqualified_id (parser,
23319 /*template_keyword_p=*/false,
23320 /*check_dependency_p=*/false,
23321 /*declarator_p=*/true,
23322 /*optional_p=*/false);
23323 if (is_overloaded_fn (id))
23324 id = DECL_NAME (get_first_fn (id));
23325 if (!constructor_name_p (id, nested_name_specifier))
23326 constructor_p = false;
23328 /* If we still think that this might be a constructor-declarator,
23329 look for a class-name. */
23330 else if (constructor_p)
23332 /* If we have:
23334 template <typename T> struct S {
23335 S();
23338 we must recognize that the nested `S' names a class. */
23339 tree type_decl;
23340 type_decl = cp_parser_class_name (parser,
23341 /*typename_keyword_p=*/false,
23342 /*template_keyword_p=*/false,
23343 none_type,
23344 /*check_dependency_p=*/false,
23345 /*class_head_p=*/false,
23346 /*is_declaration=*/false);
23347 /* If there was no class-name, then this is not a constructor.
23348 Otherwise, if we are in a class-specifier and we aren't
23349 handling a friend declaration, check that its type matches
23350 current_class_type (c++/38313). Note: error_mark_node
23351 is left alone for error recovery purposes. */
23352 constructor_p = (!cp_parser_error_occurred (parser)
23353 && (outside_class_specifier_p
23354 || type_decl == error_mark_node
23355 || same_type_p (current_class_type,
23356 TREE_TYPE (type_decl))));
23358 /* If we're still considering a constructor, we have to see a `(',
23359 to begin the parameter-declaration-clause, followed by either a
23360 `)', an `...', or a decl-specifier. We need to check for a
23361 type-specifier to avoid being fooled into thinking that:
23363 S (f) (int);
23365 is a constructor. (It is actually a function named `f' that
23366 takes one parameter (of type `int') and returns a value of type
23367 `S'. */
23368 if (constructor_p
23369 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23370 constructor_p = false;
23372 if (constructor_p
23373 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23374 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23375 /* A parameter declaration begins with a decl-specifier,
23376 which is either the "attribute" keyword, a storage class
23377 specifier, or (usually) a type-specifier. */
23378 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23380 tree type;
23381 tree pushed_scope = NULL_TREE;
23382 unsigned saved_num_template_parameter_lists;
23384 /* Names appearing in the type-specifier should be looked up
23385 in the scope of the class. */
23386 if (current_class_type)
23387 type = NULL_TREE;
23388 else
23390 type = TREE_TYPE (type_decl);
23391 if (TREE_CODE (type) == TYPENAME_TYPE)
23393 type = resolve_typename_type (type,
23394 /*only_current_p=*/false);
23395 if (TREE_CODE (type) == TYPENAME_TYPE)
23397 cp_parser_abort_tentative_parse (parser);
23398 return false;
23401 pushed_scope = push_scope (type);
23404 /* Inside the constructor parameter list, surrounding
23405 template-parameter-lists do not apply. */
23406 saved_num_template_parameter_lists
23407 = parser->num_template_parameter_lists;
23408 parser->num_template_parameter_lists = 0;
23410 /* Look for the type-specifier. */
23411 cp_parser_type_specifier (parser,
23412 CP_PARSER_FLAGS_NONE,
23413 /*decl_specs=*/NULL,
23414 /*is_declarator=*/true,
23415 /*declares_class_or_enum=*/NULL,
23416 /*is_cv_qualifier=*/NULL);
23418 parser->num_template_parameter_lists
23419 = saved_num_template_parameter_lists;
23421 /* Leave the scope of the class. */
23422 if (pushed_scope)
23423 pop_scope (pushed_scope);
23425 constructor_p = !cp_parser_error_occurred (parser);
23429 /* We did not really want to consume any tokens. */
23430 cp_parser_abort_tentative_parse (parser);
23432 return constructor_p;
23435 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23436 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23437 they must be performed once we are in the scope of the function.
23439 Returns the function defined. */
23441 static tree
23442 cp_parser_function_definition_from_specifiers_and_declarator
23443 (cp_parser* parser,
23444 cp_decl_specifier_seq *decl_specifiers,
23445 tree attributes,
23446 const cp_declarator *declarator)
23448 tree fn;
23449 bool success_p;
23451 /* Begin the function-definition. */
23452 success_p = start_function (decl_specifiers, declarator, attributes);
23454 /* The things we're about to see are not directly qualified by any
23455 template headers we've seen thus far. */
23456 reset_specialization ();
23458 /* If there were names looked up in the decl-specifier-seq that we
23459 did not check, check them now. We must wait until we are in the
23460 scope of the function to perform the checks, since the function
23461 might be a friend. */
23462 perform_deferred_access_checks (tf_warning_or_error);
23464 if (success_p)
23466 cp_finalize_omp_declare_simd (parser, current_function_decl);
23467 parser->omp_declare_simd = NULL;
23470 if (!success_p)
23472 /* Skip the entire function. */
23473 cp_parser_skip_to_end_of_block_or_statement (parser);
23474 fn = error_mark_node;
23476 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23478 /* Seen already, skip it. An error message has already been output. */
23479 cp_parser_skip_to_end_of_block_or_statement (parser);
23480 fn = current_function_decl;
23481 current_function_decl = NULL_TREE;
23482 /* If this is a function from a class, pop the nested class. */
23483 if (current_class_name)
23484 pop_nested_class ();
23486 else
23488 timevar_id_t tv;
23489 if (DECL_DECLARED_INLINE_P (current_function_decl))
23490 tv = TV_PARSE_INLINE;
23491 else
23492 tv = TV_PARSE_FUNC;
23493 timevar_push (tv);
23494 fn = cp_parser_function_definition_after_declarator (parser,
23495 /*inline_p=*/false);
23496 timevar_pop (tv);
23499 return fn;
23502 /* Parse the part of a function-definition that follows the
23503 declarator. INLINE_P is TRUE iff this function is an inline
23504 function defined within a class-specifier.
23506 Returns the function defined. */
23508 static tree
23509 cp_parser_function_definition_after_declarator (cp_parser* parser,
23510 bool inline_p)
23512 tree fn;
23513 bool ctor_initializer_p = false;
23514 bool saved_in_unbraced_linkage_specification_p;
23515 bool saved_in_function_body;
23516 unsigned saved_num_template_parameter_lists;
23517 cp_token *token;
23518 bool fully_implicit_function_template_p
23519 = parser->fully_implicit_function_template_p;
23520 parser->fully_implicit_function_template_p = false;
23521 tree implicit_template_parms
23522 = parser->implicit_template_parms;
23523 parser->implicit_template_parms = 0;
23524 cp_binding_level* implicit_template_scope
23525 = parser->implicit_template_scope;
23526 parser->implicit_template_scope = 0;
23528 saved_in_function_body = parser->in_function_body;
23529 parser->in_function_body = true;
23530 /* If the next token is `return', then the code may be trying to
23531 make use of the "named return value" extension that G++ used to
23532 support. */
23533 token = cp_lexer_peek_token (parser->lexer);
23534 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23536 /* Consume the `return' keyword. */
23537 cp_lexer_consume_token (parser->lexer);
23538 /* Look for the identifier that indicates what value is to be
23539 returned. */
23540 cp_parser_identifier (parser);
23541 /* Issue an error message. */
23542 error_at (token->location,
23543 "named return values are no longer supported");
23544 /* Skip tokens until we reach the start of the function body. */
23545 while (true)
23547 cp_token *token = cp_lexer_peek_token (parser->lexer);
23548 if (token->type == CPP_OPEN_BRACE
23549 || token->type == CPP_EOF
23550 || token->type == CPP_PRAGMA_EOL)
23551 break;
23552 cp_lexer_consume_token (parser->lexer);
23555 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23556 anything declared inside `f'. */
23557 saved_in_unbraced_linkage_specification_p
23558 = parser->in_unbraced_linkage_specification_p;
23559 parser->in_unbraced_linkage_specification_p = false;
23560 /* Inside the function, surrounding template-parameter-lists do not
23561 apply. */
23562 saved_num_template_parameter_lists
23563 = parser->num_template_parameter_lists;
23564 parser->num_template_parameter_lists = 0;
23566 start_lambda_scope (current_function_decl);
23568 /* If the next token is `try', `__transaction_atomic', or
23569 `__transaction_relaxed`, then we are looking at either function-try-block
23570 or function-transaction-block. Note that all of these include the
23571 function-body. */
23572 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23573 ctor_initializer_p = cp_parser_function_transaction (parser,
23574 RID_TRANSACTION_ATOMIC);
23575 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23576 RID_TRANSACTION_RELAXED))
23577 ctor_initializer_p = cp_parser_function_transaction (parser,
23578 RID_TRANSACTION_RELAXED);
23579 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23580 ctor_initializer_p = cp_parser_function_try_block (parser);
23581 else
23582 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23583 (parser, /*in_function_try_block=*/false);
23585 finish_lambda_scope ();
23587 /* Finish the function. */
23588 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23589 (inline_p ? 2 : 0));
23590 /* Generate code for it, if necessary. */
23591 expand_or_defer_fn (fn);
23592 /* Restore the saved values. */
23593 parser->in_unbraced_linkage_specification_p
23594 = saved_in_unbraced_linkage_specification_p;
23595 parser->num_template_parameter_lists
23596 = saved_num_template_parameter_lists;
23597 parser->in_function_body = saved_in_function_body;
23599 parser->fully_implicit_function_template_p
23600 = fully_implicit_function_template_p;
23601 parser->implicit_template_parms
23602 = implicit_template_parms;
23603 parser->implicit_template_scope
23604 = implicit_template_scope;
23606 if (parser->fully_implicit_function_template_p)
23607 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23609 return fn;
23612 /* Parse a template-declaration, assuming that the `export' (and
23613 `extern') keywords, if present, has already been scanned. MEMBER_P
23614 is as for cp_parser_template_declaration. */
23616 static void
23617 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23619 tree decl = NULL_TREE;
23620 vec<deferred_access_check, va_gc> *checks;
23621 tree parameter_list;
23622 bool friend_p = false;
23623 bool need_lang_pop;
23624 cp_token *token;
23626 /* Look for the `template' keyword. */
23627 token = cp_lexer_peek_token (parser->lexer);
23628 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23629 return;
23631 /* And the `<'. */
23632 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23633 return;
23634 if (at_class_scope_p () && current_function_decl)
23636 /* 14.5.2.2 [temp.mem]
23638 A local class shall not have member templates. */
23639 error_at (token->location,
23640 "invalid declaration of member template in local class");
23641 cp_parser_skip_to_end_of_block_or_statement (parser);
23642 return;
23644 /* [temp]
23646 A template ... shall not have C linkage. */
23647 if (current_lang_name == lang_name_c)
23649 error_at (token->location, "template with C linkage");
23650 /* Give it C++ linkage to avoid confusing other parts of the
23651 front end. */
23652 push_lang_context (lang_name_cplusplus);
23653 need_lang_pop = true;
23655 else
23656 need_lang_pop = false;
23658 /* We cannot perform access checks on the template parameter
23659 declarations until we know what is being declared, just as we
23660 cannot check the decl-specifier list. */
23661 push_deferring_access_checks (dk_deferred);
23663 /* If the next token is `>', then we have an invalid
23664 specialization. Rather than complain about an invalid template
23665 parameter, issue an error message here. */
23666 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23668 cp_parser_error (parser, "invalid explicit specialization");
23669 begin_specialization ();
23670 parameter_list = NULL_TREE;
23672 else
23674 /* Parse the template parameters. */
23675 parameter_list = cp_parser_template_parameter_list (parser);
23678 /* Get the deferred access checks from the parameter list. These
23679 will be checked once we know what is being declared, as for a
23680 member template the checks must be performed in the scope of the
23681 class containing the member. */
23682 checks = get_deferred_access_checks ();
23684 /* Look for the `>'. */
23685 cp_parser_skip_to_end_of_template_parameter_list (parser);
23686 /* We just processed one more parameter list. */
23687 ++parser->num_template_parameter_lists;
23688 /* If the next token is `template', there are more template
23689 parameters. */
23690 if (cp_lexer_next_token_is_keyword (parser->lexer,
23691 RID_TEMPLATE))
23692 cp_parser_template_declaration_after_export (parser, member_p);
23693 else if (cxx_dialect >= cxx11
23694 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23695 decl = cp_parser_alias_declaration (parser);
23696 else
23698 /* There are no access checks when parsing a template, as we do not
23699 know if a specialization will be a friend. */
23700 push_deferring_access_checks (dk_no_check);
23701 token = cp_lexer_peek_token (parser->lexer);
23702 decl = cp_parser_single_declaration (parser,
23703 checks,
23704 member_p,
23705 /*explicit_specialization_p=*/false,
23706 &friend_p);
23707 pop_deferring_access_checks ();
23709 /* If this is a member template declaration, let the front
23710 end know. */
23711 if (member_p && !friend_p && decl)
23713 if (TREE_CODE (decl) == TYPE_DECL)
23714 cp_parser_check_access_in_redeclaration (decl, token->location);
23716 decl = finish_member_template_decl (decl);
23718 else if (friend_p && decl
23719 && DECL_DECLARES_TYPE_P (decl))
23720 make_friend_class (current_class_type, TREE_TYPE (decl),
23721 /*complain=*/true);
23723 /* We are done with the current parameter list. */
23724 --parser->num_template_parameter_lists;
23726 pop_deferring_access_checks ();
23728 /* Finish up. */
23729 finish_template_decl (parameter_list);
23731 /* Check the template arguments for a literal operator template. */
23732 if (decl
23733 && DECL_DECLARES_FUNCTION_P (decl)
23734 && UDLIT_OPER_P (DECL_NAME (decl)))
23736 bool ok = true;
23737 if (parameter_list == NULL_TREE)
23738 ok = false;
23739 else
23741 int num_parms = TREE_VEC_LENGTH (parameter_list);
23742 if (num_parms == 1)
23744 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23745 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23746 if (TREE_TYPE (parm) != char_type_node
23747 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23748 ok = false;
23750 else if (num_parms == 2 && cxx_dialect >= cxx14)
23752 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23753 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23754 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23755 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23756 if (TREE_TYPE (parm) != TREE_TYPE (type)
23757 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23758 ok = false;
23760 else
23761 ok = false;
23763 if (!ok)
23765 if (cxx_dialect >= cxx14)
23766 error ("literal operator template %qD has invalid parameter list."
23767 " Expected non-type template argument pack <char...>"
23768 " or <typename CharT, CharT...>",
23769 decl);
23770 else
23771 error ("literal operator template %qD has invalid parameter list."
23772 " Expected non-type template argument pack <char...>",
23773 decl);
23776 /* Register member declarations. */
23777 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23778 finish_member_declaration (decl);
23779 /* For the erroneous case of a template with C linkage, we pushed an
23780 implicit C++ linkage scope; exit that scope now. */
23781 if (need_lang_pop)
23782 pop_lang_context ();
23783 /* If DECL is a function template, we must return to parse it later.
23784 (Even though there is no definition, there might be default
23785 arguments that need handling.) */
23786 if (member_p && decl
23787 && DECL_DECLARES_FUNCTION_P (decl))
23788 vec_safe_push (unparsed_funs_with_definitions, decl);
23791 /* Perform the deferred access checks from a template-parameter-list.
23792 CHECKS is a TREE_LIST of access checks, as returned by
23793 get_deferred_access_checks. */
23795 static void
23796 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23798 ++processing_template_parmlist;
23799 perform_access_checks (checks, tf_warning_or_error);
23800 --processing_template_parmlist;
23803 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23804 `function-definition' sequence that follows a template header.
23805 If MEMBER_P is true, this declaration appears in a class scope.
23807 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23808 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23810 static tree
23811 cp_parser_single_declaration (cp_parser* parser,
23812 vec<deferred_access_check, va_gc> *checks,
23813 bool member_p,
23814 bool explicit_specialization_p,
23815 bool* friend_p)
23817 int declares_class_or_enum;
23818 tree decl = NULL_TREE;
23819 cp_decl_specifier_seq decl_specifiers;
23820 bool function_definition_p = false;
23821 cp_token *decl_spec_token_start;
23823 /* This function is only used when processing a template
23824 declaration. */
23825 gcc_assert (innermost_scope_kind () == sk_template_parms
23826 || innermost_scope_kind () == sk_template_spec);
23828 /* Defer access checks until we know what is being declared. */
23829 push_deferring_access_checks (dk_deferred);
23831 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23832 alternative. */
23833 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23834 cp_parser_decl_specifier_seq (parser,
23835 CP_PARSER_FLAGS_OPTIONAL,
23836 &decl_specifiers,
23837 &declares_class_or_enum);
23838 if (friend_p)
23839 *friend_p = cp_parser_friend_p (&decl_specifiers);
23841 /* There are no template typedefs. */
23842 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23844 error_at (decl_spec_token_start->location,
23845 "template declaration of %<typedef%>");
23846 decl = error_mark_node;
23849 /* Gather up the access checks that occurred the
23850 decl-specifier-seq. */
23851 stop_deferring_access_checks ();
23853 /* Check for the declaration of a template class. */
23854 if (declares_class_or_enum)
23856 if (cp_parser_declares_only_class_p (parser))
23858 decl = shadow_tag (&decl_specifiers);
23860 /* In this case:
23862 struct C {
23863 friend template <typename T> struct A<T>::B;
23866 A<T>::B will be represented by a TYPENAME_TYPE, and
23867 therefore not recognized by shadow_tag. */
23868 if (friend_p && *friend_p
23869 && !decl
23870 && decl_specifiers.type
23871 && TYPE_P (decl_specifiers.type))
23872 decl = decl_specifiers.type;
23874 if (decl && decl != error_mark_node)
23875 decl = TYPE_NAME (decl);
23876 else
23877 decl = error_mark_node;
23879 /* Perform access checks for template parameters. */
23880 cp_parser_perform_template_parameter_access_checks (checks);
23884 /* Complain about missing 'typename' or other invalid type names. */
23885 if (!decl_specifiers.any_type_specifiers_p
23886 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23888 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23889 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23890 the rest of this declaration. */
23891 decl = error_mark_node;
23892 goto out;
23895 /* If it's not a template class, try for a template function. If
23896 the next token is a `;', then this declaration does not declare
23897 anything. But, if there were errors in the decl-specifiers, then
23898 the error might well have come from an attempted class-specifier.
23899 In that case, there's no need to warn about a missing declarator. */
23900 if (!decl
23901 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23902 || decl_specifiers.type != error_mark_node))
23904 decl = cp_parser_init_declarator (parser,
23905 &decl_specifiers,
23906 checks,
23907 /*function_definition_allowed_p=*/true,
23908 member_p,
23909 declares_class_or_enum,
23910 &function_definition_p,
23911 NULL, NULL);
23913 /* 7.1.1-1 [dcl.stc]
23915 A storage-class-specifier shall not be specified in an explicit
23916 specialization... */
23917 if (decl
23918 && explicit_specialization_p
23919 && decl_specifiers.storage_class != sc_none)
23921 error_at (decl_spec_token_start->location,
23922 "explicit template specialization cannot have a storage class");
23923 decl = error_mark_node;
23926 if (decl && VAR_P (decl))
23927 check_template_variable (decl);
23930 /* Look for a trailing `;' after the declaration. */
23931 if (!function_definition_p
23932 && (decl == error_mark_node
23933 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23934 cp_parser_skip_to_end_of_block_or_statement (parser);
23936 out:
23937 pop_deferring_access_checks ();
23939 /* Clear any current qualification; whatever comes next is the start
23940 of something new. */
23941 parser->scope = NULL_TREE;
23942 parser->qualifying_scope = NULL_TREE;
23943 parser->object_scope = NULL_TREE;
23945 return decl;
23948 /* Parse a cast-expression that is not the operand of a unary "&". */
23950 static tree
23951 cp_parser_simple_cast_expression (cp_parser *parser)
23953 return cp_parser_cast_expression (parser, /*address_p=*/false,
23954 /*cast_p=*/false, /*decltype*/false, NULL);
23957 /* Parse a functional cast to TYPE. Returns an expression
23958 representing the cast. */
23960 static tree
23961 cp_parser_functional_cast (cp_parser* parser, tree type)
23963 vec<tree, va_gc> *vec;
23964 tree expression_list;
23965 tree cast;
23966 bool nonconst_p;
23968 if (!type)
23969 type = error_mark_node;
23971 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23973 cp_lexer_set_source_position (parser->lexer);
23974 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23975 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23976 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23977 if (TREE_CODE (type) == TYPE_DECL)
23978 type = TREE_TYPE (type);
23979 return finish_compound_literal (type, expression_list,
23980 tf_warning_or_error);
23984 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23985 /*cast_p=*/true,
23986 /*allow_expansion_p=*/true,
23987 /*non_constant_p=*/NULL);
23988 if (vec == NULL)
23989 expression_list = error_mark_node;
23990 else
23992 expression_list = build_tree_list_vec (vec);
23993 release_tree_vector (vec);
23996 cast = build_functional_cast (type, expression_list,
23997 tf_warning_or_error);
23998 /* [expr.const]/1: In an integral constant expression "only type
23999 conversions to integral or enumeration type can be used". */
24000 if (TREE_CODE (type) == TYPE_DECL)
24001 type = TREE_TYPE (type);
24002 if (cast != error_mark_node
24003 && !cast_valid_in_integral_constant_expression_p (type)
24004 && cp_parser_non_integral_constant_expression (parser,
24005 NIC_CONSTRUCTOR))
24006 return error_mark_node;
24007 return cast;
24010 /* Save the tokens that make up the body of a member function defined
24011 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
24012 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
24013 specifiers applied to the declaration. Returns the FUNCTION_DECL
24014 for the member function. */
24016 static tree
24017 cp_parser_save_member_function_body (cp_parser* parser,
24018 cp_decl_specifier_seq *decl_specifiers,
24019 cp_declarator *declarator,
24020 tree attributes)
24022 cp_token *first;
24023 cp_token *last;
24024 tree fn;
24026 /* Create the FUNCTION_DECL. */
24027 fn = grokmethod (decl_specifiers, declarator, attributes);
24028 cp_finalize_omp_declare_simd (parser, fn);
24029 /* If something went badly wrong, bail out now. */
24030 if (fn == error_mark_node)
24032 /* If there's a function-body, skip it. */
24033 if (cp_parser_token_starts_function_definition_p
24034 (cp_lexer_peek_token (parser->lexer)))
24035 cp_parser_skip_to_end_of_block_or_statement (parser);
24036 return error_mark_node;
24039 /* Remember it, if there default args to post process. */
24040 cp_parser_save_default_args (parser, fn);
24042 /* Save away the tokens that make up the body of the
24043 function. */
24044 first = parser->lexer->next_token;
24045 /* Handle function try blocks. */
24046 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
24047 cp_lexer_consume_token (parser->lexer);
24048 /* We can have braced-init-list mem-initializers before the fn body. */
24049 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24051 cp_lexer_consume_token (parser->lexer);
24052 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
24054 /* cache_group will stop after an un-nested { } pair, too. */
24055 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
24056 break;
24058 /* variadic mem-inits have ... after the ')'. */
24059 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24060 cp_lexer_consume_token (parser->lexer);
24063 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24064 /* Handle function try blocks. */
24065 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
24066 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24067 last = parser->lexer->next_token;
24069 /* Save away the inline definition; we will process it when the
24070 class is complete. */
24071 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
24072 DECL_PENDING_INLINE_P (fn) = 1;
24074 /* We need to know that this was defined in the class, so that
24075 friend templates are handled correctly. */
24076 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
24078 /* Add FN to the queue of functions to be parsed later. */
24079 vec_safe_push (unparsed_funs_with_definitions, fn);
24081 return fn;
24084 /* Save the tokens that make up the in-class initializer for a non-static
24085 data member. Returns a DEFAULT_ARG. */
24087 static tree
24088 cp_parser_save_nsdmi (cp_parser* parser)
24090 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
24093 /* Parse a template-argument-list, as well as the trailing ">" (but
24094 not the opening "<"). See cp_parser_template_argument_list for the
24095 return value. */
24097 static tree
24098 cp_parser_enclosed_template_argument_list (cp_parser* parser)
24100 tree arguments;
24101 tree saved_scope;
24102 tree saved_qualifying_scope;
24103 tree saved_object_scope;
24104 bool saved_greater_than_is_operator_p;
24105 int saved_unevaluated_operand;
24106 int saved_inhibit_evaluation_warnings;
24108 /* [temp.names]
24110 When parsing a template-id, the first non-nested `>' is taken as
24111 the end of the template-argument-list rather than a greater-than
24112 operator. */
24113 saved_greater_than_is_operator_p
24114 = parser->greater_than_is_operator_p;
24115 parser->greater_than_is_operator_p = false;
24116 /* Parsing the argument list may modify SCOPE, so we save it
24117 here. */
24118 saved_scope = parser->scope;
24119 saved_qualifying_scope = parser->qualifying_scope;
24120 saved_object_scope = parser->object_scope;
24121 /* We need to evaluate the template arguments, even though this
24122 template-id may be nested within a "sizeof". */
24123 saved_unevaluated_operand = cp_unevaluated_operand;
24124 cp_unevaluated_operand = 0;
24125 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24126 c_inhibit_evaluation_warnings = 0;
24127 /* Parse the template-argument-list itself. */
24128 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
24129 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24130 arguments = NULL_TREE;
24131 else
24132 arguments = cp_parser_template_argument_list (parser);
24133 /* Look for the `>' that ends the template-argument-list. If we find
24134 a '>>' instead, it's probably just a typo. */
24135 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24137 if (cxx_dialect != cxx98)
24139 /* In C++0x, a `>>' in a template argument list or cast
24140 expression is considered to be two separate `>'
24141 tokens. So, change the current token to a `>', but don't
24142 consume it: it will be consumed later when the outer
24143 template argument list (or cast expression) is parsed.
24144 Note that this replacement of `>' for `>>' is necessary
24145 even if we are parsing tentatively: in the tentative
24146 case, after calling
24147 cp_parser_enclosed_template_argument_list we will always
24148 throw away all of the template arguments and the first
24149 closing `>', either because the template argument list
24150 was erroneous or because we are replacing those tokens
24151 with a CPP_TEMPLATE_ID token. The second `>' (which will
24152 not have been thrown away) is needed either to close an
24153 outer template argument list or to complete a new-style
24154 cast. */
24155 cp_token *token = cp_lexer_peek_token (parser->lexer);
24156 token->type = CPP_GREATER;
24158 else if (!saved_greater_than_is_operator_p)
24160 /* If we're in a nested template argument list, the '>>' has
24161 to be a typo for '> >'. We emit the error message, but we
24162 continue parsing and we push a '>' as next token, so that
24163 the argument list will be parsed correctly. Note that the
24164 global source location is still on the token before the
24165 '>>', so we need to say explicitly where we want it. */
24166 cp_token *token = cp_lexer_peek_token (parser->lexer);
24167 error_at (token->location, "%<>>%> should be %<> >%> "
24168 "within a nested template argument list");
24170 token->type = CPP_GREATER;
24172 else
24174 /* If this is not a nested template argument list, the '>>'
24175 is a typo for '>'. Emit an error message and continue.
24176 Same deal about the token location, but here we can get it
24177 right by consuming the '>>' before issuing the diagnostic. */
24178 cp_token *token = cp_lexer_consume_token (parser->lexer);
24179 error_at (token->location,
24180 "spurious %<>>%>, use %<>%> to terminate "
24181 "a template argument list");
24184 else
24185 cp_parser_skip_to_end_of_template_parameter_list (parser);
24186 /* The `>' token might be a greater-than operator again now. */
24187 parser->greater_than_is_operator_p
24188 = saved_greater_than_is_operator_p;
24189 /* Restore the SAVED_SCOPE. */
24190 parser->scope = saved_scope;
24191 parser->qualifying_scope = saved_qualifying_scope;
24192 parser->object_scope = saved_object_scope;
24193 cp_unevaluated_operand = saved_unevaluated_operand;
24194 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24196 return arguments;
24199 /* MEMBER_FUNCTION is a member function, or a friend. If default
24200 arguments, or the body of the function have not yet been parsed,
24201 parse them now. */
24203 static void
24204 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24206 timevar_push (TV_PARSE_INMETH);
24207 /* If this member is a template, get the underlying
24208 FUNCTION_DECL. */
24209 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24210 member_function = DECL_TEMPLATE_RESULT (member_function);
24212 /* There should not be any class definitions in progress at this
24213 point; the bodies of members are only parsed outside of all class
24214 definitions. */
24215 gcc_assert (parser->num_classes_being_defined == 0);
24216 /* While we're parsing the member functions we might encounter more
24217 classes. We want to handle them right away, but we don't want
24218 them getting mixed up with functions that are currently in the
24219 queue. */
24220 push_unparsed_function_queues (parser);
24222 /* Make sure that any template parameters are in scope. */
24223 maybe_begin_member_template_processing (member_function);
24225 /* If the body of the function has not yet been parsed, parse it
24226 now. */
24227 if (DECL_PENDING_INLINE_P (member_function))
24229 tree function_scope;
24230 cp_token_cache *tokens;
24232 /* The function is no longer pending; we are processing it. */
24233 tokens = DECL_PENDING_INLINE_INFO (member_function);
24234 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24235 DECL_PENDING_INLINE_P (member_function) = 0;
24237 /* If this is a local class, enter the scope of the containing
24238 function. */
24239 function_scope = current_function_decl;
24240 if (function_scope)
24241 push_function_context ();
24243 /* Push the body of the function onto the lexer stack. */
24244 cp_parser_push_lexer_for_tokens (parser, tokens);
24246 /* Let the front end know that we going to be defining this
24247 function. */
24248 start_preparsed_function (member_function, NULL_TREE,
24249 SF_PRE_PARSED | SF_INCLASS_INLINE);
24251 /* Don't do access checking if it is a templated function. */
24252 if (processing_template_decl)
24253 push_deferring_access_checks (dk_no_check);
24255 /* #pragma omp declare reduction needs special parsing. */
24256 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24258 parser->lexer->in_pragma = true;
24259 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24260 finish_function (/*inline*/2);
24261 cp_check_omp_declare_reduction (member_function);
24263 else
24264 /* Now, parse the body of the function. */
24265 cp_parser_function_definition_after_declarator (parser,
24266 /*inline_p=*/true);
24268 if (processing_template_decl)
24269 pop_deferring_access_checks ();
24271 /* Leave the scope of the containing function. */
24272 if (function_scope)
24273 pop_function_context ();
24274 cp_parser_pop_lexer (parser);
24277 /* Remove any template parameters from the symbol table. */
24278 maybe_end_member_template_processing ();
24280 /* Restore the queue. */
24281 pop_unparsed_function_queues (parser);
24282 timevar_pop (TV_PARSE_INMETH);
24285 /* If DECL contains any default args, remember it on the unparsed
24286 functions queue. */
24288 static void
24289 cp_parser_save_default_args (cp_parser* parser, tree decl)
24291 tree probe;
24293 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24294 probe;
24295 probe = TREE_CHAIN (probe))
24296 if (TREE_PURPOSE (probe))
24298 cp_default_arg_entry entry = {current_class_type, decl};
24299 vec_safe_push (unparsed_funs_with_default_args, entry);
24300 break;
24304 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24305 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24306 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24307 from the parameter-type-list. */
24309 static tree
24310 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24311 tree default_arg, tree parmtype)
24313 cp_token_cache *tokens;
24314 tree parsed_arg;
24315 bool dummy;
24317 if (default_arg == error_mark_node)
24318 return error_mark_node;
24320 /* Push the saved tokens for the default argument onto the parser's
24321 lexer stack. */
24322 tokens = DEFARG_TOKENS (default_arg);
24323 cp_parser_push_lexer_for_tokens (parser, tokens);
24325 start_lambda_scope (decl);
24327 /* Parse the default argument. */
24328 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24329 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24330 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24332 finish_lambda_scope ();
24334 if (parsed_arg == error_mark_node)
24335 cp_parser_skip_to_end_of_statement (parser);
24337 if (!processing_template_decl)
24339 /* In a non-template class, check conversions now. In a template,
24340 we'll wait and instantiate these as needed. */
24341 if (TREE_CODE (decl) == PARM_DECL)
24342 parsed_arg = check_default_argument (parmtype, parsed_arg,
24343 tf_warning_or_error);
24344 else
24345 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24348 /* If the token stream has not been completely used up, then
24349 there was extra junk after the end of the default
24350 argument. */
24351 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24353 if (TREE_CODE (decl) == PARM_DECL)
24354 cp_parser_error (parser, "expected %<,%>");
24355 else
24356 cp_parser_error (parser, "expected %<;%>");
24359 /* Revert to the main lexer. */
24360 cp_parser_pop_lexer (parser);
24362 return parsed_arg;
24365 /* FIELD is a non-static data member with an initializer which we saved for
24366 later; parse it now. */
24368 static void
24369 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24371 tree def;
24373 maybe_begin_member_template_processing (field);
24375 push_unparsed_function_queues (parser);
24376 def = cp_parser_late_parse_one_default_arg (parser, field,
24377 DECL_INITIAL (field),
24378 NULL_TREE);
24379 pop_unparsed_function_queues (parser);
24381 maybe_end_member_template_processing ();
24383 DECL_INITIAL (field) = def;
24386 /* FN is a FUNCTION_DECL which may contains a parameter with an
24387 unparsed DEFAULT_ARG. Parse the default args now. This function
24388 assumes that the current scope is the scope in which the default
24389 argument should be processed. */
24391 static void
24392 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24394 bool saved_local_variables_forbidden_p;
24395 tree parm, parmdecl;
24397 /* While we're parsing the default args, we might (due to the
24398 statement expression extension) encounter more classes. We want
24399 to handle them right away, but we don't want them getting mixed
24400 up with default args that are currently in the queue. */
24401 push_unparsed_function_queues (parser);
24403 /* Local variable names (and the `this' keyword) may not appear
24404 in a default argument. */
24405 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24406 parser->local_variables_forbidden_p = true;
24408 push_defarg_context (fn);
24410 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24411 parmdecl = DECL_ARGUMENTS (fn);
24412 parm && parm != void_list_node;
24413 parm = TREE_CHAIN (parm),
24414 parmdecl = DECL_CHAIN (parmdecl))
24416 tree default_arg = TREE_PURPOSE (parm);
24417 tree parsed_arg;
24418 vec<tree, va_gc> *insts;
24419 tree copy;
24420 unsigned ix;
24422 if (!default_arg)
24423 continue;
24425 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24426 /* This can happen for a friend declaration for a function
24427 already declared with default arguments. */
24428 continue;
24430 parsed_arg
24431 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24432 default_arg,
24433 TREE_VALUE (parm));
24434 if (parsed_arg == error_mark_node)
24436 continue;
24439 TREE_PURPOSE (parm) = parsed_arg;
24441 /* Update any instantiations we've already created. */
24442 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24443 vec_safe_iterate (insts, ix, &copy); ix++)
24444 TREE_PURPOSE (copy) = parsed_arg;
24447 pop_defarg_context ();
24449 /* Make sure no default arg is missing. */
24450 check_default_args (fn);
24452 /* Restore the state of local_variables_forbidden_p. */
24453 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24455 /* Restore the queue. */
24456 pop_unparsed_function_queues (parser);
24459 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24461 sizeof ... ( identifier )
24463 where the 'sizeof' token has already been consumed. */
24465 static tree
24466 cp_parser_sizeof_pack (cp_parser *parser)
24468 /* Consume the `...'. */
24469 cp_lexer_consume_token (parser->lexer);
24470 maybe_warn_variadic_templates ();
24472 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24473 if (paren)
24474 cp_lexer_consume_token (parser->lexer);
24475 else
24476 permerror (cp_lexer_peek_token (parser->lexer)->location,
24477 "%<sizeof...%> argument must be surrounded by parentheses");
24479 cp_token *token = cp_lexer_peek_token (parser->lexer);
24480 tree name = cp_parser_identifier (parser);
24481 if (name == error_mark_node)
24482 return error_mark_node;
24483 /* The name is not qualified. */
24484 parser->scope = NULL_TREE;
24485 parser->qualifying_scope = NULL_TREE;
24486 parser->object_scope = NULL_TREE;
24487 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24488 if (expr == error_mark_node)
24489 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24490 token->location);
24491 if (TREE_CODE (expr) == TYPE_DECL)
24492 expr = TREE_TYPE (expr);
24493 else if (TREE_CODE (expr) == CONST_DECL)
24494 expr = DECL_INITIAL (expr);
24495 expr = make_pack_expansion (expr);
24497 if (paren)
24498 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24500 return expr;
24503 /* Parse the operand of `sizeof' (or a similar operator). Returns
24504 either a TYPE or an expression, depending on the form of the
24505 input. The KEYWORD indicates which kind of expression we have
24506 encountered. */
24508 static tree
24509 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24511 tree expr = NULL_TREE;
24512 const char *saved_message;
24513 char *tmp;
24514 bool saved_integral_constant_expression_p;
24515 bool saved_non_integral_constant_expression_p;
24517 /* If it's a `...', then we are computing the length of a parameter
24518 pack. */
24519 if (keyword == RID_SIZEOF
24520 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24521 return cp_parser_sizeof_pack (parser);
24523 /* Types cannot be defined in a `sizeof' expression. Save away the
24524 old message. */
24525 saved_message = parser->type_definition_forbidden_message;
24526 /* And create the new one. */
24527 tmp = concat ("types may not be defined in %<",
24528 IDENTIFIER_POINTER (ridpointers[keyword]),
24529 "%> expressions", NULL);
24530 parser->type_definition_forbidden_message = tmp;
24532 /* The restrictions on constant-expressions do not apply inside
24533 sizeof expressions. */
24534 saved_integral_constant_expression_p
24535 = parser->integral_constant_expression_p;
24536 saved_non_integral_constant_expression_p
24537 = parser->non_integral_constant_expression_p;
24538 parser->integral_constant_expression_p = false;
24540 /* Do not actually evaluate the expression. */
24541 ++cp_unevaluated_operand;
24542 ++c_inhibit_evaluation_warnings;
24543 /* If it's a `(', then we might be looking at the type-id
24544 construction. */
24545 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24547 tree type = NULL_TREE;
24549 /* We can't be sure yet whether we're looking at a type-id or an
24550 expression. */
24551 cp_parser_parse_tentatively (parser);
24552 /* Note: as a GNU Extension, compound literals are considered
24553 postfix-expressions as they are in C99, so they are valid
24554 arguments to sizeof. See comment in cp_parser_cast_expression
24555 for details. */
24556 if (cp_parser_compound_literal_p (parser))
24557 cp_parser_simulate_error (parser);
24558 else
24560 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24561 parser->in_type_id_in_expr_p = true;
24562 /* Look for the type-id. */
24563 type = cp_parser_type_id (parser);
24564 /* Look for the closing `)'. */
24565 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24566 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24569 /* If all went well, then we're done. */
24570 if (cp_parser_parse_definitely (parser))
24572 cp_decl_specifier_seq decl_specs;
24574 /* Build a trivial decl-specifier-seq. */
24575 clear_decl_specs (&decl_specs);
24576 decl_specs.type = type;
24578 /* Call grokdeclarator to figure out what type this is. */
24579 expr = grokdeclarator (NULL,
24580 &decl_specs,
24581 TYPENAME,
24582 /*initialized=*/0,
24583 /*attrlist=*/NULL);
24587 /* If the type-id production did not work out, then we must be
24588 looking at the unary-expression production. */
24589 if (!expr)
24590 expr = cp_parser_unary_expression (parser);
24592 /* Go back to evaluating expressions. */
24593 --cp_unevaluated_operand;
24594 --c_inhibit_evaluation_warnings;
24596 /* Free the message we created. */
24597 free (tmp);
24598 /* And restore the old one. */
24599 parser->type_definition_forbidden_message = saved_message;
24600 parser->integral_constant_expression_p
24601 = saved_integral_constant_expression_p;
24602 parser->non_integral_constant_expression_p
24603 = saved_non_integral_constant_expression_p;
24605 return expr;
24608 /* If the current declaration has no declarator, return true. */
24610 static bool
24611 cp_parser_declares_only_class_p (cp_parser *parser)
24613 /* If the next token is a `;' or a `,' then there is no
24614 declarator. */
24615 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24616 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24619 /* Update the DECL_SPECS to reflect the storage class indicated by
24620 KEYWORD. */
24622 static void
24623 cp_parser_set_storage_class (cp_parser *parser,
24624 cp_decl_specifier_seq *decl_specs,
24625 enum rid keyword,
24626 cp_token *token)
24628 cp_storage_class storage_class;
24630 if (parser->in_unbraced_linkage_specification_p)
24632 error_at (token->location, "invalid use of %qD in linkage specification",
24633 ridpointers[keyword]);
24634 return;
24636 else if (decl_specs->storage_class != sc_none)
24638 decl_specs->conflicting_specifiers_p = true;
24639 return;
24642 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24643 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24644 && decl_specs->gnu_thread_keyword_p)
24646 pedwarn (decl_specs->locations[ds_thread], 0,
24647 "%<__thread%> before %qD", ridpointers[keyword]);
24650 switch (keyword)
24652 case RID_AUTO:
24653 storage_class = sc_auto;
24654 break;
24655 case RID_REGISTER:
24656 storage_class = sc_register;
24657 break;
24658 case RID_STATIC:
24659 storage_class = sc_static;
24660 break;
24661 case RID_EXTERN:
24662 storage_class = sc_extern;
24663 break;
24664 case RID_MUTABLE:
24665 storage_class = sc_mutable;
24666 break;
24667 default:
24668 gcc_unreachable ();
24670 decl_specs->storage_class = storage_class;
24671 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24673 /* A storage class specifier cannot be applied alongside a typedef
24674 specifier. If there is a typedef specifier present then set
24675 conflicting_specifiers_p which will trigger an error later
24676 on in grokdeclarator. */
24677 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24678 decl_specs->conflicting_specifiers_p = true;
24681 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24682 is true, the type is a class or enum definition. */
24684 static void
24685 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24686 tree type_spec,
24687 cp_token *token,
24688 bool type_definition_p)
24690 decl_specs->any_specifiers_p = true;
24692 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24693 (with, for example, in "typedef int wchar_t;") we remember that
24694 this is what happened. In system headers, we ignore these
24695 declarations so that G++ can work with system headers that are not
24696 C++-safe. */
24697 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24698 && !type_definition_p
24699 && (type_spec == boolean_type_node
24700 || type_spec == char16_type_node
24701 || type_spec == char32_type_node
24702 || type_spec == wchar_type_node)
24703 && (decl_specs->type
24704 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24705 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24706 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24707 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24709 decl_specs->redefined_builtin_type = type_spec;
24710 set_and_check_decl_spec_loc (decl_specs,
24711 ds_redefined_builtin_type_spec,
24712 token);
24713 if (!decl_specs->type)
24715 decl_specs->type = type_spec;
24716 decl_specs->type_definition_p = false;
24717 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24720 else if (decl_specs->type)
24721 decl_specs->multiple_types_p = true;
24722 else
24724 decl_specs->type = type_spec;
24725 decl_specs->type_definition_p = type_definition_p;
24726 decl_specs->redefined_builtin_type = NULL_TREE;
24727 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24731 /* True iff TOKEN is the GNU keyword __thread. */
24733 static bool
24734 token_is__thread (cp_token *token)
24736 gcc_assert (token->keyword == RID_THREAD);
24737 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24740 /* Set the location for a declarator specifier and check if it is
24741 duplicated.
24743 DECL_SPECS is the sequence of declarator specifiers onto which to
24744 set the location.
24746 DS is the single declarator specifier to set which location is to
24747 be set onto the existing sequence of declarators.
24749 LOCATION is the location for the declarator specifier to
24750 consider. */
24752 static void
24753 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24754 cp_decl_spec ds, cp_token *token)
24756 gcc_assert (ds < ds_last);
24758 if (decl_specs == NULL)
24759 return;
24761 source_location location = token->location;
24763 if (decl_specs->locations[ds] == 0)
24765 decl_specs->locations[ds] = location;
24766 if (ds == ds_thread)
24767 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24769 else
24771 if (ds == ds_long)
24773 if (decl_specs->locations[ds_long_long] != 0)
24774 error_at (location,
24775 "%<long long long%> is too long for GCC");
24776 else
24778 decl_specs->locations[ds_long_long] = location;
24779 pedwarn_cxx98 (location,
24780 OPT_Wlong_long,
24781 "ISO C++ 1998 does not support %<long long%>");
24784 else if (ds == ds_thread)
24786 bool gnu = token_is__thread (token);
24787 if (gnu != decl_specs->gnu_thread_keyword_p)
24788 error_at (location,
24789 "both %<__thread%> and %<thread_local%> specified");
24790 else
24791 error_at (location, "duplicate %qD", token->u.value);
24793 else
24795 static const char *const decl_spec_names[] = {
24796 "signed",
24797 "unsigned",
24798 "short",
24799 "long",
24800 "const",
24801 "volatile",
24802 "restrict",
24803 "inline",
24804 "virtual",
24805 "explicit",
24806 "friend",
24807 "typedef",
24808 "using",
24809 "constexpr",
24810 "__complex"
24812 error_at (location,
24813 "duplicate %qs", decl_spec_names[ds]);
24818 /* Return true iff the declarator specifier DS is present in the
24819 sequence of declarator specifiers DECL_SPECS. */
24821 bool
24822 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24823 cp_decl_spec ds)
24825 gcc_assert (ds < ds_last);
24827 if (decl_specs == NULL)
24828 return false;
24830 return decl_specs->locations[ds] != 0;
24833 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24834 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24836 static bool
24837 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24839 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24842 /* Issue an error message indicating that TOKEN_DESC was expected.
24843 If KEYWORD is true, it indicated this function is called by
24844 cp_parser_require_keword and the required token can only be
24845 a indicated keyword. */
24847 static void
24848 cp_parser_required_error (cp_parser *parser,
24849 required_token token_desc,
24850 bool keyword)
24852 switch (token_desc)
24854 case RT_NEW:
24855 cp_parser_error (parser, "expected %<new%>");
24856 return;
24857 case RT_DELETE:
24858 cp_parser_error (parser, "expected %<delete%>");
24859 return;
24860 case RT_RETURN:
24861 cp_parser_error (parser, "expected %<return%>");
24862 return;
24863 case RT_WHILE:
24864 cp_parser_error (parser, "expected %<while%>");
24865 return;
24866 case RT_EXTERN:
24867 cp_parser_error (parser, "expected %<extern%>");
24868 return;
24869 case RT_STATIC_ASSERT:
24870 cp_parser_error (parser, "expected %<static_assert%>");
24871 return;
24872 case RT_DECLTYPE:
24873 cp_parser_error (parser, "expected %<decltype%>");
24874 return;
24875 case RT_OPERATOR:
24876 cp_parser_error (parser, "expected %<operator%>");
24877 return;
24878 case RT_CLASS:
24879 cp_parser_error (parser, "expected %<class%>");
24880 return;
24881 case RT_TEMPLATE:
24882 cp_parser_error (parser, "expected %<template%>");
24883 return;
24884 case RT_NAMESPACE:
24885 cp_parser_error (parser, "expected %<namespace%>");
24886 return;
24887 case RT_USING:
24888 cp_parser_error (parser, "expected %<using%>");
24889 return;
24890 case RT_ASM:
24891 cp_parser_error (parser, "expected %<asm%>");
24892 return;
24893 case RT_TRY:
24894 cp_parser_error (parser, "expected %<try%>");
24895 return;
24896 case RT_CATCH:
24897 cp_parser_error (parser, "expected %<catch%>");
24898 return;
24899 case RT_THROW:
24900 cp_parser_error (parser, "expected %<throw%>");
24901 return;
24902 case RT_LABEL:
24903 cp_parser_error (parser, "expected %<__label__%>");
24904 return;
24905 case RT_AT_TRY:
24906 cp_parser_error (parser, "expected %<@try%>");
24907 return;
24908 case RT_AT_SYNCHRONIZED:
24909 cp_parser_error (parser, "expected %<@synchronized%>");
24910 return;
24911 case RT_AT_THROW:
24912 cp_parser_error (parser, "expected %<@throw%>");
24913 return;
24914 case RT_TRANSACTION_ATOMIC:
24915 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24916 return;
24917 case RT_TRANSACTION_RELAXED:
24918 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24919 return;
24920 default:
24921 break;
24923 if (!keyword)
24925 switch (token_desc)
24927 case RT_SEMICOLON:
24928 cp_parser_error (parser, "expected %<;%>");
24929 return;
24930 case RT_OPEN_PAREN:
24931 cp_parser_error (parser, "expected %<(%>");
24932 return;
24933 case RT_CLOSE_BRACE:
24934 cp_parser_error (parser, "expected %<}%>");
24935 return;
24936 case RT_OPEN_BRACE:
24937 cp_parser_error (parser, "expected %<{%>");
24938 return;
24939 case RT_CLOSE_SQUARE:
24940 cp_parser_error (parser, "expected %<]%>");
24941 return;
24942 case RT_OPEN_SQUARE:
24943 cp_parser_error (parser, "expected %<[%>");
24944 return;
24945 case RT_COMMA:
24946 cp_parser_error (parser, "expected %<,%>");
24947 return;
24948 case RT_SCOPE:
24949 cp_parser_error (parser, "expected %<::%>");
24950 return;
24951 case RT_LESS:
24952 cp_parser_error (parser, "expected %<<%>");
24953 return;
24954 case RT_GREATER:
24955 cp_parser_error (parser, "expected %<>%>");
24956 return;
24957 case RT_EQ:
24958 cp_parser_error (parser, "expected %<=%>");
24959 return;
24960 case RT_ELLIPSIS:
24961 cp_parser_error (parser, "expected %<...%>");
24962 return;
24963 case RT_MULT:
24964 cp_parser_error (parser, "expected %<*%>");
24965 return;
24966 case RT_COMPL:
24967 cp_parser_error (parser, "expected %<~%>");
24968 return;
24969 case RT_COLON:
24970 cp_parser_error (parser, "expected %<:%>");
24971 return;
24972 case RT_COLON_SCOPE:
24973 cp_parser_error (parser, "expected %<:%> or %<::%>");
24974 return;
24975 case RT_CLOSE_PAREN:
24976 cp_parser_error (parser, "expected %<)%>");
24977 return;
24978 case RT_COMMA_CLOSE_PAREN:
24979 cp_parser_error (parser, "expected %<,%> or %<)%>");
24980 return;
24981 case RT_PRAGMA_EOL:
24982 cp_parser_error (parser, "expected end of line");
24983 return;
24984 case RT_NAME:
24985 cp_parser_error (parser, "expected identifier");
24986 return;
24987 case RT_SELECT:
24988 cp_parser_error (parser, "expected selection-statement");
24989 return;
24990 case RT_INTERATION:
24991 cp_parser_error (parser, "expected iteration-statement");
24992 return;
24993 case RT_JUMP:
24994 cp_parser_error (parser, "expected jump-statement");
24995 return;
24996 case RT_CLASS_KEY:
24997 cp_parser_error (parser, "expected class-key");
24998 return;
24999 case RT_CLASS_TYPENAME_TEMPLATE:
25000 cp_parser_error (parser,
25001 "expected %<class%>, %<typename%>, or %<template%>");
25002 return;
25003 default:
25004 gcc_unreachable ();
25007 else
25008 gcc_unreachable ();
25013 /* If the next token is of the indicated TYPE, consume it. Otherwise,
25014 issue an error message indicating that TOKEN_DESC was expected.
25016 Returns the token consumed, if the token had the appropriate type.
25017 Otherwise, returns NULL. */
25019 static cp_token *
25020 cp_parser_require (cp_parser* parser,
25021 enum cpp_ttype type,
25022 required_token token_desc)
25024 if (cp_lexer_next_token_is (parser->lexer, type))
25025 return cp_lexer_consume_token (parser->lexer);
25026 else
25028 /* Output the MESSAGE -- unless we're parsing tentatively. */
25029 if (!cp_parser_simulate_error (parser))
25030 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
25031 return NULL;
25035 /* An error message is produced if the next token is not '>'.
25036 All further tokens are skipped until the desired token is
25037 found or '{', '}', ';' or an unbalanced ')' or ']'. */
25039 static void
25040 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
25042 /* Current level of '< ... >'. */
25043 unsigned level = 0;
25044 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
25045 unsigned nesting_depth = 0;
25047 /* Are we ready, yet? If not, issue error message. */
25048 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
25049 return;
25051 /* Skip tokens until the desired token is found. */
25052 while (true)
25054 /* Peek at the next token. */
25055 switch (cp_lexer_peek_token (parser->lexer)->type)
25057 case CPP_LESS:
25058 if (!nesting_depth)
25059 ++level;
25060 break;
25062 case CPP_RSHIFT:
25063 if (cxx_dialect == cxx98)
25064 /* C++0x views the `>>' operator as two `>' tokens, but
25065 C++98 does not. */
25066 break;
25067 else if (!nesting_depth && level-- == 0)
25069 /* We've hit a `>>' where the first `>' closes the
25070 template argument list, and the second `>' is
25071 spurious. Just consume the `>>' and stop; we've
25072 already produced at least one error. */
25073 cp_lexer_consume_token (parser->lexer);
25074 return;
25076 /* Fall through for C++0x, so we handle the second `>' in
25077 the `>>'. */
25079 case CPP_GREATER:
25080 if (!nesting_depth && level-- == 0)
25082 /* We've reached the token we want, consume it and stop. */
25083 cp_lexer_consume_token (parser->lexer);
25084 return;
25086 break;
25088 case CPP_OPEN_PAREN:
25089 case CPP_OPEN_SQUARE:
25090 ++nesting_depth;
25091 break;
25093 case CPP_CLOSE_PAREN:
25094 case CPP_CLOSE_SQUARE:
25095 if (nesting_depth-- == 0)
25096 return;
25097 break;
25099 case CPP_EOF:
25100 case CPP_PRAGMA_EOL:
25101 case CPP_SEMICOLON:
25102 case CPP_OPEN_BRACE:
25103 case CPP_CLOSE_BRACE:
25104 /* The '>' was probably forgotten, don't look further. */
25105 return;
25107 default:
25108 break;
25111 /* Consume this token. */
25112 cp_lexer_consume_token (parser->lexer);
25116 /* If the next token is the indicated keyword, consume it. Otherwise,
25117 issue an error message indicating that TOKEN_DESC was expected.
25119 Returns the token consumed, if the token had the appropriate type.
25120 Otherwise, returns NULL. */
25122 static cp_token *
25123 cp_parser_require_keyword (cp_parser* parser,
25124 enum rid keyword,
25125 required_token token_desc)
25127 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
25129 if (token && token->keyword != keyword)
25131 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
25132 return NULL;
25135 return token;
25138 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25139 function-definition. */
25141 static bool
25142 cp_parser_token_starts_function_definition_p (cp_token* token)
25144 return (/* An ordinary function-body begins with an `{'. */
25145 token->type == CPP_OPEN_BRACE
25146 /* A ctor-initializer begins with a `:'. */
25147 || token->type == CPP_COLON
25148 /* A function-try-block begins with `try'. */
25149 || token->keyword == RID_TRY
25150 /* A function-transaction-block begins with `__transaction_atomic'
25151 or `__transaction_relaxed'. */
25152 || token->keyword == RID_TRANSACTION_ATOMIC
25153 || token->keyword == RID_TRANSACTION_RELAXED
25154 /* The named return value extension begins with `return'. */
25155 || token->keyword == RID_RETURN);
25158 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25159 definition. */
25161 static bool
25162 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25164 cp_token *token;
25166 token = cp_lexer_peek_token (parser->lexer);
25167 return (token->type == CPP_OPEN_BRACE
25168 || (token->type == CPP_COLON
25169 && !parser->colon_doesnt_start_class_def_p));
25172 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25173 C++0x) ending a template-argument. */
25175 static bool
25176 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25178 cp_token *token;
25180 token = cp_lexer_peek_token (parser->lexer);
25181 return (token->type == CPP_COMMA
25182 || token->type == CPP_GREATER
25183 || token->type == CPP_ELLIPSIS
25184 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25187 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25188 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25190 static bool
25191 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25192 size_t n)
25194 cp_token *token;
25196 token = cp_lexer_peek_nth_token (parser->lexer, n);
25197 if (token->type == CPP_LESS)
25198 return true;
25199 /* Check for the sequence `<::' in the original code. It would be lexed as
25200 `[:', where `[' is a digraph, and there is no whitespace before
25201 `:'. */
25202 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25204 cp_token *token2;
25205 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25206 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25207 return true;
25209 return false;
25212 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25213 or none_type otherwise. */
25215 static enum tag_types
25216 cp_parser_token_is_class_key (cp_token* token)
25218 switch (token->keyword)
25220 case RID_CLASS:
25221 return class_type;
25222 case RID_STRUCT:
25223 return record_type;
25224 case RID_UNION:
25225 return union_type;
25227 default:
25228 return none_type;
25232 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25233 or none_type otherwise or if the token is null. */
25235 static enum tag_types
25236 cp_parser_token_is_type_parameter_key (cp_token* token)
25238 if (!token)
25239 return none_type;
25241 switch (token->keyword)
25243 case RID_CLASS:
25244 return class_type;
25245 case RID_TYPENAME:
25246 return typename_type;
25248 default:
25249 return none_type;
25253 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25255 static void
25256 cp_parser_check_class_key (enum tag_types class_key, tree type)
25258 if (type == error_mark_node)
25259 return;
25260 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25262 if (permerror (input_location, "%qs tag used in naming %q#T",
25263 class_key == union_type ? "union"
25264 : class_key == record_type ? "struct" : "class",
25265 type))
25266 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25267 "%q#T was previously declared here", type);
25271 /* Issue an error message if DECL is redeclared with different
25272 access than its original declaration [class.access.spec/3].
25273 This applies to nested classes and nested class templates.
25274 [class.mem/1]. */
25276 static void
25277 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25279 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25280 return;
25282 if ((TREE_PRIVATE (decl)
25283 != (current_access_specifier == access_private_node))
25284 || (TREE_PROTECTED (decl)
25285 != (current_access_specifier == access_protected_node)))
25286 error_at (location, "%qD redeclared with different access", decl);
25289 /* Look for the `template' keyword, as a syntactic disambiguator.
25290 Return TRUE iff it is present, in which case it will be
25291 consumed. */
25293 static bool
25294 cp_parser_optional_template_keyword (cp_parser *parser)
25296 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25298 /* In C++98 the `template' keyword can only be used within templates;
25299 outside templates the parser can always figure out what is a
25300 template and what is not. In C++11, per the resolution of DR 468,
25301 `template' is allowed in cases where it is not strictly necessary. */
25302 if (!processing_template_decl
25303 && pedantic && cxx_dialect == cxx98)
25305 cp_token *token = cp_lexer_peek_token (parser->lexer);
25306 pedwarn (token->location, OPT_Wpedantic,
25307 "in C++98 %<template%> (as a disambiguator) is only "
25308 "allowed within templates");
25309 /* If this part of the token stream is rescanned, the same
25310 error message would be generated. So, we purge the token
25311 from the stream. */
25312 cp_lexer_purge_token (parser->lexer);
25313 return false;
25315 else
25317 /* Consume the `template' keyword. */
25318 cp_lexer_consume_token (parser->lexer);
25319 return true;
25322 return false;
25325 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25326 set PARSER->SCOPE, and perform other related actions. */
25328 static void
25329 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25331 int i;
25332 struct tree_check *check_value;
25333 deferred_access_check *chk;
25334 vec<deferred_access_check, va_gc> *checks;
25336 /* Get the stored value. */
25337 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25338 /* Perform any access checks that were deferred. */
25339 checks = check_value->checks;
25340 if (checks)
25342 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25343 perform_or_defer_access_check (chk->binfo,
25344 chk->decl,
25345 chk->diag_decl, tf_warning_or_error);
25347 /* Set the scope from the stored value. */
25348 parser->scope = check_value->value;
25349 parser->qualifying_scope = check_value->qualifying_scope;
25350 parser->object_scope = NULL_TREE;
25353 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25354 encounter the end of a block before what we were looking for. */
25356 static bool
25357 cp_parser_cache_group (cp_parser *parser,
25358 enum cpp_ttype end,
25359 unsigned depth)
25361 while (true)
25363 cp_token *token = cp_lexer_peek_token (parser->lexer);
25365 /* Abort a parenthesized expression if we encounter a semicolon. */
25366 if ((end == CPP_CLOSE_PAREN || depth == 0)
25367 && token->type == CPP_SEMICOLON)
25368 return true;
25369 /* If we've reached the end of the file, stop. */
25370 if (token->type == CPP_EOF
25371 || (end != CPP_PRAGMA_EOL
25372 && token->type == CPP_PRAGMA_EOL))
25373 return true;
25374 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25375 /* We've hit the end of an enclosing block, so there's been some
25376 kind of syntax error. */
25377 return true;
25379 /* Consume the token. */
25380 cp_lexer_consume_token (parser->lexer);
25381 /* See if it starts a new group. */
25382 if (token->type == CPP_OPEN_BRACE)
25384 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25385 /* In theory this should probably check end == '}', but
25386 cp_parser_save_member_function_body needs it to exit
25387 after either '}' or ')' when called with ')'. */
25388 if (depth == 0)
25389 return false;
25391 else if (token->type == CPP_OPEN_PAREN)
25393 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25394 if (depth == 0 && end == CPP_CLOSE_PAREN)
25395 return false;
25397 else if (token->type == CPP_PRAGMA)
25398 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25399 else if (token->type == end)
25400 return false;
25404 /* Like above, for caching a default argument or NSDMI. Both of these are
25405 terminated by a non-nested comma, but it can be unclear whether or not a
25406 comma is nested in a template argument list unless we do more parsing.
25407 In order to handle this ambiguity, when we encounter a ',' after a '<'
25408 we try to parse what follows as a parameter-declaration-list (in the
25409 case of a default argument) or a member-declarator (in the case of an
25410 NSDMI). If that succeeds, then we stop caching. */
25412 static tree
25413 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25415 unsigned depth = 0;
25416 int maybe_template_id = 0;
25417 cp_token *first_token;
25418 cp_token *token;
25419 tree default_argument;
25421 /* Add tokens until we have processed the entire default
25422 argument. We add the range [first_token, token). */
25423 first_token = cp_lexer_peek_token (parser->lexer);
25424 if (first_token->type == CPP_OPEN_BRACE)
25426 /* For list-initialization, this is straightforward. */
25427 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25428 token = cp_lexer_peek_token (parser->lexer);
25430 else while (true)
25432 bool done = false;
25434 /* Peek at the next token. */
25435 token = cp_lexer_peek_token (parser->lexer);
25436 /* What we do depends on what token we have. */
25437 switch (token->type)
25439 /* In valid code, a default argument must be
25440 immediately followed by a `,' `)', or `...'. */
25441 case CPP_COMMA:
25442 if (depth == 0 && maybe_template_id)
25444 /* If we've seen a '<', we might be in a
25445 template-argument-list. Until Core issue 325 is
25446 resolved, we don't know how this situation ought
25447 to be handled, so try to DTRT. We check whether
25448 what comes after the comma is a valid parameter
25449 declaration list. If it is, then the comma ends
25450 the default argument; otherwise the default
25451 argument continues. */
25452 bool error = false;
25453 cp_token *peek;
25455 /* Set ITALP so cp_parser_parameter_declaration_list
25456 doesn't decide to commit to this parse. */
25457 bool saved_italp = parser->in_template_argument_list_p;
25458 parser->in_template_argument_list_p = true;
25460 cp_parser_parse_tentatively (parser);
25462 if (nsdmi)
25464 /* Parse declarators until we reach a non-comma or
25465 somthing that cannot be an initializer.
25466 Just checking whether we're looking at a single
25467 declarator is insufficient. Consider:
25468 int var = tuple<T,U>::x;
25469 The template parameter 'U' looks exactly like a
25470 declarator. */
25473 int ctor_dtor_or_conv_p;
25474 cp_lexer_consume_token (parser->lexer);
25475 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25476 &ctor_dtor_or_conv_p,
25477 /*parenthesized_p=*/NULL,
25478 /*member_p=*/true,
25479 /*friend_p=*/false);
25480 peek = cp_lexer_peek_token (parser->lexer);
25481 if (cp_parser_error_occurred (parser))
25482 break;
25484 while (peek->type == CPP_COMMA);
25485 /* If we met an '=' or ';' then the original comma
25486 was the end of the NSDMI. Otherwise assume
25487 we're still in the NSDMI. */
25488 error = (peek->type != CPP_EQ
25489 && peek->type != CPP_SEMICOLON);
25491 else
25493 cp_lexer_consume_token (parser->lexer);
25494 begin_scope (sk_function_parms, NULL_TREE);
25495 cp_parser_parameter_declaration_list (parser, &error);
25496 pop_bindings_and_leave_scope ();
25498 if (!cp_parser_error_occurred (parser) && !error)
25499 done = true;
25500 cp_parser_abort_tentative_parse (parser);
25502 parser->in_template_argument_list_p = saved_italp;
25503 break;
25505 case CPP_CLOSE_PAREN:
25506 case CPP_ELLIPSIS:
25507 /* If we run into a non-nested `;', `}', or `]',
25508 then the code is invalid -- but the default
25509 argument is certainly over. */
25510 case CPP_SEMICOLON:
25511 case CPP_CLOSE_BRACE:
25512 case CPP_CLOSE_SQUARE:
25513 if (depth == 0
25514 /* Handle correctly int n = sizeof ... ( p ); */
25515 && token->type != CPP_ELLIPSIS)
25516 done = true;
25517 /* Update DEPTH, if necessary. */
25518 else if (token->type == CPP_CLOSE_PAREN
25519 || token->type == CPP_CLOSE_BRACE
25520 || token->type == CPP_CLOSE_SQUARE)
25521 --depth;
25522 break;
25524 case CPP_OPEN_PAREN:
25525 case CPP_OPEN_SQUARE:
25526 case CPP_OPEN_BRACE:
25527 ++depth;
25528 break;
25530 case CPP_LESS:
25531 if (depth == 0)
25532 /* This might be the comparison operator, or it might
25533 start a template argument list. */
25534 ++maybe_template_id;
25535 break;
25537 case CPP_RSHIFT:
25538 if (cxx_dialect == cxx98)
25539 break;
25540 /* Fall through for C++0x, which treats the `>>'
25541 operator like two `>' tokens in certain
25542 cases. */
25544 case CPP_GREATER:
25545 if (depth == 0)
25547 /* This might be an operator, or it might close a
25548 template argument list. But if a previous '<'
25549 started a template argument list, this will have
25550 closed it, so we can't be in one anymore. */
25551 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25552 if (maybe_template_id < 0)
25553 maybe_template_id = 0;
25555 break;
25557 /* If we run out of tokens, issue an error message. */
25558 case CPP_EOF:
25559 case CPP_PRAGMA_EOL:
25560 error_at (token->location, "file ends in default argument");
25561 done = true;
25562 break;
25564 case CPP_NAME:
25565 case CPP_SCOPE:
25566 /* In these cases, we should look for template-ids.
25567 For example, if the default argument is
25568 `X<int, double>()', we need to do name lookup to
25569 figure out whether or not `X' is a template; if
25570 so, the `,' does not end the default argument.
25572 That is not yet done. */
25573 break;
25575 default:
25576 break;
25579 /* If we've reached the end, stop. */
25580 if (done)
25581 break;
25583 /* Add the token to the token block. */
25584 token = cp_lexer_consume_token (parser->lexer);
25587 /* Create a DEFAULT_ARG to represent the unparsed default
25588 argument. */
25589 default_argument = make_node (DEFAULT_ARG);
25590 DEFARG_TOKENS (default_argument)
25591 = cp_token_cache_new (first_token, token);
25592 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25594 return default_argument;
25597 /* Begin parsing tentatively. We always save tokens while parsing
25598 tentatively so that if the tentative parsing fails we can restore the
25599 tokens. */
25601 static void
25602 cp_parser_parse_tentatively (cp_parser* parser)
25604 /* Enter a new parsing context. */
25605 parser->context = cp_parser_context_new (parser->context);
25606 /* Begin saving tokens. */
25607 cp_lexer_save_tokens (parser->lexer);
25608 /* In order to avoid repetitive access control error messages,
25609 access checks are queued up until we are no longer parsing
25610 tentatively. */
25611 push_deferring_access_checks (dk_deferred);
25614 /* Commit to the currently active tentative parse. */
25616 static void
25617 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25619 cp_parser_context *context;
25620 cp_lexer *lexer;
25622 /* Mark all of the levels as committed. */
25623 lexer = parser->lexer;
25624 for (context = parser->context; context->next; context = context->next)
25626 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25627 break;
25628 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25629 while (!cp_lexer_saving_tokens (lexer))
25630 lexer = lexer->next;
25631 cp_lexer_commit_tokens (lexer);
25635 /* Commit to the topmost currently active tentative parse.
25637 Note that this function shouldn't be called when there are
25638 irreversible side-effects while in a tentative state. For
25639 example, we shouldn't create a permanent entry in the symbol
25640 table, or issue an error message that might not apply if the
25641 tentative parse is aborted. */
25643 static void
25644 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25646 cp_parser_context *context = parser->context;
25647 cp_lexer *lexer = parser->lexer;
25649 if (context)
25651 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25652 return;
25653 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25655 while (!cp_lexer_saving_tokens (lexer))
25656 lexer = lexer->next;
25657 cp_lexer_commit_tokens (lexer);
25661 /* Abort the currently active tentative parse. All consumed tokens
25662 will be rolled back, and no diagnostics will be issued. */
25664 static void
25665 cp_parser_abort_tentative_parse (cp_parser* parser)
25667 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25668 || errorcount > 0);
25669 cp_parser_simulate_error (parser);
25670 /* Now, pretend that we want to see if the construct was
25671 successfully parsed. */
25672 cp_parser_parse_definitely (parser);
25675 /* Stop parsing tentatively. If a parse error has occurred, restore the
25676 token stream. Otherwise, commit to the tokens we have consumed.
25677 Returns true if no error occurred; false otherwise. */
25679 static bool
25680 cp_parser_parse_definitely (cp_parser* parser)
25682 bool error_occurred;
25683 cp_parser_context *context;
25685 /* Remember whether or not an error occurred, since we are about to
25686 destroy that information. */
25687 error_occurred = cp_parser_error_occurred (parser);
25688 /* Remove the topmost context from the stack. */
25689 context = parser->context;
25690 parser->context = context->next;
25691 /* If no parse errors occurred, commit to the tentative parse. */
25692 if (!error_occurred)
25694 /* Commit to the tokens read tentatively, unless that was
25695 already done. */
25696 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25697 cp_lexer_commit_tokens (parser->lexer);
25699 pop_to_parent_deferring_access_checks ();
25701 /* Otherwise, if errors occurred, roll back our state so that things
25702 are just as they were before we began the tentative parse. */
25703 else
25705 cp_lexer_rollback_tokens (parser->lexer);
25706 pop_deferring_access_checks ();
25708 /* Add the context to the front of the free list. */
25709 context->next = cp_parser_context_free_list;
25710 cp_parser_context_free_list = context;
25712 return !error_occurred;
25715 /* Returns true if we are parsing tentatively and are not committed to
25716 this tentative parse. */
25718 static bool
25719 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25721 return (cp_parser_parsing_tentatively (parser)
25722 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25725 /* Returns nonzero iff an error has occurred during the most recent
25726 tentative parse. */
25728 static bool
25729 cp_parser_error_occurred (cp_parser* parser)
25731 return (cp_parser_parsing_tentatively (parser)
25732 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25735 /* Returns nonzero if GNU extensions are allowed. */
25737 static bool
25738 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25740 return parser->allow_gnu_extensions_p;
25743 /* Objective-C++ Productions */
25746 /* Parse an Objective-C expression, which feeds into a primary-expression
25747 above.
25749 objc-expression:
25750 objc-message-expression
25751 objc-string-literal
25752 objc-encode-expression
25753 objc-protocol-expression
25754 objc-selector-expression
25756 Returns a tree representation of the expression. */
25758 static tree
25759 cp_parser_objc_expression (cp_parser* parser)
25761 /* Try to figure out what kind of declaration is present. */
25762 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25764 switch (kwd->type)
25766 case CPP_OPEN_SQUARE:
25767 return cp_parser_objc_message_expression (parser);
25769 case CPP_OBJC_STRING:
25770 kwd = cp_lexer_consume_token (parser->lexer);
25771 return objc_build_string_object (kwd->u.value);
25773 case CPP_KEYWORD:
25774 switch (kwd->keyword)
25776 case RID_AT_ENCODE:
25777 return cp_parser_objc_encode_expression (parser);
25779 case RID_AT_PROTOCOL:
25780 return cp_parser_objc_protocol_expression (parser);
25782 case RID_AT_SELECTOR:
25783 return cp_parser_objc_selector_expression (parser);
25785 default:
25786 break;
25788 default:
25789 error_at (kwd->location,
25790 "misplaced %<@%D%> Objective-C++ construct",
25791 kwd->u.value);
25792 cp_parser_skip_to_end_of_block_or_statement (parser);
25795 return error_mark_node;
25798 /* Parse an Objective-C message expression.
25800 objc-message-expression:
25801 [ objc-message-receiver objc-message-args ]
25803 Returns a representation of an Objective-C message. */
25805 static tree
25806 cp_parser_objc_message_expression (cp_parser* parser)
25808 tree receiver, messageargs;
25810 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25811 receiver = cp_parser_objc_message_receiver (parser);
25812 messageargs = cp_parser_objc_message_args (parser);
25813 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25815 return objc_build_message_expr (receiver, messageargs);
25818 /* Parse an objc-message-receiver.
25820 objc-message-receiver:
25821 expression
25822 simple-type-specifier
25824 Returns a representation of the type or expression. */
25826 static tree
25827 cp_parser_objc_message_receiver (cp_parser* parser)
25829 tree rcv;
25831 /* An Objective-C message receiver may be either (1) a type
25832 or (2) an expression. */
25833 cp_parser_parse_tentatively (parser);
25834 rcv = cp_parser_expression (parser);
25836 /* If that worked out, fine. */
25837 if (cp_parser_parse_definitely (parser))
25838 return rcv;
25840 cp_parser_parse_tentatively (parser);
25841 rcv = cp_parser_simple_type_specifier (parser,
25842 /*decl_specs=*/NULL,
25843 CP_PARSER_FLAGS_NONE);
25845 if (cp_parser_parse_definitely (parser))
25846 return objc_get_class_reference (rcv);
25848 cp_parser_error (parser, "objective-c++ message receiver expected");
25849 return error_mark_node;
25852 /* Parse the arguments and selectors comprising an Objective-C message.
25854 objc-message-args:
25855 objc-selector
25856 objc-selector-args
25857 objc-selector-args , objc-comma-args
25859 objc-selector-args:
25860 objc-selector [opt] : assignment-expression
25861 objc-selector-args objc-selector [opt] : assignment-expression
25863 objc-comma-args:
25864 assignment-expression
25865 objc-comma-args , assignment-expression
25867 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25868 selector arguments and TREE_VALUE containing a list of comma
25869 arguments. */
25871 static tree
25872 cp_parser_objc_message_args (cp_parser* parser)
25874 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25875 bool maybe_unary_selector_p = true;
25876 cp_token *token = cp_lexer_peek_token (parser->lexer);
25878 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25880 tree selector = NULL_TREE, arg;
25882 if (token->type != CPP_COLON)
25883 selector = cp_parser_objc_selector (parser);
25885 /* Detect if we have a unary selector. */
25886 if (maybe_unary_selector_p
25887 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25888 return build_tree_list (selector, NULL_TREE);
25890 maybe_unary_selector_p = false;
25891 cp_parser_require (parser, CPP_COLON, RT_COLON);
25892 arg = cp_parser_assignment_expression (parser);
25894 sel_args
25895 = chainon (sel_args,
25896 build_tree_list (selector, arg));
25898 token = cp_lexer_peek_token (parser->lexer);
25901 /* Handle non-selector arguments, if any. */
25902 while (token->type == CPP_COMMA)
25904 tree arg;
25906 cp_lexer_consume_token (parser->lexer);
25907 arg = cp_parser_assignment_expression (parser);
25909 addl_args
25910 = chainon (addl_args,
25911 build_tree_list (NULL_TREE, arg));
25913 token = cp_lexer_peek_token (parser->lexer);
25916 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25918 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25919 return build_tree_list (error_mark_node, error_mark_node);
25922 return build_tree_list (sel_args, addl_args);
25925 /* Parse an Objective-C encode expression.
25927 objc-encode-expression:
25928 @encode objc-typename
25930 Returns an encoded representation of the type argument. */
25932 static tree
25933 cp_parser_objc_encode_expression (cp_parser* parser)
25935 tree type;
25936 cp_token *token;
25938 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25939 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25940 token = cp_lexer_peek_token (parser->lexer);
25941 type = complete_type (cp_parser_type_id (parser));
25942 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25944 if (!type)
25946 error_at (token->location,
25947 "%<@encode%> must specify a type as an argument");
25948 return error_mark_node;
25951 /* This happens if we find @encode(T) (where T is a template
25952 typename or something dependent on a template typename) when
25953 parsing a template. In that case, we can't compile it
25954 immediately, but we rather create an AT_ENCODE_EXPR which will
25955 need to be instantiated when the template is used.
25957 if (dependent_type_p (type))
25959 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25960 TREE_READONLY (value) = 1;
25961 return value;
25964 return objc_build_encode_expr (type);
25967 /* Parse an Objective-C @defs expression. */
25969 static tree
25970 cp_parser_objc_defs_expression (cp_parser *parser)
25972 tree name;
25974 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25975 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25976 name = cp_parser_identifier (parser);
25977 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25979 return objc_get_class_ivars (name);
25982 /* Parse an Objective-C protocol expression.
25984 objc-protocol-expression:
25985 @protocol ( identifier )
25987 Returns a representation of the protocol expression. */
25989 static tree
25990 cp_parser_objc_protocol_expression (cp_parser* parser)
25992 tree proto;
25994 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25995 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25996 proto = cp_parser_identifier (parser);
25997 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25999 return objc_build_protocol_expr (proto);
26002 /* Parse an Objective-C selector expression.
26004 objc-selector-expression:
26005 @selector ( objc-method-signature )
26007 objc-method-signature:
26008 objc-selector
26009 objc-selector-seq
26011 objc-selector-seq:
26012 objc-selector :
26013 objc-selector-seq objc-selector :
26015 Returns a representation of the method selector. */
26017 static tree
26018 cp_parser_objc_selector_expression (cp_parser* parser)
26020 tree sel_seq = NULL_TREE;
26021 bool maybe_unary_selector_p = true;
26022 cp_token *token;
26023 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26025 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
26026 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26027 token = cp_lexer_peek_token (parser->lexer);
26029 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
26030 || token->type == CPP_SCOPE)
26032 tree selector = NULL_TREE;
26034 if (token->type != CPP_COLON
26035 || token->type == CPP_SCOPE)
26036 selector = cp_parser_objc_selector (parser);
26038 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
26039 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
26041 /* Detect if we have a unary selector. */
26042 if (maybe_unary_selector_p)
26044 sel_seq = selector;
26045 goto finish_selector;
26047 else
26049 cp_parser_error (parser, "expected %<:%>");
26052 maybe_unary_selector_p = false;
26053 token = cp_lexer_consume_token (parser->lexer);
26055 if (token->type == CPP_SCOPE)
26057 sel_seq
26058 = chainon (sel_seq,
26059 build_tree_list (selector, NULL_TREE));
26060 sel_seq
26061 = chainon (sel_seq,
26062 build_tree_list (NULL_TREE, NULL_TREE));
26064 else
26065 sel_seq
26066 = chainon (sel_seq,
26067 build_tree_list (selector, NULL_TREE));
26069 token = cp_lexer_peek_token (parser->lexer);
26072 finish_selector:
26073 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26075 return objc_build_selector_expr (loc, sel_seq);
26078 /* Parse a list of identifiers.
26080 objc-identifier-list:
26081 identifier
26082 objc-identifier-list , identifier
26084 Returns a TREE_LIST of identifier nodes. */
26086 static tree
26087 cp_parser_objc_identifier_list (cp_parser* parser)
26089 tree identifier;
26090 tree list;
26091 cp_token *sep;
26093 identifier = cp_parser_identifier (parser);
26094 if (identifier == error_mark_node)
26095 return error_mark_node;
26097 list = build_tree_list (NULL_TREE, identifier);
26098 sep = cp_lexer_peek_token (parser->lexer);
26100 while (sep->type == CPP_COMMA)
26102 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26103 identifier = cp_parser_identifier (parser);
26104 if (identifier == error_mark_node)
26105 return list;
26107 list = chainon (list, build_tree_list (NULL_TREE,
26108 identifier));
26109 sep = cp_lexer_peek_token (parser->lexer);
26112 return list;
26115 /* Parse an Objective-C alias declaration.
26117 objc-alias-declaration:
26118 @compatibility_alias identifier identifier ;
26120 This function registers the alias mapping with the Objective-C front end.
26121 It returns nothing. */
26123 static void
26124 cp_parser_objc_alias_declaration (cp_parser* parser)
26126 tree alias, orig;
26128 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
26129 alias = cp_parser_identifier (parser);
26130 orig = cp_parser_identifier (parser);
26131 objc_declare_alias (alias, orig);
26132 cp_parser_consume_semicolon_at_end_of_statement (parser);
26135 /* Parse an Objective-C class forward-declaration.
26137 objc-class-declaration:
26138 @class objc-identifier-list ;
26140 The function registers the forward declarations with the Objective-C
26141 front end. It returns nothing. */
26143 static void
26144 cp_parser_objc_class_declaration (cp_parser* parser)
26146 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
26147 while (true)
26149 tree id;
26151 id = cp_parser_identifier (parser);
26152 if (id == error_mark_node)
26153 break;
26155 objc_declare_class (id);
26157 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26158 cp_lexer_consume_token (parser->lexer);
26159 else
26160 break;
26162 cp_parser_consume_semicolon_at_end_of_statement (parser);
26165 /* Parse a list of Objective-C protocol references.
26167 objc-protocol-refs-opt:
26168 objc-protocol-refs [opt]
26170 objc-protocol-refs:
26171 < objc-identifier-list >
26173 Returns a TREE_LIST of identifiers, if any. */
26175 static tree
26176 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26178 tree protorefs = NULL_TREE;
26180 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26182 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
26183 protorefs = cp_parser_objc_identifier_list (parser);
26184 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26187 return protorefs;
26190 /* Parse a Objective-C visibility specification. */
26192 static void
26193 cp_parser_objc_visibility_spec (cp_parser* parser)
26195 cp_token *vis = cp_lexer_peek_token (parser->lexer);
26197 switch (vis->keyword)
26199 case RID_AT_PRIVATE:
26200 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26201 break;
26202 case RID_AT_PROTECTED:
26203 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26204 break;
26205 case RID_AT_PUBLIC:
26206 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26207 break;
26208 case RID_AT_PACKAGE:
26209 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26210 break;
26211 default:
26212 return;
26215 /* Eat '@private'/'@protected'/'@public'. */
26216 cp_lexer_consume_token (parser->lexer);
26219 /* Parse an Objective-C method type. Return 'true' if it is a class
26220 (+) method, and 'false' if it is an instance (-) method. */
26222 static inline bool
26223 cp_parser_objc_method_type (cp_parser* parser)
26225 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26226 return true;
26227 else
26228 return false;
26231 /* Parse an Objective-C protocol qualifier. */
26233 static tree
26234 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26236 tree quals = NULL_TREE, node;
26237 cp_token *token = cp_lexer_peek_token (parser->lexer);
26239 node = token->u.value;
26241 while (node && identifier_p (node)
26242 && (node == ridpointers [(int) RID_IN]
26243 || node == ridpointers [(int) RID_OUT]
26244 || node == ridpointers [(int) RID_INOUT]
26245 || node == ridpointers [(int) RID_BYCOPY]
26246 || node == ridpointers [(int) RID_BYREF]
26247 || node == ridpointers [(int) RID_ONEWAY]))
26249 quals = tree_cons (NULL_TREE, node, quals);
26250 cp_lexer_consume_token (parser->lexer);
26251 token = cp_lexer_peek_token (parser->lexer);
26252 node = token->u.value;
26255 return quals;
26258 /* Parse an Objective-C typename. */
26260 static tree
26261 cp_parser_objc_typename (cp_parser* parser)
26263 tree type_name = NULL_TREE;
26265 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26267 tree proto_quals, cp_type = NULL_TREE;
26269 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26270 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26272 /* An ObjC type name may consist of just protocol qualifiers, in which
26273 case the type shall default to 'id'. */
26274 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26276 cp_type = cp_parser_type_id (parser);
26278 /* If the type could not be parsed, an error has already
26279 been produced. For error recovery, behave as if it had
26280 not been specified, which will use the default type
26281 'id'. */
26282 if (cp_type == error_mark_node)
26284 cp_type = NULL_TREE;
26285 /* We need to skip to the closing parenthesis as
26286 cp_parser_type_id() does not seem to do it for
26287 us. */
26288 cp_parser_skip_to_closing_parenthesis (parser,
26289 /*recovering=*/true,
26290 /*or_comma=*/false,
26291 /*consume_paren=*/false);
26295 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26296 type_name = build_tree_list (proto_quals, cp_type);
26299 return type_name;
26302 /* Check to see if TYPE refers to an Objective-C selector name. */
26304 static bool
26305 cp_parser_objc_selector_p (enum cpp_ttype type)
26307 return (type == CPP_NAME || type == CPP_KEYWORD
26308 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26309 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26310 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26311 || type == CPP_XOR || type == CPP_XOR_EQ);
26314 /* Parse an Objective-C selector. */
26316 static tree
26317 cp_parser_objc_selector (cp_parser* parser)
26319 cp_token *token = cp_lexer_consume_token (parser->lexer);
26321 if (!cp_parser_objc_selector_p (token->type))
26323 error_at (token->location, "invalid Objective-C++ selector name");
26324 return error_mark_node;
26327 /* C++ operator names are allowed to appear in ObjC selectors. */
26328 switch (token->type)
26330 case CPP_AND_AND: return get_identifier ("and");
26331 case CPP_AND_EQ: return get_identifier ("and_eq");
26332 case CPP_AND: return get_identifier ("bitand");
26333 case CPP_OR: return get_identifier ("bitor");
26334 case CPP_COMPL: return get_identifier ("compl");
26335 case CPP_NOT: return get_identifier ("not");
26336 case CPP_NOT_EQ: return get_identifier ("not_eq");
26337 case CPP_OR_OR: return get_identifier ("or");
26338 case CPP_OR_EQ: return get_identifier ("or_eq");
26339 case CPP_XOR: return get_identifier ("xor");
26340 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26341 default: return token->u.value;
26345 /* Parse an Objective-C params list. */
26347 static tree
26348 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26350 tree params = NULL_TREE;
26351 bool maybe_unary_selector_p = true;
26352 cp_token *token = cp_lexer_peek_token (parser->lexer);
26354 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26356 tree selector = NULL_TREE, type_name, identifier;
26357 tree parm_attr = NULL_TREE;
26359 if (token->keyword == RID_ATTRIBUTE)
26360 break;
26362 if (token->type != CPP_COLON)
26363 selector = cp_parser_objc_selector (parser);
26365 /* Detect if we have a unary selector. */
26366 if (maybe_unary_selector_p
26367 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26369 params = selector; /* Might be followed by attributes. */
26370 break;
26373 maybe_unary_selector_p = false;
26374 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26376 /* Something went quite wrong. There should be a colon
26377 here, but there is not. Stop parsing parameters. */
26378 break;
26380 type_name = cp_parser_objc_typename (parser);
26381 /* New ObjC allows attributes on parameters too. */
26382 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26383 parm_attr = cp_parser_attributes_opt (parser);
26384 identifier = cp_parser_identifier (parser);
26386 params
26387 = chainon (params,
26388 objc_build_keyword_decl (selector,
26389 type_name,
26390 identifier,
26391 parm_attr));
26393 token = cp_lexer_peek_token (parser->lexer);
26396 if (params == NULL_TREE)
26398 cp_parser_error (parser, "objective-c++ method declaration is expected");
26399 return error_mark_node;
26402 /* We allow tail attributes for the method. */
26403 if (token->keyword == RID_ATTRIBUTE)
26405 *attributes = cp_parser_attributes_opt (parser);
26406 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26407 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26408 return params;
26409 cp_parser_error (parser,
26410 "method attributes must be specified at the end");
26411 return error_mark_node;
26414 if (params == NULL_TREE)
26416 cp_parser_error (parser, "objective-c++ method declaration is expected");
26417 return error_mark_node;
26419 return params;
26422 /* Parse the non-keyword Objective-C params. */
26424 static tree
26425 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26426 tree* attributes)
26428 tree params = make_node (TREE_LIST);
26429 cp_token *token = cp_lexer_peek_token (parser->lexer);
26430 *ellipsisp = false; /* Initially, assume no ellipsis. */
26432 while (token->type == CPP_COMMA)
26434 cp_parameter_declarator *parmdecl;
26435 tree parm;
26437 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26438 token = cp_lexer_peek_token (parser->lexer);
26440 if (token->type == CPP_ELLIPSIS)
26442 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26443 *ellipsisp = true;
26444 token = cp_lexer_peek_token (parser->lexer);
26445 break;
26448 /* TODO: parse attributes for tail parameters. */
26449 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26450 parm = grokdeclarator (parmdecl->declarator,
26451 &parmdecl->decl_specifiers,
26452 PARM, /*initialized=*/0,
26453 /*attrlist=*/NULL);
26455 chainon (params, build_tree_list (NULL_TREE, parm));
26456 token = cp_lexer_peek_token (parser->lexer);
26459 /* We allow tail attributes for the method. */
26460 if (token->keyword == RID_ATTRIBUTE)
26462 if (*attributes == NULL_TREE)
26464 *attributes = cp_parser_attributes_opt (parser);
26465 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26466 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26467 return params;
26469 else
26470 /* We have an error, but parse the attributes, so that we can
26471 carry on. */
26472 *attributes = cp_parser_attributes_opt (parser);
26474 cp_parser_error (parser,
26475 "method attributes must be specified at the end");
26476 return error_mark_node;
26479 return params;
26482 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26484 static void
26485 cp_parser_objc_interstitial_code (cp_parser* parser)
26487 cp_token *token = cp_lexer_peek_token (parser->lexer);
26489 /* If the next token is `extern' and the following token is a string
26490 literal, then we have a linkage specification. */
26491 if (token->keyword == RID_EXTERN
26492 && cp_parser_is_pure_string_literal
26493 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26494 cp_parser_linkage_specification (parser);
26495 /* Handle #pragma, if any. */
26496 else if (token->type == CPP_PRAGMA)
26497 cp_parser_pragma (parser, pragma_objc_icode);
26498 /* Allow stray semicolons. */
26499 else if (token->type == CPP_SEMICOLON)
26500 cp_lexer_consume_token (parser->lexer);
26501 /* Mark methods as optional or required, when building protocols. */
26502 else if (token->keyword == RID_AT_OPTIONAL)
26504 cp_lexer_consume_token (parser->lexer);
26505 objc_set_method_opt (true);
26507 else if (token->keyword == RID_AT_REQUIRED)
26509 cp_lexer_consume_token (parser->lexer);
26510 objc_set_method_opt (false);
26512 else if (token->keyword == RID_NAMESPACE)
26513 cp_parser_namespace_definition (parser);
26514 /* Other stray characters must generate errors. */
26515 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26517 cp_lexer_consume_token (parser->lexer);
26518 error ("stray %qs between Objective-C++ methods",
26519 token->type == CPP_OPEN_BRACE ? "{" : "}");
26521 /* Finally, try to parse a block-declaration, or a function-definition. */
26522 else
26523 cp_parser_block_declaration (parser, /*statement_p=*/false);
26526 /* Parse a method signature. */
26528 static tree
26529 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26531 tree rettype, kwdparms, optparms;
26532 bool ellipsis = false;
26533 bool is_class_method;
26535 is_class_method = cp_parser_objc_method_type (parser);
26536 rettype = cp_parser_objc_typename (parser);
26537 *attributes = NULL_TREE;
26538 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26539 if (kwdparms == error_mark_node)
26540 return error_mark_node;
26541 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26542 if (optparms == error_mark_node)
26543 return error_mark_node;
26545 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26548 static bool
26549 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26551 tree tattr;
26552 cp_lexer_save_tokens (parser->lexer);
26553 tattr = cp_parser_attributes_opt (parser);
26554 gcc_assert (tattr) ;
26556 /* If the attributes are followed by a method introducer, this is not allowed.
26557 Dump the attributes and flag the situation. */
26558 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26559 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26560 return true;
26562 /* Otherwise, the attributes introduce some interstitial code, possibly so
26563 rewind to allow that check. */
26564 cp_lexer_rollback_tokens (parser->lexer);
26565 return false;
26568 /* Parse an Objective-C method prototype list. */
26570 static void
26571 cp_parser_objc_method_prototype_list (cp_parser* parser)
26573 cp_token *token = cp_lexer_peek_token (parser->lexer);
26575 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26577 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26579 tree attributes, sig;
26580 bool is_class_method;
26581 if (token->type == CPP_PLUS)
26582 is_class_method = true;
26583 else
26584 is_class_method = false;
26585 sig = cp_parser_objc_method_signature (parser, &attributes);
26586 if (sig == error_mark_node)
26588 cp_parser_skip_to_end_of_block_or_statement (parser);
26589 token = cp_lexer_peek_token (parser->lexer);
26590 continue;
26592 objc_add_method_declaration (is_class_method, sig, attributes);
26593 cp_parser_consume_semicolon_at_end_of_statement (parser);
26595 else if (token->keyword == RID_AT_PROPERTY)
26596 cp_parser_objc_at_property_declaration (parser);
26597 else if (token->keyword == RID_ATTRIBUTE
26598 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26599 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26600 OPT_Wattributes,
26601 "prefix attributes are ignored for methods");
26602 else
26603 /* Allow for interspersed non-ObjC++ code. */
26604 cp_parser_objc_interstitial_code (parser);
26606 token = cp_lexer_peek_token (parser->lexer);
26609 if (token->type != CPP_EOF)
26610 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26611 else
26612 cp_parser_error (parser, "expected %<@end%>");
26614 objc_finish_interface ();
26617 /* Parse an Objective-C method definition list. */
26619 static void
26620 cp_parser_objc_method_definition_list (cp_parser* parser)
26622 cp_token *token = cp_lexer_peek_token (parser->lexer);
26624 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26626 tree meth;
26628 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26630 cp_token *ptk;
26631 tree sig, attribute;
26632 bool is_class_method;
26633 if (token->type == CPP_PLUS)
26634 is_class_method = true;
26635 else
26636 is_class_method = false;
26637 push_deferring_access_checks (dk_deferred);
26638 sig = cp_parser_objc_method_signature (parser, &attribute);
26639 if (sig == error_mark_node)
26641 cp_parser_skip_to_end_of_block_or_statement (parser);
26642 token = cp_lexer_peek_token (parser->lexer);
26643 continue;
26645 objc_start_method_definition (is_class_method, sig, attribute,
26646 NULL_TREE);
26648 /* For historical reasons, we accept an optional semicolon. */
26649 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26650 cp_lexer_consume_token (parser->lexer);
26652 ptk = cp_lexer_peek_token (parser->lexer);
26653 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26654 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26656 perform_deferred_access_checks (tf_warning_or_error);
26657 stop_deferring_access_checks ();
26658 meth = cp_parser_function_definition_after_declarator (parser,
26659 false);
26660 pop_deferring_access_checks ();
26661 objc_finish_method_definition (meth);
26664 /* The following case will be removed once @synthesize is
26665 completely implemented. */
26666 else if (token->keyword == RID_AT_PROPERTY)
26667 cp_parser_objc_at_property_declaration (parser);
26668 else if (token->keyword == RID_AT_SYNTHESIZE)
26669 cp_parser_objc_at_synthesize_declaration (parser);
26670 else if (token->keyword == RID_AT_DYNAMIC)
26671 cp_parser_objc_at_dynamic_declaration (parser);
26672 else if (token->keyword == RID_ATTRIBUTE
26673 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26674 warning_at (token->location, OPT_Wattributes,
26675 "prefix attributes are ignored for methods");
26676 else
26677 /* Allow for interspersed non-ObjC++ code. */
26678 cp_parser_objc_interstitial_code (parser);
26680 token = cp_lexer_peek_token (parser->lexer);
26683 if (token->type != CPP_EOF)
26684 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26685 else
26686 cp_parser_error (parser, "expected %<@end%>");
26688 objc_finish_implementation ();
26691 /* Parse Objective-C ivars. */
26693 static void
26694 cp_parser_objc_class_ivars (cp_parser* parser)
26696 cp_token *token = cp_lexer_peek_token (parser->lexer);
26698 if (token->type != CPP_OPEN_BRACE)
26699 return; /* No ivars specified. */
26701 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26702 token = cp_lexer_peek_token (parser->lexer);
26704 while (token->type != CPP_CLOSE_BRACE
26705 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26707 cp_decl_specifier_seq declspecs;
26708 int decl_class_or_enum_p;
26709 tree prefix_attributes;
26711 cp_parser_objc_visibility_spec (parser);
26713 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26714 break;
26716 cp_parser_decl_specifier_seq (parser,
26717 CP_PARSER_FLAGS_OPTIONAL,
26718 &declspecs,
26719 &decl_class_or_enum_p);
26721 /* auto, register, static, extern, mutable. */
26722 if (declspecs.storage_class != sc_none)
26724 cp_parser_error (parser, "invalid type for instance variable");
26725 declspecs.storage_class = sc_none;
26728 /* thread_local. */
26729 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26731 cp_parser_error (parser, "invalid type for instance variable");
26732 declspecs.locations[ds_thread] = 0;
26735 /* typedef. */
26736 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26738 cp_parser_error (parser, "invalid type for instance variable");
26739 declspecs.locations[ds_typedef] = 0;
26742 prefix_attributes = declspecs.attributes;
26743 declspecs.attributes = NULL_TREE;
26745 /* Keep going until we hit the `;' at the end of the
26746 declaration. */
26747 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26749 tree width = NULL_TREE, attributes, first_attribute, decl;
26750 cp_declarator *declarator = NULL;
26751 int ctor_dtor_or_conv_p;
26753 /* Check for a (possibly unnamed) bitfield declaration. */
26754 token = cp_lexer_peek_token (parser->lexer);
26755 if (token->type == CPP_COLON)
26756 goto eat_colon;
26758 if (token->type == CPP_NAME
26759 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26760 == CPP_COLON))
26762 /* Get the name of the bitfield. */
26763 declarator = make_id_declarator (NULL_TREE,
26764 cp_parser_identifier (parser),
26765 sfk_none);
26767 eat_colon:
26768 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26769 /* Get the width of the bitfield. */
26770 width
26771 = cp_parser_constant_expression (parser);
26773 else
26775 /* Parse the declarator. */
26776 declarator
26777 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26778 &ctor_dtor_or_conv_p,
26779 /*parenthesized_p=*/NULL,
26780 /*member_p=*/false,
26781 /*friend_p=*/false);
26784 /* Look for attributes that apply to the ivar. */
26785 attributes = cp_parser_attributes_opt (parser);
26786 /* Remember which attributes are prefix attributes and
26787 which are not. */
26788 first_attribute = attributes;
26789 /* Combine the attributes. */
26790 attributes = chainon (prefix_attributes, attributes);
26792 if (width)
26793 /* Create the bitfield declaration. */
26794 decl = grokbitfield (declarator, &declspecs,
26795 width,
26796 attributes);
26797 else
26798 decl = grokfield (declarator, &declspecs,
26799 NULL_TREE, /*init_const_expr_p=*/false,
26800 NULL_TREE, attributes);
26802 /* Add the instance variable. */
26803 if (decl != error_mark_node && decl != NULL_TREE)
26804 objc_add_instance_variable (decl);
26806 /* Reset PREFIX_ATTRIBUTES. */
26807 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26808 attributes = TREE_CHAIN (attributes);
26809 if (attributes)
26810 TREE_CHAIN (attributes) = NULL_TREE;
26812 token = cp_lexer_peek_token (parser->lexer);
26814 if (token->type == CPP_COMMA)
26816 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26817 continue;
26819 break;
26822 cp_parser_consume_semicolon_at_end_of_statement (parser);
26823 token = cp_lexer_peek_token (parser->lexer);
26826 if (token->keyword == RID_AT_END)
26827 cp_parser_error (parser, "expected %<}%>");
26829 /* Do not consume the RID_AT_END, so it will be read again as terminating
26830 the @interface of @implementation. */
26831 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26832 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26834 /* For historical reasons, we accept an optional semicolon. */
26835 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26836 cp_lexer_consume_token (parser->lexer);
26839 /* Parse an Objective-C protocol declaration. */
26841 static void
26842 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26844 tree proto, protorefs;
26845 cp_token *tok;
26847 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26848 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26850 tok = cp_lexer_peek_token (parser->lexer);
26851 error_at (tok->location, "identifier expected after %<@protocol%>");
26852 cp_parser_consume_semicolon_at_end_of_statement (parser);
26853 return;
26856 /* See if we have a forward declaration or a definition. */
26857 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26859 /* Try a forward declaration first. */
26860 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26862 while (true)
26864 tree id;
26866 id = cp_parser_identifier (parser);
26867 if (id == error_mark_node)
26868 break;
26870 objc_declare_protocol (id, attributes);
26872 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26873 cp_lexer_consume_token (parser->lexer);
26874 else
26875 break;
26877 cp_parser_consume_semicolon_at_end_of_statement (parser);
26880 /* Ok, we got a full-fledged definition (or at least should). */
26881 else
26883 proto = cp_parser_identifier (parser);
26884 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26885 objc_start_protocol (proto, protorefs, attributes);
26886 cp_parser_objc_method_prototype_list (parser);
26890 /* Parse an Objective-C superclass or category. */
26892 static void
26893 cp_parser_objc_superclass_or_category (cp_parser *parser,
26894 bool iface_p,
26895 tree *super,
26896 tree *categ, bool *is_class_extension)
26898 cp_token *next = cp_lexer_peek_token (parser->lexer);
26900 *super = *categ = NULL_TREE;
26901 *is_class_extension = false;
26902 if (next->type == CPP_COLON)
26904 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26905 *super = cp_parser_identifier (parser);
26907 else if (next->type == CPP_OPEN_PAREN)
26909 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26911 /* If there is no category name, and this is an @interface, we
26912 have a class extension. */
26913 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26915 *categ = NULL_TREE;
26916 *is_class_extension = true;
26918 else
26919 *categ = cp_parser_identifier (parser);
26921 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26925 /* Parse an Objective-C class interface. */
26927 static void
26928 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26930 tree name, super, categ, protos;
26931 bool is_class_extension;
26933 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26934 name = cp_parser_identifier (parser);
26935 if (name == error_mark_node)
26937 /* It's hard to recover because even if valid @interface stuff
26938 is to follow, we can't compile it (or validate it) if we
26939 don't even know which class it refers to. Let's assume this
26940 was a stray '@interface' token in the stream and skip it.
26942 return;
26944 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26945 &is_class_extension);
26946 protos = cp_parser_objc_protocol_refs_opt (parser);
26948 /* We have either a class or a category on our hands. */
26949 if (categ || is_class_extension)
26950 objc_start_category_interface (name, categ, protos, attributes);
26951 else
26953 objc_start_class_interface (name, super, protos, attributes);
26954 /* Handle instance variable declarations, if any. */
26955 cp_parser_objc_class_ivars (parser);
26956 objc_continue_interface ();
26959 cp_parser_objc_method_prototype_list (parser);
26962 /* Parse an Objective-C class implementation. */
26964 static void
26965 cp_parser_objc_class_implementation (cp_parser* parser)
26967 tree name, super, categ;
26968 bool is_class_extension;
26970 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26971 name = cp_parser_identifier (parser);
26972 if (name == error_mark_node)
26974 /* It's hard to recover because even if valid @implementation
26975 stuff is to follow, we can't compile it (or validate it) if
26976 we don't even know which class it refers to. Let's assume
26977 this was a stray '@implementation' token in the stream and
26978 skip it.
26980 return;
26982 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26983 &is_class_extension);
26985 /* We have either a class or a category on our hands. */
26986 if (categ)
26987 objc_start_category_implementation (name, categ);
26988 else
26990 objc_start_class_implementation (name, super);
26991 /* Handle instance variable declarations, if any. */
26992 cp_parser_objc_class_ivars (parser);
26993 objc_continue_implementation ();
26996 cp_parser_objc_method_definition_list (parser);
26999 /* Consume the @end token and finish off the implementation. */
27001 static void
27002 cp_parser_objc_end_implementation (cp_parser* parser)
27004 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
27005 objc_finish_implementation ();
27008 /* Parse an Objective-C declaration. */
27010 static void
27011 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
27013 /* Try to figure out what kind of declaration is present. */
27014 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27016 if (attributes)
27017 switch (kwd->keyword)
27019 case RID_AT_ALIAS:
27020 case RID_AT_CLASS:
27021 case RID_AT_END:
27022 error_at (kwd->location, "attributes may not be specified before"
27023 " the %<@%D%> Objective-C++ keyword",
27024 kwd->u.value);
27025 attributes = NULL;
27026 break;
27027 case RID_AT_IMPLEMENTATION:
27028 warning_at (kwd->location, OPT_Wattributes,
27029 "prefix attributes are ignored before %<@%D%>",
27030 kwd->u.value);
27031 attributes = NULL;
27032 default:
27033 break;
27036 switch (kwd->keyword)
27038 case RID_AT_ALIAS:
27039 cp_parser_objc_alias_declaration (parser);
27040 break;
27041 case RID_AT_CLASS:
27042 cp_parser_objc_class_declaration (parser);
27043 break;
27044 case RID_AT_PROTOCOL:
27045 cp_parser_objc_protocol_declaration (parser, attributes);
27046 break;
27047 case RID_AT_INTERFACE:
27048 cp_parser_objc_class_interface (parser, attributes);
27049 break;
27050 case RID_AT_IMPLEMENTATION:
27051 cp_parser_objc_class_implementation (parser);
27052 break;
27053 case RID_AT_END:
27054 cp_parser_objc_end_implementation (parser);
27055 break;
27056 default:
27057 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27058 kwd->u.value);
27059 cp_parser_skip_to_end_of_block_or_statement (parser);
27063 /* Parse an Objective-C try-catch-finally statement.
27065 objc-try-catch-finally-stmt:
27066 @try compound-statement objc-catch-clause-seq [opt]
27067 objc-finally-clause [opt]
27069 objc-catch-clause-seq:
27070 objc-catch-clause objc-catch-clause-seq [opt]
27072 objc-catch-clause:
27073 @catch ( objc-exception-declaration ) compound-statement
27075 objc-finally-clause:
27076 @finally compound-statement
27078 objc-exception-declaration:
27079 parameter-declaration
27080 '...'
27082 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
27084 Returns NULL_TREE.
27086 PS: This function is identical to c_parser_objc_try_catch_finally_statement
27087 for C. Keep them in sync. */
27089 static tree
27090 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
27092 location_t location;
27093 tree stmt;
27095 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
27096 location = cp_lexer_peek_token (parser->lexer)->location;
27097 objc_maybe_warn_exceptions (location);
27098 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
27099 node, lest it get absorbed into the surrounding block. */
27100 stmt = push_stmt_list ();
27101 cp_parser_compound_statement (parser, NULL, false, false);
27102 objc_begin_try_stmt (location, pop_stmt_list (stmt));
27104 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
27106 cp_parameter_declarator *parm;
27107 tree parameter_declaration = error_mark_node;
27108 bool seen_open_paren = false;
27110 cp_lexer_consume_token (parser->lexer);
27111 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27112 seen_open_paren = true;
27113 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27115 /* We have "@catch (...)" (where the '...' are literally
27116 what is in the code). Skip the '...'.
27117 parameter_declaration is set to NULL_TREE, and
27118 objc_being_catch_clauses() knows that that means
27119 '...'. */
27120 cp_lexer_consume_token (parser->lexer);
27121 parameter_declaration = NULL_TREE;
27123 else
27125 /* We have "@catch (NSException *exception)" or something
27126 like that. Parse the parameter declaration. */
27127 parm = cp_parser_parameter_declaration (parser, false, NULL);
27128 if (parm == NULL)
27129 parameter_declaration = error_mark_node;
27130 else
27131 parameter_declaration = grokdeclarator (parm->declarator,
27132 &parm->decl_specifiers,
27133 PARM, /*initialized=*/0,
27134 /*attrlist=*/NULL);
27136 if (seen_open_paren)
27137 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27138 else
27140 /* If there was no open parenthesis, we are recovering from
27141 an error, and we are trying to figure out what mistake
27142 the user has made. */
27144 /* If there is an immediate closing parenthesis, the user
27145 probably forgot the opening one (ie, they typed "@catch
27146 NSException *e)". Parse the closing parenthesis and keep
27147 going. */
27148 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27149 cp_lexer_consume_token (parser->lexer);
27151 /* If these is no immediate closing parenthesis, the user
27152 probably doesn't know that parenthesis are required at
27153 all (ie, they typed "@catch NSException *e"). So, just
27154 forget about the closing parenthesis and keep going. */
27156 objc_begin_catch_clause (parameter_declaration);
27157 cp_parser_compound_statement (parser, NULL, false, false);
27158 objc_finish_catch_clause ();
27160 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
27162 cp_lexer_consume_token (parser->lexer);
27163 location = cp_lexer_peek_token (parser->lexer)->location;
27164 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27165 node, lest it get absorbed into the surrounding block. */
27166 stmt = push_stmt_list ();
27167 cp_parser_compound_statement (parser, NULL, false, false);
27168 objc_build_finally_clause (location, pop_stmt_list (stmt));
27171 return objc_finish_try_stmt ();
27174 /* Parse an Objective-C synchronized statement.
27176 objc-synchronized-stmt:
27177 @synchronized ( expression ) compound-statement
27179 Returns NULL_TREE. */
27181 static tree
27182 cp_parser_objc_synchronized_statement (cp_parser *parser)
27184 location_t location;
27185 tree lock, stmt;
27187 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27189 location = cp_lexer_peek_token (parser->lexer)->location;
27190 objc_maybe_warn_exceptions (location);
27191 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27192 lock = cp_parser_expression (parser);
27193 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27195 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27196 node, lest it get absorbed into the surrounding block. */
27197 stmt = push_stmt_list ();
27198 cp_parser_compound_statement (parser, NULL, false, false);
27200 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27203 /* Parse an Objective-C throw statement.
27205 objc-throw-stmt:
27206 @throw assignment-expression [opt] ;
27208 Returns a constructed '@throw' statement. */
27210 static tree
27211 cp_parser_objc_throw_statement (cp_parser *parser)
27213 tree expr = NULL_TREE;
27214 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27216 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27218 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27219 expr = cp_parser_expression (parser);
27221 cp_parser_consume_semicolon_at_end_of_statement (parser);
27223 return objc_build_throw_stmt (loc, expr);
27226 /* Parse an Objective-C statement. */
27228 static tree
27229 cp_parser_objc_statement (cp_parser * parser)
27231 /* Try to figure out what kind of declaration is present. */
27232 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27234 switch (kwd->keyword)
27236 case RID_AT_TRY:
27237 return cp_parser_objc_try_catch_finally_statement (parser);
27238 case RID_AT_SYNCHRONIZED:
27239 return cp_parser_objc_synchronized_statement (parser);
27240 case RID_AT_THROW:
27241 return cp_parser_objc_throw_statement (parser);
27242 default:
27243 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27244 kwd->u.value);
27245 cp_parser_skip_to_end_of_block_or_statement (parser);
27248 return error_mark_node;
27251 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27252 look ahead to see if an objc keyword follows the attributes. This
27253 is to detect the use of prefix attributes on ObjC @interface and
27254 @protocol. */
27256 static bool
27257 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27259 cp_lexer_save_tokens (parser->lexer);
27260 *attrib = cp_parser_attributes_opt (parser);
27261 gcc_assert (*attrib);
27262 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27264 cp_lexer_commit_tokens (parser->lexer);
27265 return true;
27267 cp_lexer_rollback_tokens (parser->lexer);
27268 return false;
27271 /* This routine is a minimal replacement for
27272 c_parser_struct_declaration () used when parsing the list of
27273 types/names or ObjC++ properties. For example, when parsing the
27274 code
27276 @property (readonly) int a, b, c;
27278 this function is responsible for parsing "int a, int b, int c" and
27279 returning the declarations as CHAIN of DECLs.
27281 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27282 similar parsing. */
27283 static tree
27284 cp_parser_objc_struct_declaration (cp_parser *parser)
27286 tree decls = NULL_TREE;
27287 cp_decl_specifier_seq declspecs;
27288 int decl_class_or_enum_p;
27289 tree prefix_attributes;
27291 cp_parser_decl_specifier_seq (parser,
27292 CP_PARSER_FLAGS_NONE,
27293 &declspecs,
27294 &decl_class_or_enum_p);
27296 if (declspecs.type == error_mark_node)
27297 return error_mark_node;
27299 /* auto, register, static, extern, mutable. */
27300 if (declspecs.storage_class != sc_none)
27302 cp_parser_error (parser, "invalid type for property");
27303 declspecs.storage_class = sc_none;
27306 /* thread_local. */
27307 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27309 cp_parser_error (parser, "invalid type for property");
27310 declspecs.locations[ds_thread] = 0;
27313 /* typedef. */
27314 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27316 cp_parser_error (parser, "invalid type for property");
27317 declspecs.locations[ds_typedef] = 0;
27320 prefix_attributes = declspecs.attributes;
27321 declspecs.attributes = NULL_TREE;
27323 /* Keep going until we hit the `;' at the end of the declaration. */
27324 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27326 tree attributes, first_attribute, decl;
27327 cp_declarator *declarator;
27328 cp_token *token;
27330 /* Parse the declarator. */
27331 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27332 NULL, NULL, false, false);
27334 /* Look for attributes that apply to the ivar. */
27335 attributes = cp_parser_attributes_opt (parser);
27336 /* Remember which attributes are prefix attributes and
27337 which are not. */
27338 first_attribute = attributes;
27339 /* Combine the attributes. */
27340 attributes = chainon (prefix_attributes, attributes);
27342 decl = grokfield (declarator, &declspecs,
27343 NULL_TREE, /*init_const_expr_p=*/false,
27344 NULL_TREE, attributes);
27346 if (decl == error_mark_node || decl == NULL_TREE)
27347 return error_mark_node;
27349 /* Reset PREFIX_ATTRIBUTES. */
27350 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27351 attributes = TREE_CHAIN (attributes);
27352 if (attributes)
27353 TREE_CHAIN (attributes) = NULL_TREE;
27355 DECL_CHAIN (decl) = decls;
27356 decls = decl;
27358 token = cp_lexer_peek_token (parser->lexer);
27359 if (token->type == CPP_COMMA)
27361 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27362 continue;
27364 else
27365 break;
27367 return decls;
27370 /* Parse an Objective-C @property declaration. The syntax is:
27372 objc-property-declaration:
27373 '@property' objc-property-attributes[opt] struct-declaration ;
27375 objc-property-attributes:
27376 '(' objc-property-attribute-list ')'
27378 objc-property-attribute-list:
27379 objc-property-attribute
27380 objc-property-attribute-list, objc-property-attribute
27382 objc-property-attribute
27383 'getter' = identifier
27384 'setter' = identifier
27385 'readonly'
27386 'readwrite'
27387 'assign'
27388 'retain'
27389 'copy'
27390 'nonatomic'
27392 For example:
27393 @property NSString *name;
27394 @property (readonly) id object;
27395 @property (retain, nonatomic, getter=getTheName) id name;
27396 @property int a, b, c;
27398 PS: This function is identical to
27399 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27400 static void
27401 cp_parser_objc_at_property_declaration (cp_parser *parser)
27403 /* The following variables hold the attributes of the properties as
27404 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27405 seen. When we see an attribute, we set them to 'true' (if they
27406 are boolean properties) or to the identifier (if they have an
27407 argument, ie, for getter and setter). Note that here we only
27408 parse the list of attributes, check the syntax and accumulate the
27409 attributes that we find. objc_add_property_declaration() will
27410 then process the information. */
27411 bool property_assign = false;
27412 bool property_copy = false;
27413 tree property_getter_ident = NULL_TREE;
27414 bool property_nonatomic = false;
27415 bool property_readonly = false;
27416 bool property_readwrite = false;
27417 bool property_retain = false;
27418 tree property_setter_ident = NULL_TREE;
27420 /* 'properties' is the list of properties that we read. Usually a
27421 single one, but maybe more (eg, in "@property int a, b, c;" there
27422 are three). */
27423 tree properties;
27424 location_t loc;
27426 loc = cp_lexer_peek_token (parser->lexer)->location;
27428 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27430 /* Parse the optional attribute list... */
27431 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27433 /* Eat the '('. */
27434 cp_lexer_consume_token (parser->lexer);
27436 while (true)
27438 bool syntax_error = false;
27439 cp_token *token = cp_lexer_peek_token (parser->lexer);
27440 enum rid keyword;
27442 if (token->type != CPP_NAME)
27444 cp_parser_error (parser, "expected identifier");
27445 break;
27447 keyword = C_RID_CODE (token->u.value);
27448 cp_lexer_consume_token (parser->lexer);
27449 switch (keyword)
27451 case RID_ASSIGN: property_assign = true; break;
27452 case RID_COPY: property_copy = true; break;
27453 case RID_NONATOMIC: property_nonatomic = true; break;
27454 case RID_READONLY: property_readonly = true; break;
27455 case RID_READWRITE: property_readwrite = true; break;
27456 case RID_RETAIN: property_retain = true; break;
27458 case RID_GETTER:
27459 case RID_SETTER:
27460 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27462 if (keyword == RID_GETTER)
27463 cp_parser_error (parser,
27464 "missing %<=%> (after %<getter%> attribute)");
27465 else
27466 cp_parser_error (parser,
27467 "missing %<=%> (after %<setter%> attribute)");
27468 syntax_error = true;
27469 break;
27471 cp_lexer_consume_token (parser->lexer); /* eat the = */
27472 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27474 cp_parser_error (parser, "expected identifier");
27475 syntax_error = true;
27476 break;
27478 if (keyword == RID_SETTER)
27480 if (property_setter_ident != NULL_TREE)
27482 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27483 cp_lexer_consume_token (parser->lexer);
27485 else
27486 property_setter_ident = cp_parser_objc_selector (parser);
27487 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27488 cp_parser_error (parser, "setter name must terminate with %<:%>");
27489 else
27490 cp_lexer_consume_token (parser->lexer);
27492 else
27494 if (property_getter_ident != NULL_TREE)
27496 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27497 cp_lexer_consume_token (parser->lexer);
27499 else
27500 property_getter_ident = cp_parser_objc_selector (parser);
27502 break;
27503 default:
27504 cp_parser_error (parser, "unknown property attribute");
27505 syntax_error = true;
27506 break;
27509 if (syntax_error)
27510 break;
27512 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27513 cp_lexer_consume_token (parser->lexer);
27514 else
27515 break;
27518 /* FIXME: "@property (setter, assign);" will generate a spurious
27519 "error: expected ‘)’ before ‘,’ token". This is because
27520 cp_parser_require, unlike the C counterpart, will produce an
27521 error even if we are in error recovery. */
27522 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27524 cp_parser_skip_to_closing_parenthesis (parser,
27525 /*recovering=*/true,
27526 /*or_comma=*/false,
27527 /*consume_paren=*/true);
27531 /* ... and the property declaration(s). */
27532 properties = cp_parser_objc_struct_declaration (parser);
27534 if (properties == error_mark_node)
27536 cp_parser_skip_to_end_of_statement (parser);
27537 /* If the next token is now a `;', consume it. */
27538 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27539 cp_lexer_consume_token (parser->lexer);
27540 return;
27543 if (properties == NULL_TREE)
27544 cp_parser_error (parser, "expected identifier");
27545 else
27547 /* Comma-separated properties are chained together in
27548 reverse order; add them one by one. */
27549 properties = nreverse (properties);
27551 for (; properties; properties = TREE_CHAIN (properties))
27552 objc_add_property_declaration (loc, copy_node (properties),
27553 property_readonly, property_readwrite,
27554 property_assign, property_retain,
27555 property_copy, property_nonatomic,
27556 property_getter_ident, property_setter_ident);
27559 cp_parser_consume_semicolon_at_end_of_statement (parser);
27562 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27564 objc-synthesize-declaration:
27565 @synthesize objc-synthesize-identifier-list ;
27567 objc-synthesize-identifier-list:
27568 objc-synthesize-identifier
27569 objc-synthesize-identifier-list, objc-synthesize-identifier
27571 objc-synthesize-identifier
27572 identifier
27573 identifier = identifier
27575 For example:
27576 @synthesize MyProperty;
27577 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27579 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27580 for C. Keep them in sync.
27582 static void
27583 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27585 tree list = NULL_TREE;
27586 location_t loc;
27587 loc = cp_lexer_peek_token (parser->lexer)->location;
27589 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27590 while (true)
27592 tree property, ivar;
27593 property = cp_parser_identifier (parser);
27594 if (property == error_mark_node)
27596 cp_parser_consume_semicolon_at_end_of_statement (parser);
27597 return;
27599 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27601 cp_lexer_consume_token (parser->lexer);
27602 ivar = cp_parser_identifier (parser);
27603 if (ivar == error_mark_node)
27605 cp_parser_consume_semicolon_at_end_of_statement (parser);
27606 return;
27609 else
27610 ivar = NULL_TREE;
27611 list = chainon (list, build_tree_list (ivar, property));
27612 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27613 cp_lexer_consume_token (parser->lexer);
27614 else
27615 break;
27617 cp_parser_consume_semicolon_at_end_of_statement (parser);
27618 objc_add_synthesize_declaration (loc, list);
27621 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27623 objc-dynamic-declaration:
27624 @dynamic identifier-list ;
27626 For example:
27627 @dynamic MyProperty;
27628 @dynamic MyProperty, AnotherProperty;
27630 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27631 for C. Keep them in sync.
27633 static void
27634 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27636 tree list = NULL_TREE;
27637 location_t loc;
27638 loc = cp_lexer_peek_token (parser->lexer)->location;
27640 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27641 while (true)
27643 tree property;
27644 property = cp_parser_identifier (parser);
27645 if (property == error_mark_node)
27647 cp_parser_consume_semicolon_at_end_of_statement (parser);
27648 return;
27650 list = chainon (list, build_tree_list (NULL, property));
27651 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27652 cp_lexer_consume_token (parser->lexer);
27653 else
27654 break;
27656 cp_parser_consume_semicolon_at_end_of_statement (parser);
27657 objc_add_dynamic_declaration (loc, list);
27661 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27663 /* Returns name of the next clause.
27664 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27665 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27666 returned and the token is consumed. */
27668 static pragma_omp_clause
27669 cp_parser_omp_clause_name (cp_parser *parser)
27671 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27673 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27674 result = PRAGMA_OMP_CLAUSE_IF;
27675 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27676 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27677 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
27678 result = PRAGMA_OACC_CLAUSE_DELETE;
27679 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27680 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27681 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27682 result = PRAGMA_OMP_CLAUSE_FOR;
27683 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27685 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27686 const char *p = IDENTIFIER_POINTER (id);
27688 switch (p[0])
27690 case 'a':
27691 if (!strcmp ("aligned", p))
27692 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27693 else if (!strcmp ("async", p))
27694 result = PRAGMA_OACC_CLAUSE_ASYNC;
27695 break;
27696 case 'c':
27697 if (!strcmp ("collapse", p))
27698 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27699 else if (!strcmp ("copy", p))
27700 result = PRAGMA_OACC_CLAUSE_COPY;
27701 else if (!strcmp ("copyin", p))
27702 result = PRAGMA_OMP_CLAUSE_COPYIN;
27703 else if (!strcmp ("copyout", p))
27704 result = PRAGMA_OACC_CLAUSE_COPYOUT;
27705 else if (!strcmp ("copyprivate", p))
27706 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27707 else if (!strcmp ("create", p))
27708 result = PRAGMA_OACC_CLAUSE_CREATE;
27709 break;
27710 case 'd':
27711 if (!strcmp ("defaultmap", p))
27712 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
27713 else if (!strcmp ("depend", p))
27714 result = PRAGMA_OMP_CLAUSE_DEPEND;
27715 else if (!strcmp ("device", p))
27716 result = PRAGMA_OMP_CLAUSE_DEVICE;
27717 else if (!strcmp ("deviceptr", p))
27718 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
27719 else if (!strcmp ("dist_schedule", p))
27720 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27721 break;
27722 case 'f':
27723 if (!strcmp ("final", p))
27724 result = PRAGMA_OMP_CLAUSE_FINAL;
27725 else if (!strcmp ("firstprivate", p))
27726 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27727 else if (!strcmp ("from", p))
27728 result = PRAGMA_OMP_CLAUSE_FROM;
27729 break;
27730 case 'g':
27731 if (!strcmp ("grainsize", p))
27732 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
27733 break;
27734 case 'h':
27735 if (!strcmp ("hint", p))
27736 result = PRAGMA_OMP_CLAUSE_HINT;
27737 else if (!strcmp ("host", p))
27738 result = PRAGMA_OACC_CLAUSE_HOST;
27739 break;
27740 case 'i':
27741 if (!strcmp ("inbranch", p))
27742 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27743 else if (!strcmp ("is_device_ptr", p))
27744 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
27745 break;
27746 case 'l':
27747 if (!strcmp ("lastprivate", p))
27748 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27749 else if (!strcmp ("linear", p))
27750 result = PRAGMA_OMP_CLAUSE_LINEAR;
27751 else if (!strcmp ("link", p))
27752 result = PRAGMA_OMP_CLAUSE_LINK;
27753 break;
27754 case 'm':
27755 if (!strcmp ("map", p))
27756 result = PRAGMA_OMP_CLAUSE_MAP;
27757 else if (!strcmp ("mergeable", p))
27758 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27759 else if (flag_cilkplus && !strcmp ("mask", p))
27760 result = PRAGMA_CILK_CLAUSE_MASK;
27761 break;
27762 case 'n':
27763 if (!strcmp ("nogroup", p))
27764 result = PRAGMA_OMP_CLAUSE_NOGROUP;
27765 else if (!strcmp ("notinbranch", p))
27766 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27767 else if (!strcmp ("nowait", p))
27768 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27769 else if (flag_cilkplus && !strcmp ("nomask", p))
27770 result = PRAGMA_CILK_CLAUSE_NOMASK;
27771 else if (!strcmp ("num_gangs", p))
27772 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
27773 else if (!strcmp ("num_tasks", p))
27774 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
27775 else if (!strcmp ("num_teams", p))
27776 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27777 else if (!strcmp ("num_threads", p))
27778 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27779 else if (!strcmp ("num_workers", p))
27780 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
27781 break;
27782 case 'o':
27783 if (!strcmp ("ordered", p))
27784 result = PRAGMA_OMP_CLAUSE_ORDERED;
27785 break;
27786 case 'p':
27787 if (!strcmp ("parallel", p))
27788 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27789 else if (!strcmp ("present", p))
27790 result = PRAGMA_OACC_CLAUSE_PRESENT;
27791 else if (!strcmp ("present_or_copy", p)
27792 || !strcmp ("pcopy", p))
27793 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
27794 else if (!strcmp ("present_or_copyin", p)
27795 || !strcmp ("pcopyin", p))
27796 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
27797 else if (!strcmp ("present_or_copyout", p)
27798 || !strcmp ("pcopyout", p))
27799 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
27800 else if (!strcmp ("present_or_create", p)
27801 || !strcmp ("pcreate", p))
27802 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
27803 else if (!strcmp ("priority", p))
27804 result = PRAGMA_OMP_CLAUSE_PRIORITY;
27805 else if (!strcmp ("proc_bind", p))
27806 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27807 break;
27808 case 'r':
27809 if (!strcmp ("reduction", p))
27810 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27811 break;
27812 case 's':
27813 if (!strcmp ("safelen", p))
27814 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27815 else if (!strcmp ("schedule", p))
27816 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27817 else if (!strcmp ("sections", p))
27818 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27819 else if (!strcmp ("self", p))
27820 result = PRAGMA_OACC_CLAUSE_SELF;
27821 else if (!strcmp ("shared", p))
27822 result = PRAGMA_OMP_CLAUSE_SHARED;
27823 else if (!strcmp ("simd", p))
27824 result = PRAGMA_OMP_CLAUSE_SIMD;
27825 else if (!strcmp ("simdlen", p))
27826 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27827 break;
27828 case 't':
27829 if (!strcmp ("taskgroup", p))
27830 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27831 else if (!strcmp ("thread_limit", p))
27832 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27833 else if (!strcmp ("threads", p))
27834 result = PRAGMA_OMP_CLAUSE_THREADS;
27835 else if (!strcmp ("to", p))
27836 result = PRAGMA_OMP_CLAUSE_TO;
27837 break;
27838 case 'u':
27839 if (!strcmp ("uniform", p))
27840 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27841 else if (!strcmp ("untied", p))
27842 result = PRAGMA_OMP_CLAUSE_UNTIED;
27843 else if (!strcmp ("use_device_ptr", p))
27844 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
27845 break;
27846 case 'v':
27847 if (!strcmp ("vector_length", p))
27848 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
27849 else if (flag_cilkplus && !strcmp ("vectorlength", p))
27850 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27851 break;
27852 case 'w':
27853 if (!strcmp ("wait", p))
27854 result = PRAGMA_OACC_CLAUSE_WAIT;
27855 break;
27859 if (result != PRAGMA_OMP_CLAUSE_NONE)
27860 cp_lexer_consume_token (parser->lexer);
27862 return result;
27865 /* Validate that a clause of the given type does not already exist. */
27867 static void
27868 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27869 const char *name, location_t location)
27871 tree c;
27873 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27874 if (OMP_CLAUSE_CODE (c) == code)
27876 error_at (location, "too many %qs clauses", name);
27877 break;
27881 /* OpenMP 2.5:
27882 variable-list:
27883 identifier
27884 variable-list , identifier
27886 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27887 colon). An opening parenthesis will have been consumed by the caller.
27889 If KIND is nonzero, create the appropriate node and install the decl
27890 in OMP_CLAUSE_DECL and add the node to the head of the list.
27892 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27893 return the list created.
27895 COLON can be NULL if only closing parenthesis should end the list,
27896 or pointer to bool which will receive false if the list is terminated
27897 by closing parenthesis or true if the list is terminated by colon. */
27899 static tree
27900 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27901 tree list, bool *colon)
27903 cp_token *token;
27904 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27905 if (colon)
27907 parser->colon_corrects_to_scope_p = false;
27908 *colon = false;
27910 while (1)
27912 tree name, decl;
27914 token = cp_lexer_peek_token (parser->lexer);
27915 if (kind != 0
27916 && current_class_ptr
27917 && cp_parser_is_keyword (token, RID_THIS))
27919 decl = finish_this_expr ();
27920 if (TREE_CODE (decl) == NON_LVALUE_EXPR
27921 || CONVERT_EXPR_P (decl))
27922 decl = TREE_OPERAND (decl, 0);
27923 cp_lexer_consume_token (parser->lexer);
27925 else
27927 name = cp_parser_id_expression (parser, /*template_p=*/false,
27928 /*check_dependency_p=*/true,
27929 /*template_p=*/NULL,
27930 /*declarator_p=*/false,
27931 /*optional_p=*/false);
27932 if (name == error_mark_node)
27933 goto skip_comma;
27935 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27936 if (decl == error_mark_node)
27937 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27938 token->location);
27940 if (decl == error_mark_node)
27942 else if (kind != 0)
27944 switch (kind)
27946 case OMP_CLAUSE__CACHE_:
27947 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27949 error_at (token->location, "expected %<[%>");
27950 decl = error_mark_node;
27951 break;
27953 /* FALL THROUGH. */
27954 case OMP_CLAUSE_MAP:
27955 case OMP_CLAUSE_FROM:
27956 case OMP_CLAUSE_TO:
27957 case OMP_CLAUSE_DEPEND:
27958 case OMP_CLAUSE_REDUCTION:
27959 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27961 tree low_bound = NULL_TREE, length = NULL_TREE;
27963 parser->colon_corrects_to_scope_p = false;
27964 cp_lexer_consume_token (parser->lexer);
27965 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27966 low_bound = cp_parser_expression (parser);
27967 if (!colon)
27968 parser->colon_corrects_to_scope_p
27969 = saved_colon_corrects_to_scope_p;
27970 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27971 length = integer_one_node;
27972 else
27974 /* Look for `:'. */
27975 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27976 goto skip_comma;
27977 if (!cp_lexer_next_token_is (parser->lexer,
27978 CPP_CLOSE_SQUARE))
27979 length = cp_parser_expression (parser);
27981 /* Look for the closing `]'. */
27982 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27983 RT_CLOSE_SQUARE))
27984 goto skip_comma;
27986 if (kind == OMP_CLAUSE__CACHE_)
27988 if (TREE_CODE (low_bound) != INTEGER_CST
27989 && !TREE_READONLY (low_bound))
27991 error_at (token->location,
27992 "%qD is not a constant", low_bound);
27993 decl = error_mark_node;
27996 if (TREE_CODE (length) != INTEGER_CST
27997 && !TREE_READONLY (length))
27999 error_at (token->location,
28000 "%qD is not a constant", length);
28001 decl = error_mark_node;
28005 decl = tree_cons (low_bound, length, decl);
28007 break;
28008 default:
28009 break;
28012 tree u = build_omp_clause (token->location, kind);
28013 OMP_CLAUSE_DECL (u) = decl;
28014 OMP_CLAUSE_CHAIN (u) = list;
28015 list = u;
28017 else
28018 list = tree_cons (decl, NULL_TREE, list);
28020 get_comma:
28021 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28022 break;
28023 cp_lexer_consume_token (parser->lexer);
28026 if (colon)
28027 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28029 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28031 *colon = true;
28032 cp_parser_require (parser, CPP_COLON, RT_COLON);
28033 return list;
28036 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28038 int ending;
28040 /* Try to resync to an unnested comma. Copied from
28041 cp_parser_parenthesized_expression_list. */
28042 skip_comma:
28043 if (colon)
28044 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28045 ending = cp_parser_skip_to_closing_parenthesis (parser,
28046 /*recovering=*/true,
28047 /*or_comma=*/true,
28048 /*consume_paren=*/true);
28049 if (ending < 0)
28050 goto get_comma;
28053 return list;
28056 /* Similarly, but expect leading and trailing parenthesis. This is a very
28057 common case for omp clauses. */
28059 static tree
28060 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
28062 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28063 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
28064 return list;
28067 /* OpenACC 2.0:
28068 copy ( variable-list )
28069 copyin ( variable-list )
28070 copyout ( variable-list )
28071 create ( variable-list )
28072 delete ( variable-list )
28073 present ( variable-list )
28074 present_or_copy ( variable-list )
28075 pcopy ( variable-list )
28076 present_or_copyin ( variable-list )
28077 pcopyin ( variable-list )
28078 present_or_copyout ( variable-list )
28079 pcopyout ( variable-list )
28080 present_or_create ( variable-list )
28081 pcreate ( variable-list ) */
28083 static tree
28084 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
28085 tree list)
28087 enum gomp_map_kind kind;
28088 switch (c_kind)
28090 case PRAGMA_OACC_CLAUSE_COPY:
28091 kind = GOMP_MAP_FORCE_TOFROM;
28092 break;
28093 case PRAGMA_OACC_CLAUSE_COPYIN:
28094 kind = GOMP_MAP_FORCE_TO;
28095 break;
28096 case PRAGMA_OACC_CLAUSE_COPYOUT:
28097 kind = GOMP_MAP_FORCE_FROM;
28098 break;
28099 case PRAGMA_OACC_CLAUSE_CREATE:
28100 kind = GOMP_MAP_FORCE_ALLOC;
28101 break;
28102 case PRAGMA_OACC_CLAUSE_DELETE:
28103 kind = GOMP_MAP_FORCE_DEALLOC;
28104 break;
28105 case PRAGMA_OACC_CLAUSE_DEVICE:
28106 kind = GOMP_MAP_FORCE_TO;
28107 break;
28108 case PRAGMA_OACC_CLAUSE_HOST:
28109 case PRAGMA_OACC_CLAUSE_SELF:
28110 kind = GOMP_MAP_FORCE_FROM;
28111 break;
28112 case PRAGMA_OACC_CLAUSE_PRESENT:
28113 kind = GOMP_MAP_FORCE_PRESENT;
28114 break;
28115 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
28116 kind = GOMP_MAP_TOFROM;
28117 break;
28118 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
28119 kind = GOMP_MAP_TO;
28120 break;
28121 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
28122 kind = GOMP_MAP_FROM;
28123 break;
28124 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
28125 kind = GOMP_MAP_ALLOC;
28126 break;
28127 default:
28128 gcc_unreachable ();
28130 tree nl, c;
28131 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
28133 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
28134 OMP_CLAUSE_SET_MAP_KIND (c, kind);
28136 return nl;
28139 /* OpenACC 2.0:
28140 deviceptr ( variable-list ) */
28142 static tree
28143 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
28145 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28146 tree vars, t;
28148 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
28149 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
28150 variable-list must only allow for pointer variables. */
28151 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28152 for (t = vars; t; t = TREE_CHAIN (t))
28154 tree v = TREE_PURPOSE (t);
28156 /* FIXME diagnostics: Ideally we should keep individual
28157 locations for all the variables in the var list to make the
28158 following errors more precise. Perhaps
28159 c_parser_omp_var_list_parens should construct a list of
28160 locations to go along with the var list. */
28162 if (!VAR_P (v))
28163 error_at (loc, "%qD is not a variable", v);
28164 else if (TREE_TYPE (v) == error_mark_node)
28166 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
28167 error_at (loc, "%qD is not a pointer variable", v);
28169 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
28170 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
28171 OMP_CLAUSE_DECL (u) = v;
28172 OMP_CLAUSE_CHAIN (u) = list;
28173 list = u;
28176 return list;
28179 /* OpenACC:
28180 vector_length ( expression ) */
28182 static tree
28183 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
28185 tree t, c;
28186 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28187 bool error = false;
28189 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28190 return list;
28192 t = cp_parser_condition (parser);
28193 if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
28195 error_at (location, "expected positive integer expression");
28196 error = true;
28199 if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28201 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28202 /*or_comma=*/false,
28203 /*consume_paren=*/true);
28204 return list;
28207 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
28208 location);
28210 c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
28211 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
28212 OMP_CLAUSE_CHAIN (c) = list;
28213 list = c;
28215 return list;
28218 /* OpenACC 2.0
28219 Parse wait clause or directive parameters. */
28221 static tree
28222 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
28224 vec<tree, va_gc> *args;
28225 tree t, args_tree;
28227 args = cp_parser_parenthesized_expression_list (parser, non_attr,
28228 /*cast_p=*/false,
28229 /*allow_expansion_p=*/true,
28230 /*non_constant_p=*/NULL);
28232 if (args == NULL || args->length () == 0)
28234 cp_parser_error (parser, "expected integer expression before ')'");
28235 if (args != NULL)
28236 release_tree_vector (args);
28237 return list;
28240 args_tree = build_tree_list_vec (args);
28242 release_tree_vector (args);
28244 for (t = args_tree; t; t = TREE_CHAIN (t))
28246 tree targ = TREE_VALUE (t);
28248 if (targ != error_mark_node)
28250 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
28251 error ("%<wait%> expression must be integral");
28252 else
28254 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
28256 mark_rvalue_use (targ);
28257 OMP_CLAUSE_DECL (c) = targ;
28258 OMP_CLAUSE_CHAIN (c) = list;
28259 list = c;
28264 return list;
28267 /* OpenACC:
28268 wait ( int-expr-list ) */
28270 static tree
28271 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
28273 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28275 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
28276 return list;
28278 list = cp_parser_oacc_wait_list (parser, location, list);
28280 return list;
28283 /* OpenMP 3.0:
28284 collapse ( constant-expression ) */
28286 static tree
28287 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28289 tree c, num;
28290 location_t loc;
28291 HOST_WIDE_INT n;
28293 loc = cp_lexer_peek_token (parser->lexer)->location;
28294 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28295 return list;
28297 num = cp_parser_constant_expression (parser);
28299 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28300 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28301 /*or_comma=*/false,
28302 /*consume_paren=*/true);
28304 if (num == error_mark_node)
28305 return list;
28306 num = fold_non_dependent_expr (num);
28307 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28308 || !tree_fits_shwi_p (num)
28309 || (n = tree_to_shwi (num)) <= 0
28310 || (int) n != n)
28312 error_at (loc, "collapse argument needs positive constant integer expression");
28313 return list;
28316 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28317 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28318 OMP_CLAUSE_CHAIN (c) = list;
28319 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28321 return c;
28324 /* OpenMP 2.5:
28325 default ( shared | none ) */
28327 static tree
28328 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28330 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28331 tree c;
28333 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28334 return list;
28335 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28337 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28338 const char *p = IDENTIFIER_POINTER (id);
28340 switch (p[0])
28342 case 'n':
28343 if (strcmp ("none", p) != 0)
28344 goto invalid_kind;
28345 kind = OMP_CLAUSE_DEFAULT_NONE;
28346 break;
28348 case 's':
28349 if (strcmp ("shared", p) != 0)
28350 goto invalid_kind;
28351 kind = OMP_CLAUSE_DEFAULT_SHARED;
28352 break;
28354 default:
28355 goto invalid_kind;
28358 cp_lexer_consume_token (parser->lexer);
28360 else
28362 invalid_kind:
28363 cp_parser_error (parser, "expected %<none%> or %<shared%>");
28366 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28367 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28368 /*or_comma=*/false,
28369 /*consume_paren=*/true);
28371 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28372 return list;
28374 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28375 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28376 OMP_CLAUSE_CHAIN (c) = list;
28377 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28379 return c;
28382 /* OpenMP 3.1:
28383 final ( expression ) */
28385 static tree
28386 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28388 tree t, c;
28390 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28391 return list;
28393 t = cp_parser_condition (parser);
28395 if (t == error_mark_node
28396 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28397 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28398 /*or_comma=*/false,
28399 /*consume_paren=*/true);
28401 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28403 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28404 OMP_CLAUSE_FINAL_EXPR (c) = t;
28405 OMP_CLAUSE_CHAIN (c) = list;
28407 return c;
28410 /* OpenMP 2.5:
28411 if ( expression )
28413 OpenMP 4.1:
28414 if ( directive-name-modifier : expression )
28416 directive-name-modifier:
28417 parallel | task | taskloop | target data | target | target update
28418 | target enter data | target exit data */
28420 static tree
28421 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
28422 bool is_omp)
28424 tree t, c;
28425 enum tree_code if_modifier = ERROR_MARK;
28427 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28428 return list;
28430 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28432 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28433 const char *p = IDENTIFIER_POINTER (id);
28434 int n = 2;
28436 if (strcmp ("parallel", p) == 0)
28437 if_modifier = OMP_PARALLEL;
28438 else if (strcmp ("task", p) == 0)
28439 if_modifier = OMP_TASK;
28440 else if (strcmp ("taskloop", p) == 0)
28441 if_modifier = OMP_TASKLOOP;
28442 else if (strcmp ("target", p) == 0)
28444 if_modifier = OMP_TARGET;
28445 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
28447 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
28448 p = IDENTIFIER_POINTER (id);
28449 if (strcmp ("data", p) == 0)
28450 if_modifier = OMP_TARGET_DATA;
28451 else if (strcmp ("update", p) == 0)
28452 if_modifier = OMP_TARGET_UPDATE;
28453 else if (strcmp ("enter", p) == 0)
28454 if_modifier = OMP_TARGET_ENTER_DATA;
28455 else if (strcmp ("exit", p) == 0)
28456 if_modifier = OMP_TARGET_EXIT_DATA;
28457 if (if_modifier != OMP_TARGET)
28458 n = 3;
28459 else
28461 location_t loc
28462 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
28463 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
28464 "or %<exit%>");
28465 if_modifier = ERROR_MARK;
28467 if (if_modifier == OMP_TARGET_ENTER_DATA
28468 || if_modifier == OMP_TARGET_EXIT_DATA)
28470 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
28472 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
28473 p = IDENTIFIER_POINTER (id);
28474 if (strcmp ("data", p) == 0)
28475 n = 4;
28477 if (n != 4)
28479 location_t loc
28480 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
28481 error_at (loc, "expected %<data%>");
28482 if_modifier = ERROR_MARK;
28487 if (if_modifier != ERROR_MARK)
28489 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
28491 while (n-- > 0)
28492 cp_lexer_consume_token (parser->lexer);
28494 else
28496 if (n > 2)
28498 location_t loc
28499 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
28500 error_at (loc, "expected %<:%>");
28502 if_modifier = ERROR_MARK;
28507 t = cp_parser_condition (parser);
28509 if (t == error_mark_node
28510 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28511 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28512 /*or_comma=*/false,
28513 /*consume_paren=*/true);
28515 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
28516 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
28518 if (if_modifier != ERROR_MARK
28519 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
28521 const char *p = NULL;
28522 switch (if_modifier)
28524 case OMP_PARALLEL: p = "parallel"; break;
28525 case OMP_TASK: p = "task"; break;
28526 case OMP_TASKLOOP: p = "taskloop"; break;
28527 case OMP_TARGET_DATA: p = "target data"; break;
28528 case OMP_TARGET: p = "target"; break;
28529 case OMP_TARGET_UPDATE: p = "target update"; break;
28530 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
28531 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
28532 default: gcc_unreachable ();
28534 error_at (location, "too many %<if%> clauses with %qs modifier",
28536 return list;
28538 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
28540 if (!is_omp)
28541 error_at (location, "too many %<if%> clauses");
28542 else
28543 error_at (location, "too many %<if%> clauses without modifier");
28544 return list;
28546 else if (if_modifier == ERROR_MARK
28547 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
28549 error_at (location, "if any %<if%> clause has modifier, then all "
28550 "%<if%> clauses have to use modifier");
28551 return list;
28555 c = build_omp_clause (location, OMP_CLAUSE_IF);
28556 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
28557 OMP_CLAUSE_IF_EXPR (c) = t;
28558 OMP_CLAUSE_CHAIN (c) = list;
28560 return c;
28563 /* OpenMP 3.1:
28564 mergeable */
28566 static tree
28567 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28568 tree list, location_t location)
28570 tree c;
28572 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28573 location);
28575 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28576 OMP_CLAUSE_CHAIN (c) = list;
28577 return c;
28580 /* OpenMP 2.5:
28581 nowait */
28583 static tree
28584 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28585 tree list, location_t location)
28587 tree c;
28589 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28591 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28592 OMP_CLAUSE_CHAIN (c) = list;
28593 return c;
28596 /* OpenACC:
28597 num_gangs ( expression ) */
28599 static tree
28600 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28602 tree t, c;
28603 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28605 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28606 return list;
28608 t = cp_parser_condition (parser);
28610 if (t == error_mark_node
28611 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28612 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28613 /*or_comma=*/false,
28614 /*consume_paren=*/true);
28616 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28618 error_at (location, "expected positive integer expression");
28619 return list;
28622 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28624 c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28625 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28626 OMP_CLAUSE_CHAIN (c) = list;
28627 list = c;
28629 return list;
28632 /* OpenMP 2.5:
28633 num_threads ( expression ) */
28635 static tree
28636 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28637 location_t location)
28639 tree t, c;
28641 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28642 return list;
28644 t = cp_parser_expression (parser);
28646 if (t == error_mark_node
28647 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28648 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28649 /*or_comma=*/false,
28650 /*consume_paren=*/true);
28652 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28653 "num_threads", location);
28655 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28656 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28657 OMP_CLAUSE_CHAIN (c) = list;
28659 return c;
28662 /* OpenMP 4.1:
28663 num_tasks ( expression ) */
28665 static tree
28666 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
28667 location_t location)
28669 tree t, c;
28671 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28672 return list;
28674 t = cp_parser_expression (parser);
28676 if (t == error_mark_node
28677 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28678 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28679 /*or_comma=*/false,
28680 /*consume_paren=*/true);
28682 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
28683 "num_tasks", location);
28685 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
28686 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
28687 OMP_CLAUSE_CHAIN (c) = list;
28689 return c;
28692 /* OpenMP 4.1:
28693 grainsize ( expression ) */
28695 static tree
28696 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
28697 location_t location)
28699 tree t, c;
28701 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28702 return list;
28704 t = cp_parser_expression (parser);
28706 if (t == error_mark_node
28707 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28708 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28709 /*or_comma=*/false,
28710 /*consume_paren=*/true);
28712 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
28713 "grainsize", location);
28715 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
28716 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
28717 OMP_CLAUSE_CHAIN (c) = list;
28719 return c;
28722 /* OpenMP 4.1:
28723 priority ( expression ) */
28725 static tree
28726 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
28727 location_t location)
28729 tree t, c;
28731 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28732 return list;
28734 t = cp_parser_expression (parser);
28736 if (t == error_mark_node
28737 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28738 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28739 /*or_comma=*/false,
28740 /*consume_paren=*/true);
28742 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
28743 "priority", location);
28745 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
28746 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
28747 OMP_CLAUSE_CHAIN (c) = list;
28749 return c;
28752 /* OpenMP 4.1:
28753 hint ( expression ) */
28755 static tree
28756 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
28757 location_t location)
28759 tree t, c;
28761 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28762 return list;
28764 t = cp_parser_expression (parser);
28766 if (t == error_mark_node
28767 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28768 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28769 /*or_comma=*/false,
28770 /*consume_paren=*/true);
28772 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
28774 c = build_omp_clause (location, OMP_CLAUSE_HINT);
28775 OMP_CLAUSE_HINT_EXPR (c) = t;
28776 OMP_CLAUSE_CHAIN (c) = list;
28778 return c;
28781 /* OpenMP 4.1:
28782 defaultmap ( tofrom : scalar ) */
28784 static tree
28785 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
28786 location_t location)
28788 tree c, id;
28789 const char *p;
28791 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28792 return list;
28794 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28796 cp_parser_error (parser, "expected %<tofrom%>");
28797 goto out_err;
28799 id = cp_lexer_peek_token (parser->lexer)->u.value;
28800 p = IDENTIFIER_POINTER (id);
28801 if (strcmp (p, "tofrom") != 0)
28803 cp_parser_error (parser, "expected %<tofrom%>");
28804 goto out_err;
28806 cp_lexer_consume_token (parser->lexer);
28807 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28808 goto out_err;
28810 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28812 cp_parser_error (parser, "expected %<scalar%>");
28813 goto out_err;
28815 id = cp_lexer_peek_token (parser->lexer)->u.value;
28816 p = IDENTIFIER_POINTER (id);
28817 if (strcmp (p, "scalar") != 0)
28819 cp_parser_error (parser, "expected %<scalar%>");
28820 goto out_err;
28822 cp_lexer_consume_token (parser->lexer);
28823 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28824 goto out_err;
28826 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
28827 location);
28829 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
28830 OMP_CLAUSE_CHAIN (c) = list;
28831 return c;
28833 out_err:
28834 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28835 /*or_comma=*/false,
28836 /*consume_paren=*/true);
28837 return list;
28840 /* OpenACC:
28841 num_workers ( expression ) */
28843 static tree
28844 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28846 tree t, c;
28847 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28849 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28850 return list;
28852 t = cp_parser_condition (parser);
28854 if (t == error_mark_node
28855 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28856 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28857 /*or_comma=*/false,
28858 /*consume_paren=*/true);
28860 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28862 error_at (location, "expected positive integer expression");
28863 return list;
28866 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28867 location);
28869 c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28870 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28871 OMP_CLAUSE_CHAIN (c) = list;
28872 list = c;
28874 return list;
28877 /* OpenMP 2.5:
28878 ordered
28880 OpenMP 4.1:
28881 ordered ( constant-expression ) */
28883 static tree
28884 cp_parser_omp_clause_ordered (cp_parser *parser,
28885 tree list, location_t location)
28887 tree c, num = NULL_TREE;
28888 HOST_WIDE_INT n;
28890 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28891 "ordered", location);
28893 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28895 cp_lexer_consume_token (parser->lexer);
28897 num = cp_parser_constant_expression (parser);
28899 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28900 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28901 /*or_comma=*/false,
28902 /*consume_paren=*/true);
28904 if (num == error_mark_node)
28905 return list;
28906 num = fold_non_dependent_expr (num);
28907 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28908 || !tree_fits_shwi_p (num)
28909 || (n = tree_to_shwi (num)) <= 0
28910 || (int) n != n)
28912 error_at (location,
28913 "ordered argument needs positive constant integer "
28914 "expression");
28915 return list;
28919 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28920 OMP_CLAUSE_ORDERED_EXPR (c) = num;
28921 OMP_CLAUSE_CHAIN (c) = list;
28922 return c;
28925 /* OpenMP 2.5:
28926 reduction ( reduction-operator : variable-list )
28928 reduction-operator:
28929 One of: + * - & ^ | && ||
28931 OpenMP 3.1:
28933 reduction-operator:
28934 One of: + * - & ^ | && || min max
28936 OpenMP 4.0:
28938 reduction-operator:
28939 One of: + * - & ^ | && ||
28940 id-expression */
28942 static tree
28943 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28945 enum tree_code code = ERROR_MARK;
28946 tree nlist, c, id = NULL_TREE;
28948 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28949 return list;
28951 switch (cp_lexer_peek_token (parser->lexer)->type)
28953 case CPP_PLUS: code = PLUS_EXPR; break;
28954 case CPP_MULT: code = MULT_EXPR; break;
28955 case CPP_MINUS: code = MINUS_EXPR; break;
28956 case CPP_AND: code = BIT_AND_EXPR; break;
28957 case CPP_XOR: code = BIT_XOR_EXPR; break;
28958 case CPP_OR: code = BIT_IOR_EXPR; break;
28959 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28960 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28961 default: break;
28964 if (code != ERROR_MARK)
28965 cp_lexer_consume_token (parser->lexer);
28966 else
28968 bool saved_colon_corrects_to_scope_p;
28969 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28970 parser->colon_corrects_to_scope_p = false;
28971 id = cp_parser_id_expression (parser, /*template_p=*/false,
28972 /*check_dependency_p=*/true,
28973 /*template_p=*/NULL,
28974 /*declarator_p=*/false,
28975 /*optional_p=*/false);
28976 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28977 if (identifier_p (id))
28979 const char *p = IDENTIFIER_POINTER (id);
28981 if (strcmp (p, "min") == 0)
28982 code = MIN_EXPR;
28983 else if (strcmp (p, "max") == 0)
28984 code = MAX_EXPR;
28985 else if (id == ansi_opname (PLUS_EXPR))
28986 code = PLUS_EXPR;
28987 else if (id == ansi_opname (MULT_EXPR))
28988 code = MULT_EXPR;
28989 else if (id == ansi_opname (MINUS_EXPR))
28990 code = MINUS_EXPR;
28991 else if (id == ansi_opname (BIT_AND_EXPR))
28992 code = BIT_AND_EXPR;
28993 else if (id == ansi_opname (BIT_IOR_EXPR))
28994 code = BIT_IOR_EXPR;
28995 else if (id == ansi_opname (BIT_XOR_EXPR))
28996 code = BIT_XOR_EXPR;
28997 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28998 code = TRUTH_ANDIF_EXPR;
28999 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
29000 code = TRUTH_ORIF_EXPR;
29001 id = omp_reduction_id (code, id, NULL_TREE);
29002 tree scope = parser->scope;
29003 if (scope)
29004 id = build_qualified_name (NULL_TREE, scope, id, false);
29005 parser->scope = NULL_TREE;
29006 parser->qualifying_scope = NULL_TREE;
29007 parser->object_scope = NULL_TREE;
29009 else
29011 error ("invalid reduction-identifier");
29012 resync_fail:
29013 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29014 /*or_comma=*/false,
29015 /*consume_paren=*/true);
29016 return list;
29020 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29021 goto resync_fail;
29023 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
29024 NULL);
29025 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29027 OMP_CLAUSE_REDUCTION_CODE (c) = code;
29028 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
29031 return nlist;
29034 /* OpenMP 2.5:
29035 schedule ( schedule-kind )
29036 schedule ( schedule-kind , expression )
29038 schedule-kind:
29039 static | dynamic | guided | runtime | auto
29041 OpenMP 4.1:
29042 schedule ( schedule-modifier : schedule-kind )
29043 schedule ( schedule-modifier : schedule-kind , expression )
29045 schedule-modifier:
29046 simd */
29048 static tree
29049 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
29051 tree c, t;
29053 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29054 return list;
29056 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
29058 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29060 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29061 const char *p = IDENTIFIER_POINTER (id);
29062 if (strcmp ("simd", p) == 0
29063 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
29065 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
29066 cp_lexer_consume_token (parser->lexer);
29067 cp_lexer_consume_token (parser->lexer);
29071 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29073 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29074 const char *p = IDENTIFIER_POINTER (id);
29076 switch (p[0])
29078 case 'd':
29079 if (strcmp ("dynamic", p) != 0)
29080 goto invalid_kind;
29081 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
29082 break;
29084 case 'g':
29085 if (strcmp ("guided", p) != 0)
29086 goto invalid_kind;
29087 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
29088 break;
29090 case 'r':
29091 if (strcmp ("runtime", p) != 0)
29092 goto invalid_kind;
29093 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
29094 break;
29096 default:
29097 goto invalid_kind;
29100 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29101 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
29102 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29103 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
29104 else
29105 goto invalid_kind;
29106 cp_lexer_consume_token (parser->lexer);
29108 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29110 cp_token *token;
29111 cp_lexer_consume_token (parser->lexer);
29113 token = cp_lexer_peek_token (parser->lexer);
29114 t = cp_parser_assignment_expression (parser);
29116 if (t == error_mark_node)
29117 goto resync_fail;
29118 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
29119 error_at (token->location, "schedule %<runtime%> does not take "
29120 "a %<chunk_size%> parameter");
29121 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
29122 error_at (token->location, "schedule %<auto%> does not take "
29123 "a %<chunk_size%> parameter");
29124 else
29125 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
29127 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29128 goto resync_fail;
29130 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29131 goto resync_fail;
29133 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
29134 OMP_CLAUSE_CHAIN (c) = list;
29135 return c;
29137 invalid_kind:
29138 cp_parser_error (parser, "invalid schedule kind");
29139 resync_fail:
29140 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29141 /*or_comma=*/false,
29142 /*consume_paren=*/true);
29143 return list;
29146 /* OpenMP 3.0:
29147 untied */
29149 static tree
29150 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
29151 tree list, location_t location)
29153 tree c;
29155 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
29157 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
29158 OMP_CLAUSE_CHAIN (c) = list;
29159 return c;
29162 /* OpenMP 4.0:
29163 inbranch
29164 notinbranch */
29166 static tree
29167 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
29168 tree list, location_t location)
29170 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
29171 tree c = build_omp_clause (location, code);
29172 OMP_CLAUSE_CHAIN (c) = list;
29173 return c;
29176 /* OpenMP 4.0:
29177 parallel
29179 sections
29180 taskgroup */
29182 static tree
29183 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
29184 enum omp_clause_code code,
29185 tree list, location_t location)
29187 tree c = build_omp_clause (location, code);
29188 OMP_CLAUSE_CHAIN (c) = list;
29189 return c;
29192 /* OpenMP 4.1:
29193 nogroup */
29195 static tree
29196 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
29197 tree list, location_t location)
29199 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
29200 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
29201 OMP_CLAUSE_CHAIN (c) = list;
29202 return c;
29205 /* OpenMP 4.1:
29206 simd
29207 threads */
29209 static tree
29210 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
29211 enum omp_clause_code code,
29212 tree list, location_t location)
29214 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
29215 tree c = build_omp_clause (location, code);
29216 OMP_CLAUSE_CHAIN (c) = list;
29217 return c;
29220 /* OpenMP 4.0:
29221 num_teams ( expression ) */
29223 static tree
29224 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
29225 location_t location)
29227 tree t, c;
29229 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29230 return list;
29232 t = cp_parser_expression (parser);
29234 if (t == error_mark_node
29235 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29236 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29237 /*or_comma=*/false,
29238 /*consume_paren=*/true);
29240 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
29241 "num_teams", location);
29243 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
29244 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
29245 OMP_CLAUSE_CHAIN (c) = list;
29247 return c;
29250 /* OpenMP 4.0:
29251 thread_limit ( expression ) */
29253 static tree
29254 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
29255 location_t location)
29257 tree t, c;
29259 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29260 return list;
29262 t = cp_parser_expression (parser);
29264 if (t == error_mark_node
29265 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29266 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29267 /*or_comma=*/false,
29268 /*consume_paren=*/true);
29270 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
29271 "thread_limit", location);
29273 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
29274 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
29275 OMP_CLAUSE_CHAIN (c) = list;
29277 return c;
29280 /* OpenMP 4.0:
29281 aligned ( variable-list )
29282 aligned ( variable-list : constant-expression ) */
29284 static tree
29285 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
29287 tree nlist, c, alignment = NULL_TREE;
29288 bool colon;
29290 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29291 return list;
29293 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
29294 &colon);
29296 if (colon)
29298 alignment = cp_parser_constant_expression (parser);
29300 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29301 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29302 /*or_comma=*/false,
29303 /*consume_paren=*/true);
29305 if (alignment == error_mark_node)
29306 alignment = NULL_TREE;
29309 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29310 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
29312 return nlist;
29315 /* OpenMP 4.0:
29316 linear ( variable-list )
29317 linear ( variable-list : expression )
29319 OpenMP 4.1:
29320 linear ( modifier ( variable-list ) )
29321 linear ( modifier ( variable-list ) : expression ) */
29323 static tree
29324 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
29325 bool is_cilk_simd_fn)
29327 tree nlist, c, step = integer_one_node;
29328 bool colon;
29329 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
29331 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29332 return list;
29334 if (!is_cilk_simd_fn
29335 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29337 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29338 const char *p = IDENTIFIER_POINTER (id);
29340 if (strcmp ("ref", p) == 0)
29341 kind = OMP_CLAUSE_LINEAR_REF;
29342 else if (strcmp ("val", p) == 0)
29343 kind = OMP_CLAUSE_LINEAR_VAL;
29344 else if (strcmp ("uval", p) == 0)
29345 kind = OMP_CLAUSE_LINEAR_UVAL;
29346 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
29347 cp_lexer_consume_token (parser->lexer);
29348 else
29349 kind = OMP_CLAUSE_LINEAR_DEFAULT;
29352 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
29353 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
29354 &colon);
29355 else
29357 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
29358 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
29359 if (colon)
29360 cp_parser_require (parser, CPP_COLON, RT_COLON);
29361 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29362 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29363 /*or_comma=*/false,
29364 /*consume_paren=*/true);
29367 if (colon)
29369 step = cp_parser_expression (parser);
29371 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
29373 sorry ("using parameters for %<linear%> step is not supported yet");
29374 step = integer_one_node;
29376 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29377 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29378 /*or_comma=*/false,
29379 /*consume_paren=*/true);
29381 if (step == error_mark_node)
29382 return list;
29385 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29387 OMP_CLAUSE_LINEAR_STEP (c) = step;
29388 OMP_CLAUSE_LINEAR_KIND (c) = kind;
29391 return nlist;
29394 /* OpenMP 4.0:
29395 safelen ( constant-expression ) */
29397 static tree
29398 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
29399 location_t location)
29401 tree t, c;
29403 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29404 return list;
29406 t = cp_parser_constant_expression (parser);
29408 if (t == error_mark_node
29409 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29410 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29411 /*or_comma=*/false,
29412 /*consume_paren=*/true);
29414 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
29416 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
29417 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
29418 OMP_CLAUSE_CHAIN (c) = list;
29420 return c;
29423 /* OpenMP 4.0:
29424 simdlen ( constant-expression ) */
29426 static tree
29427 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
29428 location_t location)
29430 tree t, c;
29432 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29433 return list;
29435 t = cp_parser_constant_expression (parser);
29437 if (t == error_mark_node
29438 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29439 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29440 /*or_comma=*/false,
29441 /*consume_paren=*/true);
29443 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
29445 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
29446 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
29447 OMP_CLAUSE_CHAIN (c) = list;
29449 return c;
29452 /* OpenMP 4.1:
29453 vec:
29454 identifier [+/- integer]
29455 vec , identifier [+/- integer]
29458 static tree
29459 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
29460 tree list)
29462 tree vec = NULL;
29464 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29466 cp_parser_error (parser, "expected identifier");
29467 return list;
29470 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29472 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
29473 tree t, identifier = cp_parser_identifier (parser);
29474 tree addend = NULL;
29476 if (identifier == error_mark_node)
29477 t = error_mark_node;
29478 else
29480 t = cp_parser_lookup_name_simple
29481 (parser, identifier,
29482 cp_lexer_peek_token (parser->lexer)->location);
29483 if (t == error_mark_node)
29484 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
29485 id_loc);
29488 bool neg;
29489 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29490 neg = true;
29491 else if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
29492 neg = false;
29493 else
29495 addend = integer_zero_node;
29496 goto add_to_vector;
29498 cp_lexer_consume_token (parser->lexer);
29500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
29502 cp_parser_error (parser, "expected integer");
29503 return list;
29506 addend = cp_lexer_peek_token (parser->lexer)->u.value;
29507 if (TREE_CODE (addend) != INTEGER_CST)
29509 cp_parser_error (parser, "expected integer");
29510 return list;
29512 if (neg)
29514 bool overflow;
29515 wide_int offset = wi::neg (addend, &overflow);
29516 addend = wide_int_to_tree (TREE_TYPE (addend), offset);
29517 if (overflow)
29518 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29519 OPT_Woverflow,
29520 "overflow in implicit constant conversion");
29522 cp_lexer_consume_token (parser->lexer);
29524 add_to_vector:
29525 if (t != error_mark_node)
29526 vec = tree_cons (addend, t, vec);
29528 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29529 break;
29531 cp_lexer_consume_token (parser->lexer);
29534 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
29536 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
29537 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
29538 OMP_CLAUSE_DECL (u) = nreverse (vec);
29539 OMP_CLAUSE_CHAIN (u) = list;
29540 return u;
29542 return list;
29545 /* OpenMP 4.0:
29546 depend ( depend-kind : variable-list )
29548 depend-kind:
29549 in | out | inout
29551 OpenMP 4.1:
29552 depend ( source )
29554 depend ( sink : vec ) */
29556 static tree
29557 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
29559 tree nlist, c;
29560 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
29562 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29563 return list;
29565 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29567 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29568 const char *p = IDENTIFIER_POINTER (id);
29570 if (strcmp ("in", p) == 0)
29571 kind = OMP_CLAUSE_DEPEND_IN;
29572 else if (strcmp ("inout", p) == 0)
29573 kind = OMP_CLAUSE_DEPEND_INOUT;
29574 else if (strcmp ("out", p) == 0)
29575 kind = OMP_CLAUSE_DEPEND_OUT;
29576 else if (strcmp ("source", p) == 0)
29577 kind = OMP_CLAUSE_DEPEND_SOURCE;
29578 else if (strcmp ("sink", p) == 0)
29579 kind = OMP_CLAUSE_DEPEND_SINK;
29580 else
29581 goto invalid_kind;
29583 else
29584 goto invalid_kind;
29586 cp_lexer_consume_token (parser->lexer);
29588 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
29590 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
29591 OMP_CLAUSE_DEPEND_KIND (c) = kind;
29592 OMP_CLAUSE_DECL (c) = NULL_TREE;
29593 OMP_CLAUSE_CHAIN (c) = list;
29594 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29595 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29596 /*or_comma=*/false,
29597 /*consume_paren=*/true);
29598 return c;
29601 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29602 goto resync_fail;
29604 if (kind == OMP_CLAUSE_DEPEND_SINK)
29605 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
29606 else
29608 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
29609 list, NULL);
29611 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29612 OMP_CLAUSE_DEPEND_KIND (c) = kind;
29614 return nlist;
29616 invalid_kind:
29617 cp_parser_error (parser, "invalid depend kind");
29618 resync_fail:
29619 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29620 /*or_comma=*/false,
29621 /*consume_paren=*/true);
29622 return list;
29625 /* OpenMP 4.0:
29626 map ( map-kind : variable-list )
29627 map ( variable-list )
29629 map-kind:
29630 alloc | to | from | tofrom
29632 OpenMP 4.1:
29633 map-kind:
29634 alloc | to | from | tofrom | release | delete
29636 map ( always [,] map-kind: variable-list ) */
29638 static tree
29639 cp_parser_omp_clause_map (cp_parser *parser, tree list)
29641 tree nlist, c;
29642 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
29643 bool always = false;
29645 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29646 return list;
29648 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29650 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29651 const char *p = IDENTIFIER_POINTER (id);
29653 if (strcmp ("always", p) == 0)
29655 int nth = 2;
29656 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
29657 nth++;
29658 if (cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
29659 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
29660 == CPP_COLON))
29662 always = true;
29663 cp_lexer_consume_token (parser->lexer);
29664 if (nth == 3)
29665 cp_lexer_consume_token (parser->lexer);
29670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
29671 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
29673 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29674 const char *p = IDENTIFIER_POINTER (id);
29676 if (strcmp ("alloc", p) == 0)
29677 kind = GOMP_MAP_ALLOC;
29678 else if (strcmp ("to", p) == 0)
29679 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
29680 else if (strcmp ("from", p) == 0)
29681 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
29682 else if (strcmp ("tofrom", p) == 0)
29683 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
29684 else if (strcmp ("release", p) == 0)
29685 kind = GOMP_MAP_RELEASE;
29686 else if (strcmp ("delete", p) == 0)
29687 kind = GOMP_MAP_DELETE;
29688 else
29690 cp_parser_error (parser, "invalid map kind");
29691 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29692 /*or_comma=*/false,
29693 /*consume_paren=*/true);
29694 return list;
29696 cp_lexer_consume_token (parser->lexer);
29697 cp_lexer_consume_token (parser->lexer);
29700 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
29701 NULL);
29703 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29704 OMP_CLAUSE_SET_MAP_KIND (c, kind);
29706 return nlist;
29709 /* OpenMP 4.0:
29710 device ( expression ) */
29712 static tree
29713 cp_parser_omp_clause_device (cp_parser *parser, tree list,
29714 location_t location)
29716 tree t, c;
29718 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29719 return list;
29721 t = cp_parser_expression (parser);
29723 if (t == error_mark_node
29724 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29725 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29726 /*or_comma=*/false,
29727 /*consume_paren=*/true);
29729 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
29730 "device", location);
29732 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
29733 OMP_CLAUSE_DEVICE_ID (c) = t;
29734 OMP_CLAUSE_CHAIN (c) = list;
29736 return c;
29739 /* OpenMP 4.0:
29740 dist_schedule ( static )
29741 dist_schedule ( static , expression ) */
29743 static tree
29744 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
29745 location_t location)
29747 tree c, t;
29749 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29750 return list;
29752 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
29754 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29755 goto invalid_kind;
29756 cp_lexer_consume_token (parser->lexer);
29758 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29760 cp_lexer_consume_token (parser->lexer);
29762 t = cp_parser_assignment_expression (parser);
29764 if (t == error_mark_node)
29765 goto resync_fail;
29766 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
29768 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29769 goto resync_fail;
29771 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29772 goto resync_fail;
29774 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
29775 location);
29776 OMP_CLAUSE_CHAIN (c) = list;
29777 return c;
29779 invalid_kind:
29780 cp_parser_error (parser, "invalid dist_schedule kind");
29781 resync_fail:
29782 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29783 /*or_comma=*/false,
29784 /*consume_paren=*/true);
29785 return list;
29788 /* OpenMP 4.0:
29789 proc_bind ( proc-bind-kind )
29791 proc-bind-kind:
29792 master | close | spread */
29794 static tree
29795 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
29796 location_t location)
29798 tree c;
29799 enum omp_clause_proc_bind_kind kind;
29801 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29802 return list;
29804 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29806 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29807 const char *p = IDENTIFIER_POINTER (id);
29809 if (strcmp ("master", p) == 0)
29810 kind = OMP_CLAUSE_PROC_BIND_MASTER;
29811 else if (strcmp ("close", p) == 0)
29812 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
29813 else if (strcmp ("spread", p) == 0)
29814 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
29815 else
29816 goto invalid_kind;
29818 else
29819 goto invalid_kind;
29821 cp_lexer_consume_token (parser->lexer);
29822 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29823 goto resync_fail;
29825 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29826 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29827 location);
29828 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29829 OMP_CLAUSE_CHAIN (c) = list;
29830 return c;
29832 invalid_kind:
29833 cp_parser_error (parser, "invalid depend kind");
29834 resync_fail:
29835 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29836 /*or_comma=*/false,
29837 /*consume_paren=*/true);
29838 return list;
29841 /* OpenACC:
29842 async [( int-expr )] */
29844 static tree
29845 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29847 tree c, t;
29848 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29850 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
29852 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29854 cp_lexer_consume_token (parser->lexer);
29856 t = cp_parser_expression (parser);
29857 if (t == error_mark_node
29858 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29859 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29860 /*or_comma=*/false,
29861 /*consume_paren=*/true);
29864 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29866 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29867 OMP_CLAUSE_ASYNC_EXPR (c) = t;
29868 OMP_CLAUSE_CHAIN (c) = list;
29869 list = c;
29871 return list;
29874 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29875 is a bitmask in MASK. Return the list of clauses found. */
29877 static tree
29878 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29879 const char *where, cp_token *pragma_tok,
29880 bool finish_p = true)
29882 tree clauses = NULL;
29883 bool first = true;
29885 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29887 location_t here;
29888 pragma_omp_clause c_kind;
29889 const char *c_name;
29890 tree prev = clauses;
29892 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29893 cp_lexer_consume_token (parser->lexer);
29895 here = cp_lexer_peek_token (parser->lexer)->location;
29896 c_kind = cp_parser_omp_clause_name (parser);
29898 switch (c_kind)
29900 case PRAGMA_OACC_CLAUSE_ASYNC:
29901 clauses = cp_parser_oacc_clause_async (parser, clauses);
29902 c_name = "async";
29903 break;
29904 case PRAGMA_OACC_CLAUSE_COLLAPSE:
29905 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29906 c_name = "collapse";
29907 break;
29908 case PRAGMA_OACC_CLAUSE_COPY:
29909 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29910 c_name = "copy";
29911 break;
29912 case PRAGMA_OACC_CLAUSE_COPYIN:
29913 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29914 c_name = "copyin";
29915 break;
29916 case PRAGMA_OACC_CLAUSE_COPYOUT:
29917 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29918 c_name = "copyout";
29919 break;
29920 case PRAGMA_OACC_CLAUSE_CREATE:
29921 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29922 c_name = "create";
29923 break;
29924 case PRAGMA_OACC_CLAUSE_DELETE:
29925 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29926 c_name = "delete";
29927 break;
29928 case PRAGMA_OACC_CLAUSE_DEVICE:
29929 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29930 c_name = "device";
29931 break;
29932 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
29933 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29934 c_name = "deviceptr";
29935 break;
29936 case PRAGMA_OACC_CLAUSE_HOST:
29937 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29938 c_name = "host";
29939 break;
29940 case PRAGMA_OACC_CLAUSE_IF:
29941 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
29942 c_name = "if";
29943 break;
29944 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
29945 clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29946 c_name = "num_gangs";
29947 break;
29948 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
29949 clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29950 c_name = "num_workers";
29951 break;
29952 case PRAGMA_OACC_CLAUSE_PRESENT:
29953 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29954 c_name = "present";
29955 break;
29956 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29957 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29958 c_name = "present_or_copy";
29959 break;
29960 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29961 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29962 c_name = "present_or_copyin";
29963 break;
29964 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29965 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29966 c_name = "present_or_copyout";
29967 break;
29968 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29969 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29970 c_name = "present_or_create";
29971 break;
29972 case PRAGMA_OACC_CLAUSE_REDUCTION:
29973 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29974 c_name = "reduction";
29975 break;
29976 case PRAGMA_OACC_CLAUSE_SELF:
29977 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29978 c_name = "self";
29979 break;
29980 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
29981 clauses = cp_parser_oacc_clause_vector_length (parser, clauses);
29982 c_name = "vector_length";
29983 break;
29984 case PRAGMA_OACC_CLAUSE_WAIT:
29985 clauses = cp_parser_oacc_clause_wait (parser, clauses);
29986 c_name = "wait";
29987 break;
29988 default:
29989 cp_parser_error (parser, "expected %<#pragma acc%> clause");
29990 goto saw_error;
29993 first = false;
29995 if (((mask >> c_kind) & 1) == 0)
29997 /* Remove the invalid clause(s) from the list to avoid
29998 confusing the rest of the compiler. */
29999 clauses = prev;
30000 error_at (here, "%qs is not valid for %qs", c_name, where);
30004 saw_error:
30005 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30007 if (finish_p)
30008 return finish_omp_clauses (clauses, false);
30010 return clauses;
30013 /* Parse all OpenMP clauses. The set clauses allowed by the directive
30014 is a bitmask in MASK. Return the list of clauses found; the result
30015 of clause default goes in *pdefault. */
30017 static tree
30018 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
30019 const char *where, cp_token *pragma_tok,
30020 bool finish_p = true)
30022 tree clauses = NULL;
30023 bool first = true;
30024 cp_token *token = NULL;
30025 bool cilk_simd_fn = false;
30027 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30029 pragma_omp_clause c_kind;
30030 const char *c_name;
30031 tree prev = clauses;
30033 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30034 cp_lexer_consume_token (parser->lexer);
30036 token = cp_lexer_peek_token (parser->lexer);
30037 c_kind = cp_parser_omp_clause_name (parser);
30039 switch (c_kind)
30041 case PRAGMA_OMP_CLAUSE_COLLAPSE:
30042 clauses = cp_parser_omp_clause_collapse (parser, clauses,
30043 token->location);
30044 c_name = "collapse";
30045 break;
30046 case PRAGMA_OMP_CLAUSE_COPYIN:
30047 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
30048 c_name = "copyin";
30049 break;
30050 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
30051 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
30052 clauses);
30053 c_name = "copyprivate";
30054 break;
30055 case PRAGMA_OMP_CLAUSE_DEFAULT:
30056 clauses = cp_parser_omp_clause_default (parser, clauses,
30057 token->location);
30058 c_name = "default";
30059 break;
30060 case PRAGMA_OMP_CLAUSE_FINAL:
30061 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
30062 c_name = "final";
30063 break;
30064 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
30065 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
30066 clauses);
30067 c_name = "firstprivate";
30068 break;
30069 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
30070 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
30071 token->location);
30072 c_name = "grainsize";
30073 break;
30074 case PRAGMA_OMP_CLAUSE_HINT:
30075 clauses = cp_parser_omp_clause_hint (parser, clauses,
30076 token->location);
30077 c_name = "hint";
30078 break;
30079 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
30080 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
30081 token->location);
30082 c_name = "defaultmap";
30083 break;
30084 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
30085 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
30086 clauses);
30087 c_name = "use_device_ptr";
30088 break;
30089 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
30090 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
30091 clauses);
30092 c_name = "is_device_ptr";
30093 break;
30094 case PRAGMA_OMP_CLAUSE_IF:
30095 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
30096 true);
30097 c_name = "if";
30098 break;
30099 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
30100 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
30101 clauses);
30102 c_name = "lastprivate";
30103 break;
30104 case PRAGMA_OMP_CLAUSE_MERGEABLE:
30105 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
30106 token->location);
30107 c_name = "mergeable";
30108 break;
30109 case PRAGMA_OMP_CLAUSE_NOWAIT:
30110 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
30111 c_name = "nowait";
30112 break;
30113 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
30114 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
30115 token->location);
30116 c_name = "num_tasks";
30117 break;
30118 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
30119 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
30120 token->location);
30121 c_name = "num_threads";
30122 break;
30123 case PRAGMA_OMP_CLAUSE_ORDERED:
30124 clauses = cp_parser_omp_clause_ordered (parser, clauses,
30125 token->location);
30126 c_name = "ordered";
30127 break;
30128 case PRAGMA_OMP_CLAUSE_PRIORITY:
30129 clauses = cp_parser_omp_clause_priority (parser, clauses,
30130 token->location);
30131 c_name = "priority";
30132 break;
30133 case PRAGMA_OMP_CLAUSE_PRIVATE:
30134 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
30135 clauses);
30136 c_name = "private";
30137 break;
30138 case PRAGMA_OMP_CLAUSE_REDUCTION:
30139 clauses = cp_parser_omp_clause_reduction (parser, clauses);
30140 c_name = "reduction";
30141 break;
30142 case PRAGMA_OMP_CLAUSE_SCHEDULE:
30143 clauses = cp_parser_omp_clause_schedule (parser, clauses,
30144 token->location);
30145 c_name = "schedule";
30146 break;
30147 case PRAGMA_OMP_CLAUSE_SHARED:
30148 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
30149 clauses);
30150 c_name = "shared";
30151 break;
30152 case PRAGMA_OMP_CLAUSE_UNTIED:
30153 clauses = cp_parser_omp_clause_untied (parser, clauses,
30154 token->location);
30155 c_name = "untied";
30156 break;
30157 case PRAGMA_OMP_CLAUSE_INBRANCH:
30158 case PRAGMA_CILK_CLAUSE_MASK:
30159 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
30160 clauses, token->location);
30161 c_name = "inbranch";
30162 break;
30163 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
30164 case PRAGMA_CILK_CLAUSE_NOMASK:
30165 clauses = cp_parser_omp_clause_branch (parser,
30166 OMP_CLAUSE_NOTINBRANCH,
30167 clauses, token->location);
30168 c_name = "notinbranch";
30169 break;
30170 case PRAGMA_OMP_CLAUSE_PARALLEL:
30171 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
30172 clauses, token->location);
30173 c_name = "parallel";
30174 if (!first)
30176 clause_not_first:
30177 error_at (token->location, "%qs must be the first clause of %qs",
30178 c_name, where);
30179 clauses = prev;
30181 break;
30182 case PRAGMA_OMP_CLAUSE_FOR:
30183 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
30184 clauses, token->location);
30185 c_name = "for";
30186 if (!first)
30187 goto clause_not_first;
30188 break;
30189 case PRAGMA_OMP_CLAUSE_SECTIONS:
30190 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
30191 clauses, token->location);
30192 c_name = "sections";
30193 if (!first)
30194 goto clause_not_first;
30195 break;
30196 case PRAGMA_OMP_CLAUSE_TASKGROUP:
30197 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
30198 clauses, token->location);
30199 c_name = "taskgroup";
30200 if (!first)
30201 goto clause_not_first;
30202 break;
30203 case PRAGMA_OMP_CLAUSE_LINK:
30204 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
30205 c_name = "to";
30206 break;
30207 case PRAGMA_OMP_CLAUSE_TO:
30208 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
30209 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
30210 clauses);
30211 else
30212 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
30213 c_name = "to";
30214 break;
30215 case PRAGMA_OMP_CLAUSE_FROM:
30216 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
30217 c_name = "from";
30218 break;
30219 case PRAGMA_OMP_CLAUSE_UNIFORM:
30220 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
30221 clauses);
30222 c_name = "uniform";
30223 break;
30224 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
30225 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
30226 token->location);
30227 c_name = "num_teams";
30228 break;
30229 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
30230 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
30231 token->location);
30232 c_name = "thread_limit";
30233 break;
30234 case PRAGMA_OMP_CLAUSE_ALIGNED:
30235 clauses = cp_parser_omp_clause_aligned (parser, clauses);
30236 c_name = "aligned";
30237 break;
30238 case PRAGMA_OMP_CLAUSE_LINEAR:
30239 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
30240 cilk_simd_fn = true;
30241 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
30242 c_name = "linear";
30243 break;
30244 case PRAGMA_OMP_CLAUSE_DEPEND:
30245 clauses = cp_parser_omp_clause_depend (parser, clauses,
30246 token->location);
30247 c_name = "depend";
30248 break;
30249 case PRAGMA_OMP_CLAUSE_MAP:
30250 clauses = cp_parser_omp_clause_map (parser, clauses);
30251 c_name = "map";
30252 break;
30253 case PRAGMA_OMP_CLAUSE_DEVICE:
30254 clauses = cp_parser_omp_clause_device (parser, clauses,
30255 token->location);
30256 c_name = "device";
30257 break;
30258 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
30259 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
30260 token->location);
30261 c_name = "dist_schedule";
30262 break;
30263 case PRAGMA_OMP_CLAUSE_PROC_BIND:
30264 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
30265 token->location);
30266 c_name = "proc_bind";
30267 break;
30268 case PRAGMA_OMP_CLAUSE_SAFELEN:
30269 clauses = cp_parser_omp_clause_safelen (parser, clauses,
30270 token->location);
30271 c_name = "safelen";
30272 break;
30273 case PRAGMA_OMP_CLAUSE_SIMDLEN:
30274 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
30275 token->location);
30276 c_name = "simdlen";
30277 break;
30278 case PRAGMA_OMP_CLAUSE_NOGROUP:
30279 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
30280 token->location);
30281 c_name = "nogroup";
30282 break;
30283 case PRAGMA_OMP_CLAUSE_THREADS:
30284 clauses
30285 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
30286 clauses, token->location);
30287 c_name = "threads";
30288 break;
30289 case PRAGMA_OMP_CLAUSE_SIMD:
30290 clauses
30291 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
30292 clauses, token->location);
30293 c_name = "simd";
30294 break;
30295 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
30296 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
30297 c_name = "simdlen";
30298 break;
30299 default:
30300 cp_parser_error (parser, "expected %<#pragma omp%> clause");
30301 goto saw_error;
30304 first = false;
30306 if (((mask >> c_kind) & 1) == 0)
30308 /* Remove the invalid clause(s) from the list to avoid
30309 confusing the rest of the compiler. */
30310 clauses = prev;
30311 error_at (token->location, "%qs is not valid for %qs", c_name, where);
30314 saw_error:
30315 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
30316 no reason to skip to the end. */
30317 if (!(flag_cilkplus && pragma_tok == NULL))
30318 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30319 if (finish_p)
30321 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
30322 return finish_omp_clauses (clauses, false, true);
30323 else
30324 return finish_omp_clauses (clauses, true);
30326 return clauses;
30329 /* OpenMP 2.5:
30330 structured-block:
30331 statement
30333 In practice, we're also interested in adding the statement to an
30334 outer node. So it is convenient if we work around the fact that
30335 cp_parser_statement calls add_stmt. */
30337 static unsigned
30338 cp_parser_begin_omp_structured_block (cp_parser *parser)
30340 unsigned save = parser->in_statement;
30342 /* Only move the values to IN_OMP_BLOCK if they weren't false.
30343 This preserves the "not within loop or switch" style error messages
30344 for nonsense cases like
30345 void foo() {
30346 #pragma omp single
30347 break;
30350 if (parser->in_statement)
30351 parser->in_statement = IN_OMP_BLOCK;
30353 return save;
30356 static void
30357 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
30359 parser->in_statement = save;
30362 static tree
30363 cp_parser_omp_structured_block (cp_parser *parser)
30365 tree stmt = begin_omp_structured_block ();
30366 unsigned int save = cp_parser_begin_omp_structured_block (parser);
30368 cp_parser_statement (parser, NULL_TREE, false, NULL);
30370 cp_parser_end_omp_structured_block (parser, save);
30371 return finish_omp_structured_block (stmt);
30374 /* OpenMP 2.5:
30375 # pragma omp atomic new-line
30376 expression-stmt
30378 expression-stmt:
30379 x binop= expr | x++ | ++x | x-- | --x
30380 binop:
30381 +, *, -, /, &, ^, |, <<, >>
30383 where x is an lvalue expression with scalar type.
30385 OpenMP 3.1:
30386 # pragma omp atomic new-line
30387 update-stmt
30389 # pragma omp atomic read new-line
30390 read-stmt
30392 # pragma omp atomic write new-line
30393 write-stmt
30395 # pragma omp atomic update new-line
30396 update-stmt
30398 # pragma omp atomic capture new-line
30399 capture-stmt
30401 # pragma omp atomic capture new-line
30402 capture-block
30404 read-stmt:
30405 v = x
30406 write-stmt:
30407 x = expr
30408 update-stmt:
30409 expression-stmt | x = x binop expr
30410 capture-stmt:
30411 v = expression-stmt
30412 capture-block:
30413 { v = x; update-stmt; } | { update-stmt; v = x; }
30415 OpenMP 4.0:
30416 update-stmt:
30417 expression-stmt | x = x binop expr | x = expr binop x
30418 capture-stmt:
30419 v = update-stmt
30420 capture-block:
30421 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
30423 where x and v are lvalue expressions with scalar type. */
30425 static void
30426 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
30428 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
30429 tree rhs1 = NULL_TREE, orig_lhs;
30430 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
30431 bool structured_block = false;
30432 bool seq_cst = false;
30434 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30436 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30437 const char *p = IDENTIFIER_POINTER (id);
30439 if (!strcmp (p, "seq_cst"))
30441 seq_cst = true;
30442 cp_lexer_consume_token (parser->lexer);
30443 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
30444 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
30445 cp_lexer_consume_token (parser->lexer);
30448 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30450 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30451 const char *p = IDENTIFIER_POINTER (id);
30453 if (!strcmp (p, "read"))
30454 code = OMP_ATOMIC_READ;
30455 else if (!strcmp (p, "write"))
30456 code = NOP_EXPR;
30457 else if (!strcmp (p, "update"))
30458 code = OMP_ATOMIC;
30459 else if (!strcmp (p, "capture"))
30460 code = OMP_ATOMIC_CAPTURE_NEW;
30461 else
30462 p = NULL;
30463 if (p)
30464 cp_lexer_consume_token (parser->lexer);
30466 if (!seq_cst)
30468 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
30469 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
30470 cp_lexer_consume_token (parser->lexer);
30472 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30474 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30475 const char *p = IDENTIFIER_POINTER (id);
30477 if (!strcmp (p, "seq_cst"))
30479 seq_cst = true;
30480 cp_lexer_consume_token (parser->lexer);
30484 cp_parser_require_pragma_eol (parser, pragma_tok);
30486 switch (code)
30488 case OMP_ATOMIC_READ:
30489 case NOP_EXPR: /* atomic write */
30490 v = cp_parser_unary_expression (parser);
30491 if (v == error_mark_node)
30492 goto saw_error;
30493 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30494 goto saw_error;
30495 if (code == NOP_EXPR)
30496 lhs = cp_parser_expression (parser);
30497 else
30498 lhs = cp_parser_unary_expression (parser);
30499 if (lhs == error_mark_node)
30500 goto saw_error;
30501 if (code == NOP_EXPR)
30503 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
30504 opcode. */
30505 code = OMP_ATOMIC;
30506 rhs = lhs;
30507 lhs = v;
30508 v = NULL_TREE;
30510 goto done;
30511 case OMP_ATOMIC_CAPTURE_NEW:
30512 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30514 cp_lexer_consume_token (parser->lexer);
30515 structured_block = true;
30517 else
30519 v = cp_parser_unary_expression (parser);
30520 if (v == error_mark_node)
30521 goto saw_error;
30522 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30523 goto saw_error;
30525 default:
30526 break;
30529 restart:
30530 lhs = cp_parser_unary_expression (parser);
30531 orig_lhs = lhs;
30532 switch (TREE_CODE (lhs))
30534 case ERROR_MARK:
30535 goto saw_error;
30537 case POSTINCREMENT_EXPR:
30538 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
30539 code = OMP_ATOMIC_CAPTURE_OLD;
30540 /* FALLTHROUGH */
30541 case PREINCREMENT_EXPR:
30542 lhs = TREE_OPERAND (lhs, 0);
30543 opcode = PLUS_EXPR;
30544 rhs = integer_one_node;
30545 break;
30547 case POSTDECREMENT_EXPR:
30548 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
30549 code = OMP_ATOMIC_CAPTURE_OLD;
30550 /* FALLTHROUGH */
30551 case PREDECREMENT_EXPR:
30552 lhs = TREE_OPERAND (lhs, 0);
30553 opcode = MINUS_EXPR;
30554 rhs = integer_one_node;
30555 break;
30557 case COMPOUND_EXPR:
30558 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
30559 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
30560 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
30561 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
30562 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
30563 (TREE_OPERAND (lhs, 1), 0), 0)))
30564 == BOOLEAN_TYPE)
30565 /* Undo effects of boolean_increment for post {in,de}crement. */
30566 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
30567 /* FALLTHRU */
30568 case MODIFY_EXPR:
30569 if (TREE_CODE (lhs) == MODIFY_EXPR
30570 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
30572 /* Undo effects of boolean_increment. */
30573 if (integer_onep (TREE_OPERAND (lhs, 1)))
30575 /* This is pre or post increment. */
30576 rhs = TREE_OPERAND (lhs, 1);
30577 lhs = TREE_OPERAND (lhs, 0);
30578 opcode = NOP_EXPR;
30579 if (code == OMP_ATOMIC_CAPTURE_NEW
30580 && !structured_block
30581 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
30582 code = OMP_ATOMIC_CAPTURE_OLD;
30583 break;
30586 /* FALLTHRU */
30587 default:
30588 switch (cp_lexer_peek_token (parser->lexer)->type)
30590 case CPP_MULT_EQ:
30591 opcode = MULT_EXPR;
30592 break;
30593 case CPP_DIV_EQ:
30594 opcode = TRUNC_DIV_EXPR;
30595 break;
30596 case CPP_PLUS_EQ:
30597 opcode = PLUS_EXPR;
30598 break;
30599 case CPP_MINUS_EQ:
30600 opcode = MINUS_EXPR;
30601 break;
30602 case CPP_LSHIFT_EQ:
30603 opcode = LSHIFT_EXPR;
30604 break;
30605 case CPP_RSHIFT_EQ:
30606 opcode = RSHIFT_EXPR;
30607 break;
30608 case CPP_AND_EQ:
30609 opcode = BIT_AND_EXPR;
30610 break;
30611 case CPP_OR_EQ:
30612 opcode = BIT_IOR_EXPR;
30613 break;
30614 case CPP_XOR_EQ:
30615 opcode = BIT_XOR_EXPR;
30616 break;
30617 case CPP_EQ:
30618 enum cp_parser_prec oprec;
30619 cp_token *token;
30620 cp_lexer_consume_token (parser->lexer);
30621 cp_parser_parse_tentatively (parser);
30622 rhs1 = cp_parser_simple_cast_expression (parser);
30623 if (rhs1 == error_mark_node)
30625 cp_parser_abort_tentative_parse (parser);
30626 cp_parser_simple_cast_expression (parser);
30627 goto saw_error;
30629 token = cp_lexer_peek_token (parser->lexer);
30630 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
30632 cp_parser_abort_tentative_parse (parser);
30633 cp_parser_parse_tentatively (parser);
30634 rhs = cp_parser_binary_expression (parser, false, true,
30635 PREC_NOT_OPERATOR, NULL);
30636 if (rhs == error_mark_node)
30638 cp_parser_abort_tentative_parse (parser);
30639 cp_parser_binary_expression (parser, false, true,
30640 PREC_NOT_OPERATOR, NULL);
30641 goto saw_error;
30643 switch (TREE_CODE (rhs))
30645 case MULT_EXPR:
30646 case TRUNC_DIV_EXPR:
30647 case RDIV_EXPR:
30648 case PLUS_EXPR:
30649 case MINUS_EXPR:
30650 case LSHIFT_EXPR:
30651 case RSHIFT_EXPR:
30652 case BIT_AND_EXPR:
30653 case BIT_IOR_EXPR:
30654 case BIT_XOR_EXPR:
30655 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
30657 if (cp_parser_parse_definitely (parser))
30659 opcode = TREE_CODE (rhs);
30660 rhs1 = TREE_OPERAND (rhs, 0);
30661 rhs = TREE_OPERAND (rhs, 1);
30662 goto stmt_done;
30664 else
30665 goto saw_error;
30667 break;
30668 default:
30669 break;
30671 cp_parser_abort_tentative_parse (parser);
30672 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
30674 rhs = cp_parser_expression (parser);
30675 if (rhs == error_mark_node)
30676 goto saw_error;
30677 opcode = NOP_EXPR;
30678 rhs1 = NULL_TREE;
30679 goto stmt_done;
30681 cp_parser_error (parser,
30682 "invalid form of %<#pragma omp atomic%>");
30683 goto saw_error;
30685 if (!cp_parser_parse_definitely (parser))
30686 goto saw_error;
30687 switch (token->type)
30689 case CPP_SEMICOLON:
30690 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
30692 code = OMP_ATOMIC_CAPTURE_OLD;
30693 v = lhs;
30694 lhs = NULL_TREE;
30695 lhs1 = rhs1;
30696 rhs1 = NULL_TREE;
30697 cp_lexer_consume_token (parser->lexer);
30698 goto restart;
30700 else if (structured_block)
30702 opcode = NOP_EXPR;
30703 rhs = rhs1;
30704 rhs1 = NULL_TREE;
30705 goto stmt_done;
30707 cp_parser_error (parser,
30708 "invalid form of %<#pragma omp atomic%>");
30709 goto saw_error;
30710 case CPP_MULT:
30711 opcode = MULT_EXPR;
30712 break;
30713 case CPP_DIV:
30714 opcode = TRUNC_DIV_EXPR;
30715 break;
30716 case CPP_PLUS:
30717 opcode = PLUS_EXPR;
30718 break;
30719 case CPP_MINUS:
30720 opcode = MINUS_EXPR;
30721 break;
30722 case CPP_LSHIFT:
30723 opcode = LSHIFT_EXPR;
30724 break;
30725 case CPP_RSHIFT:
30726 opcode = RSHIFT_EXPR;
30727 break;
30728 case CPP_AND:
30729 opcode = BIT_AND_EXPR;
30730 break;
30731 case CPP_OR:
30732 opcode = BIT_IOR_EXPR;
30733 break;
30734 case CPP_XOR:
30735 opcode = BIT_XOR_EXPR;
30736 break;
30737 default:
30738 cp_parser_error (parser,
30739 "invalid operator for %<#pragma omp atomic%>");
30740 goto saw_error;
30742 oprec = TOKEN_PRECEDENCE (token);
30743 gcc_assert (oprec != PREC_NOT_OPERATOR);
30744 if (commutative_tree_code (opcode))
30745 oprec = (enum cp_parser_prec) (oprec - 1);
30746 cp_lexer_consume_token (parser->lexer);
30747 rhs = cp_parser_binary_expression (parser, false, false,
30748 oprec, NULL);
30749 if (rhs == error_mark_node)
30750 goto saw_error;
30751 goto stmt_done;
30752 /* FALLTHROUGH */
30753 default:
30754 cp_parser_error (parser,
30755 "invalid operator for %<#pragma omp atomic%>");
30756 goto saw_error;
30758 cp_lexer_consume_token (parser->lexer);
30760 rhs = cp_parser_expression (parser);
30761 if (rhs == error_mark_node)
30762 goto saw_error;
30763 break;
30765 stmt_done:
30766 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
30768 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
30769 goto saw_error;
30770 v = cp_parser_unary_expression (parser);
30771 if (v == error_mark_node)
30772 goto saw_error;
30773 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30774 goto saw_error;
30775 lhs1 = cp_parser_unary_expression (parser);
30776 if (lhs1 == error_mark_node)
30777 goto saw_error;
30779 if (structured_block)
30781 cp_parser_consume_semicolon_at_end_of_statement (parser);
30782 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30784 done:
30785 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
30786 if (!structured_block)
30787 cp_parser_consume_semicolon_at_end_of_statement (parser);
30788 return;
30790 saw_error:
30791 cp_parser_skip_to_end_of_block_or_statement (parser);
30792 if (structured_block)
30794 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30795 cp_lexer_consume_token (parser->lexer);
30796 else if (code == OMP_ATOMIC_CAPTURE_NEW)
30798 cp_parser_skip_to_end_of_block_or_statement (parser);
30799 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30800 cp_lexer_consume_token (parser->lexer);
30806 /* OpenMP 2.5:
30807 # pragma omp barrier new-line */
30809 static void
30810 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
30812 cp_parser_require_pragma_eol (parser, pragma_tok);
30813 finish_omp_barrier ();
30816 /* OpenMP 2.5:
30817 # pragma omp critical [(name)] new-line
30818 structured-block
30820 OpenMP 4.1:
30821 # pragma omp critical [(name) [hint(expression)]] new-line
30822 structured-block */
30824 #define OMP_CRITICAL_CLAUSE_MASK \
30825 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
30827 static tree
30828 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
30830 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
30832 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30834 cp_lexer_consume_token (parser->lexer);
30836 name = cp_parser_identifier (parser);
30838 if (name == error_mark_node
30839 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30840 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30841 /*or_comma=*/false,
30842 /*consume_paren=*/true);
30843 if (name == error_mark_node)
30844 name = NULL;
30846 clauses = cp_parser_omp_all_clauses (parser,
30847 OMP_CRITICAL_CLAUSE_MASK,
30848 "#pragma omp critical", pragma_tok);
30850 else
30851 cp_parser_require_pragma_eol (parser, pragma_tok);
30853 stmt = cp_parser_omp_structured_block (parser);
30854 return c_finish_omp_critical (input_location, stmt, name, clauses);
30857 /* OpenMP 2.5:
30858 # pragma omp flush flush-vars[opt] new-line
30860 flush-vars:
30861 ( variable-list ) */
30863 static void
30864 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
30866 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30867 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30868 cp_parser_require_pragma_eol (parser, pragma_tok);
30870 finish_omp_flush ();
30873 /* Helper function, to parse omp for increment expression. */
30875 static tree
30876 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
30878 tree cond = cp_parser_binary_expression (parser, false, true,
30879 PREC_NOT_OPERATOR, NULL);
30880 if (cond == error_mark_node
30881 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30883 cp_parser_skip_to_end_of_statement (parser);
30884 return error_mark_node;
30887 switch (TREE_CODE (cond))
30889 case GT_EXPR:
30890 case GE_EXPR:
30891 case LT_EXPR:
30892 case LE_EXPR:
30893 break;
30894 case NE_EXPR:
30895 if (code == CILK_SIMD || code == CILK_FOR)
30896 break;
30897 /* Fall through: OpenMP disallows NE_EXPR. */
30898 default:
30899 return error_mark_node;
30902 /* If decl is an iterator, preserve LHS and RHS of the relational
30903 expr until finish_omp_for. */
30904 if (decl
30905 && (type_dependent_expression_p (decl)
30906 || CLASS_TYPE_P (TREE_TYPE (decl))))
30907 return cond;
30909 return build_x_binary_op (input_location, TREE_CODE (cond),
30910 TREE_OPERAND (cond, 0), ERROR_MARK,
30911 TREE_OPERAND (cond, 1), ERROR_MARK,
30912 /*overload=*/NULL, tf_warning_or_error);
30915 /* Helper function, to parse omp for increment expression. */
30917 static tree
30918 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
30920 cp_token *token = cp_lexer_peek_token (parser->lexer);
30921 enum tree_code op;
30922 tree lhs, rhs;
30923 cp_id_kind idk;
30924 bool decl_first;
30926 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30928 op = (token->type == CPP_PLUS_PLUS
30929 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30930 cp_lexer_consume_token (parser->lexer);
30931 lhs = cp_parser_simple_cast_expression (parser);
30932 if (lhs != decl
30933 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
30934 return error_mark_node;
30935 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30938 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30939 if (lhs != decl
30940 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
30941 return error_mark_node;
30943 token = cp_lexer_peek_token (parser->lexer);
30944 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30946 op = (token->type == CPP_PLUS_PLUS
30947 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30948 cp_lexer_consume_token (parser->lexer);
30949 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30952 op = cp_parser_assignment_operator_opt (parser);
30953 if (op == ERROR_MARK)
30954 return error_mark_node;
30956 if (op != NOP_EXPR)
30958 rhs = cp_parser_assignment_expression (parser);
30959 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30960 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30963 lhs = cp_parser_binary_expression (parser, false, false,
30964 PREC_ADDITIVE_EXPRESSION, NULL);
30965 token = cp_lexer_peek_token (parser->lexer);
30966 decl_first = (lhs == decl
30967 || (processing_template_decl && cp_tree_equal (lhs, decl)));
30968 if (decl_first)
30969 lhs = NULL_TREE;
30970 if (token->type != CPP_PLUS
30971 && token->type != CPP_MINUS)
30972 return error_mark_node;
30976 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30977 cp_lexer_consume_token (parser->lexer);
30978 rhs = cp_parser_binary_expression (parser, false, false,
30979 PREC_ADDITIVE_EXPRESSION, NULL);
30980 token = cp_lexer_peek_token (parser->lexer);
30981 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30983 if (lhs == NULL_TREE)
30985 if (op == PLUS_EXPR)
30986 lhs = rhs;
30987 else
30988 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30989 tf_warning_or_error);
30991 else
30992 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30993 ERROR_MARK, NULL, tf_warning_or_error);
30996 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30998 if (!decl_first)
31000 if ((rhs != decl
31001 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
31002 || op == MINUS_EXPR)
31003 return error_mark_node;
31004 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
31006 else
31007 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
31009 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
31012 /* Parse the initialization statement of either an OpenMP for loop or
31013 a Cilk Plus for loop.
31015 Return true if the resulting construct should have an
31016 OMP_CLAUSE_PRIVATE added to it. */
31018 static tree
31019 cp_parser_omp_for_loop_init (cp_parser *parser,
31020 enum tree_code code,
31021 tree &this_pre_body,
31022 vec<tree, va_gc> *for_block,
31023 tree &init,
31024 tree &decl,
31025 tree &real_decl)
31027 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31028 return NULL_TREE;
31030 tree add_private_clause = NULL_TREE;
31032 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
31034 init-expr:
31035 var = lb
31036 integer-type var = lb
31037 random-access-iterator-type var = lb
31038 pointer-type var = lb
31040 cp_decl_specifier_seq type_specifiers;
31042 /* First, try to parse as an initialized declaration. See
31043 cp_parser_condition, from whence the bulk of this is copied. */
31045 cp_parser_parse_tentatively (parser);
31046 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
31047 /*is_trailing_return=*/false,
31048 &type_specifiers);
31049 if (cp_parser_parse_definitely (parser))
31051 /* If parsing a type specifier seq succeeded, then this
31052 MUST be a initialized declaration. */
31053 tree asm_specification, attributes;
31054 cp_declarator *declarator;
31056 declarator = cp_parser_declarator (parser,
31057 CP_PARSER_DECLARATOR_NAMED,
31058 /*ctor_dtor_or_conv_p=*/NULL,
31059 /*parenthesized_p=*/NULL,
31060 /*member_p=*/false,
31061 /*friend_p=*/false);
31062 attributes = cp_parser_attributes_opt (parser);
31063 asm_specification = cp_parser_asm_specification_opt (parser);
31065 if (declarator == cp_error_declarator)
31066 cp_parser_skip_to_end_of_statement (parser);
31068 else
31070 tree pushed_scope, auto_node;
31072 decl = start_decl (declarator, &type_specifiers,
31073 SD_INITIALIZED, attributes,
31074 /*prefix_attributes=*/NULL_TREE,
31075 &pushed_scope);
31077 auto_node = type_uses_auto (TREE_TYPE (decl));
31078 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31080 if (cp_lexer_next_token_is (parser->lexer,
31081 CPP_OPEN_PAREN))
31083 if (code != CILK_SIMD && code != CILK_FOR)
31084 error ("parenthesized initialization is not allowed in "
31085 "OpenMP %<for%> loop");
31086 else
31087 error ("parenthesized initialization is "
31088 "not allowed in for-loop");
31090 else
31091 /* Trigger an error. */
31092 cp_parser_require (parser, CPP_EQ, RT_EQ);
31094 init = error_mark_node;
31095 cp_parser_skip_to_end_of_statement (parser);
31097 else if (CLASS_TYPE_P (TREE_TYPE (decl))
31098 || type_dependent_expression_p (decl)
31099 || auto_node)
31101 bool is_direct_init, is_non_constant_init;
31103 init = cp_parser_initializer (parser,
31104 &is_direct_init,
31105 &is_non_constant_init);
31107 if (auto_node)
31109 TREE_TYPE (decl)
31110 = do_auto_deduction (TREE_TYPE (decl), init,
31111 auto_node);
31113 if (!CLASS_TYPE_P (TREE_TYPE (decl))
31114 && !type_dependent_expression_p (decl))
31115 goto non_class;
31118 cp_finish_decl (decl, init, !is_non_constant_init,
31119 asm_specification,
31120 LOOKUP_ONLYCONVERTING);
31121 if (CLASS_TYPE_P (TREE_TYPE (decl)))
31123 vec_safe_push (for_block, this_pre_body);
31124 init = NULL_TREE;
31126 else
31127 init = pop_stmt_list (this_pre_body);
31128 this_pre_body = NULL_TREE;
31130 else
31132 /* Consume '='. */
31133 cp_lexer_consume_token (parser->lexer);
31134 init = cp_parser_assignment_expression (parser);
31136 non_class:
31137 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
31138 init = error_mark_node;
31139 else
31140 cp_finish_decl (decl, NULL_TREE,
31141 /*init_const_expr_p=*/false,
31142 asm_specification,
31143 LOOKUP_ONLYCONVERTING);
31146 if (pushed_scope)
31147 pop_scope (pushed_scope);
31150 else
31152 cp_id_kind idk;
31153 /* If parsing a type specifier sequence failed, then
31154 this MUST be a simple expression. */
31155 if (code == CILK_FOR)
31156 error ("%<_Cilk_for%> allows expression instead of declaration only "
31157 "in C, not in C++");
31158 cp_parser_parse_tentatively (parser);
31159 decl = cp_parser_primary_expression (parser, false, false,
31160 false, &idk);
31161 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
31162 if (!cp_parser_error_occurred (parser)
31163 && decl
31164 && (TREE_CODE (decl) == COMPONENT_REF
31165 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
31167 cp_parser_abort_tentative_parse (parser);
31168 cp_parser_parse_tentatively (parser);
31169 cp_token *token = cp_lexer_peek_token (parser->lexer);
31170 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
31171 /*check_dependency_p=*/true,
31172 /*template_p=*/NULL,
31173 /*declarator_p=*/false,
31174 /*optional_p=*/false);
31175 if (name != error_mark_node
31176 && last_tok == cp_lexer_peek_token (parser->lexer))
31178 decl = cp_parser_lookup_name_simple (parser, name,
31179 token->location);
31180 if (TREE_CODE (decl) == FIELD_DECL)
31181 add_private_clause = omp_privatize_field (decl);
31183 cp_parser_abort_tentative_parse (parser);
31184 cp_parser_parse_tentatively (parser);
31185 decl = cp_parser_primary_expression (parser, false, false,
31186 false, &idk);
31188 if (!cp_parser_error_occurred (parser)
31189 && decl
31190 && DECL_P (decl)
31191 && CLASS_TYPE_P (TREE_TYPE (decl)))
31193 tree rhs;
31195 cp_parser_parse_definitely (parser);
31196 cp_parser_require (parser, CPP_EQ, RT_EQ);
31197 rhs = cp_parser_assignment_expression (parser);
31198 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
31199 decl, NOP_EXPR,
31200 rhs,
31201 tf_warning_or_error));
31202 if (!add_private_clause)
31203 add_private_clause = decl;
31205 else
31207 decl = NULL;
31208 cp_parser_abort_tentative_parse (parser);
31209 init = cp_parser_expression (parser);
31210 if (init)
31212 if (TREE_CODE (init) == MODIFY_EXPR
31213 || TREE_CODE (init) == MODOP_EXPR)
31214 real_decl = TREE_OPERAND (init, 0);
31218 return add_private_clause;
31221 /* Parse the restricted form of the for statement allowed by OpenMP. */
31223 static tree
31224 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
31225 tree *cclauses)
31227 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
31228 tree real_decl, initv, condv, incrv, declv;
31229 tree this_pre_body, cl;
31230 location_t loc_first;
31231 bool collapse_err = false;
31232 int i, collapse = 1, ordered = 0, count, nbraces = 0;
31233 vec<tree, va_gc> *for_block = make_tree_vector ();
31235 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
31236 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
31237 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
31238 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
31239 && OMP_CLAUSE_ORDERED_EXPR (cl))
31240 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
31242 gcc_assert (collapse >= 1 && ordered >= 0);
31243 count = collapse + (ordered > 0 ? ordered - 1 : 0);
31245 declv = make_tree_vec (count);
31246 initv = make_tree_vec (count);
31247 condv = make_tree_vec (count);
31248 incrv = make_tree_vec (count);
31250 loc_first = cp_lexer_peek_token (parser->lexer)->location;
31252 for (i = 0; i < count; i++)
31254 int bracecount = 0;
31255 tree add_private_clause = NULL_TREE;
31256 location_t loc;
31258 if (code != CILK_FOR
31259 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31261 cp_parser_error (parser, "for statement expected");
31262 return NULL;
31264 if (code == CILK_FOR
31265 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
31267 cp_parser_error (parser, "_Cilk_for statement expected");
31268 return NULL;
31270 loc = cp_lexer_consume_token (parser->lexer)->location;
31272 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31273 return NULL;
31275 init = decl = real_decl = NULL;
31276 this_pre_body = push_stmt_list ();
31278 add_private_clause
31279 = cp_parser_omp_for_loop_init (parser, code,
31280 this_pre_body, for_block,
31281 init, decl, real_decl);
31283 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31284 if (this_pre_body)
31286 this_pre_body = pop_stmt_list (this_pre_body);
31287 if (pre_body)
31289 tree t = pre_body;
31290 pre_body = push_stmt_list ();
31291 add_stmt (t);
31292 add_stmt (this_pre_body);
31293 pre_body = pop_stmt_list (pre_body);
31295 else
31296 pre_body = this_pre_body;
31299 if (decl)
31300 real_decl = decl;
31301 if (cclauses != NULL
31302 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
31303 && real_decl != NULL_TREE)
31305 tree *c;
31306 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
31307 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
31308 && OMP_CLAUSE_DECL (*c) == real_decl)
31310 error_at (loc, "iteration variable %qD"
31311 " should not be firstprivate", real_decl);
31312 *c = OMP_CLAUSE_CHAIN (*c);
31314 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
31315 && OMP_CLAUSE_DECL (*c) == real_decl)
31317 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
31318 tree l = *c;
31319 *c = OMP_CLAUSE_CHAIN (*c);
31320 if (code == OMP_SIMD)
31322 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
31323 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
31325 else
31327 OMP_CLAUSE_CHAIN (l) = clauses;
31328 clauses = l;
31330 add_private_clause = NULL_TREE;
31332 else
31334 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
31335 && OMP_CLAUSE_DECL (*c) == real_decl)
31336 add_private_clause = NULL_TREE;
31337 c = &OMP_CLAUSE_CHAIN (*c);
31341 if (add_private_clause)
31343 tree c;
31344 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31346 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
31347 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
31348 && OMP_CLAUSE_DECL (c) == decl)
31349 break;
31350 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
31351 && OMP_CLAUSE_DECL (c) == decl)
31352 error_at (loc, "iteration variable %qD "
31353 "should not be firstprivate",
31354 decl);
31355 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
31356 && OMP_CLAUSE_DECL (c) == decl)
31357 error_at (loc, "iteration variable %qD should not be reduction",
31358 decl);
31360 if (c == NULL)
31362 if (code != OMP_SIMD)
31363 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
31364 else if (collapse == 1)
31365 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
31366 else
31367 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
31368 OMP_CLAUSE_DECL (c) = add_private_clause;
31369 c = finish_omp_clauses (c, true);
31370 if (c)
31372 OMP_CLAUSE_CHAIN (c) = clauses;
31373 clauses = c;
31374 /* For linear, signal that we need to fill up
31375 the so far unknown linear step. */
31376 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
31377 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
31382 cond = NULL;
31383 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31384 cond = cp_parser_omp_for_cond (parser, decl, code);
31385 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31387 incr = NULL;
31388 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
31390 /* If decl is an iterator, preserve the operator on decl
31391 until finish_omp_for. */
31392 if (real_decl
31393 && ((processing_template_decl
31394 && (TREE_TYPE (real_decl) == NULL_TREE
31395 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
31396 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
31397 incr = cp_parser_omp_for_incr (parser, real_decl);
31398 else
31399 incr = cp_parser_expression (parser);
31400 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
31401 SET_EXPR_LOCATION (incr, input_location);
31404 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31405 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31406 /*or_comma=*/false,
31407 /*consume_paren=*/true);
31409 TREE_VEC_ELT (declv, i) = decl;
31410 TREE_VEC_ELT (initv, i) = init;
31411 TREE_VEC_ELT (condv, i) = cond;
31412 TREE_VEC_ELT (incrv, i) = incr;
31414 if (i == count - 1)
31415 break;
31417 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
31418 in between the collapsed for loops to be still considered perfectly
31419 nested. Hopefully the final version clarifies this.
31420 For now handle (multiple) {'s and empty statements. */
31421 cp_parser_parse_tentatively (parser);
31424 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31425 break;
31426 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31428 cp_lexer_consume_token (parser->lexer);
31429 bracecount++;
31431 else if (bracecount
31432 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31433 cp_lexer_consume_token (parser->lexer);
31434 else
31436 loc = cp_lexer_peek_token (parser->lexer)->location;
31437 error_at (loc, "not enough collapsed for loops");
31438 collapse_err = true;
31439 cp_parser_abort_tentative_parse (parser);
31440 declv = NULL_TREE;
31441 break;
31444 while (1);
31446 if (declv)
31448 cp_parser_parse_definitely (parser);
31449 nbraces += bracecount;
31453 /* Note that we saved the original contents of this flag when we entered
31454 the structured block, and so we don't need to re-save it here. */
31455 if (code == CILK_SIMD || code == CILK_FOR)
31456 parser->in_statement = IN_CILK_SIMD_FOR;
31457 else
31458 parser->in_statement = IN_OMP_FOR;
31460 /* Note that the grammar doesn't call for a structured block here,
31461 though the loop as a whole is a structured block. */
31462 body = push_stmt_list ();
31463 cp_parser_statement (parser, NULL_TREE, false, NULL);
31464 body = pop_stmt_list (body);
31466 if (declv == NULL_TREE)
31467 ret = NULL_TREE;
31468 else
31469 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
31470 body, pre_body, clauses);
31472 while (nbraces)
31474 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31476 cp_lexer_consume_token (parser->lexer);
31477 nbraces--;
31479 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31480 cp_lexer_consume_token (parser->lexer);
31481 else
31483 if (!collapse_err)
31485 error_at (cp_lexer_peek_token (parser->lexer)->location,
31486 "collapsed loops not perfectly nested");
31488 collapse_err = true;
31489 cp_parser_statement_seq_opt (parser, NULL);
31490 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
31491 break;
31495 while (!for_block->is_empty ())
31496 add_stmt (pop_stmt_list (for_block->pop ()));
31497 release_tree_vector (for_block);
31499 return ret;
31502 /* Helper function for OpenMP parsing, split clauses and call
31503 finish_omp_clauses on each of the set of clauses afterwards. */
31505 static void
31506 cp_omp_split_clauses (location_t loc, enum tree_code code,
31507 omp_clause_mask mask, tree clauses, tree *cclauses)
31509 int i;
31510 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
31511 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
31512 if (cclauses[i])
31513 cclauses[i] = finish_omp_clauses (cclauses[i], true);
31516 /* OpenMP 4.0:
31517 #pragma omp simd simd-clause[optseq] new-line
31518 for-loop */
31520 #define OMP_SIMD_CLAUSE_MASK \
31521 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
31522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
31527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31530 static tree
31531 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
31532 char *p_name, omp_clause_mask mask, tree *cclauses)
31534 tree clauses, sb, ret;
31535 unsigned int save;
31536 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31538 strcat (p_name, " simd");
31539 mask |= OMP_SIMD_CLAUSE_MASK;
31540 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
31542 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31543 cclauses == NULL);
31544 if (cclauses)
31546 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
31547 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
31550 sb = begin_omp_structured_block ();
31551 save = cp_parser_begin_omp_structured_block (parser);
31553 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
31555 cp_parser_end_omp_structured_block (parser, save);
31556 add_stmt (finish_omp_structured_block (sb));
31558 return ret;
31561 /* OpenMP 2.5:
31562 #pragma omp for for-clause[optseq] new-line
31563 for-loop
31565 OpenMP 4.0:
31566 #pragma omp for simd for-simd-clause[optseq] new-line
31567 for-loop */
31569 #define OMP_FOR_CLAUSE_MASK \
31570 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
31573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
31576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
31577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
31578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31580 static tree
31581 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
31582 char *p_name, omp_clause_mask mask, tree *cclauses)
31584 tree clauses, sb, ret;
31585 unsigned int save;
31586 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31588 strcat (p_name, " for");
31589 mask |= OMP_FOR_CLAUSE_MASK;
31590 if (cclauses)
31591 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
31593 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31595 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31596 const char *p = IDENTIFIER_POINTER (id);
31598 if (strcmp (p, "simd") == 0)
31600 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31601 if (cclauses == NULL)
31602 cclauses = cclauses_buf;
31604 cp_lexer_consume_token (parser->lexer);
31605 if (!flag_openmp) /* flag_openmp_simd */
31606 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31607 cclauses);
31608 sb = begin_omp_structured_block ();
31609 save = cp_parser_begin_omp_structured_block (parser);
31610 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31611 cclauses);
31612 cp_parser_end_omp_structured_block (parser, save);
31613 tree body = finish_omp_structured_block (sb);
31614 if (ret == NULL)
31615 return ret;
31616 ret = make_node (OMP_FOR);
31617 TREE_TYPE (ret) = void_type_node;
31618 OMP_FOR_BODY (ret) = body;
31619 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
31620 SET_EXPR_LOCATION (ret, loc);
31621 add_stmt (ret);
31622 return ret;
31625 if (!flag_openmp) /* flag_openmp_simd */
31627 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31628 return NULL_TREE;
31631 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31632 cclauses == NULL);
31633 if (cclauses)
31635 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
31636 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
31639 sb = begin_omp_structured_block ();
31640 save = cp_parser_begin_omp_structured_block (parser);
31642 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
31644 cp_parser_end_omp_structured_block (parser, save);
31645 add_stmt (finish_omp_structured_block (sb));
31647 return ret;
31650 /* OpenMP 2.5:
31651 # pragma omp master new-line
31652 structured-block */
31654 static tree
31655 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
31657 cp_parser_require_pragma_eol (parser, pragma_tok);
31658 return c_finish_omp_master (input_location,
31659 cp_parser_omp_structured_block (parser));
31662 /* OpenMP 2.5:
31663 # pragma omp ordered new-line
31664 structured-block
31666 OpenMP 4.1:
31667 # pragma omp ordered ordered-clauses new-line
31668 structured-block */
31670 #define OMP_ORDERED_CLAUSE_MASK \
31671 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
31672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
31674 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
31675 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
31677 static bool
31678 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
31679 enum pragma_context context)
31681 location_t loc = pragma_tok->location;
31683 if (context != pragma_stmt && context != pragma_compound)
31685 cp_parser_error (parser, "expected declaration specifiers");
31686 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31687 return false;
31690 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31692 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31693 const char *p = IDENTIFIER_POINTER (id);
31695 if (strcmp (p, "depend") == 0)
31697 if (context == pragma_stmt)
31699 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
31700 "%<depend%> clause may only be used in compound "
31701 "statements");
31702 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31703 return false;
31705 tree clauses
31706 = cp_parser_omp_all_clauses (parser,
31707 OMP_ORDERED_DEPEND_CLAUSE_MASK,
31708 "#pragma omp ordered", pragma_tok);
31709 c_finish_omp_ordered (loc, clauses, NULL_TREE);
31710 return false;
31714 tree clauses
31715 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
31716 "#pragma omp ordered", pragma_tok);
31717 c_finish_omp_ordered (loc, clauses,
31718 cp_parser_omp_structured_block (parser));
31719 return true;
31722 /* OpenMP 2.5:
31724 section-scope:
31725 { section-sequence }
31727 section-sequence:
31728 section-directive[opt] structured-block
31729 section-sequence section-directive structured-block */
31731 static tree
31732 cp_parser_omp_sections_scope (cp_parser *parser)
31734 tree stmt, substmt;
31735 bool error_suppress = false;
31736 cp_token *tok;
31738 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
31739 return NULL_TREE;
31741 stmt = push_stmt_list ();
31743 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
31745 substmt = cp_parser_omp_structured_block (parser);
31746 substmt = build1 (OMP_SECTION, void_type_node, substmt);
31747 add_stmt (substmt);
31750 while (1)
31752 tok = cp_lexer_peek_token (parser->lexer);
31753 if (tok->type == CPP_CLOSE_BRACE)
31754 break;
31755 if (tok->type == CPP_EOF)
31756 break;
31758 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
31760 cp_lexer_consume_token (parser->lexer);
31761 cp_parser_require_pragma_eol (parser, tok);
31762 error_suppress = false;
31764 else if (!error_suppress)
31766 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
31767 error_suppress = true;
31770 substmt = cp_parser_omp_structured_block (parser);
31771 substmt = build1 (OMP_SECTION, void_type_node, substmt);
31772 add_stmt (substmt);
31774 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
31776 substmt = pop_stmt_list (stmt);
31778 stmt = make_node (OMP_SECTIONS);
31779 TREE_TYPE (stmt) = void_type_node;
31780 OMP_SECTIONS_BODY (stmt) = substmt;
31782 add_stmt (stmt);
31783 return stmt;
31786 /* OpenMP 2.5:
31787 # pragma omp sections sections-clause[optseq] newline
31788 sections-scope */
31790 #define OMP_SECTIONS_CLAUSE_MASK \
31791 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
31794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31797 static tree
31798 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
31799 char *p_name, omp_clause_mask mask, tree *cclauses)
31801 tree clauses, ret;
31802 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31804 strcat (p_name, " sections");
31805 mask |= OMP_SECTIONS_CLAUSE_MASK;
31806 if (cclauses)
31807 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
31809 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31810 cclauses == NULL);
31811 if (cclauses)
31813 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
31814 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
31817 ret = cp_parser_omp_sections_scope (parser);
31818 if (ret)
31819 OMP_SECTIONS_CLAUSES (ret) = clauses;
31821 return ret;
31824 /* OpenMP 2.5:
31825 # pragma omp parallel parallel-clause[optseq] new-line
31826 structured-block
31827 # pragma omp parallel for parallel-for-clause[optseq] new-line
31828 structured-block
31829 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
31830 structured-block
31832 OpenMP 4.0:
31833 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
31834 structured-block */
31836 #define OMP_PARALLEL_CLAUSE_MASK \
31837 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
31845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
31847 static tree
31848 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
31849 char *p_name, omp_clause_mask mask, tree *cclauses)
31851 tree stmt, clauses, block;
31852 unsigned int save;
31853 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31855 strcat (p_name, " parallel");
31856 mask |= OMP_PARALLEL_CLAUSE_MASK;
31858 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31860 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31861 if (cclauses == NULL)
31862 cclauses = cclauses_buf;
31864 cp_lexer_consume_token (parser->lexer);
31865 if (!flag_openmp) /* flag_openmp_simd */
31866 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
31867 block = begin_omp_parallel ();
31868 save = cp_parser_begin_omp_structured_block (parser);
31869 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
31870 cp_parser_end_omp_structured_block (parser, save);
31871 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31872 block);
31873 if (ret == NULL_TREE)
31874 return ret;
31875 OMP_PARALLEL_COMBINED (stmt) = 1;
31876 return stmt;
31878 /* When combined with distribute, parallel has to be followed by for.
31879 #pragma omp target parallel is allowed though. */
31880 else if (cclauses
31881 && (mask & (OMP_CLAUSE_MASK_1
31882 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
31884 error_at (loc, "expected %<for%> after %qs", p_name);
31885 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31886 return NULL_TREE;
31888 else if (!flag_openmp) /* flag_openmp_simd */
31890 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31891 return NULL_TREE;
31893 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31895 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31896 const char *p = IDENTIFIER_POINTER (id);
31897 if (strcmp (p, "sections") == 0)
31899 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31900 cclauses = cclauses_buf;
31902 cp_lexer_consume_token (parser->lexer);
31903 block = begin_omp_parallel ();
31904 save = cp_parser_begin_omp_structured_block (parser);
31905 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
31906 cp_parser_end_omp_structured_block (parser, save);
31907 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31908 block);
31909 OMP_PARALLEL_COMBINED (stmt) = 1;
31910 return stmt;
31914 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
31915 if (cclauses)
31917 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
31918 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
31921 block = begin_omp_parallel ();
31922 save = cp_parser_begin_omp_structured_block (parser);
31923 cp_parser_statement (parser, NULL_TREE, false, NULL);
31924 cp_parser_end_omp_structured_block (parser, save);
31925 stmt = finish_omp_parallel (clauses, block);
31926 return stmt;
31929 /* OpenMP 2.5:
31930 # pragma omp single single-clause[optseq] new-line
31931 structured-block */
31933 #define OMP_SINGLE_CLAUSE_MASK \
31934 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
31937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31939 static tree
31940 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
31942 tree stmt = make_node (OMP_SINGLE);
31943 TREE_TYPE (stmt) = void_type_node;
31945 OMP_SINGLE_CLAUSES (stmt)
31946 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
31947 "#pragma omp single", pragma_tok);
31948 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
31950 return add_stmt (stmt);
31953 /* OpenMP 3.0:
31954 # pragma omp task task-clause[optseq] new-line
31955 structured-block */
31957 #define OMP_TASK_CLAUSE_MASK \
31958 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
31960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
31965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
31966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
31967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
31969 static tree
31970 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
31972 tree clauses, block;
31973 unsigned int save;
31975 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
31976 "#pragma omp task", pragma_tok);
31977 block = begin_omp_task ();
31978 save = cp_parser_begin_omp_structured_block (parser);
31979 cp_parser_statement (parser, NULL_TREE, false, NULL);
31980 cp_parser_end_omp_structured_block (parser, save);
31981 return finish_omp_task (clauses, block);
31984 /* OpenMP 3.0:
31985 # pragma omp taskwait new-line */
31987 static void
31988 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
31990 cp_parser_require_pragma_eol (parser, pragma_tok);
31991 finish_omp_taskwait ();
31994 /* OpenMP 3.1:
31995 # pragma omp taskyield new-line */
31997 static void
31998 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
32000 cp_parser_require_pragma_eol (parser, pragma_tok);
32001 finish_omp_taskyield ();
32004 /* OpenMP 4.0:
32005 # pragma omp taskgroup new-line
32006 structured-block */
32008 static tree
32009 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
32011 cp_parser_require_pragma_eol (parser, pragma_tok);
32012 return c_finish_omp_taskgroup (input_location,
32013 cp_parser_omp_structured_block (parser));
32017 /* OpenMP 2.5:
32018 # pragma omp threadprivate (variable-list) */
32020 static void
32021 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
32023 tree vars;
32025 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32026 cp_parser_require_pragma_eol (parser, pragma_tok);
32028 finish_omp_threadprivate (vars);
32031 /* OpenMP 4.0:
32032 # pragma omp cancel cancel-clause[optseq] new-line */
32034 #define OMP_CANCEL_CLAUSE_MASK \
32035 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
32036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
32037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
32038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
32039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
32041 static void
32042 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
32044 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
32045 "#pragma omp cancel", pragma_tok);
32046 finish_omp_cancel (clauses);
32049 /* OpenMP 4.0:
32050 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
32052 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
32053 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
32054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
32055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
32056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
32058 static void
32059 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
32061 tree clauses;
32062 bool point_seen = false;
32064 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32066 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32067 const char *p = IDENTIFIER_POINTER (id);
32069 if (strcmp (p, "point") == 0)
32071 cp_lexer_consume_token (parser->lexer);
32072 point_seen = true;
32075 if (!point_seen)
32077 cp_parser_error (parser, "expected %<point%>");
32078 cp_parser_require_pragma_eol (parser, pragma_tok);
32079 return;
32082 clauses = cp_parser_omp_all_clauses (parser,
32083 OMP_CANCELLATION_POINT_CLAUSE_MASK,
32084 "#pragma omp cancellation point",
32085 pragma_tok);
32086 finish_omp_cancellation_point (clauses);
32089 /* OpenMP 4.0:
32090 #pragma omp distribute distribute-clause[optseq] new-line
32091 for-loop */
32093 #define OMP_DISTRIBUTE_CLAUSE_MASK \
32094 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
32097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
32099 static tree
32100 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
32101 char *p_name, omp_clause_mask mask, tree *cclauses)
32103 tree clauses, sb, ret;
32104 unsigned int save;
32105 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32107 strcat (p_name, " distribute");
32108 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
32110 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32112 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32113 const char *p = IDENTIFIER_POINTER (id);
32114 bool simd = false;
32115 bool parallel = false;
32117 if (strcmp (p, "simd") == 0)
32118 simd = true;
32119 else
32120 parallel = strcmp (p, "parallel") == 0;
32121 if (parallel || simd)
32123 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
32124 if (cclauses == NULL)
32125 cclauses = cclauses_buf;
32126 cp_lexer_consume_token (parser->lexer);
32127 if (!flag_openmp) /* flag_openmp_simd */
32129 if (simd)
32130 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
32131 cclauses);
32132 else
32133 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
32134 cclauses);
32136 sb = begin_omp_structured_block ();
32137 save = cp_parser_begin_omp_structured_block (parser);
32138 if (simd)
32139 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
32140 cclauses);
32141 else
32142 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
32143 cclauses);
32144 cp_parser_end_omp_structured_block (parser, save);
32145 tree body = finish_omp_structured_block (sb);
32146 if (ret == NULL)
32147 return ret;
32148 ret = make_node (OMP_DISTRIBUTE);
32149 TREE_TYPE (ret) = void_type_node;
32150 OMP_FOR_BODY (ret) = body;
32151 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
32152 SET_EXPR_LOCATION (ret, loc);
32153 add_stmt (ret);
32154 return ret;
32157 if (!flag_openmp) /* flag_openmp_simd */
32159 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32160 return NULL_TREE;
32163 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
32164 cclauses == NULL);
32165 if (cclauses)
32167 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
32168 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
32171 sb = begin_omp_structured_block ();
32172 save = cp_parser_begin_omp_structured_block (parser);
32174 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
32176 cp_parser_end_omp_structured_block (parser, save);
32177 add_stmt (finish_omp_structured_block (sb));
32179 return ret;
32182 /* OpenMP 4.0:
32183 # pragma omp teams teams-clause[optseq] new-line
32184 structured-block */
32186 #define OMP_TEAMS_CLAUSE_MASK \
32187 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
32190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
32191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
32192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
32193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
32195 static tree
32196 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
32197 char *p_name, omp_clause_mask mask, tree *cclauses)
32199 tree clauses, sb, ret;
32200 unsigned int save;
32201 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32203 strcat (p_name, " teams");
32204 mask |= OMP_TEAMS_CLAUSE_MASK;
32206 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32208 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32209 const char *p = IDENTIFIER_POINTER (id);
32210 if (strcmp (p, "distribute") == 0)
32212 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
32213 if (cclauses == NULL)
32214 cclauses = cclauses_buf;
32216 cp_lexer_consume_token (parser->lexer);
32217 if (!flag_openmp) /* flag_openmp_simd */
32218 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
32219 cclauses);
32220 sb = begin_omp_structured_block ();
32221 save = cp_parser_begin_omp_structured_block (parser);
32222 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
32223 cclauses);
32224 cp_parser_end_omp_structured_block (parser, save);
32225 tree body = finish_omp_structured_block (sb);
32226 if (ret == NULL)
32227 return ret;
32228 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
32229 ret = make_node (OMP_TEAMS);
32230 TREE_TYPE (ret) = void_type_node;
32231 OMP_TEAMS_CLAUSES (ret) = clauses;
32232 OMP_TEAMS_BODY (ret) = body;
32233 OMP_TEAMS_COMBINED (ret) = 1;
32234 return add_stmt (ret);
32237 if (!flag_openmp) /* flag_openmp_simd */
32239 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32240 return NULL_TREE;
32243 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
32244 cclauses == NULL);
32245 if (cclauses)
32247 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
32248 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
32251 tree stmt = make_node (OMP_TEAMS);
32252 TREE_TYPE (stmt) = void_type_node;
32253 OMP_TEAMS_CLAUSES (stmt) = clauses;
32254 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
32256 return add_stmt (stmt);
32259 /* OpenMP 4.0:
32260 # pragma omp target data target-data-clause[optseq] new-line
32261 structured-block */
32263 #define OMP_TARGET_DATA_CLAUSE_MASK \
32264 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
32266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
32267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
32269 static tree
32270 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
32272 tree clauses
32273 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
32274 "#pragma omp target data", pragma_tok);
32275 int map_seen = 0;
32276 for (tree *pc = &clauses; *pc;)
32278 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
32279 switch (OMP_CLAUSE_MAP_KIND (*pc))
32281 case GOMP_MAP_TO:
32282 case GOMP_MAP_ALWAYS_TO:
32283 case GOMP_MAP_FROM:
32284 case GOMP_MAP_ALWAYS_FROM:
32285 case GOMP_MAP_TOFROM:
32286 case GOMP_MAP_ALWAYS_TOFROM:
32287 case GOMP_MAP_ALLOC:
32288 map_seen = 3;
32289 break;
32290 case GOMP_MAP_FIRSTPRIVATE_POINTER:
32291 break;
32292 default:
32293 map_seen |= 1;
32294 error_at (OMP_CLAUSE_LOCATION (*pc),
32295 "%<#pragma omp target data%> with map-type other "
32296 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
32297 "on %<map%> clause");
32298 *pc = OMP_CLAUSE_CHAIN (*pc);
32299 continue;
32301 pc = &OMP_CLAUSE_CHAIN (*pc);
32304 if (map_seen != 3)
32306 if (map_seen == 0)
32307 error_at (pragma_tok->location,
32308 "%<#pragma omp target data%> must contain at least "
32309 "one %<map%> clause");
32310 return NULL_TREE;
32313 tree stmt = make_node (OMP_TARGET_DATA);
32314 TREE_TYPE (stmt) = void_type_node;
32315 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
32317 keep_next_level (true);
32318 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
32320 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32321 return add_stmt (stmt);
32324 /* OpenMP 4.1:
32325 # pragma omp target enter data target-enter-data-clause[optseq] new-line
32326 structured-block */
32328 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
32329 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
32331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
32332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
32333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
32335 static tree
32336 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
32337 enum pragma_context context)
32339 bool data_seen = false;
32340 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32342 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32343 const char *p = IDENTIFIER_POINTER (id);
32345 if (strcmp (p, "data") == 0)
32347 cp_lexer_consume_token (parser->lexer);
32348 data_seen = true;
32351 if (!data_seen)
32353 cp_parser_error (parser, "expected %<data%>");
32354 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32355 return NULL_TREE;
32358 if (context == pragma_stmt)
32360 error_at (pragma_tok->location,
32361 "%<#pragma omp target enter data%> may only be "
32362 "used in compound statements");
32363 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32364 return NULL_TREE;
32367 tree clauses
32368 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
32369 "#pragma omp target enter data", pragma_tok);
32370 int map_seen = 0;
32371 for (tree *pc = &clauses; *pc;)
32373 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
32374 switch (OMP_CLAUSE_MAP_KIND (*pc))
32376 case GOMP_MAP_TO:
32377 case GOMP_MAP_ALWAYS_TO:
32378 case GOMP_MAP_ALLOC:
32379 map_seen = 3;
32380 break;
32381 case GOMP_MAP_FIRSTPRIVATE_POINTER:
32382 break;
32383 default:
32384 map_seen |= 1;
32385 error_at (OMP_CLAUSE_LOCATION (*pc),
32386 "%<#pragma omp target enter data%> with map-type other "
32387 "than %<to%> or %<alloc%> on %<map%> clause");
32388 *pc = OMP_CLAUSE_CHAIN (*pc);
32389 continue;
32391 pc = &OMP_CLAUSE_CHAIN (*pc);
32394 if (map_seen != 3)
32396 if (map_seen == 0)
32397 error_at (pragma_tok->location,
32398 "%<#pragma omp target enter data%> must contain at least "
32399 "one %<map%> clause");
32400 return NULL_TREE;
32403 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
32404 TREE_TYPE (stmt) = void_type_node;
32405 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
32406 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32407 return add_stmt (stmt);
32410 /* OpenMP 4.1:
32411 # pragma omp target exit data target-enter-data-clause[optseq] new-line
32412 structured-block */
32414 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
32415 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
32417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
32418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
32419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
32421 static tree
32422 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
32423 enum pragma_context context)
32425 bool data_seen = false;
32426 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32428 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32429 const char *p = IDENTIFIER_POINTER (id);
32431 if (strcmp (p, "data") == 0)
32433 cp_lexer_consume_token (parser->lexer);
32434 data_seen = true;
32437 if (!data_seen)
32439 cp_parser_error (parser, "expected %<data%>");
32440 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32441 return NULL_TREE;
32444 if (context == pragma_stmt)
32446 error_at (pragma_tok->location,
32447 "%<#pragma omp target exit data%> may only be "
32448 "used in compound statements");
32449 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32450 return NULL_TREE;
32453 tree clauses
32454 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
32455 "#pragma omp target exit data", pragma_tok);
32456 int map_seen = 0;
32457 for (tree *pc = &clauses; *pc;)
32459 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
32460 switch (OMP_CLAUSE_MAP_KIND (*pc))
32462 case GOMP_MAP_FROM:
32463 case GOMP_MAP_ALWAYS_FROM:
32464 case GOMP_MAP_RELEASE:
32465 case GOMP_MAP_DELETE:
32466 map_seen = 3;
32467 break;
32468 case GOMP_MAP_FIRSTPRIVATE_POINTER:
32469 break;
32470 default:
32471 map_seen |= 1;
32472 error_at (OMP_CLAUSE_LOCATION (*pc),
32473 "%<#pragma omp target exit data%> with map-type other "
32474 "than %<from%>, %<release%> or %<delete%> on %<map%>"
32475 " clause");
32476 *pc = OMP_CLAUSE_CHAIN (*pc);
32477 continue;
32479 pc = &OMP_CLAUSE_CHAIN (*pc);
32482 if (map_seen != 3)
32484 if (map_seen == 0)
32485 error_at (pragma_tok->location,
32486 "%<#pragma omp target exit data%> must contain at least "
32487 "one %<map%> clause");
32488 return NULL_TREE;
32491 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
32492 TREE_TYPE (stmt) = void_type_node;
32493 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
32494 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32495 return add_stmt (stmt);
32498 /* OpenMP 4.0:
32499 # pragma omp target update target-update-clause[optseq] new-line */
32501 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
32502 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
32503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
32504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
32506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
32507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
32509 static bool
32510 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
32511 enum pragma_context context)
32513 if (context == pragma_stmt)
32515 error_at (pragma_tok->location,
32516 "%<#pragma omp target update%> may only be "
32517 "used in compound statements");
32518 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32519 return false;
32522 tree clauses
32523 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
32524 "#pragma omp target update", pragma_tok);
32525 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
32526 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
32528 error_at (pragma_tok->location,
32529 "%<#pragma omp target update%> must contain at least one "
32530 "%<from%> or %<to%> clauses");
32531 return false;
32534 tree stmt = make_node (OMP_TARGET_UPDATE);
32535 TREE_TYPE (stmt) = void_type_node;
32536 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
32537 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32538 add_stmt (stmt);
32539 return false;
32542 /* OpenMP 4.0:
32543 # pragma omp target target-clause[optseq] new-line
32544 structured-block */
32546 #define OMP_TARGET_CLAUSE_MASK \
32547 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
32548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
32549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
32550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
32551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
32552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
32553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
32554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
32555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
32557 static bool
32558 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
32559 enum pragma_context context)
32561 tree *pc = NULL, stmt;
32563 if (context != pragma_stmt && context != pragma_compound)
32565 cp_parser_error (parser, "expected declaration specifiers");
32566 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32567 return false;
32570 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32572 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32573 const char *p = IDENTIFIER_POINTER (id);
32574 enum tree_code ccode = ERROR_MARK;
32576 if (strcmp (p, "teams") == 0)
32577 ccode = OMP_TEAMS;
32578 else if (strcmp (p, "parallel") == 0)
32579 ccode = OMP_PARALLEL;
32580 else if (strcmp (p, "simd") == 0)
32581 ccode = OMP_SIMD;
32582 if (ccode != ERROR_MARK)
32584 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
32585 char p_name[sizeof ("#pragma omp target teams distribute "
32586 "parallel for simd")];
32588 cp_lexer_consume_token (parser->lexer);
32589 strcpy (p_name, "#pragma omp target");
32590 if (!flag_openmp) /* flag_openmp_simd */
32592 tree stmt;
32593 switch (ccode)
32595 case OMP_TEAMS:
32596 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
32597 OMP_TARGET_CLAUSE_MASK,
32598 cclauses);
32599 break;
32600 case OMP_PARALLEL:
32601 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
32602 OMP_TARGET_CLAUSE_MASK,
32603 cclauses);
32604 break;
32605 case OMP_SIMD:
32606 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
32607 OMP_TARGET_CLAUSE_MASK,
32608 cclauses);
32609 break;
32610 default:
32611 gcc_unreachable ();
32613 return stmt != NULL_TREE;
32615 keep_next_level (true);
32616 tree sb = begin_omp_structured_block (), ret;
32617 unsigned save = cp_parser_begin_omp_structured_block (parser);
32618 switch (ccode)
32620 case OMP_TEAMS:
32621 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
32622 OMP_TARGET_CLAUSE_MASK, cclauses);
32623 break;
32624 case OMP_PARALLEL:
32625 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
32626 OMP_TARGET_CLAUSE_MASK, cclauses);
32627 break;
32628 case OMP_SIMD:
32629 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
32630 OMP_TARGET_CLAUSE_MASK, cclauses);
32631 break;
32632 default:
32633 gcc_unreachable ();
32635 cp_parser_end_omp_structured_block (parser, save);
32636 tree body = finish_omp_structured_block (sb);
32637 if (ret == NULL_TREE)
32638 return false;
32639 tree stmt = make_node (OMP_TARGET);
32640 TREE_TYPE (stmt) = void_type_node;
32641 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
32642 OMP_TARGET_BODY (stmt) = body;
32643 OMP_TARGET_COMBINED (stmt) = 1;
32644 add_stmt (stmt);
32645 pc = &OMP_TARGET_CLAUSES (stmt);
32646 goto check_clauses;
32648 else if (!flag_openmp) /* flag_openmp_simd */
32650 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32651 return false;
32653 else if (strcmp (p, "data") == 0)
32655 cp_lexer_consume_token (parser->lexer);
32656 cp_parser_omp_target_data (parser, pragma_tok);
32657 return true;
32659 else if (strcmp (p, "enter") == 0)
32661 cp_lexer_consume_token (parser->lexer);
32662 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
32663 return false;
32665 else if (strcmp (p, "exit") == 0)
32667 cp_lexer_consume_token (parser->lexer);
32668 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
32669 return false;
32671 else if (strcmp (p, "update") == 0)
32673 cp_lexer_consume_token (parser->lexer);
32674 return cp_parser_omp_target_update (parser, pragma_tok, context);
32678 stmt = make_node (OMP_TARGET);
32679 TREE_TYPE (stmt) = void_type_node;
32681 OMP_TARGET_CLAUSES (stmt)
32682 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
32683 "#pragma omp target", pragma_tok);
32684 pc = &OMP_TARGET_CLAUSES (stmt);
32685 keep_next_level (true);
32686 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
32688 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32689 add_stmt (stmt);
32691 check_clauses:
32692 while (*pc)
32694 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
32695 switch (OMP_CLAUSE_MAP_KIND (*pc))
32697 case GOMP_MAP_TO:
32698 case GOMP_MAP_ALWAYS_TO:
32699 case GOMP_MAP_FROM:
32700 case GOMP_MAP_ALWAYS_FROM:
32701 case GOMP_MAP_TOFROM:
32702 case GOMP_MAP_ALWAYS_TOFROM:
32703 case GOMP_MAP_ALLOC:
32704 case GOMP_MAP_FIRSTPRIVATE_POINTER:
32705 break;
32706 default:
32707 error_at (OMP_CLAUSE_LOCATION (*pc),
32708 "%<#pragma omp target%> with map-type other "
32709 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
32710 "on %<map%> clause");
32711 *pc = OMP_CLAUSE_CHAIN (*pc);
32712 continue;
32714 pc = &OMP_CLAUSE_CHAIN (*pc);
32716 return true;
32719 /* OpenACC 2.0:
32720 # pragma acc cache (variable-list) new-line
32723 static tree
32724 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
32726 tree stmt, clauses;
32728 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
32729 clauses = finish_omp_clauses (clauses, false);
32731 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
32733 stmt = make_node (OACC_CACHE);
32734 TREE_TYPE (stmt) = void_type_node;
32735 OACC_CACHE_CLAUSES (stmt) = clauses;
32736 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32737 add_stmt (stmt);
32739 return stmt;
32742 /* OpenACC 2.0:
32743 # pragma acc data oacc-data-clause[optseq] new-line
32744 structured-block */
32746 #define OACC_DATA_CLAUSE_MASK \
32747 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
32748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
32752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
32754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
32755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
32757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
32759 static tree
32760 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
32762 tree stmt, clauses, block;
32763 unsigned int save;
32765 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
32766 "#pragma acc data", pragma_tok);
32768 block = begin_omp_parallel ();
32769 save = cp_parser_begin_omp_structured_block (parser);
32770 cp_parser_statement (parser, NULL_TREE, false, NULL);
32771 cp_parser_end_omp_structured_block (parser, save);
32772 stmt = finish_oacc_data (clauses, block);
32773 return stmt;
32776 /* OpenACC 2.0:
32777 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
32781 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
32783 LOC is the location of the #pragma token.
32786 #define OACC_ENTER_DATA_CLAUSE_MASK \
32787 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
32793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
32795 #define OACC_EXIT_DATA_CLAUSE_MASK \
32796 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
32800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
32802 static tree
32803 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
32804 bool enter)
32806 tree stmt, clauses;
32808 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
32809 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32811 cp_parser_error (parser, enter
32812 ? "expected %<data%> in %<#pragma acc enter data%>"
32813 : "expected %<data%> in %<#pragma acc exit data%>");
32814 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32815 return NULL_TREE;
32818 const char *p =
32819 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
32820 if (strcmp (p, "data") != 0)
32822 cp_parser_error (parser, "invalid pragma");
32823 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32824 return NULL_TREE;
32827 cp_lexer_consume_token (parser->lexer);
32829 if (enter)
32830 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
32831 "#pragma acc enter data", pragma_tok);
32832 else
32833 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
32834 "#pragma acc exit data", pragma_tok);
32836 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
32838 error_at (pragma_tok->location,
32839 "%<#pragma acc enter data%> has no data movement clause");
32840 return NULL_TREE;
32843 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
32844 TREE_TYPE (stmt) = void_type_node;
32845 OMP_STANDALONE_CLAUSES (stmt) = clauses;
32846 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32847 add_stmt (stmt);
32848 return stmt;
32851 /* OpenACC 2.0:
32852 # pragma acc kernels oacc-kernels-clause[optseq] new-line
32853 structured-block */
32855 #define OACC_KERNELS_CLAUSE_MASK \
32856 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
32858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
32862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
32864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
32865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
32867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
32868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
32870 static tree
32871 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
32873 tree stmt, clauses, block;
32874 unsigned int save;
32876 clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
32877 "#pragma acc kernels", pragma_tok);
32879 block = begin_omp_parallel ();
32880 save = cp_parser_begin_omp_structured_block (parser);
32881 cp_parser_statement (parser, NULL_TREE, false, NULL);
32882 cp_parser_end_omp_structured_block (parser, save);
32883 stmt = finish_oacc_kernels (clauses, block);
32884 return stmt;
32887 /* OpenACC 2.0:
32888 # pragma acc loop oacc-loop-clause[optseq] new-line
32889 structured-block */
32891 #define OACC_LOOP_CLAUSE_MASK \
32892 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
32893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
32895 static tree
32896 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
32898 tree stmt, clauses, block;
32899 int save;
32901 clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
32902 "#pragma acc loop", pragma_tok);
32904 block = begin_omp_structured_block ();
32905 save = cp_parser_begin_omp_structured_block (parser);
32906 stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
32907 cp_parser_end_omp_structured_block (parser, save);
32908 add_stmt (finish_omp_structured_block (block));
32909 return stmt;
32912 /* OpenACC 2.0:
32913 # pragma acc parallel oacc-parallel-clause[optseq] new-line
32914 structured-block */
32916 #define OACC_PARALLEL_CLAUSE_MASK \
32917 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
32919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
32920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
32921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
32922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
32923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
32925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
32926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
32927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
32928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
32929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
32930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
32931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
32932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
32933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
32935 static tree
32936 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
32938 tree stmt, clauses, block;
32939 unsigned int save;
32941 clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
32942 "#pragma acc parallel", pragma_tok);
32944 block = begin_omp_parallel ();
32945 save = cp_parser_begin_omp_structured_block (parser);
32946 cp_parser_statement (parser, NULL_TREE, false, NULL);
32947 cp_parser_end_omp_structured_block (parser, save);
32948 stmt = finish_oacc_parallel (clauses, block);
32949 return stmt;
32952 /* OpenACC 2.0:
32953 # pragma acc update oacc-update-clause[optseq] new-line
32956 #define OACC_UPDATE_CLAUSE_MASK \
32957 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
32958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
32959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
32960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
32961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
32962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
32964 static tree
32965 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
32967 tree stmt, clauses;
32969 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
32970 "#pragma acc update", pragma_tok);
32972 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
32974 error_at (pragma_tok->location,
32975 "%<#pragma acc update%> must contain at least one "
32976 "%<device%> or %<host/self%> clause");
32977 return NULL_TREE;
32980 stmt = make_node (OACC_UPDATE);
32981 TREE_TYPE (stmt) = void_type_node;
32982 OACC_UPDATE_CLAUSES (stmt) = clauses;
32983 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32984 add_stmt (stmt);
32985 return stmt;
32988 /* OpenACC 2.0:
32989 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
32991 LOC is the location of the #pragma token.
32994 #define OACC_WAIT_CLAUSE_MASK \
32995 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
32997 static tree
32998 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
33000 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
33001 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33003 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33004 list = cp_parser_oacc_wait_list (parser, loc, list);
33006 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
33007 "#pragma acc wait", pragma_tok);
33009 stmt = c_finish_oacc_wait (loc, list, clauses);
33011 return stmt;
33014 /* OpenMP 4.0:
33015 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
33017 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
33018 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
33022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
33023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
33025 static void
33026 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
33027 enum pragma_context context)
33029 bool first_p = parser->omp_declare_simd == NULL;
33030 cp_omp_declare_simd_data data;
33031 if (first_p)
33033 data.error_seen = false;
33034 data.fndecl_seen = false;
33035 data.tokens = vNULL;
33036 parser->omp_declare_simd = &data;
33038 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33039 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
33040 cp_lexer_consume_token (parser->lexer);
33041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33042 parser->omp_declare_simd->error_seen = true;
33043 cp_parser_require_pragma_eol (parser, pragma_tok);
33044 struct cp_token_cache *cp
33045 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
33046 parser->omp_declare_simd->tokens.safe_push (cp);
33047 if (first_p)
33049 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
33050 cp_parser_pragma (parser, context);
33051 switch (context)
33053 case pragma_external:
33054 cp_parser_declaration (parser);
33055 break;
33056 case pragma_member:
33057 cp_parser_member_declaration (parser);
33058 break;
33059 case pragma_objc_icode:
33060 cp_parser_block_declaration (parser, /*statement_p=*/false);
33061 break;
33062 default:
33063 cp_parser_declaration_statement (parser);
33064 break;
33066 if (parser->omp_declare_simd
33067 && !parser->omp_declare_simd->error_seen
33068 && !parser->omp_declare_simd->fndecl_seen)
33069 error_at (pragma_tok->location,
33070 "%<#pragma omp declare simd%> not immediately followed by "
33071 "function declaration or definition");
33072 data.tokens.release ();
33073 parser->omp_declare_simd = NULL;
33077 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
33078 This function is modelled similar to the late parsing of omp declare
33079 simd. */
33081 static tree
33082 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
33084 struct cp_token_cache *ce;
33085 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
33086 int ii = 0;
33088 if (parser->omp_declare_simd != NULL)
33090 error ("%<#pragma omp declare simd%> cannot be used in the same function"
33091 " marked as a Cilk Plus SIMD-enabled function");
33092 XDELETE (parser->cilk_simd_fn_info);
33093 parser->cilk_simd_fn_info = NULL;
33094 return attrs;
33096 if (!info->error_seen && info->fndecl_seen)
33098 error ("vector attribute not immediately followed by a single function"
33099 " declaration or definition");
33100 info->error_seen = true;
33102 if (info->error_seen)
33103 return attrs;
33105 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
33107 tree c, cl;
33109 cp_parser_push_lexer_for_tokens (parser, ce);
33110 parser->lexer->in_pragma = true;
33111 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
33112 "SIMD-enabled functions attribute",
33113 NULL);
33114 cp_parser_pop_lexer (parser);
33115 if (cl)
33116 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
33118 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
33119 TREE_CHAIN (c) = attrs;
33120 attrs = c;
33122 c = build_tree_list (get_identifier ("omp declare simd"), cl);
33123 TREE_CHAIN (c) = attrs;
33124 if (processing_template_decl)
33125 ATTR_IS_DEPENDENT (c) = 1;
33126 attrs = c;
33128 info->fndecl_seen = true;
33129 XDELETE (parser->cilk_simd_fn_info);
33130 parser->cilk_simd_fn_info = NULL;
33131 return attrs;
33134 /* Finalize #pragma omp declare simd clauses after direct declarator has
33135 been parsed, and put that into "omp declare simd" attribute. */
33137 static tree
33138 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
33140 struct cp_token_cache *ce;
33141 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
33142 int i;
33144 if (!data->error_seen && data->fndecl_seen)
33146 error ("%<#pragma omp declare simd%> not immediately followed by "
33147 "a single function declaration or definition");
33148 data->error_seen = true;
33149 return attrs;
33151 if (data->error_seen)
33152 return attrs;
33154 FOR_EACH_VEC_ELT (data->tokens, i, ce)
33156 tree c, cl;
33158 cp_parser_push_lexer_for_tokens (parser, ce);
33159 parser->lexer->in_pragma = true;
33160 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
33161 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
33162 cp_lexer_consume_token (parser->lexer);
33163 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
33164 "#pragma omp declare simd", pragma_tok);
33165 cp_parser_pop_lexer (parser);
33166 if (cl)
33167 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
33168 c = build_tree_list (get_identifier ("omp declare simd"), cl);
33169 TREE_CHAIN (c) = attrs;
33170 if (processing_template_decl)
33171 ATTR_IS_DEPENDENT (c) = 1;
33172 attrs = c;
33175 data->fndecl_seen = true;
33176 return attrs;
33180 /* OpenMP 4.0:
33181 # pragma omp declare target new-line
33182 declarations and definitions
33183 # pragma omp end declare target new-line
33185 OpenMP 4.1:
33186 # pragma omp declare target ( extended-list ) new-line
33188 # pragma omp declare target declare-target-clauses[seq] new-line */
33190 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
33191 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
33192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
33194 static void
33195 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
33197 tree clauses = NULL_TREE;
33198 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33199 clauses
33200 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
33201 "#pragma omp declare target", pragma_tok);
33202 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33204 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33205 clauses);
33206 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33208 else
33210 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33211 scope_chain->omp_declare_target_attribute++;
33212 return;
33214 if (scope_chain->omp_declare_target_attribute)
33215 error_at (pragma_tok->location,
33216 "%<#pragma omp declare target%> with clauses in between "
33217 "%<#pragma omp declare target%> without clauses and "
33218 "%<#pragma omp end declare target%>");
33219 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
33221 tree t = OMP_CLAUSE_DECL (c), id;
33222 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
33223 tree at2 = lookup_attribute ("omp declare target link",
33224 DECL_ATTRIBUTES (t));
33225 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
33227 id = get_identifier ("omp declare target link");
33228 std::swap (at1, at2);
33230 else
33231 id = get_identifier ("omp declare target");
33232 if (at2)
33234 error_at (OMP_CLAUSE_LOCATION (c),
33235 "%qD specified both in declare target %<link%> and %<to%>"
33236 " clauses", t);
33237 continue;
33239 if (!at1)
33240 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
33244 static void
33245 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
33247 const char *p = "";
33248 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33250 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33251 p = IDENTIFIER_POINTER (id);
33253 if (strcmp (p, "declare") == 0)
33255 cp_lexer_consume_token (parser->lexer);
33256 p = "";
33257 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33259 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33260 p = IDENTIFIER_POINTER (id);
33262 if (strcmp (p, "target") == 0)
33263 cp_lexer_consume_token (parser->lexer);
33264 else
33266 cp_parser_error (parser, "expected %<target%>");
33267 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33268 return;
33271 else
33273 cp_parser_error (parser, "expected %<declare%>");
33274 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33275 return;
33277 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33278 if (!scope_chain->omp_declare_target_attribute)
33279 error_at (pragma_tok->location,
33280 "%<#pragma omp end declare target%> without corresponding "
33281 "%<#pragma omp declare target%>");
33282 else
33283 scope_chain->omp_declare_target_attribute--;
33286 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
33287 expression and optional initializer clause of
33288 #pragma omp declare reduction. We store the expression(s) as
33289 either 3, 6 or 7 special statements inside of the artificial function's
33290 body. The first two statements are DECL_EXPRs for the artificial
33291 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
33292 expression that uses those variables.
33293 If there was any INITIALIZER clause, this is followed by further statements,
33294 the fourth and fifth statements are DECL_EXPRs for the artificial
33295 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
33296 constructor variant (first token after open paren is not omp_priv),
33297 then the sixth statement is a statement with the function call expression
33298 that uses the OMP_PRIV and optionally OMP_ORIG variable.
33299 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
33300 to initialize the OMP_PRIV artificial variable and there is seventh
33301 statement, a DECL_EXPR of the OMP_PRIV statement again. */
33303 static bool
33304 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
33306 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
33307 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
33308 type = TREE_TYPE (type);
33309 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
33310 DECL_ARTIFICIAL (omp_out) = 1;
33311 pushdecl (omp_out);
33312 add_decl_expr (omp_out);
33313 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
33314 DECL_ARTIFICIAL (omp_in) = 1;
33315 pushdecl (omp_in);
33316 add_decl_expr (omp_in);
33317 tree combiner;
33318 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
33320 keep_next_level (true);
33321 tree block = begin_omp_structured_block ();
33322 combiner = cp_parser_expression (parser);
33323 finish_expr_stmt (combiner);
33324 block = finish_omp_structured_block (block);
33325 add_stmt (block);
33327 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33328 return false;
33330 const char *p = "";
33331 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33333 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33334 p = IDENTIFIER_POINTER (id);
33337 if (strcmp (p, "initializer") == 0)
33339 cp_lexer_consume_token (parser->lexer);
33340 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33341 return false;
33343 p = "";
33344 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33346 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33347 p = IDENTIFIER_POINTER (id);
33350 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
33351 DECL_ARTIFICIAL (omp_priv) = 1;
33352 pushdecl (omp_priv);
33353 add_decl_expr (omp_priv);
33354 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
33355 DECL_ARTIFICIAL (omp_orig) = 1;
33356 pushdecl (omp_orig);
33357 add_decl_expr (omp_orig);
33359 keep_next_level (true);
33360 block = begin_omp_structured_block ();
33362 bool ctor = false;
33363 if (strcmp (p, "omp_priv") == 0)
33365 bool is_direct_init, is_non_constant_init;
33366 ctor = true;
33367 cp_lexer_consume_token (parser->lexer);
33368 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
33369 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
33370 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
33371 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
33372 == CPP_CLOSE_PAREN
33373 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
33374 == CPP_CLOSE_PAREN))
33376 finish_omp_structured_block (block);
33377 error ("invalid initializer clause");
33378 return false;
33380 initializer = cp_parser_initializer (parser, &is_direct_init,
33381 &is_non_constant_init);
33382 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
33383 NULL_TREE, LOOKUP_ONLYCONVERTING);
33385 else
33387 cp_parser_parse_tentatively (parser);
33388 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
33389 /*check_dependency_p=*/true,
33390 /*template_p=*/NULL,
33391 /*declarator_p=*/false,
33392 /*optional_p=*/false);
33393 vec<tree, va_gc> *args;
33394 if (fn_name == error_mark_node
33395 || cp_parser_error_occurred (parser)
33396 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
33397 || ((args = cp_parser_parenthesized_expression_list
33398 (parser, non_attr, /*cast_p=*/false,
33399 /*allow_expansion_p=*/true,
33400 /*non_constant_p=*/NULL)),
33401 cp_parser_error_occurred (parser)))
33403 finish_omp_structured_block (block);
33404 cp_parser_abort_tentative_parse (parser);
33405 cp_parser_error (parser, "expected id-expression (arguments)");
33406 return false;
33408 unsigned int i;
33409 tree arg;
33410 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
33411 if (arg == omp_priv
33412 || (TREE_CODE (arg) == ADDR_EXPR
33413 && TREE_OPERAND (arg, 0) == omp_priv))
33414 break;
33415 cp_parser_abort_tentative_parse (parser);
33416 if (arg == NULL_TREE)
33417 error ("one of the initializer call arguments should be %<omp_priv%>"
33418 " or %<&omp_priv%>");
33419 initializer = cp_parser_postfix_expression (parser, false, false, false,
33420 false, NULL);
33421 finish_expr_stmt (initializer);
33424 block = finish_omp_structured_block (block);
33425 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
33426 add_stmt (block);
33428 if (ctor)
33429 add_decl_expr (omp_orig);
33431 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33432 return false;
33435 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
33436 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
33438 return true;
33441 /* OpenMP 4.0
33442 #pragma omp declare reduction (reduction-id : typename-list : expression) \
33443 initializer-clause[opt] new-line
33445 initializer-clause:
33446 initializer (omp_priv initializer)
33447 initializer (function-name (argument-list)) */
33449 static void
33450 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
33451 enum pragma_context)
33453 auto_vec<tree> types;
33454 enum tree_code reduc_code = ERROR_MARK;
33455 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
33456 unsigned int i;
33457 cp_token *first_token;
33458 cp_token_cache *cp;
33459 int errs;
33460 void *p;
33462 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
33463 p = obstack_alloc (&declarator_obstack, 0);
33465 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33466 goto fail;
33468 switch (cp_lexer_peek_token (parser->lexer)->type)
33470 case CPP_PLUS:
33471 reduc_code = PLUS_EXPR;
33472 break;
33473 case CPP_MULT:
33474 reduc_code = MULT_EXPR;
33475 break;
33476 case CPP_MINUS:
33477 reduc_code = MINUS_EXPR;
33478 break;
33479 case CPP_AND:
33480 reduc_code = BIT_AND_EXPR;
33481 break;
33482 case CPP_XOR:
33483 reduc_code = BIT_XOR_EXPR;
33484 break;
33485 case CPP_OR:
33486 reduc_code = BIT_IOR_EXPR;
33487 break;
33488 case CPP_AND_AND:
33489 reduc_code = TRUTH_ANDIF_EXPR;
33490 break;
33491 case CPP_OR_OR:
33492 reduc_code = TRUTH_ORIF_EXPR;
33493 break;
33494 case CPP_NAME:
33495 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
33496 break;
33497 default:
33498 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
33499 "%<|%>, %<&&%>, %<||%> or identifier");
33500 goto fail;
33503 if (reduc_code != ERROR_MARK)
33504 cp_lexer_consume_token (parser->lexer);
33506 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
33507 if (reduc_id == error_mark_node)
33508 goto fail;
33510 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33511 goto fail;
33513 /* Types may not be defined in declare reduction type list. */
33514 const char *saved_message;
33515 saved_message = parser->type_definition_forbidden_message;
33516 parser->type_definition_forbidden_message
33517 = G_("types may not be defined in declare reduction type list");
33518 bool saved_colon_corrects_to_scope_p;
33519 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33520 parser->colon_corrects_to_scope_p = false;
33521 bool saved_colon_doesnt_start_class_def_p;
33522 saved_colon_doesnt_start_class_def_p
33523 = parser->colon_doesnt_start_class_def_p;
33524 parser->colon_doesnt_start_class_def_p = true;
33526 while (true)
33528 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33529 type = cp_parser_type_id (parser);
33530 if (type == error_mark_node)
33532 else if (ARITHMETIC_TYPE_P (type)
33533 && (orig_reduc_id == NULL_TREE
33534 || (TREE_CODE (type) != COMPLEX_TYPE
33535 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
33536 "min") == 0
33537 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
33538 "max") == 0))))
33539 error_at (loc, "predeclared arithmetic type %qT in "
33540 "%<#pragma omp declare reduction%>", type);
33541 else if (TREE_CODE (type) == FUNCTION_TYPE
33542 || TREE_CODE (type) == METHOD_TYPE
33543 || TREE_CODE (type) == ARRAY_TYPE)
33544 error_at (loc, "function or array type %qT in "
33545 "%<#pragma omp declare reduction%>", type);
33546 else if (TREE_CODE (type) == REFERENCE_TYPE)
33547 error_at (loc, "reference type %qT in "
33548 "%<#pragma omp declare reduction%>", type);
33549 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
33550 error_at (loc, "const, volatile or __restrict qualified type %qT in "
33551 "%<#pragma omp declare reduction%>", type);
33552 else
33553 types.safe_push (type);
33555 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33556 cp_lexer_consume_token (parser->lexer);
33557 else
33558 break;
33561 /* Restore the saved message. */
33562 parser->type_definition_forbidden_message = saved_message;
33563 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33564 parser->colon_doesnt_start_class_def_p
33565 = saved_colon_doesnt_start_class_def_p;
33567 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
33568 || types.is_empty ())
33570 fail:
33571 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33572 goto done;
33575 first_token = cp_lexer_peek_token (parser->lexer);
33576 cp = NULL;
33577 errs = errorcount;
33578 FOR_EACH_VEC_ELT (types, i, type)
33580 tree fntype
33581 = build_function_type_list (void_type_node,
33582 cp_build_reference_type (type, false),
33583 NULL_TREE);
33584 tree this_reduc_id = reduc_id;
33585 if (!dependent_type_p (type))
33586 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
33587 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
33588 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
33589 DECL_ARTIFICIAL (fndecl) = 1;
33590 DECL_EXTERNAL (fndecl) = 1;
33591 DECL_DECLARED_INLINE_P (fndecl) = 1;
33592 DECL_IGNORED_P (fndecl) = 1;
33593 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
33594 DECL_ATTRIBUTES (fndecl)
33595 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
33596 DECL_ATTRIBUTES (fndecl));
33597 if (processing_template_decl)
33598 fndecl = push_template_decl (fndecl);
33599 bool block_scope = false;
33600 tree block = NULL_TREE;
33601 if (current_function_decl)
33603 block_scope = true;
33604 DECL_CONTEXT (fndecl) = global_namespace;
33605 if (!processing_template_decl)
33606 pushdecl (fndecl);
33608 else if (current_class_type)
33610 if (cp == NULL)
33612 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33613 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
33614 cp_lexer_consume_token (parser->lexer);
33615 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33616 goto fail;
33617 cp = cp_token_cache_new (first_token,
33618 cp_lexer_peek_nth_token (parser->lexer,
33619 2));
33621 DECL_STATIC_FUNCTION_P (fndecl) = 1;
33622 finish_member_declaration (fndecl);
33623 DECL_PENDING_INLINE_INFO (fndecl) = cp;
33624 DECL_PENDING_INLINE_P (fndecl) = 1;
33625 vec_safe_push (unparsed_funs_with_definitions, fndecl);
33626 continue;
33628 else
33630 DECL_CONTEXT (fndecl) = current_namespace;
33631 pushdecl (fndecl);
33633 if (!block_scope)
33634 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
33635 else
33636 block = begin_omp_structured_block ();
33637 if (cp)
33639 cp_parser_push_lexer_for_tokens (parser, cp);
33640 parser->lexer->in_pragma = true;
33642 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
33644 if (!block_scope)
33645 finish_function (0);
33646 else
33647 DECL_CONTEXT (fndecl) = current_function_decl;
33648 if (cp)
33649 cp_parser_pop_lexer (parser);
33650 goto fail;
33652 if (cp)
33653 cp_parser_pop_lexer (parser);
33654 if (!block_scope)
33655 finish_function (0);
33656 else
33658 DECL_CONTEXT (fndecl) = current_function_decl;
33659 block = finish_omp_structured_block (block);
33660 if (TREE_CODE (block) == BIND_EXPR)
33661 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
33662 else if (TREE_CODE (block) == STATEMENT_LIST)
33663 DECL_SAVED_TREE (fndecl) = block;
33664 if (processing_template_decl)
33665 add_decl_expr (fndecl);
33667 cp_check_omp_declare_reduction (fndecl);
33668 if (cp == NULL && types.length () > 1)
33669 cp = cp_token_cache_new (first_token,
33670 cp_lexer_peek_nth_token (parser->lexer, 2));
33671 if (errs != errorcount)
33672 break;
33675 cp_parser_require_pragma_eol (parser, pragma_tok);
33677 done:
33678 /* Free any declarators allocated. */
33679 obstack_free (&declarator_obstack, p);
33682 /* OpenMP 4.0
33683 #pragma omp declare simd declare-simd-clauses[optseq] new-line
33684 #pragma omp declare reduction (reduction-id : typename-list : expression) \
33685 initializer-clause[opt] new-line
33686 #pragma omp declare target new-line */
33688 static void
33689 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
33690 enum pragma_context context)
33692 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33694 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33695 const char *p = IDENTIFIER_POINTER (id);
33697 if (strcmp (p, "simd") == 0)
33699 cp_lexer_consume_token (parser->lexer);
33700 cp_parser_omp_declare_simd (parser, pragma_tok,
33701 context);
33702 return;
33704 cp_ensure_no_omp_declare_simd (parser);
33705 if (strcmp (p, "reduction") == 0)
33707 cp_lexer_consume_token (parser->lexer);
33708 cp_parser_omp_declare_reduction (parser, pragma_tok,
33709 context);
33710 return;
33712 if (!flag_openmp) /* flag_openmp_simd */
33714 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33715 return;
33717 if (strcmp (p, "target") == 0)
33719 cp_lexer_consume_token (parser->lexer);
33720 cp_parser_omp_declare_target (parser, pragma_tok);
33721 return;
33724 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
33725 "or %<target%>");
33726 cp_parser_require_pragma_eol (parser, pragma_tok);
33729 /* OpenMP 4.1:
33730 7 #pragma omp taskloop taskloop-clause[optseq] new-line
33731 for-loop
33733 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
33734 for-loop */
33736 #define OMP_TASKLOOP_CLAUSE_MASK \
33737 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
33742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
33743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
33744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
33745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
33746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
33748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
33749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
33750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
33752 static tree
33753 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
33754 char *p_name, omp_clause_mask mask, tree *cclauses)
33756 tree clauses, sb, ret;
33757 unsigned int save;
33758 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33760 strcat (p_name, " taskloop");
33761 mask |= OMP_TASKLOOP_CLAUSE_MASK;
33763 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33765 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33766 const char *p = IDENTIFIER_POINTER (id);
33768 if (strcmp (p, "simd") == 0)
33770 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33771 if (cclauses == NULL)
33772 cclauses = cclauses_buf;
33774 cp_lexer_consume_token (parser->lexer);
33775 if (!flag_openmp) /* flag_openmp_simd */
33776 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33777 cclauses);
33778 sb = begin_omp_structured_block ();
33779 save = cp_parser_begin_omp_structured_block (parser);
33780 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33781 cclauses);
33782 cp_parser_end_omp_structured_block (parser, save);
33783 tree body = finish_omp_structured_block (sb);
33784 if (ret == NULL)
33785 return ret;
33786 ret = make_node (OMP_TASKLOOP);
33787 TREE_TYPE (ret) = void_type_node;
33788 OMP_FOR_BODY (ret) = body;
33789 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
33790 SET_EXPR_LOCATION (ret, loc);
33791 add_stmt (ret);
33792 return ret;
33795 if (!flag_openmp) /* flag_openmp_simd */
33797 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33798 return NULL_TREE;
33801 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33802 cclauses == NULL);
33803 if (cclauses)
33805 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
33806 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
33809 sb = begin_omp_structured_block ();
33810 save = cp_parser_begin_omp_structured_block (parser);
33812 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses);
33814 cp_parser_end_omp_structured_block (parser, save);
33815 add_stmt (finish_omp_structured_block (sb));
33817 return ret;
33820 /* Main entry point to OpenMP statement pragmas. */
33822 static void
33823 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
33825 tree stmt;
33826 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
33827 omp_clause_mask mask (0);
33829 switch (pragma_tok->pragma_kind)
33831 case PRAGMA_OACC_CACHE:
33832 stmt = cp_parser_oacc_cache (parser, pragma_tok);
33833 break;
33834 case PRAGMA_OACC_DATA:
33835 stmt = cp_parser_oacc_data (parser, pragma_tok);
33836 break;
33837 case PRAGMA_OACC_ENTER_DATA:
33838 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
33839 break;
33840 case PRAGMA_OACC_EXIT_DATA:
33841 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
33842 break;
33843 case PRAGMA_OACC_KERNELS:
33844 stmt = cp_parser_oacc_kernels (parser, pragma_tok);
33845 break;
33846 case PRAGMA_OACC_LOOP:
33847 stmt = cp_parser_oacc_loop (parser, pragma_tok);
33848 break;
33849 case PRAGMA_OACC_PARALLEL:
33850 stmt = cp_parser_oacc_parallel (parser, pragma_tok);
33851 break;
33852 case PRAGMA_OACC_UPDATE:
33853 stmt = cp_parser_oacc_update (parser, pragma_tok);
33854 break;
33855 case PRAGMA_OACC_WAIT:
33856 stmt = cp_parser_oacc_wait (parser, pragma_tok);
33857 break;
33858 case PRAGMA_OMP_ATOMIC:
33859 cp_parser_omp_atomic (parser, pragma_tok);
33860 return;
33861 case PRAGMA_OMP_CRITICAL:
33862 stmt = cp_parser_omp_critical (parser, pragma_tok);
33863 break;
33864 case PRAGMA_OMP_DISTRIBUTE:
33865 strcpy (p_name, "#pragma omp");
33866 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
33867 break;
33868 case PRAGMA_OMP_FOR:
33869 strcpy (p_name, "#pragma omp");
33870 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
33871 break;
33872 case PRAGMA_OMP_MASTER:
33873 stmt = cp_parser_omp_master (parser, pragma_tok);
33874 break;
33875 case PRAGMA_OMP_PARALLEL:
33876 strcpy (p_name, "#pragma omp");
33877 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
33878 break;
33879 case PRAGMA_OMP_SECTIONS:
33880 strcpy (p_name, "#pragma omp");
33881 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
33882 break;
33883 case PRAGMA_OMP_SIMD:
33884 strcpy (p_name, "#pragma omp");
33885 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
33886 break;
33887 case PRAGMA_OMP_SINGLE:
33888 stmt = cp_parser_omp_single (parser, pragma_tok);
33889 break;
33890 case PRAGMA_OMP_TASK:
33891 stmt = cp_parser_omp_task (parser, pragma_tok);
33892 break;
33893 case PRAGMA_OMP_TASKGROUP:
33894 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
33895 break;
33896 case PRAGMA_OMP_TASKLOOP:
33897 strcpy (p_name, "#pragma omp");
33898 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL);
33899 break;
33900 case PRAGMA_OMP_TEAMS:
33901 strcpy (p_name, "#pragma omp");
33902 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
33903 break;
33904 default:
33905 gcc_unreachable ();
33908 if (stmt)
33909 SET_EXPR_LOCATION (stmt, pragma_tok->location);
33912 /* Transactional Memory parsing routines. */
33914 /* Parse a transaction attribute.
33916 txn-attribute:
33917 attribute
33918 [ [ identifier ] ]
33920 ??? Simplify this when C++0x bracket attributes are
33921 implemented properly. */
33923 static tree
33924 cp_parser_txn_attribute_opt (cp_parser *parser)
33926 cp_token *token;
33927 tree attr_name, attr = NULL;
33929 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
33930 return cp_parser_attributes_opt (parser);
33932 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
33933 return NULL_TREE;
33934 cp_lexer_consume_token (parser->lexer);
33935 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
33936 goto error1;
33938 token = cp_lexer_peek_token (parser->lexer);
33939 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
33941 token = cp_lexer_consume_token (parser->lexer);
33943 attr_name = (token->type == CPP_KEYWORD
33944 /* For keywords, use the canonical spelling,
33945 not the parsed identifier. */
33946 ? ridpointers[(int) token->keyword]
33947 : token->u.value);
33948 attr = build_tree_list (attr_name, NULL_TREE);
33950 else
33951 cp_parser_error (parser, "expected identifier");
33953 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
33954 error1:
33955 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
33956 return attr;
33959 /* Parse a __transaction_atomic or __transaction_relaxed statement.
33961 transaction-statement:
33962 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
33963 compound-statement
33964 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
33967 static tree
33968 cp_parser_transaction (cp_parser *parser, enum rid keyword)
33970 unsigned char old_in = parser->in_transaction;
33971 unsigned char this_in = 1, new_in;
33972 cp_token *token;
33973 tree stmt, attrs, noex;
33975 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
33976 || keyword == RID_TRANSACTION_RELAXED);
33977 token = cp_parser_require_keyword (parser, keyword,
33978 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
33979 : RT_TRANSACTION_RELAXED));
33980 gcc_assert (token != NULL);
33982 if (keyword == RID_TRANSACTION_RELAXED)
33983 this_in |= TM_STMT_ATTR_RELAXED;
33984 else
33986 attrs = cp_parser_txn_attribute_opt (parser);
33987 if (attrs)
33988 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
33991 /* Parse a noexcept specification. */
33992 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
33994 /* Keep track if we're in the lexical scope of an outer transaction. */
33995 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
33997 stmt = begin_transaction_stmt (token->location, NULL, this_in);
33999 parser->in_transaction = new_in;
34000 cp_parser_compound_statement (parser, NULL, false, false);
34001 parser->in_transaction = old_in;
34003 finish_transaction_stmt (stmt, NULL, this_in, noex);
34005 return stmt;
34008 /* Parse a __transaction_atomic or __transaction_relaxed expression.
34010 transaction-expression:
34011 __transaction_atomic txn-noexcept-spec[opt] ( expression )
34012 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
34015 static tree
34016 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
34018 unsigned char old_in = parser->in_transaction;
34019 unsigned char this_in = 1;
34020 cp_token *token;
34021 tree expr, noex;
34022 bool noex_expr;
34024 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
34025 || keyword == RID_TRANSACTION_RELAXED);
34027 if (!flag_tm)
34028 error (keyword == RID_TRANSACTION_RELAXED
34029 ? G_("%<__transaction_relaxed%> without transactional memory "
34030 "support enabled")
34031 : G_("%<__transaction_atomic%> without transactional memory "
34032 "support enabled"));
34034 token = cp_parser_require_keyword (parser, keyword,
34035 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
34036 : RT_TRANSACTION_RELAXED));
34037 gcc_assert (token != NULL);
34039 if (keyword == RID_TRANSACTION_RELAXED)
34040 this_in |= TM_STMT_ATTR_RELAXED;
34042 /* Set this early. This might mean that we allow transaction_cancel in
34043 an expression that we find out later actually has to be a constexpr.
34044 However, we expect that cxx_constant_value will be able to deal with
34045 this; also, if the noexcept has no constexpr, then what we parse next
34046 really is a transaction's body. */
34047 parser->in_transaction = this_in;
34049 /* Parse a noexcept specification. */
34050 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
34051 true);
34053 if (!noex || !noex_expr
34054 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34056 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
34058 expr = cp_parser_expression (parser);
34059 expr = finish_parenthesized_expr (expr);
34061 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
34063 else
34065 /* The only expression that is available got parsed for the noexcept
34066 already. noexcept is true then. */
34067 expr = noex;
34068 noex = boolean_true_node;
34071 expr = build_transaction_expr (token->location, expr, this_in, noex);
34072 parser->in_transaction = old_in;
34074 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
34075 return error_mark_node;
34077 return (flag_tm ? expr : error_mark_node);
34080 /* Parse a function-transaction-block.
34082 function-transaction-block:
34083 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
34084 function-body
34085 __transaction_atomic txn-attribute[opt] function-try-block
34086 __transaction_relaxed ctor-initializer[opt] function-body
34087 __transaction_relaxed function-try-block
34090 static bool
34091 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
34093 unsigned char old_in = parser->in_transaction;
34094 unsigned char new_in = 1;
34095 tree compound_stmt, stmt, attrs;
34096 bool ctor_initializer_p;
34097 cp_token *token;
34099 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
34100 || keyword == RID_TRANSACTION_RELAXED);
34101 token = cp_parser_require_keyword (parser, keyword,
34102 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
34103 : RT_TRANSACTION_RELAXED));
34104 gcc_assert (token != NULL);
34106 if (keyword == RID_TRANSACTION_RELAXED)
34107 new_in |= TM_STMT_ATTR_RELAXED;
34108 else
34110 attrs = cp_parser_txn_attribute_opt (parser);
34111 if (attrs)
34112 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
34115 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
34117 parser->in_transaction = new_in;
34119 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
34120 ctor_initializer_p = cp_parser_function_try_block (parser);
34121 else
34122 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
34123 (parser, /*in_function_try_block=*/false);
34125 parser->in_transaction = old_in;
34127 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
34129 return ctor_initializer_p;
34132 /* Parse a __transaction_cancel statement.
34134 cancel-statement:
34135 __transaction_cancel txn-attribute[opt] ;
34136 __transaction_cancel txn-attribute[opt] throw-expression ;
34138 ??? Cancel and throw is not yet implemented. */
34140 static tree
34141 cp_parser_transaction_cancel (cp_parser *parser)
34143 cp_token *token;
34144 bool is_outer = false;
34145 tree stmt, attrs;
34147 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
34148 RT_TRANSACTION_CANCEL);
34149 gcc_assert (token != NULL);
34151 attrs = cp_parser_txn_attribute_opt (parser);
34152 if (attrs)
34153 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
34155 /* ??? Parse cancel-and-throw here. */
34157 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34159 if (!flag_tm)
34161 error_at (token->location, "%<__transaction_cancel%> without "
34162 "transactional memory support enabled");
34163 return error_mark_node;
34165 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
34167 error_at (token->location, "%<__transaction_cancel%> within a "
34168 "%<__transaction_relaxed%>");
34169 return error_mark_node;
34171 else if (is_outer)
34173 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
34174 && !is_tm_may_cancel_outer (current_function_decl))
34176 error_at (token->location, "outer %<__transaction_cancel%> not "
34177 "within outer %<__transaction_atomic%>");
34178 error_at (token->location,
34179 " or a %<transaction_may_cancel_outer%> function");
34180 return error_mark_node;
34183 else if (parser->in_transaction == 0)
34185 error_at (token->location, "%<__transaction_cancel%> not within "
34186 "%<__transaction_atomic%>");
34187 return error_mark_node;
34190 stmt = build_tm_abort_call (token->location, is_outer);
34191 add_stmt (stmt);
34193 return stmt;
34196 /* The parser. */
34198 static GTY (()) cp_parser *the_parser;
34201 /* Special handling for the first token or line in the file. The first
34202 thing in the file might be #pragma GCC pch_preprocess, which loads a
34203 PCH file, which is a GC collection point. So we need to handle this
34204 first pragma without benefit of an existing lexer structure.
34206 Always returns one token to the caller in *FIRST_TOKEN. This is
34207 either the true first token of the file, or the first token after
34208 the initial pragma. */
34210 static void
34211 cp_parser_initial_pragma (cp_token *first_token)
34213 tree name = NULL;
34215 cp_lexer_get_preprocessor_token (NULL, first_token);
34216 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
34217 return;
34219 cp_lexer_get_preprocessor_token (NULL, first_token);
34220 if (first_token->type == CPP_STRING)
34222 name = first_token->u.value;
34224 cp_lexer_get_preprocessor_token (NULL, first_token);
34225 if (first_token->type != CPP_PRAGMA_EOL)
34226 error_at (first_token->location,
34227 "junk at end of %<#pragma GCC pch_preprocess%>");
34229 else
34230 error_at (first_token->location, "expected string literal");
34232 /* Skip to the end of the pragma. */
34233 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
34234 cp_lexer_get_preprocessor_token (NULL, first_token);
34236 /* Now actually load the PCH file. */
34237 if (name)
34238 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
34240 /* Read one more token to return to our caller. We have to do this
34241 after reading the PCH file in, since its pointers have to be
34242 live. */
34243 cp_lexer_get_preprocessor_token (NULL, first_token);
34246 /* Parses the grainsize pragma for the _Cilk_for statement.
34247 Syntax:
34248 #pragma cilk grainsize = <VALUE>. */
34250 static void
34251 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
34253 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
34255 tree exp = cp_parser_binary_expression (parser, false, false,
34256 PREC_NOT_OPERATOR, NULL);
34257 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34258 if (!exp || exp == error_mark_node)
34260 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
34261 return;
34264 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
34265 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34266 cp_parser_cilk_for (parser, exp);
34267 else
34268 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
34269 "%<#pragma cilk grainsize%> is not followed by "
34270 "%<_Cilk_for%>");
34271 return;
34273 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34276 /* Normal parsing of a pragma token. Here we can (and must) use the
34277 regular lexer. */
34279 static bool
34280 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
34282 cp_token *pragma_tok;
34283 unsigned int id;
34284 tree stmt;
34285 bool ret;
34287 pragma_tok = cp_lexer_consume_token (parser->lexer);
34288 gcc_assert (pragma_tok->type == CPP_PRAGMA);
34289 parser->lexer->in_pragma = true;
34291 id = pragma_tok->pragma_kind;
34292 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
34293 cp_ensure_no_omp_declare_simd (parser);
34294 switch (id)
34296 case PRAGMA_GCC_PCH_PREPROCESS:
34297 error_at (pragma_tok->location,
34298 "%<#pragma GCC pch_preprocess%> must be first");
34299 break;
34301 case PRAGMA_OMP_BARRIER:
34302 switch (context)
34304 case pragma_compound:
34305 cp_parser_omp_barrier (parser, pragma_tok);
34306 return false;
34307 case pragma_stmt:
34308 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
34309 "used in compound statements");
34310 break;
34311 default:
34312 goto bad_stmt;
34314 break;
34316 case PRAGMA_OMP_FLUSH:
34317 switch (context)
34319 case pragma_compound:
34320 cp_parser_omp_flush (parser, pragma_tok);
34321 return false;
34322 case pragma_stmt:
34323 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
34324 "used in compound statements");
34325 break;
34326 default:
34327 goto bad_stmt;
34329 break;
34331 case PRAGMA_OMP_TASKWAIT:
34332 switch (context)
34334 case pragma_compound:
34335 cp_parser_omp_taskwait (parser, pragma_tok);
34336 return false;
34337 case pragma_stmt:
34338 error_at (pragma_tok->location,
34339 "%<#pragma omp taskwait%> may only be "
34340 "used in compound statements");
34341 break;
34342 default:
34343 goto bad_stmt;
34345 break;
34347 case PRAGMA_OMP_TASKYIELD:
34348 switch (context)
34350 case pragma_compound:
34351 cp_parser_omp_taskyield (parser, pragma_tok);
34352 return false;
34353 case pragma_stmt:
34354 error_at (pragma_tok->location,
34355 "%<#pragma omp taskyield%> may only be "
34356 "used in compound statements");
34357 break;
34358 default:
34359 goto bad_stmt;
34361 break;
34363 case PRAGMA_OMP_CANCEL:
34364 switch (context)
34366 case pragma_compound:
34367 cp_parser_omp_cancel (parser, pragma_tok);
34368 return false;
34369 case pragma_stmt:
34370 error_at (pragma_tok->location,
34371 "%<#pragma omp cancel%> may only be "
34372 "used in compound statements");
34373 break;
34374 default:
34375 goto bad_stmt;
34377 break;
34379 case PRAGMA_OMP_CANCELLATION_POINT:
34380 switch (context)
34382 case pragma_compound:
34383 cp_parser_omp_cancellation_point (parser, pragma_tok);
34384 return false;
34385 case pragma_stmt:
34386 error_at (pragma_tok->location,
34387 "%<#pragma omp cancellation point%> may only be "
34388 "used in compound statements");
34389 break;
34390 default:
34391 goto bad_stmt;
34393 break;
34395 case PRAGMA_OMP_THREADPRIVATE:
34396 cp_parser_omp_threadprivate (parser, pragma_tok);
34397 return false;
34399 case PRAGMA_OMP_DECLARE_REDUCTION:
34400 cp_parser_omp_declare (parser, pragma_tok, context);
34401 return false;
34403 case PRAGMA_OACC_CACHE:
34404 case PRAGMA_OACC_DATA:
34405 case PRAGMA_OACC_ENTER_DATA:
34406 case PRAGMA_OACC_EXIT_DATA:
34407 case PRAGMA_OACC_KERNELS:
34408 case PRAGMA_OACC_PARALLEL:
34409 case PRAGMA_OACC_LOOP:
34410 case PRAGMA_OACC_UPDATE:
34411 case PRAGMA_OACC_WAIT:
34412 case PRAGMA_OMP_ATOMIC:
34413 case PRAGMA_OMP_CRITICAL:
34414 case PRAGMA_OMP_DISTRIBUTE:
34415 case PRAGMA_OMP_FOR:
34416 case PRAGMA_OMP_MASTER:
34417 case PRAGMA_OMP_PARALLEL:
34418 case PRAGMA_OMP_SECTIONS:
34419 case PRAGMA_OMP_SIMD:
34420 case PRAGMA_OMP_SINGLE:
34421 case PRAGMA_OMP_TASK:
34422 case PRAGMA_OMP_TASKGROUP:
34423 case PRAGMA_OMP_TASKLOOP:
34424 case PRAGMA_OMP_TEAMS:
34425 if (context != pragma_stmt && context != pragma_compound)
34426 goto bad_stmt;
34427 stmt = push_omp_privatization_clauses (false);
34428 cp_parser_omp_construct (parser, pragma_tok);
34429 pop_omp_privatization_clauses (stmt);
34430 return true;
34432 case PRAGMA_OMP_ORDERED:
34433 stmt = push_omp_privatization_clauses (false);
34434 ret = cp_parser_omp_ordered (parser, pragma_tok, context);
34435 pop_omp_privatization_clauses (stmt);
34436 return ret;
34438 case PRAGMA_OMP_TARGET:
34439 stmt = push_omp_privatization_clauses (false);
34440 ret = cp_parser_omp_target (parser, pragma_tok, context);
34441 pop_omp_privatization_clauses (stmt);
34442 return ret;
34444 case PRAGMA_OMP_END_DECLARE_TARGET:
34445 cp_parser_omp_end_declare_target (parser, pragma_tok);
34446 return false;
34448 case PRAGMA_OMP_SECTION:
34449 error_at (pragma_tok->location,
34450 "%<#pragma omp section%> may only be used in "
34451 "%<#pragma omp sections%> construct");
34452 break;
34454 case PRAGMA_IVDEP:
34456 if (context == pragma_external)
34458 error_at (pragma_tok->location,
34459 "%<#pragma GCC ivdep%> must be inside a function");
34460 break;
34462 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34463 cp_token *tok;
34464 tok = cp_lexer_peek_token (the_parser->lexer);
34465 if (tok->type != CPP_KEYWORD
34466 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
34467 && tok->keyword != RID_DO))
34469 cp_parser_error (parser, "for, while or do statement expected");
34470 return false;
34472 cp_parser_iteration_statement (parser, true);
34473 return true;
34476 case PRAGMA_CILK_SIMD:
34477 if (context == pragma_external)
34479 error_at (pragma_tok->location,
34480 "%<#pragma simd%> must be inside a function");
34481 break;
34483 stmt = push_omp_privatization_clauses (false);
34484 cp_parser_cilk_simd (parser, pragma_tok);
34485 pop_omp_privatization_clauses (stmt);
34486 return true;
34488 case PRAGMA_CILK_GRAINSIZE:
34489 if (context == pragma_external)
34491 error_at (pragma_tok->location,
34492 "%<#pragma cilk grainsize%> must be inside a function");
34493 break;
34496 /* Ignore the pragma if Cilk Plus is not enabled. */
34497 if (flag_cilkplus)
34499 cp_parser_cilk_grainsize (parser, pragma_tok);
34500 return true;
34502 else
34504 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
34505 "%<#pragma cilk grainsize%>");
34506 break;
34509 default:
34510 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
34511 c_invoke_pragma_handler (id);
34512 break;
34514 bad_stmt:
34515 cp_parser_error (parser, "expected declaration specifiers");
34516 break;
34519 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34520 return false;
34523 /* The interface the pragma parsers have to the lexer. */
34525 enum cpp_ttype
34526 pragma_lex (tree *value)
34528 cp_token *tok;
34529 enum cpp_ttype ret;
34531 tok = cp_lexer_peek_token (the_parser->lexer);
34533 ret = tok->type;
34534 *value = tok->u.value;
34536 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
34537 ret = CPP_EOF;
34538 else if (ret == CPP_STRING)
34539 *value = cp_parser_string_literal (the_parser, false, false);
34540 else
34542 cp_lexer_consume_token (the_parser->lexer);
34543 if (ret == CPP_KEYWORD)
34544 ret = CPP_NAME;
34547 return ret;
34551 /* External interface. */
34553 /* Parse one entire translation unit. */
34555 void
34556 c_parse_file (void)
34558 static bool already_called = false;
34560 if (already_called)
34561 fatal_error (input_location,
34562 "inter-module optimizations not implemented for C++");
34563 already_called = true;
34565 the_parser = cp_parser_new ();
34566 push_deferring_access_checks (flag_access_control
34567 ? dk_no_deferred : dk_no_check);
34568 cp_parser_translation_unit (the_parser);
34569 the_parser = NULL;
34572 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
34573 vectorlength clause:
34574 Syntax:
34575 vectorlength ( constant-expression ) */
34577 static tree
34578 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
34579 bool is_simd_fn)
34581 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34582 tree expr;
34583 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
34584 safelen clause. Thus, vectorlength is represented as OMP 4.0
34585 safelen. For SIMD-enabled function it is represented by OMP 4.0
34586 simdlen. */
34587 if (!is_simd_fn)
34588 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
34589 loc);
34590 else
34591 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
34592 loc);
34594 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34595 return error_mark_node;
34597 expr = cp_parser_constant_expression (parser);
34598 expr = maybe_constant_value (expr);
34600 /* If expr == error_mark_node, then don't emit any errors nor
34601 create a clause. if any of the above functions returns
34602 error mark node then they would have emitted an error message. */
34603 if (expr == error_mark_node)
34605 else if (!TREE_TYPE (expr)
34606 || !TREE_CONSTANT (expr)
34607 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
34608 error_at (loc, "vectorlength must be an integer constant");
34609 else if (TREE_CONSTANT (expr)
34610 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
34611 error_at (loc, "vectorlength must be a power of 2");
34612 else
34614 tree c;
34615 if (!is_simd_fn)
34617 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
34618 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
34619 OMP_CLAUSE_CHAIN (c) = clauses;
34620 clauses = c;
34622 else
34624 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
34625 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
34626 OMP_CLAUSE_CHAIN (c) = clauses;
34627 clauses = c;
34631 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34632 return error_mark_node;
34633 return clauses;
34636 /* Handles the Cilk Plus #pragma simd linear clause.
34637 Syntax:
34638 linear ( simd-linear-variable-list )
34640 simd-linear-variable-list:
34641 simd-linear-variable
34642 simd-linear-variable-list , simd-linear-variable
34644 simd-linear-variable:
34645 id-expression
34646 id-expression : simd-linear-step
34648 simd-linear-step:
34649 conditional-expression */
34651 static tree
34652 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
34654 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34656 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34657 return clauses;
34658 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34660 cp_parser_error (parser, "expected identifier");
34661 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
34662 return error_mark_node;
34665 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
34666 parser->colon_corrects_to_scope_p = false;
34667 while (1)
34669 cp_token *token = cp_lexer_peek_token (parser->lexer);
34670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34672 cp_parser_error (parser, "expected variable-name");
34673 clauses = error_mark_node;
34674 break;
34677 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
34678 false, false);
34679 tree decl = cp_parser_lookup_name_simple (parser, var_name,
34680 token->location);
34681 if (decl == error_mark_node)
34683 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
34684 token->location);
34685 clauses = error_mark_node;
34687 else
34689 tree e = NULL_TREE;
34690 tree step_size = integer_one_node;
34692 /* If present, parse the linear step. Otherwise, assume the default
34693 value of 1. */
34694 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
34696 cp_lexer_consume_token (parser->lexer);
34698 e = cp_parser_assignment_expression (parser);
34699 e = maybe_constant_value (e);
34701 if (e == error_mark_node)
34703 /* If an error has occurred, then the whole pragma is
34704 considered ill-formed. Thus, no reason to keep
34705 parsing. */
34706 clauses = error_mark_node;
34707 break;
34709 else if (type_dependent_expression_p (e)
34710 || value_dependent_expression_p (e)
34711 || (TREE_TYPE (e)
34712 && INTEGRAL_TYPE_P (TREE_TYPE (e))
34713 && (TREE_CONSTANT (e)
34714 || DECL_P (e))))
34715 step_size = e;
34716 else
34717 cp_parser_error (parser,
34718 "step size must be an integer constant "
34719 "expression or an integer variable");
34722 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
34723 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34724 OMP_CLAUSE_DECL (l) = decl;
34725 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
34726 OMP_CLAUSE_CHAIN (l) = clauses;
34727 clauses = l;
34729 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34730 cp_lexer_consume_token (parser->lexer);
34731 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34732 break;
34733 else
34735 error_at (cp_lexer_peek_token (parser->lexer)->location,
34736 "expected %<,%> or %<)%> after %qE", decl);
34737 clauses = error_mark_node;
34738 break;
34741 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34742 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
34743 return clauses;
34746 /* Returns the name of the next clause. If the clause is not
34747 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
34748 token is not consumed. Otherwise, the appropriate enum from the
34749 pragma_simd_clause is returned and the token is consumed. */
34751 static pragma_omp_clause
34752 cp_parser_cilk_simd_clause_name (cp_parser *parser)
34754 pragma_omp_clause clause_type;
34755 cp_token *token = cp_lexer_peek_token (parser->lexer);
34757 if (token->keyword == RID_PRIVATE)
34758 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
34759 else if (!token->u.value || token->type != CPP_NAME)
34760 return PRAGMA_CILK_CLAUSE_NONE;
34761 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
34762 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
34763 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
34764 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
34765 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
34766 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
34767 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
34768 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
34769 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
34770 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
34771 else
34772 return PRAGMA_CILK_CLAUSE_NONE;
34774 cp_lexer_consume_token (parser->lexer);
34775 return clause_type;
34778 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
34780 static tree
34781 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
34783 tree clauses = NULL_TREE;
34785 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
34786 && clauses != error_mark_node)
34788 pragma_omp_clause c_kind;
34789 c_kind = cp_parser_cilk_simd_clause_name (parser);
34790 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
34791 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
34792 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
34793 clauses = cp_parser_cilk_simd_linear (parser, clauses);
34794 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
34795 /* Use the OpenMP 4.0 equivalent function. */
34796 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
34797 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
34798 /* Use the OpenMP 4.0 equivalent function. */
34799 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34800 clauses);
34801 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
34802 /* Use the OMP 4.0 equivalent function. */
34803 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34804 clauses);
34805 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
34806 /* Use the OMP 4.0 equivalent function. */
34807 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34808 else
34810 clauses = error_mark_node;
34811 cp_parser_error (parser, "expected %<#pragma simd%> clause");
34812 break;
34816 cp_parser_skip_to_pragma_eol (parser, pragma_token);
34818 if (clauses == error_mark_node)
34819 return error_mark_node;
34820 else
34821 return c_finish_cilk_clauses (clauses);
34824 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
34826 static void
34827 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
34829 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
34831 if (clauses == error_mark_node)
34832 return;
34834 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
34836 error_at (cp_lexer_peek_token (parser->lexer)->location,
34837 "for statement expected");
34838 return;
34841 tree sb = begin_omp_structured_block ();
34842 int save = cp_parser_begin_omp_structured_block (parser);
34843 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
34844 if (ret)
34845 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
34846 cp_parser_end_omp_structured_block (parser, save);
34847 add_stmt (finish_omp_structured_block (sb));
34850 /* Main entry-point for parsing Cilk Plus _Cilk_for
34851 loops. The return value is error_mark_node
34852 when errors happen and CILK_FOR tree on success. */
34854 static tree
34855 cp_parser_cilk_for (cp_parser *parser, tree grain)
34857 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
34858 gcc_unreachable ();
34860 tree sb = begin_omp_structured_block ();
34861 int save = cp_parser_begin_omp_structured_block (parser);
34863 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
34864 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
34865 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
34866 clauses = finish_omp_clauses (clauses, false);
34868 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
34869 if (ret)
34870 cpp_validate_cilk_plus_loop (ret);
34871 else
34872 ret = error_mark_node;
34874 cp_parser_end_omp_structured_block (parser, save);
34875 add_stmt (finish_omp_structured_block (sb));
34876 return ret;
34879 /* Create an identifier for a generic parameter type (a synthesized
34880 template parameter implied by `auto' or a concept identifier). */
34882 static GTY(()) int generic_parm_count;
34883 static tree
34884 make_generic_type_name ()
34886 char buf[32];
34887 sprintf (buf, "auto:%d", ++generic_parm_count);
34888 return get_identifier (buf);
34891 /* Predicate that behaves as is_auto_or_concept but matches the parent
34892 node of the generic type rather than the generic type itself. This
34893 allows for type transformation in add_implicit_template_parms. */
34895 static inline bool
34896 tree_type_is_auto_or_concept (const_tree t)
34898 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
34901 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
34902 (creating a new template parameter list if necessary). Returns the newly
34903 created template type parm. */
34905 tree
34906 synthesize_implicit_template_parm (cp_parser *parser)
34908 gcc_assert (current_binding_level->kind == sk_function_parms);
34910 /* We are either continuing a function template that already contains implicit
34911 template parameters, creating a new fully-implicit function template, or
34912 extending an existing explicit function template with implicit template
34913 parameters. */
34915 cp_binding_level *const entry_scope = current_binding_level;
34917 bool become_template = false;
34918 cp_binding_level *parent_scope = 0;
34920 if (parser->implicit_template_scope)
34922 gcc_assert (parser->implicit_template_parms);
34924 current_binding_level = parser->implicit_template_scope;
34926 else
34928 /* Roll back to the existing template parameter scope (in the case of
34929 extending an explicit function template) or introduce a new template
34930 parameter scope ahead of the function parameter scope (or class scope
34931 in the case of out-of-line member definitions). The function scope is
34932 added back after template parameter synthesis below. */
34934 cp_binding_level *scope = entry_scope;
34936 while (scope->kind == sk_function_parms)
34938 parent_scope = scope;
34939 scope = scope->level_chain;
34941 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
34943 /* If not defining a class, then any class scope is a scope level in
34944 an out-of-line member definition. In this case simply wind back
34945 beyond the first such scope to inject the template parameter list.
34946 Otherwise wind back to the class being defined. The latter can
34947 occur in class member friend declarations such as:
34949 class A {
34950 void foo (auto);
34952 class B {
34953 friend void A::foo (auto);
34956 The template parameter list synthesized for the friend declaration
34957 must be injected in the scope of 'B'. This can also occur in
34958 erroneous cases such as:
34960 struct A {
34961 struct B {
34962 void foo (auto);
34964 void B::foo (auto) {}
34967 Here the attempted definition of 'B::foo' within 'A' is ill-formed
34968 but, nevertheless, the template parameter list synthesized for the
34969 declarator should be injected into the scope of 'A' as if the
34970 ill-formed template was specified explicitly. */
34972 while (scope->kind == sk_class && !scope->defining_class_p)
34974 parent_scope = scope;
34975 scope = scope->level_chain;
34979 current_binding_level = scope;
34981 if (scope->kind != sk_template_parms
34982 || !function_being_declared_is_template_p (parser))
34984 /* Introduce a new template parameter list for implicit template
34985 parameters. */
34987 become_template = true;
34989 parser->implicit_template_scope
34990 = begin_scope (sk_template_parms, NULL);
34992 ++processing_template_decl;
34994 parser->fully_implicit_function_template_p = true;
34995 ++parser->num_template_parameter_lists;
34997 else
34999 /* Synthesize implicit template parameters at the end of the explicit
35000 template parameter list. */
35002 gcc_assert (current_template_parms);
35004 parser->implicit_template_scope = scope;
35006 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
35007 parser->implicit_template_parms
35008 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
35012 /* Synthesize a new template parameter and track the current template
35013 parameter chain with implicit_template_parms. */
35015 tree synth_id = make_generic_type_name ();
35016 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
35017 synth_id);
35018 tree new_parm
35019 = process_template_parm (parser->implicit_template_parms,
35020 input_location,
35021 build_tree_list (NULL_TREE, synth_tmpl_parm),
35022 /*non_type=*/false,
35023 /*param_pack=*/false);
35026 if (parser->implicit_template_parms)
35027 parser->implicit_template_parms
35028 = TREE_CHAIN (parser->implicit_template_parms);
35029 else
35030 parser->implicit_template_parms = new_parm;
35032 tree new_type = TREE_TYPE (getdecls ());
35034 /* If creating a fully implicit function template, start the new implicit
35035 template parameter list with this synthesized type, otherwise grow the
35036 current template parameter list. */
35038 if (become_template)
35040 parent_scope->level_chain = current_binding_level;
35042 tree new_parms = make_tree_vec (1);
35043 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
35044 current_template_parms = tree_cons (size_int (processing_template_decl),
35045 new_parms, current_template_parms);
35047 else
35049 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
35050 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
35051 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
35052 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
35055 current_binding_level = entry_scope;
35057 return new_type;
35060 /* Finish the declaration of a fully implicit function template. Such a
35061 template has no explicit template parameter list so has not been through the
35062 normal template head and tail processing. synthesize_implicit_template_parm
35063 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
35064 provided if the declaration is a class member such that its template
35065 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
35066 form is returned. Otherwise NULL_TREE is returned. */
35068 tree
35069 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
35071 gcc_assert (parser->fully_implicit_function_template_p);
35073 if (member_decl_opt && member_decl_opt != error_mark_node
35074 && DECL_VIRTUAL_P (member_decl_opt))
35076 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
35077 "implicit templates may not be %<virtual%>");
35078 DECL_VIRTUAL_P (member_decl_opt) = false;
35081 if (member_decl_opt)
35082 member_decl_opt = finish_member_template_decl (member_decl_opt);
35083 end_template_decl ();
35085 parser->fully_implicit_function_template_p = false;
35086 --parser->num_template_parameter_lists;
35088 return member_decl_opt;
35091 #include "gt-cp-parser.h"