svn merge -r215707:216846 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / cp / parser.c
blobf5747e5787f580b6f76ffa7becd72ef9a2b438d9
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2014 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 "tree.h"
28 #include "print-tree.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "cp-tree.h"
33 #include "intl.h"
34 #include "c-family/c-pragma.h"
35 #include "decl.h"
36 #include "flags.h"
37 #include "diagnostic-core.h"
38 #include "target.h"
39 #include "hash-map.h"
40 #include "is-a.h"
41 #include "plugin-api.h"
42 #include "vec.h"
43 #include "hashtab.h"
44 #include "hash-set.h"
45 #include "machmode.h"
46 #include "hard-reg-set.h"
47 #include "input.h"
48 #include "function.h"
49 #include "ipa-ref.h"
50 #include "cgraph.h"
51 #include "c-family/c-common.h"
52 #include "c-family/c-objc.h"
53 #include "plugin.h"
54 #include "tree-pretty-print.h"
55 #include "parser.h"
56 #include "type-utils.h"
57 #include "omp-low.h"
60 /* The lexer. */
62 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
63 and c-lex.c) and the C++ parser. */
65 static cp_token eof_token =
67 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
70 /* The various kinds of non integral constant we encounter. */
71 typedef enum non_integral_constant {
72 NIC_NONE,
73 /* floating-point literal */
74 NIC_FLOAT,
75 /* %<this%> */
76 NIC_THIS,
77 /* %<__FUNCTION__%> */
78 NIC_FUNC_NAME,
79 /* %<__PRETTY_FUNCTION__%> */
80 NIC_PRETTY_FUNC,
81 /* %<__func__%> */
82 NIC_C99_FUNC,
83 /* "%<va_arg%> */
84 NIC_VA_ARG,
85 /* a cast */
86 NIC_CAST,
87 /* %<typeid%> operator */
88 NIC_TYPEID,
89 /* non-constant compound literals */
90 NIC_NCC,
91 /* a function call */
92 NIC_FUNC_CALL,
93 /* an increment */
94 NIC_INC,
95 /* an decrement */
96 NIC_DEC,
97 /* an array reference */
98 NIC_ARRAY_REF,
99 /* %<->%> */
100 NIC_ARROW,
101 /* %<.%> */
102 NIC_POINT,
103 /* the address of a label */
104 NIC_ADDR_LABEL,
105 /* %<*%> */
106 NIC_STAR,
107 /* %<&%> */
108 NIC_ADDR,
109 /* %<++%> */
110 NIC_PREINCREMENT,
111 /* %<--%> */
112 NIC_PREDECREMENT,
113 /* %<new%> */
114 NIC_NEW,
115 /* %<delete%> */
116 NIC_DEL,
117 /* calls to overloaded operators */
118 NIC_OVERLOADED,
119 /* an assignment */
120 NIC_ASSIGNMENT,
121 /* a comma operator */
122 NIC_COMMA,
123 /* a call to a constructor */
124 NIC_CONSTRUCTOR,
125 /* a transaction expression */
126 NIC_TRANSACTION
127 } non_integral_constant;
129 /* The various kinds of errors about name-lookup failing. */
130 typedef enum name_lookup_error {
131 /* NULL */
132 NLE_NULL,
133 /* is not a type */
134 NLE_TYPE,
135 /* is not a class or namespace */
136 NLE_CXX98,
137 /* is not a class, namespace, or enumeration */
138 NLE_NOT_CXX98
139 } name_lookup_error;
141 /* The various kinds of required token */
142 typedef enum required_token {
143 RT_NONE,
144 RT_SEMICOLON, /* ';' */
145 RT_OPEN_PAREN, /* '(' */
146 RT_CLOSE_BRACE, /* '}' */
147 RT_OPEN_BRACE, /* '{' */
148 RT_CLOSE_SQUARE, /* ']' */
149 RT_OPEN_SQUARE, /* '[' */
150 RT_COMMA, /* ',' */
151 RT_SCOPE, /* '::' */
152 RT_LESS, /* '<' */
153 RT_GREATER, /* '>' */
154 RT_EQ, /* '=' */
155 RT_ELLIPSIS, /* '...' */
156 RT_MULT, /* '*' */
157 RT_COMPL, /* '~' */
158 RT_COLON, /* ':' */
159 RT_COLON_SCOPE, /* ':' or '::' */
160 RT_CLOSE_PAREN, /* ')' */
161 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
162 RT_PRAGMA_EOL, /* end of line */
163 RT_NAME, /* identifier */
165 /* The type is CPP_KEYWORD */
166 RT_NEW, /* new */
167 RT_DELETE, /* delete */
168 RT_RETURN, /* return */
169 RT_WHILE, /* while */
170 RT_EXTERN, /* extern */
171 RT_STATIC_ASSERT, /* static_assert */
172 RT_DECLTYPE, /* decltype */
173 RT_OPERATOR, /* operator */
174 RT_CLASS, /* class */
175 RT_TEMPLATE, /* template */
176 RT_NAMESPACE, /* namespace */
177 RT_USING, /* using */
178 RT_ASM, /* asm */
179 RT_TRY, /* try */
180 RT_CATCH, /* catch */
181 RT_THROW, /* throw */
182 RT_LABEL, /* __label__ */
183 RT_AT_TRY, /* @try */
184 RT_AT_SYNCHRONIZED, /* @synchronized */
185 RT_AT_THROW, /* @throw */
187 RT_SELECT, /* selection-statement */
188 RT_INTERATION, /* iteration-statement */
189 RT_JUMP, /* jump-statement */
190 RT_CLASS_KEY, /* class-key */
191 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
192 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
193 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
194 RT_TRANSACTION_CANCEL /* __transaction_cancel */
195 } required_token;
197 /* Prototypes. */
199 static cp_lexer *cp_lexer_new_main
200 (void);
201 static cp_lexer *cp_lexer_new_from_tokens
202 (cp_token_cache *tokens);
203 static void cp_lexer_destroy
204 (cp_lexer *);
205 static int cp_lexer_saving_tokens
206 (const cp_lexer *);
207 static cp_token *cp_lexer_token_at
208 (cp_lexer *, cp_token_position);
209 static void cp_lexer_get_preprocessor_token
210 (cp_lexer *, cp_token *);
211 static inline cp_token *cp_lexer_peek_token
212 (cp_lexer *);
213 static cp_token *cp_lexer_peek_nth_token
214 (cp_lexer *, size_t);
215 static inline bool cp_lexer_next_token_is
216 (cp_lexer *, enum cpp_ttype);
217 static bool cp_lexer_next_token_is_not
218 (cp_lexer *, enum cpp_ttype);
219 static bool cp_lexer_next_token_is_keyword
220 (cp_lexer *, enum rid);
221 static cp_token *cp_lexer_consume_token
222 (cp_lexer *);
223 static void cp_lexer_purge_token
224 (cp_lexer *);
225 static void cp_lexer_purge_tokens_after
226 (cp_lexer *, cp_token_position);
227 static void cp_lexer_save_tokens
228 (cp_lexer *);
229 static void cp_lexer_commit_tokens
230 (cp_lexer *);
231 static void cp_lexer_rollback_tokens
232 (cp_lexer *);
233 static void cp_lexer_print_token
234 (FILE *, cp_token *);
235 static inline bool cp_lexer_debugging_p
236 (cp_lexer *);
237 static void cp_lexer_start_debugging
238 (cp_lexer *) ATTRIBUTE_UNUSED;
239 static void cp_lexer_stop_debugging
240 (cp_lexer *) ATTRIBUTE_UNUSED;
242 static cp_token_cache *cp_token_cache_new
243 (cp_token *, cp_token *);
245 static void cp_parser_initial_pragma
246 (cp_token *);
248 static tree cp_literal_operator_id
249 (const char *);
251 static void cp_parser_cilk_simd
252 (cp_parser *, cp_token *);
253 static tree cp_parser_cilk_for
254 (cp_parser *, tree);
255 static bool cp_parser_omp_declare_reduction_exprs
256 (tree, cp_parser *);
257 static tree cp_parser_cilk_simd_vectorlength
258 (cp_parser *, tree, bool);
260 /* Manifest constants. */
261 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
262 #define CP_SAVED_TOKEN_STACK 5
264 /* Variables. */
266 /* The stream to which debugging output should be written. */
267 static FILE *cp_lexer_debug_stream;
269 /* Nonzero if we are parsing an unevaluated operand: an operand to
270 sizeof, typeof, or alignof. */
271 int cp_unevaluated_operand;
273 /* Dump up to NUM tokens in BUFFER to FILE starting with token
274 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
275 first token in BUFFER. If NUM is 0, dump all the tokens. If
276 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
277 highlighted by surrounding it in [[ ]]. */
279 static void
280 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
281 cp_token *start_token, unsigned num,
282 cp_token *curr_token)
284 unsigned i, nprinted;
285 cp_token *token;
286 bool do_print;
288 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
290 if (buffer == NULL)
291 return;
293 if (num == 0)
294 num = buffer->length ();
296 if (start_token == NULL)
297 start_token = buffer->address ();
299 if (start_token > buffer->address ())
301 cp_lexer_print_token (file, &(*buffer)[0]);
302 fprintf (file, " ... ");
305 do_print = false;
306 nprinted = 0;
307 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
309 if (token == start_token)
310 do_print = true;
312 if (!do_print)
313 continue;
315 nprinted++;
316 if (token == curr_token)
317 fprintf (file, "[[");
319 cp_lexer_print_token (file, token);
321 if (token == curr_token)
322 fprintf (file, "]]");
324 switch (token->type)
326 case CPP_SEMICOLON:
327 case CPP_OPEN_BRACE:
328 case CPP_CLOSE_BRACE:
329 case CPP_EOF:
330 fputc ('\n', file);
331 break;
333 default:
334 fputc (' ', file);
338 if (i == num && i < buffer->length ())
340 fprintf (file, " ... ");
341 cp_lexer_print_token (file, &buffer->last ());
344 fprintf (file, "\n");
348 /* Dump all tokens in BUFFER to stderr. */
350 void
351 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
353 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
356 DEBUG_FUNCTION void
357 debug (vec<cp_token, va_gc> &ref)
359 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
362 DEBUG_FUNCTION void
363 debug (vec<cp_token, va_gc> *ptr)
365 if (ptr)
366 debug (*ptr);
367 else
368 fprintf (stderr, "<nil>\n");
372 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
373 description for T. */
375 static void
376 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
378 if (t)
380 fprintf (file, "%s: ", desc);
381 print_node_brief (file, "", t, 0);
386 /* Dump parser context C to FILE. */
388 static void
389 cp_debug_print_context (FILE *file, cp_parser_context *c)
391 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
392 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
393 print_node_brief (file, "", c->object_type, 0);
394 fprintf (file, "}\n");
398 /* Print the stack of parsing contexts to FILE starting with FIRST. */
400 static void
401 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
403 unsigned i;
404 cp_parser_context *c;
406 fprintf (file, "Parsing context stack:\n");
407 for (i = 0, c = first; c; c = c->next, i++)
409 fprintf (file, "\t#%u: ", i);
410 cp_debug_print_context (file, c);
415 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
417 static void
418 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
420 if (flag)
421 fprintf (file, "%s: true\n", desc);
425 /* Print an unparsed function entry UF to FILE. */
427 static void
428 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
430 unsigned i;
431 cp_default_arg_entry *default_arg_fn;
432 tree fn;
434 fprintf (file, "\tFunctions with default args:\n");
435 for (i = 0;
436 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
437 i++)
439 fprintf (file, "\t\tClass type: ");
440 print_node_brief (file, "", default_arg_fn->class_type, 0);
441 fprintf (file, "\t\tDeclaration: ");
442 print_node_brief (file, "", default_arg_fn->decl, 0);
443 fprintf (file, "\n");
446 fprintf (file, "\n\tFunctions with definitions that require "
447 "post-processing\n\t\t");
448 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
450 print_node_brief (file, "", fn, 0);
451 fprintf (file, " ");
453 fprintf (file, "\n");
455 fprintf (file, "\n\tNon-static data members with initializers that require "
456 "post-processing\n\t\t");
457 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
459 print_node_brief (file, "", fn, 0);
460 fprintf (file, " ");
462 fprintf (file, "\n");
466 /* Print the stack of unparsed member functions S to FILE. */
468 static void
469 cp_debug_print_unparsed_queues (FILE *file,
470 vec<cp_unparsed_functions_entry, va_gc> *s)
472 unsigned i;
473 cp_unparsed_functions_entry *uf;
475 fprintf (file, "Unparsed functions\n");
476 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
478 fprintf (file, "#%u:\n", i);
479 cp_debug_print_unparsed_function (file, uf);
484 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
485 the given PARSER. If FILE is NULL, the output is printed on stderr. */
487 static void
488 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
490 cp_token *next_token, *first_token, *start_token;
492 if (file == NULL)
493 file = stderr;
495 next_token = parser->lexer->next_token;
496 first_token = parser->lexer->buffer->address ();
497 start_token = (next_token > first_token + window_size / 2)
498 ? next_token - window_size / 2
499 : first_token;
500 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
501 next_token);
505 /* Dump debugging information for the given PARSER. If FILE is NULL,
506 the output is printed on stderr. */
508 void
509 cp_debug_parser (FILE *file, cp_parser *parser)
511 const size_t window_size = 20;
512 cp_token *token;
513 expanded_location eloc;
515 if (file == NULL)
516 file = stderr;
518 fprintf (file, "Parser state\n\n");
519 fprintf (file, "Number of tokens: %u\n",
520 vec_safe_length (parser->lexer->buffer));
521 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
522 cp_debug_print_tree_if_set (file, "Object scope",
523 parser->object_scope);
524 cp_debug_print_tree_if_set (file, "Qualifying scope",
525 parser->qualifying_scope);
526 cp_debug_print_context_stack (file, parser->context);
527 cp_debug_print_flag (file, "Allow GNU extensions",
528 parser->allow_gnu_extensions_p);
529 cp_debug_print_flag (file, "'>' token is greater-than",
530 parser->greater_than_is_operator_p);
531 cp_debug_print_flag (file, "Default args allowed in current "
532 "parameter list", parser->default_arg_ok_p);
533 cp_debug_print_flag (file, "Parsing integral constant-expression",
534 parser->integral_constant_expression_p);
535 cp_debug_print_flag (file, "Allow non-constant expression in current "
536 "constant-expression",
537 parser->allow_non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Seen non-constant expression",
539 parser->non_integral_constant_expression_p);
540 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
541 "current context",
542 parser->local_variables_forbidden_p);
543 cp_debug_print_flag (file, "In unbraced linkage specification",
544 parser->in_unbraced_linkage_specification_p);
545 cp_debug_print_flag (file, "Parsing a declarator",
546 parser->in_declarator_p);
547 cp_debug_print_flag (file, "In template argument list",
548 parser->in_template_argument_list_p);
549 cp_debug_print_flag (file, "Parsing an iteration statement",
550 parser->in_statement & IN_ITERATION_STMT);
551 cp_debug_print_flag (file, "Parsing a switch statement",
552 parser->in_statement & IN_SWITCH_STMT);
553 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
554 parser->in_statement & IN_OMP_BLOCK);
555 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
556 parser->in_statement & IN_CILK_SIMD_FOR);
557 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
558 parser->in_statement & IN_OMP_FOR);
559 cp_debug_print_flag (file, "Parsing an if statement",
560 parser->in_statement & IN_IF_STMT);
561 cp_debug_print_flag (file, "Parsing a type-id in an expression "
562 "context", parser->in_type_id_in_expr_p);
563 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
564 parser->implicit_extern_c);
565 cp_debug_print_flag (file, "String expressions should be translated "
566 "to execution character set",
567 parser->translate_strings_p);
568 cp_debug_print_flag (file, "Parsing function body outside of a "
569 "local class", parser->in_function_body);
570 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
571 parser->colon_corrects_to_scope_p);
572 cp_debug_print_flag (file, "Colon doesn't start a class definition",
573 parser->colon_doesnt_start_class_def_p);
574 if (parser->type_definition_forbidden_message)
575 fprintf (file, "Error message for forbidden type definitions: %s\n",
576 parser->type_definition_forbidden_message);
577 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
578 fprintf (file, "Number of class definitions in progress: %u\n",
579 parser->num_classes_being_defined);
580 fprintf (file, "Number of template parameter lists for the current "
581 "declaration: %u\n", parser->num_template_parameter_lists);
582 cp_debug_parser_tokens (file, parser, window_size);
583 token = parser->lexer->next_token;
584 fprintf (file, "Next token to parse:\n");
585 fprintf (file, "\tToken: ");
586 cp_lexer_print_token (file, token);
587 eloc = expand_location (token->location);
588 fprintf (file, "\n\tFile: %s\n", eloc.file);
589 fprintf (file, "\tLine: %d\n", eloc.line);
590 fprintf (file, "\tColumn: %d\n", eloc.column);
593 DEBUG_FUNCTION void
594 debug (cp_parser &ref)
596 cp_debug_parser (stderr, &ref);
599 DEBUG_FUNCTION void
600 debug (cp_parser *ptr)
602 if (ptr)
603 debug (*ptr);
604 else
605 fprintf (stderr, "<nil>\n");
608 /* Allocate memory for a new lexer object and return it. */
610 static cp_lexer *
611 cp_lexer_alloc (void)
613 cp_lexer *lexer;
615 c_common_no_more_pch ();
617 /* Allocate the memory. */
618 lexer = ggc_cleared_alloc<cp_lexer> ();
620 /* Initially we are not debugging. */
621 lexer->debugging_p = false;
623 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
625 /* Create the buffer. */
626 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
628 return lexer;
632 /* Create a new main C++ lexer, the lexer that gets tokens from the
633 preprocessor. */
635 static cp_lexer *
636 cp_lexer_new_main (void)
638 cp_lexer *lexer;
639 cp_token token;
641 /* It's possible that parsing the first pragma will load a PCH file,
642 which is a GC collection point. So we have to do that before
643 allocating any memory. */
644 cp_parser_initial_pragma (&token);
646 lexer = cp_lexer_alloc ();
648 /* Put the first token in the buffer. */
649 lexer->buffer->quick_push (token);
651 /* Get the remaining tokens from the preprocessor. */
652 while (token.type != CPP_EOF)
654 cp_lexer_get_preprocessor_token (lexer, &token);
655 vec_safe_push (lexer->buffer, token);
658 lexer->last_token = lexer->buffer->address ()
659 + lexer->buffer->length ()
660 - 1;
661 lexer->next_token = lexer->buffer->length ()
662 ? lexer->buffer->address ()
663 : &eof_token;
665 /* Subsequent preprocessor diagnostics should use compiler
666 diagnostic functions to get the compiler source location. */
667 done_lexing = true;
669 gcc_assert (!lexer->next_token->purged_p);
670 return lexer;
673 /* Create a new lexer whose token stream is primed with the tokens in
674 CACHE. When these tokens are exhausted, no new tokens will be read. */
676 static cp_lexer *
677 cp_lexer_new_from_tokens (cp_token_cache *cache)
679 cp_token *first = cache->first;
680 cp_token *last = cache->last;
681 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
683 /* We do not own the buffer. */
684 lexer->buffer = NULL;
685 lexer->next_token = first == last ? &eof_token : first;
686 lexer->last_token = last;
688 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
690 /* Initially we are not debugging. */
691 lexer->debugging_p = false;
693 gcc_assert (!lexer->next_token->purged_p);
694 return lexer;
697 /* Frees all resources associated with LEXER. */
699 static void
700 cp_lexer_destroy (cp_lexer *lexer)
702 vec_free (lexer->buffer);
703 lexer->saved_tokens.release ();
704 ggc_free (lexer);
707 /* Returns nonzero if debugging information should be output. */
709 static inline bool
710 cp_lexer_debugging_p (cp_lexer *lexer)
712 return lexer->debugging_p;
716 static inline cp_token_position
717 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
719 gcc_assert (!previous_p || lexer->next_token != &eof_token);
721 return lexer->next_token - previous_p;
724 static inline cp_token *
725 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
727 return pos;
730 static inline void
731 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
733 lexer->next_token = cp_lexer_token_at (lexer, pos);
736 static inline cp_token_position
737 cp_lexer_previous_token_position (cp_lexer *lexer)
739 if (lexer->next_token == &eof_token)
740 return lexer->last_token - 1;
741 else
742 return cp_lexer_token_position (lexer, true);
745 static inline cp_token *
746 cp_lexer_previous_token (cp_lexer *lexer)
748 cp_token_position tp = cp_lexer_previous_token_position (lexer);
750 return cp_lexer_token_at (lexer, tp);
753 /* nonzero if we are presently saving tokens. */
755 static inline int
756 cp_lexer_saving_tokens (const cp_lexer* lexer)
758 return lexer->saved_tokens.length () != 0;
761 /* Store the next token from the preprocessor in *TOKEN. Return true
762 if we reach EOF. If LEXER is NULL, assume we are handling an
763 initial #pragma pch_preprocess, and thus want the lexer to return
764 processed strings. */
766 static void
767 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
769 static int is_extern_c = 0;
771 /* Get a new token from the preprocessor. */
772 token->type
773 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
774 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
775 token->keyword = RID_MAX;
776 token->pragma_kind = PRAGMA_NONE;
777 token->purged_p = false;
778 token->error_reported = false;
780 /* On some systems, some header files are surrounded by an
781 implicit extern "C" block. Set a flag in the token if it
782 comes from such a header. */
783 is_extern_c += pending_lang_change;
784 pending_lang_change = 0;
785 token->implicit_extern_c = is_extern_c > 0;
787 /* Check to see if this token is a keyword. */
788 if (token->type == CPP_NAME)
790 if (C_IS_RESERVED_WORD (token->u.value))
792 /* Mark this token as a keyword. */
793 token->type = CPP_KEYWORD;
794 /* Record which keyword. */
795 token->keyword = C_RID_CODE (token->u.value);
797 else
799 if (warn_cxx0x_compat
800 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
801 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
803 /* Warn about the C++0x keyword (but still treat it as
804 an identifier). */
805 warning (OPT_Wc__0x_compat,
806 "identifier %qE is a keyword in C++11",
807 token->u.value);
809 /* Clear out the C_RID_CODE so we don't warn about this
810 particular identifier-turned-keyword again. */
811 C_SET_RID_CODE (token->u.value, RID_MAX);
814 token->keyword = RID_MAX;
817 else if (token->type == CPP_AT_NAME)
819 /* This only happens in Objective-C++; it must be a keyword. */
820 token->type = CPP_KEYWORD;
821 switch (C_RID_CODE (token->u.value))
823 /* Replace 'class' with '@class', 'private' with '@private',
824 etc. This prevents confusion with the C++ keyword
825 'class', and makes the tokens consistent with other
826 Objective-C 'AT' keywords. For example '@class' is
827 reported as RID_AT_CLASS which is consistent with
828 '@synchronized', which is reported as
829 RID_AT_SYNCHRONIZED.
831 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
832 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
833 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
834 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
835 case RID_THROW: token->keyword = RID_AT_THROW; break;
836 case RID_TRY: token->keyword = RID_AT_TRY; break;
837 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
838 default: token->keyword = C_RID_CODE (token->u.value);
841 else if (token->type == CPP_PRAGMA)
843 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
844 token->pragma_kind = ((enum pragma_kind)
845 TREE_INT_CST_LOW (token->u.value));
846 token->u.value = NULL_TREE;
850 /* Update the globals input_location and the input file stack from TOKEN. */
851 static inline void
852 cp_lexer_set_source_position_from_token (cp_token *token)
854 if (token->type != CPP_EOF)
856 input_location = token->location;
860 /* Update the globals input_location and the input file stack from LEXER. */
861 static inline void
862 cp_lexer_set_source_position (cp_lexer *lexer)
864 cp_token *token = cp_lexer_peek_token (lexer);
865 cp_lexer_set_source_position_from_token (token);
868 /* Return a pointer to the next token in the token stream, but do not
869 consume it. */
871 static inline cp_token *
872 cp_lexer_peek_token (cp_lexer *lexer)
874 if (cp_lexer_debugging_p (lexer))
876 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
877 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
878 putc ('\n', cp_lexer_debug_stream);
880 return lexer->next_token;
883 /* Return true if the next token has the indicated TYPE. */
885 static inline bool
886 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
888 return cp_lexer_peek_token (lexer)->type == type;
891 /* Return true if the next token does not have the indicated TYPE. */
893 static inline bool
894 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
896 return !cp_lexer_next_token_is (lexer, type);
899 /* Return true if the next token is the indicated KEYWORD. */
901 static inline bool
902 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
904 return cp_lexer_peek_token (lexer)->keyword == keyword;
907 static inline bool
908 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
910 return cp_lexer_peek_nth_token (lexer, n)->type == type;
913 static inline bool
914 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
916 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
919 /* Return true if the next token is not the indicated KEYWORD. */
921 static inline bool
922 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
924 return cp_lexer_peek_token (lexer)->keyword != keyword;
927 /* Return true if the next token is a keyword for a decl-specifier. */
929 static bool
930 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
932 cp_token *token;
934 token = cp_lexer_peek_token (lexer);
935 switch (token->keyword)
937 /* auto specifier: storage-class-specifier in C++,
938 simple-type-specifier in C++0x. */
939 case RID_AUTO:
940 /* Storage classes. */
941 case RID_REGISTER:
942 case RID_STATIC:
943 case RID_EXTERN:
944 case RID_MUTABLE:
945 case RID_THREAD:
946 /* Elaborated type specifiers. */
947 case RID_ENUM:
948 case RID_CLASS:
949 case RID_STRUCT:
950 case RID_UNION:
951 case RID_TYPENAME:
952 /* Simple type specifiers. */
953 case RID_CHAR:
954 case RID_CHAR16:
955 case RID_CHAR32:
956 case RID_WCHAR:
957 case RID_BOOL:
958 case RID_SHORT:
959 case RID_INT:
960 case RID_LONG:
961 case RID_SIGNED:
962 case RID_UNSIGNED:
963 case RID_FLOAT:
964 case RID_DOUBLE:
965 case RID_VOID:
966 /* GNU extensions. */
967 case RID_ATTRIBUTE:
968 case RID_TYPEOF:
969 /* C++0x extensions. */
970 case RID_DECLTYPE:
971 case RID_UNDERLYING_TYPE:
972 return true;
974 default:
975 if (token->keyword >= RID_FIRST_INT_N
976 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
977 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
978 return true;
979 return false;
983 /* Returns TRUE iff the token T begins a decltype type. */
985 static bool
986 token_is_decltype (cp_token *t)
988 return (t->keyword == RID_DECLTYPE
989 || t->type == CPP_DECLTYPE);
992 /* Returns TRUE iff the next token begins a decltype type. */
994 static bool
995 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
997 cp_token *t = cp_lexer_peek_token (lexer);
998 return token_is_decltype (t);
1001 /* Return a pointer to the Nth token in the token stream. If N is 1,
1002 then this is precisely equivalent to cp_lexer_peek_token (except
1003 that it is not inline). One would like to disallow that case, but
1004 there is one case (cp_parser_nth_token_starts_template_id) where
1005 the caller passes a variable for N and it might be 1. */
1007 static cp_token *
1008 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1010 cp_token *token;
1012 /* N is 1-based, not zero-based. */
1013 gcc_assert (n > 0);
1015 if (cp_lexer_debugging_p (lexer))
1016 fprintf (cp_lexer_debug_stream,
1017 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1019 --n;
1020 token = lexer->next_token;
1021 gcc_assert (!n || token != &eof_token);
1022 while (n != 0)
1024 ++token;
1025 if (token == lexer->last_token)
1027 token = &eof_token;
1028 break;
1031 if (!token->purged_p)
1032 --n;
1035 if (cp_lexer_debugging_p (lexer))
1037 cp_lexer_print_token (cp_lexer_debug_stream, token);
1038 putc ('\n', cp_lexer_debug_stream);
1041 return token;
1044 /* Return the next token, and advance the lexer's next_token pointer
1045 to point to the next non-purged token. */
1047 static cp_token *
1048 cp_lexer_consume_token (cp_lexer* lexer)
1050 cp_token *token = lexer->next_token;
1052 gcc_assert (token != &eof_token);
1053 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1057 lexer->next_token++;
1058 if (lexer->next_token == lexer->last_token)
1060 lexer->next_token = &eof_token;
1061 break;
1065 while (lexer->next_token->purged_p);
1067 cp_lexer_set_source_position_from_token (token);
1069 /* Provide debugging output. */
1070 if (cp_lexer_debugging_p (lexer))
1072 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1073 cp_lexer_print_token (cp_lexer_debug_stream, token);
1074 putc ('\n', cp_lexer_debug_stream);
1077 return token;
1080 /* Permanently remove the next token from the token stream, and
1081 advance the next_token pointer to refer to the next non-purged
1082 token. */
1084 static void
1085 cp_lexer_purge_token (cp_lexer *lexer)
1087 cp_token *tok = lexer->next_token;
1089 gcc_assert (tok != &eof_token);
1090 tok->purged_p = true;
1091 tok->location = UNKNOWN_LOCATION;
1092 tok->u.value = NULL_TREE;
1093 tok->keyword = RID_MAX;
1097 tok++;
1098 if (tok == lexer->last_token)
1100 tok = &eof_token;
1101 break;
1104 while (tok->purged_p);
1105 lexer->next_token = tok;
1108 /* Permanently remove all tokens after TOK, up to, but not
1109 including, the token that will be returned next by
1110 cp_lexer_peek_token. */
1112 static void
1113 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1115 cp_token *peek = lexer->next_token;
1117 if (peek == &eof_token)
1118 peek = lexer->last_token;
1120 gcc_assert (tok < peek);
1122 for ( tok += 1; tok != peek; tok += 1)
1124 tok->purged_p = true;
1125 tok->location = UNKNOWN_LOCATION;
1126 tok->u.value = NULL_TREE;
1127 tok->keyword = RID_MAX;
1131 /* Begin saving tokens. All tokens consumed after this point will be
1132 preserved. */
1134 static void
1135 cp_lexer_save_tokens (cp_lexer* lexer)
1137 /* Provide debugging output. */
1138 if (cp_lexer_debugging_p (lexer))
1139 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1141 lexer->saved_tokens.safe_push (lexer->next_token);
1144 /* Commit to the portion of the token stream most recently saved. */
1146 static void
1147 cp_lexer_commit_tokens (cp_lexer* lexer)
1149 /* Provide debugging output. */
1150 if (cp_lexer_debugging_p (lexer))
1151 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1153 lexer->saved_tokens.pop ();
1156 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1157 to the token stream. Stop saving tokens. */
1159 static void
1160 cp_lexer_rollback_tokens (cp_lexer* lexer)
1162 /* Provide debugging output. */
1163 if (cp_lexer_debugging_p (lexer))
1164 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1166 lexer->next_token = lexer->saved_tokens.pop ();
1169 /* RAII wrapper around the above functions, with sanity checking. Creating
1170 a variable saves tokens, which are committed when the variable is
1171 destroyed unless they are explicitly rolled back by calling the rollback
1172 member function. */
1174 struct saved_token_sentinel
1176 cp_lexer *lexer;
1177 unsigned len;
1178 bool commit;
1179 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1181 len = lexer->saved_tokens.length ();
1182 cp_lexer_save_tokens (lexer);
1184 void rollback ()
1186 cp_lexer_rollback_tokens (lexer);
1187 commit = false;
1189 ~saved_token_sentinel()
1191 if (commit)
1192 cp_lexer_commit_tokens (lexer);
1193 gcc_assert (lexer->saved_tokens.length () == len);
1197 /* Print a representation of the TOKEN on the STREAM. */
1199 static void
1200 cp_lexer_print_token (FILE * stream, cp_token *token)
1202 /* We don't use cpp_type2name here because the parser defines
1203 a few tokens of its own. */
1204 static const char *const token_names[] = {
1205 /* cpplib-defined token types */
1206 #define OP(e, s) #e,
1207 #define TK(e, s) #e,
1208 TTYPE_TABLE
1209 #undef OP
1210 #undef TK
1211 /* C++ parser token types - see "Manifest constants", above. */
1212 "KEYWORD",
1213 "TEMPLATE_ID",
1214 "NESTED_NAME_SPECIFIER",
1217 /* For some tokens, print the associated data. */
1218 switch (token->type)
1220 case CPP_KEYWORD:
1221 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1222 For example, `struct' is mapped to an INTEGER_CST. */
1223 if (!identifier_p (token->u.value))
1224 break;
1225 /* else fall through */
1226 case CPP_NAME:
1227 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1228 break;
1230 case CPP_STRING:
1231 case CPP_STRING16:
1232 case CPP_STRING32:
1233 case CPP_WSTRING:
1234 case CPP_UTF8STRING:
1235 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1236 break;
1238 case CPP_NUMBER:
1239 print_generic_expr (stream, token->u.value, 0);
1240 break;
1242 default:
1243 /* If we have a name for the token, print it out. Otherwise, we
1244 simply give the numeric code. */
1245 if (token->type < ARRAY_SIZE(token_names))
1246 fputs (token_names[token->type], stream);
1247 else
1248 fprintf (stream, "[%d]", token->type);
1249 break;
1253 DEBUG_FUNCTION void
1254 debug (cp_token &ref)
1256 cp_lexer_print_token (stderr, &ref);
1257 fprintf (stderr, "\n");
1260 DEBUG_FUNCTION void
1261 debug (cp_token *ptr)
1263 if (ptr)
1264 debug (*ptr);
1265 else
1266 fprintf (stderr, "<nil>\n");
1270 /* Start emitting debugging information. */
1272 static void
1273 cp_lexer_start_debugging (cp_lexer* lexer)
1275 lexer->debugging_p = true;
1276 cp_lexer_debug_stream = stderr;
1279 /* Stop emitting debugging information. */
1281 static void
1282 cp_lexer_stop_debugging (cp_lexer* lexer)
1284 lexer->debugging_p = false;
1285 cp_lexer_debug_stream = NULL;
1288 /* Create a new cp_token_cache, representing a range of tokens. */
1290 static cp_token_cache *
1291 cp_token_cache_new (cp_token *first, cp_token *last)
1293 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1294 cache->first = first;
1295 cache->last = last;
1296 return cache;
1299 /* Diagnose if #pragma omp declare simd isn't followed immediately
1300 by function declaration or definition. */
1302 static inline void
1303 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1305 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1307 error ("%<#pragma omp declare simd%> not immediately followed by "
1308 "function declaration or definition");
1309 parser->omp_declare_simd = NULL;
1313 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1314 and put that into "omp declare simd" attribute. */
1316 static inline void
1317 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1319 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1321 if (fndecl == error_mark_node)
1323 parser->omp_declare_simd = NULL;
1324 return;
1326 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1328 cp_ensure_no_omp_declare_simd (parser);
1329 return;
1334 /* Decl-specifiers. */
1336 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1338 static void
1339 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1341 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1344 /* Declarators. */
1346 /* Nothing other than the parser should be creating declarators;
1347 declarators are a semi-syntactic representation of C++ entities.
1348 Other parts of the front end that need to create entities (like
1349 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1351 static cp_declarator *make_call_declarator
1352 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1353 static cp_declarator *make_array_declarator
1354 (cp_declarator *, tree);
1355 static cp_declarator *make_pointer_declarator
1356 (cp_cv_quals, cp_declarator *, tree);
1357 static cp_declarator *make_reference_declarator
1358 (cp_cv_quals, cp_declarator *, bool, tree);
1359 static cp_parameter_declarator *make_parameter_declarator
1360 (cp_decl_specifier_seq *, cp_declarator *, tree);
1361 static cp_declarator *make_ptrmem_declarator
1362 (cp_cv_quals, tree, cp_declarator *, tree);
1364 /* An erroneous declarator. */
1365 static cp_declarator *cp_error_declarator;
1367 /* The obstack on which declarators and related data structures are
1368 allocated. */
1369 static struct obstack declarator_obstack;
1371 /* Alloc BYTES from the declarator memory pool. */
1373 static inline void *
1374 alloc_declarator (size_t bytes)
1376 return obstack_alloc (&declarator_obstack, bytes);
1379 /* Allocate a declarator of the indicated KIND. Clear fields that are
1380 common to all declarators. */
1382 static cp_declarator *
1383 make_declarator (cp_declarator_kind kind)
1385 cp_declarator *declarator;
1387 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1388 declarator->kind = kind;
1389 declarator->attributes = NULL_TREE;
1390 declarator->std_attributes = NULL_TREE;
1391 declarator->declarator = NULL;
1392 declarator->parameter_pack_p = false;
1393 declarator->id_loc = UNKNOWN_LOCATION;
1395 return declarator;
1398 /* Make a declarator for a generalized identifier. If
1399 QUALIFYING_SCOPE is non-NULL, the identifier is
1400 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1401 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1402 is, if any. */
1404 static cp_declarator *
1405 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1406 special_function_kind sfk)
1408 cp_declarator *declarator;
1410 /* It is valid to write:
1412 class C { void f(); };
1413 typedef C D;
1414 void D::f();
1416 The standard is not clear about whether `typedef const C D' is
1417 legal; as of 2002-09-15 the committee is considering that
1418 question. EDG 3.0 allows that syntax. Therefore, we do as
1419 well. */
1420 if (qualifying_scope && TYPE_P (qualifying_scope))
1421 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1423 gcc_assert (identifier_p (unqualified_name)
1424 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1425 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1427 declarator = make_declarator (cdk_id);
1428 declarator->u.id.qualifying_scope = qualifying_scope;
1429 declarator->u.id.unqualified_name = unqualified_name;
1430 declarator->u.id.sfk = sfk;
1432 return declarator;
1435 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1436 of modifiers such as const or volatile to apply to the pointer
1437 type, represented as identifiers. ATTRIBUTES represent the attributes that
1438 appertain to the pointer or reference. */
1440 cp_declarator *
1441 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1442 tree attributes)
1444 cp_declarator *declarator;
1446 declarator = make_declarator (cdk_pointer);
1447 declarator->declarator = target;
1448 declarator->u.pointer.qualifiers = cv_qualifiers;
1449 declarator->u.pointer.class_type = NULL_TREE;
1450 if (target)
1452 declarator->id_loc = target->id_loc;
1453 declarator->parameter_pack_p = target->parameter_pack_p;
1454 target->parameter_pack_p = false;
1456 else
1457 declarator->parameter_pack_p = false;
1459 declarator->std_attributes = attributes;
1461 return declarator;
1464 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1465 represent the attributes that appertain to the pointer or
1466 reference. */
1468 cp_declarator *
1469 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1470 bool rvalue_ref, tree attributes)
1472 cp_declarator *declarator;
1474 declarator = make_declarator (cdk_reference);
1475 declarator->declarator = target;
1476 declarator->u.reference.qualifiers = cv_qualifiers;
1477 declarator->u.reference.rvalue_ref = rvalue_ref;
1478 if (target)
1480 declarator->id_loc = target->id_loc;
1481 declarator->parameter_pack_p = target->parameter_pack_p;
1482 target->parameter_pack_p = false;
1484 else
1485 declarator->parameter_pack_p = false;
1487 declarator->std_attributes = attributes;
1489 return declarator;
1492 /* Like make_pointer_declarator -- but for a pointer to a non-static
1493 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1494 appertain to the pointer or reference. */
1496 cp_declarator *
1497 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1498 cp_declarator *pointee,
1499 tree attributes)
1501 cp_declarator *declarator;
1503 declarator = make_declarator (cdk_ptrmem);
1504 declarator->declarator = pointee;
1505 declarator->u.pointer.qualifiers = cv_qualifiers;
1506 declarator->u.pointer.class_type = class_type;
1508 if (pointee)
1510 declarator->parameter_pack_p = pointee->parameter_pack_p;
1511 pointee->parameter_pack_p = false;
1513 else
1514 declarator->parameter_pack_p = false;
1516 declarator->std_attributes = attributes;
1518 return declarator;
1521 /* Make a declarator for the function given by TARGET, with the
1522 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1523 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1524 indicates what exceptions can be thrown. */
1526 cp_declarator *
1527 make_call_declarator (cp_declarator *target,
1528 tree parms,
1529 cp_cv_quals cv_qualifiers,
1530 cp_virt_specifiers virt_specifiers,
1531 cp_ref_qualifier ref_qualifier,
1532 tree exception_specification,
1533 tree late_return_type)
1535 cp_declarator *declarator;
1537 declarator = make_declarator (cdk_function);
1538 declarator->declarator = target;
1539 declarator->u.function.parameters = parms;
1540 declarator->u.function.qualifiers = cv_qualifiers;
1541 declarator->u.function.virt_specifiers = virt_specifiers;
1542 declarator->u.function.ref_qualifier = ref_qualifier;
1543 declarator->u.function.exception_specification = exception_specification;
1544 declarator->u.function.late_return_type = late_return_type;
1545 if (target)
1547 declarator->id_loc = target->id_loc;
1548 declarator->parameter_pack_p = target->parameter_pack_p;
1549 target->parameter_pack_p = false;
1551 else
1552 declarator->parameter_pack_p = false;
1554 return declarator;
1557 /* Make a declarator for an array of BOUNDS elements, each of which is
1558 defined by ELEMENT. */
1560 cp_declarator *
1561 make_array_declarator (cp_declarator *element, tree bounds)
1563 cp_declarator *declarator;
1565 declarator = make_declarator (cdk_array);
1566 declarator->declarator = element;
1567 declarator->u.array.bounds = bounds;
1568 if (element)
1570 declarator->id_loc = element->id_loc;
1571 declarator->parameter_pack_p = element->parameter_pack_p;
1572 element->parameter_pack_p = false;
1574 else
1575 declarator->parameter_pack_p = false;
1577 return declarator;
1580 /* Determine whether the declarator we've seen so far can be a
1581 parameter pack, when followed by an ellipsis. */
1582 static bool
1583 declarator_can_be_parameter_pack (cp_declarator *declarator)
1585 /* Search for a declarator name, or any other declarator that goes
1586 after the point where the ellipsis could appear in a parameter
1587 pack. If we find any of these, then this declarator can not be
1588 made into a parameter pack. */
1589 bool found = false;
1590 while (declarator && !found)
1592 switch ((int)declarator->kind)
1594 case cdk_id:
1595 case cdk_array:
1596 found = true;
1597 break;
1599 case cdk_error:
1600 return true;
1602 default:
1603 declarator = declarator->declarator;
1604 break;
1608 return !found;
1611 cp_parameter_declarator *no_parameters;
1613 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1614 DECLARATOR and DEFAULT_ARGUMENT. */
1616 cp_parameter_declarator *
1617 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1618 cp_declarator *declarator,
1619 tree default_argument)
1621 cp_parameter_declarator *parameter;
1623 parameter = ((cp_parameter_declarator *)
1624 alloc_declarator (sizeof (cp_parameter_declarator)));
1625 parameter->next = NULL;
1626 if (decl_specifiers)
1627 parameter->decl_specifiers = *decl_specifiers;
1628 else
1629 clear_decl_specs (&parameter->decl_specifiers);
1630 parameter->declarator = declarator;
1631 parameter->default_argument = default_argument;
1632 parameter->ellipsis_p = false;
1634 return parameter;
1637 /* Returns true iff DECLARATOR is a declaration for a function. */
1639 static bool
1640 function_declarator_p (const cp_declarator *declarator)
1642 while (declarator)
1644 if (declarator->kind == cdk_function
1645 && declarator->declarator->kind == cdk_id)
1646 return true;
1647 if (declarator->kind == cdk_id
1648 || declarator->kind == cdk_error)
1649 return false;
1650 declarator = declarator->declarator;
1652 return false;
1655 /* The parser. */
1657 /* Overview
1658 --------
1660 A cp_parser parses the token stream as specified by the C++
1661 grammar. Its job is purely parsing, not semantic analysis. For
1662 example, the parser breaks the token stream into declarators,
1663 expressions, statements, and other similar syntactic constructs.
1664 It does not check that the types of the expressions on either side
1665 of an assignment-statement are compatible, or that a function is
1666 not declared with a parameter of type `void'.
1668 The parser invokes routines elsewhere in the compiler to perform
1669 semantic analysis and to build up the abstract syntax tree for the
1670 code processed.
1672 The parser (and the template instantiation code, which is, in a
1673 way, a close relative of parsing) are the only parts of the
1674 compiler that should be calling push_scope and pop_scope, or
1675 related functions. The parser (and template instantiation code)
1676 keeps track of what scope is presently active; everything else
1677 should simply honor that. (The code that generates static
1678 initializers may also need to set the scope, in order to check
1679 access control correctly when emitting the initializers.)
1681 Methodology
1682 -----------
1684 The parser is of the standard recursive-descent variety. Upcoming
1685 tokens in the token stream are examined in order to determine which
1686 production to use when parsing a non-terminal. Some C++ constructs
1687 require arbitrary look ahead to disambiguate. For example, it is
1688 impossible, in the general case, to tell whether a statement is an
1689 expression or declaration without scanning the entire statement.
1690 Therefore, the parser is capable of "parsing tentatively." When the
1691 parser is not sure what construct comes next, it enters this mode.
1692 Then, while we attempt to parse the construct, the parser queues up
1693 error messages, rather than issuing them immediately, and saves the
1694 tokens it consumes. If the construct is parsed successfully, the
1695 parser "commits", i.e., it issues any queued error messages and
1696 the tokens that were being preserved are permanently discarded.
1697 If, however, the construct is not parsed successfully, the parser
1698 rolls back its state completely so that it can resume parsing using
1699 a different alternative.
1701 Future Improvements
1702 -------------------
1704 The performance of the parser could probably be improved substantially.
1705 We could often eliminate the need to parse tentatively by looking ahead
1706 a little bit. In some places, this approach might not entirely eliminate
1707 the need to parse tentatively, but it might still speed up the average
1708 case. */
1710 /* Flags that are passed to some parsing functions. These values can
1711 be bitwise-ored together. */
1713 enum
1715 /* No flags. */
1716 CP_PARSER_FLAGS_NONE = 0x0,
1717 /* The construct is optional. If it is not present, then no error
1718 should be issued. */
1719 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1720 /* When parsing a type-specifier, treat user-defined type-names
1721 as non-type identifiers. */
1722 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1723 /* When parsing a type-specifier, do not try to parse a class-specifier
1724 or enum-specifier. */
1725 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1726 /* When parsing a decl-specifier-seq, only allow type-specifier or
1727 constexpr. */
1728 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1731 /* This type is used for parameters and variables which hold
1732 combinations of the above flags. */
1733 typedef int cp_parser_flags;
1735 /* The different kinds of declarators we want to parse. */
1737 typedef enum cp_parser_declarator_kind
1739 /* We want an abstract declarator. */
1740 CP_PARSER_DECLARATOR_ABSTRACT,
1741 /* We want a named declarator. */
1742 CP_PARSER_DECLARATOR_NAMED,
1743 /* We don't mind, but the name must be an unqualified-id. */
1744 CP_PARSER_DECLARATOR_EITHER
1745 } cp_parser_declarator_kind;
1747 /* The precedence values used to parse binary expressions. The minimum value
1748 of PREC must be 1, because zero is reserved to quickly discriminate
1749 binary operators from other tokens. */
1751 enum cp_parser_prec
1753 PREC_NOT_OPERATOR,
1754 PREC_LOGICAL_OR_EXPRESSION,
1755 PREC_LOGICAL_AND_EXPRESSION,
1756 PREC_INCLUSIVE_OR_EXPRESSION,
1757 PREC_EXCLUSIVE_OR_EXPRESSION,
1758 PREC_AND_EXPRESSION,
1759 PREC_EQUALITY_EXPRESSION,
1760 PREC_RELATIONAL_EXPRESSION,
1761 PREC_SHIFT_EXPRESSION,
1762 PREC_ADDITIVE_EXPRESSION,
1763 PREC_MULTIPLICATIVE_EXPRESSION,
1764 PREC_PM_EXPRESSION,
1765 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1768 /* A mapping from a token type to a corresponding tree node type, with a
1769 precedence value. */
1771 typedef struct cp_parser_binary_operations_map_node
1773 /* The token type. */
1774 enum cpp_ttype token_type;
1775 /* The corresponding tree code. */
1776 enum tree_code tree_type;
1777 /* The precedence of this operator. */
1778 enum cp_parser_prec prec;
1779 } cp_parser_binary_operations_map_node;
1781 typedef struct cp_parser_expression_stack_entry
1783 /* Left hand side of the binary operation we are currently
1784 parsing. */
1785 tree lhs;
1786 /* Original tree code for left hand side, if it was a binary
1787 expression itself (used for -Wparentheses). */
1788 enum tree_code lhs_type;
1789 /* Tree code for the binary operation we are parsing. */
1790 enum tree_code tree_type;
1791 /* Precedence of the binary operation we are parsing. */
1792 enum cp_parser_prec prec;
1793 /* Location of the binary operation we are parsing. */
1794 location_t loc;
1795 } cp_parser_expression_stack_entry;
1797 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1798 entries because precedence levels on the stack are monotonically
1799 increasing. */
1800 typedef struct cp_parser_expression_stack_entry
1801 cp_parser_expression_stack[NUM_PREC_VALUES];
1803 /* Prototypes. */
1805 /* Constructors and destructors. */
1807 static cp_parser_context *cp_parser_context_new
1808 (cp_parser_context *);
1810 /* Class variables. */
1812 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1814 /* The operator-precedence table used by cp_parser_binary_expression.
1815 Transformed into an associative array (binops_by_token) by
1816 cp_parser_new. */
1818 static const cp_parser_binary_operations_map_node binops[] = {
1819 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1820 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1822 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1823 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1824 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1826 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1827 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1829 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1830 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1832 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1833 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1834 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1835 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1837 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1838 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1840 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1842 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1844 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1846 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1848 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1851 /* The same as binops, but initialized by cp_parser_new so that
1852 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1853 for speed. */
1854 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1856 /* Constructors and destructors. */
1858 /* Construct a new context. The context below this one on the stack
1859 is given by NEXT. */
1861 static cp_parser_context *
1862 cp_parser_context_new (cp_parser_context* next)
1864 cp_parser_context *context;
1866 /* Allocate the storage. */
1867 if (cp_parser_context_free_list != NULL)
1869 /* Pull the first entry from the free list. */
1870 context = cp_parser_context_free_list;
1871 cp_parser_context_free_list = context->next;
1872 memset (context, 0, sizeof (*context));
1874 else
1875 context = ggc_cleared_alloc<cp_parser_context> ();
1877 /* No errors have occurred yet in this context. */
1878 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1879 /* If this is not the bottommost context, copy information that we
1880 need from the previous context. */
1881 if (next)
1883 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1884 expression, then we are parsing one in this context, too. */
1885 context->object_type = next->object_type;
1886 /* Thread the stack. */
1887 context->next = next;
1890 return context;
1893 /* Managing the unparsed function queues. */
1895 #define unparsed_funs_with_default_args \
1896 parser->unparsed_queues->last ().funs_with_default_args
1897 #define unparsed_funs_with_definitions \
1898 parser->unparsed_queues->last ().funs_with_definitions
1899 #define unparsed_nsdmis \
1900 parser->unparsed_queues->last ().nsdmis
1901 #define unparsed_classes \
1902 parser->unparsed_queues->last ().classes
1904 static void
1905 push_unparsed_function_queues (cp_parser *parser)
1907 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1908 vec_safe_push (parser->unparsed_queues, e);
1911 static void
1912 pop_unparsed_function_queues (cp_parser *parser)
1914 release_tree_vector (unparsed_funs_with_definitions);
1915 parser->unparsed_queues->pop ();
1918 /* Prototypes. */
1920 /* Constructors and destructors. */
1922 static cp_parser *cp_parser_new
1923 (void);
1925 /* Routines to parse various constructs.
1927 Those that return `tree' will return the error_mark_node (rather
1928 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1929 Sometimes, they will return an ordinary node if error-recovery was
1930 attempted, even though a parse error occurred. So, to check
1931 whether or not a parse error occurred, you should always use
1932 cp_parser_error_occurred. If the construct is optional (indicated
1933 either by an `_opt' in the name of the function that does the
1934 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1935 the construct is not present. */
1937 /* Lexical conventions [gram.lex] */
1939 static tree cp_parser_identifier
1940 (cp_parser *);
1941 static tree cp_parser_string_literal
1942 (cp_parser *, bool, bool, bool);
1943 static tree cp_parser_userdef_char_literal
1944 (cp_parser *);
1945 static tree cp_parser_userdef_string_literal
1946 (tree);
1947 static tree cp_parser_userdef_numeric_literal
1948 (cp_parser *);
1950 /* Basic concepts [gram.basic] */
1952 static bool cp_parser_translation_unit
1953 (cp_parser *);
1955 /* Expressions [gram.expr] */
1957 static tree cp_parser_primary_expression
1958 (cp_parser *, bool, bool, bool, cp_id_kind *);
1959 static tree cp_parser_id_expression
1960 (cp_parser *, bool, bool, bool *, bool, bool);
1961 static tree cp_parser_unqualified_id
1962 (cp_parser *, bool, bool, bool, bool);
1963 static tree cp_parser_nested_name_specifier_opt
1964 (cp_parser *, bool, bool, bool, bool);
1965 static tree cp_parser_nested_name_specifier
1966 (cp_parser *, bool, bool, bool, bool);
1967 static tree cp_parser_qualifying_entity
1968 (cp_parser *, bool, bool, bool, bool, bool);
1969 static tree cp_parser_postfix_expression
1970 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1971 static tree cp_parser_postfix_open_square_expression
1972 (cp_parser *, tree, bool, bool);
1973 static tree cp_parser_postfix_dot_deref_expression
1974 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1975 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1976 (cp_parser *, int, bool, bool, bool *, bool = false);
1977 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1978 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1979 static void cp_parser_pseudo_destructor_name
1980 (cp_parser *, tree, tree *, tree *);
1981 static tree cp_parser_unary_expression
1982 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1983 static enum tree_code cp_parser_unary_operator
1984 (cp_token *);
1985 static tree cp_parser_new_expression
1986 (cp_parser *);
1987 static vec<tree, va_gc> *cp_parser_new_placement
1988 (cp_parser *);
1989 static tree cp_parser_new_type_id
1990 (cp_parser *, tree *);
1991 static cp_declarator *cp_parser_new_declarator_opt
1992 (cp_parser *);
1993 static cp_declarator *cp_parser_direct_new_declarator
1994 (cp_parser *);
1995 static vec<tree, va_gc> *cp_parser_new_initializer
1996 (cp_parser *);
1997 static tree cp_parser_delete_expression
1998 (cp_parser *);
1999 static tree cp_parser_cast_expression
2000 (cp_parser *, bool, bool, bool, cp_id_kind *);
2001 static tree cp_parser_binary_expression
2002 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2003 static tree cp_parser_question_colon_clause
2004 (cp_parser *, tree);
2005 static tree cp_parser_assignment_expression
2006 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2007 static enum tree_code cp_parser_assignment_operator_opt
2008 (cp_parser *);
2009 static tree cp_parser_expression
2010 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2011 static tree cp_parser_constant_expression
2012 (cp_parser *, bool = false, bool * = NULL);
2013 static tree cp_parser_builtin_offsetof
2014 (cp_parser *);
2015 static tree cp_parser_lambda_expression
2016 (cp_parser *);
2017 static void cp_parser_lambda_introducer
2018 (cp_parser *, tree);
2019 static bool cp_parser_lambda_declarator_opt
2020 (cp_parser *, tree);
2021 static void cp_parser_lambda_body
2022 (cp_parser *, tree);
2024 /* Statements [gram.stmt.stmt] */
2026 static void cp_parser_statement
2027 (cp_parser *, tree, bool, bool *);
2028 static void cp_parser_label_for_labeled_statement
2029 (cp_parser *, tree);
2030 static tree cp_parser_expression_statement
2031 (cp_parser *, tree);
2032 static tree cp_parser_compound_statement
2033 (cp_parser *, tree, bool, bool);
2034 static void cp_parser_statement_seq_opt
2035 (cp_parser *, tree);
2036 static tree cp_parser_selection_statement
2037 (cp_parser *, bool *);
2038 static tree cp_parser_condition
2039 (cp_parser *);
2040 static tree cp_parser_iteration_statement
2041 (cp_parser *, bool);
2042 static bool cp_parser_for_init_statement
2043 (cp_parser *, tree *decl);
2044 static tree cp_parser_for
2045 (cp_parser *, bool);
2046 static tree cp_parser_c_for
2047 (cp_parser *, tree, tree, bool);
2048 static tree cp_parser_range_for
2049 (cp_parser *, tree, tree, tree, bool);
2050 static void do_range_for_auto_deduction
2051 (tree, tree);
2052 static tree cp_parser_perform_range_for_lookup
2053 (tree, tree *, tree *);
2054 static tree cp_parser_range_for_member_function
2055 (tree, tree);
2056 static tree cp_parser_jump_statement
2057 (cp_parser *);
2058 static void cp_parser_declaration_statement
2059 (cp_parser *);
2061 static tree cp_parser_implicitly_scoped_statement
2062 (cp_parser *, bool *);
2063 static void cp_parser_already_scoped_statement
2064 (cp_parser *);
2066 /* Declarations [gram.dcl.dcl] */
2068 static void cp_parser_declaration_seq_opt
2069 (cp_parser *);
2070 static void cp_parser_declaration
2071 (cp_parser *);
2072 static void cp_parser_block_declaration
2073 (cp_parser *, bool);
2074 static void cp_parser_simple_declaration
2075 (cp_parser *, bool, tree *);
2076 static void cp_parser_decl_specifier_seq
2077 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2078 static tree cp_parser_storage_class_specifier_opt
2079 (cp_parser *);
2080 static tree cp_parser_function_specifier_opt
2081 (cp_parser *, cp_decl_specifier_seq *);
2082 static tree cp_parser_type_specifier
2083 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2084 int *, bool *);
2085 static tree cp_parser_simple_type_specifier
2086 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2087 static tree cp_parser_type_name
2088 (cp_parser *);
2089 static tree cp_parser_nonclass_name
2090 (cp_parser* parser);
2091 static tree cp_parser_elaborated_type_specifier
2092 (cp_parser *, bool, bool);
2093 static tree cp_parser_enum_specifier
2094 (cp_parser *);
2095 static void cp_parser_enumerator_list
2096 (cp_parser *, tree);
2097 static void cp_parser_enumerator_definition
2098 (cp_parser *, tree);
2099 static tree cp_parser_namespace_name
2100 (cp_parser *);
2101 static void cp_parser_namespace_definition
2102 (cp_parser *);
2103 static void cp_parser_namespace_body
2104 (cp_parser *);
2105 static tree cp_parser_qualified_namespace_specifier
2106 (cp_parser *);
2107 static void cp_parser_namespace_alias_definition
2108 (cp_parser *);
2109 static bool cp_parser_using_declaration
2110 (cp_parser *, bool);
2111 static void cp_parser_using_directive
2112 (cp_parser *);
2113 static tree cp_parser_alias_declaration
2114 (cp_parser *);
2115 static void cp_parser_asm_definition
2116 (cp_parser *);
2117 static void cp_parser_linkage_specification
2118 (cp_parser *);
2119 static void cp_parser_static_assert
2120 (cp_parser *, bool);
2121 static tree cp_parser_decltype
2122 (cp_parser *);
2124 /* Declarators [gram.dcl.decl] */
2126 static tree cp_parser_init_declarator
2127 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2128 static cp_declarator *cp_parser_declarator
2129 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2130 static cp_declarator *cp_parser_direct_declarator
2131 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2132 static enum tree_code cp_parser_ptr_operator
2133 (cp_parser *, tree *, cp_cv_quals *, tree *);
2134 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2135 (cp_parser *);
2136 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2137 (cp_parser *);
2138 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2139 (cp_parser *);
2140 static tree cp_parser_late_return_type_opt
2141 (cp_parser *, cp_declarator *, cp_cv_quals);
2142 static tree cp_parser_declarator_id
2143 (cp_parser *, bool);
2144 static tree cp_parser_type_id
2145 (cp_parser *);
2146 static tree cp_parser_template_type_arg
2147 (cp_parser *);
2148 static tree cp_parser_trailing_type_id (cp_parser *);
2149 static tree cp_parser_type_id_1
2150 (cp_parser *, bool, bool);
2151 static void cp_parser_type_specifier_seq
2152 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2153 static tree cp_parser_parameter_declaration_clause
2154 (cp_parser *);
2155 static tree cp_parser_parameter_declaration_list
2156 (cp_parser *, bool *);
2157 static cp_parameter_declarator *cp_parser_parameter_declaration
2158 (cp_parser *, bool, bool *);
2159 static tree cp_parser_default_argument
2160 (cp_parser *, bool);
2161 static void cp_parser_function_body
2162 (cp_parser *, bool);
2163 static tree cp_parser_initializer
2164 (cp_parser *, bool *, bool *);
2165 static tree cp_parser_initializer_clause
2166 (cp_parser *, bool *);
2167 static tree cp_parser_braced_list
2168 (cp_parser*, bool*);
2169 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2170 (cp_parser *, bool *);
2172 static bool cp_parser_ctor_initializer_opt_and_function_body
2173 (cp_parser *, bool);
2175 static tree cp_parser_late_parsing_omp_declare_simd
2176 (cp_parser *, tree);
2178 static tree cp_parser_late_parsing_cilk_simd_fn_info
2179 (cp_parser *, tree);
2181 static tree synthesize_implicit_template_parm
2182 (cp_parser *);
2183 static tree finish_fully_implicit_template
2184 (cp_parser *, tree);
2186 /* Classes [gram.class] */
2188 static tree cp_parser_class_name
2189 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2190 static tree cp_parser_class_specifier
2191 (cp_parser *);
2192 static tree cp_parser_class_head
2193 (cp_parser *, bool *);
2194 static enum tag_types cp_parser_class_key
2195 (cp_parser *);
2196 static void cp_parser_type_parameter_key
2197 (cp_parser* parser);
2198 static void cp_parser_member_specification_opt
2199 (cp_parser *);
2200 static void cp_parser_member_declaration
2201 (cp_parser *);
2202 static tree cp_parser_pure_specifier
2203 (cp_parser *);
2204 static tree cp_parser_constant_initializer
2205 (cp_parser *);
2207 /* Derived classes [gram.class.derived] */
2209 static tree cp_parser_base_clause
2210 (cp_parser *);
2211 static tree cp_parser_base_specifier
2212 (cp_parser *);
2214 /* Special member functions [gram.special] */
2216 static tree cp_parser_conversion_function_id
2217 (cp_parser *);
2218 static tree cp_parser_conversion_type_id
2219 (cp_parser *);
2220 static cp_declarator *cp_parser_conversion_declarator_opt
2221 (cp_parser *);
2222 static bool cp_parser_ctor_initializer_opt
2223 (cp_parser *);
2224 static void cp_parser_mem_initializer_list
2225 (cp_parser *);
2226 static tree cp_parser_mem_initializer
2227 (cp_parser *);
2228 static tree cp_parser_mem_initializer_id
2229 (cp_parser *);
2231 /* Overloading [gram.over] */
2233 static tree cp_parser_operator_function_id
2234 (cp_parser *);
2235 static tree cp_parser_operator
2236 (cp_parser *);
2238 /* Templates [gram.temp] */
2240 static void cp_parser_template_declaration
2241 (cp_parser *, bool);
2242 static tree cp_parser_template_parameter_list
2243 (cp_parser *);
2244 static tree cp_parser_template_parameter
2245 (cp_parser *, bool *, bool *);
2246 static tree cp_parser_type_parameter
2247 (cp_parser *, bool *);
2248 static tree cp_parser_template_id
2249 (cp_parser *, bool, bool, enum tag_types, bool);
2250 static tree cp_parser_template_name
2251 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2252 static tree cp_parser_template_argument_list
2253 (cp_parser *);
2254 static tree cp_parser_template_argument
2255 (cp_parser *);
2256 static void cp_parser_explicit_instantiation
2257 (cp_parser *);
2258 static void cp_parser_explicit_specialization
2259 (cp_parser *);
2261 /* Exception handling [gram.exception] */
2263 static tree cp_parser_try_block
2264 (cp_parser *);
2265 static bool cp_parser_function_try_block
2266 (cp_parser *);
2267 static void cp_parser_handler_seq
2268 (cp_parser *);
2269 static void cp_parser_handler
2270 (cp_parser *);
2271 static tree cp_parser_exception_declaration
2272 (cp_parser *);
2273 static tree cp_parser_throw_expression
2274 (cp_parser *);
2275 static tree cp_parser_exception_specification_opt
2276 (cp_parser *);
2277 static tree cp_parser_type_id_list
2278 (cp_parser *);
2280 /* GNU Extensions */
2282 static tree cp_parser_asm_specification_opt
2283 (cp_parser *);
2284 static tree cp_parser_asm_operand_list
2285 (cp_parser *);
2286 static tree cp_parser_asm_clobber_list
2287 (cp_parser *);
2288 static tree cp_parser_asm_label_list
2289 (cp_parser *);
2290 static bool cp_next_tokens_can_be_attribute_p
2291 (cp_parser *);
2292 static bool cp_next_tokens_can_be_gnu_attribute_p
2293 (cp_parser *);
2294 static bool cp_next_tokens_can_be_std_attribute_p
2295 (cp_parser *);
2296 static bool cp_nth_tokens_can_be_std_attribute_p
2297 (cp_parser *, size_t);
2298 static bool cp_nth_tokens_can_be_gnu_attribute_p
2299 (cp_parser *, size_t);
2300 static bool cp_nth_tokens_can_be_attribute_p
2301 (cp_parser *, size_t);
2302 static tree cp_parser_attributes_opt
2303 (cp_parser *);
2304 static tree cp_parser_gnu_attributes_opt
2305 (cp_parser *);
2306 static tree cp_parser_gnu_attribute_list
2307 (cp_parser *);
2308 static tree cp_parser_std_attribute
2309 (cp_parser *);
2310 static tree cp_parser_std_attribute_spec
2311 (cp_parser *);
2312 static tree cp_parser_std_attribute_spec_seq
2313 (cp_parser *);
2314 static bool cp_parser_extension_opt
2315 (cp_parser *, int *);
2316 static void cp_parser_label_declaration
2317 (cp_parser *);
2319 /* Transactional Memory Extensions */
2321 static tree cp_parser_transaction
2322 (cp_parser *, enum rid);
2323 static tree cp_parser_transaction_expression
2324 (cp_parser *, enum rid);
2325 static bool cp_parser_function_transaction
2326 (cp_parser *, enum rid);
2327 static tree cp_parser_transaction_cancel
2328 (cp_parser *);
2330 enum pragma_context {
2331 pragma_external,
2332 pragma_member,
2333 pragma_objc_icode,
2334 pragma_stmt,
2335 pragma_compound
2337 static bool cp_parser_pragma
2338 (cp_parser *, enum pragma_context);
2340 /* Objective-C++ Productions */
2342 static tree cp_parser_objc_message_receiver
2343 (cp_parser *);
2344 static tree cp_parser_objc_message_args
2345 (cp_parser *);
2346 static tree cp_parser_objc_message_expression
2347 (cp_parser *);
2348 static tree cp_parser_objc_encode_expression
2349 (cp_parser *);
2350 static tree cp_parser_objc_defs_expression
2351 (cp_parser *);
2352 static tree cp_parser_objc_protocol_expression
2353 (cp_parser *);
2354 static tree cp_parser_objc_selector_expression
2355 (cp_parser *);
2356 static tree cp_parser_objc_expression
2357 (cp_parser *);
2358 static bool cp_parser_objc_selector_p
2359 (enum cpp_ttype);
2360 static tree cp_parser_objc_selector
2361 (cp_parser *);
2362 static tree cp_parser_objc_protocol_refs_opt
2363 (cp_parser *);
2364 static void cp_parser_objc_declaration
2365 (cp_parser *, tree);
2366 static tree cp_parser_objc_statement
2367 (cp_parser *);
2368 static bool cp_parser_objc_valid_prefix_attributes
2369 (cp_parser *, tree *);
2370 static void cp_parser_objc_at_property_declaration
2371 (cp_parser *) ;
2372 static void cp_parser_objc_at_synthesize_declaration
2373 (cp_parser *) ;
2374 static void cp_parser_objc_at_dynamic_declaration
2375 (cp_parser *) ;
2376 static tree cp_parser_objc_struct_declaration
2377 (cp_parser *) ;
2379 /* Utility Routines */
2381 static tree cp_parser_lookup_name
2382 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2383 static tree cp_parser_lookup_name_simple
2384 (cp_parser *, tree, location_t);
2385 static tree cp_parser_maybe_treat_template_as_class
2386 (tree, bool);
2387 static bool cp_parser_check_declarator_template_parameters
2388 (cp_parser *, cp_declarator *, location_t);
2389 static bool cp_parser_check_template_parameters
2390 (cp_parser *, unsigned, location_t, cp_declarator *);
2391 static tree cp_parser_simple_cast_expression
2392 (cp_parser *);
2393 static tree cp_parser_global_scope_opt
2394 (cp_parser *, bool);
2395 static bool cp_parser_constructor_declarator_p
2396 (cp_parser *, bool);
2397 static tree cp_parser_function_definition_from_specifiers_and_declarator
2398 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2399 static tree cp_parser_function_definition_after_declarator
2400 (cp_parser *, bool);
2401 static void cp_parser_template_declaration_after_export
2402 (cp_parser *, bool);
2403 static void cp_parser_perform_template_parameter_access_checks
2404 (vec<deferred_access_check, va_gc> *);
2405 static tree cp_parser_single_declaration
2406 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2407 static tree cp_parser_functional_cast
2408 (cp_parser *, tree);
2409 static tree cp_parser_save_member_function_body
2410 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2411 static tree cp_parser_save_nsdmi
2412 (cp_parser *);
2413 static tree cp_parser_enclosed_template_argument_list
2414 (cp_parser *);
2415 static void cp_parser_save_default_args
2416 (cp_parser *, tree);
2417 static void cp_parser_late_parsing_for_member
2418 (cp_parser *, tree);
2419 static tree cp_parser_late_parse_one_default_arg
2420 (cp_parser *, tree, tree, tree);
2421 static void cp_parser_late_parsing_nsdmi
2422 (cp_parser *, tree);
2423 static void cp_parser_late_parsing_default_args
2424 (cp_parser *, tree);
2425 static tree cp_parser_sizeof_operand
2426 (cp_parser *, enum rid);
2427 static tree cp_parser_trait_expr
2428 (cp_parser *, enum rid);
2429 static bool cp_parser_declares_only_class_p
2430 (cp_parser *);
2431 static void cp_parser_set_storage_class
2432 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2433 static void cp_parser_set_decl_spec_type
2434 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2435 static void set_and_check_decl_spec_loc
2436 (cp_decl_specifier_seq *decl_specs,
2437 cp_decl_spec ds, cp_token *);
2438 static bool cp_parser_friend_p
2439 (const cp_decl_specifier_seq *);
2440 static void cp_parser_required_error
2441 (cp_parser *, required_token, bool);
2442 static cp_token *cp_parser_require
2443 (cp_parser *, enum cpp_ttype, required_token);
2444 static cp_token *cp_parser_require_keyword
2445 (cp_parser *, enum rid, required_token);
2446 static bool cp_parser_token_starts_function_definition_p
2447 (cp_token *);
2448 static bool cp_parser_next_token_starts_class_definition_p
2449 (cp_parser *);
2450 static bool cp_parser_next_token_ends_template_argument_p
2451 (cp_parser *);
2452 static bool cp_parser_nth_token_starts_template_argument_list_p
2453 (cp_parser *, size_t);
2454 static enum tag_types cp_parser_token_is_class_key
2455 (cp_token *);
2456 static enum tag_types cp_parser_token_is_type_parameter_key
2457 (cp_token *);
2458 static void cp_parser_check_class_key
2459 (enum tag_types, tree type);
2460 static void cp_parser_check_access_in_redeclaration
2461 (tree type, location_t location);
2462 static bool cp_parser_optional_template_keyword
2463 (cp_parser *);
2464 static void cp_parser_pre_parsed_nested_name_specifier
2465 (cp_parser *);
2466 static bool cp_parser_cache_group
2467 (cp_parser *, enum cpp_ttype, unsigned);
2468 static tree cp_parser_cache_defarg
2469 (cp_parser *parser, bool nsdmi);
2470 static void cp_parser_parse_tentatively
2471 (cp_parser *);
2472 static void cp_parser_commit_to_tentative_parse
2473 (cp_parser *);
2474 static void cp_parser_commit_to_topmost_tentative_parse
2475 (cp_parser *);
2476 static void cp_parser_abort_tentative_parse
2477 (cp_parser *);
2478 static bool cp_parser_parse_definitely
2479 (cp_parser *);
2480 static inline bool cp_parser_parsing_tentatively
2481 (cp_parser *);
2482 static bool cp_parser_uncommitted_to_tentative_parse_p
2483 (cp_parser *);
2484 static void cp_parser_error
2485 (cp_parser *, const char *);
2486 static void cp_parser_name_lookup_error
2487 (cp_parser *, tree, tree, name_lookup_error, location_t);
2488 static bool cp_parser_simulate_error
2489 (cp_parser *);
2490 static bool cp_parser_check_type_definition
2491 (cp_parser *);
2492 static void cp_parser_check_for_definition_in_return_type
2493 (cp_declarator *, tree, location_t type_location);
2494 static void cp_parser_check_for_invalid_template_id
2495 (cp_parser *, tree, enum tag_types, location_t location);
2496 static bool cp_parser_non_integral_constant_expression
2497 (cp_parser *, non_integral_constant);
2498 static void cp_parser_diagnose_invalid_type_name
2499 (cp_parser *, tree, location_t);
2500 static bool cp_parser_parse_and_diagnose_invalid_type_name
2501 (cp_parser *);
2502 static int cp_parser_skip_to_closing_parenthesis
2503 (cp_parser *, bool, bool, bool);
2504 static void cp_parser_skip_to_end_of_statement
2505 (cp_parser *);
2506 static void cp_parser_consume_semicolon_at_end_of_statement
2507 (cp_parser *);
2508 static void cp_parser_skip_to_end_of_block_or_statement
2509 (cp_parser *);
2510 static bool cp_parser_skip_to_closing_brace
2511 (cp_parser *);
2512 static void cp_parser_skip_to_end_of_template_parameter_list
2513 (cp_parser *);
2514 static void cp_parser_skip_to_pragma_eol
2515 (cp_parser*, cp_token *);
2516 static bool cp_parser_error_occurred
2517 (cp_parser *);
2518 static bool cp_parser_allow_gnu_extensions_p
2519 (cp_parser *);
2520 static bool cp_parser_is_pure_string_literal
2521 (cp_token *);
2522 static bool cp_parser_is_string_literal
2523 (cp_token *);
2524 static bool cp_parser_is_keyword
2525 (cp_token *, enum rid);
2526 static tree cp_parser_make_typename_type
2527 (cp_parser *, tree, location_t location);
2528 static cp_declarator * cp_parser_make_indirect_declarator
2529 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2530 static bool cp_parser_compound_literal_p
2531 (cp_parser *);
2532 static bool cp_parser_array_designator_p
2533 (cp_parser *);
2534 static bool cp_parser_skip_to_closing_square_bracket
2535 (cp_parser *);
2537 /* Returns nonzero if we are parsing tentatively. */
2539 static inline bool
2540 cp_parser_parsing_tentatively (cp_parser* parser)
2542 return parser->context->next != NULL;
2545 /* Returns nonzero if TOKEN is a string literal. */
2547 static bool
2548 cp_parser_is_pure_string_literal (cp_token* token)
2550 return (token->type == CPP_STRING ||
2551 token->type == CPP_STRING16 ||
2552 token->type == CPP_STRING32 ||
2553 token->type == CPP_WSTRING ||
2554 token->type == CPP_UTF8STRING);
2557 /* Returns nonzero if TOKEN is a string literal
2558 of a user-defined string literal. */
2560 static bool
2561 cp_parser_is_string_literal (cp_token* token)
2563 return (cp_parser_is_pure_string_literal (token) ||
2564 token->type == CPP_STRING_USERDEF ||
2565 token->type == CPP_STRING16_USERDEF ||
2566 token->type == CPP_STRING32_USERDEF ||
2567 token->type == CPP_WSTRING_USERDEF ||
2568 token->type == CPP_UTF8STRING_USERDEF);
2571 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2573 static bool
2574 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2576 return token->keyword == keyword;
2579 /* If not parsing tentatively, issue a diagnostic of the form
2580 FILE:LINE: MESSAGE before TOKEN
2581 where TOKEN is the next token in the input stream. MESSAGE
2582 (specified by the caller) is usually of the form "expected
2583 OTHER-TOKEN". */
2585 static void
2586 cp_parser_error (cp_parser* parser, const char* gmsgid)
2588 if (!cp_parser_simulate_error (parser))
2590 cp_token *token = cp_lexer_peek_token (parser->lexer);
2591 /* This diagnostic makes more sense if it is tagged to the line
2592 of the token we just peeked at. */
2593 cp_lexer_set_source_position_from_token (token);
2595 if (token->type == CPP_PRAGMA)
2597 error_at (token->location,
2598 "%<#pragma%> is not allowed here");
2599 cp_parser_skip_to_pragma_eol (parser, token);
2600 return;
2603 c_parse_error (gmsgid,
2604 /* Because c_parser_error does not understand
2605 CPP_KEYWORD, keywords are treated like
2606 identifiers. */
2607 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2608 token->u.value, token->flags);
2612 /* Issue an error about name-lookup failing. NAME is the
2613 IDENTIFIER_NODE DECL is the result of
2614 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2615 the thing that we hoped to find. */
2617 static void
2618 cp_parser_name_lookup_error (cp_parser* parser,
2619 tree name,
2620 tree decl,
2621 name_lookup_error desired,
2622 location_t location)
2624 /* If name lookup completely failed, tell the user that NAME was not
2625 declared. */
2626 if (decl == error_mark_node)
2628 if (parser->scope && parser->scope != global_namespace)
2629 error_at (location, "%<%E::%E%> has not been declared",
2630 parser->scope, name);
2631 else if (parser->scope == global_namespace)
2632 error_at (location, "%<::%E%> has not been declared", name);
2633 else if (parser->object_scope
2634 && !CLASS_TYPE_P (parser->object_scope))
2635 error_at (location, "request for member %qE in non-class type %qT",
2636 name, parser->object_scope);
2637 else if (parser->object_scope)
2638 error_at (location, "%<%T::%E%> has not been declared",
2639 parser->object_scope, name);
2640 else
2641 error_at (location, "%qE has not been declared", name);
2643 else if (parser->scope && parser->scope != global_namespace)
2645 switch (desired)
2647 case NLE_TYPE:
2648 error_at (location, "%<%E::%E%> is not a type",
2649 parser->scope, name);
2650 break;
2651 case NLE_CXX98:
2652 error_at (location, "%<%E::%E%> is not a class or namespace",
2653 parser->scope, name);
2654 break;
2655 case NLE_NOT_CXX98:
2656 error_at (location,
2657 "%<%E::%E%> is not a class, namespace, or enumeration",
2658 parser->scope, name);
2659 break;
2660 default:
2661 gcc_unreachable ();
2665 else if (parser->scope == global_namespace)
2667 switch (desired)
2669 case NLE_TYPE:
2670 error_at (location, "%<::%E%> is not a type", name);
2671 break;
2672 case NLE_CXX98:
2673 error_at (location, "%<::%E%> is not a class or namespace", name);
2674 break;
2675 case NLE_NOT_CXX98:
2676 error_at (location,
2677 "%<::%E%> is not a class, namespace, or enumeration",
2678 name);
2679 break;
2680 default:
2681 gcc_unreachable ();
2684 else
2686 switch (desired)
2688 case NLE_TYPE:
2689 error_at (location, "%qE is not a type", name);
2690 break;
2691 case NLE_CXX98:
2692 error_at (location, "%qE is not a class or namespace", name);
2693 break;
2694 case NLE_NOT_CXX98:
2695 error_at (location,
2696 "%qE is not a class, namespace, or enumeration", name);
2697 break;
2698 default:
2699 gcc_unreachable ();
2704 /* If we are parsing tentatively, remember that an error has occurred
2705 during this tentative parse. Returns true if the error was
2706 simulated; false if a message should be issued by the caller. */
2708 static bool
2709 cp_parser_simulate_error (cp_parser* parser)
2711 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2713 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2714 return true;
2716 return false;
2719 /* This function is called when a type is defined. If type
2720 definitions are forbidden at this point, an error message is
2721 issued. */
2723 static bool
2724 cp_parser_check_type_definition (cp_parser* parser)
2726 /* If types are forbidden here, issue a message. */
2727 if (parser->type_definition_forbidden_message)
2729 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2730 in the message need to be interpreted. */
2731 error (parser->type_definition_forbidden_message);
2732 return false;
2734 return true;
2737 /* This function is called when the DECLARATOR is processed. The TYPE
2738 was a type defined in the decl-specifiers. If it is invalid to
2739 define a type in the decl-specifiers for DECLARATOR, an error is
2740 issued. TYPE_LOCATION is the location of TYPE and is used
2741 for error reporting. */
2743 static void
2744 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2745 tree type, location_t type_location)
2747 /* [dcl.fct] forbids type definitions in return types.
2748 Unfortunately, it's not easy to know whether or not we are
2749 processing a return type until after the fact. */
2750 while (declarator
2751 && (declarator->kind == cdk_pointer
2752 || declarator->kind == cdk_reference
2753 || declarator->kind == cdk_ptrmem))
2754 declarator = declarator->declarator;
2755 if (declarator
2756 && declarator->kind == cdk_function)
2758 error_at (type_location,
2759 "new types may not be defined in a return type");
2760 inform (type_location,
2761 "(perhaps a semicolon is missing after the definition of %qT)",
2762 type);
2766 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2767 "<" in any valid C++ program. If the next token is indeed "<",
2768 issue a message warning the user about what appears to be an
2769 invalid attempt to form a template-id. LOCATION is the location
2770 of the type-specifier (TYPE) */
2772 static void
2773 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2774 tree type,
2775 enum tag_types tag_type,
2776 location_t location)
2778 cp_token_position start = 0;
2780 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2782 if (TYPE_P (type))
2783 error_at (location, "%qT is not a template", type);
2784 else if (identifier_p (type))
2786 if (tag_type != none_type)
2787 error_at (location, "%qE is not a class template", type);
2788 else
2789 error_at (location, "%qE is not a template", type);
2791 else
2792 error_at (location, "invalid template-id");
2793 /* Remember the location of the invalid "<". */
2794 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2795 start = cp_lexer_token_position (parser->lexer, true);
2796 /* Consume the "<". */
2797 cp_lexer_consume_token (parser->lexer);
2798 /* Parse the template arguments. */
2799 cp_parser_enclosed_template_argument_list (parser);
2800 /* Permanently remove the invalid template arguments so that
2801 this error message is not issued again. */
2802 if (start)
2803 cp_lexer_purge_tokens_after (parser->lexer, start);
2807 /* If parsing an integral constant-expression, issue an error message
2808 about the fact that THING appeared and return true. Otherwise,
2809 return false. In either case, set
2810 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2812 static bool
2813 cp_parser_non_integral_constant_expression (cp_parser *parser,
2814 non_integral_constant thing)
2816 parser->non_integral_constant_expression_p = true;
2817 if (parser->integral_constant_expression_p)
2819 if (!parser->allow_non_integral_constant_expression_p)
2821 const char *msg = NULL;
2822 switch (thing)
2824 case NIC_FLOAT:
2825 error ("floating-point literal "
2826 "cannot appear in a constant-expression");
2827 return true;
2828 case NIC_CAST:
2829 error ("a cast to a type other than an integral or "
2830 "enumeration type cannot appear in a "
2831 "constant-expression");
2832 return true;
2833 case NIC_TYPEID:
2834 error ("%<typeid%> operator "
2835 "cannot appear in a constant-expression");
2836 return true;
2837 case NIC_NCC:
2838 error ("non-constant compound literals "
2839 "cannot appear in a constant-expression");
2840 return true;
2841 case NIC_FUNC_CALL:
2842 error ("a function call "
2843 "cannot appear in a constant-expression");
2844 return true;
2845 case NIC_INC:
2846 error ("an increment "
2847 "cannot appear in a constant-expression");
2848 return true;
2849 case NIC_DEC:
2850 error ("an decrement "
2851 "cannot appear in a constant-expression");
2852 return true;
2853 case NIC_ARRAY_REF:
2854 error ("an array reference "
2855 "cannot appear in a constant-expression");
2856 return true;
2857 case NIC_ADDR_LABEL:
2858 error ("the address of a label "
2859 "cannot appear in a constant-expression");
2860 return true;
2861 case NIC_OVERLOADED:
2862 error ("calls to overloaded operators "
2863 "cannot appear in a constant-expression");
2864 return true;
2865 case NIC_ASSIGNMENT:
2866 error ("an assignment cannot appear in a constant-expression");
2867 return true;
2868 case NIC_COMMA:
2869 error ("a comma operator "
2870 "cannot appear in a constant-expression");
2871 return true;
2872 case NIC_CONSTRUCTOR:
2873 error ("a call to a constructor "
2874 "cannot appear in a constant-expression");
2875 return true;
2876 case NIC_TRANSACTION:
2877 error ("a transaction expression "
2878 "cannot appear in a constant-expression");
2879 return true;
2880 case NIC_THIS:
2881 msg = "this";
2882 break;
2883 case NIC_FUNC_NAME:
2884 msg = "__FUNCTION__";
2885 break;
2886 case NIC_PRETTY_FUNC:
2887 msg = "__PRETTY_FUNCTION__";
2888 break;
2889 case NIC_C99_FUNC:
2890 msg = "__func__";
2891 break;
2892 case NIC_VA_ARG:
2893 msg = "va_arg";
2894 break;
2895 case NIC_ARROW:
2896 msg = "->";
2897 break;
2898 case NIC_POINT:
2899 msg = ".";
2900 break;
2901 case NIC_STAR:
2902 msg = "*";
2903 break;
2904 case NIC_ADDR:
2905 msg = "&";
2906 break;
2907 case NIC_PREINCREMENT:
2908 msg = "++";
2909 break;
2910 case NIC_PREDECREMENT:
2911 msg = "--";
2912 break;
2913 case NIC_NEW:
2914 msg = "new";
2915 break;
2916 case NIC_DEL:
2917 msg = "delete";
2918 break;
2919 default:
2920 gcc_unreachable ();
2922 if (msg)
2923 error ("%qs cannot appear in a constant-expression", msg);
2924 return true;
2927 return false;
2930 /* Emit a diagnostic for an invalid type name. This function commits
2931 to the current active tentative parse, if any. (Otherwise, the
2932 problematic construct might be encountered again later, resulting
2933 in duplicate error messages.) LOCATION is the location of ID. */
2935 static void
2936 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2937 location_t location)
2939 tree decl, ambiguous_decls;
2940 cp_parser_commit_to_tentative_parse (parser);
2941 /* Try to lookup the identifier. */
2942 decl = cp_parser_lookup_name (parser, id, none_type,
2943 /*is_template=*/false,
2944 /*is_namespace=*/false,
2945 /*check_dependency=*/true,
2946 &ambiguous_decls, location);
2947 if (ambiguous_decls)
2948 /* If the lookup was ambiguous, an error will already have
2949 been issued. */
2950 return;
2951 /* If the lookup found a template-name, it means that the user forgot
2952 to specify an argument list. Emit a useful error message. */
2953 if (TREE_CODE (decl) == TEMPLATE_DECL)
2954 error_at (location,
2955 "invalid use of template-name %qE without an argument list",
2956 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 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2981 inform (location, "C++11 %<thread_local%> only available with "
2982 "-std=c++11 or -std=gnu++11");
2983 else if (processing_template_decl && current_class_type
2984 && TYPE_BINFO (current_class_type))
2986 tree b;
2988 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2990 b = TREE_CHAIN (b))
2992 tree base_type = BINFO_TYPE (b);
2993 if (CLASS_TYPE_P (base_type)
2994 && dependent_type_p (base_type))
2996 tree field;
2997 /* Go from a particular instantiation of the
2998 template (which will have an empty TYPE_FIELDs),
2999 to the main version. */
3000 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3001 for (field = TYPE_FIELDS (base_type);
3002 field;
3003 field = DECL_CHAIN (field))
3004 if (TREE_CODE (field) == TYPE_DECL
3005 && DECL_NAME (field) == id)
3007 inform (location,
3008 "(perhaps %<typename %T::%E%> was intended)",
3009 BINFO_TYPE (b), id);
3010 break;
3012 if (field)
3013 break;
3018 /* Here we diagnose qualified-ids where the scope is actually correct,
3019 but the identifier does not resolve to a valid type name. */
3020 else if (parser->scope != error_mark_node)
3022 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3024 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3025 error_at (location_of (id),
3026 "%qE in namespace %qE does not name a template type",
3027 id, parser->scope);
3028 else
3029 error_at (location_of (id),
3030 "%qE in namespace %qE does not name a type",
3031 id, parser->scope);
3033 else if (CLASS_TYPE_P (parser->scope)
3034 && constructor_name_p (id, parser->scope))
3036 /* A<T>::A<T>() */
3037 error_at (location, "%<%T::%E%> names the constructor, not"
3038 " the type", parser->scope, id);
3039 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3040 error_at (location, "and %qT has no template constructors",
3041 parser->scope);
3043 else if (TYPE_P (parser->scope)
3044 && dependent_scope_p (parser->scope))
3045 error_at (location, "need %<typename%> before %<%T::%E%> because "
3046 "%qT is a dependent scope",
3047 parser->scope, id, parser->scope);
3048 else if (TYPE_P (parser->scope))
3050 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3051 error_at (location_of (id),
3052 "%qE in %q#T does not name a template type",
3053 id, parser->scope);
3054 else
3055 error_at (location_of (id),
3056 "%qE in %q#T does not name a type",
3057 id, parser->scope);
3059 else
3060 gcc_unreachable ();
3064 /* Check for a common situation where a type-name should be present,
3065 but is not, and issue a sensible error message. Returns true if an
3066 invalid type-name was detected.
3068 The situation handled by this function are variable declarations of the
3069 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3070 Usually, `ID' should name a type, but if we got here it means that it
3071 does not. We try to emit the best possible error message depending on
3072 how exactly the id-expression looks like. */
3074 static bool
3075 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3077 tree id;
3078 cp_token *token = cp_lexer_peek_token (parser->lexer);
3080 /* Avoid duplicate error about ambiguous lookup. */
3081 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3083 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3084 if (next->type == CPP_NAME && next->error_reported)
3085 goto out;
3088 cp_parser_parse_tentatively (parser);
3089 id = cp_parser_id_expression (parser,
3090 /*template_keyword_p=*/false,
3091 /*check_dependency_p=*/true,
3092 /*template_p=*/NULL,
3093 /*declarator_p=*/true,
3094 /*optional_p=*/false);
3095 /* If the next token is a (, this is a function with no explicit return
3096 type, i.e. constructor, destructor or conversion op. */
3097 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3098 || TREE_CODE (id) == TYPE_DECL)
3100 cp_parser_abort_tentative_parse (parser);
3101 return false;
3103 if (!cp_parser_parse_definitely (parser))
3104 return false;
3106 /* Emit a diagnostic for the invalid type. */
3107 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3108 out:
3109 /* If we aren't in the middle of a declarator (i.e. in a
3110 parameter-declaration-clause), skip to the end of the declaration;
3111 there's no point in trying to process it. */
3112 if (!parser->in_declarator_p)
3113 cp_parser_skip_to_end_of_block_or_statement (parser);
3114 return true;
3117 /* Consume tokens up to, and including, the next non-nested closing `)'.
3118 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3119 are doing error recovery. Returns -1 if OR_COMMA is true and we
3120 found an unnested comma. */
3122 static int
3123 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3124 bool recovering,
3125 bool or_comma,
3126 bool consume_paren)
3128 unsigned paren_depth = 0;
3129 unsigned brace_depth = 0;
3130 unsigned square_depth = 0;
3132 if (recovering && !or_comma
3133 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3134 return 0;
3136 while (true)
3138 cp_token * token = cp_lexer_peek_token (parser->lexer);
3140 switch (token->type)
3142 case CPP_EOF:
3143 case CPP_PRAGMA_EOL:
3144 /* If we've run out of tokens, then there is no closing `)'. */
3145 return 0;
3147 /* This is good for lambda expression capture-lists. */
3148 case CPP_OPEN_SQUARE:
3149 ++square_depth;
3150 break;
3151 case CPP_CLOSE_SQUARE:
3152 if (!square_depth--)
3153 return 0;
3154 break;
3156 case CPP_SEMICOLON:
3157 /* This matches the processing in skip_to_end_of_statement. */
3158 if (!brace_depth)
3159 return 0;
3160 break;
3162 case CPP_OPEN_BRACE:
3163 ++brace_depth;
3164 break;
3165 case CPP_CLOSE_BRACE:
3166 if (!brace_depth--)
3167 return 0;
3168 break;
3170 case CPP_COMMA:
3171 if (recovering && or_comma && !brace_depth && !paren_depth
3172 && !square_depth)
3173 return -1;
3174 break;
3176 case CPP_OPEN_PAREN:
3177 if (!brace_depth)
3178 ++paren_depth;
3179 break;
3181 case CPP_CLOSE_PAREN:
3182 if (!brace_depth && !paren_depth--)
3184 if (consume_paren)
3185 cp_lexer_consume_token (parser->lexer);
3186 return 1;
3188 break;
3190 default:
3191 break;
3194 /* Consume the token. */
3195 cp_lexer_consume_token (parser->lexer);
3199 /* Consume tokens until we reach the end of the current statement.
3200 Normally, that will be just before consuming a `;'. However, if a
3201 non-nested `}' comes first, then we stop before consuming that. */
3203 static void
3204 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3206 unsigned nesting_depth = 0;
3208 /* Unwind generic function template scope if necessary. */
3209 if (parser->fully_implicit_function_template_p)
3210 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3212 while (true)
3214 cp_token *token = cp_lexer_peek_token (parser->lexer);
3216 switch (token->type)
3218 case CPP_EOF:
3219 case CPP_PRAGMA_EOL:
3220 /* If we've run out of tokens, stop. */
3221 return;
3223 case CPP_SEMICOLON:
3224 /* If the next token is a `;', we have reached the end of the
3225 statement. */
3226 if (!nesting_depth)
3227 return;
3228 break;
3230 case CPP_CLOSE_BRACE:
3231 /* If this is a non-nested '}', stop before consuming it.
3232 That way, when confronted with something like:
3234 { 3 + }
3236 we stop before consuming the closing '}', even though we
3237 have not yet reached a `;'. */
3238 if (nesting_depth == 0)
3239 return;
3241 /* If it is the closing '}' for a block that we have
3242 scanned, stop -- but only after consuming the token.
3243 That way given:
3245 void f g () { ... }
3246 typedef int I;
3248 we will stop after the body of the erroneously declared
3249 function, but before consuming the following `typedef'
3250 declaration. */
3251 if (--nesting_depth == 0)
3253 cp_lexer_consume_token (parser->lexer);
3254 return;
3257 case CPP_OPEN_BRACE:
3258 ++nesting_depth;
3259 break;
3261 default:
3262 break;
3265 /* Consume the token. */
3266 cp_lexer_consume_token (parser->lexer);
3270 /* This function is called at the end of a statement or declaration.
3271 If the next token is a semicolon, it is consumed; otherwise, error
3272 recovery is attempted. */
3274 static void
3275 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3277 /* Look for the trailing `;'. */
3278 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3280 /* If there is additional (erroneous) input, skip to the end of
3281 the statement. */
3282 cp_parser_skip_to_end_of_statement (parser);
3283 /* If the next token is now a `;', consume it. */
3284 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3285 cp_lexer_consume_token (parser->lexer);
3289 /* Skip tokens until we have consumed an entire block, or until we
3290 have consumed a non-nested `;'. */
3292 static void
3293 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3295 int nesting_depth = 0;
3297 /* Unwind generic function template scope if necessary. */
3298 if (parser->fully_implicit_function_template_p)
3299 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3301 while (nesting_depth >= 0)
3303 cp_token *token = cp_lexer_peek_token (parser->lexer);
3305 switch (token->type)
3307 case CPP_EOF:
3308 case CPP_PRAGMA_EOL:
3309 /* If we've run out of tokens, stop. */
3310 return;
3312 case CPP_SEMICOLON:
3313 /* Stop if this is an unnested ';'. */
3314 if (!nesting_depth)
3315 nesting_depth = -1;
3316 break;
3318 case CPP_CLOSE_BRACE:
3319 /* Stop if this is an unnested '}', or closes the outermost
3320 nesting level. */
3321 nesting_depth--;
3322 if (nesting_depth < 0)
3323 return;
3324 if (!nesting_depth)
3325 nesting_depth = -1;
3326 break;
3328 case CPP_OPEN_BRACE:
3329 /* Nest. */
3330 nesting_depth++;
3331 break;
3333 default:
3334 break;
3337 /* Consume the token. */
3338 cp_lexer_consume_token (parser->lexer);
3342 /* Skip tokens until a non-nested closing curly brace is the next
3343 token, or there are no more tokens. Return true in the first case,
3344 false otherwise. */
3346 static bool
3347 cp_parser_skip_to_closing_brace (cp_parser *parser)
3349 unsigned nesting_depth = 0;
3351 while (true)
3353 cp_token *token = cp_lexer_peek_token (parser->lexer);
3355 switch (token->type)
3357 case CPP_EOF:
3358 case CPP_PRAGMA_EOL:
3359 /* If we've run out of tokens, stop. */
3360 return false;
3362 case CPP_CLOSE_BRACE:
3363 /* If the next token is a non-nested `}', then we have reached
3364 the end of the current block. */
3365 if (nesting_depth-- == 0)
3366 return true;
3367 break;
3369 case CPP_OPEN_BRACE:
3370 /* If it the next token is a `{', then we are entering a new
3371 block. Consume the entire block. */
3372 ++nesting_depth;
3373 break;
3375 default:
3376 break;
3379 /* Consume the token. */
3380 cp_lexer_consume_token (parser->lexer);
3384 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3385 parameter is the PRAGMA token, allowing us to purge the entire pragma
3386 sequence. */
3388 static void
3389 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3391 cp_token *token;
3393 parser->lexer->in_pragma = false;
3396 token = cp_lexer_consume_token (parser->lexer);
3397 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3399 /* Ensure that the pragma is not parsed again. */
3400 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3403 /* Require pragma end of line, resyncing with it as necessary. The
3404 arguments are as for cp_parser_skip_to_pragma_eol. */
3406 static void
3407 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3409 parser->lexer->in_pragma = false;
3410 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3411 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3414 /* This is a simple wrapper around make_typename_type. When the id is
3415 an unresolved identifier node, we can provide a superior diagnostic
3416 using cp_parser_diagnose_invalid_type_name. */
3418 static tree
3419 cp_parser_make_typename_type (cp_parser *parser, tree id,
3420 location_t id_location)
3422 tree result;
3423 if (identifier_p (id))
3425 result = make_typename_type (parser->scope, id, typename_type,
3426 /*complain=*/tf_none);
3427 if (result == error_mark_node)
3428 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3429 return result;
3431 return make_typename_type (parser->scope, id, typename_type, tf_error);
3434 /* This is a wrapper around the
3435 make_{pointer,ptrmem,reference}_declarator functions that decides
3436 which one to call based on the CODE and CLASS_TYPE arguments. The
3437 CODE argument should be one of the values returned by
3438 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3439 appertain to the pointer or reference. */
3441 static cp_declarator *
3442 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3443 cp_cv_quals cv_qualifiers,
3444 cp_declarator *target,
3445 tree attributes)
3447 if (code == ERROR_MARK)
3448 return cp_error_declarator;
3450 if (code == INDIRECT_REF)
3451 if (class_type == NULL_TREE)
3452 return make_pointer_declarator (cv_qualifiers, target, attributes);
3453 else
3454 return make_ptrmem_declarator (cv_qualifiers, class_type,
3455 target, attributes);
3456 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3457 return make_reference_declarator (cv_qualifiers, target,
3458 false, attributes);
3459 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3460 return make_reference_declarator (cv_qualifiers, target,
3461 true, attributes);
3462 gcc_unreachable ();
3465 /* Create a new C++ parser. */
3467 static cp_parser *
3468 cp_parser_new (void)
3470 cp_parser *parser;
3471 cp_lexer *lexer;
3472 unsigned i;
3474 /* cp_lexer_new_main is called before doing GC allocation because
3475 cp_lexer_new_main might load a PCH file. */
3476 lexer = cp_lexer_new_main ();
3478 /* Initialize the binops_by_token so that we can get the tree
3479 directly from the token. */
3480 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3481 binops_by_token[binops[i].token_type] = binops[i];
3483 parser = ggc_cleared_alloc<cp_parser> ();
3484 parser->lexer = lexer;
3485 parser->context = cp_parser_context_new (NULL);
3487 /* For now, we always accept GNU extensions. */
3488 parser->allow_gnu_extensions_p = 1;
3490 /* The `>' token is a greater-than operator, not the end of a
3491 template-id. */
3492 parser->greater_than_is_operator_p = true;
3494 parser->default_arg_ok_p = true;
3496 /* We are not parsing a constant-expression. */
3497 parser->integral_constant_expression_p = false;
3498 parser->allow_non_integral_constant_expression_p = false;
3499 parser->non_integral_constant_expression_p = false;
3501 /* Local variable names are not forbidden. */
3502 parser->local_variables_forbidden_p = false;
3504 /* We are not processing an `extern "C"' declaration. */
3505 parser->in_unbraced_linkage_specification_p = false;
3507 /* We are not processing a declarator. */
3508 parser->in_declarator_p = false;
3510 /* We are not processing a template-argument-list. */
3511 parser->in_template_argument_list_p = false;
3513 /* We are not in an iteration statement. */
3514 parser->in_statement = 0;
3516 /* We are not in a switch statement. */
3517 parser->in_switch_statement_p = false;
3519 /* We are not parsing a type-id inside an expression. */
3520 parser->in_type_id_in_expr_p = false;
3522 /* Declarations aren't implicitly extern "C". */
3523 parser->implicit_extern_c = false;
3525 /* String literals should be translated to the execution character set. */
3526 parser->translate_strings_p = true;
3528 /* We are not parsing a function body. */
3529 parser->in_function_body = false;
3531 /* We can correct until told otherwise. */
3532 parser->colon_corrects_to_scope_p = true;
3534 /* The unparsed function queue is empty. */
3535 push_unparsed_function_queues (parser);
3537 /* There are no classes being defined. */
3538 parser->num_classes_being_defined = 0;
3540 /* No template parameters apply. */
3541 parser->num_template_parameter_lists = 0;
3543 /* Not declaring an implicit function template. */
3544 parser->auto_is_implicit_function_template_parm_p = false;
3545 parser->fully_implicit_function_template_p = false;
3546 parser->implicit_template_parms = 0;
3547 parser->implicit_template_scope = 0;
3549 return parser;
3552 /* Create a cp_lexer structure which will emit the tokens in CACHE
3553 and push it onto the parser's lexer stack. This is used for delayed
3554 parsing of in-class method bodies and default arguments, and should
3555 not be confused with tentative parsing. */
3556 static void
3557 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3559 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3560 lexer->next = parser->lexer;
3561 parser->lexer = lexer;
3563 /* Move the current source position to that of the first token in the
3564 new lexer. */
3565 cp_lexer_set_source_position_from_token (lexer->next_token);
3568 /* Pop the top lexer off the parser stack. This is never used for the
3569 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3570 static void
3571 cp_parser_pop_lexer (cp_parser *parser)
3573 cp_lexer *lexer = parser->lexer;
3574 parser->lexer = lexer->next;
3575 cp_lexer_destroy (lexer);
3577 /* Put the current source position back where it was before this
3578 lexer was pushed. */
3579 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3582 /* Lexical conventions [gram.lex] */
3584 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3585 identifier. */
3587 static tree
3588 cp_parser_identifier (cp_parser* parser)
3590 cp_token *token;
3592 /* Look for the identifier. */
3593 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3594 /* Return the value. */
3595 return token ? token->u.value : error_mark_node;
3598 /* Parse a sequence of adjacent string constants. Returns a
3599 TREE_STRING representing the combined, nul-terminated string
3600 constant. If TRANSLATE is true, translate the string to the
3601 execution character set. If WIDE_OK is true, a wide string is
3602 invalid here.
3604 C++98 [lex.string] says that if a narrow string literal token is
3605 adjacent to a wide string literal token, the behavior is undefined.
3606 However, C99 6.4.5p4 says that this results in a wide string literal.
3607 We follow C99 here, for consistency with the C front end.
3609 This code is largely lifted from lex_string() in c-lex.c.
3611 FUTURE: ObjC++ will need to handle @-strings here. */
3612 static tree
3613 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3614 bool lookup_udlit = true)
3616 tree value;
3617 size_t count;
3618 struct obstack str_ob;
3619 cpp_string str, istr, *strs;
3620 cp_token *tok;
3621 enum cpp_ttype type, curr_type;
3622 int have_suffix_p = 0;
3623 tree string_tree;
3624 tree suffix_id = NULL_TREE;
3625 bool curr_tok_is_userdef_p = false;
3627 tok = cp_lexer_peek_token (parser->lexer);
3628 if (!cp_parser_is_string_literal (tok))
3630 cp_parser_error (parser, "expected string-literal");
3631 return error_mark_node;
3634 if (cpp_userdef_string_p (tok->type))
3636 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3637 curr_type = cpp_userdef_string_remove_type (tok->type);
3638 curr_tok_is_userdef_p = true;
3640 else
3642 string_tree = tok->u.value;
3643 curr_type = tok->type;
3645 type = curr_type;
3647 /* Try to avoid the overhead of creating and destroying an obstack
3648 for the common case of just one string. */
3649 if (!cp_parser_is_string_literal
3650 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3652 cp_lexer_consume_token (parser->lexer);
3654 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3655 str.len = TREE_STRING_LENGTH (string_tree);
3656 count = 1;
3658 if (curr_tok_is_userdef_p)
3660 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3661 have_suffix_p = 1;
3662 curr_type = cpp_userdef_string_remove_type (tok->type);
3664 else
3665 curr_type = tok->type;
3667 strs = &str;
3669 else
3671 gcc_obstack_init (&str_ob);
3672 count = 0;
3676 cp_lexer_consume_token (parser->lexer);
3677 count++;
3678 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3679 str.len = TREE_STRING_LENGTH (string_tree);
3681 if (curr_tok_is_userdef_p)
3683 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3684 if (have_suffix_p == 0)
3686 suffix_id = curr_suffix_id;
3687 have_suffix_p = 1;
3689 else if (have_suffix_p == 1
3690 && curr_suffix_id != suffix_id)
3692 error ("inconsistent user-defined literal suffixes"
3693 " %qD and %qD in string literal",
3694 suffix_id, curr_suffix_id);
3695 have_suffix_p = -1;
3697 curr_type = cpp_userdef_string_remove_type (tok->type);
3699 else
3700 curr_type = tok->type;
3702 if (type != curr_type)
3704 if (type == CPP_STRING)
3705 type = curr_type;
3706 else if (curr_type != CPP_STRING)
3707 error_at (tok->location,
3708 "unsupported non-standard concatenation "
3709 "of string literals");
3712 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3714 tok = cp_lexer_peek_token (parser->lexer);
3715 if (cpp_userdef_string_p (tok->type))
3717 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3718 curr_type = cpp_userdef_string_remove_type (tok->type);
3719 curr_tok_is_userdef_p = true;
3721 else
3723 string_tree = tok->u.value;
3724 curr_type = tok->type;
3725 curr_tok_is_userdef_p = false;
3728 while (cp_parser_is_string_literal (tok));
3730 strs = (cpp_string *) obstack_finish (&str_ob);
3733 if (type != CPP_STRING && !wide_ok)
3735 cp_parser_error (parser, "a wide string is invalid in this context");
3736 type = CPP_STRING;
3739 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3740 (parse_in, strs, count, &istr, type))
3742 value = build_string (istr.len, (const char *)istr.text);
3743 free (CONST_CAST (unsigned char *, istr.text));
3745 switch (type)
3747 default:
3748 case CPP_STRING:
3749 case CPP_UTF8STRING:
3750 TREE_TYPE (value) = char_array_type_node;
3751 break;
3752 case CPP_STRING16:
3753 TREE_TYPE (value) = char16_array_type_node;
3754 break;
3755 case CPP_STRING32:
3756 TREE_TYPE (value) = char32_array_type_node;
3757 break;
3758 case CPP_WSTRING:
3759 TREE_TYPE (value) = wchar_array_type_node;
3760 break;
3763 value = fix_string_type (value);
3765 if (have_suffix_p)
3767 tree literal = build_userdef_literal (suffix_id, value,
3768 OT_NONE, NULL_TREE);
3769 if (lookup_udlit)
3770 value = cp_parser_userdef_string_literal (literal);
3771 else
3772 value = literal;
3775 else
3776 /* cpp_interpret_string has issued an error. */
3777 value = error_mark_node;
3779 if (count > 1)
3780 obstack_free (&str_ob, 0);
3782 return value;
3785 /* Look up a literal operator with the name and the exact arguments. */
3787 static tree
3788 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3790 tree decl, fns;
3791 decl = lookup_name (name);
3792 if (!decl || !is_overloaded_fn (decl))
3793 return error_mark_node;
3795 for (fns = decl; fns; fns = OVL_NEXT (fns))
3797 unsigned int ix;
3798 bool found = true;
3799 tree fn = OVL_CURRENT (fns);
3800 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3801 if (parmtypes != NULL_TREE)
3803 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3804 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3806 tree tparm = TREE_VALUE (parmtypes);
3807 tree targ = TREE_TYPE ((*args)[ix]);
3808 bool ptr = TYPE_PTR_P (tparm);
3809 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3810 if ((ptr || arr || !same_type_p (tparm, targ))
3811 && (!ptr || !arr
3812 || !same_type_p (TREE_TYPE (tparm),
3813 TREE_TYPE (targ))))
3814 found = false;
3816 if (found
3817 && ix == vec_safe_length (args)
3818 /* May be this should be sufficient_parms_p instead,
3819 depending on how exactly should user-defined literals
3820 work in presence of default arguments on the literal
3821 operator parameters. */
3822 && parmtypes == void_list_node)
3823 return fn;
3827 return error_mark_node;
3830 /* Parse a user-defined char constant. Returns a call to a user-defined
3831 literal operator taking the character as an argument. */
3833 static tree
3834 cp_parser_userdef_char_literal (cp_parser *parser)
3836 cp_token *token = cp_lexer_consume_token (parser->lexer);
3837 tree literal = token->u.value;
3838 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3839 tree value = USERDEF_LITERAL_VALUE (literal);
3840 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3841 tree decl, result;
3843 /* Build up a call to the user-defined operator */
3844 /* Lookup the name we got back from the id-expression. */
3845 vec<tree, va_gc> *args = make_tree_vector ();
3846 vec_safe_push (args, value);
3847 decl = lookup_literal_operator (name, args);
3848 if (!decl || decl == error_mark_node)
3850 error ("unable to find character literal operator %qD with %qT argument",
3851 name, TREE_TYPE (value));
3852 release_tree_vector (args);
3853 return error_mark_node;
3855 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3856 release_tree_vector (args);
3857 if (result != error_mark_node)
3858 return result;
3860 error ("unable to find character literal operator %qD with %qT argument",
3861 name, TREE_TYPE (value));
3862 return error_mark_node;
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, tf_none);
3951 if (result != error_mark_node)
3953 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3954 warning_at (token->location, OPT_Woverflow,
3955 "integer literal exceeds range of %qT type",
3956 long_long_unsigned_type_node);
3957 else
3959 if (overflow > 0)
3960 warning_at (token->location, OPT_Woverflow,
3961 "floating literal exceeds range of %qT type",
3962 long_double_type_node);
3963 else if (overflow < 0)
3964 warning_at (token->location, OPT_Woverflow,
3965 "floating literal truncated to zero");
3967 release_tree_vector (args);
3968 return result;
3971 release_tree_vector (args);
3973 /* If the numeric argument didn't work, look for a raw literal
3974 operator taking a const char* argument consisting of the number
3975 in string format. */
3976 args = make_tree_vector ();
3977 vec_safe_push (args, num_string);
3978 decl = lookup_literal_operator (name, args);
3979 if (decl && decl != error_mark_node)
3981 result = finish_call_expr (decl, &args, false, true, tf_none);
3982 if (result != error_mark_node)
3984 release_tree_vector (args);
3985 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, tf_none);
4000 if (result != error_mark_node)
4002 release_tree_vector (args);
4003 return result;
4006 release_tree_vector (args);
4008 error ("unable to find numeric literal operator %qD", name);
4009 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4010 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4011 "to enable more built-in suffixes");
4012 return error_mark_node;
4015 /* Parse a user-defined string constant. Returns a call to a user-defined
4016 literal operator taking a character pointer and the length of the string
4017 as arguments. */
4019 static tree
4020 cp_parser_userdef_string_literal (tree literal)
4022 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4023 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4024 tree value = USERDEF_LITERAL_VALUE (literal);
4025 int len = TREE_STRING_LENGTH (value)
4026 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4027 tree decl, result;
4028 vec<tree, va_gc> *args;
4030 /* Look for a template function with typename parameter CharT
4031 and parameter pack CharT... Call the function with
4032 template parameter characters representing the string. */
4033 args = make_tree_vector ();
4034 decl = lookup_literal_operator (name, args);
4035 if (decl && decl != error_mark_node)
4037 tree tmpl_args = make_string_pack (value);
4038 decl = lookup_template_function (decl, tmpl_args);
4039 result = finish_call_expr (decl, &args, false, true, tf_none);
4040 if (result != error_mark_node)
4042 release_tree_vector (args);
4043 return result;
4046 release_tree_vector (args);
4048 /* Build up a call to the user-defined operator */
4049 /* Lookup the name we got back from the id-expression. */
4050 args = make_tree_vector ();
4051 vec_safe_push (args, value);
4052 vec_safe_push (args, build_int_cst (size_type_node, len));
4053 decl = lookup_name (name);
4054 if (!decl || decl == error_mark_node)
4056 error ("unable to find string literal operator %qD", name);
4057 release_tree_vector (args);
4058 return error_mark_node;
4060 result = finish_call_expr (decl, &args, false, true, tf_none);
4061 release_tree_vector (args);
4062 if (result != error_mark_node)
4063 return result;
4065 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4066 name, TREE_TYPE (value), size_type_node);
4067 return error_mark_node;
4071 /* Basic concepts [gram.basic] */
4073 /* Parse a translation-unit.
4075 translation-unit:
4076 declaration-seq [opt]
4078 Returns TRUE if all went well. */
4080 static bool
4081 cp_parser_translation_unit (cp_parser* parser)
4083 /* The address of the first non-permanent object on the declarator
4084 obstack. */
4085 static void *declarator_obstack_base;
4087 bool success;
4089 /* Create the declarator obstack, if necessary. */
4090 if (!cp_error_declarator)
4092 gcc_obstack_init (&declarator_obstack);
4093 /* Create the error declarator. */
4094 cp_error_declarator = make_declarator (cdk_error);
4095 /* Create the empty parameter list. */
4096 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4097 /* Remember where the base of the declarator obstack lies. */
4098 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4101 cp_parser_declaration_seq_opt (parser);
4103 /* If there are no tokens left then all went well. */
4104 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4106 /* Get rid of the token array; we don't need it any more. */
4107 cp_lexer_destroy (parser->lexer);
4108 parser->lexer = NULL;
4110 /* This file might have been a context that's implicitly extern
4111 "C". If so, pop the lang context. (Only relevant for PCH.) */
4112 if (parser->implicit_extern_c)
4114 pop_lang_context ();
4115 parser->implicit_extern_c = false;
4118 /* Finish up. */
4119 finish_translation_unit ();
4121 success = true;
4123 else
4125 cp_parser_error (parser, "expected declaration");
4126 success = false;
4129 /* Make sure the declarator obstack was fully cleaned up. */
4130 gcc_assert (obstack_next_free (&declarator_obstack)
4131 == declarator_obstack_base);
4133 /* All went well. */
4134 return success;
4137 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4138 decltype context. */
4140 static inline tsubst_flags_t
4141 complain_flags (bool decltype_p)
4143 tsubst_flags_t complain = tf_warning_or_error;
4144 if (decltype_p)
4145 complain |= tf_decltype;
4146 return complain;
4149 /* We're about to parse a collection of statements. If we're currently
4150 parsing tentatively, set up a firewall so that any nested
4151 cp_parser_commit_to_tentative_parse won't affect the current context. */
4153 static cp_token_position
4154 cp_parser_start_tentative_firewall (cp_parser *parser)
4156 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4157 return 0;
4159 cp_parser_parse_tentatively (parser);
4160 cp_parser_commit_to_topmost_tentative_parse (parser);
4161 return cp_lexer_token_position (parser->lexer, false);
4164 /* We've finished parsing the collection of statements. Wrap up the
4165 firewall and replace the relevant tokens with the parsed form. */
4167 static void
4168 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4169 tree expr)
4171 if (!start)
4172 return;
4174 /* Finish the firewall level. */
4175 cp_parser_parse_definitely (parser);
4176 /* And remember the result of the parse for when we try again. */
4177 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4178 token->type = CPP_PREPARSED_EXPR;
4179 token->u.value = expr;
4180 token->keyword = RID_MAX;
4181 cp_lexer_purge_tokens_after (parser->lexer, start);
4184 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4185 enclosing parentheses. */
4187 static tree
4188 cp_parser_statement_expr (cp_parser *parser)
4190 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4192 /* Consume the '('. */
4193 cp_lexer_consume_token (parser->lexer);
4194 /* Start the statement-expression. */
4195 tree expr = begin_stmt_expr ();
4196 /* Parse the compound-statement. */
4197 cp_parser_compound_statement (parser, expr, false, false);
4198 /* Finish up. */
4199 expr = finish_stmt_expr (expr, false);
4200 /* Consume the ')'. */
4201 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4202 cp_parser_skip_to_end_of_statement (parser);
4204 cp_parser_end_tentative_firewall (parser, start, expr);
4205 return expr;
4208 /* Expressions [gram.expr] */
4210 /* Parse a primary-expression.
4212 primary-expression:
4213 literal
4214 this
4215 ( expression )
4216 id-expression
4217 lambda-expression (C++11)
4219 GNU Extensions:
4221 primary-expression:
4222 ( compound-statement )
4223 __builtin_va_arg ( assignment-expression , type-id )
4224 __builtin_offsetof ( type-id , offsetof-expression )
4226 C++ Extensions:
4227 __has_nothrow_assign ( type-id )
4228 __has_nothrow_constructor ( type-id )
4229 __has_nothrow_copy ( type-id )
4230 __has_trivial_assign ( type-id )
4231 __has_trivial_constructor ( type-id )
4232 __has_trivial_copy ( type-id )
4233 __has_trivial_destructor ( type-id )
4234 __has_virtual_destructor ( type-id )
4235 __is_abstract ( type-id )
4236 __is_base_of ( type-id , type-id )
4237 __is_class ( type-id )
4238 __is_empty ( type-id )
4239 __is_enum ( type-id )
4240 __is_final ( type-id )
4241 __is_literal_type ( type-id )
4242 __is_pod ( type-id )
4243 __is_polymorphic ( type-id )
4244 __is_std_layout ( type-id )
4245 __is_trivial ( type-id )
4246 __is_union ( type-id )
4248 Objective-C++ Extension:
4250 primary-expression:
4251 objc-expression
4253 literal:
4254 __null
4256 ADDRESS_P is true iff this expression was immediately preceded by
4257 "&" and therefore might denote a pointer-to-member. CAST_P is true
4258 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4259 true iff this expression is a template argument.
4261 Returns a representation of the expression. Upon return, *IDK
4262 indicates what kind of id-expression (if any) was present. */
4264 static tree
4265 cp_parser_primary_expression (cp_parser *parser,
4266 bool address_p,
4267 bool cast_p,
4268 bool template_arg_p,
4269 bool decltype_p,
4270 cp_id_kind *idk)
4272 cp_token *token = NULL;
4274 /* Assume the primary expression is not an id-expression. */
4275 *idk = CP_ID_KIND_NONE;
4277 /* Peek at the next token. */
4278 token = cp_lexer_peek_token (parser->lexer);
4279 switch ((int) token->type)
4281 /* literal:
4282 integer-literal
4283 character-literal
4284 floating-literal
4285 string-literal
4286 boolean-literal
4287 pointer-literal
4288 user-defined-literal */
4289 case CPP_CHAR:
4290 case CPP_CHAR16:
4291 case CPP_CHAR32:
4292 case CPP_WCHAR:
4293 case CPP_NUMBER:
4294 case CPP_PREPARSED_EXPR:
4295 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4296 return cp_parser_userdef_numeric_literal (parser);
4297 token = cp_lexer_consume_token (parser->lexer);
4298 if (TREE_CODE (token->u.value) == FIXED_CST)
4300 error_at (token->location,
4301 "fixed-point types not supported in C++");
4302 return error_mark_node;
4304 /* Floating-point literals are only allowed in an integral
4305 constant expression if they are cast to an integral or
4306 enumeration type. */
4307 if (TREE_CODE (token->u.value) == REAL_CST
4308 && parser->integral_constant_expression_p
4309 && pedantic)
4311 /* CAST_P will be set even in invalid code like "int(2.7 +
4312 ...)". Therefore, we have to check that the next token
4313 is sure to end the cast. */
4314 if (cast_p)
4316 cp_token *next_token;
4318 next_token = cp_lexer_peek_token (parser->lexer);
4319 if (/* The comma at the end of an
4320 enumerator-definition. */
4321 next_token->type != CPP_COMMA
4322 /* The curly brace at the end of an enum-specifier. */
4323 && next_token->type != CPP_CLOSE_BRACE
4324 /* The end of a statement. */
4325 && next_token->type != CPP_SEMICOLON
4326 /* The end of the cast-expression. */
4327 && next_token->type != CPP_CLOSE_PAREN
4328 /* The end of an array bound. */
4329 && next_token->type != CPP_CLOSE_SQUARE
4330 /* The closing ">" in a template-argument-list. */
4331 && (next_token->type != CPP_GREATER
4332 || parser->greater_than_is_operator_p)
4333 /* C++0x only: A ">>" treated like two ">" tokens,
4334 in a template-argument-list. */
4335 && (next_token->type != CPP_RSHIFT
4336 || (cxx_dialect == cxx98)
4337 || parser->greater_than_is_operator_p))
4338 cast_p = false;
4341 /* If we are within a cast, then the constraint that the
4342 cast is to an integral or enumeration type will be
4343 checked at that point. If we are not within a cast, then
4344 this code is invalid. */
4345 if (!cast_p)
4346 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4348 return token->u.value;
4350 case CPP_CHAR_USERDEF:
4351 case CPP_CHAR16_USERDEF:
4352 case CPP_CHAR32_USERDEF:
4353 case CPP_WCHAR_USERDEF:
4354 return cp_parser_userdef_char_literal (parser);
4356 case CPP_STRING:
4357 case CPP_STRING16:
4358 case CPP_STRING32:
4359 case CPP_WSTRING:
4360 case CPP_UTF8STRING:
4361 case CPP_STRING_USERDEF:
4362 case CPP_STRING16_USERDEF:
4363 case CPP_STRING32_USERDEF:
4364 case CPP_WSTRING_USERDEF:
4365 case CPP_UTF8STRING_USERDEF:
4366 /* ??? Should wide strings be allowed when parser->translate_strings_p
4367 is false (i.e. in attributes)? If not, we can kill the third
4368 argument to cp_parser_string_literal. */
4369 return cp_parser_string_literal (parser,
4370 parser->translate_strings_p,
4371 true);
4373 case CPP_OPEN_PAREN:
4374 /* If we see `( { ' then we are looking at the beginning of
4375 a GNU statement-expression. */
4376 if (cp_parser_allow_gnu_extensions_p (parser)
4377 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4379 /* Statement-expressions are not allowed by the standard. */
4380 pedwarn (token->location, OPT_Wpedantic,
4381 "ISO C++ forbids braced-groups within expressions");
4383 /* And they're not allowed outside of a function-body; you
4384 cannot, for example, write:
4386 int i = ({ int j = 3; j + 1; });
4388 at class or namespace scope. */
4389 if (!parser->in_function_body
4390 || parser->in_template_argument_list_p)
4392 error_at (token->location,
4393 "statement-expressions are not allowed outside "
4394 "functions nor in template-argument lists");
4395 cp_parser_skip_to_end_of_block_or_statement (parser);
4396 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4397 cp_lexer_consume_token (parser->lexer);
4398 return error_mark_node;
4400 else
4401 return cp_parser_statement_expr (parser);
4403 /* Otherwise it's a normal parenthesized expression. */
4405 tree expr;
4406 bool saved_greater_than_is_operator_p;
4408 /* Consume the `('. */
4409 cp_lexer_consume_token (parser->lexer);
4410 /* Within a parenthesized expression, a `>' token is always
4411 the greater-than operator. */
4412 saved_greater_than_is_operator_p
4413 = parser->greater_than_is_operator_p;
4414 parser->greater_than_is_operator_p = true;
4416 /* Parse the parenthesized expression. */
4417 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4418 /* Let the front end know that this expression was
4419 enclosed in parentheses. This matters in case, for
4420 example, the expression is of the form `A::B', since
4421 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4422 not. */
4423 expr = finish_parenthesized_expr (expr);
4424 /* DR 705: Wrapping an unqualified name in parentheses
4425 suppresses arg-dependent lookup. We want to pass back
4426 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4427 (c++/37862), but none of the others. */
4428 if (*idk != CP_ID_KIND_QUALIFIED)
4429 *idk = CP_ID_KIND_NONE;
4431 /* The `>' token might be the end of a template-id or
4432 template-parameter-list now. */
4433 parser->greater_than_is_operator_p
4434 = saved_greater_than_is_operator_p;
4435 /* Consume the `)'. */
4436 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4437 cp_parser_skip_to_end_of_statement (parser);
4439 return expr;
4442 case CPP_OPEN_SQUARE:
4443 if (c_dialect_objc ())
4444 /* We have an Objective-C++ message. */
4445 return cp_parser_objc_expression (parser);
4447 tree lam = cp_parser_lambda_expression (parser);
4448 /* Don't warn about a failed tentative parse. */
4449 if (cp_parser_error_occurred (parser))
4450 return error_mark_node;
4451 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4452 return lam;
4455 case CPP_OBJC_STRING:
4456 if (c_dialect_objc ())
4457 /* We have an Objective-C++ string literal. */
4458 return cp_parser_objc_expression (parser);
4459 cp_parser_error (parser, "expected primary-expression");
4460 return error_mark_node;
4462 case CPP_KEYWORD:
4463 switch (token->keyword)
4465 /* These two are the boolean literals. */
4466 case RID_TRUE:
4467 cp_lexer_consume_token (parser->lexer);
4468 return boolean_true_node;
4469 case RID_FALSE:
4470 cp_lexer_consume_token (parser->lexer);
4471 return boolean_false_node;
4473 /* The `__null' literal. */
4474 case RID_NULL:
4475 cp_lexer_consume_token (parser->lexer);
4476 return null_node;
4478 /* The `nullptr' literal. */
4479 case RID_NULLPTR:
4480 cp_lexer_consume_token (parser->lexer);
4481 return nullptr_node;
4483 /* Recognize the `this' keyword. */
4484 case RID_THIS:
4485 cp_lexer_consume_token (parser->lexer);
4486 if (parser->local_variables_forbidden_p)
4488 error_at (token->location,
4489 "%<this%> may not be used in this context");
4490 return error_mark_node;
4492 /* Pointers cannot appear in constant-expressions. */
4493 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4494 return error_mark_node;
4495 return finish_this_expr ();
4497 /* The `operator' keyword can be the beginning of an
4498 id-expression. */
4499 case RID_OPERATOR:
4500 goto id_expression;
4502 case RID_FUNCTION_NAME:
4503 case RID_PRETTY_FUNCTION_NAME:
4504 case RID_C99_FUNCTION_NAME:
4506 non_integral_constant name;
4508 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4509 __func__ are the names of variables -- but they are
4510 treated specially. Therefore, they are handled here,
4511 rather than relying on the generic id-expression logic
4512 below. Grammatically, these names are id-expressions.
4514 Consume the token. */
4515 token = cp_lexer_consume_token (parser->lexer);
4517 switch (token->keyword)
4519 case RID_FUNCTION_NAME:
4520 name = NIC_FUNC_NAME;
4521 break;
4522 case RID_PRETTY_FUNCTION_NAME:
4523 name = NIC_PRETTY_FUNC;
4524 break;
4525 case RID_C99_FUNCTION_NAME:
4526 name = NIC_C99_FUNC;
4527 break;
4528 default:
4529 gcc_unreachable ();
4532 if (cp_parser_non_integral_constant_expression (parser, name))
4533 return error_mark_node;
4535 /* Look up the name. */
4536 return finish_fname (token->u.value);
4539 case RID_VA_ARG:
4541 tree expression;
4542 tree type;
4543 source_location type_location;
4545 /* The `__builtin_va_arg' construct is used to handle
4546 `va_arg'. Consume the `__builtin_va_arg' token. */
4547 cp_lexer_consume_token (parser->lexer);
4548 /* Look for the opening `('. */
4549 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4550 /* Now, parse the assignment-expression. */
4551 expression = cp_parser_assignment_expression (parser);
4552 /* Look for the `,'. */
4553 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4554 type_location = cp_lexer_peek_token (parser->lexer)->location;
4555 /* Parse the type-id. */
4556 type = cp_parser_type_id (parser);
4557 /* Look for the closing `)'. */
4558 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4559 /* Using `va_arg' in a constant-expression is not
4560 allowed. */
4561 if (cp_parser_non_integral_constant_expression (parser,
4562 NIC_VA_ARG))
4563 return error_mark_node;
4564 return build_x_va_arg (type_location, expression, type);
4567 case RID_OFFSETOF:
4568 return cp_parser_builtin_offsetof (parser);
4570 case RID_HAS_NOTHROW_ASSIGN:
4571 case RID_HAS_NOTHROW_CONSTRUCTOR:
4572 case RID_HAS_NOTHROW_COPY:
4573 case RID_HAS_TRIVIAL_ASSIGN:
4574 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4575 case RID_HAS_TRIVIAL_COPY:
4576 case RID_HAS_TRIVIAL_DESTRUCTOR:
4577 case RID_HAS_VIRTUAL_DESTRUCTOR:
4578 case RID_IS_ABSTRACT:
4579 case RID_IS_BASE_OF:
4580 case RID_IS_CLASS:
4581 case RID_IS_EMPTY:
4582 case RID_IS_ENUM:
4583 case RID_IS_FINAL:
4584 case RID_IS_LITERAL_TYPE:
4585 case RID_IS_POD:
4586 case RID_IS_POLYMORPHIC:
4587 case RID_IS_STD_LAYOUT:
4588 case RID_IS_TRIVIAL:
4589 case RID_IS_TRIVIALLY_ASSIGNABLE:
4590 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4591 case RID_IS_TRIVIALLY_COPYABLE:
4592 case RID_IS_UNION:
4593 return cp_parser_trait_expr (parser, token->keyword);
4595 /* Objective-C++ expressions. */
4596 case RID_AT_ENCODE:
4597 case RID_AT_PROTOCOL:
4598 case RID_AT_SELECTOR:
4599 return cp_parser_objc_expression (parser);
4601 case RID_TEMPLATE:
4602 if (parser->in_function_body
4603 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4604 == CPP_LESS))
4606 error_at (token->location,
4607 "a template declaration cannot appear at block scope");
4608 cp_parser_skip_to_end_of_block_or_statement (parser);
4609 return error_mark_node;
4611 default:
4612 cp_parser_error (parser, "expected primary-expression");
4613 return error_mark_node;
4616 /* An id-expression can start with either an identifier, a
4617 `::' as the beginning of a qualified-id, or the "operator"
4618 keyword. */
4619 case CPP_NAME:
4620 case CPP_SCOPE:
4621 case CPP_TEMPLATE_ID:
4622 case CPP_NESTED_NAME_SPECIFIER:
4624 tree id_expression;
4625 tree decl;
4626 const char *error_msg;
4627 bool template_p;
4628 bool done;
4629 cp_token *id_expr_token;
4631 id_expression:
4632 /* Parse the id-expression. */
4633 id_expression
4634 = cp_parser_id_expression (parser,
4635 /*template_keyword_p=*/false,
4636 /*check_dependency_p=*/true,
4637 &template_p,
4638 /*declarator_p=*/false,
4639 /*optional_p=*/false);
4640 if (id_expression == error_mark_node)
4641 return error_mark_node;
4642 id_expr_token = token;
4643 token = cp_lexer_peek_token (parser->lexer);
4644 done = (token->type != CPP_OPEN_SQUARE
4645 && token->type != CPP_OPEN_PAREN
4646 && token->type != CPP_DOT
4647 && token->type != CPP_DEREF
4648 && token->type != CPP_PLUS_PLUS
4649 && token->type != CPP_MINUS_MINUS);
4650 /* If we have a template-id, then no further lookup is
4651 required. If the template-id was for a template-class, we
4652 will sometimes have a TYPE_DECL at this point. */
4653 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4654 || TREE_CODE (id_expression) == TYPE_DECL)
4655 decl = id_expression;
4656 /* Look up the name. */
4657 else
4659 tree ambiguous_decls;
4661 /* If we already know that this lookup is ambiguous, then
4662 we've already issued an error message; there's no reason
4663 to check again. */
4664 if (id_expr_token->type == CPP_NAME
4665 && id_expr_token->error_reported)
4667 cp_parser_simulate_error (parser);
4668 return error_mark_node;
4671 decl = cp_parser_lookup_name (parser, id_expression,
4672 none_type,
4673 template_p,
4674 /*is_namespace=*/false,
4675 /*check_dependency=*/true,
4676 &ambiguous_decls,
4677 id_expr_token->location);
4678 /* If the lookup was ambiguous, an error will already have
4679 been issued. */
4680 if (ambiguous_decls)
4681 return error_mark_node;
4683 /* In Objective-C++, we may have an Objective-C 2.0
4684 dot-syntax for classes here. */
4685 if (c_dialect_objc ()
4686 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4687 && TREE_CODE (decl) == TYPE_DECL
4688 && objc_is_class_name (decl))
4690 tree component;
4691 cp_lexer_consume_token (parser->lexer);
4692 component = cp_parser_identifier (parser);
4693 if (component == error_mark_node)
4694 return error_mark_node;
4696 return objc_build_class_component_ref (id_expression, component);
4699 /* In Objective-C++, an instance variable (ivar) may be preferred
4700 to whatever cp_parser_lookup_name() found. */
4701 decl = objc_lookup_ivar (decl, id_expression);
4703 /* If name lookup gives us a SCOPE_REF, then the
4704 qualifying scope was dependent. */
4705 if (TREE_CODE (decl) == SCOPE_REF)
4707 /* At this point, we do not know if DECL is a valid
4708 integral constant expression. We assume that it is
4709 in fact such an expression, so that code like:
4711 template <int N> struct A {
4712 int a[B<N>::i];
4715 is accepted. At template-instantiation time, we
4716 will check that B<N>::i is actually a constant. */
4717 return decl;
4719 /* Check to see if DECL is a local variable in a context
4720 where that is forbidden. */
4721 if (parser->local_variables_forbidden_p
4722 && local_variable_p (decl))
4724 /* It might be that we only found DECL because we are
4725 trying to be generous with pre-ISO scoping rules.
4726 For example, consider:
4728 int i;
4729 void g() {
4730 for (int i = 0; i < 10; ++i) {}
4731 extern void f(int j = i);
4734 Here, name look up will originally find the out
4735 of scope `i'. We need to issue a warning message,
4736 but then use the global `i'. */
4737 decl = check_for_out_of_scope_variable (decl);
4738 if (local_variable_p (decl))
4740 error_at (id_expr_token->location,
4741 "local variable %qD may not appear in this context",
4742 decl);
4743 return error_mark_node;
4748 decl = (finish_id_expression
4749 (id_expression, decl, parser->scope,
4750 idk,
4751 parser->integral_constant_expression_p,
4752 parser->allow_non_integral_constant_expression_p,
4753 &parser->non_integral_constant_expression_p,
4754 template_p, done, address_p,
4755 template_arg_p,
4756 &error_msg,
4757 id_expr_token->location));
4758 if (error_msg)
4759 cp_parser_error (parser, error_msg);
4760 return decl;
4763 /* Anything else is an error. */
4764 default:
4765 cp_parser_error (parser, "expected primary-expression");
4766 return error_mark_node;
4770 static inline tree
4771 cp_parser_primary_expression (cp_parser *parser,
4772 bool address_p,
4773 bool cast_p,
4774 bool template_arg_p,
4775 cp_id_kind *idk)
4777 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4778 /*decltype*/false, idk);
4781 /* Parse an id-expression.
4783 id-expression:
4784 unqualified-id
4785 qualified-id
4787 qualified-id:
4788 :: [opt] nested-name-specifier template [opt] unqualified-id
4789 :: identifier
4790 :: operator-function-id
4791 :: template-id
4793 Return a representation of the unqualified portion of the
4794 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4795 a `::' or nested-name-specifier.
4797 Often, if the id-expression was a qualified-id, the caller will
4798 want to make a SCOPE_REF to represent the qualified-id. This
4799 function does not do this in order to avoid wastefully creating
4800 SCOPE_REFs when they are not required.
4802 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4803 `template' keyword.
4805 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4806 uninstantiated templates.
4808 If *TEMPLATE_P is non-NULL, it is set to true iff the
4809 `template' keyword is used to explicitly indicate that the entity
4810 named is a template.
4812 If DECLARATOR_P is true, the id-expression is appearing as part of
4813 a declarator, rather than as part of an expression. */
4815 static tree
4816 cp_parser_id_expression (cp_parser *parser,
4817 bool template_keyword_p,
4818 bool check_dependency_p,
4819 bool *template_p,
4820 bool declarator_p,
4821 bool optional_p)
4823 bool global_scope_p;
4824 bool nested_name_specifier_p;
4826 /* Assume the `template' keyword was not used. */
4827 if (template_p)
4828 *template_p = template_keyword_p;
4830 /* Look for the optional `::' operator. */
4831 global_scope_p
4832 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4833 != NULL_TREE);
4834 /* Look for the optional nested-name-specifier. */
4835 nested_name_specifier_p
4836 = (cp_parser_nested_name_specifier_opt (parser,
4837 /*typename_keyword_p=*/false,
4838 check_dependency_p,
4839 /*type_p=*/false,
4840 declarator_p)
4841 != NULL_TREE);
4842 /* If there is a nested-name-specifier, then we are looking at
4843 the first qualified-id production. */
4844 if (nested_name_specifier_p)
4846 tree saved_scope;
4847 tree saved_object_scope;
4848 tree saved_qualifying_scope;
4849 tree unqualified_id;
4850 bool is_template;
4852 /* See if the next token is the `template' keyword. */
4853 if (!template_p)
4854 template_p = &is_template;
4855 *template_p = cp_parser_optional_template_keyword (parser);
4856 /* Name lookup we do during the processing of the
4857 unqualified-id might obliterate SCOPE. */
4858 saved_scope = parser->scope;
4859 saved_object_scope = parser->object_scope;
4860 saved_qualifying_scope = parser->qualifying_scope;
4861 /* Process the final unqualified-id. */
4862 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4863 check_dependency_p,
4864 declarator_p,
4865 /*optional_p=*/false);
4866 /* Restore the SAVED_SCOPE for our caller. */
4867 parser->scope = saved_scope;
4868 parser->object_scope = saved_object_scope;
4869 parser->qualifying_scope = saved_qualifying_scope;
4871 return unqualified_id;
4873 /* Otherwise, if we are in global scope, then we are looking at one
4874 of the other qualified-id productions. */
4875 else if (global_scope_p)
4877 cp_token *token;
4878 tree id;
4880 /* Peek at the next token. */
4881 token = cp_lexer_peek_token (parser->lexer);
4883 /* If it's an identifier, and the next token is not a "<", then
4884 we can avoid the template-id case. This is an optimization
4885 for this common case. */
4886 if (token->type == CPP_NAME
4887 && !cp_parser_nth_token_starts_template_argument_list_p
4888 (parser, 2))
4889 return cp_parser_identifier (parser);
4891 cp_parser_parse_tentatively (parser);
4892 /* Try a template-id. */
4893 id = cp_parser_template_id (parser,
4894 /*template_keyword_p=*/false,
4895 /*check_dependency_p=*/true,
4896 none_type,
4897 declarator_p);
4898 /* If that worked, we're done. */
4899 if (cp_parser_parse_definitely (parser))
4900 return id;
4902 /* Peek at the next token. (Changes in the token buffer may
4903 have invalidated the pointer obtained above.) */
4904 token = cp_lexer_peek_token (parser->lexer);
4906 switch (token->type)
4908 case CPP_NAME:
4909 return cp_parser_identifier (parser);
4911 case CPP_KEYWORD:
4912 if (token->keyword == RID_OPERATOR)
4913 return cp_parser_operator_function_id (parser);
4914 /* Fall through. */
4916 default:
4917 cp_parser_error (parser, "expected id-expression");
4918 return error_mark_node;
4921 else
4922 return cp_parser_unqualified_id (parser, template_keyword_p,
4923 /*check_dependency_p=*/true,
4924 declarator_p,
4925 optional_p);
4928 /* Parse an unqualified-id.
4930 unqualified-id:
4931 identifier
4932 operator-function-id
4933 conversion-function-id
4934 ~ class-name
4935 template-id
4937 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4938 keyword, in a construct like `A::template ...'.
4940 Returns a representation of unqualified-id. For the `identifier'
4941 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4942 production a BIT_NOT_EXPR is returned; the operand of the
4943 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4944 other productions, see the documentation accompanying the
4945 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4946 names are looked up in uninstantiated templates. If DECLARATOR_P
4947 is true, the unqualified-id is appearing as part of a declarator,
4948 rather than as part of an expression. */
4950 static tree
4951 cp_parser_unqualified_id (cp_parser* parser,
4952 bool template_keyword_p,
4953 bool check_dependency_p,
4954 bool declarator_p,
4955 bool optional_p)
4957 cp_token *token;
4959 /* Peek at the next token. */
4960 token = cp_lexer_peek_token (parser->lexer);
4962 switch ((int) token->type)
4964 case CPP_NAME:
4966 tree id;
4968 /* We don't know yet whether or not this will be a
4969 template-id. */
4970 cp_parser_parse_tentatively (parser);
4971 /* Try a template-id. */
4972 id = cp_parser_template_id (parser, template_keyword_p,
4973 check_dependency_p,
4974 none_type,
4975 declarator_p);
4976 /* If it worked, we're done. */
4977 if (cp_parser_parse_definitely (parser))
4978 return id;
4979 /* Otherwise, it's an ordinary identifier. */
4980 return cp_parser_identifier (parser);
4983 case CPP_TEMPLATE_ID:
4984 return cp_parser_template_id (parser, template_keyword_p,
4985 check_dependency_p,
4986 none_type,
4987 declarator_p);
4989 case CPP_COMPL:
4991 tree type_decl;
4992 tree qualifying_scope;
4993 tree object_scope;
4994 tree scope;
4995 bool done;
4997 /* Consume the `~' token. */
4998 cp_lexer_consume_token (parser->lexer);
4999 /* Parse the class-name. The standard, as written, seems to
5000 say that:
5002 template <typename T> struct S { ~S (); };
5003 template <typename T> S<T>::~S() {}
5005 is invalid, since `~' must be followed by a class-name, but
5006 `S<T>' is dependent, and so not known to be a class.
5007 That's not right; we need to look in uninstantiated
5008 templates. A further complication arises from:
5010 template <typename T> void f(T t) {
5011 t.T::~T();
5014 Here, it is not possible to look up `T' in the scope of `T'
5015 itself. We must look in both the current scope, and the
5016 scope of the containing complete expression.
5018 Yet another issue is:
5020 struct S {
5021 int S;
5022 ~S();
5025 S::~S() {}
5027 The standard does not seem to say that the `S' in `~S'
5028 should refer to the type `S' and not the data member
5029 `S::S'. */
5031 /* DR 244 says that we look up the name after the "~" in the
5032 same scope as we looked up the qualifying name. That idea
5033 isn't fully worked out; it's more complicated than that. */
5034 scope = parser->scope;
5035 object_scope = parser->object_scope;
5036 qualifying_scope = parser->qualifying_scope;
5038 /* Check for invalid scopes. */
5039 if (scope == error_mark_node)
5041 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5042 cp_lexer_consume_token (parser->lexer);
5043 return error_mark_node;
5045 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5047 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5048 error_at (token->location,
5049 "scope %qT before %<~%> is not a class-name",
5050 scope);
5051 cp_parser_simulate_error (parser);
5052 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5053 cp_lexer_consume_token (parser->lexer);
5054 return error_mark_node;
5056 gcc_assert (!scope || TYPE_P (scope));
5058 /* If the name is of the form "X::~X" it's OK even if X is a
5059 typedef. */
5060 token = cp_lexer_peek_token (parser->lexer);
5061 if (scope
5062 && token->type == CPP_NAME
5063 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5064 != CPP_LESS)
5065 && (token->u.value == TYPE_IDENTIFIER (scope)
5066 || (CLASS_TYPE_P (scope)
5067 && constructor_name_p (token->u.value, scope))))
5069 cp_lexer_consume_token (parser->lexer);
5070 return build_nt (BIT_NOT_EXPR, scope);
5073 /* ~auto means the destructor of whatever the object is. */
5074 if (cp_parser_is_keyword (token, RID_AUTO))
5076 if (cxx_dialect < cxx14)
5077 pedwarn (input_location, 0,
5078 "%<~auto%> only available with "
5079 "-std=c++14 or -std=gnu++14");
5080 cp_lexer_consume_token (parser->lexer);
5081 return build_nt (BIT_NOT_EXPR, make_auto ());
5084 /* If there was an explicit qualification (S::~T), first look
5085 in the scope given by the qualification (i.e., S).
5087 Note: in the calls to cp_parser_class_name below we pass
5088 typename_type so that lookup finds the injected-class-name
5089 rather than the constructor. */
5090 done = false;
5091 type_decl = NULL_TREE;
5092 if (scope)
5094 cp_parser_parse_tentatively (parser);
5095 type_decl = cp_parser_class_name (parser,
5096 /*typename_keyword_p=*/false,
5097 /*template_keyword_p=*/false,
5098 typename_type,
5099 /*check_dependency=*/false,
5100 /*class_head_p=*/false,
5101 declarator_p);
5102 if (cp_parser_parse_definitely (parser))
5103 done = true;
5105 /* In "N::S::~S", look in "N" as well. */
5106 if (!done && scope && qualifying_scope)
5108 cp_parser_parse_tentatively (parser);
5109 parser->scope = qualifying_scope;
5110 parser->object_scope = NULL_TREE;
5111 parser->qualifying_scope = NULL_TREE;
5112 type_decl
5113 = cp_parser_class_name (parser,
5114 /*typename_keyword_p=*/false,
5115 /*template_keyword_p=*/false,
5116 typename_type,
5117 /*check_dependency=*/false,
5118 /*class_head_p=*/false,
5119 declarator_p);
5120 if (cp_parser_parse_definitely (parser))
5121 done = true;
5123 /* In "p->S::~T", look in the scope given by "*p" as well. */
5124 else if (!done && object_scope)
5126 cp_parser_parse_tentatively (parser);
5127 parser->scope = object_scope;
5128 parser->object_scope = NULL_TREE;
5129 parser->qualifying_scope = NULL_TREE;
5130 type_decl
5131 = cp_parser_class_name (parser,
5132 /*typename_keyword_p=*/false,
5133 /*template_keyword_p=*/false,
5134 typename_type,
5135 /*check_dependency=*/false,
5136 /*class_head_p=*/false,
5137 declarator_p);
5138 if (cp_parser_parse_definitely (parser))
5139 done = true;
5141 /* Look in the surrounding context. */
5142 if (!done)
5144 parser->scope = NULL_TREE;
5145 parser->object_scope = NULL_TREE;
5146 parser->qualifying_scope = NULL_TREE;
5147 if (processing_template_decl)
5148 cp_parser_parse_tentatively (parser);
5149 type_decl
5150 = cp_parser_class_name (parser,
5151 /*typename_keyword_p=*/false,
5152 /*template_keyword_p=*/false,
5153 typename_type,
5154 /*check_dependency=*/false,
5155 /*class_head_p=*/false,
5156 declarator_p);
5157 if (processing_template_decl
5158 && ! cp_parser_parse_definitely (parser))
5160 /* We couldn't find a type with this name, so just accept
5161 it and check for a match at instantiation time. */
5162 type_decl = cp_parser_identifier (parser);
5163 if (type_decl != error_mark_node)
5164 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5165 return type_decl;
5168 /* If an error occurred, assume that the name of the
5169 destructor is the same as the name of the qualifying
5170 class. That allows us to keep parsing after running
5171 into ill-formed destructor names. */
5172 if (type_decl == error_mark_node && scope)
5173 return build_nt (BIT_NOT_EXPR, scope);
5174 else if (type_decl == error_mark_node)
5175 return error_mark_node;
5177 /* Check that destructor name and scope match. */
5178 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5180 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5181 error_at (token->location,
5182 "declaration of %<~%T%> as member of %qT",
5183 type_decl, scope);
5184 cp_parser_simulate_error (parser);
5185 return error_mark_node;
5188 /* [class.dtor]
5190 A typedef-name that names a class shall not be used as the
5191 identifier in the declarator for a destructor declaration. */
5192 if (declarator_p
5193 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5194 && !DECL_SELF_REFERENCE_P (type_decl)
5195 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5196 error_at (token->location,
5197 "typedef-name %qD used as destructor declarator",
5198 type_decl);
5200 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5203 case CPP_KEYWORD:
5204 if (token->keyword == RID_OPERATOR)
5206 tree id;
5208 /* This could be a template-id, so we try that first. */
5209 cp_parser_parse_tentatively (parser);
5210 /* Try a template-id. */
5211 id = cp_parser_template_id (parser, template_keyword_p,
5212 /*check_dependency_p=*/true,
5213 none_type,
5214 declarator_p);
5215 /* If that worked, we're done. */
5216 if (cp_parser_parse_definitely (parser))
5217 return id;
5218 /* We still don't know whether we're looking at an
5219 operator-function-id or a conversion-function-id. */
5220 cp_parser_parse_tentatively (parser);
5221 /* Try an operator-function-id. */
5222 id = cp_parser_operator_function_id (parser);
5223 /* If that didn't work, try a conversion-function-id. */
5224 if (!cp_parser_parse_definitely (parser))
5225 id = cp_parser_conversion_function_id (parser);
5226 else if (UDLIT_OPER_P (id))
5228 /* 17.6.3.3.5 */
5229 const char *name = UDLIT_OP_SUFFIX (id);
5230 if (name[0] != '_' && !in_system_header_at (input_location)
5231 && declarator_p)
5232 warning (0, "literal operator suffixes not preceded by %<_%>"
5233 " are reserved for future standardization");
5236 return id;
5238 /* Fall through. */
5240 default:
5241 if (optional_p)
5242 return NULL_TREE;
5243 cp_parser_error (parser, "expected unqualified-id");
5244 return error_mark_node;
5248 /* Parse an (optional) nested-name-specifier.
5250 nested-name-specifier: [C++98]
5251 class-or-namespace-name :: nested-name-specifier [opt]
5252 class-or-namespace-name :: template nested-name-specifier [opt]
5254 nested-name-specifier: [C++0x]
5255 type-name ::
5256 namespace-name ::
5257 nested-name-specifier identifier ::
5258 nested-name-specifier template [opt] simple-template-id ::
5260 PARSER->SCOPE should be set appropriately before this function is
5261 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5262 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5263 in name lookups.
5265 Sets PARSER->SCOPE to the class (TYPE) or namespace
5266 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5267 it unchanged if there is no nested-name-specifier. Returns the new
5268 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5270 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5271 part of a declaration and/or decl-specifier. */
5273 static tree
5274 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5275 bool typename_keyword_p,
5276 bool check_dependency_p,
5277 bool type_p,
5278 bool is_declaration)
5280 bool success = false;
5281 cp_token_position start = 0;
5282 cp_token *token;
5284 /* Remember where the nested-name-specifier starts. */
5285 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5287 start = cp_lexer_token_position (parser->lexer, false);
5288 push_deferring_access_checks (dk_deferred);
5291 while (true)
5293 tree new_scope;
5294 tree old_scope;
5295 tree saved_qualifying_scope;
5296 bool template_keyword_p;
5298 /* Spot cases that cannot be the beginning of a
5299 nested-name-specifier. */
5300 token = cp_lexer_peek_token (parser->lexer);
5302 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5303 the already parsed nested-name-specifier. */
5304 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5306 /* Grab the nested-name-specifier and continue the loop. */
5307 cp_parser_pre_parsed_nested_name_specifier (parser);
5308 /* If we originally encountered this nested-name-specifier
5309 with IS_DECLARATION set to false, we will not have
5310 resolved TYPENAME_TYPEs, so we must do so here. */
5311 if (is_declaration
5312 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5314 new_scope = resolve_typename_type (parser->scope,
5315 /*only_current_p=*/false);
5316 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5317 parser->scope = new_scope;
5319 success = true;
5320 continue;
5323 /* Spot cases that cannot be the beginning of a
5324 nested-name-specifier. On the second and subsequent times
5325 through the loop, we look for the `template' keyword. */
5326 if (success && token->keyword == RID_TEMPLATE)
5328 /* A template-id can start a nested-name-specifier. */
5329 else if (token->type == CPP_TEMPLATE_ID)
5331 /* DR 743: decltype can be used in a nested-name-specifier. */
5332 else if (token_is_decltype (token))
5334 else
5336 /* If the next token is not an identifier, then it is
5337 definitely not a type-name or namespace-name. */
5338 if (token->type != CPP_NAME)
5339 break;
5340 /* If the following token is neither a `<' (to begin a
5341 template-id), nor a `::', then we are not looking at a
5342 nested-name-specifier. */
5343 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5345 if (token->type == CPP_COLON
5346 && parser->colon_corrects_to_scope_p
5347 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5349 error_at (token->location,
5350 "found %<:%> in nested-name-specifier, expected %<::%>");
5351 token->type = CPP_SCOPE;
5354 if (token->type != CPP_SCOPE
5355 && !cp_parser_nth_token_starts_template_argument_list_p
5356 (parser, 2))
5357 break;
5360 /* The nested-name-specifier is optional, so we parse
5361 tentatively. */
5362 cp_parser_parse_tentatively (parser);
5364 /* Look for the optional `template' keyword, if this isn't the
5365 first time through the loop. */
5366 if (success)
5367 template_keyword_p = cp_parser_optional_template_keyword (parser);
5368 else
5369 template_keyword_p = false;
5371 /* Save the old scope since the name lookup we are about to do
5372 might destroy it. */
5373 old_scope = parser->scope;
5374 saved_qualifying_scope = parser->qualifying_scope;
5375 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5376 look up names in "X<T>::I" in order to determine that "Y" is
5377 a template. So, if we have a typename at this point, we make
5378 an effort to look through it. */
5379 if (is_declaration
5380 && !typename_keyword_p
5381 && parser->scope
5382 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5383 parser->scope = resolve_typename_type (parser->scope,
5384 /*only_current_p=*/false);
5385 /* Parse the qualifying entity. */
5386 new_scope
5387 = cp_parser_qualifying_entity (parser,
5388 typename_keyword_p,
5389 template_keyword_p,
5390 check_dependency_p,
5391 type_p,
5392 is_declaration);
5393 /* Look for the `::' token. */
5394 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5396 /* If we found what we wanted, we keep going; otherwise, we're
5397 done. */
5398 if (!cp_parser_parse_definitely (parser))
5400 bool error_p = false;
5402 /* Restore the OLD_SCOPE since it was valid before the
5403 failed attempt at finding the last
5404 class-or-namespace-name. */
5405 parser->scope = old_scope;
5406 parser->qualifying_scope = saved_qualifying_scope;
5408 /* If the next token is a decltype, and the one after that is a
5409 `::', then the decltype has failed to resolve to a class or
5410 enumeration type. Give this error even when parsing
5411 tentatively since it can't possibly be valid--and we're going
5412 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5413 won't get another chance.*/
5414 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5415 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5416 == CPP_SCOPE))
5418 token = cp_lexer_consume_token (parser->lexer);
5419 error_at (token->location, "decltype evaluates to %qT, "
5420 "which is not a class or enumeration type",
5421 token->u.value);
5422 parser->scope = error_mark_node;
5423 error_p = true;
5424 /* As below. */
5425 success = true;
5426 cp_lexer_consume_token (parser->lexer);
5429 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5430 break;
5431 /* If the next token is an identifier, and the one after
5432 that is a `::', then any valid interpretation would have
5433 found a class-or-namespace-name. */
5434 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5435 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5436 == CPP_SCOPE)
5437 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5438 != CPP_COMPL))
5440 token = cp_lexer_consume_token (parser->lexer);
5441 if (!error_p)
5443 if (!token->error_reported)
5445 tree decl;
5446 tree ambiguous_decls;
5448 decl = cp_parser_lookup_name (parser, token->u.value,
5449 none_type,
5450 /*is_template=*/false,
5451 /*is_namespace=*/false,
5452 /*check_dependency=*/true,
5453 &ambiguous_decls,
5454 token->location);
5455 if (TREE_CODE (decl) == TEMPLATE_DECL)
5456 error_at (token->location,
5457 "%qD used without template parameters",
5458 decl);
5459 else if (ambiguous_decls)
5461 // cp_parser_lookup_name has the same diagnostic,
5462 // thus make sure to emit it at most once.
5463 if (cp_parser_uncommitted_to_tentative_parse_p
5464 (parser))
5466 error_at (token->location,
5467 "reference to %qD is ambiguous",
5468 token->u.value);
5469 print_candidates (ambiguous_decls);
5471 decl = error_mark_node;
5473 else
5475 if (cxx_dialect != cxx98)
5476 cp_parser_name_lookup_error
5477 (parser, token->u.value, decl, NLE_NOT_CXX98,
5478 token->location);
5479 else
5480 cp_parser_name_lookup_error
5481 (parser, token->u.value, decl, NLE_CXX98,
5482 token->location);
5485 parser->scope = error_mark_node;
5486 error_p = true;
5487 /* Treat this as a successful nested-name-specifier
5488 due to:
5490 [basic.lookup.qual]
5492 If the name found is not a class-name (clause
5493 _class_) or namespace-name (_namespace.def_), the
5494 program is ill-formed. */
5495 success = true;
5497 cp_lexer_consume_token (parser->lexer);
5499 break;
5501 /* We've found one valid nested-name-specifier. */
5502 success = true;
5503 /* Name lookup always gives us a DECL. */
5504 if (TREE_CODE (new_scope) == TYPE_DECL)
5505 new_scope = TREE_TYPE (new_scope);
5506 /* Uses of "template" must be followed by actual templates. */
5507 if (template_keyword_p
5508 && !(CLASS_TYPE_P (new_scope)
5509 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5510 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5511 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5512 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5513 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5514 == TEMPLATE_ID_EXPR)))
5515 permerror (input_location, TYPE_P (new_scope)
5516 ? G_("%qT is not a template")
5517 : G_("%qD is not a template"),
5518 new_scope);
5519 /* If it is a class scope, try to complete it; we are about to
5520 be looking up names inside the class. */
5521 if (TYPE_P (new_scope)
5522 /* Since checking types for dependency can be expensive,
5523 avoid doing it if the type is already complete. */
5524 && !COMPLETE_TYPE_P (new_scope)
5525 /* Do not try to complete dependent types. */
5526 && !dependent_type_p (new_scope))
5528 new_scope = complete_type (new_scope);
5529 /* If it is a typedef to current class, use the current
5530 class instead, as the typedef won't have any names inside
5531 it yet. */
5532 if (!COMPLETE_TYPE_P (new_scope)
5533 && currently_open_class (new_scope))
5534 new_scope = TYPE_MAIN_VARIANT (new_scope);
5536 /* Make sure we look in the right scope the next time through
5537 the loop. */
5538 parser->scope = new_scope;
5541 /* If parsing tentatively, replace the sequence of tokens that makes
5542 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5543 token. That way, should we re-parse the token stream, we will
5544 not have to repeat the effort required to do the parse, nor will
5545 we issue duplicate error messages. */
5546 if (success && start)
5548 cp_token *token;
5550 token = cp_lexer_token_at (parser->lexer, start);
5551 /* Reset the contents of the START token. */
5552 token->type = CPP_NESTED_NAME_SPECIFIER;
5553 /* Retrieve any deferred checks. Do not pop this access checks yet
5554 so the memory will not be reclaimed during token replacing below. */
5555 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5556 token->u.tree_check_value->value = parser->scope;
5557 token->u.tree_check_value->checks = get_deferred_access_checks ();
5558 token->u.tree_check_value->qualifying_scope =
5559 parser->qualifying_scope;
5560 token->keyword = RID_MAX;
5562 /* Purge all subsequent tokens. */
5563 cp_lexer_purge_tokens_after (parser->lexer, start);
5566 if (start)
5567 pop_to_parent_deferring_access_checks ();
5569 return success ? parser->scope : NULL_TREE;
5572 /* Parse a nested-name-specifier. See
5573 cp_parser_nested_name_specifier_opt for details. This function
5574 behaves identically, except that it will an issue an error if no
5575 nested-name-specifier is present. */
5577 static tree
5578 cp_parser_nested_name_specifier (cp_parser *parser,
5579 bool typename_keyword_p,
5580 bool check_dependency_p,
5581 bool type_p,
5582 bool is_declaration)
5584 tree scope;
5586 /* Look for the nested-name-specifier. */
5587 scope = cp_parser_nested_name_specifier_opt (parser,
5588 typename_keyword_p,
5589 check_dependency_p,
5590 type_p,
5591 is_declaration);
5592 /* If it was not present, issue an error message. */
5593 if (!scope)
5595 cp_parser_error (parser, "expected nested-name-specifier");
5596 parser->scope = NULL_TREE;
5599 return scope;
5602 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5603 this is either a class-name or a namespace-name (which corresponds
5604 to the class-or-namespace-name production in the grammar). For
5605 C++0x, it can also be a type-name that refers to an enumeration
5606 type or a simple-template-id.
5608 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5609 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5610 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5611 TYPE_P is TRUE iff the next name should be taken as a class-name,
5612 even the same name is declared to be another entity in the same
5613 scope.
5615 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5616 specified by the class-or-namespace-name. If neither is found the
5617 ERROR_MARK_NODE is returned. */
5619 static tree
5620 cp_parser_qualifying_entity (cp_parser *parser,
5621 bool typename_keyword_p,
5622 bool template_keyword_p,
5623 bool check_dependency_p,
5624 bool type_p,
5625 bool is_declaration)
5627 tree saved_scope;
5628 tree saved_qualifying_scope;
5629 tree saved_object_scope;
5630 tree scope;
5631 bool only_class_p;
5632 bool successful_parse_p;
5634 /* DR 743: decltype can appear in a nested-name-specifier. */
5635 if (cp_lexer_next_token_is_decltype (parser->lexer))
5637 scope = cp_parser_decltype (parser);
5638 if (TREE_CODE (scope) != ENUMERAL_TYPE
5639 && !MAYBE_CLASS_TYPE_P (scope))
5641 cp_parser_simulate_error (parser);
5642 return error_mark_node;
5644 if (TYPE_NAME (scope))
5645 scope = TYPE_NAME (scope);
5646 return scope;
5649 /* Before we try to parse the class-name, we must save away the
5650 current PARSER->SCOPE since cp_parser_class_name will destroy
5651 it. */
5652 saved_scope = parser->scope;
5653 saved_qualifying_scope = parser->qualifying_scope;
5654 saved_object_scope = parser->object_scope;
5655 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5656 there is no need to look for a namespace-name. */
5657 only_class_p = template_keyword_p
5658 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5659 if (!only_class_p)
5660 cp_parser_parse_tentatively (parser);
5661 scope = cp_parser_class_name (parser,
5662 typename_keyword_p,
5663 template_keyword_p,
5664 type_p ? class_type : none_type,
5665 check_dependency_p,
5666 /*class_head_p=*/false,
5667 is_declaration);
5668 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5669 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5670 if (!only_class_p
5671 && cxx_dialect != cxx98
5672 && !successful_parse_p)
5674 /* Restore the saved scope. */
5675 parser->scope = saved_scope;
5676 parser->qualifying_scope = saved_qualifying_scope;
5677 parser->object_scope = saved_object_scope;
5679 /* Parse tentatively. */
5680 cp_parser_parse_tentatively (parser);
5682 /* Parse a type-name */
5683 scope = cp_parser_type_name (parser);
5685 /* "If the name found does not designate a namespace or a class,
5686 enumeration, or dependent type, the program is ill-formed."
5688 We cover classes and dependent types above and namespaces below,
5689 so this code is only looking for enums. */
5690 if (!scope || TREE_CODE (scope) != TYPE_DECL
5691 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5692 cp_parser_simulate_error (parser);
5694 successful_parse_p = cp_parser_parse_definitely (parser);
5696 /* If that didn't work, try for a namespace-name. */
5697 if (!only_class_p && !successful_parse_p)
5699 /* Restore the saved scope. */
5700 parser->scope = saved_scope;
5701 parser->qualifying_scope = saved_qualifying_scope;
5702 parser->object_scope = saved_object_scope;
5703 /* If we are not looking at an identifier followed by the scope
5704 resolution operator, then this is not part of a
5705 nested-name-specifier. (Note that this function is only used
5706 to parse the components of a nested-name-specifier.) */
5707 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5708 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5709 return error_mark_node;
5710 scope = cp_parser_namespace_name (parser);
5713 return scope;
5716 /* Return true if we are looking at a compound-literal, false otherwise. */
5718 static bool
5719 cp_parser_compound_literal_p (cp_parser *parser)
5721 /* Consume the `('. */
5722 cp_lexer_consume_token (parser->lexer);
5724 cp_lexer_save_tokens (parser->lexer);
5726 /* Skip tokens until the next token is a closing parenthesis.
5727 If we find the closing `)', and the next token is a `{', then
5728 we are looking at a compound-literal. */
5729 bool compound_literal_p
5730 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5731 /*consume_paren=*/true)
5732 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5734 /* Roll back the tokens we skipped. */
5735 cp_lexer_rollback_tokens (parser->lexer);
5737 return compound_literal_p;
5740 /* Parse a postfix-expression.
5742 postfix-expression:
5743 primary-expression
5744 postfix-expression [ expression ]
5745 postfix-expression ( expression-list [opt] )
5746 simple-type-specifier ( expression-list [opt] )
5747 typename :: [opt] nested-name-specifier identifier
5748 ( expression-list [opt] )
5749 typename :: [opt] nested-name-specifier template [opt] template-id
5750 ( expression-list [opt] )
5751 postfix-expression . template [opt] id-expression
5752 postfix-expression -> template [opt] id-expression
5753 postfix-expression . pseudo-destructor-name
5754 postfix-expression -> pseudo-destructor-name
5755 postfix-expression ++
5756 postfix-expression --
5757 dynamic_cast < type-id > ( expression )
5758 static_cast < type-id > ( expression )
5759 reinterpret_cast < type-id > ( expression )
5760 const_cast < type-id > ( expression )
5761 typeid ( expression )
5762 typeid ( type-id )
5764 GNU Extension:
5766 postfix-expression:
5767 ( type-id ) { initializer-list , [opt] }
5769 This extension is a GNU version of the C99 compound-literal
5770 construct. (The C99 grammar uses `type-name' instead of `type-id',
5771 but they are essentially the same concept.)
5773 If ADDRESS_P is true, the postfix expression is the operand of the
5774 `&' operator. CAST_P is true if this expression is the target of a
5775 cast.
5777 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5778 class member access expressions [expr.ref].
5780 Returns a representation of the expression. */
5782 static tree
5783 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5784 bool member_access_only_p, bool decltype_p,
5785 cp_id_kind * pidk_return)
5787 cp_token *token;
5788 location_t loc;
5789 enum rid keyword;
5790 cp_id_kind idk = CP_ID_KIND_NONE;
5791 tree postfix_expression = NULL_TREE;
5792 bool is_member_access = false;
5793 int saved_in_statement = -1;
5795 /* Peek at the next token. */
5796 token = cp_lexer_peek_token (parser->lexer);
5797 loc = token->location;
5798 /* Some of the productions are determined by keywords. */
5799 keyword = token->keyword;
5800 switch (keyword)
5802 case RID_DYNCAST:
5803 case RID_STATCAST:
5804 case RID_REINTCAST:
5805 case RID_CONSTCAST:
5807 tree type;
5808 tree expression;
5809 const char *saved_message;
5810 bool saved_in_type_id_in_expr_p;
5812 /* All of these can be handled in the same way from the point
5813 of view of parsing. Begin by consuming the token
5814 identifying the cast. */
5815 cp_lexer_consume_token (parser->lexer);
5817 /* New types cannot be defined in the cast. */
5818 saved_message = parser->type_definition_forbidden_message;
5819 parser->type_definition_forbidden_message
5820 = G_("types may not be defined in casts");
5822 /* Look for the opening `<'. */
5823 cp_parser_require (parser, CPP_LESS, RT_LESS);
5824 /* Parse the type to which we are casting. */
5825 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5826 parser->in_type_id_in_expr_p = true;
5827 type = cp_parser_type_id (parser);
5828 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5829 /* Look for the closing `>'. */
5830 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5831 /* Restore the old message. */
5832 parser->type_definition_forbidden_message = saved_message;
5834 bool saved_greater_than_is_operator_p
5835 = parser->greater_than_is_operator_p;
5836 parser->greater_than_is_operator_p = true;
5838 /* And the expression which is being cast. */
5839 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5840 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5841 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5843 parser->greater_than_is_operator_p
5844 = saved_greater_than_is_operator_p;
5846 /* Only type conversions to integral or enumeration types
5847 can be used in constant-expressions. */
5848 if (!cast_valid_in_integral_constant_expression_p (type)
5849 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5850 return error_mark_node;
5852 switch (keyword)
5854 case RID_DYNCAST:
5855 postfix_expression
5856 = build_dynamic_cast (type, expression, tf_warning_or_error);
5857 break;
5858 case RID_STATCAST:
5859 postfix_expression
5860 = build_static_cast (type, expression, tf_warning_or_error);
5861 break;
5862 case RID_REINTCAST:
5863 postfix_expression
5864 = build_reinterpret_cast (type, expression,
5865 tf_warning_or_error);
5866 break;
5867 case RID_CONSTCAST:
5868 postfix_expression
5869 = build_const_cast (type, expression, tf_warning_or_error);
5870 break;
5871 default:
5872 gcc_unreachable ();
5875 break;
5877 case RID_TYPEID:
5879 tree type;
5880 const char *saved_message;
5881 bool saved_in_type_id_in_expr_p;
5883 /* Consume the `typeid' token. */
5884 cp_lexer_consume_token (parser->lexer);
5885 /* Look for the `(' token. */
5886 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5887 /* Types cannot be defined in a `typeid' expression. */
5888 saved_message = parser->type_definition_forbidden_message;
5889 parser->type_definition_forbidden_message
5890 = G_("types may not be defined in a %<typeid%> expression");
5891 /* We can't be sure yet whether we're looking at a type-id or an
5892 expression. */
5893 cp_parser_parse_tentatively (parser);
5894 /* Try a type-id first. */
5895 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5896 parser->in_type_id_in_expr_p = true;
5897 type = cp_parser_type_id (parser);
5898 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5899 /* Look for the `)' token. Otherwise, we can't be sure that
5900 we're not looking at an expression: consider `typeid (int
5901 (3))', for example. */
5902 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5903 /* If all went well, simply lookup the type-id. */
5904 if (cp_parser_parse_definitely (parser))
5905 postfix_expression = get_typeid (type, tf_warning_or_error);
5906 /* Otherwise, fall back to the expression variant. */
5907 else
5909 tree expression;
5911 /* Look for an expression. */
5912 expression = cp_parser_expression (parser, & idk);
5913 /* Compute its typeid. */
5914 postfix_expression = build_typeid (expression, tf_warning_or_error);
5915 /* Look for the `)' token. */
5916 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5918 /* Restore the saved message. */
5919 parser->type_definition_forbidden_message = saved_message;
5920 /* `typeid' may not appear in an integral constant expression. */
5921 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5922 return error_mark_node;
5924 break;
5926 case RID_TYPENAME:
5928 tree type;
5929 /* The syntax permitted here is the same permitted for an
5930 elaborated-type-specifier. */
5931 type = cp_parser_elaborated_type_specifier (parser,
5932 /*is_friend=*/false,
5933 /*is_declaration=*/false);
5934 postfix_expression = cp_parser_functional_cast (parser, type);
5936 break;
5938 case RID_CILK_SPAWN:
5940 cp_lexer_consume_token (parser->lexer);
5941 token = cp_lexer_peek_token (parser->lexer);
5942 if (token->type == CPP_SEMICOLON)
5944 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5945 "an expression");
5946 postfix_expression = error_mark_node;
5947 break;
5949 else if (!current_function_decl)
5951 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5952 "inside a function");
5953 postfix_expression = error_mark_node;
5954 break;
5956 else
5958 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5959 saved_in_statement = parser->in_statement;
5960 parser->in_statement |= IN_CILK_SPAWN;
5962 cfun->calls_cilk_spawn = 1;
5963 postfix_expression =
5964 cp_parser_postfix_expression (parser, false, false,
5965 false, false, &idk);
5966 if (!flag_cilkplus)
5968 error_at (token->location, "-fcilkplus must be enabled to use"
5969 " %<_Cilk_spawn%>");
5970 cfun->calls_cilk_spawn = 0;
5972 else if (saved_in_statement & IN_CILK_SPAWN)
5974 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5975 "are not permitted");
5976 postfix_expression = error_mark_node;
5977 cfun->calls_cilk_spawn = 0;
5979 else
5981 postfix_expression = build_cilk_spawn (token->location,
5982 postfix_expression);
5983 if (postfix_expression != error_mark_node)
5984 SET_EXPR_LOCATION (postfix_expression, input_location);
5985 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5987 break;
5990 case RID_BUILTIN_SHUFFLE:
5992 vec<tree, va_gc> *vec;
5993 unsigned int i;
5994 tree p;
5996 cp_lexer_consume_token (parser->lexer);
5997 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5998 /*cast_p=*/false, /*allow_expansion_p=*/true,
5999 /*non_constant_p=*/NULL);
6000 if (vec == NULL)
6001 return error_mark_node;
6003 FOR_EACH_VEC_ELT (*vec, i, p)
6004 mark_exp_read (p);
6006 if (vec->length () == 2)
6007 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6008 tf_warning_or_error);
6009 else if (vec->length () == 3)
6010 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6011 tf_warning_or_error);
6012 else
6014 error_at (loc, "wrong number of arguments to "
6015 "%<__builtin_shuffle%>");
6016 return error_mark_node;
6018 break;
6021 default:
6023 tree type;
6025 /* If the next thing is a simple-type-specifier, we may be
6026 looking at a functional cast. We could also be looking at
6027 an id-expression. So, we try the functional cast, and if
6028 that doesn't work we fall back to the primary-expression. */
6029 cp_parser_parse_tentatively (parser);
6030 /* Look for the simple-type-specifier. */
6031 type = cp_parser_simple_type_specifier (parser,
6032 /*decl_specs=*/NULL,
6033 CP_PARSER_FLAGS_NONE);
6034 /* Parse the cast itself. */
6035 if (!cp_parser_error_occurred (parser))
6036 postfix_expression
6037 = cp_parser_functional_cast (parser, type);
6038 /* If that worked, we're done. */
6039 if (cp_parser_parse_definitely (parser))
6040 break;
6042 /* If the functional-cast didn't work out, try a
6043 compound-literal. */
6044 if (cp_parser_allow_gnu_extensions_p (parser)
6045 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6047 tree initializer = NULL_TREE;
6049 cp_parser_parse_tentatively (parser);
6051 /* Avoid calling cp_parser_type_id pointlessly, see comment
6052 in cp_parser_cast_expression about c++/29234. */
6053 if (!cp_parser_compound_literal_p (parser))
6054 cp_parser_simulate_error (parser);
6055 else
6057 /* Parse the type. */
6058 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6059 parser->in_type_id_in_expr_p = true;
6060 type = cp_parser_type_id (parser);
6061 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6062 /* Look for the `)'. */
6063 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6066 /* If things aren't going well, there's no need to
6067 keep going. */
6068 if (!cp_parser_error_occurred (parser))
6070 bool non_constant_p;
6071 /* Parse the brace-enclosed initializer list. */
6072 initializer = cp_parser_braced_list (parser,
6073 &non_constant_p);
6075 /* If that worked, we're definitely looking at a
6076 compound-literal expression. */
6077 if (cp_parser_parse_definitely (parser))
6079 /* Warn the user that a compound literal is not
6080 allowed in standard C++. */
6081 pedwarn (input_location, OPT_Wpedantic,
6082 "ISO C++ forbids compound-literals");
6083 /* For simplicity, we disallow compound literals in
6084 constant-expressions. We could
6085 allow compound literals of integer type, whose
6086 initializer was a constant, in constant
6087 expressions. Permitting that usage, as a further
6088 extension, would not change the meaning of any
6089 currently accepted programs. (Of course, as
6090 compound literals are not part of ISO C++, the
6091 standard has nothing to say.) */
6092 if (cp_parser_non_integral_constant_expression (parser,
6093 NIC_NCC))
6095 postfix_expression = error_mark_node;
6096 break;
6098 /* Form the representation of the compound-literal. */
6099 postfix_expression
6100 = finish_compound_literal (type, initializer,
6101 tf_warning_or_error);
6102 break;
6106 /* It must be a primary-expression. */
6107 postfix_expression
6108 = cp_parser_primary_expression (parser, address_p, cast_p,
6109 /*template_arg_p=*/false,
6110 decltype_p,
6111 &idk);
6113 break;
6116 /* Note that we don't need to worry about calling build_cplus_new on a
6117 class-valued CALL_EXPR in decltype when it isn't the end of the
6118 postfix-expression; unary_complex_lvalue will take care of that for
6119 all these cases. */
6121 /* Keep looping until the postfix-expression is complete. */
6122 while (true)
6124 if (idk == CP_ID_KIND_UNQUALIFIED
6125 && identifier_p (postfix_expression)
6126 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6127 /* It is not a Koenig lookup function call. */
6128 postfix_expression
6129 = unqualified_name_lookup_error (postfix_expression);
6131 /* Peek at the next token. */
6132 token = cp_lexer_peek_token (parser->lexer);
6134 switch (token->type)
6136 case CPP_OPEN_SQUARE:
6137 if (cp_next_tokens_can_be_std_attribute_p (parser))
6139 cp_parser_error (parser,
6140 "two consecutive %<[%> shall "
6141 "only introduce an attribute");
6142 return error_mark_node;
6144 postfix_expression
6145 = cp_parser_postfix_open_square_expression (parser,
6146 postfix_expression,
6147 false,
6148 decltype_p);
6149 idk = CP_ID_KIND_NONE;
6150 is_member_access = false;
6151 break;
6153 case CPP_OPEN_PAREN:
6154 /* postfix-expression ( expression-list [opt] ) */
6156 bool koenig_p;
6157 bool is_builtin_constant_p;
6158 bool saved_integral_constant_expression_p = false;
6159 bool saved_non_integral_constant_expression_p = false;
6160 tsubst_flags_t complain = complain_flags (decltype_p);
6161 vec<tree, va_gc> *args;
6163 is_member_access = false;
6165 is_builtin_constant_p
6166 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6167 if (is_builtin_constant_p)
6169 /* The whole point of __builtin_constant_p is to allow
6170 non-constant expressions to appear as arguments. */
6171 saved_integral_constant_expression_p
6172 = parser->integral_constant_expression_p;
6173 saved_non_integral_constant_expression_p
6174 = parser->non_integral_constant_expression_p;
6175 parser->integral_constant_expression_p = false;
6177 args = (cp_parser_parenthesized_expression_list
6178 (parser, non_attr,
6179 /*cast_p=*/false, /*allow_expansion_p=*/true,
6180 /*non_constant_p=*/NULL,
6181 /*want_literal_zero_p=*/warn_memset_transposed_args));
6182 if (is_builtin_constant_p)
6184 parser->integral_constant_expression_p
6185 = saved_integral_constant_expression_p;
6186 parser->non_integral_constant_expression_p
6187 = saved_non_integral_constant_expression_p;
6190 if (args == NULL)
6192 postfix_expression = error_mark_node;
6193 break;
6196 /* Function calls are not permitted in
6197 constant-expressions. */
6198 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6199 && cp_parser_non_integral_constant_expression (parser,
6200 NIC_FUNC_CALL))
6202 postfix_expression = error_mark_node;
6203 release_tree_vector (args);
6204 break;
6207 koenig_p = false;
6208 if (idk == CP_ID_KIND_UNQUALIFIED
6209 || idk == CP_ID_KIND_TEMPLATE_ID)
6211 if (identifier_p (postfix_expression))
6213 if (!args->is_empty ())
6215 koenig_p = true;
6216 if (!any_type_dependent_arguments_p (args))
6217 postfix_expression
6218 = perform_koenig_lookup (postfix_expression, args,
6219 complain);
6221 else
6222 postfix_expression
6223 = unqualified_fn_lookup_error (postfix_expression);
6225 /* We do not perform argument-dependent lookup if
6226 normal lookup finds a non-function, in accordance
6227 with the expected resolution of DR 218. */
6228 else if (!args->is_empty ()
6229 && is_overloaded_fn (postfix_expression))
6231 tree fn = get_first_fn (postfix_expression);
6232 fn = STRIP_TEMPLATE (fn);
6234 /* Do not do argument dependent lookup if regular
6235 lookup finds a member function or a block-scope
6236 function declaration. [basic.lookup.argdep]/3 */
6237 if (!DECL_FUNCTION_MEMBER_P (fn)
6238 && !DECL_LOCAL_FUNCTION_P (fn))
6240 koenig_p = true;
6241 if (!any_type_dependent_arguments_p (args))
6242 postfix_expression
6243 = perform_koenig_lookup (postfix_expression, args,
6244 complain);
6249 if (warn_memset_transposed_args)
6251 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6252 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6253 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6254 && vec_safe_length (args) == 3
6255 && integer_zerop ((*args)[2])
6256 && LITERAL_ZERO_P ((*args)[2])
6257 && !(integer_zerop ((*args)[1])
6258 && LITERAL_ZERO_P ((*args)[1])))
6259 warning (OPT_Wmemset_transposed_args,
6260 "%<memset%> used with constant zero length "
6261 "parameter; this could be due to transposed "
6262 "parameters");
6264 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6265 to avoid leaking those into folder and middle-end. */
6266 unsigned int i;
6267 tree arg;
6268 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6269 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6270 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6273 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6275 tree instance = TREE_OPERAND (postfix_expression, 0);
6276 tree fn = TREE_OPERAND (postfix_expression, 1);
6278 if (processing_template_decl
6279 && (type_dependent_expression_p (instance)
6280 || (!BASELINK_P (fn)
6281 && TREE_CODE (fn) != FIELD_DECL)
6282 || type_dependent_expression_p (fn)
6283 || any_type_dependent_arguments_p (args)))
6285 postfix_expression
6286 = build_nt_call_vec (postfix_expression, args);
6287 release_tree_vector (args);
6288 break;
6291 if (BASELINK_P (fn))
6293 postfix_expression
6294 = (build_new_method_call
6295 (instance, fn, &args, NULL_TREE,
6296 (idk == CP_ID_KIND_QUALIFIED
6297 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6298 : LOOKUP_NORMAL),
6299 /*fn_p=*/NULL,
6300 complain));
6302 else
6303 postfix_expression
6304 = finish_call_expr (postfix_expression, &args,
6305 /*disallow_virtual=*/false,
6306 /*koenig_p=*/false,
6307 complain);
6309 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6310 || TREE_CODE (postfix_expression) == MEMBER_REF
6311 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6312 postfix_expression = (build_offset_ref_call_from_tree
6313 (postfix_expression, &args,
6314 complain));
6315 else if (idk == CP_ID_KIND_QUALIFIED)
6316 /* A call to a static class member, or a namespace-scope
6317 function. */
6318 postfix_expression
6319 = finish_call_expr (postfix_expression, &args,
6320 /*disallow_virtual=*/true,
6321 koenig_p,
6322 complain);
6323 else
6324 /* All other function calls. */
6325 postfix_expression
6326 = finish_call_expr (postfix_expression, &args,
6327 /*disallow_virtual=*/false,
6328 koenig_p,
6329 complain);
6331 protected_set_expr_location (postfix_expression, token->location);
6333 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6334 idk = CP_ID_KIND_NONE;
6336 release_tree_vector (args);
6338 break;
6340 case CPP_DOT:
6341 case CPP_DEREF:
6342 /* postfix-expression . template [opt] id-expression
6343 postfix-expression . pseudo-destructor-name
6344 postfix-expression -> template [opt] id-expression
6345 postfix-expression -> pseudo-destructor-name */
6347 /* Consume the `.' or `->' operator. */
6348 cp_lexer_consume_token (parser->lexer);
6350 postfix_expression
6351 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6352 postfix_expression,
6353 false, &idk, loc);
6355 is_member_access = true;
6356 break;
6358 case CPP_PLUS_PLUS:
6359 /* postfix-expression ++ */
6360 /* Consume the `++' token. */
6361 cp_lexer_consume_token (parser->lexer);
6362 /* Generate a representation for the complete expression. */
6363 postfix_expression
6364 = finish_increment_expr (postfix_expression,
6365 POSTINCREMENT_EXPR);
6366 /* Increments may not appear in constant-expressions. */
6367 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6368 postfix_expression = error_mark_node;
6369 idk = CP_ID_KIND_NONE;
6370 is_member_access = false;
6371 break;
6373 case CPP_MINUS_MINUS:
6374 /* postfix-expression -- */
6375 /* Consume the `--' token. */
6376 cp_lexer_consume_token (parser->lexer);
6377 /* Generate a representation for the complete expression. */
6378 postfix_expression
6379 = finish_increment_expr (postfix_expression,
6380 POSTDECREMENT_EXPR);
6381 /* Decrements may not appear in constant-expressions. */
6382 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6383 postfix_expression = error_mark_node;
6384 idk = CP_ID_KIND_NONE;
6385 is_member_access = false;
6386 break;
6388 default:
6389 if (pidk_return != NULL)
6390 * pidk_return = idk;
6391 if (member_access_only_p)
6392 return is_member_access? postfix_expression : error_mark_node;
6393 else
6394 return postfix_expression;
6398 /* We should never get here. */
6399 gcc_unreachable ();
6400 return error_mark_node;
6403 /* This function parses Cilk Plus array notations. If a normal array expr. is
6404 parsed then the array index is passed back to the caller through *INIT_INDEX
6405 and the function returns a NULL_TREE. If array notation expr. is parsed,
6406 then *INIT_INDEX is ignored by the caller and the function returns
6407 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6408 error_mark_node. */
6410 static tree
6411 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6412 tree array_value)
6414 cp_token *token = NULL;
6415 tree length_index, stride = NULL_TREE, value_tree, array_type;
6416 if (!array_value || array_value == error_mark_node)
6418 cp_parser_skip_to_end_of_statement (parser);
6419 return error_mark_node;
6422 array_type = TREE_TYPE (array_value);
6424 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6425 parser->colon_corrects_to_scope_p = false;
6426 token = cp_lexer_peek_token (parser->lexer);
6428 if (!token)
6430 cp_parser_error (parser, "expected %<:%> or numeral");
6431 return error_mark_node;
6433 else if (token->type == CPP_COLON)
6435 /* Consume the ':'. */
6436 cp_lexer_consume_token (parser->lexer);
6438 /* If we are here, then we have a case like this A[:]. */
6439 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6441 cp_parser_error (parser, "expected %<]%>");
6442 cp_parser_skip_to_end_of_statement (parser);
6443 return error_mark_node;
6445 *init_index = NULL_TREE;
6446 stride = NULL_TREE;
6447 length_index = NULL_TREE;
6449 else
6451 /* If we are here, then there are three valid possibilities:
6452 1. ARRAY [ EXP ]
6453 2. ARRAY [ EXP : EXP ]
6454 3. ARRAY [ EXP : EXP : EXP ] */
6456 *init_index = cp_parser_expression (parser);
6457 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6459 /* This indicates that we have a normal array expression. */
6460 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6461 return NULL_TREE;
6464 /* Consume the ':'. */
6465 cp_lexer_consume_token (parser->lexer);
6466 length_index = cp_parser_expression (parser);
6467 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6469 cp_lexer_consume_token (parser->lexer);
6470 stride = cp_parser_expression (parser);
6473 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6475 if (*init_index == error_mark_node || length_index == error_mark_node
6476 || stride == error_mark_node || array_type == error_mark_node)
6478 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6479 cp_lexer_consume_token (parser->lexer);
6480 return error_mark_node;
6482 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6484 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6485 length_index, stride, array_type);
6486 return value_tree;
6489 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6490 by cp_parser_builtin_offsetof. We're looking for
6492 postfix-expression [ expression ]
6493 postfix-expression [ braced-init-list ] (C++11)
6495 FOR_OFFSETOF is set if we're being called in that context, which
6496 changes how we deal with integer constant expressions. */
6498 static tree
6499 cp_parser_postfix_open_square_expression (cp_parser *parser,
6500 tree postfix_expression,
6501 bool for_offsetof,
6502 bool decltype_p)
6504 tree index = NULL_TREE;
6505 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6506 bool saved_greater_than_is_operator_p;
6508 /* Consume the `[' token. */
6509 cp_lexer_consume_token (parser->lexer);
6511 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6512 parser->greater_than_is_operator_p = true;
6514 /* Parse the index expression. */
6515 /* ??? For offsetof, there is a question of what to allow here. If
6516 offsetof is not being used in an integral constant expression context,
6517 then we *could* get the right answer by computing the value at runtime.
6518 If we are in an integral constant expression context, then we might
6519 could accept any constant expression; hard to say without analysis.
6520 Rather than open the barn door too wide right away, allow only integer
6521 constant expressions here. */
6522 if (for_offsetof)
6523 index = cp_parser_constant_expression (parser);
6524 else
6526 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6528 bool expr_nonconst_p;
6529 cp_lexer_set_source_position (parser->lexer);
6530 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6531 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6532 if (flag_cilkplus
6533 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6535 error_at (cp_lexer_peek_token (parser->lexer)->location,
6536 "braced list index is not allowed with array "
6537 "notation");
6538 cp_parser_skip_to_end_of_statement (parser);
6539 return error_mark_node;
6542 else if (flag_cilkplus)
6544 /* Here are have these two options:
6545 ARRAY[EXP : EXP] - Array notation expr with default
6546 stride of 1.
6547 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6548 stride. */
6549 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6550 postfix_expression);
6551 if (an_exp)
6552 return an_exp;
6554 else
6555 index = cp_parser_expression (parser);
6558 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6560 /* Look for the closing `]'. */
6561 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6563 /* Build the ARRAY_REF. */
6564 postfix_expression = grok_array_decl (loc, postfix_expression,
6565 index, decltype_p);
6567 /* When not doing offsetof, array references are not permitted in
6568 constant-expressions. */
6569 if (!for_offsetof
6570 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6571 postfix_expression = error_mark_node;
6573 return postfix_expression;
6576 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6577 by cp_parser_builtin_offsetof. We're looking for
6579 postfix-expression . template [opt] id-expression
6580 postfix-expression . pseudo-destructor-name
6581 postfix-expression -> template [opt] id-expression
6582 postfix-expression -> pseudo-destructor-name
6584 FOR_OFFSETOF is set if we're being called in that context. That sorta
6585 limits what of the above we'll actually accept, but nevermind.
6586 TOKEN_TYPE is the "." or "->" token, which will already have been
6587 removed from the stream. */
6589 static tree
6590 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6591 enum cpp_ttype token_type,
6592 tree postfix_expression,
6593 bool for_offsetof, cp_id_kind *idk,
6594 location_t location)
6596 tree name;
6597 bool dependent_p;
6598 bool pseudo_destructor_p;
6599 tree scope = NULL_TREE;
6601 /* If this is a `->' operator, dereference the pointer. */
6602 if (token_type == CPP_DEREF)
6603 postfix_expression = build_x_arrow (location, postfix_expression,
6604 tf_warning_or_error);
6605 /* Check to see whether or not the expression is type-dependent. */
6606 dependent_p = type_dependent_expression_p (postfix_expression);
6607 /* The identifier following the `->' or `.' is not qualified. */
6608 parser->scope = NULL_TREE;
6609 parser->qualifying_scope = NULL_TREE;
6610 parser->object_scope = NULL_TREE;
6611 *idk = CP_ID_KIND_NONE;
6613 /* Enter the scope corresponding to the type of the object
6614 given by the POSTFIX_EXPRESSION. */
6615 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6617 scope = TREE_TYPE (postfix_expression);
6618 /* According to the standard, no expression should ever have
6619 reference type. Unfortunately, we do not currently match
6620 the standard in this respect in that our internal representation
6621 of an expression may have reference type even when the standard
6622 says it does not. Therefore, we have to manually obtain the
6623 underlying type here. */
6624 scope = non_reference (scope);
6625 /* The type of the POSTFIX_EXPRESSION must be complete. */
6626 if (scope == unknown_type_node)
6628 error_at (location, "%qE does not have class type",
6629 postfix_expression);
6630 scope = NULL_TREE;
6632 /* Unlike the object expression in other contexts, *this is not
6633 required to be of complete type for purposes of class member
6634 access (5.2.5) outside the member function body. */
6635 else if (postfix_expression != current_class_ref
6636 && !(processing_template_decl && scope == current_class_type))
6637 scope = complete_type_or_else (scope, NULL_TREE);
6638 /* Let the name lookup machinery know that we are processing a
6639 class member access expression. */
6640 parser->context->object_type = scope;
6641 /* If something went wrong, we want to be able to discern that case,
6642 as opposed to the case where there was no SCOPE due to the type
6643 of expression being dependent. */
6644 if (!scope)
6645 scope = error_mark_node;
6646 /* If the SCOPE was erroneous, make the various semantic analysis
6647 functions exit quickly -- and without issuing additional error
6648 messages. */
6649 if (scope == error_mark_node)
6650 postfix_expression = error_mark_node;
6653 /* Assume this expression is not a pseudo-destructor access. */
6654 pseudo_destructor_p = false;
6656 /* If the SCOPE is a scalar type, then, if this is a valid program,
6657 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6658 is type dependent, it can be pseudo-destructor-name or something else.
6659 Try to parse it as pseudo-destructor-name first. */
6660 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6662 tree s;
6663 tree type;
6665 cp_parser_parse_tentatively (parser);
6666 /* Parse the pseudo-destructor-name. */
6667 s = NULL_TREE;
6668 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6669 &s, &type);
6670 if (dependent_p
6671 && (cp_parser_error_occurred (parser)
6672 || !SCALAR_TYPE_P (type)))
6673 cp_parser_abort_tentative_parse (parser);
6674 else if (cp_parser_parse_definitely (parser))
6676 pseudo_destructor_p = true;
6677 postfix_expression
6678 = finish_pseudo_destructor_expr (postfix_expression,
6679 s, type, location);
6683 if (!pseudo_destructor_p)
6685 /* If the SCOPE is not a scalar type, we are looking at an
6686 ordinary class member access expression, rather than a
6687 pseudo-destructor-name. */
6688 bool template_p;
6689 cp_token *token = cp_lexer_peek_token (parser->lexer);
6690 /* Parse the id-expression. */
6691 name = (cp_parser_id_expression
6692 (parser,
6693 cp_parser_optional_template_keyword (parser),
6694 /*check_dependency_p=*/true,
6695 &template_p,
6696 /*declarator_p=*/false,
6697 /*optional_p=*/false));
6698 /* In general, build a SCOPE_REF if the member name is qualified.
6699 However, if the name was not dependent and has already been
6700 resolved; there is no need to build the SCOPE_REF. For example;
6702 struct X { void f(); };
6703 template <typename T> void f(T* t) { t->X::f(); }
6705 Even though "t" is dependent, "X::f" is not and has been resolved
6706 to a BASELINK; there is no need to include scope information. */
6708 /* But we do need to remember that there was an explicit scope for
6709 virtual function calls. */
6710 if (parser->scope)
6711 *idk = CP_ID_KIND_QUALIFIED;
6713 /* If the name is a template-id that names a type, we will get a
6714 TYPE_DECL here. That is invalid code. */
6715 if (TREE_CODE (name) == TYPE_DECL)
6717 error_at (token->location, "invalid use of %qD", name);
6718 postfix_expression = error_mark_node;
6720 else
6722 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6724 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6726 error_at (token->location, "%<%D::%D%> is not a class member",
6727 parser->scope, name);
6728 postfix_expression = error_mark_node;
6730 else
6731 name = build_qualified_name (/*type=*/NULL_TREE,
6732 parser->scope,
6733 name,
6734 template_p);
6735 parser->scope = NULL_TREE;
6736 parser->qualifying_scope = NULL_TREE;
6737 parser->object_scope = NULL_TREE;
6739 if (parser->scope && name && BASELINK_P (name))
6740 adjust_result_of_qualified_name_lookup
6741 (name, parser->scope, scope);
6742 postfix_expression
6743 = finish_class_member_access_expr (postfix_expression, name,
6744 template_p,
6745 tf_warning_or_error);
6749 /* We no longer need to look up names in the scope of the object on
6750 the left-hand side of the `.' or `->' operator. */
6751 parser->context->object_type = NULL_TREE;
6753 /* Outside of offsetof, these operators may not appear in
6754 constant-expressions. */
6755 if (!for_offsetof
6756 && (cp_parser_non_integral_constant_expression
6757 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6758 postfix_expression = error_mark_node;
6760 return postfix_expression;
6763 /* Cache of LITERAL_ZERO_P constants. */
6765 static GTY(()) tree literal_zeros[itk_none];
6767 /* Parse a parenthesized expression-list.
6769 expression-list:
6770 assignment-expression
6771 expression-list, assignment-expression
6773 attribute-list:
6774 expression-list
6775 identifier
6776 identifier, expression-list
6778 CAST_P is true if this expression is the target of a cast.
6780 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6781 argument pack.
6783 Returns a vector of trees. Each element is a representation of an
6784 assignment-expression. NULL is returned if the ( and or ) are
6785 missing. An empty, but allocated, vector is returned on no
6786 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6787 if we are parsing an attribute list for an attribute that wants a
6788 plain identifier argument, normal_attr for an attribute that wants
6789 an expression, or non_attr if we aren't parsing an attribute list. If
6790 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6791 not all of the expressions in the list were constant.
6792 WANT_LITERAL_ZERO_P is true if the caller is interested in
6793 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6794 immediately, this can be removed. */
6796 static vec<tree, va_gc> *
6797 cp_parser_parenthesized_expression_list (cp_parser* parser,
6798 int is_attribute_list,
6799 bool cast_p,
6800 bool allow_expansion_p,
6801 bool *non_constant_p,
6802 bool want_literal_zero_p)
6804 vec<tree, va_gc> *expression_list;
6805 bool fold_expr_p = is_attribute_list != non_attr;
6806 tree identifier = NULL_TREE;
6807 bool saved_greater_than_is_operator_p;
6809 /* Assume all the expressions will be constant. */
6810 if (non_constant_p)
6811 *non_constant_p = false;
6813 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6814 return NULL;
6816 expression_list = make_tree_vector ();
6818 /* Within a parenthesized expression, a `>' token is always
6819 the greater-than operator. */
6820 saved_greater_than_is_operator_p
6821 = parser->greater_than_is_operator_p;
6822 parser->greater_than_is_operator_p = true;
6824 /* Consume expressions until there are no more. */
6825 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6826 while (true)
6828 tree expr;
6830 /* At the beginning of attribute lists, check to see if the
6831 next token is an identifier. */
6832 if (is_attribute_list == id_attr
6833 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6835 cp_token *token;
6837 /* Consume the identifier. */
6838 token = cp_lexer_consume_token (parser->lexer);
6839 /* Save the identifier. */
6840 identifier = token->u.value;
6842 else
6844 bool expr_non_constant_p;
6846 /* Parse the next assignment-expression. */
6847 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6849 /* A braced-init-list. */
6850 cp_lexer_set_source_position (parser->lexer);
6851 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6852 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6853 if (non_constant_p && expr_non_constant_p)
6854 *non_constant_p = true;
6856 else if (non_constant_p)
6858 expr = (cp_parser_constant_expression
6859 (parser, /*allow_non_constant_p=*/true,
6860 &expr_non_constant_p));
6861 if (expr_non_constant_p)
6862 *non_constant_p = true;
6864 else
6866 expr = NULL_TREE;
6867 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6868 switch (tok->type)
6870 case CPP_NUMBER:
6871 case CPP_CHAR:
6872 case CPP_WCHAR:
6873 case CPP_CHAR16:
6874 case CPP_CHAR32:
6875 /* If a parameter is literal zero alone, remember it
6876 for -Wmemset-transposed-args warning. */
6877 if (integer_zerop (tok->u.value)
6878 && !TREE_OVERFLOW (tok->u.value)
6879 && want_literal_zero_p
6880 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6881 == CPP_COMMA
6882 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6883 == CPP_CLOSE_PAREN))
6885 unsigned int i;
6886 for (i = 0; i < itk_none; ++i)
6887 if (TREE_TYPE (tok->u.value) == integer_types[i])
6888 break;
6889 if (i < itk_none && literal_zeros[i])
6890 expr = literal_zeros[i];
6891 else
6893 expr = copy_node (tok->u.value);
6894 LITERAL_ZERO_P (expr) = 1;
6895 if (i < itk_none)
6896 literal_zeros[i] = expr;
6898 /* Consume the 0 token (or '\0', 0LL etc.). */
6899 cp_lexer_consume_token (parser->lexer);
6901 break;
6902 default:
6903 break;
6905 if (expr == NULL_TREE)
6906 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6907 cast_p);
6910 if (fold_expr_p)
6911 expr = fold_non_dependent_expr (expr);
6913 /* If we have an ellipsis, then this is an expression
6914 expansion. */
6915 if (allow_expansion_p
6916 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6918 /* Consume the `...'. */
6919 cp_lexer_consume_token (parser->lexer);
6921 /* Build the argument pack. */
6922 expr = make_pack_expansion (expr);
6925 /* Add it to the list. We add error_mark_node
6926 expressions to the list, so that we can still tell if
6927 the correct form for a parenthesized expression-list
6928 is found. That gives better errors. */
6929 vec_safe_push (expression_list, expr);
6931 if (expr == error_mark_node)
6932 goto skip_comma;
6935 /* After the first item, attribute lists look the same as
6936 expression lists. */
6937 is_attribute_list = non_attr;
6939 get_comma:;
6940 /* If the next token isn't a `,', then we are done. */
6941 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6942 break;
6944 /* Otherwise, consume the `,' and keep going. */
6945 cp_lexer_consume_token (parser->lexer);
6948 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6950 int ending;
6952 skip_comma:;
6953 /* We try and resync to an unnested comma, as that will give the
6954 user better diagnostics. */
6955 ending = cp_parser_skip_to_closing_parenthesis (parser,
6956 /*recovering=*/true,
6957 /*or_comma=*/true,
6958 /*consume_paren=*/true);
6959 if (ending < 0)
6960 goto get_comma;
6961 if (!ending)
6963 parser->greater_than_is_operator_p
6964 = saved_greater_than_is_operator_p;
6965 return NULL;
6969 parser->greater_than_is_operator_p
6970 = saved_greater_than_is_operator_p;
6972 if (identifier)
6973 vec_safe_insert (expression_list, 0, identifier);
6975 return expression_list;
6978 /* Parse a pseudo-destructor-name.
6980 pseudo-destructor-name:
6981 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6982 :: [opt] nested-name-specifier template template-id :: ~ type-name
6983 :: [opt] nested-name-specifier [opt] ~ type-name
6985 If either of the first two productions is used, sets *SCOPE to the
6986 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6987 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6988 or ERROR_MARK_NODE if the parse fails. */
6990 static void
6991 cp_parser_pseudo_destructor_name (cp_parser* parser,
6992 tree object,
6993 tree* scope,
6994 tree* type)
6996 bool nested_name_specifier_p;
6998 /* Handle ~auto. */
6999 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7000 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7001 && !type_dependent_expression_p (object))
7003 if (cxx_dialect < cxx14)
7004 pedwarn (input_location, 0,
7005 "%<~auto%> only available with "
7006 "-std=c++14 or -std=gnu++14");
7007 cp_lexer_consume_token (parser->lexer);
7008 cp_lexer_consume_token (parser->lexer);
7009 *scope = NULL_TREE;
7010 *type = TREE_TYPE (object);
7011 return;
7014 /* Assume that things will not work out. */
7015 *type = error_mark_node;
7017 /* Look for the optional `::' operator. */
7018 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7019 /* Look for the optional nested-name-specifier. */
7020 nested_name_specifier_p
7021 = (cp_parser_nested_name_specifier_opt (parser,
7022 /*typename_keyword_p=*/false,
7023 /*check_dependency_p=*/true,
7024 /*type_p=*/false,
7025 /*is_declaration=*/false)
7026 != NULL_TREE);
7027 /* Now, if we saw a nested-name-specifier, we might be doing the
7028 second production. */
7029 if (nested_name_specifier_p
7030 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7032 /* Consume the `template' keyword. */
7033 cp_lexer_consume_token (parser->lexer);
7034 /* Parse the template-id. */
7035 cp_parser_template_id (parser,
7036 /*template_keyword_p=*/true,
7037 /*check_dependency_p=*/false,
7038 class_type,
7039 /*is_declaration=*/true);
7040 /* Look for the `::' token. */
7041 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7043 /* If the next token is not a `~', then there might be some
7044 additional qualification. */
7045 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7047 /* At this point, we're looking for "type-name :: ~". The type-name
7048 must not be a class-name, since this is a pseudo-destructor. So,
7049 it must be either an enum-name, or a typedef-name -- both of which
7050 are just identifiers. So, we peek ahead to check that the "::"
7051 and "~" tokens are present; if they are not, then we can avoid
7052 calling type_name. */
7053 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7054 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7055 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7057 cp_parser_error (parser, "non-scalar type");
7058 return;
7061 /* Look for the type-name. */
7062 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7063 if (*scope == error_mark_node)
7064 return;
7066 /* Look for the `::' token. */
7067 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7069 else
7070 *scope = NULL_TREE;
7072 /* Look for the `~'. */
7073 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7075 /* Once we see the ~, this has to be a pseudo-destructor. */
7076 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7077 cp_parser_commit_to_topmost_tentative_parse (parser);
7079 /* Look for the type-name again. We are not responsible for
7080 checking that it matches the first type-name. */
7081 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7084 /* Parse a unary-expression.
7086 unary-expression:
7087 postfix-expression
7088 ++ cast-expression
7089 -- cast-expression
7090 unary-operator cast-expression
7091 sizeof unary-expression
7092 sizeof ( type-id )
7093 alignof ( type-id ) [C++0x]
7094 new-expression
7095 delete-expression
7097 GNU Extensions:
7099 unary-expression:
7100 __extension__ cast-expression
7101 __alignof__ unary-expression
7102 __alignof__ ( type-id )
7103 alignof unary-expression [C++0x]
7104 __real__ cast-expression
7105 __imag__ cast-expression
7106 && identifier
7107 sizeof ( type-id ) { initializer-list , [opt] }
7108 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7109 __alignof__ ( type-id ) { initializer-list , [opt] }
7111 ADDRESS_P is true iff the unary-expression is appearing as the
7112 operand of the `&' operator. CAST_P is true if this expression is
7113 the target of a cast.
7115 Returns a representation of the expression. */
7117 static tree
7118 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7119 bool address_p, bool cast_p, bool decltype_p)
7121 cp_token *token;
7122 enum tree_code unary_operator;
7124 /* Peek at the next token. */
7125 token = cp_lexer_peek_token (parser->lexer);
7126 /* Some keywords give away the kind of expression. */
7127 if (token->type == CPP_KEYWORD)
7129 enum rid keyword = token->keyword;
7131 switch (keyword)
7133 case RID_ALIGNOF:
7134 case RID_SIZEOF:
7136 tree operand, ret;
7137 enum tree_code op;
7138 location_t first_loc;
7140 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7141 /* Consume the token. */
7142 cp_lexer_consume_token (parser->lexer);
7143 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7144 /* Parse the operand. */
7145 operand = cp_parser_sizeof_operand (parser, keyword);
7147 if (TYPE_P (operand))
7148 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7149 else
7151 /* ISO C++ defines alignof only with types, not with
7152 expressions. So pedwarn if alignof is used with a non-
7153 type expression. However, __alignof__ is ok. */
7154 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7155 pedwarn (token->location, OPT_Wpedantic,
7156 "ISO C++ does not allow %<alignof%> "
7157 "with a non-type");
7159 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7161 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7162 SIZEOF_EXPR with the original operand. */
7163 if (op == SIZEOF_EXPR && ret != error_mark_node)
7165 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7167 if (!processing_template_decl && TYPE_P (operand))
7169 ret = build_min (SIZEOF_EXPR, size_type_node,
7170 build1 (NOP_EXPR, operand,
7171 error_mark_node));
7172 SIZEOF_EXPR_TYPE_P (ret) = 1;
7174 else
7175 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7176 TREE_SIDE_EFFECTS (ret) = 0;
7177 TREE_READONLY (ret) = 1;
7179 SET_EXPR_LOCATION (ret, first_loc);
7181 return ret;
7184 case RID_NEW:
7185 return cp_parser_new_expression (parser);
7187 case RID_DELETE:
7188 return cp_parser_delete_expression (parser);
7190 case RID_EXTENSION:
7192 /* The saved value of the PEDANTIC flag. */
7193 int saved_pedantic;
7194 tree expr;
7196 /* Save away the PEDANTIC flag. */
7197 cp_parser_extension_opt (parser, &saved_pedantic);
7198 /* Parse the cast-expression. */
7199 expr = cp_parser_simple_cast_expression (parser);
7200 /* Restore the PEDANTIC flag. */
7201 pedantic = saved_pedantic;
7203 return expr;
7206 case RID_REALPART:
7207 case RID_IMAGPART:
7209 tree expression;
7211 /* Consume the `__real__' or `__imag__' token. */
7212 cp_lexer_consume_token (parser->lexer);
7213 /* Parse the cast-expression. */
7214 expression = cp_parser_simple_cast_expression (parser);
7215 /* Create the complete representation. */
7216 return build_x_unary_op (token->location,
7217 (keyword == RID_REALPART
7218 ? REALPART_EXPR : IMAGPART_EXPR),
7219 expression,
7220 tf_warning_or_error);
7222 break;
7224 case RID_TRANSACTION_ATOMIC:
7225 case RID_TRANSACTION_RELAXED:
7226 return cp_parser_transaction_expression (parser, keyword);
7228 case RID_NOEXCEPT:
7230 tree expr;
7231 const char *saved_message;
7232 bool saved_integral_constant_expression_p;
7233 bool saved_non_integral_constant_expression_p;
7234 bool saved_greater_than_is_operator_p;
7236 cp_lexer_consume_token (parser->lexer);
7237 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7239 saved_message = parser->type_definition_forbidden_message;
7240 parser->type_definition_forbidden_message
7241 = G_("types may not be defined in %<noexcept%> expressions");
7243 saved_integral_constant_expression_p
7244 = parser->integral_constant_expression_p;
7245 saved_non_integral_constant_expression_p
7246 = parser->non_integral_constant_expression_p;
7247 parser->integral_constant_expression_p = false;
7249 saved_greater_than_is_operator_p
7250 = parser->greater_than_is_operator_p;
7251 parser->greater_than_is_operator_p = true;
7253 ++cp_unevaluated_operand;
7254 ++c_inhibit_evaluation_warnings;
7255 ++cp_noexcept_operand;
7256 expr = cp_parser_expression (parser);
7257 --cp_noexcept_operand;
7258 --c_inhibit_evaluation_warnings;
7259 --cp_unevaluated_operand;
7261 parser->greater_than_is_operator_p
7262 = saved_greater_than_is_operator_p;
7264 parser->integral_constant_expression_p
7265 = saved_integral_constant_expression_p;
7266 parser->non_integral_constant_expression_p
7267 = saved_non_integral_constant_expression_p;
7269 parser->type_definition_forbidden_message = saved_message;
7271 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7272 return finish_noexcept_expr (expr, tf_warning_or_error);
7275 default:
7276 break;
7280 /* Look for the `:: new' and `:: delete', which also signal the
7281 beginning of a new-expression, or delete-expression,
7282 respectively. If the next token is `::', then it might be one of
7283 these. */
7284 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7286 enum rid keyword;
7288 /* See if the token after the `::' is one of the keywords in
7289 which we're interested. */
7290 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7291 /* If it's `new', we have a new-expression. */
7292 if (keyword == RID_NEW)
7293 return cp_parser_new_expression (parser);
7294 /* Similarly, for `delete'. */
7295 else if (keyword == RID_DELETE)
7296 return cp_parser_delete_expression (parser);
7299 /* Look for a unary operator. */
7300 unary_operator = cp_parser_unary_operator (token);
7301 /* The `++' and `--' operators can be handled similarly, even though
7302 they are not technically unary-operators in the grammar. */
7303 if (unary_operator == ERROR_MARK)
7305 if (token->type == CPP_PLUS_PLUS)
7306 unary_operator = PREINCREMENT_EXPR;
7307 else if (token->type == CPP_MINUS_MINUS)
7308 unary_operator = PREDECREMENT_EXPR;
7309 /* Handle the GNU address-of-label extension. */
7310 else if (cp_parser_allow_gnu_extensions_p (parser)
7311 && token->type == CPP_AND_AND)
7313 tree identifier;
7314 tree expression;
7315 location_t loc = token->location;
7317 /* Consume the '&&' token. */
7318 cp_lexer_consume_token (parser->lexer);
7319 /* Look for the identifier. */
7320 identifier = cp_parser_identifier (parser);
7321 /* Create an expression representing the address. */
7322 expression = finish_label_address_expr (identifier, loc);
7323 if (cp_parser_non_integral_constant_expression (parser,
7324 NIC_ADDR_LABEL))
7325 expression = error_mark_node;
7326 return expression;
7329 if (unary_operator != ERROR_MARK)
7331 tree cast_expression;
7332 tree expression = error_mark_node;
7333 non_integral_constant non_constant_p = NIC_NONE;
7334 location_t loc = token->location;
7335 tsubst_flags_t complain = complain_flags (decltype_p);
7337 /* Consume the operator token. */
7338 token = cp_lexer_consume_token (parser->lexer);
7339 /* Parse the cast-expression. */
7340 cast_expression
7341 = cp_parser_cast_expression (parser,
7342 unary_operator == ADDR_EXPR,
7343 /*cast_p=*/false,
7344 /*decltype*/false,
7345 pidk);
7346 /* Now, build an appropriate representation. */
7347 switch (unary_operator)
7349 case INDIRECT_REF:
7350 non_constant_p = NIC_STAR;
7351 expression = build_x_indirect_ref (loc, cast_expression,
7352 RO_UNARY_STAR,
7353 complain);
7354 break;
7356 case ADDR_EXPR:
7357 non_constant_p = NIC_ADDR;
7358 /* Fall through. */
7359 case BIT_NOT_EXPR:
7360 expression = build_x_unary_op (loc, unary_operator,
7361 cast_expression,
7362 complain);
7363 break;
7365 case PREINCREMENT_EXPR:
7366 case PREDECREMENT_EXPR:
7367 non_constant_p = unary_operator == PREINCREMENT_EXPR
7368 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7369 /* Fall through. */
7370 case UNARY_PLUS_EXPR:
7371 case NEGATE_EXPR:
7372 case TRUTH_NOT_EXPR:
7373 expression = finish_unary_op_expr (loc, unary_operator,
7374 cast_expression, complain);
7375 break;
7377 default:
7378 gcc_unreachable ();
7381 if (non_constant_p != NIC_NONE
7382 && cp_parser_non_integral_constant_expression (parser,
7383 non_constant_p))
7384 expression = error_mark_node;
7386 return expression;
7389 return cp_parser_postfix_expression (parser, address_p, cast_p,
7390 /*member_access_only_p=*/false,
7391 decltype_p,
7392 pidk);
7395 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7396 unary-operator, the corresponding tree code is returned. */
7398 static enum tree_code
7399 cp_parser_unary_operator (cp_token* token)
7401 switch (token->type)
7403 case CPP_MULT:
7404 return INDIRECT_REF;
7406 case CPP_AND:
7407 return ADDR_EXPR;
7409 case CPP_PLUS:
7410 return UNARY_PLUS_EXPR;
7412 case CPP_MINUS:
7413 return NEGATE_EXPR;
7415 case CPP_NOT:
7416 return TRUTH_NOT_EXPR;
7418 case CPP_COMPL:
7419 return BIT_NOT_EXPR;
7421 default:
7422 return ERROR_MARK;
7426 /* Parse a new-expression.
7428 new-expression:
7429 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7430 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7432 Returns a representation of the expression. */
7434 static tree
7435 cp_parser_new_expression (cp_parser* parser)
7437 bool global_scope_p;
7438 vec<tree, va_gc> *placement;
7439 tree type;
7440 vec<tree, va_gc> *initializer;
7441 tree nelts = NULL_TREE;
7442 tree ret;
7444 /* Look for the optional `::' operator. */
7445 global_scope_p
7446 = (cp_parser_global_scope_opt (parser,
7447 /*current_scope_valid_p=*/false)
7448 != NULL_TREE);
7449 /* Look for the `new' operator. */
7450 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7451 /* There's no easy way to tell a new-placement from the
7452 `( type-id )' construct. */
7453 cp_parser_parse_tentatively (parser);
7454 /* Look for a new-placement. */
7455 placement = cp_parser_new_placement (parser);
7456 /* If that didn't work out, there's no new-placement. */
7457 if (!cp_parser_parse_definitely (parser))
7459 if (placement != NULL)
7460 release_tree_vector (placement);
7461 placement = NULL;
7464 /* If the next token is a `(', then we have a parenthesized
7465 type-id. */
7466 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7468 cp_token *token;
7469 const char *saved_message = parser->type_definition_forbidden_message;
7471 /* Consume the `('. */
7472 cp_lexer_consume_token (parser->lexer);
7474 /* Parse the type-id. */
7475 parser->type_definition_forbidden_message
7476 = G_("types may not be defined in a new-expression");
7477 type = cp_parser_type_id (parser);
7478 parser->type_definition_forbidden_message = saved_message;
7480 /* Look for the closing `)'. */
7481 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7482 token = cp_lexer_peek_token (parser->lexer);
7483 /* There should not be a direct-new-declarator in this production,
7484 but GCC used to allowed this, so we check and emit a sensible error
7485 message for this case. */
7486 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7488 error_at (token->location,
7489 "array bound forbidden after parenthesized type-id");
7490 inform (token->location,
7491 "try removing the parentheses around the type-id");
7492 cp_parser_direct_new_declarator (parser);
7495 /* Otherwise, there must be a new-type-id. */
7496 else
7497 type = cp_parser_new_type_id (parser, &nelts);
7499 /* If the next token is a `(' or '{', then we have a new-initializer. */
7500 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7501 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7502 initializer = cp_parser_new_initializer (parser);
7503 else
7504 initializer = NULL;
7506 /* A new-expression may not appear in an integral constant
7507 expression. */
7508 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7509 ret = error_mark_node;
7510 else
7512 /* Create a representation of the new-expression. */
7513 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7514 tf_warning_or_error);
7517 if (placement != NULL)
7518 release_tree_vector (placement);
7519 if (initializer != NULL)
7520 release_tree_vector (initializer);
7522 return ret;
7525 /* Parse a new-placement.
7527 new-placement:
7528 ( expression-list )
7530 Returns the same representation as for an expression-list. */
7532 static vec<tree, va_gc> *
7533 cp_parser_new_placement (cp_parser* parser)
7535 vec<tree, va_gc> *expression_list;
7537 /* Parse the expression-list. */
7538 expression_list = (cp_parser_parenthesized_expression_list
7539 (parser, non_attr, /*cast_p=*/false,
7540 /*allow_expansion_p=*/true,
7541 /*non_constant_p=*/NULL));
7543 return expression_list;
7546 /* Parse a new-type-id.
7548 new-type-id:
7549 type-specifier-seq new-declarator [opt]
7551 Returns the TYPE allocated. If the new-type-id indicates an array
7552 type, *NELTS is set to the number of elements in the last array
7553 bound; the TYPE will not include the last array bound. */
7555 static tree
7556 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7558 cp_decl_specifier_seq type_specifier_seq;
7559 cp_declarator *new_declarator;
7560 cp_declarator *declarator;
7561 cp_declarator *outer_declarator;
7562 const char *saved_message;
7564 /* The type-specifier sequence must not contain type definitions.
7565 (It cannot contain declarations of new types either, but if they
7566 are not definitions we will catch that because they are not
7567 complete.) */
7568 saved_message = parser->type_definition_forbidden_message;
7569 parser->type_definition_forbidden_message
7570 = G_("types may not be defined in a new-type-id");
7571 /* Parse the type-specifier-seq. */
7572 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7573 /*is_trailing_return=*/false,
7574 &type_specifier_seq);
7575 /* Restore the old message. */
7576 parser->type_definition_forbidden_message = saved_message;
7578 if (type_specifier_seq.type == error_mark_node)
7579 return error_mark_node;
7581 /* Parse the new-declarator. */
7582 new_declarator = cp_parser_new_declarator_opt (parser);
7584 /* Determine the number of elements in the last array dimension, if
7585 any. */
7586 *nelts = NULL_TREE;
7587 /* Skip down to the last array dimension. */
7588 declarator = new_declarator;
7589 outer_declarator = NULL;
7590 while (declarator && (declarator->kind == cdk_pointer
7591 || declarator->kind == cdk_ptrmem))
7593 outer_declarator = declarator;
7594 declarator = declarator->declarator;
7596 while (declarator
7597 && declarator->kind == cdk_array
7598 && declarator->declarator
7599 && declarator->declarator->kind == cdk_array)
7601 outer_declarator = declarator;
7602 declarator = declarator->declarator;
7605 if (declarator && declarator->kind == cdk_array)
7607 *nelts = declarator->u.array.bounds;
7608 if (*nelts == error_mark_node)
7609 *nelts = integer_one_node;
7611 if (outer_declarator)
7612 outer_declarator->declarator = declarator->declarator;
7613 else
7614 new_declarator = NULL;
7617 return groktypename (&type_specifier_seq, new_declarator, false);
7620 /* Parse an (optional) new-declarator.
7622 new-declarator:
7623 ptr-operator new-declarator [opt]
7624 direct-new-declarator
7626 Returns the declarator. */
7628 static cp_declarator *
7629 cp_parser_new_declarator_opt (cp_parser* parser)
7631 enum tree_code code;
7632 tree type, std_attributes = NULL_TREE;
7633 cp_cv_quals cv_quals;
7635 /* We don't know if there's a ptr-operator next, or not. */
7636 cp_parser_parse_tentatively (parser);
7637 /* Look for a ptr-operator. */
7638 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7639 /* If that worked, look for more new-declarators. */
7640 if (cp_parser_parse_definitely (parser))
7642 cp_declarator *declarator;
7644 /* Parse another optional declarator. */
7645 declarator = cp_parser_new_declarator_opt (parser);
7647 declarator = cp_parser_make_indirect_declarator
7648 (code, type, cv_quals, declarator, std_attributes);
7650 return declarator;
7653 /* If the next token is a `[', there is a direct-new-declarator. */
7654 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7655 return cp_parser_direct_new_declarator (parser);
7657 return NULL;
7660 /* Parse a direct-new-declarator.
7662 direct-new-declarator:
7663 [ expression ]
7664 direct-new-declarator [constant-expression]
7668 static cp_declarator *
7669 cp_parser_direct_new_declarator (cp_parser* parser)
7671 cp_declarator *declarator = NULL;
7673 while (true)
7675 tree expression;
7676 cp_token *token;
7678 /* Look for the opening `['. */
7679 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7681 token = cp_lexer_peek_token (parser->lexer);
7682 expression = cp_parser_expression (parser);
7683 /* The standard requires that the expression have integral
7684 type. DR 74 adds enumeration types. We believe that the
7685 real intent is that these expressions be handled like the
7686 expression in a `switch' condition, which also allows
7687 classes with a single conversion to integral or
7688 enumeration type. */
7689 if (!processing_template_decl)
7691 expression
7692 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7693 expression,
7694 /*complain=*/true);
7695 if (!expression)
7697 error_at (token->location,
7698 "expression in new-declarator must have integral "
7699 "or enumeration type");
7700 expression = error_mark_node;
7704 /* Look for the closing `]'. */
7705 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7707 /* Add this bound to the declarator. */
7708 declarator = make_array_declarator (declarator, expression);
7710 /* If the next token is not a `[', then there are no more
7711 bounds. */
7712 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7713 break;
7716 return declarator;
7719 /* Parse a new-initializer.
7721 new-initializer:
7722 ( expression-list [opt] )
7723 braced-init-list
7725 Returns a representation of the expression-list. */
7727 static vec<tree, va_gc> *
7728 cp_parser_new_initializer (cp_parser* parser)
7730 vec<tree, va_gc> *expression_list;
7732 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7734 tree t;
7735 bool expr_non_constant_p;
7736 cp_lexer_set_source_position (parser->lexer);
7737 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7738 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7739 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7740 expression_list = make_tree_vector_single (t);
7742 else
7743 expression_list = (cp_parser_parenthesized_expression_list
7744 (parser, non_attr, /*cast_p=*/false,
7745 /*allow_expansion_p=*/true,
7746 /*non_constant_p=*/NULL));
7748 return expression_list;
7751 /* Parse a delete-expression.
7753 delete-expression:
7754 :: [opt] delete cast-expression
7755 :: [opt] delete [ ] cast-expression
7757 Returns a representation of the expression. */
7759 static tree
7760 cp_parser_delete_expression (cp_parser* parser)
7762 bool global_scope_p;
7763 bool array_p;
7764 tree expression;
7766 /* Look for the optional `::' operator. */
7767 global_scope_p
7768 = (cp_parser_global_scope_opt (parser,
7769 /*current_scope_valid_p=*/false)
7770 != NULL_TREE);
7771 /* Look for the `delete' keyword. */
7772 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7773 /* See if the array syntax is in use. */
7774 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7776 /* Consume the `[' token. */
7777 cp_lexer_consume_token (parser->lexer);
7778 /* Look for the `]' token. */
7779 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7780 /* Remember that this is the `[]' construct. */
7781 array_p = true;
7783 else
7784 array_p = false;
7786 /* Parse the cast-expression. */
7787 expression = cp_parser_simple_cast_expression (parser);
7789 /* A delete-expression may not appear in an integral constant
7790 expression. */
7791 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7792 return error_mark_node;
7794 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7795 tf_warning_or_error);
7798 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7799 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7800 0 otherwise. */
7802 static int
7803 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7805 cp_token *token = cp_lexer_peek_token (parser->lexer);
7806 switch (token->type)
7808 case CPP_COMMA:
7809 case CPP_SEMICOLON:
7810 case CPP_QUERY:
7811 case CPP_COLON:
7812 case CPP_CLOSE_SQUARE:
7813 case CPP_CLOSE_PAREN:
7814 case CPP_CLOSE_BRACE:
7815 case CPP_OPEN_BRACE:
7816 case CPP_DOT:
7817 case CPP_DOT_STAR:
7818 case CPP_DEREF:
7819 case CPP_DEREF_STAR:
7820 case CPP_DIV:
7821 case CPP_MOD:
7822 case CPP_LSHIFT:
7823 case CPP_RSHIFT:
7824 case CPP_LESS:
7825 case CPP_GREATER:
7826 case CPP_LESS_EQ:
7827 case CPP_GREATER_EQ:
7828 case CPP_EQ_EQ:
7829 case CPP_NOT_EQ:
7830 case CPP_EQ:
7831 case CPP_MULT_EQ:
7832 case CPP_DIV_EQ:
7833 case CPP_MOD_EQ:
7834 case CPP_PLUS_EQ:
7835 case CPP_MINUS_EQ:
7836 case CPP_RSHIFT_EQ:
7837 case CPP_LSHIFT_EQ:
7838 case CPP_AND_EQ:
7839 case CPP_XOR_EQ:
7840 case CPP_OR_EQ:
7841 case CPP_XOR:
7842 case CPP_OR:
7843 case CPP_OR_OR:
7844 case CPP_EOF:
7845 case CPP_ELLIPSIS:
7846 return 0;
7848 case CPP_OPEN_PAREN:
7849 /* In ((type ()) () the last () isn't a valid cast-expression,
7850 so the whole must be parsed as postfix-expression. */
7851 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7852 != CPP_CLOSE_PAREN;
7854 case CPP_OPEN_SQUARE:
7855 /* '[' may start a primary-expression in obj-c++ and in C++11,
7856 as a lambda-expression, eg, '(void)[]{}'. */
7857 if (cxx_dialect >= cxx11)
7858 return -1;
7859 return c_dialect_objc ();
7861 case CPP_PLUS_PLUS:
7862 case CPP_MINUS_MINUS:
7863 /* '++' and '--' may or may not start a cast-expression:
7865 struct T { void operator++(int); };
7866 void f() { (T())++; }
7870 int a;
7871 (int)++a; */
7872 return -1;
7874 default:
7875 return 1;
7879 /* Parse a cast-expression.
7881 cast-expression:
7882 unary-expression
7883 ( type-id ) cast-expression
7885 ADDRESS_P is true iff the unary-expression is appearing as the
7886 operand of the `&' operator. CAST_P is true if this expression is
7887 the target of a cast.
7889 Returns a representation of the expression. */
7891 static tree
7892 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7893 bool decltype_p, cp_id_kind * pidk)
7895 /* If it's a `(', then we might be looking at a cast. */
7896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7898 tree type = NULL_TREE;
7899 tree expr = NULL_TREE;
7900 int cast_expression = 0;
7901 const char *saved_message;
7903 /* There's no way to know yet whether or not this is a cast.
7904 For example, `(int (3))' is a unary-expression, while `(int)
7905 3' is a cast. So, we resort to parsing tentatively. */
7906 cp_parser_parse_tentatively (parser);
7907 /* Types may not be defined in a cast. */
7908 saved_message = parser->type_definition_forbidden_message;
7909 parser->type_definition_forbidden_message
7910 = G_("types may not be defined in casts");
7911 /* Consume the `('. */
7912 cp_lexer_consume_token (parser->lexer);
7913 /* A very tricky bit is that `(struct S) { 3 }' is a
7914 compound-literal (which we permit in C++ as an extension).
7915 But, that construct is not a cast-expression -- it is a
7916 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7917 is legal; if the compound-literal were a cast-expression,
7918 you'd need an extra set of parentheses.) But, if we parse
7919 the type-id, and it happens to be a class-specifier, then we
7920 will commit to the parse at that point, because we cannot
7921 undo the action that is done when creating a new class. So,
7922 then we cannot back up and do a postfix-expression.
7924 Another tricky case is the following (c++/29234):
7926 struct S { void operator () (); };
7928 void foo ()
7930 ( S()() );
7933 As a type-id we parse the parenthesized S()() as a function
7934 returning a function, groktypename complains and we cannot
7935 back up in this case either.
7937 Therefore, we scan ahead to the closing `)', and check to see
7938 if the tokens after the `)' can start a cast-expression. Otherwise
7939 we are dealing with an unary-expression, a postfix-expression
7940 or something else.
7942 Yet another tricky case, in C++11, is the following (c++/54891):
7944 (void)[]{};
7946 The issue is that usually, besides the case of lambda-expressions,
7947 the parenthesized type-id cannot be followed by '[', and, eg, we
7948 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7949 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7950 we don't commit, we try a cast-expression, then an unary-expression.
7952 Save tokens so that we can put them back. */
7953 cp_lexer_save_tokens (parser->lexer);
7955 /* We may be looking at a cast-expression. */
7956 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7957 /*consume_paren=*/true))
7958 cast_expression
7959 = cp_parser_tokens_start_cast_expression (parser);
7961 /* Roll back the tokens we skipped. */
7962 cp_lexer_rollback_tokens (parser->lexer);
7963 /* If we aren't looking at a cast-expression, simulate an error so
7964 that the call to cp_parser_error_occurred below returns true. */
7965 if (!cast_expression)
7966 cp_parser_simulate_error (parser);
7967 else
7969 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7970 parser->in_type_id_in_expr_p = true;
7971 /* Look for the type-id. */
7972 type = cp_parser_type_id (parser);
7973 /* Look for the closing `)'. */
7974 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7975 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7978 /* Restore the saved message. */
7979 parser->type_definition_forbidden_message = saved_message;
7981 /* At this point this can only be either a cast or a
7982 parenthesized ctor such as `(T ())' that looks like a cast to
7983 function returning T. */
7984 if (!cp_parser_error_occurred (parser))
7986 /* Only commit if the cast-expression doesn't start with
7987 '++', '--', or '[' in C++11. */
7988 if (cast_expression > 0)
7989 cp_parser_commit_to_topmost_tentative_parse (parser);
7991 expr = cp_parser_cast_expression (parser,
7992 /*address_p=*/false,
7993 /*cast_p=*/true,
7994 /*decltype_p=*/false,
7995 pidk);
7997 if (cp_parser_parse_definitely (parser))
7999 /* Warn about old-style casts, if so requested. */
8000 if (warn_old_style_cast
8001 && !in_system_header_at (input_location)
8002 && !VOID_TYPE_P (type)
8003 && current_lang_name != lang_name_c)
8004 warning (OPT_Wold_style_cast, "use of old-style cast");
8006 /* Only type conversions to integral or enumeration types
8007 can be used in constant-expressions. */
8008 if (!cast_valid_in_integral_constant_expression_p (type)
8009 && cp_parser_non_integral_constant_expression (parser,
8010 NIC_CAST))
8011 return error_mark_node;
8013 /* Perform the cast. */
8014 expr = build_c_cast (input_location, type, expr);
8015 return expr;
8018 else
8019 cp_parser_abort_tentative_parse (parser);
8022 /* If we get here, then it's not a cast, so it must be a
8023 unary-expression. */
8024 return cp_parser_unary_expression (parser, pidk, address_p,
8025 cast_p, decltype_p);
8028 /* Parse a binary expression of the general form:
8030 pm-expression:
8031 cast-expression
8032 pm-expression .* cast-expression
8033 pm-expression ->* cast-expression
8035 multiplicative-expression:
8036 pm-expression
8037 multiplicative-expression * pm-expression
8038 multiplicative-expression / pm-expression
8039 multiplicative-expression % pm-expression
8041 additive-expression:
8042 multiplicative-expression
8043 additive-expression + multiplicative-expression
8044 additive-expression - multiplicative-expression
8046 shift-expression:
8047 additive-expression
8048 shift-expression << additive-expression
8049 shift-expression >> additive-expression
8051 relational-expression:
8052 shift-expression
8053 relational-expression < shift-expression
8054 relational-expression > shift-expression
8055 relational-expression <= shift-expression
8056 relational-expression >= shift-expression
8058 GNU Extension:
8060 relational-expression:
8061 relational-expression <? shift-expression
8062 relational-expression >? shift-expression
8064 equality-expression:
8065 relational-expression
8066 equality-expression == relational-expression
8067 equality-expression != relational-expression
8069 and-expression:
8070 equality-expression
8071 and-expression & equality-expression
8073 exclusive-or-expression:
8074 and-expression
8075 exclusive-or-expression ^ and-expression
8077 inclusive-or-expression:
8078 exclusive-or-expression
8079 inclusive-or-expression | exclusive-or-expression
8081 logical-and-expression:
8082 inclusive-or-expression
8083 logical-and-expression && inclusive-or-expression
8085 logical-or-expression:
8086 logical-and-expression
8087 logical-or-expression || logical-and-expression
8089 All these are implemented with a single function like:
8091 binary-expression:
8092 simple-cast-expression
8093 binary-expression <token> binary-expression
8095 CAST_P is true if this expression is the target of a cast.
8097 The binops_by_token map is used to get the tree codes for each <token> type.
8098 binary-expressions are associated according to a precedence table. */
8100 #define TOKEN_PRECEDENCE(token) \
8101 (((token->type == CPP_GREATER \
8102 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8103 && !parser->greater_than_is_operator_p) \
8104 ? PREC_NOT_OPERATOR \
8105 : binops_by_token[token->type].prec)
8107 static tree
8108 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8109 bool no_toplevel_fold_p,
8110 bool decltype_p,
8111 enum cp_parser_prec prec,
8112 cp_id_kind * pidk)
8114 cp_parser_expression_stack stack;
8115 cp_parser_expression_stack_entry *sp = &stack[0];
8116 cp_parser_expression_stack_entry current;
8117 tree rhs;
8118 cp_token *token;
8119 enum tree_code rhs_type;
8120 enum cp_parser_prec new_prec, lookahead_prec;
8121 tree overload;
8123 /* Parse the first expression. */
8124 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8125 ? TRUTH_NOT_EXPR : ERROR_MARK);
8126 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8127 cast_p, decltype_p, pidk);
8128 current.prec = prec;
8130 if (cp_parser_error_occurred (parser))
8131 return error_mark_node;
8133 for (;;)
8135 /* Get an operator token. */
8136 token = cp_lexer_peek_token (parser->lexer);
8138 if (warn_cxx0x_compat
8139 && token->type == CPP_RSHIFT
8140 && !parser->greater_than_is_operator_p)
8142 if (warning_at (token->location, OPT_Wc__0x_compat,
8143 "%<>>%> operator is treated"
8144 " as two right angle brackets in C++11"))
8145 inform (token->location,
8146 "suggest parentheses around %<>>%> expression");
8149 new_prec = TOKEN_PRECEDENCE (token);
8151 /* Popping an entry off the stack means we completed a subexpression:
8152 - either we found a token which is not an operator (`>' where it is not
8153 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8154 will happen repeatedly;
8155 - or, we found an operator which has lower priority. This is the case
8156 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8157 parsing `3 * 4'. */
8158 if (new_prec <= current.prec)
8160 if (sp == stack)
8161 break;
8162 else
8163 goto pop;
8166 get_rhs:
8167 current.tree_type = binops_by_token[token->type].tree_type;
8168 current.loc = token->location;
8170 /* We used the operator token. */
8171 cp_lexer_consume_token (parser->lexer);
8173 /* For "false && x" or "true || x", x will never be executed;
8174 disable warnings while evaluating it. */
8175 if (current.tree_type == TRUTH_ANDIF_EXPR)
8176 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8177 else if (current.tree_type == TRUTH_ORIF_EXPR)
8178 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8180 /* Extract another operand. It may be the RHS of this expression
8181 or the LHS of a new, higher priority expression. */
8182 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8183 ? TRUTH_NOT_EXPR : ERROR_MARK);
8184 rhs = cp_parser_simple_cast_expression (parser);
8186 /* Get another operator token. Look up its precedence to avoid
8187 building a useless (immediately popped) stack entry for common
8188 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8189 token = cp_lexer_peek_token (parser->lexer);
8190 lookahead_prec = TOKEN_PRECEDENCE (token);
8191 if (lookahead_prec > new_prec)
8193 /* ... and prepare to parse the RHS of the new, higher priority
8194 expression. Since precedence levels on the stack are
8195 monotonically increasing, we do not have to care about
8196 stack overflows. */
8197 *sp = current;
8198 ++sp;
8199 current.lhs = rhs;
8200 current.lhs_type = rhs_type;
8201 current.prec = new_prec;
8202 new_prec = lookahead_prec;
8203 goto get_rhs;
8205 pop:
8206 lookahead_prec = new_prec;
8207 /* If the stack is not empty, we have parsed into LHS the right side
8208 (`4' in the example above) of an expression we had suspended.
8209 We can use the information on the stack to recover the LHS (`3')
8210 from the stack together with the tree code (`MULT_EXPR'), and
8211 the precedence of the higher level subexpression
8212 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8213 which will be used to actually build the additive expression. */
8214 rhs = current.lhs;
8215 rhs_type = current.lhs_type;
8216 --sp;
8217 current = *sp;
8220 /* Undo the disabling of warnings done above. */
8221 if (current.tree_type == TRUTH_ANDIF_EXPR)
8222 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8223 else if (current.tree_type == TRUTH_ORIF_EXPR)
8224 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8226 if (warn_logical_not_paren
8227 && current.lhs_type == TRUTH_NOT_EXPR)
8228 warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8230 overload = NULL;
8231 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8232 ERROR_MARK for everything that is not a binary expression.
8233 This makes warn_about_parentheses miss some warnings that
8234 involve unary operators. For unary expressions we should
8235 pass the correct tree_code unless the unary expression was
8236 surrounded by parentheses.
8238 if (no_toplevel_fold_p
8239 && lookahead_prec <= current.prec
8240 && sp == stack)
8241 current.lhs = build2 (current.tree_type,
8242 TREE_CODE_CLASS (current.tree_type)
8243 == tcc_comparison
8244 ? boolean_type_node : TREE_TYPE (current.lhs),
8245 current.lhs, rhs);
8246 else
8247 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8248 current.lhs, current.lhs_type,
8249 rhs, rhs_type, &overload,
8250 complain_flags (decltype_p));
8251 current.lhs_type = current.tree_type;
8252 if (EXPR_P (current.lhs))
8253 SET_EXPR_LOCATION (current.lhs, current.loc);
8255 /* If the binary operator required the use of an overloaded operator,
8256 then this expression cannot be an integral constant-expression.
8257 An overloaded operator can be used even if both operands are
8258 otherwise permissible in an integral constant-expression if at
8259 least one of the operands is of enumeration type. */
8261 if (overload
8262 && cp_parser_non_integral_constant_expression (parser,
8263 NIC_OVERLOADED))
8264 return error_mark_node;
8267 return current.lhs;
8270 static tree
8271 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8272 bool no_toplevel_fold_p,
8273 enum cp_parser_prec prec,
8274 cp_id_kind * pidk)
8276 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8277 /*decltype*/false, prec, pidk);
8280 /* Parse the `? expression : assignment-expression' part of a
8281 conditional-expression. The LOGICAL_OR_EXPR is the
8282 logical-or-expression that started the conditional-expression.
8283 Returns a representation of the entire conditional-expression.
8285 This routine is used by cp_parser_assignment_expression.
8287 ? expression : assignment-expression
8289 GNU Extensions:
8291 ? : assignment-expression */
8293 static tree
8294 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8296 tree expr;
8297 tree assignment_expr;
8298 struct cp_token *token;
8299 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8301 /* Consume the `?' token. */
8302 cp_lexer_consume_token (parser->lexer);
8303 token = cp_lexer_peek_token (parser->lexer);
8304 if (cp_parser_allow_gnu_extensions_p (parser)
8305 && token->type == CPP_COLON)
8307 pedwarn (token->location, OPT_Wpedantic,
8308 "ISO C++ does not allow ?: with omitted middle operand");
8309 /* Implicit true clause. */
8310 expr = NULL_TREE;
8311 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8312 warn_for_omitted_condop (token->location, logical_or_expr);
8314 else
8316 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8317 parser->colon_corrects_to_scope_p = false;
8318 /* Parse the expression. */
8319 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8320 expr = cp_parser_expression (parser);
8321 c_inhibit_evaluation_warnings +=
8322 ((logical_or_expr == truthvalue_true_node)
8323 - (logical_or_expr == truthvalue_false_node));
8324 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8327 /* The next token should be a `:'. */
8328 cp_parser_require (parser, CPP_COLON, RT_COLON);
8329 /* Parse the assignment-expression. */
8330 assignment_expr = cp_parser_assignment_expression (parser);
8331 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8333 /* Build the conditional-expression. */
8334 return build_x_conditional_expr (loc, logical_or_expr,
8335 expr,
8336 assignment_expr,
8337 tf_warning_or_error);
8340 /* Parse an assignment-expression.
8342 assignment-expression:
8343 conditional-expression
8344 logical-or-expression assignment-operator assignment_expression
8345 throw-expression
8347 CAST_P is true if this expression is the target of a cast.
8348 DECLTYPE_P is true if this expression is the operand of decltype.
8350 Returns a representation for the expression. */
8352 static tree
8353 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8354 bool cast_p, bool decltype_p)
8356 tree expr;
8358 /* If the next token is the `throw' keyword, then we're looking at
8359 a throw-expression. */
8360 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8361 expr = cp_parser_throw_expression (parser);
8362 /* Otherwise, it must be that we are looking at a
8363 logical-or-expression. */
8364 else
8366 /* Parse the binary expressions (logical-or-expression). */
8367 expr = cp_parser_binary_expression (parser, cast_p, false,
8368 decltype_p,
8369 PREC_NOT_OPERATOR, pidk);
8370 /* If the next token is a `?' then we're actually looking at a
8371 conditional-expression. */
8372 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8373 return cp_parser_question_colon_clause (parser, expr);
8374 else
8376 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8378 /* If it's an assignment-operator, we're using the second
8379 production. */
8380 enum tree_code assignment_operator
8381 = cp_parser_assignment_operator_opt (parser);
8382 if (assignment_operator != ERROR_MARK)
8384 bool non_constant_p;
8385 location_t saved_input_location;
8387 /* Parse the right-hand side of the assignment. */
8388 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8390 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8391 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8393 /* An assignment may not appear in a
8394 constant-expression. */
8395 if (cp_parser_non_integral_constant_expression (parser,
8396 NIC_ASSIGNMENT))
8397 return error_mark_node;
8398 /* Build the assignment expression. Its default
8399 location is the location of the '=' token. */
8400 saved_input_location = input_location;
8401 input_location = loc;
8402 expr = build_x_modify_expr (loc, expr,
8403 assignment_operator,
8404 rhs,
8405 complain_flags (decltype_p));
8406 input_location = saved_input_location;
8411 return expr;
8414 /* Parse an (optional) assignment-operator.
8416 assignment-operator: one of
8417 = *= /= %= += -= >>= <<= &= ^= |=
8419 GNU Extension:
8421 assignment-operator: one of
8422 <?= >?=
8424 If the next token is an assignment operator, the corresponding tree
8425 code is returned, and the token is consumed. For example, for
8426 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8427 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8428 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8429 operator, ERROR_MARK is returned. */
8431 static enum tree_code
8432 cp_parser_assignment_operator_opt (cp_parser* parser)
8434 enum tree_code op;
8435 cp_token *token;
8437 /* Peek at the next token. */
8438 token = cp_lexer_peek_token (parser->lexer);
8440 switch (token->type)
8442 case CPP_EQ:
8443 op = NOP_EXPR;
8444 break;
8446 case CPP_MULT_EQ:
8447 op = MULT_EXPR;
8448 break;
8450 case CPP_DIV_EQ:
8451 op = TRUNC_DIV_EXPR;
8452 break;
8454 case CPP_MOD_EQ:
8455 op = TRUNC_MOD_EXPR;
8456 break;
8458 case CPP_PLUS_EQ:
8459 op = PLUS_EXPR;
8460 break;
8462 case CPP_MINUS_EQ:
8463 op = MINUS_EXPR;
8464 break;
8466 case CPP_RSHIFT_EQ:
8467 op = RSHIFT_EXPR;
8468 break;
8470 case CPP_LSHIFT_EQ:
8471 op = LSHIFT_EXPR;
8472 break;
8474 case CPP_AND_EQ:
8475 op = BIT_AND_EXPR;
8476 break;
8478 case CPP_XOR_EQ:
8479 op = BIT_XOR_EXPR;
8480 break;
8482 case CPP_OR_EQ:
8483 op = BIT_IOR_EXPR;
8484 break;
8486 default:
8487 /* Nothing else is an assignment operator. */
8488 op = ERROR_MARK;
8491 /* If it was an assignment operator, consume it. */
8492 if (op != ERROR_MARK)
8493 cp_lexer_consume_token (parser->lexer);
8495 return op;
8498 /* Parse an expression.
8500 expression:
8501 assignment-expression
8502 expression , assignment-expression
8504 CAST_P is true if this expression is the target of a cast.
8505 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8506 except possibly parenthesized or on the RHS of a comma (N3276).
8508 Returns a representation of the expression. */
8510 static tree
8511 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8512 bool cast_p, bool decltype_p)
8514 tree expression = NULL_TREE;
8515 location_t loc = UNKNOWN_LOCATION;
8517 while (true)
8519 tree assignment_expression;
8521 /* Parse the next assignment-expression. */
8522 assignment_expression
8523 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8525 /* We don't create a temporary for a call that is the immediate operand
8526 of decltype or on the RHS of a comma. But when we see a comma, we
8527 need to create a temporary for a call on the LHS. */
8528 if (decltype_p && !processing_template_decl
8529 && TREE_CODE (assignment_expression) == CALL_EXPR
8530 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8531 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8532 assignment_expression
8533 = build_cplus_new (TREE_TYPE (assignment_expression),
8534 assignment_expression, tf_warning_or_error);
8536 /* If this is the first assignment-expression, we can just
8537 save it away. */
8538 if (!expression)
8539 expression = assignment_expression;
8540 else
8541 expression = build_x_compound_expr (loc, expression,
8542 assignment_expression,
8543 complain_flags (decltype_p));
8544 /* If the next token is not a comma, then we are done with the
8545 expression. */
8546 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8547 break;
8548 /* Consume the `,'. */
8549 loc = cp_lexer_peek_token (parser->lexer)->location;
8550 cp_lexer_consume_token (parser->lexer);
8551 /* A comma operator cannot appear in a constant-expression. */
8552 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8553 expression = error_mark_node;
8556 return expression;
8559 /* Parse a constant-expression.
8561 constant-expression:
8562 conditional-expression
8564 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8565 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8566 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8567 is false, NON_CONSTANT_P should be NULL. */
8569 static tree
8570 cp_parser_constant_expression (cp_parser* parser,
8571 bool allow_non_constant_p,
8572 bool *non_constant_p)
8574 bool saved_integral_constant_expression_p;
8575 bool saved_allow_non_integral_constant_expression_p;
8576 bool saved_non_integral_constant_expression_p;
8577 tree expression;
8579 /* It might seem that we could simply parse the
8580 conditional-expression, and then check to see if it were
8581 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8582 one that the compiler can figure out is constant, possibly after
8583 doing some simplifications or optimizations. The standard has a
8584 precise definition of constant-expression, and we must honor
8585 that, even though it is somewhat more restrictive.
8587 For example:
8589 int i[(2, 3)];
8591 is not a legal declaration, because `(2, 3)' is not a
8592 constant-expression. The `,' operator is forbidden in a
8593 constant-expression. However, GCC's constant-folding machinery
8594 will fold this operation to an INTEGER_CST for `3'. */
8596 /* Save the old settings. */
8597 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8598 saved_allow_non_integral_constant_expression_p
8599 = parser->allow_non_integral_constant_expression_p;
8600 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8601 /* We are now parsing a constant-expression. */
8602 parser->integral_constant_expression_p = true;
8603 parser->allow_non_integral_constant_expression_p
8604 = (allow_non_constant_p || cxx_dialect >= cxx11);
8605 parser->non_integral_constant_expression_p = false;
8606 /* Although the grammar says "conditional-expression", we parse an
8607 "assignment-expression", which also permits "throw-expression"
8608 and the use of assignment operators. In the case that
8609 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8610 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8611 actually essential that we look for an assignment-expression.
8612 For example, cp_parser_initializer_clauses uses this function to
8613 determine whether a particular assignment-expression is in fact
8614 constant. */
8615 expression = cp_parser_assignment_expression (parser);
8616 /* Restore the old settings. */
8617 parser->integral_constant_expression_p
8618 = saved_integral_constant_expression_p;
8619 parser->allow_non_integral_constant_expression_p
8620 = saved_allow_non_integral_constant_expression_p;
8621 if (cxx_dialect >= cxx11)
8623 /* Require an rvalue constant expression here; that's what our
8624 callers expect. Reference constant expressions are handled
8625 separately in e.g. cp_parser_template_argument. */
8626 bool is_const = potential_rvalue_constant_expression (expression);
8627 parser->non_integral_constant_expression_p = !is_const;
8628 if (!is_const && !allow_non_constant_p)
8629 require_potential_rvalue_constant_expression (expression);
8631 if (allow_non_constant_p)
8632 *non_constant_p = parser->non_integral_constant_expression_p;
8633 parser->non_integral_constant_expression_p
8634 = saved_non_integral_constant_expression_p;
8636 return expression;
8639 /* Parse __builtin_offsetof.
8641 offsetof-expression:
8642 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8644 offsetof-member-designator:
8645 id-expression
8646 | offsetof-member-designator "." id-expression
8647 | offsetof-member-designator "[" expression "]"
8648 | offsetof-member-designator "->" id-expression */
8650 static tree
8651 cp_parser_builtin_offsetof (cp_parser *parser)
8653 int save_ice_p, save_non_ice_p;
8654 tree type, expr;
8655 cp_id_kind dummy;
8656 cp_token *token;
8658 /* We're about to accept non-integral-constant things, but will
8659 definitely yield an integral constant expression. Save and
8660 restore these values around our local parsing. */
8661 save_ice_p = parser->integral_constant_expression_p;
8662 save_non_ice_p = parser->non_integral_constant_expression_p;
8664 /* Consume the "__builtin_offsetof" token. */
8665 cp_lexer_consume_token (parser->lexer);
8666 /* Consume the opening `('. */
8667 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8668 /* Parse the type-id. */
8669 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8670 type = cp_parser_type_id (parser);
8671 /* Look for the `,'. */
8672 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8673 token = cp_lexer_peek_token (parser->lexer);
8675 /* Build the (type *)null that begins the traditional offsetof macro. */
8676 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8677 tf_warning_or_error);
8679 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8680 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8681 true, &dummy, token->location);
8682 while (true)
8684 token = cp_lexer_peek_token (parser->lexer);
8685 switch (token->type)
8687 case CPP_OPEN_SQUARE:
8688 /* offsetof-member-designator "[" expression "]" */
8689 expr = cp_parser_postfix_open_square_expression (parser, expr,
8690 true, false);
8691 break;
8693 case CPP_DEREF:
8694 /* offsetof-member-designator "->" identifier */
8695 expr = grok_array_decl (token->location, expr,
8696 integer_zero_node, false);
8697 /* FALLTHRU */
8699 case CPP_DOT:
8700 /* offsetof-member-designator "." identifier */
8701 cp_lexer_consume_token (parser->lexer);
8702 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8703 expr, true, &dummy,
8704 token->location);
8705 break;
8707 case CPP_CLOSE_PAREN:
8708 /* Consume the ")" token. */
8709 cp_lexer_consume_token (parser->lexer);
8710 goto success;
8712 default:
8713 /* Error. We know the following require will fail, but
8714 that gives the proper error message. */
8715 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8716 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8717 expr = error_mark_node;
8718 goto failure;
8722 success:
8723 /* If we're processing a template, we can't finish the semantics yet.
8724 Otherwise we can fold the entire expression now. */
8725 if (processing_template_decl)
8727 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8728 SET_EXPR_LOCATION (expr, loc);
8730 else
8731 expr = finish_offsetof (expr, loc);
8733 failure:
8734 parser->integral_constant_expression_p = save_ice_p;
8735 parser->non_integral_constant_expression_p = save_non_ice_p;
8737 return expr;
8740 /* Parse a trait expression.
8742 Returns a representation of the expression, the underlying type
8743 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8745 static tree
8746 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8748 cp_trait_kind kind;
8749 tree type1, type2 = NULL_TREE;
8750 bool binary = false;
8751 bool variadic = false;
8753 switch (keyword)
8755 case RID_HAS_NOTHROW_ASSIGN:
8756 kind = CPTK_HAS_NOTHROW_ASSIGN;
8757 break;
8758 case RID_HAS_NOTHROW_CONSTRUCTOR:
8759 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8760 break;
8761 case RID_HAS_NOTHROW_COPY:
8762 kind = CPTK_HAS_NOTHROW_COPY;
8763 break;
8764 case RID_HAS_TRIVIAL_ASSIGN:
8765 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8766 break;
8767 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8768 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8769 break;
8770 case RID_HAS_TRIVIAL_COPY:
8771 kind = CPTK_HAS_TRIVIAL_COPY;
8772 break;
8773 case RID_HAS_TRIVIAL_DESTRUCTOR:
8774 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8775 break;
8776 case RID_HAS_VIRTUAL_DESTRUCTOR:
8777 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8778 break;
8779 case RID_IS_ABSTRACT:
8780 kind = CPTK_IS_ABSTRACT;
8781 break;
8782 case RID_IS_BASE_OF:
8783 kind = CPTK_IS_BASE_OF;
8784 binary = true;
8785 break;
8786 case RID_IS_CLASS:
8787 kind = CPTK_IS_CLASS;
8788 break;
8789 case RID_IS_EMPTY:
8790 kind = CPTK_IS_EMPTY;
8791 break;
8792 case RID_IS_ENUM:
8793 kind = CPTK_IS_ENUM;
8794 break;
8795 case RID_IS_FINAL:
8796 kind = CPTK_IS_FINAL;
8797 break;
8798 case RID_IS_LITERAL_TYPE:
8799 kind = CPTK_IS_LITERAL_TYPE;
8800 break;
8801 case RID_IS_POD:
8802 kind = CPTK_IS_POD;
8803 break;
8804 case RID_IS_POLYMORPHIC:
8805 kind = CPTK_IS_POLYMORPHIC;
8806 break;
8807 case RID_IS_STD_LAYOUT:
8808 kind = CPTK_IS_STD_LAYOUT;
8809 break;
8810 case RID_IS_TRIVIAL:
8811 kind = CPTK_IS_TRIVIAL;
8812 break;
8813 case RID_IS_TRIVIALLY_ASSIGNABLE:
8814 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8815 binary = true;
8816 break;
8817 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8818 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8819 variadic = true;
8820 break;
8821 case RID_IS_TRIVIALLY_COPYABLE:
8822 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8823 break;
8824 case RID_IS_UNION:
8825 kind = CPTK_IS_UNION;
8826 break;
8827 case RID_UNDERLYING_TYPE:
8828 kind = CPTK_UNDERLYING_TYPE;
8829 break;
8830 case RID_BASES:
8831 kind = CPTK_BASES;
8832 break;
8833 case RID_DIRECT_BASES:
8834 kind = CPTK_DIRECT_BASES;
8835 break;
8836 default:
8837 gcc_unreachable ();
8840 /* Consume the token. */
8841 cp_lexer_consume_token (parser->lexer);
8843 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8845 type1 = cp_parser_type_id (parser);
8847 if (type1 == error_mark_node)
8848 return error_mark_node;
8850 if (binary)
8852 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8854 type2 = cp_parser_type_id (parser);
8856 if (type2 == error_mark_node)
8857 return error_mark_node;
8859 else if (variadic)
8861 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8863 cp_lexer_consume_token (parser->lexer);
8864 tree elt = cp_parser_type_id (parser);
8865 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8867 cp_lexer_consume_token (parser->lexer);
8868 elt = make_pack_expansion (elt);
8870 if (elt == error_mark_node)
8871 return error_mark_node;
8872 type2 = tree_cons (NULL_TREE, elt, type2);
8876 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8878 /* Complete the trait expression, which may mean either processing
8879 the trait expr now or saving it for template instantiation. */
8880 switch(kind)
8882 case CPTK_UNDERLYING_TYPE:
8883 return finish_underlying_type (type1);
8884 case CPTK_BASES:
8885 return finish_bases (type1, false);
8886 case CPTK_DIRECT_BASES:
8887 return finish_bases (type1, true);
8888 default:
8889 return finish_trait_expr (kind, type1, type2);
8893 /* Lambdas that appear in variable initializer or default argument scope
8894 get that in their mangling, so we need to record it. We might as well
8895 use the count for function and namespace scopes as well. */
8896 static GTY(()) tree lambda_scope;
8897 static GTY(()) int lambda_count;
8898 typedef struct GTY(()) tree_int
8900 tree t;
8901 int i;
8902 } tree_int;
8903 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8905 static void
8906 start_lambda_scope (tree decl)
8908 tree_int ti;
8909 gcc_assert (decl);
8910 /* Once we're inside a function, we ignore other scopes and just push
8911 the function again so that popping works properly. */
8912 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8913 decl = current_function_decl;
8914 ti.t = lambda_scope;
8915 ti.i = lambda_count;
8916 vec_safe_push (lambda_scope_stack, ti);
8917 if (lambda_scope != decl)
8919 /* Don't reset the count if we're still in the same function. */
8920 lambda_scope = decl;
8921 lambda_count = 0;
8925 static void
8926 record_lambda_scope (tree lambda)
8928 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8929 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8932 static void
8933 finish_lambda_scope (void)
8935 tree_int *p = &lambda_scope_stack->last ();
8936 if (lambda_scope != p->t)
8938 lambda_scope = p->t;
8939 lambda_count = p->i;
8941 lambda_scope_stack->pop ();
8944 /* Parse a lambda expression.
8946 lambda-expression:
8947 lambda-introducer lambda-declarator [opt] compound-statement
8949 Returns a representation of the expression. */
8951 static tree
8952 cp_parser_lambda_expression (cp_parser* parser)
8954 tree lambda_expr = build_lambda_expr ();
8955 tree type;
8956 bool ok = true;
8957 cp_token *token = cp_lexer_peek_token (parser->lexer);
8958 cp_token_position start = 0;
8960 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8962 if (cp_unevaluated_operand)
8964 if (!token->error_reported)
8966 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8967 "lambda-expression in unevaluated context");
8968 token->error_reported = true;
8970 ok = false;
8972 else if (parser->in_template_argument_list_p)
8974 if (!token->error_reported)
8976 error_at (token->location, "lambda-expression in template-argument");
8977 token->error_reported = true;
8979 ok = false;
8982 /* We may be in the middle of deferred access check. Disable
8983 it now. */
8984 push_deferring_access_checks (dk_no_deferred);
8986 cp_parser_lambda_introducer (parser, lambda_expr);
8988 type = begin_lambda_type (lambda_expr);
8989 if (type == error_mark_node)
8990 return error_mark_node;
8992 record_lambda_scope (lambda_expr);
8994 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8995 determine_visibility (TYPE_NAME (type));
8997 /* Now that we've started the type, add the capture fields for any
8998 explicit captures. */
8999 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9002 /* Inside the class, surrounding template-parameter-lists do not apply. */
9003 unsigned int saved_num_template_parameter_lists
9004 = parser->num_template_parameter_lists;
9005 unsigned char in_statement = parser->in_statement;
9006 bool in_switch_statement_p = parser->in_switch_statement_p;
9007 bool fully_implicit_function_template_p
9008 = parser->fully_implicit_function_template_p;
9009 tree implicit_template_parms = parser->implicit_template_parms;
9010 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9011 bool auto_is_implicit_function_template_parm_p
9012 = parser->auto_is_implicit_function_template_parm_p;
9014 parser->num_template_parameter_lists = 0;
9015 parser->in_statement = 0;
9016 parser->in_switch_statement_p = false;
9017 parser->fully_implicit_function_template_p = false;
9018 parser->implicit_template_parms = 0;
9019 parser->implicit_template_scope = 0;
9020 parser->auto_is_implicit_function_template_parm_p = false;
9022 /* By virtue of defining a local class, a lambda expression has access to
9023 the private variables of enclosing classes. */
9025 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9027 if (ok)
9029 if (!cp_parser_error_occurred (parser)
9030 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9031 && cp_parser_start_tentative_firewall (parser))
9032 start = token;
9033 cp_parser_lambda_body (parser, lambda_expr);
9035 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9037 if (cp_parser_skip_to_closing_brace (parser))
9038 cp_lexer_consume_token (parser->lexer);
9041 /* The capture list was built up in reverse order; fix that now. */
9042 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9043 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9045 if (ok)
9046 maybe_add_lambda_conv_op (type);
9048 type = finish_struct (type, /*attributes=*/NULL_TREE);
9050 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9051 parser->in_statement = in_statement;
9052 parser->in_switch_statement_p = in_switch_statement_p;
9053 parser->fully_implicit_function_template_p
9054 = fully_implicit_function_template_p;
9055 parser->implicit_template_parms = implicit_template_parms;
9056 parser->implicit_template_scope = implicit_template_scope;
9057 parser->auto_is_implicit_function_template_parm_p
9058 = auto_is_implicit_function_template_parm_p;
9061 pop_deferring_access_checks ();
9063 /* This field is only used during parsing of the lambda. */
9064 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9066 /* This lambda shouldn't have any proxies left at this point. */
9067 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9068 /* And now that we're done, push proxies for an enclosing lambda. */
9069 insert_pending_capture_proxies ();
9071 if (ok)
9072 lambda_expr = build_lambda_object (lambda_expr);
9073 else
9074 lambda_expr = error_mark_node;
9076 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9078 return lambda_expr;
9081 /* Parse the beginning of a lambda expression.
9083 lambda-introducer:
9084 [ lambda-capture [opt] ]
9086 LAMBDA_EXPR is the current representation of the lambda expression. */
9088 static void
9089 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9091 /* Need commas after the first capture. */
9092 bool first = true;
9094 /* Eat the leading `['. */
9095 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9097 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9098 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9099 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9100 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9101 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9102 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9104 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9106 cp_lexer_consume_token (parser->lexer);
9107 first = false;
9110 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9112 cp_token* capture_token;
9113 tree capture_id;
9114 tree capture_init_expr;
9115 cp_id_kind idk = CP_ID_KIND_NONE;
9116 bool explicit_init_p = false;
9118 enum capture_kind_type
9120 BY_COPY,
9121 BY_REFERENCE
9123 enum capture_kind_type capture_kind = BY_COPY;
9125 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9127 error ("expected end of capture-list");
9128 return;
9131 if (first)
9132 first = false;
9133 else
9134 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9136 /* Possibly capture `this'. */
9137 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9139 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9140 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9141 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9142 "with by-copy capture default");
9143 cp_lexer_consume_token (parser->lexer);
9144 add_capture (lambda_expr,
9145 /*id=*/this_identifier,
9146 /*initializer=*/finish_this_expr(),
9147 /*by_reference_p=*/false,
9148 explicit_init_p);
9149 continue;
9152 /* Remember whether we want to capture as a reference or not. */
9153 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9155 capture_kind = BY_REFERENCE;
9156 cp_lexer_consume_token (parser->lexer);
9159 /* Get the identifier. */
9160 capture_token = cp_lexer_peek_token (parser->lexer);
9161 capture_id = cp_parser_identifier (parser);
9163 if (capture_id == error_mark_node)
9164 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9165 delimiters, but I modified this to stop on unnested ']' as well. It
9166 was already changed to stop on unnested '}', so the
9167 "closing_parenthesis" name is no more misleading with my change. */
9169 cp_parser_skip_to_closing_parenthesis (parser,
9170 /*recovering=*/true,
9171 /*or_comma=*/true,
9172 /*consume_paren=*/true);
9173 break;
9176 /* Find the initializer for this capture. */
9177 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9178 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9179 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9181 bool direct, non_constant;
9182 /* An explicit initializer exists. */
9183 if (cxx_dialect < cxx14)
9184 pedwarn (input_location, 0,
9185 "lambda capture initializers "
9186 "only available with -std=c++14 or -std=gnu++14");
9187 capture_init_expr = cp_parser_initializer (parser, &direct,
9188 &non_constant);
9189 explicit_init_p = true;
9190 if (capture_init_expr == NULL_TREE)
9192 error ("empty initializer for lambda init-capture");
9193 capture_init_expr = error_mark_node;
9196 else
9198 const char* error_msg;
9200 /* Turn the identifier into an id-expression. */
9201 capture_init_expr
9202 = cp_parser_lookup_name_simple (parser, capture_id,
9203 capture_token->location);
9205 if (capture_init_expr == error_mark_node)
9207 unqualified_name_lookup_error (capture_id);
9208 continue;
9210 else if (DECL_P (capture_init_expr)
9211 && (!VAR_P (capture_init_expr)
9212 && TREE_CODE (capture_init_expr) != PARM_DECL))
9214 error_at (capture_token->location,
9215 "capture of non-variable %qD ",
9216 capture_init_expr);
9217 inform (0, "%q+#D declared here", capture_init_expr);
9218 continue;
9220 if (VAR_P (capture_init_expr)
9221 && decl_storage_duration (capture_init_expr) != dk_auto)
9223 if (pedwarn (capture_token->location, 0, "capture of variable "
9224 "%qD with non-automatic storage duration",
9225 capture_init_expr))
9226 inform (0, "%q+#D declared here", capture_init_expr);
9227 continue;
9230 capture_init_expr
9231 = finish_id_expression
9232 (capture_id,
9233 capture_init_expr,
9234 parser->scope,
9235 &idk,
9236 /*integral_constant_expression_p=*/false,
9237 /*allow_non_integral_constant_expression_p=*/false,
9238 /*non_integral_constant_expression_p=*/NULL,
9239 /*template_p=*/false,
9240 /*done=*/true,
9241 /*address_p=*/false,
9242 /*template_arg_p=*/false,
9243 &error_msg,
9244 capture_token->location);
9246 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9248 cp_lexer_consume_token (parser->lexer);
9249 capture_init_expr = make_pack_expansion (capture_init_expr);
9251 else
9252 check_for_bare_parameter_packs (capture_init_expr);
9255 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9256 && !explicit_init_p)
9258 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9259 && capture_kind == BY_COPY)
9260 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9261 "of %qD redundant with by-copy capture default",
9262 capture_id);
9263 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9264 && capture_kind == BY_REFERENCE)
9265 pedwarn (capture_token->location, 0, "explicit by-reference "
9266 "capture of %qD redundant with by-reference capture "
9267 "default", capture_id);
9270 add_capture (lambda_expr,
9271 capture_id,
9272 capture_init_expr,
9273 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9274 explicit_init_p);
9277 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9280 /* Parse the (optional) middle of a lambda expression.
9282 lambda-declarator:
9283 < template-parameter-list [opt] >
9284 ( parameter-declaration-clause [opt] )
9285 attribute-specifier [opt]
9286 mutable [opt]
9287 exception-specification [opt]
9288 lambda-return-type-clause [opt]
9290 LAMBDA_EXPR is the current representation of the lambda expression. */
9292 static bool
9293 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9295 /* 5.1.1.4 of the standard says:
9296 If a lambda-expression does not include a lambda-declarator, it is as if
9297 the lambda-declarator were ().
9298 This means an empty parameter list, no attributes, and no exception
9299 specification. */
9300 tree param_list = void_list_node;
9301 tree attributes = NULL_TREE;
9302 tree exception_spec = NULL_TREE;
9303 tree template_param_list = NULL_TREE;
9305 /* The template-parameter-list is optional, but must begin with
9306 an opening angle if present. */
9307 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9309 if (cxx_dialect < cxx14)
9310 pedwarn (parser->lexer->next_token->location, 0,
9311 "lambda templates are only available with "
9312 "-std=c++14 or -std=gnu++14");
9314 cp_lexer_consume_token (parser->lexer);
9316 template_param_list = cp_parser_template_parameter_list (parser);
9318 cp_parser_skip_to_end_of_template_parameter_list (parser);
9320 /* We just processed one more parameter list. */
9321 ++parser->num_template_parameter_lists;
9324 /* The parameter-declaration-clause is optional (unless
9325 template-parameter-list was given), but must begin with an
9326 opening parenthesis if present. */
9327 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9329 cp_lexer_consume_token (parser->lexer);
9331 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9333 /* Parse parameters. */
9334 param_list = cp_parser_parameter_declaration_clause (parser);
9336 /* Default arguments shall not be specified in the
9337 parameter-declaration-clause of a lambda-declarator. */
9338 for (tree t = param_list; t; t = TREE_CHAIN (t))
9339 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9340 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9341 "default argument specified for lambda parameter");
9343 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9345 attributes = cp_parser_attributes_opt (parser);
9347 /* Parse optional `mutable' keyword. */
9348 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9350 cp_lexer_consume_token (parser->lexer);
9351 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9354 /* Parse optional exception specification. */
9355 exception_spec = cp_parser_exception_specification_opt (parser);
9357 /* Parse optional trailing return type. */
9358 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9360 cp_lexer_consume_token (parser->lexer);
9361 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9362 = cp_parser_trailing_type_id (parser);
9365 /* The function parameters must be in scope all the way until after the
9366 trailing-return-type in case of decltype. */
9367 pop_bindings_and_leave_scope ();
9369 else if (template_param_list != NULL_TREE) // generate diagnostic
9370 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9372 /* Create the function call operator.
9374 Messing with declarators like this is no uglier than building up the
9375 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9376 other code. */
9378 cp_decl_specifier_seq return_type_specs;
9379 cp_declarator* declarator;
9380 tree fco;
9381 int quals;
9382 void *p;
9384 clear_decl_specs (&return_type_specs);
9385 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9386 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9387 else
9388 /* Maybe we will deduce the return type later. */
9389 return_type_specs.type = make_auto ();
9391 p = obstack_alloc (&declarator_obstack, 0);
9393 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9394 sfk_none);
9396 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9397 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9398 declarator = make_call_declarator (declarator, param_list, quals,
9399 VIRT_SPEC_UNSPECIFIED,
9400 REF_QUAL_NONE,
9401 exception_spec,
9402 /*late_return_type=*/NULL_TREE);
9403 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9405 fco = grokmethod (&return_type_specs,
9406 declarator,
9407 attributes);
9408 if (fco != error_mark_node)
9410 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9411 DECL_ARTIFICIAL (fco) = 1;
9412 /* Give the object parameter a different name. */
9413 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9414 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9415 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9417 if (template_param_list)
9419 fco = finish_member_template_decl (fco);
9420 finish_template_decl (template_param_list);
9421 --parser->num_template_parameter_lists;
9423 else if (parser->fully_implicit_function_template_p)
9424 fco = finish_fully_implicit_template (parser, fco);
9426 finish_member_declaration (fco);
9428 obstack_free (&declarator_obstack, p);
9430 return (fco != error_mark_node);
9434 /* Parse the body of a lambda expression, which is simply
9436 compound-statement
9438 but which requires special handling.
9439 LAMBDA_EXPR is the current representation of the lambda expression. */
9441 static void
9442 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9444 bool nested = (current_function_decl != NULL_TREE);
9445 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9446 if (nested)
9447 push_function_context ();
9448 else
9449 /* Still increment function_depth so that we don't GC in the
9450 middle of an expression. */
9451 ++function_depth;
9452 /* Clear this in case we're in the middle of a default argument. */
9453 parser->local_variables_forbidden_p = false;
9455 /* Finish the function call operator
9456 - class_specifier
9457 + late_parsing_for_member
9458 + function_definition_after_declarator
9459 + ctor_initializer_opt_and_function_body */
9461 tree fco = lambda_function (lambda_expr);
9462 tree body;
9463 bool done = false;
9464 tree compound_stmt;
9465 tree cap;
9467 /* Let the front end know that we are going to be defining this
9468 function. */
9469 start_preparsed_function (fco,
9470 NULL_TREE,
9471 SF_PRE_PARSED | SF_INCLASS_INLINE);
9473 start_lambda_scope (fco);
9474 body = begin_function_body ();
9476 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9477 goto out;
9479 /* Push the proxies for any explicit captures. */
9480 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9481 cap = TREE_CHAIN (cap))
9482 build_capture_proxy (TREE_PURPOSE (cap));
9484 compound_stmt = begin_compound_stmt (0);
9486 /* 5.1.1.4 of the standard says:
9487 If a lambda-expression does not include a trailing-return-type, it
9488 is as if the trailing-return-type denotes the following type:
9489 * if the compound-statement is of the form
9490 { return attribute-specifier [opt] expression ; }
9491 the type of the returned expression after lvalue-to-rvalue
9492 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9493 (_conv.array_ 4.2), and function-to-pointer conversion
9494 (_conv.func_ 4.3);
9495 * otherwise, void. */
9497 /* In a lambda that has neither a lambda-return-type-clause
9498 nor a deducible form, errors should be reported for return statements
9499 in the body. Since we used void as the placeholder return type, parsing
9500 the body as usual will give such desired behavior. */
9501 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9502 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9503 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9505 tree expr = NULL_TREE;
9506 cp_id_kind idk = CP_ID_KIND_NONE;
9508 /* Parse tentatively in case there's more after the initial return
9509 statement. */
9510 cp_parser_parse_tentatively (parser);
9512 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9514 expr = cp_parser_expression (parser, &idk);
9516 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9517 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9519 if (cp_parser_parse_definitely (parser))
9521 if (!processing_template_decl)
9522 apply_deduced_return_type (fco, lambda_return_type (expr));
9524 /* Will get error here if type not deduced yet. */
9525 finish_return_stmt (expr);
9527 done = true;
9531 if (!done)
9533 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9534 cp_parser_label_declaration (parser);
9535 cp_parser_statement_seq_opt (parser, NULL_TREE);
9536 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9539 finish_compound_stmt (compound_stmt);
9541 out:
9542 finish_function_body (body);
9543 finish_lambda_scope ();
9545 /* Finish the function and generate code for it if necessary. */
9546 tree fn = finish_function (/*inline*/2);
9548 /* Only expand if the call op is not a template. */
9549 if (!DECL_TEMPLATE_INFO (fco))
9550 expand_or_defer_fn (fn);
9553 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9554 if (nested)
9555 pop_function_context();
9556 else
9557 --function_depth;
9560 /* Statements [gram.stmt.stmt] */
9562 /* Parse a statement.
9564 statement:
9565 labeled-statement
9566 expression-statement
9567 compound-statement
9568 selection-statement
9569 iteration-statement
9570 jump-statement
9571 declaration-statement
9572 try-block
9574 C++11:
9576 statement:
9577 labeled-statement
9578 attribute-specifier-seq (opt) expression-statement
9579 attribute-specifier-seq (opt) compound-statement
9580 attribute-specifier-seq (opt) selection-statement
9581 attribute-specifier-seq (opt) iteration-statement
9582 attribute-specifier-seq (opt) jump-statement
9583 declaration-statement
9584 attribute-specifier-seq (opt) try-block
9586 TM Extension:
9588 statement:
9589 atomic-statement
9591 IN_COMPOUND is true when the statement is nested inside a
9592 cp_parser_compound_statement; this matters for certain pragmas.
9594 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9595 is a (possibly labeled) if statement which is not enclosed in braces
9596 and has an else clause. This is used to implement -Wparentheses. */
9598 static void
9599 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9600 bool in_compound, bool *if_p)
9602 tree statement, std_attrs = NULL_TREE;
9603 cp_token *token;
9604 location_t statement_location, attrs_location;
9606 restart:
9607 if (if_p != NULL)
9608 *if_p = false;
9609 /* There is no statement yet. */
9610 statement = NULL_TREE;
9612 saved_token_sentinel saved_tokens (parser->lexer);
9613 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9614 if (c_dialect_objc ())
9615 /* In obj-c++, seeing '[[' might be the either the beginning of
9616 c++11 attributes, or a nested objc-message-expression. So
9617 let's parse the c++11 attributes tentatively. */
9618 cp_parser_parse_tentatively (parser);
9619 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9620 if (c_dialect_objc ())
9622 if (!cp_parser_parse_definitely (parser))
9623 std_attrs = NULL_TREE;
9626 /* Peek at the next token. */
9627 token = cp_lexer_peek_token (parser->lexer);
9628 /* Remember the location of the first token in the statement. */
9629 statement_location = token->location;
9630 /* If this is a keyword, then that will often determine what kind of
9631 statement we have. */
9632 if (token->type == CPP_KEYWORD)
9634 enum rid keyword = token->keyword;
9636 switch (keyword)
9638 case RID_CASE:
9639 case RID_DEFAULT:
9640 /* Looks like a labeled-statement with a case label.
9641 Parse the label, and then use tail recursion to parse
9642 the statement. */
9643 cp_parser_label_for_labeled_statement (parser, std_attrs);
9644 goto restart;
9646 case RID_IF:
9647 case RID_SWITCH:
9648 statement = cp_parser_selection_statement (parser, if_p);
9649 break;
9651 case RID_WHILE:
9652 case RID_DO:
9653 case RID_FOR:
9654 statement = cp_parser_iteration_statement (parser, false);
9655 break;
9657 case RID_CILK_FOR:
9658 if (!flag_cilkplus)
9660 error_at (cp_lexer_peek_token (parser->lexer)->location,
9661 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9662 cp_lexer_consume_token (parser->lexer);
9663 statement = error_mark_node;
9665 else
9666 statement = cp_parser_cilk_for (parser, integer_zero_node);
9667 break;
9669 case RID_BREAK:
9670 case RID_CONTINUE:
9671 case RID_RETURN:
9672 case RID_GOTO:
9673 statement = cp_parser_jump_statement (parser);
9674 break;
9676 case RID_CILK_SYNC:
9677 cp_lexer_consume_token (parser->lexer);
9678 if (flag_cilkplus)
9680 tree sync_expr = build_cilk_sync ();
9681 SET_EXPR_LOCATION (sync_expr,
9682 token->location);
9683 statement = finish_expr_stmt (sync_expr);
9685 else
9687 error_at (token->location, "-fcilkplus must be enabled to use"
9688 " %<_Cilk_sync%>");
9689 statement = error_mark_node;
9691 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9692 break;
9694 /* Objective-C++ exception-handling constructs. */
9695 case RID_AT_TRY:
9696 case RID_AT_CATCH:
9697 case RID_AT_FINALLY:
9698 case RID_AT_SYNCHRONIZED:
9699 case RID_AT_THROW:
9700 statement = cp_parser_objc_statement (parser);
9701 break;
9703 case RID_TRY:
9704 statement = cp_parser_try_block (parser);
9705 break;
9707 case RID_NAMESPACE:
9708 /* This must be a namespace alias definition. */
9709 cp_parser_declaration_statement (parser);
9710 return;
9712 case RID_TRANSACTION_ATOMIC:
9713 case RID_TRANSACTION_RELAXED:
9714 statement = cp_parser_transaction (parser, keyword);
9715 break;
9716 case RID_TRANSACTION_CANCEL:
9717 statement = cp_parser_transaction_cancel (parser);
9718 break;
9720 default:
9721 /* It might be a keyword like `int' that can start a
9722 declaration-statement. */
9723 break;
9726 else if (token->type == CPP_NAME)
9728 /* If the next token is a `:', then we are looking at a
9729 labeled-statement. */
9730 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9731 if (token->type == CPP_COLON)
9733 /* Looks like a labeled-statement with an ordinary label.
9734 Parse the label, and then use tail recursion to parse
9735 the statement. */
9737 cp_parser_label_for_labeled_statement (parser, std_attrs);
9738 goto restart;
9741 /* Anything that starts with a `{' must be a compound-statement. */
9742 else if (token->type == CPP_OPEN_BRACE)
9743 statement = cp_parser_compound_statement (parser, NULL, false, false);
9744 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9745 a statement all its own. */
9746 else if (token->type == CPP_PRAGMA)
9748 /* Only certain OpenMP pragmas are attached to statements, and thus
9749 are considered statements themselves. All others are not. In
9750 the context of a compound, accept the pragma as a "statement" and
9751 return so that we can check for a close brace. Otherwise we
9752 require a real statement and must go back and read one. */
9753 if (in_compound)
9754 cp_parser_pragma (parser, pragma_compound);
9755 else if (!cp_parser_pragma (parser, pragma_stmt))
9756 goto restart;
9757 return;
9759 else if (token->type == CPP_EOF)
9761 cp_parser_error (parser, "expected statement");
9762 return;
9765 /* Everything else must be a declaration-statement or an
9766 expression-statement. Try for the declaration-statement
9767 first, unless we are looking at a `;', in which case we know that
9768 we have an expression-statement. */
9769 if (!statement)
9771 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9773 if (std_attrs != NULL_TREE)
9775 /* Attributes should be parsed as part of the the
9776 declaration, so let's un-parse them. */
9777 saved_tokens.rollback();
9778 std_attrs = NULL_TREE;
9781 cp_parser_parse_tentatively (parser);
9782 /* Try to parse the declaration-statement. */
9783 cp_parser_declaration_statement (parser);
9784 /* If that worked, we're done. */
9785 if (cp_parser_parse_definitely (parser))
9786 return;
9788 /* Look for an expression-statement instead. */
9789 statement = cp_parser_expression_statement (parser, in_statement_expr);
9792 /* Set the line number for the statement. */
9793 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9794 SET_EXPR_LOCATION (statement, statement_location);
9796 /* Note that for now, we don't do anything with c++11 statements
9797 parsed at this level. */
9798 if (std_attrs != NULL_TREE)
9799 warning_at (attrs_location,
9800 OPT_Wattributes,
9801 "attributes at the beginning of statement are ignored");
9804 /* Parse the label for a labeled-statement, i.e.
9806 identifier :
9807 case constant-expression :
9808 default :
9810 GNU Extension:
9811 case constant-expression ... constant-expression : statement
9813 When a label is parsed without errors, the label is added to the
9814 parse tree by the finish_* functions, so this function doesn't
9815 have to return the label. */
9817 static void
9818 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9820 cp_token *token;
9821 tree label = NULL_TREE;
9822 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9824 /* The next token should be an identifier. */
9825 token = cp_lexer_peek_token (parser->lexer);
9826 if (token->type != CPP_NAME
9827 && token->type != CPP_KEYWORD)
9829 cp_parser_error (parser, "expected labeled-statement");
9830 return;
9833 parser->colon_corrects_to_scope_p = false;
9834 switch (token->keyword)
9836 case RID_CASE:
9838 tree expr, expr_hi;
9839 cp_token *ellipsis;
9841 /* Consume the `case' token. */
9842 cp_lexer_consume_token (parser->lexer);
9843 /* Parse the constant-expression. */
9844 expr = cp_parser_constant_expression (parser);
9846 ellipsis = cp_lexer_peek_token (parser->lexer);
9847 if (ellipsis->type == CPP_ELLIPSIS)
9849 /* Consume the `...' token. */
9850 cp_lexer_consume_token (parser->lexer);
9851 expr_hi =
9852 cp_parser_constant_expression (parser);
9854 /* We don't need to emit warnings here, as the common code
9855 will do this for us. */
9857 else
9858 expr_hi = NULL_TREE;
9860 if (parser->in_switch_statement_p)
9861 finish_case_label (token->location, expr, expr_hi);
9862 else
9863 error_at (token->location,
9864 "case label %qE not within a switch statement",
9865 expr);
9867 break;
9869 case RID_DEFAULT:
9870 /* Consume the `default' token. */
9871 cp_lexer_consume_token (parser->lexer);
9873 if (parser->in_switch_statement_p)
9874 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9875 else
9876 error_at (token->location, "case label not within a switch statement");
9877 break;
9879 default:
9880 /* Anything else must be an ordinary label. */
9881 label = finish_label_stmt (cp_parser_identifier (parser));
9882 break;
9885 /* Require the `:' token. */
9886 cp_parser_require (parser, CPP_COLON, RT_COLON);
9888 /* An ordinary label may optionally be followed by attributes.
9889 However, this is only permitted if the attributes are then
9890 followed by a semicolon. This is because, for backward
9891 compatibility, when parsing
9892 lab: __attribute__ ((unused)) int i;
9893 we want the attribute to attach to "i", not "lab". */
9894 if (label != NULL_TREE
9895 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9897 tree attrs;
9898 cp_parser_parse_tentatively (parser);
9899 attrs = cp_parser_gnu_attributes_opt (parser);
9900 if (attrs == NULL_TREE
9901 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9902 cp_parser_abort_tentative_parse (parser);
9903 else if (!cp_parser_parse_definitely (parser))
9905 else
9906 attributes = chainon (attributes, attrs);
9909 if (attributes != NULL_TREE)
9910 cplus_decl_attributes (&label, attributes, 0);
9912 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9915 /* Parse an expression-statement.
9917 expression-statement:
9918 expression [opt] ;
9920 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9921 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9922 indicates whether this expression-statement is part of an
9923 expression statement. */
9925 static tree
9926 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9928 tree statement = NULL_TREE;
9929 cp_token *token = cp_lexer_peek_token (parser->lexer);
9931 /* If the next token is a ';', then there is no expression
9932 statement. */
9933 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9935 statement = cp_parser_expression (parser);
9936 if (statement == error_mark_node
9937 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9939 cp_parser_skip_to_end_of_block_or_statement (parser);
9940 return error_mark_node;
9944 /* Give a helpful message for "A<T>::type t;" and the like. */
9945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9946 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9948 if (TREE_CODE (statement) == SCOPE_REF)
9949 error_at (token->location, "need %<typename%> before %qE because "
9950 "%qT is a dependent scope",
9951 statement, TREE_OPERAND (statement, 0));
9952 else if (is_overloaded_fn (statement)
9953 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9955 /* A::A a; */
9956 tree fn = get_first_fn (statement);
9957 error_at (token->location,
9958 "%<%T::%D%> names the constructor, not the type",
9959 DECL_CONTEXT (fn), DECL_NAME (fn));
9963 /* Consume the final `;'. */
9964 cp_parser_consume_semicolon_at_end_of_statement (parser);
9966 if (in_statement_expr
9967 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9968 /* This is the final expression statement of a statement
9969 expression. */
9970 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9971 else if (statement)
9972 statement = finish_expr_stmt (statement);
9974 return statement;
9977 /* Parse a compound-statement.
9979 compound-statement:
9980 { statement-seq [opt] }
9982 GNU extension:
9984 compound-statement:
9985 { label-declaration-seq [opt] statement-seq [opt] }
9987 label-declaration-seq:
9988 label-declaration
9989 label-declaration-seq label-declaration
9991 Returns a tree representing the statement. */
9993 static tree
9994 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9995 bool in_try, bool function_body)
9997 tree compound_stmt;
9999 /* Consume the `{'. */
10000 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10001 return error_mark_node;
10002 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10003 && !function_body && cxx_dialect < cxx14)
10004 pedwarn (input_location, OPT_Wpedantic,
10005 "compound-statement in constexpr function");
10006 /* Begin the compound-statement. */
10007 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10008 /* If the next keyword is `__label__' we have a label declaration. */
10009 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10010 cp_parser_label_declaration (parser);
10011 /* Parse an (optional) statement-seq. */
10012 cp_parser_statement_seq_opt (parser, in_statement_expr);
10013 /* Finish the compound-statement. */
10014 finish_compound_stmt (compound_stmt);
10015 /* Consume the `}'. */
10016 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10018 return compound_stmt;
10021 /* Parse an (optional) statement-seq.
10023 statement-seq:
10024 statement
10025 statement-seq [opt] statement */
10027 static void
10028 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10030 /* Scan statements until there aren't any more. */
10031 while (true)
10033 cp_token *token = cp_lexer_peek_token (parser->lexer);
10035 /* If we are looking at a `}', then we have run out of
10036 statements; the same is true if we have reached the end
10037 of file, or have stumbled upon a stray '@end'. */
10038 if (token->type == CPP_CLOSE_BRACE
10039 || token->type == CPP_EOF
10040 || token->type == CPP_PRAGMA_EOL
10041 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10042 break;
10044 /* If we are in a compound statement and find 'else' then
10045 something went wrong. */
10046 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10048 if (parser->in_statement & IN_IF_STMT)
10049 break;
10050 else
10052 token = cp_lexer_consume_token (parser->lexer);
10053 error_at (token->location, "%<else%> without a previous %<if%>");
10057 /* Parse the statement. */
10058 cp_parser_statement (parser, in_statement_expr, true, NULL);
10062 /* Parse a selection-statement.
10064 selection-statement:
10065 if ( condition ) statement
10066 if ( condition ) statement else statement
10067 switch ( condition ) statement
10069 Returns the new IF_STMT or SWITCH_STMT.
10071 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10072 is a (possibly labeled) if statement which is not enclosed in
10073 braces and has an else clause. This is used to implement
10074 -Wparentheses. */
10076 static tree
10077 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10079 cp_token *token;
10080 enum rid keyword;
10082 if (if_p != NULL)
10083 *if_p = false;
10085 /* Peek at the next token. */
10086 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10088 /* See what kind of keyword it is. */
10089 keyword = token->keyword;
10090 switch (keyword)
10092 case RID_IF:
10093 case RID_SWITCH:
10095 tree statement;
10096 tree condition;
10098 /* Look for the `('. */
10099 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10101 cp_parser_skip_to_end_of_statement (parser);
10102 return error_mark_node;
10105 /* Begin the selection-statement. */
10106 if (keyword == RID_IF)
10107 statement = begin_if_stmt ();
10108 else
10109 statement = begin_switch_stmt ();
10111 /* Parse the condition. */
10112 condition = cp_parser_condition (parser);
10113 /* Look for the `)'. */
10114 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10115 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10116 /*consume_paren=*/true);
10118 if (keyword == RID_IF)
10120 bool nested_if;
10121 unsigned char in_statement;
10123 /* Add the condition. */
10124 finish_if_stmt_cond (condition, statement);
10126 /* Parse the then-clause. */
10127 in_statement = parser->in_statement;
10128 parser->in_statement |= IN_IF_STMT;
10129 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10131 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10132 add_stmt (build_empty_stmt (loc));
10133 cp_lexer_consume_token (parser->lexer);
10134 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10135 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10136 "empty body in an %<if%> statement");
10137 nested_if = false;
10139 else
10140 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10141 parser->in_statement = in_statement;
10143 finish_then_clause (statement);
10145 /* If the next token is `else', parse the else-clause. */
10146 if (cp_lexer_next_token_is_keyword (parser->lexer,
10147 RID_ELSE))
10149 /* Consume the `else' keyword. */
10150 cp_lexer_consume_token (parser->lexer);
10151 begin_else_clause (statement);
10152 /* Parse the else-clause. */
10153 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10155 location_t loc;
10156 loc = cp_lexer_peek_token (parser->lexer)->location;
10157 warning_at (loc,
10158 OPT_Wempty_body, "suggest braces around "
10159 "empty body in an %<else%> statement");
10160 add_stmt (build_empty_stmt (loc));
10161 cp_lexer_consume_token (parser->lexer);
10163 else
10164 cp_parser_implicitly_scoped_statement (parser, NULL);
10166 finish_else_clause (statement);
10168 /* If we are currently parsing a then-clause, then
10169 IF_P will not be NULL. We set it to true to
10170 indicate that this if statement has an else clause.
10171 This may trigger the Wparentheses warning below
10172 when we get back up to the parent if statement. */
10173 if (if_p != NULL)
10174 *if_p = true;
10176 else
10178 /* This if statement does not have an else clause. If
10179 NESTED_IF is true, then the then-clause is an if
10180 statement which does have an else clause. We warn
10181 about the potential ambiguity. */
10182 if (nested_if)
10183 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10184 "suggest explicit braces to avoid ambiguous"
10185 " %<else%>");
10188 /* Now we're all done with the if-statement. */
10189 finish_if_stmt (statement);
10191 else
10193 bool in_switch_statement_p;
10194 unsigned char in_statement;
10196 /* Add the condition. */
10197 finish_switch_cond (condition, statement);
10199 /* Parse the body of the switch-statement. */
10200 in_switch_statement_p = parser->in_switch_statement_p;
10201 in_statement = parser->in_statement;
10202 parser->in_switch_statement_p = true;
10203 parser->in_statement |= IN_SWITCH_STMT;
10204 cp_parser_implicitly_scoped_statement (parser, NULL);
10205 parser->in_switch_statement_p = in_switch_statement_p;
10206 parser->in_statement = in_statement;
10208 /* Now we're all done with the switch-statement. */
10209 finish_switch_stmt (statement);
10212 return statement;
10214 break;
10216 default:
10217 cp_parser_error (parser, "expected selection-statement");
10218 return error_mark_node;
10222 /* Parse a condition.
10224 condition:
10225 expression
10226 type-specifier-seq declarator = initializer-clause
10227 type-specifier-seq declarator braced-init-list
10229 GNU Extension:
10231 condition:
10232 type-specifier-seq declarator asm-specification [opt]
10233 attributes [opt] = assignment-expression
10235 Returns the expression that should be tested. */
10237 static tree
10238 cp_parser_condition (cp_parser* parser)
10240 cp_decl_specifier_seq type_specifiers;
10241 const char *saved_message;
10242 int declares_class_or_enum;
10244 /* Try the declaration first. */
10245 cp_parser_parse_tentatively (parser);
10246 /* New types are not allowed in the type-specifier-seq for a
10247 condition. */
10248 saved_message = parser->type_definition_forbidden_message;
10249 parser->type_definition_forbidden_message
10250 = G_("types may not be defined in conditions");
10251 /* Parse the type-specifier-seq. */
10252 cp_parser_decl_specifier_seq (parser,
10253 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10254 &type_specifiers,
10255 &declares_class_or_enum);
10256 /* Restore the saved message. */
10257 parser->type_definition_forbidden_message = saved_message;
10258 /* If all is well, we might be looking at a declaration. */
10259 if (!cp_parser_error_occurred (parser))
10261 tree decl;
10262 tree asm_specification;
10263 tree attributes;
10264 cp_declarator *declarator;
10265 tree initializer = NULL_TREE;
10267 /* Parse the declarator. */
10268 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10269 /*ctor_dtor_or_conv_p=*/NULL,
10270 /*parenthesized_p=*/NULL,
10271 /*member_p=*/false,
10272 /*friend_p=*/false);
10273 /* Parse the attributes. */
10274 attributes = cp_parser_attributes_opt (parser);
10275 /* Parse the asm-specification. */
10276 asm_specification = cp_parser_asm_specification_opt (parser);
10277 /* If the next token is not an `=' or '{', then we might still be
10278 looking at an expression. For example:
10280 if (A(a).x)
10282 looks like a decl-specifier-seq and a declarator -- but then
10283 there is no `=', so this is an expression. */
10284 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10285 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10286 cp_parser_simulate_error (parser);
10288 /* If we did see an `=' or '{', then we are looking at a declaration
10289 for sure. */
10290 if (cp_parser_parse_definitely (parser))
10292 tree pushed_scope;
10293 bool non_constant_p;
10294 bool flags = LOOKUP_ONLYCONVERTING;
10296 /* Create the declaration. */
10297 decl = start_decl (declarator, &type_specifiers,
10298 /*initialized_p=*/true,
10299 attributes, /*prefix_attributes=*/NULL_TREE,
10300 &pushed_scope);
10302 /* Parse the initializer. */
10303 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10305 initializer = cp_parser_braced_list (parser, &non_constant_p);
10306 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10307 flags = 0;
10309 else
10311 /* Consume the `='. */
10312 cp_parser_require (parser, CPP_EQ, RT_EQ);
10313 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10315 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10316 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10318 /* Process the initializer. */
10319 cp_finish_decl (decl,
10320 initializer, !non_constant_p,
10321 asm_specification,
10322 flags);
10324 if (pushed_scope)
10325 pop_scope (pushed_scope);
10327 return convert_from_reference (decl);
10330 /* If we didn't even get past the declarator successfully, we are
10331 definitely not looking at a declaration. */
10332 else
10333 cp_parser_abort_tentative_parse (parser);
10335 /* Otherwise, we are looking at an expression. */
10336 return cp_parser_expression (parser);
10339 /* Parses a for-statement or range-for-statement until the closing ')',
10340 not included. */
10342 static tree
10343 cp_parser_for (cp_parser *parser, bool ivdep)
10345 tree init, scope, decl;
10346 bool is_range_for;
10348 /* Begin the for-statement. */
10349 scope = begin_for_scope (&init);
10351 /* Parse the initialization. */
10352 is_range_for = cp_parser_for_init_statement (parser, &decl);
10354 if (is_range_for)
10355 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10356 else
10357 return cp_parser_c_for (parser, scope, init, ivdep);
10360 static tree
10361 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10363 /* Normal for loop */
10364 tree condition = NULL_TREE;
10365 tree expression = NULL_TREE;
10366 tree stmt;
10368 stmt = begin_for_stmt (scope, init);
10369 /* The for-init-statement has already been parsed in
10370 cp_parser_for_init_statement, so no work is needed here. */
10371 finish_for_init_stmt (stmt);
10373 /* If there's a condition, process it. */
10374 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10375 condition = cp_parser_condition (parser);
10376 else if (ivdep)
10378 cp_parser_error (parser, "missing loop condition in loop with "
10379 "%<GCC ivdep%> pragma");
10380 condition = error_mark_node;
10382 finish_for_cond (condition, stmt, ivdep);
10383 /* Look for the `;'. */
10384 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10386 /* If there's an expression, process it. */
10387 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10388 expression = cp_parser_expression (parser);
10389 finish_for_expr (expression, stmt);
10391 return stmt;
10394 /* Tries to parse a range-based for-statement:
10396 range-based-for:
10397 decl-specifier-seq declarator : expression
10399 The decl-specifier-seq declarator and the `:' are already parsed by
10400 cp_parser_for_init_statement. If processing_template_decl it returns a
10401 newly created RANGE_FOR_STMT; if not, it is converted to a
10402 regular FOR_STMT. */
10404 static tree
10405 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10406 bool ivdep)
10408 tree stmt, range_expr;
10410 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10412 bool expr_non_constant_p;
10413 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10415 else
10416 range_expr = cp_parser_expression (parser);
10418 /* If in template, STMT is converted to a normal for-statement
10419 at instantiation. If not, it is done just ahead. */
10420 if (processing_template_decl)
10422 if (check_for_bare_parameter_packs (range_expr))
10423 range_expr = error_mark_node;
10424 stmt = begin_range_for_stmt (scope, init);
10425 if (ivdep)
10426 RANGE_FOR_IVDEP (stmt) = 1;
10427 finish_range_for_decl (stmt, range_decl, range_expr);
10428 if (!type_dependent_expression_p (range_expr)
10429 /* do_auto_deduction doesn't mess with template init-lists. */
10430 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10431 do_range_for_auto_deduction (range_decl, range_expr);
10433 else
10435 stmt = begin_for_stmt (scope, init);
10436 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10438 return stmt;
10441 /* Subroutine of cp_convert_range_for: given the initializer expression,
10442 builds up the range temporary. */
10444 static tree
10445 build_range_temp (tree range_expr)
10447 tree range_type, range_temp;
10449 /* Find out the type deduced by the declaration
10450 `auto &&__range = range_expr'. */
10451 range_type = cp_build_reference_type (make_auto (), true);
10452 range_type = do_auto_deduction (range_type, range_expr,
10453 type_uses_auto (range_type));
10455 /* Create the __range variable. */
10456 range_temp = build_decl (input_location, VAR_DECL,
10457 get_identifier ("__for_range"), range_type);
10458 TREE_USED (range_temp) = 1;
10459 DECL_ARTIFICIAL (range_temp) = 1;
10461 return range_temp;
10464 /* Used by cp_parser_range_for in template context: we aren't going to
10465 do a full conversion yet, but we still need to resolve auto in the
10466 type of the for-range-declaration if present. This is basically
10467 a shortcut version of cp_convert_range_for. */
10469 static void
10470 do_range_for_auto_deduction (tree decl, tree range_expr)
10472 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10473 if (auto_node)
10475 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10476 range_temp = convert_from_reference (build_range_temp (range_expr));
10477 iter_type = (cp_parser_perform_range_for_lookup
10478 (range_temp, &begin_dummy, &end_dummy));
10479 if (iter_type)
10481 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10482 iter_type);
10483 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10484 tf_warning_or_error);
10485 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10486 iter_decl, auto_node);
10491 /* Converts a range-based for-statement into a normal
10492 for-statement, as per the definition.
10494 for (RANGE_DECL : RANGE_EXPR)
10495 BLOCK
10497 should be equivalent to:
10500 auto &&__range = RANGE_EXPR;
10501 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10502 __begin != __end;
10503 ++__begin)
10505 RANGE_DECL = *__begin;
10506 BLOCK
10510 If RANGE_EXPR is an array:
10511 BEGIN_EXPR = __range
10512 END_EXPR = __range + ARRAY_SIZE(__range)
10513 Else if RANGE_EXPR has a member 'begin' or 'end':
10514 BEGIN_EXPR = __range.begin()
10515 END_EXPR = __range.end()
10516 Else:
10517 BEGIN_EXPR = begin(__range)
10518 END_EXPR = end(__range);
10520 If __range has a member 'begin' but not 'end', or vice versa, we must
10521 still use the second alternative (it will surely fail, however).
10522 When calling begin()/end() in the third alternative we must use
10523 argument dependent lookup, but always considering 'std' as an associated
10524 namespace. */
10526 tree
10527 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10528 bool ivdep)
10530 tree begin, end;
10531 tree iter_type, begin_expr, end_expr;
10532 tree condition, expression;
10534 if (range_decl == error_mark_node || range_expr == error_mark_node)
10535 /* If an error happened previously do nothing or else a lot of
10536 unhelpful errors would be issued. */
10537 begin_expr = end_expr = iter_type = error_mark_node;
10538 else
10540 tree range_temp;
10542 if (TREE_CODE (range_expr) == VAR_DECL
10543 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10544 /* Can't bind a reference to an array of runtime bound. */
10545 range_temp = range_expr;
10546 else
10548 range_temp = build_range_temp (range_expr);
10549 pushdecl (range_temp);
10550 cp_finish_decl (range_temp, range_expr,
10551 /*is_constant_init*/false, NULL_TREE,
10552 LOOKUP_ONLYCONVERTING);
10553 range_temp = convert_from_reference (range_temp);
10555 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10556 &begin_expr, &end_expr);
10559 /* The new for initialization statement. */
10560 begin = build_decl (input_location, VAR_DECL,
10561 get_identifier ("__for_begin"), iter_type);
10562 TREE_USED (begin) = 1;
10563 DECL_ARTIFICIAL (begin) = 1;
10564 pushdecl (begin);
10565 cp_finish_decl (begin, begin_expr,
10566 /*is_constant_init*/false, NULL_TREE,
10567 LOOKUP_ONLYCONVERTING);
10569 end = build_decl (input_location, VAR_DECL,
10570 get_identifier ("__for_end"), iter_type);
10571 TREE_USED (end) = 1;
10572 DECL_ARTIFICIAL (end) = 1;
10573 pushdecl (end);
10574 cp_finish_decl (end, end_expr,
10575 /*is_constant_init*/false, NULL_TREE,
10576 LOOKUP_ONLYCONVERTING);
10578 finish_for_init_stmt (statement);
10580 /* The new for condition. */
10581 condition = build_x_binary_op (input_location, NE_EXPR,
10582 begin, ERROR_MARK,
10583 end, ERROR_MARK,
10584 NULL, tf_warning_or_error);
10585 finish_for_cond (condition, statement, ivdep);
10587 /* The new increment expression. */
10588 expression = finish_unary_op_expr (input_location,
10589 PREINCREMENT_EXPR, begin,
10590 tf_warning_or_error);
10591 finish_for_expr (expression, statement);
10593 /* The declaration is initialized with *__begin inside the loop body. */
10594 cp_finish_decl (range_decl,
10595 build_x_indirect_ref (input_location, begin, RO_NULL,
10596 tf_warning_or_error),
10597 /*is_constant_init*/false, NULL_TREE,
10598 LOOKUP_ONLYCONVERTING);
10600 return statement;
10603 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10604 We need to solve both at the same time because the method used
10605 depends on the existence of members begin or end.
10606 Returns the type deduced for the iterator expression. */
10608 static tree
10609 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10611 if (error_operand_p (range))
10613 *begin = *end = error_mark_node;
10614 return error_mark_node;
10617 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10619 error ("range-based %<for%> expression of type %qT "
10620 "has incomplete type", TREE_TYPE (range));
10621 *begin = *end = error_mark_node;
10622 return error_mark_node;
10624 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10626 /* If RANGE is an array, we will use pointer arithmetic. */
10627 *begin = range;
10628 *end = build_binary_op (input_location, PLUS_EXPR,
10629 range,
10630 array_type_nelts_top (TREE_TYPE (range)),
10632 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10634 else
10636 /* If it is not an array, we must do a bit of magic. */
10637 tree id_begin, id_end;
10638 tree member_begin, member_end;
10640 *begin = *end = error_mark_node;
10642 id_begin = get_identifier ("begin");
10643 id_end = get_identifier ("end");
10644 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10645 /*protect=*/2, /*want_type=*/false,
10646 tf_warning_or_error);
10647 member_end = lookup_member (TREE_TYPE (range), id_end,
10648 /*protect=*/2, /*want_type=*/false,
10649 tf_warning_or_error);
10651 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10653 /* Use the member functions. */
10654 if (member_begin != NULL_TREE)
10655 *begin = cp_parser_range_for_member_function (range, id_begin);
10656 else
10657 error ("range-based %<for%> expression of type %qT has an "
10658 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10660 if (member_end != NULL_TREE)
10661 *end = cp_parser_range_for_member_function (range, id_end);
10662 else
10663 error ("range-based %<for%> expression of type %qT has a "
10664 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10666 else
10668 /* Use global functions with ADL. */
10669 vec<tree, va_gc> *vec;
10670 vec = make_tree_vector ();
10672 vec_safe_push (vec, range);
10674 member_begin = perform_koenig_lookup (id_begin, vec,
10675 tf_warning_or_error);
10676 *begin = finish_call_expr (member_begin, &vec, false, true,
10677 tf_warning_or_error);
10678 member_end = perform_koenig_lookup (id_end, vec,
10679 tf_warning_or_error);
10680 *end = finish_call_expr (member_end, &vec, false, true,
10681 tf_warning_or_error);
10683 release_tree_vector (vec);
10686 /* Last common checks. */
10687 if (*begin == error_mark_node || *end == error_mark_node)
10689 /* If one of the expressions is an error do no more checks. */
10690 *begin = *end = error_mark_node;
10691 return error_mark_node;
10693 else if (type_dependent_expression_p (*begin)
10694 || type_dependent_expression_p (*end))
10695 /* Can happen, when, eg, in a template context, Koenig lookup
10696 can't resolve begin/end (c++/58503). */
10697 return NULL_TREE;
10698 else
10700 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10701 /* The unqualified type of the __begin and __end temporaries should
10702 be the same, as required by the multiple auto declaration. */
10703 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10704 error ("inconsistent begin/end types in range-based %<for%> "
10705 "statement: %qT and %qT",
10706 TREE_TYPE (*begin), TREE_TYPE (*end));
10707 return iter_type;
10712 /* Helper function for cp_parser_perform_range_for_lookup.
10713 Builds a tree for RANGE.IDENTIFIER(). */
10715 static tree
10716 cp_parser_range_for_member_function (tree range, tree identifier)
10718 tree member, res;
10719 vec<tree, va_gc> *vec;
10721 member = finish_class_member_access_expr (range, identifier,
10722 false, tf_warning_or_error);
10723 if (member == error_mark_node)
10724 return error_mark_node;
10726 vec = make_tree_vector ();
10727 res = finish_call_expr (member, &vec,
10728 /*disallow_virtual=*/false,
10729 /*koenig_p=*/false,
10730 tf_warning_or_error);
10731 release_tree_vector (vec);
10732 return res;
10735 /* Parse an iteration-statement.
10737 iteration-statement:
10738 while ( condition ) statement
10739 do statement while ( expression ) ;
10740 for ( for-init-statement condition [opt] ; expression [opt] )
10741 statement
10743 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10745 static tree
10746 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10748 cp_token *token;
10749 enum rid keyword;
10750 tree statement;
10751 unsigned char in_statement;
10753 /* Peek at the next token. */
10754 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10755 if (!token)
10756 return error_mark_node;
10758 /* Remember whether or not we are already within an iteration
10759 statement. */
10760 in_statement = parser->in_statement;
10762 /* See what kind of keyword it is. */
10763 keyword = token->keyword;
10764 switch (keyword)
10766 case RID_WHILE:
10768 tree condition;
10770 /* Begin the while-statement. */
10771 statement = begin_while_stmt ();
10772 /* Look for the `('. */
10773 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10774 /* Parse the condition. */
10775 condition = cp_parser_condition (parser);
10776 finish_while_stmt_cond (condition, statement, ivdep);
10777 /* Look for the `)'. */
10778 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10779 /* Parse the dependent statement. */
10780 parser->in_statement = IN_ITERATION_STMT;
10781 cp_parser_already_scoped_statement (parser);
10782 parser->in_statement = in_statement;
10783 /* We're done with the while-statement. */
10784 finish_while_stmt (statement);
10786 break;
10788 case RID_DO:
10790 tree expression;
10792 /* Begin the do-statement. */
10793 statement = begin_do_stmt ();
10794 /* Parse the body of the do-statement. */
10795 parser->in_statement = IN_ITERATION_STMT;
10796 cp_parser_implicitly_scoped_statement (parser, NULL);
10797 parser->in_statement = in_statement;
10798 finish_do_body (statement);
10799 /* Look for the `while' keyword. */
10800 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10801 /* Look for the `('. */
10802 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10803 /* Parse the expression. */
10804 expression = cp_parser_expression (parser);
10805 /* We're done with the do-statement. */
10806 finish_do_stmt (expression, statement, ivdep);
10807 /* Look for the `)'. */
10808 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10809 /* Look for the `;'. */
10810 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10812 break;
10814 case RID_FOR:
10816 /* Look for the `('. */
10817 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10819 statement = cp_parser_for (parser, ivdep);
10821 /* Look for the `)'. */
10822 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10824 /* Parse the body of the for-statement. */
10825 parser->in_statement = IN_ITERATION_STMT;
10826 cp_parser_already_scoped_statement (parser);
10827 parser->in_statement = in_statement;
10829 /* We're done with the for-statement. */
10830 finish_for_stmt (statement);
10832 break;
10834 default:
10835 cp_parser_error (parser, "expected iteration-statement");
10836 statement = error_mark_node;
10837 break;
10840 return statement;
10843 /* Parse a for-init-statement or the declarator of a range-based-for.
10844 Returns true if a range-based-for declaration is seen.
10846 for-init-statement:
10847 expression-statement
10848 simple-declaration */
10850 static bool
10851 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10853 /* If the next token is a `;', then we have an empty
10854 expression-statement. Grammatically, this is also a
10855 simple-declaration, but an invalid one, because it does not
10856 declare anything. Therefore, if we did not handle this case
10857 specially, we would issue an error message about an invalid
10858 declaration. */
10859 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10861 bool is_range_for = false;
10862 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10864 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10865 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10867 /* N3994 -- for (id : init) ... */
10868 if (cxx_dialect < cxx1z)
10869 pedwarn (input_location, 0, "range-based for loop without a "
10870 "type-specifier only available with "
10871 "-std=c++1z or -std=gnu++1z");
10872 tree name = cp_parser_identifier (parser);
10873 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10874 *decl = build_decl (input_location, VAR_DECL, name, type);
10875 pushdecl (*decl);
10876 cp_lexer_consume_token (parser->lexer);
10877 return true;
10880 /* A colon is used in range-based for. */
10881 parser->colon_corrects_to_scope_p = false;
10883 /* We're going to speculatively look for a declaration, falling back
10884 to an expression, if necessary. */
10885 cp_parser_parse_tentatively (parser);
10886 /* Parse the declaration. */
10887 cp_parser_simple_declaration (parser,
10888 /*function_definition_allowed_p=*/false,
10889 decl);
10890 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10891 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10893 /* It is a range-for, consume the ':' */
10894 cp_lexer_consume_token (parser->lexer);
10895 is_range_for = true;
10896 if (cxx_dialect < cxx11)
10898 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10899 "range-based %<for%> loops only available with "
10900 "-std=c++11 or -std=gnu++11");
10901 *decl = error_mark_node;
10904 else
10905 /* The ';' is not consumed yet because we told
10906 cp_parser_simple_declaration not to. */
10907 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10909 if (cp_parser_parse_definitely (parser))
10910 return is_range_for;
10911 /* If the tentative parse failed, then we shall need to look for an
10912 expression-statement. */
10914 /* If we are here, it is an expression-statement. */
10915 cp_parser_expression_statement (parser, NULL_TREE);
10916 return false;
10919 /* Parse a jump-statement.
10921 jump-statement:
10922 break ;
10923 continue ;
10924 return expression [opt] ;
10925 return braced-init-list ;
10926 goto identifier ;
10928 GNU extension:
10930 jump-statement:
10931 goto * expression ;
10933 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10935 static tree
10936 cp_parser_jump_statement (cp_parser* parser)
10938 tree statement = error_mark_node;
10939 cp_token *token;
10940 enum rid keyword;
10941 unsigned char in_statement;
10943 /* Peek at the next token. */
10944 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10945 if (!token)
10946 return error_mark_node;
10948 /* See what kind of keyword it is. */
10949 keyword = token->keyword;
10950 switch (keyword)
10952 case RID_BREAK:
10953 in_statement = parser->in_statement & ~IN_IF_STMT;
10954 switch (in_statement)
10956 case 0:
10957 error_at (token->location, "break statement not within loop or switch");
10958 break;
10959 default:
10960 gcc_assert ((in_statement & IN_SWITCH_STMT)
10961 || in_statement == IN_ITERATION_STMT);
10962 statement = finish_break_stmt ();
10963 if (in_statement == IN_ITERATION_STMT)
10964 break_maybe_infinite_loop ();
10965 break;
10966 case IN_OMP_BLOCK:
10967 error_at (token->location, "invalid exit from OpenMP structured block");
10968 break;
10969 case IN_OMP_FOR:
10970 error_at (token->location, "break statement used with OpenMP for loop");
10971 break;
10972 case IN_CILK_SIMD_FOR:
10973 error_at (token->location, "break statement used with Cilk Plus for loop");
10974 break;
10976 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10977 break;
10979 case RID_CONTINUE:
10980 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10982 case 0:
10983 error_at (token->location, "continue statement not within a loop");
10984 break;
10985 case IN_CILK_SIMD_FOR:
10986 error_at (token->location,
10987 "continue statement within %<#pragma simd%> loop body");
10988 /* Fall through. */
10989 case IN_ITERATION_STMT:
10990 case IN_OMP_FOR:
10991 statement = finish_continue_stmt ();
10992 break;
10993 case IN_OMP_BLOCK:
10994 error_at (token->location, "invalid exit from OpenMP structured block");
10995 break;
10996 default:
10997 gcc_unreachable ();
10999 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11000 break;
11002 case RID_RETURN:
11004 tree expr;
11005 bool expr_non_constant_p;
11007 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11009 cp_lexer_set_source_position (parser->lexer);
11010 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11011 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11013 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11014 expr = cp_parser_expression (parser);
11015 else
11016 /* If the next token is a `;', then there is no
11017 expression. */
11018 expr = NULL_TREE;
11019 /* Build the return-statement. */
11020 statement = finish_return_stmt (expr);
11021 /* Look for the final `;'. */
11022 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11024 break;
11026 case RID_GOTO:
11027 if (parser->in_function_body
11028 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11029 error ("%<goto%> in %<constexpr%> function");
11031 /* Create the goto-statement. */
11032 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11034 /* Issue a warning about this use of a GNU extension. */
11035 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11036 /* Consume the '*' token. */
11037 cp_lexer_consume_token (parser->lexer);
11038 /* Parse the dependent expression. */
11039 finish_goto_stmt (cp_parser_expression (parser));
11041 else
11042 finish_goto_stmt (cp_parser_identifier (parser));
11043 /* Look for the final `;'. */
11044 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11045 break;
11047 default:
11048 cp_parser_error (parser, "expected jump-statement");
11049 break;
11052 return statement;
11055 /* Parse a declaration-statement.
11057 declaration-statement:
11058 block-declaration */
11060 static void
11061 cp_parser_declaration_statement (cp_parser* parser)
11063 void *p;
11065 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11066 p = obstack_alloc (&declarator_obstack, 0);
11068 /* Parse the block-declaration. */
11069 cp_parser_block_declaration (parser, /*statement_p=*/true);
11071 /* Free any declarators allocated. */
11072 obstack_free (&declarator_obstack, p);
11075 /* Some dependent statements (like `if (cond) statement'), are
11076 implicitly in their own scope. In other words, if the statement is
11077 a single statement (as opposed to a compound-statement), it is
11078 none-the-less treated as if it were enclosed in braces. Any
11079 declarations appearing in the dependent statement are out of scope
11080 after control passes that point. This function parses a statement,
11081 but ensures that is in its own scope, even if it is not a
11082 compound-statement.
11084 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11085 is a (possibly labeled) if statement which is not enclosed in
11086 braces and has an else clause. This is used to implement
11087 -Wparentheses.
11089 Returns the new statement. */
11091 static tree
11092 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11094 tree statement;
11096 if (if_p != NULL)
11097 *if_p = false;
11099 /* Mark if () ; with a special NOP_EXPR. */
11100 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11102 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11103 cp_lexer_consume_token (parser->lexer);
11104 statement = add_stmt (build_empty_stmt (loc));
11106 /* if a compound is opened, we simply parse the statement directly. */
11107 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11108 statement = cp_parser_compound_statement (parser, NULL, false, false);
11109 /* If the token is not a `{', then we must take special action. */
11110 else
11112 /* Create a compound-statement. */
11113 statement = begin_compound_stmt (0);
11114 /* Parse the dependent-statement. */
11115 cp_parser_statement (parser, NULL_TREE, false, if_p);
11116 /* Finish the dummy compound-statement. */
11117 finish_compound_stmt (statement);
11120 /* Return the statement. */
11121 return statement;
11124 /* For some dependent statements (like `while (cond) statement'), we
11125 have already created a scope. Therefore, even if the dependent
11126 statement is a compound-statement, we do not want to create another
11127 scope. */
11129 static void
11130 cp_parser_already_scoped_statement (cp_parser* parser)
11132 /* If the token is a `{', then we must take special action. */
11133 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11134 cp_parser_statement (parser, NULL_TREE, false, NULL);
11135 else
11137 /* Avoid calling cp_parser_compound_statement, so that we
11138 don't create a new scope. Do everything else by hand. */
11139 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11140 /* If the next keyword is `__label__' we have a label declaration. */
11141 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11142 cp_parser_label_declaration (parser);
11143 /* Parse an (optional) statement-seq. */
11144 cp_parser_statement_seq_opt (parser, NULL_TREE);
11145 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11149 /* Declarations [gram.dcl.dcl] */
11151 /* Parse an optional declaration-sequence.
11153 declaration-seq:
11154 declaration
11155 declaration-seq declaration */
11157 static void
11158 cp_parser_declaration_seq_opt (cp_parser* parser)
11160 while (true)
11162 cp_token *token;
11164 token = cp_lexer_peek_token (parser->lexer);
11166 if (token->type == CPP_CLOSE_BRACE
11167 || token->type == CPP_EOF
11168 || token->type == CPP_PRAGMA_EOL)
11169 break;
11171 if (token->type == CPP_SEMICOLON)
11173 /* A declaration consisting of a single semicolon is
11174 invalid. Allow it unless we're being pedantic. */
11175 cp_lexer_consume_token (parser->lexer);
11176 if (!in_system_header_at (input_location))
11177 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11178 continue;
11181 /* If we're entering or exiting a region that's implicitly
11182 extern "C", modify the lang context appropriately. */
11183 if (!parser->implicit_extern_c && token->implicit_extern_c)
11185 push_lang_context (lang_name_c);
11186 parser->implicit_extern_c = true;
11188 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11190 pop_lang_context ();
11191 parser->implicit_extern_c = false;
11194 if (token->type == CPP_PRAGMA)
11196 /* A top-level declaration can consist solely of a #pragma.
11197 A nested declaration cannot, so this is done here and not
11198 in cp_parser_declaration. (A #pragma at block scope is
11199 handled in cp_parser_statement.) */
11200 cp_parser_pragma (parser, pragma_external);
11201 continue;
11204 /* Parse the declaration itself. */
11205 cp_parser_declaration (parser);
11209 /* Parse a declaration.
11211 declaration:
11212 block-declaration
11213 function-definition
11214 template-declaration
11215 explicit-instantiation
11216 explicit-specialization
11217 linkage-specification
11218 namespace-definition
11220 GNU extension:
11222 declaration:
11223 __extension__ declaration */
11225 static void
11226 cp_parser_declaration (cp_parser* parser)
11228 cp_token token1;
11229 cp_token token2;
11230 int saved_pedantic;
11231 void *p;
11232 tree attributes = NULL_TREE;
11234 /* Check for the `__extension__' keyword. */
11235 if (cp_parser_extension_opt (parser, &saved_pedantic))
11237 /* Parse the qualified declaration. */
11238 cp_parser_declaration (parser);
11239 /* Restore the PEDANTIC flag. */
11240 pedantic = saved_pedantic;
11242 return;
11245 /* Try to figure out what kind of declaration is present. */
11246 token1 = *cp_lexer_peek_token (parser->lexer);
11248 if (token1.type != CPP_EOF)
11249 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11250 else
11252 token2.type = CPP_EOF;
11253 token2.keyword = RID_MAX;
11256 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11257 p = obstack_alloc (&declarator_obstack, 0);
11259 /* If the next token is `extern' and the following token is a string
11260 literal, then we have a linkage specification. */
11261 if (token1.keyword == RID_EXTERN
11262 && cp_parser_is_pure_string_literal (&token2))
11263 cp_parser_linkage_specification (parser);
11264 /* If the next token is `template', then we have either a template
11265 declaration, an explicit instantiation, or an explicit
11266 specialization. */
11267 else if (token1.keyword == RID_TEMPLATE)
11269 /* `template <>' indicates a template specialization. */
11270 if (token2.type == CPP_LESS
11271 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11272 cp_parser_explicit_specialization (parser);
11273 /* `template <' indicates a template declaration. */
11274 else if (token2.type == CPP_LESS)
11275 cp_parser_template_declaration (parser, /*member_p=*/false);
11276 /* Anything else must be an explicit instantiation. */
11277 else
11278 cp_parser_explicit_instantiation (parser);
11280 /* If the next token is `export', then we have a template
11281 declaration. */
11282 else if (token1.keyword == RID_EXPORT)
11283 cp_parser_template_declaration (parser, /*member_p=*/false);
11284 /* If the next token is `extern', 'static' or 'inline' and the one
11285 after that is `template', we have a GNU extended explicit
11286 instantiation directive. */
11287 else if (cp_parser_allow_gnu_extensions_p (parser)
11288 && (token1.keyword == RID_EXTERN
11289 || token1.keyword == RID_STATIC
11290 || token1.keyword == RID_INLINE)
11291 && token2.keyword == RID_TEMPLATE)
11292 cp_parser_explicit_instantiation (parser);
11293 /* If the next token is `namespace', check for a named or unnamed
11294 namespace definition. */
11295 else if (token1.keyword == RID_NAMESPACE
11296 && (/* A named namespace definition. */
11297 (token2.type == CPP_NAME
11298 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11299 != CPP_EQ))
11300 /* An unnamed namespace definition. */
11301 || token2.type == CPP_OPEN_BRACE
11302 || token2.keyword == RID_ATTRIBUTE))
11303 cp_parser_namespace_definition (parser);
11304 /* An inline (associated) namespace definition. */
11305 else if (token1.keyword == RID_INLINE
11306 && token2.keyword == RID_NAMESPACE)
11307 cp_parser_namespace_definition (parser);
11308 /* Objective-C++ declaration/definition. */
11309 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11310 cp_parser_objc_declaration (parser, NULL_TREE);
11311 else if (c_dialect_objc ()
11312 && token1.keyword == RID_ATTRIBUTE
11313 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11314 cp_parser_objc_declaration (parser, attributes);
11315 /* We must have either a block declaration or a function
11316 definition. */
11317 else
11318 /* Try to parse a block-declaration, or a function-definition. */
11319 cp_parser_block_declaration (parser, /*statement_p=*/false);
11321 /* Free any declarators allocated. */
11322 obstack_free (&declarator_obstack, p);
11325 /* Parse a block-declaration.
11327 block-declaration:
11328 simple-declaration
11329 asm-definition
11330 namespace-alias-definition
11331 using-declaration
11332 using-directive
11334 GNU Extension:
11336 block-declaration:
11337 __extension__ block-declaration
11339 C++0x Extension:
11341 block-declaration:
11342 static_assert-declaration
11344 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11345 part of a declaration-statement. */
11347 static void
11348 cp_parser_block_declaration (cp_parser *parser,
11349 bool statement_p)
11351 cp_token *token1;
11352 int saved_pedantic;
11354 /* Check for the `__extension__' keyword. */
11355 if (cp_parser_extension_opt (parser, &saved_pedantic))
11357 /* Parse the qualified declaration. */
11358 cp_parser_block_declaration (parser, statement_p);
11359 /* Restore the PEDANTIC flag. */
11360 pedantic = saved_pedantic;
11362 return;
11365 /* Peek at the next token to figure out which kind of declaration is
11366 present. */
11367 token1 = cp_lexer_peek_token (parser->lexer);
11369 /* If the next keyword is `asm', we have an asm-definition. */
11370 if (token1->keyword == RID_ASM)
11372 if (statement_p)
11373 cp_parser_commit_to_tentative_parse (parser);
11374 cp_parser_asm_definition (parser);
11376 /* If the next keyword is `namespace', we have a
11377 namespace-alias-definition. */
11378 else if (token1->keyword == RID_NAMESPACE)
11379 cp_parser_namespace_alias_definition (parser);
11380 /* If the next keyword is `using', we have a
11381 using-declaration, a using-directive, or an alias-declaration. */
11382 else if (token1->keyword == RID_USING)
11384 cp_token *token2;
11386 if (statement_p)
11387 cp_parser_commit_to_tentative_parse (parser);
11388 /* If the token after `using' is `namespace', then we have a
11389 using-directive. */
11390 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11391 if (token2->keyword == RID_NAMESPACE)
11392 cp_parser_using_directive (parser);
11393 /* If the second token after 'using' is '=', then we have an
11394 alias-declaration. */
11395 else if (cxx_dialect >= cxx11
11396 && token2->type == CPP_NAME
11397 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11398 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11399 cp_parser_alias_declaration (parser);
11400 /* Otherwise, it's a using-declaration. */
11401 else
11402 cp_parser_using_declaration (parser,
11403 /*access_declaration_p=*/false);
11405 /* If the next keyword is `__label__' we have a misplaced label
11406 declaration. */
11407 else if (token1->keyword == RID_LABEL)
11409 cp_lexer_consume_token (parser->lexer);
11410 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11411 cp_parser_skip_to_end_of_statement (parser);
11412 /* If the next token is now a `;', consume it. */
11413 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11414 cp_lexer_consume_token (parser->lexer);
11416 /* If the next token is `static_assert' we have a static assertion. */
11417 else if (token1->keyword == RID_STATIC_ASSERT)
11418 cp_parser_static_assert (parser, /*member_p=*/false);
11419 /* Anything else must be a simple-declaration. */
11420 else
11421 cp_parser_simple_declaration (parser, !statement_p,
11422 /*maybe_range_for_decl*/NULL);
11425 /* Parse a simple-declaration.
11427 simple-declaration:
11428 decl-specifier-seq [opt] init-declarator-list [opt] ;
11430 init-declarator-list:
11431 init-declarator
11432 init-declarator-list , init-declarator
11434 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11435 function-definition as a simple-declaration.
11437 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11438 parsed declaration if it is an uninitialized single declarator not followed
11439 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11440 if present, will not be consumed. */
11442 static void
11443 cp_parser_simple_declaration (cp_parser* parser,
11444 bool function_definition_allowed_p,
11445 tree *maybe_range_for_decl)
11447 cp_decl_specifier_seq decl_specifiers;
11448 int declares_class_or_enum;
11449 bool saw_declarator;
11451 if (maybe_range_for_decl)
11452 *maybe_range_for_decl = NULL_TREE;
11454 /* Defer access checks until we know what is being declared; the
11455 checks for names appearing in the decl-specifier-seq should be
11456 done as if we were in the scope of the thing being declared. */
11457 push_deferring_access_checks (dk_deferred);
11459 /* Parse the decl-specifier-seq. We have to keep track of whether
11460 or not the decl-specifier-seq declares a named class or
11461 enumeration type, since that is the only case in which the
11462 init-declarator-list is allowed to be empty.
11464 [dcl.dcl]
11466 In a simple-declaration, the optional init-declarator-list can be
11467 omitted only when declaring a class or enumeration, that is when
11468 the decl-specifier-seq contains either a class-specifier, an
11469 elaborated-type-specifier, or an enum-specifier. */
11470 cp_parser_decl_specifier_seq (parser,
11471 CP_PARSER_FLAGS_OPTIONAL,
11472 &decl_specifiers,
11473 &declares_class_or_enum);
11474 /* We no longer need to defer access checks. */
11475 stop_deferring_access_checks ();
11477 /* In a block scope, a valid declaration must always have a
11478 decl-specifier-seq. By not trying to parse declarators, we can
11479 resolve the declaration/expression ambiguity more quickly. */
11480 if (!function_definition_allowed_p
11481 && !decl_specifiers.any_specifiers_p)
11483 cp_parser_error (parser, "expected declaration");
11484 goto done;
11487 /* If the next two tokens are both identifiers, the code is
11488 erroneous. The usual cause of this situation is code like:
11490 T t;
11492 where "T" should name a type -- but does not. */
11493 if (!decl_specifiers.any_type_specifiers_p
11494 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11496 /* If parsing tentatively, we should commit; we really are
11497 looking at a declaration. */
11498 cp_parser_commit_to_tentative_parse (parser);
11499 /* Give up. */
11500 goto done;
11503 /* If we have seen at least one decl-specifier, and the next token
11504 is not a parenthesis, then we must be looking at a declaration.
11505 (After "int (" we might be looking at a functional cast.) */
11506 if (decl_specifiers.any_specifiers_p
11507 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11508 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11509 && !cp_parser_error_occurred (parser))
11510 cp_parser_commit_to_tentative_parse (parser);
11512 /* Keep going until we hit the `;' at the end of the simple
11513 declaration. */
11514 saw_declarator = false;
11515 while (cp_lexer_next_token_is_not (parser->lexer,
11516 CPP_SEMICOLON))
11518 cp_token *token;
11519 bool function_definition_p;
11520 tree decl;
11522 if (saw_declarator)
11524 /* If we are processing next declarator, coma is expected */
11525 token = cp_lexer_peek_token (parser->lexer);
11526 gcc_assert (token->type == CPP_COMMA);
11527 cp_lexer_consume_token (parser->lexer);
11528 if (maybe_range_for_decl)
11529 *maybe_range_for_decl = error_mark_node;
11531 else
11532 saw_declarator = true;
11534 /* Parse the init-declarator. */
11535 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11536 /*checks=*/NULL,
11537 function_definition_allowed_p,
11538 /*member_p=*/false,
11539 declares_class_or_enum,
11540 &function_definition_p,
11541 maybe_range_for_decl);
11542 /* If an error occurred while parsing tentatively, exit quickly.
11543 (That usually happens when in the body of a function; each
11544 statement is treated as a declaration-statement until proven
11545 otherwise.) */
11546 if (cp_parser_error_occurred (parser))
11547 goto done;
11548 /* Handle function definitions specially. */
11549 if (function_definition_p)
11551 /* If the next token is a `,', then we are probably
11552 processing something like:
11554 void f() {}, *p;
11556 which is erroneous. */
11557 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11559 cp_token *token = cp_lexer_peek_token (parser->lexer);
11560 error_at (token->location,
11561 "mixing"
11562 " declarations and function-definitions is forbidden");
11564 /* Otherwise, we're done with the list of declarators. */
11565 else
11567 pop_deferring_access_checks ();
11568 return;
11571 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11572 *maybe_range_for_decl = decl;
11573 /* The next token should be either a `,' or a `;'. */
11574 token = cp_lexer_peek_token (parser->lexer);
11575 /* If it's a `,', there are more declarators to come. */
11576 if (token->type == CPP_COMMA)
11577 /* will be consumed next time around */;
11578 /* If it's a `;', we are done. */
11579 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11580 break;
11581 /* Anything else is an error. */
11582 else
11584 /* If we have already issued an error message we don't need
11585 to issue another one. */
11586 if (decl != error_mark_node
11587 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11588 cp_parser_error (parser, "expected %<,%> or %<;%>");
11589 /* Skip tokens until we reach the end of the statement. */
11590 cp_parser_skip_to_end_of_statement (parser);
11591 /* If the next token is now a `;', consume it. */
11592 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11593 cp_lexer_consume_token (parser->lexer);
11594 goto done;
11596 /* After the first time around, a function-definition is not
11597 allowed -- even if it was OK at first. For example:
11599 int i, f() {}
11601 is not valid. */
11602 function_definition_allowed_p = false;
11605 /* Issue an error message if no declarators are present, and the
11606 decl-specifier-seq does not itself declare a class or
11607 enumeration: [dcl.dcl]/3. */
11608 if (!saw_declarator)
11610 if (cp_parser_declares_only_class_p (parser))
11612 if (!declares_class_or_enum
11613 && decl_specifiers.type
11614 && OVERLOAD_TYPE_P (decl_specifiers.type))
11615 /* Ensure an error is issued anyway when finish_decltype_type,
11616 called via cp_parser_decl_specifier_seq, returns a class or
11617 an enumeration (c++/51786). */
11618 decl_specifiers.type = NULL_TREE;
11619 shadow_tag (&decl_specifiers);
11621 /* Perform any deferred access checks. */
11622 perform_deferred_access_checks (tf_warning_or_error);
11625 /* Consume the `;'. */
11626 if (!maybe_range_for_decl)
11627 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11629 done:
11630 pop_deferring_access_checks ();
11633 /* Parse a decl-specifier-seq.
11635 decl-specifier-seq:
11636 decl-specifier-seq [opt] decl-specifier
11637 decl-specifier attribute-specifier-seq [opt] (C++11)
11639 decl-specifier:
11640 storage-class-specifier
11641 type-specifier
11642 function-specifier
11643 friend
11644 typedef
11646 GNU Extension:
11648 decl-specifier:
11649 attributes
11651 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11653 The parser flags FLAGS is used to control type-specifier parsing.
11655 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11656 flags:
11658 1: one of the decl-specifiers is an elaborated-type-specifier
11659 (i.e., a type declaration)
11660 2: one of the decl-specifiers is an enum-specifier or a
11661 class-specifier (i.e., a type definition)
11665 static void
11666 cp_parser_decl_specifier_seq (cp_parser* parser,
11667 cp_parser_flags flags,
11668 cp_decl_specifier_seq *decl_specs,
11669 int* declares_class_or_enum)
11671 bool constructor_possible_p = !parser->in_declarator_p;
11672 bool found_decl_spec = false;
11673 cp_token *start_token = NULL;
11674 cp_decl_spec ds;
11676 /* Clear DECL_SPECS. */
11677 clear_decl_specs (decl_specs);
11679 /* Assume no class or enumeration type is declared. */
11680 *declares_class_or_enum = 0;
11682 /* Keep reading specifiers until there are no more to read. */
11683 while (true)
11685 bool constructor_p;
11686 cp_token *token;
11687 ds = ds_last;
11689 /* Peek at the next token. */
11690 token = cp_lexer_peek_token (parser->lexer);
11692 /* Save the first token of the decl spec list for error
11693 reporting. */
11694 if (!start_token)
11695 start_token = token;
11696 /* Handle attributes. */
11697 if (cp_next_tokens_can_be_attribute_p (parser))
11699 /* Parse the attributes. */
11700 tree attrs = cp_parser_attributes_opt (parser);
11702 /* In a sequence of declaration specifiers, c++11 attributes
11703 appertain to the type that precede them. In that case
11704 [dcl.spec]/1 says:
11706 The attribute-specifier-seq affects the type only for
11707 the declaration it appears in, not other declarations
11708 involving the same type.
11710 But for now let's force the user to position the
11711 attribute either at the beginning of the declaration or
11712 after the declarator-id, which would clearly mean that it
11713 applies to the declarator. */
11714 if (cxx11_attribute_p (attrs))
11716 if (!found_decl_spec)
11717 /* The c++11 attribute is at the beginning of the
11718 declaration. It appertains to the entity being
11719 declared. */;
11720 else
11722 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11724 /* This is an attribute following a
11725 class-specifier. */
11726 if (decl_specs->type_definition_p)
11727 warn_misplaced_attr_for_class_type (token->location,
11728 decl_specs->type);
11729 attrs = NULL_TREE;
11731 else
11733 decl_specs->std_attributes
11734 = chainon (decl_specs->std_attributes,
11735 attrs);
11736 if (decl_specs->locations[ds_std_attribute] == 0)
11737 decl_specs->locations[ds_std_attribute] = token->location;
11739 continue;
11743 decl_specs->attributes
11744 = chainon (decl_specs->attributes,
11745 attrs);
11746 if (decl_specs->locations[ds_attribute] == 0)
11747 decl_specs->locations[ds_attribute] = token->location;
11748 continue;
11750 /* Assume we will find a decl-specifier keyword. */
11751 found_decl_spec = true;
11752 /* If the next token is an appropriate keyword, we can simply
11753 add it to the list. */
11754 switch (token->keyword)
11756 /* decl-specifier:
11757 friend
11758 constexpr */
11759 case RID_FRIEND:
11760 if (!at_class_scope_p ())
11762 error_at (token->location, "%<friend%> used outside of class");
11763 cp_lexer_purge_token (parser->lexer);
11765 else
11767 ds = ds_friend;
11768 /* Consume the token. */
11769 cp_lexer_consume_token (parser->lexer);
11771 break;
11773 case RID_CONSTEXPR:
11774 ds = ds_constexpr;
11775 cp_lexer_consume_token (parser->lexer);
11776 break;
11778 /* function-specifier:
11779 inline
11780 virtual
11781 explicit */
11782 case RID_INLINE:
11783 case RID_VIRTUAL:
11784 case RID_EXPLICIT:
11785 cp_parser_function_specifier_opt (parser, decl_specs);
11786 break;
11788 /* decl-specifier:
11789 typedef */
11790 case RID_TYPEDEF:
11791 ds = ds_typedef;
11792 /* Consume the token. */
11793 cp_lexer_consume_token (parser->lexer);
11794 /* A constructor declarator cannot appear in a typedef. */
11795 constructor_possible_p = false;
11796 /* The "typedef" keyword can only occur in a declaration; we
11797 may as well commit at this point. */
11798 cp_parser_commit_to_tentative_parse (parser);
11800 if (decl_specs->storage_class != sc_none)
11801 decl_specs->conflicting_specifiers_p = true;
11802 break;
11804 /* storage-class-specifier:
11805 auto
11806 register
11807 static
11808 extern
11809 mutable
11811 GNU Extension:
11812 thread */
11813 case RID_AUTO:
11814 if (cxx_dialect == cxx98)
11816 /* Consume the token. */
11817 cp_lexer_consume_token (parser->lexer);
11819 /* Complain about `auto' as a storage specifier, if
11820 we're complaining about C++0x compatibility. */
11821 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11822 " changes meaning in C++11; please remove it");
11824 /* Set the storage class anyway. */
11825 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11826 token);
11828 else
11829 /* C++0x auto type-specifier. */
11830 found_decl_spec = false;
11831 break;
11833 case RID_REGISTER:
11834 case RID_STATIC:
11835 case RID_EXTERN:
11836 case RID_MUTABLE:
11837 /* Consume the token. */
11838 cp_lexer_consume_token (parser->lexer);
11839 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11840 token);
11841 break;
11842 case RID_THREAD:
11843 /* Consume the token. */
11844 ds = ds_thread;
11845 cp_lexer_consume_token (parser->lexer);
11846 break;
11848 default:
11849 /* We did not yet find a decl-specifier yet. */
11850 found_decl_spec = false;
11851 break;
11854 if (found_decl_spec
11855 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11856 && token->keyword != RID_CONSTEXPR)
11857 error ("decl-specifier invalid in condition");
11859 if (ds != ds_last)
11860 set_and_check_decl_spec_loc (decl_specs, ds, token);
11862 /* Constructors are a special case. The `S' in `S()' is not a
11863 decl-specifier; it is the beginning of the declarator. */
11864 constructor_p
11865 = (!found_decl_spec
11866 && constructor_possible_p
11867 && (cp_parser_constructor_declarator_p
11868 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11870 /* If we don't have a DECL_SPEC yet, then we must be looking at
11871 a type-specifier. */
11872 if (!found_decl_spec && !constructor_p)
11874 int decl_spec_declares_class_or_enum;
11875 bool is_cv_qualifier;
11876 tree type_spec;
11878 type_spec
11879 = cp_parser_type_specifier (parser, flags,
11880 decl_specs,
11881 /*is_declaration=*/true,
11882 &decl_spec_declares_class_or_enum,
11883 &is_cv_qualifier);
11884 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11886 /* If this type-specifier referenced a user-defined type
11887 (a typedef, class-name, etc.), then we can't allow any
11888 more such type-specifiers henceforth.
11890 [dcl.spec]
11892 The longest sequence of decl-specifiers that could
11893 possibly be a type name is taken as the
11894 decl-specifier-seq of a declaration. The sequence shall
11895 be self-consistent as described below.
11897 [dcl.type]
11899 As a general rule, at most one type-specifier is allowed
11900 in the complete decl-specifier-seq of a declaration. The
11901 only exceptions are the following:
11903 -- const or volatile can be combined with any other
11904 type-specifier.
11906 -- signed or unsigned can be combined with char, long,
11907 short, or int.
11909 -- ..
11911 Example:
11913 typedef char* Pc;
11914 void g (const int Pc);
11916 Here, Pc is *not* part of the decl-specifier seq; it's
11917 the declarator. Therefore, once we see a type-specifier
11918 (other than a cv-qualifier), we forbid any additional
11919 user-defined types. We *do* still allow things like `int
11920 int' to be considered a decl-specifier-seq, and issue the
11921 error message later. */
11922 if (type_spec && !is_cv_qualifier)
11923 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11924 /* A constructor declarator cannot follow a type-specifier. */
11925 if (type_spec)
11927 constructor_possible_p = false;
11928 found_decl_spec = true;
11929 if (!is_cv_qualifier)
11930 decl_specs->any_type_specifiers_p = true;
11934 /* If we still do not have a DECL_SPEC, then there are no more
11935 decl-specifiers. */
11936 if (!found_decl_spec)
11937 break;
11939 decl_specs->any_specifiers_p = true;
11940 /* After we see one decl-specifier, further decl-specifiers are
11941 always optional. */
11942 flags |= CP_PARSER_FLAGS_OPTIONAL;
11945 /* Don't allow a friend specifier with a class definition. */
11946 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11947 && (*declares_class_or_enum & 2))
11948 error_at (decl_specs->locations[ds_friend],
11949 "class definition may not be declared a friend");
11952 /* Parse an (optional) storage-class-specifier.
11954 storage-class-specifier:
11955 auto
11956 register
11957 static
11958 extern
11959 mutable
11961 GNU Extension:
11963 storage-class-specifier:
11964 thread
11966 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11968 static tree
11969 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11971 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11973 case RID_AUTO:
11974 if (cxx_dialect != cxx98)
11975 return NULL_TREE;
11976 /* Fall through for C++98. */
11978 case RID_REGISTER:
11979 case RID_STATIC:
11980 case RID_EXTERN:
11981 case RID_MUTABLE:
11982 case RID_THREAD:
11983 /* Consume the token. */
11984 return cp_lexer_consume_token (parser->lexer)->u.value;
11986 default:
11987 return NULL_TREE;
11991 /* Parse an (optional) function-specifier.
11993 function-specifier:
11994 inline
11995 virtual
11996 explicit
11998 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11999 Updates DECL_SPECS, if it is non-NULL. */
12001 static tree
12002 cp_parser_function_specifier_opt (cp_parser* parser,
12003 cp_decl_specifier_seq *decl_specs)
12005 cp_token *token = cp_lexer_peek_token (parser->lexer);
12006 switch (token->keyword)
12008 case RID_INLINE:
12009 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12010 break;
12012 case RID_VIRTUAL:
12013 /* 14.5.2.3 [temp.mem]
12015 A member function template shall not be virtual. */
12016 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12017 error_at (token->location, "templates may not be %<virtual%>");
12018 else
12019 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12020 break;
12022 case RID_EXPLICIT:
12023 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12024 break;
12026 default:
12027 return NULL_TREE;
12030 /* Consume the token. */
12031 return cp_lexer_consume_token (parser->lexer)->u.value;
12034 /* Parse a linkage-specification.
12036 linkage-specification:
12037 extern string-literal { declaration-seq [opt] }
12038 extern string-literal declaration */
12040 static void
12041 cp_parser_linkage_specification (cp_parser* parser)
12043 tree linkage;
12045 /* Look for the `extern' keyword. */
12046 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12048 /* Look for the string-literal. */
12049 linkage = cp_parser_string_literal (parser, false, false);
12051 /* Transform the literal into an identifier. If the literal is a
12052 wide-character string, or contains embedded NULs, then we can't
12053 handle it as the user wants. */
12054 if (strlen (TREE_STRING_POINTER (linkage))
12055 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12057 cp_parser_error (parser, "invalid linkage-specification");
12058 /* Assume C++ linkage. */
12059 linkage = lang_name_cplusplus;
12061 else
12062 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12064 /* We're now using the new linkage. */
12065 push_lang_context (linkage);
12067 /* If the next token is a `{', then we're using the first
12068 production. */
12069 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12071 cp_ensure_no_omp_declare_simd (parser);
12073 /* Consume the `{' token. */
12074 cp_lexer_consume_token (parser->lexer);
12075 /* Parse the declarations. */
12076 cp_parser_declaration_seq_opt (parser);
12077 /* Look for the closing `}'. */
12078 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12080 /* Otherwise, there's just one declaration. */
12081 else
12083 bool saved_in_unbraced_linkage_specification_p;
12085 saved_in_unbraced_linkage_specification_p
12086 = parser->in_unbraced_linkage_specification_p;
12087 parser->in_unbraced_linkage_specification_p = true;
12088 cp_parser_declaration (parser);
12089 parser->in_unbraced_linkage_specification_p
12090 = saved_in_unbraced_linkage_specification_p;
12093 /* We're done with the linkage-specification. */
12094 pop_lang_context ();
12097 /* Parse a static_assert-declaration.
12099 static_assert-declaration:
12100 static_assert ( constant-expression , string-literal ) ;
12102 If MEMBER_P, this static_assert is a class member. */
12104 static void
12105 cp_parser_static_assert(cp_parser *parser, bool member_p)
12107 tree condition;
12108 tree message;
12109 cp_token *token;
12110 location_t saved_loc;
12111 bool dummy;
12113 /* Peek at the `static_assert' token so we can keep track of exactly
12114 where the static assertion started. */
12115 token = cp_lexer_peek_token (parser->lexer);
12116 saved_loc = token->location;
12118 /* Look for the `static_assert' keyword. */
12119 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12120 RT_STATIC_ASSERT))
12121 return;
12123 /* We know we are in a static assertion; commit to any tentative
12124 parse. */
12125 if (cp_parser_parsing_tentatively (parser))
12126 cp_parser_commit_to_tentative_parse (parser);
12128 /* Parse the `(' starting the static assertion condition. */
12129 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12131 /* Parse the constant-expression. Allow a non-constant expression
12132 here in order to give better diagnostics in finish_static_assert. */
12133 condition =
12134 cp_parser_constant_expression (parser,
12135 /*allow_non_constant_p=*/true,
12136 /*non_constant_p=*/&dummy);
12138 /* Parse the separating `,'. */
12139 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12141 /* Parse the string-literal message. */
12142 message = cp_parser_string_literal (parser,
12143 /*translate=*/false,
12144 /*wide_ok=*/true);
12146 /* A `)' completes the static assertion. */
12147 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12148 cp_parser_skip_to_closing_parenthesis (parser,
12149 /*recovering=*/true,
12150 /*or_comma=*/false,
12151 /*consume_paren=*/true);
12153 /* A semicolon terminates the declaration. */
12154 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12156 /* Complete the static assertion, which may mean either processing
12157 the static assert now or saving it for template instantiation. */
12158 finish_static_assert (condition, message, saved_loc, member_p);
12161 /* Parse the expression in decltype ( expression ). */
12163 static tree
12164 cp_parser_decltype_expr (cp_parser *parser,
12165 bool &id_expression_or_member_access_p)
12167 cp_token *id_expr_start_token;
12168 tree expr;
12170 /* First, try parsing an id-expression. */
12171 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12172 cp_parser_parse_tentatively (parser);
12173 expr = cp_parser_id_expression (parser,
12174 /*template_keyword_p=*/false,
12175 /*check_dependency_p=*/true,
12176 /*template_p=*/NULL,
12177 /*declarator_p=*/false,
12178 /*optional_p=*/false);
12180 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12182 bool non_integral_constant_expression_p = false;
12183 tree id_expression = expr;
12184 cp_id_kind idk;
12185 const char *error_msg;
12187 if (identifier_p (expr))
12188 /* Lookup the name we got back from the id-expression. */
12189 expr = cp_parser_lookup_name_simple (parser, expr,
12190 id_expr_start_token->location);
12192 if (expr
12193 && expr != error_mark_node
12194 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
12195 && TREE_CODE (expr) != TYPE_DECL
12196 && (TREE_CODE (expr) != BIT_NOT_EXPR
12197 || !TYPE_P (TREE_OPERAND (expr, 0)))
12198 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12200 /* Complete lookup of the id-expression. */
12201 expr = (finish_id_expression
12202 (id_expression, expr, parser->scope, &idk,
12203 /*integral_constant_expression_p=*/false,
12204 /*allow_non_integral_constant_expression_p=*/true,
12205 &non_integral_constant_expression_p,
12206 /*template_p=*/false,
12207 /*done=*/true,
12208 /*address_p=*/false,
12209 /*template_arg_p=*/false,
12210 &error_msg,
12211 id_expr_start_token->location));
12213 if (expr == error_mark_node)
12214 /* We found an id-expression, but it was something that we
12215 should not have found. This is an error, not something
12216 we can recover from, so note that we found an
12217 id-expression and we'll recover as gracefully as
12218 possible. */
12219 id_expression_or_member_access_p = true;
12222 if (expr
12223 && expr != error_mark_node
12224 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12225 /* We have an id-expression. */
12226 id_expression_or_member_access_p = true;
12229 if (!id_expression_or_member_access_p)
12231 /* Abort the id-expression parse. */
12232 cp_parser_abort_tentative_parse (parser);
12234 /* Parsing tentatively, again. */
12235 cp_parser_parse_tentatively (parser);
12237 /* Parse a class member access. */
12238 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12239 /*cast_p=*/false, /*decltype*/true,
12240 /*member_access_only_p=*/true, NULL);
12242 if (expr
12243 && expr != error_mark_node
12244 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12245 /* We have an id-expression. */
12246 id_expression_or_member_access_p = true;
12249 if (id_expression_or_member_access_p)
12250 /* We have parsed the complete id-expression or member access. */
12251 cp_parser_parse_definitely (parser);
12252 else
12254 /* Abort our attempt to parse an id-expression or member access
12255 expression. */
12256 cp_parser_abort_tentative_parse (parser);
12258 /* Parse a full expression. */
12259 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12260 /*decltype_p=*/true);
12263 return expr;
12266 /* Parse a `decltype' type. Returns the type.
12268 simple-type-specifier:
12269 decltype ( expression )
12270 C++14 proposal:
12271 decltype ( auto ) */
12273 static tree
12274 cp_parser_decltype (cp_parser *parser)
12276 tree expr;
12277 bool id_expression_or_member_access_p = false;
12278 const char *saved_message;
12279 bool saved_integral_constant_expression_p;
12280 bool saved_non_integral_constant_expression_p;
12281 bool saved_greater_than_is_operator_p;
12282 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12284 if (start_token->type == CPP_DECLTYPE)
12286 /* Already parsed. */
12287 cp_lexer_consume_token (parser->lexer);
12288 return start_token->u.value;
12291 /* Look for the `decltype' token. */
12292 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12293 return error_mark_node;
12295 /* Parse the opening `('. */
12296 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12297 return error_mark_node;
12299 /* decltype (auto) */
12300 if (cxx_dialect >= cxx14
12301 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12303 cp_lexer_consume_token (parser->lexer);
12304 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12305 return error_mark_node;
12306 expr = make_decltype_auto ();
12307 AUTO_IS_DECLTYPE (expr) = true;
12308 goto rewrite;
12311 /* Types cannot be defined in a `decltype' expression. Save away the
12312 old message. */
12313 saved_message = parser->type_definition_forbidden_message;
12315 /* And create the new one. */
12316 parser->type_definition_forbidden_message
12317 = G_("types may not be defined in %<decltype%> expressions");
12319 /* The restrictions on constant-expressions do not apply inside
12320 decltype expressions. */
12321 saved_integral_constant_expression_p
12322 = parser->integral_constant_expression_p;
12323 saved_non_integral_constant_expression_p
12324 = parser->non_integral_constant_expression_p;
12325 parser->integral_constant_expression_p = false;
12327 /* Within a parenthesized expression, a `>' token is always
12328 the greater-than operator. */
12329 saved_greater_than_is_operator_p
12330 = parser->greater_than_is_operator_p;
12331 parser->greater_than_is_operator_p = true;
12333 /* Do not actually evaluate the expression. */
12334 ++cp_unevaluated_operand;
12336 /* Do not warn about problems with the expression. */
12337 ++c_inhibit_evaluation_warnings;
12339 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12341 /* Go back to evaluating expressions. */
12342 --cp_unevaluated_operand;
12343 --c_inhibit_evaluation_warnings;
12345 /* The `>' token might be the end of a template-id or
12346 template-parameter-list now. */
12347 parser->greater_than_is_operator_p
12348 = saved_greater_than_is_operator_p;
12350 /* Restore the old message and the integral constant expression
12351 flags. */
12352 parser->type_definition_forbidden_message = saved_message;
12353 parser->integral_constant_expression_p
12354 = saved_integral_constant_expression_p;
12355 parser->non_integral_constant_expression_p
12356 = saved_non_integral_constant_expression_p;
12358 /* Parse to the closing `)'. */
12359 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12361 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12362 /*consume_paren=*/true);
12363 return error_mark_node;
12366 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12367 tf_warning_or_error);
12369 rewrite:
12370 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12371 it again. */
12372 start_token->type = CPP_DECLTYPE;
12373 start_token->u.value = expr;
12374 start_token->keyword = RID_MAX;
12375 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12377 return expr;
12380 /* Special member functions [gram.special] */
12382 /* Parse a conversion-function-id.
12384 conversion-function-id:
12385 operator conversion-type-id
12387 Returns an IDENTIFIER_NODE representing the operator. */
12389 static tree
12390 cp_parser_conversion_function_id (cp_parser* parser)
12392 tree type;
12393 tree saved_scope;
12394 tree saved_qualifying_scope;
12395 tree saved_object_scope;
12396 tree pushed_scope = NULL_TREE;
12398 /* Look for the `operator' token. */
12399 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12400 return error_mark_node;
12401 /* When we parse the conversion-type-id, the current scope will be
12402 reset. However, we need that information in able to look up the
12403 conversion function later, so we save it here. */
12404 saved_scope = parser->scope;
12405 saved_qualifying_scope = parser->qualifying_scope;
12406 saved_object_scope = parser->object_scope;
12407 /* We must enter the scope of the class so that the names of
12408 entities declared within the class are available in the
12409 conversion-type-id. For example, consider:
12411 struct S {
12412 typedef int I;
12413 operator I();
12416 S::operator I() { ... }
12418 In order to see that `I' is a type-name in the definition, we
12419 must be in the scope of `S'. */
12420 if (saved_scope)
12421 pushed_scope = push_scope (saved_scope);
12422 /* Parse the conversion-type-id. */
12423 type = cp_parser_conversion_type_id (parser);
12424 /* Leave the scope of the class, if any. */
12425 if (pushed_scope)
12426 pop_scope (pushed_scope);
12427 /* Restore the saved scope. */
12428 parser->scope = saved_scope;
12429 parser->qualifying_scope = saved_qualifying_scope;
12430 parser->object_scope = saved_object_scope;
12431 /* If the TYPE is invalid, indicate failure. */
12432 if (type == error_mark_node)
12433 return error_mark_node;
12434 return mangle_conv_op_name_for_type (type);
12437 /* Parse a conversion-type-id:
12439 conversion-type-id:
12440 type-specifier-seq conversion-declarator [opt]
12442 Returns the TYPE specified. */
12444 static tree
12445 cp_parser_conversion_type_id (cp_parser* parser)
12447 tree attributes;
12448 cp_decl_specifier_seq type_specifiers;
12449 cp_declarator *declarator;
12450 tree type_specified;
12451 const char *saved_message;
12453 /* Parse the attributes. */
12454 attributes = cp_parser_attributes_opt (parser);
12456 saved_message = parser->type_definition_forbidden_message;
12457 parser->type_definition_forbidden_message
12458 = G_("types may not be defined in a conversion-type-id");
12460 /* Parse the type-specifiers. */
12461 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12462 /*is_trailing_return=*/false,
12463 &type_specifiers);
12465 parser->type_definition_forbidden_message = saved_message;
12467 /* If that didn't work, stop. */
12468 if (type_specifiers.type == error_mark_node)
12469 return error_mark_node;
12470 /* Parse the conversion-declarator. */
12471 declarator = cp_parser_conversion_declarator_opt (parser);
12473 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12474 /*initialized=*/0, &attributes);
12475 if (attributes)
12476 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12478 /* Don't give this error when parsing tentatively. This happens to
12479 work because we always parse this definitively once. */
12480 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12481 && type_uses_auto (type_specified))
12483 if (cxx_dialect < cxx14)
12485 error ("invalid use of %<auto%> in conversion operator");
12486 return error_mark_node;
12488 else if (template_parm_scope_p ())
12489 warning (0, "use of %<auto%> in member template "
12490 "conversion operator can never be deduced");
12493 return type_specified;
12496 /* Parse an (optional) conversion-declarator.
12498 conversion-declarator:
12499 ptr-operator conversion-declarator [opt]
12503 static cp_declarator *
12504 cp_parser_conversion_declarator_opt (cp_parser* parser)
12506 enum tree_code code;
12507 tree class_type, std_attributes = NULL_TREE;
12508 cp_cv_quals cv_quals;
12510 /* We don't know if there's a ptr-operator next, or not. */
12511 cp_parser_parse_tentatively (parser);
12512 /* Try the ptr-operator. */
12513 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12514 &std_attributes);
12515 /* If it worked, look for more conversion-declarators. */
12516 if (cp_parser_parse_definitely (parser))
12518 cp_declarator *declarator;
12520 /* Parse another optional declarator. */
12521 declarator = cp_parser_conversion_declarator_opt (parser);
12523 declarator = cp_parser_make_indirect_declarator
12524 (code, class_type, cv_quals, declarator, std_attributes);
12526 return declarator;
12529 return NULL;
12532 /* Parse an (optional) ctor-initializer.
12534 ctor-initializer:
12535 : mem-initializer-list
12537 Returns TRUE iff the ctor-initializer was actually present. */
12539 static bool
12540 cp_parser_ctor_initializer_opt (cp_parser* parser)
12542 /* If the next token is not a `:', then there is no
12543 ctor-initializer. */
12544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12546 /* Do default initialization of any bases and members. */
12547 if (DECL_CONSTRUCTOR_P (current_function_decl))
12548 finish_mem_initializers (NULL_TREE);
12550 return false;
12553 /* Consume the `:' token. */
12554 cp_lexer_consume_token (parser->lexer);
12555 /* And the mem-initializer-list. */
12556 cp_parser_mem_initializer_list (parser);
12558 return true;
12561 /* Parse a mem-initializer-list.
12563 mem-initializer-list:
12564 mem-initializer ... [opt]
12565 mem-initializer ... [opt] , mem-initializer-list */
12567 static void
12568 cp_parser_mem_initializer_list (cp_parser* parser)
12570 tree mem_initializer_list = NULL_TREE;
12571 tree target_ctor = error_mark_node;
12572 cp_token *token = cp_lexer_peek_token (parser->lexer);
12574 /* Let the semantic analysis code know that we are starting the
12575 mem-initializer-list. */
12576 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12577 error_at (token->location,
12578 "only constructors take member initializers");
12580 /* Loop through the list. */
12581 while (true)
12583 tree mem_initializer;
12585 token = cp_lexer_peek_token (parser->lexer);
12586 /* Parse the mem-initializer. */
12587 mem_initializer = cp_parser_mem_initializer (parser);
12588 /* If the next token is a `...', we're expanding member initializers. */
12589 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12591 /* Consume the `...'. */
12592 cp_lexer_consume_token (parser->lexer);
12594 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12595 can be expanded but members cannot. */
12596 if (mem_initializer != error_mark_node
12597 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12599 error_at (token->location,
12600 "cannot expand initializer for member %<%D%>",
12601 TREE_PURPOSE (mem_initializer));
12602 mem_initializer = error_mark_node;
12605 /* Construct the pack expansion type. */
12606 if (mem_initializer != error_mark_node)
12607 mem_initializer = make_pack_expansion (mem_initializer);
12609 if (target_ctor != error_mark_node
12610 && mem_initializer != error_mark_node)
12612 error ("mem-initializer for %qD follows constructor delegation",
12613 TREE_PURPOSE (mem_initializer));
12614 mem_initializer = error_mark_node;
12616 /* Look for a target constructor. */
12617 if (mem_initializer != error_mark_node
12618 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12619 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12621 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12622 if (mem_initializer_list)
12624 error ("constructor delegation follows mem-initializer for %qD",
12625 TREE_PURPOSE (mem_initializer_list));
12626 mem_initializer = error_mark_node;
12628 target_ctor = mem_initializer;
12630 /* Add it to the list, unless it was erroneous. */
12631 if (mem_initializer != error_mark_node)
12633 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12634 mem_initializer_list = mem_initializer;
12636 /* If the next token is not a `,', we're done. */
12637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12638 break;
12639 /* Consume the `,' token. */
12640 cp_lexer_consume_token (parser->lexer);
12643 /* Perform semantic analysis. */
12644 if (DECL_CONSTRUCTOR_P (current_function_decl))
12645 finish_mem_initializers (mem_initializer_list);
12648 /* Parse a mem-initializer.
12650 mem-initializer:
12651 mem-initializer-id ( expression-list [opt] )
12652 mem-initializer-id braced-init-list
12654 GNU extension:
12656 mem-initializer:
12657 ( expression-list [opt] )
12659 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12660 class) or FIELD_DECL (for a non-static data member) to initialize;
12661 the TREE_VALUE is the expression-list. An empty initialization
12662 list is represented by void_list_node. */
12664 static tree
12665 cp_parser_mem_initializer (cp_parser* parser)
12667 tree mem_initializer_id;
12668 tree expression_list;
12669 tree member;
12670 cp_token *token = cp_lexer_peek_token (parser->lexer);
12672 /* Find out what is being initialized. */
12673 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12675 permerror (token->location,
12676 "anachronistic old-style base class initializer");
12677 mem_initializer_id = NULL_TREE;
12679 else
12681 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12682 if (mem_initializer_id == error_mark_node)
12683 return mem_initializer_id;
12685 member = expand_member_init (mem_initializer_id);
12686 if (member && !DECL_P (member))
12687 in_base_initializer = 1;
12689 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12691 bool expr_non_constant_p;
12692 cp_lexer_set_source_position (parser->lexer);
12693 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12694 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12695 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12696 expression_list = build_tree_list (NULL_TREE, expression_list);
12698 else
12700 vec<tree, va_gc> *vec;
12701 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12702 /*cast_p=*/false,
12703 /*allow_expansion_p=*/true,
12704 /*non_constant_p=*/NULL);
12705 if (vec == NULL)
12706 return error_mark_node;
12707 expression_list = build_tree_list_vec (vec);
12708 release_tree_vector (vec);
12711 if (expression_list == error_mark_node)
12712 return error_mark_node;
12713 if (!expression_list)
12714 expression_list = void_type_node;
12716 in_base_initializer = 0;
12718 return member ? build_tree_list (member, expression_list) : error_mark_node;
12721 /* Parse a mem-initializer-id.
12723 mem-initializer-id:
12724 :: [opt] nested-name-specifier [opt] class-name
12725 identifier
12727 Returns a TYPE indicating the class to be initializer for the first
12728 production. Returns an IDENTIFIER_NODE indicating the data member
12729 to be initialized for the second production. */
12731 static tree
12732 cp_parser_mem_initializer_id (cp_parser* parser)
12734 bool global_scope_p;
12735 bool nested_name_specifier_p;
12736 bool template_p = false;
12737 tree id;
12739 cp_token *token = cp_lexer_peek_token (parser->lexer);
12741 /* `typename' is not allowed in this context ([temp.res]). */
12742 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12744 error_at (token->location,
12745 "keyword %<typename%> not allowed in this context (a qualified "
12746 "member initializer is implicitly a type)");
12747 cp_lexer_consume_token (parser->lexer);
12749 /* Look for the optional `::' operator. */
12750 global_scope_p
12751 = (cp_parser_global_scope_opt (parser,
12752 /*current_scope_valid_p=*/false)
12753 != NULL_TREE);
12754 /* Look for the optional nested-name-specifier. The simplest way to
12755 implement:
12757 [temp.res]
12759 The keyword `typename' is not permitted in a base-specifier or
12760 mem-initializer; in these contexts a qualified name that
12761 depends on a template-parameter is implicitly assumed to be a
12762 type name.
12764 is to assume that we have seen the `typename' keyword at this
12765 point. */
12766 nested_name_specifier_p
12767 = (cp_parser_nested_name_specifier_opt (parser,
12768 /*typename_keyword_p=*/true,
12769 /*check_dependency_p=*/true,
12770 /*type_p=*/true,
12771 /*is_declaration=*/true)
12772 != NULL_TREE);
12773 if (nested_name_specifier_p)
12774 template_p = cp_parser_optional_template_keyword (parser);
12775 /* If there is a `::' operator or a nested-name-specifier, then we
12776 are definitely looking for a class-name. */
12777 if (global_scope_p || nested_name_specifier_p)
12778 return cp_parser_class_name (parser,
12779 /*typename_keyword_p=*/true,
12780 /*template_keyword_p=*/template_p,
12781 typename_type,
12782 /*check_dependency_p=*/true,
12783 /*class_head_p=*/false,
12784 /*is_declaration=*/true);
12785 /* Otherwise, we could also be looking for an ordinary identifier. */
12786 cp_parser_parse_tentatively (parser);
12787 /* Try a class-name. */
12788 id = cp_parser_class_name (parser,
12789 /*typename_keyword_p=*/true,
12790 /*template_keyword_p=*/false,
12791 none_type,
12792 /*check_dependency_p=*/true,
12793 /*class_head_p=*/false,
12794 /*is_declaration=*/true);
12795 /* If we found one, we're done. */
12796 if (cp_parser_parse_definitely (parser))
12797 return id;
12798 /* Otherwise, look for an ordinary identifier. */
12799 return cp_parser_identifier (parser);
12802 /* Overloading [gram.over] */
12804 /* Parse an operator-function-id.
12806 operator-function-id:
12807 operator operator
12809 Returns an IDENTIFIER_NODE for the operator which is a
12810 human-readable spelling of the identifier, e.g., `operator +'. */
12812 static tree
12813 cp_parser_operator_function_id (cp_parser* parser)
12815 /* Look for the `operator' keyword. */
12816 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12817 return error_mark_node;
12818 /* And then the name of the operator itself. */
12819 return cp_parser_operator (parser);
12822 /* Return an identifier node for a user-defined literal operator.
12823 The suffix identifier is chained to the operator name identifier. */
12825 static tree
12826 cp_literal_operator_id (const char* name)
12828 tree identifier;
12829 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12830 + strlen (name) + 10);
12831 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12832 identifier = get_identifier (buffer);
12834 return identifier;
12837 /* Parse an operator.
12839 operator:
12840 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12841 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12842 || ++ -- , ->* -> () []
12844 GNU Extensions:
12846 operator:
12847 <? >? <?= >?=
12849 Returns an IDENTIFIER_NODE for the operator which is a
12850 human-readable spelling of the identifier, e.g., `operator +'. */
12852 static tree
12853 cp_parser_operator (cp_parser* parser)
12855 tree id = NULL_TREE;
12856 cp_token *token;
12857 bool utf8 = false;
12859 /* Peek at the next token. */
12860 token = cp_lexer_peek_token (parser->lexer);
12861 /* Figure out which operator we have. */
12862 switch (token->type)
12864 case CPP_KEYWORD:
12866 enum tree_code op;
12868 /* The keyword should be either `new' or `delete'. */
12869 if (token->keyword == RID_NEW)
12870 op = NEW_EXPR;
12871 else if (token->keyword == RID_DELETE)
12872 op = DELETE_EXPR;
12873 else
12874 break;
12876 /* Consume the `new' or `delete' token. */
12877 cp_lexer_consume_token (parser->lexer);
12879 /* Peek at the next token. */
12880 token = cp_lexer_peek_token (parser->lexer);
12881 /* If it's a `[' token then this is the array variant of the
12882 operator. */
12883 if (token->type == CPP_OPEN_SQUARE)
12885 /* Consume the `[' token. */
12886 cp_lexer_consume_token (parser->lexer);
12887 /* Look for the `]' token. */
12888 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12889 id = ansi_opname (op == NEW_EXPR
12890 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12892 /* Otherwise, we have the non-array variant. */
12893 else
12894 id = ansi_opname (op);
12896 return id;
12899 case CPP_PLUS:
12900 id = ansi_opname (PLUS_EXPR);
12901 break;
12903 case CPP_MINUS:
12904 id = ansi_opname (MINUS_EXPR);
12905 break;
12907 case CPP_MULT:
12908 id = ansi_opname (MULT_EXPR);
12909 break;
12911 case CPP_DIV:
12912 id = ansi_opname (TRUNC_DIV_EXPR);
12913 break;
12915 case CPP_MOD:
12916 id = ansi_opname (TRUNC_MOD_EXPR);
12917 break;
12919 case CPP_XOR:
12920 id = ansi_opname (BIT_XOR_EXPR);
12921 break;
12923 case CPP_AND:
12924 id = ansi_opname (BIT_AND_EXPR);
12925 break;
12927 case CPP_OR:
12928 id = ansi_opname (BIT_IOR_EXPR);
12929 break;
12931 case CPP_COMPL:
12932 id = ansi_opname (BIT_NOT_EXPR);
12933 break;
12935 case CPP_NOT:
12936 id = ansi_opname (TRUTH_NOT_EXPR);
12937 break;
12939 case CPP_EQ:
12940 id = ansi_assopname (NOP_EXPR);
12941 break;
12943 case CPP_LESS:
12944 id = ansi_opname (LT_EXPR);
12945 break;
12947 case CPP_GREATER:
12948 id = ansi_opname (GT_EXPR);
12949 break;
12951 case CPP_PLUS_EQ:
12952 id = ansi_assopname (PLUS_EXPR);
12953 break;
12955 case CPP_MINUS_EQ:
12956 id = ansi_assopname (MINUS_EXPR);
12957 break;
12959 case CPP_MULT_EQ:
12960 id = ansi_assopname (MULT_EXPR);
12961 break;
12963 case CPP_DIV_EQ:
12964 id = ansi_assopname (TRUNC_DIV_EXPR);
12965 break;
12967 case CPP_MOD_EQ:
12968 id = ansi_assopname (TRUNC_MOD_EXPR);
12969 break;
12971 case CPP_XOR_EQ:
12972 id = ansi_assopname (BIT_XOR_EXPR);
12973 break;
12975 case CPP_AND_EQ:
12976 id = ansi_assopname (BIT_AND_EXPR);
12977 break;
12979 case CPP_OR_EQ:
12980 id = ansi_assopname (BIT_IOR_EXPR);
12981 break;
12983 case CPP_LSHIFT:
12984 id = ansi_opname (LSHIFT_EXPR);
12985 break;
12987 case CPP_RSHIFT:
12988 id = ansi_opname (RSHIFT_EXPR);
12989 break;
12991 case CPP_LSHIFT_EQ:
12992 id = ansi_assopname (LSHIFT_EXPR);
12993 break;
12995 case CPP_RSHIFT_EQ:
12996 id = ansi_assopname (RSHIFT_EXPR);
12997 break;
12999 case CPP_EQ_EQ:
13000 id = ansi_opname (EQ_EXPR);
13001 break;
13003 case CPP_NOT_EQ:
13004 id = ansi_opname (NE_EXPR);
13005 break;
13007 case CPP_LESS_EQ:
13008 id = ansi_opname (LE_EXPR);
13009 break;
13011 case CPP_GREATER_EQ:
13012 id = ansi_opname (GE_EXPR);
13013 break;
13015 case CPP_AND_AND:
13016 id = ansi_opname (TRUTH_ANDIF_EXPR);
13017 break;
13019 case CPP_OR_OR:
13020 id = ansi_opname (TRUTH_ORIF_EXPR);
13021 break;
13023 case CPP_PLUS_PLUS:
13024 id = ansi_opname (POSTINCREMENT_EXPR);
13025 break;
13027 case CPP_MINUS_MINUS:
13028 id = ansi_opname (PREDECREMENT_EXPR);
13029 break;
13031 case CPP_COMMA:
13032 id = ansi_opname (COMPOUND_EXPR);
13033 break;
13035 case CPP_DEREF_STAR:
13036 id = ansi_opname (MEMBER_REF);
13037 break;
13039 case CPP_DEREF:
13040 id = ansi_opname (COMPONENT_REF);
13041 break;
13043 case CPP_OPEN_PAREN:
13044 /* Consume the `('. */
13045 cp_lexer_consume_token (parser->lexer);
13046 /* Look for the matching `)'. */
13047 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13048 return ansi_opname (CALL_EXPR);
13050 case CPP_OPEN_SQUARE:
13051 /* Consume the `['. */
13052 cp_lexer_consume_token (parser->lexer);
13053 /* Look for the matching `]'. */
13054 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13055 return ansi_opname (ARRAY_REF);
13057 case CPP_UTF8STRING:
13058 case CPP_UTF8STRING_USERDEF:
13059 utf8 = true;
13060 case CPP_STRING:
13061 case CPP_WSTRING:
13062 case CPP_STRING16:
13063 case CPP_STRING32:
13064 case CPP_STRING_USERDEF:
13065 case CPP_WSTRING_USERDEF:
13066 case CPP_STRING16_USERDEF:
13067 case CPP_STRING32_USERDEF:
13069 tree str, string_tree;
13070 int sz, len;
13072 if (cxx_dialect == cxx98)
13073 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13075 /* Consume the string. */
13076 str = cp_parser_string_literal (parser, /*translate=*/true,
13077 /*wide_ok=*/true, /*lookup_udlit=*/false);
13078 if (str == error_mark_node)
13079 return error_mark_node;
13080 else if (TREE_CODE (str) == USERDEF_LITERAL)
13082 string_tree = USERDEF_LITERAL_VALUE (str);
13083 id = USERDEF_LITERAL_SUFFIX_ID (str);
13085 else
13087 string_tree = str;
13088 /* Look for the suffix identifier. */
13089 token = cp_lexer_peek_token (parser->lexer);
13090 if (token->type == CPP_NAME)
13091 id = cp_parser_identifier (parser);
13092 else if (token->type == CPP_KEYWORD)
13094 error ("unexpected keyword;"
13095 " remove space between quotes and suffix identifier");
13096 return error_mark_node;
13098 else
13100 error ("expected suffix identifier");
13101 return error_mark_node;
13104 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13105 (TREE_TYPE (TREE_TYPE (string_tree))));
13106 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13107 if (len != 0)
13109 error ("expected empty string after %<operator%> keyword");
13110 return error_mark_node;
13112 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13113 != char_type_node)
13115 error ("invalid encoding prefix in literal operator");
13116 return error_mark_node;
13118 if (id != error_mark_node)
13120 const char *name = IDENTIFIER_POINTER (id);
13121 id = cp_literal_operator_id (name);
13123 return id;
13126 default:
13127 /* Anything else is an error. */
13128 break;
13131 /* If we have selected an identifier, we need to consume the
13132 operator token. */
13133 if (id)
13134 cp_lexer_consume_token (parser->lexer);
13135 /* Otherwise, no valid operator name was present. */
13136 else
13138 cp_parser_error (parser, "expected operator");
13139 id = error_mark_node;
13142 return id;
13145 /* Parse a template-declaration.
13147 template-declaration:
13148 export [opt] template < template-parameter-list > declaration
13150 If MEMBER_P is TRUE, this template-declaration occurs within a
13151 class-specifier.
13153 The grammar rule given by the standard isn't correct. What
13154 is really meant is:
13156 template-declaration:
13157 export [opt] template-parameter-list-seq
13158 decl-specifier-seq [opt] init-declarator [opt] ;
13159 export [opt] template-parameter-list-seq
13160 function-definition
13162 template-parameter-list-seq:
13163 template-parameter-list-seq [opt]
13164 template < template-parameter-list > */
13166 static void
13167 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13169 /* Check for `export'. */
13170 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13172 /* Consume the `export' token. */
13173 cp_lexer_consume_token (parser->lexer);
13174 /* Warn that we do not support `export'. */
13175 warning (0, "keyword %<export%> not implemented, and will be ignored");
13178 cp_parser_template_declaration_after_export (parser, member_p);
13181 /* Parse a template-parameter-list.
13183 template-parameter-list:
13184 template-parameter
13185 template-parameter-list , template-parameter
13187 Returns a TREE_LIST. Each node represents a template parameter.
13188 The nodes are connected via their TREE_CHAINs. */
13190 static tree
13191 cp_parser_template_parameter_list (cp_parser* parser)
13193 tree parameter_list = NULL_TREE;
13195 begin_template_parm_list ();
13197 /* The loop below parses the template parms. We first need to know
13198 the total number of template parms to be able to compute proper
13199 canonical types of each dependent type. So after the loop, when
13200 we know the total number of template parms,
13201 end_template_parm_list computes the proper canonical types and
13202 fixes up the dependent types accordingly. */
13203 while (true)
13205 tree parameter;
13206 bool is_non_type;
13207 bool is_parameter_pack;
13208 location_t parm_loc;
13210 /* Parse the template-parameter. */
13211 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13212 parameter = cp_parser_template_parameter (parser,
13213 &is_non_type,
13214 &is_parameter_pack);
13215 /* Add it to the list. */
13216 if (parameter != error_mark_node)
13217 parameter_list = process_template_parm (parameter_list,
13218 parm_loc,
13219 parameter,
13220 is_non_type,
13221 is_parameter_pack);
13222 else
13224 tree err_parm = build_tree_list (parameter, parameter);
13225 parameter_list = chainon (parameter_list, err_parm);
13228 /* If the next token is not a `,', we're done. */
13229 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13230 break;
13231 /* Otherwise, consume the `,' token. */
13232 cp_lexer_consume_token (parser->lexer);
13235 return end_template_parm_list (parameter_list);
13238 /* Parse a template-parameter.
13240 template-parameter:
13241 type-parameter
13242 parameter-declaration
13244 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13245 the parameter. The TREE_PURPOSE is the default value, if any.
13246 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13247 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13248 set to true iff this parameter is a parameter pack. */
13250 static tree
13251 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13252 bool *is_parameter_pack)
13254 cp_token *token;
13255 cp_parameter_declarator *parameter_declarator;
13256 cp_declarator *id_declarator;
13257 tree parm;
13259 /* Assume it is a type parameter or a template parameter. */
13260 *is_non_type = false;
13261 /* Assume it not a parameter pack. */
13262 *is_parameter_pack = false;
13263 /* Peek at the next token. */
13264 token = cp_lexer_peek_token (parser->lexer);
13265 /* If it is `class' or `template', we have a type-parameter. */
13266 if (token->keyword == RID_TEMPLATE)
13267 return cp_parser_type_parameter (parser, is_parameter_pack);
13268 /* If it is `class' or `typename' we do not know yet whether it is a
13269 type parameter or a non-type parameter. Consider:
13271 template <typename T, typename T::X X> ...
13275 template <class C, class D*> ...
13277 Here, the first parameter is a type parameter, and the second is
13278 a non-type parameter. We can tell by looking at the token after
13279 the identifier -- if it is a `,', `=', or `>' then we have a type
13280 parameter. */
13281 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13283 /* Peek at the token after `class' or `typename'. */
13284 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13285 /* If it's an ellipsis, we have a template type parameter
13286 pack. */
13287 if (token->type == CPP_ELLIPSIS)
13288 return cp_parser_type_parameter (parser, is_parameter_pack);
13289 /* If it's an identifier, skip it. */
13290 if (token->type == CPP_NAME)
13291 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13292 /* Now, see if the token looks like the end of a template
13293 parameter. */
13294 if (token->type == CPP_COMMA
13295 || token->type == CPP_EQ
13296 || token->type == CPP_GREATER)
13297 return cp_parser_type_parameter (parser, is_parameter_pack);
13300 /* Otherwise, it is a non-type parameter.
13302 [temp.param]
13304 When parsing a default template-argument for a non-type
13305 template-parameter, the first non-nested `>' is taken as the end
13306 of the template parameter-list rather than a greater-than
13307 operator. */
13308 *is_non_type = true;
13309 parameter_declarator
13310 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13311 /*parenthesized_p=*/NULL);
13313 if (!parameter_declarator)
13314 return error_mark_node;
13316 /* If the parameter declaration is marked as a parameter pack, set
13317 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13318 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13319 grokdeclarator. */
13320 if (parameter_declarator->declarator
13321 && parameter_declarator->declarator->parameter_pack_p)
13323 *is_parameter_pack = true;
13324 parameter_declarator->declarator->parameter_pack_p = false;
13327 if (parameter_declarator->default_argument)
13329 /* Can happen in some cases of erroneous input (c++/34892). */
13330 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13331 /* Consume the `...' for better error recovery. */
13332 cp_lexer_consume_token (parser->lexer);
13334 /* If the next token is an ellipsis, and we don't already have it
13335 marked as a parameter pack, then we have a parameter pack (that
13336 has no declarator). */
13337 else if (!*is_parameter_pack
13338 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13339 && (declarator_can_be_parameter_pack
13340 (parameter_declarator->declarator)))
13342 /* Consume the `...'. */
13343 cp_lexer_consume_token (parser->lexer);
13344 maybe_warn_variadic_templates ();
13346 *is_parameter_pack = true;
13348 /* We might end up with a pack expansion as the type of the non-type
13349 template parameter, in which case this is a non-type template
13350 parameter pack. */
13351 else if (parameter_declarator->decl_specifiers.type
13352 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13354 *is_parameter_pack = true;
13355 parameter_declarator->decl_specifiers.type =
13356 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13359 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13361 /* Parameter packs cannot have default arguments. However, a
13362 user may try to do so, so we'll parse them and give an
13363 appropriate diagnostic here. */
13365 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13367 /* Find the name of the parameter pack. */
13368 id_declarator = parameter_declarator->declarator;
13369 while (id_declarator && id_declarator->kind != cdk_id)
13370 id_declarator = id_declarator->declarator;
13372 if (id_declarator && id_declarator->kind == cdk_id)
13373 error_at (start_token->location,
13374 "template parameter pack %qD cannot have a default argument",
13375 id_declarator->u.id.unqualified_name);
13376 else
13377 error_at (start_token->location,
13378 "template parameter pack cannot have a default argument");
13380 /* Parse the default argument, but throw away the result. */
13381 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13384 parm = grokdeclarator (parameter_declarator->declarator,
13385 &parameter_declarator->decl_specifiers,
13386 TPARM, /*initialized=*/0,
13387 /*attrlist=*/NULL);
13388 if (parm == error_mark_node)
13389 return error_mark_node;
13391 return build_tree_list (parameter_declarator->default_argument, parm);
13394 /* Parse a type-parameter.
13396 type-parameter:
13397 class identifier [opt]
13398 class identifier [opt] = type-id
13399 typename identifier [opt]
13400 typename identifier [opt] = type-id
13401 template < template-parameter-list > class identifier [opt]
13402 template < template-parameter-list > class identifier [opt]
13403 = id-expression
13405 GNU Extension (variadic templates):
13407 type-parameter:
13408 class ... identifier [opt]
13409 typename ... identifier [opt]
13411 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13412 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13413 the declaration of the parameter.
13415 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13417 static tree
13418 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13420 cp_token *token;
13421 tree parameter;
13423 /* Look for a keyword to tell us what kind of parameter this is. */
13424 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13425 if (!token)
13426 return error_mark_node;
13428 switch (token->keyword)
13430 case RID_CLASS:
13431 case RID_TYPENAME:
13433 tree identifier;
13434 tree default_argument;
13436 /* If the next token is an ellipsis, we have a template
13437 argument pack. */
13438 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13440 /* Consume the `...' token. */
13441 cp_lexer_consume_token (parser->lexer);
13442 maybe_warn_variadic_templates ();
13444 *is_parameter_pack = true;
13447 /* If the next token is an identifier, then it names the
13448 parameter. */
13449 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13450 identifier = cp_parser_identifier (parser);
13451 else
13452 identifier = NULL_TREE;
13454 /* Create the parameter. */
13455 parameter = finish_template_type_parm (class_type_node, identifier);
13457 /* If the next token is an `=', we have a default argument. */
13458 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13460 /* Consume the `=' token. */
13461 cp_lexer_consume_token (parser->lexer);
13462 /* Parse the default-argument. */
13463 push_deferring_access_checks (dk_no_deferred);
13464 default_argument = cp_parser_type_id (parser);
13466 /* Template parameter packs cannot have default
13467 arguments. */
13468 if (*is_parameter_pack)
13470 if (identifier)
13471 error_at (token->location,
13472 "template parameter pack %qD cannot have a "
13473 "default argument", identifier);
13474 else
13475 error_at (token->location,
13476 "template parameter packs cannot have "
13477 "default arguments");
13478 default_argument = NULL_TREE;
13480 pop_deferring_access_checks ();
13482 else
13483 default_argument = NULL_TREE;
13485 /* Create the combined representation of the parameter and the
13486 default argument. */
13487 parameter = build_tree_list (default_argument, parameter);
13489 break;
13491 case RID_TEMPLATE:
13493 tree identifier;
13494 tree default_argument;
13496 /* Look for the `<'. */
13497 cp_parser_require (parser, CPP_LESS, RT_LESS);
13498 /* Parse the template-parameter-list. */
13499 cp_parser_template_parameter_list (parser);
13500 /* Look for the `>'. */
13501 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13502 /* Look for the `class' or 'typename' keywords. */
13503 cp_parser_type_parameter_key (parser);
13504 /* If the next token is an ellipsis, we have a template
13505 argument pack. */
13506 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13508 /* Consume the `...' token. */
13509 cp_lexer_consume_token (parser->lexer);
13510 maybe_warn_variadic_templates ();
13512 *is_parameter_pack = true;
13514 /* If the next token is an `=', then there is a
13515 default-argument. If the next token is a `>', we are at
13516 the end of the parameter-list. If the next token is a `,',
13517 then we are at the end of this parameter. */
13518 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13519 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13520 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13522 identifier = cp_parser_identifier (parser);
13523 /* Treat invalid names as if the parameter were nameless. */
13524 if (identifier == error_mark_node)
13525 identifier = NULL_TREE;
13527 else
13528 identifier = NULL_TREE;
13530 /* Create the template parameter. */
13531 parameter = finish_template_template_parm (class_type_node,
13532 identifier);
13534 /* If the next token is an `=', then there is a
13535 default-argument. */
13536 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13538 bool is_template;
13540 /* Consume the `='. */
13541 cp_lexer_consume_token (parser->lexer);
13542 /* Parse the id-expression. */
13543 push_deferring_access_checks (dk_no_deferred);
13544 /* save token before parsing the id-expression, for error
13545 reporting */
13546 token = cp_lexer_peek_token (parser->lexer);
13547 default_argument
13548 = cp_parser_id_expression (parser,
13549 /*template_keyword_p=*/false,
13550 /*check_dependency_p=*/true,
13551 /*template_p=*/&is_template,
13552 /*declarator_p=*/false,
13553 /*optional_p=*/false);
13554 if (TREE_CODE (default_argument) == TYPE_DECL)
13555 /* If the id-expression was a template-id that refers to
13556 a template-class, we already have the declaration here,
13557 so no further lookup is needed. */
13559 else
13560 /* Look up the name. */
13561 default_argument
13562 = cp_parser_lookup_name (parser, default_argument,
13563 none_type,
13564 /*is_template=*/is_template,
13565 /*is_namespace=*/false,
13566 /*check_dependency=*/true,
13567 /*ambiguous_decls=*/NULL,
13568 token->location);
13569 /* See if the default argument is valid. */
13570 default_argument
13571 = check_template_template_default_arg (default_argument);
13573 /* Template parameter packs cannot have default
13574 arguments. */
13575 if (*is_parameter_pack)
13577 if (identifier)
13578 error_at (token->location,
13579 "template parameter pack %qD cannot "
13580 "have a default argument",
13581 identifier);
13582 else
13583 error_at (token->location, "template parameter packs cannot "
13584 "have default arguments");
13585 default_argument = NULL_TREE;
13587 pop_deferring_access_checks ();
13589 else
13590 default_argument = NULL_TREE;
13592 /* Create the combined representation of the parameter and the
13593 default argument. */
13594 parameter = build_tree_list (default_argument, parameter);
13596 break;
13598 default:
13599 gcc_unreachable ();
13600 break;
13603 return parameter;
13606 /* Parse a template-id.
13608 template-id:
13609 template-name < template-argument-list [opt] >
13611 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13612 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13613 returned. Otherwise, if the template-name names a function, or set
13614 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13615 names a class, returns a TYPE_DECL for the specialization.
13617 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13618 uninstantiated templates. */
13620 static tree
13621 cp_parser_template_id (cp_parser *parser,
13622 bool template_keyword_p,
13623 bool check_dependency_p,
13624 enum tag_types tag_type,
13625 bool is_declaration)
13627 int i;
13628 tree templ;
13629 tree arguments;
13630 tree template_id;
13631 cp_token_position start_of_id = 0;
13632 deferred_access_check *chk;
13633 vec<deferred_access_check, va_gc> *access_check;
13634 cp_token *next_token = NULL, *next_token_2 = NULL;
13635 bool is_identifier;
13637 /* If the next token corresponds to a template-id, there is no need
13638 to reparse it. */
13639 next_token = cp_lexer_peek_token (parser->lexer);
13640 if (next_token->type == CPP_TEMPLATE_ID)
13642 struct tree_check *check_value;
13644 /* Get the stored value. */
13645 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13646 /* Perform any access checks that were deferred. */
13647 access_check = check_value->checks;
13648 if (access_check)
13650 FOR_EACH_VEC_ELT (*access_check, i, chk)
13651 perform_or_defer_access_check (chk->binfo,
13652 chk->decl,
13653 chk->diag_decl,
13654 tf_warning_or_error);
13656 /* Return the stored value. */
13657 return check_value->value;
13660 /* Avoid performing name lookup if there is no possibility of
13661 finding a template-id. */
13662 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13663 || (next_token->type == CPP_NAME
13664 && !cp_parser_nth_token_starts_template_argument_list_p
13665 (parser, 2)))
13667 cp_parser_error (parser, "expected template-id");
13668 return error_mark_node;
13671 /* Remember where the template-id starts. */
13672 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13673 start_of_id = cp_lexer_token_position (parser->lexer, false);
13675 push_deferring_access_checks (dk_deferred);
13677 /* Parse the template-name. */
13678 is_identifier = false;
13679 templ = cp_parser_template_name (parser, template_keyword_p,
13680 check_dependency_p,
13681 is_declaration,
13682 tag_type,
13683 &is_identifier);
13684 if (templ == error_mark_node || is_identifier)
13686 pop_deferring_access_checks ();
13687 return templ;
13690 /* If we find the sequence `[:' after a template-name, it's probably
13691 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13692 parse correctly the argument list. */
13693 next_token = cp_lexer_peek_token (parser->lexer);
13694 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13695 if (next_token->type == CPP_OPEN_SQUARE
13696 && next_token->flags & DIGRAPH
13697 && next_token_2->type == CPP_COLON
13698 && !(next_token_2->flags & PREV_WHITE))
13700 cp_parser_parse_tentatively (parser);
13701 /* Change `:' into `::'. */
13702 next_token_2->type = CPP_SCOPE;
13703 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13704 CPP_LESS. */
13705 cp_lexer_consume_token (parser->lexer);
13707 /* Parse the arguments. */
13708 arguments = cp_parser_enclosed_template_argument_list (parser);
13709 if (!cp_parser_parse_definitely (parser))
13711 /* If we couldn't parse an argument list, then we revert our changes
13712 and return simply an error. Maybe this is not a template-id
13713 after all. */
13714 next_token_2->type = CPP_COLON;
13715 cp_parser_error (parser, "expected %<<%>");
13716 pop_deferring_access_checks ();
13717 return error_mark_node;
13719 /* Otherwise, emit an error about the invalid digraph, but continue
13720 parsing because we got our argument list. */
13721 if (permerror (next_token->location,
13722 "%<<::%> cannot begin a template-argument list"))
13724 static bool hint = false;
13725 inform (next_token->location,
13726 "%<<:%> is an alternate spelling for %<[%>."
13727 " Insert whitespace between %<<%> and %<::%>");
13728 if (!hint && !flag_permissive)
13730 inform (next_token->location, "(if you use %<-fpermissive%> "
13731 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13732 "accept your code)");
13733 hint = true;
13737 else
13739 /* Look for the `<' that starts the template-argument-list. */
13740 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13742 pop_deferring_access_checks ();
13743 return error_mark_node;
13745 /* Parse the arguments. */
13746 arguments = cp_parser_enclosed_template_argument_list (parser);
13749 /* Build a representation of the specialization. */
13750 if (identifier_p (templ))
13751 template_id = build_min_nt_loc (next_token->location,
13752 TEMPLATE_ID_EXPR,
13753 templ, arguments);
13754 else if (DECL_TYPE_TEMPLATE_P (templ)
13755 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13757 bool entering_scope;
13758 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13759 template (rather than some instantiation thereof) only if
13760 is not nested within some other construct. For example, in
13761 "template <typename T> void f(T) { A<T>::", A<T> is just an
13762 instantiation of A. */
13763 entering_scope = (template_parm_scope_p ()
13764 && cp_lexer_next_token_is (parser->lexer,
13765 CPP_SCOPE));
13766 template_id
13767 = finish_template_type (templ, arguments, entering_scope);
13769 else if (variable_template_p (templ))
13771 template_id = lookup_template_variable (templ, arguments);
13773 else
13775 /* If it's not a class-template or a template-template, it should be
13776 a function-template. */
13777 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13778 || TREE_CODE (templ) == OVERLOAD
13779 || BASELINK_P (templ)));
13781 template_id = lookup_template_function (templ, arguments);
13784 /* If parsing tentatively, replace the sequence of tokens that makes
13785 up the template-id with a CPP_TEMPLATE_ID token. That way,
13786 should we re-parse the token stream, we will not have to repeat
13787 the effort required to do the parse, nor will we issue duplicate
13788 error messages about problems during instantiation of the
13789 template. */
13790 if (start_of_id
13791 /* Don't do this if we had a parse error in a declarator; re-parsing
13792 might succeed if a name changes meaning (60361). */
13793 && !(cp_parser_error_occurred (parser)
13794 && cp_parser_parsing_tentatively (parser)
13795 && parser->in_declarator_p))
13797 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13799 /* Reset the contents of the START_OF_ID token. */
13800 token->type = CPP_TEMPLATE_ID;
13801 /* Retrieve any deferred checks. Do not pop this access checks yet
13802 so the memory will not be reclaimed during token replacing below. */
13803 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13804 token->u.tree_check_value->value = template_id;
13805 token->u.tree_check_value->checks = get_deferred_access_checks ();
13806 token->keyword = RID_MAX;
13808 /* Purge all subsequent tokens. */
13809 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13811 /* ??? Can we actually assume that, if template_id ==
13812 error_mark_node, we will have issued a diagnostic to the
13813 user, as opposed to simply marking the tentative parse as
13814 failed? */
13815 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13816 error_at (token->location, "parse error in template argument list");
13819 pop_to_parent_deferring_access_checks ();
13820 return template_id;
13823 /* Parse a template-name.
13825 template-name:
13826 identifier
13828 The standard should actually say:
13830 template-name:
13831 identifier
13832 operator-function-id
13834 A defect report has been filed about this issue.
13836 A conversion-function-id cannot be a template name because they cannot
13837 be part of a template-id. In fact, looking at this code:
13839 a.operator K<int>()
13841 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13842 It is impossible to call a templated conversion-function-id with an
13843 explicit argument list, since the only allowed template parameter is
13844 the type to which it is converting.
13846 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13847 `template' keyword, in a construction like:
13849 T::template f<3>()
13851 In that case `f' is taken to be a template-name, even though there
13852 is no way of knowing for sure.
13854 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13855 name refers to a set of overloaded functions, at least one of which
13856 is a template, or an IDENTIFIER_NODE with the name of the template,
13857 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13858 names are looked up inside uninstantiated templates. */
13860 static tree
13861 cp_parser_template_name (cp_parser* parser,
13862 bool template_keyword_p,
13863 bool check_dependency_p,
13864 bool is_declaration,
13865 enum tag_types tag_type,
13866 bool *is_identifier)
13868 tree identifier;
13869 tree decl;
13870 tree fns;
13871 cp_token *token = cp_lexer_peek_token (parser->lexer);
13873 /* If the next token is `operator', then we have either an
13874 operator-function-id or a conversion-function-id. */
13875 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13877 /* We don't know whether we're looking at an
13878 operator-function-id or a conversion-function-id. */
13879 cp_parser_parse_tentatively (parser);
13880 /* Try an operator-function-id. */
13881 identifier = cp_parser_operator_function_id (parser);
13882 /* If that didn't work, try a conversion-function-id. */
13883 if (!cp_parser_parse_definitely (parser))
13885 cp_parser_error (parser, "expected template-name");
13886 return error_mark_node;
13889 /* Look for the identifier. */
13890 else
13891 identifier = cp_parser_identifier (parser);
13893 /* If we didn't find an identifier, we don't have a template-id. */
13894 if (identifier == error_mark_node)
13895 return error_mark_node;
13897 /* If the name immediately followed the `template' keyword, then it
13898 is a template-name. However, if the next token is not `<', then
13899 we do not treat it as a template-name, since it is not being used
13900 as part of a template-id. This enables us to handle constructs
13901 like:
13903 template <typename T> struct S { S(); };
13904 template <typename T> S<T>::S();
13906 correctly. We would treat `S' as a template -- if it were `S<T>'
13907 -- but we do not if there is no `<'. */
13909 if (processing_template_decl
13910 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13912 /* In a declaration, in a dependent context, we pretend that the
13913 "template" keyword was present in order to improve error
13914 recovery. For example, given:
13916 template <typename T> void f(T::X<int>);
13918 we want to treat "X<int>" as a template-id. */
13919 if (is_declaration
13920 && !template_keyword_p
13921 && parser->scope && TYPE_P (parser->scope)
13922 && check_dependency_p
13923 && dependent_scope_p (parser->scope)
13924 /* Do not do this for dtors (or ctors), since they never
13925 need the template keyword before their name. */
13926 && !constructor_name_p (identifier, parser->scope))
13928 cp_token_position start = 0;
13930 /* Explain what went wrong. */
13931 error_at (token->location, "non-template %qD used as template",
13932 identifier);
13933 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13934 parser->scope, identifier);
13935 /* If parsing tentatively, find the location of the "<" token. */
13936 if (cp_parser_simulate_error (parser))
13937 start = cp_lexer_token_position (parser->lexer, true);
13938 /* Parse the template arguments so that we can issue error
13939 messages about them. */
13940 cp_lexer_consume_token (parser->lexer);
13941 cp_parser_enclosed_template_argument_list (parser);
13942 /* Skip tokens until we find a good place from which to
13943 continue parsing. */
13944 cp_parser_skip_to_closing_parenthesis (parser,
13945 /*recovering=*/true,
13946 /*or_comma=*/true,
13947 /*consume_paren=*/false);
13948 /* If parsing tentatively, permanently remove the
13949 template argument list. That will prevent duplicate
13950 error messages from being issued about the missing
13951 "template" keyword. */
13952 if (start)
13953 cp_lexer_purge_tokens_after (parser->lexer, start);
13954 if (is_identifier)
13955 *is_identifier = true;
13956 return identifier;
13959 /* If the "template" keyword is present, then there is generally
13960 no point in doing name-lookup, so we just return IDENTIFIER.
13961 But, if the qualifying scope is non-dependent then we can
13962 (and must) do name-lookup normally. */
13963 if (template_keyword_p
13964 && (!parser->scope
13965 || (TYPE_P (parser->scope)
13966 && dependent_type_p (parser->scope))))
13967 return identifier;
13970 /* Look up the name. */
13971 decl = cp_parser_lookup_name (parser, identifier,
13972 tag_type,
13973 /*is_template=*/true,
13974 /*is_namespace=*/false,
13975 check_dependency_p,
13976 /*ambiguous_decls=*/NULL,
13977 token->location);
13979 /* If DECL is a template, then the name was a template-name. */
13980 if (TREE_CODE (decl) == TEMPLATE_DECL)
13982 else
13984 tree fn = NULL_TREE;
13986 /* The standard does not explicitly indicate whether a name that
13987 names a set of overloaded declarations, some of which are
13988 templates, is a template-name. However, such a name should
13989 be a template-name; otherwise, there is no way to form a
13990 template-id for the overloaded templates. */
13991 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13992 if (TREE_CODE (fns) == OVERLOAD)
13993 for (fn = fns; fn; fn = OVL_NEXT (fn))
13994 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13995 break;
13997 if (!fn)
13999 /* The name does not name a template. */
14000 cp_parser_error (parser, "expected template-name");
14001 return error_mark_node;
14005 /* If DECL is dependent, and refers to a function, then just return
14006 its name; we will look it up again during template instantiation. */
14007 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14009 tree scope = ovl_scope (decl);
14010 if (TYPE_P (scope) && dependent_type_p (scope))
14011 return identifier;
14014 return decl;
14017 /* Parse a template-argument-list.
14019 template-argument-list:
14020 template-argument ... [opt]
14021 template-argument-list , template-argument ... [opt]
14023 Returns a TREE_VEC containing the arguments. */
14025 static tree
14026 cp_parser_template_argument_list (cp_parser* parser)
14028 tree fixed_args[10];
14029 unsigned n_args = 0;
14030 unsigned alloced = 10;
14031 tree *arg_ary = fixed_args;
14032 tree vec;
14033 bool saved_in_template_argument_list_p;
14034 bool saved_ice_p;
14035 bool saved_non_ice_p;
14037 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14038 parser->in_template_argument_list_p = true;
14039 /* Even if the template-id appears in an integral
14040 constant-expression, the contents of the argument list do
14041 not. */
14042 saved_ice_p = parser->integral_constant_expression_p;
14043 parser->integral_constant_expression_p = false;
14044 saved_non_ice_p = parser->non_integral_constant_expression_p;
14045 parser->non_integral_constant_expression_p = false;
14047 /* Parse the arguments. */
14050 tree argument;
14052 if (n_args)
14053 /* Consume the comma. */
14054 cp_lexer_consume_token (parser->lexer);
14056 /* Parse the template-argument. */
14057 argument = cp_parser_template_argument (parser);
14059 /* If the next token is an ellipsis, we're expanding a template
14060 argument pack. */
14061 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14063 if (argument == error_mark_node)
14065 cp_token *token = cp_lexer_peek_token (parser->lexer);
14066 error_at (token->location,
14067 "expected parameter pack before %<...%>");
14069 /* Consume the `...' token. */
14070 cp_lexer_consume_token (parser->lexer);
14072 /* Make the argument into a TYPE_PACK_EXPANSION or
14073 EXPR_PACK_EXPANSION. */
14074 argument = make_pack_expansion (argument);
14077 if (n_args == alloced)
14079 alloced *= 2;
14081 if (arg_ary == fixed_args)
14083 arg_ary = XNEWVEC (tree, alloced);
14084 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14086 else
14087 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14089 arg_ary[n_args++] = argument;
14091 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14093 vec = make_tree_vec (n_args);
14095 while (n_args--)
14096 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14098 if (arg_ary != fixed_args)
14099 free (arg_ary);
14100 parser->non_integral_constant_expression_p = saved_non_ice_p;
14101 parser->integral_constant_expression_p = saved_ice_p;
14102 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14103 #ifdef ENABLE_CHECKING
14104 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14105 #endif
14106 return vec;
14109 /* Parse a template-argument.
14111 template-argument:
14112 assignment-expression
14113 type-id
14114 id-expression
14116 The representation is that of an assignment-expression, type-id, or
14117 id-expression -- except that the qualified id-expression is
14118 evaluated, so that the value returned is either a DECL or an
14119 OVERLOAD.
14121 Although the standard says "assignment-expression", it forbids
14122 throw-expressions or assignments in the template argument.
14123 Therefore, we use "conditional-expression" instead. */
14125 static tree
14126 cp_parser_template_argument (cp_parser* parser)
14128 tree argument;
14129 bool template_p;
14130 bool address_p;
14131 bool maybe_type_id = false;
14132 cp_token *token = NULL, *argument_start_token = NULL;
14133 location_t loc = 0;
14134 cp_id_kind idk;
14136 /* There's really no way to know what we're looking at, so we just
14137 try each alternative in order.
14139 [temp.arg]
14141 In a template-argument, an ambiguity between a type-id and an
14142 expression is resolved to a type-id, regardless of the form of
14143 the corresponding template-parameter.
14145 Therefore, we try a type-id first. */
14146 cp_parser_parse_tentatively (parser);
14147 argument = cp_parser_template_type_arg (parser);
14148 /* If there was no error parsing the type-id but the next token is a
14149 '>>', our behavior depends on which dialect of C++ we're
14150 parsing. In C++98, we probably found a typo for '> >'. But there
14151 are type-id which are also valid expressions. For instance:
14153 struct X { int operator >> (int); };
14154 template <int V> struct Foo {};
14155 Foo<X () >> 5> r;
14157 Here 'X()' is a valid type-id of a function type, but the user just
14158 wanted to write the expression "X() >> 5". Thus, we remember that we
14159 found a valid type-id, but we still try to parse the argument as an
14160 expression to see what happens.
14162 In C++0x, the '>>' will be considered two separate '>'
14163 tokens. */
14164 if (!cp_parser_error_occurred (parser)
14165 && cxx_dialect == cxx98
14166 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14168 maybe_type_id = true;
14169 cp_parser_abort_tentative_parse (parser);
14171 else
14173 /* If the next token isn't a `,' or a `>', then this argument wasn't
14174 really finished. This means that the argument is not a valid
14175 type-id. */
14176 if (!cp_parser_next_token_ends_template_argument_p (parser))
14177 cp_parser_error (parser, "expected template-argument");
14178 /* If that worked, we're done. */
14179 if (cp_parser_parse_definitely (parser))
14180 return argument;
14182 /* We're still not sure what the argument will be. */
14183 cp_parser_parse_tentatively (parser);
14184 /* Try a template. */
14185 argument_start_token = cp_lexer_peek_token (parser->lexer);
14186 argument = cp_parser_id_expression (parser,
14187 /*template_keyword_p=*/false,
14188 /*check_dependency_p=*/true,
14189 &template_p,
14190 /*declarator_p=*/false,
14191 /*optional_p=*/false);
14192 /* If the next token isn't a `,' or a `>', then this argument wasn't
14193 really finished. */
14194 if (!cp_parser_next_token_ends_template_argument_p (parser))
14195 cp_parser_error (parser, "expected template-argument");
14196 if (!cp_parser_error_occurred (parser))
14198 /* Figure out what is being referred to. If the id-expression
14199 was for a class template specialization, then we will have a
14200 TYPE_DECL at this point. There is no need to do name lookup
14201 at this point in that case. */
14202 if (TREE_CODE (argument) != TYPE_DECL)
14203 argument = cp_parser_lookup_name (parser, argument,
14204 none_type,
14205 /*is_template=*/template_p,
14206 /*is_namespace=*/false,
14207 /*check_dependency=*/true,
14208 /*ambiguous_decls=*/NULL,
14209 argument_start_token->location);
14210 if (TREE_CODE (argument) != TEMPLATE_DECL
14211 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14212 cp_parser_error (parser, "expected template-name");
14214 if (cp_parser_parse_definitely (parser))
14215 return argument;
14216 /* It must be a non-type argument. There permitted cases are given
14217 in [temp.arg.nontype]:
14219 -- an integral constant-expression of integral or enumeration
14220 type; or
14222 -- the name of a non-type template-parameter; or
14224 -- the name of an object or function with external linkage...
14226 -- the address of an object or function with external linkage...
14228 -- a pointer to member... */
14229 /* Look for a non-type template parameter. */
14230 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14232 cp_parser_parse_tentatively (parser);
14233 argument = cp_parser_primary_expression (parser,
14234 /*address_p=*/false,
14235 /*cast_p=*/false,
14236 /*template_arg_p=*/true,
14237 &idk);
14238 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14239 || !cp_parser_next_token_ends_template_argument_p (parser))
14240 cp_parser_simulate_error (parser);
14241 if (cp_parser_parse_definitely (parser))
14242 return argument;
14245 /* If the next token is "&", the argument must be the address of an
14246 object or function with external linkage. */
14247 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14248 if (address_p)
14250 loc = cp_lexer_peek_token (parser->lexer)->location;
14251 cp_lexer_consume_token (parser->lexer);
14253 /* See if we might have an id-expression. */
14254 token = cp_lexer_peek_token (parser->lexer);
14255 if (token->type == CPP_NAME
14256 || token->keyword == RID_OPERATOR
14257 || token->type == CPP_SCOPE
14258 || token->type == CPP_TEMPLATE_ID
14259 || token->type == CPP_NESTED_NAME_SPECIFIER)
14261 cp_parser_parse_tentatively (parser);
14262 argument = cp_parser_primary_expression (parser,
14263 address_p,
14264 /*cast_p=*/false,
14265 /*template_arg_p=*/true,
14266 &idk);
14267 if (cp_parser_error_occurred (parser)
14268 || !cp_parser_next_token_ends_template_argument_p (parser))
14269 cp_parser_abort_tentative_parse (parser);
14270 else
14272 tree probe;
14274 if (INDIRECT_REF_P (argument))
14276 /* Strip the dereference temporarily. */
14277 gcc_assert (REFERENCE_REF_P (argument));
14278 argument = TREE_OPERAND (argument, 0);
14281 /* If we're in a template, we represent a qualified-id referring
14282 to a static data member as a SCOPE_REF even if the scope isn't
14283 dependent so that we can check access control later. */
14284 probe = argument;
14285 if (TREE_CODE (probe) == SCOPE_REF)
14286 probe = TREE_OPERAND (probe, 1);
14287 if (VAR_P (probe))
14289 /* A variable without external linkage might still be a
14290 valid constant-expression, so no error is issued here
14291 if the external-linkage check fails. */
14292 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14293 cp_parser_simulate_error (parser);
14295 else if (is_overloaded_fn (argument))
14296 /* All overloaded functions are allowed; if the external
14297 linkage test does not pass, an error will be issued
14298 later. */
14300 else if (address_p
14301 && (TREE_CODE (argument) == OFFSET_REF
14302 || TREE_CODE (argument) == SCOPE_REF))
14303 /* A pointer-to-member. */
14305 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14307 else
14308 cp_parser_simulate_error (parser);
14310 if (cp_parser_parse_definitely (parser))
14312 if (address_p)
14313 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14314 tf_warning_or_error);
14315 else
14316 argument = convert_from_reference (argument);
14317 return argument;
14321 /* If the argument started with "&", there are no other valid
14322 alternatives at this point. */
14323 if (address_p)
14325 cp_parser_error (parser, "invalid non-type template argument");
14326 return error_mark_node;
14329 /* If the argument wasn't successfully parsed as a type-id followed
14330 by '>>', the argument can only be a constant expression now.
14331 Otherwise, we try parsing the constant-expression tentatively,
14332 because the argument could really be a type-id. */
14333 if (maybe_type_id)
14334 cp_parser_parse_tentatively (parser);
14335 argument = cp_parser_constant_expression (parser);
14337 if (!maybe_type_id)
14338 return argument;
14339 if (!cp_parser_next_token_ends_template_argument_p (parser))
14340 cp_parser_error (parser, "expected template-argument");
14341 if (cp_parser_parse_definitely (parser))
14342 return argument;
14343 /* We did our best to parse the argument as a non type-id, but that
14344 was the only alternative that matched (albeit with a '>' after
14345 it). We can assume it's just a typo from the user, and a
14346 diagnostic will then be issued. */
14347 return cp_parser_template_type_arg (parser);
14350 /* Parse an explicit-instantiation.
14352 explicit-instantiation:
14353 template declaration
14355 Although the standard says `declaration', what it really means is:
14357 explicit-instantiation:
14358 template decl-specifier-seq [opt] declarator [opt] ;
14360 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14361 supposed to be allowed. A defect report has been filed about this
14362 issue.
14364 GNU Extension:
14366 explicit-instantiation:
14367 storage-class-specifier template
14368 decl-specifier-seq [opt] declarator [opt] ;
14369 function-specifier template
14370 decl-specifier-seq [opt] declarator [opt] ; */
14372 static void
14373 cp_parser_explicit_instantiation (cp_parser* parser)
14375 int declares_class_or_enum;
14376 cp_decl_specifier_seq decl_specifiers;
14377 tree extension_specifier = NULL_TREE;
14379 timevar_push (TV_TEMPLATE_INST);
14381 /* Look for an (optional) storage-class-specifier or
14382 function-specifier. */
14383 if (cp_parser_allow_gnu_extensions_p (parser))
14385 extension_specifier
14386 = cp_parser_storage_class_specifier_opt (parser);
14387 if (!extension_specifier)
14388 extension_specifier
14389 = cp_parser_function_specifier_opt (parser,
14390 /*decl_specs=*/NULL);
14393 /* Look for the `template' keyword. */
14394 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14395 /* Let the front end know that we are processing an explicit
14396 instantiation. */
14397 begin_explicit_instantiation ();
14398 /* [temp.explicit] says that we are supposed to ignore access
14399 control while processing explicit instantiation directives. */
14400 push_deferring_access_checks (dk_no_check);
14401 /* Parse a decl-specifier-seq. */
14402 cp_parser_decl_specifier_seq (parser,
14403 CP_PARSER_FLAGS_OPTIONAL,
14404 &decl_specifiers,
14405 &declares_class_or_enum);
14406 /* If there was exactly one decl-specifier, and it declared a class,
14407 and there's no declarator, then we have an explicit type
14408 instantiation. */
14409 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14411 tree type;
14413 type = check_tag_decl (&decl_specifiers,
14414 /*explicit_type_instantiation_p=*/true);
14415 /* Turn access control back on for names used during
14416 template instantiation. */
14417 pop_deferring_access_checks ();
14418 if (type)
14419 do_type_instantiation (type, extension_specifier,
14420 /*complain=*/tf_error);
14422 else
14424 cp_declarator *declarator;
14425 tree decl;
14427 /* Parse the declarator. */
14428 declarator
14429 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14430 /*ctor_dtor_or_conv_p=*/NULL,
14431 /*parenthesized_p=*/NULL,
14432 /*member_p=*/false,
14433 /*friend_p=*/false);
14434 if (declares_class_or_enum & 2)
14435 cp_parser_check_for_definition_in_return_type (declarator,
14436 decl_specifiers.type,
14437 decl_specifiers.locations[ds_type_spec]);
14438 if (declarator != cp_error_declarator)
14440 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14441 permerror (decl_specifiers.locations[ds_inline],
14442 "explicit instantiation shall not use"
14443 " %<inline%> specifier");
14444 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14445 permerror (decl_specifiers.locations[ds_constexpr],
14446 "explicit instantiation shall not use"
14447 " %<constexpr%> specifier");
14449 decl = grokdeclarator (declarator, &decl_specifiers,
14450 NORMAL, 0, &decl_specifiers.attributes);
14451 /* Turn access control back on for names used during
14452 template instantiation. */
14453 pop_deferring_access_checks ();
14454 /* Do the explicit instantiation. */
14455 do_decl_instantiation (decl, extension_specifier);
14457 else
14459 pop_deferring_access_checks ();
14460 /* Skip the body of the explicit instantiation. */
14461 cp_parser_skip_to_end_of_statement (parser);
14464 /* We're done with the instantiation. */
14465 end_explicit_instantiation ();
14467 cp_parser_consume_semicolon_at_end_of_statement (parser);
14469 timevar_pop (TV_TEMPLATE_INST);
14472 /* Parse an explicit-specialization.
14474 explicit-specialization:
14475 template < > declaration
14477 Although the standard says `declaration', what it really means is:
14479 explicit-specialization:
14480 template <> decl-specifier [opt] init-declarator [opt] ;
14481 template <> function-definition
14482 template <> explicit-specialization
14483 template <> template-declaration */
14485 static void
14486 cp_parser_explicit_specialization (cp_parser* parser)
14488 bool need_lang_pop;
14489 cp_token *token = cp_lexer_peek_token (parser->lexer);
14491 /* Look for the `template' keyword. */
14492 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14493 /* Look for the `<'. */
14494 cp_parser_require (parser, CPP_LESS, RT_LESS);
14495 /* Look for the `>'. */
14496 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14497 /* We have processed another parameter list. */
14498 ++parser->num_template_parameter_lists;
14499 /* [temp]
14501 A template ... explicit specialization ... shall not have C
14502 linkage. */
14503 if (current_lang_name == lang_name_c)
14505 error_at (token->location, "template specialization with C linkage");
14506 /* Give it C++ linkage to avoid confusing other parts of the
14507 front end. */
14508 push_lang_context (lang_name_cplusplus);
14509 need_lang_pop = true;
14511 else
14512 need_lang_pop = false;
14513 /* Let the front end know that we are beginning a specialization. */
14514 if (!begin_specialization ())
14516 end_specialization ();
14517 return;
14520 /* If the next keyword is `template', we need to figure out whether
14521 or not we're looking a template-declaration. */
14522 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14524 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14525 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14526 cp_parser_template_declaration_after_export (parser,
14527 /*member_p=*/false);
14528 else
14529 cp_parser_explicit_specialization (parser);
14531 else
14532 /* Parse the dependent declaration. */
14533 cp_parser_single_declaration (parser,
14534 /*checks=*/NULL,
14535 /*member_p=*/false,
14536 /*explicit_specialization_p=*/true,
14537 /*friend_p=*/NULL);
14538 /* We're done with the specialization. */
14539 end_specialization ();
14540 /* For the erroneous case of a template with C linkage, we pushed an
14541 implicit C++ linkage scope; exit that scope now. */
14542 if (need_lang_pop)
14543 pop_lang_context ();
14544 /* We're done with this parameter list. */
14545 --parser->num_template_parameter_lists;
14548 /* Parse a type-specifier.
14550 type-specifier:
14551 simple-type-specifier
14552 class-specifier
14553 enum-specifier
14554 elaborated-type-specifier
14555 cv-qualifier
14557 GNU Extension:
14559 type-specifier:
14560 __complex__
14562 Returns a representation of the type-specifier. For a
14563 class-specifier, enum-specifier, or elaborated-type-specifier, a
14564 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14566 The parser flags FLAGS is used to control type-specifier parsing.
14568 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14569 in a decl-specifier-seq.
14571 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14572 class-specifier, enum-specifier, or elaborated-type-specifier, then
14573 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14574 if a type is declared; 2 if it is defined. Otherwise, it is set to
14575 zero.
14577 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14578 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14579 is set to FALSE. */
14581 static tree
14582 cp_parser_type_specifier (cp_parser* parser,
14583 cp_parser_flags flags,
14584 cp_decl_specifier_seq *decl_specs,
14585 bool is_declaration,
14586 int* declares_class_or_enum,
14587 bool* is_cv_qualifier)
14589 tree type_spec = NULL_TREE;
14590 cp_token *token;
14591 enum rid keyword;
14592 cp_decl_spec ds = ds_last;
14594 /* Assume this type-specifier does not declare a new type. */
14595 if (declares_class_or_enum)
14596 *declares_class_or_enum = 0;
14597 /* And that it does not specify a cv-qualifier. */
14598 if (is_cv_qualifier)
14599 *is_cv_qualifier = false;
14600 /* Peek at the next token. */
14601 token = cp_lexer_peek_token (parser->lexer);
14603 /* If we're looking at a keyword, we can use that to guide the
14604 production we choose. */
14605 keyword = token->keyword;
14606 switch (keyword)
14608 case RID_ENUM:
14609 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14610 goto elaborated_type_specifier;
14612 /* Look for the enum-specifier. */
14613 type_spec = cp_parser_enum_specifier (parser);
14614 /* If that worked, we're done. */
14615 if (type_spec)
14617 if (declares_class_or_enum)
14618 *declares_class_or_enum = 2;
14619 if (decl_specs)
14620 cp_parser_set_decl_spec_type (decl_specs,
14621 type_spec,
14622 token,
14623 /*type_definition_p=*/true);
14624 return type_spec;
14626 else
14627 goto elaborated_type_specifier;
14629 /* Any of these indicate either a class-specifier, or an
14630 elaborated-type-specifier. */
14631 case RID_CLASS:
14632 case RID_STRUCT:
14633 case RID_UNION:
14634 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14635 goto elaborated_type_specifier;
14637 /* Parse tentatively so that we can back up if we don't find a
14638 class-specifier. */
14639 cp_parser_parse_tentatively (parser);
14640 /* Look for the class-specifier. */
14641 type_spec = cp_parser_class_specifier (parser);
14642 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14643 /* If that worked, we're done. */
14644 if (cp_parser_parse_definitely (parser))
14646 if (declares_class_or_enum)
14647 *declares_class_or_enum = 2;
14648 if (decl_specs)
14649 cp_parser_set_decl_spec_type (decl_specs,
14650 type_spec,
14651 token,
14652 /*type_definition_p=*/true);
14653 return type_spec;
14656 /* Fall through. */
14657 elaborated_type_specifier:
14658 /* We're declaring (not defining) a class or enum. */
14659 if (declares_class_or_enum)
14660 *declares_class_or_enum = 1;
14662 /* Fall through. */
14663 case RID_TYPENAME:
14664 /* Look for an elaborated-type-specifier. */
14665 type_spec
14666 = (cp_parser_elaborated_type_specifier
14667 (parser,
14668 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14669 is_declaration));
14670 if (decl_specs)
14671 cp_parser_set_decl_spec_type (decl_specs,
14672 type_spec,
14673 token,
14674 /*type_definition_p=*/false);
14675 return type_spec;
14677 case RID_CONST:
14678 ds = ds_const;
14679 if (is_cv_qualifier)
14680 *is_cv_qualifier = true;
14681 break;
14683 case RID_VOLATILE:
14684 ds = ds_volatile;
14685 if (is_cv_qualifier)
14686 *is_cv_qualifier = true;
14687 break;
14689 case RID_RESTRICT:
14690 ds = ds_restrict;
14691 if (is_cv_qualifier)
14692 *is_cv_qualifier = true;
14693 break;
14695 case RID_COMPLEX:
14696 /* The `__complex__' keyword is a GNU extension. */
14697 ds = ds_complex;
14698 break;
14700 default:
14701 break;
14704 /* Handle simple keywords. */
14705 if (ds != ds_last)
14707 if (decl_specs)
14709 set_and_check_decl_spec_loc (decl_specs, ds, token);
14710 decl_specs->any_specifiers_p = true;
14712 return cp_lexer_consume_token (parser->lexer)->u.value;
14715 /* If we do not already have a type-specifier, assume we are looking
14716 at a simple-type-specifier. */
14717 type_spec = cp_parser_simple_type_specifier (parser,
14718 decl_specs,
14719 flags);
14721 /* If we didn't find a type-specifier, and a type-specifier was not
14722 optional in this context, issue an error message. */
14723 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14725 cp_parser_error (parser, "expected type specifier");
14726 return error_mark_node;
14729 return type_spec;
14732 /* Parse a simple-type-specifier.
14734 simple-type-specifier:
14735 :: [opt] nested-name-specifier [opt] type-name
14736 :: [opt] nested-name-specifier template template-id
14737 char
14738 wchar_t
14739 bool
14740 short
14742 long
14743 signed
14744 unsigned
14745 float
14746 double
14747 void
14749 C++0x Extension:
14751 simple-type-specifier:
14752 auto
14753 decltype ( expression )
14754 char16_t
14755 char32_t
14756 __underlying_type ( type-id )
14758 GNU Extension:
14760 simple-type-specifier:
14761 __int128
14762 __typeof__ unary-expression
14763 __typeof__ ( type-id )
14764 __typeof__ ( type-id ) { initializer-list , [opt] }
14766 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14767 appropriately updated. */
14769 static tree
14770 cp_parser_simple_type_specifier (cp_parser* parser,
14771 cp_decl_specifier_seq *decl_specs,
14772 cp_parser_flags flags)
14774 tree type = NULL_TREE;
14775 cp_token *token;
14776 int idx;
14778 /* Peek at the next token. */
14779 token = cp_lexer_peek_token (parser->lexer);
14781 /* If we're looking at a keyword, things are easy. */
14782 switch (token->keyword)
14784 case RID_CHAR:
14785 if (decl_specs)
14786 decl_specs->explicit_char_p = true;
14787 type = char_type_node;
14788 break;
14789 case RID_CHAR16:
14790 type = char16_type_node;
14791 break;
14792 case RID_CHAR32:
14793 type = char32_type_node;
14794 break;
14795 case RID_WCHAR:
14796 type = wchar_type_node;
14797 break;
14798 case RID_BOOL:
14799 type = boolean_type_node;
14800 break;
14801 case RID_SHORT:
14802 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14803 type = short_integer_type_node;
14804 break;
14805 case RID_INT:
14806 if (decl_specs)
14807 decl_specs->explicit_int_p = true;
14808 type = integer_type_node;
14809 break;
14810 case RID_INT_N_0:
14811 case RID_INT_N_1:
14812 case RID_INT_N_2:
14813 case RID_INT_N_3:
14814 idx = token->keyword - RID_INT_N_0;
14815 if (! int_n_enabled_p [idx])
14816 break;
14817 if (decl_specs)
14819 decl_specs->explicit_intN_p = true;
14820 decl_specs->int_n_idx = idx;
14822 type = int_n_trees [idx].signed_type;
14823 break;
14824 case RID_LONG:
14825 if (decl_specs)
14826 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14827 type = long_integer_type_node;
14828 break;
14829 case RID_SIGNED:
14830 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14831 type = integer_type_node;
14832 break;
14833 case RID_UNSIGNED:
14834 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14835 type = unsigned_type_node;
14836 break;
14837 case RID_FLOAT:
14838 type = float_type_node;
14839 break;
14840 case RID_DOUBLE:
14841 type = double_type_node;
14842 break;
14843 case RID_VOID:
14844 type = void_type_node;
14845 break;
14847 case RID_AUTO:
14848 maybe_warn_cpp0x (CPP0X_AUTO);
14849 if (parser->auto_is_implicit_function_template_parm_p)
14851 type = synthesize_implicit_template_parm (parser);
14853 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14855 if (cxx_dialect < cxx14)
14856 pedwarn (location_of (type), 0,
14857 "use of %<auto%> in lambda parameter declaration "
14858 "only available with "
14859 "-std=c++14 or -std=gnu++14");
14861 else if (cxx_dialect < cxx14)
14862 pedwarn (location_of (type), 0,
14863 "use of %<auto%> in parameter declaration "
14864 "only available with "
14865 "-std=c++14 or -std=gnu++14");
14866 else
14867 pedwarn (location_of (type), OPT_Wpedantic,
14868 "ISO C++ forbids use of %<auto%> in parameter "
14869 "declaration");
14871 else
14872 type = make_auto ();
14873 break;
14875 case RID_DECLTYPE:
14876 /* Since DR 743, decltype can either be a simple-type-specifier by
14877 itself or begin a nested-name-specifier. Parsing it will replace
14878 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14879 handling below decide what to do. */
14880 cp_parser_decltype (parser);
14881 cp_lexer_set_token_position (parser->lexer, token);
14882 break;
14884 case RID_TYPEOF:
14885 /* Consume the `typeof' token. */
14886 cp_lexer_consume_token (parser->lexer);
14887 /* Parse the operand to `typeof'. */
14888 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14889 /* If it is not already a TYPE, take its type. */
14890 if (!TYPE_P (type))
14891 type = finish_typeof (type);
14893 if (decl_specs)
14894 cp_parser_set_decl_spec_type (decl_specs, type,
14895 token,
14896 /*type_definition_p=*/false);
14898 return type;
14900 case RID_UNDERLYING_TYPE:
14901 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14902 if (decl_specs)
14903 cp_parser_set_decl_spec_type (decl_specs, type,
14904 token,
14905 /*type_definition_p=*/false);
14907 return type;
14909 case RID_BASES:
14910 case RID_DIRECT_BASES:
14911 type = cp_parser_trait_expr (parser, token->keyword);
14912 if (decl_specs)
14913 cp_parser_set_decl_spec_type (decl_specs, type,
14914 token,
14915 /*type_definition_p=*/false);
14916 return type;
14917 default:
14918 break;
14921 /* If token is an already-parsed decltype not followed by ::,
14922 it's a simple-type-specifier. */
14923 if (token->type == CPP_DECLTYPE
14924 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14926 type = token->u.value;
14927 if (decl_specs)
14928 cp_parser_set_decl_spec_type (decl_specs, type,
14929 token,
14930 /*type_definition_p=*/false);
14931 cp_lexer_consume_token (parser->lexer);
14932 return type;
14935 /* If the type-specifier was for a built-in type, we're done. */
14936 if (type)
14938 /* Record the type. */
14939 if (decl_specs
14940 && (token->keyword != RID_SIGNED
14941 && token->keyword != RID_UNSIGNED
14942 && token->keyword != RID_SHORT
14943 && token->keyword != RID_LONG))
14944 cp_parser_set_decl_spec_type (decl_specs,
14945 type,
14946 token,
14947 /*type_definition_p=*/false);
14948 if (decl_specs)
14949 decl_specs->any_specifiers_p = true;
14951 /* Consume the token. */
14952 cp_lexer_consume_token (parser->lexer);
14954 /* There is no valid C++ program where a non-template type is
14955 followed by a "<". That usually indicates that the user thought
14956 that the type was a template. */
14957 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14958 token->location);
14960 return TYPE_NAME (type);
14963 /* The type-specifier must be a user-defined type. */
14964 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14966 bool qualified_p;
14967 bool global_p;
14969 /* Don't gobble tokens or issue error messages if this is an
14970 optional type-specifier. */
14971 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14972 cp_parser_parse_tentatively (parser);
14974 /* Look for the optional `::' operator. */
14975 global_p
14976 = (cp_parser_global_scope_opt (parser,
14977 /*current_scope_valid_p=*/false)
14978 != NULL_TREE);
14979 /* Look for the nested-name specifier. */
14980 qualified_p
14981 = (cp_parser_nested_name_specifier_opt (parser,
14982 /*typename_keyword_p=*/false,
14983 /*check_dependency_p=*/true,
14984 /*type_p=*/false,
14985 /*is_declaration=*/false)
14986 != NULL_TREE);
14987 token = cp_lexer_peek_token (parser->lexer);
14988 /* If we have seen a nested-name-specifier, and the next token
14989 is `template', then we are using the template-id production. */
14990 if (parser->scope
14991 && cp_parser_optional_template_keyword (parser))
14993 /* Look for the template-id. */
14994 type = cp_parser_template_id (parser,
14995 /*template_keyword_p=*/true,
14996 /*check_dependency_p=*/true,
14997 none_type,
14998 /*is_declaration=*/false);
14999 /* If the template-id did not name a type, we are out of
15000 luck. */
15001 if (TREE_CODE (type) != TYPE_DECL)
15003 cp_parser_error (parser, "expected template-id for type");
15004 type = NULL_TREE;
15007 /* Otherwise, look for a type-name. */
15008 else
15009 type = cp_parser_type_name (parser);
15010 /* Keep track of all name-lookups performed in class scopes. */
15011 if (type
15012 && !global_p
15013 && !qualified_p
15014 && TREE_CODE (type) == TYPE_DECL
15015 && identifier_p (DECL_NAME (type)))
15016 maybe_note_name_used_in_class (DECL_NAME (type), type);
15017 /* If it didn't work out, we don't have a TYPE. */
15018 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15019 && !cp_parser_parse_definitely (parser))
15020 type = NULL_TREE;
15021 if (type && decl_specs)
15022 cp_parser_set_decl_spec_type (decl_specs, type,
15023 token,
15024 /*type_definition_p=*/false);
15027 /* If we didn't get a type-name, issue an error message. */
15028 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15030 cp_parser_error (parser, "expected type-name");
15031 return error_mark_node;
15034 if (type && type != error_mark_node)
15036 /* See if TYPE is an Objective-C type, and if so, parse and
15037 accept any protocol references following it. Do this before
15038 the cp_parser_check_for_invalid_template_id() call, because
15039 Objective-C types can be followed by '<...>' which would
15040 enclose protocol names rather than template arguments, and so
15041 everything is fine. */
15042 if (c_dialect_objc () && !parser->scope
15043 && (objc_is_id (type) || objc_is_class_name (type)))
15045 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15046 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15048 /* Clobber the "unqualified" type previously entered into
15049 DECL_SPECS with the new, improved protocol-qualified version. */
15050 if (decl_specs)
15051 decl_specs->type = qual_type;
15053 return qual_type;
15056 /* There is no valid C++ program where a non-template type is
15057 followed by a "<". That usually indicates that the user
15058 thought that the type was a template. */
15059 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15060 none_type,
15061 token->location);
15064 return type;
15067 /* Parse a type-name.
15069 type-name:
15070 class-name
15071 enum-name
15072 typedef-name
15073 simple-template-id [in c++0x]
15075 enum-name:
15076 identifier
15078 typedef-name:
15079 identifier
15081 Returns a TYPE_DECL for the type. */
15083 static tree
15084 cp_parser_type_name (cp_parser* parser)
15086 tree type_decl;
15088 /* We can't know yet whether it is a class-name or not. */
15089 cp_parser_parse_tentatively (parser);
15090 /* Try a class-name. */
15091 type_decl = cp_parser_class_name (parser,
15092 /*typename_keyword_p=*/false,
15093 /*template_keyword_p=*/false,
15094 none_type,
15095 /*check_dependency_p=*/true,
15096 /*class_head_p=*/false,
15097 /*is_declaration=*/false);
15098 /* If it's not a class-name, keep looking. */
15099 if (!cp_parser_parse_definitely (parser))
15101 if (cxx_dialect < cxx11)
15102 /* It must be a typedef-name or an enum-name. */
15103 return cp_parser_nonclass_name (parser);
15105 cp_parser_parse_tentatively (parser);
15106 /* It is either a simple-template-id representing an
15107 instantiation of an alias template... */
15108 type_decl = cp_parser_template_id (parser,
15109 /*template_keyword_p=*/false,
15110 /*check_dependency_p=*/true,
15111 none_type,
15112 /*is_declaration=*/false);
15113 /* Note that this must be an instantiation of an alias template
15114 because [temp.names]/6 says:
15116 A template-id that names an alias template specialization
15117 is a type-name.
15119 Whereas [temp.names]/7 says:
15121 A simple-template-id that names a class template
15122 specialization is a class-name. */
15123 if (type_decl != NULL_TREE
15124 && TREE_CODE (type_decl) == TYPE_DECL
15125 && TYPE_DECL_ALIAS_P (type_decl))
15126 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15127 else
15128 cp_parser_simulate_error (parser);
15130 if (!cp_parser_parse_definitely (parser))
15131 /* ... Or a typedef-name or an enum-name. */
15132 return cp_parser_nonclass_name (parser);
15135 return type_decl;
15138 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15140 enum-name:
15141 identifier
15143 typedef-name:
15144 identifier
15146 Returns a TYPE_DECL for the type. */
15148 static tree
15149 cp_parser_nonclass_name (cp_parser* parser)
15151 tree type_decl;
15152 tree identifier;
15154 cp_token *token = cp_lexer_peek_token (parser->lexer);
15155 identifier = cp_parser_identifier (parser);
15156 if (identifier == error_mark_node)
15157 return error_mark_node;
15159 /* Look up the type-name. */
15160 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15162 type_decl = strip_using_decl (type_decl);
15164 if (TREE_CODE (type_decl) != TYPE_DECL
15165 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15167 /* See if this is an Objective-C type. */
15168 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15169 tree type = objc_get_protocol_qualified_type (identifier, protos);
15170 if (type)
15171 type_decl = TYPE_NAME (type);
15174 /* Issue an error if we did not find a type-name. */
15175 if (TREE_CODE (type_decl) != TYPE_DECL
15176 /* In Objective-C, we have the complication that class names are
15177 normally type names and start declarations (eg, the
15178 "NSObject" in "NSObject *object;"), but can be used in an
15179 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15180 is an expression. So, a classname followed by a dot is not a
15181 valid type-name. */
15182 || (objc_is_class_name (TREE_TYPE (type_decl))
15183 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15185 if (!cp_parser_simulate_error (parser))
15186 cp_parser_name_lookup_error (parser, identifier, type_decl,
15187 NLE_TYPE, token->location);
15188 return error_mark_node;
15190 /* Remember that the name was used in the definition of the
15191 current class so that we can check later to see if the
15192 meaning would have been different after the class was
15193 entirely defined. */
15194 else if (type_decl != error_mark_node
15195 && !parser->scope)
15196 maybe_note_name_used_in_class (identifier, type_decl);
15198 return type_decl;
15201 /* Parse an elaborated-type-specifier. Note that the grammar given
15202 here incorporates the resolution to DR68.
15204 elaborated-type-specifier:
15205 class-key :: [opt] nested-name-specifier [opt] identifier
15206 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15207 enum-key :: [opt] nested-name-specifier [opt] identifier
15208 typename :: [opt] nested-name-specifier identifier
15209 typename :: [opt] nested-name-specifier template [opt]
15210 template-id
15212 GNU extension:
15214 elaborated-type-specifier:
15215 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15216 class-key attributes :: [opt] nested-name-specifier [opt]
15217 template [opt] template-id
15218 enum attributes :: [opt] nested-name-specifier [opt] identifier
15220 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15221 declared `friend'. If IS_DECLARATION is TRUE, then this
15222 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15223 something is being declared.
15225 Returns the TYPE specified. */
15227 static tree
15228 cp_parser_elaborated_type_specifier (cp_parser* parser,
15229 bool is_friend,
15230 bool is_declaration)
15232 enum tag_types tag_type;
15233 tree identifier;
15234 tree type = NULL_TREE;
15235 tree attributes = NULL_TREE;
15236 tree globalscope;
15237 cp_token *token = NULL;
15239 /* See if we're looking at the `enum' keyword. */
15240 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15242 /* Consume the `enum' token. */
15243 cp_lexer_consume_token (parser->lexer);
15244 /* Remember that it's an enumeration type. */
15245 tag_type = enum_type;
15246 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15247 enums) is used here. */
15248 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15249 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15251 pedwarn (input_location, 0, "elaborated-type-specifier "
15252 "for a scoped enum must not use the %<%D%> keyword",
15253 cp_lexer_peek_token (parser->lexer)->u.value);
15254 /* Consume the `struct' or `class' and parse it anyway. */
15255 cp_lexer_consume_token (parser->lexer);
15257 /* Parse the attributes. */
15258 attributes = cp_parser_attributes_opt (parser);
15260 /* Or, it might be `typename'. */
15261 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15262 RID_TYPENAME))
15264 /* Consume the `typename' token. */
15265 cp_lexer_consume_token (parser->lexer);
15266 /* Remember that it's a `typename' type. */
15267 tag_type = typename_type;
15269 /* Otherwise it must be a class-key. */
15270 else
15272 tag_type = cp_parser_class_key (parser);
15273 if (tag_type == none_type)
15274 return error_mark_node;
15275 /* Parse the attributes. */
15276 attributes = cp_parser_attributes_opt (parser);
15279 /* Look for the `::' operator. */
15280 globalscope = cp_parser_global_scope_opt (parser,
15281 /*current_scope_valid_p=*/false);
15282 /* Look for the nested-name-specifier. */
15283 if (tag_type == typename_type && !globalscope)
15285 if (!cp_parser_nested_name_specifier (parser,
15286 /*typename_keyword_p=*/true,
15287 /*check_dependency_p=*/true,
15288 /*type_p=*/true,
15289 is_declaration))
15290 return error_mark_node;
15292 else
15293 /* Even though `typename' is not present, the proposed resolution
15294 to Core Issue 180 says that in `class A<T>::B', `B' should be
15295 considered a type-name, even if `A<T>' is dependent. */
15296 cp_parser_nested_name_specifier_opt (parser,
15297 /*typename_keyword_p=*/true,
15298 /*check_dependency_p=*/true,
15299 /*type_p=*/true,
15300 is_declaration);
15301 /* For everything but enumeration types, consider a template-id.
15302 For an enumeration type, consider only a plain identifier. */
15303 if (tag_type != enum_type)
15305 bool template_p = false;
15306 tree decl;
15308 /* Allow the `template' keyword. */
15309 template_p = cp_parser_optional_template_keyword (parser);
15310 /* If we didn't see `template', we don't know if there's a
15311 template-id or not. */
15312 if (!template_p)
15313 cp_parser_parse_tentatively (parser);
15314 /* Parse the template-id. */
15315 token = cp_lexer_peek_token (parser->lexer);
15316 decl = cp_parser_template_id (parser, template_p,
15317 /*check_dependency_p=*/true,
15318 tag_type,
15319 is_declaration);
15320 /* If we didn't find a template-id, look for an ordinary
15321 identifier. */
15322 if (!template_p && !cp_parser_parse_definitely (parser))
15324 /* We can get here when cp_parser_template_id, called by
15325 cp_parser_class_name with tag_type == none_type, succeeds
15326 and caches a BASELINK. Then, when called again here,
15327 instead of failing and returning an error_mark_node
15328 returns it (see template/typename17.C in C++11).
15329 ??? Could we diagnose this earlier? */
15330 else if (tag_type == typename_type && BASELINK_P (decl))
15332 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15333 type = error_mark_node;
15335 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15336 in effect, then we must assume that, upon instantiation, the
15337 template will correspond to a class. */
15338 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15339 && tag_type == typename_type)
15340 type = make_typename_type (parser->scope, decl,
15341 typename_type,
15342 /*complain=*/tf_error);
15343 /* If the `typename' keyword is in effect and DECL is not a type
15344 decl, then type is non existent. */
15345 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15347 else if (TREE_CODE (decl) == TYPE_DECL)
15348 type = check_elaborated_type_specifier (tag_type, decl,
15349 /*allow_template_p=*/true);
15350 else if (decl == error_mark_node)
15351 type = error_mark_node;
15354 if (!type)
15356 token = cp_lexer_peek_token (parser->lexer);
15357 identifier = cp_parser_identifier (parser);
15359 if (identifier == error_mark_node)
15361 parser->scope = NULL_TREE;
15362 return error_mark_node;
15365 /* For a `typename', we needn't call xref_tag. */
15366 if (tag_type == typename_type
15367 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15368 return cp_parser_make_typename_type (parser, identifier,
15369 token->location);
15371 /* Template parameter lists apply only if we are not within a
15372 function parameter list. */
15373 bool template_parm_lists_apply
15374 = parser->num_template_parameter_lists;
15375 if (template_parm_lists_apply)
15376 for (cp_binding_level *s = current_binding_level;
15377 s && s->kind != sk_template_parms;
15378 s = s->level_chain)
15379 if (s->kind == sk_function_parms)
15380 template_parm_lists_apply = false;
15382 /* Look up a qualified name in the usual way. */
15383 if (parser->scope)
15385 tree decl;
15386 tree ambiguous_decls;
15388 decl = cp_parser_lookup_name (parser, identifier,
15389 tag_type,
15390 /*is_template=*/false,
15391 /*is_namespace=*/false,
15392 /*check_dependency=*/true,
15393 &ambiguous_decls,
15394 token->location);
15396 /* If the lookup was ambiguous, an error will already have been
15397 issued. */
15398 if (ambiguous_decls)
15399 return error_mark_node;
15401 /* If we are parsing friend declaration, DECL may be a
15402 TEMPLATE_DECL tree node here. However, we need to check
15403 whether this TEMPLATE_DECL results in valid code. Consider
15404 the following example:
15406 namespace N {
15407 template <class T> class C {};
15409 class X {
15410 template <class T> friend class N::C; // #1, valid code
15412 template <class T> class Y {
15413 friend class N::C; // #2, invalid code
15416 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15417 name lookup of `N::C'. We see that friend declaration must
15418 be template for the code to be valid. Note that
15419 processing_template_decl does not work here since it is
15420 always 1 for the above two cases. */
15422 decl = (cp_parser_maybe_treat_template_as_class
15423 (decl, /*tag_name_p=*/is_friend
15424 && template_parm_lists_apply));
15426 if (TREE_CODE (decl) != TYPE_DECL)
15428 cp_parser_diagnose_invalid_type_name (parser,
15429 identifier,
15430 token->location);
15431 return error_mark_node;
15434 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15436 bool allow_template = (template_parm_lists_apply
15437 || DECL_SELF_REFERENCE_P (decl));
15438 type = check_elaborated_type_specifier (tag_type, decl,
15439 allow_template);
15441 if (type == error_mark_node)
15442 return error_mark_node;
15445 /* Forward declarations of nested types, such as
15447 class C1::C2;
15448 class C1::C2::C3;
15450 are invalid unless all components preceding the final '::'
15451 are complete. If all enclosing types are complete, these
15452 declarations become merely pointless.
15454 Invalid forward declarations of nested types are errors
15455 caught elsewhere in parsing. Those that are pointless arrive
15456 here. */
15458 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15459 && !is_friend && !processing_explicit_instantiation)
15460 warning (0, "declaration %qD does not declare anything", decl);
15462 type = TREE_TYPE (decl);
15464 else
15466 /* An elaborated-type-specifier sometimes introduces a new type and
15467 sometimes names an existing type. Normally, the rule is that it
15468 introduces a new type only if there is not an existing type of
15469 the same name already in scope. For example, given:
15471 struct S {};
15472 void f() { struct S s; }
15474 the `struct S' in the body of `f' is the same `struct S' as in
15475 the global scope; the existing definition is used. However, if
15476 there were no global declaration, this would introduce a new
15477 local class named `S'.
15479 An exception to this rule applies to the following code:
15481 namespace N { struct S; }
15483 Here, the elaborated-type-specifier names a new type
15484 unconditionally; even if there is already an `S' in the
15485 containing scope this declaration names a new type.
15486 This exception only applies if the elaborated-type-specifier
15487 forms the complete declaration:
15489 [class.name]
15491 A declaration consisting solely of `class-key identifier ;' is
15492 either a redeclaration of the name in the current scope or a
15493 forward declaration of the identifier as a class name. It
15494 introduces the name into the current scope.
15496 We are in this situation precisely when the next token is a `;'.
15498 An exception to the exception is that a `friend' declaration does
15499 *not* name a new type; i.e., given:
15501 struct S { friend struct T; };
15503 `T' is not a new type in the scope of `S'.
15505 Also, `new struct S' or `sizeof (struct S)' never results in the
15506 definition of a new type; a new type can only be declared in a
15507 declaration context. */
15509 tag_scope ts;
15510 bool template_p;
15512 if (is_friend)
15513 /* Friends have special name lookup rules. */
15514 ts = ts_within_enclosing_non_class;
15515 else if (is_declaration
15516 && cp_lexer_next_token_is (parser->lexer,
15517 CPP_SEMICOLON))
15518 /* This is a `class-key identifier ;' */
15519 ts = ts_current;
15520 else
15521 ts = ts_global;
15523 template_p =
15524 (template_parm_lists_apply
15525 && (cp_parser_next_token_starts_class_definition_p (parser)
15526 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15527 /* An unqualified name was used to reference this type, so
15528 there were no qualifying templates. */
15529 if (template_parm_lists_apply
15530 && !cp_parser_check_template_parameters (parser,
15531 /*num_templates=*/0,
15532 token->location,
15533 /*declarator=*/NULL))
15534 return error_mark_node;
15535 type = xref_tag (tag_type, identifier, ts, template_p);
15539 if (type == error_mark_node)
15540 return error_mark_node;
15542 /* Allow attributes on forward declarations of classes. */
15543 if (attributes)
15545 if (TREE_CODE (type) == TYPENAME_TYPE)
15546 warning (OPT_Wattributes,
15547 "attributes ignored on uninstantiated type");
15548 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15549 && ! processing_explicit_instantiation)
15550 warning (OPT_Wattributes,
15551 "attributes ignored on template instantiation");
15552 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15553 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15554 else
15555 warning (OPT_Wattributes,
15556 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15559 if (tag_type != enum_type)
15561 /* Indicate whether this class was declared as a `class' or as a
15562 `struct'. */
15563 if (TREE_CODE (type) == RECORD_TYPE)
15564 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15565 cp_parser_check_class_key (tag_type, type);
15568 /* A "<" cannot follow an elaborated type specifier. If that
15569 happens, the user was probably trying to form a template-id. */
15570 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15571 token->location);
15573 return type;
15576 /* Parse an enum-specifier.
15578 enum-specifier:
15579 enum-head { enumerator-list [opt] }
15580 enum-head { enumerator-list , } [C++0x]
15582 enum-head:
15583 enum-key identifier [opt] enum-base [opt]
15584 enum-key nested-name-specifier identifier enum-base [opt]
15586 enum-key:
15587 enum
15588 enum class [C++0x]
15589 enum struct [C++0x]
15591 enum-base: [C++0x]
15592 : type-specifier-seq
15594 opaque-enum-specifier:
15595 enum-key identifier enum-base [opt] ;
15597 GNU Extensions:
15598 enum-key attributes[opt] identifier [opt] enum-base [opt]
15599 { enumerator-list [opt] }attributes[opt]
15600 enum-key attributes[opt] identifier [opt] enum-base [opt]
15601 { enumerator-list, }attributes[opt] [C++0x]
15603 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15604 if the token stream isn't an enum-specifier after all. */
15606 static tree
15607 cp_parser_enum_specifier (cp_parser* parser)
15609 tree identifier;
15610 tree type = NULL_TREE;
15611 tree prev_scope;
15612 tree nested_name_specifier = NULL_TREE;
15613 tree attributes;
15614 bool scoped_enum_p = false;
15615 bool has_underlying_type = false;
15616 bool nested_being_defined = false;
15617 bool new_value_list = false;
15618 bool is_new_type = false;
15619 bool is_anonymous = false;
15620 tree underlying_type = NULL_TREE;
15621 cp_token *type_start_token = NULL;
15622 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15624 parser->colon_corrects_to_scope_p = false;
15626 /* Parse tentatively so that we can back up if we don't find a
15627 enum-specifier. */
15628 cp_parser_parse_tentatively (parser);
15630 /* Caller guarantees that the current token is 'enum', an identifier
15631 possibly follows, and the token after that is an opening brace.
15632 If we don't have an identifier, fabricate an anonymous name for
15633 the enumeration being defined. */
15634 cp_lexer_consume_token (parser->lexer);
15636 /* Parse the "class" or "struct", which indicates a scoped
15637 enumeration type in C++0x. */
15638 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15639 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15641 if (cxx_dialect < cxx11)
15642 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15644 /* Consume the `struct' or `class' token. */
15645 cp_lexer_consume_token (parser->lexer);
15647 scoped_enum_p = true;
15650 attributes = cp_parser_attributes_opt (parser);
15652 /* Clear the qualification. */
15653 parser->scope = NULL_TREE;
15654 parser->qualifying_scope = NULL_TREE;
15655 parser->object_scope = NULL_TREE;
15657 /* Figure out in what scope the declaration is being placed. */
15658 prev_scope = current_scope ();
15660 type_start_token = cp_lexer_peek_token (parser->lexer);
15662 push_deferring_access_checks (dk_no_check);
15663 nested_name_specifier
15664 = cp_parser_nested_name_specifier_opt (parser,
15665 /*typename_keyword_p=*/true,
15666 /*check_dependency_p=*/false,
15667 /*type_p=*/false,
15668 /*is_declaration=*/false);
15670 if (nested_name_specifier)
15672 tree name;
15674 identifier = cp_parser_identifier (parser);
15675 name = cp_parser_lookup_name (parser, identifier,
15676 enum_type,
15677 /*is_template=*/false,
15678 /*is_namespace=*/false,
15679 /*check_dependency=*/true,
15680 /*ambiguous_decls=*/NULL,
15681 input_location);
15682 if (name && name != error_mark_node)
15684 type = TREE_TYPE (name);
15685 if (TREE_CODE (type) == TYPENAME_TYPE)
15687 /* Are template enums allowed in ISO? */
15688 if (template_parm_scope_p ())
15689 pedwarn (type_start_token->location, OPT_Wpedantic,
15690 "%qD is an enumeration template", name);
15691 /* ignore a typename reference, for it will be solved by name
15692 in start_enum. */
15693 type = NULL_TREE;
15696 else if (nested_name_specifier == error_mark_node)
15697 /* We already issued an error. */;
15698 else
15699 error_at (type_start_token->location,
15700 "%qD is not an enumerator-name", identifier);
15702 else
15704 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15705 identifier = cp_parser_identifier (parser);
15706 else
15708 identifier = make_anon_name ();
15709 is_anonymous = true;
15710 if (scoped_enum_p)
15711 error_at (type_start_token->location,
15712 "anonymous scoped enum is not allowed");
15715 pop_deferring_access_checks ();
15717 /* Check for the `:' that denotes a specified underlying type in C++0x.
15718 Note that a ':' could also indicate a bitfield width, however. */
15719 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15721 cp_decl_specifier_seq type_specifiers;
15723 /* Consume the `:'. */
15724 cp_lexer_consume_token (parser->lexer);
15726 /* Parse the type-specifier-seq. */
15727 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15728 /*is_trailing_return=*/false,
15729 &type_specifiers);
15731 /* At this point this is surely not elaborated type specifier. */
15732 if (!cp_parser_parse_definitely (parser))
15733 return NULL_TREE;
15735 if (cxx_dialect < cxx11)
15736 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15738 has_underlying_type = true;
15740 /* If that didn't work, stop. */
15741 if (type_specifiers.type != error_mark_node)
15743 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15744 /*initialized=*/0, NULL);
15745 if (underlying_type == error_mark_node
15746 || check_for_bare_parameter_packs (underlying_type))
15747 underlying_type = NULL_TREE;
15751 /* Look for the `{' but don't consume it yet. */
15752 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15754 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15756 cp_parser_error (parser, "expected %<{%>");
15757 if (has_underlying_type)
15759 type = NULL_TREE;
15760 goto out;
15763 /* An opaque-enum-specifier must have a ';' here. */
15764 if ((scoped_enum_p || underlying_type)
15765 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15767 cp_parser_error (parser, "expected %<;%> or %<{%>");
15768 if (has_underlying_type)
15770 type = NULL_TREE;
15771 goto out;
15776 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15777 return NULL_TREE;
15779 if (nested_name_specifier)
15781 if (CLASS_TYPE_P (nested_name_specifier))
15783 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15784 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15785 push_scope (nested_name_specifier);
15787 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15789 push_nested_namespace (nested_name_specifier);
15793 /* Issue an error message if type-definitions are forbidden here. */
15794 if (!cp_parser_check_type_definition (parser))
15795 type = error_mark_node;
15796 else
15797 /* Create the new type. We do this before consuming the opening
15798 brace so the enum will be recorded as being on the line of its
15799 tag (or the 'enum' keyword, if there is no tag). */
15800 type = start_enum (identifier, type, underlying_type,
15801 scoped_enum_p, &is_new_type);
15803 /* If the next token is not '{' it is an opaque-enum-specifier or an
15804 elaborated-type-specifier. */
15805 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15807 timevar_push (TV_PARSE_ENUM);
15808 if (nested_name_specifier
15809 && nested_name_specifier != error_mark_node)
15811 /* The following catches invalid code such as:
15812 enum class S<int>::E { A, B, C }; */
15813 if (!processing_specialization
15814 && CLASS_TYPE_P (nested_name_specifier)
15815 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15816 error_at (type_start_token->location, "cannot add an enumerator "
15817 "list to a template instantiation");
15819 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15821 error_at (type_start_token->location,
15822 "%<%T::%E%> has not been declared",
15823 TYPE_CONTEXT (nested_name_specifier),
15824 nested_name_specifier);
15825 type = error_mark_node;
15827 /* If that scope does not contain the scope in which the
15828 class was originally declared, the program is invalid. */
15829 else if (prev_scope && !is_ancestor (prev_scope,
15830 nested_name_specifier))
15832 if (at_namespace_scope_p ())
15833 error_at (type_start_token->location,
15834 "declaration of %qD in namespace %qD which does not "
15835 "enclose %qD",
15836 type, prev_scope, nested_name_specifier);
15837 else
15838 error_at (type_start_token->location,
15839 "declaration of %qD in %qD which does not "
15840 "enclose %qD",
15841 type, prev_scope, nested_name_specifier);
15842 type = error_mark_node;
15846 if (scoped_enum_p)
15847 begin_scope (sk_scoped_enum, type);
15849 /* Consume the opening brace. */
15850 cp_lexer_consume_token (parser->lexer);
15852 if (type == error_mark_node)
15853 ; /* Nothing to add */
15854 else if (OPAQUE_ENUM_P (type)
15855 || (cxx_dialect > cxx98 && processing_specialization))
15857 new_value_list = true;
15858 SET_OPAQUE_ENUM_P (type, false);
15859 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15861 else
15863 error_at (type_start_token->location,
15864 "multiple definition of %q#T", type);
15865 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15866 "previous definition here");
15867 type = error_mark_node;
15870 if (type == error_mark_node)
15871 cp_parser_skip_to_end_of_block_or_statement (parser);
15872 /* If the next token is not '}', then there are some enumerators. */
15873 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15875 if (is_anonymous && !scoped_enum_p)
15876 pedwarn (type_start_token->location, OPT_Wpedantic,
15877 "ISO C++ forbids empty anonymous enum");
15879 else
15880 cp_parser_enumerator_list (parser, type);
15882 /* Consume the final '}'. */
15883 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15885 if (scoped_enum_p)
15886 finish_scope ();
15887 timevar_pop (TV_PARSE_ENUM);
15889 else
15891 /* If a ';' follows, then it is an opaque-enum-specifier
15892 and additional restrictions apply. */
15893 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15895 if (is_anonymous)
15896 error_at (type_start_token->location,
15897 "opaque-enum-specifier without name");
15898 else if (nested_name_specifier)
15899 error_at (type_start_token->location,
15900 "opaque-enum-specifier must use a simple identifier");
15904 /* Look for trailing attributes to apply to this enumeration, and
15905 apply them if appropriate. */
15906 if (cp_parser_allow_gnu_extensions_p (parser))
15908 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15909 trailing_attr = chainon (trailing_attr, attributes);
15910 cplus_decl_attributes (&type,
15911 trailing_attr,
15912 (int) ATTR_FLAG_TYPE_IN_PLACE);
15915 /* Finish up the enumeration. */
15916 if (type != error_mark_node)
15918 if (new_value_list)
15919 finish_enum_value_list (type);
15920 if (is_new_type)
15921 finish_enum (type);
15924 if (nested_name_specifier)
15926 if (CLASS_TYPE_P (nested_name_specifier))
15928 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15929 pop_scope (nested_name_specifier);
15931 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15933 pop_nested_namespace (nested_name_specifier);
15936 out:
15937 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15938 return type;
15941 /* Parse an enumerator-list. The enumerators all have the indicated
15942 TYPE.
15944 enumerator-list:
15945 enumerator-definition
15946 enumerator-list , enumerator-definition */
15948 static void
15949 cp_parser_enumerator_list (cp_parser* parser, tree type)
15951 while (true)
15953 /* Parse an enumerator-definition. */
15954 cp_parser_enumerator_definition (parser, type);
15956 /* If the next token is not a ',', we've reached the end of
15957 the list. */
15958 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15959 break;
15960 /* Otherwise, consume the `,' and keep going. */
15961 cp_lexer_consume_token (parser->lexer);
15962 /* If the next token is a `}', there is a trailing comma. */
15963 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15965 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15966 pedwarn (input_location, OPT_Wpedantic,
15967 "comma at end of enumerator list");
15968 break;
15973 /* Parse an enumerator-definition. The enumerator has the indicated
15974 TYPE.
15976 enumerator-definition:
15977 enumerator
15978 enumerator = constant-expression
15980 enumerator:
15981 identifier */
15983 static void
15984 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15986 tree identifier;
15987 tree value;
15988 location_t loc;
15990 /* Save the input location because we are interested in the location
15991 of the identifier and not the location of the explicit value. */
15992 loc = cp_lexer_peek_token (parser->lexer)->location;
15994 /* Look for the identifier. */
15995 identifier = cp_parser_identifier (parser);
15996 if (identifier == error_mark_node)
15997 return;
15999 /* If the next token is an '=', then there is an explicit value. */
16000 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16002 /* Consume the `=' token. */
16003 cp_lexer_consume_token (parser->lexer);
16004 /* Parse the value. */
16005 value = cp_parser_constant_expression (parser);
16007 else
16008 value = NULL_TREE;
16010 /* If we are processing a template, make sure the initializer of the
16011 enumerator doesn't contain any bare template parameter pack. */
16012 if (check_for_bare_parameter_packs (value))
16013 value = error_mark_node;
16015 /* integral_constant_value will pull out this expression, so make sure
16016 it's folded as appropriate. */
16017 value = fold_non_dependent_expr (value);
16019 /* Create the enumerator. */
16020 build_enumerator (identifier, value, type, loc);
16023 /* Parse a namespace-name.
16025 namespace-name:
16026 original-namespace-name
16027 namespace-alias
16029 Returns the NAMESPACE_DECL for the namespace. */
16031 static tree
16032 cp_parser_namespace_name (cp_parser* parser)
16034 tree identifier;
16035 tree namespace_decl;
16037 cp_token *token = cp_lexer_peek_token (parser->lexer);
16039 /* Get the name of the namespace. */
16040 identifier = cp_parser_identifier (parser);
16041 if (identifier == error_mark_node)
16042 return error_mark_node;
16044 /* Look up the identifier in the currently active scope. Look only
16045 for namespaces, due to:
16047 [basic.lookup.udir]
16049 When looking up a namespace-name in a using-directive or alias
16050 definition, only namespace names are considered.
16052 And:
16054 [basic.lookup.qual]
16056 During the lookup of a name preceding the :: scope resolution
16057 operator, object, function, and enumerator names are ignored.
16059 (Note that cp_parser_qualifying_entity only calls this
16060 function if the token after the name is the scope resolution
16061 operator.) */
16062 namespace_decl = cp_parser_lookup_name (parser, identifier,
16063 none_type,
16064 /*is_template=*/false,
16065 /*is_namespace=*/true,
16066 /*check_dependency=*/true,
16067 /*ambiguous_decls=*/NULL,
16068 token->location);
16069 /* If it's not a namespace, issue an error. */
16070 if (namespace_decl == error_mark_node
16071 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16073 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16074 error_at (token->location, "%qD is not a namespace-name", identifier);
16075 cp_parser_error (parser, "expected namespace-name");
16076 namespace_decl = error_mark_node;
16079 return namespace_decl;
16082 /* Parse a namespace-definition.
16084 namespace-definition:
16085 named-namespace-definition
16086 unnamed-namespace-definition
16088 named-namespace-definition:
16089 original-namespace-definition
16090 extension-namespace-definition
16092 original-namespace-definition:
16093 namespace identifier { namespace-body }
16095 extension-namespace-definition:
16096 namespace original-namespace-name { namespace-body }
16098 unnamed-namespace-definition:
16099 namespace { namespace-body } */
16101 static void
16102 cp_parser_namespace_definition (cp_parser* parser)
16104 tree identifier, attribs;
16105 bool has_visibility;
16106 bool is_inline;
16108 cp_ensure_no_omp_declare_simd (parser);
16109 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16111 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16112 is_inline = true;
16113 cp_lexer_consume_token (parser->lexer);
16115 else
16116 is_inline = false;
16118 /* Look for the `namespace' keyword. */
16119 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16121 /* Get the name of the namespace. We do not attempt to distinguish
16122 between an original-namespace-definition and an
16123 extension-namespace-definition at this point. The semantic
16124 analysis routines are responsible for that. */
16125 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16126 identifier = cp_parser_identifier (parser);
16127 else
16128 identifier = NULL_TREE;
16130 /* Parse any specified attributes. */
16131 attribs = cp_parser_attributes_opt (parser);
16133 /* Look for the `{' to start the namespace. */
16134 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16135 /* Start the namespace. */
16136 push_namespace (identifier);
16138 /* "inline namespace" is equivalent to a stub namespace definition
16139 followed by a strong using directive. */
16140 if (is_inline)
16142 tree name_space = current_namespace;
16143 /* Set up namespace association. */
16144 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16145 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16146 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16147 /* Import the contents of the inline namespace. */
16148 pop_namespace ();
16149 do_using_directive (name_space);
16150 push_namespace (identifier);
16153 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16155 /* Parse the body of the namespace. */
16156 cp_parser_namespace_body (parser);
16158 if (has_visibility)
16159 pop_visibility (1);
16161 /* Finish the namespace. */
16162 pop_namespace ();
16163 /* Look for the final `}'. */
16164 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16167 /* Parse a namespace-body.
16169 namespace-body:
16170 declaration-seq [opt] */
16172 static void
16173 cp_parser_namespace_body (cp_parser* parser)
16175 cp_parser_declaration_seq_opt (parser);
16178 /* Parse a namespace-alias-definition.
16180 namespace-alias-definition:
16181 namespace identifier = qualified-namespace-specifier ; */
16183 static void
16184 cp_parser_namespace_alias_definition (cp_parser* parser)
16186 tree identifier;
16187 tree namespace_specifier;
16189 cp_token *token = cp_lexer_peek_token (parser->lexer);
16191 /* Look for the `namespace' keyword. */
16192 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16193 /* Look for the identifier. */
16194 identifier = cp_parser_identifier (parser);
16195 if (identifier == error_mark_node)
16196 return;
16197 /* Look for the `=' token. */
16198 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16199 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16201 error_at (token->location, "%<namespace%> definition is not allowed here");
16202 /* Skip the definition. */
16203 cp_lexer_consume_token (parser->lexer);
16204 if (cp_parser_skip_to_closing_brace (parser))
16205 cp_lexer_consume_token (parser->lexer);
16206 return;
16208 cp_parser_require (parser, CPP_EQ, RT_EQ);
16209 /* Look for the qualified-namespace-specifier. */
16210 namespace_specifier
16211 = cp_parser_qualified_namespace_specifier (parser);
16212 /* Look for the `;' token. */
16213 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16215 /* Register the alias in the symbol table. */
16216 do_namespace_alias (identifier, namespace_specifier);
16219 /* Parse a qualified-namespace-specifier.
16221 qualified-namespace-specifier:
16222 :: [opt] nested-name-specifier [opt] namespace-name
16224 Returns a NAMESPACE_DECL corresponding to the specified
16225 namespace. */
16227 static tree
16228 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16230 /* Look for the optional `::'. */
16231 cp_parser_global_scope_opt (parser,
16232 /*current_scope_valid_p=*/false);
16234 /* Look for the optional nested-name-specifier. */
16235 cp_parser_nested_name_specifier_opt (parser,
16236 /*typename_keyword_p=*/false,
16237 /*check_dependency_p=*/true,
16238 /*type_p=*/false,
16239 /*is_declaration=*/true);
16241 return cp_parser_namespace_name (parser);
16244 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16245 access declaration.
16247 using-declaration:
16248 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16249 using :: unqualified-id ;
16251 access-declaration:
16252 qualified-id ;
16256 static bool
16257 cp_parser_using_declaration (cp_parser* parser,
16258 bool access_declaration_p)
16260 cp_token *token;
16261 bool typename_p = false;
16262 bool global_scope_p;
16263 tree decl;
16264 tree identifier;
16265 tree qscope;
16266 int oldcount = errorcount;
16267 cp_token *diag_token = NULL;
16269 if (access_declaration_p)
16271 diag_token = cp_lexer_peek_token (parser->lexer);
16272 cp_parser_parse_tentatively (parser);
16274 else
16276 /* Look for the `using' keyword. */
16277 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16279 /* Peek at the next token. */
16280 token = cp_lexer_peek_token (parser->lexer);
16281 /* See if it's `typename'. */
16282 if (token->keyword == RID_TYPENAME)
16284 /* Remember that we've seen it. */
16285 typename_p = true;
16286 /* Consume the `typename' token. */
16287 cp_lexer_consume_token (parser->lexer);
16291 /* Look for the optional global scope qualification. */
16292 global_scope_p
16293 = (cp_parser_global_scope_opt (parser,
16294 /*current_scope_valid_p=*/false)
16295 != NULL_TREE);
16297 /* If we saw `typename', or didn't see `::', then there must be a
16298 nested-name-specifier present. */
16299 if (typename_p || !global_scope_p)
16301 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16302 /*check_dependency_p=*/true,
16303 /*type_p=*/false,
16304 /*is_declaration=*/true);
16305 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16307 cp_parser_skip_to_end_of_block_or_statement (parser);
16308 return false;
16311 /* Otherwise, we could be in either of the two productions. In that
16312 case, treat the nested-name-specifier as optional. */
16313 else
16314 qscope = cp_parser_nested_name_specifier_opt (parser,
16315 /*typename_keyword_p=*/false,
16316 /*check_dependency_p=*/true,
16317 /*type_p=*/false,
16318 /*is_declaration=*/true);
16319 if (!qscope)
16320 qscope = global_namespace;
16321 else if (UNSCOPED_ENUM_P (qscope))
16322 qscope = CP_TYPE_CONTEXT (qscope);
16324 if (access_declaration_p && cp_parser_error_occurred (parser))
16325 /* Something has already gone wrong; there's no need to parse
16326 further. Since an error has occurred, the return value of
16327 cp_parser_parse_definitely will be false, as required. */
16328 return cp_parser_parse_definitely (parser);
16330 token = cp_lexer_peek_token (parser->lexer);
16331 /* Parse the unqualified-id. */
16332 identifier = cp_parser_unqualified_id (parser,
16333 /*template_keyword_p=*/false,
16334 /*check_dependency_p=*/true,
16335 /*declarator_p=*/true,
16336 /*optional_p=*/false);
16338 if (access_declaration_p)
16340 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16341 cp_parser_simulate_error (parser);
16342 if (!cp_parser_parse_definitely (parser))
16343 return false;
16346 /* The function we call to handle a using-declaration is different
16347 depending on what scope we are in. */
16348 if (qscope == error_mark_node || identifier == error_mark_node)
16350 else if (!identifier_p (identifier)
16351 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16352 /* [namespace.udecl]
16354 A using declaration shall not name a template-id. */
16355 error_at (token->location,
16356 "a template-id may not appear in a using-declaration");
16357 else
16359 if (at_class_scope_p ())
16361 /* Create the USING_DECL. */
16362 decl = do_class_using_decl (parser->scope, identifier);
16364 if (decl && typename_p)
16365 USING_DECL_TYPENAME_P (decl) = 1;
16367 if (check_for_bare_parameter_packs (decl))
16369 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16370 return false;
16372 else
16373 /* Add it to the list of members in this class. */
16374 finish_member_declaration (decl);
16376 else
16378 decl = cp_parser_lookup_name_simple (parser,
16379 identifier,
16380 token->location);
16381 if (decl == error_mark_node)
16382 cp_parser_name_lookup_error (parser, identifier,
16383 decl, NLE_NULL,
16384 token->location);
16385 else if (check_for_bare_parameter_packs (decl))
16387 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16388 return false;
16390 else if (!at_namespace_scope_p ())
16391 do_local_using_decl (decl, qscope, identifier);
16392 else
16393 do_toplevel_using_decl (decl, qscope, identifier);
16397 /* Look for the final `;'. */
16398 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16400 if (access_declaration_p && errorcount == oldcount)
16401 warning_at (diag_token->location, OPT_Wdeprecated,
16402 "access declarations are deprecated "
16403 "in favour of using-declarations; "
16404 "suggestion: add the %<using%> keyword");
16406 return true;
16409 /* Parse an alias-declaration.
16411 alias-declaration:
16412 using identifier attribute-specifier-seq [opt] = type-id */
16414 static tree
16415 cp_parser_alias_declaration (cp_parser* parser)
16417 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16418 location_t id_location;
16419 cp_declarator *declarator;
16420 cp_decl_specifier_seq decl_specs;
16421 bool member_p;
16422 const char *saved_message = NULL;
16424 /* Look for the `using' keyword. */
16425 cp_token *using_token
16426 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16427 if (using_token == NULL)
16428 return error_mark_node;
16430 id_location = cp_lexer_peek_token (parser->lexer)->location;
16431 id = cp_parser_identifier (parser);
16432 if (id == error_mark_node)
16433 return error_mark_node;
16435 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16436 attributes = cp_parser_attributes_opt (parser);
16437 if (attributes == error_mark_node)
16438 return error_mark_node;
16440 cp_parser_require (parser, CPP_EQ, RT_EQ);
16442 if (cp_parser_error_occurred (parser))
16443 return error_mark_node;
16445 cp_parser_commit_to_tentative_parse (parser);
16447 /* Now we are going to parse the type-id of the declaration. */
16450 [dcl.type]/3 says:
16452 "A type-specifier-seq shall not define a class or enumeration
16453 unless it appears in the type-id of an alias-declaration (7.1.3) that
16454 is not the declaration of a template-declaration."
16456 In other words, if we currently are in an alias template, the
16457 type-id should not define a type.
16459 So let's set parser->type_definition_forbidden_message in that
16460 case; cp_parser_check_type_definition (called by
16461 cp_parser_class_specifier) will then emit an error if a type is
16462 defined in the type-id. */
16463 if (parser->num_template_parameter_lists)
16465 saved_message = parser->type_definition_forbidden_message;
16466 parser->type_definition_forbidden_message =
16467 G_("types may not be defined in alias template declarations");
16470 type = cp_parser_type_id (parser);
16472 /* Restore the error message if need be. */
16473 if (parser->num_template_parameter_lists)
16474 parser->type_definition_forbidden_message = saved_message;
16476 if (type == error_mark_node
16477 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16479 cp_parser_skip_to_end_of_block_or_statement (parser);
16480 return error_mark_node;
16483 /* A typedef-name can also be introduced by an alias-declaration. The
16484 identifier following the using keyword becomes a typedef-name. It has
16485 the same semantics as if it were introduced by the typedef
16486 specifier. In particular, it does not define a new type and it shall
16487 not appear in the type-id. */
16489 clear_decl_specs (&decl_specs);
16490 decl_specs.type = type;
16491 if (attributes != NULL_TREE)
16493 decl_specs.attributes = attributes;
16494 set_and_check_decl_spec_loc (&decl_specs,
16495 ds_attribute,
16496 attrs_token);
16498 set_and_check_decl_spec_loc (&decl_specs,
16499 ds_typedef,
16500 using_token);
16501 set_and_check_decl_spec_loc (&decl_specs,
16502 ds_alias,
16503 using_token);
16505 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16506 declarator->id_loc = id_location;
16508 member_p = at_class_scope_p ();
16509 if (member_p)
16510 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16511 NULL_TREE, attributes);
16512 else
16513 decl = start_decl (declarator, &decl_specs, 0,
16514 attributes, NULL_TREE, &pushed_scope);
16515 if (decl == error_mark_node)
16516 return decl;
16518 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16520 if (pushed_scope)
16521 pop_scope (pushed_scope);
16523 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16524 added into the symbol table; otherwise, return the TYPE_DECL. */
16525 if (DECL_LANG_SPECIFIC (decl)
16526 && DECL_TEMPLATE_INFO (decl)
16527 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16529 decl = DECL_TI_TEMPLATE (decl);
16530 if (member_p)
16531 check_member_template (decl);
16534 return decl;
16537 /* Parse a using-directive.
16539 using-directive:
16540 using namespace :: [opt] nested-name-specifier [opt]
16541 namespace-name ; */
16543 static void
16544 cp_parser_using_directive (cp_parser* parser)
16546 tree namespace_decl;
16547 tree attribs;
16549 /* Look for the `using' keyword. */
16550 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16551 /* And the `namespace' keyword. */
16552 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16553 /* Look for the optional `::' operator. */
16554 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16555 /* And the optional nested-name-specifier. */
16556 cp_parser_nested_name_specifier_opt (parser,
16557 /*typename_keyword_p=*/false,
16558 /*check_dependency_p=*/true,
16559 /*type_p=*/false,
16560 /*is_declaration=*/true);
16561 /* Get the namespace being used. */
16562 namespace_decl = cp_parser_namespace_name (parser);
16563 /* And any specified attributes. */
16564 attribs = cp_parser_attributes_opt (parser);
16565 /* Update the symbol table. */
16566 parse_using_directive (namespace_decl, attribs);
16567 /* Look for the final `;'. */
16568 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16571 /* Parse an asm-definition.
16573 asm-definition:
16574 asm ( string-literal ) ;
16576 GNU Extension:
16578 asm-definition:
16579 asm volatile [opt] ( string-literal ) ;
16580 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16581 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16582 : asm-operand-list [opt] ) ;
16583 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16584 : asm-operand-list [opt]
16585 : asm-clobber-list [opt] ) ;
16586 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16587 : asm-clobber-list [opt]
16588 : asm-goto-list ) ; */
16590 static void
16591 cp_parser_asm_definition (cp_parser* parser)
16593 tree string;
16594 tree outputs = NULL_TREE;
16595 tree inputs = NULL_TREE;
16596 tree clobbers = NULL_TREE;
16597 tree labels = NULL_TREE;
16598 tree asm_stmt;
16599 bool volatile_p = false;
16600 bool extended_p = false;
16601 bool invalid_inputs_p = false;
16602 bool invalid_outputs_p = false;
16603 bool goto_p = false;
16604 required_token missing = RT_NONE;
16606 /* Look for the `asm' keyword. */
16607 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16609 if (parser->in_function_body
16610 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16611 error ("%<asm%> in %<constexpr%> function");
16613 /* See if the next token is `volatile'. */
16614 if (cp_parser_allow_gnu_extensions_p (parser)
16615 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16617 /* Remember that we saw the `volatile' keyword. */
16618 volatile_p = true;
16619 /* Consume the token. */
16620 cp_lexer_consume_token (parser->lexer);
16622 if (cp_parser_allow_gnu_extensions_p (parser)
16623 && parser->in_function_body
16624 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16626 /* Remember that we saw the `goto' keyword. */
16627 goto_p = true;
16628 /* Consume the token. */
16629 cp_lexer_consume_token (parser->lexer);
16631 /* Look for the opening `('. */
16632 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16633 return;
16634 /* Look for the string. */
16635 string = cp_parser_string_literal (parser, false, false);
16636 if (string == error_mark_node)
16638 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16639 /*consume_paren=*/true);
16640 return;
16643 /* If we're allowing GNU extensions, check for the extended assembly
16644 syntax. Unfortunately, the `:' tokens need not be separated by
16645 a space in C, and so, for compatibility, we tolerate that here
16646 too. Doing that means that we have to treat the `::' operator as
16647 two `:' tokens. */
16648 if (cp_parser_allow_gnu_extensions_p (parser)
16649 && parser->in_function_body
16650 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16651 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16653 bool inputs_p = false;
16654 bool clobbers_p = false;
16655 bool labels_p = false;
16657 /* The extended syntax was used. */
16658 extended_p = true;
16660 /* Look for outputs. */
16661 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16663 /* Consume the `:'. */
16664 cp_lexer_consume_token (parser->lexer);
16665 /* Parse the output-operands. */
16666 if (cp_lexer_next_token_is_not (parser->lexer,
16667 CPP_COLON)
16668 && cp_lexer_next_token_is_not (parser->lexer,
16669 CPP_SCOPE)
16670 && cp_lexer_next_token_is_not (parser->lexer,
16671 CPP_CLOSE_PAREN)
16672 && !goto_p)
16673 outputs = cp_parser_asm_operand_list (parser);
16675 if (outputs == error_mark_node)
16676 invalid_outputs_p = true;
16678 /* If the next token is `::', there are no outputs, and the
16679 next token is the beginning of the inputs. */
16680 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16681 /* The inputs are coming next. */
16682 inputs_p = true;
16684 /* Look for inputs. */
16685 if (inputs_p
16686 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16688 /* Consume the `:' or `::'. */
16689 cp_lexer_consume_token (parser->lexer);
16690 /* Parse the output-operands. */
16691 if (cp_lexer_next_token_is_not (parser->lexer,
16692 CPP_COLON)
16693 && cp_lexer_next_token_is_not (parser->lexer,
16694 CPP_SCOPE)
16695 && cp_lexer_next_token_is_not (parser->lexer,
16696 CPP_CLOSE_PAREN))
16697 inputs = cp_parser_asm_operand_list (parser);
16699 if (inputs == error_mark_node)
16700 invalid_inputs_p = true;
16702 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16703 /* The clobbers are coming next. */
16704 clobbers_p = true;
16706 /* Look for clobbers. */
16707 if (clobbers_p
16708 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16710 clobbers_p = true;
16711 /* Consume the `:' or `::'. */
16712 cp_lexer_consume_token (parser->lexer);
16713 /* Parse the clobbers. */
16714 if (cp_lexer_next_token_is_not (parser->lexer,
16715 CPP_COLON)
16716 && cp_lexer_next_token_is_not (parser->lexer,
16717 CPP_CLOSE_PAREN))
16718 clobbers = cp_parser_asm_clobber_list (parser);
16720 else if (goto_p
16721 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16722 /* The labels are coming next. */
16723 labels_p = true;
16725 /* Look for labels. */
16726 if (labels_p
16727 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16729 labels_p = true;
16730 /* Consume the `:' or `::'. */
16731 cp_lexer_consume_token (parser->lexer);
16732 /* Parse the labels. */
16733 labels = cp_parser_asm_label_list (parser);
16736 if (goto_p && !labels_p)
16737 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16739 else if (goto_p)
16740 missing = RT_COLON_SCOPE;
16742 /* Look for the closing `)'. */
16743 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16744 missing ? missing : RT_CLOSE_PAREN))
16745 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16746 /*consume_paren=*/true);
16747 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16749 if (!invalid_inputs_p && !invalid_outputs_p)
16751 /* Create the ASM_EXPR. */
16752 if (parser->in_function_body)
16754 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16755 inputs, clobbers, labels);
16756 /* If the extended syntax was not used, mark the ASM_EXPR. */
16757 if (!extended_p)
16759 tree temp = asm_stmt;
16760 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16761 temp = TREE_OPERAND (temp, 0);
16763 ASM_INPUT_P (temp) = 1;
16766 else
16767 symtab->finalize_toplevel_asm (string);
16771 /* Declarators [gram.dcl.decl] */
16773 /* Parse an init-declarator.
16775 init-declarator:
16776 declarator initializer [opt]
16778 GNU Extension:
16780 init-declarator:
16781 declarator asm-specification [opt] attributes [opt] initializer [opt]
16783 function-definition:
16784 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16785 function-body
16786 decl-specifier-seq [opt] declarator function-try-block
16788 GNU Extension:
16790 function-definition:
16791 __extension__ function-definition
16793 TM Extension:
16795 function-definition:
16796 decl-specifier-seq [opt] declarator function-transaction-block
16798 The DECL_SPECIFIERS apply to this declarator. Returns a
16799 representation of the entity declared. If MEMBER_P is TRUE, then
16800 this declarator appears in a class scope. The new DECL created by
16801 this declarator is returned.
16803 The CHECKS are access checks that should be performed once we know
16804 what entity is being declared (and, therefore, what classes have
16805 befriended it).
16807 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16808 for a function-definition here as well. If the declarator is a
16809 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16810 be TRUE upon return. By that point, the function-definition will
16811 have been completely parsed.
16813 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16814 is FALSE.
16816 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16817 parsed declaration if it is an uninitialized single declarator not followed
16818 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16819 if present, will not be consumed. If returned, this declarator will be
16820 created with SD_INITIALIZED but will not call cp_finish_decl. */
16822 static tree
16823 cp_parser_init_declarator (cp_parser* parser,
16824 cp_decl_specifier_seq *decl_specifiers,
16825 vec<deferred_access_check, va_gc> *checks,
16826 bool function_definition_allowed_p,
16827 bool member_p,
16828 int declares_class_or_enum,
16829 bool* function_definition_p,
16830 tree* maybe_range_for_decl)
16832 cp_token *token = NULL, *asm_spec_start_token = NULL,
16833 *attributes_start_token = NULL;
16834 cp_declarator *declarator;
16835 tree prefix_attributes;
16836 tree attributes = NULL;
16837 tree asm_specification;
16838 tree initializer;
16839 tree decl = NULL_TREE;
16840 tree scope;
16841 int is_initialized;
16842 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16843 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16844 "(...)". */
16845 enum cpp_ttype initialization_kind;
16846 bool is_direct_init = false;
16847 bool is_non_constant_init;
16848 int ctor_dtor_or_conv_p;
16849 bool friend_p = cp_parser_friend_p (decl_specifiers);
16850 tree pushed_scope = NULL_TREE;
16851 bool range_for_decl_p = false;
16852 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16854 /* Gather the attributes that were provided with the
16855 decl-specifiers. */
16856 prefix_attributes = decl_specifiers->attributes;
16858 /* Assume that this is not the declarator for a function
16859 definition. */
16860 if (function_definition_p)
16861 *function_definition_p = false;
16863 /* Default arguments are only permitted for function parameters. */
16864 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16865 parser->default_arg_ok_p = false;
16867 /* Defer access checks while parsing the declarator; we cannot know
16868 what names are accessible until we know what is being
16869 declared. */
16870 resume_deferring_access_checks ();
16872 /* Parse the declarator. */
16873 token = cp_lexer_peek_token (parser->lexer);
16874 declarator
16875 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16876 &ctor_dtor_or_conv_p,
16877 /*parenthesized_p=*/NULL,
16878 member_p, friend_p);
16879 /* Gather up the deferred checks. */
16880 stop_deferring_access_checks ();
16882 parser->default_arg_ok_p = saved_default_arg_ok_p;
16884 /* If the DECLARATOR was erroneous, there's no need to go
16885 further. */
16886 if (declarator == cp_error_declarator)
16887 return error_mark_node;
16889 /* Check that the number of template-parameter-lists is OK. */
16890 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16891 token->location))
16892 return error_mark_node;
16894 if (declares_class_or_enum & 2)
16895 cp_parser_check_for_definition_in_return_type (declarator,
16896 decl_specifiers->type,
16897 decl_specifiers->locations[ds_type_spec]);
16899 /* Figure out what scope the entity declared by the DECLARATOR is
16900 located in. `grokdeclarator' sometimes changes the scope, so
16901 we compute it now. */
16902 scope = get_scope_of_declarator (declarator);
16904 /* Perform any lookups in the declared type which were thought to be
16905 dependent, but are not in the scope of the declarator. */
16906 decl_specifiers->type
16907 = maybe_update_decl_type (decl_specifiers->type, scope);
16909 /* If we're allowing GNU extensions, look for an
16910 asm-specification. */
16911 if (cp_parser_allow_gnu_extensions_p (parser))
16913 /* Look for an asm-specification. */
16914 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16915 asm_specification = cp_parser_asm_specification_opt (parser);
16917 else
16918 asm_specification = NULL_TREE;
16920 /* Look for attributes. */
16921 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16922 attributes = cp_parser_attributes_opt (parser);
16924 /* Peek at the next token. */
16925 token = cp_lexer_peek_token (parser->lexer);
16927 bool bogus_implicit_tmpl = false;
16929 if (function_declarator_p (declarator))
16931 /* Check to see if the token indicates the start of a
16932 function-definition. */
16933 if (cp_parser_token_starts_function_definition_p (token))
16935 if (!function_definition_allowed_p)
16937 /* If a function-definition should not appear here, issue an
16938 error message. */
16939 cp_parser_error (parser,
16940 "a function-definition is not allowed here");
16941 return error_mark_node;
16944 location_t func_brace_location
16945 = cp_lexer_peek_token (parser->lexer)->location;
16947 /* Neither attributes nor an asm-specification are allowed
16948 on a function-definition. */
16949 if (asm_specification)
16950 error_at (asm_spec_start_token->location,
16951 "an asm-specification is not allowed "
16952 "on a function-definition");
16953 if (attributes)
16954 error_at (attributes_start_token->location,
16955 "attributes are not allowed "
16956 "on a function-definition");
16957 /* This is a function-definition. */
16958 *function_definition_p = true;
16960 /* Parse the function definition. */
16961 if (member_p)
16962 decl = cp_parser_save_member_function_body (parser,
16963 decl_specifiers,
16964 declarator,
16965 prefix_attributes);
16966 else
16967 decl =
16968 (cp_parser_function_definition_from_specifiers_and_declarator
16969 (parser, decl_specifiers, prefix_attributes, declarator));
16971 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16973 /* This is where the prologue starts... */
16974 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16975 = func_brace_location;
16978 return decl;
16981 else if (parser->fully_implicit_function_template_p)
16983 /* A non-template declaration involving a function parameter list
16984 containing an implicit template parameter will be made into a
16985 template. If the resulting declaration is not going to be an
16986 actual function then finish the template scope here to prevent it.
16987 An error message will be issued once we have a decl to talk about.
16989 FIXME probably we should do type deduction rather than create an
16990 implicit template, but the standard currently doesn't allow it. */
16991 bogus_implicit_tmpl = true;
16992 finish_fully_implicit_template (parser, NULL_TREE);
16995 /* [dcl.dcl]
16997 Only in function declarations for constructors, destructors, and
16998 type conversions can the decl-specifier-seq be omitted.
17000 We explicitly postpone this check past the point where we handle
17001 function-definitions because we tolerate function-definitions
17002 that are missing their return types in some modes. */
17003 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17005 cp_parser_error (parser,
17006 "expected constructor, destructor, or type conversion");
17007 return error_mark_node;
17010 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17011 if (token->type == CPP_EQ
17012 || token->type == CPP_OPEN_PAREN
17013 || token->type == CPP_OPEN_BRACE)
17015 is_initialized = SD_INITIALIZED;
17016 initialization_kind = token->type;
17017 if (maybe_range_for_decl)
17018 *maybe_range_for_decl = error_mark_node;
17020 if (token->type == CPP_EQ
17021 && function_declarator_p (declarator))
17023 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17024 if (t2->keyword == RID_DEFAULT)
17025 is_initialized = SD_DEFAULTED;
17026 else if (t2->keyword == RID_DELETE)
17027 is_initialized = SD_DELETED;
17030 else
17032 /* If the init-declarator isn't initialized and isn't followed by a
17033 `,' or `;', it's not a valid init-declarator. */
17034 if (token->type != CPP_COMMA
17035 && token->type != CPP_SEMICOLON)
17037 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17038 range_for_decl_p = true;
17039 else
17041 cp_parser_error (parser, "expected initializer");
17042 return error_mark_node;
17045 is_initialized = SD_UNINITIALIZED;
17046 initialization_kind = CPP_EOF;
17049 /* Because start_decl has side-effects, we should only call it if we
17050 know we're going ahead. By this point, we know that we cannot
17051 possibly be looking at any other construct. */
17052 cp_parser_commit_to_tentative_parse (parser);
17054 /* Enter the newly declared entry in the symbol table. If we're
17055 processing a declaration in a class-specifier, we wait until
17056 after processing the initializer. */
17057 if (!member_p)
17059 if (parser->in_unbraced_linkage_specification_p)
17060 decl_specifiers->storage_class = sc_extern;
17061 decl = start_decl (declarator, decl_specifiers,
17062 range_for_decl_p? SD_INITIALIZED : is_initialized,
17063 attributes, prefix_attributes, &pushed_scope);
17064 cp_finalize_omp_declare_simd (parser, decl);
17065 /* Adjust location of decl if declarator->id_loc is more appropriate:
17066 set, and decl wasn't merged with another decl, in which case its
17067 location would be different from input_location, and more accurate. */
17068 if (DECL_P (decl)
17069 && declarator->id_loc != UNKNOWN_LOCATION
17070 && DECL_SOURCE_LOCATION (decl) == input_location)
17071 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17073 else if (scope)
17074 /* Enter the SCOPE. That way unqualified names appearing in the
17075 initializer will be looked up in SCOPE. */
17076 pushed_scope = push_scope (scope);
17078 /* Perform deferred access control checks, now that we know in which
17079 SCOPE the declared entity resides. */
17080 if (!member_p && decl)
17082 tree saved_current_function_decl = NULL_TREE;
17084 /* If the entity being declared is a function, pretend that we
17085 are in its scope. If it is a `friend', it may have access to
17086 things that would not otherwise be accessible. */
17087 if (TREE_CODE (decl) == FUNCTION_DECL)
17089 saved_current_function_decl = current_function_decl;
17090 current_function_decl = decl;
17093 /* Perform access checks for template parameters. */
17094 cp_parser_perform_template_parameter_access_checks (checks);
17096 /* Perform the access control checks for the declarator and the
17097 decl-specifiers. */
17098 perform_deferred_access_checks (tf_warning_or_error);
17100 /* Restore the saved value. */
17101 if (TREE_CODE (decl) == FUNCTION_DECL)
17102 current_function_decl = saved_current_function_decl;
17105 /* Parse the initializer. */
17106 initializer = NULL_TREE;
17107 is_direct_init = false;
17108 is_non_constant_init = true;
17109 if (is_initialized)
17111 if (function_declarator_p (declarator))
17113 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
17114 if (initialization_kind == CPP_EQ)
17115 initializer = cp_parser_pure_specifier (parser);
17116 else
17118 /* If the declaration was erroneous, we don't really
17119 know what the user intended, so just silently
17120 consume the initializer. */
17121 if (decl != error_mark_node)
17122 error_at (initializer_start_token->location,
17123 "initializer provided for function");
17124 cp_parser_skip_to_closing_parenthesis (parser,
17125 /*recovering=*/true,
17126 /*or_comma=*/false,
17127 /*consume_paren=*/true);
17130 else
17132 /* We want to record the extra mangling scope for in-class
17133 initializers of class members and initializers of static data
17134 member templates. The former involves deferring
17135 parsing of the initializer until end of class as with default
17136 arguments. So right here we only handle the latter. */
17137 if (!member_p && processing_template_decl)
17138 start_lambda_scope (decl);
17139 initializer = cp_parser_initializer (parser,
17140 &is_direct_init,
17141 &is_non_constant_init);
17142 if (!member_p && processing_template_decl)
17143 finish_lambda_scope ();
17144 if (initializer == error_mark_node)
17145 cp_parser_skip_to_end_of_statement (parser);
17149 /* The old parser allows attributes to appear after a parenthesized
17150 initializer. Mark Mitchell proposed removing this functionality
17151 on the GCC mailing lists on 2002-08-13. This parser accepts the
17152 attributes -- but ignores them. */
17153 if (cp_parser_allow_gnu_extensions_p (parser)
17154 && initialization_kind == CPP_OPEN_PAREN)
17155 if (cp_parser_attributes_opt (parser))
17156 warning (OPT_Wattributes,
17157 "attributes after parenthesized initializer ignored");
17159 /* And now complain about a non-function implicit template. */
17160 if (bogus_implicit_tmpl)
17161 error_at (DECL_SOURCE_LOCATION (decl),
17162 "non-function %qD declared as implicit template", decl);
17164 /* For an in-class declaration, use `grokfield' to create the
17165 declaration. */
17166 if (member_p)
17168 if (pushed_scope)
17170 pop_scope (pushed_scope);
17171 pushed_scope = NULL_TREE;
17173 decl = grokfield (declarator, decl_specifiers,
17174 initializer, !is_non_constant_init,
17175 /*asmspec=*/NULL_TREE,
17176 chainon (attributes, prefix_attributes));
17177 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17178 cp_parser_save_default_args (parser, decl);
17179 cp_finalize_omp_declare_simd (parser, decl);
17182 /* Finish processing the declaration. But, skip member
17183 declarations. */
17184 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17186 cp_finish_decl (decl,
17187 initializer, !is_non_constant_init,
17188 asm_specification,
17189 /* If the initializer is in parentheses, then this is
17190 a direct-initialization, which means that an
17191 `explicit' constructor is OK. Otherwise, an
17192 `explicit' constructor cannot be used. */
17193 ((is_direct_init || !is_initialized)
17194 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17196 else if ((cxx_dialect != cxx98) && friend_p
17197 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17198 /* Core issue #226 (C++0x only): A default template-argument
17199 shall not be specified in a friend class template
17200 declaration. */
17201 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17202 /*is_partial=*/false, /*is_friend_decl=*/1);
17204 if (!friend_p && pushed_scope)
17205 pop_scope (pushed_scope);
17207 if (function_declarator_p (declarator)
17208 && parser->fully_implicit_function_template_p)
17210 if (member_p)
17211 decl = finish_fully_implicit_template (parser, decl);
17212 else
17213 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17216 return decl;
17219 /* Parse a declarator.
17221 declarator:
17222 direct-declarator
17223 ptr-operator declarator
17225 abstract-declarator:
17226 ptr-operator abstract-declarator [opt]
17227 direct-abstract-declarator
17229 GNU Extensions:
17231 declarator:
17232 attributes [opt] direct-declarator
17233 attributes [opt] ptr-operator declarator
17235 abstract-declarator:
17236 attributes [opt] ptr-operator abstract-declarator [opt]
17237 attributes [opt] direct-abstract-declarator
17239 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17240 detect constructor, destructor or conversion operators. It is set
17241 to -1 if the declarator is a name, and +1 if it is a
17242 function. Otherwise it is set to zero. Usually you just want to
17243 test for >0, but internally the negative value is used.
17245 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17246 a decl-specifier-seq unless it declares a constructor, destructor,
17247 or conversion. It might seem that we could check this condition in
17248 semantic analysis, rather than parsing, but that makes it difficult
17249 to handle something like `f()'. We want to notice that there are
17250 no decl-specifiers, and therefore realize that this is an
17251 expression, not a declaration.)
17253 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17254 the declarator is a direct-declarator of the form "(...)".
17256 MEMBER_P is true iff this declarator is a member-declarator.
17258 FRIEND_P is true iff this declarator is a friend. */
17260 static cp_declarator *
17261 cp_parser_declarator (cp_parser* parser,
17262 cp_parser_declarator_kind dcl_kind,
17263 int* ctor_dtor_or_conv_p,
17264 bool* parenthesized_p,
17265 bool member_p, bool friend_p)
17267 cp_declarator *declarator;
17268 enum tree_code code;
17269 cp_cv_quals cv_quals;
17270 tree class_type;
17271 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17273 /* Assume this is not a constructor, destructor, or type-conversion
17274 operator. */
17275 if (ctor_dtor_or_conv_p)
17276 *ctor_dtor_or_conv_p = 0;
17278 if (cp_parser_allow_gnu_extensions_p (parser))
17279 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17281 /* Check for the ptr-operator production. */
17282 cp_parser_parse_tentatively (parser);
17283 /* Parse the ptr-operator. */
17284 code = cp_parser_ptr_operator (parser,
17285 &class_type,
17286 &cv_quals,
17287 &std_attributes);
17289 /* If that worked, then we have a ptr-operator. */
17290 if (cp_parser_parse_definitely (parser))
17292 /* If a ptr-operator was found, then this declarator was not
17293 parenthesized. */
17294 if (parenthesized_p)
17295 *parenthesized_p = true;
17296 /* The dependent declarator is optional if we are parsing an
17297 abstract-declarator. */
17298 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17299 cp_parser_parse_tentatively (parser);
17301 /* Parse the dependent declarator. */
17302 declarator = cp_parser_declarator (parser, dcl_kind,
17303 /*ctor_dtor_or_conv_p=*/NULL,
17304 /*parenthesized_p=*/NULL,
17305 /*member_p=*/false,
17306 friend_p);
17308 /* If we are parsing an abstract-declarator, we must handle the
17309 case where the dependent declarator is absent. */
17310 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17311 && !cp_parser_parse_definitely (parser))
17312 declarator = NULL;
17314 declarator = cp_parser_make_indirect_declarator
17315 (code, class_type, cv_quals, declarator, std_attributes);
17317 /* Everything else is a direct-declarator. */
17318 else
17320 if (parenthesized_p)
17321 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17322 CPP_OPEN_PAREN);
17323 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17324 ctor_dtor_or_conv_p,
17325 member_p, friend_p);
17328 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17329 declarator->attributes = gnu_attributes;
17330 return declarator;
17333 /* Parse a direct-declarator or direct-abstract-declarator.
17335 direct-declarator:
17336 declarator-id
17337 direct-declarator ( parameter-declaration-clause )
17338 cv-qualifier-seq [opt]
17339 ref-qualifier [opt]
17340 exception-specification [opt]
17341 direct-declarator [ constant-expression [opt] ]
17342 ( declarator )
17344 direct-abstract-declarator:
17345 direct-abstract-declarator [opt]
17346 ( parameter-declaration-clause )
17347 cv-qualifier-seq [opt]
17348 ref-qualifier [opt]
17349 exception-specification [opt]
17350 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17351 ( abstract-declarator )
17353 Returns a representation of the declarator. DCL_KIND is
17354 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17355 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17356 we are parsing a direct-declarator. It is
17357 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17358 of ambiguity we prefer an abstract declarator, as per
17359 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17360 as for cp_parser_declarator. */
17362 static cp_declarator *
17363 cp_parser_direct_declarator (cp_parser* parser,
17364 cp_parser_declarator_kind dcl_kind,
17365 int* ctor_dtor_or_conv_p,
17366 bool member_p, bool friend_p)
17368 cp_token *token;
17369 cp_declarator *declarator = NULL;
17370 tree scope = NULL_TREE;
17371 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17372 bool saved_in_declarator_p = parser->in_declarator_p;
17373 bool first = true;
17374 tree pushed_scope = NULL_TREE;
17376 while (true)
17378 /* Peek at the next token. */
17379 token = cp_lexer_peek_token (parser->lexer);
17380 if (token->type == CPP_OPEN_PAREN)
17382 /* This is either a parameter-declaration-clause, or a
17383 parenthesized declarator. When we know we are parsing a
17384 named declarator, it must be a parenthesized declarator
17385 if FIRST is true. For instance, `(int)' is a
17386 parameter-declaration-clause, with an omitted
17387 direct-abstract-declarator. But `((*))', is a
17388 parenthesized abstract declarator. Finally, when T is a
17389 template parameter `(T)' is a
17390 parameter-declaration-clause, and not a parenthesized
17391 named declarator.
17393 We first try and parse a parameter-declaration-clause,
17394 and then try a nested declarator (if FIRST is true).
17396 It is not an error for it not to be a
17397 parameter-declaration-clause, even when FIRST is
17398 false. Consider,
17400 int i (int);
17401 int i (3);
17403 The first is the declaration of a function while the
17404 second is the definition of a variable, including its
17405 initializer.
17407 Having seen only the parenthesis, we cannot know which of
17408 these two alternatives should be selected. Even more
17409 complex are examples like:
17411 int i (int (a));
17412 int i (int (3));
17414 The former is a function-declaration; the latter is a
17415 variable initialization.
17417 Thus again, we try a parameter-declaration-clause, and if
17418 that fails, we back out and return. */
17420 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17422 tree params;
17423 bool is_declarator = false;
17425 /* In a member-declarator, the only valid interpretation
17426 of a parenthesis is the start of a
17427 parameter-declaration-clause. (It is invalid to
17428 initialize a static data member with a parenthesized
17429 initializer; only the "=" form of initialization is
17430 permitted.) */
17431 if (!member_p)
17432 cp_parser_parse_tentatively (parser);
17434 /* Consume the `('. */
17435 cp_lexer_consume_token (parser->lexer);
17436 if (first)
17438 /* If this is going to be an abstract declarator, we're
17439 in a declarator and we can't have default args. */
17440 parser->default_arg_ok_p = false;
17441 parser->in_declarator_p = true;
17444 begin_scope (sk_function_parms, NULL_TREE);
17446 /* Parse the parameter-declaration-clause. */
17447 params = cp_parser_parameter_declaration_clause (parser);
17449 /* Consume the `)'. */
17450 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17452 /* If all went well, parse the cv-qualifier-seq,
17453 ref-qualifier and the exception-specification. */
17454 if (member_p || cp_parser_parse_definitely (parser))
17456 cp_cv_quals cv_quals;
17457 cp_virt_specifiers virt_specifiers;
17458 cp_ref_qualifier ref_qual;
17459 tree exception_specification;
17460 tree late_return;
17461 tree attrs;
17462 bool memfn = (member_p || (pushed_scope
17463 && CLASS_TYPE_P (pushed_scope)));
17465 is_declarator = true;
17467 if (ctor_dtor_or_conv_p)
17468 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17469 first = false;
17471 /* Parse the cv-qualifier-seq. */
17472 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17473 /* Parse the ref-qualifier. */
17474 ref_qual = cp_parser_ref_qualifier_opt (parser);
17475 /* And the exception-specification. */
17476 exception_specification
17477 = cp_parser_exception_specification_opt (parser);
17479 attrs = cp_parser_std_attribute_spec_seq (parser);
17481 /* In here, we handle cases where attribute is used after
17482 the function declaration. For example:
17483 void func (int x) __attribute__((vector(..))); */
17484 if (flag_cilkplus
17485 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17487 cp_parser_parse_tentatively (parser);
17488 tree attr = cp_parser_gnu_attributes_opt (parser);
17489 if (cp_lexer_next_token_is_not (parser->lexer,
17490 CPP_SEMICOLON)
17491 && cp_lexer_next_token_is_not (parser->lexer,
17492 CPP_OPEN_BRACE))
17493 cp_parser_abort_tentative_parse (parser);
17494 else if (!cp_parser_parse_definitely (parser))
17496 else
17497 attrs = chainon (attr, attrs);
17499 late_return = (cp_parser_late_return_type_opt
17500 (parser, declarator,
17501 memfn ? cv_quals : -1));
17504 /* Parse the virt-specifier-seq. */
17505 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17507 /* Create the function-declarator. */
17508 declarator = make_call_declarator (declarator,
17509 params,
17510 cv_quals,
17511 virt_specifiers,
17512 ref_qual,
17513 exception_specification,
17514 late_return);
17515 declarator->std_attributes = attrs;
17516 /* Any subsequent parameter lists are to do with
17517 return type, so are not those of the declared
17518 function. */
17519 parser->default_arg_ok_p = false;
17522 /* Remove the function parms from scope. */
17523 pop_bindings_and_leave_scope ();
17525 if (is_declarator)
17526 /* Repeat the main loop. */
17527 continue;
17530 /* If this is the first, we can try a parenthesized
17531 declarator. */
17532 if (first)
17534 bool saved_in_type_id_in_expr_p;
17536 parser->default_arg_ok_p = saved_default_arg_ok_p;
17537 parser->in_declarator_p = saved_in_declarator_p;
17539 /* Consume the `('. */
17540 cp_lexer_consume_token (parser->lexer);
17541 /* Parse the nested declarator. */
17542 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17543 parser->in_type_id_in_expr_p = true;
17544 declarator
17545 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17546 /*parenthesized_p=*/NULL,
17547 member_p, friend_p);
17548 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17549 first = false;
17550 /* Expect a `)'. */
17551 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17552 declarator = cp_error_declarator;
17553 if (declarator == cp_error_declarator)
17554 break;
17556 goto handle_declarator;
17558 /* Otherwise, we must be done. */
17559 else
17560 break;
17562 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17563 && token->type == CPP_OPEN_SQUARE
17564 && !cp_next_tokens_can_be_attribute_p (parser))
17566 /* Parse an array-declarator. */
17567 tree bounds, attrs;
17569 if (ctor_dtor_or_conv_p)
17570 *ctor_dtor_or_conv_p = 0;
17572 first = false;
17573 parser->default_arg_ok_p = false;
17574 parser->in_declarator_p = true;
17575 /* Consume the `['. */
17576 cp_lexer_consume_token (parser->lexer);
17577 /* Peek at the next token. */
17578 token = cp_lexer_peek_token (parser->lexer);
17579 /* If the next token is `]', then there is no
17580 constant-expression. */
17581 if (token->type != CPP_CLOSE_SQUARE)
17583 bool non_constant_p;
17584 bounds
17585 = cp_parser_constant_expression (parser,
17586 /*allow_non_constant=*/true,
17587 &non_constant_p);
17588 if (!non_constant_p)
17589 /* OK */;
17590 else if (error_operand_p (bounds))
17591 /* Already gave an error. */;
17592 else if (!parser->in_function_body
17593 || current_binding_level->kind == sk_function_parms)
17595 /* Normally, the array bound must be an integral constant
17596 expression. However, as an extension, we allow VLAs
17597 in function scopes as long as they aren't part of a
17598 parameter declaration. */
17599 cp_parser_error (parser,
17600 "array bound is not an integer constant");
17601 bounds = error_mark_node;
17603 else if (processing_template_decl
17604 && !type_dependent_expression_p (bounds))
17606 /* Remember this wasn't a constant-expression. */
17607 bounds = build_nop (TREE_TYPE (bounds), bounds);
17608 TREE_SIDE_EFFECTS (bounds) = 1;
17611 else
17612 bounds = NULL_TREE;
17613 /* Look for the closing `]'. */
17614 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17616 declarator = cp_error_declarator;
17617 break;
17620 attrs = cp_parser_std_attribute_spec_seq (parser);
17621 declarator = make_array_declarator (declarator, bounds);
17622 declarator->std_attributes = attrs;
17624 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17627 tree qualifying_scope;
17628 tree unqualified_name;
17629 tree attrs;
17630 special_function_kind sfk;
17631 bool abstract_ok;
17632 bool pack_expansion_p = false;
17633 cp_token *declarator_id_start_token;
17635 /* Parse a declarator-id */
17636 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17637 if (abstract_ok)
17639 cp_parser_parse_tentatively (parser);
17641 /* If we see an ellipsis, we should be looking at a
17642 parameter pack. */
17643 if (token->type == CPP_ELLIPSIS)
17645 /* Consume the `...' */
17646 cp_lexer_consume_token (parser->lexer);
17648 pack_expansion_p = true;
17652 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17653 unqualified_name
17654 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17655 qualifying_scope = parser->scope;
17656 if (abstract_ok)
17658 bool okay = false;
17660 if (!unqualified_name && pack_expansion_p)
17662 /* Check whether an error occurred. */
17663 okay = !cp_parser_error_occurred (parser);
17665 /* We already consumed the ellipsis to mark a
17666 parameter pack, but we have no way to report it,
17667 so abort the tentative parse. We will be exiting
17668 immediately anyway. */
17669 cp_parser_abort_tentative_parse (parser);
17671 else
17672 okay = cp_parser_parse_definitely (parser);
17674 if (!okay)
17675 unqualified_name = error_mark_node;
17676 else if (unqualified_name
17677 && (qualifying_scope
17678 || (!identifier_p (unqualified_name))))
17680 cp_parser_error (parser, "expected unqualified-id");
17681 unqualified_name = error_mark_node;
17685 if (!unqualified_name)
17686 return NULL;
17687 if (unqualified_name == error_mark_node)
17689 declarator = cp_error_declarator;
17690 pack_expansion_p = false;
17691 declarator->parameter_pack_p = false;
17692 break;
17695 attrs = cp_parser_std_attribute_spec_seq (parser);
17697 if (qualifying_scope && at_namespace_scope_p ()
17698 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17700 /* In the declaration of a member of a template class
17701 outside of the class itself, the SCOPE will sometimes
17702 be a TYPENAME_TYPE. For example, given:
17704 template <typename T>
17705 int S<T>::R::i = 3;
17707 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17708 this context, we must resolve S<T>::R to an ordinary
17709 type, rather than a typename type.
17711 The reason we normally avoid resolving TYPENAME_TYPEs
17712 is that a specialization of `S' might render
17713 `S<T>::R' not a type. However, if `S' is
17714 specialized, then this `i' will not be used, so there
17715 is no harm in resolving the types here. */
17716 tree type;
17718 /* Resolve the TYPENAME_TYPE. */
17719 type = resolve_typename_type (qualifying_scope,
17720 /*only_current_p=*/false);
17721 /* If that failed, the declarator is invalid. */
17722 if (TREE_CODE (type) == TYPENAME_TYPE)
17724 if (typedef_variant_p (type))
17725 error_at (declarator_id_start_token->location,
17726 "cannot define member of dependent typedef "
17727 "%qT", type);
17728 else
17729 error_at (declarator_id_start_token->location,
17730 "%<%T::%E%> is not a type",
17731 TYPE_CONTEXT (qualifying_scope),
17732 TYPE_IDENTIFIER (qualifying_scope));
17734 qualifying_scope = type;
17737 sfk = sfk_none;
17739 if (unqualified_name)
17741 tree class_type;
17743 if (qualifying_scope
17744 && CLASS_TYPE_P (qualifying_scope))
17745 class_type = qualifying_scope;
17746 else
17747 class_type = current_class_type;
17749 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17751 tree name_type = TREE_TYPE (unqualified_name);
17752 if (class_type && same_type_p (name_type, class_type))
17754 if (qualifying_scope
17755 && CLASSTYPE_USE_TEMPLATE (name_type))
17757 error_at (declarator_id_start_token->location,
17758 "invalid use of constructor as a template");
17759 inform (declarator_id_start_token->location,
17760 "use %<%T::%D%> instead of %<%T::%D%> to "
17761 "name the constructor in a qualified name",
17762 class_type,
17763 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17764 class_type, name_type);
17765 declarator = cp_error_declarator;
17766 break;
17768 else
17769 unqualified_name = constructor_name (class_type);
17771 else
17773 /* We do not attempt to print the declarator
17774 here because we do not have enough
17775 information about its original syntactic
17776 form. */
17777 cp_parser_error (parser, "invalid declarator");
17778 declarator = cp_error_declarator;
17779 break;
17783 if (class_type)
17785 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17786 sfk = sfk_destructor;
17787 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17788 sfk = sfk_conversion;
17789 else if (/* There's no way to declare a constructor
17790 for an anonymous type, even if the type
17791 got a name for linkage purposes. */
17792 !TYPE_WAS_ANONYMOUS (class_type)
17793 /* Handle correctly (c++/19200):
17795 struct S {
17796 struct T{};
17797 friend void S(T);
17800 and also:
17802 namespace N {
17803 void S();
17806 struct S {
17807 friend void N::S();
17808 }; */
17809 && !(friend_p
17810 && class_type != qualifying_scope)
17811 && constructor_name_p (unqualified_name,
17812 class_type))
17814 unqualified_name = constructor_name (class_type);
17815 sfk = sfk_constructor;
17817 else if (is_overloaded_fn (unqualified_name)
17818 && DECL_CONSTRUCTOR_P (get_first_fn
17819 (unqualified_name)))
17820 sfk = sfk_constructor;
17822 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17823 *ctor_dtor_or_conv_p = -1;
17826 declarator = make_id_declarator (qualifying_scope,
17827 unqualified_name,
17828 sfk);
17829 declarator->std_attributes = attrs;
17830 declarator->id_loc = token->location;
17831 declarator->parameter_pack_p = pack_expansion_p;
17833 if (pack_expansion_p)
17834 maybe_warn_variadic_templates ();
17837 handle_declarator:;
17838 scope = get_scope_of_declarator (declarator);
17839 if (scope)
17841 /* Any names that appear after the declarator-id for a
17842 member are looked up in the containing scope. */
17843 if (at_function_scope_p ())
17845 /* But declarations with qualified-ids can't appear in a
17846 function. */
17847 cp_parser_error (parser, "qualified-id in declaration");
17848 declarator = cp_error_declarator;
17849 break;
17851 pushed_scope = push_scope (scope);
17853 parser->in_declarator_p = true;
17854 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17855 || (declarator && declarator->kind == cdk_id))
17856 /* Default args are only allowed on function
17857 declarations. */
17858 parser->default_arg_ok_p = saved_default_arg_ok_p;
17859 else
17860 parser->default_arg_ok_p = false;
17862 first = false;
17864 /* We're done. */
17865 else
17866 break;
17869 /* For an abstract declarator, we might wind up with nothing at this
17870 point. That's an error; the declarator is not optional. */
17871 if (!declarator)
17872 cp_parser_error (parser, "expected declarator");
17874 /* If we entered a scope, we must exit it now. */
17875 if (pushed_scope)
17876 pop_scope (pushed_scope);
17878 parser->default_arg_ok_p = saved_default_arg_ok_p;
17879 parser->in_declarator_p = saved_in_declarator_p;
17881 return declarator;
17884 /* Parse a ptr-operator.
17886 ptr-operator:
17887 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17888 * cv-qualifier-seq [opt]
17890 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17891 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17893 GNU Extension:
17895 ptr-operator:
17896 & cv-qualifier-seq [opt]
17898 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17899 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17900 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17901 filled in with the TYPE containing the member. *CV_QUALS is
17902 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17903 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17904 Note that the tree codes returned by this function have nothing
17905 to do with the types of trees that will be eventually be created
17906 to represent the pointer or reference type being parsed. They are
17907 just constants with suggestive names. */
17908 static enum tree_code
17909 cp_parser_ptr_operator (cp_parser* parser,
17910 tree* type,
17911 cp_cv_quals *cv_quals,
17912 tree *attributes)
17914 enum tree_code code = ERROR_MARK;
17915 cp_token *token;
17916 tree attrs = NULL_TREE;
17918 /* Assume that it's not a pointer-to-member. */
17919 *type = NULL_TREE;
17920 /* And that there are no cv-qualifiers. */
17921 *cv_quals = TYPE_UNQUALIFIED;
17923 /* Peek at the next token. */
17924 token = cp_lexer_peek_token (parser->lexer);
17926 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17927 if (token->type == CPP_MULT)
17928 code = INDIRECT_REF;
17929 else if (token->type == CPP_AND)
17930 code = ADDR_EXPR;
17931 else if ((cxx_dialect != cxx98) &&
17932 token->type == CPP_AND_AND) /* C++0x only */
17933 code = NON_LVALUE_EXPR;
17935 if (code != ERROR_MARK)
17937 /* Consume the `*', `&' or `&&'. */
17938 cp_lexer_consume_token (parser->lexer);
17940 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17941 `&', if we are allowing GNU extensions. (The only qualifier
17942 that can legally appear after `&' is `restrict', but that is
17943 enforced during semantic analysis. */
17944 if (code == INDIRECT_REF
17945 || cp_parser_allow_gnu_extensions_p (parser))
17946 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17948 attrs = cp_parser_std_attribute_spec_seq (parser);
17949 if (attributes != NULL)
17950 *attributes = attrs;
17952 else
17954 /* Try the pointer-to-member case. */
17955 cp_parser_parse_tentatively (parser);
17956 /* Look for the optional `::' operator. */
17957 cp_parser_global_scope_opt (parser,
17958 /*current_scope_valid_p=*/false);
17959 /* Look for the nested-name specifier. */
17960 token = cp_lexer_peek_token (parser->lexer);
17961 cp_parser_nested_name_specifier (parser,
17962 /*typename_keyword_p=*/false,
17963 /*check_dependency_p=*/true,
17964 /*type_p=*/false,
17965 /*is_declaration=*/false);
17966 /* If we found it, and the next token is a `*', then we are
17967 indeed looking at a pointer-to-member operator. */
17968 if (!cp_parser_error_occurred (parser)
17969 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17971 /* Indicate that the `*' operator was used. */
17972 code = INDIRECT_REF;
17974 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17975 error_at (token->location, "%qD is a namespace", parser->scope);
17976 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17977 error_at (token->location, "cannot form pointer to member of "
17978 "non-class %q#T", parser->scope);
17979 else
17981 /* The type of which the member is a member is given by the
17982 current SCOPE. */
17983 *type = parser->scope;
17984 /* The next name will not be qualified. */
17985 parser->scope = NULL_TREE;
17986 parser->qualifying_scope = NULL_TREE;
17987 parser->object_scope = NULL_TREE;
17988 /* Look for optional c++11 attributes. */
17989 attrs = cp_parser_std_attribute_spec_seq (parser);
17990 if (attributes != NULL)
17991 *attributes = attrs;
17992 /* Look for the optional cv-qualifier-seq. */
17993 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17996 /* If that didn't work we don't have a ptr-operator. */
17997 if (!cp_parser_parse_definitely (parser))
17998 cp_parser_error (parser, "expected ptr-operator");
18001 return code;
18004 /* Parse an (optional) cv-qualifier-seq.
18006 cv-qualifier-seq:
18007 cv-qualifier cv-qualifier-seq [opt]
18009 cv-qualifier:
18010 const
18011 volatile
18013 GNU Extension:
18015 cv-qualifier:
18016 __restrict__
18018 Returns a bitmask representing the cv-qualifiers. */
18020 static cp_cv_quals
18021 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18023 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18025 while (true)
18027 cp_token *token;
18028 cp_cv_quals cv_qualifier;
18030 /* Peek at the next token. */
18031 token = cp_lexer_peek_token (parser->lexer);
18032 /* See if it's a cv-qualifier. */
18033 switch (token->keyword)
18035 case RID_CONST:
18036 cv_qualifier = TYPE_QUAL_CONST;
18037 break;
18039 case RID_VOLATILE:
18040 cv_qualifier = TYPE_QUAL_VOLATILE;
18041 break;
18043 case RID_RESTRICT:
18044 cv_qualifier = TYPE_QUAL_RESTRICT;
18045 break;
18047 default:
18048 cv_qualifier = TYPE_UNQUALIFIED;
18049 break;
18052 if (!cv_qualifier)
18053 break;
18055 if (cv_quals & cv_qualifier)
18057 error_at (token->location, "duplicate cv-qualifier");
18058 cp_lexer_purge_token (parser->lexer);
18060 else
18062 cp_lexer_consume_token (parser->lexer);
18063 cv_quals |= cv_qualifier;
18067 return cv_quals;
18070 /* Parse an (optional) ref-qualifier
18072 ref-qualifier:
18076 Returns cp_ref_qualifier representing ref-qualifier. */
18078 static cp_ref_qualifier
18079 cp_parser_ref_qualifier_opt (cp_parser* parser)
18081 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18083 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18084 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18085 return ref_qual;
18087 while (true)
18089 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18090 cp_token *token = cp_lexer_peek_token (parser->lexer);
18092 switch (token->type)
18094 case CPP_AND:
18095 curr_ref_qual = REF_QUAL_LVALUE;
18096 break;
18098 case CPP_AND_AND:
18099 curr_ref_qual = REF_QUAL_RVALUE;
18100 break;
18102 default:
18103 curr_ref_qual = REF_QUAL_NONE;
18104 break;
18107 if (!curr_ref_qual)
18108 break;
18109 else if (ref_qual)
18111 error_at (token->location, "multiple ref-qualifiers");
18112 cp_lexer_purge_token (parser->lexer);
18114 else
18116 ref_qual = curr_ref_qual;
18117 cp_lexer_consume_token (parser->lexer);
18121 return ref_qual;
18124 /* Parse an (optional) virt-specifier-seq.
18126 virt-specifier-seq:
18127 virt-specifier virt-specifier-seq [opt]
18129 virt-specifier:
18130 override
18131 final
18133 Returns a bitmask representing the virt-specifiers. */
18135 static cp_virt_specifiers
18136 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18138 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18140 while (true)
18142 cp_token *token;
18143 cp_virt_specifiers virt_specifier;
18145 /* Peek at the next token. */
18146 token = cp_lexer_peek_token (parser->lexer);
18147 /* See if it's a virt-specifier-qualifier. */
18148 if (token->type != CPP_NAME)
18149 break;
18150 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18152 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18153 virt_specifier = VIRT_SPEC_OVERRIDE;
18155 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18157 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18158 virt_specifier = VIRT_SPEC_FINAL;
18160 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18162 virt_specifier = VIRT_SPEC_FINAL;
18164 else
18165 break;
18167 if (virt_specifiers & virt_specifier)
18169 error_at (token->location, "duplicate virt-specifier");
18170 cp_lexer_purge_token (parser->lexer);
18172 else
18174 cp_lexer_consume_token (parser->lexer);
18175 virt_specifiers |= virt_specifier;
18178 return virt_specifiers;
18181 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18182 is in scope even though it isn't real. */
18184 void
18185 inject_this_parameter (tree ctype, cp_cv_quals quals)
18187 tree this_parm;
18189 if (current_class_ptr)
18191 /* We don't clear this between NSDMIs. Is it already what we want? */
18192 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18193 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18194 && cp_type_quals (type) == quals)
18195 return;
18198 this_parm = build_this_parm (ctype, quals);
18199 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18200 current_class_ptr = NULL_TREE;
18201 current_class_ref
18202 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18203 current_class_ptr = this_parm;
18206 /* Return true iff our current scope is a non-static data member
18207 initializer. */
18209 bool
18210 parsing_nsdmi (void)
18212 /* We recognize NSDMI context by the context-less 'this' pointer set up
18213 by the function above. */
18214 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18215 return true;
18216 return false;
18219 /* Parse a late-specified return type, if any. This is not a separate
18220 non-terminal, but part of a function declarator, which looks like
18222 -> trailing-type-specifier-seq abstract-declarator(opt)
18224 Returns the type indicated by the type-id.
18226 In addition to this this parses any queued up omp declare simd
18227 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18229 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18230 function. */
18232 static tree
18233 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18234 cp_cv_quals quals)
18236 cp_token *token;
18237 tree type = NULL_TREE;
18238 bool declare_simd_p = (parser->omp_declare_simd
18239 && declarator
18240 && declarator->kind == cdk_id);
18242 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18243 && declarator && declarator->kind == cdk_id);
18245 /* Peek at the next token. */
18246 token = cp_lexer_peek_token (parser->lexer);
18247 /* A late-specified return type is indicated by an initial '->'. */
18248 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18249 return NULL_TREE;
18251 tree save_ccp = current_class_ptr;
18252 tree save_ccr = current_class_ref;
18253 if (quals >= 0)
18255 /* DR 1207: 'this' is in scope in the trailing return type. */
18256 inject_this_parameter (current_class_type, quals);
18259 if (token->type == CPP_DEREF)
18261 /* Consume the ->. */
18262 cp_lexer_consume_token (parser->lexer);
18264 type = cp_parser_trailing_type_id (parser);
18267 if (cilk_simd_fn_vector_p)
18268 declarator->std_attributes
18269 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18270 declarator->std_attributes);
18271 if (declare_simd_p)
18272 declarator->std_attributes
18273 = cp_parser_late_parsing_omp_declare_simd (parser,
18274 declarator->std_attributes);
18276 if (quals >= 0)
18278 current_class_ptr = save_ccp;
18279 current_class_ref = save_ccr;
18282 return type;
18285 /* Parse a declarator-id.
18287 declarator-id:
18288 id-expression
18289 :: [opt] nested-name-specifier [opt] type-name
18291 In the `id-expression' case, the value returned is as for
18292 cp_parser_id_expression if the id-expression was an unqualified-id.
18293 If the id-expression was a qualified-id, then a SCOPE_REF is
18294 returned. The first operand is the scope (either a NAMESPACE_DECL
18295 or TREE_TYPE), but the second is still just a representation of an
18296 unqualified-id. */
18298 static tree
18299 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18301 tree id;
18302 /* The expression must be an id-expression. Assume that qualified
18303 names are the names of types so that:
18305 template <class T>
18306 int S<T>::R::i = 3;
18308 will work; we must treat `S<T>::R' as the name of a type.
18309 Similarly, assume that qualified names are templates, where
18310 required, so that:
18312 template <class T>
18313 int S<T>::R<T>::i = 3;
18315 will work, too. */
18316 id = cp_parser_id_expression (parser,
18317 /*template_keyword_p=*/false,
18318 /*check_dependency_p=*/false,
18319 /*template_p=*/NULL,
18320 /*declarator_p=*/true,
18321 optional_p);
18322 if (id && BASELINK_P (id))
18323 id = BASELINK_FUNCTIONS (id);
18324 return id;
18327 /* Parse a type-id.
18329 type-id:
18330 type-specifier-seq abstract-declarator [opt]
18332 Returns the TYPE specified. */
18334 static tree
18335 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18336 bool is_trailing_return)
18338 cp_decl_specifier_seq type_specifier_seq;
18339 cp_declarator *abstract_declarator;
18341 /* Parse the type-specifier-seq. */
18342 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18343 is_trailing_return,
18344 &type_specifier_seq);
18345 if (type_specifier_seq.type == error_mark_node)
18346 return error_mark_node;
18348 /* There might or might not be an abstract declarator. */
18349 cp_parser_parse_tentatively (parser);
18350 /* Look for the declarator. */
18351 abstract_declarator
18352 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18353 /*parenthesized_p=*/NULL,
18354 /*member_p=*/false,
18355 /*friend_p=*/false);
18356 /* Check to see if there really was a declarator. */
18357 if (!cp_parser_parse_definitely (parser))
18358 abstract_declarator = NULL;
18360 if (type_specifier_seq.type
18361 /* None of the valid uses of 'auto' in C++14 involve the type-id
18362 nonterminal, but it is valid in a trailing-return-type. */
18363 && !(cxx_dialect >= cxx14 && is_trailing_return)
18364 && type_uses_auto (type_specifier_seq.type))
18366 /* A type-id with type 'auto' is only ok if the abstract declarator
18367 is a function declarator with a late-specified return type. */
18368 if (abstract_declarator
18369 && abstract_declarator->kind == cdk_function
18370 && abstract_declarator->u.function.late_return_type)
18371 /* OK */;
18372 else
18374 error ("invalid use of %<auto%>");
18375 return error_mark_node;
18379 return groktypename (&type_specifier_seq, abstract_declarator,
18380 is_template_arg);
18383 static tree cp_parser_type_id (cp_parser *parser)
18385 return cp_parser_type_id_1 (parser, false, false);
18388 static tree cp_parser_template_type_arg (cp_parser *parser)
18390 tree r;
18391 const char *saved_message = parser->type_definition_forbidden_message;
18392 parser->type_definition_forbidden_message
18393 = G_("types may not be defined in template arguments");
18394 r = cp_parser_type_id_1 (parser, true, false);
18395 parser->type_definition_forbidden_message = saved_message;
18396 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18398 error ("invalid use of %<auto%> in template argument");
18399 r = error_mark_node;
18401 return r;
18404 static tree cp_parser_trailing_type_id (cp_parser *parser)
18406 return cp_parser_type_id_1 (parser, false, true);
18409 /* Parse a type-specifier-seq.
18411 type-specifier-seq:
18412 type-specifier type-specifier-seq [opt]
18414 GNU extension:
18416 type-specifier-seq:
18417 attributes type-specifier-seq [opt]
18419 If IS_DECLARATION is true, we are at the start of a "condition" or
18420 exception-declaration, so we might be followed by a declarator-id.
18422 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18423 i.e. we've just seen "->".
18425 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18427 static void
18428 cp_parser_type_specifier_seq (cp_parser* parser,
18429 bool is_declaration,
18430 bool is_trailing_return,
18431 cp_decl_specifier_seq *type_specifier_seq)
18433 bool seen_type_specifier = false;
18434 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18435 cp_token *start_token = NULL;
18437 /* Clear the TYPE_SPECIFIER_SEQ. */
18438 clear_decl_specs (type_specifier_seq);
18440 /* In the context of a trailing return type, enum E { } is an
18441 elaborated-type-specifier followed by a function-body, not an
18442 enum-specifier. */
18443 if (is_trailing_return)
18444 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18446 /* Parse the type-specifiers and attributes. */
18447 while (true)
18449 tree type_specifier;
18450 bool is_cv_qualifier;
18452 /* Check for attributes first. */
18453 if (cp_next_tokens_can_be_attribute_p (parser))
18455 type_specifier_seq->attributes =
18456 chainon (type_specifier_seq->attributes,
18457 cp_parser_attributes_opt (parser));
18458 continue;
18461 /* record the token of the beginning of the type specifier seq,
18462 for error reporting purposes*/
18463 if (!start_token)
18464 start_token = cp_lexer_peek_token (parser->lexer);
18466 /* Look for the type-specifier. */
18467 type_specifier = cp_parser_type_specifier (parser,
18468 flags,
18469 type_specifier_seq,
18470 /*is_declaration=*/false,
18471 NULL,
18472 &is_cv_qualifier);
18473 if (!type_specifier)
18475 /* If the first type-specifier could not be found, this is not a
18476 type-specifier-seq at all. */
18477 if (!seen_type_specifier)
18479 /* Set in_declarator_p to avoid skipping to the semicolon. */
18480 int in_decl = parser->in_declarator_p;
18481 parser->in_declarator_p = true;
18483 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18484 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18485 cp_parser_error (parser, "expected type-specifier");
18487 parser->in_declarator_p = in_decl;
18489 type_specifier_seq->type = error_mark_node;
18490 return;
18492 /* If subsequent type-specifiers could not be found, the
18493 type-specifier-seq is complete. */
18494 break;
18497 seen_type_specifier = true;
18498 /* The standard says that a condition can be:
18500 type-specifier-seq declarator = assignment-expression
18502 However, given:
18504 struct S {};
18505 if (int S = ...)
18507 we should treat the "S" as a declarator, not as a
18508 type-specifier. The standard doesn't say that explicitly for
18509 type-specifier-seq, but it does say that for
18510 decl-specifier-seq in an ordinary declaration. Perhaps it
18511 would be clearer just to allow a decl-specifier-seq here, and
18512 then add a semantic restriction that if any decl-specifiers
18513 that are not type-specifiers appear, the program is invalid. */
18514 if (is_declaration && !is_cv_qualifier)
18515 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18519 /* Return whether the function currently being declared has an associated
18520 template parameter list. */
18522 static bool
18523 function_being_declared_is_template_p (cp_parser* parser)
18525 if (!current_template_parms || processing_template_parmlist)
18526 return false;
18528 if (parser->implicit_template_scope)
18529 return true;
18531 if (at_class_scope_p ()
18532 && TYPE_BEING_DEFINED (current_class_type))
18533 return parser->num_template_parameter_lists != 0;
18535 return ((int) parser->num_template_parameter_lists > template_class_depth
18536 (current_class_type));
18539 /* Parse a parameter-declaration-clause.
18541 parameter-declaration-clause:
18542 parameter-declaration-list [opt] ... [opt]
18543 parameter-declaration-list , ...
18545 Returns a representation for the parameter declarations. A return
18546 value of NULL indicates a parameter-declaration-clause consisting
18547 only of an ellipsis. */
18549 static tree
18550 cp_parser_parameter_declaration_clause (cp_parser* parser)
18552 tree parameters;
18553 cp_token *token;
18554 bool ellipsis_p;
18555 bool is_error;
18557 struct cleanup {
18558 cp_parser* parser;
18559 int auto_is_implicit_function_template_parm_p;
18560 ~cleanup() {
18561 parser->auto_is_implicit_function_template_parm_p
18562 = auto_is_implicit_function_template_parm_p;
18564 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18566 (void) cleanup;
18568 if (!processing_specialization
18569 && !processing_template_parmlist
18570 && !processing_explicit_instantiation)
18571 if (!current_function_decl
18572 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18573 parser->auto_is_implicit_function_template_parm_p = true;
18575 /* Peek at the next token. */
18576 token = cp_lexer_peek_token (parser->lexer);
18577 /* Check for trivial parameter-declaration-clauses. */
18578 if (token->type == CPP_ELLIPSIS)
18580 /* Consume the `...' token. */
18581 cp_lexer_consume_token (parser->lexer);
18582 return NULL_TREE;
18584 else if (token->type == CPP_CLOSE_PAREN)
18585 /* There are no parameters. */
18587 #ifndef NO_IMPLICIT_EXTERN_C
18588 if (in_system_header_at (input_location)
18589 && current_class_type == NULL
18590 && current_lang_name == lang_name_c)
18591 return NULL_TREE;
18592 else
18593 #endif
18594 return void_list_node;
18596 /* Check for `(void)', too, which is a special case. */
18597 else if (token->keyword == RID_VOID
18598 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18599 == CPP_CLOSE_PAREN))
18601 /* Consume the `void' token. */
18602 cp_lexer_consume_token (parser->lexer);
18603 /* There are no parameters. */
18604 return void_list_node;
18607 /* Parse the parameter-declaration-list. */
18608 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18609 /* If a parse error occurred while parsing the
18610 parameter-declaration-list, then the entire
18611 parameter-declaration-clause is erroneous. */
18612 if (is_error)
18613 return NULL;
18615 /* Peek at the next token. */
18616 token = cp_lexer_peek_token (parser->lexer);
18617 /* If it's a `,', the clause should terminate with an ellipsis. */
18618 if (token->type == CPP_COMMA)
18620 /* Consume the `,'. */
18621 cp_lexer_consume_token (parser->lexer);
18622 /* Expect an ellipsis. */
18623 ellipsis_p
18624 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18626 /* It might also be `...' if the optional trailing `,' was
18627 omitted. */
18628 else if (token->type == CPP_ELLIPSIS)
18630 /* Consume the `...' token. */
18631 cp_lexer_consume_token (parser->lexer);
18632 /* And remember that we saw it. */
18633 ellipsis_p = true;
18635 else
18636 ellipsis_p = false;
18638 /* Finish the parameter list. */
18639 if (!ellipsis_p)
18640 parameters = chainon (parameters, void_list_node);
18642 return parameters;
18645 /* Parse a parameter-declaration-list.
18647 parameter-declaration-list:
18648 parameter-declaration
18649 parameter-declaration-list , parameter-declaration
18651 Returns a representation of the parameter-declaration-list, as for
18652 cp_parser_parameter_declaration_clause. However, the
18653 `void_list_node' is never appended to the list. Upon return,
18654 *IS_ERROR will be true iff an error occurred. */
18656 static tree
18657 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18659 tree parameters = NULL_TREE;
18660 tree *tail = &parameters;
18661 bool saved_in_unbraced_linkage_specification_p;
18662 int index = 0;
18664 /* Assume all will go well. */
18665 *is_error = false;
18666 /* The special considerations that apply to a function within an
18667 unbraced linkage specifications do not apply to the parameters
18668 to the function. */
18669 saved_in_unbraced_linkage_specification_p
18670 = parser->in_unbraced_linkage_specification_p;
18671 parser->in_unbraced_linkage_specification_p = false;
18673 /* Look for more parameters. */
18674 while (true)
18676 cp_parameter_declarator *parameter;
18677 tree decl = error_mark_node;
18678 bool parenthesized_p = false;
18679 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18680 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18681 (current_template_parms)) : 0);
18683 /* Parse the parameter. */
18684 parameter
18685 = cp_parser_parameter_declaration (parser,
18686 /*template_parm_p=*/false,
18687 &parenthesized_p);
18689 /* We don't know yet if the enclosing context is deprecated, so wait
18690 and warn in grokparms if appropriate. */
18691 deprecated_state = DEPRECATED_SUPPRESS;
18693 if (parameter)
18695 /* If a function parameter pack was specified and an implicit template
18696 parameter was introduced during cp_parser_parameter_declaration,
18697 change any implicit parameters introduced into packs. */
18698 if (parser->implicit_template_parms
18699 && parameter->declarator
18700 && parameter->declarator->parameter_pack_p)
18702 int latest_template_parm_idx = TREE_VEC_LENGTH
18703 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18705 if (latest_template_parm_idx != template_parm_idx)
18706 parameter->decl_specifiers.type = convert_generic_types_to_packs
18707 (parameter->decl_specifiers.type,
18708 template_parm_idx, latest_template_parm_idx);
18711 decl = grokdeclarator (parameter->declarator,
18712 &parameter->decl_specifiers,
18713 PARM,
18714 parameter->default_argument != NULL_TREE,
18715 &parameter->decl_specifiers.attributes);
18718 deprecated_state = DEPRECATED_NORMAL;
18720 /* If a parse error occurred parsing the parameter declaration,
18721 then the entire parameter-declaration-list is erroneous. */
18722 if (decl == error_mark_node)
18724 *is_error = true;
18725 parameters = error_mark_node;
18726 break;
18729 if (parameter->decl_specifiers.attributes)
18730 cplus_decl_attributes (&decl,
18731 parameter->decl_specifiers.attributes,
18733 if (DECL_NAME (decl))
18734 decl = pushdecl (decl);
18736 if (decl != error_mark_node)
18738 retrofit_lang_decl (decl);
18739 DECL_PARM_INDEX (decl) = ++index;
18740 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18743 /* Add the new parameter to the list. */
18744 *tail = build_tree_list (parameter->default_argument, decl);
18745 tail = &TREE_CHAIN (*tail);
18747 /* Peek at the next token. */
18748 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18749 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18750 /* These are for Objective-C++ */
18751 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18752 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18753 /* The parameter-declaration-list is complete. */
18754 break;
18755 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18757 cp_token *token;
18759 /* Peek at the next token. */
18760 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18761 /* If it's an ellipsis, then the list is complete. */
18762 if (token->type == CPP_ELLIPSIS)
18763 break;
18764 /* Otherwise, there must be more parameters. Consume the
18765 `,'. */
18766 cp_lexer_consume_token (parser->lexer);
18767 /* When parsing something like:
18769 int i(float f, double d)
18771 we can tell after seeing the declaration for "f" that we
18772 are not looking at an initialization of a variable "i",
18773 but rather at the declaration of a function "i".
18775 Due to the fact that the parsing of template arguments
18776 (as specified to a template-id) requires backtracking we
18777 cannot use this technique when inside a template argument
18778 list. */
18779 if (!parser->in_template_argument_list_p
18780 && !parser->in_type_id_in_expr_p
18781 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18782 /* However, a parameter-declaration of the form
18783 "float(f)" (which is a valid declaration of a
18784 parameter "f") can also be interpreted as an
18785 expression (the conversion of "f" to "float"). */
18786 && !parenthesized_p)
18787 cp_parser_commit_to_tentative_parse (parser);
18789 else
18791 cp_parser_error (parser, "expected %<,%> or %<...%>");
18792 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18793 cp_parser_skip_to_closing_parenthesis (parser,
18794 /*recovering=*/true,
18795 /*or_comma=*/false,
18796 /*consume_paren=*/false);
18797 break;
18801 parser->in_unbraced_linkage_specification_p
18802 = saved_in_unbraced_linkage_specification_p;
18804 /* Reset implicit_template_scope if we are about to leave the function
18805 parameter list that introduced it. Note that for out-of-line member
18806 definitions, there will be one or more class scopes before we get to
18807 the template parameter scope. */
18809 if (cp_binding_level *its = parser->implicit_template_scope)
18810 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18812 while (maybe_its->kind == sk_class)
18813 maybe_its = maybe_its->level_chain;
18814 if (maybe_its == its)
18816 parser->implicit_template_parms = 0;
18817 parser->implicit_template_scope = 0;
18821 return parameters;
18824 /* Parse a parameter declaration.
18826 parameter-declaration:
18827 decl-specifier-seq ... [opt] declarator
18828 decl-specifier-seq declarator = assignment-expression
18829 decl-specifier-seq ... [opt] abstract-declarator [opt]
18830 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18832 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18833 declares a template parameter. (In that case, a non-nested `>'
18834 token encountered during the parsing of the assignment-expression
18835 is not interpreted as a greater-than operator.)
18837 Returns a representation of the parameter, or NULL if an error
18838 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18839 true iff the declarator is of the form "(p)". */
18841 static cp_parameter_declarator *
18842 cp_parser_parameter_declaration (cp_parser *parser,
18843 bool template_parm_p,
18844 bool *parenthesized_p)
18846 int declares_class_or_enum;
18847 cp_decl_specifier_seq decl_specifiers;
18848 cp_declarator *declarator;
18849 tree default_argument;
18850 cp_token *token = NULL, *declarator_token_start = NULL;
18851 const char *saved_message;
18853 /* In a template parameter, `>' is not an operator.
18855 [temp.param]
18857 When parsing a default template-argument for a non-type
18858 template-parameter, the first non-nested `>' is taken as the end
18859 of the template parameter-list rather than a greater-than
18860 operator. */
18862 /* Type definitions may not appear in parameter types. */
18863 saved_message = parser->type_definition_forbidden_message;
18864 parser->type_definition_forbidden_message
18865 = G_("types may not be defined in parameter types");
18867 /* Parse the declaration-specifiers. */
18868 cp_parser_decl_specifier_seq (parser,
18869 CP_PARSER_FLAGS_NONE,
18870 &decl_specifiers,
18871 &declares_class_or_enum);
18873 /* Complain about missing 'typename' or other invalid type names. */
18874 if (!decl_specifiers.any_type_specifiers_p
18875 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18876 decl_specifiers.type = error_mark_node;
18878 /* If an error occurred, there's no reason to attempt to parse the
18879 rest of the declaration. */
18880 if (cp_parser_error_occurred (parser))
18882 parser->type_definition_forbidden_message = saved_message;
18883 return NULL;
18886 /* Peek at the next token. */
18887 token = cp_lexer_peek_token (parser->lexer);
18889 /* If the next token is a `)', `,', `=', `>', or `...', then there
18890 is no declarator. However, when variadic templates are enabled,
18891 there may be a declarator following `...'. */
18892 if (token->type == CPP_CLOSE_PAREN
18893 || token->type == CPP_COMMA
18894 || token->type == CPP_EQ
18895 || token->type == CPP_GREATER)
18897 declarator = NULL;
18898 if (parenthesized_p)
18899 *parenthesized_p = false;
18901 /* Otherwise, there should be a declarator. */
18902 else
18904 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18905 parser->default_arg_ok_p = false;
18907 /* After seeing a decl-specifier-seq, if the next token is not a
18908 "(", there is no possibility that the code is a valid
18909 expression. Therefore, if parsing tentatively, we commit at
18910 this point. */
18911 if (!parser->in_template_argument_list_p
18912 /* In an expression context, having seen:
18914 (int((char ...
18916 we cannot be sure whether we are looking at a
18917 function-type (taking a "char" as a parameter) or a cast
18918 of some object of type "char" to "int". */
18919 && !parser->in_type_id_in_expr_p
18920 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18921 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18922 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18923 cp_parser_commit_to_tentative_parse (parser);
18924 /* Parse the declarator. */
18925 declarator_token_start = token;
18926 declarator = cp_parser_declarator (parser,
18927 CP_PARSER_DECLARATOR_EITHER,
18928 /*ctor_dtor_or_conv_p=*/NULL,
18929 parenthesized_p,
18930 /*member_p=*/false,
18931 /*friend_p=*/false);
18932 parser->default_arg_ok_p = saved_default_arg_ok_p;
18933 /* After the declarator, allow more attributes. */
18934 decl_specifiers.attributes
18935 = chainon (decl_specifiers.attributes,
18936 cp_parser_attributes_opt (parser));
18939 /* If the next token is an ellipsis, and we have not seen a
18940 declarator name, and the type of the declarator contains parameter
18941 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18942 a parameter pack expansion expression. Otherwise, leave the
18943 ellipsis for a C-style variadic function. */
18944 token = cp_lexer_peek_token (parser->lexer);
18945 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18947 tree type = decl_specifiers.type;
18949 if (type && DECL_P (type))
18950 type = TREE_TYPE (type);
18952 if (type
18953 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18954 && declarator_can_be_parameter_pack (declarator)
18955 && (!declarator || !declarator->parameter_pack_p)
18956 && uses_parameter_packs (type))
18958 /* Consume the `...'. */
18959 cp_lexer_consume_token (parser->lexer);
18960 maybe_warn_variadic_templates ();
18962 /* Build a pack expansion type */
18963 if (declarator)
18964 declarator->parameter_pack_p = true;
18965 else
18966 decl_specifiers.type = make_pack_expansion (type);
18970 /* The restriction on defining new types applies only to the type
18971 of the parameter, not to the default argument. */
18972 parser->type_definition_forbidden_message = saved_message;
18974 /* If the next token is `=', then process a default argument. */
18975 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18977 token = cp_lexer_peek_token (parser->lexer);
18978 /* If we are defining a class, then the tokens that make up the
18979 default argument must be saved and processed later. */
18980 if (!template_parm_p && at_class_scope_p ()
18981 && TYPE_BEING_DEFINED (current_class_type)
18982 && !LAMBDA_TYPE_P (current_class_type))
18983 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18984 /* Outside of a class definition, we can just parse the
18985 assignment-expression. */
18986 else
18987 default_argument
18988 = cp_parser_default_argument (parser, template_parm_p);
18990 if (!parser->default_arg_ok_p)
18992 if (flag_permissive)
18993 warning (0, "deprecated use of default argument for parameter of non-function");
18994 else
18996 error_at (token->location,
18997 "default arguments are only "
18998 "permitted for function parameters");
18999 default_argument = NULL_TREE;
19002 else if ((declarator && declarator->parameter_pack_p)
19003 || (decl_specifiers.type
19004 && PACK_EXPANSION_P (decl_specifiers.type)))
19006 /* Find the name of the parameter pack. */
19007 cp_declarator *id_declarator = declarator;
19008 while (id_declarator && id_declarator->kind != cdk_id)
19009 id_declarator = id_declarator->declarator;
19011 if (id_declarator && id_declarator->kind == cdk_id)
19012 error_at (declarator_token_start->location,
19013 template_parm_p
19014 ? G_("template parameter pack %qD "
19015 "cannot have a default argument")
19016 : G_("parameter pack %qD cannot have "
19017 "a default argument"),
19018 id_declarator->u.id.unqualified_name);
19019 else
19020 error_at (declarator_token_start->location,
19021 template_parm_p
19022 ? G_("template parameter pack cannot have "
19023 "a default argument")
19024 : G_("parameter pack cannot have a "
19025 "default argument"));
19027 default_argument = NULL_TREE;
19030 else
19031 default_argument = NULL_TREE;
19033 return make_parameter_declarator (&decl_specifiers,
19034 declarator,
19035 default_argument);
19038 /* Parse a default argument and return it.
19040 TEMPLATE_PARM_P is true if this is a default argument for a
19041 non-type template parameter. */
19042 static tree
19043 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19045 tree default_argument = NULL_TREE;
19046 bool saved_greater_than_is_operator_p;
19047 bool saved_local_variables_forbidden_p;
19048 bool non_constant_p, is_direct_init;
19050 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19051 set correctly. */
19052 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19053 parser->greater_than_is_operator_p = !template_parm_p;
19054 /* Local variable names (and the `this' keyword) may not
19055 appear in a default argument. */
19056 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19057 parser->local_variables_forbidden_p = true;
19058 /* Parse the assignment-expression. */
19059 if (template_parm_p)
19060 push_deferring_access_checks (dk_no_deferred);
19061 tree saved_class_ptr = NULL_TREE;
19062 tree saved_class_ref = NULL_TREE;
19063 /* The "this" pointer is not valid in a default argument. */
19064 if (cfun)
19066 saved_class_ptr = current_class_ptr;
19067 cp_function_chain->x_current_class_ptr = NULL_TREE;
19068 saved_class_ref = current_class_ref;
19069 cp_function_chain->x_current_class_ref = NULL_TREE;
19071 default_argument
19072 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19073 /* Restore the "this" pointer. */
19074 if (cfun)
19076 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19077 cp_function_chain->x_current_class_ref = saved_class_ref;
19079 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19080 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19081 if (template_parm_p)
19082 pop_deferring_access_checks ();
19083 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19084 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19086 return default_argument;
19089 /* Parse a function-body.
19091 function-body:
19092 compound_statement */
19094 static void
19095 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19097 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19100 /* Parse a ctor-initializer-opt followed by a function-body. Return
19101 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19102 is true we are parsing a function-try-block. */
19104 static bool
19105 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19106 bool in_function_try_block)
19108 tree body, list;
19109 bool ctor_initializer_p;
19110 const bool check_body_p =
19111 DECL_CONSTRUCTOR_P (current_function_decl)
19112 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19113 tree last = NULL;
19115 /* Begin the function body. */
19116 body = begin_function_body ();
19117 /* Parse the optional ctor-initializer. */
19118 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19120 /* If we're parsing a constexpr constructor definition, we need
19121 to check that the constructor body is indeed empty. However,
19122 before we get to cp_parser_function_body lot of junk has been
19123 generated, so we can't just check that we have an empty block.
19124 Rather we take a snapshot of the outermost block, and check whether
19125 cp_parser_function_body changed its state. */
19126 if (check_body_p)
19128 list = cur_stmt_list;
19129 if (STATEMENT_LIST_TAIL (list))
19130 last = STATEMENT_LIST_TAIL (list)->stmt;
19132 /* Parse the function-body. */
19133 cp_parser_function_body (parser, in_function_try_block);
19134 if (check_body_p)
19135 check_constexpr_ctor_body (last, list, /*complain=*/true);
19136 /* Finish the function body. */
19137 finish_function_body (body);
19139 return ctor_initializer_p;
19142 /* Parse an initializer.
19144 initializer:
19145 = initializer-clause
19146 ( expression-list )
19148 Returns an expression representing the initializer. If no
19149 initializer is present, NULL_TREE is returned.
19151 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19152 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19153 set to TRUE if there is no initializer present. If there is an
19154 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19155 is set to true; otherwise it is set to false. */
19157 static tree
19158 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19159 bool* non_constant_p)
19161 cp_token *token;
19162 tree init;
19164 /* Peek at the next token. */
19165 token = cp_lexer_peek_token (parser->lexer);
19167 /* Let our caller know whether or not this initializer was
19168 parenthesized. */
19169 *is_direct_init = (token->type != CPP_EQ);
19170 /* Assume that the initializer is constant. */
19171 *non_constant_p = false;
19173 if (token->type == CPP_EQ)
19175 /* Consume the `='. */
19176 cp_lexer_consume_token (parser->lexer);
19177 /* Parse the initializer-clause. */
19178 init = cp_parser_initializer_clause (parser, non_constant_p);
19180 else if (token->type == CPP_OPEN_PAREN)
19182 vec<tree, va_gc> *vec;
19183 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19184 /*cast_p=*/false,
19185 /*allow_expansion_p=*/true,
19186 non_constant_p);
19187 if (vec == NULL)
19188 return error_mark_node;
19189 init = build_tree_list_vec (vec);
19190 release_tree_vector (vec);
19192 else if (token->type == CPP_OPEN_BRACE)
19194 cp_lexer_set_source_position (parser->lexer);
19195 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19196 init = cp_parser_braced_list (parser, non_constant_p);
19197 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19199 else
19201 /* Anything else is an error. */
19202 cp_parser_error (parser, "expected initializer");
19203 init = error_mark_node;
19206 return init;
19209 /* Parse an initializer-clause.
19211 initializer-clause:
19212 assignment-expression
19213 braced-init-list
19215 Returns an expression representing the initializer.
19217 If the `assignment-expression' production is used the value
19218 returned is simply a representation for the expression.
19220 Otherwise, calls cp_parser_braced_list. */
19222 static tree
19223 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19225 tree initializer;
19227 /* Assume the expression is constant. */
19228 *non_constant_p = false;
19230 /* If it is not a `{', then we are looking at an
19231 assignment-expression. */
19232 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19234 initializer
19235 = cp_parser_constant_expression (parser,
19236 /*allow_non_constant_p=*/true,
19237 non_constant_p);
19239 else
19240 initializer = cp_parser_braced_list (parser, non_constant_p);
19242 return initializer;
19245 /* Parse a brace-enclosed initializer list.
19247 braced-init-list:
19248 { initializer-list , [opt] }
19251 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19252 the elements of the initializer-list (or NULL, if the last
19253 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19254 NULL_TREE. There is no way to detect whether or not the optional
19255 trailing `,' was provided. NON_CONSTANT_P is as for
19256 cp_parser_initializer. */
19258 static tree
19259 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19261 tree initializer;
19263 /* Consume the `{' token. */
19264 cp_lexer_consume_token (parser->lexer);
19265 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19266 initializer = make_node (CONSTRUCTOR);
19267 /* If it's not a `}', then there is a non-trivial initializer. */
19268 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19270 /* Parse the initializer list. */
19271 CONSTRUCTOR_ELTS (initializer)
19272 = cp_parser_initializer_list (parser, non_constant_p);
19273 /* A trailing `,' token is allowed. */
19274 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19275 cp_lexer_consume_token (parser->lexer);
19277 else
19278 *non_constant_p = false;
19279 /* Now, there should be a trailing `}'. */
19280 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19281 TREE_TYPE (initializer) = init_list_type_node;
19282 return initializer;
19285 /* Consume tokens up to, and including, the next non-nested closing `]'.
19286 Returns true iff we found a closing `]'. */
19288 static bool
19289 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19291 unsigned square_depth = 0;
19293 while (true)
19295 cp_token * token = cp_lexer_peek_token (parser->lexer);
19297 switch (token->type)
19299 case CPP_EOF:
19300 case CPP_PRAGMA_EOL:
19301 /* If we've run out of tokens, then there is no closing `]'. */
19302 return false;
19304 case CPP_OPEN_SQUARE:
19305 ++square_depth;
19306 break;
19308 case CPP_CLOSE_SQUARE:
19309 if (!square_depth--)
19311 cp_lexer_consume_token (parser->lexer);
19312 return true;
19314 break;
19316 default:
19317 break;
19320 /* Consume the token. */
19321 cp_lexer_consume_token (parser->lexer);
19325 /* Return true if we are looking at an array-designator, false otherwise. */
19327 static bool
19328 cp_parser_array_designator_p (cp_parser *parser)
19330 /* Consume the `['. */
19331 cp_lexer_consume_token (parser->lexer);
19333 cp_lexer_save_tokens (parser->lexer);
19335 /* Skip tokens until the next token is a closing square bracket.
19336 If we find the closing `]', and the next token is a `=', then
19337 we are looking at an array designator. */
19338 bool array_designator_p
19339 = (cp_parser_skip_to_closing_square_bracket (parser)
19340 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19342 /* Roll back the tokens we skipped. */
19343 cp_lexer_rollback_tokens (parser->lexer);
19345 return array_designator_p;
19348 /* Parse an initializer-list.
19350 initializer-list:
19351 initializer-clause ... [opt]
19352 initializer-list , initializer-clause ... [opt]
19354 GNU Extension:
19356 initializer-list:
19357 designation initializer-clause ...[opt]
19358 initializer-list , designation initializer-clause ...[opt]
19360 designation:
19361 . identifier =
19362 identifier :
19363 [ constant-expression ] =
19365 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19366 for the initializer. If the INDEX of the elt is non-NULL, it is the
19367 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19368 as for cp_parser_initializer. */
19370 static vec<constructor_elt, va_gc> *
19371 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19373 vec<constructor_elt, va_gc> *v = NULL;
19375 /* Assume all of the expressions are constant. */
19376 *non_constant_p = false;
19378 /* Parse the rest of the list. */
19379 while (true)
19381 cp_token *token;
19382 tree designator;
19383 tree initializer;
19384 bool clause_non_constant_p;
19386 /* If the next token is an identifier and the following one is a
19387 colon, we are looking at the GNU designated-initializer
19388 syntax. */
19389 if (cp_parser_allow_gnu_extensions_p (parser)
19390 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19391 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19393 /* Warn the user that they are using an extension. */
19394 pedwarn (input_location, OPT_Wpedantic,
19395 "ISO C++ does not allow designated initializers");
19396 /* Consume the identifier. */
19397 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19398 /* Consume the `:'. */
19399 cp_lexer_consume_token (parser->lexer);
19401 /* Also handle the C99 syntax, '. id ='. */
19402 else if (cp_parser_allow_gnu_extensions_p (parser)
19403 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19404 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19405 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19407 /* Warn the user that they are using an extension. */
19408 pedwarn (input_location, OPT_Wpedantic,
19409 "ISO C++ does not allow C99 designated initializers");
19410 /* Consume the `.'. */
19411 cp_lexer_consume_token (parser->lexer);
19412 /* Consume the identifier. */
19413 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19414 /* Consume the `='. */
19415 cp_lexer_consume_token (parser->lexer);
19417 /* Also handle C99 array designators, '[ const ] ='. */
19418 else if (cp_parser_allow_gnu_extensions_p (parser)
19419 && !c_dialect_objc ()
19420 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19422 /* In C++11, [ could start a lambda-introducer. */
19423 bool non_const = false;
19425 cp_parser_parse_tentatively (parser);
19427 if (!cp_parser_array_designator_p (parser))
19429 cp_parser_simulate_error (parser);
19430 designator = NULL_TREE;
19432 else
19434 designator = cp_parser_constant_expression (parser, true,
19435 &non_const);
19436 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19437 cp_parser_require (parser, CPP_EQ, RT_EQ);
19440 if (!cp_parser_parse_definitely (parser))
19441 designator = NULL_TREE;
19442 else if (non_const)
19443 require_potential_rvalue_constant_expression (designator);
19445 else
19446 designator = NULL_TREE;
19448 /* Parse the initializer. */
19449 initializer = cp_parser_initializer_clause (parser,
19450 &clause_non_constant_p);
19451 /* If any clause is non-constant, so is the entire initializer. */
19452 if (clause_non_constant_p)
19453 *non_constant_p = true;
19455 /* If we have an ellipsis, this is an initializer pack
19456 expansion. */
19457 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19459 /* Consume the `...'. */
19460 cp_lexer_consume_token (parser->lexer);
19462 /* Turn the initializer into an initializer expansion. */
19463 initializer = make_pack_expansion (initializer);
19466 /* Add it to the vector. */
19467 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19469 /* If the next token is not a comma, we have reached the end of
19470 the list. */
19471 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19472 break;
19474 /* Peek at the next token. */
19475 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19476 /* If the next token is a `}', then we're still done. An
19477 initializer-clause can have a trailing `,' after the
19478 initializer-list and before the closing `}'. */
19479 if (token->type == CPP_CLOSE_BRACE)
19480 break;
19482 /* Consume the `,' token. */
19483 cp_lexer_consume_token (parser->lexer);
19486 return v;
19489 /* Classes [gram.class] */
19491 /* Parse a class-name.
19493 class-name:
19494 identifier
19495 template-id
19497 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19498 to indicate that names looked up in dependent types should be
19499 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19500 keyword has been used to indicate that the name that appears next
19501 is a template. TAG_TYPE indicates the explicit tag given before
19502 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19503 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19504 is the class being defined in a class-head.
19506 Returns the TYPE_DECL representing the class. */
19508 static tree
19509 cp_parser_class_name (cp_parser *parser,
19510 bool typename_keyword_p,
19511 bool template_keyword_p,
19512 enum tag_types tag_type,
19513 bool check_dependency_p,
19514 bool class_head_p,
19515 bool is_declaration)
19517 tree decl;
19518 tree scope;
19519 bool typename_p;
19520 cp_token *token;
19521 tree identifier = NULL_TREE;
19523 /* All class-names start with an identifier. */
19524 token = cp_lexer_peek_token (parser->lexer);
19525 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19527 cp_parser_error (parser, "expected class-name");
19528 return error_mark_node;
19531 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19532 to a template-id, so we save it here. */
19533 scope = parser->scope;
19534 if (scope == error_mark_node)
19535 return error_mark_node;
19537 /* Any name names a type if we're following the `typename' keyword
19538 in a qualified name where the enclosing scope is type-dependent. */
19539 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19540 && dependent_type_p (scope));
19541 /* Handle the common case (an identifier, but not a template-id)
19542 efficiently. */
19543 if (token->type == CPP_NAME
19544 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19546 cp_token *identifier_token;
19547 bool ambiguous_p;
19549 /* Look for the identifier. */
19550 identifier_token = cp_lexer_peek_token (parser->lexer);
19551 ambiguous_p = identifier_token->error_reported;
19552 identifier = cp_parser_identifier (parser);
19553 /* If the next token isn't an identifier, we are certainly not
19554 looking at a class-name. */
19555 if (identifier == error_mark_node)
19556 decl = error_mark_node;
19557 /* If we know this is a type-name, there's no need to look it
19558 up. */
19559 else if (typename_p)
19560 decl = identifier;
19561 else
19563 tree ambiguous_decls;
19564 /* If we already know that this lookup is ambiguous, then
19565 we've already issued an error message; there's no reason
19566 to check again. */
19567 if (ambiguous_p)
19569 cp_parser_simulate_error (parser);
19570 return error_mark_node;
19572 /* If the next token is a `::', then the name must be a type
19573 name.
19575 [basic.lookup.qual]
19577 During the lookup for a name preceding the :: scope
19578 resolution operator, object, function, and enumerator
19579 names are ignored. */
19580 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19581 tag_type = typename_type;
19582 /* Look up the name. */
19583 decl = cp_parser_lookup_name (parser, identifier,
19584 tag_type,
19585 /*is_template=*/false,
19586 /*is_namespace=*/false,
19587 check_dependency_p,
19588 &ambiguous_decls,
19589 identifier_token->location);
19590 if (ambiguous_decls)
19592 if (cp_parser_parsing_tentatively (parser))
19593 cp_parser_simulate_error (parser);
19594 return error_mark_node;
19598 else
19600 /* Try a template-id. */
19601 decl = cp_parser_template_id (parser, template_keyword_p,
19602 check_dependency_p,
19603 tag_type,
19604 is_declaration);
19605 if (decl == error_mark_node)
19606 return error_mark_node;
19609 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19611 /* If this is a typename, create a TYPENAME_TYPE. */
19612 if (typename_p && decl != error_mark_node)
19614 decl = make_typename_type (scope, decl, typename_type,
19615 /*complain=*/tf_error);
19616 if (decl != error_mark_node)
19617 decl = TYPE_NAME (decl);
19620 decl = strip_using_decl (decl);
19622 /* Check to see that it is really the name of a class. */
19623 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19624 && identifier_p (TREE_OPERAND (decl, 0))
19625 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19626 /* Situations like this:
19628 template <typename T> struct A {
19629 typename T::template X<int>::I i;
19632 are problematic. Is `T::template X<int>' a class-name? The
19633 standard does not seem to be definitive, but there is no other
19634 valid interpretation of the following `::'. Therefore, those
19635 names are considered class-names. */
19637 decl = make_typename_type (scope, decl, tag_type, tf_error);
19638 if (decl != error_mark_node)
19639 decl = TYPE_NAME (decl);
19641 else if (TREE_CODE (decl) != TYPE_DECL
19642 || TREE_TYPE (decl) == error_mark_node
19643 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19644 /* In Objective-C 2.0, a classname followed by '.' starts a
19645 dot-syntax expression, and it's not a type-name. */
19646 || (c_dialect_objc ()
19647 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19648 && objc_is_class_name (decl)))
19649 decl = error_mark_node;
19651 if (decl == error_mark_node)
19652 cp_parser_error (parser, "expected class-name");
19653 else if (identifier && !parser->scope)
19654 maybe_note_name_used_in_class (identifier, decl);
19656 return decl;
19659 /* Parse a class-specifier.
19661 class-specifier:
19662 class-head { member-specification [opt] }
19664 Returns the TREE_TYPE representing the class. */
19666 static tree
19667 cp_parser_class_specifier_1 (cp_parser* parser)
19669 tree type;
19670 tree attributes = NULL_TREE;
19671 bool nested_name_specifier_p;
19672 unsigned saved_num_template_parameter_lists;
19673 bool saved_in_function_body;
19674 unsigned char in_statement;
19675 bool in_switch_statement_p;
19676 bool saved_in_unbraced_linkage_specification_p;
19677 tree old_scope = NULL_TREE;
19678 tree scope = NULL_TREE;
19679 cp_token *closing_brace;
19681 push_deferring_access_checks (dk_no_deferred);
19683 /* Parse the class-head. */
19684 type = cp_parser_class_head (parser,
19685 &nested_name_specifier_p);
19686 /* If the class-head was a semantic disaster, skip the entire body
19687 of the class. */
19688 if (!type)
19690 cp_parser_skip_to_end_of_block_or_statement (parser);
19691 pop_deferring_access_checks ();
19692 return error_mark_node;
19695 /* Look for the `{'. */
19696 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19698 pop_deferring_access_checks ();
19699 return error_mark_node;
19702 cp_ensure_no_omp_declare_simd (parser);
19704 /* Issue an error message if type-definitions are forbidden here. */
19705 cp_parser_check_type_definition (parser);
19706 /* Remember that we are defining one more class. */
19707 ++parser->num_classes_being_defined;
19708 /* Inside the class, surrounding template-parameter-lists do not
19709 apply. */
19710 saved_num_template_parameter_lists
19711 = parser->num_template_parameter_lists;
19712 parser->num_template_parameter_lists = 0;
19713 /* We are not in a function body. */
19714 saved_in_function_body = parser->in_function_body;
19715 parser->in_function_body = false;
19716 /* Or in a loop. */
19717 in_statement = parser->in_statement;
19718 parser->in_statement = 0;
19719 /* Or in a switch. */
19720 in_switch_statement_p = parser->in_switch_statement_p;
19721 parser->in_switch_statement_p = false;
19722 /* We are not immediately inside an extern "lang" block. */
19723 saved_in_unbraced_linkage_specification_p
19724 = parser->in_unbraced_linkage_specification_p;
19725 parser->in_unbraced_linkage_specification_p = false;
19727 /* Start the class. */
19728 if (nested_name_specifier_p)
19730 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19731 old_scope = push_inner_scope (scope);
19733 type = begin_class_definition (type);
19735 if (type == error_mark_node)
19736 /* If the type is erroneous, skip the entire body of the class. */
19737 cp_parser_skip_to_closing_brace (parser);
19738 else
19739 /* Parse the member-specification. */
19740 cp_parser_member_specification_opt (parser);
19742 /* Look for the trailing `}'. */
19743 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19744 /* Look for trailing attributes to apply to this class. */
19745 if (cp_parser_allow_gnu_extensions_p (parser))
19746 attributes = cp_parser_gnu_attributes_opt (parser);
19747 if (type != error_mark_node)
19748 type = finish_struct (type, attributes);
19749 if (nested_name_specifier_p)
19750 pop_inner_scope (old_scope, scope);
19752 /* We've finished a type definition. Check for the common syntax
19753 error of forgetting a semicolon after the definition. We need to
19754 be careful, as we can't just check for not-a-semicolon and be done
19755 with it; the user might have typed:
19757 class X { } c = ...;
19758 class X { } *p = ...;
19760 and so forth. Instead, enumerate all the possible tokens that
19761 might follow this production; if we don't see one of them, then
19762 complain and silently insert the semicolon. */
19764 cp_token *token = cp_lexer_peek_token (parser->lexer);
19765 bool want_semicolon = true;
19767 if (cp_next_tokens_can_be_std_attribute_p (parser))
19768 /* Don't try to parse c++11 attributes here. As per the
19769 grammar, that should be a task for
19770 cp_parser_decl_specifier_seq. */
19771 want_semicolon = false;
19773 switch (token->type)
19775 case CPP_NAME:
19776 case CPP_SEMICOLON:
19777 case CPP_MULT:
19778 case CPP_AND:
19779 case CPP_OPEN_PAREN:
19780 case CPP_CLOSE_PAREN:
19781 case CPP_COMMA:
19782 want_semicolon = false;
19783 break;
19785 /* While it's legal for type qualifiers and storage class
19786 specifiers to follow type definitions in the grammar, only
19787 compiler testsuites contain code like that. Assume that if
19788 we see such code, then what we're really seeing is a case
19789 like:
19791 class X { }
19792 const <type> var = ...;
19796 class Y { }
19797 static <type> func (...) ...
19799 i.e. the qualifier or specifier applies to the next
19800 declaration. To do so, however, we need to look ahead one
19801 more token to see if *that* token is a type specifier.
19803 This code could be improved to handle:
19805 class Z { }
19806 static const <type> var = ...; */
19807 case CPP_KEYWORD:
19808 if (keyword_is_decl_specifier (token->keyword))
19810 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19812 /* Handling user-defined types here would be nice, but very
19813 tricky. */
19814 want_semicolon
19815 = (lookahead->type == CPP_KEYWORD
19816 && keyword_begins_type_specifier (lookahead->keyword));
19818 break;
19819 default:
19820 break;
19823 /* If we don't have a type, then something is very wrong and we
19824 shouldn't try to do anything clever. Likewise for not seeing the
19825 closing brace. */
19826 if (closing_brace && TYPE_P (type) && want_semicolon)
19828 cp_token_position prev
19829 = cp_lexer_previous_token_position (parser->lexer);
19830 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19831 location_t loc = prev_token->location;
19833 if (CLASSTYPE_DECLARED_CLASS (type))
19834 error_at (loc, "expected %<;%> after class definition");
19835 else if (TREE_CODE (type) == RECORD_TYPE)
19836 error_at (loc, "expected %<;%> after struct definition");
19837 else if (TREE_CODE (type) == UNION_TYPE)
19838 error_at (loc, "expected %<;%> after union definition");
19839 else
19840 gcc_unreachable ();
19842 /* Unget one token and smash it to look as though we encountered
19843 a semicolon in the input stream. */
19844 cp_lexer_set_token_position (parser->lexer, prev);
19845 token = cp_lexer_peek_token (parser->lexer);
19846 token->type = CPP_SEMICOLON;
19847 token->keyword = RID_MAX;
19851 /* If this class is not itself within the scope of another class,
19852 then we need to parse the bodies of all of the queued function
19853 definitions. Note that the queued functions defined in a class
19854 are not always processed immediately following the
19855 class-specifier for that class. Consider:
19857 struct A {
19858 struct B { void f() { sizeof (A); } };
19861 If `f' were processed before the processing of `A' were
19862 completed, there would be no way to compute the size of `A'.
19863 Note that the nesting we are interested in here is lexical --
19864 not the semantic nesting given by TYPE_CONTEXT. In particular,
19865 for:
19867 struct A { struct B; };
19868 struct A::B { void f() { } };
19870 there is no need to delay the parsing of `A::B::f'. */
19871 if (--parser->num_classes_being_defined == 0)
19873 tree decl;
19874 tree class_type = NULL_TREE;
19875 tree pushed_scope = NULL_TREE;
19876 unsigned ix;
19877 cp_default_arg_entry *e;
19878 tree save_ccp, save_ccr;
19880 /* In a first pass, parse default arguments to the functions.
19881 Then, in a second pass, parse the bodies of the functions.
19882 This two-phased approach handles cases like:
19884 struct S {
19885 void f() { g(); }
19886 void g(int i = 3);
19890 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19892 decl = e->decl;
19893 /* If there are default arguments that have not yet been processed,
19894 take care of them now. */
19895 if (class_type != e->class_type)
19897 if (pushed_scope)
19898 pop_scope (pushed_scope);
19899 class_type = e->class_type;
19900 pushed_scope = push_scope (class_type);
19902 /* Make sure that any template parameters are in scope. */
19903 maybe_begin_member_template_processing (decl);
19904 /* Parse the default argument expressions. */
19905 cp_parser_late_parsing_default_args (parser, decl);
19906 /* Remove any template parameters from the symbol table. */
19907 maybe_end_member_template_processing ();
19909 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19910 /* Now parse any NSDMIs. */
19911 save_ccp = current_class_ptr;
19912 save_ccr = current_class_ref;
19913 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19915 if (class_type != DECL_CONTEXT (decl))
19917 if (pushed_scope)
19918 pop_scope (pushed_scope);
19919 class_type = DECL_CONTEXT (decl);
19920 pushed_scope = push_scope (class_type);
19922 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19923 cp_parser_late_parsing_nsdmi (parser, decl);
19925 vec_safe_truncate (unparsed_nsdmis, 0);
19926 current_class_ptr = save_ccp;
19927 current_class_ref = save_ccr;
19928 if (pushed_scope)
19929 pop_scope (pushed_scope);
19931 /* Now do some post-NSDMI bookkeeping. */
19932 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19933 after_nsdmi_defaulted_late_checks (class_type);
19934 vec_safe_truncate (unparsed_classes, 0);
19935 after_nsdmi_defaulted_late_checks (type);
19937 /* Now parse the body of the functions. */
19938 if (flag_openmp)
19940 /* OpenMP UDRs need to be parsed before all other functions. */
19941 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19942 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19943 cp_parser_late_parsing_for_member (parser, decl);
19944 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19945 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19946 cp_parser_late_parsing_for_member (parser, decl);
19948 else
19949 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19950 cp_parser_late_parsing_for_member (parser, decl);
19951 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19953 else
19954 vec_safe_push (unparsed_classes, type);
19956 /* Put back any saved access checks. */
19957 pop_deferring_access_checks ();
19959 /* Restore saved state. */
19960 parser->in_switch_statement_p = in_switch_statement_p;
19961 parser->in_statement = in_statement;
19962 parser->in_function_body = saved_in_function_body;
19963 parser->num_template_parameter_lists
19964 = saved_num_template_parameter_lists;
19965 parser->in_unbraced_linkage_specification_p
19966 = saved_in_unbraced_linkage_specification_p;
19968 return type;
19971 static tree
19972 cp_parser_class_specifier (cp_parser* parser)
19974 tree ret;
19975 timevar_push (TV_PARSE_STRUCT);
19976 ret = cp_parser_class_specifier_1 (parser);
19977 timevar_pop (TV_PARSE_STRUCT);
19978 return ret;
19981 /* Parse a class-head.
19983 class-head:
19984 class-key identifier [opt] base-clause [opt]
19985 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19986 class-key nested-name-specifier [opt] template-id
19987 base-clause [opt]
19989 class-virt-specifier:
19990 final
19992 GNU Extensions:
19993 class-key attributes identifier [opt] base-clause [opt]
19994 class-key attributes nested-name-specifier identifier base-clause [opt]
19995 class-key attributes nested-name-specifier [opt] template-id
19996 base-clause [opt]
19998 Upon return BASES is initialized to the list of base classes (or
19999 NULL, if there are none) in the same form returned by
20000 cp_parser_base_clause.
20002 Returns the TYPE of the indicated class. Sets
20003 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20004 involving a nested-name-specifier was used, and FALSE otherwise.
20006 Returns error_mark_node if this is not a class-head.
20008 Returns NULL_TREE if the class-head is syntactically valid, but
20009 semantically invalid in a way that means we should skip the entire
20010 body of the class. */
20012 static tree
20013 cp_parser_class_head (cp_parser* parser,
20014 bool* nested_name_specifier_p)
20016 tree nested_name_specifier;
20017 enum tag_types class_key;
20018 tree id = NULL_TREE;
20019 tree type = NULL_TREE;
20020 tree attributes;
20021 tree bases;
20022 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20023 bool template_id_p = false;
20024 bool qualified_p = false;
20025 bool invalid_nested_name_p = false;
20026 bool invalid_explicit_specialization_p = false;
20027 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20028 tree pushed_scope = NULL_TREE;
20029 unsigned num_templates;
20030 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20031 /* Assume no nested-name-specifier will be present. */
20032 *nested_name_specifier_p = false;
20033 /* Assume no template parameter lists will be used in defining the
20034 type. */
20035 num_templates = 0;
20036 parser->colon_corrects_to_scope_p = false;
20038 /* Look for the class-key. */
20039 class_key = cp_parser_class_key (parser);
20040 if (class_key == none_type)
20041 return error_mark_node;
20043 /* Parse the attributes. */
20044 attributes = cp_parser_attributes_opt (parser);
20046 /* If the next token is `::', that is invalid -- but sometimes
20047 people do try to write:
20049 struct ::S {};
20051 Handle this gracefully by accepting the extra qualifier, and then
20052 issuing an error about it later if this really is a
20053 class-head. If it turns out just to be an elaborated type
20054 specifier, remain silent. */
20055 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20056 qualified_p = true;
20058 push_deferring_access_checks (dk_no_check);
20060 /* Determine the name of the class. Begin by looking for an
20061 optional nested-name-specifier. */
20062 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20063 nested_name_specifier
20064 = cp_parser_nested_name_specifier_opt (parser,
20065 /*typename_keyword_p=*/false,
20066 /*check_dependency_p=*/false,
20067 /*type_p=*/true,
20068 /*is_declaration=*/false);
20069 /* If there was a nested-name-specifier, then there *must* be an
20070 identifier. */
20071 if (nested_name_specifier)
20073 type_start_token = cp_lexer_peek_token (parser->lexer);
20074 /* Although the grammar says `identifier', it really means
20075 `class-name' or `template-name'. You are only allowed to
20076 define a class that has already been declared with this
20077 syntax.
20079 The proposed resolution for Core Issue 180 says that wherever
20080 you see `class T::X' you should treat `X' as a type-name.
20082 It is OK to define an inaccessible class; for example:
20084 class A { class B; };
20085 class A::B {};
20087 We do not know if we will see a class-name, or a
20088 template-name. We look for a class-name first, in case the
20089 class-name is a template-id; if we looked for the
20090 template-name first we would stop after the template-name. */
20091 cp_parser_parse_tentatively (parser);
20092 type = cp_parser_class_name (parser,
20093 /*typename_keyword_p=*/false,
20094 /*template_keyword_p=*/false,
20095 class_type,
20096 /*check_dependency_p=*/false,
20097 /*class_head_p=*/true,
20098 /*is_declaration=*/false);
20099 /* If that didn't work, ignore the nested-name-specifier. */
20100 if (!cp_parser_parse_definitely (parser))
20102 invalid_nested_name_p = true;
20103 type_start_token = cp_lexer_peek_token (parser->lexer);
20104 id = cp_parser_identifier (parser);
20105 if (id == error_mark_node)
20106 id = NULL_TREE;
20108 /* If we could not find a corresponding TYPE, treat this
20109 declaration like an unqualified declaration. */
20110 if (type == error_mark_node)
20111 nested_name_specifier = NULL_TREE;
20112 /* Otherwise, count the number of templates used in TYPE and its
20113 containing scopes. */
20114 else
20116 tree scope;
20118 for (scope = TREE_TYPE (type);
20119 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20120 scope = get_containing_scope (scope))
20121 if (TYPE_P (scope)
20122 && CLASS_TYPE_P (scope)
20123 && CLASSTYPE_TEMPLATE_INFO (scope)
20124 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20125 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20126 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20127 ++num_templates;
20130 /* Otherwise, the identifier is optional. */
20131 else
20133 /* We don't know whether what comes next is a template-id,
20134 an identifier, or nothing at all. */
20135 cp_parser_parse_tentatively (parser);
20136 /* Check for a template-id. */
20137 type_start_token = cp_lexer_peek_token (parser->lexer);
20138 id = cp_parser_template_id (parser,
20139 /*template_keyword_p=*/false,
20140 /*check_dependency_p=*/true,
20141 class_key,
20142 /*is_declaration=*/true);
20143 /* If that didn't work, it could still be an identifier. */
20144 if (!cp_parser_parse_definitely (parser))
20146 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20148 type_start_token = cp_lexer_peek_token (parser->lexer);
20149 id = cp_parser_identifier (parser);
20151 else
20152 id = NULL_TREE;
20154 else
20156 template_id_p = true;
20157 ++num_templates;
20161 pop_deferring_access_checks ();
20163 if (id)
20165 cp_parser_check_for_invalid_template_id (parser, id,
20166 class_key,
20167 type_start_token->location);
20169 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20171 /* If it's not a `:' or a `{' then we can't really be looking at a
20172 class-head, since a class-head only appears as part of a
20173 class-specifier. We have to detect this situation before calling
20174 xref_tag, since that has irreversible side-effects. */
20175 if (!cp_parser_next_token_starts_class_definition_p (parser))
20177 cp_parser_error (parser, "expected %<{%> or %<:%>");
20178 type = error_mark_node;
20179 goto out;
20182 /* At this point, we're going ahead with the class-specifier, even
20183 if some other problem occurs. */
20184 cp_parser_commit_to_tentative_parse (parser);
20185 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20187 cp_parser_error (parser,
20188 "cannot specify %<override%> for a class");
20189 type = error_mark_node;
20190 goto out;
20192 /* Issue the error about the overly-qualified name now. */
20193 if (qualified_p)
20195 cp_parser_error (parser,
20196 "global qualification of class name is invalid");
20197 type = error_mark_node;
20198 goto out;
20200 else if (invalid_nested_name_p)
20202 cp_parser_error (parser,
20203 "qualified name does not name a class");
20204 type = error_mark_node;
20205 goto out;
20207 else if (nested_name_specifier)
20209 tree scope;
20211 /* Reject typedef-names in class heads. */
20212 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20214 error_at (type_start_token->location,
20215 "invalid class name in declaration of %qD",
20216 type);
20217 type = NULL_TREE;
20218 goto done;
20221 /* Figure out in what scope the declaration is being placed. */
20222 scope = current_scope ();
20223 /* If that scope does not contain the scope in which the
20224 class was originally declared, the program is invalid. */
20225 if (scope && !is_ancestor (scope, nested_name_specifier))
20227 if (at_namespace_scope_p ())
20228 error_at (type_start_token->location,
20229 "declaration of %qD in namespace %qD which does not "
20230 "enclose %qD",
20231 type, scope, nested_name_specifier);
20232 else
20233 error_at (type_start_token->location,
20234 "declaration of %qD in %qD which does not enclose %qD",
20235 type, scope, nested_name_specifier);
20236 type = NULL_TREE;
20237 goto done;
20239 /* [dcl.meaning]
20241 A declarator-id shall not be qualified except for the
20242 definition of a ... nested class outside of its class
20243 ... [or] the definition or explicit instantiation of a
20244 class member of a namespace outside of its namespace. */
20245 if (scope == nested_name_specifier)
20247 permerror (nested_name_specifier_token_start->location,
20248 "extra qualification not allowed");
20249 nested_name_specifier = NULL_TREE;
20250 num_templates = 0;
20253 /* An explicit-specialization must be preceded by "template <>". If
20254 it is not, try to recover gracefully. */
20255 if (at_namespace_scope_p ()
20256 && parser->num_template_parameter_lists == 0
20257 && template_id_p)
20259 error_at (type_start_token->location,
20260 "an explicit specialization must be preceded by %<template <>%>");
20261 invalid_explicit_specialization_p = true;
20262 /* Take the same action that would have been taken by
20263 cp_parser_explicit_specialization. */
20264 ++parser->num_template_parameter_lists;
20265 begin_specialization ();
20267 /* There must be no "return" statements between this point and the
20268 end of this function; set "type "to the correct return value and
20269 use "goto done;" to return. */
20270 /* Make sure that the right number of template parameters were
20271 present. */
20272 if (!cp_parser_check_template_parameters (parser, num_templates,
20273 type_start_token->location,
20274 /*declarator=*/NULL))
20276 /* If something went wrong, there is no point in even trying to
20277 process the class-definition. */
20278 type = NULL_TREE;
20279 goto done;
20282 /* Look up the type. */
20283 if (template_id_p)
20285 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20286 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20287 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20289 error_at (type_start_token->location,
20290 "function template %qD redeclared as a class template", id);
20291 type = error_mark_node;
20293 else
20295 type = TREE_TYPE (id);
20296 type = maybe_process_partial_specialization (type);
20298 if (nested_name_specifier)
20299 pushed_scope = push_scope (nested_name_specifier);
20301 else if (nested_name_specifier)
20303 tree class_type;
20305 /* Given:
20307 template <typename T> struct S { struct T };
20308 template <typename T> struct S<T>::T { };
20310 we will get a TYPENAME_TYPE when processing the definition of
20311 `S::T'. We need to resolve it to the actual type before we
20312 try to define it. */
20313 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20315 class_type = resolve_typename_type (TREE_TYPE (type),
20316 /*only_current_p=*/false);
20317 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20318 type = TYPE_NAME (class_type);
20319 else
20321 cp_parser_error (parser, "could not resolve typename type");
20322 type = error_mark_node;
20326 if (maybe_process_partial_specialization (TREE_TYPE (type))
20327 == error_mark_node)
20329 type = NULL_TREE;
20330 goto done;
20333 class_type = current_class_type;
20334 /* Enter the scope indicated by the nested-name-specifier. */
20335 pushed_scope = push_scope (nested_name_specifier);
20336 /* Get the canonical version of this type. */
20337 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20338 /* Call push_template_decl if it seems like we should be defining a
20339 template either from the template headers or the type we're
20340 defining, so that we diagnose both extra and missing headers. */
20341 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20342 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20343 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20345 type = push_template_decl (type);
20346 if (type == error_mark_node)
20348 type = NULL_TREE;
20349 goto done;
20353 type = TREE_TYPE (type);
20354 *nested_name_specifier_p = true;
20356 else /* The name is not a nested name. */
20358 /* If the class was unnamed, create a dummy name. */
20359 if (!id)
20360 id = make_anon_name ();
20361 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20362 parser->num_template_parameter_lists);
20365 /* Indicate whether this class was declared as a `class' or as a
20366 `struct'. */
20367 if (TREE_CODE (type) == RECORD_TYPE)
20368 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20369 cp_parser_check_class_key (class_key, type);
20371 /* If this type was already complete, and we see another definition,
20372 that's an error. */
20373 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20375 error_at (type_start_token->location, "redefinition of %q#T",
20376 type);
20377 error_at (type_start_token->location, "previous definition of %q+#T",
20378 type);
20379 type = NULL_TREE;
20380 goto done;
20382 else if (type == error_mark_node)
20383 type = NULL_TREE;
20385 if (type)
20387 /* Apply attributes now, before any use of the class as a template
20388 argument in its base list. */
20389 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20390 fixup_attribute_variants (type);
20393 /* We will have entered the scope containing the class; the names of
20394 base classes should be looked up in that context. For example:
20396 struct A { struct B {}; struct C; };
20397 struct A::C : B {};
20399 is valid. */
20401 /* Get the list of base-classes, if there is one. */
20402 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20404 /* PR59482: enter the class scope so that base-specifiers are looked
20405 up correctly. */
20406 if (type)
20407 pushclass (type);
20408 bases = cp_parser_base_clause (parser);
20409 /* PR59482: get out of the previously pushed class scope so that the
20410 subsequent pops pop the right thing. */
20411 if (type)
20412 popclass ();
20414 else
20415 bases = NULL_TREE;
20417 /* If we're really defining a class, process the base classes.
20418 If they're invalid, fail. */
20419 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20420 && !xref_basetypes (type, bases))
20421 type = NULL_TREE;
20423 done:
20424 /* Leave the scope given by the nested-name-specifier. We will
20425 enter the class scope itself while processing the members. */
20426 if (pushed_scope)
20427 pop_scope (pushed_scope);
20429 if (invalid_explicit_specialization_p)
20431 end_specialization ();
20432 --parser->num_template_parameter_lists;
20435 if (type)
20436 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20437 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20438 CLASSTYPE_FINAL (type) = 1;
20439 out:
20440 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20441 return type;
20444 /* Parse a class-key.
20446 class-key:
20447 class
20448 struct
20449 union
20451 Returns the kind of class-key specified, or none_type to indicate
20452 error. */
20454 static enum tag_types
20455 cp_parser_class_key (cp_parser* parser)
20457 cp_token *token;
20458 enum tag_types tag_type;
20460 /* Look for the class-key. */
20461 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20462 if (!token)
20463 return none_type;
20465 /* Check to see if the TOKEN is a class-key. */
20466 tag_type = cp_parser_token_is_class_key (token);
20467 if (!tag_type)
20468 cp_parser_error (parser, "expected class-key");
20469 return tag_type;
20472 /* Parse a type-parameter-key.
20474 type-parameter-key:
20475 class
20476 typename
20479 static void
20480 cp_parser_type_parameter_key (cp_parser* parser)
20482 /* Look for the type-parameter-key. */
20483 enum tag_types tag_type = none_type;
20484 cp_token *token = cp_lexer_peek_token (parser->lexer);
20485 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20487 cp_lexer_consume_token (parser->lexer);
20488 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20489 /* typename is not allowed in a template template parameter
20490 by the standard until C++1Z. */
20491 pedwarn (token->location, OPT_Wpedantic,
20492 "ISO C++ forbids typename key in template template parameter;"
20493 " use -std=c++1z or -std=gnu++1z");
20495 else
20496 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20498 return;
20501 /* Parse an (optional) member-specification.
20503 member-specification:
20504 member-declaration member-specification [opt]
20505 access-specifier : member-specification [opt] */
20507 static void
20508 cp_parser_member_specification_opt (cp_parser* parser)
20510 while (true)
20512 cp_token *token;
20513 enum rid keyword;
20515 /* Peek at the next token. */
20516 token = cp_lexer_peek_token (parser->lexer);
20517 /* If it's a `}', or EOF then we've seen all the members. */
20518 if (token->type == CPP_CLOSE_BRACE
20519 || token->type == CPP_EOF
20520 || token->type == CPP_PRAGMA_EOL)
20521 break;
20523 /* See if this token is a keyword. */
20524 keyword = token->keyword;
20525 switch (keyword)
20527 case RID_PUBLIC:
20528 case RID_PROTECTED:
20529 case RID_PRIVATE:
20530 /* Consume the access-specifier. */
20531 cp_lexer_consume_token (parser->lexer);
20532 /* Remember which access-specifier is active. */
20533 current_access_specifier = token->u.value;
20534 /* Look for the `:'. */
20535 cp_parser_require (parser, CPP_COLON, RT_COLON);
20536 break;
20538 default:
20539 /* Accept #pragmas at class scope. */
20540 if (token->type == CPP_PRAGMA)
20542 cp_parser_pragma (parser, pragma_member);
20543 break;
20546 /* Otherwise, the next construction must be a
20547 member-declaration. */
20548 cp_parser_member_declaration (parser);
20553 /* Parse a member-declaration.
20555 member-declaration:
20556 decl-specifier-seq [opt] member-declarator-list [opt] ;
20557 function-definition ; [opt]
20558 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20559 using-declaration
20560 template-declaration
20561 alias-declaration
20563 member-declarator-list:
20564 member-declarator
20565 member-declarator-list , member-declarator
20567 member-declarator:
20568 declarator pure-specifier [opt]
20569 declarator constant-initializer [opt]
20570 identifier [opt] : constant-expression
20572 GNU Extensions:
20574 member-declaration:
20575 __extension__ member-declaration
20577 member-declarator:
20578 declarator attributes [opt] pure-specifier [opt]
20579 declarator attributes [opt] constant-initializer [opt]
20580 identifier [opt] attributes [opt] : constant-expression
20582 C++0x Extensions:
20584 member-declaration:
20585 static_assert-declaration */
20587 static void
20588 cp_parser_member_declaration (cp_parser* parser)
20590 cp_decl_specifier_seq decl_specifiers;
20591 tree prefix_attributes;
20592 tree decl;
20593 int declares_class_or_enum;
20594 bool friend_p;
20595 cp_token *token = NULL;
20596 cp_token *decl_spec_token_start = NULL;
20597 cp_token *initializer_token_start = NULL;
20598 int saved_pedantic;
20599 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20601 /* Check for the `__extension__' keyword. */
20602 if (cp_parser_extension_opt (parser, &saved_pedantic))
20604 /* Recurse. */
20605 cp_parser_member_declaration (parser);
20606 /* Restore the old value of the PEDANTIC flag. */
20607 pedantic = saved_pedantic;
20609 return;
20612 /* Check for a template-declaration. */
20613 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20615 /* An explicit specialization here is an error condition, and we
20616 expect the specialization handler to detect and report this. */
20617 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20618 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20619 cp_parser_explicit_specialization (parser);
20620 else
20621 cp_parser_template_declaration (parser, /*member_p=*/true);
20623 return;
20626 /* Check for a using-declaration. */
20627 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20629 if (cxx_dialect < cxx11)
20631 /* Parse the using-declaration. */
20632 cp_parser_using_declaration (parser,
20633 /*access_declaration_p=*/false);
20634 return;
20636 else
20638 tree decl;
20639 bool alias_decl_expected;
20640 cp_parser_parse_tentatively (parser);
20641 decl = cp_parser_alias_declaration (parser);
20642 /* Note that if we actually see the '=' token after the
20643 identifier, cp_parser_alias_declaration commits the
20644 tentative parse. In that case, we really expects an
20645 alias-declaration. Otherwise, we expect a using
20646 declaration. */
20647 alias_decl_expected =
20648 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20649 cp_parser_parse_definitely (parser);
20651 if (alias_decl_expected)
20652 finish_member_declaration (decl);
20653 else
20654 cp_parser_using_declaration (parser,
20655 /*access_declaration_p=*/false);
20656 return;
20660 /* Check for @defs. */
20661 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20663 tree ivar, member;
20664 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20665 ivar = ivar_chains;
20666 while (ivar)
20668 member = ivar;
20669 ivar = TREE_CHAIN (member);
20670 TREE_CHAIN (member) = NULL_TREE;
20671 finish_member_declaration (member);
20673 return;
20676 /* If the next token is `static_assert' we have a static assertion. */
20677 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20679 cp_parser_static_assert (parser, /*member_p=*/true);
20680 return;
20683 parser->colon_corrects_to_scope_p = false;
20685 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20686 goto out;
20688 /* Parse the decl-specifier-seq. */
20689 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20690 cp_parser_decl_specifier_seq (parser,
20691 CP_PARSER_FLAGS_OPTIONAL,
20692 &decl_specifiers,
20693 &declares_class_or_enum);
20694 /* Check for an invalid type-name. */
20695 if (!decl_specifiers.any_type_specifiers_p
20696 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20697 goto out;
20698 /* If there is no declarator, then the decl-specifier-seq should
20699 specify a type. */
20700 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20702 /* If there was no decl-specifier-seq, and the next token is a
20703 `;', then we have something like:
20705 struct S { ; };
20707 [class.mem]
20709 Each member-declaration shall declare at least one member
20710 name of the class. */
20711 if (!decl_specifiers.any_specifiers_p)
20713 cp_token *token = cp_lexer_peek_token (parser->lexer);
20714 if (!in_system_header_at (token->location))
20715 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20717 else
20719 tree type;
20721 /* See if this declaration is a friend. */
20722 friend_p = cp_parser_friend_p (&decl_specifiers);
20723 /* If there were decl-specifiers, check to see if there was
20724 a class-declaration. */
20725 type = check_tag_decl (&decl_specifiers,
20726 /*explicit_type_instantiation_p=*/false);
20727 /* Nested classes have already been added to the class, but
20728 a `friend' needs to be explicitly registered. */
20729 if (friend_p)
20731 /* If the `friend' keyword was present, the friend must
20732 be introduced with a class-key. */
20733 if (!declares_class_or_enum && cxx_dialect < cxx11)
20734 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20735 "in C++03 a class-key must be used "
20736 "when declaring a friend");
20737 /* In this case:
20739 template <typename T> struct A {
20740 friend struct A<T>::B;
20743 A<T>::B will be represented by a TYPENAME_TYPE, and
20744 therefore not recognized by check_tag_decl. */
20745 if (!type)
20747 type = decl_specifiers.type;
20748 if (type && TREE_CODE (type) == TYPE_DECL)
20749 type = TREE_TYPE (type);
20751 if (!type || !TYPE_P (type))
20752 error_at (decl_spec_token_start->location,
20753 "friend declaration does not name a class or "
20754 "function");
20755 else
20756 make_friend_class (current_class_type, type,
20757 /*complain=*/true);
20759 /* If there is no TYPE, an error message will already have
20760 been issued. */
20761 else if (!type || type == error_mark_node)
20763 /* An anonymous aggregate has to be handled specially; such
20764 a declaration really declares a data member (with a
20765 particular type), as opposed to a nested class. */
20766 else if (ANON_AGGR_TYPE_P (type))
20768 /* C++11 9.5/6. */
20769 if (decl_specifiers.storage_class != sc_none)
20770 error_at (decl_spec_token_start->location,
20771 "a storage class on an anonymous aggregate "
20772 "in class scope is not allowed");
20774 /* Remove constructors and such from TYPE, now that we
20775 know it is an anonymous aggregate. */
20776 fixup_anonymous_aggr (type);
20777 /* And make the corresponding data member. */
20778 decl = build_decl (decl_spec_token_start->location,
20779 FIELD_DECL, NULL_TREE, type);
20780 /* Add it to the class. */
20781 finish_member_declaration (decl);
20783 else
20784 cp_parser_check_access_in_redeclaration
20785 (TYPE_NAME (type),
20786 decl_spec_token_start->location);
20789 else
20791 bool assume_semicolon = false;
20793 /* Clear attributes from the decl_specifiers but keep them
20794 around as prefix attributes that apply them to the entity
20795 being declared. */
20796 prefix_attributes = decl_specifiers.attributes;
20797 decl_specifiers.attributes = NULL_TREE;
20799 /* See if these declarations will be friends. */
20800 friend_p = cp_parser_friend_p (&decl_specifiers);
20802 /* Keep going until we hit the `;' at the end of the
20803 declaration. */
20804 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20806 tree attributes = NULL_TREE;
20807 tree first_attribute;
20809 /* Peek at the next token. */
20810 token = cp_lexer_peek_token (parser->lexer);
20812 /* Check for a bitfield declaration. */
20813 if (token->type == CPP_COLON
20814 || (token->type == CPP_NAME
20815 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20816 == CPP_COLON))
20818 tree identifier;
20819 tree width;
20821 /* Get the name of the bitfield. Note that we cannot just
20822 check TOKEN here because it may have been invalidated by
20823 the call to cp_lexer_peek_nth_token above. */
20824 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20825 identifier = cp_parser_identifier (parser);
20826 else
20827 identifier = NULL_TREE;
20829 /* Consume the `:' token. */
20830 cp_lexer_consume_token (parser->lexer);
20831 /* Get the width of the bitfield. */
20832 width
20833 = cp_parser_constant_expression (parser);
20835 /* Look for attributes that apply to the bitfield. */
20836 attributes = cp_parser_attributes_opt (parser);
20837 /* Remember which attributes are prefix attributes and
20838 which are not. */
20839 first_attribute = attributes;
20840 /* Combine the attributes. */
20841 attributes = chainon (prefix_attributes, attributes);
20843 /* Create the bitfield declaration. */
20844 decl = grokbitfield (identifier
20845 ? make_id_declarator (NULL_TREE,
20846 identifier,
20847 sfk_none)
20848 : NULL,
20849 &decl_specifiers,
20850 width,
20851 attributes);
20853 else
20855 cp_declarator *declarator;
20856 tree initializer;
20857 tree asm_specification;
20858 int ctor_dtor_or_conv_p;
20860 /* Parse the declarator. */
20861 declarator
20862 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20863 &ctor_dtor_or_conv_p,
20864 /*parenthesized_p=*/NULL,
20865 /*member_p=*/true,
20866 friend_p);
20868 /* If something went wrong parsing the declarator, make sure
20869 that we at least consume some tokens. */
20870 if (declarator == cp_error_declarator)
20872 /* Skip to the end of the statement. */
20873 cp_parser_skip_to_end_of_statement (parser);
20874 /* If the next token is not a semicolon, that is
20875 probably because we just skipped over the body of
20876 a function. So, we consume a semicolon if
20877 present, but do not issue an error message if it
20878 is not present. */
20879 if (cp_lexer_next_token_is (parser->lexer,
20880 CPP_SEMICOLON))
20881 cp_lexer_consume_token (parser->lexer);
20882 goto out;
20885 if (declares_class_or_enum & 2)
20886 cp_parser_check_for_definition_in_return_type
20887 (declarator, decl_specifiers.type,
20888 decl_specifiers.locations[ds_type_spec]);
20890 /* Look for an asm-specification. */
20891 asm_specification = cp_parser_asm_specification_opt (parser);
20892 /* Look for attributes that apply to the declaration. */
20893 attributes = cp_parser_attributes_opt (parser);
20894 /* Remember which attributes are prefix attributes and
20895 which are not. */
20896 first_attribute = attributes;
20897 /* Combine the attributes. */
20898 attributes = chainon (prefix_attributes, attributes);
20900 /* If it's an `=', then we have a constant-initializer or a
20901 pure-specifier. It is not correct to parse the
20902 initializer before registering the member declaration
20903 since the member declaration should be in scope while
20904 its initializer is processed. However, the rest of the
20905 front end does not yet provide an interface that allows
20906 us to handle this correctly. */
20907 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20909 /* In [class.mem]:
20911 A pure-specifier shall be used only in the declaration of
20912 a virtual function.
20914 A member-declarator can contain a constant-initializer
20915 only if it declares a static member of integral or
20916 enumeration type.
20918 Therefore, if the DECLARATOR is for a function, we look
20919 for a pure-specifier; otherwise, we look for a
20920 constant-initializer. When we call `grokfield', it will
20921 perform more stringent semantics checks. */
20922 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20923 if (function_declarator_p (declarator)
20924 || (decl_specifiers.type
20925 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20926 && declarator->kind == cdk_id
20927 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20928 == FUNCTION_TYPE)))
20929 initializer = cp_parser_pure_specifier (parser);
20930 else if (decl_specifiers.storage_class != sc_static)
20931 initializer = cp_parser_save_nsdmi (parser);
20932 else if (cxx_dialect >= cxx11)
20934 bool nonconst;
20935 /* Don't require a constant rvalue in C++11, since we
20936 might want a reference constant. We'll enforce
20937 constancy later. */
20938 cp_lexer_consume_token (parser->lexer);
20939 /* Parse the initializer. */
20940 initializer = cp_parser_initializer_clause (parser,
20941 &nonconst);
20943 else
20944 /* Parse the initializer. */
20945 initializer = cp_parser_constant_initializer (parser);
20947 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20948 && !function_declarator_p (declarator))
20950 bool x;
20951 if (decl_specifiers.storage_class != sc_static)
20952 initializer = cp_parser_save_nsdmi (parser);
20953 else
20954 initializer = cp_parser_initializer (parser, &x, &x);
20956 /* Otherwise, there is no initializer. */
20957 else
20958 initializer = NULL_TREE;
20960 /* See if we are probably looking at a function
20961 definition. We are certainly not looking at a
20962 member-declarator. Calling `grokfield' has
20963 side-effects, so we must not do it unless we are sure
20964 that we are looking at a member-declarator. */
20965 if (cp_parser_token_starts_function_definition_p
20966 (cp_lexer_peek_token (parser->lexer)))
20968 /* The grammar does not allow a pure-specifier to be
20969 used when a member function is defined. (It is
20970 possible that this fact is an oversight in the
20971 standard, since a pure function may be defined
20972 outside of the class-specifier. */
20973 if (initializer && initializer_token_start)
20974 error_at (initializer_token_start->location,
20975 "pure-specifier on function-definition");
20976 decl = cp_parser_save_member_function_body (parser,
20977 &decl_specifiers,
20978 declarator,
20979 attributes);
20980 if (parser->fully_implicit_function_template_p)
20981 decl = finish_fully_implicit_template (parser, decl);
20982 /* If the member was not a friend, declare it here. */
20983 if (!friend_p)
20984 finish_member_declaration (decl);
20985 /* Peek at the next token. */
20986 token = cp_lexer_peek_token (parser->lexer);
20987 /* If the next token is a semicolon, consume it. */
20988 if (token->type == CPP_SEMICOLON)
20989 cp_lexer_consume_token (parser->lexer);
20990 goto out;
20992 else
20993 if (declarator->kind == cdk_function)
20994 declarator->id_loc = token->location;
20995 /* Create the declaration. */
20996 decl = grokfield (declarator, &decl_specifiers,
20997 initializer, /*init_const_expr_p=*/true,
20998 asm_specification, attributes);
20999 if (parser->fully_implicit_function_template_p)
21001 if (friend_p)
21002 finish_fully_implicit_template (parser, 0);
21003 else
21004 decl = finish_fully_implicit_template (parser, decl);
21008 cp_finalize_omp_declare_simd (parser, decl);
21010 /* Reset PREFIX_ATTRIBUTES. */
21011 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21012 attributes = TREE_CHAIN (attributes);
21013 if (attributes)
21014 TREE_CHAIN (attributes) = NULL_TREE;
21016 /* If there is any qualification still in effect, clear it
21017 now; we will be starting fresh with the next declarator. */
21018 parser->scope = NULL_TREE;
21019 parser->qualifying_scope = NULL_TREE;
21020 parser->object_scope = NULL_TREE;
21021 /* If it's a `,', then there are more declarators. */
21022 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21024 cp_lexer_consume_token (parser->lexer);
21025 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21027 cp_token *token = cp_lexer_previous_token (parser->lexer);
21028 error_at (token->location,
21029 "stray %<,%> at end of member declaration");
21032 /* If the next token isn't a `;', then we have a parse error. */
21033 else if (cp_lexer_next_token_is_not (parser->lexer,
21034 CPP_SEMICOLON))
21036 /* The next token might be a ways away from where the
21037 actual semicolon is missing. Find the previous token
21038 and use that for our error position. */
21039 cp_token *token = cp_lexer_previous_token (parser->lexer);
21040 error_at (token->location,
21041 "expected %<;%> at end of member declaration");
21043 /* Assume that the user meant to provide a semicolon. If
21044 we were to cp_parser_skip_to_end_of_statement, we might
21045 skip to a semicolon inside a member function definition
21046 and issue nonsensical error messages. */
21047 assume_semicolon = true;
21050 if (decl)
21052 /* Add DECL to the list of members. */
21053 if (!friend_p)
21054 finish_member_declaration (decl);
21056 if (TREE_CODE (decl) == FUNCTION_DECL)
21057 cp_parser_save_default_args (parser, decl);
21058 else if (TREE_CODE (decl) == FIELD_DECL
21059 && !DECL_C_BIT_FIELD (decl)
21060 && DECL_INITIAL (decl))
21061 /* Add DECL to the queue of NSDMI to be parsed later. */
21062 vec_safe_push (unparsed_nsdmis, decl);
21065 if (assume_semicolon)
21066 goto out;
21070 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21071 out:
21072 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21075 /* Parse a pure-specifier.
21077 pure-specifier:
21080 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21081 Otherwise, ERROR_MARK_NODE is returned. */
21083 static tree
21084 cp_parser_pure_specifier (cp_parser* parser)
21086 cp_token *token;
21088 /* Look for the `=' token. */
21089 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21090 return error_mark_node;
21091 /* Look for the `0' token. */
21092 token = cp_lexer_peek_token (parser->lexer);
21094 if (token->type == CPP_EOF
21095 || token->type == CPP_PRAGMA_EOL)
21096 return error_mark_node;
21098 cp_lexer_consume_token (parser->lexer);
21100 /* Accept = default or = delete in c++0x mode. */
21101 if (token->keyword == RID_DEFAULT
21102 || token->keyword == RID_DELETE)
21104 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21105 return token->u.value;
21108 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21109 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21111 cp_parser_error (parser,
21112 "invalid pure specifier (only %<= 0%> is allowed)");
21113 cp_parser_skip_to_end_of_statement (parser);
21114 return error_mark_node;
21116 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21118 error_at (token->location, "templates may not be %<virtual%>");
21119 return error_mark_node;
21122 return integer_zero_node;
21125 /* Parse a constant-initializer.
21127 constant-initializer:
21128 = constant-expression
21130 Returns a representation of the constant-expression. */
21132 static tree
21133 cp_parser_constant_initializer (cp_parser* parser)
21135 /* Look for the `=' token. */
21136 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21137 return error_mark_node;
21139 /* It is invalid to write:
21141 struct S { static const int i = { 7 }; };
21144 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21146 cp_parser_error (parser,
21147 "a brace-enclosed initializer is not allowed here");
21148 /* Consume the opening brace. */
21149 cp_lexer_consume_token (parser->lexer);
21150 /* Skip the initializer. */
21151 cp_parser_skip_to_closing_brace (parser);
21152 /* Look for the trailing `}'. */
21153 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21155 return error_mark_node;
21158 return cp_parser_constant_expression (parser);
21161 /* Derived classes [gram.class.derived] */
21163 /* Parse a base-clause.
21165 base-clause:
21166 : base-specifier-list
21168 base-specifier-list:
21169 base-specifier ... [opt]
21170 base-specifier-list , base-specifier ... [opt]
21172 Returns a TREE_LIST representing the base-classes, in the order in
21173 which they were declared. The representation of each node is as
21174 described by cp_parser_base_specifier.
21176 In the case that no bases are specified, this function will return
21177 NULL_TREE, not ERROR_MARK_NODE. */
21179 static tree
21180 cp_parser_base_clause (cp_parser* parser)
21182 tree bases = NULL_TREE;
21184 /* Look for the `:' that begins the list. */
21185 cp_parser_require (parser, CPP_COLON, RT_COLON);
21187 /* Scan the base-specifier-list. */
21188 while (true)
21190 cp_token *token;
21191 tree base;
21192 bool pack_expansion_p = false;
21194 /* Look for the base-specifier. */
21195 base = cp_parser_base_specifier (parser);
21196 /* Look for the (optional) ellipsis. */
21197 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21199 /* Consume the `...'. */
21200 cp_lexer_consume_token (parser->lexer);
21202 pack_expansion_p = true;
21205 /* Add BASE to the front of the list. */
21206 if (base && base != error_mark_node)
21208 if (pack_expansion_p)
21209 /* Make this a pack expansion type. */
21210 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21212 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21214 TREE_CHAIN (base) = bases;
21215 bases = base;
21218 /* Peek at the next token. */
21219 token = cp_lexer_peek_token (parser->lexer);
21220 /* If it's not a comma, then the list is complete. */
21221 if (token->type != CPP_COMMA)
21222 break;
21223 /* Consume the `,'. */
21224 cp_lexer_consume_token (parser->lexer);
21227 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21228 base class had a qualified name. However, the next name that
21229 appears is certainly not qualified. */
21230 parser->scope = NULL_TREE;
21231 parser->qualifying_scope = NULL_TREE;
21232 parser->object_scope = NULL_TREE;
21234 return nreverse (bases);
21237 /* Parse a base-specifier.
21239 base-specifier:
21240 :: [opt] nested-name-specifier [opt] class-name
21241 virtual access-specifier [opt] :: [opt] nested-name-specifier
21242 [opt] class-name
21243 access-specifier virtual [opt] :: [opt] nested-name-specifier
21244 [opt] class-name
21246 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21247 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21248 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21249 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21251 static tree
21252 cp_parser_base_specifier (cp_parser* parser)
21254 cp_token *token;
21255 bool done = false;
21256 bool virtual_p = false;
21257 bool duplicate_virtual_error_issued_p = false;
21258 bool duplicate_access_error_issued_p = false;
21259 bool class_scope_p, template_p;
21260 tree access = access_default_node;
21261 tree type;
21263 /* Process the optional `virtual' and `access-specifier'. */
21264 while (!done)
21266 /* Peek at the next token. */
21267 token = cp_lexer_peek_token (parser->lexer);
21268 /* Process `virtual'. */
21269 switch (token->keyword)
21271 case RID_VIRTUAL:
21272 /* If `virtual' appears more than once, issue an error. */
21273 if (virtual_p && !duplicate_virtual_error_issued_p)
21275 cp_parser_error (parser,
21276 "%<virtual%> specified more than once in base-specified");
21277 duplicate_virtual_error_issued_p = true;
21280 virtual_p = true;
21282 /* Consume the `virtual' token. */
21283 cp_lexer_consume_token (parser->lexer);
21285 break;
21287 case RID_PUBLIC:
21288 case RID_PROTECTED:
21289 case RID_PRIVATE:
21290 /* If more than one access specifier appears, issue an
21291 error. */
21292 if (access != access_default_node
21293 && !duplicate_access_error_issued_p)
21295 cp_parser_error (parser,
21296 "more than one access specifier in base-specified");
21297 duplicate_access_error_issued_p = true;
21300 access = ridpointers[(int) token->keyword];
21302 /* Consume the access-specifier. */
21303 cp_lexer_consume_token (parser->lexer);
21305 break;
21307 default:
21308 done = true;
21309 break;
21312 /* It is not uncommon to see programs mechanically, erroneously, use
21313 the 'typename' keyword to denote (dependent) qualified types
21314 as base classes. */
21315 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21317 token = cp_lexer_peek_token (parser->lexer);
21318 if (!processing_template_decl)
21319 error_at (token->location,
21320 "keyword %<typename%> not allowed outside of templates");
21321 else
21322 error_at (token->location,
21323 "keyword %<typename%> not allowed in this context "
21324 "(the base class is implicitly a type)");
21325 cp_lexer_consume_token (parser->lexer);
21328 /* Look for the optional `::' operator. */
21329 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21330 /* Look for the nested-name-specifier. The simplest way to
21331 implement:
21333 [temp.res]
21335 The keyword `typename' is not permitted in a base-specifier or
21336 mem-initializer; in these contexts a qualified name that
21337 depends on a template-parameter is implicitly assumed to be a
21338 type name.
21340 is to pretend that we have seen the `typename' keyword at this
21341 point. */
21342 cp_parser_nested_name_specifier_opt (parser,
21343 /*typename_keyword_p=*/true,
21344 /*check_dependency_p=*/true,
21345 typename_type,
21346 /*is_declaration=*/true);
21347 /* If the base class is given by a qualified name, assume that names
21348 we see are type names or templates, as appropriate. */
21349 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21350 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21352 if (!parser->scope
21353 && cp_lexer_next_token_is_decltype (parser->lexer))
21354 /* DR 950 allows decltype as a base-specifier. */
21355 type = cp_parser_decltype (parser);
21356 else
21358 /* Otherwise, look for the class-name. */
21359 type = cp_parser_class_name (parser,
21360 class_scope_p,
21361 template_p,
21362 typename_type,
21363 /*check_dependency_p=*/true,
21364 /*class_head_p=*/false,
21365 /*is_declaration=*/true);
21366 type = TREE_TYPE (type);
21369 if (type == error_mark_node)
21370 return error_mark_node;
21372 return finish_base_specifier (type, access, virtual_p);
21375 /* Exception handling [gram.exception] */
21377 /* Parse an (optional) noexcept-specification.
21379 noexcept-specification:
21380 noexcept ( constant-expression ) [opt]
21382 If no noexcept-specification is present, returns NULL_TREE.
21383 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21384 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21385 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21386 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21387 in which case a boolean condition is returned instead. */
21389 static tree
21390 cp_parser_noexcept_specification_opt (cp_parser* parser,
21391 bool require_constexpr,
21392 bool* consumed_expr,
21393 bool return_cond)
21395 cp_token *token;
21396 const char *saved_message;
21398 /* Peek at the next token. */
21399 token = cp_lexer_peek_token (parser->lexer);
21401 /* Is it a noexcept-specification? */
21402 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21404 tree expr;
21405 cp_lexer_consume_token (parser->lexer);
21407 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21409 cp_lexer_consume_token (parser->lexer);
21411 if (require_constexpr)
21413 /* Types may not be defined in an exception-specification. */
21414 saved_message = parser->type_definition_forbidden_message;
21415 parser->type_definition_forbidden_message
21416 = G_("types may not be defined in an exception-specification");
21418 expr = cp_parser_constant_expression (parser);
21420 /* Restore the saved message. */
21421 parser->type_definition_forbidden_message = saved_message;
21423 else
21425 expr = cp_parser_expression (parser);
21426 *consumed_expr = true;
21429 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21431 else
21433 expr = boolean_true_node;
21434 if (!require_constexpr)
21435 *consumed_expr = false;
21438 /* We cannot build a noexcept-spec right away because this will check
21439 that expr is a constexpr. */
21440 if (!return_cond)
21441 return build_noexcept_spec (expr, tf_warning_or_error);
21442 else
21443 return expr;
21445 else
21446 return NULL_TREE;
21449 /* Parse an (optional) exception-specification.
21451 exception-specification:
21452 throw ( type-id-list [opt] )
21454 Returns a TREE_LIST representing the exception-specification. The
21455 TREE_VALUE of each node is a type. */
21457 static tree
21458 cp_parser_exception_specification_opt (cp_parser* parser)
21460 cp_token *token;
21461 tree type_id_list;
21462 const char *saved_message;
21464 /* Peek at the next token. */
21465 token = cp_lexer_peek_token (parser->lexer);
21467 /* Is it a noexcept-specification? */
21468 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21469 false);
21470 if (type_id_list != NULL_TREE)
21471 return type_id_list;
21473 /* If it's not `throw', then there's no exception-specification. */
21474 if (!cp_parser_is_keyword (token, RID_THROW))
21475 return NULL_TREE;
21477 #if 0
21478 /* Enable this once a lot of code has transitioned to noexcept? */
21479 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21480 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21481 "deprecated in C++0x; use %<noexcept%> instead");
21482 #endif
21484 /* Consume the `throw'. */
21485 cp_lexer_consume_token (parser->lexer);
21487 /* Look for the `('. */
21488 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21490 /* Peek at the next token. */
21491 token = cp_lexer_peek_token (parser->lexer);
21492 /* If it's not a `)', then there is a type-id-list. */
21493 if (token->type != CPP_CLOSE_PAREN)
21495 /* Types may not be defined in an exception-specification. */
21496 saved_message = parser->type_definition_forbidden_message;
21497 parser->type_definition_forbidden_message
21498 = G_("types may not be defined in an exception-specification");
21499 /* Parse the type-id-list. */
21500 type_id_list = cp_parser_type_id_list (parser);
21501 /* Restore the saved message. */
21502 parser->type_definition_forbidden_message = saved_message;
21504 else
21505 type_id_list = empty_except_spec;
21507 /* Look for the `)'. */
21508 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21510 return type_id_list;
21513 /* Parse an (optional) type-id-list.
21515 type-id-list:
21516 type-id ... [opt]
21517 type-id-list , type-id ... [opt]
21519 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21520 in the order that the types were presented. */
21522 static tree
21523 cp_parser_type_id_list (cp_parser* parser)
21525 tree types = NULL_TREE;
21527 while (true)
21529 cp_token *token;
21530 tree type;
21532 /* Get the next type-id. */
21533 type = cp_parser_type_id (parser);
21534 /* Parse the optional ellipsis. */
21535 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21537 /* Consume the `...'. */
21538 cp_lexer_consume_token (parser->lexer);
21540 /* Turn the type into a pack expansion expression. */
21541 type = make_pack_expansion (type);
21543 /* Add it to the list. */
21544 types = add_exception_specifier (types, type, /*complain=*/1);
21545 /* Peek at the next token. */
21546 token = cp_lexer_peek_token (parser->lexer);
21547 /* If it is not a `,', we are done. */
21548 if (token->type != CPP_COMMA)
21549 break;
21550 /* Consume the `,'. */
21551 cp_lexer_consume_token (parser->lexer);
21554 return nreverse (types);
21557 /* Parse a try-block.
21559 try-block:
21560 try compound-statement handler-seq */
21562 static tree
21563 cp_parser_try_block (cp_parser* parser)
21565 tree try_block;
21567 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21568 if (parser->in_function_body
21569 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21570 error ("%<try%> in %<constexpr%> function");
21572 try_block = begin_try_block ();
21573 cp_parser_compound_statement (parser, NULL, true, false);
21574 finish_try_block (try_block);
21575 cp_parser_handler_seq (parser);
21576 finish_handler_sequence (try_block);
21578 return try_block;
21581 /* Parse a function-try-block.
21583 function-try-block:
21584 try ctor-initializer [opt] function-body handler-seq */
21586 static bool
21587 cp_parser_function_try_block (cp_parser* parser)
21589 tree compound_stmt;
21590 tree try_block;
21591 bool ctor_initializer_p;
21593 /* Look for the `try' keyword. */
21594 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21595 return false;
21596 /* Let the rest of the front end know where we are. */
21597 try_block = begin_function_try_block (&compound_stmt);
21598 /* Parse the function-body. */
21599 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21600 (parser, /*in_function_try_block=*/true);
21601 /* We're done with the `try' part. */
21602 finish_function_try_block (try_block);
21603 /* Parse the handlers. */
21604 cp_parser_handler_seq (parser);
21605 /* We're done with the handlers. */
21606 finish_function_handler_sequence (try_block, compound_stmt);
21608 return ctor_initializer_p;
21611 /* Parse a handler-seq.
21613 handler-seq:
21614 handler handler-seq [opt] */
21616 static void
21617 cp_parser_handler_seq (cp_parser* parser)
21619 while (true)
21621 cp_token *token;
21623 /* Parse the handler. */
21624 cp_parser_handler (parser);
21625 /* Peek at the next token. */
21626 token = cp_lexer_peek_token (parser->lexer);
21627 /* If it's not `catch' then there are no more handlers. */
21628 if (!cp_parser_is_keyword (token, RID_CATCH))
21629 break;
21633 /* Parse a handler.
21635 handler:
21636 catch ( exception-declaration ) compound-statement */
21638 static void
21639 cp_parser_handler (cp_parser* parser)
21641 tree handler;
21642 tree declaration;
21644 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21645 handler = begin_handler ();
21646 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21647 declaration = cp_parser_exception_declaration (parser);
21648 finish_handler_parms (declaration, handler);
21649 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21650 cp_parser_compound_statement (parser, NULL, false, false);
21651 finish_handler (handler);
21654 /* Parse an exception-declaration.
21656 exception-declaration:
21657 type-specifier-seq declarator
21658 type-specifier-seq abstract-declarator
21659 type-specifier-seq
21662 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21663 ellipsis variant is used. */
21665 static tree
21666 cp_parser_exception_declaration (cp_parser* parser)
21668 cp_decl_specifier_seq type_specifiers;
21669 cp_declarator *declarator;
21670 const char *saved_message;
21672 /* If it's an ellipsis, it's easy to handle. */
21673 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21675 /* Consume the `...' token. */
21676 cp_lexer_consume_token (parser->lexer);
21677 return NULL_TREE;
21680 /* Types may not be defined in exception-declarations. */
21681 saved_message = parser->type_definition_forbidden_message;
21682 parser->type_definition_forbidden_message
21683 = G_("types may not be defined in exception-declarations");
21685 /* Parse the type-specifier-seq. */
21686 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21687 /*is_trailing_return=*/false,
21688 &type_specifiers);
21689 /* If it's a `)', then there is no declarator. */
21690 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21691 declarator = NULL;
21692 else
21693 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21694 /*ctor_dtor_or_conv_p=*/NULL,
21695 /*parenthesized_p=*/NULL,
21696 /*member_p=*/false,
21697 /*friend_p=*/false);
21699 /* Restore the saved message. */
21700 parser->type_definition_forbidden_message = saved_message;
21702 if (!type_specifiers.any_specifiers_p)
21703 return error_mark_node;
21705 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21708 /* Parse a throw-expression.
21710 throw-expression:
21711 throw assignment-expression [opt]
21713 Returns a THROW_EXPR representing the throw-expression. */
21715 static tree
21716 cp_parser_throw_expression (cp_parser* parser)
21718 tree expression;
21719 cp_token* token;
21721 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21722 token = cp_lexer_peek_token (parser->lexer);
21723 /* Figure out whether or not there is an assignment-expression
21724 following the "throw" keyword. */
21725 if (token->type == CPP_COMMA
21726 || token->type == CPP_SEMICOLON
21727 || token->type == CPP_CLOSE_PAREN
21728 || token->type == CPP_CLOSE_SQUARE
21729 || token->type == CPP_CLOSE_BRACE
21730 || token->type == CPP_COLON)
21731 expression = NULL_TREE;
21732 else
21733 expression = cp_parser_assignment_expression (parser);
21735 return build_throw (expression);
21738 /* GNU Extensions */
21740 /* Parse an (optional) asm-specification.
21742 asm-specification:
21743 asm ( string-literal )
21745 If the asm-specification is present, returns a STRING_CST
21746 corresponding to the string-literal. Otherwise, returns
21747 NULL_TREE. */
21749 static tree
21750 cp_parser_asm_specification_opt (cp_parser* parser)
21752 cp_token *token;
21753 tree asm_specification;
21755 /* Peek at the next token. */
21756 token = cp_lexer_peek_token (parser->lexer);
21757 /* If the next token isn't the `asm' keyword, then there's no
21758 asm-specification. */
21759 if (!cp_parser_is_keyword (token, RID_ASM))
21760 return NULL_TREE;
21762 /* Consume the `asm' token. */
21763 cp_lexer_consume_token (parser->lexer);
21764 /* Look for the `('. */
21765 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21767 /* Look for the string-literal. */
21768 asm_specification = cp_parser_string_literal (parser, false, false);
21770 /* Look for the `)'. */
21771 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21773 return asm_specification;
21776 /* Parse an asm-operand-list.
21778 asm-operand-list:
21779 asm-operand
21780 asm-operand-list , asm-operand
21782 asm-operand:
21783 string-literal ( expression )
21784 [ string-literal ] string-literal ( expression )
21786 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21787 each node is the expression. The TREE_PURPOSE is itself a
21788 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21789 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21790 is a STRING_CST for the string literal before the parenthesis. Returns
21791 ERROR_MARK_NODE if any of the operands are invalid. */
21793 static tree
21794 cp_parser_asm_operand_list (cp_parser* parser)
21796 tree asm_operands = NULL_TREE;
21797 bool invalid_operands = false;
21799 while (true)
21801 tree string_literal;
21802 tree expression;
21803 tree name;
21805 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21807 /* Consume the `[' token. */
21808 cp_lexer_consume_token (parser->lexer);
21809 /* Read the operand name. */
21810 name = cp_parser_identifier (parser);
21811 if (name != error_mark_node)
21812 name = build_string (IDENTIFIER_LENGTH (name),
21813 IDENTIFIER_POINTER (name));
21814 /* Look for the closing `]'. */
21815 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21817 else
21818 name = NULL_TREE;
21819 /* Look for the string-literal. */
21820 string_literal = cp_parser_string_literal (parser, false, false);
21822 /* Look for the `('. */
21823 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21824 /* Parse the expression. */
21825 expression = cp_parser_expression (parser);
21826 /* Look for the `)'. */
21827 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21829 if (name == error_mark_node
21830 || string_literal == error_mark_node
21831 || expression == error_mark_node)
21832 invalid_operands = true;
21834 /* Add this operand to the list. */
21835 asm_operands = tree_cons (build_tree_list (name, string_literal),
21836 expression,
21837 asm_operands);
21838 /* If the next token is not a `,', there are no more
21839 operands. */
21840 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21841 break;
21842 /* Consume the `,'. */
21843 cp_lexer_consume_token (parser->lexer);
21846 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21849 /* Parse an asm-clobber-list.
21851 asm-clobber-list:
21852 string-literal
21853 asm-clobber-list , string-literal
21855 Returns a TREE_LIST, indicating the clobbers in the order that they
21856 appeared. The TREE_VALUE of each node is a STRING_CST. */
21858 static tree
21859 cp_parser_asm_clobber_list (cp_parser* parser)
21861 tree clobbers = NULL_TREE;
21863 while (true)
21865 tree string_literal;
21867 /* Look for the string literal. */
21868 string_literal = cp_parser_string_literal (parser, false, false);
21869 /* Add it to the list. */
21870 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21871 /* If the next token is not a `,', then the list is
21872 complete. */
21873 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21874 break;
21875 /* Consume the `,' token. */
21876 cp_lexer_consume_token (parser->lexer);
21879 return clobbers;
21882 /* Parse an asm-label-list.
21884 asm-label-list:
21885 identifier
21886 asm-label-list , identifier
21888 Returns a TREE_LIST, indicating the labels in the order that they
21889 appeared. The TREE_VALUE of each node is a label. */
21891 static tree
21892 cp_parser_asm_label_list (cp_parser* parser)
21894 tree labels = NULL_TREE;
21896 while (true)
21898 tree identifier, label, name;
21900 /* Look for the identifier. */
21901 identifier = cp_parser_identifier (parser);
21902 if (!error_operand_p (identifier))
21904 label = lookup_label (identifier);
21905 if (TREE_CODE (label) == LABEL_DECL)
21907 TREE_USED (label) = 1;
21908 check_goto (label);
21909 name = build_string (IDENTIFIER_LENGTH (identifier),
21910 IDENTIFIER_POINTER (identifier));
21911 labels = tree_cons (name, label, labels);
21914 /* If the next token is not a `,', then the list is
21915 complete. */
21916 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21917 break;
21918 /* Consume the `,' token. */
21919 cp_lexer_consume_token (parser->lexer);
21922 return nreverse (labels);
21925 /* Return TRUE iff the next tokens in the stream are possibly the
21926 beginning of a GNU extension attribute. */
21928 static bool
21929 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21931 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21934 /* Return TRUE iff the next tokens in the stream are possibly the
21935 beginning of a standard C++-11 attribute specifier. */
21937 static bool
21938 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21940 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21943 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21944 beginning of a standard C++-11 attribute specifier. */
21946 static bool
21947 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21949 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21951 return (cxx_dialect >= cxx11
21952 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21953 || (token->type == CPP_OPEN_SQUARE
21954 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21955 && token->type == CPP_OPEN_SQUARE)));
21958 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21959 beginning of a GNU extension attribute. */
21961 static bool
21962 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21964 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21966 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21969 /* Return true iff the next tokens can be the beginning of either a
21970 GNU attribute list, or a standard C++11 attribute sequence. */
21972 static bool
21973 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21975 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21976 || cp_next_tokens_can_be_std_attribute_p (parser));
21979 /* Return true iff the next Nth tokens can be the beginning of either
21980 a GNU attribute list, or a standard C++11 attribute sequence. */
21982 static bool
21983 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21985 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21986 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21989 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21990 of GNU attributes, or return NULL. */
21992 static tree
21993 cp_parser_attributes_opt (cp_parser *parser)
21995 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21996 return cp_parser_gnu_attributes_opt (parser);
21997 return cp_parser_std_attribute_spec_seq (parser);
22000 #define CILK_SIMD_FN_CLAUSE_MASK \
22001 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22002 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22003 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22004 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22005 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22007 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22008 vector [(<clauses>)] */
22010 static void
22011 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22013 bool first_p = parser->cilk_simd_fn_info == NULL;
22014 cp_token *token = v_token;
22015 if (first_p)
22017 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22018 parser->cilk_simd_fn_info->error_seen = false;
22019 parser->cilk_simd_fn_info->fndecl_seen = false;
22020 parser->cilk_simd_fn_info->tokens = vNULL;
22022 int paren_scope = 0;
22023 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22025 cp_lexer_consume_token (parser->lexer);
22026 v_token = cp_lexer_peek_token (parser->lexer);
22027 paren_scope++;
22029 while (paren_scope > 0)
22031 token = cp_lexer_peek_token (parser->lexer);
22032 if (token->type == CPP_OPEN_PAREN)
22033 paren_scope++;
22034 else if (token->type == CPP_CLOSE_PAREN)
22035 paren_scope--;
22036 /* Do not push the last ')' */
22037 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22038 cp_lexer_consume_token (parser->lexer);
22041 token->type = CPP_PRAGMA_EOL;
22042 parser->lexer->next_token = token;
22043 cp_lexer_consume_token (parser->lexer);
22045 struct cp_token_cache *cp
22046 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22047 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22050 /* Parse an (optional) series of attributes.
22052 attributes:
22053 attributes attribute
22055 attribute:
22056 __attribute__ (( attribute-list [opt] ))
22058 The return value is as for cp_parser_gnu_attribute_list. */
22060 static tree
22061 cp_parser_gnu_attributes_opt (cp_parser* parser)
22063 tree attributes = NULL_TREE;
22065 while (true)
22067 cp_token *token;
22068 tree attribute_list;
22069 bool ok = true;
22071 /* Peek at the next token. */
22072 token = cp_lexer_peek_token (parser->lexer);
22073 /* If it's not `__attribute__', then we're done. */
22074 if (token->keyword != RID_ATTRIBUTE)
22075 break;
22077 /* Consume the `__attribute__' keyword. */
22078 cp_lexer_consume_token (parser->lexer);
22079 /* Look for the two `(' tokens. */
22080 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22081 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22083 /* Peek at the next token. */
22084 token = cp_lexer_peek_token (parser->lexer);
22085 if (token->type != CPP_CLOSE_PAREN)
22086 /* Parse the attribute-list. */
22087 attribute_list = cp_parser_gnu_attribute_list (parser);
22088 else
22089 /* If the next token is a `)', then there is no attribute
22090 list. */
22091 attribute_list = NULL;
22093 /* Look for the two `)' tokens. */
22094 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22095 ok = false;
22096 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22097 ok = false;
22098 if (!ok)
22099 cp_parser_skip_to_end_of_statement (parser);
22101 /* Add these new attributes to the list. */
22102 attributes = chainon (attributes, attribute_list);
22105 return attributes;
22108 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22109 "__vector" or "__vector__." */
22111 static inline bool
22112 is_cilkplus_vector_p (tree name)
22114 if (flag_cilkplus && is_attribute_p ("vector", name))
22115 return true;
22116 return false;
22119 /* Parse a GNU attribute-list.
22121 attribute-list:
22122 attribute
22123 attribute-list , attribute
22125 attribute:
22126 identifier
22127 identifier ( identifier )
22128 identifier ( identifier , expression-list )
22129 identifier ( expression-list )
22131 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22132 to an attribute. The TREE_PURPOSE of each node is the identifier
22133 indicating which attribute is in use. The TREE_VALUE represents
22134 the arguments, if any. */
22136 static tree
22137 cp_parser_gnu_attribute_list (cp_parser* parser)
22139 tree attribute_list = NULL_TREE;
22140 bool save_translate_strings_p = parser->translate_strings_p;
22142 parser->translate_strings_p = false;
22143 while (true)
22145 cp_token *token;
22146 tree identifier;
22147 tree attribute;
22149 /* Look for the identifier. We also allow keywords here; for
22150 example `__attribute__ ((const))' is legal. */
22151 token = cp_lexer_peek_token (parser->lexer);
22152 if (token->type == CPP_NAME
22153 || token->type == CPP_KEYWORD)
22155 tree arguments = NULL_TREE;
22157 /* Consume the token, but save it since we need it for the
22158 SIMD enabled function parsing. */
22159 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22161 /* Save away the identifier that indicates which attribute
22162 this is. */
22163 identifier = (token->type == CPP_KEYWORD)
22164 /* For keywords, use the canonical spelling, not the
22165 parsed identifier. */
22166 ? ridpointers[(int) token->keyword]
22167 : id_token->u.value;
22169 attribute = build_tree_list (identifier, NULL_TREE);
22171 /* Peek at the next token. */
22172 token = cp_lexer_peek_token (parser->lexer);
22173 /* If it's an `(', then parse the attribute arguments. */
22174 if (token->type == CPP_OPEN_PAREN)
22176 vec<tree, va_gc> *vec;
22177 int attr_flag = (attribute_takes_identifier_p (identifier)
22178 ? id_attr : normal_attr);
22179 if (is_cilkplus_vector_p (identifier))
22181 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22182 continue;
22184 else
22185 vec = cp_parser_parenthesized_expression_list
22186 (parser, attr_flag, /*cast_p=*/false,
22187 /*allow_expansion_p=*/false,
22188 /*non_constant_p=*/NULL);
22189 if (vec == NULL)
22190 arguments = error_mark_node;
22191 else
22193 arguments = build_tree_list_vec (vec);
22194 release_tree_vector (vec);
22196 /* Save the arguments away. */
22197 TREE_VALUE (attribute) = arguments;
22199 else if (is_cilkplus_vector_p (identifier))
22201 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22202 continue;
22205 if (arguments != error_mark_node)
22207 /* Add this attribute to the list. */
22208 TREE_CHAIN (attribute) = attribute_list;
22209 attribute_list = attribute;
22212 token = cp_lexer_peek_token (parser->lexer);
22214 /* Now, look for more attributes. If the next token isn't a
22215 `,', we're done. */
22216 if (token->type != CPP_COMMA)
22217 break;
22219 /* Consume the comma and keep going. */
22220 cp_lexer_consume_token (parser->lexer);
22222 parser->translate_strings_p = save_translate_strings_p;
22224 /* We built up the list in reverse order. */
22225 return nreverse (attribute_list);
22228 /* Parse a standard C++11 attribute.
22230 The returned representation is a TREE_LIST which TREE_PURPOSE is
22231 the scoped name of the attribute, and the TREE_VALUE is its
22232 arguments list.
22234 Note that the scoped name of the attribute is itself a TREE_LIST
22235 which TREE_PURPOSE is the namespace of the attribute, and
22236 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22237 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22238 and which TREE_PURPOSE is directly the attribute name.
22240 Clients of the attribute code should use get_attribute_namespace
22241 and get_attribute_name to get the actual namespace and name of
22242 attributes, regardless of their being GNU or C++11 attributes.
22244 attribute:
22245 attribute-token attribute-argument-clause [opt]
22247 attribute-token:
22248 identifier
22249 attribute-scoped-token
22251 attribute-scoped-token:
22252 attribute-namespace :: identifier
22254 attribute-namespace:
22255 identifier
22257 attribute-argument-clause:
22258 ( balanced-token-seq )
22260 balanced-token-seq:
22261 balanced-token [opt]
22262 balanced-token-seq balanced-token
22264 balanced-token:
22265 ( balanced-token-seq )
22266 [ balanced-token-seq ]
22267 { balanced-token-seq }. */
22269 static tree
22270 cp_parser_std_attribute (cp_parser *parser)
22272 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22273 cp_token *token;
22275 /* First, parse name of the the attribute, a.k.a
22276 attribute-token. */
22278 token = cp_lexer_peek_token (parser->lexer);
22279 if (token->type == CPP_NAME)
22280 attr_id = token->u.value;
22281 else if (token->type == CPP_KEYWORD)
22282 attr_id = ridpointers[(int) token->keyword];
22283 else if (token->flags & NAMED_OP)
22284 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22286 if (attr_id == NULL_TREE)
22287 return NULL_TREE;
22289 cp_lexer_consume_token (parser->lexer);
22291 token = cp_lexer_peek_token (parser->lexer);
22292 if (token->type == CPP_SCOPE)
22294 /* We are seeing a scoped attribute token. */
22296 cp_lexer_consume_token (parser->lexer);
22297 attr_ns = attr_id;
22299 token = cp_lexer_consume_token (parser->lexer);
22300 if (token->type == CPP_NAME)
22301 attr_id = token->u.value;
22302 else if (token->type == CPP_KEYWORD)
22303 attr_id = ridpointers[(int) token->keyword];
22304 else
22306 error_at (token->location,
22307 "expected an identifier for the attribute name");
22308 return error_mark_node;
22310 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22311 NULL_TREE);
22312 token = cp_lexer_peek_token (parser->lexer);
22314 else
22316 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22317 NULL_TREE);
22318 /* C++11 noreturn attribute is equivalent to GNU's. */
22319 if (is_attribute_p ("noreturn", attr_id))
22320 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22321 /* C++14 deprecated attribute is equivalent to GNU's. */
22322 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22324 if (cxx_dialect == cxx11)
22325 pedwarn (token->location, OPT_Wpedantic,
22326 "%<deprecated%> is a C++14 feature;"
22327 " use %<gnu::deprecated%>");
22328 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22332 /* Now parse the optional argument clause of the attribute. */
22334 if (token->type != CPP_OPEN_PAREN)
22335 return attribute;
22338 vec<tree, va_gc> *vec;
22339 int attr_flag = normal_attr;
22341 if (attr_ns == get_identifier ("gnu")
22342 && attribute_takes_identifier_p (attr_id))
22343 /* A GNU attribute that takes an identifier in parameter. */
22344 attr_flag = id_attr;
22346 vec = cp_parser_parenthesized_expression_list
22347 (parser, attr_flag, /*cast_p=*/false,
22348 /*allow_expansion_p=*/true,
22349 /*non_constant_p=*/NULL);
22350 if (vec == NULL)
22351 arguments = error_mark_node;
22352 else
22354 arguments = build_tree_list_vec (vec);
22355 release_tree_vector (vec);
22358 if (arguments == error_mark_node)
22359 attribute = error_mark_node;
22360 else
22361 TREE_VALUE (attribute) = arguments;
22364 return attribute;
22367 /* Parse a list of standard C++-11 attributes.
22369 attribute-list:
22370 attribute [opt]
22371 attribute-list , attribute[opt]
22372 attribute ...
22373 attribute-list , attribute ...
22376 static tree
22377 cp_parser_std_attribute_list (cp_parser *parser)
22379 tree attributes = NULL_TREE, attribute = NULL_TREE;
22380 cp_token *token = NULL;
22382 while (true)
22384 attribute = cp_parser_std_attribute (parser);
22385 if (attribute == error_mark_node)
22386 break;
22387 if (attribute != NULL_TREE)
22389 TREE_CHAIN (attribute) = attributes;
22390 attributes = attribute;
22392 token = cp_lexer_peek_token (parser->lexer);
22393 if (token->type != CPP_COMMA)
22394 break;
22395 cp_lexer_consume_token (parser->lexer);
22397 attributes = nreverse (attributes);
22398 return attributes;
22401 /* Parse a standard C++-11 attribute specifier.
22403 attribute-specifier:
22404 [ [ attribute-list ] ]
22405 alignment-specifier
22407 alignment-specifier:
22408 alignas ( type-id ... [opt] )
22409 alignas ( alignment-expression ... [opt] ). */
22411 static tree
22412 cp_parser_std_attribute_spec (cp_parser *parser)
22414 tree attributes = NULL_TREE;
22415 cp_token *token = cp_lexer_peek_token (parser->lexer);
22417 if (token->type == CPP_OPEN_SQUARE
22418 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22420 cp_lexer_consume_token (parser->lexer);
22421 cp_lexer_consume_token (parser->lexer);
22423 attributes = cp_parser_std_attribute_list (parser);
22425 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22426 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22427 cp_parser_skip_to_end_of_statement (parser);
22428 else
22429 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22430 when we are sure that we have actually parsed them. */
22431 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22433 else
22435 tree alignas_expr;
22437 /* Look for an alignment-specifier. */
22439 token = cp_lexer_peek_token (parser->lexer);
22441 if (token->type != CPP_KEYWORD
22442 || token->keyword != RID_ALIGNAS)
22443 return NULL_TREE;
22445 cp_lexer_consume_token (parser->lexer);
22446 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22448 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22450 cp_parser_error (parser, "expected %<(%>");
22451 return error_mark_node;
22454 cp_parser_parse_tentatively (parser);
22455 alignas_expr = cp_parser_type_id (parser);
22457 if (!cp_parser_parse_definitely (parser))
22459 gcc_assert (alignas_expr == error_mark_node
22460 || alignas_expr == NULL_TREE);
22462 alignas_expr =
22463 cp_parser_assignment_expression (parser);
22464 if (alignas_expr == error_mark_node)
22465 cp_parser_skip_to_end_of_statement (parser);
22466 if (alignas_expr == NULL_TREE
22467 || alignas_expr == error_mark_node)
22468 return alignas_expr;
22471 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22473 cp_parser_error (parser, "expected %<)%>");
22474 return error_mark_node;
22477 alignas_expr = cxx_alignas_expr (alignas_expr);
22479 /* Build the C++-11 representation of an 'aligned'
22480 attribute. */
22481 attributes =
22482 build_tree_list (build_tree_list (get_identifier ("gnu"),
22483 get_identifier ("aligned")),
22484 build_tree_list (NULL_TREE, alignas_expr));
22487 return attributes;
22490 /* Parse a standard C++-11 attribute-specifier-seq.
22492 attribute-specifier-seq:
22493 attribute-specifier-seq [opt] attribute-specifier
22496 static tree
22497 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22499 tree attr_specs = NULL;
22501 while (true)
22503 tree attr_spec = cp_parser_std_attribute_spec (parser);
22504 if (attr_spec == NULL_TREE)
22505 break;
22506 if (attr_spec == error_mark_node)
22507 return error_mark_node;
22509 TREE_CHAIN (attr_spec) = attr_specs;
22510 attr_specs = attr_spec;
22513 attr_specs = nreverse (attr_specs);
22514 return attr_specs;
22517 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22518 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22519 current value of the PEDANTIC flag, regardless of whether or not
22520 the `__extension__' keyword is present. The caller is responsible
22521 for restoring the value of the PEDANTIC flag. */
22523 static bool
22524 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22526 /* Save the old value of the PEDANTIC flag. */
22527 *saved_pedantic = pedantic;
22529 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22531 /* Consume the `__extension__' token. */
22532 cp_lexer_consume_token (parser->lexer);
22533 /* We're not being pedantic while the `__extension__' keyword is
22534 in effect. */
22535 pedantic = 0;
22537 return true;
22540 return false;
22543 /* Parse a label declaration.
22545 label-declaration:
22546 __label__ label-declarator-seq ;
22548 label-declarator-seq:
22549 identifier , label-declarator-seq
22550 identifier */
22552 static void
22553 cp_parser_label_declaration (cp_parser* parser)
22555 /* Look for the `__label__' keyword. */
22556 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22558 while (true)
22560 tree identifier;
22562 /* Look for an identifier. */
22563 identifier = cp_parser_identifier (parser);
22564 /* If we failed, stop. */
22565 if (identifier == error_mark_node)
22566 break;
22567 /* Declare it as a label. */
22568 finish_label_decl (identifier);
22569 /* If the next token is a `;', stop. */
22570 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22571 break;
22572 /* Look for the `,' separating the label declarations. */
22573 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22576 /* Look for the final `;'. */
22577 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22580 /* Support Functions */
22582 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22583 NAME should have one of the representations used for an
22584 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22585 is returned. If PARSER->SCOPE is a dependent type, then a
22586 SCOPE_REF is returned.
22588 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22589 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22590 was formed. Abstractly, such entities should not be passed to this
22591 function, because they do not need to be looked up, but it is
22592 simpler to check for this special case here, rather than at the
22593 call-sites.
22595 In cases not explicitly covered above, this function returns a
22596 DECL, OVERLOAD, or baselink representing the result of the lookup.
22597 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22598 is returned.
22600 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22601 (e.g., "struct") that was used. In that case bindings that do not
22602 refer to types are ignored.
22604 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22605 ignored.
22607 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22608 are ignored.
22610 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22611 types.
22613 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22614 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22615 NULL_TREE otherwise. */
22617 static tree
22618 cp_parser_lookup_name (cp_parser *parser, tree name,
22619 enum tag_types tag_type,
22620 bool is_template,
22621 bool is_namespace,
22622 bool check_dependency,
22623 tree *ambiguous_decls,
22624 location_t name_location)
22626 tree decl;
22627 tree object_type = parser->context->object_type;
22629 /* Assume that the lookup will be unambiguous. */
22630 if (ambiguous_decls)
22631 *ambiguous_decls = NULL_TREE;
22633 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22634 no longer valid. Note that if we are parsing tentatively, and
22635 the parse fails, OBJECT_TYPE will be automatically restored. */
22636 parser->context->object_type = NULL_TREE;
22638 if (name == error_mark_node)
22639 return error_mark_node;
22641 /* A template-id has already been resolved; there is no lookup to
22642 do. */
22643 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22644 return name;
22645 if (BASELINK_P (name))
22647 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22648 == TEMPLATE_ID_EXPR);
22649 return name;
22652 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22653 it should already have been checked to make sure that the name
22654 used matches the type being destroyed. */
22655 if (TREE_CODE (name) == BIT_NOT_EXPR)
22657 tree type;
22659 /* Figure out to which type this destructor applies. */
22660 if (parser->scope)
22661 type = parser->scope;
22662 else if (object_type)
22663 type = object_type;
22664 else
22665 type = current_class_type;
22666 /* If that's not a class type, there is no destructor. */
22667 if (!type || !CLASS_TYPE_P (type))
22668 return error_mark_node;
22669 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22670 lazily_declare_fn (sfk_destructor, type);
22671 if (!CLASSTYPE_DESTRUCTORS (type))
22672 return error_mark_node;
22673 /* If it was a class type, return the destructor. */
22674 return CLASSTYPE_DESTRUCTORS (type);
22677 /* By this point, the NAME should be an ordinary identifier. If
22678 the id-expression was a qualified name, the qualifying scope is
22679 stored in PARSER->SCOPE at this point. */
22680 gcc_assert (identifier_p (name));
22682 /* Perform the lookup. */
22683 if (parser->scope)
22685 bool dependent_p;
22687 if (parser->scope == error_mark_node)
22688 return error_mark_node;
22690 /* If the SCOPE is dependent, the lookup must be deferred until
22691 the template is instantiated -- unless we are explicitly
22692 looking up names in uninstantiated templates. Even then, we
22693 cannot look up the name if the scope is not a class type; it
22694 might, for example, be a template type parameter. */
22695 dependent_p = (TYPE_P (parser->scope)
22696 && dependent_scope_p (parser->scope));
22697 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22698 && dependent_p)
22699 /* Defer lookup. */
22700 decl = error_mark_node;
22701 else
22703 tree pushed_scope = NULL_TREE;
22705 /* If PARSER->SCOPE is a dependent type, then it must be a
22706 class type, and we must not be checking dependencies;
22707 otherwise, we would have processed this lookup above. So
22708 that PARSER->SCOPE is not considered a dependent base by
22709 lookup_member, we must enter the scope here. */
22710 if (dependent_p)
22711 pushed_scope = push_scope (parser->scope);
22713 /* If the PARSER->SCOPE is a template specialization, it
22714 may be instantiated during name lookup. In that case,
22715 errors may be issued. Even if we rollback the current
22716 tentative parse, those errors are valid. */
22717 decl = lookup_qualified_name (parser->scope, name,
22718 tag_type != none_type,
22719 /*complain=*/true);
22721 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22722 lookup result and the nested-name-specifier nominates a class C:
22723 * if the name specified after the nested-name-specifier, when
22724 looked up in C, is the injected-class-name of C (Clause 9), or
22725 * if the name specified after the nested-name-specifier is the
22726 same as the identifier or the simple-template-id's template-
22727 name in the last component of the nested-name-specifier,
22728 the name is instead considered to name the constructor of
22729 class C. [ Note: for example, the constructor is not an
22730 acceptable lookup result in an elaborated-type-specifier so
22731 the constructor would not be used in place of the
22732 injected-class-name. --end note ] Such a constructor name
22733 shall be used only in the declarator-id of a declaration that
22734 names a constructor or in a using-declaration. */
22735 if (tag_type == none_type
22736 && DECL_SELF_REFERENCE_P (decl)
22737 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22738 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22739 tag_type != none_type,
22740 /*complain=*/true);
22742 /* If we have a single function from a using decl, pull it out. */
22743 if (TREE_CODE (decl) == OVERLOAD
22744 && !really_overloaded_fn (decl))
22745 decl = OVL_FUNCTION (decl);
22747 if (pushed_scope)
22748 pop_scope (pushed_scope);
22751 /* If the scope is a dependent type and either we deferred lookup or
22752 we did lookup but didn't find the name, rememeber the name. */
22753 if (decl == error_mark_node && TYPE_P (parser->scope)
22754 && dependent_type_p (parser->scope))
22756 if (tag_type)
22758 tree type;
22760 /* The resolution to Core Issue 180 says that `struct
22761 A::B' should be considered a type-name, even if `A'
22762 is dependent. */
22763 type = make_typename_type (parser->scope, name, tag_type,
22764 /*complain=*/tf_error);
22765 if (type != error_mark_node)
22766 decl = TYPE_NAME (type);
22768 else if (is_template
22769 && (cp_parser_next_token_ends_template_argument_p (parser)
22770 || cp_lexer_next_token_is (parser->lexer,
22771 CPP_CLOSE_PAREN)))
22772 decl = make_unbound_class_template (parser->scope,
22773 name, NULL_TREE,
22774 /*complain=*/tf_error);
22775 else
22776 decl = build_qualified_name (/*type=*/NULL_TREE,
22777 parser->scope, name,
22778 is_template);
22780 parser->qualifying_scope = parser->scope;
22781 parser->object_scope = NULL_TREE;
22783 else if (object_type)
22785 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22786 OBJECT_TYPE is not a class. */
22787 if (CLASS_TYPE_P (object_type))
22788 /* If the OBJECT_TYPE is a template specialization, it may
22789 be instantiated during name lookup. In that case, errors
22790 may be issued. Even if we rollback the current tentative
22791 parse, those errors are valid. */
22792 decl = lookup_member (object_type,
22793 name,
22794 /*protect=*/0,
22795 tag_type != none_type,
22796 tf_warning_or_error);
22797 else
22798 decl = NULL_TREE;
22800 if (!decl)
22801 /* Look it up in the enclosing context. */
22802 decl = lookup_name_real (name, tag_type != none_type,
22803 /*nonclass=*/0,
22804 /*block_p=*/true, is_namespace, 0);
22805 parser->object_scope = object_type;
22806 parser->qualifying_scope = NULL_TREE;
22808 else
22810 decl = lookup_name_real (name, tag_type != none_type,
22811 /*nonclass=*/0,
22812 /*block_p=*/true, is_namespace, 0);
22813 parser->qualifying_scope = NULL_TREE;
22814 parser->object_scope = NULL_TREE;
22817 /* If the lookup failed, let our caller know. */
22818 if (!decl || decl == error_mark_node)
22819 return error_mark_node;
22821 /* Pull out the template from an injected-class-name (or multiple). */
22822 if (is_template)
22823 decl = maybe_get_template_decl_from_type_decl (decl);
22825 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22826 if (TREE_CODE (decl) == TREE_LIST)
22828 if (ambiguous_decls)
22829 *ambiguous_decls = decl;
22830 /* The error message we have to print is too complicated for
22831 cp_parser_error, so we incorporate its actions directly. */
22832 if (!cp_parser_simulate_error (parser))
22834 error_at (name_location, "reference to %qD is ambiguous",
22835 name);
22836 print_candidates (decl);
22838 return error_mark_node;
22841 gcc_assert (DECL_P (decl)
22842 || TREE_CODE (decl) == OVERLOAD
22843 || TREE_CODE (decl) == SCOPE_REF
22844 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22845 || BASELINK_P (decl));
22847 /* If we have resolved the name of a member declaration, check to
22848 see if the declaration is accessible. When the name resolves to
22849 set of overloaded functions, accessibility is checked when
22850 overload resolution is done.
22852 During an explicit instantiation, access is not checked at all,
22853 as per [temp.explicit]. */
22854 if (DECL_P (decl))
22855 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22857 maybe_record_typedef_use (decl);
22859 return decl;
22862 /* Like cp_parser_lookup_name, but for use in the typical case where
22863 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22864 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22866 static tree
22867 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22869 return cp_parser_lookup_name (parser, name,
22870 none_type,
22871 /*is_template=*/false,
22872 /*is_namespace=*/false,
22873 /*check_dependency=*/true,
22874 /*ambiguous_decls=*/NULL,
22875 location);
22878 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22879 the current context, return the TYPE_DECL. If TAG_NAME_P is
22880 true, the DECL indicates the class being defined in a class-head,
22881 or declared in an elaborated-type-specifier.
22883 Otherwise, return DECL. */
22885 static tree
22886 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22888 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22889 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22891 struct A {
22892 template <typename T> struct B;
22895 template <typename T> struct A::B {};
22897 Similarly, in an elaborated-type-specifier:
22899 namespace N { struct X{}; }
22901 struct A {
22902 template <typename T> friend struct N::X;
22905 However, if the DECL refers to a class type, and we are in
22906 the scope of the class, then the name lookup automatically
22907 finds the TYPE_DECL created by build_self_reference rather
22908 than a TEMPLATE_DECL. For example, in:
22910 template <class T> struct S {
22911 S s;
22914 there is no need to handle such case. */
22916 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22917 return DECL_TEMPLATE_RESULT (decl);
22919 return decl;
22922 /* If too many, or too few, template-parameter lists apply to the
22923 declarator, issue an error message. Returns TRUE if all went well,
22924 and FALSE otherwise. */
22926 static bool
22927 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22928 cp_declarator *declarator,
22929 location_t declarator_location)
22931 switch (declarator->kind)
22933 case cdk_id:
22935 unsigned num_templates = 0;
22936 tree scope = declarator->u.id.qualifying_scope;
22938 if (scope)
22939 num_templates = num_template_headers_for_class (scope);
22940 else if (TREE_CODE (declarator->u.id.unqualified_name)
22941 == TEMPLATE_ID_EXPR)
22942 /* If the DECLARATOR has the form `X<y>' then it uses one
22943 additional level of template parameters. */
22944 ++num_templates;
22946 return cp_parser_check_template_parameters
22947 (parser, num_templates, declarator_location, declarator);
22950 case cdk_function:
22951 case cdk_array:
22952 case cdk_pointer:
22953 case cdk_reference:
22954 case cdk_ptrmem:
22955 return (cp_parser_check_declarator_template_parameters
22956 (parser, declarator->declarator, declarator_location));
22958 case cdk_error:
22959 return true;
22961 default:
22962 gcc_unreachable ();
22964 return false;
22967 /* NUM_TEMPLATES were used in the current declaration. If that is
22968 invalid, return FALSE and issue an error messages. Otherwise,
22969 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22970 declarator and we can print more accurate diagnostics. */
22972 static bool
22973 cp_parser_check_template_parameters (cp_parser* parser,
22974 unsigned num_templates,
22975 location_t location,
22976 cp_declarator *declarator)
22978 /* If there are the same number of template classes and parameter
22979 lists, that's OK. */
22980 if (parser->num_template_parameter_lists == num_templates)
22981 return true;
22982 /* If there are more, but only one more, then we are referring to a
22983 member template. That's OK too. */
22984 if (parser->num_template_parameter_lists == num_templates + 1)
22985 return true;
22986 /* If there are more template classes than parameter lists, we have
22987 something like:
22989 template <class T> void S<T>::R<T>::f (); */
22990 if (parser->num_template_parameter_lists < num_templates)
22992 if (declarator && !current_function_decl)
22993 error_at (location, "specializing member %<%T::%E%> "
22994 "requires %<template<>%> syntax",
22995 declarator->u.id.qualifying_scope,
22996 declarator->u.id.unqualified_name);
22997 else if (declarator)
22998 error_at (location, "invalid declaration of %<%T::%E%>",
22999 declarator->u.id.qualifying_scope,
23000 declarator->u.id.unqualified_name);
23001 else
23002 error_at (location, "too few template-parameter-lists");
23003 return false;
23005 /* Otherwise, there are too many template parameter lists. We have
23006 something like:
23008 template <class T> template <class U> void S::f(); */
23009 error_at (location, "too many template-parameter-lists");
23010 return false;
23013 /* Parse an optional `::' token indicating that the following name is
23014 from the global namespace. If so, PARSER->SCOPE is set to the
23015 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23016 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23017 Returns the new value of PARSER->SCOPE, if the `::' token is
23018 present, and NULL_TREE otherwise. */
23020 static tree
23021 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23023 cp_token *token;
23025 /* Peek at the next token. */
23026 token = cp_lexer_peek_token (parser->lexer);
23027 /* If we're looking at a `::' token then we're starting from the
23028 global namespace, not our current location. */
23029 if (token->type == CPP_SCOPE)
23031 /* Consume the `::' token. */
23032 cp_lexer_consume_token (parser->lexer);
23033 /* Set the SCOPE so that we know where to start the lookup. */
23034 parser->scope = global_namespace;
23035 parser->qualifying_scope = global_namespace;
23036 parser->object_scope = NULL_TREE;
23038 return parser->scope;
23040 else if (!current_scope_valid_p)
23042 parser->scope = NULL_TREE;
23043 parser->qualifying_scope = NULL_TREE;
23044 parser->object_scope = NULL_TREE;
23047 return NULL_TREE;
23050 /* Returns TRUE if the upcoming token sequence is the start of a
23051 constructor declarator. If FRIEND_P is true, the declarator is
23052 preceded by the `friend' specifier. */
23054 static bool
23055 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23057 bool constructor_p;
23058 bool outside_class_specifier_p;
23059 tree nested_name_specifier;
23060 cp_token *next_token;
23062 /* The common case is that this is not a constructor declarator, so
23063 try to avoid doing lots of work if at all possible. It's not
23064 valid declare a constructor at function scope. */
23065 if (parser->in_function_body)
23066 return false;
23067 /* And only certain tokens can begin a constructor declarator. */
23068 next_token = cp_lexer_peek_token (parser->lexer);
23069 if (next_token->type != CPP_NAME
23070 && next_token->type != CPP_SCOPE
23071 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23072 && next_token->type != CPP_TEMPLATE_ID)
23073 return false;
23075 /* Parse tentatively; we are going to roll back all of the tokens
23076 consumed here. */
23077 cp_parser_parse_tentatively (parser);
23078 /* Assume that we are looking at a constructor declarator. */
23079 constructor_p = true;
23081 /* Look for the optional `::' operator. */
23082 cp_parser_global_scope_opt (parser,
23083 /*current_scope_valid_p=*/false);
23084 /* Look for the nested-name-specifier. */
23085 nested_name_specifier
23086 = (cp_parser_nested_name_specifier_opt (parser,
23087 /*typename_keyword_p=*/false,
23088 /*check_dependency_p=*/false,
23089 /*type_p=*/false,
23090 /*is_declaration=*/false));
23092 outside_class_specifier_p = (!at_class_scope_p ()
23093 || !TYPE_BEING_DEFINED (current_class_type)
23094 || friend_p);
23096 /* Outside of a class-specifier, there must be a
23097 nested-name-specifier. */
23098 if (!nested_name_specifier && outside_class_specifier_p)
23099 constructor_p = false;
23100 else if (nested_name_specifier == error_mark_node)
23101 constructor_p = false;
23103 /* If we have a class scope, this is easy; DR 147 says that S::S always
23104 names the constructor, and no other qualified name could. */
23105 if (constructor_p && nested_name_specifier
23106 && CLASS_TYPE_P (nested_name_specifier))
23108 tree id = cp_parser_unqualified_id (parser,
23109 /*template_keyword_p=*/false,
23110 /*check_dependency_p=*/false,
23111 /*declarator_p=*/true,
23112 /*optional_p=*/false);
23113 if (is_overloaded_fn (id))
23114 id = DECL_NAME (get_first_fn (id));
23115 if (!constructor_name_p (id, nested_name_specifier))
23116 constructor_p = false;
23118 /* If we still think that this might be a constructor-declarator,
23119 look for a class-name. */
23120 else if (constructor_p)
23122 /* If we have:
23124 template <typename T> struct S {
23125 S();
23128 we must recognize that the nested `S' names a class. */
23129 tree type_decl;
23130 type_decl = cp_parser_class_name (parser,
23131 /*typename_keyword_p=*/false,
23132 /*template_keyword_p=*/false,
23133 none_type,
23134 /*check_dependency_p=*/false,
23135 /*class_head_p=*/false,
23136 /*is_declaration=*/false);
23137 /* If there was no class-name, then this is not a constructor.
23138 Otherwise, if we are in a class-specifier and we aren't
23139 handling a friend declaration, check that its type matches
23140 current_class_type (c++/38313). Note: error_mark_node
23141 is left alone for error recovery purposes. */
23142 constructor_p = (!cp_parser_error_occurred (parser)
23143 && (outside_class_specifier_p
23144 || type_decl == error_mark_node
23145 || same_type_p (current_class_type,
23146 TREE_TYPE (type_decl))));
23148 /* If we're still considering a constructor, we have to see a `(',
23149 to begin the parameter-declaration-clause, followed by either a
23150 `)', an `...', or a decl-specifier. We need to check for a
23151 type-specifier to avoid being fooled into thinking that:
23153 S (f) (int);
23155 is a constructor. (It is actually a function named `f' that
23156 takes one parameter (of type `int') and returns a value of type
23157 `S'. */
23158 if (constructor_p
23159 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23160 constructor_p = false;
23162 if (constructor_p
23163 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23164 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23165 /* A parameter declaration begins with a decl-specifier,
23166 which is either the "attribute" keyword, a storage class
23167 specifier, or (usually) a type-specifier. */
23168 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23170 tree type;
23171 tree pushed_scope = NULL_TREE;
23172 unsigned saved_num_template_parameter_lists;
23174 /* Names appearing in the type-specifier should be looked up
23175 in the scope of the class. */
23176 if (current_class_type)
23177 type = NULL_TREE;
23178 else
23180 type = TREE_TYPE (type_decl);
23181 if (TREE_CODE (type) == TYPENAME_TYPE)
23183 type = resolve_typename_type (type,
23184 /*only_current_p=*/false);
23185 if (TREE_CODE (type) == TYPENAME_TYPE)
23187 cp_parser_abort_tentative_parse (parser);
23188 return false;
23191 pushed_scope = push_scope (type);
23194 /* Inside the constructor parameter list, surrounding
23195 template-parameter-lists do not apply. */
23196 saved_num_template_parameter_lists
23197 = parser->num_template_parameter_lists;
23198 parser->num_template_parameter_lists = 0;
23200 /* Look for the type-specifier. */
23201 cp_parser_type_specifier (parser,
23202 CP_PARSER_FLAGS_NONE,
23203 /*decl_specs=*/NULL,
23204 /*is_declarator=*/true,
23205 /*declares_class_or_enum=*/NULL,
23206 /*is_cv_qualifier=*/NULL);
23208 parser->num_template_parameter_lists
23209 = saved_num_template_parameter_lists;
23211 /* Leave the scope of the class. */
23212 if (pushed_scope)
23213 pop_scope (pushed_scope);
23215 constructor_p = !cp_parser_error_occurred (parser);
23219 /* We did not really want to consume any tokens. */
23220 cp_parser_abort_tentative_parse (parser);
23222 return constructor_p;
23225 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23226 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23227 they must be performed once we are in the scope of the function.
23229 Returns the function defined. */
23231 static tree
23232 cp_parser_function_definition_from_specifiers_and_declarator
23233 (cp_parser* parser,
23234 cp_decl_specifier_seq *decl_specifiers,
23235 tree attributes,
23236 const cp_declarator *declarator)
23238 tree fn;
23239 bool success_p;
23241 /* Begin the function-definition. */
23242 success_p = start_function (decl_specifiers, declarator, attributes);
23244 /* The things we're about to see are not directly qualified by any
23245 template headers we've seen thus far. */
23246 reset_specialization ();
23248 /* If there were names looked up in the decl-specifier-seq that we
23249 did not check, check them now. We must wait until we are in the
23250 scope of the function to perform the checks, since the function
23251 might be a friend. */
23252 perform_deferred_access_checks (tf_warning_or_error);
23254 if (success_p)
23256 cp_finalize_omp_declare_simd (parser, current_function_decl);
23257 parser->omp_declare_simd = NULL;
23260 if (!success_p)
23262 /* Skip the entire function. */
23263 cp_parser_skip_to_end_of_block_or_statement (parser);
23264 fn = error_mark_node;
23266 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23268 /* Seen already, skip it. An error message has already been output. */
23269 cp_parser_skip_to_end_of_block_or_statement (parser);
23270 fn = current_function_decl;
23271 current_function_decl = NULL_TREE;
23272 /* If this is a function from a class, pop the nested class. */
23273 if (current_class_name)
23274 pop_nested_class ();
23276 else
23278 timevar_id_t tv;
23279 if (DECL_DECLARED_INLINE_P (current_function_decl))
23280 tv = TV_PARSE_INLINE;
23281 else
23282 tv = TV_PARSE_FUNC;
23283 timevar_push (tv);
23284 fn = cp_parser_function_definition_after_declarator (parser,
23285 /*inline_p=*/false);
23286 timevar_pop (tv);
23289 return fn;
23292 /* Parse the part of a function-definition that follows the
23293 declarator. INLINE_P is TRUE iff this function is an inline
23294 function defined within a class-specifier.
23296 Returns the function defined. */
23298 static tree
23299 cp_parser_function_definition_after_declarator (cp_parser* parser,
23300 bool inline_p)
23302 tree fn;
23303 bool ctor_initializer_p = false;
23304 bool saved_in_unbraced_linkage_specification_p;
23305 bool saved_in_function_body;
23306 unsigned saved_num_template_parameter_lists;
23307 cp_token *token;
23308 bool fully_implicit_function_template_p
23309 = parser->fully_implicit_function_template_p;
23310 parser->fully_implicit_function_template_p = false;
23311 tree implicit_template_parms
23312 = parser->implicit_template_parms;
23313 parser->implicit_template_parms = 0;
23314 cp_binding_level* implicit_template_scope
23315 = parser->implicit_template_scope;
23316 parser->implicit_template_scope = 0;
23318 saved_in_function_body = parser->in_function_body;
23319 parser->in_function_body = true;
23320 /* If the next token is `return', then the code may be trying to
23321 make use of the "named return value" extension that G++ used to
23322 support. */
23323 token = cp_lexer_peek_token (parser->lexer);
23324 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23326 /* Consume the `return' keyword. */
23327 cp_lexer_consume_token (parser->lexer);
23328 /* Look for the identifier that indicates what value is to be
23329 returned. */
23330 cp_parser_identifier (parser);
23331 /* Issue an error message. */
23332 error_at (token->location,
23333 "named return values are no longer supported");
23334 /* Skip tokens until we reach the start of the function body. */
23335 while (true)
23337 cp_token *token = cp_lexer_peek_token (parser->lexer);
23338 if (token->type == CPP_OPEN_BRACE
23339 || token->type == CPP_EOF
23340 || token->type == CPP_PRAGMA_EOL)
23341 break;
23342 cp_lexer_consume_token (parser->lexer);
23345 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23346 anything declared inside `f'. */
23347 saved_in_unbraced_linkage_specification_p
23348 = parser->in_unbraced_linkage_specification_p;
23349 parser->in_unbraced_linkage_specification_p = false;
23350 /* Inside the function, surrounding template-parameter-lists do not
23351 apply. */
23352 saved_num_template_parameter_lists
23353 = parser->num_template_parameter_lists;
23354 parser->num_template_parameter_lists = 0;
23356 start_lambda_scope (current_function_decl);
23358 /* If the next token is `try', `__transaction_atomic', or
23359 `__transaction_relaxed`, then we are looking at either function-try-block
23360 or function-transaction-block. Note that all of these include the
23361 function-body. */
23362 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23363 ctor_initializer_p = cp_parser_function_transaction (parser,
23364 RID_TRANSACTION_ATOMIC);
23365 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23366 RID_TRANSACTION_RELAXED))
23367 ctor_initializer_p = cp_parser_function_transaction (parser,
23368 RID_TRANSACTION_RELAXED);
23369 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23370 ctor_initializer_p = cp_parser_function_try_block (parser);
23371 else
23372 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23373 (parser, /*in_function_try_block=*/false);
23375 finish_lambda_scope ();
23377 /* Finish the function. */
23378 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23379 (inline_p ? 2 : 0));
23380 /* Generate code for it, if necessary. */
23381 expand_or_defer_fn (fn);
23382 /* Restore the saved values. */
23383 parser->in_unbraced_linkage_specification_p
23384 = saved_in_unbraced_linkage_specification_p;
23385 parser->num_template_parameter_lists
23386 = saved_num_template_parameter_lists;
23387 parser->in_function_body = saved_in_function_body;
23389 parser->fully_implicit_function_template_p
23390 = fully_implicit_function_template_p;
23391 parser->implicit_template_parms
23392 = implicit_template_parms;
23393 parser->implicit_template_scope
23394 = implicit_template_scope;
23396 if (parser->fully_implicit_function_template_p)
23397 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23399 return fn;
23402 /* Parse a template-declaration, assuming that the `export' (and
23403 `extern') keywords, if present, has already been scanned. MEMBER_P
23404 is as for cp_parser_template_declaration. */
23406 static void
23407 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23409 tree decl = NULL_TREE;
23410 vec<deferred_access_check, va_gc> *checks;
23411 tree parameter_list;
23412 bool friend_p = false;
23413 bool need_lang_pop;
23414 cp_token *token;
23416 /* Look for the `template' keyword. */
23417 token = cp_lexer_peek_token (parser->lexer);
23418 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23419 return;
23421 /* And the `<'. */
23422 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23423 return;
23424 if (at_class_scope_p () && current_function_decl)
23426 /* 14.5.2.2 [temp.mem]
23428 A local class shall not have member templates. */
23429 error_at (token->location,
23430 "invalid declaration of member template in local class");
23431 cp_parser_skip_to_end_of_block_or_statement (parser);
23432 return;
23434 /* [temp]
23436 A template ... shall not have C linkage. */
23437 if (current_lang_name == lang_name_c)
23439 error_at (token->location, "template with C linkage");
23440 /* Give it C++ linkage to avoid confusing other parts of the
23441 front end. */
23442 push_lang_context (lang_name_cplusplus);
23443 need_lang_pop = true;
23445 else
23446 need_lang_pop = false;
23448 /* We cannot perform access checks on the template parameter
23449 declarations until we know what is being declared, just as we
23450 cannot check the decl-specifier list. */
23451 push_deferring_access_checks (dk_deferred);
23453 /* If the next token is `>', then we have an invalid
23454 specialization. Rather than complain about an invalid template
23455 parameter, issue an error message here. */
23456 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23458 cp_parser_error (parser, "invalid explicit specialization");
23459 begin_specialization ();
23460 parameter_list = NULL_TREE;
23462 else
23464 /* Parse the template parameters. */
23465 parameter_list = cp_parser_template_parameter_list (parser);
23468 /* Get the deferred access checks from the parameter list. These
23469 will be checked once we know what is being declared, as for a
23470 member template the checks must be performed in the scope of the
23471 class containing the member. */
23472 checks = get_deferred_access_checks ();
23474 /* Look for the `>'. */
23475 cp_parser_skip_to_end_of_template_parameter_list (parser);
23476 /* We just processed one more parameter list. */
23477 ++parser->num_template_parameter_lists;
23478 /* If the next token is `template', there are more template
23479 parameters. */
23480 if (cp_lexer_next_token_is_keyword (parser->lexer,
23481 RID_TEMPLATE))
23482 cp_parser_template_declaration_after_export (parser, member_p);
23483 else if (cxx_dialect >= cxx11
23484 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23485 decl = cp_parser_alias_declaration (parser);
23486 else
23488 /* There are no access checks when parsing a template, as we do not
23489 know if a specialization will be a friend. */
23490 push_deferring_access_checks (dk_no_check);
23491 token = cp_lexer_peek_token (parser->lexer);
23492 decl = cp_parser_single_declaration (parser,
23493 checks,
23494 member_p,
23495 /*explicit_specialization_p=*/false,
23496 &friend_p);
23497 pop_deferring_access_checks ();
23499 /* If this is a member template declaration, let the front
23500 end know. */
23501 if (member_p && !friend_p && decl)
23503 if (TREE_CODE (decl) == TYPE_DECL)
23504 cp_parser_check_access_in_redeclaration (decl, token->location);
23506 decl = finish_member_template_decl (decl);
23508 else if (friend_p && decl
23509 && DECL_DECLARES_TYPE_P (decl))
23510 make_friend_class (current_class_type, TREE_TYPE (decl),
23511 /*complain=*/true);
23513 /* We are done with the current parameter list. */
23514 --parser->num_template_parameter_lists;
23516 pop_deferring_access_checks ();
23518 /* Finish up. */
23519 finish_template_decl (parameter_list);
23521 /* Check the template arguments for a literal operator template. */
23522 if (decl
23523 && DECL_DECLARES_FUNCTION_P (decl)
23524 && UDLIT_OPER_P (DECL_NAME (decl)))
23526 bool ok = true;
23527 if (parameter_list == NULL_TREE)
23528 ok = false;
23529 else
23531 int num_parms = TREE_VEC_LENGTH (parameter_list);
23532 if (num_parms == 1)
23534 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23535 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23536 if (TREE_TYPE (parm) != char_type_node
23537 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23538 ok = false;
23540 else if (num_parms == 2 && cxx_dialect >= cxx14)
23542 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23543 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23544 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23545 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23546 if (TREE_TYPE (parm) != TREE_TYPE (type)
23547 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23548 ok = false;
23550 else
23551 ok = false;
23553 if (!ok)
23555 if (cxx_dialect >= cxx14)
23556 error ("literal operator template %qD has invalid parameter list."
23557 " Expected non-type template argument pack <char...>"
23558 " or <typename CharT, CharT...>",
23559 decl);
23560 else
23561 error ("literal operator template %qD has invalid parameter list."
23562 " Expected non-type template argument pack <char...>",
23563 decl);
23566 /* Register member declarations. */
23567 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23568 finish_member_declaration (decl);
23569 /* For the erroneous case of a template with C linkage, we pushed an
23570 implicit C++ linkage scope; exit that scope now. */
23571 if (need_lang_pop)
23572 pop_lang_context ();
23573 /* If DECL is a function template, we must return to parse it later.
23574 (Even though there is no definition, there might be default
23575 arguments that need handling.) */
23576 if (member_p && decl
23577 && DECL_DECLARES_FUNCTION_P (decl))
23578 vec_safe_push (unparsed_funs_with_definitions, decl);
23581 /* Perform the deferred access checks from a template-parameter-list.
23582 CHECKS is a TREE_LIST of access checks, as returned by
23583 get_deferred_access_checks. */
23585 static void
23586 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23588 ++processing_template_parmlist;
23589 perform_access_checks (checks, tf_warning_or_error);
23590 --processing_template_parmlist;
23593 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23594 `function-definition' sequence that follows a template header.
23595 If MEMBER_P is true, this declaration appears in a class scope.
23597 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23598 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23600 static tree
23601 cp_parser_single_declaration (cp_parser* parser,
23602 vec<deferred_access_check, va_gc> *checks,
23603 bool member_p,
23604 bool explicit_specialization_p,
23605 bool* friend_p)
23607 int declares_class_or_enum;
23608 tree decl = NULL_TREE;
23609 cp_decl_specifier_seq decl_specifiers;
23610 bool function_definition_p = false;
23611 cp_token *decl_spec_token_start;
23613 /* This function is only used when processing a template
23614 declaration. */
23615 gcc_assert (innermost_scope_kind () == sk_template_parms
23616 || innermost_scope_kind () == sk_template_spec);
23618 /* Defer access checks until we know what is being declared. */
23619 push_deferring_access_checks (dk_deferred);
23621 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23622 alternative. */
23623 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23624 cp_parser_decl_specifier_seq (parser,
23625 CP_PARSER_FLAGS_OPTIONAL,
23626 &decl_specifiers,
23627 &declares_class_or_enum);
23628 if (friend_p)
23629 *friend_p = cp_parser_friend_p (&decl_specifiers);
23631 /* There are no template typedefs. */
23632 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23634 error_at (decl_spec_token_start->location,
23635 "template declaration of %<typedef%>");
23636 decl = error_mark_node;
23639 /* Gather up the access checks that occurred the
23640 decl-specifier-seq. */
23641 stop_deferring_access_checks ();
23643 /* Check for the declaration of a template class. */
23644 if (declares_class_or_enum)
23646 if (cp_parser_declares_only_class_p (parser))
23648 decl = shadow_tag (&decl_specifiers);
23650 /* In this case:
23652 struct C {
23653 friend template <typename T> struct A<T>::B;
23656 A<T>::B will be represented by a TYPENAME_TYPE, and
23657 therefore not recognized by shadow_tag. */
23658 if (friend_p && *friend_p
23659 && !decl
23660 && decl_specifiers.type
23661 && TYPE_P (decl_specifiers.type))
23662 decl = decl_specifiers.type;
23664 if (decl && decl != error_mark_node)
23665 decl = TYPE_NAME (decl);
23666 else
23667 decl = error_mark_node;
23669 /* Perform access checks for template parameters. */
23670 cp_parser_perform_template_parameter_access_checks (checks);
23674 /* Complain about missing 'typename' or other invalid type names. */
23675 if (!decl_specifiers.any_type_specifiers_p
23676 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23678 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23679 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23680 the rest of this declaration. */
23681 decl = error_mark_node;
23682 goto out;
23685 /* If it's not a template class, try for a template function. If
23686 the next token is a `;', then this declaration does not declare
23687 anything. But, if there were errors in the decl-specifiers, then
23688 the error might well have come from an attempted class-specifier.
23689 In that case, there's no need to warn about a missing declarator. */
23690 if (!decl
23691 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23692 || decl_specifiers.type != error_mark_node))
23694 decl = cp_parser_init_declarator (parser,
23695 &decl_specifiers,
23696 checks,
23697 /*function_definition_allowed_p=*/true,
23698 member_p,
23699 declares_class_or_enum,
23700 &function_definition_p,
23701 NULL);
23703 /* 7.1.1-1 [dcl.stc]
23705 A storage-class-specifier shall not be specified in an explicit
23706 specialization... */
23707 if (decl
23708 && explicit_specialization_p
23709 && decl_specifiers.storage_class != sc_none)
23711 error_at (decl_spec_token_start->location,
23712 "explicit template specialization cannot have a storage class");
23713 decl = error_mark_node;
23716 if (decl && VAR_P (decl))
23717 check_template_variable (decl);
23720 /* Look for a trailing `;' after the declaration. */
23721 if (!function_definition_p
23722 && (decl == error_mark_node
23723 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23724 cp_parser_skip_to_end_of_block_or_statement (parser);
23726 out:
23727 pop_deferring_access_checks ();
23729 /* Clear any current qualification; whatever comes next is the start
23730 of something new. */
23731 parser->scope = NULL_TREE;
23732 parser->qualifying_scope = NULL_TREE;
23733 parser->object_scope = NULL_TREE;
23735 return decl;
23738 /* Parse a cast-expression that is not the operand of a unary "&". */
23740 static tree
23741 cp_parser_simple_cast_expression (cp_parser *parser)
23743 return cp_parser_cast_expression (parser, /*address_p=*/false,
23744 /*cast_p=*/false, /*decltype*/false, NULL);
23747 /* Parse a functional cast to TYPE. Returns an expression
23748 representing the cast. */
23750 static tree
23751 cp_parser_functional_cast (cp_parser* parser, tree type)
23753 vec<tree, va_gc> *vec;
23754 tree expression_list;
23755 tree cast;
23756 bool nonconst_p;
23758 if (!type)
23759 type = error_mark_node;
23761 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23763 cp_lexer_set_source_position (parser->lexer);
23764 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23765 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23766 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23767 if (TREE_CODE (type) == TYPE_DECL)
23768 type = TREE_TYPE (type);
23769 return finish_compound_literal (type, expression_list,
23770 tf_warning_or_error);
23774 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23775 /*cast_p=*/true,
23776 /*allow_expansion_p=*/true,
23777 /*non_constant_p=*/NULL);
23778 if (vec == NULL)
23779 expression_list = error_mark_node;
23780 else
23782 expression_list = build_tree_list_vec (vec);
23783 release_tree_vector (vec);
23786 cast = build_functional_cast (type, expression_list,
23787 tf_warning_or_error);
23788 /* [expr.const]/1: In an integral constant expression "only type
23789 conversions to integral or enumeration type can be used". */
23790 if (TREE_CODE (type) == TYPE_DECL)
23791 type = TREE_TYPE (type);
23792 if (cast != error_mark_node
23793 && !cast_valid_in_integral_constant_expression_p (type)
23794 && cp_parser_non_integral_constant_expression (parser,
23795 NIC_CONSTRUCTOR))
23796 return error_mark_node;
23797 return cast;
23800 /* Save the tokens that make up the body of a member function defined
23801 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23802 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23803 specifiers applied to the declaration. Returns the FUNCTION_DECL
23804 for the member function. */
23806 static tree
23807 cp_parser_save_member_function_body (cp_parser* parser,
23808 cp_decl_specifier_seq *decl_specifiers,
23809 cp_declarator *declarator,
23810 tree attributes)
23812 cp_token *first;
23813 cp_token *last;
23814 tree fn;
23816 /* Create the FUNCTION_DECL. */
23817 fn = grokmethod (decl_specifiers, declarator, attributes);
23818 cp_finalize_omp_declare_simd (parser, fn);
23819 /* If something went badly wrong, bail out now. */
23820 if (fn == error_mark_node)
23822 /* If there's a function-body, skip it. */
23823 if (cp_parser_token_starts_function_definition_p
23824 (cp_lexer_peek_token (parser->lexer)))
23825 cp_parser_skip_to_end_of_block_or_statement (parser);
23826 return error_mark_node;
23829 /* Remember it, if there default args to post process. */
23830 cp_parser_save_default_args (parser, fn);
23832 /* Save away the tokens that make up the body of the
23833 function. */
23834 first = parser->lexer->next_token;
23835 /* Handle function try blocks. */
23836 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23837 cp_lexer_consume_token (parser->lexer);
23838 /* We can have braced-init-list mem-initializers before the fn body. */
23839 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23841 cp_lexer_consume_token (parser->lexer);
23842 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23844 /* cache_group will stop after an un-nested { } pair, too. */
23845 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23846 break;
23848 /* variadic mem-inits have ... after the ')'. */
23849 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23850 cp_lexer_consume_token (parser->lexer);
23853 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23854 /* Handle function try blocks. */
23855 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23856 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23857 last = parser->lexer->next_token;
23859 /* Save away the inline definition; we will process it when the
23860 class is complete. */
23861 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23862 DECL_PENDING_INLINE_P (fn) = 1;
23864 /* We need to know that this was defined in the class, so that
23865 friend templates are handled correctly. */
23866 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23868 /* Add FN to the queue of functions to be parsed later. */
23869 vec_safe_push (unparsed_funs_with_definitions, fn);
23871 return fn;
23874 /* Save the tokens that make up the in-class initializer for a non-static
23875 data member. Returns a DEFAULT_ARG. */
23877 static tree
23878 cp_parser_save_nsdmi (cp_parser* parser)
23880 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23883 /* Parse a template-argument-list, as well as the trailing ">" (but
23884 not the opening "<"). See cp_parser_template_argument_list for the
23885 return value. */
23887 static tree
23888 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23890 tree arguments;
23891 tree saved_scope;
23892 tree saved_qualifying_scope;
23893 tree saved_object_scope;
23894 bool saved_greater_than_is_operator_p;
23895 int saved_unevaluated_operand;
23896 int saved_inhibit_evaluation_warnings;
23898 /* [temp.names]
23900 When parsing a template-id, the first non-nested `>' is taken as
23901 the end of the template-argument-list rather than a greater-than
23902 operator. */
23903 saved_greater_than_is_operator_p
23904 = parser->greater_than_is_operator_p;
23905 parser->greater_than_is_operator_p = false;
23906 /* Parsing the argument list may modify SCOPE, so we save it
23907 here. */
23908 saved_scope = parser->scope;
23909 saved_qualifying_scope = parser->qualifying_scope;
23910 saved_object_scope = parser->object_scope;
23911 /* We need to evaluate the template arguments, even though this
23912 template-id may be nested within a "sizeof". */
23913 saved_unevaluated_operand = cp_unevaluated_operand;
23914 cp_unevaluated_operand = 0;
23915 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23916 c_inhibit_evaluation_warnings = 0;
23917 /* Parse the template-argument-list itself. */
23918 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23919 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23920 arguments = NULL_TREE;
23921 else
23922 arguments = cp_parser_template_argument_list (parser);
23923 /* Look for the `>' that ends the template-argument-list. If we find
23924 a '>>' instead, it's probably just a typo. */
23925 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23927 if (cxx_dialect != cxx98)
23929 /* In C++0x, a `>>' in a template argument list or cast
23930 expression is considered to be two separate `>'
23931 tokens. So, change the current token to a `>', but don't
23932 consume it: it will be consumed later when the outer
23933 template argument list (or cast expression) is parsed.
23934 Note that this replacement of `>' for `>>' is necessary
23935 even if we are parsing tentatively: in the tentative
23936 case, after calling
23937 cp_parser_enclosed_template_argument_list we will always
23938 throw away all of the template arguments and the first
23939 closing `>', either because the template argument list
23940 was erroneous or because we are replacing those tokens
23941 with a CPP_TEMPLATE_ID token. The second `>' (which will
23942 not have been thrown away) is needed either to close an
23943 outer template argument list or to complete a new-style
23944 cast. */
23945 cp_token *token = cp_lexer_peek_token (parser->lexer);
23946 token->type = CPP_GREATER;
23948 else if (!saved_greater_than_is_operator_p)
23950 /* If we're in a nested template argument list, the '>>' has
23951 to be a typo for '> >'. We emit the error message, but we
23952 continue parsing and we push a '>' as next token, so that
23953 the argument list will be parsed correctly. Note that the
23954 global source location is still on the token before the
23955 '>>', so we need to say explicitly where we want it. */
23956 cp_token *token = cp_lexer_peek_token (parser->lexer);
23957 error_at (token->location, "%<>>%> should be %<> >%> "
23958 "within a nested template argument list");
23960 token->type = CPP_GREATER;
23962 else
23964 /* If this is not a nested template argument list, the '>>'
23965 is a typo for '>'. Emit an error message and continue.
23966 Same deal about the token location, but here we can get it
23967 right by consuming the '>>' before issuing the diagnostic. */
23968 cp_token *token = cp_lexer_consume_token (parser->lexer);
23969 error_at (token->location,
23970 "spurious %<>>%>, use %<>%> to terminate "
23971 "a template argument list");
23974 else
23975 cp_parser_skip_to_end_of_template_parameter_list (parser);
23976 /* The `>' token might be a greater-than operator again now. */
23977 parser->greater_than_is_operator_p
23978 = saved_greater_than_is_operator_p;
23979 /* Restore the SAVED_SCOPE. */
23980 parser->scope = saved_scope;
23981 parser->qualifying_scope = saved_qualifying_scope;
23982 parser->object_scope = saved_object_scope;
23983 cp_unevaluated_operand = saved_unevaluated_operand;
23984 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23986 return arguments;
23989 /* MEMBER_FUNCTION is a member function, or a friend. If default
23990 arguments, or the body of the function have not yet been parsed,
23991 parse them now. */
23993 static void
23994 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23996 timevar_push (TV_PARSE_INMETH);
23997 /* If this member is a template, get the underlying
23998 FUNCTION_DECL. */
23999 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24000 member_function = DECL_TEMPLATE_RESULT (member_function);
24002 /* There should not be any class definitions in progress at this
24003 point; the bodies of members are only parsed outside of all class
24004 definitions. */
24005 gcc_assert (parser->num_classes_being_defined == 0);
24006 /* While we're parsing the member functions we might encounter more
24007 classes. We want to handle them right away, but we don't want
24008 them getting mixed up with functions that are currently in the
24009 queue. */
24010 push_unparsed_function_queues (parser);
24012 /* Make sure that any template parameters are in scope. */
24013 maybe_begin_member_template_processing (member_function);
24015 /* If the body of the function has not yet been parsed, parse it
24016 now. */
24017 if (DECL_PENDING_INLINE_P (member_function))
24019 tree function_scope;
24020 cp_token_cache *tokens;
24022 /* The function is no longer pending; we are processing it. */
24023 tokens = DECL_PENDING_INLINE_INFO (member_function);
24024 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24025 DECL_PENDING_INLINE_P (member_function) = 0;
24027 /* If this is a local class, enter the scope of the containing
24028 function. */
24029 function_scope = current_function_decl;
24030 if (function_scope)
24031 push_function_context ();
24033 /* Push the body of the function onto the lexer stack. */
24034 cp_parser_push_lexer_for_tokens (parser, tokens);
24036 /* Let the front end know that we going to be defining this
24037 function. */
24038 start_preparsed_function (member_function, NULL_TREE,
24039 SF_PRE_PARSED | SF_INCLASS_INLINE);
24041 /* Don't do access checking if it is a templated function. */
24042 if (processing_template_decl)
24043 push_deferring_access_checks (dk_no_check);
24045 /* #pragma omp declare reduction needs special parsing. */
24046 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24048 parser->lexer->in_pragma = true;
24049 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24050 finish_function (/*inline*/2);
24051 cp_check_omp_declare_reduction (member_function);
24053 else
24054 /* Now, parse the body of the function. */
24055 cp_parser_function_definition_after_declarator (parser,
24056 /*inline_p=*/true);
24058 if (processing_template_decl)
24059 pop_deferring_access_checks ();
24061 /* Leave the scope of the containing function. */
24062 if (function_scope)
24063 pop_function_context ();
24064 cp_parser_pop_lexer (parser);
24067 /* Remove any template parameters from the symbol table. */
24068 maybe_end_member_template_processing ();
24070 /* Restore the queue. */
24071 pop_unparsed_function_queues (parser);
24072 timevar_pop (TV_PARSE_INMETH);
24075 /* If DECL contains any default args, remember it on the unparsed
24076 functions queue. */
24078 static void
24079 cp_parser_save_default_args (cp_parser* parser, tree decl)
24081 tree probe;
24083 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24084 probe;
24085 probe = TREE_CHAIN (probe))
24086 if (TREE_PURPOSE (probe))
24088 cp_default_arg_entry entry = {current_class_type, decl};
24089 vec_safe_push (unparsed_funs_with_default_args, entry);
24090 break;
24094 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24095 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24096 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24097 from the parameter-type-list. */
24099 static tree
24100 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24101 tree default_arg, tree parmtype)
24103 cp_token_cache *tokens;
24104 tree parsed_arg;
24105 bool dummy;
24107 if (default_arg == error_mark_node)
24108 return error_mark_node;
24110 /* Push the saved tokens for the default argument onto the parser's
24111 lexer stack. */
24112 tokens = DEFARG_TOKENS (default_arg);
24113 cp_parser_push_lexer_for_tokens (parser, tokens);
24115 start_lambda_scope (decl);
24117 /* Parse the default argument. */
24118 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24119 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24120 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24122 finish_lambda_scope ();
24124 if (parsed_arg == error_mark_node)
24125 cp_parser_skip_to_end_of_statement (parser);
24127 if (!processing_template_decl)
24129 /* In a non-template class, check conversions now. In a template,
24130 we'll wait and instantiate these as needed. */
24131 if (TREE_CODE (decl) == PARM_DECL)
24132 parsed_arg = check_default_argument (parmtype, parsed_arg,
24133 tf_warning_or_error);
24134 else
24135 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24138 /* If the token stream has not been completely used up, then
24139 there was extra junk after the end of the default
24140 argument. */
24141 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24143 if (TREE_CODE (decl) == PARM_DECL)
24144 cp_parser_error (parser, "expected %<,%>");
24145 else
24146 cp_parser_error (parser, "expected %<;%>");
24149 /* Revert to the main lexer. */
24150 cp_parser_pop_lexer (parser);
24152 return parsed_arg;
24155 /* FIELD is a non-static data member with an initializer which we saved for
24156 later; parse it now. */
24158 static void
24159 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24161 tree def;
24163 maybe_begin_member_template_processing (field);
24165 push_unparsed_function_queues (parser);
24166 def = cp_parser_late_parse_one_default_arg (parser, field,
24167 DECL_INITIAL (field),
24168 NULL_TREE);
24169 pop_unparsed_function_queues (parser);
24171 maybe_end_member_template_processing ();
24173 DECL_INITIAL (field) = def;
24176 /* FN is a FUNCTION_DECL which may contains a parameter with an
24177 unparsed DEFAULT_ARG. Parse the default args now. This function
24178 assumes that the current scope is the scope in which the default
24179 argument should be processed. */
24181 static void
24182 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24184 bool saved_local_variables_forbidden_p;
24185 tree parm, parmdecl;
24187 /* While we're parsing the default args, we might (due to the
24188 statement expression extension) encounter more classes. We want
24189 to handle them right away, but we don't want them getting mixed
24190 up with default args that are currently in the queue. */
24191 push_unparsed_function_queues (parser);
24193 /* Local variable names (and the `this' keyword) may not appear
24194 in a default argument. */
24195 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24196 parser->local_variables_forbidden_p = true;
24198 push_defarg_context (fn);
24200 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24201 parmdecl = DECL_ARGUMENTS (fn);
24202 parm && parm != void_list_node;
24203 parm = TREE_CHAIN (parm),
24204 parmdecl = DECL_CHAIN (parmdecl))
24206 tree default_arg = TREE_PURPOSE (parm);
24207 tree parsed_arg;
24208 vec<tree, va_gc> *insts;
24209 tree copy;
24210 unsigned ix;
24212 if (!default_arg)
24213 continue;
24215 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24216 /* This can happen for a friend declaration for a function
24217 already declared with default arguments. */
24218 continue;
24220 parsed_arg
24221 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24222 default_arg,
24223 TREE_VALUE (parm));
24224 if (parsed_arg == error_mark_node)
24226 continue;
24229 TREE_PURPOSE (parm) = parsed_arg;
24231 /* Update any instantiations we've already created. */
24232 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24233 vec_safe_iterate (insts, ix, &copy); ix++)
24234 TREE_PURPOSE (copy) = parsed_arg;
24237 pop_defarg_context ();
24239 /* Make sure no default arg is missing. */
24240 check_default_args (fn);
24242 /* Restore the state of local_variables_forbidden_p. */
24243 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24245 /* Restore the queue. */
24246 pop_unparsed_function_queues (parser);
24249 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24251 sizeof ... ( identifier )
24253 where the 'sizeof' token has already been consumed. */
24255 static tree
24256 cp_parser_sizeof_pack (cp_parser *parser)
24258 /* Consume the `...'. */
24259 cp_lexer_consume_token (parser->lexer);
24260 maybe_warn_variadic_templates ();
24262 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24263 if (paren)
24264 cp_lexer_consume_token (parser->lexer);
24265 else
24266 permerror (cp_lexer_peek_token (parser->lexer)->location,
24267 "%<sizeof...%> argument must be surrounded by parentheses");
24269 cp_token *token = cp_lexer_peek_token (parser->lexer);
24270 tree name = cp_parser_identifier (parser);
24271 if (name == error_mark_node)
24272 return error_mark_node;
24273 /* The name is not qualified. */
24274 parser->scope = NULL_TREE;
24275 parser->qualifying_scope = NULL_TREE;
24276 parser->object_scope = NULL_TREE;
24277 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24278 if (expr == error_mark_node)
24279 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24280 token->location);
24281 if (TREE_CODE (expr) == TYPE_DECL)
24282 expr = TREE_TYPE (expr);
24283 else if (TREE_CODE (expr) == CONST_DECL)
24284 expr = DECL_INITIAL (expr);
24285 expr = make_pack_expansion (expr);
24287 if (paren)
24288 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24290 return expr;
24293 /* Parse the operand of `sizeof' (or a similar operator). Returns
24294 either a TYPE or an expression, depending on the form of the
24295 input. The KEYWORD indicates which kind of expression we have
24296 encountered. */
24298 static tree
24299 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24301 tree expr = NULL_TREE;
24302 const char *saved_message;
24303 char *tmp;
24304 bool saved_integral_constant_expression_p;
24305 bool saved_non_integral_constant_expression_p;
24307 /* If it's a `...', then we are computing the length of a parameter
24308 pack. */
24309 if (keyword == RID_SIZEOF
24310 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24311 return cp_parser_sizeof_pack (parser);
24313 /* Types cannot be defined in a `sizeof' expression. Save away the
24314 old message. */
24315 saved_message = parser->type_definition_forbidden_message;
24316 /* And create the new one. */
24317 tmp = concat ("types may not be defined in %<",
24318 IDENTIFIER_POINTER (ridpointers[keyword]),
24319 "%> expressions", NULL);
24320 parser->type_definition_forbidden_message = tmp;
24322 /* The restrictions on constant-expressions do not apply inside
24323 sizeof expressions. */
24324 saved_integral_constant_expression_p
24325 = parser->integral_constant_expression_p;
24326 saved_non_integral_constant_expression_p
24327 = parser->non_integral_constant_expression_p;
24328 parser->integral_constant_expression_p = false;
24330 /* Do not actually evaluate the expression. */
24331 ++cp_unevaluated_operand;
24332 ++c_inhibit_evaluation_warnings;
24333 /* If it's a `(', then we might be looking at the type-id
24334 construction. */
24335 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24337 tree type = NULL_TREE;
24339 /* We can't be sure yet whether we're looking at a type-id or an
24340 expression. */
24341 cp_parser_parse_tentatively (parser);
24342 /* Note: as a GNU Extension, compound literals are considered
24343 postfix-expressions as they are in C99, so they are valid
24344 arguments to sizeof. See comment in cp_parser_cast_expression
24345 for details. */
24346 if (cp_parser_compound_literal_p (parser))
24347 cp_parser_simulate_error (parser);
24348 else
24350 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24351 parser->in_type_id_in_expr_p = true;
24352 /* Look for the type-id. */
24353 type = cp_parser_type_id (parser);
24354 /* Look for the closing `)'. */
24355 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24356 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24359 /* If all went well, then we're done. */
24360 if (cp_parser_parse_definitely (parser))
24362 cp_decl_specifier_seq decl_specs;
24364 /* Build a trivial decl-specifier-seq. */
24365 clear_decl_specs (&decl_specs);
24366 decl_specs.type = type;
24368 /* Call grokdeclarator to figure out what type this is. */
24369 expr = grokdeclarator (NULL,
24370 &decl_specs,
24371 TYPENAME,
24372 /*initialized=*/0,
24373 /*attrlist=*/NULL);
24377 /* If the type-id production did not work out, then we must be
24378 looking at the unary-expression production. */
24379 if (!expr)
24380 expr = cp_parser_unary_expression (parser);
24382 /* Go back to evaluating expressions. */
24383 --cp_unevaluated_operand;
24384 --c_inhibit_evaluation_warnings;
24386 /* Free the message we created. */
24387 free (tmp);
24388 /* And restore the old one. */
24389 parser->type_definition_forbidden_message = saved_message;
24390 parser->integral_constant_expression_p
24391 = saved_integral_constant_expression_p;
24392 parser->non_integral_constant_expression_p
24393 = saved_non_integral_constant_expression_p;
24395 return expr;
24398 /* If the current declaration has no declarator, return true. */
24400 static bool
24401 cp_parser_declares_only_class_p (cp_parser *parser)
24403 /* If the next token is a `;' or a `,' then there is no
24404 declarator. */
24405 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24406 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24409 /* Update the DECL_SPECS to reflect the storage class indicated by
24410 KEYWORD. */
24412 static void
24413 cp_parser_set_storage_class (cp_parser *parser,
24414 cp_decl_specifier_seq *decl_specs,
24415 enum rid keyword,
24416 cp_token *token)
24418 cp_storage_class storage_class;
24420 if (parser->in_unbraced_linkage_specification_p)
24422 error_at (token->location, "invalid use of %qD in linkage specification",
24423 ridpointers[keyword]);
24424 return;
24426 else if (decl_specs->storage_class != sc_none)
24428 decl_specs->conflicting_specifiers_p = true;
24429 return;
24432 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24433 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24434 && decl_specs->gnu_thread_keyword_p)
24436 pedwarn (decl_specs->locations[ds_thread], 0,
24437 "%<__thread%> before %qD", ridpointers[keyword]);
24440 switch (keyword)
24442 case RID_AUTO:
24443 storage_class = sc_auto;
24444 break;
24445 case RID_REGISTER:
24446 storage_class = sc_register;
24447 break;
24448 case RID_STATIC:
24449 storage_class = sc_static;
24450 break;
24451 case RID_EXTERN:
24452 storage_class = sc_extern;
24453 break;
24454 case RID_MUTABLE:
24455 storage_class = sc_mutable;
24456 break;
24457 default:
24458 gcc_unreachable ();
24460 decl_specs->storage_class = storage_class;
24461 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24463 /* A storage class specifier cannot be applied alongside a typedef
24464 specifier. If there is a typedef specifier present then set
24465 conflicting_specifiers_p which will trigger an error later
24466 on in grokdeclarator. */
24467 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24468 decl_specs->conflicting_specifiers_p = true;
24471 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24472 is true, the type is a class or enum definition. */
24474 static void
24475 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24476 tree type_spec,
24477 cp_token *token,
24478 bool type_definition_p)
24480 decl_specs->any_specifiers_p = true;
24482 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24483 (with, for example, in "typedef int wchar_t;") we remember that
24484 this is what happened. In system headers, we ignore these
24485 declarations so that G++ can work with system headers that are not
24486 C++-safe. */
24487 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24488 && !type_definition_p
24489 && (type_spec == boolean_type_node
24490 || type_spec == char16_type_node
24491 || type_spec == char32_type_node
24492 || type_spec == wchar_type_node)
24493 && (decl_specs->type
24494 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24495 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24496 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24497 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24499 decl_specs->redefined_builtin_type = type_spec;
24500 set_and_check_decl_spec_loc (decl_specs,
24501 ds_redefined_builtin_type_spec,
24502 token);
24503 if (!decl_specs->type)
24505 decl_specs->type = type_spec;
24506 decl_specs->type_definition_p = false;
24507 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24510 else if (decl_specs->type)
24511 decl_specs->multiple_types_p = true;
24512 else
24514 decl_specs->type = type_spec;
24515 decl_specs->type_definition_p = type_definition_p;
24516 decl_specs->redefined_builtin_type = NULL_TREE;
24517 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24521 /* True iff TOKEN is the GNU keyword __thread. */
24523 static bool
24524 token_is__thread (cp_token *token)
24526 gcc_assert (token->keyword == RID_THREAD);
24527 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24530 /* Set the location for a declarator specifier and check if it is
24531 duplicated.
24533 DECL_SPECS is the sequence of declarator specifiers onto which to
24534 set the location.
24536 DS is the single declarator specifier to set which location is to
24537 be set onto the existing sequence of declarators.
24539 LOCATION is the location for the declarator specifier to
24540 consider. */
24542 static void
24543 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24544 cp_decl_spec ds, cp_token *token)
24546 gcc_assert (ds < ds_last);
24548 if (decl_specs == NULL)
24549 return;
24551 source_location location = token->location;
24553 if (decl_specs->locations[ds] == 0)
24555 decl_specs->locations[ds] = location;
24556 if (ds == ds_thread)
24557 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24559 else
24561 if (ds == ds_long)
24563 if (decl_specs->locations[ds_long_long] != 0)
24564 error_at (location,
24565 "%<long long long%> is too long for GCC");
24566 else
24568 decl_specs->locations[ds_long_long] = location;
24569 pedwarn_cxx98 (location,
24570 OPT_Wlong_long,
24571 "ISO C++ 1998 does not support %<long long%>");
24574 else if (ds == ds_thread)
24576 bool gnu = token_is__thread (token);
24577 if (gnu != decl_specs->gnu_thread_keyword_p)
24578 error_at (location,
24579 "both %<__thread%> and %<thread_local%> specified");
24580 else
24581 error_at (location, "duplicate %qD", token->u.value);
24583 else
24585 static const char *const decl_spec_names[] = {
24586 "signed",
24587 "unsigned",
24588 "short",
24589 "long",
24590 "const",
24591 "volatile",
24592 "restrict",
24593 "inline",
24594 "virtual",
24595 "explicit",
24596 "friend",
24597 "typedef",
24598 "using",
24599 "constexpr",
24600 "__complex"
24602 error_at (location,
24603 "duplicate %qs", decl_spec_names[ds]);
24608 /* Return true iff the declarator specifier DS is present in the
24609 sequence of declarator specifiers DECL_SPECS. */
24611 bool
24612 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24613 cp_decl_spec ds)
24615 gcc_assert (ds < ds_last);
24617 if (decl_specs == NULL)
24618 return false;
24620 return decl_specs->locations[ds] != 0;
24623 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24624 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24626 static bool
24627 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24629 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24632 /* Issue an error message indicating that TOKEN_DESC was expected.
24633 If KEYWORD is true, it indicated this function is called by
24634 cp_parser_require_keword and the required token can only be
24635 a indicated keyword. */
24637 static void
24638 cp_parser_required_error (cp_parser *parser,
24639 required_token token_desc,
24640 bool keyword)
24642 switch (token_desc)
24644 case RT_NEW:
24645 cp_parser_error (parser, "expected %<new%>");
24646 return;
24647 case RT_DELETE:
24648 cp_parser_error (parser, "expected %<delete%>");
24649 return;
24650 case RT_RETURN:
24651 cp_parser_error (parser, "expected %<return%>");
24652 return;
24653 case RT_WHILE:
24654 cp_parser_error (parser, "expected %<while%>");
24655 return;
24656 case RT_EXTERN:
24657 cp_parser_error (parser, "expected %<extern%>");
24658 return;
24659 case RT_STATIC_ASSERT:
24660 cp_parser_error (parser, "expected %<static_assert%>");
24661 return;
24662 case RT_DECLTYPE:
24663 cp_parser_error (parser, "expected %<decltype%>");
24664 return;
24665 case RT_OPERATOR:
24666 cp_parser_error (parser, "expected %<operator%>");
24667 return;
24668 case RT_CLASS:
24669 cp_parser_error (parser, "expected %<class%>");
24670 return;
24671 case RT_TEMPLATE:
24672 cp_parser_error (parser, "expected %<template%>");
24673 return;
24674 case RT_NAMESPACE:
24675 cp_parser_error (parser, "expected %<namespace%>");
24676 return;
24677 case RT_USING:
24678 cp_parser_error (parser, "expected %<using%>");
24679 return;
24680 case RT_ASM:
24681 cp_parser_error (parser, "expected %<asm%>");
24682 return;
24683 case RT_TRY:
24684 cp_parser_error (parser, "expected %<try%>");
24685 return;
24686 case RT_CATCH:
24687 cp_parser_error (parser, "expected %<catch%>");
24688 return;
24689 case RT_THROW:
24690 cp_parser_error (parser, "expected %<throw%>");
24691 return;
24692 case RT_LABEL:
24693 cp_parser_error (parser, "expected %<__label__%>");
24694 return;
24695 case RT_AT_TRY:
24696 cp_parser_error (parser, "expected %<@try%>");
24697 return;
24698 case RT_AT_SYNCHRONIZED:
24699 cp_parser_error (parser, "expected %<@synchronized%>");
24700 return;
24701 case RT_AT_THROW:
24702 cp_parser_error (parser, "expected %<@throw%>");
24703 return;
24704 case RT_TRANSACTION_ATOMIC:
24705 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24706 return;
24707 case RT_TRANSACTION_RELAXED:
24708 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24709 return;
24710 default:
24711 break;
24713 if (!keyword)
24715 switch (token_desc)
24717 case RT_SEMICOLON:
24718 cp_parser_error (parser, "expected %<;%>");
24719 return;
24720 case RT_OPEN_PAREN:
24721 cp_parser_error (parser, "expected %<(%>");
24722 return;
24723 case RT_CLOSE_BRACE:
24724 cp_parser_error (parser, "expected %<}%>");
24725 return;
24726 case RT_OPEN_BRACE:
24727 cp_parser_error (parser, "expected %<{%>");
24728 return;
24729 case RT_CLOSE_SQUARE:
24730 cp_parser_error (parser, "expected %<]%>");
24731 return;
24732 case RT_OPEN_SQUARE:
24733 cp_parser_error (parser, "expected %<[%>");
24734 return;
24735 case RT_COMMA:
24736 cp_parser_error (parser, "expected %<,%>");
24737 return;
24738 case RT_SCOPE:
24739 cp_parser_error (parser, "expected %<::%>");
24740 return;
24741 case RT_LESS:
24742 cp_parser_error (parser, "expected %<<%>");
24743 return;
24744 case RT_GREATER:
24745 cp_parser_error (parser, "expected %<>%>");
24746 return;
24747 case RT_EQ:
24748 cp_parser_error (parser, "expected %<=%>");
24749 return;
24750 case RT_ELLIPSIS:
24751 cp_parser_error (parser, "expected %<...%>");
24752 return;
24753 case RT_MULT:
24754 cp_parser_error (parser, "expected %<*%>");
24755 return;
24756 case RT_COMPL:
24757 cp_parser_error (parser, "expected %<~%>");
24758 return;
24759 case RT_COLON:
24760 cp_parser_error (parser, "expected %<:%>");
24761 return;
24762 case RT_COLON_SCOPE:
24763 cp_parser_error (parser, "expected %<:%> or %<::%>");
24764 return;
24765 case RT_CLOSE_PAREN:
24766 cp_parser_error (parser, "expected %<)%>");
24767 return;
24768 case RT_COMMA_CLOSE_PAREN:
24769 cp_parser_error (parser, "expected %<,%> or %<)%>");
24770 return;
24771 case RT_PRAGMA_EOL:
24772 cp_parser_error (parser, "expected end of line");
24773 return;
24774 case RT_NAME:
24775 cp_parser_error (parser, "expected identifier");
24776 return;
24777 case RT_SELECT:
24778 cp_parser_error (parser, "expected selection-statement");
24779 return;
24780 case RT_INTERATION:
24781 cp_parser_error (parser, "expected iteration-statement");
24782 return;
24783 case RT_JUMP:
24784 cp_parser_error (parser, "expected jump-statement");
24785 return;
24786 case RT_CLASS_KEY:
24787 cp_parser_error (parser, "expected class-key");
24788 return;
24789 case RT_CLASS_TYPENAME_TEMPLATE:
24790 cp_parser_error (parser,
24791 "expected %<class%>, %<typename%>, or %<template%>");
24792 return;
24793 default:
24794 gcc_unreachable ();
24797 else
24798 gcc_unreachable ();
24803 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24804 issue an error message indicating that TOKEN_DESC was expected.
24806 Returns the token consumed, if the token had the appropriate type.
24807 Otherwise, returns NULL. */
24809 static cp_token *
24810 cp_parser_require (cp_parser* parser,
24811 enum cpp_ttype type,
24812 required_token token_desc)
24814 if (cp_lexer_next_token_is (parser->lexer, type))
24815 return cp_lexer_consume_token (parser->lexer);
24816 else
24818 /* Output the MESSAGE -- unless we're parsing tentatively. */
24819 if (!cp_parser_simulate_error (parser))
24820 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24821 return NULL;
24825 /* An error message is produced if the next token is not '>'.
24826 All further tokens are skipped until the desired token is
24827 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24829 static void
24830 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24832 /* Current level of '< ... >'. */
24833 unsigned level = 0;
24834 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24835 unsigned nesting_depth = 0;
24837 /* Are we ready, yet? If not, issue error message. */
24838 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24839 return;
24841 /* Skip tokens until the desired token is found. */
24842 while (true)
24844 /* Peek at the next token. */
24845 switch (cp_lexer_peek_token (parser->lexer)->type)
24847 case CPP_LESS:
24848 if (!nesting_depth)
24849 ++level;
24850 break;
24852 case CPP_RSHIFT:
24853 if (cxx_dialect == cxx98)
24854 /* C++0x views the `>>' operator as two `>' tokens, but
24855 C++98 does not. */
24856 break;
24857 else if (!nesting_depth && level-- == 0)
24859 /* We've hit a `>>' where the first `>' closes the
24860 template argument list, and the second `>' is
24861 spurious. Just consume the `>>' and stop; we've
24862 already produced at least one error. */
24863 cp_lexer_consume_token (parser->lexer);
24864 return;
24866 /* Fall through for C++0x, so we handle the second `>' in
24867 the `>>'. */
24869 case CPP_GREATER:
24870 if (!nesting_depth && level-- == 0)
24872 /* We've reached the token we want, consume it and stop. */
24873 cp_lexer_consume_token (parser->lexer);
24874 return;
24876 break;
24878 case CPP_OPEN_PAREN:
24879 case CPP_OPEN_SQUARE:
24880 ++nesting_depth;
24881 break;
24883 case CPP_CLOSE_PAREN:
24884 case CPP_CLOSE_SQUARE:
24885 if (nesting_depth-- == 0)
24886 return;
24887 break;
24889 case CPP_EOF:
24890 case CPP_PRAGMA_EOL:
24891 case CPP_SEMICOLON:
24892 case CPP_OPEN_BRACE:
24893 case CPP_CLOSE_BRACE:
24894 /* The '>' was probably forgotten, don't look further. */
24895 return;
24897 default:
24898 break;
24901 /* Consume this token. */
24902 cp_lexer_consume_token (parser->lexer);
24906 /* If the next token is the indicated keyword, consume it. Otherwise,
24907 issue an error message indicating that TOKEN_DESC was expected.
24909 Returns the token consumed, if the token had the appropriate type.
24910 Otherwise, returns NULL. */
24912 static cp_token *
24913 cp_parser_require_keyword (cp_parser* parser,
24914 enum rid keyword,
24915 required_token token_desc)
24917 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24919 if (token && token->keyword != keyword)
24921 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24922 return NULL;
24925 return token;
24928 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24929 function-definition. */
24931 static bool
24932 cp_parser_token_starts_function_definition_p (cp_token* token)
24934 return (/* An ordinary function-body begins with an `{'. */
24935 token->type == CPP_OPEN_BRACE
24936 /* A ctor-initializer begins with a `:'. */
24937 || token->type == CPP_COLON
24938 /* A function-try-block begins with `try'. */
24939 || token->keyword == RID_TRY
24940 /* A function-transaction-block begins with `__transaction_atomic'
24941 or `__transaction_relaxed'. */
24942 || token->keyword == RID_TRANSACTION_ATOMIC
24943 || token->keyword == RID_TRANSACTION_RELAXED
24944 /* The named return value extension begins with `return'. */
24945 || token->keyword == RID_RETURN);
24948 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24949 definition. */
24951 static bool
24952 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24954 cp_token *token;
24956 token = cp_lexer_peek_token (parser->lexer);
24957 return (token->type == CPP_OPEN_BRACE
24958 || (token->type == CPP_COLON
24959 && !parser->colon_doesnt_start_class_def_p));
24962 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24963 C++0x) ending a template-argument. */
24965 static bool
24966 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24968 cp_token *token;
24970 token = cp_lexer_peek_token (parser->lexer);
24971 return (token->type == CPP_COMMA
24972 || token->type == CPP_GREATER
24973 || token->type == CPP_ELLIPSIS
24974 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24977 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24978 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24980 static bool
24981 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24982 size_t n)
24984 cp_token *token;
24986 token = cp_lexer_peek_nth_token (parser->lexer, n);
24987 if (token->type == CPP_LESS)
24988 return true;
24989 /* Check for the sequence `<::' in the original code. It would be lexed as
24990 `[:', where `[' is a digraph, and there is no whitespace before
24991 `:'. */
24992 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24994 cp_token *token2;
24995 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24996 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24997 return true;
24999 return false;
25002 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25003 or none_type otherwise. */
25005 static enum tag_types
25006 cp_parser_token_is_class_key (cp_token* token)
25008 switch (token->keyword)
25010 case RID_CLASS:
25011 return class_type;
25012 case RID_STRUCT:
25013 return record_type;
25014 case RID_UNION:
25015 return union_type;
25017 default:
25018 return none_type;
25022 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25023 or none_type otherwise or if the token is null. */
25025 static enum tag_types
25026 cp_parser_token_is_type_parameter_key (cp_token* token)
25028 if (!token)
25029 return none_type;
25031 switch (token->keyword)
25033 case RID_CLASS:
25034 return class_type;
25035 case RID_TYPENAME:
25036 return typename_type;
25038 default:
25039 return none_type;
25043 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25045 static void
25046 cp_parser_check_class_key (enum tag_types class_key, tree type)
25048 if (type == error_mark_node)
25049 return;
25050 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25052 if (permerror (input_location, "%qs tag used in naming %q#T",
25053 class_key == union_type ? "union"
25054 : class_key == record_type ? "struct" : "class",
25055 type))
25056 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25057 "%q#T was previously declared here", type);
25061 /* Issue an error message if DECL is redeclared with different
25062 access than its original declaration [class.access.spec/3].
25063 This applies to nested classes and nested class templates.
25064 [class.mem/1]. */
25066 static void
25067 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25069 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25070 return;
25072 if ((TREE_PRIVATE (decl)
25073 != (current_access_specifier == access_private_node))
25074 || (TREE_PROTECTED (decl)
25075 != (current_access_specifier == access_protected_node)))
25076 error_at (location, "%qD redeclared with different access", decl);
25079 /* Look for the `template' keyword, as a syntactic disambiguator.
25080 Return TRUE iff it is present, in which case it will be
25081 consumed. */
25083 static bool
25084 cp_parser_optional_template_keyword (cp_parser *parser)
25086 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25088 /* In C++98 the `template' keyword can only be used within templates;
25089 outside templates the parser can always figure out what is a
25090 template and what is not. In C++11, per the resolution of DR 468,
25091 `template' is allowed in cases where it is not strictly necessary. */
25092 if (!processing_template_decl
25093 && pedantic && cxx_dialect == cxx98)
25095 cp_token *token = cp_lexer_peek_token (parser->lexer);
25096 pedwarn (token->location, OPT_Wpedantic,
25097 "in C++98 %<template%> (as a disambiguator) is only "
25098 "allowed within templates");
25099 /* If this part of the token stream is rescanned, the same
25100 error message would be generated. So, we purge the token
25101 from the stream. */
25102 cp_lexer_purge_token (parser->lexer);
25103 return false;
25105 else
25107 /* Consume the `template' keyword. */
25108 cp_lexer_consume_token (parser->lexer);
25109 return true;
25112 return false;
25115 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25116 set PARSER->SCOPE, and perform other related actions. */
25118 static void
25119 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25121 int i;
25122 struct tree_check *check_value;
25123 deferred_access_check *chk;
25124 vec<deferred_access_check, va_gc> *checks;
25126 /* Get the stored value. */
25127 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25128 /* Perform any access checks that were deferred. */
25129 checks = check_value->checks;
25130 if (checks)
25132 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25133 perform_or_defer_access_check (chk->binfo,
25134 chk->decl,
25135 chk->diag_decl, tf_warning_or_error);
25137 /* Set the scope from the stored value. */
25138 parser->scope = check_value->value;
25139 parser->qualifying_scope = check_value->qualifying_scope;
25140 parser->object_scope = NULL_TREE;
25143 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25144 encounter the end of a block before what we were looking for. */
25146 static bool
25147 cp_parser_cache_group (cp_parser *parser,
25148 enum cpp_ttype end,
25149 unsigned depth)
25151 while (true)
25153 cp_token *token = cp_lexer_peek_token (parser->lexer);
25155 /* Abort a parenthesized expression if we encounter a semicolon. */
25156 if ((end == CPP_CLOSE_PAREN || depth == 0)
25157 && token->type == CPP_SEMICOLON)
25158 return true;
25159 /* If we've reached the end of the file, stop. */
25160 if (token->type == CPP_EOF
25161 || (end != CPP_PRAGMA_EOL
25162 && token->type == CPP_PRAGMA_EOL))
25163 return true;
25164 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25165 /* We've hit the end of an enclosing block, so there's been some
25166 kind of syntax error. */
25167 return true;
25169 /* Consume the token. */
25170 cp_lexer_consume_token (parser->lexer);
25171 /* See if it starts a new group. */
25172 if (token->type == CPP_OPEN_BRACE)
25174 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25175 /* In theory this should probably check end == '}', but
25176 cp_parser_save_member_function_body needs it to exit
25177 after either '}' or ')' when called with ')'. */
25178 if (depth == 0)
25179 return false;
25181 else if (token->type == CPP_OPEN_PAREN)
25183 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25184 if (depth == 0 && end == CPP_CLOSE_PAREN)
25185 return false;
25187 else if (token->type == CPP_PRAGMA)
25188 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25189 else if (token->type == end)
25190 return false;
25194 /* Like above, for caching a default argument or NSDMI. Both of these are
25195 terminated by a non-nested comma, but it can be unclear whether or not a
25196 comma is nested in a template argument list unless we do more parsing.
25197 In order to handle this ambiguity, when we encounter a ',' after a '<'
25198 we try to parse what follows as a parameter-declaration-list (in the
25199 case of a default argument) or a member-declarator (in the case of an
25200 NSDMI). If that succeeds, then we stop caching. */
25202 static tree
25203 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25205 unsigned depth = 0;
25206 int maybe_template_id = 0;
25207 cp_token *first_token;
25208 cp_token *token;
25209 tree default_argument;
25211 /* Add tokens until we have processed the entire default
25212 argument. We add the range [first_token, token). */
25213 first_token = cp_lexer_peek_token (parser->lexer);
25214 if (first_token->type == CPP_OPEN_BRACE)
25216 /* For list-initialization, this is straightforward. */
25217 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25218 token = cp_lexer_peek_token (parser->lexer);
25220 else while (true)
25222 bool done = false;
25224 /* Peek at the next token. */
25225 token = cp_lexer_peek_token (parser->lexer);
25226 /* What we do depends on what token we have. */
25227 switch (token->type)
25229 /* In valid code, a default argument must be
25230 immediately followed by a `,' `)', or `...'. */
25231 case CPP_COMMA:
25232 if (depth == 0 && maybe_template_id)
25234 /* If we've seen a '<', we might be in a
25235 template-argument-list. Until Core issue 325 is
25236 resolved, we don't know how this situation ought
25237 to be handled, so try to DTRT. We check whether
25238 what comes after the comma is a valid parameter
25239 declaration list. If it is, then the comma ends
25240 the default argument; otherwise the default
25241 argument continues. */
25242 bool error = false;
25244 /* Set ITALP so cp_parser_parameter_declaration_list
25245 doesn't decide to commit to this parse. */
25246 bool saved_italp = parser->in_template_argument_list_p;
25247 parser->in_template_argument_list_p = true;
25249 cp_parser_parse_tentatively (parser);
25250 cp_lexer_consume_token (parser->lexer);
25252 if (nsdmi)
25254 int ctor_dtor_or_conv_p;
25255 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25256 &ctor_dtor_or_conv_p,
25257 /*parenthesized_p=*/NULL,
25258 /*member_p=*/true,
25259 /*friend_p=*/false);
25261 else
25263 begin_scope (sk_function_parms, NULL_TREE);
25264 cp_parser_parameter_declaration_list (parser, &error);
25265 pop_bindings_and_leave_scope ();
25267 if (!cp_parser_error_occurred (parser) && !error)
25268 done = true;
25269 cp_parser_abort_tentative_parse (parser);
25271 parser->in_template_argument_list_p = saved_italp;
25272 break;
25274 case CPP_CLOSE_PAREN:
25275 case CPP_ELLIPSIS:
25276 /* If we run into a non-nested `;', `}', or `]',
25277 then the code is invalid -- but the default
25278 argument is certainly over. */
25279 case CPP_SEMICOLON:
25280 case CPP_CLOSE_BRACE:
25281 case CPP_CLOSE_SQUARE:
25282 if (depth == 0
25283 /* Handle correctly int n = sizeof ... ( p ); */
25284 && token->type != CPP_ELLIPSIS)
25285 done = true;
25286 /* Update DEPTH, if necessary. */
25287 else if (token->type == CPP_CLOSE_PAREN
25288 || token->type == CPP_CLOSE_BRACE
25289 || token->type == CPP_CLOSE_SQUARE)
25290 --depth;
25291 break;
25293 case CPP_OPEN_PAREN:
25294 case CPP_OPEN_SQUARE:
25295 case CPP_OPEN_BRACE:
25296 ++depth;
25297 break;
25299 case CPP_LESS:
25300 if (depth == 0)
25301 /* This might be the comparison operator, or it might
25302 start a template argument list. */
25303 ++maybe_template_id;
25304 break;
25306 case CPP_RSHIFT:
25307 if (cxx_dialect == cxx98)
25308 break;
25309 /* Fall through for C++0x, which treats the `>>'
25310 operator like two `>' tokens in certain
25311 cases. */
25313 case CPP_GREATER:
25314 if (depth == 0)
25316 /* This might be an operator, or it might close a
25317 template argument list. But if a previous '<'
25318 started a template argument list, this will have
25319 closed it, so we can't be in one anymore. */
25320 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25321 if (maybe_template_id < 0)
25322 maybe_template_id = 0;
25324 break;
25326 /* If we run out of tokens, issue an error message. */
25327 case CPP_EOF:
25328 case CPP_PRAGMA_EOL:
25329 error_at (token->location, "file ends in default argument");
25330 done = true;
25331 break;
25333 case CPP_NAME:
25334 case CPP_SCOPE:
25335 /* In these cases, we should look for template-ids.
25336 For example, if the default argument is
25337 `X<int, double>()', we need to do name lookup to
25338 figure out whether or not `X' is a template; if
25339 so, the `,' does not end the default argument.
25341 That is not yet done. */
25342 break;
25344 default:
25345 break;
25348 /* If we've reached the end, stop. */
25349 if (done)
25350 break;
25352 /* Add the token to the token block. */
25353 token = cp_lexer_consume_token (parser->lexer);
25356 /* Create a DEFAULT_ARG to represent the unparsed default
25357 argument. */
25358 default_argument = make_node (DEFAULT_ARG);
25359 DEFARG_TOKENS (default_argument)
25360 = cp_token_cache_new (first_token, token);
25361 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25363 return default_argument;
25366 /* Begin parsing tentatively. We always save tokens while parsing
25367 tentatively so that if the tentative parsing fails we can restore the
25368 tokens. */
25370 static void
25371 cp_parser_parse_tentatively (cp_parser* parser)
25373 /* Enter a new parsing context. */
25374 parser->context = cp_parser_context_new (parser->context);
25375 /* Begin saving tokens. */
25376 cp_lexer_save_tokens (parser->lexer);
25377 /* In order to avoid repetitive access control error messages,
25378 access checks are queued up until we are no longer parsing
25379 tentatively. */
25380 push_deferring_access_checks (dk_deferred);
25383 /* Commit to the currently active tentative parse. */
25385 static void
25386 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25388 cp_parser_context *context;
25389 cp_lexer *lexer;
25391 /* Mark all of the levels as committed. */
25392 lexer = parser->lexer;
25393 for (context = parser->context; context->next; context = context->next)
25395 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25396 break;
25397 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25398 while (!cp_lexer_saving_tokens (lexer))
25399 lexer = lexer->next;
25400 cp_lexer_commit_tokens (lexer);
25404 /* Commit to the topmost currently active tentative parse.
25406 Note that this function shouldn't be called when there are
25407 irreversible side-effects while in a tentative state. For
25408 example, we shouldn't create a permanent entry in the symbol
25409 table, or issue an error message that might not apply if the
25410 tentative parse is aborted. */
25412 static void
25413 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25415 cp_parser_context *context = parser->context;
25416 cp_lexer *lexer = parser->lexer;
25418 if (context)
25420 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25421 return;
25422 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25424 while (!cp_lexer_saving_tokens (lexer))
25425 lexer = lexer->next;
25426 cp_lexer_commit_tokens (lexer);
25430 /* Abort the currently active tentative parse. All consumed tokens
25431 will be rolled back, and no diagnostics will be issued. */
25433 static void
25434 cp_parser_abort_tentative_parse (cp_parser* parser)
25436 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25437 || errorcount > 0);
25438 cp_parser_simulate_error (parser);
25439 /* Now, pretend that we want to see if the construct was
25440 successfully parsed. */
25441 cp_parser_parse_definitely (parser);
25444 /* Stop parsing tentatively. If a parse error has occurred, restore the
25445 token stream. Otherwise, commit to the tokens we have consumed.
25446 Returns true if no error occurred; false otherwise. */
25448 static bool
25449 cp_parser_parse_definitely (cp_parser* parser)
25451 bool error_occurred;
25452 cp_parser_context *context;
25454 /* Remember whether or not an error occurred, since we are about to
25455 destroy that information. */
25456 error_occurred = cp_parser_error_occurred (parser);
25457 /* Remove the topmost context from the stack. */
25458 context = parser->context;
25459 parser->context = context->next;
25460 /* If no parse errors occurred, commit to the tentative parse. */
25461 if (!error_occurred)
25463 /* Commit to the tokens read tentatively, unless that was
25464 already done. */
25465 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25466 cp_lexer_commit_tokens (parser->lexer);
25468 pop_to_parent_deferring_access_checks ();
25470 /* Otherwise, if errors occurred, roll back our state so that things
25471 are just as they were before we began the tentative parse. */
25472 else
25474 cp_lexer_rollback_tokens (parser->lexer);
25475 pop_deferring_access_checks ();
25477 /* Add the context to the front of the free list. */
25478 context->next = cp_parser_context_free_list;
25479 cp_parser_context_free_list = context;
25481 return !error_occurred;
25484 /* Returns true if we are parsing tentatively and are not committed to
25485 this tentative parse. */
25487 static bool
25488 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25490 return (cp_parser_parsing_tentatively (parser)
25491 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25494 /* Returns nonzero iff an error has occurred during the most recent
25495 tentative parse. */
25497 static bool
25498 cp_parser_error_occurred (cp_parser* parser)
25500 return (cp_parser_parsing_tentatively (parser)
25501 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25504 /* Returns nonzero if GNU extensions are allowed. */
25506 static bool
25507 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25509 return parser->allow_gnu_extensions_p;
25512 /* Objective-C++ Productions */
25515 /* Parse an Objective-C expression, which feeds into a primary-expression
25516 above.
25518 objc-expression:
25519 objc-message-expression
25520 objc-string-literal
25521 objc-encode-expression
25522 objc-protocol-expression
25523 objc-selector-expression
25525 Returns a tree representation of the expression. */
25527 static tree
25528 cp_parser_objc_expression (cp_parser* parser)
25530 /* Try to figure out what kind of declaration is present. */
25531 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25533 switch (kwd->type)
25535 case CPP_OPEN_SQUARE:
25536 return cp_parser_objc_message_expression (parser);
25538 case CPP_OBJC_STRING:
25539 kwd = cp_lexer_consume_token (parser->lexer);
25540 return objc_build_string_object (kwd->u.value);
25542 case CPP_KEYWORD:
25543 switch (kwd->keyword)
25545 case RID_AT_ENCODE:
25546 return cp_parser_objc_encode_expression (parser);
25548 case RID_AT_PROTOCOL:
25549 return cp_parser_objc_protocol_expression (parser);
25551 case RID_AT_SELECTOR:
25552 return cp_parser_objc_selector_expression (parser);
25554 default:
25555 break;
25557 default:
25558 error_at (kwd->location,
25559 "misplaced %<@%D%> Objective-C++ construct",
25560 kwd->u.value);
25561 cp_parser_skip_to_end_of_block_or_statement (parser);
25564 return error_mark_node;
25567 /* Parse an Objective-C message expression.
25569 objc-message-expression:
25570 [ objc-message-receiver objc-message-args ]
25572 Returns a representation of an Objective-C message. */
25574 static tree
25575 cp_parser_objc_message_expression (cp_parser* parser)
25577 tree receiver, messageargs;
25579 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25580 receiver = cp_parser_objc_message_receiver (parser);
25581 messageargs = cp_parser_objc_message_args (parser);
25582 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25584 return objc_build_message_expr (receiver, messageargs);
25587 /* Parse an objc-message-receiver.
25589 objc-message-receiver:
25590 expression
25591 simple-type-specifier
25593 Returns a representation of the type or expression. */
25595 static tree
25596 cp_parser_objc_message_receiver (cp_parser* parser)
25598 tree rcv;
25600 /* An Objective-C message receiver may be either (1) a type
25601 or (2) an expression. */
25602 cp_parser_parse_tentatively (parser);
25603 rcv = cp_parser_expression (parser);
25605 if (cp_parser_parse_definitely (parser))
25606 return rcv;
25608 rcv = cp_parser_simple_type_specifier (parser,
25609 /*decl_specs=*/NULL,
25610 CP_PARSER_FLAGS_NONE);
25612 return objc_get_class_reference (rcv);
25615 /* Parse the arguments and selectors comprising an Objective-C message.
25617 objc-message-args:
25618 objc-selector
25619 objc-selector-args
25620 objc-selector-args , objc-comma-args
25622 objc-selector-args:
25623 objc-selector [opt] : assignment-expression
25624 objc-selector-args objc-selector [opt] : assignment-expression
25626 objc-comma-args:
25627 assignment-expression
25628 objc-comma-args , assignment-expression
25630 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25631 selector arguments and TREE_VALUE containing a list of comma
25632 arguments. */
25634 static tree
25635 cp_parser_objc_message_args (cp_parser* parser)
25637 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25638 bool maybe_unary_selector_p = true;
25639 cp_token *token = cp_lexer_peek_token (parser->lexer);
25641 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25643 tree selector = NULL_TREE, arg;
25645 if (token->type != CPP_COLON)
25646 selector = cp_parser_objc_selector (parser);
25648 /* Detect if we have a unary selector. */
25649 if (maybe_unary_selector_p
25650 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25651 return build_tree_list (selector, NULL_TREE);
25653 maybe_unary_selector_p = false;
25654 cp_parser_require (parser, CPP_COLON, RT_COLON);
25655 arg = cp_parser_assignment_expression (parser);
25657 sel_args
25658 = chainon (sel_args,
25659 build_tree_list (selector, arg));
25661 token = cp_lexer_peek_token (parser->lexer);
25664 /* Handle non-selector arguments, if any. */
25665 while (token->type == CPP_COMMA)
25667 tree arg;
25669 cp_lexer_consume_token (parser->lexer);
25670 arg = cp_parser_assignment_expression (parser);
25672 addl_args
25673 = chainon (addl_args,
25674 build_tree_list (NULL_TREE, arg));
25676 token = cp_lexer_peek_token (parser->lexer);
25679 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25681 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25682 return build_tree_list (error_mark_node, error_mark_node);
25685 return build_tree_list (sel_args, addl_args);
25688 /* Parse an Objective-C encode expression.
25690 objc-encode-expression:
25691 @encode objc-typename
25693 Returns an encoded representation of the type argument. */
25695 static tree
25696 cp_parser_objc_encode_expression (cp_parser* parser)
25698 tree type;
25699 cp_token *token;
25701 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25702 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25703 token = cp_lexer_peek_token (parser->lexer);
25704 type = complete_type (cp_parser_type_id (parser));
25705 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25707 if (!type)
25709 error_at (token->location,
25710 "%<@encode%> must specify a type as an argument");
25711 return error_mark_node;
25714 /* This happens if we find @encode(T) (where T is a template
25715 typename or something dependent on a template typename) when
25716 parsing a template. In that case, we can't compile it
25717 immediately, but we rather create an AT_ENCODE_EXPR which will
25718 need to be instantiated when the template is used.
25720 if (dependent_type_p (type))
25722 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25723 TREE_READONLY (value) = 1;
25724 return value;
25727 return objc_build_encode_expr (type);
25730 /* Parse an Objective-C @defs expression. */
25732 static tree
25733 cp_parser_objc_defs_expression (cp_parser *parser)
25735 tree name;
25737 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25738 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25739 name = cp_parser_identifier (parser);
25740 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25742 return objc_get_class_ivars (name);
25745 /* Parse an Objective-C protocol expression.
25747 objc-protocol-expression:
25748 @protocol ( identifier )
25750 Returns a representation of the protocol expression. */
25752 static tree
25753 cp_parser_objc_protocol_expression (cp_parser* parser)
25755 tree proto;
25757 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25758 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25759 proto = cp_parser_identifier (parser);
25760 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25762 return objc_build_protocol_expr (proto);
25765 /* Parse an Objective-C selector expression.
25767 objc-selector-expression:
25768 @selector ( objc-method-signature )
25770 objc-method-signature:
25771 objc-selector
25772 objc-selector-seq
25774 objc-selector-seq:
25775 objc-selector :
25776 objc-selector-seq objc-selector :
25778 Returns a representation of the method selector. */
25780 static tree
25781 cp_parser_objc_selector_expression (cp_parser* parser)
25783 tree sel_seq = NULL_TREE;
25784 bool maybe_unary_selector_p = true;
25785 cp_token *token;
25786 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25788 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25789 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25790 token = cp_lexer_peek_token (parser->lexer);
25792 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25793 || token->type == CPP_SCOPE)
25795 tree selector = NULL_TREE;
25797 if (token->type != CPP_COLON
25798 || token->type == CPP_SCOPE)
25799 selector = cp_parser_objc_selector (parser);
25801 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25802 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25804 /* Detect if we have a unary selector. */
25805 if (maybe_unary_selector_p)
25807 sel_seq = selector;
25808 goto finish_selector;
25810 else
25812 cp_parser_error (parser, "expected %<:%>");
25815 maybe_unary_selector_p = false;
25816 token = cp_lexer_consume_token (parser->lexer);
25818 if (token->type == CPP_SCOPE)
25820 sel_seq
25821 = chainon (sel_seq,
25822 build_tree_list (selector, NULL_TREE));
25823 sel_seq
25824 = chainon (sel_seq,
25825 build_tree_list (NULL_TREE, NULL_TREE));
25827 else
25828 sel_seq
25829 = chainon (sel_seq,
25830 build_tree_list (selector, NULL_TREE));
25832 token = cp_lexer_peek_token (parser->lexer);
25835 finish_selector:
25836 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25838 return objc_build_selector_expr (loc, sel_seq);
25841 /* Parse a list of identifiers.
25843 objc-identifier-list:
25844 identifier
25845 objc-identifier-list , identifier
25847 Returns a TREE_LIST of identifier nodes. */
25849 static tree
25850 cp_parser_objc_identifier_list (cp_parser* parser)
25852 tree identifier;
25853 tree list;
25854 cp_token *sep;
25856 identifier = cp_parser_identifier (parser);
25857 if (identifier == error_mark_node)
25858 return error_mark_node;
25860 list = build_tree_list (NULL_TREE, identifier);
25861 sep = cp_lexer_peek_token (parser->lexer);
25863 while (sep->type == CPP_COMMA)
25865 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25866 identifier = cp_parser_identifier (parser);
25867 if (identifier == error_mark_node)
25868 return list;
25870 list = chainon (list, build_tree_list (NULL_TREE,
25871 identifier));
25872 sep = cp_lexer_peek_token (parser->lexer);
25875 return list;
25878 /* Parse an Objective-C alias declaration.
25880 objc-alias-declaration:
25881 @compatibility_alias identifier identifier ;
25883 This function registers the alias mapping with the Objective-C front end.
25884 It returns nothing. */
25886 static void
25887 cp_parser_objc_alias_declaration (cp_parser* parser)
25889 tree alias, orig;
25891 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25892 alias = cp_parser_identifier (parser);
25893 orig = cp_parser_identifier (parser);
25894 objc_declare_alias (alias, orig);
25895 cp_parser_consume_semicolon_at_end_of_statement (parser);
25898 /* Parse an Objective-C class forward-declaration.
25900 objc-class-declaration:
25901 @class objc-identifier-list ;
25903 The function registers the forward declarations with the Objective-C
25904 front end. It returns nothing. */
25906 static void
25907 cp_parser_objc_class_declaration (cp_parser* parser)
25909 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25910 while (true)
25912 tree id;
25914 id = cp_parser_identifier (parser);
25915 if (id == error_mark_node)
25916 break;
25918 objc_declare_class (id);
25920 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25921 cp_lexer_consume_token (parser->lexer);
25922 else
25923 break;
25925 cp_parser_consume_semicolon_at_end_of_statement (parser);
25928 /* Parse a list of Objective-C protocol references.
25930 objc-protocol-refs-opt:
25931 objc-protocol-refs [opt]
25933 objc-protocol-refs:
25934 < objc-identifier-list >
25936 Returns a TREE_LIST of identifiers, if any. */
25938 static tree
25939 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25941 tree protorefs = NULL_TREE;
25943 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25945 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25946 protorefs = cp_parser_objc_identifier_list (parser);
25947 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25950 return protorefs;
25953 /* Parse a Objective-C visibility specification. */
25955 static void
25956 cp_parser_objc_visibility_spec (cp_parser* parser)
25958 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25960 switch (vis->keyword)
25962 case RID_AT_PRIVATE:
25963 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25964 break;
25965 case RID_AT_PROTECTED:
25966 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25967 break;
25968 case RID_AT_PUBLIC:
25969 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25970 break;
25971 case RID_AT_PACKAGE:
25972 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25973 break;
25974 default:
25975 return;
25978 /* Eat '@private'/'@protected'/'@public'. */
25979 cp_lexer_consume_token (parser->lexer);
25982 /* Parse an Objective-C method type. Return 'true' if it is a class
25983 (+) method, and 'false' if it is an instance (-) method. */
25985 static inline bool
25986 cp_parser_objc_method_type (cp_parser* parser)
25988 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25989 return true;
25990 else
25991 return false;
25994 /* Parse an Objective-C protocol qualifier. */
25996 static tree
25997 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25999 tree quals = NULL_TREE, node;
26000 cp_token *token = cp_lexer_peek_token (parser->lexer);
26002 node = token->u.value;
26004 while (node && identifier_p (node)
26005 && (node == ridpointers [(int) RID_IN]
26006 || node == ridpointers [(int) RID_OUT]
26007 || node == ridpointers [(int) RID_INOUT]
26008 || node == ridpointers [(int) RID_BYCOPY]
26009 || node == ridpointers [(int) RID_BYREF]
26010 || node == ridpointers [(int) RID_ONEWAY]))
26012 quals = tree_cons (NULL_TREE, node, quals);
26013 cp_lexer_consume_token (parser->lexer);
26014 token = cp_lexer_peek_token (parser->lexer);
26015 node = token->u.value;
26018 return quals;
26021 /* Parse an Objective-C typename. */
26023 static tree
26024 cp_parser_objc_typename (cp_parser* parser)
26026 tree type_name = NULL_TREE;
26028 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26030 tree proto_quals, cp_type = NULL_TREE;
26032 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26033 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26035 /* An ObjC type name may consist of just protocol qualifiers, in which
26036 case the type shall default to 'id'. */
26037 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26039 cp_type = cp_parser_type_id (parser);
26041 /* If the type could not be parsed, an error has already
26042 been produced. For error recovery, behave as if it had
26043 not been specified, which will use the default type
26044 'id'. */
26045 if (cp_type == error_mark_node)
26047 cp_type = NULL_TREE;
26048 /* We need to skip to the closing parenthesis as
26049 cp_parser_type_id() does not seem to do it for
26050 us. */
26051 cp_parser_skip_to_closing_parenthesis (parser,
26052 /*recovering=*/true,
26053 /*or_comma=*/false,
26054 /*consume_paren=*/false);
26058 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26059 type_name = build_tree_list (proto_quals, cp_type);
26062 return type_name;
26065 /* Check to see if TYPE refers to an Objective-C selector name. */
26067 static bool
26068 cp_parser_objc_selector_p (enum cpp_ttype type)
26070 return (type == CPP_NAME || type == CPP_KEYWORD
26071 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26072 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26073 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26074 || type == CPP_XOR || type == CPP_XOR_EQ);
26077 /* Parse an Objective-C selector. */
26079 static tree
26080 cp_parser_objc_selector (cp_parser* parser)
26082 cp_token *token = cp_lexer_consume_token (parser->lexer);
26084 if (!cp_parser_objc_selector_p (token->type))
26086 error_at (token->location, "invalid Objective-C++ selector name");
26087 return error_mark_node;
26090 /* C++ operator names are allowed to appear in ObjC selectors. */
26091 switch (token->type)
26093 case CPP_AND_AND: return get_identifier ("and");
26094 case CPP_AND_EQ: return get_identifier ("and_eq");
26095 case CPP_AND: return get_identifier ("bitand");
26096 case CPP_OR: return get_identifier ("bitor");
26097 case CPP_COMPL: return get_identifier ("compl");
26098 case CPP_NOT: return get_identifier ("not");
26099 case CPP_NOT_EQ: return get_identifier ("not_eq");
26100 case CPP_OR_OR: return get_identifier ("or");
26101 case CPP_OR_EQ: return get_identifier ("or_eq");
26102 case CPP_XOR: return get_identifier ("xor");
26103 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26104 default: return token->u.value;
26108 /* Parse an Objective-C params list. */
26110 static tree
26111 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26113 tree params = NULL_TREE;
26114 bool maybe_unary_selector_p = true;
26115 cp_token *token = cp_lexer_peek_token (parser->lexer);
26117 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26119 tree selector = NULL_TREE, type_name, identifier;
26120 tree parm_attr = NULL_TREE;
26122 if (token->keyword == RID_ATTRIBUTE)
26123 break;
26125 if (token->type != CPP_COLON)
26126 selector = cp_parser_objc_selector (parser);
26128 /* Detect if we have a unary selector. */
26129 if (maybe_unary_selector_p
26130 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26132 params = selector; /* Might be followed by attributes. */
26133 break;
26136 maybe_unary_selector_p = false;
26137 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26139 /* Something went quite wrong. There should be a colon
26140 here, but there is not. Stop parsing parameters. */
26141 break;
26143 type_name = cp_parser_objc_typename (parser);
26144 /* New ObjC allows attributes on parameters too. */
26145 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26146 parm_attr = cp_parser_attributes_opt (parser);
26147 identifier = cp_parser_identifier (parser);
26149 params
26150 = chainon (params,
26151 objc_build_keyword_decl (selector,
26152 type_name,
26153 identifier,
26154 parm_attr));
26156 token = cp_lexer_peek_token (parser->lexer);
26159 if (params == NULL_TREE)
26161 cp_parser_error (parser, "objective-c++ method declaration is expected");
26162 return error_mark_node;
26165 /* We allow tail attributes for the method. */
26166 if (token->keyword == RID_ATTRIBUTE)
26168 *attributes = cp_parser_attributes_opt (parser);
26169 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26170 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26171 return params;
26172 cp_parser_error (parser,
26173 "method attributes must be specified at the end");
26174 return error_mark_node;
26177 if (params == NULL_TREE)
26179 cp_parser_error (parser, "objective-c++ method declaration is expected");
26180 return error_mark_node;
26182 return params;
26185 /* Parse the non-keyword Objective-C params. */
26187 static tree
26188 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26189 tree* attributes)
26191 tree params = make_node (TREE_LIST);
26192 cp_token *token = cp_lexer_peek_token (parser->lexer);
26193 *ellipsisp = false; /* Initially, assume no ellipsis. */
26195 while (token->type == CPP_COMMA)
26197 cp_parameter_declarator *parmdecl;
26198 tree parm;
26200 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26201 token = cp_lexer_peek_token (parser->lexer);
26203 if (token->type == CPP_ELLIPSIS)
26205 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26206 *ellipsisp = true;
26207 token = cp_lexer_peek_token (parser->lexer);
26208 break;
26211 /* TODO: parse attributes for tail parameters. */
26212 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26213 parm = grokdeclarator (parmdecl->declarator,
26214 &parmdecl->decl_specifiers,
26215 PARM, /*initialized=*/0,
26216 /*attrlist=*/NULL);
26218 chainon (params, build_tree_list (NULL_TREE, parm));
26219 token = cp_lexer_peek_token (parser->lexer);
26222 /* We allow tail attributes for the method. */
26223 if (token->keyword == RID_ATTRIBUTE)
26225 if (*attributes == NULL_TREE)
26227 *attributes = cp_parser_attributes_opt (parser);
26228 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26229 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26230 return params;
26232 else
26233 /* We have an error, but parse the attributes, so that we can
26234 carry on. */
26235 *attributes = cp_parser_attributes_opt (parser);
26237 cp_parser_error (parser,
26238 "method attributes must be specified at the end");
26239 return error_mark_node;
26242 return params;
26245 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26247 static void
26248 cp_parser_objc_interstitial_code (cp_parser* parser)
26250 cp_token *token = cp_lexer_peek_token (parser->lexer);
26252 /* If the next token is `extern' and the following token is a string
26253 literal, then we have a linkage specification. */
26254 if (token->keyword == RID_EXTERN
26255 && cp_parser_is_pure_string_literal
26256 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26257 cp_parser_linkage_specification (parser);
26258 /* Handle #pragma, if any. */
26259 else if (token->type == CPP_PRAGMA)
26260 cp_parser_pragma (parser, pragma_objc_icode);
26261 /* Allow stray semicolons. */
26262 else if (token->type == CPP_SEMICOLON)
26263 cp_lexer_consume_token (parser->lexer);
26264 /* Mark methods as optional or required, when building protocols. */
26265 else if (token->keyword == RID_AT_OPTIONAL)
26267 cp_lexer_consume_token (parser->lexer);
26268 objc_set_method_opt (true);
26270 else if (token->keyword == RID_AT_REQUIRED)
26272 cp_lexer_consume_token (parser->lexer);
26273 objc_set_method_opt (false);
26275 else if (token->keyword == RID_NAMESPACE)
26276 cp_parser_namespace_definition (parser);
26277 /* Other stray characters must generate errors. */
26278 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26280 cp_lexer_consume_token (parser->lexer);
26281 error ("stray %qs between Objective-C++ methods",
26282 token->type == CPP_OPEN_BRACE ? "{" : "}");
26284 /* Finally, try to parse a block-declaration, or a function-definition. */
26285 else
26286 cp_parser_block_declaration (parser, /*statement_p=*/false);
26289 /* Parse a method signature. */
26291 static tree
26292 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26294 tree rettype, kwdparms, optparms;
26295 bool ellipsis = false;
26296 bool is_class_method;
26298 is_class_method = cp_parser_objc_method_type (parser);
26299 rettype = cp_parser_objc_typename (parser);
26300 *attributes = NULL_TREE;
26301 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26302 if (kwdparms == error_mark_node)
26303 return error_mark_node;
26304 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26305 if (optparms == error_mark_node)
26306 return error_mark_node;
26308 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26311 static bool
26312 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26314 tree tattr;
26315 cp_lexer_save_tokens (parser->lexer);
26316 tattr = cp_parser_attributes_opt (parser);
26317 gcc_assert (tattr) ;
26319 /* If the attributes are followed by a method introducer, this is not allowed.
26320 Dump the attributes and flag the situation. */
26321 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26322 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26323 return true;
26325 /* Otherwise, the attributes introduce some interstitial code, possibly so
26326 rewind to allow that check. */
26327 cp_lexer_rollback_tokens (parser->lexer);
26328 return false;
26331 /* Parse an Objective-C method prototype list. */
26333 static void
26334 cp_parser_objc_method_prototype_list (cp_parser* parser)
26336 cp_token *token = cp_lexer_peek_token (parser->lexer);
26338 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26340 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26342 tree attributes, sig;
26343 bool is_class_method;
26344 if (token->type == CPP_PLUS)
26345 is_class_method = true;
26346 else
26347 is_class_method = false;
26348 sig = cp_parser_objc_method_signature (parser, &attributes);
26349 if (sig == error_mark_node)
26351 cp_parser_skip_to_end_of_block_or_statement (parser);
26352 token = cp_lexer_peek_token (parser->lexer);
26353 continue;
26355 objc_add_method_declaration (is_class_method, sig, attributes);
26356 cp_parser_consume_semicolon_at_end_of_statement (parser);
26358 else if (token->keyword == RID_AT_PROPERTY)
26359 cp_parser_objc_at_property_declaration (parser);
26360 else if (token->keyword == RID_ATTRIBUTE
26361 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26362 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26363 OPT_Wattributes,
26364 "prefix attributes are ignored for methods");
26365 else
26366 /* Allow for interspersed non-ObjC++ code. */
26367 cp_parser_objc_interstitial_code (parser);
26369 token = cp_lexer_peek_token (parser->lexer);
26372 if (token->type != CPP_EOF)
26373 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26374 else
26375 cp_parser_error (parser, "expected %<@end%>");
26377 objc_finish_interface ();
26380 /* Parse an Objective-C method definition list. */
26382 static void
26383 cp_parser_objc_method_definition_list (cp_parser* parser)
26385 cp_token *token = cp_lexer_peek_token (parser->lexer);
26387 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26389 tree meth;
26391 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26393 cp_token *ptk;
26394 tree sig, attribute;
26395 bool is_class_method;
26396 if (token->type == CPP_PLUS)
26397 is_class_method = true;
26398 else
26399 is_class_method = false;
26400 push_deferring_access_checks (dk_deferred);
26401 sig = cp_parser_objc_method_signature (parser, &attribute);
26402 if (sig == error_mark_node)
26404 cp_parser_skip_to_end_of_block_or_statement (parser);
26405 token = cp_lexer_peek_token (parser->lexer);
26406 continue;
26408 objc_start_method_definition (is_class_method, sig, attribute,
26409 NULL_TREE);
26411 /* For historical reasons, we accept an optional semicolon. */
26412 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26413 cp_lexer_consume_token (parser->lexer);
26415 ptk = cp_lexer_peek_token (parser->lexer);
26416 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26417 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26419 perform_deferred_access_checks (tf_warning_or_error);
26420 stop_deferring_access_checks ();
26421 meth = cp_parser_function_definition_after_declarator (parser,
26422 false);
26423 pop_deferring_access_checks ();
26424 objc_finish_method_definition (meth);
26427 /* The following case will be removed once @synthesize is
26428 completely implemented. */
26429 else if (token->keyword == RID_AT_PROPERTY)
26430 cp_parser_objc_at_property_declaration (parser);
26431 else if (token->keyword == RID_AT_SYNTHESIZE)
26432 cp_parser_objc_at_synthesize_declaration (parser);
26433 else if (token->keyword == RID_AT_DYNAMIC)
26434 cp_parser_objc_at_dynamic_declaration (parser);
26435 else if (token->keyword == RID_ATTRIBUTE
26436 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26437 warning_at (token->location, OPT_Wattributes,
26438 "prefix attributes are ignored for methods");
26439 else
26440 /* Allow for interspersed non-ObjC++ code. */
26441 cp_parser_objc_interstitial_code (parser);
26443 token = cp_lexer_peek_token (parser->lexer);
26446 if (token->type != CPP_EOF)
26447 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26448 else
26449 cp_parser_error (parser, "expected %<@end%>");
26451 objc_finish_implementation ();
26454 /* Parse Objective-C ivars. */
26456 static void
26457 cp_parser_objc_class_ivars (cp_parser* parser)
26459 cp_token *token = cp_lexer_peek_token (parser->lexer);
26461 if (token->type != CPP_OPEN_BRACE)
26462 return; /* No ivars specified. */
26464 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26465 token = cp_lexer_peek_token (parser->lexer);
26467 while (token->type != CPP_CLOSE_BRACE
26468 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26470 cp_decl_specifier_seq declspecs;
26471 int decl_class_or_enum_p;
26472 tree prefix_attributes;
26474 cp_parser_objc_visibility_spec (parser);
26476 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26477 break;
26479 cp_parser_decl_specifier_seq (parser,
26480 CP_PARSER_FLAGS_OPTIONAL,
26481 &declspecs,
26482 &decl_class_or_enum_p);
26484 /* auto, register, static, extern, mutable. */
26485 if (declspecs.storage_class != sc_none)
26487 cp_parser_error (parser, "invalid type for instance variable");
26488 declspecs.storage_class = sc_none;
26491 /* thread_local. */
26492 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26494 cp_parser_error (parser, "invalid type for instance variable");
26495 declspecs.locations[ds_thread] = 0;
26498 /* typedef. */
26499 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26501 cp_parser_error (parser, "invalid type for instance variable");
26502 declspecs.locations[ds_typedef] = 0;
26505 prefix_attributes = declspecs.attributes;
26506 declspecs.attributes = NULL_TREE;
26508 /* Keep going until we hit the `;' at the end of the
26509 declaration. */
26510 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26512 tree width = NULL_TREE, attributes, first_attribute, decl;
26513 cp_declarator *declarator = NULL;
26514 int ctor_dtor_or_conv_p;
26516 /* Check for a (possibly unnamed) bitfield declaration. */
26517 token = cp_lexer_peek_token (parser->lexer);
26518 if (token->type == CPP_COLON)
26519 goto eat_colon;
26521 if (token->type == CPP_NAME
26522 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26523 == CPP_COLON))
26525 /* Get the name of the bitfield. */
26526 declarator = make_id_declarator (NULL_TREE,
26527 cp_parser_identifier (parser),
26528 sfk_none);
26530 eat_colon:
26531 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26532 /* Get the width of the bitfield. */
26533 width
26534 = cp_parser_constant_expression (parser);
26536 else
26538 /* Parse the declarator. */
26539 declarator
26540 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26541 &ctor_dtor_or_conv_p,
26542 /*parenthesized_p=*/NULL,
26543 /*member_p=*/false,
26544 /*friend_p=*/false);
26547 /* Look for attributes that apply to the ivar. */
26548 attributes = cp_parser_attributes_opt (parser);
26549 /* Remember which attributes are prefix attributes and
26550 which are not. */
26551 first_attribute = attributes;
26552 /* Combine the attributes. */
26553 attributes = chainon (prefix_attributes, attributes);
26555 if (width)
26556 /* Create the bitfield declaration. */
26557 decl = grokbitfield (declarator, &declspecs,
26558 width,
26559 attributes);
26560 else
26561 decl = grokfield (declarator, &declspecs,
26562 NULL_TREE, /*init_const_expr_p=*/false,
26563 NULL_TREE, attributes);
26565 /* Add the instance variable. */
26566 if (decl != error_mark_node && decl != NULL_TREE)
26567 objc_add_instance_variable (decl);
26569 /* Reset PREFIX_ATTRIBUTES. */
26570 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26571 attributes = TREE_CHAIN (attributes);
26572 if (attributes)
26573 TREE_CHAIN (attributes) = NULL_TREE;
26575 token = cp_lexer_peek_token (parser->lexer);
26577 if (token->type == CPP_COMMA)
26579 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26580 continue;
26582 break;
26585 cp_parser_consume_semicolon_at_end_of_statement (parser);
26586 token = cp_lexer_peek_token (parser->lexer);
26589 if (token->keyword == RID_AT_END)
26590 cp_parser_error (parser, "expected %<}%>");
26592 /* Do not consume the RID_AT_END, so it will be read again as terminating
26593 the @interface of @implementation. */
26594 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26595 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26597 /* For historical reasons, we accept an optional semicolon. */
26598 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26599 cp_lexer_consume_token (parser->lexer);
26602 /* Parse an Objective-C protocol declaration. */
26604 static void
26605 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26607 tree proto, protorefs;
26608 cp_token *tok;
26610 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26613 tok = cp_lexer_peek_token (parser->lexer);
26614 error_at (tok->location, "identifier expected after %<@protocol%>");
26615 cp_parser_consume_semicolon_at_end_of_statement (parser);
26616 return;
26619 /* See if we have a forward declaration or a definition. */
26620 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26622 /* Try a forward declaration first. */
26623 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26625 while (true)
26627 tree id;
26629 id = cp_parser_identifier (parser);
26630 if (id == error_mark_node)
26631 break;
26633 objc_declare_protocol (id, attributes);
26635 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26636 cp_lexer_consume_token (parser->lexer);
26637 else
26638 break;
26640 cp_parser_consume_semicolon_at_end_of_statement (parser);
26643 /* Ok, we got a full-fledged definition (or at least should). */
26644 else
26646 proto = cp_parser_identifier (parser);
26647 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26648 objc_start_protocol (proto, protorefs, attributes);
26649 cp_parser_objc_method_prototype_list (parser);
26653 /* Parse an Objective-C superclass or category. */
26655 static void
26656 cp_parser_objc_superclass_or_category (cp_parser *parser,
26657 bool iface_p,
26658 tree *super,
26659 tree *categ, bool *is_class_extension)
26661 cp_token *next = cp_lexer_peek_token (parser->lexer);
26663 *super = *categ = NULL_TREE;
26664 *is_class_extension = false;
26665 if (next->type == CPP_COLON)
26667 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26668 *super = cp_parser_identifier (parser);
26670 else if (next->type == CPP_OPEN_PAREN)
26672 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26674 /* If there is no category name, and this is an @interface, we
26675 have a class extension. */
26676 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26678 *categ = NULL_TREE;
26679 *is_class_extension = true;
26681 else
26682 *categ = cp_parser_identifier (parser);
26684 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26688 /* Parse an Objective-C class interface. */
26690 static void
26691 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26693 tree name, super, categ, protos;
26694 bool is_class_extension;
26696 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26697 name = cp_parser_identifier (parser);
26698 if (name == error_mark_node)
26700 /* It's hard to recover because even if valid @interface stuff
26701 is to follow, we can't compile it (or validate it) if we
26702 don't even know which class it refers to. Let's assume this
26703 was a stray '@interface' token in the stream and skip it.
26705 return;
26707 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26708 &is_class_extension);
26709 protos = cp_parser_objc_protocol_refs_opt (parser);
26711 /* We have either a class or a category on our hands. */
26712 if (categ || is_class_extension)
26713 objc_start_category_interface (name, categ, protos, attributes);
26714 else
26716 objc_start_class_interface (name, super, protos, attributes);
26717 /* Handle instance variable declarations, if any. */
26718 cp_parser_objc_class_ivars (parser);
26719 objc_continue_interface ();
26722 cp_parser_objc_method_prototype_list (parser);
26725 /* Parse an Objective-C class implementation. */
26727 static void
26728 cp_parser_objc_class_implementation (cp_parser* parser)
26730 tree name, super, categ;
26731 bool is_class_extension;
26733 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26734 name = cp_parser_identifier (parser);
26735 if (name == error_mark_node)
26737 /* It's hard to recover because even if valid @implementation
26738 stuff is to follow, we can't compile it (or validate it) if
26739 we don't even know which class it refers to. Let's assume
26740 this was a stray '@implementation' token in the stream and
26741 skip it.
26743 return;
26745 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26746 &is_class_extension);
26748 /* We have either a class or a category on our hands. */
26749 if (categ)
26750 objc_start_category_implementation (name, categ);
26751 else
26753 objc_start_class_implementation (name, super);
26754 /* Handle instance variable declarations, if any. */
26755 cp_parser_objc_class_ivars (parser);
26756 objc_continue_implementation ();
26759 cp_parser_objc_method_definition_list (parser);
26762 /* Consume the @end token and finish off the implementation. */
26764 static void
26765 cp_parser_objc_end_implementation (cp_parser* parser)
26767 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26768 objc_finish_implementation ();
26771 /* Parse an Objective-C declaration. */
26773 static void
26774 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26776 /* Try to figure out what kind of declaration is present. */
26777 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26779 if (attributes)
26780 switch (kwd->keyword)
26782 case RID_AT_ALIAS:
26783 case RID_AT_CLASS:
26784 case RID_AT_END:
26785 error_at (kwd->location, "attributes may not be specified before"
26786 " the %<@%D%> Objective-C++ keyword",
26787 kwd->u.value);
26788 attributes = NULL;
26789 break;
26790 case RID_AT_IMPLEMENTATION:
26791 warning_at (kwd->location, OPT_Wattributes,
26792 "prefix attributes are ignored before %<@%D%>",
26793 kwd->u.value);
26794 attributes = NULL;
26795 default:
26796 break;
26799 switch (kwd->keyword)
26801 case RID_AT_ALIAS:
26802 cp_parser_objc_alias_declaration (parser);
26803 break;
26804 case RID_AT_CLASS:
26805 cp_parser_objc_class_declaration (parser);
26806 break;
26807 case RID_AT_PROTOCOL:
26808 cp_parser_objc_protocol_declaration (parser, attributes);
26809 break;
26810 case RID_AT_INTERFACE:
26811 cp_parser_objc_class_interface (parser, attributes);
26812 break;
26813 case RID_AT_IMPLEMENTATION:
26814 cp_parser_objc_class_implementation (parser);
26815 break;
26816 case RID_AT_END:
26817 cp_parser_objc_end_implementation (parser);
26818 break;
26819 default:
26820 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26821 kwd->u.value);
26822 cp_parser_skip_to_end_of_block_or_statement (parser);
26826 /* Parse an Objective-C try-catch-finally statement.
26828 objc-try-catch-finally-stmt:
26829 @try compound-statement objc-catch-clause-seq [opt]
26830 objc-finally-clause [opt]
26832 objc-catch-clause-seq:
26833 objc-catch-clause objc-catch-clause-seq [opt]
26835 objc-catch-clause:
26836 @catch ( objc-exception-declaration ) compound-statement
26838 objc-finally-clause:
26839 @finally compound-statement
26841 objc-exception-declaration:
26842 parameter-declaration
26843 '...'
26845 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26847 Returns NULL_TREE.
26849 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26850 for C. Keep them in sync. */
26852 static tree
26853 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26855 location_t location;
26856 tree stmt;
26858 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26859 location = cp_lexer_peek_token (parser->lexer)->location;
26860 objc_maybe_warn_exceptions (location);
26861 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26862 node, lest it get absorbed into the surrounding block. */
26863 stmt = push_stmt_list ();
26864 cp_parser_compound_statement (parser, NULL, false, false);
26865 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26867 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26869 cp_parameter_declarator *parm;
26870 tree parameter_declaration = error_mark_node;
26871 bool seen_open_paren = false;
26873 cp_lexer_consume_token (parser->lexer);
26874 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26875 seen_open_paren = true;
26876 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26878 /* We have "@catch (...)" (where the '...' are literally
26879 what is in the code). Skip the '...'.
26880 parameter_declaration is set to NULL_TREE, and
26881 objc_being_catch_clauses() knows that that means
26882 '...'. */
26883 cp_lexer_consume_token (parser->lexer);
26884 parameter_declaration = NULL_TREE;
26886 else
26888 /* We have "@catch (NSException *exception)" or something
26889 like that. Parse the parameter declaration. */
26890 parm = cp_parser_parameter_declaration (parser, false, NULL);
26891 if (parm == NULL)
26892 parameter_declaration = error_mark_node;
26893 else
26894 parameter_declaration = grokdeclarator (parm->declarator,
26895 &parm->decl_specifiers,
26896 PARM, /*initialized=*/0,
26897 /*attrlist=*/NULL);
26899 if (seen_open_paren)
26900 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26901 else
26903 /* If there was no open parenthesis, we are recovering from
26904 an error, and we are trying to figure out what mistake
26905 the user has made. */
26907 /* If there is an immediate closing parenthesis, the user
26908 probably forgot the opening one (ie, they typed "@catch
26909 NSException *e)". Parse the closing parenthesis and keep
26910 going. */
26911 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26912 cp_lexer_consume_token (parser->lexer);
26914 /* If these is no immediate closing parenthesis, the user
26915 probably doesn't know that parenthesis are required at
26916 all (ie, they typed "@catch NSException *e"). So, just
26917 forget about the closing parenthesis and keep going. */
26919 objc_begin_catch_clause (parameter_declaration);
26920 cp_parser_compound_statement (parser, NULL, false, false);
26921 objc_finish_catch_clause ();
26923 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26925 cp_lexer_consume_token (parser->lexer);
26926 location = cp_lexer_peek_token (parser->lexer)->location;
26927 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26928 node, lest it get absorbed into the surrounding block. */
26929 stmt = push_stmt_list ();
26930 cp_parser_compound_statement (parser, NULL, false, false);
26931 objc_build_finally_clause (location, pop_stmt_list (stmt));
26934 return objc_finish_try_stmt ();
26937 /* Parse an Objective-C synchronized statement.
26939 objc-synchronized-stmt:
26940 @synchronized ( expression ) compound-statement
26942 Returns NULL_TREE. */
26944 static tree
26945 cp_parser_objc_synchronized_statement (cp_parser *parser)
26947 location_t location;
26948 tree lock, stmt;
26950 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26952 location = cp_lexer_peek_token (parser->lexer)->location;
26953 objc_maybe_warn_exceptions (location);
26954 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26955 lock = cp_parser_expression (parser);
26956 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26958 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26959 node, lest it get absorbed into the surrounding block. */
26960 stmt = push_stmt_list ();
26961 cp_parser_compound_statement (parser, NULL, false, false);
26963 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26966 /* Parse an Objective-C throw statement.
26968 objc-throw-stmt:
26969 @throw assignment-expression [opt] ;
26971 Returns a constructed '@throw' statement. */
26973 static tree
26974 cp_parser_objc_throw_statement (cp_parser *parser)
26976 tree expr = NULL_TREE;
26977 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26979 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26981 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26982 expr = cp_parser_expression (parser);
26984 cp_parser_consume_semicolon_at_end_of_statement (parser);
26986 return objc_build_throw_stmt (loc, expr);
26989 /* Parse an Objective-C statement. */
26991 static tree
26992 cp_parser_objc_statement (cp_parser * parser)
26994 /* Try to figure out what kind of declaration is present. */
26995 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26997 switch (kwd->keyword)
26999 case RID_AT_TRY:
27000 return cp_parser_objc_try_catch_finally_statement (parser);
27001 case RID_AT_SYNCHRONIZED:
27002 return cp_parser_objc_synchronized_statement (parser);
27003 case RID_AT_THROW:
27004 return cp_parser_objc_throw_statement (parser);
27005 default:
27006 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27007 kwd->u.value);
27008 cp_parser_skip_to_end_of_block_or_statement (parser);
27011 return error_mark_node;
27014 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27015 look ahead to see if an objc keyword follows the attributes. This
27016 is to detect the use of prefix attributes on ObjC @interface and
27017 @protocol. */
27019 static bool
27020 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27022 cp_lexer_save_tokens (parser->lexer);
27023 *attrib = cp_parser_attributes_opt (parser);
27024 gcc_assert (*attrib);
27025 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27027 cp_lexer_commit_tokens (parser->lexer);
27028 return true;
27030 cp_lexer_rollback_tokens (parser->lexer);
27031 return false;
27034 /* This routine is a minimal replacement for
27035 c_parser_struct_declaration () used when parsing the list of
27036 types/names or ObjC++ properties. For example, when parsing the
27037 code
27039 @property (readonly) int a, b, c;
27041 this function is responsible for parsing "int a, int b, int c" and
27042 returning the declarations as CHAIN of DECLs.
27044 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27045 similar parsing. */
27046 static tree
27047 cp_parser_objc_struct_declaration (cp_parser *parser)
27049 tree decls = NULL_TREE;
27050 cp_decl_specifier_seq declspecs;
27051 int decl_class_or_enum_p;
27052 tree prefix_attributes;
27054 cp_parser_decl_specifier_seq (parser,
27055 CP_PARSER_FLAGS_NONE,
27056 &declspecs,
27057 &decl_class_or_enum_p);
27059 if (declspecs.type == error_mark_node)
27060 return error_mark_node;
27062 /* auto, register, static, extern, mutable. */
27063 if (declspecs.storage_class != sc_none)
27065 cp_parser_error (parser, "invalid type for property");
27066 declspecs.storage_class = sc_none;
27069 /* thread_local. */
27070 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27072 cp_parser_error (parser, "invalid type for property");
27073 declspecs.locations[ds_thread] = 0;
27076 /* typedef. */
27077 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27079 cp_parser_error (parser, "invalid type for property");
27080 declspecs.locations[ds_typedef] = 0;
27083 prefix_attributes = declspecs.attributes;
27084 declspecs.attributes = NULL_TREE;
27086 /* Keep going until we hit the `;' at the end of the declaration. */
27087 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27089 tree attributes, first_attribute, decl;
27090 cp_declarator *declarator;
27091 cp_token *token;
27093 /* Parse the declarator. */
27094 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27095 NULL, NULL, false, false);
27097 /* Look for attributes that apply to the ivar. */
27098 attributes = cp_parser_attributes_opt (parser);
27099 /* Remember which attributes are prefix attributes and
27100 which are not. */
27101 first_attribute = attributes;
27102 /* Combine the attributes. */
27103 attributes = chainon (prefix_attributes, attributes);
27105 decl = grokfield (declarator, &declspecs,
27106 NULL_TREE, /*init_const_expr_p=*/false,
27107 NULL_TREE, attributes);
27109 if (decl == error_mark_node || decl == NULL_TREE)
27110 return error_mark_node;
27112 /* Reset PREFIX_ATTRIBUTES. */
27113 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27114 attributes = TREE_CHAIN (attributes);
27115 if (attributes)
27116 TREE_CHAIN (attributes) = NULL_TREE;
27118 DECL_CHAIN (decl) = decls;
27119 decls = decl;
27121 token = cp_lexer_peek_token (parser->lexer);
27122 if (token->type == CPP_COMMA)
27124 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27125 continue;
27127 else
27128 break;
27130 return decls;
27133 /* Parse an Objective-C @property declaration. The syntax is:
27135 objc-property-declaration:
27136 '@property' objc-property-attributes[opt] struct-declaration ;
27138 objc-property-attributes:
27139 '(' objc-property-attribute-list ')'
27141 objc-property-attribute-list:
27142 objc-property-attribute
27143 objc-property-attribute-list, objc-property-attribute
27145 objc-property-attribute
27146 'getter' = identifier
27147 'setter' = identifier
27148 'readonly'
27149 'readwrite'
27150 'assign'
27151 'retain'
27152 'copy'
27153 'nonatomic'
27155 For example:
27156 @property NSString *name;
27157 @property (readonly) id object;
27158 @property (retain, nonatomic, getter=getTheName) id name;
27159 @property int a, b, c;
27161 PS: This function is identical to
27162 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27163 static void
27164 cp_parser_objc_at_property_declaration (cp_parser *parser)
27166 /* The following variables hold the attributes of the properties as
27167 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27168 seen. When we see an attribute, we set them to 'true' (if they
27169 are boolean properties) or to the identifier (if they have an
27170 argument, ie, for getter and setter). Note that here we only
27171 parse the list of attributes, check the syntax and accumulate the
27172 attributes that we find. objc_add_property_declaration() will
27173 then process the information. */
27174 bool property_assign = false;
27175 bool property_copy = false;
27176 tree property_getter_ident = NULL_TREE;
27177 bool property_nonatomic = false;
27178 bool property_readonly = false;
27179 bool property_readwrite = false;
27180 bool property_retain = false;
27181 tree property_setter_ident = NULL_TREE;
27183 /* 'properties' is the list of properties that we read. Usually a
27184 single one, but maybe more (eg, in "@property int a, b, c;" there
27185 are three). */
27186 tree properties;
27187 location_t loc;
27189 loc = cp_lexer_peek_token (parser->lexer)->location;
27191 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27193 /* Parse the optional attribute list... */
27194 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27196 /* Eat the '('. */
27197 cp_lexer_consume_token (parser->lexer);
27199 while (true)
27201 bool syntax_error = false;
27202 cp_token *token = cp_lexer_peek_token (parser->lexer);
27203 enum rid keyword;
27205 if (token->type != CPP_NAME)
27207 cp_parser_error (parser, "expected identifier");
27208 break;
27210 keyword = C_RID_CODE (token->u.value);
27211 cp_lexer_consume_token (parser->lexer);
27212 switch (keyword)
27214 case RID_ASSIGN: property_assign = true; break;
27215 case RID_COPY: property_copy = true; break;
27216 case RID_NONATOMIC: property_nonatomic = true; break;
27217 case RID_READONLY: property_readonly = true; break;
27218 case RID_READWRITE: property_readwrite = true; break;
27219 case RID_RETAIN: property_retain = true; break;
27221 case RID_GETTER:
27222 case RID_SETTER:
27223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27225 if (keyword == RID_GETTER)
27226 cp_parser_error (parser,
27227 "missing %<=%> (after %<getter%> attribute)");
27228 else
27229 cp_parser_error (parser,
27230 "missing %<=%> (after %<setter%> attribute)");
27231 syntax_error = true;
27232 break;
27234 cp_lexer_consume_token (parser->lexer); /* eat the = */
27235 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27237 cp_parser_error (parser, "expected identifier");
27238 syntax_error = true;
27239 break;
27241 if (keyword == RID_SETTER)
27243 if (property_setter_ident != NULL_TREE)
27245 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27246 cp_lexer_consume_token (parser->lexer);
27248 else
27249 property_setter_ident = cp_parser_objc_selector (parser);
27250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27251 cp_parser_error (parser, "setter name must terminate with %<:%>");
27252 else
27253 cp_lexer_consume_token (parser->lexer);
27255 else
27257 if (property_getter_ident != NULL_TREE)
27259 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27260 cp_lexer_consume_token (parser->lexer);
27262 else
27263 property_getter_ident = cp_parser_objc_selector (parser);
27265 break;
27266 default:
27267 cp_parser_error (parser, "unknown property attribute");
27268 syntax_error = true;
27269 break;
27272 if (syntax_error)
27273 break;
27275 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27276 cp_lexer_consume_token (parser->lexer);
27277 else
27278 break;
27281 /* FIXME: "@property (setter, assign);" will generate a spurious
27282 "error: expected ‘)’ before ‘,’ token". This is because
27283 cp_parser_require, unlike the C counterpart, will produce an
27284 error even if we are in error recovery. */
27285 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27287 cp_parser_skip_to_closing_parenthesis (parser,
27288 /*recovering=*/true,
27289 /*or_comma=*/false,
27290 /*consume_paren=*/true);
27294 /* ... and the property declaration(s). */
27295 properties = cp_parser_objc_struct_declaration (parser);
27297 if (properties == error_mark_node)
27299 cp_parser_skip_to_end_of_statement (parser);
27300 /* If the next token is now a `;', consume it. */
27301 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27302 cp_lexer_consume_token (parser->lexer);
27303 return;
27306 if (properties == NULL_TREE)
27307 cp_parser_error (parser, "expected identifier");
27308 else
27310 /* Comma-separated properties are chained together in
27311 reverse order; add them one by one. */
27312 properties = nreverse (properties);
27314 for (; properties; properties = TREE_CHAIN (properties))
27315 objc_add_property_declaration (loc, copy_node (properties),
27316 property_readonly, property_readwrite,
27317 property_assign, property_retain,
27318 property_copy, property_nonatomic,
27319 property_getter_ident, property_setter_ident);
27322 cp_parser_consume_semicolon_at_end_of_statement (parser);
27325 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27327 objc-synthesize-declaration:
27328 @synthesize objc-synthesize-identifier-list ;
27330 objc-synthesize-identifier-list:
27331 objc-synthesize-identifier
27332 objc-synthesize-identifier-list, objc-synthesize-identifier
27334 objc-synthesize-identifier
27335 identifier
27336 identifier = identifier
27338 For example:
27339 @synthesize MyProperty;
27340 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27342 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27343 for C. Keep them in sync.
27345 static void
27346 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27348 tree list = NULL_TREE;
27349 location_t loc;
27350 loc = cp_lexer_peek_token (parser->lexer)->location;
27352 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27353 while (true)
27355 tree property, ivar;
27356 property = cp_parser_identifier (parser);
27357 if (property == error_mark_node)
27359 cp_parser_consume_semicolon_at_end_of_statement (parser);
27360 return;
27362 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27364 cp_lexer_consume_token (parser->lexer);
27365 ivar = cp_parser_identifier (parser);
27366 if (ivar == error_mark_node)
27368 cp_parser_consume_semicolon_at_end_of_statement (parser);
27369 return;
27372 else
27373 ivar = NULL_TREE;
27374 list = chainon (list, build_tree_list (ivar, property));
27375 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27376 cp_lexer_consume_token (parser->lexer);
27377 else
27378 break;
27380 cp_parser_consume_semicolon_at_end_of_statement (parser);
27381 objc_add_synthesize_declaration (loc, list);
27384 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27386 objc-dynamic-declaration:
27387 @dynamic identifier-list ;
27389 For example:
27390 @dynamic MyProperty;
27391 @dynamic MyProperty, AnotherProperty;
27393 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27394 for C. Keep them in sync.
27396 static void
27397 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27399 tree list = NULL_TREE;
27400 location_t loc;
27401 loc = cp_lexer_peek_token (parser->lexer)->location;
27403 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27404 while (true)
27406 tree property;
27407 property = cp_parser_identifier (parser);
27408 if (property == error_mark_node)
27410 cp_parser_consume_semicolon_at_end_of_statement (parser);
27411 return;
27413 list = chainon (list, build_tree_list (NULL, property));
27414 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27415 cp_lexer_consume_token (parser->lexer);
27416 else
27417 break;
27419 cp_parser_consume_semicolon_at_end_of_statement (parser);
27420 objc_add_dynamic_declaration (loc, list);
27424 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27426 /* Returns name of the next clause.
27427 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27428 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27429 returned and the token is consumed. */
27431 static pragma_omp_clause
27432 cp_parser_omp_clause_name (cp_parser *parser)
27434 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27436 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27437 result = PRAGMA_OMP_CLAUSE_IF;
27438 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27439 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27440 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
27441 result = PRAGMA_OMP_CLAUSE_DELETE;
27442 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27443 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27444 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27445 result = PRAGMA_OMP_CLAUSE_FOR;
27446 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27448 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27449 const char *p = IDENTIFIER_POINTER (id);
27451 switch (p[0])
27453 case 'a':
27454 if (!strcmp ("aligned", p))
27455 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27456 else if (!strcmp ("async", p))
27457 result = PRAGMA_OMP_CLAUSE_ASYNC;
27458 break;
27459 case 'c':
27460 if (!strcmp ("collapse", p))
27461 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27462 else if (!strcmp ("copy", p))
27463 result = PRAGMA_OMP_CLAUSE_COPY;
27464 else if (!strcmp ("copyin", p))
27465 result = PRAGMA_OMP_CLAUSE_COPYIN;
27466 else if (!strcmp ("copyout", p))
27467 result = PRAGMA_OMP_CLAUSE_COPYOUT;
27468 else if (!strcmp ("copyprivate", p))
27469 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27470 else if (!strcmp ("create", p))
27471 result = PRAGMA_OMP_CLAUSE_CREATE;
27472 break;
27473 case 'd':
27474 if (!strcmp ("depend", p))
27475 result = PRAGMA_OMP_CLAUSE_DEPEND;
27476 else if (!strcmp ("device", p))
27477 result = PRAGMA_OMP_CLAUSE_DEVICE;
27478 else if (!strcmp ("deviceptr", p))
27479 result = PRAGMA_OMP_CLAUSE_DEVICEPTR;
27480 else if (!strcmp ("dist_schedule", p))
27481 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27482 break;
27483 case 'f':
27484 if (!strcmp ("final", p))
27485 result = PRAGMA_OMP_CLAUSE_FINAL;
27486 else if (!strcmp ("firstprivate", p))
27487 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27488 else if (!strcmp ("from", p))
27489 result = PRAGMA_OMP_CLAUSE_FROM;
27490 break;
27491 case 'h':
27492 if (!strcmp ("host", p))
27493 result = PRAGMA_OMP_CLAUSE_HOST;
27494 break;
27495 case 'i':
27496 if (!strcmp ("inbranch", p))
27497 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27498 break;
27499 case 'l':
27500 if (!strcmp ("lastprivate", p))
27501 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27502 else if (!strcmp ("linear", p))
27503 result = PRAGMA_OMP_CLAUSE_LINEAR;
27504 break;
27505 case 'm':
27506 if (!strcmp ("map", p))
27507 result = PRAGMA_OMP_CLAUSE_MAP;
27508 else if (!strcmp ("mergeable", p))
27509 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27510 else if (flag_cilkplus && !strcmp ("mask", p))
27511 result = PRAGMA_CILK_CLAUSE_MASK;
27512 break;
27513 case 'n':
27514 if (!strcmp ("notinbranch", p))
27515 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27516 else if (!strcmp ("nowait", p))
27517 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27518 else if (flag_cilkplus && !strcmp ("nomask", p))
27519 result = PRAGMA_CILK_CLAUSE_NOMASK;
27520 else if (!strcmp ("num_gangs", p))
27521 result = PRAGMA_OMP_CLAUSE_NUM_GANGS;
27522 else if (!strcmp ("num_teams", p))
27523 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27524 else if (!strcmp ("num_threads", p))
27525 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27526 else if (!strcmp ("num_workers", p))
27527 result = PRAGMA_OMP_CLAUSE_NUM_WORKERS;
27528 break;
27529 case 'o':
27530 if (!strcmp ("ordered", p))
27531 result = PRAGMA_OMP_CLAUSE_ORDERED;
27532 break;
27533 case 'p':
27534 if (!strcmp ("parallel", p))
27535 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27536 else if (!strcmp ("present", p))
27537 result = PRAGMA_OMP_CLAUSE_PRESENT;
27538 else if (!strcmp ("present_or_copy", p))
27539 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY;
27540 else if (!strcmp ("present_or_copyin", p))
27541 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN;
27542 else if (!strcmp ("present_or_copyout", p))
27543 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT;
27544 else if (!strcmp ("present_or_create", p))
27545 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE;
27546 else if (!strcmp ("private", p))
27547 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27548 else if (!strcmp ("proc_bind", p))
27549 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27550 break;
27551 case 'r':
27552 if (!strcmp ("reduction", p))
27553 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27554 break;
27555 case 's':
27556 if (!strcmp ("safelen", p))
27557 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27558 else if (!strcmp ("schedule", p))
27559 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27560 else if (!strcmp ("sections", p))
27561 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27562 else if (!strcmp ("self", p))
27563 result = PRAGMA_OMP_CLAUSE_SELF;
27564 else if (!strcmp ("shared", p))
27565 result = PRAGMA_OMP_CLAUSE_SHARED;
27566 else if (!strcmp ("simdlen", p))
27567 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27568 break;
27569 case 't':
27570 if (!strcmp ("taskgroup", p))
27571 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27572 else if (!strcmp ("thread_limit", p))
27573 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27574 else if (!strcmp ("to", p))
27575 result = PRAGMA_OMP_CLAUSE_TO;
27576 break;
27577 case 'u':
27578 if (!strcmp ("uniform", p))
27579 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27580 else if (!strcmp ("untied", p))
27581 result = PRAGMA_OMP_CLAUSE_UNTIED;
27582 break;
27583 case 'v':
27584 if (!strcmp ("vector_length", p))
27585 result = PRAGMA_OMP_CLAUSE_VECTOR_LENGTH;
27586 else if (flag_cilkplus && !strcmp ("vectorlength", p))
27587 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27588 break;
27589 case 'w':
27590 if (!strcmp ("WAIT", p))
27591 result = PRAGMA_OMP_CLAUSE_WAIT;
27592 break;
27596 if (result != PRAGMA_OMP_CLAUSE_NONE)
27597 cp_lexer_consume_token (parser->lexer);
27599 return result;
27602 /* Validate that a clause of the given type does not already exist. */
27604 static void
27605 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27606 const char *name, location_t location)
27608 tree c;
27610 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27611 if (OMP_CLAUSE_CODE (c) == code)
27613 error_at (location, "too many %qs clauses", name);
27614 break;
27618 /* OpenMP 2.5:
27619 variable-list:
27620 identifier
27621 variable-list , identifier
27623 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27624 colon). An opening parenthesis will have been consumed by the caller.
27626 If KIND is nonzero, create the appropriate node and install the decl
27627 in OMP_CLAUSE_DECL and add the node to the head of the list.
27629 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27630 return the list created.
27632 COLON can be NULL if only closing parenthesis should end the list,
27633 or pointer to bool which will receive false if the list is terminated
27634 by closing parenthesis or true if the list is terminated by colon. */
27636 static tree
27637 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27638 tree list, bool *colon)
27640 cp_token *token;
27641 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27642 if (colon)
27644 parser->colon_corrects_to_scope_p = false;
27645 *colon = false;
27647 while (1)
27649 tree name, decl;
27651 token = cp_lexer_peek_token (parser->lexer);
27652 name = cp_parser_id_expression (parser, /*template_p=*/false,
27653 /*check_dependency_p=*/true,
27654 /*template_p=*/NULL,
27655 /*declarator_p=*/false,
27656 /*optional_p=*/false);
27657 if (name == error_mark_node)
27658 goto skip_comma;
27660 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27661 if (decl == error_mark_node)
27662 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27663 token->location);
27664 else if (kind != 0)
27666 switch (kind)
27668 case OMP_NO_CLAUSE_CACHE:
27669 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27671 error_at (token->location, "expected %<[%>");
27672 decl = error_mark_node;
27673 break;
27675 /* FALL THROUGH. */
27676 case OMP_CLAUSE_MAP:
27677 case OMP_CLAUSE_FROM:
27678 case OMP_CLAUSE_TO:
27679 case OMP_CLAUSE_DEPEND:
27680 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27682 tree low_bound = NULL_TREE, length = NULL_TREE;
27684 parser->colon_corrects_to_scope_p = false;
27685 cp_lexer_consume_token (parser->lexer);
27686 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27687 low_bound = cp_parser_expression (parser);
27688 if (!colon)
27689 parser->colon_corrects_to_scope_p
27690 = saved_colon_corrects_to_scope_p;
27691 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27692 length = integer_one_node;
27693 else
27695 /* Look for `:'. */
27696 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27697 goto skip_comma;
27698 if (!cp_lexer_next_token_is (parser->lexer,
27699 CPP_CLOSE_SQUARE))
27700 length = cp_parser_expression (parser);
27702 /* Look for the closing `]'. */
27703 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27704 RT_CLOSE_SQUARE))
27705 goto skip_comma;
27707 if (kind == OMP_NO_CLAUSE_CACHE)
27709 mark_exp_read (low_bound);
27710 mark_exp_read (length);
27712 if (TREE_CODE (low_bound) != INTEGER_CST
27713 && !TREE_READONLY (low_bound))
27715 error_at (token->location,
27716 "%qD is not a constant", low_bound);
27717 decl = error_mark_node;
27720 if (TREE_CODE (length) != INTEGER_CST
27721 && !TREE_READONLY (length))
27723 error_at (token->location,
27724 "%qD is not a constant", length);
27725 decl = error_mark_node;
27729 decl = tree_cons (low_bound, length, decl);
27731 break;
27732 default:
27733 break;
27736 tree u = build_omp_clause (token->location, kind);
27737 OMP_CLAUSE_DECL (u) = decl;
27738 OMP_CLAUSE_CHAIN (u) = list;
27739 list = u;
27741 else
27742 list = tree_cons (decl, NULL_TREE, list);
27744 get_comma:
27745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27746 break;
27747 cp_lexer_consume_token (parser->lexer);
27750 if (colon)
27751 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27753 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27755 *colon = true;
27756 cp_parser_require (parser, CPP_COLON, RT_COLON);
27757 return list;
27760 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27762 int ending;
27764 /* Try to resync to an unnested comma. Copied from
27765 cp_parser_parenthesized_expression_list. */
27766 skip_comma:
27767 if (colon)
27768 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27769 ending = cp_parser_skip_to_closing_parenthesis (parser,
27770 /*recovering=*/true,
27771 /*or_comma=*/true,
27772 /*consume_paren=*/true);
27773 if (ending < 0)
27774 goto get_comma;
27777 return list;
27780 /* Similarly, but expect leading and trailing parenthesis. This is a very
27781 common case for omp clauses. */
27783 static tree
27784 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27786 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27787 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27788 return list;
27791 /* OpenACC 2.0:
27792 copy ( variable-list )
27793 copyin ( variable-list )
27794 copyout ( variable-list )
27795 create ( variable-list )
27796 delete ( variable-list )
27797 present ( variable-list )
27798 present_or_copy ( variable-list )
27799 pcopy ( variable-list )
27800 present_or_copyin ( variable-list )
27801 pcopyin ( variable-list )
27802 present_or_copyout ( variable-list )
27803 pcopyout ( variable-list )
27804 present_or_create ( variable-list )
27805 pcreate ( variable-list ) */
27807 static tree
27808 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
27809 tree list)
27811 enum omp_clause_map_kind kind;
27812 switch (c_kind)
27814 default:
27815 gcc_unreachable ();
27816 case PRAGMA_OMP_CLAUSE_COPY:
27817 kind = OMP_CLAUSE_MAP_FORCE_TOFROM;
27818 break;
27819 case PRAGMA_OMP_CLAUSE_COPYIN:
27820 kind = OMP_CLAUSE_MAP_FORCE_TO;
27821 break;
27822 case PRAGMA_OMP_CLAUSE_COPYOUT:
27823 kind = OMP_CLAUSE_MAP_FORCE_FROM;
27824 break;
27825 case PRAGMA_OMP_CLAUSE_CREATE:
27826 kind = OMP_CLAUSE_MAP_FORCE_ALLOC;
27827 break;
27828 case PRAGMA_OMP_CLAUSE_DELETE:
27829 kind = OMP_CLAUSE_MAP_FORCE_DEALLOC;
27830 break;
27831 case PRAGMA_OMP_CLAUSE_DEVICE:
27832 kind = OMP_CLAUSE_MAP_FORCE_TO;
27833 break;
27834 case PRAGMA_OMP_CLAUSE_HOST:
27835 kind = OMP_CLAUSE_MAP_FORCE_FROM;
27836 break;
27837 case PRAGMA_OMP_CLAUSE_PRESENT:
27838 kind = OMP_CLAUSE_MAP_FORCE_PRESENT;
27839 break;
27840 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY:
27841 kind = OMP_CLAUSE_MAP_TOFROM;
27842 break;
27843 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN:
27844 kind = OMP_CLAUSE_MAP_TO;
27845 break;
27846 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT:
27847 kind = OMP_CLAUSE_MAP_FROM;
27848 break;
27849 case PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE:
27850 kind = OMP_CLAUSE_MAP_ALLOC;
27851 break;
27852 case PRAGMA_OMP_CLAUSE_SELF:
27853 kind = OMP_CLAUSE_MAP_FORCE_FROM;
27854 break;
27856 tree nl, c;
27857 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
27859 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
27860 OMP_CLAUSE_MAP_KIND (c) = kind;
27862 return nl;
27865 /* OpenACC 2.0:
27866 deviceptr ( variable-list ) */
27868 static tree
27869 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
27871 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27872 tree vars, t;
27874 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
27875 cp_parser_oacc_data_clause), as for PRAGMA_OMP_CLAUSE_DEVICEPTR,
27876 variable-list must only allow for pointer variables. */
27877 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27878 for (t = vars; t; t = TREE_CHAIN (t))
27880 tree v = TREE_PURPOSE (t);
27882 /* FIXME diagnostics: Ideally we should keep individual
27883 locations for all the variables in the var list to make the
27884 following errors more precise. Perhaps
27885 c_parser_omp_var_list_parens should construct a list of
27886 locations to go along with the var list. */
27888 if (TREE_CODE (v) != VAR_DECL)
27889 error_at (loc, "%qD is not a variable", v);
27890 else if (TREE_TYPE (v) == error_mark_node)
27892 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
27893 error_at (loc, "%qD is not a pointer variable", v);
27895 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
27896 OMP_CLAUSE_MAP_KIND (u) = OMP_CLAUSE_MAP_FORCE_DEVICEPTR;
27897 OMP_CLAUSE_DECL (u) = v;
27898 OMP_CLAUSE_CHAIN (u) = list;
27899 list = u;
27902 return list;
27905 /* OpenACC:
27906 vector_length ( expression ) */
27908 static tree
27909 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
27911 tree t, c;
27912 location_t location = cp_lexer_peek_token (parser->lexer)->location;
27913 bool error = false;
27914 HOST_WIDE_INT n;
27916 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27917 return list;
27919 t = cp_parser_condition (parser);
27920 if (t == error_mark_node
27921 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
27922 || !tree_fits_shwi_p (t)
27923 || (n = tree_to_shwi (t)) <= 0
27924 || (int) n != n)
27926 error_at (location, "expected positive integer expression");
27927 error = true;
27930 if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27932 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27933 /*or_comma=*/false,
27934 /*consume_paren=*/true);
27935 return list;
27938 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH,
27939 "vector_length", location);
27941 c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
27942 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
27943 OMP_CLAUSE_CHAIN (c) = list;
27944 list = c;
27946 return list;
27949 /* OpenACC 2.0
27950 Parse wait clause or directive parameters. */
27952 static tree
27953 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
27955 vec<tree, va_gc> *args;
27956 tree t, args_tree;
27958 args = cp_parser_parenthesized_expression_list (parser, non_attr,
27959 /*cast_p=*/false,
27960 /*allow_expansion_p=*/true,
27961 /*non_constant_p=*/NULL);
27963 if (args == NULL || args->length() == 0)
27965 cp_parser_error (parser, "expected integer expression before ')'");
27966 if (args != NULL)
27967 release_tree_vector (args);
27968 return list;
27971 args_tree = build_tree_list_vec (args);
27973 release_tree_vector (args);
27975 for (t = args_tree; t; t = TREE_CHAIN (t))
27977 tree targ = TREE_VALUE (t);
27979 if (targ != error_mark_node)
27981 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
27983 error("%<wait%> expression must be integral");
27984 targ = error_mark_node;
27986 else
27988 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
27990 mark_rvalue_use (targ);
27992 OMP_CLAUSE_DECL (c) = targ;
27993 OMP_CLAUSE_CHAIN (c) = list;
27994 list = c;
27999 return list;
28002 /* OpenACC:
28003 wait ( int-expr-list ) */
28005 static tree
28006 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
28008 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28010 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
28011 return list;
28013 list = cp_parser_oacc_wait_list (parser, location, list);
28015 return list;
28018 /* OpenMP 3.0:
28019 collapse ( constant-expression ) */
28021 static tree
28022 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28024 tree c, num;
28025 location_t loc;
28026 HOST_WIDE_INT n;
28028 loc = cp_lexer_peek_token (parser->lexer)->location;
28029 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28030 return list;
28032 num = cp_parser_constant_expression (parser);
28034 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28035 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28036 /*or_comma=*/false,
28037 /*consume_paren=*/true);
28039 if (num == error_mark_node)
28040 return list;
28041 num = fold_non_dependent_expr (num);
28042 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28043 || !tree_fits_shwi_p (num)
28044 || (n = tree_to_shwi (num)) <= 0
28045 || (int) n != n)
28047 error_at (loc, "collapse argument needs positive constant integer expression");
28048 return list;
28051 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28052 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28053 OMP_CLAUSE_CHAIN (c) = list;
28054 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28056 return c;
28059 /* OpenMP 2.5:
28060 default ( shared | none ) */
28062 static tree
28063 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28065 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28066 tree c;
28068 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28069 return list;
28070 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28072 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28073 const char *p = IDENTIFIER_POINTER (id);
28075 switch (p[0])
28077 case 'n':
28078 if (strcmp ("none", p) != 0)
28079 goto invalid_kind;
28080 kind = OMP_CLAUSE_DEFAULT_NONE;
28081 break;
28083 case 's':
28084 if (strcmp ("shared", p) != 0)
28085 goto invalid_kind;
28086 kind = OMP_CLAUSE_DEFAULT_SHARED;
28087 break;
28089 default:
28090 goto invalid_kind;
28093 cp_lexer_consume_token (parser->lexer);
28095 else
28097 invalid_kind:
28098 cp_parser_error (parser, "expected %<none%> or %<shared%>");
28101 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28102 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28103 /*or_comma=*/false,
28104 /*consume_paren=*/true);
28106 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28107 return list;
28109 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28110 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28111 OMP_CLAUSE_CHAIN (c) = list;
28112 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28114 return c;
28117 /* OpenMP 3.1:
28118 final ( expression ) */
28120 static tree
28121 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28123 tree t, c;
28125 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28126 return list;
28128 t = cp_parser_condition (parser);
28130 if (t == error_mark_node
28131 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28132 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28133 /*or_comma=*/false,
28134 /*consume_paren=*/true);
28136 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28138 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28139 OMP_CLAUSE_FINAL_EXPR (c) = t;
28140 OMP_CLAUSE_CHAIN (c) = list;
28142 return c;
28145 /* OpenMP 2.5:
28146 if ( expression ) */
28148 static tree
28149 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28151 tree t, c;
28153 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28154 return list;
28156 t = cp_parser_condition (parser);
28158 if (t == error_mark_node
28159 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28160 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28161 /*or_comma=*/false,
28162 /*consume_paren=*/true);
28164 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28166 c = build_omp_clause (location, OMP_CLAUSE_IF);
28167 OMP_CLAUSE_IF_EXPR (c) = t;
28168 OMP_CLAUSE_CHAIN (c) = list;
28170 return c;
28173 /* OpenMP 3.1:
28174 mergeable */
28176 static tree
28177 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28178 tree list, location_t location)
28180 tree c;
28182 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28183 location);
28185 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28186 OMP_CLAUSE_CHAIN (c) = list;
28187 return c;
28190 /* OpenMP 2.5:
28191 nowait */
28193 static tree
28194 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28195 tree list, location_t location)
28197 tree c;
28199 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28201 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28202 OMP_CLAUSE_CHAIN (c) = list;
28203 return c;
28206 /* OpenACC:
28207 num_gangs ( expression ) */
28209 static tree
28210 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28212 tree t, c;
28213 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28214 HOST_WIDE_INT n;
28216 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28217 return list;
28219 t = cp_parser_condition (parser);
28221 if (t == error_mark_node
28222 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28223 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28224 /*or_comma=*/false,
28225 /*consume_paren=*/true);
28227 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
28228 || !tree_fits_shwi_p (t)
28229 || (n = tree_to_shwi (t)) <= 0
28230 || (int) n != n)
28232 error_at (location, "expected positive integer expression");
28233 return list;
28236 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28238 c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28239 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28240 OMP_CLAUSE_CHAIN (c) = list;
28241 list = c;
28243 return list;
28246 /* OpenMP 2.5:
28247 num_threads ( expression ) */
28249 static tree
28250 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28251 location_t location)
28253 tree t, c;
28255 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28256 return list;
28258 t = cp_parser_expression (parser);
28260 if (t == error_mark_node
28261 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28262 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28263 /*or_comma=*/false,
28264 /*consume_paren=*/true);
28266 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28267 "num_threads", location);
28269 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28270 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28271 OMP_CLAUSE_CHAIN (c) = list;
28273 return c;
28276 /* OpenACC:
28277 num_workers ( expression ) */
28279 static tree
28280 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28282 tree t, c;
28283 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28284 HOST_WIDE_INT n;
28286 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28287 return list;
28289 t = cp_parser_condition (parser);
28291 if (t == error_mark_node
28292 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28293 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28294 /*or_comma=*/false,
28295 /*consume_paren=*/true);
28297 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
28298 || !tree_fits_shwi_p (t)
28299 || (n = tree_to_shwi (t)) <= 0
28300 || (int) n != n)
28302 error_at (location, "expected positive integer expression");
28303 return list;
28306 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28307 location);
28309 c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28310 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28311 OMP_CLAUSE_CHAIN (c) = list;
28312 list = c;
28314 return list;
28317 /* OpenMP 2.5:
28318 ordered */
28320 static tree
28321 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28322 tree list, location_t location)
28324 tree c;
28326 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28327 "ordered", location);
28329 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28330 OMP_CLAUSE_CHAIN (c) = list;
28331 return c;
28334 /* OpenMP 2.5:
28335 reduction ( reduction-operator : variable-list )
28337 reduction-operator:
28338 One of: + * - & ^ | && ||
28340 OpenMP 3.1:
28342 reduction-operator:
28343 One of: + * - & ^ | && || min max
28345 OpenMP 4.0:
28347 reduction-operator:
28348 One of: + * - & ^ | && ||
28349 id-expression */
28351 static tree
28352 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28354 enum tree_code code = ERROR_MARK;
28355 tree nlist, c, id = NULL_TREE;
28357 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28358 return list;
28360 switch (cp_lexer_peek_token (parser->lexer)->type)
28362 case CPP_PLUS: code = PLUS_EXPR; break;
28363 case CPP_MULT: code = MULT_EXPR; break;
28364 case CPP_MINUS: code = MINUS_EXPR; break;
28365 case CPP_AND: code = BIT_AND_EXPR; break;
28366 case CPP_XOR: code = BIT_XOR_EXPR; break;
28367 case CPP_OR: code = BIT_IOR_EXPR; break;
28368 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28369 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28370 default: break;
28373 if (code != ERROR_MARK)
28374 cp_lexer_consume_token (parser->lexer);
28375 else
28377 bool saved_colon_corrects_to_scope_p;
28378 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28379 parser->colon_corrects_to_scope_p = false;
28380 id = cp_parser_id_expression (parser, /*template_p=*/false,
28381 /*check_dependency_p=*/true,
28382 /*template_p=*/NULL,
28383 /*declarator_p=*/false,
28384 /*optional_p=*/false);
28385 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28386 if (identifier_p (id))
28388 const char *p = IDENTIFIER_POINTER (id);
28390 if (strcmp (p, "min") == 0)
28391 code = MIN_EXPR;
28392 else if (strcmp (p, "max") == 0)
28393 code = MAX_EXPR;
28394 else if (id == ansi_opname (PLUS_EXPR))
28395 code = PLUS_EXPR;
28396 else if (id == ansi_opname (MULT_EXPR))
28397 code = MULT_EXPR;
28398 else if (id == ansi_opname (MINUS_EXPR))
28399 code = MINUS_EXPR;
28400 else if (id == ansi_opname (BIT_AND_EXPR))
28401 code = BIT_AND_EXPR;
28402 else if (id == ansi_opname (BIT_IOR_EXPR))
28403 code = BIT_IOR_EXPR;
28404 else if (id == ansi_opname (BIT_XOR_EXPR))
28405 code = BIT_XOR_EXPR;
28406 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28407 code = TRUTH_ANDIF_EXPR;
28408 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28409 code = TRUTH_ORIF_EXPR;
28410 id = omp_reduction_id (code, id, NULL_TREE);
28411 tree scope = parser->scope;
28412 if (scope)
28413 id = build_qualified_name (NULL_TREE, scope, id, false);
28414 parser->scope = NULL_TREE;
28415 parser->qualifying_scope = NULL_TREE;
28416 parser->object_scope = NULL_TREE;
28418 else
28420 error ("invalid reduction-identifier");
28421 resync_fail:
28422 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28423 /*or_comma=*/false,
28424 /*consume_paren=*/true);
28425 return list;
28429 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28430 goto resync_fail;
28432 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28433 NULL);
28434 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28436 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28437 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28440 return nlist;
28443 /* OpenMP 2.5:
28444 schedule ( schedule-kind )
28445 schedule ( schedule-kind , expression )
28447 schedule-kind:
28448 static | dynamic | guided | runtime | auto */
28450 static tree
28451 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28453 tree c, t;
28455 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28456 return list;
28458 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28460 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28462 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28463 const char *p = IDENTIFIER_POINTER (id);
28465 switch (p[0])
28467 case 'd':
28468 if (strcmp ("dynamic", p) != 0)
28469 goto invalid_kind;
28470 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28471 break;
28473 case 'g':
28474 if (strcmp ("guided", p) != 0)
28475 goto invalid_kind;
28476 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28477 break;
28479 case 'r':
28480 if (strcmp ("runtime", p) != 0)
28481 goto invalid_kind;
28482 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28483 break;
28485 default:
28486 goto invalid_kind;
28489 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28490 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28491 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28492 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28493 else
28494 goto invalid_kind;
28495 cp_lexer_consume_token (parser->lexer);
28497 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28499 cp_token *token;
28500 cp_lexer_consume_token (parser->lexer);
28502 token = cp_lexer_peek_token (parser->lexer);
28503 t = cp_parser_assignment_expression (parser);
28505 if (t == error_mark_node)
28506 goto resync_fail;
28507 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28508 error_at (token->location, "schedule %<runtime%> does not take "
28509 "a %<chunk_size%> parameter");
28510 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28511 error_at (token->location, "schedule %<auto%> does not take "
28512 "a %<chunk_size%> parameter");
28513 else
28514 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28516 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28517 goto resync_fail;
28519 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28520 goto resync_fail;
28522 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28523 OMP_CLAUSE_CHAIN (c) = list;
28524 return c;
28526 invalid_kind:
28527 cp_parser_error (parser, "invalid schedule kind");
28528 resync_fail:
28529 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28530 /*or_comma=*/false,
28531 /*consume_paren=*/true);
28532 return list;
28535 /* OpenMP 3.0:
28536 untied */
28538 static tree
28539 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28540 tree list, location_t location)
28542 tree c;
28544 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28546 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28547 OMP_CLAUSE_CHAIN (c) = list;
28548 return c;
28551 /* OpenMP 4.0:
28552 inbranch
28553 notinbranch */
28555 static tree
28556 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28557 tree list, location_t location)
28559 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28560 tree c = build_omp_clause (location, code);
28561 OMP_CLAUSE_CHAIN (c) = list;
28562 return c;
28565 /* OpenMP 4.0:
28566 parallel
28568 sections
28569 taskgroup */
28571 static tree
28572 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28573 enum omp_clause_code code,
28574 tree list, location_t location)
28576 tree c = build_omp_clause (location, code);
28577 OMP_CLAUSE_CHAIN (c) = list;
28578 return c;
28581 /* OpenMP 4.0:
28582 num_teams ( expression ) */
28584 static tree
28585 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28586 location_t location)
28588 tree t, c;
28590 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28591 return list;
28593 t = cp_parser_expression (parser);
28595 if (t == error_mark_node
28596 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28597 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28598 /*or_comma=*/false,
28599 /*consume_paren=*/true);
28601 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28602 "num_teams", location);
28604 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28605 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28606 OMP_CLAUSE_CHAIN (c) = list;
28608 return c;
28611 /* OpenMP 4.0:
28612 thread_limit ( expression ) */
28614 static tree
28615 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28616 location_t location)
28618 tree t, c;
28620 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28621 return list;
28623 t = cp_parser_expression (parser);
28625 if (t == error_mark_node
28626 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28627 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28628 /*or_comma=*/false,
28629 /*consume_paren=*/true);
28631 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28632 "thread_limit", location);
28634 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28635 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28636 OMP_CLAUSE_CHAIN (c) = list;
28638 return c;
28641 /* OpenMP 4.0:
28642 aligned ( variable-list )
28643 aligned ( variable-list : constant-expression ) */
28645 static tree
28646 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28648 tree nlist, c, alignment = NULL_TREE;
28649 bool colon;
28651 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28652 return list;
28654 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28655 &colon);
28657 if (colon)
28659 alignment = cp_parser_constant_expression (parser);
28661 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28662 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28663 /*or_comma=*/false,
28664 /*consume_paren=*/true);
28666 if (alignment == error_mark_node)
28667 alignment = NULL_TREE;
28670 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28671 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28673 return nlist;
28676 /* OpenMP 4.0:
28677 linear ( variable-list )
28678 linear ( variable-list : expression ) */
28680 static tree
28681 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28682 bool is_cilk_simd_fn)
28684 tree nlist, c, step = integer_one_node;
28685 bool colon;
28687 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28688 return list;
28690 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28691 &colon);
28693 if (colon)
28695 step = cp_parser_expression (parser);
28697 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28699 sorry ("using parameters for %<linear%> step is not supported yet");
28700 step = integer_one_node;
28702 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28703 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28704 /*or_comma=*/false,
28705 /*consume_paren=*/true);
28707 if (step == error_mark_node)
28708 return list;
28711 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28712 OMP_CLAUSE_LINEAR_STEP (c) = step;
28714 return nlist;
28717 /* OpenMP 4.0:
28718 safelen ( constant-expression ) */
28720 static tree
28721 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28722 location_t location)
28724 tree t, c;
28726 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28727 return list;
28729 t = cp_parser_constant_expression (parser);
28731 if (t == error_mark_node
28732 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28733 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28734 /*or_comma=*/false,
28735 /*consume_paren=*/true);
28737 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28739 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28740 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28741 OMP_CLAUSE_CHAIN (c) = list;
28743 return c;
28746 /* OpenMP 4.0:
28747 simdlen ( constant-expression ) */
28749 static tree
28750 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28751 location_t location)
28753 tree t, c;
28755 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28756 return list;
28758 t = cp_parser_constant_expression (parser);
28760 if (t == error_mark_node
28761 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28762 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28763 /*or_comma=*/false,
28764 /*consume_paren=*/true);
28766 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28768 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28769 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28770 OMP_CLAUSE_CHAIN (c) = list;
28772 return c;
28775 /* OpenMP 4.0:
28776 depend ( depend-kind : variable-list )
28778 depend-kind:
28779 in | out | inout */
28781 static tree
28782 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28784 tree nlist, c;
28785 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28787 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28788 return list;
28790 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28792 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28793 const char *p = IDENTIFIER_POINTER (id);
28795 if (strcmp ("in", p) == 0)
28796 kind = OMP_CLAUSE_DEPEND_IN;
28797 else if (strcmp ("inout", p) == 0)
28798 kind = OMP_CLAUSE_DEPEND_INOUT;
28799 else if (strcmp ("out", p) == 0)
28800 kind = OMP_CLAUSE_DEPEND_OUT;
28801 else
28802 goto invalid_kind;
28804 else
28805 goto invalid_kind;
28807 cp_lexer_consume_token (parser->lexer);
28808 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28809 goto resync_fail;
28811 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28812 NULL);
28814 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28815 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28817 return nlist;
28819 invalid_kind:
28820 cp_parser_error (parser, "invalid depend kind");
28821 resync_fail:
28822 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28823 /*or_comma=*/false,
28824 /*consume_paren=*/true);
28825 return list;
28828 /* OpenMP 4.0:
28829 map ( map-kind : variable-list )
28830 map ( variable-list )
28832 map-kind:
28833 alloc | to | from | tofrom */
28835 static tree
28836 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28838 tree nlist, c;
28839 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28841 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28842 return list;
28844 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28845 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28847 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28848 const char *p = IDENTIFIER_POINTER (id);
28850 if (strcmp ("alloc", p) == 0)
28851 kind = OMP_CLAUSE_MAP_ALLOC;
28852 else if (strcmp ("to", p) == 0)
28853 kind = OMP_CLAUSE_MAP_TO;
28854 else if (strcmp ("from", p) == 0)
28855 kind = OMP_CLAUSE_MAP_FROM;
28856 else if (strcmp ("tofrom", p) == 0)
28857 kind = OMP_CLAUSE_MAP_TOFROM;
28858 else
28860 cp_parser_error (parser, "invalid map kind");
28861 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28862 /*or_comma=*/false,
28863 /*consume_paren=*/true);
28864 return list;
28866 cp_lexer_consume_token (parser->lexer);
28867 cp_lexer_consume_token (parser->lexer);
28870 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28871 NULL);
28873 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28874 OMP_CLAUSE_MAP_KIND (c) = kind;
28876 return nlist;
28879 /* OpenMP 4.0:
28880 device ( expression ) */
28882 static tree
28883 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28884 location_t location)
28886 tree t, c;
28888 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28889 return list;
28891 t = cp_parser_expression (parser);
28893 if (t == error_mark_node
28894 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28895 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28896 /*or_comma=*/false,
28897 /*consume_paren=*/true);
28899 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28900 "device", location);
28902 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28903 OMP_CLAUSE_DEVICE_ID (c) = t;
28904 OMP_CLAUSE_CHAIN (c) = list;
28906 return c;
28909 /* OpenMP 4.0:
28910 dist_schedule ( static )
28911 dist_schedule ( static , expression ) */
28913 static tree
28914 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28915 location_t location)
28917 tree c, t;
28919 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28920 return list;
28922 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28924 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28925 goto invalid_kind;
28926 cp_lexer_consume_token (parser->lexer);
28928 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28930 cp_lexer_consume_token (parser->lexer);
28932 t = cp_parser_assignment_expression (parser);
28934 if (t == error_mark_node)
28935 goto resync_fail;
28936 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28938 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28939 goto resync_fail;
28941 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28942 goto resync_fail;
28944 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28945 location);
28946 OMP_CLAUSE_CHAIN (c) = list;
28947 return c;
28949 invalid_kind:
28950 cp_parser_error (parser, "invalid dist_schedule kind");
28951 resync_fail:
28952 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28953 /*or_comma=*/false,
28954 /*consume_paren=*/true);
28955 return list;
28958 /* OpenMP 4.0:
28959 proc_bind ( proc-bind-kind )
28961 proc-bind-kind:
28962 master | close | spread */
28964 static tree
28965 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28966 location_t location)
28968 tree c;
28969 enum omp_clause_proc_bind_kind kind;
28971 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28972 return list;
28974 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28976 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28977 const char *p = IDENTIFIER_POINTER (id);
28979 if (strcmp ("master", p) == 0)
28980 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28981 else if (strcmp ("close", p) == 0)
28982 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28983 else if (strcmp ("spread", p) == 0)
28984 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28985 else
28986 goto invalid_kind;
28988 else
28989 goto invalid_kind;
28991 cp_lexer_consume_token (parser->lexer);
28992 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28993 goto resync_fail;
28995 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28996 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28997 location);
28998 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28999 OMP_CLAUSE_CHAIN (c) = list;
29000 return c;
29002 invalid_kind:
29003 cp_parser_error (parser, "invalid depend kind");
29004 resync_fail:
29005 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29006 /*or_comma=*/false,
29007 /*consume_paren=*/true);
29008 return list;
29011 /* OpenACC:
29012 async [( int-expr )] */
29014 static tree
29015 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29017 tree c, t;
29018 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29020 /* TODO XXX: FIX -1 (acc_async_noval). */
29021 t = build_int_cst (integer_type_node, -1);
29023 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29025 cp_lexer_consume_token (parser->lexer);
29027 t = cp_parser_expression (parser);
29028 if (t == error_mark_node
29029 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29030 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29031 /*or_comma=*/false,
29032 /*consume_paren=*/true);
29035 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29037 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29038 OMP_CLAUSE_ASYNC_EXPR (c) = t;
29039 OMP_CLAUSE_CHAIN (c) = list;
29040 list = c;
29042 return list;
29045 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29046 is a bitmask in MASK. Return the list of clauses found. */
29048 static tree
29049 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29050 const char *where, cp_token *pragma_tok,
29051 bool finish_p = true)
29053 tree clauses = NULL;
29054 bool first = true;
29056 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29058 location_t here;
29059 pragma_omp_clause c_kind;
29060 const char *c_name;
29061 tree prev = clauses;
29063 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29064 cp_lexer_consume_token (parser->lexer);
29066 here = cp_lexer_peek_token (parser->lexer)->location;
29067 c_kind = cp_parser_omp_clause_name (parser);
29069 switch (c_kind)
29071 case PRAGMA_OMP_CLAUSE_ASYNC:
29072 clauses = cp_parser_oacc_clause_async (parser, clauses);
29073 c_name = "async";
29074 break;
29075 case PRAGMA_OMP_CLAUSE_COLLAPSE:
29076 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29077 c_name = "collapse";
29078 break;
29079 case PRAGMA_OMP_CLAUSE_COPY:
29080 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29081 c_name = "copy";
29082 break;
29083 case PRAGMA_OMP_CLAUSE_COPYIN:
29084 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29085 c_name = "copyin";
29086 break;
29087 case PRAGMA_OMP_CLAUSE_COPYOUT:
29088 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29089 c_name = "copyout";
29090 break;
29091 case PRAGMA_OMP_CLAUSE_CREATE:
29092 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29093 c_name = "create";
29094 break;
29095 case PRAGMA_OMP_CLAUSE_DELETE:
29096 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29097 c_name = "delete";
29098 break;
29099 case PRAGMA_OMP_CLAUSE_DEVICE:
29100 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29101 c_name = "device";
29102 break;
29103 case PRAGMA_OMP_CLAUSE_DEVICEPTR:
29104 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29105 c_name = "deviceptr";
29106 break;
29107 case PRAGMA_OMP_CLAUSE_HOST:
29108 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29109 c_name = "host";
29110 break;
29111 case PRAGMA_OMP_CLAUSE_IF:
29112 clauses = cp_parser_omp_clause_if (parser, clauses, here);
29113 c_name = "if";
29114 break;
29115 case PRAGMA_OMP_CLAUSE_NUM_GANGS:
29116 clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29117 c_name = "num_gangs";
29118 break;
29119 case PRAGMA_OMP_CLAUSE_NUM_WORKERS:
29120 clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29121 c_name = "num_workers";
29122 break;
29123 case PRAGMA_OMP_CLAUSE_PRESENT:
29124 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29125 c_name = "present";
29126 break;
29127 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY:
29128 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29129 c_name = "present_or_copy";
29130 break;
29131 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN:
29132 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29133 c_name = "present_or_copyin";
29134 break;
29135 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT:
29136 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29137 c_name = "present_or_copyout";
29138 break;
29139 case PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE:
29140 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29141 c_name = "present_or_create";
29142 break;
29143 case PRAGMA_OMP_CLAUSE_REDUCTION:
29144 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29145 c_name = "reduction";
29146 break;
29147 case PRAGMA_OMP_CLAUSE_SELF:
29148 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29149 c_name = "self";
29150 break;
29151 case PRAGMA_OMP_CLAUSE_VECTOR_LENGTH:
29152 clauses =
29153 cp_parser_oacc_clause_vector_length (parser, clauses);
29154 c_name = "vector_length";
29155 break;
29156 case PRAGMA_OMP_CLAUSE_WAIT:
29157 clauses = cp_parser_oacc_clause_wait (parser, clauses);
29158 c_name = "wait";
29159 break;
29160 default:
29161 cp_parser_error (parser, "expected clause");
29162 goto saw_error;
29165 first = false;
29167 if (((mask >> c_kind) & 1) == 0)
29169 /* Remove the invalid clause(s) from the list to avoid
29170 confusing the rest of the compiler. */
29171 clauses = prev;
29172 error_at (here, "%qs is not valid for %qs", c_name, where);
29176 saw_error:
29177 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29179 if (finish_p)
29180 return finish_omp_clauses (clauses);
29182 return clauses;
29185 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29186 is a bitmask in MASK. Return the list of clauses found; the result
29187 of clause default goes in *pdefault. */
29189 static tree
29190 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29191 const char *where, cp_token *pragma_tok,
29192 bool finish_p = true)
29194 tree clauses = NULL;
29195 bool first = true;
29196 cp_token *token = NULL;
29197 bool cilk_simd_fn = false;
29199 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29201 pragma_omp_clause c_kind;
29202 const char *c_name;
29203 tree prev = clauses;
29205 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29206 cp_lexer_consume_token (parser->lexer);
29208 token = cp_lexer_peek_token (parser->lexer);
29209 c_kind = cp_parser_omp_clause_name (parser);
29211 switch (c_kind)
29213 case PRAGMA_OMP_CLAUSE_COLLAPSE:
29214 clauses = cp_parser_omp_clause_collapse (parser, clauses,
29215 token->location);
29216 c_name = "collapse";
29217 break;
29218 case PRAGMA_OMP_CLAUSE_COPYIN:
29219 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29220 c_name = "copyin";
29221 break;
29222 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29223 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29224 clauses);
29225 c_name = "copyprivate";
29226 break;
29227 case PRAGMA_OMP_CLAUSE_DEFAULT:
29228 clauses = cp_parser_omp_clause_default (parser, clauses,
29229 token->location);
29230 c_name = "default";
29231 break;
29232 case PRAGMA_OMP_CLAUSE_FINAL:
29233 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29234 c_name = "final";
29235 break;
29236 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29237 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29238 clauses);
29239 c_name = "firstprivate";
29240 break;
29241 case PRAGMA_OMP_CLAUSE_IF:
29242 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29243 c_name = "if";
29244 break;
29245 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29246 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29247 clauses);
29248 c_name = "lastprivate";
29249 break;
29250 case PRAGMA_OMP_CLAUSE_MERGEABLE:
29251 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29252 token->location);
29253 c_name = "mergeable";
29254 break;
29255 case PRAGMA_OMP_CLAUSE_NOWAIT:
29256 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29257 c_name = "nowait";
29258 break;
29259 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29260 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29261 token->location);
29262 c_name = "num_threads";
29263 break;
29264 case PRAGMA_OMP_CLAUSE_ORDERED:
29265 clauses = cp_parser_omp_clause_ordered (parser, clauses,
29266 token->location);
29267 c_name = "ordered";
29268 break;
29269 case PRAGMA_OMP_CLAUSE_PRIVATE:
29270 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29271 clauses);
29272 c_name = "private";
29273 break;
29274 case PRAGMA_OMP_CLAUSE_REDUCTION:
29275 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29276 c_name = "reduction";
29277 break;
29278 case PRAGMA_OMP_CLAUSE_SCHEDULE:
29279 clauses = cp_parser_omp_clause_schedule (parser, clauses,
29280 token->location);
29281 c_name = "schedule";
29282 break;
29283 case PRAGMA_OMP_CLAUSE_SHARED:
29284 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29285 clauses);
29286 c_name = "shared";
29287 break;
29288 case PRAGMA_OMP_CLAUSE_UNTIED:
29289 clauses = cp_parser_omp_clause_untied (parser, clauses,
29290 token->location);
29291 c_name = "untied";
29292 break;
29293 case PRAGMA_OMP_CLAUSE_INBRANCH:
29294 case PRAGMA_CILK_CLAUSE_MASK:
29295 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29296 clauses, token->location);
29297 c_name = "inbranch";
29298 break;
29299 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29300 case PRAGMA_CILK_CLAUSE_NOMASK:
29301 clauses = cp_parser_omp_clause_branch (parser,
29302 OMP_CLAUSE_NOTINBRANCH,
29303 clauses, token->location);
29304 c_name = "notinbranch";
29305 break;
29306 case PRAGMA_OMP_CLAUSE_PARALLEL:
29307 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29308 clauses, token->location);
29309 c_name = "parallel";
29310 if (!first)
29312 clause_not_first:
29313 error_at (token->location, "%qs must be the first clause of %qs",
29314 c_name, where);
29315 clauses = prev;
29317 break;
29318 case PRAGMA_OMP_CLAUSE_FOR:
29319 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29320 clauses, token->location);
29321 c_name = "for";
29322 if (!first)
29323 goto clause_not_first;
29324 break;
29325 case PRAGMA_OMP_CLAUSE_SECTIONS:
29326 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29327 clauses, token->location);
29328 c_name = "sections";
29329 if (!first)
29330 goto clause_not_first;
29331 break;
29332 case PRAGMA_OMP_CLAUSE_TASKGROUP:
29333 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29334 clauses, token->location);
29335 c_name = "taskgroup";
29336 if (!first)
29337 goto clause_not_first;
29338 break;
29339 case PRAGMA_OMP_CLAUSE_TO:
29340 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29341 clauses);
29342 c_name = "to";
29343 break;
29344 case PRAGMA_OMP_CLAUSE_FROM:
29345 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29346 clauses);
29347 c_name = "from";
29348 break;
29349 case PRAGMA_OMP_CLAUSE_UNIFORM:
29350 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29351 clauses);
29352 c_name = "uniform";
29353 break;
29354 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29355 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29356 token->location);
29357 c_name = "num_teams";
29358 break;
29359 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29360 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29361 token->location);
29362 c_name = "thread_limit";
29363 break;
29364 case PRAGMA_OMP_CLAUSE_ALIGNED:
29365 clauses = cp_parser_omp_clause_aligned (parser, clauses);
29366 c_name = "aligned";
29367 break;
29368 case PRAGMA_OMP_CLAUSE_LINEAR:
29369 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29370 cilk_simd_fn = true;
29371 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29372 c_name = "linear";
29373 break;
29374 case PRAGMA_OMP_CLAUSE_DEPEND:
29375 clauses = cp_parser_omp_clause_depend (parser, clauses);
29376 c_name = "depend";
29377 break;
29378 case PRAGMA_OMP_CLAUSE_MAP:
29379 clauses = cp_parser_omp_clause_map (parser, clauses);
29380 c_name = "map";
29381 break;
29382 case PRAGMA_OMP_CLAUSE_DEVICE:
29383 clauses = cp_parser_omp_clause_device (parser, clauses,
29384 token->location);
29385 c_name = "device";
29386 break;
29387 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29388 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29389 token->location);
29390 c_name = "dist_schedule";
29391 break;
29392 case PRAGMA_OMP_CLAUSE_PROC_BIND:
29393 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29394 token->location);
29395 c_name = "proc_bind";
29396 break;
29397 case PRAGMA_OMP_CLAUSE_SAFELEN:
29398 clauses = cp_parser_omp_clause_safelen (parser, clauses,
29399 token->location);
29400 c_name = "safelen";
29401 break;
29402 case PRAGMA_OMP_CLAUSE_SIMDLEN:
29403 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29404 token->location);
29405 c_name = "simdlen";
29406 break;
29407 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29408 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29409 c_name = "simdlen";
29410 break;
29411 default:
29412 cp_parser_error (parser, "expected clause");
29413 goto saw_error;
29416 first = false;
29418 if (((mask >> c_kind) & 1) == 0)
29420 /* Remove the invalid clause(s) from the list to avoid
29421 confusing the rest of the compiler. */
29422 clauses = prev;
29423 error_at (token->location, "%qs is not valid for %qs", c_name, where);
29426 saw_error:
29427 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29428 no reason to skip to the end. */
29429 if (!(flag_cilkplus && pragma_tok == NULL))
29430 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29431 if (finish_p)
29432 return finish_omp_clauses (clauses);
29433 return clauses;
29436 /* OpenMP 2.5:
29437 structured-block:
29438 statement
29440 In practice, we're also interested in adding the statement to an
29441 outer node. So it is convenient if we work around the fact that
29442 cp_parser_statement calls add_stmt. */
29444 static unsigned
29445 cp_parser_begin_omp_structured_block (cp_parser *parser)
29447 unsigned save = parser->in_statement;
29449 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29450 This preserves the "not within loop or switch" style error messages
29451 for nonsense cases like
29452 void foo() {
29453 #pragma omp single
29454 break;
29457 if (parser->in_statement)
29458 parser->in_statement = IN_OMP_BLOCK;
29460 return save;
29463 static void
29464 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29466 parser->in_statement = save;
29469 static tree
29470 cp_parser_omp_structured_block (cp_parser *parser)
29472 tree stmt = begin_omp_structured_block ();
29473 unsigned int save = cp_parser_begin_omp_structured_block (parser);
29475 cp_parser_statement (parser, NULL_TREE, false, NULL);
29477 cp_parser_end_omp_structured_block (parser, save);
29478 return finish_omp_structured_block (stmt);
29481 /* OpenMP 2.5:
29482 # pragma omp atomic new-line
29483 expression-stmt
29485 expression-stmt:
29486 x binop= expr | x++ | ++x | x-- | --x
29487 binop:
29488 +, *, -, /, &, ^, |, <<, >>
29490 where x is an lvalue expression with scalar type.
29492 OpenMP 3.1:
29493 # pragma omp atomic new-line
29494 update-stmt
29496 # pragma omp atomic read new-line
29497 read-stmt
29499 # pragma omp atomic write new-line
29500 write-stmt
29502 # pragma omp atomic update new-line
29503 update-stmt
29505 # pragma omp atomic capture new-line
29506 capture-stmt
29508 # pragma omp atomic capture new-line
29509 capture-block
29511 read-stmt:
29512 v = x
29513 write-stmt:
29514 x = expr
29515 update-stmt:
29516 expression-stmt | x = x binop expr
29517 capture-stmt:
29518 v = expression-stmt
29519 capture-block:
29520 { v = x; update-stmt; } | { update-stmt; v = x; }
29522 OpenMP 4.0:
29523 update-stmt:
29524 expression-stmt | x = x binop expr | x = expr binop x
29525 capture-stmt:
29526 v = update-stmt
29527 capture-block:
29528 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29530 where x and v are lvalue expressions with scalar type. */
29532 static void
29533 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29535 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29536 tree rhs1 = NULL_TREE, orig_lhs;
29537 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29538 bool structured_block = false;
29539 bool seq_cst = false;
29541 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29543 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29544 const char *p = IDENTIFIER_POINTER (id);
29546 if (!strcmp (p, "seq_cst"))
29548 seq_cst = true;
29549 cp_lexer_consume_token (parser->lexer);
29550 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29551 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29552 cp_lexer_consume_token (parser->lexer);
29555 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29557 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29558 const char *p = IDENTIFIER_POINTER (id);
29560 if (!strcmp (p, "read"))
29561 code = OMP_ATOMIC_READ;
29562 else if (!strcmp (p, "write"))
29563 code = NOP_EXPR;
29564 else if (!strcmp (p, "update"))
29565 code = OMP_ATOMIC;
29566 else if (!strcmp (p, "capture"))
29567 code = OMP_ATOMIC_CAPTURE_NEW;
29568 else
29569 p = NULL;
29570 if (p)
29571 cp_lexer_consume_token (parser->lexer);
29573 if (!seq_cst)
29575 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29576 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29577 cp_lexer_consume_token (parser->lexer);
29579 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29581 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29582 const char *p = IDENTIFIER_POINTER (id);
29584 if (!strcmp (p, "seq_cst"))
29586 seq_cst = true;
29587 cp_lexer_consume_token (parser->lexer);
29591 cp_parser_require_pragma_eol (parser, pragma_tok);
29593 switch (code)
29595 case OMP_ATOMIC_READ:
29596 case NOP_EXPR: /* atomic write */
29597 v = cp_parser_unary_expression (parser);
29598 if (v == error_mark_node)
29599 goto saw_error;
29600 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29601 goto saw_error;
29602 if (code == NOP_EXPR)
29603 lhs = cp_parser_expression (parser);
29604 else
29605 lhs = cp_parser_unary_expression (parser);
29606 if (lhs == error_mark_node)
29607 goto saw_error;
29608 if (code == NOP_EXPR)
29610 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29611 opcode. */
29612 code = OMP_ATOMIC;
29613 rhs = lhs;
29614 lhs = v;
29615 v = NULL_TREE;
29617 goto done;
29618 case OMP_ATOMIC_CAPTURE_NEW:
29619 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29621 cp_lexer_consume_token (parser->lexer);
29622 structured_block = true;
29624 else
29626 v = cp_parser_unary_expression (parser);
29627 if (v == error_mark_node)
29628 goto saw_error;
29629 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29630 goto saw_error;
29632 default:
29633 break;
29636 restart:
29637 lhs = cp_parser_unary_expression (parser);
29638 orig_lhs = lhs;
29639 switch (TREE_CODE (lhs))
29641 case ERROR_MARK:
29642 goto saw_error;
29644 case POSTINCREMENT_EXPR:
29645 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29646 code = OMP_ATOMIC_CAPTURE_OLD;
29647 /* FALLTHROUGH */
29648 case PREINCREMENT_EXPR:
29649 lhs = TREE_OPERAND (lhs, 0);
29650 opcode = PLUS_EXPR;
29651 rhs = integer_one_node;
29652 break;
29654 case POSTDECREMENT_EXPR:
29655 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29656 code = OMP_ATOMIC_CAPTURE_OLD;
29657 /* FALLTHROUGH */
29658 case PREDECREMENT_EXPR:
29659 lhs = TREE_OPERAND (lhs, 0);
29660 opcode = MINUS_EXPR;
29661 rhs = integer_one_node;
29662 break;
29664 case COMPOUND_EXPR:
29665 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29666 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29667 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29668 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29669 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29670 (TREE_OPERAND (lhs, 1), 0), 0)))
29671 == BOOLEAN_TYPE)
29672 /* Undo effects of boolean_increment for post {in,de}crement. */
29673 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29674 /* FALLTHRU */
29675 case MODIFY_EXPR:
29676 if (TREE_CODE (lhs) == MODIFY_EXPR
29677 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29679 /* Undo effects of boolean_increment. */
29680 if (integer_onep (TREE_OPERAND (lhs, 1)))
29682 /* This is pre or post increment. */
29683 rhs = TREE_OPERAND (lhs, 1);
29684 lhs = TREE_OPERAND (lhs, 0);
29685 opcode = NOP_EXPR;
29686 if (code == OMP_ATOMIC_CAPTURE_NEW
29687 && !structured_block
29688 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29689 code = OMP_ATOMIC_CAPTURE_OLD;
29690 break;
29693 /* FALLTHRU */
29694 default:
29695 switch (cp_lexer_peek_token (parser->lexer)->type)
29697 case CPP_MULT_EQ:
29698 opcode = MULT_EXPR;
29699 break;
29700 case CPP_DIV_EQ:
29701 opcode = TRUNC_DIV_EXPR;
29702 break;
29703 case CPP_PLUS_EQ:
29704 opcode = PLUS_EXPR;
29705 break;
29706 case CPP_MINUS_EQ:
29707 opcode = MINUS_EXPR;
29708 break;
29709 case CPP_LSHIFT_EQ:
29710 opcode = LSHIFT_EXPR;
29711 break;
29712 case CPP_RSHIFT_EQ:
29713 opcode = RSHIFT_EXPR;
29714 break;
29715 case CPP_AND_EQ:
29716 opcode = BIT_AND_EXPR;
29717 break;
29718 case CPP_OR_EQ:
29719 opcode = BIT_IOR_EXPR;
29720 break;
29721 case CPP_XOR_EQ:
29722 opcode = BIT_XOR_EXPR;
29723 break;
29724 case CPP_EQ:
29725 enum cp_parser_prec oprec;
29726 cp_token *token;
29727 cp_lexer_consume_token (parser->lexer);
29728 cp_parser_parse_tentatively (parser);
29729 rhs1 = cp_parser_simple_cast_expression (parser);
29730 if (rhs1 == error_mark_node)
29732 cp_parser_abort_tentative_parse (parser);
29733 cp_parser_simple_cast_expression (parser);
29734 goto saw_error;
29736 token = cp_lexer_peek_token (parser->lexer);
29737 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29739 cp_parser_abort_tentative_parse (parser);
29740 cp_parser_parse_tentatively (parser);
29741 rhs = cp_parser_binary_expression (parser, false, true,
29742 PREC_NOT_OPERATOR, NULL);
29743 if (rhs == error_mark_node)
29745 cp_parser_abort_tentative_parse (parser);
29746 cp_parser_binary_expression (parser, false, true,
29747 PREC_NOT_OPERATOR, NULL);
29748 goto saw_error;
29750 switch (TREE_CODE (rhs))
29752 case MULT_EXPR:
29753 case TRUNC_DIV_EXPR:
29754 case PLUS_EXPR:
29755 case MINUS_EXPR:
29756 case LSHIFT_EXPR:
29757 case RSHIFT_EXPR:
29758 case BIT_AND_EXPR:
29759 case BIT_IOR_EXPR:
29760 case BIT_XOR_EXPR:
29761 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29763 if (cp_parser_parse_definitely (parser))
29765 opcode = TREE_CODE (rhs);
29766 rhs1 = TREE_OPERAND (rhs, 0);
29767 rhs = TREE_OPERAND (rhs, 1);
29768 goto stmt_done;
29770 else
29771 goto saw_error;
29773 break;
29774 default:
29775 break;
29777 cp_parser_abort_tentative_parse (parser);
29778 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29780 rhs = cp_parser_expression (parser);
29781 if (rhs == error_mark_node)
29782 goto saw_error;
29783 opcode = NOP_EXPR;
29784 rhs1 = NULL_TREE;
29785 goto stmt_done;
29787 cp_parser_error (parser,
29788 "invalid form of %<#pragma omp atomic%>");
29789 goto saw_error;
29791 if (!cp_parser_parse_definitely (parser))
29792 goto saw_error;
29793 switch (token->type)
29795 case CPP_SEMICOLON:
29796 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29798 code = OMP_ATOMIC_CAPTURE_OLD;
29799 v = lhs;
29800 lhs = NULL_TREE;
29801 lhs1 = rhs1;
29802 rhs1 = NULL_TREE;
29803 cp_lexer_consume_token (parser->lexer);
29804 goto restart;
29806 else if (structured_block)
29808 opcode = NOP_EXPR;
29809 rhs = rhs1;
29810 rhs1 = NULL_TREE;
29811 goto stmt_done;
29813 cp_parser_error (parser,
29814 "invalid form of %<#pragma omp atomic%>");
29815 goto saw_error;
29816 case CPP_MULT:
29817 opcode = MULT_EXPR;
29818 break;
29819 case CPP_DIV:
29820 opcode = TRUNC_DIV_EXPR;
29821 break;
29822 case CPP_PLUS:
29823 opcode = PLUS_EXPR;
29824 break;
29825 case CPP_MINUS:
29826 opcode = MINUS_EXPR;
29827 break;
29828 case CPP_LSHIFT:
29829 opcode = LSHIFT_EXPR;
29830 break;
29831 case CPP_RSHIFT:
29832 opcode = RSHIFT_EXPR;
29833 break;
29834 case CPP_AND:
29835 opcode = BIT_AND_EXPR;
29836 break;
29837 case CPP_OR:
29838 opcode = BIT_IOR_EXPR;
29839 break;
29840 case CPP_XOR:
29841 opcode = BIT_XOR_EXPR;
29842 break;
29843 default:
29844 cp_parser_error (parser,
29845 "invalid operator for %<#pragma omp atomic%>");
29846 goto saw_error;
29848 oprec = TOKEN_PRECEDENCE (token);
29849 gcc_assert (oprec != PREC_NOT_OPERATOR);
29850 if (commutative_tree_code (opcode))
29851 oprec = (enum cp_parser_prec) (oprec - 1);
29852 cp_lexer_consume_token (parser->lexer);
29853 rhs = cp_parser_binary_expression (parser, false, false,
29854 oprec, NULL);
29855 if (rhs == error_mark_node)
29856 goto saw_error;
29857 goto stmt_done;
29858 /* FALLTHROUGH */
29859 default:
29860 cp_parser_error (parser,
29861 "invalid operator for %<#pragma omp atomic%>");
29862 goto saw_error;
29864 cp_lexer_consume_token (parser->lexer);
29866 rhs = cp_parser_expression (parser);
29867 if (rhs == error_mark_node)
29868 goto saw_error;
29869 break;
29871 stmt_done:
29872 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29874 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29875 goto saw_error;
29876 v = cp_parser_unary_expression (parser);
29877 if (v == error_mark_node)
29878 goto saw_error;
29879 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29880 goto saw_error;
29881 lhs1 = cp_parser_unary_expression (parser);
29882 if (lhs1 == error_mark_node)
29883 goto saw_error;
29885 if (structured_block)
29887 cp_parser_consume_semicolon_at_end_of_statement (parser);
29888 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29890 done:
29891 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29892 if (!structured_block)
29893 cp_parser_consume_semicolon_at_end_of_statement (parser);
29894 return;
29896 saw_error:
29897 cp_parser_skip_to_end_of_block_or_statement (parser);
29898 if (structured_block)
29900 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29901 cp_lexer_consume_token (parser->lexer);
29902 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29904 cp_parser_skip_to_end_of_block_or_statement (parser);
29905 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29906 cp_lexer_consume_token (parser->lexer);
29912 /* OpenMP 2.5:
29913 # pragma omp barrier new-line */
29915 static void
29916 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29918 cp_parser_require_pragma_eol (parser, pragma_tok);
29919 finish_omp_barrier ();
29922 /* OpenMP 2.5:
29923 # pragma omp critical [(name)] new-line
29924 structured-block */
29926 static tree
29927 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29929 tree stmt, name = NULL;
29931 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29933 cp_lexer_consume_token (parser->lexer);
29935 name = cp_parser_identifier (parser);
29937 if (name == error_mark_node
29938 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29939 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29940 /*or_comma=*/false,
29941 /*consume_paren=*/true);
29942 if (name == error_mark_node)
29943 name = NULL;
29945 cp_parser_require_pragma_eol (parser, pragma_tok);
29947 stmt = cp_parser_omp_structured_block (parser);
29948 return c_finish_omp_critical (input_location, stmt, name);
29951 /* OpenMP 2.5:
29952 # pragma omp flush flush-vars[opt] new-line
29954 flush-vars:
29955 ( variable-list ) */
29957 static void
29958 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29960 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29961 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29962 cp_parser_require_pragma_eol (parser, pragma_tok);
29964 finish_omp_flush ();
29967 /* Helper function, to parse omp for increment expression. */
29969 static tree
29970 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29972 tree cond = cp_parser_binary_expression (parser, false, true,
29973 PREC_NOT_OPERATOR, NULL);
29974 if (cond == error_mark_node
29975 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29977 cp_parser_skip_to_end_of_statement (parser);
29978 return error_mark_node;
29981 switch (TREE_CODE (cond))
29983 case GT_EXPR:
29984 case GE_EXPR:
29985 case LT_EXPR:
29986 case LE_EXPR:
29987 break;
29988 case NE_EXPR:
29989 if (code == CILK_SIMD || code == CILK_FOR)
29990 break;
29991 /* Fall through: OpenMP disallows NE_EXPR. */
29992 default:
29993 return error_mark_node;
29996 /* If decl is an iterator, preserve LHS and RHS of the relational
29997 expr until finish_omp_for. */
29998 if (decl
29999 && (type_dependent_expression_p (decl)
30000 || CLASS_TYPE_P (TREE_TYPE (decl))))
30001 return cond;
30003 return build_x_binary_op (input_location, TREE_CODE (cond),
30004 TREE_OPERAND (cond, 0), ERROR_MARK,
30005 TREE_OPERAND (cond, 1), ERROR_MARK,
30006 /*overload=*/NULL, tf_warning_or_error);
30009 /* Helper function, to parse omp for increment expression. */
30011 static tree
30012 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
30014 cp_token *token = cp_lexer_peek_token (parser->lexer);
30015 enum tree_code op;
30016 tree lhs, rhs;
30017 cp_id_kind idk;
30018 bool decl_first;
30020 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30022 op = (token->type == CPP_PLUS_PLUS
30023 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30024 cp_lexer_consume_token (parser->lexer);
30025 lhs = cp_parser_simple_cast_expression (parser);
30026 if (lhs != decl)
30027 return error_mark_node;
30028 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30031 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30032 if (lhs != decl)
30033 return error_mark_node;
30035 token = cp_lexer_peek_token (parser->lexer);
30036 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30038 op = (token->type == CPP_PLUS_PLUS
30039 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30040 cp_lexer_consume_token (parser->lexer);
30041 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30044 op = cp_parser_assignment_operator_opt (parser);
30045 if (op == ERROR_MARK)
30046 return error_mark_node;
30048 if (op != NOP_EXPR)
30050 rhs = cp_parser_assignment_expression (parser);
30051 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30052 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30055 lhs = cp_parser_binary_expression (parser, false, false,
30056 PREC_ADDITIVE_EXPRESSION, NULL);
30057 token = cp_lexer_peek_token (parser->lexer);
30058 decl_first = lhs == decl;
30059 if (decl_first)
30060 lhs = NULL_TREE;
30061 if (token->type != CPP_PLUS
30062 && token->type != CPP_MINUS)
30063 return error_mark_node;
30067 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30068 cp_lexer_consume_token (parser->lexer);
30069 rhs = cp_parser_binary_expression (parser, false, false,
30070 PREC_ADDITIVE_EXPRESSION, NULL);
30071 token = cp_lexer_peek_token (parser->lexer);
30072 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30074 if (lhs == NULL_TREE)
30076 if (op == PLUS_EXPR)
30077 lhs = rhs;
30078 else
30079 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30080 tf_warning_or_error);
30082 else
30083 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30084 ERROR_MARK, NULL, tf_warning_or_error);
30087 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30089 if (!decl_first)
30091 if (rhs != decl || op == MINUS_EXPR)
30092 return error_mark_node;
30093 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30095 else
30096 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30098 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30101 /* Parse the initialization statement of either an OpenMP for loop or
30102 a Cilk Plus for loop.
30104 Return true if the resulting construct should have an
30105 OMP_CLAUSE_PRIVATE added to it. */
30107 static bool
30108 cp_parser_omp_for_loop_init (cp_parser *parser,
30109 enum tree_code code,
30110 tree &this_pre_body,
30111 vec<tree, va_gc> *for_block,
30112 tree &init,
30113 tree &decl,
30114 tree &real_decl)
30116 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30117 return false;
30119 bool add_private_clause = false;
30121 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30123 init-expr:
30124 var = lb
30125 integer-type var = lb
30126 random-access-iterator-type var = lb
30127 pointer-type var = lb
30129 cp_decl_specifier_seq type_specifiers;
30131 /* First, try to parse as an initialized declaration. See
30132 cp_parser_condition, from whence the bulk of this is copied. */
30134 cp_parser_parse_tentatively (parser);
30135 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30136 /*is_trailing_return=*/false,
30137 &type_specifiers);
30138 if (cp_parser_parse_definitely (parser))
30140 /* If parsing a type specifier seq succeeded, then this
30141 MUST be a initialized declaration. */
30142 tree asm_specification, attributes;
30143 cp_declarator *declarator;
30145 declarator = cp_parser_declarator (parser,
30146 CP_PARSER_DECLARATOR_NAMED,
30147 /*ctor_dtor_or_conv_p=*/NULL,
30148 /*parenthesized_p=*/NULL,
30149 /*member_p=*/false,
30150 /*friend_p=*/false);
30151 attributes = cp_parser_attributes_opt (parser);
30152 asm_specification = cp_parser_asm_specification_opt (parser);
30154 if (declarator == cp_error_declarator)
30155 cp_parser_skip_to_end_of_statement (parser);
30157 else
30159 tree pushed_scope, auto_node;
30161 decl = start_decl (declarator, &type_specifiers,
30162 SD_INITIALIZED, attributes,
30163 /*prefix_attributes=*/NULL_TREE,
30164 &pushed_scope);
30166 auto_node = type_uses_auto (TREE_TYPE (decl));
30167 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30169 if (cp_lexer_next_token_is (parser->lexer,
30170 CPP_OPEN_PAREN))
30172 if (code != CILK_SIMD && code != CILK_FOR)
30173 error ("parenthesized initialization is not allowed in "
30174 "OpenMP %<for%> loop");
30175 else
30176 error ("parenthesized initialization is "
30177 "not allowed in for-loop");
30179 else
30180 /* Trigger an error. */
30181 cp_parser_require (parser, CPP_EQ, RT_EQ);
30183 init = error_mark_node;
30184 cp_parser_skip_to_end_of_statement (parser);
30186 else if (CLASS_TYPE_P (TREE_TYPE (decl))
30187 || type_dependent_expression_p (decl)
30188 || auto_node)
30190 bool is_direct_init, is_non_constant_init;
30192 init = cp_parser_initializer (parser,
30193 &is_direct_init,
30194 &is_non_constant_init);
30196 if (auto_node)
30198 TREE_TYPE (decl)
30199 = do_auto_deduction (TREE_TYPE (decl), init,
30200 auto_node);
30202 if (!CLASS_TYPE_P (TREE_TYPE (decl))
30203 && !type_dependent_expression_p (decl))
30204 goto non_class;
30207 cp_finish_decl (decl, init, !is_non_constant_init,
30208 asm_specification,
30209 LOOKUP_ONLYCONVERTING);
30210 if (CLASS_TYPE_P (TREE_TYPE (decl)))
30212 vec_safe_push (for_block, this_pre_body);
30213 init = NULL_TREE;
30215 else
30216 init = pop_stmt_list (this_pre_body);
30217 this_pre_body = NULL_TREE;
30219 else
30221 /* Consume '='. */
30222 cp_lexer_consume_token (parser->lexer);
30223 init = cp_parser_assignment_expression (parser);
30225 non_class:
30226 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30227 init = error_mark_node;
30228 else
30229 cp_finish_decl (decl, NULL_TREE,
30230 /*init_const_expr_p=*/false,
30231 asm_specification,
30232 LOOKUP_ONLYCONVERTING);
30235 if (pushed_scope)
30236 pop_scope (pushed_scope);
30239 else
30241 cp_id_kind idk;
30242 /* If parsing a type specifier sequence failed, then
30243 this MUST be a simple expression. */
30244 if (code == CILK_FOR)
30245 error ("%<_Cilk_for%> allows expression instead of declaration only "
30246 "in C, not in C++");
30247 cp_parser_parse_tentatively (parser);
30248 decl = cp_parser_primary_expression (parser, false, false,
30249 false, &idk);
30250 if (!cp_parser_error_occurred (parser)
30251 && decl
30252 && DECL_P (decl)
30253 && CLASS_TYPE_P (TREE_TYPE (decl)))
30255 tree rhs;
30257 cp_parser_parse_definitely (parser);
30258 cp_parser_require (parser, CPP_EQ, RT_EQ);
30259 rhs = cp_parser_assignment_expression (parser);
30260 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30261 decl, NOP_EXPR,
30262 rhs,
30263 tf_warning_or_error));
30264 add_private_clause = true;
30266 else
30268 decl = NULL;
30269 cp_parser_abort_tentative_parse (parser);
30270 init = cp_parser_expression (parser);
30271 if (init)
30273 if (TREE_CODE (init) == MODIFY_EXPR
30274 || TREE_CODE (init) == MODOP_EXPR)
30275 real_decl = TREE_OPERAND (init, 0);
30279 return add_private_clause;
30282 /* Parse the restricted form of the for statement allowed by OpenMP. */
30284 static tree
30285 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30286 tree *cclauses)
30288 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30289 tree real_decl, initv, condv, incrv, declv;
30290 tree this_pre_body, cl;
30291 location_t loc_first;
30292 bool collapse_err = false;
30293 int i, collapse = 1, nbraces = 0;
30294 vec<tree, va_gc> *for_block = make_tree_vector ();
30296 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30297 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30298 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30300 gcc_assert (collapse >= 1);
30302 declv = make_tree_vec (collapse);
30303 initv = make_tree_vec (collapse);
30304 condv = make_tree_vec (collapse);
30305 incrv = make_tree_vec (collapse);
30307 loc_first = cp_lexer_peek_token (parser->lexer)->location;
30309 for (i = 0; i < collapse; i++)
30311 int bracecount = 0;
30312 bool add_private_clause = false;
30313 location_t loc;
30315 if (code != CILK_FOR
30316 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30318 cp_parser_error (parser, "for statement expected");
30319 return NULL;
30321 if (code == CILK_FOR
30322 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
30324 cp_parser_error (parser, "_Cilk_for statement expected");
30325 return NULL;
30327 loc = cp_lexer_consume_token (parser->lexer)->location;
30329 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30330 return NULL;
30332 init = decl = real_decl = NULL;
30333 this_pre_body = push_stmt_list ();
30335 add_private_clause
30336 |= cp_parser_omp_for_loop_init (parser, code,
30337 this_pre_body, for_block,
30338 init, decl, real_decl);
30340 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30341 if (this_pre_body)
30343 this_pre_body = pop_stmt_list (this_pre_body);
30344 if (pre_body)
30346 tree t = pre_body;
30347 pre_body = push_stmt_list ();
30348 add_stmt (t);
30349 add_stmt (this_pre_body);
30350 pre_body = pop_stmt_list (pre_body);
30352 else
30353 pre_body = this_pre_body;
30356 if (decl)
30357 real_decl = decl;
30358 if (cclauses != NULL
30359 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30360 && real_decl != NULL_TREE)
30362 tree *c;
30363 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30364 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30365 && OMP_CLAUSE_DECL (*c) == real_decl)
30367 error_at (loc, "iteration variable %qD"
30368 " should not be firstprivate", real_decl);
30369 *c = OMP_CLAUSE_CHAIN (*c);
30371 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30372 && OMP_CLAUSE_DECL (*c) == real_decl)
30374 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
30375 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
30376 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
30377 OMP_CLAUSE_DECL (l) = real_decl;
30378 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
30379 if (code == OMP_SIMD)
30381 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30382 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
30384 else
30386 OMP_CLAUSE_CHAIN (l) = clauses;
30387 clauses = l;
30389 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
30390 CP_OMP_CLAUSE_INFO (*c) = NULL;
30391 add_private_clause = false;
30393 else
30395 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30396 && OMP_CLAUSE_DECL (*c) == real_decl)
30397 add_private_clause = false;
30398 c = &OMP_CLAUSE_CHAIN (*c);
30402 if (add_private_clause)
30404 tree c;
30405 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30407 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30408 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30409 && OMP_CLAUSE_DECL (c) == decl)
30410 break;
30411 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30412 && OMP_CLAUSE_DECL (c) == decl)
30413 error_at (loc, "iteration variable %qD "
30414 "should not be firstprivate",
30415 decl);
30416 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30417 && OMP_CLAUSE_DECL (c) == decl)
30418 error_at (loc, "iteration variable %qD should not be reduction",
30419 decl);
30421 if (c == NULL)
30423 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30424 OMP_CLAUSE_DECL (c) = decl;
30425 c = finish_omp_clauses (c);
30426 if (c)
30428 OMP_CLAUSE_CHAIN (c) = clauses;
30429 clauses = c;
30434 cond = NULL;
30435 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30436 cond = cp_parser_omp_for_cond (parser, decl, code);
30437 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30439 incr = NULL;
30440 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30442 /* If decl is an iterator, preserve the operator on decl
30443 until finish_omp_for. */
30444 if (real_decl
30445 && ((processing_template_decl
30446 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30447 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30448 incr = cp_parser_omp_for_incr (parser, real_decl);
30449 else
30450 incr = cp_parser_expression (parser);
30451 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30452 SET_EXPR_LOCATION (incr, input_location);
30455 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30456 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30457 /*or_comma=*/false,
30458 /*consume_paren=*/true);
30460 TREE_VEC_ELT (declv, i) = decl;
30461 TREE_VEC_ELT (initv, i) = init;
30462 TREE_VEC_ELT (condv, i) = cond;
30463 TREE_VEC_ELT (incrv, i) = incr;
30465 if (i == collapse - 1)
30466 break;
30468 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30469 in between the collapsed for loops to be still considered perfectly
30470 nested. Hopefully the final version clarifies this.
30471 For now handle (multiple) {'s and empty statements. */
30472 cp_parser_parse_tentatively (parser);
30475 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30476 break;
30477 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30479 cp_lexer_consume_token (parser->lexer);
30480 bracecount++;
30482 else if (bracecount
30483 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30484 cp_lexer_consume_token (parser->lexer);
30485 else
30487 loc = cp_lexer_peek_token (parser->lexer)->location;
30488 error_at (loc, "not enough collapsed for loops");
30489 collapse_err = true;
30490 cp_parser_abort_tentative_parse (parser);
30491 declv = NULL_TREE;
30492 break;
30495 while (1);
30497 if (declv)
30499 cp_parser_parse_definitely (parser);
30500 nbraces += bracecount;
30504 /* Note that we saved the original contents of this flag when we entered
30505 the structured block, and so we don't need to re-save it here. */
30506 if (code == CILK_SIMD || code == CILK_FOR)
30507 parser->in_statement = IN_CILK_SIMD_FOR;
30508 else
30509 parser->in_statement = IN_OMP_FOR;
30511 /* Note that the grammar doesn't call for a structured block here,
30512 though the loop as a whole is a structured block. */
30513 body = push_stmt_list ();
30514 cp_parser_statement (parser, NULL_TREE, false, NULL);
30515 body = pop_stmt_list (body);
30517 if (declv == NULL_TREE)
30518 ret = NULL_TREE;
30519 else
30520 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30521 pre_body, clauses);
30523 while (nbraces)
30525 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30527 cp_lexer_consume_token (parser->lexer);
30528 nbraces--;
30530 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30531 cp_lexer_consume_token (parser->lexer);
30532 else
30534 if (!collapse_err)
30536 error_at (cp_lexer_peek_token (parser->lexer)->location,
30537 "collapsed loops not perfectly nested");
30539 collapse_err = true;
30540 cp_parser_statement_seq_opt (parser, NULL);
30541 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30542 break;
30546 while (!for_block->is_empty ())
30547 add_stmt (pop_stmt_list (for_block->pop ()));
30548 release_tree_vector (for_block);
30550 return ret;
30553 /* Helper function for OpenMP parsing, split clauses and call
30554 finish_omp_clauses on each of the set of clauses afterwards. */
30556 static void
30557 cp_omp_split_clauses (location_t loc, enum tree_code code,
30558 omp_clause_mask mask, tree clauses, tree *cclauses)
30560 int i;
30561 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30562 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30563 if (cclauses[i])
30564 cclauses[i] = finish_omp_clauses (cclauses[i]);
30567 /* OpenMP 4.0:
30568 #pragma omp simd simd-clause[optseq] new-line
30569 for-loop */
30571 #define OMP_SIMD_CLAUSE_MASK \
30572 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30580 static tree
30581 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30582 char *p_name, omp_clause_mask mask, tree *cclauses)
30584 tree clauses, sb, ret;
30585 unsigned int save;
30586 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30588 strcat (p_name, " simd");
30589 mask |= OMP_SIMD_CLAUSE_MASK;
30590 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30592 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30593 cclauses == NULL);
30594 if (cclauses)
30596 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30597 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30600 sb = begin_omp_structured_block ();
30601 save = cp_parser_begin_omp_structured_block (parser);
30603 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30605 cp_parser_end_omp_structured_block (parser, save);
30606 add_stmt (finish_omp_structured_block (sb));
30608 return ret;
30611 /* OpenMP 2.5:
30612 #pragma omp for for-clause[optseq] new-line
30613 for-loop
30615 OpenMP 4.0:
30616 #pragma omp for simd for-simd-clause[optseq] new-line
30617 for-loop */
30619 #define OMP_FOR_CLAUSE_MASK \
30620 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30629 static tree
30630 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30631 char *p_name, omp_clause_mask mask, tree *cclauses)
30633 tree clauses, sb, ret;
30634 unsigned int save;
30635 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30637 strcat (p_name, " for");
30638 mask |= OMP_FOR_CLAUSE_MASK;
30639 if (cclauses)
30640 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30642 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30644 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30645 const char *p = IDENTIFIER_POINTER (id);
30647 if (strcmp (p, "simd") == 0)
30649 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30650 if (cclauses == NULL)
30651 cclauses = cclauses_buf;
30653 cp_lexer_consume_token (parser->lexer);
30654 if (!flag_openmp) /* flag_openmp_simd */
30655 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30656 cclauses);
30657 sb = begin_omp_structured_block ();
30658 save = cp_parser_begin_omp_structured_block (parser);
30659 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30660 cclauses);
30661 cp_parser_end_omp_structured_block (parser, save);
30662 tree body = finish_omp_structured_block (sb);
30663 if (ret == NULL)
30664 return ret;
30665 ret = make_node (OMP_FOR);
30666 TREE_TYPE (ret) = void_type_node;
30667 OMP_FOR_BODY (ret) = body;
30668 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30669 SET_EXPR_LOCATION (ret, loc);
30670 add_stmt (ret);
30671 return ret;
30674 if (!flag_openmp) /* flag_openmp_simd */
30676 cp_parser_require_pragma_eol (parser, pragma_tok);
30677 return NULL_TREE;
30680 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30681 cclauses == NULL);
30682 if (cclauses)
30684 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30685 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30688 sb = begin_omp_structured_block ();
30689 save = cp_parser_begin_omp_structured_block (parser);
30691 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30693 cp_parser_end_omp_structured_block (parser, save);
30694 add_stmt (finish_omp_structured_block (sb));
30696 return ret;
30699 /* OpenMP 2.5:
30700 # pragma omp master new-line
30701 structured-block */
30703 static tree
30704 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30706 cp_parser_require_pragma_eol (parser, pragma_tok);
30707 return c_finish_omp_master (input_location,
30708 cp_parser_omp_structured_block (parser));
30711 /* OpenMP 2.5:
30712 # pragma omp ordered new-line
30713 structured-block */
30715 static tree
30716 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30718 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30719 cp_parser_require_pragma_eol (parser, pragma_tok);
30720 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30723 /* OpenMP 2.5:
30725 section-scope:
30726 { section-sequence }
30728 section-sequence:
30729 section-directive[opt] structured-block
30730 section-sequence section-directive structured-block */
30732 static tree
30733 cp_parser_omp_sections_scope (cp_parser *parser)
30735 tree stmt, substmt;
30736 bool error_suppress = false;
30737 cp_token *tok;
30739 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30740 return NULL_TREE;
30742 stmt = push_stmt_list ();
30744 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30746 substmt = cp_parser_omp_structured_block (parser);
30747 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30748 add_stmt (substmt);
30751 while (1)
30753 tok = cp_lexer_peek_token (parser->lexer);
30754 if (tok->type == CPP_CLOSE_BRACE)
30755 break;
30756 if (tok->type == CPP_EOF)
30757 break;
30759 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30761 cp_lexer_consume_token (parser->lexer);
30762 cp_parser_require_pragma_eol (parser, tok);
30763 error_suppress = false;
30765 else if (!error_suppress)
30767 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30768 error_suppress = true;
30771 substmt = cp_parser_omp_structured_block (parser);
30772 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30773 add_stmt (substmt);
30775 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30777 substmt = pop_stmt_list (stmt);
30779 stmt = make_node (OMP_SECTIONS);
30780 TREE_TYPE (stmt) = void_type_node;
30781 OMP_SECTIONS_BODY (stmt) = substmt;
30783 add_stmt (stmt);
30784 return stmt;
30787 /* OpenMP 2.5:
30788 # pragma omp sections sections-clause[optseq] newline
30789 sections-scope */
30791 #define OMP_SECTIONS_CLAUSE_MASK \
30792 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30798 static tree
30799 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30800 char *p_name, omp_clause_mask mask, tree *cclauses)
30802 tree clauses, ret;
30803 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30805 strcat (p_name, " sections");
30806 mask |= OMP_SECTIONS_CLAUSE_MASK;
30807 if (cclauses)
30808 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30810 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30811 cclauses == NULL);
30812 if (cclauses)
30814 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30815 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30818 ret = cp_parser_omp_sections_scope (parser);
30819 if (ret)
30820 OMP_SECTIONS_CLAUSES (ret) = clauses;
30822 return ret;
30825 /* OpenMP 2.5:
30826 # pragma omp parallel parallel-clause[optseq] new-line
30827 structured-block
30828 # pragma omp parallel for parallel-for-clause[optseq] new-line
30829 structured-block
30830 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30831 structured-block
30833 OpenMP 4.0:
30834 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30835 structured-block */
30837 #define OMP_PARALLEL_CLAUSE_MASK \
30838 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30848 static tree
30849 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30850 char *p_name, omp_clause_mask mask, tree *cclauses)
30852 tree stmt, clauses, block;
30853 unsigned int save;
30854 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30856 strcat (p_name, " parallel");
30857 mask |= OMP_PARALLEL_CLAUSE_MASK;
30859 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30861 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30862 if (cclauses == NULL)
30863 cclauses = cclauses_buf;
30865 cp_lexer_consume_token (parser->lexer);
30866 if (!flag_openmp) /* flag_openmp_simd */
30867 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30868 block = begin_omp_parallel ();
30869 save = cp_parser_begin_omp_structured_block (parser);
30870 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30871 cp_parser_end_omp_structured_block (parser, save);
30872 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30873 block);
30874 if (ret == NULL_TREE)
30875 return ret;
30876 OMP_PARALLEL_COMBINED (stmt) = 1;
30877 return stmt;
30879 else if (cclauses)
30881 error_at (loc, "expected %<for%> after %qs", p_name);
30882 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30883 return NULL_TREE;
30885 else if (!flag_openmp) /* flag_openmp_simd */
30887 cp_parser_require_pragma_eol (parser, pragma_tok);
30888 return NULL_TREE;
30890 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30892 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30893 const char *p = IDENTIFIER_POINTER (id);
30894 if (strcmp (p, "sections") == 0)
30896 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30897 cclauses = cclauses_buf;
30899 cp_lexer_consume_token (parser->lexer);
30900 block = begin_omp_parallel ();
30901 save = cp_parser_begin_omp_structured_block (parser);
30902 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30903 cp_parser_end_omp_structured_block (parser, save);
30904 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30905 block);
30906 OMP_PARALLEL_COMBINED (stmt) = 1;
30907 return stmt;
30911 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30913 block = begin_omp_parallel ();
30914 save = cp_parser_begin_omp_structured_block (parser);
30915 cp_parser_statement (parser, NULL_TREE, false, NULL);
30916 cp_parser_end_omp_structured_block (parser, save);
30917 stmt = finish_omp_parallel (clauses, block);
30918 return stmt;
30921 /* OpenMP 2.5:
30922 # pragma omp single single-clause[optseq] new-line
30923 structured-block */
30925 #define OMP_SINGLE_CLAUSE_MASK \
30926 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30931 static tree
30932 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30934 tree stmt = make_node (OMP_SINGLE);
30935 TREE_TYPE (stmt) = void_type_node;
30937 OMP_SINGLE_CLAUSES (stmt)
30938 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30939 "#pragma omp single", pragma_tok);
30940 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30942 return add_stmt (stmt);
30945 /* OpenMP 3.0:
30946 # pragma omp task task-clause[optseq] new-line
30947 structured-block */
30949 #define OMP_TASK_CLAUSE_MASK \
30950 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30960 static tree
30961 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30963 tree clauses, block;
30964 unsigned int save;
30966 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30967 "#pragma omp task", pragma_tok);
30968 block = begin_omp_task ();
30969 save = cp_parser_begin_omp_structured_block (parser);
30970 cp_parser_statement (parser, NULL_TREE, false, NULL);
30971 cp_parser_end_omp_structured_block (parser, save);
30972 return finish_omp_task (clauses, block);
30975 /* OpenMP 3.0:
30976 # pragma omp taskwait new-line */
30978 static void
30979 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30981 cp_parser_require_pragma_eol (parser, pragma_tok);
30982 finish_omp_taskwait ();
30985 /* OpenMP 3.1:
30986 # pragma omp taskyield new-line */
30988 static void
30989 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30991 cp_parser_require_pragma_eol (parser, pragma_tok);
30992 finish_omp_taskyield ();
30995 /* OpenMP 4.0:
30996 # pragma omp taskgroup new-line
30997 structured-block */
30999 static tree
31000 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
31002 cp_parser_require_pragma_eol (parser, pragma_tok);
31003 return c_finish_omp_taskgroup (input_location,
31004 cp_parser_omp_structured_block (parser));
31008 /* OpenMP 2.5:
31009 # pragma omp threadprivate (variable-list) */
31011 static void
31012 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
31014 tree vars;
31016 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31017 cp_parser_require_pragma_eol (parser, pragma_tok);
31019 finish_omp_threadprivate (vars);
31022 /* OpenMP 4.0:
31023 # pragma omp cancel cancel-clause[optseq] new-line */
31025 #define OMP_CANCEL_CLAUSE_MASK \
31026 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
31030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31032 static void
31033 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31035 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31036 "#pragma omp cancel", pragma_tok);
31037 finish_omp_cancel (clauses);
31040 /* OpenMP 4.0:
31041 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31043 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31044 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31049 static void
31050 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31052 tree clauses;
31053 bool point_seen = false;
31055 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31057 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31058 const char *p = IDENTIFIER_POINTER (id);
31060 if (strcmp (p, "point") == 0)
31062 cp_lexer_consume_token (parser->lexer);
31063 point_seen = true;
31066 if (!point_seen)
31068 cp_parser_error (parser, "expected %<point%>");
31069 cp_parser_require_pragma_eol (parser, pragma_tok);
31070 return;
31073 clauses = cp_parser_omp_all_clauses (parser,
31074 OMP_CANCELLATION_POINT_CLAUSE_MASK,
31075 "#pragma omp cancellation point",
31076 pragma_tok);
31077 finish_omp_cancellation_point (clauses);
31080 /* OpenMP 4.0:
31081 #pragma omp distribute distribute-clause[optseq] new-line
31082 for-loop */
31084 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31085 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31090 static tree
31091 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31092 char *p_name, omp_clause_mask mask, tree *cclauses)
31094 tree clauses, sb, ret;
31095 unsigned int save;
31096 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31098 strcat (p_name, " distribute");
31099 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31101 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31103 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31104 const char *p = IDENTIFIER_POINTER (id);
31105 bool simd = false;
31106 bool parallel = false;
31108 if (strcmp (p, "simd") == 0)
31109 simd = true;
31110 else
31111 parallel = strcmp (p, "parallel") == 0;
31112 if (parallel || simd)
31114 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31115 if (cclauses == NULL)
31116 cclauses = cclauses_buf;
31117 cp_lexer_consume_token (parser->lexer);
31118 if (!flag_openmp) /* flag_openmp_simd */
31120 if (simd)
31121 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31122 cclauses);
31123 else
31124 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31125 cclauses);
31127 sb = begin_omp_structured_block ();
31128 save = cp_parser_begin_omp_structured_block (parser);
31129 if (simd)
31130 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31131 cclauses);
31132 else
31133 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31134 cclauses);
31135 cp_parser_end_omp_structured_block (parser, save);
31136 tree body = finish_omp_structured_block (sb);
31137 if (ret == NULL)
31138 return ret;
31139 ret = make_node (OMP_DISTRIBUTE);
31140 TREE_TYPE (ret) = void_type_node;
31141 OMP_FOR_BODY (ret) = body;
31142 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31143 SET_EXPR_LOCATION (ret, loc);
31144 add_stmt (ret);
31145 return ret;
31148 if (!flag_openmp) /* flag_openmp_simd */
31150 cp_parser_require_pragma_eol (parser, pragma_tok);
31151 return NULL_TREE;
31154 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31155 cclauses == NULL);
31156 if (cclauses)
31158 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31159 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31162 sb = begin_omp_structured_block ();
31163 save = cp_parser_begin_omp_structured_block (parser);
31165 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31167 cp_parser_end_omp_structured_block (parser, save);
31168 add_stmt (finish_omp_structured_block (sb));
31170 return ret;
31173 /* OpenMP 4.0:
31174 # pragma omp teams teams-clause[optseq] new-line
31175 structured-block */
31177 #define OMP_TEAMS_CLAUSE_MASK \
31178 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31186 static tree
31187 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31188 char *p_name, omp_clause_mask mask, tree *cclauses)
31190 tree clauses, sb, ret;
31191 unsigned int save;
31192 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31194 strcat (p_name, " teams");
31195 mask |= OMP_TEAMS_CLAUSE_MASK;
31197 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31199 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31200 const char *p = IDENTIFIER_POINTER (id);
31201 if (strcmp (p, "distribute") == 0)
31203 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31204 if (cclauses == NULL)
31205 cclauses = cclauses_buf;
31207 cp_lexer_consume_token (parser->lexer);
31208 if (!flag_openmp) /* flag_openmp_simd */
31209 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31210 cclauses);
31211 sb = begin_omp_structured_block ();
31212 save = cp_parser_begin_omp_structured_block (parser);
31213 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31214 cclauses);
31215 cp_parser_end_omp_structured_block (parser, save);
31216 tree body = finish_omp_structured_block (sb);
31217 if (ret == NULL)
31218 return ret;
31219 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31220 ret = make_node (OMP_TEAMS);
31221 TREE_TYPE (ret) = void_type_node;
31222 OMP_TEAMS_CLAUSES (ret) = clauses;
31223 OMP_TEAMS_BODY (ret) = body;
31224 return add_stmt (ret);
31227 if (!flag_openmp) /* flag_openmp_simd */
31229 cp_parser_require_pragma_eol (parser, pragma_tok);
31230 return NULL_TREE;
31233 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31234 cclauses == NULL);
31235 if (cclauses)
31237 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31238 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31241 tree stmt = make_node (OMP_TEAMS);
31242 TREE_TYPE (stmt) = void_type_node;
31243 OMP_TEAMS_CLAUSES (stmt) = clauses;
31244 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31246 return add_stmt (stmt);
31249 /* OpenMP 4.0:
31250 # pragma omp target data target-data-clause[optseq] new-line
31251 structured-block */
31253 #define OMP_TARGET_DATA_CLAUSE_MASK \
31254 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31258 static tree
31259 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31261 tree stmt = make_node (OMP_TARGET_DATA);
31262 TREE_TYPE (stmt) = void_type_node;
31264 OMP_TARGET_DATA_CLAUSES (stmt)
31265 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31266 "#pragma omp target data", pragma_tok);
31267 keep_next_level (true);
31268 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31270 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31271 return add_stmt (stmt);
31274 /* OpenMP 4.0:
31275 # pragma omp target update target-update-clause[optseq] new-line */
31277 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31278 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31283 static bool
31284 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31285 enum pragma_context context)
31287 if (context == pragma_stmt)
31289 error_at (pragma_tok->location,
31290 "%<#pragma omp target update%> may only be "
31291 "used in compound statements");
31292 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31293 return false;
31296 tree clauses
31297 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31298 "#pragma omp target update", pragma_tok);
31299 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31300 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31302 error_at (pragma_tok->location,
31303 "%<#pragma omp target update must contain at least one "
31304 "%<from%> or %<to%> clauses");
31305 return false;
31308 tree stmt = make_node (OMP_TARGET_UPDATE);
31309 TREE_TYPE (stmt) = void_type_node;
31310 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31311 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31312 add_stmt (stmt);
31313 return false;
31316 /* OpenMP 4.0:
31317 # pragma omp target target-clause[optseq] new-line
31318 structured-block */
31320 #define OMP_TARGET_CLAUSE_MASK \
31321 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31325 static bool
31326 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31327 enum pragma_context context)
31329 if (context != pragma_stmt && context != pragma_compound)
31331 cp_parser_error (parser, "expected declaration specifiers");
31332 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31333 return false;
31336 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31338 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31339 const char *p = IDENTIFIER_POINTER (id);
31341 if (strcmp (p, "teams") == 0)
31343 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31344 char p_name[sizeof ("#pragma omp target teams distribute "
31345 "parallel for simd")];
31347 cp_lexer_consume_token (parser->lexer);
31348 strcpy (p_name, "#pragma omp target");
31349 if (!flag_openmp) /* flag_openmp_simd */
31351 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31352 OMP_TARGET_CLAUSE_MASK,
31353 cclauses);
31354 return stmt != NULL_TREE;
31356 keep_next_level (true);
31357 tree sb = begin_omp_structured_block ();
31358 unsigned save = cp_parser_begin_omp_structured_block (parser);
31359 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31360 OMP_TARGET_CLAUSE_MASK, cclauses);
31361 cp_parser_end_omp_structured_block (parser, save);
31362 tree body = finish_omp_structured_block (sb);
31363 if (ret == NULL_TREE)
31364 return false;
31365 tree stmt = make_node (OMP_TARGET);
31366 TREE_TYPE (stmt) = void_type_node;
31367 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31368 OMP_TARGET_BODY (stmt) = body;
31369 add_stmt (stmt);
31370 return true;
31372 else if (!flag_openmp) /* flag_openmp_simd */
31374 cp_parser_require_pragma_eol (parser, pragma_tok);
31375 return false;
31377 else if (strcmp (p, "data") == 0)
31379 cp_lexer_consume_token (parser->lexer);
31380 cp_parser_omp_target_data (parser, pragma_tok);
31381 return true;
31383 else if (strcmp (p, "update") == 0)
31385 cp_lexer_consume_token (parser->lexer);
31386 return cp_parser_omp_target_update (parser, pragma_tok, context);
31390 tree stmt = make_node (OMP_TARGET);
31391 TREE_TYPE (stmt) = void_type_node;
31393 OMP_TARGET_CLAUSES (stmt)
31394 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31395 "#pragma omp target", pragma_tok);
31396 keep_next_level (true);
31397 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31399 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31400 add_stmt (stmt);
31401 return true;
31404 /* OpenACC 2.0:
31405 # pragma acc cache (variable-list) new-line
31408 static tree
31409 cp_parser_oacc_cache (cp_parser *parser,
31410 cp_token *pragma_tok __attribute__((unused)))
31412 cp_parser_omp_var_list (parser, OMP_NO_CLAUSE_CACHE, NULL_TREE);
31413 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
31415 return NULL_TREE;
31418 /* OpenACC 2.0:
31419 # pragma acc data oacc-data-clause[optseq] new-line
31420 structured-block */
31422 #define OACC_DATA_CLAUSE_MASK \
31423 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
31424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
31426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
31427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
31428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
31430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
31431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
31432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
31433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE))
31435 static tree
31436 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
31438 tree stmt, clauses, block;
31439 unsigned int save;
31441 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
31442 "#pragma acc data", pragma_tok);
31444 block = begin_omp_parallel ();
31445 save = cp_parser_begin_omp_structured_block (parser);
31446 cp_parser_statement (parser, NULL_TREE, false, NULL);
31447 cp_parser_end_omp_structured_block (parser, save);
31448 stmt = finish_oacc_data (clauses, block);
31449 return stmt;
31452 /* OpenACC 2.0:
31453 # pragma acc kernels oacc-kernels-clause[optseq] new-line
31454 structured-block */
31456 #define OACC_KERNELS_CLAUSE_MASK \
31457 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
31458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
31459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
31461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
31462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
31463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
31465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
31466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
31467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
31468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) \
31469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT))
31471 static tree
31472 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
31474 tree stmt, clauses, block;
31475 unsigned int save;
31477 clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
31478 "#pragma acc kernels", pragma_tok);
31480 block = begin_omp_parallel ();
31481 save = cp_parser_begin_omp_structured_block (parser);
31482 cp_parser_statement (parser, NULL_TREE, false, NULL);
31483 cp_parser_end_omp_structured_block (parser, save);
31484 stmt = finish_oacc_kernels (clauses, block);
31485 return stmt;
31488 /* OpenACC 2.0:
31489 # pragma acc loop oacc-loop-clause[optseq] new-line
31490 structured-block */
31492 #define OACC_LOOP_CLAUSE_MASK \
31493 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
31494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION))
31496 static tree
31497 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
31499 tree stmt, clauses, block;
31500 int save;
31502 clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
31503 "#pragma acc loop", pragma_tok);
31505 block = begin_omp_structured_block ();
31506 save = cp_parser_begin_omp_structured_block (parser);
31507 stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
31508 cp_parser_end_omp_structured_block (parser, save);
31509 add_stmt (finish_omp_structured_block (block));
31510 return stmt;
31513 /* OpenACC 2.0:
31514 # pragma acc parallel oacc-parallel-clause[optseq] new-line
31515 structured-block */
31517 #define OACC_PARALLEL_CLAUSE_MASK \
31518 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
31519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
31520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
31522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
31523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
31524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_GANGS) \
31526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_WORKERS) \
31527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
31528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
31529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
31530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
31531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) \
31532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_VECTOR_LENGTH) \
31534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT))
31536 static tree
31537 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
31539 tree stmt, clauses, block;
31540 unsigned int save;
31542 clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
31543 "#pragma acc parallel", pragma_tok);
31545 block = begin_omp_parallel ();
31546 save = cp_parser_begin_omp_structured_block (parser);
31547 cp_parser_statement (parser, NULL_TREE, false, NULL);
31548 cp_parser_end_omp_structured_block (parser, save);
31549 stmt = finish_oacc_parallel (clauses, block);
31550 return stmt;
31553 /* OpenACC 2.0:
31554 # pragma acc update oacc-update-clause[optseq] new-line
31557 #define OACC_UPDATE_CLAUSE_MASK \
31558 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
31559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HOST) \
31561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SELF) \
31563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT))
31565 static tree
31566 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
31568 tree stmt, clauses;
31570 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
31571 "#pragma acc update", pragma_tok);
31573 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31575 error_at (pragma_tok->location,
31576 "%<#pragma acc update%> must contain at least one "
31577 "%<device%> or %<host/self%> clause");
31578 return NULL_TREE;
31581 stmt = make_node (OACC_UPDATE);
31582 TREE_TYPE (stmt) = void_type_node;
31583 OACC_UPDATE_CLAUSES (stmt) = clauses;
31584 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31585 add_stmt (stmt);
31586 return stmt;
31589 /* OpenACC 2.0:
31590 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31592 LOC is the location of the #pragma token.
31595 #define OACC_WAIT_CLAUSE_MASK \
31596 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC))
31598 static tree
31599 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
31601 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
31602 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31604 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31605 list = cp_parser_oacc_wait_list (parser, loc, list);
31607 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
31608 "#pragma acc wait", pragma_tok);
31610 stmt = c_finish_oacc_wait (loc, list, clauses);
31612 return stmt;
31615 /* OpenMP 4.0:
31616 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31618 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31619 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31626 static void
31627 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31628 enum pragma_context context)
31630 bool first_p = parser->omp_declare_simd == NULL;
31631 cp_omp_declare_simd_data data;
31632 if (first_p)
31634 data.error_seen = false;
31635 data.fndecl_seen = false;
31636 data.tokens = vNULL;
31637 parser->omp_declare_simd = &data;
31639 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31640 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31641 cp_lexer_consume_token (parser->lexer);
31642 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31643 parser->omp_declare_simd->error_seen = true;
31644 cp_parser_require_pragma_eol (parser, pragma_tok);
31645 struct cp_token_cache *cp
31646 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31647 parser->omp_declare_simd->tokens.safe_push (cp);
31648 if (first_p)
31650 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31651 cp_parser_pragma (parser, context);
31652 switch (context)
31654 case pragma_external:
31655 cp_parser_declaration (parser);
31656 break;
31657 case pragma_member:
31658 cp_parser_member_declaration (parser);
31659 break;
31660 case pragma_objc_icode:
31661 cp_parser_block_declaration (parser, /*statement_p=*/false);
31662 break;
31663 default:
31664 cp_parser_declaration_statement (parser);
31665 break;
31667 if (parser->omp_declare_simd
31668 && !parser->omp_declare_simd->error_seen
31669 && !parser->omp_declare_simd->fndecl_seen)
31670 error_at (pragma_tok->location,
31671 "%<#pragma omp declare simd%> not immediately followed by "
31672 "function declaration or definition");
31673 data.tokens.release ();
31674 parser->omp_declare_simd = NULL;
31678 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31679 This function is modelled similar to the late parsing of omp declare
31680 simd. */
31682 static tree
31683 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31685 struct cp_token_cache *ce;
31686 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31687 int ii = 0;
31689 if (parser->omp_declare_simd != NULL)
31691 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31692 " marked as a Cilk Plus SIMD-enabled function");
31693 XDELETE (parser->cilk_simd_fn_info);
31694 parser->cilk_simd_fn_info = NULL;
31695 return attrs;
31697 if (!info->error_seen && info->fndecl_seen)
31699 error ("vector attribute not immediately followed by a single function"
31700 " declaration or definition");
31701 info->error_seen = true;
31703 if (info->error_seen)
31704 return attrs;
31706 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31708 tree c, cl;
31710 cp_parser_push_lexer_for_tokens (parser, ce);
31711 parser->lexer->in_pragma = true;
31712 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31713 "SIMD-enabled functions attribute",
31714 NULL);
31715 cp_parser_pop_lexer (parser);
31716 if (cl)
31717 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31719 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31720 TREE_CHAIN (c) = attrs;
31721 attrs = c;
31723 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31724 TREE_CHAIN (c) = attrs;
31725 if (processing_template_decl)
31726 ATTR_IS_DEPENDENT (c) = 1;
31727 attrs = c;
31729 info->fndecl_seen = true;
31730 XDELETE (parser->cilk_simd_fn_info);
31731 parser->cilk_simd_fn_info = NULL;
31732 return attrs;
31735 /* Finalize #pragma omp declare simd clauses after direct declarator has
31736 been parsed, and put that into "omp declare simd" attribute. */
31738 static tree
31739 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31741 struct cp_token_cache *ce;
31742 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31743 int i;
31745 if (!data->error_seen && data->fndecl_seen)
31747 error ("%<#pragma omp declare simd%> not immediately followed by "
31748 "a single function declaration or definition");
31749 data->error_seen = true;
31750 return attrs;
31752 if (data->error_seen)
31753 return attrs;
31755 FOR_EACH_VEC_ELT (data->tokens, i, ce)
31757 tree c, cl;
31759 cp_parser_push_lexer_for_tokens (parser, ce);
31760 parser->lexer->in_pragma = true;
31761 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31762 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31763 cp_lexer_consume_token (parser->lexer);
31764 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31765 "#pragma omp declare simd", pragma_tok);
31766 cp_parser_pop_lexer (parser);
31767 if (cl)
31768 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31769 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31770 TREE_CHAIN (c) = attrs;
31771 if (processing_template_decl)
31772 ATTR_IS_DEPENDENT (c) = 1;
31773 attrs = c;
31776 data->fndecl_seen = true;
31777 return attrs;
31781 /* OpenMP 4.0:
31782 # pragma omp declare target new-line
31783 declarations and definitions
31784 # pragma omp end declare target new-line */
31786 static void
31787 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31789 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31790 scope_chain->omp_declare_target_attribute++;
31793 static void
31794 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
31796 const char *p = "";
31797 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31799 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31800 p = IDENTIFIER_POINTER (id);
31802 if (strcmp (p, "declare") == 0)
31804 cp_lexer_consume_token (parser->lexer);
31805 p = "";
31806 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31808 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31809 p = IDENTIFIER_POINTER (id);
31811 if (strcmp (p, "target") == 0)
31812 cp_lexer_consume_token (parser->lexer);
31813 else
31815 cp_parser_error (parser, "expected %<target%>");
31816 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31817 return;
31820 else
31822 cp_parser_error (parser, "expected %<declare%>");
31823 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31824 return;
31826 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31827 if (!scope_chain->omp_declare_target_attribute)
31828 error_at (pragma_tok->location,
31829 "%<#pragma omp end declare target%> without corresponding "
31830 "%<#pragma omp declare target%>");
31831 else
31832 scope_chain->omp_declare_target_attribute--;
31835 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31836 expression and optional initializer clause of
31837 #pragma omp declare reduction. We store the expression(s) as
31838 either 3, 6 or 7 special statements inside of the artificial function's
31839 body. The first two statements are DECL_EXPRs for the artificial
31840 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
31841 expression that uses those variables.
31842 If there was any INITIALIZER clause, this is followed by further statements,
31843 the fourth and fifth statements are DECL_EXPRs for the artificial
31844 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
31845 constructor variant (first token after open paren is not omp_priv),
31846 then the sixth statement is a statement with the function call expression
31847 that uses the OMP_PRIV and optionally OMP_ORIG variable.
31848 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
31849 to initialize the OMP_PRIV artificial variable and there is seventh
31850 statement, a DECL_EXPR of the OMP_PRIV statement again. */
31852 static bool
31853 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
31855 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
31856 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
31857 type = TREE_TYPE (type);
31858 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
31859 DECL_ARTIFICIAL (omp_out) = 1;
31860 pushdecl (omp_out);
31861 add_decl_expr (omp_out);
31862 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
31863 DECL_ARTIFICIAL (omp_in) = 1;
31864 pushdecl (omp_in);
31865 add_decl_expr (omp_in);
31866 tree combiner;
31867 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
31869 keep_next_level (true);
31870 tree block = begin_omp_structured_block ();
31871 combiner = cp_parser_expression (parser);
31872 finish_expr_stmt (combiner);
31873 block = finish_omp_structured_block (block);
31874 add_stmt (block);
31876 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31877 return false;
31879 const char *p = "";
31880 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31882 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31883 p = IDENTIFIER_POINTER (id);
31886 if (strcmp (p, "initializer") == 0)
31888 cp_lexer_consume_token (parser->lexer);
31889 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31890 return false;
31892 p = "";
31893 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31895 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31896 p = IDENTIFIER_POINTER (id);
31899 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
31900 DECL_ARTIFICIAL (omp_priv) = 1;
31901 pushdecl (omp_priv);
31902 add_decl_expr (omp_priv);
31903 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
31904 DECL_ARTIFICIAL (omp_orig) = 1;
31905 pushdecl (omp_orig);
31906 add_decl_expr (omp_orig);
31908 keep_next_level (true);
31909 block = begin_omp_structured_block ();
31911 bool ctor = false;
31912 if (strcmp (p, "omp_priv") == 0)
31914 bool is_direct_init, is_non_constant_init;
31915 ctor = true;
31916 cp_lexer_consume_token (parser->lexer);
31917 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
31918 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
31919 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31920 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
31921 == CPP_CLOSE_PAREN
31922 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
31923 == CPP_CLOSE_PAREN))
31925 finish_omp_structured_block (block);
31926 error ("invalid initializer clause");
31927 return false;
31929 initializer = cp_parser_initializer (parser, &is_direct_init,
31930 &is_non_constant_init);
31931 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
31932 NULL_TREE, LOOKUP_ONLYCONVERTING);
31934 else
31936 cp_parser_parse_tentatively (parser);
31937 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
31938 /*check_dependency_p=*/true,
31939 /*template_p=*/NULL,
31940 /*declarator_p=*/false,
31941 /*optional_p=*/false);
31942 vec<tree, va_gc> *args;
31943 if (fn_name == error_mark_node
31944 || cp_parser_error_occurred (parser)
31945 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31946 || ((args = cp_parser_parenthesized_expression_list
31947 (parser, non_attr, /*cast_p=*/false,
31948 /*allow_expansion_p=*/true,
31949 /*non_constant_p=*/NULL)),
31950 cp_parser_error_occurred (parser)))
31952 finish_omp_structured_block (block);
31953 cp_parser_abort_tentative_parse (parser);
31954 cp_parser_error (parser, "expected id-expression (arguments)");
31955 return false;
31957 unsigned int i;
31958 tree arg;
31959 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
31960 if (arg == omp_priv
31961 || (TREE_CODE (arg) == ADDR_EXPR
31962 && TREE_OPERAND (arg, 0) == omp_priv))
31963 break;
31964 cp_parser_abort_tentative_parse (parser);
31965 if (arg == NULL_TREE)
31966 error ("one of the initializer call arguments should be %<omp_priv%>"
31967 " or %<&omp_priv%>");
31968 initializer = cp_parser_postfix_expression (parser, false, false, false,
31969 false, NULL);
31970 finish_expr_stmt (initializer);
31973 block = finish_omp_structured_block (block);
31974 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
31975 finish_expr_stmt (block);
31977 if (ctor)
31978 add_decl_expr (omp_orig);
31980 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31981 return false;
31984 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31985 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31987 return true;
31990 /* OpenMP 4.0
31991 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31992 initializer-clause[opt] new-line
31994 initializer-clause:
31995 initializer (omp_priv initializer)
31996 initializer (function-name (argument-list)) */
31998 static void
31999 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
32000 enum pragma_context)
32002 auto_vec<tree> types;
32003 enum tree_code reduc_code = ERROR_MARK;
32004 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
32005 unsigned int i;
32006 cp_token *first_token;
32007 cp_token_cache *cp;
32008 int errs;
32009 void *p;
32011 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
32012 p = obstack_alloc (&declarator_obstack, 0);
32014 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32015 goto fail;
32017 switch (cp_lexer_peek_token (parser->lexer)->type)
32019 case CPP_PLUS:
32020 reduc_code = PLUS_EXPR;
32021 break;
32022 case CPP_MULT:
32023 reduc_code = MULT_EXPR;
32024 break;
32025 case CPP_MINUS:
32026 reduc_code = MINUS_EXPR;
32027 break;
32028 case CPP_AND:
32029 reduc_code = BIT_AND_EXPR;
32030 break;
32031 case CPP_XOR:
32032 reduc_code = BIT_XOR_EXPR;
32033 break;
32034 case CPP_OR:
32035 reduc_code = BIT_IOR_EXPR;
32036 break;
32037 case CPP_AND_AND:
32038 reduc_code = TRUTH_ANDIF_EXPR;
32039 break;
32040 case CPP_OR_OR:
32041 reduc_code = TRUTH_ORIF_EXPR;
32042 break;
32043 case CPP_NAME:
32044 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
32045 break;
32046 default:
32047 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32048 "%<|%>, %<&&%>, %<||%> or identifier");
32049 goto fail;
32052 if (reduc_code != ERROR_MARK)
32053 cp_lexer_consume_token (parser->lexer);
32055 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
32056 if (reduc_id == error_mark_node)
32057 goto fail;
32059 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32060 goto fail;
32062 /* Types may not be defined in declare reduction type list. */
32063 const char *saved_message;
32064 saved_message = parser->type_definition_forbidden_message;
32065 parser->type_definition_forbidden_message
32066 = G_("types may not be defined in declare reduction type list");
32067 bool saved_colon_corrects_to_scope_p;
32068 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32069 parser->colon_corrects_to_scope_p = false;
32070 bool saved_colon_doesnt_start_class_def_p;
32071 saved_colon_doesnt_start_class_def_p
32072 = parser->colon_doesnt_start_class_def_p;
32073 parser->colon_doesnt_start_class_def_p = true;
32075 while (true)
32077 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32078 type = cp_parser_type_id (parser);
32079 if (type == error_mark_node)
32081 else if (ARITHMETIC_TYPE_P (type)
32082 && (orig_reduc_id == NULL_TREE
32083 || (TREE_CODE (type) != COMPLEX_TYPE
32084 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32085 "min") == 0
32086 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32087 "max") == 0))))
32088 error_at (loc, "predeclared arithmetic type %qT in "
32089 "%<#pragma omp declare reduction%>", type);
32090 else if (TREE_CODE (type) == FUNCTION_TYPE
32091 || TREE_CODE (type) == METHOD_TYPE
32092 || TREE_CODE (type) == ARRAY_TYPE)
32093 error_at (loc, "function or array type %qT in "
32094 "%<#pragma omp declare reduction%>", type);
32095 else if (TREE_CODE (type) == REFERENCE_TYPE)
32096 error_at (loc, "reference type %qT in "
32097 "%<#pragma omp declare reduction%>", type);
32098 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
32099 error_at (loc, "const, volatile or __restrict qualified type %qT in "
32100 "%<#pragma omp declare reduction%>", type);
32101 else
32102 types.safe_push (type);
32104 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32105 cp_lexer_consume_token (parser->lexer);
32106 else
32107 break;
32110 /* Restore the saved message. */
32111 parser->type_definition_forbidden_message = saved_message;
32112 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32113 parser->colon_doesnt_start_class_def_p
32114 = saved_colon_doesnt_start_class_def_p;
32116 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
32117 || types.is_empty ())
32119 fail:
32120 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32121 goto done;
32124 first_token = cp_lexer_peek_token (parser->lexer);
32125 cp = NULL;
32126 errs = errorcount;
32127 FOR_EACH_VEC_ELT (types, i, type)
32129 tree fntype
32130 = build_function_type_list (void_type_node,
32131 cp_build_reference_type (type, false),
32132 NULL_TREE);
32133 tree this_reduc_id = reduc_id;
32134 if (!dependent_type_p (type))
32135 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
32136 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
32137 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
32138 DECL_ARTIFICIAL (fndecl) = 1;
32139 DECL_EXTERNAL (fndecl) = 1;
32140 DECL_DECLARED_INLINE_P (fndecl) = 1;
32141 DECL_IGNORED_P (fndecl) = 1;
32142 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
32143 DECL_ATTRIBUTES (fndecl)
32144 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
32145 DECL_ATTRIBUTES (fndecl));
32146 if (processing_template_decl)
32147 fndecl = push_template_decl (fndecl);
32148 bool block_scope = false;
32149 tree block = NULL_TREE;
32150 if (current_function_decl)
32152 block_scope = true;
32153 DECL_CONTEXT (fndecl) = global_namespace;
32154 if (!processing_template_decl)
32155 pushdecl (fndecl);
32157 else if (current_class_type)
32159 if (cp == NULL)
32161 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32162 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32163 cp_lexer_consume_token (parser->lexer);
32164 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32165 goto fail;
32166 cp = cp_token_cache_new (first_token,
32167 cp_lexer_peek_nth_token (parser->lexer,
32168 2));
32170 DECL_STATIC_FUNCTION_P (fndecl) = 1;
32171 finish_member_declaration (fndecl);
32172 DECL_PENDING_INLINE_INFO (fndecl) = cp;
32173 DECL_PENDING_INLINE_P (fndecl) = 1;
32174 vec_safe_push (unparsed_funs_with_definitions, fndecl);
32175 continue;
32177 else
32179 DECL_CONTEXT (fndecl) = current_namespace;
32180 pushdecl (fndecl);
32182 if (!block_scope)
32183 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
32184 else
32185 block = begin_omp_structured_block ();
32186 if (cp)
32188 cp_parser_push_lexer_for_tokens (parser, cp);
32189 parser->lexer->in_pragma = true;
32191 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
32193 if (!block_scope)
32194 finish_function (0);
32195 else
32196 DECL_CONTEXT (fndecl) = current_function_decl;
32197 if (cp)
32198 cp_parser_pop_lexer (parser);
32199 goto fail;
32201 if (cp)
32202 cp_parser_pop_lexer (parser);
32203 if (!block_scope)
32204 finish_function (0);
32205 else
32207 DECL_CONTEXT (fndecl) = current_function_decl;
32208 block = finish_omp_structured_block (block);
32209 if (TREE_CODE (block) == BIND_EXPR)
32210 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
32211 else if (TREE_CODE (block) == STATEMENT_LIST)
32212 DECL_SAVED_TREE (fndecl) = block;
32213 if (processing_template_decl)
32214 add_decl_expr (fndecl);
32216 cp_check_omp_declare_reduction (fndecl);
32217 if (cp == NULL && types.length () > 1)
32218 cp = cp_token_cache_new (first_token,
32219 cp_lexer_peek_nth_token (parser->lexer, 2));
32220 if (errs != errorcount)
32221 break;
32224 cp_parser_require_pragma_eol (parser, pragma_tok);
32226 done:
32227 /* Free any declarators allocated. */
32228 obstack_free (&declarator_obstack, p);
32231 /* OpenMP 4.0
32232 #pragma omp declare simd declare-simd-clauses[optseq] new-line
32233 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32234 initializer-clause[opt] new-line
32235 #pragma omp declare target new-line */
32237 static void
32238 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
32239 enum pragma_context context)
32241 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32243 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32244 const char *p = IDENTIFIER_POINTER (id);
32246 if (strcmp (p, "simd") == 0)
32248 cp_lexer_consume_token (parser->lexer);
32249 cp_parser_omp_declare_simd (parser, pragma_tok,
32250 context);
32251 return;
32253 cp_ensure_no_omp_declare_simd (parser);
32254 if (strcmp (p, "reduction") == 0)
32256 cp_lexer_consume_token (parser->lexer);
32257 cp_parser_omp_declare_reduction (parser, pragma_tok,
32258 context);
32259 return;
32261 if (!flag_openmp) /* flag_openmp_simd */
32263 cp_parser_require_pragma_eol (parser, pragma_tok);
32264 return;
32266 if (strcmp (p, "target") == 0)
32268 cp_lexer_consume_token (parser->lexer);
32269 cp_parser_omp_declare_target (parser, pragma_tok);
32270 return;
32273 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
32274 "or %<target%>");
32275 cp_parser_require_pragma_eol (parser, pragma_tok);
32278 /* Main entry point to OpenMP statement pragmas. */
32280 static void
32281 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
32283 tree stmt;
32284 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
32285 omp_clause_mask mask (0);
32287 switch (pragma_tok->pragma_kind)
32289 case PRAGMA_OACC_CACHE:
32290 stmt = cp_parser_oacc_cache (parser, pragma_tok);
32291 break;
32292 case PRAGMA_OACC_DATA:
32293 stmt = cp_parser_oacc_data (parser, pragma_tok);
32294 break;
32295 case PRAGMA_OACC_KERNELS:
32296 stmt = cp_parser_oacc_kernels (parser, pragma_tok);
32297 break;
32298 case PRAGMA_OACC_LOOP:
32299 stmt = cp_parser_oacc_loop (parser, pragma_tok);
32300 break;
32301 case PRAGMA_OACC_PARALLEL:
32302 stmt = cp_parser_oacc_parallel (parser, pragma_tok);
32303 break;
32304 case PRAGMA_OACC_UPDATE:
32305 stmt = cp_parser_oacc_update (parser, pragma_tok);
32306 break;
32307 case PRAGMA_OACC_WAIT:
32308 stmt = cp_parser_oacc_wait (parser, pragma_tok);
32309 break;
32310 case PRAGMA_OMP_ATOMIC:
32311 cp_parser_omp_atomic (parser, pragma_tok);
32312 return;
32313 case PRAGMA_OMP_CRITICAL:
32314 stmt = cp_parser_omp_critical (parser, pragma_tok);
32315 break;
32316 case PRAGMA_OMP_DISTRIBUTE:
32317 strcpy (p_name, "#pragma omp");
32318 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
32319 break;
32320 case PRAGMA_OMP_FOR:
32321 strcpy (p_name, "#pragma omp");
32322 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
32323 break;
32324 case PRAGMA_OMP_MASTER:
32325 stmt = cp_parser_omp_master (parser, pragma_tok);
32326 break;
32327 case PRAGMA_OMP_ORDERED:
32328 stmt = cp_parser_omp_ordered (parser, pragma_tok);
32329 break;
32330 case PRAGMA_OMP_PARALLEL:
32331 strcpy (p_name, "#pragma omp");
32332 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
32333 break;
32334 case PRAGMA_OMP_SECTIONS:
32335 strcpy (p_name, "#pragma omp");
32336 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
32337 break;
32338 case PRAGMA_OMP_SIMD:
32339 strcpy (p_name, "#pragma omp");
32340 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
32341 break;
32342 case PRAGMA_OMP_SINGLE:
32343 stmt = cp_parser_omp_single (parser, pragma_tok);
32344 break;
32345 case PRAGMA_OMP_TASK:
32346 stmt = cp_parser_omp_task (parser, pragma_tok);
32347 break;
32348 case PRAGMA_OMP_TASKGROUP:
32349 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
32350 break;
32351 case PRAGMA_OMP_TEAMS:
32352 strcpy (p_name, "#pragma omp");
32353 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
32354 break;
32355 default:
32356 gcc_unreachable ();
32359 if (stmt)
32360 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32363 /* Transactional Memory parsing routines. */
32365 /* Parse a transaction attribute.
32367 txn-attribute:
32368 attribute
32369 [ [ identifier ] ]
32371 ??? Simplify this when C++0x bracket attributes are
32372 implemented properly. */
32374 static tree
32375 cp_parser_txn_attribute_opt (cp_parser *parser)
32377 cp_token *token;
32378 tree attr_name, attr = NULL;
32380 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32381 return cp_parser_attributes_opt (parser);
32383 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
32384 return NULL_TREE;
32385 cp_lexer_consume_token (parser->lexer);
32386 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
32387 goto error1;
32389 token = cp_lexer_peek_token (parser->lexer);
32390 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
32392 token = cp_lexer_consume_token (parser->lexer);
32394 attr_name = (token->type == CPP_KEYWORD
32395 /* For keywords, use the canonical spelling,
32396 not the parsed identifier. */
32397 ? ridpointers[(int) token->keyword]
32398 : token->u.value);
32399 attr = build_tree_list (attr_name, NULL_TREE);
32401 else
32402 cp_parser_error (parser, "expected identifier");
32404 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32405 error1:
32406 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32407 return attr;
32410 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32412 transaction-statement:
32413 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32414 compound-statement
32415 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32418 static tree
32419 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32421 unsigned char old_in = parser->in_transaction;
32422 unsigned char this_in = 1, new_in;
32423 cp_token *token;
32424 tree stmt, attrs, noex;
32426 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32427 || keyword == RID_TRANSACTION_RELAXED);
32428 token = cp_parser_require_keyword (parser, keyword,
32429 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32430 : RT_TRANSACTION_RELAXED));
32431 gcc_assert (token != NULL);
32433 if (keyword == RID_TRANSACTION_RELAXED)
32434 this_in |= TM_STMT_ATTR_RELAXED;
32435 else
32437 attrs = cp_parser_txn_attribute_opt (parser);
32438 if (attrs)
32439 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32442 /* Parse a noexcept specification. */
32443 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32445 /* Keep track if we're in the lexical scope of an outer transaction. */
32446 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32448 stmt = begin_transaction_stmt (token->location, NULL, this_in);
32450 parser->in_transaction = new_in;
32451 cp_parser_compound_statement (parser, NULL, false, false);
32452 parser->in_transaction = old_in;
32454 finish_transaction_stmt (stmt, NULL, this_in, noex);
32456 return stmt;
32459 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32461 transaction-expression:
32462 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32463 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32466 static tree
32467 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32469 unsigned char old_in = parser->in_transaction;
32470 unsigned char this_in = 1;
32471 cp_token *token;
32472 tree expr, noex;
32473 bool noex_expr;
32475 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32476 || keyword == RID_TRANSACTION_RELAXED);
32478 if (!flag_tm)
32479 error (keyword == RID_TRANSACTION_RELAXED
32480 ? G_("%<__transaction_relaxed%> without transactional memory "
32481 "support enabled")
32482 : G_("%<__transaction_atomic%> without transactional memory "
32483 "support enabled"));
32485 token = cp_parser_require_keyword (parser, keyword,
32486 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32487 : RT_TRANSACTION_RELAXED));
32488 gcc_assert (token != NULL);
32490 if (keyword == RID_TRANSACTION_RELAXED)
32491 this_in |= TM_STMT_ATTR_RELAXED;
32493 /* Set this early. This might mean that we allow transaction_cancel in
32494 an expression that we find out later actually has to be a constexpr.
32495 However, we expect that cxx_constant_value will be able to deal with
32496 this; also, if the noexcept has no constexpr, then what we parse next
32497 really is a transaction's body. */
32498 parser->in_transaction = this_in;
32500 /* Parse a noexcept specification. */
32501 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32502 true);
32504 if (!noex || !noex_expr
32505 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32507 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32509 expr = cp_parser_expression (parser);
32510 expr = finish_parenthesized_expr (expr);
32512 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32514 else
32516 /* The only expression that is available got parsed for the noexcept
32517 already. noexcept is true then. */
32518 expr = noex;
32519 noex = boolean_true_node;
32522 expr = build_transaction_expr (token->location, expr, this_in, noex);
32523 parser->in_transaction = old_in;
32525 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32526 return error_mark_node;
32528 return (flag_tm ? expr : error_mark_node);
32531 /* Parse a function-transaction-block.
32533 function-transaction-block:
32534 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32535 function-body
32536 __transaction_atomic txn-attribute[opt] function-try-block
32537 __transaction_relaxed ctor-initializer[opt] function-body
32538 __transaction_relaxed function-try-block
32541 static bool
32542 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32544 unsigned char old_in = parser->in_transaction;
32545 unsigned char new_in = 1;
32546 tree compound_stmt, stmt, attrs;
32547 bool ctor_initializer_p;
32548 cp_token *token;
32550 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32551 || keyword == RID_TRANSACTION_RELAXED);
32552 token = cp_parser_require_keyword (parser, keyword,
32553 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32554 : RT_TRANSACTION_RELAXED));
32555 gcc_assert (token != NULL);
32557 if (keyword == RID_TRANSACTION_RELAXED)
32558 new_in |= TM_STMT_ATTR_RELAXED;
32559 else
32561 attrs = cp_parser_txn_attribute_opt (parser);
32562 if (attrs)
32563 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32566 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32568 parser->in_transaction = new_in;
32570 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32571 ctor_initializer_p = cp_parser_function_try_block (parser);
32572 else
32573 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32574 (parser, /*in_function_try_block=*/false);
32576 parser->in_transaction = old_in;
32578 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32580 return ctor_initializer_p;
32583 /* Parse a __transaction_cancel statement.
32585 cancel-statement:
32586 __transaction_cancel txn-attribute[opt] ;
32587 __transaction_cancel txn-attribute[opt] throw-expression ;
32589 ??? Cancel and throw is not yet implemented. */
32591 static tree
32592 cp_parser_transaction_cancel (cp_parser *parser)
32594 cp_token *token;
32595 bool is_outer = false;
32596 tree stmt, attrs;
32598 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32599 RT_TRANSACTION_CANCEL);
32600 gcc_assert (token != NULL);
32602 attrs = cp_parser_txn_attribute_opt (parser);
32603 if (attrs)
32604 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32606 /* ??? Parse cancel-and-throw here. */
32608 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32610 if (!flag_tm)
32612 error_at (token->location, "%<__transaction_cancel%> without "
32613 "transactional memory support enabled");
32614 return error_mark_node;
32616 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32618 error_at (token->location, "%<__transaction_cancel%> within a "
32619 "%<__transaction_relaxed%>");
32620 return error_mark_node;
32622 else if (is_outer)
32624 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32625 && !is_tm_may_cancel_outer (current_function_decl))
32627 error_at (token->location, "outer %<__transaction_cancel%> not "
32628 "within outer %<__transaction_atomic%>");
32629 error_at (token->location,
32630 " or a %<transaction_may_cancel_outer%> function");
32631 return error_mark_node;
32634 else if (parser->in_transaction == 0)
32636 error_at (token->location, "%<__transaction_cancel%> not within "
32637 "%<__transaction_atomic%>");
32638 return error_mark_node;
32641 stmt = build_tm_abort_call (token->location, is_outer);
32642 add_stmt (stmt);
32644 return stmt;
32647 /* The parser. */
32649 static GTY (()) cp_parser *the_parser;
32652 /* Special handling for the first token or line in the file. The first
32653 thing in the file might be #pragma GCC pch_preprocess, which loads a
32654 PCH file, which is a GC collection point. So we need to handle this
32655 first pragma without benefit of an existing lexer structure.
32657 Always returns one token to the caller in *FIRST_TOKEN. This is
32658 either the true first token of the file, or the first token after
32659 the initial pragma. */
32661 static void
32662 cp_parser_initial_pragma (cp_token *first_token)
32664 tree name = NULL;
32666 cp_lexer_get_preprocessor_token (NULL, first_token);
32667 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32668 return;
32670 cp_lexer_get_preprocessor_token (NULL, first_token);
32671 if (first_token->type == CPP_STRING)
32673 name = first_token->u.value;
32675 cp_lexer_get_preprocessor_token (NULL, first_token);
32676 if (first_token->type != CPP_PRAGMA_EOL)
32677 error_at (first_token->location,
32678 "junk at end of %<#pragma GCC pch_preprocess%>");
32680 else
32681 error_at (first_token->location, "expected string literal");
32683 /* Skip to the end of the pragma. */
32684 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32685 cp_lexer_get_preprocessor_token (NULL, first_token);
32687 /* Now actually load the PCH file. */
32688 if (name)
32689 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32691 /* Read one more token to return to our caller. We have to do this
32692 after reading the PCH file in, since its pointers have to be
32693 live. */
32694 cp_lexer_get_preprocessor_token (NULL, first_token);
32697 /* Parses the grainsize pragma for the _Cilk_for statement.
32698 Syntax:
32699 #pragma cilk grainsize = <VALUE>. */
32701 static void
32702 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
32704 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
32706 tree exp = cp_parser_binary_expression (parser, false, false,
32707 PREC_NOT_OPERATOR, NULL);
32708 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32709 if (!exp || exp == error_mark_node)
32711 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
32712 return;
32715 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
32716 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32717 cp_parser_cilk_for (parser, exp);
32718 else
32719 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
32720 "%<#pragma cilk grainsize%> is not followed by "
32721 "%<_Cilk_for%>");
32722 return;
32724 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32727 /* Normal parsing of a pragma token. Here we can (and must) use the
32728 regular lexer. */
32730 static bool
32731 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32733 cp_token *pragma_tok;
32734 unsigned int id;
32736 pragma_tok = cp_lexer_consume_token (parser->lexer);
32737 gcc_assert (pragma_tok->type == CPP_PRAGMA);
32738 parser->lexer->in_pragma = true;
32740 id = pragma_tok->pragma_kind;
32741 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
32742 cp_ensure_no_omp_declare_simd (parser);
32743 switch (id)
32745 case PRAGMA_GCC_PCH_PREPROCESS:
32746 error_at (pragma_tok->location,
32747 "%<#pragma GCC pch_preprocess%> must be first");
32748 break;
32750 case PRAGMA_OMP_BARRIER:
32751 switch (context)
32753 case pragma_compound:
32754 cp_parser_omp_barrier (parser, pragma_tok);
32755 return false;
32756 case pragma_stmt:
32757 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32758 "used in compound statements");
32759 break;
32760 default:
32761 goto bad_stmt;
32763 break;
32765 case PRAGMA_OMP_FLUSH:
32766 switch (context)
32768 case pragma_compound:
32769 cp_parser_omp_flush (parser, pragma_tok);
32770 return false;
32771 case pragma_stmt:
32772 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32773 "used in compound statements");
32774 break;
32775 default:
32776 goto bad_stmt;
32778 break;
32780 case PRAGMA_OMP_TASKWAIT:
32781 switch (context)
32783 case pragma_compound:
32784 cp_parser_omp_taskwait (parser, pragma_tok);
32785 return false;
32786 case pragma_stmt:
32787 error_at (pragma_tok->location,
32788 "%<#pragma omp taskwait%> may only be "
32789 "used in compound statements");
32790 break;
32791 default:
32792 goto bad_stmt;
32794 break;
32796 case PRAGMA_OMP_TASKYIELD:
32797 switch (context)
32799 case pragma_compound:
32800 cp_parser_omp_taskyield (parser, pragma_tok);
32801 return false;
32802 case pragma_stmt:
32803 error_at (pragma_tok->location,
32804 "%<#pragma omp taskyield%> may only be "
32805 "used in compound statements");
32806 break;
32807 default:
32808 goto bad_stmt;
32810 break;
32812 case PRAGMA_OMP_CANCEL:
32813 switch (context)
32815 case pragma_compound:
32816 cp_parser_omp_cancel (parser, pragma_tok);
32817 return false;
32818 case pragma_stmt:
32819 error_at (pragma_tok->location,
32820 "%<#pragma omp cancel%> may only be "
32821 "used in compound statements");
32822 break;
32823 default:
32824 goto bad_stmt;
32826 break;
32828 case PRAGMA_OMP_CANCELLATION_POINT:
32829 switch (context)
32831 case pragma_compound:
32832 cp_parser_omp_cancellation_point (parser, pragma_tok);
32833 return false;
32834 case pragma_stmt:
32835 error_at (pragma_tok->location,
32836 "%<#pragma omp cancellation point%> may only be "
32837 "used in compound statements");
32838 break;
32839 default:
32840 goto bad_stmt;
32842 break;
32844 case PRAGMA_OMP_THREADPRIVATE:
32845 cp_parser_omp_threadprivate (parser, pragma_tok);
32846 return false;
32848 case PRAGMA_OMP_DECLARE_REDUCTION:
32849 cp_parser_omp_declare (parser, pragma_tok, context);
32850 return false;
32852 case PRAGMA_OACC_CACHE:
32853 case PRAGMA_OACC_DATA:
32854 case PRAGMA_OACC_KERNELS:
32855 case PRAGMA_OACC_PARALLEL:
32856 case PRAGMA_OACC_LOOP:
32857 case PRAGMA_OACC_UPDATE:
32858 case PRAGMA_OACC_WAIT:
32859 case PRAGMA_OMP_ATOMIC:
32860 case PRAGMA_OMP_CRITICAL:
32861 case PRAGMA_OMP_DISTRIBUTE:
32862 case PRAGMA_OMP_FOR:
32863 case PRAGMA_OMP_MASTER:
32864 case PRAGMA_OMP_ORDERED:
32865 case PRAGMA_OMP_PARALLEL:
32866 case PRAGMA_OMP_SECTIONS:
32867 case PRAGMA_OMP_SIMD:
32868 case PRAGMA_OMP_SINGLE:
32869 case PRAGMA_OMP_TASK:
32870 case PRAGMA_OMP_TASKGROUP:
32871 case PRAGMA_OMP_TEAMS:
32872 if (context != pragma_stmt && context != pragma_compound)
32873 goto bad_stmt;
32874 cp_parser_omp_construct (parser, pragma_tok);
32875 return true;
32877 case PRAGMA_OMP_TARGET:
32878 return cp_parser_omp_target (parser, pragma_tok, context);
32880 case PRAGMA_OMP_END_DECLARE_TARGET:
32881 cp_parser_omp_end_declare_target (parser, pragma_tok);
32882 return false;
32884 case PRAGMA_OMP_SECTION:
32885 error_at (pragma_tok->location,
32886 "%<#pragma omp section%> may only be used in "
32887 "%<#pragma omp sections%> construct");
32888 break;
32890 case PRAGMA_IVDEP:
32892 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32893 cp_token *tok;
32894 tok = cp_lexer_peek_token (the_parser->lexer);
32895 if (tok->type != CPP_KEYWORD
32896 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
32897 && tok->keyword != RID_DO))
32899 cp_parser_error (parser, "for, while or do statement expected");
32900 return false;
32902 cp_parser_iteration_statement (parser, true);
32903 return true;
32906 case PRAGMA_CILK_SIMD:
32907 if (context == pragma_external)
32909 error_at (pragma_tok->location,
32910 "%<#pragma simd%> must be inside a function");
32911 break;
32913 cp_parser_cilk_simd (parser, pragma_tok);
32914 return true;
32916 case PRAGMA_CILK_GRAINSIZE:
32917 if (context == pragma_external)
32919 error_at (pragma_tok->location,
32920 "%<#pragma cilk grainsize%> must be inside a function");
32921 break;
32924 /* Ignore the pragma if Cilk Plus is not enabled. */
32925 if (flag_cilkplus)
32927 cp_parser_cilk_grainsize (parser, pragma_tok);
32928 return true;
32930 else
32932 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
32933 "%<#pragma cilk grainsize%>");
32934 break;
32937 default:
32938 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
32939 c_invoke_pragma_handler (id);
32940 break;
32942 bad_stmt:
32943 cp_parser_error (parser, "expected declaration specifiers");
32944 break;
32947 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32948 return false;
32951 /* The interface the pragma parsers have to the lexer. */
32953 enum cpp_ttype
32954 pragma_lex (tree *value)
32956 cp_token *tok;
32957 enum cpp_ttype ret;
32959 tok = cp_lexer_peek_token (the_parser->lexer);
32961 ret = tok->type;
32962 *value = tok->u.value;
32964 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
32965 ret = CPP_EOF;
32966 else if (ret == CPP_STRING)
32967 *value = cp_parser_string_literal (the_parser, false, false);
32968 else
32970 cp_lexer_consume_token (the_parser->lexer);
32971 if (ret == CPP_KEYWORD)
32972 ret = CPP_NAME;
32975 return ret;
32979 /* External interface. */
32981 /* Parse one entire translation unit. */
32983 void
32984 c_parse_file (void)
32986 static bool already_called = false;
32988 if (already_called)
32989 fatal_error ("inter-module optimizations not implemented for C++");
32990 already_called = true;
32992 the_parser = cp_parser_new ();
32993 push_deferring_access_checks (flag_access_control
32994 ? dk_no_deferred : dk_no_check);
32995 cp_parser_translation_unit (the_parser);
32996 the_parser = NULL;
32999 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
33000 vectorlength clause:
33001 Syntax:
33002 vectorlength ( constant-expression ) */
33004 static tree
33005 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
33006 bool is_simd_fn)
33008 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33009 tree expr;
33010 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33011 safelen clause. Thus, vectorlength is represented as OMP 4.0
33012 safelen. For SIMD-enabled function it is represented by OMP 4.0
33013 simdlen. */
33014 if (!is_simd_fn)
33015 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
33016 loc);
33017 else
33018 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
33019 loc);
33021 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33022 return error_mark_node;
33024 expr = cp_parser_constant_expression (parser);
33025 expr = maybe_constant_value (expr);
33027 /* If expr == error_mark_node, then don't emit any errors nor
33028 create a clause. if any of the above functions returns
33029 error mark node then they would have emitted an error message. */
33030 if (expr == error_mark_node)
33032 else if (!TREE_TYPE (expr)
33033 || !TREE_CONSTANT (expr)
33034 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
33035 error_at (loc, "vectorlength must be an integer constant");
33036 else if (TREE_CONSTANT (expr)
33037 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
33038 error_at (loc, "vectorlength must be a power of 2");
33039 else
33041 tree c;
33042 if (!is_simd_fn)
33044 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
33045 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
33046 OMP_CLAUSE_CHAIN (c) = clauses;
33047 clauses = c;
33049 else
33051 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
33052 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
33053 OMP_CLAUSE_CHAIN (c) = clauses;
33054 clauses = c;
33058 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33059 return error_mark_node;
33060 return clauses;
33063 /* Handles the Cilk Plus #pragma simd linear clause.
33064 Syntax:
33065 linear ( simd-linear-variable-list )
33067 simd-linear-variable-list:
33068 simd-linear-variable
33069 simd-linear-variable-list , simd-linear-variable
33071 simd-linear-variable:
33072 id-expression
33073 id-expression : simd-linear-step
33075 simd-linear-step:
33076 conditional-expression */
33078 static tree
33079 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
33081 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33083 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33084 return clauses;
33085 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33087 cp_parser_error (parser, "expected identifier");
33088 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33089 return error_mark_node;
33092 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33093 parser->colon_corrects_to_scope_p = false;
33094 while (1)
33096 cp_token *token = cp_lexer_peek_token (parser->lexer);
33097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33099 cp_parser_error (parser, "expected variable-name");
33100 clauses = error_mark_node;
33101 break;
33104 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
33105 false, false);
33106 tree decl = cp_parser_lookup_name_simple (parser, var_name,
33107 token->location);
33108 if (decl == error_mark_node)
33110 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
33111 token->location);
33112 clauses = error_mark_node;
33114 else
33116 tree e = NULL_TREE;
33117 tree step_size = integer_one_node;
33119 /* If present, parse the linear step. Otherwise, assume the default
33120 value of 1. */
33121 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
33123 cp_lexer_consume_token (parser->lexer);
33125 e = cp_parser_assignment_expression (parser);
33126 e = maybe_constant_value (e);
33128 if (e == error_mark_node)
33130 /* If an error has occurred, then the whole pragma is
33131 considered ill-formed. Thus, no reason to keep
33132 parsing. */
33133 clauses = error_mark_node;
33134 break;
33136 else if (type_dependent_expression_p (e)
33137 || value_dependent_expression_p (e)
33138 || (TREE_TYPE (e)
33139 && INTEGRAL_TYPE_P (TREE_TYPE (e))
33140 && (TREE_CONSTANT (e)
33141 || DECL_P (e))))
33142 step_size = e;
33143 else
33144 cp_parser_error (parser,
33145 "step size must be an integer constant "
33146 "expression or an integer variable");
33149 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
33150 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33151 OMP_CLAUSE_DECL (l) = decl;
33152 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
33153 OMP_CLAUSE_CHAIN (l) = clauses;
33154 clauses = l;
33156 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33157 cp_lexer_consume_token (parser->lexer);
33158 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33159 break;
33160 else
33162 error_at (cp_lexer_peek_token (parser->lexer)->location,
33163 "expected %<,%> or %<)%> after %qE", decl);
33164 clauses = error_mark_node;
33165 break;
33168 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33169 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33170 return clauses;
33173 /* Returns the name of the next clause. If the clause is not
33174 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33175 token is not consumed. Otherwise, the appropriate enum from the
33176 pragma_simd_clause is returned and the token is consumed. */
33178 static pragma_omp_clause
33179 cp_parser_cilk_simd_clause_name (cp_parser *parser)
33181 pragma_omp_clause clause_type;
33182 cp_token *token = cp_lexer_peek_token (parser->lexer);
33184 if (token->keyword == RID_PRIVATE)
33185 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
33186 else if (!token->u.value || token->type != CPP_NAME)
33187 return PRAGMA_CILK_CLAUSE_NONE;
33188 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
33189 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
33190 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
33191 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
33192 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
33193 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
33194 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
33195 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
33196 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
33197 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
33198 else
33199 return PRAGMA_CILK_CLAUSE_NONE;
33201 cp_lexer_consume_token (parser->lexer);
33202 return clause_type;
33205 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
33207 static tree
33208 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
33210 tree clauses = NULL_TREE;
33212 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33213 && clauses != error_mark_node)
33215 pragma_omp_clause c_kind;
33216 c_kind = cp_parser_cilk_simd_clause_name (parser);
33217 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
33218 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
33219 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
33220 clauses = cp_parser_cilk_simd_linear (parser, clauses);
33221 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
33222 /* Use the OpenMP 4.0 equivalent function. */
33223 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
33224 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
33225 /* Use the OpenMP 4.0 equivalent function. */
33226 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33227 clauses);
33228 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
33229 /* Use the OMP 4.0 equivalent function. */
33230 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33231 clauses);
33232 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
33233 /* Use the OMP 4.0 equivalent function. */
33234 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33235 else
33237 clauses = error_mark_node;
33238 cp_parser_error (parser, "expected %<#pragma simd%> clause");
33239 break;
33243 cp_parser_skip_to_pragma_eol (parser, pragma_token);
33245 if (clauses == error_mark_node)
33246 return error_mark_node;
33247 else
33248 return c_finish_cilk_clauses (clauses);
33251 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
33253 static void
33254 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
33256 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
33258 if (clauses == error_mark_node)
33259 return;
33261 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
33263 error_at (cp_lexer_peek_token (parser->lexer)->location,
33264 "for statement expected");
33265 return;
33268 tree sb = begin_omp_structured_block ();
33269 int save = cp_parser_begin_omp_structured_block (parser);
33270 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
33271 if (ret)
33272 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
33273 cp_parser_end_omp_structured_block (parser, save);
33274 add_stmt (finish_omp_structured_block (sb));
33277 /* Main entry-point for parsing Cilk Plus _Cilk_for
33278 loops. The return value is error_mark_node
33279 when errors happen and CILK_FOR tree on success. */
33281 static tree
33282 cp_parser_cilk_for (cp_parser *parser, tree grain)
33284 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
33285 gcc_unreachable ();
33287 tree sb = begin_omp_structured_block ();
33288 int save = cp_parser_begin_omp_structured_block (parser);
33290 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
33291 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
33292 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
33293 clauses = finish_omp_clauses (clauses);
33295 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
33296 if (ret)
33297 cpp_validate_cilk_plus_loop (ret);
33298 else
33299 ret = error_mark_node;
33301 cp_parser_end_omp_structured_block (parser, save);
33302 add_stmt (finish_omp_structured_block (sb));
33303 return ret;
33306 /* Create an identifier for a generic parameter type (a synthesized
33307 template parameter implied by `auto' or a concept identifier). */
33309 static GTY(()) int generic_parm_count;
33310 static tree
33311 make_generic_type_name ()
33313 char buf[32];
33314 sprintf (buf, "auto:%d", ++generic_parm_count);
33315 return get_identifier (buf);
33318 /* Predicate that behaves as is_auto_or_concept but matches the parent
33319 node of the generic type rather than the generic type itself. This
33320 allows for type transformation in add_implicit_template_parms. */
33322 static inline bool
33323 tree_type_is_auto_or_concept (const_tree t)
33325 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
33328 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33329 (creating a new template parameter list if necessary). Returns the newly
33330 created template type parm. */
33332 tree
33333 synthesize_implicit_template_parm (cp_parser *parser)
33335 gcc_assert (current_binding_level->kind == sk_function_parms);
33337 /* We are either continuing a function template that already contains implicit
33338 template parameters, creating a new fully-implicit function template, or
33339 extending an existing explicit function template with implicit template
33340 parameters. */
33342 cp_binding_level *const entry_scope = current_binding_level;
33344 bool become_template = false;
33345 cp_binding_level *parent_scope = 0;
33347 if (parser->implicit_template_scope)
33349 gcc_assert (parser->implicit_template_parms);
33351 current_binding_level = parser->implicit_template_scope;
33353 else
33355 /* Roll back to the existing template parameter scope (in the case of
33356 extending an explicit function template) or introduce a new template
33357 parameter scope ahead of the function parameter scope (or class scope
33358 in the case of out-of-line member definitions). The function scope is
33359 added back after template parameter synthesis below. */
33361 cp_binding_level *scope = entry_scope;
33363 while (scope->kind == sk_function_parms)
33365 parent_scope = scope;
33366 scope = scope->level_chain;
33368 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
33370 /* If not defining a class, then any class scope is a scope level in
33371 an out-of-line member definition. In this case simply wind back
33372 beyond the first such scope to inject the template parameter list.
33373 Otherwise wind back to the class being defined. The latter can
33374 occur in class member friend declarations such as:
33376 class A {
33377 void foo (auto);
33379 class B {
33380 friend void A::foo (auto);
33383 The template parameter list synthesized for the friend declaration
33384 must be injected in the scope of 'B'. This can also occur in
33385 erroneous cases such as:
33387 struct A {
33388 struct B {
33389 void foo (auto);
33391 void B::foo (auto) {}
33394 Here the attempted definition of 'B::foo' within 'A' is ill-formed
33395 but, nevertheless, the template parameter list synthesized for the
33396 declarator should be injected into the scope of 'A' as if the
33397 ill-formed template was specified explicitly. */
33399 while (scope->kind == sk_class && !scope->defining_class_p)
33401 parent_scope = scope;
33402 scope = scope->level_chain;
33406 current_binding_level = scope;
33408 if (scope->kind != sk_template_parms
33409 || !function_being_declared_is_template_p (parser))
33411 /* Introduce a new template parameter list for implicit template
33412 parameters. */
33414 become_template = true;
33416 parser->implicit_template_scope
33417 = begin_scope (sk_template_parms, NULL);
33419 ++processing_template_decl;
33421 parser->fully_implicit_function_template_p = true;
33422 ++parser->num_template_parameter_lists;
33424 else
33426 /* Synthesize implicit template parameters at the end of the explicit
33427 template parameter list. */
33429 gcc_assert (current_template_parms);
33431 parser->implicit_template_scope = scope;
33433 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33434 parser->implicit_template_parms
33435 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
33439 /* Synthesize a new template parameter and track the current template
33440 parameter chain with implicit_template_parms. */
33442 tree synth_id = make_generic_type_name ();
33443 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33444 synth_id);
33445 tree new_parm
33446 = process_template_parm (parser->implicit_template_parms,
33447 input_location,
33448 build_tree_list (NULL_TREE, synth_tmpl_parm),
33449 /*non_type=*/false,
33450 /*param_pack=*/false);
33453 if (parser->implicit_template_parms)
33454 parser->implicit_template_parms
33455 = TREE_CHAIN (parser->implicit_template_parms);
33456 else
33457 parser->implicit_template_parms = new_parm;
33459 tree new_type = TREE_TYPE (getdecls ());
33461 /* If creating a fully implicit function template, start the new implicit
33462 template parameter list with this synthesized type, otherwise grow the
33463 current template parameter list. */
33465 if (become_template)
33467 parent_scope->level_chain = current_binding_level;
33469 tree new_parms = make_tree_vec (1);
33470 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33471 current_template_parms = tree_cons (size_int (processing_template_decl),
33472 new_parms, current_template_parms);
33474 else
33476 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33477 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33478 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33479 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33482 current_binding_level = entry_scope;
33484 return new_type;
33487 /* Finish the declaration of a fully implicit function template. Such a
33488 template has no explicit template parameter list so has not been through the
33489 normal template head and tail processing. synthesize_implicit_template_parm
33490 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33491 provided if the declaration is a class member such that its template
33492 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33493 form is returned. Otherwise NULL_TREE is returned. */
33495 tree
33496 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33498 gcc_assert (parser->fully_implicit_function_template_p);
33500 if (member_decl_opt && member_decl_opt != error_mark_node
33501 && DECL_VIRTUAL_P (member_decl_opt))
33503 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33504 "implicit templates may not be %<virtual%>");
33505 DECL_VIRTUAL_P (member_decl_opt) = false;
33508 if (member_decl_opt)
33509 member_decl_opt = finish_member_template_decl (member_decl_opt);
33510 end_template_decl ();
33512 parser->fully_implicit_function_template_p = false;
33513 --parser->num_template_parameter_lists;
33515 return member_decl_opt;
33518 #include "gt-cp-parser.h"