svn merge -r 216846:217483 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / cp / parser.c
blobd16e0d34fa5130017db0ddec261580219de18232
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:
4505 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4506 __func__ are the names of variables. */
4507 goto id_expression;
4509 case RID_VA_ARG:
4511 tree expression;
4512 tree type;
4513 source_location type_location;
4515 /* The `__builtin_va_arg' construct is used to handle
4516 `va_arg'. Consume the `__builtin_va_arg' token. */
4517 cp_lexer_consume_token (parser->lexer);
4518 /* Look for the opening `('. */
4519 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4520 /* Now, parse the assignment-expression. */
4521 expression = cp_parser_assignment_expression (parser);
4522 /* Look for the `,'. */
4523 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4524 type_location = cp_lexer_peek_token (parser->lexer)->location;
4525 /* Parse the type-id. */
4526 type = cp_parser_type_id (parser);
4527 /* Look for the closing `)'. */
4528 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4529 /* Using `va_arg' in a constant-expression is not
4530 allowed. */
4531 if (cp_parser_non_integral_constant_expression (parser,
4532 NIC_VA_ARG))
4533 return error_mark_node;
4534 return build_x_va_arg (type_location, expression, type);
4537 case RID_OFFSETOF:
4538 return cp_parser_builtin_offsetof (parser);
4540 case RID_HAS_NOTHROW_ASSIGN:
4541 case RID_HAS_NOTHROW_CONSTRUCTOR:
4542 case RID_HAS_NOTHROW_COPY:
4543 case RID_HAS_TRIVIAL_ASSIGN:
4544 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4545 case RID_HAS_TRIVIAL_COPY:
4546 case RID_HAS_TRIVIAL_DESTRUCTOR:
4547 case RID_HAS_VIRTUAL_DESTRUCTOR:
4548 case RID_IS_ABSTRACT:
4549 case RID_IS_BASE_OF:
4550 case RID_IS_CLASS:
4551 case RID_IS_EMPTY:
4552 case RID_IS_ENUM:
4553 case RID_IS_FINAL:
4554 case RID_IS_LITERAL_TYPE:
4555 case RID_IS_POD:
4556 case RID_IS_POLYMORPHIC:
4557 case RID_IS_STD_LAYOUT:
4558 case RID_IS_TRIVIAL:
4559 case RID_IS_TRIVIALLY_ASSIGNABLE:
4560 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4561 case RID_IS_TRIVIALLY_COPYABLE:
4562 case RID_IS_UNION:
4563 return cp_parser_trait_expr (parser, token->keyword);
4565 /* Objective-C++ expressions. */
4566 case RID_AT_ENCODE:
4567 case RID_AT_PROTOCOL:
4568 case RID_AT_SELECTOR:
4569 return cp_parser_objc_expression (parser);
4571 case RID_TEMPLATE:
4572 if (parser->in_function_body
4573 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4574 == CPP_LESS))
4576 error_at (token->location,
4577 "a template declaration cannot appear at block scope");
4578 cp_parser_skip_to_end_of_block_or_statement (parser);
4579 return error_mark_node;
4581 default:
4582 cp_parser_error (parser, "expected primary-expression");
4583 return error_mark_node;
4586 /* An id-expression can start with either an identifier, a
4587 `::' as the beginning of a qualified-id, or the "operator"
4588 keyword. */
4589 case CPP_NAME:
4590 case CPP_SCOPE:
4591 case CPP_TEMPLATE_ID:
4592 case CPP_NESTED_NAME_SPECIFIER:
4594 tree id_expression;
4595 tree decl;
4596 const char *error_msg;
4597 bool template_p;
4598 bool done;
4599 cp_token *id_expr_token;
4601 id_expression:
4602 /* Parse the id-expression. */
4603 id_expression
4604 = cp_parser_id_expression (parser,
4605 /*template_keyword_p=*/false,
4606 /*check_dependency_p=*/true,
4607 &template_p,
4608 /*declarator_p=*/false,
4609 /*optional_p=*/false);
4610 if (id_expression == error_mark_node)
4611 return error_mark_node;
4612 id_expr_token = token;
4613 token = cp_lexer_peek_token (parser->lexer);
4614 done = (token->type != CPP_OPEN_SQUARE
4615 && token->type != CPP_OPEN_PAREN
4616 && token->type != CPP_DOT
4617 && token->type != CPP_DEREF
4618 && token->type != CPP_PLUS_PLUS
4619 && token->type != CPP_MINUS_MINUS);
4620 /* If we have a template-id, then no further lookup is
4621 required. If the template-id was for a template-class, we
4622 will sometimes have a TYPE_DECL at this point. */
4623 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4624 || TREE_CODE (id_expression) == TYPE_DECL)
4625 decl = id_expression;
4626 /* Look up the name. */
4627 else
4629 tree ambiguous_decls;
4631 /* If we already know that this lookup is ambiguous, then
4632 we've already issued an error message; there's no reason
4633 to check again. */
4634 if (id_expr_token->type == CPP_NAME
4635 && id_expr_token->error_reported)
4637 cp_parser_simulate_error (parser);
4638 return error_mark_node;
4641 decl = cp_parser_lookup_name (parser, id_expression,
4642 none_type,
4643 template_p,
4644 /*is_namespace=*/false,
4645 /*check_dependency=*/true,
4646 &ambiguous_decls,
4647 id_expr_token->location);
4648 /* If the lookup was ambiguous, an error will already have
4649 been issued. */
4650 if (ambiguous_decls)
4651 return error_mark_node;
4653 /* In Objective-C++, we may have an Objective-C 2.0
4654 dot-syntax for classes here. */
4655 if (c_dialect_objc ()
4656 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4657 && TREE_CODE (decl) == TYPE_DECL
4658 && objc_is_class_name (decl))
4660 tree component;
4661 cp_lexer_consume_token (parser->lexer);
4662 component = cp_parser_identifier (parser);
4663 if (component == error_mark_node)
4664 return error_mark_node;
4666 return objc_build_class_component_ref (id_expression, component);
4669 /* In Objective-C++, an instance variable (ivar) may be preferred
4670 to whatever cp_parser_lookup_name() found. */
4671 decl = objc_lookup_ivar (decl, id_expression);
4673 /* If name lookup gives us a SCOPE_REF, then the
4674 qualifying scope was dependent. */
4675 if (TREE_CODE (decl) == SCOPE_REF)
4677 /* At this point, we do not know if DECL is a valid
4678 integral constant expression. We assume that it is
4679 in fact such an expression, so that code like:
4681 template <int N> struct A {
4682 int a[B<N>::i];
4685 is accepted. At template-instantiation time, we
4686 will check that B<N>::i is actually a constant. */
4687 return decl;
4689 /* Check to see if DECL is a local variable in a context
4690 where that is forbidden. */
4691 if (parser->local_variables_forbidden_p
4692 && local_variable_p (decl))
4694 /* It might be that we only found DECL because we are
4695 trying to be generous with pre-ISO scoping rules.
4696 For example, consider:
4698 int i;
4699 void g() {
4700 for (int i = 0; i < 10; ++i) {}
4701 extern void f(int j = i);
4704 Here, name look up will originally find the out
4705 of scope `i'. We need to issue a warning message,
4706 but then use the global `i'. */
4707 decl = check_for_out_of_scope_variable (decl);
4708 if (local_variable_p (decl))
4710 error_at (id_expr_token->location,
4711 "local variable %qD may not appear in this context",
4712 decl);
4713 return error_mark_node;
4718 decl = (finish_id_expression
4719 (id_expression, decl, parser->scope,
4720 idk,
4721 parser->integral_constant_expression_p,
4722 parser->allow_non_integral_constant_expression_p,
4723 &parser->non_integral_constant_expression_p,
4724 template_p, done, address_p,
4725 template_arg_p,
4726 &error_msg,
4727 id_expr_token->location));
4728 if (error_msg)
4729 cp_parser_error (parser, error_msg);
4730 return decl;
4733 /* Anything else is an error. */
4734 default:
4735 cp_parser_error (parser, "expected primary-expression");
4736 return error_mark_node;
4740 static inline tree
4741 cp_parser_primary_expression (cp_parser *parser,
4742 bool address_p,
4743 bool cast_p,
4744 bool template_arg_p,
4745 cp_id_kind *idk)
4747 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4748 /*decltype*/false, idk);
4751 /* Parse an id-expression.
4753 id-expression:
4754 unqualified-id
4755 qualified-id
4757 qualified-id:
4758 :: [opt] nested-name-specifier template [opt] unqualified-id
4759 :: identifier
4760 :: operator-function-id
4761 :: template-id
4763 Return a representation of the unqualified portion of the
4764 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4765 a `::' or nested-name-specifier.
4767 Often, if the id-expression was a qualified-id, the caller will
4768 want to make a SCOPE_REF to represent the qualified-id. This
4769 function does not do this in order to avoid wastefully creating
4770 SCOPE_REFs when they are not required.
4772 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4773 `template' keyword.
4775 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4776 uninstantiated templates.
4778 If *TEMPLATE_P is non-NULL, it is set to true iff the
4779 `template' keyword is used to explicitly indicate that the entity
4780 named is a template.
4782 If DECLARATOR_P is true, the id-expression is appearing as part of
4783 a declarator, rather than as part of an expression. */
4785 static tree
4786 cp_parser_id_expression (cp_parser *parser,
4787 bool template_keyword_p,
4788 bool check_dependency_p,
4789 bool *template_p,
4790 bool declarator_p,
4791 bool optional_p)
4793 bool global_scope_p;
4794 bool nested_name_specifier_p;
4796 /* Assume the `template' keyword was not used. */
4797 if (template_p)
4798 *template_p = template_keyword_p;
4800 /* Look for the optional `::' operator. */
4801 global_scope_p
4802 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4803 != NULL_TREE);
4804 /* Look for the optional nested-name-specifier. */
4805 nested_name_specifier_p
4806 = (cp_parser_nested_name_specifier_opt (parser,
4807 /*typename_keyword_p=*/false,
4808 check_dependency_p,
4809 /*type_p=*/false,
4810 declarator_p)
4811 != NULL_TREE);
4812 /* If there is a nested-name-specifier, then we are looking at
4813 the first qualified-id production. */
4814 if (nested_name_specifier_p)
4816 tree saved_scope;
4817 tree saved_object_scope;
4818 tree saved_qualifying_scope;
4819 tree unqualified_id;
4820 bool is_template;
4822 /* See if the next token is the `template' keyword. */
4823 if (!template_p)
4824 template_p = &is_template;
4825 *template_p = cp_parser_optional_template_keyword (parser);
4826 /* Name lookup we do during the processing of the
4827 unqualified-id might obliterate SCOPE. */
4828 saved_scope = parser->scope;
4829 saved_object_scope = parser->object_scope;
4830 saved_qualifying_scope = parser->qualifying_scope;
4831 /* Process the final unqualified-id. */
4832 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4833 check_dependency_p,
4834 declarator_p,
4835 /*optional_p=*/false);
4836 /* Restore the SAVED_SCOPE for our caller. */
4837 parser->scope = saved_scope;
4838 parser->object_scope = saved_object_scope;
4839 parser->qualifying_scope = saved_qualifying_scope;
4841 return unqualified_id;
4843 /* Otherwise, if we are in global scope, then we are looking at one
4844 of the other qualified-id productions. */
4845 else if (global_scope_p)
4847 cp_token *token;
4848 tree id;
4850 /* Peek at the next token. */
4851 token = cp_lexer_peek_token (parser->lexer);
4853 /* If it's an identifier, and the next token is not a "<", then
4854 we can avoid the template-id case. This is an optimization
4855 for this common case. */
4856 if (token->type == CPP_NAME
4857 && !cp_parser_nth_token_starts_template_argument_list_p
4858 (parser, 2))
4859 return cp_parser_identifier (parser);
4861 cp_parser_parse_tentatively (parser);
4862 /* Try a template-id. */
4863 id = cp_parser_template_id (parser,
4864 /*template_keyword_p=*/false,
4865 /*check_dependency_p=*/true,
4866 none_type,
4867 declarator_p);
4868 /* If that worked, we're done. */
4869 if (cp_parser_parse_definitely (parser))
4870 return id;
4872 /* Peek at the next token. (Changes in the token buffer may
4873 have invalidated the pointer obtained above.) */
4874 token = cp_lexer_peek_token (parser->lexer);
4876 switch (token->type)
4878 case CPP_NAME:
4879 return cp_parser_identifier (parser);
4881 case CPP_KEYWORD:
4882 if (token->keyword == RID_OPERATOR)
4883 return cp_parser_operator_function_id (parser);
4884 /* Fall through. */
4886 default:
4887 cp_parser_error (parser, "expected id-expression");
4888 return error_mark_node;
4891 else
4892 return cp_parser_unqualified_id (parser, template_keyword_p,
4893 /*check_dependency_p=*/true,
4894 declarator_p,
4895 optional_p);
4898 /* Parse an unqualified-id.
4900 unqualified-id:
4901 identifier
4902 operator-function-id
4903 conversion-function-id
4904 ~ class-name
4905 template-id
4907 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4908 keyword, in a construct like `A::template ...'.
4910 Returns a representation of unqualified-id. For the `identifier'
4911 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4912 production a BIT_NOT_EXPR is returned; the operand of the
4913 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4914 other productions, see the documentation accompanying the
4915 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4916 names are looked up in uninstantiated templates. If DECLARATOR_P
4917 is true, the unqualified-id is appearing as part of a declarator,
4918 rather than as part of an expression. */
4920 static tree
4921 cp_parser_unqualified_id (cp_parser* parser,
4922 bool template_keyword_p,
4923 bool check_dependency_p,
4924 bool declarator_p,
4925 bool optional_p)
4927 cp_token *token;
4928 tree id;
4930 /* Peek at the next token. */
4931 token = cp_lexer_peek_token (parser->lexer);
4933 switch ((int) token->type)
4935 case CPP_NAME:
4937 /* We don't know yet whether or not this will be a
4938 template-id. */
4939 cp_parser_parse_tentatively (parser);
4940 /* Try a template-id. */
4941 id = cp_parser_template_id (parser, template_keyword_p,
4942 check_dependency_p,
4943 none_type,
4944 declarator_p);
4945 /* If it worked, we're done. */
4946 if (cp_parser_parse_definitely (parser))
4947 return id;
4948 /* Otherwise, it's an ordinary identifier. */
4949 return cp_parser_identifier (parser);
4952 case CPP_TEMPLATE_ID:
4953 return cp_parser_template_id (parser, template_keyword_p,
4954 check_dependency_p,
4955 none_type,
4956 declarator_p);
4958 case CPP_COMPL:
4960 tree type_decl;
4961 tree qualifying_scope;
4962 tree object_scope;
4963 tree scope;
4964 bool done;
4966 /* Consume the `~' token. */
4967 cp_lexer_consume_token (parser->lexer);
4968 /* Parse the class-name. The standard, as written, seems to
4969 say that:
4971 template <typename T> struct S { ~S (); };
4972 template <typename T> S<T>::~S() {}
4974 is invalid, since `~' must be followed by a class-name, but
4975 `S<T>' is dependent, and so not known to be a class.
4976 That's not right; we need to look in uninstantiated
4977 templates. A further complication arises from:
4979 template <typename T> void f(T t) {
4980 t.T::~T();
4983 Here, it is not possible to look up `T' in the scope of `T'
4984 itself. We must look in both the current scope, and the
4985 scope of the containing complete expression.
4987 Yet another issue is:
4989 struct S {
4990 int S;
4991 ~S();
4994 S::~S() {}
4996 The standard does not seem to say that the `S' in `~S'
4997 should refer to the type `S' and not the data member
4998 `S::S'. */
5000 /* DR 244 says that we look up the name after the "~" in the
5001 same scope as we looked up the qualifying name. That idea
5002 isn't fully worked out; it's more complicated than that. */
5003 scope = parser->scope;
5004 object_scope = parser->object_scope;
5005 qualifying_scope = parser->qualifying_scope;
5007 /* Check for invalid scopes. */
5008 if (scope == error_mark_node)
5010 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5011 cp_lexer_consume_token (parser->lexer);
5012 return error_mark_node;
5014 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5016 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5017 error_at (token->location,
5018 "scope %qT before %<~%> is not a class-name",
5019 scope);
5020 cp_parser_simulate_error (parser);
5021 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5022 cp_lexer_consume_token (parser->lexer);
5023 return error_mark_node;
5025 gcc_assert (!scope || TYPE_P (scope));
5027 /* If the name is of the form "X::~X" it's OK even if X is a
5028 typedef. */
5029 token = cp_lexer_peek_token (parser->lexer);
5030 if (scope
5031 && token->type == CPP_NAME
5032 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5033 != CPP_LESS)
5034 && (token->u.value == TYPE_IDENTIFIER (scope)
5035 || (CLASS_TYPE_P (scope)
5036 && constructor_name_p (token->u.value, scope))))
5038 cp_lexer_consume_token (parser->lexer);
5039 return build_nt (BIT_NOT_EXPR, scope);
5042 /* ~auto means the destructor of whatever the object is. */
5043 if (cp_parser_is_keyword (token, RID_AUTO))
5045 if (cxx_dialect < cxx14)
5046 pedwarn (input_location, 0,
5047 "%<~auto%> only available with "
5048 "-std=c++14 or -std=gnu++14");
5049 cp_lexer_consume_token (parser->lexer);
5050 return build_nt (BIT_NOT_EXPR, make_auto ());
5053 /* If there was an explicit qualification (S::~T), first look
5054 in the scope given by the qualification (i.e., S).
5056 Note: in the calls to cp_parser_class_name below we pass
5057 typename_type so that lookup finds the injected-class-name
5058 rather than the constructor. */
5059 done = false;
5060 type_decl = NULL_TREE;
5061 if (scope)
5063 cp_parser_parse_tentatively (parser);
5064 type_decl = cp_parser_class_name (parser,
5065 /*typename_keyword_p=*/false,
5066 /*template_keyword_p=*/false,
5067 typename_type,
5068 /*check_dependency=*/false,
5069 /*class_head_p=*/false,
5070 declarator_p);
5071 if (cp_parser_parse_definitely (parser))
5072 done = true;
5074 /* In "N::S::~S", look in "N" as well. */
5075 if (!done && scope && qualifying_scope)
5077 cp_parser_parse_tentatively (parser);
5078 parser->scope = qualifying_scope;
5079 parser->object_scope = NULL_TREE;
5080 parser->qualifying_scope = NULL_TREE;
5081 type_decl
5082 = cp_parser_class_name (parser,
5083 /*typename_keyword_p=*/false,
5084 /*template_keyword_p=*/false,
5085 typename_type,
5086 /*check_dependency=*/false,
5087 /*class_head_p=*/false,
5088 declarator_p);
5089 if (cp_parser_parse_definitely (parser))
5090 done = true;
5092 /* In "p->S::~T", look in the scope given by "*p" as well. */
5093 else if (!done && object_scope)
5095 cp_parser_parse_tentatively (parser);
5096 parser->scope = object_scope;
5097 parser->object_scope = NULL_TREE;
5098 parser->qualifying_scope = NULL_TREE;
5099 type_decl
5100 = cp_parser_class_name (parser,
5101 /*typename_keyword_p=*/false,
5102 /*template_keyword_p=*/false,
5103 typename_type,
5104 /*check_dependency=*/false,
5105 /*class_head_p=*/false,
5106 declarator_p);
5107 if (cp_parser_parse_definitely (parser))
5108 done = true;
5110 /* Look in the surrounding context. */
5111 if (!done)
5113 parser->scope = NULL_TREE;
5114 parser->object_scope = NULL_TREE;
5115 parser->qualifying_scope = NULL_TREE;
5116 if (processing_template_decl)
5117 cp_parser_parse_tentatively (parser);
5118 type_decl
5119 = cp_parser_class_name (parser,
5120 /*typename_keyword_p=*/false,
5121 /*template_keyword_p=*/false,
5122 typename_type,
5123 /*check_dependency=*/false,
5124 /*class_head_p=*/false,
5125 declarator_p);
5126 if (processing_template_decl
5127 && ! cp_parser_parse_definitely (parser))
5129 /* We couldn't find a type with this name, so just accept
5130 it and check for a match at instantiation time. */
5131 type_decl = cp_parser_identifier (parser);
5132 if (type_decl != error_mark_node)
5133 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5134 return type_decl;
5137 /* If an error occurred, assume that the name of the
5138 destructor is the same as the name of the qualifying
5139 class. That allows us to keep parsing after running
5140 into ill-formed destructor names. */
5141 if (type_decl == error_mark_node && scope)
5142 return build_nt (BIT_NOT_EXPR, scope);
5143 else if (type_decl == error_mark_node)
5144 return error_mark_node;
5146 /* Check that destructor name and scope match. */
5147 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5149 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5150 error_at (token->location,
5151 "declaration of %<~%T%> as member of %qT",
5152 type_decl, scope);
5153 cp_parser_simulate_error (parser);
5154 return error_mark_node;
5157 /* [class.dtor]
5159 A typedef-name that names a class shall not be used as the
5160 identifier in the declarator for a destructor declaration. */
5161 if (declarator_p
5162 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5163 && !DECL_SELF_REFERENCE_P (type_decl)
5164 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5165 error_at (token->location,
5166 "typedef-name %qD used as destructor declarator",
5167 type_decl);
5169 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5172 case CPP_KEYWORD:
5173 switch (token->keyword)
5175 case RID_OPERATOR:
5176 /* This could be a template-id, so we try that first. */
5177 cp_parser_parse_tentatively (parser);
5178 /* Try a template-id. */
5179 id = cp_parser_template_id (parser, template_keyword_p,
5180 /*check_dependency_p=*/true,
5181 none_type,
5182 declarator_p);
5183 /* If that worked, we're done. */
5184 if (cp_parser_parse_definitely (parser))
5185 return id;
5186 /* We still don't know whether we're looking at an
5187 operator-function-id or a conversion-function-id. */
5188 cp_parser_parse_tentatively (parser);
5189 /* Try an operator-function-id. */
5190 id = cp_parser_operator_function_id (parser);
5191 /* If that didn't work, try a conversion-function-id. */
5192 if (!cp_parser_parse_definitely (parser))
5193 id = cp_parser_conversion_function_id (parser);
5194 else if (UDLIT_OPER_P (id))
5196 /* 17.6.3.3.5 */
5197 const char *name = UDLIT_OP_SUFFIX (id);
5198 if (name[0] != '_' && !in_system_header_at (input_location)
5199 && declarator_p)
5200 warning (0, "literal operator suffixes not preceded by %<_%>"
5201 " are reserved for future standardization");
5204 return id;
5206 case RID_FUNCTION_NAME:
5207 case RID_PRETTY_FUNCTION_NAME:
5208 case RID_C99_FUNCTION_NAME:
5209 cp_lexer_consume_token (parser->lexer);
5210 finish_fname (token->u.value);
5211 return token->u.value;
5213 default:
5214 break;
5216 /* Fall through. */
5218 default:
5219 if (optional_p)
5220 return NULL_TREE;
5221 cp_parser_error (parser, "expected unqualified-id");
5222 return error_mark_node;
5226 /* Parse an (optional) nested-name-specifier.
5228 nested-name-specifier: [C++98]
5229 class-or-namespace-name :: nested-name-specifier [opt]
5230 class-or-namespace-name :: template nested-name-specifier [opt]
5232 nested-name-specifier: [C++0x]
5233 type-name ::
5234 namespace-name ::
5235 nested-name-specifier identifier ::
5236 nested-name-specifier template [opt] simple-template-id ::
5238 PARSER->SCOPE should be set appropriately before this function is
5239 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5240 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5241 in name lookups.
5243 Sets PARSER->SCOPE to the class (TYPE) or namespace
5244 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5245 it unchanged if there is no nested-name-specifier. Returns the new
5246 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5248 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5249 part of a declaration and/or decl-specifier. */
5251 static tree
5252 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5253 bool typename_keyword_p,
5254 bool check_dependency_p,
5255 bool type_p,
5256 bool is_declaration)
5258 bool success = false;
5259 cp_token_position start = 0;
5260 cp_token *token;
5262 /* Remember where the nested-name-specifier starts. */
5263 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5265 start = cp_lexer_token_position (parser->lexer, false);
5266 push_deferring_access_checks (dk_deferred);
5269 while (true)
5271 tree new_scope;
5272 tree old_scope;
5273 tree saved_qualifying_scope;
5274 bool template_keyword_p;
5276 /* Spot cases that cannot be the beginning of a
5277 nested-name-specifier. */
5278 token = cp_lexer_peek_token (parser->lexer);
5280 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5281 the already parsed nested-name-specifier. */
5282 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5284 /* Grab the nested-name-specifier and continue the loop. */
5285 cp_parser_pre_parsed_nested_name_specifier (parser);
5286 /* If we originally encountered this nested-name-specifier
5287 with IS_DECLARATION set to false, we will not have
5288 resolved TYPENAME_TYPEs, so we must do so here. */
5289 if (is_declaration
5290 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5292 new_scope = resolve_typename_type (parser->scope,
5293 /*only_current_p=*/false);
5294 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5295 parser->scope = new_scope;
5297 success = true;
5298 continue;
5301 /* Spot cases that cannot be the beginning of a
5302 nested-name-specifier. On the second and subsequent times
5303 through the loop, we look for the `template' keyword. */
5304 if (success && token->keyword == RID_TEMPLATE)
5306 /* A template-id can start a nested-name-specifier. */
5307 else if (token->type == CPP_TEMPLATE_ID)
5309 /* DR 743: decltype can be used in a nested-name-specifier. */
5310 else if (token_is_decltype (token))
5312 else
5314 /* If the next token is not an identifier, then it is
5315 definitely not a type-name or namespace-name. */
5316 if (token->type != CPP_NAME)
5317 break;
5318 /* If the following token is neither a `<' (to begin a
5319 template-id), nor a `::', then we are not looking at a
5320 nested-name-specifier. */
5321 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5323 if (token->type == CPP_COLON
5324 && parser->colon_corrects_to_scope_p
5325 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5327 error_at (token->location,
5328 "found %<:%> in nested-name-specifier, expected %<::%>");
5329 token->type = CPP_SCOPE;
5332 if (token->type != CPP_SCOPE
5333 && !cp_parser_nth_token_starts_template_argument_list_p
5334 (parser, 2))
5335 break;
5338 /* The nested-name-specifier is optional, so we parse
5339 tentatively. */
5340 cp_parser_parse_tentatively (parser);
5342 /* Look for the optional `template' keyword, if this isn't the
5343 first time through the loop. */
5344 if (success)
5345 template_keyword_p = cp_parser_optional_template_keyword (parser);
5346 else
5347 template_keyword_p = false;
5349 /* Save the old scope since the name lookup we are about to do
5350 might destroy it. */
5351 old_scope = parser->scope;
5352 saved_qualifying_scope = parser->qualifying_scope;
5353 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5354 look up names in "X<T>::I" in order to determine that "Y" is
5355 a template. So, if we have a typename at this point, we make
5356 an effort to look through it. */
5357 if (is_declaration
5358 && !typename_keyword_p
5359 && parser->scope
5360 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5361 parser->scope = resolve_typename_type (parser->scope,
5362 /*only_current_p=*/false);
5363 /* Parse the qualifying entity. */
5364 new_scope
5365 = cp_parser_qualifying_entity (parser,
5366 typename_keyword_p,
5367 template_keyword_p,
5368 check_dependency_p,
5369 type_p,
5370 is_declaration);
5371 /* Look for the `::' token. */
5372 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5374 /* If we found what we wanted, we keep going; otherwise, we're
5375 done. */
5376 if (!cp_parser_parse_definitely (parser))
5378 bool error_p = false;
5380 /* Restore the OLD_SCOPE since it was valid before the
5381 failed attempt at finding the last
5382 class-or-namespace-name. */
5383 parser->scope = old_scope;
5384 parser->qualifying_scope = saved_qualifying_scope;
5386 /* If the next token is a decltype, and the one after that is a
5387 `::', then the decltype has failed to resolve to a class or
5388 enumeration type. Give this error even when parsing
5389 tentatively since it can't possibly be valid--and we're going
5390 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5391 won't get another chance.*/
5392 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5393 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5394 == CPP_SCOPE))
5396 token = cp_lexer_consume_token (parser->lexer);
5397 error_at (token->location, "decltype evaluates to %qT, "
5398 "which is not a class or enumeration type",
5399 token->u.value);
5400 parser->scope = error_mark_node;
5401 error_p = true;
5402 /* As below. */
5403 success = true;
5404 cp_lexer_consume_token (parser->lexer);
5407 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5408 break;
5409 /* If the next token is an identifier, and the one after
5410 that is a `::', then any valid interpretation would have
5411 found a class-or-namespace-name. */
5412 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5413 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5414 == CPP_SCOPE)
5415 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5416 != CPP_COMPL))
5418 token = cp_lexer_consume_token (parser->lexer);
5419 if (!error_p)
5421 if (!token->error_reported)
5423 tree decl;
5424 tree ambiguous_decls;
5426 decl = cp_parser_lookup_name (parser, token->u.value,
5427 none_type,
5428 /*is_template=*/false,
5429 /*is_namespace=*/false,
5430 /*check_dependency=*/true,
5431 &ambiguous_decls,
5432 token->location);
5433 if (TREE_CODE (decl) == TEMPLATE_DECL)
5434 error_at (token->location,
5435 "%qD used without template parameters",
5436 decl);
5437 else if (ambiguous_decls)
5439 // cp_parser_lookup_name has the same diagnostic,
5440 // thus make sure to emit it at most once.
5441 if (cp_parser_uncommitted_to_tentative_parse_p
5442 (parser))
5444 error_at (token->location,
5445 "reference to %qD is ambiguous",
5446 token->u.value);
5447 print_candidates (ambiguous_decls);
5449 decl = error_mark_node;
5451 else
5453 if (cxx_dialect != cxx98)
5454 cp_parser_name_lookup_error
5455 (parser, token->u.value, decl, NLE_NOT_CXX98,
5456 token->location);
5457 else
5458 cp_parser_name_lookup_error
5459 (parser, token->u.value, decl, NLE_CXX98,
5460 token->location);
5463 parser->scope = error_mark_node;
5464 error_p = true;
5465 /* Treat this as a successful nested-name-specifier
5466 due to:
5468 [basic.lookup.qual]
5470 If the name found is not a class-name (clause
5471 _class_) or namespace-name (_namespace.def_), the
5472 program is ill-formed. */
5473 success = true;
5475 cp_lexer_consume_token (parser->lexer);
5477 break;
5479 /* We've found one valid nested-name-specifier. */
5480 success = true;
5481 /* Name lookup always gives us a DECL. */
5482 if (TREE_CODE (new_scope) == TYPE_DECL)
5483 new_scope = TREE_TYPE (new_scope);
5484 /* Uses of "template" must be followed by actual templates. */
5485 if (template_keyword_p
5486 && !(CLASS_TYPE_P (new_scope)
5487 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5488 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5489 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5490 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5491 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5492 == TEMPLATE_ID_EXPR)))
5493 permerror (input_location, TYPE_P (new_scope)
5494 ? G_("%qT is not a template")
5495 : G_("%qD is not a template"),
5496 new_scope);
5497 /* If it is a class scope, try to complete it; we are about to
5498 be looking up names inside the class. */
5499 if (TYPE_P (new_scope)
5500 /* Since checking types for dependency can be expensive,
5501 avoid doing it if the type is already complete. */
5502 && !COMPLETE_TYPE_P (new_scope)
5503 /* Do not try to complete dependent types. */
5504 && !dependent_type_p (new_scope))
5506 new_scope = complete_type (new_scope);
5507 /* If it is a typedef to current class, use the current
5508 class instead, as the typedef won't have any names inside
5509 it yet. */
5510 if (!COMPLETE_TYPE_P (new_scope)
5511 && currently_open_class (new_scope))
5512 new_scope = TYPE_MAIN_VARIANT (new_scope);
5514 /* Make sure we look in the right scope the next time through
5515 the loop. */
5516 parser->scope = new_scope;
5519 /* If parsing tentatively, replace the sequence of tokens that makes
5520 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5521 token. That way, should we re-parse the token stream, we will
5522 not have to repeat the effort required to do the parse, nor will
5523 we issue duplicate error messages. */
5524 if (success && start)
5526 cp_token *token;
5528 token = cp_lexer_token_at (parser->lexer, start);
5529 /* Reset the contents of the START token. */
5530 token->type = CPP_NESTED_NAME_SPECIFIER;
5531 /* Retrieve any deferred checks. Do not pop this access checks yet
5532 so the memory will not be reclaimed during token replacing below. */
5533 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5534 token->u.tree_check_value->value = parser->scope;
5535 token->u.tree_check_value->checks = get_deferred_access_checks ();
5536 token->u.tree_check_value->qualifying_scope =
5537 parser->qualifying_scope;
5538 token->keyword = RID_MAX;
5540 /* Purge all subsequent tokens. */
5541 cp_lexer_purge_tokens_after (parser->lexer, start);
5544 if (start)
5545 pop_to_parent_deferring_access_checks ();
5547 return success ? parser->scope : NULL_TREE;
5550 /* Parse a nested-name-specifier. See
5551 cp_parser_nested_name_specifier_opt for details. This function
5552 behaves identically, except that it will an issue an error if no
5553 nested-name-specifier is present. */
5555 static tree
5556 cp_parser_nested_name_specifier (cp_parser *parser,
5557 bool typename_keyword_p,
5558 bool check_dependency_p,
5559 bool type_p,
5560 bool is_declaration)
5562 tree scope;
5564 /* Look for the nested-name-specifier. */
5565 scope = cp_parser_nested_name_specifier_opt (parser,
5566 typename_keyword_p,
5567 check_dependency_p,
5568 type_p,
5569 is_declaration);
5570 /* If it was not present, issue an error message. */
5571 if (!scope)
5573 cp_parser_error (parser, "expected nested-name-specifier");
5574 parser->scope = NULL_TREE;
5577 return scope;
5580 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5581 this is either a class-name or a namespace-name (which corresponds
5582 to the class-or-namespace-name production in the grammar). For
5583 C++0x, it can also be a type-name that refers to an enumeration
5584 type or a simple-template-id.
5586 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5587 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5588 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5589 TYPE_P is TRUE iff the next name should be taken as a class-name,
5590 even the same name is declared to be another entity in the same
5591 scope.
5593 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5594 specified by the class-or-namespace-name. If neither is found the
5595 ERROR_MARK_NODE is returned. */
5597 static tree
5598 cp_parser_qualifying_entity (cp_parser *parser,
5599 bool typename_keyword_p,
5600 bool template_keyword_p,
5601 bool check_dependency_p,
5602 bool type_p,
5603 bool is_declaration)
5605 tree saved_scope;
5606 tree saved_qualifying_scope;
5607 tree saved_object_scope;
5608 tree scope;
5609 bool only_class_p;
5610 bool successful_parse_p;
5612 /* DR 743: decltype can appear in a nested-name-specifier. */
5613 if (cp_lexer_next_token_is_decltype (parser->lexer))
5615 scope = cp_parser_decltype (parser);
5616 if (TREE_CODE (scope) != ENUMERAL_TYPE
5617 && !MAYBE_CLASS_TYPE_P (scope))
5619 cp_parser_simulate_error (parser);
5620 return error_mark_node;
5622 if (TYPE_NAME (scope))
5623 scope = TYPE_NAME (scope);
5624 return scope;
5627 /* Before we try to parse the class-name, we must save away the
5628 current PARSER->SCOPE since cp_parser_class_name will destroy
5629 it. */
5630 saved_scope = parser->scope;
5631 saved_qualifying_scope = parser->qualifying_scope;
5632 saved_object_scope = parser->object_scope;
5633 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5634 there is no need to look for a namespace-name. */
5635 only_class_p = template_keyword_p
5636 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5637 if (!only_class_p)
5638 cp_parser_parse_tentatively (parser);
5639 scope = cp_parser_class_name (parser,
5640 typename_keyword_p,
5641 template_keyword_p,
5642 type_p ? class_type : none_type,
5643 check_dependency_p,
5644 /*class_head_p=*/false,
5645 is_declaration);
5646 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5647 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5648 if (!only_class_p
5649 && cxx_dialect != cxx98
5650 && !successful_parse_p)
5652 /* Restore the saved scope. */
5653 parser->scope = saved_scope;
5654 parser->qualifying_scope = saved_qualifying_scope;
5655 parser->object_scope = saved_object_scope;
5657 /* Parse tentatively. */
5658 cp_parser_parse_tentatively (parser);
5660 /* Parse a type-name */
5661 scope = cp_parser_type_name (parser);
5663 /* "If the name found does not designate a namespace or a class,
5664 enumeration, or dependent type, the program is ill-formed."
5666 We cover classes and dependent types above and namespaces below,
5667 so this code is only looking for enums. */
5668 if (!scope || TREE_CODE (scope) != TYPE_DECL
5669 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5670 cp_parser_simulate_error (parser);
5672 successful_parse_p = cp_parser_parse_definitely (parser);
5674 /* If that didn't work, try for a namespace-name. */
5675 if (!only_class_p && !successful_parse_p)
5677 /* Restore the saved scope. */
5678 parser->scope = saved_scope;
5679 parser->qualifying_scope = saved_qualifying_scope;
5680 parser->object_scope = saved_object_scope;
5681 /* If we are not looking at an identifier followed by the scope
5682 resolution operator, then this is not part of a
5683 nested-name-specifier. (Note that this function is only used
5684 to parse the components of a nested-name-specifier.) */
5685 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5686 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5687 return error_mark_node;
5688 scope = cp_parser_namespace_name (parser);
5691 return scope;
5694 /* Return true if we are looking at a compound-literal, false otherwise. */
5696 static bool
5697 cp_parser_compound_literal_p (cp_parser *parser)
5699 /* Consume the `('. */
5700 cp_lexer_consume_token (parser->lexer);
5702 cp_lexer_save_tokens (parser->lexer);
5704 /* Skip tokens until the next token is a closing parenthesis.
5705 If we find the closing `)', and the next token is a `{', then
5706 we are looking at a compound-literal. */
5707 bool compound_literal_p
5708 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5709 /*consume_paren=*/true)
5710 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5712 /* Roll back the tokens we skipped. */
5713 cp_lexer_rollback_tokens (parser->lexer);
5715 return compound_literal_p;
5718 /* Parse a postfix-expression.
5720 postfix-expression:
5721 primary-expression
5722 postfix-expression [ expression ]
5723 postfix-expression ( expression-list [opt] )
5724 simple-type-specifier ( expression-list [opt] )
5725 typename :: [opt] nested-name-specifier identifier
5726 ( expression-list [opt] )
5727 typename :: [opt] nested-name-specifier template [opt] template-id
5728 ( expression-list [opt] )
5729 postfix-expression . template [opt] id-expression
5730 postfix-expression -> template [opt] id-expression
5731 postfix-expression . pseudo-destructor-name
5732 postfix-expression -> pseudo-destructor-name
5733 postfix-expression ++
5734 postfix-expression --
5735 dynamic_cast < type-id > ( expression )
5736 static_cast < type-id > ( expression )
5737 reinterpret_cast < type-id > ( expression )
5738 const_cast < type-id > ( expression )
5739 typeid ( expression )
5740 typeid ( type-id )
5742 GNU Extension:
5744 postfix-expression:
5745 ( type-id ) { initializer-list , [opt] }
5747 This extension is a GNU version of the C99 compound-literal
5748 construct. (The C99 grammar uses `type-name' instead of `type-id',
5749 but they are essentially the same concept.)
5751 If ADDRESS_P is true, the postfix expression is the operand of the
5752 `&' operator. CAST_P is true if this expression is the target of a
5753 cast.
5755 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5756 class member access expressions [expr.ref].
5758 Returns a representation of the expression. */
5760 static tree
5761 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5762 bool member_access_only_p, bool decltype_p,
5763 cp_id_kind * pidk_return)
5765 cp_token *token;
5766 location_t loc;
5767 enum rid keyword;
5768 cp_id_kind idk = CP_ID_KIND_NONE;
5769 tree postfix_expression = NULL_TREE;
5770 bool is_member_access = false;
5771 int saved_in_statement = -1;
5773 /* Peek at the next token. */
5774 token = cp_lexer_peek_token (parser->lexer);
5775 loc = token->location;
5776 /* Some of the productions are determined by keywords. */
5777 keyword = token->keyword;
5778 switch (keyword)
5780 case RID_DYNCAST:
5781 case RID_STATCAST:
5782 case RID_REINTCAST:
5783 case RID_CONSTCAST:
5785 tree type;
5786 tree expression;
5787 const char *saved_message;
5788 bool saved_in_type_id_in_expr_p;
5790 /* All of these can be handled in the same way from the point
5791 of view of parsing. Begin by consuming the token
5792 identifying the cast. */
5793 cp_lexer_consume_token (parser->lexer);
5795 /* New types cannot be defined in the cast. */
5796 saved_message = parser->type_definition_forbidden_message;
5797 parser->type_definition_forbidden_message
5798 = G_("types may not be defined in casts");
5800 /* Look for the opening `<'. */
5801 cp_parser_require (parser, CPP_LESS, RT_LESS);
5802 /* Parse the type to which we are casting. */
5803 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5804 parser->in_type_id_in_expr_p = true;
5805 type = cp_parser_type_id (parser);
5806 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5807 /* Look for the closing `>'. */
5808 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5809 /* Restore the old message. */
5810 parser->type_definition_forbidden_message = saved_message;
5812 bool saved_greater_than_is_operator_p
5813 = parser->greater_than_is_operator_p;
5814 parser->greater_than_is_operator_p = true;
5816 /* And the expression which is being cast. */
5817 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5818 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5819 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5821 parser->greater_than_is_operator_p
5822 = saved_greater_than_is_operator_p;
5824 /* Only type conversions to integral or enumeration types
5825 can be used in constant-expressions. */
5826 if (!cast_valid_in_integral_constant_expression_p (type)
5827 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5828 return error_mark_node;
5830 switch (keyword)
5832 case RID_DYNCAST:
5833 postfix_expression
5834 = build_dynamic_cast (type, expression, tf_warning_or_error);
5835 break;
5836 case RID_STATCAST:
5837 postfix_expression
5838 = build_static_cast (type, expression, tf_warning_or_error);
5839 break;
5840 case RID_REINTCAST:
5841 postfix_expression
5842 = build_reinterpret_cast (type, expression,
5843 tf_warning_or_error);
5844 break;
5845 case RID_CONSTCAST:
5846 postfix_expression
5847 = build_const_cast (type, expression, tf_warning_or_error);
5848 break;
5849 default:
5850 gcc_unreachable ();
5853 break;
5855 case RID_TYPEID:
5857 tree type;
5858 const char *saved_message;
5859 bool saved_in_type_id_in_expr_p;
5861 /* Consume the `typeid' token. */
5862 cp_lexer_consume_token (parser->lexer);
5863 /* Look for the `(' token. */
5864 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5865 /* Types cannot be defined in a `typeid' expression. */
5866 saved_message = parser->type_definition_forbidden_message;
5867 parser->type_definition_forbidden_message
5868 = G_("types may not be defined in a %<typeid%> expression");
5869 /* We can't be sure yet whether we're looking at a type-id or an
5870 expression. */
5871 cp_parser_parse_tentatively (parser);
5872 /* Try a type-id first. */
5873 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5874 parser->in_type_id_in_expr_p = true;
5875 type = cp_parser_type_id (parser);
5876 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5877 /* Look for the `)' token. Otherwise, we can't be sure that
5878 we're not looking at an expression: consider `typeid (int
5879 (3))', for example. */
5880 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5881 /* If all went well, simply lookup the type-id. */
5882 if (cp_parser_parse_definitely (parser))
5883 postfix_expression = get_typeid (type, tf_warning_or_error);
5884 /* Otherwise, fall back to the expression variant. */
5885 else
5887 tree expression;
5889 /* Look for an expression. */
5890 expression = cp_parser_expression (parser, & idk);
5891 /* Compute its typeid. */
5892 postfix_expression = build_typeid (expression, tf_warning_or_error);
5893 /* Look for the `)' token. */
5894 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5896 /* Restore the saved message. */
5897 parser->type_definition_forbidden_message = saved_message;
5898 /* `typeid' may not appear in an integral constant expression. */
5899 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5900 return error_mark_node;
5902 break;
5904 case RID_TYPENAME:
5906 tree type;
5907 /* The syntax permitted here is the same permitted for an
5908 elaborated-type-specifier. */
5909 type = cp_parser_elaborated_type_specifier (parser,
5910 /*is_friend=*/false,
5911 /*is_declaration=*/false);
5912 postfix_expression = cp_parser_functional_cast (parser, type);
5914 break;
5916 case RID_CILK_SPAWN:
5918 cp_lexer_consume_token (parser->lexer);
5919 token = cp_lexer_peek_token (parser->lexer);
5920 if (token->type == CPP_SEMICOLON)
5922 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5923 "an expression");
5924 postfix_expression = error_mark_node;
5925 break;
5927 else if (!current_function_decl)
5929 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5930 "inside a function");
5931 postfix_expression = error_mark_node;
5932 break;
5934 else
5936 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5937 saved_in_statement = parser->in_statement;
5938 parser->in_statement |= IN_CILK_SPAWN;
5940 cfun->calls_cilk_spawn = 1;
5941 postfix_expression =
5942 cp_parser_postfix_expression (parser, false, false,
5943 false, false, &idk);
5944 if (!flag_cilkplus)
5946 error_at (token->location, "-fcilkplus must be enabled to use"
5947 " %<_Cilk_spawn%>");
5948 cfun->calls_cilk_spawn = 0;
5950 else if (saved_in_statement & IN_CILK_SPAWN)
5952 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5953 "are not permitted");
5954 postfix_expression = error_mark_node;
5955 cfun->calls_cilk_spawn = 0;
5957 else
5959 postfix_expression = build_cilk_spawn (token->location,
5960 postfix_expression);
5961 if (postfix_expression != error_mark_node)
5962 SET_EXPR_LOCATION (postfix_expression, input_location);
5963 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5965 break;
5968 case RID_BUILTIN_SHUFFLE:
5970 vec<tree, va_gc> *vec;
5971 unsigned int i;
5972 tree p;
5974 cp_lexer_consume_token (parser->lexer);
5975 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5976 /*cast_p=*/false, /*allow_expansion_p=*/true,
5977 /*non_constant_p=*/NULL);
5978 if (vec == NULL)
5979 return error_mark_node;
5981 FOR_EACH_VEC_ELT (*vec, i, p)
5982 mark_exp_read (p);
5984 if (vec->length () == 2)
5985 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5986 tf_warning_or_error);
5987 else if (vec->length () == 3)
5988 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5989 tf_warning_or_error);
5990 else
5992 error_at (loc, "wrong number of arguments to "
5993 "%<__builtin_shuffle%>");
5994 return error_mark_node;
5996 break;
5999 default:
6001 tree type;
6003 /* If the next thing is a simple-type-specifier, we may be
6004 looking at a functional cast. We could also be looking at
6005 an id-expression. So, we try the functional cast, and if
6006 that doesn't work we fall back to the primary-expression. */
6007 cp_parser_parse_tentatively (parser);
6008 /* Look for the simple-type-specifier. */
6009 type = cp_parser_simple_type_specifier (parser,
6010 /*decl_specs=*/NULL,
6011 CP_PARSER_FLAGS_NONE);
6012 /* Parse the cast itself. */
6013 if (!cp_parser_error_occurred (parser))
6014 postfix_expression
6015 = cp_parser_functional_cast (parser, type);
6016 /* If that worked, we're done. */
6017 if (cp_parser_parse_definitely (parser))
6018 break;
6020 /* If the functional-cast didn't work out, try a
6021 compound-literal. */
6022 if (cp_parser_allow_gnu_extensions_p (parser)
6023 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6025 tree initializer = NULL_TREE;
6027 cp_parser_parse_tentatively (parser);
6029 /* Avoid calling cp_parser_type_id pointlessly, see comment
6030 in cp_parser_cast_expression about c++/29234. */
6031 if (!cp_parser_compound_literal_p (parser))
6032 cp_parser_simulate_error (parser);
6033 else
6035 /* Parse the type. */
6036 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6037 parser->in_type_id_in_expr_p = true;
6038 type = cp_parser_type_id (parser);
6039 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6040 /* Look for the `)'. */
6041 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6044 /* If things aren't going well, there's no need to
6045 keep going. */
6046 if (!cp_parser_error_occurred (parser))
6048 bool non_constant_p;
6049 /* Parse the brace-enclosed initializer list. */
6050 initializer = cp_parser_braced_list (parser,
6051 &non_constant_p);
6053 /* If that worked, we're definitely looking at a
6054 compound-literal expression. */
6055 if (cp_parser_parse_definitely (parser))
6057 /* Warn the user that a compound literal is not
6058 allowed in standard C++. */
6059 pedwarn (input_location, OPT_Wpedantic,
6060 "ISO C++ forbids compound-literals");
6061 /* For simplicity, we disallow compound literals in
6062 constant-expressions. We could
6063 allow compound literals of integer type, whose
6064 initializer was a constant, in constant
6065 expressions. Permitting that usage, as a further
6066 extension, would not change the meaning of any
6067 currently accepted programs. (Of course, as
6068 compound literals are not part of ISO C++, the
6069 standard has nothing to say.) */
6070 if (cp_parser_non_integral_constant_expression (parser,
6071 NIC_NCC))
6073 postfix_expression = error_mark_node;
6074 break;
6076 /* Form the representation of the compound-literal. */
6077 postfix_expression
6078 = finish_compound_literal (type, initializer,
6079 tf_warning_or_error);
6080 break;
6084 /* It must be a primary-expression. */
6085 postfix_expression
6086 = cp_parser_primary_expression (parser, address_p, cast_p,
6087 /*template_arg_p=*/false,
6088 decltype_p,
6089 &idk);
6091 break;
6094 /* Note that we don't need to worry about calling build_cplus_new on a
6095 class-valued CALL_EXPR in decltype when it isn't the end of the
6096 postfix-expression; unary_complex_lvalue will take care of that for
6097 all these cases. */
6099 /* Keep looping until the postfix-expression is complete. */
6100 while (true)
6102 if (idk == CP_ID_KIND_UNQUALIFIED
6103 && identifier_p (postfix_expression)
6104 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6105 /* It is not a Koenig lookup function call. */
6106 postfix_expression
6107 = unqualified_name_lookup_error (postfix_expression);
6109 /* Peek at the next token. */
6110 token = cp_lexer_peek_token (parser->lexer);
6112 switch (token->type)
6114 case CPP_OPEN_SQUARE:
6115 if (cp_next_tokens_can_be_std_attribute_p (parser))
6117 cp_parser_error (parser,
6118 "two consecutive %<[%> shall "
6119 "only introduce an attribute");
6120 return error_mark_node;
6122 postfix_expression
6123 = cp_parser_postfix_open_square_expression (parser,
6124 postfix_expression,
6125 false,
6126 decltype_p);
6127 idk = CP_ID_KIND_NONE;
6128 is_member_access = false;
6129 break;
6131 case CPP_OPEN_PAREN:
6132 /* postfix-expression ( expression-list [opt] ) */
6134 bool koenig_p;
6135 bool is_builtin_constant_p;
6136 bool saved_integral_constant_expression_p = false;
6137 bool saved_non_integral_constant_expression_p = false;
6138 tsubst_flags_t complain = complain_flags (decltype_p);
6139 vec<tree, va_gc> *args;
6141 is_member_access = false;
6143 is_builtin_constant_p
6144 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6145 if (is_builtin_constant_p)
6147 /* The whole point of __builtin_constant_p is to allow
6148 non-constant expressions to appear as arguments. */
6149 saved_integral_constant_expression_p
6150 = parser->integral_constant_expression_p;
6151 saved_non_integral_constant_expression_p
6152 = parser->non_integral_constant_expression_p;
6153 parser->integral_constant_expression_p = false;
6155 args = (cp_parser_parenthesized_expression_list
6156 (parser, non_attr,
6157 /*cast_p=*/false, /*allow_expansion_p=*/true,
6158 /*non_constant_p=*/NULL,
6159 /*want_literal_zero_p=*/warn_memset_transposed_args));
6160 if (is_builtin_constant_p)
6162 parser->integral_constant_expression_p
6163 = saved_integral_constant_expression_p;
6164 parser->non_integral_constant_expression_p
6165 = saved_non_integral_constant_expression_p;
6168 if (args == NULL)
6170 postfix_expression = error_mark_node;
6171 break;
6174 /* Function calls are not permitted in
6175 constant-expressions. */
6176 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6177 && cp_parser_non_integral_constant_expression (parser,
6178 NIC_FUNC_CALL))
6180 postfix_expression = error_mark_node;
6181 release_tree_vector (args);
6182 break;
6185 koenig_p = false;
6186 if (idk == CP_ID_KIND_UNQUALIFIED
6187 || idk == CP_ID_KIND_TEMPLATE_ID)
6189 if (identifier_p (postfix_expression))
6191 if (!args->is_empty ())
6193 koenig_p = true;
6194 if (!any_type_dependent_arguments_p (args))
6195 postfix_expression
6196 = perform_koenig_lookup (postfix_expression, args,
6197 complain);
6199 else
6200 postfix_expression
6201 = unqualified_fn_lookup_error (postfix_expression);
6203 /* We do not perform argument-dependent lookup if
6204 normal lookup finds a non-function, in accordance
6205 with the expected resolution of DR 218. */
6206 else if (!args->is_empty ()
6207 && is_overloaded_fn (postfix_expression))
6209 tree fn = get_first_fn (postfix_expression);
6210 fn = STRIP_TEMPLATE (fn);
6212 /* Do not do argument dependent lookup if regular
6213 lookup finds a member function or a block-scope
6214 function declaration. [basic.lookup.argdep]/3 */
6215 if (!DECL_FUNCTION_MEMBER_P (fn)
6216 && !DECL_LOCAL_FUNCTION_P (fn))
6218 koenig_p = true;
6219 if (!any_type_dependent_arguments_p (args))
6220 postfix_expression
6221 = perform_koenig_lookup (postfix_expression, args,
6222 complain);
6227 if (warn_memset_transposed_args)
6229 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6230 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6231 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6232 && vec_safe_length (args) == 3
6233 && integer_zerop ((*args)[2])
6234 && LITERAL_ZERO_P ((*args)[2])
6235 && !(integer_zerop ((*args)[1])
6236 && LITERAL_ZERO_P ((*args)[1])))
6237 warning (OPT_Wmemset_transposed_args,
6238 "%<memset%> used with constant zero length "
6239 "parameter; this could be due to transposed "
6240 "parameters");
6242 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6243 to avoid leaking those into folder and middle-end. */
6244 unsigned int i;
6245 tree arg;
6246 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6247 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6248 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6251 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6253 tree instance = TREE_OPERAND (postfix_expression, 0);
6254 tree fn = TREE_OPERAND (postfix_expression, 1);
6256 if (processing_template_decl
6257 && (type_dependent_expression_p (instance)
6258 || (!BASELINK_P (fn)
6259 && TREE_CODE (fn) != FIELD_DECL)
6260 || type_dependent_expression_p (fn)
6261 || any_type_dependent_arguments_p (args)))
6263 postfix_expression
6264 = build_nt_call_vec (postfix_expression, args);
6265 release_tree_vector (args);
6266 break;
6269 if (BASELINK_P (fn))
6271 postfix_expression
6272 = (build_new_method_call
6273 (instance, fn, &args, NULL_TREE,
6274 (idk == CP_ID_KIND_QUALIFIED
6275 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6276 : LOOKUP_NORMAL),
6277 /*fn_p=*/NULL,
6278 complain));
6280 else
6281 postfix_expression
6282 = finish_call_expr (postfix_expression, &args,
6283 /*disallow_virtual=*/false,
6284 /*koenig_p=*/false,
6285 complain);
6287 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6288 || TREE_CODE (postfix_expression) == MEMBER_REF
6289 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6290 postfix_expression = (build_offset_ref_call_from_tree
6291 (postfix_expression, &args,
6292 complain));
6293 else if (idk == CP_ID_KIND_QUALIFIED)
6294 /* A call to a static class member, or a namespace-scope
6295 function. */
6296 postfix_expression
6297 = finish_call_expr (postfix_expression, &args,
6298 /*disallow_virtual=*/true,
6299 koenig_p,
6300 complain);
6301 else
6302 /* All other function calls. */
6303 postfix_expression
6304 = finish_call_expr (postfix_expression, &args,
6305 /*disallow_virtual=*/false,
6306 koenig_p,
6307 complain);
6309 protected_set_expr_location (postfix_expression, token->location);
6311 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6312 idk = CP_ID_KIND_NONE;
6314 release_tree_vector (args);
6316 break;
6318 case CPP_DOT:
6319 case CPP_DEREF:
6320 /* postfix-expression . template [opt] id-expression
6321 postfix-expression . pseudo-destructor-name
6322 postfix-expression -> template [opt] id-expression
6323 postfix-expression -> pseudo-destructor-name */
6325 /* Consume the `.' or `->' operator. */
6326 cp_lexer_consume_token (parser->lexer);
6328 postfix_expression
6329 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6330 postfix_expression,
6331 false, &idk, loc);
6333 is_member_access = true;
6334 break;
6336 case CPP_PLUS_PLUS:
6337 /* postfix-expression ++ */
6338 /* Consume the `++' token. */
6339 cp_lexer_consume_token (parser->lexer);
6340 /* Generate a representation for the complete expression. */
6341 postfix_expression
6342 = finish_increment_expr (postfix_expression,
6343 POSTINCREMENT_EXPR);
6344 /* Increments may not appear in constant-expressions. */
6345 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6346 postfix_expression = error_mark_node;
6347 idk = CP_ID_KIND_NONE;
6348 is_member_access = false;
6349 break;
6351 case CPP_MINUS_MINUS:
6352 /* postfix-expression -- */
6353 /* Consume the `--' token. */
6354 cp_lexer_consume_token (parser->lexer);
6355 /* Generate a representation for the complete expression. */
6356 postfix_expression
6357 = finish_increment_expr (postfix_expression,
6358 POSTDECREMENT_EXPR);
6359 /* Decrements may not appear in constant-expressions. */
6360 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6361 postfix_expression = error_mark_node;
6362 idk = CP_ID_KIND_NONE;
6363 is_member_access = false;
6364 break;
6366 default:
6367 if (pidk_return != NULL)
6368 * pidk_return = idk;
6369 if (member_access_only_p)
6370 return is_member_access? postfix_expression : error_mark_node;
6371 else
6372 return postfix_expression;
6376 /* We should never get here. */
6377 gcc_unreachable ();
6378 return error_mark_node;
6381 /* This function parses Cilk Plus array notations. If a normal array expr. is
6382 parsed then the array index is passed back to the caller through *INIT_INDEX
6383 and the function returns a NULL_TREE. If array notation expr. is parsed,
6384 then *INIT_INDEX is ignored by the caller and the function returns
6385 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6386 error_mark_node. */
6388 static tree
6389 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6390 tree array_value)
6392 cp_token *token = NULL;
6393 tree length_index, stride = NULL_TREE, value_tree, array_type;
6394 if (!array_value || array_value == error_mark_node)
6396 cp_parser_skip_to_end_of_statement (parser);
6397 return error_mark_node;
6400 array_type = TREE_TYPE (array_value);
6402 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6403 parser->colon_corrects_to_scope_p = false;
6404 token = cp_lexer_peek_token (parser->lexer);
6406 if (!token)
6408 cp_parser_error (parser, "expected %<:%> or numeral");
6409 return error_mark_node;
6411 else if (token->type == CPP_COLON)
6413 /* Consume the ':'. */
6414 cp_lexer_consume_token (parser->lexer);
6416 /* If we are here, then we have a case like this A[:]. */
6417 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6419 cp_parser_error (parser, "expected %<]%>");
6420 cp_parser_skip_to_end_of_statement (parser);
6421 return error_mark_node;
6423 *init_index = NULL_TREE;
6424 stride = NULL_TREE;
6425 length_index = NULL_TREE;
6427 else
6429 /* If we are here, then there are three valid possibilities:
6430 1. ARRAY [ EXP ]
6431 2. ARRAY [ EXP : EXP ]
6432 3. ARRAY [ EXP : EXP : EXP ] */
6434 *init_index = cp_parser_expression (parser);
6435 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6437 /* This indicates that we have a normal array expression. */
6438 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6439 return NULL_TREE;
6442 /* Consume the ':'. */
6443 cp_lexer_consume_token (parser->lexer);
6444 length_index = cp_parser_expression (parser);
6445 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6447 cp_lexer_consume_token (parser->lexer);
6448 stride = cp_parser_expression (parser);
6451 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6453 if (*init_index == error_mark_node || length_index == error_mark_node
6454 || stride == error_mark_node || array_type == error_mark_node)
6456 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6457 cp_lexer_consume_token (parser->lexer);
6458 return error_mark_node;
6460 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6462 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6463 length_index, stride, array_type);
6464 return value_tree;
6467 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6468 by cp_parser_builtin_offsetof. We're looking for
6470 postfix-expression [ expression ]
6471 postfix-expression [ braced-init-list ] (C++11)
6473 FOR_OFFSETOF is set if we're being called in that context, which
6474 changes how we deal with integer constant expressions. */
6476 static tree
6477 cp_parser_postfix_open_square_expression (cp_parser *parser,
6478 tree postfix_expression,
6479 bool for_offsetof,
6480 bool decltype_p)
6482 tree index = NULL_TREE;
6483 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6484 bool saved_greater_than_is_operator_p;
6486 /* Consume the `[' token. */
6487 cp_lexer_consume_token (parser->lexer);
6489 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6490 parser->greater_than_is_operator_p = true;
6492 /* Parse the index expression. */
6493 /* ??? For offsetof, there is a question of what to allow here. If
6494 offsetof is not being used in an integral constant expression context,
6495 then we *could* get the right answer by computing the value at runtime.
6496 If we are in an integral constant expression context, then we might
6497 could accept any constant expression; hard to say without analysis.
6498 Rather than open the barn door too wide right away, allow only integer
6499 constant expressions here. */
6500 if (for_offsetof)
6501 index = cp_parser_constant_expression (parser);
6502 else
6504 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6506 bool expr_nonconst_p;
6507 cp_lexer_set_source_position (parser->lexer);
6508 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6509 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6510 if (flag_cilkplus
6511 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6513 error_at (cp_lexer_peek_token (parser->lexer)->location,
6514 "braced list index is not allowed with array "
6515 "notation");
6516 cp_parser_skip_to_end_of_statement (parser);
6517 return error_mark_node;
6520 else if (flag_cilkplus)
6522 /* Here are have these two options:
6523 ARRAY[EXP : EXP] - Array notation expr with default
6524 stride of 1.
6525 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6526 stride. */
6527 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6528 postfix_expression);
6529 if (an_exp)
6530 return an_exp;
6532 else
6533 index = cp_parser_expression (parser);
6536 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6538 /* Look for the closing `]'. */
6539 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6541 /* Build the ARRAY_REF. */
6542 postfix_expression = grok_array_decl (loc, postfix_expression,
6543 index, decltype_p);
6545 /* When not doing offsetof, array references are not permitted in
6546 constant-expressions. */
6547 if (!for_offsetof
6548 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6549 postfix_expression = error_mark_node;
6551 return postfix_expression;
6554 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6555 by cp_parser_builtin_offsetof. We're looking for
6557 postfix-expression . template [opt] id-expression
6558 postfix-expression . pseudo-destructor-name
6559 postfix-expression -> template [opt] id-expression
6560 postfix-expression -> pseudo-destructor-name
6562 FOR_OFFSETOF is set if we're being called in that context. That sorta
6563 limits what of the above we'll actually accept, but nevermind.
6564 TOKEN_TYPE is the "." or "->" token, which will already have been
6565 removed from the stream. */
6567 static tree
6568 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6569 enum cpp_ttype token_type,
6570 tree postfix_expression,
6571 bool for_offsetof, cp_id_kind *idk,
6572 location_t location)
6574 tree name;
6575 bool dependent_p;
6576 bool pseudo_destructor_p;
6577 tree scope = NULL_TREE;
6579 /* If this is a `->' operator, dereference the pointer. */
6580 if (token_type == CPP_DEREF)
6581 postfix_expression = build_x_arrow (location, postfix_expression,
6582 tf_warning_or_error);
6583 /* Check to see whether or not the expression is type-dependent. */
6584 dependent_p = type_dependent_expression_p (postfix_expression);
6585 /* The identifier following the `->' or `.' is not qualified. */
6586 parser->scope = NULL_TREE;
6587 parser->qualifying_scope = NULL_TREE;
6588 parser->object_scope = NULL_TREE;
6589 *idk = CP_ID_KIND_NONE;
6591 /* Enter the scope corresponding to the type of the object
6592 given by the POSTFIX_EXPRESSION. */
6593 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6595 scope = TREE_TYPE (postfix_expression);
6596 /* According to the standard, no expression should ever have
6597 reference type. Unfortunately, we do not currently match
6598 the standard in this respect in that our internal representation
6599 of an expression may have reference type even when the standard
6600 says it does not. Therefore, we have to manually obtain the
6601 underlying type here. */
6602 scope = non_reference (scope);
6603 /* The type of the POSTFIX_EXPRESSION must be complete. */
6604 if (scope == unknown_type_node)
6606 error_at (location, "%qE does not have class type",
6607 postfix_expression);
6608 scope = NULL_TREE;
6610 /* Unlike the object expression in other contexts, *this is not
6611 required to be of complete type for purposes of class member
6612 access (5.2.5) outside the member function body. */
6613 else if (postfix_expression != current_class_ref
6614 && !(processing_template_decl && scope == current_class_type))
6615 scope = complete_type_or_else (scope, NULL_TREE);
6616 /* Let the name lookup machinery know that we are processing a
6617 class member access expression. */
6618 parser->context->object_type = scope;
6619 /* If something went wrong, we want to be able to discern that case,
6620 as opposed to the case where there was no SCOPE due to the type
6621 of expression being dependent. */
6622 if (!scope)
6623 scope = error_mark_node;
6624 /* If the SCOPE was erroneous, make the various semantic analysis
6625 functions exit quickly -- and without issuing additional error
6626 messages. */
6627 if (scope == error_mark_node)
6628 postfix_expression = error_mark_node;
6631 /* Assume this expression is not a pseudo-destructor access. */
6632 pseudo_destructor_p = false;
6634 /* If the SCOPE is a scalar type, then, if this is a valid program,
6635 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6636 is type dependent, it can be pseudo-destructor-name or something else.
6637 Try to parse it as pseudo-destructor-name first. */
6638 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6640 tree s;
6641 tree type;
6643 cp_parser_parse_tentatively (parser);
6644 /* Parse the pseudo-destructor-name. */
6645 s = NULL_TREE;
6646 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6647 &s, &type);
6648 if (dependent_p
6649 && (cp_parser_error_occurred (parser)
6650 || !SCALAR_TYPE_P (type)))
6651 cp_parser_abort_tentative_parse (parser);
6652 else if (cp_parser_parse_definitely (parser))
6654 pseudo_destructor_p = true;
6655 postfix_expression
6656 = finish_pseudo_destructor_expr (postfix_expression,
6657 s, type, location);
6661 if (!pseudo_destructor_p)
6663 /* If the SCOPE is not a scalar type, we are looking at an
6664 ordinary class member access expression, rather than a
6665 pseudo-destructor-name. */
6666 bool template_p;
6667 cp_token *token = cp_lexer_peek_token (parser->lexer);
6668 /* Parse the id-expression. */
6669 name = (cp_parser_id_expression
6670 (parser,
6671 cp_parser_optional_template_keyword (parser),
6672 /*check_dependency_p=*/true,
6673 &template_p,
6674 /*declarator_p=*/false,
6675 /*optional_p=*/false));
6676 /* In general, build a SCOPE_REF if the member name is qualified.
6677 However, if the name was not dependent and has already been
6678 resolved; there is no need to build the SCOPE_REF. For example;
6680 struct X { void f(); };
6681 template <typename T> void f(T* t) { t->X::f(); }
6683 Even though "t" is dependent, "X::f" is not and has been resolved
6684 to a BASELINK; there is no need to include scope information. */
6686 /* But we do need to remember that there was an explicit scope for
6687 virtual function calls. */
6688 if (parser->scope)
6689 *idk = CP_ID_KIND_QUALIFIED;
6691 /* If the name is a template-id that names a type, we will get a
6692 TYPE_DECL here. That is invalid code. */
6693 if (TREE_CODE (name) == TYPE_DECL)
6695 error_at (token->location, "invalid use of %qD", name);
6696 postfix_expression = error_mark_node;
6698 else
6700 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6702 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6704 error_at (token->location, "%<%D::%D%> is not a class member",
6705 parser->scope, name);
6706 postfix_expression = error_mark_node;
6708 else
6709 name = build_qualified_name (/*type=*/NULL_TREE,
6710 parser->scope,
6711 name,
6712 template_p);
6713 parser->scope = NULL_TREE;
6714 parser->qualifying_scope = NULL_TREE;
6715 parser->object_scope = NULL_TREE;
6717 if (parser->scope && name && BASELINK_P (name))
6718 adjust_result_of_qualified_name_lookup
6719 (name, parser->scope, scope);
6720 postfix_expression
6721 = finish_class_member_access_expr (postfix_expression, name,
6722 template_p,
6723 tf_warning_or_error);
6727 /* We no longer need to look up names in the scope of the object on
6728 the left-hand side of the `.' or `->' operator. */
6729 parser->context->object_type = NULL_TREE;
6731 /* Outside of offsetof, these operators may not appear in
6732 constant-expressions. */
6733 if (!for_offsetof
6734 && (cp_parser_non_integral_constant_expression
6735 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6736 postfix_expression = error_mark_node;
6738 return postfix_expression;
6741 /* Cache of LITERAL_ZERO_P constants. */
6743 static GTY(()) tree literal_zeros[itk_none];
6745 /* Parse a parenthesized expression-list.
6747 expression-list:
6748 assignment-expression
6749 expression-list, assignment-expression
6751 attribute-list:
6752 expression-list
6753 identifier
6754 identifier, expression-list
6756 CAST_P is true if this expression is the target of a cast.
6758 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6759 argument pack.
6761 Returns a vector of trees. Each element is a representation of an
6762 assignment-expression. NULL is returned if the ( and or ) are
6763 missing. An empty, but allocated, vector is returned on no
6764 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6765 if we are parsing an attribute list for an attribute that wants a
6766 plain identifier argument, normal_attr for an attribute that wants
6767 an expression, or non_attr if we aren't parsing an attribute list. If
6768 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6769 not all of the expressions in the list were constant.
6770 WANT_LITERAL_ZERO_P is true if the caller is interested in
6771 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6772 immediately, this can be removed. */
6774 static vec<tree, va_gc> *
6775 cp_parser_parenthesized_expression_list (cp_parser* parser,
6776 int is_attribute_list,
6777 bool cast_p,
6778 bool allow_expansion_p,
6779 bool *non_constant_p,
6780 bool want_literal_zero_p)
6782 vec<tree, va_gc> *expression_list;
6783 bool fold_expr_p = is_attribute_list != non_attr;
6784 tree identifier = NULL_TREE;
6785 bool saved_greater_than_is_operator_p;
6787 /* Assume all the expressions will be constant. */
6788 if (non_constant_p)
6789 *non_constant_p = false;
6791 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6792 return NULL;
6794 expression_list = make_tree_vector ();
6796 /* Within a parenthesized expression, a `>' token is always
6797 the greater-than operator. */
6798 saved_greater_than_is_operator_p
6799 = parser->greater_than_is_operator_p;
6800 parser->greater_than_is_operator_p = true;
6802 /* Consume expressions until there are no more. */
6803 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6804 while (true)
6806 tree expr;
6808 /* At the beginning of attribute lists, check to see if the
6809 next token is an identifier. */
6810 if (is_attribute_list == id_attr
6811 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6813 cp_token *token;
6815 /* Consume the identifier. */
6816 token = cp_lexer_consume_token (parser->lexer);
6817 /* Save the identifier. */
6818 identifier = token->u.value;
6820 else
6822 bool expr_non_constant_p;
6824 /* Parse the next assignment-expression. */
6825 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6827 /* A braced-init-list. */
6828 cp_lexer_set_source_position (parser->lexer);
6829 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6830 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6831 if (non_constant_p && expr_non_constant_p)
6832 *non_constant_p = true;
6834 else if (non_constant_p)
6836 expr = (cp_parser_constant_expression
6837 (parser, /*allow_non_constant_p=*/true,
6838 &expr_non_constant_p));
6839 if (expr_non_constant_p)
6840 *non_constant_p = true;
6842 else
6844 expr = NULL_TREE;
6845 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6846 switch (tok->type)
6848 case CPP_NUMBER:
6849 case CPP_CHAR:
6850 case CPP_WCHAR:
6851 case CPP_CHAR16:
6852 case CPP_CHAR32:
6853 /* If a parameter is literal zero alone, remember it
6854 for -Wmemset-transposed-args warning. */
6855 if (integer_zerop (tok->u.value)
6856 && !TREE_OVERFLOW (tok->u.value)
6857 && want_literal_zero_p
6858 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6859 == CPP_COMMA
6860 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6861 == CPP_CLOSE_PAREN))
6863 unsigned int i;
6864 for (i = 0; i < itk_none; ++i)
6865 if (TREE_TYPE (tok->u.value) == integer_types[i])
6866 break;
6867 if (i < itk_none && literal_zeros[i])
6868 expr = literal_zeros[i];
6869 else
6871 expr = copy_node (tok->u.value);
6872 LITERAL_ZERO_P (expr) = 1;
6873 if (i < itk_none)
6874 literal_zeros[i] = expr;
6876 /* Consume the 0 token (or '\0', 0LL etc.). */
6877 cp_lexer_consume_token (parser->lexer);
6879 break;
6880 default:
6881 break;
6883 if (expr == NULL_TREE)
6884 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6885 cast_p);
6888 if (fold_expr_p)
6889 expr = fold_non_dependent_expr (expr);
6891 /* If we have an ellipsis, then this is an expression
6892 expansion. */
6893 if (allow_expansion_p
6894 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6896 /* Consume the `...'. */
6897 cp_lexer_consume_token (parser->lexer);
6899 /* Build the argument pack. */
6900 expr = make_pack_expansion (expr);
6903 /* Add it to the list. We add error_mark_node
6904 expressions to the list, so that we can still tell if
6905 the correct form for a parenthesized expression-list
6906 is found. That gives better errors. */
6907 vec_safe_push (expression_list, expr);
6909 if (expr == error_mark_node)
6910 goto skip_comma;
6913 /* After the first item, attribute lists look the same as
6914 expression lists. */
6915 is_attribute_list = non_attr;
6917 get_comma:;
6918 /* If the next token isn't a `,', then we are done. */
6919 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6920 break;
6922 /* Otherwise, consume the `,' and keep going. */
6923 cp_lexer_consume_token (parser->lexer);
6926 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6928 int ending;
6930 skip_comma:;
6931 /* We try and resync to an unnested comma, as that will give the
6932 user better diagnostics. */
6933 ending = cp_parser_skip_to_closing_parenthesis (parser,
6934 /*recovering=*/true,
6935 /*or_comma=*/true,
6936 /*consume_paren=*/true);
6937 if (ending < 0)
6938 goto get_comma;
6939 if (!ending)
6941 parser->greater_than_is_operator_p
6942 = saved_greater_than_is_operator_p;
6943 return NULL;
6947 parser->greater_than_is_operator_p
6948 = saved_greater_than_is_operator_p;
6950 if (identifier)
6951 vec_safe_insert (expression_list, 0, identifier);
6953 return expression_list;
6956 /* Parse a pseudo-destructor-name.
6958 pseudo-destructor-name:
6959 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6960 :: [opt] nested-name-specifier template template-id :: ~ type-name
6961 :: [opt] nested-name-specifier [opt] ~ type-name
6963 If either of the first two productions is used, sets *SCOPE to the
6964 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6965 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6966 or ERROR_MARK_NODE if the parse fails. */
6968 static void
6969 cp_parser_pseudo_destructor_name (cp_parser* parser,
6970 tree object,
6971 tree* scope,
6972 tree* type)
6974 bool nested_name_specifier_p;
6976 /* Handle ~auto. */
6977 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6978 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6979 && !type_dependent_expression_p (object))
6981 if (cxx_dialect < cxx14)
6982 pedwarn (input_location, 0,
6983 "%<~auto%> only available with "
6984 "-std=c++14 or -std=gnu++14");
6985 cp_lexer_consume_token (parser->lexer);
6986 cp_lexer_consume_token (parser->lexer);
6987 *scope = NULL_TREE;
6988 *type = TREE_TYPE (object);
6989 return;
6992 /* Assume that things will not work out. */
6993 *type = error_mark_node;
6995 /* Look for the optional `::' operator. */
6996 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6997 /* Look for the optional nested-name-specifier. */
6998 nested_name_specifier_p
6999 = (cp_parser_nested_name_specifier_opt (parser,
7000 /*typename_keyword_p=*/false,
7001 /*check_dependency_p=*/true,
7002 /*type_p=*/false,
7003 /*is_declaration=*/false)
7004 != NULL_TREE);
7005 /* Now, if we saw a nested-name-specifier, we might be doing the
7006 second production. */
7007 if (nested_name_specifier_p
7008 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7010 /* Consume the `template' keyword. */
7011 cp_lexer_consume_token (parser->lexer);
7012 /* Parse the template-id. */
7013 cp_parser_template_id (parser,
7014 /*template_keyword_p=*/true,
7015 /*check_dependency_p=*/false,
7016 class_type,
7017 /*is_declaration=*/true);
7018 /* Look for the `::' token. */
7019 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7021 /* If the next token is not a `~', then there might be some
7022 additional qualification. */
7023 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7025 /* At this point, we're looking for "type-name :: ~". The type-name
7026 must not be a class-name, since this is a pseudo-destructor. So,
7027 it must be either an enum-name, or a typedef-name -- both of which
7028 are just identifiers. So, we peek ahead to check that the "::"
7029 and "~" tokens are present; if they are not, then we can avoid
7030 calling type_name. */
7031 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7032 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7033 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7035 cp_parser_error (parser, "non-scalar type");
7036 return;
7039 /* Look for the type-name. */
7040 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7041 if (*scope == error_mark_node)
7042 return;
7044 /* Look for the `::' token. */
7045 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7047 else
7048 *scope = NULL_TREE;
7050 /* Look for the `~'. */
7051 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7053 /* Once we see the ~, this has to be a pseudo-destructor. */
7054 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7055 cp_parser_commit_to_topmost_tentative_parse (parser);
7057 /* Look for the type-name again. We are not responsible for
7058 checking that it matches the first type-name. */
7059 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7062 /* Parse a unary-expression.
7064 unary-expression:
7065 postfix-expression
7066 ++ cast-expression
7067 -- cast-expression
7068 unary-operator cast-expression
7069 sizeof unary-expression
7070 sizeof ( type-id )
7071 alignof ( type-id ) [C++0x]
7072 new-expression
7073 delete-expression
7075 GNU Extensions:
7077 unary-expression:
7078 __extension__ cast-expression
7079 __alignof__ unary-expression
7080 __alignof__ ( type-id )
7081 alignof unary-expression [C++0x]
7082 __real__ cast-expression
7083 __imag__ cast-expression
7084 && identifier
7085 sizeof ( type-id ) { initializer-list , [opt] }
7086 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7087 __alignof__ ( type-id ) { initializer-list , [opt] }
7089 ADDRESS_P is true iff the unary-expression is appearing as the
7090 operand of the `&' operator. CAST_P is true if this expression is
7091 the target of a cast.
7093 Returns a representation of the expression. */
7095 static tree
7096 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7097 bool address_p, bool cast_p, bool decltype_p)
7099 cp_token *token;
7100 enum tree_code unary_operator;
7102 /* Peek at the next token. */
7103 token = cp_lexer_peek_token (parser->lexer);
7104 /* Some keywords give away the kind of expression. */
7105 if (token->type == CPP_KEYWORD)
7107 enum rid keyword = token->keyword;
7109 switch (keyword)
7111 case RID_ALIGNOF:
7112 case RID_SIZEOF:
7114 tree operand, ret;
7115 enum tree_code op;
7116 location_t first_loc;
7118 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7119 /* Consume the token. */
7120 cp_lexer_consume_token (parser->lexer);
7121 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7122 /* Parse the operand. */
7123 operand = cp_parser_sizeof_operand (parser, keyword);
7125 if (TYPE_P (operand))
7126 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7127 else
7129 /* ISO C++ defines alignof only with types, not with
7130 expressions. So pedwarn if alignof is used with a non-
7131 type expression. However, __alignof__ is ok. */
7132 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7133 pedwarn (token->location, OPT_Wpedantic,
7134 "ISO C++ does not allow %<alignof%> "
7135 "with a non-type");
7137 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7139 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7140 SIZEOF_EXPR with the original operand. */
7141 if (op == SIZEOF_EXPR && ret != error_mark_node)
7143 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7145 if (!processing_template_decl && TYPE_P (operand))
7147 ret = build_min (SIZEOF_EXPR, size_type_node,
7148 build1 (NOP_EXPR, operand,
7149 error_mark_node));
7150 SIZEOF_EXPR_TYPE_P (ret) = 1;
7152 else
7153 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7154 TREE_SIDE_EFFECTS (ret) = 0;
7155 TREE_READONLY (ret) = 1;
7157 SET_EXPR_LOCATION (ret, first_loc);
7159 return ret;
7162 case RID_NEW:
7163 return cp_parser_new_expression (parser);
7165 case RID_DELETE:
7166 return cp_parser_delete_expression (parser);
7168 case RID_EXTENSION:
7170 /* The saved value of the PEDANTIC flag. */
7171 int saved_pedantic;
7172 tree expr;
7174 /* Save away the PEDANTIC flag. */
7175 cp_parser_extension_opt (parser, &saved_pedantic);
7176 /* Parse the cast-expression. */
7177 expr = cp_parser_simple_cast_expression (parser);
7178 /* Restore the PEDANTIC flag. */
7179 pedantic = saved_pedantic;
7181 return expr;
7184 case RID_REALPART:
7185 case RID_IMAGPART:
7187 tree expression;
7189 /* Consume the `__real__' or `__imag__' token. */
7190 cp_lexer_consume_token (parser->lexer);
7191 /* Parse the cast-expression. */
7192 expression = cp_parser_simple_cast_expression (parser);
7193 /* Create the complete representation. */
7194 return build_x_unary_op (token->location,
7195 (keyword == RID_REALPART
7196 ? REALPART_EXPR : IMAGPART_EXPR),
7197 expression,
7198 tf_warning_or_error);
7200 break;
7202 case RID_TRANSACTION_ATOMIC:
7203 case RID_TRANSACTION_RELAXED:
7204 return cp_parser_transaction_expression (parser, keyword);
7206 case RID_NOEXCEPT:
7208 tree expr;
7209 const char *saved_message;
7210 bool saved_integral_constant_expression_p;
7211 bool saved_non_integral_constant_expression_p;
7212 bool saved_greater_than_is_operator_p;
7214 cp_lexer_consume_token (parser->lexer);
7215 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7217 saved_message = parser->type_definition_forbidden_message;
7218 parser->type_definition_forbidden_message
7219 = G_("types may not be defined in %<noexcept%> expressions");
7221 saved_integral_constant_expression_p
7222 = parser->integral_constant_expression_p;
7223 saved_non_integral_constant_expression_p
7224 = parser->non_integral_constant_expression_p;
7225 parser->integral_constant_expression_p = false;
7227 saved_greater_than_is_operator_p
7228 = parser->greater_than_is_operator_p;
7229 parser->greater_than_is_operator_p = true;
7231 ++cp_unevaluated_operand;
7232 ++c_inhibit_evaluation_warnings;
7233 ++cp_noexcept_operand;
7234 expr = cp_parser_expression (parser);
7235 --cp_noexcept_operand;
7236 --c_inhibit_evaluation_warnings;
7237 --cp_unevaluated_operand;
7239 parser->greater_than_is_operator_p
7240 = saved_greater_than_is_operator_p;
7242 parser->integral_constant_expression_p
7243 = saved_integral_constant_expression_p;
7244 parser->non_integral_constant_expression_p
7245 = saved_non_integral_constant_expression_p;
7247 parser->type_definition_forbidden_message = saved_message;
7249 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7250 return finish_noexcept_expr (expr, tf_warning_or_error);
7253 default:
7254 break;
7258 /* Look for the `:: new' and `:: delete', which also signal the
7259 beginning of a new-expression, or delete-expression,
7260 respectively. If the next token is `::', then it might be one of
7261 these. */
7262 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7264 enum rid keyword;
7266 /* See if the token after the `::' is one of the keywords in
7267 which we're interested. */
7268 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7269 /* If it's `new', we have a new-expression. */
7270 if (keyword == RID_NEW)
7271 return cp_parser_new_expression (parser);
7272 /* Similarly, for `delete'. */
7273 else if (keyword == RID_DELETE)
7274 return cp_parser_delete_expression (parser);
7277 /* Look for a unary operator. */
7278 unary_operator = cp_parser_unary_operator (token);
7279 /* The `++' and `--' operators can be handled similarly, even though
7280 they are not technically unary-operators in the grammar. */
7281 if (unary_operator == ERROR_MARK)
7283 if (token->type == CPP_PLUS_PLUS)
7284 unary_operator = PREINCREMENT_EXPR;
7285 else if (token->type == CPP_MINUS_MINUS)
7286 unary_operator = PREDECREMENT_EXPR;
7287 /* Handle the GNU address-of-label extension. */
7288 else if (cp_parser_allow_gnu_extensions_p (parser)
7289 && token->type == CPP_AND_AND)
7291 tree identifier;
7292 tree expression;
7293 location_t loc = token->location;
7295 /* Consume the '&&' token. */
7296 cp_lexer_consume_token (parser->lexer);
7297 /* Look for the identifier. */
7298 identifier = cp_parser_identifier (parser);
7299 /* Create an expression representing the address. */
7300 expression = finish_label_address_expr (identifier, loc);
7301 if (cp_parser_non_integral_constant_expression (parser,
7302 NIC_ADDR_LABEL))
7303 expression = error_mark_node;
7304 return expression;
7307 if (unary_operator != ERROR_MARK)
7309 tree cast_expression;
7310 tree expression = error_mark_node;
7311 non_integral_constant non_constant_p = NIC_NONE;
7312 location_t loc = token->location;
7313 tsubst_flags_t complain = complain_flags (decltype_p);
7315 /* Consume the operator token. */
7316 token = cp_lexer_consume_token (parser->lexer);
7317 /* Parse the cast-expression. */
7318 cast_expression
7319 = cp_parser_cast_expression (parser,
7320 unary_operator == ADDR_EXPR,
7321 /*cast_p=*/false,
7322 /*decltype*/false,
7323 pidk);
7324 /* Now, build an appropriate representation. */
7325 switch (unary_operator)
7327 case INDIRECT_REF:
7328 non_constant_p = NIC_STAR;
7329 expression = build_x_indirect_ref (loc, cast_expression,
7330 RO_UNARY_STAR,
7331 complain);
7332 break;
7334 case ADDR_EXPR:
7335 non_constant_p = NIC_ADDR;
7336 /* Fall through. */
7337 case BIT_NOT_EXPR:
7338 expression = build_x_unary_op (loc, unary_operator,
7339 cast_expression,
7340 complain);
7341 break;
7343 case PREINCREMENT_EXPR:
7344 case PREDECREMENT_EXPR:
7345 non_constant_p = unary_operator == PREINCREMENT_EXPR
7346 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7347 /* Fall through. */
7348 case UNARY_PLUS_EXPR:
7349 case NEGATE_EXPR:
7350 case TRUTH_NOT_EXPR:
7351 expression = finish_unary_op_expr (loc, unary_operator,
7352 cast_expression, complain);
7353 break;
7355 default:
7356 gcc_unreachable ();
7359 if (non_constant_p != NIC_NONE
7360 && cp_parser_non_integral_constant_expression (parser,
7361 non_constant_p))
7362 expression = error_mark_node;
7364 return expression;
7367 return cp_parser_postfix_expression (parser, address_p, cast_p,
7368 /*member_access_only_p=*/false,
7369 decltype_p,
7370 pidk);
7373 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7374 unary-operator, the corresponding tree code is returned. */
7376 static enum tree_code
7377 cp_parser_unary_operator (cp_token* token)
7379 switch (token->type)
7381 case CPP_MULT:
7382 return INDIRECT_REF;
7384 case CPP_AND:
7385 return ADDR_EXPR;
7387 case CPP_PLUS:
7388 return UNARY_PLUS_EXPR;
7390 case CPP_MINUS:
7391 return NEGATE_EXPR;
7393 case CPP_NOT:
7394 return TRUTH_NOT_EXPR;
7396 case CPP_COMPL:
7397 return BIT_NOT_EXPR;
7399 default:
7400 return ERROR_MARK;
7404 /* Parse a new-expression.
7406 new-expression:
7407 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7408 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7410 Returns a representation of the expression. */
7412 static tree
7413 cp_parser_new_expression (cp_parser* parser)
7415 bool global_scope_p;
7416 vec<tree, va_gc> *placement;
7417 tree type;
7418 vec<tree, va_gc> *initializer;
7419 tree nelts = NULL_TREE;
7420 tree ret;
7422 /* Look for the optional `::' operator. */
7423 global_scope_p
7424 = (cp_parser_global_scope_opt (parser,
7425 /*current_scope_valid_p=*/false)
7426 != NULL_TREE);
7427 /* Look for the `new' operator. */
7428 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7429 /* There's no easy way to tell a new-placement from the
7430 `( type-id )' construct. */
7431 cp_parser_parse_tentatively (parser);
7432 /* Look for a new-placement. */
7433 placement = cp_parser_new_placement (parser);
7434 /* If that didn't work out, there's no new-placement. */
7435 if (!cp_parser_parse_definitely (parser))
7437 if (placement != NULL)
7438 release_tree_vector (placement);
7439 placement = NULL;
7442 /* If the next token is a `(', then we have a parenthesized
7443 type-id. */
7444 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7446 cp_token *token;
7447 const char *saved_message = parser->type_definition_forbidden_message;
7449 /* Consume the `('. */
7450 cp_lexer_consume_token (parser->lexer);
7452 /* Parse the type-id. */
7453 parser->type_definition_forbidden_message
7454 = G_("types may not be defined in a new-expression");
7455 type = cp_parser_type_id (parser);
7456 parser->type_definition_forbidden_message = saved_message;
7458 /* Look for the closing `)'. */
7459 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7460 token = cp_lexer_peek_token (parser->lexer);
7461 /* There should not be a direct-new-declarator in this production,
7462 but GCC used to allowed this, so we check and emit a sensible error
7463 message for this case. */
7464 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7466 error_at (token->location,
7467 "array bound forbidden after parenthesized type-id");
7468 inform (token->location,
7469 "try removing the parentheses around the type-id");
7470 cp_parser_direct_new_declarator (parser);
7473 /* Otherwise, there must be a new-type-id. */
7474 else
7475 type = cp_parser_new_type_id (parser, &nelts);
7477 /* If the next token is a `(' or '{', then we have a new-initializer. */
7478 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7479 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7480 initializer = cp_parser_new_initializer (parser);
7481 else
7482 initializer = NULL;
7484 /* A new-expression may not appear in an integral constant
7485 expression. */
7486 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7487 ret = error_mark_node;
7488 else
7490 /* Create a representation of the new-expression. */
7491 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7492 tf_warning_or_error);
7495 if (placement != NULL)
7496 release_tree_vector (placement);
7497 if (initializer != NULL)
7498 release_tree_vector (initializer);
7500 return ret;
7503 /* Parse a new-placement.
7505 new-placement:
7506 ( expression-list )
7508 Returns the same representation as for an expression-list. */
7510 static vec<tree, va_gc> *
7511 cp_parser_new_placement (cp_parser* parser)
7513 vec<tree, va_gc> *expression_list;
7515 /* Parse the expression-list. */
7516 expression_list = (cp_parser_parenthesized_expression_list
7517 (parser, non_attr, /*cast_p=*/false,
7518 /*allow_expansion_p=*/true,
7519 /*non_constant_p=*/NULL));
7521 return expression_list;
7524 /* Parse a new-type-id.
7526 new-type-id:
7527 type-specifier-seq new-declarator [opt]
7529 Returns the TYPE allocated. If the new-type-id indicates an array
7530 type, *NELTS is set to the number of elements in the last array
7531 bound; the TYPE will not include the last array bound. */
7533 static tree
7534 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7536 cp_decl_specifier_seq type_specifier_seq;
7537 cp_declarator *new_declarator;
7538 cp_declarator *declarator;
7539 cp_declarator *outer_declarator;
7540 const char *saved_message;
7542 /* The type-specifier sequence must not contain type definitions.
7543 (It cannot contain declarations of new types either, but if they
7544 are not definitions we will catch that because they are not
7545 complete.) */
7546 saved_message = parser->type_definition_forbidden_message;
7547 parser->type_definition_forbidden_message
7548 = G_("types may not be defined in a new-type-id");
7549 /* Parse the type-specifier-seq. */
7550 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7551 /*is_trailing_return=*/false,
7552 &type_specifier_seq);
7553 /* Restore the old message. */
7554 parser->type_definition_forbidden_message = saved_message;
7556 if (type_specifier_seq.type == error_mark_node)
7557 return error_mark_node;
7559 /* Parse the new-declarator. */
7560 new_declarator = cp_parser_new_declarator_opt (parser);
7562 /* Determine the number of elements in the last array dimension, if
7563 any. */
7564 *nelts = NULL_TREE;
7565 /* Skip down to the last array dimension. */
7566 declarator = new_declarator;
7567 outer_declarator = NULL;
7568 while (declarator && (declarator->kind == cdk_pointer
7569 || declarator->kind == cdk_ptrmem))
7571 outer_declarator = declarator;
7572 declarator = declarator->declarator;
7574 while (declarator
7575 && declarator->kind == cdk_array
7576 && declarator->declarator
7577 && declarator->declarator->kind == cdk_array)
7579 outer_declarator = declarator;
7580 declarator = declarator->declarator;
7583 if (declarator && declarator->kind == cdk_array)
7585 *nelts = declarator->u.array.bounds;
7586 if (*nelts == error_mark_node)
7587 *nelts = integer_one_node;
7589 if (outer_declarator)
7590 outer_declarator->declarator = declarator->declarator;
7591 else
7592 new_declarator = NULL;
7595 return groktypename (&type_specifier_seq, new_declarator, false);
7598 /* Parse an (optional) new-declarator.
7600 new-declarator:
7601 ptr-operator new-declarator [opt]
7602 direct-new-declarator
7604 Returns the declarator. */
7606 static cp_declarator *
7607 cp_parser_new_declarator_opt (cp_parser* parser)
7609 enum tree_code code;
7610 tree type, std_attributes = NULL_TREE;
7611 cp_cv_quals cv_quals;
7613 /* We don't know if there's a ptr-operator next, or not. */
7614 cp_parser_parse_tentatively (parser);
7615 /* Look for a ptr-operator. */
7616 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7617 /* If that worked, look for more new-declarators. */
7618 if (cp_parser_parse_definitely (parser))
7620 cp_declarator *declarator;
7622 /* Parse another optional declarator. */
7623 declarator = cp_parser_new_declarator_opt (parser);
7625 declarator = cp_parser_make_indirect_declarator
7626 (code, type, cv_quals, declarator, std_attributes);
7628 return declarator;
7631 /* If the next token is a `[', there is a direct-new-declarator. */
7632 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7633 return cp_parser_direct_new_declarator (parser);
7635 return NULL;
7638 /* Parse a direct-new-declarator.
7640 direct-new-declarator:
7641 [ expression ]
7642 direct-new-declarator [constant-expression]
7646 static cp_declarator *
7647 cp_parser_direct_new_declarator (cp_parser* parser)
7649 cp_declarator *declarator = NULL;
7651 while (true)
7653 tree expression;
7654 cp_token *token;
7656 /* Look for the opening `['. */
7657 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7659 token = cp_lexer_peek_token (parser->lexer);
7660 expression = cp_parser_expression (parser);
7661 /* The standard requires that the expression have integral
7662 type. DR 74 adds enumeration types. We believe that the
7663 real intent is that these expressions be handled like the
7664 expression in a `switch' condition, which also allows
7665 classes with a single conversion to integral or
7666 enumeration type. */
7667 if (!processing_template_decl)
7669 expression
7670 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7671 expression,
7672 /*complain=*/true);
7673 if (!expression)
7675 error_at (token->location,
7676 "expression in new-declarator must have integral "
7677 "or enumeration type");
7678 expression = error_mark_node;
7682 /* Look for the closing `]'. */
7683 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7685 /* Add this bound to the declarator. */
7686 declarator = make_array_declarator (declarator, expression);
7688 /* If the next token is not a `[', then there are no more
7689 bounds. */
7690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7691 break;
7694 return declarator;
7697 /* Parse a new-initializer.
7699 new-initializer:
7700 ( expression-list [opt] )
7701 braced-init-list
7703 Returns a representation of the expression-list. */
7705 static vec<tree, va_gc> *
7706 cp_parser_new_initializer (cp_parser* parser)
7708 vec<tree, va_gc> *expression_list;
7710 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7712 tree t;
7713 bool expr_non_constant_p;
7714 cp_lexer_set_source_position (parser->lexer);
7715 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7716 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7717 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7718 expression_list = make_tree_vector_single (t);
7720 else
7721 expression_list = (cp_parser_parenthesized_expression_list
7722 (parser, non_attr, /*cast_p=*/false,
7723 /*allow_expansion_p=*/true,
7724 /*non_constant_p=*/NULL));
7726 return expression_list;
7729 /* Parse a delete-expression.
7731 delete-expression:
7732 :: [opt] delete cast-expression
7733 :: [opt] delete [ ] cast-expression
7735 Returns a representation of the expression. */
7737 static tree
7738 cp_parser_delete_expression (cp_parser* parser)
7740 bool global_scope_p;
7741 bool array_p;
7742 tree expression;
7744 /* Look for the optional `::' operator. */
7745 global_scope_p
7746 = (cp_parser_global_scope_opt (parser,
7747 /*current_scope_valid_p=*/false)
7748 != NULL_TREE);
7749 /* Look for the `delete' keyword. */
7750 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7751 /* See if the array syntax is in use. */
7752 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7754 /* Consume the `[' token. */
7755 cp_lexer_consume_token (parser->lexer);
7756 /* Look for the `]' token. */
7757 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7758 /* Remember that this is the `[]' construct. */
7759 array_p = true;
7761 else
7762 array_p = false;
7764 /* Parse the cast-expression. */
7765 expression = cp_parser_simple_cast_expression (parser);
7767 /* A delete-expression may not appear in an integral constant
7768 expression. */
7769 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7770 return error_mark_node;
7772 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7773 tf_warning_or_error);
7776 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7777 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7778 0 otherwise. */
7780 static int
7781 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7783 cp_token *token = cp_lexer_peek_token (parser->lexer);
7784 switch (token->type)
7786 case CPP_COMMA:
7787 case CPP_SEMICOLON:
7788 case CPP_QUERY:
7789 case CPP_COLON:
7790 case CPP_CLOSE_SQUARE:
7791 case CPP_CLOSE_PAREN:
7792 case CPP_CLOSE_BRACE:
7793 case CPP_OPEN_BRACE:
7794 case CPP_DOT:
7795 case CPP_DOT_STAR:
7796 case CPP_DEREF:
7797 case CPP_DEREF_STAR:
7798 case CPP_DIV:
7799 case CPP_MOD:
7800 case CPP_LSHIFT:
7801 case CPP_RSHIFT:
7802 case CPP_LESS:
7803 case CPP_GREATER:
7804 case CPP_LESS_EQ:
7805 case CPP_GREATER_EQ:
7806 case CPP_EQ_EQ:
7807 case CPP_NOT_EQ:
7808 case CPP_EQ:
7809 case CPP_MULT_EQ:
7810 case CPP_DIV_EQ:
7811 case CPP_MOD_EQ:
7812 case CPP_PLUS_EQ:
7813 case CPP_MINUS_EQ:
7814 case CPP_RSHIFT_EQ:
7815 case CPP_LSHIFT_EQ:
7816 case CPP_AND_EQ:
7817 case CPP_XOR_EQ:
7818 case CPP_OR_EQ:
7819 case CPP_XOR:
7820 case CPP_OR:
7821 case CPP_OR_OR:
7822 case CPP_EOF:
7823 case CPP_ELLIPSIS:
7824 return 0;
7826 case CPP_OPEN_PAREN:
7827 /* In ((type ()) () the last () isn't a valid cast-expression,
7828 so the whole must be parsed as postfix-expression. */
7829 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7830 != CPP_CLOSE_PAREN;
7832 case CPP_OPEN_SQUARE:
7833 /* '[' may start a primary-expression in obj-c++ and in C++11,
7834 as a lambda-expression, eg, '(void)[]{}'. */
7835 if (cxx_dialect >= cxx11)
7836 return -1;
7837 return c_dialect_objc ();
7839 case CPP_PLUS_PLUS:
7840 case CPP_MINUS_MINUS:
7841 /* '++' and '--' may or may not start a cast-expression:
7843 struct T { void operator++(int); };
7844 void f() { (T())++; }
7848 int a;
7849 (int)++a; */
7850 return -1;
7852 default:
7853 return 1;
7857 /* Parse a cast-expression.
7859 cast-expression:
7860 unary-expression
7861 ( type-id ) cast-expression
7863 ADDRESS_P is true iff the unary-expression is appearing as the
7864 operand of the `&' operator. CAST_P is true if this expression is
7865 the target of a cast.
7867 Returns a representation of the expression. */
7869 static tree
7870 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7871 bool decltype_p, cp_id_kind * pidk)
7873 /* If it's a `(', then we might be looking at a cast. */
7874 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7876 tree type = NULL_TREE;
7877 tree expr = NULL_TREE;
7878 int cast_expression = 0;
7879 const char *saved_message;
7881 /* There's no way to know yet whether or not this is a cast.
7882 For example, `(int (3))' is a unary-expression, while `(int)
7883 3' is a cast. So, we resort to parsing tentatively. */
7884 cp_parser_parse_tentatively (parser);
7885 /* Types may not be defined in a cast. */
7886 saved_message = parser->type_definition_forbidden_message;
7887 parser->type_definition_forbidden_message
7888 = G_("types may not be defined in casts");
7889 /* Consume the `('. */
7890 cp_lexer_consume_token (parser->lexer);
7891 /* A very tricky bit is that `(struct S) { 3 }' is a
7892 compound-literal (which we permit in C++ as an extension).
7893 But, that construct is not a cast-expression -- it is a
7894 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7895 is legal; if the compound-literal were a cast-expression,
7896 you'd need an extra set of parentheses.) But, if we parse
7897 the type-id, and it happens to be a class-specifier, then we
7898 will commit to the parse at that point, because we cannot
7899 undo the action that is done when creating a new class. So,
7900 then we cannot back up and do a postfix-expression.
7902 Another tricky case is the following (c++/29234):
7904 struct S { void operator () (); };
7906 void foo ()
7908 ( S()() );
7911 As a type-id we parse the parenthesized S()() as a function
7912 returning a function, groktypename complains and we cannot
7913 back up in this case either.
7915 Therefore, we scan ahead to the closing `)', and check to see
7916 if the tokens after the `)' can start a cast-expression. Otherwise
7917 we are dealing with an unary-expression, a postfix-expression
7918 or something else.
7920 Yet another tricky case, in C++11, is the following (c++/54891):
7922 (void)[]{};
7924 The issue is that usually, besides the case of lambda-expressions,
7925 the parenthesized type-id cannot be followed by '[', and, eg, we
7926 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7927 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7928 we don't commit, we try a cast-expression, then an unary-expression.
7930 Save tokens so that we can put them back. */
7931 cp_lexer_save_tokens (parser->lexer);
7933 /* We may be looking at a cast-expression. */
7934 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7935 /*consume_paren=*/true))
7936 cast_expression
7937 = cp_parser_tokens_start_cast_expression (parser);
7939 /* Roll back the tokens we skipped. */
7940 cp_lexer_rollback_tokens (parser->lexer);
7941 /* If we aren't looking at a cast-expression, simulate an error so
7942 that the call to cp_parser_error_occurred below returns true. */
7943 if (!cast_expression)
7944 cp_parser_simulate_error (parser);
7945 else
7947 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7948 parser->in_type_id_in_expr_p = true;
7949 /* Look for the type-id. */
7950 type = cp_parser_type_id (parser);
7951 /* Look for the closing `)'. */
7952 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7953 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7956 /* Restore the saved message. */
7957 parser->type_definition_forbidden_message = saved_message;
7959 /* At this point this can only be either a cast or a
7960 parenthesized ctor such as `(T ())' that looks like a cast to
7961 function returning T. */
7962 if (!cp_parser_error_occurred (parser))
7964 /* Only commit if the cast-expression doesn't start with
7965 '++', '--', or '[' in C++11. */
7966 if (cast_expression > 0)
7967 cp_parser_commit_to_topmost_tentative_parse (parser);
7969 expr = cp_parser_cast_expression (parser,
7970 /*address_p=*/false,
7971 /*cast_p=*/true,
7972 /*decltype_p=*/false,
7973 pidk);
7975 if (cp_parser_parse_definitely (parser))
7977 /* Warn about old-style casts, if so requested. */
7978 if (warn_old_style_cast
7979 && !in_system_header_at (input_location)
7980 && !VOID_TYPE_P (type)
7981 && current_lang_name != lang_name_c)
7982 warning (OPT_Wold_style_cast, "use of old-style cast");
7984 /* Only type conversions to integral or enumeration types
7985 can be used in constant-expressions. */
7986 if (!cast_valid_in_integral_constant_expression_p (type)
7987 && cp_parser_non_integral_constant_expression (parser,
7988 NIC_CAST))
7989 return error_mark_node;
7991 /* Perform the cast. */
7992 expr = build_c_cast (input_location, type, expr);
7993 return expr;
7996 else
7997 cp_parser_abort_tentative_parse (parser);
8000 /* If we get here, then it's not a cast, so it must be a
8001 unary-expression. */
8002 return cp_parser_unary_expression (parser, pidk, address_p,
8003 cast_p, decltype_p);
8006 /* Parse a binary expression of the general form:
8008 pm-expression:
8009 cast-expression
8010 pm-expression .* cast-expression
8011 pm-expression ->* cast-expression
8013 multiplicative-expression:
8014 pm-expression
8015 multiplicative-expression * pm-expression
8016 multiplicative-expression / pm-expression
8017 multiplicative-expression % pm-expression
8019 additive-expression:
8020 multiplicative-expression
8021 additive-expression + multiplicative-expression
8022 additive-expression - multiplicative-expression
8024 shift-expression:
8025 additive-expression
8026 shift-expression << additive-expression
8027 shift-expression >> additive-expression
8029 relational-expression:
8030 shift-expression
8031 relational-expression < shift-expression
8032 relational-expression > shift-expression
8033 relational-expression <= shift-expression
8034 relational-expression >= shift-expression
8036 GNU Extension:
8038 relational-expression:
8039 relational-expression <? shift-expression
8040 relational-expression >? shift-expression
8042 equality-expression:
8043 relational-expression
8044 equality-expression == relational-expression
8045 equality-expression != relational-expression
8047 and-expression:
8048 equality-expression
8049 and-expression & equality-expression
8051 exclusive-or-expression:
8052 and-expression
8053 exclusive-or-expression ^ and-expression
8055 inclusive-or-expression:
8056 exclusive-or-expression
8057 inclusive-or-expression | exclusive-or-expression
8059 logical-and-expression:
8060 inclusive-or-expression
8061 logical-and-expression && inclusive-or-expression
8063 logical-or-expression:
8064 logical-and-expression
8065 logical-or-expression || logical-and-expression
8067 All these are implemented with a single function like:
8069 binary-expression:
8070 simple-cast-expression
8071 binary-expression <token> binary-expression
8073 CAST_P is true if this expression is the target of a cast.
8075 The binops_by_token map is used to get the tree codes for each <token> type.
8076 binary-expressions are associated according to a precedence table. */
8078 #define TOKEN_PRECEDENCE(token) \
8079 (((token->type == CPP_GREATER \
8080 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8081 && !parser->greater_than_is_operator_p) \
8082 ? PREC_NOT_OPERATOR \
8083 : binops_by_token[token->type].prec)
8085 static tree
8086 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8087 bool no_toplevel_fold_p,
8088 bool decltype_p,
8089 enum cp_parser_prec prec,
8090 cp_id_kind * pidk)
8092 cp_parser_expression_stack stack;
8093 cp_parser_expression_stack_entry *sp = &stack[0];
8094 cp_parser_expression_stack_entry current;
8095 tree rhs;
8096 cp_token *token;
8097 enum tree_code rhs_type;
8098 enum cp_parser_prec new_prec, lookahead_prec;
8099 tree overload;
8101 /* Parse the first expression. */
8102 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8103 ? TRUTH_NOT_EXPR : ERROR_MARK);
8104 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8105 cast_p, decltype_p, pidk);
8106 current.prec = prec;
8108 if (cp_parser_error_occurred (parser))
8109 return error_mark_node;
8111 for (;;)
8113 /* Get an operator token. */
8114 token = cp_lexer_peek_token (parser->lexer);
8116 if (warn_cxx0x_compat
8117 && token->type == CPP_RSHIFT
8118 && !parser->greater_than_is_operator_p)
8120 if (warning_at (token->location, OPT_Wc__0x_compat,
8121 "%<>>%> operator is treated"
8122 " as two right angle brackets in C++11"))
8123 inform (token->location,
8124 "suggest parentheses around %<>>%> expression");
8127 new_prec = TOKEN_PRECEDENCE (token);
8129 /* Popping an entry off the stack means we completed a subexpression:
8130 - either we found a token which is not an operator (`>' where it is not
8131 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8132 will happen repeatedly;
8133 - or, we found an operator which has lower priority. This is the case
8134 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8135 parsing `3 * 4'. */
8136 if (new_prec <= current.prec)
8138 if (sp == stack)
8139 break;
8140 else
8141 goto pop;
8144 get_rhs:
8145 current.tree_type = binops_by_token[token->type].tree_type;
8146 current.loc = token->location;
8148 /* We used the operator token. */
8149 cp_lexer_consume_token (parser->lexer);
8151 /* For "false && x" or "true || x", x will never be executed;
8152 disable warnings while evaluating it. */
8153 if (current.tree_type == TRUTH_ANDIF_EXPR)
8154 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8155 else if (current.tree_type == TRUTH_ORIF_EXPR)
8156 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8158 /* Extract another operand. It may be the RHS of this expression
8159 or the LHS of a new, higher priority expression. */
8160 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8161 ? TRUTH_NOT_EXPR : ERROR_MARK);
8162 rhs = cp_parser_simple_cast_expression (parser);
8164 /* Get another operator token. Look up its precedence to avoid
8165 building a useless (immediately popped) stack entry for common
8166 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8167 token = cp_lexer_peek_token (parser->lexer);
8168 lookahead_prec = TOKEN_PRECEDENCE (token);
8169 if (lookahead_prec > new_prec)
8171 /* ... and prepare to parse the RHS of the new, higher priority
8172 expression. Since precedence levels on the stack are
8173 monotonically increasing, we do not have to care about
8174 stack overflows. */
8175 *sp = current;
8176 ++sp;
8177 current.lhs = rhs;
8178 current.lhs_type = rhs_type;
8179 current.prec = new_prec;
8180 new_prec = lookahead_prec;
8181 goto get_rhs;
8183 pop:
8184 lookahead_prec = new_prec;
8185 /* If the stack is not empty, we have parsed into LHS the right side
8186 (`4' in the example above) of an expression we had suspended.
8187 We can use the information on the stack to recover the LHS (`3')
8188 from the stack together with the tree code (`MULT_EXPR'), and
8189 the precedence of the higher level subexpression
8190 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8191 which will be used to actually build the additive expression. */
8192 rhs = current.lhs;
8193 rhs_type = current.lhs_type;
8194 --sp;
8195 current = *sp;
8198 /* Undo the disabling of warnings done above. */
8199 if (current.tree_type == TRUTH_ANDIF_EXPR)
8200 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8201 else if (current.tree_type == TRUTH_ORIF_EXPR)
8202 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8204 if (warn_logical_not_paren
8205 && current.lhs_type == TRUTH_NOT_EXPR)
8206 warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8208 overload = NULL;
8209 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8210 ERROR_MARK for everything that is not a binary expression.
8211 This makes warn_about_parentheses miss some warnings that
8212 involve unary operators. For unary expressions we should
8213 pass the correct tree_code unless the unary expression was
8214 surrounded by parentheses.
8216 if (no_toplevel_fold_p
8217 && lookahead_prec <= current.prec
8218 && sp == stack)
8219 current.lhs = build2 (current.tree_type,
8220 TREE_CODE_CLASS (current.tree_type)
8221 == tcc_comparison
8222 ? boolean_type_node : TREE_TYPE (current.lhs),
8223 current.lhs, rhs);
8224 else
8225 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8226 current.lhs, current.lhs_type,
8227 rhs, rhs_type, &overload,
8228 complain_flags (decltype_p));
8229 current.lhs_type = current.tree_type;
8230 if (EXPR_P (current.lhs))
8231 SET_EXPR_LOCATION (current.lhs, current.loc);
8233 /* If the binary operator required the use of an overloaded operator,
8234 then this expression cannot be an integral constant-expression.
8235 An overloaded operator can be used even if both operands are
8236 otherwise permissible in an integral constant-expression if at
8237 least one of the operands is of enumeration type. */
8239 if (overload
8240 && cp_parser_non_integral_constant_expression (parser,
8241 NIC_OVERLOADED))
8242 return error_mark_node;
8245 return current.lhs;
8248 static tree
8249 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8250 bool no_toplevel_fold_p,
8251 enum cp_parser_prec prec,
8252 cp_id_kind * pidk)
8254 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8255 /*decltype*/false, prec, pidk);
8258 /* Parse the `? expression : assignment-expression' part of a
8259 conditional-expression. The LOGICAL_OR_EXPR is the
8260 logical-or-expression that started the conditional-expression.
8261 Returns a representation of the entire conditional-expression.
8263 This routine is used by cp_parser_assignment_expression.
8265 ? expression : assignment-expression
8267 GNU Extensions:
8269 ? : assignment-expression */
8271 static tree
8272 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8274 tree expr;
8275 tree assignment_expr;
8276 struct cp_token *token;
8277 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8279 /* Consume the `?' token. */
8280 cp_lexer_consume_token (parser->lexer);
8281 token = cp_lexer_peek_token (parser->lexer);
8282 if (cp_parser_allow_gnu_extensions_p (parser)
8283 && token->type == CPP_COLON)
8285 pedwarn (token->location, OPT_Wpedantic,
8286 "ISO C++ does not allow ?: with omitted middle operand");
8287 /* Implicit true clause. */
8288 expr = NULL_TREE;
8289 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8290 warn_for_omitted_condop (token->location, logical_or_expr);
8292 else
8294 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8295 parser->colon_corrects_to_scope_p = false;
8296 /* Parse the expression. */
8297 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8298 expr = cp_parser_expression (parser);
8299 c_inhibit_evaluation_warnings +=
8300 ((logical_or_expr == truthvalue_true_node)
8301 - (logical_or_expr == truthvalue_false_node));
8302 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8305 /* The next token should be a `:'. */
8306 cp_parser_require (parser, CPP_COLON, RT_COLON);
8307 /* Parse the assignment-expression. */
8308 assignment_expr = cp_parser_assignment_expression (parser);
8309 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8311 /* Build the conditional-expression. */
8312 return build_x_conditional_expr (loc, logical_or_expr,
8313 expr,
8314 assignment_expr,
8315 tf_warning_or_error);
8318 /* Parse an assignment-expression.
8320 assignment-expression:
8321 conditional-expression
8322 logical-or-expression assignment-operator assignment_expression
8323 throw-expression
8325 CAST_P is true if this expression is the target of a cast.
8326 DECLTYPE_P is true if this expression is the operand of decltype.
8328 Returns a representation for the expression. */
8330 static tree
8331 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8332 bool cast_p, bool decltype_p)
8334 tree expr;
8336 /* If the next token is the `throw' keyword, then we're looking at
8337 a throw-expression. */
8338 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8339 expr = cp_parser_throw_expression (parser);
8340 /* Otherwise, it must be that we are looking at a
8341 logical-or-expression. */
8342 else
8344 /* Parse the binary expressions (logical-or-expression). */
8345 expr = cp_parser_binary_expression (parser, cast_p, false,
8346 decltype_p,
8347 PREC_NOT_OPERATOR, pidk);
8348 /* If the next token is a `?' then we're actually looking at a
8349 conditional-expression. */
8350 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8351 return cp_parser_question_colon_clause (parser, expr);
8352 else
8354 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8356 /* If it's an assignment-operator, we're using the second
8357 production. */
8358 enum tree_code assignment_operator
8359 = cp_parser_assignment_operator_opt (parser);
8360 if (assignment_operator != ERROR_MARK)
8362 bool non_constant_p;
8363 location_t saved_input_location;
8365 /* Parse the right-hand side of the assignment. */
8366 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8368 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8369 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8371 /* An assignment may not appear in a
8372 constant-expression. */
8373 if (cp_parser_non_integral_constant_expression (parser,
8374 NIC_ASSIGNMENT))
8375 return error_mark_node;
8376 /* Build the assignment expression. Its default
8377 location is the location of the '=' token. */
8378 saved_input_location = input_location;
8379 input_location = loc;
8380 expr = build_x_modify_expr (loc, expr,
8381 assignment_operator,
8382 rhs,
8383 complain_flags (decltype_p));
8384 input_location = saved_input_location;
8389 return expr;
8392 /* Parse an (optional) assignment-operator.
8394 assignment-operator: one of
8395 = *= /= %= += -= >>= <<= &= ^= |=
8397 GNU Extension:
8399 assignment-operator: one of
8400 <?= >?=
8402 If the next token is an assignment operator, the corresponding tree
8403 code is returned, and the token is consumed. For example, for
8404 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8405 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8406 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8407 operator, ERROR_MARK is returned. */
8409 static enum tree_code
8410 cp_parser_assignment_operator_opt (cp_parser* parser)
8412 enum tree_code op;
8413 cp_token *token;
8415 /* Peek at the next token. */
8416 token = cp_lexer_peek_token (parser->lexer);
8418 switch (token->type)
8420 case CPP_EQ:
8421 op = NOP_EXPR;
8422 break;
8424 case CPP_MULT_EQ:
8425 op = MULT_EXPR;
8426 break;
8428 case CPP_DIV_EQ:
8429 op = TRUNC_DIV_EXPR;
8430 break;
8432 case CPP_MOD_EQ:
8433 op = TRUNC_MOD_EXPR;
8434 break;
8436 case CPP_PLUS_EQ:
8437 op = PLUS_EXPR;
8438 break;
8440 case CPP_MINUS_EQ:
8441 op = MINUS_EXPR;
8442 break;
8444 case CPP_RSHIFT_EQ:
8445 op = RSHIFT_EXPR;
8446 break;
8448 case CPP_LSHIFT_EQ:
8449 op = LSHIFT_EXPR;
8450 break;
8452 case CPP_AND_EQ:
8453 op = BIT_AND_EXPR;
8454 break;
8456 case CPP_XOR_EQ:
8457 op = BIT_XOR_EXPR;
8458 break;
8460 case CPP_OR_EQ:
8461 op = BIT_IOR_EXPR;
8462 break;
8464 default:
8465 /* Nothing else is an assignment operator. */
8466 op = ERROR_MARK;
8469 /* If it was an assignment operator, consume it. */
8470 if (op != ERROR_MARK)
8471 cp_lexer_consume_token (parser->lexer);
8473 return op;
8476 /* Parse an expression.
8478 expression:
8479 assignment-expression
8480 expression , assignment-expression
8482 CAST_P is true if this expression is the target of a cast.
8483 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8484 except possibly parenthesized or on the RHS of a comma (N3276).
8486 Returns a representation of the expression. */
8488 static tree
8489 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8490 bool cast_p, bool decltype_p)
8492 tree expression = NULL_TREE;
8493 location_t loc = UNKNOWN_LOCATION;
8495 while (true)
8497 tree assignment_expression;
8499 /* Parse the next assignment-expression. */
8500 assignment_expression
8501 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8503 /* We don't create a temporary for a call that is the immediate operand
8504 of decltype or on the RHS of a comma. But when we see a comma, we
8505 need to create a temporary for a call on the LHS. */
8506 if (decltype_p && !processing_template_decl
8507 && TREE_CODE (assignment_expression) == CALL_EXPR
8508 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8509 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8510 assignment_expression
8511 = build_cplus_new (TREE_TYPE (assignment_expression),
8512 assignment_expression, tf_warning_or_error);
8514 /* If this is the first assignment-expression, we can just
8515 save it away. */
8516 if (!expression)
8517 expression = assignment_expression;
8518 else
8519 expression = build_x_compound_expr (loc, expression,
8520 assignment_expression,
8521 complain_flags (decltype_p));
8522 /* If the next token is not a comma, then we are done with the
8523 expression. */
8524 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8525 break;
8526 /* Consume the `,'. */
8527 loc = cp_lexer_peek_token (parser->lexer)->location;
8528 cp_lexer_consume_token (parser->lexer);
8529 /* A comma operator cannot appear in a constant-expression. */
8530 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8531 expression = error_mark_node;
8534 return expression;
8537 /* Parse a constant-expression.
8539 constant-expression:
8540 conditional-expression
8542 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8543 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8544 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8545 is false, NON_CONSTANT_P should be NULL. */
8547 static tree
8548 cp_parser_constant_expression (cp_parser* parser,
8549 bool allow_non_constant_p,
8550 bool *non_constant_p)
8552 bool saved_integral_constant_expression_p;
8553 bool saved_allow_non_integral_constant_expression_p;
8554 bool saved_non_integral_constant_expression_p;
8555 tree expression;
8557 /* It might seem that we could simply parse the
8558 conditional-expression, and then check to see if it were
8559 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8560 one that the compiler can figure out is constant, possibly after
8561 doing some simplifications or optimizations. The standard has a
8562 precise definition of constant-expression, and we must honor
8563 that, even though it is somewhat more restrictive.
8565 For example:
8567 int i[(2, 3)];
8569 is not a legal declaration, because `(2, 3)' is not a
8570 constant-expression. The `,' operator is forbidden in a
8571 constant-expression. However, GCC's constant-folding machinery
8572 will fold this operation to an INTEGER_CST for `3'. */
8574 /* Save the old settings. */
8575 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8576 saved_allow_non_integral_constant_expression_p
8577 = parser->allow_non_integral_constant_expression_p;
8578 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8579 /* We are now parsing a constant-expression. */
8580 parser->integral_constant_expression_p = true;
8581 parser->allow_non_integral_constant_expression_p
8582 = (allow_non_constant_p || cxx_dialect >= cxx11);
8583 parser->non_integral_constant_expression_p = false;
8584 /* Although the grammar says "conditional-expression", we parse an
8585 "assignment-expression", which also permits "throw-expression"
8586 and the use of assignment operators. In the case that
8587 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8588 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8589 actually essential that we look for an assignment-expression.
8590 For example, cp_parser_initializer_clauses uses this function to
8591 determine whether a particular assignment-expression is in fact
8592 constant. */
8593 expression = cp_parser_assignment_expression (parser);
8594 /* Restore the old settings. */
8595 parser->integral_constant_expression_p
8596 = saved_integral_constant_expression_p;
8597 parser->allow_non_integral_constant_expression_p
8598 = saved_allow_non_integral_constant_expression_p;
8599 if (cxx_dialect >= cxx11)
8601 /* Require an rvalue constant expression here; that's what our
8602 callers expect. Reference constant expressions are handled
8603 separately in e.g. cp_parser_template_argument. */
8604 bool is_const = potential_rvalue_constant_expression (expression);
8605 parser->non_integral_constant_expression_p = !is_const;
8606 if (!is_const && !allow_non_constant_p)
8607 require_potential_rvalue_constant_expression (expression);
8609 if (allow_non_constant_p)
8610 *non_constant_p = parser->non_integral_constant_expression_p;
8611 parser->non_integral_constant_expression_p
8612 = saved_non_integral_constant_expression_p;
8614 return expression;
8617 /* Parse __builtin_offsetof.
8619 offsetof-expression:
8620 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8622 offsetof-member-designator:
8623 id-expression
8624 | offsetof-member-designator "." id-expression
8625 | offsetof-member-designator "[" expression "]"
8626 | offsetof-member-designator "->" id-expression */
8628 static tree
8629 cp_parser_builtin_offsetof (cp_parser *parser)
8631 int save_ice_p, save_non_ice_p;
8632 tree type, expr;
8633 cp_id_kind dummy;
8634 cp_token *token;
8636 /* We're about to accept non-integral-constant things, but will
8637 definitely yield an integral constant expression. Save and
8638 restore these values around our local parsing. */
8639 save_ice_p = parser->integral_constant_expression_p;
8640 save_non_ice_p = parser->non_integral_constant_expression_p;
8642 /* Consume the "__builtin_offsetof" token. */
8643 cp_lexer_consume_token (parser->lexer);
8644 /* Consume the opening `('. */
8645 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8646 /* Parse the type-id. */
8647 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8648 type = cp_parser_type_id (parser);
8649 /* Look for the `,'. */
8650 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8651 token = cp_lexer_peek_token (parser->lexer);
8653 /* Build the (type *)null that begins the traditional offsetof macro. */
8654 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8655 tf_warning_or_error);
8657 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8658 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8659 true, &dummy, token->location);
8660 while (true)
8662 token = cp_lexer_peek_token (parser->lexer);
8663 switch (token->type)
8665 case CPP_OPEN_SQUARE:
8666 /* offsetof-member-designator "[" expression "]" */
8667 expr = cp_parser_postfix_open_square_expression (parser, expr,
8668 true, false);
8669 break;
8671 case CPP_DEREF:
8672 /* offsetof-member-designator "->" identifier */
8673 expr = grok_array_decl (token->location, expr,
8674 integer_zero_node, false);
8675 /* FALLTHRU */
8677 case CPP_DOT:
8678 /* offsetof-member-designator "." identifier */
8679 cp_lexer_consume_token (parser->lexer);
8680 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8681 expr, true, &dummy,
8682 token->location);
8683 break;
8685 case CPP_CLOSE_PAREN:
8686 /* Consume the ")" token. */
8687 cp_lexer_consume_token (parser->lexer);
8688 goto success;
8690 default:
8691 /* Error. We know the following require will fail, but
8692 that gives the proper error message. */
8693 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8694 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8695 expr = error_mark_node;
8696 goto failure;
8700 success:
8701 /* If we're processing a template, we can't finish the semantics yet.
8702 Otherwise we can fold the entire expression now. */
8703 if (processing_template_decl)
8705 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8706 SET_EXPR_LOCATION (expr, loc);
8708 else
8709 expr = finish_offsetof (expr, loc);
8711 failure:
8712 parser->integral_constant_expression_p = save_ice_p;
8713 parser->non_integral_constant_expression_p = save_non_ice_p;
8715 return expr;
8718 /* Parse a trait expression.
8720 Returns a representation of the expression, the underlying type
8721 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8723 static tree
8724 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8726 cp_trait_kind kind;
8727 tree type1, type2 = NULL_TREE;
8728 bool binary = false;
8729 bool variadic = false;
8731 switch (keyword)
8733 case RID_HAS_NOTHROW_ASSIGN:
8734 kind = CPTK_HAS_NOTHROW_ASSIGN;
8735 break;
8736 case RID_HAS_NOTHROW_CONSTRUCTOR:
8737 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8738 break;
8739 case RID_HAS_NOTHROW_COPY:
8740 kind = CPTK_HAS_NOTHROW_COPY;
8741 break;
8742 case RID_HAS_TRIVIAL_ASSIGN:
8743 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8744 break;
8745 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8746 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8747 break;
8748 case RID_HAS_TRIVIAL_COPY:
8749 kind = CPTK_HAS_TRIVIAL_COPY;
8750 break;
8751 case RID_HAS_TRIVIAL_DESTRUCTOR:
8752 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8753 break;
8754 case RID_HAS_VIRTUAL_DESTRUCTOR:
8755 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8756 break;
8757 case RID_IS_ABSTRACT:
8758 kind = CPTK_IS_ABSTRACT;
8759 break;
8760 case RID_IS_BASE_OF:
8761 kind = CPTK_IS_BASE_OF;
8762 binary = true;
8763 break;
8764 case RID_IS_CLASS:
8765 kind = CPTK_IS_CLASS;
8766 break;
8767 case RID_IS_EMPTY:
8768 kind = CPTK_IS_EMPTY;
8769 break;
8770 case RID_IS_ENUM:
8771 kind = CPTK_IS_ENUM;
8772 break;
8773 case RID_IS_FINAL:
8774 kind = CPTK_IS_FINAL;
8775 break;
8776 case RID_IS_LITERAL_TYPE:
8777 kind = CPTK_IS_LITERAL_TYPE;
8778 break;
8779 case RID_IS_POD:
8780 kind = CPTK_IS_POD;
8781 break;
8782 case RID_IS_POLYMORPHIC:
8783 kind = CPTK_IS_POLYMORPHIC;
8784 break;
8785 case RID_IS_STD_LAYOUT:
8786 kind = CPTK_IS_STD_LAYOUT;
8787 break;
8788 case RID_IS_TRIVIAL:
8789 kind = CPTK_IS_TRIVIAL;
8790 break;
8791 case RID_IS_TRIVIALLY_ASSIGNABLE:
8792 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8793 binary = true;
8794 break;
8795 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8796 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8797 variadic = true;
8798 break;
8799 case RID_IS_TRIVIALLY_COPYABLE:
8800 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8801 break;
8802 case RID_IS_UNION:
8803 kind = CPTK_IS_UNION;
8804 break;
8805 case RID_UNDERLYING_TYPE:
8806 kind = CPTK_UNDERLYING_TYPE;
8807 break;
8808 case RID_BASES:
8809 kind = CPTK_BASES;
8810 break;
8811 case RID_DIRECT_BASES:
8812 kind = CPTK_DIRECT_BASES;
8813 break;
8814 default:
8815 gcc_unreachable ();
8818 /* Consume the token. */
8819 cp_lexer_consume_token (parser->lexer);
8821 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8823 type1 = cp_parser_type_id (parser);
8825 if (type1 == error_mark_node)
8826 return error_mark_node;
8828 if (binary)
8830 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8832 type2 = cp_parser_type_id (parser);
8834 if (type2 == error_mark_node)
8835 return error_mark_node;
8837 else if (variadic)
8839 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8841 cp_lexer_consume_token (parser->lexer);
8842 tree elt = cp_parser_type_id (parser);
8843 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8845 cp_lexer_consume_token (parser->lexer);
8846 elt = make_pack_expansion (elt);
8848 if (elt == error_mark_node)
8849 return error_mark_node;
8850 type2 = tree_cons (NULL_TREE, elt, type2);
8854 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8856 /* Complete the trait expression, which may mean either processing
8857 the trait expr now or saving it for template instantiation. */
8858 switch(kind)
8860 case CPTK_UNDERLYING_TYPE:
8861 return finish_underlying_type (type1);
8862 case CPTK_BASES:
8863 return finish_bases (type1, false);
8864 case CPTK_DIRECT_BASES:
8865 return finish_bases (type1, true);
8866 default:
8867 return finish_trait_expr (kind, type1, type2);
8871 /* Lambdas that appear in variable initializer or default argument scope
8872 get that in their mangling, so we need to record it. We might as well
8873 use the count for function and namespace scopes as well. */
8874 static GTY(()) tree lambda_scope;
8875 static GTY(()) int lambda_count;
8876 typedef struct GTY(()) tree_int
8878 tree t;
8879 int i;
8880 } tree_int;
8881 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8883 static void
8884 start_lambda_scope (tree decl)
8886 tree_int ti;
8887 gcc_assert (decl);
8888 /* Once we're inside a function, we ignore other scopes and just push
8889 the function again so that popping works properly. */
8890 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8891 decl = current_function_decl;
8892 ti.t = lambda_scope;
8893 ti.i = lambda_count;
8894 vec_safe_push (lambda_scope_stack, ti);
8895 if (lambda_scope != decl)
8897 /* Don't reset the count if we're still in the same function. */
8898 lambda_scope = decl;
8899 lambda_count = 0;
8903 static void
8904 record_lambda_scope (tree lambda)
8906 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8907 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8910 static void
8911 finish_lambda_scope (void)
8913 tree_int *p = &lambda_scope_stack->last ();
8914 if (lambda_scope != p->t)
8916 lambda_scope = p->t;
8917 lambda_count = p->i;
8919 lambda_scope_stack->pop ();
8922 /* Parse a lambda expression.
8924 lambda-expression:
8925 lambda-introducer lambda-declarator [opt] compound-statement
8927 Returns a representation of the expression. */
8929 static tree
8930 cp_parser_lambda_expression (cp_parser* parser)
8932 tree lambda_expr = build_lambda_expr ();
8933 tree type;
8934 bool ok = true;
8935 cp_token *token = cp_lexer_peek_token (parser->lexer);
8936 cp_token_position start = 0;
8938 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8940 if (cp_unevaluated_operand)
8942 if (!token->error_reported)
8944 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8945 "lambda-expression in unevaluated context");
8946 token->error_reported = true;
8948 ok = false;
8950 else if (parser->in_template_argument_list_p)
8952 if (!token->error_reported)
8954 error_at (token->location, "lambda-expression in template-argument");
8955 token->error_reported = true;
8957 ok = false;
8960 /* We may be in the middle of deferred access check. Disable
8961 it now. */
8962 push_deferring_access_checks (dk_no_deferred);
8964 cp_parser_lambda_introducer (parser, lambda_expr);
8966 type = begin_lambda_type (lambda_expr);
8967 if (type == error_mark_node)
8968 return error_mark_node;
8970 record_lambda_scope (lambda_expr);
8972 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8973 determine_visibility (TYPE_NAME (type));
8975 /* Now that we've started the type, add the capture fields for any
8976 explicit captures. */
8977 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8980 /* Inside the class, surrounding template-parameter-lists do not apply. */
8981 unsigned int saved_num_template_parameter_lists
8982 = parser->num_template_parameter_lists;
8983 unsigned char in_statement = parser->in_statement;
8984 bool in_switch_statement_p = parser->in_switch_statement_p;
8985 bool fully_implicit_function_template_p
8986 = parser->fully_implicit_function_template_p;
8987 tree implicit_template_parms = parser->implicit_template_parms;
8988 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8989 bool auto_is_implicit_function_template_parm_p
8990 = parser->auto_is_implicit_function_template_parm_p;
8992 parser->num_template_parameter_lists = 0;
8993 parser->in_statement = 0;
8994 parser->in_switch_statement_p = false;
8995 parser->fully_implicit_function_template_p = false;
8996 parser->implicit_template_parms = 0;
8997 parser->implicit_template_scope = 0;
8998 parser->auto_is_implicit_function_template_parm_p = false;
9000 /* By virtue of defining a local class, a lambda expression has access to
9001 the private variables of enclosing classes. */
9003 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9005 if (ok)
9007 if (!cp_parser_error_occurred (parser)
9008 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9009 && cp_parser_start_tentative_firewall (parser))
9010 start = token;
9011 cp_parser_lambda_body (parser, lambda_expr);
9013 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9015 if (cp_parser_skip_to_closing_brace (parser))
9016 cp_lexer_consume_token (parser->lexer);
9019 /* The capture list was built up in reverse order; fix that now. */
9020 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9021 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9023 if (ok)
9024 maybe_add_lambda_conv_op (type);
9026 type = finish_struct (type, /*attributes=*/NULL_TREE);
9028 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9029 parser->in_statement = in_statement;
9030 parser->in_switch_statement_p = in_switch_statement_p;
9031 parser->fully_implicit_function_template_p
9032 = fully_implicit_function_template_p;
9033 parser->implicit_template_parms = implicit_template_parms;
9034 parser->implicit_template_scope = implicit_template_scope;
9035 parser->auto_is_implicit_function_template_parm_p
9036 = auto_is_implicit_function_template_parm_p;
9039 pop_deferring_access_checks ();
9041 /* This field is only used during parsing of the lambda. */
9042 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9044 /* This lambda shouldn't have any proxies left at this point. */
9045 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9046 /* And now that we're done, push proxies for an enclosing lambda. */
9047 insert_pending_capture_proxies ();
9049 if (ok)
9050 lambda_expr = build_lambda_object (lambda_expr);
9051 else
9052 lambda_expr = error_mark_node;
9054 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9056 return lambda_expr;
9059 /* Parse the beginning of a lambda expression.
9061 lambda-introducer:
9062 [ lambda-capture [opt] ]
9064 LAMBDA_EXPR is the current representation of the lambda expression. */
9066 static void
9067 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9069 /* Need commas after the first capture. */
9070 bool first = true;
9072 /* Eat the leading `['. */
9073 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9075 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9076 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9077 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9078 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9079 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9080 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9082 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9084 cp_lexer_consume_token (parser->lexer);
9085 first = false;
9088 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9090 cp_token* capture_token;
9091 tree capture_id;
9092 tree capture_init_expr;
9093 cp_id_kind idk = CP_ID_KIND_NONE;
9094 bool explicit_init_p = false;
9096 enum capture_kind_type
9098 BY_COPY,
9099 BY_REFERENCE
9101 enum capture_kind_type capture_kind = BY_COPY;
9103 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9105 error ("expected end of capture-list");
9106 return;
9109 if (first)
9110 first = false;
9111 else
9112 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9114 /* Possibly capture `this'. */
9115 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9117 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9118 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9119 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9120 "with by-copy capture default");
9121 cp_lexer_consume_token (parser->lexer);
9122 add_capture (lambda_expr,
9123 /*id=*/this_identifier,
9124 /*initializer=*/finish_this_expr(),
9125 /*by_reference_p=*/false,
9126 explicit_init_p);
9127 continue;
9130 /* Remember whether we want to capture as a reference or not. */
9131 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9133 capture_kind = BY_REFERENCE;
9134 cp_lexer_consume_token (parser->lexer);
9137 /* Get the identifier. */
9138 capture_token = cp_lexer_peek_token (parser->lexer);
9139 capture_id = cp_parser_identifier (parser);
9141 if (capture_id == error_mark_node)
9142 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9143 delimiters, but I modified this to stop on unnested ']' as well. It
9144 was already changed to stop on unnested '}', so the
9145 "closing_parenthesis" name is no more misleading with my change. */
9147 cp_parser_skip_to_closing_parenthesis (parser,
9148 /*recovering=*/true,
9149 /*or_comma=*/true,
9150 /*consume_paren=*/true);
9151 break;
9154 /* Find the initializer for this capture. */
9155 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9156 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9157 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9159 bool direct, non_constant;
9160 /* An explicit initializer exists. */
9161 if (cxx_dialect < cxx14)
9162 pedwarn (input_location, 0,
9163 "lambda capture initializers "
9164 "only available with -std=c++14 or -std=gnu++14");
9165 capture_init_expr = cp_parser_initializer (parser, &direct,
9166 &non_constant);
9167 explicit_init_p = true;
9168 if (capture_init_expr == NULL_TREE)
9170 error ("empty initializer for lambda init-capture");
9171 capture_init_expr = error_mark_node;
9174 else
9176 const char* error_msg;
9178 /* Turn the identifier into an id-expression. */
9179 capture_init_expr
9180 = cp_parser_lookup_name_simple (parser, capture_id,
9181 capture_token->location);
9183 if (capture_init_expr == error_mark_node)
9185 unqualified_name_lookup_error (capture_id);
9186 continue;
9188 else if (DECL_P (capture_init_expr)
9189 && (!VAR_P (capture_init_expr)
9190 && TREE_CODE (capture_init_expr) != PARM_DECL))
9192 error_at (capture_token->location,
9193 "capture of non-variable %qD ",
9194 capture_init_expr);
9195 inform (0, "%q+#D declared here", capture_init_expr);
9196 continue;
9198 if (VAR_P (capture_init_expr)
9199 && decl_storage_duration (capture_init_expr) != dk_auto)
9201 if (pedwarn (capture_token->location, 0, "capture of variable "
9202 "%qD with non-automatic storage duration",
9203 capture_init_expr))
9204 inform (0, "%q+#D declared here", capture_init_expr);
9205 continue;
9208 capture_init_expr
9209 = finish_id_expression
9210 (capture_id,
9211 capture_init_expr,
9212 parser->scope,
9213 &idk,
9214 /*integral_constant_expression_p=*/false,
9215 /*allow_non_integral_constant_expression_p=*/false,
9216 /*non_integral_constant_expression_p=*/NULL,
9217 /*template_p=*/false,
9218 /*done=*/true,
9219 /*address_p=*/false,
9220 /*template_arg_p=*/false,
9221 &error_msg,
9222 capture_token->location);
9224 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9226 cp_lexer_consume_token (parser->lexer);
9227 capture_init_expr = make_pack_expansion (capture_init_expr);
9229 else
9230 check_for_bare_parameter_packs (capture_init_expr);
9233 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9234 && !explicit_init_p)
9236 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9237 && capture_kind == BY_COPY)
9238 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9239 "of %qD redundant with by-copy capture default",
9240 capture_id);
9241 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9242 && capture_kind == BY_REFERENCE)
9243 pedwarn (capture_token->location, 0, "explicit by-reference "
9244 "capture of %qD redundant with by-reference capture "
9245 "default", capture_id);
9248 add_capture (lambda_expr,
9249 capture_id,
9250 capture_init_expr,
9251 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9252 explicit_init_p);
9255 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9258 /* Parse the (optional) middle of a lambda expression.
9260 lambda-declarator:
9261 < template-parameter-list [opt] >
9262 ( parameter-declaration-clause [opt] )
9263 attribute-specifier [opt]
9264 mutable [opt]
9265 exception-specification [opt]
9266 lambda-return-type-clause [opt]
9268 LAMBDA_EXPR is the current representation of the lambda expression. */
9270 static bool
9271 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9273 /* 5.1.1.4 of the standard says:
9274 If a lambda-expression does not include a lambda-declarator, it is as if
9275 the lambda-declarator were ().
9276 This means an empty parameter list, no attributes, and no exception
9277 specification. */
9278 tree param_list = void_list_node;
9279 tree attributes = NULL_TREE;
9280 tree exception_spec = NULL_TREE;
9281 tree template_param_list = NULL_TREE;
9283 /* The template-parameter-list is optional, but must begin with
9284 an opening angle if present. */
9285 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9287 if (cxx_dialect < cxx14)
9288 pedwarn (parser->lexer->next_token->location, 0,
9289 "lambda templates are only available with "
9290 "-std=c++14 or -std=gnu++14");
9292 cp_lexer_consume_token (parser->lexer);
9294 template_param_list = cp_parser_template_parameter_list (parser);
9296 cp_parser_skip_to_end_of_template_parameter_list (parser);
9298 /* We just processed one more parameter list. */
9299 ++parser->num_template_parameter_lists;
9302 /* The parameter-declaration-clause is optional (unless
9303 template-parameter-list was given), but must begin with an
9304 opening parenthesis if present. */
9305 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9307 cp_lexer_consume_token (parser->lexer);
9309 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9311 /* Parse parameters. */
9312 param_list = cp_parser_parameter_declaration_clause (parser);
9314 /* Default arguments shall not be specified in the
9315 parameter-declaration-clause of a lambda-declarator. */
9316 for (tree t = param_list; t; t = TREE_CHAIN (t))
9317 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9318 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9319 "default argument specified for lambda parameter");
9321 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9323 attributes = cp_parser_attributes_opt (parser);
9325 /* Parse optional `mutable' keyword. */
9326 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9328 cp_lexer_consume_token (parser->lexer);
9329 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9332 /* Parse optional exception specification. */
9333 exception_spec = cp_parser_exception_specification_opt (parser);
9335 /* Parse optional trailing return type. */
9336 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9338 cp_lexer_consume_token (parser->lexer);
9339 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9340 = cp_parser_trailing_type_id (parser);
9343 /* The function parameters must be in scope all the way until after the
9344 trailing-return-type in case of decltype. */
9345 pop_bindings_and_leave_scope ();
9347 else if (template_param_list != NULL_TREE) // generate diagnostic
9348 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9350 /* Create the function call operator.
9352 Messing with declarators like this is no uglier than building up the
9353 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9354 other code. */
9356 cp_decl_specifier_seq return_type_specs;
9357 cp_declarator* declarator;
9358 tree fco;
9359 int quals;
9360 void *p;
9362 clear_decl_specs (&return_type_specs);
9363 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9364 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9365 else
9366 /* Maybe we will deduce the return type later. */
9367 return_type_specs.type = make_auto ();
9369 p = obstack_alloc (&declarator_obstack, 0);
9371 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9372 sfk_none);
9374 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9375 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9376 declarator = make_call_declarator (declarator, param_list, quals,
9377 VIRT_SPEC_UNSPECIFIED,
9378 REF_QUAL_NONE,
9379 exception_spec,
9380 /*late_return_type=*/NULL_TREE);
9381 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9383 fco = grokmethod (&return_type_specs,
9384 declarator,
9385 attributes);
9386 if (fco != error_mark_node)
9388 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9389 DECL_ARTIFICIAL (fco) = 1;
9390 /* Give the object parameter a different name. */
9391 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9392 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9393 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9395 if (template_param_list)
9397 fco = finish_member_template_decl (fco);
9398 finish_template_decl (template_param_list);
9399 --parser->num_template_parameter_lists;
9401 else if (parser->fully_implicit_function_template_p)
9402 fco = finish_fully_implicit_template (parser, fco);
9404 finish_member_declaration (fco);
9406 obstack_free (&declarator_obstack, p);
9408 return (fco != error_mark_node);
9412 /* Parse the body of a lambda expression, which is simply
9414 compound-statement
9416 but which requires special handling.
9417 LAMBDA_EXPR is the current representation of the lambda expression. */
9419 static void
9420 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9422 bool nested = (current_function_decl != NULL_TREE);
9423 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9424 if (nested)
9425 push_function_context ();
9426 else
9427 /* Still increment function_depth so that we don't GC in the
9428 middle of an expression. */
9429 ++function_depth;
9430 /* Clear this in case we're in the middle of a default argument. */
9431 parser->local_variables_forbidden_p = false;
9433 /* Finish the function call operator
9434 - class_specifier
9435 + late_parsing_for_member
9436 + function_definition_after_declarator
9437 + ctor_initializer_opt_and_function_body */
9439 tree fco = lambda_function (lambda_expr);
9440 tree body;
9441 bool done = false;
9442 tree compound_stmt;
9443 tree cap;
9445 /* Let the front end know that we are going to be defining this
9446 function. */
9447 start_preparsed_function (fco,
9448 NULL_TREE,
9449 SF_PRE_PARSED | SF_INCLASS_INLINE);
9451 start_lambda_scope (fco);
9452 body = begin_function_body ();
9454 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9455 goto out;
9457 /* Push the proxies for any explicit captures. */
9458 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9459 cap = TREE_CHAIN (cap))
9460 build_capture_proxy (TREE_PURPOSE (cap));
9462 compound_stmt = begin_compound_stmt (0);
9464 /* 5.1.1.4 of the standard says:
9465 If a lambda-expression does not include a trailing-return-type, it
9466 is as if the trailing-return-type denotes the following type:
9467 * if the compound-statement is of the form
9468 { return attribute-specifier [opt] expression ; }
9469 the type of the returned expression after lvalue-to-rvalue
9470 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9471 (_conv.array_ 4.2), and function-to-pointer conversion
9472 (_conv.func_ 4.3);
9473 * otherwise, void. */
9475 /* In a lambda that has neither a lambda-return-type-clause
9476 nor a deducible form, errors should be reported for return statements
9477 in the body. Since we used void as the placeholder return type, parsing
9478 the body as usual will give such desired behavior. */
9479 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9480 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9481 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9483 tree expr = NULL_TREE;
9484 cp_id_kind idk = CP_ID_KIND_NONE;
9486 /* Parse tentatively in case there's more after the initial return
9487 statement. */
9488 cp_parser_parse_tentatively (parser);
9490 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9492 expr = cp_parser_expression (parser, &idk);
9494 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9495 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9497 if (cp_parser_parse_definitely (parser))
9499 if (!processing_template_decl)
9500 apply_deduced_return_type (fco, lambda_return_type (expr));
9502 /* Will get error here if type not deduced yet. */
9503 finish_return_stmt (expr);
9505 done = true;
9509 if (!done)
9511 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9512 cp_parser_label_declaration (parser);
9513 cp_parser_statement_seq_opt (parser, NULL_TREE);
9514 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9517 finish_compound_stmt (compound_stmt);
9519 out:
9520 finish_function_body (body);
9521 finish_lambda_scope ();
9523 /* Finish the function and generate code for it if necessary. */
9524 tree fn = finish_function (/*inline*/2);
9526 /* Only expand if the call op is not a template. */
9527 if (!DECL_TEMPLATE_INFO (fco))
9528 expand_or_defer_fn (fn);
9531 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9532 if (nested)
9533 pop_function_context();
9534 else
9535 --function_depth;
9538 /* Statements [gram.stmt.stmt] */
9540 /* Parse a statement.
9542 statement:
9543 labeled-statement
9544 expression-statement
9545 compound-statement
9546 selection-statement
9547 iteration-statement
9548 jump-statement
9549 declaration-statement
9550 try-block
9552 C++11:
9554 statement:
9555 labeled-statement
9556 attribute-specifier-seq (opt) expression-statement
9557 attribute-specifier-seq (opt) compound-statement
9558 attribute-specifier-seq (opt) selection-statement
9559 attribute-specifier-seq (opt) iteration-statement
9560 attribute-specifier-seq (opt) jump-statement
9561 declaration-statement
9562 attribute-specifier-seq (opt) try-block
9564 TM Extension:
9566 statement:
9567 atomic-statement
9569 IN_COMPOUND is true when the statement is nested inside a
9570 cp_parser_compound_statement; this matters for certain pragmas.
9572 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9573 is a (possibly labeled) if statement which is not enclosed in braces
9574 and has an else clause. This is used to implement -Wparentheses. */
9576 static void
9577 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9578 bool in_compound, bool *if_p)
9580 tree statement, std_attrs = NULL_TREE;
9581 cp_token *token;
9582 location_t statement_location, attrs_location;
9584 restart:
9585 if (if_p != NULL)
9586 *if_p = false;
9587 /* There is no statement yet. */
9588 statement = NULL_TREE;
9590 saved_token_sentinel saved_tokens (parser->lexer);
9591 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9592 if (c_dialect_objc ())
9593 /* In obj-c++, seeing '[[' might be the either the beginning of
9594 c++11 attributes, or a nested objc-message-expression. So
9595 let's parse the c++11 attributes tentatively. */
9596 cp_parser_parse_tentatively (parser);
9597 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9598 if (c_dialect_objc ())
9600 if (!cp_parser_parse_definitely (parser))
9601 std_attrs = NULL_TREE;
9604 /* Peek at the next token. */
9605 token = cp_lexer_peek_token (parser->lexer);
9606 /* Remember the location of the first token in the statement. */
9607 statement_location = token->location;
9608 /* If this is a keyword, then that will often determine what kind of
9609 statement we have. */
9610 if (token->type == CPP_KEYWORD)
9612 enum rid keyword = token->keyword;
9614 switch (keyword)
9616 case RID_CASE:
9617 case RID_DEFAULT:
9618 /* Looks like a labeled-statement with a case label.
9619 Parse the label, and then use tail recursion to parse
9620 the statement. */
9621 cp_parser_label_for_labeled_statement (parser, std_attrs);
9622 goto restart;
9624 case RID_IF:
9625 case RID_SWITCH:
9626 statement = cp_parser_selection_statement (parser, if_p);
9627 break;
9629 case RID_WHILE:
9630 case RID_DO:
9631 case RID_FOR:
9632 statement = cp_parser_iteration_statement (parser, false);
9633 break;
9635 case RID_CILK_FOR:
9636 if (!flag_cilkplus)
9638 error_at (cp_lexer_peek_token (parser->lexer)->location,
9639 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9640 cp_lexer_consume_token (parser->lexer);
9641 statement = error_mark_node;
9643 else
9644 statement = cp_parser_cilk_for (parser, integer_zero_node);
9645 break;
9647 case RID_BREAK:
9648 case RID_CONTINUE:
9649 case RID_RETURN:
9650 case RID_GOTO:
9651 statement = cp_parser_jump_statement (parser);
9652 break;
9654 case RID_CILK_SYNC:
9655 cp_lexer_consume_token (parser->lexer);
9656 if (flag_cilkplus)
9658 tree sync_expr = build_cilk_sync ();
9659 SET_EXPR_LOCATION (sync_expr,
9660 token->location);
9661 statement = finish_expr_stmt (sync_expr);
9663 else
9665 error_at (token->location, "-fcilkplus must be enabled to use"
9666 " %<_Cilk_sync%>");
9667 statement = error_mark_node;
9669 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9670 break;
9672 /* Objective-C++ exception-handling constructs. */
9673 case RID_AT_TRY:
9674 case RID_AT_CATCH:
9675 case RID_AT_FINALLY:
9676 case RID_AT_SYNCHRONIZED:
9677 case RID_AT_THROW:
9678 statement = cp_parser_objc_statement (parser);
9679 break;
9681 case RID_TRY:
9682 statement = cp_parser_try_block (parser);
9683 break;
9685 case RID_NAMESPACE:
9686 /* This must be a namespace alias definition. */
9687 cp_parser_declaration_statement (parser);
9688 return;
9690 case RID_TRANSACTION_ATOMIC:
9691 case RID_TRANSACTION_RELAXED:
9692 statement = cp_parser_transaction (parser, keyword);
9693 break;
9694 case RID_TRANSACTION_CANCEL:
9695 statement = cp_parser_transaction_cancel (parser);
9696 break;
9698 default:
9699 /* It might be a keyword like `int' that can start a
9700 declaration-statement. */
9701 break;
9704 else if (token->type == CPP_NAME)
9706 /* If the next token is a `:', then we are looking at a
9707 labeled-statement. */
9708 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9709 if (token->type == CPP_COLON)
9711 /* Looks like a labeled-statement with an ordinary label.
9712 Parse the label, and then use tail recursion to parse
9713 the statement. */
9715 cp_parser_label_for_labeled_statement (parser, std_attrs);
9716 goto restart;
9719 /* Anything that starts with a `{' must be a compound-statement. */
9720 else if (token->type == CPP_OPEN_BRACE)
9721 statement = cp_parser_compound_statement (parser, NULL, false, false);
9722 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9723 a statement all its own. */
9724 else if (token->type == CPP_PRAGMA)
9726 /* Only certain OpenMP pragmas are attached to statements, and thus
9727 are considered statements themselves. All others are not. In
9728 the context of a compound, accept the pragma as a "statement" and
9729 return so that we can check for a close brace. Otherwise we
9730 require a real statement and must go back and read one. */
9731 if (in_compound)
9732 cp_parser_pragma (parser, pragma_compound);
9733 else if (!cp_parser_pragma (parser, pragma_stmt))
9734 goto restart;
9735 return;
9737 else if (token->type == CPP_EOF)
9739 cp_parser_error (parser, "expected statement");
9740 return;
9743 /* Everything else must be a declaration-statement or an
9744 expression-statement. Try for the declaration-statement
9745 first, unless we are looking at a `;', in which case we know that
9746 we have an expression-statement. */
9747 if (!statement)
9749 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9751 if (std_attrs != NULL_TREE)
9753 /* Attributes should be parsed as part of the the
9754 declaration, so let's un-parse them. */
9755 saved_tokens.rollback();
9756 std_attrs = NULL_TREE;
9759 cp_parser_parse_tentatively (parser);
9760 /* Try to parse the declaration-statement. */
9761 cp_parser_declaration_statement (parser);
9762 /* If that worked, we're done. */
9763 if (cp_parser_parse_definitely (parser))
9764 return;
9766 /* Look for an expression-statement instead. */
9767 statement = cp_parser_expression_statement (parser, in_statement_expr);
9770 /* Set the line number for the statement. */
9771 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9772 SET_EXPR_LOCATION (statement, statement_location);
9774 /* Note that for now, we don't do anything with c++11 statements
9775 parsed at this level. */
9776 if (std_attrs != NULL_TREE)
9777 warning_at (attrs_location,
9778 OPT_Wattributes,
9779 "attributes at the beginning of statement are ignored");
9782 /* Parse the label for a labeled-statement, i.e.
9784 identifier :
9785 case constant-expression :
9786 default :
9788 GNU Extension:
9789 case constant-expression ... constant-expression : statement
9791 When a label is parsed without errors, the label is added to the
9792 parse tree by the finish_* functions, so this function doesn't
9793 have to return the label. */
9795 static void
9796 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9798 cp_token *token;
9799 tree label = NULL_TREE;
9800 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9802 /* The next token should be an identifier. */
9803 token = cp_lexer_peek_token (parser->lexer);
9804 if (token->type != CPP_NAME
9805 && token->type != CPP_KEYWORD)
9807 cp_parser_error (parser, "expected labeled-statement");
9808 return;
9811 parser->colon_corrects_to_scope_p = false;
9812 switch (token->keyword)
9814 case RID_CASE:
9816 tree expr, expr_hi;
9817 cp_token *ellipsis;
9819 /* Consume the `case' token. */
9820 cp_lexer_consume_token (parser->lexer);
9821 /* Parse the constant-expression. */
9822 expr = cp_parser_constant_expression (parser);
9824 ellipsis = cp_lexer_peek_token (parser->lexer);
9825 if (ellipsis->type == CPP_ELLIPSIS)
9827 /* Consume the `...' token. */
9828 cp_lexer_consume_token (parser->lexer);
9829 expr_hi =
9830 cp_parser_constant_expression (parser);
9832 /* We don't need to emit warnings here, as the common code
9833 will do this for us. */
9835 else
9836 expr_hi = NULL_TREE;
9838 if (parser->in_switch_statement_p)
9839 finish_case_label (token->location, expr, expr_hi);
9840 else
9841 error_at (token->location,
9842 "case label %qE not within a switch statement",
9843 expr);
9845 break;
9847 case RID_DEFAULT:
9848 /* Consume the `default' token. */
9849 cp_lexer_consume_token (parser->lexer);
9851 if (parser->in_switch_statement_p)
9852 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9853 else
9854 error_at (token->location, "case label not within a switch statement");
9855 break;
9857 default:
9858 /* Anything else must be an ordinary label. */
9859 label = finish_label_stmt (cp_parser_identifier (parser));
9860 break;
9863 /* Require the `:' token. */
9864 cp_parser_require (parser, CPP_COLON, RT_COLON);
9866 /* An ordinary label may optionally be followed by attributes.
9867 However, this is only permitted if the attributes are then
9868 followed by a semicolon. This is because, for backward
9869 compatibility, when parsing
9870 lab: __attribute__ ((unused)) int i;
9871 we want the attribute to attach to "i", not "lab". */
9872 if (label != NULL_TREE
9873 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9875 tree attrs;
9876 cp_parser_parse_tentatively (parser);
9877 attrs = cp_parser_gnu_attributes_opt (parser);
9878 if (attrs == NULL_TREE
9879 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9880 cp_parser_abort_tentative_parse (parser);
9881 else if (!cp_parser_parse_definitely (parser))
9883 else
9884 attributes = chainon (attributes, attrs);
9887 if (attributes != NULL_TREE)
9888 cplus_decl_attributes (&label, attributes, 0);
9890 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9893 /* Parse an expression-statement.
9895 expression-statement:
9896 expression [opt] ;
9898 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9899 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9900 indicates whether this expression-statement is part of an
9901 expression statement. */
9903 static tree
9904 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9906 tree statement = NULL_TREE;
9907 cp_token *token = cp_lexer_peek_token (parser->lexer);
9909 /* If the next token is a ';', then there is no expression
9910 statement. */
9911 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9913 statement = cp_parser_expression (parser);
9914 if (statement == error_mark_node
9915 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9917 cp_parser_skip_to_end_of_block_or_statement (parser);
9918 return error_mark_node;
9922 /* Give a helpful message for "A<T>::type t;" and the like. */
9923 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9924 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9926 if (TREE_CODE (statement) == SCOPE_REF)
9927 error_at (token->location, "need %<typename%> before %qE because "
9928 "%qT is a dependent scope",
9929 statement, TREE_OPERAND (statement, 0));
9930 else if (is_overloaded_fn (statement)
9931 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9933 /* A::A a; */
9934 tree fn = get_first_fn (statement);
9935 error_at (token->location,
9936 "%<%T::%D%> names the constructor, not the type",
9937 DECL_CONTEXT (fn), DECL_NAME (fn));
9941 /* Consume the final `;'. */
9942 cp_parser_consume_semicolon_at_end_of_statement (parser);
9944 if (in_statement_expr
9945 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9946 /* This is the final expression statement of a statement
9947 expression. */
9948 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9949 else if (statement)
9950 statement = finish_expr_stmt (statement);
9952 return statement;
9955 /* Parse a compound-statement.
9957 compound-statement:
9958 { statement-seq [opt] }
9960 GNU extension:
9962 compound-statement:
9963 { label-declaration-seq [opt] statement-seq [opt] }
9965 label-declaration-seq:
9966 label-declaration
9967 label-declaration-seq label-declaration
9969 Returns a tree representing the statement. */
9971 static tree
9972 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9973 bool in_try, bool function_body)
9975 tree compound_stmt;
9977 /* Consume the `{'. */
9978 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9979 return error_mark_node;
9980 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9981 && !function_body && cxx_dialect < cxx14)
9982 pedwarn (input_location, OPT_Wpedantic,
9983 "compound-statement in constexpr function");
9984 /* Begin the compound-statement. */
9985 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9986 /* If the next keyword is `__label__' we have a label declaration. */
9987 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9988 cp_parser_label_declaration (parser);
9989 /* Parse an (optional) statement-seq. */
9990 cp_parser_statement_seq_opt (parser, in_statement_expr);
9991 /* Finish the compound-statement. */
9992 finish_compound_stmt (compound_stmt);
9993 /* Consume the `}'. */
9994 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9996 return compound_stmt;
9999 /* Parse an (optional) statement-seq.
10001 statement-seq:
10002 statement
10003 statement-seq [opt] statement */
10005 static void
10006 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10008 /* Scan statements until there aren't any more. */
10009 while (true)
10011 cp_token *token = cp_lexer_peek_token (parser->lexer);
10013 /* If we are looking at a `}', then we have run out of
10014 statements; the same is true if we have reached the end
10015 of file, or have stumbled upon a stray '@end'. */
10016 if (token->type == CPP_CLOSE_BRACE
10017 || token->type == CPP_EOF
10018 || token->type == CPP_PRAGMA_EOL
10019 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10020 break;
10022 /* If we are in a compound statement and find 'else' then
10023 something went wrong. */
10024 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10026 if (parser->in_statement & IN_IF_STMT)
10027 break;
10028 else
10030 token = cp_lexer_consume_token (parser->lexer);
10031 error_at (token->location, "%<else%> without a previous %<if%>");
10035 /* Parse the statement. */
10036 cp_parser_statement (parser, in_statement_expr, true, NULL);
10040 /* Parse a selection-statement.
10042 selection-statement:
10043 if ( condition ) statement
10044 if ( condition ) statement else statement
10045 switch ( condition ) statement
10047 Returns the new IF_STMT or SWITCH_STMT.
10049 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10050 is a (possibly labeled) if statement which is not enclosed in
10051 braces and has an else clause. This is used to implement
10052 -Wparentheses. */
10054 static tree
10055 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10057 cp_token *token;
10058 enum rid keyword;
10060 if (if_p != NULL)
10061 *if_p = false;
10063 /* Peek at the next token. */
10064 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10066 /* See what kind of keyword it is. */
10067 keyword = token->keyword;
10068 switch (keyword)
10070 case RID_IF:
10071 case RID_SWITCH:
10073 tree statement;
10074 tree condition;
10076 /* Look for the `('. */
10077 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10079 cp_parser_skip_to_end_of_statement (parser);
10080 return error_mark_node;
10083 /* Begin the selection-statement. */
10084 if (keyword == RID_IF)
10085 statement = begin_if_stmt ();
10086 else
10087 statement = begin_switch_stmt ();
10089 /* Parse the condition. */
10090 condition = cp_parser_condition (parser);
10091 /* Look for the `)'. */
10092 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10093 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10094 /*consume_paren=*/true);
10096 if (keyword == RID_IF)
10098 bool nested_if;
10099 unsigned char in_statement;
10101 /* Add the condition. */
10102 finish_if_stmt_cond (condition, statement);
10104 /* Parse the then-clause. */
10105 in_statement = parser->in_statement;
10106 parser->in_statement |= IN_IF_STMT;
10107 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10109 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10110 add_stmt (build_empty_stmt (loc));
10111 cp_lexer_consume_token (parser->lexer);
10112 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10113 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10114 "empty body in an %<if%> statement");
10115 nested_if = false;
10117 else
10118 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10119 parser->in_statement = in_statement;
10121 finish_then_clause (statement);
10123 /* If the next token is `else', parse the else-clause. */
10124 if (cp_lexer_next_token_is_keyword (parser->lexer,
10125 RID_ELSE))
10127 /* Consume the `else' keyword. */
10128 cp_lexer_consume_token (parser->lexer);
10129 begin_else_clause (statement);
10130 /* Parse the else-clause. */
10131 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10133 location_t loc;
10134 loc = cp_lexer_peek_token (parser->lexer)->location;
10135 warning_at (loc,
10136 OPT_Wempty_body, "suggest braces around "
10137 "empty body in an %<else%> statement");
10138 add_stmt (build_empty_stmt (loc));
10139 cp_lexer_consume_token (parser->lexer);
10141 else
10142 cp_parser_implicitly_scoped_statement (parser, NULL);
10144 finish_else_clause (statement);
10146 /* If we are currently parsing a then-clause, then
10147 IF_P will not be NULL. We set it to true to
10148 indicate that this if statement has an else clause.
10149 This may trigger the Wparentheses warning below
10150 when we get back up to the parent if statement. */
10151 if (if_p != NULL)
10152 *if_p = true;
10154 else
10156 /* This if statement does not have an else clause. If
10157 NESTED_IF is true, then the then-clause is an if
10158 statement which does have an else clause. We warn
10159 about the potential ambiguity. */
10160 if (nested_if)
10161 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10162 "suggest explicit braces to avoid ambiguous"
10163 " %<else%>");
10166 /* Now we're all done with the if-statement. */
10167 finish_if_stmt (statement);
10169 else
10171 bool in_switch_statement_p;
10172 unsigned char in_statement;
10174 /* Add the condition. */
10175 finish_switch_cond (condition, statement);
10177 /* Parse the body of the switch-statement. */
10178 in_switch_statement_p = parser->in_switch_statement_p;
10179 in_statement = parser->in_statement;
10180 parser->in_switch_statement_p = true;
10181 parser->in_statement |= IN_SWITCH_STMT;
10182 cp_parser_implicitly_scoped_statement (parser, NULL);
10183 parser->in_switch_statement_p = in_switch_statement_p;
10184 parser->in_statement = in_statement;
10186 /* Now we're all done with the switch-statement. */
10187 finish_switch_stmt (statement);
10190 return statement;
10192 break;
10194 default:
10195 cp_parser_error (parser, "expected selection-statement");
10196 return error_mark_node;
10200 /* Parse a condition.
10202 condition:
10203 expression
10204 type-specifier-seq declarator = initializer-clause
10205 type-specifier-seq declarator braced-init-list
10207 GNU Extension:
10209 condition:
10210 type-specifier-seq declarator asm-specification [opt]
10211 attributes [opt] = assignment-expression
10213 Returns the expression that should be tested. */
10215 static tree
10216 cp_parser_condition (cp_parser* parser)
10218 cp_decl_specifier_seq type_specifiers;
10219 const char *saved_message;
10220 int declares_class_or_enum;
10222 /* Try the declaration first. */
10223 cp_parser_parse_tentatively (parser);
10224 /* New types are not allowed in the type-specifier-seq for a
10225 condition. */
10226 saved_message = parser->type_definition_forbidden_message;
10227 parser->type_definition_forbidden_message
10228 = G_("types may not be defined in conditions");
10229 /* Parse the type-specifier-seq. */
10230 cp_parser_decl_specifier_seq (parser,
10231 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10232 &type_specifiers,
10233 &declares_class_or_enum);
10234 /* Restore the saved message. */
10235 parser->type_definition_forbidden_message = saved_message;
10236 /* If all is well, we might be looking at a declaration. */
10237 if (!cp_parser_error_occurred (parser))
10239 tree decl;
10240 tree asm_specification;
10241 tree attributes;
10242 cp_declarator *declarator;
10243 tree initializer = NULL_TREE;
10245 /* Parse the declarator. */
10246 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10247 /*ctor_dtor_or_conv_p=*/NULL,
10248 /*parenthesized_p=*/NULL,
10249 /*member_p=*/false,
10250 /*friend_p=*/false);
10251 /* Parse the attributes. */
10252 attributes = cp_parser_attributes_opt (parser);
10253 /* Parse the asm-specification. */
10254 asm_specification = cp_parser_asm_specification_opt (parser);
10255 /* If the next token is not an `=' or '{', then we might still be
10256 looking at an expression. For example:
10258 if (A(a).x)
10260 looks like a decl-specifier-seq and a declarator -- but then
10261 there is no `=', so this is an expression. */
10262 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10263 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10264 cp_parser_simulate_error (parser);
10266 /* If we did see an `=' or '{', then we are looking at a declaration
10267 for sure. */
10268 if (cp_parser_parse_definitely (parser))
10270 tree pushed_scope;
10271 bool non_constant_p;
10272 bool flags = LOOKUP_ONLYCONVERTING;
10274 /* Create the declaration. */
10275 decl = start_decl (declarator, &type_specifiers,
10276 /*initialized_p=*/true,
10277 attributes, /*prefix_attributes=*/NULL_TREE,
10278 &pushed_scope);
10280 /* Parse the initializer. */
10281 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10283 initializer = cp_parser_braced_list (parser, &non_constant_p);
10284 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10285 flags = 0;
10287 else
10289 /* Consume the `='. */
10290 cp_parser_require (parser, CPP_EQ, RT_EQ);
10291 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10293 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10294 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10296 /* Process the initializer. */
10297 cp_finish_decl (decl,
10298 initializer, !non_constant_p,
10299 asm_specification,
10300 flags);
10302 if (pushed_scope)
10303 pop_scope (pushed_scope);
10305 return convert_from_reference (decl);
10308 /* If we didn't even get past the declarator successfully, we are
10309 definitely not looking at a declaration. */
10310 else
10311 cp_parser_abort_tentative_parse (parser);
10313 /* Otherwise, we are looking at an expression. */
10314 return cp_parser_expression (parser);
10317 /* Parses a for-statement or range-for-statement until the closing ')',
10318 not included. */
10320 static tree
10321 cp_parser_for (cp_parser *parser, bool ivdep)
10323 tree init, scope, decl;
10324 bool is_range_for;
10326 /* Begin the for-statement. */
10327 scope = begin_for_scope (&init);
10329 /* Parse the initialization. */
10330 is_range_for = cp_parser_for_init_statement (parser, &decl);
10332 if (is_range_for)
10333 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10334 else
10335 return cp_parser_c_for (parser, scope, init, ivdep);
10338 static tree
10339 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10341 /* Normal for loop */
10342 tree condition = NULL_TREE;
10343 tree expression = NULL_TREE;
10344 tree stmt;
10346 stmt = begin_for_stmt (scope, init);
10347 /* The for-init-statement has already been parsed in
10348 cp_parser_for_init_statement, so no work is needed here. */
10349 finish_for_init_stmt (stmt);
10351 /* If there's a condition, process it. */
10352 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10353 condition = cp_parser_condition (parser);
10354 else if (ivdep)
10356 cp_parser_error (parser, "missing loop condition in loop with "
10357 "%<GCC ivdep%> pragma");
10358 condition = error_mark_node;
10360 finish_for_cond (condition, stmt, ivdep);
10361 /* Look for the `;'. */
10362 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10364 /* If there's an expression, process it. */
10365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10366 expression = cp_parser_expression (parser);
10367 finish_for_expr (expression, stmt);
10369 return stmt;
10372 /* Tries to parse a range-based for-statement:
10374 range-based-for:
10375 decl-specifier-seq declarator : expression
10377 The decl-specifier-seq declarator and the `:' are already parsed by
10378 cp_parser_for_init_statement. If processing_template_decl it returns a
10379 newly created RANGE_FOR_STMT; if not, it is converted to a
10380 regular FOR_STMT. */
10382 static tree
10383 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10384 bool ivdep)
10386 tree stmt, range_expr;
10388 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10390 bool expr_non_constant_p;
10391 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10393 else
10394 range_expr = cp_parser_expression (parser);
10396 /* If in template, STMT is converted to a normal for-statement
10397 at instantiation. If not, it is done just ahead. */
10398 if (processing_template_decl)
10400 if (check_for_bare_parameter_packs (range_expr))
10401 range_expr = error_mark_node;
10402 stmt = begin_range_for_stmt (scope, init);
10403 if (ivdep)
10404 RANGE_FOR_IVDEP (stmt) = 1;
10405 finish_range_for_decl (stmt, range_decl, range_expr);
10406 if (!type_dependent_expression_p (range_expr)
10407 /* do_auto_deduction doesn't mess with template init-lists. */
10408 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10409 do_range_for_auto_deduction (range_decl, range_expr);
10411 else
10413 stmt = begin_for_stmt (scope, init);
10414 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10416 return stmt;
10419 /* Subroutine of cp_convert_range_for: given the initializer expression,
10420 builds up the range temporary. */
10422 static tree
10423 build_range_temp (tree range_expr)
10425 tree range_type, range_temp;
10427 /* Find out the type deduced by the declaration
10428 `auto &&__range = range_expr'. */
10429 range_type = cp_build_reference_type (make_auto (), true);
10430 range_type = do_auto_deduction (range_type, range_expr,
10431 type_uses_auto (range_type));
10433 /* Create the __range variable. */
10434 range_temp = build_decl (input_location, VAR_DECL,
10435 get_identifier ("__for_range"), range_type);
10436 TREE_USED (range_temp) = 1;
10437 DECL_ARTIFICIAL (range_temp) = 1;
10439 return range_temp;
10442 /* Used by cp_parser_range_for in template context: we aren't going to
10443 do a full conversion yet, but we still need to resolve auto in the
10444 type of the for-range-declaration if present. This is basically
10445 a shortcut version of cp_convert_range_for. */
10447 static void
10448 do_range_for_auto_deduction (tree decl, tree range_expr)
10450 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10451 if (auto_node)
10453 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10454 range_temp = convert_from_reference (build_range_temp (range_expr));
10455 iter_type = (cp_parser_perform_range_for_lookup
10456 (range_temp, &begin_dummy, &end_dummy));
10457 if (iter_type)
10459 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10460 iter_type);
10461 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10462 tf_warning_or_error);
10463 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10464 iter_decl, auto_node);
10469 /* Converts a range-based for-statement into a normal
10470 for-statement, as per the definition.
10472 for (RANGE_DECL : RANGE_EXPR)
10473 BLOCK
10475 should be equivalent to:
10478 auto &&__range = RANGE_EXPR;
10479 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10480 __begin != __end;
10481 ++__begin)
10483 RANGE_DECL = *__begin;
10484 BLOCK
10488 If RANGE_EXPR is an array:
10489 BEGIN_EXPR = __range
10490 END_EXPR = __range + ARRAY_SIZE(__range)
10491 Else if RANGE_EXPR has a member 'begin' or 'end':
10492 BEGIN_EXPR = __range.begin()
10493 END_EXPR = __range.end()
10494 Else:
10495 BEGIN_EXPR = begin(__range)
10496 END_EXPR = end(__range);
10498 If __range has a member 'begin' but not 'end', or vice versa, we must
10499 still use the second alternative (it will surely fail, however).
10500 When calling begin()/end() in the third alternative we must use
10501 argument dependent lookup, but always considering 'std' as an associated
10502 namespace. */
10504 tree
10505 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10506 bool ivdep)
10508 tree begin, end;
10509 tree iter_type, begin_expr, end_expr;
10510 tree condition, expression;
10512 if (range_decl == error_mark_node || range_expr == error_mark_node)
10513 /* If an error happened previously do nothing or else a lot of
10514 unhelpful errors would be issued. */
10515 begin_expr = end_expr = iter_type = error_mark_node;
10516 else
10518 tree range_temp;
10520 if (TREE_CODE (range_expr) == VAR_DECL
10521 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10522 /* Can't bind a reference to an array of runtime bound. */
10523 range_temp = range_expr;
10524 else
10526 range_temp = build_range_temp (range_expr);
10527 pushdecl (range_temp);
10528 cp_finish_decl (range_temp, range_expr,
10529 /*is_constant_init*/false, NULL_TREE,
10530 LOOKUP_ONLYCONVERTING);
10531 range_temp = convert_from_reference (range_temp);
10533 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10534 &begin_expr, &end_expr);
10537 /* The new for initialization statement. */
10538 begin = build_decl (input_location, VAR_DECL,
10539 get_identifier ("__for_begin"), iter_type);
10540 TREE_USED (begin) = 1;
10541 DECL_ARTIFICIAL (begin) = 1;
10542 pushdecl (begin);
10543 cp_finish_decl (begin, begin_expr,
10544 /*is_constant_init*/false, NULL_TREE,
10545 LOOKUP_ONLYCONVERTING);
10547 end = build_decl (input_location, VAR_DECL,
10548 get_identifier ("__for_end"), iter_type);
10549 TREE_USED (end) = 1;
10550 DECL_ARTIFICIAL (end) = 1;
10551 pushdecl (end);
10552 cp_finish_decl (end, end_expr,
10553 /*is_constant_init*/false, NULL_TREE,
10554 LOOKUP_ONLYCONVERTING);
10556 finish_for_init_stmt (statement);
10558 /* The new for condition. */
10559 condition = build_x_binary_op (input_location, NE_EXPR,
10560 begin, ERROR_MARK,
10561 end, ERROR_MARK,
10562 NULL, tf_warning_or_error);
10563 finish_for_cond (condition, statement, ivdep);
10565 /* The new increment expression. */
10566 expression = finish_unary_op_expr (input_location,
10567 PREINCREMENT_EXPR, begin,
10568 tf_warning_or_error);
10569 finish_for_expr (expression, statement);
10571 /* The declaration is initialized with *__begin inside the loop body. */
10572 cp_finish_decl (range_decl,
10573 build_x_indirect_ref (input_location, begin, RO_NULL,
10574 tf_warning_or_error),
10575 /*is_constant_init*/false, NULL_TREE,
10576 LOOKUP_ONLYCONVERTING);
10578 return statement;
10581 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10582 We need to solve both at the same time because the method used
10583 depends on the existence of members begin or end.
10584 Returns the type deduced for the iterator expression. */
10586 static tree
10587 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10589 if (error_operand_p (range))
10591 *begin = *end = error_mark_node;
10592 return error_mark_node;
10595 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10597 error ("range-based %<for%> expression of type %qT "
10598 "has incomplete type", TREE_TYPE (range));
10599 *begin = *end = error_mark_node;
10600 return error_mark_node;
10602 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10604 /* If RANGE is an array, we will use pointer arithmetic. */
10605 *begin = range;
10606 *end = build_binary_op (input_location, PLUS_EXPR,
10607 range,
10608 array_type_nelts_top (TREE_TYPE (range)),
10610 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10612 else
10614 /* If it is not an array, we must do a bit of magic. */
10615 tree id_begin, id_end;
10616 tree member_begin, member_end;
10618 *begin = *end = error_mark_node;
10620 id_begin = get_identifier ("begin");
10621 id_end = get_identifier ("end");
10622 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10623 /*protect=*/2, /*want_type=*/false,
10624 tf_warning_or_error);
10625 member_end = lookup_member (TREE_TYPE (range), id_end,
10626 /*protect=*/2, /*want_type=*/false,
10627 tf_warning_or_error);
10629 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10631 /* Use the member functions. */
10632 if (member_begin != NULL_TREE)
10633 *begin = cp_parser_range_for_member_function (range, id_begin);
10634 else
10635 error ("range-based %<for%> expression of type %qT has an "
10636 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10638 if (member_end != NULL_TREE)
10639 *end = cp_parser_range_for_member_function (range, id_end);
10640 else
10641 error ("range-based %<for%> expression of type %qT has a "
10642 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10644 else
10646 /* Use global functions with ADL. */
10647 vec<tree, va_gc> *vec;
10648 vec = make_tree_vector ();
10650 vec_safe_push (vec, range);
10652 member_begin = perform_koenig_lookup (id_begin, vec,
10653 tf_warning_or_error);
10654 *begin = finish_call_expr (member_begin, &vec, false, true,
10655 tf_warning_or_error);
10656 member_end = perform_koenig_lookup (id_end, vec,
10657 tf_warning_or_error);
10658 *end = finish_call_expr (member_end, &vec, false, true,
10659 tf_warning_or_error);
10661 release_tree_vector (vec);
10664 /* Last common checks. */
10665 if (*begin == error_mark_node || *end == error_mark_node)
10667 /* If one of the expressions is an error do no more checks. */
10668 *begin = *end = error_mark_node;
10669 return error_mark_node;
10671 else if (type_dependent_expression_p (*begin)
10672 || type_dependent_expression_p (*end))
10673 /* Can happen, when, eg, in a template context, Koenig lookup
10674 can't resolve begin/end (c++/58503). */
10675 return NULL_TREE;
10676 else
10678 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10679 /* The unqualified type of the __begin and __end temporaries should
10680 be the same, as required by the multiple auto declaration. */
10681 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10682 error ("inconsistent begin/end types in range-based %<for%> "
10683 "statement: %qT and %qT",
10684 TREE_TYPE (*begin), TREE_TYPE (*end));
10685 return iter_type;
10690 /* Helper function for cp_parser_perform_range_for_lookup.
10691 Builds a tree for RANGE.IDENTIFIER(). */
10693 static tree
10694 cp_parser_range_for_member_function (tree range, tree identifier)
10696 tree member, res;
10697 vec<tree, va_gc> *vec;
10699 member = finish_class_member_access_expr (range, identifier,
10700 false, tf_warning_or_error);
10701 if (member == error_mark_node)
10702 return error_mark_node;
10704 vec = make_tree_vector ();
10705 res = finish_call_expr (member, &vec,
10706 /*disallow_virtual=*/false,
10707 /*koenig_p=*/false,
10708 tf_warning_or_error);
10709 release_tree_vector (vec);
10710 return res;
10713 /* Parse an iteration-statement.
10715 iteration-statement:
10716 while ( condition ) statement
10717 do statement while ( expression ) ;
10718 for ( for-init-statement condition [opt] ; expression [opt] )
10719 statement
10721 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10723 static tree
10724 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10726 cp_token *token;
10727 enum rid keyword;
10728 tree statement;
10729 unsigned char in_statement;
10731 /* Peek at the next token. */
10732 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10733 if (!token)
10734 return error_mark_node;
10736 /* Remember whether or not we are already within an iteration
10737 statement. */
10738 in_statement = parser->in_statement;
10740 /* See what kind of keyword it is. */
10741 keyword = token->keyword;
10742 switch (keyword)
10744 case RID_WHILE:
10746 tree condition;
10748 /* Begin the while-statement. */
10749 statement = begin_while_stmt ();
10750 /* Look for the `('. */
10751 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10752 /* Parse the condition. */
10753 condition = cp_parser_condition (parser);
10754 finish_while_stmt_cond (condition, statement, ivdep);
10755 /* Look for the `)'. */
10756 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10757 /* Parse the dependent statement. */
10758 parser->in_statement = IN_ITERATION_STMT;
10759 cp_parser_already_scoped_statement (parser);
10760 parser->in_statement = in_statement;
10761 /* We're done with the while-statement. */
10762 finish_while_stmt (statement);
10764 break;
10766 case RID_DO:
10768 tree expression;
10770 /* Begin the do-statement. */
10771 statement = begin_do_stmt ();
10772 /* Parse the body of the do-statement. */
10773 parser->in_statement = IN_ITERATION_STMT;
10774 cp_parser_implicitly_scoped_statement (parser, NULL);
10775 parser->in_statement = in_statement;
10776 finish_do_body (statement);
10777 /* Look for the `while' keyword. */
10778 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10779 /* Look for the `('. */
10780 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10781 /* Parse the expression. */
10782 expression = cp_parser_expression (parser);
10783 /* We're done with the do-statement. */
10784 finish_do_stmt (expression, statement, ivdep);
10785 /* Look for the `)'. */
10786 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10787 /* Look for the `;'. */
10788 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10790 break;
10792 case RID_FOR:
10794 /* Look for the `('. */
10795 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10797 statement = cp_parser_for (parser, ivdep);
10799 /* Look for the `)'. */
10800 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10802 /* Parse the body of the for-statement. */
10803 parser->in_statement = IN_ITERATION_STMT;
10804 cp_parser_already_scoped_statement (parser);
10805 parser->in_statement = in_statement;
10807 /* We're done with the for-statement. */
10808 finish_for_stmt (statement);
10810 break;
10812 default:
10813 cp_parser_error (parser, "expected iteration-statement");
10814 statement = error_mark_node;
10815 break;
10818 return statement;
10821 /* Parse a for-init-statement or the declarator of a range-based-for.
10822 Returns true if a range-based-for declaration is seen.
10824 for-init-statement:
10825 expression-statement
10826 simple-declaration */
10828 static bool
10829 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10831 /* If the next token is a `;', then we have an empty
10832 expression-statement. Grammatically, this is also a
10833 simple-declaration, but an invalid one, because it does not
10834 declare anything. Therefore, if we did not handle this case
10835 specially, we would issue an error message about an invalid
10836 declaration. */
10837 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10839 bool is_range_for = false;
10840 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10842 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10843 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10845 /* N3994 -- for (id : init) ... */
10846 if (cxx_dialect < cxx1z)
10847 pedwarn (input_location, 0, "range-based for loop without a "
10848 "type-specifier only available with "
10849 "-std=c++1z or -std=gnu++1z");
10850 tree name = cp_parser_identifier (parser);
10851 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10852 *decl = build_decl (input_location, VAR_DECL, name, type);
10853 pushdecl (*decl);
10854 cp_lexer_consume_token (parser->lexer);
10855 return true;
10858 /* A colon is used in range-based for. */
10859 parser->colon_corrects_to_scope_p = false;
10861 /* We're going to speculatively look for a declaration, falling back
10862 to an expression, if necessary. */
10863 cp_parser_parse_tentatively (parser);
10864 /* Parse the declaration. */
10865 cp_parser_simple_declaration (parser,
10866 /*function_definition_allowed_p=*/false,
10867 decl);
10868 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10869 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10871 /* It is a range-for, consume the ':' */
10872 cp_lexer_consume_token (parser->lexer);
10873 is_range_for = true;
10874 if (cxx_dialect < cxx11)
10876 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10877 "range-based %<for%> loops only available with "
10878 "-std=c++11 or -std=gnu++11");
10879 *decl = error_mark_node;
10882 else
10883 /* The ';' is not consumed yet because we told
10884 cp_parser_simple_declaration not to. */
10885 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10887 if (cp_parser_parse_definitely (parser))
10888 return is_range_for;
10889 /* If the tentative parse failed, then we shall need to look for an
10890 expression-statement. */
10892 /* If we are here, it is an expression-statement. */
10893 cp_parser_expression_statement (parser, NULL_TREE);
10894 return false;
10897 /* Parse a jump-statement.
10899 jump-statement:
10900 break ;
10901 continue ;
10902 return expression [opt] ;
10903 return braced-init-list ;
10904 goto identifier ;
10906 GNU extension:
10908 jump-statement:
10909 goto * expression ;
10911 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10913 static tree
10914 cp_parser_jump_statement (cp_parser* parser)
10916 tree statement = error_mark_node;
10917 cp_token *token;
10918 enum rid keyword;
10919 unsigned char in_statement;
10921 /* Peek at the next token. */
10922 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10923 if (!token)
10924 return error_mark_node;
10926 /* See what kind of keyword it is. */
10927 keyword = token->keyword;
10928 switch (keyword)
10930 case RID_BREAK:
10931 in_statement = parser->in_statement & ~IN_IF_STMT;
10932 switch (in_statement)
10934 case 0:
10935 error_at (token->location, "break statement not within loop or switch");
10936 break;
10937 default:
10938 gcc_assert ((in_statement & IN_SWITCH_STMT)
10939 || in_statement == IN_ITERATION_STMT);
10940 statement = finish_break_stmt ();
10941 if (in_statement == IN_ITERATION_STMT)
10942 break_maybe_infinite_loop ();
10943 break;
10944 case IN_OMP_BLOCK:
10945 error_at (token->location, "invalid exit from OpenMP structured block");
10946 break;
10947 case IN_OMP_FOR:
10948 error_at (token->location, "break statement used with OpenMP for loop");
10949 break;
10950 case IN_CILK_SIMD_FOR:
10951 error_at (token->location, "break statement used with Cilk Plus for loop");
10952 break;
10954 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10955 break;
10957 case RID_CONTINUE:
10958 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10960 case 0:
10961 error_at (token->location, "continue statement not within a loop");
10962 break;
10963 case IN_CILK_SIMD_FOR:
10964 error_at (token->location,
10965 "continue statement within %<#pragma simd%> loop body");
10966 /* Fall through. */
10967 case IN_ITERATION_STMT:
10968 case IN_OMP_FOR:
10969 statement = finish_continue_stmt ();
10970 break;
10971 case IN_OMP_BLOCK:
10972 error_at (token->location, "invalid exit from OpenMP structured block");
10973 break;
10974 default:
10975 gcc_unreachable ();
10977 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10978 break;
10980 case RID_RETURN:
10982 tree expr;
10983 bool expr_non_constant_p;
10985 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10987 cp_lexer_set_source_position (parser->lexer);
10988 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10989 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10991 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10992 expr = cp_parser_expression (parser);
10993 else
10994 /* If the next token is a `;', then there is no
10995 expression. */
10996 expr = NULL_TREE;
10997 /* Build the return-statement. */
10998 statement = finish_return_stmt (expr);
10999 /* Look for the final `;'. */
11000 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11002 break;
11004 case RID_GOTO:
11005 if (parser->in_function_body
11006 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11007 error ("%<goto%> in %<constexpr%> function");
11009 /* Create the goto-statement. */
11010 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11012 /* Issue a warning about this use of a GNU extension. */
11013 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11014 /* Consume the '*' token. */
11015 cp_lexer_consume_token (parser->lexer);
11016 /* Parse the dependent expression. */
11017 finish_goto_stmt (cp_parser_expression (parser));
11019 else
11020 finish_goto_stmt (cp_parser_identifier (parser));
11021 /* Look for the final `;'. */
11022 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11023 break;
11025 default:
11026 cp_parser_error (parser, "expected jump-statement");
11027 break;
11030 return statement;
11033 /* Parse a declaration-statement.
11035 declaration-statement:
11036 block-declaration */
11038 static void
11039 cp_parser_declaration_statement (cp_parser* parser)
11041 void *p;
11043 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11044 p = obstack_alloc (&declarator_obstack, 0);
11046 /* Parse the block-declaration. */
11047 cp_parser_block_declaration (parser, /*statement_p=*/true);
11049 /* Free any declarators allocated. */
11050 obstack_free (&declarator_obstack, p);
11053 /* Some dependent statements (like `if (cond) statement'), are
11054 implicitly in their own scope. In other words, if the statement is
11055 a single statement (as opposed to a compound-statement), it is
11056 none-the-less treated as if it were enclosed in braces. Any
11057 declarations appearing in the dependent statement are out of scope
11058 after control passes that point. This function parses a statement,
11059 but ensures that is in its own scope, even if it is not a
11060 compound-statement.
11062 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11063 is a (possibly labeled) if statement which is not enclosed in
11064 braces and has an else clause. This is used to implement
11065 -Wparentheses.
11067 Returns the new statement. */
11069 static tree
11070 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11072 tree statement;
11074 if (if_p != NULL)
11075 *if_p = false;
11077 /* Mark if () ; with a special NOP_EXPR. */
11078 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11080 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11081 cp_lexer_consume_token (parser->lexer);
11082 statement = add_stmt (build_empty_stmt (loc));
11084 /* if a compound is opened, we simply parse the statement directly. */
11085 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11086 statement = cp_parser_compound_statement (parser, NULL, false, false);
11087 /* If the token is not a `{', then we must take special action. */
11088 else
11090 /* Create a compound-statement. */
11091 statement = begin_compound_stmt (0);
11092 /* Parse the dependent-statement. */
11093 cp_parser_statement (parser, NULL_TREE, false, if_p);
11094 /* Finish the dummy compound-statement. */
11095 finish_compound_stmt (statement);
11098 /* Return the statement. */
11099 return statement;
11102 /* For some dependent statements (like `while (cond) statement'), we
11103 have already created a scope. Therefore, even if the dependent
11104 statement is a compound-statement, we do not want to create another
11105 scope. */
11107 static void
11108 cp_parser_already_scoped_statement (cp_parser* parser)
11110 /* If the token is a `{', then we must take special action. */
11111 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11112 cp_parser_statement (parser, NULL_TREE, false, NULL);
11113 else
11115 /* Avoid calling cp_parser_compound_statement, so that we
11116 don't create a new scope. Do everything else by hand. */
11117 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11118 /* If the next keyword is `__label__' we have a label declaration. */
11119 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11120 cp_parser_label_declaration (parser);
11121 /* Parse an (optional) statement-seq. */
11122 cp_parser_statement_seq_opt (parser, NULL_TREE);
11123 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11127 /* Declarations [gram.dcl.dcl] */
11129 /* Parse an optional declaration-sequence.
11131 declaration-seq:
11132 declaration
11133 declaration-seq declaration */
11135 static void
11136 cp_parser_declaration_seq_opt (cp_parser* parser)
11138 while (true)
11140 cp_token *token;
11142 token = cp_lexer_peek_token (parser->lexer);
11144 if (token->type == CPP_CLOSE_BRACE
11145 || token->type == CPP_EOF
11146 || token->type == CPP_PRAGMA_EOL)
11147 break;
11149 if (token->type == CPP_SEMICOLON)
11151 /* A declaration consisting of a single semicolon is
11152 invalid. Allow it unless we're being pedantic. */
11153 cp_lexer_consume_token (parser->lexer);
11154 if (!in_system_header_at (input_location))
11155 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11156 continue;
11159 /* If we're entering or exiting a region that's implicitly
11160 extern "C", modify the lang context appropriately. */
11161 if (!parser->implicit_extern_c && token->implicit_extern_c)
11163 push_lang_context (lang_name_c);
11164 parser->implicit_extern_c = true;
11166 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11168 pop_lang_context ();
11169 parser->implicit_extern_c = false;
11172 if (token->type == CPP_PRAGMA)
11174 /* A top-level declaration can consist solely of a #pragma.
11175 A nested declaration cannot, so this is done here and not
11176 in cp_parser_declaration. (A #pragma at block scope is
11177 handled in cp_parser_statement.) */
11178 cp_parser_pragma (parser, pragma_external);
11179 continue;
11182 /* Parse the declaration itself. */
11183 cp_parser_declaration (parser);
11187 /* Parse a declaration.
11189 declaration:
11190 block-declaration
11191 function-definition
11192 template-declaration
11193 explicit-instantiation
11194 explicit-specialization
11195 linkage-specification
11196 namespace-definition
11198 GNU extension:
11200 declaration:
11201 __extension__ declaration */
11203 static void
11204 cp_parser_declaration (cp_parser* parser)
11206 cp_token token1;
11207 cp_token token2;
11208 int saved_pedantic;
11209 void *p;
11210 tree attributes = NULL_TREE;
11212 /* Check for the `__extension__' keyword. */
11213 if (cp_parser_extension_opt (parser, &saved_pedantic))
11215 /* Parse the qualified declaration. */
11216 cp_parser_declaration (parser);
11217 /* Restore the PEDANTIC flag. */
11218 pedantic = saved_pedantic;
11220 return;
11223 /* Try to figure out what kind of declaration is present. */
11224 token1 = *cp_lexer_peek_token (parser->lexer);
11226 if (token1.type != CPP_EOF)
11227 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11228 else
11230 token2.type = CPP_EOF;
11231 token2.keyword = RID_MAX;
11234 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11235 p = obstack_alloc (&declarator_obstack, 0);
11237 /* If the next token is `extern' and the following token is a string
11238 literal, then we have a linkage specification. */
11239 if (token1.keyword == RID_EXTERN
11240 && cp_parser_is_pure_string_literal (&token2))
11241 cp_parser_linkage_specification (parser);
11242 /* If the next token is `template', then we have either a template
11243 declaration, an explicit instantiation, or an explicit
11244 specialization. */
11245 else if (token1.keyword == RID_TEMPLATE)
11247 /* `template <>' indicates a template specialization. */
11248 if (token2.type == CPP_LESS
11249 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11250 cp_parser_explicit_specialization (parser);
11251 /* `template <' indicates a template declaration. */
11252 else if (token2.type == CPP_LESS)
11253 cp_parser_template_declaration (parser, /*member_p=*/false);
11254 /* Anything else must be an explicit instantiation. */
11255 else
11256 cp_parser_explicit_instantiation (parser);
11258 /* If the next token is `export', then we have a template
11259 declaration. */
11260 else if (token1.keyword == RID_EXPORT)
11261 cp_parser_template_declaration (parser, /*member_p=*/false);
11262 /* If the next token is `extern', 'static' or 'inline' and the one
11263 after that is `template', we have a GNU extended explicit
11264 instantiation directive. */
11265 else if (cp_parser_allow_gnu_extensions_p (parser)
11266 && (token1.keyword == RID_EXTERN
11267 || token1.keyword == RID_STATIC
11268 || token1.keyword == RID_INLINE)
11269 && token2.keyword == RID_TEMPLATE)
11270 cp_parser_explicit_instantiation (parser);
11271 /* If the next token is `namespace', check for a named or unnamed
11272 namespace definition. */
11273 else if (token1.keyword == RID_NAMESPACE
11274 && (/* A named namespace definition. */
11275 (token2.type == CPP_NAME
11276 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11277 != CPP_EQ))
11278 /* An unnamed namespace definition. */
11279 || token2.type == CPP_OPEN_BRACE
11280 || token2.keyword == RID_ATTRIBUTE))
11281 cp_parser_namespace_definition (parser);
11282 /* An inline (associated) namespace definition. */
11283 else if (token1.keyword == RID_INLINE
11284 && token2.keyword == RID_NAMESPACE)
11285 cp_parser_namespace_definition (parser);
11286 /* Objective-C++ declaration/definition. */
11287 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11288 cp_parser_objc_declaration (parser, NULL_TREE);
11289 else if (c_dialect_objc ()
11290 && token1.keyword == RID_ATTRIBUTE
11291 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11292 cp_parser_objc_declaration (parser, attributes);
11293 /* We must have either a block declaration or a function
11294 definition. */
11295 else
11296 /* Try to parse a block-declaration, or a function-definition. */
11297 cp_parser_block_declaration (parser, /*statement_p=*/false);
11299 /* Free any declarators allocated. */
11300 obstack_free (&declarator_obstack, p);
11303 /* Parse a block-declaration.
11305 block-declaration:
11306 simple-declaration
11307 asm-definition
11308 namespace-alias-definition
11309 using-declaration
11310 using-directive
11312 GNU Extension:
11314 block-declaration:
11315 __extension__ block-declaration
11317 C++0x Extension:
11319 block-declaration:
11320 static_assert-declaration
11322 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11323 part of a declaration-statement. */
11325 static void
11326 cp_parser_block_declaration (cp_parser *parser,
11327 bool statement_p)
11329 cp_token *token1;
11330 int saved_pedantic;
11332 /* Check for the `__extension__' keyword. */
11333 if (cp_parser_extension_opt (parser, &saved_pedantic))
11335 /* Parse the qualified declaration. */
11336 cp_parser_block_declaration (parser, statement_p);
11337 /* Restore the PEDANTIC flag. */
11338 pedantic = saved_pedantic;
11340 return;
11343 /* Peek at the next token to figure out which kind of declaration is
11344 present. */
11345 token1 = cp_lexer_peek_token (parser->lexer);
11347 /* If the next keyword is `asm', we have an asm-definition. */
11348 if (token1->keyword == RID_ASM)
11350 if (statement_p)
11351 cp_parser_commit_to_tentative_parse (parser);
11352 cp_parser_asm_definition (parser);
11354 /* If the next keyword is `namespace', we have a
11355 namespace-alias-definition. */
11356 else if (token1->keyword == RID_NAMESPACE)
11357 cp_parser_namespace_alias_definition (parser);
11358 /* If the next keyword is `using', we have a
11359 using-declaration, a using-directive, or an alias-declaration. */
11360 else if (token1->keyword == RID_USING)
11362 cp_token *token2;
11364 if (statement_p)
11365 cp_parser_commit_to_tentative_parse (parser);
11366 /* If the token after `using' is `namespace', then we have a
11367 using-directive. */
11368 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11369 if (token2->keyword == RID_NAMESPACE)
11370 cp_parser_using_directive (parser);
11371 /* If the second token after 'using' is '=', then we have an
11372 alias-declaration. */
11373 else if (cxx_dialect >= cxx11
11374 && token2->type == CPP_NAME
11375 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11376 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11377 cp_parser_alias_declaration (parser);
11378 /* Otherwise, it's a using-declaration. */
11379 else
11380 cp_parser_using_declaration (parser,
11381 /*access_declaration_p=*/false);
11383 /* If the next keyword is `__label__' we have a misplaced label
11384 declaration. */
11385 else if (token1->keyword == RID_LABEL)
11387 cp_lexer_consume_token (parser->lexer);
11388 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11389 cp_parser_skip_to_end_of_statement (parser);
11390 /* If the next token is now a `;', consume it. */
11391 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11392 cp_lexer_consume_token (parser->lexer);
11394 /* If the next token is `static_assert' we have a static assertion. */
11395 else if (token1->keyword == RID_STATIC_ASSERT)
11396 cp_parser_static_assert (parser, /*member_p=*/false);
11397 /* Anything else must be a simple-declaration. */
11398 else
11399 cp_parser_simple_declaration (parser, !statement_p,
11400 /*maybe_range_for_decl*/NULL);
11403 /* Parse a simple-declaration.
11405 simple-declaration:
11406 decl-specifier-seq [opt] init-declarator-list [opt] ;
11408 init-declarator-list:
11409 init-declarator
11410 init-declarator-list , init-declarator
11412 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11413 function-definition as a simple-declaration.
11415 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11416 parsed declaration if it is an uninitialized single declarator not followed
11417 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11418 if present, will not be consumed. */
11420 static void
11421 cp_parser_simple_declaration (cp_parser* parser,
11422 bool function_definition_allowed_p,
11423 tree *maybe_range_for_decl)
11425 cp_decl_specifier_seq decl_specifiers;
11426 int declares_class_or_enum;
11427 bool saw_declarator;
11429 if (maybe_range_for_decl)
11430 *maybe_range_for_decl = NULL_TREE;
11432 /* Defer access checks until we know what is being declared; the
11433 checks for names appearing in the decl-specifier-seq should be
11434 done as if we were in the scope of the thing being declared. */
11435 push_deferring_access_checks (dk_deferred);
11437 /* Parse the decl-specifier-seq. We have to keep track of whether
11438 or not the decl-specifier-seq declares a named class or
11439 enumeration type, since that is the only case in which the
11440 init-declarator-list is allowed to be empty.
11442 [dcl.dcl]
11444 In a simple-declaration, the optional init-declarator-list can be
11445 omitted only when declaring a class or enumeration, that is when
11446 the decl-specifier-seq contains either a class-specifier, an
11447 elaborated-type-specifier, or an enum-specifier. */
11448 cp_parser_decl_specifier_seq (parser,
11449 CP_PARSER_FLAGS_OPTIONAL,
11450 &decl_specifiers,
11451 &declares_class_or_enum);
11452 /* We no longer need to defer access checks. */
11453 stop_deferring_access_checks ();
11455 /* In a block scope, a valid declaration must always have a
11456 decl-specifier-seq. By not trying to parse declarators, we can
11457 resolve the declaration/expression ambiguity more quickly. */
11458 if (!function_definition_allowed_p
11459 && !decl_specifiers.any_specifiers_p)
11461 cp_parser_error (parser, "expected declaration");
11462 goto done;
11465 /* If the next two tokens are both identifiers, the code is
11466 erroneous. The usual cause of this situation is code like:
11468 T t;
11470 where "T" should name a type -- but does not. */
11471 if (!decl_specifiers.any_type_specifiers_p
11472 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11474 /* If parsing tentatively, we should commit; we really are
11475 looking at a declaration. */
11476 cp_parser_commit_to_tentative_parse (parser);
11477 /* Give up. */
11478 goto done;
11481 /* If we have seen at least one decl-specifier, and the next token
11482 is not a parenthesis, then we must be looking at a declaration.
11483 (After "int (" we might be looking at a functional cast.) */
11484 if (decl_specifiers.any_specifiers_p
11485 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11486 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11487 && !cp_parser_error_occurred (parser))
11488 cp_parser_commit_to_tentative_parse (parser);
11490 /* Keep going until we hit the `;' at the end of the simple
11491 declaration. */
11492 saw_declarator = false;
11493 while (cp_lexer_next_token_is_not (parser->lexer,
11494 CPP_SEMICOLON))
11496 cp_token *token;
11497 bool function_definition_p;
11498 tree decl;
11500 if (saw_declarator)
11502 /* If we are processing next declarator, coma is expected */
11503 token = cp_lexer_peek_token (parser->lexer);
11504 gcc_assert (token->type == CPP_COMMA);
11505 cp_lexer_consume_token (parser->lexer);
11506 if (maybe_range_for_decl)
11507 *maybe_range_for_decl = error_mark_node;
11509 else
11510 saw_declarator = true;
11512 /* Parse the init-declarator. */
11513 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11514 /*checks=*/NULL,
11515 function_definition_allowed_p,
11516 /*member_p=*/false,
11517 declares_class_or_enum,
11518 &function_definition_p,
11519 maybe_range_for_decl);
11520 /* If an error occurred while parsing tentatively, exit quickly.
11521 (That usually happens when in the body of a function; each
11522 statement is treated as a declaration-statement until proven
11523 otherwise.) */
11524 if (cp_parser_error_occurred (parser))
11525 goto done;
11526 /* Handle function definitions specially. */
11527 if (function_definition_p)
11529 /* If the next token is a `,', then we are probably
11530 processing something like:
11532 void f() {}, *p;
11534 which is erroneous. */
11535 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11537 cp_token *token = cp_lexer_peek_token (parser->lexer);
11538 error_at (token->location,
11539 "mixing"
11540 " declarations and function-definitions is forbidden");
11542 /* Otherwise, we're done with the list of declarators. */
11543 else
11545 pop_deferring_access_checks ();
11546 return;
11549 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11550 *maybe_range_for_decl = decl;
11551 /* The next token should be either a `,' or a `;'. */
11552 token = cp_lexer_peek_token (parser->lexer);
11553 /* If it's a `,', there are more declarators to come. */
11554 if (token->type == CPP_COMMA)
11555 /* will be consumed next time around */;
11556 /* If it's a `;', we are done. */
11557 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11558 break;
11559 /* Anything else is an error. */
11560 else
11562 /* If we have already issued an error message we don't need
11563 to issue another one. */
11564 if (decl != error_mark_node
11565 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11566 cp_parser_error (parser, "expected %<,%> or %<;%>");
11567 /* Skip tokens until we reach the end of the statement. */
11568 cp_parser_skip_to_end_of_statement (parser);
11569 /* If the next token is now a `;', consume it. */
11570 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11571 cp_lexer_consume_token (parser->lexer);
11572 goto done;
11574 /* After the first time around, a function-definition is not
11575 allowed -- even if it was OK at first. For example:
11577 int i, f() {}
11579 is not valid. */
11580 function_definition_allowed_p = false;
11583 /* Issue an error message if no declarators are present, and the
11584 decl-specifier-seq does not itself declare a class or
11585 enumeration: [dcl.dcl]/3. */
11586 if (!saw_declarator)
11588 if (cp_parser_declares_only_class_p (parser))
11590 if (!declares_class_or_enum
11591 && decl_specifiers.type
11592 && OVERLOAD_TYPE_P (decl_specifiers.type))
11593 /* Ensure an error is issued anyway when finish_decltype_type,
11594 called via cp_parser_decl_specifier_seq, returns a class or
11595 an enumeration (c++/51786). */
11596 decl_specifiers.type = NULL_TREE;
11597 shadow_tag (&decl_specifiers);
11599 /* Perform any deferred access checks. */
11600 perform_deferred_access_checks (tf_warning_or_error);
11603 /* Consume the `;'. */
11604 if (!maybe_range_for_decl)
11605 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11607 done:
11608 pop_deferring_access_checks ();
11611 /* Parse a decl-specifier-seq.
11613 decl-specifier-seq:
11614 decl-specifier-seq [opt] decl-specifier
11615 decl-specifier attribute-specifier-seq [opt] (C++11)
11617 decl-specifier:
11618 storage-class-specifier
11619 type-specifier
11620 function-specifier
11621 friend
11622 typedef
11624 GNU Extension:
11626 decl-specifier:
11627 attributes
11629 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11631 The parser flags FLAGS is used to control type-specifier parsing.
11633 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11634 flags:
11636 1: one of the decl-specifiers is an elaborated-type-specifier
11637 (i.e., a type declaration)
11638 2: one of the decl-specifiers is an enum-specifier or a
11639 class-specifier (i.e., a type definition)
11643 static void
11644 cp_parser_decl_specifier_seq (cp_parser* parser,
11645 cp_parser_flags flags,
11646 cp_decl_specifier_seq *decl_specs,
11647 int* declares_class_or_enum)
11649 bool constructor_possible_p = !parser->in_declarator_p;
11650 bool found_decl_spec = false;
11651 cp_token *start_token = NULL;
11652 cp_decl_spec ds;
11654 /* Clear DECL_SPECS. */
11655 clear_decl_specs (decl_specs);
11657 /* Assume no class or enumeration type is declared. */
11658 *declares_class_or_enum = 0;
11660 /* Keep reading specifiers until there are no more to read. */
11661 while (true)
11663 bool constructor_p;
11664 cp_token *token;
11665 ds = ds_last;
11667 /* Peek at the next token. */
11668 token = cp_lexer_peek_token (parser->lexer);
11670 /* Save the first token of the decl spec list for error
11671 reporting. */
11672 if (!start_token)
11673 start_token = token;
11674 /* Handle attributes. */
11675 if (cp_next_tokens_can_be_attribute_p (parser))
11677 /* Parse the attributes. */
11678 tree attrs = cp_parser_attributes_opt (parser);
11680 /* In a sequence of declaration specifiers, c++11 attributes
11681 appertain to the type that precede them. In that case
11682 [dcl.spec]/1 says:
11684 The attribute-specifier-seq affects the type only for
11685 the declaration it appears in, not other declarations
11686 involving the same type.
11688 But for now let's force the user to position the
11689 attribute either at the beginning of the declaration or
11690 after the declarator-id, which would clearly mean that it
11691 applies to the declarator. */
11692 if (cxx11_attribute_p (attrs))
11694 if (!found_decl_spec)
11695 /* The c++11 attribute is at the beginning of the
11696 declaration. It appertains to the entity being
11697 declared. */;
11698 else
11700 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11702 /* This is an attribute following a
11703 class-specifier. */
11704 if (decl_specs->type_definition_p)
11705 warn_misplaced_attr_for_class_type (token->location,
11706 decl_specs->type);
11707 attrs = NULL_TREE;
11709 else
11711 decl_specs->std_attributes
11712 = chainon (decl_specs->std_attributes,
11713 attrs);
11714 if (decl_specs->locations[ds_std_attribute] == 0)
11715 decl_specs->locations[ds_std_attribute] = token->location;
11717 continue;
11721 decl_specs->attributes
11722 = chainon (decl_specs->attributes,
11723 attrs);
11724 if (decl_specs->locations[ds_attribute] == 0)
11725 decl_specs->locations[ds_attribute] = token->location;
11726 continue;
11728 /* Assume we will find a decl-specifier keyword. */
11729 found_decl_spec = true;
11730 /* If the next token is an appropriate keyword, we can simply
11731 add it to the list. */
11732 switch (token->keyword)
11734 /* decl-specifier:
11735 friend
11736 constexpr */
11737 case RID_FRIEND:
11738 if (!at_class_scope_p ())
11740 error_at (token->location, "%<friend%> used outside of class");
11741 cp_lexer_purge_token (parser->lexer);
11743 else
11745 ds = ds_friend;
11746 /* Consume the token. */
11747 cp_lexer_consume_token (parser->lexer);
11749 break;
11751 case RID_CONSTEXPR:
11752 ds = ds_constexpr;
11753 cp_lexer_consume_token (parser->lexer);
11754 break;
11756 /* function-specifier:
11757 inline
11758 virtual
11759 explicit */
11760 case RID_INLINE:
11761 case RID_VIRTUAL:
11762 case RID_EXPLICIT:
11763 cp_parser_function_specifier_opt (parser, decl_specs);
11764 break;
11766 /* decl-specifier:
11767 typedef */
11768 case RID_TYPEDEF:
11769 ds = ds_typedef;
11770 /* Consume the token. */
11771 cp_lexer_consume_token (parser->lexer);
11772 /* A constructor declarator cannot appear in a typedef. */
11773 constructor_possible_p = false;
11774 /* The "typedef" keyword can only occur in a declaration; we
11775 may as well commit at this point. */
11776 cp_parser_commit_to_tentative_parse (parser);
11778 if (decl_specs->storage_class != sc_none)
11779 decl_specs->conflicting_specifiers_p = true;
11780 break;
11782 /* storage-class-specifier:
11783 auto
11784 register
11785 static
11786 extern
11787 mutable
11789 GNU Extension:
11790 thread */
11791 case RID_AUTO:
11792 if (cxx_dialect == cxx98)
11794 /* Consume the token. */
11795 cp_lexer_consume_token (parser->lexer);
11797 /* Complain about `auto' as a storage specifier, if
11798 we're complaining about C++0x compatibility. */
11799 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11800 " changes meaning in C++11; please remove it");
11802 /* Set the storage class anyway. */
11803 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11804 token);
11806 else
11807 /* C++0x auto type-specifier. */
11808 found_decl_spec = false;
11809 break;
11811 case RID_REGISTER:
11812 case RID_STATIC:
11813 case RID_EXTERN:
11814 case RID_MUTABLE:
11815 /* Consume the token. */
11816 cp_lexer_consume_token (parser->lexer);
11817 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11818 token);
11819 break;
11820 case RID_THREAD:
11821 /* Consume the token. */
11822 ds = ds_thread;
11823 cp_lexer_consume_token (parser->lexer);
11824 break;
11826 default:
11827 /* We did not yet find a decl-specifier yet. */
11828 found_decl_spec = false;
11829 break;
11832 if (found_decl_spec
11833 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11834 && token->keyword != RID_CONSTEXPR)
11835 error ("decl-specifier invalid in condition");
11837 if (ds != ds_last)
11838 set_and_check_decl_spec_loc (decl_specs, ds, token);
11840 /* Constructors are a special case. The `S' in `S()' is not a
11841 decl-specifier; it is the beginning of the declarator. */
11842 constructor_p
11843 = (!found_decl_spec
11844 && constructor_possible_p
11845 && (cp_parser_constructor_declarator_p
11846 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11848 /* If we don't have a DECL_SPEC yet, then we must be looking at
11849 a type-specifier. */
11850 if (!found_decl_spec && !constructor_p)
11852 int decl_spec_declares_class_or_enum;
11853 bool is_cv_qualifier;
11854 tree type_spec;
11856 type_spec
11857 = cp_parser_type_specifier (parser, flags,
11858 decl_specs,
11859 /*is_declaration=*/true,
11860 &decl_spec_declares_class_or_enum,
11861 &is_cv_qualifier);
11862 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11864 /* If this type-specifier referenced a user-defined type
11865 (a typedef, class-name, etc.), then we can't allow any
11866 more such type-specifiers henceforth.
11868 [dcl.spec]
11870 The longest sequence of decl-specifiers that could
11871 possibly be a type name is taken as the
11872 decl-specifier-seq of a declaration. The sequence shall
11873 be self-consistent as described below.
11875 [dcl.type]
11877 As a general rule, at most one type-specifier is allowed
11878 in the complete decl-specifier-seq of a declaration. The
11879 only exceptions are the following:
11881 -- const or volatile can be combined with any other
11882 type-specifier.
11884 -- signed or unsigned can be combined with char, long,
11885 short, or int.
11887 -- ..
11889 Example:
11891 typedef char* Pc;
11892 void g (const int Pc);
11894 Here, Pc is *not* part of the decl-specifier seq; it's
11895 the declarator. Therefore, once we see a type-specifier
11896 (other than a cv-qualifier), we forbid any additional
11897 user-defined types. We *do* still allow things like `int
11898 int' to be considered a decl-specifier-seq, and issue the
11899 error message later. */
11900 if (type_spec && !is_cv_qualifier)
11901 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11902 /* A constructor declarator cannot follow a type-specifier. */
11903 if (type_spec)
11905 constructor_possible_p = false;
11906 found_decl_spec = true;
11907 if (!is_cv_qualifier)
11908 decl_specs->any_type_specifiers_p = true;
11912 /* If we still do not have a DECL_SPEC, then there are no more
11913 decl-specifiers. */
11914 if (!found_decl_spec)
11915 break;
11917 decl_specs->any_specifiers_p = true;
11918 /* After we see one decl-specifier, further decl-specifiers are
11919 always optional. */
11920 flags |= CP_PARSER_FLAGS_OPTIONAL;
11923 /* Don't allow a friend specifier with a class definition. */
11924 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11925 && (*declares_class_or_enum & 2))
11926 error_at (decl_specs->locations[ds_friend],
11927 "class definition may not be declared a friend");
11930 /* Parse an (optional) storage-class-specifier.
11932 storage-class-specifier:
11933 auto
11934 register
11935 static
11936 extern
11937 mutable
11939 GNU Extension:
11941 storage-class-specifier:
11942 thread
11944 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11946 static tree
11947 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11949 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11951 case RID_AUTO:
11952 if (cxx_dialect != cxx98)
11953 return NULL_TREE;
11954 /* Fall through for C++98. */
11956 case RID_REGISTER:
11957 case RID_STATIC:
11958 case RID_EXTERN:
11959 case RID_MUTABLE:
11960 case RID_THREAD:
11961 /* Consume the token. */
11962 return cp_lexer_consume_token (parser->lexer)->u.value;
11964 default:
11965 return NULL_TREE;
11969 /* Parse an (optional) function-specifier.
11971 function-specifier:
11972 inline
11973 virtual
11974 explicit
11976 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11977 Updates DECL_SPECS, if it is non-NULL. */
11979 static tree
11980 cp_parser_function_specifier_opt (cp_parser* parser,
11981 cp_decl_specifier_seq *decl_specs)
11983 cp_token *token = cp_lexer_peek_token (parser->lexer);
11984 switch (token->keyword)
11986 case RID_INLINE:
11987 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11988 break;
11990 case RID_VIRTUAL:
11991 /* 14.5.2.3 [temp.mem]
11993 A member function template shall not be virtual. */
11994 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11995 error_at (token->location, "templates may not be %<virtual%>");
11996 else
11997 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11998 break;
12000 case RID_EXPLICIT:
12001 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12002 break;
12004 default:
12005 return NULL_TREE;
12008 /* Consume the token. */
12009 return cp_lexer_consume_token (parser->lexer)->u.value;
12012 /* Parse a linkage-specification.
12014 linkage-specification:
12015 extern string-literal { declaration-seq [opt] }
12016 extern string-literal declaration */
12018 static void
12019 cp_parser_linkage_specification (cp_parser* parser)
12021 tree linkage;
12023 /* Look for the `extern' keyword. */
12024 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12026 /* Look for the string-literal. */
12027 linkage = cp_parser_string_literal (parser, false, false);
12029 /* Transform the literal into an identifier. If the literal is a
12030 wide-character string, or contains embedded NULs, then we can't
12031 handle it as the user wants. */
12032 if (strlen (TREE_STRING_POINTER (linkage))
12033 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12035 cp_parser_error (parser, "invalid linkage-specification");
12036 /* Assume C++ linkage. */
12037 linkage = lang_name_cplusplus;
12039 else
12040 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12042 /* We're now using the new linkage. */
12043 push_lang_context (linkage);
12045 /* If the next token is a `{', then we're using the first
12046 production. */
12047 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12049 cp_ensure_no_omp_declare_simd (parser);
12051 /* Consume the `{' token. */
12052 cp_lexer_consume_token (parser->lexer);
12053 /* Parse the declarations. */
12054 cp_parser_declaration_seq_opt (parser);
12055 /* Look for the closing `}'. */
12056 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12058 /* Otherwise, there's just one declaration. */
12059 else
12061 bool saved_in_unbraced_linkage_specification_p;
12063 saved_in_unbraced_linkage_specification_p
12064 = parser->in_unbraced_linkage_specification_p;
12065 parser->in_unbraced_linkage_specification_p = true;
12066 cp_parser_declaration (parser);
12067 parser->in_unbraced_linkage_specification_p
12068 = saved_in_unbraced_linkage_specification_p;
12071 /* We're done with the linkage-specification. */
12072 pop_lang_context ();
12075 /* Parse a static_assert-declaration.
12077 static_assert-declaration:
12078 static_assert ( constant-expression , string-literal ) ;
12080 If MEMBER_P, this static_assert is a class member. */
12082 static void
12083 cp_parser_static_assert(cp_parser *parser, bool member_p)
12085 tree condition;
12086 tree message;
12087 cp_token *token;
12088 location_t saved_loc;
12089 bool dummy;
12091 /* Peek at the `static_assert' token so we can keep track of exactly
12092 where the static assertion started. */
12093 token = cp_lexer_peek_token (parser->lexer);
12094 saved_loc = token->location;
12096 /* Look for the `static_assert' keyword. */
12097 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12098 RT_STATIC_ASSERT))
12099 return;
12101 /* We know we are in a static assertion; commit to any tentative
12102 parse. */
12103 if (cp_parser_parsing_tentatively (parser))
12104 cp_parser_commit_to_tentative_parse (parser);
12106 /* Parse the `(' starting the static assertion condition. */
12107 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12109 /* Parse the constant-expression. Allow a non-constant expression
12110 here in order to give better diagnostics in finish_static_assert. */
12111 condition =
12112 cp_parser_constant_expression (parser,
12113 /*allow_non_constant_p=*/true,
12114 /*non_constant_p=*/&dummy);
12116 /* Parse the separating `,'. */
12117 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12119 /* Parse the string-literal message. */
12120 message = cp_parser_string_literal (parser,
12121 /*translate=*/false,
12122 /*wide_ok=*/true);
12124 /* A `)' completes the static assertion. */
12125 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12126 cp_parser_skip_to_closing_parenthesis (parser,
12127 /*recovering=*/true,
12128 /*or_comma=*/false,
12129 /*consume_paren=*/true);
12131 /* A semicolon terminates the declaration. */
12132 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12134 /* Complete the static assertion, which may mean either processing
12135 the static assert now or saving it for template instantiation. */
12136 finish_static_assert (condition, message, saved_loc, member_p);
12139 /* Parse the expression in decltype ( expression ). */
12141 static tree
12142 cp_parser_decltype_expr (cp_parser *parser,
12143 bool &id_expression_or_member_access_p)
12145 cp_token *id_expr_start_token;
12146 tree expr;
12148 /* First, try parsing an id-expression. */
12149 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12150 cp_parser_parse_tentatively (parser);
12151 expr = cp_parser_id_expression (parser,
12152 /*template_keyword_p=*/false,
12153 /*check_dependency_p=*/true,
12154 /*template_p=*/NULL,
12155 /*declarator_p=*/false,
12156 /*optional_p=*/false);
12158 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12160 bool non_integral_constant_expression_p = false;
12161 tree id_expression = expr;
12162 cp_id_kind idk;
12163 const char *error_msg;
12165 if (identifier_p (expr))
12166 /* Lookup the name we got back from the id-expression. */
12167 expr = cp_parser_lookup_name_simple (parser, expr,
12168 id_expr_start_token->location);
12170 if (expr
12171 && expr != error_mark_node
12172 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
12173 && TREE_CODE (expr) != TYPE_DECL
12174 && (TREE_CODE (expr) != BIT_NOT_EXPR
12175 || !TYPE_P (TREE_OPERAND (expr, 0)))
12176 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12178 /* Complete lookup of the id-expression. */
12179 expr = (finish_id_expression
12180 (id_expression, expr, parser->scope, &idk,
12181 /*integral_constant_expression_p=*/false,
12182 /*allow_non_integral_constant_expression_p=*/true,
12183 &non_integral_constant_expression_p,
12184 /*template_p=*/false,
12185 /*done=*/true,
12186 /*address_p=*/false,
12187 /*template_arg_p=*/false,
12188 &error_msg,
12189 id_expr_start_token->location));
12191 if (expr == error_mark_node)
12192 /* We found an id-expression, but it was something that we
12193 should not have found. This is an error, not something
12194 we can recover from, so note that we found an
12195 id-expression and we'll recover as gracefully as
12196 possible. */
12197 id_expression_or_member_access_p = true;
12200 if (expr
12201 && expr != error_mark_node
12202 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12203 /* We have an id-expression. */
12204 id_expression_or_member_access_p = true;
12207 if (!id_expression_or_member_access_p)
12209 /* Abort the id-expression parse. */
12210 cp_parser_abort_tentative_parse (parser);
12212 /* Parsing tentatively, again. */
12213 cp_parser_parse_tentatively (parser);
12215 /* Parse a class member access. */
12216 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12217 /*cast_p=*/false, /*decltype*/true,
12218 /*member_access_only_p=*/true, NULL);
12220 if (expr
12221 && expr != error_mark_node
12222 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12223 /* We have an id-expression. */
12224 id_expression_or_member_access_p = true;
12227 if (id_expression_or_member_access_p)
12228 /* We have parsed the complete id-expression or member access. */
12229 cp_parser_parse_definitely (parser);
12230 else
12232 /* Abort our attempt to parse an id-expression or member access
12233 expression. */
12234 cp_parser_abort_tentative_parse (parser);
12236 /* Parse a full expression. */
12237 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12238 /*decltype_p=*/true);
12241 return expr;
12244 /* Parse a `decltype' type. Returns the type.
12246 simple-type-specifier:
12247 decltype ( expression )
12248 C++14 proposal:
12249 decltype ( auto ) */
12251 static tree
12252 cp_parser_decltype (cp_parser *parser)
12254 tree expr;
12255 bool id_expression_or_member_access_p = false;
12256 const char *saved_message;
12257 bool saved_integral_constant_expression_p;
12258 bool saved_non_integral_constant_expression_p;
12259 bool saved_greater_than_is_operator_p;
12260 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12262 if (start_token->type == CPP_DECLTYPE)
12264 /* Already parsed. */
12265 cp_lexer_consume_token (parser->lexer);
12266 return start_token->u.value;
12269 /* Look for the `decltype' token. */
12270 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12271 return error_mark_node;
12273 /* Parse the opening `('. */
12274 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12275 return error_mark_node;
12277 /* decltype (auto) */
12278 if (cxx_dialect >= cxx14
12279 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12281 cp_lexer_consume_token (parser->lexer);
12282 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12283 return error_mark_node;
12284 expr = make_decltype_auto ();
12285 AUTO_IS_DECLTYPE (expr) = true;
12286 goto rewrite;
12289 /* Types cannot be defined in a `decltype' expression. Save away the
12290 old message. */
12291 saved_message = parser->type_definition_forbidden_message;
12293 /* And create the new one. */
12294 parser->type_definition_forbidden_message
12295 = G_("types may not be defined in %<decltype%> expressions");
12297 /* The restrictions on constant-expressions do not apply inside
12298 decltype expressions. */
12299 saved_integral_constant_expression_p
12300 = parser->integral_constant_expression_p;
12301 saved_non_integral_constant_expression_p
12302 = parser->non_integral_constant_expression_p;
12303 parser->integral_constant_expression_p = false;
12305 /* Within a parenthesized expression, a `>' token is always
12306 the greater-than operator. */
12307 saved_greater_than_is_operator_p
12308 = parser->greater_than_is_operator_p;
12309 parser->greater_than_is_operator_p = true;
12311 /* Do not actually evaluate the expression. */
12312 ++cp_unevaluated_operand;
12314 /* Do not warn about problems with the expression. */
12315 ++c_inhibit_evaluation_warnings;
12317 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12319 /* Go back to evaluating expressions. */
12320 --cp_unevaluated_operand;
12321 --c_inhibit_evaluation_warnings;
12323 /* The `>' token might be the end of a template-id or
12324 template-parameter-list now. */
12325 parser->greater_than_is_operator_p
12326 = saved_greater_than_is_operator_p;
12328 /* Restore the old message and the integral constant expression
12329 flags. */
12330 parser->type_definition_forbidden_message = saved_message;
12331 parser->integral_constant_expression_p
12332 = saved_integral_constant_expression_p;
12333 parser->non_integral_constant_expression_p
12334 = saved_non_integral_constant_expression_p;
12336 /* Parse to the closing `)'. */
12337 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12339 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12340 /*consume_paren=*/true);
12341 return error_mark_node;
12344 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12345 tf_warning_or_error);
12347 rewrite:
12348 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12349 it again. */
12350 start_token->type = CPP_DECLTYPE;
12351 start_token->u.value = expr;
12352 start_token->keyword = RID_MAX;
12353 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12355 return expr;
12358 /* Special member functions [gram.special] */
12360 /* Parse a conversion-function-id.
12362 conversion-function-id:
12363 operator conversion-type-id
12365 Returns an IDENTIFIER_NODE representing the operator. */
12367 static tree
12368 cp_parser_conversion_function_id (cp_parser* parser)
12370 tree type;
12371 tree saved_scope;
12372 tree saved_qualifying_scope;
12373 tree saved_object_scope;
12374 tree pushed_scope = NULL_TREE;
12376 /* Look for the `operator' token. */
12377 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12378 return error_mark_node;
12379 /* When we parse the conversion-type-id, the current scope will be
12380 reset. However, we need that information in able to look up the
12381 conversion function later, so we save it here. */
12382 saved_scope = parser->scope;
12383 saved_qualifying_scope = parser->qualifying_scope;
12384 saved_object_scope = parser->object_scope;
12385 /* We must enter the scope of the class so that the names of
12386 entities declared within the class are available in the
12387 conversion-type-id. For example, consider:
12389 struct S {
12390 typedef int I;
12391 operator I();
12394 S::operator I() { ... }
12396 In order to see that `I' is a type-name in the definition, we
12397 must be in the scope of `S'. */
12398 if (saved_scope)
12399 pushed_scope = push_scope (saved_scope);
12400 /* Parse the conversion-type-id. */
12401 type = cp_parser_conversion_type_id (parser);
12402 /* Leave the scope of the class, if any. */
12403 if (pushed_scope)
12404 pop_scope (pushed_scope);
12405 /* Restore the saved scope. */
12406 parser->scope = saved_scope;
12407 parser->qualifying_scope = saved_qualifying_scope;
12408 parser->object_scope = saved_object_scope;
12409 /* If the TYPE is invalid, indicate failure. */
12410 if (type == error_mark_node)
12411 return error_mark_node;
12412 return mangle_conv_op_name_for_type (type);
12415 /* Parse a conversion-type-id:
12417 conversion-type-id:
12418 type-specifier-seq conversion-declarator [opt]
12420 Returns the TYPE specified. */
12422 static tree
12423 cp_parser_conversion_type_id (cp_parser* parser)
12425 tree attributes;
12426 cp_decl_specifier_seq type_specifiers;
12427 cp_declarator *declarator;
12428 tree type_specified;
12429 const char *saved_message;
12431 /* Parse the attributes. */
12432 attributes = cp_parser_attributes_opt (parser);
12434 saved_message = parser->type_definition_forbidden_message;
12435 parser->type_definition_forbidden_message
12436 = G_("types may not be defined in a conversion-type-id");
12438 /* Parse the type-specifiers. */
12439 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12440 /*is_trailing_return=*/false,
12441 &type_specifiers);
12443 parser->type_definition_forbidden_message = saved_message;
12445 /* If that didn't work, stop. */
12446 if (type_specifiers.type == error_mark_node)
12447 return error_mark_node;
12448 /* Parse the conversion-declarator. */
12449 declarator = cp_parser_conversion_declarator_opt (parser);
12451 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12452 /*initialized=*/0, &attributes);
12453 if (attributes)
12454 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12456 /* Don't give this error when parsing tentatively. This happens to
12457 work because we always parse this definitively once. */
12458 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12459 && type_uses_auto (type_specified))
12461 if (cxx_dialect < cxx14)
12463 error ("invalid use of %<auto%> in conversion operator");
12464 return error_mark_node;
12466 else if (template_parm_scope_p ())
12467 warning (0, "use of %<auto%> in member template "
12468 "conversion operator can never be deduced");
12471 return type_specified;
12474 /* Parse an (optional) conversion-declarator.
12476 conversion-declarator:
12477 ptr-operator conversion-declarator [opt]
12481 static cp_declarator *
12482 cp_parser_conversion_declarator_opt (cp_parser* parser)
12484 enum tree_code code;
12485 tree class_type, std_attributes = NULL_TREE;
12486 cp_cv_quals cv_quals;
12488 /* We don't know if there's a ptr-operator next, or not. */
12489 cp_parser_parse_tentatively (parser);
12490 /* Try the ptr-operator. */
12491 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12492 &std_attributes);
12493 /* If it worked, look for more conversion-declarators. */
12494 if (cp_parser_parse_definitely (parser))
12496 cp_declarator *declarator;
12498 /* Parse another optional declarator. */
12499 declarator = cp_parser_conversion_declarator_opt (parser);
12501 declarator = cp_parser_make_indirect_declarator
12502 (code, class_type, cv_quals, declarator, std_attributes);
12504 return declarator;
12507 return NULL;
12510 /* Parse an (optional) ctor-initializer.
12512 ctor-initializer:
12513 : mem-initializer-list
12515 Returns TRUE iff the ctor-initializer was actually present. */
12517 static bool
12518 cp_parser_ctor_initializer_opt (cp_parser* parser)
12520 /* If the next token is not a `:', then there is no
12521 ctor-initializer. */
12522 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12524 /* Do default initialization of any bases and members. */
12525 if (DECL_CONSTRUCTOR_P (current_function_decl))
12526 finish_mem_initializers (NULL_TREE);
12528 return false;
12531 /* Consume the `:' token. */
12532 cp_lexer_consume_token (parser->lexer);
12533 /* And the mem-initializer-list. */
12534 cp_parser_mem_initializer_list (parser);
12536 return true;
12539 /* Parse a mem-initializer-list.
12541 mem-initializer-list:
12542 mem-initializer ... [opt]
12543 mem-initializer ... [opt] , mem-initializer-list */
12545 static void
12546 cp_parser_mem_initializer_list (cp_parser* parser)
12548 tree mem_initializer_list = NULL_TREE;
12549 tree target_ctor = error_mark_node;
12550 cp_token *token = cp_lexer_peek_token (parser->lexer);
12552 /* Let the semantic analysis code know that we are starting the
12553 mem-initializer-list. */
12554 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12555 error_at (token->location,
12556 "only constructors take member initializers");
12558 /* Loop through the list. */
12559 while (true)
12561 tree mem_initializer;
12563 token = cp_lexer_peek_token (parser->lexer);
12564 /* Parse the mem-initializer. */
12565 mem_initializer = cp_parser_mem_initializer (parser);
12566 /* If the next token is a `...', we're expanding member initializers. */
12567 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12569 /* Consume the `...'. */
12570 cp_lexer_consume_token (parser->lexer);
12572 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12573 can be expanded but members cannot. */
12574 if (mem_initializer != error_mark_node
12575 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12577 error_at (token->location,
12578 "cannot expand initializer for member %<%D%>",
12579 TREE_PURPOSE (mem_initializer));
12580 mem_initializer = error_mark_node;
12583 /* Construct the pack expansion type. */
12584 if (mem_initializer != error_mark_node)
12585 mem_initializer = make_pack_expansion (mem_initializer);
12587 if (target_ctor != error_mark_node
12588 && mem_initializer != error_mark_node)
12590 error ("mem-initializer for %qD follows constructor delegation",
12591 TREE_PURPOSE (mem_initializer));
12592 mem_initializer = error_mark_node;
12594 /* Look for a target constructor. */
12595 if (mem_initializer != error_mark_node
12596 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12597 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12599 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12600 if (mem_initializer_list)
12602 error ("constructor delegation follows mem-initializer for %qD",
12603 TREE_PURPOSE (mem_initializer_list));
12604 mem_initializer = error_mark_node;
12606 target_ctor = mem_initializer;
12608 /* Add it to the list, unless it was erroneous. */
12609 if (mem_initializer != error_mark_node)
12611 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12612 mem_initializer_list = mem_initializer;
12614 /* If the next token is not a `,', we're done. */
12615 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12616 break;
12617 /* Consume the `,' token. */
12618 cp_lexer_consume_token (parser->lexer);
12621 /* Perform semantic analysis. */
12622 if (DECL_CONSTRUCTOR_P (current_function_decl))
12623 finish_mem_initializers (mem_initializer_list);
12626 /* Parse a mem-initializer.
12628 mem-initializer:
12629 mem-initializer-id ( expression-list [opt] )
12630 mem-initializer-id braced-init-list
12632 GNU extension:
12634 mem-initializer:
12635 ( expression-list [opt] )
12637 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12638 class) or FIELD_DECL (for a non-static data member) to initialize;
12639 the TREE_VALUE is the expression-list. An empty initialization
12640 list is represented by void_list_node. */
12642 static tree
12643 cp_parser_mem_initializer (cp_parser* parser)
12645 tree mem_initializer_id;
12646 tree expression_list;
12647 tree member;
12648 cp_token *token = cp_lexer_peek_token (parser->lexer);
12650 /* Find out what is being initialized. */
12651 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12653 permerror (token->location,
12654 "anachronistic old-style base class initializer");
12655 mem_initializer_id = NULL_TREE;
12657 else
12659 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12660 if (mem_initializer_id == error_mark_node)
12661 return mem_initializer_id;
12663 member = expand_member_init (mem_initializer_id);
12664 if (member && !DECL_P (member))
12665 in_base_initializer = 1;
12667 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12669 bool expr_non_constant_p;
12670 cp_lexer_set_source_position (parser->lexer);
12671 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12672 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12673 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12674 expression_list = build_tree_list (NULL_TREE, expression_list);
12676 else
12678 vec<tree, va_gc> *vec;
12679 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12680 /*cast_p=*/false,
12681 /*allow_expansion_p=*/true,
12682 /*non_constant_p=*/NULL);
12683 if (vec == NULL)
12684 return error_mark_node;
12685 expression_list = build_tree_list_vec (vec);
12686 release_tree_vector (vec);
12689 if (expression_list == error_mark_node)
12690 return error_mark_node;
12691 if (!expression_list)
12692 expression_list = void_type_node;
12694 in_base_initializer = 0;
12696 return member ? build_tree_list (member, expression_list) : error_mark_node;
12699 /* Parse a mem-initializer-id.
12701 mem-initializer-id:
12702 :: [opt] nested-name-specifier [opt] class-name
12703 identifier
12705 Returns a TYPE indicating the class to be initializer for the first
12706 production. Returns an IDENTIFIER_NODE indicating the data member
12707 to be initialized for the second production. */
12709 static tree
12710 cp_parser_mem_initializer_id (cp_parser* parser)
12712 bool global_scope_p;
12713 bool nested_name_specifier_p;
12714 bool template_p = false;
12715 tree id;
12717 cp_token *token = cp_lexer_peek_token (parser->lexer);
12719 /* `typename' is not allowed in this context ([temp.res]). */
12720 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12722 error_at (token->location,
12723 "keyword %<typename%> not allowed in this context (a qualified "
12724 "member initializer is implicitly a type)");
12725 cp_lexer_consume_token (parser->lexer);
12727 /* Look for the optional `::' operator. */
12728 global_scope_p
12729 = (cp_parser_global_scope_opt (parser,
12730 /*current_scope_valid_p=*/false)
12731 != NULL_TREE);
12732 /* Look for the optional nested-name-specifier. The simplest way to
12733 implement:
12735 [temp.res]
12737 The keyword `typename' is not permitted in a base-specifier or
12738 mem-initializer; in these contexts a qualified name that
12739 depends on a template-parameter is implicitly assumed to be a
12740 type name.
12742 is to assume that we have seen the `typename' keyword at this
12743 point. */
12744 nested_name_specifier_p
12745 = (cp_parser_nested_name_specifier_opt (parser,
12746 /*typename_keyword_p=*/true,
12747 /*check_dependency_p=*/true,
12748 /*type_p=*/true,
12749 /*is_declaration=*/true)
12750 != NULL_TREE);
12751 if (nested_name_specifier_p)
12752 template_p = cp_parser_optional_template_keyword (parser);
12753 /* If there is a `::' operator or a nested-name-specifier, then we
12754 are definitely looking for a class-name. */
12755 if (global_scope_p || nested_name_specifier_p)
12756 return cp_parser_class_name (parser,
12757 /*typename_keyword_p=*/true,
12758 /*template_keyword_p=*/template_p,
12759 typename_type,
12760 /*check_dependency_p=*/true,
12761 /*class_head_p=*/false,
12762 /*is_declaration=*/true);
12763 /* Otherwise, we could also be looking for an ordinary identifier. */
12764 cp_parser_parse_tentatively (parser);
12765 /* Try a class-name. */
12766 id = cp_parser_class_name (parser,
12767 /*typename_keyword_p=*/true,
12768 /*template_keyword_p=*/false,
12769 none_type,
12770 /*check_dependency_p=*/true,
12771 /*class_head_p=*/false,
12772 /*is_declaration=*/true);
12773 /* If we found one, we're done. */
12774 if (cp_parser_parse_definitely (parser))
12775 return id;
12776 /* Otherwise, look for an ordinary identifier. */
12777 return cp_parser_identifier (parser);
12780 /* Overloading [gram.over] */
12782 /* Parse an operator-function-id.
12784 operator-function-id:
12785 operator operator
12787 Returns an IDENTIFIER_NODE for the operator which is a
12788 human-readable spelling of the identifier, e.g., `operator +'. */
12790 static tree
12791 cp_parser_operator_function_id (cp_parser* parser)
12793 /* Look for the `operator' keyword. */
12794 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12795 return error_mark_node;
12796 /* And then the name of the operator itself. */
12797 return cp_parser_operator (parser);
12800 /* Return an identifier node for a user-defined literal operator.
12801 The suffix identifier is chained to the operator name identifier. */
12803 static tree
12804 cp_literal_operator_id (const char* name)
12806 tree identifier;
12807 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12808 + strlen (name) + 10);
12809 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12810 identifier = get_identifier (buffer);
12812 return identifier;
12815 /* Parse an operator.
12817 operator:
12818 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12819 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12820 || ++ -- , ->* -> () []
12822 GNU Extensions:
12824 operator:
12825 <? >? <?= >?=
12827 Returns an IDENTIFIER_NODE for the operator which is a
12828 human-readable spelling of the identifier, e.g., `operator +'. */
12830 static tree
12831 cp_parser_operator (cp_parser* parser)
12833 tree id = NULL_TREE;
12834 cp_token *token;
12835 bool utf8 = false;
12837 /* Peek at the next token. */
12838 token = cp_lexer_peek_token (parser->lexer);
12839 /* Figure out which operator we have. */
12840 switch (token->type)
12842 case CPP_KEYWORD:
12844 enum tree_code op;
12846 /* The keyword should be either `new' or `delete'. */
12847 if (token->keyword == RID_NEW)
12848 op = NEW_EXPR;
12849 else if (token->keyword == RID_DELETE)
12850 op = DELETE_EXPR;
12851 else
12852 break;
12854 /* Consume the `new' or `delete' token. */
12855 cp_lexer_consume_token (parser->lexer);
12857 /* Peek at the next token. */
12858 token = cp_lexer_peek_token (parser->lexer);
12859 /* If it's a `[' token then this is the array variant of the
12860 operator. */
12861 if (token->type == CPP_OPEN_SQUARE)
12863 /* Consume the `[' token. */
12864 cp_lexer_consume_token (parser->lexer);
12865 /* Look for the `]' token. */
12866 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12867 id = ansi_opname (op == NEW_EXPR
12868 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12870 /* Otherwise, we have the non-array variant. */
12871 else
12872 id = ansi_opname (op);
12874 return id;
12877 case CPP_PLUS:
12878 id = ansi_opname (PLUS_EXPR);
12879 break;
12881 case CPP_MINUS:
12882 id = ansi_opname (MINUS_EXPR);
12883 break;
12885 case CPP_MULT:
12886 id = ansi_opname (MULT_EXPR);
12887 break;
12889 case CPP_DIV:
12890 id = ansi_opname (TRUNC_DIV_EXPR);
12891 break;
12893 case CPP_MOD:
12894 id = ansi_opname (TRUNC_MOD_EXPR);
12895 break;
12897 case CPP_XOR:
12898 id = ansi_opname (BIT_XOR_EXPR);
12899 break;
12901 case CPP_AND:
12902 id = ansi_opname (BIT_AND_EXPR);
12903 break;
12905 case CPP_OR:
12906 id = ansi_opname (BIT_IOR_EXPR);
12907 break;
12909 case CPP_COMPL:
12910 id = ansi_opname (BIT_NOT_EXPR);
12911 break;
12913 case CPP_NOT:
12914 id = ansi_opname (TRUTH_NOT_EXPR);
12915 break;
12917 case CPP_EQ:
12918 id = ansi_assopname (NOP_EXPR);
12919 break;
12921 case CPP_LESS:
12922 id = ansi_opname (LT_EXPR);
12923 break;
12925 case CPP_GREATER:
12926 id = ansi_opname (GT_EXPR);
12927 break;
12929 case CPP_PLUS_EQ:
12930 id = ansi_assopname (PLUS_EXPR);
12931 break;
12933 case CPP_MINUS_EQ:
12934 id = ansi_assopname (MINUS_EXPR);
12935 break;
12937 case CPP_MULT_EQ:
12938 id = ansi_assopname (MULT_EXPR);
12939 break;
12941 case CPP_DIV_EQ:
12942 id = ansi_assopname (TRUNC_DIV_EXPR);
12943 break;
12945 case CPP_MOD_EQ:
12946 id = ansi_assopname (TRUNC_MOD_EXPR);
12947 break;
12949 case CPP_XOR_EQ:
12950 id = ansi_assopname (BIT_XOR_EXPR);
12951 break;
12953 case CPP_AND_EQ:
12954 id = ansi_assopname (BIT_AND_EXPR);
12955 break;
12957 case CPP_OR_EQ:
12958 id = ansi_assopname (BIT_IOR_EXPR);
12959 break;
12961 case CPP_LSHIFT:
12962 id = ansi_opname (LSHIFT_EXPR);
12963 break;
12965 case CPP_RSHIFT:
12966 id = ansi_opname (RSHIFT_EXPR);
12967 break;
12969 case CPP_LSHIFT_EQ:
12970 id = ansi_assopname (LSHIFT_EXPR);
12971 break;
12973 case CPP_RSHIFT_EQ:
12974 id = ansi_assopname (RSHIFT_EXPR);
12975 break;
12977 case CPP_EQ_EQ:
12978 id = ansi_opname (EQ_EXPR);
12979 break;
12981 case CPP_NOT_EQ:
12982 id = ansi_opname (NE_EXPR);
12983 break;
12985 case CPP_LESS_EQ:
12986 id = ansi_opname (LE_EXPR);
12987 break;
12989 case CPP_GREATER_EQ:
12990 id = ansi_opname (GE_EXPR);
12991 break;
12993 case CPP_AND_AND:
12994 id = ansi_opname (TRUTH_ANDIF_EXPR);
12995 break;
12997 case CPP_OR_OR:
12998 id = ansi_opname (TRUTH_ORIF_EXPR);
12999 break;
13001 case CPP_PLUS_PLUS:
13002 id = ansi_opname (POSTINCREMENT_EXPR);
13003 break;
13005 case CPP_MINUS_MINUS:
13006 id = ansi_opname (PREDECREMENT_EXPR);
13007 break;
13009 case CPP_COMMA:
13010 id = ansi_opname (COMPOUND_EXPR);
13011 break;
13013 case CPP_DEREF_STAR:
13014 id = ansi_opname (MEMBER_REF);
13015 break;
13017 case CPP_DEREF:
13018 id = ansi_opname (COMPONENT_REF);
13019 break;
13021 case CPP_OPEN_PAREN:
13022 /* Consume the `('. */
13023 cp_lexer_consume_token (parser->lexer);
13024 /* Look for the matching `)'. */
13025 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13026 return ansi_opname (CALL_EXPR);
13028 case CPP_OPEN_SQUARE:
13029 /* Consume the `['. */
13030 cp_lexer_consume_token (parser->lexer);
13031 /* Look for the matching `]'. */
13032 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13033 return ansi_opname (ARRAY_REF);
13035 case CPP_UTF8STRING:
13036 case CPP_UTF8STRING_USERDEF:
13037 utf8 = true;
13038 case CPP_STRING:
13039 case CPP_WSTRING:
13040 case CPP_STRING16:
13041 case CPP_STRING32:
13042 case CPP_STRING_USERDEF:
13043 case CPP_WSTRING_USERDEF:
13044 case CPP_STRING16_USERDEF:
13045 case CPP_STRING32_USERDEF:
13047 tree str, string_tree;
13048 int sz, len;
13050 if (cxx_dialect == cxx98)
13051 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13053 /* Consume the string. */
13054 str = cp_parser_string_literal (parser, /*translate=*/true,
13055 /*wide_ok=*/true, /*lookup_udlit=*/false);
13056 if (str == error_mark_node)
13057 return error_mark_node;
13058 else if (TREE_CODE (str) == USERDEF_LITERAL)
13060 string_tree = USERDEF_LITERAL_VALUE (str);
13061 id = USERDEF_LITERAL_SUFFIX_ID (str);
13063 else
13065 string_tree = str;
13066 /* Look for the suffix identifier. */
13067 token = cp_lexer_peek_token (parser->lexer);
13068 if (token->type == CPP_NAME)
13069 id = cp_parser_identifier (parser);
13070 else if (token->type == CPP_KEYWORD)
13072 error ("unexpected keyword;"
13073 " remove space between quotes and suffix identifier");
13074 return error_mark_node;
13076 else
13078 error ("expected suffix identifier");
13079 return error_mark_node;
13082 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13083 (TREE_TYPE (TREE_TYPE (string_tree))));
13084 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13085 if (len != 0)
13087 error ("expected empty string after %<operator%> keyword");
13088 return error_mark_node;
13090 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13091 != char_type_node)
13093 error ("invalid encoding prefix in literal operator");
13094 return error_mark_node;
13096 if (id != error_mark_node)
13098 const char *name = IDENTIFIER_POINTER (id);
13099 id = cp_literal_operator_id (name);
13101 return id;
13104 default:
13105 /* Anything else is an error. */
13106 break;
13109 /* If we have selected an identifier, we need to consume the
13110 operator token. */
13111 if (id)
13112 cp_lexer_consume_token (parser->lexer);
13113 /* Otherwise, no valid operator name was present. */
13114 else
13116 cp_parser_error (parser, "expected operator");
13117 id = error_mark_node;
13120 return id;
13123 /* Parse a template-declaration.
13125 template-declaration:
13126 export [opt] template < template-parameter-list > declaration
13128 If MEMBER_P is TRUE, this template-declaration occurs within a
13129 class-specifier.
13131 The grammar rule given by the standard isn't correct. What
13132 is really meant is:
13134 template-declaration:
13135 export [opt] template-parameter-list-seq
13136 decl-specifier-seq [opt] init-declarator [opt] ;
13137 export [opt] template-parameter-list-seq
13138 function-definition
13140 template-parameter-list-seq:
13141 template-parameter-list-seq [opt]
13142 template < template-parameter-list > */
13144 static void
13145 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13147 /* Check for `export'. */
13148 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13150 /* Consume the `export' token. */
13151 cp_lexer_consume_token (parser->lexer);
13152 /* Warn that we do not support `export'. */
13153 warning (0, "keyword %<export%> not implemented, and will be ignored");
13156 cp_parser_template_declaration_after_export (parser, member_p);
13159 /* Parse a template-parameter-list.
13161 template-parameter-list:
13162 template-parameter
13163 template-parameter-list , template-parameter
13165 Returns a TREE_LIST. Each node represents a template parameter.
13166 The nodes are connected via their TREE_CHAINs. */
13168 static tree
13169 cp_parser_template_parameter_list (cp_parser* parser)
13171 tree parameter_list = NULL_TREE;
13173 begin_template_parm_list ();
13175 /* The loop below parses the template parms. We first need to know
13176 the total number of template parms to be able to compute proper
13177 canonical types of each dependent type. So after the loop, when
13178 we know the total number of template parms,
13179 end_template_parm_list computes the proper canonical types and
13180 fixes up the dependent types accordingly. */
13181 while (true)
13183 tree parameter;
13184 bool is_non_type;
13185 bool is_parameter_pack;
13186 location_t parm_loc;
13188 /* Parse the template-parameter. */
13189 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13190 parameter = cp_parser_template_parameter (parser,
13191 &is_non_type,
13192 &is_parameter_pack);
13193 /* Add it to the list. */
13194 if (parameter != error_mark_node)
13195 parameter_list = process_template_parm (parameter_list,
13196 parm_loc,
13197 parameter,
13198 is_non_type,
13199 is_parameter_pack);
13200 else
13202 tree err_parm = build_tree_list (parameter, parameter);
13203 parameter_list = chainon (parameter_list, err_parm);
13206 /* If the next token is not a `,', we're done. */
13207 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13208 break;
13209 /* Otherwise, consume the `,' token. */
13210 cp_lexer_consume_token (parser->lexer);
13213 return end_template_parm_list (parameter_list);
13216 /* Parse a template-parameter.
13218 template-parameter:
13219 type-parameter
13220 parameter-declaration
13222 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13223 the parameter. The TREE_PURPOSE is the default value, if any.
13224 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13225 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13226 set to true iff this parameter is a parameter pack. */
13228 static tree
13229 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13230 bool *is_parameter_pack)
13232 cp_token *token;
13233 cp_parameter_declarator *parameter_declarator;
13234 cp_declarator *id_declarator;
13235 tree parm;
13237 /* Assume it is a type parameter or a template parameter. */
13238 *is_non_type = false;
13239 /* Assume it not a parameter pack. */
13240 *is_parameter_pack = false;
13241 /* Peek at the next token. */
13242 token = cp_lexer_peek_token (parser->lexer);
13243 /* If it is `class' or `template', we have a type-parameter. */
13244 if (token->keyword == RID_TEMPLATE)
13245 return cp_parser_type_parameter (parser, is_parameter_pack);
13246 /* If it is `class' or `typename' we do not know yet whether it is a
13247 type parameter or a non-type parameter. Consider:
13249 template <typename T, typename T::X X> ...
13253 template <class C, class D*> ...
13255 Here, the first parameter is a type parameter, and the second is
13256 a non-type parameter. We can tell by looking at the token after
13257 the identifier -- if it is a `,', `=', or `>' then we have a type
13258 parameter. */
13259 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13261 /* Peek at the token after `class' or `typename'. */
13262 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13263 /* If it's an ellipsis, we have a template type parameter
13264 pack. */
13265 if (token->type == CPP_ELLIPSIS)
13266 return cp_parser_type_parameter (parser, is_parameter_pack);
13267 /* If it's an identifier, skip it. */
13268 if (token->type == CPP_NAME)
13269 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13270 /* Now, see if the token looks like the end of a template
13271 parameter. */
13272 if (token->type == CPP_COMMA
13273 || token->type == CPP_EQ
13274 || token->type == CPP_GREATER)
13275 return cp_parser_type_parameter (parser, is_parameter_pack);
13278 /* Otherwise, it is a non-type parameter.
13280 [temp.param]
13282 When parsing a default template-argument for a non-type
13283 template-parameter, the first non-nested `>' is taken as the end
13284 of the template parameter-list rather than a greater-than
13285 operator. */
13286 *is_non_type = true;
13287 parameter_declarator
13288 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13289 /*parenthesized_p=*/NULL);
13291 if (!parameter_declarator)
13292 return error_mark_node;
13294 /* If the parameter declaration is marked as a parameter pack, set
13295 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13296 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13297 grokdeclarator. */
13298 if (parameter_declarator->declarator
13299 && parameter_declarator->declarator->parameter_pack_p)
13301 *is_parameter_pack = true;
13302 parameter_declarator->declarator->parameter_pack_p = false;
13305 if (parameter_declarator->default_argument)
13307 /* Can happen in some cases of erroneous input (c++/34892). */
13308 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13309 /* Consume the `...' for better error recovery. */
13310 cp_lexer_consume_token (parser->lexer);
13312 /* If the next token is an ellipsis, and we don't already have it
13313 marked as a parameter pack, then we have a parameter pack (that
13314 has no declarator). */
13315 else if (!*is_parameter_pack
13316 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13317 && (declarator_can_be_parameter_pack
13318 (parameter_declarator->declarator)))
13320 /* Consume the `...'. */
13321 cp_lexer_consume_token (parser->lexer);
13322 maybe_warn_variadic_templates ();
13324 *is_parameter_pack = true;
13326 /* We might end up with a pack expansion as the type of the non-type
13327 template parameter, in which case this is a non-type template
13328 parameter pack. */
13329 else if (parameter_declarator->decl_specifiers.type
13330 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13332 *is_parameter_pack = true;
13333 parameter_declarator->decl_specifiers.type =
13334 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13337 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13339 /* Parameter packs cannot have default arguments. However, a
13340 user may try to do so, so we'll parse them and give an
13341 appropriate diagnostic here. */
13343 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13345 /* Find the name of the parameter pack. */
13346 id_declarator = parameter_declarator->declarator;
13347 while (id_declarator && id_declarator->kind != cdk_id)
13348 id_declarator = id_declarator->declarator;
13350 if (id_declarator && id_declarator->kind == cdk_id)
13351 error_at (start_token->location,
13352 "template parameter pack %qD cannot have a default argument",
13353 id_declarator->u.id.unqualified_name);
13354 else
13355 error_at (start_token->location,
13356 "template parameter pack cannot have a default argument");
13358 /* Parse the default argument, but throw away the result. */
13359 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13362 parm = grokdeclarator (parameter_declarator->declarator,
13363 &parameter_declarator->decl_specifiers,
13364 TPARM, /*initialized=*/0,
13365 /*attrlist=*/NULL);
13366 if (parm == error_mark_node)
13367 return error_mark_node;
13369 return build_tree_list (parameter_declarator->default_argument, parm);
13372 /* Parse a type-parameter.
13374 type-parameter:
13375 class identifier [opt]
13376 class identifier [opt] = type-id
13377 typename identifier [opt]
13378 typename identifier [opt] = type-id
13379 template < template-parameter-list > class identifier [opt]
13380 template < template-parameter-list > class identifier [opt]
13381 = id-expression
13383 GNU Extension (variadic templates):
13385 type-parameter:
13386 class ... identifier [opt]
13387 typename ... identifier [opt]
13389 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13390 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13391 the declaration of the parameter.
13393 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13395 static tree
13396 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13398 cp_token *token;
13399 tree parameter;
13401 /* Look for a keyword to tell us what kind of parameter this is. */
13402 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13403 if (!token)
13404 return error_mark_node;
13406 switch (token->keyword)
13408 case RID_CLASS:
13409 case RID_TYPENAME:
13411 tree identifier;
13412 tree default_argument;
13414 /* If the next token is an ellipsis, we have a template
13415 argument pack. */
13416 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13418 /* Consume the `...' token. */
13419 cp_lexer_consume_token (parser->lexer);
13420 maybe_warn_variadic_templates ();
13422 *is_parameter_pack = true;
13425 /* If the next token is an identifier, then it names the
13426 parameter. */
13427 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13428 identifier = cp_parser_identifier (parser);
13429 else
13430 identifier = NULL_TREE;
13432 /* Create the parameter. */
13433 parameter = finish_template_type_parm (class_type_node, identifier);
13435 /* If the next token is an `=', we have a default argument. */
13436 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13438 /* Consume the `=' token. */
13439 cp_lexer_consume_token (parser->lexer);
13440 /* Parse the default-argument. */
13441 push_deferring_access_checks (dk_no_deferred);
13442 default_argument = cp_parser_type_id (parser);
13444 /* Template parameter packs cannot have default
13445 arguments. */
13446 if (*is_parameter_pack)
13448 if (identifier)
13449 error_at (token->location,
13450 "template parameter pack %qD cannot have a "
13451 "default argument", identifier);
13452 else
13453 error_at (token->location,
13454 "template parameter packs cannot have "
13455 "default arguments");
13456 default_argument = NULL_TREE;
13458 pop_deferring_access_checks ();
13460 else
13461 default_argument = NULL_TREE;
13463 /* Create the combined representation of the parameter and the
13464 default argument. */
13465 parameter = build_tree_list (default_argument, parameter);
13467 break;
13469 case RID_TEMPLATE:
13471 tree identifier;
13472 tree default_argument;
13474 /* Look for the `<'. */
13475 cp_parser_require (parser, CPP_LESS, RT_LESS);
13476 /* Parse the template-parameter-list. */
13477 cp_parser_template_parameter_list (parser);
13478 /* Look for the `>'. */
13479 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13480 /* Look for the `class' or 'typename' keywords. */
13481 cp_parser_type_parameter_key (parser);
13482 /* If the next token is an ellipsis, we have a template
13483 argument pack. */
13484 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13486 /* Consume the `...' token. */
13487 cp_lexer_consume_token (parser->lexer);
13488 maybe_warn_variadic_templates ();
13490 *is_parameter_pack = true;
13492 /* If the next token is an `=', then there is a
13493 default-argument. If the next token is a `>', we are at
13494 the end of the parameter-list. If the next token is a `,',
13495 then we are at the end of this parameter. */
13496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13497 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13498 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13500 identifier = cp_parser_identifier (parser);
13501 /* Treat invalid names as if the parameter were nameless. */
13502 if (identifier == error_mark_node)
13503 identifier = NULL_TREE;
13505 else
13506 identifier = NULL_TREE;
13508 /* Create the template parameter. */
13509 parameter = finish_template_template_parm (class_type_node,
13510 identifier);
13512 /* If the next token is an `=', then there is a
13513 default-argument. */
13514 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13516 bool is_template;
13518 /* Consume the `='. */
13519 cp_lexer_consume_token (parser->lexer);
13520 /* Parse the id-expression. */
13521 push_deferring_access_checks (dk_no_deferred);
13522 /* save token before parsing the id-expression, for error
13523 reporting */
13524 token = cp_lexer_peek_token (parser->lexer);
13525 default_argument
13526 = cp_parser_id_expression (parser,
13527 /*template_keyword_p=*/false,
13528 /*check_dependency_p=*/true,
13529 /*template_p=*/&is_template,
13530 /*declarator_p=*/false,
13531 /*optional_p=*/false);
13532 if (TREE_CODE (default_argument) == TYPE_DECL)
13533 /* If the id-expression was a template-id that refers to
13534 a template-class, we already have the declaration here,
13535 so no further lookup is needed. */
13537 else
13538 /* Look up the name. */
13539 default_argument
13540 = cp_parser_lookup_name (parser, default_argument,
13541 none_type,
13542 /*is_template=*/is_template,
13543 /*is_namespace=*/false,
13544 /*check_dependency=*/true,
13545 /*ambiguous_decls=*/NULL,
13546 token->location);
13547 /* See if the default argument is valid. */
13548 default_argument
13549 = check_template_template_default_arg (default_argument);
13551 /* Template parameter packs cannot have default
13552 arguments. */
13553 if (*is_parameter_pack)
13555 if (identifier)
13556 error_at (token->location,
13557 "template parameter pack %qD cannot "
13558 "have a default argument",
13559 identifier);
13560 else
13561 error_at (token->location, "template parameter packs cannot "
13562 "have default arguments");
13563 default_argument = NULL_TREE;
13565 pop_deferring_access_checks ();
13567 else
13568 default_argument = NULL_TREE;
13570 /* Create the combined representation of the parameter and the
13571 default argument. */
13572 parameter = build_tree_list (default_argument, parameter);
13574 break;
13576 default:
13577 gcc_unreachable ();
13578 break;
13581 return parameter;
13584 /* Parse a template-id.
13586 template-id:
13587 template-name < template-argument-list [opt] >
13589 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13590 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13591 returned. Otherwise, if the template-name names a function, or set
13592 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13593 names a class, returns a TYPE_DECL for the specialization.
13595 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13596 uninstantiated templates. */
13598 static tree
13599 cp_parser_template_id (cp_parser *parser,
13600 bool template_keyword_p,
13601 bool check_dependency_p,
13602 enum tag_types tag_type,
13603 bool is_declaration)
13605 int i;
13606 tree templ;
13607 tree arguments;
13608 tree template_id;
13609 cp_token_position start_of_id = 0;
13610 deferred_access_check *chk;
13611 vec<deferred_access_check, va_gc> *access_check;
13612 cp_token *next_token = NULL, *next_token_2 = NULL;
13613 bool is_identifier;
13615 /* If the next token corresponds to a template-id, there is no need
13616 to reparse it. */
13617 next_token = cp_lexer_peek_token (parser->lexer);
13618 if (next_token->type == CPP_TEMPLATE_ID)
13620 struct tree_check *check_value;
13622 /* Get the stored value. */
13623 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13624 /* Perform any access checks that were deferred. */
13625 access_check = check_value->checks;
13626 if (access_check)
13628 FOR_EACH_VEC_ELT (*access_check, i, chk)
13629 perform_or_defer_access_check (chk->binfo,
13630 chk->decl,
13631 chk->diag_decl,
13632 tf_warning_or_error);
13634 /* Return the stored value. */
13635 return check_value->value;
13638 /* Avoid performing name lookup if there is no possibility of
13639 finding a template-id. */
13640 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13641 || (next_token->type == CPP_NAME
13642 && !cp_parser_nth_token_starts_template_argument_list_p
13643 (parser, 2)))
13645 cp_parser_error (parser, "expected template-id");
13646 return error_mark_node;
13649 /* Remember where the template-id starts. */
13650 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13651 start_of_id = cp_lexer_token_position (parser->lexer, false);
13653 push_deferring_access_checks (dk_deferred);
13655 /* Parse the template-name. */
13656 is_identifier = false;
13657 templ = cp_parser_template_name (parser, template_keyword_p,
13658 check_dependency_p,
13659 is_declaration,
13660 tag_type,
13661 &is_identifier);
13662 if (templ == error_mark_node || is_identifier)
13664 pop_deferring_access_checks ();
13665 return templ;
13668 /* If we find the sequence `[:' after a template-name, it's probably
13669 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13670 parse correctly the argument list. */
13671 next_token = cp_lexer_peek_token (parser->lexer);
13672 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13673 if (next_token->type == CPP_OPEN_SQUARE
13674 && next_token->flags & DIGRAPH
13675 && next_token_2->type == CPP_COLON
13676 && !(next_token_2->flags & PREV_WHITE))
13678 cp_parser_parse_tentatively (parser);
13679 /* Change `:' into `::'. */
13680 next_token_2->type = CPP_SCOPE;
13681 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13682 CPP_LESS. */
13683 cp_lexer_consume_token (parser->lexer);
13685 /* Parse the arguments. */
13686 arguments = cp_parser_enclosed_template_argument_list (parser);
13687 if (!cp_parser_parse_definitely (parser))
13689 /* If we couldn't parse an argument list, then we revert our changes
13690 and return simply an error. Maybe this is not a template-id
13691 after all. */
13692 next_token_2->type = CPP_COLON;
13693 cp_parser_error (parser, "expected %<<%>");
13694 pop_deferring_access_checks ();
13695 return error_mark_node;
13697 /* Otherwise, emit an error about the invalid digraph, but continue
13698 parsing because we got our argument list. */
13699 if (permerror (next_token->location,
13700 "%<<::%> cannot begin a template-argument list"))
13702 static bool hint = false;
13703 inform (next_token->location,
13704 "%<<:%> is an alternate spelling for %<[%>."
13705 " Insert whitespace between %<<%> and %<::%>");
13706 if (!hint && !flag_permissive)
13708 inform (next_token->location, "(if you use %<-fpermissive%> "
13709 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13710 "accept your code)");
13711 hint = true;
13715 else
13717 /* Look for the `<' that starts the template-argument-list. */
13718 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13720 pop_deferring_access_checks ();
13721 return error_mark_node;
13723 /* Parse the arguments. */
13724 arguments = cp_parser_enclosed_template_argument_list (parser);
13727 /* Build a representation of the specialization. */
13728 if (identifier_p (templ))
13729 template_id = build_min_nt_loc (next_token->location,
13730 TEMPLATE_ID_EXPR,
13731 templ, arguments);
13732 else if (DECL_TYPE_TEMPLATE_P (templ)
13733 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13735 bool entering_scope;
13736 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13737 template (rather than some instantiation thereof) only if
13738 is not nested within some other construct. For example, in
13739 "template <typename T> void f(T) { A<T>::", A<T> is just an
13740 instantiation of A. */
13741 entering_scope = (template_parm_scope_p ()
13742 && cp_lexer_next_token_is (parser->lexer,
13743 CPP_SCOPE));
13744 template_id
13745 = finish_template_type (templ, arguments, entering_scope);
13747 else if (variable_template_p (templ))
13749 template_id = lookup_template_variable (templ, arguments);
13751 else
13753 /* If it's not a class-template or a template-template, it should be
13754 a function-template. */
13755 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13756 || TREE_CODE (templ) == OVERLOAD
13757 || BASELINK_P (templ)));
13759 template_id = lookup_template_function (templ, arguments);
13762 /* If parsing tentatively, replace the sequence of tokens that makes
13763 up the template-id with a CPP_TEMPLATE_ID token. That way,
13764 should we re-parse the token stream, we will not have to repeat
13765 the effort required to do the parse, nor will we issue duplicate
13766 error messages about problems during instantiation of the
13767 template. */
13768 if (start_of_id
13769 /* Don't do this if we had a parse error in a declarator; re-parsing
13770 might succeed if a name changes meaning (60361). */
13771 && !(cp_parser_error_occurred (parser)
13772 && cp_parser_parsing_tentatively (parser)
13773 && parser->in_declarator_p))
13775 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13777 /* Reset the contents of the START_OF_ID token. */
13778 token->type = CPP_TEMPLATE_ID;
13779 /* Retrieve any deferred checks. Do not pop this access checks yet
13780 so the memory will not be reclaimed during token replacing below. */
13781 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13782 token->u.tree_check_value->value = template_id;
13783 token->u.tree_check_value->checks = get_deferred_access_checks ();
13784 token->keyword = RID_MAX;
13786 /* Purge all subsequent tokens. */
13787 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13789 /* ??? Can we actually assume that, if template_id ==
13790 error_mark_node, we will have issued a diagnostic to the
13791 user, as opposed to simply marking the tentative parse as
13792 failed? */
13793 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13794 error_at (token->location, "parse error in template argument list");
13797 pop_to_parent_deferring_access_checks ();
13798 return template_id;
13801 /* Parse a template-name.
13803 template-name:
13804 identifier
13806 The standard should actually say:
13808 template-name:
13809 identifier
13810 operator-function-id
13812 A defect report has been filed about this issue.
13814 A conversion-function-id cannot be a template name because they cannot
13815 be part of a template-id. In fact, looking at this code:
13817 a.operator K<int>()
13819 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13820 It is impossible to call a templated conversion-function-id with an
13821 explicit argument list, since the only allowed template parameter is
13822 the type to which it is converting.
13824 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13825 `template' keyword, in a construction like:
13827 T::template f<3>()
13829 In that case `f' is taken to be a template-name, even though there
13830 is no way of knowing for sure.
13832 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13833 name refers to a set of overloaded functions, at least one of which
13834 is a template, or an IDENTIFIER_NODE with the name of the template,
13835 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13836 names are looked up inside uninstantiated templates. */
13838 static tree
13839 cp_parser_template_name (cp_parser* parser,
13840 bool template_keyword_p,
13841 bool check_dependency_p,
13842 bool is_declaration,
13843 enum tag_types tag_type,
13844 bool *is_identifier)
13846 tree identifier;
13847 tree decl;
13848 tree fns;
13849 cp_token *token = cp_lexer_peek_token (parser->lexer);
13851 /* If the next token is `operator', then we have either an
13852 operator-function-id or a conversion-function-id. */
13853 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13855 /* We don't know whether we're looking at an
13856 operator-function-id or a conversion-function-id. */
13857 cp_parser_parse_tentatively (parser);
13858 /* Try an operator-function-id. */
13859 identifier = cp_parser_operator_function_id (parser);
13860 /* If that didn't work, try a conversion-function-id. */
13861 if (!cp_parser_parse_definitely (parser))
13863 cp_parser_error (parser, "expected template-name");
13864 return error_mark_node;
13867 /* Look for the identifier. */
13868 else
13869 identifier = cp_parser_identifier (parser);
13871 /* If we didn't find an identifier, we don't have a template-id. */
13872 if (identifier == error_mark_node)
13873 return error_mark_node;
13875 /* If the name immediately followed the `template' keyword, then it
13876 is a template-name. However, if the next token is not `<', then
13877 we do not treat it as a template-name, since it is not being used
13878 as part of a template-id. This enables us to handle constructs
13879 like:
13881 template <typename T> struct S { S(); };
13882 template <typename T> S<T>::S();
13884 correctly. We would treat `S' as a template -- if it were `S<T>'
13885 -- but we do not if there is no `<'. */
13887 if (processing_template_decl
13888 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13890 /* In a declaration, in a dependent context, we pretend that the
13891 "template" keyword was present in order to improve error
13892 recovery. For example, given:
13894 template <typename T> void f(T::X<int>);
13896 we want to treat "X<int>" as a template-id. */
13897 if (is_declaration
13898 && !template_keyword_p
13899 && parser->scope && TYPE_P (parser->scope)
13900 && check_dependency_p
13901 && dependent_scope_p (parser->scope)
13902 /* Do not do this for dtors (or ctors), since they never
13903 need the template keyword before their name. */
13904 && !constructor_name_p (identifier, parser->scope))
13906 cp_token_position start = 0;
13908 /* Explain what went wrong. */
13909 error_at (token->location, "non-template %qD used as template",
13910 identifier);
13911 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13912 parser->scope, identifier);
13913 /* If parsing tentatively, find the location of the "<" token. */
13914 if (cp_parser_simulate_error (parser))
13915 start = cp_lexer_token_position (parser->lexer, true);
13916 /* Parse the template arguments so that we can issue error
13917 messages about them. */
13918 cp_lexer_consume_token (parser->lexer);
13919 cp_parser_enclosed_template_argument_list (parser);
13920 /* Skip tokens until we find a good place from which to
13921 continue parsing. */
13922 cp_parser_skip_to_closing_parenthesis (parser,
13923 /*recovering=*/true,
13924 /*or_comma=*/true,
13925 /*consume_paren=*/false);
13926 /* If parsing tentatively, permanently remove the
13927 template argument list. That will prevent duplicate
13928 error messages from being issued about the missing
13929 "template" keyword. */
13930 if (start)
13931 cp_lexer_purge_tokens_after (parser->lexer, start);
13932 if (is_identifier)
13933 *is_identifier = true;
13934 return identifier;
13937 /* If the "template" keyword is present, then there is generally
13938 no point in doing name-lookup, so we just return IDENTIFIER.
13939 But, if the qualifying scope is non-dependent then we can
13940 (and must) do name-lookup normally. */
13941 if (template_keyword_p
13942 && (!parser->scope
13943 || (TYPE_P (parser->scope)
13944 && dependent_type_p (parser->scope))))
13945 return identifier;
13948 /* Look up the name. */
13949 decl = cp_parser_lookup_name (parser, identifier,
13950 tag_type,
13951 /*is_template=*/true,
13952 /*is_namespace=*/false,
13953 check_dependency_p,
13954 /*ambiguous_decls=*/NULL,
13955 token->location);
13957 /* If DECL is a template, then the name was a template-name. */
13958 if (TREE_CODE (decl) == TEMPLATE_DECL)
13960 else
13962 tree fn = NULL_TREE;
13964 /* The standard does not explicitly indicate whether a name that
13965 names a set of overloaded declarations, some of which are
13966 templates, is a template-name. However, such a name should
13967 be a template-name; otherwise, there is no way to form a
13968 template-id for the overloaded templates. */
13969 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13970 if (TREE_CODE (fns) == OVERLOAD)
13971 for (fn = fns; fn; fn = OVL_NEXT (fn))
13972 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13973 break;
13975 if (!fn)
13977 /* The name does not name a template. */
13978 cp_parser_error (parser, "expected template-name");
13979 return error_mark_node;
13983 /* If DECL is dependent, and refers to a function, then just return
13984 its name; we will look it up again during template instantiation. */
13985 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13987 tree scope = ovl_scope (decl);
13988 if (TYPE_P (scope) && dependent_type_p (scope))
13989 return identifier;
13992 return decl;
13995 /* Parse a template-argument-list.
13997 template-argument-list:
13998 template-argument ... [opt]
13999 template-argument-list , template-argument ... [opt]
14001 Returns a TREE_VEC containing the arguments. */
14003 static tree
14004 cp_parser_template_argument_list (cp_parser* parser)
14006 tree fixed_args[10];
14007 unsigned n_args = 0;
14008 unsigned alloced = 10;
14009 tree *arg_ary = fixed_args;
14010 tree vec;
14011 bool saved_in_template_argument_list_p;
14012 bool saved_ice_p;
14013 bool saved_non_ice_p;
14015 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14016 parser->in_template_argument_list_p = true;
14017 /* Even if the template-id appears in an integral
14018 constant-expression, the contents of the argument list do
14019 not. */
14020 saved_ice_p = parser->integral_constant_expression_p;
14021 parser->integral_constant_expression_p = false;
14022 saved_non_ice_p = parser->non_integral_constant_expression_p;
14023 parser->non_integral_constant_expression_p = false;
14025 /* Parse the arguments. */
14028 tree argument;
14030 if (n_args)
14031 /* Consume the comma. */
14032 cp_lexer_consume_token (parser->lexer);
14034 /* Parse the template-argument. */
14035 argument = cp_parser_template_argument (parser);
14037 /* If the next token is an ellipsis, we're expanding a template
14038 argument pack. */
14039 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14041 if (argument == error_mark_node)
14043 cp_token *token = cp_lexer_peek_token (parser->lexer);
14044 error_at (token->location,
14045 "expected parameter pack before %<...%>");
14047 /* Consume the `...' token. */
14048 cp_lexer_consume_token (parser->lexer);
14050 /* Make the argument into a TYPE_PACK_EXPANSION or
14051 EXPR_PACK_EXPANSION. */
14052 argument = make_pack_expansion (argument);
14055 if (n_args == alloced)
14057 alloced *= 2;
14059 if (arg_ary == fixed_args)
14061 arg_ary = XNEWVEC (tree, alloced);
14062 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14064 else
14065 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14067 arg_ary[n_args++] = argument;
14069 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14071 vec = make_tree_vec (n_args);
14073 while (n_args--)
14074 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14076 if (arg_ary != fixed_args)
14077 free (arg_ary);
14078 parser->non_integral_constant_expression_p = saved_non_ice_p;
14079 parser->integral_constant_expression_p = saved_ice_p;
14080 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14081 #ifdef ENABLE_CHECKING
14082 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14083 #endif
14084 return vec;
14087 /* Parse a template-argument.
14089 template-argument:
14090 assignment-expression
14091 type-id
14092 id-expression
14094 The representation is that of an assignment-expression, type-id, or
14095 id-expression -- except that the qualified id-expression is
14096 evaluated, so that the value returned is either a DECL or an
14097 OVERLOAD.
14099 Although the standard says "assignment-expression", it forbids
14100 throw-expressions or assignments in the template argument.
14101 Therefore, we use "conditional-expression" instead. */
14103 static tree
14104 cp_parser_template_argument (cp_parser* parser)
14106 tree argument;
14107 bool template_p;
14108 bool address_p;
14109 bool maybe_type_id = false;
14110 cp_token *token = NULL, *argument_start_token = NULL;
14111 location_t loc = 0;
14112 cp_id_kind idk;
14114 /* There's really no way to know what we're looking at, so we just
14115 try each alternative in order.
14117 [temp.arg]
14119 In a template-argument, an ambiguity between a type-id and an
14120 expression is resolved to a type-id, regardless of the form of
14121 the corresponding template-parameter.
14123 Therefore, we try a type-id first. */
14124 cp_parser_parse_tentatively (parser);
14125 argument = cp_parser_template_type_arg (parser);
14126 /* If there was no error parsing the type-id but the next token is a
14127 '>>', our behavior depends on which dialect of C++ we're
14128 parsing. In C++98, we probably found a typo for '> >'. But there
14129 are type-id which are also valid expressions. For instance:
14131 struct X { int operator >> (int); };
14132 template <int V> struct Foo {};
14133 Foo<X () >> 5> r;
14135 Here 'X()' is a valid type-id of a function type, but the user just
14136 wanted to write the expression "X() >> 5". Thus, we remember that we
14137 found a valid type-id, but we still try to parse the argument as an
14138 expression to see what happens.
14140 In C++0x, the '>>' will be considered two separate '>'
14141 tokens. */
14142 if (!cp_parser_error_occurred (parser)
14143 && cxx_dialect == cxx98
14144 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14146 maybe_type_id = true;
14147 cp_parser_abort_tentative_parse (parser);
14149 else
14151 /* If the next token isn't a `,' or a `>', then this argument wasn't
14152 really finished. This means that the argument is not a valid
14153 type-id. */
14154 if (!cp_parser_next_token_ends_template_argument_p (parser))
14155 cp_parser_error (parser, "expected template-argument");
14156 /* If that worked, we're done. */
14157 if (cp_parser_parse_definitely (parser))
14158 return argument;
14160 /* We're still not sure what the argument will be. */
14161 cp_parser_parse_tentatively (parser);
14162 /* Try a template. */
14163 argument_start_token = cp_lexer_peek_token (parser->lexer);
14164 argument = cp_parser_id_expression (parser,
14165 /*template_keyword_p=*/false,
14166 /*check_dependency_p=*/true,
14167 &template_p,
14168 /*declarator_p=*/false,
14169 /*optional_p=*/false);
14170 /* If the next token isn't a `,' or a `>', then this argument wasn't
14171 really finished. */
14172 if (!cp_parser_next_token_ends_template_argument_p (parser))
14173 cp_parser_error (parser, "expected template-argument");
14174 if (!cp_parser_error_occurred (parser))
14176 /* Figure out what is being referred to. If the id-expression
14177 was for a class template specialization, then we will have a
14178 TYPE_DECL at this point. There is no need to do name lookup
14179 at this point in that case. */
14180 if (TREE_CODE (argument) != TYPE_DECL)
14181 argument = cp_parser_lookup_name (parser, argument,
14182 none_type,
14183 /*is_template=*/template_p,
14184 /*is_namespace=*/false,
14185 /*check_dependency=*/true,
14186 /*ambiguous_decls=*/NULL,
14187 argument_start_token->location);
14188 if (TREE_CODE (argument) != TEMPLATE_DECL
14189 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14190 cp_parser_error (parser, "expected template-name");
14192 if (cp_parser_parse_definitely (parser))
14193 return argument;
14194 /* It must be a non-type argument. There permitted cases are given
14195 in [temp.arg.nontype]:
14197 -- an integral constant-expression of integral or enumeration
14198 type; or
14200 -- the name of a non-type template-parameter; or
14202 -- the name of an object or function with external linkage...
14204 -- the address of an object or function with external linkage...
14206 -- a pointer to member... */
14207 /* Look for a non-type template parameter. */
14208 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14210 cp_parser_parse_tentatively (parser);
14211 argument = cp_parser_primary_expression (parser,
14212 /*address_p=*/false,
14213 /*cast_p=*/false,
14214 /*template_arg_p=*/true,
14215 &idk);
14216 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14217 || !cp_parser_next_token_ends_template_argument_p (parser))
14218 cp_parser_simulate_error (parser);
14219 if (cp_parser_parse_definitely (parser))
14220 return argument;
14223 /* If the next token is "&", the argument must be the address of an
14224 object or function with external linkage. */
14225 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14226 if (address_p)
14228 loc = cp_lexer_peek_token (parser->lexer)->location;
14229 cp_lexer_consume_token (parser->lexer);
14231 /* See if we might have an id-expression. */
14232 token = cp_lexer_peek_token (parser->lexer);
14233 if (token->type == CPP_NAME
14234 || token->keyword == RID_OPERATOR
14235 || token->type == CPP_SCOPE
14236 || token->type == CPP_TEMPLATE_ID
14237 || token->type == CPP_NESTED_NAME_SPECIFIER)
14239 cp_parser_parse_tentatively (parser);
14240 argument = cp_parser_primary_expression (parser,
14241 address_p,
14242 /*cast_p=*/false,
14243 /*template_arg_p=*/true,
14244 &idk);
14245 if (cp_parser_error_occurred (parser)
14246 || !cp_parser_next_token_ends_template_argument_p (parser))
14247 cp_parser_abort_tentative_parse (parser);
14248 else
14250 tree probe;
14252 if (INDIRECT_REF_P (argument))
14254 /* Strip the dereference temporarily. */
14255 gcc_assert (REFERENCE_REF_P (argument));
14256 argument = TREE_OPERAND (argument, 0);
14259 /* If we're in a template, we represent a qualified-id referring
14260 to a static data member as a SCOPE_REF even if the scope isn't
14261 dependent so that we can check access control later. */
14262 probe = argument;
14263 if (TREE_CODE (probe) == SCOPE_REF)
14264 probe = TREE_OPERAND (probe, 1);
14265 if (VAR_P (probe))
14267 /* A variable without external linkage might still be a
14268 valid constant-expression, so no error is issued here
14269 if the external-linkage check fails. */
14270 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14271 cp_parser_simulate_error (parser);
14273 else if (is_overloaded_fn (argument))
14274 /* All overloaded functions are allowed; if the external
14275 linkage test does not pass, an error will be issued
14276 later. */
14278 else if (address_p
14279 && (TREE_CODE (argument) == OFFSET_REF
14280 || TREE_CODE (argument) == SCOPE_REF))
14281 /* A pointer-to-member. */
14283 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14285 else
14286 cp_parser_simulate_error (parser);
14288 if (cp_parser_parse_definitely (parser))
14290 if (address_p)
14291 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14292 tf_warning_or_error);
14293 else
14294 argument = convert_from_reference (argument);
14295 return argument;
14299 /* If the argument started with "&", there are no other valid
14300 alternatives at this point. */
14301 if (address_p)
14303 cp_parser_error (parser, "invalid non-type template argument");
14304 return error_mark_node;
14307 /* If the argument wasn't successfully parsed as a type-id followed
14308 by '>>', the argument can only be a constant expression now.
14309 Otherwise, we try parsing the constant-expression tentatively,
14310 because the argument could really be a type-id. */
14311 if (maybe_type_id)
14312 cp_parser_parse_tentatively (parser);
14313 argument = cp_parser_constant_expression (parser);
14315 if (!maybe_type_id)
14316 return argument;
14317 if (!cp_parser_next_token_ends_template_argument_p (parser))
14318 cp_parser_error (parser, "expected template-argument");
14319 if (cp_parser_parse_definitely (parser))
14320 return argument;
14321 /* We did our best to parse the argument as a non type-id, but that
14322 was the only alternative that matched (albeit with a '>' after
14323 it). We can assume it's just a typo from the user, and a
14324 diagnostic will then be issued. */
14325 return cp_parser_template_type_arg (parser);
14328 /* Parse an explicit-instantiation.
14330 explicit-instantiation:
14331 template declaration
14333 Although the standard says `declaration', what it really means is:
14335 explicit-instantiation:
14336 template decl-specifier-seq [opt] declarator [opt] ;
14338 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14339 supposed to be allowed. A defect report has been filed about this
14340 issue.
14342 GNU Extension:
14344 explicit-instantiation:
14345 storage-class-specifier template
14346 decl-specifier-seq [opt] declarator [opt] ;
14347 function-specifier template
14348 decl-specifier-seq [opt] declarator [opt] ; */
14350 static void
14351 cp_parser_explicit_instantiation (cp_parser* parser)
14353 int declares_class_or_enum;
14354 cp_decl_specifier_seq decl_specifiers;
14355 tree extension_specifier = NULL_TREE;
14357 timevar_push (TV_TEMPLATE_INST);
14359 /* Look for an (optional) storage-class-specifier or
14360 function-specifier. */
14361 if (cp_parser_allow_gnu_extensions_p (parser))
14363 extension_specifier
14364 = cp_parser_storage_class_specifier_opt (parser);
14365 if (!extension_specifier)
14366 extension_specifier
14367 = cp_parser_function_specifier_opt (parser,
14368 /*decl_specs=*/NULL);
14371 /* Look for the `template' keyword. */
14372 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14373 /* Let the front end know that we are processing an explicit
14374 instantiation. */
14375 begin_explicit_instantiation ();
14376 /* [temp.explicit] says that we are supposed to ignore access
14377 control while processing explicit instantiation directives. */
14378 push_deferring_access_checks (dk_no_check);
14379 /* Parse a decl-specifier-seq. */
14380 cp_parser_decl_specifier_seq (parser,
14381 CP_PARSER_FLAGS_OPTIONAL,
14382 &decl_specifiers,
14383 &declares_class_or_enum);
14384 /* If there was exactly one decl-specifier, and it declared a class,
14385 and there's no declarator, then we have an explicit type
14386 instantiation. */
14387 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14389 tree type;
14391 type = check_tag_decl (&decl_specifiers,
14392 /*explicit_type_instantiation_p=*/true);
14393 /* Turn access control back on for names used during
14394 template instantiation. */
14395 pop_deferring_access_checks ();
14396 if (type)
14397 do_type_instantiation (type, extension_specifier,
14398 /*complain=*/tf_error);
14400 else
14402 cp_declarator *declarator;
14403 tree decl;
14405 /* Parse the declarator. */
14406 declarator
14407 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14408 /*ctor_dtor_or_conv_p=*/NULL,
14409 /*parenthesized_p=*/NULL,
14410 /*member_p=*/false,
14411 /*friend_p=*/false);
14412 if (declares_class_or_enum & 2)
14413 cp_parser_check_for_definition_in_return_type (declarator,
14414 decl_specifiers.type,
14415 decl_specifiers.locations[ds_type_spec]);
14416 if (declarator != cp_error_declarator)
14418 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14419 permerror (decl_specifiers.locations[ds_inline],
14420 "explicit instantiation shall not use"
14421 " %<inline%> specifier");
14422 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14423 permerror (decl_specifiers.locations[ds_constexpr],
14424 "explicit instantiation shall not use"
14425 " %<constexpr%> specifier");
14427 decl = grokdeclarator (declarator, &decl_specifiers,
14428 NORMAL, 0, &decl_specifiers.attributes);
14429 /* Turn access control back on for names used during
14430 template instantiation. */
14431 pop_deferring_access_checks ();
14432 /* Do the explicit instantiation. */
14433 do_decl_instantiation (decl, extension_specifier);
14435 else
14437 pop_deferring_access_checks ();
14438 /* Skip the body of the explicit instantiation. */
14439 cp_parser_skip_to_end_of_statement (parser);
14442 /* We're done with the instantiation. */
14443 end_explicit_instantiation ();
14445 cp_parser_consume_semicolon_at_end_of_statement (parser);
14447 timevar_pop (TV_TEMPLATE_INST);
14450 /* Parse an explicit-specialization.
14452 explicit-specialization:
14453 template < > declaration
14455 Although the standard says `declaration', what it really means is:
14457 explicit-specialization:
14458 template <> decl-specifier [opt] init-declarator [opt] ;
14459 template <> function-definition
14460 template <> explicit-specialization
14461 template <> template-declaration */
14463 static void
14464 cp_parser_explicit_specialization (cp_parser* parser)
14466 bool need_lang_pop;
14467 cp_token *token = cp_lexer_peek_token (parser->lexer);
14469 /* Look for the `template' keyword. */
14470 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14471 /* Look for the `<'. */
14472 cp_parser_require (parser, CPP_LESS, RT_LESS);
14473 /* Look for the `>'. */
14474 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14475 /* We have processed another parameter list. */
14476 ++parser->num_template_parameter_lists;
14477 /* [temp]
14479 A template ... explicit specialization ... shall not have C
14480 linkage. */
14481 if (current_lang_name == lang_name_c)
14483 error_at (token->location, "template specialization with C linkage");
14484 /* Give it C++ linkage to avoid confusing other parts of the
14485 front end. */
14486 push_lang_context (lang_name_cplusplus);
14487 need_lang_pop = true;
14489 else
14490 need_lang_pop = false;
14491 /* Let the front end know that we are beginning a specialization. */
14492 if (!begin_specialization ())
14494 end_specialization ();
14495 return;
14498 /* If the next keyword is `template', we need to figure out whether
14499 or not we're looking a template-declaration. */
14500 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14502 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14503 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14504 cp_parser_template_declaration_after_export (parser,
14505 /*member_p=*/false);
14506 else
14507 cp_parser_explicit_specialization (parser);
14509 else
14510 /* Parse the dependent declaration. */
14511 cp_parser_single_declaration (parser,
14512 /*checks=*/NULL,
14513 /*member_p=*/false,
14514 /*explicit_specialization_p=*/true,
14515 /*friend_p=*/NULL);
14516 /* We're done with the specialization. */
14517 end_specialization ();
14518 /* For the erroneous case of a template with C linkage, we pushed an
14519 implicit C++ linkage scope; exit that scope now. */
14520 if (need_lang_pop)
14521 pop_lang_context ();
14522 /* We're done with this parameter list. */
14523 --parser->num_template_parameter_lists;
14526 /* Parse a type-specifier.
14528 type-specifier:
14529 simple-type-specifier
14530 class-specifier
14531 enum-specifier
14532 elaborated-type-specifier
14533 cv-qualifier
14535 GNU Extension:
14537 type-specifier:
14538 __complex__
14540 Returns a representation of the type-specifier. For a
14541 class-specifier, enum-specifier, or elaborated-type-specifier, a
14542 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14544 The parser flags FLAGS is used to control type-specifier parsing.
14546 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14547 in a decl-specifier-seq.
14549 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14550 class-specifier, enum-specifier, or elaborated-type-specifier, then
14551 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14552 if a type is declared; 2 if it is defined. Otherwise, it is set to
14553 zero.
14555 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14556 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14557 is set to FALSE. */
14559 static tree
14560 cp_parser_type_specifier (cp_parser* parser,
14561 cp_parser_flags flags,
14562 cp_decl_specifier_seq *decl_specs,
14563 bool is_declaration,
14564 int* declares_class_or_enum,
14565 bool* is_cv_qualifier)
14567 tree type_spec = NULL_TREE;
14568 cp_token *token;
14569 enum rid keyword;
14570 cp_decl_spec ds = ds_last;
14572 /* Assume this type-specifier does not declare a new type. */
14573 if (declares_class_or_enum)
14574 *declares_class_or_enum = 0;
14575 /* And that it does not specify a cv-qualifier. */
14576 if (is_cv_qualifier)
14577 *is_cv_qualifier = false;
14578 /* Peek at the next token. */
14579 token = cp_lexer_peek_token (parser->lexer);
14581 /* If we're looking at a keyword, we can use that to guide the
14582 production we choose. */
14583 keyword = token->keyword;
14584 switch (keyword)
14586 case RID_ENUM:
14587 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14588 goto elaborated_type_specifier;
14590 /* Look for the enum-specifier. */
14591 type_spec = cp_parser_enum_specifier (parser);
14592 /* If that worked, we're done. */
14593 if (type_spec)
14595 if (declares_class_or_enum)
14596 *declares_class_or_enum = 2;
14597 if (decl_specs)
14598 cp_parser_set_decl_spec_type (decl_specs,
14599 type_spec,
14600 token,
14601 /*type_definition_p=*/true);
14602 return type_spec;
14604 else
14605 goto elaborated_type_specifier;
14607 /* Any of these indicate either a class-specifier, or an
14608 elaborated-type-specifier. */
14609 case RID_CLASS:
14610 case RID_STRUCT:
14611 case RID_UNION:
14612 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14613 goto elaborated_type_specifier;
14615 /* Parse tentatively so that we can back up if we don't find a
14616 class-specifier. */
14617 cp_parser_parse_tentatively (parser);
14618 /* Look for the class-specifier. */
14619 type_spec = cp_parser_class_specifier (parser);
14620 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14621 /* If that worked, we're done. */
14622 if (cp_parser_parse_definitely (parser))
14624 if (declares_class_or_enum)
14625 *declares_class_or_enum = 2;
14626 if (decl_specs)
14627 cp_parser_set_decl_spec_type (decl_specs,
14628 type_spec,
14629 token,
14630 /*type_definition_p=*/true);
14631 return type_spec;
14634 /* Fall through. */
14635 elaborated_type_specifier:
14636 /* We're declaring (not defining) a class or enum. */
14637 if (declares_class_or_enum)
14638 *declares_class_or_enum = 1;
14640 /* Fall through. */
14641 case RID_TYPENAME:
14642 /* Look for an elaborated-type-specifier. */
14643 type_spec
14644 = (cp_parser_elaborated_type_specifier
14645 (parser,
14646 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14647 is_declaration));
14648 if (decl_specs)
14649 cp_parser_set_decl_spec_type (decl_specs,
14650 type_spec,
14651 token,
14652 /*type_definition_p=*/false);
14653 return type_spec;
14655 case RID_CONST:
14656 ds = ds_const;
14657 if (is_cv_qualifier)
14658 *is_cv_qualifier = true;
14659 break;
14661 case RID_VOLATILE:
14662 ds = ds_volatile;
14663 if (is_cv_qualifier)
14664 *is_cv_qualifier = true;
14665 break;
14667 case RID_RESTRICT:
14668 ds = ds_restrict;
14669 if (is_cv_qualifier)
14670 *is_cv_qualifier = true;
14671 break;
14673 case RID_COMPLEX:
14674 /* The `__complex__' keyword is a GNU extension. */
14675 ds = ds_complex;
14676 break;
14678 default:
14679 break;
14682 /* Handle simple keywords. */
14683 if (ds != ds_last)
14685 if (decl_specs)
14687 set_and_check_decl_spec_loc (decl_specs, ds, token);
14688 decl_specs->any_specifiers_p = true;
14690 return cp_lexer_consume_token (parser->lexer)->u.value;
14693 /* If we do not already have a type-specifier, assume we are looking
14694 at a simple-type-specifier. */
14695 type_spec = cp_parser_simple_type_specifier (parser,
14696 decl_specs,
14697 flags);
14699 /* If we didn't find a type-specifier, and a type-specifier was not
14700 optional in this context, issue an error message. */
14701 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14703 cp_parser_error (parser, "expected type specifier");
14704 return error_mark_node;
14707 return type_spec;
14710 /* Parse a simple-type-specifier.
14712 simple-type-specifier:
14713 :: [opt] nested-name-specifier [opt] type-name
14714 :: [opt] nested-name-specifier template template-id
14715 char
14716 wchar_t
14717 bool
14718 short
14720 long
14721 signed
14722 unsigned
14723 float
14724 double
14725 void
14727 C++0x Extension:
14729 simple-type-specifier:
14730 auto
14731 decltype ( expression )
14732 char16_t
14733 char32_t
14734 __underlying_type ( type-id )
14736 GNU Extension:
14738 simple-type-specifier:
14739 __int128
14740 __typeof__ unary-expression
14741 __typeof__ ( type-id )
14742 __typeof__ ( type-id ) { initializer-list , [opt] }
14744 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14745 appropriately updated. */
14747 static tree
14748 cp_parser_simple_type_specifier (cp_parser* parser,
14749 cp_decl_specifier_seq *decl_specs,
14750 cp_parser_flags flags)
14752 tree type = NULL_TREE;
14753 cp_token *token;
14754 int idx;
14756 /* Peek at the next token. */
14757 token = cp_lexer_peek_token (parser->lexer);
14759 /* If we're looking at a keyword, things are easy. */
14760 switch (token->keyword)
14762 case RID_CHAR:
14763 if (decl_specs)
14764 decl_specs->explicit_char_p = true;
14765 type = char_type_node;
14766 break;
14767 case RID_CHAR16:
14768 type = char16_type_node;
14769 break;
14770 case RID_CHAR32:
14771 type = char32_type_node;
14772 break;
14773 case RID_WCHAR:
14774 type = wchar_type_node;
14775 break;
14776 case RID_BOOL:
14777 type = boolean_type_node;
14778 break;
14779 case RID_SHORT:
14780 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14781 type = short_integer_type_node;
14782 break;
14783 case RID_INT:
14784 if (decl_specs)
14785 decl_specs->explicit_int_p = true;
14786 type = integer_type_node;
14787 break;
14788 case RID_INT_N_0:
14789 case RID_INT_N_1:
14790 case RID_INT_N_2:
14791 case RID_INT_N_3:
14792 idx = token->keyword - RID_INT_N_0;
14793 if (! int_n_enabled_p [idx])
14794 break;
14795 if (decl_specs)
14797 decl_specs->explicit_intN_p = true;
14798 decl_specs->int_n_idx = idx;
14800 type = int_n_trees [idx].signed_type;
14801 break;
14802 case RID_LONG:
14803 if (decl_specs)
14804 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14805 type = long_integer_type_node;
14806 break;
14807 case RID_SIGNED:
14808 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14809 type = integer_type_node;
14810 break;
14811 case RID_UNSIGNED:
14812 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14813 type = unsigned_type_node;
14814 break;
14815 case RID_FLOAT:
14816 type = float_type_node;
14817 break;
14818 case RID_DOUBLE:
14819 type = double_type_node;
14820 break;
14821 case RID_VOID:
14822 type = void_type_node;
14823 break;
14825 case RID_AUTO:
14826 maybe_warn_cpp0x (CPP0X_AUTO);
14827 if (parser->auto_is_implicit_function_template_parm_p)
14829 type = synthesize_implicit_template_parm (parser);
14831 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14833 if (cxx_dialect < cxx14)
14834 pedwarn (location_of (type), 0,
14835 "use of %<auto%> in lambda parameter declaration "
14836 "only available with "
14837 "-std=c++14 or -std=gnu++14");
14839 else if (cxx_dialect < cxx14)
14840 pedwarn (location_of (type), 0,
14841 "use of %<auto%> in parameter declaration "
14842 "only available with "
14843 "-std=c++14 or -std=gnu++14");
14844 else
14845 pedwarn (location_of (type), OPT_Wpedantic,
14846 "ISO C++ forbids use of %<auto%> in parameter "
14847 "declaration");
14849 else
14850 type = make_auto ();
14851 break;
14853 case RID_DECLTYPE:
14854 /* Since DR 743, decltype can either be a simple-type-specifier by
14855 itself or begin a nested-name-specifier. Parsing it will replace
14856 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14857 handling below decide what to do. */
14858 cp_parser_decltype (parser);
14859 cp_lexer_set_token_position (parser->lexer, token);
14860 break;
14862 case RID_TYPEOF:
14863 /* Consume the `typeof' token. */
14864 cp_lexer_consume_token (parser->lexer);
14865 /* Parse the operand to `typeof'. */
14866 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14867 /* If it is not already a TYPE, take its type. */
14868 if (!TYPE_P (type))
14869 type = finish_typeof (type);
14871 if (decl_specs)
14872 cp_parser_set_decl_spec_type (decl_specs, type,
14873 token,
14874 /*type_definition_p=*/false);
14876 return type;
14878 case RID_UNDERLYING_TYPE:
14879 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14880 if (decl_specs)
14881 cp_parser_set_decl_spec_type (decl_specs, type,
14882 token,
14883 /*type_definition_p=*/false);
14885 return type;
14887 case RID_BASES:
14888 case RID_DIRECT_BASES:
14889 type = cp_parser_trait_expr (parser, token->keyword);
14890 if (decl_specs)
14891 cp_parser_set_decl_spec_type (decl_specs, type,
14892 token,
14893 /*type_definition_p=*/false);
14894 return type;
14895 default:
14896 break;
14899 /* If token is an already-parsed decltype not followed by ::,
14900 it's a simple-type-specifier. */
14901 if (token->type == CPP_DECLTYPE
14902 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14904 type = token->u.value;
14905 if (decl_specs)
14907 cp_parser_set_decl_spec_type (decl_specs, type,
14908 token,
14909 /*type_definition_p=*/false);
14910 /* Remember that we are handling a decltype in order to
14911 implement the resolution of DR 1510 when the argument
14912 isn't instantiation dependent. */
14913 decl_specs->decltype_p = true;
14915 cp_lexer_consume_token (parser->lexer);
14916 return type;
14919 /* If the type-specifier was for a built-in type, we're done. */
14920 if (type)
14922 /* Record the type. */
14923 if (decl_specs
14924 && (token->keyword != RID_SIGNED
14925 && token->keyword != RID_UNSIGNED
14926 && token->keyword != RID_SHORT
14927 && token->keyword != RID_LONG))
14928 cp_parser_set_decl_spec_type (decl_specs,
14929 type,
14930 token,
14931 /*type_definition_p=*/false);
14932 if (decl_specs)
14933 decl_specs->any_specifiers_p = true;
14935 /* Consume the token. */
14936 cp_lexer_consume_token (parser->lexer);
14938 /* There is no valid C++ program where a non-template type is
14939 followed by a "<". That usually indicates that the user thought
14940 that the type was a template. */
14941 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14942 token->location);
14944 return TYPE_NAME (type);
14947 /* The type-specifier must be a user-defined type. */
14948 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14950 bool qualified_p;
14951 bool global_p;
14953 /* Don't gobble tokens or issue error messages if this is an
14954 optional type-specifier. */
14955 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14956 cp_parser_parse_tentatively (parser);
14958 /* Look for the optional `::' operator. */
14959 global_p
14960 = (cp_parser_global_scope_opt (parser,
14961 /*current_scope_valid_p=*/false)
14962 != NULL_TREE);
14963 /* Look for the nested-name specifier. */
14964 qualified_p
14965 = (cp_parser_nested_name_specifier_opt (parser,
14966 /*typename_keyword_p=*/false,
14967 /*check_dependency_p=*/true,
14968 /*type_p=*/false,
14969 /*is_declaration=*/false)
14970 != NULL_TREE);
14971 token = cp_lexer_peek_token (parser->lexer);
14972 /* If we have seen a nested-name-specifier, and the next token
14973 is `template', then we are using the template-id production. */
14974 if (parser->scope
14975 && cp_parser_optional_template_keyword (parser))
14977 /* Look for the template-id. */
14978 type = cp_parser_template_id (parser,
14979 /*template_keyword_p=*/true,
14980 /*check_dependency_p=*/true,
14981 none_type,
14982 /*is_declaration=*/false);
14983 /* If the template-id did not name a type, we are out of
14984 luck. */
14985 if (TREE_CODE (type) != TYPE_DECL)
14987 cp_parser_error (parser, "expected template-id for type");
14988 type = NULL_TREE;
14991 /* Otherwise, look for a type-name. */
14992 else
14993 type = cp_parser_type_name (parser);
14994 /* Keep track of all name-lookups performed in class scopes. */
14995 if (type
14996 && !global_p
14997 && !qualified_p
14998 && TREE_CODE (type) == TYPE_DECL
14999 && identifier_p (DECL_NAME (type)))
15000 maybe_note_name_used_in_class (DECL_NAME (type), type);
15001 /* If it didn't work out, we don't have a TYPE. */
15002 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15003 && !cp_parser_parse_definitely (parser))
15004 type = NULL_TREE;
15005 if (type && decl_specs)
15006 cp_parser_set_decl_spec_type (decl_specs, type,
15007 token,
15008 /*type_definition_p=*/false);
15011 /* If we didn't get a type-name, issue an error message. */
15012 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15014 cp_parser_error (parser, "expected type-name");
15015 return error_mark_node;
15018 if (type && type != error_mark_node)
15020 /* See if TYPE is an Objective-C type, and if so, parse and
15021 accept any protocol references following it. Do this before
15022 the cp_parser_check_for_invalid_template_id() call, because
15023 Objective-C types can be followed by '<...>' which would
15024 enclose protocol names rather than template arguments, and so
15025 everything is fine. */
15026 if (c_dialect_objc () && !parser->scope
15027 && (objc_is_id (type) || objc_is_class_name (type)))
15029 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15030 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15032 /* Clobber the "unqualified" type previously entered into
15033 DECL_SPECS with the new, improved protocol-qualified version. */
15034 if (decl_specs)
15035 decl_specs->type = qual_type;
15037 return qual_type;
15040 /* There is no valid C++ program where a non-template type is
15041 followed by a "<". That usually indicates that the user
15042 thought that the type was a template. */
15043 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15044 none_type,
15045 token->location);
15048 return type;
15051 /* Parse a type-name.
15053 type-name:
15054 class-name
15055 enum-name
15056 typedef-name
15057 simple-template-id [in c++0x]
15059 enum-name:
15060 identifier
15062 typedef-name:
15063 identifier
15065 Returns a TYPE_DECL for the type. */
15067 static tree
15068 cp_parser_type_name (cp_parser* parser)
15070 tree type_decl;
15072 /* We can't know yet whether it is a class-name or not. */
15073 cp_parser_parse_tentatively (parser);
15074 /* Try a class-name. */
15075 type_decl = cp_parser_class_name (parser,
15076 /*typename_keyword_p=*/false,
15077 /*template_keyword_p=*/false,
15078 none_type,
15079 /*check_dependency_p=*/true,
15080 /*class_head_p=*/false,
15081 /*is_declaration=*/false);
15082 /* If it's not a class-name, keep looking. */
15083 if (!cp_parser_parse_definitely (parser))
15085 if (cxx_dialect < cxx11)
15086 /* It must be a typedef-name or an enum-name. */
15087 return cp_parser_nonclass_name (parser);
15089 cp_parser_parse_tentatively (parser);
15090 /* It is either a simple-template-id representing an
15091 instantiation of an alias template... */
15092 type_decl = cp_parser_template_id (parser,
15093 /*template_keyword_p=*/false,
15094 /*check_dependency_p=*/true,
15095 none_type,
15096 /*is_declaration=*/false);
15097 /* Note that this must be an instantiation of an alias template
15098 because [temp.names]/6 says:
15100 A template-id that names an alias template specialization
15101 is a type-name.
15103 Whereas [temp.names]/7 says:
15105 A simple-template-id that names a class template
15106 specialization is a class-name. */
15107 if (type_decl != NULL_TREE
15108 && TREE_CODE (type_decl) == TYPE_DECL
15109 && TYPE_DECL_ALIAS_P (type_decl))
15110 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15111 else
15112 cp_parser_simulate_error (parser);
15114 if (!cp_parser_parse_definitely (parser))
15115 /* ... Or a typedef-name or an enum-name. */
15116 return cp_parser_nonclass_name (parser);
15119 return type_decl;
15122 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15124 enum-name:
15125 identifier
15127 typedef-name:
15128 identifier
15130 Returns a TYPE_DECL for the type. */
15132 static tree
15133 cp_parser_nonclass_name (cp_parser* parser)
15135 tree type_decl;
15136 tree identifier;
15138 cp_token *token = cp_lexer_peek_token (parser->lexer);
15139 identifier = cp_parser_identifier (parser);
15140 if (identifier == error_mark_node)
15141 return error_mark_node;
15143 /* Look up the type-name. */
15144 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15146 type_decl = strip_using_decl (type_decl);
15148 if (TREE_CODE (type_decl) != TYPE_DECL
15149 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15151 /* See if this is an Objective-C type. */
15152 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15153 tree type = objc_get_protocol_qualified_type (identifier, protos);
15154 if (type)
15155 type_decl = TYPE_NAME (type);
15158 /* Issue an error if we did not find a type-name. */
15159 if (TREE_CODE (type_decl) != TYPE_DECL
15160 /* In Objective-C, we have the complication that class names are
15161 normally type names and start declarations (eg, the
15162 "NSObject" in "NSObject *object;"), but can be used in an
15163 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15164 is an expression. So, a classname followed by a dot is not a
15165 valid type-name. */
15166 || (objc_is_class_name (TREE_TYPE (type_decl))
15167 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15169 if (!cp_parser_simulate_error (parser))
15170 cp_parser_name_lookup_error (parser, identifier, type_decl,
15171 NLE_TYPE, token->location);
15172 return error_mark_node;
15174 /* Remember that the name was used in the definition of the
15175 current class so that we can check later to see if the
15176 meaning would have been different after the class was
15177 entirely defined. */
15178 else if (type_decl != error_mark_node
15179 && !parser->scope)
15180 maybe_note_name_used_in_class (identifier, type_decl);
15182 return type_decl;
15185 /* Parse an elaborated-type-specifier. Note that the grammar given
15186 here incorporates the resolution to DR68.
15188 elaborated-type-specifier:
15189 class-key :: [opt] nested-name-specifier [opt] identifier
15190 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15191 enum-key :: [opt] nested-name-specifier [opt] identifier
15192 typename :: [opt] nested-name-specifier identifier
15193 typename :: [opt] nested-name-specifier template [opt]
15194 template-id
15196 GNU extension:
15198 elaborated-type-specifier:
15199 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15200 class-key attributes :: [opt] nested-name-specifier [opt]
15201 template [opt] template-id
15202 enum attributes :: [opt] nested-name-specifier [opt] identifier
15204 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15205 declared `friend'. If IS_DECLARATION is TRUE, then this
15206 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15207 something is being declared.
15209 Returns the TYPE specified. */
15211 static tree
15212 cp_parser_elaborated_type_specifier (cp_parser* parser,
15213 bool is_friend,
15214 bool is_declaration)
15216 enum tag_types tag_type;
15217 tree identifier;
15218 tree type = NULL_TREE;
15219 tree attributes = NULL_TREE;
15220 tree globalscope;
15221 cp_token *token = NULL;
15223 /* See if we're looking at the `enum' keyword. */
15224 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15226 /* Consume the `enum' token. */
15227 cp_lexer_consume_token (parser->lexer);
15228 /* Remember that it's an enumeration type. */
15229 tag_type = enum_type;
15230 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15231 enums) is used here. */
15232 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15233 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15235 pedwarn (input_location, 0, "elaborated-type-specifier "
15236 "for a scoped enum must not use the %<%D%> keyword",
15237 cp_lexer_peek_token (parser->lexer)->u.value);
15238 /* Consume the `struct' or `class' and parse it anyway. */
15239 cp_lexer_consume_token (parser->lexer);
15241 /* Parse the attributes. */
15242 attributes = cp_parser_attributes_opt (parser);
15244 /* Or, it might be `typename'. */
15245 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15246 RID_TYPENAME))
15248 /* Consume the `typename' token. */
15249 cp_lexer_consume_token (parser->lexer);
15250 /* Remember that it's a `typename' type. */
15251 tag_type = typename_type;
15253 /* Otherwise it must be a class-key. */
15254 else
15256 tag_type = cp_parser_class_key (parser);
15257 if (tag_type == none_type)
15258 return error_mark_node;
15259 /* Parse the attributes. */
15260 attributes = cp_parser_attributes_opt (parser);
15263 /* Look for the `::' operator. */
15264 globalscope = cp_parser_global_scope_opt (parser,
15265 /*current_scope_valid_p=*/false);
15266 /* Look for the nested-name-specifier. */
15267 if (tag_type == typename_type && !globalscope)
15269 if (!cp_parser_nested_name_specifier (parser,
15270 /*typename_keyword_p=*/true,
15271 /*check_dependency_p=*/true,
15272 /*type_p=*/true,
15273 is_declaration))
15274 return error_mark_node;
15276 else
15277 /* Even though `typename' is not present, the proposed resolution
15278 to Core Issue 180 says that in `class A<T>::B', `B' should be
15279 considered a type-name, even if `A<T>' is dependent. */
15280 cp_parser_nested_name_specifier_opt (parser,
15281 /*typename_keyword_p=*/true,
15282 /*check_dependency_p=*/true,
15283 /*type_p=*/true,
15284 is_declaration);
15285 /* For everything but enumeration types, consider a template-id.
15286 For an enumeration type, consider only a plain identifier. */
15287 if (tag_type != enum_type)
15289 bool template_p = false;
15290 tree decl;
15292 /* Allow the `template' keyword. */
15293 template_p = cp_parser_optional_template_keyword (parser);
15294 /* If we didn't see `template', we don't know if there's a
15295 template-id or not. */
15296 if (!template_p)
15297 cp_parser_parse_tentatively (parser);
15298 /* Parse the template-id. */
15299 token = cp_lexer_peek_token (parser->lexer);
15300 decl = cp_parser_template_id (parser, template_p,
15301 /*check_dependency_p=*/true,
15302 tag_type,
15303 is_declaration);
15304 /* If we didn't find a template-id, look for an ordinary
15305 identifier. */
15306 if (!template_p && !cp_parser_parse_definitely (parser))
15308 /* We can get here when cp_parser_template_id, called by
15309 cp_parser_class_name with tag_type == none_type, succeeds
15310 and caches a BASELINK. Then, when called again here,
15311 instead of failing and returning an error_mark_node
15312 returns it (see template/typename17.C in C++11).
15313 ??? Could we diagnose this earlier? */
15314 else if (tag_type == typename_type && BASELINK_P (decl))
15316 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15317 type = error_mark_node;
15319 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15320 in effect, then we must assume that, upon instantiation, the
15321 template will correspond to a class. */
15322 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15323 && tag_type == typename_type)
15324 type = make_typename_type (parser->scope, decl,
15325 typename_type,
15326 /*complain=*/tf_error);
15327 /* If the `typename' keyword is in effect and DECL is not a type
15328 decl, then type is non existent. */
15329 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15331 else if (TREE_CODE (decl) == TYPE_DECL)
15332 type = check_elaborated_type_specifier (tag_type, decl,
15333 /*allow_template_p=*/true);
15334 else if (decl == error_mark_node)
15335 type = error_mark_node;
15338 if (!type)
15340 token = cp_lexer_peek_token (parser->lexer);
15341 identifier = cp_parser_identifier (parser);
15343 if (identifier == error_mark_node)
15345 parser->scope = NULL_TREE;
15346 return error_mark_node;
15349 /* For a `typename', we needn't call xref_tag. */
15350 if (tag_type == typename_type
15351 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15352 return cp_parser_make_typename_type (parser, identifier,
15353 token->location);
15355 /* Template parameter lists apply only if we are not within a
15356 function parameter list. */
15357 bool template_parm_lists_apply
15358 = parser->num_template_parameter_lists;
15359 if (template_parm_lists_apply)
15360 for (cp_binding_level *s = current_binding_level;
15361 s && s->kind != sk_template_parms;
15362 s = s->level_chain)
15363 if (s->kind == sk_function_parms)
15364 template_parm_lists_apply = false;
15366 /* Look up a qualified name in the usual way. */
15367 if (parser->scope)
15369 tree decl;
15370 tree ambiguous_decls;
15372 decl = cp_parser_lookup_name (parser, identifier,
15373 tag_type,
15374 /*is_template=*/false,
15375 /*is_namespace=*/false,
15376 /*check_dependency=*/true,
15377 &ambiguous_decls,
15378 token->location);
15380 /* If the lookup was ambiguous, an error will already have been
15381 issued. */
15382 if (ambiguous_decls)
15383 return error_mark_node;
15385 /* If we are parsing friend declaration, DECL may be a
15386 TEMPLATE_DECL tree node here. However, we need to check
15387 whether this TEMPLATE_DECL results in valid code. Consider
15388 the following example:
15390 namespace N {
15391 template <class T> class C {};
15393 class X {
15394 template <class T> friend class N::C; // #1, valid code
15396 template <class T> class Y {
15397 friend class N::C; // #2, invalid code
15400 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15401 name lookup of `N::C'. We see that friend declaration must
15402 be template for the code to be valid. Note that
15403 processing_template_decl does not work here since it is
15404 always 1 for the above two cases. */
15406 decl = (cp_parser_maybe_treat_template_as_class
15407 (decl, /*tag_name_p=*/is_friend
15408 && template_parm_lists_apply));
15410 if (TREE_CODE (decl) != TYPE_DECL)
15412 cp_parser_diagnose_invalid_type_name (parser,
15413 identifier,
15414 token->location);
15415 return error_mark_node;
15418 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15420 bool allow_template = (template_parm_lists_apply
15421 || DECL_SELF_REFERENCE_P (decl));
15422 type = check_elaborated_type_specifier (tag_type, decl,
15423 allow_template);
15425 if (type == error_mark_node)
15426 return error_mark_node;
15429 /* Forward declarations of nested types, such as
15431 class C1::C2;
15432 class C1::C2::C3;
15434 are invalid unless all components preceding the final '::'
15435 are complete. If all enclosing types are complete, these
15436 declarations become merely pointless.
15438 Invalid forward declarations of nested types are errors
15439 caught elsewhere in parsing. Those that are pointless arrive
15440 here. */
15442 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15443 && !is_friend && !processing_explicit_instantiation)
15444 warning (0, "declaration %qD does not declare anything", decl);
15446 type = TREE_TYPE (decl);
15448 else
15450 /* An elaborated-type-specifier sometimes introduces a new type and
15451 sometimes names an existing type. Normally, the rule is that it
15452 introduces a new type only if there is not an existing type of
15453 the same name already in scope. For example, given:
15455 struct S {};
15456 void f() { struct S s; }
15458 the `struct S' in the body of `f' is the same `struct S' as in
15459 the global scope; the existing definition is used. However, if
15460 there were no global declaration, this would introduce a new
15461 local class named `S'.
15463 An exception to this rule applies to the following code:
15465 namespace N { struct S; }
15467 Here, the elaborated-type-specifier names a new type
15468 unconditionally; even if there is already an `S' in the
15469 containing scope this declaration names a new type.
15470 This exception only applies if the elaborated-type-specifier
15471 forms the complete declaration:
15473 [class.name]
15475 A declaration consisting solely of `class-key identifier ;' is
15476 either a redeclaration of the name in the current scope or a
15477 forward declaration of the identifier as a class name. It
15478 introduces the name into the current scope.
15480 We are in this situation precisely when the next token is a `;'.
15482 An exception to the exception is that a `friend' declaration does
15483 *not* name a new type; i.e., given:
15485 struct S { friend struct T; };
15487 `T' is not a new type in the scope of `S'.
15489 Also, `new struct S' or `sizeof (struct S)' never results in the
15490 definition of a new type; a new type can only be declared in a
15491 declaration context. */
15493 tag_scope ts;
15494 bool template_p;
15496 if (is_friend)
15497 /* Friends have special name lookup rules. */
15498 ts = ts_within_enclosing_non_class;
15499 else if (is_declaration
15500 && cp_lexer_next_token_is (parser->lexer,
15501 CPP_SEMICOLON))
15502 /* This is a `class-key identifier ;' */
15503 ts = ts_current;
15504 else
15505 ts = ts_global;
15507 template_p =
15508 (template_parm_lists_apply
15509 && (cp_parser_next_token_starts_class_definition_p (parser)
15510 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15511 /* An unqualified name was used to reference this type, so
15512 there were no qualifying templates. */
15513 if (template_parm_lists_apply
15514 && !cp_parser_check_template_parameters (parser,
15515 /*num_templates=*/0,
15516 token->location,
15517 /*declarator=*/NULL))
15518 return error_mark_node;
15519 type = xref_tag (tag_type, identifier, ts, template_p);
15523 if (type == error_mark_node)
15524 return error_mark_node;
15526 /* Allow attributes on forward declarations of classes. */
15527 if (attributes)
15529 if (TREE_CODE (type) == TYPENAME_TYPE)
15530 warning (OPT_Wattributes,
15531 "attributes ignored on uninstantiated type");
15532 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15533 && ! processing_explicit_instantiation)
15534 warning (OPT_Wattributes,
15535 "attributes ignored on template instantiation");
15536 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15537 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15538 else
15539 warning (OPT_Wattributes,
15540 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15543 if (tag_type != enum_type)
15545 /* Indicate whether this class was declared as a `class' or as a
15546 `struct'. */
15547 if (TREE_CODE (type) == RECORD_TYPE)
15548 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15549 cp_parser_check_class_key (tag_type, type);
15552 /* A "<" cannot follow an elaborated type specifier. If that
15553 happens, the user was probably trying to form a template-id. */
15554 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15555 token->location);
15557 return type;
15560 /* Parse an enum-specifier.
15562 enum-specifier:
15563 enum-head { enumerator-list [opt] }
15564 enum-head { enumerator-list , } [C++0x]
15566 enum-head:
15567 enum-key identifier [opt] enum-base [opt]
15568 enum-key nested-name-specifier identifier enum-base [opt]
15570 enum-key:
15571 enum
15572 enum class [C++0x]
15573 enum struct [C++0x]
15575 enum-base: [C++0x]
15576 : type-specifier-seq
15578 opaque-enum-specifier:
15579 enum-key identifier enum-base [opt] ;
15581 GNU Extensions:
15582 enum-key attributes[opt] identifier [opt] enum-base [opt]
15583 { enumerator-list [opt] }attributes[opt]
15584 enum-key attributes[opt] identifier [opt] enum-base [opt]
15585 { enumerator-list, }attributes[opt] [C++0x]
15587 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15588 if the token stream isn't an enum-specifier after all. */
15590 static tree
15591 cp_parser_enum_specifier (cp_parser* parser)
15593 tree identifier;
15594 tree type = NULL_TREE;
15595 tree prev_scope;
15596 tree nested_name_specifier = NULL_TREE;
15597 tree attributes;
15598 bool scoped_enum_p = false;
15599 bool has_underlying_type = false;
15600 bool nested_being_defined = false;
15601 bool new_value_list = false;
15602 bool is_new_type = false;
15603 bool is_anonymous = false;
15604 tree underlying_type = NULL_TREE;
15605 cp_token *type_start_token = NULL;
15606 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15608 parser->colon_corrects_to_scope_p = false;
15610 /* Parse tentatively so that we can back up if we don't find a
15611 enum-specifier. */
15612 cp_parser_parse_tentatively (parser);
15614 /* Caller guarantees that the current token is 'enum', an identifier
15615 possibly follows, and the token after that is an opening brace.
15616 If we don't have an identifier, fabricate an anonymous name for
15617 the enumeration being defined. */
15618 cp_lexer_consume_token (parser->lexer);
15620 /* Parse the "class" or "struct", which indicates a scoped
15621 enumeration type in C++0x. */
15622 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15623 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15625 if (cxx_dialect < cxx11)
15626 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15628 /* Consume the `struct' or `class' token. */
15629 cp_lexer_consume_token (parser->lexer);
15631 scoped_enum_p = true;
15634 attributes = cp_parser_attributes_opt (parser);
15636 /* Clear the qualification. */
15637 parser->scope = NULL_TREE;
15638 parser->qualifying_scope = NULL_TREE;
15639 parser->object_scope = NULL_TREE;
15641 /* Figure out in what scope the declaration is being placed. */
15642 prev_scope = current_scope ();
15644 type_start_token = cp_lexer_peek_token (parser->lexer);
15646 push_deferring_access_checks (dk_no_check);
15647 nested_name_specifier
15648 = cp_parser_nested_name_specifier_opt (parser,
15649 /*typename_keyword_p=*/true,
15650 /*check_dependency_p=*/false,
15651 /*type_p=*/false,
15652 /*is_declaration=*/false);
15654 if (nested_name_specifier)
15656 tree name;
15658 identifier = cp_parser_identifier (parser);
15659 name = cp_parser_lookup_name (parser, identifier,
15660 enum_type,
15661 /*is_template=*/false,
15662 /*is_namespace=*/false,
15663 /*check_dependency=*/true,
15664 /*ambiguous_decls=*/NULL,
15665 input_location);
15666 if (name && name != error_mark_node)
15668 type = TREE_TYPE (name);
15669 if (TREE_CODE (type) == TYPENAME_TYPE)
15671 /* Are template enums allowed in ISO? */
15672 if (template_parm_scope_p ())
15673 pedwarn (type_start_token->location, OPT_Wpedantic,
15674 "%qD is an enumeration template", name);
15675 /* ignore a typename reference, for it will be solved by name
15676 in start_enum. */
15677 type = NULL_TREE;
15680 else if (nested_name_specifier == error_mark_node)
15681 /* We already issued an error. */;
15682 else
15683 error_at (type_start_token->location,
15684 "%qD is not an enumerator-name", identifier);
15686 else
15688 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15689 identifier = cp_parser_identifier (parser);
15690 else
15692 identifier = make_anon_name ();
15693 is_anonymous = true;
15694 if (scoped_enum_p)
15695 error_at (type_start_token->location,
15696 "anonymous scoped enum is not allowed");
15699 pop_deferring_access_checks ();
15701 /* Check for the `:' that denotes a specified underlying type in C++0x.
15702 Note that a ':' could also indicate a bitfield width, however. */
15703 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15705 cp_decl_specifier_seq type_specifiers;
15707 /* Consume the `:'. */
15708 cp_lexer_consume_token (parser->lexer);
15710 /* Parse the type-specifier-seq. */
15711 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15712 /*is_trailing_return=*/false,
15713 &type_specifiers);
15715 /* At this point this is surely not elaborated type specifier. */
15716 if (!cp_parser_parse_definitely (parser))
15717 return NULL_TREE;
15719 if (cxx_dialect < cxx11)
15720 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15722 has_underlying_type = true;
15724 /* If that didn't work, stop. */
15725 if (type_specifiers.type != error_mark_node)
15727 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15728 /*initialized=*/0, NULL);
15729 if (underlying_type == error_mark_node
15730 || check_for_bare_parameter_packs (underlying_type))
15731 underlying_type = NULL_TREE;
15735 /* Look for the `{' but don't consume it yet. */
15736 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15738 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15740 cp_parser_error (parser, "expected %<{%>");
15741 if (has_underlying_type)
15743 type = NULL_TREE;
15744 goto out;
15747 /* An opaque-enum-specifier must have a ';' here. */
15748 if ((scoped_enum_p || underlying_type)
15749 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15751 cp_parser_error (parser, "expected %<;%> or %<{%>");
15752 if (has_underlying_type)
15754 type = NULL_TREE;
15755 goto out;
15760 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15761 return NULL_TREE;
15763 if (nested_name_specifier)
15765 if (CLASS_TYPE_P (nested_name_specifier))
15767 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15768 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15769 push_scope (nested_name_specifier);
15771 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15773 push_nested_namespace (nested_name_specifier);
15777 /* Issue an error message if type-definitions are forbidden here. */
15778 if (!cp_parser_check_type_definition (parser))
15779 type = error_mark_node;
15780 else
15781 /* Create the new type. We do this before consuming the opening
15782 brace so the enum will be recorded as being on the line of its
15783 tag (or the 'enum' keyword, if there is no tag). */
15784 type = start_enum (identifier, type, underlying_type,
15785 scoped_enum_p, &is_new_type);
15787 /* If the next token is not '{' it is an opaque-enum-specifier or an
15788 elaborated-type-specifier. */
15789 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15791 timevar_push (TV_PARSE_ENUM);
15792 if (nested_name_specifier
15793 && nested_name_specifier != error_mark_node)
15795 /* The following catches invalid code such as:
15796 enum class S<int>::E { A, B, C }; */
15797 if (!processing_specialization
15798 && CLASS_TYPE_P (nested_name_specifier)
15799 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15800 error_at (type_start_token->location, "cannot add an enumerator "
15801 "list to a template instantiation");
15803 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15805 error_at (type_start_token->location,
15806 "%<%T::%E%> has not been declared",
15807 TYPE_CONTEXT (nested_name_specifier),
15808 nested_name_specifier);
15809 type = error_mark_node;
15811 /* If that scope does not contain the scope in which the
15812 class was originally declared, the program is invalid. */
15813 else if (prev_scope && !is_ancestor (prev_scope,
15814 nested_name_specifier))
15816 if (at_namespace_scope_p ())
15817 error_at (type_start_token->location,
15818 "declaration of %qD in namespace %qD which does not "
15819 "enclose %qD",
15820 type, prev_scope, nested_name_specifier);
15821 else
15822 error_at (type_start_token->location,
15823 "declaration of %qD in %qD which does not "
15824 "enclose %qD",
15825 type, prev_scope, nested_name_specifier);
15826 type = error_mark_node;
15830 if (scoped_enum_p)
15831 begin_scope (sk_scoped_enum, type);
15833 /* Consume the opening brace. */
15834 cp_lexer_consume_token (parser->lexer);
15836 if (type == error_mark_node)
15837 ; /* Nothing to add */
15838 else if (OPAQUE_ENUM_P (type)
15839 || (cxx_dialect > cxx98 && processing_specialization))
15841 new_value_list = true;
15842 SET_OPAQUE_ENUM_P (type, false);
15843 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15845 else
15847 error_at (type_start_token->location,
15848 "multiple definition of %q#T", type);
15849 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15850 "previous definition here");
15851 type = error_mark_node;
15854 if (type == error_mark_node)
15855 cp_parser_skip_to_end_of_block_or_statement (parser);
15856 /* If the next token is not '}', then there are some enumerators. */
15857 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15859 if (is_anonymous && !scoped_enum_p)
15860 pedwarn (type_start_token->location, OPT_Wpedantic,
15861 "ISO C++ forbids empty anonymous enum");
15863 else
15864 cp_parser_enumerator_list (parser, type);
15866 /* Consume the final '}'. */
15867 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15869 if (scoped_enum_p)
15870 finish_scope ();
15871 timevar_pop (TV_PARSE_ENUM);
15873 else
15875 /* If a ';' follows, then it is an opaque-enum-specifier
15876 and additional restrictions apply. */
15877 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15879 if (is_anonymous)
15880 error_at (type_start_token->location,
15881 "opaque-enum-specifier without name");
15882 else if (nested_name_specifier)
15883 error_at (type_start_token->location,
15884 "opaque-enum-specifier must use a simple identifier");
15888 /* Look for trailing attributes to apply to this enumeration, and
15889 apply them if appropriate. */
15890 if (cp_parser_allow_gnu_extensions_p (parser))
15892 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15893 trailing_attr = chainon (trailing_attr, attributes);
15894 cplus_decl_attributes (&type,
15895 trailing_attr,
15896 (int) ATTR_FLAG_TYPE_IN_PLACE);
15899 /* Finish up the enumeration. */
15900 if (type != error_mark_node)
15902 if (new_value_list)
15903 finish_enum_value_list (type);
15904 if (is_new_type)
15905 finish_enum (type);
15908 if (nested_name_specifier)
15910 if (CLASS_TYPE_P (nested_name_specifier))
15912 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15913 pop_scope (nested_name_specifier);
15915 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15917 pop_nested_namespace (nested_name_specifier);
15920 out:
15921 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15922 return type;
15925 /* Parse an enumerator-list. The enumerators all have the indicated
15926 TYPE.
15928 enumerator-list:
15929 enumerator-definition
15930 enumerator-list , enumerator-definition */
15932 static void
15933 cp_parser_enumerator_list (cp_parser* parser, tree type)
15935 while (true)
15937 /* Parse an enumerator-definition. */
15938 cp_parser_enumerator_definition (parser, type);
15940 /* If the next token is not a ',', we've reached the end of
15941 the list. */
15942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15943 break;
15944 /* Otherwise, consume the `,' and keep going. */
15945 cp_lexer_consume_token (parser->lexer);
15946 /* If the next token is a `}', there is a trailing comma. */
15947 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15949 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15950 pedwarn (input_location, OPT_Wpedantic,
15951 "comma at end of enumerator list");
15952 break;
15957 /* Parse an enumerator-definition. The enumerator has the indicated
15958 TYPE.
15960 enumerator-definition:
15961 enumerator
15962 enumerator = constant-expression
15964 enumerator:
15965 identifier */
15967 static void
15968 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15970 tree identifier;
15971 tree value;
15972 location_t loc;
15974 /* Save the input location because we are interested in the location
15975 of the identifier and not the location of the explicit value. */
15976 loc = cp_lexer_peek_token (parser->lexer)->location;
15978 /* Look for the identifier. */
15979 identifier = cp_parser_identifier (parser);
15980 if (identifier == error_mark_node)
15981 return;
15983 /* If the next token is an '=', then there is an explicit value. */
15984 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15986 /* Consume the `=' token. */
15987 cp_lexer_consume_token (parser->lexer);
15988 /* Parse the value. */
15989 value = cp_parser_constant_expression (parser);
15991 else
15992 value = NULL_TREE;
15994 /* If we are processing a template, make sure the initializer of the
15995 enumerator doesn't contain any bare template parameter pack. */
15996 if (check_for_bare_parameter_packs (value))
15997 value = error_mark_node;
15999 /* integral_constant_value will pull out this expression, so make sure
16000 it's folded as appropriate. */
16001 value = fold_non_dependent_expr (value);
16003 /* Create the enumerator. */
16004 build_enumerator (identifier, value, type, loc);
16007 /* Parse a namespace-name.
16009 namespace-name:
16010 original-namespace-name
16011 namespace-alias
16013 Returns the NAMESPACE_DECL for the namespace. */
16015 static tree
16016 cp_parser_namespace_name (cp_parser* parser)
16018 tree identifier;
16019 tree namespace_decl;
16021 cp_token *token = cp_lexer_peek_token (parser->lexer);
16023 /* Get the name of the namespace. */
16024 identifier = cp_parser_identifier (parser);
16025 if (identifier == error_mark_node)
16026 return error_mark_node;
16028 /* Look up the identifier in the currently active scope. Look only
16029 for namespaces, due to:
16031 [basic.lookup.udir]
16033 When looking up a namespace-name in a using-directive or alias
16034 definition, only namespace names are considered.
16036 And:
16038 [basic.lookup.qual]
16040 During the lookup of a name preceding the :: scope resolution
16041 operator, object, function, and enumerator names are ignored.
16043 (Note that cp_parser_qualifying_entity only calls this
16044 function if the token after the name is the scope resolution
16045 operator.) */
16046 namespace_decl = cp_parser_lookup_name (parser, identifier,
16047 none_type,
16048 /*is_template=*/false,
16049 /*is_namespace=*/true,
16050 /*check_dependency=*/true,
16051 /*ambiguous_decls=*/NULL,
16052 token->location);
16053 /* If it's not a namespace, issue an error. */
16054 if (namespace_decl == error_mark_node
16055 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16057 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16058 error_at (token->location, "%qD is not a namespace-name", identifier);
16059 cp_parser_error (parser, "expected namespace-name");
16060 namespace_decl = error_mark_node;
16063 return namespace_decl;
16066 /* Parse a namespace-definition.
16068 namespace-definition:
16069 named-namespace-definition
16070 unnamed-namespace-definition
16072 named-namespace-definition:
16073 original-namespace-definition
16074 extension-namespace-definition
16076 original-namespace-definition:
16077 namespace identifier { namespace-body }
16079 extension-namespace-definition:
16080 namespace original-namespace-name { namespace-body }
16082 unnamed-namespace-definition:
16083 namespace { namespace-body } */
16085 static void
16086 cp_parser_namespace_definition (cp_parser* parser)
16088 tree identifier, attribs;
16089 bool has_visibility;
16090 bool is_inline;
16092 cp_ensure_no_omp_declare_simd (parser);
16093 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16095 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16096 is_inline = true;
16097 cp_lexer_consume_token (parser->lexer);
16099 else
16100 is_inline = false;
16102 /* Look for the `namespace' keyword. */
16103 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16105 /* Get the name of the namespace. We do not attempt to distinguish
16106 between an original-namespace-definition and an
16107 extension-namespace-definition at this point. The semantic
16108 analysis routines are responsible for that. */
16109 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16110 identifier = cp_parser_identifier (parser);
16111 else
16112 identifier = NULL_TREE;
16114 /* Parse any specified attributes. */
16115 attribs = cp_parser_attributes_opt (parser);
16117 /* Look for the `{' to start the namespace. */
16118 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16119 /* Start the namespace. */
16120 push_namespace (identifier);
16122 /* "inline namespace" is equivalent to a stub namespace definition
16123 followed by a strong using directive. */
16124 if (is_inline)
16126 tree name_space = current_namespace;
16127 /* Set up namespace association. */
16128 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16129 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16130 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16131 /* Import the contents of the inline namespace. */
16132 pop_namespace ();
16133 do_using_directive (name_space);
16134 push_namespace (identifier);
16137 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16139 /* Parse the body of the namespace. */
16140 cp_parser_namespace_body (parser);
16142 if (has_visibility)
16143 pop_visibility (1);
16145 /* Finish the namespace. */
16146 pop_namespace ();
16147 /* Look for the final `}'. */
16148 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16151 /* Parse a namespace-body.
16153 namespace-body:
16154 declaration-seq [opt] */
16156 static void
16157 cp_parser_namespace_body (cp_parser* parser)
16159 cp_parser_declaration_seq_opt (parser);
16162 /* Parse a namespace-alias-definition.
16164 namespace-alias-definition:
16165 namespace identifier = qualified-namespace-specifier ; */
16167 static void
16168 cp_parser_namespace_alias_definition (cp_parser* parser)
16170 tree identifier;
16171 tree namespace_specifier;
16173 cp_token *token = cp_lexer_peek_token (parser->lexer);
16175 /* Look for the `namespace' keyword. */
16176 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16177 /* Look for the identifier. */
16178 identifier = cp_parser_identifier (parser);
16179 if (identifier == error_mark_node)
16180 return;
16181 /* Look for the `=' token. */
16182 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16183 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16185 error_at (token->location, "%<namespace%> definition is not allowed here");
16186 /* Skip the definition. */
16187 cp_lexer_consume_token (parser->lexer);
16188 if (cp_parser_skip_to_closing_brace (parser))
16189 cp_lexer_consume_token (parser->lexer);
16190 return;
16192 cp_parser_require (parser, CPP_EQ, RT_EQ);
16193 /* Look for the qualified-namespace-specifier. */
16194 namespace_specifier
16195 = cp_parser_qualified_namespace_specifier (parser);
16196 /* Look for the `;' token. */
16197 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16199 /* Register the alias in the symbol table. */
16200 do_namespace_alias (identifier, namespace_specifier);
16203 /* Parse a qualified-namespace-specifier.
16205 qualified-namespace-specifier:
16206 :: [opt] nested-name-specifier [opt] namespace-name
16208 Returns a NAMESPACE_DECL corresponding to the specified
16209 namespace. */
16211 static tree
16212 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16214 /* Look for the optional `::'. */
16215 cp_parser_global_scope_opt (parser,
16216 /*current_scope_valid_p=*/false);
16218 /* Look for the optional nested-name-specifier. */
16219 cp_parser_nested_name_specifier_opt (parser,
16220 /*typename_keyword_p=*/false,
16221 /*check_dependency_p=*/true,
16222 /*type_p=*/false,
16223 /*is_declaration=*/true);
16225 return cp_parser_namespace_name (parser);
16228 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16229 access declaration.
16231 using-declaration:
16232 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16233 using :: unqualified-id ;
16235 access-declaration:
16236 qualified-id ;
16240 static bool
16241 cp_parser_using_declaration (cp_parser* parser,
16242 bool access_declaration_p)
16244 cp_token *token;
16245 bool typename_p = false;
16246 bool global_scope_p;
16247 tree decl;
16248 tree identifier;
16249 tree qscope;
16250 int oldcount = errorcount;
16251 cp_token *diag_token = NULL;
16253 if (access_declaration_p)
16255 diag_token = cp_lexer_peek_token (parser->lexer);
16256 cp_parser_parse_tentatively (parser);
16258 else
16260 /* Look for the `using' keyword. */
16261 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16263 /* Peek at the next token. */
16264 token = cp_lexer_peek_token (parser->lexer);
16265 /* See if it's `typename'. */
16266 if (token->keyword == RID_TYPENAME)
16268 /* Remember that we've seen it. */
16269 typename_p = true;
16270 /* Consume the `typename' token. */
16271 cp_lexer_consume_token (parser->lexer);
16275 /* Look for the optional global scope qualification. */
16276 global_scope_p
16277 = (cp_parser_global_scope_opt (parser,
16278 /*current_scope_valid_p=*/false)
16279 != NULL_TREE);
16281 /* If we saw `typename', or didn't see `::', then there must be a
16282 nested-name-specifier present. */
16283 if (typename_p || !global_scope_p)
16285 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16286 /*check_dependency_p=*/true,
16287 /*type_p=*/false,
16288 /*is_declaration=*/true);
16289 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16291 cp_parser_skip_to_end_of_block_or_statement (parser);
16292 return false;
16295 /* Otherwise, we could be in either of the two productions. In that
16296 case, treat the nested-name-specifier as optional. */
16297 else
16298 qscope = cp_parser_nested_name_specifier_opt (parser,
16299 /*typename_keyword_p=*/false,
16300 /*check_dependency_p=*/true,
16301 /*type_p=*/false,
16302 /*is_declaration=*/true);
16303 if (!qscope)
16304 qscope = global_namespace;
16305 else if (UNSCOPED_ENUM_P (qscope))
16306 qscope = CP_TYPE_CONTEXT (qscope);
16308 if (access_declaration_p && cp_parser_error_occurred (parser))
16309 /* Something has already gone wrong; there's no need to parse
16310 further. Since an error has occurred, the return value of
16311 cp_parser_parse_definitely will be false, as required. */
16312 return cp_parser_parse_definitely (parser);
16314 token = cp_lexer_peek_token (parser->lexer);
16315 /* Parse the unqualified-id. */
16316 identifier = cp_parser_unqualified_id (parser,
16317 /*template_keyword_p=*/false,
16318 /*check_dependency_p=*/true,
16319 /*declarator_p=*/true,
16320 /*optional_p=*/false);
16322 if (access_declaration_p)
16324 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16325 cp_parser_simulate_error (parser);
16326 if (!cp_parser_parse_definitely (parser))
16327 return false;
16330 /* The function we call to handle a using-declaration is different
16331 depending on what scope we are in. */
16332 if (qscope == error_mark_node || identifier == error_mark_node)
16334 else if (!identifier_p (identifier)
16335 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16336 /* [namespace.udecl]
16338 A using declaration shall not name a template-id. */
16339 error_at (token->location,
16340 "a template-id may not appear in a using-declaration");
16341 else
16343 if (at_class_scope_p ())
16345 /* Create the USING_DECL. */
16346 decl = do_class_using_decl (parser->scope, identifier);
16348 if (decl && typename_p)
16349 USING_DECL_TYPENAME_P (decl) = 1;
16351 if (check_for_bare_parameter_packs (decl))
16353 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16354 return false;
16356 else
16357 /* Add it to the list of members in this class. */
16358 finish_member_declaration (decl);
16360 else
16362 decl = cp_parser_lookup_name_simple (parser,
16363 identifier,
16364 token->location);
16365 if (decl == error_mark_node)
16366 cp_parser_name_lookup_error (parser, identifier,
16367 decl, NLE_NULL,
16368 token->location);
16369 else if (check_for_bare_parameter_packs (decl))
16371 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16372 return false;
16374 else if (!at_namespace_scope_p ())
16375 do_local_using_decl (decl, qscope, identifier);
16376 else
16377 do_toplevel_using_decl (decl, qscope, identifier);
16381 /* Look for the final `;'. */
16382 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16384 if (access_declaration_p && errorcount == oldcount)
16385 warning_at (diag_token->location, OPT_Wdeprecated,
16386 "access declarations are deprecated "
16387 "in favour of using-declarations; "
16388 "suggestion: add the %<using%> keyword");
16390 return true;
16393 /* Parse an alias-declaration.
16395 alias-declaration:
16396 using identifier attribute-specifier-seq [opt] = type-id */
16398 static tree
16399 cp_parser_alias_declaration (cp_parser* parser)
16401 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16402 location_t id_location;
16403 cp_declarator *declarator;
16404 cp_decl_specifier_seq decl_specs;
16405 bool member_p;
16406 const char *saved_message = NULL;
16408 /* Look for the `using' keyword. */
16409 cp_token *using_token
16410 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16411 if (using_token == NULL)
16412 return error_mark_node;
16414 id_location = cp_lexer_peek_token (parser->lexer)->location;
16415 id = cp_parser_identifier (parser);
16416 if (id == error_mark_node)
16417 return error_mark_node;
16419 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16420 attributes = cp_parser_attributes_opt (parser);
16421 if (attributes == error_mark_node)
16422 return error_mark_node;
16424 cp_parser_require (parser, CPP_EQ, RT_EQ);
16426 if (cp_parser_error_occurred (parser))
16427 return error_mark_node;
16429 cp_parser_commit_to_tentative_parse (parser);
16431 /* Now we are going to parse the type-id of the declaration. */
16434 [dcl.type]/3 says:
16436 "A type-specifier-seq shall not define a class or enumeration
16437 unless it appears in the type-id of an alias-declaration (7.1.3) that
16438 is not the declaration of a template-declaration."
16440 In other words, if we currently are in an alias template, the
16441 type-id should not define a type.
16443 So let's set parser->type_definition_forbidden_message in that
16444 case; cp_parser_check_type_definition (called by
16445 cp_parser_class_specifier) will then emit an error if a type is
16446 defined in the type-id. */
16447 if (parser->num_template_parameter_lists)
16449 saved_message = parser->type_definition_forbidden_message;
16450 parser->type_definition_forbidden_message =
16451 G_("types may not be defined in alias template declarations");
16454 type = cp_parser_type_id (parser);
16456 /* Restore the error message if need be. */
16457 if (parser->num_template_parameter_lists)
16458 parser->type_definition_forbidden_message = saved_message;
16460 if (type == error_mark_node
16461 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16463 cp_parser_skip_to_end_of_block_or_statement (parser);
16464 return error_mark_node;
16467 /* A typedef-name can also be introduced by an alias-declaration. The
16468 identifier following the using keyword becomes a typedef-name. It has
16469 the same semantics as if it were introduced by the typedef
16470 specifier. In particular, it does not define a new type and it shall
16471 not appear in the type-id. */
16473 clear_decl_specs (&decl_specs);
16474 decl_specs.type = type;
16475 if (attributes != NULL_TREE)
16477 decl_specs.attributes = attributes;
16478 set_and_check_decl_spec_loc (&decl_specs,
16479 ds_attribute,
16480 attrs_token);
16482 set_and_check_decl_spec_loc (&decl_specs,
16483 ds_typedef,
16484 using_token);
16485 set_and_check_decl_spec_loc (&decl_specs,
16486 ds_alias,
16487 using_token);
16489 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16490 declarator->id_loc = id_location;
16492 member_p = at_class_scope_p ();
16493 if (member_p)
16494 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16495 NULL_TREE, attributes);
16496 else
16497 decl = start_decl (declarator, &decl_specs, 0,
16498 attributes, NULL_TREE, &pushed_scope);
16499 if (decl == error_mark_node)
16500 return decl;
16502 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16504 if (pushed_scope)
16505 pop_scope (pushed_scope);
16507 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16508 added into the symbol table; otherwise, return the TYPE_DECL. */
16509 if (DECL_LANG_SPECIFIC (decl)
16510 && DECL_TEMPLATE_INFO (decl)
16511 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16513 decl = DECL_TI_TEMPLATE (decl);
16514 if (member_p)
16515 check_member_template (decl);
16518 return decl;
16521 /* Parse a using-directive.
16523 using-directive:
16524 using namespace :: [opt] nested-name-specifier [opt]
16525 namespace-name ; */
16527 static void
16528 cp_parser_using_directive (cp_parser* parser)
16530 tree namespace_decl;
16531 tree attribs;
16533 /* Look for the `using' keyword. */
16534 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16535 /* And the `namespace' keyword. */
16536 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16537 /* Look for the optional `::' operator. */
16538 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16539 /* And the optional nested-name-specifier. */
16540 cp_parser_nested_name_specifier_opt (parser,
16541 /*typename_keyword_p=*/false,
16542 /*check_dependency_p=*/true,
16543 /*type_p=*/false,
16544 /*is_declaration=*/true);
16545 /* Get the namespace being used. */
16546 namespace_decl = cp_parser_namespace_name (parser);
16547 /* And any specified attributes. */
16548 attribs = cp_parser_attributes_opt (parser);
16549 /* Update the symbol table. */
16550 parse_using_directive (namespace_decl, attribs);
16551 /* Look for the final `;'. */
16552 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16555 /* Parse an asm-definition.
16557 asm-definition:
16558 asm ( string-literal ) ;
16560 GNU Extension:
16562 asm-definition:
16563 asm volatile [opt] ( string-literal ) ;
16564 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16565 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16566 : asm-operand-list [opt] ) ;
16567 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16568 : asm-operand-list [opt]
16569 : asm-clobber-list [opt] ) ;
16570 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16571 : asm-clobber-list [opt]
16572 : asm-goto-list ) ; */
16574 static void
16575 cp_parser_asm_definition (cp_parser* parser)
16577 tree string;
16578 tree outputs = NULL_TREE;
16579 tree inputs = NULL_TREE;
16580 tree clobbers = NULL_TREE;
16581 tree labels = NULL_TREE;
16582 tree asm_stmt;
16583 bool volatile_p = false;
16584 bool extended_p = false;
16585 bool invalid_inputs_p = false;
16586 bool invalid_outputs_p = false;
16587 bool goto_p = false;
16588 required_token missing = RT_NONE;
16590 /* Look for the `asm' keyword. */
16591 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16593 if (parser->in_function_body
16594 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16595 error ("%<asm%> in %<constexpr%> function");
16597 /* See if the next token is `volatile'. */
16598 if (cp_parser_allow_gnu_extensions_p (parser)
16599 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16601 /* Remember that we saw the `volatile' keyword. */
16602 volatile_p = true;
16603 /* Consume the token. */
16604 cp_lexer_consume_token (parser->lexer);
16606 if (cp_parser_allow_gnu_extensions_p (parser)
16607 && parser->in_function_body
16608 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16610 /* Remember that we saw the `goto' keyword. */
16611 goto_p = true;
16612 /* Consume the token. */
16613 cp_lexer_consume_token (parser->lexer);
16615 /* Look for the opening `('. */
16616 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16617 return;
16618 /* Look for the string. */
16619 string = cp_parser_string_literal (parser, false, false);
16620 if (string == error_mark_node)
16622 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16623 /*consume_paren=*/true);
16624 return;
16627 /* If we're allowing GNU extensions, check for the extended assembly
16628 syntax. Unfortunately, the `:' tokens need not be separated by
16629 a space in C, and so, for compatibility, we tolerate that here
16630 too. Doing that means that we have to treat the `::' operator as
16631 two `:' tokens. */
16632 if (cp_parser_allow_gnu_extensions_p (parser)
16633 && parser->in_function_body
16634 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16635 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16637 bool inputs_p = false;
16638 bool clobbers_p = false;
16639 bool labels_p = false;
16641 /* The extended syntax was used. */
16642 extended_p = true;
16644 /* Look for outputs. */
16645 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16647 /* Consume the `:'. */
16648 cp_lexer_consume_token (parser->lexer);
16649 /* Parse the output-operands. */
16650 if (cp_lexer_next_token_is_not (parser->lexer,
16651 CPP_COLON)
16652 && cp_lexer_next_token_is_not (parser->lexer,
16653 CPP_SCOPE)
16654 && cp_lexer_next_token_is_not (parser->lexer,
16655 CPP_CLOSE_PAREN)
16656 && !goto_p)
16657 outputs = cp_parser_asm_operand_list (parser);
16659 if (outputs == error_mark_node)
16660 invalid_outputs_p = true;
16662 /* If the next token is `::', there are no outputs, and the
16663 next token is the beginning of the inputs. */
16664 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16665 /* The inputs are coming next. */
16666 inputs_p = true;
16668 /* Look for inputs. */
16669 if (inputs_p
16670 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16672 /* Consume the `:' or `::'. */
16673 cp_lexer_consume_token (parser->lexer);
16674 /* Parse the output-operands. */
16675 if (cp_lexer_next_token_is_not (parser->lexer,
16676 CPP_COLON)
16677 && cp_lexer_next_token_is_not (parser->lexer,
16678 CPP_SCOPE)
16679 && cp_lexer_next_token_is_not (parser->lexer,
16680 CPP_CLOSE_PAREN))
16681 inputs = cp_parser_asm_operand_list (parser);
16683 if (inputs == error_mark_node)
16684 invalid_inputs_p = true;
16686 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16687 /* The clobbers are coming next. */
16688 clobbers_p = true;
16690 /* Look for clobbers. */
16691 if (clobbers_p
16692 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16694 clobbers_p = true;
16695 /* Consume the `:' or `::'. */
16696 cp_lexer_consume_token (parser->lexer);
16697 /* Parse the clobbers. */
16698 if (cp_lexer_next_token_is_not (parser->lexer,
16699 CPP_COLON)
16700 && cp_lexer_next_token_is_not (parser->lexer,
16701 CPP_CLOSE_PAREN))
16702 clobbers = cp_parser_asm_clobber_list (parser);
16704 else if (goto_p
16705 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16706 /* The labels are coming next. */
16707 labels_p = true;
16709 /* Look for labels. */
16710 if (labels_p
16711 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16713 labels_p = true;
16714 /* Consume the `:' or `::'. */
16715 cp_lexer_consume_token (parser->lexer);
16716 /* Parse the labels. */
16717 labels = cp_parser_asm_label_list (parser);
16720 if (goto_p && !labels_p)
16721 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16723 else if (goto_p)
16724 missing = RT_COLON_SCOPE;
16726 /* Look for the closing `)'. */
16727 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16728 missing ? missing : RT_CLOSE_PAREN))
16729 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16730 /*consume_paren=*/true);
16731 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16733 if (!invalid_inputs_p && !invalid_outputs_p)
16735 /* Create the ASM_EXPR. */
16736 if (parser->in_function_body)
16738 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16739 inputs, clobbers, labels);
16740 /* If the extended syntax was not used, mark the ASM_EXPR. */
16741 if (!extended_p)
16743 tree temp = asm_stmt;
16744 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16745 temp = TREE_OPERAND (temp, 0);
16747 ASM_INPUT_P (temp) = 1;
16750 else
16751 symtab->finalize_toplevel_asm (string);
16755 /* Declarators [gram.dcl.decl] */
16757 /* Parse an init-declarator.
16759 init-declarator:
16760 declarator initializer [opt]
16762 GNU Extension:
16764 init-declarator:
16765 declarator asm-specification [opt] attributes [opt] initializer [opt]
16767 function-definition:
16768 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16769 function-body
16770 decl-specifier-seq [opt] declarator function-try-block
16772 GNU Extension:
16774 function-definition:
16775 __extension__ function-definition
16777 TM Extension:
16779 function-definition:
16780 decl-specifier-seq [opt] declarator function-transaction-block
16782 The DECL_SPECIFIERS apply to this declarator. Returns a
16783 representation of the entity declared. If MEMBER_P is TRUE, then
16784 this declarator appears in a class scope. The new DECL created by
16785 this declarator is returned.
16787 The CHECKS are access checks that should be performed once we know
16788 what entity is being declared (and, therefore, what classes have
16789 befriended it).
16791 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16792 for a function-definition here as well. If the declarator is a
16793 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16794 be TRUE upon return. By that point, the function-definition will
16795 have been completely parsed.
16797 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16798 is FALSE.
16800 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16801 parsed declaration if it is an uninitialized single declarator not followed
16802 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16803 if present, will not be consumed. If returned, this declarator will be
16804 created with SD_INITIALIZED but will not call cp_finish_decl. */
16806 static tree
16807 cp_parser_init_declarator (cp_parser* parser,
16808 cp_decl_specifier_seq *decl_specifiers,
16809 vec<deferred_access_check, va_gc> *checks,
16810 bool function_definition_allowed_p,
16811 bool member_p,
16812 int declares_class_or_enum,
16813 bool* function_definition_p,
16814 tree* maybe_range_for_decl)
16816 cp_token *token = NULL, *asm_spec_start_token = NULL,
16817 *attributes_start_token = NULL;
16818 cp_declarator *declarator;
16819 tree prefix_attributes;
16820 tree attributes = NULL;
16821 tree asm_specification;
16822 tree initializer;
16823 tree decl = NULL_TREE;
16824 tree scope;
16825 int is_initialized;
16826 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16827 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16828 "(...)". */
16829 enum cpp_ttype initialization_kind;
16830 bool is_direct_init = false;
16831 bool is_non_constant_init;
16832 int ctor_dtor_or_conv_p;
16833 bool friend_p = cp_parser_friend_p (decl_specifiers);
16834 tree pushed_scope = NULL_TREE;
16835 bool range_for_decl_p = false;
16836 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16838 /* Gather the attributes that were provided with the
16839 decl-specifiers. */
16840 prefix_attributes = decl_specifiers->attributes;
16842 /* Assume that this is not the declarator for a function
16843 definition. */
16844 if (function_definition_p)
16845 *function_definition_p = false;
16847 /* Default arguments are only permitted for function parameters. */
16848 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16849 parser->default_arg_ok_p = false;
16851 /* Defer access checks while parsing the declarator; we cannot know
16852 what names are accessible until we know what is being
16853 declared. */
16854 resume_deferring_access_checks ();
16856 /* Parse the declarator. */
16857 token = cp_lexer_peek_token (parser->lexer);
16858 declarator
16859 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16860 &ctor_dtor_or_conv_p,
16861 /*parenthesized_p=*/NULL,
16862 member_p, friend_p);
16863 /* Gather up the deferred checks. */
16864 stop_deferring_access_checks ();
16866 parser->default_arg_ok_p = saved_default_arg_ok_p;
16868 /* If the DECLARATOR was erroneous, there's no need to go
16869 further. */
16870 if (declarator == cp_error_declarator)
16871 return error_mark_node;
16873 /* Check that the number of template-parameter-lists is OK. */
16874 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16875 token->location))
16876 return error_mark_node;
16878 if (declares_class_or_enum & 2)
16879 cp_parser_check_for_definition_in_return_type (declarator,
16880 decl_specifiers->type,
16881 decl_specifiers->locations[ds_type_spec]);
16883 /* Figure out what scope the entity declared by the DECLARATOR is
16884 located in. `grokdeclarator' sometimes changes the scope, so
16885 we compute it now. */
16886 scope = get_scope_of_declarator (declarator);
16888 /* Perform any lookups in the declared type which were thought to be
16889 dependent, but are not in the scope of the declarator. */
16890 decl_specifiers->type
16891 = maybe_update_decl_type (decl_specifiers->type, scope);
16893 /* If we're allowing GNU extensions, look for an
16894 asm-specification. */
16895 if (cp_parser_allow_gnu_extensions_p (parser))
16897 /* Look for an asm-specification. */
16898 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16899 asm_specification = cp_parser_asm_specification_opt (parser);
16901 else
16902 asm_specification = NULL_TREE;
16904 /* Look for attributes. */
16905 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16906 attributes = cp_parser_attributes_opt (parser);
16908 /* Peek at the next token. */
16909 token = cp_lexer_peek_token (parser->lexer);
16911 bool bogus_implicit_tmpl = false;
16913 if (function_declarator_p (declarator))
16915 /* Check to see if the token indicates the start of a
16916 function-definition. */
16917 if (cp_parser_token_starts_function_definition_p (token))
16919 if (!function_definition_allowed_p)
16921 /* If a function-definition should not appear here, issue an
16922 error message. */
16923 cp_parser_error (parser,
16924 "a function-definition is not allowed here");
16925 return error_mark_node;
16928 location_t func_brace_location
16929 = cp_lexer_peek_token (parser->lexer)->location;
16931 /* Neither attributes nor an asm-specification are allowed
16932 on a function-definition. */
16933 if (asm_specification)
16934 error_at (asm_spec_start_token->location,
16935 "an asm-specification is not allowed "
16936 "on a function-definition");
16937 if (attributes)
16938 error_at (attributes_start_token->location,
16939 "attributes are not allowed "
16940 "on a function-definition");
16941 /* This is a function-definition. */
16942 *function_definition_p = true;
16944 /* Parse the function definition. */
16945 if (member_p)
16946 decl = cp_parser_save_member_function_body (parser,
16947 decl_specifiers,
16948 declarator,
16949 prefix_attributes);
16950 else
16951 decl =
16952 (cp_parser_function_definition_from_specifiers_and_declarator
16953 (parser, decl_specifiers, prefix_attributes, declarator));
16955 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16957 /* This is where the prologue starts... */
16958 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16959 = func_brace_location;
16962 return decl;
16965 else if (parser->fully_implicit_function_template_p)
16967 /* A non-template declaration involving a function parameter list
16968 containing an implicit template parameter will be made into a
16969 template. If the resulting declaration is not going to be an
16970 actual function then finish the template scope here to prevent it.
16971 An error message will be issued once we have a decl to talk about.
16973 FIXME probably we should do type deduction rather than create an
16974 implicit template, but the standard currently doesn't allow it. */
16975 bogus_implicit_tmpl = true;
16976 finish_fully_implicit_template (parser, NULL_TREE);
16979 /* [dcl.dcl]
16981 Only in function declarations for constructors, destructors, and
16982 type conversions can the decl-specifier-seq be omitted.
16984 We explicitly postpone this check past the point where we handle
16985 function-definitions because we tolerate function-definitions
16986 that are missing their return types in some modes. */
16987 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16989 cp_parser_error (parser,
16990 "expected constructor, destructor, or type conversion");
16991 return error_mark_node;
16994 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16995 if (token->type == CPP_EQ
16996 || token->type == CPP_OPEN_PAREN
16997 || token->type == CPP_OPEN_BRACE)
16999 is_initialized = SD_INITIALIZED;
17000 initialization_kind = token->type;
17001 if (maybe_range_for_decl)
17002 *maybe_range_for_decl = error_mark_node;
17004 if (token->type == CPP_EQ
17005 && function_declarator_p (declarator))
17007 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17008 if (t2->keyword == RID_DEFAULT)
17009 is_initialized = SD_DEFAULTED;
17010 else if (t2->keyword == RID_DELETE)
17011 is_initialized = SD_DELETED;
17014 else
17016 /* If the init-declarator isn't initialized and isn't followed by a
17017 `,' or `;', it's not a valid init-declarator. */
17018 if (token->type != CPP_COMMA
17019 && token->type != CPP_SEMICOLON)
17021 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17022 range_for_decl_p = true;
17023 else
17025 cp_parser_error (parser, "expected initializer");
17026 return error_mark_node;
17029 is_initialized = SD_UNINITIALIZED;
17030 initialization_kind = CPP_EOF;
17033 /* Because start_decl has side-effects, we should only call it if we
17034 know we're going ahead. By this point, we know that we cannot
17035 possibly be looking at any other construct. */
17036 cp_parser_commit_to_tentative_parse (parser);
17038 /* Enter the newly declared entry in the symbol table. If we're
17039 processing a declaration in a class-specifier, we wait until
17040 after processing the initializer. */
17041 if (!member_p)
17043 if (parser->in_unbraced_linkage_specification_p)
17044 decl_specifiers->storage_class = sc_extern;
17045 decl = start_decl (declarator, decl_specifiers,
17046 range_for_decl_p? SD_INITIALIZED : is_initialized,
17047 attributes, prefix_attributes, &pushed_scope);
17048 cp_finalize_omp_declare_simd (parser, decl);
17049 /* Adjust location of decl if declarator->id_loc is more appropriate:
17050 set, and decl wasn't merged with another decl, in which case its
17051 location would be different from input_location, and more accurate. */
17052 if (DECL_P (decl)
17053 && declarator->id_loc != UNKNOWN_LOCATION
17054 && DECL_SOURCE_LOCATION (decl) == input_location)
17055 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17057 else if (scope)
17058 /* Enter the SCOPE. That way unqualified names appearing in the
17059 initializer will be looked up in SCOPE. */
17060 pushed_scope = push_scope (scope);
17062 /* Perform deferred access control checks, now that we know in which
17063 SCOPE the declared entity resides. */
17064 if (!member_p && decl)
17066 tree saved_current_function_decl = NULL_TREE;
17068 /* If the entity being declared is a function, pretend that we
17069 are in its scope. If it is a `friend', it may have access to
17070 things that would not otherwise be accessible. */
17071 if (TREE_CODE (decl) == FUNCTION_DECL)
17073 saved_current_function_decl = current_function_decl;
17074 current_function_decl = decl;
17077 /* Perform access checks for template parameters. */
17078 cp_parser_perform_template_parameter_access_checks (checks);
17080 /* Perform the access control checks for the declarator and the
17081 decl-specifiers. */
17082 perform_deferred_access_checks (tf_warning_or_error);
17084 /* Restore the saved value. */
17085 if (TREE_CODE (decl) == FUNCTION_DECL)
17086 current_function_decl = saved_current_function_decl;
17089 /* Parse the initializer. */
17090 initializer = NULL_TREE;
17091 is_direct_init = false;
17092 is_non_constant_init = true;
17093 if (is_initialized)
17095 if (function_declarator_p (declarator))
17097 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
17098 if (initialization_kind == CPP_EQ)
17099 initializer = cp_parser_pure_specifier (parser);
17100 else
17102 /* If the declaration was erroneous, we don't really
17103 know what the user intended, so just silently
17104 consume the initializer. */
17105 if (decl != error_mark_node)
17106 error_at (initializer_start_token->location,
17107 "initializer provided for function");
17108 cp_parser_skip_to_closing_parenthesis (parser,
17109 /*recovering=*/true,
17110 /*or_comma=*/false,
17111 /*consume_paren=*/true);
17114 else
17116 /* We want to record the extra mangling scope for in-class
17117 initializers of class members and initializers of static data
17118 member templates. The former involves deferring
17119 parsing of the initializer until end of class as with default
17120 arguments. So right here we only handle the latter. */
17121 if (!member_p && processing_template_decl)
17122 start_lambda_scope (decl);
17123 initializer = cp_parser_initializer (parser,
17124 &is_direct_init,
17125 &is_non_constant_init);
17126 if (!member_p && processing_template_decl)
17127 finish_lambda_scope ();
17128 if (initializer == error_mark_node)
17129 cp_parser_skip_to_end_of_statement (parser);
17133 /* The old parser allows attributes to appear after a parenthesized
17134 initializer. Mark Mitchell proposed removing this functionality
17135 on the GCC mailing lists on 2002-08-13. This parser accepts the
17136 attributes -- but ignores them. */
17137 if (cp_parser_allow_gnu_extensions_p (parser)
17138 && initialization_kind == CPP_OPEN_PAREN)
17139 if (cp_parser_attributes_opt (parser))
17140 warning (OPT_Wattributes,
17141 "attributes after parenthesized initializer ignored");
17143 /* And now complain about a non-function implicit template. */
17144 if (bogus_implicit_tmpl)
17145 error_at (DECL_SOURCE_LOCATION (decl),
17146 "non-function %qD declared as implicit template", decl);
17148 /* For an in-class declaration, use `grokfield' to create the
17149 declaration. */
17150 if (member_p)
17152 if (pushed_scope)
17154 pop_scope (pushed_scope);
17155 pushed_scope = NULL_TREE;
17157 decl = grokfield (declarator, decl_specifiers,
17158 initializer, !is_non_constant_init,
17159 /*asmspec=*/NULL_TREE,
17160 chainon (attributes, prefix_attributes));
17161 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17162 cp_parser_save_default_args (parser, decl);
17163 cp_finalize_omp_declare_simd (parser, decl);
17166 /* Finish processing the declaration. But, skip member
17167 declarations. */
17168 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17170 cp_finish_decl (decl,
17171 initializer, !is_non_constant_init,
17172 asm_specification,
17173 /* If the initializer is in parentheses, then this is
17174 a direct-initialization, which means that an
17175 `explicit' constructor is OK. Otherwise, an
17176 `explicit' constructor cannot be used. */
17177 ((is_direct_init || !is_initialized)
17178 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17180 else if ((cxx_dialect != cxx98) && friend_p
17181 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17182 /* Core issue #226 (C++0x only): A default template-argument
17183 shall not be specified in a friend class template
17184 declaration. */
17185 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17186 /*is_partial=*/false, /*is_friend_decl=*/1);
17188 if (!friend_p && pushed_scope)
17189 pop_scope (pushed_scope);
17191 if (function_declarator_p (declarator)
17192 && parser->fully_implicit_function_template_p)
17194 if (member_p)
17195 decl = finish_fully_implicit_template (parser, decl);
17196 else
17197 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17200 return decl;
17203 /* Parse a declarator.
17205 declarator:
17206 direct-declarator
17207 ptr-operator declarator
17209 abstract-declarator:
17210 ptr-operator abstract-declarator [opt]
17211 direct-abstract-declarator
17213 GNU Extensions:
17215 declarator:
17216 attributes [opt] direct-declarator
17217 attributes [opt] ptr-operator declarator
17219 abstract-declarator:
17220 attributes [opt] ptr-operator abstract-declarator [opt]
17221 attributes [opt] direct-abstract-declarator
17223 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17224 detect constructor, destructor or conversion operators. It is set
17225 to -1 if the declarator is a name, and +1 if it is a
17226 function. Otherwise it is set to zero. Usually you just want to
17227 test for >0, but internally the negative value is used.
17229 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17230 a decl-specifier-seq unless it declares a constructor, destructor,
17231 or conversion. It might seem that we could check this condition in
17232 semantic analysis, rather than parsing, but that makes it difficult
17233 to handle something like `f()'. We want to notice that there are
17234 no decl-specifiers, and therefore realize that this is an
17235 expression, not a declaration.)
17237 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17238 the declarator is a direct-declarator of the form "(...)".
17240 MEMBER_P is true iff this declarator is a member-declarator.
17242 FRIEND_P is true iff this declarator is a friend. */
17244 static cp_declarator *
17245 cp_parser_declarator (cp_parser* parser,
17246 cp_parser_declarator_kind dcl_kind,
17247 int* ctor_dtor_or_conv_p,
17248 bool* parenthesized_p,
17249 bool member_p, bool friend_p)
17251 cp_declarator *declarator;
17252 enum tree_code code;
17253 cp_cv_quals cv_quals;
17254 tree class_type;
17255 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17257 /* Assume this is not a constructor, destructor, or type-conversion
17258 operator. */
17259 if (ctor_dtor_or_conv_p)
17260 *ctor_dtor_or_conv_p = 0;
17262 if (cp_parser_allow_gnu_extensions_p (parser))
17263 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17265 /* Check for the ptr-operator production. */
17266 cp_parser_parse_tentatively (parser);
17267 /* Parse the ptr-operator. */
17268 code = cp_parser_ptr_operator (parser,
17269 &class_type,
17270 &cv_quals,
17271 &std_attributes);
17273 /* If that worked, then we have a ptr-operator. */
17274 if (cp_parser_parse_definitely (parser))
17276 /* If a ptr-operator was found, then this declarator was not
17277 parenthesized. */
17278 if (parenthesized_p)
17279 *parenthesized_p = true;
17280 /* The dependent declarator is optional if we are parsing an
17281 abstract-declarator. */
17282 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17283 cp_parser_parse_tentatively (parser);
17285 /* Parse the dependent declarator. */
17286 declarator = cp_parser_declarator (parser, dcl_kind,
17287 /*ctor_dtor_or_conv_p=*/NULL,
17288 /*parenthesized_p=*/NULL,
17289 /*member_p=*/false,
17290 friend_p);
17292 /* If we are parsing an abstract-declarator, we must handle the
17293 case where the dependent declarator is absent. */
17294 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17295 && !cp_parser_parse_definitely (parser))
17296 declarator = NULL;
17298 declarator = cp_parser_make_indirect_declarator
17299 (code, class_type, cv_quals, declarator, std_attributes);
17301 /* Everything else is a direct-declarator. */
17302 else
17304 if (parenthesized_p)
17305 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17306 CPP_OPEN_PAREN);
17307 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17308 ctor_dtor_or_conv_p,
17309 member_p, friend_p);
17312 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17313 declarator->attributes = gnu_attributes;
17314 return declarator;
17317 /* Parse a direct-declarator or direct-abstract-declarator.
17319 direct-declarator:
17320 declarator-id
17321 direct-declarator ( parameter-declaration-clause )
17322 cv-qualifier-seq [opt]
17323 ref-qualifier [opt]
17324 exception-specification [opt]
17325 direct-declarator [ constant-expression [opt] ]
17326 ( declarator )
17328 direct-abstract-declarator:
17329 direct-abstract-declarator [opt]
17330 ( parameter-declaration-clause )
17331 cv-qualifier-seq [opt]
17332 ref-qualifier [opt]
17333 exception-specification [opt]
17334 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17335 ( abstract-declarator )
17337 Returns a representation of the declarator. DCL_KIND is
17338 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17339 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17340 we are parsing a direct-declarator. It is
17341 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17342 of ambiguity we prefer an abstract declarator, as per
17343 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17344 as for cp_parser_declarator. */
17346 static cp_declarator *
17347 cp_parser_direct_declarator (cp_parser* parser,
17348 cp_parser_declarator_kind dcl_kind,
17349 int* ctor_dtor_or_conv_p,
17350 bool member_p, bool friend_p)
17352 cp_token *token;
17353 cp_declarator *declarator = NULL;
17354 tree scope = NULL_TREE;
17355 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17356 bool saved_in_declarator_p = parser->in_declarator_p;
17357 bool first = true;
17358 tree pushed_scope = NULL_TREE;
17360 while (true)
17362 /* Peek at the next token. */
17363 token = cp_lexer_peek_token (parser->lexer);
17364 if (token->type == CPP_OPEN_PAREN)
17366 /* This is either a parameter-declaration-clause, or a
17367 parenthesized declarator. When we know we are parsing a
17368 named declarator, it must be a parenthesized declarator
17369 if FIRST is true. For instance, `(int)' is a
17370 parameter-declaration-clause, with an omitted
17371 direct-abstract-declarator. But `((*))', is a
17372 parenthesized abstract declarator. Finally, when T is a
17373 template parameter `(T)' is a
17374 parameter-declaration-clause, and not a parenthesized
17375 named declarator.
17377 We first try and parse a parameter-declaration-clause,
17378 and then try a nested declarator (if FIRST is true).
17380 It is not an error for it not to be a
17381 parameter-declaration-clause, even when FIRST is
17382 false. Consider,
17384 int i (int);
17385 int i (3);
17387 The first is the declaration of a function while the
17388 second is the definition of a variable, including its
17389 initializer.
17391 Having seen only the parenthesis, we cannot know which of
17392 these two alternatives should be selected. Even more
17393 complex are examples like:
17395 int i (int (a));
17396 int i (int (3));
17398 The former is a function-declaration; the latter is a
17399 variable initialization.
17401 Thus again, we try a parameter-declaration-clause, and if
17402 that fails, we back out and return. */
17404 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17406 tree params;
17407 bool is_declarator = false;
17409 /* In a member-declarator, the only valid interpretation
17410 of a parenthesis is the start of a
17411 parameter-declaration-clause. (It is invalid to
17412 initialize a static data member with a parenthesized
17413 initializer; only the "=" form of initialization is
17414 permitted.) */
17415 if (!member_p)
17416 cp_parser_parse_tentatively (parser);
17418 /* Consume the `('. */
17419 cp_lexer_consume_token (parser->lexer);
17420 if (first)
17422 /* If this is going to be an abstract declarator, we're
17423 in a declarator and we can't have default args. */
17424 parser->default_arg_ok_p = false;
17425 parser->in_declarator_p = true;
17428 begin_scope (sk_function_parms, NULL_TREE);
17430 /* Parse the parameter-declaration-clause. */
17431 params = cp_parser_parameter_declaration_clause (parser);
17433 /* Consume the `)'. */
17434 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17436 /* If all went well, parse the cv-qualifier-seq,
17437 ref-qualifier and the exception-specification. */
17438 if (member_p || cp_parser_parse_definitely (parser))
17440 cp_cv_quals cv_quals;
17441 cp_virt_specifiers virt_specifiers;
17442 cp_ref_qualifier ref_qual;
17443 tree exception_specification;
17444 tree late_return;
17445 tree attrs;
17446 bool memfn = (member_p || (pushed_scope
17447 && CLASS_TYPE_P (pushed_scope)));
17449 is_declarator = true;
17451 if (ctor_dtor_or_conv_p)
17452 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17453 first = false;
17455 /* Parse the cv-qualifier-seq. */
17456 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17457 /* Parse the ref-qualifier. */
17458 ref_qual = cp_parser_ref_qualifier_opt (parser);
17459 /* And the exception-specification. */
17460 exception_specification
17461 = cp_parser_exception_specification_opt (parser);
17463 attrs = cp_parser_std_attribute_spec_seq (parser);
17465 /* In here, we handle cases where attribute is used after
17466 the function declaration. For example:
17467 void func (int x) __attribute__((vector(..))); */
17468 if (flag_cilkplus
17469 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17471 cp_parser_parse_tentatively (parser);
17472 tree attr = cp_parser_gnu_attributes_opt (parser);
17473 if (cp_lexer_next_token_is_not (parser->lexer,
17474 CPP_SEMICOLON)
17475 && cp_lexer_next_token_is_not (parser->lexer,
17476 CPP_OPEN_BRACE))
17477 cp_parser_abort_tentative_parse (parser);
17478 else if (!cp_parser_parse_definitely (parser))
17480 else
17481 attrs = chainon (attr, attrs);
17483 late_return = (cp_parser_late_return_type_opt
17484 (parser, declarator,
17485 memfn ? cv_quals : -1));
17488 /* Parse the virt-specifier-seq. */
17489 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17491 /* Create the function-declarator. */
17492 declarator = make_call_declarator (declarator,
17493 params,
17494 cv_quals,
17495 virt_specifiers,
17496 ref_qual,
17497 exception_specification,
17498 late_return);
17499 declarator->std_attributes = attrs;
17500 /* Any subsequent parameter lists are to do with
17501 return type, so are not those of the declared
17502 function. */
17503 parser->default_arg_ok_p = false;
17506 /* Remove the function parms from scope. */
17507 pop_bindings_and_leave_scope ();
17509 if (is_declarator)
17510 /* Repeat the main loop. */
17511 continue;
17514 /* If this is the first, we can try a parenthesized
17515 declarator. */
17516 if (first)
17518 bool saved_in_type_id_in_expr_p;
17520 parser->default_arg_ok_p = saved_default_arg_ok_p;
17521 parser->in_declarator_p = saved_in_declarator_p;
17523 /* Consume the `('. */
17524 cp_lexer_consume_token (parser->lexer);
17525 /* Parse the nested declarator. */
17526 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17527 parser->in_type_id_in_expr_p = true;
17528 declarator
17529 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17530 /*parenthesized_p=*/NULL,
17531 member_p, friend_p);
17532 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17533 first = false;
17534 /* Expect a `)'. */
17535 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17536 declarator = cp_error_declarator;
17537 if (declarator == cp_error_declarator)
17538 break;
17540 goto handle_declarator;
17542 /* Otherwise, we must be done. */
17543 else
17544 break;
17546 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17547 && token->type == CPP_OPEN_SQUARE
17548 && !cp_next_tokens_can_be_attribute_p (parser))
17550 /* Parse an array-declarator. */
17551 tree bounds, attrs;
17553 if (ctor_dtor_or_conv_p)
17554 *ctor_dtor_or_conv_p = 0;
17556 first = false;
17557 parser->default_arg_ok_p = false;
17558 parser->in_declarator_p = true;
17559 /* Consume the `['. */
17560 cp_lexer_consume_token (parser->lexer);
17561 /* Peek at the next token. */
17562 token = cp_lexer_peek_token (parser->lexer);
17563 /* If the next token is `]', then there is no
17564 constant-expression. */
17565 if (token->type != CPP_CLOSE_SQUARE)
17567 bool non_constant_p;
17568 bounds
17569 = cp_parser_constant_expression (parser,
17570 /*allow_non_constant=*/true,
17571 &non_constant_p);
17572 if (!non_constant_p)
17573 /* OK */;
17574 else if (error_operand_p (bounds))
17575 /* Already gave an error. */;
17576 else if (!parser->in_function_body
17577 || current_binding_level->kind == sk_function_parms)
17579 /* Normally, the array bound must be an integral constant
17580 expression. However, as an extension, we allow VLAs
17581 in function scopes as long as they aren't part of a
17582 parameter declaration. */
17583 cp_parser_error (parser,
17584 "array bound is not an integer constant");
17585 bounds = error_mark_node;
17587 else if (processing_template_decl
17588 && !type_dependent_expression_p (bounds))
17590 /* Remember this wasn't a constant-expression. */
17591 bounds = build_nop (TREE_TYPE (bounds), bounds);
17592 TREE_SIDE_EFFECTS (bounds) = 1;
17595 else
17596 bounds = NULL_TREE;
17597 /* Look for the closing `]'. */
17598 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17600 declarator = cp_error_declarator;
17601 break;
17604 attrs = cp_parser_std_attribute_spec_seq (parser);
17605 declarator = make_array_declarator (declarator, bounds);
17606 declarator->std_attributes = attrs;
17608 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17611 tree qualifying_scope;
17612 tree unqualified_name;
17613 tree attrs;
17614 special_function_kind sfk;
17615 bool abstract_ok;
17616 bool pack_expansion_p = false;
17617 cp_token *declarator_id_start_token;
17619 /* Parse a declarator-id */
17620 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17621 if (abstract_ok)
17623 cp_parser_parse_tentatively (parser);
17625 /* If we see an ellipsis, we should be looking at a
17626 parameter pack. */
17627 if (token->type == CPP_ELLIPSIS)
17629 /* Consume the `...' */
17630 cp_lexer_consume_token (parser->lexer);
17632 pack_expansion_p = true;
17636 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17637 unqualified_name
17638 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17639 qualifying_scope = parser->scope;
17640 if (abstract_ok)
17642 bool okay = false;
17644 if (!unqualified_name && pack_expansion_p)
17646 /* Check whether an error occurred. */
17647 okay = !cp_parser_error_occurred (parser);
17649 /* We already consumed the ellipsis to mark a
17650 parameter pack, but we have no way to report it,
17651 so abort the tentative parse. We will be exiting
17652 immediately anyway. */
17653 cp_parser_abort_tentative_parse (parser);
17655 else
17656 okay = cp_parser_parse_definitely (parser);
17658 if (!okay)
17659 unqualified_name = error_mark_node;
17660 else if (unqualified_name
17661 && (qualifying_scope
17662 || (!identifier_p (unqualified_name))))
17664 cp_parser_error (parser, "expected unqualified-id");
17665 unqualified_name = error_mark_node;
17669 if (!unqualified_name)
17670 return NULL;
17671 if (unqualified_name == error_mark_node)
17673 declarator = cp_error_declarator;
17674 pack_expansion_p = false;
17675 declarator->parameter_pack_p = false;
17676 break;
17679 attrs = cp_parser_std_attribute_spec_seq (parser);
17681 if (qualifying_scope && at_namespace_scope_p ()
17682 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17684 /* In the declaration of a member of a template class
17685 outside of the class itself, the SCOPE will sometimes
17686 be a TYPENAME_TYPE. For example, given:
17688 template <typename T>
17689 int S<T>::R::i = 3;
17691 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17692 this context, we must resolve S<T>::R to an ordinary
17693 type, rather than a typename type.
17695 The reason we normally avoid resolving TYPENAME_TYPEs
17696 is that a specialization of `S' might render
17697 `S<T>::R' not a type. However, if `S' is
17698 specialized, then this `i' will not be used, so there
17699 is no harm in resolving the types here. */
17700 tree type;
17702 /* Resolve the TYPENAME_TYPE. */
17703 type = resolve_typename_type (qualifying_scope,
17704 /*only_current_p=*/false);
17705 /* If that failed, the declarator is invalid. */
17706 if (TREE_CODE (type) == TYPENAME_TYPE)
17708 if (typedef_variant_p (type))
17709 error_at (declarator_id_start_token->location,
17710 "cannot define member of dependent typedef "
17711 "%qT", type);
17712 else
17713 error_at (declarator_id_start_token->location,
17714 "%<%T::%E%> is not a type",
17715 TYPE_CONTEXT (qualifying_scope),
17716 TYPE_IDENTIFIER (qualifying_scope));
17718 qualifying_scope = type;
17721 sfk = sfk_none;
17723 if (unqualified_name)
17725 tree class_type;
17727 if (qualifying_scope
17728 && CLASS_TYPE_P (qualifying_scope))
17729 class_type = qualifying_scope;
17730 else
17731 class_type = current_class_type;
17733 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17735 tree name_type = TREE_TYPE (unqualified_name);
17736 if (class_type && same_type_p (name_type, class_type))
17738 if (qualifying_scope
17739 && CLASSTYPE_USE_TEMPLATE (name_type))
17741 error_at (declarator_id_start_token->location,
17742 "invalid use of constructor as a template");
17743 inform (declarator_id_start_token->location,
17744 "use %<%T::%D%> instead of %<%T::%D%> to "
17745 "name the constructor in a qualified name",
17746 class_type,
17747 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17748 class_type, name_type);
17749 declarator = cp_error_declarator;
17750 break;
17752 else
17753 unqualified_name = constructor_name (class_type);
17755 else
17757 /* We do not attempt to print the declarator
17758 here because we do not have enough
17759 information about its original syntactic
17760 form. */
17761 cp_parser_error (parser, "invalid declarator");
17762 declarator = cp_error_declarator;
17763 break;
17767 if (class_type)
17769 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17770 sfk = sfk_destructor;
17771 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17772 sfk = sfk_conversion;
17773 else if (/* There's no way to declare a constructor
17774 for an anonymous type, even if the type
17775 got a name for linkage purposes. */
17776 !TYPE_WAS_ANONYMOUS (class_type)
17777 /* Handle correctly (c++/19200):
17779 struct S {
17780 struct T{};
17781 friend void S(T);
17784 and also:
17786 namespace N {
17787 void S();
17790 struct S {
17791 friend void N::S();
17792 }; */
17793 && !(friend_p
17794 && class_type != qualifying_scope)
17795 && constructor_name_p (unqualified_name,
17796 class_type))
17798 unqualified_name = constructor_name (class_type);
17799 sfk = sfk_constructor;
17801 else if (is_overloaded_fn (unqualified_name)
17802 && DECL_CONSTRUCTOR_P (get_first_fn
17803 (unqualified_name)))
17804 sfk = sfk_constructor;
17806 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17807 *ctor_dtor_or_conv_p = -1;
17810 declarator = make_id_declarator (qualifying_scope,
17811 unqualified_name,
17812 sfk);
17813 declarator->std_attributes = attrs;
17814 declarator->id_loc = token->location;
17815 declarator->parameter_pack_p = pack_expansion_p;
17817 if (pack_expansion_p)
17818 maybe_warn_variadic_templates ();
17821 handle_declarator:;
17822 scope = get_scope_of_declarator (declarator);
17823 if (scope)
17825 /* Any names that appear after the declarator-id for a
17826 member are looked up in the containing scope. */
17827 if (at_function_scope_p ())
17829 /* But declarations with qualified-ids can't appear in a
17830 function. */
17831 cp_parser_error (parser, "qualified-id in declaration");
17832 declarator = cp_error_declarator;
17833 break;
17835 pushed_scope = push_scope (scope);
17837 parser->in_declarator_p = true;
17838 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17839 || (declarator && declarator->kind == cdk_id))
17840 /* Default args are only allowed on function
17841 declarations. */
17842 parser->default_arg_ok_p = saved_default_arg_ok_p;
17843 else
17844 parser->default_arg_ok_p = false;
17846 first = false;
17848 /* We're done. */
17849 else
17850 break;
17853 /* For an abstract declarator, we might wind up with nothing at this
17854 point. That's an error; the declarator is not optional. */
17855 if (!declarator)
17856 cp_parser_error (parser, "expected declarator");
17858 /* If we entered a scope, we must exit it now. */
17859 if (pushed_scope)
17860 pop_scope (pushed_scope);
17862 parser->default_arg_ok_p = saved_default_arg_ok_p;
17863 parser->in_declarator_p = saved_in_declarator_p;
17865 return declarator;
17868 /* Parse a ptr-operator.
17870 ptr-operator:
17871 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17872 * cv-qualifier-seq [opt]
17874 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17875 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17877 GNU Extension:
17879 ptr-operator:
17880 & cv-qualifier-seq [opt]
17882 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17883 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17884 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17885 filled in with the TYPE containing the member. *CV_QUALS is
17886 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17887 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17888 Note that the tree codes returned by this function have nothing
17889 to do with the types of trees that will be eventually be created
17890 to represent the pointer or reference type being parsed. They are
17891 just constants with suggestive names. */
17892 static enum tree_code
17893 cp_parser_ptr_operator (cp_parser* parser,
17894 tree* type,
17895 cp_cv_quals *cv_quals,
17896 tree *attributes)
17898 enum tree_code code = ERROR_MARK;
17899 cp_token *token;
17900 tree attrs = NULL_TREE;
17902 /* Assume that it's not a pointer-to-member. */
17903 *type = NULL_TREE;
17904 /* And that there are no cv-qualifiers. */
17905 *cv_quals = TYPE_UNQUALIFIED;
17907 /* Peek at the next token. */
17908 token = cp_lexer_peek_token (parser->lexer);
17910 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17911 if (token->type == CPP_MULT)
17912 code = INDIRECT_REF;
17913 else if (token->type == CPP_AND)
17914 code = ADDR_EXPR;
17915 else if ((cxx_dialect != cxx98) &&
17916 token->type == CPP_AND_AND) /* C++0x only */
17917 code = NON_LVALUE_EXPR;
17919 if (code != ERROR_MARK)
17921 /* Consume the `*', `&' or `&&'. */
17922 cp_lexer_consume_token (parser->lexer);
17924 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17925 `&', if we are allowing GNU extensions. (The only qualifier
17926 that can legally appear after `&' is `restrict', but that is
17927 enforced during semantic analysis. */
17928 if (code == INDIRECT_REF
17929 || cp_parser_allow_gnu_extensions_p (parser))
17930 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17932 attrs = cp_parser_std_attribute_spec_seq (parser);
17933 if (attributes != NULL)
17934 *attributes = attrs;
17936 else
17938 /* Try the pointer-to-member case. */
17939 cp_parser_parse_tentatively (parser);
17940 /* Look for the optional `::' operator. */
17941 cp_parser_global_scope_opt (parser,
17942 /*current_scope_valid_p=*/false);
17943 /* Look for the nested-name specifier. */
17944 token = cp_lexer_peek_token (parser->lexer);
17945 cp_parser_nested_name_specifier (parser,
17946 /*typename_keyword_p=*/false,
17947 /*check_dependency_p=*/true,
17948 /*type_p=*/false,
17949 /*is_declaration=*/false);
17950 /* If we found it, and the next token is a `*', then we are
17951 indeed looking at a pointer-to-member operator. */
17952 if (!cp_parser_error_occurred (parser)
17953 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17955 /* Indicate that the `*' operator was used. */
17956 code = INDIRECT_REF;
17958 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17959 error_at (token->location, "%qD is a namespace", parser->scope);
17960 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17961 error_at (token->location, "cannot form pointer to member of "
17962 "non-class %q#T", parser->scope);
17963 else
17965 /* The type of which the member is a member is given by the
17966 current SCOPE. */
17967 *type = parser->scope;
17968 /* The next name will not be qualified. */
17969 parser->scope = NULL_TREE;
17970 parser->qualifying_scope = NULL_TREE;
17971 parser->object_scope = NULL_TREE;
17972 /* Look for optional c++11 attributes. */
17973 attrs = cp_parser_std_attribute_spec_seq (parser);
17974 if (attributes != NULL)
17975 *attributes = attrs;
17976 /* Look for the optional cv-qualifier-seq. */
17977 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17980 /* If that didn't work we don't have a ptr-operator. */
17981 if (!cp_parser_parse_definitely (parser))
17982 cp_parser_error (parser, "expected ptr-operator");
17985 return code;
17988 /* Parse an (optional) cv-qualifier-seq.
17990 cv-qualifier-seq:
17991 cv-qualifier cv-qualifier-seq [opt]
17993 cv-qualifier:
17994 const
17995 volatile
17997 GNU Extension:
17999 cv-qualifier:
18000 __restrict__
18002 Returns a bitmask representing the cv-qualifiers. */
18004 static cp_cv_quals
18005 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18007 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18009 while (true)
18011 cp_token *token;
18012 cp_cv_quals cv_qualifier;
18014 /* Peek at the next token. */
18015 token = cp_lexer_peek_token (parser->lexer);
18016 /* See if it's a cv-qualifier. */
18017 switch (token->keyword)
18019 case RID_CONST:
18020 cv_qualifier = TYPE_QUAL_CONST;
18021 break;
18023 case RID_VOLATILE:
18024 cv_qualifier = TYPE_QUAL_VOLATILE;
18025 break;
18027 case RID_RESTRICT:
18028 cv_qualifier = TYPE_QUAL_RESTRICT;
18029 break;
18031 default:
18032 cv_qualifier = TYPE_UNQUALIFIED;
18033 break;
18036 if (!cv_qualifier)
18037 break;
18039 if (cv_quals & cv_qualifier)
18041 error_at (token->location, "duplicate cv-qualifier");
18042 cp_lexer_purge_token (parser->lexer);
18044 else
18046 cp_lexer_consume_token (parser->lexer);
18047 cv_quals |= cv_qualifier;
18051 return cv_quals;
18054 /* Parse an (optional) ref-qualifier
18056 ref-qualifier:
18060 Returns cp_ref_qualifier representing ref-qualifier. */
18062 static cp_ref_qualifier
18063 cp_parser_ref_qualifier_opt (cp_parser* parser)
18065 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18067 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18068 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18069 return ref_qual;
18071 while (true)
18073 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18074 cp_token *token = cp_lexer_peek_token (parser->lexer);
18076 switch (token->type)
18078 case CPP_AND:
18079 curr_ref_qual = REF_QUAL_LVALUE;
18080 break;
18082 case CPP_AND_AND:
18083 curr_ref_qual = REF_QUAL_RVALUE;
18084 break;
18086 default:
18087 curr_ref_qual = REF_QUAL_NONE;
18088 break;
18091 if (!curr_ref_qual)
18092 break;
18093 else if (ref_qual)
18095 error_at (token->location, "multiple ref-qualifiers");
18096 cp_lexer_purge_token (parser->lexer);
18098 else
18100 ref_qual = curr_ref_qual;
18101 cp_lexer_consume_token (parser->lexer);
18105 return ref_qual;
18108 /* Parse an (optional) virt-specifier-seq.
18110 virt-specifier-seq:
18111 virt-specifier virt-specifier-seq [opt]
18113 virt-specifier:
18114 override
18115 final
18117 Returns a bitmask representing the virt-specifiers. */
18119 static cp_virt_specifiers
18120 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18122 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18124 while (true)
18126 cp_token *token;
18127 cp_virt_specifiers virt_specifier;
18129 /* Peek at the next token. */
18130 token = cp_lexer_peek_token (parser->lexer);
18131 /* See if it's a virt-specifier-qualifier. */
18132 if (token->type != CPP_NAME)
18133 break;
18134 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18136 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18137 virt_specifier = VIRT_SPEC_OVERRIDE;
18139 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18141 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18142 virt_specifier = VIRT_SPEC_FINAL;
18144 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18146 virt_specifier = VIRT_SPEC_FINAL;
18148 else
18149 break;
18151 if (virt_specifiers & virt_specifier)
18153 error_at (token->location, "duplicate virt-specifier");
18154 cp_lexer_purge_token (parser->lexer);
18156 else
18158 cp_lexer_consume_token (parser->lexer);
18159 virt_specifiers |= virt_specifier;
18162 return virt_specifiers;
18165 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18166 is in scope even though it isn't real. */
18168 void
18169 inject_this_parameter (tree ctype, cp_cv_quals quals)
18171 tree this_parm;
18173 if (current_class_ptr)
18175 /* We don't clear this between NSDMIs. Is it already what we want? */
18176 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18177 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18178 && cp_type_quals (type) == quals)
18179 return;
18182 this_parm = build_this_parm (ctype, quals);
18183 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18184 current_class_ptr = NULL_TREE;
18185 current_class_ref
18186 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18187 current_class_ptr = this_parm;
18190 /* Return true iff our current scope is a non-static data member
18191 initializer. */
18193 bool
18194 parsing_nsdmi (void)
18196 /* We recognize NSDMI context by the context-less 'this' pointer set up
18197 by the function above. */
18198 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18199 return true;
18200 return false;
18203 /* Parse a late-specified return type, if any. This is not a separate
18204 non-terminal, but part of a function declarator, which looks like
18206 -> trailing-type-specifier-seq abstract-declarator(opt)
18208 Returns the type indicated by the type-id.
18210 In addition to this this parses any queued up omp declare simd
18211 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18213 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18214 function. */
18216 static tree
18217 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18218 cp_cv_quals quals)
18220 cp_token *token;
18221 tree type = NULL_TREE;
18222 bool declare_simd_p = (parser->omp_declare_simd
18223 && declarator
18224 && declarator->kind == cdk_id);
18226 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18227 && declarator && declarator->kind == cdk_id);
18229 /* Peek at the next token. */
18230 token = cp_lexer_peek_token (parser->lexer);
18231 /* A late-specified return type is indicated by an initial '->'. */
18232 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18233 return NULL_TREE;
18235 tree save_ccp = current_class_ptr;
18236 tree save_ccr = current_class_ref;
18237 if (quals >= 0)
18239 /* DR 1207: 'this' is in scope in the trailing return type. */
18240 inject_this_parameter (current_class_type, quals);
18243 if (token->type == CPP_DEREF)
18245 /* Consume the ->. */
18246 cp_lexer_consume_token (parser->lexer);
18248 type = cp_parser_trailing_type_id (parser);
18251 if (cilk_simd_fn_vector_p)
18252 declarator->std_attributes
18253 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18254 declarator->std_attributes);
18255 if (declare_simd_p)
18256 declarator->std_attributes
18257 = cp_parser_late_parsing_omp_declare_simd (parser,
18258 declarator->std_attributes);
18260 if (quals >= 0)
18262 current_class_ptr = save_ccp;
18263 current_class_ref = save_ccr;
18266 return type;
18269 /* Parse a declarator-id.
18271 declarator-id:
18272 id-expression
18273 :: [opt] nested-name-specifier [opt] type-name
18275 In the `id-expression' case, the value returned is as for
18276 cp_parser_id_expression if the id-expression was an unqualified-id.
18277 If the id-expression was a qualified-id, then a SCOPE_REF is
18278 returned. The first operand is the scope (either a NAMESPACE_DECL
18279 or TREE_TYPE), but the second is still just a representation of an
18280 unqualified-id. */
18282 static tree
18283 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18285 tree id;
18286 /* The expression must be an id-expression. Assume that qualified
18287 names are the names of types so that:
18289 template <class T>
18290 int S<T>::R::i = 3;
18292 will work; we must treat `S<T>::R' as the name of a type.
18293 Similarly, assume that qualified names are templates, where
18294 required, so that:
18296 template <class T>
18297 int S<T>::R<T>::i = 3;
18299 will work, too. */
18300 id = cp_parser_id_expression (parser,
18301 /*template_keyword_p=*/false,
18302 /*check_dependency_p=*/false,
18303 /*template_p=*/NULL,
18304 /*declarator_p=*/true,
18305 optional_p);
18306 if (id && BASELINK_P (id))
18307 id = BASELINK_FUNCTIONS (id);
18308 return id;
18311 /* Parse a type-id.
18313 type-id:
18314 type-specifier-seq abstract-declarator [opt]
18316 Returns the TYPE specified. */
18318 static tree
18319 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18320 bool is_trailing_return)
18322 cp_decl_specifier_seq type_specifier_seq;
18323 cp_declarator *abstract_declarator;
18325 /* Parse the type-specifier-seq. */
18326 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18327 is_trailing_return,
18328 &type_specifier_seq);
18329 if (type_specifier_seq.type == error_mark_node)
18330 return error_mark_node;
18332 /* There might or might not be an abstract declarator. */
18333 cp_parser_parse_tentatively (parser);
18334 /* Look for the declarator. */
18335 abstract_declarator
18336 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18337 /*parenthesized_p=*/NULL,
18338 /*member_p=*/false,
18339 /*friend_p=*/false);
18340 /* Check to see if there really was a declarator. */
18341 if (!cp_parser_parse_definitely (parser))
18342 abstract_declarator = NULL;
18344 if (type_specifier_seq.type
18345 /* None of the valid uses of 'auto' in C++14 involve the type-id
18346 nonterminal, but it is valid in a trailing-return-type. */
18347 && !(cxx_dialect >= cxx14 && is_trailing_return)
18348 && type_uses_auto (type_specifier_seq.type))
18350 /* A type-id with type 'auto' is only ok if the abstract declarator
18351 is a function declarator with a late-specified return type. */
18352 if (abstract_declarator
18353 && abstract_declarator->kind == cdk_function
18354 && abstract_declarator->u.function.late_return_type)
18355 /* OK */;
18356 else
18358 error ("invalid use of %<auto%>");
18359 return error_mark_node;
18363 return groktypename (&type_specifier_seq, abstract_declarator,
18364 is_template_arg);
18367 static tree cp_parser_type_id (cp_parser *parser)
18369 return cp_parser_type_id_1 (parser, false, false);
18372 static tree cp_parser_template_type_arg (cp_parser *parser)
18374 tree r;
18375 const char *saved_message = parser->type_definition_forbidden_message;
18376 parser->type_definition_forbidden_message
18377 = G_("types may not be defined in template arguments");
18378 r = cp_parser_type_id_1 (parser, true, false);
18379 parser->type_definition_forbidden_message = saved_message;
18380 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18382 error ("invalid use of %<auto%> in template argument");
18383 r = error_mark_node;
18385 return r;
18388 static tree cp_parser_trailing_type_id (cp_parser *parser)
18390 return cp_parser_type_id_1 (parser, false, true);
18393 /* Parse a type-specifier-seq.
18395 type-specifier-seq:
18396 type-specifier type-specifier-seq [opt]
18398 GNU extension:
18400 type-specifier-seq:
18401 attributes type-specifier-seq [opt]
18403 If IS_DECLARATION is true, we are at the start of a "condition" or
18404 exception-declaration, so we might be followed by a declarator-id.
18406 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18407 i.e. we've just seen "->".
18409 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18411 static void
18412 cp_parser_type_specifier_seq (cp_parser* parser,
18413 bool is_declaration,
18414 bool is_trailing_return,
18415 cp_decl_specifier_seq *type_specifier_seq)
18417 bool seen_type_specifier = false;
18418 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18419 cp_token *start_token = NULL;
18421 /* Clear the TYPE_SPECIFIER_SEQ. */
18422 clear_decl_specs (type_specifier_seq);
18424 /* In the context of a trailing return type, enum E { } is an
18425 elaborated-type-specifier followed by a function-body, not an
18426 enum-specifier. */
18427 if (is_trailing_return)
18428 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18430 /* Parse the type-specifiers and attributes. */
18431 while (true)
18433 tree type_specifier;
18434 bool is_cv_qualifier;
18436 /* Check for attributes first. */
18437 if (cp_next_tokens_can_be_attribute_p (parser))
18439 type_specifier_seq->attributes =
18440 chainon (type_specifier_seq->attributes,
18441 cp_parser_attributes_opt (parser));
18442 continue;
18445 /* record the token of the beginning of the type specifier seq,
18446 for error reporting purposes*/
18447 if (!start_token)
18448 start_token = cp_lexer_peek_token (parser->lexer);
18450 /* Look for the type-specifier. */
18451 type_specifier = cp_parser_type_specifier (parser,
18452 flags,
18453 type_specifier_seq,
18454 /*is_declaration=*/false,
18455 NULL,
18456 &is_cv_qualifier);
18457 if (!type_specifier)
18459 /* If the first type-specifier could not be found, this is not a
18460 type-specifier-seq at all. */
18461 if (!seen_type_specifier)
18463 /* Set in_declarator_p to avoid skipping to the semicolon. */
18464 int in_decl = parser->in_declarator_p;
18465 parser->in_declarator_p = true;
18467 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18468 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18469 cp_parser_error (parser, "expected type-specifier");
18471 parser->in_declarator_p = in_decl;
18473 type_specifier_seq->type = error_mark_node;
18474 return;
18476 /* If subsequent type-specifiers could not be found, the
18477 type-specifier-seq is complete. */
18478 break;
18481 seen_type_specifier = true;
18482 /* The standard says that a condition can be:
18484 type-specifier-seq declarator = assignment-expression
18486 However, given:
18488 struct S {};
18489 if (int S = ...)
18491 we should treat the "S" as a declarator, not as a
18492 type-specifier. The standard doesn't say that explicitly for
18493 type-specifier-seq, but it does say that for
18494 decl-specifier-seq in an ordinary declaration. Perhaps it
18495 would be clearer just to allow a decl-specifier-seq here, and
18496 then add a semantic restriction that if any decl-specifiers
18497 that are not type-specifiers appear, the program is invalid. */
18498 if (is_declaration && !is_cv_qualifier)
18499 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18503 /* Return whether the function currently being declared has an associated
18504 template parameter list. */
18506 static bool
18507 function_being_declared_is_template_p (cp_parser* parser)
18509 if (!current_template_parms || processing_template_parmlist)
18510 return false;
18512 if (parser->implicit_template_scope)
18513 return true;
18515 if (at_class_scope_p ()
18516 && TYPE_BEING_DEFINED (current_class_type))
18517 return parser->num_template_parameter_lists != 0;
18519 return ((int) parser->num_template_parameter_lists > template_class_depth
18520 (current_class_type));
18523 /* Parse a parameter-declaration-clause.
18525 parameter-declaration-clause:
18526 parameter-declaration-list [opt] ... [opt]
18527 parameter-declaration-list , ...
18529 Returns a representation for the parameter declarations. A return
18530 value of NULL indicates a parameter-declaration-clause consisting
18531 only of an ellipsis. */
18533 static tree
18534 cp_parser_parameter_declaration_clause (cp_parser* parser)
18536 tree parameters;
18537 cp_token *token;
18538 bool ellipsis_p;
18539 bool is_error;
18541 struct cleanup {
18542 cp_parser* parser;
18543 int auto_is_implicit_function_template_parm_p;
18544 ~cleanup() {
18545 parser->auto_is_implicit_function_template_parm_p
18546 = auto_is_implicit_function_template_parm_p;
18548 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18550 (void) cleanup;
18552 if (!processing_specialization
18553 && !processing_template_parmlist
18554 && !processing_explicit_instantiation)
18555 if (!current_function_decl
18556 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18557 parser->auto_is_implicit_function_template_parm_p = true;
18559 /* Peek at the next token. */
18560 token = cp_lexer_peek_token (parser->lexer);
18561 /* Check for trivial parameter-declaration-clauses. */
18562 if (token->type == CPP_ELLIPSIS)
18564 /* Consume the `...' token. */
18565 cp_lexer_consume_token (parser->lexer);
18566 return NULL_TREE;
18568 else if (token->type == CPP_CLOSE_PAREN)
18569 /* There are no parameters. */
18571 #ifndef NO_IMPLICIT_EXTERN_C
18572 if (in_system_header_at (input_location)
18573 && current_class_type == NULL
18574 && current_lang_name == lang_name_c)
18575 return NULL_TREE;
18576 else
18577 #endif
18578 return void_list_node;
18580 /* Check for `(void)', too, which is a special case. */
18581 else if (token->keyword == RID_VOID
18582 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18583 == CPP_CLOSE_PAREN))
18585 /* Consume the `void' token. */
18586 cp_lexer_consume_token (parser->lexer);
18587 /* There are no parameters. */
18588 return void_list_node;
18591 /* Parse the parameter-declaration-list. */
18592 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18593 /* If a parse error occurred while parsing the
18594 parameter-declaration-list, then the entire
18595 parameter-declaration-clause is erroneous. */
18596 if (is_error)
18597 return NULL;
18599 /* Peek at the next token. */
18600 token = cp_lexer_peek_token (parser->lexer);
18601 /* If it's a `,', the clause should terminate with an ellipsis. */
18602 if (token->type == CPP_COMMA)
18604 /* Consume the `,'. */
18605 cp_lexer_consume_token (parser->lexer);
18606 /* Expect an ellipsis. */
18607 ellipsis_p
18608 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18610 /* It might also be `...' if the optional trailing `,' was
18611 omitted. */
18612 else if (token->type == CPP_ELLIPSIS)
18614 /* Consume the `...' token. */
18615 cp_lexer_consume_token (parser->lexer);
18616 /* And remember that we saw it. */
18617 ellipsis_p = true;
18619 else
18620 ellipsis_p = false;
18622 /* Finish the parameter list. */
18623 if (!ellipsis_p)
18624 parameters = chainon (parameters, void_list_node);
18626 return parameters;
18629 /* Parse a parameter-declaration-list.
18631 parameter-declaration-list:
18632 parameter-declaration
18633 parameter-declaration-list , parameter-declaration
18635 Returns a representation of the parameter-declaration-list, as for
18636 cp_parser_parameter_declaration_clause. However, the
18637 `void_list_node' is never appended to the list. Upon return,
18638 *IS_ERROR will be true iff an error occurred. */
18640 static tree
18641 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18643 tree parameters = NULL_TREE;
18644 tree *tail = &parameters;
18645 bool saved_in_unbraced_linkage_specification_p;
18646 int index = 0;
18648 /* Assume all will go well. */
18649 *is_error = false;
18650 /* The special considerations that apply to a function within an
18651 unbraced linkage specifications do not apply to the parameters
18652 to the function. */
18653 saved_in_unbraced_linkage_specification_p
18654 = parser->in_unbraced_linkage_specification_p;
18655 parser->in_unbraced_linkage_specification_p = false;
18657 /* Look for more parameters. */
18658 while (true)
18660 cp_parameter_declarator *parameter;
18661 tree decl = error_mark_node;
18662 bool parenthesized_p = false;
18663 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18664 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18665 (current_template_parms)) : 0);
18667 /* Parse the parameter. */
18668 parameter
18669 = cp_parser_parameter_declaration (parser,
18670 /*template_parm_p=*/false,
18671 &parenthesized_p);
18673 /* We don't know yet if the enclosing context is deprecated, so wait
18674 and warn in grokparms if appropriate. */
18675 deprecated_state = DEPRECATED_SUPPRESS;
18677 if (parameter)
18679 /* If a function parameter pack was specified and an implicit template
18680 parameter was introduced during cp_parser_parameter_declaration,
18681 change any implicit parameters introduced into packs. */
18682 if (parser->implicit_template_parms
18683 && parameter->declarator
18684 && parameter->declarator->parameter_pack_p)
18686 int latest_template_parm_idx = TREE_VEC_LENGTH
18687 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18689 if (latest_template_parm_idx != template_parm_idx)
18690 parameter->decl_specifiers.type = convert_generic_types_to_packs
18691 (parameter->decl_specifiers.type,
18692 template_parm_idx, latest_template_parm_idx);
18695 decl = grokdeclarator (parameter->declarator,
18696 &parameter->decl_specifiers,
18697 PARM,
18698 parameter->default_argument != NULL_TREE,
18699 &parameter->decl_specifiers.attributes);
18702 deprecated_state = DEPRECATED_NORMAL;
18704 /* If a parse error occurred parsing the parameter declaration,
18705 then the entire parameter-declaration-list is erroneous. */
18706 if (decl == error_mark_node)
18708 *is_error = true;
18709 parameters = error_mark_node;
18710 break;
18713 if (parameter->decl_specifiers.attributes)
18714 cplus_decl_attributes (&decl,
18715 parameter->decl_specifiers.attributes,
18717 if (DECL_NAME (decl))
18718 decl = pushdecl (decl);
18720 if (decl != error_mark_node)
18722 retrofit_lang_decl (decl);
18723 DECL_PARM_INDEX (decl) = ++index;
18724 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18727 /* Add the new parameter to the list. */
18728 *tail = build_tree_list (parameter->default_argument, decl);
18729 tail = &TREE_CHAIN (*tail);
18731 /* Peek at the next token. */
18732 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18733 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18734 /* These are for Objective-C++ */
18735 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18736 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18737 /* The parameter-declaration-list is complete. */
18738 break;
18739 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18741 cp_token *token;
18743 /* Peek at the next token. */
18744 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18745 /* If it's an ellipsis, then the list is complete. */
18746 if (token->type == CPP_ELLIPSIS)
18747 break;
18748 /* Otherwise, there must be more parameters. Consume the
18749 `,'. */
18750 cp_lexer_consume_token (parser->lexer);
18751 /* When parsing something like:
18753 int i(float f, double d)
18755 we can tell after seeing the declaration for "f" that we
18756 are not looking at an initialization of a variable "i",
18757 but rather at the declaration of a function "i".
18759 Due to the fact that the parsing of template arguments
18760 (as specified to a template-id) requires backtracking we
18761 cannot use this technique when inside a template argument
18762 list. */
18763 if (!parser->in_template_argument_list_p
18764 && !parser->in_type_id_in_expr_p
18765 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18766 /* However, a parameter-declaration of the form
18767 "float(f)" (which is a valid declaration of a
18768 parameter "f") can also be interpreted as an
18769 expression (the conversion of "f" to "float"). */
18770 && !parenthesized_p)
18771 cp_parser_commit_to_tentative_parse (parser);
18773 else
18775 cp_parser_error (parser, "expected %<,%> or %<...%>");
18776 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18777 cp_parser_skip_to_closing_parenthesis (parser,
18778 /*recovering=*/true,
18779 /*or_comma=*/false,
18780 /*consume_paren=*/false);
18781 break;
18785 parser->in_unbraced_linkage_specification_p
18786 = saved_in_unbraced_linkage_specification_p;
18788 /* Reset implicit_template_scope if we are about to leave the function
18789 parameter list that introduced it. Note that for out-of-line member
18790 definitions, there will be one or more class scopes before we get to
18791 the template parameter scope. */
18793 if (cp_binding_level *its = parser->implicit_template_scope)
18794 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18796 while (maybe_its->kind == sk_class)
18797 maybe_its = maybe_its->level_chain;
18798 if (maybe_its == its)
18800 parser->implicit_template_parms = 0;
18801 parser->implicit_template_scope = 0;
18805 return parameters;
18808 /* Parse a parameter declaration.
18810 parameter-declaration:
18811 decl-specifier-seq ... [opt] declarator
18812 decl-specifier-seq declarator = assignment-expression
18813 decl-specifier-seq ... [opt] abstract-declarator [opt]
18814 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18816 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18817 declares a template parameter. (In that case, a non-nested `>'
18818 token encountered during the parsing of the assignment-expression
18819 is not interpreted as a greater-than operator.)
18821 Returns a representation of the parameter, or NULL if an error
18822 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18823 true iff the declarator is of the form "(p)". */
18825 static cp_parameter_declarator *
18826 cp_parser_parameter_declaration (cp_parser *parser,
18827 bool template_parm_p,
18828 bool *parenthesized_p)
18830 int declares_class_or_enum;
18831 cp_decl_specifier_seq decl_specifiers;
18832 cp_declarator *declarator;
18833 tree default_argument;
18834 cp_token *token = NULL, *declarator_token_start = NULL;
18835 const char *saved_message;
18837 /* In a template parameter, `>' is not an operator.
18839 [temp.param]
18841 When parsing a default template-argument for a non-type
18842 template-parameter, the first non-nested `>' is taken as the end
18843 of the template parameter-list rather than a greater-than
18844 operator. */
18846 /* Type definitions may not appear in parameter types. */
18847 saved_message = parser->type_definition_forbidden_message;
18848 parser->type_definition_forbidden_message
18849 = G_("types may not be defined in parameter types");
18851 /* Parse the declaration-specifiers. */
18852 cp_parser_decl_specifier_seq (parser,
18853 CP_PARSER_FLAGS_NONE,
18854 &decl_specifiers,
18855 &declares_class_or_enum);
18857 /* Complain about missing 'typename' or other invalid type names. */
18858 if (!decl_specifiers.any_type_specifiers_p
18859 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18860 decl_specifiers.type = error_mark_node;
18862 /* If an error occurred, there's no reason to attempt to parse the
18863 rest of the declaration. */
18864 if (cp_parser_error_occurred (parser))
18866 parser->type_definition_forbidden_message = saved_message;
18867 return NULL;
18870 /* Peek at the next token. */
18871 token = cp_lexer_peek_token (parser->lexer);
18873 /* If the next token is a `)', `,', `=', `>', or `...', then there
18874 is no declarator. However, when variadic templates are enabled,
18875 there may be a declarator following `...'. */
18876 if (token->type == CPP_CLOSE_PAREN
18877 || token->type == CPP_COMMA
18878 || token->type == CPP_EQ
18879 || token->type == CPP_GREATER)
18881 declarator = NULL;
18882 if (parenthesized_p)
18883 *parenthesized_p = false;
18885 /* Otherwise, there should be a declarator. */
18886 else
18888 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18889 parser->default_arg_ok_p = false;
18891 /* After seeing a decl-specifier-seq, if the next token is not a
18892 "(", there is no possibility that the code is a valid
18893 expression. Therefore, if parsing tentatively, we commit at
18894 this point. */
18895 if (!parser->in_template_argument_list_p
18896 /* In an expression context, having seen:
18898 (int((char ...
18900 we cannot be sure whether we are looking at a
18901 function-type (taking a "char" as a parameter) or a cast
18902 of some object of type "char" to "int". */
18903 && !parser->in_type_id_in_expr_p
18904 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18905 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18906 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18907 cp_parser_commit_to_tentative_parse (parser);
18908 /* Parse the declarator. */
18909 declarator_token_start = token;
18910 declarator = cp_parser_declarator (parser,
18911 CP_PARSER_DECLARATOR_EITHER,
18912 /*ctor_dtor_or_conv_p=*/NULL,
18913 parenthesized_p,
18914 /*member_p=*/false,
18915 /*friend_p=*/false);
18916 parser->default_arg_ok_p = saved_default_arg_ok_p;
18917 /* After the declarator, allow more attributes. */
18918 decl_specifiers.attributes
18919 = chainon (decl_specifiers.attributes,
18920 cp_parser_attributes_opt (parser));
18923 /* If the next token is an ellipsis, and we have not seen a
18924 declarator name, and the type of the declarator contains parameter
18925 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18926 a parameter pack expansion expression. Otherwise, leave the
18927 ellipsis for a C-style variadic function. */
18928 token = cp_lexer_peek_token (parser->lexer);
18929 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18931 tree type = decl_specifiers.type;
18933 if (type && DECL_P (type))
18934 type = TREE_TYPE (type);
18936 if (type
18937 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18938 && declarator_can_be_parameter_pack (declarator)
18939 && (!declarator || !declarator->parameter_pack_p)
18940 && uses_parameter_packs (type))
18942 /* Consume the `...'. */
18943 cp_lexer_consume_token (parser->lexer);
18944 maybe_warn_variadic_templates ();
18946 /* Build a pack expansion type */
18947 if (declarator)
18948 declarator->parameter_pack_p = true;
18949 else
18950 decl_specifiers.type = make_pack_expansion (type);
18954 /* The restriction on defining new types applies only to the type
18955 of the parameter, not to the default argument. */
18956 parser->type_definition_forbidden_message = saved_message;
18958 /* If the next token is `=', then process a default argument. */
18959 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18961 token = cp_lexer_peek_token (parser->lexer);
18962 /* If we are defining a class, then the tokens that make up the
18963 default argument must be saved and processed later. */
18964 if (!template_parm_p && at_class_scope_p ()
18965 && TYPE_BEING_DEFINED (current_class_type)
18966 && !LAMBDA_TYPE_P (current_class_type))
18967 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18968 /* Outside of a class definition, we can just parse the
18969 assignment-expression. */
18970 else
18971 default_argument
18972 = cp_parser_default_argument (parser, template_parm_p);
18974 if (!parser->default_arg_ok_p)
18976 if (flag_permissive)
18977 warning (0, "deprecated use of default argument for parameter of non-function");
18978 else
18980 error_at (token->location,
18981 "default arguments are only "
18982 "permitted for function parameters");
18983 default_argument = NULL_TREE;
18986 else if ((declarator && declarator->parameter_pack_p)
18987 || (decl_specifiers.type
18988 && PACK_EXPANSION_P (decl_specifiers.type)))
18990 /* Find the name of the parameter pack. */
18991 cp_declarator *id_declarator = declarator;
18992 while (id_declarator && id_declarator->kind != cdk_id)
18993 id_declarator = id_declarator->declarator;
18995 if (id_declarator && id_declarator->kind == cdk_id)
18996 error_at (declarator_token_start->location,
18997 template_parm_p
18998 ? G_("template parameter pack %qD "
18999 "cannot have a default argument")
19000 : G_("parameter pack %qD cannot have "
19001 "a default argument"),
19002 id_declarator->u.id.unqualified_name);
19003 else
19004 error_at (declarator_token_start->location,
19005 template_parm_p
19006 ? G_("template parameter pack cannot have "
19007 "a default argument")
19008 : G_("parameter pack cannot have a "
19009 "default argument"));
19011 default_argument = NULL_TREE;
19014 else
19015 default_argument = NULL_TREE;
19017 return make_parameter_declarator (&decl_specifiers,
19018 declarator,
19019 default_argument);
19022 /* Parse a default argument and return it.
19024 TEMPLATE_PARM_P is true if this is a default argument for a
19025 non-type template parameter. */
19026 static tree
19027 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19029 tree default_argument = NULL_TREE;
19030 bool saved_greater_than_is_operator_p;
19031 bool saved_local_variables_forbidden_p;
19032 bool non_constant_p, is_direct_init;
19034 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19035 set correctly. */
19036 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19037 parser->greater_than_is_operator_p = !template_parm_p;
19038 /* Local variable names (and the `this' keyword) may not
19039 appear in a default argument. */
19040 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19041 parser->local_variables_forbidden_p = true;
19042 /* Parse the assignment-expression. */
19043 if (template_parm_p)
19044 push_deferring_access_checks (dk_no_deferred);
19045 tree saved_class_ptr = NULL_TREE;
19046 tree saved_class_ref = NULL_TREE;
19047 /* The "this" pointer is not valid in a default argument. */
19048 if (cfun)
19050 saved_class_ptr = current_class_ptr;
19051 cp_function_chain->x_current_class_ptr = NULL_TREE;
19052 saved_class_ref = current_class_ref;
19053 cp_function_chain->x_current_class_ref = NULL_TREE;
19055 default_argument
19056 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19057 /* Restore the "this" pointer. */
19058 if (cfun)
19060 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19061 cp_function_chain->x_current_class_ref = saved_class_ref;
19063 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19064 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19065 if (template_parm_p)
19066 pop_deferring_access_checks ();
19067 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19068 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19070 return default_argument;
19073 /* Parse a function-body.
19075 function-body:
19076 compound_statement */
19078 static void
19079 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19081 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19084 /* Parse a ctor-initializer-opt followed by a function-body. Return
19085 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19086 is true we are parsing a function-try-block. */
19088 static bool
19089 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19090 bool in_function_try_block)
19092 tree body, list;
19093 bool ctor_initializer_p;
19094 const bool check_body_p =
19095 DECL_CONSTRUCTOR_P (current_function_decl)
19096 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19097 tree last = NULL;
19099 /* Begin the function body. */
19100 body = begin_function_body ();
19101 /* Parse the optional ctor-initializer. */
19102 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19104 /* If we're parsing a constexpr constructor definition, we need
19105 to check that the constructor body is indeed empty. However,
19106 before we get to cp_parser_function_body lot of junk has been
19107 generated, so we can't just check that we have an empty block.
19108 Rather we take a snapshot of the outermost block, and check whether
19109 cp_parser_function_body changed its state. */
19110 if (check_body_p)
19112 list = cur_stmt_list;
19113 if (STATEMENT_LIST_TAIL (list))
19114 last = STATEMENT_LIST_TAIL (list)->stmt;
19116 /* Parse the function-body. */
19117 cp_parser_function_body (parser, in_function_try_block);
19118 if (check_body_p)
19119 check_constexpr_ctor_body (last, list, /*complain=*/true);
19120 /* Finish the function body. */
19121 finish_function_body (body);
19123 return ctor_initializer_p;
19126 /* Parse an initializer.
19128 initializer:
19129 = initializer-clause
19130 ( expression-list )
19132 Returns an expression representing the initializer. If no
19133 initializer is present, NULL_TREE is returned.
19135 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19136 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19137 set to TRUE if there is no initializer present. If there is an
19138 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19139 is set to true; otherwise it is set to false. */
19141 static tree
19142 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19143 bool* non_constant_p)
19145 cp_token *token;
19146 tree init;
19148 /* Peek at the next token. */
19149 token = cp_lexer_peek_token (parser->lexer);
19151 /* Let our caller know whether or not this initializer was
19152 parenthesized. */
19153 *is_direct_init = (token->type != CPP_EQ);
19154 /* Assume that the initializer is constant. */
19155 *non_constant_p = false;
19157 if (token->type == CPP_EQ)
19159 /* Consume the `='. */
19160 cp_lexer_consume_token (parser->lexer);
19161 /* Parse the initializer-clause. */
19162 init = cp_parser_initializer_clause (parser, non_constant_p);
19164 else if (token->type == CPP_OPEN_PAREN)
19166 vec<tree, va_gc> *vec;
19167 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19168 /*cast_p=*/false,
19169 /*allow_expansion_p=*/true,
19170 non_constant_p);
19171 if (vec == NULL)
19172 return error_mark_node;
19173 init = build_tree_list_vec (vec);
19174 release_tree_vector (vec);
19176 else if (token->type == CPP_OPEN_BRACE)
19178 cp_lexer_set_source_position (parser->lexer);
19179 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19180 init = cp_parser_braced_list (parser, non_constant_p);
19181 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19183 else
19185 /* Anything else is an error. */
19186 cp_parser_error (parser, "expected initializer");
19187 init = error_mark_node;
19190 return init;
19193 /* Parse an initializer-clause.
19195 initializer-clause:
19196 assignment-expression
19197 braced-init-list
19199 Returns an expression representing the initializer.
19201 If the `assignment-expression' production is used the value
19202 returned is simply a representation for the expression.
19204 Otherwise, calls cp_parser_braced_list. */
19206 static tree
19207 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19209 tree initializer;
19211 /* Assume the expression is constant. */
19212 *non_constant_p = false;
19214 /* If it is not a `{', then we are looking at an
19215 assignment-expression. */
19216 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19218 initializer
19219 = cp_parser_constant_expression (parser,
19220 /*allow_non_constant_p=*/true,
19221 non_constant_p);
19223 else
19224 initializer = cp_parser_braced_list (parser, non_constant_p);
19226 return initializer;
19229 /* Parse a brace-enclosed initializer list.
19231 braced-init-list:
19232 { initializer-list , [opt] }
19235 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19236 the elements of the initializer-list (or NULL, if the last
19237 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19238 NULL_TREE. There is no way to detect whether or not the optional
19239 trailing `,' was provided. NON_CONSTANT_P is as for
19240 cp_parser_initializer. */
19242 static tree
19243 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19245 tree initializer;
19247 /* Consume the `{' token. */
19248 cp_lexer_consume_token (parser->lexer);
19249 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19250 initializer = make_node (CONSTRUCTOR);
19251 /* If it's not a `}', then there is a non-trivial initializer. */
19252 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19254 /* Parse the initializer list. */
19255 CONSTRUCTOR_ELTS (initializer)
19256 = cp_parser_initializer_list (parser, non_constant_p);
19257 /* A trailing `,' token is allowed. */
19258 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19259 cp_lexer_consume_token (parser->lexer);
19261 else
19262 *non_constant_p = false;
19263 /* Now, there should be a trailing `}'. */
19264 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19265 TREE_TYPE (initializer) = init_list_type_node;
19266 return initializer;
19269 /* Consume tokens up to, and including, the next non-nested closing `]'.
19270 Returns true iff we found a closing `]'. */
19272 static bool
19273 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19275 unsigned square_depth = 0;
19277 while (true)
19279 cp_token * token = cp_lexer_peek_token (parser->lexer);
19281 switch (token->type)
19283 case CPP_EOF:
19284 case CPP_PRAGMA_EOL:
19285 /* If we've run out of tokens, then there is no closing `]'. */
19286 return false;
19288 case CPP_OPEN_SQUARE:
19289 ++square_depth;
19290 break;
19292 case CPP_CLOSE_SQUARE:
19293 if (!square_depth--)
19295 cp_lexer_consume_token (parser->lexer);
19296 return true;
19298 break;
19300 default:
19301 break;
19304 /* Consume the token. */
19305 cp_lexer_consume_token (parser->lexer);
19309 /* Return true if we are looking at an array-designator, false otherwise. */
19311 static bool
19312 cp_parser_array_designator_p (cp_parser *parser)
19314 /* Consume the `['. */
19315 cp_lexer_consume_token (parser->lexer);
19317 cp_lexer_save_tokens (parser->lexer);
19319 /* Skip tokens until the next token is a closing square bracket.
19320 If we find the closing `]', and the next token is a `=', then
19321 we are looking at an array designator. */
19322 bool array_designator_p
19323 = (cp_parser_skip_to_closing_square_bracket (parser)
19324 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19326 /* Roll back the tokens we skipped. */
19327 cp_lexer_rollback_tokens (parser->lexer);
19329 return array_designator_p;
19332 /* Parse an initializer-list.
19334 initializer-list:
19335 initializer-clause ... [opt]
19336 initializer-list , initializer-clause ... [opt]
19338 GNU Extension:
19340 initializer-list:
19341 designation initializer-clause ...[opt]
19342 initializer-list , designation initializer-clause ...[opt]
19344 designation:
19345 . identifier =
19346 identifier :
19347 [ constant-expression ] =
19349 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19350 for the initializer. If the INDEX of the elt is non-NULL, it is the
19351 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19352 as for cp_parser_initializer. */
19354 static vec<constructor_elt, va_gc> *
19355 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19357 vec<constructor_elt, va_gc> *v = NULL;
19359 /* Assume all of the expressions are constant. */
19360 *non_constant_p = false;
19362 /* Parse the rest of the list. */
19363 while (true)
19365 cp_token *token;
19366 tree designator;
19367 tree initializer;
19368 bool clause_non_constant_p;
19370 /* If the next token is an identifier and the following one is a
19371 colon, we are looking at the GNU designated-initializer
19372 syntax. */
19373 if (cp_parser_allow_gnu_extensions_p (parser)
19374 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19375 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19377 /* Warn the user that they are using an extension. */
19378 pedwarn (input_location, OPT_Wpedantic,
19379 "ISO C++ does not allow designated initializers");
19380 /* Consume the identifier. */
19381 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19382 /* Consume the `:'. */
19383 cp_lexer_consume_token (parser->lexer);
19385 /* Also handle the C99 syntax, '. id ='. */
19386 else if (cp_parser_allow_gnu_extensions_p (parser)
19387 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19388 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19389 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19391 /* Warn the user that they are using an extension. */
19392 pedwarn (input_location, OPT_Wpedantic,
19393 "ISO C++ does not allow C99 designated initializers");
19394 /* Consume the `.'. */
19395 cp_lexer_consume_token (parser->lexer);
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 C99 array designators, '[ const ] ='. */
19402 else if (cp_parser_allow_gnu_extensions_p (parser)
19403 && !c_dialect_objc ()
19404 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19406 /* In C++11, [ could start a lambda-introducer. */
19407 bool non_const = false;
19409 cp_parser_parse_tentatively (parser);
19411 if (!cp_parser_array_designator_p (parser))
19413 cp_parser_simulate_error (parser);
19414 designator = NULL_TREE;
19416 else
19418 designator = cp_parser_constant_expression (parser, true,
19419 &non_const);
19420 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19421 cp_parser_require (parser, CPP_EQ, RT_EQ);
19424 if (!cp_parser_parse_definitely (parser))
19425 designator = NULL_TREE;
19426 else if (non_const)
19427 require_potential_rvalue_constant_expression (designator);
19429 else
19430 designator = NULL_TREE;
19432 /* Parse the initializer. */
19433 initializer = cp_parser_initializer_clause (parser,
19434 &clause_non_constant_p);
19435 /* If any clause is non-constant, so is the entire initializer. */
19436 if (clause_non_constant_p)
19437 *non_constant_p = true;
19439 /* If we have an ellipsis, this is an initializer pack
19440 expansion. */
19441 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19443 /* Consume the `...'. */
19444 cp_lexer_consume_token (parser->lexer);
19446 /* Turn the initializer into an initializer expansion. */
19447 initializer = make_pack_expansion (initializer);
19450 /* Add it to the vector. */
19451 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19453 /* If the next token is not a comma, we have reached the end of
19454 the list. */
19455 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19456 break;
19458 /* Peek at the next token. */
19459 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19460 /* If the next token is a `}', then we're still done. An
19461 initializer-clause can have a trailing `,' after the
19462 initializer-list and before the closing `}'. */
19463 if (token->type == CPP_CLOSE_BRACE)
19464 break;
19466 /* Consume the `,' token. */
19467 cp_lexer_consume_token (parser->lexer);
19470 return v;
19473 /* Classes [gram.class] */
19475 /* Parse a class-name.
19477 class-name:
19478 identifier
19479 template-id
19481 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19482 to indicate that names looked up in dependent types should be
19483 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19484 keyword has been used to indicate that the name that appears next
19485 is a template. TAG_TYPE indicates the explicit tag given before
19486 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19487 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19488 is the class being defined in a class-head.
19490 Returns the TYPE_DECL representing the class. */
19492 static tree
19493 cp_parser_class_name (cp_parser *parser,
19494 bool typename_keyword_p,
19495 bool template_keyword_p,
19496 enum tag_types tag_type,
19497 bool check_dependency_p,
19498 bool class_head_p,
19499 bool is_declaration)
19501 tree decl;
19502 tree scope;
19503 bool typename_p;
19504 cp_token *token;
19505 tree identifier = NULL_TREE;
19507 /* All class-names start with an identifier. */
19508 token = cp_lexer_peek_token (parser->lexer);
19509 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19511 cp_parser_error (parser, "expected class-name");
19512 return error_mark_node;
19515 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19516 to a template-id, so we save it here. */
19517 scope = parser->scope;
19518 if (scope == error_mark_node)
19519 return error_mark_node;
19521 /* Any name names a type if we're following the `typename' keyword
19522 in a qualified name where the enclosing scope is type-dependent. */
19523 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19524 && dependent_type_p (scope));
19525 /* Handle the common case (an identifier, but not a template-id)
19526 efficiently. */
19527 if (token->type == CPP_NAME
19528 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19530 cp_token *identifier_token;
19531 bool ambiguous_p;
19533 /* Look for the identifier. */
19534 identifier_token = cp_lexer_peek_token (parser->lexer);
19535 ambiguous_p = identifier_token->error_reported;
19536 identifier = cp_parser_identifier (parser);
19537 /* If the next token isn't an identifier, we are certainly not
19538 looking at a class-name. */
19539 if (identifier == error_mark_node)
19540 decl = error_mark_node;
19541 /* If we know this is a type-name, there's no need to look it
19542 up. */
19543 else if (typename_p)
19544 decl = identifier;
19545 else
19547 tree ambiguous_decls;
19548 /* If we already know that this lookup is ambiguous, then
19549 we've already issued an error message; there's no reason
19550 to check again. */
19551 if (ambiguous_p)
19553 cp_parser_simulate_error (parser);
19554 return error_mark_node;
19556 /* If the next token is a `::', then the name must be a type
19557 name.
19559 [basic.lookup.qual]
19561 During the lookup for a name preceding the :: scope
19562 resolution operator, object, function, and enumerator
19563 names are ignored. */
19564 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19565 tag_type = typename_type;
19566 /* Look up the name. */
19567 decl = cp_parser_lookup_name (parser, identifier,
19568 tag_type,
19569 /*is_template=*/false,
19570 /*is_namespace=*/false,
19571 check_dependency_p,
19572 &ambiguous_decls,
19573 identifier_token->location);
19574 if (ambiguous_decls)
19576 if (cp_parser_parsing_tentatively (parser))
19577 cp_parser_simulate_error (parser);
19578 return error_mark_node;
19582 else
19584 /* Try a template-id. */
19585 decl = cp_parser_template_id (parser, template_keyword_p,
19586 check_dependency_p,
19587 tag_type,
19588 is_declaration);
19589 if (decl == error_mark_node)
19590 return error_mark_node;
19593 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19595 /* If this is a typename, create a TYPENAME_TYPE. */
19596 if (typename_p && decl != error_mark_node)
19598 decl = make_typename_type (scope, decl, typename_type,
19599 /*complain=*/tf_error);
19600 if (decl != error_mark_node)
19601 decl = TYPE_NAME (decl);
19604 decl = strip_using_decl (decl);
19606 /* Check to see that it is really the name of a class. */
19607 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19608 && identifier_p (TREE_OPERAND (decl, 0))
19609 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19610 /* Situations like this:
19612 template <typename T> struct A {
19613 typename T::template X<int>::I i;
19616 are problematic. Is `T::template X<int>' a class-name? The
19617 standard does not seem to be definitive, but there is no other
19618 valid interpretation of the following `::'. Therefore, those
19619 names are considered class-names. */
19621 decl = make_typename_type (scope, decl, tag_type, tf_error);
19622 if (decl != error_mark_node)
19623 decl = TYPE_NAME (decl);
19625 else if (TREE_CODE (decl) != TYPE_DECL
19626 || TREE_TYPE (decl) == error_mark_node
19627 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19628 /* In Objective-C 2.0, a classname followed by '.' starts a
19629 dot-syntax expression, and it's not a type-name. */
19630 || (c_dialect_objc ()
19631 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19632 && objc_is_class_name (decl)))
19633 decl = error_mark_node;
19635 if (decl == error_mark_node)
19636 cp_parser_error (parser, "expected class-name");
19637 else if (identifier && !parser->scope)
19638 maybe_note_name_used_in_class (identifier, decl);
19640 return decl;
19643 /* Parse a class-specifier.
19645 class-specifier:
19646 class-head { member-specification [opt] }
19648 Returns the TREE_TYPE representing the class. */
19650 static tree
19651 cp_parser_class_specifier_1 (cp_parser* parser)
19653 tree type;
19654 tree attributes = NULL_TREE;
19655 bool nested_name_specifier_p;
19656 unsigned saved_num_template_parameter_lists;
19657 bool saved_in_function_body;
19658 unsigned char in_statement;
19659 bool in_switch_statement_p;
19660 bool saved_in_unbraced_linkage_specification_p;
19661 tree old_scope = NULL_TREE;
19662 tree scope = NULL_TREE;
19663 cp_token *closing_brace;
19665 push_deferring_access_checks (dk_no_deferred);
19667 /* Parse the class-head. */
19668 type = cp_parser_class_head (parser,
19669 &nested_name_specifier_p);
19670 /* If the class-head was a semantic disaster, skip the entire body
19671 of the class. */
19672 if (!type)
19674 cp_parser_skip_to_end_of_block_or_statement (parser);
19675 pop_deferring_access_checks ();
19676 return error_mark_node;
19679 /* Look for the `{'. */
19680 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19682 pop_deferring_access_checks ();
19683 return error_mark_node;
19686 cp_ensure_no_omp_declare_simd (parser);
19688 /* Issue an error message if type-definitions are forbidden here. */
19689 cp_parser_check_type_definition (parser);
19690 /* Remember that we are defining one more class. */
19691 ++parser->num_classes_being_defined;
19692 /* Inside the class, surrounding template-parameter-lists do not
19693 apply. */
19694 saved_num_template_parameter_lists
19695 = parser->num_template_parameter_lists;
19696 parser->num_template_parameter_lists = 0;
19697 /* We are not in a function body. */
19698 saved_in_function_body = parser->in_function_body;
19699 parser->in_function_body = false;
19700 /* Or in a loop. */
19701 in_statement = parser->in_statement;
19702 parser->in_statement = 0;
19703 /* Or in a switch. */
19704 in_switch_statement_p = parser->in_switch_statement_p;
19705 parser->in_switch_statement_p = false;
19706 /* We are not immediately inside an extern "lang" block. */
19707 saved_in_unbraced_linkage_specification_p
19708 = parser->in_unbraced_linkage_specification_p;
19709 parser->in_unbraced_linkage_specification_p = false;
19711 /* Start the class. */
19712 if (nested_name_specifier_p)
19714 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19715 old_scope = push_inner_scope (scope);
19717 type = begin_class_definition (type);
19719 if (type == error_mark_node)
19720 /* If the type is erroneous, skip the entire body of the class. */
19721 cp_parser_skip_to_closing_brace (parser);
19722 else
19723 /* Parse the member-specification. */
19724 cp_parser_member_specification_opt (parser);
19726 /* Look for the trailing `}'. */
19727 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19728 /* Look for trailing attributes to apply to this class. */
19729 if (cp_parser_allow_gnu_extensions_p (parser))
19730 attributes = cp_parser_gnu_attributes_opt (parser);
19731 if (type != error_mark_node)
19732 type = finish_struct (type, attributes);
19733 if (nested_name_specifier_p)
19734 pop_inner_scope (old_scope, scope);
19736 /* We've finished a type definition. Check for the common syntax
19737 error of forgetting a semicolon after the definition. We need to
19738 be careful, as we can't just check for not-a-semicolon and be done
19739 with it; the user might have typed:
19741 class X { } c = ...;
19742 class X { } *p = ...;
19744 and so forth. Instead, enumerate all the possible tokens that
19745 might follow this production; if we don't see one of them, then
19746 complain and silently insert the semicolon. */
19748 cp_token *token = cp_lexer_peek_token (parser->lexer);
19749 bool want_semicolon = true;
19751 if (cp_next_tokens_can_be_std_attribute_p (parser))
19752 /* Don't try to parse c++11 attributes here. As per the
19753 grammar, that should be a task for
19754 cp_parser_decl_specifier_seq. */
19755 want_semicolon = false;
19757 switch (token->type)
19759 case CPP_NAME:
19760 case CPP_SEMICOLON:
19761 case CPP_MULT:
19762 case CPP_AND:
19763 case CPP_OPEN_PAREN:
19764 case CPP_CLOSE_PAREN:
19765 case CPP_COMMA:
19766 want_semicolon = false;
19767 break;
19769 /* While it's legal for type qualifiers and storage class
19770 specifiers to follow type definitions in the grammar, only
19771 compiler testsuites contain code like that. Assume that if
19772 we see such code, then what we're really seeing is a case
19773 like:
19775 class X { }
19776 const <type> var = ...;
19780 class Y { }
19781 static <type> func (...) ...
19783 i.e. the qualifier or specifier applies to the next
19784 declaration. To do so, however, we need to look ahead one
19785 more token to see if *that* token is a type specifier.
19787 This code could be improved to handle:
19789 class Z { }
19790 static const <type> var = ...; */
19791 case CPP_KEYWORD:
19792 if (keyword_is_decl_specifier (token->keyword))
19794 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19796 /* Handling user-defined types here would be nice, but very
19797 tricky. */
19798 want_semicolon
19799 = (lookahead->type == CPP_KEYWORD
19800 && keyword_begins_type_specifier (lookahead->keyword));
19802 break;
19803 default:
19804 break;
19807 /* If we don't have a type, then something is very wrong and we
19808 shouldn't try to do anything clever. Likewise for not seeing the
19809 closing brace. */
19810 if (closing_brace && TYPE_P (type) && want_semicolon)
19812 cp_token_position prev
19813 = cp_lexer_previous_token_position (parser->lexer);
19814 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19815 location_t loc = prev_token->location;
19817 if (CLASSTYPE_DECLARED_CLASS (type))
19818 error_at (loc, "expected %<;%> after class definition");
19819 else if (TREE_CODE (type) == RECORD_TYPE)
19820 error_at (loc, "expected %<;%> after struct definition");
19821 else if (TREE_CODE (type) == UNION_TYPE)
19822 error_at (loc, "expected %<;%> after union definition");
19823 else
19824 gcc_unreachable ();
19826 /* Unget one token and smash it to look as though we encountered
19827 a semicolon in the input stream. */
19828 cp_lexer_set_token_position (parser->lexer, prev);
19829 token = cp_lexer_peek_token (parser->lexer);
19830 token->type = CPP_SEMICOLON;
19831 token->keyword = RID_MAX;
19835 /* If this class is not itself within the scope of another class,
19836 then we need to parse the bodies of all of the queued function
19837 definitions. Note that the queued functions defined in a class
19838 are not always processed immediately following the
19839 class-specifier for that class. Consider:
19841 struct A {
19842 struct B { void f() { sizeof (A); } };
19845 If `f' were processed before the processing of `A' were
19846 completed, there would be no way to compute the size of `A'.
19847 Note that the nesting we are interested in here is lexical --
19848 not the semantic nesting given by TYPE_CONTEXT. In particular,
19849 for:
19851 struct A { struct B; };
19852 struct A::B { void f() { } };
19854 there is no need to delay the parsing of `A::B::f'. */
19855 if (--parser->num_classes_being_defined == 0)
19857 tree decl;
19858 tree class_type = NULL_TREE;
19859 tree pushed_scope = NULL_TREE;
19860 unsigned ix;
19861 cp_default_arg_entry *e;
19862 tree save_ccp, save_ccr;
19864 /* In a first pass, parse default arguments to the functions.
19865 Then, in a second pass, parse the bodies of the functions.
19866 This two-phased approach handles cases like:
19868 struct S {
19869 void f() { g(); }
19870 void g(int i = 3);
19874 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19876 decl = e->decl;
19877 /* If there are default arguments that have not yet been processed,
19878 take care of them now. */
19879 if (class_type != e->class_type)
19881 if (pushed_scope)
19882 pop_scope (pushed_scope);
19883 class_type = e->class_type;
19884 pushed_scope = push_scope (class_type);
19886 /* Make sure that any template parameters are in scope. */
19887 maybe_begin_member_template_processing (decl);
19888 /* Parse the default argument expressions. */
19889 cp_parser_late_parsing_default_args (parser, decl);
19890 /* Remove any template parameters from the symbol table. */
19891 maybe_end_member_template_processing ();
19893 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19894 /* Now parse any NSDMIs. */
19895 save_ccp = current_class_ptr;
19896 save_ccr = current_class_ref;
19897 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19899 if (class_type != DECL_CONTEXT (decl))
19901 if (pushed_scope)
19902 pop_scope (pushed_scope);
19903 class_type = DECL_CONTEXT (decl);
19904 pushed_scope = push_scope (class_type);
19906 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19907 cp_parser_late_parsing_nsdmi (parser, decl);
19909 vec_safe_truncate (unparsed_nsdmis, 0);
19910 current_class_ptr = save_ccp;
19911 current_class_ref = save_ccr;
19912 if (pushed_scope)
19913 pop_scope (pushed_scope);
19915 /* Now do some post-NSDMI bookkeeping. */
19916 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19917 after_nsdmi_defaulted_late_checks (class_type);
19918 vec_safe_truncate (unparsed_classes, 0);
19919 after_nsdmi_defaulted_late_checks (type);
19921 /* Now parse the body of the functions. */
19922 if (flag_openmp)
19924 /* OpenMP UDRs need to be parsed before all other functions. */
19925 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19926 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19927 cp_parser_late_parsing_for_member (parser, decl);
19928 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19929 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19930 cp_parser_late_parsing_for_member (parser, decl);
19932 else
19933 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19934 cp_parser_late_parsing_for_member (parser, decl);
19935 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19937 else
19938 vec_safe_push (unparsed_classes, type);
19940 /* Put back any saved access checks. */
19941 pop_deferring_access_checks ();
19943 /* Restore saved state. */
19944 parser->in_switch_statement_p = in_switch_statement_p;
19945 parser->in_statement = in_statement;
19946 parser->in_function_body = saved_in_function_body;
19947 parser->num_template_parameter_lists
19948 = saved_num_template_parameter_lists;
19949 parser->in_unbraced_linkage_specification_p
19950 = saved_in_unbraced_linkage_specification_p;
19952 return type;
19955 static tree
19956 cp_parser_class_specifier (cp_parser* parser)
19958 tree ret;
19959 timevar_push (TV_PARSE_STRUCT);
19960 ret = cp_parser_class_specifier_1 (parser);
19961 timevar_pop (TV_PARSE_STRUCT);
19962 return ret;
19965 /* Parse a class-head.
19967 class-head:
19968 class-key identifier [opt] base-clause [opt]
19969 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19970 class-key nested-name-specifier [opt] template-id
19971 base-clause [opt]
19973 class-virt-specifier:
19974 final
19976 GNU Extensions:
19977 class-key attributes identifier [opt] base-clause [opt]
19978 class-key attributes nested-name-specifier identifier base-clause [opt]
19979 class-key attributes nested-name-specifier [opt] template-id
19980 base-clause [opt]
19982 Upon return BASES is initialized to the list of base classes (or
19983 NULL, if there are none) in the same form returned by
19984 cp_parser_base_clause.
19986 Returns the TYPE of the indicated class. Sets
19987 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19988 involving a nested-name-specifier was used, and FALSE otherwise.
19990 Returns error_mark_node if this is not a class-head.
19992 Returns NULL_TREE if the class-head is syntactically valid, but
19993 semantically invalid in a way that means we should skip the entire
19994 body of the class. */
19996 static tree
19997 cp_parser_class_head (cp_parser* parser,
19998 bool* nested_name_specifier_p)
20000 tree nested_name_specifier;
20001 enum tag_types class_key;
20002 tree id = NULL_TREE;
20003 tree type = NULL_TREE;
20004 tree attributes;
20005 tree bases;
20006 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20007 bool template_id_p = false;
20008 bool qualified_p = false;
20009 bool invalid_nested_name_p = false;
20010 bool invalid_explicit_specialization_p = false;
20011 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20012 tree pushed_scope = NULL_TREE;
20013 unsigned num_templates;
20014 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20015 /* Assume no nested-name-specifier will be present. */
20016 *nested_name_specifier_p = false;
20017 /* Assume no template parameter lists will be used in defining the
20018 type. */
20019 num_templates = 0;
20020 parser->colon_corrects_to_scope_p = false;
20022 /* Look for the class-key. */
20023 class_key = cp_parser_class_key (parser);
20024 if (class_key == none_type)
20025 return error_mark_node;
20027 /* Parse the attributes. */
20028 attributes = cp_parser_attributes_opt (parser);
20030 /* If the next token is `::', that is invalid -- but sometimes
20031 people do try to write:
20033 struct ::S {};
20035 Handle this gracefully by accepting the extra qualifier, and then
20036 issuing an error about it later if this really is a
20037 class-head. If it turns out just to be an elaborated type
20038 specifier, remain silent. */
20039 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20040 qualified_p = true;
20042 push_deferring_access_checks (dk_no_check);
20044 /* Determine the name of the class. Begin by looking for an
20045 optional nested-name-specifier. */
20046 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20047 nested_name_specifier
20048 = cp_parser_nested_name_specifier_opt (parser,
20049 /*typename_keyword_p=*/false,
20050 /*check_dependency_p=*/false,
20051 /*type_p=*/true,
20052 /*is_declaration=*/false);
20053 /* If there was a nested-name-specifier, then there *must* be an
20054 identifier. */
20055 if (nested_name_specifier)
20057 type_start_token = cp_lexer_peek_token (parser->lexer);
20058 /* Although the grammar says `identifier', it really means
20059 `class-name' or `template-name'. You are only allowed to
20060 define a class that has already been declared with this
20061 syntax.
20063 The proposed resolution for Core Issue 180 says that wherever
20064 you see `class T::X' you should treat `X' as a type-name.
20066 It is OK to define an inaccessible class; for example:
20068 class A { class B; };
20069 class A::B {};
20071 We do not know if we will see a class-name, or a
20072 template-name. We look for a class-name first, in case the
20073 class-name is a template-id; if we looked for the
20074 template-name first we would stop after the template-name. */
20075 cp_parser_parse_tentatively (parser);
20076 type = cp_parser_class_name (parser,
20077 /*typename_keyword_p=*/false,
20078 /*template_keyword_p=*/false,
20079 class_type,
20080 /*check_dependency_p=*/false,
20081 /*class_head_p=*/true,
20082 /*is_declaration=*/false);
20083 /* If that didn't work, ignore the nested-name-specifier. */
20084 if (!cp_parser_parse_definitely (parser))
20086 invalid_nested_name_p = true;
20087 type_start_token = cp_lexer_peek_token (parser->lexer);
20088 id = cp_parser_identifier (parser);
20089 if (id == error_mark_node)
20090 id = NULL_TREE;
20092 /* If we could not find a corresponding TYPE, treat this
20093 declaration like an unqualified declaration. */
20094 if (type == error_mark_node)
20095 nested_name_specifier = NULL_TREE;
20096 /* Otherwise, count the number of templates used in TYPE and its
20097 containing scopes. */
20098 else
20100 tree scope;
20102 for (scope = TREE_TYPE (type);
20103 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20104 scope = get_containing_scope (scope))
20105 if (TYPE_P (scope)
20106 && CLASS_TYPE_P (scope)
20107 && CLASSTYPE_TEMPLATE_INFO (scope)
20108 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20109 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20110 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20111 ++num_templates;
20114 /* Otherwise, the identifier is optional. */
20115 else
20117 /* We don't know whether what comes next is a template-id,
20118 an identifier, or nothing at all. */
20119 cp_parser_parse_tentatively (parser);
20120 /* Check for a template-id. */
20121 type_start_token = cp_lexer_peek_token (parser->lexer);
20122 id = cp_parser_template_id (parser,
20123 /*template_keyword_p=*/false,
20124 /*check_dependency_p=*/true,
20125 class_key,
20126 /*is_declaration=*/true);
20127 /* If that didn't work, it could still be an identifier. */
20128 if (!cp_parser_parse_definitely (parser))
20130 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20132 type_start_token = cp_lexer_peek_token (parser->lexer);
20133 id = cp_parser_identifier (parser);
20135 else
20136 id = NULL_TREE;
20138 else
20140 template_id_p = true;
20141 ++num_templates;
20145 pop_deferring_access_checks ();
20147 if (id)
20149 cp_parser_check_for_invalid_template_id (parser, id,
20150 class_key,
20151 type_start_token->location);
20153 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20155 /* If it's not a `:' or a `{' then we can't really be looking at a
20156 class-head, since a class-head only appears as part of a
20157 class-specifier. We have to detect this situation before calling
20158 xref_tag, since that has irreversible side-effects. */
20159 if (!cp_parser_next_token_starts_class_definition_p (parser))
20161 cp_parser_error (parser, "expected %<{%> or %<:%>");
20162 type = error_mark_node;
20163 goto out;
20166 /* At this point, we're going ahead with the class-specifier, even
20167 if some other problem occurs. */
20168 cp_parser_commit_to_tentative_parse (parser);
20169 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20171 cp_parser_error (parser,
20172 "cannot specify %<override%> for a class");
20173 type = error_mark_node;
20174 goto out;
20176 /* Issue the error about the overly-qualified name now. */
20177 if (qualified_p)
20179 cp_parser_error (parser,
20180 "global qualification of class name is invalid");
20181 type = error_mark_node;
20182 goto out;
20184 else if (invalid_nested_name_p)
20186 cp_parser_error (parser,
20187 "qualified name does not name a class");
20188 type = error_mark_node;
20189 goto out;
20191 else if (nested_name_specifier)
20193 tree scope;
20195 /* Reject typedef-names in class heads. */
20196 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20198 error_at (type_start_token->location,
20199 "invalid class name in declaration of %qD",
20200 type);
20201 type = NULL_TREE;
20202 goto done;
20205 /* Figure out in what scope the declaration is being placed. */
20206 scope = current_scope ();
20207 /* If that scope does not contain the scope in which the
20208 class was originally declared, the program is invalid. */
20209 if (scope && !is_ancestor (scope, nested_name_specifier))
20211 if (at_namespace_scope_p ())
20212 error_at (type_start_token->location,
20213 "declaration of %qD in namespace %qD which does not "
20214 "enclose %qD",
20215 type, scope, nested_name_specifier);
20216 else
20217 error_at (type_start_token->location,
20218 "declaration of %qD in %qD which does not enclose %qD",
20219 type, scope, nested_name_specifier);
20220 type = NULL_TREE;
20221 goto done;
20223 /* [dcl.meaning]
20225 A declarator-id shall not be qualified except for the
20226 definition of a ... nested class outside of its class
20227 ... [or] the definition or explicit instantiation of a
20228 class member of a namespace outside of its namespace. */
20229 if (scope == nested_name_specifier)
20231 permerror (nested_name_specifier_token_start->location,
20232 "extra qualification not allowed");
20233 nested_name_specifier = NULL_TREE;
20234 num_templates = 0;
20237 /* An explicit-specialization must be preceded by "template <>". If
20238 it is not, try to recover gracefully. */
20239 if (at_namespace_scope_p ()
20240 && parser->num_template_parameter_lists == 0
20241 && template_id_p)
20243 error_at (type_start_token->location,
20244 "an explicit specialization must be preceded by %<template <>%>");
20245 invalid_explicit_specialization_p = true;
20246 /* Take the same action that would have been taken by
20247 cp_parser_explicit_specialization. */
20248 ++parser->num_template_parameter_lists;
20249 begin_specialization ();
20251 /* There must be no "return" statements between this point and the
20252 end of this function; set "type "to the correct return value and
20253 use "goto done;" to return. */
20254 /* Make sure that the right number of template parameters were
20255 present. */
20256 if (!cp_parser_check_template_parameters (parser, num_templates,
20257 type_start_token->location,
20258 /*declarator=*/NULL))
20260 /* If something went wrong, there is no point in even trying to
20261 process the class-definition. */
20262 type = NULL_TREE;
20263 goto done;
20266 /* Look up the type. */
20267 if (template_id_p)
20269 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20270 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20271 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20273 error_at (type_start_token->location,
20274 "function template %qD redeclared as a class template", id);
20275 type = error_mark_node;
20277 else
20279 type = TREE_TYPE (id);
20280 type = maybe_process_partial_specialization (type);
20282 if (nested_name_specifier)
20283 pushed_scope = push_scope (nested_name_specifier);
20285 else if (nested_name_specifier)
20287 tree class_type;
20289 /* Given:
20291 template <typename T> struct S { struct T };
20292 template <typename T> struct S<T>::T { };
20294 we will get a TYPENAME_TYPE when processing the definition of
20295 `S::T'. We need to resolve it to the actual type before we
20296 try to define it. */
20297 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20299 class_type = resolve_typename_type (TREE_TYPE (type),
20300 /*only_current_p=*/false);
20301 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20302 type = TYPE_NAME (class_type);
20303 else
20305 cp_parser_error (parser, "could not resolve typename type");
20306 type = error_mark_node;
20310 if (maybe_process_partial_specialization (TREE_TYPE (type))
20311 == error_mark_node)
20313 type = NULL_TREE;
20314 goto done;
20317 class_type = current_class_type;
20318 /* Enter the scope indicated by the nested-name-specifier. */
20319 pushed_scope = push_scope (nested_name_specifier);
20320 /* Get the canonical version of this type. */
20321 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20322 /* Call push_template_decl if it seems like we should be defining a
20323 template either from the template headers or the type we're
20324 defining, so that we diagnose both extra and missing headers. */
20325 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20326 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20327 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20329 type = push_template_decl (type);
20330 if (type == error_mark_node)
20332 type = NULL_TREE;
20333 goto done;
20337 type = TREE_TYPE (type);
20338 *nested_name_specifier_p = true;
20340 else /* The name is not a nested name. */
20342 /* If the class was unnamed, create a dummy name. */
20343 if (!id)
20344 id = make_anon_name ();
20345 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20346 parser->num_template_parameter_lists);
20349 /* Indicate whether this class was declared as a `class' or as a
20350 `struct'. */
20351 if (TREE_CODE (type) == RECORD_TYPE)
20352 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20353 cp_parser_check_class_key (class_key, type);
20355 /* If this type was already complete, and we see another definition,
20356 that's an error. */
20357 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20359 error_at (type_start_token->location, "redefinition of %q#T",
20360 type);
20361 error_at (type_start_token->location, "previous definition of %q+#T",
20362 type);
20363 type = NULL_TREE;
20364 goto done;
20366 else if (type == error_mark_node)
20367 type = NULL_TREE;
20369 if (type)
20371 /* Apply attributes now, before any use of the class as a template
20372 argument in its base list. */
20373 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20374 fixup_attribute_variants (type);
20377 /* We will have entered the scope containing the class; the names of
20378 base classes should be looked up in that context. For example:
20380 struct A { struct B {}; struct C; };
20381 struct A::C : B {};
20383 is valid. */
20385 /* Get the list of base-classes, if there is one. */
20386 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20388 /* PR59482: enter the class scope so that base-specifiers are looked
20389 up correctly. */
20390 if (type)
20391 pushclass (type);
20392 bases = cp_parser_base_clause (parser);
20393 /* PR59482: get out of the previously pushed class scope so that the
20394 subsequent pops pop the right thing. */
20395 if (type)
20396 popclass ();
20398 else
20399 bases = NULL_TREE;
20401 /* If we're really defining a class, process the base classes.
20402 If they're invalid, fail. */
20403 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20404 && !xref_basetypes (type, bases))
20405 type = NULL_TREE;
20407 done:
20408 /* Leave the scope given by the nested-name-specifier. We will
20409 enter the class scope itself while processing the members. */
20410 if (pushed_scope)
20411 pop_scope (pushed_scope);
20413 if (invalid_explicit_specialization_p)
20415 end_specialization ();
20416 --parser->num_template_parameter_lists;
20419 if (type)
20420 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20421 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20422 CLASSTYPE_FINAL (type) = 1;
20423 out:
20424 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20425 return type;
20428 /* Parse a class-key.
20430 class-key:
20431 class
20432 struct
20433 union
20435 Returns the kind of class-key specified, or none_type to indicate
20436 error. */
20438 static enum tag_types
20439 cp_parser_class_key (cp_parser* parser)
20441 cp_token *token;
20442 enum tag_types tag_type;
20444 /* Look for the class-key. */
20445 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20446 if (!token)
20447 return none_type;
20449 /* Check to see if the TOKEN is a class-key. */
20450 tag_type = cp_parser_token_is_class_key (token);
20451 if (!tag_type)
20452 cp_parser_error (parser, "expected class-key");
20453 return tag_type;
20456 /* Parse a type-parameter-key.
20458 type-parameter-key:
20459 class
20460 typename
20463 static void
20464 cp_parser_type_parameter_key (cp_parser* parser)
20466 /* Look for the type-parameter-key. */
20467 enum tag_types tag_type = none_type;
20468 cp_token *token = cp_lexer_peek_token (parser->lexer);
20469 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20471 cp_lexer_consume_token (parser->lexer);
20472 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20473 /* typename is not allowed in a template template parameter
20474 by the standard until C++1Z. */
20475 pedwarn (token->location, OPT_Wpedantic,
20476 "ISO C++ forbids typename key in template template parameter;"
20477 " use -std=c++1z or -std=gnu++1z");
20479 else
20480 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20482 return;
20485 /* Parse an (optional) member-specification.
20487 member-specification:
20488 member-declaration member-specification [opt]
20489 access-specifier : member-specification [opt] */
20491 static void
20492 cp_parser_member_specification_opt (cp_parser* parser)
20494 while (true)
20496 cp_token *token;
20497 enum rid keyword;
20499 /* Peek at the next token. */
20500 token = cp_lexer_peek_token (parser->lexer);
20501 /* If it's a `}', or EOF then we've seen all the members. */
20502 if (token->type == CPP_CLOSE_BRACE
20503 || token->type == CPP_EOF
20504 || token->type == CPP_PRAGMA_EOL)
20505 break;
20507 /* See if this token is a keyword. */
20508 keyword = token->keyword;
20509 switch (keyword)
20511 case RID_PUBLIC:
20512 case RID_PROTECTED:
20513 case RID_PRIVATE:
20514 /* Consume the access-specifier. */
20515 cp_lexer_consume_token (parser->lexer);
20516 /* Remember which access-specifier is active. */
20517 current_access_specifier = token->u.value;
20518 /* Look for the `:'. */
20519 cp_parser_require (parser, CPP_COLON, RT_COLON);
20520 break;
20522 default:
20523 /* Accept #pragmas at class scope. */
20524 if (token->type == CPP_PRAGMA)
20526 cp_parser_pragma (parser, pragma_member);
20527 break;
20530 /* Otherwise, the next construction must be a
20531 member-declaration. */
20532 cp_parser_member_declaration (parser);
20537 /* Parse a member-declaration.
20539 member-declaration:
20540 decl-specifier-seq [opt] member-declarator-list [opt] ;
20541 function-definition ; [opt]
20542 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20543 using-declaration
20544 template-declaration
20545 alias-declaration
20547 member-declarator-list:
20548 member-declarator
20549 member-declarator-list , member-declarator
20551 member-declarator:
20552 declarator pure-specifier [opt]
20553 declarator constant-initializer [opt]
20554 identifier [opt] : constant-expression
20556 GNU Extensions:
20558 member-declaration:
20559 __extension__ member-declaration
20561 member-declarator:
20562 declarator attributes [opt] pure-specifier [opt]
20563 declarator attributes [opt] constant-initializer [opt]
20564 identifier [opt] attributes [opt] : constant-expression
20566 C++0x Extensions:
20568 member-declaration:
20569 static_assert-declaration */
20571 static void
20572 cp_parser_member_declaration (cp_parser* parser)
20574 cp_decl_specifier_seq decl_specifiers;
20575 tree prefix_attributes;
20576 tree decl;
20577 int declares_class_or_enum;
20578 bool friend_p;
20579 cp_token *token = NULL;
20580 cp_token *decl_spec_token_start = NULL;
20581 cp_token *initializer_token_start = NULL;
20582 int saved_pedantic;
20583 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20585 /* Check for the `__extension__' keyword. */
20586 if (cp_parser_extension_opt (parser, &saved_pedantic))
20588 /* Recurse. */
20589 cp_parser_member_declaration (parser);
20590 /* Restore the old value of the PEDANTIC flag. */
20591 pedantic = saved_pedantic;
20593 return;
20596 /* Check for a template-declaration. */
20597 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20599 /* An explicit specialization here is an error condition, and we
20600 expect the specialization handler to detect and report this. */
20601 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20602 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20603 cp_parser_explicit_specialization (parser);
20604 else
20605 cp_parser_template_declaration (parser, /*member_p=*/true);
20607 return;
20610 /* Check for a using-declaration. */
20611 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20613 if (cxx_dialect < cxx11)
20615 /* Parse the using-declaration. */
20616 cp_parser_using_declaration (parser,
20617 /*access_declaration_p=*/false);
20618 return;
20620 else
20622 tree decl;
20623 bool alias_decl_expected;
20624 cp_parser_parse_tentatively (parser);
20625 decl = cp_parser_alias_declaration (parser);
20626 /* Note that if we actually see the '=' token after the
20627 identifier, cp_parser_alias_declaration commits the
20628 tentative parse. In that case, we really expects an
20629 alias-declaration. Otherwise, we expect a using
20630 declaration. */
20631 alias_decl_expected =
20632 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20633 cp_parser_parse_definitely (parser);
20635 if (alias_decl_expected)
20636 finish_member_declaration (decl);
20637 else
20638 cp_parser_using_declaration (parser,
20639 /*access_declaration_p=*/false);
20640 return;
20644 /* Check for @defs. */
20645 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20647 tree ivar, member;
20648 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20649 ivar = ivar_chains;
20650 while (ivar)
20652 member = ivar;
20653 ivar = TREE_CHAIN (member);
20654 TREE_CHAIN (member) = NULL_TREE;
20655 finish_member_declaration (member);
20657 return;
20660 /* If the next token is `static_assert' we have a static assertion. */
20661 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20663 cp_parser_static_assert (parser, /*member_p=*/true);
20664 return;
20667 parser->colon_corrects_to_scope_p = false;
20669 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20670 goto out;
20672 /* Parse the decl-specifier-seq. */
20673 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20674 cp_parser_decl_specifier_seq (parser,
20675 CP_PARSER_FLAGS_OPTIONAL,
20676 &decl_specifiers,
20677 &declares_class_or_enum);
20678 /* Check for an invalid type-name. */
20679 if (!decl_specifiers.any_type_specifiers_p
20680 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20681 goto out;
20682 /* If there is no declarator, then the decl-specifier-seq should
20683 specify a type. */
20684 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20686 /* If there was no decl-specifier-seq, and the next token is a
20687 `;', then we have something like:
20689 struct S { ; };
20691 [class.mem]
20693 Each member-declaration shall declare at least one member
20694 name of the class. */
20695 if (!decl_specifiers.any_specifiers_p)
20697 cp_token *token = cp_lexer_peek_token (parser->lexer);
20698 if (!in_system_header_at (token->location))
20699 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20701 else
20703 tree type;
20705 /* See if this declaration is a friend. */
20706 friend_p = cp_parser_friend_p (&decl_specifiers);
20707 /* If there were decl-specifiers, check to see if there was
20708 a class-declaration. */
20709 type = check_tag_decl (&decl_specifiers,
20710 /*explicit_type_instantiation_p=*/false);
20711 /* Nested classes have already been added to the class, but
20712 a `friend' needs to be explicitly registered. */
20713 if (friend_p)
20715 /* If the `friend' keyword was present, the friend must
20716 be introduced with a class-key. */
20717 if (!declares_class_or_enum && cxx_dialect < cxx11)
20718 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20719 "in C++03 a class-key must be used "
20720 "when declaring a friend");
20721 /* In this case:
20723 template <typename T> struct A {
20724 friend struct A<T>::B;
20727 A<T>::B will be represented by a TYPENAME_TYPE, and
20728 therefore not recognized by check_tag_decl. */
20729 if (!type)
20731 type = decl_specifiers.type;
20732 if (type && TREE_CODE (type) == TYPE_DECL)
20733 type = TREE_TYPE (type);
20735 if (!type || !TYPE_P (type))
20736 error_at (decl_spec_token_start->location,
20737 "friend declaration does not name a class or "
20738 "function");
20739 else
20740 make_friend_class (current_class_type, type,
20741 /*complain=*/true);
20743 /* If there is no TYPE, an error message will already have
20744 been issued. */
20745 else if (!type || type == error_mark_node)
20747 /* An anonymous aggregate has to be handled specially; such
20748 a declaration really declares a data member (with a
20749 particular type), as opposed to a nested class. */
20750 else if (ANON_AGGR_TYPE_P (type))
20752 /* C++11 9.5/6. */
20753 if (decl_specifiers.storage_class != sc_none)
20754 error_at (decl_spec_token_start->location,
20755 "a storage class on an anonymous aggregate "
20756 "in class scope is not allowed");
20758 /* Remove constructors and such from TYPE, now that we
20759 know it is an anonymous aggregate. */
20760 fixup_anonymous_aggr (type);
20761 /* And make the corresponding data member. */
20762 decl = build_decl (decl_spec_token_start->location,
20763 FIELD_DECL, NULL_TREE, type);
20764 /* Add it to the class. */
20765 finish_member_declaration (decl);
20767 else
20768 cp_parser_check_access_in_redeclaration
20769 (TYPE_NAME (type),
20770 decl_spec_token_start->location);
20773 else
20775 bool assume_semicolon = false;
20777 /* Clear attributes from the decl_specifiers but keep them
20778 around as prefix attributes that apply them to the entity
20779 being declared. */
20780 prefix_attributes = decl_specifiers.attributes;
20781 decl_specifiers.attributes = NULL_TREE;
20783 /* See if these declarations will be friends. */
20784 friend_p = cp_parser_friend_p (&decl_specifiers);
20786 /* Keep going until we hit the `;' at the end of the
20787 declaration. */
20788 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20790 tree attributes = NULL_TREE;
20791 tree first_attribute;
20793 /* Peek at the next token. */
20794 token = cp_lexer_peek_token (parser->lexer);
20796 /* Check for a bitfield declaration. */
20797 if (token->type == CPP_COLON
20798 || (token->type == CPP_NAME
20799 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20800 == CPP_COLON))
20802 tree identifier;
20803 tree width;
20805 /* Get the name of the bitfield. Note that we cannot just
20806 check TOKEN here because it may have been invalidated by
20807 the call to cp_lexer_peek_nth_token above. */
20808 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20809 identifier = cp_parser_identifier (parser);
20810 else
20811 identifier = NULL_TREE;
20813 /* Consume the `:' token. */
20814 cp_lexer_consume_token (parser->lexer);
20815 /* Get the width of the bitfield. */
20816 width
20817 = cp_parser_constant_expression (parser);
20819 /* Look for attributes that apply to the bitfield. */
20820 attributes = cp_parser_attributes_opt (parser);
20821 /* Remember which attributes are prefix attributes and
20822 which are not. */
20823 first_attribute = attributes;
20824 /* Combine the attributes. */
20825 attributes = chainon (prefix_attributes, attributes);
20827 /* Create the bitfield declaration. */
20828 decl = grokbitfield (identifier
20829 ? make_id_declarator (NULL_TREE,
20830 identifier,
20831 sfk_none)
20832 : NULL,
20833 &decl_specifiers,
20834 width,
20835 attributes);
20837 else
20839 cp_declarator *declarator;
20840 tree initializer;
20841 tree asm_specification;
20842 int ctor_dtor_or_conv_p;
20844 /* Parse the declarator. */
20845 declarator
20846 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20847 &ctor_dtor_or_conv_p,
20848 /*parenthesized_p=*/NULL,
20849 /*member_p=*/true,
20850 friend_p);
20852 /* If something went wrong parsing the declarator, make sure
20853 that we at least consume some tokens. */
20854 if (declarator == cp_error_declarator)
20856 /* Skip to the end of the statement. */
20857 cp_parser_skip_to_end_of_statement (parser);
20858 /* If the next token is not a semicolon, that is
20859 probably because we just skipped over the body of
20860 a function. So, we consume a semicolon if
20861 present, but do not issue an error message if it
20862 is not present. */
20863 if (cp_lexer_next_token_is (parser->lexer,
20864 CPP_SEMICOLON))
20865 cp_lexer_consume_token (parser->lexer);
20866 goto out;
20869 if (declares_class_or_enum & 2)
20870 cp_parser_check_for_definition_in_return_type
20871 (declarator, decl_specifiers.type,
20872 decl_specifiers.locations[ds_type_spec]);
20874 /* Look for an asm-specification. */
20875 asm_specification = cp_parser_asm_specification_opt (parser);
20876 /* Look for attributes that apply to the declaration. */
20877 attributes = cp_parser_attributes_opt (parser);
20878 /* Remember which attributes are prefix attributes and
20879 which are not. */
20880 first_attribute = attributes;
20881 /* Combine the attributes. */
20882 attributes = chainon (prefix_attributes, attributes);
20884 /* If it's an `=', then we have a constant-initializer or a
20885 pure-specifier. It is not correct to parse the
20886 initializer before registering the member declaration
20887 since the member declaration should be in scope while
20888 its initializer is processed. However, the rest of the
20889 front end does not yet provide an interface that allows
20890 us to handle this correctly. */
20891 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20893 /* In [class.mem]:
20895 A pure-specifier shall be used only in the declaration of
20896 a virtual function.
20898 A member-declarator can contain a constant-initializer
20899 only if it declares a static member of integral or
20900 enumeration type.
20902 Therefore, if the DECLARATOR is for a function, we look
20903 for a pure-specifier; otherwise, we look for a
20904 constant-initializer. When we call `grokfield', it will
20905 perform more stringent semantics checks. */
20906 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20907 if (function_declarator_p (declarator)
20908 || (decl_specifiers.type
20909 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20910 && declarator->kind == cdk_id
20911 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20912 == FUNCTION_TYPE)))
20913 initializer = cp_parser_pure_specifier (parser);
20914 else if (decl_specifiers.storage_class != sc_static)
20915 initializer = cp_parser_save_nsdmi (parser);
20916 else if (cxx_dialect >= cxx11)
20918 bool nonconst;
20919 /* Don't require a constant rvalue in C++11, since we
20920 might want a reference constant. We'll enforce
20921 constancy later. */
20922 cp_lexer_consume_token (parser->lexer);
20923 /* Parse the initializer. */
20924 initializer = cp_parser_initializer_clause (parser,
20925 &nonconst);
20927 else
20928 /* Parse the initializer. */
20929 initializer = cp_parser_constant_initializer (parser);
20931 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20932 && !function_declarator_p (declarator))
20934 bool x;
20935 if (decl_specifiers.storage_class != sc_static)
20936 initializer = cp_parser_save_nsdmi (parser);
20937 else
20938 initializer = cp_parser_initializer (parser, &x, &x);
20940 /* Otherwise, there is no initializer. */
20941 else
20942 initializer = NULL_TREE;
20944 /* See if we are probably looking at a function
20945 definition. We are certainly not looking at a
20946 member-declarator. Calling `grokfield' has
20947 side-effects, so we must not do it unless we are sure
20948 that we are looking at a member-declarator. */
20949 if (cp_parser_token_starts_function_definition_p
20950 (cp_lexer_peek_token (parser->lexer)))
20952 /* The grammar does not allow a pure-specifier to be
20953 used when a member function is defined. (It is
20954 possible that this fact is an oversight in the
20955 standard, since a pure function may be defined
20956 outside of the class-specifier. */
20957 if (initializer && initializer_token_start)
20958 error_at (initializer_token_start->location,
20959 "pure-specifier on function-definition");
20960 decl = cp_parser_save_member_function_body (parser,
20961 &decl_specifiers,
20962 declarator,
20963 attributes);
20964 if (parser->fully_implicit_function_template_p)
20965 decl = finish_fully_implicit_template (parser, decl);
20966 /* If the member was not a friend, declare it here. */
20967 if (!friend_p)
20968 finish_member_declaration (decl);
20969 /* Peek at the next token. */
20970 token = cp_lexer_peek_token (parser->lexer);
20971 /* If the next token is a semicolon, consume it. */
20972 if (token->type == CPP_SEMICOLON)
20973 cp_lexer_consume_token (parser->lexer);
20974 goto out;
20976 else
20977 if (declarator->kind == cdk_function)
20978 declarator->id_loc = token->location;
20979 /* Create the declaration. */
20980 decl = grokfield (declarator, &decl_specifiers,
20981 initializer, /*init_const_expr_p=*/true,
20982 asm_specification, attributes);
20983 if (parser->fully_implicit_function_template_p)
20985 if (friend_p)
20986 finish_fully_implicit_template (parser, 0);
20987 else
20988 decl = finish_fully_implicit_template (parser, decl);
20992 cp_finalize_omp_declare_simd (parser, decl);
20994 /* Reset PREFIX_ATTRIBUTES. */
20995 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20996 attributes = TREE_CHAIN (attributes);
20997 if (attributes)
20998 TREE_CHAIN (attributes) = NULL_TREE;
21000 /* If there is any qualification still in effect, clear it
21001 now; we will be starting fresh with the next declarator. */
21002 parser->scope = NULL_TREE;
21003 parser->qualifying_scope = NULL_TREE;
21004 parser->object_scope = NULL_TREE;
21005 /* If it's a `,', then there are more declarators. */
21006 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21008 cp_lexer_consume_token (parser->lexer);
21009 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21011 cp_token *token = cp_lexer_previous_token (parser->lexer);
21012 error_at (token->location,
21013 "stray %<,%> at end of member declaration");
21016 /* If the next token isn't a `;', then we have a parse error. */
21017 else if (cp_lexer_next_token_is_not (parser->lexer,
21018 CPP_SEMICOLON))
21020 /* The next token might be a ways away from where the
21021 actual semicolon is missing. Find the previous token
21022 and use that for our error position. */
21023 cp_token *token = cp_lexer_previous_token (parser->lexer);
21024 error_at (token->location,
21025 "expected %<;%> at end of member declaration");
21027 /* Assume that the user meant to provide a semicolon. If
21028 we were to cp_parser_skip_to_end_of_statement, we might
21029 skip to a semicolon inside a member function definition
21030 and issue nonsensical error messages. */
21031 assume_semicolon = true;
21034 if (decl)
21036 /* Add DECL to the list of members. */
21037 if (!friend_p)
21038 finish_member_declaration (decl);
21040 if (TREE_CODE (decl) == FUNCTION_DECL)
21041 cp_parser_save_default_args (parser, decl);
21042 else if (TREE_CODE (decl) == FIELD_DECL
21043 && !DECL_C_BIT_FIELD (decl)
21044 && DECL_INITIAL (decl))
21045 /* Add DECL to the queue of NSDMI to be parsed later. */
21046 vec_safe_push (unparsed_nsdmis, decl);
21049 if (assume_semicolon)
21050 goto out;
21054 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21055 out:
21056 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21059 /* Parse a pure-specifier.
21061 pure-specifier:
21064 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21065 Otherwise, ERROR_MARK_NODE is returned. */
21067 static tree
21068 cp_parser_pure_specifier (cp_parser* parser)
21070 cp_token *token;
21072 /* Look for the `=' token. */
21073 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21074 return error_mark_node;
21075 /* Look for the `0' token. */
21076 token = cp_lexer_peek_token (parser->lexer);
21078 if (token->type == CPP_EOF
21079 || token->type == CPP_PRAGMA_EOL)
21080 return error_mark_node;
21082 cp_lexer_consume_token (parser->lexer);
21084 /* Accept = default or = delete in c++0x mode. */
21085 if (token->keyword == RID_DEFAULT
21086 || token->keyword == RID_DELETE)
21088 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21089 return token->u.value;
21092 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21093 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21095 cp_parser_error (parser,
21096 "invalid pure specifier (only %<= 0%> is allowed)");
21097 cp_parser_skip_to_end_of_statement (parser);
21098 return error_mark_node;
21100 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21102 error_at (token->location, "templates may not be %<virtual%>");
21103 return error_mark_node;
21106 return integer_zero_node;
21109 /* Parse a constant-initializer.
21111 constant-initializer:
21112 = constant-expression
21114 Returns a representation of the constant-expression. */
21116 static tree
21117 cp_parser_constant_initializer (cp_parser* parser)
21119 /* Look for the `=' token. */
21120 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21121 return error_mark_node;
21123 /* It is invalid to write:
21125 struct S { static const int i = { 7 }; };
21128 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21130 cp_parser_error (parser,
21131 "a brace-enclosed initializer is not allowed here");
21132 /* Consume the opening brace. */
21133 cp_lexer_consume_token (parser->lexer);
21134 /* Skip the initializer. */
21135 cp_parser_skip_to_closing_brace (parser);
21136 /* Look for the trailing `}'. */
21137 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21139 return error_mark_node;
21142 return cp_parser_constant_expression (parser);
21145 /* Derived classes [gram.class.derived] */
21147 /* Parse a base-clause.
21149 base-clause:
21150 : base-specifier-list
21152 base-specifier-list:
21153 base-specifier ... [opt]
21154 base-specifier-list , base-specifier ... [opt]
21156 Returns a TREE_LIST representing the base-classes, in the order in
21157 which they were declared. The representation of each node is as
21158 described by cp_parser_base_specifier.
21160 In the case that no bases are specified, this function will return
21161 NULL_TREE, not ERROR_MARK_NODE. */
21163 static tree
21164 cp_parser_base_clause (cp_parser* parser)
21166 tree bases = NULL_TREE;
21168 /* Look for the `:' that begins the list. */
21169 cp_parser_require (parser, CPP_COLON, RT_COLON);
21171 /* Scan the base-specifier-list. */
21172 while (true)
21174 cp_token *token;
21175 tree base;
21176 bool pack_expansion_p = false;
21178 /* Look for the base-specifier. */
21179 base = cp_parser_base_specifier (parser);
21180 /* Look for the (optional) ellipsis. */
21181 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21183 /* Consume the `...'. */
21184 cp_lexer_consume_token (parser->lexer);
21186 pack_expansion_p = true;
21189 /* Add BASE to the front of the list. */
21190 if (base && base != error_mark_node)
21192 if (pack_expansion_p)
21193 /* Make this a pack expansion type. */
21194 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21196 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21198 TREE_CHAIN (base) = bases;
21199 bases = base;
21202 /* Peek at the next token. */
21203 token = cp_lexer_peek_token (parser->lexer);
21204 /* If it's not a comma, then the list is complete. */
21205 if (token->type != CPP_COMMA)
21206 break;
21207 /* Consume the `,'. */
21208 cp_lexer_consume_token (parser->lexer);
21211 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21212 base class had a qualified name. However, the next name that
21213 appears is certainly not qualified. */
21214 parser->scope = NULL_TREE;
21215 parser->qualifying_scope = NULL_TREE;
21216 parser->object_scope = NULL_TREE;
21218 return nreverse (bases);
21221 /* Parse a base-specifier.
21223 base-specifier:
21224 :: [opt] nested-name-specifier [opt] class-name
21225 virtual access-specifier [opt] :: [opt] nested-name-specifier
21226 [opt] class-name
21227 access-specifier virtual [opt] :: [opt] nested-name-specifier
21228 [opt] class-name
21230 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21231 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21232 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21233 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21235 static tree
21236 cp_parser_base_specifier (cp_parser* parser)
21238 cp_token *token;
21239 bool done = false;
21240 bool virtual_p = false;
21241 bool duplicate_virtual_error_issued_p = false;
21242 bool duplicate_access_error_issued_p = false;
21243 bool class_scope_p, template_p;
21244 tree access = access_default_node;
21245 tree type;
21247 /* Process the optional `virtual' and `access-specifier'. */
21248 while (!done)
21250 /* Peek at the next token. */
21251 token = cp_lexer_peek_token (parser->lexer);
21252 /* Process `virtual'. */
21253 switch (token->keyword)
21255 case RID_VIRTUAL:
21256 /* If `virtual' appears more than once, issue an error. */
21257 if (virtual_p && !duplicate_virtual_error_issued_p)
21259 cp_parser_error (parser,
21260 "%<virtual%> specified more than once in base-specified");
21261 duplicate_virtual_error_issued_p = true;
21264 virtual_p = true;
21266 /* Consume the `virtual' token. */
21267 cp_lexer_consume_token (parser->lexer);
21269 break;
21271 case RID_PUBLIC:
21272 case RID_PROTECTED:
21273 case RID_PRIVATE:
21274 /* If more than one access specifier appears, issue an
21275 error. */
21276 if (access != access_default_node
21277 && !duplicate_access_error_issued_p)
21279 cp_parser_error (parser,
21280 "more than one access specifier in base-specified");
21281 duplicate_access_error_issued_p = true;
21284 access = ridpointers[(int) token->keyword];
21286 /* Consume the access-specifier. */
21287 cp_lexer_consume_token (parser->lexer);
21289 break;
21291 default:
21292 done = true;
21293 break;
21296 /* It is not uncommon to see programs mechanically, erroneously, use
21297 the 'typename' keyword to denote (dependent) qualified types
21298 as base classes. */
21299 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21301 token = cp_lexer_peek_token (parser->lexer);
21302 if (!processing_template_decl)
21303 error_at (token->location,
21304 "keyword %<typename%> not allowed outside of templates");
21305 else
21306 error_at (token->location,
21307 "keyword %<typename%> not allowed in this context "
21308 "(the base class is implicitly a type)");
21309 cp_lexer_consume_token (parser->lexer);
21312 /* Look for the optional `::' operator. */
21313 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21314 /* Look for the nested-name-specifier. The simplest way to
21315 implement:
21317 [temp.res]
21319 The keyword `typename' is not permitted in a base-specifier or
21320 mem-initializer; in these contexts a qualified name that
21321 depends on a template-parameter is implicitly assumed to be a
21322 type name.
21324 is to pretend that we have seen the `typename' keyword at this
21325 point. */
21326 cp_parser_nested_name_specifier_opt (parser,
21327 /*typename_keyword_p=*/true,
21328 /*check_dependency_p=*/true,
21329 typename_type,
21330 /*is_declaration=*/true);
21331 /* If the base class is given by a qualified name, assume that names
21332 we see are type names or templates, as appropriate. */
21333 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21334 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21336 if (!parser->scope
21337 && cp_lexer_next_token_is_decltype (parser->lexer))
21338 /* DR 950 allows decltype as a base-specifier. */
21339 type = cp_parser_decltype (parser);
21340 else
21342 /* Otherwise, look for the class-name. */
21343 type = cp_parser_class_name (parser,
21344 class_scope_p,
21345 template_p,
21346 typename_type,
21347 /*check_dependency_p=*/true,
21348 /*class_head_p=*/false,
21349 /*is_declaration=*/true);
21350 type = TREE_TYPE (type);
21353 if (type == error_mark_node)
21354 return error_mark_node;
21356 return finish_base_specifier (type, access, virtual_p);
21359 /* Exception handling [gram.exception] */
21361 /* Parse an (optional) noexcept-specification.
21363 noexcept-specification:
21364 noexcept ( constant-expression ) [opt]
21366 If no noexcept-specification is present, returns NULL_TREE.
21367 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21368 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21369 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21370 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21371 in which case a boolean condition is returned instead. */
21373 static tree
21374 cp_parser_noexcept_specification_opt (cp_parser* parser,
21375 bool require_constexpr,
21376 bool* consumed_expr,
21377 bool return_cond)
21379 cp_token *token;
21380 const char *saved_message;
21382 /* Peek at the next token. */
21383 token = cp_lexer_peek_token (parser->lexer);
21385 /* Is it a noexcept-specification? */
21386 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21388 tree expr;
21389 cp_lexer_consume_token (parser->lexer);
21391 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21393 cp_lexer_consume_token (parser->lexer);
21395 if (require_constexpr)
21397 /* Types may not be defined in an exception-specification. */
21398 saved_message = parser->type_definition_forbidden_message;
21399 parser->type_definition_forbidden_message
21400 = G_("types may not be defined in an exception-specification");
21402 expr = cp_parser_constant_expression (parser);
21404 /* Restore the saved message. */
21405 parser->type_definition_forbidden_message = saved_message;
21407 else
21409 expr = cp_parser_expression (parser);
21410 *consumed_expr = true;
21413 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21415 else
21417 expr = boolean_true_node;
21418 if (!require_constexpr)
21419 *consumed_expr = false;
21422 /* We cannot build a noexcept-spec right away because this will check
21423 that expr is a constexpr. */
21424 if (!return_cond)
21425 return build_noexcept_spec (expr, tf_warning_or_error);
21426 else
21427 return expr;
21429 else
21430 return NULL_TREE;
21433 /* Parse an (optional) exception-specification.
21435 exception-specification:
21436 throw ( type-id-list [opt] )
21438 Returns a TREE_LIST representing the exception-specification. The
21439 TREE_VALUE of each node is a type. */
21441 static tree
21442 cp_parser_exception_specification_opt (cp_parser* parser)
21444 cp_token *token;
21445 tree type_id_list;
21446 const char *saved_message;
21448 /* Peek at the next token. */
21449 token = cp_lexer_peek_token (parser->lexer);
21451 /* Is it a noexcept-specification? */
21452 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21453 false);
21454 if (type_id_list != NULL_TREE)
21455 return type_id_list;
21457 /* If it's not `throw', then there's no exception-specification. */
21458 if (!cp_parser_is_keyword (token, RID_THROW))
21459 return NULL_TREE;
21461 #if 0
21462 /* Enable this once a lot of code has transitioned to noexcept? */
21463 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21464 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21465 "deprecated in C++0x; use %<noexcept%> instead");
21466 #endif
21468 /* Consume the `throw'. */
21469 cp_lexer_consume_token (parser->lexer);
21471 /* Look for the `('. */
21472 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21474 /* Peek at the next token. */
21475 token = cp_lexer_peek_token (parser->lexer);
21476 /* If it's not a `)', then there is a type-id-list. */
21477 if (token->type != CPP_CLOSE_PAREN)
21479 /* Types may not be defined in an exception-specification. */
21480 saved_message = parser->type_definition_forbidden_message;
21481 parser->type_definition_forbidden_message
21482 = G_("types may not be defined in an exception-specification");
21483 /* Parse the type-id-list. */
21484 type_id_list = cp_parser_type_id_list (parser);
21485 /* Restore the saved message. */
21486 parser->type_definition_forbidden_message = saved_message;
21488 else
21489 type_id_list = empty_except_spec;
21491 /* Look for the `)'. */
21492 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21494 return type_id_list;
21497 /* Parse an (optional) type-id-list.
21499 type-id-list:
21500 type-id ... [opt]
21501 type-id-list , type-id ... [opt]
21503 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21504 in the order that the types were presented. */
21506 static tree
21507 cp_parser_type_id_list (cp_parser* parser)
21509 tree types = NULL_TREE;
21511 while (true)
21513 cp_token *token;
21514 tree type;
21516 /* Get the next type-id. */
21517 type = cp_parser_type_id (parser);
21518 /* Parse the optional ellipsis. */
21519 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21521 /* Consume the `...'. */
21522 cp_lexer_consume_token (parser->lexer);
21524 /* Turn the type into a pack expansion expression. */
21525 type = make_pack_expansion (type);
21527 /* Add it to the list. */
21528 types = add_exception_specifier (types, type, /*complain=*/1);
21529 /* Peek at the next token. */
21530 token = cp_lexer_peek_token (parser->lexer);
21531 /* If it is not a `,', we are done. */
21532 if (token->type != CPP_COMMA)
21533 break;
21534 /* Consume the `,'. */
21535 cp_lexer_consume_token (parser->lexer);
21538 return nreverse (types);
21541 /* Parse a try-block.
21543 try-block:
21544 try compound-statement handler-seq */
21546 static tree
21547 cp_parser_try_block (cp_parser* parser)
21549 tree try_block;
21551 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21552 if (parser->in_function_body
21553 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21554 error ("%<try%> in %<constexpr%> function");
21556 try_block = begin_try_block ();
21557 cp_parser_compound_statement (parser, NULL, true, false);
21558 finish_try_block (try_block);
21559 cp_parser_handler_seq (parser);
21560 finish_handler_sequence (try_block);
21562 return try_block;
21565 /* Parse a function-try-block.
21567 function-try-block:
21568 try ctor-initializer [opt] function-body handler-seq */
21570 static bool
21571 cp_parser_function_try_block (cp_parser* parser)
21573 tree compound_stmt;
21574 tree try_block;
21575 bool ctor_initializer_p;
21577 /* Look for the `try' keyword. */
21578 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21579 return false;
21580 /* Let the rest of the front end know where we are. */
21581 try_block = begin_function_try_block (&compound_stmt);
21582 /* Parse the function-body. */
21583 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21584 (parser, /*in_function_try_block=*/true);
21585 /* We're done with the `try' part. */
21586 finish_function_try_block (try_block);
21587 /* Parse the handlers. */
21588 cp_parser_handler_seq (parser);
21589 /* We're done with the handlers. */
21590 finish_function_handler_sequence (try_block, compound_stmt);
21592 return ctor_initializer_p;
21595 /* Parse a handler-seq.
21597 handler-seq:
21598 handler handler-seq [opt] */
21600 static void
21601 cp_parser_handler_seq (cp_parser* parser)
21603 while (true)
21605 cp_token *token;
21607 /* Parse the handler. */
21608 cp_parser_handler (parser);
21609 /* Peek at the next token. */
21610 token = cp_lexer_peek_token (parser->lexer);
21611 /* If it's not `catch' then there are no more handlers. */
21612 if (!cp_parser_is_keyword (token, RID_CATCH))
21613 break;
21617 /* Parse a handler.
21619 handler:
21620 catch ( exception-declaration ) compound-statement */
21622 static void
21623 cp_parser_handler (cp_parser* parser)
21625 tree handler;
21626 tree declaration;
21628 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21629 handler = begin_handler ();
21630 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21631 declaration = cp_parser_exception_declaration (parser);
21632 finish_handler_parms (declaration, handler);
21633 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21634 cp_parser_compound_statement (parser, NULL, false, false);
21635 finish_handler (handler);
21638 /* Parse an exception-declaration.
21640 exception-declaration:
21641 type-specifier-seq declarator
21642 type-specifier-seq abstract-declarator
21643 type-specifier-seq
21646 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21647 ellipsis variant is used. */
21649 static tree
21650 cp_parser_exception_declaration (cp_parser* parser)
21652 cp_decl_specifier_seq type_specifiers;
21653 cp_declarator *declarator;
21654 const char *saved_message;
21656 /* If it's an ellipsis, it's easy to handle. */
21657 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21659 /* Consume the `...' token. */
21660 cp_lexer_consume_token (parser->lexer);
21661 return NULL_TREE;
21664 /* Types may not be defined in exception-declarations. */
21665 saved_message = parser->type_definition_forbidden_message;
21666 parser->type_definition_forbidden_message
21667 = G_("types may not be defined in exception-declarations");
21669 /* Parse the type-specifier-seq. */
21670 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21671 /*is_trailing_return=*/false,
21672 &type_specifiers);
21673 /* If it's a `)', then there is no declarator. */
21674 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21675 declarator = NULL;
21676 else
21677 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21678 /*ctor_dtor_or_conv_p=*/NULL,
21679 /*parenthesized_p=*/NULL,
21680 /*member_p=*/false,
21681 /*friend_p=*/false);
21683 /* Restore the saved message. */
21684 parser->type_definition_forbidden_message = saved_message;
21686 if (!type_specifiers.any_specifiers_p)
21687 return error_mark_node;
21689 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21692 /* Parse a throw-expression.
21694 throw-expression:
21695 throw assignment-expression [opt]
21697 Returns a THROW_EXPR representing the throw-expression. */
21699 static tree
21700 cp_parser_throw_expression (cp_parser* parser)
21702 tree expression;
21703 cp_token* token;
21705 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21706 token = cp_lexer_peek_token (parser->lexer);
21707 /* Figure out whether or not there is an assignment-expression
21708 following the "throw" keyword. */
21709 if (token->type == CPP_COMMA
21710 || token->type == CPP_SEMICOLON
21711 || token->type == CPP_CLOSE_PAREN
21712 || token->type == CPP_CLOSE_SQUARE
21713 || token->type == CPP_CLOSE_BRACE
21714 || token->type == CPP_COLON)
21715 expression = NULL_TREE;
21716 else
21717 expression = cp_parser_assignment_expression (parser);
21719 return build_throw (expression);
21722 /* GNU Extensions */
21724 /* Parse an (optional) asm-specification.
21726 asm-specification:
21727 asm ( string-literal )
21729 If the asm-specification is present, returns a STRING_CST
21730 corresponding to the string-literal. Otherwise, returns
21731 NULL_TREE. */
21733 static tree
21734 cp_parser_asm_specification_opt (cp_parser* parser)
21736 cp_token *token;
21737 tree asm_specification;
21739 /* Peek at the next token. */
21740 token = cp_lexer_peek_token (parser->lexer);
21741 /* If the next token isn't the `asm' keyword, then there's no
21742 asm-specification. */
21743 if (!cp_parser_is_keyword (token, RID_ASM))
21744 return NULL_TREE;
21746 /* Consume the `asm' token. */
21747 cp_lexer_consume_token (parser->lexer);
21748 /* Look for the `('. */
21749 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21751 /* Look for the string-literal. */
21752 asm_specification = cp_parser_string_literal (parser, false, false);
21754 /* Look for the `)'. */
21755 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21757 return asm_specification;
21760 /* Parse an asm-operand-list.
21762 asm-operand-list:
21763 asm-operand
21764 asm-operand-list , asm-operand
21766 asm-operand:
21767 string-literal ( expression )
21768 [ string-literal ] string-literal ( expression )
21770 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21771 each node is the expression. The TREE_PURPOSE is itself a
21772 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21773 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21774 is a STRING_CST for the string literal before the parenthesis. Returns
21775 ERROR_MARK_NODE if any of the operands are invalid. */
21777 static tree
21778 cp_parser_asm_operand_list (cp_parser* parser)
21780 tree asm_operands = NULL_TREE;
21781 bool invalid_operands = false;
21783 while (true)
21785 tree string_literal;
21786 tree expression;
21787 tree name;
21789 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21791 /* Consume the `[' token. */
21792 cp_lexer_consume_token (parser->lexer);
21793 /* Read the operand name. */
21794 name = cp_parser_identifier (parser);
21795 if (name != error_mark_node)
21796 name = build_string (IDENTIFIER_LENGTH (name),
21797 IDENTIFIER_POINTER (name));
21798 /* Look for the closing `]'. */
21799 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21801 else
21802 name = NULL_TREE;
21803 /* Look for the string-literal. */
21804 string_literal = cp_parser_string_literal (parser, false, false);
21806 /* Look for the `('. */
21807 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21808 /* Parse the expression. */
21809 expression = cp_parser_expression (parser);
21810 /* Look for the `)'. */
21811 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21813 if (name == error_mark_node
21814 || string_literal == error_mark_node
21815 || expression == error_mark_node)
21816 invalid_operands = true;
21818 /* Add this operand to the list. */
21819 asm_operands = tree_cons (build_tree_list (name, string_literal),
21820 expression,
21821 asm_operands);
21822 /* If the next token is not a `,', there are no more
21823 operands. */
21824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21825 break;
21826 /* Consume the `,'. */
21827 cp_lexer_consume_token (parser->lexer);
21830 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21833 /* Parse an asm-clobber-list.
21835 asm-clobber-list:
21836 string-literal
21837 asm-clobber-list , string-literal
21839 Returns a TREE_LIST, indicating the clobbers in the order that they
21840 appeared. The TREE_VALUE of each node is a STRING_CST. */
21842 static tree
21843 cp_parser_asm_clobber_list (cp_parser* parser)
21845 tree clobbers = NULL_TREE;
21847 while (true)
21849 tree string_literal;
21851 /* Look for the string literal. */
21852 string_literal = cp_parser_string_literal (parser, false, false);
21853 /* Add it to the list. */
21854 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21855 /* If the next token is not a `,', then the list is
21856 complete. */
21857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21858 break;
21859 /* Consume the `,' token. */
21860 cp_lexer_consume_token (parser->lexer);
21863 return clobbers;
21866 /* Parse an asm-label-list.
21868 asm-label-list:
21869 identifier
21870 asm-label-list , identifier
21872 Returns a TREE_LIST, indicating the labels in the order that they
21873 appeared. The TREE_VALUE of each node is a label. */
21875 static tree
21876 cp_parser_asm_label_list (cp_parser* parser)
21878 tree labels = NULL_TREE;
21880 while (true)
21882 tree identifier, label, name;
21884 /* Look for the identifier. */
21885 identifier = cp_parser_identifier (parser);
21886 if (!error_operand_p (identifier))
21888 label = lookup_label (identifier);
21889 if (TREE_CODE (label) == LABEL_DECL)
21891 TREE_USED (label) = 1;
21892 check_goto (label);
21893 name = build_string (IDENTIFIER_LENGTH (identifier),
21894 IDENTIFIER_POINTER (identifier));
21895 labels = tree_cons (name, label, labels);
21898 /* If the next token is not a `,', then the list is
21899 complete. */
21900 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21901 break;
21902 /* Consume the `,' token. */
21903 cp_lexer_consume_token (parser->lexer);
21906 return nreverse (labels);
21909 /* Return TRUE iff the next tokens in the stream are possibly the
21910 beginning of a GNU extension attribute. */
21912 static bool
21913 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21915 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21918 /* Return TRUE iff the next tokens in the stream are possibly the
21919 beginning of a standard C++-11 attribute specifier. */
21921 static bool
21922 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21924 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21927 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21928 beginning of a standard C++-11 attribute specifier. */
21930 static bool
21931 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21933 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21935 return (cxx_dialect >= cxx11
21936 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21937 || (token->type == CPP_OPEN_SQUARE
21938 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21939 && token->type == CPP_OPEN_SQUARE)));
21942 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21943 beginning of a GNU extension attribute. */
21945 static bool
21946 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21948 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21950 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21953 /* Return true iff the next tokens can be the beginning of either a
21954 GNU attribute list, or a standard C++11 attribute sequence. */
21956 static bool
21957 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21959 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21960 || cp_next_tokens_can_be_std_attribute_p (parser));
21963 /* Return true iff the next Nth tokens can be the beginning of either
21964 a GNU attribute list, or a standard C++11 attribute sequence. */
21966 static bool
21967 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21969 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21970 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21973 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21974 of GNU attributes, or return NULL. */
21976 static tree
21977 cp_parser_attributes_opt (cp_parser *parser)
21979 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21980 return cp_parser_gnu_attributes_opt (parser);
21981 return cp_parser_std_attribute_spec_seq (parser);
21984 #define CILK_SIMD_FN_CLAUSE_MASK \
21985 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21986 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21987 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21988 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21989 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21991 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21992 vector [(<clauses>)] */
21994 static void
21995 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
21997 bool first_p = parser->cilk_simd_fn_info == NULL;
21998 cp_token *token = v_token;
21999 if (first_p)
22001 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22002 parser->cilk_simd_fn_info->error_seen = false;
22003 parser->cilk_simd_fn_info->fndecl_seen = false;
22004 parser->cilk_simd_fn_info->tokens = vNULL;
22006 int paren_scope = 0;
22007 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22009 cp_lexer_consume_token (parser->lexer);
22010 v_token = cp_lexer_peek_token (parser->lexer);
22011 paren_scope++;
22013 while (paren_scope > 0)
22015 token = cp_lexer_peek_token (parser->lexer);
22016 if (token->type == CPP_OPEN_PAREN)
22017 paren_scope++;
22018 else if (token->type == CPP_CLOSE_PAREN)
22019 paren_scope--;
22020 /* Do not push the last ')' */
22021 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22022 cp_lexer_consume_token (parser->lexer);
22025 token->type = CPP_PRAGMA_EOL;
22026 parser->lexer->next_token = token;
22027 cp_lexer_consume_token (parser->lexer);
22029 struct cp_token_cache *cp
22030 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22031 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22034 /* Parse an (optional) series of attributes.
22036 attributes:
22037 attributes attribute
22039 attribute:
22040 __attribute__ (( attribute-list [opt] ))
22042 The return value is as for cp_parser_gnu_attribute_list. */
22044 static tree
22045 cp_parser_gnu_attributes_opt (cp_parser* parser)
22047 tree attributes = NULL_TREE;
22049 while (true)
22051 cp_token *token;
22052 tree attribute_list;
22053 bool ok = true;
22055 /* Peek at the next token. */
22056 token = cp_lexer_peek_token (parser->lexer);
22057 /* If it's not `__attribute__', then we're done. */
22058 if (token->keyword != RID_ATTRIBUTE)
22059 break;
22061 /* Consume the `__attribute__' keyword. */
22062 cp_lexer_consume_token (parser->lexer);
22063 /* Look for the two `(' tokens. */
22064 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22065 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22067 /* Peek at the next token. */
22068 token = cp_lexer_peek_token (parser->lexer);
22069 if (token->type != CPP_CLOSE_PAREN)
22070 /* Parse the attribute-list. */
22071 attribute_list = cp_parser_gnu_attribute_list (parser);
22072 else
22073 /* If the next token is a `)', then there is no attribute
22074 list. */
22075 attribute_list = NULL;
22077 /* Look for the two `)' tokens. */
22078 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22079 ok = false;
22080 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22081 ok = false;
22082 if (!ok)
22083 cp_parser_skip_to_end_of_statement (parser);
22085 /* Add these new attributes to the list. */
22086 attributes = chainon (attributes, attribute_list);
22089 return attributes;
22092 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22093 "__vector" or "__vector__." */
22095 static inline bool
22096 is_cilkplus_vector_p (tree name)
22098 if (flag_cilkplus && is_attribute_p ("vector", name))
22099 return true;
22100 return false;
22103 /* Parse a GNU attribute-list.
22105 attribute-list:
22106 attribute
22107 attribute-list , attribute
22109 attribute:
22110 identifier
22111 identifier ( identifier )
22112 identifier ( identifier , expression-list )
22113 identifier ( expression-list )
22115 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22116 to an attribute. The TREE_PURPOSE of each node is the identifier
22117 indicating which attribute is in use. The TREE_VALUE represents
22118 the arguments, if any. */
22120 static tree
22121 cp_parser_gnu_attribute_list (cp_parser* parser)
22123 tree attribute_list = NULL_TREE;
22124 bool save_translate_strings_p = parser->translate_strings_p;
22126 parser->translate_strings_p = false;
22127 while (true)
22129 cp_token *token;
22130 tree identifier;
22131 tree attribute;
22133 /* Look for the identifier. We also allow keywords here; for
22134 example `__attribute__ ((const))' is legal. */
22135 token = cp_lexer_peek_token (parser->lexer);
22136 if (token->type == CPP_NAME
22137 || token->type == CPP_KEYWORD)
22139 tree arguments = NULL_TREE;
22141 /* Consume the token, but save it since we need it for the
22142 SIMD enabled function parsing. */
22143 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22145 /* Save away the identifier that indicates which attribute
22146 this is. */
22147 identifier = (token->type == CPP_KEYWORD)
22148 /* For keywords, use the canonical spelling, not the
22149 parsed identifier. */
22150 ? ridpointers[(int) token->keyword]
22151 : id_token->u.value;
22153 attribute = build_tree_list (identifier, NULL_TREE);
22155 /* Peek at the next token. */
22156 token = cp_lexer_peek_token (parser->lexer);
22157 /* If it's an `(', then parse the attribute arguments. */
22158 if (token->type == CPP_OPEN_PAREN)
22160 vec<tree, va_gc> *vec;
22161 int attr_flag = (attribute_takes_identifier_p (identifier)
22162 ? id_attr : normal_attr);
22163 if (is_cilkplus_vector_p (identifier))
22165 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22166 continue;
22168 else
22169 vec = cp_parser_parenthesized_expression_list
22170 (parser, attr_flag, /*cast_p=*/false,
22171 /*allow_expansion_p=*/false,
22172 /*non_constant_p=*/NULL);
22173 if (vec == NULL)
22174 arguments = error_mark_node;
22175 else
22177 arguments = build_tree_list_vec (vec);
22178 release_tree_vector (vec);
22180 /* Save the arguments away. */
22181 TREE_VALUE (attribute) = arguments;
22183 else if (is_cilkplus_vector_p (identifier))
22185 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22186 continue;
22189 if (arguments != error_mark_node)
22191 /* Add this attribute to the list. */
22192 TREE_CHAIN (attribute) = attribute_list;
22193 attribute_list = attribute;
22196 token = cp_lexer_peek_token (parser->lexer);
22198 /* Now, look for more attributes. If the next token isn't a
22199 `,', we're done. */
22200 if (token->type != CPP_COMMA)
22201 break;
22203 /* Consume the comma and keep going. */
22204 cp_lexer_consume_token (parser->lexer);
22206 parser->translate_strings_p = save_translate_strings_p;
22208 /* We built up the list in reverse order. */
22209 return nreverse (attribute_list);
22212 /* Parse a standard C++11 attribute.
22214 The returned representation is a TREE_LIST which TREE_PURPOSE is
22215 the scoped name of the attribute, and the TREE_VALUE is its
22216 arguments list.
22218 Note that the scoped name of the attribute is itself a TREE_LIST
22219 which TREE_PURPOSE is the namespace of the attribute, and
22220 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22221 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22222 and which TREE_PURPOSE is directly the attribute name.
22224 Clients of the attribute code should use get_attribute_namespace
22225 and get_attribute_name to get the actual namespace and name of
22226 attributes, regardless of their being GNU or C++11 attributes.
22228 attribute:
22229 attribute-token attribute-argument-clause [opt]
22231 attribute-token:
22232 identifier
22233 attribute-scoped-token
22235 attribute-scoped-token:
22236 attribute-namespace :: identifier
22238 attribute-namespace:
22239 identifier
22241 attribute-argument-clause:
22242 ( balanced-token-seq )
22244 balanced-token-seq:
22245 balanced-token [opt]
22246 balanced-token-seq balanced-token
22248 balanced-token:
22249 ( balanced-token-seq )
22250 [ balanced-token-seq ]
22251 { balanced-token-seq }. */
22253 static tree
22254 cp_parser_std_attribute (cp_parser *parser)
22256 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22257 cp_token *token;
22259 /* First, parse name of the the attribute, a.k.a
22260 attribute-token. */
22262 token = cp_lexer_peek_token (parser->lexer);
22263 if (token->type == CPP_NAME)
22264 attr_id = token->u.value;
22265 else if (token->type == CPP_KEYWORD)
22266 attr_id = ridpointers[(int) token->keyword];
22267 else if (token->flags & NAMED_OP)
22268 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22270 if (attr_id == NULL_TREE)
22271 return NULL_TREE;
22273 cp_lexer_consume_token (parser->lexer);
22275 token = cp_lexer_peek_token (parser->lexer);
22276 if (token->type == CPP_SCOPE)
22278 /* We are seeing a scoped attribute token. */
22280 cp_lexer_consume_token (parser->lexer);
22281 attr_ns = attr_id;
22283 token = cp_lexer_consume_token (parser->lexer);
22284 if (token->type == CPP_NAME)
22285 attr_id = token->u.value;
22286 else if (token->type == CPP_KEYWORD)
22287 attr_id = ridpointers[(int) token->keyword];
22288 else
22290 error_at (token->location,
22291 "expected an identifier for the attribute name");
22292 return error_mark_node;
22294 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22295 NULL_TREE);
22296 token = cp_lexer_peek_token (parser->lexer);
22298 else
22300 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22301 NULL_TREE);
22302 /* C++11 noreturn attribute is equivalent to GNU's. */
22303 if (is_attribute_p ("noreturn", attr_id))
22304 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22305 /* C++14 deprecated attribute is equivalent to GNU's. */
22306 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22308 if (cxx_dialect == cxx11)
22309 pedwarn (token->location, OPT_Wpedantic,
22310 "%<deprecated%> is a C++14 feature;"
22311 " use %<gnu::deprecated%>");
22312 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22316 /* Now parse the optional argument clause of the attribute. */
22318 if (token->type != CPP_OPEN_PAREN)
22319 return attribute;
22322 vec<tree, va_gc> *vec;
22323 int attr_flag = normal_attr;
22325 if (attr_ns == get_identifier ("gnu")
22326 && attribute_takes_identifier_p (attr_id))
22327 /* A GNU attribute that takes an identifier in parameter. */
22328 attr_flag = id_attr;
22330 vec = cp_parser_parenthesized_expression_list
22331 (parser, attr_flag, /*cast_p=*/false,
22332 /*allow_expansion_p=*/true,
22333 /*non_constant_p=*/NULL);
22334 if (vec == NULL)
22335 arguments = error_mark_node;
22336 else
22338 arguments = build_tree_list_vec (vec);
22339 release_tree_vector (vec);
22342 if (arguments == error_mark_node)
22343 attribute = error_mark_node;
22344 else
22345 TREE_VALUE (attribute) = arguments;
22348 return attribute;
22351 /* Parse a list of standard C++-11 attributes.
22353 attribute-list:
22354 attribute [opt]
22355 attribute-list , attribute[opt]
22356 attribute ...
22357 attribute-list , attribute ...
22360 static tree
22361 cp_parser_std_attribute_list (cp_parser *parser)
22363 tree attributes = NULL_TREE, attribute = NULL_TREE;
22364 cp_token *token = NULL;
22366 while (true)
22368 attribute = cp_parser_std_attribute (parser);
22369 if (attribute == error_mark_node)
22370 break;
22371 if (attribute != NULL_TREE)
22373 TREE_CHAIN (attribute) = attributes;
22374 attributes = attribute;
22376 token = cp_lexer_peek_token (parser->lexer);
22377 if (token->type != CPP_COMMA)
22378 break;
22379 cp_lexer_consume_token (parser->lexer);
22381 attributes = nreverse (attributes);
22382 return attributes;
22385 /* Parse a standard C++-11 attribute specifier.
22387 attribute-specifier:
22388 [ [ attribute-list ] ]
22389 alignment-specifier
22391 alignment-specifier:
22392 alignas ( type-id ... [opt] )
22393 alignas ( alignment-expression ... [opt] ). */
22395 static tree
22396 cp_parser_std_attribute_spec (cp_parser *parser)
22398 tree attributes = NULL_TREE;
22399 cp_token *token = cp_lexer_peek_token (parser->lexer);
22401 if (token->type == CPP_OPEN_SQUARE
22402 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22404 cp_lexer_consume_token (parser->lexer);
22405 cp_lexer_consume_token (parser->lexer);
22407 attributes = cp_parser_std_attribute_list (parser);
22409 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22410 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22411 cp_parser_skip_to_end_of_statement (parser);
22412 else
22413 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22414 when we are sure that we have actually parsed them. */
22415 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22417 else
22419 tree alignas_expr;
22421 /* Look for an alignment-specifier. */
22423 token = cp_lexer_peek_token (parser->lexer);
22425 if (token->type != CPP_KEYWORD
22426 || token->keyword != RID_ALIGNAS)
22427 return NULL_TREE;
22429 cp_lexer_consume_token (parser->lexer);
22430 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22432 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22434 cp_parser_error (parser, "expected %<(%>");
22435 return error_mark_node;
22438 cp_parser_parse_tentatively (parser);
22439 alignas_expr = cp_parser_type_id (parser);
22441 if (!cp_parser_parse_definitely (parser))
22443 gcc_assert (alignas_expr == error_mark_node
22444 || alignas_expr == NULL_TREE);
22446 alignas_expr =
22447 cp_parser_assignment_expression (parser);
22448 if (alignas_expr == error_mark_node)
22449 cp_parser_skip_to_end_of_statement (parser);
22450 if (alignas_expr == NULL_TREE
22451 || alignas_expr == error_mark_node)
22452 return alignas_expr;
22455 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22457 cp_parser_error (parser, "expected %<)%>");
22458 return error_mark_node;
22461 alignas_expr = cxx_alignas_expr (alignas_expr);
22463 /* Build the C++-11 representation of an 'aligned'
22464 attribute. */
22465 attributes =
22466 build_tree_list (build_tree_list (get_identifier ("gnu"),
22467 get_identifier ("aligned")),
22468 build_tree_list (NULL_TREE, alignas_expr));
22471 return attributes;
22474 /* Parse a standard C++-11 attribute-specifier-seq.
22476 attribute-specifier-seq:
22477 attribute-specifier-seq [opt] attribute-specifier
22480 static tree
22481 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22483 tree attr_specs = NULL;
22485 while (true)
22487 tree attr_spec = cp_parser_std_attribute_spec (parser);
22488 if (attr_spec == NULL_TREE)
22489 break;
22490 if (attr_spec == error_mark_node)
22491 return error_mark_node;
22493 TREE_CHAIN (attr_spec) = attr_specs;
22494 attr_specs = attr_spec;
22497 attr_specs = nreverse (attr_specs);
22498 return attr_specs;
22501 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22502 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22503 current value of the PEDANTIC flag, regardless of whether or not
22504 the `__extension__' keyword is present. The caller is responsible
22505 for restoring the value of the PEDANTIC flag. */
22507 static bool
22508 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22510 /* Save the old value of the PEDANTIC flag. */
22511 *saved_pedantic = pedantic;
22513 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22515 /* Consume the `__extension__' token. */
22516 cp_lexer_consume_token (parser->lexer);
22517 /* We're not being pedantic while the `__extension__' keyword is
22518 in effect. */
22519 pedantic = 0;
22521 return true;
22524 return false;
22527 /* Parse a label declaration.
22529 label-declaration:
22530 __label__ label-declarator-seq ;
22532 label-declarator-seq:
22533 identifier , label-declarator-seq
22534 identifier */
22536 static void
22537 cp_parser_label_declaration (cp_parser* parser)
22539 /* Look for the `__label__' keyword. */
22540 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22542 while (true)
22544 tree identifier;
22546 /* Look for an identifier. */
22547 identifier = cp_parser_identifier (parser);
22548 /* If we failed, stop. */
22549 if (identifier == error_mark_node)
22550 break;
22551 /* Declare it as a label. */
22552 finish_label_decl (identifier);
22553 /* If the next token is a `;', stop. */
22554 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22555 break;
22556 /* Look for the `,' separating the label declarations. */
22557 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22560 /* Look for the final `;'. */
22561 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22564 /* Support Functions */
22566 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22567 NAME should have one of the representations used for an
22568 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22569 is returned. If PARSER->SCOPE is a dependent type, then a
22570 SCOPE_REF is returned.
22572 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22573 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22574 was formed. Abstractly, such entities should not be passed to this
22575 function, because they do not need to be looked up, but it is
22576 simpler to check for this special case here, rather than at the
22577 call-sites.
22579 In cases not explicitly covered above, this function returns a
22580 DECL, OVERLOAD, or baselink representing the result of the lookup.
22581 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22582 is returned.
22584 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22585 (e.g., "struct") that was used. In that case bindings that do not
22586 refer to types are ignored.
22588 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22589 ignored.
22591 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22592 are ignored.
22594 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22595 types.
22597 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22598 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22599 NULL_TREE otherwise. */
22601 static tree
22602 cp_parser_lookup_name (cp_parser *parser, tree name,
22603 enum tag_types tag_type,
22604 bool is_template,
22605 bool is_namespace,
22606 bool check_dependency,
22607 tree *ambiguous_decls,
22608 location_t name_location)
22610 tree decl;
22611 tree object_type = parser->context->object_type;
22613 /* Assume that the lookup will be unambiguous. */
22614 if (ambiguous_decls)
22615 *ambiguous_decls = NULL_TREE;
22617 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22618 no longer valid. Note that if we are parsing tentatively, and
22619 the parse fails, OBJECT_TYPE will be automatically restored. */
22620 parser->context->object_type = NULL_TREE;
22622 if (name == error_mark_node)
22623 return error_mark_node;
22625 /* A template-id has already been resolved; there is no lookup to
22626 do. */
22627 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22628 return name;
22629 if (BASELINK_P (name))
22631 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22632 == TEMPLATE_ID_EXPR);
22633 return name;
22636 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22637 it should already have been checked to make sure that the name
22638 used matches the type being destroyed. */
22639 if (TREE_CODE (name) == BIT_NOT_EXPR)
22641 tree type;
22643 /* Figure out to which type this destructor applies. */
22644 if (parser->scope)
22645 type = parser->scope;
22646 else if (object_type)
22647 type = object_type;
22648 else
22649 type = current_class_type;
22650 /* If that's not a class type, there is no destructor. */
22651 if (!type || !CLASS_TYPE_P (type))
22652 return error_mark_node;
22653 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22654 lazily_declare_fn (sfk_destructor, type);
22655 if (!CLASSTYPE_DESTRUCTORS (type))
22656 return error_mark_node;
22657 /* If it was a class type, return the destructor. */
22658 return CLASSTYPE_DESTRUCTORS (type);
22661 /* By this point, the NAME should be an ordinary identifier. If
22662 the id-expression was a qualified name, the qualifying scope is
22663 stored in PARSER->SCOPE at this point. */
22664 gcc_assert (identifier_p (name));
22666 /* Perform the lookup. */
22667 if (parser->scope)
22669 bool dependent_p;
22671 if (parser->scope == error_mark_node)
22672 return error_mark_node;
22674 /* If the SCOPE is dependent, the lookup must be deferred until
22675 the template is instantiated -- unless we are explicitly
22676 looking up names in uninstantiated templates. Even then, we
22677 cannot look up the name if the scope is not a class type; it
22678 might, for example, be a template type parameter. */
22679 dependent_p = (TYPE_P (parser->scope)
22680 && dependent_scope_p (parser->scope));
22681 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22682 && dependent_p)
22683 /* Defer lookup. */
22684 decl = error_mark_node;
22685 else
22687 tree pushed_scope = NULL_TREE;
22689 /* If PARSER->SCOPE is a dependent type, then it must be a
22690 class type, and we must not be checking dependencies;
22691 otherwise, we would have processed this lookup above. So
22692 that PARSER->SCOPE is not considered a dependent base by
22693 lookup_member, we must enter the scope here. */
22694 if (dependent_p)
22695 pushed_scope = push_scope (parser->scope);
22697 /* If the PARSER->SCOPE is a template specialization, it
22698 may be instantiated during name lookup. In that case,
22699 errors may be issued. Even if we rollback the current
22700 tentative parse, those errors are valid. */
22701 decl = lookup_qualified_name (parser->scope, name,
22702 tag_type != none_type,
22703 /*complain=*/true);
22705 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22706 lookup result and the nested-name-specifier nominates a class C:
22707 * if the name specified after the nested-name-specifier, when
22708 looked up in C, is the injected-class-name of C (Clause 9), or
22709 * if the name specified after the nested-name-specifier is the
22710 same as the identifier or the simple-template-id's template-
22711 name in the last component of the nested-name-specifier,
22712 the name is instead considered to name the constructor of
22713 class C. [ Note: for example, the constructor is not an
22714 acceptable lookup result in an elaborated-type-specifier so
22715 the constructor would not be used in place of the
22716 injected-class-name. --end note ] Such a constructor name
22717 shall be used only in the declarator-id of a declaration that
22718 names a constructor or in a using-declaration. */
22719 if (tag_type == none_type
22720 && DECL_SELF_REFERENCE_P (decl)
22721 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22722 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22723 tag_type != none_type,
22724 /*complain=*/true);
22726 /* If we have a single function from a using decl, pull it out. */
22727 if (TREE_CODE (decl) == OVERLOAD
22728 && !really_overloaded_fn (decl))
22729 decl = OVL_FUNCTION (decl);
22731 if (pushed_scope)
22732 pop_scope (pushed_scope);
22735 /* If the scope is a dependent type and either we deferred lookup or
22736 we did lookup but didn't find the name, rememeber the name. */
22737 if (decl == error_mark_node && TYPE_P (parser->scope)
22738 && dependent_type_p (parser->scope))
22740 if (tag_type)
22742 tree type;
22744 /* The resolution to Core Issue 180 says that `struct
22745 A::B' should be considered a type-name, even if `A'
22746 is dependent. */
22747 type = make_typename_type (parser->scope, name, tag_type,
22748 /*complain=*/tf_error);
22749 if (type != error_mark_node)
22750 decl = TYPE_NAME (type);
22752 else if (is_template
22753 && (cp_parser_next_token_ends_template_argument_p (parser)
22754 || cp_lexer_next_token_is (parser->lexer,
22755 CPP_CLOSE_PAREN)))
22756 decl = make_unbound_class_template (parser->scope,
22757 name, NULL_TREE,
22758 /*complain=*/tf_error);
22759 else
22760 decl = build_qualified_name (/*type=*/NULL_TREE,
22761 parser->scope, name,
22762 is_template);
22764 parser->qualifying_scope = parser->scope;
22765 parser->object_scope = NULL_TREE;
22767 else if (object_type)
22769 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22770 OBJECT_TYPE is not a class. */
22771 if (CLASS_TYPE_P (object_type))
22772 /* If the OBJECT_TYPE is a template specialization, it may
22773 be instantiated during name lookup. In that case, errors
22774 may be issued. Even if we rollback the current tentative
22775 parse, those errors are valid. */
22776 decl = lookup_member (object_type,
22777 name,
22778 /*protect=*/0,
22779 tag_type != none_type,
22780 tf_warning_or_error);
22781 else
22782 decl = NULL_TREE;
22784 if (!decl)
22785 /* Look it up in the enclosing context. */
22786 decl = lookup_name_real (name, tag_type != none_type,
22787 /*nonclass=*/0,
22788 /*block_p=*/true, is_namespace, 0);
22789 parser->object_scope = object_type;
22790 parser->qualifying_scope = NULL_TREE;
22792 else
22794 decl = lookup_name_real (name, tag_type != none_type,
22795 /*nonclass=*/0,
22796 /*block_p=*/true, is_namespace, 0);
22797 parser->qualifying_scope = NULL_TREE;
22798 parser->object_scope = NULL_TREE;
22801 /* If the lookup failed, let our caller know. */
22802 if (!decl || decl == error_mark_node)
22803 return error_mark_node;
22805 /* Pull out the template from an injected-class-name (or multiple). */
22806 if (is_template)
22807 decl = maybe_get_template_decl_from_type_decl (decl);
22809 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22810 if (TREE_CODE (decl) == TREE_LIST)
22812 if (ambiguous_decls)
22813 *ambiguous_decls = decl;
22814 /* The error message we have to print is too complicated for
22815 cp_parser_error, so we incorporate its actions directly. */
22816 if (!cp_parser_simulate_error (parser))
22818 error_at (name_location, "reference to %qD is ambiguous",
22819 name);
22820 print_candidates (decl);
22822 return error_mark_node;
22825 gcc_assert (DECL_P (decl)
22826 || TREE_CODE (decl) == OVERLOAD
22827 || TREE_CODE (decl) == SCOPE_REF
22828 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22829 || BASELINK_P (decl));
22831 /* If we have resolved the name of a member declaration, check to
22832 see if the declaration is accessible. When the name resolves to
22833 set of overloaded functions, accessibility is checked when
22834 overload resolution is done.
22836 During an explicit instantiation, access is not checked at all,
22837 as per [temp.explicit]. */
22838 if (DECL_P (decl))
22839 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22841 maybe_record_typedef_use (decl);
22843 return decl;
22846 /* Like cp_parser_lookup_name, but for use in the typical case where
22847 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22848 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22850 static tree
22851 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22853 return cp_parser_lookup_name (parser, name,
22854 none_type,
22855 /*is_template=*/false,
22856 /*is_namespace=*/false,
22857 /*check_dependency=*/true,
22858 /*ambiguous_decls=*/NULL,
22859 location);
22862 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22863 the current context, return the TYPE_DECL. If TAG_NAME_P is
22864 true, the DECL indicates the class being defined in a class-head,
22865 or declared in an elaborated-type-specifier.
22867 Otherwise, return DECL. */
22869 static tree
22870 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22872 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22873 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22875 struct A {
22876 template <typename T> struct B;
22879 template <typename T> struct A::B {};
22881 Similarly, in an elaborated-type-specifier:
22883 namespace N { struct X{}; }
22885 struct A {
22886 template <typename T> friend struct N::X;
22889 However, if the DECL refers to a class type, and we are in
22890 the scope of the class, then the name lookup automatically
22891 finds the TYPE_DECL created by build_self_reference rather
22892 than a TEMPLATE_DECL. For example, in:
22894 template <class T> struct S {
22895 S s;
22898 there is no need to handle such case. */
22900 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22901 return DECL_TEMPLATE_RESULT (decl);
22903 return decl;
22906 /* If too many, or too few, template-parameter lists apply to the
22907 declarator, issue an error message. Returns TRUE if all went well,
22908 and FALSE otherwise. */
22910 static bool
22911 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22912 cp_declarator *declarator,
22913 location_t declarator_location)
22915 switch (declarator->kind)
22917 case cdk_id:
22919 unsigned num_templates = 0;
22920 tree scope = declarator->u.id.qualifying_scope;
22922 if (scope)
22923 num_templates = num_template_headers_for_class (scope);
22924 else if (TREE_CODE (declarator->u.id.unqualified_name)
22925 == TEMPLATE_ID_EXPR)
22926 /* If the DECLARATOR has the form `X<y>' then it uses one
22927 additional level of template parameters. */
22928 ++num_templates;
22930 return cp_parser_check_template_parameters
22931 (parser, num_templates, declarator_location, declarator);
22934 case cdk_function:
22935 case cdk_array:
22936 case cdk_pointer:
22937 case cdk_reference:
22938 case cdk_ptrmem:
22939 return (cp_parser_check_declarator_template_parameters
22940 (parser, declarator->declarator, declarator_location));
22942 case cdk_error:
22943 return true;
22945 default:
22946 gcc_unreachable ();
22948 return false;
22951 /* NUM_TEMPLATES were used in the current declaration. If that is
22952 invalid, return FALSE and issue an error messages. Otherwise,
22953 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22954 declarator and we can print more accurate diagnostics. */
22956 static bool
22957 cp_parser_check_template_parameters (cp_parser* parser,
22958 unsigned num_templates,
22959 location_t location,
22960 cp_declarator *declarator)
22962 /* If there are the same number of template classes and parameter
22963 lists, that's OK. */
22964 if (parser->num_template_parameter_lists == num_templates)
22965 return true;
22966 /* If there are more, but only one more, then we are referring to a
22967 member template. That's OK too. */
22968 if (parser->num_template_parameter_lists == num_templates + 1)
22969 return true;
22970 /* If there are more template classes than parameter lists, we have
22971 something like:
22973 template <class T> void S<T>::R<T>::f (); */
22974 if (parser->num_template_parameter_lists < num_templates)
22976 if (declarator && !current_function_decl)
22977 error_at (location, "specializing member %<%T::%E%> "
22978 "requires %<template<>%> syntax",
22979 declarator->u.id.qualifying_scope,
22980 declarator->u.id.unqualified_name);
22981 else if (declarator)
22982 error_at (location, "invalid declaration of %<%T::%E%>",
22983 declarator->u.id.qualifying_scope,
22984 declarator->u.id.unqualified_name);
22985 else
22986 error_at (location, "too few template-parameter-lists");
22987 return false;
22989 /* Otherwise, there are too many template parameter lists. We have
22990 something like:
22992 template <class T> template <class U> void S::f(); */
22993 error_at (location, "too many template-parameter-lists");
22994 return false;
22997 /* Parse an optional `::' token indicating that the following name is
22998 from the global namespace. If so, PARSER->SCOPE is set to the
22999 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23000 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23001 Returns the new value of PARSER->SCOPE, if the `::' token is
23002 present, and NULL_TREE otherwise. */
23004 static tree
23005 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23007 cp_token *token;
23009 /* Peek at the next token. */
23010 token = cp_lexer_peek_token (parser->lexer);
23011 /* If we're looking at a `::' token then we're starting from the
23012 global namespace, not our current location. */
23013 if (token->type == CPP_SCOPE)
23015 /* Consume the `::' token. */
23016 cp_lexer_consume_token (parser->lexer);
23017 /* Set the SCOPE so that we know where to start the lookup. */
23018 parser->scope = global_namespace;
23019 parser->qualifying_scope = global_namespace;
23020 parser->object_scope = NULL_TREE;
23022 return parser->scope;
23024 else if (!current_scope_valid_p)
23026 parser->scope = NULL_TREE;
23027 parser->qualifying_scope = NULL_TREE;
23028 parser->object_scope = NULL_TREE;
23031 return NULL_TREE;
23034 /* Returns TRUE if the upcoming token sequence is the start of a
23035 constructor declarator. If FRIEND_P is true, the declarator is
23036 preceded by the `friend' specifier. */
23038 static bool
23039 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23041 bool constructor_p;
23042 bool outside_class_specifier_p;
23043 tree nested_name_specifier;
23044 cp_token *next_token;
23046 /* The common case is that this is not a constructor declarator, so
23047 try to avoid doing lots of work if at all possible. It's not
23048 valid declare a constructor at function scope. */
23049 if (parser->in_function_body)
23050 return false;
23051 /* And only certain tokens can begin a constructor declarator. */
23052 next_token = cp_lexer_peek_token (parser->lexer);
23053 if (next_token->type != CPP_NAME
23054 && next_token->type != CPP_SCOPE
23055 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23056 && next_token->type != CPP_TEMPLATE_ID)
23057 return false;
23059 /* Parse tentatively; we are going to roll back all of the tokens
23060 consumed here. */
23061 cp_parser_parse_tentatively (parser);
23062 /* Assume that we are looking at a constructor declarator. */
23063 constructor_p = true;
23065 /* Look for the optional `::' operator. */
23066 cp_parser_global_scope_opt (parser,
23067 /*current_scope_valid_p=*/false);
23068 /* Look for the nested-name-specifier. */
23069 nested_name_specifier
23070 = (cp_parser_nested_name_specifier_opt (parser,
23071 /*typename_keyword_p=*/false,
23072 /*check_dependency_p=*/false,
23073 /*type_p=*/false,
23074 /*is_declaration=*/false));
23076 outside_class_specifier_p = (!at_class_scope_p ()
23077 || !TYPE_BEING_DEFINED (current_class_type)
23078 || friend_p);
23080 /* Outside of a class-specifier, there must be a
23081 nested-name-specifier. */
23082 if (!nested_name_specifier && outside_class_specifier_p)
23083 constructor_p = false;
23084 else if (nested_name_specifier == error_mark_node)
23085 constructor_p = false;
23087 /* If we have a class scope, this is easy; DR 147 says that S::S always
23088 names the constructor, and no other qualified name could. */
23089 if (constructor_p && nested_name_specifier
23090 && CLASS_TYPE_P (nested_name_specifier))
23092 tree id = cp_parser_unqualified_id (parser,
23093 /*template_keyword_p=*/false,
23094 /*check_dependency_p=*/false,
23095 /*declarator_p=*/true,
23096 /*optional_p=*/false);
23097 if (is_overloaded_fn (id))
23098 id = DECL_NAME (get_first_fn (id));
23099 if (!constructor_name_p (id, nested_name_specifier))
23100 constructor_p = false;
23102 /* If we still think that this might be a constructor-declarator,
23103 look for a class-name. */
23104 else if (constructor_p)
23106 /* If we have:
23108 template <typename T> struct S {
23109 S();
23112 we must recognize that the nested `S' names a class. */
23113 tree type_decl;
23114 type_decl = cp_parser_class_name (parser,
23115 /*typename_keyword_p=*/false,
23116 /*template_keyword_p=*/false,
23117 none_type,
23118 /*check_dependency_p=*/false,
23119 /*class_head_p=*/false,
23120 /*is_declaration=*/false);
23121 /* If there was no class-name, then this is not a constructor.
23122 Otherwise, if we are in a class-specifier and we aren't
23123 handling a friend declaration, check that its type matches
23124 current_class_type (c++/38313). Note: error_mark_node
23125 is left alone for error recovery purposes. */
23126 constructor_p = (!cp_parser_error_occurred (parser)
23127 && (outside_class_specifier_p
23128 || type_decl == error_mark_node
23129 || same_type_p (current_class_type,
23130 TREE_TYPE (type_decl))));
23132 /* If we're still considering a constructor, we have to see a `(',
23133 to begin the parameter-declaration-clause, followed by either a
23134 `)', an `...', or a decl-specifier. We need to check for a
23135 type-specifier to avoid being fooled into thinking that:
23137 S (f) (int);
23139 is a constructor. (It is actually a function named `f' that
23140 takes one parameter (of type `int') and returns a value of type
23141 `S'. */
23142 if (constructor_p
23143 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23144 constructor_p = false;
23146 if (constructor_p
23147 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23148 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23149 /* A parameter declaration begins with a decl-specifier,
23150 which is either the "attribute" keyword, a storage class
23151 specifier, or (usually) a type-specifier. */
23152 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23154 tree type;
23155 tree pushed_scope = NULL_TREE;
23156 unsigned saved_num_template_parameter_lists;
23158 /* Names appearing in the type-specifier should be looked up
23159 in the scope of the class. */
23160 if (current_class_type)
23161 type = NULL_TREE;
23162 else
23164 type = TREE_TYPE (type_decl);
23165 if (TREE_CODE (type) == TYPENAME_TYPE)
23167 type = resolve_typename_type (type,
23168 /*only_current_p=*/false);
23169 if (TREE_CODE (type) == TYPENAME_TYPE)
23171 cp_parser_abort_tentative_parse (parser);
23172 return false;
23175 pushed_scope = push_scope (type);
23178 /* Inside the constructor parameter list, surrounding
23179 template-parameter-lists do not apply. */
23180 saved_num_template_parameter_lists
23181 = parser->num_template_parameter_lists;
23182 parser->num_template_parameter_lists = 0;
23184 /* Look for the type-specifier. */
23185 cp_parser_type_specifier (parser,
23186 CP_PARSER_FLAGS_NONE,
23187 /*decl_specs=*/NULL,
23188 /*is_declarator=*/true,
23189 /*declares_class_or_enum=*/NULL,
23190 /*is_cv_qualifier=*/NULL);
23192 parser->num_template_parameter_lists
23193 = saved_num_template_parameter_lists;
23195 /* Leave the scope of the class. */
23196 if (pushed_scope)
23197 pop_scope (pushed_scope);
23199 constructor_p = !cp_parser_error_occurred (parser);
23203 /* We did not really want to consume any tokens. */
23204 cp_parser_abort_tentative_parse (parser);
23206 return constructor_p;
23209 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23210 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23211 they must be performed once we are in the scope of the function.
23213 Returns the function defined. */
23215 static tree
23216 cp_parser_function_definition_from_specifiers_and_declarator
23217 (cp_parser* parser,
23218 cp_decl_specifier_seq *decl_specifiers,
23219 tree attributes,
23220 const cp_declarator *declarator)
23222 tree fn;
23223 bool success_p;
23225 /* Begin the function-definition. */
23226 success_p = start_function (decl_specifiers, declarator, attributes);
23228 /* The things we're about to see are not directly qualified by any
23229 template headers we've seen thus far. */
23230 reset_specialization ();
23232 /* If there were names looked up in the decl-specifier-seq that we
23233 did not check, check them now. We must wait until we are in the
23234 scope of the function to perform the checks, since the function
23235 might be a friend. */
23236 perform_deferred_access_checks (tf_warning_or_error);
23238 if (success_p)
23240 cp_finalize_omp_declare_simd (parser, current_function_decl);
23241 parser->omp_declare_simd = NULL;
23244 if (!success_p)
23246 /* Skip the entire function. */
23247 cp_parser_skip_to_end_of_block_or_statement (parser);
23248 fn = error_mark_node;
23250 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23252 /* Seen already, skip it. An error message has already been output. */
23253 cp_parser_skip_to_end_of_block_or_statement (parser);
23254 fn = current_function_decl;
23255 current_function_decl = NULL_TREE;
23256 /* If this is a function from a class, pop the nested class. */
23257 if (current_class_name)
23258 pop_nested_class ();
23260 else
23262 timevar_id_t tv;
23263 if (DECL_DECLARED_INLINE_P (current_function_decl))
23264 tv = TV_PARSE_INLINE;
23265 else
23266 tv = TV_PARSE_FUNC;
23267 timevar_push (tv);
23268 fn = cp_parser_function_definition_after_declarator (parser,
23269 /*inline_p=*/false);
23270 timevar_pop (tv);
23273 return fn;
23276 /* Parse the part of a function-definition that follows the
23277 declarator. INLINE_P is TRUE iff this function is an inline
23278 function defined within a class-specifier.
23280 Returns the function defined. */
23282 static tree
23283 cp_parser_function_definition_after_declarator (cp_parser* parser,
23284 bool inline_p)
23286 tree fn;
23287 bool ctor_initializer_p = false;
23288 bool saved_in_unbraced_linkage_specification_p;
23289 bool saved_in_function_body;
23290 unsigned saved_num_template_parameter_lists;
23291 cp_token *token;
23292 bool fully_implicit_function_template_p
23293 = parser->fully_implicit_function_template_p;
23294 parser->fully_implicit_function_template_p = false;
23295 tree implicit_template_parms
23296 = parser->implicit_template_parms;
23297 parser->implicit_template_parms = 0;
23298 cp_binding_level* implicit_template_scope
23299 = parser->implicit_template_scope;
23300 parser->implicit_template_scope = 0;
23302 saved_in_function_body = parser->in_function_body;
23303 parser->in_function_body = true;
23304 /* If the next token is `return', then the code may be trying to
23305 make use of the "named return value" extension that G++ used to
23306 support. */
23307 token = cp_lexer_peek_token (parser->lexer);
23308 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23310 /* Consume the `return' keyword. */
23311 cp_lexer_consume_token (parser->lexer);
23312 /* Look for the identifier that indicates what value is to be
23313 returned. */
23314 cp_parser_identifier (parser);
23315 /* Issue an error message. */
23316 error_at (token->location,
23317 "named return values are no longer supported");
23318 /* Skip tokens until we reach the start of the function body. */
23319 while (true)
23321 cp_token *token = cp_lexer_peek_token (parser->lexer);
23322 if (token->type == CPP_OPEN_BRACE
23323 || token->type == CPP_EOF
23324 || token->type == CPP_PRAGMA_EOL)
23325 break;
23326 cp_lexer_consume_token (parser->lexer);
23329 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23330 anything declared inside `f'. */
23331 saved_in_unbraced_linkage_specification_p
23332 = parser->in_unbraced_linkage_specification_p;
23333 parser->in_unbraced_linkage_specification_p = false;
23334 /* Inside the function, surrounding template-parameter-lists do not
23335 apply. */
23336 saved_num_template_parameter_lists
23337 = parser->num_template_parameter_lists;
23338 parser->num_template_parameter_lists = 0;
23340 start_lambda_scope (current_function_decl);
23342 /* If the next token is `try', `__transaction_atomic', or
23343 `__transaction_relaxed`, then we are looking at either function-try-block
23344 or function-transaction-block. Note that all of these include the
23345 function-body. */
23346 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23347 ctor_initializer_p = cp_parser_function_transaction (parser,
23348 RID_TRANSACTION_ATOMIC);
23349 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23350 RID_TRANSACTION_RELAXED))
23351 ctor_initializer_p = cp_parser_function_transaction (parser,
23352 RID_TRANSACTION_RELAXED);
23353 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23354 ctor_initializer_p = cp_parser_function_try_block (parser);
23355 else
23356 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23357 (parser, /*in_function_try_block=*/false);
23359 finish_lambda_scope ();
23361 /* Finish the function. */
23362 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23363 (inline_p ? 2 : 0));
23364 /* Generate code for it, if necessary. */
23365 expand_or_defer_fn (fn);
23366 /* Restore the saved values. */
23367 parser->in_unbraced_linkage_specification_p
23368 = saved_in_unbraced_linkage_specification_p;
23369 parser->num_template_parameter_lists
23370 = saved_num_template_parameter_lists;
23371 parser->in_function_body = saved_in_function_body;
23373 parser->fully_implicit_function_template_p
23374 = fully_implicit_function_template_p;
23375 parser->implicit_template_parms
23376 = implicit_template_parms;
23377 parser->implicit_template_scope
23378 = implicit_template_scope;
23380 if (parser->fully_implicit_function_template_p)
23381 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23383 return fn;
23386 /* Parse a template-declaration, assuming that the `export' (and
23387 `extern') keywords, if present, has already been scanned. MEMBER_P
23388 is as for cp_parser_template_declaration. */
23390 static void
23391 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23393 tree decl = NULL_TREE;
23394 vec<deferred_access_check, va_gc> *checks;
23395 tree parameter_list;
23396 bool friend_p = false;
23397 bool need_lang_pop;
23398 cp_token *token;
23400 /* Look for the `template' keyword. */
23401 token = cp_lexer_peek_token (parser->lexer);
23402 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23403 return;
23405 /* And the `<'. */
23406 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23407 return;
23408 if (at_class_scope_p () && current_function_decl)
23410 /* 14.5.2.2 [temp.mem]
23412 A local class shall not have member templates. */
23413 error_at (token->location,
23414 "invalid declaration of member template in local class");
23415 cp_parser_skip_to_end_of_block_or_statement (parser);
23416 return;
23418 /* [temp]
23420 A template ... shall not have C linkage. */
23421 if (current_lang_name == lang_name_c)
23423 error_at (token->location, "template with C linkage");
23424 /* Give it C++ linkage to avoid confusing other parts of the
23425 front end. */
23426 push_lang_context (lang_name_cplusplus);
23427 need_lang_pop = true;
23429 else
23430 need_lang_pop = false;
23432 /* We cannot perform access checks on the template parameter
23433 declarations until we know what is being declared, just as we
23434 cannot check the decl-specifier list. */
23435 push_deferring_access_checks (dk_deferred);
23437 /* If the next token is `>', then we have an invalid
23438 specialization. Rather than complain about an invalid template
23439 parameter, issue an error message here. */
23440 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23442 cp_parser_error (parser, "invalid explicit specialization");
23443 begin_specialization ();
23444 parameter_list = NULL_TREE;
23446 else
23448 /* Parse the template parameters. */
23449 parameter_list = cp_parser_template_parameter_list (parser);
23452 /* Get the deferred access checks from the parameter list. These
23453 will be checked once we know what is being declared, as for a
23454 member template the checks must be performed in the scope of the
23455 class containing the member. */
23456 checks = get_deferred_access_checks ();
23458 /* Look for the `>'. */
23459 cp_parser_skip_to_end_of_template_parameter_list (parser);
23460 /* We just processed one more parameter list. */
23461 ++parser->num_template_parameter_lists;
23462 /* If the next token is `template', there are more template
23463 parameters. */
23464 if (cp_lexer_next_token_is_keyword (parser->lexer,
23465 RID_TEMPLATE))
23466 cp_parser_template_declaration_after_export (parser, member_p);
23467 else if (cxx_dialect >= cxx11
23468 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23469 decl = cp_parser_alias_declaration (parser);
23470 else
23472 /* There are no access checks when parsing a template, as we do not
23473 know if a specialization will be a friend. */
23474 push_deferring_access_checks (dk_no_check);
23475 token = cp_lexer_peek_token (parser->lexer);
23476 decl = cp_parser_single_declaration (parser,
23477 checks,
23478 member_p,
23479 /*explicit_specialization_p=*/false,
23480 &friend_p);
23481 pop_deferring_access_checks ();
23483 /* If this is a member template declaration, let the front
23484 end know. */
23485 if (member_p && !friend_p && decl)
23487 if (TREE_CODE (decl) == TYPE_DECL)
23488 cp_parser_check_access_in_redeclaration (decl, token->location);
23490 decl = finish_member_template_decl (decl);
23492 else if (friend_p && decl
23493 && DECL_DECLARES_TYPE_P (decl))
23494 make_friend_class (current_class_type, TREE_TYPE (decl),
23495 /*complain=*/true);
23497 /* We are done with the current parameter list. */
23498 --parser->num_template_parameter_lists;
23500 pop_deferring_access_checks ();
23502 /* Finish up. */
23503 finish_template_decl (parameter_list);
23505 /* Check the template arguments for a literal operator template. */
23506 if (decl
23507 && DECL_DECLARES_FUNCTION_P (decl)
23508 && UDLIT_OPER_P (DECL_NAME (decl)))
23510 bool ok = true;
23511 if (parameter_list == NULL_TREE)
23512 ok = false;
23513 else
23515 int num_parms = TREE_VEC_LENGTH (parameter_list);
23516 if (num_parms == 1)
23518 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23519 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23520 if (TREE_TYPE (parm) != char_type_node
23521 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23522 ok = false;
23524 else if (num_parms == 2 && cxx_dialect >= cxx14)
23526 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23527 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23528 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23529 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23530 if (TREE_TYPE (parm) != TREE_TYPE (type)
23531 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23532 ok = false;
23534 else
23535 ok = false;
23537 if (!ok)
23539 if (cxx_dialect >= cxx14)
23540 error ("literal operator template %qD has invalid parameter list."
23541 " Expected non-type template argument pack <char...>"
23542 " or <typename CharT, CharT...>",
23543 decl);
23544 else
23545 error ("literal operator template %qD has invalid parameter list."
23546 " Expected non-type template argument pack <char...>",
23547 decl);
23550 /* Register member declarations. */
23551 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23552 finish_member_declaration (decl);
23553 /* For the erroneous case of a template with C linkage, we pushed an
23554 implicit C++ linkage scope; exit that scope now. */
23555 if (need_lang_pop)
23556 pop_lang_context ();
23557 /* If DECL is a function template, we must return to parse it later.
23558 (Even though there is no definition, there might be default
23559 arguments that need handling.) */
23560 if (member_p && decl
23561 && DECL_DECLARES_FUNCTION_P (decl))
23562 vec_safe_push (unparsed_funs_with_definitions, decl);
23565 /* Perform the deferred access checks from a template-parameter-list.
23566 CHECKS is a TREE_LIST of access checks, as returned by
23567 get_deferred_access_checks. */
23569 static void
23570 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23572 ++processing_template_parmlist;
23573 perform_access_checks (checks, tf_warning_or_error);
23574 --processing_template_parmlist;
23577 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23578 `function-definition' sequence that follows a template header.
23579 If MEMBER_P is true, this declaration appears in a class scope.
23581 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23582 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23584 static tree
23585 cp_parser_single_declaration (cp_parser* parser,
23586 vec<deferred_access_check, va_gc> *checks,
23587 bool member_p,
23588 bool explicit_specialization_p,
23589 bool* friend_p)
23591 int declares_class_or_enum;
23592 tree decl = NULL_TREE;
23593 cp_decl_specifier_seq decl_specifiers;
23594 bool function_definition_p = false;
23595 cp_token *decl_spec_token_start;
23597 /* This function is only used when processing a template
23598 declaration. */
23599 gcc_assert (innermost_scope_kind () == sk_template_parms
23600 || innermost_scope_kind () == sk_template_spec);
23602 /* Defer access checks until we know what is being declared. */
23603 push_deferring_access_checks (dk_deferred);
23605 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23606 alternative. */
23607 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23608 cp_parser_decl_specifier_seq (parser,
23609 CP_PARSER_FLAGS_OPTIONAL,
23610 &decl_specifiers,
23611 &declares_class_or_enum);
23612 if (friend_p)
23613 *friend_p = cp_parser_friend_p (&decl_specifiers);
23615 /* There are no template typedefs. */
23616 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23618 error_at (decl_spec_token_start->location,
23619 "template declaration of %<typedef%>");
23620 decl = error_mark_node;
23623 /* Gather up the access checks that occurred the
23624 decl-specifier-seq. */
23625 stop_deferring_access_checks ();
23627 /* Check for the declaration of a template class. */
23628 if (declares_class_or_enum)
23630 if (cp_parser_declares_only_class_p (parser))
23632 decl = shadow_tag (&decl_specifiers);
23634 /* In this case:
23636 struct C {
23637 friend template <typename T> struct A<T>::B;
23640 A<T>::B will be represented by a TYPENAME_TYPE, and
23641 therefore not recognized by shadow_tag. */
23642 if (friend_p && *friend_p
23643 && !decl
23644 && decl_specifiers.type
23645 && TYPE_P (decl_specifiers.type))
23646 decl = decl_specifiers.type;
23648 if (decl && decl != error_mark_node)
23649 decl = TYPE_NAME (decl);
23650 else
23651 decl = error_mark_node;
23653 /* Perform access checks for template parameters. */
23654 cp_parser_perform_template_parameter_access_checks (checks);
23658 /* Complain about missing 'typename' or other invalid type names. */
23659 if (!decl_specifiers.any_type_specifiers_p
23660 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23662 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23663 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23664 the rest of this declaration. */
23665 decl = error_mark_node;
23666 goto out;
23669 /* If it's not a template class, try for a template function. If
23670 the next token is a `;', then this declaration does not declare
23671 anything. But, if there were errors in the decl-specifiers, then
23672 the error might well have come from an attempted class-specifier.
23673 In that case, there's no need to warn about a missing declarator. */
23674 if (!decl
23675 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23676 || decl_specifiers.type != error_mark_node))
23678 decl = cp_parser_init_declarator (parser,
23679 &decl_specifiers,
23680 checks,
23681 /*function_definition_allowed_p=*/true,
23682 member_p,
23683 declares_class_or_enum,
23684 &function_definition_p,
23685 NULL);
23687 /* 7.1.1-1 [dcl.stc]
23689 A storage-class-specifier shall not be specified in an explicit
23690 specialization... */
23691 if (decl
23692 && explicit_specialization_p
23693 && decl_specifiers.storage_class != sc_none)
23695 error_at (decl_spec_token_start->location,
23696 "explicit template specialization cannot have a storage class");
23697 decl = error_mark_node;
23700 if (decl && VAR_P (decl))
23701 check_template_variable (decl);
23704 /* Look for a trailing `;' after the declaration. */
23705 if (!function_definition_p
23706 && (decl == error_mark_node
23707 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23708 cp_parser_skip_to_end_of_block_or_statement (parser);
23710 out:
23711 pop_deferring_access_checks ();
23713 /* Clear any current qualification; whatever comes next is the start
23714 of something new. */
23715 parser->scope = NULL_TREE;
23716 parser->qualifying_scope = NULL_TREE;
23717 parser->object_scope = NULL_TREE;
23719 return decl;
23722 /* Parse a cast-expression that is not the operand of a unary "&". */
23724 static tree
23725 cp_parser_simple_cast_expression (cp_parser *parser)
23727 return cp_parser_cast_expression (parser, /*address_p=*/false,
23728 /*cast_p=*/false, /*decltype*/false, NULL);
23731 /* Parse a functional cast to TYPE. Returns an expression
23732 representing the cast. */
23734 static tree
23735 cp_parser_functional_cast (cp_parser* parser, tree type)
23737 vec<tree, va_gc> *vec;
23738 tree expression_list;
23739 tree cast;
23740 bool nonconst_p;
23742 if (!type)
23743 type = error_mark_node;
23745 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23747 cp_lexer_set_source_position (parser->lexer);
23748 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23749 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23750 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23751 if (TREE_CODE (type) == TYPE_DECL)
23752 type = TREE_TYPE (type);
23753 return finish_compound_literal (type, expression_list,
23754 tf_warning_or_error);
23758 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23759 /*cast_p=*/true,
23760 /*allow_expansion_p=*/true,
23761 /*non_constant_p=*/NULL);
23762 if (vec == NULL)
23763 expression_list = error_mark_node;
23764 else
23766 expression_list = build_tree_list_vec (vec);
23767 release_tree_vector (vec);
23770 cast = build_functional_cast (type, expression_list,
23771 tf_warning_or_error);
23772 /* [expr.const]/1: In an integral constant expression "only type
23773 conversions to integral or enumeration type can be used". */
23774 if (TREE_CODE (type) == TYPE_DECL)
23775 type = TREE_TYPE (type);
23776 if (cast != error_mark_node
23777 && !cast_valid_in_integral_constant_expression_p (type)
23778 && cp_parser_non_integral_constant_expression (parser,
23779 NIC_CONSTRUCTOR))
23780 return error_mark_node;
23781 return cast;
23784 /* Save the tokens that make up the body of a member function defined
23785 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23786 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23787 specifiers applied to the declaration. Returns the FUNCTION_DECL
23788 for the member function. */
23790 static tree
23791 cp_parser_save_member_function_body (cp_parser* parser,
23792 cp_decl_specifier_seq *decl_specifiers,
23793 cp_declarator *declarator,
23794 tree attributes)
23796 cp_token *first;
23797 cp_token *last;
23798 tree fn;
23800 /* Create the FUNCTION_DECL. */
23801 fn = grokmethod (decl_specifiers, declarator, attributes);
23802 cp_finalize_omp_declare_simd (parser, fn);
23803 /* If something went badly wrong, bail out now. */
23804 if (fn == error_mark_node)
23806 /* If there's a function-body, skip it. */
23807 if (cp_parser_token_starts_function_definition_p
23808 (cp_lexer_peek_token (parser->lexer)))
23809 cp_parser_skip_to_end_of_block_or_statement (parser);
23810 return error_mark_node;
23813 /* Remember it, if there default args to post process. */
23814 cp_parser_save_default_args (parser, fn);
23816 /* Save away the tokens that make up the body of the
23817 function. */
23818 first = parser->lexer->next_token;
23819 /* Handle function try blocks. */
23820 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23821 cp_lexer_consume_token (parser->lexer);
23822 /* We can have braced-init-list mem-initializers before the fn body. */
23823 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23825 cp_lexer_consume_token (parser->lexer);
23826 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23828 /* cache_group will stop after an un-nested { } pair, too. */
23829 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23830 break;
23832 /* variadic mem-inits have ... after the ')'. */
23833 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23834 cp_lexer_consume_token (parser->lexer);
23837 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23838 /* Handle function try blocks. */
23839 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23840 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23841 last = parser->lexer->next_token;
23843 /* Save away the inline definition; we will process it when the
23844 class is complete. */
23845 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23846 DECL_PENDING_INLINE_P (fn) = 1;
23848 /* We need to know that this was defined in the class, so that
23849 friend templates are handled correctly. */
23850 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23852 /* Add FN to the queue of functions to be parsed later. */
23853 vec_safe_push (unparsed_funs_with_definitions, fn);
23855 return fn;
23858 /* Save the tokens that make up the in-class initializer for a non-static
23859 data member. Returns a DEFAULT_ARG. */
23861 static tree
23862 cp_parser_save_nsdmi (cp_parser* parser)
23864 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23867 /* Parse a template-argument-list, as well as the trailing ">" (but
23868 not the opening "<"). See cp_parser_template_argument_list for the
23869 return value. */
23871 static tree
23872 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23874 tree arguments;
23875 tree saved_scope;
23876 tree saved_qualifying_scope;
23877 tree saved_object_scope;
23878 bool saved_greater_than_is_operator_p;
23879 int saved_unevaluated_operand;
23880 int saved_inhibit_evaluation_warnings;
23882 /* [temp.names]
23884 When parsing a template-id, the first non-nested `>' is taken as
23885 the end of the template-argument-list rather than a greater-than
23886 operator. */
23887 saved_greater_than_is_operator_p
23888 = parser->greater_than_is_operator_p;
23889 parser->greater_than_is_operator_p = false;
23890 /* Parsing the argument list may modify SCOPE, so we save it
23891 here. */
23892 saved_scope = parser->scope;
23893 saved_qualifying_scope = parser->qualifying_scope;
23894 saved_object_scope = parser->object_scope;
23895 /* We need to evaluate the template arguments, even though this
23896 template-id may be nested within a "sizeof". */
23897 saved_unevaluated_operand = cp_unevaluated_operand;
23898 cp_unevaluated_operand = 0;
23899 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23900 c_inhibit_evaluation_warnings = 0;
23901 /* Parse the template-argument-list itself. */
23902 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23903 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23904 arguments = NULL_TREE;
23905 else
23906 arguments = cp_parser_template_argument_list (parser);
23907 /* Look for the `>' that ends the template-argument-list. If we find
23908 a '>>' instead, it's probably just a typo. */
23909 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23911 if (cxx_dialect != cxx98)
23913 /* In C++0x, a `>>' in a template argument list or cast
23914 expression is considered to be two separate `>'
23915 tokens. So, change the current token to a `>', but don't
23916 consume it: it will be consumed later when the outer
23917 template argument list (or cast expression) is parsed.
23918 Note that this replacement of `>' for `>>' is necessary
23919 even if we are parsing tentatively: in the tentative
23920 case, after calling
23921 cp_parser_enclosed_template_argument_list we will always
23922 throw away all of the template arguments and the first
23923 closing `>', either because the template argument list
23924 was erroneous or because we are replacing those tokens
23925 with a CPP_TEMPLATE_ID token. The second `>' (which will
23926 not have been thrown away) is needed either to close an
23927 outer template argument list or to complete a new-style
23928 cast. */
23929 cp_token *token = cp_lexer_peek_token (parser->lexer);
23930 token->type = CPP_GREATER;
23932 else if (!saved_greater_than_is_operator_p)
23934 /* If we're in a nested template argument list, the '>>' has
23935 to be a typo for '> >'. We emit the error message, but we
23936 continue parsing and we push a '>' as next token, so that
23937 the argument list will be parsed correctly. Note that the
23938 global source location is still on the token before the
23939 '>>', so we need to say explicitly where we want it. */
23940 cp_token *token = cp_lexer_peek_token (parser->lexer);
23941 error_at (token->location, "%<>>%> should be %<> >%> "
23942 "within a nested template argument list");
23944 token->type = CPP_GREATER;
23946 else
23948 /* If this is not a nested template argument list, the '>>'
23949 is a typo for '>'. Emit an error message and continue.
23950 Same deal about the token location, but here we can get it
23951 right by consuming the '>>' before issuing the diagnostic. */
23952 cp_token *token = cp_lexer_consume_token (parser->lexer);
23953 error_at (token->location,
23954 "spurious %<>>%>, use %<>%> to terminate "
23955 "a template argument list");
23958 else
23959 cp_parser_skip_to_end_of_template_parameter_list (parser);
23960 /* The `>' token might be a greater-than operator again now. */
23961 parser->greater_than_is_operator_p
23962 = saved_greater_than_is_operator_p;
23963 /* Restore the SAVED_SCOPE. */
23964 parser->scope = saved_scope;
23965 parser->qualifying_scope = saved_qualifying_scope;
23966 parser->object_scope = saved_object_scope;
23967 cp_unevaluated_operand = saved_unevaluated_operand;
23968 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23970 return arguments;
23973 /* MEMBER_FUNCTION is a member function, or a friend. If default
23974 arguments, or the body of the function have not yet been parsed,
23975 parse them now. */
23977 static void
23978 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23980 timevar_push (TV_PARSE_INMETH);
23981 /* If this member is a template, get the underlying
23982 FUNCTION_DECL. */
23983 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23984 member_function = DECL_TEMPLATE_RESULT (member_function);
23986 /* There should not be any class definitions in progress at this
23987 point; the bodies of members are only parsed outside of all class
23988 definitions. */
23989 gcc_assert (parser->num_classes_being_defined == 0);
23990 /* While we're parsing the member functions we might encounter more
23991 classes. We want to handle them right away, but we don't want
23992 them getting mixed up with functions that are currently in the
23993 queue. */
23994 push_unparsed_function_queues (parser);
23996 /* Make sure that any template parameters are in scope. */
23997 maybe_begin_member_template_processing (member_function);
23999 /* If the body of the function has not yet been parsed, parse it
24000 now. */
24001 if (DECL_PENDING_INLINE_P (member_function))
24003 tree function_scope;
24004 cp_token_cache *tokens;
24006 /* The function is no longer pending; we are processing it. */
24007 tokens = DECL_PENDING_INLINE_INFO (member_function);
24008 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24009 DECL_PENDING_INLINE_P (member_function) = 0;
24011 /* If this is a local class, enter the scope of the containing
24012 function. */
24013 function_scope = current_function_decl;
24014 if (function_scope)
24015 push_function_context ();
24017 /* Push the body of the function onto the lexer stack. */
24018 cp_parser_push_lexer_for_tokens (parser, tokens);
24020 /* Let the front end know that we going to be defining this
24021 function. */
24022 start_preparsed_function (member_function, NULL_TREE,
24023 SF_PRE_PARSED | SF_INCLASS_INLINE);
24025 /* Don't do access checking if it is a templated function. */
24026 if (processing_template_decl)
24027 push_deferring_access_checks (dk_no_check);
24029 /* #pragma omp declare reduction needs special parsing. */
24030 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24032 parser->lexer->in_pragma = true;
24033 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24034 finish_function (/*inline*/2);
24035 cp_check_omp_declare_reduction (member_function);
24037 else
24038 /* Now, parse the body of the function. */
24039 cp_parser_function_definition_after_declarator (parser,
24040 /*inline_p=*/true);
24042 if (processing_template_decl)
24043 pop_deferring_access_checks ();
24045 /* Leave the scope of the containing function. */
24046 if (function_scope)
24047 pop_function_context ();
24048 cp_parser_pop_lexer (parser);
24051 /* Remove any template parameters from the symbol table. */
24052 maybe_end_member_template_processing ();
24054 /* Restore the queue. */
24055 pop_unparsed_function_queues (parser);
24056 timevar_pop (TV_PARSE_INMETH);
24059 /* If DECL contains any default args, remember it on the unparsed
24060 functions queue. */
24062 static void
24063 cp_parser_save_default_args (cp_parser* parser, tree decl)
24065 tree probe;
24067 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24068 probe;
24069 probe = TREE_CHAIN (probe))
24070 if (TREE_PURPOSE (probe))
24072 cp_default_arg_entry entry = {current_class_type, decl};
24073 vec_safe_push (unparsed_funs_with_default_args, entry);
24074 break;
24078 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24079 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24080 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24081 from the parameter-type-list. */
24083 static tree
24084 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24085 tree default_arg, tree parmtype)
24087 cp_token_cache *tokens;
24088 tree parsed_arg;
24089 bool dummy;
24091 if (default_arg == error_mark_node)
24092 return error_mark_node;
24094 /* Push the saved tokens for the default argument onto the parser's
24095 lexer stack. */
24096 tokens = DEFARG_TOKENS (default_arg);
24097 cp_parser_push_lexer_for_tokens (parser, tokens);
24099 start_lambda_scope (decl);
24101 /* Parse the default argument. */
24102 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24103 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24104 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24106 finish_lambda_scope ();
24108 if (parsed_arg == error_mark_node)
24109 cp_parser_skip_to_end_of_statement (parser);
24111 if (!processing_template_decl)
24113 /* In a non-template class, check conversions now. In a template,
24114 we'll wait and instantiate these as needed. */
24115 if (TREE_CODE (decl) == PARM_DECL)
24116 parsed_arg = check_default_argument (parmtype, parsed_arg,
24117 tf_warning_or_error);
24118 else
24119 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24122 /* If the token stream has not been completely used up, then
24123 there was extra junk after the end of the default
24124 argument. */
24125 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24127 if (TREE_CODE (decl) == PARM_DECL)
24128 cp_parser_error (parser, "expected %<,%>");
24129 else
24130 cp_parser_error (parser, "expected %<;%>");
24133 /* Revert to the main lexer. */
24134 cp_parser_pop_lexer (parser);
24136 return parsed_arg;
24139 /* FIELD is a non-static data member with an initializer which we saved for
24140 later; parse it now. */
24142 static void
24143 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24145 tree def;
24147 maybe_begin_member_template_processing (field);
24149 push_unparsed_function_queues (parser);
24150 def = cp_parser_late_parse_one_default_arg (parser, field,
24151 DECL_INITIAL (field),
24152 NULL_TREE);
24153 pop_unparsed_function_queues (parser);
24155 maybe_end_member_template_processing ();
24157 DECL_INITIAL (field) = def;
24160 /* FN is a FUNCTION_DECL which may contains a parameter with an
24161 unparsed DEFAULT_ARG. Parse the default args now. This function
24162 assumes that the current scope is the scope in which the default
24163 argument should be processed. */
24165 static void
24166 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24168 bool saved_local_variables_forbidden_p;
24169 tree parm, parmdecl;
24171 /* While we're parsing the default args, we might (due to the
24172 statement expression extension) encounter more classes. We want
24173 to handle them right away, but we don't want them getting mixed
24174 up with default args that are currently in the queue. */
24175 push_unparsed_function_queues (parser);
24177 /* Local variable names (and the `this' keyword) may not appear
24178 in a default argument. */
24179 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24180 parser->local_variables_forbidden_p = true;
24182 push_defarg_context (fn);
24184 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24185 parmdecl = DECL_ARGUMENTS (fn);
24186 parm && parm != void_list_node;
24187 parm = TREE_CHAIN (parm),
24188 parmdecl = DECL_CHAIN (parmdecl))
24190 tree default_arg = TREE_PURPOSE (parm);
24191 tree parsed_arg;
24192 vec<tree, va_gc> *insts;
24193 tree copy;
24194 unsigned ix;
24196 if (!default_arg)
24197 continue;
24199 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24200 /* This can happen for a friend declaration for a function
24201 already declared with default arguments. */
24202 continue;
24204 parsed_arg
24205 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24206 default_arg,
24207 TREE_VALUE (parm));
24208 if (parsed_arg == error_mark_node)
24210 continue;
24213 TREE_PURPOSE (parm) = parsed_arg;
24215 /* Update any instantiations we've already created. */
24216 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24217 vec_safe_iterate (insts, ix, &copy); ix++)
24218 TREE_PURPOSE (copy) = parsed_arg;
24221 pop_defarg_context ();
24223 /* Make sure no default arg is missing. */
24224 check_default_args (fn);
24226 /* Restore the state of local_variables_forbidden_p. */
24227 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24229 /* Restore the queue. */
24230 pop_unparsed_function_queues (parser);
24233 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24235 sizeof ... ( identifier )
24237 where the 'sizeof' token has already been consumed. */
24239 static tree
24240 cp_parser_sizeof_pack (cp_parser *parser)
24242 /* Consume the `...'. */
24243 cp_lexer_consume_token (parser->lexer);
24244 maybe_warn_variadic_templates ();
24246 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24247 if (paren)
24248 cp_lexer_consume_token (parser->lexer);
24249 else
24250 permerror (cp_lexer_peek_token (parser->lexer)->location,
24251 "%<sizeof...%> argument must be surrounded by parentheses");
24253 cp_token *token = cp_lexer_peek_token (parser->lexer);
24254 tree name = cp_parser_identifier (parser);
24255 if (name == error_mark_node)
24256 return error_mark_node;
24257 /* The name is not qualified. */
24258 parser->scope = NULL_TREE;
24259 parser->qualifying_scope = NULL_TREE;
24260 parser->object_scope = NULL_TREE;
24261 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24262 if (expr == error_mark_node)
24263 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24264 token->location);
24265 if (TREE_CODE (expr) == TYPE_DECL)
24266 expr = TREE_TYPE (expr);
24267 else if (TREE_CODE (expr) == CONST_DECL)
24268 expr = DECL_INITIAL (expr);
24269 expr = make_pack_expansion (expr);
24271 if (paren)
24272 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24274 return expr;
24277 /* Parse the operand of `sizeof' (or a similar operator). Returns
24278 either a TYPE or an expression, depending on the form of the
24279 input. The KEYWORD indicates which kind of expression we have
24280 encountered. */
24282 static tree
24283 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24285 tree expr = NULL_TREE;
24286 const char *saved_message;
24287 char *tmp;
24288 bool saved_integral_constant_expression_p;
24289 bool saved_non_integral_constant_expression_p;
24291 /* If it's a `...', then we are computing the length of a parameter
24292 pack. */
24293 if (keyword == RID_SIZEOF
24294 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24295 return cp_parser_sizeof_pack (parser);
24297 /* Types cannot be defined in a `sizeof' expression. Save away the
24298 old message. */
24299 saved_message = parser->type_definition_forbidden_message;
24300 /* And create the new one. */
24301 tmp = concat ("types may not be defined in %<",
24302 IDENTIFIER_POINTER (ridpointers[keyword]),
24303 "%> expressions", NULL);
24304 parser->type_definition_forbidden_message = tmp;
24306 /* The restrictions on constant-expressions do not apply inside
24307 sizeof expressions. */
24308 saved_integral_constant_expression_p
24309 = parser->integral_constant_expression_p;
24310 saved_non_integral_constant_expression_p
24311 = parser->non_integral_constant_expression_p;
24312 parser->integral_constant_expression_p = false;
24314 /* Do not actually evaluate the expression. */
24315 ++cp_unevaluated_operand;
24316 ++c_inhibit_evaluation_warnings;
24317 /* If it's a `(', then we might be looking at the type-id
24318 construction. */
24319 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24321 tree type = NULL_TREE;
24323 /* We can't be sure yet whether we're looking at a type-id or an
24324 expression. */
24325 cp_parser_parse_tentatively (parser);
24326 /* Note: as a GNU Extension, compound literals are considered
24327 postfix-expressions as they are in C99, so they are valid
24328 arguments to sizeof. See comment in cp_parser_cast_expression
24329 for details. */
24330 if (cp_parser_compound_literal_p (parser))
24331 cp_parser_simulate_error (parser);
24332 else
24334 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24335 parser->in_type_id_in_expr_p = true;
24336 /* Look for the type-id. */
24337 type = cp_parser_type_id (parser);
24338 /* Look for the closing `)'. */
24339 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24340 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24343 /* If all went well, then we're done. */
24344 if (cp_parser_parse_definitely (parser))
24346 cp_decl_specifier_seq decl_specs;
24348 /* Build a trivial decl-specifier-seq. */
24349 clear_decl_specs (&decl_specs);
24350 decl_specs.type = type;
24352 /* Call grokdeclarator to figure out what type this is. */
24353 expr = grokdeclarator (NULL,
24354 &decl_specs,
24355 TYPENAME,
24356 /*initialized=*/0,
24357 /*attrlist=*/NULL);
24361 /* If the type-id production did not work out, then we must be
24362 looking at the unary-expression production. */
24363 if (!expr)
24364 expr = cp_parser_unary_expression (parser);
24366 /* Go back to evaluating expressions. */
24367 --cp_unevaluated_operand;
24368 --c_inhibit_evaluation_warnings;
24370 /* Free the message we created. */
24371 free (tmp);
24372 /* And restore the old one. */
24373 parser->type_definition_forbidden_message = saved_message;
24374 parser->integral_constant_expression_p
24375 = saved_integral_constant_expression_p;
24376 parser->non_integral_constant_expression_p
24377 = saved_non_integral_constant_expression_p;
24379 return expr;
24382 /* If the current declaration has no declarator, return true. */
24384 static bool
24385 cp_parser_declares_only_class_p (cp_parser *parser)
24387 /* If the next token is a `;' or a `,' then there is no
24388 declarator. */
24389 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24390 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24393 /* Update the DECL_SPECS to reflect the storage class indicated by
24394 KEYWORD. */
24396 static void
24397 cp_parser_set_storage_class (cp_parser *parser,
24398 cp_decl_specifier_seq *decl_specs,
24399 enum rid keyword,
24400 cp_token *token)
24402 cp_storage_class storage_class;
24404 if (parser->in_unbraced_linkage_specification_p)
24406 error_at (token->location, "invalid use of %qD in linkage specification",
24407 ridpointers[keyword]);
24408 return;
24410 else if (decl_specs->storage_class != sc_none)
24412 decl_specs->conflicting_specifiers_p = true;
24413 return;
24416 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24417 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24418 && decl_specs->gnu_thread_keyword_p)
24420 pedwarn (decl_specs->locations[ds_thread], 0,
24421 "%<__thread%> before %qD", ridpointers[keyword]);
24424 switch (keyword)
24426 case RID_AUTO:
24427 storage_class = sc_auto;
24428 break;
24429 case RID_REGISTER:
24430 storage_class = sc_register;
24431 break;
24432 case RID_STATIC:
24433 storage_class = sc_static;
24434 break;
24435 case RID_EXTERN:
24436 storage_class = sc_extern;
24437 break;
24438 case RID_MUTABLE:
24439 storage_class = sc_mutable;
24440 break;
24441 default:
24442 gcc_unreachable ();
24444 decl_specs->storage_class = storage_class;
24445 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24447 /* A storage class specifier cannot be applied alongside a typedef
24448 specifier. If there is a typedef specifier present then set
24449 conflicting_specifiers_p which will trigger an error later
24450 on in grokdeclarator. */
24451 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24452 decl_specs->conflicting_specifiers_p = true;
24455 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24456 is true, the type is a class or enum definition. */
24458 static void
24459 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24460 tree type_spec,
24461 cp_token *token,
24462 bool type_definition_p)
24464 decl_specs->any_specifiers_p = true;
24466 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24467 (with, for example, in "typedef int wchar_t;") we remember that
24468 this is what happened. In system headers, we ignore these
24469 declarations so that G++ can work with system headers that are not
24470 C++-safe. */
24471 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24472 && !type_definition_p
24473 && (type_spec == boolean_type_node
24474 || type_spec == char16_type_node
24475 || type_spec == char32_type_node
24476 || type_spec == wchar_type_node)
24477 && (decl_specs->type
24478 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24479 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24480 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24481 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24483 decl_specs->redefined_builtin_type = type_spec;
24484 set_and_check_decl_spec_loc (decl_specs,
24485 ds_redefined_builtin_type_spec,
24486 token);
24487 if (!decl_specs->type)
24489 decl_specs->type = type_spec;
24490 decl_specs->type_definition_p = false;
24491 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24494 else if (decl_specs->type)
24495 decl_specs->multiple_types_p = true;
24496 else
24498 decl_specs->type = type_spec;
24499 decl_specs->type_definition_p = type_definition_p;
24500 decl_specs->redefined_builtin_type = NULL_TREE;
24501 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24505 /* True iff TOKEN is the GNU keyword __thread. */
24507 static bool
24508 token_is__thread (cp_token *token)
24510 gcc_assert (token->keyword == RID_THREAD);
24511 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24514 /* Set the location for a declarator specifier and check if it is
24515 duplicated.
24517 DECL_SPECS is the sequence of declarator specifiers onto which to
24518 set the location.
24520 DS is the single declarator specifier to set which location is to
24521 be set onto the existing sequence of declarators.
24523 LOCATION is the location for the declarator specifier to
24524 consider. */
24526 static void
24527 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24528 cp_decl_spec ds, cp_token *token)
24530 gcc_assert (ds < ds_last);
24532 if (decl_specs == NULL)
24533 return;
24535 source_location location = token->location;
24537 if (decl_specs->locations[ds] == 0)
24539 decl_specs->locations[ds] = location;
24540 if (ds == ds_thread)
24541 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24543 else
24545 if (ds == ds_long)
24547 if (decl_specs->locations[ds_long_long] != 0)
24548 error_at (location,
24549 "%<long long long%> is too long for GCC");
24550 else
24552 decl_specs->locations[ds_long_long] = location;
24553 pedwarn_cxx98 (location,
24554 OPT_Wlong_long,
24555 "ISO C++ 1998 does not support %<long long%>");
24558 else if (ds == ds_thread)
24560 bool gnu = token_is__thread (token);
24561 if (gnu != decl_specs->gnu_thread_keyword_p)
24562 error_at (location,
24563 "both %<__thread%> and %<thread_local%> specified");
24564 else
24565 error_at (location, "duplicate %qD", token->u.value);
24567 else
24569 static const char *const decl_spec_names[] = {
24570 "signed",
24571 "unsigned",
24572 "short",
24573 "long",
24574 "const",
24575 "volatile",
24576 "restrict",
24577 "inline",
24578 "virtual",
24579 "explicit",
24580 "friend",
24581 "typedef",
24582 "using",
24583 "constexpr",
24584 "__complex"
24586 error_at (location,
24587 "duplicate %qs", decl_spec_names[ds]);
24592 /* Return true iff the declarator specifier DS is present in the
24593 sequence of declarator specifiers DECL_SPECS. */
24595 bool
24596 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24597 cp_decl_spec ds)
24599 gcc_assert (ds < ds_last);
24601 if (decl_specs == NULL)
24602 return false;
24604 return decl_specs->locations[ds] != 0;
24607 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24608 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24610 static bool
24611 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24613 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24616 /* Issue an error message indicating that TOKEN_DESC was expected.
24617 If KEYWORD is true, it indicated this function is called by
24618 cp_parser_require_keword and the required token can only be
24619 a indicated keyword. */
24621 static void
24622 cp_parser_required_error (cp_parser *parser,
24623 required_token token_desc,
24624 bool keyword)
24626 switch (token_desc)
24628 case RT_NEW:
24629 cp_parser_error (parser, "expected %<new%>");
24630 return;
24631 case RT_DELETE:
24632 cp_parser_error (parser, "expected %<delete%>");
24633 return;
24634 case RT_RETURN:
24635 cp_parser_error (parser, "expected %<return%>");
24636 return;
24637 case RT_WHILE:
24638 cp_parser_error (parser, "expected %<while%>");
24639 return;
24640 case RT_EXTERN:
24641 cp_parser_error (parser, "expected %<extern%>");
24642 return;
24643 case RT_STATIC_ASSERT:
24644 cp_parser_error (parser, "expected %<static_assert%>");
24645 return;
24646 case RT_DECLTYPE:
24647 cp_parser_error (parser, "expected %<decltype%>");
24648 return;
24649 case RT_OPERATOR:
24650 cp_parser_error (parser, "expected %<operator%>");
24651 return;
24652 case RT_CLASS:
24653 cp_parser_error (parser, "expected %<class%>");
24654 return;
24655 case RT_TEMPLATE:
24656 cp_parser_error (parser, "expected %<template%>");
24657 return;
24658 case RT_NAMESPACE:
24659 cp_parser_error (parser, "expected %<namespace%>");
24660 return;
24661 case RT_USING:
24662 cp_parser_error (parser, "expected %<using%>");
24663 return;
24664 case RT_ASM:
24665 cp_parser_error (parser, "expected %<asm%>");
24666 return;
24667 case RT_TRY:
24668 cp_parser_error (parser, "expected %<try%>");
24669 return;
24670 case RT_CATCH:
24671 cp_parser_error (parser, "expected %<catch%>");
24672 return;
24673 case RT_THROW:
24674 cp_parser_error (parser, "expected %<throw%>");
24675 return;
24676 case RT_LABEL:
24677 cp_parser_error (parser, "expected %<__label__%>");
24678 return;
24679 case RT_AT_TRY:
24680 cp_parser_error (parser, "expected %<@try%>");
24681 return;
24682 case RT_AT_SYNCHRONIZED:
24683 cp_parser_error (parser, "expected %<@synchronized%>");
24684 return;
24685 case RT_AT_THROW:
24686 cp_parser_error (parser, "expected %<@throw%>");
24687 return;
24688 case RT_TRANSACTION_ATOMIC:
24689 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24690 return;
24691 case RT_TRANSACTION_RELAXED:
24692 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24693 return;
24694 default:
24695 break;
24697 if (!keyword)
24699 switch (token_desc)
24701 case RT_SEMICOLON:
24702 cp_parser_error (parser, "expected %<;%>");
24703 return;
24704 case RT_OPEN_PAREN:
24705 cp_parser_error (parser, "expected %<(%>");
24706 return;
24707 case RT_CLOSE_BRACE:
24708 cp_parser_error (parser, "expected %<}%>");
24709 return;
24710 case RT_OPEN_BRACE:
24711 cp_parser_error (parser, "expected %<{%>");
24712 return;
24713 case RT_CLOSE_SQUARE:
24714 cp_parser_error (parser, "expected %<]%>");
24715 return;
24716 case RT_OPEN_SQUARE:
24717 cp_parser_error (parser, "expected %<[%>");
24718 return;
24719 case RT_COMMA:
24720 cp_parser_error (parser, "expected %<,%>");
24721 return;
24722 case RT_SCOPE:
24723 cp_parser_error (parser, "expected %<::%>");
24724 return;
24725 case RT_LESS:
24726 cp_parser_error (parser, "expected %<<%>");
24727 return;
24728 case RT_GREATER:
24729 cp_parser_error (parser, "expected %<>%>");
24730 return;
24731 case RT_EQ:
24732 cp_parser_error (parser, "expected %<=%>");
24733 return;
24734 case RT_ELLIPSIS:
24735 cp_parser_error (parser, "expected %<...%>");
24736 return;
24737 case RT_MULT:
24738 cp_parser_error (parser, "expected %<*%>");
24739 return;
24740 case RT_COMPL:
24741 cp_parser_error (parser, "expected %<~%>");
24742 return;
24743 case RT_COLON:
24744 cp_parser_error (parser, "expected %<:%>");
24745 return;
24746 case RT_COLON_SCOPE:
24747 cp_parser_error (parser, "expected %<:%> or %<::%>");
24748 return;
24749 case RT_CLOSE_PAREN:
24750 cp_parser_error (parser, "expected %<)%>");
24751 return;
24752 case RT_COMMA_CLOSE_PAREN:
24753 cp_parser_error (parser, "expected %<,%> or %<)%>");
24754 return;
24755 case RT_PRAGMA_EOL:
24756 cp_parser_error (parser, "expected end of line");
24757 return;
24758 case RT_NAME:
24759 cp_parser_error (parser, "expected identifier");
24760 return;
24761 case RT_SELECT:
24762 cp_parser_error (parser, "expected selection-statement");
24763 return;
24764 case RT_INTERATION:
24765 cp_parser_error (parser, "expected iteration-statement");
24766 return;
24767 case RT_JUMP:
24768 cp_parser_error (parser, "expected jump-statement");
24769 return;
24770 case RT_CLASS_KEY:
24771 cp_parser_error (parser, "expected class-key");
24772 return;
24773 case RT_CLASS_TYPENAME_TEMPLATE:
24774 cp_parser_error (parser,
24775 "expected %<class%>, %<typename%>, or %<template%>");
24776 return;
24777 default:
24778 gcc_unreachable ();
24781 else
24782 gcc_unreachable ();
24787 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24788 issue an error message indicating that TOKEN_DESC was expected.
24790 Returns the token consumed, if the token had the appropriate type.
24791 Otherwise, returns NULL. */
24793 static cp_token *
24794 cp_parser_require (cp_parser* parser,
24795 enum cpp_ttype type,
24796 required_token token_desc)
24798 if (cp_lexer_next_token_is (parser->lexer, type))
24799 return cp_lexer_consume_token (parser->lexer);
24800 else
24802 /* Output the MESSAGE -- unless we're parsing tentatively. */
24803 if (!cp_parser_simulate_error (parser))
24804 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24805 return NULL;
24809 /* An error message is produced if the next token is not '>'.
24810 All further tokens are skipped until the desired token is
24811 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24813 static void
24814 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24816 /* Current level of '< ... >'. */
24817 unsigned level = 0;
24818 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24819 unsigned nesting_depth = 0;
24821 /* Are we ready, yet? If not, issue error message. */
24822 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24823 return;
24825 /* Skip tokens until the desired token is found. */
24826 while (true)
24828 /* Peek at the next token. */
24829 switch (cp_lexer_peek_token (parser->lexer)->type)
24831 case CPP_LESS:
24832 if (!nesting_depth)
24833 ++level;
24834 break;
24836 case CPP_RSHIFT:
24837 if (cxx_dialect == cxx98)
24838 /* C++0x views the `>>' operator as two `>' tokens, but
24839 C++98 does not. */
24840 break;
24841 else if (!nesting_depth && level-- == 0)
24843 /* We've hit a `>>' where the first `>' closes the
24844 template argument list, and the second `>' is
24845 spurious. Just consume the `>>' and stop; we've
24846 already produced at least one error. */
24847 cp_lexer_consume_token (parser->lexer);
24848 return;
24850 /* Fall through for C++0x, so we handle the second `>' in
24851 the `>>'. */
24853 case CPP_GREATER:
24854 if (!nesting_depth && level-- == 0)
24856 /* We've reached the token we want, consume it and stop. */
24857 cp_lexer_consume_token (parser->lexer);
24858 return;
24860 break;
24862 case CPP_OPEN_PAREN:
24863 case CPP_OPEN_SQUARE:
24864 ++nesting_depth;
24865 break;
24867 case CPP_CLOSE_PAREN:
24868 case CPP_CLOSE_SQUARE:
24869 if (nesting_depth-- == 0)
24870 return;
24871 break;
24873 case CPP_EOF:
24874 case CPP_PRAGMA_EOL:
24875 case CPP_SEMICOLON:
24876 case CPP_OPEN_BRACE:
24877 case CPP_CLOSE_BRACE:
24878 /* The '>' was probably forgotten, don't look further. */
24879 return;
24881 default:
24882 break;
24885 /* Consume this token. */
24886 cp_lexer_consume_token (parser->lexer);
24890 /* If the next token is the indicated keyword, consume it. Otherwise,
24891 issue an error message indicating that TOKEN_DESC was expected.
24893 Returns the token consumed, if the token had the appropriate type.
24894 Otherwise, returns NULL. */
24896 static cp_token *
24897 cp_parser_require_keyword (cp_parser* parser,
24898 enum rid keyword,
24899 required_token token_desc)
24901 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24903 if (token && token->keyword != keyword)
24905 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24906 return NULL;
24909 return token;
24912 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24913 function-definition. */
24915 static bool
24916 cp_parser_token_starts_function_definition_p (cp_token* token)
24918 return (/* An ordinary function-body begins with an `{'. */
24919 token->type == CPP_OPEN_BRACE
24920 /* A ctor-initializer begins with a `:'. */
24921 || token->type == CPP_COLON
24922 /* A function-try-block begins with `try'. */
24923 || token->keyword == RID_TRY
24924 /* A function-transaction-block begins with `__transaction_atomic'
24925 or `__transaction_relaxed'. */
24926 || token->keyword == RID_TRANSACTION_ATOMIC
24927 || token->keyword == RID_TRANSACTION_RELAXED
24928 /* The named return value extension begins with `return'. */
24929 || token->keyword == RID_RETURN);
24932 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24933 definition. */
24935 static bool
24936 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24938 cp_token *token;
24940 token = cp_lexer_peek_token (parser->lexer);
24941 return (token->type == CPP_OPEN_BRACE
24942 || (token->type == CPP_COLON
24943 && !parser->colon_doesnt_start_class_def_p));
24946 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24947 C++0x) ending a template-argument. */
24949 static bool
24950 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24952 cp_token *token;
24954 token = cp_lexer_peek_token (parser->lexer);
24955 return (token->type == CPP_COMMA
24956 || token->type == CPP_GREATER
24957 || token->type == CPP_ELLIPSIS
24958 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24961 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24962 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24964 static bool
24965 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24966 size_t n)
24968 cp_token *token;
24970 token = cp_lexer_peek_nth_token (parser->lexer, n);
24971 if (token->type == CPP_LESS)
24972 return true;
24973 /* Check for the sequence `<::' in the original code. It would be lexed as
24974 `[:', where `[' is a digraph, and there is no whitespace before
24975 `:'. */
24976 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24978 cp_token *token2;
24979 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24980 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24981 return true;
24983 return false;
24986 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24987 or none_type otherwise. */
24989 static enum tag_types
24990 cp_parser_token_is_class_key (cp_token* token)
24992 switch (token->keyword)
24994 case RID_CLASS:
24995 return class_type;
24996 case RID_STRUCT:
24997 return record_type;
24998 case RID_UNION:
24999 return union_type;
25001 default:
25002 return none_type;
25006 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25007 or none_type otherwise or if the token is null. */
25009 static enum tag_types
25010 cp_parser_token_is_type_parameter_key (cp_token* token)
25012 if (!token)
25013 return none_type;
25015 switch (token->keyword)
25017 case RID_CLASS:
25018 return class_type;
25019 case RID_TYPENAME:
25020 return typename_type;
25022 default:
25023 return none_type;
25027 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25029 static void
25030 cp_parser_check_class_key (enum tag_types class_key, tree type)
25032 if (type == error_mark_node)
25033 return;
25034 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25036 if (permerror (input_location, "%qs tag used in naming %q#T",
25037 class_key == union_type ? "union"
25038 : class_key == record_type ? "struct" : "class",
25039 type))
25040 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25041 "%q#T was previously declared here", type);
25045 /* Issue an error message if DECL is redeclared with different
25046 access than its original declaration [class.access.spec/3].
25047 This applies to nested classes and nested class templates.
25048 [class.mem/1]. */
25050 static void
25051 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25053 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25054 return;
25056 if ((TREE_PRIVATE (decl)
25057 != (current_access_specifier == access_private_node))
25058 || (TREE_PROTECTED (decl)
25059 != (current_access_specifier == access_protected_node)))
25060 error_at (location, "%qD redeclared with different access", decl);
25063 /* Look for the `template' keyword, as a syntactic disambiguator.
25064 Return TRUE iff it is present, in which case it will be
25065 consumed. */
25067 static bool
25068 cp_parser_optional_template_keyword (cp_parser *parser)
25070 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25072 /* In C++98 the `template' keyword can only be used within templates;
25073 outside templates the parser can always figure out what is a
25074 template and what is not. In C++11, per the resolution of DR 468,
25075 `template' is allowed in cases where it is not strictly necessary. */
25076 if (!processing_template_decl
25077 && pedantic && cxx_dialect == cxx98)
25079 cp_token *token = cp_lexer_peek_token (parser->lexer);
25080 pedwarn (token->location, OPT_Wpedantic,
25081 "in C++98 %<template%> (as a disambiguator) is only "
25082 "allowed within templates");
25083 /* If this part of the token stream is rescanned, the same
25084 error message would be generated. So, we purge the token
25085 from the stream. */
25086 cp_lexer_purge_token (parser->lexer);
25087 return false;
25089 else
25091 /* Consume the `template' keyword. */
25092 cp_lexer_consume_token (parser->lexer);
25093 return true;
25096 return false;
25099 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25100 set PARSER->SCOPE, and perform other related actions. */
25102 static void
25103 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25105 int i;
25106 struct tree_check *check_value;
25107 deferred_access_check *chk;
25108 vec<deferred_access_check, va_gc> *checks;
25110 /* Get the stored value. */
25111 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25112 /* Perform any access checks that were deferred. */
25113 checks = check_value->checks;
25114 if (checks)
25116 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25117 perform_or_defer_access_check (chk->binfo,
25118 chk->decl,
25119 chk->diag_decl, tf_warning_or_error);
25121 /* Set the scope from the stored value. */
25122 parser->scope = check_value->value;
25123 parser->qualifying_scope = check_value->qualifying_scope;
25124 parser->object_scope = NULL_TREE;
25127 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25128 encounter the end of a block before what we were looking for. */
25130 static bool
25131 cp_parser_cache_group (cp_parser *parser,
25132 enum cpp_ttype end,
25133 unsigned depth)
25135 while (true)
25137 cp_token *token = cp_lexer_peek_token (parser->lexer);
25139 /* Abort a parenthesized expression if we encounter a semicolon. */
25140 if ((end == CPP_CLOSE_PAREN || depth == 0)
25141 && token->type == CPP_SEMICOLON)
25142 return true;
25143 /* If we've reached the end of the file, stop. */
25144 if (token->type == CPP_EOF
25145 || (end != CPP_PRAGMA_EOL
25146 && token->type == CPP_PRAGMA_EOL))
25147 return true;
25148 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25149 /* We've hit the end of an enclosing block, so there's been some
25150 kind of syntax error. */
25151 return true;
25153 /* Consume the token. */
25154 cp_lexer_consume_token (parser->lexer);
25155 /* See if it starts a new group. */
25156 if (token->type == CPP_OPEN_BRACE)
25158 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25159 /* In theory this should probably check end == '}', but
25160 cp_parser_save_member_function_body needs it to exit
25161 after either '}' or ')' when called with ')'. */
25162 if (depth == 0)
25163 return false;
25165 else if (token->type == CPP_OPEN_PAREN)
25167 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25168 if (depth == 0 && end == CPP_CLOSE_PAREN)
25169 return false;
25171 else if (token->type == CPP_PRAGMA)
25172 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25173 else if (token->type == end)
25174 return false;
25178 /* Like above, for caching a default argument or NSDMI. Both of these are
25179 terminated by a non-nested comma, but it can be unclear whether or not a
25180 comma is nested in a template argument list unless we do more parsing.
25181 In order to handle this ambiguity, when we encounter a ',' after a '<'
25182 we try to parse what follows as a parameter-declaration-list (in the
25183 case of a default argument) or a member-declarator (in the case of an
25184 NSDMI). If that succeeds, then we stop caching. */
25186 static tree
25187 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25189 unsigned depth = 0;
25190 int maybe_template_id = 0;
25191 cp_token *first_token;
25192 cp_token *token;
25193 tree default_argument;
25195 /* Add tokens until we have processed the entire default
25196 argument. We add the range [first_token, token). */
25197 first_token = cp_lexer_peek_token (parser->lexer);
25198 if (first_token->type == CPP_OPEN_BRACE)
25200 /* For list-initialization, this is straightforward. */
25201 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25202 token = cp_lexer_peek_token (parser->lexer);
25204 else while (true)
25206 bool done = false;
25208 /* Peek at the next token. */
25209 token = cp_lexer_peek_token (parser->lexer);
25210 /* What we do depends on what token we have. */
25211 switch (token->type)
25213 /* In valid code, a default argument must be
25214 immediately followed by a `,' `)', or `...'. */
25215 case CPP_COMMA:
25216 if (depth == 0 && maybe_template_id)
25218 /* If we've seen a '<', we might be in a
25219 template-argument-list. Until Core issue 325 is
25220 resolved, we don't know how this situation ought
25221 to be handled, so try to DTRT. We check whether
25222 what comes after the comma is a valid parameter
25223 declaration list. If it is, then the comma ends
25224 the default argument; otherwise the default
25225 argument continues. */
25226 bool error = false;
25228 /* Set ITALP so cp_parser_parameter_declaration_list
25229 doesn't decide to commit to this parse. */
25230 bool saved_italp = parser->in_template_argument_list_p;
25231 parser->in_template_argument_list_p = true;
25233 cp_parser_parse_tentatively (parser);
25234 cp_lexer_consume_token (parser->lexer);
25236 if (nsdmi)
25238 int ctor_dtor_or_conv_p;
25239 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25240 &ctor_dtor_or_conv_p,
25241 /*parenthesized_p=*/NULL,
25242 /*member_p=*/true,
25243 /*friend_p=*/false);
25245 else
25247 begin_scope (sk_function_parms, NULL_TREE);
25248 cp_parser_parameter_declaration_list (parser, &error);
25249 pop_bindings_and_leave_scope ();
25251 if (!cp_parser_error_occurred (parser) && !error)
25252 done = true;
25253 cp_parser_abort_tentative_parse (parser);
25255 parser->in_template_argument_list_p = saved_italp;
25256 break;
25258 case CPP_CLOSE_PAREN:
25259 case CPP_ELLIPSIS:
25260 /* If we run into a non-nested `;', `}', or `]',
25261 then the code is invalid -- but the default
25262 argument is certainly over. */
25263 case CPP_SEMICOLON:
25264 case CPP_CLOSE_BRACE:
25265 case CPP_CLOSE_SQUARE:
25266 if (depth == 0
25267 /* Handle correctly int n = sizeof ... ( p ); */
25268 && token->type != CPP_ELLIPSIS)
25269 done = true;
25270 /* Update DEPTH, if necessary. */
25271 else if (token->type == CPP_CLOSE_PAREN
25272 || token->type == CPP_CLOSE_BRACE
25273 || token->type == CPP_CLOSE_SQUARE)
25274 --depth;
25275 break;
25277 case CPP_OPEN_PAREN:
25278 case CPP_OPEN_SQUARE:
25279 case CPP_OPEN_BRACE:
25280 ++depth;
25281 break;
25283 case CPP_LESS:
25284 if (depth == 0)
25285 /* This might be the comparison operator, or it might
25286 start a template argument list. */
25287 ++maybe_template_id;
25288 break;
25290 case CPP_RSHIFT:
25291 if (cxx_dialect == cxx98)
25292 break;
25293 /* Fall through for C++0x, which treats the `>>'
25294 operator like two `>' tokens in certain
25295 cases. */
25297 case CPP_GREATER:
25298 if (depth == 0)
25300 /* This might be an operator, or it might close a
25301 template argument list. But if a previous '<'
25302 started a template argument list, this will have
25303 closed it, so we can't be in one anymore. */
25304 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25305 if (maybe_template_id < 0)
25306 maybe_template_id = 0;
25308 break;
25310 /* If we run out of tokens, issue an error message. */
25311 case CPP_EOF:
25312 case CPP_PRAGMA_EOL:
25313 error_at (token->location, "file ends in default argument");
25314 done = true;
25315 break;
25317 case CPP_NAME:
25318 case CPP_SCOPE:
25319 /* In these cases, we should look for template-ids.
25320 For example, if the default argument is
25321 `X<int, double>()', we need to do name lookup to
25322 figure out whether or not `X' is a template; if
25323 so, the `,' does not end the default argument.
25325 That is not yet done. */
25326 break;
25328 default:
25329 break;
25332 /* If we've reached the end, stop. */
25333 if (done)
25334 break;
25336 /* Add the token to the token block. */
25337 token = cp_lexer_consume_token (parser->lexer);
25340 /* Create a DEFAULT_ARG to represent the unparsed default
25341 argument. */
25342 default_argument = make_node (DEFAULT_ARG);
25343 DEFARG_TOKENS (default_argument)
25344 = cp_token_cache_new (first_token, token);
25345 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25347 return default_argument;
25350 /* Begin parsing tentatively. We always save tokens while parsing
25351 tentatively so that if the tentative parsing fails we can restore the
25352 tokens. */
25354 static void
25355 cp_parser_parse_tentatively (cp_parser* parser)
25357 /* Enter a new parsing context. */
25358 parser->context = cp_parser_context_new (parser->context);
25359 /* Begin saving tokens. */
25360 cp_lexer_save_tokens (parser->lexer);
25361 /* In order to avoid repetitive access control error messages,
25362 access checks are queued up until we are no longer parsing
25363 tentatively. */
25364 push_deferring_access_checks (dk_deferred);
25367 /* Commit to the currently active tentative parse. */
25369 static void
25370 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25372 cp_parser_context *context;
25373 cp_lexer *lexer;
25375 /* Mark all of the levels as committed. */
25376 lexer = parser->lexer;
25377 for (context = parser->context; context->next; context = context->next)
25379 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25380 break;
25381 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25382 while (!cp_lexer_saving_tokens (lexer))
25383 lexer = lexer->next;
25384 cp_lexer_commit_tokens (lexer);
25388 /* Commit to the topmost currently active tentative parse.
25390 Note that this function shouldn't be called when there are
25391 irreversible side-effects while in a tentative state. For
25392 example, we shouldn't create a permanent entry in the symbol
25393 table, or issue an error message that might not apply if the
25394 tentative parse is aborted. */
25396 static void
25397 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25399 cp_parser_context *context = parser->context;
25400 cp_lexer *lexer = parser->lexer;
25402 if (context)
25404 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25405 return;
25406 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25408 while (!cp_lexer_saving_tokens (lexer))
25409 lexer = lexer->next;
25410 cp_lexer_commit_tokens (lexer);
25414 /* Abort the currently active tentative parse. All consumed tokens
25415 will be rolled back, and no diagnostics will be issued. */
25417 static void
25418 cp_parser_abort_tentative_parse (cp_parser* parser)
25420 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25421 || errorcount > 0);
25422 cp_parser_simulate_error (parser);
25423 /* Now, pretend that we want to see if the construct was
25424 successfully parsed. */
25425 cp_parser_parse_definitely (parser);
25428 /* Stop parsing tentatively. If a parse error has occurred, restore the
25429 token stream. Otherwise, commit to the tokens we have consumed.
25430 Returns true if no error occurred; false otherwise. */
25432 static bool
25433 cp_parser_parse_definitely (cp_parser* parser)
25435 bool error_occurred;
25436 cp_parser_context *context;
25438 /* Remember whether or not an error occurred, since we are about to
25439 destroy that information. */
25440 error_occurred = cp_parser_error_occurred (parser);
25441 /* Remove the topmost context from the stack. */
25442 context = parser->context;
25443 parser->context = context->next;
25444 /* If no parse errors occurred, commit to the tentative parse. */
25445 if (!error_occurred)
25447 /* Commit to the tokens read tentatively, unless that was
25448 already done. */
25449 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25450 cp_lexer_commit_tokens (parser->lexer);
25452 pop_to_parent_deferring_access_checks ();
25454 /* Otherwise, if errors occurred, roll back our state so that things
25455 are just as they were before we began the tentative parse. */
25456 else
25458 cp_lexer_rollback_tokens (parser->lexer);
25459 pop_deferring_access_checks ();
25461 /* Add the context to the front of the free list. */
25462 context->next = cp_parser_context_free_list;
25463 cp_parser_context_free_list = context;
25465 return !error_occurred;
25468 /* Returns true if we are parsing tentatively and are not committed to
25469 this tentative parse. */
25471 static bool
25472 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25474 return (cp_parser_parsing_tentatively (parser)
25475 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25478 /* Returns nonzero iff an error has occurred during the most recent
25479 tentative parse. */
25481 static bool
25482 cp_parser_error_occurred (cp_parser* parser)
25484 return (cp_parser_parsing_tentatively (parser)
25485 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25488 /* Returns nonzero if GNU extensions are allowed. */
25490 static bool
25491 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25493 return parser->allow_gnu_extensions_p;
25496 /* Objective-C++ Productions */
25499 /* Parse an Objective-C expression, which feeds into a primary-expression
25500 above.
25502 objc-expression:
25503 objc-message-expression
25504 objc-string-literal
25505 objc-encode-expression
25506 objc-protocol-expression
25507 objc-selector-expression
25509 Returns a tree representation of the expression. */
25511 static tree
25512 cp_parser_objc_expression (cp_parser* parser)
25514 /* Try to figure out what kind of declaration is present. */
25515 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25517 switch (kwd->type)
25519 case CPP_OPEN_SQUARE:
25520 return cp_parser_objc_message_expression (parser);
25522 case CPP_OBJC_STRING:
25523 kwd = cp_lexer_consume_token (parser->lexer);
25524 return objc_build_string_object (kwd->u.value);
25526 case CPP_KEYWORD:
25527 switch (kwd->keyword)
25529 case RID_AT_ENCODE:
25530 return cp_parser_objc_encode_expression (parser);
25532 case RID_AT_PROTOCOL:
25533 return cp_parser_objc_protocol_expression (parser);
25535 case RID_AT_SELECTOR:
25536 return cp_parser_objc_selector_expression (parser);
25538 default:
25539 break;
25541 default:
25542 error_at (kwd->location,
25543 "misplaced %<@%D%> Objective-C++ construct",
25544 kwd->u.value);
25545 cp_parser_skip_to_end_of_block_or_statement (parser);
25548 return error_mark_node;
25551 /* Parse an Objective-C message expression.
25553 objc-message-expression:
25554 [ objc-message-receiver objc-message-args ]
25556 Returns a representation of an Objective-C message. */
25558 static tree
25559 cp_parser_objc_message_expression (cp_parser* parser)
25561 tree receiver, messageargs;
25563 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25564 receiver = cp_parser_objc_message_receiver (parser);
25565 messageargs = cp_parser_objc_message_args (parser);
25566 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25568 return objc_build_message_expr (receiver, messageargs);
25571 /* Parse an objc-message-receiver.
25573 objc-message-receiver:
25574 expression
25575 simple-type-specifier
25577 Returns a representation of the type or expression. */
25579 static tree
25580 cp_parser_objc_message_receiver (cp_parser* parser)
25582 tree rcv;
25584 /* An Objective-C message receiver may be either (1) a type
25585 or (2) an expression. */
25586 cp_parser_parse_tentatively (parser);
25587 rcv = cp_parser_expression (parser);
25589 if (cp_parser_parse_definitely (parser))
25590 return rcv;
25592 rcv = cp_parser_simple_type_specifier (parser,
25593 /*decl_specs=*/NULL,
25594 CP_PARSER_FLAGS_NONE);
25596 return objc_get_class_reference (rcv);
25599 /* Parse the arguments and selectors comprising an Objective-C message.
25601 objc-message-args:
25602 objc-selector
25603 objc-selector-args
25604 objc-selector-args , objc-comma-args
25606 objc-selector-args:
25607 objc-selector [opt] : assignment-expression
25608 objc-selector-args objc-selector [opt] : assignment-expression
25610 objc-comma-args:
25611 assignment-expression
25612 objc-comma-args , assignment-expression
25614 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25615 selector arguments and TREE_VALUE containing a list of comma
25616 arguments. */
25618 static tree
25619 cp_parser_objc_message_args (cp_parser* parser)
25621 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25622 bool maybe_unary_selector_p = true;
25623 cp_token *token = cp_lexer_peek_token (parser->lexer);
25625 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25627 tree selector = NULL_TREE, arg;
25629 if (token->type != CPP_COLON)
25630 selector = cp_parser_objc_selector (parser);
25632 /* Detect if we have a unary selector. */
25633 if (maybe_unary_selector_p
25634 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25635 return build_tree_list (selector, NULL_TREE);
25637 maybe_unary_selector_p = false;
25638 cp_parser_require (parser, CPP_COLON, RT_COLON);
25639 arg = cp_parser_assignment_expression (parser);
25641 sel_args
25642 = chainon (sel_args,
25643 build_tree_list (selector, arg));
25645 token = cp_lexer_peek_token (parser->lexer);
25648 /* Handle non-selector arguments, if any. */
25649 while (token->type == CPP_COMMA)
25651 tree arg;
25653 cp_lexer_consume_token (parser->lexer);
25654 arg = cp_parser_assignment_expression (parser);
25656 addl_args
25657 = chainon (addl_args,
25658 build_tree_list (NULL_TREE, arg));
25660 token = cp_lexer_peek_token (parser->lexer);
25663 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25665 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25666 return build_tree_list (error_mark_node, error_mark_node);
25669 return build_tree_list (sel_args, addl_args);
25672 /* Parse an Objective-C encode expression.
25674 objc-encode-expression:
25675 @encode objc-typename
25677 Returns an encoded representation of the type argument. */
25679 static tree
25680 cp_parser_objc_encode_expression (cp_parser* parser)
25682 tree type;
25683 cp_token *token;
25685 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25686 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25687 token = cp_lexer_peek_token (parser->lexer);
25688 type = complete_type (cp_parser_type_id (parser));
25689 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25691 if (!type)
25693 error_at (token->location,
25694 "%<@encode%> must specify a type as an argument");
25695 return error_mark_node;
25698 /* This happens if we find @encode(T) (where T is a template
25699 typename or something dependent on a template typename) when
25700 parsing a template. In that case, we can't compile it
25701 immediately, but we rather create an AT_ENCODE_EXPR which will
25702 need to be instantiated when the template is used.
25704 if (dependent_type_p (type))
25706 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25707 TREE_READONLY (value) = 1;
25708 return value;
25711 return objc_build_encode_expr (type);
25714 /* Parse an Objective-C @defs expression. */
25716 static tree
25717 cp_parser_objc_defs_expression (cp_parser *parser)
25719 tree name;
25721 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25722 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25723 name = cp_parser_identifier (parser);
25724 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25726 return objc_get_class_ivars (name);
25729 /* Parse an Objective-C protocol expression.
25731 objc-protocol-expression:
25732 @protocol ( identifier )
25734 Returns a representation of the protocol expression. */
25736 static tree
25737 cp_parser_objc_protocol_expression (cp_parser* parser)
25739 tree proto;
25741 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25742 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25743 proto = cp_parser_identifier (parser);
25744 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25746 return objc_build_protocol_expr (proto);
25749 /* Parse an Objective-C selector expression.
25751 objc-selector-expression:
25752 @selector ( objc-method-signature )
25754 objc-method-signature:
25755 objc-selector
25756 objc-selector-seq
25758 objc-selector-seq:
25759 objc-selector :
25760 objc-selector-seq objc-selector :
25762 Returns a representation of the method selector. */
25764 static tree
25765 cp_parser_objc_selector_expression (cp_parser* parser)
25767 tree sel_seq = NULL_TREE;
25768 bool maybe_unary_selector_p = true;
25769 cp_token *token;
25770 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25772 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25773 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25774 token = cp_lexer_peek_token (parser->lexer);
25776 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25777 || token->type == CPP_SCOPE)
25779 tree selector = NULL_TREE;
25781 if (token->type != CPP_COLON
25782 || token->type == CPP_SCOPE)
25783 selector = cp_parser_objc_selector (parser);
25785 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25786 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25788 /* Detect if we have a unary selector. */
25789 if (maybe_unary_selector_p)
25791 sel_seq = selector;
25792 goto finish_selector;
25794 else
25796 cp_parser_error (parser, "expected %<:%>");
25799 maybe_unary_selector_p = false;
25800 token = cp_lexer_consume_token (parser->lexer);
25802 if (token->type == CPP_SCOPE)
25804 sel_seq
25805 = chainon (sel_seq,
25806 build_tree_list (selector, NULL_TREE));
25807 sel_seq
25808 = chainon (sel_seq,
25809 build_tree_list (NULL_TREE, NULL_TREE));
25811 else
25812 sel_seq
25813 = chainon (sel_seq,
25814 build_tree_list (selector, NULL_TREE));
25816 token = cp_lexer_peek_token (parser->lexer);
25819 finish_selector:
25820 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25822 return objc_build_selector_expr (loc, sel_seq);
25825 /* Parse a list of identifiers.
25827 objc-identifier-list:
25828 identifier
25829 objc-identifier-list , identifier
25831 Returns a TREE_LIST of identifier nodes. */
25833 static tree
25834 cp_parser_objc_identifier_list (cp_parser* parser)
25836 tree identifier;
25837 tree list;
25838 cp_token *sep;
25840 identifier = cp_parser_identifier (parser);
25841 if (identifier == error_mark_node)
25842 return error_mark_node;
25844 list = build_tree_list (NULL_TREE, identifier);
25845 sep = cp_lexer_peek_token (parser->lexer);
25847 while (sep->type == CPP_COMMA)
25849 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25850 identifier = cp_parser_identifier (parser);
25851 if (identifier == error_mark_node)
25852 return list;
25854 list = chainon (list, build_tree_list (NULL_TREE,
25855 identifier));
25856 sep = cp_lexer_peek_token (parser->lexer);
25859 return list;
25862 /* Parse an Objective-C alias declaration.
25864 objc-alias-declaration:
25865 @compatibility_alias identifier identifier ;
25867 This function registers the alias mapping with the Objective-C front end.
25868 It returns nothing. */
25870 static void
25871 cp_parser_objc_alias_declaration (cp_parser* parser)
25873 tree alias, orig;
25875 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25876 alias = cp_parser_identifier (parser);
25877 orig = cp_parser_identifier (parser);
25878 objc_declare_alias (alias, orig);
25879 cp_parser_consume_semicolon_at_end_of_statement (parser);
25882 /* Parse an Objective-C class forward-declaration.
25884 objc-class-declaration:
25885 @class objc-identifier-list ;
25887 The function registers the forward declarations with the Objective-C
25888 front end. It returns nothing. */
25890 static void
25891 cp_parser_objc_class_declaration (cp_parser* parser)
25893 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25894 while (true)
25896 tree id;
25898 id = cp_parser_identifier (parser);
25899 if (id == error_mark_node)
25900 break;
25902 objc_declare_class (id);
25904 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25905 cp_lexer_consume_token (parser->lexer);
25906 else
25907 break;
25909 cp_parser_consume_semicolon_at_end_of_statement (parser);
25912 /* Parse a list of Objective-C protocol references.
25914 objc-protocol-refs-opt:
25915 objc-protocol-refs [opt]
25917 objc-protocol-refs:
25918 < objc-identifier-list >
25920 Returns a TREE_LIST of identifiers, if any. */
25922 static tree
25923 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25925 tree protorefs = NULL_TREE;
25927 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25929 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25930 protorefs = cp_parser_objc_identifier_list (parser);
25931 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25934 return protorefs;
25937 /* Parse a Objective-C visibility specification. */
25939 static void
25940 cp_parser_objc_visibility_spec (cp_parser* parser)
25942 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25944 switch (vis->keyword)
25946 case RID_AT_PRIVATE:
25947 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25948 break;
25949 case RID_AT_PROTECTED:
25950 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25951 break;
25952 case RID_AT_PUBLIC:
25953 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25954 break;
25955 case RID_AT_PACKAGE:
25956 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25957 break;
25958 default:
25959 return;
25962 /* Eat '@private'/'@protected'/'@public'. */
25963 cp_lexer_consume_token (parser->lexer);
25966 /* Parse an Objective-C method type. Return 'true' if it is a class
25967 (+) method, and 'false' if it is an instance (-) method. */
25969 static inline bool
25970 cp_parser_objc_method_type (cp_parser* parser)
25972 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25973 return true;
25974 else
25975 return false;
25978 /* Parse an Objective-C protocol qualifier. */
25980 static tree
25981 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25983 tree quals = NULL_TREE, node;
25984 cp_token *token = cp_lexer_peek_token (parser->lexer);
25986 node = token->u.value;
25988 while (node && identifier_p (node)
25989 && (node == ridpointers [(int) RID_IN]
25990 || node == ridpointers [(int) RID_OUT]
25991 || node == ridpointers [(int) RID_INOUT]
25992 || node == ridpointers [(int) RID_BYCOPY]
25993 || node == ridpointers [(int) RID_BYREF]
25994 || node == ridpointers [(int) RID_ONEWAY]))
25996 quals = tree_cons (NULL_TREE, node, quals);
25997 cp_lexer_consume_token (parser->lexer);
25998 token = cp_lexer_peek_token (parser->lexer);
25999 node = token->u.value;
26002 return quals;
26005 /* Parse an Objective-C typename. */
26007 static tree
26008 cp_parser_objc_typename (cp_parser* parser)
26010 tree type_name = NULL_TREE;
26012 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26014 tree proto_quals, cp_type = NULL_TREE;
26016 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26017 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26019 /* An ObjC type name may consist of just protocol qualifiers, in which
26020 case the type shall default to 'id'. */
26021 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26023 cp_type = cp_parser_type_id (parser);
26025 /* If the type could not be parsed, an error has already
26026 been produced. For error recovery, behave as if it had
26027 not been specified, which will use the default type
26028 'id'. */
26029 if (cp_type == error_mark_node)
26031 cp_type = NULL_TREE;
26032 /* We need to skip to the closing parenthesis as
26033 cp_parser_type_id() does not seem to do it for
26034 us. */
26035 cp_parser_skip_to_closing_parenthesis (parser,
26036 /*recovering=*/true,
26037 /*or_comma=*/false,
26038 /*consume_paren=*/false);
26042 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26043 type_name = build_tree_list (proto_quals, cp_type);
26046 return type_name;
26049 /* Check to see if TYPE refers to an Objective-C selector name. */
26051 static bool
26052 cp_parser_objc_selector_p (enum cpp_ttype type)
26054 return (type == CPP_NAME || type == CPP_KEYWORD
26055 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26056 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26057 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26058 || type == CPP_XOR || type == CPP_XOR_EQ);
26061 /* Parse an Objective-C selector. */
26063 static tree
26064 cp_parser_objc_selector (cp_parser* parser)
26066 cp_token *token = cp_lexer_consume_token (parser->lexer);
26068 if (!cp_parser_objc_selector_p (token->type))
26070 error_at (token->location, "invalid Objective-C++ selector name");
26071 return error_mark_node;
26074 /* C++ operator names are allowed to appear in ObjC selectors. */
26075 switch (token->type)
26077 case CPP_AND_AND: return get_identifier ("and");
26078 case CPP_AND_EQ: return get_identifier ("and_eq");
26079 case CPP_AND: return get_identifier ("bitand");
26080 case CPP_OR: return get_identifier ("bitor");
26081 case CPP_COMPL: return get_identifier ("compl");
26082 case CPP_NOT: return get_identifier ("not");
26083 case CPP_NOT_EQ: return get_identifier ("not_eq");
26084 case CPP_OR_OR: return get_identifier ("or");
26085 case CPP_OR_EQ: return get_identifier ("or_eq");
26086 case CPP_XOR: return get_identifier ("xor");
26087 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26088 default: return token->u.value;
26092 /* Parse an Objective-C params list. */
26094 static tree
26095 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26097 tree params = NULL_TREE;
26098 bool maybe_unary_selector_p = true;
26099 cp_token *token = cp_lexer_peek_token (parser->lexer);
26101 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26103 tree selector = NULL_TREE, type_name, identifier;
26104 tree parm_attr = NULL_TREE;
26106 if (token->keyword == RID_ATTRIBUTE)
26107 break;
26109 if (token->type != CPP_COLON)
26110 selector = cp_parser_objc_selector (parser);
26112 /* Detect if we have a unary selector. */
26113 if (maybe_unary_selector_p
26114 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26116 params = selector; /* Might be followed by attributes. */
26117 break;
26120 maybe_unary_selector_p = false;
26121 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26123 /* Something went quite wrong. There should be a colon
26124 here, but there is not. Stop parsing parameters. */
26125 break;
26127 type_name = cp_parser_objc_typename (parser);
26128 /* New ObjC allows attributes on parameters too. */
26129 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26130 parm_attr = cp_parser_attributes_opt (parser);
26131 identifier = cp_parser_identifier (parser);
26133 params
26134 = chainon (params,
26135 objc_build_keyword_decl (selector,
26136 type_name,
26137 identifier,
26138 parm_attr));
26140 token = cp_lexer_peek_token (parser->lexer);
26143 if (params == NULL_TREE)
26145 cp_parser_error (parser, "objective-c++ method declaration is expected");
26146 return error_mark_node;
26149 /* We allow tail attributes for the method. */
26150 if (token->keyword == RID_ATTRIBUTE)
26152 *attributes = cp_parser_attributes_opt (parser);
26153 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26154 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26155 return params;
26156 cp_parser_error (parser,
26157 "method attributes must be specified at the end");
26158 return error_mark_node;
26161 if (params == NULL_TREE)
26163 cp_parser_error (parser, "objective-c++ method declaration is expected");
26164 return error_mark_node;
26166 return params;
26169 /* Parse the non-keyword Objective-C params. */
26171 static tree
26172 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26173 tree* attributes)
26175 tree params = make_node (TREE_LIST);
26176 cp_token *token = cp_lexer_peek_token (parser->lexer);
26177 *ellipsisp = false; /* Initially, assume no ellipsis. */
26179 while (token->type == CPP_COMMA)
26181 cp_parameter_declarator *parmdecl;
26182 tree parm;
26184 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26185 token = cp_lexer_peek_token (parser->lexer);
26187 if (token->type == CPP_ELLIPSIS)
26189 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26190 *ellipsisp = true;
26191 token = cp_lexer_peek_token (parser->lexer);
26192 break;
26195 /* TODO: parse attributes for tail parameters. */
26196 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26197 parm = grokdeclarator (parmdecl->declarator,
26198 &parmdecl->decl_specifiers,
26199 PARM, /*initialized=*/0,
26200 /*attrlist=*/NULL);
26202 chainon (params, build_tree_list (NULL_TREE, parm));
26203 token = cp_lexer_peek_token (parser->lexer);
26206 /* We allow tail attributes for the method. */
26207 if (token->keyword == RID_ATTRIBUTE)
26209 if (*attributes == NULL_TREE)
26211 *attributes = cp_parser_attributes_opt (parser);
26212 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26213 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26214 return params;
26216 else
26217 /* We have an error, but parse the attributes, so that we can
26218 carry on. */
26219 *attributes = cp_parser_attributes_opt (parser);
26221 cp_parser_error (parser,
26222 "method attributes must be specified at the end");
26223 return error_mark_node;
26226 return params;
26229 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26231 static void
26232 cp_parser_objc_interstitial_code (cp_parser* parser)
26234 cp_token *token = cp_lexer_peek_token (parser->lexer);
26236 /* If the next token is `extern' and the following token is a string
26237 literal, then we have a linkage specification. */
26238 if (token->keyword == RID_EXTERN
26239 && cp_parser_is_pure_string_literal
26240 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26241 cp_parser_linkage_specification (parser);
26242 /* Handle #pragma, if any. */
26243 else if (token->type == CPP_PRAGMA)
26244 cp_parser_pragma (parser, pragma_objc_icode);
26245 /* Allow stray semicolons. */
26246 else if (token->type == CPP_SEMICOLON)
26247 cp_lexer_consume_token (parser->lexer);
26248 /* Mark methods as optional or required, when building protocols. */
26249 else if (token->keyword == RID_AT_OPTIONAL)
26251 cp_lexer_consume_token (parser->lexer);
26252 objc_set_method_opt (true);
26254 else if (token->keyword == RID_AT_REQUIRED)
26256 cp_lexer_consume_token (parser->lexer);
26257 objc_set_method_opt (false);
26259 else if (token->keyword == RID_NAMESPACE)
26260 cp_parser_namespace_definition (parser);
26261 /* Other stray characters must generate errors. */
26262 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26264 cp_lexer_consume_token (parser->lexer);
26265 error ("stray %qs between Objective-C++ methods",
26266 token->type == CPP_OPEN_BRACE ? "{" : "}");
26268 /* Finally, try to parse a block-declaration, or a function-definition. */
26269 else
26270 cp_parser_block_declaration (parser, /*statement_p=*/false);
26273 /* Parse a method signature. */
26275 static tree
26276 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26278 tree rettype, kwdparms, optparms;
26279 bool ellipsis = false;
26280 bool is_class_method;
26282 is_class_method = cp_parser_objc_method_type (parser);
26283 rettype = cp_parser_objc_typename (parser);
26284 *attributes = NULL_TREE;
26285 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26286 if (kwdparms == error_mark_node)
26287 return error_mark_node;
26288 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26289 if (optparms == error_mark_node)
26290 return error_mark_node;
26292 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26295 static bool
26296 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26298 tree tattr;
26299 cp_lexer_save_tokens (parser->lexer);
26300 tattr = cp_parser_attributes_opt (parser);
26301 gcc_assert (tattr) ;
26303 /* If the attributes are followed by a method introducer, this is not allowed.
26304 Dump the attributes and flag the situation. */
26305 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26306 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26307 return true;
26309 /* Otherwise, the attributes introduce some interstitial code, possibly so
26310 rewind to allow that check. */
26311 cp_lexer_rollback_tokens (parser->lexer);
26312 return false;
26315 /* Parse an Objective-C method prototype list. */
26317 static void
26318 cp_parser_objc_method_prototype_list (cp_parser* parser)
26320 cp_token *token = cp_lexer_peek_token (parser->lexer);
26322 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26324 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26326 tree attributes, sig;
26327 bool is_class_method;
26328 if (token->type == CPP_PLUS)
26329 is_class_method = true;
26330 else
26331 is_class_method = false;
26332 sig = cp_parser_objc_method_signature (parser, &attributes);
26333 if (sig == error_mark_node)
26335 cp_parser_skip_to_end_of_block_or_statement (parser);
26336 token = cp_lexer_peek_token (parser->lexer);
26337 continue;
26339 objc_add_method_declaration (is_class_method, sig, attributes);
26340 cp_parser_consume_semicolon_at_end_of_statement (parser);
26342 else if (token->keyword == RID_AT_PROPERTY)
26343 cp_parser_objc_at_property_declaration (parser);
26344 else if (token->keyword == RID_ATTRIBUTE
26345 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26346 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26347 OPT_Wattributes,
26348 "prefix attributes are ignored for methods");
26349 else
26350 /* Allow for interspersed non-ObjC++ code. */
26351 cp_parser_objc_interstitial_code (parser);
26353 token = cp_lexer_peek_token (parser->lexer);
26356 if (token->type != CPP_EOF)
26357 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26358 else
26359 cp_parser_error (parser, "expected %<@end%>");
26361 objc_finish_interface ();
26364 /* Parse an Objective-C method definition list. */
26366 static void
26367 cp_parser_objc_method_definition_list (cp_parser* parser)
26369 cp_token *token = cp_lexer_peek_token (parser->lexer);
26371 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26373 tree meth;
26375 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26377 cp_token *ptk;
26378 tree sig, attribute;
26379 bool is_class_method;
26380 if (token->type == CPP_PLUS)
26381 is_class_method = true;
26382 else
26383 is_class_method = false;
26384 push_deferring_access_checks (dk_deferred);
26385 sig = cp_parser_objc_method_signature (parser, &attribute);
26386 if (sig == error_mark_node)
26388 cp_parser_skip_to_end_of_block_or_statement (parser);
26389 token = cp_lexer_peek_token (parser->lexer);
26390 continue;
26392 objc_start_method_definition (is_class_method, sig, attribute,
26393 NULL_TREE);
26395 /* For historical reasons, we accept an optional semicolon. */
26396 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26397 cp_lexer_consume_token (parser->lexer);
26399 ptk = cp_lexer_peek_token (parser->lexer);
26400 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26401 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26403 perform_deferred_access_checks (tf_warning_or_error);
26404 stop_deferring_access_checks ();
26405 meth = cp_parser_function_definition_after_declarator (parser,
26406 false);
26407 pop_deferring_access_checks ();
26408 objc_finish_method_definition (meth);
26411 /* The following case will be removed once @synthesize is
26412 completely implemented. */
26413 else if (token->keyword == RID_AT_PROPERTY)
26414 cp_parser_objc_at_property_declaration (parser);
26415 else if (token->keyword == RID_AT_SYNTHESIZE)
26416 cp_parser_objc_at_synthesize_declaration (parser);
26417 else if (token->keyword == RID_AT_DYNAMIC)
26418 cp_parser_objc_at_dynamic_declaration (parser);
26419 else if (token->keyword == RID_ATTRIBUTE
26420 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26421 warning_at (token->location, OPT_Wattributes,
26422 "prefix attributes are ignored for methods");
26423 else
26424 /* Allow for interspersed non-ObjC++ code. */
26425 cp_parser_objc_interstitial_code (parser);
26427 token = cp_lexer_peek_token (parser->lexer);
26430 if (token->type != CPP_EOF)
26431 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26432 else
26433 cp_parser_error (parser, "expected %<@end%>");
26435 objc_finish_implementation ();
26438 /* Parse Objective-C ivars. */
26440 static void
26441 cp_parser_objc_class_ivars (cp_parser* parser)
26443 cp_token *token = cp_lexer_peek_token (parser->lexer);
26445 if (token->type != CPP_OPEN_BRACE)
26446 return; /* No ivars specified. */
26448 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26449 token = cp_lexer_peek_token (parser->lexer);
26451 while (token->type != CPP_CLOSE_BRACE
26452 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26454 cp_decl_specifier_seq declspecs;
26455 int decl_class_or_enum_p;
26456 tree prefix_attributes;
26458 cp_parser_objc_visibility_spec (parser);
26460 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26461 break;
26463 cp_parser_decl_specifier_seq (parser,
26464 CP_PARSER_FLAGS_OPTIONAL,
26465 &declspecs,
26466 &decl_class_or_enum_p);
26468 /* auto, register, static, extern, mutable. */
26469 if (declspecs.storage_class != sc_none)
26471 cp_parser_error (parser, "invalid type for instance variable");
26472 declspecs.storage_class = sc_none;
26475 /* thread_local. */
26476 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26478 cp_parser_error (parser, "invalid type for instance variable");
26479 declspecs.locations[ds_thread] = 0;
26482 /* typedef. */
26483 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26485 cp_parser_error (parser, "invalid type for instance variable");
26486 declspecs.locations[ds_typedef] = 0;
26489 prefix_attributes = declspecs.attributes;
26490 declspecs.attributes = NULL_TREE;
26492 /* Keep going until we hit the `;' at the end of the
26493 declaration. */
26494 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26496 tree width = NULL_TREE, attributes, first_attribute, decl;
26497 cp_declarator *declarator = NULL;
26498 int ctor_dtor_or_conv_p;
26500 /* Check for a (possibly unnamed) bitfield declaration. */
26501 token = cp_lexer_peek_token (parser->lexer);
26502 if (token->type == CPP_COLON)
26503 goto eat_colon;
26505 if (token->type == CPP_NAME
26506 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26507 == CPP_COLON))
26509 /* Get the name of the bitfield. */
26510 declarator = make_id_declarator (NULL_TREE,
26511 cp_parser_identifier (parser),
26512 sfk_none);
26514 eat_colon:
26515 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26516 /* Get the width of the bitfield. */
26517 width
26518 = cp_parser_constant_expression (parser);
26520 else
26522 /* Parse the declarator. */
26523 declarator
26524 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26525 &ctor_dtor_or_conv_p,
26526 /*parenthesized_p=*/NULL,
26527 /*member_p=*/false,
26528 /*friend_p=*/false);
26531 /* Look for attributes that apply to the ivar. */
26532 attributes = cp_parser_attributes_opt (parser);
26533 /* Remember which attributes are prefix attributes and
26534 which are not. */
26535 first_attribute = attributes;
26536 /* Combine the attributes. */
26537 attributes = chainon (prefix_attributes, attributes);
26539 if (width)
26540 /* Create the bitfield declaration. */
26541 decl = grokbitfield (declarator, &declspecs,
26542 width,
26543 attributes);
26544 else
26545 decl = grokfield (declarator, &declspecs,
26546 NULL_TREE, /*init_const_expr_p=*/false,
26547 NULL_TREE, attributes);
26549 /* Add the instance variable. */
26550 if (decl != error_mark_node && decl != NULL_TREE)
26551 objc_add_instance_variable (decl);
26553 /* Reset PREFIX_ATTRIBUTES. */
26554 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26555 attributes = TREE_CHAIN (attributes);
26556 if (attributes)
26557 TREE_CHAIN (attributes) = NULL_TREE;
26559 token = cp_lexer_peek_token (parser->lexer);
26561 if (token->type == CPP_COMMA)
26563 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26564 continue;
26566 break;
26569 cp_parser_consume_semicolon_at_end_of_statement (parser);
26570 token = cp_lexer_peek_token (parser->lexer);
26573 if (token->keyword == RID_AT_END)
26574 cp_parser_error (parser, "expected %<}%>");
26576 /* Do not consume the RID_AT_END, so it will be read again as terminating
26577 the @interface of @implementation. */
26578 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26579 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26581 /* For historical reasons, we accept an optional semicolon. */
26582 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26583 cp_lexer_consume_token (parser->lexer);
26586 /* Parse an Objective-C protocol declaration. */
26588 static void
26589 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26591 tree proto, protorefs;
26592 cp_token *tok;
26594 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26597 tok = cp_lexer_peek_token (parser->lexer);
26598 error_at (tok->location, "identifier expected after %<@protocol%>");
26599 cp_parser_consume_semicolon_at_end_of_statement (parser);
26600 return;
26603 /* See if we have a forward declaration or a definition. */
26604 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26606 /* Try a forward declaration first. */
26607 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26609 while (true)
26611 tree id;
26613 id = cp_parser_identifier (parser);
26614 if (id == error_mark_node)
26615 break;
26617 objc_declare_protocol (id, attributes);
26619 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26620 cp_lexer_consume_token (parser->lexer);
26621 else
26622 break;
26624 cp_parser_consume_semicolon_at_end_of_statement (parser);
26627 /* Ok, we got a full-fledged definition (or at least should). */
26628 else
26630 proto = cp_parser_identifier (parser);
26631 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26632 objc_start_protocol (proto, protorefs, attributes);
26633 cp_parser_objc_method_prototype_list (parser);
26637 /* Parse an Objective-C superclass or category. */
26639 static void
26640 cp_parser_objc_superclass_or_category (cp_parser *parser,
26641 bool iface_p,
26642 tree *super,
26643 tree *categ, bool *is_class_extension)
26645 cp_token *next = cp_lexer_peek_token (parser->lexer);
26647 *super = *categ = NULL_TREE;
26648 *is_class_extension = false;
26649 if (next->type == CPP_COLON)
26651 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26652 *super = cp_parser_identifier (parser);
26654 else if (next->type == CPP_OPEN_PAREN)
26656 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26658 /* If there is no category name, and this is an @interface, we
26659 have a class extension. */
26660 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26662 *categ = NULL_TREE;
26663 *is_class_extension = true;
26665 else
26666 *categ = cp_parser_identifier (parser);
26668 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26672 /* Parse an Objective-C class interface. */
26674 static void
26675 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26677 tree name, super, categ, protos;
26678 bool is_class_extension;
26680 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26681 name = cp_parser_identifier (parser);
26682 if (name == error_mark_node)
26684 /* It's hard to recover because even if valid @interface stuff
26685 is to follow, we can't compile it (or validate it) if we
26686 don't even know which class it refers to. Let's assume this
26687 was a stray '@interface' token in the stream and skip it.
26689 return;
26691 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26692 &is_class_extension);
26693 protos = cp_parser_objc_protocol_refs_opt (parser);
26695 /* We have either a class or a category on our hands. */
26696 if (categ || is_class_extension)
26697 objc_start_category_interface (name, categ, protos, attributes);
26698 else
26700 objc_start_class_interface (name, super, protos, attributes);
26701 /* Handle instance variable declarations, if any. */
26702 cp_parser_objc_class_ivars (parser);
26703 objc_continue_interface ();
26706 cp_parser_objc_method_prototype_list (parser);
26709 /* Parse an Objective-C class implementation. */
26711 static void
26712 cp_parser_objc_class_implementation (cp_parser* parser)
26714 tree name, super, categ;
26715 bool is_class_extension;
26717 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26718 name = cp_parser_identifier (parser);
26719 if (name == error_mark_node)
26721 /* It's hard to recover because even if valid @implementation
26722 stuff is to follow, we can't compile it (or validate it) if
26723 we don't even know which class it refers to. Let's assume
26724 this was a stray '@implementation' token in the stream and
26725 skip it.
26727 return;
26729 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26730 &is_class_extension);
26732 /* We have either a class or a category on our hands. */
26733 if (categ)
26734 objc_start_category_implementation (name, categ);
26735 else
26737 objc_start_class_implementation (name, super);
26738 /* Handle instance variable declarations, if any. */
26739 cp_parser_objc_class_ivars (parser);
26740 objc_continue_implementation ();
26743 cp_parser_objc_method_definition_list (parser);
26746 /* Consume the @end token and finish off the implementation. */
26748 static void
26749 cp_parser_objc_end_implementation (cp_parser* parser)
26751 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26752 objc_finish_implementation ();
26755 /* Parse an Objective-C declaration. */
26757 static void
26758 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26760 /* Try to figure out what kind of declaration is present. */
26761 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26763 if (attributes)
26764 switch (kwd->keyword)
26766 case RID_AT_ALIAS:
26767 case RID_AT_CLASS:
26768 case RID_AT_END:
26769 error_at (kwd->location, "attributes may not be specified before"
26770 " the %<@%D%> Objective-C++ keyword",
26771 kwd->u.value);
26772 attributes = NULL;
26773 break;
26774 case RID_AT_IMPLEMENTATION:
26775 warning_at (kwd->location, OPT_Wattributes,
26776 "prefix attributes are ignored before %<@%D%>",
26777 kwd->u.value);
26778 attributes = NULL;
26779 default:
26780 break;
26783 switch (kwd->keyword)
26785 case RID_AT_ALIAS:
26786 cp_parser_objc_alias_declaration (parser);
26787 break;
26788 case RID_AT_CLASS:
26789 cp_parser_objc_class_declaration (parser);
26790 break;
26791 case RID_AT_PROTOCOL:
26792 cp_parser_objc_protocol_declaration (parser, attributes);
26793 break;
26794 case RID_AT_INTERFACE:
26795 cp_parser_objc_class_interface (parser, attributes);
26796 break;
26797 case RID_AT_IMPLEMENTATION:
26798 cp_parser_objc_class_implementation (parser);
26799 break;
26800 case RID_AT_END:
26801 cp_parser_objc_end_implementation (parser);
26802 break;
26803 default:
26804 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26805 kwd->u.value);
26806 cp_parser_skip_to_end_of_block_or_statement (parser);
26810 /* Parse an Objective-C try-catch-finally statement.
26812 objc-try-catch-finally-stmt:
26813 @try compound-statement objc-catch-clause-seq [opt]
26814 objc-finally-clause [opt]
26816 objc-catch-clause-seq:
26817 objc-catch-clause objc-catch-clause-seq [opt]
26819 objc-catch-clause:
26820 @catch ( objc-exception-declaration ) compound-statement
26822 objc-finally-clause:
26823 @finally compound-statement
26825 objc-exception-declaration:
26826 parameter-declaration
26827 '...'
26829 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26831 Returns NULL_TREE.
26833 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26834 for C. Keep them in sync. */
26836 static tree
26837 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26839 location_t location;
26840 tree stmt;
26842 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26843 location = cp_lexer_peek_token (parser->lexer)->location;
26844 objc_maybe_warn_exceptions (location);
26845 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26846 node, lest it get absorbed into the surrounding block. */
26847 stmt = push_stmt_list ();
26848 cp_parser_compound_statement (parser, NULL, false, false);
26849 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26851 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26853 cp_parameter_declarator *parm;
26854 tree parameter_declaration = error_mark_node;
26855 bool seen_open_paren = false;
26857 cp_lexer_consume_token (parser->lexer);
26858 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26859 seen_open_paren = true;
26860 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26862 /* We have "@catch (...)" (where the '...' are literally
26863 what is in the code). Skip the '...'.
26864 parameter_declaration is set to NULL_TREE, and
26865 objc_being_catch_clauses() knows that that means
26866 '...'. */
26867 cp_lexer_consume_token (parser->lexer);
26868 parameter_declaration = NULL_TREE;
26870 else
26872 /* We have "@catch (NSException *exception)" or something
26873 like that. Parse the parameter declaration. */
26874 parm = cp_parser_parameter_declaration (parser, false, NULL);
26875 if (parm == NULL)
26876 parameter_declaration = error_mark_node;
26877 else
26878 parameter_declaration = grokdeclarator (parm->declarator,
26879 &parm->decl_specifiers,
26880 PARM, /*initialized=*/0,
26881 /*attrlist=*/NULL);
26883 if (seen_open_paren)
26884 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26885 else
26887 /* If there was no open parenthesis, we are recovering from
26888 an error, and we are trying to figure out what mistake
26889 the user has made. */
26891 /* If there is an immediate closing parenthesis, the user
26892 probably forgot the opening one (ie, they typed "@catch
26893 NSException *e)". Parse the closing parenthesis and keep
26894 going. */
26895 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26896 cp_lexer_consume_token (parser->lexer);
26898 /* If these is no immediate closing parenthesis, the user
26899 probably doesn't know that parenthesis are required at
26900 all (ie, they typed "@catch NSException *e"). So, just
26901 forget about the closing parenthesis and keep going. */
26903 objc_begin_catch_clause (parameter_declaration);
26904 cp_parser_compound_statement (parser, NULL, false, false);
26905 objc_finish_catch_clause ();
26907 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26909 cp_lexer_consume_token (parser->lexer);
26910 location = cp_lexer_peek_token (parser->lexer)->location;
26911 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26912 node, lest it get absorbed into the surrounding block. */
26913 stmt = push_stmt_list ();
26914 cp_parser_compound_statement (parser, NULL, false, false);
26915 objc_build_finally_clause (location, pop_stmt_list (stmt));
26918 return objc_finish_try_stmt ();
26921 /* Parse an Objective-C synchronized statement.
26923 objc-synchronized-stmt:
26924 @synchronized ( expression ) compound-statement
26926 Returns NULL_TREE. */
26928 static tree
26929 cp_parser_objc_synchronized_statement (cp_parser *parser)
26931 location_t location;
26932 tree lock, stmt;
26934 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26936 location = cp_lexer_peek_token (parser->lexer)->location;
26937 objc_maybe_warn_exceptions (location);
26938 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26939 lock = cp_parser_expression (parser);
26940 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26942 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26943 node, lest it get absorbed into the surrounding block. */
26944 stmt = push_stmt_list ();
26945 cp_parser_compound_statement (parser, NULL, false, false);
26947 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26950 /* Parse an Objective-C throw statement.
26952 objc-throw-stmt:
26953 @throw assignment-expression [opt] ;
26955 Returns a constructed '@throw' statement. */
26957 static tree
26958 cp_parser_objc_throw_statement (cp_parser *parser)
26960 tree expr = NULL_TREE;
26961 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26963 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26965 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26966 expr = cp_parser_expression (parser);
26968 cp_parser_consume_semicolon_at_end_of_statement (parser);
26970 return objc_build_throw_stmt (loc, expr);
26973 /* Parse an Objective-C statement. */
26975 static tree
26976 cp_parser_objc_statement (cp_parser * parser)
26978 /* Try to figure out what kind of declaration is present. */
26979 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26981 switch (kwd->keyword)
26983 case RID_AT_TRY:
26984 return cp_parser_objc_try_catch_finally_statement (parser);
26985 case RID_AT_SYNCHRONIZED:
26986 return cp_parser_objc_synchronized_statement (parser);
26987 case RID_AT_THROW:
26988 return cp_parser_objc_throw_statement (parser);
26989 default:
26990 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26991 kwd->u.value);
26992 cp_parser_skip_to_end_of_block_or_statement (parser);
26995 return error_mark_node;
26998 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26999 look ahead to see if an objc keyword follows the attributes. This
27000 is to detect the use of prefix attributes on ObjC @interface and
27001 @protocol. */
27003 static bool
27004 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27006 cp_lexer_save_tokens (parser->lexer);
27007 *attrib = cp_parser_attributes_opt (parser);
27008 gcc_assert (*attrib);
27009 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27011 cp_lexer_commit_tokens (parser->lexer);
27012 return true;
27014 cp_lexer_rollback_tokens (parser->lexer);
27015 return false;
27018 /* This routine is a minimal replacement for
27019 c_parser_struct_declaration () used when parsing the list of
27020 types/names or ObjC++ properties. For example, when parsing the
27021 code
27023 @property (readonly) int a, b, c;
27025 this function is responsible for parsing "int a, int b, int c" and
27026 returning the declarations as CHAIN of DECLs.
27028 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27029 similar parsing. */
27030 static tree
27031 cp_parser_objc_struct_declaration (cp_parser *parser)
27033 tree decls = NULL_TREE;
27034 cp_decl_specifier_seq declspecs;
27035 int decl_class_or_enum_p;
27036 tree prefix_attributes;
27038 cp_parser_decl_specifier_seq (parser,
27039 CP_PARSER_FLAGS_NONE,
27040 &declspecs,
27041 &decl_class_or_enum_p);
27043 if (declspecs.type == error_mark_node)
27044 return error_mark_node;
27046 /* auto, register, static, extern, mutable. */
27047 if (declspecs.storage_class != sc_none)
27049 cp_parser_error (parser, "invalid type for property");
27050 declspecs.storage_class = sc_none;
27053 /* thread_local. */
27054 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27056 cp_parser_error (parser, "invalid type for property");
27057 declspecs.locations[ds_thread] = 0;
27060 /* typedef. */
27061 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27063 cp_parser_error (parser, "invalid type for property");
27064 declspecs.locations[ds_typedef] = 0;
27067 prefix_attributes = declspecs.attributes;
27068 declspecs.attributes = NULL_TREE;
27070 /* Keep going until we hit the `;' at the end of the declaration. */
27071 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27073 tree attributes, first_attribute, decl;
27074 cp_declarator *declarator;
27075 cp_token *token;
27077 /* Parse the declarator. */
27078 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27079 NULL, NULL, false, false);
27081 /* Look for attributes that apply to the ivar. */
27082 attributes = cp_parser_attributes_opt (parser);
27083 /* Remember which attributes are prefix attributes and
27084 which are not. */
27085 first_attribute = attributes;
27086 /* Combine the attributes. */
27087 attributes = chainon (prefix_attributes, attributes);
27089 decl = grokfield (declarator, &declspecs,
27090 NULL_TREE, /*init_const_expr_p=*/false,
27091 NULL_TREE, attributes);
27093 if (decl == error_mark_node || decl == NULL_TREE)
27094 return error_mark_node;
27096 /* Reset PREFIX_ATTRIBUTES. */
27097 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27098 attributes = TREE_CHAIN (attributes);
27099 if (attributes)
27100 TREE_CHAIN (attributes) = NULL_TREE;
27102 DECL_CHAIN (decl) = decls;
27103 decls = decl;
27105 token = cp_lexer_peek_token (parser->lexer);
27106 if (token->type == CPP_COMMA)
27108 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27109 continue;
27111 else
27112 break;
27114 return decls;
27117 /* Parse an Objective-C @property declaration. The syntax is:
27119 objc-property-declaration:
27120 '@property' objc-property-attributes[opt] struct-declaration ;
27122 objc-property-attributes:
27123 '(' objc-property-attribute-list ')'
27125 objc-property-attribute-list:
27126 objc-property-attribute
27127 objc-property-attribute-list, objc-property-attribute
27129 objc-property-attribute
27130 'getter' = identifier
27131 'setter' = identifier
27132 'readonly'
27133 'readwrite'
27134 'assign'
27135 'retain'
27136 'copy'
27137 'nonatomic'
27139 For example:
27140 @property NSString *name;
27141 @property (readonly) id object;
27142 @property (retain, nonatomic, getter=getTheName) id name;
27143 @property int a, b, c;
27145 PS: This function is identical to
27146 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27147 static void
27148 cp_parser_objc_at_property_declaration (cp_parser *parser)
27150 /* The following variables hold the attributes of the properties as
27151 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27152 seen. When we see an attribute, we set them to 'true' (if they
27153 are boolean properties) or to the identifier (if they have an
27154 argument, ie, for getter and setter). Note that here we only
27155 parse the list of attributes, check the syntax and accumulate the
27156 attributes that we find. objc_add_property_declaration() will
27157 then process the information. */
27158 bool property_assign = false;
27159 bool property_copy = false;
27160 tree property_getter_ident = NULL_TREE;
27161 bool property_nonatomic = false;
27162 bool property_readonly = false;
27163 bool property_readwrite = false;
27164 bool property_retain = false;
27165 tree property_setter_ident = NULL_TREE;
27167 /* 'properties' is the list of properties that we read. Usually a
27168 single one, but maybe more (eg, in "@property int a, b, c;" there
27169 are three). */
27170 tree properties;
27171 location_t loc;
27173 loc = cp_lexer_peek_token (parser->lexer)->location;
27175 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27177 /* Parse the optional attribute list... */
27178 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27180 /* Eat the '('. */
27181 cp_lexer_consume_token (parser->lexer);
27183 while (true)
27185 bool syntax_error = false;
27186 cp_token *token = cp_lexer_peek_token (parser->lexer);
27187 enum rid keyword;
27189 if (token->type != CPP_NAME)
27191 cp_parser_error (parser, "expected identifier");
27192 break;
27194 keyword = C_RID_CODE (token->u.value);
27195 cp_lexer_consume_token (parser->lexer);
27196 switch (keyword)
27198 case RID_ASSIGN: property_assign = true; break;
27199 case RID_COPY: property_copy = true; break;
27200 case RID_NONATOMIC: property_nonatomic = true; break;
27201 case RID_READONLY: property_readonly = true; break;
27202 case RID_READWRITE: property_readwrite = true; break;
27203 case RID_RETAIN: property_retain = true; break;
27205 case RID_GETTER:
27206 case RID_SETTER:
27207 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27209 if (keyword == RID_GETTER)
27210 cp_parser_error (parser,
27211 "missing %<=%> (after %<getter%> attribute)");
27212 else
27213 cp_parser_error (parser,
27214 "missing %<=%> (after %<setter%> attribute)");
27215 syntax_error = true;
27216 break;
27218 cp_lexer_consume_token (parser->lexer); /* eat the = */
27219 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27221 cp_parser_error (parser, "expected identifier");
27222 syntax_error = true;
27223 break;
27225 if (keyword == RID_SETTER)
27227 if (property_setter_ident != NULL_TREE)
27229 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27230 cp_lexer_consume_token (parser->lexer);
27232 else
27233 property_setter_ident = cp_parser_objc_selector (parser);
27234 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27235 cp_parser_error (parser, "setter name must terminate with %<:%>");
27236 else
27237 cp_lexer_consume_token (parser->lexer);
27239 else
27241 if (property_getter_ident != NULL_TREE)
27243 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27244 cp_lexer_consume_token (parser->lexer);
27246 else
27247 property_getter_ident = cp_parser_objc_selector (parser);
27249 break;
27250 default:
27251 cp_parser_error (parser, "unknown property attribute");
27252 syntax_error = true;
27253 break;
27256 if (syntax_error)
27257 break;
27259 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27260 cp_lexer_consume_token (parser->lexer);
27261 else
27262 break;
27265 /* FIXME: "@property (setter, assign);" will generate a spurious
27266 "error: expected ‘)’ before ‘,’ token". This is because
27267 cp_parser_require, unlike the C counterpart, will produce an
27268 error even if we are in error recovery. */
27269 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27271 cp_parser_skip_to_closing_parenthesis (parser,
27272 /*recovering=*/true,
27273 /*or_comma=*/false,
27274 /*consume_paren=*/true);
27278 /* ... and the property declaration(s). */
27279 properties = cp_parser_objc_struct_declaration (parser);
27281 if (properties == error_mark_node)
27283 cp_parser_skip_to_end_of_statement (parser);
27284 /* If the next token is now a `;', consume it. */
27285 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27286 cp_lexer_consume_token (parser->lexer);
27287 return;
27290 if (properties == NULL_TREE)
27291 cp_parser_error (parser, "expected identifier");
27292 else
27294 /* Comma-separated properties are chained together in
27295 reverse order; add them one by one. */
27296 properties = nreverse (properties);
27298 for (; properties; properties = TREE_CHAIN (properties))
27299 objc_add_property_declaration (loc, copy_node (properties),
27300 property_readonly, property_readwrite,
27301 property_assign, property_retain,
27302 property_copy, property_nonatomic,
27303 property_getter_ident, property_setter_ident);
27306 cp_parser_consume_semicolon_at_end_of_statement (parser);
27309 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27311 objc-synthesize-declaration:
27312 @synthesize objc-synthesize-identifier-list ;
27314 objc-synthesize-identifier-list:
27315 objc-synthesize-identifier
27316 objc-synthesize-identifier-list, objc-synthesize-identifier
27318 objc-synthesize-identifier
27319 identifier
27320 identifier = identifier
27322 For example:
27323 @synthesize MyProperty;
27324 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27326 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27327 for C. Keep them in sync.
27329 static void
27330 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27332 tree list = NULL_TREE;
27333 location_t loc;
27334 loc = cp_lexer_peek_token (parser->lexer)->location;
27336 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27337 while (true)
27339 tree property, ivar;
27340 property = cp_parser_identifier (parser);
27341 if (property == error_mark_node)
27343 cp_parser_consume_semicolon_at_end_of_statement (parser);
27344 return;
27346 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27348 cp_lexer_consume_token (parser->lexer);
27349 ivar = cp_parser_identifier (parser);
27350 if (ivar == error_mark_node)
27352 cp_parser_consume_semicolon_at_end_of_statement (parser);
27353 return;
27356 else
27357 ivar = NULL_TREE;
27358 list = chainon (list, build_tree_list (ivar, property));
27359 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27360 cp_lexer_consume_token (parser->lexer);
27361 else
27362 break;
27364 cp_parser_consume_semicolon_at_end_of_statement (parser);
27365 objc_add_synthesize_declaration (loc, list);
27368 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27370 objc-dynamic-declaration:
27371 @dynamic identifier-list ;
27373 For example:
27374 @dynamic MyProperty;
27375 @dynamic MyProperty, AnotherProperty;
27377 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27378 for C. Keep them in sync.
27380 static void
27381 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27383 tree list = NULL_TREE;
27384 location_t loc;
27385 loc = cp_lexer_peek_token (parser->lexer)->location;
27387 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27388 while (true)
27390 tree property;
27391 property = cp_parser_identifier (parser);
27392 if (property == error_mark_node)
27394 cp_parser_consume_semicolon_at_end_of_statement (parser);
27395 return;
27397 list = chainon (list, build_tree_list (NULL, property));
27398 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27399 cp_lexer_consume_token (parser->lexer);
27400 else
27401 break;
27403 cp_parser_consume_semicolon_at_end_of_statement (parser);
27404 objc_add_dynamic_declaration (loc, list);
27408 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27410 /* Returns name of the next clause.
27411 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27412 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27413 returned and the token is consumed. */
27415 static pragma_omp_clause
27416 cp_parser_omp_clause_name (cp_parser *parser)
27418 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27420 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27421 result = PRAGMA_OMP_CLAUSE_IF;
27422 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27423 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27424 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
27425 result = PRAGMA_OMP_CLAUSE_DELETE;
27426 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27427 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27428 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27429 result = PRAGMA_OMP_CLAUSE_FOR;
27430 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27432 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27433 const char *p = IDENTIFIER_POINTER (id);
27435 switch (p[0])
27437 case 'a':
27438 if (!strcmp ("aligned", p))
27439 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27440 else if (!strcmp ("async", p))
27441 result = PRAGMA_OMP_CLAUSE_ASYNC;
27442 break;
27443 case 'c':
27444 if (!strcmp ("collapse", p))
27445 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27446 else if (!strcmp ("copy", p))
27447 result = PRAGMA_OMP_CLAUSE_COPY;
27448 else if (!strcmp ("copyin", p))
27449 result = PRAGMA_OMP_CLAUSE_COPYIN;
27450 else if (!strcmp ("copyout", p))
27451 result = PRAGMA_OMP_CLAUSE_COPYOUT;
27452 else if (!strcmp ("copyprivate", p))
27453 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27454 else if (!strcmp ("create", p))
27455 result = PRAGMA_OMP_CLAUSE_CREATE;
27456 break;
27457 case 'd':
27458 if (!strcmp ("depend", p))
27459 result = PRAGMA_OMP_CLAUSE_DEPEND;
27460 else if (!strcmp ("device", p))
27461 result = PRAGMA_OMP_CLAUSE_DEVICE;
27462 else if (!strcmp ("deviceptr", p))
27463 result = PRAGMA_OMP_CLAUSE_DEVICEPTR;
27464 else if (!strcmp ("dist_schedule", p))
27465 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27466 break;
27467 case 'f':
27468 if (!strcmp ("final", p))
27469 result = PRAGMA_OMP_CLAUSE_FINAL;
27470 else if (!strcmp ("firstprivate", p))
27471 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27472 else if (!strcmp ("from", p))
27473 result = PRAGMA_OMP_CLAUSE_FROM;
27474 break;
27475 case 'h':
27476 if (!strcmp ("host", p))
27477 result = PRAGMA_OMP_CLAUSE_HOST;
27478 break;
27479 case 'i':
27480 if (!strcmp ("inbranch", p))
27481 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27482 break;
27483 case 'l':
27484 if (!strcmp ("lastprivate", p))
27485 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27486 else if (!strcmp ("linear", p))
27487 result = PRAGMA_OMP_CLAUSE_LINEAR;
27488 break;
27489 case 'm':
27490 if (!strcmp ("map", p))
27491 result = PRAGMA_OMP_CLAUSE_MAP;
27492 else if (!strcmp ("mergeable", p))
27493 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27494 else if (flag_cilkplus && !strcmp ("mask", p))
27495 result = PRAGMA_CILK_CLAUSE_MASK;
27496 break;
27497 case 'n':
27498 if (!strcmp ("notinbranch", p))
27499 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27500 else if (!strcmp ("nowait", p))
27501 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27502 else if (flag_cilkplus && !strcmp ("nomask", p))
27503 result = PRAGMA_CILK_CLAUSE_NOMASK;
27504 else if (!strcmp ("num_gangs", p))
27505 result = PRAGMA_OMP_CLAUSE_NUM_GANGS;
27506 else if (!strcmp ("num_teams", p))
27507 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27508 else if (!strcmp ("num_threads", p))
27509 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27510 else if (!strcmp ("num_workers", p))
27511 result = PRAGMA_OMP_CLAUSE_NUM_WORKERS;
27512 break;
27513 case 'o':
27514 if (!strcmp ("ordered", p))
27515 result = PRAGMA_OMP_CLAUSE_ORDERED;
27516 break;
27517 case 'p':
27518 if (!strcmp ("parallel", p))
27519 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27520 else if (!strcmp ("present", p))
27521 result = PRAGMA_OMP_CLAUSE_PRESENT;
27522 else if (!strcmp ("present_or_copy", p)
27523 || !strcmp ("pcopy", p))
27524 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY;
27525 else if (!strcmp ("present_or_copyin", p)
27526 || !strcmp ("pcopyin", p))
27527 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN;
27528 else if (!strcmp ("present_or_copyout", p)
27529 || !strcmp ("pcopyout", p))
27530 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT;
27531 else if (!strcmp ("present_or_create", p)
27532 || !strcmp ("pcreate", p))
27533 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE;
27534 else if (!strcmp ("private", p))
27535 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27536 else if (!strcmp ("proc_bind", p))
27537 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27538 break;
27539 case 'r':
27540 if (!strcmp ("reduction", p))
27541 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27542 break;
27543 case 's':
27544 if (!strcmp ("safelen", p))
27545 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27546 else if (!strcmp ("schedule", p))
27547 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27548 else if (!strcmp ("sections", p))
27549 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27550 else if (!strcmp ("self", p))
27551 result = PRAGMA_OMP_CLAUSE_SELF;
27552 else if (!strcmp ("shared", p))
27553 result = PRAGMA_OMP_CLAUSE_SHARED;
27554 else if (!strcmp ("simdlen", p))
27555 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27556 break;
27557 case 't':
27558 if (!strcmp ("taskgroup", p))
27559 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27560 else if (!strcmp ("thread_limit", p))
27561 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27562 else if (!strcmp ("to", p))
27563 result = PRAGMA_OMP_CLAUSE_TO;
27564 break;
27565 case 'u':
27566 if (!strcmp ("uniform", p))
27567 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27568 else if (!strcmp ("untied", p))
27569 result = PRAGMA_OMP_CLAUSE_UNTIED;
27570 break;
27571 case 'v':
27572 if (!strcmp ("vector_length", p))
27573 result = PRAGMA_OMP_CLAUSE_VECTOR_LENGTH;
27574 else if (flag_cilkplus && !strcmp ("vectorlength", p))
27575 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27576 break;
27577 case 'w':
27578 if (!strcmp ("wait", p))
27579 result = PRAGMA_OMP_CLAUSE_WAIT;
27580 break;
27584 if (result != PRAGMA_OMP_CLAUSE_NONE)
27585 cp_lexer_consume_token (parser->lexer);
27587 return result;
27590 /* Validate that a clause of the given type does not already exist. */
27592 static void
27593 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27594 const char *name, location_t location)
27596 tree c;
27598 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27599 if (OMP_CLAUSE_CODE (c) == code)
27601 error_at (location, "too many %qs clauses", name);
27602 break;
27606 /* OpenMP 2.5:
27607 variable-list:
27608 identifier
27609 variable-list , identifier
27611 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27612 colon). An opening parenthesis will have been consumed by the caller.
27614 If KIND is nonzero, create the appropriate node and install the decl
27615 in OMP_CLAUSE_DECL and add the node to the head of the list.
27617 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27618 return the list created.
27620 COLON can be NULL if only closing parenthesis should end the list,
27621 or pointer to bool which will receive false if the list is terminated
27622 by closing parenthesis or true if the list is terminated by colon. */
27624 static tree
27625 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27626 tree list, bool *colon)
27628 cp_token *token;
27629 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27630 if (colon)
27632 parser->colon_corrects_to_scope_p = false;
27633 *colon = false;
27635 while (1)
27637 tree name, decl;
27639 token = cp_lexer_peek_token (parser->lexer);
27640 name = cp_parser_id_expression (parser, /*template_p=*/false,
27641 /*check_dependency_p=*/true,
27642 /*template_p=*/NULL,
27643 /*declarator_p=*/false,
27644 /*optional_p=*/false);
27645 if (name == error_mark_node)
27646 goto skip_comma;
27648 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27649 if (decl == error_mark_node)
27650 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27651 token->location);
27652 else if (kind != 0)
27654 switch (kind)
27656 case OMP_CLAUSE__CACHE_:
27657 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27659 error_at (token->location, "expected %<[%>");
27660 decl = error_mark_node;
27661 break;
27663 /* FALL THROUGH. */
27664 case OMP_CLAUSE_MAP:
27665 case OMP_CLAUSE_FROM:
27666 case OMP_CLAUSE_TO:
27667 case OMP_CLAUSE_DEPEND:
27668 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27670 tree low_bound = NULL_TREE, length = NULL_TREE;
27672 parser->colon_corrects_to_scope_p = false;
27673 cp_lexer_consume_token (parser->lexer);
27674 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27675 low_bound = cp_parser_expression (parser);
27676 if (!colon)
27677 parser->colon_corrects_to_scope_p
27678 = saved_colon_corrects_to_scope_p;
27679 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27680 length = integer_one_node;
27681 else
27683 /* Look for `:'. */
27684 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27685 goto skip_comma;
27686 if (!cp_lexer_next_token_is (parser->lexer,
27687 CPP_CLOSE_SQUARE))
27688 length = cp_parser_expression (parser);
27690 /* Look for the closing `]'. */
27691 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27692 RT_CLOSE_SQUARE))
27693 goto skip_comma;
27695 if (kind == OMP_CLAUSE__CACHE_)
27697 if (TREE_CODE (low_bound) != INTEGER_CST
27698 && !TREE_READONLY (low_bound))
27700 error_at (token->location,
27701 "%qD is not a constant", low_bound);
27702 decl = error_mark_node;
27705 if (TREE_CODE (length) != INTEGER_CST
27706 && !TREE_READONLY (length))
27708 error_at (token->location,
27709 "%qD is not a constant", length);
27710 decl = error_mark_node;
27714 decl = tree_cons (low_bound, length, decl);
27716 break;
27717 default:
27718 break;
27721 tree u = build_omp_clause (token->location, kind);
27722 OMP_CLAUSE_DECL (u) = decl;
27723 OMP_CLAUSE_CHAIN (u) = list;
27724 list = u;
27726 else
27727 list = tree_cons (decl, NULL_TREE, list);
27729 get_comma:
27730 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27731 break;
27732 cp_lexer_consume_token (parser->lexer);
27735 if (colon)
27736 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27738 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27740 *colon = true;
27741 cp_parser_require (parser, CPP_COLON, RT_COLON);
27742 return list;
27745 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27747 int ending;
27749 /* Try to resync to an unnested comma. Copied from
27750 cp_parser_parenthesized_expression_list. */
27751 skip_comma:
27752 if (colon)
27753 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27754 ending = cp_parser_skip_to_closing_parenthesis (parser,
27755 /*recovering=*/true,
27756 /*or_comma=*/true,
27757 /*consume_paren=*/true);
27758 if (ending < 0)
27759 goto get_comma;
27762 return list;
27765 /* Similarly, but expect leading and trailing parenthesis. This is a very
27766 common case for omp clauses. */
27768 static tree
27769 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27771 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27772 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27773 return list;
27776 /* OpenACC 2.0:
27777 copy ( variable-list )
27778 copyin ( variable-list )
27779 copyout ( variable-list )
27780 create ( variable-list )
27781 delete ( variable-list )
27782 present ( variable-list )
27783 present_or_copy ( variable-list )
27784 pcopy ( variable-list )
27785 present_or_copyin ( variable-list )
27786 pcopyin ( variable-list )
27787 present_or_copyout ( variable-list )
27788 pcopyout ( variable-list )
27789 present_or_create ( variable-list )
27790 pcreate ( variable-list ) */
27792 static tree
27793 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
27794 tree list)
27796 enum omp_clause_map_kind kind;
27797 switch (c_kind)
27799 case PRAGMA_OMP_CLAUSE_COPY:
27800 kind = OMP_CLAUSE_MAP_FORCE_TOFROM;
27801 break;
27802 case PRAGMA_OMP_CLAUSE_COPYIN:
27803 kind = OMP_CLAUSE_MAP_FORCE_TO;
27804 break;
27805 case PRAGMA_OMP_CLAUSE_COPYOUT:
27806 kind = OMP_CLAUSE_MAP_FORCE_FROM;
27807 break;
27808 case PRAGMA_OMP_CLAUSE_CREATE:
27809 kind = OMP_CLAUSE_MAP_FORCE_ALLOC;
27810 break;
27811 case PRAGMA_OMP_CLAUSE_DELETE:
27812 kind = OMP_CLAUSE_MAP_FORCE_DEALLOC;
27813 break;
27814 case PRAGMA_OMP_CLAUSE_DEVICE:
27815 kind = OMP_CLAUSE_MAP_FORCE_TO;
27816 break;
27817 case PRAGMA_OMP_CLAUSE_HOST:
27818 case PRAGMA_OMP_CLAUSE_SELF:
27819 kind = OMP_CLAUSE_MAP_FORCE_FROM;
27820 break;
27821 case PRAGMA_OMP_CLAUSE_PRESENT:
27822 kind = OMP_CLAUSE_MAP_FORCE_PRESENT;
27823 break;
27824 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY:
27825 kind = OMP_CLAUSE_MAP_TOFROM;
27826 break;
27827 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN:
27828 kind = OMP_CLAUSE_MAP_TO;
27829 break;
27830 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT:
27831 kind = OMP_CLAUSE_MAP_FROM;
27832 break;
27833 case PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE:
27834 kind = OMP_CLAUSE_MAP_ALLOC;
27835 break;
27836 default:
27837 gcc_unreachable ();
27839 tree nl, c;
27840 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
27842 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
27843 OMP_CLAUSE_MAP_KIND (c) = kind;
27845 return nl;
27848 /* OpenACC 2.0:
27849 deviceptr ( variable-list ) */
27851 static tree
27852 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
27854 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27855 tree vars, t;
27857 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
27858 cp_parser_oacc_data_clause), as for PRAGMA_OMP_CLAUSE_DEVICEPTR,
27859 variable-list must only allow for pointer variables. */
27860 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27861 for (t = vars; t; t = TREE_CHAIN (t))
27863 tree v = TREE_PURPOSE (t);
27865 /* FIXME diagnostics: Ideally we should keep individual
27866 locations for all the variables in the var list to make the
27867 following errors more precise. Perhaps
27868 c_parser_omp_var_list_parens should construct a list of
27869 locations to go along with the var list. */
27871 if (TREE_CODE (v) != VAR_DECL)
27872 error_at (loc, "%qD is not a variable", v);
27873 else if (TREE_TYPE (v) == error_mark_node)
27875 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
27876 error_at (loc, "%qD is not a pointer variable", v);
27878 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
27879 OMP_CLAUSE_MAP_KIND (u) = OMP_CLAUSE_MAP_FORCE_DEVICEPTR;
27880 OMP_CLAUSE_DECL (u) = v;
27881 OMP_CLAUSE_CHAIN (u) = list;
27882 list = u;
27885 return list;
27888 /* OpenACC:
27889 vector_length ( expression ) */
27891 static tree
27892 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
27894 tree t, c;
27895 location_t location = cp_lexer_peek_token (parser->lexer)->location;
27896 bool error = false;
27897 HOST_WIDE_INT n;
27899 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27900 return list;
27902 t = cp_parser_condition (parser);
27903 if (t == error_mark_node
27904 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
27905 || !tree_fits_shwi_p (t)
27906 || (n = tree_to_shwi (t)) <= 0
27907 || (int) n != n)
27909 error_at (location, "expected positive integer expression");
27910 error = true;
27913 if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27915 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27916 /*or_comma=*/false,
27917 /*consume_paren=*/true);
27918 return list;
27921 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH,
27922 "vector_length", location);
27924 c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
27925 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
27926 OMP_CLAUSE_CHAIN (c) = list;
27927 list = c;
27929 return list;
27932 /* OpenACC 2.0
27933 Parse wait clause or directive parameters. */
27935 static tree
27936 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
27938 vec<tree, va_gc> *args;
27939 tree t, args_tree;
27941 args = cp_parser_parenthesized_expression_list (parser, non_attr,
27942 /*cast_p=*/false,
27943 /*allow_expansion_p=*/true,
27944 /*non_constant_p=*/NULL);
27946 if (args == NULL || args->length() == 0)
27948 cp_parser_error (parser, "expected integer expression before ')'");
27949 if (args != NULL)
27950 release_tree_vector (args);
27951 return list;
27954 args_tree = build_tree_list_vec (args);
27956 release_tree_vector (args);
27958 for (t = args_tree; t; t = TREE_CHAIN (t))
27960 tree targ = TREE_VALUE (t);
27962 if (targ != error_mark_node)
27964 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
27966 error("%<wait%> expression must be integral");
27967 targ = error_mark_node;
27969 else
27971 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
27973 mark_rvalue_use (targ);
27975 OMP_CLAUSE_DECL (c) = targ;
27976 OMP_CLAUSE_CHAIN (c) = list;
27977 list = c;
27982 return list;
27985 /* OpenACC:
27986 wait ( int-expr-list ) */
27988 static tree
27989 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
27991 location_t location = cp_lexer_peek_token (parser->lexer)->location;
27993 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
27994 return list;
27996 list = cp_parser_oacc_wait_list (parser, location, list);
27998 return list;
28001 /* OpenMP 3.0:
28002 collapse ( constant-expression ) */
28004 static tree
28005 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28007 tree c, num;
28008 location_t loc;
28009 HOST_WIDE_INT n;
28011 loc = cp_lexer_peek_token (parser->lexer)->location;
28012 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28013 return list;
28015 num = cp_parser_constant_expression (parser);
28017 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28018 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28019 /*or_comma=*/false,
28020 /*consume_paren=*/true);
28022 if (num == error_mark_node)
28023 return list;
28024 num = fold_non_dependent_expr (num);
28025 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28026 || !tree_fits_shwi_p (num)
28027 || (n = tree_to_shwi (num)) <= 0
28028 || (int) n != n)
28030 error_at (loc, "collapse argument needs positive constant integer expression");
28031 return list;
28034 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28035 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28036 OMP_CLAUSE_CHAIN (c) = list;
28037 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28039 return c;
28042 /* OpenMP 2.5:
28043 default ( shared | none ) */
28045 static tree
28046 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28048 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28049 tree c;
28051 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28052 return list;
28053 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28055 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28056 const char *p = IDENTIFIER_POINTER (id);
28058 switch (p[0])
28060 case 'n':
28061 if (strcmp ("none", p) != 0)
28062 goto invalid_kind;
28063 kind = OMP_CLAUSE_DEFAULT_NONE;
28064 break;
28066 case 's':
28067 if (strcmp ("shared", p) != 0)
28068 goto invalid_kind;
28069 kind = OMP_CLAUSE_DEFAULT_SHARED;
28070 break;
28072 default:
28073 goto invalid_kind;
28076 cp_lexer_consume_token (parser->lexer);
28078 else
28080 invalid_kind:
28081 cp_parser_error (parser, "expected %<none%> or %<shared%>");
28084 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28085 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28086 /*or_comma=*/false,
28087 /*consume_paren=*/true);
28089 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28090 return list;
28092 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28093 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28094 OMP_CLAUSE_CHAIN (c) = list;
28095 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28097 return c;
28100 /* OpenMP 3.1:
28101 final ( expression ) */
28103 static tree
28104 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28106 tree t, c;
28108 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28109 return list;
28111 t = cp_parser_condition (parser);
28113 if (t == error_mark_node
28114 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28115 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28116 /*or_comma=*/false,
28117 /*consume_paren=*/true);
28119 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28121 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28122 OMP_CLAUSE_FINAL_EXPR (c) = t;
28123 OMP_CLAUSE_CHAIN (c) = list;
28125 return c;
28128 /* OpenMP 2.5:
28129 if ( expression ) */
28131 static tree
28132 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28134 tree t, c;
28136 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28137 return list;
28139 t = cp_parser_condition (parser);
28141 if (t == error_mark_node
28142 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28143 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28144 /*or_comma=*/false,
28145 /*consume_paren=*/true);
28147 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28149 c = build_omp_clause (location, OMP_CLAUSE_IF);
28150 OMP_CLAUSE_IF_EXPR (c) = t;
28151 OMP_CLAUSE_CHAIN (c) = list;
28153 return c;
28156 /* OpenMP 3.1:
28157 mergeable */
28159 static tree
28160 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28161 tree list, location_t location)
28163 tree c;
28165 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28166 location);
28168 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28169 OMP_CLAUSE_CHAIN (c) = list;
28170 return c;
28173 /* OpenMP 2.5:
28174 nowait */
28176 static tree
28177 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28178 tree list, location_t location)
28180 tree c;
28182 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28184 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28185 OMP_CLAUSE_CHAIN (c) = list;
28186 return c;
28189 /* OpenACC:
28190 num_gangs ( expression ) */
28192 static tree
28193 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28195 tree t, c;
28196 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28197 HOST_WIDE_INT n;
28199 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28200 return list;
28202 t = cp_parser_condition (parser);
28204 if (t == error_mark_node
28205 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28206 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28207 /*or_comma=*/false,
28208 /*consume_paren=*/true);
28210 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
28211 || !tree_fits_shwi_p (t)
28212 || (n = tree_to_shwi (t)) <= 0
28213 || (int) n != n)
28215 error_at (location, "expected positive integer expression");
28216 return list;
28219 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28221 c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28222 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28223 OMP_CLAUSE_CHAIN (c) = list;
28224 list = c;
28226 return list;
28229 /* OpenMP 2.5:
28230 num_threads ( expression ) */
28232 static tree
28233 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28234 location_t location)
28236 tree t, c;
28238 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28239 return list;
28241 t = cp_parser_expression (parser);
28243 if (t == error_mark_node
28244 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28245 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28246 /*or_comma=*/false,
28247 /*consume_paren=*/true);
28249 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28250 "num_threads", location);
28252 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28253 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28254 OMP_CLAUSE_CHAIN (c) = list;
28256 return c;
28259 /* OpenACC:
28260 num_workers ( expression ) */
28262 static tree
28263 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28265 tree t, c;
28266 location_t location = cp_lexer_peek_token (parser->lexer)->location;
28267 HOST_WIDE_INT n;
28269 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28270 return list;
28272 t = cp_parser_condition (parser);
28274 if (t == error_mark_node
28275 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28276 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28277 /*or_comma=*/false,
28278 /*consume_paren=*/true);
28280 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
28281 || !tree_fits_shwi_p (t)
28282 || (n = tree_to_shwi (t)) <= 0
28283 || (int) n != n)
28285 error_at (location, "expected positive integer expression");
28286 return list;
28289 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28290 location);
28292 c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28293 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28294 OMP_CLAUSE_CHAIN (c) = list;
28295 list = c;
28297 return list;
28300 /* OpenMP 2.5:
28301 ordered */
28303 static tree
28304 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28305 tree list, location_t location)
28307 tree c;
28309 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28310 "ordered", location);
28312 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28313 OMP_CLAUSE_CHAIN (c) = list;
28314 return c;
28317 /* OpenMP 2.5:
28318 reduction ( reduction-operator : variable-list )
28320 reduction-operator:
28321 One of: + * - & ^ | && ||
28323 OpenMP 3.1:
28325 reduction-operator:
28326 One of: + * - & ^ | && || min max
28328 OpenMP 4.0:
28330 reduction-operator:
28331 One of: + * - & ^ | && ||
28332 id-expression */
28334 static tree
28335 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28337 enum tree_code code = ERROR_MARK;
28338 tree nlist, c, id = NULL_TREE;
28340 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28341 return list;
28343 switch (cp_lexer_peek_token (parser->lexer)->type)
28345 case CPP_PLUS: code = PLUS_EXPR; break;
28346 case CPP_MULT: code = MULT_EXPR; break;
28347 case CPP_MINUS: code = MINUS_EXPR; break;
28348 case CPP_AND: code = BIT_AND_EXPR; break;
28349 case CPP_XOR: code = BIT_XOR_EXPR; break;
28350 case CPP_OR: code = BIT_IOR_EXPR; break;
28351 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28352 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28353 default: break;
28356 if (code != ERROR_MARK)
28357 cp_lexer_consume_token (parser->lexer);
28358 else
28360 bool saved_colon_corrects_to_scope_p;
28361 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28362 parser->colon_corrects_to_scope_p = false;
28363 id = cp_parser_id_expression (parser, /*template_p=*/false,
28364 /*check_dependency_p=*/true,
28365 /*template_p=*/NULL,
28366 /*declarator_p=*/false,
28367 /*optional_p=*/false);
28368 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28369 if (identifier_p (id))
28371 const char *p = IDENTIFIER_POINTER (id);
28373 if (strcmp (p, "min") == 0)
28374 code = MIN_EXPR;
28375 else if (strcmp (p, "max") == 0)
28376 code = MAX_EXPR;
28377 else if (id == ansi_opname (PLUS_EXPR))
28378 code = PLUS_EXPR;
28379 else if (id == ansi_opname (MULT_EXPR))
28380 code = MULT_EXPR;
28381 else if (id == ansi_opname (MINUS_EXPR))
28382 code = MINUS_EXPR;
28383 else if (id == ansi_opname (BIT_AND_EXPR))
28384 code = BIT_AND_EXPR;
28385 else if (id == ansi_opname (BIT_IOR_EXPR))
28386 code = BIT_IOR_EXPR;
28387 else if (id == ansi_opname (BIT_XOR_EXPR))
28388 code = BIT_XOR_EXPR;
28389 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28390 code = TRUTH_ANDIF_EXPR;
28391 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28392 code = TRUTH_ORIF_EXPR;
28393 id = omp_reduction_id (code, id, NULL_TREE);
28394 tree scope = parser->scope;
28395 if (scope)
28396 id = build_qualified_name (NULL_TREE, scope, id, false);
28397 parser->scope = NULL_TREE;
28398 parser->qualifying_scope = NULL_TREE;
28399 parser->object_scope = NULL_TREE;
28401 else
28403 error ("invalid reduction-identifier");
28404 resync_fail:
28405 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28406 /*or_comma=*/false,
28407 /*consume_paren=*/true);
28408 return list;
28412 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28413 goto resync_fail;
28415 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28416 NULL);
28417 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28419 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28420 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28423 return nlist;
28426 /* OpenMP 2.5:
28427 schedule ( schedule-kind )
28428 schedule ( schedule-kind , expression )
28430 schedule-kind:
28431 static | dynamic | guided | runtime | auto */
28433 static tree
28434 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28436 tree c, t;
28438 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28439 return list;
28441 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28443 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28445 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28446 const char *p = IDENTIFIER_POINTER (id);
28448 switch (p[0])
28450 case 'd':
28451 if (strcmp ("dynamic", p) != 0)
28452 goto invalid_kind;
28453 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28454 break;
28456 case 'g':
28457 if (strcmp ("guided", p) != 0)
28458 goto invalid_kind;
28459 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28460 break;
28462 case 'r':
28463 if (strcmp ("runtime", p) != 0)
28464 goto invalid_kind;
28465 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28466 break;
28468 default:
28469 goto invalid_kind;
28472 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28473 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28474 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28475 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28476 else
28477 goto invalid_kind;
28478 cp_lexer_consume_token (parser->lexer);
28480 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28482 cp_token *token;
28483 cp_lexer_consume_token (parser->lexer);
28485 token = cp_lexer_peek_token (parser->lexer);
28486 t = cp_parser_assignment_expression (parser);
28488 if (t == error_mark_node)
28489 goto resync_fail;
28490 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28491 error_at (token->location, "schedule %<runtime%> does not take "
28492 "a %<chunk_size%> parameter");
28493 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28494 error_at (token->location, "schedule %<auto%> does not take "
28495 "a %<chunk_size%> parameter");
28496 else
28497 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28499 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28500 goto resync_fail;
28502 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28503 goto resync_fail;
28505 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28506 OMP_CLAUSE_CHAIN (c) = list;
28507 return c;
28509 invalid_kind:
28510 cp_parser_error (parser, "invalid schedule kind");
28511 resync_fail:
28512 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28513 /*or_comma=*/false,
28514 /*consume_paren=*/true);
28515 return list;
28518 /* OpenMP 3.0:
28519 untied */
28521 static tree
28522 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28523 tree list, location_t location)
28525 tree c;
28527 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28529 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28530 OMP_CLAUSE_CHAIN (c) = list;
28531 return c;
28534 /* OpenMP 4.0:
28535 inbranch
28536 notinbranch */
28538 static tree
28539 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28540 tree list, location_t location)
28542 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28543 tree c = build_omp_clause (location, code);
28544 OMP_CLAUSE_CHAIN (c) = list;
28545 return c;
28548 /* OpenMP 4.0:
28549 parallel
28551 sections
28552 taskgroup */
28554 static tree
28555 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28556 enum omp_clause_code code,
28557 tree list, location_t location)
28559 tree c = build_omp_clause (location, code);
28560 OMP_CLAUSE_CHAIN (c) = list;
28561 return c;
28564 /* OpenMP 4.0:
28565 num_teams ( expression ) */
28567 static tree
28568 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28569 location_t location)
28571 tree t, c;
28573 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28574 return list;
28576 t = cp_parser_expression (parser);
28578 if (t == error_mark_node
28579 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28580 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28581 /*or_comma=*/false,
28582 /*consume_paren=*/true);
28584 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28585 "num_teams", location);
28587 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28588 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28589 OMP_CLAUSE_CHAIN (c) = list;
28591 return c;
28594 /* OpenMP 4.0:
28595 thread_limit ( expression ) */
28597 static tree
28598 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28599 location_t location)
28601 tree t, c;
28603 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28604 return list;
28606 t = cp_parser_expression (parser);
28608 if (t == error_mark_node
28609 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28610 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28611 /*or_comma=*/false,
28612 /*consume_paren=*/true);
28614 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28615 "thread_limit", location);
28617 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28618 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28619 OMP_CLAUSE_CHAIN (c) = list;
28621 return c;
28624 /* OpenMP 4.0:
28625 aligned ( variable-list )
28626 aligned ( variable-list : constant-expression ) */
28628 static tree
28629 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28631 tree nlist, c, alignment = NULL_TREE;
28632 bool colon;
28634 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28635 return list;
28637 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28638 &colon);
28640 if (colon)
28642 alignment = cp_parser_constant_expression (parser);
28644 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28645 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28646 /*or_comma=*/false,
28647 /*consume_paren=*/true);
28649 if (alignment == error_mark_node)
28650 alignment = NULL_TREE;
28653 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28654 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28656 return nlist;
28659 /* OpenMP 4.0:
28660 linear ( variable-list )
28661 linear ( variable-list : expression ) */
28663 static tree
28664 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28665 bool is_cilk_simd_fn)
28667 tree nlist, c, step = integer_one_node;
28668 bool colon;
28670 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28671 return list;
28673 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28674 &colon);
28676 if (colon)
28678 step = cp_parser_expression (parser);
28680 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28682 sorry ("using parameters for %<linear%> step is not supported yet");
28683 step = integer_one_node;
28685 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28686 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28687 /*or_comma=*/false,
28688 /*consume_paren=*/true);
28690 if (step == error_mark_node)
28691 return list;
28694 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28695 OMP_CLAUSE_LINEAR_STEP (c) = step;
28697 return nlist;
28700 /* OpenMP 4.0:
28701 safelen ( constant-expression ) */
28703 static tree
28704 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28705 location_t location)
28707 tree t, c;
28709 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28710 return list;
28712 t = cp_parser_constant_expression (parser);
28714 if (t == error_mark_node
28715 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28716 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28717 /*or_comma=*/false,
28718 /*consume_paren=*/true);
28720 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28722 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28723 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28724 OMP_CLAUSE_CHAIN (c) = list;
28726 return c;
28729 /* OpenMP 4.0:
28730 simdlen ( constant-expression ) */
28732 static tree
28733 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28734 location_t location)
28736 tree t, c;
28738 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28739 return list;
28741 t = cp_parser_constant_expression (parser);
28743 if (t == error_mark_node
28744 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28745 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28746 /*or_comma=*/false,
28747 /*consume_paren=*/true);
28749 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28751 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28752 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28753 OMP_CLAUSE_CHAIN (c) = list;
28755 return c;
28758 /* OpenMP 4.0:
28759 depend ( depend-kind : variable-list )
28761 depend-kind:
28762 in | out | inout */
28764 static tree
28765 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28767 tree nlist, c;
28768 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28770 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28771 return list;
28773 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28775 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28776 const char *p = IDENTIFIER_POINTER (id);
28778 if (strcmp ("in", p) == 0)
28779 kind = OMP_CLAUSE_DEPEND_IN;
28780 else if (strcmp ("inout", p) == 0)
28781 kind = OMP_CLAUSE_DEPEND_INOUT;
28782 else if (strcmp ("out", p) == 0)
28783 kind = OMP_CLAUSE_DEPEND_OUT;
28784 else
28785 goto invalid_kind;
28787 else
28788 goto invalid_kind;
28790 cp_lexer_consume_token (parser->lexer);
28791 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28792 goto resync_fail;
28794 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28795 NULL);
28797 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28798 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28800 return nlist;
28802 invalid_kind:
28803 cp_parser_error (parser, "invalid depend kind");
28804 resync_fail:
28805 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28806 /*or_comma=*/false,
28807 /*consume_paren=*/true);
28808 return list;
28811 /* OpenMP 4.0:
28812 map ( map-kind : variable-list )
28813 map ( variable-list )
28815 map-kind:
28816 alloc | to | from | tofrom */
28818 static tree
28819 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28821 tree nlist, c;
28822 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28824 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28825 return list;
28827 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28828 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28830 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28831 const char *p = IDENTIFIER_POINTER (id);
28833 if (strcmp ("alloc", p) == 0)
28834 kind = OMP_CLAUSE_MAP_ALLOC;
28835 else if (strcmp ("to", p) == 0)
28836 kind = OMP_CLAUSE_MAP_TO;
28837 else if (strcmp ("from", p) == 0)
28838 kind = OMP_CLAUSE_MAP_FROM;
28839 else if (strcmp ("tofrom", p) == 0)
28840 kind = OMP_CLAUSE_MAP_TOFROM;
28841 else
28843 cp_parser_error (parser, "invalid map kind");
28844 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28845 /*or_comma=*/false,
28846 /*consume_paren=*/true);
28847 return list;
28849 cp_lexer_consume_token (parser->lexer);
28850 cp_lexer_consume_token (parser->lexer);
28853 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28854 NULL);
28856 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28857 OMP_CLAUSE_MAP_KIND (c) = kind;
28859 return nlist;
28862 /* OpenMP 4.0:
28863 device ( expression ) */
28865 static tree
28866 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28867 location_t location)
28869 tree t, c;
28871 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28872 return list;
28874 t = cp_parser_expression (parser);
28876 if (t == error_mark_node
28877 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28878 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28879 /*or_comma=*/false,
28880 /*consume_paren=*/true);
28882 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28883 "device", location);
28885 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28886 OMP_CLAUSE_DEVICE_ID (c) = t;
28887 OMP_CLAUSE_CHAIN (c) = list;
28889 return c;
28892 /* OpenMP 4.0:
28893 dist_schedule ( static )
28894 dist_schedule ( static , expression ) */
28896 static tree
28897 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28898 location_t location)
28900 tree c, t;
28902 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28903 return list;
28905 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28907 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28908 goto invalid_kind;
28909 cp_lexer_consume_token (parser->lexer);
28911 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28913 cp_lexer_consume_token (parser->lexer);
28915 t = cp_parser_assignment_expression (parser);
28917 if (t == error_mark_node)
28918 goto resync_fail;
28919 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28921 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28922 goto resync_fail;
28924 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28925 goto resync_fail;
28927 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28928 location);
28929 OMP_CLAUSE_CHAIN (c) = list;
28930 return c;
28932 invalid_kind:
28933 cp_parser_error (parser, "invalid dist_schedule kind");
28934 resync_fail:
28935 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28936 /*or_comma=*/false,
28937 /*consume_paren=*/true);
28938 return list;
28941 /* OpenMP 4.0:
28942 proc_bind ( proc-bind-kind )
28944 proc-bind-kind:
28945 master | close | spread */
28947 static tree
28948 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28949 location_t location)
28951 tree c;
28952 enum omp_clause_proc_bind_kind kind;
28954 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28955 return list;
28957 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28959 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28960 const char *p = IDENTIFIER_POINTER (id);
28962 if (strcmp ("master", p) == 0)
28963 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28964 else if (strcmp ("close", p) == 0)
28965 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28966 else if (strcmp ("spread", p) == 0)
28967 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28968 else
28969 goto invalid_kind;
28971 else
28972 goto invalid_kind;
28974 cp_lexer_consume_token (parser->lexer);
28975 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28976 goto resync_fail;
28978 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28979 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28980 location);
28981 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28982 OMP_CLAUSE_CHAIN (c) = list;
28983 return c;
28985 invalid_kind:
28986 cp_parser_error (parser, "invalid depend kind");
28987 resync_fail:
28988 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28989 /*or_comma=*/false,
28990 /*consume_paren=*/true);
28991 return list;
28994 /* OpenACC:
28995 async [( int-expr )] */
28997 static tree
28998 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29000 tree c, t;
29001 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29003 /* TODO XXX: FIX -1 (acc_async_noval). */
29004 t = build_int_cst (integer_type_node, -1);
29006 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29008 cp_lexer_consume_token (parser->lexer);
29010 t = cp_parser_expression (parser);
29011 if (t == error_mark_node
29012 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29013 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29014 /*or_comma=*/false,
29015 /*consume_paren=*/true);
29018 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29020 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29021 OMP_CLAUSE_ASYNC_EXPR (c) = t;
29022 OMP_CLAUSE_CHAIN (c) = list;
29023 list = c;
29025 return list;
29028 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29029 is a bitmask in MASK. Return the list of clauses found. */
29031 static tree
29032 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29033 const char *where, cp_token *pragma_tok,
29034 bool finish_p = true)
29036 tree clauses = NULL;
29037 bool first = true;
29039 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29041 location_t here;
29042 pragma_omp_clause c_kind;
29043 const char *c_name;
29044 tree prev = clauses;
29046 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29047 cp_lexer_consume_token (parser->lexer);
29049 here = cp_lexer_peek_token (parser->lexer)->location;
29050 c_kind = cp_parser_omp_clause_name (parser);
29052 switch (c_kind)
29054 case PRAGMA_OMP_CLAUSE_ASYNC:
29055 clauses = cp_parser_oacc_clause_async (parser, clauses);
29056 c_name = "async";
29057 break;
29058 case PRAGMA_OMP_CLAUSE_COLLAPSE:
29059 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29060 c_name = "collapse";
29061 break;
29062 case PRAGMA_OMP_CLAUSE_COPY:
29063 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29064 c_name = "copy";
29065 break;
29066 case PRAGMA_OMP_CLAUSE_COPYIN:
29067 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29068 c_name = "copyin";
29069 break;
29070 case PRAGMA_OMP_CLAUSE_COPYOUT:
29071 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29072 c_name = "copyout";
29073 break;
29074 case PRAGMA_OMP_CLAUSE_CREATE:
29075 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29076 c_name = "create";
29077 break;
29078 case PRAGMA_OMP_CLAUSE_DELETE:
29079 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29080 c_name = "delete";
29081 break;
29082 case PRAGMA_OMP_CLAUSE_DEVICE:
29083 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29084 c_name = "device";
29085 break;
29086 case PRAGMA_OMP_CLAUSE_DEVICEPTR:
29087 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29088 c_name = "deviceptr";
29089 break;
29090 case PRAGMA_OMP_CLAUSE_HOST:
29091 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29092 c_name = "host";
29093 break;
29094 case PRAGMA_OMP_CLAUSE_IF:
29095 clauses = cp_parser_omp_clause_if (parser, clauses, here);
29096 c_name = "if";
29097 break;
29098 case PRAGMA_OMP_CLAUSE_NUM_GANGS:
29099 clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29100 c_name = "num_gangs";
29101 break;
29102 case PRAGMA_OMP_CLAUSE_NUM_WORKERS:
29103 clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29104 c_name = "num_workers";
29105 break;
29106 case PRAGMA_OMP_CLAUSE_PRESENT:
29107 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29108 c_name = "present";
29109 break;
29110 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY:
29111 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29112 c_name = "present_or_copy";
29113 break;
29114 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN:
29115 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29116 c_name = "present_or_copyin";
29117 break;
29118 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT:
29119 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29120 c_name = "present_or_copyout";
29121 break;
29122 case PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE:
29123 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29124 c_name = "present_or_create";
29125 break;
29126 case PRAGMA_OMP_CLAUSE_REDUCTION:
29127 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29128 c_name = "reduction";
29129 break;
29130 case PRAGMA_OMP_CLAUSE_SELF:
29131 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29132 c_name = "self";
29133 break;
29134 case PRAGMA_OMP_CLAUSE_VECTOR_LENGTH:
29135 clauses =
29136 cp_parser_oacc_clause_vector_length (parser, clauses);
29137 c_name = "vector_length";
29138 break;
29139 case PRAGMA_OMP_CLAUSE_WAIT:
29140 clauses = cp_parser_oacc_clause_wait (parser, clauses);
29141 c_name = "wait";
29142 break;
29143 default:
29144 cp_parser_error (parser, "expected clause");
29145 goto saw_error;
29148 first = false;
29150 if (((mask >> c_kind) & 1) == 0)
29152 /* Remove the invalid clause(s) from the list to avoid
29153 confusing the rest of the compiler. */
29154 clauses = prev;
29155 error_at (here, "%qs is not valid for %qs", c_name, where);
29159 saw_error:
29160 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29162 if (finish_p)
29163 return finish_omp_clauses (clauses);
29165 return clauses;
29168 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29169 is a bitmask in MASK. Return the list of clauses found; the result
29170 of clause default goes in *pdefault. */
29172 static tree
29173 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29174 const char *where, cp_token *pragma_tok,
29175 bool finish_p = true)
29177 tree clauses = NULL;
29178 bool first = true;
29179 cp_token *token = NULL;
29180 bool cilk_simd_fn = false;
29182 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29184 pragma_omp_clause c_kind;
29185 const char *c_name;
29186 tree prev = clauses;
29188 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29189 cp_lexer_consume_token (parser->lexer);
29191 token = cp_lexer_peek_token (parser->lexer);
29192 c_kind = cp_parser_omp_clause_name (parser);
29194 switch (c_kind)
29196 case PRAGMA_OMP_CLAUSE_COLLAPSE:
29197 clauses = cp_parser_omp_clause_collapse (parser, clauses,
29198 token->location);
29199 c_name = "collapse";
29200 break;
29201 case PRAGMA_OMP_CLAUSE_COPYIN:
29202 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29203 c_name = "copyin";
29204 break;
29205 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29206 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29207 clauses);
29208 c_name = "copyprivate";
29209 break;
29210 case PRAGMA_OMP_CLAUSE_DEFAULT:
29211 clauses = cp_parser_omp_clause_default (parser, clauses,
29212 token->location);
29213 c_name = "default";
29214 break;
29215 case PRAGMA_OMP_CLAUSE_FINAL:
29216 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29217 c_name = "final";
29218 break;
29219 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29220 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29221 clauses);
29222 c_name = "firstprivate";
29223 break;
29224 case PRAGMA_OMP_CLAUSE_IF:
29225 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29226 c_name = "if";
29227 break;
29228 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29229 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29230 clauses);
29231 c_name = "lastprivate";
29232 break;
29233 case PRAGMA_OMP_CLAUSE_MERGEABLE:
29234 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29235 token->location);
29236 c_name = "mergeable";
29237 break;
29238 case PRAGMA_OMP_CLAUSE_NOWAIT:
29239 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29240 c_name = "nowait";
29241 break;
29242 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29243 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29244 token->location);
29245 c_name = "num_threads";
29246 break;
29247 case PRAGMA_OMP_CLAUSE_ORDERED:
29248 clauses = cp_parser_omp_clause_ordered (parser, clauses,
29249 token->location);
29250 c_name = "ordered";
29251 break;
29252 case PRAGMA_OMP_CLAUSE_PRIVATE:
29253 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29254 clauses);
29255 c_name = "private";
29256 break;
29257 case PRAGMA_OMP_CLAUSE_REDUCTION:
29258 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29259 c_name = "reduction";
29260 break;
29261 case PRAGMA_OMP_CLAUSE_SCHEDULE:
29262 clauses = cp_parser_omp_clause_schedule (parser, clauses,
29263 token->location);
29264 c_name = "schedule";
29265 break;
29266 case PRAGMA_OMP_CLAUSE_SHARED:
29267 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29268 clauses);
29269 c_name = "shared";
29270 break;
29271 case PRAGMA_OMP_CLAUSE_UNTIED:
29272 clauses = cp_parser_omp_clause_untied (parser, clauses,
29273 token->location);
29274 c_name = "untied";
29275 break;
29276 case PRAGMA_OMP_CLAUSE_INBRANCH:
29277 case PRAGMA_CILK_CLAUSE_MASK:
29278 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29279 clauses, token->location);
29280 c_name = "inbranch";
29281 break;
29282 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29283 case PRAGMA_CILK_CLAUSE_NOMASK:
29284 clauses = cp_parser_omp_clause_branch (parser,
29285 OMP_CLAUSE_NOTINBRANCH,
29286 clauses, token->location);
29287 c_name = "notinbranch";
29288 break;
29289 case PRAGMA_OMP_CLAUSE_PARALLEL:
29290 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29291 clauses, token->location);
29292 c_name = "parallel";
29293 if (!first)
29295 clause_not_first:
29296 error_at (token->location, "%qs must be the first clause of %qs",
29297 c_name, where);
29298 clauses = prev;
29300 break;
29301 case PRAGMA_OMP_CLAUSE_FOR:
29302 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29303 clauses, token->location);
29304 c_name = "for";
29305 if (!first)
29306 goto clause_not_first;
29307 break;
29308 case PRAGMA_OMP_CLAUSE_SECTIONS:
29309 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29310 clauses, token->location);
29311 c_name = "sections";
29312 if (!first)
29313 goto clause_not_first;
29314 break;
29315 case PRAGMA_OMP_CLAUSE_TASKGROUP:
29316 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29317 clauses, token->location);
29318 c_name = "taskgroup";
29319 if (!first)
29320 goto clause_not_first;
29321 break;
29322 case PRAGMA_OMP_CLAUSE_TO:
29323 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29324 clauses);
29325 c_name = "to";
29326 break;
29327 case PRAGMA_OMP_CLAUSE_FROM:
29328 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29329 clauses);
29330 c_name = "from";
29331 break;
29332 case PRAGMA_OMP_CLAUSE_UNIFORM:
29333 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29334 clauses);
29335 c_name = "uniform";
29336 break;
29337 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29338 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29339 token->location);
29340 c_name = "num_teams";
29341 break;
29342 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29343 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29344 token->location);
29345 c_name = "thread_limit";
29346 break;
29347 case PRAGMA_OMP_CLAUSE_ALIGNED:
29348 clauses = cp_parser_omp_clause_aligned (parser, clauses);
29349 c_name = "aligned";
29350 break;
29351 case PRAGMA_OMP_CLAUSE_LINEAR:
29352 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29353 cilk_simd_fn = true;
29354 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29355 c_name = "linear";
29356 break;
29357 case PRAGMA_OMP_CLAUSE_DEPEND:
29358 clauses = cp_parser_omp_clause_depend (parser, clauses);
29359 c_name = "depend";
29360 break;
29361 case PRAGMA_OMP_CLAUSE_MAP:
29362 clauses = cp_parser_omp_clause_map (parser, clauses);
29363 c_name = "map";
29364 break;
29365 case PRAGMA_OMP_CLAUSE_DEVICE:
29366 clauses = cp_parser_omp_clause_device (parser, clauses,
29367 token->location);
29368 c_name = "device";
29369 break;
29370 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29371 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29372 token->location);
29373 c_name = "dist_schedule";
29374 break;
29375 case PRAGMA_OMP_CLAUSE_PROC_BIND:
29376 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29377 token->location);
29378 c_name = "proc_bind";
29379 break;
29380 case PRAGMA_OMP_CLAUSE_SAFELEN:
29381 clauses = cp_parser_omp_clause_safelen (parser, clauses,
29382 token->location);
29383 c_name = "safelen";
29384 break;
29385 case PRAGMA_OMP_CLAUSE_SIMDLEN:
29386 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29387 token->location);
29388 c_name = "simdlen";
29389 break;
29390 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29391 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29392 c_name = "simdlen";
29393 break;
29394 default:
29395 cp_parser_error (parser, "expected clause");
29396 goto saw_error;
29399 first = false;
29401 if (((mask >> c_kind) & 1) == 0)
29403 /* Remove the invalid clause(s) from the list to avoid
29404 confusing the rest of the compiler. */
29405 clauses = prev;
29406 error_at (token->location, "%qs is not valid for %qs", c_name, where);
29409 saw_error:
29410 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29411 no reason to skip to the end. */
29412 if (!(flag_cilkplus && pragma_tok == NULL))
29413 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29414 if (finish_p)
29415 return finish_omp_clauses (clauses);
29416 return clauses;
29419 /* OpenMP 2.5:
29420 structured-block:
29421 statement
29423 In practice, we're also interested in adding the statement to an
29424 outer node. So it is convenient if we work around the fact that
29425 cp_parser_statement calls add_stmt. */
29427 static unsigned
29428 cp_parser_begin_omp_structured_block (cp_parser *parser)
29430 unsigned save = parser->in_statement;
29432 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29433 This preserves the "not within loop or switch" style error messages
29434 for nonsense cases like
29435 void foo() {
29436 #pragma omp single
29437 break;
29440 if (parser->in_statement)
29441 parser->in_statement = IN_OMP_BLOCK;
29443 return save;
29446 static void
29447 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29449 parser->in_statement = save;
29452 static tree
29453 cp_parser_omp_structured_block (cp_parser *parser)
29455 tree stmt = begin_omp_structured_block ();
29456 unsigned int save = cp_parser_begin_omp_structured_block (parser);
29458 cp_parser_statement (parser, NULL_TREE, false, NULL);
29460 cp_parser_end_omp_structured_block (parser, save);
29461 return finish_omp_structured_block (stmt);
29464 /* OpenMP 2.5:
29465 # pragma omp atomic new-line
29466 expression-stmt
29468 expression-stmt:
29469 x binop= expr | x++ | ++x | x-- | --x
29470 binop:
29471 +, *, -, /, &, ^, |, <<, >>
29473 where x is an lvalue expression with scalar type.
29475 OpenMP 3.1:
29476 # pragma omp atomic new-line
29477 update-stmt
29479 # pragma omp atomic read new-line
29480 read-stmt
29482 # pragma omp atomic write new-line
29483 write-stmt
29485 # pragma omp atomic update new-line
29486 update-stmt
29488 # pragma omp atomic capture new-line
29489 capture-stmt
29491 # pragma omp atomic capture new-line
29492 capture-block
29494 read-stmt:
29495 v = x
29496 write-stmt:
29497 x = expr
29498 update-stmt:
29499 expression-stmt | x = x binop expr
29500 capture-stmt:
29501 v = expression-stmt
29502 capture-block:
29503 { v = x; update-stmt; } | { update-stmt; v = x; }
29505 OpenMP 4.0:
29506 update-stmt:
29507 expression-stmt | x = x binop expr | x = expr binop x
29508 capture-stmt:
29509 v = update-stmt
29510 capture-block:
29511 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29513 where x and v are lvalue expressions with scalar type. */
29515 static void
29516 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29518 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29519 tree rhs1 = NULL_TREE, orig_lhs;
29520 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29521 bool structured_block = false;
29522 bool seq_cst = false;
29524 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29526 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29527 const char *p = IDENTIFIER_POINTER (id);
29529 if (!strcmp (p, "seq_cst"))
29531 seq_cst = true;
29532 cp_lexer_consume_token (parser->lexer);
29533 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29534 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29535 cp_lexer_consume_token (parser->lexer);
29538 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29540 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29541 const char *p = IDENTIFIER_POINTER (id);
29543 if (!strcmp (p, "read"))
29544 code = OMP_ATOMIC_READ;
29545 else if (!strcmp (p, "write"))
29546 code = NOP_EXPR;
29547 else if (!strcmp (p, "update"))
29548 code = OMP_ATOMIC;
29549 else if (!strcmp (p, "capture"))
29550 code = OMP_ATOMIC_CAPTURE_NEW;
29551 else
29552 p = NULL;
29553 if (p)
29554 cp_lexer_consume_token (parser->lexer);
29556 if (!seq_cst)
29558 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29559 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29560 cp_lexer_consume_token (parser->lexer);
29562 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29564 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29565 const char *p = IDENTIFIER_POINTER (id);
29567 if (!strcmp (p, "seq_cst"))
29569 seq_cst = true;
29570 cp_lexer_consume_token (parser->lexer);
29574 cp_parser_require_pragma_eol (parser, pragma_tok);
29576 switch (code)
29578 case OMP_ATOMIC_READ:
29579 case NOP_EXPR: /* atomic write */
29580 v = cp_parser_unary_expression (parser);
29581 if (v == error_mark_node)
29582 goto saw_error;
29583 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29584 goto saw_error;
29585 if (code == NOP_EXPR)
29586 lhs = cp_parser_expression (parser);
29587 else
29588 lhs = cp_parser_unary_expression (parser);
29589 if (lhs == error_mark_node)
29590 goto saw_error;
29591 if (code == NOP_EXPR)
29593 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29594 opcode. */
29595 code = OMP_ATOMIC;
29596 rhs = lhs;
29597 lhs = v;
29598 v = NULL_TREE;
29600 goto done;
29601 case OMP_ATOMIC_CAPTURE_NEW:
29602 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29604 cp_lexer_consume_token (parser->lexer);
29605 structured_block = true;
29607 else
29609 v = cp_parser_unary_expression (parser);
29610 if (v == error_mark_node)
29611 goto saw_error;
29612 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29613 goto saw_error;
29615 default:
29616 break;
29619 restart:
29620 lhs = cp_parser_unary_expression (parser);
29621 orig_lhs = lhs;
29622 switch (TREE_CODE (lhs))
29624 case ERROR_MARK:
29625 goto saw_error;
29627 case POSTINCREMENT_EXPR:
29628 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29629 code = OMP_ATOMIC_CAPTURE_OLD;
29630 /* FALLTHROUGH */
29631 case PREINCREMENT_EXPR:
29632 lhs = TREE_OPERAND (lhs, 0);
29633 opcode = PLUS_EXPR;
29634 rhs = integer_one_node;
29635 break;
29637 case POSTDECREMENT_EXPR:
29638 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29639 code = OMP_ATOMIC_CAPTURE_OLD;
29640 /* FALLTHROUGH */
29641 case PREDECREMENT_EXPR:
29642 lhs = TREE_OPERAND (lhs, 0);
29643 opcode = MINUS_EXPR;
29644 rhs = integer_one_node;
29645 break;
29647 case COMPOUND_EXPR:
29648 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29649 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29650 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29651 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29652 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29653 (TREE_OPERAND (lhs, 1), 0), 0)))
29654 == BOOLEAN_TYPE)
29655 /* Undo effects of boolean_increment for post {in,de}crement. */
29656 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29657 /* FALLTHRU */
29658 case MODIFY_EXPR:
29659 if (TREE_CODE (lhs) == MODIFY_EXPR
29660 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29662 /* Undo effects of boolean_increment. */
29663 if (integer_onep (TREE_OPERAND (lhs, 1)))
29665 /* This is pre or post increment. */
29666 rhs = TREE_OPERAND (lhs, 1);
29667 lhs = TREE_OPERAND (lhs, 0);
29668 opcode = NOP_EXPR;
29669 if (code == OMP_ATOMIC_CAPTURE_NEW
29670 && !structured_block
29671 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29672 code = OMP_ATOMIC_CAPTURE_OLD;
29673 break;
29676 /* FALLTHRU */
29677 default:
29678 switch (cp_lexer_peek_token (parser->lexer)->type)
29680 case CPP_MULT_EQ:
29681 opcode = MULT_EXPR;
29682 break;
29683 case CPP_DIV_EQ:
29684 opcode = TRUNC_DIV_EXPR;
29685 break;
29686 case CPP_PLUS_EQ:
29687 opcode = PLUS_EXPR;
29688 break;
29689 case CPP_MINUS_EQ:
29690 opcode = MINUS_EXPR;
29691 break;
29692 case CPP_LSHIFT_EQ:
29693 opcode = LSHIFT_EXPR;
29694 break;
29695 case CPP_RSHIFT_EQ:
29696 opcode = RSHIFT_EXPR;
29697 break;
29698 case CPP_AND_EQ:
29699 opcode = BIT_AND_EXPR;
29700 break;
29701 case CPP_OR_EQ:
29702 opcode = BIT_IOR_EXPR;
29703 break;
29704 case CPP_XOR_EQ:
29705 opcode = BIT_XOR_EXPR;
29706 break;
29707 case CPP_EQ:
29708 enum cp_parser_prec oprec;
29709 cp_token *token;
29710 cp_lexer_consume_token (parser->lexer);
29711 cp_parser_parse_tentatively (parser);
29712 rhs1 = cp_parser_simple_cast_expression (parser);
29713 if (rhs1 == error_mark_node)
29715 cp_parser_abort_tentative_parse (parser);
29716 cp_parser_simple_cast_expression (parser);
29717 goto saw_error;
29719 token = cp_lexer_peek_token (parser->lexer);
29720 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29722 cp_parser_abort_tentative_parse (parser);
29723 cp_parser_parse_tentatively (parser);
29724 rhs = cp_parser_binary_expression (parser, false, true,
29725 PREC_NOT_OPERATOR, NULL);
29726 if (rhs == error_mark_node)
29728 cp_parser_abort_tentative_parse (parser);
29729 cp_parser_binary_expression (parser, false, true,
29730 PREC_NOT_OPERATOR, NULL);
29731 goto saw_error;
29733 switch (TREE_CODE (rhs))
29735 case MULT_EXPR:
29736 case TRUNC_DIV_EXPR:
29737 case PLUS_EXPR:
29738 case MINUS_EXPR:
29739 case LSHIFT_EXPR:
29740 case RSHIFT_EXPR:
29741 case BIT_AND_EXPR:
29742 case BIT_IOR_EXPR:
29743 case BIT_XOR_EXPR:
29744 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29746 if (cp_parser_parse_definitely (parser))
29748 opcode = TREE_CODE (rhs);
29749 rhs1 = TREE_OPERAND (rhs, 0);
29750 rhs = TREE_OPERAND (rhs, 1);
29751 goto stmt_done;
29753 else
29754 goto saw_error;
29756 break;
29757 default:
29758 break;
29760 cp_parser_abort_tentative_parse (parser);
29761 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29763 rhs = cp_parser_expression (parser);
29764 if (rhs == error_mark_node)
29765 goto saw_error;
29766 opcode = NOP_EXPR;
29767 rhs1 = NULL_TREE;
29768 goto stmt_done;
29770 cp_parser_error (parser,
29771 "invalid form of %<#pragma omp atomic%>");
29772 goto saw_error;
29774 if (!cp_parser_parse_definitely (parser))
29775 goto saw_error;
29776 switch (token->type)
29778 case CPP_SEMICOLON:
29779 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29781 code = OMP_ATOMIC_CAPTURE_OLD;
29782 v = lhs;
29783 lhs = NULL_TREE;
29784 lhs1 = rhs1;
29785 rhs1 = NULL_TREE;
29786 cp_lexer_consume_token (parser->lexer);
29787 goto restart;
29789 else if (structured_block)
29791 opcode = NOP_EXPR;
29792 rhs = rhs1;
29793 rhs1 = NULL_TREE;
29794 goto stmt_done;
29796 cp_parser_error (parser,
29797 "invalid form of %<#pragma omp atomic%>");
29798 goto saw_error;
29799 case CPP_MULT:
29800 opcode = MULT_EXPR;
29801 break;
29802 case CPP_DIV:
29803 opcode = TRUNC_DIV_EXPR;
29804 break;
29805 case CPP_PLUS:
29806 opcode = PLUS_EXPR;
29807 break;
29808 case CPP_MINUS:
29809 opcode = MINUS_EXPR;
29810 break;
29811 case CPP_LSHIFT:
29812 opcode = LSHIFT_EXPR;
29813 break;
29814 case CPP_RSHIFT:
29815 opcode = RSHIFT_EXPR;
29816 break;
29817 case CPP_AND:
29818 opcode = BIT_AND_EXPR;
29819 break;
29820 case CPP_OR:
29821 opcode = BIT_IOR_EXPR;
29822 break;
29823 case CPP_XOR:
29824 opcode = BIT_XOR_EXPR;
29825 break;
29826 default:
29827 cp_parser_error (parser,
29828 "invalid operator for %<#pragma omp atomic%>");
29829 goto saw_error;
29831 oprec = TOKEN_PRECEDENCE (token);
29832 gcc_assert (oprec != PREC_NOT_OPERATOR);
29833 if (commutative_tree_code (opcode))
29834 oprec = (enum cp_parser_prec) (oprec - 1);
29835 cp_lexer_consume_token (parser->lexer);
29836 rhs = cp_parser_binary_expression (parser, false, false,
29837 oprec, NULL);
29838 if (rhs == error_mark_node)
29839 goto saw_error;
29840 goto stmt_done;
29841 /* FALLTHROUGH */
29842 default:
29843 cp_parser_error (parser,
29844 "invalid operator for %<#pragma omp atomic%>");
29845 goto saw_error;
29847 cp_lexer_consume_token (parser->lexer);
29849 rhs = cp_parser_expression (parser);
29850 if (rhs == error_mark_node)
29851 goto saw_error;
29852 break;
29854 stmt_done:
29855 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29857 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29858 goto saw_error;
29859 v = cp_parser_unary_expression (parser);
29860 if (v == error_mark_node)
29861 goto saw_error;
29862 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29863 goto saw_error;
29864 lhs1 = cp_parser_unary_expression (parser);
29865 if (lhs1 == error_mark_node)
29866 goto saw_error;
29868 if (structured_block)
29870 cp_parser_consume_semicolon_at_end_of_statement (parser);
29871 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29873 done:
29874 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29875 if (!structured_block)
29876 cp_parser_consume_semicolon_at_end_of_statement (parser);
29877 return;
29879 saw_error:
29880 cp_parser_skip_to_end_of_block_or_statement (parser);
29881 if (structured_block)
29883 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29884 cp_lexer_consume_token (parser->lexer);
29885 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29887 cp_parser_skip_to_end_of_block_or_statement (parser);
29888 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29889 cp_lexer_consume_token (parser->lexer);
29895 /* OpenMP 2.5:
29896 # pragma omp barrier new-line */
29898 static void
29899 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29901 cp_parser_require_pragma_eol (parser, pragma_tok);
29902 finish_omp_barrier ();
29905 /* OpenMP 2.5:
29906 # pragma omp critical [(name)] new-line
29907 structured-block */
29909 static tree
29910 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29912 tree stmt, name = NULL;
29914 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29916 cp_lexer_consume_token (parser->lexer);
29918 name = cp_parser_identifier (parser);
29920 if (name == error_mark_node
29921 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29922 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29923 /*or_comma=*/false,
29924 /*consume_paren=*/true);
29925 if (name == error_mark_node)
29926 name = NULL;
29928 cp_parser_require_pragma_eol (parser, pragma_tok);
29930 stmt = cp_parser_omp_structured_block (parser);
29931 return c_finish_omp_critical (input_location, stmt, name);
29934 /* OpenMP 2.5:
29935 # pragma omp flush flush-vars[opt] new-line
29937 flush-vars:
29938 ( variable-list ) */
29940 static void
29941 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29943 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29944 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29945 cp_parser_require_pragma_eol (parser, pragma_tok);
29947 finish_omp_flush ();
29950 /* Helper function, to parse omp for increment expression. */
29952 static tree
29953 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29955 tree cond = cp_parser_binary_expression (parser, false, true,
29956 PREC_NOT_OPERATOR, NULL);
29957 if (cond == error_mark_node
29958 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29960 cp_parser_skip_to_end_of_statement (parser);
29961 return error_mark_node;
29964 switch (TREE_CODE (cond))
29966 case GT_EXPR:
29967 case GE_EXPR:
29968 case LT_EXPR:
29969 case LE_EXPR:
29970 break;
29971 case NE_EXPR:
29972 if (code == CILK_SIMD || code == CILK_FOR)
29973 break;
29974 /* Fall through: OpenMP disallows NE_EXPR. */
29975 default:
29976 return error_mark_node;
29979 /* If decl is an iterator, preserve LHS and RHS of the relational
29980 expr until finish_omp_for. */
29981 if (decl
29982 && (type_dependent_expression_p (decl)
29983 || CLASS_TYPE_P (TREE_TYPE (decl))))
29984 return cond;
29986 return build_x_binary_op (input_location, TREE_CODE (cond),
29987 TREE_OPERAND (cond, 0), ERROR_MARK,
29988 TREE_OPERAND (cond, 1), ERROR_MARK,
29989 /*overload=*/NULL, tf_warning_or_error);
29992 /* Helper function, to parse omp for increment expression. */
29994 static tree
29995 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29997 cp_token *token = cp_lexer_peek_token (parser->lexer);
29998 enum tree_code op;
29999 tree lhs, rhs;
30000 cp_id_kind idk;
30001 bool decl_first;
30003 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30005 op = (token->type == CPP_PLUS_PLUS
30006 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30007 cp_lexer_consume_token (parser->lexer);
30008 lhs = cp_parser_simple_cast_expression (parser);
30009 if (lhs != decl)
30010 return error_mark_node;
30011 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30014 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30015 if (lhs != decl)
30016 return error_mark_node;
30018 token = cp_lexer_peek_token (parser->lexer);
30019 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30021 op = (token->type == CPP_PLUS_PLUS
30022 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30023 cp_lexer_consume_token (parser->lexer);
30024 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30027 op = cp_parser_assignment_operator_opt (parser);
30028 if (op == ERROR_MARK)
30029 return error_mark_node;
30031 if (op != NOP_EXPR)
30033 rhs = cp_parser_assignment_expression (parser);
30034 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30035 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30038 lhs = cp_parser_binary_expression (parser, false, false,
30039 PREC_ADDITIVE_EXPRESSION, NULL);
30040 token = cp_lexer_peek_token (parser->lexer);
30041 decl_first = lhs == decl;
30042 if (decl_first)
30043 lhs = NULL_TREE;
30044 if (token->type != CPP_PLUS
30045 && token->type != CPP_MINUS)
30046 return error_mark_node;
30050 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30051 cp_lexer_consume_token (parser->lexer);
30052 rhs = cp_parser_binary_expression (parser, false, false,
30053 PREC_ADDITIVE_EXPRESSION, NULL);
30054 token = cp_lexer_peek_token (parser->lexer);
30055 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30057 if (lhs == NULL_TREE)
30059 if (op == PLUS_EXPR)
30060 lhs = rhs;
30061 else
30062 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30063 tf_warning_or_error);
30065 else
30066 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30067 ERROR_MARK, NULL, tf_warning_or_error);
30070 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30072 if (!decl_first)
30074 if (rhs != decl || op == MINUS_EXPR)
30075 return error_mark_node;
30076 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30078 else
30079 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30081 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30084 /* Parse the initialization statement of either an OpenMP for loop or
30085 a Cilk Plus for loop.
30087 Return true if the resulting construct should have an
30088 OMP_CLAUSE_PRIVATE added to it. */
30090 static bool
30091 cp_parser_omp_for_loop_init (cp_parser *parser,
30092 enum tree_code code,
30093 tree &this_pre_body,
30094 vec<tree, va_gc> *for_block,
30095 tree &init,
30096 tree &decl,
30097 tree &real_decl)
30099 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30100 return false;
30102 bool add_private_clause = false;
30104 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30106 init-expr:
30107 var = lb
30108 integer-type var = lb
30109 random-access-iterator-type var = lb
30110 pointer-type var = lb
30112 cp_decl_specifier_seq type_specifiers;
30114 /* First, try to parse as an initialized declaration. See
30115 cp_parser_condition, from whence the bulk of this is copied. */
30117 cp_parser_parse_tentatively (parser);
30118 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30119 /*is_trailing_return=*/false,
30120 &type_specifiers);
30121 if (cp_parser_parse_definitely (parser))
30123 /* If parsing a type specifier seq succeeded, then this
30124 MUST be a initialized declaration. */
30125 tree asm_specification, attributes;
30126 cp_declarator *declarator;
30128 declarator = cp_parser_declarator (parser,
30129 CP_PARSER_DECLARATOR_NAMED,
30130 /*ctor_dtor_or_conv_p=*/NULL,
30131 /*parenthesized_p=*/NULL,
30132 /*member_p=*/false,
30133 /*friend_p=*/false);
30134 attributes = cp_parser_attributes_opt (parser);
30135 asm_specification = cp_parser_asm_specification_opt (parser);
30137 if (declarator == cp_error_declarator)
30138 cp_parser_skip_to_end_of_statement (parser);
30140 else
30142 tree pushed_scope, auto_node;
30144 decl = start_decl (declarator, &type_specifiers,
30145 SD_INITIALIZED, attributes,
30146 /*prefix_attributes=*/NULL_TREE,
30147 &pushed_scope);
30149 auto_node = type_uses_auto (TREE_TYPE (decl));
30150 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30152 if (cp_lexer_next_token_is (parser->lexer,
30153 CPP_OPEN_PAREN))
30155 if (code != CILK_SIMD && code != CILK_FOR)
30156 error ("parenthesized initialization is not allowed in "
30157 "OpenMP %<for%> loop");
30158 else
30159 error ("parenthesized initialization is "
30160 "not allowed in for-loop");
30162 else
30163 /* Trigger an error. */
30164 cp_parser_require (parser, CPP_EQ, RT_EQ);
30166 init = error_mark_node;
30167 cp_parser_skip_to_end_of_statement (parser);
30169 else if (CLASS_TYPE_P (TREE_TYPE (decl))
30170 || type_dependent_expression_p (decl)
30171 || auto_node)
30173 bool is_direct_init, is_non_constant_init;
30175 init = cp_parser_initializer (parser,
30176 &is_direct_init,
30177 &is_non_constant_init);
30179 if (auto_node)
30181 TREE_TYPE (decl)
30182 = do_auto_deduction (TREE_TYPE (decl), init,
30183 auto_node);
30185 if (!CLASS_TYPE_P (TREE_TYPE (decl))
30186 && !type_dependent_expression_p (decl))
30187 goto non_class;
30190 cp_finish_decl (decl, init, !is_non_constant_init,
30191 asm_specification,
30192 LOOKUP_ONLYCONVERTING);
30193 if (CLASS_TYPE_P (TREE_TYPE (decl)))
30195 vec_safe_push (for_block, this_pre_body);
30196 init = NULL_TREE;
30198 else
30199 init = pop_stmt_list (this_pre_body);
30200 this_pre_body = NULL_TREE;
30202 else
30204 /* Consume '='. */
30205 cp_lexer_consume_token (parser->lexer);
30206 init = cp_parser_assignment_expression (parser);
30208 non_class:
30209 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30210 init = error_mark_node;
30211 else
30212 cp_finish_decl (decl, NULL_TREE,
30213 /*init_const_expr_p=*/false,
30214 asm_specification,
30215 LOOKUP_ONLYCONVERTING);
30218 if (pushed_scope)
30219 pop_scope (pushed_scope);
30222 else
30224 cp_id_kind idk;
30225 /* If parsing a type specifier sequence failed, then
30226 this MUST be a simple expression. */
30227 if (code == CILK_FOR)
30228 error ("%<_Cilk_for%> allows expression instead of declaration only "
30229 "in C, not in C++");
30230 cp_parser_parse_tentatively (parser);
30231 decl = cp_parser_primary_expression (parser, false, false,
30232 false, &idk);
30233 if (!cp_parser_error_occurred (parser)
30234 && decl
30235 && DECL_P (decl)
30236 && CLASS_TYPE_P (TREE_TYPE (decl)))
30238 tree rhs;
30240 cp_parser_parse_definitely (parser);
30241 cp_parser_require (parser, CPP_EQ, RT_EQ);
30242 rhs = cp_parser_assignment_expression (parser);
30243 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30244 decl, NOP_EXPR,
30245 rhs,
30246 tf_warning_or_error));
30247 add_private_clause = true;
30249 else
30251 decl = NULL;
30252 cp_parser_abort_tentative_parse (parser);
30253 init = cp_parser_expression (parser);
30254 if (init)
30256 if (TREE_CODE (init) == MODIFY_EXPR
30257 || TREE_CODE (init) == MODOP_EXPR)
30258 real_decl = TREE_OPERAND (init, 0);
30262 return add_private_clause;
30265 /* Parse the restricted form of the for statement allowed by OpenMP. */
30267 static tree
30268 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30269 tree *cclauses)
30271 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30272 tree real_decl, initv, condv, incrv, declv;
30273 tree this_pre_body, cl;
30274 location_t loc_first;
30275 bool collapse_err = false;
30276 int i, collapse = 1, nbraces = 0;
30277 vec<tree, va_gc> *for_block = make_tree_vector ();
30279 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30280 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30281 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30283 gcc_assert (collapse >= 1);
30285 declv = make_tree_vec (collapse);
30286 initv = make_tree_vec (collapse);
30287 condv = make_tree_vec (collapse);
30288 incrv = make_tree_vec (collapse);
30290 loc_first = cp_lexer_peek_token (parser->lexer)->location;
30292 for (i = 0; i < collapse; i++)
30294 int bracecount = 0;
30295 bool add_private_clause = false;
30296 location_t loc;
30298 if (code != CILK_FOR
30299 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30301 cp_parser_error (parser, "for statement expected");
30302 return NULL;
30304 if (code == CILK_FOR
30305 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
30307 cp_parser_error (parser, "_Cilk_for statement expected");
30308 return NULL;
30310 loc = cp_lexer_consume_token (parser->lexer)->location;
30312 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30313 return NULL;
30315 init = decl = real_decl = NULL;
30316 this_pre_body = push_stmt_list ();
30318 add_private_clause
30319 |= cp_parser_omp_for_loop_init (parser, code,
30320 this_pre_body, for_block,
30321 init, decl, real_decl);
30323 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30324 if (this_pre_body)
30326 this_pre_body = pop_stmt_list (this_pre_body);
30327 if (pre_body)
30329 tree t = pre_body;
30330 pre_body = push_stmt_list ();
30331 add_stmt (t);
30332 add_stmt (this_pre_body);
30333 pre_body = pop_stmt_list (pre_body);
30335 else
30336 pre_body = this_pre_body;
30339 if (decl)
30340 real_decl = decl;
30341 if (cclauses != NULL
30342 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30343 && real_decl != NULL_TREE)
30345 tree *c;
30346 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30347 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30348 && OMP_CLAUSE_DECL (*c) == real_decl)
30350 error_at (loc, "iteration variable %qD"
30351 " should not be firstprivate", real_decl);
30352 *c = OMP_CLAUSE_CHAIN (*c);
30354 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30355 && OMP_CLAUSE_DECL (*c) == real_decl)
30357 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
30358 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
30359 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
30360 OMP_CLAUSE_DECL (l) = real_decl;
30361 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
30362 if (code == OMP_SIMD)
30364 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30365 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
30367 else
30369 OMP_CLAUSE_CHAIN (l) = clauses;
30370 clauses = l;
30372 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
30373 CP_OMP_CLAUSE_INFO (*c) = NULL;
30374 add_private_clause = false;
30376 else
30378 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30379 && OMP_CLAUSE_DECL (*c) == real_decl)
30380 add_private_clause = false;
30381 c = &OMP_CLAUSE_CHAIN (*c);
30385 if (add_private_clause)
30387 tree c;
30388 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30390 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30391 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30392 && OMP_CLAUSE_DECL (c) == decl)
30393 break;
30394 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30395 && OMP_CLAUSE_DECL (c) == decl)
30396 error_at (loc, "iteration variable %qD "
30397 "should not be firstprivate",
30398 decl);
30399 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30400 && OMP_CLAUSE_DECL (c) == decl)
30401 error_at (loc, "iteration variable %qD should not be reduction",
30402 decl);
30404 if (c == NULL)
30406 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30407 OMP_CLAUSE_DECL (c) = decl;
30408 c = finish_omp_clauses (c);
30409 if (c)
30411 OMP_CLAUSE_CHAIN (c) = clauses;
30412 clauses = c;
30417 cond = NULL;
30418 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30419 cond = cp_parser_omp_for_cond (parser, decl, code);
30420 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30422 incr = NULL;
30423 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30425 /* If decl is an iterator, preserve the operator on decl
30426 until finish_omp_for. */
30427 if (real_decl
30428 && ((processing_template_decl
30429 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30430 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30431 incr = cp_parser_omp_for_incr (parser, real_decl);
30432 else
30433 incr = cp_parser_expression (parser);
30434 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30435 SET_EXPR_LOCATION (incr, input_location);
30438 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30439 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30440 /*or_comma=*/false,
30441 /*consume_paren=*/true);
30443 TREE_VEC_ELT (declv, i) = decl;
30444 TREE_VEC_ELT (initv, i) = init;
30445 TREE_VEC_ELT (condv, i) = cond;
30446 TREE_VEC_ELT (incrv, i) = incr;
30448 if (i == collapse - 1)
30449 break;
30451 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30452 in between the collapsed for loops to be still considered perfectly
30453 nested. Hopefully the final version clarifies this.
30454 For now handle (multiple) {'s and empty statements. */
30455 cp_parser_parse_tentatively (parser);
30458 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30459 break;
30460 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30462 cp_lexer_consume_token (parser->lexer);
30463 bracecount++;
30465 else if (bracecount
30466 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30467 cp_lexer_consume_token (parser->lexer);
30468 else
30470 loc = cp_lexer_peek_token (parser->lexer)->location;
30471 error_at (loc, "not enough collapsed for loops");
30472 collapse_err = true;
30473 cp_parser_abort_tentative_parse (parser);
30474 declv = NULL_TREE;
30475 break;
30478 while (1);
30480 if (declv)
30482 cp_parser_parse_definitely (parser);
30483 nbraces += bracecount;
30487 /* Note that we saved the original contents of this flag when we entered
30488 the structured block, and so we don't need to re-save it here. */
30489 if (code == CILK_SIMD || code == CILK_FOR)
30490 parser->in_statement = IN_CILK_SIMD_FOR;
30491 else
30492 parser->in_statement = IN_OMP_FOR;
30494 /* Note that the grammar doesn't call for a structured block here,
30495 though the loop as a whole is a structured block. */
30496 body = push_stmt_list ();
30497 cp_parser_statement (parser, NULL_TREE, false, NULL);
30498 body = pop_stmt_list (body);
30500 if (declv == NULL_TREE)
30501 ret = NULL_TREE;
30502 else
30503 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30504 pre_body, clauses);
30506 while (nbraces)
30508 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30510 cp_lexer_consume_token (parser->lexer);
30511 nbraces--;
30513 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30514 cp_lexer_consume_token (parser->lexer);
30515 else
30517 if (!collapse_err)
30519 error_at (cp_lexer_peek_token (parser->lexer)->location,
30520 "collapsed loops not perfectly nested");
30522 collapse_err = true;
30523 cp_parser_statement_seq_opt (parser, NULL);
30524 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30525 break;
30529 while (!for_block->is_empty ())
30530 add_stmt (pop_stmt_list (for_block->pop ()));
30531 release_tree_vector (for_block);
30533 return ret;
30536 /* Helper function for OpenMP parsing, split clauses and call
30537 finish_omp_clauses on each of the set of clauses afterwards. */
30539 static void
30540 cp_omp_split_clauses (location_t loc, enum tree_code code,
30541 omp_clause_mask mask, tree clauses, tree *cclauses)
30543 int i;
30544 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30545 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30546 if (cclauses[i])
30547 cclauses[i] = finish_omp_clauses (cclauses[i]);
30550 /* OpenMP 4.0:
30551 #pragma omp simd simd-clause[optseq] new-line
30552 for-loop */
30554 #define OMP_SIMD_CLAUSE_MASK \
30555 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30563 static tree
30564 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30565 char *p_name, omp_clause_mask mask, tree *cclauses)
30567 tree clauses, sb, ret;
30568 unsigned int save;
30569 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30571 strcat (p_name, " simd");
30572 mask |= OMP_SIMD_CLAUSE_MASK;
30573 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30575 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30576 cclauses == NULL);
30577 if (cclauses)
30579 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30580 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30583 sb = begin_omp_structured_block ();
30584 save = cp_parser_begin_omp_structured_block (parser);
30586 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30588 cp_parser_end_omp_structured_block (parser, save);
30589 add_stmt (finish_omp_structured_block (sb));
30591 return ret;
30594 /* OpenMP 2.5:
30595 #pragma omp for for-clause[optseq] new-line
30596 for-loop
30598 OpenMP 4.0:
30599 #pragma omp for simd for-simd-clause[optseq] new-line
30600 for-loop */
30602 #define OMP_FOR_CLAUSE_MASK \
30603 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30612 static tree
30613 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30614 char *p_name, omp_clause_mask mask, tree *cclauses)
30616 tree clauses, sb, ret;
30617 unsigned int save;
30618 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30620 strcat (p_name, " for");
30621 mask |= OMP_FOR_CLAUSE_MASK;
30622 if (cclauses)
30623 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30625 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30627 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30628 const char *p = IDENTIFIER_POINTER (id);
30630 if (strcmp (p, "simd") == 0)
30632 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30633 if (cclauses == NULL)
30634 cclauses = cclauses_buf;
30636 cp_lexer_consume_token (parser->lexer);
30637 if (!flag_openmp) /* flag_openmp_simd */
30638 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30639 cclauses);
30640 sb = begin_omp_structured_block ();
30641 save = cp_parser_begin_omp_structured_block (parser);
30642 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30643 cclauses);
30644 cp_parser_end_omp_structured_block (parser, save);
30645 tree body = finish_omp_structured_block (sb);
30646 if (ret == NULL)
30647 return ret;
30648 ret = make_node (OMP_FOR);
30649 TREE_TYPE (ret) = void_type_node;
30650 OMP_FOR_BODY (ret) = body;
30651 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30652 SET_EXPR_LOCATION (ret, loc);
30653 add_stmt (ret);
30654 return ret;
30657 if (!flag_openmp) /* flag_openmp_simd */
30659 cp_parser_require_pragma_eol (parser, pragma_tok);
30660 return NULL_TREE;
30663 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30664 cclauses == NULL);
30665 if (cclauses)
30667 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30668 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30671 sb = begin_omp_structured_block ();
30672 save = cp_parser_begin_omp_structured_block (parser);
30674 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30676 cp_parser_end_omp_structured_block (parser, save);
30677 add_stmt (finish_omp_structured_block (sb));
30679 return ret;
30682 /* OpenMP 2.5:
30683 # pragma omp master new-line
30684 structured-block */
30686 static tree
30687 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30689 cp_parser_require_pragma_eol (parser, pragma_tok);
30690 return c_finish_omp_master (input_location,
30691 cp_parser_omp_structured_block (parser));
30694 /* OpenMP 2.5:
30695 # pragma omp ordered new-line
30696 structured-block */
30698 static tree
30699 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30701 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30702 cp_parser_require_pragma_eol (parser, pragma_tok);
30703 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30706 /* OpenMP 2.5:
30708 section-scope:
30709 { section-sequence }
30711 section-sequence:
30712 section-directive[opt] structured-block
30713 section-sequence section-directive structured-block */
30715 static tree
30716 cp_parser_omp_sections_scope (cp_parser *parser)
30718 tree stmt, substmt;
30719 bool error_suppress = false;
30720 cp_token *tok;
30722 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30723 return NULL_TREE;
30725 stmt = push_stmt_list ();
30727 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30729 substmt = cp_parser_omp_structured_block (parser);
30730 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30731 add_stmt (substmt);
30734 while (1)
30736 tok = cp_lexer_peek_token (parser->lexer);
30737 if (tok->type == CPP_CLOSE_BRACE)
30738 break;
30739 if (tok->type == CPP_EOF)
30740 break;
30742 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30744 cp_lexer_consume_token (parser->lexer);
30745 cp_parser_require_pragma_eol (parser, tok);
30746 error_suppress = false;
30748 else if (!error_suppress)
30750 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30751 error_suppress = true;
30754 substmt = cp_parser_omp_structured_block (parser);
30755 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30756 add_stmt (substmt);
30758 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30760 substmt = pop_stmt_list (stmt);
30762 stmt = make_node (OMP_SECTIONS);
30763 TREE_TYPE (stmt) = void_type_node;
30764 OMP_SECTIONS_BODY (stmt) = substmt;
30766 add_stmt (stmt);
30767 return stmt;
30770 /* OpenMP 2.5:
30771 # pragma omp sections sections-clause[optseq] newline
30772 sections-scope */
30774 #define OMP_SECTIONS_CLAUSE_MASK \
30775 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30781 static tree
30782 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30783 char *p_name, omp_clause_mask mask, tree *cclauses)
30785 tree clauses, ret;
30786 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30788 strcat (p_name, " sections");
30789 mask |= OMP_SECTIONS_CLAUSE_MASK;
30790 if (cclauses)
30791 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30793 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30794 cclauses == NULL);
30795 if (cclauses)
30797 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30798 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30801 ret = cp_parser_omp_sections_scope (parser);
30802 if (ret)
30803 OMP_SECTIONS_CLAUSES (ret) = clauses;
30805 return ret;
30808 /* OpenMP 2.5:
30809 # pragma omp parallel parallel-clause[optseq] new-line
30810 structured-block
30811 # pragma omp parallel for parallel-for-clause[optseq] new-line
30812 structured-block
30813 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30814 structured-block
30816 OpenMP 4.0:
30817 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30818 structured-block */
30820 #define OMP_PARALLEL_CLAUSE_MASK \
30821 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30831 static tree
30832 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30833 char *p_name, omp_clause_mask mask, tree *cclauses)
30835 tree stmt, clauses, block;
30836 unsigned int save;
30837 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30839 strcat (p_name, " parallel");
30840 mask |= OMP_PARALLEL_CLAUSE_MASK;
30842 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30844 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30845 if (cclauses == NULL)
30846 cclauses = cclauses_buf;
30848 cp_lexer_consume_token (parser->lexer);
30849 if (!flag_openmp) /* flag_openmp_simd */
30850 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30851 block = begin_omp_parallel ();
30852 save = cp_parser_begin_omp_structured_block (parser);
30853 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30854 cp_parser_end_omp_structured_block (parser, save);
30855 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30856 block);
30857 if (ret == NULL_TREE)
30858 return ret;
30859 OMP_PARALLEL_COMBINED (stmt) = 1;
30860 return stmt;
30862 else if (cclauses)
30864 error_at (loc, "expected %<for%> after %qs", p_name);
30865 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30866 return NULL_TREE;
30868 else if (!flag_openmp) /* flag_openmp_simd */
30870 cp_parser_require_pragma_eol (parser, pragma_tok);
30871 return NULL_TREE;
30873 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30875 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30876 const char *p = IDENTIFIER_POINTER (id);
30877 if (strcmp (p, "sections") == 0)
30879 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30880 cclauses = cclauses_buf;
30882 cp_lexer_consume_token (parser->lexer);
30883 block = begin_omp_parallel ();
30884 save = cp_parser_begin_omp_structured_block (parser);
30885 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30886 cp_parser_end_omp_structured_block (parser, save);
30887 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30888 block);
30889 OMP_PARALLEL_COMBINED (stmt) = 1;
30890 return stmt;
30894 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30896 block = begin_omp_parallel ();
30897 save = cp_parser_begin_omp_structured_block (parser);
30898 cp_parser_statement (parser, NULL_TREE, false, NULL);
30899 cp_parser_end_omp_structured_block (parser, save);
30900 stmt = finish_omp_parallel (clauses, block);
30901 return stmt;
30904 /* OpenMP 2.5:
30905 # pragma omp single single-clause[optseq] new-line
30906 structured-block */
30908 #define OMP_SINGLE_CLAUSE_MASK \
30909 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30914 static tree
30915 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30917 tree stmt = make_node (OMP_SINGLE);
30918 TREE_TYPE (stmt) = void_type_node;
30920 OMP_SINGLE_CLAUSES (stmt)
30921 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30922 "#pragma omp single", pragma_tok);
30923 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30925 return add_stmt (stmt);
30928 /* OpenMP 3.0:
30929 # pragma omp task task-clause[optseq] new-line
30930 structured-block */
30932 #define OMP_TASK_CLAUSE_MASK \
30933 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30943 static tree
30944 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30946 tree clauses, block;
30947 unsigned int save;
30949 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30950 "#pragma omp task", pragma_tok);
30951 block = begin_omp_task ();
30952 save = cp_parser_begin_omp_structured_block (parser);
30953 cp_parser_statement (parser, NULL_TREE, false, NULL);
30954 cp_parser_end_omp_structured_block (parser, save);
30955 return finish_omp_task (clauses, block);
30958 /* OpenMP 3.0:
30959 # pragma omp taskwait new-line */
30961 static void
30962 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30964 cp_parser_require_pragma_eol (parser, pragma_tok);
30965 finish_omp_taskwait ();
30968 /* OpenMP 3.1:
30969 # pragma omp taskyield new-line */
30971 static void
30972 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30974 cp_parser_require_pragma_eol (parser, pragma_tok);
30975 finish_omp_taskyield ();
30978 /* OpenMP 4.0:
30979 # pragma omp taskgroup new-line
30980 structured-block */
30982 static tree
30983 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30985 cp_parser_require_pragma_eol (parser, pragma_tok);
30986 return c_finish_omp_taskgroup (input_location,
30987 cp_parser_omp_structured_block (parser));
30991 /* OpenMP 2.5:
30992 # pragma omp threadprivate (variable-list) */
30994 static void
30995 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30997 tree vars;
30999 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31000 cp_parser_require_pragma_eol (parser, pragma_tok);
31002 finish_omp_threadprivate (vars);
31005 /* OpenMP 4.0:
31006 # pragma omp cancel cancel-clause[optseq] new-line */
31008 #define OMP_CANCEL_CLAUSE_MASK \
31009 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
31013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31015 static void
31016 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31018 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31019 "#pragma omp cancel", pragma_tok);
31020 finish_omp_cancel (clauses);
31023 /* OpenMP 4.0:
31024 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31026 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31027 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31032 static void
31033 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31035 tree clauses;
31036 bool point_seen = false;
31038 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31040 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31041 const char *p = IDENTIFIER_POINTER (id);
31043 if (strcmp (p, "point") == 0)
31045 cp_lexer_consume_token (parser->lexer);
31046 point_seen = true;
31049 if (!point_seen)
31051 cp_parser_error (parser, "expected %<point%>");
31052 cp_parser_require_pragma_eol (parser, pragma_tok);
31053 return;
31056 clauses = cp_parser_omp_all_clauses (parser,
31057 OMP_CANCELLATION_POINT_CLAUSE_MASK,
31058 "#pragma omp cancellation point",
31059 pragma_tok);
31060 finish_omp_cancellation_point (clauses);
31063 /* OpenMP 4.0:
31064 #pragma omp distribute distribute-clause[optseq] new-line
31065 for-loop */
31067 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31068 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31073 static tree
31074 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31075 char *p_name, omp_clause_mask mask, tree *cclauses)
31077 tree clauses, sb, ret;
31078 unsigned int save;
31079 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31081 strcat (p_name, " distribute");
31082 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31084 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31086 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31087 const char *p = IDENTIFIER_POINTER (id);
31088 bool simd = false;
31089 bool parallel = false;
31091 if (strcmp (p, "simd") == 0)
31092 simd = true;
31093 else
31094 parallel = strcmp (p, "parallel") == 0;
31095 if (parallel || simd)
31097 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31098 if (cclauses == NULL)
31099 cclauses = cclauses_buf;
31100 cp_lexer_consume_token (parser->lexer);
31101 if (!flag_openmp) /* flag_openmp_simd */
31103 if (simd)
31104 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31105 cclauses);
31106 else
31107 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31108 cclauses);
31110 sb = begin_omp_structured_block ();
31111 save = cp_parser_begin_omp_structured_block (parser);
31112 if (simd)
31113 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31114 cclauses);
31115 else
31116 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31117 cclauses);
31118 cp_parser_end_omp_structured_block (parser, save);
31119 tree body = finish_omp_structured_block (sb);
31120 if (ret == NULL)
31121 return ret;
31122 ret = make_node (OMP_DISTRIBUTE);
31123 TREE_TYPE (ret) = void_type_node;
31124 OMP_FOR_BODY (ret) = body;
31125 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31126 SET_EXPR_LOCATION (ret, loc);
31127 add_stmt (ret);
31128 return ret;
31131 if (!flag_openmp) /* flag_openmp_simd */
31133 cp_parser_require_pragma_eol (parser, pragma_tok);
31134 return NULL_TREE;
31137 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31138 cclauses == NULL);
31139 if (cclauses)
31141 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31142 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31145 sb = begin_omp_structured_block ();
31146 save = cp_parser_begin_omp_structured_block (parser);
31148 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31150 cp_parser_end_omp_structured_block (parser, save);
31151 add_stmt (finish_omp_structured_block (sb));
31153 return ret;
31156 /* OpenMP 4.0:
31157 # pragma omp teams teams-clause[optseq] new-line
31158 structured-block */
31160 #define OMP_TEAMS_CLAUSE_MASK \
31161 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31169 static tree
31170 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31171 char *p_name, omp_clause_mask mask, tree *cclauses)
31173 tree clauses, sb, ret;
31174 unsigned int save;
31175 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31177 strcat (p_name, " teams");
31178 mask |= OMP_TEAMS_CLAUSE_MASK;
31180 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31182 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31183 const char *p = IDENTIFIER_POINTER (id);
31184 if (strcmp (p, "distribute") == 0)
31186 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31187 if (cclauses == NULL)
31188 cclauses = cclauses_buf;
31190 cp_lexer_consume_token (parser->lexer);
31191 if (!flag_openmp) /* flag_openmp_simd */
31192 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31193 cclauses);
31194 sb = begin_omp_structured_block ();
31195 save = cp_parser_begin_omp_structured_block (parser);
31196 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31197 cclauses);
31198 cp_parser_end_omp_structured_block (parser, save);
31199 tree body = finish_omp_structured_block (sb);
31200 if (ret == NULL)
31201 return ret;
31202 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31203 ret = make_node (OMP_TEAMS);
31204 TREE_TYPE (ret) = void_type_node;
31205 OMP_TEAMS_CLAUSES (ret) = clauses;
31206 OMP_TEAMS_BODY (ret) = body;
31207 return add_stmt (ret);
31210 if (!flag_openmp) /* flag_openmp_simd */
31212 cp_parser_require_pragma_eol (parser, pragma_tok);
31213 return NULL_TREE;
31216 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31217 cclauses == NULL);
31218 if (cclauses)
31220 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31221 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31224 tree stmt = make_node (OMP_TEAMS);
31225 TREE_TYPE (stmt) = void_type_node;
31226 OMP_TEAMS_CLAUSES (stmt) = clauses;
31227 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31229 return add_stmt (stmt);
31232 /* OpenMP 4.0:
31233 # pragma omp target data target-data-clause[optseq] new-line
31234 structured-block */
31236 #define OMP_TARGET_DATA_CLAUSE_MASK \
31237 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31241 static tree
31242 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31244 tree stmt = make_node (OMP_TARGET_DATA);
31245 TREE_TYPE (stmt) = void_type_node;
31247 OMP_TARGET_DATA_CLAUSES (stmt)
31248 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31249 "#pragma omp target data", pragma_tok);
31250 keep_next_level (true);
31251 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31253 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31254 return add_stmt (stmt);
31257 /* OpenMP 4.0:
31258 # pragma omp target update target-update-clause[optseq] new-line */
31260 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31261 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31266 static bool
31267 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31268 enum pragma_context context)
31270 if (context == pragma_stmt)
31272 error_at (pragma_tok->location,
31273 "%<#pragma omp target update%> may only be "
31274 "used in compound statements");
31275 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31276 return false;
31279 tree clauses
31280 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31281 "#pragma omp target update", pragma_tok);
31282 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31283 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31285 error_at (pragma_tok->location,
31286 "%<#pragma omp target update must contain at least one "
31287 "%<from%> or %<to%> clauses");
31288 return false;
31291 tree stmt = make_node (OMP_TARGET_UPDATE);
31292 TREE_TYPE (stmt) = void_type_node;
31293 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31294 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31295 add_stmt (stmt);
31296 return false;
31299 /* OpenMP 4.0:
31300 # pragma omp target target-clause[optseq] new-line
31301 structured-block */
31303 #define OMP_TARGET_CLAUSE_MASK \
31304 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31308 static bool
31309 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31310 enum pragma_context context)
31312 if (context != pragma_stmt && context != pragma_compound)
31314 cp_parser_error (parser, "expected declaration specifiers");
31315 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31316 return false;
31319 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31321 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31322 const char *p = IDENTIFIER_POINTER (id);
31324 if (strcmp (p, "teams") == 0)
31326 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31327 char p_name[sizeof ("#pragma omp target teams distribute "
31328 "parallel for simd")];
31330 cp_lexer_consume_token (parser->lexer);
31331 strcpy (p_name, "#pragma omp target");
31332 if (!flag_openmp) /* flag_openmp_simd */
31334 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31335 OMP_TARGET_CLAUSE_MASK,
31336 cclauses);
31337 return stmt != NULL_TREE;
31339 keep_next_level (true);
31340 tree sb = begin_omp_structured_block ();
31341 unsigned save = cp_parser_begin_omp_structured_block (parser);
31342 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31343 OMP_TARGET_CLAUSE_MASK, cclauses);
31344 cp_parser_end_omp_structured_block (parser, save);
31345 tree body = finish_omp_structured_block (sb);
31346 if (ret == NULL_TREE)
31347 return false;
31348 tree stmt = make_node (OMP_TARGET);
31349 TREE_TYPE (stmt) = void_type_node;
31350 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31351 OMP_TARGET_BODY (stmt) = body;
31352 add_stmt (stmt);
31353 return true;
31355 else if (!flag_openmp) /* flag_openmp_simd */
31357 cp_parser_require_pragma_eol (parser, pragma_tok);
31358 return false;
31360 else if (strcmp (p, "data") == 0)
31362 cp_lexer_consume_token (parser->lexer);
31363 cp_parser_omp_target_data (parser, pragma_tok);
31364 return true;
31366 else if (strcmp (p, "update") == 0)
31368 cp_lexer_consume_token (parser->lexer);
31369 return cp_parser_omp_target_update (parser, pragma_tok, context);
31373 tree stmt = make_node (OMP_TARGET);
31374 TREE_TYPE (stmt) = void_type_node;
31376 OMP_TARGET_CLAUSES (stmt)
31377 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31378 "#pragma omp target", pragma_tok);
31379 keep_next_level (true);
31380 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31382 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31383 add_stmt (stmt);
31384 return true;
31387 /* OpenACC 2.0:
31388 # pragma acc cache (variable-list) new-line
31391 static tree
31392 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
31394 tree stmt, clauses;
31396 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
31397 clauses = finish_omp_clauses (clauses);
31399 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
31401 stmt = make_node (OACC_CACHE);
31402 TREE_TYPE (stmt) = void_type_node;
31403 OACC_CACHE_CLAUSES (stmt) = clauses;
31404 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31405 add_stmt (stmt);
31407 return stmt;
31410 /* OpenACC 2.0:
31411 # pragma acc data oacc-data-clause[optseq] new-line
31412 structured-block */
31414 #define OACC_DATA_CLAUSE_MASK \
31415 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
31416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
31418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
31419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
31420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
31422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
31423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
31424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
31425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE))
31427 static tree
31428 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
31430 tree stmt, clauses, block;
31431 unsigned int save;
31433 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
31434 "#pragma acc data", pragma_tok);
31436 block = begin_omp_parallel ();
31437 save = cp_parser_begin_omp_structured_block (parser);
31438 cp_parser_statement (parser, NULL_TREE, false, NULL);
31439 cp_parser_end_omp_structured_block (parser, save);
31440 stmt = finish_oacc_data (clauses, block);
31441 return stmt;
31444 /* OpenACC 2.0:
31445 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31449 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31451 LOC is the location of the #pragma token.
31454 #define OACC_ENTER_DATA_CLAUSE_MASK \
31455 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
31457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
31459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
31460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) \
31461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT) )
31463 #define OACC_EXIT_DATA_CLAUSE_MASK \
31464 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
31466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
31467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DELETE) \
31468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT) )
31470 static tree
31471 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
31472 bool enter)
31474 tree stmt, clauses;
31476 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
31477 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31479 cp_parser_error (parser, enter
31480 ? "expected %<data%> in %<#pragma acc enter data%>"
31481 : "expected %<data%> in %<#pragma acc exit data%>");
31482 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31483 return NULL_TREE;
31486 const char *p =
31487 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
31488 if (strcmp (p, "data") != 0)
31490 cp_parser_error (parser, "invalid pragma");
31491 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31492 return NULL_TREE;
31495 cp_lexer_consume_token (parser->lexer);
31497 if (enter)
31498 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
31499 "#pragma acc enter data", pragma_tok);
31500 else
31501 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
31502 "#pragma acc exit data", pragma_tok);
31504 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31506 error_at (pragma_tok->location,
31507 "%<#pragma acc enter data%> has no data movement clause");
31508 return NULL_TREE;
31511 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
31512 TREE_TYPE (stmt) = void_type_node;
31513 if (enter)
31514 OACC_ENTER_DATA_CLAUSES (stmt) = clauses;
31515 else
31516 OACC_EXIT_DATA_CLAUSES (stmt) = clauses;
31517 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31518 add_stmt (stmt);
31519 return stmt;
31522 /* OpenACC 2.0:
31523 # pragma acc kernels oacc-kernels-clause[optseq] new-line
31524 structured-block */
31526 #define OACC_KERNELS_CLAUSE_MASK \
31527 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
31528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
31529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
31531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
31532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
31533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
31535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
31536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
31537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
31538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) \
31539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT))
31541 static tree
31542 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
31544 tree stmt, clauses, block;
31545 unsigned int save;
31547 clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
31548 "#pragma acc kernels", pragma_tok);
31550 block = begin_omp_parallel ();
31551 save = cp_parser_begin_omp_structured_block (parser);
31552 cp_parser_statement (parser, NULL_TREE, false, NULL);
31553 cp_parser_end_omp_structured_block (parser, save);
31554 stmt = finish_oacc_kernels (clauses, block);
31555 return stmt;
31558 /* OpenACC 2.0:
31559 # pragma acc loop oacc-loop-clause[optseq] new-line
31560 structured-block */
31562 #define OACC_LOOP_CLAUSE_MASK \
31563 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
31564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION))
31566 static tree
31567 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
31569 tree stmt, clauses, block;
31570 int save;
31572 clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
31573 "#pragma acc loop", pragma_tok);
31575 block = begin_omp_structured_block ();
31576 save = cp_parser_begin_omp_structured_block (parser);
31577 stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
31578 cp_parser_end_omp_structured_block (parser, save);
31579 add_stmt (finish_omp_structured_block (block));
31580 return stmt;
31583 /* OpenACC 2.0:
31584 # pragma acc parallel oacc-parallel-clause[optseq] new-line
31585 structured-block */
31587 #define OACC_PARALLEL_CLAUSE_MASK \
31588 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
31589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
31590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
31592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
31593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
31594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_GANGS) \
31596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_WORKERS) \
31597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
31598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
31599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
31600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
31601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) \
31602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_VECTOR_LENGTH) \
31604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT))
31606 static tree
31607 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
31609 tree stmt, clauses, block;
31610 unsigned int save;
31612 clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
31613 "#pragma acc parallel", pragma_tok);
31615 block = begin_omp_parallel ();
31616 save = cp_parser_begin_omp_structured_block (parser);
31617 cp_parser_statement (parser, NULL_TREE, false, NULL);
31618 cp_parser_end_omp_structured_block (parser, save);
31619 stmt = finish_oacc_parallel (clauses, block);
31620 return stmt;
31623 /* OpenACC 2.0:
31624 # pragma acc update oacc-update-clause[optseq] new-line
31627 #define OACC_UPDATE_CLAUSE_MASK \
31628 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
31629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HOST) \
31631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SELF) \
31633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT))
31635 static tree
31636 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
31638 tree stmt, clauses;
31640 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
31641 "#pragma acc update", pragma_tok);
31643 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31645 error_at (pragma_tok->location,
31646 "%<#pragma acc update%> must contain at least one "
31647 "%<device%> or %<host/self%> clause");
31648 return NULL_TREE;
31651 stmt = make_node (OACC_UPDATE);
31652 TREE_TYPE (stmt) = void_type_node;
31653 OACC_UPDATE_CLAUSES (stmt) = clauses;
31654 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31655 add_stmt (stmt);
31656 return stmt;
31659 /* OpenACC 2.0:
31660 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31662 LOC is the location of the #pragma token.
31665 #define OACC_WAIT_CLAUSE_MASK \
31666 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC))
31668 static tree
31669 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
31671 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
31672 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31674 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31675 list = cp_parser_oacc_wait_list (parser, loc, list);
31677 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
31678 "#pragma acc wait", pragma_tok);
31680 stmt = c_finish_oacc_wait (loc, list, clauses);
31682 return stmt;
31685 /* OpenMP 4.0:
31686 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31688 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31689 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31696 static void
31697 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31698 enum pragma_context context)
31700 bool first_p = parser->omp_declare_simd == NULL;
31701 cp_omp_declare_simd_data data;
31702 if (first_p)
31704 data.error_seen = false;
31705 data.fndecl_seen = false;
31706 data.tokens = vNULL;
31707 parser->omp_declare_simd = &data;
31709 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31710 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31711 cp_lexer_consume_token (parser->lexer);
31712 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31713 parser->omp_declare_simd->error_seen = true;
31714 cp_parser_require_pragma_eol (parser, pragma_tok);
31715 struct cp_token_cache *cp
31716 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31717 parser->omp_declare_simd->tokens.safe_push (cp);
31718 if (first_p)
31720 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31721 cp_parser_pragma (parser, context);
31722 switch (context)
31724 case pragma_external:
31725 cp_parser_declaration (parser);
31726 break;
31727 case pragma_member:
31728 cp_parser_member_declaration (parser);
31729 break;
31730 case pragma_objc_icode:
31731 cp_parser_block_declaration (parser, /*statement_p=*/false);
31732 break;
31733 default:
31734 cp_parser_declaration_statement (parser);
31735 break;
31737 if (parser->omp_declare_simd
31738 && !parser->omp_declare_simd->error_seen
31739 && !parser->omp_declare_simd->fndecl_seen)
31740 error_at (pragma_tok->location,
31741 "%<#pragma omp declare simd%> not immediately followed by "
31742 "function declaration or definition");
31743 data.tokens.release ();
31744 parser->omp_declare_simd = NULL;
31748 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31749 This function is modelled similar to the late parsing of omp declare
31750 simd. */
31752 static tree
31753 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31755 struct cp_token_cache *ce;
31756 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31757 int ii = 0;
31759 if (parser->omp_declare_simd != NULL)
31761 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31762 " marked as a Cilk Plus SIMD-enabled function");
31763 XDELETE (parser->cilk_simd_fn_info);
31764 parser->cilk_simd_fn_info = NULL;
31765 return attrs;
31767 if (!info->error_seen && info->fndecl_seen)
31769 error ("vector attribute not immediately followed by a single function"
31770 " declaration or definition");
31771 info->error_seen = true;
31773 if (info->error_seen)
31774 return attrs;
31776 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31778 tree c, cl;
31780 cp_parser_push_lexer_for_tokens (parser, ce);
31781 parser->lexer->in_pragma = true;
31782 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31783 "SIMD-enabled functions attribute",
31784 NULL);
31785 cp_parser_pop_lexer (parser);
31786 if (cl)
31787 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31789 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31790 TREE_CHAIN (c) = attrs;
31791 attrs = c;
31793 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31794 TREE_CHAIN (c) = attrs;
31795 if (processing_template_decl)
31796 ATTR_IS_DEPENDENT (c) = 1;
31797 attrs = c;
31799 info->fndecl_seen = true;
31800 XDELETE (parser->cilk_simd_fn_info);
31801 parser->cilk_simd_fn_info = NULL;
31802 return attrs;
31805 /* Finalize #pragma omp declare simd clauses after direct declarator has
31806 been parsed, and put that into "omp declare simd" attribute. */
31808 static tree
31809 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31811 struct cp_token_cache *ce;
31812 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31813 int i;
31815 if (!data->error_seen && data->fndecl_seen)
31817 error ("%<#pragma omp declare simd%> not immediately followed by "
31818 "a single function declaration or definition");
31819 data->error_seen = true;
31820 return attrs;
31822 if (data->error_seen)
31823 return attrs;
31825 FOR_EACH_VEC_ELT (data->tokens, i, ce)
31827 tree c, cl;
31829 cp_parser_push_lexer_for_tokens (parser, ce);
31830 parser->lexer->in_pragma = true;
31831 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31832 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31833 cp_lexer_consume_token (parser->lexer);
31834 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31835 "#pragma omp declare simd", pragma_tok);
31836 cp_parser_pop_lexer (parser);
31837 if (cl)
31838 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31839 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31840 TREE_CHAIN (c) = attrs;
31841 if (processing_template_decl)
31842 ATTR_IS_DEPENDENT (c) = 1;
31843 attrs = c;
31846 data->fndecl_seen = true;
31847 return attrs;
31851 /* OpenMP 4.0:
31852 # pragma omp declare target new-line
31853 declarations and definitions
31854 # pragma omp end declare target new-line */
31856 static void
31857 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31859 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31860 scope_chain->omp_declare_target_attribute++;
31863 static void
31864 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
31866 const char *p = "";
31867 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31869 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31870 p = IDENTIFIER_POINTER (id);
31872 if (strcmp (p, "declare") == 0)
31874 cp_lexer_consume_token (parser->lexer);
31875 p = "";
31876 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31878 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31879 p = IDENTIFIER_POINTER (id);
31881 if (strcmp (p, "target") == 0)
31882 cp_lexer_consume_token (parser->lexer);
31883 else
31885 cp_parser_error (parser, "expected %<target%>");
31886 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31887 return;
31890 else
31892 cp_parser_error (parser, "expected %<declare%>");
31893 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31894 return;
31896 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31897 if (!scope_chain->omp_declare_target_attribute)
31898 error_at (pragma_tok->location,
31899 "%<#pragma omp end declare target%> without corresponding "
31900 "%<#pragma omp declare target%>");
31901 else
31902 scope_chain->omp_declare_target_attribute--;
31905 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31906 expression and optional initializer clause of
31907 #pragma omp declare reduction. We store the expression(s) as
31908 either 3, 6 or 7 special statements inside of the artificial function's
31909 body. The first two statements are DECL_EXPRs for the artificial
31910 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
31911 expression that uses those variables.
31912 If there was any INITIALIZER clause, this is followed by further statements,
31913 the fourth and fifth statements are DECL_EXPRs for the artificial
31914 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
31915 constructor variant (first token after open paren is not omp_priv),
31916 then the sixth statement is a statement with the function call expression
31917 that uses the OMP_PRIV and optionally OMP_ORIG variable.
31918 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
31919 to initialize the OMP_PRIV artificial variable and there is seventh
31920 statement, a DECL_EXPR of the OMP_PRIV statement again. */
31922 static bool
31923 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
31925 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
31926 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
31927 type = TREE_TYPE (type);
31928 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
31929 DECL_ARTIFICIAL (omp_out) = 1;
31930 pushdecl (omp_out);
31931 add_decl_expr (omp_out);
31932 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
31933 DECL_ARTIFICIAL (omp_in) = 1;
31934 pushdecl (omp_in);
31935 add_decl_expr (omp_in);
31936 tree combiner;
31937 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
31939 keep_next_level (true);
31940 tree block = begin_omp_structured_block ();
31941 combiner = cp_parser_expression (parser);
31942 finish_expr_stmt (combiner);
31943 block = finish_omp_structured_block (block);
31944 add_stmt (block);
31946 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31947 return false;
31949 const char *p = "";
31950 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31952 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31953 p = IDENTIFIER_POINTER (id);
31956 if (strcmp (p, "initializer") == 0)
31958 cp_lexer_consume_token (parser->lexer);
31959 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31960 return false;
31962 p = "";
31963 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31965 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31966 p = IDENTIFIER_POINTER (id);
31969 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
31970 DECL_ARTIFICIAL (omp_priv) = 1;
31971 pushdecl (omp_priv);
31972 add_decl_expr (omp_priv);
31973 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
31974 DECL_ARTIFICIAL (omp_orig) = 1;
31975 pushdecl (omp_orig);
31976 add_decl_expr (omp_orig);
31978 keep_next_level (true);
31979 block = begin_omp_structured_block ();
31981 bool ctor = false;
31982 if (strcmp (p, "omp_priv") == 0)
31984 bool is_direct_init, is_non_constant_init;
31985 ctor = true;
31986 cp_lexer_consume_token (parser->lexer);
31987 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
31988 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
31989 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31990 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
31991 == CPP_CLOSE_PAREN
31992 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
31993 == CPP_CLOSE_PAREN))
31995 finish_omp_structured_block (block);
31996 error ("invalid initializer clause");
31997 return false;
31999 initializer = cp_parser_initializer (parser, &is_direct_init,
32000 &is_non_constant_init);
32001 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
32002 NULL_TREE, LOOKUP_ONLYCONVERTING);
32004 else
32006 cp_parser_parse_tentatively (parser);
32007 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
32008 /*check_dependency_p=*/true,
32009 /*template_p=*/NULL,
32010 /*declarator_p=*/false,
32011 /*optional_p=*/false);
32012 vec<tree, va_gc> *args;
32013 if (fn_name == error_mark_node
32014 || cp_parser_error_occurred (parser)
32015 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32016 || ((args = cp_parser_parenthesized_expression_list
32017 (parser, non_attr, /*cast_p=*/false,
32018 /*allow_expansion_p=*/true,
32019 /*non_constant_p=*/NULL)),
32020 cp_parser_error_occurred (parser)))
32022 finish_omp_structured_block (block);
32023 cp_parser_abort_tentative_parse (parser);
32024 cp_parser_error (parser, "expected id-expression (arguments)");
32025 return false;
32027 unsigned int i;
32028 tree arg;
32029 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
32030 if (arg == omp_priv
32031 || (TREE_CODE (arg) == ADDR_EXPR
32032 && TREE_OPERAND (arg, 0) == omp_priv))
32033 break;
32034 cp_parser_abort_tentative_parse (parser);
32035 if (arg == NULL_TREE)
32036 error ("one of the initializer call arguments should be %<omp_priv%>"
32037 " or %<&omp_priv%>");
32038 initializer = cp_parser_postfix_expression (parser, false, false, false,
32039 false, NULL);
32040 finish_expr_stmt (initializer);
32043 block = finish_omp_structured_block (block);
32044 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
32045 finish_expr_stmt (block);
32047 if (ctor)
32048 add_decl_expr (omp_orig);
32050 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32051 return false;
32054 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
32055 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
32057 return true;
32060 /* OpenMP 4.0
32061 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32062 initializer-clause[opt] new-line
32064 initializer-clause:
32065 initializer (omp_priv initializer)
32066 initializer (function-name (argument-list)) */
32068 static void
32069 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
32070 enum pragma_context)
32072 auto_vec<tree> types;
32073 enum tree_code reduc_code = ERROR_MARK;
32074 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
32075 unsigned int i;
32076 cp_token *first_token;
32077 cp_token_cache *cp;
32078 int errs;
32079 void *p;
32081 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
32082 p = obstack_alloc (&declarator_obstack, 0);
32084 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32085 goto fail;
32087 switch (cp_lexer_peek_token (parser->lexer)->type)
32089 case CPP_PLUS:
32090 reduc_code = PLUS_EXPR;
32091 break;
32092 case CPP_MULT:
32093 reduc_code = MULT_EXPR;
32094 break;
32095 case CPP_MINUS:
32096 reduc_code = MINUS_EXPR;
32097 break;
32098 case CPP_AND:
32099 reduc_code = BIT_AND_EXPR;
32100 break;
32101 case CPP_XOR:
32102 reduc_code = BIT_XOR_EXPR;
32103 break;
32104 case CPP_OR:
32105 reduc_code = BIT_IOR_EXPR;
32106 break;
32107 case CPP_AND_AND:
32108 reduc_code = TRUTH_ANDIF_EXPR;
32109 break;
32110 case CPP_OR_OR:
32111 reduc_code = TRUTH_ORIF_EXPR;
32112 break;
32113 case CPP_NAME:
32114 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
32115 break;
32116 default:
32117 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32118 "%<|%>, %<&&%>, %<||%> or identifier");
32119 goto fail;
32122 if (reduc_code != ERROR_MARK)
32123 cp_lexer_consume_token (parser->lexer);
32125 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
32126 if (reduc_id == error_mark_node)
32127 goto fail;
32129 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32130 goto fail;
32132 /* Types may not be defined in declare reduction type list. */
32133 const char *saved_message;
32134 saved_message = parser->type_definition_forbidden_message;
32135 parser->type_definition_forbidden_message
32136 = G_("types may not be defined in declare reduction type list");
32137 bool saved_colon_corrects_to_scope_p;
32138 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32139 parser->colon_corrects_to_scope_p = false;
32140 bool saved_colon_doesnt_start_class_def_p;
32141 saved_colon_doesnt_start_class_def_p
32142 = parser->colon_doesnt_start_class_def_p;
32143 parser->colon_doesnt_start_class_def_p = true;
32145 while (true)
32147 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32148 type = cp_parser_type_id (parser);
32149 if (type == error_mark_node)
32151 else if (ARITHMETIC_TYPE_P (type)
32152 && (orig_reduc_id == NULL_TREE
32153 || (TREE_CODE (type) != COMPLEX_TYPE
32154 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32155 "min") == 0
32156 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32157 "max") == 0))))
32158 error_at (loc, "predeclared arithmetic type %qT in "
32159 "%<#pragma omp declare reduction%>", type);
32160 else if (TREE_CODE (type) == FUNCTION_TYPE
32161 || TREE_CODE (type) == METHOD_TYPE
32162 || TREE_CODE (type) == ARRAY_TYPE)
32163 error_at (loc, "function or array type %qT in "
32164 "%<#pragma omp declare reduction%>", type);
32165 else if (TREE_CODE (type) == REFERENCE_TYPE)
32166 error_at (loc, "reference type %qT in "
32167 "%<#pragma omp declare reduction%>", type);
32168 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
32169 error_at (loc, "const, volatile or __restrict qualified type %qT in "
32170 "%<#pragma omp declare reduction%>", type);
32171 else
32172 types.safe_push (type);
32174 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32175 cp_lexer_consume_token (parser->lexer);
32176 else
32177 break;
32180 /* Restore the saved message. */
32181 parser->type_definition_forbidden_message = saved_message;
32182 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32183 parser->colon_doesnt_start_class_def_p
32184 = saved_colon_doesnt_start_class_def_p;
32186 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
32187 || types.is_empty ())
32189 fail:
32190 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32191 goto done;
32194 first_token = cp_lexer_peek_token (parser->lexer);
32195 cp = NULL;
32196 errs = errorcount;
32197 FOR_EACH_VEC_ELT (types, i, type)
32199 tree fntype
32200 = build_function_type_list (void_type_node,
32201 cp_build_reference_type (type, false),
32202 NULL_TREE);
32203 tree this_reduc_id = reduc_id;
32204 if (!dependent_type_p (type))
32205 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
32206 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
32207 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
32208 DECL_ARTIFICIAL (fndecl) = 1;
32209 DECL_EXTERNAL (fndecl) = 1;
32210 DECL_DECLARED_INLINE_P (fndecl) = 1;
32211 DECL_IGNORED_P (fndecl) = 1;
32212 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
32213 DECL_ATTRIBUTES (fndecl)
32214 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
32215 DECL_ATTRIBUTES (fndecl));
32216 if (processing_template_decl)
32217 fndecl = push_template_decl (fndecl);
32218 bool block_scope = false;
32219 tree block = NULL_TREE;
32220 if (current_function_decl)
32222 block_scope = true;
32223 DECL_CONTEXT (fndecl) = global_namespace;
32224 if (!processing_template_decl)
32225 pushdecl (fndecl);
32227 else if (current_class_type)
32229 if (cp == NULL)
32231 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32232 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32233 cp_lexer_consume_token (parser->lexer);
32234 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32235 goto fail;
32236 cp = cp_token_cache_new (first_token,
32237 cp_lexer_peek_nth_token (parser->lexer,
32238 2));
32240 DECL_STATIC_FUNCTION_P (fndecl) = 1;
32241 finish_member_declaration (fndecl);
32242 DECL_PENDING_INLINE_INFO (fndecl) = cp;
32243 DECL_PENDING_INLINE_P (fndecl) = 1;
32244 vec_safe_push (unparsed_funs_with_definitions, fndecl);
32245 continue;
32247 else
32249 DECL_CONTEXT (fndecl) = current_namespace;
32250 pushdecl (fndecl);
32252 if (!block_scope)
32253 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
32254 else
32255 block = begin_omp_structured_block ();
32256 if (cp)
32258 cp_parser_push_lexer_for_tokens (parser, cp);
32259 parser->lexer->in_pragma = true;
32261 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
32263 if (!block_scope)
32264 finish_function (0);
32265 else
32266 DECL_CONTEXT (fndecl) = current_function_decl;
32267 if (cp)
32268 cp_parser_pop_lexer (parser);
32269 goto fail;
32271 if (cp)
32272 cp_parser_pop_lexer (parser);
32273 if (!block_scope)
32274 finish_function (0);
32275 else
32277 DECL_CONTEXT (fndecl) = current_function_decl;
32278 block = finish_omp_structured_block (block);
32279 if (TREE_CODE (block) == BIND_EXPR)
32280 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
32281 else if (TREE_CODE (block) == STATEMENT_LIST)
32282 DECL_SAVED_TREE (fndecl) = block;
32283 if (processing_template_decl)
32284 add_decl_expr (fndecl);
32286 cp_check_omp_declare_reduction (fndecl);
32287 if (cp == NULL && types.length () > 1)
32288 cp = cp_token_cache_new (first_token,
32289 cp_lexer_peek_nth_token (parser->lexer, 2));
32290 if (errs != errorcount)
32291 break;
32294 cp_parser_require_pragma_eol (parser, pragma_tok);
32296 done:
32297 /* Free any declarators allocated. */
32298 obstack_free (&declarator_obstack, p);
32301 /* OpenMP 4.0
32302 #pragma omp declare simd declare-simd-clauses[optseq] new-line
32303 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32304 initializer-clause[opt] new-line
32305 #pragma omp declare target new-line */
32307 static void
32308 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
32309 enum pragma_context context)
32311 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32313 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32314 const char *p = IDENTIFIER_POINTER (id);
32316 if (strcmp (p, "simd") == 0)
32318 cp_lexer_consume_token (parser->lexer);
32319 cp_parser_omp_declare_simd (parser, pragma_tok,
32320 context);
32321 return;
32323 cp_ensure_no_omp_declare_simd (parser);
32324 if (strcmp (p, "reduction") == 0)
32326 cp_lexer_consume_token (parser->lexer);
32327 cp_parser_omp_declare_reduction (parser, pragma_tok,
32328 context);
32329 return;
32331 if (!flag_openmp) /* flag_openmp_simd */
32333 cp_parser_require_pragma_eol (parser, pragma_tok);
32334 return;
32336 if (strcmp (p, "target") == 0)
32338 cp_lexer_consume_token (parser->lexer);
32339 cp_parser_omp_declare_target (parser, pragma_tok);
32340 return;
32343 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
32344 "or %<target%>");
32345 cp_parser_require_pragma_eol (parser, pragma_tok);
32348 /* Main entry point to OpenMP statement pragmas. */
32350 static void
32351 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
32353 tree stmt;
32354 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
32355 omp_clause_mask mask (0);
32357 switch (pragma_tok->pragma_kind)
32359 case PRAGMA_OACC_CACHE:
32360 stmt = cp_parser_oacc_cache (parser, pragma_tok);
32361 break;
32362 case PRAGMA_OACC_DATA:
32363 stmt = cp_parser_oacc_data (parser, pragma_tok);
32364 break;
32365 case PRAGMA_OACC_ENTER_DATA:
32366 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
32367 break;
32368 case PRAGMA_OACC_EXIT_DATA:
32369 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
32370 break;
32371 case PRAGMA_OACC_KERNELS:
32372 stmt = cp_parser_oacc_kernels (parser, pragma_tok);
32373 break;
32374 case PRAGMA_OACC_LOOP:
32375 stmt = cp_parser_oacc_loop (parser, pragma_tok);
32376 break;
32377 case PRAGMA_OACC_PARALLEL:
32378 stmt = cp_parser_oacc_parallel (parser, pragma_tok);
32379 break;
32380 case PRAGMA_OACC_UPDATE:
32381 stmt = cp_parser_oacc_update (parser, pragma_tok);
32382 break;
32383 case PRAGMA_OACC_WAIT:
32384 stmt = cp_parser_oacc_wait (parser, pragma_tok);
32385 break;
32386 case PRAGMA_OMP_ATOMIC:
32387 cp_parser_omp_atomic (parser, pragma_tok);
32388 return;
32389 case PRAGMA_OMP_CRITICAL:
32390 stmt = cp_parser_omp_critical (parser, pragma_tok);
32391 break;
32392 case PRAGMA_OMP_DISTRIBUTE:
32393 strcpy (p_name, "#pragma omp");
32394 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
32395 break;
32396 case PRAGMA_OMP_FOR:
32397 strcpy (p_name, "#pragma omp");
32398 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
32399 break;
32400 case PRAGMA_OMP_MASTER:
32401 stmt = cp_parser_omp_master (parser, pragma_tok);
32402 break;
32403 case PRAGMA_OMP_ORDERED:
32404 stmt = cp_parser_omp_ordered (parser, pragma_tok);
32405 break;
32406 case PRAGMA_OMP_PARALLEL:
32407 strcpy (p_name, "#pragma omp");
32408 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
32409 break;
32410 case PRAGMA_OMP_SECTIONS:
32411 strcpy (p_name, "#pragma omp");
32412 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
32413 break;
32414 case PRAGMA_OMP_SIMD:
32415 strcpy (p_name, "#pragma omp");
32416 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
32417 break;
32418 case PRAGMA_OMP_SINGLE:
32419 stmt = cp_parser_omp_single (parser, pragma_tok);
32420 break;
32421 case PRAGMA_OMP_TASK:
32422 stmt = cp_parser_omp_task (parser, pragma_tok);
32423 break;
32424 case PRAGMA_OMP_TASKGROUP:
32425 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
32426 break;
32427 case PRAGMA_OMP_TEAMS:
32428 strcpy (p_name, "#pragma omp");
32429 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
32430 break;
32431 default:
32432 gcc_unreachable ();
32435 if (stmt)
32436 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32439 /* Transactional Memory parsing routines. */
32441 /* Parse a transaction attribute.
32443 txn-attribute:
32444 attribute
32445 [ [ identifier ] ]
32447 ??? Simplify this when C++0x bracket attributes are
32448 implemented properly. */
32450 static tree
32451 cp_parser_txn_attribute_opt (cp_parser *parser)
32453 cp_token *token;
32454 tree attr_name, attr = NULL;
32456 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32457 return cp_parser_attributes_opt (parser);
32459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
32460 return NULL_TREE;
32461 cp_lexer_consume_token (parser->lexer);
32462 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
32463 goto error1;
32465 token = cp_lexer_peek_token (parser->lexer);
32466 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
32468 token = cp_lexer_consume_token (parser->lexer);
32470 attr_name = (token->type == CPP_KEYWORD
32471 /* For keywords, use the canonical spelling,
32472 not the parsed identifier. */
32473 ? ridpointers[(int) token->keyword]
32474 : token->u.value);
32475 attr = build_tree_list (attr_name, NULL_TREE);
32477 else
32478 cp_parser_error (parser, "expected identifier");
32480 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32481 error1:
32482 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32483 return attr;
32486 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32488 transaction-statement:
32489 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32490 compound-statement
32491 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32494 static tree
32495 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32497 unsigned char old_in = parser->in_transaction;
32498 unsigned char this_in = 1, new_in;
32499 cp_token *token;
32500 tree stmt, attrs, noex;
32502 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32503 || keyword == RID_TRANSACTION_RELAXED);
32504 token = cp_parser_require_keyword (parser, keyword,
32505 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32506 : RT_TRANSACTION_RELAXED));
32507 gcc_assert (token != NULL);
32509 if (keyword == RID_TRANSACTION_RELAXED)
32510 this_in |= TM_STMT_ATTR_RELAXED;
32511 else
32513 attrs = cp_parser_txn_attribute_opt (parser);
32514 if (attrs)
32515 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32518 /* Parse a noexcept specification. */
32519 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32521 /* Keep track if we're in the lexical scope of an outer transaction. */
32522 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32524 stmt = begin_transaction_stmt (token->location, NULL, this_in);
32526 parser->in_transaction = new_in;
32527 cp_parser_compound_statement (parser, NULL, false, false);
32528 parser->in_transaction = old_in;
32530 finish_transaction_stmt (stmt, NULL, this_in, noex);
32532 return stmt;
32535 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32537 transaction-expression:
32538 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32539 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32542 static tree
32543 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32545 unsigned char old_in = parser->in_transaction;
32546 unsigned char this_in = 1;
32547 cp_token *token;
32548 tree expr, noex;
32549 bool noex_expr;
32551 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32552 || keyword == RID_TRANSACTION_RELAXED);
32554 if (!flag_tm)
32555 error (keyword == RID_TRANSACTION_RELAXED
32556 ? G_("%<__transaction_relaxed%> without transactional memory "
32557 "support enabled")
32558 : G_("%<__transaction_atomic%> without transactional memory "
32559 "support enabled"));
32561 token = cp_parser_require_keyword (parser, keyword,
32562 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32563 : RT_TRANSACTION_RELAXED));
32564 gcc_assert (token != NULL);
32566 if (keyword == RID_TRANSACTION_RELAXED)
32567 this_in |= TM_STMT_ATTR_RELAXED;
32569 /* Set this early. This might mean that we allow transaction_cancel in
32570 an expression that we find out later actually has to be a constexpr.
32571 However, we expect that cxx_constant_value will be able to deal with
32572 this; also, if the noexcept has no constexpr, then what we parse next
32573 really is a transaction's body. */
32574 parser->in_transaction = this_in;
32576 /* Parse a noexcept specification. */
32577 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32578 true);
32580 if (!noex || !noex_expr
32581 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32583 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32585 expr = cp_parser_expression (parser);
32586 expr = finish_parenthesized_expr (expr);
32588 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32590 else
32592 /* The only expression that is available got parsed for the noexcept
32593 already. noexcept is true then. */
32594 expr = noex;
32595 noex = boolean_true_node;
32598 expr = build_transaction_expr (token->location, expr, this_in, noex);
32599 parser->in_transaction = old_in;
32601 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32602 return error_mark_node;
32604 return (flag_tm ? expr : error_mark_node);
32607 /* Parse a function-transaction-block.
32609 function-transaction-block:
32610 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32611 function-body
32612 __transaction_atomic txn-attribute[opt] function-try-block
32613 __transaction_relaxed ctor-initializer[opt] function-body
32614 __transaction_relaxed function-try-block
32617 static bool
32618 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32620 unsigned char old_in = parser->in_transaction;
32621 unsigned char new_in = 1;
32622 tree compound_stmt, stmt, attrs;
32623 bool ctor_initializer_p;
32624 cp_token *token;
32626 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32627 || keyword == RID_TRANSACTION_RELAXED);
32628 token = cp_parser_require_keyword (parser, keyword,
32629 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32630 : RT_TRANSACTION_RELAXED));
32631 gcc_assert (token != NULL);
32633 if (keyword == RID_TRANSACTION_RELAXED)
32634 new_in |= TM_STMT_ATTR_RELAXED;
32635 else
32637 attrs = cp_parser_txn_attribute_opt (parser);
32638 if (attrs)
32639 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32642 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32644 parser->in_transaction = new_in;
32646 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32647 ctor_initializer_p = cp_parser_function_try_block (parser);
32648 else
32649 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32650 (parser, /*in_function_try_block=*/false);
32652 parser->in_transaction = old_in;
32654 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32656 return ctor_initializer_p;
32659 /* Parse a __transaction_cancel statement.
32661 cancel-statement:
32662 __transaction_cancel txn-attribute[opt] ;
32663 __transaction_cancel txn-attribute[opt] throw-expression ;
32665 ??? Cancel and throw is not yet implemented. */
32667 static tree
32668 cp_parser_transaction_cancel (cp_parser *parser)
32670 cp_token *token;
32671 bool is_outer = false;
32672 tree stmt, attrs;
32674 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32675 RT_TRANSACTION_CANCEL);
32676 gcc_assert (token != NULL);
32678 attrs = cp_parser_txn_attribute_opt (parser);
32679 if (attrs)
32680 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32682 /* ??? Parse cancel-and-throw here. */
32684 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32686 if (!flag_tm)
32688 error_at (token->location, "%<__transaction_cancel%> without "
32689 "transactional memory support enabled");
32690 return error_mark_node;
32692 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32694 error_at (token->location, "%<__transaction_cancel%> within a "
32695 "%<__transaction_relaxed%>");
32696 return error_mark_node;
32698 else if (is_outer)
32700 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32701 && !is_tm_may_cancel_outer (current_function_decl))
32703 error_at (token->location, "outer %<__transaction_cancel%> not "
32704 "within outer %<__transaction_atomic%>");
32705 error_at (token->location,
32706 " or a %<transaction_may_cancel_outer%> function");
32707 return error_mark_node;
32710 else if (parser->in_transaction == 0)
32712 error_at (token->location, "%<__transaction_cancel%> not within "
32713 "%<__transaction_atomic%>");
32714 return error_mark_node;
32717 stmt = build_tm_abort_call (token->location, is_outer);
32718 add_stmt (stmt);
32720 return stmt;
32723 /* The parser. */
32725 static GTY (()) cp_parser *the_parser;
32728 /* Special handling for the first token or line in the file. The first
32729 thing in the file might be #pragma GCC pch_preprocess, which loads a
32730 PCH file, which is a GC collection point. So we need to handle this
32731 first pragma without benefit of an existing lexer structure.
32733 Always returns one token to the caller in *FIRST_TOKEN. This is
32734 either the true first token of the file, or the first token after
32735 the initial pragma. */
32737 static void
32738 cp_parser_initial_pragma (cp_token *first_token)
32740 tree name = NULL;
32742 cp_lexer_get_preprocessor_token (NULL, first_token);
32743 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32744 return;
32746 cp_lexer_get_preprocessor_token (NULL, first_token);
32747 if (first_token->type == CPP_STRING)
32749 name = first_token->u.value;
32751 cp_lexer_get_preprocessor_token (NULL, first_token);
32752 if (first_token->type != CPP_PRAGMA_EOL)
32753 error_at (first_token->location,
32754 "junk at end of %<#pragma GCC pch_preprocess%>");
32756 else
32757 error_at (first_token->location, "expected string literal");
32759 /* Skip to the end of the pragma. */
32760 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32761 cp_lexer_get_preprocessor_token (NULL, first_token);
32763 /* Now actually load the PCH file. */
32764 if (name)
32765 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32767 /* Read one more token to return to our caller. We have to do this
32768 after reading the PCH file in, since its pointers have to be
32769 live. */
32770 cp_lexer_get_preprocessor_token (NULL, first_token);
32773 /* Parses the grainsize pragma for the _Cilk_for statement.
32774 Syntax:
32775 #pragma cilk grainsize = <VALUE>. */
32777 static void
32778 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
32780 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
32782 tree exp = cp_parser_binary_expression (parser, false, false,
32783 PREC_NOT_OPERATOR, NULL);
32784 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32785 if (!exp || exp == error_mark_node)
32787 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
32788 return;
32791 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
32792 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32793 cp_parser_cilk_for (parser, exp);
32794 else
32795 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
32796 "%<#pragma cilk grainsize%> is not followed by "
32797 "%<_Cilk_for%>");
32798 return;
32800 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32803 /* Normal parsing of a pragma token. Here we can (and must) use the
32804 regular lexer. */
32806 static bool
32807 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32809 cp_token *pragma_tok;
32810 unsigned int id;
32812 pragma_tok = cp_lexer_consume_token (parser->lexer);
32813 gcc_assert (pragma_tok->type == CPP_PRAGMA);
32814 parser->lexer->in_pragma = true;
32816 id = pragma_tok->pragma_kind;
32817 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
32818 cp_ensure_no_omp_declare_simd (parser);
32819 switch (id)
32821 case PRAGMA_GCC_PCH_PREPROCESS:
32822 error_at (pragma_tok->location,
32823 "%<#pragma GCC pch_preprocess%> must be first");
32824 break;
32826 case PRAGMA_OMP_BARRIER:
32827 switch (context)
32829 case pragma_compound:
32830 cp_parser_omp_barrier (parser, pragma_tok);
32831 return false;
32832 case pragma_stmt:
32833 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32834 "used in compound statements");
32835 break;
32836 default:
32837 goto bad_stmt;
32839 break;
32841 case PRAGMA_OMP_FLUSH:
32842 switch (context)
32844 case pragma_compound:
32845 cp_parser_omp_flush (parser, pragma_tok);
32846 return false;
32847 case pragma_stmt:
32848 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32849 "used in compound statements");
32850 break;
32851 default:
32852 goto bad_stmt;
32854 break;
32856 case PRAGMA_OMP_TASKWAIT:
32857 switch (context)
32859 case pragma_compound:
32860 cp_parser_omp_taskwait (parser, pragma_tok);
32861 return false;
32862 case pragma_stmt:
32863 error_at (pragma_tok->location,
32864 "%<#pragma omp taskwait%> may only be "
32865 "used in compound statements");
32866 break;
32867 default:
32868 goto bad_stmt;
32870 break;
32872 case PRAGMA_OMP_TASKYIELD:
32873 switch (context)
32875 case pragma_compound:
32876 cp_parser_omp_taskyield (parser, pragma_tok);
32877 return false;
32878 case pragma_stmt:
32879 error_at (pragma_tok->location,
32880 "%<#pragma omp taskyield%> may only be "
32881 "used in compound statements");
32882 break;
32883 default:
32884 goto bad_stmt;
32886 break;
32888 case PRAGMA_OMP_CANCEL:
32889 switch (context)
32891 case pragma_compound:
32892 cp_parser_omp_cancel (parser, pragma_tok);
32893 return false;
32894 case pragma_stmt:
32895 error_at (pragma_tok->location,
32896 "%<#pragma omp cancel%> may only be "
32897 "used in compound statements");
32898 break;
32899 default:
32900 goto bad_stmt;
32902 break;
32904 case PRAGMA_OMP_CANCELLATION_POINT:
32905 switch (context)
32907 case pragma_compound:
32908 cp_parser_omp_cancellation_point (parser, pragma_tok);
32909 return false;
32910 case pragma_stmt:
32911 error_at (pragma_tok->location,
32912 "%<#pragma omp cancellation point%> may only be "
32913 "used in compound statements");
32914 break;
32915 default:
32916 goto bad_stmt;
32918 break;
32920 case PRAGMA_OMP_THREADPRIVATE:
32921 cp_parser_omp_threadprivate (parser, pragma_tok);
32922 return false;
32924 case PRAGMA_OMP_DECLARE_REDUCTION:
32925 cp_parser_omp_declare (parser, pragma_tok, context);
32926 return false;
32928 case PRAGMA_OACC_CACHE:
32929 case PRAGMA_OACC_DATA:
32930 case PRAGMA_OACC_ENTER_DATA:
32931 case PRAGMA_OACC_EXIT_DATA:
32932 case PRAGMA_OACC_KERNELS:
32933 case PRAGMA_OACC_PARALLEL:
32934 case PRAGMA_OACC_LOOP:
32935 case PRAGMA_OACC_UPDATE:
32936 case PRAGMA_OACC_WAIT:
32937 case PRAGMA_OMP_ATOMIC:
32938 case PRAGMA_OMP_CRITICAL:
32939 case PRAGMA_OMP_DISTRIBUTE:
32940 case PRAGMA_OMP_FOR:
32941 case PRAGMA_OMP_MASTER:
32942 case PRAGMA_OMP_ORDERED:
32943 case PRAGMA_OMP_PARALLEL:
32944 case PRAGMA_OMP_SECTIONS:
32945 case PRAGMA_OMP_SIMD:
32946 case PRAGMA_OMP_SINGLE:
32947 case PRAGMA_OMP_TASK:
32948 case PRAGMA_OMP_TASKGROUP:
32949 case PRAGMA_OMP_TEAMS:
32950 if (context != pragma_stmt && context != pragma_compound)
32951 goto bad_stmt;
32952 cp_parser_omp_construct (parser, pragma_tok);
32953 return true;
32955 case PRAGMA_OMP_TARGET:
32956 return cp_parser_omp_target (parser, pragma_tok, context);
32958 case PRAGMA_OMP_END_DECLARE_TARGET:
32959 cp_parser_omp_end_declare_target (parser, pragma_tok);
32960 return false;
32962 case PRAGMA_OMP_SECTION:
32963 error_at (pragma_tok->location,
32964 "%<#pragma omp section%> may only be used in "
32965 "%<#pragma omp sections%> construct");
32966 break;
32968 case PRAGMA_IVDEP:
32970 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32971 cp_token *tok;
32972 tok = cp_lexer_peek_token (the_parser->lexer);
32973 if (tok->type != CPP_KEYWORD
32974 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
32975 && tok->keyword != RID_DO))
32977 cp_parser_error (parser, "for, while or do statement expected");
32978 return false;
32980 cp_parser_iteration_statement (parser, true);
32981 return true;
32984 case PRAGMA_CILK_SIMD:
32985 if (context == pragma_external)
32987 error_at (pragma_tok->location,
32988 "%<#pragma simd%> must be inside a function");
32989 break;
32991 cp_parser_cilk_simd (parser, pragma_tok);
32992 return true;
32994 case PRAGMA_CILK_GRAINSIZE:
32995 if (context == pragma_external)
32997 error_at (pragma_tok->location,
32998 "%<#pragma cilk grainsize%> must be inside a function");
32999 break;
33002 /* Ignore the pragma if Cilk Plus is not enabled. */
33003 if (flag_cilkplus)
33005 cp_parser_cilk_grainsize (parser, pragma_tok);
33006 return true;
33008 else
33010 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
33011 "%<#pragma cilk grainsize%>");
33012 break;
33015 default:
33016 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
33017 c_invoke_pragma_handler (id);
33018 break;
33020 bad_stmt:
33021 cp_parser_error (parser, "expected declaration specifiers");
33022 break;
33025 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33026 return false;
33029 /* The interface the pragma parsers have to the lexer. */
33031 enum cpp_ttype
33032 pragma_lex (tree *value)
33034 cp_token *tok;
33035 enum cpp_ttype ret;
33037 tok = cp_lexer_peek_token (the_parser->lexer);
33039 ret = tok->type;
33040 *value = tok->u.value;
33042 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
33043 ret = CPP_EOF;
33044 else if (ret == CPP_STRING)
33045 *value = cp_parser_string_literal (the_parser, false, false);
33046 else
33048 cp_lexer_consume_token (the_parser->lexer);
33049 if (ret == CPP_KEYWORD)
33050 ret = CPP_NAME;
33053 return ret;
33057 /* External interface. */
33059 /* Parse one entire translation unit. */
33061 void
33062 c_parse_file (void)
33064 static bool already_called = false;
33066 if (already_called)
33067 fatal_error ("inter-module optimizations not implemented for C++");
33068 already_called = true;
33070 the_parser = cp_parser_new ();
33071 push_deferring_access_checks (flag_access_control
33072 ? dk_no_deferred : dk_no_check);
33073 cp_parser_translation_unit (the_parser);
33074 the_parser = NULL;
33077 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
33078 vectorlength clause:
33079 Syntax:
33080 vectorlength ( constant-expression ) */
33082 static tree
33083 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
33084 bool is_simd_fn)
33086 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33087 tree expr;
33088 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33089 safelen clause. Thus, vectorlength is represented as OMP 4.0
33090 safelen. For SIMD-enabled function it is represented by OMP 4.0
33091 simdlen. */
33092 if (!is_simd_fn)
33093 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
33094 loc);
33095 else
33096 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
33097 loc);
33099 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33100 return error_mark_node;
33102 expr = cp_parser_constant_expression (parser);
33103 expr = maybe_constant_value (expr);
33105 /* If expr == error_mark_node, then don't emit any errors nor
33106 create a clause. if any of the above functions returns
33107 error mark node then they would have emitted an error message. */
33108 if (expr == error_mark_node)
33110 else if (!TREE_TYPE (expr)
33111 || !TREE_CONSTANT (expr)
33112 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
33113 error_at (loc, "vectorlength must be an integer constant");
33114 else if (TREE_CONSTANT (expr)
33115 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
33116 error_at (loc, "vectorlength must be a power of 2");
33117 else
33119 tree c;
33120 if (!is_simd_fn)
33122 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
33123 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
33124 OMP_CLAUSE_CHAIN (c) = clauses;
33125 clauses = c;
33127 else
33129 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
33130 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
33131 OMP_CLAUSE_CHAIN (c) = clauses;
33132 clauses = c;
33136 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33137 return error_mark_node;
33138 return clauses;
33141 /* Handles the Cilk Plus #pragma simd linear clause.
33142 Syntax:
33143 linear ( simd-linear-variable-list )
33145 simd-linear-variable-list:
33146 simd-linear-variable
33147 simd-linear-variable-list , simd-linear-variable
33149 simd-linear-variable:
33150 id-expression
33151 id-expression : simd-linear-step
33153 simd-linear-step:
33154 conditional-expression */
33156 static tree
33157 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
33159 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33161 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33162 return clauses;
33163 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33165 cp_parser_error (parser, "expected identifier");
33166 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33167 return error_mark_node;
33170 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33171 parser->colon_corrects_to_scope_p = false;
33172 while (1)
33174 cp_token *token = cp_lexer_peek_token (parser->lexer);
33175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33177 cp_parser_error (parser, "expected variable-name");
33178 clauses = error_mark_node;
33179 break;
33182 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
33183 false, false);
33184 tree decl = cp_parser_lookup_name_simple (parser, var_name,
33185 token->location);
33186 if (decl == error_mark_node)
33188 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
33189 token->location);
33190 clauses = error_mark_node;
33192 else
33194 tree e = NULL_TREE;
33195 tree step_size = integer_one_node;
33197 /* If present, parse the linear step. Otherwise, assume the default
33198 value of 1. */
33199 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
33201 cp_lexer_consume_token (parser->lexer);
33203 e = cp_parser_assignment_expression (parser);
33204 e = maybe_constant_value (e);
33206 if (e == error_mark_node)
33208 /* If an error has occurred, then the whole pragma is
33209 considered ill-formed. Thus, no reason to keep
33210 parsing. */
33211 clauses = error_mark_node;
33212 break;
33214 else if (type_dependent_expression_p (e)
33215 || value_dependent_expression_p (e)
33216 || (TREE_TYPE (e)
33217 && INTEGRAL_TYPE_P (TREE_TYPE (e))
33218 && (TREE_CONSTANT (e)
33219 || DECL_P (e))))
33220 step_size = e;
33221 else
33222 cp_parser_error (parser,
33223 "step size must be an integer constant "
33224 "expression or an integer variable");
33227 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
33228 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33229 OMP_CLAUSE_DECL (l) = decl;
33230 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
33231 OMP_CLAUSE_CHAIN (l) = clauses;
33232 clauses = l;
33234 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33235 cp_lexer_consume_token (parser->lexer);
33236 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33237 break;
33238 else
33240 error_at (cp_lexer_peek_token (parser->lexer)->location,
33241 "expected %<,%> or %<)%> after %qE", decl);
33242 clauses = error_mark_node;
33243 break;
33246 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33247 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33248 return clauses;
33251 /* Returns the name of the next clause. If the clause is not
33252 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33253 token is not consumed. Otherwise, the appropriate enum from the
33254 pragma_simd_clause is returned and the token is consumed. */
33256 static pragma_omp_clause
33257 cp_parser_cilk_simd_clause_name (cp_parser *parser)
33259 pragma_omp_clause clause_type;
33260 cp_token *token = cp_lexer_peek_token (parser->lexer);
33262 if (token->keyword == RID_PRIVATE)
33263 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
33264 else if (!token->u.value || token->type != CPP_NAME)
33265 return PRAGMA_CILK_CLAUSE_NONE;
33266 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
33267 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
33268 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
33269 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
33270 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
33271 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
33272 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
33273 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
33274 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
33275 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
33276 else
33277 return PRAGMA_CILK_CLAUSE_NONE;
33279 cp_lexer_consume_token (parser->lexer);
33280 return clause_type;
33283 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
33285 static tree
33286 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
33288 tree clauses = NULL_TREE;
33290 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33291 && clauses != error_mark_node)
33293 pragma_omp_clause c_kind;
33294 c_kind = cp_parser_cilk_simd_clause_name (parser);
33295 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
33296 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
33297 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
33298 clauses = cp_parser_cilk_simd_linear (parser, clauses);
33299 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
33300 /* Use the OpenMP 4.0 equivalent function. */
33301 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
33302 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
33303 /* Use the OpenMP 4.0 equivalent function. */
33304 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33305 clauses);
33306 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
33307 /* Use the OMP 4.0 equivalent function. */
33308 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33309 clauses);
33310 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
33311 /* Use the OMP 4.0 equivalent function. */
33312 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33313 else
33315 clauses = error_mark_node;
33316 cp_parser_error (parser, "expected %<#pragma simd%> clause");
33317 break;
33321 cp_parser_skip_to_pragma_eol (parser, pragma_token);
33323 if (clauses == error_mark_node)
33324 return error_mark_node;
33325 else
33326 return c_finish_cilk_clauses (clauses);
33329 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
33331 static void
33332 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
33334 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
33336 if (clauses == error_mark_node)
33337 return;
33339 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
33341 error_at (cp_lexer_peek_token (parser->lexer)->location,
33342 "for statement expected");
33343 return;
33346 tree sb = begin_omp_structured_block ();
33347 int save = cp_parser_begin_omp_structured_block (parser);
33348 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
33349 if (ret)
33350 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
33351 cp_parser_end_omp_structured_block (parser, save);
33352 add_stmt (finish_omp_structured_block (sb));
33355 /* Main entry-point for parsing Cilk Plus _Cilk_for
33356 loops. The return value is error_mark_node
33357 when errors happen and CILK_FOR tree on success. */
33359 static tree
33360 cp_parser_cilk_for (cp_parser *parser, tree grain)
33362 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
33363 gcc_unreachable ();
33365 tree sb = begin_omp_structured_block ();
33366 int save = cp_parser_begin_omp_structured_block (parser);
33368 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
33369 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
33370 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
33371 clauses = finish_omp_clauses (clauses);
33373 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
33374 if (ret)
33375 cpp_validate_cilk_plus_loop (ret);
33376 else
33377 ret = error_mark_node;
33379 cp_parser_end_omp_structured_block (parser, save);
33380 add_stmt (finish_omp_structured_block (sb));
33381 return ret;
33384 /* Create an identifier for a generic parameter type (a synthesized
33385 template parameter implied by `auto' or a concept identifier). */
33387 static GTY(()) int generic_parm_count;
33388 static tree
33389 make_generic_type_name ()
33391 char buf[32];
33392 sprintf (buf, "auto:%d", ++generic_parm_count);
33393 return get_identifier (buf);
33396 /* Predicate that behaves as is_auto_or_concept but matches the parent
33397 node of the generic type rather than the generic type itself. This
33398 allows for type transformation in add_implicit_template_parms. */
33400 static inline bool
33401 tree_type_is_auto_or_concept (const_tree t)
33403 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
33406 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33407 (creating a new template parameter list if necessary). Returns the newly
33408 created template type parm. */
33410 tree
33411 synthesize_implicit_template_parm (cp_parser *parser)
33413 gcc_assert (current_binding_level->kind == sk_function_parms);
33415 /* We are either continuing a function template that already contains implicit
33416 template parameters, creating a new fully-implicit function template, or
33417 extending an existing explicit function template with implicit template
33418 parameters. */
33420 cp_binding_level *const entry_scope = current_binding_level;
33422 bool become_template = false;
33423 cp_binding_level *parent_scope = 0;
33425 if (parser->implicit_template_scope)
33427 gcc_assert (parser->implicit_template_parms);
33429 current_binding_level = parser->implicit_template_scope;
33431 else
33433 /* Roll back to the existing template parameter scope (in the case of
33434 extending an explicit function template) or introduce a new template
33435 parameter scope ahead of the function parameter scope (or class scope
33436 in the case of out-of-line member definitions). The function scope is
33437 added back after template parameter synthesis below. */
33439 cp_binding_level *scope = entry_scope;
33441 while (scope->kind == sk_function_parms)
33443 parent_scope = scope;
33444 scope = scope->level_chain;
33446 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
33448 /* If not defining a class, then any class scope is a scope level in
33449 an out-of-line member definition. In this case simply wind back
33450 beyond the first such scope to inject the template parameter list.
33451 Otherwise wind back to the class being defined. The latter can
33452 occur in class member friend declarations such as:
33454 class A {
33455 void foo (auto);
33457 class B {
33458 friend void A::foo (auto);
33461 The template parameter list synthesized for the friend declaration
33462 must be injected in the scope of 'B'. This can also occur in
33463 erroneous cases such as:
33465 struct A {
33466 struct B {
33467 void foo (auto);
33469 void B::foo (auto) {}
33472 Here the attempted definition of 'B::foo' within 'A' is ill-formed
33473 but, nevertheless, the template parameter list synthesized for the
33474 declarator should be injected into the scope of 'A' as if the
33475 ill-formed template was specified explicitly. */
33477 while (scope->kind == sk_class && !scope->defining_class_p)
33479 parent_scope = scope;
33480 scope = scope->level_chain;
33484 current_binding_level = scope;
33486 if (scope->kind != sk_template_parms
33487 || !function_being_declared_is_template_p (parser))
33489 /* Introduce a new template parameter list for implicit template
33490 parameters. */
33492 become_template = true;
33494 parser->implicit_template_scope
33495 = begin_scope (sk_template_parms, NULL);
33497 ++processing_template_decl;
33499 parser->fully_implicit_function_template_p = true;
33500 ++parser->num_template_parameter_lists;
33502 else
33504 /* Synthesize implicit template parameters at the end of the explicit
33505 template parameter list. */
33507 gcc_assert (current_template_parms);
33509 parser->implicit_template_scope = scope;
33511 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33512 parser->implicit_template_parms
33513 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
33517 /* Synthesize a new template parameter and track the current template
33518 parameter chain with implicit_template_parms. */
33520 tree synth_id = make_generic_type_name ();
33521 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33522 synth_id);
33523 tree new_parm
33524 = process_template_parm (parser->implicit_template_parms,
33525 input_location,
33526 build_tree_list (NULL_TREE, synth_tmpl_parm),
33527 /*non_type=*/false,
33528 /*param_pack=*/false);
33531 if (parser->implicit_template_parms)
33532 parser->implicit_template_parms
33533 = TREE_CHAIN (parser->implicit_template_parms);
33534 else
33535 parser->implicit_template_parms = new_parm;
33537 tree new_type = TREE_TYPE (getdecls ());
33539 /* If creating a fully implicit function template, start the new implicit
33540 template parameter list with this synthesized type, otherwise grow the
33541 current template parameter list. */
33543 if (become_template)
33545 parent_scope->level_chain = current_binding_level;
33547 tree new_parms = make_tree_vec (1);
33548 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33549 current_template_parms = tree_cons (size_int (processing_template_decl),
33550 new_parms, current_template_parms);
33552 else
33554 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33555 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33556 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33557 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33560 current_binding_level = entry_scope;
33562 return new_type;
33565 /* Finish the declaration of a fully implicit function template. Such a
33566 template has no explicit template parameter list so has not been through the
33567 normal template head and tail processing. synthesize_implicit_template_parm
33568 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33569 provided if the declaration is a class member such that its template
33570 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33571 form is returned. Otherwise NULL_TREE is returned. */
33573 tree
33574 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33576 gcc_assert (parser->fully_implicit_function_template_p);
33578 if (member_decl_opt && member_decl_opt != error_mark_node
33579 && DECL_VIRTUAL_P (member_decl_opt))
33581 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33582 "implicit templates may not be %<virtual%>");
33583 DECL_VIRTUAL_P (member_decl_opt) = false;
33586 if (member_decl_opt)
33587 member_decl_opt = finish_member_template_decl (member_decl_opt);
33588 end_template_decl ();
33590 parser->fully_implicit_function_template_p = false;
33591 --parser->num_template_parameter_lists;
33593 return member_decl_opt;
33596 #include "gt-cp-parser.h"