/cp
[official-gcc.git] / gcc / cp / parser.c
blob574ffba3ecec5e6246f458533547d10c834c7f9c
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_declarator *make_ptrmem_declarator
1353 (cp_cv_quals, tree, cp_declarator *, tree);
1355 /* An erroneous declarator. */
1356 static cp_declarator *cp_error_declarator;
1358 /* The obstack on which declarators and related data structures are
1359 allocated. */
1360 static struct obstack declarator_obstack;
1362 /* Alloc BYTES from the declarator memory pool. */
1364 static inline void *
1365 alloc_declarator (size_t bytes)
1367 return obstack_alloc (&declarator_obstack, bytes);
1370 /* Allocate a declarator of the indicated KIND. Clear fields that are
1371 common to all declarators. */
1373 static cp_declarator *
1374 make_declarator (cp_declarator_kind kind)
1376 cp_declarator *declarator;
1378 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1379 declarator->kind = kind;
1380 declarator->attributes = NULL_TREE;
1381 declarator->std_attributes = NULL_TREE;
1382 declarator->declarator = NULL;
1383 declarator->parameter_pack_p = false;
1384 declarator->id_loc = UNKNOWN_LOCATION;
1386 return declarator;
1389 /* Make a declarator for a generalized identifier. If
1390 QUALIFYING_SCOPE is non-NULL, the identifier is
1391 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1392 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1393 is, if any. */
1395 static cp_declarator *
1396 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1397 special_function_kind sfk)
1399 cp_declarator *declarator;
1401 /* It is valid to write:
1403 class C { void f(); };
1404 typedef C D;
1405 void D::f();
1407 The standard is not clear about whether `typedef const C D' is
1408 legal; as of 2002-09-15 the committee is considering that
1409 question. EDG 3.0 allows that syntax. Therefore, we do as
1410 well. */
1411 if (qualifying_scope && TYPE_P (qualifying_scope))
1412 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1414 gcc_assert (identifier_p (unqualified_name)
1415 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1416 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1418 declarator = make_declarator (cdk_id);
1419 declarator->u.id.qualifying_scope = qualifying_scope;
1420 declarator->u.id.unqualified_name = unqualified_name;
1421 declarator->u.id.sfk = sfk;
1423 return declarator;
1426 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1427 of modifiers such as const or volatile to apply to the pointer
1428 type, represented as identifiers. ATTRIBUTES represent the attributes that
1429 appertain to the pointer or reference. */
1431 cp_declarator *
1432 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1433 tree attributes)
1435 cp_declarator *declarator;
1437 declarator = make_declarator (cdk_pointer);
1438 declarator->declarator = target;
1439 declarator->u.pointer.qualifiers = cv_qualifiers;
1440 declarator->u.pointer.class_type = NULL_TREE;
1441 if (target)
1443 declarator->id_loc = target->id_loc;
1444 declarator->parameter_pack_p = target->parameter_pack_p;
1445 target->parameter_pack_p = false;
1447 else
1448 declarator->parameter_pack_p = false;
1450 declarator->std_attributes = attributes;
1452 return declarator;
1455 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1456 represent the attributes that appertain to the pointer or
1457 reference. */
1459 cp_declarator *
1460 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1461 bool rvalue_ref, tree attributes)
1463 cp_declarator *declarator;
1465 declarator = make_declarator (cdk_reference);
1466 declarator->declarator = target;
1467 declarator->u.reference.qualifiers = cv_qualifiers;
1468 declarator->u.reference.rvalue_ref = rvalue_ref;
1469 if (target)
1471 declarator->id_loc = target->id_loc;
1472 declarator->parameter_pack_p = target->parameter_pack_p;
1473 target->parameter_pack_p = false;
1475 else
1476 declarator->parameter_pack_p = false;
1478 declarator->std_attributes = attributes;
1480 return declarator;
1483 /* Like make_pointer_declarator -- but for a pointer to a non-static
1484 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1485 appertain to the pointer or reference. */
1487 cp_declarator *
1488 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1489 cp_declarator *pointee,
1490 tree attributes)
1492 cp_declarator *declarator;
1494 declarator = make_declarator (cdk_ptrmem);
1495 declarator->declarator = pointee;
1496 declarator->u.pointer.qualifiers = cv_qualifiers;
1497 declarator->u.pointer.class_type = class_type;
1499 if (pointee)
1501 declarator->parameter_pack_p = pointee->parameter_pack_p;
1502 pointee->parameter_pack_p = false;
1504 else
1505 declarator->parameter_pack_p = false;
1507 declarator->std_attributes = attributes;
1509 return declarator;
1512 /* Make a declarator for the function given by TARGET, with the
1513 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1514 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1515 indicates what exceptions can be thrown. */
1517 cp_declarator *
1518 make_call_declarator (cp_declarator *target,
1519 tree parms,
1520 cp_cv_quals cv_qualifiers,
1521 cp_virt_specifiers virt_specifiers,
1522 cp_ref_qualifier ref_qualifier,
1523 tree exception_specification,
1524 tree late_return_type)
1526 cp_declarator *declarator;
1528 declarator = make_declarator (cdk_function);
1529 declarator->declarator = target;
1530 declarator->u.function.parameters = parms;
1531 declarator->u.function.qualifiers = cv_qualifiers;
1532 declarator->u.function.virt_specifiers = virt_specifiers;
1533 declarator->u.function.ref_qualifier = ref_qualifier;
1534 declarator->u.function.exception_specification = exception_specification;
1535 declarator->u.function.late_return_type = late_return_type;
1536 if (target)
1538 declarator->id_loc = target->id_loc;
1539 declarator->parameter_pack_p = target->parameter_pack_p;
1540 target->parameter_pack_p = false;
1542 else
1543 declarator->parameter_pack_p = false;
1545 return declarator;
1548 /* Make a declarator for an array of BOUNDS elements, each of which is
1549 defined by ELEMENT. */
1551 cp_declarator *
1552 make_array_declarator (cp_declarator *element, tree bounds)
1554 cp_declarator *declarator;
1556 declarator = make_declarator (cdk_array);
1557 declarator->declarator = element;
1558 declarator->u.array.bounds = bounds;
1559 if (element)
1561 declarator->id_loc = element->id_loc;
1562 declarator->parameter_pack_p = element->parameter_pack_p;
1563 element->parameter_pack_p = false;
1565 else
1566 declarator->parameter_pack_p = false;
1568 return declarator;
1571 /* Determine whether the declarator we've seen so far can be a
1572 parameter pack, when followed by an ellipsis. */
1573 static bool
1574 declarator_can_be_parameter_pack (cp_declarator *declarator)
1576 if (declarator && declarator->parameter_pack_p)
1577 /* We already saw an ellipsis. */
1578 return false;
1580 /* Search for a declarator name, or any other declarator that goes
1581 after the point where the ellipsis could appear in a parameter
1582 pack. If we find any of these, then this declarator can not be
1583 made into a parameter pack. */
1584 bool found = false;
1585 while (declarator && !found)
1587 switch ((int)declarator->kind)
1589 case cdk_id:
1590 case cdk_array:
1591 found = true;
1592 break;
1594 case cdk_error:
1595 return true;
1597 default:
1598 declarator = declarator->declarator;
1599 break;
1603 return !found;
1606 cp_parameter_declarator *no_parameters;
1608 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1609 DECLARATOR and DEFAULT_ARGUMENT. */
1611 cp_parameter_declarator *
1612 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1613 cp_declarator *declarator,
1614 tree default_argument,
1615 bool template_parameter_pack_p = false)
1617 cp_parameter_declarator *parameter;
1619 parameter = ((cp_parameter_declarator *)
1620 alloc_declarator (sizeof (cp_parameter_declarator)));
1621 parameter->next = NULL;
1622 if (decl_specifiers)
1623 parameter->decl_specifiers = *decl_specifiers;
1624 else
1625 clear_decl_specs (&parameter->decl_specifiers);
1626 parameter->declarator = declarator;
1627 parameter->default_argument = default_argument;
1628 parameter->template_parameter_pack_p = template_parameter_pack_p;
1630 return parameter;
1633 /* Returns true iff DECLARATOR is a declaration for a function. */
1635 static bool
1636 function_declarator_p (const cp_declarator *declarator)
1638 while (declarator)
1640 if (declarator->kind == cdk_function
1641 && declarator->declarator->kind == cdk_id)
1642 return true;
1643 if (declarator->kind == cdk_id
1644 || declarator->kind == cdk_error)
1645 return false;
1646 declarator = declarator->declarator;
1648 return false;
1651 /* The parser. */
1653 /* Overview
1654 --------
1656 A cp_parser parses the token stream as specified by the C++
1657 grammar. Its job is purely parsing, not semantic analysis. For
1658 example, the parser breaks the token stream into declarators,
1659 expressions, statements, and other similar syntactic constructs.
1660 It does not check that the types of the expressions on either side
1661 of an assignment-statement are compatible, or that a function is
1662 not declared with a parameter of type `void'.
1664 The parser invokes routines elsewhere in the compiler to perform
1665 semantic analysis and to build up the abstract syntax tree for the
1666 code processed.
1668 The parser (and the template instantiation code, which is, in a
1669 way, a close relative of parsing) are the only parts of the
1670 compiler that should be calling push_scope and pop_scope, or
1671 related functions. The parser (and template instantiation code)
1672 keeps track of what scope is presently active; everything else
1673 should simply honor that. (The code that generates static
1674 initializers may also need to set the scope, in order to check
1675 access control correctly when emitting the initializers.)
1677 Methodology
1678 -----------
1680 The parser is of the standard recursive-descent variety. Upcoming
1681 tokens in the token stream are examined in order to determine which
1682 production to use when parsing a non-terminal. Some C++ constructs
1683 require arbitrary look ahead to disambiguate. For example, it is
1684 impossible, in the general case, to tell whether a statement is an
1685 expression or declaration without scanning the entire statement.
1686 Therefore, the parser is capable of "parsing tentatively." When the
1687 parser is not sure what construct comes next, it enters this mode.
1688 Then, while we attempt to parse the construct, the parser queues up
1689 error messages, rather than issuing them immediately, and saves the
1690 tokens it consumes. If the construct is parsed successfully, the
1691 parser "commits", i.e., it issues any queued error messages and
1692 the tokens that were being preserved are permanently discarded.
1693 If, however, the construct is not parsed successfully, the parser
1694 rolls back its state completely so that it can resume parsing using
1695 a different alternative.
1697 Future Improvements
1698 -------------------
1700 The performance of the parser could probably be improved substantially.
1701 We could often eliminate the need to parse tentatively by looking ahead
1702 a little bit. In some places, this approach might not entirely eliminate
1703 the need to parse tentatively, but it might still speed up the average
1704 case. */
1706 /* Flags that are passed to some parsing functions. These values can
1707 be bitwise-ored together. */
1709 enum
1711 /* No flags. */
1712 CP_PARSER_FLAGS_NONE = 0x0,
1713 /* The construct is optional. If it is not present, then no error
1714 should be issued. */
1715 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1716 /* When parsing a type-specifier, treat user-defined type-names
1717 as non-type identifiers. */
1718 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1719 /* When parsing a type-specifier, do not try to parse a class-specifier
1720 or enum-specifier. */
1721 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1722 /* When parsing a decl-specifier-seq, only allow type-specifier or
1723 constexpr. */
1724 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1727 /* This type is used for parameters and variables which hold
1728 combinations of the above flags. */
1729 typedef int cp_parser_flags;
1731 /* The different kinds of declarators we want to parse. */
1733 typedef enum cp_parser_declarator_kind
1735 /* We want an abstract declarator. */
1736 CP_PARSER_DECLARATOR_ABSTRACT,
1737 /* We want a named declarator. */
1738 CP_PARSER_DECLARATOR_NAMED,
1739 /* We don't mind, but the name must be an unqualified-id. */
1740 CP_PARSER_DECLARATOR_EITHER
1741 } cp_parser_declarator_kind;
1743 /* The precedence values used to parse binary expressions. The minimum value
1744 of PREC must be 1, because zero is reserved to quickly discriminate
1745 binary operators from other tokens. */
1747 enum cp_parser_prec
1749 PREC_NOT_OPERATOR,
1750 PREC_LOGICAL_OR_EXPRESSION,
1751 PREC_LOGICAL_AND_EXPRESSION,
1752 PREC_INCLUSIVE_OR_EXPRESSION,
1753 PREC_EXCLUSIVE_OR_EXPRESSION,
1754 PREC_AND_EXPRESSION,
1755 PREC_EQUALITY_EXPRESSION,
1756 PREC_RELATIONAL_EXPRESSION,
1757 PREC_SHIFT_EXPRESSION,
1758 PREC_ADDITIVE_EXPRESSION,
1759 PREC_MULTIPLICATIVE_EXPRESSION,
1760 PREC_PM_EXPRESSION,
1761 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1764 /* A mapping from a token type to a corresponding tree node type, with a
1765 precedence value. */
1767 typedef struct cp_parser_binary_operations_map_node
1769 /* The token type. */
1770 enum cpp_ttype token_type;
1771 /* The corresponding tree code. */
1772 enum tree_code tree_type;
1773 /* The precedence of this operator. */
1774 enum cp_parser_prec prec;
1775 } cp_parser_binary_operations_map_node;
1777 typedef struct cp_parser_expression_stack_entry
1779 /* Left hand side of the binary operation we are currently
1780 parsing. */
1781 tree lhs;
1782 /* Original tree code for left hand side, if it was a binary
1783 expression itself (used for -Wparentheses). */
1784 enum tree_code lhs_type;
1785 /* Tree code for the binary operation we are parsing. */
1786 enum tree_code tree_type;
1787 /* Precedence of the binary operation we are parsing. */
1788 enum cp_parser_prec prec;
1789 /* Location of the binary operation we are parsing. */
1790 location_t loc;
1791 } cp_parser_expression_stack_entry;
1793 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1794 entries because precedence levels on the stack are monotonically
1795 increasing. */
1796 typedef struct cp_parser_expression_stack_entry
1797 cp_parser_expression_stack[NUM_PREC_VALUES];
1799 /* Prototypes. */
1801 /* Constructors and destructors. */
1803 static cp_parser_context *cp_parser_context_new
1804 (cp_parser_context *);
1806 /* Class variables. */
1808 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1810 /* The operator-precedence table used by cp_parser_binary_expression.
1811 Transformed into an associative array (binops_by_token) by
1812 cp_parser_new. */
1814 static const cp_parser_binary_operations_map_node binops[] = {
1815 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1816 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1818 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1819 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1820 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1822 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1823 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1825 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1826 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1828 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1829 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1830 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1831 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1833 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1834 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1836 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1838 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1840 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1842 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1844 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1847 /* The same as binops, but initialized by cp_parser_new so that
1848 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1849 for speed. */
1850 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1852 /* Constructors and destructors. */
1854 /* Construct a new context. The context below this one on the stack
1855 is given by NEXT. */
1857 static cp_parser_context *
1858 cp_parser_context_new (cp_parser_context* next)
1860 cp_parser_context *context;
1862 /* Allocate the storage. */
1863 if (cp_parser_context_free_list != NULL)
1865 /* Pull the first entry from the free list. */
1866 context = cp_parser_context_free_list;
1867 cp_parser_context_free_list = context->next;
1868 memset (context, 0, sizeof (*context));
1870 else
1871 context = ggc_cleared_alloc<cp_parser_context> ();
1873 /* No errors have occurred yet in this context. */
1874 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1875 /* If this is not the bottommost context, copy information that we
1876 need from the previous context. */
1877 if (next)
1879 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1880 expression, then we are parsing one in this context, too. */
1881 context->object_type = next->object_type;
1882 /* Thread the stack. */
1883 context->next = next;
1886 return context;
1889 /* Managing the unparsed function queues. */
1891 #define unparsed_funs_with_default_args \
1892 parser->unparsed_queues->last ().funs_with_default_args
1893 #define unparsed_funs_with_definitions \
1894 parser->unparsed_queues->last ().funs_with_definitions
1895 #define unparsed_nsdmis \
1896 parser->unparsed_queues->last ().nsdmis
1897 #define unparsed_classes \
1898 parser->unparsed_queues->last ().classes
1900 static void
1901 push_unparsed_function_queues (cp_parser *parser)
1903 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1904 vec_safe_push (parser->unparsed_queues, e);
1907 static void
1908 pop_unparsed_function_queues (cp_parser *parser)
1910 release_tree_vector (unparsed_funs_with_definitions);
1911 parser->unparsed_queues->pop ();
1914 /* Prototypes. */
1916 /* Constructors and destructors. */
1918 static cp_parser *cp_parser_new
1919 (void);
1921 /* Routines to parse various constructs.
1923 Those that return `tree' will return the error_mark_node (rather
1924 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1925 Sometimes, they will return an ordinary node if error-recovery was
1926 attempted, even though a parse error occurred. So, to check
1927 whether or not a parse error occurred, you should always use
1928 cp_parser_error_occurred. If the construct is optional (indicated
1929 either by an `_opt' in the name of the function that does the
1930 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1931 the construct is not present. */
1933 /* Lexical conventions [gram.lex] */
1935 static tree cp_parser_identifier
1936 (cp_parser *);
1937 static tree cp_parser_string_literal
1938 (cp_parser *, bool, bool, bool);
1939 static tree cp_parser_userdef_char_literal
1940 (cp_parser *);
1941 static tree cp_parser_userdef_string_literal
1942 (tree);
1943 static tree cp_parser_userdef_numeric_literal
1944 (cp_parser *);
1946 /* Basic concepts [gram.basic] */
1948 static bool cp_parser_translation_unit
1949 (cp_parser *);
1951 /* Expressions [gram.expr] */
1953 static tree cp_parser_primary_expression
1954 (cp_parser *, bool, bool, bool, cp_id_kind *);
1955 static tree cp_parser_id_expression
1956 (cp_parser *, bool, bool, bool *, bool, bool);
1957 static tree cp_parser_unqualified_id
1958 (cp_parser *, bool, bool, bool, bool);
1959 static tree cp_parser_nested_name_specifier_opt
1960 (cp_parser *, bool, bool, bool, bool);
1961 static tree cp_parser_nested_name_specifier
1962 (cp_parser *, bool, bool, bool, bool);
1963 static tree cp_parser_qualifying_entity
1964 (cp_parser *, bool, bool, bool, bool, bool);
1965 static tree cp_parser_postfix_expression
1966 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1967 static tree cp_parser_postfix_open_square_expression
1968 (cp_parser *, tree, bool, bool);
1969 static tree cp_parser_postfix_dot_deref_expression
1970 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1971 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1972 (cp_parser *, int, bool, bool, bool *, bool = false);
1973 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1974 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1975 static void cp_parser_pseudo_destructor_name
1976 (cp_parser *, tree, tree *, tree *);
1977 static tree cp_parser_unary_expression
1978 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1979 static enum tree_code cp_parser_unary_operator
1980 (cp_token *);
1981 static tree cp_parser_new_expression
1982 (cp_parser *);
1983 static vec<tree, va_gc> *cp_parser_new_placement
1984 (cp_parser *);
1985 static tree cp_parser_new_type_id
1986 (cp_parser *, tree *);
1987 static cp_declarator *cp_parser_new_declarator_opt
1988 (cp_parser *);
1989 static cp_declarator *cp_parser_direct_new_declarator
1990 (cp_parser *);
1991 static vec<tree, va_gc> *cp_parser_new_initializer
1992 (cp_parser *);
1993 static tree cp_parser_delete_expression
1994 (cp_parser *);
1995 static tree cp_parser_cast_expression
1996 (cp_parser *, bool, bool, bool, cp_id_kind *);
1997 static tree cp_parser_binary_expression
1998 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1999 static tree cp_parser_question_colon_clause
2000 (cp_parser *, tree);
2001 static tree cp_parser_assignment_expression
2002 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2003 static enum tree_code cp_parser_assignment_operator_opt
2004 (cp_parser *);
2005 static tree cp_parser_expression
2006 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2007 static tree cp_parser_constant_expression
2008 (cp_parser *, bool = false, bool * = NULL);
2009 static tree cp_parser_builtin_offsetof
2010 (cp_parser *);
2011 static tree cp_parser_lambda_expression
2012 (cp_parser *);
2013 static void cp_parser_lambda_introducer
2014 (cp_parser *, tree);
2015 static bool cp_parser_lambda_declarator_opt
2016 (cp_parser *, tree);
2017 static void cp_parser_lambda_body
2018 (cp_parser *, tree);
2020 /* Statements [gram.stmt.stmt] */
2022 static void cp_parser_statement
2023 (cp_parser *, tree, bool, bool *);
2024 static void cp_parser_label_for_labeled_statement
2025 (cp_parser *, tree);
2026 static tree cp_parser_expression_statement
2027 (cp_parser *, tree);
2028 static tree cp_parser_compound_statement
2029 (cp_parser *, tree, bool, bool);
2030 static void cp_parser_statement_seq_opt
2031 (cp_parser *, tree);
2032 static tree cp_parser_selection_statement
2033 (cp_parser *, bool *);
2034 static tree cp_parser_condition
2035 (cp_parser *);
2036 static tree cp_parser_iteration_statement
2037 (cp_parser *, bool);
2038 static bool cp_parser_for_init_statement
2039 (cp_parser *, tree *decl);
2040 static tree cp_parser_for
2041 (cp_parser *, bool);
2042 static tree cp_parser_c_for
2043 (cp_parser *, tree, tree, bool);
2044 static tree cp_parser_range_for
2045 (cp_parser *, tree, tree, tree, bool);
2046 static void do_range_for_auto_deduction
2047 (tree, tree);
2048 static tree cp_parser_perform_range_for_lookup
2049 (tree, tree *, tree *);
2050 static tree cp_parser_range_for_member_function
2051 (tree, tree);
2052 static tree cp_parser_jump_statement
2053 (cp_parser *);
2054 static void cp_parser_declaration_statement
2055 (cp_parser *);
2057 static tree cp_parser_implicitly_scoped_statement
2058 (cp_parser *, bool *, location_t, const char *);
2059 static void cp_parser_already_scoped_statement
2060 (cp_parser *, location_t, const char *);
2062 /* Declarations [gram.dcl.dcl] */
2064 static void cp_parser_declaration_seq_opt
2065 (cp_parser *);
2066 static void cp_parser_declaration
2067 (cp_parser *);
2068 static void cp_parser_block_declaration
2069 (cp_parser *, bool);
2070 static void cp_parser_simple_declaration
2071 (cp_parser *, bool, tree *);
2072 static void cp_parser_decl_specifier_seq
2073 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2074 static tree cp_parser_storage_class_specifier_opt
2075 (cp_parser *);
2076 static tree cp_parser_function_specifier_opt
2077 (cp_parser *, cp_decl_specifier_seq *);
2078 static tree cp_parser_type_specifier
2079 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2080 int *, bool *);
2081 static tree cp_parser_simple_type_specifier
2082 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2083 static tree cp_parser_type_name
2084 (cp_parser *);
2085 static tree cp_parser_nonclass_name
2086 (cp_parser* parser);
2087 static tree cp_parser_elaborated_type_specifier
2088 (cp_parser *, bool, bool);
2089 static tree cp_parser_enum_specifier
2090 (cp_parser *);
2091 static void cp_parser_enumerator_list
2092 (cp_parser *, tree);
2093 static void cp_parser_enumerator_definition
2094 (cp_parser *, tree);
2095 static tree cp_parser_namespace_name
2096 (cp_parser *);
2097 static void cp_parser_namespace_definition
2098 (cp_parser *);
2099 static void cp_parser_namespace_body
2100 (cp_parser *);
2101 static tree cp_parser_qualified_namespace_specifier
2102 (cp_parser *);
2103 static void cp_parser_namespace_alias_definition
2104 (cp_parser *);
2105 static bool cp_parser_using_declaration
2106 (cp_parser *, bool);
2107 static void cp_parser_using_directive
2108 (cp_parser *);
2109 static tree cp_parser_alias_declaration
2110 (cp_parser *);
2111 static void cp_parser_asm_definition
2112 (cp_parser *);
2113 static void cp_parser_linkage_specification
2114 (cp_parser *);
2115 static void cp_parser_static_assert
2116 (cp_parser *, bool);
2117 static tree cp_parser_decltype
2118 (cp_parser *);
2120 /* Declarators [gram.dcl.decl] */
2122 static tree cp_parser_init_declarator
2123 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2124 bool, bool, int, bool *, tree *, location_t *);
2125 static cp_declarator *cp_parser_declarator
2126 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2127 static cp_declarator *cp_parser_direct_declarator
2128 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2129 static enum tree_code cp_parser_ptr_operator
2130 (cp_parser *, tree *, cp_cv_quals *, tree *);
2131 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2132 (cp_parser *);
2133 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2134 (cp_parser *);
2135 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2136 (cp_parser *);
2137 static tree cp_parser_late_return_type_opt
2138 (cp_parser *, cp_declarator *, cp_cv_quals);
2139 static tree cp_parser_declarator_id
2140 (cp_parser *, bool);
2141 static tree cp_parser_type_id
2142 (cp_parser *);
2143 static tree cp_parser_template_type_arg
2144 (cp_parser *);
2145 static tree cp_parser_trailing_type_id (cp_parser *);
2146 static tree cp_parser_type_id_1
2147 (cp_parser *, bool, bool);
2148 static void cp_parser_type_specifier_seq
2149 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2150 static tree cp_parser_parameter_declaration_clause
2151 (cp_parser *);
2152 static tree cp_parser_parameter_declaration_list
2153 (cp_parser *, bool *);
2154 static cp_parameter_declarator *cp_parser_parameter_declaration
2155 (cp_parser *, bool, bool *);
2156 static tree cp_parser_default_argument
2157 (cp_parser *, bool);
2158 static void cp_parser_function_body
2159 (cp_parser *, bool);
2160 static tree cp_parser_initializer
2161 (cp_parser *, bool *, bool *);
2162 static tree cp_parser_initializer_clause
2163 (cp_parser *, bool *);
2164 static tree cp_parser_braced_list
2165 (cp_parser*, bool*);
2166 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2167 (cp_parser *, bool *);
2169 static bool cp_parser_ctor_initializer_opt_and_function_body
2170 (cp_parser *, bool);
2172 static tree cp_parser_late_parsing_omp_declare_simd
2173 (cp_parser *, tree);
2175 static tree cp_parser_late_parsing_cilk_simd_fn_info
2176 (cp_parser *, tree);
2178 static tree synthesize_implicit_template_parm
2179 (cp_parser *);
2180 static tree finish_fully_implicit_template
2181 (cp_parser *, tree);
2183 /* Classes [gram.class] */
2185 static tree cp_parser_class_name
2186 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2187 static tree cp_parser_class_specifier
2188 (cp_parser *);
2189 static tree cp_parser_class_head
2190 (cp_parser *, bool *);
2191 static enum tag_types cp_parser_class_key
2192 (cp_parser *);
2193 static void cp_parser_type_parameter_key
2194 (cp_parser* parser);
2195 static void cp_parser_member_specification_opt
2196 (cp_parser *);
2197 static void cp_parser_member_declaration
2198 (cp_parser *);
2199 static tree cp_parser_pure_specifier
2200 (cp_parser *);
2201 static tree cp_parser_constant_initializer
2202 (cp_parser *);
2204 /* Derived classes [gram.class.derived] */
2206 static tree cp_parser_base_clause
2207 (cp_parser *);
2208 static tree cp_parser_base_specifier
2209 (cp_parser *);
2211 /* Special member functions [gram.special] */
2213 static tree cp_parser_conversion_function_id
2214 (cp_parser *);
2215 static tree cp_parser_conversion_type_id
2216 (cp_parser *);
2217 static cp_declarator *cp_parser_conversion_declarator_opt
2218 (cp_parser *);
2219 static bool cp_parser_ctor_initializer_opt
2220 (cp_parser *);
2221 static void cp_parser_mem_initializer_list
2222 (cp_parser *);
2223 static tree cp_parser_mem_initializer
2224 (cp_parser *);
2225 static tree cp_parser_mem_initializer_id
2226 (cp_parser *);
2228 /* Overloading [gram.over] */
2230 static tree cp_parser_operator_function_id
2231 (cp_parser *);
2232 static tree cp_parser_operator
2233 (cp_parser *);
2235 /* Templates [gram.temp] */
2237 static void cp_parser_template_declaration
2238 (cp_parser *, bool);
2239 static tree cp_parser_template_parameter_list
2240 (cp_parser *);
2241 static tree cp_parser_template_parameter
2242 (cp_parser *, bool *, bool *);
2243 static tree cp_parser_type_parameter
2244 (cp_parser *, bool *);
2245 static tree cp_parser_template_id
2246 (cp_parser *, bool, bool, enum tag_types, bool);
2247 static tree cp_parser_template_name
2248 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2249 static tree cp_parser_template_argument_list
2250 (cp_parser *);
2251 static tree cp_parser_template_argument
2252 (cp_parser *);
2253 static void cp_parser_explicit_instantiation
2254 (cp_parser *);
2255 static void cp_parser_explicit_specialization
2256 (cp_parser *);
2258 /* Exception handling [gram.exception] */
2260 static tree cp_parser_try_block
2261 (cp_parser *);
2262 static bool cp_parser_function_try_block
2263 (cp_parser *);
2264 static void cp_parser_handler_seq
2265 (cp_parser *);
2266 static void cp_parser_handler
2267 (cp_parser *);
2268 static tree cp_parser_exception_declaration
2269 (cp_parser *);
2270 static tree cp_parser_throw_expression
2271 (cp_parser *);
2272 static tree cp_parser_exception_specification_opt
2273 (cp_parser *);
2274 static tree cp_parser_type_id_list
2275 (cp_parser *);
2277 /* GNU Extensions */
2279 static tree cp_parser_asm_specification_opt
2280 (cp_parser *);
2281 static tree cp_parser_asm_operand_list
2282 (cp_parser *);
2283 static tree cp_parser_asm_clobber_list
2284 (cp_parser *);
2285 static tree cp_parser_asm_label_list
2286 (cp_parser *);
2287 static bool cp_next_tokens_can_be_attribute_p
2288 (cp_parser *);
2289 static bool cp_next_tokens_can_be_gnu_attribute_p
2290 (cp_parser *);
2291 static bool cp_next_tokens_can_be_std_attribute_p
2292 (cp_parser *);
2293 static bool cp_nth_tokens_can_be_std_attribute_p
2294 (cp_parser *, size_t);
2295 static bool cp_nth_tokens_can_be_gnu_attribute_p
2296 (cp_parser *, size_t);
2297 static bool cp_nth_tokens_can_be_attribute_p
2298 (cp_parser *, size_t);
2299 static tree cp_parser_attributes_opt
2300 (cp_parser *);
2301 static tree cp_parser_gnu_attributes_opt
2302 (cp_parser *);
2303 static tree cp_parser_gnu_attribute_list
2304 (cp_parser *);
2305 static tree cp_parser_std_attribute
2306 (cp_parser *);
2307 static tree cp_parser_std_attribute_spec
2308 (cp_parser *);
2309 static tree cp_parser_std_attribute_spec_seq
2310 (cp_parser *);
2311 static bool cp_parser_extension_opt
2312 (cp_parser *, int *);
2313 static void cp_parser_label_declaration
2314 (cp_parser *);
2316 /* Transactional Memory Extensions */
2318 static tree cp_parser_transaction
2319 (cp_parser *, enum rid);
2320 static tree cp_parser_transaction_expression
2321 (cp_parser *, enum rid);
2322 static bool cp_parser_function_transaction
2323 (cp_parser *, enum rid);
2324 static tree cp_parser_transaction_cancel
2325 (cp_parser *);
2327 enum pragma_context {
2328 pragma_external,
2329 pragma_member,
2330 pragma_objc_icode,
2331 pragma_stmt,
2332 pragma_compound
2334 static bool cp_parser_pragma
2335 (cp_parser *, enum pragma_context);
2337 /* Objective-C++ Productions */
2339 static tree cp_parser_objc_message_receiver
2340 (cp_parser *);
2341 static tree cp_parser_objc_message_args
2342 (cp_parser *);
2343 static tree cp_parser_objc_message_expression
2344 (cp_parser *);
2345 static tree cp_parser_objc_encode_expression
2346 (cp_parser *);
2347 static tree cp_parser_objc_defs_expression
2348 (cp_parser *);
2349 static tree cp_parser_objc_protocol_expression
2350 (cp_parser *);
2351 static tree cp_parser_objc_selector_expression
2352 (cp_parser *);
2353 static tree cp_parser_objc_expression
2354 (cp_parser *);
2355 static bool cp_parser_objc_selector_p
2356 (enum cpp_ttype);
2357 static tree cp_parser_objc_selector
2358 (cp_parser *);
2359 static tree cp_parser_objc_protocol_refs_opt
2360 (cp_parser *);
2361 static void cp_parser_objc_declaration
2362 (cp_parser *, tree);
2363 static tree cp_parser_objc_statement
2364 (cp_parser *);
2365 static bool cp_parser_objc_valid_prefix_attributes
2366 (cp_parser *, tree *);
2367 static void cp_parser_objc_at_property_declaration
2368 (cp_parser *) ;
2369 static void cp_parser_objc_at_synthesize_declaration
2370 (cp_parser *) ;
2371 static void cp_parser_objc_at_dynamic_declaration
2372 (cp_parser *) ;
2373 static tree cp_parser_objc_struct_declaration
2374 (cp_parser *) ;
2376 /* Utility Routines */
2378 static tree cp_parser_lookup_name
2379 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2380 static tree cp_parser_lookup_name_simple
2381 (cp_parser *, tree, location_t);
2382 static tree cp_parser_maybe_treat_template_as_class
2383 (tree, bool);
2384 static bool cp_parser_check_declarator_template_parameters
2385 (cp_parser *, cp_declarator *, location_t);
2386 static bool cp_parser_check_template_parameters
2387 (cp_parser *, unsigned, location_t, cp_declarator *);
2388 static tree cp_parser_simple_cast_expression
2389 (cp_parser *);
2390 static tree cp_parser_global_scope_opt
2391 (cp_parser *, bool);
2392 static bool cp_parser_constructor_declarator_p
2393 (cp_parser *, bool);
2394 static tree cp_parser_function_definition_from_specifiers_and_declarator
2395 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2396 static tree cp_parser_function_definition_after_declarator
2397 (cp_parser *, bool);
2398 static void cp_parser_template_declaration_after_export
2399 (cp_parser *, bool);
2400 static void cp_parser_perform_template_parameter_access_checks
2401 (vec<deferred_access_check, va_gc> *);
2402 static tree cp_parser_single_declaration
2403 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2404 static tree cp_parser_functional_cast
2405 (cp_parser *, tree);
2406 static tree cp_parser_save_member_function_body
2407 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2408 static tree cp_parser_save_nsdmi
2409 (cp_parser *);
2410 static tree cp_parser_enclosed_template_argument_list
2411 (cp_parser *);
2412 static void cp_parser_save_default_args
2413 (cp_parser *, tree);
2414 static void cp_parser_late_parsing_for_member
2415 (cp_parser *, tree);
2416 static tree cp_parser_late_parse_one_default_arg
2417 (cp_parser *, tree, tree, tree);
2418 static void cp_parser_late_parsing_nsdmi
2419 (cp_parser *, tree);
2420 static void cp_parser_late_parsing_default_args
2421 (cp_parser *, tree);
2422 static tree cp_parser_sizeof_operand
2423 (cp_parser *, enum rid);
2424 static tree cp_parser_trait_expr
2425 (cp_parser *, enum rid);
2426 static bool cp_parser_declares_only_class_p
2427 (cp_parser *);
2428 static void cp_parser_set_storage_class
2429 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2430 static void cp_parser_set_decl_spec_type
2431 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2432 static void set_and_check_decl_spec_loc
2433 (cp_decl_specifier_seq *decl_specs,
2434 cp_decl_spec ds, cp_token *);
2435 static bool cp_parser_friend_p
2436 (const cp_decl_specifier_seq *);
2437 static void cp_parser_required_error
2438 (cp_parser *, required_token, bool);
2439 static cp_token *cp_parser_require
2440 (cp_parser *, enum cpp_ttype, required_token);
2441 static cp_token *cp_parser_require_keyword
2442 (cp_parser *, enum rid, required_token);
2443 static bool cp_parser_token_starts_function_definition_p
2444 (cp_token *);
2445 static bool cp_parser_next_token_starts_class_definition_p
2446 (cp_parser *);
2447 static bool cp_parser_next_token_ends_template_argument_p
2448 (cp_parser *);
2449 static bool cp_parser_nth_token_starts_template_argument_list_p
2450 (cp_parser *, size_t);
2451 static enum tag_types cp_parser_token_is_class_key
2452 (cp_token *);
2453 static enum tag_types cp_parser_token_is_type_parameter_key
2454 (cp_token *);
2455 static void cp_parser_check_class_key
2456 (enum tag_types, tree type);
2457 static void cp_parser_check_access_in_redeclaration
2458 (tree type, location_t location);
2459 static bool cp_parser_optional_template_keyword
2460 (cp_parser *);
2461 static void cp_parser_pre_parsed_nested_name_specifier
2462 (cp_parser *);
2463 static bool cp_parser_cache_group
2464 (cp_parser *, enum cpp_ttype, unsigned);
2465 static tree cp_parser_cache_defarg
2466 (cp_parser *parser, bool nsdmi);
2467 static void cp_parser_parse_tentatively
2468 (cp_parser *);
2469 static void cp_parser_commit_to_tentative_parse
2470 (cp_parser *);
2471 static void cp_parser_commit_to_topmost_tentative_parse
2472 (cp_parser *);
2473 static void cp_parser_abort_tentative_parse
2474 (cp_parser *);
2475 static bool cp_parser_parse_definitely
2476 (cp_parser *);
2477 static inline bool cp_parser_parsing_tentatively
2478 (cp_parser *);
2479 static bool cp_parser_uncommitted_to_tentative_parse_p
2480 (cp_parser *);
2481 static void cp_parser_error
2482 (cp_parser *, const char *);
2483 static void cp_parser_name_lookup_error
2484 (cp_parser *, tree, tree, name_lookup_error, location_t);
2485 static bool cp_parser_simulate_error
2486 (cp_parser *);
2487 static bool cp_parser_check_type_definition
2488 (cp_parser *);
2489 static void cp_parser_check_for_definition_in_return_type
2490 (cp_declarator *, tree, location_t type_location);
2491 static void cp_parser_check_for_invalid_template_id
2492 (cp_parser *, tree, enum tag_types, location_t location);
2493 static bool cp_parser_non_integral_constant_expression
2494 (cp_parser *, non_integral_constant);
2495 static void cp_parser_diagnose_invalid_type_name
2496 (cp_parser *, tree, location_t);
2497 static bool cp_parser_parse_and_diagnose_invalid_type_name
2498 (cp_parser *);
2499 static int cp_parser_skip_to_closing_parenthesis
2500 (cp_parser *, bool, bool, bool);
2501 static void cp_parser_skip_to_end_of_statement
2502 (cp_parser *);
2503 static void cp_parser_consume_semicolon_at_end_of_statement
2504 (cp_parser *);
2505 static void cp_parser_skip_to_end_of_block_or_statement
2506 (cp_parser *);
2507 static bool cp_parser_skip_to_closing_brace
2508 (cp_parser *);
2509 static void cp_parser_skip_to_end_of_template_parameter_list
2510 (cp_parser *);
2511 static void cp_parser_skip_to_pragma_eol
2512 (cp_parser*, cp_token *);
2513 static bool cp_parser_error_occurred
2514 (cp_parser *);
2515 static bool cp_parser_allow_gnu_extensions_p
2516 (cp_parser *);
2517 static bool cp_parser_is_pure_string_literal
2518 (cp_token *);
2519 static bool cp_parser_is_string_literal
2520 (cp_token *);
2521 static bool cp_parser_is_keyword
2522 (cp_token *, enum rid);
2523 static tree cp_parser_make_typename_type
2524 (cp_parser *, tree, location_t location);
2525 static cp_declarator * cp_parser_make_indirect_declarator
2526 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2527 static bool cp_parser_compound_literal_p
2528 (cp_parser *);
2529 static bool cp_parser_array_designator_p
2530 (cp_parser *);
2531 static bool cp_parser_skip_to_closing_square_bracket
2532 (cp_parser *);
2534 /* Returns nonzero if we are parsing tentatively. */
2536 static inline bool
2537 cp_parser_parsing_tentatively (cp_parser* parser)
2539 return parser->context->next != NULL;
2542 /* Returns nonzero if TOKEN is a string literal. */
2544 static bool
2545 cp_parser_is_pure_string_literal (cp_token* token)
2547 return (token->type == CPP_STRING ||
2548 token->type == CPP_STRING16 ||
2549 token->type == CPP_STRING32 ||
2550 token->type == CPP_WSTRING ||
2551 token->type == CPP_UTF8STRING);
2554 /* Returns nonzero if TOKEN is a string literal
2555 of a user-defined string literal. */
2557 static bool
2558 cp_parser_is_string_literal (cp_token* token)
2560 return (cp_parser_is_pure_string_literal (token) ||
2561 token->type == CPP_STRING_USERDEF ||
2562 token->type == CPP_STRING16_USERDEF ||
2563 token->type == CPP_STRING32_USERDEF ||
2564 token->type == CPP_WSTRING_USERDEF ||
2565 token->type == CPP_UTF8STRING_USERDEF);
2568 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2570 static bool
2571 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2573 return token->keyword == keyword;
2576 /* If not parsing tentatively, issue a diagnostic of the form
2577 FILE:LINE: MESSAGE before TOKEN
2578 where TOKEN is the next token in the input stream. MESSAGE
2579 (specified by the caller) is usually of the form "expected
2580 OTHER-TOKEN". */
2582 static void
2583 cp_parser_error (cp_parser* parser, const char* gmsgid)
2585 if (!cp_parser_simulate_error (parser))
2587 cp_token *token = cp_lexer_peek_token (parser->lexer);
2588 /* This diagnostic makes more sense if it is tagged to the line
2589 of the token we just peeked at. */
2590 cp_lexer_set_source_position_from_token (token);
2592 if (token->type == CPP_PRAGMA)
2594 error_at (token->location,
2595 "%<#pragma%> is not allowed here");
2596 cp_parser_skip_to_pragma_eol (parser, token);
2597 return;
2600 c_parse_error (gmsgid,
2601 /* Because c_parser_error does not understand
2602 CPP_KEYWORD, keywords are treated like
2603 identifiers. */
2604 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2605 token->u.value, token->flags);
2609 /* Issue an error about name-lookup failing. NAME is the
2610 IDENTIFIER_NODE DECL is the result of
2611 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2612 the thing that we hoped to find. */
2614 static void
2615 cp_parser_name_lookup_error (cp_parser* parser,
2616 tree name,
2617 tree decl,
2618 name_lookup_error desired,
2619 location_t location)
2621 /* If name lookup completely failed, tell the user that NAME was not
2622 declared. */
2623 if (decl == error_mark_node)
2625 if (parser->scope && parser->scope != global_namespace)
2626 error_at (location, "%<%E::%E%> has not been declared",
2627 parser->scope, name);
2628 else if (parser->scope == global_namespace)
2629 error_at (location, "%<::%E%> has not been declared", name);
2630 else if (parser->object_scope
2631 && !CLASS_TYPE_P (parser->object_scope))
2632 error_at (location, "request for member %qE in non-class type %qT",
2633 name, parser->object_scope);
2634 else if (parser->object_scope)
2635 error_at (location, "%<%T::%E%> has not been declared",
2636 parser->object_scope, name);
2637 else
2638 error_at (location, "%qE has not been declared", name);
2640 else if (parser->scope && parser->scope != global_namespace)
2642 switch (desired)
2644 case NLE_TYPE:
2645 error_at (location, "%<%E::%E%> is not a type",
2646 parser->scope, name);
2647 break;
2648 case NLE_CXX98:
2649 error_at (location, "%<%E::%E%> is not a class or namespace",
2650 parser->scope, name);
2651 break;
2652 case NLE_NOT_CXX98:
2653 error_at (location,
2654 "%<%E::%E%> is not a class, namespace, or enumeration",
2655 parser->scope, name);
2656 break;
2657 default:
2658 gcc_unreachable ();
2662 else if (parser->scope == global_namespace)
2664 switch (desired)
2666 case NLE_TYPE:
2667 error_at (location, "%<::%E%> is not a type", name);
2668 break;
2669 case NLE_CXX98:
2670 error_at (location, "%<::%E%> is not a class or namespace", name);
2671 break;
2672 case NLE_NOT_CXX98:
2673 error_at (location,
2674 "%<::%E%> is not a class, namespace, or enumeration",
2675 name);
2676 break;
2677 default:
2678 gcc_unreachable ();
2681 else
2683 switch (desired)
2685 case NLE_TYPE:
2686 error_at (location, "%qE is not a type", name);
2687 break;
2688 case NLE_CXX98:
2689 error_at (location, "%qE is not a class or namespace", name);
2690 break;
2691 case NLE_NOT_CXX98:
2692 error_at (location,
2693 "%qE is not a class, namespace, or enumeration", name);
2694 break;
2695 default:
2696 gcc_unreachable ();
2701 /* If we are parsing tentatively, remember that an error has occurred
2702 during this tentative parse. Returns true if the error was
2703 simulated; false if a message should be issued by the caller. */
2705 static bool
2706 cp_parser_simulate_error (cp_parser* parser)
2708 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2710 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2711 return true;
2713 return false;
2716 /* This function is called when a type is defined. If type
2717 definitions are forbidden at this point, an error message is
2718 issued. */
2720 static bool
2721 cp_parser_check_type_definition (cp_parser* parser)
2723 /* If types are forbidden here, issue a message. */
2724 if (parser->type_definition_forbidden_message)
2726 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2727 in the message need to be interpreted. */
2728 error (parser->type_definition_forbidden_message);
2729 return false;
2731 return true;
2734 /* This function is called when the DECLARATOR is processed. The TYPE
2735 was a type defined in the decl-specifiers. If it is invalid to
2736 define a type in the decl-specifiers for DECLARATOR, an error is
2737 issued. TYPE_LOCATION is the location of TYPE and is used
2738 for error reporting. */
2740 static void
2741 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2742 tree type, location_t type_location)
2744 /* [dcl.fct] forbids type definitions in return types.
2745 Unfortunately, it's not easy to know whether or not we are
2746 processing a return type until after the fact. */
2747 while (declarator
2748 && (declarator->kind == cdk_pointer
2749 || declarator->kind == cdk_reference
2750 || declarator->kind == cdk_ptrmem))
2751 declarator = declarator->declarator;
2752 if (declarator
2753 && declarator->kind == cdk_function)
2755 error_at (type_location,
2756 "new types may not be defined in a return type");
2757 inform (type_location,
2758 "(perhaps a semicolon is missing after the definition of %qT)",
2759 type);
2763 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2764 "<" in any valid C++ program. If the next token is indeed "<",
2765 issue a message warning the user about what appears to be an
2766 invalid attempt to form a template-id. LOCATION is the location
2767 of the type-specifier (TYPE) */
2769 static void
2770 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2771 tree type,
2772 enum tag_types tag_type,
2773 location_t location)
2775 cp_token_position start = 0;
2777 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2779 if (TYPE_P (type))
2780 error_at (location, "%qT is not a template", type);
2781 else if (identifier_p (type))
2783 if (tag_type != none_type)
2784 error_at (location, "%qE is not a class template", type);
2785 else
2786 error_at (location, "%qE is not a template", type);
2788 else
2789 error_at (location, "invalid template-id");
2790 /* Remember the location of the invalid "<". */
2791 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2792 start = cp_lexer_token_position (parser->lexer, true);
2793 /* Consume the "<". */
2794 cp_lexer_consume_token (parser->lexer);
2795 /* Parse the template arguments. */
2796 cp_parser_enclosed_template_argument_list (parser);
2797 /* Permanently remove the invalid template arguments so that
2798 this error message is not issued again. */
2799 if (start)
2800 cp_lexer_purge_tokens_after (parser->lexer, start);
2804 /* If parsing an integral constant-expression, issue an error message
2805 about the fact that THING appeared and return true. Otherwise,
2806 return false. In either case, set
2807 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2809 static bool
2810 cp_parser_non_integral_constant_expression (cp_parser *parser,
2811 non_integral_constant thing)
2813 parser->non_integral_constant_expression_p = true;
2814 if (parser->integral_constant_expression_p)
2816 if (!parser->allow_non_integral_constant_expression_p)
2818 const char *msg = NULL;
2819 switch (thing)
2821 case NIC_FLOAT:
2822 error ("floating-point literal "
2823 "cannot appear in a constant-expression");
2824 return true;
2825 case NIC_CAST:
2826 error ("a cast to a type other than an integral or "
2827 "enumeration type cannot appear in a "
2828 "constant-expression");
2829 return true;
2830 case NIC_TYPEID:
2831 error ("%<typeid%> operator "
2832 "cannot appear in a constant-expression");
2833 return true;
2834 case NIC_NCC:
2835 error ("non-constant compound literals "
2836 "cannot appear in a constant-expression");
2837 return true;
2838 case NIC_FUNC_CALL:
2839 error ("a function call "
2840 "cannot appear in a constant-expression");
2841 return true;
2842 case NIC_INC:
2843 error ("an increment "
2844 "cannot appear in a constant-expression");
2845 return true;
2846 case NIC_DEC:
2847 error ("an decrement "
2848 "cannot appear in a constant-expression");
2849 return true;
2850 case NIC_ARRAY_REF:
2851 error ("an array reference "
2852 "cannot appear in a constant-expression");
2853 return true;
2854 case NIC_ADDR_LABEL:
2855 error ("the address of a label "
2856 "cannot appear in a constant-expression");
2857 return true;
2858 case NIC_OVERLOADED:
2859 error ("calls to overloaded operators "
2860 "cannot appear in a constant-expression");
2861 return true;
2862 case NIC_ASSIGNMENT:
2863 error ("an assignment cannot appear in a constant-expression");
2864 return true;
2865 case NIC_COMMA:
2866 error ("a comma operator "
2867 "cannot appear in a constant-expression");
2868 return true;
2869 case NIC_CONSTRUCTOR:
2870 error ("a call to a constructor "
2871 "cannot appear in a constant-expression");
2872 return true;
2873 case NIC_TRANSACTION:
2874 error ("a transaction expression "
2875 "cannot appear in a constant-expression");
2876 return true;
2877 case NIC_THIS:
2878 msg = "this";
2879 break;
2880 case NIC_FUNC_NAME:
2881 msg = "__FUNCTION__";
2882 break;
2883 case NIC_PRETTY_FUNC:
2884 msg = "__PRETTY_FUNCTION__";
2885 break;
2886 case NIC_C99_FUNC:
2887 msg = "__func__";
2888 break;
2889 case NIC_VA_ARG:
2890 msg = "va_arg";
2891 break;
2892 case NIC_ARROW:
2893 msg = "->";
2894 break;
2895 case NIC_POINT:
2896 msg = ".";
2897 break;
2898 case NIC_STAR:
2899 msg = "*";
2900 break;
2901 case NIC_ADDR:
2902 msg = "&";
2903 break;
2904 case NIC_PREINCREMENT:
2905 msg = "++";
2906 break;
2907 case NIC_PREDECREMENT:
2908 msg = "--";
2909 break;
2910 case NIC_NEW:
2911 msg = "new";
2912 break;
2913 case NIC_DEL:
2914 msg = "delete";
2915 break;
2916 default:
2917 gcc_unreachable ();
2919 if (msg)
2920 error ("%qs cannot appear in a constant-expression", msg);
2921 return true;
2924 return false;
2927 /* Emit a diagnostic for an invalid type name. This function commits
2928 to the current active tentative parse, if any. (Otherwise, the
2929 problematic construct might be encountered again later, resulting
2930 in duplicate error messages.) LOCATION is the location of ID. */
2932 static void
2933 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2934 location_t location)
2936 tree decl, ambiguous_decls;
2937 cp_parser_commit_to_tentative_parse (parser);
2938 /* Try to lookup the identifier. */
2939 decl = cp_parser_lookup_name (parser, id, none_type,
2940 /*is_template=*/false,
2941 /*is_namespace=*/false,
2942 /*check_dependency=*/true,
2943 &ambiguous_decls, location);
2944 if (ambiguous_decls)
2945 /* If the lookup was ambiguous, an error will already have
2946 been issued. */
2947 return;
2948 /* If the lookup found a template-name, it means that the user forgot
2949 to specify an argument list. Emit a useful error message. */
2950 if (DECL_TYPE_TEMPLATE_P (decl))
2952 error_at (location,
2953 "invalid use of template-name %qE without an argument list",
2954 decl);
2955 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
2957 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2958 error_at (location, "invalid use of destructor %qD as a type", id);
2959 else if (TREE_CODE (decl) == TYPE_DECL)
2960 /* Something like 'unsigned A a;' */
2961 error_at (location, "invalid combination of multiple type-specifiers");
2962 else if (!parser->scope)
2964 /* Issue an error message. */
2965 error_at (location, "%qE does not name a type", id);
2966 /* If we're in a template class, it's possible that the user was
2967 referring to a type from a base class. For example:
2969 template <typename T> struct A { typedef T X; };
2970 template <typename T> struct B : public A<T> { X x; };
2972 The user should have said "typename A<T>::X". */
2973 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2974 inform (location, "C++11 %<constexpr%> only available with "
2975 "-std=c++11 or -std=gnu++11");
2976 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2977 inform (location, "C++11 %<noexcept%> only available with "
2978 "-std=c++11 or -std=gnu++11");
2979 else if (cxx_dialect < cxx11
2980 && TREE_CODE (id) == IDENTIFIER_NODE
2981 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2982 inform (location, "C++11 %<thread_local%> only available with "
2983 "-std=c++11 or -std=gnu++11");
2984 else if (processing_template_decl && current_class_type
2985 && TYPE_BINFO (current_class_type))
2987 tree b;
2989 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2991 b = TREE_CHAIN (b))
2993 tree base_type = BINFO_TYPE (b);
2994 if (CLASS_TYPE_P (base_type)
2995 && dependent_type_p (base_type))
2997 tree field;
2998 /* Go from a particular instantiation of the
2999 template (which will have an empty TYPE_FIELDs),
3000 to the main version. */
3001 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3002 for (field = TYPE_FIELDS (base_type);
3003 field;
3004 field = DECL_CHAIN (field))
3005 if (TREE_CODE (field) == TYPE_DECL
3006 && DECL_NAME (field) == id)
3008 inform (location,
3009 "(perhaps %<typename %T::%E%> was intended)",
3010 BINFO_TYPE (b), id);
3011 break;
3013 if (field)
3014 break;
3019 /* Here we diagnose qualified-ids where the scope is actually correct,
3020 but the identifier does not resolve to a valid type name. */
3021 else if (parser->scope != error_mark_node)
3023 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3025 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3026 error_at (location_of (id),
3027 "%qE in namespace %qE does not name a template type",
3028 id, parser->scope);
3029 else
3030 error_at (location_of (id),
3031 "%qE in namespace %qE does not name a type",
3032 id, parser->scope);
3033 if (DECL_P (decl))
3034 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3036 else if (CLASS_TYPE_P (parser->scope)
3037 && constructor_name_p (id, parser->scope))
3039 /* A<T>::A<T>() */
3040 error_at (location, "%<%T::%E%> names the constructor, not"
3041 " the type", parser->scope, id);
3042 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3043 error_at (location, "and %qT has no template constructors",
3044 parser->scope);
3046 else if (TYPE_P (parser->scope)
3047 && dependent_scope_p (parser->scope))
3048 error_at (location, "need %<typename%> before %<%T::%E%> because "
3049 "%qT is a dependent scope",
3050 parser->scope, id, parser->scope);
3051 else if (TYPE_P (parser->scope))
3053 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3054 error_at (location_of (id),
3055 "%qE in %q#T does not name a template type",
3056 id, parser->scope);
3057 else
3058 error_at (location_of (id),
3059 "%qE in %q#T does not name a type",
3060 id, parser->scope);
3061 if (DECL_P (decl))
3062 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3064 else
3065 gcc_unreachable ();
3069 /* Check for a common situation where a type-name should be present,
3070 but is not, and issue a sensible error message. Returns true if an
3071 invalid type-name was detected.
3073 The situation handled by this function are variable declarations of the
3074 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3075 Usually, `ID' should name a type, but if we got here it means that it
3076 does not. We try to emit the best possible error message depending on
3077 how exactly the id-expression looks like. */
3079 static bool
3080 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3082 tree id;
3083 cp_token *token = cp_lexer_peek_token (parser->lexer);
3085 /* Avoid duplicate error about ambiguous lookup. */
3086 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3088 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3089 if (next->type == CPP_NAME && next->error_reported)
3090 goto out;
3093 cp_parser_parse_tentatively (parser);
3094 id = cp_parser_id_expression (parser,
3095 /*template_keyword_p=*/false,
3096 /*check_dependency_p=*/true,
3097 /*template_p=*/NULL,
3098 /*declarator_p=*/true,
3099 /*optional_p=*/false);
3100 /* If the next token is a (, this is a function with no explicit return
3101 type, i.e. constructor, destructor or conversion op. */
3102 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3103 || TREE_CODE (id) == TYPE_DECL)
3105 cp_parser_abort_tentative_parse (parser);
3106 return false;
3108 if (!cp_parser_parse_definitely (parser))
3109 return false;
3111 /* Emit a diagnostic for the invalid type. */
3112 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3113 out:
3114 /* If we aren't in the middle of a declarator (i.e. in a
3115 parameter-declaration-clause), skip to the end of the declaration;
3116 there's no point in trying to process it. */
3117 if (!parser->in_declarator_p)
3118 cp_parser_skip_to_end_of_block_or_statement (parser);
3119 return true;
3122 /* Consume tokens up to, and including, the next non-nested closing `)'.
3123 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3124 are doing error recovery. Returns -1 if OR_COMMA is true and we
3125 found an unnested comma. */
3127 static int
3128 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3129 bool recovering,
3130 bool or_comma,
3131 bool consume_paren)
3133 unsigned paren_depth = 0;
3134 unsigned brace_depth = 0;
3135 unsigned square_depth = 0;
3137 if (recovering && !or_comma
3138 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3139 return 0;
3141 while (true)
3143 cp_token * token = cp_lexer_peek_token (parser->lexer);
3145 switch (token->type)
3147 case CPP_EOF:
3148 case CPP_PRAGMA_EOL:
3149 /* If we've run out of tokens, then there is no closing `)'. */
3150 return 0;
3152 /* This is good for lambda expression capture-lists. */
3153 case CPP_OPEN_SQUARE:
3154 ++square_depth;
3155 break;
3156 case CPP_CLOSE_SQUARE:
3157 if (!square_depth--)
3158 return 0;
3159 break;
3161 case CPP_SEMICOLON:
3162 /* This matches the processing in skip_to_end_of_statement. */
3163 if (!brace_depth)
3164 return 0;
3165 break;
3167 case CPP_OPEN_BRACE:
3168 ++brace_depth;
3169 break;
3170 case CPP_CLOSE_BRACE:
3171 if (!brace_depth--)
3172 return 0;
3173 break;
3175 case CPP_COMMA:
3176 if (recovering && or_comma && !brace_depth && !paren_depth
3177 && !square_depth)
3178 return -1;
3179 break;
3181 case CPP_OPEN_PAREN:
3182 if (!brace_depth)
3183 ++paren_depth;
3184 break;
3186 case CPP_CLOSE_PAREN:
3187 if (!brace_depth && !paren_depth--)
3189 if (consume_paren)
3190 cp_lexer_consume_token (parser->lexer);
3191 return 1;
3193 break;
3195 default:
3196 break;
3199 /* Consume the token. */
3200 cp_lexer_consume_token (parser->lexer);
3204 /* Consume tokens until we reach the end of the current statement.
3205 Normally, that will be just before consuming a `;'. However, if a
3206 non-nested `}' comes first, then we stop before consuming that. */
3208 static void
3209 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3211 unsigned nesting_depth = 0;
3213 /* Unwind generic function template scope if necessary. */
3214 if (parser->fully_implicit_function_template_p)
3215 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3217 while (true)
3219 cp_token *token = cp_lexer_peek_token (parser->lexer);
3221 switch (token->type)
3223 case CPP_EOF:
3224 case CPP_PRAGMA_EOL:
3225 /* If we've run out of tokens, stop. */
3226 return;
3228 case CPP_SEMICOLON:
3229 /* If the next token is a `;', we have reached the end of the
3230 statement. */
3231 if (!nesting_depth)
3232 return;
3233 break;
3235 case CPP_CLOSE_BRACE:
3236 /* If this is a non-nested '}', stop before consuming it.
3237 That way, when confronted with something like:
3239 { 3 + }
3241 we stop before consuming the closing '}', even though we
3242 have not yet reached a `;'. */
3243 if (nesting_depth == 0)
3244 return;
3246 /* If it is the closing '}' for a block that we have
3247 scanned, stop -- but only after consuming the token.
3248 That way given:
3250 void f g () { ... }
3251 typedef int I;
3253 we will stop after the body of the erroneously declared
3254 function, but before consuming the following `typedef'
3255 declaration. */
3256 if (--nesting_depth == 0)
3258 cp_lexer_consume_token (parser->lexer);
3259 return;
3262 case CPP_OPEN_BRACE:
3263 ++nesting_depth;
3264 break;
3266 default:
3267 break;
3270 /* Consume the token. */
3271 cp_lexer_consume_token (parser->lexer);
3275 /* This function is called at the end of a statement or declaration.
3276 If the next token is a semicolon, it is consumed; otherwise, error
3277 recovery is attempted. */
3279 static void
3280 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3282 /* Look for the trailing `;'. */
3283 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3285 /* If there is additional (erroneous) input, skip to the end of
3286 the statement. */
3287 cp_parser_skip_to_end_of_statement (parser);
3288 /* If the next token is now a `;', consume it. */
3289 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3290 cp_lexer_consume_token (parser->lexer);
3294 /* Skip tokens until we have consumed an entire block, or until we
3295 have consumed a non-nested `;'. */
3297 static void
3298 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3300 int nesting_depth = 0;
3302 /* Unwind generic function template scope if necessary. */
3303 if (parser->fully_implicit_function_template_p)
3304 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3306 while (nesting_depth >= 0)
3308 cp_token *token = cp_lexer_peek_token (parser->lexer);
3310 switch (token->type)
3312 case CPP_EOF:
3313 case CPP_PRAGMA_EOL:
3314 /* If we've run out of tokens, stop. */
3315 return;
3317 case CPP_SEMICOLON:
3318 /* Stop if this is an unnested ';'. */
3319 if (!nesting_depth)
3320 nesting_depth = -1;
3321 break;
3323 case CPP_CLOSE_BRACE:
3324 /* Stop if this is an unnested '}', or closes the outermost
3325 nesting level. */
3326 nesting_depth--;
3327 if (nesting_depth < 0)
3328 return;
3329 if (!nesting_depth)
3330 nesting_depth = -1;
3331 break;
3333 case CPP_OPEN_BRACE:
3334 /* Nest. */
3335 nesting_depth++;
3336 break;
3338 default:
3339 break;
3342 /* Consume the token. */
3343 cp_lexer_consume_token (parser->lexer);
3347 /* Skip tokens until a non-nested closing curly brace is the next
3348 token, or there are no more tokens. Return true in the first case,
3349 false otherwise. */
3351 static bool
3352 cp_parser_skip_to_closing_brace (cp_parser *parser)
3354 unsigned nesting_depth = 0;
3356 while (true)
3358 cp_token *token = cp_lexer_peek_token (parser->lexer);
3360 switch (token->type)
3362 case CPP_EOF:
3363 case CPP_PRAGMA_EOL:
3364 /* If we've run out of tokens, stop. */
3365 return false;
3367 case CPP_CLOSE_BRACE:
3368 /* If the next token is a non-nested `}', then we have reached
3369 the end of the current block. */
3370 if (nesting_depth-- == 0)
3371 return true;
3372 break;
3374 case CPP_OPEN_BRACE:
3375 /* If it the next token is a `{', then we are entering a new
3376 block. Consume the entire block. */
3377 ++nesting_depth;
3378 break;
3380 default:
3381 break;
3384 /* Consume the token. */
3385 cp_lexer_consume_token (parser->lexer);
3389 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3390 parameter is the PRAGMA token, allowing us to purge the entire pragma
3391 sequence. */
3393 static void
3394 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3396 cp_token *token;
3398 parser->lexer->in_pragma = false;
3401 token = cp_lexer_consume_token (parser->lexer);
3402 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3404 /* Ensure that the pragma is not parsed again. */
3405 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3408 /* Require pragma end of line, resyncing with it as necessary. The
3409 arguments are as for cp_parser_skip_to_pragma_eol. */
3411 static void
3412 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3414 parser->lexer->in_pragma = false;
3415 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3416 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3419 /* This is a simple wrapper around make_typename_type. When the id is
3420 an unresolved identifier node, we can provide a superior diagnostic
3421 using cp_parser_diagnose_invalid_type_name. */
3423 static tree
3424 cp_parser_make_typename_type (cp_parser *parser, tree id,
3425 location_t id_location)
3427 tree result;
3428 if (identifier_p (id))
3430 result = make_typename_type (parser->scope, id, typename_type,
3431 /*complain=*/tf_none);
3432 if (result == error_mark_node)
3433 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3434 return result;
3436 return make_typename_type (parser->scope, id, typename_type, tf_error);
3439 /* This is a wrapper around the
3440 make_{pointer,ptrmem,reference}_declarator functions that decides
3441 which one to call based on the CODE and CLASS_TYPE arguments. The
3442 CODE argument should be one of the values returned by
3443 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3444 appertain to the pointer or reference. */
3446 static cp_declarator *
3447 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3448 cp_cv_quals cv_qualifiers,
3449 cp_declarator *target,
3450 tree attributes)
3452 if (code == ERROR_MARK)
3453 return cp_error_declarator;
3455 if (code == INDIRECT_REF)
3456 if (class_type == NULL_TREE)
3457 return make_pointer_declarator (cv_qualifiers, target, attributes);
3458 else
3459 return make_ptrmem_declarator (cv_qualifiers, class_type,
3460 target, attributes);
3461 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3462 return make_reference_declarator (cv_qualifiers, target,
3463 false, attributes);
3464 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3465 return make_reference_declarator (cv_qualifiers, target,
3466 true, attributes);
3467 gcc_unreachable ();
3470 /* Create a new C++ parser. */
3472 static cp_parser *
3473 cp_parser_new (void)
3475 cp_parser *parser;
3476 cp_lexer *lexer;
3477 unsigned i;
3479 /* cp_lexer_new_main is called before doing GC allocation because
3480 cp_lexer_new_main might load a PCH file. */
3481 lexer = cp_lexer_new_main ();
3483 /* Initialize the binops_by_token so that we can get the tree
3484 directly from the token. */
3485 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3486 binops_by_token[binops[i].token_type] = binops[i];
3488 parser = ggc_cleared_alloc<cp_parser> ();
3489 parser->lexer = lexer;
3490 parser->context = cp_parser_context_new (NULL);
3492 /* For now, we always accept GNU extensions. */
3493 parser->allow_gnu_extensions_p = 1;
3495 /* The `>' token is a greater-than operator, not the end of a
3496 template-id. */
3497 parser->greater_than_is_operator_p = true;
3499 parser->default_arg_ok_p = true;
3501 /* We are not parsing a constant-expression. */
3502 parser->integral_constant_expression_p = false;
3503 parser->allow_non_integral_constant_expression_p = false;
3504 parser->non_integral_constant_expression_p = false;
3506 /* Local variable names are not forbidden. */
3507 parser->local_variables_forbidden_p = false;
3509 /* We are not processing an `extern "C"' declaration. */
3510 parser->in_unbraced_linkage_specification_p = false;
3512 /* We are not processing a declarator. */
3513 parser->in_declarator_p = false;
3515 /* We are not processing a template-argument-list. */
3516 parser->in_template_argument_list_p = false;
3518 /* We are not in an iteration statement. */
3519 parser->in_statement = 0;
3521 /* We are not in a switch statement. */
3522 parser->in_switch_statement_p = false;
3524 /* We are not parsing a type-id inside an expression. */
3525 parser->in_type_id_in_expr_p = false;
3527 /* Declarations aren't implicitly extern "C". */
3528 parser->implicit_extern_c = false;
3530 /* String literals should be translated to the execution character set. */
3531 parser->translate_strings_p = true;
3533 /* We are not parsing a function body. */
3534 parser->in_function_body = false;
3536 /* We can correct until told otherwise. */
3537 parser->colon_corrects_to_scope_p = true;
3539 /* The unparsed function queue is empty. */
3540 push_unparsed_function_queues (parser);
3542 /* There are no classes being defined. */
3543 parser->num_classes_being_defined = 0;
3545 /* No template parameters apply. */
3546 parser->num_template_parameter_lists = 0;
3548 /* Not declaring an implicit function template. */
3549 parser->auto_is_implicit_function_template_parm_p = false;
3550 parser->fully_implicit_function_template_p = false;
3551 parser->implicit_template_parms = 0;
3552 parser->implicit_template_scope = 0;
3554 return parser;
3557 /* Create a cp_lexer structure which will emit the tokens in CACHE
3558 and push it onto the parser's lexer stack. This is used for delayed
3559 parsing of in-class method bodies and default arguments, and should
3560 not be confused with tentative parsing. */
3561 static void
3562 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3564 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3565 lexer->next = parser->lexer;
3566 parser->lexer = lexer;
3568 /* Move the current source position to that of the first token in the
3569 new lexer. */
3570 cp_lexer_set_source_position_from_token (lexer->next_token);
3573 /* Pop the top lexer off the parser stack. This is never used for the
3574 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3575 static void
3576 cp_parser_pop_lexer (cp_parser *parser)
3578 cp_lexer *lexer = parser->lexer;
3579 parser->lexer = lexer->next;
3580 cp_lexer_destroy (lexer);
3582 /* Put the current source position back where it was before this
3583 lexer was pushed. */
3584 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3587 /* Lexical conventions [gram.lex] */
3589 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3590 identifier. */
3592 static tree
3593 cp_parser_identifier (cp_parser* parser)
3595 cp_token *token;
3597 /* Look for the identifier. */
3598 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3599 /* Return the value. */
3600 return token ? token->u.value : error_mark_node;
3603 /* Parse a sequence of adjacent string constants. Returns a
3604 TREE_STRING representing the combined, nul-terminated string
3605 constant. If TRANSLATE is true, translate the string to the
3606 execution character set. If WIDE_OK is true, a wide string is
3607 invalid here.
3609 C++98 [lex.string] says that if a narrow string literal token is
3610 adjacent to a wide string literal token, the behavior is undefined.
3611 However, C99 6.4.5p4 says that this results in a wide string literal.
3612 We follow C99 here, for consistency with the C front end.
3614 This code is largely lifted from lex_string() in c-lex.c.
3616 FUTURE: ObjC++ will need to handle @-strings here. */
3617 static tree
3618 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3619 bool lookup_udlit = true)
3621 tree value;
3622 size_t count;
3623 struct obstack str_ob;
3624 cpp_string str, istr, *strs;
3625 cp_token *tok;
3626 enum cpp_ttype type, curr_type;
3627 int have_suffix_p = 0;
3628 tree string_tree;
3629 tree suffix_id = NULL_TREE;
3630 bool curr_tok_is_userdef_p = false;
3632 tok = cp_lexer_peek_token (parser->lexer);
3633 if (!cp_parser_is_string_literal (tok))
3635 cp_parser_error (parser, "expected string-literal");
3636 return error_mark_node;
3639 if (cpp_userdef_string_p (tok->type))
3641 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3642 curr_type = cpp_userdef_string_remove_type (tok->type);
3643 curr_tok_is_userdef_p = true;
3645 else
3647 string_tree = tok->u.value;
3648 curr_type = tok->type;
3650 type = curr_type;
3652 /* Try to avoid the overhead of creating and destroying an obstack
3653 for the common case of just one string. */
3654 if (!cp_parser_is_string_literal
3655 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3657 cp_lexer_consume_token (parser->lexer);
3659 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3660 str.len = TREE_STRING_LENGTH (string_tree);
3661 count = 1;
3663 if (curr_tok_is_userdef_p)
3665 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3666 have_suffix_p = 1;
3667 curr_type = cpp_userdef_string_remove_type (tok->type);
3669 else
3670 curr_type = tok->type;
3672 strs = &str;
3674 else
3676 gcc_obstack_init (&str_ob);
3677 count = 0;
3681 cp_lexer_consume_token (parser->lexer);
3682 count++;
3683 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3684 str.len = TREE_STRING_LENGTH (string_tree);
3686 if (curr_tok_is_userdef_p)
3688 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3689 if (have_suffix_p == 0)
3691 suffix_id = curr_suffix_id;
3692 have_suffix_p = 1;
3694 else if (have_suffix_p == 1
3695 && curr_suffix_id != suffix_id)
3697 error ("inconsistent user-defined literal suffixes"
3698 " %qD and %qD in string literal",
3699 suffix_id, curr_suffix_id);
3700 have_suffix_p = -1;
3702 curr_type = cpp_userdef_string_remove_type (tok->type);
3704 else
3705 curr_type = tok->type;
3707 if (type != curr_type)
3709 if (type == CPP_STRING)
3710 type = curr_type;
3711 else if (curr_type != CPP_STRING)
3712 error_at (tok->location,
3713 "unsupported non-standard concatenation "
3714 "of string literals");
3717 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3719 tok = cp_lexer_peek_token (parser->lexer);
3720 if (cpp_userdef_string_p (tok->type))
3722 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3723 curr_type = cpp_userdef_string_remove_type (tok->type);
3724 curr_tok_is_userdef_p = true;
3726 else
3728 string_tree = tok->u.value;
3729 curr_type = tok->type;
3730 curr_tok_is_userdef_p = false;
3733 while (cp_parser_is_string_literal (tok));
3735 strs = (cpp_string *) obstack_finish (&str_ob);
3738 if (type != CPP_STRING && !wide_ok)
3740 cp_parser_error (parser, "a wide string is invalid in this context");
3741 type = CPP_STRING;
3744 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3745 (parse_in, strs, count, &istr, type))
3747 value = build_string (istr.len, (const char *)istr.text);
3748 free (CONST_CAST (unsigned char *, istr.text));
3750 switch (type)
3752 default:
3753 case CPP_STRING:
3754 case CPP_UTF8STRING:
3755 TREE_TYPE (value) = char_array_type_node;
3756 break;
3757 case CPP_STRING16:
3758 TREE_TYPE (value) = char16_array_type_node;
3759 break;
3760 case CPP_STRING32:
3761 TREE_TYPE (value) = char32_array_type_node;
3762 break;
3763 case CPP_WSTRING:
3764 TREE_TYPE (value) = wchar_array_type_node;
3765 break;
3768 value = fix_string_type (value);
3770 if (have_suffix_p)
3772 tree literal = build_userdef_literal (suffix_id, value,
3773 OT_NONE, NULL_TREE);
3774 if (lookup_udlit)
3775 value = cp_parser_userdef_string_literal (literal);
3776 else
3777 value = literal;
3780 else
3781 /* cpp_interpret_string has issued an error. */
3782 value = error_mark_node;
3784 if (count > 1)
3785 obstack_free (&str_ob, 0);
3787 return value;
3790 /* Look up a literal operator with the name and the exact arguments. */
3792 static tree
3793 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3795 tree decl, fns;
3796 decl = lookup_name (name);
3797 if (!decl || !is_overloaded_fn (decl))
3798 return error_mark_node;
3800 for (fns = decl; fns; fns = OVL_NEXT (fns))
3802 unsigned int ix;
3803 bool found = true;
3804 tree fn = OVL_CURRENT (fns);
3805 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3806 if (parmtypes != NULL_TREE)
3808 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3809 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3811 tree tparm = TREE_VALUE (parmtypes);
3812 tree targ = TREE_TYPE ((*args)[ix]);
3813 bool ptr = TYPE_PTR_P (tparm);
3814 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3815 if ((ptr || arr || !same_type_p (tparm, targ))
3816 && (!ptr || !arr
3817 || !same_type_p (TREE_TYPE (tparm),
3818 TREE_TYPE (targ))))
3819 found = false;
3821 if (found
3822 && ix == vec_safe_length (args)
3823 /* May be this should be sufficient_parms_p instead,
3824 depending on how exactly should user-defined literals
3825 work in presence of default arguments on the literal
3826 operator parameters. */
3827 && parmtypes == void_list_node)
3828 return decl;
3832 return error_mark_node;
3835 /* Parse a user-defined char constant. Returns a call to a user-defined
3836 literal operator taking the character as an argument. */
3838 static tree
3839 cp_parser_userdef_char_literal (cp_parser *parser)
3841 cp_token *token = cp_lexer_consume_token (parser->lexer);
3842 tree literal = token->u.value;
3843 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3844 tree value = USERDEF_LITERAL_VALUE (literal);
3845 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3846 tree decl, result;
3848 /* Build up a call to the user-defined operator */
3849 /* Lookup the name we got back from the id-expression. */
3850 vec<tree, va_gc> *args = make_tree_vector ();
3851 vec_safe_push (args, value);
3852 decl = lookup_literal_operator (name, args);
3853 if (!decl || decl == error_mark_node)
3855 error ("unable to find character literal operator %qD with %qT argument",
3856 name, TREE_TYPE (value));
3857 release_tree_vector (args);
3858 return error_mark_node;
3860 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3861 release_tree_vector (args);
3862 return result;
3865 /* A subroutine of cp_parser_userdef_numeric_literal to
3866 create a char... template parameter pack from a string node. */
3868 static tree
3869 make_char_string_pack (tree value)
3871 tree charvec;
3872 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3873 const char *str = TREE_STRING_POINTER (value);
3874 int i, len = TREE_STRING_LENGTH (value) - 1;
3875 tree argvec = make_tree_vec (1);
3877 /* Fill in CHARVEC with all of the parameters. */
3878 charvec = make_tree_vec (len);
3879 for (i = 0; i < len; ++i)
3880 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3882 /* Build the argument packs. */
3883 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3884 TREE_TYPE (argpack) = char_type_node;
3886 TREE_VEC_ELT (argvec, 0) = argpack;
3888 return argvec;
3891 /* A subroutine of cp_parser_userdef_numeric_literal to
3892 create a char... template parameter pack from a string node. */
3894 static tree
3895 make_string_pack (tree value)
3897 tree charvec;
3898 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3899 const unsigned char *str
3900 = (const unsigned char *) TREE_STRING_POINTER (value);
3901 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3902 int len = TREE_STRING_LENGTH (value) / sz - 1;
3903 tree argvec = make_tree_vec (2);
3905 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3906 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3908 /* First template parm is character type. */
3909 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3911 /* Fill in CHARVEC with all of the parameters. */
3912 charvec = make_tree_vec (len);
3913 for (int i = 0; i < len; ++i)
3914 TREE_VEC_ELT (charvec, i)
3915 = double_int_to_tree (str_char_type_node,
3916 double_int::from_buffer (str + i * sz, sz));
3918 /* Build the argument packs. */
3919 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3920 TREE_TYPE (argpack) = str_char_type_node;
3922 TREE_VEC_ELT (argvec, 1) = argpack;
3924 return argvec;
3927 /* Parse a user-defined numeric constant. returns a call to a user-defined
3928 literal operator. */
3930 static tree
3931 cp_parser_userdef_numeric_literal (cp_parser *parser)
3933 cp_token *token = cp_lexer_consume_token (parser->lexer);
3934 tree literal = token->u.value;
3935 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3936 tree value = USERDEF_LITERAL_VALUE (literal);
3937 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3938 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3939 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3940 tree decl, result;
3941 vec<tree, va_gc> *args;
3943 /* Look for a literal operator taking the exact type of numeric argument
3944 as the literal value. */
3945 args = make_tree_vector ();
3946 vec_safe_push (args, value);
3947 decl = lookup_literal_operator (name, args);
3948 if (decl && decl != error_mark_node)
3950 result = finish_call_expr (decl, &args, false, true,
3951 tf_warning_or_error);
3953 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3955 warning_at (token->location, OPT_Woverflow,
3956 "integer literal exceeds range of %qT type",
3957 long_long_unsigned_type_node);
3959 else
3961 if (overflow > 0)
3962 warning_at (token->location, OPT_Woverflow,
3963 "floating literal exceeds range of %qT type",
3964 long_double_type_node);
3965 else if (overflow < 0)
3966 warning_at (token->location, OPT_Woverflow,
3967 "floating literal truncated to zero");
3970 release_tree_vector (args);
3971 return result;
3973 release_tree_vector (args);
3975 /* If the numeric argument didn't work, look for a raw literal
3976 operator taking a const char* argument consisting of the number
3977 in string format. */
3978 args = make_tree_vector ();
3979 vec_safe_push (args, num_string);
3980 decl = lookup_literal_operator (name, args);
3981 if (decl && decl != error_mark_node)
3983 result = finish_call_expr (decl, &args, false, true,
3984 tf_warning_or_error);
3985 release_tree_vector (args);
3986 return result;
3988 release_tree_vector (args);
3990 /* If the raw literal didn't work, look for a non-type template
3991 function with parameter pack char.... Call the function with
3992 template parameter characters representing the number. */
3993 args = make_tree_vector ();
3994 decl = lookup_literal_operator (name, args);
3995 if (decl && decl != error_mark_node)
3997 tree tmpl_args = make_char_string_pack (num_string);
3998 decl = lookup_template_function (decl, tmpl_args);
3999 result = finish_call_expr (decl, &args, false, true,
4000 tf_warning_or_error);
4001 release_tree_vector (args);
4002 return result;
4005 release_tree_vector (args);
4007 error ("unable to find numeric literal operator %qD", name);
4008 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4009 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4010 "to enable more built-in suffixes");
4011 return error_mark_node;
4014 /* Parse a user-defined string constant. Returns a call to a user-defined
4015 literal operator taking a character pointer and the length of the string
4016 as arguments. */
4018 static tree
4019 cp_parser_userdef_string_literal (tree literal)
4021 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4022 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4023 tree value = USERDEF_LITERAL_VALUE (literal);
4024 int len = TREE_STRING_LENGTH (value)
4025 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4026 tree decl, result;
4027 vec<tree, va_gc> *args;
4029 /* Build up a call to the user-defined operator. */
4030 /* Lookup the name we got back from the id-expression. */
4031 args = make_tree_vector ();
4032 vec_safe_push (args, value);
4033 vec_safe_push (args, build_int_cst (size_type_node, len));
4034 decl = lookup_literal_operator (name, args);
4036 if (decl && decl != error_mark_node)
4038 result = finish_call_expr (decl, &args, false, true,
4039 tf_warning_or_error);
4040 release_tree_vector (args);
4041 return result;
4043 release_tree_vector (args);
4045 /* Look for a template function with typename parameter CharT
4046 and parameter pack CharT... Call the function with
4047 template parameter characters representing the string. */
4048 args = make_tree_vector ();
4049 decl = lookup_literal_operator (name, args);
4050 if (decl && decl != error_mark_node)
4052 tree tmpl_args = make_string_pack (value);
4053 decl = lookup_template_function (decl, tmpl_args);
4054 result = finish_call_expr (decl, &args, false, true,
4055 tf_warning_or_error);
4056 release_tree_vector (args);
4057 return result;
4059 release_tree_vector (args);
4061 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4062 name, TREE_TYPE (value), size_type_node);
4063 return error_mark_node;
4067 /* Basic concepts [gram.basic] */
4069 /* Parse a translation-unit.
4071 translation-unit:
4072 declaration-seq [opt]
4074 Returns TRUE if all went well. */
4076 static bool
4077 cp_parser_translation_unit (cp_parser* parser)
4079 /* The address of the first non-permanent object on the declarator
4080 obstack. */
4081 static void *declarator_obstack_base;
4083 bool success;
4085 /* Create the declarator obstack, if necessary. */
4086 if (!cp_error_declarator)
4088 gcc_obstack_init (&declarator_obstack);
4089 /* Create the error declarator. */
4090 cp_error_declarator = make_declarator (cdk_error);
4091 /* Create the empty parameter list. */
4092 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4093 /* Remember where the base of the declarator obstack lies. */
4094 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4097 cp_parser_declaration_seq_opt (parser);
4099 /* If there are no tokens left then all went well. */
4100 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4102 /* Get rid of the token array; we don't need it any more. */
4103 cp_lexer_destroy (parser->lexer);
4104 parser->lexer = NULL;
4106 /* This file might have been a context that's implicitly extern
4107 "C". If so, pop the lang context. (Only relevant for PCH.) */
4108 if (parser->implicit_extern_c)
4110 pop_lang_context ();
4111 parser->implicit_extern_c = false;
4114 /* Finish up. */
4115 finish_translation_unit ();
4117 success = true;
4119 else
4121 cp_parser_error (parser, "expected declaration");
4122 success = false;
4125 /* Make sure the declarator obstack was fully cleaned up. */
4126 gcc_assert (obstack_next_free (&declarator_obstack)
4127 == declarator_obstack_base);
4129 /* All went well. */
4130 return success;
4133 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4134 decltype context. */
4136 static inline tsubst_flags_t
4137 complain_flags (bool decltype_p)
4139 tsubst_flags_t complain = tf_warning_or_error;
4140 if (decltype_p)
4141 complain |= tf_decltype;
4142 return complain;
4145 /* We're about to parse a collection of statements. If we're currently
4146 parsing tentatively, set up a firewall so that any nested
4147 cp_parser_commit_to_tentative_parse won't affect the current context. */
4149 static cp_token_position
4150 cp_parser_start_tentative_firewall (cp_parser *parser)
4152 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4153 return 0;
4155 cp_parser_parse_tentatively (parser);
4156 cp_parser_commit_to_topmost_tentative_parse (parser);
4157 return cp_lexer_token_position (parser->lexer, false);
4160 /* We've finished parsing the collection of statements. Wrap up the
4161 firewall and replace the relevant tokens with the parsed form. */
4163 static void
4164 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4165 tree expr)
4167 if (!start)
4168 return;
4170 /* Finish the firewall level. */
4171 cp_parser_parse_definitely (parser);
4172 /* And remember the result of the parse for when we try again. */
4173 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4174 token->type = CPP_PREPARSED_EXPR;
4175 token->u.value = expr;
4176 token->keyword = RID_MAX;
4177 cp_lexer_purge_tokens_after (parser->lexer, start);
4180 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4181 enclosing parentheses. */
4183 static tree
4184 cp_parser_statement_expr (cp_parser *parser)
4186 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4188 /* Consume the '('. */
4189 cp_lexer_consume_token (parser->lexer);
4190 /* Start the statement-expression. */
4191 tree expr = begin_stmt_expr ();
4192 /* Parse the compound-statement. */
4193 cp_parser_compound_statement (parser, expr, false, false);
4194 /* Finish up. */
4195 expr = finish_stmt_expr (expr, false);
4196 /* Consume the ')'. */
4197 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4198 cp_parser_skip_to_end_of_statement (parser);
4200 cp_parser_end_tentative_firewall (parser, start, expr);
4201 return expr;
4204 /* Expressions [gram.expr] */
4206 /* Parse a primary-expression.
4208 primary-expression:
4209 literal
4210 this
4211 ( expression )
4212 id-expression
4213 lambda-expression (C++11)
4215 GNU Extensions:
4217 primary-expression:
4218 ( compound-statement )
4219 __builtin_va_arg ( assignment-expression , type-id )
4220 __builtin_offsetof ( type-id , offsetof-expression )
4222 C++ Extensions:
4223 __has_nothrow_assign ( type-id )
4224 __has_nothrow_constructor ( type-id )
4225 __has_nothrow_copy ( type-id )
4226 __has_trivial_assign ( type-id )
4227 __has_trivial_constructor ( type-id )
4228 __has_trivial_copy ( type-id )
4229 __has_trivial_destructor ( type-id )
4230 __has_virtual_destructor ( type-id )
4231 __is_abstract ( type-id )
4232 __is_base_of ( type-id , type-id )
4233 __is_class ( type-id )
4234 __is_empty ( type-id )
4235 __is_enum ( type-id )
4236 __is_final ( type-id )
4237 __is_literal_type ( type-id )
4238 __is_pod ( type-id )
4239 __is_polymorphic ( type-id )
4240 __is_std_layout ( type-id )
4241 __is_trivial ( type-id )
4242 __is_union ( type-id )
4244 Objective-C++ Extension:
4246 primary-expression:
4247 objc-expression
4249 literal:
4250 __null
4252 ADDRESS_P is true iff this expression was immediately preceded by
4253 "&" and therefore might denote a pointer-to-member. CAST_P is true
4254 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4255 true iff this expression is a template argument.
4257 Returns a representation of the expression. Upon return, *IDK
4258 indicates what kind of id-expression (if any) was present. */
4260 static tree
4261 cp_parser_primary_expression (cp_parser *parser,
4262 bool address_p,
4263 bool cast_p,
4264 bool template_arg_p,
4265 bool decltype_p,
4266 cp_id_kind *idk)
4268 cp_token *token = NULL;
4270 /* Assume the primary expression is not an id-expression. */
4271 *idk = CP_ID_KIND_NONE;
4273 /* Peek at the next token. */
4274 token = cp_lexer_peek_token (parser->lexer);
4275 switch ((int) token->type)
4277 /* literal:
4278 integer-literal
4279 character-literal
4280 floating-literal
4281 string-literal
4282 boolean-literal
4283 pointer-literal
4284 user-defined-literal */
4285 case CPP_CHAR:
4286 case CPP_CHAR16:
4287 case CPP_CHAR32:
4288 case CPP_WCHAR:
4289 case CPP_UTF8CHAR:
4290 case CPP_NUMBER:
4291 case CPP_PREPARSED_EXPR:
4292 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4293 return cp_parser_userdef_numeric_literal (parser);
4294 token = cp_lexer_consume_token (parser->lexer);
4295 if (TREE_CODE (token->u.value) == FIXED_CST)
4297 error_at (token->location,
4298 "fixed-point types not supported in C++");
4299 return error_mark_node;
4301 /* Floating-point literals are only allowed in an integral
4302 constant expression if they are cast to an integral or
4303 enumeration type. */
4304 if (TREE_CODE (token->u.value) == REAL_CST
4305 && parser->integral_constant_expression_p
4306 && pedantic)
4308 /* CAST_P will be set even in invalid code like "int(2.7 +
4309 ...)". Therefore, we have to check that the next token
4310 is sure to end the cast. */
4311 if (cast_p)
4313 cp_token *next_token;
4315 next_token = cp_lexer_peek_token (parser->lexer);
4316 if (/* The comma at the end of an
4317 enumerator-definition. */
4318 next_token->type != CPP_COMMA
4319 /* The curly brace at the end of an enum-specifier. */
4320 && next_token->type != CPP_CLOSE_BRACE
4321 /* The end of a statement. */
4322 && next_token->type != CPP_SEMICOLON
4323 /* The end of the cast-expression. */
4324 && next_token->type != CPP_CLOSE_PAREN
4325 /* The end of an array bound. */
4326 && next_token->type != CPP_CLOSE_SQUARE
4327 /* The closing ">" in a template-argument-list. */
4328 && (next_token->type != CPP_GREATER
4329 || parser->greater_than_is_operator_p)
4330 /* C++0x only: A ">>" treated like two ">" tokens,
4331 in a template-argument-list. */
4332 && (next_token->type != CPP_RSHIFT
4333 || (cxx_dialect == cxx98)
4334 || parser->greater_than_is_operator_p))
4335 cast_p = false;
4338 /* If we are within a cast, then the constraint that the
4339 cast is to an integral or enumeration type will be
4340 checked at that point. If we are not within a cast, then
4341 this code is invalid. */
4342 if (!cast_p)
4343 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4345 return token->u.value;
4347 case CPP_CHAR_USERDEF:
4348 case CPP_CHAR16_USERDEF:
4349 case CPP_CHAR32_USERDEF:
4350 case CPP_WCHAR_USERDEF:
4351 case CPP_UTF8CHAR_USERDEF:
4352 return cp_parser_userdef_char_literal (parser);
4354 case CPP_STRING:
4355 case CPP_STRING16:
4356 case CPP_STRING32:
4357 case CPP_WSTRING:
4358 case CPP_UTF8STRING:
4359 case CPP_STRING_USERDEF:
4360 case CPP_STRING16_USERDEF:
4361 case CPP_STRING32_USERDEF:
4362 case CPP_WSTRING_USERDEF:
4363 case CPP_UTF8STRING_USERDEF:
4364 /* ??? Should wide strings be allowed when parser->translate_strings_p
4365 is false (i.e. in attributes)? If not, we can kill the third
4366 argument to cp_parser_string_literal. */
4367 return cp_parser_string_literal (parser,
4368 parser->translate_strings_p,
4369 true);
4371 case CPP_OPEN_PAREN:
4372 /* If we see `( { ' then we are looking at the beginning of
4373 a GNU statement-expression. */
4374 if (cp_parser_allow_gnu_extensions_p (parser)
4375 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4377 /* Statement-expressions are not allowed by the standard. */
4378 pedwarn (token->location, OPT_Wpedantic,
4379 "ISO C++ forbids braced-groups within expressions");
4381 /* And they're not allowed outside of a function-body; you
4382 cannot, for example, write:
4384 int i = ({ int j = 3; j + 1; });
4386 at class or namespace scope. */
4387 if (!parser->in_function_body
4388 || parser->in_template_argument_list_p)
4390 error_at (token->location,
4391 "statement-expressions are not allowed outside "
4392 "functions nor in template-argument lists");
4393 cp_parser_skip_to_end_of_block_or_statement (parser);
4394 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4395 cp_lexer_consume_token (parser->lexer);
4396 return error_mark_node;
4398 else
4399 return cp_parser_statement_expr (parser);
4401 /* Otherwise it's a normal parenthesized expression. */
4403 tree expr;
4404 bool saved_greater_than_is_operator_p;
4406 /* Consume the `('. */
4407 cp_lexer_consume_token (parser->lexer);
4408 /* Within a parenthesized expression, a `>' token is always
4409 the greater-than operator. */
4410 saved_greater_than_is_operator_p
4411 = parser->greater_than_is_operator_p;
4412 parser->greater_than_is_operator_p = true;
4414 /* Parse the parenthesized expression. */
4415 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4416 /* Let the front end know that this expression was
4417 enclosed in parentheses. This matters in case, for
4418 example, the expression is of the form `A::B', since
4419 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4420 not. */
4421 expr = finish_parenthesized_expr (expr);
4422 /* DR 705: Wrapping an unqualified name in parentheses
4423 suppresses arg-dependent lookup. We want to pass back
4424 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4425 (c++/37862), but none of the others. */
4426 if (*idk != CP_ID_KIND_QUALIFIED)
4427 *idk = CP_ID_KIND_NONE;
4429 /* The `>' token might be the end of a template-id or
4430 template-parameter-list now. */
4431 parser->greater_than_is_operator_p
4432 = saved_greater_than_is_operator_p;
4433 /* Consume the `)'. */
4434 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4435 cp_parser_skip_to_end_of_statement (parser);
4437 return expr;
4440 case CPP_OPEN_SQUARE:
4442 if (c_dialect_objc ())
4444 /* We might have an Objective-C++ message. */
4445 cp_parser_parse_tentatively (parser);
4446 tree msg = cp_parser_objc_message_expression (parser);
4447 /* If that works out, we're done ... */
4448 if (cp_parser_parse_definitely (parser))
4449 return msg;
4450 /* ... else, fall though to see if it's a lambda. */
4452 tree lam = cp_parser_lambda_expression (parser);
4453 /* Don't warn about a failed tentative parse. */
4454 if (cp_parser_error_occurred (parser))
4455 return error_mark_node;
4456 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4457 return lam;
4460 case CPP_OBJC_STRING:
4461 if (c_dialect_objc ())
4462 /* We have an Objective-C++ string literal. */
4463 return cp_parser_objc_expression (parser);
4464 cp_parser_error (parser, "expected primary-expression");
4465 return error_mark_node;
4467 case CPP_KEYWORD:
4468 switch (token->keyword)
4470 /* These two are the boolean literals. */
4471 case RID_TRUE:
4472 cp_lexer_consume_token (parser->lexer);
4473 return boolean_true_node;
4474 case RID_FALSE:
4475 cp_lexer_consume_token (parser->lexer);
4476 return boolean_false_node;
4478 /* The `__null' literal. */
4479 case RID_NULL:
4480 cp_lexer_consume_token (parser->lexer);
4481 return null_node;
4483 /* The `nullptr' literal. */
4484 case RID_NULLPTR:
4485 cp_lexer_consume_token (parser->lexer);
4486 return nullptr_node;
4488 /* Recognize the `this' keyword. */
4489 case RID_THIS:
4490 cp_lexer_consume_token (parser->lexer);
4491 if (parser->local_variables_forbidden_p)
4493 error_at (token->location,
4494 "%<this%> may not be used in this context");
4495 return error_mark_node;
4497 /* Pointers cannot appear in constant-expressions. */
4498 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4499 return error_mark_node;
4500 return finish_this_expr ();
4502 /* The `operator' keyword can be the beginning of an
4503 id-expression. */
4504 case RID_OPERATOR:
4505 goto id_expression;
4507 case RID_FUNCTION_NAME:
4508 case RID_PRETTY_FUNCTION_NAME:
4509 case RID_C99_FUNCTION_NAME:
4511 non_integral_constant name;
4513 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4514 __func__ are the names of variables -- but they are
4515 treated specially. Therefore, they are handled here,
4516 rather than relying on the generic id-expression logic
4517 below. Grammatically, these names are id-expressions.
4519 Consume the token. */
4520 token = cp_lexer_consume_token (parser->lexer);
4522 switch (token->keyword)
4524 case RID_FUNCTION_NAME:
4525 name = NIC_FUNC_NAME;
4526 break;
4527 case RID_PRETTY_FUNCTION_NAME:
4528 name = NIC_PRETTY_FUNC;
4529 break;
4530 case RID_C99_FUNCTION_NAME:
4531 name = NIC_C99_FUNC;
4532 break;
4533 default:
4534 gcc_unreachable ();
4537 if (cp_parser_non_integral_constant_expression (parser, name))
4538 return error_mark_node;
4540 /* Look up the name. */
4541 return finish_fname (token->u.value);
4544 case RID_VA_ARG:
4546 tree expression;
4547 tree type;
4548 source_location type_location;
4550 /* The `__builtin_va_arg' construct is used to handle
4551 `va_arg'. Consume the `__builtin_va_arg' token. */
4552 cp_lexer_consume_token (parser->lexer);
4553 /* Look for the opening `('. */
4554 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4555 /* Now, parse the assignment-expression. */
4556 expression = cp_parser_assignment_expression (parser);
4557 /* Look for the `,'. */
4558 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4559 type_location = cp_lexer_peek_token (parser->lexer)->location;
4560 /* Parse the type-id. */
4561 type = cp_parser_type_id (parser);
4562 /* Look for the closing `)'. */
4563 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4564 /* Using `va_arg' in a constant-expression is not
4565 allowed. */
4566 if (cp_parser_non_integral_constant_expression (parser,
4567 NIC_VA_ARG))
4568 return error_mark_node;
4569 return build_x_va_arg (type_location, expression, type);
4572 case RID_OFFSETOF:
4573 return cp_parser_builtin_offsetof (parser);
4575 case RID_HAS_NOTHROW_ASSIGN:
4576 case RID_HAS_NOTHROW_CONSTRUCTOR:
4577 case RID_HAS_NOTHROW_COPY:
4578 case RID_HAS_TRIVIAL_ASSIGN:
4579 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4580 case RID_HAS_TRIVIAL_COPY:
4581 case RID_HAS_TRIVIAL_DESTRUCTOR:
4582 case RID_HAS_VIRTUAL_DESTRUCTOR:
4583 case RID_IS_ABSTRACT:
4584 case RID_IS_BASE_OF:
4585 case RID_IS_CLASS:
4586 case RID_IS_EMPTY:
4587 case RID_IS_ENUM:
4588 case RID_IS_FINAL:
4589 case RID_IS_LITERAL_TYPE:
4590 case RID_IS_POD:
4591 case RID_IS_POLYMORPHIC:
4592 case RID_IS_STD_LAYOUT:
4593 case RID_IS_TRIVIAL:
4594 case RID_IS_TRIVIALLY_ASSIGNABLE:
4595 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4596 case RID_IS_TRIVIALLY_COPYABLE:
4597 case RID_IS_UNION:
4598 return cp_parser_trait_expr (parser, token->keyword);
4600 /* Objective-C++ expressions. */
4601 case RID_AT_ENCODE:
4602 case RID_AT_PROTOCOL:
4603 case RID_AT_SELECTOR:
4604 return cp_parser_objc_expression (parser);
4606 case RID_TEMPLATE:
4607 if (parser->in_function_body
4608 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4609 == CPP_LESS))
4611 error_at (token->location,
4612 "a template declaration cannot appear at block scope");
4613 cp_parser_skip_to_end_of_block_or_statement (parser);
4614 return error_mark_node;
4616 default:
4617 cp_parser_error (parser, "expected primary-expression");
4618 return error_mark_node;
4621 /* An id-expression can start with either an identifier, a
4622 `::' as the beginning of a qualified-id, or the "operator"
4623 keyword. */
4624 case CPP_NAME:
4625 case CPP_SCOPE:
4626 case CPP_TEMPLATE_ID:
4627 case CPP_NESTED_NAME_SPECIFIER:
4629 tree id_expression;
4630 tree decl;
4631 const char *error_msg;
4632 bool template_p;
4633 bool done;
4634 cp_token *id_expr_token;
4636 id_expression:
4637 /* Parse the id-expression. */
4638 id_expression
4639 = cp_parser_id_expression (parser,
4640 /*template_keyword_p=*/false,
4641 /*check_dependency_p=*/true,
4642 &template_p,
4643 /*declarator_p=*/false,
4644 /*optional_p=*/false);
4645 if (id_expression == error_mark_node)
4646 return error_mark_node;
4647 id_expr_token = token;
4648 token = cp_lexer_peek_token (parser->lexer);
4649 done = (token->type != CPP_OPEN_SQUARE
4650 && token->type != CPP_OPEN_PAREN
4651 && token->type != CPP_DOT
4652 && token->type != CPP_DEREF
4653 && token->type != CPP_PLUS_PLUS
4654 && token->type != CPP_MINUS_MINUS);
4655 /* If we have a template-id, then no further lookup is
4656 required. If the template-id was for a template-class, we
4657 will sometimes have a TYPE_DECL at this point. */
4658 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4659 || TREE_CODE (id_expression) == TYPE_DECL)
4660 decl = id_expression;
4661 /* Look up the name. */
4662 else
4664 tree ambiguous_decls;
4666 /* If we already know that this lookup is ambiguous, then
4667 we've already issued an error message; there's no reason
4668 to check again. */
4669 if (id_expr_token->type == CPP_NAME
4670 && id_expr_token->error_reported)
4672 cp_parser_simulate_error (parser);
4673 return error_mark_node;
4676 decl = cp_parser_lookup_name (parser, id_expression,
4677 none_type,
4678 template_p,
4679 /*is_namespace=*/false,
4680 /*check_dependency=*/true,
4681 &ambiguous_decls,
4682 id_expr_token->location);
4683 /* If the lookup was ambiguous, an error will already have
4684 been issued. */
4685 if (ambiguous_decls)
4686 return error_mark_node;
4688 /* In Objective-C++, we may have an Objective-C 2.0
4689 dot-syntax for classes here. */
4690 if (c_dialect_objc ()
4691 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4692 && TREE_CODE (decl) == TYPE_DECL
4693 && objc_is_class_name (decl))
4695 tree component;
4696 cp_lexer_consume_token (parser->lexer);
4697 component = cp_parser_identifier (parser);
4698 if (component == error_mark_node)
4699 return error_mark_node;
4701 return objc_build_class_component_ref (id_expression, component);
4704 /* In Objective-C++, an instance variable (ivar) may be preferred
4705 to whatever cp_parser_lookup_name() found. */
4706 decl = objc_lookup_ivar (decl, id_expression);
4708 /* If name lookup gives us a SCOPE_REF, then the
4709 qualifying scope was dependent. */
4710 if (TREE_CODE (decl) == SCOPE_REF)
4712 /* At this point, we do not know if DECL is a valid
4713 integral constant expression. We assume that it is
4714 in fact such an expression, so that code like:
4716 template <int N> struct A {
4717 int a[B<N>::i];
4720 is accepted. At template-instantiation time, we
4721 will check that B<N>::i is actually a constant. */
4722 return decl;
4724 /* Check to see if DECL is a local variable in a context
4725 where that is forbidden. */
4726 if (parser->local_variables_forbidden_p
4727 && local_variable_p (decl))
4729 /* It might be that we only found DECL because we are
4730 trying to be generous with pre-ISO scoping rules.
4731 For example, consider:
4733 int i;
4734 void g() {
4735 for (int i = 0; i < 10; ++i) {}
4736 extern void f(int j = i);
4739 Here, name look up will originally find the out
4740 of scope `i'. We need to issue a warning message,
4741 but then use the global `i'. */
4742 decl = check_for_out_of_scope_variable (decl);
4743 if (local_variable_p (decl))
4745 error_at (id_expr_token->location,
4746 "local variable %qD may not appear in this context",
4747 decl);
4748 return error_mark_node;
4753 decl = (finish_id_expression
4754 (id_expression, decl, parser->scope,
4755 idk,
4756 parser->integral_constant_expression_p,
4757 parser->allow_non_integral_constant_expression_p,
4758 &parser->non_integral_constant_expression_p,
4759 template_p, done, address_p,
4760 template_arg_p,
4761 &error_msg,
4762 id_expr_token->location));
4763 if (error_msg)
4764 cp_parser_error (parser, error_msg);
4765 return decl;
4768 /* Anything else is an error. */
4769 default:
4770 cp_parser_error (parser, "expected primary-expression");
4771 return error_mark_node;
4775 static inline tree
4776 cp_parser_primary_expression (cp_parser *parser,
4777 bool address_p,
4778 bool cast_p,
4779 bool template_arg_p,
4780 cp_id_kind *idk)
4782 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4783 /*decltype*/false, idk);
4786 /* Parse an id-expression.
4788 id-expression:
4789 unqualified-id
4790 qualified-id
4792 qualified-id:
4793 :: [opt] nested-name-specifier template [opt] unqualified-id
4794 :: identifier
4795 :: operator-function-id
4796 :: template-id
4798 Return a representation of the unqualified portion of the
4799 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4800 a `::' or nested-name-specifier.
4802 Often, if the id-expression was a qualified-id, the caller will
4803 want to make a SCOPE_REF to represent the qualified-id. This
4804 function does not do this in order to avoid wastefully creating
4805 SCOPE_REFs when they are not required.
4807 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4808 `template' keyword.
4810 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4811 uninstantiated templates.
4813 If *TEMPLATE_P is non-NULL, it is set to true iff the
4814 `template' keyword is used to explicitly indicate that the entity
4815 named is a template.
4817 If DECLARATOR_P is true, the id-expression is appearing as part of
4818 a declarator, rather than as part of an expression. */
4820 static tree
4821 cp_parser_id_expression (cp_parser *parser,
4822 bool template_keyword_p,
4823 bool check_dependency_p,
4824 bool *template_p,
4825 bool declarator_p,
4826 bool optional_p)
4828 bool global_scope_p;
4829 bool nested_name_specifier_p;
4831 /* Assume the `template' keyword was not used. */
4832 if (template_p)
4833 *template_p = template_keyword_p;
4835 /* Look for the optional `::' operator. */
4836 global_scope_p
4837 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4838 != NULL_TREE);
4839 /* Look for the optional nested-name-specifier. */
4840 nested_name_specifier_p
4841 = (cp_parser_nested_name_specifier_opt (parser,
4842 /*typename_keyword_p=*/false,
4843 check_dependency_p,
4844 /*type_p=*/false,
4845 declarator_p)
4846 != NULL_TREE);
4847 /* If there is a nested-name-specifier, then we are looking at
4848 the first qualified-id production. */
4849 if (nested_name_specifier_p)
4851 tree saved_scope;
4852 tree saved_object_scope;
4853 tree saved_qualifying_scope;
4854 tree unqualified_id;
4855 bool is_template;
4857 /* See if the next token is the `template' keyword. */
4858 if (!template_p)
4859 template_p = &is_template;
4860 *template_p = cp_parser_optional_template_keyword (parser);
4861 /* Name lookup we do during the processing of the
4862 unqualified-id might obliterate SCOPE. */
4863 saved_scope = parser->scope;
4864 saved_object_scope = parser->object_scope;
4865 saved_qualifying_scope = parser->qualifying_scope;
4866 /* Process the final unqualified-id. */
4867 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4868 check_dependency_p,
4869 declarator_p,
4870 /*optional_p=*/false);
4871 /* Restore the SAVED_SCOPE for our caller. */
4872 parser->scope = saved_scope;
4873 parser->object_scope = saved_object_scope;
4874 parser->qualifying_scope = saved_qualifying_scope;
4876 return unqualified_id;
4878 /* Otherwise, if we are in global scope, then we are looking at one
4879 of the other qualified-id productions. */
4880 else if (global_scope_p)
4882 cp_token *token;
4883 tree id;
4885 /* Peek at the next token. */
4886 token = cp_lexer_peek_token (parser->lexer);
4888 /* If it's an identifier, and the next token is not a "<", then
4889 we can avoid the template-id case. This is an optimization
4890 for this common case. */
4891 if (token->type == CPP_NAME
4892 && !cp_parser_nth_token_starts_template_argument_list_p
4893 (parser, 2))
4894 return cp_parser_identifier (parser);
4896 cp_parser_parse_tentatively (parser);
4897 /* Try a template-id. */
4898 id = cp_parser_template_id (parser,
4899 /*template_keyword_p=*/false,
4900 /*check_dependency_p=*/true,
4901 none_type,
4902 declarator_p);
4903 /* If that worked, we're done. */
4904 if (cp_parser_parse_definitely (parser))
4905 return id;
4907 /* Peek at the next token. (Changes in the token buffer may
4908 have invalidated the pointer obtained above.) */
4909 token = cp_lexer_peek_token (parser->lexer);
4911 switch (token->type)
4913 case CPP_NAME:
4914 return cp_parser_identifier (parser);
4916 case CPP_KEYWORD:
4917 if (token->keyword == RID_OPERATOR)
4918 return cp_parser_operator_function_id (parser);
4919 /* Fall through. */
4921 default:
4922 cp_parser_error (parser, "expected id-expression");
4923 return error_mark_node;
4926 else
4927 return cp_parser_unqualified_id (parser, template_keyword_p,
4928 /*check_dependency_p=*/true,
4929 declarator_p,
4930 optional_p);
4933 /* Parse an unqualified-id.
4935 unqualified-id:
4936 identifier
4937 operator-function-id
4938 conversion-function-id
4939 ~ class-name
4940 template-id
4942 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4943 keyword, in a construct like `A::template ...'.
4945 Returns a representation of unqualified-id. For the `identifier'
4946 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4947 production a BIT_NOT_EXPR is returned; the operand of the
4948 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4949 other productions, see the documentation accompanying the
4950 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4951 names are looked up in uninstantiated templates. If DECLARATOR_P
4952 is true, the unqualified-id is appearing as part of a declarator,
4953 rather than as part of an expression. */
4955 static tree
4956 cp_parser_unqualified_id (cp_parser* parser,
4957 bool template_keyword_p,
4958 bool check_dependency_p,
4959 bool declarator_p,
4960 bool optional_p)
4962 cp_token *token;
4964 /* Peek at the next token. */
4965 token = cp_lexer_peek_token (parser->lexer);
4967 switch ((int) token->type)
4969 case CPP_NAME:
4971 tree id;
4973 /* We don't know yet whether or not this will be a
4974 template-id. */
4975 cp_parser_parse_tentatively (parser);
4976 /* Try a template-id. */
4977 id = cp_parser_template_id (parser, template_keyword_p,
4978 check_dependency_p,
4979 none_type,
4980 declarator_p);
4981 /* If it worked, we're done. */
4982 if (cp_parser_parse_definitely (parser))
4983 return id;
4984 /* Otherwise, it's an ordinary identifier. */
4985 return cp_parser_identifier (parser);
4988 case CPP_TEMPLATE_ID:
4989 return cp_parser_template_id (parser, template_keyword_p,
4990 check_dependency_p,
4991 none_type,
4992 declarator_p);
4994 case CPP_COMPL:
4996 tree type_decl;
4997 tree qualifying_scope;
4998 tree object_scope;
4999 tree scope;
5000 bool done;
5002 /* Consume the `~' token. */
5003 cp_lexer_consume_token (parser->lexer);
5004 /* Parse the class-name. The standard, as written, seems to
5005 say that:
5007 template <typename T> struct S { ~S (); };
5008 template <typename T> S<T>::~S() {}
5010 is invalid, since `~' must be followed by a class-name, but
5011 `S<T>' is dependent, and so not known to be a class.
5012 That's not right; we need to look in uninstantiated
5013 templates. A further complication arises from:
5015 template <typename T> void f(T t) {
5016 t.T::~T();
5019 Here, it is not possible to look up `T' in the scope of `T'
5020 itself. We must look in both the current scope, and the
5021 scope of the containing complete expression.
5023 Yet another issue is:
5025 struct S {
5026 int S;
5027 ~S();
5030 S::~S() {}
5032 The standard does not seem to say that the `S' in `~S'
5033 should refer to the type `S' and not the data member
5034 `S::S'. */
5036 /* DR 244 says that we look up the name after the "~" in the
5037 same scope as we looked up the qualifying name. That idea
5038 isn't fully worked out; it's more complicated than that. */
5039 scope = parser->scope;
5040 object_scope = parser->object_scope;
5041 qualifying_scope = parser->qualifying_scope;
5043 /* Check for invalid scopes. */
5044 if (scope == error_mark_node)
5046 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5047 cp_lexer_consume_token (parser->lexer);
5048 return error_mark_node;
5050 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5052 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5053 error_at (token->location,
5054 "scope %qT before %<~%> is not a class-name",
5055 scope);
5056 cp_parser_simulate_error (parser);
5057 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5058 cp_lexer_consume_token (parser->lexer);
5059 return error_mark_node;
5061 gcc_assert (!scope || TYPE_P (scope));
5063 /* If the name is of the form "X::~X" it's OK even if X is a
5064 typedef. */
5065 token = cp_lexer_peek_token (parser->lexer);
5066 if (scope
5067 && token->type == CPP_NAME
5068 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5069 != CPP_LESS)
5070 && (token->u.value == TYPE_IDENTIFIER (scope)
5071 || (CLASS_TYPE_P (scope)
5072 && constructor_name_p (token->u.value, scope))))
5074 cp_lexer_consume_token (parser->lexer);
5075 return build_nt (BIT_NOT_EXPR, scope);
5078 /* ~auto means the destructor of whatever the object is. */
5079 if (cp_parser_is_keyword (token, RID_AUTO))
5081 if (cxx_dialect < cxx14)
5082 pedwarn (input_location, 0,
5083 "%<~auto%> only available with "
5084 "-std=c++14 or -std=gnu++14");
5085 cp_lexer_consume_token (parser->lexer);
5086 return build_nt (BIT_NOT_EXPR, make_auto ());
5089 /* If there was an explicit qualification (S::~T), first look
5090 in the scope given by the qualification (i.e., S).
5092 Note: in the calls to cp_parser_class_name below we pass
5093 typename_type so that lookup finds the injected-class-name
5094 rather than the constructor. */
5095 done = false;
5096 type_decl = NULL_TREE;
5097 if (scope)
5099 cp_parser_parse_tentatively (parser);
5100 type_decl = cp_parser_class_name (parser,
5101 /*typename_keyword_p=*/false,
5102 /*template_keyword_p=*/false,
5103 typename_type,
5104 /*check_dependency=*/false,
5105 /*class_head_p=*/false,
5106 declarator_p);
5107 if (cp_parser_parse_definitely (parser))
5108 done = true;
5110 /* In "N::S::~S", look in "N" as well. */
5111 if (!done && scope && qualifying_scope)
5113 cp_parser_parse_tentatively (parser);
5114 parser->scope = qualifying_scope;
5115 parser->object_scope = NULL_TREE;
5116 parser->qualifying_scope = NULL_TREE;
5117 type_decl
5118 = cp_parser_class_name (parser,
5119 /*typename_keyword_p=*/false,
5120 /*template_keyword_p=*/false,
5121 typename_type,
5122 /*check_dependency=*/false,
5123 /*class_head_p=*/false,
5124 declarator_p);
5125 if (cp_parser_parse_definitely (parser))
5126 done = true;
5128 /* In "p->S::~T", look in the scope given by "*p" as well. */
5129 else if (!done && object_scope)
5131 cp_parser_parse_tentatively (parser);
5132 parser->scope = object_scope;
5133 parser->object_scope = NULL_TREE;
5134 parser->qualifying_scope = NULL_TREE;
5135 type_decl
5136 = cp_parser_class_name (parser,
5137 /*typename_keyword_p=*/false,
5138 /*template_keyword_p=*/false,
5139 typename_type,
5140 /*check_dependency=*/false,
5141 /*class_head_p=*/false,
5142 declarator_p);
5143 if (cp_parser_parse_definitely (parser))
5144 done = true;
5146 /* Look in the surrounding context. */
5147 if (!done)
5149 parser->scope = NULL_TREE;
5150 parser->object_scope = NULL_TREE;
5151 parser->qualifying_scope = NULL_TREE;
5152 if (processing_template_decl)
5153 cp_parser_parse_tentatively (parser);
5154 type_decl
5155 = cp_parser_class_name (parser,
5156 /*typename_keyword_p=*/false,
5157 /*template_keyword_p=*/false,
5158 typename_type,
5159 /*check_dependency=*/false,
5160 /*class_head_p=*/false,
5161 declarator_p);
5162 if (processing_template_decl
5163 && ! cp_parser_parse_definitely (parser))
5165 /* We couldn't find a type with this name, so just accept
5166 it and check for a match at instantiation time. */
5167 type_decl = cp_parser_identifier (parser);
5168 if (type_decl != error_mark_node)
5169 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5170 return type_decl;
5173 /* If an error occurred, assume that the name of the
5174 destructor is the same as the name of the qualifying
5175 class. That allows us to keep parsing after running
5176 into ill-formed destructor names. */
5177 if (type_decl == error_mark_node && scope)
5178 return build_nt (BIT_NOT_EXPR, scope);
5179 else if (type_decl == error_mark_node)
5180 return error_mark_node;
5182 /* Check that destructor name and scope match. */
5183 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5185 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5186 error_at (token->location,
5187 "declaration of %<~%T%> as member of %qT",
5188 type_decl, scope);
5189 cp_parser_simulate_error (parser);
5190 return error_mark_node;
5193 /* [class.dtor]
5195 A typedef-name that names a class shall not be used as the
5196 identifier in the declarator for a destructor declaration. */
5197 if (declarator_p
5198 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5199 && !DECL_SELF_REFERENCE_P (type_decl)
5200 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5201 error_at (token->location,
5202 "typedef-name %qD used as destructor declarator",
5203 type_decl);
5205 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5208 case CPP_KEYWORD:
5209 if (token->keyword == RID_OPERATOR)
5211 tree id;
5213 /* This could be a template-id, so we try that first. */
5214 cp_parser_parse_tentatively (parser);
5215 /* Try a template-id. */
5216 id = cp_parser_template_id (parser, template_keyword_p,
5217 /*check_dependency_p=*/true,
5218 none_type,
5219 declarator_p);
5220 /* If that worked, we're done. */
5221 if (cp_parser_parse_definitely (parser))
5222 return id;
5223 /* We still don't know whether we're looking at an
5224 operator-function-id or a conversion-function-id. */
5225 cp_parser_parse_tentatively (parser);
5226 /* Try an operator-function-id. */
5227 id = cp_parser_operator_function_id (parser);
5228 /* If that didn't work, try a conversion-function-id. */
5229 if (!cp_parser_parse_definitely (parser))
5230 id = cp_parser_conversion_function_id (parser);
5231 else if (UDLIT_OPER_P (id))
5233 /* 17.6.3.3.5 */
5234 const char *name = UDLIT_OP_SUFFIX (id);
5235 if (name[0] != '_' && !in_system_header_at (input_location)
5236 && declarator_p)
5237 warning (0, "literal operator suffixes not preceded by %<_%>"
5238 " are reserved for future standardization");
5241 return id;
5243 /* Fall through. */
5245 default:
5246 if (optional_p)
5247 return NULL_TREE;
5248 cp_parser_error (parser, "expected unqualified-id");
5249 return error_mark_node;
5253 /* Parse an (optional) nested-name-specifier.
5255 nested-name-specifier: [C++98]
5256 class-or-namespace-name :: nested-name-specifier [opt]
5257 class-or-namespace-name :: template nested-name-specifier [opt]
5259 nested-name-specifier: [C++0x]
5260 type-name ::
5261 namespace-name ::
5262 nested-name-specifier identifier ::
5263 nested-name-specifier template [opt] simple-template-id ::
5265 PARSER->SCOPE should be set appropriately before this function is
5266 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5267 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5268 in name lookups.
5270 Sets PARSER->SCOPE to the class (TYPE) or namespace
5271 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5272 it unchanged if there is no nested-name-specifier. Returns the new
5273 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5275 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5276 part of a declaration and/or decl-specifier. */
5278 static tree
5279 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5280 bool typename_keyword_p,
5281 bool check_dependency_p,
5282 bool type_p,
5283 bool is_declaration)
5285 bool success = false;
5286 cp_token_position start = 0;
5287 cp_token *token;
5289 /* Remember where the nested-name-specifier starts. */
5290 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5292 start = cp_lexer_token_position (parser->lexer, false);
5293 push_deferring_access_checks (dk_deferred);
5296 while (true)
5298 tree new_scope;
5299 tree old_scope;
5300 tree saved_qualifying_scope;
5301 bool template_keyword_p;
5303 /* Spot cases that cannot be the beginning of a
5304 nested-name-specifier. */
5305 token = cp_lexer_peek_token (parser->lexer);
5307 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5308 the already parsed nested-name-specifier. */
5309 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5311 /* Grab the nested-name-specifier and continue the loop. */
5312 cp_parser_pre_parsed_nested_name_specifier (parser);
5313 /* If we originally encountered this nested-name-specifier
5314 with IS_DECLARATION set to false, we will not have
5315 resolved TYPENAME_TYPEs, so we must do so here. */
5316 if (is_declaration
5317 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5319 new_scope = resolve_typename_type (parser->scope,
5320 /*only_current_p=*/false);
5321 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5322 parser->scope = new_scope;
5324 success = true;
5325 continue;
5328 /* Spot cases that cannot be the beginning of a
5329 nested-name-specifier. On the second and subsequent times
5330 through the loop, we look for the `template' keyword. */
5331 if (success && token->keyword == RID_TEMPLATE)
5333 /* A template-id can start a nested-name-specifier. */
5334 else if (token->type == CPP_TEMPLATE_ID)
5336 /* DR 743: decltype can be used in a nested-name-specifier. */
5337 else if (token_is_decltype (token))
5339 else
5341 /* If the next token is not an identifier, then it is
5342 definitely not a type-name or namespace-name. */
5343 if (token->type != CPP_NAME)
5344 break;
5345 /* If the following token is neither a `<' (to begin a
5346 template-id), nor a `::', then we are not looking at a
5347 nested-name-specifier. */
5348 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5350 if (token->type == CPP_COLON
5351 && parser->colon_corrects_to_scope_p
5352 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5354 error_at (token->location,
5355 "found %<:%> in nested-name-specifier, expected %<::%>");
5356 token->type = CPP_SCOPE;
5359 if (token->type != CPP_SCOPE
5360 && !cp_parser_nth_token_starts_template_argument_list_p
5361 (parser, 2))
5362 break;
5365 /* The nested-name-specifier is optional, so we parse
5366 tentatively. */
5367 cp_parser_parse_tentatively (parser);
5369 /* Look for the optional `template' keyword, if this isn't the
5370 first time through the loop. */
5371 if (success)
5372 template_keyword_p = cp_parser_optional_template_keyword (parser);
5373 else
5374 template_keyword_p = false;
5376 /* Save the old scope since the name lookup we are about to do
5377 might destroy it. */
5378 old_scope = parser->scope;
5379 saved_qualifying_scope = parser->qualifying_scope;
5380 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5381 look up names in "X<T>::I" in order to determine that "Y" is
5382 a template. So, if we have a typename at this point, we make
5383 an effort to look through it. */
5384 if (is_declaration
5385 && !typename_keyword_p
5386 && parser->scope
5387 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5388 parser->scope = resolve_typename_type (parser->scope,
5389 /*only_current_p=*/false);
5390 /* Parse the qualifying entity. */
5391 new_scope
5392 = cp_parser_qualifying_entity (parser,
5393 typename_keyword_p,
5394 template_keyword_p,
5395 check_dependency_p,
5396 type_p,
5397 is_declaration);
5398 /* Look for the `::' token. */
5399 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5401 /* If we found what we wanted, we keep going; otherwise, we're
5402 done. */
5403 if (!cp_parser_parse_definitely (parser))
5405 bool error_p = false;
5407 /* Restore the OLD_SCOPE since it was valid before the
5408 failed attempt at finding the last
5409 class-or-namespace-name. */
5410 parser->scope = old_scope;
5411 parser->qualifying_scope = saved_qualifying_scope;
5413 /* If the next token is a decltype, and the one after that is a
5414 `::', then the decltype has failed to resolve to a class or
5415 enumeration type. Give this error even when parsing
5416 tentatively since it can't possibly be valid--and we're going
5417 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5418 won't get another chance.*/
5419 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5420 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5421 == CPP_SCOPE))
5423 token = cp_lexer_consume_token (parser->lexer);
5424 error_at (token->location, "decltype evaluates to %qT, "
5425 "which is not a class or enumeration type",
5426 token->u.value);
5427 parser->scope = error_mark_node;
5428 error_p = true;
5429 /* As below. */
5430 success = true;
5431 cp_lexer_consume_token (parser->lexer);
5434 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5435 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5437 /* If we have a non-type template-id followed by ::, it can't
5438 possibly be valid. */
5439 token = cp_lexer_peek_token (parser->lexer);
5440 tree tid = token->u.tree_check_value->value;
5441 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5442 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5444 tree tmpl = NULL_TREE;
5445 if (is_overloaded_fn (tid))
5447 tree fns = get_fns (tid);
5448 if (!OVL_CHAIN (fns))
5449 tmpl = OVL_CURRENT (fns);
5450 error_at (token->location, "function template-id %qD "
5451 "in nested-name-specifier", tid);
5453 else
5455 /* Variable template. */
5456 tmpl = TREE_OPERAND (tid, 0);
5457 gcc_assert (variable_template_p (tmpl));
5458 error_at (token->location, "variable template-id %qD "
5459 "in nested-name-specifier", tid);
5461 if (tmpl)
5462 inform (DECL_SOURCE_LOCATION (tmpl),
5463 "%qD declared here", tmpl);
5465 parser->scope = error_mark_node;
5466 error_p = true;
5467 /* As below. */
5468 success = true;
5469 cp_lexer_consume_token (parser->lexer);
5470 cp_lexer_consume_token (parser->lexer);
5474 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5475 break;
5476 /* If the next token is an identifier, and the one after
5477 that is a `::', then any valid interpretation would have
5478 found a class-or-namespace-name. */
5479 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5480 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5481 == CPP_SCOPE)
5482 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5483 != CPP_COMPL))
5485 token = cp_lexer_consume_token (parser->lexer);
5486 if (!error_p)
5488 if (!token->error_reported)
5490 tree decl;
5491 tree ambiguous_decls;
5493 decl = cp_parser_lookup_name (parser, token->u.value,
5494 none_type,
5495 /*is_template=*/false,
5496 /*is_namespace=*/false,
5497 /*check_dependency=*/true,
5498 &ambiguous_decls,
5499 token->location);
5500 if (TREE_CODE (decl) == TEMPLATE_DECL)
5501 error_at (token->location,
5502 "%qD used without template parameters",
5503 decl);
5504 else if (ambiguous_decls)
5506 // cp_parser_lookup_name has the same diagnostic,
5507 // thus make sure to emit it at most once.
5508 if (cp_parser_uncommitted_to_tentative_parse_p
5509 (parser))
5511 error_at (token->location,
5512 "reference to %qD is ambiguous",
5513 token->u.value);
5514 print_candidates (ambiguous_decls);
5516 decl = error_mark_node;
5518 else
5520 if (cxx_dialect != cxx98)
5521 cp_parser_name_lookup_error
5522 (parser, token->u.value, decl, NLE_NOT_CXX98,
5523 token->location);
5524 else
5525 cp_parser_name_lookup_error
5526 (parser, token->u.value, decl, NLE_CXX98,
5527 token->location);
5530 parser->scope = error_mark_node;
5531 error_p = true;
5532 /* Treat this as a successful nested-name-specifier
5533 due to:
5535 [basic.lookup.qual]
5537 If the name found is not a class-name (clause
5538 _class_) or namespace-name (_namespace.def_), the
5539 program is ill-formed. */
5540 success = true;
5542 cp_lexer_consume_token (parser->lexer);
5544 break;
5546 /* We've found one valid nested-name-specifier. */
5547 success = true;
5548 /* Name lookup always gives us a DECL. */
5549 if (TREE_CODE (new_scope) == TYPE_DECL)
5550 new_scope = TREE_TYPE (new_scope);
5551 /* Uses of "template" must be followed by actual templates. */
5552 if (template_keyword_p
5553 && !(CLASS_TYPE_P (new_scope)
5554 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5555 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5556 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5557 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5558 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5559 == TEMPLATE_ID_EXPR)))
5560 permerror (input_location, TYPE_P (new_scope)
5561 ? G_("%qT is not a template")
5562 : G_("%qD is not a template"),
5563 new_scope);
5564 /* If it is a class scope, try to complete it; we are about to
5565 be looking up names inside the class. */
5566 if (TYPE_P (new_scope)
5567 /* Since checking types for dependency can be expensive,
5568 avoid doing it if the type is already complete. */
5569 && !COMPLETE_TYPE_P (new_scope)
5570 /* Do not try to complete dependent types. */
5571 && !dependent_type_p (new_scope))
5573 new_scope = complete_type (new_scope);
5574 /* If it is a typedef to current class, use the current
5575 class instead, as the typedef won't have any names inside
5576 it yet. */
5577 if (!COMPLETE_TYPE_P (new_scope)
5578 && currently_open_class (new_scope))
5579 new_scope = TYPE_MAIN_VARIANT (new_scope);
5581 /* Make sure we look in the right scope the next time through
5582 the loop. */
5583 parser->scope = new_scope;
5586 /* If parsing tentatively, replace the sequence of tokens that makes
5587 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5588 token. That way, should we re-parse the token stream, we will
5589 not have to repeat the effort required to do the parse, nor will
5590 we issue duplicate error messages. */
5591 if (success && start)
5593 cp_token *token;
5595 token = cp_lexer_token_at (parser->lexer, start);
5596 /* Reset the contents of the START token. */
5597 token->type = CPP_NESTED_NAME_SPECIFIER;
5598 /* Retrieve any deferred checks. Do not pop this access checks yet
5599 so the memory will not be reclaimed during token replacing below. */
5600 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5601 token->u.tree_check_value->value = parser->scope;
5602 token->u.tree_check_value->checks = get_deferred_access_checks ();
5603 token->u.tree_check_value->qualifying_scope =
5604 parser->qualifying_scope;
5605 token->keyword = RID_MAX;
5607 /* Purge all subsequent tokens. */
5608 cp_lexer_purge_tokens_after (parser->lexer, start);
5611 if (start)
5612 pop_to_parent_deferring_access_checks ();
5614 return success ? parser->scope : NULL_TREE;
5617 /* Parse a nested-name-specifier. See
5618 cp_parser_nested_name_specifier_opt for details. This function
5619 behaves identically, except that it will an issue an error if no
5620 nested-name-specifier is present. */
5622 static tree
5623 cp_parser_nested_name_specifier (cp_parser *parser,
5624 bool typename_keyword_p,
5625 bool check_dependency_p,
5626 bool type_p,
5627 bool is_declaration)
5629 tree scope;
5631 /* Look for the nested-name-specifier. */
5632 scope = cp_parser_nested_name_specifier_opt (parser,
5633 typename_keyword_p,
5634 check_dependency_p,
5635 type_p,
5636 is_declaration);
5637 /* If it was not present, issue an error message. */
5638 if (!scope)
5640 cp_parser_error (parser, "expected nested-name-specifier");
5641 parser->scope = NULL_TREE;
5644 return scope;
5647 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5648 this is either a class-name or a namespace-name (which corresponds
5649 to the class-or-namespace-name production in the grammar). For
5650 C++0x, it can also be a type-name that refers to an enumeration
5651 type or a simple-template-id.
5653 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5654 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5655 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5656 TYPE_P is TRUE iff the next name should be taken as a class-name,
5657 even the same name is declared to be another entity in the same
5658 scope.
5660 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5661 specified by the class-or-namespace-name. If neither is found the
5662 ERROR_MARK_NODE is returned. */
5664 static tree
5665 cp_parser_qualifying_entity (cp_parser *parser,
5666 bool typename_keyword_p,
5667 bool template_keyword_p,
5668 bool check_dependency_p,
5669 bool type_p,
5670 bool is_declaration)
5672 tree saved_scope;
5673 tree saved_qualifying_scope;
5674 tree saved_object_scope;
5675 tree scope;
5676 bool only_class_p;
5677 bool successful_parse_p;
5679 /* DR 743: decltype can appear in a nested-name-specifier. */
5680 if (cp_lexer_next_token_is_decltype (parser->lexer))
5682 scope = cp_parser_decltype (parser);
5683 if (TREE_CODE (scope) != ENUMERAL_TYPE
5684 && !MAYBE_CLASS_TYPE_P (scope))
5686 cp_parser_simulate_error (parser);
5687 return error_mark_node;
5689 if (TYPE_NAME (scope))
5690 scope = TYPE_NAME (scope);
5691 return scope;
5694 /* Before we try to parse the class-name, we must save away the
5695 current PARSER->SCOPE since cp_parser_class_name will destroy
5696 it. */
5697 saved_scope = parser->scope;
5698 saved_qualifying_scope = parser->qualifying_scope;
5699 saved_object_scope = parser->object_scope;
5700 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5701 there is no need to look for a namespace-name. */
5702 only_class_p = template_keyword_p
5703 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5704 if (!only_class_p)
5705 cp_parser_parse_tentatively (parser);
5706 scope = cp_parser_class_name (parser,
5707 typename_keyword_p,
5708 template_keyword_p,
5709 type_p ? class_type : none_type,
5710 check_dependency_p,
5711 /*class_head_p=*/false,
5712 is_declaration,
5713 /*enum_ok=*/cxx_dialect > cxx98);
5714 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5715 /* If that didn't work, try for a namespace-name. */
5716 if (!only_class_p && !successful_parse_p)
5718 /* Restore the saved scope. */
5719 parser->scope = saved_scope;
5720 parser->qualifying_scope = saved_qualifying_scope;
5721 parser->object_scope = saved_object_scope;
5722 /* If we are not looking at an identifier followed by the scope
5723 resolution operator, then this is not part of a
5724 nested-name-specifier. (Note that this function is only used
5725 to parse the components of a nested-name-specifier.) */
5726 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5727 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5728 return error_mark_node;
5729 scope = cp_parser_namespace_name (parser);
5732 return scope;
5735 /* Return true if we are looking at a compound-literal, false otherwise. */
5737 static bool
5738 cp_parser_compound_literal_p (cp_parser *parser)
5740 /* Consume the `('. */
5741 cp_lexer_consume_token (parser->lexer);
5743 cp_lexer_save_tokens (parser->lexer);
5745 /* Skip tokens until the next token is a closing parenthesis.
5746 If we find the closing `)', and the next token is a `{', then
5747 we are looking at a compound-literal. */
5748 bool compound_literal_p
5749 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5750 /*consume_paren=*/true)
5751 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5753 /* Roll back the tokens we skipped. */
5754 cp_lexer_rollback_tokens (parser->lexer);
5756 return compound_literal_p;
5759 /* Parse a postfix-expression.
5761 postfix-expression:
5762 primary-expression
5763 postfix-expression [ expression ]
5764 postfix-expression ( expression-list [opt] )
5765 simple-type-specifier ( expression-list [opt] )
5766 typename :: [opt] nested-name-specifier identifier
5767 ( expression-list [opt] )
5768 typename :: [opt] nested-name-specifier template [opt] template-id
5769 ( expression-list [opt] )
5770 postfix-expression . template [opt] id-expression
5771 postfix-expression -> template [opt] id-expression
5772 postfix-expression . pseudo-destructor-name
5773 postfix-expression -> pseudo-destructor-name
5774 postfix-expression ++
5775 postfix-expression --
5776 dynamic_cast < type-id > ( expression )
5777 static_cast < type-id > ( expression )
5778 reinterpret_cast < type-id > ( expression )
5779 const_cast < type-id > ( expression )
5780 typeid ( expression )
5781 typeid ( type-id )
5783 GNU Extension:
5785 postfix-expression:
5786 ( type-id ) { initializer-list , [opt] }
5788 This extension is a GNU version of the C99 compound-literal
5789 construct. (The C99 grammar uses `type-name' instead of `type-id',
5790 but they are essentially the same concept.)
5792 If ADDRESS_P is true, the postfix expression is the operand of the
5793 `&' operator. CAST_P is true if this expression is the target of a
5794 cast.
5796 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5797 class member access expressions [expr.ref].
5799 Returns a representation of the expression. */
5801 static tree
5802 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5803 bool member_access_only_p, bool decltype_p,
5804 cp_id_kind * pidk_return)
5806 cp_token *token;
5807 location_t loc;
5808 enum rid keyword;
5809 cp_id_kind idk = CP_ID_KIND_NONE;
5810 tree postfix_expression = NULL_TREE;
5811 bool is_member_access = false;
5812 int saved_in_statement = -1;
5814 /* Peek at the next token. */
5815 token = cp_lexer_peek_token (parser->lexer);
5816 loc = token->location;
5817 /* Some of the productions are determined by keywords. */
5818 keyword = token->keyword;
5819 switch (keyword)
5821 case RID_DYNCAST:
5822 case RID_STATCAST:
5823 case RID_REINTCAST:
5824 case RID_CONSTCAST:
5826 tree type;
5827 tree expression;
5828 const char *saved_message;
5829 bool saved_in_type_id_in_expr_p;
5831 /* All of these can be handled in the same way from the point
5832 of view of parsing. Begin by consuming the token
5833 identifying the cast. */
5834 cp_lexer_consume_token (parser->lexer);
5836 /* New types cannot be defined in the cast. */
5837 saved_message = parser->type_definition_forbidden_message;
5838 parser->type_definition_forbidden_message
5839 = G_("types may not be defined in casts");
5841 /* Look for the opening `<'. */
5842 cp_parser_require (parser, CPP_LESS, RT_LESS);
5843 /* Parse the type to which we are casting. */
5844 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5845 parser->in_type_id_in_expr_p = true;
5846 type = cp_parser_type_id (parser);
5847 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5848 /* Look for the closing `>'. */
5849 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5850 /* Restore the old message. */
5851 parser->type_definition_forbidden_message = saved_message;
5853 bool saved_greater_than_is_operator_p
5854 = parser->greater_than_is_operator_p;
5855 parser->greater_than_is_operator_p = true;
5857 /* And the expression which is being cast. */
5858 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5859 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5860 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5862 parser->greater_than_is_operator_p
5863 = saved_greater_than_is_operator_p;
5865 /* Only type conversions to integral or enumeration types
5866 can be used in constant-expressions. */
5867 if (!cast_valid_in_integral_constant_expression_p (type)
5868 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5869 return error_mark_node;
5871 switch (keyword)
5873 case RID_DYNCAST:
5874 postfix_expression
5875 = build_dynamic_cast (type, expression, tf_warning_or_error);
5876 break;
5877 case RID_STATCAST:
5878 postfix_expression
5879 = build_static_cast (type, expression, tf_warning_or_error);
5880 break;
5881 case RID_REINTCAST:
5882 postfix_expression
5883 = build_reinterpret_cast (type, expression,
5884 tf_warning_or_error);
5885 break;
5886 case RID_CONSTCAST:
5887 postfix_expression
5888 = build_const_cast (type, expression, tf_warning_or_error);
5889 break;
5890 default:
5891 gcc_unreachable ();
5894 break;
5896 case RID_TYPEID:
5898 tree type;
5899 const char *saved_message;
5900 bool saved_in_type_id_in_expr_p;
5902 /* Consume the `typeid' token. */
5903 cp_lexer_consume_token (parser->lexer);
5904 /* Look for the `(' token. */
5905 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5906 /* Types cannot be defined in a `typeid' expression. */
5907 saved_message = parser->type_definition_forbidden_message;
5908 parser->type_definition_forbidden_message
5909 = G_("types may not be defined in a %<typeid%> expression");
5910 /* We can't be sure yet whether we're looking at a type-id or an
5911 expression. */
5912 cp_parser_parse_tentatively (parser);
5913 /* Try a type-id first. */
5914 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5915 parser->in_type_id_in_expr_p = true;
5916 type = cp_parser_type_id (parser);
5917 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5918 /* Look for the `)' token. Otherwise, we can't be sure that
5919 we're not looking at an expression: consider `typeid (int
5920 (3))', for example. */
5921 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5922 /* If all went well, simply lookup the type-id. */
5923 if (cp_parser_parse_definitely (parser))
5924 postfix_expression = get_typeid (type, tf_warning_or_error);
5925 /* Otherwise, fall back to the expression variant. */
5926 else
5928 tree expression;
5930 /* Look for an expression. */
5931 expression = cp_parser_expression (parser, & idk);
5932 /* Compute its typeid. */
5933 postfix_expression = build_typeid (expression, tf_warning_or_error);
5934 /* Look for the `)' token. */
5935 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5937 /* Restore the saved message. */
5938 parser->type_definition_forbidden_message = saved_message;
5939 /* `typeid' may not appear in an integral constant expression. */
5940 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5941 return error_mark_node;
5943 break;
5945 case RID_TYPENAME:
5947 tree type;
5948 /* The syntax permitted here is the same permitted for an
5949 elaborated-type-specifier. */
5950 type = cp_parser_elaborated_type_specifier (parser,
5951 /*is_friend=*/false,
5952 /*is_declaration=*/false);
5953 postfix_expression = cp_parser_functional_cast (parser, type);
5955 break;
5957 case RID_CILK_SPAWN:
5959 cp_lexer_consume_token (parser->lexer);
5960 token = cp_lexer_peek_token (parser->lexer);
5961 if (token->type == CPP_SEMICOLON)
5963 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5964 "an expression");
5965 postfix_expression = error_mark_node;
5966 break;
5968 else if (!current_function_decl)
5970 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5971 "inside a function");
5972 postfix_expression = error_mark_node;
5973 break;
5975 else
5977 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5978 saved_in_statement = parser->in_statement;
5979 parser->in_statement |= IN_CILK_SPAWN;
5981 cfun->calls_cilk_spawn = 1;
5982 postfix_expression =
5983 cp_parser_postfix_expression (parser, false, false,
5984 false, false, &idk);
5985 if (!flag_cilkplus)
5987 error_at (token->location, "-fcilkplus must be enabled to use"
5988 " %<_Cilk_spawn%>");
5989 cfun->calls_cilk_spawn = 0;
5991 else if (saved_in_statement & IN_CILK_SPAWN)
5993 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5994 "are not permitted");
5995 postfix_expression = error_mark_node;
5996 cfun->calls_cilk_spawn = 0;
5998 else
6000 postfix_expression = build_cilk_spawn (token->location,
6001 postfix_expression);
6002 if (postfix_expression != error_mark_node)
6003 SET_EXPR_LOCATION (postfix_expression, input_location);
6004 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6006 break;
6009 case RID_BUILTIN_SHUFFLE:
6011 vec<tree, va_gc> *vec;
6012 unsigned int i;
6013 tree p;
6015 cp_lexer_consume_token (parser->lexer);
6016 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6017 /*cast_p=*/false, /*allow_expansion_p=*/true,
6018 /*non_constant_p=*/NULL);
6019 if (vec == NULL)
6020 return error_mark_node;
6022 FOR_EACH_VEC_ELT (*vec, i, p)
6023 mark_exp_read (p);
6025 if (vec->length () == 2)
6026 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6027 tf_warning_or_error);
6028 else if (vec->length () == 3)
6029 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6030 tf_warning_or_error);
6031 else
6033 error_at (loc, "wrong number of arguments to "
6034 "%<__builtin_shuffle%>");
6035 return error_mark_node;
6037 break;
6040 default:
6042 tree type;
6044 /* If the next thing is a simple-type-specifier, we may be
6045 looking at a functional cast. We could also be looking at
6046 an id-expression. So, we try the functional cast, and if
6047 that doesn't work we fall back to the primary-expression. */
6048 cp_parser_parse_tentatively (parser);
6049 /* Look for the simple-type-specifier. */
6050 type = cp_parser_simple_type_specifier (parser,
6051 /*decl_specs=*/NULL,
6052 CP_PARSER_FLAGS_NONE);
6053 /* Parse the cast itself. */
6054 if (!cp_parser_error_occurred (parser))
6055 postfix_expression
6056 = cp_parser_functional_cast (parser, type);
6057 /* If that worked, we're done. */
6058 if (cp_parser_parse_definitely (parser))
6059 break;
6061 /* If the functional-cast didn't work out, try a
6062 compound-literal. */
6063 if (cp_parser_allow_gnu_extensions_p (parser)
6064 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6066 tree initializer = NULL_TREE;
6068 cp_parser_parse_tentatively (parser);
6070 /* Avoid calling cp_parser_type_id pointlessly, see comment
6071 in cp_parser_cast_expression about c++/29234. */
6072 if (!cp_parser_compound_literal_p (parser))
6073 cp_parser_simulate_error (parser);
6074 else
6076 /* Parse the type. */
6077 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6078 parser->in_type_id_in_expr_p = true;
6079 type = cp_parser_type_id (parser);
6080 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6081 /* Look for the `)'. */
6082 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6085 /* If things aren't going well, there's no need to
6086 keep going. */
6087 if (!cp_parser_error_occurred (parser))
6089 bool non_constant_p;
6090 /* Parse the brace-enclosed initializer list. */
6091 initializer = cp_parser_braced_list (parser,
6092 &non_constant_p);
6094 /* If that worked, we're definitely looking at a
6095 compound-literal expression. */
6096 if (cp_parser_parse_definitely (parser))
6098 /* Warn the user that a compound literal is not
6099 allowed in standard C++. */
6100 pedwarn (input_location, OPT_Wpedantic,
6101 "ISO C++ forbids compound-literals");
6102 /* For simplicity, we disallow compound literals in
6103 constant-expressions. We could
6104 allow compound literals of integer type, whose
6105 initializer was a constant, in constant
6106 expressions. Permitting that usage, as a further
6107 extension, would not change the meaning of any
6108 currently accepted programs. (Of course, as
6109 compound literals are not part of ISO C++, the
6110 standard has nothing to say.) */
6111 if (cp_parser_non_integral_constant_expression (parser,
6112 NIC_NCC))
6114 postfix_expression = error_mark_node;
6115 break;
6117 /* Form the representation of the compound-literal. */
6118 postfix_expression
6119 = finish_compound_literal (type, initializer,
6120 tf_warning_or_error);
6121 break;
6125 /* It must be a primary-expression. */
6126 postfix_expression
6127 = cp_parser_primary_expression (parser, address_p, cast_p,
6128 /*template_arg_p=*/false,
6129 decltype_p,
6130 &idk);
6132 break;
6135 /* Note that we don't need to worry about calling build_cplus_new on a
6136 class-valued CALL_EXPR in decltype when it isn't the end of the
6137 postfix-expression; unary_complex_lvalue will take care of that for
6138 all these cases. */
6140 /* Keep looping until the postfix-expression is complete. */
6141 while (true)
6143 if (idk == CP_ID_KIND_UNQUALIFIED
6144 && identifier_p (postfix_expression)
6145 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6146 /* It is not a Koenig lookup function call. */
6147 postfix_expression
6148 = unqualified_name_lookup_error (postfix_expression);
6150 /* Peek at the next token. */
6151 token = cp_lexer_peek_token (parser->lexer);
6153 switch (token->type)
6155 case CPP_OPEN_SQUARE:
6156 if (cp_next_tokens_can_be_std_attribute_p (parser))
6158 cp_parser_error (parser,
6159 "two consecutive %<[%> shall "
6160 "only introduce an attribute");
6161 return error_mark_node;
6163 postfix_expression
6164 = cp_parser_postfix_open_square_expression (parser,
6165 postfix_expression,
6166 false,
6167 decltype_p);
6168 idk = CP_ID_KIND_NONE;
6169 is_member_access = false;
6170 break;
6172 case CPP_OPEN_PAREN:
6173 /* postfix-expression ( expression-list [opt] ) */
6175 bool koenig_p;
6176 bool is_builtin_constant_p;
6177 bool saved_integral_constant_expression_p = false;
6178 bool saved_non_integral_constant_expression_p = false;
6179 tsubst_flags_t complain = complain_flags (decltype_p);
6180 vec<tree, va_gc> *args;
6182 is_member_access = false;
6184 is_builtin_constant_p
6185 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6186 if (is_builtin_constant_p)
6188 /* The whole point of __builtin_constant_p is to allow
6189 non-constant expressions to appear as arguments. */
6190 saved_integral_constant_expression_p
6191 = parser->integral_constant_expression_p;
6192 saved_non_integral_constant_expression_p
6193 = parser->non_integral_constant_expression_p;
6194 parser->integral_constant_expression_p = false;
6196 args = (cp_parser_parenthesized_expression_list
6197 (parser, non_attr,
6198 /*cast_p=*/false, /*allow_expansion_p=*/true,
6199 /*non_constant_p=*/NULL,
6200 /*want_literal_zero_p=*/warn_memset_transposed_args));
6201 if (is_builtin_constant_p)
6203 parser->integral_constant_expression_p
6204 = saved_integral_constant_expression_p;
6205 parser->non_integral_constant_expression_p
6206 = saved_non_integral_constant_expression_p;
6209 if (args == NULL)
6211 postfix_expression = error_mark_node;
6212 break;
6215 /* Function calls are not permitted in
6216 constant-expressions. */
6217 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6218 && cp_parser_non_integral_constant_expression (parser,
6219 NIC_FUNC_CALL))
6221 postfix_expression = error_mark_node;
6222 release_tree_vector (args);
6223 break;
6226 koenig_p = false;
6227 if (idk == CP_ID_KIND_UNQUALIFIED
6228 || idk == CP_ID_KIND_TEMPLATE_ID)
6230 if (identifier_p (postfix_expression))
6232 if (!args->is_empty ())
6234 koenig_p = true;
6235 if (!any_type_dependent_arguments_p (args))
6236 postfix_expression
6237 = perform_koenig_lookup (postfix_expression, args,
6238 complain);
6240 else
6241 postfix_expression
6242 = unqualified_fn_lookup_error (postfix_expression);
6244 /* We do not perform argument-dependent lookup if
6245 normal lookup finds a non-function, in accordance
6246 with the expected resolution of DR 218. */
6247 else if (!args->is_empty ()
6248 && is_overloaded_fn (postfix_expression))
6250 tree fn = get_first_fn (postfix_expression);
6251 fn = STRIP_TEMPLATE (fn);
6253 /* Do not do argument dependent lookup if regular
6254 lookup finds a member function or a block-scope
6255 function declaration. [basic.lookup.argdep]/3 */
6256 if (!DECL_FUNCTION_MEMBER_P (fn)
6257 && !DECL_LOCAL_FUNCTION_P (fn))
6259 koenig_p = true;
6260 if (!any_type_dependent_arguments_p (args))
6261 postfix_expression
6262 = perform_koenig_lookup (postfix_expression, args,
6263 complain);
6268 if (warn_memset_transposed_args)
6270 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6271 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6272 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6273 && vec_safe_length (args) == 3
6274 && integer_zerop ((*args)[2])
6275 && LITERAL_ZERO_P ((*args)[2])
6276 && !(integer_zerop ((*args)[1])
6277 && LITERAL_ZERO_P ((*args)[1])))
6278 warning (OPT_Wmemset_transposed_args,
6279 "%<memset%> used with constant zero length "
6280 "parameter; this could be due to transposed "
6281 "parameters");
6283 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6284 to avoid leaking those into folder and middle-end. */
6285 unsigned int i;
6286 tree arg;
6287 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6288 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6289 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6292 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6294 tree instance = TREE_OPERAND (postfix_expression, 0);
6295 tree fn = TREE_OPERAND (postfix_expression, 1);
6297 if (processing_template_decl
6298 && (type_dependent_expression_p (instance)
6299 || (!BASELINK_P (fn)
6300 && TREE_CODE (fn) != FIELD_DECL)
6301 || type_dependent_expression_p (fn)
6302 || any_type_dependent_arguments_p (args)))
6304 postfix_expression
6305 = build_nt_call_vec (postfix_expression, args);
6306 release_tree_vector (args);
6307 break;
6310 if (BASELINK_P (fn))
6312 postfix_expression
6313 = (build_new_method_call
6314 (instance, fn, &args, NULL_TREE,
6315 (idk == CP_ID_KIND_QUALIFIED
6316 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6317 : LOOKUP_NORMAL),
6318 /*fn_p=*/NULL,
6319 complain));
6321 else
6322 postfix_expression
6323 = finish_call_expr (postfix_expression, &args,
6324 /*disallow_virtual=*/false,
6325 /*koenig_p=*/false,
6326 complain);
6328 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6329 || TREE_CODE (postfix_expression) == MEMBER_REF
6330 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6331 postfix_expression = (build_offset_ref_call_from_tree
6332 (postfix_expression, &args,
6333 complain));
6334 else if (idk == CP_ID_KIND_QUALIFIED)
6335 /* A call to a static class member, or a namespace-scope
6336 function. */
6337 postfix_expression
6338 = finish_call_expr (postfix_expression, &args,
6339 /*disallow_virtual=*/true,
6340 koenig_p,
6341 complain);
6342 else
6343 /* All other function calls. */
6344 postfix_expression
6345 = finish_call_expr (postfix_expression, &args,
6346 /*disallow_virtual=*/false,
6347 koenig_p,
6348 complain);
6350 protected_set_expr_location (postfix_expression, token->location);
6352 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6353 idk = CP_ID_KIND_NONE;
6355 release_tree_vector (args);
6357 break;
6359 case CPP_DOT:
6360 case CPP_DEREF:
6361 /* postfix-expression . template [opt] id-expression
6362 postfix-expression . pseudo-destructor-name
6363 postfix-expression -> template [opt] id-expression
6364 postfix-expression -> pseudo-destructor-name */
6366 /* Consume the `.' or `->' operator. */
6367 cp_lexer_consume_token (parser->lexer);
6369 postfix_expression
6370 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6371 postfix_expression,
6372 false, &idk, loc);
6374 is_member_access = true;
6375 break;
6377 case CPP_PLUS_PLUS:
6378 /* postfix-expression ++ */
6379 /* Consume the `++' token. */
6380 cp_lexer_consume_token (parser->lexer);
6381 /* Generate a representation for the complete expression. */
6382 postfix_expression
6383 = finish_increment_expr (postfix_expression,
6384 POSTINCREMENT_EXPR);
6385 /* Increments may not appear in constant-expressions. */
6386 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6387 postfix_expression = error_mark_node;
6388 idk = CP_ID_KIND_NONE;
6389 is_member_access = false;
6390 break;
6392 case CPP_MINUS_MINUS:
6393 /* postfix-expression -- */
6394 /* Consume the `--' token. */
6395 cp_lexer_consume_token (parser->lexer);
6396 /* Generate a representation for the complete expression. */
6397 postfix_expression
6398 = finish_increment_expr (postfix_expression,
6399 POSTDECREMENT_EXPR);
6400 /* Decrements may not appear in constant-expressions. */
6401 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6402 postfix_expression = error_mark_node;
6403 idk = CP_ID_KIND_NONE;
6404 is_member_access = false;
6405 break;
6407 default:
6408 if (pidk_return != NULL)
6409 * pidk_return = idk;
6410 if (member_access_only_p)
6411 return is_member_access? postfix_expression : error_mark_node;
6412 else
6413 return postfix_expression;
6417 /* We should never get here. */
6418 gcc_unreachable ();
6419 return error_mark_node;
6422 /* This function parses Cilk Plus array notations. If a normal array expr. is
6423 parsed then the array index is passed back to the caller through *INIT_INDEX
6424 and the function returns a NULL_TREE. If array notation expr. is parsed,
6425 then *INIT_INDEX is ignored by the caller and the function returns
6426 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6427 error_mark_node. */
6429 static tree
6430 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6431 tree array_value)
6433 cp_token *token = NULL;
6434 tree length_index, stride = NULL_TREE, value_tree, array_type;
6435 if (!array_value || array_value == error_mark_node)
6437 cp_parser_skip_to_end_of_statement (parser);
6438 return error_mark_node;
6441 array_type = TREE_TYPE (array_value);
6443 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6444 parser->colon_corrects_to_scope_p = false;
6445 token = cp_lexer_peek_token (parser->lexer);
6447 if (!token)
6449 cp_parser_error (parser, "expected %<:%> or numeral");
6450 return error_mark_node;
6452 else if (token->type == CPP_COLON)
6454 /* Consume the ':'. */
6455 cp_lexer_consume_token (parser->lexer);
6457 /* If we are here, then we have a case like this A[:]. */
6458 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6460 cp_parser_error (parser, "expected %<]%>");
6461 cp_parser_skip_to_end_of_statement (parser);
6462 return error_mark_node;
6464 *init_index = NULL_TREE;
6465 stride = NULL_TREE;
6466 length_index = NULL_TREE;
6468 else
6470 /* If we are here, then there are three valid possibilities:
6471 1. ARRAY [ EXP ]
6472 2. ARRAY [ EXP : EXP ]
6473 3. ARRAY [ EXP : EXP : EXP ] */
6475 *init_index = cp_parser_expression (parser);
6476 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6478 /* This indicates that we have a normal array expression. */
6479 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6480 return NULL_TREE;
6483 /* Consume the ':'. */
6484 cp_lexer_consume_token (parser->lexer);
6485 length_index = cp_parser_expression (parser);
6486 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6488 cp_lexer_consume_token (parser->lexer);
6489 stride = cp_parser_expression (parser);
6492 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6494 if (*init_index == error_mark_node || length_index == error_mark_node
6495 || stride == error_mark_node || array_type == error_mark_node)
6497 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6498 cp_lexer_consume_token (parser->lexer);
6499 return error_mark_node;
6501 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6503 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6504 length_index, stride, array_type);
6505 return value_tree;
6508 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6509 by cp_parser_builtin_offsetof. We're looking for
6511 postfix-expression [ expression ]
6512 postfix-expression [ braced-init-list ] (C++11)
6514 FOR_OFFSETOF is set if we're being called in that context, which
6515 changes how we deal with integer constant expressions. */
6517 static tree
6518 cp_parser_postfix_open_square_expression (cp_parser *parser,
6519 tree postfix_expression,
6520 bool for_offsetof,
6521 bool decltype_p)
6523 tree index = NULL_TREE;
6524 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6525 bool saved_greater_than_is_operator_p;
6527 /* Consume the `[' token. */
6528 cp_lexer_consume_token (parser->lexer);
6530 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6531 parser->greater_than_is_operator_p = true;
6533 /* Parse the index expression. */
6534 /* ??? For offsetof, there is a question of what to allow here. If
6535 offsetof is not being used in an integral constant expression context,
6536 then we *could* get the right answer by computing the value at runtime.
6537 If we are in an integral constant expression context, then we might
6538 could accept any constant expression; hard to say without analysis.
6539 Rather than open the barn door too wide right away, allow only integer
6540 constant expressions here. */
6541 if (for_offsetof)
6542 index = cp_parser_constant_expression (parser);
6543 else
6545 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6547 bool expr_nonconst_p;
6548 cp_lexer_set_source_position (parser->lexer);
6549 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6550 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6551 if (flag_cilkplus
6552 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6554 error_at (cp_lexer_peek_token (parser->lexer)->location,
6555 "braced list index is not allowed with array "
6556 "notation");
6557 cp_parser_skip_to_end_of_statement (parser);
6558 return error_mark_node;
6561 else if (flag_cilkplus)
6563 /* Here are have these two options:
6564 ARRAY[EXP : EXP] - Array notation expr with default
6565 stride of 1.
6566 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6567 stride. */
6568 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6569 postfix_expression);
6570 if (an_exp)
6571 return an_exp;
6573 else
6574 index = cp_parser_expression (parser);
6577 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6579 /* Look for the closing `]'. */
6580 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6582 /* Build the ARRAY_REF. */
6583 postfix_expression = grok_array_decl (loc, postfix_expression,
6584 index, decltype_p);
6586 /* When not doing offsetof, array references are not permitted in
6587 constant-expressions. */
6588 if (!for_offsetof
6589 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6590 postfix_expression = error_mark_node;
6592 return postfix_expression;
6595 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6596 by cp_parser_builtin_offsetof. We're looking for
6598 postfix-expression . template [opt] id-expression
6599 postfix-expression . pseudo-destructor-name
6600 postfix-expression -> template [opt] id-expression
6601 postfix-expression -> pseudo-destructor-name
6603 FOR_OFFSETOF is set if we're being called in that context. That sorta
6604 limits what of the above we'll actually accept, but nevermind.
6605 TOKEN_TYPE is the "." or "->" token, which will already have been
6606 removed from the stream. */
6608 static tree
6609 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6610 enum cpp_ttype token_type,
6611 tree postfix_expression,
6612 bool for_offsetof, cp_id_kind *idk,
6613 location_t location)
6615 tree name;
6616 bool dependent_p;
6617 bool pseudo_destructor_p;
6618 tree scope = NULL_TREE;
6620 /* If this is a `->' operator, dereference the pointer. */
6621 if (token_type == CPP_DEREF)
6622 postfix_expression = build_x_arrow (location, postfix_expression,
6623 tf_warning_or_error);
6624 /* Check to see whether or not the expression is type-dependent. */
6625 dependent_p = type_dependent_expression_p (postfix_expression);
6626 /* The identifier following the `->' or `.' is not qualified. */
6627 parser->scope = NULL_TREE;
6628 parser->qualifying_scope = NULL_TREE;
6629 parser->object_scope = NULL_TREE;
6630 *idk = CP_ID_KIND_NONE;
6632 /* Enter the scope corresponding to the type of the object
6633 given by the POSTFIX_EXPRESSION. */
6634 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6636 scope = TREE_TYPE (postfix_expression);
6637 /* According to the standard, no expression should ever have
6638 reference type. Unfortunately, we do not currently match
6639 the standard in this respect in that our internal representation
6640 of an expression may have reference type even when the standard
6641 says it does not. Therefore, we have to manually obtain the
6642 underlying type here. */
6643 scope = non_reference (scope);
6644 /* The type of the POSTFIX_EXPRESSION must be complete. */
6645 if (scope == unknown_type_node)
6647 error_at (location, "%qE does not have class type",
6648 postfix_expression);
6649 scope = NULL_TREE;
6651 /* Unlike the object expression in other contexts, *this is not
6652 required to be of complete type for purposes of class member
6653 access (5.2.5) outside the member function body. */
6654 else if (postfix_expression != current_class_ref
6655 && !(processing_template_decl && scope == current_class_type))
6656 scope = complete_type_or_else (scope, NULL_TREE);
6657 /* Let the name lookup machinery know that we are processing a
6658 class member access expression. */
6659 parser->context->object_type = scope;
6660 /* If something went wrong, we want to be able to discern that case,
6661 as opposed to the case where there was no SCOPE due to the type
6662 of expression being dependent. */
6663 if (!scope)
6664 scope = error_mark_node;
6665 /* If the SCOPE was erroneous, make the various semantic analysis
6666 functions exit quickly -- and without issuing additional error
6667 messages. */
6668 if (scope == error_mark_node)
6669 postfix_expression = error_mark_node;
6672 /* Assume this expression is not a pseudo-destructor access. */
6673 pseudo_destructor_p = false;
6675 /* If the SCOPE is a scalar type, then, if this is a valid program,
6676 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6677 is type dependent, it can be pseudo-destructor-name or something else.
6678 Try to parse it as pseudo-destructor-name first. */
6679 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6681 tree s;
6682 tree type;
6684 cp_parser_parse_tentatively (parser);
6685 /* Parse the pseudo-destructor-name. */
6686 s = NULL_TREE;
6687 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6688 &s, &type);
6689 if (dependent_p
6690 && (cp_parser_error_occurred (parser)
6691 || !SCALAR_TYPE_P (type)))
6692 cp_parser_abort_tentative_parse (parser);
6693 else if (cp_parser_parse_definitely (parser))
6695 pseudo_destructor_p = true;
6696 postfix_expression
6697 = finish_pseudo_destructor_expr (postfix_expression,
6698 s, type, location);
6702 if (!pseudo_destructor_p)
6704 /* If the SCOPE is not a scalar type, we are looking at an
6705 ordinary class member access expression, rather than a
6706 pseudo-destructor-name. */
6707 bool template_p;
6708 cp_token *token = cp_lexer_peek_token (parser->lexer);
6709 /* Parse the id-expression. */
6710 name = (cp_parser_id_expression
6711 (parser,
6712 cp_parser_optional_template_keyword (parser),
6713 /*check_dependency_p=*/true,
6714 &template_p,
6715 /*declarator_p=*/false,
6716 /*optional_p=*/false));
6717 /* In general, build a SCOPE_REF if the member name is qualified.
6718 However, if the name was not dependent and has already been
6719 resolved; there is no need to build the SCOPE_REF. For example;
6721 struct X { void f(); };
6722 template <typename T> void f(T* t) { t->X::f(); }
6724 Even though "t" is dependent, "X::f" is not and has been resolved
6725 to a BASELINK; there is no need to include scope information. */
6727 /* But we do need to remember that there was an explicit scope for
6728 virtual function calls. */
6729 if (parser->scope)
6730 *idk = CP_ID_KIND_QUALIFIED;
6732 /* If the name is a template-id that names a type, we will get a
6733 TYPE_DECL here. That is invalid code. */
6734 if (TREE_CODE (name) == TYPE_DECL)
6736 error_at (token->location, "invalid use of %qD", name);
6737 postfix_expression = error_mark_node;
6739 else
6741 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6743 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6745 error_at (token->location, "%<%D::%D%> is not a class member",
6746 parser->scope, name);
6747 postfix_expression = error_mark_node;
6749 else
6750 name = build_qualified_name (/*type=*/NULL_TREE,
6751 parser->scope,
6752 name,
6753 template_p);
6754 parser->scope = NULL_TREE;
6755 parser->qualifying_scope = NULL_TREE;
6756 parser->object_scope = NULL_TREE;
6758 if (parser->scope && name && BASELINK_P (name))
6759 adjust_result_of_qualified_name_lookup
6760 (name, parser->scope, scope);
6761 postfix_expression
6762 = finish_class_member_access_expr (postfix_expression, name,
6763 template_p,
6764 tf_warning_or_error);
6768 /* We no longer need to look up names in the scope of the object on
6769 the left-hand side of the `.' or `->' operator. */
6770 parser->context->object_type = NULL_TREE;
6772 /* Outside of offsetof, these operators may not appear in
6773 constant-expressions. */
6774 if (!for_offsetof
6775 && (cp_parser_non_integral_constant_expression
6776 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6777 postfix_expression = error_mark_node;
6779 return postfix_expression;
6782 /* Cache of LITERAL_ZERO_P constants. */
6784 static GTY(()) tree literal_zeros[itk_none];
6786 /* Parse a parenthesized expression-list.
6788 expression-list:
6789 assignment-expression
6790 expression-list, assignment-expression
6792 attribute-list:
6793 expression-list
6794 identifier
6795 identifier, expression-list
6797 CAST_P is true if this expression is the target of a cast.
6799 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6800 argument pack.
6802 Returns a vector of trees. Each element is a representation of an
6803 assignment-expression. NULL is returned if the ( and or ) are
6804 missing. An empty, but allocated, vector is returned on no
6805 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6806 if we are parsing an attribute list for an attribute that wants a
6807 plain identifier argument, normal_attr for an attribute that wants
6808 an expression, or non_attr if we aren't parsing an attribute list. If
6809 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6810 not all of the expressions in the list were constant.
6811 WANT_LITERAL_ZERO_P is true if the caller is interested in
6812 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6813 immediately, this can be removed. */
6815 static vec<tree, va_gc> *
6816 cp_parser_parenthesized_expression_list (cp_parser* parser,
6817 int is_attribute_list,
6818 bool cast_p,
6819 bool allow_expansion_p,
6820 bool *non_constant_p,
6821 bool want_literal_zero_p)
6823 vec<tree, va_gc> *expression_list;
6824 bool fold_expr_p = is_attribute_list != non_attr;
6825 tree identifier = NULL_TREE;
6826 bool saved_greater_than_is_operator_p;
6828 /* Assume all the expressions will be constant. */
6829 if (non_constant_p)
6830 *non_constant_p = false;
6832 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6833 return NULL;
6835 expression_list = make_tree_vector ();
6837 /* Within a parenthesized expression, a `>' token is always
6838 the greater-than operator. */
6839 saved_greater_than_is_operator_p
6840 = parser->greater_than_is_operator_p;
6841 parser->greater_than_is_operator_p = true;
6843 /* Consume expressions until there are no more. */
6844 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6845 while (true)
6847 tree expr;
6849 /* At the beginning of attribute lists, check to see if the
6850 next token is an identifier. */
6851 if (is_attribute_list == id_attr
6852 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6854 cp_token *token;
6856 /* Consume the identifier. */
6857 token = cp_lexer_consume_token (parser->lexer);
6858 /* Save the identifier. */
6859 identifier = token->u.value;
6861 else
6863 bool expr_non_constant_p;
6865 /* Parse the next assignment-expression. */
6866 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6868 /* A braced-init-list. */
6869 cp_lexer_set_source_position (parser->lexer);
6870 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6871 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6872 if (non_constant_p && expr_non_constant_p)
6873 *non_constant_p = true;
6875 else if (non_constant_p)
6877 expr = (cp_parser_constant_expression
6878 (parser, /*allow_non_constant_p=*/true,
6879 &expr_non_constant_p));
6880 if (expr_non_constant_p)
6881 *non_constant_p = true;
6883 else
6885 expr = NULL_TREE;
6886 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6887 switch (tok->type)
6889 case CPP_NUMBER:
6890 case CPP_CHAR:
6891 case CPP_WCHAR:
6892 case CPP_CHAR16:
6893 case CPP_CHAR32:
6894 case CPP_UTF8CHAR:
6895 /* If a parameter is literal zero alone, remember it
6896 for -Wmemset-transposed-args warning. */
6897 if (integer_zerop (tok->u.value)
6898 && !TREE_OVERFLOW (tok->u.value)
6899 && want_literal_zero_p
6900 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6901 == CPP_COMMA
6902 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6903 == CPP_CLOSE_PAREN))
6905 unsigned int i;
6906 for (i = 0; i < itk_none; ++i)
6907 if (TREE_TYPE (tok->u.value) == integer_types[i])
6908 break;
6909 if (i < itk_none && literal_zeros[i])
6910 expr = literal_zeros[i];
6911 else
6913 expr = copy_node (tok->u.value);
6914 LITERAL_ZERO_P (expr) = 1;
6915 if (i < itk_none)
6916 literal_zeros[i] = expr;
6918 /* Consume the 0 token (or '\0', 0LL etc.). */
6919 cp_lexer_consume_token (parser->lexer);
6921 break;
6922 default:
6923 break;
6925 if (expr == NULL_TREE)
6926 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6927 cast_p);
6930 if (fold_expr_p)
6931 expr = instantiate_non_dependent_expr (expr);
6933 /* If we have an ellipsis, then this is an expression
6934 expansion. */
6935 if (allow_expansion_p
6936 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6938 /* Consume the `...'. */
6939 cp_lexer_consume_token (parser->lexer);
6941 /* Build the argument pack. */
6942 expr = make_pack_expansion (expr);
6945 /* Add it to the list. We add error_mark_node
6946 expressions to the list, so that we can still tell if
6947 the correct form for a parenthesized expression-list
6948 is found. That gives better errors. */
6949 vec_safe_push (expression_list, expr);
6951 if (expr == error_mark_node)
6952 goto skip_comma;
6955 /* After the first item, attribute lists look the same as
6956 expression lists. */
6957 is_attribute_list = non_attr;
6959 get_comma:;
6960 /* If the next token isn't a `,', then we are done. */
6961 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6962 break;
6964 /* Otherwise, consume the `,' and keep going. */
6965 cp_lexer_consume_token (parser->lexer);
6968 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6970 int ending;
6972 skip_comma:;
6973 /* We try and resync to an unnested comma, as that will give the
6974 user better diagnostics. */
6975 ending = cp_parser_skip_to_closing_parenthesis (parser,
6976 /*recovering=*/true,
6977 /*or_comma=*/true,
6978 /*consume_paren=*/true);
6979 if (ending < 0)
6980 goto get_comma;
6981 if (!ending)
6983 parser->greater_than_is_operator_p
6984 = saved_greater_than_is_operator_p;
6985 return NULL;
6989 parser->greater_than_is_operator_p
6990 = saved_greater_than_is_operator_p;
6992 if (identifier)
6993 vec_safe_insert (expression_list, 0, identifier);
6995 return expression_list;
6998 /* Parse a pseudo-destructor-name.
7000 pseudo-destructor-name:
7001 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7002 :: [opt] nested-name-specifier template template-id :: ~ type-name
7003 :: [opt] nested-name-specifier [opt] ~ type-name
7005 If either of the first two productions is used, sets *SCOPE to the
7006 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7007 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7008 or ERROR_MARK_NODE if the parse fails. */
7010 static void
7011 cp_parser_pseudo_destructor_name (cp_parser* parser,
7012 tree object,
7013 tree* scope,
7014 tree* type)
7016 bool nested_name_specifier_p;
7018 /* Handle ~auto. */
7019 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7020 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7021 && !type_dependent_expression_p (object))
7023 if (cxx_dialect < cxx14)
7024 pedwarn (input_location, 0,
7025 "%<~auto%> only available with "
7026 "-std=c++14 or -std=gnu++14");
7027 cp_lexer_consume_token (parser->lexer);
7028 cp_lexer_consume_token (parser->lexer);
7029 *scope = NULL_TREE;
7030 *type = TREE_TYPE (object);
7031 return;
7034 /* Assume that things will not work out. */
7035 *type = error_mark_node;
7037 /* Look for the optional `::' operator. */
7038 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7039 /* Look for the optional nested-name-specifier. */
7040 nested_name_specifier_p
7041 = (cp_parser_nested_name_specifier_opt (parser,
7042 /*typename_keyword_p=*/false,
7043 /*check_dependency_p=*/true,
7044 /*type_p=*/false,
7045 /*is_declaration=*/false)
7046 != NULL_TREE);
7047 /* Now, if we saw a nested-name-specifier, we might be doing the
7048 second production. */
7049 if (nested_name_specifier_p
7050 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7052 /* Consume the `template' keyword. */
7053 cp_lexer_consume_token (parser->lexer);
7054 /* Parse the template-id. */
7055 cp_parser_template_id (parser,
7056 /*template_keyword_p=*/true,
7057 /*check_dependency_p=*/false,
7058 class_type,
7059 /*is_declaration=*/true);
7060 /* Look for the `::' token. */
7061 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7063 /* If the next token is not a `~', then there might be some
7064 additional qualification. */
7065 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7067 /* At this point, we're looking for "type-name :: ~". The type-name
7068 must not be a class-name, since this is a pseudo-destructor. So,
7069 it must be either an enum-name, or a typedef-name -- both of which
7070 are just identifiers. So, we peek ahead to check that the "::"
7071 and "~" tokens are present; if they are not, then we can avoid
7072 calling type_name. */
7073 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7074 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7075 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7077 cp_parser_error (parser, "non-scalar type");
7078 return;
7081 /* Look for the type-name. */
7082 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7083 if (*scope == error_mark_node)
7084 return;
7086 /* Look for the `::' token. */
7087 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7089 else
7090 *scope = NULL_TREE;
7092 /* Look for the `~'. */
7093 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7095 /* Once we see the ~, this has to be a pseudo-destructor. */
7096 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7097 cp_parser_commit_to_topmost_tentative_parse (parser);
7099 /* Look for the type-name again. We are not responsible for
7100 checking that it matches the first type-name. */
7101 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7104 /* Parse a unary-expression.
7106 unary-expression:
7107 postfix-expression
7108 ++ cast-expression
7109 -- cast-expression
7110 unary-operator cast-expression
7111 sizeof unary-expression
7112 sizeof ( type-id )
7113 alignof ( type-id ) [C++0x]
7114 new-expression
7115 delete-expression
7117 GNU Extensions:
7119 unary-expression:
7120 __extension__ cast-expression
7121 __alignof__ unary-expression
7122 __alignof__ ( type-id )
7123 alignof unary-expression [C++0x]
7124 __real__ cast-expression
7125 __imag__ cast-expression
7126 && identifier
7127 sizeof ( type-id ) { initializer-list , [opt] }
7128 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7129 __alignof__ ( type-id ) { initializer-list , [opt] }
7131 ADDRESS_P is true iff the unary-expression is appearing as the
7132 operand of the `&' operator. CAST_P is true if this expression is
7133 the target of a cast.
7135 Returns a representation of the expression. */
7137 static tree
7138 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7139 bool address_p, bool cast_p, bool decltype_p)
7141 cp_token *token;
7142 enum tree_code unary_operator;
7144 /* Peek at the next token. */
7145 token = cp_lexer_peek_token (parser->lexer);
7146 /* Some keywords give away the kind of expression. */
7147 if (token->type == CPP_KEYWORD)
7149 enum rid keyword = token->keyword;
7151 switch (keyword)
7153 case RID_ALIGNOF:
7154 case RID_SIZEOF:
7156 tree operand, ret;
7157 enum tree_code op;
7158 location_t first_loc;
7160 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7161 /* Consume the token. */
7162 cp_lexer_consume_token (parser->lexer);
7163 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7164 /* Parse the operand. */
7165 operand = cp_parser_sizeof_operand (parser, keyword);
7167 if (TYPE_P (operand))
7168 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7169 else
7171 /* ISO C++ defines alignof only with types, not with
7172 expressions. So pedwarn if alignof is used with a non-
7173 type expression. However, __alignof__ is ok. */
7174 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7175 pedwarn (token->location, OPT_Wpedantic,
7176 "ISO C++ does not allow %<alignof%> "
7177 "with a non-type");
7179 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7181 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7182 SIZEOF_EXPR with the original operand. */
7183 if (op == SIZEOF_EXPR && ret != error_mark_node)
7185 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7187 if (!processing_template_decl && TYPE_P (operand))
7189 ret = build_min (SIZEOF_EXPR, size_type_node,
7190 build1 (NOP_EXPR, operand,
7191 error_mark_node));
7192 SIZEOF_EXPR_TYPE_P (ret) = 1;
7194 else
7195 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7196 TREE_SIDE_EFFECTS (ret) = 0;
7197 TREE_READONLY (ret) = 1;
7199 SET_EXPR_LOCATION (ret, first_loc);
7201 return ret;
7204 case RID_NEW:
7205 return cp_parser_new_expression (parser);
7207 case RID_DELETE:
7208 return cp_parser_delete_expression (parser);
7210 case RID_EXTENSION:
7212 /* The saved value of the PEDANTIC flag. */
7213 int saved_pedantic;
7214 tree expr;
7216 /* Save away the PEDANTIC flag. */
7217 cp_parser_extension_opt (parser, &saved_pedantic);
7218 /* Parse the cast-expression. */
7219 expr = cp_parser_simple_cast_expression (parser);
7220 /* Restore the PEDANTIC flag. */
7221 pedantic = saved_pedantic;
7223 return expr;
7226 case RID_REALPART:
7227 case RID_IMAGPART:
7229 tree expression;
7231 /* Consume the `__real__' or `__imag__' token. */
7232 cp_lexer_consume_token (parser->lexer);
7233 /* Parse the cast-expression. */
7234 expression = cp_parser_simple_cast_expression (parser);
7235 /* Create the complete representation. */
7236 return build_x_unary_op (token->location,
7237 (keyword == RID_REALPART
7238 ? REALPART_EXPR : IMAGPART_EXPR),
7239 expression,
7240 tf_warning_or_error);
7242 break;
7244 case RID_TRANSACTION_ATOMIC:
7245 case RID_TRANSACTION_RELAXED:
7246 return cp_parser_transaction_expression (parser, keyword);
7248 case RID_NOEXCEPT:
7250 tree expr;
7251 const char *saved_message;
7252 bool saved_integral_constant_expression_p;
7253 bool saved_non_integral_constant_expression_p;
7254 bool saved_greater_than_is_operator_p;
7256 cp_lexer_consume_token (parser->lexer);
7257 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7259 saved_message = parser->type_definition_forbidden_message;
7260 parser->type_definition_forbidden_message
7261 = G_("types may not be defined in %<noexcept%> expressions");
7263 saved_integral_constant_expression_p
7264 = parser->integral_constant_expression_p;
7265 saved_non_integral_constant_expression_p
7266 = parser->non_integral_constant_expression_p;
7267 parser->integral_constant_expression_p = false;
7269 saved_greater_than_is_operator_p
7270 = parser->greater_than_is_operator_p;
7271 parser->greater_than_is_operator_p = true;
7273 ++cp_unevaluated_operand;
7274 ++c_inhibit_evaluation_warnings;
7275 ++cp_noexcept_operand;
7276 expr = cp_parser_expression (parser);
7277 --cp_noexcept_operand;
7278 --c_inhibit_evaluation_warnings;
7279 --cp_unevaluated_operand;
7281 parser->greater_than_is_operator_p
7282 = saved_greater_than_is_operator_p;
7284 parser->integral_constant_expression_p
7285 = saved_integral_constant_expression_p;
7286 parser->non_integral_constant_expression_p
7287 = saved_non_integral_constant_expression_p;
7289 parser->type_definition_forbidden_message = saved_message;
7291 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7292 return finish_noexcept_expr (expr, tf_warning_or_error);
7295 default:
7296 break;
7300 /* Look for the `:: new' and `:: delete', which also signal the
7301 beginning of a new-expression, or delete-expression,
7302 respectively. If the next token is `::', then it might be one of
7303 these. */
7304 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7306 enum rid keyword;
7308 /* See if the token after the `::' is one of the keywords in
7309 which we're interested. */
7310 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7311 /* If it's `new', we have a new-expression. */
7312 if (keyword == RID_NEW)
7313 return cp_parser_new_expression (parser);
7314 /* Similarly, for `delete'. */
7315 else if (keyword == RID_DELETE)
7316 return cp_parser_delete_expression (parser);
7319 /* Look for a unary operator. */
7320 unary_operator = cp_parser_unary_operator (token);
7321 /* The `++' and `--' operators can be handled similarly, even though
7322 they are not technically unary-operators in the grammar. */
7323 if (unary_operator == ERROR_MARK)
7325 if (token->type == CPP_PLUS_PLUS)
7326 unary_operator = PREINCREMENT_EXPR;
7327 else if (token->type == CPP_MINUS_MINUS)
7328 unary_operator = PREDECREMENT_EXPR;
7329 /* Handle the GNU address-of-label extension. */
7330 else if (cp_parser_allow_gnu_extensions_p (parser)
7331 && token->type == CPP_AND_AND)
7333 tree identifier;
7334 tree expression;
7335 location_t loc = token->location;
7337 /* Consume the '&&' token. */
7338 cp_lexer_consume_token (parser->lexer);
7339 /* Look for the identifier. */
7340 identifier = cp_parser_identifier (parser);
7341 /* Create an expression representing the address. */
7342 expression = finish_label_address_expr (identifier, loc);
7343 if (cp_parser_non_integral_constant_expression (parser,
7344 NIC_ADDR_LABEL))
7345 expression = error_mark_node;
7346 return expression;
7349 if (unary_operator != ERROR_MARK)
7351 tree cast_expression;
7352 tree expression = error_mark_node;
7353 non_integral_constant non_constant_p = NIC_NONE;
7354 location_t loc = token->location;
7355 tsubst_flags_t complain = complain_flags (decltype_p);
7357 /* Consume the operator token. */
7358 token = cp_lexer_consume_token (parser->lexer);
7359 /* Parse the cast-expression. */
7360 cast_expression
7361 = cp_parser_cast_expression (parser,
7362 unary_operator == ADDR_EXPR,
7363 /*cast_p=*/false,
7364 /*decltype*/false,
7365 pidk);
7366 /* Now, build an appropriate representation. */
7367 switch (unary_operator)
7369 case INDIRECT_REF:
7370 non_constant_p = NIC_STAR;
7371 expression = build_x_indirect_ref (loc, cast_expression,
7372 RO_UNARY_STAR,
7373 complain);
7374 break;
7376 case ADDR_EXPR:
7377 non_constant_p = NIC_ADDR;
7378 /* Fall through. */
7379 case BIT_NOT_EXPR:
7380 expression = build_x_unary_op (loc, unary_operator,
7381 cast_expression,
7382 complain);
7383 break;
7385 case PREINCREMENT_EXPR:
7386 case PREDECREMENT_EXPR:
7387 non_constant_p = unary_operator == PREINCREMENT_EXPR
7388 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7389 /* Fall through. */
7390 case UNARY_PLUS_EXPR:
7391 case NEGATE_EXPR:
7392 case TRUTH_NOT_EXPR:
7393 expression = finish_unary_op_expr (loc, unary_operator,
7394 cast_expression, complain);
7395 break;
7397 default:
7398 gcc_unreachable ();
7401 if (non_constant_p != NIC_NONE
7402 && cp_parser_non_integral_constant_expression (parser,
7403 non_constant_p))
7404 expression = error_mark_node;
7406 return expression;
7409 return cp_parser_postfix_expression (parser, address_p, cast_p,
7410 /*member_access_only_p=*/false,
7411 decltype_p,
7412 pidk);
7415 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7416 unary-operator, the corresponding tree code is returned. */
7418 static enum tree_code
7419 cp_parser_unary_operator (cp_token* token)
7421 switch (token->type)
7423 case CPP_MULT:
7424 return INDIRECT_REF;
7426 case CPP_AND:
7427 return ADDR_EXPR;
7429 case CPP_PLUS:
7430 return UNARY_PLUS_EXPR;
7432 case CPP_MINUS:
7433 return NEGATE_EXPR;
7435 case CPP_NOT:
7436 return TRUTH_NOT_EXPR;
7438 case CPP_COMPL:
7439 return BIT_NOT_EXPR;
7441 default:
7442 return ERROR_MARK;
7446 /* Parse a new-expression.
7448 new-expression:
7449 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7450 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7452 Returns a representation of the expression. */
7454 static tree
7455 cp_parser_new_expression (cp_parser* parser)
7457 bool global_scope_p;
7458 vec<tree, va_gc> *placement;
7459 tree type;
7460 vec<tree, va_gc> *initializer;
7461 tree nelts = NULL_TREE;
7462 tree ret;
7464 /* Look for the optional `::' operator. */
7465 global_scope_p
7466 = (cp_parser_global_scope_opt (parser,
7467 /*current_scope_valid_p=*/false)
7468 != NULL_TREE);
7469 /* Look for the `new' operator. */
7470 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7471 /* There's no easy way to tell a new-placement from the
7472 `( type-id )' construct. */
7473 cp_parser_parse_tentatively (parser);
7474 /* Look for a new-placement. */
7475 placement = cp_parser_new_placement (parser);
7476 /* If that didn't work out, there's no new-placement. */
7477 if (!cp_parser_parse_definitely (parser))
7479 if (placement != NULL)
7480 release_tree_vector (placement);
7481 placement = NULL;
7484 /* If the next token is a `(', then we have a parenthesized
7485 type-id. */
7486 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7488 cp_token *token;
7489 const char *saved_message = parser->type_definition_forbidden_message;
7491 /* Consume the `('. */
7492 cp_lexer_consume_token (parser->lexer);
7494 /* Parse the type-id. */
7495 parser->type_definition_forbidden_message
7496 = G_("types may not be defined in a new-expression");
7497 type = cp_parser_type_id (parser);
7498 parser->type_definition_forbidden_message = saved_message;
7500 /* Look for the closing `)'. */
7501 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7502 token = cp_lexer_peek_token (parser->lexer);
7503 /* There should not be a direct-new-declarator in this production,
7504 but GCC used to allowed this, so we check and emit a sensible error
7505 message for this case. */
7506 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7508 error_at (token->location,
7509 "array bound forbidden after parenthesized type-id");
7510 inform (token->location,
7511 "try removing the parentheses around the type-id");
7512 cp_parser_direct_new_declarator (parser);
7515 /* Otherwise, there must be a new-type-id. */
7516 else
7517 type = cp_parser_new_type_id (parser, &nelts);
7519 /* If the next token is a `(' or '{', then we have a new-initializer. */
7520 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7521 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7522 initializer = cp_parser_new_initializer (parser);
7523 else
7524 initializer = NULL;
7526 /* A new-expression may not appear in an integral constant
7527 expression. */
7528 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7529 ret = error_mark_node;
7530 else
7532 /* Create a representation of the new-expression. */
7533 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7534 tf_warning_or_error);
7537 if (placement != NULL)
7538 release_tree_vector (placement);
7539 if (initializer != NULL)
7540 release_tree_vector (initializer);
7542 return ret;
7545 /* Parse a new-placement.
7547 new-placement:
7548 ( expression-list )
7550 Returns the same representation as for an expression-list. */
7552 static vec<tree, va_gc> *
7553 cp_parser_new_placement (cp_parser* parser)
7555 vec<tree, va_gc> *expression_list;
7557 /* Parse the expression-list. */
7558 expression_list = (cp_parser_parenthesized_expression_list
7559 (parser, non_attr, /*cast_p=*/false,
7560 /*allow_expansion_p=*/true,
7561 /*non_constant_p=*/NULL));
7563 if (expression_list && expression_list->is_empty ())
7564 error ("expected expression-list or type-id");
7566 return expression_list;
7569 /* Parse a new-type-id.
7571 new-type-id:
7572 type-specifier-seq new-declarator [opt]
7574 Returns the TYPE allocated. If the new-type-id indicates an array
7575 type, *NELTS is set to the number of elements in the last array
7576 bound; the TYPE will not include the last array bound. */
7578 static tree
7579 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7581 cp_decl_specifier_seq type_specifier_seq;
7582 cp_declarator *new_declarator;
7583 cp_declarator *declarator;
7584 cp_declarator *outer_declarator;
7585 const char *saved_message;
7587 /* The type-specifier sequence must not contain type definitions.
7588 (It cannot contain declarations of new types either, but if they
7589 are not definitions we will catch that because they are not
7590 complete.) */
7591 saved_message = parser->type_definition_forbidden_message;
7592 parser->type_definition_forbidden_message
7593 = G_("types may not be defined in a new-type-id");
7594 /* Parse the type-specifier-seq. */
7595 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7596 /*is_trailing_return=*/false,
7597 &type_specifier_seq);
7598 /* Restore the old message. */
7599 parser->type_definition_forbidden_message = saved_message;
7601 if (type_specifier_seq.type == error_mark_node)
7602 return error_mark_node;
7604 /* Parse the new-declarator. */
7605 new_declarator = cp_parser_new_declarator_opt (parser);
7607 /* Determine the number of elements in the last array dimension, if
7608 any. */
7609 *nelts = NULL_TREE;
7610 /* Skip down to the last array dimension. */
7611 declarator = new_declarator;
7612 outer_declarator = NULL;
7613 while (declarator && (declarator->kind == cdk_pointer
7614 || declarator->kind == cdk_ptrmem))
7616 outer_declarator = declarator;
7617 declarator = declarator->declarator;
7619 while (declarator
7620 && declarator->kind == cdk_array
7621 && declarator->declarator
7622 && declarator->declarator->kind == cdk_array)
7624 outer_declarator = declarator;
7625 declarator = declarator->declarator;
7628 if (declarator && declarator->kind == cdk_array)
7630 *nelts = declarator->u.array.bounds;
7631 if (*nelts == error_mark_node)
7632 *nelts = integer_one_node;
7634 if (outer_declarator)
7635 outer_declarator->declarator = declarator->declarator;
7636 else
7637 new_declarator = NULL;
7640 return groktypename (&type_specifier_seq, new_declarator, false);
7643 /* Parse an (optional) new-declarator.
7645 new-declarator:
7646 ptr-operator new-declarator [opt]
7647 direct-new-declarator
7649 Returns the declarator. */
7651 static cp_declarator *
7652 cp_parser_new_declarator_opt (cp_parser* parser)
7654 enum tree_code code;
7655 tree type, std_attributes = NULL_TREE;
7656 cp_cv_quals cv_quals;
7658 /* We don't know if there's a ptr-operator next, or not. */
7659 cp_parser_parse_tentatively (parser);
7660 /* Look for a ptr-operator. */
7661 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7662 /* If that worked, look for more new-declarators. */
7663 if (cp_parser_parse_definitely (parser))
7665 cp_declarator *declarator;
7667 /* Parse another optional declarator. */
7668 declarator = cp_parser_new_declarator_opt (parser);
7670 declarator = cp_parser_make_indirect_declarator
7671 (code, type, cv_quals, declarator, std_attributes);
7673 return declarator;
7676 /* If the next token is a `[', there is a direct-new-declarator. */
7677 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7678 return cp_parser_direct_new_declarator (parser);
7680 return NULL;
7683 /* Parse a direct-new-declarator.
7685 direct-new-declarator:
7686 [ expression ]
7687 direct-new-declarator [constant-expression]
7691 static cp_declarator *
7692 cp_parser_direct_new_declarator (cp_parser* parser)
7694 cp_declarator *declarator = NULL;
7696 while (true)
7698 tree expression;
7699 cp_token *token;
7701 /* Look for the opening `['. */
7702 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7704 token = cp_lexer_peek_token (parser->lexer);
7705 expression = cp_parser_expression (parser);
7706 /* The standard requires that the expression have integral
7707 type. DR 74 adds enumeration types. We believe that the
7708 real intent is that these expressions be handled like the
7709 expression in a `switch' condition, which also allows
7710 classes with a single conversion to integral or
7711 enumeration type. */
7712 if (!processing_template_decl)
7714 expression
7715 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7716 expression,
7717 /*complain=*/true);
7718 if (!expression)
7720 error_at (token->location,
7721 "expression in new-declarator must have integral "
7722 "or enumeration type");
7723 expression = error_mark_node;
7727 /* Look for the closing `]'. */
7728 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7730 /* Add this bound to the declarator. */
7731 declarator = make_array_declarator (declarator, expression);
7733 /* If the next token is not a `[', then there are no more
7734 bounds. */
7735 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7736 break;
7739 return declarator;
7742 /* Parse a new-initializer.
7744 new-initializer:
7745 ( expression-list [opt] )
7746 braced-init-list
7748 Returns a representation of the expression-list. */
7750 static vec<tree, va_gc> *
7751 cp_parser_new_initializer (cp_parser* parser)
7753 vec<tree, va_gc> *expression_list;
7755 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7757 tree t;
7758 bool expr_non_constant_p;
7759 cp_lexer_set_source_position (parser->lexer);
7760 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7761 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7762 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7763 expression_list = make_tree_vector_single (t);
7765 else
7766 expression_list = (cp_parser_parenthesized_expression_list
7767 (parser, non_attr, /*cast_p=*/false,
7768 /*allow_expansion_p=*/true,
7769 /*non_constant_p=*/NULL));
7771 return expression_list;
7774 /* Parse a delete-expression.
7776 delete-expression:
7777 :: [opt] delete cast-expression
7778 :: [opt] delete [ ] cast-expression
7780 Returns a representation of the expression. */
7782 static tree
7783 cp_parser_delete_expression (cp_parser* parser)
7785 bool global_scope_p;
7786 bool array_p;
7787 tree expression;
7789 /* Look for the optional `::' operator. */
7790 global_scope_p
7791 = (cp_parser_global_scope_opt (parser,
7792 /*current_scope_valid_p=*/false)
7793 != NULL_TREE);
7794 /* Look for the `delete' keyword. */
7795 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7796 /* See if the array syntax is in use. */
7797 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7799 /* Consume the `[' token. */
7800 cp_lexer_consume_token (parser->lexer);
7801 /* Look for the `]' token. */
7802 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7803 /* Remember that this is the `[]' construct. */
7804 array_p = true;
7806 else
7807 array_p = false;
7809 /* Parse the cast-expression. */
7810 expression = cp_parser_simple_cast_expression (parser);
7812 /* A delete-expression may not appear in an integral constant
7813 expression. */
7814 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7815 return error_mark_node;
7817 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7818 tf_warning_or_error);
7821 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7822 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7823 0 otherwise. */
7825 static int
7826 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7828 cp_token *token = cp_lexer_peek_token (parser->lexer);
7829 switch (token->type)
7831 case CPP_COMMA:
7832 case CPP_SEMICOLON:
7833 case CPP_QUERY:
7834 case CPP_COLON:
7835 case CPP_CLOSE_SQUARE:
7836 case CPP_CLOSE_PAREN:
7837 case CPP_CLOSE_BRACE:
7838 case CPP_OPEN_BRACE:
7839 case CPP_DOT:
7840 case CPP_DOT_STAR:
7841 case CPP_DEREF:
7842 case CPP_DEREF_STAR:
7843 case CPP_DIV:
7844 case CPP_MOD:
7845 case CPP_LSHIFT:
7846 case CPP_RSHIFT:
7847 case CPP_LESS:
7848 case CPP_GREATER:
7849 case CPP_LESS_EQ:
7850 case CPP_GREATER_EQ:
7851 case CPP_EQ_EQ:
7852 case CPP_NOT_EQ:
7853 case CPP_EQ:
7854 case CPP_MULT_EQ:
7855 case CPP_DIV_EQ:
7856 case CPP_MOD_EQ:
7857 case CPP_PLUS_EQ:
7858 case CPP_MINUS_EQ:
7859 case CPP_RSHIFT_EQ:
7860 case CPP_LSHIFT_EQ:
7861 case CPP_AND_EQ:
7862 case CPP_XOR_EQ:
7863 case CPP_OR_EQ:
7864 case CPP_XOR:
7865 case CPP_OR:
7866 case CPP_OR_OR:
7867 case CPP_EOF:
7868 case CPP_ELLIPSIS:
7869 return 0;
7871 case CPP_OPEN_PAREN:
7872 /* In ((type ()) () the last () isn't a valid cast-expression,
7873 so the whole must be parsed as postfix-expression. */
7874 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7875 != CPP_CLOSE_PAREN;
7877 case CPP_OPEN_SQUARE:
7878 /* '[' may start a primary-expression in obj-c++ and in C++11,
7879 as a lambda-expression, eg, '(void)[]{}'. */
7880 if (cxx_dialect >= cxx11)
7881 return -1;
7882 return c_dialect_objc ();
7884 case CPP_PLUS_PLUS:
7885 case CPP_MINUS_MINUS:
7886 /* '++' and '--' may or may not start a cast-expression:
7888 struct T { void operator++(int); };
7889 void f() { (T())++; }
7893 int a;
7894 (int)++a; */
7895 return -1;
7897 default:
7898 return 1;
7902 /* Parse a cast-expression.
7904 cast-expression:
7905 unary-expression
7906 ( type-id ) cast-expression
7908 ADDRESS_P is true iff the unary-expression is appearing as the
7909 operand of the `&' operator. CAST_P is true if this expression is
7910 the target of a cast.
7912 Returns a representation of the expression. */
7914 static tree
7915 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7916 bool decltype_p, cp_id_kind * pidk)
7918 /* If it's a `(', then we might be looking at a cast. */
7919 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7921 tree type = NULL_TREE;
7922 tree expr = NULL_TREE;
7923 int cast_expression = 0;
7924 const char *saved_message;
7926 /* There's no way to know yet whether or not this is a cast.
7927 For example, `(int (3))' is a unary-expression, while `(int)
7928 3' is a cast. So, we resort to parsing tentatively. */
7929 cp_parser_parse_tentatively (parser);
7930 /* Types may not be defined in a cast. */
7931 saved_message = parser->type_definition_forbidden_message;
7932 parser->type_definition_forbidden_message
7933 = G_("types may not be defined in casts");
7934 /* Consume the `('. */
7935 cp_lexer_consume_token (parser->lexer);
7936 /* A very tricky bit is that `(struct S) { 3 }' is a
7937 compound-literal (which we permit in C++ as an extension).
7938 But, that construct is not a cast-expression -- it is a
7939 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7940 is legal; if the compound-literal were a cast-expression,
7941 you'd need an extra set of parentheses.) But, if we parse
7942 the type-id, and it happens to be a class-specifier, then we
7943 will commit to the parse at that point, because we cannot
7944 undo the action that is done when creating a new class. So,
7945 then we cannot back up and do a postfix-expression.
7947 Another tricky case is the following (c++/29234):
7949 struct S { void operator () (); };
7951 void foo ()
7953 ( S()() );
7956 As a type-id we parse the parenthesized S()() as a function
7957 returning a function, groktypename complains and we cannot
7958 back up in this case either.
7960 Therefore, we scan ahead to the closing `)', and check to see
7961 if the tokens after the `)' can start a cast-expression. Otherwise
7962 we are dealing with an unary-expression, a postfix-expression
7963 or something else.
7965 Yet another tricky case, in C++11, is the following (c++/54891):
7967 (void)[]{};
7969 The issue is that usually, besides the case of lambda-expressions,
7970 the parenthesized type-id cannot be followed by '[', and, eg, we
7971 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7972 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7973 we don't commit, we try a cast-expression, then an unary-expression.
7975 Save tokens so that we can put them back. */
7976 cp_lexer_save_tokens (parser->lexer);
7978 /* We may be looking at a cast-expression. */
7979 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7980 /*consume_paren=*/true))
7981 cast_expression
7982 = cp_parser_tokens_start_cast_expression (parser);
7984 /* Roll back the tokens we skipped. */
7985 cp_lexer_rollback_tokens (parser->lexer);
7986 /* If we aren't looking at a cast-expression, simulate an error so
7987 that the call to cp_parser_error_occurred below returns true. */
7988 if (!cast_expression)
7989 cp_parser_simulate_error (parser);
7990 else
7992 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7993 parser->in_type_id_in_expr_p = true;
7994 /* Look for the type-id. */
7995 type = cp_parser_type_id (parser);
7996 /* Look for the closing `)'. */
7997 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7998 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8001 /* Restore the saved message. */
8002 parser->type_definition_forbidden_message = saved_message;
8004 /* At this point this can only be either a cast or a
8005 parenthesized ctor such as `(T ())' that looks like a cast to
8006 function returning T. */
8007 if (!cp_parser_error_occurred (parser))
8009 /* Only commit if the cast-expression doesn't start with
8010 '++', '--', or '[' in C++11. */
8011 if (cast_expression > 0)
8012 cp_parser_commit_to_topmost_tentative_parse (parser);
8014 expr = cp_parser_cast_expression (parser,
8015 /*address_p=*/false,
8016 /*cast_p=*/true,
8017 /*decltype_p=*/false,
8018 pidk);
8020 if (cp_parser_parse_definitely (parser))
8022 /* Warn about old-style casts, if so requested. */
8023 if (warn_old_style_cast
8024 && !in_system_header_at (input_location)
8025 && !VOID_TYPE_P (type)
8026 && current_lang_name != lang_name_c)
8027 warning (OPT_Wold_style_cast, "use of old-style cast");
8029 /* Only type conversions to integral or enumeration types
8030 can be used in constant-expressions. */
8031 if (!cast_valid_in_integral_constant_expression_p (type)
8032 && cp_parser_non_integral_constant_expression (parser,
8033 NIC_CAST))
8034 return error_mark_node;
8036 /* Perform the cast. */
8037 expr = build_c_cast (input_location, type, expr);
8038 return expr;
8041 else
8042 cp_parser_abort_tentative_parse (parser);
8045 /* If we get here, then it's not a cast, so it must be a
8046 unary-expression. */
8047 return cp_parser_unary_expression (parser, pidk, address_p,
8048 cast_p, decltype_p);
8051 /* Parse a binary expression of the general form:
8053 pm-expression:
8054 cast-expression
8055 pm-expression .* cast-expression
8056 pm-expression ->* cast-expression
8058 multiplicative-expression:
8059 pm-expression
8060 multiplicative-expression * pm-expression
8061 multiplicative-expression / pm-expression
8062 multiplicative-expression % pm-expression
8064 additive-expression:
8065 multiplicative-expression
8066 additive-expression + multiplicative-expression
8067 additive-expression - multiplicative-expression
8069 shift-expression:
8070 additive-expression
8071 shift-expression << additive-expression
8072 shift-expression >> additive-expression
8074 relational-expression:
8075 shift-expression
8076 relational-expression < shift-expression
8077 relational-expression > shift-expression
8078 relational-expression <= shift-expression
8079 relational-expression >= shift-expression
8081 GNU Extension:
8083 relational-expression:
8084 relational-expression <? shift-expression
8085 relational-expression >? shift-expression
8087 equality-expression:
8088 relational-expression
8089 equality-expression == relational-expression
8090 equality-expression != relational-expression
8092 and-expression:
8093 equality-expression
8094 and-expression & equality-expression
8096 exclusive-or-expression:
8097 and-expression
8098 exclusive-or-expression ^ and-expression
8100 inclusive-or-expression:
8101 exclusive-or-expression
8102 inclusive-or-expression | exclusive-or-expression
8104 logical-and-expression:
8105 inclusive-or-expression
8106 logical-and-expression && inclusive-or-expression
8108 logical-or-expression:
8109 logical-and-expression
8110 logical-or-expression || logical-and-expression
8112 All these are implemented with a single function like:
8114 binary-expression:
8115 simple-cast-expression
8116 binary-expression <token> binary-expression
8118 CAST_P is true if this expression is the target of a cast.
8120 The binops_by_token map is used to get the tree codes for each <token> type.
8121 binary-expressions are associated according to a precedence table. */
8123 #define TOKEN_PRECEDENCE(token) \
8124 (((token->type == CPP_GREATER \
8125 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8126 && !parser->greater_than_is_operator_p) \
8127 ? PREC_NOT_OPERATOR \
8128 : binops_by_token[token->type].prec)
8130 static tree
8131 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8132 bool no_toplevel_fold_p,
8133 bool decltype_p,
8134 enum cp_parser_prec prec,
8135 cp_id_kind * pidk)
8137 cp_parser_expression_stack stack;
8138 cp_parser_expression_stack_entry *sp = &stack[0];
8139 cp_parser_expression_stack_entry current;
8140 tree rhs;
8141 cp_token *token;
8142 enum tree_code rhs_type;
8143 enum cp_parser_prec new_prec, lookahead_prec;
8144 tree overload;
8146 /* Parse the first expression. */
8147 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8148 ? TRUTH_NOT_EXPR : ERROR_MARK);
8149 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8150 cast_p, decltype_p, pidk);
8151 current.prec = prec;
8153 if (cp_parser_error_occurred (parser))
8154 return error_mark_node;
8156 for (;;)
8158 /* Get an operator token. */
8159 token = cp_lexer_peek_token (parser->lexer);
8161 if (warn_cxx11_compat
8162 && token->type == CPP_RSHIFT
8163 && !parser->greater_than_is_operator_p)
8165 if (warning_at (token->location, OPT_Wc__11_compat,
8166 "%<>>%> operator is treated"
8167 " as two right angle brackets in C++11"))
8168 inform (token->location,
8169 "suggest parentheses around %<>>%> expression");
8172 new_prec = TOKEN_PRECEDENCE (token);
8174 /* Popping an entry off the stack means we completed a subexpression:
8175 - either we found a token which is not an operator (`>' where it is not
8176 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8177 will happen repeatedly;
8178 - or, we found an operator which has lower priority. This is the case
8179 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8180 parsing `3 * 4'. */
8181 if (new_prec <= current.prec)
8183 if (sp == stack)
8184 break;
8185 else
8186 goto pop;
8189 get_rhs:
8190 current.tree_type = binops_by_token[token->type].tree_type;
8191 current.loc = token->location;
8193 /* We used the operator token. */
8194 cp_lexer_consume_token (parser->lexer);
8196 /* For "false && x" or "true || x", x will never be executed;
8197 disable warnings while evaluating it. */
8198 if (current.tree_type == TRUTH_ANDIF_EXPR)
8199 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8200 else if (current.tree_type == TRUTH_ORIF_EXPR)
8201 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8203 /* Extract another operand. It may be the RHS of this expression
8204 or the LHS of a new, higher priority expression. */
8205 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8206 ? TRUTH_NOT_EXPR : ERROR_MARK);
8207 rhs = cp_parser_simple_cast_expression (parser);
8209 /* Get another operator token. Look up its precedence to avoid
8210 building a useless (immediately popped) stack entry for common
8211 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8212 token = cp_lexer_peek_token (parser->lexer);
8213 lookahead_prec = TOKEN_PRECEDENCE (token);
8214 if (lookahead_prec > new_prec)
8216 /* ... and prepare to parse the RHS of the new, higher priority
8217 expression. Since precedence levels on the stack are
8218 monotonically increasing, we do not have to care about
8219 stack overflows. */
8220 *sp = current;
8221 ++sp;
8222 current.lhs = rhs;
8223 current.lhs_type = rhs_type;
8224 current.prec = new_prec;
8225 new_prec = lookahead_prec;
8226 goto get_rhs;
8228 pop:
8229 lookahead_prec = new_prec;
8230 /* If the stack is not empty, we have parsed into LHS the right side
8231 (`4' in the example above) of an expression we had suspended.
8232 We can use the information on the stack to recover the LHS (`3')
8233 from the stack together with the tree code (`MULT_EXPR'), and
8234 the precedence of the higher level subexpression
8235 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8236 which will be used to actually build the additive expression. */
8237 rhs = current.lhs;
8238 rhs_type = current.lhs_type;
8239 --sp;
8240 current = *sp;
8243 /* Undo the disabling of warnings done above. */
8244 if (current.tree_type == TRUTH_ANDIF_EXPR)
8245 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8246 else if (current.tree_type == TRUTH_ORIF_EXPR)
8247 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8249 if (warn_logical_not_paren
8250 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8251 && current.lhs_type == TRUTH_NOT_EXPR
8252 /* Avoid warning for !!x == y. */
8253 && (TREE_CODE (current.lhs) != NE_EXPR
8254 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8255 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8256 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8257 /* Avoid warning for !b == y where b is boolean. */
8258 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8259 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8260 != BOOLEAN_TYPE))))
8261 /* Avoid warning for !!b == y where b is boolean. */
8262 && (!DECL_P (current.lhs)
8263 || TREE_TYPE (current.lhs) == NULL_TREE
8264 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8265 warn_logical_not_parentheses (current.loc, current.tree_type,
8266 maybe_constant_value (rhs));
8268 overload = NULL;
8269 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8270 ERROR_MARK for everything that is not a binary expression.
8271 This makes warn_about_parentheses miss some warnings that
8272 involve unary operators. For unary expressions we should
8273 pass the correct tree_code unless the unary expression was
8274 surrounded by parentheses.
8276 if (no_toplevel_fold_p
8277 && lookahead_prec <= current.prec
8278 && sp == stack)
8279 current.lhs = build2 (current.tree_type,
8280 TREE_CODE_CLASS (current.tree_type)
8281 == tcc_comparison
8282 ? boolean_type_node : TREE_TYPE (current.lhs),
8283 current.lhs, rhs);
8284 else
8285 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8286 current.lhs, current.lhs_type,
8287 rhs, rhs_type, &overload,
8288 complain_flags (decltype_p));
8289 current.lhs_type = current.tree_type;
8290 if (EXPR_P (current.lhs))
8291 SET_EXPR_LOCATION (current.lhs, current.loc);
8293 /* If the binary operator required the use of an overloaded operator,
8294 then this expression cannot be an integral constant-expression.
8295 An overloaded operator can be used even if both operands are
8296 otherwise permissible in an integral constant-expression if at
8297 least one of the operands is of enumeration type. */
8299 if (overload
8300 && cp_parser_non_integral_constant_expression (parser,
8301 NIC_OVERLOADED))
8302 return error_mark_node;
8305 return current.lhs;
8308 static tree
8309 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8310 bool no_toplevel_fold_p,
8311 enum cp_parser_prec prec,
8312 cp_id_kind * pidk)
8314 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8315 /*decltype*/false, prec, pidk);
8318 /* Parse the `? expression : assignment-expression' part of a
8319 conditional-expression. The LOGICAL_OR_EXPR is the
8320 logical-or-expression that started the conditional-expression.
8321 Returns a representation of the entire conditional-expression.
8323 This routine is used by cp_parser_assignment_expression.
8325 ? expression : assignment-expression
8327 GNU Extensions:
8329 ? : assignment-expression */
8331 static tree
8332 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8334 tree expr;
8335 tree assignment_expr;
8336 struct cp_token *token;
8337 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8339 /* Consume the `?' token. */
8340 cp_lexer_consume_token (parser->lexer);
8341 token = cp_lexer_peek_token (parser->lexer);
8342 if (cp_parser_allow_gnu_extensions_p (parser)
8343 && token->type == CPP_COLON)
8345 pedwarn (token->location, OPT_Wpedantic,
8346 "ISO C++ does not allow ?: with omitted middle operand");
8347 /* Implicit true clause. */
8348 expr = NULL_TREE;
8349 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8350 warn_for_omitted_condop (token->location, logical_or_expr);
8352 else
8354 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8355 parser->colon_corrects_to_scope_p = false;
8356 /* Parse the expression. */
8357 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8358 expr = cp_parser_expression (parser);
8359 c_inhibit_evaluation_warnings +=
8360 ((logical_or_expr == truthvalue_true_node)
8361 - (logical_or_expr == truthvalue_false_node));
8362 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8365 /* The next token should be a `:'. */
8366 cp_parser_require (parser, CPP_COLON, RT_COLON);
8367 /* Parse the assignment-expression. */
8368 assignment_expr = cp_parser_assignment_expression (parser);
8369 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8371 /* Build the conditional-expression. */
8372 return build_x_conditional_expr (loc, logical_or_expr,
8373 expr,
8374 assignment_expr,
8375 tf_warning_or_error);
8378 /* Parse an assignment-expression.
8380 assignment-expression:
8381 conditional-expression
8382 logical-or-expression assignment-operator assignment_expression
8383 throw-expression
8385 CAST_P is true if this expression is the target of a cast.
8386 DECLTYPE_P is true if this expression is the operand of decltype.
8388 Returns a representation for the expression. */
8390 static tree
8391 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8392 bool cast_p, bool decltype_p)
8394 tree expr;
8396 /* If the next token is the `throw' keyword, then we're looking at
8397 a throw-expression. */
8398 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8399 expr = cp_parser_throw_expression (parser);
8400 /* Otherwise, it must be that we are looking at a
8401 logical-or-expression. */
8402 else
8404 /* Parse the binary expressions (logical-or-expression). */
8405 expr = cp_parser_binary_expression (parser, cast_p, false,
8406 decltype_p,
8407 PREC_NOT_OPERATOR, pidk);
8408 /* If the next token is a `?' then we're actually looking at a
8409 conditional-expression. */
8410 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8411 return cp_parser_question_colon_clause (parser, expr);
8412 else
8414 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8416 /* If it's an assignment-operator, we're using the second
8417 production. */
8418 enum tree_code assignment_operator
8419 = cp_parser_assignment_operator_opt (parser);
8420 if (assignment_operator != ERROR_MARK)
8422 bool non_constant_p;
8423 location_t saved_input_location;
8425 /* Parse the right-hand side of the assignment. */
8426 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8428 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8429 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8431 /* An assignment may not appear in a
8432 constant-expression. */
8433 if (cp_parser_non_integral_constant_expression (parser,
8434 NIC_ASSIGNMENT))
8435 return error_mark_node;
8436 /* Build the assignment expression. Its default
8437 location is the location of the '=' token. */
8438 saved_input_location = input_location;
8439 input_location = loc;
8440 expr = build_x_modify_expr (loc, expr,
8441 assignment_operator,
8442 rhs,
8443 complain_flags (decltype_p));
8444 input_location = saved_input_location;
8449 return expr;
8452 /* Parse an (optional) assignment-operator.
8454 assignment-operator: one of
8455 = *= /= %= += -= >>= <<= &= ^= |=
8457 GNU Extension:
8459 assignment-operator: one of
8460 <?= >?=
8462 If the next token is an assignment operator, the corresponding tree
8463 code is returned, and the token is consumed. For example, for
8464 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8465 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8466 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8467 operator, ERROR_MARK is returned. */
8469 static enum tree_code
8470 cp_parser_assignment_operator_opt (cp_parser* parser)
8472 enum tree_code op;
8473 cp_token *token;
8475 /* Peek at the next token. */
8476 token = cp_lexer_peek_token (parser->lexer);
8478 switch (token->type)
8480 case CPP_EQ:
8481 op = NOP_EXPR;
8482 break;
8484 case CPP_MULT_EQ:
8485 op = MULT_EXPR;
8486 break;
8488 case CPP_DIV_EQ:
8489 op = TRUNC_DIV_EXPR;
8490 break;
8492 case CPP_MOD_EQ:
8493 op = TRUNC_MOD_EXPR;
8494 break;
8496 case CPP_PLUS_EQ:
8497 op = PLUS_EXPR;
8498 break;
8500 case CPP_MINUS_EQ:
8501 op = MINUS_EXPR;
8502 break;
8504 case CPP_RSHIFT_EQ:
8505 op = RSHIFT_EXPR;
8506 break;
8508 case CPP_LSHIFT_EQ:
8509 op = LSHIFT_EXPR;
8510 break;
8512 case CPP_AND_EQ:
8513 op = BIT_AND_EXPR;
8514 break;
8516 case CPP_XOR_EQ:
8517 op = BIT_XOR_EXPR;
8518 break;
8520 case CPP_OR_EQ:
8521 op = BIT_IOR_EXPR;
8522 break;
8524 default:
8525 /* Nothing else is an assignment operator. */
8526 op = ERROR_MARK;
8529 /* If it was an assignment operator, consume it. */
8530 if (op != ERROR_MARK)
8531 cp_lexer_consume_token (parser->lexer);
8533 return op;
8536 /* Parse an expression.
8538 expression:
8539 assignment-expression
8540 expression , assignment-expression
8542 CAST_P is true if this expression is the target of a cast.
8543 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8544 except possibly parenthesized or on the RHS of a comma (N3276).
8546 Returns a representation of the expression. */
8548 static tree
8549 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8550 bool cast_p, bool decltype_p)
8552 tree expression = NULL_TREE;
8553 location_t loc = UNKNOWN_LOCATION;
8555 while (true)
8557 tree assignment_expression;
8559 /* Parse the next assignment-expression. */
8560 assignment_expression
8561 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8563 /* We don't create a temporary for a call that is the immediate operand
8564 of decltype or on the RHS of a comma. But when we see a comma, we
8565 need to create a temporary for a call on the LHS. */
8566 if (decltype_p && !processing_template_decl
8567 && TREE_CODE (assignment_expression) == CALL_EXPR
8568 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8569 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8570 assignment_expression
8571 = build_cplus_new (TREE_TYPE (assignment_expression),
8572 assignment_expression, tf_warning_or_error);
8574 /* If this is the first assignment-expression, we can just
8575 save it away. */
8576 if (!expression)
8577 expression = assignment_expression;
8578 else
8579 expression = build_x_compound_expr (loc, expression,
8580 assignment_expression,
8581 complain_flags (decltype_p));
8582 /* If the next token is not a comma, then we are done with the
8583 expression. */
8584 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8585 break;
8586 /* Consume the `,'. */
8587 loc = cp_lexer_peek_token (parser->lexer)->location;
8588 cp_lexer_consume_token (parser->lexer);
8589 /* A comma operator cannot appear in a constant-expression. */
8590 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8591 expression = error_mark_node;
8594 return expression;
8597 /* Parse a constant-expression.
8599 constant-expression:
8600 conditional-expression
8602 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8603 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8604 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8605 is false, NON_CONSTANT_P should be NULL. */
8607 static tree
8608 cp_parser_constant_expression (cp_parser* parser,
8609 bool allow_non_constant_p,
8610 bool *non_constant_p)
8612 bool saved_integral_constant_expression_p;
8613 bool saved_allow_non_integral_constant_expression_p;
8614 bool saved_non_integral_constant_expression_p;
8615 tree expression;
8617 /* It might seem that we could simply parse the
8618 conditional-expression, and then check to see if it were
8619 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8620 one that the compiler can figure out is constant, possibly after
8621 doing some simplifications or optimizations. The standard has a
8622 precise definition of constant-expression, and we must honor
8623 that, even though it is somewhat more restrictive.
8625 For example:
8627 int i[(2, 3)];
8629 is not a legal declaration, because `(2, 3)' is not a
8630 constant-expression. The `,' operator is forbidden in a
8631 constant-expression. However, GCC's constant-folding machinery
8632 will fold this operation to an INTEGER_CST for `3'. */
8634 /* Save the old settings. */
8635 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8636 saved_allow_non_integral_constant_expression_p
8637 = parser->allow_non_integral_constant_expression_p;
8638 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8639 /* We are now parsing a constant-expression. */
8640 parser->integral_constant_expression_p = true;
8641 parser->allow_non_integral_constant_expression_p
8642 = (allow_non_constant_p || cxx_dialect >= cxx11);
8643 parser->non_integral_constant_expression_p = false;
8644 /* Although the grammar says "conditional-expression", we parse an
8645 "assignment-expression", which also permits "throw-expression"
8646 and the use of assignment operators. In the case that
8647 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8648 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8649 actually essential that we look for an assignment-expression.
8650 For example, cp_parser_initializer_clauses uses this function to
8651 determine whether a particular assignment-expression is in fact
8652 constant. */
8653 expression = cp_parser_assignment_expression (parser);
8654 /* Restore the old settings. */
8655 parser->integral_constant_expression_p
8656 = saved_integral_constant_expression_p;
8657 parser->allow_non_integral_constant_expression_p
8658 = saved_allow_non_integral_constant_expression_p;
8659 if (cxx_dialect >= cxx11)
8661 /* Require an rvalue constant expression here; that's what our
8662 callers expect. Reference constant expressions are handled
8663 separately in e.g. cp_parser_template_argument. */
8664 bool is_const = potential_rvalue_constant_expression (expression);
8665 parser->non_integral_constant_expression_p = !is_const;
8666 if (!is_const && !allow_non_constant_p)
8667 require_potential_rvalue_constant_expression (expression);
8669 if (allow_non_constant_p)
8670 *non_constant_p = parser->non_integral_constant_expression_p;
8671 parser->non_integral_constant_expression_p
8672 = saved_non_integral_constant_expression_p;
8674 return expression;
8677 /* Parse __builtin_offsetof.
8679 offsetof-expression:
8680 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8682 offsetof-member-designator:
8683 id-expression
8684 | offsetof-member-designator "." id-expression
8685 | offsetof-member-designator "[" expression "]"
8686 | offsetof-member-designator "->" id-expression */
8688 static tree
8689 cp_parser_builtin_offsetof (cp_parser *parser)
8691 int save_ice_p, save_non_ice_p;
8692 tree type, expr;
8693 cp_id_kind dummy;
8694 cp_token *token;
8696 /* We're about to accept non-integral-constant things, but will
8697 definitely yield an integral constant expression. Save and
8698 restore these values around our local parsing. */
8699 save_ice_p = parser->integral_constant_expression_p;
8700 save_non_ice_p = parser->non_integral_constant_expression_p;
8702 /* Consume the "__builtin_offsetof" token. */
8703 cp_lexer_consume_token (parser->lexer);
8704 /* Consume the opening `('. */
8705 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8706 /* Parse the type-id. */
8707 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8708 type = cp_parser_type_id (parser);
8709 /* Look for the `,'. */
8710 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8711 token = cp_lexer_peek_token (parser->lexer);
8713 /* Build the (type *)null that begins the traditional offsetof macro. */
8714 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8715 tf_warning_or_error);
8717 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8718 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8719 true, &dummy, token->location);
8720 while (true)
8722 token = cp_lexer_peek_token (parser->lexer);
8723 switch (token->type)
8725 case CPP_OPEN_SQUARE:
8726 /* offsetof-member-designator "[" expression "]" */
8727 expr = cp_parser_postfix_open_square_expression (parser, expr,
8728 true, false);
8729 break;
8731 case CPP_DEREF:
8732 /* offsetof-member-designator "->" identifier */
8733 expr = grok_array_decl (token->location, expr,
8734 integer_zero_node, false);
8735 /* FALLTHRU */
8737 case CPP_DOT:
8738 /* offsetof-member-designator "." identifier */
8739 cp_lexer_consume_token (parser->lexer);
8740 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8741 expr, true, &dummy,
8742 token->location);
8743 break;
8745 case CPP_CLOSE_PAREN:
8746 /* Consume the ")" token. */
8747 cp_lexer_consume_token (parser->lexer);
8748 goto success;
8750 default:
8751 /* Error. We know the following require will fail, but
8752 that gives the proper error message. */
8753 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8754 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8755 expr = error_mark_node;
8756 goto failure;
8760 success:
8761 expr = finish_offsetof (expr, loc);
8763 failure:
8764 parser->integral_constant_expression_p = save_ice_p;
8765 parser->non_integral_constant_expression_p = save_non_ice_p;
8767 return expr;
8770 /* Parse a trait expression.
8772 Returns a representation of the expression, the underlying type
8773 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8775 static tree
8776 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8778 cp_trait_kind kind;
8779 tree type1, type2 = NULL_TREE;
8780 bool binary = false;
8781 bool variadic = false;
8783 switch (keyword)
8785 case RID_HAS_NOTHROW_ASSIGN:
8786 kind = CPTK_HAS_NOTHROW_ASSIGN;
8787 break;
8788 case RID_HAS_NOTHROW_CONSTRUCTOR:
8789 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8790 break;
8791 case RID_HAS_NOTHROW_COPY:
8792 kind = CPTK_HAS_NOTHROW_COPY;
8793 break;
8794 case RID_HAS_TRIVIAL_ASSIGN:
8795 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8796 break;
8797 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8798 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8799 break;
8800 case RID_HAS_TRIVIAL_COPY:
8801 kind = CPTK_HAS_TRIVIAL_COPY;
8802 break;
8803 case RID_HAS_TRIVIAL_DESTRUCTOR:
8804 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8805 break;
8806 case RID_HAS_VIRTUAL_DESTRUCTOR:
8807 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8808 break;
8809 case RID_IS_ABSTRACT:
8810 kind = CPTK_IS_ABSTRACT;
8811 break;
8812 case RID_IS_BASE_OF:
8813 kind = CPTK_IS_BASE_OF;
8814 binary = true;
8815 break;
8816 case RID_IS_CLASS:
8817 kind = CPTK_IS_CLASS;
8818 break;
8819 case RID_IS_EMPTY:
8820 kind = CPTK_IS_EMPTY;
8821 break;
8822 case RID_IS_ENUM:
8823 kind = CPTK_IS_ENUM;
8824 break;
8825 case RID_IS_FINAL:
8826 kind = CPTK_IS_FINAL;
8827 break;
8828 case RID_IS_LITERAL_TYPE:
8829 kind = CPTK_IS_LITERAL_TYPE;
8830 break;
8831 case RID_IS_POD:
8832 kind = CPTK_IS_POD;
8833 break;
8834 case RID_IS_POLYMORPHIC:
8835 kind = CPTK_IS_POLYMORPHIC;
8836 break;
8837 case RID_IS_STD_LAYOUT:
8838 kind = CPTK_IS_STD_LAYOUT;
8839 break;
8840 case RID_IS_TRIVIAL:
8841 kind = CPTK_IS_TRIVIAL;
8842 break;
8843 case RID_IS_TRIVIALLY_ASSIGNABLE:
8844 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8845 binary = true;
8846 break;
8847 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8848 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8849 variadic = true;
8850 break;
8851 case RID_IS_TRIVIALLY_COPYABLE:
8852 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8853 break;
8854 case RID_IS_UNION:
8855 kind = CPTK_IS_UNION;
8856 break;
8857 case RID_UNDERLYING_TYPE:
8858 kind = CPTK_UNDERLYING_TYPE;
8859 break;
8860 case RID_BASES:
8861 kind = CPTK_BASES;
8862 break;
8863 case RID_DIRECT_BASES:
8864 kind = CPTK_DIRECT_BASES;
8865 break;
8866 default:
8867 gcc_unreachable ();
8870 /* Consume the token. */
8871 cp_lexer_consume_token (parser->lexer);
8873 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8875 type1 = cp_parser_type_id (parser);
8877 if (type1 == error_mark_node)
8878 return error_mark_node;
8880 if (binary)
8882 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8884 type2 = cp_parser_type_id (parser);
8886 if (type2 == error_mark_node)
8887 return error_mark_node;
8889 else if (variadic)
8891 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8893 cp_lexer_consume_token (parser->lexer);
8894 tree elt = cp_parser_type_id (parser);
8895 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8897 cp_lexer_consume_token (parser->lexer);
8898 elt = make_pack_expansion (elt);
8900 if (elt == error_mark_node)
8901 return error_mark_node;
8902 type2 = tree_cons (NULL_TREE, elt, type2);
8906 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8908 /* Complete the trait expression, which may mean either processing
8909 the trait expr now or saving it for template instantiation. */
8910 switch(kind)
8912 case CPTK_UNDERLYING_TYPE:
8913 return finish_underlying_type (type1);
8914 case CPTK_BASES:
8915 return finish_bases (type1, false);
8916 case CPTK_DIRECT_BASES:
8917 return finish_bases (type1, true);
8918 default:
8919 return finish_trait_expr (kind, type1, type2);
8923 /* Lambdas that appear in variable initializer or default argument scope
8924 get that in their mangling, so we need to record it. We might as well
8925 use the count for function and namespace scopes as well. */
8926 static GTY(()) tree lambda_scope;
8927 static GTY(()) int lambda_count;
8928 typedef struct GTY(()) tree_int
8930 tree t;
8931 int i;
8932 } tree_int;
8933 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8935 static void
8936 start_lambda_scope (tree decl)
8938 tree_int ti;
8939 gcc_assert (decl);
8940 /* Once we're inside a function, we ignore other scopes and just push
8941 the function again so that popping works properly. */
8942 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8943 decl = current_function_decl;
8944 ti.t = lambda_scope;
8945 ti.i = lambda_count;
8946 vec_safe_push (lambda_scope_stack, ti);
8947 if (lambda_scope != decl)
8949 /* Don't reset the count if we're still in the same function. */
8950 lambda_scope = decl;
8951 lambda_count = 0;
8955 static void
8956 record_lambda_scope (tree lambda)
8958 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8959 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8962 static void
8963 finish_lambda_scope (void)
8965 tree_int *p = &lambda_scope_stack->last ();
8966 if (lambda_scope != p->t)
8968 lambda_scope = p->t;
8969 lambda_count = p->i;
8971 lambda_scope_stack->pop ();
8974 /* Parse a lambda expression.
8976 lambda-expression:
8977 lambda-introducer lambda-declarator [opt] compound-statement
8979 Returns a representation of the expression. */
8981 static tree
8982 cp_parser_lambda_expression (cp_parser* parser)
8984 tree lambda_expr = build_lambda_expr ();
8985 tree type;
8986 bool ok = true;
8987 cp_token *token = cp_lexer_peek_token (parser->lexer);
8988 cp_token_position start = 0;
8990 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8992 if (cp_unevaluated_operand)
8994 if (!token->error_reported)
8996 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8997 "lambda-expression in unevaluated context");
8998 token->error_reported = true;
9000 ok = false;
9002 else if (parser->in_template_argument_list_p)
9004 if (!token->error_reported)
9006 error_at (token->location, "lambda-expression in template-argument");
9007 token->error_reported = true;
9009 ok = false;
9012 /* We may be in the middle of deferred access check. Disable
9013 it now. */
9014 push_deferring_access_checks (dk_no_deferred);
9016 cp_parser_lambda_introducer (parser, lambda_expr);
9018 type = begin_lambda_type (lambda_expr);
9019 if (type == error_mark_node)
9020 return error_mark_node;
9022 record_lambda_scope (lambda_expr);
9024 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9025 determine_visibility (TYPE_NAME (type));
9027 /* Now that we've started the type, add the capture fields for any
9028 explicit captures. */
9029 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9032 /* Inside the class, surrounding template-parameter-lists do not apply. */
9033 unsigned int saved_num_template_parameter_lists
9034 = parser->num_template_parameter_lists;
9035 unsigned char in_statement = parser->in_statement;
9036 bool in_switch_statement_p = parser->in_switch_statement_p;
9037 bool fully_implicit_function_template_p
9038 = parser->fully_implicit_function_template_p;
9039 tree implicit_template_parms = parser->implicit_template_parms;
9040 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9041 bool auto_is_implicit_function_template_parm_p
9042 = parser->auto_is_implicit_function_template_parm_p;
9044 parser->num_template_parameter_lists = 0;
9045 parser->in_statement = 0;
9046 parser->in_switch_statement_p = false;
9047 parser->fully_implicit_function_template_p = false;
9048 parser->implicit_template_parms = 0;
9049 parser->implicit_template_scope = 0;
9050 parser->auto_is_implicit_function_template_parm_p = false;
9052 /* By virtue of defining a local class, a lambda expression has access to
9053 the private variables of enclosing classes. */
9055 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9057 if (ok)
9059 if (!cp_parser_error_occurred (parser)
9060 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9061 && cp_parser_start_tentative_firewall (parser))
9062 start = token;
9063 cp_parser_lambda_body (parser, lambda_expr);
9065 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9067 if (cp_parser_skip_to_closing_brace (parser))
9068 cp_lexer_consume_token (parser->lexer);
9071 /* The capture list was built up in reverse order; fix that now. */
9072 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9073 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9075 if (ok)
9076 maybe_add_lambda_conv_op (type);
9078 type = finish_struct (type, /*attributes=*/NULL_TREE);
9080 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9081 parser->in_statement = in_statement;
9082 parser->in_switch_statement_p = in_switch_statement_p;
9083 parser->fully_implicit_function_template_p
9084 = fully_implicit_function_template_p;
9085 parser->implicit_template_parms = implicit_template_parms;
9086 parser->implicit_template_scope = implicit_template_scope;
9087 parser->auto_is_implicit_function_template_parm_p
9088 = auto_is_implicit_function_template_parm_p;
9091 pop_deferring_access_checks ();
9093 /* This field is only used during parsing of the lambda. */
9094 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9096 /* This lambda shouldn't have any proxies left at this point. */
9097 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9098 /* And now that we're done, push proxies for an enclosing lambda. */
9099 insert_pending_capture_proxies ();
9101 if (ok)
9102 lambda_expr = build_lambda_object (lambda_expr);
9103 else
9104 lambda_expr = error_mark_node;
9106 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9108 return lambda_expr;
9111 /* Parse the beginning of a lambda expression.
9113 lambda-introducer:
9114 [ lambda-capture [opt] ]
9116 LAMBDA_EXPR is the current representation of the lambda expression. */
9118 static void
9119 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9121 /* Need commas after the first capture. */
9122 bool first = true;
9124 /* Eat the leading `['. */
9125 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9127 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9128 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9129 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9130 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9131 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9132 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9134 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9136 cp_lexer_consume_token (parser->lexer);
9137 first = false;
9140 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9142 cp_token* capture_token;
9143 tree capture_id;
9144 tree capture_init_expr;
9145 cp_id_kind idk = CP_ID_KIND_NONE;
9146 bool explicit_init_p = false;
9148 enum capture_kind_type
9150 BY_COPY,
9151 BY_REFERENCE
9153 enum capture_kind_type capture_kind = BY_COPY;
9155 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9157 error ("expected end of capture-list");
9158 return;
9161 if (first)
9162 first = false;
9163 else
9164 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9166 /* Possibly capture `this'. */
9167 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9169 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9170 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9171 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9172 "with by-copy capture default");
9173 cp_lexer_consume_token (parser->lexer);
9174 add_capture (lambda_expr,
9175 /*id=*/this_identifier,
9176 /*initializer=*/finish_this_expr(),
9177 /*by_reference_p=*/false,
9178 explicit_init_p);
9179 continue;
9182 /* Remember whether we want to capture as a reference or not. */
9183 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9185 capture_kind = BY_REFERENCE;
9186 cp_lexer_consume_token (parser->lexer);
9189 /* Get the identifier. */
9190 capture_token = cp_lexer_peek_token (parser->lexer);
9191 capture_id = cp_parser_identifier (parser);
9193 if (capture_id == error_mark_node)
9194 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9195 delimiters, but I modified this to stop on unnested ']' as well. It
9196 was already changed to stop on unnested '}', so the
9197 "closing_parenthesis" name is no more misleading with my change. */
9199 cp_parser_skip_to_closing_parenthesis (parser,
9200 /*recovering=*/true,
9201 /*or_comma=*/true,
9202 /*consume_paren=*/true);
9203 break;
9206 /* Find the initializer for this capture. */
9207 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9208 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9209 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9211 bool direct, non_constant;
9212 /* An explicit initializer exists. */
9213 if (cxx_dialect < cxx14)
9214 pedwarn (input_location, 0,
9215 "lambda capture initializers "
9216 "only available with -std=c++14 or -std=gnu++14");
9217 capture_init_expr = cp_parser_initializer (parser, &direct,
9218 &non_constant);
9219 explicit_init_p = true;
9220 if (capture_init_expr == NULL_TREE)
9222 error ("empty initializer for lambda init-capture");
9223 capture_init_expr = error_mark_node;
9226 else
9228 const char* error_msg;
9230 /* Turn the identifier into an id-expression. */
9231 capture_init_expr
9232 = cp_parser_lookup_name_simple (parser, capture_id,
9233 capture_token->location);
9235 if (capture_init_expr == error_mark_node)
9237 unqualified_name_lookup_error (capture_id);
9238 continue;
9240 else if (DECL_P (capture_init_expr)
9241 && (!VAR_P (capture_init_expr)
9242 && TREE_CODE (capture_init_expr) != PARM_DECL))
9244 error_at (capture_token->location,
9245 "capture of non-variable %qD ",
9246 capture_init_expr);
9247 inform (0, "%q+#D declared here", capture_init_expr);
9248 continue;
9250 if (VAR_P (capture_init_expr)
9251 && decl_storage_duration (capture_init_expr) != dk_auto)
9253 if (pedwarn (capture_token->location, 0, "capture of variable "
9254 "%qD with non-automatic storage duration",
9255 capture_init_expr))
9256 inform (0, "%q+#D declared here", capture_init_expr);
9257 continue;
9260 capture_init_expr
9261 = finish_id_expression
9262 (capture_id,
9263 capture_init_expr,
9264 parser->scope,
9265 &idk,
9266 /*integral_constant_expression_p=*/false,
9267 /*allow_non_integral_constant_expression_p=*/false,
9268 /*non_integral_constant_expression_p=*/NULL,
9269 /*template_p=*/false,
9270 /*done=*/true,
9271 /*address_p=*/false,
9272 /*template_arg_p=*/false,
9273 &error_msg,
9274 capture_token->location);
9276 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9278 cp_lexer_consume_token (parser->lexer);
9279 capture_init_expr = make_pack_expansion (capture_init_expr);
9281 else
9282 check_for_bare_parameter_packs (capture_init_expr);
9285 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9286 && !explicit_init_p)
9288 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9289 && capture_kind == BY_COPY)
9290 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9291 "of %qD redundant with by-copy capture default",
9292 capture_id);
9293 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9294 && capture_kind == BY_REFERENCE)
9295 pedwarn (capture_token->location, 0, "explicit by-reference "
9296 "capture of %qD redundant with by-reference capture "
9297 "default", capture_id);
9300 add_capture (lambda_expr,
9301 capture_id,
9302 capture_init_expr,
9303 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9304 explicit_init_p);
9307 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9310 /* Parse the (optional) middle of a lambda expression.
9312 lambda-declarator:
9313 < template-parameter-list [opt] >
9314 ( parameter-declaration-clause [opt] )
9315 attribute-specifier [opt]
9316 mutable [opt]
9317 exception-specification [opt]
9318 lambda-return-type-clause [opt]
9320 LAMBDA_EXPR is the current representation of the lambda expression. */
9322 static bool
9323 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9325 /* 5.1.1.4 of the standard says:
9326 If a lambda-expression does not include a lambda-declarator, it is as if
9327 the lambda-declarator were ().
9328 This means an empty parameter list, no attributes, and no exception
9329 specification. */
9330 tree param_list = void_list_node;
9331 tree attributes = NULL_TREE;
9332 tree exception_spec = NULL_TREE;
9333 tree template_param_list = NULL_TREE;
9335 /* The template-parameter-list is optional, but must begin with
9336 an opening angle if present. */
9337 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9339 if (cxx_dialect < cxx14)
9340 pedwarn (parser->lexer->next_token->location, 0,
9341 "lambda templates are only available with "
9342 "-std=c++14 or -std=gnu++14");
9344 cp_lexer_consume_token (parser->lexer);
9346 template_param_list = cp_parser_template_parameter_list (parser);
9348 cp_parser_skip_to_end_of_template_parameter_list (parser);
9350 /* We just processed one more parameter list. */
9351 ++parser->num_template_parameter_lists;
9354 /* The parameter-declaration-clause is optional (unless
9355 template-parameter-list was given), but must begin with an
9356 opening parenthesis if present. */
9357 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9359 cp_lexer_consume_token (parser->lexer);
9361 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9363 /* Parse parameters. */
9364 param_list = cp_parser_parameter_declaration_clause (parser);
9366 /* Default arguments shall not be specified in the
9367 parameter-declaration-clause of a lambda-declarator. */
9368 for (tree t = param_list; t; t = TREE_CHAIN (t))
9369 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9370 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9371 "default argument specified for lambda parameter");
9373 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9375 attributes = cp_parser_attributes_opt (parser);
9377 /* Parse optional `mutable' keyword. */
9378 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9380 cp_lexer_consume_token (parser->lexer);
9381 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9384 /* Parse optional exception specification. */
9385 exception_spec = cp_parser_exception_specification_opt (parser);
9387 /* Parse optional trailing return type. */
9388 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9390 cp_lexer_consume_token (parser->lexer);
9391 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9392 = cp_parser_trailing_type_id (parser);
9395 /* The function parameters must be in scope all the way until after the
9396 trailing-return-type in case of decltype. */
9397 pop_bindings_and_leave_scope ();
9399 else if (template_param_list != NULL_TREE) // generate diagnostic
9400 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9402 /* Create the function call operator.
9404 Messing with declarators like this is no uglier than building up the
9405 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9406 other code. */
9408 cp_decl_specifier_seq return_type_specs;
9409 cp_declarator* declarator;
9410 tree fco;
9411 int quals;
9412 void *p;
9414 clear_decl_specs (&return_type_specs);
9415 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9416 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9417 else
9418 /* Maybe we will deduce the return type later. */
9419 return_type_specs.type = make_auto ();
9421 p = obstack_alloc (&declarator_obstack, 0);
9423 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9424 sfk_none);
9426 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9427 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9428 declarator = make_call_declarator (declarator, param_list, quals,
9429 VIRT_SPEC_UNSPECIFIED,
9430 REF_QUAL_NONE,
9431 exception_spec,
9432 /*late_return_type=*/NULL_TREE);
9433 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9435 fco = grokmethod (&return_type_specs,
9436 declarator,
9437 attributes);
9438 if (fco != error_mark_node)
9440 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9441 DECL_ARTIFICIAL (fco) = 1;
9442 /* Give the object parameter a different name. */
9443 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9444 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9445 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9447 if (template_param_list)
9449 fco = finish_member_template_decl (fco);
9450 finish_template_decl (template_param_list);
9451 --parser->num_template_parameter_lists;
9453 else if (parser->fully_implicit_function_template_p)
9454 fco = finish_fully_implicit_template (parser, fco);
9456 finish_member_declaration (fco);
9458 obstack_free (&declarator_obstack, p);
9460 return (fco != error_mark_node);
9464 /* Parse the body of a lambda expression, which is simply
9466 compound-statement
9468 but which requires special handling.
9469 LAMBDA_EXPR is the current representation of the lambda expression. */
9471 static void
9472 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9474 bool nested = (current_function_decl != NULL_TREE);
9475 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9476 if (nested)
9477 push_function_context ();
9478 else
9479 /* Still increment function_depth so that we don't GC in the
9480 middle of an expression. */
9481 ++function_depth;
9482 /* Clear this in case we're in the middle of a default argument. */
9483 parser->local_variables_forbidden_p = false;
9485 /* Finish the function call operator
9486 - class_specifier
9487 + late_parsing_for_member
9488 + function_definition_after_declarator
9489 + ctor_initializer_opt_and_function_body */
9491 tree fco = lambda_function (lambda_expr);
9492 tree body;
9493 bool done = false;
9494 tree compound_stmt;
9495 tree cap;
9497 /* Let the front end know that we are going to be defining this
9498 function. */
9499 start_preparsed_function (fco,
9500 NULL_TREE,
9501 SF_PRE_PARSED | SF_INCLASS_INLINE);
9503 start_lambda_scope (fco);
9504 body = begin_function_body ();
9506 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9507 goto out;
9509 /* Push the proxies for any explicit captures. */
9510 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9511 cap = TREE_CHAIN (cap))
9512 build_capture_proxy (TREE_PURPOSE (cap));
9514 compound_stmt = begin_compound_stmt (0);
9516 /* 5.1.1.4 of the standard says:
9517 If a lambda-expression does not include a trailing-return-type, it
9518 is as if the trailing-return-type denotes the following type:
9519 * if the compound-statement is of the form
9520 { return attribute-specifier [opt] expression ; }
9521 the type of the returned expression after lvalue-to-rvalue
9522 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9523 (_conv.array_ 4.2), and function-to-pointer conversion
9524 (_conv.func_ 4.3);
9525 * otherwise, void. */
9527 /* In a lambda that has neither a lambda-return-type-clause
9528 nor a deducible form, errors should be reported for return statements
9529 in the body. Since we used void as the placeholder return type, parsing
9530 the body as usual will give such desired behavior. */
9531 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9532 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9533 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9535 tree expr = NULL_TREE;
9536 cp_id_kind idk = CP_ID_KIND_NONE;
9538 /* Parse tentatively in case there's more after the initial return
9539 statement. */
9540 cp_parser_parse_tentatively (parser);
9542 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9544 expr = cp_parser_expression (parser, &idk);
9546 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9547 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9549 if (cp_parser_parse_definitely (parser))
9551 if (!processing_template_decl)
9552 apply_deduced_return_type (fco, lambda_return_type (expr));
9554 /* Will get error here if type not deduced yet. */
9555 finish_return_stmt (expr);
9557 done = true;
9561 if (!done)
9563 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9564 cp_parser_label_declaration (parser);
9565 cp_parser_statement_seq_opt (parser, NULL_TREE);
9566 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9569 finish_compound_stmt (compound_stmt);
9571 out:
9572 finish_function_body (body);
9573 finish_lambda_scope ();
9575 /* Finish the function and generate code for it if necessary. */
9576 tree fn = finish_function (/*inline*/2);
9578 /* Only expand if the call op is not a template. */
9579 if (!DECL_TEMPLATE_INFO (fco))
9580 expand_or_defer_fn (fn);
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 default argument for a type template-parameter.
13337 Note that diagnostics are handled in cp_parser_template_parameter. */
13339 static tree
13340 cp_parser_default_type_template_argument (cp_parser *parser)
13342 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
13344 /* Consume the `=' token. */
13345 cp_lexer_consume_token (parser->lexer);
13347 /* Parse the default-argument. */
13348 push_deferring_access_checks (dk_no_deferred);
13349 tree default_argument = cp_parser_type_id (parser);
13350 pop_deferring_access_checks ();
13352 return default_argument;
13355 /* Parse a default argument for a template template-parameter. */
13357 static tree
13358 cp_parser_default_template_template_argument (cp_parser *parser)
13360 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
13362 bool is_template;
13364 /* Consume the `='. */
13365 cp_lexer_consume_token (parser->lexer);
13366 /* Parse the id-expression. */
13367 push_deferring_access_checks (dk_no_deferred);
13368 /* save token before parsing the id-expression, for error
13369 reporting */
13370 const cp_token* token = cp_lexer_peek_token (parser->lexer);
13371 tree default_argument
13372 = cp_parser_id_expression (parser,
13373 /*template_keyword_p=*/false,
13374 /*check_dependency_p=*/true,
13375 /*template_p=*/&is_template,
13376 /*declarator_p=*/false,
13377 /*optional_p=*/false);
13378 if (TREE_CODE (default_argument) == TYPE_DECL)
13379 /* If the id-expression was a template-id that refers to
13380 a template-class, we already have the declaration here,
13381 so no further lookup is needed. */
13383 else
13384 /* Look up the name. */
13385 default_argument
13386 = cp_parser_lookup_name (parser, default_argument,
13387 none_type,
13388 /*is_template=*/is_template,
13389 /*is_namespace=*/false,
13390 /*check_dependency=*/true,
13391 /*ambiguous_decls=*/NULL,
13392 token->location);
13393 /* See if the default argument is valid. */
13394 default_argument = check_template_template_default_arg (default_argument);
13395 pop_deferring_access_checks ();
13396 return default_argument;
13399 /* Parse a template-parameter.
13401 template-parameter:
13402 type-parameter
13403 parameter-declaration
13405 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13406 the parameter. The TREE_PURPOSE is the default value, if any.
13407 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13408 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13409 set to true iff this parameter is a parameter pack. */
13411 static tree
13412 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13413 bool *is_parameter_pack)
13415 cp_token *token;
13416 cp_parameter_declarator *parameter_declarator;
13417 tree parm;
13419 /* Assume it is a type parameter or a template parameter. */
13420 *is_non_type = false;
13421 /* Assume it not a parameter pack. */
13422 *is_parameter_pack = false;
13423 /* Peek at the next token. */
13424 token = cp_lexer_peek_token (parser->lexer);
13425 /* If it is `class' or `template', we have a type-parameter. */
13426 if (token->keyword == RID_TEMPLATE)
13427 return cp_parser_type_parameter (parser, is_parameter_pack);
13428 /* If it is `class' or `typename' we do not know yet whether it is a
13429 type parameter or a non-type parameter. Consider:
13431 template <typename T, typename T::X X> ...
13435 template <class C, class D*> ...
13437 Here, the first parameter is a type parameter, and the second is
13438 a non-type parameter. We can tell by looking at the token after
13439 the identifier -- if it is a `,', `=', or `>' then we have a type
13440 parameter. */
13441 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13443 /* Peek at the token after `class' or `typename'. */
13444 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13445 /* If it's an ellipsis, we have a template type parameter
13446 pack. */
13447 if (token->type == CPP_ELLIPSIS)
13448 return cp_parser_type_parameter (parser, is_parameter_pack);
13449 /* If it's an identifier, skip it. */
13450 if (token->type == CPP_NAME)
13451 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13452 /* Now, see if the token looks like the end of a template
13453 parameter. */
13454 if (token->type == CPP_COMMA
13455 || token->type == CPP_EQ
13456 || token->type == CPP_GREATER)
13457 return cp_parser_type_parameter (parser, is_parameter_pack);
13460 /* Otherwise, it is a non-type parameter.
13462 [temp.param]
13464 When parsing a default template-argument for a non-type
13465 template-parameter, the first non-nested `>' is taken as the end
13466 of the template parameter-list rather than a greater-than
13467 operator. */
13468 *is_non_type = true;
13469 parameter_declarator
13470 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13471 /*parenthesized_p=*/NULL);
13473 if (!parameter_declarator)
13474 return error_mark_node;
13476 /* If the parameter declaration is marked as a parameter pack, set
13477 *IS_PARAMETER_PACK to notify the caller. */
13478 if (parameter_declarator->template_parameter_pack_p)
13479 *is_parameter_pack = true;
13481 if (parameter_declarator->default_argument)
13483 /* Can happen in some cases of erroneous input (c++/34892). */
13484 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13485 /* Consume the `...' for better error recovery. */
13486 cp_lexer_consume_token (parser->lexer);
13489 parm = grokdeclarator (parameter_declarator->declarator,
13490 &parameter_declarator->decl_specifiers,
13491 TPARM, /*initialized=*/0,
13492 /*attrlist=*/NULL);
13493 if (parm == error_mark_node)
13494 return error_mark_node;
13496 return build_tree_list (parameter_declarator->default_argument, parm);
13499 /* Parse a type-parameter.
13501 type-parameter:
13502 class identifier [opt]
13503 class identifier [opt] = type-id
13504 typename identifier [opt]
13505 typename identifier [opt] = type-id
13506 template < template-parameter-list > class identifier [opt]
13507 template < template-parameter-list > class identifier [opt]
13508 = id-expression
13510 GNU Extension (variadic templates):
13512 type-parameter:
13513 class ... identifier [opt]
13514 typename ... identifier [opt]
13516 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13517 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13518 the declaration of the parameter.
13520 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13522 static tree
13523 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13525 cp_token *token;
13526 tree parameter;
13528 /* Look for a keyword to tell us what kind of parameter this is. */
13529 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13530 if (!token)
13531 return error_mark_node;
13533 switch (token->keyword)
13535 case RID_CLASS:
13536 case RID_TYPENAME:
13538 tree identifier;
13539 tree default_argument;
13541 /* If the next token is an ellipsis, we have a template
13542 argument pack. */
13543 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13545 /* Consume the `...' token. */
13546 cp_lexer_consume_token (parser->lexer);
13547 maybe_warn_variadic_templates ();
13549 *is_parameter_pack = true;
13552 /* If the next token is an identifier, then it names the
13553 parameter. */
13554 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13555 identifier = cp_parser_identifier (parser);
13556 else
13557 identifier = NULL_TREE;
13559 /* Create the parameter. */
13560 parameter = finish_template_type_parm (class_type_node, identifier);
13562 /* If the next token is an `=', we have a default argument. */
13563 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13565 default_argument
13566 = cp_parser_default_type_template_argument (parser);
13568 /* Template parameter packs cannot have default
13569 arguments. */
13570 if (*is_parameter_pack)
13572 if (identifier)
13573 error_at (token->location,
13574 "template parameter pack %qD cannot have a "
13575 "default argument", identifier);
13576 else
13577 error_at (token->location,
13578 "template parameter packs cannot have "
13579 "default arguments");
13580 default_argument = NULL_TREE;
13582 else if (check_for_bare_parameter_packs (default_argument))
13583 default_argument = error_mark_node;
13585 else
13586 default_argument = NULL_TREE;
13588 /* Create the combined representation of the parameter and the
13589 default argument. */
13590 parameter = build_tree_list (default_argument, parameter);
13592 break;
13594 case RID_TEMPLATE:
13596 tree identifier;
13597 tree default_argument;
13599 /* Look for the `<'. */
13600 cp_parser_require (parser, CPP_LESS, RT_LESS);
13601 /* Parse the template-parameter-list. */
13602 cp_parser_template_parameter_list (parser);
13603 /* Look for the `>'. */
13604 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13605 /* Look for the `class' or 'typename' keywords. */
13606 cp_parser_type_parameter_key (parser);
13607 /* If the next token is an ellipsis, we have a template
13608 argument pack. */
13609 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13611 /* Consume the `...' token. */
13612 cp_lexer_consume_token (parser->lexer);
13613 maybe_warn_variadic_templates ();
13615 *is_parameter_pack = true;
13617 /* If the next token is an `=', then there is a
13618 default-argument. If the next token is a `>', we are at
13619 the end of the parameter-list. If the next token is a `,',
13620 then we are at the end of this parameter. */
13621 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13622 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13623 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13625 identifier = cp_parser_identifier (parser);
13626 /* Treat invalid names as if the parameter were nameless. */
13627 if (identifier == error_mark_node)
13628 identifier = NULL_TREE;
13630 else
13631 identifier = NULL_TREE;
13633 /* Create the template parameter. */
13634 parameter = finish_template_template_parm (class_type_node,
13635 identifier);
13637 /* If the next token is an `=', then there is a
13638 default-argument. */
13639 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13641 default_argument
13642 = cp_parser_default_template_template_argument (parser);
13644 /* Template parameter packs cannot have default
13645 arguments. */
13646 if (*is_parameter_pack)
13648 if (identifier)
13649 error_at (token->location,
13650 "template parameter pack %qD cannot "
13651 "have a default argument",
13652 identifier);
13653 else
13654 error_at (token->location, "template parameter packs cannot "
13655 "have default arguments");
13656 default_argument = NULL_TREE;
13659 else
13660 default_argument = NULL_TREE;
13662 /* Create the combined representation of the parameter and the
13663 default argument. */
13664 parameter = build_tree_list (default_argument, parameter);
13666 break;
13668 default:
13669 gcc_unreachable ();
13670 break;
13673 return parameter;
13676 /* Parse a template-id.
13678 template-id:
13679 template-name < template-argument-list [opt] >
13681 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13682 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13683 returned. Otherwise, if the template-name names a function, or set
13684 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13685 names a class, returns a TYPE_DECL for the specialization.
13687 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13688 uninstantiated templates. */
13690 static tree
13691 cp_parser_template_id (cp_parser *parser,
13692 bool template_keyword_p,
13693 bool check_dependency_p,
13694 enum tag_types tag_type,
13695 bool is_declaration)
13697 int i;
13698 tree templ;
13699 tree arguments;
13700 tree template_id;
13701 cp_token_position start_of_id = 0;
13702 deferred_access_check *chk;
13703 vec<deferred_access_check, va_gc> *access_check;
13704 cp_token *next_token = NULL, *next_token_2 = NULL;
13705 bool is_identifier;
13707 /* If the next token corresponds to a template-id, there is no need
13708 to reparse it. */
13709 next_token = cp_lexer_peek_token (parser->lexer);
13710 if (next_token->type == CPP_TEMPLATE_ID)
13712 struct tree_check *check_value;
13714 /* Get the stored value. */
13715 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13716 /* Perform any access checks that were deferred. */
13717 access_check = check_value->checks;
13718 if (access_check)
13720 FOR_EACH_VEC_ELT (*access_check, i, chk)
13721 perform_or_defer_access_check (chk->binfo,
13722 chk->decl,
13723 chk->diag_decl,
13724 tf_warning_or_error);
13726 /* Return the stored value. */
13727 return check_value->value;
13730 /* Avoid performing name lookup if there is no possibility of
13731 finding a template-id. */
13732 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13733 || (next_token->type == CPP_NAME
13734 && !cp_parser_nth_token_starts_template_argument_list_p
13735 (parser, 2)))
13737 cp_parser_error (parser, "expected template-id");
13738 return error_mark_node;
13741 /* Remember where the template-id starts. */
13742 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13743 start_of_id = cp_lexer_token_position (parser->lexer, false);
13745 push_deferring_access_checks (dk_deferred);
13747 /* Parse the template-name. */
13748 is_identifier = false;
13749 templ = cp_parser_template_name (parser, template_keyword_p,
13750 check_dependency_p,
13751 is_declaration,
13752 tag_type,
13753 &is_identifier);
13754 if (templ == error_mark_node || is_identifier)
13756 pop_deferring_access_checks ();
13757 return templ;
13760 /* If we find the sequence `[:' after a template-name, it's probably
13761 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13762 parse correctly the argument list. */
13763 next_token = cp_lexer_peek_token (parser->lexer);
13764 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13765 if (next_token->type == CPP_OPEN_SQUARE
13766 && next_token->flags & DIGRAPH
13767 && next_token_2->type == CPP_COLON
13768 && !(next_token_2->flags & PREV_WHITE))
13770 cp_parser_parse_tentatively (parser);
13771 /* Change `:' into `::'. */
13772 next_token_2->type = CPP_SCOPE;
13773 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13774 CPP_LESS. */
13775 cp_lexer_consume_token (parser->lexer);
13777 /* Parse the arguments. */
13778 arguments = cp_parser_enclosed_template_argument_list (parser);
13779 if (!cp_parser_parse_definitely (parser))
13781 /* If we couldn't parse an argument list, then we revert our changes
13782 and return simply an error. Maybe this is not a template-id
13783 after all. */
13784 next_token_2->type = CPP_COLON;
13785 cp_parser_error (parser, "expected %<<%>");
13786 pop_deferring_access_checks ();
13787 return error_mark_node;
13789 /* Otherwise, emit an error about the invalid digraph, but continue
13790 parsing because we got our argument list. */
13791 if (permerror (next_token->location,
13792 "%<<::%> cannot begin a template-argument list"))
13794 static bool hint = false;
13795 inform (next_token->location,
13796 "%<<:%> is an alternate spelling for %<[%>."
13797 " Insert whitespace between %<<%> and %<::%>");
13798 if (!hint && !flag_permissive)
13800 inform (next_token->location, "(if you use %<-fpermissive%> "
13801 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13802 "accept your code)");
13803 hint = true;
13807 else
13809 /* Look for the `<' that starts the template-argument-list. */
13810 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13812 pop_deferring_access_checks ();
13813 return error_mark_node;
13815 /* Parse the arguments. */
13816 arguments = cp_parser_enclosed_template_argument_list (parser);
13819 /* Build a representation of the specialization. */
13820 if (identifier_p (templ))
13821 template_id = build_min_nt_loc (next_token->location,
13822 TEMPLATE_ID_EXPR,
13823 templ, arguments);
13824 else if (DECL_TYPE_TEMPLATE_P (templ)
13825 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13827 bool entering_scope;
13828 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13829 template (rather than some instantiation thereof) only if
13830 is not nested within some other construct. For example, in
13831 "template <typename T> void f(T) { A<T>::", A<T> is just an
13832 instantiation of A. */
13833 entering_scope = (template_parm_scope_p ()
13834 && cp_lexer_next_token_is (parser->lexer,
13835 CPP_SCOPE));
13836 template_id
13837 = finish_template_type (templ, arguments, entering_scope);
13839 else if (variable_template_p (templ))
13841 template_id = lookup_template_variable (templ, arguments);
13842 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
13843 SET_EXPR_LOCATION (template_id, next_token->location);
13845 else
13847 /* If it's not a class-template or a template-template, it should be
13848 a function-template. */
13849 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13850 || TREE_CODE (templ) == OVERLOAD
13851 || BASELINK_P (templ)));
13853 template_id = lookup_template_function (templ, arguments);
13854 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
13855 SET_EXPR_LOCATION (template_id, next_token->location);
13858 /* If parsing tentatively, replace the sequence of tokens that makes
13859 up the template-id with a CPP_TEMPLATE_ID token. That way,
13860 should we re-parse the token stream, we will not have to repeat
13861 the effort required to do the parse, nor will we issue duplicate
13862 error messages about problems during instantiation of the
13863 template. */
13864 if (start_of_id
13865 /* Don't do this if we had a parse error in a declarator; re-parsing
13866 might succeed if a name changes meaning (60361). */
13867 && !(cp_parser_error_occurred (parser)
13868 && cp_parser_parsing_tentatively (parser)
13869 && parser->in_declarator_p))
13871 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13873 /* Reset the contents of the START_OF_ID token. */
13874 token->type = CPP_TEMPLATE_ID;
13875 /* Retrieve any deferred checks. Do not pop this access checks yet
13876 so the memory will not be reclaimed during token replacing below. */
13877 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13878 token->u.tree_check_value->value = template_id;
13879 token->u.tree_check_value->checks = get_deferred_access_checks ();
13880 token->keyword = RID_MAX;
13882 /* Purge all subsequent tokens. */
13883 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13885 /* ??? Can we actually assume that, if template_id ==
13886 error_mark_node, we will have issued a diagnostic to the
13887 user, as opposed to simply marking the tentative parse as
13888 failed? */
13889 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13890 error_at (token->location, "parse error in template argument list");
13893 pop_to_parent_deferring_access_checks ();
13894 return template_id;
13897 /* Parse a template-name.
13899 template-name:
13900 identifier
13902 The standard should actually say:
13904 template-name:
13905 identifier
13906 operator-function-id
13908 A defect report has been filed about this issue.
13910 A conversion-function-id cannot be a template name because they cannot
13911 be part of a template-id. In fact, looking at this code:
13913 a.operator K<int>()
13915 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13916 It is impossible to call a templated conversion-function-id with an
13917 explicit argument list, since the only allowed template parameter is
13918 the type to which it is converting.
13920 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13921 `template' keyword, in a construction like:
13923 T::template f<3>()
13925 In that case `f' is taken to be a template-name, even though there
13926 is no way of knowing for sure.
13928 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13929 name refers to a set of overloaded functions, at least one of which
13930 is a template, or an IDENTIFIER_NODE with the name of the template,
13931 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13932 names are looked up inside uninstantiated templates. */
13934 static tree
13935 cp_parser_template_name (cp_parser* parser,
13936 bool template_keyword_p,
13937 bool check_dependency_p,
13938 bool is_declaration,
13939 enum tag_types tag_type,
13940 bool *is_identifier)
13942 tree identifier;
13943 tree decl;
13944 tree fns;
13945 cp_token *token = cp_lexer_peek_token (parser->lexer);
13947 /* If the next token is `operator', then we have either an
13948 operator-function-id or a conversion-function-id. */
13949 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13951 /* We don't know whether we're looking at an
13952 operator-function-id or a conversion-function-id. */
13953 cp_parser_parse_tentatively (parser);
13954 /* Try an operator-function-id. */
13955 identifier = cp_parser_operator_function_id (parser);
13956 /* If that didn't work, try a conversion-function-id. */
13957 if (!cp_parser_parse_definitely (parser))
13959 cp_parser_error (parser, "expected template-name");
13960 return error_mark_node;
13963 /* Look for the identifier. */
13964 else
13965 identifier = cp_parser_identifier (parser);
13967 /* If we didn't find an identifier, we don't have a template-id. */
13968 if (identifier == error_mark_node)
13969 return error_mark_node;
13971 /* If the name immediately followed the `template' keyword, then it
13972 is a template-name. However, if the next token is not `<', then
13973 we do not treat it as a template-name, since it is not being used
13974 as part of a template-id. This enables us to handle constructs
13975 like:
13977 template <typename T> struct S { S(); };
13978 template <typename T> S<T>::S();
13980 correctly. We would treat `S' as a template -- if it were `S<T>'
13981 -- but we do not if there is no `<'. */
13983 if (processing_template_decl
13984 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13986 /* In a declaration, in a dependent context, we pretend that the
13987 "template" keyword was present in order to improve error
13988 recovery. For example, given:
13990 template <typename T> void f(T::X<int>);
13992 we want to treat "X<int>" as a template-id. */
13993 if (is_declaration
13994 && !template_keyword_p
13995 && parser->scope && TYPE_P (parser->scope)
13996 && check_dependency_p
13997 && dependent_scope_p (parser->scope)
13998 /* Do not do this for dtors (or ctors), since they never
13999 need the template keyword before their name. */
14000 && !constructor_name_p (identifier, parser->scope))
14002 cp_token_position start = 0;
14004 /* Explain what went wrong. */
14005 error_at (token->location, "non-template %qD used as template",
14006 identifier);
14007 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14008 parser->scope, identifier);
14009 /* If parsing tentatively, find the location of the "<" token. */
14010 if (cp_parser_simulate_error (parser))
14011 start = cp_lexer_token_position (parser->lexer, true);
14012 /* Parse the template arguments so that we can issue error
14013 messages about them. */
14014 cp_lexer_consume_token (parser->lexer);
14015 cp_parser_enclosed_template_argument_list (parser);
14016 /* Skip tokens until we find a good place from which to
14017 continue parsing. */
14018 cp_parser_skip_to_closing_parenthesis (parser,
14019 /*recovering=*/true,
14020 /*or_comma=*/true,
14021 /*consume_paren=*/false);
14022 /* If parsing tentatively, permanently remove the
14023 template argument list. That will prevent duplicate
14024 error messages from being issued about the missing
14025 "template" keyword. */
14026 if (start)
14027 cp_lexer_purge_tokens_after (parser->lexer, start);
14028 if (is_identifier)
14029 *is_identifier = true;
14030 return identifier;
14033 /* If the "template" keyword is present, then there is generally
14034 no point in doing name-lookup, so we just return IDENTIFIER.
14035 But, if the qualifying scope is non-dependent then we can
14036 (and must) do name-lookup normally. */
14037 if (template_keyword_p
14038 && (!parser->scope
14039 || (TYPE_P (parser->scope)
14040 && dependent_type_p (parser->scope))))
14041 return identifier;
14044 /* Look up the name. */
14045 decl = cp_parser_lookup_name (parser, identifier,
14046 tag_type,
14047 /*is_template=*/true,
14048 /*is_namespace=*/false,
14049 check_dependency_p,
14050 /*ambiguous_decls=*/NULL,
14051 token->location);
14053 decl = strip_using_decl (decl);
14055 /* If DECL is a template, then the name was a template-name. */
14056 if (TREE_CODE (decl) == TEMPLATE_DECL)
14058 if (TREE_DEPRECATED (decl)
14059 && deprecated_state != DEPRECATED_SUPPRESS)
14060 warn_deprecated_use (decl, NULL_TREE);
14062 else
14064 tree fn = NULL_TREE;
14066 /* The standard does not explicitly indicate whether a name that
14067 names a set of overloaded declarations, some of which are
14068 templates, is a template-name. However, such a name should
14069 be a template-name; otherwise, there is no way to form a
14070 template-id for the overloaded templates. */
14071 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14072 if (TREE_CODE (fns) == OVERLOAD)
14073 for (fn = fns; fn; fn = OVL_NEXT (fn))
14074 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14075 break;
14077 if (!fn)
14079 /* The name does not name a template. */
14080 cp_parser_error (parser, "expected template-name");
14081 return error_mark_node;
14085 /* If DECL is dependent, and refers to a function, then just return
14086 its name; we will look it up again during template instantiation. */
14087 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14089 tree scope = ovl_scope (decl);
14090 if (TYPE_P (scope) && dependent_type_p (scope))
14091 return identifier;
14094 return decl;
14097 /* Parse a template-argument-list.
14099 template-argument-list:
14100 template-argument ... [opt]
14101 template-argument-list , template-argument ... [opt]
14103 Returns a TREE_VEC containing the arguments. */
14105 static tree
14106 cp_parser_template_argument_list (cp_parser* parser)
14108 tree fixed_args[10];
14109 unsigned n_args = 0;
14110 unsigned alloced = 10;
14111 tree *arg_ary = fixed_args;
14112 tree vec;
14113 bool saved_in_template_argument_list_p;
14114 bool saved_ice_p;
14115 bool saved_non_ice_p;
14117 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14118 parser->in_template_argument_list_p = true;
14119 /* Even if the template-id appears in an integral
14120 constant-expression, the contents of the argument list do
14121 not. */
14122 saved_ice_p = parser->integral_constant_expression_p;
14123 parser->integral_constant_expression_p = false;
14124 saved_non_ice_p = parser->non_integral_constant_expression_p;
14125 parser->non_integral_constant_expression_p = false;
14127 /* Parse the arguments. */
14130 tree argument;
14132 if (n_args)
14133 /* Consume the comma. */
14134 cp_lexer_consume_token (parser->lexer);
14136 /* Parse the template-argument. */
14137 argument = cp_parser_template_argument (parser);
14139 /* If the next token is an ellipsis, we're expanding a template
14140 argument pack. */
14141 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14143 if (argument == error_mark_node)
14145 cp_token *token = cp_lexer_peek_token (parser->lexer);
14146 error_at (token->location,
14147 "expected parameter pack before %<...%>");
14149 /* Consume the `...' token. */
14150 cp_lexer_consume_token (parser->lexer);
14152 /* Make the argument into a TYPE_PACK_EXPANSION or
14153 EXPR_PACK_EXPANSION. */
14154 argument = make_pack_expansion (argument);
14157 if (n_args == alloced)
14159 alloced *= 2;
14161 if (arg_ary == fixed_args)
14163 arg_ary = XNEWVEC (tree, alloced);
14164 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14166 else
14167 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14169 arg_ary[n_args++] = argument;
14171 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14173 vec = make_tree_vec (n_args);
14175 while (n_args--)
14176 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14178 if (arg_ary != fixed_args)
14179 free (arg_ary);
14180 parser->non_integral_constant_expression_p = saved_non_ice_p;
14181 parser->integral_constant_expression_p = saved_ice_p;
14182 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14183 #ifdef ENABLE_CHECKING
14184 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14185 #endif
14186 return vec;
14189 /* Parse a template-argument.
14191 template-argument:
14192 assignment-expression
14193 type-id
14194 id-expression
14196 The representation is that of an assignment-expression, type-id, or
14197 id-expression -- except that the qualified id-expression is
14198 evaluated, so that the value returned is either a DECL or an
14199 OVERLOAD.
14201 Although the standard says "assignment-expression", it forbids
14202 throw-expressions or assignments in the template argument.
14203 Therefore, we use "conditional-expression" instead. */
14205 static tree
14206 cp_parser_template_argument (cp_parser* parser)
14208 tree argument;
14209 bool template_p;
14210 bool address_p;
14211 bool maybe_type_id = false;
14212 cp_token *token = NULL, *argument_start_token = NULL;
14213 location_t loc = 0;
14214 cp_id_kind idk;
14216 /* There's really no way to know what we're looking at, so we just
14217 try each alternative in order.
14219 [temp.arg]
14221 In a template-argument, an ambiguity between a type-id and an
14222 expression is resolved to a type-id, regardless of the form of
14223 the corresponding template-parameter.
14225 Therefore, we try a type-id first. */
14226 cp_parser_parse_tentatively (parser);
14227 argument = cp_parser_template_type_arg (parser);
14228 /* If there was no error parsing the type-id but the next token is a
14229 '>>', our behavior depends on which dialect of C++ we're
14230 parsing. In C++98, we probably found a typo for '> >'. But there
14231 are type-id which are also valid expressions. For instance:
14233 struct X { int operator >> (int); };
14234 template <int V> struct Foo {};
14235 Foo<X () >> 5> r;
14237 Here 'X()' is a valid type-id of a function type, but the user just
14238 wanted to write the expression "X() >> 5". Thus, we remember that we
14239 found a valid type-id, but we still try to parse the argument as an
14240 expression to see what happens.
14242 In C++0x, the '>>' will be considered two separate '>'
14243 tokens. */
14244 if (!cp_parser_error_occurred (parser)
14245 && cxx_dialect == cxx98
14246 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14248 maybe_type_id = true;
14249 cp_parser_abort_tentative_parse (parser);
14251 else
14253 /* If the next token isn't a `,' or a `>', then this argument wasn't
14254 really finished. This means that the argument is not a valid
14255 type-id. */
14256 if (!cp_parser_next_token_ends_template_argument_p (parser))
14257 cp_parser_error (parser, "expected template-argument");
14258 /* If that worked, we're done. */
14259 if (cp_parser_parse_definitely (parser))
14260 return argument;
14262 /* We're still not sure what the argument will be. */
14263 cp_parser_parse_tentatively (parser);
14264 /* Try a template. */
14265 argument_start_token = cp_lexer_peek_token (parser->lexer);
14266 argument = cp_parser_id_expression (parser,
14267 /*template_keyword_p=*/false,
14268 /*check_dependency_p=*/true,
14269 &template_p,
14270 /*declarator_p=*/false,
14271 /*optional_p=*/false);
14272 /* If the next token isn't a `,' or a `>', then this argument wasn't
14273 really finished. */
14274 if (!cp_parser_next_token_ends_template_argument_p (parser))
14275 cp_parser_error (parser, "expected template-argument");
14276 if (!cp_parser_error_occurred (parser))
14278 /* Figure out what is being referred to. If the id-expression
14279 was for a class template specialization, then we will have a
14280 TYPE_DECL at this point. There is no need to do name lookup
14281 at this point in that case. */
14282 if (TREE_CODE (argument) != TYPE_DECL)
14283 argument = cp_parser_lookup_name (parser, argument,
14284 none_type,
14285 /*is_template=*/template_p,
14286 /*is_namespace=*/false,
14287 /*check_dependency=*/true,
14288 /*ambiguous_decls=*/NULL,
14289 argument_start_token->location);
14290 if (TREE_CODE (argument) != TEMPLATE_DECL
14291 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14292 cp_parser_error (parser, "expected template-name");
14294 if (cp_parser_parse_definitely (parser))
14296 if (TREE_DEPRECATED (argument))
14297 warn_deprecated_use (argument, NULL_TREE);
14298 return argument;
14300 /* It must be a non-type argument. There permitted cases are given
14301 in [temp.arg.nontype]:
14303 -- an integral constant-expression of integral or enumeration
14304 type; or
14306 -- the name of a non-type template-parameter; or
14308 -- the name of an object or function with external linkage...
14310 -- the address of an object or function with external linkage...
14312 -- a pointer to member... */
14313 /* Look for a non-type template parameter. */
14314 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14316 cp_parser_parse_tentatively (parser);
14317 argument = cp_parser_primary_expression (parser,
14318 /*address_p=*/false,
14319 /*cast_p=*/false,
14320 /*template_arg_p=*/true,
14321 &idk);
14322 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14323 || !cp_parser_next_token_ends_template_argument_p (parser))
14324 cp_parser_simulate_error (parser);
14325 if (cp_parser_parse_definitely (parser))
14326 return argument;
14329 /* If the next token is "&", the argument must be the address of an
14330 object or function with external linkage. */
14331 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14332 if (address_p)
14334 loc = cp_lexer_peek_token (parser->lexer)->location;
14335 cp_lexer_consume_token (parser->lexer);
14337 /* See if we might have an id-expression. */
14338 token = cp_lexer_peek_token (parser->lexer);
14339 if (token->type == CPP_NAME
14340 || token->keyword == RID_OPERATOR
14341 || token->type == CPP_SCOPE
14342 || token->type == CPP_TEMPLATE_ID
14343 || token->type == CPP_NESTED_NAME_SPECIFIER)
14345 cp_parser_parse_tentatively (parser);
14346 argument = cp_parser_primary_expression (parser,
14347 address_p,
14348 /*cast_p=*/false,
14349 /*template_arg_p=*/true,
14350 &idk);
14351 if (cp_parser_error_occurred (parser)
14352 || !cp_parser_next_token_ends_template_argument_p (parser))
14353 cp_parser_abort_tentative_parse (parser);
14354 else
14356 tree probe;
14358 if (INDIRECT_REF_P (argument))
14360 /* Strip the dereference temporarily. */
14361 gcc_assert (REFERENCE_REF_P (argument));
14362 argument = TREE_OPERAND (argument, 0);
14365 /* If we're in a template, we represent a qualified-id referring
14366 to a static data member as a SCOPE_REF even if the scope isn't
14367 dependent so that we can check access control later. */
14368 probe = argument;
14369 if (TREE_CODE (probe) == SCOPE_REF)
14370 probe = TREE_OPERAND (probe, 1);
14371 if (VAR_P (probe))
14373 /* A variable without external linkage might still be a
14374 valid constant-expression, so no error is issued here
14375 if the external-linkage check fails. */
14376 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14377 cp_parser_simulate_error (parser);
14379 else if (is_overloaded_fn (argument))
14380 /* All overloaded functions are allowed; if the external
14381 linkage test does not pass, an error will be issued
14382 later. */
14384 else if (address_p
14385 && (TREE_CODE (argument) == OFFSET_REF
14386 || TREE_CODE (argument) == SCOPE_REF))
14387 /* A pointer-to-member. */
14389 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14391 else
14392 cp_parser_simulate_error (parser);
14394 if (cp_parser_parse_definitely (parser))
14396 if (address_p)
14397 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14398 tf_warning_or_error);
14399 else
14400 argument = convert_from_reference (argument);
14401 return argument;
14405 /* If the argument started with "&", there are no other valid
14406 alternatives at this point. */
14407 if (address_p)
14409 cp_parser_error (parser, "invalid non-type template argument");
14410 return error_mark_node;
14413 /* If the argument wasn't successfully parsed as a type-id followed
14414 by '>>', the argument can only be a constant expression now.
14415 Otherwise, we try parsing the constant-expression tentatively,
14416 because the argument could really be a type-id. */
14417 if (maybe_type_id)
14418 cp_parser_parse_tentatively (parser);
14419 argument = cp_parser_constant_expression (parser);
14421 if (!maybe_type_id)
14422 return argument;
14423 if (!cp_parser_next_token_ends_template_argument_p (parser))
14424 cp_parser_error (parser, "expected template-argument");
14425 if (cp_parser_parse_definitely (parser))
14426 return argument;
14427 /* We did our best to parse the argument as a non type-id, but that
14428 was the only alternative that matched (albeit with a '>' after
14429 it). We can assume it's just a typo from the user, and a
14430 diagnostic will then be issued. */
14431 return cp_parser_template_type_arg (parser);
14434 /* Parse an explicit-instantiation.
14436 explicit-instantiation:
14437 template declaration
14439 Although the standard says `declaration', what it really means is:
14441 explicit-instantiation:
14442 template decl-specifier-seq [opt] declarator [opt] ;
14444 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14445 supposed to be allowed. A defect report has been filed about this
14446 issue.
14448 GNU Extension:
14450 explicit-instantiation:
14451 storage-class-specifier template
14452 decl-specifier-seq [opt] declarator [opt] ;
14453 function-specifier template
14454 decl-specifier-seq [opt] declarator [opt] ; */
14456 static void
14457 cp_parser_explicit_instantiation (cp_parser* parser)
14459 int declares_class_or_enum;
14460 cp_decl_specifier_seq decl_specifiers;
14461 tree extension_specifier = NULL_TREE;
14463 timevar_push (TV_TEMPLATE_INST);
14465 /* Look for an (optional) storage-class-specifier or
14466 function-specifier. */
14467 if (cp_parser_allow_gnu_extensions_p (parser))
14469 extension_specifier
14470 = cp_parser_storage_class_specifier_opt (parser);
14471 if (!extension_specifier)
14472 extension_specifier
14473 = cp_parser_function_specifier_opt (parser,
14474 /*decl_specs=*/NULL);
14477 /* Look for the `template' keyword. */
14478 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14479 /* Let the front end know that we are processing an explicit
14480 instantiation. */
14481 begin_explicit_instantiation ();
14482 /* [temp.explicit] says that we are supposed to ignore access
14483 control while processing explicit instantiation directives. */
14484 push_deferring_access_checks (dk_no_check);
14485 /* Parse a decl-specifier-seq. */
14486 cp_parser_decl_specifier_seq (parser,
14487 CP_PARSER_FLAGS_OPTIONAL,
14488 &decl_specifiers,
14489 &declares_class_or_enum);
14490 /* If there was exactly one decl-specifier, and it declared a class,
14491 and there's no declarator, then we have an explicit type
14492 instantiation. */
14493 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14495 tree type;
14497 type = check_tag_decl (&decl_specifiers,
14498 /*explicit_type_instantiation_p=*/true);
14499 /* Turn access control back on for names used during
14500 template instantiation. */
14501 pop_deferring_access_checks ();
14502 if (type)
14503 do_type_instantiation (type, extension_specifier,
14504 /*complain=*/tf_error);
14506 else
14508 cp_declarator *declarator;
14509 tree decl;
14511 /* Parse the declarator. */
14512 declarator
14513 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14514 /*ctor_dtor_or_conv_p=*/NULL,
14515 /*parenthesized_p=*/NULL,
14516 /*member_p=*/false,
14517 /*friend_p=*/false);
14518 if (declares_class_or_enum & 2)
14519 cp_parser_check_for_definition_in_return_type (declarator,
14520 decl_specifiers.type,
14521 decl_specifiers.locations[ds_type_spec]);
14522 if (declarator != cp_error_declarator)
14524 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14525 permerror (decl_specifiers.locations[ds_inline],
14526 "explicit instantiation shall not use"
14527 " %<inline%> specifier");
14528 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14529 permerror (decl_specifiers.locations[ds_constexpr],
14530 "explicit instantiation shall not use"
14531 " %<constexpr%> specifier");
14533 decl = grokdeclarator (declarator, &decl_specifiers,
14534 NORMAL, 0, &decl_specifiers.attributes);
14535 /* Turn access control back on for names used during
14536 template instantiation. */
14537 pop_deferring_access_checks ();
14538 /* Do the explicit instantiation. */
14539 do_decl_instantiation (decl, extension_specifier);
14541 else
14543 pop_deferring_access_checks ();
14544 /* Skip the body of the explicit instantiation. */
14545 cp_parser_skip_to_end_of_statement (parser);
14548 /* We're done with the instantiation. */
14549 end_explicit_instantiation ();
14551 cp_parser_consume_semicolon_at_end_of_statement (parser);
14553 timevar_pop (TV_TEMPLATE_INST);
14556 /* Parse an explicit-specialization.
14558 explicit-specialization:
14559 template < > declaration
14561 Although the standard says `declaration', what it really means is:
14563 explicit-specialization:
14564 template <> decl-specifier [opt] init-declarator [opt] ;
14565 template <> function-definition
14566 template <> explicit-specialization
14567 template <> template-declaration */
14569 static void
14570 cp_parser_explicit_specialization (cp_parser* parser)
14572 bool need_lang_pop;
14573 cp_token *token = cp_lexer_peek_token (parser->lexer);
14575 /* Look for the `template' keyword. */
14576 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14577 /* Look for the `<'. */
14578 cp_parser_require (parser, CPP_LESS, RT_LESS);
14579 /* Look for the `>'. */
14580 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14581 /* We have processed another parameter list. */
14582 ++parser->num_template_parameter_lists;
14583 /* [temp]
14585 A template ... explicit specialization ... shall not have C
14586 linkage. */
14587 if (current_lang_name == lang_name_c)
14589 error_at (token->location, "template specialization with C linkage");
14590 /* Give it C++ linkage to avoid confusing other parts of the
14591 front end. */
14592 push_lang_context (lang_name_cplusplus);
14593 need_lang_pop = true;
14595 else
14596 need_lang_pop = false;
14597 /* Let the front end know that we are beginning a specialization. */
14598 if (!begin_specialization ())
14600 end_specialization ();
14601 return;
14604 /* If the next keyword is `template', we need to figure out whether
14605 or not we're looking a template-declaration. */
14606 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14608 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14609 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14610 cp_parser_template_declaration_after_export (parser,
14611 /*member_p=*/false);
14612 else
14613 cp_parser_explicit_specialization (parser);
14615 else
14616 /* Parse the dependent declaration. */
14617 cp_parser_single_declaration (parser,
14618 /*checks=*/NULL,
14619 /*member_p=*/false,
14620 /*explicit_specialization_p=*/true,
14621 /*friend_p=*/NULL);
14622 /* We're done with the specialization. */
14623 end_specialization ();
14624 /* For the erroneous case of a template with C linkage, we pushed an
14625 implicit C++ linkage scope; exit that scope now. */
14626 if (need_lang_pop)
14627 pop_lang_context ();
14628 /* We're done with this parameter list. */
14629 --parser->num_template_parameter_lists;
14632 /* Parse a type-specifier.
14634 type-specifier:
14635 simple-type-specifier
14636 class-specifier
14637 enum-specifier
14638 elaborated-type-specifier
14639 cv-qualifier
14641 GNU Extension:
14643 type-specifier:
14644 __complex__
14646 Returns a representation of the type-specifier. For a
14647 class-specifier, enum-specifier, or elaborated-type-specifier, a
14648 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14650 The parser flags FLAGS is used to control type-specifier parsing.
14652 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14653 in a decl-specifier-seq.
14655 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14656 class-specifier, enum-specifier, or elaborated-type-specifier, then
14657 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14658 if a type is declared; 2 if it is defined. Otherwise, it is set to
14659 zero.
14661 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14662 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14663 is set to FALSE. */
14665 static tree
14666 cp_parser_type_specifier (cp_parser* parser,
14667 cp_parser_flags flags,
14668 cp_decl_specifier_seq *decl_specs,
14669 bool is_declaration,
14670 int* declares_class_or_enum,
14671 bool* is_cv_qualifier)
14673 tree type_spec = NULL_TREE;
14674 cp_token *token;
14675 enum rid keyword;
14676 cp_decl_spec ds = ds_last;
14678 /* Assume this type-specifier does not declare a new type. */
14679 if (declares_class_or_enum)
14680 *declares_class_or_enum = 0;
14681 /* And that it does not specify a cv-qualifier. */
14682 if (is_cv_qualifier)
14683 *is_cv_qualifier = false;
14684 /* Peek at the next token. */
14685 token = cp_lexer_peek_token (parser->lexer);
14687 /* If we're looking at a keyword, we can use that to guide the
14688 production we choose. */
14689 keyword = token->keyword;
14690 switch (keyword)
14692 case RID_ENUM:
14693 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14694 goto elaborated_type_specifier;
14696 /* Look for the enum-specifier. */
14697 type_spec = cp_parser_enum_specifier (parser);
14698 /* If that worked, we're done. */
14699 if (type_spec)
14701 if (declares_class_or_enum)
14702 *declares_class_or_enum = 2;
14703 if (decl_specs)
14704 cp_parser_set_decl_spec_type (decl_specs,
14705 type_spec,
14706 token,
14707 /*type_definition_p=*/true);
14708 return type_spec;
14710 else
14711 goto elaborated_type_specifier;
14713 /* Any of these indicate either a class-specifier, or an
14714 elaborated-type-specifier. */
14715 case RID_CLASS:
14716 case RID_STRUCT:
14717 case RID_UNION:
14718 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14719 goto elaborated_type_specifier;
14721 /* Parse tentatively so that we can back up if we don't find a
14722 class-specifier. */
14723 cp_parser_parse_tentatively (parser);
14724 /* Look for the class-specifier. */
14725 type_spec = cp_parser_class_specifier (parser);
14726 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14727 /* If that worked, we're done. */
14728 if (cp_parser_parse_definitely (parser))
14730 if (declares_class_or_enum)
14731 *declares_class_or_enum = 2;
14732 if (decl_specs)
14733 cp_parser_set_decl_spec_type (decl_specs,
14734 type_spec,
14735 token,
14736 /*type_definition_p=*/true);
14737 return type_spec;
14740 /* Fall through. */
14741 elaborated_type_specifier:
14742 /* We're declaring (not defining) a class or enum. */
14743 if (declares_class_or_enum)
14744 *declares_class_or_enum = 1;
14746 /* Fall through. */
14747 case RID_TYPENAME:
14748 /* Look for an elaborated-type-specifier. */
14749 type_spec
14750 = (cp_parser_elaborated_type_specifier
14751 (parser,
14752 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14753 is_declaration));
14754 if (decl_specs)
14755 cp_parser_set_decl_spec_type (decl_specs,
14756 type_spec,
14757 token,
14758 /*type_definition_p=*/false);
14759 return type_spec;
14761 case RID_CONST:
14762 ds = ds_const;
14763 if (is_cv_qualifier)
14764 *is_cv_qualifier = true;
14765 break;
14767 case RID_VOLATILE:
14768 ds = ds_volatile;
14769 if (is_cv_qualifier)
14770 *is_cv_qualifier = true;
14771 break;
14773 case RID_RESTRICT:
14774 ds = ds_restrict;
14775 if (is_cv_qualifier)
14776 *is_cv_qualifier = true;
14777 break;
14779 case RID_COMPLEX:
14780 /* The `__complex__' keyword is a GNU extension. */
14781 ds = ds_complex;
14782 break;
14784 default:
14785 break;
14788 /* Handle simple keywords. */
14789 if (ds != ds_last)
14791 if (decl_specs)
14793 set_and_check_decl_spec_loc (decl_specs, ds, token);
14794 decl_specs->any_specifiers_p = true;
14796 return cp_lexer_consume_token (parser->lexer)->u.value;
14799 /* If we do not already have a type-specifier, assume we are looking
14800 at a simple-type-specifier. */
14801 type_spec = cp_parser_simple_type_specifier (parser,
14802 decl_specs,
14803 flags);
14805 /* If we didn't find a type-specifier, and a type-specifier was not
14806 optional in this context, issue an error message. */
14807 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14809 cp_parser_error (parser, "expected type specifier");
14810 return error_mark_node;
14813 return type_spec;
14816 /* Parse a simple-type-specifier.
14818 simple-type-specifier:
14819 :: [opt] nested-name-specifier [opt] type-name
14820 :: [opt] nested-name-specifier template template-id
14821 char
14822 wchar_t
14823 bool
14824 short
14826 long
14827 signed
14828 unsigned
14829 float
14830 double
14831 void
14833 C++0x Extension:
14835 simple-type-specifier:
14836 auto
14837 decltype ( expression )
14838 char16_t
14839 char32_t
14840 __underlying_type ( type-id )
14842 GNU Extension:
14844 simple-type-specifier:
14845 __int128
14846 __typeof__ unary-expression
14847 __typeof__ ( type-id )
14848 __typeof__ ( type-id ) { initializer-list , [opt] }
14850 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14851 appropriately updated. */
14853 static tree
14854 cp_parser_simple_type_specifier (cp_parser* parser,
14855 cp_decl_specifier_seq *decl_specs,
14856 cp_parser_flags flags)
14858 tree type = NULL_TREE;
14859 cp_token *token;
14860 int idx;
14862 /* Peek at the next token. */
14863 token = cp_lexer_peek_token (parser->lexer);
14865 /* If we're looking at a keyword, things are easy. */
14866 switch (token->keyword)
14868 case RID_CHAR:
14869 if (decl_specs)
14870 decl_specs->explicit_char_p = true;
14871 type = char_type_node;
14872 break;
14873 case RID_CHAR16:
14874 type = char16_type_node;
14875 break;
14876 case RID_CHAR32:
14877 type = char32_type_node;
14878 break;
14879 case RID_WCHAR:
14880 type = wchar_type_node;
14881 break;
14882 case RID_BOOL:
14883 type = boolean_type_node;
14884 break;
14885 case RID_SHORT:
14886 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14887 type = short_integer_type_node;
14888 break;
14889 case RID_INT:
14890 if (decl_specs)
14891 decl_specs->explicit_int_p = true;
14892 type = integer_type_node;
14893 break;
14894 case RID_INT_N_0:
14895 case RID_INT_N_1:
14896 case RID_INT_N_2:
14897 case RID_INT_N_3:
14898 idx = token->keyword - RID_INT_N_0;
14899 if (! int_n_enabled_p [idx])
14900 break;
14901 if (decl_specs)
14903 decl_specs->explicit_intN_p = true;
14904 decl_specs->int_n_idx = idx;
14906 type = int_n_trees [idx].signed_type;
14907 break;
14908 case RID_LONG:
14909 if (decl_specs)
14910 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14911 type = long_integer_type_node;
14912 break;
14913 case RID_SIGNED:
14914 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14915 type = integer_type_node;
14916 break;
14917 case RID_UNSIGNED:
14918 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14919 type = unsigned_type_node;
14920 break;
14921 case RID_FLOAT:
14922 type = float_type_node;
14923 break;
14924 case RID_DOUBLE:
14925 type = double_type_node;
14926 break;
14927 case RID_VOID:
14928 type = void_type_node;
14929 break;
14931 case RID_AUTO:
14932 maybe_warn_cpp0x (CPP0X_AUTO);
14933 if (parser->auto_is_implicit_function_template_parm_p)
14935 /* The 'auto' might be the placeholder return type for a function decl
14936 with trailing return type. */
14937 bool have_trailing_return_fn_decl = false;
14938 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14939 == CPP_OPEN_PAREN)
14941 cp_parser_parse_tentatively (parser);
14942 cp_lexer_consume_token (parser->lexer);
14943 cp_lexer_consume_token (parser->lexer);
14944 if (cp_parser_skip_to_closing_parenthesis (parser,
14945 /*recovering*/false,
14946 /*or_comma*/false,
14947 /*consume_paren*/true))
14948 have_trailing_return_fn_decl
14949 = cp_lexer_next_token_is (parser->lexer, CPP_DEREF);
14950 cp_parser_abort_tentative_parse (parser);
14953 if (have_trailing_return_fn_decl)
14955 type = make_auto ();
14956 break;
14959 if (cxx_dialect >= cxx14)
14960 type = synthesize_implicit_template_parm (parser);
14961 else
14962 type = error_mark_node;
14964 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14966 if (cxx_dialect < cxx14)
14967 error_at (token->location,
14968 "use of %<auto%> in lambda parameter declaration "
14969 "only available with "
14970 "-std=c++14 or -std=gnu++14");
14972 else if (cxx_dialect < cxx14)
14973 error_at (token->location,
14974 "use of %<auto%> in parameter declaration "
14975 "only available with "
14976 "-std=c++14 or -std=gnu++14");
14977 else
14978 pedwarn (token->location, OPT_Wpedantic,
14979 "ISO C++ forbids use of %<auto%> in parameter "
14980 "declaration");
14982 else
14983 type = make_auto ();
14984 break;
14986 case RID_DECLTYPE:
14987 /* Since DR 743, decltype can either be a simple-type-specifier by
14988 itself or begin a nested-name-specifier. Parsing it will replace
14989 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14990 handling below decide what to do. */
14991 cp_parser_decltype (parser);
14992 cp_lexer_set_token_position (parser->lexer, token);
14993 break;
14995 case RID_TYPEOF:
14996 /* Consume the `typeof' token. */
14997 cp_lexer_consume_token (parser->lexer);
14998 /* Parse the operand to `typeof'. */
14999 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
15000 /* If it is not already a TYPE, take its type. */
15001 if (!TYPE_P (type))
15002 type = finish_typeof (type);
15004 if (decl_specs)
15005 cp_parser_set_decl_spec_type (decl_specs, type,
15006 token,
15007 /*type_definition_p=*/false);
15009 return type;
15011 case RID_UNDERLYING_TYPE:
15012 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
15013 if (decl_specs)
15014 cp_parser_set_decl_spec_type (decl_specs, type,
15015 token,
15016 /*type_definition_p=*/false);
15018 return type;
15020 case RID_BASES:
15021 case RID_DIRECT_BASES:
15022 type = cp_parser_trait_expr (parser, token->keyword);
15023 if (decl_specs)
15024 cp_parser_set_decl_spec_type (decl_specs, type,
15025 token,
15026 /*type_definition_p=*/false);
15027 return type;
15028 default:
15029 break;
15032 /* If token is an already-parsed decltype not followed by ::,
15033 it's a simple-type-specifier. */
15034 if (token->type == CPP_DECLTYPE
15035 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15037 type = token->u.value;
15038 if (decl_specs)
15040 cp_parser_set_decl_spec_type (decl_specs, type,
15041 token,
15042 /*type_definition_p=*/false);
15043 /* Remember that we are handling a decltype in order to
15044 implement the resolution of DR 1510 when the argument
15045 isn't instantiation dependent. */
15046 decl_specs->decltype_p = true;
15048 cp_lexer_consume_token (parser->lexer);
15049 return type;
15052 /* If the type-specifier was for a built-in type, we're done. */
15053 if (type)
15055 /* Record the type. */
15056 if (decl_specs
15057 && (token->keyword != RID_SIGNED
15058 && token->keyword != RID_UNSIGNED
15059 && token->keyword != RID_SHORT
15060 && token->keyword != RID_LONG))
15061 cp_parser_set_decl_spec_type (decl_specs,
15062 type,
15063 token,
15064 /*type_definition_p=*/false);
15065 if (decl_specs)
15066 decl_specs->any_specifiers_p = true;
15068 /* Consume the token. */
15069 cp_lexer_consume_token (parser->lexer);
15071 if (type == error_mark_node)
15072 return error_mark_node;
15074 /* There is no valid C++ program where a non-template type is
15075 followed by a "<". That usually indicates that the user thought
15076 that the type was a template. */
15077 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15078 token->location);
15080 return TYPE_NAME (type);
15083 /* The type-specifier must be a user-defined type. */
15084 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15086 bool qualified_p;
15087 bool global_p;
15089 /* Don't gobble tokens or issue error messages if this is an
15090 optional type-specifier. */
15091 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15092 cp_parser_parse_tentatively (parser);
15094 /* Look for the optional `::' operator. */
15095 global_p
15096 = (cp_parser_global_scope_opt (parser,
15097 /*current_scope_valid_p=*/false)
15098 != NULL_TREE);
15099 /* Look for the nested-name specifier. */
15100 qualified_p
15101 = (cp_parser_nested_name_specifier_opt (parser,
15102 /*typename_keyword_p=*/false,
15103 /*check_dependency_p=*/true,
15104 /*type_p=*/false,
15105 /*is_declaration=*/false)
15106 != NULL_TREE);
15107 token = cp_lexer_peek_token (parser->lexer);
15108 /* If we have seen a nested-name-specifier, and the next token
15109 is `template', then we are using the template-id production. */
15110 if (parser->scope
15111 && cp_parser_optional_template_keyword (parser))
15113 /* Look for the template-id. */
15114 type = cp_parser_template_id (parser,
15115 /*template_keyword_p=*/true,
15116 /*check_dependency_p=*/true,
15117 none_type,
15118 /*is_declaration=*/false);
15119 /* If the template-id did not name a type, we are out of
15120 luck. */
15121 if (TREE_CODE (type) != TYPE_DECL)
15123 cp_parser_error (parser, "expected template-id for type");
15124 type = NULL_TREE;
15127 /* Otherwise, look for a type-name. */
15128 else
15129 type = cp_parser_type_name (parser);
15130 /* Keep track of all name-lookups performed in class scopes. */
15131 if (type
15132 && !global_p
15133 && !qualified_p
15134 && TREE_CODE (type) == TYPE_DECL
15135 && identifier_p (DECL_NAME (type)))
15136 maybe_note_name_used_in_class (DECL_NAME (type), type);
15137 /* If it didn't work out, we don't have a TYPE. */
15138 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15139 && !cp_parser_parse_definitely (parser))
15140 type = NULL_TREE;
15141 if (type && decl_specs)
15142 cp_parser_set_decl_spec_type (decl_specs, type,
15143 token,
15144 /*type_definition_p=*/false);
15147 /* If we didn't get a type-name, issue an error message. */
15148 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15150 cp_parser_error (parser, "expected type-name");
15151 return error_mark_node;
15154 if (type && type != error_mark_node)
15156 /* See if TYPE is an Objective-C type, and if so, parse and
15157 accept any protocol references following it. Do this before
15158 the cp_parser_check_for_invalid_template_id() call, because
15159 Objective-C types can be followed by '<...>' which would
15160 enclose protocol names rather than template arguments, and so
15161 everything is fine. */
15162 if (c_dialect_objc () && !parser->scope
15163 && (objc_is_id (type) || objc_is_class_name (type)))
15165 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15166 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15168 /* Clobber the "unqualified" type previously entered into
15169 DECL_SPECS with the new, improved protocol-qualified version. */
15170 if (decl_specs)
15171 decl_specs->type = qual_type;
15173 return qual_type;
15176 /* There is no valid C++ program where a non-template type is
15177 followed by a "<". That usually indicates that the user
15178 thought that the type was a template. */
15179 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15180 none_type,
15181 token->location);
15184 return type;
15187 /* Parse a type-name.
15189 type-name:
15190 class-name
15191 enum-name
15192 typedef-name
15193 simple-template-id [in c++0x]
15195 enum-name:
15196 identifier
15198 typedef-name:
15199 identifier
15201 Returns a TYPE_DECL for the type. */
15203 static tree
15204 cp_parser_type_name (cp_parser* parser)
15206 tree type_decl;
15208 /* We can't know yet whether it is a class-name or not. */
15209 cp_parser_parse_tentatively (parser);
15210 /* Try a class-name. */
15211 type_decl = cp_parser_class_name (parser,
15212 /*typename_keyword_p=*/false,
15213 /*template_keyword_p=*/false,
15214 none_type,
15215 /*check_dependency_p=*/true,
15216 /*class_head_p=*/false,
15217 /*is_declaration=*/false);
15218 /* If it's not a class-name, keep looking. */
15219 if (!cp_parser_parse_definitely (parser))
15221 if (cxx_dialect < cxx11)
15222 /* It must be a typedef-name or an enum-name. */
15223 return cp_parser_nonclass_name (parser);
15225 cp_parser_parse_tentatively (parser);
15226 /* It is either a simple-template-id representing an
15227 instantiation of an alias template... */
15228 type_decl = cp_parser_template_id (parser,
15229 /*template_keyword_p=*/false,
15230 /*check_dependency_p=*/true,
15231 none_type,
15232 /*is_declaration=*/false);
15233 /* Note that this must be an instantiation of an alias template
15234 because [temp.names]/6 says:
15236 A template-id that names an alias template specialization
15237 is a type-name.
15239 Whereas [temp.names]/7 says:
15241 A simple-template-id that names a class template
15242 specialization is a class-name. */
15243 if (type_decl != NULL_TREE
15244 && TREE_CODE (type_decl) == TYPE_DECL
15245 && TYPE_DECL_ALIAS_P (type_decl))
15246 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15247 else
15248 cp_parser_simulate_error (parser);
15250 if (!cp_parser_parse_definitely (parser))
15251 /* ... Or a typedef-name or an enum-name. */
15252 return cp_parser_nonclass_name (parser);
15255 return type_decl;
15258 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15260 enum-name:
15261 identifier
15263 typedef-name:
15264 identifier
15266 Returns a TYPE_DECL for the type. */
15268 static tree
15269 cp_parser_nonclass_name (cp_parser* parser)
15271 tree type_decl;
15272 tree identifier;
15274 cp_token *token = cp_lexer_peek_token (parser->lexer);
15275 identifier = cp_parser_identifier (parser);
15276 if (identifier == error_mark_node)
15277 return error_mark_node;
15279 /* Look up the type-name. */
15280 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15282 type_decl = strip_using_decl (type_decl);
15284 if (TREE_CODE (type_decl) != TYPE_DECL
15285 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15287 /* See if this is an Objective-C type. */
15288 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15289 tree type = objc_get_protocol_qualified_type (identifier, protos);
15290 if (type)
15291 type_decl = TYPE_NAME (type);
15294 /* Issue an error if we did not find a type-name. */
15295 if (TREE_CODE (type_decl) != TYPE_DECL
15296 /* In Objective-C, we have the complication that class names are
15297 normally type names and start declarations (eg, the
15298 "NSObject" in "NSObject *object;"), but can be used in an
15299 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15300 is an expression. So, a classname followed by a dot is not a
15301 valid type-name. */
15302 || (objc_is_class_name (TREE_TYPE (type_decl))
15303 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15305 if (!cp_parser_simulate_error (parser))
15306 cp_parser_name_lookup_error (parser, identifier, type_decl,
15307 NLE_TYPE, token->location);
15308 return error_mark_node;
15310 /* Remember that the name was used in the definition of the
15311 current class so that we can check later to see if the
15312 meaning would have been different after the class was
15313 entirely defined. */
15314 else if (type_decl != error_mark_node
15315 && !parser->scope)
15316 maybe_note_name_used_in_class (identifier, type_decl);
15318 return type_decl;
15321 /* Parse an elaborated-type-specifier. Note that the grammar given
15322 here incorporates the resolution to DR68.
15324 elaborated-type-specifier:
15325 class-key :: [opt] nested-name-specifier [opt] identifier
15326 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15327 enum-key :: [opt] nested-name-specifier [opt] identifier
15328 typename :: [opt] nested-name-specifier identifier
15329 typename :: [opt] nested-name-specifier template [opt]
15330 template-id
15332 GNU extension:
15334 elaborated-type-specifier:
15335 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15336 class-key attributes :: [opt] nested-name-specifier [opt]
15337 template [opt] template-id
15338 enum attributes :: [opt] nested-name-specifier [opt] identifier
15340 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15341 declared `friend'. If IS_DECLARATION is TRUE, then this
15342 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15343 something is being declared.
15345 Returns the TYPE specified. */
15347 static tree
15348 cp_parser_elaborated_type_specifier (cp_parser* parser,
15349 bool is_friend,
15350 bool is_declaration)
15352 enum tag_types tag_type;
15353 tree identifier;
15354 tree type = NULL_TREE;
15355 tree attributes = NULL_TREE;
15356 tree globalscope;
15357 cp_token *token = NULL;
15359 /* See if we're looking at the `enum' keyword. */
15360 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15362 /* Consume the `enum' token. */
15363 cp_lexer_consume_token (parser->lexer);
15364 /* Remember that it's an enumeration type. */
15365 tag_type = enum_type;
15366 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15367 enums) is used here. */
15368 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15369 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15371 pedwarn (input_location, 0, "elaborated-type-specifier "
15372 "for a scoped enum must not use the %<%D%> keyword",
15373 cp_lexer_peek_token (parser->lexer)->u.value);
15374 /* Consume the `struct' or `class' and parse it anyway. */
15375 cp_lexer_consume_token (parser->lexer);
15377 /* Parse the attributes. */
15378 attributes = cp_parser_attributes_opt (parser);
15380 /* Or, it might be `typename'. */
15381 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15382 RID_TYPENAME))
15384 /* Consume the `typename' token. */
15385 cp_lexer_consume_token (parser->lexer);
15386 /* Remember that it's a `typename' type. */
15387 tag_type = typename_type;
15389 /* Otherwise it must be a class-key. */
15390 else
15392 tag_type = cp_parser_class_key (parser);
15393 if (tag_type == none_type)
15394 return error_mark_node;
15395 /* Parse the attributes. */
15396 attributes = cp_parser_attributes_opt (parser);
15399 /* Look for the `::' operator. */
15400 globalscope = cp_parser_global_scope_opt (parser,
15401 /*current_scope_valid_p=*/false);
15402 /* Look for the nested-name-specifier. */
15403 if (tag_type == typename_type && !globalscope)
15405 if (!cp_parser_nested_name_specifier (parser,
15406 /*typename_keyword_p=*/true,
15407 /*check_dependency_p=*/true,
15408 /*type_p=*/true,
15409 is_declaration))
15410 return error_mark_node;
15412 else
15413 /* Even though `typename' is not present, the proposed resolution
15414 to Core Issue 180 says that in `class A<T>::B', `B' should be
15415 considered a type-name, even if `A<T>' is dependent. */
15416 cp_parser_nested_name_specifier_opt (parser,
15417 /*typename_keyword_p=*/true,
15418 /*check_dependency_p=*/true,
15419 /*type_p=*/true,
15420 is_declaration);
15421 /* For everything but enumeration types, consider a template-id.
15422 For an enumeration type, consider only a plain identifier. */
15423 if (tag_type != enum_type)
15425 bool template_p = false;
15426 tree decl;
15428 /* Allow the `template' keyword. */
15429 template_p = cp_parser_optional_template_keyword (parser);
15430 /* If we didn't see `template', we don't know if there's a
15431 template-id or not. */
15432 if (!template_p)
15433 cp_parser_parse_tentatively (parser);
15434 /* Parse the template-id. */
15435 token = cp_lexer_peek_token (parser->lexer);
15436 decl = cp_parser_template_id (parser, template_p,
15437 /*check_dependency_p=*/true,
15438 tag_type,
15439 is_declaration);
15440 /* If we didn't find a template-id, look for an ordinary
15441 identifier. */
15442 if (!template_p && !cp_parser_parse_definitely (parser))
15444 /* We can get here when cp_parser_template_id, called by
15445 cp_parser_class_name with tag_type == none_type, succeeds
15446 and caches a BASELINK. Then, when called again here,
15447 instead of failing and returning an error_mark_node
15448 returns it (see template/typename17.C in C++11).
15449 ??? Could we diagnose this earlier? */
15450 else if (tag_type == typename_type && BASELINK_P (decl))
15452 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15453 type = error_mark_node;
15455 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15456 in effect, then we must assume that, upon instantiation, the
15457 template will correspond to a class. */
15458 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15459 && tag_type == typename_type)
15460 type = make_typename_type (parser->scope, decl,
15461 typename_type,
15462 /*complain=*/tf_error);
15463 /* If the `typename' keyword is in effect and DECL is not a type
15464 decl, then type is non existent. */
15465 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15467 else if (TREE_CODE (decl) == TYPE_DECL)
15468 type = check_elaborated_type_specifier (tag_type, decl,
15469 /*allow_template_p=*/true);
15470 else if (decl == error_mark_node)
15471 type = error_mark_node;
15474 if (!type)
15476 token = cp_lexer_peek_token (parser->lexer);
15477 identifier = cp_parser_identifier (parser);
15479 if (identifier == error_mark_node)
15481 parser->scope = NULL_TREE;
15482 return error_mark_node;
15485 /* For a `typename', we needn't call xref_tag. */
15486 if (tag_type == typename_type
15487 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15488 return cp_parser_make_typename_type (parser, identifier,
15489 token->location);
15491 /* Template parameter lists apply only if we are not within a
15492 function parameter list. */
15493 bool template_parm_lists_apply
15494 = parser->num_template_parameter_lists;
15495 if (template_parm_lists_apply)
15496 for (cp_binding_level *s = current_binding_level;
15497 s && s->kind != sk_template_parms;
15498 s = s->level_chain)
15499 if (s->kind == sk_function_parms)
15500 template_parm_lists_apply = false;
15502 /* Look up a qualified name in the usual way. */
15503 if (parser->scope)
15505 tree decl;
15506 tree ambiguous_decls;
15508 decl = cp_parser_lookup_name (parser, identifier,
15509 tag_type,
15510 /*is_template=*/false,
15511 /*is_namespace=*/false,
15512 /*check_dependency=*/true,
15513 &ambiguous_decls,
15514 token->location);
15516 /* If the lookup was ambiguous, an error will already have been
15517 issued. */
15518 if (ambiguous_decls)
15519 return error_mark_node;
15521 /* If we are parsing friend declaration, DECL may be a
15522 TEMPLATE_DECL tree node here. However, we need to check
15523 whether this TEMPLATE_DECL results in valid code. Consider
15524 the following example:
15526 namespace N {
15527 template <class T> class C {};
15529 class X {
15530 template <class T> friend class N::C; // #1, valid code
15532 template <class T> class Y {
15533 friend class N::C; // #2, invalid code
15536 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15537 name lookup of `N::C'. We see that friend declaration must
15538 be template for the code to be valid. Note that
15539 processing_template_decl does not work here since it is
15540 always 1 for the above two cases. */
15542 decl = (cp_parser_maybe_treat_template_as_class
15543 (decl, /*tag_name_p=*/is_friend
15544 && template_parm_lists_apply));
15546 if (TREE_CODE (decl) != TYPE_DECL)
15548 cp_parser_diagnose_invalid_type_name (parser,
15549 identifier,
15550 token->location);
15551 return error_mark_node;
15554 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15556 bool allow_template = (template_parm_lists_apply
15557 || DECL_SELF_REFERENCE_P (decl));
15558 type = check_elaborated_type_specifier (tag_type, decl,
15559 allow_template);
15561 if (type == error_mark_node)
15562 return error_mark_node;
15565 /* Forward declarations of nested types, such as
15567 class C1::C2;
15568 class C1::C2::C3;
15570 are invalid unless all components preceding the final '::'
15571 are complete. If all enclosing types are complete, these
15572 declarations become merely pointless.
15574 Invalid forward declarations of nested types are errors
15575 caught elsewhere in parsing. Those that are pointless arrive
15576 here. */
15578 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15579 && !is_friend && !processing_explicit_instantiation)
15580 warning (0, "declaration %qD does not declare anything", decl);
15582 type = TREE_TYPE (decl);
15584 else
15586 /* An elaborated-type-specifier sometimes introduces a new type and
15587 sometimes names an existing type. Normally, the rule is that it
15588 introduces a new type only if there is not an existing type of
15589 the same name already in scope. For example, given:
15591 struct S {};
15592 void f() { struct S s; }
15594 the `struct S' in the body of `f' is the same `struct S' as in
15595 the global scope; the existing definition is used. However, if
15596 there were no global declaration, this would introduce a new
15597 local class named `S'.
15599 An exception to this rule applies to the following code:
15601 namespace N { struct S; }
15603 Here, the elaborated-type-specifier names a new type
15604 unconditionally; even if there is already an `S' in the
15605 containing scope this declaration names a new type.
15606 This exception only applies if the elaborated-type-specifier
15607 forms the complete declaration:
15609 [class.name]
15611 A declaration consisting solely of `class-key identifier ;' is
15612 either a redeclaration of the name in the current scope or a
15613 forward declaration of the identifier as a class name. It
15614 introduces the name into the current scope.
15616 We are in this situation precisely when the next token is a `;'.
15618 An exception to the exception is that a `friend' declaration does
15619 *not* name a new type; i.e., given:
15621 struct S { friend struct T; };
15623 `T' is not a new type in the scope of `S'.
15625 Also, `new struct S' or `sizeof (struct S)' never results in the
15626 definition of a new type; a new type can only be declared in a
15627 declaration context. */
15629 tag_scope ts;
15630 bool template_p;
15632 if (is_friend)
15633 /* Friends have special name lookup rules. */
15634 ts = ts_within_enclosing_non_class;
15635 else if (is_declaration
15636 && cp_lexer_next_token_is (parser->lexer,
15637 CPP_SEMICOLON))
15638 /* This is a `class-key identifier ;' */
15639 ts = ts_current;
15640 else
15641 ts = ts_global;
15643 template_p =
15644 (template_parm_lists_apply
15645 && (cp_parser_next_token_starts_class_definition_p (parser)
15646 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15647 /* An unqualified name was used to reference this type, so
15648 there were no qualifying templates. */
15649 if (template_parm_lists_apply
15650 && !cp_parser_check_template_parameters (parser,
15651 /*num_templates=*/0,
15652 token->location,
15653 /*declarator=*/NULL))
15654 return error_mark_node;
15655 type = xref_tag (tag_type, identifier, ts, template_p);
15659 if (type == error_mark_node)
15660 return error_mark_node;
15662 /* Allow attributes on forward declarations of classes. */
15663 if (attributes)
15665 if (TREE_CODE (type) == TYPENAME_TYPE)
15666 warning (OPT_Wattributes,
15667 "attributes ignored on uninstantiated type");
15668 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15669 && ! processing_explicit_instantiation)
15670 warning (OPT_Wattributes,
15671 "attributes ignored on template instantiation");
15672 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15673 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15674 else
15675 warning (OPT_Wattributes,
15676 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15679 if (tag_type != enum_type)
15681 /* Indicate whether this class was declared as a `class' or as a
15682 `struct'. */
15683 if (TREE_CODE (type) == RECORD_TYPE)
15684 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15685 cp_parser_check_class_key (tag_type, type);
15688 /* A "<" cannot follow an elaborated type specifier. If that
15689 happens, the user was probably trying to form a template-id. */
15690 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15691 token->location);
15693 return type;
15696 /* Parse an enum-specifier.
15698 enum-specifier:
15699 enum-head { enumerator-list [opt] }
15700 enum-head { enumerator-list , } [C++0x]
15702 enum-head:
15703 enum-key identifier [opt] enum-base [opt]
15704 enum-key nested-name-specifier identifier enum-base [opt]
15706 enum-key:
15707 enum
15708 enum class [C++0x]
15709 enum struct [C++0x]
15711 enum-base: [C++0x]
15712 : type-specifier-seq
15714 opaque-enum-specifier:
15715 enum-key identifier enum-base [opt] ;
15717 GNU Extensions:
15718 enum-key attributes[opt] identifier [opt] enum-base [opt]
15719 { enumerator-list [opt] }attributes[opt]
15720 enum-key attributes[opt] identifier [opt] enum-base [opt]
15721 { enumerator-list, }attributes[opt] [C++0x]
15723 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15724 if the token stream isn't an enum-specifier after all. */
15726 static tree
15727 cp_parser_enum_specifier (cp_parser* parser)
15729 tree identifier;
15730 tree type = NULL_TREE;
15731 tree prev_scope;
15732 tree nested_name_specifier = NULL_TREE;
15733 tree attributes;
15734 bool scoped_enum_p = false;
15735 bool has_underlying_type = false;
15736 bool nested_being_defined = false;
15737 bool new_value_list = false;
15738 bool is_new_type = false;
15739 bool is_anonymous = false;
15740 tree underlying_type = NULL_TREE;
15741 cp_token *type_start_token = NULL;
15742 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15744 parser->colon_corrects_to_scope_p = false;
15746 /* Parse tentatively so that we can back up if we don't find a
15747 enum-specifier. */
15748 cp_parser_parse_tentatively (parser);
15750 /* Caller guarantees that the current token is 'enum', an identifier
15751 possibly follows, and the token after that is an opening brace.
15752 If we don't have an identifier, fabricate an anonymous name for
15753 the enumeration being defined. */
15754 cp_lexer_consume_token (parser->lexer);
15756 /* Parse the "class" or "struct", which indicates a scoped
15757 enumeration type in C++0x. */
15758 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15759 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15761 if (cxx_dialect < cxx11)
15762 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15764 /* Consume the `struct' or `class' token. */
15765 cp_lexer_consume_token (parser->lexer);
15767 scoped_enum_p = true;
15770 attributes = cp_parser_attributes_opt (parser);
15772 /* Clear the qualification. */
15773 parser->scope = NULL_TREE;
15774 parser->qualifying_scope = NULL_TREE;
15775 parser->object_scope = NULL_TREE;
15777 /* Figure out in what scope the declaration is being placed. */
15778 prev_scope = current_scope ();
15780 type_start_token = cp_lexer_peek_token (parser->lexer);
15782 push_deferring_access_checks (dk_no_check);
15783 nested_name_specifier
15784 = cp_parser_nested_name_specifier_opt (parser,
15785 /*typename_keyword_p=*/true,
15786 /*check_dependency_p=*/false,
15787 /*type_p=*/false,
15788 /*is_declaration=*/false);
15790 if (nested_name_specifier)
15792 tree name;
15794 identifier = cp_parser_identifier (parser);
15795 name = cp_parser_lookup_name (parser, identifier,
15796 enum_type,
15797 /*is_template=*/false,
15798 /*is_namespace=*/false,
15799 /*check_dependency=*/true,
15800 /*ambiguous_decls=*/NULL,
15801 input_location);
15802 if (name && name != error_mark_node)
15804 type = TREE_TYPE (name);
15805 if (TREE_CODE (type) == TYPENAME_TYPE)
15807 /* Are template enums allowed in ISO? */
15808 if (template_parm_scope_p ())
15809 pedwarn (type_start_token->location, OPT_Wpedantic,
15810 "%qD is an enumeration template", name);
15811 /* ignore a typename reference, for it will be solved by name
15812 in start_enum. */
15813 type = NULL_TREE;
15816 else if (nested_name_specifier == error_mark_node)
15817 /* We already issued an error. */;
15818 else
15819 error_at (type_start_token->location,
15820 "%qD is not an enumerator-name", identifier);
15822 else
15824 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15825 identifier = cp_parser_identifier (parser);
15826 else
15828 identifier = make_anon_name ();
15829 is_anonymous = true;
15830 if (scoped_enum_p)
15831 error_at (type_start_token->location,
15832 "anonymous scoped enum is not allowed");
15835 pop_deferring_access_checks ();
15837 /* Check for the `:' that denotes a specified underlying type in C++0x.
15838 Note that a ':' could also indicate a bitfield width, however. */
15839 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15841 cp_decl_specifier_seq type_specifiers;
15843 /* Consume the `:'. */
15844 cp_lexer_consume_token (parser->lexer);
15846 /* Parse the type-specifier-seq. */
15847 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15848 /*is_trailing_return=*/false,
15849 &type_specifiers);
15851 /* At this point this is surely not elaborated type specifier. */
15852 if (!cp_parser_parse_definitely (parser))
15853 return NULL_TREE;
15855 if (cxx_dialect < cxx11)
15856 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15858 has_underlying_type = true;
15860 /* If that didn't work, stop. */
15861 if (type_specifiers.type != error_mark_node)
15863 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15864 /*initialized=*/0, NULL);
15865 if (underlying_type == error_mark_node
15866 || check_for_bare_parameter_packs (underlying_type))
15867 underlying_type = NULL_TREE;
15871 /* Look for the `{' but don't consume it yet. */
15872 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15874 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15876 cp_parser_error (parser, "expected %<{%>");
15877 if (has_underlying_type)
15879 type = NULL_TREE;
15880 goto out;
15883 /* An opaque-enum-specifier must have a ';' here. */
15884 if ((scoped_enum_p || underlying_type)
15885 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15887 cp_parser_error (parser, "expected %<;%> or %<{%>");
15888 if (has_underlying_type)
15890 type = NULL_TREE;
15891 goto out;
15896 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15897 return NULL_TREE;
15899 if (nested_name_specifier)
15901 if (CLASS_TYPE_P (nested_name_specifier))
15903 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15904 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15905 push_scope (nested_name_specifier);
15907 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15909 push_nested_namespace (nested_name_specifier);
15913 /* Issue an error message if type-definitions are forbidden here. */
15914 if (!cp_parser_check_type_definition (parser))
15915 type = error_mark_node;
15916 else
15917 /* Create the new type. We do this before consuming the opening
15918 brace so the enum will be recorded as being on the line of its
15919 tag (or the 'enum' keyword, if there is no tag). */
15920 type = start_enum (identifier, type, underlying_type,
15921 scoped_enum_p, &is_new_type);
15923 /* If the next token is not '{' it is an opaque-enum-specifier or an
15924 elaborated-type-specifier. */
15925 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15927 timevar_push (TV_PARSE_ENUM);
15928 if (nested_name_specifier
15929 && nested_name_specifier != error_mark_node)
15931 /* The following catches invalid code such as:
15932 enum class S<int>::E { A, B, C }; */
15933 if (!processing_specialization
15934 && CLASS_TYPE_P (nested_name_specifier)
15935 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15936 error_at (type_start_token->location, "cannot add an enumerator "
15937 "list to a template instantiation");
15939 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15941 error_at (type_start_token->location,
15942 "%<%T::%E%> has not been declared",
15943 TYPE_CONTEXT (nested_name_specifier),
15944 nested_name_specifier);
15945 type = error_mark_node;
15947 /* If that scope does not contain the scope in which the
15948 class was originally declared, the program is invalid. */
15949 else if (prev_scope && !is_ancestor (prev_scope,
15950 nested_name_specifier))
15952 if (at_namespace_scope_p ())
15953 error_at (type_start_token->location,
15954 "declaration of %qD in namespace %qD which does not "
15955 "enclose %qD",
15956 type, prev_scope, nested_name_specifier);
15957 else
15958 error_at (type_start_token->location,
15959 "declaration of %qD in %qD which does not "
15960 "enclose %qD",
15961 type, prev_scope, nested_name_specifier);
15962 type = error_mark_node;
15966 if (scoped_enum_p)
15967 begin_scope (sk_scoped_enum, type);
15969 /* Consume the opening brace. */
15970 cp_lexer_consume_token (parser->lexer);
15972 if (type == error_mark_node)
15973 ; /* Nothing to add */
15974 else if (OPAQUE_ENUM_P (type)
15975 || (cxx_dialect > cxx98 && processing_specialization))
15977 new_value_list = true;
15978 SET_OPAQUE_ENUM_P (type, false);
15979 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15981 else
15983 error_at (type_start_token->location,
15984 "multiple definition of %q#T", type);
15985 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15986 "previous definition here");
15987 type = error_mark_node;
15990 if (type == error_mark_node)
15991 cp_parser_skip_to_end_of_block_or_statement (parser);
15992 /* If the next token is not '}', then there are some enumerators. */
15993 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15995 if (is_anonymous && !scoped_enum_p)
15996 pedwarn (type_start_token->location, OPT_Wpedantic,
15997 "ISO C++ forbids empty anonymous enum");
15999 else
16000 cp_parser_enumerator_list (parser, type);
16002 /* Consume the final '}'. */
16003 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16005 if (scoped_enum_p)
16006 finish_scope ();
16007 timevar_pop (TV_PARSE_ENUM);
16009 else
16011 /* If a ';' follows, then it is an opaque-enum-specifier
16012 and additional restrictions apply. */
16013 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16015 if (is_anonymous)
16016 error_at (type_start_token->location,
16017 "opaque-enum-specifier without name");
16018 else if (nested_name_specifier)
16019 error_at (type_start_token->location,
16020 "opaque-enum-specifier must use a simple identifier");
16024 /* Look for trailing attributes to apply to this enumeration, and
16025 apply them if appropriate. */
16026 if (cp_parser_allow_gnu_extensions_p (parser))
16028 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16029 trailing_attr = chainon (trailing_attr, attributes);
16030 cplus_decl_attributes (&type,
16031 trailing_attr,
16032 (int) ATTR_FLAG_TYPE_IN_PLACE);
16035 /* Finish up the enumeration. */
16036 if (type != error_mark_node)
16038 if (new_value_list)
16039 finish_enum_value_list (type);
16040 if (is_new_type)
16041 finish_enum (type);
16044 if (nested_name_specifier)
16046 if (CLASS_TYPE_P (nested_name_specifier))
16048 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16049 pop_scope (nested_name_specifier);
16051 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16053 pop_nested_namespace (nested_name_specifier);
16056 out:
16057 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16058 return type;
16061 /* Parse an enumerator-list. The enumerators all have the indicated
16062 TYPE.
16064 enumerator-list:
16065 enumerator-definition
16066 enumerator-list , enumerator-definition */
16068 static void
16069 cp_parser_enumerator_list (cp_parser* parser, tree type)
16071 while (true)
16073 /* Parse an enumerator-definition. */
16074 cp_parser_enumerator_definition (parser, type);
16076 /* If the next token is not a ',', we've reached the end of
16077 the list. */
16078 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16079 break;
16080 /* Otherwise, consume the `,' and keep going. */
16081 cp_lexer_consume_token (parser->lexer);
16082 /* If the next token is a `}', there is a trailing comma. */
16083 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16085 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16086 pedwarn (input_location, OPT_Wpedantic,
16087 "comma at end of enumerator list");
16088 break;
16093 /* Parse an enumerator-definition. The enumerator has the indicated
16094 TYPE.
16096 enumerator-definition:
16097 enumerator
16098 enumerator = constant-expression
16100 enumerator:
16101 identifier
16103 GNU Extensions:
16105 enumerator-definition:
16106 enumerator attributes [opt]
16107 enumerator attributes [opt] = constant-expression */
16109 static void
16110 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16112 tree identifier;
16113 tree value;
16114 location_t loc;
16116 /* Save the input location because we are interested in the location
16117 of the identifier and not the location of the explicit value. */
16118 loc = cp_lexer_peek_token (parser->lexer)->location;
16120 /* Look for the identifier. */
16121 identifier = cp_parser_identifier (parser);
16122 if (identifier == error_mark_node)
16123 return;
16125 /* Parse any specified attributes. */
16126 tree attrs = cp_parser_attributes_opt (parser);
16128 /* If the next token is an '=', then there is an explicit value. */
16129 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16131 /* Consume the `=' token. */
16132 cp_lexer_consume_token (parser->lexer);
16133 /* Parse the value. */
16134 value = cp_parser_constant_expression (parser);
16136 else
16137 value = NULL_TREE;
16139 /* If we are processing a template, make sure the initializer of the
16140 enumerator doesn't contain any bare template parameter pack. */
16141 if (check_for_bare_parameter_packs (value))
16142 value = error_mark_node;
16144 /* Create the enumerator. */
16145 build_enumerator (identifier, value, type, attrs, loc);
16148 /* Parse a namespace-name.
16150 namespace-name:
16151 original-namespace-name
16152 namespace-alias
16154 Returns the NAMESPACE_DECL for the namespace. */
16156 static tree
16157 cp_parser_namespace_name (cp_parser* parser)
16159 tree identifier;
16160 tree namespace_decl;
16162 cp_token *token = cp_lexer_peek_token (parser->lexer);
16164 /* Get the name of the namespace. */
16165 identifier = cp_parser_identifier (parser);
16166 if (identifier == error_mark_node)
16167 return error_mark_node;
16169 /* Look up the identifier in the currently active scope. Look only
16170 for namespaces, due to:
16172 [basic.lookup.udir]
16174 When looking up a namespace-name in a using-directive or alias
16175 definition, only namespace names are considered.
16177 And:
16179 [basic.lookup.qual]
16181 During the lookup of a name preceding the :: scope resolution
16182 operator, object, function, and enumerator names are ignored.
16184 (Note that cp_parser_qualifying_entity only calls this
16185 function if the token after the name is the scope resolution
16186 operator.) */
16187 namespace_decl = cp_parser_lookup_name (parser, identifier,
16188 none_type,
16189 /*is_template=*/false,
16190 /*is_namespace=*/true,
16191 /*check_dependency=*/true,
16192 /*ambiguous_decls=*/NULL,
16193 token->location);
16194 /* If it's not a namespace, issue an error. */
16195 if (namespace_decl == error_mark_node
16196 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16198 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16199 error_at (token->location, "%qD is not a namespace-name", identifier);
16200 cp_parser_error (parser, "expected namespace-name");
16201 namespace_decl = error_mark_node;
16204 return namespace_decl;
16207 /* Parse a namespace-definition.
16209 namespace-definition:
16210 named-namespace-definition
16211 unnamed-namespace-definition
16213 named-namespace-definition:
16214 original-namespace-definition
16215 extension-namespace-definition
16217 original-namespace-definition:
16218 namespace identifier { namespace-body }
16220 extension-namespace-definition:
16221 namespace original-namespace-name { namespace-body }
16223 unnamed-namespace-definition:
16224 namespace { namespace-body } */
16226 static void
16227 cp_parser_namespace_definition (cp_parser* parser)
16229 tree identifier, attribs;
16230 bool has_visibility;
16231 bool is_inline;
16233 cp_ensure_no_omp_declare_simd (parser);
16234 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16236 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16237 is_inline = true;
16238 cp_lexer_consume_token (parser->lexer);
16240 else
16241 is_inline = false;
16243 /* Look for the `namespace' keyword. */
16244 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16246 /* Get the name of the namespace. We do not attempt to distinguish
16247 between an original-namespace-definition and an
16248 extension-namespace-definition at this point. The semantic
16249 analysis routines are responsible for that. */
16250 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16251 identifier = cp_parser_identifier (parser);
16252 else
16253 identifier = NULL_TREE;
16255 /* Parse any specified attributes. */
16256 attribs = cp_parser_attributes_opt (parser);
16258 /* Look for the `{' to start the namespace. */
16259 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16260 /* Start the namespace. */
16261 push_namespace (identifier);
16263 /* "inline namespace" is equivalent to a stub namespace definition
16264 followed by a strong using directive. */
16265 if (is_inline)
16267 tree name_space = current_namespace;
16268 /* Set up namespace association. */
16269 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16270 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16271 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16272 /* Import the contents of the inline namespace. */
16273 pop_namespace ();
16274 do_using_directive (name_space);
16275 push_namespace (identifier);
16278 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16280 /* Parse the body of the namespace. */
16281 cp_parser_namespace_body (parser);
16283 if (has_visibility)
16284 pop_visibility (1);
16286 /* Finish the namespace. */
16287 pop_namespace ();
16288 /* Look for the final `}'. */
16289 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16292 /* Parse a namespace-body.
16294 namespace-body:
16295 declaration-seq [opt] */
16297 static void
16298 cp_parser_namespace_body (cp_parser* parser)
16300 cp_parser_declaration_seq_opt (parser);
16303 /* Parse a namespace-alias-definition.
16305 namespace-alias-definition:
16306 namespace identifier = qualified-namespace-specifier ; */
16308 static void
16309 cp_parser_namespace_alias_definition (cp_parser* parser)
16311 tree identifier;
16312 tree namespace_specifier;
16314 cp_token *token = cp_lexer_peek_token (parser->lexer);
16316 /* Look for the `namespace' keyword. */
16317 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16318 /* Look for the identifier. */
16319 identifier = cp_parser_identifier (parser);
16320 if (identifier == error_mark_node)
16321 return;
16322 /* Look for the `=' token. */
16323 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16324 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16326 error_at (token->location, "%<namespace%> definition is not allowed here");
16327 /* Skip the definition. */
16328 cp_lexer_consume_token (parser->lexer);
16329 if (cp_parser_skip_to_closing_brace (parser))
16330 cp_lexer_consume_token (parser->lexer);
16331 return;
16333 cp_parser_require (parser, CPP_EQ, RT_EQ);
16334 /* Look for the qualified-namespace-specifier. */
16335 namespace_specifier
16336 = cp_parser_qualified_namespace_specifier (parser);
16337 /* Look for the `;' token. */
16338 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16340 /* Register the alias in the symbol table. */
16341 do_namespace_alias (identifier, namespace_specifier);
16344 /* Parse a qualified-namespace-specifier.
16346 qualified-namespace-specifier:
16347 :: [opt] nested-name-specifier [opt] namespace-name
16349 Returns a NAMESPACE_DECL corresponding to the specified
16350 namespace. */
16352 static tree
16353 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16355 /* Look for the optional `::'. */
16356 cp_parser_global_scope_opt (parser,
16357 /*current_scope_valid_p=*/false);
16359 /* Look for the optional nested-name-specifier. */
16360 cp_parser_nested_name_specifier_opt (parser,
16361 /*typename_keyword_p=*/false,
16362 /*check_dependency_p=*/true,
16363 /*type_p=*/false,
16364 /*is_declaration=*/true);
16366 return cp_parser_namespace_name (parser);
16369 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16370 access declaration.
16372 using-declaration:
16373 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16374 using :: unqualified-id ;
16376 access-declaration:
16377 qualified-id ;
16381 static bool
16382 cp_parser_using_declaration (cp_parser* parser,
16383 bool access_declaration_p)
16385 cp_token *token;
16386 bool typename_p = false;
16387 bool global_scope_p;
16388 tree decl;
16389 tree identifier;
16390 tree qscope;
16391 int oldcount = errorcount;
16392 cp_token *diag_token = NULL;
16394 if (access_declaration_p)
16396 diag_token = cp_lexer_peek_token (parser->lexer);
16397 cp_parser_parse_tentatively (parser);
16399 else
16401 /* Look for the `using' keyword. */
16402 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16404 /* Peek at the next token. */
16405 token = cp_lexer_peek_token (parser->lexer);
16406 /* See if it's `typename'. */
16407 if (token->keyword == RID_TYPENAME)
16409 /* Remember that we've seen it. */
16410 typename_p = true;
16411 /* Consume the `typename' token. */
16412 cp_lexer_consume_token (parser->lexer);
16416 /* Look for the optional global scope qualification. */
16417 global_scope_p
16418 = (cp_parser_global_scope_opt (parser,
16419 /*current_scope_valid_p=*/false)
16420 != NULL_TREE);
16422 /* If we saw `typename', or didn't see `::', then there must be a
16423 nested-name-specifier present. */
16424 if (typename_p || !global_scope_p)
16426 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16427 /*check_dependency_p=*/true,
16428 /*type_p=*/false,
16429 /*is_declaration=*/true);
16430 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16432 cp_parser_skip_to_end_of_block_or_statement (parser);
16433 return false;
16436 /* Otherwise, we could be in either of the two productions. In that
16437 case, treat the nested-name-specifier as optional. */
16438 else
16439 qscope = cp_parser_nested_name_specifier_opt (parser,
16440 /*typename_keyword_p=*/false,
16441 /*check_dependency_p=*/true,
16442 /*type_p=*/false,
16443 /*is_declaration=*/true);
16444 if (!qscope)
16445 qscope = global_namespace;
16446 else if (UNSCOPED_ENUM_P (qscope))
16447 qscope = CP_TYPE_CONTEXT (qscope);
16449 if (access_declaration_p && cp_parser_error_occurred (parser))
16450 /* Something has already gone wrong; there's no need to parse
16451 further. Since an error has occurred, the return value of
16452 cp_parser_parse_definitely will be false, as required. */
16453 return cp_parser_parse_definitely (parser);
16455 token = cp_lexer_peek_token (parser->lexer);
16456 /* Parse the unqualified-id. */
16457 identifier = cp_parser_unqualified_id (parser,
16458 /*template_keyword_p=*/false,
16459 /*check_dependency_p=*/true,
16460 /*declarator_p=*/true,
16461 /*optional_p=*/false);
16463 if (access_declaration_p)
16465 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16466 cp_parser_simulate_error (parser);
16467 if (!cp_parser_parse_definitely (parser))
16468 return false;
16471 /* The function we call to handle a using-declaration is different
16472 depending on what scope we are in. */
16473 if (qscope == error_mark_node || identifier == error_mark_node)
16475 else if (!identifier_p (identifier)
16476 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16477 /* [namespace.udecl]
16479 A using declaration shall not name a template-id. */
16480 error_at (token->location,
16481 "a template-id may not appear in a using-declaration");
16482 else
16484 if (at_class_scope_p ())
16486 /* Create the USING_DECL. */
16487 decl = do_class_using_decl (parser->scope, identifier);
16489 if (decl && typename_p)
16490 USING_DECL_TYPENAME_P (decl) = 1;
16492 if (check_for_bare_parameter_packs (decl))
16494 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16495 return false;
16497 else
16498 /* Add it to the list of members in this class. */
16499 finish_member_declaration (decl);
16501 else
16503 decl = cp_parser_lookup_name_simple (parser,
16504 identifier,
16505 token->location);
16506 if (decl == error_mark_node)
16507 cp_parser_name_lookup_error (parser, identifier,
16508 decl, NLE_NULL,
16509 token->location);
16510 else if (check_for_bare_parameter_packs (decl))
16512 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16513 return false;
16515 else if (!at_namespace_scope_p ())
16516 do_local_using_decl (decl, qscope, identifier);
16517 else
16518 do_toplevel_using_decl (decl, qscope, identifier);
16522 /* Look for the final `;'. */
16523 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16525 if (access_declaration_p && errorcount == oldcount)
16526 warning_at (diag_token->location, OPT_Wdeprecated,
16527 "access declarations are deprecated "
16528 "in favour of using-declarations; "
16529 "suggestion: add the %<using%> keyword");
16531 return true;
16534 /* Parse an alias-declaration.
16536 alias-declaration:
16537 using identifier attribute-specifier-seq [opt] = type-id */
16539 static tree
16540 cp_parser_alias_declaration (cp_parser* parser)
16542 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16543 location_t id_location;
16544 cp_declarator *declarator;
16545 cp_decl_specifier_seq decl_specs;
16546 bool member_p;
16547 const char *saved_message = NULL;
16549 /* Look for the `using' keyword. */
16550 cp_token *using_token
16551 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16552 if (using_token == NULL)
16553 return error_mark_node;
16555 id_location = cp_lexer_peek_token (parser->lexer)->location;
16556 id = cp_parser_identifier (parser);
16557 if (id == error_mark_node)
16558 return error_mark_node;
16560 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16561 attributes = cp_parser_attributes_opt (parser);
16562 if (attributes == error_mark_node)
16563 return error_mark_node;
16565 cp_parser_require (parser, CPP_EQ, RT_EQ);
16567 if (cp_parser_error_occurred (parser))
16568 return error_mark_node;
16570 cp_parser_commit_to_tentative_parse (parser);
16572 /* Now we are going to parse the type-id of the declaration. */
16575 [dcl.type]/3 says:
16577 "A type-specifier-seq shall not define a class or enumeration
16578 unless it appears in the type-id of an alias-declaration (7.1.3) that
16579 is not the declaration of a template-declaration."
16581 In other words, if we currently are in an alias template, the
16582 type-id should not define a type.
16584 So let's set parser->type_definition_forbidden_message in that
16585 case; cp_parser_check_type_definition (called by
16586 cp_parser_class_specifier) will then emit an error if a type is
16587 defined in the type-id. */
16588 if (parser->num_template_parameter_lists)
16590 saved_message = parser->type_definition_forbidden_message;
16591 parser->type_definition_forbidden_message =
16592 G_("types may not be defined in alias template declarations");
16595 type = cp_parser_type_id (parser);
16597 /* Restore the error message if need be. */
16598 if (parser->num_template_parameter_lists)
16599 parser->type_definition_forbidden_message = saved_message;
16601 if (type == error_mark_node
16602 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16604 cp_parser_skip_to_end_of_block_or_statement (parser);
16605 return error_mark_node;
16608 /* A typedef-name can also be introduced by an alias-declaration. The
16609 identifier following the using keyword becomes a typedef-name. It has
16610 the same semantics as if it were introduced by the typedef
16611 specifier. In particular, it does not define a new type and it shall
16612 not appear in the type-id. */
16614 clear_decl_specs (&decl_specs);
16615 decl_specs.type = type;
16616 if (attributes != NULL_TREE)
16618 decl_specs.attributes = attributes;
16619 set_and_check_decl_spec_loc (&decl_specs,
16620 ds_attribute,
16621 attrs_token);
16623 set_and_check_decl_spec_loc (&decl_specs,
16624 ds_typedef,
16625 using_token);
16626 set_and_check_decl_spec_loc (&decl_specs,
16627 ds_alias,
16628 using_token);
16630 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16631 declarator->id_loc = id_location;
16633 member_p = at_class_scope_p ();
16634 if (member_p)
16635 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16636 NULL_TREE, attributes);
16637 else
16638 decl = start_decl (declarator, &decl_specs, 0,
16639 attributes, NULL_TREE, &pushed_scope);
16640 if (decl == error_mark_node)
16641 return decl;
16643 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16645 if (pushed_scope)
16646 pop_scope (pushed_scope);
16648 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16649 added into the symbol table; otherwise, return the TYPE_DECL. */
16650 if (DECL_LANG_SPECIFIC (decl)
16651 && DECL_TEMPLATE_INFO (decl)
16652 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16654 decl = DECL_TI_TEMPLATE (decl);
16655 if (member_p)
16656 check_member_template (decl);
16659 return decl;
16662 /* Parse a using-directive.
16664 using-directive:
16665 using namespace :: [opt] nested-name-specifier [opt]
16666 namespace-name ; */
16668 static void
16669 cp_parser_using_directive (cp_parser* parser)
16671 tree namespace_decl;
16672 tree attribs;
16674 /* Look for the `using' keyword. */
16675 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16676 /* And the `namespace' keyword. */
16677 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16678 /* Look for the optional `::' operator. */
16679 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16680 /* And the optional nested-name-specifier. */
16681 cp_parser_nested_name_specifier_opt (parser,
16682 /*typename_keyword_p=*/false,
16683 /*check_dependency_p=*/true,
16684 /*type_p=*/false,
16685 /*is_declaration=*/true);
16686 /* Get the namespace being used. */
16687 namespace_decl = cp_parser_namespace_name (parser);
16688 /* And any specified attributes. */
16689 attribs = cp_parser_attributes_opt (parser);
16690 /* Update the symbol table. */
16691 parse_using_directive (namespace_decl, attribs);
16692 /* Look for the final `;'. */
16693 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16696 /* Parse an asm-definition.
16698 asm-definition:
16699 asm ( string-literal ) ;
16701 GNU Extension:
16703 asm-definition:
16704 asm volatile [opt] ( string-literal ) ;
16705 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16706 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16707 : asm-operand-list [opt] ) ;
16708 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16709 : asm-operand-list [opt]
16710 : asm-clobber-list [opt] ) ;
16711 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16712 : asm-clobber-list [opt]
16713 : asm-goto-list ) ; */
16715 static void
16716 cp_parser_asm_definition (cp_parser* parser)
16718 tree string;
16719 tree outputs = NULL_TREE;
16720 tree inputs = NULL_TREE;
16721 tree clobbers = NULL_TREE;
16722 tree labels = NULL_TREE;
16723 tree asm_stmt;
16724 bool volatile_p = false;
16725 bool extended_p = false;
16726 bool invalid_inputs_p = false;
16727 bool invalid_outputs_p = false;
16728 bool goto_p = false;
16729 required_token missing = RT_NONE;
16731 /* Look for the `asm' keyword. */
16732 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16734 if (parser->in_function_body
16735 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16737 error ("%<asm%> in %<constexpr%> function");
16738 cp_function_chain->invalid_constexpr = true;
16741 /* See if the next token is `volatile'. */
16742 if (cp_parser_allow_gnu_extensions_p (parser)
16743 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16745 /* Remember that we saw the `volatile' keyword. */
16746 volatile_p = true;
16747 /* Consume the token. */
16748 cp_lexer_consume_token (parser->lexer);
16750 if (cp_parser_allow_gnu_extensions_p (parser)
16751 && parser->in_function_body
16752 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16754 /* Remember that we saw the `goto' keyword. */
16755 goto_p = true;
16756 /* Consume the token. */
16757 cp_lexer_consume_token (parser->lexer);
16759 /* Look for the opening `('. */
16760 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16761 return;
16762 /* Look for the string. */
16763 string = cp_parser_string_literal (parser, false, false);
16764 if (string == error_mark_node)
16766 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16767 /*consume_paren=*/true);
16768 return;
16771 /* If we're allowing GNU extensions, check for the extended assembly
16772 syntax. Unfortunately, the `:' tokens need not be separated by
16773 a space in C, and so, for compatibility, we tolerate that here
16774 too. Doing that means that we have to treat the `::' operator as
16775 two `:' tokens. */
16776 if (cp_parser_allow_gnu_extensions_p (parser)
16777 && parser->in_function_body
16778 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16779 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16781 bool inputs_p = false;
16782 bool clobbers_p = false;
16783 bool labels_p = false;
16785 /* The extended syntax was used. */
16786 extended_p = true;
16788 /* Look for outputs. */
16789 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16791 /* Consume the `:'. */
16792 cp_lexer_consume_token (parser->lexer);
16793 /* Parse the output-operands. */
16794 if (cp_lexer_next_token_is_not (parser->lexer,
16795 CPP_COLON)
16796 && cp_lexer_next_token_is_not (parser->lexer,
16797 CPP_SCOPE)
16798 && cp_lexer_next_token_is_not (parser->lexer,
16799 CPP_CLOSE_PAREN)
16800 && !goto_p)
16802 outputs = cp_parser_asm_operand_list (parser);
16803 if (outputs == error_mark_node)
16804 invalid_outputs_p = true;
16807 /* If the next token is `::', there are no outputs, and the
16808 next token is the beginning of the inputs. */
16809 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16810 /* The inputs are coming next. */
16811 inputs_p = true;
16813 /* Look for inputs. */
16814 if (inputs_p
16815 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16817 /* Consume the `:' or `::'. */
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))
16827 inputs = cp_parser_asm_operand_list (parser);
16828 if (inputs == error_mark_node)
16829 invalid_inputs_p = true;
16832 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16833 /* The clobbers are coming next. */
16834 clobbers_p = true;
16836 /* Look for clobbers. */
16837 if (clobbers_p
16838 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16840 clobbers_p = true;
16841 /* Consume the `:' or `::'. */
16842 cp_lexer_consume_token (parser->lexer);
16843 /* Parse the clobbers. */
16844 if (cp_lexer_next_token_is_not (parser->lexer,
16845 CPP_COLON)
16846 && cp_lexer_next_token_is_not (parser->lexer,
16847 CPP_CLOSE_PAREN))
16848 clobbers = cp_parser_asm_clobber_list (parser);
16850 else if (goto_p
16851 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16852 /* The labels are coming next. */
16853 labels_p = true;
16855 /* Look for labels. */
16856 if (labels_p
16857 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16859 labels_p = true;
16860 /* Consume the `:' or `::'. */
16861 cp_lexer_consume_token (parser->lexer);
16862 /* Parse the labels. */
16863 labels = cp_parser_asm_label_list (parser);
16866 if (goto_p && !labels_p)
16867 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16869 else if (goto_p)
16870 missing = RT_COLON_SCOPE;
16872 /* Look for the closing `)'. */
16873 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16874 missing ? missing : RT_CLOSE_PAREN))
16875 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16876 /*consume_paren=*/true);
16877 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16879 if (!invalid_inputs_p && !invalid_outputs_p)
16881 /* Create the ASM_EXPR. */
16882 if (parser->in_function_body)
16884 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16885 inputs, clobbers, labels);
16886 /* If the extended syntax was not used, mark the ASM_EXPR. */
16887 if (!extended_p)
16889 tree temp = asm_stmt;
16890 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16891 temp = TREE_OPERAND (temp, 0);
16893 ASM_INPUT_P (temp) = 1;
16896 else
16897 symtab->finalize_toplevel_asm (string);
16901 /* Declarators [gram.dcl.decl] */
16903 /* Parse an init-declarator.
16905 init-declarator:
16906 declarator initializer [opt]
16908 GNU Extension:
16910 init-declarator:
16911 declarator asm-specification [opt] attributes [opt] initializer [opt]
16913 function-definition:
16914 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16915 function-body
16916 decl-specifier-seq [opt] declarator function-try-block
16918 GNU Extension:
16920 function-definition:
16921 __extension__ function-definition
16923 TM Extension:
16925 function-definition:
16926 decl-specifier-seq [opt] declarator function-transaction-block
16928 The DECL_SPECIFIERS apply to this declarator. Returns a
16929 representation of the entity declared. If MEMBER_P is TRUE, then
16930 this declarator appears in a class scope. The new DECL created by
16931 this declarator is returned.
16933 The CHECKS are access checks that should be performed once we know
16934 what entity is being declared (and, therefore, what classes have
16935 befriended it).
16937 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16938 for a function-definition here as well. If the declarator is a
16939 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16940 be TRUE upon return. By that point, the function-definition will
16941 have been completely parsed.
16943 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16944 is FALSE.
16946 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16947 parsed declaration if it is an uninitialized single declarator not followed
16948 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16949 if present, will not be consumed. If returned, this declarator will be
16950 created with SD_INITIALIZED but will not call cp_finish_decl.
16952 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16953 and there is an initializer, the pointed location_t is set to the
16954 location of the '=' or `(', or '{' in C++11 token introducing the
16955 initializer. */
16957 static tree
16958 cp_parser_init_declarator (cp_parser* parser,
16959 cp_decl_specifier_seq *decl_specifiers,
16960 vec<deferred_access_check, va_gc> *checks,
16961 bool function_definition_allowed_p,
16962 bool member_p,
16963 int declares_class_or_enum,
16964 bool* function_definition_p,
16965 tree* maybe_range_for_decl,
16966 location_t* init_loc)
16968 cp_token *token = NULL, *asm_spec_start_token = NULL,
16969 *attributes_start_token = NULL;
16970 cp_declarator *declarator;
16971 tree prefix_attributes;
16972 tree attributes = NULL;
16973 tree asm_specification;
16974 tree initializer;
16975 tree decl = NULL_TREE;
16976 tree scope;
16977 int is_initialized;
16978 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16979 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16980 "(...)". */
16981 enum cpp_ttype initialization_kind;
16982 bool is_direct_init = false;
16983 bool is_non_constant_init;
16984 int ctor_dtor_or_conv_p;
16985 bool friend_p = cp_parser_friend_p (decl_specifiers);
16986 tree pushed_scope = NULL_TREE;
16987 bool range_for_decl_p = false;
16988 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16989 location_t tmp_init_loc = UNKNOWN_LOCATION;
16991 /* Gather the attributes that were provided with the
16992 decl-specifiers. */
16993 prefix_attributes = decl_specifiers->attributes;
16995 /* Assume that this is not the declarator for a function
16996 definition. */
16997 if (function_definition_p)
16998 *function_definition_p = false;
17000 /* Default arguments are only permitted for function parameters. */
17001 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
17002 parser->default_arg_ok_p = false;
17004 /* Defer access checks while parsing the declarator; we cannot know
17005 what names are accessible until we know what is being
17006 declared. */
17007 resume_deferring_access_checks ();
17009 /* Parse the declarator. */
17010 token = cp_lexer_peek_token (parser->lexer);
17011 declarator
17012 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17013 &ctor_dtor_or_conv_p,
17014 /*parenthesized_p=*/NULL,
17015 member_p, friend_p);
17016 /* Gather up the deferred checks. */
17017 stop_deferring_access_checks ();
17019 parser->default_arg_ok_p = saved_default_arg_ok_p;
17021 /* If the DECLARATOR was erroneous, there's no need to go
17022 further. */
17023 if (declarator == cp_error_declarator)
17024 return error_mark_node;
17026 /* Check that the number of template-parameter-lists is OK. */
17027 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17028 token->location))
17029 return error_mark_node;
17031 if (declares_class_or_enum & 2)
17032 cp_parser_check_for_definition_in_return_type (declarator,
17033 decl_specifiers->type,
17034 decl_specifiers->locations[ds_type_spec]);
17036 /* Figure out what scope the entity declared by the DECLARATOR is
17037 located in. `grokdeclarator' sometimes changes the scope, so
17038 we compute it now. */
17039 scope = get_scope_of_declarator (declarator);
17041 /* Perform any lookups in the declared type which were thought to be
17042 dependent, but are not in the scope of the declarator. */
17043 decl_specifiers->type
17044 = maybe_update_decl_type (decl_specifiers->type, scope);
17046 /* If we're allowing GNU extensions, look for an
17047 asm-specification. */
17048 if (cp_parser_allow_gnu_extensions_p (parser))
17050 /* Look for an asm-specification. */
17051 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17052 asm_specification = cp_parser_asm_specification_opt (parser);
17054 else
17055 asm_specification = NULL_TREE;
17057 /* Look for attributes. */
17058 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17059 attributes = cp_parser_attributes_opt (parser);
17061 /* Peek at the next token. */
17062 token = cp_lexer_peek_token (parser->lexer);
17064 bool bogus_implicit_tmpl = false;
17066 if (function_declarator_p (declarator))
17068 /* Check to see if the token indicates the start of a
17069 function-definition. */
17070 if (cp_parser_token_starts_function_definition_p (token))
17072 if (!function_definition_allowed_p)
17074 /* If a function-definition should not appear here, issue an
17075 error message. */
17076 cp_parser_error (parser,
17077 "a function-definition is not allowed here");
17078 return error_mark_node;
17081 location_t func_brace_location
17082 = cp_lexer_peek_token (parser->lexer)->location;
17084 /* Neither attributes nor an asm-specification are allowed
17085 on a function-definition. */
17086 if (asm_specification)
17087 error_at (asm_spec_start_token->location,
17088 "an asm-specification is not allowed "
17089 "on a function-definition");
17090 if (attributes)
17091 error_at (attributes_start_token->location,
17092 "attributes are not allowed "
17093 "on a function-definition");
17094 /* This is a function-definition. */
17095 *function_definition_p = true;
17097 /* Parse the function definition. */
17098 if (member_p)
17099 decl = cp_parser_save_member_function_body (parser,
17100 decl_specifiers,
17101 declarator,
17102 prefix_attributes);
17103 else
17104 decl =
17105 (cp_parser_function_definition_from_specifiers_and_declarator
17106 (parser, decl_specifiers, prefix_attributes, declarator));
17108 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17110 /* This is where the prologue starts... */
17111 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17112 = func_brace_location;
17115 return decl;
17118 else if (parser->fully_implicit_function_template_p)
17120 /* A non-template declaration involving a function parameter list
17121 containing an implicit template parameter will be made into a
17122 template. If the resulting declaration is not going to be an
17123 actual function then finish the template scope here to prevent it.
17124 An error message will be issued once we have a decl to talk about.
17126 FIXME probably we should do type deduction rather than create an
17127 implicit template, but the standard currently doesn't allow it. */
17128 bogus_implicit_tmpl = true;
17129 finish_fully_implicit_template (parser, NULL_TREE);
17132 /* [dcl.dcl]
17134 Only in function declarations for constructors, destructors, and
17135 type conversions can the decl-specifier-seq be omitted.
17137 We explicitly postpone this check past the point where we handle
17138 function-definitions because we tolerate function-definitions
17139 that are missing their return types in some modes. */
17140 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17142 cp_parser_error (parser,
17143 "expected constructor, destructor, or type conversion");
17144 return error_mark_node;
17147 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17148 if (token->type == CPP_EQ
17149 || token->type == CPP_OPEN_PAREN
17150 || token->type == CPP_OPEN_BRACE)
17152 is_initialized = SD_INITIALIZED;
17153 initialization_kind = token->type;
17154 if (maybe_range_for_decl)
17155 *maybe_range_for_decl = error_mark_node;
17156 tmp_init_loc = token->location;
17157 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17158 *init_loc = tmp_init_loc;
17160 if (token->type == CPP_EQ
17161 && function_declarator_p (declarator))
17163 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17164 if (t2->keyword == RID_DEFAULT)
17165 is_initialized = SD_DEFAULTED;
17166 else if (t2->keyword == RID_DELETE)
17167 is_initialized = SD_DELETED;
17170 else
17172 /* If the init-declarator isn't initialized and isn't followed by a
17173 `,' or `;', it's not a valid init-declarator. */
17174 if (token->type != CPP_COMMA
17175 && token->type != CPP_SEMICOLON)
17177 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17178 range_for_decl_p = true;
17179 else
17181 if (!maybe_range_for_decl)
17182 cp_parser_error (parser, "expected initializer");
17183 return error_mark_node;
17186 is_initialized = SD_UNINITIALIZED;
17187 initialization_kind = CPP_EOF;
17190 /* Because start_decl has side-effects, we should only call it if we
17191 know we're going ahead. By this point, we know that we cannot
17192 possibly be looking at any other construct. */
17193 cp_parser_commit_to_tentative_parse (parser);
17195 /* Enter the newly declared entry in the symbol table. If we're
17196 processing a declaration in a class-specifier, we wait until
17197 after processing the initializer. */
17198 if (!member_p)
17200 if (parser->in_unbraced_linkage_specification_p)
17201 decl_specifiers->storage_class = sc_extern;
17202 decl = start_decl (declarator, decl_specifiers,
17203 range_for_decl_p? SD_INITIALIZED : is_initialized,
17204 attributes, prefix_attributes, &pushed_scope);
17205 cp_finalize_omp_declare_simd (parser, decl);
17206 /* Adjust location of decl if declarator->id_loc is more appropriate:
17207 set, and decl wasn't merged with another decl, in which case its
17208 location would be different from input_location, and more accurate. */
17209 if (DECL_P (decl)
17210 && declarator->id_loc != UNKNOWN_LOCATION
17211 && DECL_SOURCE_LOCATION (decl) == input_location)
17212 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17214 else if (scope)
17215 /* Enter the SCOPE. That way unqualified names appearing in the
17216 initializer will be looked up in SCOPE. */
17217 pushed_scope = push_scope (scope);
17219 /* Perform deferred access control checks, now that we know in which
17220 SCOPE the declared entity resides. */
17221 if (!member_p && decl)
17223 tree saved_current_function_decl = NULL_TREE;
17225 /* If the entity being declared is a function, pretend that we
17226 are in its scope. If it is a `friend', it may have access to
17227 things that would not otherwise be accessible. */
17228 if (TREE_CODE (decl) == FUNCTION_DECL)
17230 saved_current_function_decl = current_function_decl;
17231 current_function_decl = decl;
17234 /* Perform access checks for template parameters. */
17235 cp_parser_perform_template_parameter_access_checks (checks);
17237 /* Perform the access control checks for the declarator and the
17238 decl-specifiers. */
17239 perform_deferred_access_checks (tf_warning_or_error);
17241 /* Restore the saved value. */
17242 if (TREE_CODE (decl) == FUNCTION_DECL)
17243 current_function_decl = saved_current_function_decl;
17246 /* Parse the initializer. */
17247 initializer = NULL_TREE;
17248 is_direct_init = false;
17249 is_non_constant_init = true;
17250 if (is_initialized)
17252 if (function_declarator_p (declarator))
17254 if (initialization_kind == CPP_EQ)
17255 initializer = cp_parser_pure_specifier (parser);
17256 else
17258 /* If the declaration was erroneous, we don't really
17259 know what the user intended, so just silently
17260 consume the initializer. */
17261 if (decl != error_mark_node)
17262 error_at (tmp_init_loc, "initializer provided for function");
17263 cp_parser_skip_to_closing_parenthesis (parser,
17264 /*recovering=*/true,
17265 /*or_comma=*/false,
17266 /*consume_paren=*/true);
17269 else
17271 /* We want to record the extra mangling scope for in-class
17272 initializers of class members and initializers of static data
17273 member templates. The former involves deferring
17274 parsing of the initializer until end of class as with default
17275 arguments. So right here we only handle the latter. */
17276 if (!member_p && processing_template_decl)
17277 start_lambda_scope (decl);
17278 initializer = cp_parser_initializer (parser,
17279 &is_direct_init,
17280 &is_non_constant_init);
17281 if (!member_p && processing_template_decl)
17282 finish_lambda_scope ();
17283 if (initializer == error_mark_node)
17284 cp_parser_skip_to_end_of_statement (parser);
17288 /* The old parser allows attributes to appear after a parenthesized
17289 initializer. Mark Mitchell proposed removing this functionality
17290 on the GCC mailing lists on 2002-08-13. This parser accepts the
17291 attributes -- but ignores them. */
17292 if (cp_parser_allow_gnu_extensions_p (parser)
17293 && initialization_kind == CPP_OPEN_PAREN)
17294 if (cp_parser_attributes_opt (parser))
17295 warning (OPT_Wattributes,
17296 "attributes after parenthesized initializer ignored");
17298 /* And now complain about a non-function implicit template. */
17299 if (bogus_implicit_tmpl)
17300 error_at (DECL_SOURCE_LOCATION (decl),
17301 "non-function %qD declared as implicit template", decl);
17303 /* For an in-class declaration, use `grokfield' to create the
17304 declaration. */
17305 if (member_p)
17307 if (pushed_scope)
17309 pop_scope (pushed_scope);
17310 pushed_scope = NULL_TREE;
17312 decl = grokfield (declarator, decl_specifiers,
17313 initializer, !is_non_constant_init,
17314 /*asmspec=*/NULL_TREE,
17315 chainon (attributes, prefix_attributes));
17316 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17317 cp_parser_save_default_args (parser, decl);
17318 cp_finalize_omp_declare_simd (parser, decl);
17321 /* Finish processing the declaration. But, skip member
17322 declarations. */
17323 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17325 cp_finish_decl (decl,
17326 initializer, !is_non_constant_init,
17327 asm_specification,
17328 /* If the initializer is in parentheses, then this is
17329 a direct-initialization, which means that an
17330 `explicit' constructor is OK. Otherwise, an
17331 `explicit' constructor cannot be used. */
17332 ((is_direct_init || !is_initialized)
17333 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17335 else if ((cxx_dialect != cxx98) && friend_p
17336 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17337 /* Core issue #226 (C++0x only): A default template-argument
17338 shall not be specified in a friend class template
17339 declaration. */
17340 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17341 /*is_partial=*/false, /*is_friend_decl=*/1);
17343 if (!friend_p && pushed_scope)
17344 pop_scope (pushed_scope);
17346 if (function_declarator_p (declarator)
17347 && parser->fully_implicit_function_template_p)
17349 if (member_p)
17350 decl = finish_fully_implicit_template (parser, decl);
17351 else
17352 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17355 return decl;
17358 /* Parse a declarator.
17360 declarator:
17361 direct-declarator
17362 ptr-operator declarator
17364 abstract-declarator:
17365 ptr-operator abstract-declarator [opt]
17366 direct-abstract-declarator
17368 GNU Extensions:
17370 declarator:
17371 attributes [opt] direct-declarator
17372 attributes [opt] ptr-operator declarator
17374 abstract-declarator:
17375 attributes [opt] ptr-operator abstract-declarator [opt]
17376 attributes [opt] direct-abstract-declarator
17378 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17379 detect constructor, destructor or conversion operators. It is set
17380 to -1 if the declarator is a name, and +1 if it is a
17381 function. Otherwise it is set to zero. Usually you just want to
17382 test for >0, but internally the negative value is used.
17384 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17385 a decl-specifier-seq unless it declares a constructor, destructor,
17386 or conversion. It might seem that we could check this condition in
17387 semantic analysis, rather than parsing, but that makes it difficult
17388 to handle something like `f()'. We want to notice that there are
17389 no decl-specifiers, and therefore realize that this is an
17390 expression, not a declaration.)
17392 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17393 the declarator is a direct-declarator of the form "(...)".
17395 MEMBER_P is true iff this declarator is a member-declarator.
17397 FRIEND_P is true iff this declarator is a friend. */
17399 static cp_declarator *
17400 cp_parser_declarator (cp_parser* parser,
17401 cp_parser_declarator_kind dcl_kind,
17402 int* ctor_dtor_or_conv_p,
17403 bool* parenthesized_p,
17404 bool member_p, bool friend_p)
17406 cp_declarator *declarator;
17407 enum tree_code code;
17408 cp_cv_quals cv_quals;
17409 tree class_type;
17410 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17412 /* Assume this is not a constructor, destructor, or type-conversion
17413 operator. */
17414 if (ctor_dtor_or_conv_p)
17415 *ctor_dtor_or_conv_p = 0;
17417 if (cp_parser_allow_gnu_extensions_p (parser))
17418 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17420 /* Check for the ptr-operator production. */
17421 cp_parser_parse_tentatively (parser);
17422 /* Parse the ptr-operator. */
17423 code = cp_parser_ptr_operator (parser,
17424 &class_type,
17425 &cv_quals,
17426 &std_attributes);
17428 /* If that worked, then we have a ptr-operator. */
17429 if (cp_parser_parse_definitely (parser))
17431 /* If a ptr-operator was found, then this declarator was not
17432 parenthesized. */
17433 if (parenthesized_p)
17434 *parenthesized_p = true;
17435 /* The dependent declarator is optional if we are parsing an
17436 abstract-declarator. */
17437 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17438 cp_parser_parse_tentatively (parser);
17440 /* Parse the dependent declarator. */
17441 declarator = cp_parser_declarator (parser, dcl_kind,
17442 /*ctor_dtor_or_conv_p=*/NULL,
17443 /*parenthesized_p=*/NULL,
17444 /*member_p=*/false,
17445 friend_p);
17447 /* If we are parsing an abstract-declarator, we must handle the
17448 case where the dependent declarator is absent. */
17449 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17450 && !cp_parser_parse_definitely (parser))
17451 declarator = NULL;
17453 declarator = cp_parser_make_indirect_declarator
17454 (code, class_type, cv_quals, declarator, std_attributes);
17456 /* Everything else is a direct-declarator. */
17457 else
17459 if (parenthesized_p)
17460 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17461 CPP_OPEN_PAREN);
17462 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17463 ctor_dtor_or_conv_p,
17464 member_p, friend_p);
17467 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17468 declarator->attributes = gnu_attributes;
17469 return declarator;
17472 /* Parse a direct-declarator or direct-abstract-declarator.
17474 direct-declarator:
17475 declarator-id
17476 direct-declarator ( parameter-declaration-clause )
17477 cv-qualifier-seq [opt]
17478 ref-qualifier [opt]
17479 exception-specification [opt]
17480 direct-declarator [ constant-expression [opt] ]
17481 ( declarator )
17483 direct-abstract-declarator:
17484 direct-abstract-declarator [opt]
17485 ( parameter-declaration-clause )
17486 cv-qualifier-seq [opt]
17487 ref-qualifier [opt]
17488 exception-specification [opt]
17489 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17490 ( abstract-declarator )
17492 Returns a representation of the declarator. DCL_KIND is
17493 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17494 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17495 we are parsing a direct-declarator. It is
17496 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17497 of ambiguity we prefer an abstract declarator, as per
17498 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17499 as for cp_parser_declarator. */
17501 static cp_declarator *
17502 cp_parser_direct_declarator (cp_parser* parser,
17503 cp_parser_declarator_kind dcl_kind,
17504 int* ctor_dtor_or_conv_p,
17505 bool member_p, bool friend_p)
17507 cp_token *token;
17508 cp_declarator *declarator = NULL;
17509 tree scope = NULL_TREE;
17510 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17511 bool saved_in_declarator_p = parser->in_declarator_p;
17512 bool first = true;
17513 tree pushed_scope = NULL_TREE;
17515 while (true)
17517 /* Peek at the next token. */
17518 token = cp_lexer_peek_token (parser->lexer);
17519 if (token->type == CPP_OPEN_PAREN)
17521 /* This is either a parameter-declaration-clause, or a
17522 parenthesized declarator. When we know we are parsing a
17523 named declarator, it must be a parenthesized declarator
17524 if FIRST is true. For instance, `(int)' is a
17525 parameter-declaration-clause, with an omitted
17526 direct-abstract-declarator. But `((*))', is a
17527 parenthesized abstract declarator. Finally, when T is a
17528 template parameter `(T)' is a
17529 parameter-declaration-clause, and not a parenthesized
17530 named declarator.
17532 We first try and parse a parameter-declaration-clause,
17533 and then try a nested declarator (if FIRST is true).
17535 It is not an error for it not to be a
17536 parameter-declaration-clause, even when FIRST is
17537 false. Consider,
17539 int i (int);
17540 int i (3);
17542 The first is the declaration of a function while the
17543 second is the definition of a variable, including its
17544 initializer.
17546 Having seen only the parenthesis, we cannot know which of
17547 these two alternatives should be selected. Even more
17548 complex are examples like:
17550 int i (int (a));
17551 int i (int (3));
17553 The former is a function-declaration; the latter is a
17554 variable initialization.
17556 Thus again, we try a parameter-declaration-clause, and if
17557 that fails, we back out and return. */
17559 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17561 tree params;
17562 bool is_declarator = false;
17564 /* In a member-declarator, the only valid interpretation
17565 of a parenthesis is the start of a
17566 parameter-declaration-clause. (It is invalid to
17567 initialize a static data member with a parenthesized
17568 initializer; only the "=" form of initialization is
17569 permitted.) */
17570 if (!member_p)
17571 cp_parser_parse_tentatively (parser);
17573 /* Consume the `('. */
17574 cp_lexer_consume_token (parser->lexer);
17575 if (first)
17577 /* If this is going to be an abstract declarator, we're
17578 in a declarator and we can't have default args. */
17579 parser->default_arg_ok_p = false;
17580 parser->in_declarator_p = true;
17583 begin_scope (sk_function_parms, NULL_TREE);
17585 /* Parse the parameter-declaration-clause. */
17586 params = cp_parser_parameter_declaration_clause (parser);
17588 /* Consume the `)'. */
17589 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17591 /* If all went well, parse the cv-qualifier-seq,
17592 ref-qualifier and the exception-specification. */
17593 if (member_p || cp_parser_parse_definitely (parser))
17595 cp_cv_quals cv_quals;
17596 cp_virt_specifiers virt_specifiers;
17597 cp_ref_qualifier ref_qual;
17598 tree exception_specification;
17599 tree late_return;
17600 tree attrs;
17601 bool memfn = (member_p || (pushed_scope
17602 && CLASS_TYPE_P (pushed_scope)));
17604 is_declarator = true;
17606 if (ctor_dtor_or_conv_p)
17607 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17608 first = false;
17610 /* Parse the cv-qualifier-seq. */
17611 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17612 /* Parse the ref-qualifier. */
17613 ref_qual = cp_parser_ref_qualifier_opt (parser);
17614 /* And the exception-specification. */
17615 exception_specification
17616 = cp_parser_exception_specification_opt (parser);
17618 attrs = cp_parser_std_attribute_spec_seq (parser);
17620 /* In here, we handle cases where attribute is used after
17621 the function declaration. For example:
17622 void func (int x) __attribute__((vector(..))); */
17623 if (flag_cilkplus
17624 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17626 cp_parser_parse_tentatively (parser);
17627 tree attr = cp_parser_gnu_attributes_opt (parser);
17628 if (cp_lexer_next_token_is_not (parser->lexer,
17629 CPP_SEMICOLON)
17630 && cp_lexer_next_token_is_not (parser->lexer,
17631 CPP_OPEN_BRACE))
17632 cp_parser_abort_tentative_parse (parser);
17633 else if (!cp_parser_parse_definitely (parser))
17635 else
17636 attrs = chainon (attr, attrs);
17638 late_return = (cp_parser_late_return_type_opt
17639 (parser, declarator,
17640 memfn ? cv_quals : -1));
17643 /* Parse the virt-specifier-seq. */
17644 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17646 /* Create the function-declarator. */
17647 declarator = make_call_declarator (declarator,
17648 params,
17649 cv_quals,
17650 virt_specifiers,
17651 ref_qual,
17652 exception_specification,
17653 late_return);
17654 declarator->std_attributes = attrs;
17655 /* Any subsequent parameter lists are to do with
17656 return type, so are not those of the declared
17657 function. */
17658 parser->default_arg_ok_p = false;
17661 /* Remove the function parms from scope. */
17662 pop_bindings_and_leave_scope ();
17664 if (is_declarator)
17665 /* Repeat the main loop. */
17666 continue;
17669 /* If this is the first, we can try a parenthesized
17670 declarator. */
17671 if (first)
17673 bool saved_in_type_id_in_expr_p;
17675 parser->default_arg_ok_p = saved_default_arg_ok_p;
17676 parser->in_declarator_p = saved_in_declarator_p;
17678 /* Consume the `('. */
17679 cp_lexer_consume_token (parser->lexer);
17680 /* Parse the nested declarator. */
17681 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17682 parser->in_type_id_in_expr_p = true;
17683 declarator
17684 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17685 /*parenthesized_p=*/NULL,
17686 member_p, friend_p);
17687 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17688 first = false;
17689 /* Expect a `)'. */
17690 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17691 declarator = cp_error_declarator;
17692 if (declarator == cp_error_declarator)
17693 break;
17695 goto handle_declarator;
17697 /* Otherwise, we must be done. */
17698 else
17699 break;
17701 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17702 && token->type == CPP_OPEN_SQUARE
17703 && !cp_next_tokens_can_be_attribute_p (parser))
17705 /* Parse an array-declarator. */
17706 tree bounds, attrs;
17708 if (ctor_dtor_or_conv_p)
17709 *ctor_dtor_or_conv_p = 0;
17711 first = false;
17712 parser->default_arg_ok_p = false;
17713 parser->in_declarator_p = true;
17714 /* Consume the `['. */
17715 cp_lexer_consume_token (parser->lexer);
17716 /* Peek at the next token. */
17717 token = cp_lexer_peek_token (parser->lexer);
17718 /* If the next token is `]', then there is no
17719 constant-expression. */
17720 if (token->type != CPP_CLOSE_SQUARE)
17722 bool non_constant_p;
17723 bounds
17724 = cp_parser_constant_expression (parser,
17725 /*allow_non_constant=*/true,
17726 &non_constant_p);
17727 if (!non_constant_p)
17728 /* OK */;
17729 else if (error_operand_p (bounds))
17730 /* Already gave an error. */;
17731 else if (!parser->in_function_body
17732 || current_binding_level->kind == sk_function_parms)
17734 /* Normally, the array bound must be an integral constant
17735 expression. However, as an extension, we allow VLAs
17736 in function scopes as long as they aren't part of a
17737 parameter declaration. */
17738 cp_parser_error (parser,
17739 "array bound is not an integer constant");
17740 bounds = error_mark_node;
17742 else if (processing_template_decl
17743 && !type_dependent_expression_p (bounds))
17745 /* Remember this wasn't a constant-expression. */
17746 bounds = build_nop (TREE_TYPE (bounds), bounds);
17747 TREE_SIDE_EFFECTS (bounds) = 1;
17750 else
17751 bounds = NULL_TREE;
17752 /* Look for the closing `]'. */
17753 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17755 declarator = cp_error_declarator;
17756 break;
17759 attrs = cp_parser_std_attribute_spec_seq (parser);
17760 declarator = make_array_declarator (declarator, bounds);
17761 declarator->std_attributes = attrs;
17763 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17766 tree qualifying_scope;
17767 tree unqualified_name;
17768 tree attrs;
17769 special_function_kind sfk;
17770 bool abstract_ok;
17771 bool pack_expansion_p = false;
17772 cp_token *declarator_id_start_token;
17774 /* Parse a declarator-id */
17775 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17776 if (abstract_ok)
17778 cp_parser_parse_tentatively (parser);
17780 /* If we see an ellipsis, we should be looking at a
17781 parameter pack. */
17782 if (token->type == CPP_ELLIPSIS)
17784 /* Consume the `...' */
17785 cp_lexer_consume_token (parser->lexer);
17787 pack_expansion_p = true;
17791 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17792 unqualified_name
17793 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17794 qualifying_scope = parser->scope;
17795 if (abstract_ok)
17797 bool okay = false;
17799 if (!unqualified_name && pack_expansion_p)
17801 /* Check whether an error occurred. */
17802 okay = !cp_parser_error_occurred (parser);
17804 /* We already consumed the ellipsis to mark a
17805 parameter pack, but we have no way to report it,
17806 so abort the tentative parse. We will be exiting
17807 immediately anyway. */
17808 cp_parser_abort_tentative_parse (parser);
17810 else
17811 okay = cp_parser_parse_definitely (parser);
17813 if (!okay)
17814 unqualified_name = error_mark_node;
17815 else if (unqualified_name
17816 && (qualifying_scope
17817 || (!identifier_p (unqualified_name))))
17819 cp_parser_error (parser, "expected unqualified-id");
17820 unqualified_name = error_mark_node;
17824 if (!unqualified_name)
17825 return NULL;
17826 if (unqualified_name == error_mark_node)
17828 declarator = cp_error_declarator;
17829 pack_expansion_p = false;
17830 declarator->parameter_pack_p = false;
17831 break;
17834 attrs = cp_parser_std_attribute_spec_seq (parser);
17836 if (qualifying_scope && at_namespace_scope_p ()
17837 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17839 /* In the declaration of a member of a template class
17840 outside of the class itself, the SCOPE will sometimes
17841 be a TYPENAME_TYPE. For example, given:
17843 template <typename T>
17844 int S<T>::R::i = 3;
17846 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17847 this context, we must resolve S<T>::R to an ordinary
17848 type, rather than a typename type.
17850 The reason we normally avoid resolving TYPENAME_TYPEs
17851 is that a specialization of `S' might render
17852 `S<T>::R' not a type. However, if `S' is
17853 specialized, then this `i' will not be used, so there
17854 is no harm in resolving the types here. */
17855 tree type;
17857 /* Resolve the TYPENAME_TYPE. */
17858 type = resolve_typename_type (qualifying_scope,
17859 /*only_current_p=*/false);
17860 /* If that failed, the declarator is invalid. */
17861 if (TREE_CODE (type) == TYPENAME_TYPE)
17863 if (typedef_variant_p (type))
17864 error_at (declarator_id_start_token->location,
17865 "cannot define member of dependent typedef "
17866 "%qT", type);
17867 else
17868 error_at (declarator_id_start_token->location,
17869 "%<%T::%E%> is not a type",
17870 TYPE_CONTEXT (qualifying_scope),
17871 TYPE_IDENTIFIER (qualifying_scope));
17873 qualifying_scope = type;
17876 sfk = sfk_none;
17878 if (unqualified_name)
17880 tree class_type;
17882 if (qualifying_scope
17883 && CLASS_TYPE_P (qualifying_scope))
17884 class_type = qualifying_scope;
17885 else
17886 class_type = current_class_type;
17888 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17890 tree name_type = TREE_TYPE (unqualified_name);
17891 if (class_type && same_type_p (name_type, class_type))
17893 if (qualifying_scope
17894 && CLASSTYPE_USE_TEMPLATE (name_type))
17896 error_at (declarator_id_start_token->location,
17897 "invalid use of constructor as a template");
17898 inform (declarator_id_start_token->location,
17899 "use %<%T::%D%> instead of %<%T::%D%> to "
17900 "name the constructor in a qualified name",
17901 class_type,
17902 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17903 class_type, name_type);
17904 declarator = cp_error_declarator;
17905 break;
17907 else
17908 unqualified_name = constructor_name (class_type);
17910 else
17912 /* We do not attempt to print the declarator
17913 here because we do not have enough
17914 information about its original syntactic
17915 form. */
17916 cp_parser_error (parser, "invalid declarator");
17917 declarator = cp_error_declarator;
17918 break;
17922 if (class_type)
17924 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17925 sfk = sfk_destructor;
17926 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17927 sfk = sfk_conversion;
17928 else if (/* There's no way to declare a constructor
17929 for an anonymous type, even if the type
17930 got a name for linkage purposes. */
17931 !TYPE_WAS_ANONYMOUS (class_type)
17932 /* Handle correctly (c++/19200):
17934 struct S {
17935 struct T{};
17936 friend void S(T);
17939 and also:
17941 namespace N {
17942 void S();
17945 struct S {
17946 friend void N::S();
17947 }; */
17948 && !(friend_p
17949 && class_type != qualifying_scope)
17950 && constructor_name_p (unqualified_name,
17951 class_type))
17953 unqualified_name = constructor_name (class_type);
17954 sfk = sfk_constructor;
17956 else if (is_overloaded_fn (unqualified_name)
17957 && DECL_CONSTRUCTOR_P (get_first_fn
17958 (unqualified_name)))
17959 sfk = sfk_constructor;
17961 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17962 *ctor_dtor_or_conv_p = -1;
17965 declarator = make_id_declarator (qualifying_scope,
17966 unqualified_name,
17967 sfk);
17968 declarator->std_attributes = attrs;
17969 declarator->id_loc = token->location;
17970 declarator->parameter_pack_p = pack_expansion_p;
17972 if (pack_expansion_p)
17973 maybe_warn_variadic_templates ();
17976 handle_declarator:;
17977 scope = get_scope_of_declarator (declarator);
17978 if (scope)
17980 /* Any names that appear after the declarator-id for a
17981 member are looked up in the containing scope. */
17982 if (at_function_scope_p ())
17984 /* But declarations with qualified-ids can't appear in a
17985 function. */
17986 cp_parser_error (parser, "qualified-id in declaration");
17987 declarator = cp_error_declarator;
17988 break;
17990 pushed_scope = push_scope (scope);
17992 parser->in_declarator_p = true;
17993 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17994 || (declarator && declarator->kind == cdk_id))
17995 /* Default args are only allowed on function
17996 declarations. */
17997 parser->default_arg_ok_p = saved_default_arg_ok_p;
17998 else
17999 parser->default_arg_ok_p = false;
18001 first = false;
18003 /* We're done. */
18004 else
18005 break;
18008 /* For an abstract declarator, we might wind up with nothing at this
18009 point. That's an error; the declarator is not optional. */
18010 if (!declarator)
18011 cp_parser_error (parser, "expected declarator");
18013 /* If we entered a scope, we must exit it now. */
18014 if (pushed_scope)
18015 pop_scope (pushed_scope);
18017 parser->default_arg_ok_p = saved_default_arg_ok_p;
18018 parser->in_declarator_p = saved_in_declarator_p;
18020 return declarator;
18023 /* Parse a ptr-operator.
18025 ptr-operator:
18026 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18027 * cv-qualifier-seq [opt]
18029 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18030 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18032 GNU Extension:
18034 ptr-operator:
18035 & cv-qualifier-seq [opt]
18037 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18038 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18039 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18040 filled in with the TYPE containing the member. *CV_QUALS is
18041 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18042 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18043 Note that the tree codes returned by this function have nothing
18044 to do with the types of trees that will be eventually be created
18045 to represent the pointer or reference type being parsed. They are
18046 just constants with suggestive names. */
18047 static enum tree_code
18048 cp_parser_ptr_operator (cp_parser* parser,
18049 tree* type,
18050 cp_cv_quals *cv_quals,
18051 tree *attributes)
18053 enum tree_code code = ERROR_MARK;
18054 cp_token *token;
18055 tree attrs = NULL_TREE;
18057 /* Assume that it's not a pointer-to-member. */
18058 *type = NULL_TREE;
18059 /* And that there are no cv-qualifiers. */
18060 *cv_quals = TYPE_UNQUALIFIED;
18062 /* Peek at the next token. */
18063 token = cp_lexer_peek_token (parser->lexer);
18065 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18066 if (token->type == CPP_MULT)
18067 code = INDIRECT_REF;
18068 else if (token->type == CPP_AND)
18069 code = ADDR_EXPR;
18070 else if ((cxx_dialect != cxx98) &&
18071 token->type == CPP_AND_AND) /* C++0x only */
18072 code = NON_LVALUE_EXPR;
18074 if (code != ERROR_MARK)
18076 /* Consume the `*', `&' or `&&'. */
18077 cp_lexer_consume_token (parser->lexer);
18079 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18080 `&', if we are allowing GNU extensions. (The only qualifier
18081 that can legally appear after `&' is `restrict', but that is
18082 enforced during semantic analysis. */
18083 if (code == INDIRECT_REF
18084 || cp_parser_allow_gnu_extensions_p (parser))
18085 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18087 attrs = cp_parser_std_attribute_spec_seq (parser);
18088 if (attributes != NULL)
18089 *attributes = attrs;
18091 else
18093 /* Try the pointer-to-member case. */
18094 cp_parser_parse_tentatively (parser);
18095 /* Look for the optional `::' operator. */
18096 cp_parser_global_scope_opt (parser,
18097 /*current_scope_valid_p=*/false);
18098 /* Look for the nested-name specifier. */
18099 token = cp_lexer_peek_token (parser->lexer);
18100 cp_parser_nested_name_specifier (parser,
18101 /*typename_keyword_p=*/false,
18102 /*check_dependency_p=*/true,
18103 /*type_p=*/false,
18104 /*is_declaration=*/false);
18105 /* If we found it, and the next token is a `*', then we are
18106 indeed looking at a pointer-to-member operator. */
18107 if (!cp_parser_error_occurred (parser)
18108 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18110 /* Indicate that the `*' operator was used. */
18111 code = INDIRECT_REF;
18113 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18114 error_at (token->location, "%qD is a namespace", parser->scope);
18115 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18116 error_at (token->location, "cannot form pointer to member of "
18117 "non-class %q#T", parser->scope);
18118 else
18120 /* The type of which the member is a member is given by the
18121 current SCOPE. */
18122 *type = parser->scope;
18123 /* The next name will not be qualified. */
18124 parser->scope = NULL_TREE;
18125 parser->qualifying_scope = NULL_TREE;
18126 parser->object_scope = NULL_TREE;
18127 /* Look for optional c++11 attributes. */
18128 attrs = cp_parser_std_attribute_spec_seq (parser);
18129 if (attributes != NULL)
18130 *attributes = attrs;
18131 /* Look for the optional cv-qualifier-seq. */
18132 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18135 /* If that didn't work we don't have a ptr-operator. */
18136 if (!cp_parser_parse_definitely (parser))
18137 cp_parser_error (parser, "expected ptr-operator");
18140 return code;
18143 /* Parse an (optional) cv-qualifier-seq.
18145 cv-qualifier-seq:
18146 cv-qualifier cv-qualifier-seq [opt]
18148 cv-qualifier:
18149 const
18150 volatile
18152 GNU Extension:
18154 cv-qualifier:
18155 __restrict__
18157 Returns a bitmask representing the cv-qualifiers. */
18159 static cp_cv_quals
18160 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18162 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18164 while (true)
18166 cp_token *token;
18167 cp_cv_quals cv_qualifier;
18169 /* Peek at the next token. */
18170 token = cp_lexer_peek_token (parser->lexer);
18171 /* See if it's a cv-qualifier. */
18172 switch (token->keyword)
18174 case RID_CONST:
18175 cv_qualifier = TYPE_QUAL_CONST;
18176 break;
18178 case RID_VOLATILE:
18179 cv_qualifier = TYPE_QUAL_VOLATILE;
18180 break;
18182 case RID_RESTRICT:
18183 cv_qualifier = TYPE_QUAL_RESTRICT;
18184 break;
18186 default:
18187 cv_qualifier = TYPE_UNQUALIFIED;
18188 break;
18191 if (!cv_qualifier)
18192 break;
18194 if (cv_quals & cv_qualifier)
18196 error_at (token->location, "duplicate cv-qualifier");
18197 cp_lexer_purge_token (parser->lexer);
18199 else
18201 cp_lexer_consume_token (parser->lexer);
18202 cv_quals |= cv_qualifier;
18206 return cv_quals;
18209 /* Parse an (optional) ref-qualifier
18211 ref-qualifier:
18215 Returns cp_ref_qualifier representing ref-qualifier. */
18217 static cp_ref_qualifier
18218 cp_parser_ref_qualifier_opt (cp_parser* parser)
18220 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18222 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18223 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18224 return ref_qual;
18226 while (true)
18228 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18229 cp_token *token = cp_lexer_peek_token (parser->lexer);
18231 switch (token->type)
18233 case CPP_AND:
18234 curr_ref_qual = REF_QUAL_LVALUE;
18235 break;
18237 case CPP_AND_AND:
18238 curr_ref_qual = REF_QUAL_RVALUE;
18239 break;
18241 default:
18242 curr_ref_qual = REF_QUAL_NONE;
18243 break;
18246 if (!curr_ref_qual)
18247 break;
18248 else if (ref_qual)
18250 error_at (token->location, "multiple ref-qualifiers");
18251 cp_lexer_purge_token (parser->lexer);
18253 else
18255 ref_qual = curr_ref_qual;
18256 cp_lexer_consume_token (parser->lexer);
18260 return ref_qual;
18263 /* Parse an (optional) virt-specifier-seq.
18265 virt-specifier-seq:
18266 virt-specifier virt-specifier-seq [opt]
18268 virt-specifier:
18269 override
18270 final
18272 Returns a bitmask representing the virt-specifiers. */
18274 static cp_virt_specifiers
18275 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18277 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18279 while (true)
18281 cp_token *token;
18282 cp_virt_specifiers virt_specifier;
18284 /* Peek at the next token. */
18285 token = cp_lexer_peek_token (parser->lexer);
18286 /* See if it's a virt-specifier-qualifier. */
18287 if (token->type != CPP_NAME)
18288 break;
18289 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18291 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18292 virt_specifier = VIRT_SPEC_OVERRIDE;
18294 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18296 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18297 virt_specifier = VIRT_SPEC_FINAL;
18299 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18301 virt_specifier = VIRT_SPEC_FINAL;
18303 else
18304 break;
18306 if (virt_specifiers & virt_specifier)
18308 error_at (token->location, "duplicate virt-specifier");
18309 cp_lexer_purge_token (parser->lexer);
18311 else
18313 cp_lexer_consume_token (parser->lexer);
18314 virt_specifiers |= virt_specifier;
18317 return virt_specifiers;
18320 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18321 is in scope even though it isn't real. */
18323 void
18324 inject_this_parameter (tree ctype, cp_cv_quals quals)
18326 tree this_parm;
18328 if (current_class_ptr)
18330 /* We don't clear this between NSDMIs. Is it already what we want? */
18331 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18332 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18333 && cp_type_quals (type) == quals)
18334 return;
18337 this_parm = build_this_parm (ctype, quals);
18338 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18339 current_class_ptr = NULL_TREE;
18340 current_class_ref
18341 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18342 current_class_ptr = this_parm;
18345 /* Return true iff our current scope is a non-static data member
18346 initializer. */
18348 bool
18349 parsing_nsdmi (void)
18351 /* We recognize NSDMI context by the context-less 'this' pointer set up
18352 by the function above. */
18353 if (current_class_ptr
18354 && TREE_CODE (current_class_ptr) == PARM_DECL
18355 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18356 return true;
18357 return false;
18360 /* Parse a late-specified return type, if any. This is not a separate
18361 non-terminal, but part of a function declarator, which looks like
18363 -> trailing-type-specifier-seq abstract-declarator(opt)
18365 Returns the type indicated by the type-id.
18367 In addition to this, parse any queued up omp declare simd
18368 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18370 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18371 function. */
18373 static tree
18374 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18375 cp_cv_quals quals)
18377 cp_token *token;
18378 tree type = NULL_TREE;
18379 bool declare_simd_p = (parser->omp_declare_simd
18380 && declarator
18381 && declarator->kind == cdk_id);
18383 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18384 && declarator && declarator->kind == cdk_id);
18386 /* Peek at the next token. */
18387 token = cp_lexer_peek_token (parser->lexer);
18388 /* A late-specified return type is indicated by an initial '->'. */
18389 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18390 return NULL_TREE;
18392 tree save_ccp = current_class_ptr;
18393 tree save_ccr = current_class_ref;
18394 if (quals >= 0)
18396 /* DR 1207: 'this' is in scope in the trailing return type. */
18397 inject_this_parameter (current_class_type, quals);
18400 if (token->type == CPP_DEREF)
18402 /* Consume the ->. */
18403 cp_lexer_consume_token (parser->lexer);
18405 type = cp_parser_trailing_type_id (parser);
18408 if (cilk_simd_fn_vector_p)
18409 declarator->std_attributes
18410 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18411 declarator->std_attributes);
18412 if (declare_simd_p)
18413 declarator->std_attributes
18414 = cp_parser_late_parsing_omp_declare_simd (parser,
18415 declarator->std_attributes);
18417 if (quals >= 0)
18419 current_class_ptr = save_ccp;
18420 current_class_ref = save_ccr;
18423 return type;
18426 /* Parse a declarator-id.
18428 declarator-id:
18429 id-expression
18430 :: [opt] nested-name-specifier [opt] type-name
18432 In the `id-expression' case, the value returned is as for
18433 cp_parser_id_expression if the id-expression was an unqualified-id.
18434 If the id-expression was a qualified-id, then a SCOPE_REF is
18435 returned. The first operand is the scope (either a NAMESPACE_DECL
18436 or TREE_TYPE), but the second is still just a representation of an
18437 unqualified-id. */
18439 static tree
18440 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18442 tree id;
18443 /* The expression must be an id-expression. Assume that qualified
18444 names are the names of types so that:
18446 template <class T>
18447 int S<T>::R::i = 3;
18449 will work; we must treat `S<T>::R' as the name of a type.
18450 Similarly, assume that qualified names are templates, where
18451 required, so that:
18453 template <class T>
18454 int S<T>::R<T>::i = 3;
18456 will work, too. */
18457 id = cp_parser_id_expression (parser,
18458 /*template_keyword_p=*/false,
18459 /*check_dependency_p=*/false,
18460 /*template_p=*/NULL,
18461 /*declarator_p=*/true,
18462 optional_p);
18463 if (id && BASELINK_P (id))
18464 id = BASELINK_FUNCTIONS (id);
18465 return id;
18468 /* Parse a type-id.
18470 type-id:
18471 type-specifier-seq abstract-declarator [opt]
18473 Returns the TYPE specified. */
18475 static tree
18476 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18477 bool is_trailing_return)
18479 cp_decl_specifier_seq type_specifier_seq;
18480 cp_declarator *abstract_declarator;
18482 /* Parse the type-specifier-seq. */
18483 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18484 is_trailing_return,
18485 &type_specifier_seq);
18486 if (type_specifier_seq.type == error_mark_node)
18487 return error_mark_node;
18489 /* There might or might not be an abstract declarator. */
18490 cp_parser_parse_tentatively (parser);
18491 /* Look for the declarator. */
18492 abstract_declarator
18493 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18494 /*parenthesized_p=*/NULL,
18495 /*member_p=*/false,
18496 /*friend_p=*/false);
18497 /* Check to see if there really was a declarator. */
18498 if (!cp_parser_parse_definitely (parser))
18499 abstract_declarator = NULL;
18501 if (type_specifier_seq.type
18502 /* None of the valid uses of 'auto' in C++14 involve the type-id
18503 nonterminal, but it is valid in a trailing-return-type. */
18504 && !(cxx_dialect >= cxx14 && is_trailing_return)
18505 && type_uses_auto (type_specifier_seq.type))
18507 /* A type-id with type 'auto' is only ok if the abstract declarator
18508 is a function declarator with a late-specified return type. */
18509 if (abstract_declarator
18510 && abstract_declarator->kind == cdk_function
18511 && abstract_declarator->u.function.late_return_type)
18512 /* OK */;
18513 else
18515 error ("invalid use of %<auto%>");
18516 return error_mark_node;
18520 return groktypename (&type_specifier_seq, abstract_declarator,
18521 is_template_arg);
18524 static tree cp_parser_type_id (cp_parser *parser)
18526 return cp_parser_type_id_1 (parser, false, false);
18529 static tree cp_parser_template_type_arg (cp_parser *parser)
18531 tree r;
18532 const char *saved_message = parser->type_definition_forbidden_message;
18533 parser->type_definition_forbidden_message
18534 = G_("types may not be defined in template arguments");
18535 r = cp_parser_type_id_1 (parser, true, false);
18536 parser->type_definition_forbidden_message = saved_message;
18537 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18539 error ("invalid use of %<auto%> in template argument");
18540 r = error_mark_node;
18542 return r;
18545 static tree cp_parser_trailing_type_id (cp_parser *parser)
18547 return cp_parser_type_id_1 (parser, false, true);
18550 /* Parse a type-specifier-seq.
18552 type-specifier-seq:
18553 type-specifier type-specifier-seq [opt]
18555 GNU extension:
18557 type-specifier-seq:
18558 attributes type-specifier-seq [opt]
18560 If IS_DECLARATION is true, we are at the start of a "condition" or
18561 exception-declaration, so we might be followed by a declarator-id.
18563 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18564 i.e. we've just seen "->".
18566 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18568 static void
18569 cp_parser_type_specifier_seq (cp_parser* parser,
18570 bool is_declaration,
18571 bool is_trailing_return,
18572 cp_decl_specifier_seq *type_specifier_seq)
18574 bool seen_type_specifier = false;
18575 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18576 cp_token *start_token = NULL;
18578 /* Clear the TYPE_SPECIFIER_SEQ. */
18579 clear_decl_specs (type_specifier_seq);
18581 /* In the context of a trailing return type, enum E { } is an
18582 elaborated-type-specifier followed by a function-body, not an
18583 enum-specifier. */
18584 if (is_trailing_return)
18585 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18587 /* Parse the type-specifiers and attributes. */
18588 while (true)
18590 tree type_specifier;
18591 bool is_cv_qualifier;
18593 /* Check for attributes first. */
18594 if (cp_next_tokens_can_be_attribute_p (parser))
18596 type_specifier_seq->attributes =
18597 chainon (type_specifier_seq->attributes,
18598 cp_parser_attributes_opt (parser));
18599 continue;
18602 /* record the token of the beginning of the type specifier seq,
18603 for error reporting purposes*/
18604 if (!start_token)
18605 start_token = cp_lexer_peek_token (parser->lexer);
18607 /* Look for the type-specifier. */
18608 type_specifier = cp_parser_type_specifier (parser,
18609 flags,
18610 type_specifier_seq,
18611 /*is_declaration=*/false,
18612 NULL,
18613 &is_cv_qualifier);
18614 if (!type_specifier)
18616 /* If the first type-specifier could not be found, this is not a
18617 type-specifier-seq at all. */
18618 if (!seen_type_specifier)
18620 /* Set in_declarator_p to avoid skipping to the semicolon. */
18621 int in_decl = parser->in_declarator_p;
18622 parser->in_declarator_p = true;
18624 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18625 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18626 cp_parser_error (parser, "expected type-specifier");
18628 parser->in_declarator_p = in_decl;
18630 type_specifier_seq->type = error_mark_node;
18631 return;
18633 /* If subsequent type-specifiers could not be found, the
18634 type-specifier-seq is complete. */
18635 break;
18638 seen_type_specifier = true;
18639 /* The standard says that a condition can be:
18641 type-specifier-seq declarator = assignment-expression
18643 However, given:
18645 struct S {};
18646 if (int S = ...)
18648 we should treat the "S" as a declarator, not as a
18649 type-specifier. The standard doesn't say that explicitly for
18650 type-specifier-seq, but it does say that for
18651 decl-specifier-seq in an ordinary declaration. Perhaps it
18652 would be clearer just to allow a decl-specifier-seq here, and
18653 then add a semantic restriction that if any decl-specifiers
18654 that are not type-specifiers appear, the program is invalid. */
18655 if (is_declaration && !is_cv_qualifier)
18656 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18660 /* Return whether the function currently being declared has an associated
18661 template parameter list. */
18663 static bool
18664 function_being_declared_is_template_p (cp_parser* parser)
18666 if (!current_template_parms || processing_template_parmlist)
18667 return false;
18669 if (parser->implicit_template_scope)
18670 return true;
18672 if (at_class_scope_p ()
18673 && TYPE_BEING_DEFINED (current_class_type))
18674 return parser->num_template_parameter_lists != 0;
18676 return ((int) parser->num_template_parameter_lists > template_class_depth
18677 (current_class_type));
18680 /* Parse a parameter-declaration-clause.
18682 parameter-declaration-clause:
18683 parameter-declaration-list [opt] ... [opt]
18684 parameter-declaration-list , ...
18686 Returns a representation for the parameter declarations. A return
18687 value of NULL indicates a parameter-declaration-clause consisting
18688 only of an ellipsis. */
18690 static tree
18691 cp_parser_parameter_declaration_clause (cp_parser* parser)
18693 tree parameters;
18694 cp_token *token;
18695 bool ellipsis_p;
18696 bool is_error;
18698 struct cleanup {
18699 cp_parser* parser;
18700 int auto_is_implicit_function_template_parm_p;
18701 ~cleanup() {
18702 parser->auto_is_implicit_function_template_parm_p
18703 = auto_is_implicit_function_template_parm_p;
18705 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18707 (void) cleanup;
18709 if (!processing_specialization
18710 && !processing_template_parmlist
18711 && !processing_explicit_instantiation)
18712 if (!current_function_decl
18713 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18714 parser->auto_is_implicit_function_template_parm_p = true;
18716 /* Peek at the next token. */
18717 token = cp_lexer_peek_token (parser->lexer);
18718 /* Check for trivial parameter-declaration-clauses. */
18719 if (token->type == CPP_ELLIPSIS)
18721 /* Consume the `...' token. */
18722 cp_lexer_consume_token (parser->lexer);
18723 return NULL_TREE;
18725 else if (token->type == CPP_CLOSE_PAREN)
18726 /* There are no parameters. */
18728 #ifndef NO_IMPLICIT_EXTERN_C
18729 if (in_system_header_at (input_location)
18730 && current_class_type == NULL
18731 && current_lang_name == lang_name_c)
18732 return NULL_TREE;
18733 else
18734 #endif
18735 return void_list_node;
18737 /* Check for `(void)', too, which is a special case. */
18738 else if (token->keyword == RID_VOID
18739 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18740 == CPP_CLOSE_PAREN))
18742 /* Consume the `void' token. */
18743 cp_lexer_consume_token (parser->lexer);
18744 /* There are no parameters. */
18745 return void_list_node;
18748 /* Parse the parameter-declaration-list. */
18749 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18750 /* If a parse error occurred while parsing the
18751 parameter-declaration-list, then the entire
18752 parameter-declaration-clause is erroneous. */
18753 if (is_error)
18754 return NULL;
18756 /* Peek at the next token. */
18757 token = cp_lexer_peek_token (parser->lexer);
18758 /* If it's a `,', the clause should terminate with an ellipsis. */
18759 if (token->type == CPP_COMMA)
18761 /* Consume the `,'. */
18762 cp_lexer_consume_token (parser->lexer);
18763 /* Expect an ellipsis. */
18764 ellipsis_p
18765 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18767 /* It might also be `...' if the optional trailing `,' was
18768 omitted. */
18769 else if (token->type == CPP_ELLIPSIS)
18771 /* Consume the `...' token. */
18772 cp_lexer_consume_token (parser->lexer);
18773 /* And remember that we saw it. */
18774 ellipsis_p = true;
18776 else
18777 ellipsis_p = false;
18779 /* Finish the parameter list. */
18780 if (!ellipsis_p)
18781 parameters = chainon (parameters, void_list_node);
18783 return parameters;
18786 /* Parse a parameter-declaration-list.
18788 parameter-declaration-list:
18789 parameter-declaration
18790 parameter-declaration-list , parameter-declaration
18792 Returns a representation of the parameter-declaration-list, as for
18793 cp_parser_parameter_declaration_clause. However, the
18794 `void_list_node' is never appended to the list. Upon return,
18795 *IS_ERROR will be true iff an error occurred. */
18797 static tree
18798 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18800 tree parameters = NULL_TREE;
18801 tree *tail = &parameters;
18802 bool saved_in_unbraced_linkage_specification_p;
18803 int index = 0;
18805 /* Assume all will go well. */
18806 *is_error = false;
18807 /* The special considerations that apply to a function within an
18808 unbraced linkage specifications do not apply to the parameters
18809 to the function. */
18810 saved_in_unbraced_linkage_specification_p
18811 = parser->in_unbraced_linkage_specification_p;
18812 parser->in_unbraced_linkage_specification_p = false;
18814 /* Look for more parameters. */
18815 while (true)
18817 cp_parameter_declarator *parameter;
18818 tree decl = error_mark_node;
18819 bool parenthesized_p = false;
18820 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18821 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18822 (current_template_parms)) : 0);
18824 /* Parse the parameter. */
18825 parameter
18826 = cp_parser_parameter_declaration (parser,
18827 /*template_parm_p=*/false,
18828 &parenthesized_p);
18830 /* We don't know yet if the enclosing context is deprecated, so wait
18831 and warn in grokparms if appropriate. */
18832 deprecated_state = DEPRECATED_SUPPRESS;
18834 if (parameter)
18836 /* If a function parameter pack was specified and an implicit template
18837 parameter was introduced during cp_parser_parameter_declaration,
18838 change any implicit parameters introduced into packs. */
18839 if (parser->implicit_template_parms
18840 && parameter->declarator
18841 && parameter->declarator->parameter_pack_p)
18843 int latest_template_parm_idx = TREE_VEC_LENGTH
18844 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18846 if (latest_template_parm_idx != template_parm_idx)
18847 parameter->decl_specifiers.type = convert_generic_types_to_packs
18848 (parameter->decl_specifiers.type,
18849 template_parm_idx, latest_template_parm_idx);
18852 decl = grokdeclarator (parameter->declarator,
18853 &parameter->decl_specifiers,
18854 PARM,
18855 parameter->default_argument != NULL_TREE,
18856 &parameter->decl_specifiers.attributes);
18859 deprecated_state = DEPRECATED_NORMAL;
18861 /* If a parse error occurred parsing the parameter declaration,
18862 then the entire parameter-declaration-list is erroneous. */
18863 if (decl == error_mark_node)
18865 *is_error = true;
18866 parameters = error_mark_node;
18867 break;
18870 if (parameter->decl_specifiers.attributes)
18871 cplus_decl_attributes (&decl,
18872 parameter->decl_specifiers.attributes,
18874 if (DECL_NAME (decl))
18875 decl = pushdecl (decl);
18877 if (decl != error_mark_node)
18879 retrofit_lang_decl (decl);
18880 DECL_PARM_INDEX (decl) = ++index;
18881 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18884 /* Add the new parameter to the list. */
18885 *tail = build_tree_list (parameter->default_argument, decl);
18886 tail = &TREE_CHAIN (*tail);
18888 /* Peek at the next token. */
18889 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18890 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18891 /* These are for Objective-C++ */
18892 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18893 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18894 /* The parameter-declaration-list is complete. */
18895 break;
18896 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18898 cp_token *token;
18900 /* Peek at the next token. */
18901 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18902 /* If it's an ellipsis, then the list is complete. */
18903 if (token->type == CPP_ELLIPSIS)
18904 break;
18905 /* Otherwise, there must be more parameters. Consume the
18906 `,'. */
18907 cp_lexer_consume_token (parser->lexer);
18908 /* When parsing something like:
18910 int i(float f, double d)
18912 we can tell after seeing the declaration for "f" that we
18913 are not looking at an initialization of a variable "i",
18914 but rather at the declaration of a function "i".
18916 Due to the fact that the parsing of template arguments
18917 (as specified to a template-id) requires backtracking we
18918 cannot use this technique when inside a template argument
18919 list. */
18920 if (!parser->in_template_argument_list_p
18921 && !parser->in_type_id_in_expr_p
18922 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18923 /* However, a parameter-declaration of the form
18924 "float(f)" (which is a valid declaration of a
18925 parameter "f") can also be interpreted as an
18926 expression (the conversion of "f" to "float"). */
18927 && !parenthesized_p)
18928 cp_parser_commit_to_tentative_parse (parser);
18930 else
18932 cp_parser_error (parser, "expected %<,%> or %<...%>");
18933 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18934 cp_parser_skip_to_closing_parenthesis (parser,
18935 /*recovering=*/true,
18936 /*or_comma=*/false,
18937 /*consume_paren=*/false);
18938 break;
18942 parser->in_unbraced_linkage_specification_p
18943 = saved_in_unbraced_linkage_specification_p;
18945 /* Reset implicit_template_scope if we are about to leave the function
18946 parameter list that introduced it. Note that for out-of-line member
18947 definitions, there will be one or more class scopes before we get to
18948 the template parameter scope. */
18950 if (cp_binding_level *its = parser->implicit_template_scope)
18951 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18953 while (maybe_its->kind == sk_class)
18954 maybe_its = maybe_its->level_chain;
18955 if (maybe_its == its)
18957 parser->implicit_template_parms = 0;
18958 parser->implicit_template_scope = 0;
18962 return parameters;
18965 /* Parse a parameter declaration.
18967 parameter-declaration:
18968 decl-specifier-seq ... [opt] declarator
18969 decl-specifier-seq declarator = assignment-expression
18970 decl-specifier-seq ... [opt] abstract-declarator [opt]
18971 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18973 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18974 declares a template parameter. (In that case, a non-nested `>'
18975 token encountered during the parsing of the assignment-expression
18976 is not interpreted as a greater-than operator.)
18978 Returns a representation of the parameter, or NULL if an error
18979 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18980 true iff the declarator is of the form "(p)". */
18982 static cp_parameter_declarator *
18983 cp_parser_parameter_declaration (cp_parser *parser,
18984 bool template_parm_p,
18985 bool *parenthesized_p)
18987 int declares_class_or_enum;
18988 cp_decl_specifier_seq decl_specifiers;
18989 cp_declarator *declarator;
18990 tree default_argument;
18991 cp_token *token = NULL, *declarator_token_start = NULL;
18992 const char *saved_message;
18993 bool template_parameter_pack_p = false;
18995 /* In a template parameter, `>' is not an operator.
18997 [temp.param]
18999 When parsing a default template-argument for a non-type
19000 template-parameter, the first non-nested `>' is taken as the end
19001 of the template parameter-list rather than a greater-than
19002 operator. */
19004 /* Type definitions may not appear in parameter types. */
19005 saved_message = parser->type_definition_forbidden_message;
19006 parser->type_definition_forbidden_message
19007 = G_("types may not be defined in parameter types");
19009 /* Parse the declaration-specifiers. */
19010 cp_parser_decl_specifier_seq (parser,
19011 CP_PARSER_FLAGS_NONE,
19012 &decl_specifiers,
19013 &declares_class_or_enum);
19015 /* Complain about missing 'typename' or other invalid type names. */
19016 if (!decl_specifiers.any_type_specifiers_p
19017 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19018 decl_specifiers.type = error_mark_node;
19020 /* If an error occurred, there's no reason to attempt to parse the
19021 rest of the declaration. */
19022 if (cp_parser_error_occurred (parser))
19024 parser->type_definition_forbidden_message = saved_message;
19025 return NULL;
19028 /* Peek at the next token. */
19029 token = cp_lexer_peek_token (parser->lexer);
19031 /* If the next token is a `)', `,', `=', `>', or `...', then there
19032 is no declarator. However, when variadic templates are enabled,
19033 there may be a declarator following `...'. */
19034 if (token->type == CPP_CLOSE_PAREN
19035 || token->type == CPP_COMMA
19036 || token->type == CPP_EQ
19037 || token->type == CPP_GREATER)
19039 declarator = NULL;
19040 if (parenthesized_p)
19041 *parenthesized_p = false;
19043 /* Otherwise, there should be a declarator. */
19044 else
19046 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19047 parser->default_arg_ok_p = false;
19049 /* After seeing a decl-specifier-seq, if the next token is not a
19050 "(", there is no possibility that the code is a valid
19051 expression. Therefore, if parsing tentatively, we commit at
19052 this point. */
19053 if (!parser->in_template_argument_list_p
19054 /* In an expression context, having seen:
19056 (int((char ...
19058 we cannot be sure whether we are looking at a
19059 function-type (taking a "char" as a parameter) or a cast
19060 of some object of type "char" to "int". */
19061 && !parser->in_type_id_in_expr_p
19062 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19063 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19064 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19065 cp_parser_commit_to_tentative_parse (parser);
19066 /* Parse the declarator. */
19067 declarator_token_start = token;
19068 declarator = cp_parser_declarator (parser,
19069 CP_PARSER_DECLARATOR_EITHER,
19070 /*ctor_dtor_or_conv_p=*/NULL,
19071 parenthesized_p,
19072 /*member_p=*/false,
19073 /*friend_p=*/false);
19074 parser->default_arg_ok_p = saved_default_arg_ok_p;
19075 /* After the declarator, allow more attributes. */
19076 decl_specifiers.attributes
19077 = chainon (decl_specifiers.attributes,
19078 cp_parser_attributes_opt (parser));
19080 /* If the declarator is a template parameter pack, remember that and
19081 clear the flag in the declarator itself so we don't get errors
19082 from grokdeclarator. */
19083 if (template_parm_p && declarator && declarator->parameter_pack_p)
19085 declarator->parameter_pack_p = false;
19086 template_parameter_pack_p = true;
19090 /* If the next token is an ellipsis, and we have not seen a
19091 declarator name, and the type of the declarator contains parameter
19092 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19093 a parameter pack expansion expression. Otherwise, leave the
19094 ellipsis for a C-style variadic function. */
19095 token = cp_lexer_peek_token (parser->lexer);
19096 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19098 tree type = decl_specifiers.type;
19100 if (type && DECL_P (type))
19101 type = TREE_TYPE (type);
19103 if (type
19104 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19105 && declarator_can_be_parameter_pack (declarator)
19106 && (template_parm_p || uses_parameter_packs (type)))
19108 /* Consume the `...'. */
19109 cp_lexer_consume_token (parser->lexer);
19110 maybe_warn_variadic_templates ();
19112 /* Build a pack expansion type */
19113 if (template_parm_p)
19114 template_parameter_pack_p = true;
19115 else if (declarator)
19116 declarator->parameter_pack_p = true;
19117 else
19118 decl_specifiers.type = make_pack_expansion (type);
19122 /* The restriction on defining new types applies only to the type
19123 of the parameter, not to the default argument. */
19124 parser->type_definition_forbidden_message = saved_message;
19126 /* If the next token is `=', then process a default argument. */
19127 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19129 token = cp_lexer_peek_token (parser->lexer);
19130 /* If we are defining a class, then the tokens that make up the
19131 default argument must be saved and processed later. */
19132 if (!template_parm_p && at_class_scope_p ()
19133 && TYPE_BEING_DEFINED (current_class_type)
19134 && !LAMBDA_TYPE_P (current_class_type))
19135 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19136 /* Outside of a class definition, we can just parse the
19137 assignment-expression. */
19138 else
19139 default_argument
19140 = cp_parser_default_argument (parser, template_parm_p);
19142 if (!parser->default_arg_ok_p)
19144 permerror (token->location,
19145 "default arguments are only "
19146 "permitted for function parameters");
19148 else if ((declarator && declarator->parameter_pack_p)
19149 || template_parameter_pack_p
19150 || (decl_specifiers.type
19151 && PACK_EXPANSION_P (decl_specifiers.type)))
19153 /* Find the name of the parameter pack. */
19154 cp_declarator *id_declarator = declarator;
19155 while (id_declarator && id_declarator->kind != cdk_id)
19156 id_declarator = id_declarator->declarator;
19158 if (id_declarator && id_declarator->kind == cdk_id)
19159 error_at (declarator_token_start->location,
19160 template_parm_p
19161 ? G_("template parameter pack %qD "
19162 "cannot have a default argument")
19163 : G_("parameter pack %qD cannot have "
19164 "a default argument"),
19165 id_declarator->u.id.unqualified_name);
19166 else
19167 error_at (declarator_token_start->location,
19168 template_parm_p
19169 ? G_("template parameter pack cannot have "
19170 "a default argument")
19171 : G_("parameter pack cannot have a "
19172 "default argument"));
19174 default_argument = NULL_TREE;
19177 else
19178 default_argument = NULL_TREE;
19180 return make_parameter_declarator (&decl_specifiers,
19181 declarator,
19182 default_argument,
19183 template_parameter_pack_p);
19186 /* Parse a default argument and return it.
19188 TEMPLATE_PARM_P is true if this is a default argument for a
19189 non-type template parameter. */
19190 static tree
19191 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19193 tree default_argument = NULL_TREE;
19194 bool saved_greater_than_is_operator_p;
19195 bool saved_local_variables_forbidden_p;
19196 bool non_constant_p, is_direct_init;
19198 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19199 set correctly. */
19200 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19201 parser->greater_than_is_operator_p = !template_parm_p;
19202 /* Local variable names (and the `this' keyword) may not
19203 appear in a default argument. */
19204 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19205 parser->local_variables_forbidden_p = true;
19206 /* Parse the assignment-expression. */
19207 if (template_parm_p)
19208 push_deferring_access_checks (dk_no_deferred);
19209 tree saved_class_ptr = NULL_TREE;
19210 tree saved_class_ref = NULL_TREE;
19211 /* The "this" pointer is not valid in a default argument. */
19212 if (cfun)
19214 saved_class_ptr = current_class_ptr;
19215 cp_function_chain->x_current_class_ptr = NULL_TREE;
19216 saved_class_ref = current_class_ref;
19217 cp_function_chain->x_current_class_ref = NULL_TREE;
19219 default_argument
19220 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19221 /* Restore the "this" pointer. */
19222 if (cfun)
19224 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19225 cp_function_chain->x_current_class_ref = saved_class_ref;
19227 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19228 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19229 if (template_parm_p)
19230 pop_deferring_access_checks ();
19231 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19232 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19234 return default_argument;
19237 /* Parse a function-body.
19239 function-body:
19240 compound_statement */
19242 static void
19243 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19245 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19248 /* Parse a ctor-initializer-opt followed by a function-body. Return
19249 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19250 is true we are parsing a function-try-block. */
19252 static bool
19253 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19254 bool in_function_try_block)
19256 tree body, list;
19257 bool ctor_initializer_p;
19258 const bool check_body_p =
19259 DECL_CONSTRUCTOR_P (current_function_decl)
19260 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19261 tree last = NULL;
19263 /* Begin the function body. */
19264 body = begin_function_body ();
19265 /* Parse the optional ctor-initializer. */
19266 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19268 /* If we're parsing a constexpr constructor definition, we need
19269 to check that the constructor body is indeed empty. However,
19270 before we get to cp_parser_function_body lot of junk has been
19271 generated, so we can't just check that we have an empty block.
19272 Rather we take a snapshot of the outermost block, and check whether
19273 cp_parser_function_body changed its state. */
19274 if (check_body_p)
19276 list = cur_stmt_list;
19277 if (STATEMENT_LIST_TAIL (list))
19278 last = STATEMENT_LIST_TAIL (list)->stmt;
19280 /* Parse the function-body. */
19281 cp_parser_function_body (parser, in_function_try_block);
19282 if (check_body_p)
19283 check_constexpr_ctor_body (last, list, /*complain=*/true);
19284 /* Finish the function body. */
19285 finish_function_body (body);
19287 return ctor_initializer_p;
19290 /* Parse an initializer.
19292 initializer:
19293 = initializer-clause
19294 ( expression-list )
19296 Returns an expression representing the initializer. If no
19297 initializer is present, NULL_TREE is returned.
19299 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19300 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19301 set to TRUE if there is no initializer present. If there is an
19302 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19303 is set to true; otherwise it is set to false. */
19305 static tree
19306 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19307 bool* non_constant_p)
19309 cp_token *token;
19310 tree init;
19312 /* Peek at the next token. */
19313 token = cp_lexer_peek_token (parser->lexer);
19315 /* Let our caller know whether or not this initializer was
19316 parenthesized. */
19317 *is_direct_init = (token->type != CPP_EQ);
19318 /* Assume that the initializer is constant. */
19319 *non_constant_p = false;
19321 if (token->type == CPP_EQ)
19323 /* Consume the `='. */
19324 cp_lexer_consume_token (parser->lexer);
19325 /* Parse the initializer-clause. */
19326 init = cp_parser_initializer_clause (parser, non_constant_p);
19328 else if (token->type == CPP_OPEN_PAREN)
19330 vec<tree, va_gc> *vec;
19331 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19332 /*cast_p=*/false,
19333 /*allow_expansion_p=*/true,
19334 non_constant_p);
19335 if (vec == NULL)
19336 return error_mark_node;
19337 init = build_tree_list_vec (vec);
19338 release_tree_vector (vec);
19340 else if (token->type == CPP_OPEN_BRACE)
19342 cp_lexer_set_source_position (parser->lexer);
19343 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19344 init = cp_parser_braced_list (parser, non_constant_p);
19345 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19347 else
19349 /* Anything else is an error. */
19350 cp_parser_error (parser, "expected initializer");
19351 init = error_mark_node;
19354 return init;
19357 /* Parse an initializer-clause.
19359 initializer-clause:
19360 assignment-expression
19361 braced-init-list
19363 Returns an expression representing the initializer.
19365 If the `assignment-expression' production is used the value
19366 returned is simply a representation for the expression.
19368 Otherwise, calls cp_parser_braced_list. */
19370 static tree
19371 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19373 tree initializer;
19375 /* Assume the expression is constant. */
19376 *non_constant_p = false;
19378 /* If it is not a `{', then we are looking at an
19379 assignment-expression. */
19380 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19382 initializer
19383 = cp_parser_constant_expression (parser,
19384 /*allow_non_constant_p=*/true,
19385 non_constant_p);
19387 else
19388 initializer = cp_parser_braced_list (parser, non_constant_p);
19390 return initializer;
19393 /* Parse a brace-enclosed initializer list.
19395 braced-init-list:
19396 { initializer-list , [opt] }
19399 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19400 the elements of the initializer-list (or NULL, if the last
19401 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19402 NULL_TREE. There is no way to detect whether or not the optional
19403 trailing `,' was provided. NON_CONSTANT_P is as for
19404 cp_parser_initializer. */
19406 static tree
19407 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19409 tree initializer;
19411 /* Consume the `{' token. */
19412 cp_lexer_consume_token (parser->lexer);
19413 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19414 initializer = make_node (CONSTRUCTOR);
19415 /* If it's not a `}', then there is a non-trivial initializer. */
19416 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19418 /* Parse the initializer list. */
19419 CONSTRUCTOR_ELTS (initializer)
19420 = cp_parser_initializer_list (parser, non_constant_p);
19421 /* A trailing `,' token is allowed. */
19422 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19423 cp_lexer_consume_token (parser->lexer);
19425 else
19426 *non_constant_p = false;
19427 /* Now, there should be a trailing `}'. */
19428 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19429 TREE_TYPE (initializer) = init_list_type_node;
19430 return initializer;
19433 /* Consume tokens up to, and including, the next non-nested closing `]'.
19434 Returns true iff we found a closing `]'. */
19436 static bool
19437 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19439 unsigned square_depth = 0;
19441 while (true)
19443 cp_token * token = cp_lexer_peek_token (parser->lexer);
19445 switch (token->type)
19447 case CPP_EOF:
19448 case CPP_PRAGMA_EOL:
19449 /* If we've run out of tokens, then there is no closing `]'. */
19450 return false;
19452 case CPP_OPEN_SQUARE:
19453 ++square_depth;
19454 break;
19456 case CPP_CLOSE_SQUARE:
19457 if (!square_depth--)
19459 cp_lexer_consume_token (parser->lexer);
19460 return true;
19462 break;
19464 default:
19465 break;
19468 /* Consume the token. */
19469 cp_lexer_consume_token (parser->lexer);
19473 /* Return true if we are looking at an array-designator, false otherwise. */
19475 static bool
19476 cp_parser_array_designator_p (cp_parser *parser)
19478 /* Consume the `['. */
19479 cp_lexer_consume_token (parser->lexer);
19481 cp_lexer_save_tokens (parser->lexer);
19483 /* Skip tokens until the next token is a closing square bracket.
19484 If we find the closing `]', and the next token is a `=', then
19485 we are looking at an array designator. */
19486 bool array_designator_p
19487 = (cp_parser_skip_to_closing_square_bracket (parser)
19488 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19490 /* Roll back the tokens we skipped. */
19491 cp_lexer_rollback_tokens (parser->lexer);
19493 return array_designator_p;
19496 /* Parse an initializer-list.
19498 initializer-list:
19499 initializer-clause ... [opt]
19500 initializer-list , initializer-clause ... [opt]
19502 GNU Extension:
19504 initializer-list:
19505 designation initializer-clause ...[opt]
19506 initializer-list , designation initializer-clause ...[opt]
19508 designation:
19509 . identifier =
19510 identifier :
19511 [ constant-expression ] =
19513 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19514 for the initializer. If the INDEX of the elt is non-NULL, it is the
19515 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19516 as for cp_parser_initializer. */
19518 static vec<constructor_elt, va_gc> *
19519 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19521 vec<constructor_elt, va_gc> *v = NULL;
19523 /* Assume all of the expressions are constant. */
19524 *non_constant_p = false;
19526 /* Parse the rest of the list. */
19527 while (true)
19529 cp_token *token;
19530 tree designator;
19531 tree initializer;
19532 bool clause_non_constant_p;
19534 /* If the next token is an identifier and the following one is a
19535 colon, we are looking at the GNU designated-initializer
19536 syntax. */
19537 if (cp_parser_allow_gnu_extensions_p (parser)
19538 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19539 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19541 /* Warn the user that they are using an extension. */
19542 pedwarn (input_location, OPT_Wpedantic,
19543 "ISO C++ does not allow designated initializers");
19544 /* Consume the identifier. */
19545 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19546 /* Consume the `:'. */
19547 cp_lexer_consume_token (parser->lexer);
19549 /* Also handle the C99 syntax, '. id ='. */
19550 else if (cp_parser_allow_gnu_extensions_p (parser)
19551 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19552 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19553 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19555 /* Warn the user that they are using an extension. */
19556 pedwarn (input_location, OPT_Wpedantic,
19557 "ISO C++ does not allow C99 designated initializers");
19558 /* Consume the `.'. */
19559 cp_lexer_consume_token (parser->lexer);
19560 /* Consume the identifier. */
19561 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19562 /* Consume the `='. */
19563 cp_lexer_consume_token (parser->lexer);
19565 /* Also handle C99 array designators, '[ const ] ='. */
19566 else if (cp_parser_allow_gnu_extensions_p (parser)
19567 && !c_dialect_objc ()
19568 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19570 /* In C++11, [ could start a lambda-introducer. */
19571 bool non_const = false;
19573 cp_parser_parse_tentatively (parser);
19575 if (!cp_parser_array_designator_p (parser))
19577 cp_parser_simulate_error (parser);
19578 designator = NULL_TREE;
19580 else
19582 designator = cp_parser_constant_expression (parser, true,
19583 &non_const);
19584 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19585 cp_parser_require (parser, CPP_EQ, RT_EQ);
19588 if (!cp_parser_parse_definitely (parser))
19589 designator = NULL_TREE;
19590 else if (non_const)
19591 require_potential_rvalue_constant_expression (designator);
19593 else
19594 designator = NULL_TREE;
19596 /* Parse the initializer. */
19597 initializer = cp_parser_initializer_clause (parser,
19598 &clause_non_constant_p);
19599 /* If any clause is non-constant, so is the entire initializer. */
19600 if (clause_non_constant_p)
19601 *non_constant_p = true;
19603 /* If we have an ellipsis, this is an initializer pack
19604 expansion. */
19605 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19607 /* Consume the `...'. */
19608 cp_lexer_consume_token (parser->lexer);
19610 /* Turn the initializer into an initializer expansion. */
19611 initializer = make_pack_expansion (initializer);
19614 /* Add it to the vector. */
19615 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19617 /* If the next token is not a comma, we have reached the end of
19618 the list. */
19619 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19620 break;
19622 /* Peek at the next token. */
19623 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19624 /* If the next token is a `}', then we're still done. An
19625 initializer-clause can have a trailing `,' after the
19626 initializer-list and before the closing `}'. */
19627 if (token->type == CPP_CLOSE_BRACE)
19628 break;
19630 /* Consume the `,' token. */
19631 cp_lexer_consume_token (parser->lexer);
19634 return v;
19637 /* Classes [gram.class] */
19639 /* Parse a class-name.
19641 class-name:
19642 identifier
19643 template-id
19645 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19646 to indicate that names looked up in dependent types should be
19647 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19648 keyword has been used to indicate that the name that appears next
19649 is a template. TAG_TYPE indicates the explicit tag given before
19650 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19651 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19652 is the class being defined in a class-head. If ENUM_OK is TRUE,
19653 enum-names are also accepted.
19655 Returns the TYPE_DECL representing the class. */
19657 static tree
19658 cp_parser_class_name (cp_parser *parser,
19659 bool typename_keyword_p,
19660 bool template_keyword_p,
19661 enum tag_types tag_type,
19662 bool check_dependency_p,
19663 bool class_head_p,
19664 bool is_declaration,
19665 bool enum_ok)
19667 tree decl;
19668 tree scope;
19669 bool typename_p;
19670 cp_token *token;
19671 tree identifier = NULL_TREE;
19673 /* All class-names start with an identifier. */
19674 token = cp_lexer_peek_token (parser->lexer);
19675 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19677 cp_parser_error (parser, "expected class-name");
19678 return error_mark_node;
19681 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19682 to a template-id, so we save it here. */
19683 scope = parser->scope;
19684 if (scope == error_mark_node)
19685 return error_mark_node;
19687 /* Any name names a type if we're following the `typename' keyword
19688 in a qualified name where the enclosing scope is type-dependent. */
19689 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19690 && dependent_type_p (scope));
19691 /* Handle the common case (an identifier, but not a template-id)
19692 efficiently. */
19693 if (token->type == CPP_NAME
19694 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19696 cp_token *identifier_token;
19697 bool ambiguous_p;
19699 /* Look for the identifier. */
19700 identifier_token = cp_lexer_peek_token (parser->lexer);
19701 ambiguous_p = identifier_token->error_reported;
19702 identifier = cp_parser_identifier (parser);
19703 /* If the next token isn't an identifier, we are certainly not
19704 looking at a class-name. */
19705 if (identifier == error_mark_node)
19706 decl = error_mark_node;
19707 /* If we know this is a type-name, there's no need to look it
19708 up. */
19709 else if (typename_p)
19710 decl = identifier;
19711 else
19713 tree ambiguous_decls;
19714 /* If we already know that this lookup is ambiguous, then
19715 we've already issued an error message; there's no reason
19716 to check again. */
19717 if (ambiguous_p)
19719 cp_parser_simulate_error (parser);
19720 return error_mark_node;
19722 /* If the next token is a `::', then the name must be a type
19723 name.
19725 [basic.lookup.qual]
19727 During the lookup for a name preceding the :: scope
19728 resolution operator, object, function, and enumerator
19729 names are ignored. */
19730 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19731 tag_type = typename_type;
19732 /* Look up the name. */
19733 decl = cp_parser_lookup_name (parser, identifier,
19734 tag_type,
19735 /*is_template=*/false,
19736 /*is_namespace=*/false,
19737 check_dependency_p,
19738 &ambiguous_decls,
19739 identifier_token->location);
19740 if (ambiguous_decls)
19742 if (cp_parser_parsing_tentatively (parser))
19743 cp_parser_simulate_error (parser);
19744 return error_mark_node;
19748 else
19750 /* Try a template-id. */
19751 decl = cp_parser_template_id (parser, template_keyword_p,
19752 check_dependency_p,
19753 tag_type,
19754 is_declaration);
19755 if (decl == error_mark_node)
19756 return error_mark_node;
19759 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19761 /* If this is a typename, create a TYPENAME_TYPE. */
19762 if (typename_p && decl != error_mark_node)
19764 decl = make_typename_type (scope, decl, typename_type,
19765 /*complain=*/tf_error);
19766 if (decl != error_mark_node)
19767 decl = TYPE_NAME (decl);
19770 decl = strip_using_decl (decl);
19772 /* Check to see that it is really the name of a class. */
19773 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19774 && identifier_p (TREE_OPERAND (decl, 0))
19775 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19776 /* Situations like this:
19778 template <typename T> struct A {
19779 typename T::template X<int>::I i;
19782 are problematic. Is `T::template X<int>' a class-name? The
19783 standard does not seem to be definitive, but there is no other
19784 valid interpretation of the following `::'. Therefore, those
19785 names are considered class-names. */
19787 decl = make_typename_type (scope, decl, tag_type, tf_error);
19788 if (decl != error_mark_node)
19789 decl = TYPE_NAME (decl);
19791 else if (TREE_CODE (decl) != TYPE_DECL
19792 || TREE_TYPE (decl) == error_mark_node
19793 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19794 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
19795 /* In Objective-C 2.0, a classname followed by '.' starts a
19796 dot-syntax expression, and it's not a type-name. */
19797 || (c_dialect_objc ()
19798 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19799 && objc_is_class_name (decl)))
19800 decl = error_mark_node;
19802 if (decl == error_mark_node)
19803 cp_parser_error (parser, "expected class-name");
19804 else if (identifier && !parser->scope)
19805 maybe_note_name_used_in_class (identifier, decl);
19807 return decl;
19810 /* Parse a class-specifier.
19812 class-specifier:
19813 class-head { member-specification [opt] }
19815 Returns the TREE_TYPE representing the class. */
19817 static tree
19818 cp_parser_class_specifier_1 (cp_parser* parser)
19820 tree type;
19821 tree attributes = NULL_TREE;
19822 bool nested_name_specifier_p;
19823 unsigned saved_num_template_parameter_lists;
19824 bool saved_in_function_body;
19825 unsigned char in_statement;
19826 bool in_switch_statement_p;
19827 bool saved_in_unbraced_linkage_specification_p;
19828 tree old_scope = NULL_TREE;
19829 tree scope = NULL_TREE;
19830 cp_token *closing_brace;
19832 push_deferring_access_checks (dk_no_deferred);
19834 /* Parse the class-head. */
19835 type = cp_parser_class_head (parser,
19836 &nested_name_specifier_p);
19837 /* If the class-head was a semantic disaster, skip the entire body
19838 of the class. */
19839 if (!type)
19841 cp_parser_skip_to_end_of_block_or_statement (parser);
19842 pop_deferring_access_checks ();
19843 return error_mark_node;
19846 /* Look for the `{'. */
19847 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19849 pop_deferring_access_checks ();
19850 return error_mark_node;
19853 cp_ensure_no_omp_declare_simd (parser);
19855 /* Issue an error message if type-definitions are forbidden here. */
19856 cp_parser_check_type_definition (parser);
19857 /* Remember that we are defining one more class. */
19858 ++parser->num_classes_being_defined;
19859 /* Inside the class, surrounding template-parameter-lists do not
19860 apply. */
19861 saved_num_template_parameter_lists
19862 = parser->num_template_parameter_lists;
19863 parser->num_template_parameter_lists = 0;
19864 /* We are not in a function body. */
19865 saved_in_function_body = parser->in_function_body;
19866 parser->in_function_body = false;
19867 /* Or in a loop. */
19868 in_statement = parser->in_statement;
19869 parser->in_statement = 0;
19870 /* Or in a switch. */
19871 in_switch_statement_p = parser->in_switch_statement_p;
19872 parser->in_switch_statement_p = false;
19873 /* We are not immediately inside an extern "lang" block. */
19874 saved_in_unbraced_linkage_specification_p
19875 = parser->in_unbraced_linkage_specification_p;
19876 parser->in_unbraced_linkage_specification_p = false;
19878 /* Start the class. */
19879 if (nested_name_specifier_p)
19881 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19882 old_scope = push_inner_scope (scope);
19884 type = begin_class_definition (type);
19886 if (type == error_mark_node)
19887 /* If the type is erroneous, skip the entire body of the class. */
19888 cp_parser_skip_to_closing_brace (parser);
19889 else
19890 /* Parse the member-specification. */
19891 cp_parser_member_specification_opt (parser);
19893 /* Look for the trailing `}'. */
19894 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19895 /* Look for trailing attributes to apply to this class. */
19896 if (cp_parser_allow_gnu_extensions_p (parser))
19897 attributes = cp_parser_gnu_attributes_opt (parser);
19898 if (type != error_mark_node)
19899 type = finish_struct (type, attributes);
19900 if (nested_name_specifier_p)
19901 pop_inner_scope (old_scope, scope);
19903 /* We've finished a type definition. Check for the common syntax
19904 error of forgetting a semicolon after the definition. We need to
19905 be careful, as we can't just check for not-a-semicolon and be done
19906 with it; the user might have typed:
19908 class X { } c = ...;
19909 class X { } *p = ...;
19911 and so forth. Instead, enumerate all the possible tokens that
19912 might follow this production; if we don't see one of them, then
19913 complain and silently insert the semicolon. */
19915 cp_token *token = cp_lexer_peek_token (parser->lexer);
19916 bool want_semicolon = true;
19918 if (cp_next_tokens_can_be_std_attribute_p (parser))
19919 /* Don't try to parse c++11 attributes here. As per the
19920 grammar, that should be a task for
19921 cp_parser_decl_specifier_seq. */
19922 want_semicolon = false;
19924 switch (token->type)
19926 case CPP_NAME:
19927 case CPP_SEMICOLON:
19928 case CPP_MULT:
19929 case CPP_AND:
19930 case CPP_OPEN_PAREN:
19931 case CPP_CLOSE_PAREN:
19932 case CPP_COMMA:
19933 want_semicolon = false;
19934 break;
19936 /* While it's legal for type qualifiers and storage class
19937 specifiers to follow type definitions in the grammar, only
19938 compiler testsuites contain code like that. Assume that if
19939 we see such code, then what we're really seeing is a case
19940 like:
19942 class X { }
19943 const <type> var = ...;
19947 class Y { }
19948 static <type> func (...) ...
19950 i.e. the qualifier or specifier applies to the next
19951 declaration. To do so, however, we need to look ahead one
19952 more token to see if *that* token is a type specifier.
19954 This code could be improved to handle:
19956 class Z { }
19957 static const <type> var = ...; */
19958 case CPP_KEYWORD:
19959 if (keyword_is_decl_specifier (token->keyword))
19961 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19963 /* Handling user-defined types here would be nice, but very
19964 tricky. */
19965 want_semicolon
19966 = (lookahead->type == CPP_KEYWORD
19967 && keyword_begins_type_specifier (lookahead->keyword));
19969 break;
19970 default:
19971 break;
19974 /* If we don't have a type, then something is very wrong and we
19975 shouldn't try to do anything clever. Likewise for not seeing the
19976 closing brace. */
19977 if (closing_brace && TYPE_P (type) && want_semicolon)
19979 cp_token_position prev
19980 = cp_lexer_previous_token_position (parser->lexer);
19981 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19982 location_t loc = prev_token->location;
19984 if (CLASSTYPE_DECLARED_CLASS (type))
19985 error_at (loc, "expected %<;%> after class definition");
19986 else if (TREE_CODE (type) == RECORD_TYPE)
19987 error_at (loc, "expected %<;%> after struct definition");
19988 else if (TREE_CODE (type) == UNION_TYPE)
19989 error_at (loc, "expected %<;%> after union definition");
19990 else
19991 gcc_unreachable ();
19993 /* Unget one token and smash it to look as though we encountered
19994 a semicolon in the input stream. */
19995 cp_lexer_set_token_position (parser->lexer, prev);
19996 token = cp_lexer_peek_token (parser->lexer);
19997 token->type = CPP_SEMICOLON;
19998 token->keyword = RID_MAX;
20002 /* If this class is not itself within the scope of another class,
20003 then we need to parse the bodies of all of the queued function
20004 definitions. Note that the queued functions defined in a class
20005 are not always processed immediately following the
20006 class-specifier for that class. Consider:
20008 struct A {
20009 struct B { void f() { sizeof (A); } };
20012 If `f' were processed before the processing of `A' were
20013 completed, there would be no way to compute the size of `A'.
20014 Note that the nesting we are interested in here is lexical --
20015 not the semantic nesting given by TYPE_CONTEXT. In particular,
20016 for:
20018 struct A { struct B; };
20019 struct A::B { void f() { } };
20021 there is no need to delay the parsing of `A::B::f'. */
20022 if (--parser->num_classes_being_defined == 0)
20024 tree decl;
20025 tree class_type = NULL_TREE;
20026 tree pushed_scope = NULL_TREE;
20027 unsigned ix;
20028 cp_default_arg_entry *e;
20029 tree save_ccp, save_ccr;
20031 /* In a first pass, parse default arguments to the functions.
20032 Then, in a second pass, parse the bodies of the functions.
20033 This two-phased approach handles cases like:
20035 struct S {
20036 void f() { g(); }
20037 void g(int i = 3);
20041 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
20043 decl = e->decl;
20044 /* If there are default arguments that have not yet been processed,
20045 take care of them now. */
20046 if (class_type != e->class_type)
20048 if (pushed_scope)
20049 pop_scope (pushed_scope);
20050 class_type = e->class_type;
20051 pushed_scope = push_scope (class_type);
20053 /* Make sure that any template parameters are in scope. */
20054 maybe_begin_member_template_processing (decl);
20055 /* Parse the default argument expressions. */
20056 cp_parser_late_parsing_default_args (parser, decl);
20057 /* Remove any template parameters from the symbol table. */
20058 maybe_end_member_template_processing ();
20060 vec_safe_truncate (unparsed_funs_with_default_args, 0);
20061 /* Now parse any NSDMIs. */
20062 save_ccp = current_class_ptr;
20063 save_ccr = current_class_ref;
20064 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20066 if (class_type != DECL_CONTEXT (decl))
20068 if (pushed_scope)
20069 pop_scope (pushed_scope);
20070 class_type = DECL_CONTEXT (decl);
20071 pushed_scope = push_scope (class_type);
20073 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20074 cp_parser_late_parsing_nsdmi (parser, decl);
20076 vec_safe_truncate (unparsed_nsdmis, 0);
20077 current_class_ptr = save_ccp;
20078 current_class_ref = save_ccr;
20079 if (pushed_scope)
20080 pop_scope (pushed_scope);
20082 /* Now do some post-NSDMI bookkeeping. */
20083 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20084 after_nsdmi_defaulted_late_checks (class_type);
20085 vec_safe_truncate (unparsed_classes, 0);
20086 after_nsdmi_defaulted_late_checks (type);
20088 /* Now parse the body of the functions. */
20089 if (flag_openmp)
20091 /* OpenMP UDRs need to be parsed before all other functions. */
20092 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20093 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20094 cp_parser_late_parsing_for_member (parser, decl);
20095 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20096 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20097 cp_parser_late_parsing_for_member (parser, decl);
20099 else
20100 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20101 cp_parser_late_parsing_for_member (parser, decl);
20102 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20104 else
20105 vec_safe_push (unparsed_classes, type);
20107 /* Put back any saved access checks. */
20108 pop_deferring_access_checks ();
20110 /* Restore saved state. */
20111 parser->in_switch_statement_p = in_switch_statement_p;
20112 parser->in_statement = in_statement;
20113 parser->in_function_body = saved_in_function_body;
20114 parser->num_template_parameter_lists
20115 = saved_num_template_parameter_lists;
20116 parser->in_unbraced_linkage_specification_p
20117 = saved_in_unbraced_linkage_specification_p;
20119 return type;
20122 static tree
20123 cp_parser_class_specifier (cp_parser* parser)
20125 tree ret;
20126 timevar_push (TV_PARSE_STRUCT);
20127 ret = cp_parser_class_specifier_1 (parser);
20128 timevar_pop (TV_PARSE_STRUCT);
20129 return ret;
20132 /* Parse a class-head.
20134 class-head:
20135 class-key identifier [opt] base-clause [opt]
20136 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20137 class-key nested-name-specifier [opt] template-id
20138 base-clause [opt]
20140 class-virt-specifier:
20141 final
20143 GNU Extensions:
20144 class-key attributes identifier [opt] base-clause [opt]
20145 class-key attributes nested-name-specifier identifier base-clause [opt]
20146 class-key attributes nested-name-specifier [opt] template-id
20147 base-clause [opt]
20149 Upon return BASES is initialized to the list of base classes (or
20150 NULL, if there are none) in the same form returned by
20151 cp_parser_base_clause.
20153 Returns the TYPE of the indicated class. Sets
20154 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20155 involving a nested-name-specifier was used, and FALSE otherwise.
20157 Returns error_mark_node if this is not a class-head.
20159 Returns NULL_TREE if the class-head is syntactically valid, but
20160 semantically invalid in a way that means we should skip the entire
20161 body of the class. */
20163 static tree
20164 cp_parser_class_head (cp_parser* parser,
20165 bool* nested_name_specifier_p)
20167 tree nested_name_specifier;
20168 enum tag_types class_key;
20169 tree id = NULL_TREE;
20170 tree type = NULL_TREE;
20171 tree attributes;
20172 tree bases;
20173 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20174 bool template_id_p = false;
20175 bool qualified_p = false;
20176 bool invalid_nested_name_p = false;
20177 bool invalid_explicit_specialization_p = false;
20178 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20179 tree pushed_scope = NULL_TREE;
20180 unsigned num_templates;
20181 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20182 /* Assume no nested-name-specifier will be present. */
20183 *nested_name_specifier_p = false;
20184 /* Assume no template parameter lists will be used in defining the
20185 type. */
20186 num_templates = 0;
20187 parser->colon_corrects_to_scope_p = false;
20189 /* Look for the class-key. */
20190 class_key = cp_parser_class_key (parser);
20191 if (class_key == none_type)
20192 return error_mark_node;
20194 /* Parse the attributes. */
20195 attributes = cp_parser_attributes_opt (parser);
20197 /* If the next token is `::', that is invalid -- but sometimes
20198 people do try to write:
20200 struct ::S {};
20202 Handle this gracefully by accepting the extra qualifier, and then
20203 issuing an error about it later if this really is a
20204 class-head. If it turns out just to be an elaborated type
20205 specifier, remain silent. */
20206 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20207 qualified_p = true;
20209 push_deferring_access_checks (dk_no_check);
20211 /* Determine the name of the class. Begin by looking for an
20212 optional nested-name-specifier. */
20213 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20214 nested_name_specifier
20215 = cp_parser_nested_name_specifier_opt (parser,
20216 /*typename_keyword_p=*/false,
20217 /*check_dependency_p=*/false,
20218 /*type_p=*/true,
20219 /*is_declaration=*/false);
20220 /* If there was a nested-name-specifier, then there *must* be an
20221 identifier. */
20222 if (nested_name_specifier)
20224 type_start_token = cp_lexer_peek_token (parser->lexer);
20225 /* Although the grammar says `identifier', it really means
20226 `class-name' or `template-name'. You are only allowed to
20227 define a class that has already been declared with this
20228 syntax.
20230 The proposed resolution for Core Issue 180 says that wherever
20231 you see `class T::X' you should treat `X' as a type-name.
20233 It is OK to define an inaccessible class; for example:
20235 class A { class B; };
20236 class A::B {};
20238 We do not know if we will see a class-name, or a
20239 template-name. We look for a class-name first, in case the
20240 class-name is a template-id; if we looked for the
20241 template-name first we would stop after the template-name. */
20242 cp_parser_parse_tentatively (parser);
20243 type = cp_parser_class_name (parser,
20244 /*typename_keyword_p=*/false,
20245 /*template_keyword_p=*/false,
20246 class_type,
20247 /*check_dependency_p=*/false,
20248 /*class_head_p=*/true,
20249 /*is_declaration=*/false);
20250 /* If that didn't work, ignore the nested-name-specifier. */
20251 if (!cp_parser_parse_definitely (parser))
20253 invalid_nested_name_p = true;
20254 type_start_token = cp_lexer_peek_token (parser->lexer);
20255 id = cp_parser_identifier (parser);
20256 if (id == error_mark_node)
20257 id = NULL_TREE;
20259 /* If we could not find a corresponding TYPE, treat this
20260 declaration like an unqualified declaration. */
20261 if (type == error_mark_node)
20262 nested_name_specifier = NULL_TREE;
20263 /* Otherwise, count the number of templates used in TYPE and its
20264 containing scopes. */
20265 else
20267 tree scope;
20269 for (scope = TREE_TYPE (type);
20270 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20271 scope = get_containing_scope (scope))
20272 if (TYPE_P (scope)
20273 && CLASS_TYPE_P (scope)
20274 && CLASSTYPE_TEMPLATE_INFO (scope)
20275 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20276 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20277 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20278 ++num_templates;
20281 /* Otherwise, the identifier is optional. */
20282 else
20284 /* We don't know whether what comes next is a template-id,
20285 an identifier, or nothing at all. */
20286 cp_parser_parse_tentatively (parser);
20287 /* Check for a template-id. */
20288 type_start_token = cp_lexer_peek_token (parser->lexer);
20289 id = cp_parser_template_id (parser,
20290 /*template_keyword_p=*/false,
20291 /*check_dependency_p=*/true,
20292 class_key,
20293 /*is_declaration=*/true);
20294 /* If that didn't work, it could still be an identifier. */
20295 if (!cp_parser_parse_definitely (parser))
20297 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20299 type_start_token = cp_lexer_peek_token (parser->lexer);
20300 id = cp_parser_identifier (parser);
20302 else
20303 id = NULL_TREE;
20305 else
20307 template_id_p = true;
20308 ++num_templates;
20312 pop_deferring_access_checks ();
20314 if (id)
20316 cp_parser_check_for_invalid_template_id (parser, id,
20317 class_key,
20318 type_start_token->location);
20320 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20322 /* If it's not a `:' or a `{' then we can't really be looking at a
20323 class-head, since a class-head only appears as part of a
20324 class-specifier. We have to detect this situation before calling
20325 xref_tag, since that has irreversible side-effects. */
20326 if (!cp_parser_next_token_starts_class_definition_p (parser))
20328 cp_parser_error (parser, "expected %<{%> or %<:%>");
20329 type = error_mark_node;
20330 goto out;
20333 /* At this point, we're going ahead with the class-specifier, even
20334 if some other problem occurs. */
20335 cp_parser_commit_to_tentative_parse (parser);
20336 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20338 cp_parser_error (parser,
20339 "cannot specify %<override%> for a class");
20340 type = error_mark_node;
20341 goto out;
20343 /* Issue the error about the overly-qualified name now. */
20344 if (qualified_p)
20346 cp_parser_error (parser,
20347 "global qualification of class name is invalid");
20348 type = error_mark_node;
20349 goto out;
20351 else if (invalid_nested_name_p)
20353 cp_parser_error (parser,
20354 "qualified name does not name a class");
20355 type = error_mark_node;
20356 goto out;
20358 else if (nested_name_specifier)
20360 tree scope;
20362 /* Reject typedef-names in class heads. */
20363 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20365 error_at (type_start_token->location,
20366 "invalid class name in declaration of %qD",
20367 type);
20368 type = NULL_TREE;
20369 goto done;
20372 /* Figure out in what scope the declaration is being placed. */
20373 scope = current_scope ();
20374 /* If that scope does not contain the scope in which the
20375 class was originally declared, the program is invalid. */
20376 if (scope && !is_ancestor (scope, nested_name_specifier))
20378 if (at_namespace_scope_p ())
20379 error_at (type_start_token->location,
20380 "declaration of %qD in namespace %qD which does not "
20381 "enclose %qD",
20382 type, scope, nested_name_specifier);
20383 else
20384 error_at (type_start_token->location,
20385 "declaration of %qD in %qD which does not enclose %qD",
20386 type, scope, nested_name_specifier);
20387 type = NULL_TREE;
20388 goto done;
20390 /* [dcl.meaning]
20392 A declarator-id shall not be qualified except for the
20393 definition of a ... nested class outside of its class
20394 ... [or] the definition or explicit instantiation of a
20395 class member of a namespace outside of its namespace. */
20396 if (scope == nested_name_specifier)
20398 permerror (nested_name_specifier_token_start->location,
20399 "extra qualification not allowed");
20400 nested_name_specifier = NULL_TREE;
20401 num_templates = 0;
20404 /* An explicit-specialization must be preceded by "template <>". If
20405 it is not, try to recover gracefully. */
20406 if (at_namespace_scope_p ()
20407 && parser->num_template_parameter_lists == 0
20408 && template_id_p)
20410 error_at (type_start_token->location,
20411 "an explicit specialization must be preceded by %<template <>%>");
20412 invalid_explicit_specialization_p = true;
20413 /* Take the same action that would have been taken by
20414 cp_parser_explicit_specialization. */
20415 ++parser->num_template_parameter_lists;
20416 begin_specialization ();
20418 /* There must be no "return" statements between this point and the
20419 end of this function; set "type "to the correct return value and
20420 use "goto done;" to return. */
20421 /* Make sure that the right number of template parameters were
20422 present. */
20423 if (!cp_parser_check_template_parameters (parser, num_templates,
20424 type_start_token->location,
20425 /*declarator=*/NULL))
20427 /* If something went wrong, there is no point in even trying to
20428 process the class-definition. */
20429 type = NULL_TREE;
20430 goto done;
20433 /* Look up the type. */
20434 if (template_id_p)
20436 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20437 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20438 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20440 error_at (type_start_token->location,
20441 "function template %qD redeclared as a class template", id);
20442 type = error_mark_node;
20444 else
20446 type = TREE_TYPE (id);
20447 type = maybe_process_partial_specialization (type);
20449 if (nested_name_specifier)
20450 pushed_scope = push_scope (nested_name_specifier);
20452 else if (nested_name_specifier)
20454 tree class_type;
20456 /* Given:
20458 template <typename T> struct S { struct T };
20459 template <typename T> struct S<T>::T { };
20461 we will get a TYPENAME_TYPE when processing the definition of
20462 `S::T'. We need to resolve it to the actual type before we
20463 try to define it. */
20464 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20466 class_type = resolve_typename_type (TREE_TYPE (type),
20467 /*only_current_p=*/false);
20468 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20469 type = TYPE_NAME (class_type);
20470 else
20472 cp_parser_error (parser, "could not resolve typename type");
20473 type = error_mark_node;
20477 if (maybe_process_partial_specialization (TREE_TYPE (type))
20478 == error_mark_node)
20480 type = NULL_TREE;
20481 goto done;
20484 class_type = current_class_type;
20485 /* Enter the scope indicated by the nested-name-specifier. */
20486 pushed_scope = push_scope (nested_name_specifier);
20487 /* Get the canonical version of this type. */
20488 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20489 /* Call push_template_decl if it seems like we should be defining a
20490 template either from the template headers or the type we're
20491 defining, so that we diagnose both extra and missing headers. */
20492 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20493 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20494 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20496 type = push_template_decl (type);
20497 if (type == error_mark_node)
20499 type = NULL_TREE;
20500 goto done;
20504 type = TREE_TYPE (type);
20505 *nested_name_specifier_p = true;
20507 else /* The name is not a nested name. */
20509 /* If the class was unnamed, create a dummy name. */
20510 if (!id)
20511 id = make_anon_name ();
20512 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20513 parser->num_template_parameter_lists);
20516 /* Indicate whether this class was declared as a `class' or as a
20517 `struct'. */
20518 if (TREE_CODE (type) == RECORD_TYPE)
20519 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20520 cp_parser_check_class_key (class_key, type);
20522 /* If this type was already complete, and we see another definition,
20523 that's an error. */
20524 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20526 error_at (type_start_token->location, "redefinition of %q#T",
20527 type);
20528 error_at (type_start_token->location, "previous definition of %q+#T",
20529 type);
20530 type = NULL_TREE;
20531 goto done;
20533 else if (type == error_mark_node)
20534 type = NULL_TREE;
20536 if (type)
20538 /* Apply attributes now, before any use of the class as a template
20539 argument in its base list. */
20540 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20541 fixup_attribute_variants (type);
20544 /* We will have entered the scope containing the class; the names of
20545 base classes should be looked up in that context. For example:
20547 struct A { struct B {}; struct C; };
20548 struct A::C : B {};
20550 is valid. */
20552 /* Get the list of base-classes, if there is one. */
20553 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20555 /* PR59482: enter the class scope so that base-specifiers are looked
20556 up correctly. */
20557 if (type)
20558 pushclass (type);
20559 bases = cp_parser_base_clause (parser);
20560 /* PR59482: get out of the previously pushed class scope so that the
20561 subsequent pops pop the right thing. */
20562 if (type)
20563 popclass ();
20565 else
20566 bases = NULL_TREE;
20568 /* If we're really defining a class, process the base classes.
20569 If they're invalid, fail. */
20570 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20571 && !xref_basetypes (type, bases))
20572 type = NULL_TREE;
20574 done:
20575 /* Leave the scope given by the nested-name-specifier. We will
20576 enter the class scope itself while processing the members. */
20577 if (pushed_scope)
20578 pop_scope (pushed_scope);
20580 if (invalid_explicit_specialization_p)
20582 end_specialization ();
20583 --parser->num_template_parameter_lists;
20586 if (type)
20587 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20588 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20589 CLASSTYPE_FINAL (type) = 1;
20590 out:
20591 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20592 return type;
20595 /* Parse a class-key.
20597 class-key:
20598 class
20599 struct
20600 union
20602 Returns the kind of class-key specified, or none_type to indicate
20603 error. */
20605 static enum tag_types
20606 cp_parser_class_key (cp_parser* parser)
20608 cp_token *token;
20609 enum tag_types tag_type;
20611 /* Look for the class-key. */
20612 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20613 if (!token)
20614 return none_type;
20616 /* Check to see if the TOKEN is a class-key. */
20617 tag_type = cp_parser_token_is_class_key (token);
20618 if (!tag_type)
20619 cp_parser_error (parser, "expected class-key");
20620 return tag_type;
20623 /* Parse a type-parameter-key.
20625 type-parameter-key:
20626 class
20627 typename
20630 static void
20631 cp_parser_type_parameter_key (cp_parser* parser)
20633 /* Look for the type-parameter-key. */
20634 enum tag_types tag_type = none_type;
20635 cp_token *token = cp_lexer_peek_token (parser->lexer);
20636 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20638 cp_lexer_consume_token (parser->lexer);
20639 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20640 /* typename is not allowed in a template template parameter
20641 by the standard until C++1Z. */
20642 pedwarn (token->location, OPT_Wpedantic,
20643 "ISO C++ forbids typename key in template template parameter;"
20644 " use -std=c++1z or -std=gnu++1z");
20646 else
20647 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20649 return;
20652 /* Parse an (optional) member-specification.
20654 member-specification:
20655 member-declaration member-specification [opt]
20656 access-specifier : member-specification [opt] */
20658 static void
20659 cp_parser_member_specification_opt (cp_parser* parser)
20661 while (true)
20663 cp_token *token;
20664 enum rid keyword;
20666 /* Peek at the next token. */
20667 token = cp_lexer_peek_token (parser->lexer);
20668 /* If it's a `}', or EOF then we've seen all the members. */
20669 if (token->type == CPP_CLOSE_BRACE
20670 || token->type == CPP_EOF
20671 || token->type == CPP_PRAGMA_EOL)
20672 break;
20674 /* See if this token is a keyword. */
20675 keyword = token->keyword;
20676 switch (keyword)
20678 case RID_PUBLIC:
20679 case RID_PROTECTED:
20680 case RID_PRIVATE:
20681 /* Consume the access-specifier. */
20682 cp_lexer_consume_token (parser->lexer);
20683 /* Remember which access-specifier is active. */
20684 current_access_specifier = token->u.value;
20685 /* Look for the `:'. */
20686 cp_parser_require (parser, CPP_COLON, RT_COLON);
20687 break;
20689 default:
20690 /* Accept #pragmas at class scope. */
20691 if (token->type == CPP_PRAGMA)
20693 cp_parser_pragma (parser, pragma_member);
20694 break;
20697 /* Otherwise, the next construction must be a
20698 member-declaration. */
20699 cp_parser_member_declaration (parser);
20704 /* Parse a member-declaration.
20706 member-declaration:
20707 decl-specifier-seq [opt] member-declarator-list [opt] ;
20708 function-definition ; [opt]
20709 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20710 using-declaration
20711 template-declaration
20712 alias-declaration
20714 member-declarator-list:
20715 member-declarator
20716 member-declarator-list , member-declarator
20718 member-declarator:
20719 declarator pure-specifier [opt]
20720 declarator constant-initializer [opt]
20721 identifier [opt] : constant-expression
20723 GNU Extensions:
20725 member-declaration:
20726 __extension__ member-declaration
20728 member-declarator:
20729 declarator attributes [opt] pure-specifier [opt]
20730 declarator attributes [opt] constant-initializer [opt]
20731 identifier [opt] attributes [opt] : constant-expression
20733 C++0x Extensions:
20735 member-declaration:
20736 static_assert-declaration */
20738 static void
20739 cp_parser_member_declaration (cp_parser* parser)
20741 cp_decl_specifier_seq decl_specifiers;
20742 tree prefix_attributes;
20743 tree decl;
20744 int declares_class_or_enum;
20745 bool friend_p;
20746 cp_token *token = NULL;
20747 cp_token *decl_spec_token_start = NULL;
20748 cp_token *initializer_token_start = NULL;
20749 int saved_pedantic;
20750 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20752 /* Check for the `__extension__' keyword. */
20753 if (cp_parser_extension_opt (parser, &saved_pedantic))
20755 /* Recurse. */
20756 cp_parser_member_declaration (parser);
20757 /* Restore the old value of the PEDANTIC flag. */
20758 pedantic = saved_pedantic;
20760 return;
20763 /* Check for a template-declaration. */
20764 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20766 /* An explicit specialization here is an error condition, and we
20767 expect the specialization handler to detect and report this. */
20768 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20769 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20770 cp_parser_explicit_specialization (parser);
20771 else
20772 cp_parser_template_declaration (parser, /*member_p=*/true);
20774 return;
20777 /* Check for a using-declaration. */
20778 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20780 if (cxx_dialect < cxx11)
20782 /* Parse the using-declaration. */
20783 cp_parser_using_declaration (parser,
20784 /*access_declaration_p=*/false);
20785 return;
20787 else
20789 tree decl;
20790 bool alias_decl_expected;
20791 cp_parser_parse_tentatively (parser);
20792 decl = cp_parser_alias_declaration (parser);
20793 /* Note that if we actually see the '=' token after the
20794 identifier, cp_parser_alias_declaration commits the
20795 tentative parse. In that case, we really expect an
20796 alias-declaration. Otherwise, we expect a using
20797 declaration. */
20798 alias_decl_expected =
20799 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20800 cp_parser_parse_definitely (parser);
20802 if (alias_decl_expected)
20803 finish_member_declaration (decl);
20804 else
20805 cp_parser_using_declaration (parser,
20806 /*access_declaration_p=*/false);
20807 return;
20811 /* Check for @defs. */
20812 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20814 tree ivar, member;
20815 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20816 ivar = ivar_chains;
20817 while (ivar)
20819 member = ivar;
20820 ivar = TREE_CHAIN (member);
20821 TREE_CHAIN (member) = NULL_TREE;
20822 finish_member_declaration (member);
20824 return;
20827 /* If the next token is `static_assert' we have a static assertion. */
20828 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20830 cp_parser_static_assert (parser, /*member_p=*/true);
20831 return;
20834 parser->colon_corrects_to_scope_p = false;
20836 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20837 goto out;
20839 /* Parse the decl-specifier-seq. */
20840 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20841 cp_parser_decl_specifier_seq (parser,
20842 CP_PARSER_FLAGS_OPTIONAL,
20843 &decl_specifiers,
20844 &declares_class_or_enum);
20845 /* Check for an invalid type-name. */
20846 if (!decl_specifiers.any_type_specifiers_p
20847 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20848 goto out;
20849 /* If there is no declarator, then the decl-specifier-seq should
20850 specify a type. */
20851 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20853 /* If there was no decl-specifier-seq, and the next token is a
20854 `;', then we have something like:
20856 struct S { ; };
20858 [class.mem]
20860 Each member-declaration shall declare at least one member
20861 name of the class. */
20862 if (!decl_specifiers.any_specifiers_p)
20864 cp_token *token = cp_lexer_peek_token (parser->lexer);
20865 if (!in_system_header_at (token->location))
20866 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20868 else
20870 tree type;
20872 /* See if this declaration is a friend. */
20873 friend_p = cp_parser_friend_p (&decl_specifiers);
20874 /* If there were decl-specifiers, check to see if there was
20875 a class-declaration. */
20876 type = check_tag_decl (&decl_specifiers,
20877 /*explicit_type_instantiation_p=*/false);
20878 /* Nested classes have already been added to the class, but
20879 a `friend' needs to be explicitly registered. */
20880 if (friend_p)
20882 /* If the `friend' keyword was present, the friend must
20883 be introduced with a class-key. */
20884 if (!declares_class_or_enum && cxx_dialect < cxx11)
20885 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20886 "in C++03 a class-key must be used "
20887 "when declaring a friend");
20888 /* In this case:
20890 template <typename T> struct A {
20891 friend struct A<T>::B;
20894 A<T>::B will be represented by a TYPENAME_TYPE, and
20895 therefore not recognized by check_tag_decl. */
20896 if (!type)
20898 type = decl_specifiers.type;
20899 if (type && TREE_CODE (type) == TYPE_DECL)
20900 type = TREE_TYPE (type);
20902 if (!type || !TYPE_P (type))
20903 error_at (decl_spec_token_start->location,
20904 "friend declaration does not name a class or "
20905 "function");
20906 else
20907 make_friend_class (current_class_type, type,
20908 /*complain=*/true);
20910 /* If there is no TYPE, an error message will already have
20911 been issued. */
20912 else if (!type || type == error_mark_node)
20914 /* An anonymous aggregate has to be handled specially; such
20915 a declaration really declares a data member (with a
20916 particular type), as opposed to a nested class. */
20917 else if (ANON_AGGR_TYPE_P (type))
20919 /* C++11 9.5/6. */
20920 if (decl_specifiers.storage_class != sc_none)
20921 error_at (decl_spec_token_start->location,
20922 "a storage class on an anonymous aggregate "
20923 "in class scope is not allowed");
20925 /* Remove constructors and such from TYPE, now that we
20926 know it is an anonymous aggregate. */
20927 fixup_anonymous_aggr (type);
20928 /* And make the corresponding data member. */
20929 decl = build_decl (decl_spec_token_start->location,
20930 FIELD_DECL, NULL_TREE, type);
20931 /* Add it to the class. */
20932 finish_member_declaration (decl);
20934 else
20935 cp_parser_check_access_in_redeclaration
20936 (TYPE_NAME (type),
20937 decl_spec_token_start->location);
20940 else
20942 bool assume_semicolon = false;
20944 /* Clear attributes from the decl_specifiers but keep them
20945 around as prefix attributes that apply them to the entity
20946 being declared. */
20947 prefix_attributes = decl_specifiers.attributes;
20948 decl_specifiers.attributes = NULL_TREE;
20950 /* See if these declarations will be friends. */
20951 friend_p = cp_parser_friend_p (&decl_specifiers);
20953 /* Keep going until we hit the `;' at the end of the
20954 declaration. */
20955 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20957 tree attributes = NULL_TREE;
20958 tree first_attribute;
20960 /* Peek at the next token. */
20961 token = cp_lexer_peek_token (parser->lexer);
20963 /* Check for a bitfield declaration. */
20964 if (token->type == CPP_COLON
20965 || (token->type == CPP_NAME
20966 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20967 == CPP_COLON))
20969 tree identifier;
20970 tree width;
20972 /* Get the name of the bitfield. Note that we cannot just
20973 check TOKEN here because it may have been invalidated by
20974 the call to cp_lexer_peek_nth_token above. */
20975 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20976 identifier = cp_parser_identifier (parser);
20977 else
20978 identifier = NULL_TREE;
20980 /* Consume the `:' token. */
20981 cp_lexer_consume_token (parser->lexer);
20982 /* Get the width of the bitfield. */
20983 width
20984 = cp_parser_constant_expression (parser);
20986 /* Look for attributes that apply to the bitfield. */
20987 attributes = cp_parser_attributes_opt (parser);
20988 /* Remember which attributes are prefix attributes and
20989 which are not. */
20990 first_attribute = attributes;
20991 /* Combine the attributes. */
20992 attributes = chainon (prefix_attributes, attributes);
20994 /* Create the bitfield declaration. */
20995 decl = grokbitfield (identifier
20996 ? make_id_declarator (NULL_TREE,
20997 identifier,
20998 sfk_none)
20999 : NULL,
21000 &decl_specifiers,
21001 width,
21002 attributes);
21004 else
21006 cp_declarator *declarator;
21007 tree initializer;
21008 tree asm_specification;
21009 int ctor_dtor_or_conv_p;
21011 /* Parse the declarator. */
21012 declarator
21013 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21014 &ctor_dtor_or_conv_p,
21015 /*parenthesized_p=*/NULL,
21016 /*member_p=*/true,
21017 friend_p);
21019 /* If something went wrong parsing the declarator, make sure
21020 that we at least consume some tokens. */
21021 if (declarator == cp_error_declarator)
21023 /* Skip to the end of the statement. */
21024 cp_parser_skip_to_end_of_statement (parser);
21025 /* If the next token is not a semicolon, that is
21026 probably because we just skipped over the body of
21027 a function. So, we consume a semicolon if
21028 present, but do not issue an error message if it
21029 is not present. */
21030 if (cp_lexer_next_token_is (parser->lexer,
21031 CPP_SEMICOLON))
21032 cp_lexer_consume_token (parser->lexer);
21033 goto out;
21036 if (declares_class_or_enum & 2)
21037 cp_parser_check_for_definition_in_return_type
21038 (declarator, decl_specifiers.type,
21039 decl_specifiers.locations[ds_type_spec]);
21041 /* Look for an asm-specification. */
21042 asm_specification = cp_parser_asm_specification_opt (parser);
21043 /* Look for attributes that apply to the declaration. */
21044 attributes = cp_parser_attributes_opt (parser);
21045 /* Remember which attributes are prefix attributes and
21046 which are not. */
21047 first_attribute = attributes;
21048 /* Combine the attributes. */
21049 attributes = chainon (prefix_attributes, attributes);
21051 /* If it's an `=', then we have a constant-initializer or a
21052 pure-specifier. It is not correct to parse the
21053 initializer before registering the member declaration
21054 since the member declaration should be in scope while
21055 its initializer is processed. However, the rest of the
21056 front end does not yet provide an interface that allows
21057 us to handle this correctly. */
21058 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21060 /* In [class.mem]:
21062 A pure-specifier shall be used only in the declaration of
21063 a virtual function.
21065 A member-declarator can contain a constant-initializer
21066 only if it declares a static member of integral or
21067 enumeration type.
21069 Therefore, if the DECLARATOR is for a function, we look
21070 for a pure-specifier; otherwise, we look for a
21071 constant-initializer. When we call `grokfield', it will
21072 perform more stringent semantics checks. */
21073 initializer_token_start = cp_lexer_peek_token (parser->lexer);
21074 if (function_declarator_p (declarator)
21075 || (decl_specifiers.type
21076 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21077 && declarator->kind == cdk_id
21078 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21079 == FUNCTION_TYPE)))
21080 initializer = cp_parser_pure_specifier (parser);
21081 else if (decl_specifiers.storage_class != sc_static)
21082 initializer = cp_parser_save_nsdmi (parser);
21083 else if (cxx_dialect >= cxx11)
21085 bool nonconst;
21086 /* Don't require a constant rvalue in C++11, since we
21087 might want a reference constant. We'll enforce
21088 constancy later. */
21089 cp_lexer_consume_token (parser->lexer);
21090 /* Parse the initializer. */
21091 initializer = cp_parser_initializer_clause (parser,
21092 &nonconst);
21094 else
21095 /* Parse the initializer. */
21096 initializer = cp_parser_constant_initializer (parser);
21098 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21099 && !function_declarator_p (declarator))
21101 bool x;
21102 if (decl_specifiers.storage_class != sc_static)
21103 initializer = cp_parser_save_nsdmi (parser);
21104 else
21105 initializer = cp_parser_initializer (parser, &x, &x);
21107 /* Otherwise, there is no initializer. */
21108 else
21109 initializer = NULL_TREE;
21111 /* See if we are probably looking at a function
21112 definition. We are certainly not looking at a
21113 member-declarator. Calling `grokfield' has
21114 side-effects, so we must not do it unless we are sure
21115 that we are looking at a member-declarator. */
21116 if (cp_parser_token_starts_function_definition_p
21117 (cp_lexer_peek_token (parser->lexer)))
21119 /* The grammar does not allow a pure-specifier to be
21120 used when a member function is defined. (It is
21121 possible that this fact is an oversight in the
21122 standard, since a pure function may be defined
21123 outside of the class-specifier. */
21124 if (initializer && initializer_token_start)
21125 error_at (initializer_token_start->location,
21126 "pure-specifier on function-definition");
21127 decl = cp_parser_save_member_function_body (parser,
21128 &decl_specifiers,
21129 declarator,
21130 attributes);
21131 if (parser->fully_implicit_function_template_p)
21132 decl = finish_fully_implicit_template (parser, decl);
21133 /* If the member was not a friend, declare it here. */
21134 if (!friend_p)
21135 finish_member_declaration (decl);
21136 /* Peek at the next token. */
21137 token = cp_lexer_peek_token (parser->lexer);
21138 /* If the next token is a semicolon, consume it. */
21139 if (token->type == CPP_SEMICOLON)
21140 cp_lexer_consume_token (parser->lexer);
21141 goto out;
21143 else
21144 if (declarator->kind == cdk_function)
21145 declarator->id_loc = token->location;
21146 /* Create the declaration. */
21147 decl = grokfield (declarator, &decl_specifiers,
21148 initializer, /*init_const_expr_p=*/true,
21149 asm_specification, attributes);
21150 if (parser->fully_implicit_function_template_p)
21152 if (friend_p)
21153 finish_fully_implicit_template (parser, 0);
21154 else
21155 decl = finish_fully_implicit_template (parser, decl);
21159 cp_finalize_omp_declare_simd (parser, decl);
21161 /* Reset PREFIX_ATTRIBUTES. */
21162 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21163 attributes = TREE_CHAIN (attributes);
21164 if (attributes)
21165 TREE_CHAIN (attributes) = NULL_TREE;
21167 /* If there is any qualification still in effect, clear it
21168 now; we will be starting fresh with the next declarator. */
21169 parser->scope = NULL_TREE;
21170 parser->qualifying_scope = NULL_TREE;
21171 parser->object_scope = NULL_TREE;
21172 /* If it's a `,', then there are more declarators. */
21173 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21175 cp_lexer_consume_token (parser->lexer);
21176 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21178 cp_token *token = cp_lexer_previous_token (parser->lexer);
21179 error_at (token->location,
21180 "stray %<,%> at end of member declaration");
21183 /* If the next token isn't a `;', then we have a parse error. */
21184 else if (cp_lexer_next_token_is_not (parser->lexer,
21185 CPP_SEMICOLON))
21187 /* The next token might be a ways away from where the
21188 actual semicolon is missing. Find the previous token
21189 and use that for our error position. */
21190 cp_token *token = cp_lexer_previous_token (parser->lexer);
21191 error_at (token->location,
21192 "expected %<;%> at end of member declaration");
21194 /* Assume that the user meant to provide a semicolon. If
21195 we were to cp_parser_skip_to_end_of_statement, we might
21196 skip to a semicolon inside a member function definition
21197 and issue nonsensical error messages. */
21198 assume_semicolon = true;
21201 if (decl)
21203 /* Add DECL to the list of members. */
21204 if (!friend_p
21205 /* Explicitly include, eg, NSDMIs, for better error
21206 recovery (c++/58650). */
21207 || !DECL_DECLARES_FUNCTION_P (decl))
21208 finish_member_declaration (decl);
21210 if (TREE_CODE (decl) == FUNCTION_DECL)
21211 cp_parser_save_default_args (parser, decl);
21212 else if (TREE_CODE (decl) == FIELD_DECL
21213 && !DECL_C_BIT_FIELD (decl)
21214 && DECL_INITIAL (decl))
21215 /* Add DECL to the queue of NSDMI to be parsed later. */
21216 vec_safe_push (unparsed_nsdmis, decl);
21219 if (assume_semicolon)
21220 goto out;
21224 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21225 out:
21226 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21229 /* Parse a pure-specifier.
21231 pure-specifier:
21234 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21235 Otherwise, ERROR_MARK_NODE is returned. */
21237 static tree
21238 cp_parser_pure_specifier (cp_parser* parser)
21240 cp_token *token;
21242 /* Look for the `=' token. */
21243 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21244 return error_mark_node;
21245 /* Look for the `0' token. */
21246 token = cp_lexer_peek_token (parser->lexer);
21248 if (token->type == CPP_EOF
21249 || token->type == CPP_PRAGMA_EOL)
21250 return error_mark_node;
21252 cp_lexer_consume_token (parser->lexer);
21254 /* Accept = default or = delete in c++0x mode. */
21255 if (token->keyword == RID_DEFAULT
21256 || token->keyword == RID_DELETE)
21258 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21259 return token->u.value;
21262 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21263 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21265 cp_parser_error (parser,
21266 "invalid pure specifier (only %<= 0%> is allowed)");
21267 cp_parser_skip_to_end_of_statement (parser);
21268 return error_mark_node;
21270 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21272 error_at (token->location, "templates may not be %<virtual%>");
21273 return error_mark_node;
21276 return integer_zero_node;
21279 /* Parse a constant-initializer.
21281 constant-initializer:
21282 = constant-expression
21284 Returns a representation of the constant-expression. */
21286 static tree
21287 cp_parser_constant_initializer (cp_parser* parser)
21289 /* Look for the `=' token. */
21290 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21291 return error_mark_node;
21293 /* It is invalid to write:
21295 struct S { static const int i = { 7 }; };
21298 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21300 cp_parser_error (parser,
21301 "a brace-enclosed initializer is not allowed here");
21302 /* Consume the opening brace. */
21303 cp_lexer_consume_token (parser->lexer);
21304 /* Skip the initializer. */
21305 cp_parser_skip_to_closing_brace (parser);
21306 /* Look for the trailing `}'. */
21307 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21309 return error_mark_node;
21312 return cp_parser_constant_expression (parser);
21315 /* Derived classes [gram.class.derived] */
21317 /* Parse a base-clause.
21319 base-clause:
21320 : base-specifier-list
21322 base-specifier-list:
21323 base-specifier ... [opt]
21324 base-specifier-list , base-specifier ... [opt]
21326 Returns a TREE_LIST representing the base-classes, in the order in
21327 which they were declared. The representation of each node is as
21328 described by cp_parser_base_specifier.
21330 In the case that no bases are specified, this function will return
21331 NULL_TREE, not ERROR_MARK_NODE. */
21333 static tree
21334 cp_parser_base_clause (cp_parser* parser)
21336 tree bases = NULL_TREE;
21338 /* Look for the `:' that begins the list. */
21339 cp_parser_require (parser, CPP_COLON, RT_COLON);
21341 /* Scan the base-specifier-list. */
21342 while (true)
21344 cp_token *token;
21345 tree base;
21346 bool pack_expansion_p = false;
21348 /* Look for the base-specifier. */
21349 base = cp_parser_base_specifier (parser);
21350 /* Look for the (optional) ellipsis. */
21351 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21353 /* Consume the `...'. */
21354 cp_lexer_consume_token (parser->lexer);
21356 pack_expansion_p = true;
21359 /* Add BASE to the front of the list. */
21360 if (base && base != error_mark_node)
21362 if (pack_expansion_p)
21363 /* Make this a pack expansion type. */
21364 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21366 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21368 TREE_CHAIN (base) = bases;
21369 bases = base;
21372 /* Peek at the next token. */
21373 token = cp_lexer_peek_token (parser->lexer);
21374 /* If it's not a comma, then the list is complete. */
21375 if (token->type != CPP_COMMA)
21376 break;
21377 /* Consume the `,'. */
21378 cp_lexer_consume_token (parser->lexer);
21381 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21382 base class had a qualified name. However, the next name that
21383 appears is certainly not qualified. */
21384 parser->scope = NULL_TREE;
21385 parser->qualifying_scope = NULL_TREE;
21386 parser->object_scope = NULL_TREE;
21388 return nreverse (bases);
21391 /* Parse a base-specifier.
21393 base-specifier:
21394 :: [opt] nested-name-specifier [opt] class-name
21395 virtual access-specifier [opt] :: [opt] nested-name-specifier
21396 [opt] class-name
21397 access-specifier virtual [opt] :: [opt] nested-name-specifier
21398 [opt] class-name
21400 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21401 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21402 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21403 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21405 static tree
21406 cp_parser_base_specifier (cp_parser* parser)
21408 cp_token *token;
21409 bool done = false;
21410 bool virtual_p = false;
21411 bool duplicate_virtual_error_issued_p = false;
21412 bool duplicate_access_error_issued_p = false;
21413 bool class_scope_p, template_p;
21414 tree access = access_default_node;
21415 tree type;
21417 /* Process the optional `virtual' and `access-specifier'. */
21418 while (!done)
21420 /* Peek at the next token. */
21421 token = cp_lexer_peek_token (parser->lexer);
21422 /* Process `virtual'. */
21423 switch (token->keyword)
21425 case RID_VIRTUAL:
21426 /* If `virtual' appears more than once, issue an error. */
21427 if (virtual_p && !duplicate_virtual_error_issued_p)
21429 cp_parser_error (parser,
21430 "%<virtual%> specified more than once in base-specified");
21431 duplicate_virtual_error_issued_p = true;
21434 virtual_p = true;
21436 /* Consume the `virtual' token. */
21437 cp_lexer_consume_token (parser->lexer);
21439 break;
21441 case RID_PUBLIC:
21442 case RID_PROTECTED:
21443 case RID_PRIVATE:
21444 /* If more than one access specifier appears, issue an
21445 error. */
21446 if (access != access_default_node
21447 && !duplicate_access_error_issued_p)
21449 cp_parser_error (parser,
21450 "more than one access specifier in base-specified");
21451 duplicate_access_error_issued_p = true;
21454 access = ridpointers[(int) token->keyword];
21456 /* Consume the access-specifier. */
21457 cp_lexer_consume_token (parser->lexer);
21459 break;
21461 default:
21462 done = true;
21463 break;
21466 /* It is not uncommon to see programs mechanically, erroneously, use
21467 the 'typename' keyword to denote (dependent) qualified types
21468 as base classes. */
21469 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21471 token = cp_lexer_peek_token (parser->lexer);
21472 if (!processing_template_decl)
21473 error_at (token->location,
21474 "keyword %<typename%> not allowed outside of templates");
21475 else
21476 error_at (token->location,
21477 "keyword %<typename%> not allowed in this context "
21478 "(the base class is implicitly a type)");
21479 cp_lexer_consume_token (parser->lexer);
21482 /* Look for the optional `::' operator. */
21483 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21484 /* Look for the nested-name-specifier. The simplest way to
21485 implement:
21487 [temp.res]
21489 The keyword `typename' is not permitted in a base-specifier or
21490 mem-initializer; in these contexts a qualified name that
21491 depends on a template-parameter is implicitly assumed to be a
21492 type name.
21494 is to pretend that we have seen the `typename' keyword at this
21495 point. */
21496 cp_parser_nested_name_specifier_opt (parser,
21497 /*typename_keyword_p=*/true,
21498 /*check_dependency_p=*/true,
21499 typename_type,
21500 /*is_declaration=*/true);
21501 /* If the base class is given by a qualified name, assume that names
21502 we see are type names or templates, as appropriate. */
21503 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21504 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21506 if (!parser->scope
21507 && cp_lexer_next_token_is_decltype (parser->lexer))
21508 /* DR 950 allows decltype as a base-specifier. */
21509 type = cp_parser_decltype (parser);
21510 else
21512 /* Otherwise, look for the class-name. */
21513 type = cp_parser_class_name (parser,
21514 class_scope_p,
21515 template_p,
21516 typename_type,
21517 /*check_dependency_p=*/true,
21518 /*class_head_p=*/false,
21519 /*is_declaration=*/true);
21520 type = TREE_TYPE (type);
21523 if (type == error_mark_node)
21524 return error_mark_node;
21526 return finish_base_specifier (type, access, virtual_p);
21529 /* Exception handling [gram.exception] */
21531 /* Parse an (optional) noexcept-specification.
21533 noexcept-specification:
21534 noexcept ( constant-expression ) [opt]
21536 If no noexcept-specification is present, returns NULL_TREE.
21537 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21538 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21539 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21540 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21541 in which case a boolean condition is returned instead. */
21543 static tree
21544 cp_parser_noexcept_specification_opt (cp_parser* parser,
21545 bool require_constexpr,
21546 bool* consumed_expr,
21547 bool return_cond)
21549 cp_token *token;
21550 const char *saved_message;
21552 /* Peek at the next token. */
21553 token = cp_lexer_peek_token (parser->lexer);
21555 /* Is it a noexcept-specification? */
21556 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21558 tree expr;
21559 cp_lexer_consume_token (parser->lexer);
21561 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21563 cp_lexer_consume_token (parser->lexer);
21565 if (require_constexpr)
21567 /* Types may not be defined in an exception-specification. */
21568 saved_message = parser->type_definition_forbidden_message;
21569 parser->type_definition_forbidden_message
21570 = G_("types may not be defined in an exception-specification");
21572 expr = cp_parser_constant_expression (parser);
21574 /* Restore the saved message. */
21575 parser->type_definition_forbidden_message = saved_message;
21577 else
21579 expr = cp_parser_expression (parser);
21580 *consumed_expr = true;
21583 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21585 else
21587 expr = boolean_true_node;
21588 if (!require_constexpr)
21589 *consumed_expr = false;
21592 /* We cannot build a noexcept-spec right away because this will check
21593 that expr is a constexpr. */
21594 if (!return_cond)
21595 return build_noexcept_spec (expr, tf_warning_or_error);
21596 else
21597 return expr;
21599 else
21600 return NULL_TREE;
21603 /* Parse an (optional) exception-specification.
21605 exception-specification:
21606 throw ( type-id-list [opt] )
21608 Returns a TREE_LIST representing the exception-specification. The
21609 TREE_VALUE of each node is a type. */
21611 static tree
21612 cp_parser_exception_specification_opt (cp_parser* parser)
21614 cp_token *token;
21615 tree type_id_list;
21616 const char *saved_message;
21618 /* Peek at the next token. */
21619 token = cp_lexer_peek_token (parser->lexer);
21621 /* Is it a noexcept-specification? */
21622 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21623 false);
21624 if (type_id_list != NULL_TREE)
21625 return type_id_list;
21627 /* If it's not `throw', then there's no exception-specification. */
21628 if (!cp_parser_is_keyword (token, RID_THROW))
21629 return NULL_TREE;
21631 #if 0
21632 /* Enable this once a lot of code has transitioned to noexcept? */
21633 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21634 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21635 "deprecated in C++0x; use %<noexcept%> instead");
21636 #endif
21638 /* Consume the `throw'. */
21639 cp_lexer_consume_token (parser->lexer);
21641 /* Look for the `('. */
21642 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21644 /* Peek at the next token. */
21645 token = cp_lexer_peek_token (parser->lexer);
21646 /* If it's not a `)', then there is a type-id-list. */
21647 if (token->type != CPP_CLOSE_PAREN)
21649 /* Types may not be defined in an exception-specification. */
21650 saved_message = parser->type_definition_forbidden_message;
21651 parser->type_definition_forbidden_message
21652 = G_("types may not be defined in an exception-specification");
21653 /* Parse the type-id-list. */
21654 type_id_list = cp_parser_type_id_list (parser);
21655 /* Restore the saved message. */
21656 parser->type_definition_forbidden_message = saved_message;
21658 else
21659 type_id_list = empty_except_spec;
21661 /* Look for the `)'. */
21662 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21664 return type_id_list;
21667 /* Parse an (optional) type-id-list.
21669 type-id-list:
21670 type-id ... [opt]
21671 type-id-list , type-id ... [opt]
21673 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21674 in the order that the types were presented. */
21676 static tree
21677 cp_parser_type_id_list (cp_parser* parser)
21679 tree types = NULL_TREE;
21681 while (true)
21683 cp_token *token;
21684 tree type;
21686 /* Get the next type-id. */
21687 type = cp_parser_type_id (parser);
21688 /* Parse the optional ellipsis. */
21689 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21691 /* Consume the `...'. */
21692 cp_lexer_consume_token (parser->lexer);
21694 /* Turn the type into a pack expansion expression. */
21695 type = make_pack_expansion (type);
21697 /* Add it to the list. */
21698 types = add_exception_specifier (types, type, /*complain=*/1);
21699 /* Peek at the next token. */
21700 token = cp_lexer_peek_token (parser->lexer);
21701 /* If it is not a `,', we are done. */
21702 if (token->type != CPP_COMMA)
21703 break;
21704 /* Consume the `,'. */
21705 cp_lexer_consume_token (parser->lexer);
21708 return nreverse (types);
21711 /* Parse a try-block.
21713 try-block:
21714 try compound-statement handler-seq */
21716 static tree
21717 cp_parser_try_block (cp_parser* parser)
21719 tree try_block;
21721 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21722 if (parser->in_function_body
21723 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21724 error ("%<try%> in %<constexpr%> function");
21726 try_block = begin_try_block ();
21727 cp_parser_compound_statement (parser, NULL, true, false);
21728 finish_try_block (try_block);
21729 cp_parser_handler_seq (parser);
21730 finish_handler_sequence (try_block);
21732 return try_block;
21735 /* Parse a function-try-block.
21737 function-try-block:
21738 try ctor-initializer [opt] function-body handler-seq */
21740 static bool
21741 cp_parser_function_try_block (cp_parser* parser)
21743 tree compound_stmt;
21744 tree try_block;
21745 bool ctor_initializer_p;
21747 /* Look for the `try' keyword. */
21748 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21749 return false;
21750 /* Let the rest of the front end know where we are. */
21751 try_block = begin_function_try_block (&compound_stmt);
21752 /* Parse the function-body. */
21753 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21754 (parser, /*in_function_try_block=*/true);
21755 /* We're done with the `try' part. */
21756 finish_function_try_block (try_block);
21757 /* Parse the handlers. */
21758 cp_parser_handler_seq (parser);
21759 /* We're done with the handlers. */
21760 finish_function_handler_sequence (try_block, compound_stmt);
21762 return ctor_initializer_p;
21765 /* Parse a handler-seq.
21767 handler-seq:
21768 handler handler-seq [opt] */
21770 static void
21771 cp_parser_handler_seq (cp_parser* parser)
21773 while (true)
21775 cp_token *token;
21777 /* Parse the handler. */
21778 cp_parser_handler (parser);
21779 /* Peek at the next token. */
21780 token = cp_lexer_peek_token (parser->lexer);
21781 /* If it's not `catch' then there are no more handlers. */
21782 if (!cp_parser_is_keyword (token, RID_CATCH))
21783 break;
21787 /* Parse a handler.
21789 handler:
21790 catch ( exception-declaration ) compound-statement */
21792 static void
21793 cp_parser_handler (cp_parser* parser)
21795 tree handler;
21796 tree declaration;
21798 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21799 handler = begin_handler ();
21800 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21801 declaration = cp_parser_exception_declaration (parser);
21802 finish_handler_parms (declaration, handler);
21803 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21804 cp_parser_compound_statement (parser, NULL, false, false);
21805 finish_handler (handler);
21808 /* Parse an exception-declaration.
21810 exception-declaration:
21811 type-specifier-seq declarator
21812 type-specifier-seq abstract-declarator
21813 type-specifier-seq
21816 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21817 ellipsis variant is used. */
21819 static tree
21820 cp_parser_exception_declaration (cp_parser* parser)
21822 cp_decl_specifier_seq type_specifiers;
21823 cp_declarator *declarator;
21824 const char *saved_message;
21826 /* If it's an ellipsis, it's easy to handle. */
21827 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21829 /* Consume the `...' token. */
21830 cp_lexer_consume_token (parser->lexer);
21831 return NULL_TREE;
21834 /* Types may not be defined in exception-declarations. */
21835 saved_message = parser->type_definition_forbidden_message;
21836 parser->type_definition_forbidden_message
21837 = G_("types may not be defined in exception-declarations");
21839 /* Parse the type-specifier-seq. */
21840 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21841 /*is_trailing_return=*/false,
21842 &type_specifiers);
21843 /* If it's a `)', then there is no declarator. */
21844 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21845 declarator = NULL;
21846 else
21847 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21848 /*ctor_dtor_or_conv_p=*/NULL,
21849 /*parenthesized_p=*/NULL,
21850 /*member_p=*/false,
21851 /*friend_p=*/false);
21853 /* Restore the saved message. */
21854 parser->type_definition_forbidden_message = saved_message;
21856 if (!type_specifiers.any_specifiers_p)
21857 return error_mark_node;
21859 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21862 /* Parse a throw-expression.
21864 throw-expression:
21865 throw assignment-expression [opt]
21867 Returns a THROW_EXPR representing the throw-expression. */
21869 static tree
21870 cp_parser_throw_expression (cp_parser* parser)
21872 tree expression;
21873 cp_token* token;
21875 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21876 token = cp_lexer_peek_token (parser->lexer);
21877 /* Figure out whether or not there is an assignment-expression
21878 following the "throw" keyword. */
21879 if (token->type == CPP_COMMA
21880 || token->type == CPP_SEMICOLON
21881 || token->type == CPP_CLOSE_PAREN
21882 || token->type == CPP_CLOSE_SQUARE
21883 || token->type == CPP_CLOSE_BRACE
21884 || token->type == CPP_COLON)
21885 expression = NULL_TREE;
21886 else
21887 expression = cp_parser_assignment_expression (parser);
21889 return build_throw (expression);
21892 /* GNU Extensions */
21894 /* Parse an (optional) asm-specification.
21896 asm-specification:
21897 asm ( string-literal )
21899 If the asm-specification is present, returns a STRING_CST
21900 corresponding to the string-literal. Otherwise, returns
21901 NULL_TREE. */
21903 static tree
21904 cp_parser_asm_specification_opt (cp_parser* parser)
21906 cp_token *token;
21907 tree asm_specification;
21909 /* Peek at the next token. */
21910 token = cp_lexer_peek_token (parser->lexer);
21911 /* If the next token isn't the `asm' keyword, then there's no
21912 asm-specification. */
21913 if (!cp_parser_is_keyword (token, RID_ASM))
21914 return NULL_TREE;
21916 /* Consume the `asm' token. */
21917 cp_lexer_consume_token (parser->lexer);
21918 /* Look for the `('. */
21919 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21921 /* Look for the string-literal. */
21922 asm_specification = cp_parser_string_literal (parser, false, false);
21924 /* Look for the `)'. */
21925 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21927 return asm_specification;
21930 /* Parse an asm-operand-list.
21932 asm-operand-list:
21933 asm-operand
21934 asm-operand-list , asm-operand
21936 asm-operand:
21937 string-literal ( expression )
21938 [ string-literal ] string-literal ( expression )
21940 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21941 each node is the expression. The TREE_PURPOSE is itself a
21942 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21943 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21944 is a STRING_CST for the string literal before the parenthesis. Returns
21945 ERROR_MARK_NODE if any of the operands are invalid. */
21947 static tree
21948 cp_parser_asm_operand_list (cp_parser* parser)
21950 tree asm_operands = NULL_TREE;
21951 bool invalid_operands = false;
21953 while (true)
21955 tree string_literal;
21956 tree expression;
21957 tree name;
21959 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21961 /* Consume the `[' token. */
21962 cp_lexer_consume_token (parser->lexer);
21963 /* Read the operand name. */
21964 name = cp_parser_identifier (parser);
21965 if (name != error_mark_node)
21966 name = build_string (IDENTIFIER_LENGTH (name),
21967 IDENTIFIER_POINTER (name));
21968 /* Look for the closing `]'. */
21969 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21971 else
21972 name = NULL_TREE;
21973 /* Look for the string-literal. */
21974 string_literal = cp_parser_string_literal (parser, false, false);
21976 /* Look for the `('. */
21977 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21978 /* Parse the expression. */
21979 expression = cp_parser_expression (parser);
21980 /* Look for the `)'. */
21981 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21983 if (name == error_mark_node
21984 || string_literal == error_mark_node
21985 || expression == error_mark_node)
21986 invalid_operands = true;
21988 /* Add this operand to the list. */
21989 asm_operands = tree_cons (build_tree_list (name, string_literal),
21990 expression,
21991 asm_operands);
21992 /* If the next token is not a `,', there are no more
21993 operands. */
21994 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21995 break;
21996 /* Consume the `,'. */
21997 cp_lexer_consume_token (parser->lexer);
22000 return invalid_operands ? error_mark_node : nreverse (asm_operands);
22003 /* Parse an asm-clobber-list.
22005 asm-clobber-list:
22006 string-literal
22007 asm-clobber-list , string-literal
22009 Returns a TREE_LIST, indicating the clobbers in the order that they
22010 appeared. The TREE_VALUE of each node is a STRING_CST. */
22012 static tree
22013 cp_parser_asm_clobber_list (cp_parser* parser)
22015 tree clobbers = NULL_TREE;
22017 while (true)
22019 tree string_literal;
22021 /* Look for the string literal. */
22022 string_literal = cp_parser_string_literal (parser, false, false);
22023 /* Add it to the list. */
22024 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
22025 /* If the next token is not a `,', then the list is
22026 complete. */
22027 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22028 break;
22029 /* Consume the `,' token. */
22030 cp_lexer_consume_token (parser->lexer);
22033 return clobbers;
22036 /* Parse an asm-label-list.
22038 asm-label-list:
22039 identifier
22040 asm-label-list , identifier
22042 Returns a TREE_LIST, indicating the labels in the order that they
22043 appeared. The TREE_VALUE of each node is a label. */
22045 static tree
22046 cp_parser_asm_label_list (cp_parser* parser)
22048 tree labels = NULL_TREE;
22050 while (true)
22052 tree identifier, label, name;
22054 /* Look for the identifier. */
22055 identifier = cp_parser_identifier (parser);
22056 if (!error_operand_p (identifier))
22058 label = lookup_label (identifier);
22059 if (TREE_CODE (label) == LABEL_DECL)
22061 TREE_USED (label) = 1;
22062 check_goto (label);
22063 name = build_string (IDENTIFIER_LENGTH (identifier),
22064 IDENTIFIER_POINTER (identifier));
22065 labels = tree_cons (name, label, labels);
22068 /* If the next token is not a `,', then the list is
22069 complete. */
22070 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22071 break;
22072 /* Consume the `,' token. */
22073 cp_lexer_consume_token (parser->lexer);
22076 return nreverse (labels);
22079 /* Return TRUE iff the next tokens in the stream are possibly the
22080 beginning of a GNU extension attribute. */
22082 static bool
22083 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22085 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22088 /* Return TRUE iff the next tokens in the stream are possibly the
22089 beginning of a standard C++-11 attribute specifier. */
22091 static bool
22092 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22094 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22097 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22098 beginning of a standard C++-11 attribute specifier. */
22100 static bool
22101 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22103 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22105 return (cxx_dialect >= cxx11
22106 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22107 || (token->type == CPP_OPEN_SQUARE
22108 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22109 && token->type == CPP_OPEN_SQUARE)));
22112 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22113 beginning of a GNU extension attribute. */
22115 static bool
22116 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22118 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22120 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22123 /* Return true iff the next tokens can be the beginning of either a
22124 GNU attribute list, or a standard C++11 attribute sequence. */
22126 static bool
22127 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22129 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22130 || cp_next_tokens_can_be_std_attribute_p (parser));
22133 /* Return true iff the next Nth tokens can be the beginning of either
22134 a GNU attribute list, or a standard C++11 attribute sequence. */
22136 static bool
22137 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22139 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22140 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22143 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22144 of GNU attributes, or return NULL. */
22146 static tree
22147 cp_parser_attributes_opt (cp_parser *parser)
22149 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22150 return cp_parser_gnu_attributes_opt (parser);
22151 return cp_parser_std_attribute_spec_seq (parser);
22154 #define CILK_SIMD_FN_CLAUSE_MASK \
22155 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22156 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22157 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22158 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22159 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22161 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22162 vector [(<clauses>)] */
22164 static void
22165 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22167 bool first_p = parser->cilk_simd_fn_info == NULL;
22168 cp_token *token = v_token;
22169 if (first_p)
22171 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22172 parser->cilk_simd_fn_info->error_seen = false;
22173 parser->cilk_simd_fn_info->fndecl_seen = false;
22174 parser->cilk_simd_fn_info->tokens = vNULL;
22176 int paren_scope = 0;
22177 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22179 cp_lexer_consume_token (parser->lexer);
22180 v_token = cp_lexer_peek_token (parser->lexer);
22181 paren_scope++;
22183 while (paren_scope > 0)
22185 token = cp_lexer_peek_token (parser->lexer);
22186 if (token->type == CPP_OPEN_PAREN)
22187 paren_scope++;
22188 else if (token->type == CPP_CLOSE_PAREN)
22189 paren_scope--;
22190 /* Do not push the last ')' */
22191 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22192 cp_lexer_consume_token (parser->lexer);
22195 token->type = CPP_PRAGMA_EOL;
22196 parser->lexer->next_token = token;
22197 cp_lexer_consume_token (parser->lexer);
22199 struct cp_token_cache *cp
22200 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22201 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22204 /* Parse an (optional) series of attributes.
22206 attributes:
22207 attributes attribute
22209 attribute:
22210 __attribute__ (( attribute-list [opt] ))
22212 The return value is as for cp_parser_gnu_attribute_list. */
22214 static tree
22215 cp_parser_gnu_attributes_opt (cp_parser* parser)
22217 tree attributes = NULL_TREE;
22219 while (true)
22221 cp_token *token;
22222 tree attribute_list;
22223 bool ok = true;
22225 /* Peek at the next token. */
22226 token = cp_lexer_peek_token (parser->lexer);
22227 /* If it's not `__attribute__', then we're done. */
22228 if (token->keyword != RID_ATTRIBUTE)
22229 break;
22231 /* Consume the `__attribute__' keyword. */
22232 cp_lexer_consume_token (parser->lexer);
22233 /* Look for the two `(' tokens. */
22234 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22235 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22237 /* Peek at the next token. */
22238 token = cp_lexer_peek_token (parser->lexer);
22239 if (token->type != CPP_CLOSE_PAREN)
22240 /* Parse the attribute-list. */
22241 attribute_list = cp_parser_gnu_attribute_list (parser);
22242 else
22243 /* If the next token is a `)', then there is no attribute
22244 list. */
22245 attribute_list = NULL;
22247 /* Look for the two `)' tokens. */
22248 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22249 ok = false;
22250 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22251 ok = false;
22252 if (!ok)
22253 cp_parser_skip_to_end_of_statement (parser);
22255 /* Add these new attributes to the list. */
22256 attributes = chainon (attributes, attribute_list);
22259 return attributes;
22262 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22263 "__vector" or "__vector__." */
22265 static inline bool
22266 is_cilkplus_vector_p (tree name)
22268 if (flag_cilkplus && is_attribute_p ("vector", name))
22269 return true;
22270 return false;
22273 /* Parse a GNU attribute-list.
22275 attribute-list:
22276 attribute
22277 attribute-list , attribute
22279 attribute:
22280 identifier
22281 identifier ( identifier )
22282 identifier ( identifier , expression-list )
22283 identifier ( expression-list )
22285 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22286 to an attribute. The TREE_PURPOSE of each node is the identifier
22287 indicating which attribute is in use. The TREE_VALUE represents
22288 the arguments, if any. */
22290 static tree
22291 cp_parser_gnu_attribute_list (cp_parser* parser)
22293 tree attribute_list = NULL_TREE;
22294 bool save_translate_strings_p = parser->translate_strings_p;
22296 parser->translate_strings_p = false;
22297 while (true)
22299 cp_token *token;
22300 tree identifier;
22301 tree attribute;
22303 /* Look for the identifier. We also allow keywords here; for
22304 example `__attribute__ ((const))' is legal. */
22305 token = cp_lexer_peek_token (parser->lexer);
22306 if (token->type == CPP_NAME
22307 || token->type == CPP_KEYWORD)
22309 tree arguments = NULL_TREE;
22311 /* Consume the token, but save it since we need it for the
22312 SIMD enabled function parsing. */
22313 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22315 /* Save away the identifier that indicates which attribute
22316 this is. */
22317 identifier = (token->type == CPP_KEYWORD)
22318 /* For keywords, use the canonical spelling, not the
22319 parsed identifier. */
22320 ? ridpointers[(int) token->keyword]
22321 : id_token->u.value;
22323 attribute = build_tree_list (identifier, NULL_TREE);
22325 /* Peek at the next token. */
22326 token = cp_lexer_peek_token (parser->lexer);
22327 /* If it's an `(', then parse the attribute arguments. */
22328 if (token->type == CPP_OPEN_PAREN)
22330 vec<tree, va_gc> *vec;
22331 int attr_flag = (attribute_takes_identifier_p (identifier)
22332 ? id_attr : normal_attr);
22333 if (is_cilkplus_vector_p (identifier))
22335 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22336 continue;
22338 else
22339 vec = cp_parser_parenthesized_expression_list
22340 (parser, attr_flag, /*cast_p=*/false,
22341 /*allow_expansion_p=*/false,
22342 /*non_constant_p=*/NULL);
22343 if (vec == NULL)
22344 arguments = error_mark_node;
22345 else
22347 arguments = build_tree_list_vec (vec);
22348 release_tree_vector (vec);
22350 /* Save the arguments away. */
22351 TREE_VALUE (attribute) = arguments;
22353 else if (is_cilkplus_vector_p (identifier))
22355 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22356 continue;
22359 if (arguments != error_mark_node)
22361 /* Add this attribute to the list. */
22362 TREE_CHAIN (attribute) = attribute_list;
22363 attribute_list = attribute;
22366 token = cp_lexer_peek_token (parser->lexer);
22368 /* Now, look for more attributes. If the next token isn't a
22369 `,', we're done. */
22370 if (token->type != CPP_COMMA)
22371 break;
22373 /* Consume the comma and keep going. */
22374 cp_lexer_consume_token (parser->lexer);
22376 parser->translate_strings_p = save_translate_strings_p;
22378 /* We built up the list in reverse order. */
22379 return nreverse (attribute_list);
22382 /* Parse a standard C++11 attribute.
22384 The returned representation is a TREE_LIST which TREE_PURPOSE is
22385 the scoped name of the attribute, and the TREE_VALUE is its
22386 arguments list.
22388 Note that the scoped name of the attribute is itself a TREE_LIST
22389 which TREE_PURPOSE is the namespace of the attribute, and
22390 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22391 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22392 and which TREE_PURPOSE is directly the attribute name.
22394 Clients of the attribute code should use get_attribute_namespace
22395 and get_attribute_name to get the actual namespace and name of
22396 attributes, regardless of their being GNU or C++11 attributes.
22398 attribute:
22399 attribute-token attribute-argument-clause [opt]
22401 attribute-token:
22402 identifier
22403 attribute-scoped-token
22405 attribute-scoped-token:
22406 attribute-namespace :: identifier
22408 attribute-namespace:
22409 identifier
22411 attribute-argument-clause:
22412 ( balanced-token-seq )
22414 balanced-token-seq:
22415 balanced-token [opt]
22416 balanced-token-seq balanced-token
22418 balanced-token:
22419 ( balanced-token-seq )
22420 [ balanced-token-seq ]
22421 { balanced-token-seq }. */
22423 static tree
22424 cp_parser_std_attribute (cp_parser *parser)
22426 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22427 cp_token *token;
22429 /* First, parse name of the attribute, a.k.a attribute-token. */
22431 token = cp_lexer_peek_token (parser->lexer);
22432 if (token->type == CPP_NAME)
22433 attr_id = token->u.value;
22434 else if (token->type == CPP_KEYWORD)
22435 attr_id = ridpointers[(int) token->keyword];
22436 else if (token->flags & NAMED_OP)
22437 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22439 if (attr_id == NULL_TREE)
22440 return NULL_TREE;
22442 cp_lexer_consume_token (parser->lexer);
22444 token = cp_lexer_peek_token (parser->lexer);
22445 if (token->type == CPP_SCOPE)
22447 /* We are seeing a scoped attribute token. */
22449 cp_lexer_consume_token (parser->lexer);
22450 attr_ns = attr_id;
22452 token = cp_lexer_consume_token (parser->lexer);
22453 if (token->type == CPP_NAME)
22454 attr_id = token->u.value;
22455 else if (token->type == CPP_KEYWORD)
22456 attr_id = ridpointers[(int) token->keyword];
22457 else
22459 error_at (token->location,
22460 "expected an identifier for the attribute name");
22461 return error_mark_node;
22463 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22464 NULL_TREE);
22465 token = cp_lexer_peek_token (parser->lexer);
22467 else
22469 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22470 NULL_TREE);
22471 /* C++11 noreturn attribute is equivalent to GNU's. */
22472 if (is_attribute_p ("noreturn", attr_id))
22473 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22474 /* C++14 deprecated attribute is equivalent to GNU's. */
22475 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22477 if (cxx_dialect == cxx11)
22478 pedwarn (token->location, OPT_Wpedantic,
22479 "%<deprecated%> is a C++14 feature;"
22480 " use %<gnu::deprecated%>");
22481 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22485 /* Now parse the optional argument clause of the attribute. */
22487 if (token->type != CPP_OPEN_PAREN)
22488 return attribute;
22491 vec<tree, va_gc> *vec;
22492 int attr_flag = normal_attr;
22494 if (attr_ns == get_identifier ("gnu")
22495 && attribute_takes_identifier_p (attr_id))
22496 /* A GNU attribute that takes an identifier in parameter. */
22497 attr_flag = id_attr;
22499 vec = cp_parser_parenthesized_expression_list
22500 (parser, attr_flag, /*cast_p=*/false,
22501 /*allow_expansion_p=*/true,
22502 /*non_constant_p=*/NULL);
22503 if (vec == NULL)
22504 arguments = error_mark_node;
22505 else
22507 arguments = build_tree_list_vec (vec);
22508 release_tree_vector (vec);
22511 if (arguments == error_mark_node)
22512 attribute = error_mark_node;
22513 else
22514 TREE_VALUE (attribute) = arguments;
22517 return attribute;
22520 /* Check that the attribute ATTRIBUTE appears at most once in the
22521 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
22522 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
22523 isn't implemented yet in GCC. */
22525 static void
22526 cp_parser_check_std_attribute (tree attributes, tree attribute)
22528 if (attributes)
22530 tree name = get_attribute_name (attribute);
22531 if (is_attribute_p ("noreturn", name)
22532 && lookup_attribute ("noreturn", attributes))
22533 error ("attribute noreturn can appear at most once "
22534 "in an attribute-list");
22535 else if (is_attribute_p ("deprecated", name)
22536 && lookup_attribute ("deprecated", attributes))
22537 error ("attribute deprecated can appear at most once "
22538 "in an attribute-list");
22542 /* Parse a list of standard C++-11 attributes.
22544 attribute-list:
22545 attribute [opt]
22546 attribute-list , attribute[opt]
22547 attribute ...
22548 attribute-list , attribute ...
22551 static tree
22552 cp_parser_std_attribute_list (cp_parser *parser)
22554 tree attributes = NULL_TREE, attribute = NULL_TREE;
22555 cp_token *token = NULL;
22557 while (true)
22559 attribute = cp_parser_std_attribute (parser);
22560 if (attribute == error_mark_node)
22561 break;
22562 if (attribute != NULL_TREE)
22564 cp_parser_check_std_attribute (attributes, attribute);
22565 TREE_CHAIN (attribute) = attributes;
22566 attributes = attribute;
22568 token = cp_lexer_peek_token (parser->lexer);
22569 if (token->type == CPP_ELLIPSIS)
22571 cp_lexer_consume_token (parser->lexer);
22572 TREE_VALUE (attribute)
22573 = make_pack_expansion (TREE_VALUE (attribute));
22574 token = cp_lexer_peek_token (parser->lexer);
22576 if (token->type != CPP_COMMA)
22577 break;
22578 cp_lexer_consume_token (parser->lexer);
22580 attributes = nreverse (attributes);
22581 return attributes;
22584 /* Parse a standard C++-11 attribute specifier.
22586 attribute-specifier:
22587 [ [ attribute-list ] ]
22588 alignment-specifier
22590 alignment-specifier:
22591 alignas ( type-id ... [opt] )
22592 alignas ( alignment-expression ... [opt] ). */
22594 static tree
22595 cp_parser_std_attribute_spec (cp_parser *parser)
22597 tree attributes = NULL_TREE;
22598 cp_token *token = cp_lexer_peek_token (parser->lexer);
22600 if (token->type == CPP_OPEN_SQUARE
22601 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22603 cp_lexer_consume_token (parser->lexer);
22604 cp_lexer_consume_token (parser->lexer);
22606 attributes = cp_parser_std_attribute_list (parser);
22608 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22609 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22610 cp_parser_skip_to_end_of_statement (parser);
22611 else
22612 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22613 when we are sure that we have actually parsed them. */
22614 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22616 else
22618 tree alignas_expr;
22620 /* Look for an alignment-specifier. */
22622 token = cp_lexer_peek_token (parser->lexer);
22624 if (token->type != CPP_KEYWORD
22625 || token->keyword != RID_ALIGNAS)
22626 return NULL_TREE;
22628 cp_lexer_consume_token (parser->lexer);
22629 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22631 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22633 cp_parser_error (parser, "expected %<(%>");
22634 return error_mark_node;
22637 cp_parser_parse_tentatively (parser);
22638 alignas_expr = cp_parser_type_id (parser);
22640 if (!cp_parser_parse_definitely (parser))
22642 gcc_assert (alignas_expr == error_mark_node
22643 || alignas_expr == NULL_TREE);
22645 alignas_expr =
22646 cp_parser_assignment_expression (parser);
22647 if (alignas_expr == error_mark_node)
22648 cp_parser_skip_to_end_of_statement (parser);
22649 if (alignas_expr == NULL_TREE
22650 || alignas_expr == error_mark_node)
22651 return alignas_expr;
22654 alignas_expr = cxx_alignas_expr (alignas_expr);
22655 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
22657 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22659 cp_lexer_consume_token (parser->lexer);
22660 alignas_expr = make_pack_expansion (alignas_expr);
22663 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22665 cp_parser_error (parser, "expected %<)%>");
22666 return error_mark_node;
22669 /* Build the C++-11 representation of an 'aligned'
22670 attribute. */
22671 attributes =
22672 build_tree_list (build_tree_list (get_identifier ("gnu"),
22673 get_identifier ("aligned")),
22674 alignas_expr);
22677 return attributes;
22680 /* Parse a standard C++-11 attribute-specifier-seq.
22682 attribute-specifier-seq:
22683 attribute-specifier-seq [opt] attribute-specifier
22686 static tree
22687 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22689 tree attr_specs = NULL;
22691 while (true)
22693 tree attr_spec = cp_parser_std_attribute_spec (parser);
22694 if (attr_spec == NULL_TREE)
22695 break;
22696 if (attr_spec == error_mark_node)
22697 return error_mark_node;
22699 TREE_CHAIN (attr_spec) = attr_specs;
22700 attr_specs = attr_spec;
22703 attr_specs = nreverse (attr_specs);
22704 return attr_specs;
22707 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22708 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22709 current value of the PEDANTIC flag, regardless of whether or not
22710 the `__extension__' keyword is present. The caller is responsible
22711 for restoring the value of the PEDANTIC flag. */
22713 static bool
22714 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22716 /* Save the old value of the PEDANTIC flag. */
22717 *saved_pedantic = pedantic;
22719 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22721 /* Consume the `__extension__' token. */
22722 cp_lexer_consume_token (parser->lexer);
22723 /* We're not being pedantic while the `__extension__' keyword is
22724 in effect. */
22725 pedantic = 0;
22727 return true;
22730 return false;
22733 /* Parse a label declaration.
22735 label-declaration:
22736 __label__ label-declarator-seq ;
22738 label-declarator-seq:
22739 identifier , label-declarator-seq
22740 identifier */
22742 static void
22743 cp_parser_label_declaration (cp_parser* parser)
22745 /* Look for the `__label__' keyword. */
22746 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22748 while (true)
22750 tree identifier;
22752 /* Look for an identifier. */
22753 identifier = cp_parser_identifier (parser);
22754 /* If we failed, stop. */
22755 if (identifier == error_mark_node)
22756 break;
22757 /* Declare it as a label. */
22758 finish_label_decl (identifier);
22759 /* If the next token is a `;', stop. */
22760 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22761 break;
22762 /* Look for the `,' separating the label declarations. */
22763 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22766 /* Look for the final `;'. */
22767 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22770 /* Support Functions */
22772 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22773 NAME should have one of the representations used for an
22774 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22775 is returned. If PARSER->SCOPE is a dependent type, then a
22776 SCOPE_REF is returned.
22778 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22779 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22780 was formed. Abstractly, such entities should not be passed to this
22781 function, because they do not need to be looked up, but it is
22782 simpler to check for this special case here, rather than at the
22783 call-sites.
22785 In cases not explicitly covered above, this function returns a
22786 DECL, OVERLOAD, or baselink representing the result of the lookup.
22787 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22788 is returned.
22790 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22791 (e.g., "struct") that was used. In that case bindings that do not
22792 refer to types are ignored.
22794 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22795 ignored.
22797 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22798 are ignored.
22800 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22801 types.
22803 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22804 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22805 NULL_TREE otherwise. */
22807 static tree
22808 cp_parser_lookup_name (cp_parser *parser, tree name,
22809 enum tag_types tag_type,
22810 bool is_template,
22811 bool is_namespace,
22812 bool check_dependency,
22813 tree *ambiguous_decls,
22814 location_t name_location)
22816 tree decl;
22817 tree object_type = parser->context->object_type;
22819 /* Assume that the lookup will be unambiguous. */
22820 if (ambiguous_decls)
22821 *ambiguous_decls = NULL_TREE;
22823 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22824 no longer valid. Note that if we are parsing tentatively, and
22825 the parse fails, OBJECT_TYPE will be automatically restored. */
22826 parser->context->object_type = NULL_TREE;
22828 if (name == error_mark_node)
22829 return error_mark_node;
22831 /* A template-id has already been resolved; there is no lookup to
22832 do. */
22833 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22834 return name;
22835 if (BASELINK_P (name))
22837 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22838 == TEMPLATE_ID_EXPR);
22839 return name;
22842 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22843 it should already have been checked to make sure that the name
22844 used matches the type being destroyed. */
22845 if (TREE_CODE (name) == BIT_NOT_EXPR)
22847 tree type;
22849 /* Figure out to which type this destructor applies. */
22850 if (parser->scope)
22851 type = parser->scope;
22852 else if (object_type)
22853 type = object_type;
22854 else
22855 type = current_class_type;
22856 /* If that's not a class type, there is no destructor. */
22857 if (!type || !CLASS_TYPE_P (type))
22858 return error_mark_node;
22859 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22860 lazily_declare_fn (sfk_destructor, type);
22861 if (!CLASSTYPE_DESTRUCTORS (type))
22862 return error_mark_node;
22863 /* If it was a class type, return the destructor. */
22864 return CLASSTYPE_DESTRUCTORS (type);
22867 /* By this point, the NAME should be an ordinary identifier. If
22868 the id-expression was a qualified name, the qualifying scope is
22869 stored in PARSER->SCOPE at this point. */
22870 gcc_assert (identifier_p (name));
22872 /* Perform the lookup. */
22873 if (parser->scope)
22875 bool dependent_p;
22877 if (parser->scope == error_mark_node)
22878 return error_mark_node;
22880 /* If the SCOPE is dependent, the lookup must be deferred until
22881 the template is instantiated -- unless we are explicitly
22882 looking up names in uninstantiated templates. Even then, we
22883 cannot look up the name if the scope is not a class type; it
22884 might, for example, be a template type parameter. */
22885 dependent_p = (TYPE_P (parser->scope)
22886 && dependent_scope_p (parser->scope));
22887 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22888 && dependent_p)
22889 /* Defer lookup. */
22890 decl = error_mark_node;
22891 else
22893 tree pushed_scope = NULL_TREE;
22895 /* If PARSER->SCOPE is a dependent type, then it must be a
22896 class type, and we must not be checking dependencies;
22897 otherwise, we would have processed this lookup above. So
22898 that PARSER->SCOPE is not considered a dependent base by
22899 lookup_member, we must enter the scope here. */
22900 if (dependent_p)
22901 pushed_scope = push_scope (parser->scope);
22903 /* If the PARSER->SCOPE is a template specialization, it
22904 may be instantiated during name lookup. In that case,
22905 errors may be issued. Even if we rollback the current
22906 tentative parse, those errors are valid. */
22907 decl = lookup_qualified_name (parser->scope, name,
22908 tag_type != none_type,
22909 /*complain=*/true);
22911 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22912 lookup result and the nested-name-specifier nominates a class C:
22913 * if the name specified after the nested-name-specifier, when
22914 looked up in C, is the injected-class-name of C (Clause 9), or
22915 * if the name specified after the nested-name-specifier is the
22916 same as the identifier or the simple-template-id's template-
22917 name in the last component of the nested-name-specifier,
22918 the name is instead considered to name the constructor of
22919 class C. [ Note: for example, the constructor is not an
22920 acceptable lookup result in an elaborated-type-specifier so
22921 the constructor would not be used in place of the
22922 injected-class-name. --end note ] Such a constructor name
22923 shall be used only in the declarator-id of a declaration that
22924 names a constructor or in a using-declaration. */
22925 if (tag_type == none_type
22926 && DECL_SELF_REFERENCE_P (decl)
22927 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22928 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22929 tag_type != none_type,
22930 /*complain=*/true);
22932 /* If we have a single function from a using decl, pull it out. */
22933 if (TREE_CODE (decl) == OVERLOAD
22934 && !really_overloaded_fn (decl))
22935 decl = OVL_FUNCTION (decl);
22937 if (pushed_scope)
22938 pop_scope (pushed_scope);
22941 /* If the scope is a dependent type and either we deferred lookup or
22942 we did lookup but didn't find the name, rememeber the name. */
22943 if (decl == error_mark_node && TYPE_P (parser->scope)
22944 && dependent_type_p (parser->scope))
22946 if (tag_type)
22948 tree type;
22950 /* The resolution to Core Issue 180 says that `struct
22951 A::B' should be considered a type-name, even if `A'
22952 is dependent. */
22953 type = make_typename_type (parser->scope, name, tag_type,
22954 /*complain=*/tf_error);
22955 if (type != error_mark_node)
22956 decl = TYPE_NAME (type);
22958 else if (is_template
22959 && (cp_parser_next_token_ends_template_argument_p (parser)
22960 || cp_lexer_next_token_is (parser->lexer,
22961 CPP_CLOSE_PAREN)))
22962 decl = make_unbound_class_template (parser->scope,
22963 name, NULL_TREE,
22964 /*complain=*/tf_error);
22965 else
22966 decl = build_qualified_name (/*type=*/NULL_TREE,
22967 parser->scope, name,
22968 is_template);
22970 parser->qualifying_scope = parser->scope;
22971 parser->object_scope = NULL_TREE;
22973 else if (object_type)
22975 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22976 OBJECT_TYPE is not a class. */
22977 if (CLASS_TYPE_P (object_type))
22978 /* If the OBJECT_TYPE is a template specialization, it may
22979 be instantiated during name lookup. In that case, errors
22980 may be issued. Even if we rollback the current tentative
22981 parse, those errors are valid. */
22982 decl = lookup_member (object_type,
22983 name,
22984 /*protect=*/0,
22985 tag_type != none_type,
22986 tf_warning_or_error);
22987 else
22988 decl = NULL_TREE;
22990 if (!decl)
22991 /* Look it up in the enclosing context. */
22992 decl = lookup_name_real (name, tag_type != none_type,
22993 /*nonclass=*/0,
22994 /*block_p=*/true, is_namespace, 0);
22995 parser->object_scope = object_type;
22996 parser->qualifying_scope = NULL_TREE;
22998 else
23000 decl = lookup_name_real (name, tag_type != none_type,
23001 /*nonclass=*/0,
23002 /*block_p=*/true, is_namespace, 0);
23003 parser->qualifying_scope = NULL_TREE;
23004 parser->object_scope = NULL_TREE;
23007 /* If the lookup failed, let our caller know. */
23008 if (!decl || decl == error_mark_node)
23009 return error_mark_node;
23011 /* Pull out the template from an injected-class-name (or multiple). */
23012 if (is_template)
23013 decl = maybe_get_template_decl_from_type_decl (decl);
23015 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
23016 if (TREE_CODE (decl) == TREE_LIST)
23018 if (ambiguous_decls)
23019 *ambiguous_decls = decl;
23020 /* The error message we have to print is too complicated for
23021 cp_parser_error, so we incorporate its actions directly. */
23022 if (!cp_parser_simulate_error (parser))
23024 error_at (name_location, "reference to %qD is ambiguous",
23025 name);
23026 print_candidates (decl);
23028 return error_mark_node;
23031 gcc_assert (DECL_P (decl)
23032 || TREE_CODE (decl) == OVERLOAD
23033 || TREE_CODE (decl) == SCOPE_REF
23034 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
23035 || BASELINK_P (decl));
23037 /* If we have resolved the name of a member declaration, check to
23038 see if the declaration is accessible. When the name resolves to
23039 set of overloaded functions, accessibility is checked when
23040 overload resolution is done.
23042 During an explicit instantiation, access is not checked at all,
23043 as per [temp.explicit]. */
23044 if (DECL_P (decl))
23045 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
23047 maybe_record_typedef_use (decl);
23049 return decl;
23052 /* Like cp_parser_lookup_name, but for use in the typical case where
23053 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
23054 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
23056 static tree
23057 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
23059 return cp_parser_lookup_name (parser, name,
23060 none_type,
23061 /*is_template=*/false,
23062 /*is_namespace=*/false,
23063 /*check_dependency=*/true,
23064 /*ambiguous_decls=*/NULL,
23065 location);
23068 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
23069 the current context, return the TYPE_DECL. If TAG_NAME_P is
23070 true, the DECL indicates the class being defined in a class-head,
23071 or declared in an elaborated-type-specifier.
23073 Otherwise, return DECL. */
23075 static tree
23076 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
23078 /* If the TEMPLATE_DECL is being declared as part of a class-head,
23079 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
23081 struct A {
23082 template <typename T> struct B;
23085 template <typename T> struct A::B {};
23087 Similarly, in an elaborated-type-specifier:
23089 namespace N { struct X{}; }
23091 struct A {
23092 template <typename T> friend struct N::X;
23095 However, if the DECL refers to a class type, and we are in
23096 the scope of the class, then the name lookup automatically
23097 finds the TYPE_DECL created by build_self_reference rather
23098 than a TEMPLATE_DECL. For example, in:
23100 template <class T> struct S {
23101 S s;
23104 there is no need to handle such case. */
23106 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23107 return DECL_TEMPLATE_RESULT (decl);
23109 return decl;
23112 /* If too many, or too few, template-parameter lists apply to the
23113 declarator, issue an error message. Returns TRUE if all went well,
23114 and FALSE otherwise. */
23116 static bool
23117 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23118 cp_declarator *declarator,
23119 location_t declarator_location)
23121 switch (declarator->kind)
23123 case cdk_id:
23125 unsigned num_templates = 0;
23126 tree scope = declarator->u.id.qualifying_scope;
23128 if (scope)
23129 num_templates = num_template_headers_for_class (scope);
23130 else if (TREE_CODE (declarator->u.id.unqualified_name)
23131 == TEMPLATE_ID_EXPR)
23132 /* If the DECLARATOR has the form `X<y>' then it uses one
23133 additional level of template parameters. */
23134 ++num_templates;
23136 return cp_parser_check_template_parameters
23137 (parser, num_templates, declarator_location, declarator);
23140 case cdk_function:
23141 case cdk_array:
23142 case cdk_pointer:
23143 case cdk_reference:
23144 case cdk_ptrmem:
23145 return (cp_parser_check_declarator_template_parameters
23146 (parser, declarator->declarator, declarator_location));
23148 case cdk_error:
23149 return true;
23151 default:
23152 gcc_unreachable ();
23154 return false;
23157 /* NUM_TEMPLATES were used in the current declaration. If that is
23158 invalid, return FALSE and issue an error messages. Otherwise,
23159 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23160 declarator and we can print more accurate diagnostics. */
23162 static bool
23163 cp_parser_check_template_parameters (cp_parser* parser,
23164 unsigned num_templates,
23165 location_t location,
23166 cp_declarator *declarator)
23168 /* If there are the same number of template classes and parameter
23169 lists, that's OK. */
23170 if (parser->num_template_parameter_lists == num_templates)
23171 return true;
23172 /* If there are more, but only one more, then we are referring to a
23173 member template. That's OK too. */
23174 if (parser->num_template_parameter_lists == num_templates + 1)
23175 return true;
23176 /* If there are more template classes than parameter lists, we have
23177 something like:
23179 template <class T> void S<T>::R<T>::f (); */
23180 if (parser->num_template_parameter_lists < num_templates)
23182 if (declarator && !current_function_decl)
23183 error_at (location, "specializing member %<%T::%E%> "
23184 "requires %<template<>%> syntax",
23185 declarator->u.id.qualifying_scope,
23186 declarator->u.id.unqualified_name);
23187 else if (declarator)
23188 error_at (location, "invalid declaration of %<%T::%E%>",
23189 declarator->u.id.qualifying_scope,
23190 declarator->u.id.unqualified_name);
23191 else
23192 error_at (location, "too few template-parameter-lists");
23193 return false;
23195 /* Otherwise, there are too many template parameter lists. We have
23196 something like:
23198 template <class T> template <class U> void S::f(); */
23199 error_at (location, "too many template-parameter-lists");
23200 return false;
23203 /* Parse an optional `::' token indicating that the following name is
23204 from the global namespace. If so, PARSER->SCOPE is set to the
23205 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23206 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23207 Returns the new value of PARSER->SCOPE, if the `::' token is
23208 present, and NULL_TREE otherwise. */
23210 static tree
23211 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23213 cp_token *token;
23215 /* Peek at the next token. */
23216 token = cp_lexer_peek_token (parser->lexer);
23217 /* If we're looking at a `::' token then we're starting from the
23218 global namespace, not our current location. */
23219 if (token->type == CPP_SCOPE)
23221 /* Consume the `::' token. */
23222 cp_lexer_consume_token (parser->lexer);
23223 /* Set the SCOPE so that we know where to start the lookup. */
23224 parser->scope = global_namespace;
23225 parser->qualifying_scope = global_namespace;
23226 parser->object_scope = NULL_TREE;
23228 return parser->scope;
23230 else if (!current_scope_valid_p)
23232 parser->scope = NULL_TREE;
23233 parser->qualifying_scope = NULL_TREE;
23234 parser->object_scope = NULL_TREE;
23237 return NULL_TREE;
23240 /* Returns TRUE if the upcoming token sequence is the start of a
23241 constructor declarator. If FRIEND_P is true, the declarator is
23242 preceded by the `friend' specifier. */
23244 static bool
23245 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23247 bool constructor_p;
23248 bool outside_class_specifier_p;
23249 tree nested_name_specifier;
23250 cp_token *next_token;
23252 /* The common case is that this is not a constructor declarator, so
23253 try to avoid doing lots of work if at all possible. It's not
23254 valid declare a constructor at function scope. */
23255 if (parser->in_function_body)
23256 return false;
23257 /* And only certain tokens can begin a constructor declarator. */
23258 next_token = cp_lexer_peek_token (parser->lexer);
23259 if (next_token->type != CPP_NAME
23260 && next_token->type != CPP_SCOPE
23261 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23262 && next_token->type != CPP_TEMPLATE_ID)
23263 return false;
23265 /* Parse tentatively; we are going to roll back all of the tokens
23266 consumed here. */
23267 cp_parser_parse_tentatively (parser);
23268 /* Assume that we are looking at a constructor declarator. */
23269 constructor_p = true;
23271 /* Look for the optional `::' operator. */
23272 cp_parser_global_scope_opt (parser,
23273 /*current_scope_valid_p=*/false);
23274 /* Look for the nested-name-specifier. */
23275 nested_name_specifier
23276 = (cp_parser_nested_name_specifier_opt (parser,
23277 /*typename_keyword_p=*/false,
23278 /*check_dependency_p=*/false,
23279 /*type_p=*/false,
23280 /*is_declaration=*/false));
23282 outside_class_specifier_p = (!at_class_scope_p ()
23283 || !TYPE_BEING_DEFINED (current_class_type)
23284 || friend_p);
23286 /* Outside of a class-specifier, there must be a
23287 nested-name-specifier. */
23288 if (!nested_name_specifier && outside_class_specifier_p)
23289 constructor_p = false;
23290 else if (nested_name_specifier == error_mark_node)
23291 constructor_p = false;
23293 /* If we have a class scope, this is easy; DR 147 says that S::S always
23294 names the constructor, and no other qualified name could. */
23295 if (constructor_p && nested_name_specifier
23296 && CLASS_TYPE_P (nested_name_specifier))
23298 tree id = cp_parser_unqualified_id (parser,
23299 /*template_keyword_p=*/false,
23300 /*check_dependency_p=*/false,
23301 /*declarator_p=*/true,
23302 /*optional_p=*/false);
23303 if (is_overloaded_fn (id))
23304 id = DECL_NAME (get_first_fn (id));
23305 if (!constructor_name_p (id, nested_name_specifier))
23306 constructor_p = false;
23308 /* If we still think that this might be a constructor-declarator,
23309 look for a class-name. */
23310 else if (constructor_p)
23312 /* If we have:
23314 template <typename T> struct S {
23315 S();
23318 we must recognize that the nested `S' names a class. */
23319 tree type_decl;
23320 type_decl = cp_parser_class_name (parser,
23321 /*typename_keyword_p=*/false,
23322 /*template_keyword_p=*/false,
23323 none_type,
23324 /*check_dependency_p=*/false,
23325 /*class_head_p=*/false,
23326 /*is_declaration=*/false);
23327 /* If there was no class-name, then this is not a constructor.
23328 Otherwise, if we are in a class-specifier and we aren't
23329 handling a friend declaration, check that its type matches
23330 current_class_type (c++/38313). Note: error_mark_node
23331 is left alone for error recovery purposes. */
23332 constructor_p = (!cp_parser_error_occurred (parser)
23333 && (outside_class_specifier_p
23334 || type_decl == error_mark_node
23335 || same_type_p (current_class_type,
23336 TREE_TYPE (type_decl))));
23338 /* If we're still considering a constructor, we have to see a `(',
23339 to begin the parameter-declaration-clause, followed by either a
23340 `)', an `...', or a decl-specifier. We need to check for a
23341 type-specifier to avoid being fooled into thinking that:
23343 S (f) (int);
23345 is a constructor. (It is actually a function named `f' that
23346 takes one parameter (of type `int') and returns a value of type
23347 `S'. */
23348 if (constructor_p
23349 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23350 constructor_p = false;
23352 if (constructor_p
23353 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23354 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23355 /* A parameter declaration begins with a decl-specifier,
23356 which is either the "attribute" keyword, a storage class
23357 specifier, or (usually) a type-specifier. */
23358 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23360 tree type;
23361 tree pushed_scope = NULL_TREE;
23362 unsigned saved_num_template_parameter_lists;
23364 /* Names appearing in the type-specifier should be looked up
23365 in the scope of the class. */
23366 if (current_class_type)
23367 type = NULL_TREE;
23368 else
23370 type = TREE_TYPE (type_decl);
23371 if (TREE_CODE (type) == TYPENAME_TYPE)
23373 type = resolve_typename_type (type,
23374 /*only_current_p=*/false);
23375 if (TREE_CODE (type) == TYPENAME_TYPE)
23377 cp_parser_abort_tentative_parse (parser);
23378 return false;
23381 pushed_scope = push_scope (type);
23384 /* Inside the constructor parameter list, surrounding
23385 template-parameter-lists do not apply. */
23386 saved_num_template_parameter_lists
23387 = parser->num_template_parameter_lists;
23388 parser->num_template_parameter_lists = 0;
23390 /* Look for the type-specifier. */
23391 cp_parser_type_specifier (parser,
23392 CP_PARSER_FLAGS_NONE,
23393 /*decl_specs=*/NULL,
23394 /*is_declarator=*/true,
23395 /*declares_class_or_enum=*/NULL,
23396 /*is_cv_qualifier=*/NULL);
23398 parser->num_template_parameter_lists
23399 = saved_num_template_parameter_lists;
23401 /* Leave the scope of the class. */
23402 if (pushed_scope)
23403 pop_scope (pushed_scope);
23405 constructor_p = !cp_parser_error_occurred (parser);
23409 /* We did not really want to consume any tokens. */
23410 cp_parser_abort_tentative_parse (parser);
23412 return constructor_p;
23415 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23416 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23417 they must be performed once we are in the scope of the function.
23419 Returns the function defined. */
23421 static tree
23422 cp_parser_function_definition_from_specifiers_and_declarator
23423 (cp_parser* parser,
23424 cp_decl_specifier_seq *decl_specifiers,
23425 tree attributes,
23426 const cp_declarator *declarator)
23428 tree fn;
23429 bool success_p;
23431 /* Begin the function-definition. */
23432 success_p = start_function (decl_specifiers, declarator, attributes);
23434 /* The things we're about to see are not directly qualified by any
23435 template headers we've seen thus far. */
23436 reset_specialization ();
23438 /* If there were names looked up in the decl-specifier-seq that we
23439 did not check, check them now. We must wait until we are in the
23440 scope of the function to perform the checks, since the function
23441 might be a friend. */
23442 perform_deferred_access_checks (tf_warning_or_error);
23444 if (success_p)
23446 cp_finalize_omp_declare_simd (parser, current_function_decl);
23447 parser->omp_declare_simd = NULL;
23450 if (!success_p)
23452 /* Skip the entire function. */
23453 cp_parser_skip_to_end_of_block_or_statement (parser);
23454 fn = error_mark_node;
23456 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23458 /* Seen already, skip it. An error message has already been output. */
23459 cp_parser_skip_to_end_of_block_or_statement (parser);
23460 fn = current_function_decl;
23461 current_function_decl = NULL_TREE;
23462 /* If this is a function from a class, pop the nested class. */
23463 if (current_class_name)
23464 pop_nested_class ();
23466 else
23468 timevar_id_t tv;
23469 if (DECL_DECLARED_INLINE_P (current_function_decl))
23470 tv = TV_PARSE_INLINE;
23471 else
23472 tv = TV_PARSE_FUNC;
23473 timevar_push (tv);
23474 fn = cp_parser_function_definition_after_declarator (parser,
23475 /*inline_p=*/false);
23476 timevar_pop (tv);
23479 return fn;
23482 /* Parse the part of a function-definition that follows the
23483 declarator. INLINE_P is TRUE iff this function is an inline
23484 function defined within a class-specifier.
23486 Returns the function defined. */
23488 static tree
23489 cp_parser_function_definition_after_declarator (cp_parser* parser,
23490 bool inline_p)
23492 tree fn;
23493 bool ctor_initializer_p = false;
23494 bool saved_in_unbraced_linkage_specification_p;
23495 bool saved_in_function_body;
23496 unsigned saved_num_template_parameter_lists;
23497 cp_token *token;
23498 bool fully_implicit_function_template_p
23499 = parser->fully_implicit_function_template_p;
23500 parser->fully_implicit_function_template_p = false;
23501 tree implicit_template_parms
23502 = parser->implicit_template_parms;
23503 parser->implicit_template_parms = 0;
23504 cp_binding_level* implicit_template_scope
23505 = parser->implicit_template_scope;
23506 parser->implicit_template_scope = 0;
23508 saved_in_function_body = parser->in_function_body;
23509 parser->in_function_body = true;
23510 /* If the next token is `return', then the code may be trying to
23511 make use of the "named return value" extension that G++ used to
23512 support. */
23513 token = cp_lexer_peek_token (parser->lexer);
23514 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23516 /* Consume the `return' keyword. */
23517 cp_lexer_consume_token (parser->lexer);
23518 /* Look for the identifier that indicates what value is to be
23519 returned. */
23520 cp_parser_identifier (parser);
23521 /* Issue an error message. */
23522 error_at (token->location,
23523 "named return values are no longer supported");
23524 /* Skip tokens until we reach the start of the function body. */
23525 while (true)
23527 cp_token *token = cp_lexer_peek_token (parser->lexer);
23528 if (token->type == CPP_OPEN_BRACE
23529 || token->type == CPP_EOF
23530 || token->type == CPP_PRAGMA_EOL)
23531 break;
23532 cp_lexer_consume_token (parser->lexer);
23535 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23536 anything declared inside `f'. */
23537 saved_in_unbraced_linkage_specification_p
23538 = parser->in_unbraced_linkage_specification_p;
23539 parser->in_unbraced_linkage_specification_p = false;
23540 /* Inside the function, surrounding template-parameter-lists do not
23541 apply. */
23542 saved_num_template_parameter_lists
23543 = parser->num_template_parameter_lists;
23544 parser->num_template_parameter_lists = 0;
23546 start_lambda_scope (current_function_decl);
23548 /* If the next token is `try', `__transaction_atomic', or
23549 `__transaction_relaxed`, then we are looking at either function-try-block
23550 or function-transaction-block. Note that all of these include the
23551 function-body. */
23552 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23553 ctor_initializer_p = cp_parser_function_transaction (parser,
23554 RID_TRANSACTION_ATOMIC);
23555 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23556 RID_TRANSACTION_RELAXED))
23557 ctor_initializer_p = cp_parser_function_transaction (parser,
23558 RID_TRANSACTION_RELAXED);
23559 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23560 ctor_initializer_p = cp_parser_function_try_block (parser);
23561 else
23562 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23563 (parser, /*in_function_try_block=*/false);
23565 finish_lambda_scope ();
23567 /* Finish the function. */
23568 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23569 (inline_p ? 2 : 0));
23570 /* Generate code for it, if necessary. */
23571 expand_or_defer_fn (fn);
23572 /* Restore the saved values. */
23573 parser->in_unbraced_linkage_specification_p
23574 = saved_in_unbraced_linkage_specification_p;
23575 parser->num_template_parameter_lists
23576 = saved_num_template_parameter_lists;
23577 parser->in_function_body = saved_in_function_body;
23579 parser->fully_implicit_function_template_p
23580 = fully_implicit_function_template_p;
23581 parser->implicit_template_parms
23582 = implicit_template_parms;
23583 parser->implicit_template_scope
23584 = implicit_template_scope;
23586 if (parser->fully_implicit_function_template_p)
23587 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23589 return fn;
23592 /* Parse a template-declaration, assuming that the `export' (and
23593 `extern') keywords, if present, has already been scanned. MEMBER_P
23594 is as for cp_parser_template_declaration. */
23596 static void
23597 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23599 tree decl = NULL_TREE;
23600 vec<deferred_access_check, va_gc> *checks;
23601 tree parameter_list;
23602 bool friend_p = false;
23603 bool need_lang_pop;
23604 cp_token *token;
23606 /* Look for the `template' keyword. */
23607 token = cp_lexer_peek_token (parser->lexer);
23608 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23609 return;
23611 /* And the `<'. */
23612 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23613 return;
23614 if (at_class_scope_p () && current_function_decl)
23616 /* 14.5.2.2 [temp.mem]
23618 A local class shall not have member templates. */
23619 error_at (token->location,
23620 "invalid declaration of member template in local class");
23621 cp_parser_skip_to_end_of_block_or_statement (parser);
23622 return;
23624 /* [temp]
23626 A template ... shall not have C linkage. */
23627 if (current_lang_name == lang_name_c)
23629 error_at (token->location, "template with C linkage");
23630 /* Give it C++ linkage to avoid confusing other parts of the
23631 front end. */
23632 push_lang_context (lang_name_cplusplus);
23633 need_lang_pop = true;
23635 else
23636 need_lang_pop = false;
23638 /* We cannot perform access checks on the template parameter
23639 declarations until we know what is being declared, just as we
23640 cannot check the decl-specifier list. */
23641 push_deferring_access_checks (dk_deferred);
23643 /* If the next token is `>', then we have an invalid
23644 specialization. Rather than complain about an invalid template
23645 parameter, issue an error message here. */
23646 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23648 cp_parser_error (parser, "invalid explicit specialization");
23649 begin_specialization ();
23650 parameter_list = NULL_TREE;
23652 else
23654 /* Parse the template parameters. */
23655 parameter_list = cp_parser_template_parameter_list (parser);
23658 /* Get the deferred access checks from the parameter list. These
23659 will be checked once we know what is being declared, as for a
23660 member template the checks must be performed in the scope of the
23661 class containing the member. */
23662 checks = get_deferred_access_checks ();
23664 /* Look for the `>'. */
23665 cp_parser_skip_to_end_of_template_parameter_list (parser);
23666 /* We just processed one more parameter list. */
23667 ++parser->num_template_parameter_lists;
23668 /* If the next token is `template', there are more template
23669 parameters. */
23670 if (cp_lexer_next_token_is_keyword (parser->lexer,
23671 RID_TEMPLATE))
23672 cp_parser_template_declaration_after_export (parser, member_p);
23673 else if (cxx_dialect >= cxx11
23674 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23675 decl = cp_parser_alias_declaration (parser);
23676 else
23678 /* There are no access checks when parsing a template, as we do not
23679 know if a specialization will be a friend. */
23680 push_deferring_access_checks (dk_no_check);
23681 token = cp_lexer_peek_token (parser->lexer);
23682 decl = cp_parser_single_declaration (parser,
23683 checks,
23684 member_p,
23685 /*explicit_specialization_p=*/false,
23686 &friend_p);
23687 pop_deferring_access_checks ();
23689 /* If this is a member template declaration, let the front
23690 end know. */
23691 if (member_p && !friend_p && decl)
23693 if (TREE_CODE (decl) == TYPE_DECL)
23694 cp_parser_check_access_in_redeclaration (decl, token->location);
23696 decl = finish_member_template_decl (decl);
23698 else if (friend_p && decl
23699 && DECL_DECLARES_TYPE_P (decl))
23700 make_friend_class (current_class_type, TREE_TYPE (decl),
23701 /*complain=*/true);
23703 /* We are done with the current parameter list. */
23704 --parser->num_template_parameter_lists;
23706 pop_deferring_access_checks ();
23708 /* Finish up. */
23709 finish_template_decl (parameter_list);
23711 /* Check the template arguments for a literal operator template. */
23712 if (decl
23713 && DECL_DECLARES_FUNCTION_P (decl)
23714 && UDLIT_OPER_P (DECL_NAME (decl)))
23716 bool ok = true;
23717 if (parameter_list == NULL_TREE)
23718 ok = false;
23719 else
23721 int num_parms = TREE_VEC_LENGTH (parameter_list);
23722 if (num_parms == 1)
23724 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23725 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23726 if (TREE_TYPE (parm) != char_type_node
23727 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23728 ok = false;
23730 else if (num_parms == 2 && cxx_dialect >= cxx14)
23732 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23733 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23734 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23735 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23736 if (TREE_TYPE (parm) != TREE_TYPE (type)
23737 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23738 ok = false;
23740 else
23741 ok = false;
23743 if (!ok)
23745 if (cxx_dialect >= cxx14)
23746 error ("literal operator template %qD has invalid parameter list."
23747 " Expected non-type template argument pack <char...>"
23748 " or <typename CharT, CharT...>",
23749 decl);
23750 else
23751 error ("literal operator template %qD has invalid parameter list."
23752 " Expected non-type template argument pack <char...>",
23753 decl);
23756 /* Register member declarations. */
23757 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23758 finish_member_declaration (decl);
23759 /* For the erroneous case of a template with C linkage, we pushed an
23760 implicit C++ linkage scope; exit that scope now. */
23761 if (need_lang_pop)
23762 pop_lang_context ();
23763 /* If DECL is a function template, we must return to parse it later.
23764 (Even though there is no definition, there might be default
23765 arguments that need handling.) */
23766 if (member_p && decl
23767 && DECL_DECLARES_FUNCTION_P (decl))
23768 vec_safe_push (unparsed_funs_with_definitions, decl);
23771 /* Perform the deferred access checks from a template-parameter-list.
23772 CHECKS is a TREE_LIST of access checks, as returned by
23773 get_deferred_access_checks. */
23775 static void
23776 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23778 ++processing_template_parmlist;
23779 perform_access_checks (checks, tf_warning_or_error);
23780 --processing_template_parmlist;
23783 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23784 `function-definition' sequence that follows a template header.
23785 If MEMBER_P is true, this declaration appears in a class scope.
23787 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23788 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23790 static tree
23791 cp_parser_single_declaration (cp_parser* parser,
23792 vec<deferred_access_check, va_gc> *checks,
23793 bool member_p,
23794 bool explicit_specialization_p,
23795 bool* friend_p)
23797 int declares_class_or_enum;
23798 tree decl = NULL_TREE;
23799 cp_decl_specifier_seq decl_specifiers;
23800 bool function_definition_p = false;
23801 cp_token *decl_spec_token_start;
23803 /* This function is only used when processing a template
23804 declaration. */
23805 gcc_assert (innermost_scope_kind () == sk_template_parms
23806 || innermost_scope_kind () == sk_template_spec);
23808 /* Defer access checks until we know what is being declared. */
23809 push_deferring_access_checks (dk_deferred);
23811 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23812 alternative. */
23813 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23814 cp_parser_decl_specifier_seq (parser,
23815 CP_PARSER_FLAGS_OPTIONAL,
23816 &decl_specifiers,
23817 &declares_class_or_enum);
23818 if (friend_p)
23819 *friend_p = cp_parser_friend_p (&decl_specifiers);
23821 /* There are no template typedefs. */
23822 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23824 error_at (decl_spec_token_start->location,
23825 "template declaration of %<typedef%>");
23826 decl = error_mark_node;
23829 /* Gather up the access checks that occurred the
23830 decl-specifier-seq. */
23831 stop_deferring_access_checks ();
23833 /* Check for the declaration of a template class. */
23834 if (declares_class_or_enum)
23836 if (cp_parser_declares_only_class_p (parser))
23838 decl = shadow_tag (&decl_specifiers);
23840 /* In this case:
23842 struct C {
23843 friend template <typename T> struct A<T>::B;
23846 A<T>::B will be represented by a TYPENAME_TYPE, and
23847 therefore not recognized by shadow_tag. */
23848 if (friend_p && *friend_p
23849 && !decl
23850 && decl_specifiers.type
23851 && TYPE_P (decl_specifiers.type))
23852 decl = decl_specifiers.type;
23854 if (decl && decl != error_mark_node)
23855 decl = TYPE_NAME (decl);
23856 else
23857 decl = error_mark_node;
23859 /* Perform access checks for template parameters. */
23860 cp_parser_perform_template_parameter_access_checks (checks);
23864 /* Complain about missing 'typename' or other invalid type names. */
23865 if (!decl_specifiers.any_type_specifiers_p
23866 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23868 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23869 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23870 the rest of this declaration. */
23871 decl = error_mark_node;
23872 goto out;
23875 /* If it's not a template class, try for a template function. If
23876 the next token is a `;', then this declaration does not declare
23877 anything. But, if there were errors in the decl-specifiers, then
23878 the error might well have come from an attempted class-specifier.
23879 In that case, there's no need to warn about a missing declarator. */
23880 if (!decl
23881 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23882 || decl_specifiers.type != error_mark_node))
23884 decl = cp_parser_init_declarator (parser,
23885 &decl_specifiers,
23886 checks,
23887 /*function_definition_allowed_p=*/true,
23888 member_p,
23889 declares_class_or_enum,
23890 &function_definition_p,
23891 NULL, NULL);
23893 /* 7.1.1-1 [dcl.stc]
23895 A storage-class-specifier shall not be specified in an explicit
23896 specialization... */
23897 if (decl
23898 && explicit_specialization_p
23899 && decl_specifiers.storage_class != sc_none)
23901 error_at (decl_spec_token_start->location,
23902 "explicit template specialization cannot have a storage class");
23903 decl = error_mark_node;
23906 if (decl && VAR_P (decl))
23907 check_template_variable (decl);
23910 /* Look for a trailing `;' after the declaration. */
23911 if (!function_definition_p
23912 && (decl == error_mark_node
23913 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23914 cp_parser_skip_to_end_of_block_or_statement (parser);
23916 out:
23917 pop_deferring_access_checks ();
23919 /* Clear any current qualification; whatever comes next is the start
23920 of something new. */
23921 parser->scope = NULL_TREE;
23922 parser->qualifying_scope = NULL_TREE;
23923 parser->object_scope = NULL_TREE;
23925 return decl;
23928 /* Parse a cast-expression that is not the operand of a unary "&". */
23930 static tree
23931 cp_parser_simple_cast_expression (cp_parser *parser)
23933 return cp_parser_cast_expression (parser, /*address_p=*/false,
23934 /*cast_p=*/false, /*decltype*/false, NULL);
23937 /* Parse a functional cast to TYPE. Returns an expression
23938 representing the cast. */
23940 static tree
23941 cp_parser_functional_cast (cp_parser* parser, tree type)
23943 vec<tree, va_gc> *vec;
23944 tree expression_list;
23945 tree cast;
23946 bool nonconst_p;
23948 if (!type)
23949 type = error_mark_node;
23951 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23953 cp_lexer_set_source_position (parser->lexer);
23954 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23955 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23956 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23957 if (TREE_CODE (type) == TYPE_DECL)
23958 type = TREE_TYPE (type);
23959 return finish_compound_literal (type, expression_list,
23960 tf_warning_or_error);
23964 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23965 /*cast_p=*/true,
23966 /*allow_expansion_p=*/true,
23967 /*non_constant_p=*/NULL);
23968 if (vec == NULL)
23969 expression_list = error_mark_node;
23970 else
23972 expression_list = build_tree_list_vec (vec);
23973 release_tree_vector (vec);
23976 cast = build_functional_cast (type, expression_list,
23977 tf_warning_or_error);
23978 /* [expr.const]/1: In an integral constant expression "only type
23979 conversions to integral or enumeration type can be used". */
23980 if (TREE_CODE (type) == TYPE_DECL)
23981 type = TREE_TYPE (type);
23982 if (cast != error_mark_node
23983 && !cast_valid_in_integral_constant_expression_p (type)
23984 && cp_parser_non_integral_constant_expression (parser,
23985 NIC_CONSTRUCTOR))
23986 return error_mark_node;
23987 return cast;
23990 /* Save the tokens that make up the body of a member function defined
23991 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23992 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23993 specifiers applied to the declaration. Returns the FUNCTION_DECL
23994 for the member function. */
23996 static tree
23997 cp_parser_save_member_function_body (cp_parser* parser,
23998 cp_decl_specifier_seq *decl_specifiers,
23999 cp_declarator *declarator,
24000 tree attributes)
24002 cp_token *first;
24003 cp_token *last;
24004 tree fn;
24006 /* Create the FUNCTION_DECL. */
24007 fn = grokmethod (decl_specifiers, declarator, attributes);
24008 cp_finalize_omp_declare_simd (parser, fn);
24009 /* If something went badly wrong, bail out now. */
24010 if (fn == error_mark_node)
24012 /* If there's a function-body, skip it. */
24013 if (cp_parser_token_starts_function_definition_p
24014 (cp_lexer_peek_token (parser->lexer)))
24015 cp_parser_skip_to_end_of_block_or_statement (parser);
24016 return error_mark_node;
24019 /* Remember it, if there default args to post process. */
24020 cp_parser_save_default_args (parser, fn);
24022 /* Save away the tokens that make up the body of the
24023 function. */
24024 first = parser->lexer->next_token;
24025 /* Handle function try blocks. */
24026 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
24027 cp_lexer_consume_token (parser->lexer);
24028 /* We can have braced-init-list mem-initializers before the fn body. */
24029 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24031 cp_lexer_consume_token (parser->lexer);
24032 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
24034 /* cache_group will stop after an un-nested { } pair, too. */
24035 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
24036 break;
24038 /* variadic mem-inits have ... after the ')'. */
24039 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24040 cp_lexer_consume_token (parser->lexer);
24043 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24044 /* Handle function try blocks. */
24045 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
24046 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24047 last = parser->lexer->next_token;
24049 /* Save away the inline definition; we will process it when the
24050 class is complete. */
24051 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
24052 DECL_PENDING_INLINE_P (fn) = 1;
24054 /* We need to know that this was defined in the class, so that
24055 friend templates are handled correctly. */
24056 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
24058 /* Add FN to the queue of functions to be parsed later. */
24059 vec_safe_push (unparsed_funs_with_definitions, fn);
24061 return fn;
24064 /* Save the tokens that make up the in-class initializer for a non-static
24065 data member. Returns a DEFAULT_ARG. */
24067 static tree
24068 cp_parser_save_nsdmi (cp_parser* parser)
24070 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
24073 /* Parse a template-argument-list, as well as the trailing ">" (but
24074 not the opening "<"). See cp_parser_template_argument_list for the
24075 return value. */
24077 static tree
24078 cp_parser_enclosed_template_argument_list (cp_parser* parser)
24080 tree arguments;
24081 tree saved_scope;
24082 tree saved_qualifying_scope;
24083 tree saved_object_scope;
24084 bool saved_greater_than_is_operator_p;
24085 int saved_unevaluated_operand;
24086 int saved_inhibit_evaluation_warnings;
24088 /* [temp.names]
24090 When parsing a template-id, the first non-nested `>' is taken as
24091 the end of the template-argument-list rather than a greater-than
24092 operator. */
24093 saved_greater_than_is_operator_p
24094 = parser->greater_than_is_operator_p;
24095 parser->greater_than_is_operator_p = false;
24096 /* Parsing the argument list may modify SCOPE, so we save it
24097 here. */
24098 saved_scope = parser->scope;
24099 saved_qualifying_scope = parser->qualifying_scope;
24100 saved_object_scope = parser->object_scope;
24101 /* We need to evaluate the template arguments, even though this
24102 template-id may be nested within a "sizeof". */
24103 saved_unevaluated_operand = cp_unevaluated_operand;
24104 cp_unevaluated_operand = 0;
24105 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24106 c_inhibit_evaluation_warnings = 0;
24107 /* Parse the template-argument-list itself. */
24108 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
24109 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24110 arguments = NULL_TREE;
24111 else
24112 arguments = cp_parser_template_argument_list (parser);
24113 /* Look for the `>' that ends the template-argument-list. If we find
24114 a '>>' instead, it's probably just a typo. */
24115 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24117 if (cxx_dialect != cxx98)
24119 /* In C++0x, a `>>' in a template argument list or cast
24120 expression is considered to be two separate `>'
24121 tokens. So, change the current token to a `>', but don't
24122 consume it: it will be consumed later when the outer
24123 template argument list (or cast expression) is parsed.
24124 Note that this replacement of `>' for `>>' is necessary
24125 even if we are parsing tentatively: in the tentative
24126 case, after calling
24127 cp_parser_enclosed_template_argument_list we will always
24128 throw away all of the template arguments and the first
24129 closing `>', either because the template argument list
24130 was erroneous or because we are replacing those tokens
24131 with a CPP_TEMPLATE_ID token. The second `>' (which will
24132 not have been thrown away) is needed either to close an
24133 outer template argument list or to complete a new-style
24134 cast. */
24135 cp_token *token = cp_lexer_peek_token (parser->lexer);
24136 token->type = CPP_GREATER;
24138 else if (!saved_greater_than_is_operator_p)
24140 /* If we're in a nested template argument list, the '>>' has
24141 to be a typo for '> >'. We emit the error message, but we
24142 continue parsing and we push a '>' as next token, so that
24143 the argument list will be parsed correctly. Note that the
24144 global source location is still on the token before the
24145 '>>', so we need to say explicitly where we want it. */
24146 cp_token *token = cp_lexer_peek_token (parser->lexer);
24147 error_at (token->location, "%<>>%> should be %<> >%> "
24148 "within a nested template argument list");
24150 token->type = CPP_GREATER;
24152 else
24154 /* If this is not a nested template argument list, the '>>'
24155 is a typo for '>'. Emit an error message and continue.
24156 Same deal about the token location, but here we can get it
24157 right by consuming the '>>' before issuing the diagnostic. */
24158 cp_token *token = cp_lexer_consume_token (parser->lexer);
24159 error_at (token->location,
24160 "spurious %<>>%>, use %<>%> to terminate "
24161 "a template argument list");
24164 else
24165 cp_parser_skip_to_end_of_template_parameter_list (parser);
24166 /* The `>' token might be a greater-than operator again now. */
24167 parser->greater_than_is_operator_p
24168 = saved_greater_than_is_operator_p;
24169 /* Restore the SAVED_SCOPE. */
24170 parser->scope = saved_scope;
24171 parser->qualifying_scope = saved_qualifying_scope;
24172 parser->object_scope = saved_object_scope;
24173 cp_unevaluated_operand = saved_unevaluated_operand;
24174 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24176 return arguments;
24179 /* MEMBER_FUNCTION is a member function, or a friend. If default
24180 arguments, or the body of the function have not yet been parsed,
24181 parse them now. */
24183 static void
24184 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24186 timevar_push (TV_PARSE_INMETH);
24187 /* If this member is a template, get the underlying
24188 FUNCTION_DECL. */
24189 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24190 member_function = DECL_TEMPLATE_RESULT (member_function);
24192 /* There should not be any class definitions in progress at this
24193 point; the bodies of members are only parsed outside of all class
24194 definitions. */
24195 gcc_assert (parser->num_classes_being_defined == 0);
24196 /* While we're parsing the member functions we might encounter more
24197 classes. We want to handle them right away, but we don't want
24198 them getting mixed up with functions that are currently in the
24199 queue. */
24200 push_unparsed_function_queues (parser);
24202 /* Make sure that any template parameters are in scope. */
24203 maybe_begin_member_template_processing (member_function);
24205 /* If the body of the function has not yet been parsed, parse it
24206 now. */
24207 if (DECL_PENDING_INLINE_P (member_function))
24209 tree function_scope;
24210 cp_token_cache *tokens;
24212 /* The function is no longer pending; we are processing it. */
24213 tokens = DECL_PENDING_INLINE_INFO (member_function);
24214 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24215 DECL_PENDING_INLINE_P (member_function) = 0;
24217 /* If this is a local class, enter the scope of the containing
24218 function. */
24219 function_scope = current_function_decl;
24220 if (function_scope)
24221 push_function_context ();
24223 /* Push the body of the function onto the lexer stack. */
24224 cp_parser_push_lexer_for_tokens (parser, tokens);
24226 /* Let the front end know that we going to be defining this
24227 function. */
24228 start_preparsed_function (member_function, NULL_TREE,
24229 SF_PRE_PARSED | SF_INCLASS_INLINE);
24231 /* Don't do access checking if it is a templated function. */
24232 if (processing_template_decl)
24233 push_deferring_access_checks (dk_no_check);
24235 /* #pragma omp declare reduction needs special parsing. */
24236 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24238 parser->lexer->in_pragma = true;
24239 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24240 finish_function (/*inline*/2);
24241 cp_check_omp_declare_reduction (member_function);
24243 else
24244 /* Now, parse the body of the function. */
24245 cp_parser_function_definition_after_declarator (parser,
24246 /*inline_p=*/true);
24248 if (processing_template_decl)
24249 pop_deferring_access_checks ();
24251 /* Leave the scope of the containing function. */
24252 if (function_scope)
24253 pop_function_context ();
24254 cp_parser_pop_lexer (parser);
24257 /* Remove any template parameters from the symbol table. */
24258 maybe_end_member_template_processing ();
24260 /* Restore the queue. */
24261 pop_unparsed_function_queues (parser);
24262 timevar_pop (TV_PARSE_INMETH);
24265 /* If DECL contains any default args, remember it on the unparsed
24266 functions queue. */
24268 static void
24269 cp_parser_save_default_args (cp_parser* parser, tree decl)
24271 tree probe;
24273 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24274 probe;
24275 probe = TREE_CHAIN (probe))
24276 if (TREE_PURPOSE (probe))
24278 cp_default_arg_entry entry = {current_class_type, decl};
24279 vec_safe_push (unparsed_funs_with_default_args, entry);
24280 break;
24284 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24285 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24286 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24287 from the parameter-type-list. */
24289 static tree
24290 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24291 tree default_arg, tree parmtype)
24293 cp_token_cache *tokens;
24294 tree parsed_arg;
24295 bool dummy;
24297 if (default_arg == error_mark_node)
24298 return error_mark_node;
24300 /* Push the saved tokens for the default argument onto the parser's
24301 lexer stack. */
24302 tokens = DEFARG_TOKENS (default_arg);
24303 cp_parser_push_lexer_for_tokens (parser, tokens);
24305 start_lambda_scope (decl);
24307 /* Parse the default argument. */
24308 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24309 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24310 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24312 finish_lambda_scope ();
24314 if (parsed_arg == error_mark_node)
24315 cp_parser_skip_to_end_of_statement (parser);
24317 if (!processing_template_decl)
24319 /* In a non-template class, check conversions now. In a template,
24320 we'll wait and instantiate these as needed. */
24321 if (TREE_CODE (decl) == PARM_DECL)
24322 parsed_arg = check_default_argument (parmtype, parsed_arg,
24323 tf_warning_or_error);
24324 else
24325 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24328 /* If the token stream has not been completely used up, then
24329 there was extra junk after the end of the default
24330 argument. */
24331 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24333 if (TREE_CODE (decl) == PARM_DECL)
24334 cp_parser_error (parser, "expected %<,%>");
24335 else
24336 cp_parser_error (parser, "expected %<;%>");
24339 /* Revert to the main lexer. */
24340 cp_parser_pop_lexer (parser);
24342 return parsed_arg;
24345 /* FIELD is a non-static data member with an initializer which we saved for
24346 later; parse it now. */
24348 static void
24349 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24351 tree def;
24353 maybe_begin_member_template_processing (field);
24355 push_unparsed_function_queues (parser);
24356 def = cp_parser_late_parse_one_default_arg (parser, field,
24357 DECL_INITIAL (field),
24358 NULL_TREE);
24359 pop_unparsed_function_queues (parser);
24361 maybe_end_member_template_processing ();
24363 DECL_INITIAL (field) = def;
24366 /* FN is a FUNCTION_DECL which may contains a parameter with an
24367 unparsed DEFAULT_ARG. Parse the default args now. This function
24368 assumes that the current scope is the scope in which the default
24369 argument should be processed. */
24371 static void
24372 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24374 bool saved_local_variables_forbidden_p;
24375 tree parm, parmdecl;
24377 /* While we're parsing the default args, we might (due to the
24378 statement expression extension) encounter more classes. We want
24379 to handle them right away, but we don't want them getting mixed
24380 up with default args that are currently in the queue. */
24381 push_unparsed_function_queues (parser);
24383 /* Local variable names (and the `this' keyword) may not appear
24384 in a default argument. */
24385 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24386 parser->local_variables_forbidden_p = true;
24388 push_defarg_context (fn);
24390 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24391 parmdecl = DECL_ARGUMENTS (fn);
24392 parm && parm != void_list_node;
24393 parm = TREE_CHAIN (parm),
24394 parmdecl = DECL_CHAIN (parmdecl))
24396 tree default_arg = TREE_PURPOSE (parm);
24397 tree parsed_arg;
24398 vec<tree, va_gc> *insts;
24399 tree copy;
24400 unsigned ix;
24402 if (!default_arg)
24403 continue;
24405 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24406 /* This can happen for a friend declaration for a function
24407 already declared with default arguments. */
24408 continue;
24410 parsed_arg
24411 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24412 default_arg,
24413 TREE_VALUE (parm));
24414 if (parsed_arg == error_mark_node)
24416 continue;
24419 TREE_PURPOSE (parm) = parsed_arg;
24421 /* Update any instantiations we've already created. */
24422 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24423 vec_safe_iterate (insts, ix, &copy); ix++)
24424 TREE_PURPOSE (copy) = parsed_arg;
24427 pop_defarg_context ();
24429 /* Make sure no default arg is missing. */
24430 check_default_args (fn);
24432 /* Restore the state of local_variables_forbidden_p. */
24433 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24435 /* Restore the queue. */
24436 pop_unparsed_function_queues (parser);
24439 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24441 sizeof ... ( identifier )
24443 where the 'sizeof' token has already been consumed. */
24445 static tree
24446 cp_parser_sizeof_pack (cp_parser *parser)
24448 /* Consume the `...'. */
24449 cp_lexer_consume_token (parser->lexer);
24450 maybe_warn_variadic_templates ();
24452 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24453 if (paren)
24454 cp_lexer_consume_token (parser->lexer);
24455 else
24456 permerror (cp_lexer_peek_token (parser->lexer)->location,
24457 "%<sizeof...%> argument must be surrounded by parentheses");
24459 cp_token *token = cp_lexer_peek_token (parser->lexer);
24460 tree name = cp_parser_identifier (parser);
24461 if (name == error_mark_node)
24462 return error_mark_node;
24463 /* The name is not qualified. */
24464 parser->scope = NULL_TREE;
24465 parser->qualifying_scope = NULL_TREE;
24466 parser->object_scope = NULL_TREE;
24467 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24468 if (expr == error_mark_node)
24469 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24470 token->location);
24471 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
24472 expr = TREE_TYPE (expr);
24473 else if (TREE_CODE (expr) == CONST_DECL)
24474 expr = DECL_INITIAL (expr);
24475 expr = make_pack_expansion (expr);
24477 if (paren)
24478 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24480 return expr;
24483 /* Parse the operand of `sizeof' (or a similar operator). Returns
24484 either a TYPE or an expression, depending on the form of the
24485 input. The KEYWORD indicates which kind of expression we have
24486 encountered. */
24488 static tree
24489 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24491 tree expr = NULL_TREE;
24492 const char *saved_message;
24493 char *tmp;
24494 bool saved_integral_constant_expression_p;
24495 bool saved_non_integral_constant_expression_p;
24497 /* If it's a `...', then we are computing the length of a parameter
24498 pack. */
24499 if (keyword == RID_SIZEOF
24500 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24501 return cp_parser_sizeof_pack (parser);
24503 /* Types cannot be defined in a `sizeof' expression. Save away the
24504 old message. */
24505 saved_message = parser->type_definition_forbidden_message;
24506 /* And create the new one. */
24507 tmp = concat ("types may not be defined in %<",
24508 IDENTIFIER_POINTER (ridpointers[keyword]),
24509 "%> expressions", NULL);
24510 parser->type_definition_forbidden_message = tmp;
24512 /* The restrictions on constant-expressions do not apply inside
24513 sizeof expressions. */
24514 saved_integral_constant_expression_p
24515 = parser->integral_constant_expression_p;
24516 saved_non_integral_constant_expression_p
24517 = parser->non_integral_constant_expression_p;
24518 parser->integral_constant_expression_p = false;
24520 /* Do not actually evaluate the expression. */
24521 ++cp_unevaluated_operand;
24522 ++c_inhibit_evaluation_warnings;
24523 /* If it's a `(', then we might be looking at the type-id
24524 construction. */
24525 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24527 tree type = NULL_TREE;
24529 /* We can't be sure yet whether we're looking at a type-id or an
24530 expression. */
24531 cp_parser_parse_tentatively (parser);
24532 /* Note: as a GNU Extension, compound literals are considered
24533 postfix-expressions as they are in C99, so they are valid
24534 arguments to sizeof. See comment in cp_parser_cast_expression
24535 for details. */
24536 if (cp_parser_compound_literal_p (parser))
24537 cp_parser_simulate_error (parser);
24538 else
24540 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24541 parser->in_type_id_in_expr_p = true;
24542 /* Look for the type-id. */
24543 type = cp_parser_type_id (parser);
24544 /* Look for the closing `)'. */
24545 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24546 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24549 /* If all went well, then we're done. */
24550 if (cp_parser_parse_definitely (parser))
24552 cp_decl_specifier_seq decl_specs;
24554 /* Build a trivial decl-specifier-seq. */
24555 clear_decl_specs (&decl_specs);
24556 decl_specs.type = type;
24558 /* Call grokdeclarator to figure out what type this is. */
24559 expr = grokdeclarator (NULL,
24560 &decl_specs,
24561 TYPENAME,
24562 /*initialized=*/0,
24563 /*attrlist=*/NULL);
24567 /* If the type-id production did not work out, then we must be
24568 looking at the unary-expression production. */
24569 if (!expr)
24570 expr = cp_parser_unary_expression (parser);
24572 /* Go back to evaluating expressions. */
24573 --cp_unevaluated_operand;
24574 --c_inhibit_evaluation_warnings;
24576 /* Free the message we created. */
24577 free (tmp);
24578 /* And restore the old one. */
24579 parser->type_definition_forbidden_message = saved_message;
24580 parser->integral_constant_expression_p
24581 = saved_integral_constant_expression_p;
24582 parser->non_integral_constant_expression_p
24583 = saved_non_integral_constant_expression_p;
24585 return expr;
24588 /* If the current declaration has no declarator, return true. */
24590 static bool
24591 cp_parser_declares_only_class_p (cp_parser *parser)
24593 /* If the next token is a `;' or a `,' then there is no
24594 declarator. */
24595 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24596 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24599 /* Update the DECL_SPECS to reflect the storage class indicated by
24600 KEYWORD. */
24602 static void
24603 cp_parser_set_storage_class (cp_parser *parser,
24604 cp_decl_specifier_seq *decl_specs,
24605 enum rid keyword,
24606 cp_token *token)
24608 cp_storage_class storage_class;
24610 if (parser->in_unbraced_linkage_specification_p)
24612 error_at (token->location, "invalid use of %qD in linkage specification",
24613 ridpointers[keyword]);
24614 return;
24616 else if (decl_specs->storage_class != sc_none)
24618 decl_specs->conflicting_specifiers_p = true;
24619 return;
24622 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24623 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24624 && decl_specs->gnu_thread_keyword_p)
24626 pedwarn (decl_specs->locations[ds_thread], 0,
24627 "%<__thread%> before %qD", ridpointers[keyword]);
24630 switch (keyword)
24632 case RID_AUTO:
24633 storage_class = sc_auto;
24634 break;
24635 case RID_REGISTER:
24636 storage_class = sc_register;
24637 break;
24638 case RID_STATIC:
24639 storage_class = sc_static;
24640 break;
24641 case RID_EXTERN:
24642 storage_class = sc_extern;
24643 break;
24644 case RID_MUTABLE:
24645 storage_class = sc_mutable;
24646 break;
24647 default:
24648 gcc_unreachable ();
24650 decl_specs->storage_class = storage_class;
24651 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24653 /* A storage class specifier cannot be applied alongside a typedef
24654 specifier. If there is a typedef specifier present then set
24655 conflicting_specifiers_p which will trigger an error later
24656 on in grokdeclarator. */
24657 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24658 decl_specs->conflicting_specifiers_p = true;
24661 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24662 is true, the type is a class or enum definition. */
24664 static void
24665 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24666 tree type_spec,
24667 cp_token *token,
24668 bool type_definition_p)
24670 decl_specs->any_specifiers_p = true;
24672 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24673 (with, for example, in "typedef int wchar_t;") we remember that
24674 this is what happened. In system headers, we ignore these
24675 declarations so that G++ can work with system headers that are not
24676 C++-safe. */
24677 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24678 && !type_definition_p
24679 && (type_spec == boolean_type_node
24680 || type_spec == char16_type_node
24681 || type_spec == char32_type_node
24682 || type_spec == wchar_type_node)
24683 && (decl_specs->type
24684 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24685 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24686 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24687 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24689 decl_specs->redefined_builtin_type = type_spec;
24690 set_and_check_decl_spec_loc (decl_specs,
24691 ds_redefined_builtin_type_spec,
24692 token);
24693 if (!decl_specs->type)
24695 decl_specs->type = type_spec;
24696 decl_specs->type_definition_p = false;
24697 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24700 else if (decl_specs->type)
24701 decl_specs->multiple_types_p = true;
24702 else
24704 decl_specs->type = type_spec;
24705 decl_specs->type_definition_p = type_definition_p;
24706 decl_specs->redefined_builtin_type = NULL_TREE;
24707 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24711 /* True iff TOKEN is the GNU keyword __thread. */
24713 static bool
24714 token_is__thread (cp_token *token)
24716 gcc_assert (token->keyword == RID_THREAD);
24717 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24720 /* Set the location for a declarator specifier and check if it is
24721 duplicated.
24723 DECL_SPECS is the sequence of declarator specifiers onto which to
24724 set the location.
24726 DS is the single declarator specifier to set which location is to
24727 be set onto the existing sequence of declarators.
24729 LOCATION is the location for the declarator specifier to
24730 consider. */
24732 static void
24733 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24734 cp_decl_spec ds, cp_token *token)
24736 gcc_assert (ds < ds_last);
24738 if (decl_specs == NULL)
24739 return;
24741 source_location location = token->location;
24743 if (decl_specs->locations[ds] == 0)
24745 decl_specs->locations[ds] = location;
24746 if (ds == ds_thread)
24747 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24749 else
24751 if (ds == ds_long)
24753 if (decl_specs->locations[ds_long_long] != 0)
24754 error_at (location,
24755 "%<long long long%> is too long for GCC");
24756 else
24758 decl_specs->locations[ds_long_long] = location;
24759 pedwarn_cxx98 (location,
24760 OPT_Wlong_long,
24761 "ISO C++ 1998 does not support %<long long%>");
24764 else if (ds == ds_thread)
24766 bool gnu = token_is__thread (token);
24767 if (gnu != decl_specs->gnu_thread_keyword_p)
24768 error_at (location,
24769 "both %<__thread%> and %<thread_local%> specified");
24770 else
24771 error_at (location, "duplicate %qD", token->u.value);
24773 else
24775 static const char *const decl_spec_names[] = {
24776 "signed",
24777 "unsigned",
24778 "short",
24779 "long",
24780 "const",
24781 "volatile",
24782 "restrict",
24783 "inline",
24784 "virtual",
24785 "explicit",
24786 "friend",
24787 "typedef",
24788 "using",
24789 "constexpr",
24790 "__complex"
24792 error_at (location,
24793 "duplicate %qs", decl_spec_names[ds]);
24798 /* Return true iff the declarator specifier DS is present in the
24799 sequence of declarator specifiers DECL_SPECS. */
24801 bool
24802 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24803 cp_decl_spec ds)
24805 gcc_assert (ds < ds_last);
24807 if (decl_specs == NULL)
24808 return false;
24810 return decl_specs->locations[ds] != 0;
24813 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24814 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24816 static bool
24817 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24819 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24822 /* Issue an error message indicating that TOKEN_DESC was expected.
24823 If KEYWORD is true, it indicated this function is called by
24824 cp_parser_require_keword and the required token can only be
24825 a indicated keyword. */
24827 static void
24828 cp_parser_required_error (cp_parser *parser,
24829 required_token token_desc,
24830 bool keyword)
24832 switch (token_desc)
24834 case RT_NEW:
24835 cp_parser_error (parser, "expected %<new%>");
24836 return;
24837 case RT_DELETE:
24838 cp_parser_error (parser, "expected %<delete%>");
24839 return;
24840 case RT_RETURN:
24841 cp_parser_error (parser, "expected %<return%>");
24842 return;
24843 case RT_WHILE:
24844 cp_parser_error (parser, "expected %<while%>");
24845 return;
24846 case RT_EXTERN:
24847 cp_parser_error (parser, "expected %<extern%>");
24848 return;
24849 case RT_STATIC_ASSERT:
24850 cp_parser_error (parser, "expected %<static_assert%>");
24851 return;
24852 case RT_DECLTYPE:
24853 cp_parser_error (parser, "expected %<decltype%>");
24854 return;
24855 case RT_OPERATOR:
24856 cp_parser_error (parser, "expected %<operator%>");
24857 return;
24858 case RT_CLASS:
24859 cp_parser_error (parser, "expected %<class%>");
24860 return;
24861 case RT_TEMPLATE:
24862 cp_parser_error (parser, "expected %<template%>");
24863 return;
24864 case RT_NAMESPACE:
24865 cp_parser_error (parser, "expected %<namespace%>");
24866 return;
24867 case RT_USING:
24868 cp_parser_error (parser, "expected %<using%>");
24869 return;
24870 case RT_ASM:
24871 cp_parser_error (parser, "expected %<asm%>");
24872 return;
24873 case RT_TRY:
24874 cp_parser_error (parser, "expected %<try%>");
24875 return;
24876 case RT_CATCH:
24877 cp_parser_error (parser, "expected %<catch%>");
24878 return;
24879 case RT_THROW:
24880 cp_parser_error (parser, "expected %<throw%>");
24881 return;
24882 case RT_LABEL:
24883 cp_parser_error (parser, "expected %<__label__%>");
24884 return;
24885 case RT_AT_TRY:
24886 cp_parser_error (parser, "expected %<@try%>");
24887 return;
24888 case RT_AT_SYNCHRONIZED:
24889 cp_parser_error (parser, "expected %<@synchronized%>");
24890 return;
24891 case RT_AT_THROW:
24892 cp_parser_error (parser, "expected %<@throw%>");
24893 return;
24894 case RT_TRANSACTION_ATOMIC:
24895 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24896 return;
24897 case RT_TRANSACTION_RELAXED:
24898 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24899 return;
24900 default:
24901 break;
24903 if (!keyword)
24905 switch (token_desc)
24907 case RT_SEMICOLON:
24908 cp_parser_error (parser, "expected %<;%>");
24909 return;
24910 case RT_OPEN_PAREN:
24911 cp_parser_error (parser, "expected %<(%>");
24912 return;
24913 case RT_CLOSE_BRACE:
24914 cp_parser_error (parser, "expected %<}%>");
24915 return;
24916 case RT_OPEN_BRACE:
24917 cp_parser_error (parser, "expected %<{%>");
24918 return;
24919 case RT_CLOSE_SQUARE:
24920 cp_parser_error (parser, "expected %<]%>");
24921 return;
24922 case RT_OPEN_SQUARE:
24923 cp_parser_error (parser, "expected %<[%>");
24924 return;
24925 case RT_COMMA:
24926 cp_parser_error (parser, "expected %<,%>");
24927 return;
24928 case RT_SCOPE:
24929 cp_parser_error (parser, "expected %<::%>");
24930 return;
24931 case RT_LESS:
24932 cp_parser_error (parser, "expected %<<%>");
24933 return;
24934 case RT_GREATER:
24935 cp_parser_error (parser, "expected %<>%>");
24936 return;
24937 case RT_EQ:
24938 cp_parser_error (parser, "expected %<=%>");
24939 return;
24940 case RT_ELLIPSIS:
24941 cp_parser_error (parser, "expected %<...%>");
24942 return;
24943 case RT_MULT:
24944 cp_parser_error (parser, "expected %<*%>");
24945 return;
24946 case RT_COMPL:
24947 cp_parser_error (parser, "expected %<~%>");
24948 return;
24949 case RT_COLON:
24950 cp_parser_error (parser, "expected %<:%>");
24951 return;
24952 case RT_COLON_SCOPE:
24953 cp_parser_error (parser, "expected %<:%> or %<::%>");
24954 return;
24955 case RT_CLOSE_PAREN:
24956 cp_parser_error (parser, "expected %<)%>");
24957 return;
24958 case RT_COMMA_CLOSE_PAREN:
24959 cp_parser_error (parser, "expected %<,%> or %<)%>");
24960 return;
24961 case RT_PRAGMA_EOL:
24962 cp_parser_error (parser, "expected end of line");
24963 return;
24964 case RT_NAME:
24965 cp_parser_error (parser, "expected identifier");
24966 return;
24967 case RT_SELECT:
24968 cp_parser_error (parser, "expected selection-statement");
24969 return;
24970 case RT_INTERATION:
24971 cp_parser_error (parser, "expected iteration-statement");
24972 return;
24973 case RT_JUMP:
24974 cp_parser_error (parser, "expected jump-statement");
24975 return;
24976 case RT_CLASS_KEY:
24977 cp_parser_error (parser, "expected class-key");
24978 return;
24979 case RT_CLASS_TYPENAME_TEMPLATE:
24980 cp_parser_error (parser,
24981 "expected %<class%>, %<typename%>, or %<template%>");
24982 return;
24983 default:
24984 gcc_unreachable ();
24987 else
24988 gcc_unreachable ();
24993 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24994 issue an error message indicating that TOKEN_DESC was expected.
24996 Returns the token consumed, if the token had the appropriate type.
24997 Otherwise, returns NULL. */
24999 static cp_token *
25000 cp_parser_require (cp_parser* parser,
25001 enum cpp_ttype type,
25002 required_token token_desc)
25004 if (cp_lexer_next_token_is (parser->lexer, type))
25005 return cp_lexer_consume_token (parser->lexer);
25006 else
25008 /* Output the MESSAGE -- unless we're parsing tentatively. */
25009 if (!cp_parser_simulate_error (parser))
25010 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
25011 return NULL;
25015 /* An error message is produced if the next token is not '>'.
25016 All further tokens are skipped until the desired token is
25017 found or '{', '}', ';' or an unbalanced ')' or ']'. */
25019 static void
25020 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
25022 /* Current level of '< ... >'. */
25023 unsigned level = 0;
25024 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
25025 unsigned nesting_depth = 0;
25027 /* Are we ready, yet? If not, issue error message. */
25028 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
25029 return;
25031 /* Skip tokens until the desired token is found. */
25032 while (true)
25034 /* Peek at the next token. */
25035 switch (cp_lexer_peek_token (parser->lexer)->type)
25037 case CPP_LESS:
25038 if (!nesting_depth)
25039 ++level;
25040 break;
25042 case CPP_RSHIFT:
25043 if (cxx_dialect == cxx98)
25044 /* C++0x views the `>>' operator as two `>' tokens, but
25045 C++98 does not. */
25046 break;
25047 else if (!nesting_depth && level-- == 0)
25049 /* We've hit a `>>' where the first `>' closes the
25050 template argument list, and the second `>' is
25051 spurious. Just consume the `>>' and stop; we've
25052 already produced at least one error. */
25053 cp_lexer_consume_token (parser->lexer);
25054 return;
25056 /* Fall through for C++0x, so we handle the second `>' in
25057 the `>>'. */
25059 case CPP_GREATER:
25060 if (!nesting_depth && level-- == 0)
25062 /* We've reached the token we want, consume it and stop. */
25063 cp_lexer_consume_token (parser->lexer);
25064 return;
25066 break;
25068 case CPP_OPEN_PAREN:
25069 case CPP_OPEN_SQUARE:
25070 ++nesting_depth;
25071 break;
25073 case CPP_CLOSE_PAREN:
25074 case CPP_CLOSE_SQUARE:
25075 if (nesting_depth-- == 0)
25076 return;
25077 break;
25079 case CPP_EOF:
25080 case CPP_PRAGMA_EOL:
25081 case CPP_SEMICOLON:
25082 case CPP_OPEN_BRACE:
25083 case CPP_CLOSE_BRACE:
25084 /* The '>' was probably forgotten, don't look further. */
25085 return;
25087 default:
25088 break;
25091 /* Consume this token. */
25092 cp_lexer_consume_token (parser->lexer);
25096 /* If the next token is the indicated keyword, consume it. Otherwise,
25097 issue an error message indicating that TOKEN_DESC was expected.
25099 Returns the token consumed, if the token had the appropriate type.
25100 Otherwise, returns NULL. */
25102 static cp_token *
25103 cp_parser_require_keyword (cp_parser* parser,
25104 enum rid keyword,
25105 required_token token_desc)
25107 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
25109 if (token && token->keyword != keyword)
25111 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
25112 return NULL;
25115 return token;
25118 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25119 function-definition. */
25121 static bool
25122 cp_parser_token_starts_function_definition_p (cp_token* token)
25124 return (/* An ordinary function-body begins with an `{'. */
25125 token->type == CPP_OPEN_BRACE
25126 /* A ctor-initializer begins with a `:'. */
25127 || token->type == CPP_COLON
25128 /* A function-try-block begins with `try'. */
25129 || token->keyword == RID_TRY
25130 /* A function-transaction-block begins with `__transaction_atomic'
25131 or `__transaction_relaxed'. */
25132 || token->keyword == RID_TRANSACTION_ATOMIC
25133 || token->keyword == RID_TRANSACTION_RELAXED
25134 /* The named return value extension begins with `return'. */
25135 || token->keyword == RID_RETURN);
25138 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25139 definition. */
25141 static bool
25142 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25144 cp_token *token;
25146 token = cp_lexer_peek_token (parser->lexer);
25147 return (token->type == CPP_OPEN_BRACE
25148 || (token->type == CPP_COLON
25149 && !parser->colon_doesnt_start_class_def_p));
25152 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25153 C++0x) ending a template-argument. */
25155 static bool
25156 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25158 cp_token *token;
25160 token = cp_lexer_peek_token (parser->lexer);
25161 return (token->type == CPP_COMMA
25162 || token->type == CPP_GREATER
25163 || token->type == CPP_ELLIPSIS
25164 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25167 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25168 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25170 static bool
25171 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25172 size_t n)
25174 cp_token *token;
25176 token = cp_lexer_peek_nth_token (parser->lexer, n);
25177 if (token->type == CPP_LESS)
25178 return true;
25179 /* Check for the sequence `<::' in the original code. It would be lexed as
25180 `[:', where `[' is a digraph, and there is no whitespace before
25181 `:'. */
25182 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25184 cp_token *token2;
25185 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25186 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25187 return true;
25189 return false;
25192 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25193 or none_type otherwise. */
25195 static enum tag_types
25196 cp_parser_token_is_class_key (cp_token* token)
25198 switch (token->keyword)
25200 case RID_CLASS:
25201 return class_type;
25202 case RID_STRUCT:
25203 return record_type;
25204 case RID_UNION:
25205 return union_type;
25207 default:
25208 return none_type;
25212 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25213 or none_type otherwise or if the token is null. */
25215 static enum tag_types
25216 cp_parser_token_is_type_parameter_key (cp_token* token)
25218 if (!token)
25219 return none_type;
25221 switch (token->keyword)
25223 case RID_CLASS:
25224 return class_type;
25225 case RID_TYPENAME:
25226 return typename_type;
25228 default:
25229 return none_type;
25233 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25235 static void
25236 cp_parser_check_class_key (enum tag_types class_key, tree type)
25238 if (type == error_mark_node)
25239 return;
25240 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25242 if (permerror (input_location, "%qs tag used in naming %q#T",
25243 class_key == union_type ? "union"
25244 : class_key == record_type ? "struct" : "class",
25245 type))
25246 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25247 "%q#T was previously declared here", type);
25251 /* Issue an error message if DECL is redeclared with different
25252 access than its original declaration [class.access.spec/3].
25253 This applies to nested classes and nested class templates.
25254 [class.mem/1]. */
25256 static void
25257 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25259 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25260 return;
25262 if ((TREE_PRIVATE (decl)
25263 != (current_access_specifier == access_private_node))
25264 || (TREE_PROTECTED (decl)
25265 != (current_access_specifier == access_protected_node)))
25266 error_at (location, "%qD redeclared with different access", decl);
25269 /* Look for the `template' keyword, as a syntactic disambiguator.
25270 Return TRUE iff it is present, in which case it will be
25271 consumed. */
25273 static bool
25274 cp_parser_optional_template_keyword (cp_parser *parser)
25276 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25278 /* In C++98 the `template' keyword can only be used within templates;
25279 outside templates the parser can always figure out what is a
25280 template and what is not. In C++11, per the resolution of DR 468,
25281 `template' is allowed in cases where it is not strictly necessary. */
25282 if (!processing_template_decl
25283 && pedantic && cxx_dialect == cxx98)
25285 cp_token *token = cp_lexer_peek_token (parser->lexer);
25286 pedwarn (token->location, OPT_Wpedantic,
25287 "in C++98 %<template%> (as a disambiguator) is only "
25288 "allowed within templates");
25289 /* If this part of the token stream is rescanned, the same
25290 error message would be generated. So, we purge the token
25291 from the stream. */
25292 cp_lexer_purge_token (parser->lexer);
25293 return false;
25295 else
25297 /* Consume the `template' keyword. */
25298 cp_lexer_consume_token (parser->lexer);
25299 return true;
25302 return false;
25305 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25306 set PARSER->SCOPE, and perform other related actions. */
25308 static void
25309 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25311 int i;
25312 struct tree_check *check_value;
25313 deferred_access_check *chk;
25314 vec<deferred_access_check, va_gc> *checks;
25316 /* Get the stored value. */
25317 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25318 /* Perform any access checks that were deferred. */
25319 checks = check_value->checks;
25320 if (checks)
25322 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25323 perform_or_defer_access_check (chk->binfo,
25324 chk->decl,
25325 chk->diag_decl, tf_warning_or_error);
25327 /* Set the scope from the stored value. */
25328 parser->scope = check_value->value;
25329 parser->qualifying_scope = check_value->qualifying_scope;
25330 parser->object_scope = NULL_TREE;
25333 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25334 encounter the end of a block before what we were looking for. */
25336 static bool
25337 cp_parser_cache_group (cp_parser *parser,
25338 enum cpp_ttype end,
25339 unsigned depth)
25341 while (true)
25343 cp_token *token = cp_lexer_peek_token (parser->lexer);
25345 /* Abort a parenthesized expression if we encounter a semicolon. */
25346 if ((end == CPP_CLOSE_PAREN || depth == 0)
25347 && token->type == CPP_SEMICOLON)
25348 return true;
25349 /* If we've reached the end of the file, stop. */
25350 if (token->type == CPP_EOF
25351 || (end != CPP_PRAGMA_EOL
25352 && token->type == CPP_PRAGMA_EOL))
25353 return true;
25354 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25355 /* We've hit the end of an enclosing block, so there's been some
25356 kind of syntax error. */
25357 return true;
25359 /* Consume the token. */
25360 cp_lexer_consume_token (parser->lexer);
25361 /* See if it starts a new group. */
25362 if (token->type == CPP_OPEN_BRACE)
25364 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25365 /* In theory this should probably check end == '}', but
25366 cp_parser_save_member_function_body needs it to exit
25367 after either '}' or ')' when called with ')'. */
25368 if (depth == 0)
25369 return false;
25371 else if (token->type == CPP_OPEN_PAREN)
25373 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25374 if (depth == 0 && end == CPP_CLOSE_PAREN)
25375 return false;
25377 else if (token->type == CPP_PRAGMA)
25378 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25379 else if (token->type == end)
25380 return false;
25384 /* Like above, for caching a default argument or NSDMI. Both of these are
25385 terminated by a non-nested comma, but it can be unclear whether or not a
25386 comma is nested in a template argument list unless we do more parsing.
25387 In order to handle this ambiguity, when we encounter a ',' after a '<'
25388 we try to parse what follows as a parameter-declaration-list (in the
25389 case of a default argument) or a member-declarator (in the case of an
25390 NSDMI). If that succeeds, then we stop caching. */
25392 static tree
25393 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25395 unsigned depth = 0;
25396 int maybe_template_id = 0;
25397 cp_token *first_token;
25398 cp_token *token;
25399 tree default_argument;
25401 /* Add tokens until we have processed the entire default
25402 argument. We add the range [first_token, token). */
25403 first_token = cp_lexer_peek_token (parser->lexer);
25404 if (first_token->type == CPP_OPEN_BRACE)
25406 /* For list-initialization, this is straightforward. */
25407 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25408 token = cp_lexer_peek_token (parser->lexer);
25410 else while (true)
25412 bool done = false;
25414 /* Peek at the next token. */
25415 token = cp_lexer_peek_token (parser->lexer);
25416 /* What we do depends on what token we have. */
25417 switch (token->type)
25419 /* In valid code, a default argument must be
25420 immediately followed by a `,' `)', or `...'. */
25421 case CPP_COMMA:
25422 if (depth == 0 && maybe_template_id)
25424 /* If we've seen a '<', we might be in a
25425 template-argument-list. Until Core issue 325 is
25426 resolved, we don't know how this situation ought
25427 to be handled, so try to DTRT. We check whether
25428 what comes after the comma is a valid parameter
25429 declaration list. If it is, then the comma ends
25430 the default argument; otherwise the default
25431 argument continues. */
25432 bool error = false;
25433 cp_token *peek;
25435 /* Set ITALP so cp_parser_parameter_declaration_list
25436 doesn't decide to commit to this parse. */
25437 bool saved_italp = parser->in_template_argument_list_p;
25438 parser->in_template_argument_list_p = true;
25440 cp_parser_parse_tentatively (parser);
25442 if (nsdmi)
25444 /* Parse declarators until we reach a non-comma or
25445 somthing that cannot be an initializer.
25446 Just checking whether we're looking at a single
25447 declarator is insufficient. Consider:
25448 int var = tuple<T,U>::x;
25449 The template parameter 'U' looks exactly like a
25450 declarator. */
25453 int ctor_dtor_or_conv_p;
25454 cp_lexer_consume_token (parser->lexer);
25455 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25456 &ctor_dtor_or_conv_p,
25457 /*parenthesized_p=*/NULL,
25458 /*member_p=*/true,
25459 /*friend_p=*/false);
25460 peek = cp_lexer_peek_token (parser->lexer);
25461 if (cp_parser_error_occurred (parser))
25462 break;
25464 while (peek->type == CPP_COMMA);
25465 /* If we met an '=' or ';' then the original comma
25466 was the end of the NSDMI. Otherwise assume
25467 we're still in the NSDMI. */
25468 error = (peek->type != CPP_EQ
25469 && peek->type != CPP_SEMICOLON);
25471 else
25473 cp_lexer_consume_token (parser->lexer);
25474 begin_scope (sk_function_parms, NULL_TREE);
25475 cp_parser_parameter_declaration_list (parser, &error);
25476 pop_bindings_and_leave_scope ();
25478 if (!cp_parser_error_occurred (parser) && !error)
25479 done = true;
25480 cp_parser_abort_tentative_parse (parser);
25482 parser->in_template_argument_list_p = saved_italp;
25483 break;
25485 case CPP_CLOSE_PAREN:
25486 case CPP_ELLIPSIS:
25487 /* If we run into a non-nested `;', `}', or `]',
25488 then the code is invalid -- but the default
25489 argument is certainly over. */
25490 case CPP_SEMICOLON:
25491 case CPP_CLOSE_BRACE:
25492 case CPP_CLOSE_SQUARE:
25493 if (depth == 0
25494 /* Handle correctly int n = sizeof ... ( p ); */
25495 && token->type != CPP_ELLIPSIS)
25496 done = true;
25497 /* Update DEPTH, if necessary. */
25498 else if (token->type == CPP_CLOSE_PAREN
25499 || token->type == CPP_CLOSE_BRACE
25500 || token->type == CPP_CLOSE_SQUARE)
25501 --depth;
25502 break;
25504 case CPP_OPEN_PAREN:
25505 case CPP_OPEN_SQUARE:
25506 case CPP_OPEN_BRACE:
25507 ++depth;
25508 break;
25510 case CPP_LESS:
25511 if (depth == 0)
25512 /* This might be the comparison operator, or it might
25513 start a template argument list. */
25514 ++maybe_template_id;
25515 break;
25517 case CPP_RSHIFT:
25518 if (cxx_dialect == cxx98)
25519 break;
25520 /* Fall through for C++0x, which treats the `>>'
25521 operator like two `>' tokens in certain
25522 cases. */
25524 case CPP_GREATER:
25525 if (depth == 0)
25527 /* This might be an operator, or it might close a
25528 template argument list. But if a previous '<'
25529 started a template argument list, this will have
25530 closed it, so we can't be in one anymore. */
25531 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25532 if (maybe_template_id < 0)
25533 maybe_template_id = 0;
25535 break;
25537 /* If we run out of tokens, issue an error message. */
25538 case CPP_EOF:
25539 case CPP_PRAGMA_EOL:
25540 error_at (token->location, "file ends in default argument");
25541 done = true;
25542 break;
25544 case CPP_NAME:
25545 case CPP_SCOPE:
25546 /* In these cases, we should look for template-ids.
25547 For example, if the default argument is
25548 `X<int, double>()', we need to do name lookup to
25549 figure out whether or not `X' is a template; if
25550 so, the `,' does not end the default argument.
25552 That is not yet done. */
25553 break;
25555 default:
25556 break;
25559 /* If we've reached the end, stop. */
25560 if (done)
25561 break;
25563 /* Add the token to the token block. */
25564 token = cp_lexer_consume_token (parser->lexer);
25567 /* Create a DEFAULT_ARG to represent the unparsed default
25568 argument. */
25569 default_argument = make_node (DEFAULT_ARG);
25570 DEFARG_TOKENS (default_argument)
25571 = cp_token_cache_new (first_token, token);
25572 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25574 return default_argument;
25577 /* Begin parsing tentatively. We always save tokens while parsing
25578 tentatively so that if the tentative parsing fails we can restore the
25579 tokens. */
25581 static void
25582 cp_parser_parse_tentatively (cp_parser* parser)
25584 /* Enter a new parsing context. */
25585 parser->context = cp_parser_context_new (parser->context);
25586 /* Begin saving tokens. */
25587 cp_lexer_save_tokens (parser->lexer);
25588 /* In order to avoid repetitive access control error messages,
25589 access checks are queued up until we are no longer parsing
25590 tentatively. */
25591 push_deferring_access_checks (dk_deferred);
25594 /* Commit to the currently active tentative parse. */
25596 static void
25597 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25599 cp_parser_context *context;
25600 cp_lexer *lexer;
25602 /* Mark all of the levels as committed. */
25603 lexer = parser->lexer;
25604 for (context = parser->context; context->next; context = context->next)
25606 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25607 break;
25608 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25609 while (!cp_lexer_saving_tokens (lexer))
25610 lexer = lexer->next;
25611 cp_lexer_commit_tokens (lexer);
25615 /* Commit to the topmost currently active tentative parse.
25617 Note that this function shouldn't be called when there are
25618 irreversible side-effects while in a tentative state. For
25619 example, we shouldn't create a permanent entry in the symbol
25620 table, or issue an error message that might not apply if the
25621 tentative parse is aborted. */
25623 static void
25624 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25626 cp_parser_context *context = parser->context;
25627 cp_lexer *lexer = parser->lexer;
25629 if (context)
25631 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25632 return;
25633 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25635 while (!cp_lexer_saving_tokens (lexer))
25636 lexer = lexer->next;
25637 cp_lexer_commit_tokens (lexer);
25641 /* Abort the currently active tentative parse. All consumed tokens
25642 will be rolled back, and no diagnostics will be issued. */
25644 static void
25645 cp_parser_abort_tentative_parse (cp_parser* parser)
25647 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25648 || errorcount > 0);
25649 cp_parser_simulate_error (parser);
25650 /* Now, pretend that we want to see if the construct was
25651 successfully parsed. */
25652 cp_parser_parse_definitely (parser);
25655 /* Stop parsing tentatively. If a parse error has occurred, restore the
25656 token stream. Otherwise, commit to the tokens we have consumed.
25657 Returns true if no error occurred; false otherwise. */
25659 static bool
25660 cp_parser_parse_definitely (cp_parser* parser)
25662 bool error_occurred;
25663 cp_parser_context *context;
25665 /* Remember whether or not an error occurred, since we are about to
25666 destroy that information. */
25667 error_occurred = cp_parser_error_occurred (parser);
25668 /* Remove the topmost context from the stack. */
25669 context = parser->context;
25670 parser->context = context->next;
25671 /* If no parse errors occurred, commit to the tentative parse. */
25672 if (!error_occurred)
25674 /* Commit to the tokens read tentatively, unless that was
25675 already done. */
25676 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25677 cp_lexer_commit_tokens (parser->lexer);
25679 pop_to_parent_deferring_access_checks ();
25681 /* Otherwise, if errors occurred, roll back our state so that things
25682 are just as they were before we began the tentative parse. */
25683 else
25685 cp_lexer_rollback_tokens (parser->lexer);
25686 pop_deferring_access_checks ();
25688 /* Add the context to the front of the free list. */
25689 context->next = cp_parser_context_free_list;
25690 cp_parser_context_free_list = context;
25692 return !error_occurred;
25695 /* Returns true if we are parsing tentatively and are not committed to
25696 this tentative parse. */
25698 static bool
25699 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25701 return (cp_parser_parsing_tentatively (parser)
25702 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25705 /* Returns nonzero iff an error has occurred during the most recent
25706 tentative parse. */
25708 static bool
25709 cp_parser_error_occurred (cp_parser* parser)
25711 return (cp_parser_parsing_tentatively (parser)
25712 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25715 /* Returns nonzero if GNU extensions are allowed. */
25717 static bool
25718 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25720 return parser->allow_gnu_extensions_p;
25723 /* Objective-C++ Productions */
25726 /* Parse an Objective-C expression, which feeds into a primary-expression
25727 above.
25729 objc-expression:
25730 objc-message-expression
25731 objc-string-literal
25732 objc-encode-expression
25733 objc-protocol-expression
25734 objc-selector-expression
25736 Returns a tree representation of the expression. */
25738 static tree
25739 cp_parser_objc_expression (cp_parser* parser)
25741 /* Try to figure out what kind of declaration is present. */
25742 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25744 switch (kwd->type)
25746 case CPP_OPEN_SQUARE:
25747 return cp_parser_objc_message_expression (parser);
25749 case CPP_OBJC_STRING:
25750 kwd = cp_lexer_consume_token (parser->lexer);
25751 return objc_build_string_object (kwd->u.value);
25753 case CPP_KEYWORD:
25754 switch (kwd->keyword)
25756 case RID_AT_ENCODE:
25757 return cp_parser_objc_encode_expression (parser);
25759 case RID_AT_PROTOCOL:
25760 return cp_parser_objc_protocol_expression (parser);
25762 case RID_AT_SELECTOR:
25763 return cp_parser_objc_selector_expression (parser);
25765 default:
25766 break;
25768 default:
25769 error_at (kwd->location,
25770 "misplaced %<@%D%> Objective-C++ construct",
25771 kwd->u.value);
25772 cp_parser_skip_to_end_of_block_or_statement (parser);
25775 return error_mark_node;
25778 /* Parse an Objective-C message expression.
25780 objc-message-expression:
25781 [ objc-message-receiver objc-message-args ]
25783 Returns a representation of an Objective-C message. */
25785 static tree
25786 cp_parser_objc_message_expression (cp_parser* parser)
25788 tree receiver, messageargs;
25790 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25791 receiver = cp_parser_objc_message_receiver (parser);
25792 messageargs = cp_parser_objc_message_args (parser);
25793 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25795 return objc_build_message_expr (receiver, messageargs);
25798 /* Parse an objc-message-receiver.
25800 objc-message-receiver:
25801 expression
25802 simple-type-specifier
25804 Returns a representation of the type or expression. */
25806 static tree
25807 cp_parser_objc_message_receiver (cp_parser* parser)
25809 tree rcv;
25811 /* An Objective-C message receiver may be either (1) a type
25812 or (2) an expression. */
25813 cp_parser_parse_tentatively (parser);
25814 rcv = cp_parser_expression (parser);
25816 /* If that worked out, fine. */
25817 if (cp_parser_parse_definitely (parser))
25818 return rcv;
25820 cp_parser_parse_tentatively (parser);
25821 rcv = cp_parser_simple_type_specifier (parser,
25822 /*decl_specs=*/NULL,
25823 CP_PARSER_FLAGS_NONE);
25825 if (cp_parser_parse_definitely (parser))
25826 return objc_get_class_reference (rcv);
25828 cp_parser_error (parser, "objective-c++ message receiver expected");
25829 return error_mark_node;
25832 /* Parse the arguments and selectors comprising an Objective-C message.
25834 objc-message-args:
25835 objc-selector
25836 objc-selector-args
25837 objc-selector-args , objc-comma-args
25839 objc-selector-args:
25840 objc-selector [opt] : assignment-expression
25841 objc-selector-args objc-selector [opt] : assignment-expression
25843 objc-comma-args:
25844 assignment-expression
25845 objc-comma-args , assignment-expression
25847 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25848 selector arguments and TREE_VALUE containing a list of comma
25849 arguments. */
25851 static tree
25852 cp_parser_objc_message_args (cp_parser* parser)
25854 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25855 bool maybe_unary_selector_p = true;
25856 cp_token *token = cp_lexer_peek_token (parser->lexer);
25858 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25860 tree selector = NULL_TREE, arg;
25862 if (token->type != CPP_COLON)
25863 selector = cp_parser_objc_selector (parser);
25865 /* Detect if we have a unary selector. */
25866 if (maybe_unary_selector_p
25867 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25868 return build_tree_list (selector, NULL_TREE);
25870 maybe_unary_selector_p = false;
25871 cp_parser_require (parser, CPP_COLON, RT_COLON);
25872 arg = cp_parser_assignment_expression (parser);
25874 sel_args
25875 = chainon (sel_args,
25876 build_tree_list (selector, arg));
25878 token = cp_lexer_peek_token (parser->lexer);
25881 /* Handle non-selector arguments, if any. */
25882 while (token->type == CPP_COMMA)
25884 tree arg;
25886 cp_lexer_consume_token (parser->lexer);
25887 arg = cp_parser_assignment_expression (parser);
25889 addl_args
25890 = chainon (addl_args,
25891 build_tree_list (NULL_TREE, arg));
25893 token = cp_lexer_peek_token (parser->lexer);
25896 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25898 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25899 return build_tree_list (error_mark_node, error_mark_node);
25902 return build_tree_list (sel_args, addl_args);
25905 /* Parse an Objective-C encode expression.
25907 objc-encode-expression:
25908 @encode objc-typename
25910 Returns an encoded representation of the type argument. */
25912 static tree
25913 cp_parser_objc_encode_expression (cp_parser* parser)
25915 tree type;
25916 cp_token *token;
25918 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25919 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25920 token = cp_lexer_peek_token (parser->lexer);
25921 type = complete_type (cp_parser_type_id (parser));
25922 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25924 if (!type)
25926 error_at (token->location,
25927 "%<@encode%> must specify a type as an argument");
25928 return error_mark_node;
25931 /* This happens if we find @encode(T) (where T is a template
25932 typename or something dependent on a template typename) when
25933 parsing a template. In that case, we can't compile it
25934 immediately, but we rather create an AT_ENCODE_EXPR which will
25935 need to be instantiated when the template is used.
25937 if (dependent_type_p (type))
25939 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25940 TREE_READONLY (value) = 1;
25941 return value;
25944 return objc_build_encode_expr (type);
25947 /* Parse an Objective-C @defs expression. */
25949 static tree
25950 cp_parser_objc_defs_expression (cp_parser *parser)
25952 tree name;
25954 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25955 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25956 name = cp_parser_identifier (parser);
25957 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25959 return objc_get_class_ivars (name);
25962 /* Parse an Objective-C protocol expression.
25964 objc-protocol-expression:
25965 @protocol ( identifier )
25967 Returns a representation of the protocol expression. */
25969 static tree
25970 cp_parser_objc_protocol_expression (cp_parser* parser)
25972 tree proto;
25974 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25975 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25976 proto = cp_parser_identifier (parser);
25977 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25979 return objc_build_protocol_expr (proto);
25982 /* Parse an Objective-C selector expression.
25984 objc-selector-expression:
25985 @selector ( objc-method-signature )
25987 objc-method-signature:
25988 objc-selector
25989 objc-selector-seq
25991 objc-selector-seq:
25992 objc-selector :
25993 objc-selector-seq objc-selector :
25995 Returns a representation of the method selector. */
25997 static tree
25998 cp_parser_objc_selector_expression (cp_parser* parser)
26000 tree sel_seq = NULL_TREE;
26001 bool maybe_unary_selector_p = true;
26002 cp_token *token;
26003 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26005 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
26006 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26007 token = cp_lexer_peek_token (parser->lexer);
26009 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
26010 || token->type == CPP_SCOPE)
26012 tree selector = NULL_TREE;
26014 if (token->type != CPP_COLON
26015 || token->type == CPP_SCOPE)
26016 selector = cp_parser_objc_selector (parser);
26018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
26019 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
26021 /* Detect if we have a unary selector. */
26022 if (maybe_unary_selector_p)
26024 sel_seq = selector;
26025 goto finish_selector;
26027 else
26029 cp_parser_error (parser, "expected %<:%>");
26032 maybe_unary_selector_p = false;
26033 token = cp_lexer_consume_token (parser->lexer);
26035 if (token->type == CPP_SCOPE)
26037 sel_seq
26038 = chainon (sel_seq,
26039 build_tree_list (selector, NULL_TREE));
26040 sel_seq
26041 = chainon (sel_seq,
26042 build_tree_list (NULL_TREE, NULL_TREE));
26044 else
26045 sel_seq
26046 = chainon (sel_seq,
26047 build_tree_list (selector, NULL_TREE));
26049 token = cp_lexer_peek_token (parser->lexer);
26052 finish_selector:
26053 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26055 return objc_build_selector_expr (loc, sel_seq);
26058 /* Parse a list of identifiers.
26060 objc-identifier-list:
26061 identifier
26062 objc-identifier-list , identifier
26064 Returns a TREE_LIST of identifier nodes. */
26066 static tree
26067 cp_parser_objc_identifier_list (cp_parser* parser)
26069 tree identifier;
26070 tree list;
26071 cp_token *sep;
26073 identifier = cp_parser_identifier (parser);
26074 if (identifier == error_mark_node)
26075 return error_mark_node;
26077 list = build_tree_list (NULL_TREE, identifier);
26078 sep = cp_lexer_peek_token (parser->lexer);
26080 while (sep->type == CPP_COMMA)
26082 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26083 identifier = cp_parser_identifier (parser);
26084 if (identifier == error_mark_node)
26085 return list;
26087 list = chainon (list, build_tree_list (NULL_TREE,
26088 identifier));
26089 sep = cp_lexer_peek_token (parser->lexer);
26092 return list;
26095 /* Parse an Objective-C alias declaration.
26097 objc-alias-declaration:
26098 @compatibility_alias identifier identifier ;
26100 This function registers the alias mapping with the Objective-C front end.
26101 It returns nothing. */
26103 static void
26104 cp_parser_objc_alias_declaration (cp_parser* parser)
26106 tree alias, orig;
26108 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
26109 alias = cp_parser_identifier (parser);
26110 orig = cp_parser_identifier (parser);
26111 objc_declare_alias (alias, orig);
26112 cp_parser_consume_semicolon_at_end_of_statement (parser);
26115 /* Parse an Objective-C class forward-declaration.
26117 objc-class-declaration:
26118 @class objc-identifier-list ;
26120 The function registers the forward declarations with the Objective-C
26121 front end. It returns nothing. */
26123 static void
26124 cp_parser_objc_class_declaration (cp_parser* parser)
26126 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
26127 while (true)
26129 tree id;
26131 id = cp_parser_identifier (parser);
26132 if (id == error_mark_node)
26133 break;
26135 objc_declare_class (id);
26137 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26138 cp_lexer_consume_token (parser->lexer);
26139 else
26140 break;
26142 cp_parser_consume_semicolon_at_end_of_statement (parser);
26145 /* Parse a list of Objective-C protocol references.
26147 objc-protocol-refs-opt:
26148 objc-protocol-refs [opt]
26150 objc-protocol-refs:
26151 < objc-identifier-list >
26153 Returns a TREE_LIST of identifiers, if any. */
26155 static tree
26156 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26158 tree protorefs = NULL_TREE;
26160 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26162 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
26163 protorefs = cp_parser_objc_identifier_list (parser);
26164 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26167 return protorefs;
26170 /* Parse a Objective-C visibility specification. */
26172 static void
26173 cp_parser_objc_visibility_spec (cp_parser* parser)
26175 cp_token *vis = cp_lexer_peek_token (parser->lexer);
26177 switch (vis->keyword)
26179 case RID_AT_PRIVATE:
26180 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26181 break;
26182 case RID_AT_PROTECTED:
26183 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26184 break;
26185 case RID_AT_PUBLIC:
26186 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26187 break;
26188 case RID_AT_PACKAGE:
26189 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26190 break;
26191 default:
26192 return;
26195 /* Eat '@private'/'@protected'/'@public'. */
26196 cp_lexer_consume_token (parser->lexer);
26199 /* Parse an Objective-C method type. Return 'true' if it is a class
26200 (+) method, and 'false' if it is an instance (-) method. */
26202 static inline bool
26203 cp_parser_objc_method_type (cp_parser* parser)
26205 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26206 return true;
26207 else
26208 return false;
26211 /* Parse an Objective-C protocol qualifier. */
26213 static tree
26214 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26216 tree quals = NULL_TREE, node;
26217 cp_token *token = cp_lexer_peek_token (parser->lexer);
26219 node = token->u.value;
26221 while (node && identifier_p (node)
26222 && (node == ridpointers [(int) RID_IN]
26223 || node == ridpointers [(int) RID_OUT]
26224 || node == ridpointers [(int) RID_INOUT]
26225 || node == ridpointers [(int) RID_BYCOPY]
26226 || node == ridpointers [(int) RID_BYREF]
26227 || node == ridpointers [(int) RID_ONEWAY]))
26229 quals = tree_cons (NULL_TREE, node, quals);
26230 cp_lexer_consume_token (parser->lexer);
26231 token = cp_lexer_peek_token (parser->lexer);
26232 node = token->u.value;
26235 return quals;
26238 /* Parse an Objective-C typename. */
26240 static tree
26241 cp_parser_objc_typename (cp_parser* parser)
26243 tree type_name = NULL_TREE;
26245 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26247 tree proto_quals, cp_type = NULL_TREE;
26249 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26250 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26252 /* An ObjC type name may consist of just protocol qualifiers, in which
26253 case the type shall default to 'id'. */
26254 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26256 cp_type = cp_parser_type_id (parser);
26258 /* If the type could not be parsed, an error has already
26259 been produced. For error recovery, behave as if it had
26260 not been specified, which will use the default type
26261 'id'. */
26262 if (cp_type == error_mark_node)
26264 cp_type = NULL_TREE;
26265 /* We need to skip to the closing parenthesis as
26266 cp_parser_type_id() does not seem to do it for
26267 us. */
26268 cp_parser_skip_to_closing_parenthesis (parser,
26269 /*recovering=*/true,
26270 /*or_comma=*/false,
26271 /*consume_paren=*/false);
26275 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26276 type_name = build_tree_list (proto_quals, cp_type);
26279 return type_name;
26282 /* Check to see if TYPE refers to an Objective-C selector name. */
26284 static bool
26285 cp_parser_objc_selector_p (enum cpp_ttype type)
26287 return (type == CPP_NAME || type == CPP_KEYWORD
26288 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26289 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26290 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26291 || type == CPP_XOR || type == CPP_XOR_EQ);
26294 /* Parse an Objective-C selector. */
26296 static tree
26297 cp_parser_objc_selector (cp_parser* parser)
26299 cp_token *token = cp_lexer_consume_token (parser->lexer);
26301 if (!cp_parser_objc_selector_p (token->type))
26303 error_at (token->location, "invalid Objective-C++ selector name");
26304 return error_mark_node;
26307 /* C++ operator names are allowed to appear in ObjC selectors. */
26308 switch (token->type)
26310 case CPP_AND_AND: return get_identifier ("and");
26311 case CPP_AND_EQ: return get_identifier ("and_eq");
26312 case CPP_AND: return get_identifier ("bitand");
26313 case CPP_OR: return get_identifier ("bitor");
26314 case CPP_COMPL: return get_identifier ("compl");
26315 case CPP_NOT: return get_identifier ("not");
26316 case CPP_NOT_EQ: return get_identifier ("not_eq");
26317 case CPP_OR_OR: return get_identifier ("or");
26318 case CPP_OR_EQ: return get_identifier ("or_eq");
26319 case CPP_XOR: return get_identifier ("xor");
26320 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26321 default: return token->u.value;
26325 /* Parse an Objective-C params list. */
26327 static tree
26328 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26330 tree params = NULL_TREE;
26331 bool maybe_unary_selector_p = true;
26332 cp_token *token = cp_lexer_peek_token (parser->lexer);
26334 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26336 tree selector = NULL_TREE, type_name, identifier;
26337 tree parm_attr = NULL_TREE;
26339 if (token->keyword == RID_ATTRIBUTE)
26340 break;
26342 if (token->type != CPP_COLON)
26343 selector = cp_parser_objc_selector (parser);
26345 /* Detect if we have a unary selector. */
26346 if (maybe_unary_selector_p
26347 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26349 params = selector; /* Might be followed by attributes. */
26350 break;
26353 maybe_unary_selector_p = false;
26354 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26356 /* Something went quite wrong. There should be a colon
26357 here, but there is not. Stop parsing parameters. */
26358 break;
26360 type_name = cp_parser_objc_typename (parser);
26361 /* New ObjC allows attributes on parameters too. */
26362 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26363 parm_attr = cp_parser_attributes_opt (parser);
26364 identifier = cp_parser_identifier (parser);
26366 params
26367 = chainon (params,
26368 objc_build_keyword_decl (selector,
26369 type_name,
26370 identifier,
26371 parm_attr));
26373 token = cp_lexer_peek_token (parser->lexer);
26376 if (params == NULL_TREE)
26378 cp_parser_error (parser, "objective-c++ method declaration is expected");
26379 return error_mark_node;
26382 /* We allow tail attributes for the method. */
26383 if (token->keyword == RID_ATTRIBUTE)
26385 *attributes = cp_parser_attributes_opt (parser);
26386 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26387 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26388 return params;
26389 cp_parser_error (parser,
26390 "method attributes must be specified at the end");
26391 return error_mark_node;
26394 if (params == NULL_TREE)
26396 cp_parser_error (parser, "objective-c++ method declaration is expected");
26397 return error_mark_node;
26399 return params;
26402 /* Parse the non-keyword Objective-C params. */
26404 static tree
26405 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26406 tree* attributes)
26408 tree params = make_node (TREE_LIST);
26409 cp_token *token = cp_lexer_peek_token (parser->lexer);
26410 *ellipsisp = false; /* Initially, assume no ellipsis. */
26412 while (token->type == CPP_COMMA)
26414 cp_parameter_declarator *parmdecl;
26415 tree parm;
26417 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26418 token = cp_lexer_peek_token (parser->lexer);
26420 if (token->type == CPP_ELLIPSIS)
26422 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26423 *ellipsisp = true;
26424 token = cp_lexer_peek_token (parser->lexer);
26425 break;
26428 /* TODO: parse attributes for tail parameters. */
26429 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26430 parm = grokdeclarator (parmdecl->declarator,
26431 &parmdecl->decl_specifiers,
26432 PARM, /*initialized=*/0,
26433 /*attrlist=*/NULL);
26435 chainon (params, build_tree_list (NULL_TREE, parm));
26436 token = cp_lexer_peek_token (parser->lexer);
26439 /* We allow tail attributes for the method. */
26440 if (token->keyword == RID_ATTRIBUTE)
26442 if (*attributes == NULL_TREE)
26444 *attributes = cp_parser_attributes_opt (parser);
26445 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26446 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26447 return params;
26449 else
26450 /* We have an error, but parse the attributes, so that we can
26451 carry on. */
26452 *attributes = cp_parser_attributes_opt (parser);
26454 cp_parser_error (parser,
26455 "method attributes must be specified at the end");
26456 return error_mark_node;
26459 return params;
26462 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26464 static void
26465 cp_parser_objc_interstitial_code (cp_parser* parser)
26467 cp_token *token = cp_lexer_peek_token (parser->lexer);
26469 /* If the next token is `extern' and the following token is a string
26470 literal, then we have a linkage specification. */
26471 if (token->keyword == RID_EXTERN
26472 && cp_parser_is_pure_string_literal
26473 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26474 cp_parser_linkage_specification (parser);
26475 /* Handle #pragma, if any. */
26476 else if (token->type == CPP_PRAGMA)
26477 cp_parser_pragma (parser, pragma_objc_icode);
26478 /* Allow stray semicolons. */
26479 else if (token->type == CPP_SEMICOLON)
26480 cp_lexer_consume_token (parser->lexer);
26481 /* Mark methods as optional or required, when building protocols. */
26482 else if (token->keyword == RID_AT_OPTIONAL)
26484 cp_lexer_consume_token (parser->lexer);
26485 objc_set_method_opt (true);
26487 else if (token->keyword == RID_AT_REQUIRED)
26489 cp_lexer_consume_token (parser->lexer);
26490 objc_set_method_opt (false);
26492 else if (token->keyword == RID_NAMESPACE)
26493 cp_parser_namespace_definition (parser);
26494 /* Other stray characters must generate errors. */
26495 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26497 cp_lexer_consume_token (parser->lexer);
26498 error ("stray %qs between Objective-C++ methods",
26499 token->type == CPP_OPEN_BRACE ? "{" : "}");
26501 /* Finally, try to parse a block-declaration, or a function-definition. */
26502 else
26503 cp_parser_block_declaration (parser, /*statement_p=*/false);
26506 /* Parse a method signature. */
26508 static tree
26509 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26511 tree rettype, kwdparms, optparms;
26512 bool ellipsis = false;
26513 bool is_class_method;
26515 is_class_method = cp_parser_objc_method_type (parser);
26516 rettype = cp_parser_objc_typename (parser);
26517 *attributes = NULL_TREE;
26518 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26519 if (kwdparms == error_mark_node)
26520 return error_mark_node;
26521 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26522 if (optparms == error_mark_node)
26523 return error_mark_node;
26525 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26528 static bool
26529 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26531 tree tattr;
26532 cp_lexer_save_tokens (parser->lexer);
26533 tattr = cp_parser_attributes_opt (parser);
26534 gcc_assert (tattr) ;
26536 /* If the attributes are followed by a method introducer, this is not allowed.
26537 Dump the attributes and flag the situation. */
26538 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26539 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26540 return true;
26542 /* Otherwise, the attributes introduce some interstitial code, possibly so
26543 rewind to allow that check. */
26544 cp_lexer_rollback_tokens (parser->lexer);
26545 return false;
26548 /* Parse an Objective-C method prototype list. */
26550 static void
26551 cp_parser_objc_method_prototype_list (cp_parser* parser)
26553 cp_token *token = cp_lexer_peek_token (parser->lexer);
26555 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26557 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26559 tree attributes, sig;
26560 bool is_class_method;
26561 if (token->type == CPP_PLUS)
26562 is_class_method = true;
26563 else
26564 is_class_method = false;
26565 sig = cp_parser_objc_method_signature (parser, &attributes);
26566 if (sig == error_mark_node)
26568 cp_parser_skip_to_end_of_block_or_statement (parser);
26569 token = cp_lexer_peek_token (parser->lexer);
26570 continue;
26572 objc_add_method_declaration (is_class_method, sig, attributes);
26573 cp_parser_consume_semicolon_at_end_of_statement (parser);
26575 else if (token->keyword == RID_AT_PROPERTY)
26576 cp_parser_objc_at_property_declaration (parser);
26577 else if (token->keyword == RID_ATTRIBUTE
26578 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26579 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26580 OPT_Wattributes,
26581 "prefix attributes are ignored for methods");
26582 else
26583 /* Allow for interspersed non-ObjC++ code. */
26584 cp_parser_objc_interstitial_code (parser);
26586 token = cp_lexer_peek_token (parser->lexer);
26589 if (token->type != CPP_EOF)
26590 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26591 else
26592 cp_parser_error (parser, "expected %<@end%>");
26594 objc_finish_interface ();
26597 /* Parse an Objective-C method definition list. */
26599 static void
26600 cp_parser_objc_method_definition_list (cp_parser* parser)
26602 cp_token *token = cp_lexer_peek_token (parser->lexer);
26604 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26606 tree meth;
26608 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26610 cp_token *ptk;
26611 tree sig, attribute;
26612 bool is_class_method;
26613 if (token->type == CPP_PLUS)
26614 is_class_method = true;
26615 else
26616 is_class_method = false;
26617 push_deferring_access_checks (dk_deferred);
26618 sig = cp_parser_objc_method_signature (parser, &attribute);
26619 if (sig == error_mark_node)
26621 cp_parser_skip_to_end_of_block_or_statement (parser);
26622 token = cp_lexer_peek_token (parser->lexer);
26623 continue;
26625 objc_start_method_definition (is_class_method, sig, attribute,
26626 NULL_TREE);
26628 /* For historical reasons, we accept an optional semicolon. */
26629 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26630 cp_lexer_consume_token (parser->lexer);
26632 ptk = cp_lexer_peek_token (parser->lexer);
26633 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26634 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26636 perform_deferred_access_checks (tf_warning_or_error);
26637 stop_deferring_access_checks ();
26638 meth = cp_parser_function_definition_after_declarator (parser,
26639 false);
26640 pop_deferring_access_checks ();
26641 objc_finish_method_definition (meth);
26644 /* The following case will be removed once @synthesize is
26645 completely implemented. */
26646 else if (token->keyword == RID_AT_PROPERTY)
26647 cp_parser_objc_at_property_declaration (parser);
26648 else if (token->keyword == RID_AT_SYNTHESIZE)
26649 cp_parser_objc_at_synthesize_declaration (parser);
26650 else if (token->keyword == RID_AT_DYNAMIC)
26651 cp_parser_objc_at_dynamic_declaration (parser);
26652 else if (token->keyword == RID_ATTRIBUTE
26653 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26654 warning_at (token->location, OPT_Wattributes,
26655 "prefix attributes are ignored for methods");
26656 else
26657 /* Allow for interspersed non-ObjC++ code. */
26658 cp_parser_objc_interstitial_code (parser);
26660 token = cp_lexer_peek_token (parser->lexer);
26663 if (token->type != CPP_EOF)
26664 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26665 else
26666 cp_parser_error (parser, "expected %<@end%>");
26668 objc_finish_implementation ();
26671 /* Parse Objective-C ivars. */
26673 static void
26674 cp_parser_objc_class_ivars (cp_parser* parser)
26676 cp_token *token = cp_lexer_peek_token (parser->lexer);
26678 if (token->type != CPP_OPEN_BRACE)
26679 return; /* No ivars specified. */
26681 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26682 token = cp_lexer_peek_token (parser->lexer);
26684 while (token->type != CPP_CLOSE_BRACE
26685 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26687 cp_decl_specifier_seq declspecs;
26688 int decl_class_or_enum_p;
26689 tree prefix_attributes;
26691 cp_parser_objc_visibility_spec (parser);
26693 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26694 break;
26696 cp_parser_decl_specifier_seq (parser,
26697 CP_PARSER_FLAGS_OPTIONAL,
26698 &declspecs,
26699 &decl_class_or_enum_p);
26701 /* auto, register, static, extern, mutable. */
26702 if (declspecs.storage_class != sc_none)
26704 cp_parser_error (parser, "invalid type for instance variable");
26705 declspecs.storage_class = sc_none;
26708 /* thread_local. */
26709 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26711 cp_parser_error (parser, "invalid type for instance variable");
26712 declspecs.locations[ds_thread] = 0;
26715 /* typedef. */
26716 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26718 cp_parser_error (parser, "invalid type for instance variable");
26719 declspecs.locations[ds_typedef] = 0;
26722 prefix_attributes = declspecs.attributes;
26723 declspecs.attributes = NULL_TREE;
26725 /* Keep going until we hit the `;' at the end of the
26726 declaration. */
26727 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26729 tree width = NULL_TREE, attributes, first_attribute, decl;
26730 cp_declarator *declarator = NULL;
26731 int ctor_dtor_or_conv_p;
26733 /* Check for a (possibly unnamed) bitfield declaration. */
26734 token = cp_lexer_peek_token (parser->lexer);
26735 if (token->type == CPP_COLON)
26736 goto eat_colon;
26738 if (token->type == CPP_NAME
26739 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26740 == CPP_COLON))
26742 /* Get the name of the bitfield. */
26743 declarator = make_id_declarator (NULL_TREE,
26744 cp_parser_identifier (parser),
26745 sfk_none);
26747 eat_colon:
26748 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26749 /* Get the width of the bitfield. */
26750 width
26751 = cp_parser_constant_expression (parser);
26753 else
26755 /* Parse the declarator. */
26756 declarator
26757 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26758 &ctor_dtor_or_conv_p,
26759 /*parenthesized_p=*/NULL,
26760 /*member_p=*/false,
26761 /*friend_p=*/false);
26764 /* Look for attributes that apply to the ivar. */
26765 attributes = cp_parser_attributes_opt (parser);
26766 /* Remember which attributes are prefix attributes and
26767 which are not. */
26768 first_attribute = attributes;
26769 /* Combine the attributes. */
26770 attributes = chainon (prefix_attributes, attributes);
26772 if (width)
26773 /* Create the bitfield declaration. */
26774 decl = grokbitfield (declarator, &declspecs,
26775 width,
26776 attributes);
26777 else
26778 decl = grokfield (declarator, &declspecs,
26779 NULL_TREE, /*init_const_expr_p=*/false,
26780 NULL_TREE, attributes);
26782 /* Add the instance variable. */
26783 if (decl != error_mark_node && decl != NULL_TREE)
26784 objc_add_instance_variable (decl);
26786 /* Reset PREFIX_ATTRIBUTES. */
26787 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26788 attributes = TREE_CHAIN (attributes);
26789 if (attributes)
26790 TREE_CHAIN (attributes) = NULL_TREE;
26792 token = cp_lexer_peek_token (parser->lexer);
26794 if (token->type == CPP_COMMA)
26796 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26797 continue;
26799 break;
26802 cp_parser_consume_semicolon_at_end_of_statement (parser);
26803 token = cp_lexer_peek_token (parser->lexer);
26806 if (token->keyword == RID_AT_END)
26807 cp_parser_error (parser, "expected %<}%>");
26809 /* Do not consume the RID_AT_END, so it will be read again as terminating
26810 the @interface of @implementation. */
26811 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26812 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26814 /* For historical reasons, we accept an optional semicolon. */
26815 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26816 cp_lexer_consume_token (parser->lexer);
26819 /* Parse an Objective-C protocol declaration. */
26821 static void
26822 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26824 tree proto, protorefs;
26825 cp_token *tok;
26827 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26828 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26830 tok = cp_lexer_peek_token (parser->lexer);
26831 error_at (tok->location, "identifier expected after %<@protocol%>");
26832 cp_parser_consume_semicolon_at_end_of_statement (parser);
26833 return;
26836 /* See if we have a forward declaration or a definition. */
26837 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26839 /* Try a forward declaration first. */
26840 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26842 while (true)
26844 tree id;
26846 id = cp_parser_identifier (parser);
26847 if (id == error_mark_node)
26848 break;
26850 objc_declare_protocol (id, attributes);
26852 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26853 cp_lexer_consume_token (parser->lexer);
26854 else
26855 break;
26857 cp_parser_consume_semicolon_at_end_of_statement (parser);
26860 /* Ok, we got a full-fledged definition (or at least should). */
26861 else
26863 proto = cp_parser_identifier (parser);
26864 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26865 objc_start_protocol (proto, protorefs, attributes);
26866 cp_parser_objc_method_prototype_list (parser);
26870 /* Parse an Objective-C superclass or category. */
26872 static void
26873 cp_parser_objc_superclass_or_category (cp_parser *parser,
26874 bool iface_p,
26875 tree *super,
26876 tree *categ, bool *is_class_extension)
26878 cp_token *next = cp_lexer_peek_token (parser->lexer);
26880 *super = *categ = NULL_TREE;
26881 *is_class_extension = false;
26882 if (next->type == CPP_COLON)
26884 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26885 *super = cp_parser_identifier (parser);
26887 else if (next->type == CPP_OPEN_PAREN)
26889 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26891 /* If there is no category name, and this is an @interface, we
26892 have a class extension. */
26893 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26895 *categ = NULL_TREE;
26896 *is_class_extension = true;
26898 else
26899 *categ = cp_parser_identifier (parser);
26901 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26905 /* Parse an Objective-C class interface. */
26907 static void
26908 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26910 tree name, super, categ, protos;
26911 bool is_class_extension;
26913 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26914 name = cp_parser_identifier (parser);
26915 if (name == error_mark_node)
26917 /* It's hard to recover because even if valid @interface stuff
26918 is to follow, we can't compile it (or validate it) if we
26919 don't even know which class it refers to. Let's assume this
26920 was a stray '@interface' token in the stream and skip it.
26922 return;
26924 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26925 &is_class_extension);
26926 protos = cp_parser_objc_protocol_refs_opt (parser);
26928 /* We have either a class or a category on our hands. */
26929 if (categ || is_class_extension)
26930 objc_start_category_interface (name, categ, protos, attributes);
26931 else
26933 objc_start_class_interface (name, super, protos, attributes);
26934 /* Handle instance variable declarations, if any. */
26935 cp_parser_objc_class_ivars (parser);
26936 objc_continue_interface ();
26939 cp_parser_objc_method_prototype_list (parser);
26942 /* Parse an Objective-C class implementation. */
26944 static void
26945 cp_parser_objc_class_implementation (cp_parser* parser)
26947 tree name, super, categ;
26948 bool is_class_extension;
26950 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26951 name = cp_parser_identifier (parser);
26952 if (name == error_mark_node)
26954 /* It's hard to recover because even if valid @implementation
26955 stuff is to follow, we can't compile it (or validate it) if
26956 we don't even know which class it refers to. Let's assume
26957 this was a stray '@implementation' token in the stream and
26958 skip it.
26960 return;
26962 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26963 &is_class_extension);
26965 /* We have either a class or a category on our hands. */
26966 if (categ)
26967 objc_start_category_implementation (name, categ);
26968 else
26970 objc_start_class_implementation (name, super);
26971 /* Handle instance variable declarations, if any. */
26972 cp_parser_objc_class_ivars (parser);
26973 objc_continue_implementation ();
26976 cp_parser_objc_method_definition_list (parser);
26979 /* Consume the @end token and finish off the implementation. */
26981 static void
26982 cp_parser_objc_end_implementation (cp_parser* parser)
26984 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26985 objc_finish_implementation ();
26988 /* Parse an Objective-C declaration. */
26990 static void
26991 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26993 /* Try to figure out what kind of declaration is present. */
26994 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26996 if (attributes)
26997 switch (kwd->keyword)
26999 case RID_AT_ALIAS:
27000 case RID_AT_CLASS:
27001 case RID_AT_END:
27002 error_at (kwd->location, "attributes may not be specified before"
27003 " the %<@%D%> Objective-C++ keyword",
27004 kwd->u.value);
27005 attributes = NULL;
27006 break;
27007 case RID_AT_IMPLEMENTATION:
27008 warning_at (kwd->location, OPT_Wattributes,
27009 "prefix attributes are ignored before %<@%D%>",
27010 kwd->u.value);
27011 attributes = NULL;
27012 default:
27013 break;
27016 switch (kwd->keyword)
27018 case RID_AT_ALIAS:
27019 cp_parser_objc_alias_declaration (parser);
27020 break;
27021 case RID_AT_CLASS:
27022 cp_parser_objc_class_declaration (parser);
27023 break;
27024 case RID_AT_PROTOCOL:
27025 cp_parser_objc_protocol_declaration (parser, attributes);
27026 break;
27027 case RID_AT_INTERFACE:
27028 cp_parser_objc_class_interface (parser, attributes);
27029 break;
27030 case RID_AT_IMPLEMENTATION:
27031 cp_parser_objc_class_implementation (parser);
27032 break;
27033 case RID_AT_END:
27034 cp_parser_objc_end_implementation (parser);
27035 break;
27036 default:
27037 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27038 kwd->u.value);
27039 cp_parser_skip_to_end_of_block_or_statement (parser);
27043 /* Parse an Objective-C try-catch-finally statement.
27045 objc-try-catch-finally-stmt:
27046 @try compound-statement objc-catch-clause-seq [opt]
27047 objc-finally-clause [opt]
27049 objc-catch-clause-seq:
27050 objc-catch-clause objc-catch-clause-seq [opt]
27052 objc-catch-clause:
27053 @catch ( objc-exception-declaration ) compound-statement
27055 objc-finally-clause:
27056 @finally compound-statement
27058 objc-exception-declaration:
27059 parameter-declaration
27060 '...'
27062 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
27064 Returns NULL_TREE.
27066 PS: This function is identical to c_parser_objc_try_catch_finally_statement
27067 for C. Keep them in sync. */
27069 static tree
27070 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
27072 location_t location;
27073 tree stmt;
27075 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
27076 location = cp_lexer_peek_token (parser->lexer)->location;
27077 objc_maybe_warn_exceptions (location);
27078 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
27079 node, lest it get absorbed into the surrounding block. */
27080 stmt = push_stmt_list ();
27081 cp_parser_compound_statement (parser, NULL, false, false);
27082 objc_begin_try_stmt (location, pop_stmt_list (stmt));
27084 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
27086 cp_parameter_declarator *parm;
27087 tree parameter_declaration = error_mark_node;
27088 bool seen_open_paren = false;
27090 cp_lexer_consume_token (parser->lexer);
27091 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27092 seen_open_paren = true;
27093 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27095 /* We have "@catch (...)" (where the '...' are literally
27096 what is in the code). Skip the '...'.
27097 parameter_declaration is set to NULL_TREE, and
27098 objc_being_catch_clauses() knows that that means
27099 '...'. */
27100 cp_lexer_consume_token (parser->lexer);
27101 parameter_declaration = NULL_TREE;
27103 else
27105 /* We have "@catch (NSException *exception)" or something
27106 like that. Parse the parameter declaration. */
27107 parm = cp_parser_parameter_declaration (parser, false, NULL);
27108 if (parm == NULL)
27109 parameter_declaration = error_mark_node;
27110 else
27111 parameter_declaration = grokdeclarator (parm->declarator,
27112 &parm->decl_specifiers,
27113 PARM, /*initialized=*/0,
27114 /*attrlist=*/NULL);
27116 if (seen_open_paren)
27117 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27118 else
27120 /* If there was no open parenthesis, we are recovering from
27121 an error, and we are trying to figure out what mistake
27122 the user has made. */
27124 /* If there is an immediate closing parenthesis, the user
27125 probably forgot the opening one (ie, they typed "@catch
27126 NSException *e)". Parse the closing parenthesis and keep
27127 going. */
27128 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27129 cp_lexer_consume_token (parser->lexer);
27131 /* If these is no immediate closing parenthesis, the user
27132 probably doesn't know that parenthesis are required at
27133 all (ie, they typed "@catch NSException *e"). So, just
27134 forget about the closing parenthesis and keep going. */
27136 objc_begin_catch_clause (parameter_declaration);
27137 cp_parser_compound_statement (parser, NULL, false, false);
27138 objc_finish_catch_clause ();
27140 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
27142 cp_lexer_consume_token (parser->lexer);
27143 location = cp_lexer_peek_token (parser->lexer)->location;
27144 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27145 node, lest it get absorbed into the surrounding block. */
27146 stmt = push_stmt_list ();
27147 cp_parser_compound_statement (parser, NULL, false, false);
27148 objc_build_finally_clause (location, pop_stmt_list (stmt));
27151 return objc_finish_try_stmt ();
27154 /* Parse an Objective-C synchronized statement.
27156 objc-synchronized-stmt:
27157 @synchronized ( expression ) compound-statement
27159 Returns NULL_TREE. */
27161 static tree
27162 cp_parser_objc_synchronized_statement (cp_parser *parser)
27164 location_t location;
27165 tree lock, stmt;
27167 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27169 location = cp_lexer_peek_token (parser->lexer)->location;
27170 objc_maybe_warn_exceptions (location);
27171 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27172 lock = cp_parser_expression (parser);
27173 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27175 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27176 node, lest it get absorbed into the surrounding block. */
27177 stmt = push_stmt_list ();
27178 cp_parser_compound_statement (parser, NULL, false, false);
27180 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27183 /* Parse an Objective-C throw statement.
27185 objc-throw-stmt:
27186 @throw assignment-expression [opt] ;
27188 Returns a constructed '@throw' statement. */
27190 static tree
27191 cp_parser_objc_throw_statement (cp_parser *parser)
27193 tree expr = NULL_TREE;
27194 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27196 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27198 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27199 expr = cp_parser_expression (parser);
27201 cp_parser_consume_semicolon_at_end_of_statement (parser);
27203 return objc_build_throw_stmt (loc, expr);
27206 /* Parse an Objective-C statement. */
27208 static tree
27209 cp_parser_objc_statement (cp_parser * parser)
27211 /* Try to figure out what kind of declaration is present. */
27212 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27214 switch (kwd->keyword)
27216 case RID_AT_TRY:
27217 return cp_parser_objc_try_catch_finally_statement (parser);
27218 case RID_AT_SYNCHRONIZED:
27219 return cp_parser_objc_synchronized_statement (parser);
27220 case RID_AT_THROW:
27221 return cp_parser_objc_throw_statement (parser);
27222 default:
27223 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27224 kwd->u.value);
27225 cp_parser_skip_to_end_of_block_or_statement (parser);
27228 return error_mark_node;
27231 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27232 look ahead to see if an objc keyword follows the attributes. This
27233 is to detect the use of prefix attributes on ObjC @interface and
27234 @protocol. */
27236 static bool
27237 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27239 cp_lexer_save_tokens (parser->lexer);
27240 *attrib = cp_parser_attributes_opt (parser);
27241 gcc_assert (*attrib);
27242 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27244 cp_lexer_commit_tokens (parser->lexer);
27245 return true;
27247 cp_lexer_rollback_tokens (parser->lexer);
27248 return false;
27251 /* This routine is a minimal replacement for
27252 c_parser_struct_declaration () used when parsing the list of
27253 types/names or ObjC++ properties. For example, when parsing the
27254 code
27256 @property (readonly) int a, b, c;
27258 this function is responsible for parsing "int a, int b, int c" and
27259 returning the declarations as CHAIN of DECLs.
27261 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27262 similar parsing. */
27263 static tree
27264 cp_parser_objc_struct_declaration (cp_parser *parser)
27266 tree decls = NULL_TREE;
27267 cp_decl_specifier_seq declspecs;
27268 int decl_class_or_enum_p;
27269 tree prefix_attributes;
27271 cp_parser_decl_specifier_seq (parser,
27272 CP_PARSER_FLAGS_NONE,
27273 &declspecs,
27274 &decl_class_or_enum_p);
27276 if (declspecs.type == error_mark_node)
27277 return error_mark_node;
27279 /* auto, register, static, extern, mutable. */
27280 if (declspecs.storage_class != sc_none)
27282 cp_parser_error (parser, "invalid type for property");
27283 declspecs.storage_class = sc_none;
27286 /* thread_local. */
27287 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27289 cp_parser_error (parser, "invalid type for property");
27290 declspecs.locations[ds_thread] = 0;
27293 /* typedef. */
27294 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27296 cp_parser_error (parser, "invalid type for property");
27297 declspecs.locations[ds_typedef] = 0;
27300 prefix_attributes = declspecs.attributes;
27301 declspecs.attributes = NULL_TREE;
27303 /* Keep going until we hit the `;' at the end of the declaration. */
27304 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27306 tree attributes, first_attribute, decl;
27307 cp_declarator *declarator;
27308 cp_token *token;
27310 /* Parse the declarator. */
27311 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27312 NULL, NULL, false, false);
27314 /* Look for attributes that apply to the ivar. */
27315 attributes = cp_parser_attributes_opt (parser);
27316 /* Remember which attributes are prefix attributes and
27317 which are not. */
27318 first_attribute = attributes;
27319 /* Combine the attributes. */
27320 attributes = chainon (prefix_attributes, attributes);
27322 decl = grokfield (declarator, &declspecs,
27323 NULL_TREE, /*init_const_expr_p=*/false,
27324 NULL_TREE, attributes);
27326 if (decl == error_mark_node || decl == NULL_TREE)
27327 return error_mark_node;
27329 /* Reset PREFIX_ATTRIBUTES. */
27330 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27331 attributes = TREE_CHAIN (attributes);
27332 if (attributes)
27333 TREE_CHAIN (attributes) = NULL_TREE;
27335 DECL_CHAIN (decl) = decls;
27336 decls = decl;
27338 token = cp_lexer_peek_token (parser->lexer);
27339 if (token->type == CPP_COMMA)
27341 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27342 continue;
27344 else
27345 break;
27347 return decls;
27350 /* Parse an Objective-C @property declaration. The syntax is:
27352 objc-property-declaration:
27353 '@property' objc-property-attributes[opt] struct-declaration ;
27355 objc-property-attributes:
27356 '(' objc-property-attribute-list ')'
27358 objc-property-attribute-list:
27359 objc-property-attribute
27360 objc-property-attribute-list, objc-property-attribute
27362 objc-property-attribute
27363 'getter' = identifier
27364 'setter' = identifier
27365 'readonly'
27366 'readwrite'
27367 'assign'
27368 'retain'
27369 'copy'
27370 'nonatomic'
27372 For example:
27373 @property NSString *name;
27374 @property (readonly) id object;
27375 @property (retain, nonatomic, getter=getTheName) id name;
27376 @property int a, b, c;
27378 PS: This function is identical to
27379 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27380 static void
27381 cp_parser_objc_at_property_declaration (cp_parser *parser)
27383 /* The following variables hold the attributes of the properties as
27384 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27385 seen. When we see an attribute, we set them to 'true' (if they
27386 are boolean properties) or to the identifier (if they have an
27387 argument, ie, for getter and setter). Note that here we only
27388 parse the list of attributes, check the syntax and accumulate the
27389 attributes that we find. objc_add_property_declaration() will
27390 then process the information. */
27391 bool property_assign = false;
27392 bool property_copy = false;
27393 tree property_getter_ident = NULL_TREE;
27394 bool property_nonatomic = false;
27395 bool property_readonly = false;
27396 bool property_readwrite = false;
27397 bool property_retain = false;
27398 tree property_setter_ident = NULL_TREE;
27400 /* 'properties' is the list of properties that we read. Usually a
27401 single one, but maybe more (eg, in "@property int a, b, c;" there
27402 are three). */
27403 tree properties;
27404 location_t loc;
27406 loc = cp_lexer_peek_token (parser->lexer)->location;
27408 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27410 /* Parse the optional attribute list... */
27411 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27413 /* Eat the '('. */
27414 cp_lexer_consume_token (parser->lexer);
27416 while (true)
27418 bool syntax_error = false;
27419 cp_token *token = cp_lexer_peek_token (parser->lexer);
27420 enum rid keyword;
27422 if (token->type != CPP_NAME)
27424 cp_parser_error (parser, "expected identifier");
27425 break;
27427 keyword = C_RID_CODE (token->u.value);
27428 cp_lexer_consume_token (parser->lexer);
27429 switch (keyword)
27431 case RID_ASSIGN: property_assign = true; break;
27432 case RID_COPY: property_copy = true; break;
27433 case RID_NONATOMIC: property_nonatomic = true; break;
27434 case RID_READONLY: property_readonly = true; break;
27435 case RID_READWRITE: property_readwrite = true; break;
27436 case RID_RETAIN: property_retain = true; break;
27438 case RID_GETTER:
27439 case RID_SETTER:
27440 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27442 if (keyword == RID_GETTER)
27443 cp_parser_error (parser,
27444 "missing %<=%> (after %<getter%> attribute)");
27445 else
27446 cp_parser_error (parser,
27447 "missing %<=%> (after %<setter%> attribute)");
27448 syntax_error = true;
27449 break;
27451 cp_lexer_consume_token (parser->lexer); /* eat the = */
27452 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27454 cp_parser_error (parser, "expected identifier");
27455 syntax_error = true;
27456 break;
27458 if (keyword == RID_SETTER)
27460 if (property_setter_ident != NULL_TREE)
27462 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27463 cp_lexer_consume_token (parser->lexer);
27465 else
27466 property_setter_ident = cp_parser_objc_selector (parser);
27467 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27468 cp_parser_error (parser, "setter name must terminate with %<:%>");
27469 else
27470 cp_lexer_consume_token (parser->lexer);
27472 else
27474 if (property_getter_ident != NULL_TREE)
27476 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27477 cp_lexer_consume_token (parser->lexer);
27479 else
27480 property_getter_ident = cp_parser_objc_selector (parser);
27482 break;
27483 default:
27484 cp_parser_error (parser, "unknown property attribute");
27485 syntax_error = true;
27486 break;
27489 if (syntax_error)
27490 break;
27492 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27493 cp_lexer_consume_token (parser->lexer);
27494 else
27495 break;
27498 /* FIXME: "@property (setter, assign);" will generate a spurious
27499 "error: expected ‘)’ before ‘,’ token". This is because
27500 cp_parser_require, unlike the C counterpart, will produce an
27501 error even if we are in error recovery. */
27502 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27504 cp_parser_skip_to_closing_parenthesis (parser,
27505 /*recovering=*/true,
27506 /*or_comma=*/false,
27507 /*consume_paren=*/true);
27511 /* ... and the property declaration(s). */
27512 properties = cp_parser_objc_struct_declaration (parser);
27514 if (properties == error_mark_node)
27516 cp_parser_skip_to_end_of_statement (parser);
27517 /* If the next token is now a `;', consume it. */
27518 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27519 cp_lexer_consume_token (parser->lexer);
27520 return;
27523 if (properties == NULL_TREE)
27524 cp_parser_error (parser, "expected identifier");
27525 else
27527 /* Comma-separated properties are chained together in
27528 reverse order; add them one by one. */
27529 properties = nreverse (properties);
27531 for (; properties; properties = TREE_CHAIN (properties))
27532 objc_add_property_declaration (loc, copy_node (properties),
27533 property_readonly, property_readwrite,
27534 property_assign, property_retain,
27535 property_copy, property_nonatomic,
27536 property_getter_ident, property_setter_ident);
27539 cp_parser_consume_semicolon_at_end_of_statement (parser);
27542 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27544 objc-synthesize-declaration:
27545 @synthesize objc-synthesize-identifier-list ;
27547 objc-synthesize-identifier-list:
27548 objc-synthesize-identifier
27549 objc-synthesize-identifier-list, objc-synthesize-identifier
27551 objc-synthesize-identifier
27552 identifier
27553 identifier = identifier
27555 For example:
27556 @synthesize MyProperty;
27557 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27559 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27560 for C. Keep them in sync.
27562 static void
27563 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27565 tree list = NULL_TREE;
27566 location_t loc;
27567 loc = cp_lexer_peek_token (parser->lexer)->location;
27569 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27570 while (true)
27572 tree property, ivar;
27573 property = cp_parser_identifier (parser);
27574 if (property == error_mark_node)
27576 cp_parser_consume_semicolon_at_end_of_statement (parser);
27577 return;
27579 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27581 cp_lexer_consume_token (parser->lexer);
27582 ivar = cp_parser_identifier (parser);
27583 if (ivar == error_mark_node)
27585 cp_parser_consume_semicolon_at_end_of_statement (parser);
27586 return;
27589 else
27590 ivar = NULL_TREE;
27591 list = chainon (list, build_tree_list (ivar, property));
27592 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27593 cp_lexer_consume_token (parser->lexer);
27594 else
27595 break;
27597 cp_parser_consume_semicolon_at_end_of_statement (parser);
27598 objc_add_synthesize_declaration (loc, list);
27601 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27603 objc-dynamic-declaration:
27604 @dynamic identifier-list ;
27606 For example:
27607 @dynamic MyProperty;
27608 @dynamic MyProperty, AnotherProperty;
27610 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27611 for C. Keep them in sync.
27613 static void
27614 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27616 tree list = NULL_TREE;
27617 location_t loc;
27618 loc = cp_lexer_peek_token (parser->lexer)->location;
27620 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27621 while (true)
27623 tree property;
27624 property = cp_parser_identifier (parser);
27625 if (property == error_mark_node)
27627 cp_parser_consume_semicolon_at_end_of_statement (parser);
27628 return;
27630 list = chainon (list, build_tree_list (NULL, property));
27631 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27632 cp_lexer_consume_token (parser->lexer);
27633 else
27634 break;
27636 cp_parser_consume_semicolon_at_end_of_statement (parser);
27637 objc_add_dynamic_declaration (loc, list);
27641 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27643 /* Returns name of the next clause.
27644 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27645 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27646 returned and the token is consumed. */
27648 static pragma_omp_clause
27649 cp_parser_omp_clause_name (cp_parser *parser)
27651 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27653 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27654 result = PRAGMA_OMP_CLAUSE_IF;
27655 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27656 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27657 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
27658 result = PRAGMA_OACC_CLAUSE_DELETE;
27659 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27660 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27661 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27662 result = PRAGMA_OMP_CLAUSE_FOR;
27663 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27665 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27666 const char *p = IDENTIFIER_POINTER (id);
27668 switch (p[0])
27670 case 'a':
27671 if (!strcmp ("aligned", p))
27672 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27673 else if (!strcmp ("async", p))
27674 result = PRAGMA_OACC_CLAUSE_ASYNC;
27675 break;
27676 case 'c':
27677 if (!strcmp ("collapse", p))
27678 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27679 else if (!strcmp ("copy", p))
27680 result = PRAGMA_OACC_CLAUSE_COPY;
27681 else if (!strcmp ("copyin", p))
27682 result = PRAGMA_OMP_CLAUSE_COPYIN;
27683 else if (!strcmp ("copyout", p))
27684 result = PRAGMA_OACC_CLAUSE_COPYOUT;
27685 else if (!strcmp ("copyprivate", p))
27686 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27687 else if (!strcmp ("create", p))
27688 result = PRAGMA_OACC_CLAUSE_CREATE;
27689 break;
27690 case 'd':
27691 if (!strcmp ("depend", p))
27692 result = PRAGMA_OMP_CLAUSE_DEPEND;
27693 else if (!strcmp ("device", p))
27694 result = PRAGMA_OMP_CLAUSE_DEVICE;
27695 else if (!strcmp ("deviceptr", p))
27696 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
27697 else if (!strcmp ("dist_schedule", p))
27698 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27699 break;
27700 case 'f':
27701 if (!strcmp ("final", p))
27702 result = PRAGMA_OMP_CLAUSE_FINAL;
27703 else if (!strcmp ("firstprivate", p))
27704 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27705 else if (!strcmp ("from", p))
27706 result = PRAGMA_OMP_CLAUSE_FROM;
27707 break;
27708 case 'h':
27709 if (!strcmp ("host", p))
27710 result = PRAGMA_OACC_CLAUSE_HOST;
27711 break;
27712 case 'i':
27713 if (!strcmp ("inbranch", p))
27714 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27715 break;
27716 case 'l':
27717 if (!strcmp ("lastprivate", p))
27718 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27719 else if (!strcmp ("linear", p))
27720 result = PRAGMA_OMP_CLAUSE_LINEAR;
27721 break;
27722 case 'm':
27723 if (!strcmp ("map", p))
27724 result = PRAGMA_OMP_CLAUSE_MAP;
27725 else if (!strcmp ("mergeable", p))
27726 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27727 else if (flag_cilkplus && !strcmp ("mask", p))
27728 result = PRAGMA_CILK_CLAUSE_MASK;
27729 break;
27730 case 'n':
27731 if (!strcmp ("notinbranch", p))
27732 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27733 else if (!strcmp ("nowait", p))
27734 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27735 else if (flag_cilkplus && !strcmp ("nomask", p))
27736 result = PRAGMA_CILK_CLAUSE_NOMASK;
27737 else if (!strcmp ("num_gangs", p))
27738 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
27739 else if (!strcmp ("num_teams", p))
27740 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27741 else if (!strcmp ("num_threads", p))
27742 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27743 else if (!strcmp ("num_workers", p))
27744 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
27745 break;
27746 case 'o':
27747 if (!strcmp ("ordered", p))
27748 result = PRAGMA_OMP_CLAUSE_ORDERED;
27749 break;
27750 case 'p':
27751 if (!strcmp ("parallel", p))
27752 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27753 else if (!strcmp ("present", p))
27754 result = PRAGMA_OACC_CLAUSE_PRESENT;
27755 else if (!strcmp ("present_or_copy", p)
27756 || !strcmp ("pcopy", p))
27757 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
27758 else if (!strcmp ("present_or_copyin", p)
27759 || !strcmp ("pcopyin", p))
27760 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
27761 else if (!strcmp ("present_or_copyout", p)
27762 || !strcmp ("pcopyout", p))
27763 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
27764 else if (!strcmp ("present_or_create", p)
27765 || !strcmp ("pcreate", p))
27766 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
27767 else if (!strcmp ("proc_bind", p))
27768 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27769 break;
27770 case 'r':
27771 if (!strcmp ("reduction", p))
27772 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27773 break;
27774 case 's':
27775 if (!strcmp ("safelen", p))
27776 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27777 else if (!strcmp ("schedule", p))
27778 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27779 else if (!strcmp ("sections", p))
27780 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27781 else if (!strcmp ("self", p))
27782 result = PRAGMA_OACC_CLAUSE_SELF;
27783 else if (!strcmp ("shared", p))
27784 result = PRAGMA_OMP_CLAUSE_SHARED;
27785 else if (!strcmp ("simdlen", p))
27786 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27787 break;
27788 case 't':
27789 if (!strcmp ("taskgroup", p))
27790 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27791 else if (!strcmp ("thread_limit", p))
27792 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27793 else if (!strcmp ("to", p))
27794 result = PRAGMA_OMP_CLAUSE_TO;
27795 break;
27796 case 'u':
27797 if (!strcmp ("uniform", p))
27798 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27799 else if (!strcmp ("untied", p))
27800 result = PRAGMA_OMP_CLAUSE_UNTIED;
27801 break;
27802 case 'v':
27803 if (!strcmp ("vector_length", p))
27804 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
27805 else if (flag_cilkplus && !strcmp ("vectorlength", p))
27806 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27807 break;
27808 case 'w':
27809 if (!strcmp ("wait", p))
27810 result = PRAGMA_OACC_CLAUSE_WAIT;
27811 break;
27815 if (result != PRAGMA_OMP_CLAUSE_NONE)
27816 cp_lexer_consume_token (parser->lexer);
27818 return result;
27821 /* Validate that a clause of the given type does not already exist. */
27823 static void
27824 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27825 const char *name, location_t location)
27827 tree c;
27829 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27830 if (OMP_CLAUSE_CODE (c) == code)
27832 error_at (location, "too many %qs clauses", name);
27833 break;
27837 /* OpenMP 2.5:
27838 variable-list:
27839 identifier
27840 variable-list , identifier
27842 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27843 colon). An opening parenthesis will have been consumed by the caller.
27845 If KIND is nonzero, create the appropriate node and install the decl
27846 in OMP_CLAUSE_DECL and add the node to the head of the list.
27848 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27849 return the list created.
27851 COLON can be NULL if only closing parenthesis should end the list,
27852 or pointer to bool which will receive false if the list is terminated
27853 by closing parenthesis or true if the list is terminated by colon. */
27855 static tree
27856 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27857 tree list, bool *colon)
27859 cp_token *token;
27860 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27861 if (colon)
27863 parser->colon_corrects_to_scope_p = false;
27864 *colon = false;
27866 while (1)
27868 tree name, decl;
27870 token = cp_lexer_peek_token (parser->lexer);
27871 name = cp_parser_id_expression (parser, /*template_p=*/false,
27872 /*check_dependency_p=*/true,
27873 /*template_p=*/NULL,
27874 /*declarator_p=*/false,
27875 /*optional_p=*/false);
27876 if (name == error_mark_node)
27877 goto skip_comma;
27879 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27880 if (decl == error_mark_node)
27881 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27882 token->location);
27883 else if (kind != 0)
27885 switch (kind)
27887 case OMP_CLAUSE__CACHE_:
27888 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27890 error_at (token->location, "expected %<[%>");
27891 decl = error_mark_node;
27892 break;
27894 /* FALL THROUGH. */
27895 case OMP_CLAUSE_MAP:
27896 case OMP_CLAUSE_FROM:
27897 case OMP_CLAUSE_TO:
27898 case OMP_CLAUSE_DEPEND:
27899 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27901 tree low_bound = NULL_TREE, length = NULL_TREE;
27903 parser->colon_corrects_to_scope_p = false;
27904 cp_lexer_consume_token (parser->lexer);
27905 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27906 low_bound = cp_parser_expression (parser);
27907 if (!colon)
27908 parser->colon_corrects_to_scope_p
27909 = saved_colon_corrects_to_scope_p;
27910 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27911 length = integer_one_node;
27912 else
27914 /* Look for `:'. */
27915 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27916 goto skip_comma;
27917 if (!cp_lexer_next_token_is (parser->lexer,
27918 CPP_CLOSE_SQUARE))
27919 length = cp_parser_expression (parser);
27921 /* Look for the closing `]'. */
27922 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27923 RT_CLOSE_SQUARE))
27924 goto skip_comma;
27926 if (kind == OMP_CLAUSE__CACHE_)
27928 if (TREE_CODE (low_bound) != INTEGER_CST
27929 && !TREE_READONLY (low_bound))
27931 error_at (token->location,
27932 "%qD is not a constant", low_bound);
27933 decl = error_mark_node;
27936 if (TREE_CODE (length) != INTEGER_CST
27937 && !TREE_READONLY (length))
27939 error_at (token->location,
27940 "%qD is not a constant", length);
27941 decl = error_mark_node;
27945 decl = tree_cons (low_bound, length, decl);
27947 break;
27948 default:
27949 break;
27952 tree u = build_omp_clause (token->location, kind);
27953 OMP_CLAUSE_DECL (u) = decl;
27954 OMP_CLAUSE_CHAIN (u) = list;
27955 list = u;
27957 else
27958 list = tree_cons (decl, NULL_TREE, list);
27960 get_comma:
27961 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27962 break;
27963 cp_lexer_consume_token (parser->lexer);
27966 if (colon)
27967 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27969 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27971 *colon = true;
27972 cp_parser_require (parser, CPP_COLON, RT_COLON);
27973 return list;
27976 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27978 int ending;
27980 /* Try to resync to an unnested comma. Copied from
27981 cp_parser_parenthesized_expression_list. */
27982 skip_comma:
27983 if (colon)
27984 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27985 ending = cp_parser_skip_to_closing_parenthesis (parser,
27986 /*recovering=*/true,
27987 /*or_comma=*/true,
27988 /*consume_paren=*/true);
27989 if (ending < 0)
27990 goto get_comma;
27993 return list;
27996 /* Similarly, but expect leading and trailing parenthesis. This is a very
27997 common case for omp clauses. */
27999 static tree
28000 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
28002 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28003 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
28004 return list;
28007 /* OpenACC 2.0:
28008 copy ( variable-list )
28009 copyin ( variable-list )
28010 copyout ( variable-list )
28011 create ( variable-list )
28012 delete ( variable-list )
28013 present ( variable-list )
28014 present_or_copy ( variable-list )
28015 pcopy ( variable-list )
28016 present_or_copyin ( variable-list )
28017 pcopyin ( variable-list )
28018 present_or_copyout ( variable-list )
28019 pcopyout ( variable-list )
28020 present_or_create ( variable-list )
28021 pcreate ( variable-list ) */
28023 static tree
28024 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
28025 tree list)
28027 enum gomp_map_kind kind;
28028 switch (c_kind)
28030 case PRAGMA_OACC_CLAUSE_COPY:
28031 kind = GOMP_MAP_FORCE_TOFROM;
28032 break;
28033 case PRAGMA_OACC_CLAUSE_COPYIN:
28034 kind = GOMP_MAP_FORCE_TO;
28035 break;
28036 case PRAGMA_OACC_CLAUSE_COPYOUT:
28037 kind = GOMP_MAP_FORCE_FROM;
28038 break;
28039 case PRAGMA_OACC_CLAUSE_CREATE:
28040 kind = GOMP_MAP_FORCE_ALLOC;
28041 break;
28042 case PRAGMA_OACC_CLAUSE_DELETE:
28043 kind = GOMP_MAP_FORCE_DEALLOC;
28044 break;
28045 case PRAGMA_OACC_CLAUSE_DEVICE:
28046 kind = GOMP_MAP_FORCE_TO;
28047 break;
28048 case PRAGMA_OACC_CLAUSE_HOST:
28049 case PRAGMA_OACC_CLAUSE_SELF:
28050 kind = GOMP_MAP_FORCE_FROM;
28051 break;
28052 case PRAGMA_OACC_CLAUSE_PRESENT:
28053 kind = GOMP_MAP_FORCE_PRESENT;
28054 break;
28055 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
28056 kind = GOMP_MAP_TOFROM;
28057 break;
28058 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
28059 kind = GOMP_MAP_TO;
28060 break;
28061 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
28062 kind = GOMP_MAP_FROM;
28063 break;
28064 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
28065 kind = GOMP_MAP_ALLOC;
28066 break;
28067 default:
28068 gcc_unreachable ();
28070 tree nl, c;
28071 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
28073 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
28074 OMP_CLAUSE_SET_MAP_KIND (c, kind);
28076 return nl;
28079 /* OpenACC 2.0:
28080 deviceptr ( variable-list ) */
28082 static tree
28083 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
28085 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28086 tree vars, t;
28088 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
28089 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
28090 variable-list must only allow for pointer variables. */
28091 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28092 for (t = vars; t; t = TREE_CHAIN (t))
28094 tree v = TREE_PURPOSE (t);
28096 /* FIXME diagnostics: Ideally we should keep individual
28097 locations for all the variables in the var list to make the
28098 following errors more precise. Perhaps
28099 c_parser_omp_var_list_parens should construct a list of
28100 locations to go along with the var list. */
28102 if (!VAR_P (v))
28103 error_at (loc, "%qD is not a variable", v);
28104 else if (TREE_TYPE (v) == error_mark_node)
28106 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
28107 error_at (loc, "%qD is not a pointer variable", v);
28109 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
28110 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
28111 OMP_CLAUSE_DECL (u) = v;
28112 OMP_CLAUSE_CHAIN (u) = list;
28113 list = u;
28116 return list;
28119 /* OpenACC:
28120 vector_length ( expression ) */
28122 static tree
28123 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
28125 tree t, c;
28126 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28127 bool error = false;
28129 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28130 return list;
28132 t = cp_parser_condition (parser);
28133 if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
28135 error_at (location, "expected positive integer expression");
28136 error = true;
28139 if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28141 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28142 /*or_comma=*/false,
28143 /*consume_paren=*/true);
28144 return list;
28147 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
28148 location);
28150 c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
28151 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
28152 OMP_CLAUSE_CHAIN (c) = list;
28153 list = c;
28155 return list;
28158 /* OpenACC 2.0
28159 Parse wait clause or directive parameters. */
28161 static tree
28162 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
28164 vec<tree, va_gc> *args;
28165 tree t, args_tree;
28167 args = cp_parser_parenthesized_expression_list (parser, non_attr,
28168 /*cast_p=*/false,
28169 /*allow_expansion_p=*/true,
28170 /*non_constant_p=*/NULL);
28172 if (args == NULL || args->length () == 0)
28174 cp_parser_error (parser, "expected integer expression before ')'");
28175 if (args != NULL)
28176 release_tree_vector (args);
28177 return list;
28180 args_tree = build_tree_list_vec (args);
28182 release_tree_vector (args);
28184 for (t = args_tree; t; t = TREE_CHAIN (t))
28186 tree targ = TREE_VALUE (t);
28188 if (targ != error_mark_node)
28190 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
28191 error ("%<wait%> expression must be integral");
28192 else
28194 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
28196 mark_rvalue_use (targ);
28197 OMP_CLAUSE_DECL (c) = targ;
28198 OMP_CLAUSE_CHAIN (c) = list;
28199 list = c;
28204 return list;
28207 /* OpenACC:
28208 wait ( int-expr-list ) */
28210 static tree
28211 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
28213 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28215 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
28216 return list;
28218 list = cp_parser_oacc_wait_list (parser, location, list);
28220 return list;
28223 /* OpenMP 3.0:
28224 collapse ( constant-expression ) */
28226 static tree
28227 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28229 tree c, num;
28230 location_t loc;
28231 HOST_WIDE_INT n;
28233 loc = cp_lexer_peek_token (parser->lexer)->location;
28234 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28235 return list;
28237 num = cp_parser_constant_expression (parser);
28239 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28240 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28241 /*or_comma=*/false,
28242 /*consume_paren=*/true);
28244 if (num == error_mark_node)
28245 return list;
28246 num = fold_non_dependent_expr (num);
28247 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28248 || !tree_fits_shwi_p (num)
28249 || (n = tree_to_shwi (num)) <= 0
28250 || (int) n != n)
28252 error_at (loc, "collapse argument needs positive constant integer expression");
28253 return list;
28256 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28257 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28258 OMP_CLAUSE_CHAIN (c) = list;
28259 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28261 return c;
28264 /* OpenMP 2.5:
28265 default ( shared | none ) */
28267 static tree
28268 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28270 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28271 tree c;
28273 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28274 return list;
28275 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28277 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28278 const char *p = IDENTIFIER_POINTER (id);
28280 switch (p[0])
28282 case 'n':
28283 if (strcmp ("none", p) != 0)
28284 goto invalid_kind;
28285 kind = OMP_CLAUSE_DEFAULT_NONE;
28286 break;
28288 case 's':
28289 if (strcmp ("shared", p) != 0)
28290 goto invalid_kind;
28291 kind = OMP_CLAUSE_DEFAULT_SHARED;
28292 break;
28294 default:
28295 goto invalid_kind;
28298 cp_lexer_consume_token (parser->lexer);
28300 else
28302 invalid_kind:
28303 cp_parser_error (parser, "expected %<none%> or %<shared%>");
28306 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28307 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28308 /*or_comma=*/false,
28309 /*consume_paren=*/true);
28311 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28312 return list;
28314 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28315 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28316 OMP_CLAUSE_CHAIN (c) = list;
28317 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28319 return c;
28322 /* OpenMP 3.1:
28323 final ( expression ) */
28325 static tree
28326 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28328 tree t, c;
28330 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28331 return list;
28333 t = cp_parser_condition (parser);
28335 if (t == error_mark_node
28336 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28337 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28338 /*or_comma=*/false,
28339 /*consume_paren=*/true);
28341 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28343 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28344 OMP_CLAUSE_FINAL_EXPR (c) = t;
28345 OMP_CLAUSE_CHAIN (c) = list;
28347 return c;
28350 /* OpenMP 2.5:
28351 if ( expression ) */
28353 static tree
28354 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28356 tree t, c;
28358 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28359 return list;
28361 t = cp_parser_condition (parser);
28363 if (t == error_mark_node
28364 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28365 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28366 /*or_comma=*/false,
28367 /*consume_paren=*/true);
28369 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28371 c = build_omp_clause (location, OMP_CLAUSE_IF);
28372 OMP_CLAUSE_IF_EXPR (c) = t;
28373 OMP_CLAUSE_CHAIN (c) = list;
28375 return c;
28378 /* OpenMP 3.1:
28379 mergeable */
28381 static tree
28382 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28383 tree list, location_t location)
28385 tree c;
28387 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28388 location);
28390 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28391 OMP_CLAUSE_CHAIN (c) = list;
28392 return c;
28395 /* OpenMP 2.5:
28396 nowait */
28398 static tree
28399 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28400 tree list, location_t location)
28402 tree c;
28404 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28406 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28407 OMP_CLAUSE_CHAIN (c) = list;
28408 return c;
28411 /* OpenACC:
28412 num_gangs ( expression ) */
28414 static tree
28415 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28417 tree t, c;
28418 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28420 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28421 return list;
28423 t = cp_parser_condition (parser);
28425 if (t == error_mark_node
28426 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28427 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28428 /*or_comma=*/false,
28429 /*consume_paren=*/true);
28431 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28433 error_at (location, "expected positive integer expression");
28434 return list;
28437 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28439 c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28440 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28441 OMP_CLAUSE_CHAIN (c) = list;
28442 list = c;
28444 return list;
28447 /* OpenMP 2.5:
28448 num_threads ( expression ) */
28450 static tree
28451 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28452 location_t location)
28454 tree t, c;
28456 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28457 return list;
28459 t = cp_parser_expression (parser);
28461 if (t == error_mark_node
28462 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28463 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28464 /*or_comma=*/false,
28465 /*consume_paren=*/true);
28467 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28468 "num_threads", location);
28470 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28471 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28472 OMP_CLAUSE_CHAIN (c) = list;
28474 return c;
28477 /* OpenACC:
28478 num_workers ( expression ) */
28480 static tree
28481 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28483 tree t, c;
28484 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28486 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28487 return list;
28489 t = cp_parser_condition (parser);
28491 if (t == error_mark_node
28492 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28493 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28494 /*or_comma=*/false,
28495 /*consume_paren=*/true);
28497 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28499 error_at (location, "expected positive integer expression");
28500 return list;
28503 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28504 location);
28506 c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28507 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28508 OMP_CLAUSE_CHAIN (c) = list;
28509 list = c;
28511 return list;
28514 /* OpenMP 2.5:
28515 ordered */
28517 static tree
28518 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28519 tree list, location_t location)
28521 tree c;
28523 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28524 "ordered", location);
28526 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28527 OMP_CLAUSE_CHAIN (c) = list;
28528 return c;
28531 /* OpenMP 2.5:
28532 reduction ( reduction-operator : variable-list )
28534 reduction-operator:
28535 One of: + * - & ^ | && ||
28537 OpenMP 3.1:
28539 reduction-operator:
28540 One of: + * - & ^ | && || min max
28542 OpenMP 4.0:
28544 reduction-operator:
28545 One of: + * - & ^ | && ||
28546 id-expression */
28548 static tree
28549 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28551 enum tree_code code = ERROR_MARK;
28552 tree nlist, c, id = NULL_TREE;
28554 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28555 return list;
28557 switch (cp_lexer_peek_token (parser->lexer)->type)
28559 case CPP_PLUS: code = PLUS_EXPR; break;
28560 case CPP_MULT: code = MULT_EXPR; break;
28561 case CPP_MINUS: code = MINUS_EXPR; break;
28562 case CPP_AND: code = BIT_AND_EXPR; break;
28563 case CPP_XOR: code = BIT_XOR_EXPR; break;
28564 case CPP_OR: code = BIT_IOR_EXPR; break;
28565 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28566 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28567 default: break;
28570 if (code != ERROR_MARK)
28571 cp_lexer_consume_token (parser->lexer);
28572 else
28574 bool saved_colon_corrects_to_scope_p;
28575 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28576 parser->colon_corrects_to_scope_p = false;
28577 id = cp_parser_id_expression (parser, /*template_p=*/false,
28578 /*check_dependency_p=*/true,
28579 /*template_p=*/NULL,
28580 /*declarator_p=*/false,
28581 /*optional_p=*/false);
28582 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28583 if (identifier_p (id))
28585 const char *p = IDENTIFIER_POINTER (id);
28587 if (strcmp (p, "min") == 0)
28588 code = MIN_EXPR;
28589 else if (strcmp (p, "max") == 0)
28590 code = MAX_EXPR;
28591 else if (id == ansi_opname (PLUS_EXPR))
28592 code = PLUS_EXPR;
28593 else if (id == ansi_opname (MULT_EXPR))
28594 code = MULT_EXPR;
28595 else if (id == ansi_opname (MINUS_EXPR))
28596 code = MINUS_EXPR;
28597 else if (id == ansi_opname (BIT_AND_EXPR))
28598 code = BIT_AND_EXPR;
28599 else if (id == ansi_opname (BIT_IOR_EXPR))
28600 code = BIT_IOR_EXPR;
28601 else if (id == ansi_opname (BIT_XOR_EXPR))
28602 code = BIT_XOR_EXPR;
28603 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28604 code = TRUTH_ANDIF_EXPR;
28605 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28606 code = TRUTH_ORIF_EXPR;
28607 id = omp_reduction_id (code, id, NULL_TREE);
28608 tree scope = parser->scope;
28609 if (scope)
28610 id = build_qualified_name (NULL_TREE, scope, id, false);
28611 parser->scope = NULL_TREE;
28612 parser->qualifying_scope = NULL_TREE;
28613 parser->object_scope = NULL_TREE;
28615 else
28617 error ("invalid reduction-identifier");
28618 resync_fail:
28619 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28620 /*or_comma=*/false,
28621 /*consume_paren=*/true);
28622 return list;
28626 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28627 goto resync_fail;
28629 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28630 NULL);
28631 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28633 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28634 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28637 return nlist;
28640 /* OpenMP 2.5:
28641 schedule ( schedule-kind )
28642 schedule ( schedule-kind , expression )
28644 schedule-kind:
28645 static | dynamic | guided | runtime | auto */
28647 static tree
28648 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28650 tree c, t;
28652 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28653 return list;
28655 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28657 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28659 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28660 const char *p = IDENTIFIER_POINTER (id);
28662 switch (p[0])
28664 case 'd':
28665 if (strcmp ("dynamic", p) != 0)
28666 goto invalid_kind;
28667 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28668 break;
28670 case 'g':
28671 if (strcmp ("guided", p) != 0)
28672 goto invalid_kind;
28673 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28674 break;
28676 case 'r':
28677 if (strcmp ("runtime", p) != 0)
28678 goto invalid_kind;
28679 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28680 break;
28682 default:
28683 goto invalid_kind;
28686 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28687 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28688 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28689 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28690 else
28691 goto invalid_kind;
28692 cp_lexer_consume_token (parser->lexer);
28694 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28696 cp_token *token;
28697 cp_lexer_consume_token (parser->lexer);
28699 token = cp_lexer_peek_token (parser->lexer);
28700 t = cp_parser_assignment_expression (parser);
28702 if (t == error_mark_node)
28703 goto resync_fail;
28704 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28705 error_at (token->location, "schedule %<runtime%> does not take "
28706 "a %<chunk_size%> parameter");
28707 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28708 error_at (token->location, "schedule %<auto%> does not take "
28709 "a %<chunk_size%> parameter");
28710 else
28711 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28713 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28714 goto resync_fail;
28716 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28717 goto resync_fail;
28719 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28720 OMP_CLAUSE_CHAIN (c) = list;
28721 return c;
28723 invalid_kind:
28724 cp_parser_error (parser, "invalid schedule kind");
28725 resync_fail:
28726 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28727 /*or_comma=*/false,
28728 /*consume_paren=*/true);
28729 return list;
28732 /* OpenMP 3.0:
28733 untied */
28735 static tree
28736 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28737 tree list, location_t location)
28739 tree c;
28741 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28743 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28744 OMP_CLAUSE_CHAIN (c) = list;
28745 return c;
28748 /* OpenMP 4.0:
28749 inbranch
28750 notinbranch */
28752 static tree
28753 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28754 tree list, location_t location)
28756 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28757 tree c = build_omp_clause (location, code);
28758 OMP_CLAUSE_CHAIN (c) = list;
28759 return c;
28762 /* OpenMP 4.0:
28763 parallel
28765 sections
28766 taskgroup */
28768 static tree
28769 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28770 enum omp_clause_code code,
28771 tree list, location_t location)
28773 tree c = build_omp_clause (location, code);
28774 OMP_CLAUSE_CHAIN (c) = list;
28775 return c;
28778 /* OpenMP 4.0:
28779 num_teams ( expression ) */
28781 static tree
28782 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28783 location_t location)
28785 tree t, c;
28787 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28788 return list;
28790 t = cp_parser_expression (parser);
28792 if (t == error_mark_node
28793 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28794 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28795 /*or_comma=*/false,
28796 /*consume_paren=*/true);
28798 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28799 "num_teams", location);
28801 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28802 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28803 OMP_CLAUSE_CHAIN (c) = list;
28805 return c;
28808 /* OpenMP 4.0:
28809 thread_limit ( expression ) */
28811 static tree
28812 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28813 location_t location)
28815 tree t, c;
28817 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28818 return list;
28820 t = cp_parser_expression (parser);
28822 if (t == error_mark_node
28823 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28824 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28825 /*or_comma=*/false,
28826 /*consume_paren=*/true);
28828 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28829 "thread_limit", location);
28831 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28832 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28833 OMP_CLAUSE_CHAIN (c) = list;
28835 return c;
28838 /* OpenMP 4.0:
28839 aligned ( variable-list )
28840 aligned ( variable-list : constant-expression ) */
28842 static tree
28843 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28845 tree nlist, c, alignment = NULL_TREE;
28846 bool colon;
28848 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28849 return list;
28851 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28852 &colon);
28854 if (colon)
28856 alignment = cp_parser_constant_expression (parser);
28858 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28859 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28860 /*or_comma=*/false,
28861 /*consume_paren=*/true);
28863 if (alignment == error_mark_node)
28864 alignment = NULL_TREE;
28867 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28868 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28870 return nlist;
28873 /* OpenMP 4.0:
28874 linear ( variable-list )
28875 linear ( variable-list : expression ) */
28877 static tree
28878 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28879 bool is_cilk_simd_fn)
28881 tree nlist, c, step = integer_one_node;
28882 bool colon;
28884 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28885 return list;
28887 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28888 &colon);
28890 if (colon)
28892 step = cp_parser_expression (parser);
28894 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28896 sorry ("using parameters for %<linear%> step is not supported yet");
28897 step = integer_one_node;
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 (step == error_mark_node)
28905 return list;
28908 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28909 OMP_CLAUSE_LINEAR_STEP (c) = step;
28911 return nlist;
28914 /* OpenMP 4.0:
28915 safelen ( constant-expression ) */
28917 static tree
28918 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28919 location_t location)
28921 tree t, c;
28923 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28924 return list;
28926 t = cp_parser_constant_expression (parser);
28928 if (t == error_mark_node
28929 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28930 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28931 /*or_comma=*/false,
28932 /*consume_paren=*/true);
28934 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28936 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28937 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28938 OMP_CLAUSE_CHAIN (c) = list;
28940 return c;
28943 /* OpenMP 4.0:
28944 simdlen ( constant-expression ) */
28946 static tree
28947 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28948 location_t location)
28950 tree t, c;
28952 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28953 return list;
28955 t = cp_parser_constant_expression (parser);
28957 if (t == error_mark_node
28958 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28959 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28960 /*or_comma=*/false,
28961 /*consume_paren=*/true);
28963 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28965 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28966 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28967 OMP_CLAUSE_CHAIN (c) = list;
28969 return c;
28972 /* OpenMP 4.0:
28973 depend ( depend-kind : variable-list )
28975 depend-kind:
28976 in | out | inout */
28978 static tree
28979 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28981 tree nlist, c;
28982 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28984 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28985 return list;
28987 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28989 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28990 const char *p = IDENTIFIER_POINTER (id);
28992 if (strcmp ("in", p) == 0)
28993 kind = OMP_CLAUSE_DEPEND_IN;
28994 else if (strcmp ("inout", p) == 0)
28995 kind = OMP_CLAUSE_DEPEND_INOUT;
28996 else if (strcmp ("out", p) == 0)
28997 kind = OMP_CLAUSE_DEPEND_OUT;
28998 else
28999 goto invalid_kind;
29001 else
29002 goto invalid_kind;
29004 cp_lexer_consume_token (parser->lexer);
29005 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29006 goto resync_fail;
29008 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
29009 NULL);
29011 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29012 OMP_CLAUSE_DEPEND_KIND (c) = kind;
29014 return nlist;
29016 invalid_kind:
29017 cp_parser_error (parser, "invalid depend kind");
29018 resync_fail:
29019 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29020 /*or_comma=*/false,
29021 /*consume_paren=*/true);
29022 return list;
29025 /* OpenMP 4.0:
29026 map ( map-kind : variable-list )
29027 map ( variable-list )
29029 map-kind:
29030 alloc | to | from | tofrom */
29032 static tree
29033 cp_parser_omp_clause_map (cp_parser *parser, tree list)
29035 tree nlist, c;
29036 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
29038 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29039 return list;
29041 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
29042 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
29044 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29045 const char *p = IDENTIFIER_POINTER (id);
29047 if (strcmp ("alloc", p) == 0)
29048 kind = GOMP_MAP_ALLOC;
29049 else if (strcmp ("to", p) == 0)
29050 kind = GOMP_MAP_TO;
29051 else if (strcmp ("from", p) == 0)
29052 kind = GOMP_MAP_FROM;
29053 else if (strcmp ("tofrom", p) == 0)
29054 kind = GOMP_MAP_TOFROM;
29055 else
29057 cp_parser_error (parser, "invalid map kind");
29058 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29059 /*or_comma=*/false,
29060 /*consume_paren=*/true);
29061 return list;
29063 cp_lexer_consume_token (parser->lexer);
29064 cp_lexer_consume_token (parser->lexer);
29067 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
29068 NULL);
29070 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29071 OMP_CLAUSE_SET_MAP_KIND (c, kind);
29073 return nlist;
29076 /* OpenMP 4.0:
29077 device ( expression ) */
29079 static tree
29080 cp_parser_omp_clause_device (cp_parser *parser, tree list,
29081 location_t location)
29083 tree t, c;
29085 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29086 return list;
29088 t = cp_parser_expression (parser);
29090 if (t == error_mark_node
29091 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29092 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29093 /*or_comma=*/false,
29094 /*consume_paren=*/true);
29096 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
29097 "device", location);
29099 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
29100 OMP_CLAUSE_DEVICE_ID (c) = t;
29101 OMP_CLAUSE_CHAIN (c) = list;
29103 return c;
29106 /* OpenMP 4.0:
29107 dist_schedule ( static )
29108 dist_schedule ( static , expression ) */
29110 static tree
29111 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
29112 location_t location)
29114 tree c, t;
29116 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29117 return list;
29119 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
29121 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29122 goto invalid_kind;
29123 cp_lexer_consume_token (parser->lexer);
29125 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29127 cp_lexer_consume_token (parser->lexer);
29129 t = cp_parser_assignment_expression (parser);
29131 if (t == error_mark_node)
29132 goto resync_fail;
29133 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
29135 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29136 goto resync_fail;
29138 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29139 goto resync_fail;
29141 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
29142 location);
29143 OMP_CLAUSE_CHAIN (c) = list;
29144 return c;
29146 invalid_kind:
29147 cp_parser_error (parser, "invalid dist_schedule kind");
29148 resync_fail:
29149 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29150 /*or_comma=*/false,
29151 /*consume_paren=*/true);
29152 return list;
29155 /* OpenMP 4.0:
29156 proc_bind ( proc-bind-kind )
29158 proc-bind-kind:
29159 master | close | spread */
29161 static tree
29162 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
29163 location_t location)
29165 tree c;
29166 enum omp_clause_proc_bind_kind kind;
29168 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29169 return list;
29171 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29173 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29174 const char *p = IDENTIFIER_POINTER (id);
29176 if (strcmp ("master", p) == 0)
29177 kind = OMP_CLAUSE_PROC_BIND_MASTER;
29178 else if (strcmp ("close", p) == 0)
29179 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
29180 else if (strcmp ("spread", p) == 0)
29181 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
29182 else
29183 goto invalid_kind;
29185 else
29186 goto invalid_kind;
29188 cp_lexer_consume_token (parser->lexer);
29189 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29190 goto resync_fail;
29192 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29193 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29194 location);
29195 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29196 OMP_CLAUSE_CHAIN (c) = list;
29197 return c;
29199 invalid_kind:
29200 cp_parser_error (parser, "invalid depend kind");
29201 resync_fail:
29202 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29203 /*or_comma=*/false,
29204 /*consume_paren=*/true);
29205 return list;
29208 /* OpenACC:
29209 async [( int-expr )] */
29211 static tree
29212 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29214 tree c, t;
29215 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29217 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
29219 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29221 cp_lexer_consume_token (parser->lexer);
29223 t = cp_parser_expression (parser);
29224 if (t == error_mark_node
29225 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29226 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29227 /*or_comma=*/false,
29228 /*consume_paren=*/true);
29231 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29233 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29234 OMP_CLAUSE_ASYNC_EXPR (c) = t;
29235 OMP_CLAUSE_CHAIN (c) = list;
29236 list = c;
29238 return list;
29241 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29242 is a bitmask in MASK. Return the list of clauses found. */
29244 static tree
29245 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29246 const char *where, cp_token *pragma_tok,
29247 bool finish_p = true)
29249 tree clauses = NULL;
29250 bool first = true;
29252 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29254 location_t here;
29255 pragma_omp_clause c_kind;
29256 const char *c_name;
29257 tree prev = clauses;
29259 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29260 cp_lexer_consume_token (parser->lexer);
29262 here = cp_lexer_peek_token (parser->lexer)->location;
29263 c_kind = cp_parser_omp_clause_name (parser);
29265 switch (c_kind)
29267 case PRAGMA_OACC_CLAUSE_ASYNC:
29268 clauses = cp_parser_oacc_clause_async (parser, clauses);
29269 c_name = "async";
29270 break;
29271 case PRAGMA_OACC_CLAUSE_COLLAPSE:
29272 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29273 c_name = "collapse";
29274 break;
29275 case PRAGMA_OACC_CLAUSE_COPY:
29276 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29277 c_name = "copy";
29278 break;
29279 case PRAGMA_OACC_CLAUSE_COPYIN:
29280 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29281 c_name = "copyin";
29282 break;
29283 case PRAGMA_OACC_CLAUSE_COPYOUT:
29284 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29285 c_name = "copyout";
29286 break;
29287 case PRAGMA_OACC_CLAUSE_CREATE:
29288 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29289 c_name = "create";
29290 break;
29291 case PRAGMA_OACC_CLAUSE_DELETE:
29292 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29293 c_name = "delete";
29294 break;
29295 case PRAGMA_OACC_CLAUSE_DEVICE:
29296 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29297 c_name = "device";
29298 break;
29299 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
29300 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29301 c_name = "deviceptr";
29302 break;
29303 case PRAGMA_OACC_CLAUSE_HOST:
29304 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29305 c_name = "host";
29306 break;
29307 case PRAGMA_OACC_CLAUSE_IF:
29308 clauses = cp_parser_omp_clause_if (parser, clauses, here);
29309 c_name = "if";
29310 break;
29311 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
29312 clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29313 c_name = "num_gangs";
29314 break;
29315 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
29316 clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29317 c_name = "num_workers";
29318 break;
29319 case PRAGMA_OACC_CLAUSE_PRESENT:
29320 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29321 c_name = "present";
29322 break;
29323 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29324 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29325 c_name = "present_or_copy";
29326 break;
29327 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29328 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29329 c_name = "present_or_copyin";
29330 break;
29331 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29332 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29333 c_name = "present_or_copyout";
29334 break;
29335 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29336 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29337 c_name = "present_or_create";
29338 break;
29339 case PRAGMA_OACC_CLAUSE_REDUCTION:
29340 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29341 c_name = "reduction";
29342 break;
29343 case PRAGMA_OACC_CLAUSE_SELF:
29344 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29345 c_name = "self";
29346 break;
29347 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
29348 clauses = cp_parser_oacc_clause_vector_length (parser, clauses);
29349 c_name = "vector_length";
29350 break;
29351 case PRAGMA_OACC_CLAUSE_WAIT:
29352 clauses = cp_parser_oacc_clause_wait (parser, clauses);
29353 c_name = "wait";
29354 break;
29355 default:
29356 cp_parser_error (parser, "expected %<#pragma acc%> clause");
29357 goto saw_error;
29360 first = false;
29362 if (((mask >> c_kind) & 1) == 0)
29364 /* Remove the invalid clause(s) from the list to avoid
29365 confusing the rest of the compiler. */
29366 clauses = prev;
29367 error_at (here, "%qs is not valid for %qs", c_name, where);
29371 saw_error:
29372 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29374 if (finish_p)
29375 return finish_omp_clauses (clauses);
29377 return clauses;
29380 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29381 is a bitmask in MASK. Return the list of clauses found; the result
29382 of clause default goes in *pdefault. */
29384 static tree
29385 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29386 const char *where, cp_token *pragma_tok,
29387 bool finish_p = true)
29389 tree clauses = NULL;
29390 bool first = true;
29391 cp_token *token = NULL;
29392 bool cilk_simd_fn = false;
29394 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29396 pragma_omp_clause c_kind;
29397 const char *c_name;
29398 tree prev = clauses;
29400 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29401 cp_lexer_consume_token (parser->lexer);
29403 token = cp_lexer_peek_token (parser->lexer);
29404 c_kind = cp_parser_omp_clause_name (parser);
29406 switch (c_kind)
29408 case PRAGMA_OMP_CLAUSE_COLLAPSE:
29409 clauses = cp_parser_omp_clause_collapse (parser, clauses,
29410 token->location);
29411 c_name = "collapse";
29412 break;
29413 case PRAGMA_OMP_CLAUSE_COPYIN:
29414 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29415 c_name = "copyin";
29416 break;
29417 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29418 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29419 clauses);
29420 c_name = "copyprivate";
29421 break;
29422 case PRAGMA_OMP_CLAUSE_DEFAULT:
29423 clauses = cp_parser_omp_clause_default (parser, clauses,
29424 token->location);
29425 c_name = "default";
29426 break;
29427 case PRAGMA_OMP_CLAUSE_FINAL:
29428 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29429 c_name = "final";
29430 break;
29431 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29432 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29433 clauses);
29434 c_name = "firstprivate";
29435 break;
29436 case PRAGMA_OMP_CLAUSE_IF:
29437 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29438 c_name = "if";
29439 break;
29440 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29441 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29442 clauses);
29443 c_name = "lastprivate";
29444 break;
29445 case PRAGMA_OMP_CLAUSE_MERGEABLE:
29446 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29447 token->location);
29448 c_name = "mergeable";
29449 break;
29450 case PRAGMA_OMP_CLAUSE_NOWAIT:
29451 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29452 c_name = "nowait";
29453 break;
29454 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29455 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29456 token->location);
29457 c_name = "num_threads";
29458 break;
29459 case PRAGMA_OMP_CLAUSE_ORDERED:
29460 clauses = cp_parser_omp_clause_ordered (parser, clauses,
29461 token->location);
29462 c_name = "ordered";
29463 break;
29464 case PRAGMA_OMP_CLAUSE_PRIVATE:
29465 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29466 clauses);
29467 c_name = "private";
29468 break;
29469 case PRAGMA_OMP_CLAUSE_REDUCTION:
29470 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29471 c_name = "reduction";
29472 break;
29473 case PRAGMA_OMP_CLAUSE_SCHEDULE:
29474 clauses = cp_parser_omp_clause_schedule (parser, clauses,
29475 token->location);
29476 c_name = "schedule";
29477 break;
29478 case PRAGMA_OMP_CLAUSE_SHARED:
29479 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29480 clauses);
29481 c_name = "shared";
29482 break;
29483 case PRAGMA_OMP_CLAUSE_UNTIED:
29484 clauses = cp_parser_omp_clause_untied (parser, clauses,
29485 token->location);
29486 c_name = "untied";
29487 break;
29488 case PRAGMA_OMP_CLAUSE_INBRANCH:
29489 case PRAGMA_CILK_CLAUSE_MASK:
29490 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29491 clauses, token->location);
29492 c_name = "inbranch";
29493 break;
29494 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29495 case PRAGMA_CILK_CLAUSE_NOMASK:
29496 clauses = cp_parser_omp_clause_branch (parser,
29497 OMP_CLAUSE_NOTINBRANCH,
29498 clauses, token->location);
29499 c_name = "notinbranch";
29500 break;
29501 case PRAGMA_OMP_CLAUSE_PARALLEL:
29502 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29503 clauses, token->location);
29504 c_name = "parallel";
29505 if (!first)
29507 clause_not_first:
29508 error_at (token->location, "%qs must be the first clause of %qs",
29509 c_name, where);
29510 clauses = prev;
29512 break;
29513 case PRAGMA_OMP_CLAUSE_FOR:
29514 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29515 clauses, token->location);
29516 c_name = "for";
29517 if (!first)
29518 goto clause_not_first;
29519 break;
29520 case PRAGMA_OMP_CLAUSE_SECTIONS:
29521 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29522 clauses, token->location);
29523 c_name = "sections";
29524 if (!first)
29525 goto clause_not_first;
29526 break;
29527 case PRAGMA_OMP_CLAUSE_TASKGROUP:
29528 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29529 clauses, token->location);
29530 c_name = "taskgroup";
29531 if (!first)
29532 goto clause_not_first;
29533 break;
29534 case PRAGMA_OMP_CLAUSE_TO:
29535 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29536 clauses);
29537 c_name = "to";
29538 break;
29539 case PRAGMA_OMP_CLAUSE_FROM:
29540 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29541 clauses);
29542 c_name = "from";
29543 break;
29544 case PRAGMA_OMP_CLAUSE_UNIFORM:
29545 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29546 clauses);
29547 c_name = "uniform";
29548 break;
29549 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29550 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29551 token->location);
29552 c_name = "num_teams";
29553 break;
29554 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29555 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29556 token->location);
29557 c_name = "thread_limit";
29558 break;
29559 case PRAGMA_OMP_CLAUSE_ALIGNED:
29560 clauses = cp_parser_omp_clause_aligned (parser, clauses);
29561 c_name = "aligned";
29562 break;
29563 case PRAGMA_OMP_CLAUSE_LINEAR:
29564 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29565 cilk_simd_fn = true;
29566 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29567 c_name = "linear";
29568 break;
29569 case PRAGMA_OMP_CLAUSE_DEPEND:
29570 clauses = cp_parser_omp_clause_depend (parser, clauses);
29571 c_name = "depend";
29572 break;
29573 case PRAGMA_OMP_CLAUSE_MAP:
29574 clauses = cp_parser_omp_clause_map (parser, clauses);
29575 c_name = "map";
29576 break;
29577 case PRAGMA_OMP_CLAUSE_DEVICE:
29578 clauses = cp_parser_omp_clause_device (parser, clauses,
29579 token->location);
29580 c_name = "device";
29581 break;
29582 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29583 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29584 token->location);
29585 c_name = "dist_schedule";
29586 break;
29587 case PRAGMA_OMP_CLAUSE_PROC_BIND:
29588 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29589 token->location);
29590 c_name = "proc_bind";
29591 break;
29592 case PRAGMA_OMP_CLAUSE_SAFELEN:
29593 clauses = cp_parser_omp_clause_safelen (parser, clauses,
29594 token->location);
29595 c_name = "safelen";
29596 break;
29597 case PRAGMA_OMP_CLAUSE_SIMDLEN:
29598 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29599 token->location);
29600 c_name = "simdlen";
29601 break;
29602 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29603 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29604 c_name = "simdlen";
29605 break;
29606 default:
29607 cp_parser_error (parser, "expected %<#pragma omp%> clause");
29608 goto saw_error;
29611 first = false;
29613 if (((mask >> c_kind) & 1) == 0)
29615 /* Remove the invalid clause(s) from the list to avoid
29616 confusing the rest of the compiler. */
29617 clauses = prev;
29618 error_at (token->location, "%qs is not valid for %qs", c_name, where);
29621 saw_error:
29622 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29623 no reason to skip to the end. */
29624 if (!(flag_cilkplus && pragma_tok == NULL))
29625 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29626 if (finish_p)
29627 return finish_omp_clauses (clauses);
29628 return clauses;
29631 /* OpenMP 2.5:
29632 structured-block:
29633 statement
29635 In practice, we're also interested in adding the statement to an
29636 outer node. So it is convenient if we work around the fact that
29637 cp_parser_statement calls add_stmt. */
29639 static unsigned
29640 cp_parser_begin_omp_structured_block (cp_parser *parser)
29642 unsigned save = parser->in_statement;
29644 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29645 This preserves the "not within loop or switch" style error messages
29646 for nonsense cases like
29647 void foo() {
29648 #pragma omp single
29649 break;
29652 if (parser->in_statement)
29653 parser->in_statement = IN_OMP_BLOCK;
29655 return save;
29658 static void
29659 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29661 parser->in_statement = save;
29664 static tree
29665 cp_parser_omp_structured_block (cp_parser *parser)
29667 tree stmt = begin_omp_structured_block ();
29668 unsigned int save = cp_parser_begin_omp_structured_block (parser);
29670 cp_parser_statement (parser, NULL_TREE, false, NULL);
29672 cp_parser_end_omp_structured_block (parser, save);
29673 return finish_omp_structured_block (stmt);
29676 /* OpenMP 2.5:
29677 # pragma omp atomic new-line
29678 expression-stmt
29680 expression-stmt:
29681 x binop= expr | x++ | ++x | x-- | --x
29682 binop:
29683 +, *, -, /, &, ^, |, <<, >>
29685 where x is an lvalue expression with scalar type.
29687 OpenMP 3.1:
29688 # pragma omp atomic new-line
29689 update-stmt
29691 # pragma omp atomic read new-line
29692 read-stmt
29694 # pragma omp atomic write new-line
29695 write-stmt
29697 # pragma omp atomic update new-line
29698 update-stmt
29700 # pragma omp atomic capture new-line
29701 capture-stmt
29703 # pragma omp atomic capture new-line
29704 capture-block
29706 read-stmt:
29707 v = x
29708 write-stmt:
29709 x = expr
29710 update-stmt:
29711 expression-stmt | x = x binop expr
29712 capture-stmt:
29713 v = expression-stmt
29714 capture-block:
29715 { v = x; update-stmt; } | { update-stmt; v = x; }
29717 OpenMP 4.0:
29718 update-stmt:
29719 expression-stmt | x = x binop expr | x = expr binop x
29720 capture-stmt:
29721 v = update-stmt
29722 capture-block:
29723 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29725 where x and v are lvalue expressions with scalar type. */
29727 static void
29728 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29730 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29731 tree rhs1 = NULL_TREE, orig_lhs;
29732 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29733 bool structured_block = false;
29734 bool seq_cst = false;
29736 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29738 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29739 const char *p = IDENTIFIER_POINTER (id);
29741 if (!strcmp (p, "seq_cst"))
29743 seq_cst = true;
29744 cp_lexer_consume_token (parser->lexer);
29745 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29746 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29747 cp_lexer_consume_token (parser->lexer);
29750 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29752 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29753 const char *p = IDENTIFIER_POINTER (id);
29755 if (!strcmp (p, "read"))
29756 code = OMP_ATOMIC_READ;
29757 else if (!strcmp (p, "write"))
29758 code = NOP_EXPR;
29759 else if (!strcmp (p, "update"))
29760 code = OMP_ATOMIC;
29761 else if (!strcmp (p, "capture"))
29762 code = OMP_ATOMIC_CAPTURE_NEW;
29763 else
29764 p = NULL;
29765 if (p)
29766 cp_lexer_consume_token (parser->lexer);
29768 if (!seq_cst)
29770 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29771 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29772 cp_lexer_consume_token (parser->lexer);
29774 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29776 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29777 const char *p = IDENTIFIER_POINTER (id);
29779 if (!strcmp (p, "seq_cst"))
29781 seq_cst = true;
29782 cp_lexer_consume_token (parser->lexer);
29786 cp_parser_require_pragma_eol (parser, pragma_tok);
29788 switch (code)
29790 case OMP_ATOMIC_READ:
29791 case NOP_EXPR: /* atomic write */
29792 v = cp_parser_unary_expression (parser);
29793 if (v == error_mark_node)
29794 goto saw_error;
29795 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29796 goto saw_error;
29797 if (code == NOP_EXPR)
29798 lhs = cp_parser_expression (parser);
29799 else
29800 lhs = cp_parser_unary_expression (parser);
29801 if (lhs == error_mark_node)
29802 goto saw_error;
29803 if (code == NOP_EXPR)
29805 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29806 opcode. */
29807 code = OMP_ATOMIC;
29808 rhs = lhs;
29809 lhs = v;
29810 v = NULL_TREE;
29812 goto done;
29813 case OMP_ATOMIC_CAPTURE_NEW:
29814 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29816 cp_lexer_consume_token (parser->lexer);
29817 structured_block = true;
29819 else
29821 v = cp_parser_unary_expression (parser);
29822 if (v == error_mark_node)
29823 goto saw_error;
29824 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29825 goto saw_error;
29827 default:
29828 break;
29831 restart:
29832 lhs = cp_parser_unary_expression (parser);
29833 orig_lhs = lhs;
29834 switch (TREE_CODE (lhs))
29836 case ERROR_MARK:
29837 goto saw_error;
29839 case POSTINCREMENT_EXPR:
29840 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29841 code = OMP_ATOMIC_CAPTURE_OLD;
29842 /* FALLTHROUGH */
29843 case PREINCREMENT_EXPR:
29844 lhs = TREE_OPERAND (lhs, 0);
29845 opcode = PLUS_EXPR;
29846 rhs = integer_one_node;
29847 break;
29849 case POSTDECREMENT_EXPR:
29850 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29851 code = OMP_ATOMIC_CAPTURE_OLD;
29852 /* FALLTHROUGH */
29853 case PREDECREMENT_EXPR:
29854 lhs = TREE_OPERAND (lhs, 0);
29855 opcode = MINUS_EXPR;
29856 rhs = integer_one_node;
29857 break;
29859 case COMPOUND_EXPR:
29860 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29861 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29862 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29863 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29864 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29865 (TREE_OPERAND (lhs, 1), 0), 0)))
29866 == BOOLEAN_TYPE)
29867 /* Undo effects of boolean_increment for post {in,de}crement. */
29868 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29869 /* FALLTHRU */
29870 case MODIFY_EXPR:
29871 if (TREE_CODE (lhs) == MODIFY_EXPR
29872 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29874 /* Undo effects of boolean_increment. */
29875 if (integer_onep (TREE_OPERAND (lhs, 1)))
29877 /* This is pre or post increment. */
29878 rhs = TREE_OPERAND (lhs, 1);
29879 lhs = TREE_OPERAND (lhs, 0);
29880 opcode = NOP_EXPR;
29881 if (code == OMP_ATOMIC_CAPTURE_NEW
29882 && !structured_block
29883 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29884 code = OMP_ATOMIC_CAPTURE_OLD;
29885 break;
29888 /* FALLTHRU */
29889 default:
29890 switch (cp_lexer_peek_token (parser->lexer)->type)
29892 case CPP_MULT_EQ:
29893 opcode = MULT_EXPR;
29894 break;
29895 case CPP_DIV_EQ:
29896 opcode = TRUNC_DIV_EXPR;
29897 break;
29898 case CPP_PLUS_EQ:
29899 opcode = PLUS_EXPR;
29900 break;
29901 case CPP_MINUS_EQ:
29902 opcode = MINUS_EXPR;
29903 break;
29904 case CPP_LSHIFT_EQ:
29905 opcode = LSHIFT_EXPR;
29906 break;
29907 case CPP_RSHIFT_EQ:
29908 opcode = RSHIFT_EXPR;
29909 break;
29910 case CPP_AND_EQ:
29911 opcode = BIT_AND_EXPR;
29912 break;
29913 case CPP_OR_EQ:
29914 opcode = BIT_IOR_EXPR;
29915 break;
29916 case CPP_XOR_EQ:
29917 opcode = BIT_XOR_EXPR;
29918 break;
29919 case CPP_EQ:
29920 enum cp_parser_prec oprec;
29921 cp_token *token;
29922 cp_lexer_consume_token (parser->lexer);
29923 cp_parser_parse_tentatively (parser);
29924 rhs1 = cp_parser_simple_cast_expression (parser);
29925 if (rhs1 == error_mark_node)
29927 cp_parser_abort_tentative_parse (parser);
29928 cp_parser_simple_cast_expression (parser);
29929 goto saw_error;
29931 token = cp_lexer_peek_token (parser->lexer);
29932 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29934 cp_parser_abort_tentative_parse (parser);
29935 cp_parser_parse_tentatively (parser);
29936 rhs = cp_parser_binary_expression (parser, false, true,
29937 PREC_NOT_OPERATOR, NULL);
29938 if (rhs == error_mark_node)
29940 cp_parser_abort_tentative_parse (parser);
29941 cp_parser_binary_expression (parser, false, true,
29942 PREC_NOT_OPERATOR, NULL);
29943 goto saw_error;
29945 switch (TREE_CODE (rhs))
29947 case MULT_EXPR:
29948 case TRUNC_DIV_EXPR:
29949 case RDIV_EXPR:
29950 case PLUS_EXPR:
29951 case MINUS_EXPR:
29952 case LSHIFT_EXPR:
29953 case RSHIFT_EXPR:
29954 case BIT_AND_EXPR:
29955 case BIT_IOR_EXPR:
29956 case BIT_XOR_EXPR:
29957 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29959 if (cp_parser_parse_definitely (parser))
29961 opcode = TREE_CODE (rhs);
29962 rhs1 = TREE_OPERAND (rhs, 0);
29963 rhs = TREE_OPERAND (rhs, 1);
29964 goto stmt_done;
29966 else
29967 goto saw_error;
29969 break;
29970 default:
29971 break;
29973 cp_parser_abort_tentative_parse (parser);
29974 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29976 rhs = cp_parser_expression (parser);
29977 if (rhs == error_mark_node)
29978 goto saw_error;
29979 opcode = NOP_EXPR;
29980 rhs1 = NULL_TREE;
29981 goto stmt_done;
29983 cp_parser_error (parser,
29984 "invalid form of %<#pragma omp atomic%>");
29985 goto saw_error;
29987 if (!cp_parser_parse_definitely (parser))
29988 goto saw_error;
29989 switch (token->type)
29991 case CPP_SEMICOLON:
29992 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29994 code = OMP_ATOMIC_CAPTURE_OLD;
29995 v = lhs;
29996 lhs = NULL_TREE;
29997 lhs1 = rhs1;
29998 rhs1 = NULL_TREE;
29999 cp_lexer_consume_token (parser->lexer);
30000 goto restart;
30002 else if (structured_block)
30004 opcode = NOP_EXPR;
30005 rhs = rhs1;
30006 rhs1 = NULL_TREE;
30007 goto stmt_done;
30009 cp_parser_error (parser,
30010 "invalid form of %<#pragma omp atomic%>");
30011 goto saw_error;
30012 case CPP_MULT:
30013 opcode = MULT_EXPR;
30014 break;
30015 case CPP_DIV:
30016 opcode = TRUNC_DIV_EXPR;
30017 break;
30018 case CPP_PLUS:
30019 opcode = PLUS_EXPR;
30020 break;
30021 case CPP_MINUS:
30022 opcode = MINUS_EXPR;
30023 break;
30024 case CPP_LSHIFT:
30025 opcode = LSHIFT_EXPR;
30026 break;
30027 case CPP_RSHIFT:
30028 opcode = RSHIFT_EXPR;
30029 break;
30030 case CPP_AND:
30031 opcode = BIT_AND_EXPR;
30032 break;
30033 case CPP_OR:
30034 opcode = BIT_IOR_EXPR;
30035 break;
30036 case CPP_XOR:
30037 opcode = BIT_XOR_EXPR;
30038 break;
30039 default:
30040 cp_parser_error (parser,
30041 "invalid operator for %<#pragma omp atomic%>");
30042 goto saw_error;
30044 oprec = TOKEN_PRECEDENCE (token);
30045 gcc_assert (oprec != PREC_NOT_OPERATOR);
30046 if (commutative_tree_code (opcode))
30047 oprec = (enum cp_parser_prec) (oprec - 1);
30048 cp_lexer_consume_token (parser->lexer);
30049 rhs = cp_parser_binary_expression (parser, false, false,
30050 oprec, NULL);
30051 if (rhs == error_mark_node)
30052 goto saw_error;
30053 goto stmt_done;
30054 /* FALLTHROUGH */
30055 default:
30056 cp_parser_error (parser,
30057 "invalid operator for %<#pragma omp atomic%>");
30058 goto saw_error;
30060 cp_lexer_consume_token (parser->lexer);
30062 rhs = cp_parser_expression (parser);
30063 if (rhs == error_mark_node)
30064 goto saw_error;
30065 break;
30067 stmt_done:
30068 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
30070 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
30071 goto saw_error;
30072 v = cp_parser_unary_expression (parser);
30073 if (v == error_mark_node)
30074 goto saw_error;
30075 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30076 goto saw_error;
30077 lhs1 = cp_parser_unary_expression (parser);
30078 if (lhs1 == error_mark_node)
30079 goto saw_error;
30081 if (structured_block)
30083 cp_parser_consume_semicolon_at_end_of_statement (parser);
30084 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30086 done:
30087 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
30088 if (!structured_block)
30089 cp_parser_consume_semicolon_at_end_of_statement (parser);
30090 return;
30092 saw_error:
30093 cp_parser_skip_to_end_of_block_or_statement (parser);
30094 if (structured_block)
30096 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30097 cp_lexer_consume_token (parser->lexer);
30098 else if (code == OMP_ATOMIC_CAPTURE_NEW)
30100 cp_parser_skip_to_end_of_block_or_statement (parser);
30101 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30102 cp_lexer_consume_token (parser->lexer);
30108 /* OpenMP 2.5:
30109 # pragma omp barrier new-line */
30111 static void
30112 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
30114 cp_parser_require_pragma_eol (parser, pragma_tok);
30115 finish_omp_barrier ();
30118 /* OpenMP 2.5:
30119 # pragma omp critical [(name)] new-line
30120 structured-block */
30122 static tree
30123 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
30125 tree stmt, name = NULL;
30127 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30129 cp_lexer_consume_token (parser->lexer);
30131 name = cp_parser_identifier (parser);
30133 if (name == error_mark_node
30134 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30135 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30136 /*or_comma=*/false,
30137 /*consume_paren=*/true);
30138 if (name == error_mark_node)
30139 name = NULL;
30141 cp_parser_require_pragma_eol (parser, pragma_tok);
30143 stmt = cp_parser_omp_structured_block (parser);
30144 return c_finish_omp_critical (input_location, stmt, name);
30147 /* OpenMP 2.5:
30148 # pragma omp flush flush-vars[opt] new-line
30150 flush-vars:
30151 ( variable-list ) */
30153 static void
30154 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
30156 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30157 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30158 cp_parser_require_pragma_eol (parser, pragma_tok);
30160 finish_omp_flush ();
30163 /* Helper function, to parse omp for increment expression. */
30165 static tree
30166 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
30168 tree cond = cp_parser_binary_expression (parser, false, true,
30169 PREC_NOT_OPERATOR, NULL);
30170 if (cond == error_mark_node
30171 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30173 cp_parser_skip_to_end_of_statement (parser);
30174 return error_mark_node;
30177 switch (TREE_CODE (cond))
30179 case GT_EXPR:
30180 case GE_EXPR:
30181 case LT_EXPR:
30182 case LE_EXPR:
30183 break;
30184 case NE_EXPR:
30185 if (code == CILK_SIMD || code == CILK_FOR)
30186 break;
30187 /* Fall through: OpenMP disallows NE_EXPR. */
30188 default:
30189 return error_mark_node;
30192 /* If decl is an iterator, preserve LHS and RHS of the relational
30193 expr until finish_omp_for. */
30194 if (decl
30195 && (type_dependent_expression_p (decl)
30196 || CLASS_TYPE_P (TREE_TYPE (decl))))
30197 return cond;
30199 return build_x_binary_op (input_location, TREE_CODE (cond),
30200 TREE_OPERAND (cond, 0), ERROR_MARK,
30201 TREE_OPERAND (cond, 1), ERROR_MARK,
30202 /*overload=*/NULL, tf_warning_or_error);
30205 /* Helper function, to parse omp for increment expression. */
30207 static tree
30208 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
30210 cp_token *token = cp_lexer_peek_token (parser->lexer);
30211 enum tree_code op;
30212 tree lhs, rhs;
30213 cp_id_kind idk;
30214 bool decl_first;
30216 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30218 op = (token->type == CPP_PLUS_PLUS
30219 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30220 cp_lexer_consume_token (parser->lexer);
30221 lhs = cp_parser_simple_cast_expression (parser);
30222 if (lhs != decl)
30223 return error_mark_node;
30224 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30227 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30228 if (lhs != decl)
30229 return error_mark_node;
30231 token = cp_lexer_peek_token (parser->lexer);
30232 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30234 op = (token->type == CPP_PLUS_PLUS
30235 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30236 cp_lexer_consume_token (parser->lexer);
30237 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30240 op = cp_parser_assignment_operator_opt (parser);
30241 if (op == ERROR_MARK)
30242 return error_mark_node;
30244 if (op != NOP_EXPR)
30246 rhs = cp_parser_assignment_expression (parser);
30247 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30248 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30251 lhs = cp_parser_binary_expression (parser, false, false,
30252 PREC_ADDITIVE_EXPRESSION, NULL);
30253 token = cp_lexer_peek_token (parser->lexer);
30254 decl_first = lhs == decl;
30255 if (decl_first)
30256 lhs = NULL_TREE;
30257 if (token->type != CPP_PLUS
30258 && token->type != CPP_MINUS)
30259 return error_mark_node;
30263 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30264 cp_lexer_consume_token (parser->lexer);
30265 rhs = cp_parser_binary_expression (parser, false, false,
30266 PREC_ADDITIVE_EXPRESSION, NULL);
30267 token = cp_lexer_peek_token (parser->lexer);
30268 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30270 if (lhs == NULL_TREE)
30272 if (op == PLUS_EXPR)
30273 lhs = rhs;
30274 else
30275 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30276 tf_warning_or_error);
30278 else
30279 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30280 ERROR_MARK, NULL, tf_warning_or_error);
30283 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30285 if (!decl_first)
30287 if (rhs != decl || op == MINUS_EXPR)
30288 return error_mark_node;
30289 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30291 else
30292 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30294 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30297 /* Parse the initialization statement of either an OpenMP for loop or
30298 a Cilk Plus for loop.
30300 Return true if the resulting construct should have an
30301 OMP_CLAUSE_PRIVATE added to it. */
30303 static bool
30304 cp_parser_omp_for_loop_init (cp_parser *parser,
30305 enum tree_code code,
30306 tree &this_pre_body,
30307 vec<tree, va_gc> *for_block,
30308 tree &init,
30309 tree &decl,
30310 tree &real_decl)
30312 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30313 return false;
30315 bool add_private_clause = false;
30317 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30319 init-expr:
30320 var = lb
30321 integer-type var = lb
30322 random-access-iterator-type var = lb
30323 pointer-type var = lb
30325 cp_decl_specifier_seq type_specifiers;
30327 /* First, try to parse as an initialized declaration. See
30328 cp_parser_condition, from whence the bulk of this is copied. */
30330 cp_parser_parse_tentatively (parser);
30331 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30332 /*is_trailing_return=*/false,
30333 &type_specifiers);
30334 if (cp_parser_parse_definitely (parser))
30336 /* If parsing a type specifier seq succeeded, then this
30337 MUST be a initialized declaration. */
30338 tree asm_specification, attributes;
30339 cp_declarator *declarator;
30341 declarator = cp_parser_declarator (parser,
30342 CP_PARSER_DECLARATOR_NAMED,
30343 /*ctor_dtor_or_conv_p=*/NULL,
30344 /*parenthesized_p=*/NULL,
30345 /*member_p=*/false,
30346 /*friend_p=*/false);
30347 attributes = cp_parser_attributes_opt (parser);
30348 asm_specification = cp_parser_asm_specification_opt (parser);
30350 if (declarator == cp_error_declarator)
30351 cp_parser_skip_to_end_of_statement (parser);
30353 else
30355 tree pushed_scope, auto_node;
30357 decl = start_decl (declarator, &type_specifiers,
30358 SD_INITIALIZED, attributes,
30359 /*prefix_attributes=*/NULL_TREE,
30360 &pushed_scope);
30362 auto_node = type_uses_auto (TREE_TYPE (decl));
30363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30365 if (cp_lexer_next_token_is (parser->lexer,
30366 CPP_OPEN_PAREN))
30368 if (code != CILK_SIMD && code != CILK_FOR)
30369 error ("parenthesized initialization is not allowed in "
30370 "OpenMP %<for%> loop");
30371 else
30372 error ("parenthesized initialization is "
30373 "not allowed in for-loop");
30375 else
30376 /* Trigger an error. */
30377 cp_parser_require (parser, CPP_EQ, RT_EQ);
30379 init = error_mark_node;
30380 cp_parser_skip_to_end_of_statement (parser);
30382 else if (CLASS_TYPE_P (TREE_TYPE (decl))
30383 || type_dependent_expression_p (decl)
30384 || auto_node)
30386 bool is_direct_init, is_non_constant_init;
30388 init = cp_parser_initializer (parser,
30389 &is_direct_init,
30390 &is_non_constant_init);
30392 if (auto_node)
30394 TREE_TYPE (decl)
30395 = do_auto_deduction (TREE_TYPE (decl), init,
30396 auto_node);
30398 if (!CLASS_TYPE_P (TREE_TYPE (decl))
30399 && !type_dependent_expression_p (decl))
30400 goto non_class;
30403 cp_finish_decl (decl, init, !is_non_constant_init,
30404 asm_specification,
30405 LOOKUP_ONLYCONVERTING);
30406 if (CLASS_TYPE_P (TREE_TYPE (decl)))
30408 vec_safe_push (for_block, this_pre_body);
30409 init = NULL_TREE;
30411 else
30412 init = pop_stmt_list (this_pre_body);
30413 this_pre_body = NULL_TREE;
30415 else
30417 /* Consume '='. */
30418 cp_lexer_consume_token (parser->lexer);
30419 init = cp_parser_assignment_expression (parser);
30421 non_class:
30422 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30423 init = error_mark_node;
30424 else
30425 cp_finish_decl (decl, NULL_TREE,
30426 /*init_const_expr_p=*/false,
30427 asm_specification,
30428 LOOKUP_ONLYCONVERTING);
30431 if (pushed_scope)
30432 pop_scope (pushed_scope);
30435 else
30437 cp_id_kind idk;
30438 /* If parsing a type specifier sequence failed, then
30439 this MUST be a simple expression. */
30440 if (code == CILK_FOR)
30441 error ("%<_Cilk_for%> allows expression instead of declaration only "
30442 "in C, not in C++");
30443 cp_parser_parse_tentatively (parser);
30444 decl = cp_parser_primary_expression (parser, false, false,
30445 false, &idk);
30446 if (!cp_parser_error_occurred (parser)
30447 && decl
30448 && DECL_P (decl)
30449 && CLASS_TYPE_P (TREE_TYPE (decl)))
30451 tree rhs;
30453 cp_parser_parse_definitely (parser);
30454 cp_parser_require (parser, CPP_EQ, RT_EQ);
30455 rhs = cp_parser_assignment_expression (parser);
30456 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30457 decl, NOP_EXPR,
30458 rhs,
30459 tf_warning_or_error));
30460 add_private_clause = true;
30462 else
30464 decl = NULL;
30465 cp_parser_abort_tentative_parse (parser);
30466 init = cp_parser_expression (parser);
30467 if (init)
30469 if (TREE_CODE (init) == MODIFY_EXPR
30470 || TREE_CODE (init) == MODOP_EXPR)
30471 real_decl = TREE_OPERAND (init, 0);
30475 return add_private_clause;
30478 /* Parse the restricted form of the for statement allowed by OpenMP. */
30480 static tree
30481 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30482 tree *cclauses)
30484 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30485 tree real_decl, initv, condv, incrv, declv;
30486 tree this_pre_body, cl;
30487 location_t loc_first;
30488 bool collapse_err = false;
30489 int i, collapse = 1, nbraces = 0;
30490 vec<tree, va_gc> *for_block = make_tree_vector ();
30492 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30493 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30494 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30496 gcc_assert (collapse >= 1);
30498 declv = make_tree_vec (collapse);
30499 initv = make_tree_vec (collapse);
30500 condv = make_tree_vec (collapse);
30501 incrv = make_tree_vec (collapse);
30503 loc_first = cp_lexer_peek_token (parser->lexer)->location;
30505 for (i = 0; i < collapse; i++)
30507 int bracecount = 0;
30508 bool add_private_clause = false;
30509 location_t loc;
30511 if (code != CILK_FOR
30512 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30514 cp_parser_error (parser, "for statement expected");
30515 return NULL;
30517 if (code == CILK_FOR
30518 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
30520 cp_parser_error (parser, "_Cilk_for statement expected");
30521 return NULL;
30523 loc = cp_lexer_consume_token (parser->lexer)->location;
30525 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30526 return NULL;
30528 init = decl = real_decl = NULL;
30529 this_pre_body = push_stmt_list ();
30531 add_private_clause
30532 |= cp_parser_omp_for_loop_init (parser, code,
30533 this_pre_body, for_block,
30534 init, decl, real_decl);
30536 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30537 if (this_pre_body)
30539 this_pre_body = pop_stmt_list (this_pre_body);
30540 if (pre_body)
30542 tree t = pre_body;
30543 pre_body = push_stmt_list ();
30544 add_stmt (t);
30545 add_stmt (this_pre_body);
30546 pre_body = pop_stmt_list (pre_body);
30548 else
30549 pre_body = this_pre_body;
30552 if (decl)
30553 real_decl = decl;
30554 if (cclauses != NULL
30555 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30556 && real_decl != NULL_TREE)
30558 tree *c;
30559 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30560 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30561 && OMP_CLAUSE_DECL (*c) == real_decl)
30563 error_at (loc, "iteration variable %qD"
30564 " should not be firstprivate", real_decl);
30565 *c = OMP_CLAUSE_CHAIN (*c);
30567 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30568 && OMP_CLAUSE_DECL (*c) == real_decl)
30570 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
30571 tree l = *c;
30572 *c = OMP_CLAUSE_CHAIN (*c);
30573 if (code == OMP_SIMD)
30575 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30576 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
30578 else
30580 OMP_CLAUSE_CHAIN (l) = clauses;
30581 clauses = l;
30583 add_private_clause = false;
30585 else
30587 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30588 && OMP_CLAUSE_DECL (*c) == real_decl)
30589 add_private_clause = false;
30590 c = &OMP_CLAUSE_CHAIN (*c);
30594 if (add_private_clause)
30596 tree c;
30597 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30599 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30600 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30601 && OMP_CLAUSE_DECL (c) == decl)
30602 break;
30603 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30604 && OMP_CLAUSE_DECL (c) == decl)
30605 error_at (loc, "iteration variable %qD "
30606 "should not be firstprivate",
30607 decl);
30608 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30609 && OMP_CLAUSE_DECL (c) == decl)
30610 error_at (loc, "iteration variable %qD should not be reduction",
30611 decl);
30613 if (c == NULL)
30615 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30616 OMP_CLAUSE_DECL (c) = decl;
30617 c = finish_omp_clauses (c);
30618 if (c)
30620 OMP_CLAUSE_CHAIN (c) = clauses;
30621 clauses = c;
30626 cond = NULL;
30627 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30628 cond = cp_parser_omp_for_cond (parser, decl, code);
30629 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30631 incr = NULL;
30632 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30634 /* If decl is an iterator, preserve the operator on decl
30635 until finish_omp_for. */
30636 if (real_decl
30637 && ((processing_template_decl
30638 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30639 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30640 incr = cp_parser_omp_for_incr (parser, real_decl);
30641 else
30642 incr = cp_parser_expression (parser);
30643 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30644 SET_EXPR_LOCATION (incr, input_location);
30647 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30648 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30649 /*or_comma=*/false,
30650 /*consume_paren=*/true);
30652 TREE_VEC_ELT (declv, i) = decl;
30653 TREE_VEC_ELT (initv, i) = init;
30654 TREE_VEC_ELT (condv, i) = cond;
30655 TREE_VEC_ELT (incrv, i) = incr;
30657 if (i == collapse - 1)
30658 break;
30660 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30661 in between the collapsed for loops to be still considered perfectly
30662 nested. Hopefully the final version clarifies this.
30663 For now handle (multiple) {'s and empty statements. */
30664 cp_parser_parse_tentatively (parser);
30667 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30668 break;
30669 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30671 cp_lexer_consume_token (parser->lexer);
30672 bracecount++;
30674 else if (bracecount
30675 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30676 cp_lexer_consume_token (parser->lexer);
30677 else
30679 loc = cp_lexer_peek_token (parser->lexer)->location;
30680 error_at (loc, "not enough collapsed for loops");
30681 collapse_err = true;
30682 cp_parser_abort_tentative_parse (parser);
30683 declv = NULL_TREE;
30684 break;
30687 while (1);
30689 if (declv)
30691 cp_parser_parse_definitely (parser);
30692 nbraces += bracecount;
30696 /* Note that we saved the original contents of this flag when we entered
30697 the structured block, and so we don't need to re-save it here. */
30698 if (code == CILK_SIMD || code == CILK_FOR)
30699 parser->in_statement = IN_CILK_SIMD_FOR;
30700 else
30701 parser->in_statement = IN_OMP_FOR;
30703 /* Note that the grammar doesn't call for a structured block here,
30704 though the loop as a whole is a structured block. */
30705 body = push_stmt_list ();
30706 cp_parser_statement (parser, NULL_TREE, false, NULL);
30707 body = pop_stmt_list (body);
30709 if (declv == NULL_TREE)
30710 ret = NULL_TREE;
30711 else
30712 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30713 pre_body, clauses);
30715 while (nbraces)
30717 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30719 cp_lexer_consume_token (parser->lexer);
30720 nbraces--;
30722 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30723 cp_lexer_consume_token (parser->lexer);
30724 else
30726 if (!collapse_err)
30728 error_at (cp_lexer_peek_token (parser->lexer)->location,
30729 "collapsed loops not perfectly nested");
30731 collapse_err = true;
30732 cp_parser_statement_seq_opt (parser, NULL);
30733 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30734 break;
30738 while (!for_block->is_empty ())
30739 add_stmt (pop_stmt_list (for_block->pop ()));
30740 release_tree_vector (for_block);
30742 return ret;
30745 /* Helper function for OpenMP parsing, split clauses and call
30746 finish_omp_clauses on each of the set of clauses afterwards. */
30748 static void
30749 cp_omp_split_clauses (location_t loc, enum tree_code code,
30750 omp_clause_mask mask, tree clauses, tree *cclauses)
30752 int i;
30753 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30754 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30755 if (cclauses[i])
30756 cclauses[i] = finish_omp_clauses (cclauses[i]);
30759 /* OpenMP 4.0:
30760 #pragma omp simd simd-clause[optseq] new-line
30761 for-loop */
30763 #define OMP_SIMD_CLAUSE_MASK \
30764 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30772 static tree
30773 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30774 char *p_name, omp_clause_mask mask, tree *cclauses)
30776 tree clauses, sb, ret;
30777 unsigned int save;
30778 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30780 strcat (p_name, " simd");
30781 mask |= OMP_SIMD_CLAUSE_MASK;
30782 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30784 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30785 cclauses == NULL);
30786 if (cclauses)
30788 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30789 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30792 sb = begin_omp_structured_block ();
30793 save = cp_parser_begin_omp_structured_block (parser);
30795 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30797 cp_parser_end_omp_structured_block (parser, save);
30798 add_stmt (finish_omp_structured_block (sb));
30800 return ret;
30803 /* OpenMP 2.5:
30804 #pragma omp for for-clause[optseq] new-line
30805 for-loop
30807 OpenMP 4.0:
30808 #pragma omp for simd for-simd-clause[optseq] new-line
30809 for-loop */
30811 #define OMP_FOR_CLAUSE_MASK \
30812 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30821 static tree
30822 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30823 char *p_name, omp_clause_mask mask, tree *cclauses)
30825 tree clauses, sb, ret;
30826 unsigned int save;
30827 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30829 strcat (p_name, " for");
30830 mask |= OMP_FOR_CLAUSE_MASK;
30831 if (cclauses)
30832 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30834 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30836 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30837 const char *p = IDENTIFIER_POINTER (id);
30839 if (strcmp (p, "simd") == 0)
30841 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30842 if (cclauses == NULL)
30843 cclauses = cclauses_buf;
30845 cp_lexer_consume_token (parser->lexer);
30846 if (!flag_openmp) /* flag_openmp_simd */
30847 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30848 cclauses);
30849 sb = begin_omp_structured_block ();
30850 save = cp_parser_begin_omp_structured_block (parser);
30851 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30852 cclauses);
30853 cp_parser_end_omp_structured_block (parser, save);
30854 tree body = finish_omp_structured_block (sb);
30855 if (ret == NULL)
30856 return ret;
30857 ret = make_node (OMP_FOR);
30858 TREE_TYPE (ret) = void_type_node;
30859 OMP_FOR_BODY (ret) = body;
30860 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30861 SET_EXPR_LOCATION (ret, loc);
30862 add_stmt (ret);
30863 return ret;
30866 if (!flag_openmp) /* flag_openmp_simd */
30868 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30869 return NULL_TREE;
30872 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30873 cclauses == NULL);
30874 if (cclauses)
30876 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30877 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30880 sb = begin_omp_structured_block ();
30881 save = cp_parser_begin_omp_structured_block (parser);
30883 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30885 cp_parser_end_omp_structured_block (parser, save);
30886 add_stmt (finish_omp_structured_block (sb));
30888 return ret;
30891 /* OpenMP 2.5:
30892 # pragma omp master new-line
30893 structured-block */
30895 static tree
30896 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30898 cp_parser_require_pragma_eol (parser, pragma_tok);
30899 return c_finish_omp_master (input_location,
30900 cp_parser_omp_structured_block (parser));
30903 /* OpenMP 2.5:
30904 # pragma omp ordered new-line
30905 structured-block */
30907 static tree
30908 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30910 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30911 cp_parser_require_pragma_eol (parser, pragma_tok);
30912 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30915 /* OpenMP 2.5:
30917 section-scope:
30918 { section-sequence }
30920 section-sequence:
30921 section-directive[opt] structured-block
30922 section-sequence section-directive structured-block */
30924 static tree
30925 cp_parser_omp_sections_scope (cp_parser *parser)
30927 tree stmt, substmt;
30928 bool error_suppress = false;
30929 cp_token *tok;
30931 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30932 return NULL_TREE;
30934 stmt = push_stmt_list ();
30936 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30938 substmt = cp_parser_omp_structured_block (parser);
30939 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30940 add_stmt (substmt);
30943 while (1)
30945 tok = cp_lexer_peek_token (parser->lexer);
30946 if (tok->type == CPP_CLOSE_BRACE)
30947 break;
30948 if (tok->type == CPP_EOF)
30949 break;
30951 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30953 cp_lexer_consume_token (parser->lexer);
30954 cp_parser_require_pragma_eol (parser, tok);
30955 error_suppress = false;
30957 else if (!error_suppress)
30959 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30960 error_suppress = true;
30963 substmt = cp_parser_omp_structured_block (parser);
30964 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30965 add_stmt (substmt);
30967 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30969 substmt = pop_stmt_list (stmt);
30971 stmt = make_node (OMP_SECTIONS);
30972 TREE_TYPE (stmt) = void_type_node;
30973 OMP_SECTIONS_BODY (stmt) = substmt;
30975 add_stmt (stmt);
30976 return stmt;
30979 /* OpenMP 2.5:
30980 # pragma omp sections sections-clause[optseq] newline
30981 sections-scope */
30983 #define OMP_SECTIONS_CLAUSE_MASK \
30984 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30990 static tree
30991 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30992 char *p_name, omp_clause_mask mask, tree *cclauses)
30994 tree clauses, ret;
30995 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30997 strcat (p_name, " sections");
30998 mask |= OMP_SECTIONS_CLAUSE_MASK;
30999 if (cclauses)
31000 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
31002 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31003 cclauses == NULL);
31004 if (cclauses)
31006 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
31007 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
31010 ret = cp_parser_omp_sections_scope (parser);
31011 if (ret)
31012 OMP_SECTIONS_CLAUSES (ret) = clauses;
31014 return ret;
31017 /* OpenMP 2.5:
31018 # pragma omp parallel parallel-clause[optseq] new-line
31019 structured-block
31020 # pragma omp parallel for parallel-for-clause[optseq] new-line
31021 structured-block
31022 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
31023 structured-block
31025 OpenMP 4.0:
31026 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
31027 structured-block */
31029 #define OMP_PARALLEL_CLAUSE_MASK \
31030 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
31038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
31040 static tree
31041 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
31042 char *p_name, omp_clause_mask mask, tree *cclauses)
31044 tree stmt, clauses, block;
31045 unsigned int save;
31046 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31048 strcat (p_name, " parallel");
31049 mask |= OMP_PARALLEL_CLAUSE_MASK;
31051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31053 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31054 if (cclauses == NULL)
31055 cclauses = cclauses_buf;
31057 cp_lexer_consume_token (parser->lexer);
31058 if (!flag_openmp) /* flag_openmp_simd */
31059 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
31060 block = begin_omp_parallel ();
31061 save = cp_parser_begin_omp_structured_block (parser);
31062 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
31063 cp_parser_end_omp_structured_block (parser, save);
31064 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31065 block);
31066 if (ret == NULL_TREE)
31067 return ret;
31068 OMP_PARALLEL_COMBINED (stmt) = 1;
31069 return stmt;
31071 else if (cclauses)
31073 error_at (loc, "expected %<for%> after %qs", p_name);
31074 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31075 return NULL_TREE;
31077 else if (!flag_openmp) /* flag_openmp_simd */
31079 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31080 return NULL_TREE;
31082 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31084 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31085 const char *p = IDENTIFIER_POINTER (id);
31086 if (strcmp (p, "sections") == 0)
31088 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31089 cclauses = cclauses_buf;
31091 cp_lexer_consume_token (parser->lexer);
31092 block = begin_omp_parallel ();
31093 save = cp_parser_begin_omp_structured_block (parser);
31094 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
31095 cp_parser_end_omp_structured_block (parser, save);
31096 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31097 block);
31098 OMP_PARALLEL_COMBINED (stmt) = 1;
31099 return stmt;
31103 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
31105 block = begin_omp_parallel ();
31106 save = cp_parser_begin_omp_structured_block (parser);
31107 cp_parser_statement (parser, NULL_TREE, false, NULL);
31108 cp_parser_end_omp_structured_block (parser, save);
31109 stmt = finish_omp_parallel (clauses, block);
31110 return stmt;
31113 /* OpenMP 2.5:
31114 # pragma omp single single-clause[optseq] new-line
31115 structured-block */
31117 #define OMP_SINGLE_CLAUSE_MASK \
31118 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
31121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31123 static tree
31124 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
31126 tree stmt = make_node (OMP_SINGLE);
31127 TREE_TYPE (stmt) = void_type_node;
31129 OMP_SINGLE_CLAUSES (stmt)
31130 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
31131 "#pragma omp single", pragma_tok);
31132 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
31134 return add_stmt (stmt);
31137 /* OpenMP 3.0:
31138 # pragma omp task task-clause[optseq] new-line
31139 structured-block */
31141 #define OMP_TASK_CLAUSE_MASK \
31142 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
31144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
31149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
31150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
31152 static tree
31153 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
31155 tree clauses, block;
31156 unsigned int save;
31158 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
31159 "#pragma omp task", pragma_tok);
31160 block = begin_omp_task ();
31161 save = cp_parser_begin_omp_structured_block (parser);
31162 cp_parser_statement (parser, NULL_TREE, false, NULL);
31163 cp_parser_end_omp_structured_block (parser, save);
31164 return finish_omp_task (clauses, block);
31167 /* OpenMP 3.0:
31168 # pragma omp taskwait new-line */
31170 static void
31171 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
31173 cp_parser_require_pragma_eol (parser, pragma_tok);
31174 finish_omp_taskwait ();
31177 /* OpenMP 3.1:
31178 # pragma omp taskyield new-line */
31180 static void
31181 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
31183 cp_parser_require_pragma_eol (parser, pragma_tok);
31184 finish_omp_taskyield ();
31187 /* OpenMP 4.0:
31188 # pragma omp taskgroup new-line
31189 structured-block */
31191 static tree
31192 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
31194 cp_parser_require_pragma_eol (parser, pragma_tok);
31195 return c_finish_omp_taskgroup (input_location,
31196 cp_parser_omp_structured_block (parser));
31200 /* OpenMP 2.5:
31201 # pragma omp threadprivate (variable-list) */
31203 static void
31204 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
31206 tree vars;
31208 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31209 cp_parser_require_pragma_eol (parser, pragma_tok);
31211 finish_omp_threadprivate (vars);
31214 /* OpenMP 4.0:
31215 # pragma omp cancel cancel-clause[optseq] new-line */
31217 #define OMP_CANCEL_CLAUSE_MASK \
31218 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
31222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31224 static void
31225 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31227 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31228 "#pragma omp cancel", pragma_tok);
31229 finish_omp_cancel (clauses);
31232 /* OpenMP 4.0:
31233 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31235 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31236 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31241 static void
31242 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31244 tree clauses;
31245 bool point_seen = false;
31247 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31249 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31250 const char *p = IDENTIFIER_POINTER (id);
31252 if (strcmp (p, "point") == 0)
31254 cp_lexer_consume_token (parser->lexer);
31255 point_seen = true;
31258 if (!point_seen)
31260 cp_parser_error (parser, "expected %<point%>");
31261 cp_parser_require_pragma_eol (parser, pragma_tok);
31262 return;
31265 clauses = cp_parser_omp_all_clauses (parser,
31266 OMP_CANCELLATION_POINT_CLAUSE_MASK,
31267 "#pragma omp cancellation point",
31268 pragma_tok);
31269 finish_omp_cancellation_point (clauses);
31272 /* OpenMP 4.0:
31273 #pragma omp distribute distribute-clause[optseq] new-line
31274 for-loop */
31276 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31277 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31282 static tree
31283 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31284 char *p_name, omp_clause_mask mask, tree *cclauses)
31286 tree clauses, sb, ret;
31287 unsigned int save;
31288 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31290 strcat (p_name, " distribute");
31291 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31293 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31295 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31296 const char *p = IDENTIFIER_POINTER (id);
31297 bool simd = false;
31298 bool parallel = false;
31300 if (strcmp (p, "simd") == 0)
31301 simd = true;
31302 else
31303 parallel = strcmp (p, "parallel") == 0;
31304 if (parallel || simd)
31306 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31307 if (cclauses == NULL)
31308 cclauses = cclauses_buf;
31309 cp_lexer_consume_token (parser->lexer);
31310 if (!flag_openmp) /* flag_openmp_simd */
31312 if (simd)
31313 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31314 cclauses);
31315 else
31316 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31317 cclauses);
31319 sb = begin_omp_structured_block ();
31320 save = cp_parser_begin_omp_structured_block (parser);
31321 if (simd)
31322 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31323 cclauses);
31324 else
31325 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31326 cclauses);
31327 cp_parser_end_omp_structured_block (parser, save);
31328 tree body = finish_omp_structured_block (sb);
31329 if (ret == NULL)
31330 return ret;
31331 ret = make_node (OMP_DISTRIBUTE);
31332 TREE_TYPE (ret) = void_type_node;
31333 OMP_FOR_BODY (ret) = body;
31334 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31335 SET_EXPR_LOCATION (ret, loc);
31336 add_stmt (ret);
31337 return ret;
31340 if (!flag_openmp) /* flag_openmp_simd */
31342 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31343 return NULL_TREE;
31346 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31347 cclauses == NULL);
31348 if (cclauses)
31350 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31351 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31354 sb = begin_omp_structured_block ();
31355 save = cp_parser_begin_omp_structured_block (parser);
31357 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31359 cp_parser_end_omp_structured_block (parser, save);
31360 add_stmt (finish_omp_structured_block (sb));
31362 return ret;
31365 /* OpenMP 4.0:
31366 # pragma omp teams teams-clause[optseq] new-line
31367 structured-block */
31369 #define OMP_TEAMS_CLAUSE_MASK \
31370 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31378 static tree
31379 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31380 char *p_name, omp_clause_mask mask, tree *cclauses)
31382 tree clauses, sb, ret;
31383 unsigned int save;
31384 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31386 strcat (p_name, " teams");
31387 mask |= OMP_TEAMS_CLAUSE_MASK;
31389 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31391 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31392 const char *p = IDENTIFIER_POINTER (id);
31393 if (strcmp (p, "distribute") == 0)
31395 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31396 if (cclauses == NULL)
31397 cclauses = cclauses_buf;
31399 cp_lexer_consume_token (parser->lexer);
31400 if (!flag_openmp) /* flag_openmp_simd */
31401 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31402 cclauses);
31403 sb = begin_omp_structured_block ();
31404 save = cp_parser_begin_omp_structured_block (parser);
31405 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31406 cclauses);
31407 cp_parser_end_omp_structured_block (parser, save);
31408 tree body = finish_omp_structured_block (sb);
31409 if (ret == NULL)
31410 return ret;
31411 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31412 ret = make_node (OMP_TEAMS);
31413 TREE_TYPE (ret) = void_type_node;
31414 OMP_TEAMS_CLAUSES (ret) = clauses;
31415 OMP_TEAMS_BODY (ret) = body;
31416 OMP_TEAMS_COMBINED (ret) = 1;
31417 return add_stmt (ret);
31420 if (!flag_openmp) /* flag_openmp_simd */
31422 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31423 return NULL_TREE;
31426 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31427 cclauses == NULL);
31428 if (cclauses)
31430 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31431 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31434 tree stmt = make_node (OMP_TEAMS);
31435 TREE_TYPE (stmt) = void_type_node;
31436 OMP_TEAMS_CLAUSES (stmt) = clauses;
31437 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31439 return add_stmt (stmt);
31442 /* OpenMP 4.0:
31443 # pragma omp target data target-data-clause[optseq] new-line
31444 structured-block */
31446 #define OMP_TARGET_DATA_CLAUSE_MASK \
31447 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31451 static tree
31452 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31454 tree stmt = make_node (OMP_TARGET_DATA);
31455 TREE_TYPE (stmt) = void_type_node;
31457 OMP_TARGET_DATA_CLAUSES (stmt)
31458 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31459 "#pragma omp target data", pragma_tok);
31460 keep_next_level (true);
31461 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31463 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31464 return add_stmt (stmt);
31467 /* OpenMP 4.0:
31468 # pragma omp target update target-update-clause[optseq] new-line */
31470 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31471 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31476 static bool
31477 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31478 enum pragma_context context)
31480 if (context == pragma_stmt)
31482 error_at (pragma_tok->location,
31483 "%<#pragma omp target update%> may only be "
31484 "used in compound statements");
31485 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31486 return false;
31489 tree clauses
31490 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31491 "#pragma omp target update", pragma_tok);
31492 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31493 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31495 error_at (pragma_tok->location,
31496 "%<#pragma omp target update%> must contain at least one "
31497 "%<from%> or %<to%> clauses");
31498 return false;
31501 tree stmt = make_node (OMP_TARGET_UPDATE);
31502 TREE_TYPE (stmt) = void_type_node;
31503 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31504 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31505 add_stmt (stmt);
31506 return false;
31509 /* OpenMP 4.0:
31510 # pragma omp target target-clause[optseq] new-line
31511 structured-block */
31513 #define OMP_TARGET_CLAUSE_MASK \
31514 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31518 static bool
31519 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31520 enum pragma_context context)
31522 if (context != pragma_stmt && context != pragma_compound)
31524 cp_parser_error (parser, "expected declaration specifiers");
31525 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31526 return false;
31529 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31531 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31532 const char *p = IDENTIFIER_POINTER (id);
31534 if (strcmp (p, "teams") == 0)
31536 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31537 char p_name[sizeof ("#pragma omp target teams distribute "
31538 "parallel for simd")];
31540 cp_lexer_consume_token (parser->lexer);
31541 strcpy (p_name, "#pragma omp target");
31542 if (!flag_openmp) /* flag_openmp_simd */
31544 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31545 OMP_TARGET_CLAUSE_MASK,
31546 cclauses);
31547 return stmt != NULL_TREE;
31549 keep_next_level (true);
31550 tree sb = begin_omp_structured_block ();
31551 unsigned save = cp_parser_begin_omp_structured_block (parser);
31552 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31553 OMP_TARGET_CLAUSE_MASK, cclauses);
31554 cp_parser_end_omp_structured_block (parser, save);
31555 tree body = finish_omp_structured_block (sb);
31556 if (ret == NULL_TREE)
31557 return false;
31558 tree stmt = make_node (OMP_TARGET);
31559 TREE_TYPE (stmt) = void_type_node;
31560 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31561 OMP_TARGET_BODY (stmt) = body;
31562 add_stmt (stmt);
31563 return true;
31565 else if (!flag_openmp) /* flag_openmp_simd */
31567 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31568 return false;
31570 else if (strcmp (p, "data") == 0)
31572 cp_lexer_consume_token (parser->lexer);
31573 cp_parser_omp_target_data (parser, pragma_tok);
31574 return true;
31576 else if (strcmp (p, "update") == 0)
31578 cp_lexer_consume_token (parser->lexer);
31579 return cp_parser_omp_target_update (parser, pragma_tok, context);
31583 tree stmt = make_node (OMP_TARGET);
31584 TREE_TYPE (stmt) = void_type_node;
31586 OMP_TARGET_CLAUSES (stmt)
31587 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31588 "#pragma omp target", pragma_tok);
31589 keep_next_level (true);
31590 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31592 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31593 add_stmt (stmt);
31594 return true;
31597 /* OpenACC 2.0:
31598 # pragma acc cache (variable-list) new-line
31601 static tree
31602 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
31604 tree stmt, clauses;
31606 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
31607 clauses = finish_omp_clauses (clauses);
31609 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
31611 stmt = make_node (OACC_CACHE);
31612 TREE_TYPE (stmt) = void_type_node;
31613 OACC_CACHE_CLAUSES (stmt) = clauses;
31614 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31615 add_stmt (stmt);
31617 return stmt;
31620 /* OpenACC 2.0:
31621 # pragma acc data oacc-data-clause[optseq] new-line
31622 structured-block */
31624 #define OACC_DATA_CLAUSE_MASK \
31625 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
31637 static tree
31638 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
31640 tree stmt, clauses, block;
31641 unsigned int save;
31643 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
31644 "#pragma acc data", pragma_tok);
31646 block = begin_omp_parallel ();
31647 save = cp_parser_begin_omp_structured_block (parser);
31648 cp_parser_statement (parser, NULL_TREE, false, NULL);
31649 cp_parser_end_omp_structured_block (parser, save);
31650 stmt = finish_oacc_data (clauses, block);
31651 return stmt;
31654 /* OpenACC 2.0:
31655 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31659 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31661 LOC is the location of the #pragma token.
31664 #define OACC_ENTER_DATA_CLAUSE_MASK \
31665 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31673 #define OACC_EXIT_DATA_CLAUSE_MASK \
31674 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
31678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31680 static tree
31681 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
31682 bool enter)
31684 tree stmt, clauses;
31686 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
31687 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31689 cp_parser_error (parser, enter
31690 ? "expected %<data%> in %<#pragma acc enter data%>"
31691 : "expected %<data%> in %<#pragma acc exit data%>");
31692 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31693 return NULL_TREE;
31696 const char *p =
31697 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
31698 if (strcmp (p, "data") != 0)
31700 cp_parser_error (parser, "invalid pragma");
31701 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31702 return NULL_TREE;
31705 cp_lexer_consume_token (parser->lexer);
31707 if (enter)
31708 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
31709 "#pragma acc enter data", pragma_tok);
31710 else
31711 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
31712 "#pragma acc exit data", pragma_tok);
31714 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31716 error_at (pragma_tok->location,
31717 "%<#pragma acc enter data%> has no data movement clause");
31718 return NULL_TREE;
31721 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
31722 TREE_TYPE (stmt) = void_type_node;
31723 OMP_STANDALONE_CLAUSES (stmt) = clauses;
31724 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31725 add_stmt (stmt);
31726 return stmt;
31729 /* OpenACC 2.0:
31730 # pragma acc kernels oacc-kernels-clause[optseq] new-line
31731 structured-block */
31733 #define OACC_KERNELS_CLAUSE_MASK \
31734 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31748 static tree
31749 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
31751 tree stmt, clauses, block;
31752 unsigned int save;
31754 clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
31755 "#pragma acc kernels", pragma_tok);
31757 block = begin_omp_parallel ();
31758 save = cp_parser_begin_omp_structured_block (parser);
31759 cp_parser_statement (parser, NULL_TREE, false, NULL);
31760 cp_parser_end_omp_structured_block (parser, save);
31761 stmt = finish_oacc_kernels (clauses, block);
31762 return stmt;
31765 /* OpenACC 2.0:
31766 # pragma acc loop oacc-loop-clause[optseq] new-line
31767 structured-block */
31769 #define OACC_LOOP_CLAUSE_MASK \
31770 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
31771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
31773 static tree
31774 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
31776 tree stmt, clauses, block;
31777 int save;
31779 clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
31780 "#pragma acc loop", pragma_tok);
31782 block = begin_omp_structured_block ();
31783 save = cp_parser_begin_omp_structured_block (parser);
31784 stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
31785 cp_parser_end_omp_structured_block (parser, save);
31786 add_stmt (finish_omp_structured_block (block));
31787 return stmt;
31790 /* OpenACC 2.0:
31791 # pragma acc parallel oacc-parallel-clause[optseq] new-line
31792 structured-block */
31794 #define OACC_PARALLEL_CLAUSE_MASK \
31795 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
31803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
31804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
31810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
31811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31813 static tree
31814 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
31816 tree stmt, clauses, block;
31817 unsigned int save;
31819 clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
31820 "#pragma acc parallel", pragma_tok);
31822 block = begin_omp_parallel ();
31823 save = cp_parser_begin_omp_structured_block (parser);
31824 cp_parser_statement (parser, NULL_TREE, false, NULL);
31825 cp_parser_end_omp_structured_block (parser, save);
31826 stmt = finish_oacc_parallel (clauses, block);
31827 return stmt;
31830 /* OpenACC 2.0:
31831 # pragma acc update oacc-update-clause[optseq] new-line
31834 #define OACC_UPDATE_CLAUSE_MASK \
31835 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
31837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
31838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
31840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31842 static tree
31843 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
31845 tree stmt, clauses;
31847 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
31848 "#pragma acc update", pragma_tok);
31850 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31852 error_at (pragma_tok->location,
31853 "%<#pragma acc update%> must contain at least one "
31854 "%<device%> or %<host/self%> clause");
31855 return NULL_TREE;
31858 stmt = make_node (OACC_UPDATE);
31859 TREE_TYPE (stmt) = void_type_node;
31860 OACC_UPDATE_CLAUSES (stmt) = clauses;
31861 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31862 add_stmt (stmt);
31863 return stmt;
31866 /* OpenACC 2.0:
31867 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31869 LOC is the location of the #pragma token.
31872 #define OACC_WAIT_CLAUSE_MASK \
31873 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
31875 static tree
31876 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
31878 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
31879 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31881 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31882 list = cp_parser_oacc_wait_list (parser, loc, list);
31884 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
31885 "#pragma acc wait", pragma_tok);
31887 stmt = c_finish_oacc_wait (loc, list, clauses);
31889 return stmt;
31892 /* OpenMP 4.0:
31893 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31895 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31896 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31903 static void
31904 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31905 enum pragma_context context)
31907 bool first_p = parser->omp_declare_simd == NULL;
31908 cp_omp_declare_simd_data data;
31909 if (first_p)
31911 data.error_seen = false;
31912 data.fndecl_seen = false;
31913 data.tokens = vNULL;
31914 parser->omp_declare_simd = &data;
31916 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31917 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31918 cp_lexer_consume_token (parser->lexer);
31919 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31920 parser->omp_declare_simd->error_seen = true;
31921 cp_parser_require_pragma_eol (parser, pragma_tok);
31922 struct cp_token_cache *cp
31923 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31924 parser->omp_declare_simd->tokens.safe_push (cp);
31925 if (first_p)
31927 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31928 cp_parser_pragma (parser, context);
31929 switch (context)
31931 case pragma_external:
31932 cp_parser_declaration (parser);
31933 break;
31934 case pragma_member:
31935 cp_parser_member_declaration (parser);
31936 break;
31937 case pragma_objc_icode:
31938 cp_parser_block_declaration (parser, /*statement_p=*/false);
31939 break;
31940 default:
31941 cp_parser_declaration_statement (parser);
31942 break;
31944 if (parser->omp_declare_simd
31945 && !parser->omp_declare_simd->error_seen
31946 && !parser->omp_declare_simd->fndecl_seen)
31947 error_at (pragma_tok->location,
31948 "%<#pragma omp declare simd%> not immediately followed by "
31949 "function declaration or definition");
31950 data.tokens.release ();
31951 parser->omp_declare_simd = NULL;
31955 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31956 This function is modelled similar to the late parsing of omp declare
31957 simd. */
31959 static tree
31960 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31962 struct cp_token_cache *ce;
31963 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31964 int ii = 0;
31966 if (parser->omp_declare_simd != NULL)
31968 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31969 " marked as a Cilk Plus SIMD-enabled function");
31970 XDELETE (parser->cilk_simd_fn_info);
31971 parser->cilk_simd_fn_info = NULL;
31972 return attrs;
31974 if (!info->error_seen && info->fndecl_seen)
31976 error ("vector attribute not immediately followed by a single function"
31977 " declaration or definition");
31978 info->error_seen = true;
31980 if (info->error_seen)
31981 return attrs;
31983 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31985 tree c, cl;
31987 cp_parser_push_lexer_for_tokens (parser, ce);
31988 parser->lexer->in_pragma = true;
31989 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31990 "SIMD-enabled functions attribute",
31991 NULL);
31992 cp_parser_pop_lexer (parser);
31993 if (cl)
31994 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31996 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31997 TREE_CHAIN (c) = attrs;
31998 attrs = c;
32000 c = build_tree_list (get_identifier ("omp declare simd"), cl);
32001 TREE_CHAIN (c) = attrs;
32002 if (processing_template_decl)
32003 ATTR_IS_DEPENDENT (c) = 1;
32004 attrs = c;
32006 info->fndecl_seen = true;
32007 XDELETE (parser->cilk_simd_fn_info);
32008 parser->cilk_simd_fn_info = NULL;
32009 return attrs;
32012 /* Finalize #pragma omp declare simd clauses after direct declarator has
32013 been parsed, and put that into "omp declare simd" attribute. */
32015 static tree
32016 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
32018 struct cp_token_cache *ce;
32019 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
32020 int i;
32022 if (!data->error_seen && data->fndecl_seen)
32024 error ("%<#pragma omp declare simd%> not immediately followed by "
32025 "a single function declaration or definition");
32026 data->error_seen = true;
32027 return attrs;
32029 if (data->error_seen)
32030 return attrs;
32032 FOR_EACH_VEC_ELT (data->tokens, i, ce)
32034 tree c, cl;
32036 cp_parser_push_lexer_for_tokens (parser, ce);
32037 parser->lexer->in_pragma = true;
32038 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
32039 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
32040 cp_lexer_consume_token (parser->lexer);
32041 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
32042 "#pragma omp declare simd", pragma_tok);
32043 cp_parser_pop_lexer (parser);
32044 if (cl)
32045 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
32046 c = build_tree_list (get_identifier ("omp declare simd"), cl);
32047 TREE_CHAIN (c) = attrs;
32048 if (processing_template_decl)
32049 ATTR_IS_DEPENDENT (c) = 1;
32050 attrs = c;
32053 data->fndecl_seen = true;
32054 return attrs;
32058 /* OpenMP 4.0:
32059 # pragma omp declare target new-line
32060 declarations and definitions
32061 # pragma omp end declare target new-line */
32063 static void
32064 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
32066 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32067 scope_chain->omp_declare_target_attribute++;
32070 static void
32071 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
32073 const char *p = "";
32074 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32076 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32077 p = IDENTIFIER_POINTER (id);
32079 if (strcmp (p, "declare") == 0)
32081 cp_lexer_consume_token (parser->lexer);
32082 p = "";
32083 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32085 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32086 p = IDENTIFIER_POINTER (id);
32088 if (strcmp (p, "target") == 0)
32089 cp_lexer_consume_token (parser->lexer);
32090 else
32092 cp_parser_error (parser, "expected %<target%>");
32093 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32094 return;
32097 else
32099 cp_parser_error (parser, "expected %<declare%>");
32100 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32101 return;
32103 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32104 if (!scope_chain->omp_declare_target_attribute)
32105 error_at (pragma_tok->location,
32106 "%<#pragma omp end declare target%> without corresponding "
32107 "%<#pragma omp declare target%>");
32108 else
32109 scope_chain->omp_declare_target_attribute--;
32112 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
32113 expression and optional initializer clause of
32114 #pragma omp declare reduction. We store the expression(s) as
32115 either 3, 6 or 7 special statements inside of the artificial function's
32116 body. The first two statements are DECL_EXPRs for the artificial
32117 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
32118 expression that uses those variables.
32119 If there was any INITIALIZER clause, this is followed by further statements,
32120 the fourth and fifth statements are DECL_EXPRs for the artificial
32121 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
32122 constructor variant (first token after open paren is not omp_priv),
32123 then the sixth statement is a statement with the function call expression
32124 that uses the OMP_PRIV and optionally OMP_ORIG variable.
32125 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
32126 to initialize the OMP_PRIV artificial variable and there is seventh
32127 statement, a DECL_EXPR of the OMP_PRIV statement again. */
32129 static bool
32130 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
32132 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
32133 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
32134 type = TREE_TYPE (type);
32135 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
32136 DECL_ARTIFICIAL (omp_out) = 1;
32137 pushdecl (omp_out);
32138 add_decl_expr (omp_out);
32139 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
32140 DECL_ARTIFICIAL (omp_in) = 1;
32141 pushdecl (omp_in);
32142 add_decl_expr (omp_in);
32143 tree combiner;
32144 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
32146 keep_next_level (true);
32147 tree block = begin_omp_structured_block ();
32148 combiner = cp_parser_expression (parser);
32149 finish_expr_stmt (combiner);
32150 block = finish_omp_structured_block (block);
32151 add_stmt (block);
32153 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32154 return false;
32156 const char *p = "";
32157 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32159 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32160 p = IDENTIFIER_POINTER (id);
32163 if (strcmp (p, "initializer") == 0)
32165 cp_lexer_consume_token (parser->lexer);
32166 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32167 return false;
32169 p = "";
32170 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32172 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32173 p = IDENTIFIER_POINTER (id);
32176 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
32177 DECL_ARTIFICIAL (omp_priv) = 1;
32178 pushdecl (omp_priv);
32179 add_decl_expr (omp_priv);
32180 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
32181 DECL_ARTIFICIAL (omp_orig) = 1;
32182 pushdecl (omp_orig);
32183 add_decl_expr (omp_orig);
32185 keep_next_level (true);
32186 block = begin_omp_structured_block ();
32188 bool ctor = false;
32189 if (strcmp (p, "omp_priv") == 0)
32191 bool is_direct_init, is_non_constant_init;
32192 ctor = true;
32193 cp_lexer_consume_token (parser->lexer);
32194 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
32195 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
32196 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32197 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
32198 == CPP_CLOSE_PAREN
32199 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
32200 == CPP_CLOSE_PAREN))
32202 finish_omp_structured_block (block);
32203 error ("invalid initializer clause");
32204 return false;
32206 initializer = cp_parser_initializer (parser, &is_direct_init,
32207 &is_non_constant_init);
32208 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
32209 NULL_TREE, LOOKUP_ONLYCONVERTING);
32211 else
32213 cp_parser_parse_tentatively (parser);
32214 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
32215 /*check_dependency_p=*/true,
32216 /*template_p=*/NULL,
32217 /*declarator_p=*/false,
32218 /*optional_p=*/false);
32219 vec<tree, va_gc> *args;
32220 if (fn_name == error_mark_node
32221 || cp_parser_error_occurred (parser)
32222 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32223 || ((args = cp_parser_parenthesized_expression_list
32224 (parser, non_attr, /*cast_p=*/false,
32225 /*allow_expansion_p=*/true,
32226 /*non_constant_p=*/NULL)),
32227 cp_parser_error_occurred (parser)))
32229 finish_omp_structured_block (block);
32230 cp_parser_abort_tentative_parse (parser);
32231 cp_parser_error (parser, "expected id-expression (arguments)");
32232 return false;
32234 unsigned int i;
32235 tree arg;
32236 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
32237 if (arg == omp_priv
32238 || (TREE_CODE (arg) == ADDR_EXPR
32239 && TREE_OPERAND (arg, 0) == omp_priv))
32240 break;
32241 cp_parser_abort_tentative_parse (parser);
32242 if (arg == NULL_TREE)
32243 error ("one of the initializer call arguments should be %<omp_priv%>"
32244 " or %<&omp_priv%>");
32245 initializer = cp_parser_postfix_expression (parser, false, false, false,
32246 false, NULL);
32247 finish_expr_stmt (initializer);
32250 block = finish_omp_structured_block (block);
32251 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
32252 add_stmt (block);
32254 if (ctor)
32255 add_decl_expr (omp_orig);
32257 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32258 return false;
32261 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
32262 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
32264 return true;
32267 /* OpenMP 4.0
32268 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32269 initializer-clause[opt] new-line
32271 initializer-clause:
32272 initializer (omp_priv initializer)
32273 initializer (function-name (argument-list)) */
32275 static void
32276 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
32277 enum pragma_context)
32279 auto_vec<tree> types;
32280 enum tree_code reduc_code = ERROR_MARK;
32281 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
32282 unsigned int i;
32283 cp_token *first_token;
32284 cp_token_cache *cp;
32285 int errs;
32286 void *p;
32288 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
32289 p = obstack_alloc (&declarator_obstack, 0);
32291 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32292 goto fail;
32294 switch (cp_lexer_peek_token (parser->lexer)->type)
32296 case CPP_PLUS:
32297 reduc_code = PLUS_EXPR;
32298 break;
32299 case CPP_MULT:
32300 reduc_code = MULT_EXPR;
32301 break;
32302 case CPP_MINUS:
32303 reduc_code = MINUS_EXPR;
32304 break;
32305 case CPP_AND:
32306 reduc_code = BIT_AND_EXPR;
32307 break;
32308 case CPP_XOR:
32309 reduc_code = BIT_XOR_EXPR;
32310 break;
32311 case CPP_OR:
32312 reduc_code = BIT_IOR_EXPR;
32313 break;
32314 case CPP_AND_AND:
32315 reduc_code = TRUTH_ANDIF_EXPR;
32316 break;
32317 case CPP_OR_OR:
32318 reduc_code = TRUTH_ORIF_EXPR;
32319 break;
32320 case CPP_NAME:
32321 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
32322 break;
32323 default:
32324 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32325 "%<|%>, %<&&%>, %<||%> or identifier");
32326 goto fail;
32329 if (reduc_code != ERROR_MARK)
32330 cp_lexer_consume_token (parser->lexer);
32332 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
32333 if (reduc_id == error_mark_node)
32334 goto fail;
32336 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32337 goto fail;
32339 /* Types may not be defined in declare reduction type list. */
32340 const char *saved_message;
32341 saved_message = parser->type_definition_forbidden_message;
32342 parser->type_definition_forbidden_message
32343 = G_("types may not be defined in declare reduction type list");
32344 bool saved_colon_corrects_to_scope_p;
32345 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32346 parser->colon_corrects_to_scope_p = false;
32347 bool saved_colon_doesnt_start_class_def_p;
32348 saved_colon_doesnt_start_class_def_p
32349 = parser->colon_doesnt_start_class_def_p;
32350 parser->colon_doesnt_start_class_def_p = true;
32352 while (true)
32354 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32355 type = cp_parser_type_id (parser);
32356 if (type == error_mark_node)
32358 else if (ARITHMETIC_TYPE_P (type)
32359 && (orig_reduc_id == NULL_TREE
32360 || (TREE_CODE (type) != COMPLEX_TYPE
32361 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32362 "min") == 0
32363 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32364 "max") == 0))))
32365 error_at (loc, "predeclared arithmetic type %qT in "
32366 "%<#pragma omp declare reduction%>", type);
32367 else if (TREE_CODE (type) == FUNCTION_TYPE
32368 || TREE_CODE (type) == METHOD_TYPE
32369 || TREE_CODE (type) == ARRAY_TYPE)
32370 error_at (loc, "function or array type %qT in "
32371 "%<#pragma omp declare reduction%>", type);
32372 else if (TREE_CODE (type) == REFERENCE_TYPE)
32373 error_at (loc, "reference type %qT in "
32374 "%<#pragma omp declare reduction%>", type);
32375 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
32376 error_at (loc, "const, volatile or __restrict qualified type %qT in "
32377 "%<#pragma omp declare reduction%>", type);
32378 else
32379 types.safe_push (type);
32381 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32382 cp_lexer_consume_token (parser->lexer);
32383 else
32384 break;
32387 /* Restore the saved message. */
32388 parser->type_definition_forbidden_message = saved_message;
32389 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32390 parser->colon_doesnt_start_class_def_p
32391 = saved_colon_doesnt_start_class_def_p;
32393 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
32394 || types.is_empty ())
32396 fail:
32397 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32398 goto done;
32401 first_token = cp_lexer_peek_token (parser->lexer);
32402 cp = NULL;
32403 errs = errorcount;
32404 FOR_EACH_VEC_ELT (types, i, type)
32406 tree fntype
32407 = build_function_type_list (void_type_node,
32408 cp_build_reference_type (type, false),
32409 NULL_TREE);
32410 tree this_reduc_id = reduc_id;
32411 if (!dependent_type_p (type))
32412 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
32413 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
32414 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
32415 DECL_ARTIFICIAL (fndecl) = 1;
32416 DECL_EXTERNAL (fndecl) = 1;
32417 DECL_DECLARED_INLINE_P (fndecl) = 1;
32418 DECL_IGNORED_P (fndecl) = 1;
32419 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
32420 DECL_ATTRIBUTES (fndecl)
32421 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
32422 DECL_ATTRIBUTES (fndecl));
32423 if (processing_template_decl)
32424 fndecl = push_template_decl (fndecl);
32425 bool block_scope = false;
32426 tree block = NULL_TREE;
32427 if (current_function_decl)
32429 block_scope = true;
32430 DECL_CONTEXT (fndecl) = global_namespace;
32431 if (!processing_template_decl)
32432 pushdecl (fndecl);
32434 else if (current_class_type)
32436 if (cp == NULL)
32438 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32439 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32440 cp_lexer_consume_token (parser->lexer);
32441 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32442 goto fail;
32443 cp = cp_token_cache_new (first_token,
32444 cp_lexer_peek_nth_token (parser->lexer,
32445 2));
32447 DECL_STATIC_FUNCTION_P (fndecl) = 1;
32448 finish_member_declaration (fndecl);
32449 DECL_PENDING_INLINE_INFO (fndecl) = cp;
32450 DECL_PENDING_INLINE_P (fndecl) = 1;
32451 vec_safe_push (unparsed_funs_with_definitions, fndecl);
32452 continue;
32454 else
32456 DECL_CONTEXT (fndecl) = current_namespace;
32457 pushdecl (fndecl);
32459 if (!block_scope)
32460 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
32461 else
32462 block = begin_omp_structured_block ();
32463 if (cp)
32465 cp_parser_push_lexer_for_tokens (parser, cp);
32466 parser->lexer->in_pragma = true;
32468 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
32470 if (!block_scope)
32471 finish_function (0);
32472 else
32473 DECL_CONTEXT (fndecl) = current_function_decl;
32474 if (cp)
32475 cp_parser_pop_lexer (parser);
32476 goto fail;
32478 if (cp)
32479 cp_parser_pop_lexer (parser);
32480 if (!block_scope)
32481 finish_function (0);
32482 else
32484 DECL_CONTEXT (fndecl) = current_function_decl;
32485 block = finish_omp_structured_block (block);
32486 if (TREE_CODE (block) == BIND_EXPR)
32487 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
32488 else if (TREE_CODE (block) == STATEMENT_LIST)
32489 DECL_SAVED_TREE (fndecl) = block;
32490 if (processing_template_decl)
32491 add_decl_expr (fndecl);
32493 cp_check_omp_declare_reduction (fndecl);
32494 if (cp == NULL && types.length () > 1)
32495 cp = cp_token_cache_new (first_token,
32496 cp_lexer_peek_nth_token (parser->lexer, 2));
32497 if (errs != errorcount)
32498 break;
32501 cp_parser_require_pragma_eol (parser, pragma_tok);
32503 done:
32504 /* Free any declarators allocated. */
32505 obstack_free (&declarator_obstack, p);
32508 /* OpenMP 4.0
32509 #pragma omp declare simd declare-simd-clauses[optseq] new-line
32510 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32511 initializer-clause[opt] new-line
32512 #pragma omp declare target new-line */
32514 static void
32515 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
32516 enum pragma_context context)
32518 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32520 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32521 const char *p = IDENTIFIER_POINTER (id);
32523 if (strcmp (p, "simd") == 0)
32525 cp_lexer_consume_token (parser->lexer);
32526 cp_parser_omp_declare_simd (parser, pragma_tok,
32527 context);
32528 return;
32530 cp_ensure_no_omp_declare_simd (parser);
32531 if (strcmp (p, "reduction") == 0)
32533 cp_lexer_consume_token (parser->lexer);
32534 cp_parser_omp_declare_reduction (parser, pragma_tok,
32535 context);
32536 return;
32538 if (!flag_openmp) /* flag_openmp_simd */
32540 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32541 return;
32543 if (strcmp (p, "target") == 0)
32545 cp_lexer_consume_token (parser->lexer);
32546 cp_parser_omp_declare_target (parser, pragma_tok);
32547 return;
32550 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
32551 "or %<target%>");
32552 cp_parser_require_pragma_eol (parser, pragma_tok);
32555 /* Main entry point to OpenMP statement pragmas. */
32557 static void
32558 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
32560 tree stmt;
32561 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
32562 omp_clause_mask mask (0);
32564 switch (pragma_tok->pragma_kind)
32566 case PRAGMA_OACC_CACHE:
32567 stmt = cp_parser_oacc_cache (parser, pragma_tok);
32568 break;
32569 case PRAGMA_OACC_DATA:
32570 stmt = cp_parser_oacc_data (parser, pragma_tok);
32571 break;
32572 case PRAGMA_OACC_ENTER_DATA:
32573 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
32574 break;
32575 case PRAGMA_OACC_EXIT_DATA:
32576 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
32577 break;
32578 case PRAGMA_OACC_KERNELS:
32579 stmt = cp_parser_oacc_kernels (parser, pragma_tok);
32580 break;
32581 case PRAGMA_OACC_LOOP:
32582 stmt = cp_parser_oacc_loop (parser, pragma_tok);
32583 break;
32584 case PRAGMA_OACC_PARALLEL:
32585 stmt = cp_parser_oacc_parallel (parser, pragma_tok);
32586 break;
32587 case PRAGMA_OACC_UPDATE:
32588 stmt = cp_parser_oacc_update (parser, pragma_tok);
32589 break;
32590 case PRAGMA_OACC_WAIT:
32591 stmt = cp_parser_oacc_wait (parser, pragma_tok);
32592 break;
32593 case PRAGMA_OMP_ATOMIC:
32594 cp_parser_omp_atomic (parser, pragma_tok);
32595 return;
32596 case PRAGMA_OMP_CRITICAL:
32597 stmt = cp_parser_omp_critical (parser, pragma_tok);
32598 break;
32599 case PRAGMA_OMP_DISTRIBUTE:
32600 strcpy (p_name, "#pragma omp");
32601 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
32602 break;
32603 case PRAGMA_OMP_FOR:
32604 strcpy (p_name, "#pragma omp");
32605 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
32606 break;
32607 case PRAGMA_OMP_MASTER:
32608 stmt = cp_parser_omp_master (parser, pragma_tok);
32609 break;
32610 case PRAGMA_OMP_ORDERED:
32611 stmt = cp_parser_omp_ordered (parser, pragma_tok);
32612 break;
32613 case PRAGMA_OMP_PARALLEL:
32614 strcpy (p_name, "#pragma omp");
32615 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
32616 break;
32617 case PRAGMA_OMP_SECTIONS:
32618 strcpy (p_name, "#pragma omp");
32619 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
32620 break;
32621 case PRAGMA_OMP_SIMD:
32622 strcpy (p_name, "#pragma omp");
32623 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
32624 break;
32625 case PRAGMA_OMP_SINGLE:
32626 stmt = cp_parser_omp_single (parser, pragma_tok);
32627 break;
32628 case PRAGMA_OMP_TASK:
32629 stmt = cp_parser_omp_task (parser, pragma_tok);
32630 break;
32631 case PRAGMA_OMP_TASKGROUP:
32632 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
32633 break;
32634 case PRAGMA_OMP_TEAMS:
32635 strcpy (p_name, "#pragma omp");
32636 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
32637 break;
32638 default:
32639 gcc_unreachable ();
32642 if (stmt)
32643 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32646 /* Transactional Memory parsing routines. */
32648 /* Parse a transaction attribute.
32650 txn-attribute:
32651 attribute
32652 [ [ identifier ] ]
32654 ??? Simplify this when C++0x bracket attributes are
32655 implemented properly. */
32657 static tree
32658 cp_parser_txn_attribute_opt (cp_parser *parser)
32660 cp_token *token;
32661 tree attr_name, attr = NULL;
32663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32664 return cp_parser_attributes_opt (parser);
32666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
32667 return NULL_TREE;
32668 cp_lexer_consume_token (parser->lexer);
32669 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
32670 goto error1;
32672 token = cp_lexer_peek_token (parser->lexer);
32673 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
32675 token = cp_lexer_consume_token (parser->lexer);
32677 attr_name = (token->type == CPP_KEYWORD
32678 /* For keywords, use the canonical spelling,
32679 not the parsed identifier. */
32680 ? ridpointers[(int) token->keyword]
32681 : token->u.value);
32682 attr = build_tree_list (attr_name, NULL_TREE);
32684 else
32685 cp_parser_error (parser, "expected identifier");
32687 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32688 error1:
32689 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32690 return attr;
32693 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32695 transaction-statement:
32696 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32697 compound-statement
32698 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32701 static tree
32702 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32704 unsigned char old_in = parser->in_transaction;
32705 unsigned char this_in = 1, new_in;
32706 cp_token *token;
32707 tree stmt, attrs, noex;
32709 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32710 || keyword == RID_TRANSACTION_RELAXED);
32711 token = cp_parser_require_keyword (parser, keyword,
32712 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32713 : RT_TRANSACTION_RELAXED));
32714 gcc_assert (token != NULL);
32716 if (keyword == RID_TRANSACTION_RELAXED)
32717 this_in |= TM_STMT_ATTR_RELAXED;
32718 else
32720 attrs = cp_parser_txn_attribute_opt (parser);
32721 if (attrs)
32722 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32725 /* Parse a noexcept specification. */
32726 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32728 /* Keep track if we're in the lexical scope of an outer transaction. */
32729 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32731 stmt = begin_transaction_stmt (token->location, NULL, this_in);
32733 parser->in_transaction = new_in;
32734 cp_parser_compound_statement (parser, NULL, false, false);
32735 parser->in_transaction = old_in;
32737 finish_transaction_stmt (stmt, NULL, this_in, noex);
32739 return stmt;
32742 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32744 transaction-expression:
32745 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32746 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32749 static tree
32750 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32752 unsigned char old_in = parser->in_transaction;
32753 unsigned char this_in = 1;
32754 cp_token *token;
32755 tree expr, noex;
32756 bool noex_expr;
32758 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32759 || keyword == RID_TRANSACTION_RELAXED);
32761 if (!flag_tm)
32762 error (keyword == RID_TRANSACTION_RELAXED
32763 ? G_("%<__transaction_relaxed%> without transactional memory "
32764 "support enabled")
32765 : G_("%<__transaction_atomic%> without transactional memory "
32766 "support enabled"));
32768 token = cp_parser_require_keyword (parser, keyword,
32769 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32770 : RT_TRANSACTION_RELAXED));
32771 gcc_assert (token != NULL);
32773 if (keyword == RID_TRANSACTION_RELAXED)
32774 this_in |= TM_STMT_ATTR_RELAXED;
32776 /* Set this early. This might mean that we allow transaction_cancel in
32777 an expression that we find out later actually has to be a constexpr.
32778 However, we expect that cxx_constant_value will be able to deal with
32779 this; also, if the noexcept has no constexpr, then what we parse next
32780 really is a transaction's body. */
32781 parser->in_transaction = this_in;
32783 /* Parse a noexcept specification. */
32784 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32785 true);
32787 if (!noex || !noex_expr
32788 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32790 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32792 expr = cp_parser_expression (parser);
32793 expr = finish_parenthesized_expr (expr);
32795 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32797 else
32799 /* The only expression that is available got parsed for the noexcept
32800 already. noexcept is true then. */
32801 expr = noex;
32802 noex = boolean_true_node;
32805 expr = build_transaction_expr (token->location, expr, this_in, noex);
32806 parser->in_transaction = old_in;
32808 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32809 return error_mark_node;
32811 return (flag_tm ? expr : error_mark_node);
32814 /* Parse a function-transaction-block.
32816 function-transaction-block:
32817 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32818 function-body
32819 __transaction_atomic txn-attribute[opt] function-try-block
32820 __transaction_relaxed ctor-initializer[opt] function-body
32821 __transaction_relaxed function-try-block
32824 static bool
32825 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32827 unsigned char old_in = parser->in_transaction;
32828 unsigned char new_in = 1;
32829 tree compound_stmt, stmt, attrs;
32830 bool ctor_initializer_p;
32831 cp_token *token;
32833 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32834 || keyword == RID_TRANSACTION_RELAXED);
32835 token = cp_parser_require_keyword (parser, keyword,
32836 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32837 : RT_TRANSACTION_RELAXED));
32838 gcc_assert (token != NULL);
32840 if (keyword == RID_TRANSACTION_RELAXED)
32841 new_in |= TM_STMT_ATTR_RELAXED;
32842 else
32844 attrs = cp_parser_txn_attribute_opt (parser);
32845 if (attrs)
32846 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32849 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32851 parser->in_transaction = new_in;
32853 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32854 ctor_initializer_p = cp_parser_function_try_block (parser);
32855 else
32856 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32857 (parser, /*in_function_try_block=*/false);
32859 parser->in_transaction = old_in;
32861 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32863 return ctor_initializer_p;
32866 /* Parse a __transaction_cancel statement.
32868 cancel-statement:
32869 __transaction_cancel txn-attribute[opt] ;
32870 __transaction_cancel txn-attribute[opt] throw-expression ;
32872 ??? Cancel and throw is not yet implemented. */
32874 static tree
32875 cp_parser_transaction_cancel (cp_parser *parser)
32877 cp_token *token;
32878 bool is_outer = false;
32879 tree stmt, attrs;
32881 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32882 RT_TRANSACTION_CANCEL);
32883 gcc_assert (token != NULL);
32885 attrs = cp_parser_txn_attribute_opt (parser);
32886 if (attrs)
32887 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32889 /* ??? Parse cancel-and-throw here. */
32891 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32893 if (!flag_tm)
32895 error_at (token->location, "%<__transaction_cancel%> without "
32896 "transactional memory support enabled");
32897 return error_mark_node;
32899 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32901 error_at (token->location, "%<__transaction_cancel%> within a "
32902 "%<__transaction_relaxed%>");
32903 return error_mark_node;
32905 else if (is_outer)
32907 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32908 && !is_tm_may_cancel_outer (current_function_decl))
32910 error_at (token->location, "outer %<__transaction_cancel%> not "
32911 "within outer %<__transaction_atomic%>");
32912 error_at (token->location,
32913 " or a %<transaction_may_cancel_outer%> function");
32914 return error_mark_node;
32917 else if (parser->in_transaction == 0)
32919 error_at (token->location, "%<__transaction_cancel%> not within "
32920 "%<__transaction_atomic%>");
32921 return error_mark_node;
32924 stmt = build_tm_abort_call (token->location, is_outer);
32925 add_stmt (stmt);
32927 return stmt;
32930 /* The parser. */
32932 static GTY (()) cp_parser *the_parser;
32935 /* Special handling for the first token or line in the file. The first
32936 thing in the file might be #pragma GCC pch_preprocess, which loads a
32937 PCH file, which is a GC collection point. So we need to handle this
32938 first pragma without benefit of an existing lexer structure.
32940 Always returns one token to the caller in *FIRST_TOKEN. This is
32941 either the true first token of the file, or the first token after
32942 the initial pragma. */
32944 static void
32945 cp_parser_initial_pragma (cp_token *first_token)
32947 tree name = NULL;
32949 cp_lexer_get_preprocessor_token (NULL, first_token);
32950 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32951 return;
32953 cp_lexer_get_preprocessor_token (NULL, first_token);
32954 if (first_token->type == CPP_STRING)
32956 name = first_token->u.value;
32958 cp_lexer_get_preprocessor_token (NULL, first_token);
32959 if (first_token->type != CPP_PRAGMA_EOL)
32960 error_at (first_token->location,
32961 "junk at end of %<#pragma GCC pch_preprocess%>");
32963 else
32964 error_at (first_token->location, "expected string literal");
32966 /* Skip to the end of the pragma. */
32967 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32968 cp_lexer_get_preprocessor_token (NULL, first_token);
32970 /* Now actually load the PCH file. */
32971 if (name)
32972 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32974 /* Read one more token to return to our caller. We have to do this
32975 after reading the PCH file in, since its pointers have to be
32976 live. */
32977 cp_lexer_get_preprocessor_token (NULL, first_token);
32980 /* Parses the grainsize pragma for the _Cilk_for statement.
32981 Syntax:
32982 #pragma cilk grainsize = <VALUE>. */
32984 static void
32985 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
32987 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
32989 tree exp = cp_parser_binary_expression (parser, false, false,
32990 PREC_NOT_OPERATOR, NULL);
32991 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32992 if (!exp || exp == error_mark_node)
32994 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
32995 return;
32998 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
32999 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33000 cp_parser_cilk_for (parser, exp);
33001 else
33002 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
33003 "%<#pragma cilk grainsize%> is not followed by "
33004 "%<_Cilk_for%>");
33005 return;
33007 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33010 /* Normal parsing of a pragma token. Here we can (and must) use the
33011 regular lexer. */
33013 static bool
33014 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
33016 cp_token *pragma_tok;
33017 unsigned int id;
33019 pragma_tok = cp_lexer_consume_token (parser->lexer);
33020 gcc_assert (pragma_tok->type == CPP_PRAGMA);
33021 parser->lexer->in_pragma = true;
33023 id = pragma_tok->pragma_kind;
33024 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
33025 cp_ensure_no_omp_declare_simd (parser);
33026 switch (id)
33028 case PRAGMA_GCC_PCH_PREPROCESS:
33029 error_at (pragma_tok->location,
33030 "%<#pragma GCC pch_preprocess%> must be first");
33031 break;
33033 case PRAGMA_OMP_BARRIER:
33034 switch (context)
33036 case pragma_compound:
33037 cp_parser_omp_barrier (parser, pragma_tok);
33038 return false;
33039 case pragma_stmt:
33040 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
33041 "used in compound statements");
33042 break;
33043 default:
33044 goto bad_stmt;
33046 break;
33048 case PRAGMA_OMP_FLUSH:
33049 switch (context)
33051 case pragma_compound:
33052 cp_parser_omp_flush (parser, pragma_tok);
33053 return false;
33054 case pragma_stmt:
33055 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
33056 "used in compound statements");
33057 break;
33058 default:
33059 goto bad_stmt;
33061 break;
33063 case PRAGMA_OMP_TASKWAIT:
33064 switch (context)
33066 case pragma_compound:
33067 cp_parser_omp_taskwait (parser, pragma_tok);
33068 return false;
33069 case pragma_stmt:
33070 error_at (pragma_tok->location,
33071 "%<#pragma omp taskwait%> may only be "
33072 "used in compound statements");
33073 break;
33074 default:
33075 goto bad_stmt;
33077 break;
33079 case PRAGMA_OMP_TASKYIELD:
33080 switch (context)
33082 case pragma_compound:
33083 cp_parser_omp_taskyield (parser, pragma_tok);
33084 return false;
33085 case pragma_stmt:
33086 error_at (pragma_tok->location,
33087 "%<#pragma omp taskyield%> may only be "
33088 "used in compound statements");
33089 break;
33090 default:
33091 goto bad_stmt;
33093 break;
33095 case PRAGMA_OMP_CANCEL:
33096 switch (context)
33098 case pragma_compound:
33099 cp_parser_omp_cancel (parser, pragma_tok);
33100 return false;
33101 case pragma_stmt:
33102 error_at (pragma_tok->location,
33103 "%<#pragma omp cancel%> may only be "
33104 "used in compound statements");
33105 break;
33106 default:
33107 goto bad_stmt;
33109 break;
33111 case PRAGMA_OMP_CANCELLATION_POINT:
33112 switch (context)
33114 case pragma_compound:
33115 cp_parser_omp_cancellation_point (parser, pragma_tok);
33116 return false;
33117 case pragma_stmt:
33118 error_at (pragma_tok->location,
33119 "%<#pragma omp cancellation point%> may only be "
33120 "used in compound statements");
33121 break;
33122 default:
33123 goto bad_stmt;
33125 break;
33127 case PRAGMA_OMP_THREADPRIVATE:
33128 cp_parser_omp_threadprivate (parser, pragma_tok);
33129 return false;
33131 case PRAGMA_OMP_DECLARE_REDUCTION:
33132 cp_parser_omp_declare (parser, pragma_tok, context);
33133 return false;
33135 case PRAGMA_OACC_CACHE:
33136 case PRAGMA_OACC_DATA:
33137 case PRAGMA_OACC_ENTER_DATA:
33138 case PRAGMA_OACC_EXIT_DATA:
33139 case PRAGMA_OACC_KERNELS:
33140 case PRAGMA_OACC_PARALLEL:
33141 case PRAGMA_OACC_LOOP:
33142 case PRAGMA_OACC_UPDATE:
33143 case PRAGMA_OACC_WAIT:
33144 case PRAGMA_OMP_ATOMIC:
33145 case PRAGMA_OMP_CRITICAL:
33146 case PRAGMA_OMP_DISTRIBUTE:
33147 case PRAGMA_OMP_FOR:
33148 case PRAGMA_OMP_MASTER:
33149 case PRAGMA_OMP_ORDERED:
33150 case PRAGMA_OMP_PARALLEL:
33151 case PRAGMA_OMP_SECTIONS:
33152 case PRAGMA_OMP_SIMD:
33153 case PRAGMA_OMP_SINGLE:
33154 case PRAGMA_OMP_TASK:
33155 case PRAGMA_OMP_TASKGROUP:
33156 case PRAGMA_OMP_TEAMS:
33157 if (context != pragma_stmt && context != pragma_compound)
33158 goto bad_stmt;
33159 cp_parser_omp_construct (parser, pragma_tok);
33160 return true;
33162 case PRAGMA_OMP_TARGET:
33163 return cp_parser_omp_target (parser, pragma_tok, context);
33165 case PRAGMA_OMP_END_DECLARE_TARGET:
33166 cp_parser_omp_end_declare_target (parser, pragma_tok);
33167 return false;
33169 case PRAGMA_OMP_SECTION:
33170 error_at (pragma_tok->location,
33171 "%<#pragma omp section%> may only be used in "
33172 "%<#pragma omp sections%> construct");
33173 break;
33175 case PRAGMA_IVDEP:
33177 if (context == pragma_external)
33179 error_at (pragma_tok->location,
33180 "%<#pragma GCC ivdep%> must be inside a function");
33181 break;
33183 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33184 cp_token *tok;
33185 tok = cp_lexer_peek_token (the_parser->lexer);
33186 if (tok->type != CPP_KEYWORD
33187 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
33188 && tok->keyword != RID_DO))
33190 cp_parser_error (parser, "for, while or do statement expected");
33191 return false;
33193 cp_parser_iteration_statement (parser, true);
33194 return true;
33197 case PRAGMA_CILK_SIMD:
33198 if (context == pragma_external)
33200 error_at (pragma_tok->location,
33201 "%<#pragma simd%> must be inside a function");
33202 break;
33204 cp_parser_cilk_simd (parser, pragma_tok);
33205 return true;
33207 case PRAGMA_CILK_GRAINSIZE:
33208 if (context == pragma_external)
33210 error_at (pragma_tok->location,
33211 "%<#pragma cilk grainsize%> must be inside a function");
33212 break;
33215 /* Ignore the pragma if Cilk Plus is not enabled. */
33216 if (flag_cilkplus)
33218 cp_parser_cilk_grainsize (parser, pragma_tok);
33219 return true;
33221 else
33223 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
33224 "%<#pragma cilk grainsize%>");
33225 break;
33228 default:
33229 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
33230 c_invoke_pragma_handler (id);
33231 break;
33233 bad_stmt:
33234 cp_parser_error (parser, "expected declaration specifiers");
33235 break;
33238 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33239 return false;
33242 /* The interface the pragma parsers have to the lexer. */
33244 enum cpp_ttype
33245 pragma_lex (tree *value)
33247 cp_token *tok;
33248 enum cpp_ttype ret;
33250 tok = cp_lexer_peek_token (the_parser->lexer);
33252 ret = tok->type;
33253 *value = tok->u.value;
33255 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
33256 ret = CPP_EOF;
33257 else if (ret == CPP_STRING)
33258 *value = cp_parser_string_literal (the_parser, false, false);
33259 else
33261 cp_lexer_consume_token (the_parser->lexer);
33262 if (ret == CPP_KEYWORD)
33263 ret = CPP_NAME;
33266 return ret;
33270 /* External interface. */
33272 /* Parse one entire translation unit. */
33274 void
33275 c_parse_file (void)
33277 static bool already_called = false;
33279 if (already_called)
33280 fatal_error (input_location,
33281 "inter-module optimizations not implemented for C++");
33282 already_called = true;
33284 the_parser = cp_parser_new ();
33285 push_deferring_access_checks (flag_access_control
33286 ? dk_no_deferred : dk_no_check);
33287 cp_parser_translation_unit (the_parser);
33288 the_parser = NULL;
33291 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
33292 vectorlength clause:
33293 Syntax:
33294 vectorlength ( constant-expression ) */
33296 static tree
33297 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
33298 bool is_simd_fn)
33300 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33301 tree expr;
33302 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33303 safelen clause. Thus, vectorlength is represented as OMP 4.0
33304 safelen. For SIMD-enabled function it is represented by OMP 4.0
33305 simdlen. */
33306 if (!is_simd_fn)
33307 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
33308 loc);
33309 else
33310 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
33311 loc);
33313 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33314 return error_mark_node;
33316 expr = cp_parser_constant_expression (parser);
33317 expr = maybe_constant_value (expr);
33319 /* If expr == error_mark_node, then don't emit any errors nor
33320 create a clause. if any of the above functions returns
33321 error mark node then they would have emitted an error message. */
33322 if (expr == error_mark_node)
33324 else if (!TREE_TYPE (expr)
33325 || !TREE_CONSTANT (expr)
33326 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
33327 error_at (loc, "vectorlength must be an integer constant");
33328 else if (TREE_CONSTANT (expr)
33329 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
33330 error_at (loc, "vectorlength must be a power of 2");
33331 else
33333 tree c;
33334 if (!is_simd_fn)
33336 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
33337 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
33338 OMP_CLAUSE_CHAIN (c) = clauses;
33339 clauses = c;
33341 else
33343 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
33344 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
33345 OMP_CLAUSE_CHAIN (c) = clauses;
33346 clauses = c;
33350 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33351 return error_mark_node;
33352 return clauses;
33355 /* Handles the Cilk Plus #pragma simd linear clause.
33356 Syntax:
33357 linear ( simd-linear-variable-list )
33359 simd-linear-variable-list:
33360 simd-linear-variable
33361 simd-linear-variable-list , simd-linear-variable
33363 simd-linear-variable:
33364 id-expression
33365 id-expression : simd-linear-step
33367 simd-linear-step:
33368 conditional-expression */
33370 static tree
33371 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
33373 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33375 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33376 return clauses;
33377 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33379 cp_parser_error (parser, "expected identifier");
33380 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33381 return error_mark_node;
33384 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33385 parser->colon_corrects_to_scope_p = false;
33386 while (1)
33388 cp_token *token = cp_lexer_peek_token (parser->lexer);
33389 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33391 cp_parser_error (parser, "expected variable-name");
33392 clauses = error_mark_node;
33393 break;
33396 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
33397 false, false);
33398 tree decl = cp_parser_lookup_name_simple (parser, var_name,
33399 token->location);
33400 if (decl == error_mark_node)
33402 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
33403 token->location);
33404 clauses = error_mark_node;
33406 else
33408 tree e = NULL_TREE;
33409 tree step_size = integer_one_node;
33411 /* If present, parse the linear step. Otherwise, assume the default
33412 value of 1. */
33413 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
33415 cp_lexer_consume_token (parser->lexer);
33417 e = cp_parser_assignment_expression (parser);
33418 e = maybe_constant_value (e);
33420 if (e == error_mark_node)
33422 /* If an error has occurred, then the whole pragma is
33423 considered ill-formed. Thus, no reason to keep
33424 parsing. */
33425 clauses = error_mark_node;
33426 break;
33428 else if (type_dependent_expression_p (e)
33429 || value_dependent_expression_p (e)
33430 || (TREE_TYPE (e)
33431 && INTEGRAL_TYPE_P (TREE_TYPE (e))
33432 && (TREE_CONSTANT (e)
33433 || DECL_P (e))))
33434 step_size = e;
33435 else
33436 cp_parser_error (parser,
33437 "step size must be an integer constant "
33438 "expression or an integer variable");
33441 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
33442 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33443 OMP_CLAUSE_DECL (l) = decl;
33444 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
33445 OMP_CLAUSE_CHAIN (l) = clauses;
33446 clauses = l;
33448 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33449 cp_lexer_consume_token (parser->lexer);
33450 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33451 break;
33452 else
33454 error_at (cp_lexer_peek_token (parser->lexer)->location,
33455 "expected %<,%> or %<)%> after %qE", decl);
33456 clauses = error_mark_node;
33457 break;
33460 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33461 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33462 return clauses;
33465 /* Returns the name of the next clause. If the clause is not
33466 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33467 token is not consumed. Otherwise, the appropriate enum from the
33468 pragma_simd_clause is returned and the token is consumed. */
33470 static pragma_omp_clause
33471 cp_parser_cilk_simd_clause_name (cp_parser *parser)
33473 pragma_omp_clause clause_type;
33474 cp_token *token = cp_lexer_peek_token (parser->lexer);
33476 if (token->keyword == RID_PRIVATE)
33477 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
33478 else if (!token->u.value || token->type != CPP_NAME)
33479 return PRAGMA_CILK_CLAUSE_NONE;
33480 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
33481 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
33482 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
33483 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
33484 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
33485 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
33486 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
33487 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
33488 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
33489 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
33490 else
33491 return PRAGMA_CILK_CLAUSE_NONE;
33493 cp_lexer_consume_token (parser->lexer);
33494 return clause_type;
33497 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
33499 static tree
33500 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
33502 tree clauses = NULL_TREE;
33504 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33505 && clauses != error_mark_node)
33507 pragma_omp_clause c_kind;
33508 c_kind = cp_parser_cilk_simd_clause_name (parser);
33509 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
33510 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
33511 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
33512 clauses = cp_parser_cilk_simd_linear (parser, clauses);
33513 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
33514 /* Use the OpenMP 4.0 equivalent function. */
33515 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
33516 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
33517 /* Use the OpenMP 4.0 equivalent function. */
33518 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33519 clauses);
33520 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
33521 /* Use the OMP 4.0 equivalent function. */
33522 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33523 clauses);
33524 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
33525 /* Use the OMP 4.0 equivalent function. */
33526 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33527 else
33529 clauses = error_mark_node;
33530 cp_parser_error (parser, "expected %<#pragma simd%> clause");
33531 break;
33535 cp_parser_skip_to_pragma_eol (parser, pragma_token);
33537 if (clauses == error_mark_node)
33538 return error_mark_node;
33539 else
33540 return c_finish_cilk_clauses (clauses);
33543 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
33545 static void
33546 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
33548 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
33550 if (clauses == error_mark_node)
33551 return;
33553 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
33555 error_at (cp_lexer_peek_token (parser->lexer)->location,
33556 "for statement expected");
33557 return;
33560 tree sb = begin_omp_structured_block ();
33561 int save = cp_parser_begin_omp_structured_block (parser);
33562 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
33563 if (ret)
33564 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
33565 cp_parser_end_omp_structured_block (parser, save);
33566 add_stmt (finish_omp_structured_block (sb));
33569 /* Main entry-point for parsing Cilk Plus _Cilk_for
33570 loops. The return value is error_mark_node
33571 when errors happen and CILK_FOR tree on success. */
33573 static tree
33574 cp_parser_cilk_for (cp_parser *parser, tree grain)
33576 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
33577 gcc_unreachable ();
33579 tree sb = begin_omp_structured_block ();
33580 int save = cp_parser_begin_omp_structured_block (parser);
33582 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
33583 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
33584 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
33585 clauses = finish_omp_clauses (clauses);
33587 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
33588 if (ret)
33589 cpp_validate_cilk_plus_loop (ret);
33590 else
33591 ret = error_mark_node;
33593 cp_parser_end_omp_structured_block (parser, save);
33594 add_stmt (finish_omp_structured_block (sb));
33595 return ret;
33598 /* Create an identifier for a generic parameter type (a synthesized
33599 template parameter implied by `auto' or a concept identifier). */
33601 static GTY(()) int generic_parm_count;
33602 static tree
33603 make_generic_type_name ()
33605 char buf[32];
33606 sprintf (buf, "auto:%d", ++generic_parm_count);
33607 return get_identifier (buf);
33610 /* Predicate that behaves as is_auto_or_concept but matches the parent
33611 node of the generic type rather than the generic type itself. This
33612 allows for type transformation in add_implicit_template_parms. */
33614 static inline bool
33615 tree_type_is_auto_or_concept (const_tree t)
33617 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
33620 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33621 (creating a new template parameter list if necessary). Returns the newly
33622 created template type parm. */
33624 tree
33625 synthesize_implicit_template_parm (cp_parser *parser)
33627 gcc_assert (current_binding_level->kind == sk_function_parms);
33629 /* We are either continuing a function template that already contains implicit
33630 template parameters, creating a new fully-implicit function template, or
33631 extending an existing explicit function template with implicit template
33632 parameters. */
33634 cp_binding_level *const entry_scope = current_binding_level;
33636 bool become_template = false;
33637 cp_binding_level *parent_scope = 0;
33639 if (parser->implicit_template_scope)
33641 gcc_assert (parser->implicit_template_parms);
33643 current_binding_level = parser->implicit_template_scope;
33645 else
33647 /* Roll back to the existing template parameter scope (in the case of
33648 extending an explicit function template) or introduce a new template
33649 parameter scope ahead of the function parameter scope (or class scope
33650 in the case of out-of-line member definitions). The function scope is
33651 added back after template parameter synthesis below. */
33653 cp_binding_level *scope = entry_scope;
33655 while (scope->kind == sk_function_parms)
33657 parent_scope = scope;
33658 scope = scope->level_chain;
33660 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
33662 /* If not defining a class, then any class scope is a scope level in
33663 an out-of-line member definition. In this case simply wind back
33664 beyond the first such scope to inject the template parameter list.
33665 Otherwise wind back to the class being defined. The latter can
33666 occur in class member friend declarations such as:
33668 class A {
33669 void foo (auto);
33671 class B {
33672 friend void A::foo (auto);
33675 The template parameter list synthesized for the friend declaration
33676 must be injected in the scope of 'B'. This can also occur in
33677 erroneous cases such as:
33679 struct A {
33680 struct B {
33681 void foo (auto);
33683 void B::foo (auto) {}
33686 Here the attempted definition of 'B::foo' within 'A' is ill-formed
33687 but, nevertheless, the template parameter list synthesized for the
33688 declarator should be injected into the scope of 'A' as if the
33689 ill-formed template was specified explicitly. */
33691 while (scope->kind == sk_class && !scope->defining_class_p)
33693 parent_scope = scope;
33694 scope = scope->level_chain;
33698 current_binding_level = scope;
33700 if (scope->kind != sk_template_parms
33701 || !function_being_declared_is_template_p (parser))
33703 /* Introduce a new template parameter list for implicit template
33704 parameters. */
33706 become_template = true;
33708 parser->implicit_template_scope
33709 = begin_scope (sk_template_parms, NULL);
33711 ++processing_template_decl;
33713 parser->fully_implicit_function_template_p = true;
33714 ++parser->num_template_parameter_lists;
33716 else
33718 /* Synthesize implicit template parameters at the end of the explicit
33719 template parameter list. */
33721 gcc_assert (current_template_parms);
33723 parser->implicit_template_scope = scope;
33725 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33726 parser->implicit_template_parms
33727 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
33731 /* Synthesize a new template parameter and track the current template
33732 parameter chain with implicit_template_parms. */
33734 tree synth_id = make_generic_type_name ();
33735 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33736 synth_id);
33737 tree new_parm
33738 = process_template_parm (parser->implicit_template_parms,
33739 input_location,
33740 build_tree_list (NULL_TREE, synth_tmpl_parm),
33741 /*non_type=*/false,
33742 /*param_pack=*/false);
33745 if (parser->implicit_template_parms)
33746 parser->implicit_template_parms
33747 = TREE_CHAIN (parser->implicit_template_parms);
33748 else
33749 parser->implicit_template_parms = new_parm;
33751 tree new_type = TREE_TYPE (getdecls ());
33753 /* If creating a fully implicit function template, start the new implicit
33754 template parameter list with this synthesized type, otherwise grow the
33755 current template parameter list. */
33757 if (become_template)
33759 parent_scope->level_chain = current_binding_level;
33761 tree new_parms = make_tree_vec (1);
33762 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33763 current_template_parms = tree_cons (size_int (processing_template_decl),
33764 new_parms, current_template_parms);
33766 else
33768 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33769 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33770 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33771 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33774 current_binding_level = entry_scope;
33776 return new_type;
33779 /* Finish the declaration of a fully implicit function template. Such a
33780 template has no explicit template parameter list so has not been through the
33781 normal template head and tail processing. synthesize_implicit_template_parm
33782 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33783 provided if the declaration is a class member such that its template
33784 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33785 form is returned. Otherwise NULL_TREE is returned. */
33787 tree
33788 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33790 gcc_assert (parser->fully_implicit_function_template_p);
33792 if (member_decl_opt && member_decl_opt != error_mark_node
33793 && DECL_VIRTUAL_P (member_decl_opt))
33795 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33796 "implicit templates may not be %<virtual%>");
33797 DECL_VIRTUAL_P (member_decl_opt) = false;
33800 if (member_decl_opt)
33801 member_decl_opt = finish_member_template_decl (member_decl_opt);
33802 end_template_decl ();
33804 parser->fully_implicit_function_template_p = false;
33805 --parser->num_template_parameter_lists;
33807 return member_decl_opt;
33810 #include "gt-cp-parser.h"