Allow Objective-c++ to recognise lambdas.
[official-gcc.git] / gcc / cp / parser.c
blob68940aab1c9e173223fc7bdd0f6ff2da181bd874
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> *,
2128 bool, bool, int, bool *, tree *, location_t *);
2129 static cp_declarator *cp_parser_declarator
2130 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2131 static cp_declarator *cp_parser_direct_declarator
2132 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2133 static enum tree_code cp_parser_ptr_operator
2134 (cp_parser *, tree *, cp_cv_quals *, tree *);
2135 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2136 (cp_parser *);
2137 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2138 (cp_parser *);
2139 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2140 (cp_parser *);
2141 static tree cp_parser_late_return_type_opt
2142 (cp_parser *, cp_declarator *, cp_cv_quals);
2143 static tree cp_parser_declarator_id
2144 (cp_parser *, bool);
2145 static tree cp_parser_type_id
2146 (cp_parser *);
2147 static tree cp_parser_template_type_arg
2148 (cp_parser *);
2149 static tree cp_parser_trailing_type_id (cp_parser *);
2150 static tree cp_parser_type_id_1
2151 (cp_parser *, bool, bool);
2152 static void cp_parser_type_specifier_seq
2153 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2154 static tree cp_parser_parameter_declaration_clause
2155 (cp_parser *);
2156 static tree cp_parser_parameter_declaration_list
2157 (cp_parser *, bool *);
2158 static cp_parameter_declarator *cp_parser_parameter_declaration
2159 (cp_parser *, bool, bool *);
2160 static tree cp_parser_default_argument
2161 (cp_parser *, bool);
2162 static void cp_parser_function_body
2163 (cp_parser *, bool);
2164 static tree cp_parser_initializer
2165 (cp_parser *, bool *, bool *);
2166 static tree cp_parser_initializer_clause
2167 (cp_parser *, bool *);
2168 static tree cp_parser_braced_list
2169 (cp_parser*, bool*);
2170 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2171 (cp_parser *, bool *);
2173 static bool cp_parser_ctor_initializer_opt_and_function_body
2174 (cp_parser *, bool);
2176 static tree cp_parser_late_parsing_omp_declare_simd
2177 (cp_parser *, tree);
2179 static tree cp_parser_late_parsing_cilk_simd_fn_info
2180 (cp_parser *, tree);
2182 static tree synthesize_implicit_template_parm
2183 (cp_parser *);
2184 static tree finish_fully_implicit_template
2185 (cp_parser *, tree);
2187 /* Classes [gram.class] */
2189 static tree cp_parser_class_name
2190 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2191 static tree cp_parser_class_specifier
2192 (cp_parser *);
2193 static tree cp_parser_class_head
2194 (cp_parser *, bool *);
2195 static enum tag_types cp_parser_class_key
2196 (cp_parser *);
2197 static void cp_parser_type_parameter_key
2198 (cp_parser* parser);
2199 static void cp_parser_member_specification_opt
2200 (cp_parser *);
2201 static void cp_parser_member_declaration
2202 (cp_parser *);
2203 static tree cp_parser_pure_specifier
2204 (cp_parser *);
2205 static tree cp_parser_constant_initializer
2206 (cp_parser *);
2208 /* Derived classes [gram.class.derived] */
2210 static tree cp_parser_base_clause
2211 (cp_parser *);
2212 static tree cp_parser_base_specifier
2213 (cp_parser *);
2215 /* Special member functions [gram.special] */
2217 static tree cp_parser_conversion_function_id
2218 (cp_parser *);
2219 static tree cp_parser_conversion_type_id
2220 (cp_parser *);
2221 static cp_declarator *cp_parser_conversion_declarator_opt
2222 (cp_parser *);
2223 static bool cp_parser_ctor_initializer_opt
2224 (cp_parser *);
2225 static void cp_parser_mem_initializer_list
2226 (cp_parser *);
2227 static tree cp_parser_mem_initializer
2228 (cp_parser *);
2229 static tree cp_parser_mem_initializer_id
2230 (cp_parser *);
2232 /* Overloading [gram.over] */
2234 static tree cp_parser_operator_function_id
2235 (cp_parser *);
2236 static tree cp_parser_operator
2237 (cp_parser *);
2239 /* Templates [gram.temp] */
2241 static void cp_parser_template_declaration
2242 (cp_parser *, bool);
2243 static tree cp_parser_template_parameter_list
2244 (cp_parser *);
2245 static tree cp_parser_template_parameter
2246 (cp_parser *, bool *, bool *);
2247 static tree cp_parser_type_parameter
2248 (cp_parser *, bool *);
2249 static tree cp_parser_template_id
2250 (cp_parser *, bool, bool, enum tag_types, bool);
2251 static tree cp_parser_template_name
2252 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2253 static tree cp_parser_template_argument_list
2254 (cp_parser *);
2255 static tree cp_parser_template_argument
2256 (cp_parser *);
2257 static void cp_parser_explicit_instantiation
2258 (cp_parser *);
2259 static void cp_parser_explicit_specialization
2260 (cp_parser *);
2262 /* Exception handling [gram.exception] */
2264 static tree cp_parser_try_block
2265 (cp_parser *);
2266 static bool cp_parser_function_try_block
2267 (cp_parser *);
2268 static void cp_parser_handler_seq
2269 (cp_parser *);
2270 static void cp_parser_handler
2271 (cp_parser *);
2272 static tree cp_parser_exception_declaration
2273 (cp_parser *);
2274 static tree cp_parser_throw_expression
2275 (cp_parser *);
2276 static tree cp_parser_exception_specification_opt
2277 (cp_parser *);
2278 static tree cp_parser_type_id_list
2279 (cp_parser *);
2281 /* GNU Extensions */
2283 static tree cp_parser_asm_specification_opt
2284 (cp_parser *);
2285 static tree cp_parser_asm_operand_list
2286 (cp_parser *);
2287 static tree cp_parser_asm_clobber_list
2288 (cp_parser *);
2289 static tree cp_parser_asm_label_list
2290 (cp_parser *);
2291 static bool cp_next_tokens_can_be_attribute_p
2292 (cp_parser *);
2293 static bool cp_next_tokens_can_be_gnu_attribute_p
2294 (cp_parser *);
2295 static bool cp_next_tokens_can_be_std_attribute_p
2296 (cp_parser *);
2297 static bool cp_nth_tokens_can_be_std_attribute_p
2298 (cp_parser *, size_t);
2299 static bool cp_nth_tokens_can_be_gnu_attribute_p
2300 (cp_parser *, size_t);
2301 static bool cp_nth_tokens_can_be_attribute_p
2302 (cp_parser *, size_t);
2303 static tree cp_parser_attributes_opt
2304 (cp_parser *);
2305 static tree cp_parser_gnu_attributes_opt
2306 (cp_parser *);
2307 static tree cp_parser_gnu_attribute_list
2308 (cp_parser *);
2309 static tree cp_parser_std_attribute
2310 (cp_parser *);
2311 static tree cp_parser_std_attribute_spec
2312 (cp_parser *);
2313 static tree cp_parser_std_attribute_spec_seq
2314 (cp_parser *);
2315 static bool cp_parser_extension_opt
2316 (cp_parser *, int *);
2317 static void cp_parser_label_declaration
2318 (cp_parser *);
2320 /* Transactional Memory Extensions */
2322 static tree cp_parser_transaction
2323 (cp_parser *, enum rid);
2324 static tree cp_parser_transaction_expression
2325 (cp_parser *, enum rid);
2326 static bool cp_parser_function_transaction
2327 (cp_parser *, enum rid);
2328 static tree cp_parser_transaction_cancel
2329 (cp_parser *);
2331 enum pragma_context {
2332 pragma_external,
2333 pragma_member,
2334 pragma_objc_icode,
2335 pragma_stmt,
2336 pragma_compound
2338 static bool cp_parser_pragma
2339 (cp_parser *, enum pragma_context);
2341 /* Objective-C++ Productions */
2343 static tree cp_parser_objc_message_receiver
2344 (cp_parser *);
2345 static tree cp_parser_objc_message_args
2346 (cp_parser *);
2347 static tree cp_parser_objc_message_expression
2348 (cp_parser *);
2349 static tree cp_parser_objc_encode_expression
2350 (cp_parser *);
2351 static tree cp_parser_objc_defs_expression
2352 (cp_parser *);
2353 static tree cp_parser_objc_protocol_expression
2354 (cp_parser *);
2355 static tree cp_parser_objc_selector_expression
2356 (cp_parser *);
2357 static tree cp_parser_objc_expression
2358 (cp_parser *);
2359 static bool cp_parser_objc_selector_p
2360 (enum cpp_ttype);
2361 static tree cp_parser_objc_selector
2362 (cp_parser *);
2363 static tree cp_parser_objc_protocol_refs_opt
2364 (cp_parser *);
2365 static void cp_parser_objc_declaration
2366 (cp_parser *, tree);
2367 static tree cp_parser_objc_statement
2368 (cp_parser *);
2369 static bool cp_parser_objc_valid_prefix_attributes
2370 (cp_parser *, tree *);
2371 static void cp_parser_objc_at_property_declaration
2372 (cp_parser *) ;
2373 static void cp_parser_objc_at_synthesize_declaration
2374 (cp_parser *) ;
2375 static void cp_parser_objc_at_dynamic_declaration
2376 (cp_parser *) ;
2377 static tree cp_parser_objc_struct_declaration
2378 (cp_parser *) ;
2380 /* Utility Routines */
2382 static tree cp_parser_lookup_name
2383 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2384 static tree cp_parser_lookup_name_simple
2385 (cp_parser *, tree, location_t);
2386 static tree cp_parser_maybe_treat_template_as_class
2387 (tree, bool);
2388 static bool cp_parser_check_declarator_template_parameters
2389 (cp_parser *, cp_declarator *, location_t);
2390 static bool cp_parser_check_template_parameters
2391 (cp_parser *, unsigned, location_t, cp_declarator *);
2392 static tree cp_parser_simple_cast_expression
2393 (cp_parser *);
2394 static tree cp_parser_global_scope_opt
2395 (cp_parser *, bool);
2396 static bool cp_parser_constructor_declarator_p
2397 (cp_parser *, bool);
2398 static tree cp_parser_function_definition_from_specifiers_and_declarator
2399 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2400 static tree cp_parser_function_definition_after_declarator
2401 (cp_parser *, bool);
2402 static void cp_parser_template_declaration_after_export
2403 (cp_parser *, bool);
2404 static void cp_parser_perform_template_parameter_access_checks
2405 (vec<deferred_access_check, va_gc> *);
2406 static tree cp_parser_single_declaration
2407 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2408 static tree cp_parser_functional_cast
2409 (cp_parser *, tree);
2410 static tree cp_parser_save_member_function_body
2411 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2412 static tree cp_parser_save_nsdmi
2413 (cp_parser *);
2414 static tree cp_parser_enclosed_template_argument_list
2415 (cp_parser *);
2416 static void cp_parser_save_default_args
2417 (cp_parser *, tree);
2418 static void cp_parser_late_parsing_for_member
2419 (cp_parser *, tree);
2420 static tree cp_parser_late_parse_one_default_arg
2421 (cp_parser *, tree, tree, tree);
2422 static void cp_parser_late_parsing_nsdmi
2423 (cp_parser *, tree);
2424 static void cp_parser_late_parsing_default_args
2425 (cp_parser *, tree);
2426 static tree cp_parser_sizeof_operand
2427 (cp_parser *, enum rid);
2428 static tree cp_parser_trait_expr
2429 (cp_parser *, enum rid);
2430 static bool cp_parser_declares_only_class_p
2431 (cp_parser *);
2432 static void cp_parser_set_storage_class
2433 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2434 static void cp_parser_set_decl_spec_type
2435 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2436 static void set_and_check_decl_spec_loc
2437 (cp_decl_specifier_seq *decl_specs,
2438 cp_decl_spec ds, cp_token *);
2439 static bool cp_parser_friend_p
2440 (const cp_decl_specifier_seq *);
2441 static void cp_parser_required_error
2442 (cp_parser *, required_token, bool);
2443 static cp_token *cp_parser_require
2444 (cp_parser *, enum cpp_ttype, required_token);
2445 static cp_token *cp_parser_require_keyword
2446 (cp_parser *, enum rid, required_token);
2447 static bool cp_parser_token_starts_function_definition_p
2448 (cp_token *);
2449 static bool cp_parser_next_token_starts_class_definition_p
2450 (cp_parser *);
2451 static bool cp_parser_next_token_ends_template_argument_p
2452 (cp_parser *);
2453 static bool cp_parser_nth_token_starts_template_argument_list_p
2454 (cp_parser *, size_t);
2455 static enum tag_types cp_parser_token_is_class_key
2456 (cp_token *);
2457 static enum tag_types cp_parser_token_is_type_parameter_key
2458 (cp_token *);
2459 static void cp_parser_check_class_key
2460 (enum tag_types, tree type);
2461 static void cp_parser_check_access_in_redeclaration
2462 (tree type, location_t location);
2463 static bool cp_parser_optional_template_keyword
2464 (cp_parser *);
2465 static void cp_parser_pre_parsed_nested_name_specifier
2466 (cp_parser *);
2467 static bool cp_parser_cache_group
2468 (cp_parser *, enum cpp_ttype, unsigned);
2469 static tree cp_parser_cache_defarg
2470 (cp_parser *parser, bool nsdmi);
2471 static void cp_parser_parse_tentatively
2472 (cp_parser *);
2473 static void cp_parser_commit_to_tentative_parse
2474 (cp_parser *);
2475 static void cp_parser_commit_to_topmost_tentative_parse
2476 (cp_parser *);
2477 static void cp_parser_abort_tentative_parse
2478 (cp_parser *);
2479 static bool cp_parser_parse_definitely
2480 (cp_parser *);
2481 static inline bool cp_parser_parsing_tentatively
2482 (cp_parser *);
2483 static bool cp_parser_uncommitted_to_tentative_parse_p
2484 (cp_parser *);
2485 static void cp_parser_error
2486 (cp_parser *, const char *);
2487 static void cp_parser_name_lookup_error
2488 (cp_parser *, tree, tree, name_lookup_error, location_t);
2489 static bool cp_parser_simulate_error
2490 (cp_parser *);
2491 static bool cp_parser_check_type_definition
2492 (cp_parser *);
2493 static void cp_parser_check_for_definition_in_return_type
2494 (cp_declarator *, tree, location_t type_location);
2495 static void cp_parser_check_for_invalid_template_id
2496 (cp_parser *, tree, enum tag_types, location_t location);
2497 static bool cp_parser_non_integral_constant_expression
2498 (cp_parser *, non_integral_constant);
2499 static void cp_parser_diagnose_invalid_type_name
2500 (cp_parser *, tree, location_t);
2501 static bool cp_parser_parse_and_diagnose_invalid_type_name
2502 (cp_parser *);
2503 static int cp_parser_skip_to_closing_parenthesis
2504 (cp_parser *, bool, bool, bool);
2505 static void cp_parser_skip_to_end_of_statement
2506 (cp_parser *);
2507 static void cp_parser_consume_semicolon_at_end_of_statement
2508 (cp_parser *);
2509 static void cp_parser_skip_to_end_of_block_or_statement
2510 (cp_parser *);
2511 static bool cp_parser_skip_to_closing_brace
2512 (cp_parser *);
2513 static void cp_parser_skip_to_end_of_template_parameter_list
2514 (cp_parser *);
2515 static void cp_parser_skip_to_pragma_eol
2516 (cp_parser*, cp_token *);
2517 static bool cp_parser_error_occurred
2518 (cp_parser *);
2519 static bool cp_parser_allow_gnu_extensions_p
2520 (cp_parser *);
2521 static bool cp_parser_is_pure_string_literal
2522 (cp_token *);
2523 static bool cp_parser_is_string_literal
2524 (cp_token *);
2525 static bool cp_parser_is_keyword
2526 (cp_token *, enum rid);
2527 static tree cp_parser_make_typename_type
2528 (cp_parser *, tree, location_t location);
2529 static cp_declarator * cp_parser_make_indirect_declarator
2530 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2531 static bool cp_parser_compound_literal_p
2532 (cp_parser *);
2533 static bool cp_parser_array_designator_p
2534 (cp_parser *);
2535 static bool cp_parser_skip_to_closing_square_bracket
2536 (cp_parser *);
2538 /* Returns nonzero if we are parsing tentatively. */
2540 static inline bool
2541 cp_parser_parsing_tentatively (cp_parser* parser)
2543 return parser->context->next != NULL;
2546 /* Returns nonzero if TOKEN is a string literal. */
2548 static bool
2549 cp_parser_is_pure_string_literal (cp_token* token)
2551 return (token->type == CPP_STRING ||
2552 token->type == CPP_STRING16 ||
2553 token->type == CPP_STRING32 ||
2554 token->type == CPP_WSTRING ||
2555 token->type == CPP_UTF8STRING);
2558 /* Returns nonzero if TOKEN is a string literal
2559 of a user-defined string literal. */
2561 static bool
2562 cp_parser_is_string_literal (cp_token* token)
2564 return (cp_parser_is_pure_string_literal (token) ||
2565 token->type == CPP_STRING_USERDEF ||
2566 token->type == CPP_STRING16_USERDEF ||
2567 token->type == CPP_STRING32_USERDEF ||
2568 token->type == CPP_WSTRING_USERDEF ||
2569 token->type == CPP_UTF8STRING_USERDEF);
2572 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2574 static bool
2575 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2577 return token->keyword == keyword;
2580 /* If not parsing tentatively, issue a diagnostic of the form
2581 FILE:LINE: MESSAGE before TOKEN
2582 where TOKEN is the next token in the input stream. MESSAGE
2583 (specified by the caller) is usually of the form "expected
2584 OTHER-TOKEN". */
2586 static void
2587 cp_parser_error (cp_parser* parser, const char* gmsgid)
2589 if (!cp_parser_simulate_error (parser))
2591 cp_token *token = cp_lexer_peek_token (parser->lexer);
2592 /* This diagnostic makes more sense if it is tagged to the line
2593 of the token we just peeked at. */
2594 cp_lexer_set_source_position_from_token (token);
2596 if (token->type == CPP_PRAGMA)
2598 error_at (token->location,
2599 "%<#pragma%> is not allowed here");
2600 cp_parser_skip_to_pragma_eol (parser, token);
2601 return;
2604 c_parse_error (gmsgid,
2605 /* Because c_parser_error does not understand
2606 CPP_KEYWORD, keywords are treated like
2607 identifiers. */
2608 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2609 token->u.value, token->flags);
2613 /* Issue an error about name-lookup failing. NAME is the
2614 IDENTIFIER_NODE DECL is the result of
2615 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2616 the thing that we hoped to find. */
2618 static void
2619 cp_parser_name_lookup_error (cp_parser* parser,
2620 tree name,
2621 tree decl,
2622 name_lookup_error desired,
2623 location_t location)
2625 /* If name lookup completely failed, tell the user that NAME was not
2626 declared. */
2627 if (decl == error_mark_node)
2629 if (parser->scope && parser->scope != global_namespace)
2630 error_at (location, "%<%E::%E%> has not been declared",
2631 parser->scope, name);
2632 else if (parser->scope == global_namespace)
2633 error_at (location, "%<::%E%> has not been declared", name);
2634 else if (parser->object_scope
2635 && !CLASS_TYPE_P (parser->object_scope))
2636 error_at (location, "request for member %qE in non-class type %qT",
2637 name, parser->object_scope);
2638 else if (parser->object_scope)
2639 error_at (location, "%<%T::%E%> has not been declared",
2640 parser->object_scope, name);
2641 else
2642 error_at (location, "%qE has not been declared", name);
2644 else if (parser->scope && parser->scope != global_namespace)
2646 switch (desired)
2648 case NLE_TYPE:
2649 error_at (location, "%<%E::%E%> is not a type",
2650 parser->scope, name);
2651 break;
2652 case NLE_CXX98:
2653 error_at (location, "%<%E::%E%> is not a class or namespace",
2654 parser->scope, name);
2655 break;
2656 case NLE_NOT_CXX98:
2657 error_at (location,
2658 "%<%E::%E%> is not a class, namespace, or enumeration",
2659 parser->scope, name);
2660 break;
2661 default:
2662 gcc_unreachable ();
2666 else if (parser->scope == global_namespace)
2668 switch (desired)
2670 case NLE_TYPE:
2671 error_at (location, "%<::%E%> is not a type", name);
2672 break;
2673 case NLE_CXX98:
2674 error_at (location, "%<::%E%> is not a class or namespace", name);
2675 break;
2676 case NLE_NOT_CXX98:
2677 error_at (location,
2678 "%<::%E%> is not a class, namespace, or enumeration",
2679 name);
2680 break;
2681 default:
2682 gcc_unreachable ();
2685 else
2687 switch (desired)
2689 case NLE_TYPE:
2690 error_at (location, "%qE is not a type", name);
2691 break;
2692 case NLE_CXX98:
2693 error_at (location, "%qE is not a class or namespace", name);
2694 break;
2695 case NLE_NOT_CXX98:
2696 error_at (location,
2697 "%qE is not a class, namespace, or enumeration", name);
2698 break;
2699 default:
2700 gcc_unreachable ();
2705 /* If we are parsing tentatively, remember that an error has occurred
2706 during this tentative parse. Returns true if the error was
2707 simulated; false if a message should be issued by the caller. */
2709 static bool
2710 cp_parser_simulate_error (cp_parser* parser)
2712 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2714 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2715 return true;
2717 return false;
2720 /* This function is called when a type is defined. If type
2721 definitions are forbidden at this point, an error message is
2722 issued. */
2724 static bool
2725 cp_parser_check_type_definition (cp_parser* parser)
2727 /* If types are forbidden here, issue a message. */
2728 if (parser->type_definition_forbidden_message)
2730 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2731 in the message need to be interpreted. */
2732 error (parser->type_definition_forbidden_message);
2733 return false;
2735 return true;
2738 /* This function is called when the DECLARATOR is processed. The TYPE
2739 was a type defined in the decl-specifiers. If it is invalid to
2740 define a type in the decl-specifiers for DECLARATOR, an error is
2741 issued. TYPE_LOCATION is the location of TYPE and is used
2742 for error reporting. */
2744 static void
2745 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2746 tree type, location_t type_location)
2748 /* [dcl.fct] forbids type definitions in return types.
2749 Unfortunately, it's not easy to know whether or not we are
2750 processing a return type until after the fact. */
2751 while (declarator
2752 && (declarator->kind == cdk_pointer
2753 || declarator->kind == cdk_reference
2754 || declarator->kind == cdk_ptrmem))
2755 declarator = declarator->declarator;
2756 if (declarator
2757 && declarator->kind == cdk_function)
2759 error_at (type_location,
2760 "new types may not be defined in a return type");
2761 inform (type_location,
2762 "(perhaps a semicolon is missing after the definition of %qT)",
2763 type);
2767 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2768 "<" in any valid C++ program. If the next token is indeed "<",
2769 issue a message warning the user about what appears to be an
2770 invalid attempt to form a template-id. LOCATION is the location
2771 of the type-specifier (TYPE) */
2773 static void
2774 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2775 tree type,
2776 enum tag_types tag_type,
2777 location_t location)
2779 cp_token_position start = 0;
2781 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2783 if (TYPE_P (type))
2784 error_at (location, "%qT is not a template", type);
2785 else if (identifier_p (type))
2787 if (tag_type != none_type)
2788 error_at (location, "%qE is not a class template", type);
2789 else
2790 error_at (location, "%qE is not a template", type);
2792 else
2793 error_at (location, "invalid template-id");
2794 /* Remember the location of the invalid "<". */
2795 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2796 start = cp_lexer_token_position (parser->lexer, true);
2797 /* Consume the "<". */
2798 cp_lexer_consume_token (parser->lexer);
2799 /* Parse the template arguments. */
2800 cp_parser_enclosed_template_argument_list (parser);
2801 /* Permanently remove the invalid template arguments so that
2802 this error message is not issued again. */
2803 if (start)
2804 cp_lexer_purge_tokens_after (parser->lexer, start);
2808 /* If parsing an integral constant-expression, issue an error message
2809 about the fact that THING appeared and return true. Otherwise,
2810 return false. In either case, set
2811 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2813 static bool
2814 cp_parser_non_integral_constant_expression (cp_parser *parser,
2815 non_integral_constant thing)
2817 parser->non_integral_constant_expression_p = true;
2818 if (parser->integral_constant_expression_p)
2820 if (!parser->allow_non_integral_constant_expression_p)
2822 const char *msg = NULL;
2823 switch (thing)
2825 case NIC_FLOAT:
2826 error ("floating-point literal "
2827 "cannot appear in a constant-expression");
2828 return true;
2829 case NIC_CAST:
2830 error ("a cast to a type other than an integral or "
2831 "enumeration type cannot appear in a "
2832 "constant-expression");
2833 return true;
2834 case NIC_TYPEID:
2835 error ("%<typeid%> operator "
2836 "cannot appear in a constant-expression");
2837 return true;
2838 case NIC_NCC:
2839 error ("non-constant compound literals "
2840 "cannot appear in a constant-expression");
2841 return true;
2842 case NIC_FUNC_CALL:
2843 error ("a function call "
2844 "cannot appear in a constant-expression");
2845 return true;
2846 case NIC_INC:
2847 error ("an increment "
2848 "cannot appear in a constant-expression");
2849 return true;
2850 case NIC_DEC:
2851 error ("an decrement "
2852 "cannot appear in a constant-expression");
2853 return true;
2854 case NIC_ARRAY_REF:
2855 error ("an array reference "
2856 "cannot appear in a constant-expression");
2857 return true;
2858 case NIC_ADDR_LABEL:
2859 error ("the address of a label "
2860 "cannot appear in a constant-expression");
2861 return true;
2862 case NIC_OVERLOADED:
2863 error ("calls to overloaded operators "
2864 "cannot appear in a constant-expression");
2865 return true;
2866 case NIC_ASSIGNMENT:
2867 error ("an assignment cannot appear in a constant-expression");
2868 return true;
2869 case NIC_COMMA:
2870 error ("a comma operator "
2871 "cannot appear in a constant-expression");
2872 return true;
2873 case NIC_CONSTRUCTOR:
2874 error ("a call to a constructor "
2875 "cannot appear in a constant-expression");
2876 return true;
2877 case NIC_TRANSACTION:
2878 error ("a transaction expression "
2879 "cannot appear in a constant-expression");
2880 return true;
2881 case NIC_THIS:
2882 msg = "this";
2883 break;
2884 case NIC_FUNC_NAME:
2885 msg = "__FUNCTION__";
2886 break;
2887 case NIC_PRETTY_FUNC:
2888 msg = "__PRETTY_FUNCTION__";
2889 break;
2890 case NIC_C99_FUNC:
2891 msg = "__func__";
2892 break;
2893 case NIC_VA_ARG:
2894 msg = "va_arg";
2895 break;
2896 case NIC_ARROW:
2897 msg = "->";
2898 break;
2899 case NIC_POINT:
2900 msg = ".";
2901 break;
2902 case NIC_STAR:
2903 msg = "*";
2904 break;
2905 case NIC_ADDR:
2906 msg = "&";
2907 break;
2908 case NIC_PREINCREMENT:
2909 msg = "++";
2910 break;
2911 case NIC_PREDECREMENT:
2912 msg = "--";
2913 break;
2914 case NIC_NEW:
2915 msg = "new";
2916 break;
2917 case NIC_DEL:
2918 msg = "delete";
2919 break;
2920 default:
2921 gcc_unreachable ();
2923 if (msg)
2924 error ("%qs cannot appear in a constant-expression", msg);
2925 return true;
2928 return false;
2931 /* Emit a diagnostic for an invalid type name. This function commits
2932 to the current active tentative parse, if any. (Otherwise, the
2933 problematic construct might be encountered again later, resulting
2934 in duplicate error messages.) LOCATION is the location of ID. */
2936 static void
2937 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2938 location_t location)
2940 tree decl, ambiguous_decls;
2941 cp_parser_commit_to_tentative_parse (parser);
2942 /* Try to lookup the identifier. */
2943 decl = cp_parser_lookup_name (parser, id, none_type,
2944 /*is_template=*/false,
2945 /*is_namespace=*/false,
2946 /*check_dependency=*/true,
2947 &ambiguous_decls, location);
2948 if (ambiguous_decls)
2949 /* If the lookup was ambiguous, an error will already have
2950 been issued. */
2951 return;
2952 /* If the lookup found a template-name, it means that the user forgot
2953 to specify an argument list. Emit a useful error message. */
2954 if (TREE_CODE (decl) == TEMPLATE_DECL)
2955 error_at (location,
2956 "invalid use of template-name %qE without an argument list",
2957 decl);
2958 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2959 error_at (location, "invalid use of destructor %qD as a type", id);
2960 else if (TREE_CODE (decl) == TYPE_DECL)
2961 /* Something like 'unsigned A a;' */
2962 error_at (location, "invalid combination of multiple type-specifiers");
2963 else if (!parser->scope)
2965 /* Issue an error message. */
2966 error_at (location, "%qE does not name a type", id);
2967 /* If we're in a template class, it's possible that the user was
2968 referring to a type from a base class. For example:
2970 template <typename T> struct A { typedef T X; };
2971 template <typename T> struct B : public A<T> { X x; };
2973 The user should have said "typename A<T>::X". */
2974 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2975 inform (location, "C++11 %<constexpr%> only available with "
2976 "-std=c++11 or -std=gnu++11");
2977 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2978 inform (location, "C++11 %<noexcept%> only available with "
2979 "-std=c++11 or -std=gnu++11");
2980 else if (cxx_dialect < cxx11
2981 && TREE_CODE (id) == IDENTIFIER_NODE
2982 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2983 inform (location, "C++11 %<thread_local%> only available with "
2984 "-std=c++11 or -std=gnu++11");
2985 else if (processing_template_decl && current_class_type
2986 && TYPE_BINFO (current_class_type))
2988 tree b;
2990 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2992 b = TREE_CHAIN (b))
2994 tree base_type = BINFO_TYPE (b);
2995 if (CLASS_TYPE_P (base_type)
2996 && dependent_type_p (base_type))
2998 tree field;
2999 /* Go from a particular instantiation of the
3000 template (which will have an empty TYPE_FIELDs),
3001 to the main version. */
3002 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3003 for (field = TYPE_FIELDS (base_type);
3004 field;
3005 field = DECL_CHAIN (field))
3006 if (TREE_CODE (field) == TYPE_DECL
3007 && DECL_NAME (field) == id)
3009 inform (location,
3010 "(perhaps %<typename %T::%E%> was intended)",
3011 BINFO_TYPE (b), id);
3012 break;
3014 if (field)
3015 break;
3020 /* Here we diagnose qualified-ids where the scope is actually correct,
3021 but the identifier does not resolve to a valid type name. */
3022 else if (parser->scope != error_mark_node)
3024 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3026 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3027 error_at (location_of (id),
3028 "%qE in namespace %qE does not name a template type",
3029 id, parser->scope);
3030 else
3031 error_at (location_of (id),
3032 "%qE in namespace %qE does not name a type",
3033 id, parser->scope);
3035 else if (CLASS_TYPE_P (parser->scope)
3036 && constructor_name_p (id, parser->scope))
3038 /* A<T>::A<T>() */
3039 error_at (location, "%<%T::%E%> names the constructor, not"
3040 " the type", parser->scope, id);
3041 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3042 error_at (location, "and %qT has no template constructors",
3043 parser->scope);
3045 else if (TYPE_P (parser->scope)
3046 && dependent_scope_p (parser->scope))
3047 error_at (location, "need %<typename%> before %<%T::%E%> because "
3048 "%qT is a dependent scope",
3049 parser->scope, id, parser->scope);
3050 else if (TYPE_P (parser->scope))
3052 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3053 error_at (location_of (id),
3054 "%qE in %q#T does not name a template type",
3055 id, parser->scope);
3056 else
3057 error_at (location_of (id),
3058 "%qE in %q#T does not name a type",
3059 id, parser->scope);
3061 else
3062 gcc_unreachable ();
3066 /* Check for a common situation where a type-name should be present,
3067 but is not, and issue a sensible error message. Returns true if an
3068 invalid type-name was detected.
3070 The situation handled by this function are variable declarations of the
3071 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3072 Usually, `ID' should name a type, but if we got here it means that it
3073 does not. We try to emit the best possible error message depending on
3074 how exactly the id-expression looks like. */
3076 static bool
3077 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3079 tree id;
3080 cp_token *token = cp_lexer_peek_token (parser->lexer);
3082 /* Avoid duplicate error about ambiguous lookup. */
3083 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3085 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3086 if (next->type == CPP_NAME && next->error_reported)
3087 goto out;
3090 cp_parser_parse_tentatively (parser);
3091 id = cp_parser_id_expression (parser,
3092 /*template_keyword_p=*/false,
3093 /*check_dependency_p=*/true,
3094 /*template_p=*/NULL,
3095 /*declarator_p=*/true,
3096 /*optional_p=*/false);
3097 /* If the next token is a (, this is a function with no explicit return
3098 type, i.e. constructor, destructor or conversion op. */
3099 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3100 || TREE_CODE (id) == TYPE_DECL)
3102 cp_parser_abort_tentative_parse (parser);
3103 return false;
3105 if (!cp_parser_parse_definitely (parser))
3106 return false;
3108 /* Emit a diagnostic for the invalid type. */
3109 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3110 out:
3111 /* If we aren't in the middle of a declarator (i.e. in a
3112 parameter-declaration-clause), skip to the end of the declaration;
3113 there's no point in trying to process it. */
3114 if (!parser->in_declarator_p)
3115 cp_parser_skip_to_end_of_block_or_statement (parser);
3116 return true;
3119 /* Consume tokens up to, and including, the next non-nested closing `)'.
3120 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3121 are doing error recovery. Returns -1 if OR_COMMA is true and we
3122 found an unnested comma. */
3124 static int
3125 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3126 bool recovering,
3127 bool or_comma,
3128 bool consume_paren)
3130 unsigned paren_depth = 0;
3131 unsigned brace_depth = 0;
3132 unsigned square_depth = 0;
3134 if (recovering && !or_comma
3135 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3136 return 0;
3138 while (true)
3140 cp_token * token = cp_lexer_peek_token (parser->lexer);
3142 switch (token->type)
3144 case CPP_EOF:
3145 case CPP_PRAGMA_EOL:
3146 /* If we've run out of tokens, then there is no closing `)'. */
3147 return 0;
3149 /* This is good for lambda expression capture-lists. */
3150 case CPP_OPEN_SQUARE:
3151 ++square_depth;
3152 break;
3153 case CPP_CLOSE_SQUARE:
3154 if (!square_depth--)
3155 return 0;
3156 break;
3158 case CPP_SEMICOLON:
3159 /* This matches the processing in skip_to_end_of_statement. */
3160 if (!brace_depth)
3161 return 0;
3162 break;
3164 case CPP_OPEN_BRACE:
3165 ++brace_depth;
3166 break;
3167 case CPP_CLOSE_BRACE:
3168 if (!brace_depth--)
3169 return 0;
3170 break;
3172 case CPP_COMMA:
3173 if (recovering && or_comma && !brace_depth && !paren_depth
3174 && !square_depth)
3175 return -1;
3176 break;
3178 case CPP_OPEN_PAREN:
3179 if (!brace_depth)
3180 ++paren_depth;
3181 break;
3183 case CPP_CLOSE_PAREN:
3184 if (!brace_depth && !paren_depth--)
3186 if (consume_paren)
3187 cp_lexer_consume_token (parser->lexer);
3188 return 1;
3190 break;
3192 default:
3193 break;
3196 /* Consume the token. */
3197 cp_lexer_consume_token (parser->lexer);
3201 /* Consume tokens until we reach the end of the current statement.
3202 Normally, that will be just before consuming a `;'. However, if a
3203 non-nested `}' comes first, then we stop before consuming that. */
3205 static void
3206 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3208 unsigned nesting_depth = 0;
3210 /* Unwind generic function template scope if necessary. */
3211 if (parser->fully_implicit_function_template_p)
3212 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3214 while (true)
3216 cp_token *token = cp_lexer_peek_token (parser->lexer);
3218 switch (token->type)
3220 case CPP_EOF:
3221 case CPP_PRAGMA_EOL:
3222 /* If we've run out of tokens, stop. */
3223 return;
3225 case CPP_SEMICOLON:
3226 /* If the next token is a `;', we have reached the end of the
3227 statement. */
3228 if (!nesting_depth)
3229 return;
3230 break;
3232 case CPP_CLOSE_BRACE:
3233 /* If this is a non-nested '}', stop before consuming it.
3234 That way, when confronted with something like:
3236 { 3 + }
3238 we stop before consuming the closing '}', even though we
3239 have not yet reached a `;'. */
3240 if (nesting_depth == 0)
3241 return;
3243 /* If it is the closing '}' for a block that we have
3244 scanned, stop -- but only after consuming the token.
3245 That way given:
3247 void f g () { ... }
3248 typedef int I;
3250 we will stop after the body of the erroneously declared
3251 function, but before consuming the following `typedef'
3252 declaration. */
3253 if (--nesting_depth == 0)
3255 cp_lexer_consume_token (parser->lexer);
3256 return;
3259 case CPP_OPEN_BRACE:
3260 ++nesting_depth;
3261 break;
3263 default:
3264 break;
3267 /* Consume the token. */
3268 cp_lexer_consume_token (parser->lexer);
3272 /* This function is called at the end of a statement or declaration.
3273 If the next token is a semicolon, it is consumed; otherwise, error
3274 recovery is attempted. */
3276 static void
3277 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3279 /* Look for the trailing `;'. */
3280 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3282 /* If there is additional (erroneous) input, skip to the end of
3283 the statement. */
3284 cp_parser_skip_to_end_of_statement (parser);
3285 /* If the next token is now a `;', consume it. */
3286 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3287 cp_lexer_consume_token (parser->lexer);
3291 /* Skip tokens until we have consumed an entire block, or until we
3292 have consumed a non-nested `;'. */
3294 static void
3295 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3297 int nesting_depth = 0;
3299 /* Unwind generic function template scope if necessary. */
3300 if (parser->fully_implicit_function_template_p)
3301 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3303 while (nesting_depth >= 0)
3305 cp_token *token = cp_lexer_peek_token (parser->lexer);
3307 switch (token->type)
3309 case CPP_EOF:
3310 case CPP_PRAGMA_EOL:
3311 /* If we've run out of tokens, stop. */
3312 return;
3314 case CPP_SEMICOLON:
3315 /* Stop if this is an unnested ';'. */
3316 if (!nesting_depth)
3317 nesting_depth = -1;
3318 break;
3320 case CPP_CLOSE_BRACE:
3321 /* Stop if this is an unnested '}', or closes the outermost
3322 nesting level. */
3323 nesting_depth--;
3324 if (nesting_depth < 0)
3325 return;
3326 if (!nesting_depth)
3327 nesting_depth = -1;
3328 break;
3330 case CPP_OPEN_BRACE:
3331 /* Nest. */
3332 nesting_depth++;
3333 break;
3335 default:
3336 break;
3339 /* Consume the token. */
3340 cp_lexer_consume_token (parser->lexer);
3344 /* Skip tokens until a non-nested closing curly brace is the next
3345 token, or there are no more tokens. Return true in the first case,
3346 false otherwise. */
3348 static bool
3349 cp_parser_skip_to_closing_brace (cp_parser *parser)
3351 unsigned nesting_depth = 0;
3353 while (true)
3355 cp_token *token = cp_lexer_peek_token (parser->lexer);
3357 switch (token->type)
3359 case CPP_EOF:
3360 case CPP_PRAGMA_EOL:
3361 /* If we've run out of tokens, stop. */
3362 return false;
3364 case CPP_CLOSE_BRACE:
3365 /* If the next token is a non-nested `}', then we have reached
3366 the end of the current block. */
3367 if (nesting_depth-- == 0)
3368 return true;
3369 break;
3371 case CPP_OPEN_BRACE:
3372 /* If it the next token is a `{', then we are entering a new
3373 block. Consume the entire block. */
3374 ++nesting_depth;
3375 break;
3377 default:
3378 break;
3381 /* Consume the token. */
3382 cp_lexer_consume_token (parser->lexer);
3386 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3387 parameter is the PRAGMA token, allowing us to purge the entire pragma
3388 sequence. */
3390 static void
3391 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3393 cp_token *token;
3395 parser->lexer->in_pragma = false;
3398 token = cp_lexer_consume_token (parser->lexer);
3399 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3401 /* Ensure that the pragma is not parsed again. */
3402 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3405 /* Require pragma end of line, resyncing with it as necessary. The
3406 arguments are as for cp_parser_skip_to_pragma_eol. */
3408 static void
3409 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3411 parser->lexer->in_pragma = false;
3412 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3413 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3416 /* This is a simple wrapper around make_typename_type. When the id is
3417 an unresolved identifier node, we can provide a superior diagnostic
3418 using cp_parser_diagnose_invalid_type_name. */
3420 static tree
3421 cp_parser_make_typename_type (cp_parser *parser, tree id,
3422 location_t id_location)
3424 tree result;
3425 if (identifier_p (id))
3427 result = make_typename_type (parser->scope, id, typename_type,
3428 /*complain=*/tf_none);
3429 if (result == error_mark_node)
3430 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3431 return result;
3433 return make_typename_type (parser->scope, id, typename_type, tf_error);
3436 /* This is a wrapper around the
3437 make_{pointer,ptrmem,reference}_declarator functions that decides
3438 which one to call based on the CODE and CLASS_TYPE arguments. The
3439 CODE argument should be one of the values returned by
3440 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3441 appertain to the pointer or reference. */
3443 static cp_declarator *
3444 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3445 cp_cv_quals cv_qualifiers,
3446 cp_declarator *target,
3447 tree attributes)
3449 if (code == ERROR_MARK)
3450 return cp_error_declarator;
3452 if (code == INDIRECT_REF)
3453 if (class_type == NULL_TREE)
3454 return make_pointer_declarator (cv_qualifiers, target, attributes);
3455 else
3456 return make_ptrmem_declarator (cv_qualifiers, class_type,
3457 target, attributes);
3458 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3459 return make_reference_declarator (cv_qualifiers, target,
3460 false, attributes);
3461 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3462 return make_reference_declarator (cv_qualifiers, target,
3463 true, attributes);
3464 gcc_unreachable ();
3467 /* Create a new C++ parser. */
3469 static cp_parser *
3470 cp_parser_new (void)
3472 cp_parser *parser;
3473 cp_lexer *lexer;
3474 unsigned i;
3476 /* cp_lexer_new_main is called before doing GC allocation because
3477 cp_lexer_new_main might load a PCH file. */
3478 lexer = cp_lexer_new_main ();
3480 /* Initialize the binops_by_token so that we can get the tree
3481 directly from the token. */
3482 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3483 binops_by_token[binops[i].token_type] = binops[i];
3485 parser = ggc_cleared_alloc<cp_parser> ();
3486 parser->lexer = lexer;
3487 parser->context = cp_parser_context_new (NULL);
3489 /* For now, we always accept GNU extensions. */
3490 parser->allow_gnu_extensions_p = 1;
3492 /* The `>' token is a greater-than operator, not the end of a
3493 template-id. */
3494 parser->greater_than_is_operator_p = true;
3496 parser->default_arg_ok_p = true;
3498 /* We are not parsing a constant-expression. */
3499 parser->integral_constant_expression_p = false;
3500 parser->allow_non_integral_constant_expression_p = false;
3501 parser->non_integral_constant_expression_p = false;
3503 /* Local variable names are not forbidden. */
3504 parser->local_variables_forbidden_p = false;
3506 /* We are not processing an `extern "C"' declaration. */
3507 parser->in_unbraced_linkage_specification_p = false;
3509 /* We are not processing a declarator. */
3510 parser->in_declarator_p = false;
3512 /* We are not processing a template-argument-list. */
3513 parser->in_template_argument_list_p = false;
3515 /* We are not in an iteration statement. */
3516 parser->in_statement = 0;
3518 /* We are not in a switch statement. */
3519 parser->in_switch_statement_p = false;
3521 /* We are not parsing a type-id inside an expression. */
3522 parser->in_type_id_in_expr_p = false;
3524 /* Declarations aren't implicitly extern "C". */
3525 parser->implicit_extern_c = false;
3527 /* String literals should be translated to the execution character set. */
3528 parser->translate_strings_p = true;
3530 /* We are not parsing a function body. */
3531 parser->in_function_body = false;
3533 /* We can correct until told otherwise. */
3534 parser->colon_corrects_to_scope_p = true;
3536 /* The unparsed function queue is empty. */
3537 push_unparsed_function_queues (parser);
3539 /* There are no classes being defined. */
3540 parser->num_classes_being_defined = 0;
3542 /* No template parameters apply. */
3543 parser->num_template_parameter_lists = 0;
3545 /* Not declaring an implicit function template. */
3546 parser->auto_is_implicit_function_template_parm_p = false;
3547 parser->fully_implicit_function_template_p = false;
3548 parser->implicit_template_parms = 0;
3549 parser->implicit_template_scope = 0;
3551 return parser;
3554 /* Create a cp_lexer structure which will emit the tokens in CACHE
3555 and push it onto the parser's lexer stack. This is used for delayed
3556 parsing of in-class method bodies and default arguments, and should
3557 not be confused with tentative parsing. */
3558 static void
3559 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3561 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3562 lexer->next = parser->lexer;
3563 parser->lexer = lexer;
3565 /* Move the current source position to that of the first token in the
3566 new lexer. */
3567 cp_lexer_set_source_position_from_token (lexer->next_token);
3570 /* Pop the top lexer off the parser stack. This is never used for the
3571 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3572 static void
3573 cp_parser_pop_lexer (cp_parser *parser)
3575 cp_lexer *lexer = parser->lexer;
3576 parser->lexer = lexer->next;
3577 cp_lexer_destroy (lexer);
3579 /* Put the current source position back where it was before this
3580 lexer was pushed. */
3581 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3584 /* Lexical conventions [gram.lex] */
3586 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3587 identifier. */
3589 static tree
3590 cp_parser_identifier (cp_parser* parser)
3592 cp_token *token;
3594 /* Look for the identifier. */
3595 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3596 /* Return the value. */
3597 return token ? token->u.value : error_mark_node;
3600 /* Parse a sequence of adjacent string constants. Returns a
3601 TREE_STRING representing the combined, nul-terminated string
3602 constant. If TRANSLATE is true, translate the string to the
3603 execution character set. If WIDE_OK is true, a wide string is
3604 invalid here.
3606 C++98 [lex.string] says that if a narrow string literal token is
3607 adjacent to a wide string literal token, the behavior is undefined.
3608 However, C99 6.4.5p4 says that this results in a wide string literal.
3609 We follow C99 here, for consistency with the C front end.
3611 This code is largely lifted from lex_string() in c-lex.c.
3613 FUTURE: ObjC++ will need to handle @-strings here. */
3614 static tree
3615 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3616 bool lookup_udlit = true)
3618 tree value;
3619 size_t count;
3620 struct obstack str_ob;
3621 cpp_string str, istr, *strs;
3622 cp_token *tok;
3623 enum cpp_ttype type, curr_type;
3624 int have_suffix_p = 0;
3625 tree string_tree;
3626 tree suffix_id = NULL_TREE;
3627 bool curr_tok_is_userdef_p = false;
3629 tok = cp_lexer_peek_token (parser->lexer);
3630 if (!cp_parser_is_string_literal (tok))
3632 cp_parser_error (parser, "expected string-literal");
3633 return error_mark_node;
3636 if (cpp_userdef_string_p (tok->type))
3638 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3639 curr_type = cpp_userdef_string_remove_type (tok->type);
3640 curr_tok_is_userdef_p = true;
3642 else
3644 string_tree = tok->u.value;
3645 curr_type = tok->type;
3647 type = curr_type;
3649 /* Try to avoid the overhead of creating and destroying an obstack
3650 for the common case of just one string. */
3651 if (!cp_parser_is_string_literal
3652 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3654 cp_lexer_consume_token (parser->lexer);
3656 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3657 str.len = TREE_STRING_LENGTH (string_tree);
3658 count = 1;
3660 if (curr_tok_is_userdef_p)
3662 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3663 have_suffix_p = 1;
3664 curr_type = cpp_userdef_string_remove_type (tok->type);
3666 else
3667 curr_type = tok->type;
3669 strs = &str;
3671 else
3673 gcc_obstack_init (&str_ob);
3674 count = 0;
3678 cp_lexer_consume_token (parser->lexer);
3679 count++;
3680 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3681 str.len = TREE_STRING_LENGTH (string_tree);
3683 if (curr_tok_is_userdef_p)
3685 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3686 if (have_suffix_p == 0)
3688 suffix_id = curr_suffix_id;
3689 have_suffix_p = 1;
3691 else if (have_suffix_p == 1
3692 && curr_suffix_id != suffix_id)
3694 error ("inconsistent user-defined literal suffixes"
3695 " %qD and %qD in string literal",
3696 suffix_id, curr_suffix_id);
3697 have_suffix_p = -1;
3699 curr_type = cpp_userdef_string_remove_type (tok->type);
3701 else
3702 curr_type = tok->type;
3704 if (type != curr_type)
3706 if (type == CPP_STRING)
3707 type = curr_type;
3708 else if (curr_type != CPP_STRING)
3709 error_at (tok->location,
3710 "unsupported non-standard concatenation "
3711 "of string literals");
3714 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3716 tok = cp_lexer_peek_token (parser->lexer);
3717 if (cpp_userdef_string_p (tok->type))
3719 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3720 curr_type = cpp_userdef_string_remove_type (tok->type);
3721 curr_tok_is_userdef_p = true;
3723 else
3725 string_tree = tok->u.value;
3726 curr_type = tok->type;
3727 curr_tok_is_userdef_p = false;
3730 while (cp_parser_is_string_literal (tok));
3732 strs = (cpp_string *) obstack_finish (&str_ob);
3735 if (type != CPP_STRING && !wide_ok)
3737 cp_parser_error (parser, "a wide string is invalid in this context");
3738 type = CPP_STRING;
3741 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3742 (parse_in, strs, count, &istr, type))
3744 value = build_string (istr.len, (const char *)istr.text);
3745 free (CONST_CAST (unsigned char *, istr.text));
3747 switch (type)
3749 default:
3750 case CPP_STRING:
3751 case CPP_UTF8STRING:
3752 TREE_TYPE (value) = char_array_type_node;
3753 break;
3754 case CPP_STRING16:
3755 TREE_TYPE (value) = char16_array_type_node;
3756 break;
3757 case CPP_STRING32:
3758 TREE_TYPE (value) = char32_array_type_node;
3759 break;
3760 case CPP_WSTRING:
3761 TREE_TYPE (value) = wchar_array_type_node;
3762 break;
3765 value = fix_string_type (value);
3767 if (have_suffix_p)
3769 tree literal = build_userdef_literal (suffix_id, value,
3770 OT_NONE, NULL_TREE);
3771 if (lookup_udlit)
3772 value = cp_parser_userdef_string_literal (literal);
3773 else
3774 value = literal;
3777 else
3778 /* cpp_interpret_string has issued an error. */
3779 value = error_mark_node;
3781 if (count > 1)
3782 obstack_free (&str_ob, 0);
3784 return value;
3787 /* Look up a literal operator with the name and the exact arguments. */
3789 static tree
3790 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3792 tree decl, fns;
3793 decl = lookup_name (name);
3794 if (!decl || !is_overloaded_fn (decl))
3795 return error_mark_node;
3797 for (fns = decl; fns; fns = OVL_NEXT (fns))
3799 unsigned int ix;
3800 bool found = true;
3801 tree fn = OVL_CURRENT (fns);
3802 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3803 if (parmtypes != NULL_TREE)
3805 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3806 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3808 tree tparm = TREE_VALUE (parmtypes);
3809 tree targ = TREE_TYPE ((*args)[ix]);
3810 bool ptr = TYPE_PTR_P (tparm);
3811 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3812 if ((ptr || arr || !same_type_p (tparm, targ))
3813 && (!ptr || !arr
3814 || !same_type_p (TREE_TYPE (tparm),
3815 TREE_TYPE (targ))))
3816 found = false;
3818 if (found
3819 && ix == vec_safe_length (args)
3820 /* May be this should be sufficient_parms_p instead,
3821 depending on how exactly should user-defined literals
3822 work in presence of default arguments on the literal
3823 operator parameters. */
3824 && parmtypes == void_list_node)
3825 return fn;
3829 return error_mark_node;
3832 /* Parse a user-defined char constant. Returns a call to a user-defined
3833 literal operator taking the character as an argument. */
3835 static tree
3836 cp_parser_userdef_char_literal (cp_parser *parser)
3838 cp_token *token = cp_lexer_consume_token (parser->lexer);
3839 tree literal = token->u.value;
3840 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3841 tree value = USERDEF_LITERAL_VALUE (literal);
3842 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3843 tree decl, result;
3845 /* Build up a call to the user-defined operator */
3846 /* Lookup the name we got back from the id-expression. */
3847 vec<tree, va_gc> *args = make_tree_vector ();
3848 vec_safe_push (args, value);
3849 decl = lookup_literal_operator (name, args);
3850 if (!decl || decl == error_mark_node)
3852 error ("unable to find character literal operator %qD with %qT argument",
3853 name, TREE_TYPE (value));
3854 release_tree_vector (args);
3855 return error_mark_node;
3857 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3858 release_tree_vector (args);
3859 if (result != error_mark_node)
3860 return result;
3862 error ("unable to find character literal operator %qD with %qT argument",
3863 name, TREE_TYPE (value));
3864 return error_mark_node;
3867 /* A subroutine of cp_parser_userdef_numeric_literal to
3868 create a char... template parameter pack from a string node. */
3870 static tree
3871 make_char_string_pack (tree value)
3873 tree charvec;
3874 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3875 const char *str = TREE_STRING_POINTER (value);
3876 int i, len = TREE_STRING_LENGTH (value) - 1;
3877 tree argvec = make_tree_vec (1);
3879 /* Fill in CHARVEC with all of the parameters. */
3880 charvec = make_tree_vec (len);
3881 for (i = 0; i < len; ++i)
3882 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3884 /* Build the argument packs. */
3885 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3886 TREE_TYPE (argpack) = char_type_node;
3888 TREE_VEC_ELT (argvec, 0) = argpack;
3890 return argvec;
3893 /* A subroutine of cp_parser_userdef_numeric_literal to
3894 create a char... template parameter pack from a string node. */
3896 static tree
3897 make_string_pack (tree value)
3899 tree charvec;
3900 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3901 const unsigned char *str
3902 = (const unsigned char *) TREE_STRING_POINTER (value);
3903 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3904 int len = TREE_STRING_LENGTH (value) / sz - 1;
3905 tree argvec = make_tree_vec (2);
3907 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3908 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3910 /* First template parm is character type. */
3911 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3913 /* Fill in CHARVEC with all of the parameters. */
3914 charvec = make_tree_vec (len);
3915 for (int i = 0; i < len; ++i)
3916 TREE_VEC_ELT (charvec, i)
3917 = double_int_to_tree (str_char_type_node,
3918 double_int::from_buffer (str + i * sz, sz));
3920 /* Build the argument packs. */
3921 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3922 TREE_TYPE (argpack) = str_char_type_node;
3924 TREE_VEC_ELT (argvec, 1) = argpack;
3926 return argvec;
3929 /* Parse a user-defined numeric constant. returns a call to a user-defined
3930 literal operator. */
3932 static tree
3933 cp_parser_userdef_numeric_literal (cp_parser *parser)
3935 cp_token *token = cp_lexer_consume_token (parser->lexer);
3936 tree literal = token->u.value;
3937 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3938 tree value = USERDEF_LITERAL_VALUE (literal);
3939 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3940 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3941 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3942 tree decl, result;
3943 vec<tree, va_gc> *args;
3945 /* Look for a literal operator taking the exact type of numeric argument
3946 as the literal value. */
3947 args = make_tree_vector ();
3948 vec_safe_push (args, value);
3949 decl = lookup_literal_operator (name, args);
3950 if (decl && decl != error_mark_node)
3952 result = finish_call_expr (decl, &args, false, true, tf_none);
3953 if (result != error_mark_node)
3955 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3956 warning_at (token->location, OPT_Woverflow,
3957 "integer literal exceeds range of %qT type",
3958 long_long_unsigned_type_node);
3959 else
3961 if (overflow > 0)
3962 warning_at (token->location, OPT_Woverflow,
3963 "floating literal exceeds range of %qT type",
3964 long_double_type_node);
3965 else if (overflow < 0)
3966 warning_at (token->location, OPT_Woverflow,
3967 "floating literal truncated to zero");
3969 release_tree_vector (args);
3970 return result;
3973 release_tree_vector (args);
3975 /* If the numeric argument didn't work, look for a raw literal
3976 operator taking a const char* argument consisting of the number
3977 in string format. */
3978 args = make_tree_vector ();
3979 vec_safe_push (args, num_string);
3980 decl = lookup_literal_operator (name, args);
3981 if (decl && decl != error_mark_node)
3983 result = finish_call_expr (decl, &args, false, true, tf_none);
3984 if (result != error_mark_node)
3986 release_tree_vector (args);
3987 return result;
3990 release_tree_vector (args);
3992 /* If the raw literal didn't work, look for a non-type template
3993 function with parameter pack char.... Call the function with
3994 template parameter characters representing the number. */
3995 args = make_tree_vector ();
3996 decl = lookup_literal_operator (name, args);
3997 if (decl && decl != error_mark_node)
3999 tree tmpl_args = make_char_string_pack (num_string);
4000 decl = lookup_template_function (decl, tmpl_args);
4001 result = finish_call_expr (decl, &args, false, true, tf_none);
4002 if (result != error_mark_node)
4004 release_tree_vector (args);
4005 return result;
4008 release_tree_vector (args);
4010 error ("unable to find numeric literal operator %qD", name);
4011 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4012 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4013 "to enable more built-in suffixes");
4014 return error_mark_node;
4017 /* Parse a user-defined string constant. Returns a call to a user-defined
4018 literal operator taking a character pointer and the length of the string
4019 as arguments. */
4021 static tree
4022 cp_parser_userdef_string_literal (tree literal)
4024 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4025 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4026 tree value = USERDEF_LITERAL_VALUE (literal);
4027 int len = TREE_STRING_LENGTH (value)
4028 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4029 tree decl, result;
4030 vec<tree, va_gc> *args;
4032 /* Look for a template function with typename parameter CharT
4033 and parameter pack CharT... Call the function with
4034 template parameter characters representing the string. */
4035 args = make_tree_vector ();
4036 decl = lookup_literal_operator (name, args);
4037 if (decl && decl != error_mark_node)
4039 tree tmpl_args = make_string_pack (value);
4040 decl = lookup_template_function (decl, tmpl_args);
4041 result = finish_call_expr (decl, &args, false, true, tf_none);
4042 if (result != error_mark_node)
4044 release_tree_vector (args);
4045 return result;
4048 release_tree_vector (args);
4050 /* Build up a call to the user-defined operator */
4051 /* Lookup the name we got back from the id-expression. */
4052 args = make_tree_vector ();
4053 vec_safe_push (args, value);
4054 vec_safe_push (args, build_int_cst (size_type_node, len));
4055 decl = lookup_name (name);
4056 if (!decl || decl == error_mark_node)
4058 error ("unable to find string literal operator %qD", name);
4059 release_tree_vector (args);
4060 return error_mark_node;
4062 result = finish_call_expr (decl, &args, false, true, tf_none);
4063 release_tree_vector (args);
4064 if (result != error_mark_node)
4065 return result;
4067 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4068 name, TREE_TYPE (value), size_type_node);
4069 return error_mark_node;
4073 /* Basic concepts [gram.basic] */
4075 /* Parse a translation-unit.
4077 translation-unit:
4078 declaration-seq [opt]
4080 Returns TRUE if all went well. */
4082 static bool
4083 cp_parser_translation_unit (cp_parser* parser)
4085 /* The address of the first non-permanent object on the declarator
4086 obstack. */
4087 static void *declarator_obstack_base;
4089 bool success;
4091 /* Create the declarator obstack, if necessary. */
4092 if (!cp_error_declarator)
4094 gcc_obstack_init (&declarator_obstack);
4095 /* Create the error declarator. */
4096 cp_error_declarator = make_declarator (cdk_error);
4097 /* Create the empty parameter list. */
4098 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4099 /* Remember where the base of the declarator obstack lies. */
4100 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4103 cp_parser_declaration_seq_opt (parser);
4105 /* If there are no tokens left then all went well. */
4106 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4108 /* Get rid of the token array; we don't need it any more. */
4109 cp_lexer_destroy (parser->lexer);
4110 parser->lexer = NULL;
4112 /* This file might have been a context that's implicitly extern
4113 "C". If so, pop the lang context. (Only relevant for PCH.) */
4114 if (parser->implicit_extern_c)
4116 pop_lang_context ();
4117 parser->implicit_extern_c = false;
4120 /* Finish up. */
4121 finish_translation_unit ();
4123 success = true;
4125 else
4127 cp_parser_error (parser, "expected declaration");
4128 success = false;
4131 /* Make sure the declarator obstack was fully cleaned up. */
4132 gcc_assert (obstack_next_free (&declarator_obstack)
4133 == declarator_obstack_base);
4135 /* All went well. */
4136 return success;
4139 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4140 decltype context. */
4142 static inline tsubst_flags_t
4143 complain_flags (bool decltype_p)
4145 tsubst_flags_t complain = tf_warning_or_error;
4146 if (decltype_p)
4147 complain |= tf_decltype;
4148 return complain;
4151 /* We're about to parse a collection of statements. If we're currently
4152 parsing tentatively, set up a firewall so that any nested
4153 cp_parser_commit_to_tentative_parse won't affect the current context. */
4155 static cp_token_position
4156 cp_parser_start_tentative_firewall (cp_parser *parser)
4158 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4159 return 0;
4161 cp_parser_parse_tentatively (parser);
4162 cp_parser_commit_to_topmost_tentative_parse (parser);
4163 return cp_lexer_token_position (parser->lexer, false);
4166 /* We've finished parsing the collection of statements. Wrap up the
4167 firewall and replace the relevant tokens with the parsed form. */
4169 static void
4170 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4171 tree expr)
4173 if (!start)
4174 return;
4176 /* Finish the firewall level. */
4177 cp_parser_parse_definitely (parser);
4178 /* And remember the result of the parse for when we try again. */
4179 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4180 token->type = CPP_PREPARSED_EXPR;
4181 token->u.value = expr;
4182 token->keyword = RID_MAX;
4183 cp_lexer_purge_tokens_after (parser->lexer, start);
4186 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4187 enclosing parentheses. */
4189 static tree
4190 cp_parser_statement_expr (cp_parser *parser)
4192 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4194 /* Consume the '('. */
4195 cp_lexer_consume_token (parser->lexer);
4196 /* Start the statement-expression. */
4197 tree expr = begin_stmt_expr ();
4198 /* Parse the compound-statement. */
4199 cp_parser_compound_statement (parser, expr, false, false);
4200 /* Finish up. */
4201 expr = finish_stmt_expr (expr, false);
4202 /* Consume the ')'. */
4203 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4204 cp_parser_skip_to_end_of_statement (parser);
4206 cp_parser_end_tentative_firewall (parser, start, expr);
4207 return expr;
4210 /* Expressions [gram.expr] */
4212 /* Parse a primary-expression.
4214 primary-expression:
4215 literal
4216 this
4217 ( expression )
4218 id-expression
4219 lambda-expression (C++11)
4221 GNU Extensions:
4223 primary-expression:
4224 ( compound-statement )
4225 __builtin_va_arg ( assignment-expression , type-id )
4226 __builtin_offsetof ( type-id , offsetof-expression )
4228 C++ Extensions:
4229 __has_nothrow_assign ( type-id )
4230 __has_nothrow_constructor ( type-id )
4231 __has_nothrow_copy ( type-id )
4232 __has_trivial_assign ( type-id )
4233 __has_trivial_constructor ( type-id )
4234 __has_trivial_copy ( type-id )
4235 __has_trivial_destructor ( type-id )
4236 __has_virtual_destructor ( type-id )
4237 __is_abstract ( type-id )
4238 __is_base_of ( type-id , type-id )
4239 __is_class ( type-id )
4240 __is_empty ( type-id )
4241 __is_enum ( type-id )
4242 __is_final ( type-id )
4243 __is_literal_type ( type-id )
4244 __is_pod ( type-id )
4245 __is_polymorphic ( type-id )
4246 __is_std_layout ( type-id )
4247 __is_trivial ( type-id )
4248 __is_union ( type-id )
4250 Objective-C++ Extension:
4252 primary-expression:
4253 objc-expression
4255 literal:
4256 __null
4258 ADDRESS_P is true iff this expression was immediately preceded by
4259 "&" and therefore might denote a pointer-to-member. CAST_P is true
4260 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4261 true iff this expression is a template argument.
4263 Returns a representation of the expression. Upon return, *IDK
4264 indicates what kind of id-expression (if any) was present. */
4266 static tree
4267 cp_parser_primary_expression (cp_parser *parser,
4268 bool address_p,
4269 bool cast_p,
4270 bool template_arg_p,
4271 bool decltype_p,
4272 cp_id_kind *idk)
4274 cp_token *token = NULL;
4276 /* Assume the primary expression is not an id-expression. */
4277 *idk = CP_ID_KIND_NONE;
4279 /* Peek at the next token. */
4280 token = cp_lexer_peek_token (parser->lexer);
4281 switch ((int) token->type)
4283 /* literal:
4284 integer-literal
4285 character-literal
4286 floating-literal
4287 string-literal
4288 boolean-literal
4289 pointer-literal
4290 user-defined-literal */
4291 case CPP_CHAR:
4292 case CPP_CHAR16:
4293 case CPP_CHAR32:
4294 case CPP_WCHAR:
4295 case CPP_NUMBER:
4296 case CPP_PREPARSED_EXPR:
4297 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4298 return cp_parser_userdef_numeric_literal (parser);
4299 token = cp_lexer_consume_token (parser->lexer);
4300 if (TREE_CODE (token->u.value) == FIXED_CST)
4302 error_at (token->location,
4303 "fixed-point types not supported in C++");
4304 return error_mark_node;
4306 /* Floating-point literals are only allowed in an integral
4307 constant expression if they are cast to an integral or
4308 enumeration type. */
4309 if (TREE_CODE (token->u.value) == REAL_CST
4310 && parser->integral_constant_expression_p
4311 && pedantic)
4313 /* CAST_P will be set even in invalid code like "int(2.7 +
4314 ...)". Therefore, we have to check that the next token
4315 is sure to end the cast. */
4316 if (cast_p)
4318 cp_token *next_token;
4320 next_token = cp_lexer_peek_token (parser->lexer);
4321 if (/* The comma at the end of an
4322 enumerator-definition. */
4323 next_token->type != CPP_COMMA
4324 /* The curly brace at the end of an enum-specifier. */
4325 && next_token->type != CPP_CLOSE_BRACE
4326 /* The end of a statement. */
4327 && next_token->type != CPP_SEMICOLON
4328 /* The end of the cast-expression. */
4329 && next_token->type != CPP_CLOSE_PAREN
4330 /* The end of an array bound. */
4331 && next_token->type != CPP_CLOSE_SQUARE
4332 /* The closing ">" in a template-argument-list. */
4333 && (next_token->type != CPP_GREATER
4334 || parser->greater_than_is_operator_p)
4335 /* C++0x only: A ">>" treated like two ">" tokens,
4336 in a template-argument-list. */
4337 && (next_token->type != CPP_RSHIFT
4338 || (cxx_dialect == cxx98)
4339 || parser->greater_than_is_operator_p))
4340 cast_p = false;
4343 /* If we are within a cast, then the constraint that the
4344 cast is to an integral or enumeration type will be
4345 checked at that point. If we are not within a cast, then
4346 this code is invalid. */
4347 if (!cast_p)
4348 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4350 return token->u.value;
4352 case CPP_CHAR_USERDEF:
4353 case CPP_CHAR16_USERDEF:
4354 case CPP_CHAR32_USERDEF:
4355 case CPP_WCHAR_USERDEF:
4356 return cp_parser_userdef_char_literal (parser);
4358 case CPP_STRING:
4359 case CPP_STRING16:
4360 case CPP_STRING32:
4361 case CPP_WSTRING:
4362 case CPP_UTF8STRING:
4363 case CPP_STRING_USERDEF:
4364 case CPP_STRING16_USERDEF:
4365 case CPP_STRING32_USERDEF:
4366 case CPP_WSTRING_USERDEF:
4367 case CPP_UTF8STRING_USERDEF:
4368 /* ??? Should wide strings be allowed when parser->translate_strings_p
4369 is false (i.e. in attributes)? If not, we can kill the third
4370 argument to cp_parser_string_literal. */
4371 return cp_parser_string_literal (parser,
4372 parser->translate_strings_p,
4373 true);
4375 case CPP_OPEN_PAREN:
4376 /* If we see `( { ' then we are looking at the beginning of
4377 a GNU statement-expression. */
4378 if (cp_parser_allow_gnu_extensions_p (parser)
4379 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4381 /* Statement-expressions are not allowed by the standard. */
4382 pedwarn (token->location, OPT_Wpedantic,
4383 "ISO C++ forbids braced-groups within expressions");
4385 /* And they're not allowed outside of a function-body; you
4386 cannot, for example, write:
4388 int i = ({ int j = 3; j + 1; });
4390 at class or namespace scope. */
4391 if (!parser->in_function_body
4392 || parser->in_template_argument_list_p)
4394 error_at (token->location,
4395 "statement-expressions are not allowed outside "
4396 "functions nor in template-argument lists");
4397 cp_parser_skip_to_end_of_block_or_statement (parser);
4398 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4399 cp_lexer_consume_token (parser->lexer);
4400 return error_mark_node;
4402 else
4403 return cp_parser_statement_expr (parser);
4405 /* Otherwise it's a normal parenthesized expression. */
4407 tree expr;
4408 bool saved_greater_than_is_operator_p;
4410 /* Consume the `('. */
4411 cp_lexer_consume_token (parser->lexer);
4412 /* Within a parenthesized expression, a `>' token is always
4413 the greater-than operator. */
4414 saved_greater_than_is_operator_p
4415 = parser->greater_than_is_operator_p;
4416 parser->greater_than_is_operator_p = true;
4418 /* Parse the parenthesized expression. */
4419 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4420 /* Let the front end know that this expression was
4421 enclosed in parentheses. This matters in case, for
4422 example, the expression is of the form `A::B', since
4423 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4424 not. */
4425 expr = finish_parenthesized_expr (expr);
4426 /* DR 705: Wrapping an unqualified name in parentheses
4427 suppresses arg-dependent lookup. We want to pass back
4428 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4429 (c++/37862), but none of the others. */
4430 if (*idk != CP_ID_KIND_QUALIFIED)
4431 *idk = CP_ID_KIND_NONE;
4433 /* The `>' token might be the end of a template-id or
4434 template-parameter-list now. */
4435 parser->greater_than_is_operator_p
4436 = saved_greater_than_is_operator_p;
4437 /* Consume the `)'. */
4438 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4439 cp_parser_skip_to_end_of_statement (parser);
4441 return expr;
4444 case CPP_OPEN_SQUARE:
4446 if (c_dialect_objc ())
4448 /* We might have an Objective-C++ message. */
4449 cp_parser_parse_tentatively (parser);
4450 tree msg = cp_parser_objc_message_expression (parser);
4451 /* If that works out, we're done ... */
4452 if (cp_parser_parse_definitely (parser))
4453 return msg;
4454 /* ... else, fall though to see if it's a lambda. */
4456 tree lam = cp_parser_lambda_expression (parser);
4457 /* Don't warn about a failed tentative parse. */
4458 if (cp_parser_error_occurred (parser))
4459 return error_mark_node;
4460 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4461 return lam;
4464 case CPP_OBJC_STRING:
4465 if (c_dialect_objc ())
4466 /* We have an Objective-C++ string literal. */
4467 return cp_parser_objc_expression (parser);
4468 cp_parser_error (parser, "expected primary-expression");
4469 return error_mark_node;
4471 case CPP_KEYWORD:
4472 switch (token->keyword)
4474 /* These two are the boolean literals. */
4475 case RID_TRUE:
4476 cp_lexer_consume_token (parser->lexer);
4477 return boolean_true_node;
4478 case RID_FALSE:
4479 cp_lexer_consume_token (parser->lexer);
4480 return boolean_false_node;
4482 /* The `__null' literal. */
4483 case RID_NULL:
4484 cp_lexer_consume_token (parser->lexer);
4485 return null_node;
4487 /* The `nullptr' literal. */
4488 case RID_NULLPTR:
4489 cp_lexer_consume_token (parser->lexer);
4490 return nullptr_node;
4492 /* Recognize the `this' keyword. */
4493 case RID_THIS:
4494 cp_lexer_consume_token (parser->lexer);
4495 if (parser->local_variables_forbidden_p)
4497 error_at (token->location,
4498 "%<this%> may not be used in this context");
4499 return error_mark_node;
4501 /* Pointers cannot appear in constant-expressions. */
4502 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4503 return error_mark_node;
4504 return finish_this_expr ();
4506 /* The `operator' keyword can be the beginning of an
4507 id-expression. */
4508 case RID_OPERATOR:
4509 goto id_expression;
4511 case RID_FUNCTION_NAME:
4512 case RID_PRETTY_FUNCTION_NAME:
4513 case RID_C99_FUNCTION_NAME:
4515 non_integral_constant name;
4517 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4518 __func__ are the names of variables -- but they are
4519 treated specially. Therefore, they are handled here,
4520 rather than relying on the generic id-expression logic
4521 below. Grammatically, these names are id-expressions.
4523 Consume the token. */
4524 token = cp_lexer_consume_token (parser->lexer);
4526 switch (token->keyword)
4528 case RID_FUNCTION_NAME:
4529 name = NIC_FUNC_NAME;
4530 break;
4531 case RID_PRETTY_FUNCTION_NAME:
4532 name = NIC_PRETTY_FUNC;
4533 break;
4534 case RID_C99_FUNCTION_NAME:
4535 name = NIC_C99_FUNC;
4536 break;
4537 default:
4538 gcc_unreachable ();
4541 if (cp_parser_non_integral_constant_expression (parser, name))
4542 return error_mark_node;
4544 /* Look up the name. */
4545 return finish_fname (token->u.value);
4548 case RID_VA_ARG:
4550 tree expression;
4551 tree type;
4552 source_location type_location;
4554 /* The `__builtin_va_arg' construct is used to handle
4555 `va_arg'. Consume the `__builtin_va_arg' token. */
4556 cp_lexer_consume_token (parser->lexer);
4557 /* Look for the opening `('. */
4558 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4559 /* Now, parse the assignment-expression. */
4560 expression = cp_parser_assignment_expression (parser);
4561 /* Look for the `,'. */
4562 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4563 type_location = cp_lexer_peek_token (parser->lexer)->location;
4564 /* Parse the type-id. */
4565 type = cp_parser_type_id (parser);
4566 /* Look for the closing `)'. */
4567 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4568 /* Using `va_arg' in a constant-expression is not
4569 allowed. */
4570 if (cp_parser_non_integral_constant_expression (parser,
4571 NIC_VA_ARG))
4572 return error_mark_node;
4573 return build_x_va_arg (type_location, expression, type);
4576 case RID_OFFSETOF:
4577 return cp_parser_builtin_offsetof (parser);
4579 case RID_HAS_NOTHROW_ASSIGN:
4580 case RID_HAS_NOTHROW_CONSTRUCTOR:
4581 case RID_HAS_NOTHROW_COPY:
4582 case RID_HAS_TRIVIAL_ASSIGN:
4583 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4584 case RID_HAS_TRIVIAL_COPY:
4585 case RID_HAS_TRIVIAL_DESTRUCTOR:
4586 case RID_HAS_VIRTUAL_DESTRUCTOR:
4587 case RID_IS_ABSTRACT:
4588 case RID_IS_BASE_OF:
4589 case RID_IS_CLASS:
4590 case RID_IS_EMPTY:
4591 case RID_IS_ENUM:
4592 case RID_IS_FINAL:
4593 case RID_IS_LITERAL_TYPE:
4594 case RID_IS_POD:
4595 case RID_IS_POLYMORPHIC:
4596 case RID_IS_STD_LAYOUT:
4597 case RID_IS_TRIVIAL:
4598 case RID_IS_TRIVIALLY_ASSIGNABLE:
4599 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4600 case RID_IS_TRIVIALLY_COPYABLE:
4601 case RID_IS_UNION:
4602 return cp_parser_trait_expr (parser, token->keyword);
4604 /* Objective-C++ expressions. */
4605 case RID_AT_ENCODE:
4606 case RID_AT_PROTOCOL:
4607 case RID_AT_SELECTOR:
4608 return cp_parser_objc_expression (parser);
4610 case RID_TEMPLATE:
4611 if (parser->in_function_body
4612 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4613 == CPP_LESS))
4615 error_at (token->location,
4616 "a template declaration cannot appear at block scope");
4617 cp_parser_skip_to_end_of_block_or_statement (parser);
4618 return error_mark_node;
4620 default:
4621 cp_parser_error (parser, "expected primary-expression");
4622 return error_mark_node;
4625 /* An id-expression can start with either an identifier, a
4626 `::' as the beginning of a qualified-id, or the "operator"
4627 keyword. */
4628 case CPP_NAME:
4629 case CPP_SCOPE:
4630 case CPP_TEMPLATE_ID:
4631 case CPP_NESTED_NAME_SPECIFIER:
4633 tree id_expression;
4634 tree decl;
4635 const char *error_msg;
4636 bool template_p;
4637 bool done;
4638 cp_token *id_expr_token;
4640 id_expression:
4641 /* Parse the id-expression. */
4642 id_expression
4643 = cp_parser_id_expression (parser,
4644 /*template_keyword_p=*/false,
4645 /*check_dependency_p=*/true,
4646 &template_p,
4647 /*declarator_p=*/false,
4648 /*optional_p=*/false);
4649 if (id_expression == error_mark_node)
4650 return error_mark_node;
4651 id_expr_token = token;
4652 token = cp_lexer_peek_token (parser->lexer);
4653 done = (token->type != CPP_OPEN_SQUARE
4654 && token->type != CPP_OPEN_PAREN
4655 && token->type != CPP_DOT
4656 && token->type != CPP_DEREF
4657 && token->type != CPP_PLUS_PLUS
4658 && token->type != CPP_MINUS_MINUS);
4659 /* If we have a template-id, then no further lookup is
4660 required. If the template-id was for a template-class, we
4661 will sometimes have a TYPE_DECL at this point. */
4662 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4663 || TREE_CODE (id_expression) == TYPE_DECL)
4664 decl = id_expression;
4665 /* Look up the name. */
4666 else
4668 tree ambiguous_decls;
4670 /* If we already know that this lookup is ambiguous, then
4671 we've already issued an error message; there's no reason
4672 to check again. */
4673 if (id_expr_token->type == CPP_NAME
4674 && id_expr_token->error_reported)
4676 cp_parser_simulate_error (parser);
4677 return error_mark_node;
4680 decl = cp_parser_lookup_name (parser, id_expression,
4681 none_type,
4682 template_p,
4683 /*is_namespace=*/false,
4684 /*check_dependency=*/true,
4685 &ambiguous_decls,
4686 id_expr_token->location);
4687 /* If the lookup was ambiguous, an error will already have
4688 been issued. */
4689 if (ambiguous_decls)
4690 return error_mark_node;
4692 /* In Objective-C++, we may have an Objective-C 2.0
4693 dot-syntax for classes here. */
4694 if (c_dialect_objc ()
4695 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4696 && TREE_CODE (decl) == TYPE_DECL
4697 && objc_is_class_name (decl))
4699 tree component;
4700 cp_lexer_consume_token (parser->lexer);
4701 component = cp_parser_identifier (parser);
4702 if (component == error_mark_node)
4703 return error_mark_node;
4705 return objc_build_class_component_ref (id_expression, component);
4708 /* In Objective-C++, an instance variable (ivar) may be preferred
4709 to whatever cp_parser_lookup_name() found. */
4710 decl = objc_lookup_ivar (decl, id_expression);
4712 /* If name lookup gives us a SCOPE_REF, then the
4713 qualifying scope was dependent. */
4714 if (TREE_CODE (decl) == SCOPE_REF)
4716 /* At this point, we do not know if DECL is a valid
4717 integral constant expression. We assume that it is
4718 in fact such an expression, so that code like:
4720 template <int N> struct A {
4721 int a[B<N>::i];
4724 is accepted. At template-instantiation time, we
4725 will check that B<N>::i is actually a constant. */
4726 return decl;
4728 /* Check to see if DECL is a local variable in a context
4729 where that is forbidden. */
4730 if (parser->local_variables_forbidden_p
4731 && local_variable_p (decl))
4733 /* It might be that we only found DECL because we are
4734 trying to be generous with pre-ISO scoping rules.
4735 For example, consider:
4737 int i;
4738 void g() {
4739 for (int i = 0; i < 10; ++i) {}
4740 extern void f(int j = i);
4743 Here, name look up will originally find the out
4744 of scope `i'. We need to issue a warning message,
4745 but then use the global `i'. */
4746 decl = check_for_out_of_scope_variable (decl);
4747 if (local_variable_p (decl))
4749 error_at (id_expr_token->location,
4750 "local variable %qD may not appear in this context",
4751 decl);
4752 return error_mark_node;
4757 decl = (finish_id_expression
4758 (id_expression, decl, parser->scope,
4759 idk,
4760 parser->integral_constant_expression_p,
4761 parser->allow_non_integral_constant_expression_p,
4762 &parser->non_integral_constant_expression_p,
4763 template_p, done, address_p,
4764 template_arg_p,
4765 &error_msg,
4766 id_expr_token->location));
4767 if (error_msg)
4768 cp_parser_error (parser, error_msg);
4769 return decl;
4772 /* Anything else is an error. */
4773 default:
4774 cp_parser_error (parser, "expected primary-expression");
4775 return error_mark_node;
4779 static inline tree
4780 cp_parser_primary_expression (cp_parser *parser,
4781 bool address_p,
4782 bool cast_p,
4783 bool template_arg_p,
4784 cp_id_kind *idk)
4786 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4787 /*decltype*/false, idk);
4790 /* Parse an id-expression.
4792 id-expression:
4793 unqualified-id
4794 qualified-id
4796 qualified-id:
4797 :: [opt] nested-name-specifier template [opt] unqualified-id
4798 :: identifier
4799 :: operator-function-id
4800 :: template-id
4802 Return a representation of the unqualified portion of the
4803 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4804 a `::' or nested-name-specifier.
4806 Often, if the id-expression was a qualified-id, the caller will
4807 want to make a SCOPE_REF to represent the qualified-id. This
4808 function does not do this in order to avoid wastefully creating
4809 SCOPE_REFs when they are not required.
4811 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4812 `template' keyword.
4814 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4815 uninstantiated templates.
4817 If *TEMPLATE_P is non-NULL, it is set to true iff the
4818 `template' keyword is used to explicitly indicate that the entity
4819 named is a template.
4821 If DECLARATOR_P is true, the id-expression is appearing as part of
4822 a declarator, rather than as part of an expression. */
4824 static tree
4825 cp_parser_id_expression (cp_parser *parser,
4826 bool template_keyword_p,
4827 bool check_dependency_p,
4828 bool *template_p,
4829 bool declarator_p,
4830 bool optional_p)
4832 bool global_scope_p;
4833 bool nested_name_specifier_p;
4835 /* Assume the `template' keyword was not used. */
4836 if (template_p)
4837 *template_p = template_keyword_p;
4839 /* Look for the optional `::' operator. */
4840 global_scope_p
4841 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4842 != NULL_TREE);
4843 /* Look for the optional nested-name-specifier. */
4844 nested_name_specifier_p
4845 = (cp_parser_nested_name_specifier_opt (parser,
4846 /*typename_keyword_p=*/false,
4847 check_dependency_p,
4848 /*type_p=*/false,
4849 declarator_p)
4850 != NULL_TREE);
4851 /* If there is a nested-name-specifier, then we are looking at
4852 the first qualified-id production. */
4853 if (nested_name_specifier_p)
4855 tree saved_scope;
4856 tree saved_object_scope;
4857 tree saved_qualifying_scope;
4858 tree unqualified_id;
4859 bool is_template;
4861 /* See if the next token is the `template' keyword. */
4862 if (!template_p)
4863 template_p = &is_template;
4864 *template_p = cp_parser_optional_template_keyword (parser);
4865 /* Name lookup we do during the processing of the
4866 unqualified-id might obliterate SCOPE. */
4867 saved_scope = parser->scope;
4868 saved_object_scope = parser->object_scope;
4869 saved_qualifying_scope = parser->qualifying_scope;
4870 /* Process the final unqualified-id. */
4871 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4872 check_dependency_p,
4873 declarator_p,
4874 /*optional_p=*/false);
4875 /* Restore the SAVED_SCOPE for our caller. */
4876 parser->scope = saved_scope;
4877 parser->object_scope = saved_object_scope;
4878 parser->qualifying_scope = saved_qualifying_scope;
4880 return unqualified_id;
4882 /* Otherwise, if we are in global scope, then we are looking at one
4883 of the other qualified-id productions. */
4884 else if (global_scope_p)
4886 cp_token *token;
4887 tree id;
4889 /* Peek at the next token. */
4890 token = cp_lexer_peek_token (parser->lexer);
4892 /* If it's an identifier, and the next token is not a "<", then
4893 we can avoid the template-id case. This is an optimization
4894 for this common case. */
4895 if (token->type == CPP_NAME
4896 && !cp_parser_nth_token_starts_template_argument_list_p
4897 (parser, 2))
4898 return cp_parser_identifier (parser);
4900 cp_parser_parse_tentatively (parser);
4901 /* Try a template-id. */
4902 id = cp_parser_template_id (parser,
4903 /*template_keyword_p=*/false,
4904 /*check_dependency_p=*/true,
4905 none_type,
4906 declarator_p);
4907 /* If that worked, we're done. */
4908 if (cp_parser_parse_definitely (parser))
4909 return id;
4911 /* Peek at the next token. (Changes in the token buffer may
4912 have invalidated the pointer obtained above.) */
4913 token = cp_lexer_peek_token (parser->lexer);
4915 switch (token->type)
4917 case CPP_NAME:
4918 return cp_parser_identifier (parser);
4920 case CPP_KEYWORD:
4921 if (token->keyword == RID_OPERATOR)
4922 return cp_parser_operator_function_id (parser);
4923 /* Fall through. */
4925 default:
4926 cp_parser_error (parser, "expected id-expression");
4927 return error_mark_node;
4930 else
4931 return cp_parser_unqualified_id (parser, template_keyword_p,
4932 /*check_dependency_p=*/true,
4933 declarator_p,
4934 optional_p);
4937 /* Parse an unqualified-id.
4939 unqualified-id:
4940 identifier
4941 operator-function-id
4942 conversion-function-id
4943 ~ class-name
4944 template-id
4946 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4947 keyword, in a construct like `A::template ...'.
4949 Returns a representation of unqualified-id. For the `identifier'
4950 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4951 production a BIT_NOT_EXPR is returned; the operand of the
4952 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4953 other productions, see the documentation accompanying the
4954 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4955 names are looked up in uninstantiated templates. If DECLARATOR_P
4956 is true, the unqualified-id is appearing as part of a declarator,
4957 rather than as part of an expression. */
4959 static tree
4960 cp_parser_unqualified_id (cp_parser* parser,
4961 bool template_keyword_p,
4962 bool check_dependency_p,
4963 bool declarator_p,
4964 bool optional_p)
4966 cp_token *token;
4968 /* Peek at the next token. */
4969 token = cp_lexer_peek_token (parser->lexer);
4971 switch ((int) token->type)
4973 case CPP_NAME:
4975 tree id;
4977 /* We don't know yet whether or not this will be a
4978 template-id. */
4979 cp_parser_parse_tentatively (parser);
4980 /* Try a template-id. */
4981 id = cp_parser_template_id (parser, template_keyword_p,
4982 check_dependency_p,
4983 none_type,
4984 declarator_p);
4985 /* If it worked, we're done. */
4986 if (cp_parser_parse_definitely (parser))
4987 return id;
4988 /* Otherwise, it's an ordinary identifier. */
4989 return cp_parser_identifier (parser);
4992 case CPP_TEMPLATE_ID:
4993 return cp_parser_template_id (parser, template_keyword_p,
4994 check_dependency_p,
4995 none_type,
4996 declarator_p);
4998 case CPP_COMPL:
5000 tree type_decl;
5001 tree qualifying_scope;
5002 tree object_scope;
5003 tree scope;
5004 bool done;
5006 /* Consume the `~' token. */
5007 cp_lexer_consume_token (parser->lexer);
5008 /* Parse the class-name. The standard, as written, seems to
5009 say that:
5011 template <typename T> struct S { ~S (); };
5012 template <typename T> S<T>::~S() {}
5014 is invalid, since `~' must be followed by a class-name, but
5015 `S<T>' is dependent, and so not known to be a class.
5016 That's not right; we need to look in uninstantiated
5017 templates. A further complication arises from:
5019 template <typename T> void f(T t) {
5020 t.T::~T();
5023 Here, it is not possible to look up `T' in the scope of `T'
5024 itself. We must look in both the current scope, and the
5025 scope of the containing complete expression.
5027 Yet another issue is:
5029 struct S {
5030 int S;
5031 ~S();
5034 S::~S() {}
5036 The standard does not seem to say that the `S' in `~S'
5037 should refer to the type `S' and not the data member
5038 `S::S'. */
5040 /* DR 244 says that we look up the name after the "~" in the
5041 same scope as we looked up the qualifying name. That idea
5042 isn't fully worked out; it's more complicated than that. */
5043 scope = parser->scope;
5044 object_scope = parser->object_scope;
5045 qualifying_scope = parser->qualifying_scope;
5047 /* Check for invalid scopes. */
5048 if (scope == error_mark_node)
5050 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5051 cp_lexer_consume_token (parser->lexer);
5052 return error_mark_node;
5054 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5056 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5057 error_at (token->location,
5058 "scope %qT before %<~%> is not a class-name",
5059 scope);
5060 cp_parser_simulate_error (parser);
5061 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5062 cp_lexer_consume_token (parser->lexer);
5063 return error_mark_node;
5065 gcc_assert (!scope || TYPE_P (scope));
5067 /* If the name is of the form "X::~X" it's OK even if X is a
5068 typedef. */
5069 token = cp_lexer_peek_token (parser->lexer);
5070 if (scope
5071 && token->type == CPP_NAME
5072 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5073 != CPP_LESS)
5074 && (token->u.value == TYPE_IDENTIFIER (scope)
5075 || (CLASS_TYPE_P (scope)
5076 && constructor_name_p (token->u.value, scope))))
5078 cp_lexer_consume_token (parser->lexer);
5079 return build_nt (BIT_NOT_EXPR, scope);
5082 /* ~auto means the destructor of whatever the object is. */
5083 if (cp_parser_is_keyword (token, RID_AUTO))
5085 if (cxx_dialect < cxx14)
5086 pedwarn (input_location, 0,
5087 "%<~auto%> only available with "
5088 "-std=c++14 or -std=gnu++14");
5089 cp_lexer_consume_token (parser->lexer);
5090 return build_nt (BIT_NOT_EXPR, make_auto ());
5093 /* If there was an explicit qualification (S::~T), first look
5094 in the scope given by the qualification (i.e., S).
5096 Note: in the calls to cp_parser_class_name below we pass
5097 typename_type so that lookup finds the injected-class-name
5098 rather than the constructor. */
5099 done = false;
5100 type_decl = NULL_TREE;
5101 if (scope)
5103 cp_parser_parse_tentatively (parser);
5104 type_decl = cp_parser_class_name (parser,
5105 /*typename_keyword_p=*/false,
5106 /*template_keyword_p=*/false,
5107 typename_type,
5108 /*check_dependency=*/false,
5109 /*class_head_p=*/false,
5110 declarator_p);
5111 if (cp_parser_parse_definitely (parser))
5112 done = true;
5114 /* In "N::S::~S", look in "N" as well. */
5115 if (!done && scope && qualifying_scope)
5117 cp_parser_parse_tentatively (parser);
5118 parser->scope = qualifying_scope;
5119 parser->object_scope = NULL_TREE;
5120 parser->qualifying_scope = NULL_TREE;
5121 type_decl
5122 = cp_parser_class_name (parser,
5123 /*typename_keyword_p=*/false,
5124 /*template_keyword_p=*/false,
5125 typename_type,
5126 /*check_dependency=*/false,
5127 /*class_head_p=*/false,
5128 declarator_p);
5129 if (cp_parser_parse_definitely (parser))
5130 done = true;
5132 /* In "p->S::~T", look in the scope given by "*p" as well. */
5133 else if (!done && object_scope)
5135 cp_parser_parse_tentatively (parser);
5136 parser->scope = object_scope;
5137 parser->object_scope = NULL_TREE;
5138 parser->qualifying_scope = NULL_TREE;
5139 type_decl
5140 = cp_parser_class_name (parser,
5141 /*typename_keyword_p=*/false,
5142 /*template_keyword_p=*/false,
5143 typename_type,
5144 /*check_dependency=*/false,
5145 /*class_head_p=*/false,
5146 declarator_p);
5147 if (cp_parser_parse_definitely (parser))
5148 done = true;
5150 /* Look in the surrounding context. */
5151 if (!done)
5153 parser->scope = NULL_TREE;
5154 parser->object_scope = NULL_TREE;
5155 parser->qualifying_scope = NULL_TREE;
5156 if (processing_template_decl)
5157 cp_parser_parse_tentatively (parser);
5158 type_decl
5159 = cp_parser_class_name (parser,
5160 /*typename_keyword_p=*/false,
5161 /*template_keyword_p=*/false,
5162 typename_type,
5163 /*check_dependency=*/false,
5164 /*class_head_p=*/false,
5165 declarator_p);
5166 if (processing_template_decl
5167 && ! cp_parser_parse_definitely (parser))
5169 /* We couldn't find a type with this name, so just accept
5170 it and check for a match at instantiation time. */
5171 type_decl = cp_parser_identifier (parser);
5172 if (type_decl != error_mark_node)
5173 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5174 return type_decl;
5177 /* If an error occurred, assume that the name of the
5178 destructor is the same as the name of the qualifying
5179 class. That allows us to keep parsing after running
5180 into ill-formed destructor names. */
5181 if (type_decl == error_mark_node && scope)
5182 return build_nt (BIT_NOT_EXPR, scope);
5183 else if (type_decl == error_mark_node)
5184 return error_mark_node;
5186 /* Check that destructor name and scope match. */
5187 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5189 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5190 error_at (token->location,
5191 "declaration of %<~%T%> as member of %qT",
5192 type_decl, scope);
5193 cp_parser_simulate_error (parser);
5194 return error_mark_node;
5197 /* [class.dtor]
5199 A typedef-name that names a class shall not be used as the
5200 identifier in the declarator for a destructor declaration. */
5201 if (declarator_p
5202 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5203 && !DECL_SELF_REFERENCE_P (type_decl)
5204 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5205 error_at (token->location,
5206 "typedef-name %qD used as destructor declarator",
5207 type_decl);
5209 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5212 case CPP_KEYWORD:
5213 if (token->keyword == RID_OPERATOR)
5215 tree id;
5217 /* This could be a template-id, so we try that first. */
5218 cp_parser_parse_tentatively (parser);
5219 /* Try a template-id. */
5220 id = cp_parser_template_id (parser, template_keyword_p,
5221 /*check_dependency_p=*/true,
5222 none_type,
5223 declarator_p);
5224 /* If that worked, we're done. */
5225 if (cp_parser_parse_definitely (parser))
5226 return id;
5227 /* We still don't know whether we're looking at an
5228 operator-function-id or a conversion-function-id. */
5229 cp_parser_parse_tentatively (parser);
5230 /* Try an operator-function-id. */
5231 id = cp_parser_operator_function_id (parser);
5232 /* If that didn't work, try a conversion-function-id. */
5233 if (!cp_parser_parse_definitely (parser))
5234 id = cp_parser_conversion_function_id (parser);
5235 else if (UDLIT_OPER_P (id))
5237 /* 17.6.3.3.5 */
5238 const char *name = UDLIT_OP_SUFFIX (id);
5239 if (name[0] != '_' && !in_system_header_at (input_location)
5240 && declarator_p)
5241 warning (0, "literal operator suffixes not preceded by %<_%>"
5242 " are reserved for future standardization");
5245 return id;
5247 /* Fall through. */
5249 default:
5250 if (optional_p)
5251 return NULL_TREE;
5252 cp_parser_error (parser, "expected unqualified-id");
5253 return error_mark_node;
5257 /* Parse an (optional) nested-name-specifier.
5259 nested-name-specifier: [C++98]
5260 class-or-namespace-name :: nested-name-specifier [opt]
5261 class-or-namespace-name :: template nested-name-specifier [opt]
5263 nested-name-specifier: [C++0x]
5264 type-name ::
5265 namespace-name ::
5266 nested-name-specifier identifier ::
5267 nested-name-specifier template [opt] simple-template-id ::
5269 PARSER->SCOPE should be set appropriately before this function is
5270 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5271 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5272 in name lookups.
5274 Sets PARSER->SCOPE to the class (TYPE) or namespace
5275 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5276 it unchanged if there is no nested-name-specifier. Returns the new
5277 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5279 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5280 part of a declaration and/or decl-specifier. */
5282 static tree
5283 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5284 bool typename_keyword_p,
5285 bool check_dependency_p,
5286 bool type_p,
5287 bool is_declaration)
5289 bool success = false;
5290 cp_token_position start = 0;
5291 cp_token *token;
5293 /* Remember where the nested-name-specifier starts. */
5294 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5296 start = cp_lexer_token_position (parser->lexer, false);
5297 push_deferring_access_checks (dk_deferred);
5300 while (true)
5302 tree new_scope;
5303 tree old_scope;
5304 tree saved_qualifying_scope;
5305 bool template_keyword_p;
5307 /* Spot cases that cannot be the beginning of a
5308 nested-name-specifier. */
5309 token = cp_lexer_peek_token (parser->lexer);
5311 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5312 the already parsed nested-name-specifier. */
5313 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5315 /* Grab the nested-name-specifier and continue the loop. */
5316 cp_parser_pre_parsed_nested_name_specifier (parser);
5317 /* If we originally encountered this nested-name-specifier
5318 with IS_DECLARATION set to false, we will not have
5319 resolved TYPENAME_TYPEs, so we must do so here. */
5320 if (is_declaration
5321 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5323 new_scope = resolve_typename_type (parser->scope,
5324 /*only_current_p=*/false);
5325 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5326 parser->scope = new_scope;
5328 success = true;
5329 continue;
5332 /* Spot cases that cannot be the beginning of a
5333 nested-name-specifier. On the second and subsequent times
5334 through the loop, we look for the `template' keyword. */
5335 if (success && token->keyword == RID_TEMPLATE)
5337 /* A template-id can start a nested-name-specifier. */
5338 else if (token->type == CPP_TEMPLATE_ID)
5340 /* DR 743: decltype can be used in a nested-name-specifier. */
5341 else if (token_is_decltype (token))
5343 else
5345 /* If the next token is not an identifier, then it is
5346 definitely not a type-name or namespace-name. */
5347 if (token->type != CPP_NAME)
5348 break;
5349 /* If the following token is neither a `<' (to begin a
5350 template-id), nor a `::', then we are not looking at a
5351 nested-name-specifier. */
5352 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5354 if (token->type == CPP_COLON
5355 && parser->colon_corrects_to_scope_p
5356 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5358 error_at (token->location,
5359 "found %<:%> in nested-name-specifier, expected %<::%>");
5360 token->type = CPP_SCOPE;
5363 if (token->type != CPP_SCOPE
5364 && !cp_parser_nth_token_starts_template_argument_list_p
5365 (parser, 2))
5366 break;
5369 /* The nested-name-specifier is optional, so we parse
5370 tentatively. */
5371 cp_parser_parse_tentatively (parser);
5373 /* Look for the optional `template' keyword, if this isn't the
5374 first time through the loop. */
5375 if (success)
5376 template_keyword_p = cp_parser_optional_template_keyword (parser);
5377 else
5378 template_keyword_p = false;
5380 /* Save the old scope since the name lookup we are about to do
5381 might destroy it. */
5382 old_scope = parser->scope;
5383 saved_qualifying_scope = parser->qualifying_scope;
5384 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5385 look up names in "X<T>::I" in order to determine that "Y" is
5386 a template. So, if we have a typename at this point, we make
5387 an effort to look through it. */
5388 if (is_declaration
5389 && !typename_keyword_p
5390 && parser->scope
5391 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5392 parser->scope = resolve_typename_type (parser->scope,
5393 /*only_current_p=*/false);
5394 /* Parse the qualifying entity. */
5395 new_scope
5396 = cp_parser_qualifying_entity (parser,
5397 typename_keyword_p,
5398 template_keyword_p,
5399 check_dependency_p,
5400 type_p,
5401 is_declaration);
5402 /* Look for the `::' token. */
5403 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5405 /* If we found what we wanted, we keep going; otherwise, we're
5406 done. */
5407 if (!cp_parser_parse_definitely (parser))
5409 bool error_p = false;
5411 /* Restore the OLD_SCOPE since it was valid before the
5412 failed attempt at finding the last
5413 class-or-namespace-name. */
5414 parser->scope = old_scope;
5415 parser->qualifying_scope = saved_qualifying_scope;
5417 /* If the next token is a decltype, and the one after that is a
5418 `::', then the decltype has failed to resolve to a class or
5419 enumeration type. Give this error even when parsing
5420 tentatively since it can't possibly be valid--and we're going
5421 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5422 won't get another chance.*/
5423 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5424 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5425 == CPP_SCOPE))
5427 token = cp_lexer_consume_token (parser->lexer);
5428 error_at (token->location, "decltype evaluates to %qT, "
5429 "which is not a class or enumeration type",
5430 token->u.value);
5431 parser->scope = error_mark_node;
5432 error_p = true;
5433 /* As below. */
5434 success = true;
5435 cp_lexer_consume_token (parser->lexer);
5438 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5439 break;
5440 /* If the next token is an identifier, and the one after
5441 that is a `::', then any valid interpretation would have
5442 found a class-or-namespace-name. */
5443 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5444 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5445 == CPP_SCOPE)
5446 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5447 != CPP_COMPL))
5449 token = cp_lexer_consume_token (parser->lexer);
5450 if (!error_p)
5452 if (!token->error_reported)
5454 tree decl;
5455 tree ambiguous_decls;
5457 decl = cp_parser_lookup_name (parser, token->u.value,
5458 none_type,
5459 /*is_template=*/false,
5460 /*is_namespace=*/false,
5461 /*check_dependency=*/true,
5462 &ambiguous_decls,
5463 token->location);
5464 if (TREE_CODE (decl) == TEMPLATE_DECL)
5465 error_at (token->location,
5466 "%qD used without template parameters",
5467 decl);
5468 else if (ambiguous_decls)
5470 // cp_parser_lookup_name has the same diagnostic,
5471 // thus make sure to emit it at most once.
5472 if (cp_parser_uncommitted_to_tentative_parse_p
5473 (parser))
5475 error_at (token->location,
5476 "reference to %qD is ambiguous",
5477 token->u.value);
5478 print_candidates (ambiguous_decls);
5480 decl = error_mark_node;
5482 else
5484 if (cxx_dialect != cxx98)
5485 cp_parser_name_lookup_error
5486 (parser, token->u.value, decl, NLE_NOT_CXX98,
5487 token->location);
5488 else
5489 cp_parser_name_lookup_error
5490 (parser, token->u.value, decl, NLE_CXX98,
5491 token->location);
5494 parser->scope = error_mark_node;
5495 error_p = true;
5496 /* Treat this as a successful nested-name-specifier
5497 due to:
5499 [basic.lookup.qual]
5501 If the name found is not a class-name (clause
5502 _class_) or namespace-name (_namespace.def_), the
5503 program is ill-formed. */
5504 success = true;
5506 cp_lexer_consume_token (parser->lexer);
5508 break;
5510 /* We've found one valid nested-name-specifier. */
5511 success = true;
5512 /* Name lookup always gives us a DECL. */
5513 if (TREE_CODE (new_scope) == TYPE_DECL)
5514 new_scope = TREE_TYPE (new_scope);
5515 /* Uses of "template" must be followed by actual templates. */
5516 if (template_keyword_p
5517 && !(CLASS_TYPE_P (new_scope)
5518 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5519 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5520 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5521 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5522 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5523 == TEMPLATE_ID_EXPR)))
5524 permerror (input_location, TYPE_P (new_scope)
5525 ? G_("%qT is not a template")
5526 : G_("%qD is not a template"),
5527 new_scope);
5528 /* If it is a class scope, try to complete it; we are about to
5529 be looking up names inside the class. */
5530 if (TYPE_P (new_scope)
5531 /* Since checking types for dependency can be expensive,
5532 avoid doing it if the type is already complete. */
5533 && !COMPLETE_TYPE_P (new_scope)
5534 /* Do not try to complete dependent types. */
5535 && !dependent_type_p (new_scope))
5537 new_scope = complete_type (new_scope);
5538 /* If it is a typedef to current class, use the current
5539 class instead, as the typedef won't have any names inside
5540 it yet. */
5541 if (!COMPLETE_TYPE_P (new_scope)
5542 && currently_open_class (new_scope))
5543 new_scope = TYPE_MAIN_VARIANT (new_scope);
5545 /* Make sure we look in the right scope the next time through
5546 the loop. */
5547 parser->scope = new_scope;
5550 /* If parsing tentatively, replace the sequence of tokens that makes
5551 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5552 token. That way, should we re-parse the token stream, we will
5553 not have to repeat the effort required to do the parse, nor will
5554 we issue duplicate error messages. */
5555 if (success && start)
5557 cp_token *token;
5559 token = cp_lexer_token_at (parser->lexer, start);
5560 /* Reset the contents of the START token. */
5561 token->type = CPP_NESTED_NAME_SPECIFIER;
5562 /* Retrieve any deferred checks. Do not pop this access checks yet
5563 so the memory will not be reclaimed during token replacing below. */
5564 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5565 token->u.tree_check_value->value = parser->scope;
5566 token->u.tree_check_value->checks = get_deferred_access_checks ();
5567 token->u.tree_check_value->qualifying_scope =
5568 parser->qualifying_scope;
5569 token->keyword = RID_MAX;
5571 /* Purge all subsequent tokens. */
5572 cp_lexer_purge_tokens_after (parser->lexer, start);
5575 if (start)
5576 pop_to_parent_deferring_access_checks ();
5578 return success ? parser->scope : NULL_TREE;
5581 /* Parse a nested-name-specifier. See
5582 cp_parser_nested_name_specifier_opt for details. This function
5583 behaves identically, except that it will an issue an error if no
5584 nested-name-specifier is present. */
5586 static tree
5587 cp_parser_nested_name_specifier (cp_parser *parser,
5588 bool typename_keyword_p,
5589 bool check_dependency_p,
5590 bool type_p,
5591 bool is_declaration)
5593 tree scope;
5595 /* Look for the nested-name-specifier. */
5596 scope = cp_parser_nested_name_specifier_opt (parser,
5597 typename_keyword_p,
5598 check_dependency_p,
5599 type_p,
5600 is_declaration);
5601 /* If it was not present, issue an error message. */
5602 if (!scope)
5604 cp_parser_error (parser, "expected nested-name-specifier");
5605 parser->scope = NULL_TREE;
5608 return scope;
5611 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5612 this is either a class-name or a namespace-name (which corresponds
5613 to the class-or-namespace-name production in the grammar). For
5614 C++0x, it can also be a type-name that refers to an enumeration
5615 type or a simple-template-id.
5617 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5618 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5619 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5620 TYPE_P is TRUE iff the next name should be taken as a class-name,
5621 even the same name is declared to be another entity in the same
5622 scope.
5624 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5625 specified by the class-or-namespace-name. If neither is found the
5626 ERROR_MARK_NODE is returned. */
5628 static tree
5629 cp_parser_qualifying_entity (cp_parser *parser,
5630 bool typename_keyword_p,
5631 bool template_keyword_p,
5632 bool check_dependency_p,
5633 bool type_p,
5634 bool is_declaration)
5636 tree saved_scope;
5637 tree saved_qualifying_scope;
5638 tree saved_object_scope;
5639 tree scope;
5640 bool only_class_p;
5641 bool successful_parse_p;
5643 /* DR 743: decltype can appear in a nested-name-specifier. */
5644 if (cp_lexer_next_token_is_decltype (parser->lexer))
5646 scope = cp_parser_decltype (parser);
5647 if (TREE_CODE (scope) != ENUMERAL_TYPE
5648 && !MAYBE_CLASS_TYPE_P (scope))
5650 cp_parser_simulate_error (parser);
5651 return error_mark_node;
5653 if (TYPE_NAME (scope))
5654 scope = TYPE_NAME (scope);
5655 return scope;
5658 /* Before we try to parse the class-name, we must save away the
5659 current PARSER->SCOPE since cp_parser_class_name will destroy
5660 it. */
5661 saved_scope = parser->scope;
5662 saved_qualifying_scope = parser->qualifying_scope;
5663 saved_object_scope = parser->object_scope;
5664 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5665 there is no need to look for a namespace-name. */
5666 only_class_p = template_keyword_p
5667 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5668 if (!only_class_p)
5669 cp_parser_parse_tentatively (parser);
5670 scope = cp_parser_class_name (parser,
5671 typename_keyword_p,
5672 template_keyword_p,
5673 type_p ? class_type : none_type,
5674 check_dependency_p,
5675 /*class_head_p=*/false,
5676 is_declaration);
5677 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5678 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5679 if (!only_class_p
5680 && cxx_dialect != cxx98
5681 && !successful_parse_p)
5683 /* Restore the saved scope. */
5684 parser->scope = saved_scope;
5685 parser->qualifying_scope = saved_qualifying_scope;
5686 parser->object_scope = saved_object_scope;
5688 /* Parse tentatively. */
5689 cp_parser_parse_tentatively (parser);
5691 /* Parse a type-name */
5692 scope = cp_parser_type_name (parser);
5694 /* "If the name found does not designate a namespace or a class,
5695 enumeration, or dependent type, the program is ill-formed."
5697 We cover classes and dependent types above and namespaces below,
5698 so this code is only looking for enums. */
5699 if (!scope || TREE_CODE (scope) != TYPE_DECL
5700 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5701 cp_parser_simulate_error (parser);
5703 successful_parse_p = cp_parser_parse_definitely (parser);
5705 /* If that didn't work, try for a namespace-name. */
5706 if (!only_class_p && !successful_parse_p)
5708 /* Restore the saved scope. */
5709 parser->scope = saved_scope;
5710 parser->qualifying_scope = saved_qualifying_scope;
5711 parser->object_scope = saved_object_scope;
5712 /* If we are not looking at an identifier followed by the scope
5713 resolution operator, then this is not part of a
5714 nested-name-specifier. (Note that this function is only used
5715 to parse the components of a nested-name-specifier.) */
5716 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5717 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5718 return error_mark_node;
5719 scope = cp_parser_namespace_name (parser);
5722 return scope;
5725 /* Return true if we are looking at a compound-literal, false otherwise. */
5727 static bool
5728 cp_parser_compound_literal_p (cp_parser *parser)
5730 /* Consume the `('. */
5731 cp_lexer_consume_token (parser->lexer);
5733 cp_lexer_save_tokens (parser->lexer);
5735 /* Skip tokens until the next token is a closing parenthesis.
5736 If we find the closing `)', and the next token is a `{', then
5737 we are looking at a compound-literal. */
5738 bool compound_literal_p
5739 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5740 /*consume_paren=*/true)
5741 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5743 /* Roll back the tokens we skipped. */
5744 cp_lexer_rollback_tokens (parser->lexer);
5746 return compound_literal_p;
5749 /* Parse a postfix-expression.
5751 postfix-expression:
5752 primary-expression
5753 postfix-expression [ expression ]
5754 postfix-expression ( expression-list [opt] )
5755 simple-type-specifier ( expression-list [opt] )
5756 typename :: [opt] nested-name-specifier identifier
5757 ( expression-list [opt] )
5758 typename :: [opt] nested-name-specifier template [opt] template-id
5759 ( expression-list [opt] )
5760 postfix-expression . template [opt] id-expression
5761 postfix-expression -> template [opt] id-expression
5762 postfix-expression . pseudo-destructor-name
5763 postfix-expression -> pseudo-destructor-name
5764 postfix-expression ++
5765 postfix-expression --
5766 dynamic_cast < type-id > ( expression )
5767 static_cast < type-id > ( expression )
5768 reinterpret_cast < type-id > ( expression )
5769 const_cast < type-id > ( expression )
5770 typeid ( expression )
5771 typeid ( type-id )
5773 GNU Extension:
5775 postfix-expression:
5776 ( type-id ) { initializer-list , [opt] }
5778 This extension is a GNU version of the C99 compound-literal
5779 construct. (The C99 grammar uses `type-name' instead of `type-id',
5780 but they are essentially the same concept.)
5782 If ADDRESS_P is true, the postfix expression is the operand of the
5783 `&' operator. CAST_P is true if this expression is the target of a
5784 cast.
5786 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5787 class member access expressions [expr.ref].
5789 Returns a representation of the expression. */
5791 static tree
5792 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5793 bool member_access_only_p, bool decltype_p,
5794 cp_id_kind * pidk_return)
5796 cp_token *token;
5797 location_t loc;
5798 enum rid keyword;
5799 cp_id_kind idk = CP_ID_KIND_NONE;
5800 tree postfix_expression = NULL_TREE;
5801 bool is_member_access = false;
5802 int saved_in_statement = -1;
5804 /* Peek at the next token. */
5805 token = cp_lexer_peek_token (parser->lexer);
5806 loc = token->location;
5807 /* Some of the productions are determined by keywords. */
5808 keyword = token->keyword;
5809 switch (keyword)
5811 case RID_DYNCAST:
5812 case RID_STATCAST:
5813 case RID_REINTCAST:
5814 case RID_CONSTCAST:
5816 tree type;
5817 tree expression;
5818 const char *saved_message;
5819 bool saved_in_type_id_in_expr_p;
5821 /* All of these can be handled in the same way from the point
5822 of view of parsing. Begin by consuming the token
5823 identifying the cast. */
5824 cp_lexer_consume_token (parser->lexer);
5826 /* New types cannot be defined in the cast. */
5827 saved_message = parser->type_definition_forbidden_message;
5828 parser->type_definition_forbidden_message
5829 = G_("types may not be defined in casts");
5831 /* Look for the opening `<'. */
5832 cp_parser_require (parser, CPP_LESS, RT_LESS);
5833 /* Parse the type to which we are casting. */
5834 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5835 parser->in_type_id_in_expr_p = true;
5836 type = cp_parser_type_id (parser);
5837 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5838 /* Look for the closing `>'. */
5839 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5840 /* Restore the old message. */
5841 parser->type_definition_forbidden_message = saved_message;
5843 bool saved_greater_than_is_operator_p
5844 = parser->greater_than_is_operator_p;
5845 parser->greater_than_is_operator_p = true;
5847 /* And the expression which is being cast. */
5848 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5849 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5850 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5852 parser->greater_than_is_operator_p
5853 = saved_greater_than_is_operator_p;
5855 /* Only type conversions to integral or enumeration types
5856 can be used in constant-expressions. */
5857 if (!cast_valid_in_integral_constant_expression_p (type)
5858 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5859 return error_mark_node;
5861 switch (keyword)
5863 case RID_DYNCAST:
5864 postfix_expression
5865 = build_dynamic_cast (type, expression, tf_warning_or_error);
5866 break;
5867 case RID_STATCAST:
5868 postfix_expression
5869 = build_static_cast (type, expression, tf_warning_or_error);
5870 break;
5871 case RID_REINTCAST:
5872 postfix_expression
5873 = build_reinterpret_cast (type, expression,
5874 tf_warning_or_error);
5875 break;
5876 case RID_CONSTCAST:
5877 postfix_expression
5878 = build_const_cast (type, expression, tf_warning_or_error);
5879 break;
5880 default:
5881 gcc_unreachable ();
5884 break;
5886 case RID_TYPEID:
5888 tree type;
5889 const char *saved_message;
5890 bool saved_in_type_id_in_expr_p;
5892 /* Consume the `typeid' token. */
5893 cp_lexer_consume_token (parser->lexer);
5894 /* Look for the `(' token. */
5895 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5896 /* Types cannot be defined in a `typeid' expression. */
5897 saved_message = parser->type_definition_forbidden_message;
5898 parser->type_definition_forbidden_message
5899 = G_("types may not be defined in a %<typeid%> expression");
5900 /* We can't be sure yet whether we're looking at a type-id or an
5901 expression. */
5902 cp_parser_parse_tentatively (parser);
5903 /* Try a type-id first. */
5904 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5905 parser->in_type_id_in_expr_p = true;
5906 type = cp_parser_type_id (parser);
5907 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5908 /* Look for the `)' token. Otherwise, we can't be sure that
5909 we're not looking at an expression: consider `typeid (int
5910 (3))', for example. */
5911 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5912 /* If all went well, simply lookup the type-id. */
5913 if (cp_parser_parse_definitely (parser))
5914 postfix_expression = get_typeid (type, tf_warning_or_error);
5915 /* Otherwise, fall back to the expression variant. */
5916 else
5918 tree expression;
5920 /* Look for an expression. */
5921 expression = cp_parser_expression (parser, & idk);
5922 /* Compute its typeid. */
5923 postfix_expression = build_typeid (expression, tf_warning_or_error);
5924 /* Look for the `)' token. */
5925 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5927 /* Restore the saved message. */
5928 parser->type_definition_forbidden_message = saved_message;
5929 /* `typeid' may not appear in an integral constant expression. */
5930 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5931 return error_mark_node;
5933 break;
5935 case RID_TYPENAME:
5937 tree type;
5938 /* The syntax permitted here is the same permitted for an
5939 elaborated-type-specifier. */
5940 type = cp_parser_elaborated_type_specifier (parser,
5941 /*is_friend=*/false,
5942 /*is_declaration=*/false);
5943 postfix_expression = cp_parser_functional_cast (parser, type);
5945 break;
5947 case RID_CILK_SPAWN:
5949 cp_lexer_consume_token (parser->lexer);
5950 token = cp_lexer_peek_token (parser->lexer);
5951 if (token->type == CPP_SEMICOLON)
5953 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5954 "an expression");
5955 postfix_expression = error_mark_node;
5956 break;
5958 else if (!current_function_decl)
5960 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5961 "inside a function");
5962 postfix_expression = error_mark_node;
5963 break;
5965 else
5967 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5968 saved_in_statement = parser->in_statement;
5969 parser->in_statement |= IN_CILK_SPAWN;
5971 cfun->calls_cilk_spawn = 1;
5972 postfix_expression =
5973 cp_parser_postfix_expression (parser, false, false,
5974 false, false, &idk);
5975 if (!flag_cilkplus)
5977 error_at (token->location, "-fcilkplus must be enabled to use"
5978 " %<_Cilk_spawn%>");
5979 cfun->calls_cilk_spawn = 0;
5981 else if (saved_in_statement & IN_CILK_SPAWN)
5983 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5984 "are not permitted");
5985 postfix_expression = error_mark_node;
5986 cfun->calls_cilk_spawn = 0;
5988 else
5990 postfix_expression = build_cilk_spawn (token->location,
5991 postfix_expression);
5992 if (postfix_expression != error_mark_node)
5993 SET_EXPR_LOCATION (postfix_expression, input_location);
5994 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5996 break;
5999 case RID_BUILTIN_SHUFFLE:
6001 vec<tree, va_gc> *vec;
6002 unsigned int i;
6003 tree p;
6005 cp_lexer_consume_token (parser->lexer);
6006 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6007 /*cast_p=*/false, /*allow_expansion_p=*/true,
6008 /*non_constant_p=*/NULL);
6009 if (vec == NULL)
6010 return error_mark_node;
6012 FOR_EACH_VEC_ELT (*vec, i, p)
6013 mark_exp_read (p);
6015 if (vec->length () == 2)
6016 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6017 tf_warning_or_error);
6018 else if (vec->length () == 3)
6019 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6020 tf_warning_or_error);
6021 else
6023 error_at (loc, "wrong number of arguments to "
6024 "%<__builtin_shuffle%>");
6025 return error_mark_node;
6027 break;
6030 default:
6032 tree type;
6034 /* If the next thing is a simple-type-specifier, we may be
6035 looking at a functional cast. We could also be looking at
6036 an id-expression. So, we try the functional cast, and if
6037 that doesn't work we fall back to the primary-expression. */
6038 cp_parser_parse_tentatively (parser);
6039 /* Look for the simple-type-specifier. */
6040 type = cp_parser_simple_type_specifier (parser,
6041 /*decl_specs=*/NULL,
6042 CP_PARSER_FLAGS_NONE);
6043 /* Parse the cast itself. */
6044 if (!cp_parser_error_occurred (parser))
6045 postfix_expression
6046 = cp_parser_functional_cast (parser, type);
6047 /* If that worked, we're done. */
6048 if (cp_parser_parse_definitely (parser))
6049 break;
6051 /* If the functional-cast didn't work out, try a
6052 compound-literal. */
6053 if (cp_parser_allow_gnu_extensions_p (parser)
6054 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6056 tree initializer = NULL_TREE;
6058 cp_parser_parse_tentatively (parser);
6060 /* Avoid calling cp_parser_type_id pointlessly, see comment
6061 in cp_parser_cast_expression about c++/29234. */
6062 if (!cp_parser_compound_literal_p (parser))
6063 cp_parser_simulate_error (parser);
6064 else
6066 /* Parse the type. */
6067 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6068 parser->in_type_id_in_expr_p = true;
6069 type = cp_parser_type_id (parser);
6070 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6071 /* Look for the `)'. */
6072 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6075 /* If things aren't going well, there's no need to
6076 keep going. */
6077 if (!cp_parser_error_occurred (parser))
6079 bool non_constant_p;
6080 /* Parse the brace-enclosed initializer list. */
6081 initializer = cp_parser_braced_list (parser,
6082 &non_constant_p);
6084 /* If that worked, we're definitely looking at a
6085 compound-literal expression. */
6086 if (cp_parser_parse_definitely (parser))
6088 /* Warn the user that a compound literal is not
6089 allowed in standard C++. */
6090 pedwarn (input_location, OPT_Wpedantic,
6091 "ISO C++ forbids compound-literals");
6092 /* For simplicity, we disallow compound literals in
6093 constant-expressions. We could
6094 allow compound literals of integer type, whose
6095 initializer was a constant, in constant
6096 expressions. Permitting that usage, as a further
6097 extension, would not change the meaning of any
6098 currently accepted programs. (Of course, as
6099 compound literals are not part of ISO C++, the
6100 standard has nothing to say.) */
6101 if (cp_parser_non_integral_constant_expression (parser,
6102 NIC_NCC))
6104 postfix_expression = error_mark_node;
6105 break;
6107 /* Form the representation of the compound-literal. */
6108 postfix_expression
6109 = finish_compound_literal (type, initializer,
6110 tf_warning_or_error);
6111 break;
6115 /* It must be a primary-expression. */
6116 postfix_expression
6117 = cp_parser_primary_expression (parser, address_p, cast_p,
6118 /*template_arg_p=*/false,
6119 decltype_p,
6120 &idk);
6122 break;
6125 /* Note that we don't need to worry about calling build_cplus_new on a
6126 class-valued CALL_EXPR in decltype when it isn't the end of the
6127 postfix-expression; unary_complex_lvalue will take care of that for
6128 all these cases. */
6130 /* Keep looping until the postfix-expression is complete. */
6131 while (true)
6133 if (idk == CP_ID_KIND_UNQUALIFIED
6134 && identifier_p (postfix_expression)
6135 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6136 /* It is not a Koenig lookup function call. */
6137 postfix_expression
6138 = unqualified_name_lookup_error (postfix_expression);
6140 /* Peek at the next token. */
6141 token = cp_lexer_peek_token (parser->lexer);
6143 switch (token->type)
6145 case CPP_OPEN_SQUARE:
6146 if (cp_next_tokens_can_be_std_attribute_p (parser))
6148 cp_parser_error (parser,
6149 "two consecutive %<[%> shall "
6150 "only introduce an attribute");
6151 return error_mark_node;
6153 postfix_expression
6154 = cp_parser_postfix_open_square_expression (parser,
6155 postfix_expression,
6156 false,
6157 decltype_p);
6158 idk = CP_ID_KIND_NONE;
6159 is_member_access = false;
6160 break;
6162 case CPP_OPEN_PAREN:
6163 /* postfix-expression ( expression-list [opt] ) */
6165 bool koenig_p;
6166 bool is_builtin_constant_p;
6167 bool saved_integral_constant_expression_p = false;
6168 bool saved_non_integral_constant_expression_p = false;
6169 tsubst_flags_t complain = complain_flags (decltype_p);
6170 vec<tree, va_gc> *args;
6172 is_member_access = false;
6174 is_builtin_constant_p
6175 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6176 if (is_builtin_constant_p)
6178 /* The whole point of __builtin_constant_p is to allow
6179 non-constant expressions to appear as arguments. */
6180 saved_integral_constant_expression_p
6181 = parser->integral_constant_expression_p;
6182 saved_non_integral_constant_expression_p
6183 = parser->non_integral_constant_expression_p;
6184 parser->integral_constant_expression_p = false;
6186 args = (cp_parser_parenthesized_expression_list
6187 (parser, non_attr,
6188 /*cast_p=*/false, /*allow_expansion_p=*/true,
6189 /*non_constant_p=*/NULL,
6190 /*want_literal_zero_p=*/warn_memset_transposed_args));
6191 if (is_builtin_constant_p)
6193 parser->integral_constant_expression_p
6194 = saved_integral_constant_expression_p;
6195 parser->non_integral_constant_expression_p
6196 = saved_non_integral_constant_expression_p;
6199 if (args == NULL)
6201 postfix_expression = error_mark_node;
6202 break;
6205 /* Function calls are not permitted in
6206 constant-expressions. */
6207 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6208 && cp_parser_non_integral_constant_expression (parser,
6209 NIC_FUNC_CALL))
6211 postfix_expression = error_mark_node;
6212 release_tree_vector (args);
6213 break;
6216 koenig_p = false;
6217 if (idk == CP_ID_KIND_UNQUALIFIED
6218 || idk == CP_ID_KIND_TEMPLATE_ID)
6220 if (identifier_p (postfix_expression))
6222 if (!args->is_empty ())
6224 koenig_p = true;
6225 if (!any_type_dependent_arguments_p (args))
6226 postfix_expression
6227 = perform_koenig_lookup (postfix_expression, args,
6228 complain);
6230 else
6231 postfix_expression
6232 = unqualified_fn_lookup_error (postfix_expression);
6234 /* We do not perform argument-dependent lookup if
6235 normal lookup finds a non-function, in accordance
6236 with the expected resolution of DR 218. */
6237 else if (!args->is_empty ()
6238 && is_overloaded_fn (postfix_expression))
6240 tree fn = get_first_fn (postfix_expression);
6241 fn = STRIP_TEMPLATE (fn);
6243 /* Do not do argument dependent lookup if regular
6244 lookup finds a member function or a block-scope
6245 function declaration. [basic.lookup.argdep]/3 */
6246 if (!DECL_FUNCTION_MEMBER_P (fn)
6247 && !DECL_LOCAL_FUNCTION_P (fn))
6249 koenig_p = true;
6250 if (!any_type_dependent_arguments_p (args))
6251 postfix_expression
6252 = perform_koenig_lookup (postfix_expression, args,
6253 complain);
6258 if (warn_memset_transposed_args)
6260 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6261 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6262 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6263 && vec_safe_length (args) == 3
6264 && integer_zerop ((*args)[2])
6265 && LITERAL_ZERO_P ((*args)[2])
6266 && !(integer_zerop ((*args)[1])
6267 && LITERAL_ZERO_P ((*args)[1])))
6268 warning (OPT_Wmemset_transposed_args,
6269 "%<memset%> used with constant zero length "
6270 "parameter; this could be due to transposed "
6271 "parameters");
6273 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6274 to avoid leaking those into folder and middle-end. */
6275 unsigned int i;
6276 tree arg;
6277 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6278 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6279 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6282 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6284 tree instance = TREE_OPERAND (postfix_expression, 0);
6285 tree fn = TREE_OPERAND (postfix_expression, 1);
6287 if (processing_template_decl
6288 && (type_dependent_expression_p (instance)
6289 || (!BASELINK_P (fn)
6290 && TREE_CODE (fn) != FIELD_DECL)
6291 || type_dependent_expression_p (fn)
6292 || any_type_dependent_arguments_p (args)))
6294 postfix_expression
6295 = build_nt_call_vec (postfix_expression, args);
6296 release_tree_vector (args);
6297 break;
6300 if (BASELINK_P (fn))
6302 postfix_expression
6303 = (build_new_method_call
6304 (instance, fn, &args, NULL_TREE,
6305 (idk == CP_ID_KIND_QUALIFIED
6306 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6307 : LOOKUP_NORMAL),
6308 /*fn_p=*/NULL,
6309 complain));
6311 else
6312 postfix_expression
6313 = finish_call_expr (postfix_expression, &args,
6314 /*disallow_virtual=*/false,
6315 /*koenig_p=*/false,
6316 complain);
6318 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6319 || TREE_CODE (postfix_expression) == MEMBER_REF
6320 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6321 postfix_expression = (build_offset_ref_call_from_tree
6322 (postfix_expression, &args,
6323 complain));
6324 else if (idk == CP_ID_KIND_QUALIFIED)
6325 /* A call to a static class member, or a namespace-scope
6326 function. */
6327 postfix_expression
6328 = finish_call_expr (postfix_expression, &args,
6329 /*disallow_virtual=*/true,
6330 koenig_p,
6331 complain);
6332 else
6333 /* All other function calls. */
6334 postfix_expression
6335 = finish_call_expr (postfix_expression, &args,
6336 /*disallow_virtual=*/false,
6337 koenig_p,
6338 complain);
6340 protected_set_expr_location (postfix_expression, token->location);
6342 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6343 idk = CP_ID_KIND_NONE;
6345 release_tree_vector (args);
6347 break;
6349 case CPP_DOT:
6350 case CPP_DEREF:
6351 /* postfix-expression . template [opt] id-expression
6352 postfix-expression . pseudo-destructor-name
6353 postfix-expression -> template [opt] id-expression
6354 postfix-expression -> pseudo-destructor-name */
6356 /* Consume the `.' or `->' operator. */
6357 cp_lexer_consume_token (parser->lexer);
6359 postfix_expression
6360 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6361 postfix_expression,
6362 false, &idk, loc);
6364 is_member_access = true;
6365 break;
6367 case CPP_PLUS_PLUS:
6368 /* postfix-expression ++ */
6369 /* Consume the `++' token. */
6370 cp_lexer_consume_token (parser->lexer);
6371 /* Generate a representation for the complete expression. */
6372 postfix_expression
6373 = finish_increment_expr (postfix_expression,
6374 POSTINCREMENT_EXPR);
6375 /* Increments may not appear in constant-expressions. */
6376 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6377 postfix_expression = error_mark_node;
6378 idk = CP_ID_KIND_NONE;
6379 is_member_access = false;
6380 break;
6382 case CPP_MINUS_MINUS:
6383 /* postfix-expression -- */
6384 /* Consume the `--' token. */
6385 cp_lexer_consume_token (parser->lexer);
6386 /* Generate a representation for the complete expression. */
6387 postfix_expression
6388 = finish_increment_expr (postfix_expression,
6389 POSTDECREMENT_EXPR);
6390 /* Decrements may not appear in constant-expressions. */
6391 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6392 postfix_expression = error_mark_node;
6393 idk = CP_ID_KIND_NONE;
6394 is_member_access = false;
6395 break;
6397 default:
6398 if (pidk_return != NULL)
6399 * pidk_return = idk;
6400 if (member_access_only_p)
6401 return is_member_access? postfix_expression : error_mark_node;
6402 else
6403 return postfix_expression;
6407 /* We should never get here. */
6408 gcc_unreachable ();
6409 return error_mark_node;
6412 /* This function parses Cilk Plus array notations. If a normal array expr. is
6413 parsed then the array index is passed back to the caller through *INIT_INDEX
6414 and the function returns a NULL_TREE. If array notation expr. is parsed,
6415 then *INIT_INDEX is ignored by the caller and the function returns
6416 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6417 error_mark_node. */
6419 static tree
6420 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6421 tree array_value)
6423 cp_token *token = NULL;
6424 tree length_index, stride = NULL_TREE, value_tree, array_type;
6425 if (!array_value || array_value == error_mark_node)
6427 cp_parser_skip_to_end_of_statement (parser);
6428 return error_mark_node;
6431 array_type = TREE_TYPE (array_value);
6433 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6434 parser->colon_corrects_to_scope_p = false;
6435 token = cp_lexer_peek_token (parser->lexer);
6437 if (!token)
6439 cp_parser_error (parser, "expected %<:%> or numeral");
6440 return error_mark_node;
6442 else if (token->type == CPP_COLON)
6444 /* Consume the ':'. */
6445 cp_lexer_consume_token (parser->lexer);
6447 /* If we are here, then we have a case like this A[:]. */
6448 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6450 cp_parser_error (parser, "expected %<]%>");
6451 cp_parser_skip_to_end_of_statement (parser);
6452 return error_mark_node;
6454 *init_index = NULL_TREE;
6455 stride = NULL_TREE;
6456 length_index = NULL_TREE;
6458 else
6460 /* If we are here, then there are three valid possibilities:
6461 1. ARRAY [ EXP ]
6462 2. ARRAY [ EXP : EXP ]
6463 3. ARRAY [ EXP : EXP : EXP ] */
6465 *init_index = cp_parser_expression (parser);
6466 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6468 /* This indicates that we have a normal array expression. */
6469 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6470 return NULL_TREE;
6473 /* Consume the ':'. */
6474 cp_lexer_consume_token (parser->lexer);
6475 length_index = cp_parser_expression (parser);
6476 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6478 cp_lexer_consume_token (parser->lexer);
6479 stride = cp_parser_expression (parser);
6482 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6484 if (*init_index == error_mark_node || length_index == error_mark_node
6485 || stride == error_mark_node || array_type == error_mark_node)
6487 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6488 cp_lexer_consume_token (parser->lexer);
6489 return error_mark_node;
6491 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6493 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6494 length_index, stride, array_type);
6495 return value_tree;
6498 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6499 by cp_parser_builtin_offsetof. We're looking for
6501 postfix-expression [ expression ]
6502 postfix-expression [ braced-init-list ] (C++11)
6504 FOR_OFFSETOF is set if we're being called in that context, which
6505 changes how we deal with integer constant expressions. */
6507 static tree
6508 cp_parser_postfix_open_square_expression (cp_parser *parser,
6509 tree postfix_expression,
6510 bool for_offsetof,
6511 bool decltype_p)
6513 tree index = NULL_TREE;
6514 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6515 bool saved_greater_than_is_operator_p;
6517 /* Consume the `[' token. */
6518 cp_lexer_consume_token (parser->lexer);
6520 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6521 parser->greater_than_is_operator_p = true;
6523 /* Parse the index expression. */
6524 /* ??? For offsetof, there is a question of what to allow here. If
6525 offsetof is not being used in an integral constant expression context,
6526 then we *could* get the right answer by computing the value at runtime.
6527 If we are in an integral constant expression context, then we might
6528 could accept any constant expression; hard to say without analysis.
6529 Rather than open the barn door too wide right away, allow only integer
6530 constant expressions here. */
6531 if (for_offsetof)
6532 index = cp_parser_constant_expression (parser);
6533 else
6535 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6537 bool expr_nonconst_p;
6538 cp_lexer_set_source_position (parser->lexer);
6539 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6540 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6541 if (flag_cilkplus
6542 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6544 error_at (cp_lexer_peek_token (parser->lexer)->location,
6545 "braced list index is not allowed with array "
6546 "notation");
6547 cp_parser_skip_to_end_of_statement (parser);
6548 return error_mark_node;
6551 else if (flag_cilkplus)
6553 /* Here are have these two options:
6554 ARRAY[EXP : EXP] - Array notation expr with default
6555 stride of 1.
6556 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6557 stride. */
6558 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6559 postfix_expression);
6560 if (an_exp)
6561 return an_exp;
6563 else
6564 index = cp_parser_expression (parser);
6567 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6569 /* Look for the closing `]'. */
6570 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6572 /* Build the ARRAY_REF. */
6573 postfix_expression = grok_array_decl (loc, postfix_expression,
6574 index, decltype_p);
6576 /* When not doing offsetof, array references are not permitted in
6577 constant-expressions. */
6578 if (!for_offsetof
6579 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6580 postfix_expression = error_mark_node;
6582 return postfix_expression;
6585 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6586 by cp_parser_builtin_offsetof. We're looking for
6588 postfix-expression . template [opt] id-expression
6589 postfix-expression . pseudo-destructor-name
6590 postfix-expression -> template [opt] id-expression
6591 postfix-expression -> pseudo-destructor-name
6593 FOR_OFFSETOF is set if we're being called in that context. That sorta
6594 limits what of the above we'll actually accept, but nevermind.
6595 TOKEN_TYPE is the "." or "->" token, which will already have been
6596 removed from the stream. */
6598 static tree
6599 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6600 enum cpp_ttype token_type,
6601 tree postfix_expression,
6602 bool for_offsetof, cp_id_kind *idk,
6603 location_t location)
6605 tree name;
6606 bool dependent_p;
6607 bool pseudo_destructor_p;
6608 tree scope = NULL_TREE;
6610 /* If this is a `->' operator, dereference the pointer. */
6611 if (token_type == CPP_DEREF)
6612 postfix_expression = build_x_arrow (location, postfix_expression,
6613 tf_warning_or_error);
6614 /* Check to see whether or not the expression is type-dependent. */
6615 dependent_p = type_dependent_expression_p (postfix_expression);
6616 /* The identifier following the `->' or `.' is not qualified. */
6617 parser->scope = NULL_TREE;
6618 parser->qualifying_scope = NULL_TREE;
6619 parser->object_scope = NULL_TREE;
6620 *idk = CP_ID_KIND_NONE;
6622 /* Enter the scope corresponding to the type of the object
6623 given by the POSTFIX_EXPRESSION. */
6624 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6626 scope = TREE_TYPE (postfix_expression);
6627 /* According to the standard, no expression should ever have
6628 reference type. Unfortunately, we do not currently match
6629 the standard in this respect in that our internal representation
6630 of an expression may have reference type even when the standard
6631 says it does not. Therefore, we have to manually obtain the
6632 underlying type here. */
6633 scope = non_reference (scope);
6634 /* The type of the POSTFIX_EXPRESSION must be complete. */
6635 if (scope == unknown_type_node)
6637 error_at (location, "%qE does not have class type",
6638 postfix_expression);
6639 scope = NULL_TREE;
6641 /* Unlike the object expression in other contexts, *this is not
6642 required to be of complete type for purposes of class member
6643 access (5.2.5) outside the member function body. */
6644 else if (postfix_expression != current_class_ref
6645 && !(processing_template_decl && scope == current_class_type))
6646 scope = complete_type_or_else (scope, NULL_TREE);
6647 /* Let the name lookup machinery know that we are processing a
6648 class member access expression. */
6649 parser->context->object_type = scope;
6650 /* If something went wrong, we want to be able to discern that case,
6651 as opposed to the case where there was no SCOPE due to the type
6652 of expression being dependent. */
6653 if (!scope)
6654 scope = error_mark_node;
6655 /* If the SCOPE was erroneous, make the various semantic analysis
6656 functions exit quickly -- and without issuing additional error
6657 messages. */
6658 if (scope == error_mark_node)
6659 postfix_expression = error_mark_node;
6662 /* Assume this expression is not a pseudo-destructor access. */
6663 pseudo_destructor_p = false;
6665 /* If the SCOPE is a scalar type, then, if this is a valid program,
6666 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6667 is type dependent, it can be pseudo-destructor-name or something else.
6668 Try to parse it as pseudo-destructor-name first. */
6669 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6671 tree s;
6672 tree type;
6674 cp_parser_parse_tentatively (parser);
6675 /* Parse the pseudo-destructor-name. */
6676 s = NULL_TREE;
6677 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6678 &s, &type);
6679 if (dependent_p
6680 && (cp_parser_error_occurred (parser)
6681 || !SCALAR_TYPE_P (type)))
6682 cp_parser_abort_tentative_parse (parser);
6683 else if (cp_parser_parse_definitely (parser))
6685 pseudo_destructor_p = true;
6686 postfix_expression
6687 = finish_pseudo_destructor_expr (postfix_expression,
6688 s, type, location);
6692 if (!pseudo_destructor_p)
6694 /* If the SCOPE is not a scalar type, we are looking at an
6695 ordinary class member access expression, rather than a
6696 pseudo-destructor-name. */
6697 bool template_p;
6698 cp_token *token = cp_lexer_peek_token (parser->lexer);
6699 /* Parse the id-expression. */
6700 name = (cp_parser_id_expression
6701 (parser,
6702 cp_parser_optional_template_keyword (parser),
6703 /*check_dependency_p=*/true,
6704 &template_p,
6705 /*declarator_p=*/false,
6706 /*optional_p=*/false));
6707 /* In general, build a SCOPE_REF if the member name is qualified.
6708 However, if the name was not dependent and has already been
6709 resolved; there is no need to build the SCOPE_REF. For example;
6711 struct X { void f(); };
6712 template <typename T> void f(T* t) { t->X::f(); }
6714 Even though "t" is dependent, "X::f" is not and has been resolved
6715 to a BASELINK; there is no need to include scope information. */
6717 /* But we do need to remember that there was an explicit scope for
6718 virtual function calls. */
6719 if (parser->scope)
6720 *idk = CP_ID_KIND_QUALIFIED;
6722 /* If the name is a template-id that names a type, we will get a
6723 TYPE_DECL here. That is invalid code. */
6724 if (TREE_CODE (name) == TYPE_DECL)
6726 error_at (token->location, "invalid use of %qD", name);
6727 postfix_expression = error_mark_node;
6729 else
6731 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6733 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6735 error_at (token->location, "%<%D::%D%> is not a class member",
6736 parser->scope, name);
6737 postfix_expression = error_mark_node;
6739 else
6740 name = build_qualified_name (/*type=*/NULL_TREE,
6741 parser->scope,
6742 name,
6743 template_p);
6744 parser->scope = NULL_TREE;
6745 parser->qualifying_scope = NULL_TREE;
6746 parser->object_scope = NULL_TREE;
6748 if (parser->scope && name && BASELINK_P (name))
6749 adjust_result_of_qualified_name_lookup
6750 (name, parser->scope, scope);
6751 postfix_expression
6752 = finish_class_member_access_expr (postfix_expression, name,
6753 template_p,
6754 tf_warning_or_error);
6758 /* We no longer need to look up names in the scope of the object on
6759 the left-hand side of the `.' or `->' operator. */
6760 parser->context->object_type = NULL_TREE;
6762 /* Outside of offsetof, these operators may not appear in
6763 constant-expressions. */
6764 if (!for_offsetof
6765 && (cp_parser_non_integral_constant_expression
6766 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6767 postfix_expression = error_mark_node;
6769 return postfix_expression;
6772 /* Cache of LITERAL_ZERO_P constants. */
6774 static GTY(()) tree literal_zeros[itk_none];
6776 /* Parse a parenthesized expression-list.
6778 expression-list:
6779 assignment-expression
6780 expression-list, assignment-expression
6782 attribute-list:
6783 expression-list
6784 identifier
6785 identifier, expression-list
6787 CAST_P is true if this expression is the target of a cast.
6789 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6790 argument pack.
6792 Returns a vector of trees. Each element is a representation of an
6793 assignment-expression. NULL is returned if the ( and or ) are
6794 missing. An empty, but allocated, vector is returned on no
6795 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6796 if we are parsing an attribute list for an attribute that wants a
6797 plain identifier argument, normal_attr for an attribute that wants
6798 an expression, or non_attr if we aren't parsing an attribute list. If
6799 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6800 not all of the expressions in the list were constant.
6801 WANT_LITERAL_ZERO_P is true if the caller is interested in
6802 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6803 immediately, this can be removed. */
6805 static vec<tree, va_gc> *
6806 cp_parser_parenthesized_expression_list (cp_parser* parser,
6807 int is_attribute_list,
6808 bool cast_p,
6809 bool allow_expansion_p,
6810 bool *non_constant_p,
6811 bool want_literal_zero_p)
6813 vec<tree, va_gc> *expression_list;
6814 bool fold_expr_p = is_attribute_list != non_attr;
6815 tree identifier = NULL_TREE;
6816 bool saved_greater_than_is_operator_p;
6818 /* Assume all the expressions will be constant. */
6819 if (non_constant_p)
6820 *non_constant_p = false;
6822 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6823 return NULL;
6825 expression_list = make_tree_vector ();
6827 /* Within a parenthesized expression, a `>' token is always
6828 the greater-than operator. */
6829 saved_greater_than_is_operator_p
6830 = parser->greater_than_is_operator_p;
6831 parser->greater_than_is_operator_p = true;
6833 /* Consume expressions until there are no more. */
6834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6835 while (true)
6837 tree expr;
6839 /* At the beginning of attribute lists, check to see if the
6840 next token is an identifier. */
6841 if (is_attribute_list == id_attr
6842 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6844 cp_token *token;
6846 /* Consume the identifier. */
6847 token = cp_lexer_consume_token (parser->lexer);
6848 /* Save the identifier. */
6849 identifier = token->u.value;
6851 else
6853 bool expr_non_constant_p;
6855 /* Parse the next assignment-expression. */
6856 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6858 /* A braced-init-list. */
6859 cp_lexer_set_source_position (parser->lexer);
6860 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6861 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6862 if (non_constant_p && expr_non_constant_p)
6863 *non_constant_p = true;
6865 else if (non_constant_p)
6867 expr = (cp_parser_constant_expression
6868 (parser, /*allow_non_constant_p=*/true,
6869 &expr_non_constant_p));
6870 if (expr_non_constant_p)
6871 *non_constant_p = true;
6873 else
6875 expr = NULL_TREE;
6876 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6877 switch (tok->type)
6879 case CPP_NUMBER:
6880 case CPP_CHAR:
6881 case CPP_WCHAR:
6882 case CPP_CHAR16:
6883 case CPP_CHAR32:
6884 /* If a parameter is literal zero alone, remember it
6885 for -Wmemset-transposed-args warning. */
6886 if (integer_zerop (tok->u.value)
6887 && !TREE_OVERFLOW (tok->u.value)
6888 && want_literal_zero_p
6889 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6890 == CPP_COMMA
6891 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6892 == CPP_CLOSE_PAREN))
6894 unsigned int i;
6895 for (i = 0; i < itk_none; ++i)
6896 if (TREE_TYPE (tok->u.value) == integer_types[i])
6897 break;
6898 if (i < itk_none && literal_zeros[i])
6899 expr = literal_zeros[i];
6900 else
6902 expr = copy_node (tok->u.value);
6903 LITERAL_ZERO_P (expr) = 1;
6904 if (i < itk_none)
6905 literal_zeros[i] = expr;
6907 /* Consume the 0 token (or '\0', 0LL etc.). */
6908 cp_lexer_consume_token (parser->lexer);
6910 break;
6911 default:
6912 break;
6914 if (expr == NULL_TREE)
6915 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6916 cast_p);
6919 if (fold_expr_p)
6920 expr = instantiate_non_dependent_expr (expr);
6922 /* If we have an ellipsis, then this is an expression
6923 expansion. */
6924 if (allow_expansion_p
6925 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6927 /* Consume the `...'. */
6928 cp_lexer_consume_token (parser->lexer);
6930 /* Build the argument pack. */
6931 expr = make_pack_expansion (expr);
6934 /* Add it to the list. We add error_mark_node
6935 expressions to the list, so that we can still tell if
6936 the correct form for a parenthesized expression-list
6937 is found. That gives better errors. */
6938 vec_safe_push (expression_list, expr);
6940 if (expr == error_mark_node)
6941 goto skip_comma;
6944 /* After the first item, attribute lists look the same as
6945 expression lists. */
6946 is_attribute_list = non_attr;
6948 get_comma:;
6949 /* If the next token isn't a `,', then we are done. */
6950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6951 break;
6953 /* Otherwise, consume the `,' and keep going. */
6954 cp_lexer_consume_token (parser->lexer);
6957 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6959 int ending;
6961 skip_comma:;
6962 /* We try and resync to an unnested comma, as that will give the
6963 user better diagnostics. */
6964 ending = cp_parser_skip_to_closing_parenthesis (parser,
6965 /*recovering=*/true,
6966 /*or_comma=*/true,
6967 /*consume_paren=*/true);
6968 if (ending < 0)
6969 goto get_comma;
6970 if (!ending)
6972 parser->greater_than_is_operator_p
6973 = saved_greater_than_is_operator_p;
6974 return NULL;
6978 parser->greater_than_is_operator_p
6979 = saved_greater_than_is_operator_p;
6981 if (identifier)
6982 vec_safe_insert (expression_list, 0, identifier);
6984 return expression_list;
6987 /* Parse a pseudo-destructor-name.
6989 pseudo-destructor-name:
6990 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6991 :: [opt] nested-name-specifier template template-id :: ~ type-name
6992 :: [opt] nested-name-specifier [opt] ~ type-name
6994 If either of the first two productions is used, sets *SCOPE to the
6995 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6996 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6997 or ERROR_MARK_NODE if the parse fails. */
6999 static void
7000 cp_parser_pseudo_destructor_name (cp_parser* parser,
7001 tree object,
7002 tree* scope,
7003 tree* type)
7005 bool nested_name_specifier_p;
7007 /* Handle ~auto. */
7008 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7009 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7010 && !type_dependent_expression_p (object))
7012 if (cxx_dialect < cxx14)
7013 pedwarn (input_location, 0,
7014 "%<~auto%> only available with "
7015 "-std=c++14 or -std=gnu++14");
7016 cp_lexer_consume_token (parser->lexer);
7017 cp_lexer_consume_token (parser->lexer);
7018 *scope = NULL_TREE;
7019 *type = TREE_TYPE (object);
7020 return;
7023 /* Assume that things will not work out. */
7024 *type = error_mark_node;
7026 /* Look for the optional `::' operator. */
7027 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7028 /* Look for the optional nested-name-specifier. */
7029 nested_name_specifier_p
7030 = (cp_parser_nested_name_specifier_opt (parser,
7031 /*typename_keyword_p=*/false,
7032 /*check_dependency_p=*/true,
7033 /*type_p=*/false,
7034 /*is_declaration=*/false)
7035 != NULL_TREE);
7036 /* Now, if we saw a nested-name-specifier, we might be doing the
7037 second production. */
7038 if (nested_name_specifier_p
7039 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7041 /* Consume the `template' keyword. */
7042 cp_lexer_consume_token (parser->lexer);
7043 /* Parse the template-id. */
7044 cp_parser_template_id (parser,
7045 /*template_keyword_p=*/true,
7046 /*check_dependency_p=*/false,
7047 class_type,
7048 /*is_declaration=*/true);
7049 /* Look for the `::' token. */
7050 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7052 /* If the next token is not a `~', then there might be some
7053 additional qualification. */
7054 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7056 /* At this point, we're looking for "type-name :: ~". The type-name
7057 must not be a class-name, since this is a pseudo-destructor. So,
7058 it must be either an enum-name, or a typedef-name -- both of which
7059 are just identifiers. So, we peek ahead to check that the "::"
7060 and "~" tokens are present; if they are not, then we can avoid
7061 calling type_name. */
7062 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7063 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7064 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7066 cp_parser_error (parser, "non-scalar type");
7067 return;
7070 /* Look for the type-name. */
7071 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7072 if (*scope == error_mark_node)
7073 return;
7075 /* Look for the `::' token. */
7076 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7078 else
7079 *scope = NULL_TREE;
7081 /* Look for the `~'. */
7082 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7084 /* Once we see the ~, this has to be a pseudo-destructor. */
7085 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7086 cp_parser_commit_to_topmost_tentative_parse (parser);
7088 /* Look for the type-name again. We are not responsible for
7089 checking that it matches the first type-name. */
7090 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7093 /* Parse a unary-expression.
7095 unary-expression:
7096 postfix-expression
7097 ++ cast-expression
7098 -- cast-expression
7099 unary-operator cast-expression
7100 sizeof unary-expression
7101 sizeof ( type-id )
7102 alignof ( type-id ) [C++0x]
7103 new-expression
7104 delete-expression
7106 GNU Extensions:
7108 unary-expression:
7109 __extension__ cast-expression
7110 __alignof__ unary-expression
7111 __alignof__ ( type-id )
7112 alignof unary-expression [C++0x]
7113 __real__ cast-expression
7114 __imag__ cast-expression
7115 && identifier
7116 sizeof ( type-id ) { initializer-list , [opt] }
7117 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7118 __alignof__ ( type-id ) { initializer-list , [opt] }
7120 ADDRESS_P is true iff the unary-expression is appearing as the
7121 operand of the `&' operator. CAST_P is true if this expression is
7122 the target of a cast.
7124 Returns a representation of the expression. */
7126 static tree
7127 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7128 bool address_p, bool cast_p, bool decltype_p)
7130 cp_token *token;
7131 enum tree_code unary_operator;
7133 /* Peek at the next token. */
7134 token = cp_lexer_peek_token (parser->lexer);
7135 /* Some keywords give away the kind of expression. */
7136 if (token->type == CPP_KEYWORD)
7138 enum rid keyword = token->keyword;
7140 switch (keyword)
7142 case RID_ALIGNOF:
7143 case RID_SIZEOF:
7145 tree operand, ret;
7146 enum tree_code op;
7147 location_t first_loc;
7149 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7150 /* Consume the token. */
7151 cp_lexer_consume_token (parser->lexer);
7152 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7153 /* Parse the operand. */
7154 operand = cp_parser_sizeof_operand (parser, keyword);
7156 if (TYPE_P (operand))
7157 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7158 else
7160 /* ISO C++ defines alignof only with types, not with
7161 expressions. So pedwarn if alignof is used with a non-
7162 type expression. However, __alignof__ is ok. */
7163 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7164 pedwarn (token->location, OPT_Wpedantic,
7165 "ISO C++ does not allow %<alignof%> "
7166 "with a non-type");
7168 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7170 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7171 SIZEOF_EXPR with the original operand. */
7172 if (op == SIZEOF_EXPR && ret != error_mark_node)
7174 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7176 if (!processing_template_decl && TYPE_P (operand))
7178 ret = build_min (SIZEOF_EXPR, size_type_node,
7179 build1 (NOP_EXPR, operand,
7180 error_mark_node));
7181 SIZEOF_EXPR_TYPE_P (ret) = 1;
7183 else
7184 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7185 TREE_SIDE_EFFECTS (ret) = 0;
7186 TREE_READONLY (ret) = 1;
7188 SET_EXPR_LOCATION (ret, first_loc);
7190 return ret;
7193 case RID_NEW:
7194 return cp_parser_new_expression (parser);
7196 case RID_DELETE:
7197 return cp_parser_delete_expression (parser);
7199 case RID_EXTENSION:
7201 /* The saved value of the PEDANTIC flag. */
7202 int saved_pedantic;
7203 tree expr;
7205 /* Save away the PEDANTIC flag. */
7206 cp_parser_extension_opt (parser, &saved_pedantic);
7207 /* Parse the cast-expression. */
7208 expr = cp_parser_simple_cast_expression (parser);
7209 /* Restore the PEDANTIC flag. */
7210 pedantic = saved_pedantic;
7212 return expr;
7215 case RID_REALPART:
7216 case RID_IMAGPART:
7218 tree expression;
7220 /* Consume the `__real__' or `__imag__' token. */
7221 cp_lexer_consume_token (parser->lexer);
7222 /* Parse the cast-expression. */
7223 expression = cp_parser_simple_cast_expression (parser);
7224 /* Create the complete representation. */
7225 return build_x_unary_op (token->location,
7226 (keyword == RID_REALPART
7227 ? REALPART_EXPR : IMAGPART_EXPR),
7228 expression,
7229 tf_warning_or_error);
7231 break;
7233 case RID_TRANSACTION_ATOMIC:
7234 case RID_TRANSACTION_RELAXED:
7235 return cp_parser_transaction_expression (parser, keyword);
7237 case RID_NOEXCEPT:
7239 tree expr;
7240 const char *saved_message;
7241 bool saved_integral_constant_expression_p;
7242 bool saved_non_integral_constant_expression_p;
7243 bool saved_greater_than_is_operator_p;
7245 cp_lexer_consume_token (parser->lexer);
7246 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7248 saved_message = parser->type_definition_forbidden_message;
7249 parser->type_definition_forbidden_message
7250 = G_("types may not be defined in %<noexcept%> expressions");
7252 saved_integral_constant_expression_p
7253 = parser->integral_constant_expression_p;
7254 saved_non_integral_constant_expression_p
7255 = parser->non_integral_constant_expression_p;
7256 parser->integral_constant_expression_p = false;
7258 saved_greater_than_is_operator_p
7259 = parser->greater_than_is_operator_p;
7260 parser->greater_than_is_operator_p = true;
7262 ++cp_unevaluated_operand;
7263 ++c_inhibit_evaluation_warnings;
7264 ++cp_noexcept_operand;
7265 expr = cp_parser_expression (parser);
7266 --cp_noexcept_operand;
7267 --c_inhibit_evaluation_warnings;
7268 --cp_unevaluated_operand;
7270 parser->greater_than_is_operator_p
7271 = saved_greater_than_is_operator_p;
7273 parser->integral_constant_expression_p
7274 = saved_integral_constant_expression_p;
7275 parser->non_integral_constant_expression_p
7276 = saved_non_integral_constant_expression_p;
7278 parser->type_definition_forbidden_message = saved_message;
7280 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7281 return finish_noexcept_expr (expr, tf_warning_or_error);
7284 default:
7285 break;
7289 /* Look for the `:: new' and `:: delete', which also signal the
7290 beginning of a new-expression, or delete-expression,
7291 respectively. If the next token is `::', then it might be one of
7292 these. */
7293 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7295 enum rid keyword;
7297 /* See if the token after the `::' is one of the keywords in
7298 which we're interested. */
7299 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7300 /* If it's `new', we have a new-expression. */
7301 if (keyword == RID_NEW)
7302 return cp_parser_new_expression (parser);
7303 /* Similarly, for `delete'. */
7304 else if (keyword == RID_DELETE)
7305 return cp_parser_delete_expression (parser);
7308 /* Look for a unary operator. */
7309 unary_operator = cp_parser_unary_operator (token);
7310 /* The `++' and `--' operators can be handled similarly, even though
7311 they are not technically unary-operators in the grammar. */
7312 if (unary_operator == ERROR_MARK)
7314 if (token->type == CPP_PLUS_PLUS)
7315 unary_operator = PREINCREMENT_EXPR;
7316 else if (token->type == CPP_MINUS_MINUS)
7317 unary_operator = PREDECREMENT_EXPR;
7318 /* Handle the GNU address-of-label extension. */
7319 else if (cp_parser_allow_gnu_extensions_p (parser)
7320 && token->type == CPP_AND_AND)
7322 tree identifier;
7323 tree expression;
7324 location_t loc = token->location;
7326 /* Consume the '&&' token. */
7327 cp_lexer_consume_token (parser->lexer);
7328 /* Look for the identifier. */
7329 identifier = cp_parser_identifier (parser);
7330 /* Create an expression representing the address. */
7331 expression = finish_label_address_expr (identifier, loc);
7332 if (cp_parser_non_integral_constant_expression (parser,
7333 NIC_ADDR_LABEL))
7334 expression = error_mark_node;
7335 return expression;
7338 if (unary_operator != ERROR_MARK)
7340 tree cast_expression;
7341 tree expression = error_mark_node;
7342 non_integral_constant non_constant_p = NIC_NONE;
7343 location_t loc = token->location;
7344 tsubst_flags_t complain = complain_flags (decltype_p);
7346 /* Consume the operator token. */
7347 token = cp_lexer_consume_token (parser->lexer);
7348 /* Parse the cast-expression. */
7349 cast_expression
7350 = cp_parser_cast_expression (parser,
7351 unary_operator == ADDR_EXPR,
7352 /*cast_p=*/false,
7353 /*decltype*/false,
7354 pidk);
7355 /* Now, build an appropriate representation. */
7356 switch (unary_operator)
7358 case INDIRECT_REF:
7359 non_constant_p = NIC_STAR;
7360 expression = build_x_indirect_ref (loc, cast_expression,
7361 RO_UNARY_STAR,
7362 complain);
7363 break;
7365 case ADDR_EXPR:
7366 non_constant_p = NIC_ADDR;
7367 /* Fall through. */
7368 case BIT_NOT_EXPR:
7369 expression = build_x_unary_op (loc, unary_operator,
7370 cast_expression,
7371 complain);
7372 break;
7374 case PREINCREMENT_EXPR:
7375 case PREDECREMENT_EXPR:
7376 non_constant_p = unary_operator == PREINCREMENT_EXPR
7377 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7378 /* Fall through. */
7379 case UNARY_PLUS_EXPR:
7380 case NEGATE_EXPR:
7381 case TRUTH_NOT_EXPR:
7382 expression = finish_unary_op_expr (loc, unary_operator,
7383 cast_expression, complain);
7384 break;
7386 default:
7387 gcc_unreachable ();
7390 if (non_constant_p != NIC_NONE
7391 && cp_parser_non_integral_constant_expression (parser,
7392 non_constant_p))
7393 expression = error_mark_node;
7395 return expression;
7398 return cp_parser_postfix_expression (parser, address_p, cast_p,
7399 /*member_access_only_p=*/false,
7400 decltype_p,
7401 pidk);
7404 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7405 unary-operator, the corresponding tree code is returned. */
7407 static enum tree_code
7408 cp_parser_unary_operator (cp_token* token)
7410 switch (token->type)
7412 case CPP_MULT:
7413 return INDIRECT_REF;
7415 case CPP_AND:
7416 return ADDR_EXPR;
7418 case CPP_PLUS:
7419 return UNARY_PLUS_EXPR;
7421 case CPP_MINUS:
7422 return NEGATE_EXPR;
7424 case CPP_NOT:
7425 return TRUTH_NOT_EXPR;
7427 case CPP_COMPL:
7428 return BIT_NOT_EXPR;
7430 default:
7431 return ERROR_MARK;
7435 /* Parse a new-expression.
7437 new-expression:
7438 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7439 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7441 Returns a representation of the expression. */
7443 static tree
7444 cp_parser_new_expression (cp_parser* parser)
7446 bool global_scope_p;
7447 vec<tree, va_gc> *placement;
7448 tree type;
7449 vec<tree, va_gc> *initializer;
7450 tree nelts = NULL_TREE;
7451 tree ret;
7453 /* Look for the optional `::' operator. */
7454 global_scope_p
7455 = (cp_parser_global_scope_opt (parser,
7456 /*current_scope_valid_p=*/false)
7457 != NULL_TREE);
7458 /* Look for the `new' operator. */
7459 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7460 /* There's no easy way to tell a new-placement from the
7461 `( type-id )' construct. */
7462 cp_parser_parse_tentatively (parser);
7463 /* Look for a new-placement. */
7464 placement = cp_parser_new_placement (parser);
7465 /* If that didn't work out, there's no new-placement. */
7466 if (!cp_parser_parse_definitely (parser))
7468 if (placement != NULL)
7469 release_tree_vector (placement);
7470 placement = NULL;
7473 /* If the next token is a `(', then we have a parenthesized
7474 type-id. */
7475 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7477 cp_token *token;
7478 const char *saved_message = parser->type_definition_forbidden_message;
7480 /* Consume the `('. */
7481 cp_lexer_consume_token (parser->lexer);
7483 /* Parse the type-id. */
7484 parser->type_definition_forbidden_message
7485 = G_("types may not be defined in a new-expression");
7486 type = cp_parser_type_id (parser);
7487 parser->type_definition_forbidden_message = saved_message;
7489 /* Look for the closing `)'. */
7490 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7491 token = cp_lexer_peek_token (parser->lexer);
7492 /* There should not be a direct-new-declarator in this production,
7493 but GCC used to allowed this, so we check and emit a sensible error
7494 message for this case. */
7495 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7497 error_at (token->location,
7498 "array bound forbidden after parenthesized type-id");
7499 inform (token->location,
7500 "try removing the parentheses around the type-id");
7501 cp_parser_direct_new_declarator (parser);
7504 /* Otherwise, there must be a new-type-id. */
7505 else
7506 type = cp_parser_new_type_id (parser, &nelts);
7508 /* If the next token is a `(' or '{', then we have a new-initializer. */
7509 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7510 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7511 initializer = cp_parser_new_initializer (parser);
7512 else
7513 initializer = NULL;
7515 /* A new-expression may not appear in an integral constant
7516 expression. */
7517 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7518 ret = error_mark_node;
7519 else
7521 /* Create a representation of the new-expression. */
7522 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7523 tf_warning_or_error);
7526 if (placement != NULL)
7527 release_tree_vector (placement);
7528 if (initializer != NULL)
7529 release_tree_vector (initializer);
7531 return ret;
7534 /* Parse a new-placement.
7536 new-placement:
7537 ( expression-list )
7539 Returns the same representation as for an expression-list. */
7541 static vec<tree, va_gc> *
7542 cp_parser_new_placement (cp_parser* parser)
7544 vec<tree, va_gc> *expression_list;
7546 /* Parse the expression-list. */
7547 expression_list = (cp_parser_parenthesized_expression_list
7548 (parser, non_attr, /*cast_p=*/false,
7549 /*allow_expansion_p=*/true,
7550 /*non_constant_p=*/NULL));
7552 return expression_list;
7555 /* Parse a new-type-id.
7557 new-type-id:
7558 type-specifier-seq new-declarator [opt]
7560 Returns the TYPE allocated. If the new-type-id indicates an array
7561 type, *NELTS is set to the number of elements in the last array
7562 bound; the TYPE will not include the last array bound. */
7564 static tree
7565 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7567 cp_decl_specifier_seq type_specifier_seq;
7568 cp_declarator *new_declarator;
7569 cp_declarator *declarator;
7570 cp_declarator *outer_declarator;
7571 const char *saved_message;
7573 /* The type-specifier sequence must not contain type definitions.
7574 (It cannot contain declarations of new types either, but if they
7575 are not definitions we will catch that because they are not
7576 complete.) */
7577 saved_message = parser->type_definition_forbidden_message;
7578 parser->type_definition_forbidden_message
7579 = G_("types may not be defined in a new-type-id");
7580 /* Parse the type-specifier-seq. */
7581 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7582 /*is_trailing_return=*/false,
7583 &type_specifier_seq);
7584 /* Restore the old message. */
7585 parser->type_definition_forbidden_message = saved_message;
7587 if (type_specifier_seq.type == error_mark_node)
7588 return error_mark_node;
7590 /* Parse the new-declarator. */
7591 new_declarator = cp_parser_new_declarator_opt (parser);
7593 /* Determine the number of elements in the last array dimension, if
7594 any. */
7595 *nelts = NULL_TREE;
7596 /* Skip down to the last array dimension. */
7597 declarator = new_declarator;
7598 outer_declarator = NULL;
7599 while (declarator && (declarator->kind == cdk_pointer
7600 || declarator->kind == cdk_ptrmem))
7602 outer_declarator = declarator;
7603 declarator = declarator->declarator;
7605 while (declarator
7606 && declarator->kind == cdk_array
7607 && declarator->declarator
7608 && declarator->declarator->kind == cdk_array)
7610 outer_declarator = declarator;
7611 declarator = declarator->declarator;
7614 if (declarator && declarator->kind == cdk_array)
7616 *nelts = declarator->u.array.bounds;
7617 if (*nelts == error_mark_node)
7618 *nelts = integer_one_node;
7620 if (outer_declarator)
7621 outer_declarator->declarator = declarator->declarator;
7622 else
7623 new_declarator = NULL;
7626 return groktypename (&type_specifier_seq, new_declarator, false);
7629 /* Parse an (optional) new-declarator.
7631 new-declarator:
7632 ptr-operator new-declarator [opt]
7633 direct-new-declarator
7635 Returns the declarator. */
7637 static cp_declarator *
7638 cp_parser_new_declarator_opt (cp_parser* parser)
7640 enum tree_code code;
7641 tree type, std_attributes = NULL_TREE;
7642 cp_cv_quals cv_quals;
7644 /* We don't know if there's a ptr-operator next, or not. */
7645 cp_parser_parse_tentatively (parser);
7646 /* Look for a ptr-operator. */
7647 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7648 /* If that worked, look for more new-declarators. */
7649 if (cp_parser_parse_definitely (parser))
7651 cp_declarator *declarator;
7653 /* Parse another optional declarator. */
7654 declarator = cp_parser_new_declarator_opt (parser);
7656 declarator = cp_parser_make_indirect_declarator
7657 (code, type, cv_quals, declarator, std_attributes);
7659 return declarator;
7662 /* If the next token is a `[', there is a direct-new-declarator. */
7663 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7664 return cp_parser_direct_new_declarator (parser);
7666 return NULL;
7669 /* Parse a direct-new-declarator.
7671 direct-new-declarator:
7672 [ expression ]
7673 direct-new-declarator [constant-expression]
7677 static cp_declarator *
7678 cp_parser_direct_new_declarator (cp_parser* parser)
7680 cp_declarator *declarator = NULL;
7682 while (true)
7684 tree expression;
7685 cp_token *token;
7687 /* Look for the opening `['. */
7688 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7690 token = cp_lexer_peek_token (parser->lexer);
7691 expression = cp_parser_expression (parser);
7692 /* The standard requires that the expression have integral
7693 type. DR 74 adds enumeration types. We believe that the
7694 real intent is that these expressions be handled like the
7695 expression in a `switch' condition, which also allows
7696 classes with a single conversion to integral or
7697 enumeration type. */
7698 if (!processing_template_decl)
7700 expression
7701 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7702 expression,
7703 /*complain=*/true);
7704 if (!expression)
7706 error_at (token->location,
7707 "expression in new-declarator must have integral "
7708 "or enumeration type");
7709 expression = error_mark_node;
7713 /* Look for the closing `]'. */
7714 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7716 /* Add this bound to the declarator. */
7717 declarator = make_array_declarator (declarator, expression);
7719 /* If the next token is not a `[', then there are no more
7720 bounds. */
7721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7722 break;
7725 return declarator;
7728 /* Parse a new-initializer.
7730 new-initializer:
7731 ( expression-list [opt] )
7732 braced-init-list
7734 Returns a representation of the expression-list. */
7736 static vec<tree, va_gc> *
7737 cp_parser_new_initializer (cp_parser* parser)
7739 vec<tree, va_gc> *expression_list;
7741 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7743 tree t;
7744 bool expr_non_constant_p;
7745 cp_lexer_set_source_position (parser->lexer);
7746 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7747 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7748 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7749 expression_list = make_tree_vector_single (t);
7751 else
7752 expression_list = (cp_parser_parenthesized_expression_list
7753 (parser, non_attr, /*cast_p=*/false,
7754 /*allow_expansion_p=*/true,
7755 /*non_constant_p=*/NULL));
7757 return expression_list;
7760 /* Parse a delete-expression.
7762 delete-expression:
7763 :: [opt] delete cast-expression
7764 :: [opt] delete [ ] cast-expression
7766 Returns a representation of the expression. */
7768 static tree
7769 cp_parser_delete_expression (cp_parser* parser)
7771 bool global_scope_p;
7772 bool array_p;
7773 tree expression;
7775 /* Look for the optional `::' operator. */
7776 global_scope_p
7777 = (cp_parser_global_scope_opt (parser,
7778 /*current_scope_valid_p=*/false)
7779 != NULL_TREE);
7780 /* Look for the `delete' keyword. */
7781 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7782 /* See if the array syntax is in use. */
7783 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7785 /* Consume the `[' token. */
7786 cp_lexer_consume_token (parser->lexer);
7787 /* Look for the `]' token. */
7788 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7789 /* Remember that this is the `[]' construct. */
7790 array_p = true;
7792 else
7793 array_p = false;
7795 /* Parse the cast-expression. */
7796 expression = cp_parser_simple_cast_expression (parser);
7798 /* A delete-expression may not appear in an integral constant
7799 expression. */
7800 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7801 return error_mark_node;
7803 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7804 tf_warning_or_error);
7807 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7808 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7809 0 otherwise. */
7811 static int
7812 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7814 cp_token *token = cp_lexer_peek_token (parser->lexer);
7815 switch (token->type)
7817 case CPP_COMMA:
7818 case CPP_SEMICOLON:
7819 case CPP_QUERY:
7820 case CPP_COLON:
7821 case CPP_CLOSE_SQUARE:
7822 case CPP_CLOSE_PAREN:
7823 case CPP_CLOSE_BRACE:
7824 case CPP_OPEN_BRACE:
7825 case CPP_DOT:
7826 case CPP_DOT_STAR:
7827 case CPP_DEREF:
7828 case CPP_DEREF_STAR:
7829 case CPP_DIV:
7830 case CPP_MOD:
7831 case CPP_LSHIFT:
7832 case CPP_RSHIFT:
7833 case CPP_LESS:
7834 case CPP_GREATER:
7835 case CPP_LESS_EQ:
7836 case CPP_GREATER_EQ:
7837 case CPP_EQ_EQ:
7838 case CPP_NOT_EQ:
7839 case CPP_EQ:
7840 case CPP_MULT_EQ:
7841 case CPP_DIV_EQ:
7842 case CPP_MOD_EQ:
7843 case CPP_PLUS_EQ:
7844 case CPP_MINUS_EQ:
7845 case CPP_RSHIFT_EQ:
7846 case CPP_LSHIFT_EQ:
7847 case CPP_AND_EQ:
7848 case CPP_XOR_EQ:
7849 case CPP_OR_EQ:
7850 case CPP_XOR:
7851 case CPP_OR:
7852 case CPP_OR_OR:
7853 case CPP_EOF:
7854 case CPP_ELLIPSIS:
7855 return 0;
7857 case CPP_OPEN_PAREN:
7858 /* In ((type ()) () the last () isn't a valid cast-expression,
7859 so the whole must be parsed as postfix-expression. */
7860 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7861 != CPP_CLOSE_PAREN;
7863 case CPP_OPEN_SQUARE:
7864 /* '[' may start a primary-expression in obj-c++ and in C++11,
7865 as a lambda-expression, eg, '(void)[]{}'. */
7866 if (cxx_dialect >= cxx11)
7867 return -1;
7868 return c_dialect_objc ();
7870 case CPP_PLUS_PLUS:
7871 case CPP_MINUS_MINUS:
7872 /* '++' and '--' may or may not start a cast-expression:
7874 struct T { void operator++(int); };
7875 void f() { (T())++; }
7879 int a;
7880 (int)++a; */
7881 return -1;
7883 default:
7884 return 1;
7888 /* Parse a cast-expression.
7890 cast-expression:
7891 unary-expression
7892 ( type-id ) cast-expression
7894 ADDRESS_P is true iff the unary-expression is appearing as the
7895 operand of the `&' operator. CAST_P is true if this expression is
7896 the target of a cast.
7898 Returns a representation of the expression. */
7900 static tree
7901 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7902 bool decltype_p, cp_id_kind * pidk)
7904 /* If it's a `(', then we might be looking at a cast. */
7905 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7907 tree type = NULL_TREE;
7908 tree expr = NULL_TREE;
7909 int cast_expression = 0;
7910 const char *saved_message;
7912 /* There's no way to know yet whether or not this is a cast.
7913 For example, `(int (3))' is a unary-expression, while `(int)
7914 3' is a cast. So, we resort to parsing tentatively. */
7915 cp_parser_parse_tentatively (parser);
7916 /* Types may not be defined in a cast. */
7917 saved_message = parser->type_definition_forbidden_message;
7918 parser->type_definition_forbidden_message
7919 = G_("types may not be defined in casts");
7920 /* Consume the `('. */
7921 cp_lexer_consume_token (parser->lexer);
7922 /* A very tricky bit is that `(struct S) { 3 }' is a
7923 compound-literal (which we permit in C++ as an extension).
7924 But, that construct is not a cast-expression -- it is a
7925 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7926 is legal; if the compound-literal were a cast-expression,
7927 you'd need an extra set of parentheses.) But, if we parse
7928 the type-id, and it happens to be a class-specifier, then we
7929 will commit to the parse at that point, because we cannot
7930 undo the action that is done when creating a new class. So,
7931 then we cannot back up and do a postfix-expression.
7933 Another tricky case is the following (c++/29234):
7935 struct S { void operator () (); };
7937 void foo ()
7939 ( S()() );
7942 As a type-id we parse the parenthesized S()() as a function
7943 returning a function, groktypename complains and we cannot
7944 back up in this case either.
7946 Therefore, we scan ahead to the closing `)', and check to see
7947 if the tokens after the `)' can start a cast-expression. Otherwise
7948 we are dealing with an unary-expression, a postfix-expression
7949 or something else.
7951 Yet another tricky case, in C++11, is the following (c++/54891):
7953 (void)[]{};
7955 The issue is that usually, besides the case of lambda-expressions,
7956 the parenthesized type-id cannot be followed by '[', and, eg, we
7957 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7958 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7959 we don't commit, we try a cast-expression, then an unary-expression.
7961 Save tokens so that we can put them back. */
7962 cp_lexer_save_tokens (parser->lexer);
7964 /* We may be looking at a cast-expression. */
7965 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7966 /*consume_paren=*/true))
7967 cast_expression
7968 = cp_parser_tokens_start_cast_expression (parser);
7970 /* Roll back the tokens we skipped. */
7971 cp_lexer_rollback_tokens (parser->lexer);
7972 /* If we aren't looking at a cast-expression, simulate an error so
7973 that the call to cp_parser_error_occurred below returns true. */
7974 if (!cast_expression)
7975 cp_parser_simulate_error (parser);
7976 else
7978 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7979 parser->in_type_id_in_expr_p = true;
7980 /* Look for the type-id. */
7981 type = cp_parser_type_id (parser);
7982 /* Look for the closing `)'. */
7983 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7984 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7987 /* Restore the saved message. */
7988 parser->type_definition_forbidden_message = saved_message;
7990 /* At this point this can only be either a cast or a
7991 parenthesized ctor such as `(T ())' that looks like a cast to
7992 function returning T. */
7993 if (!cp_parser_error_occurred (parser))
7995 /* Only commit if the cast-expression doesn't start with
7996 '++', '--', or '[' in C++11. */
7997 if (cast_expression > 0)
7998 cp_parser_commit_to_topmost_tentative_parse (parser);
8000 expr = cp_parser_cast_expression (parser,
8001 /*address_p=*/false,
8002 /*cast_p=*/true,
8003 /*decltype_p=*/false,
8004 pidk);
8006 if (cp_parser_parse_definitely (parser))
8008 /* Warn about old-style casts, if so requested. */
8009 if (warn_old_style_cast
8010 && !in_system_header_at (input_location)
8011 && !VOID_TYPE_P (type)
8012 && current_lang_name != lang_name_c)
8013 warning (OPT_Wold_style_cast, "use of old-style cast");
8015 /* Only type conversions to integral or enumeration types
8016 can be used in constant-expressions. */
8017 if (!cast_valid_in_integral_constant_expression_p (type)
8018 && cp_parser_non_integral_constant_expression (parser,
8019 NIC_CAST))
8020 return error_mark_node;
8022 /* Perform the cast. */
8023 expr = build_c_cast (input_location, type, expr);
8024 return expr;
8027 else
8028 cp_parser_abort_tentative_parse (parser);
8031 /* If we get here, then it's not a cast, so it must be a
8032 unary-expression. */
8033 return cp_parser_unary_expression (parser, pidk, address_p,
8034 cast_p, decltype_p);
8037 /* Parse a binary expression of the general form:
8039 pm-expression:
8040 cast-expression
8041 pm-expression .* cast-expression
8042 pm-expression ->* cast-expression
8044 multiplicative-expression:
8045 pm-expression
8046 multiplicative-expression * pm-expression
8047 multiplicative-expression / pm-expression
8048 multiplicative-expression % pm-expression
8050 additive-expression:
8051 multiplicative-expression
8052 additive-expression + multiplicative-expression
8053 additive-expression - multiplicative-expression
8055 shift-expression:
8056 additive-expression
8057 shift-expression << additive-expression
8058 shift-expression >> additive-expression
8060 relational-expression:
8061 shift-expression
8062 relational-expression < shift-expression
8063 relational-expression > shift-expression
8064 relational-expression <= shift-expression
8065 relational-expression >= shift-expression
8067 GNU Extension:
8069 relational-expression:
8070 relational-expression <? shift-expression
8071 relational-expression >? shift-expression
8073 equality-expression:
8074 relational-expression
8075 equality-expression == relational-expression
8076 equality-expression != relational-expression
8078 and-expression:
8079 equality-expression
8080 and-expression & equality-expression
8082 exclusive-or-expression:
8083 and-expression
8084 exclusive-or-expression ^ and-expression
8086 inclusive-or-expression:
8087 exclusive-or-expression
8088 inclusive-or-expression | exclusive-or-expression
8090 logical-and-expression:
8091 inclusive-or-expression
8092 logical-and-expression && inclusive-or-expression
8094 logical-or-expression:
8095 logical-and-expression
8096 logical-or-expression || logical-and-expression
8098 All these are implemented with a single function like:
8100 binary-expression:
8101 simple-cast-expression
8102 binary-expression <token> binary-expression
8104 CAST_P is true if this expression is the target of a cast.
8106 The binops_by_token map is used to get the tree codes for each <token> type.
8107 binary-expressions are associated according to a precedence table. */
8109 #define TOKEN_PRECEDENCE(token) \
8110 (((token->type == CPP_GREATER \
8111 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8112 && !parser->greater_than_is_operator_p) \
8113 ? PREC_NOT_OPERATOR \
8114 : binops_by_token[token->type].prec)
8116 static tree
8117 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8118 bool no_toplevel_fold_p,
8119 bool decltype_p,
8120 enum cp_parser_prec prec,
8121 cp_id_kind * pidk)
8123 cp_parser_expression_stack stack;
8124 cp_parser_expression_stack_entry *sp = &stack[0];
8125 cp_parser_expression_stack_entry current;
8126 tree rhs;
8127 cp_token *token;
8128 enum tree_code rhs_type;
8129 enum cp_parser_prec new_prec, lookahead_prec;
8130 tree overload;
8132 /* Parse the first expression. */
8133 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8134 ? TRUTH_NOT_EXPR : ERROR_MARK);
8135 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8136 cast_p, decltype_p, pidk);
8137 current.prec = prec;
8139 if (cp_parser_error_occurred (parser))
8140 return error_mark_node;
8142 for (;;)
8144 /* Get an operator token. */
8145 token = cp_lexer_peek_token (parser->lexer);
8147 if (warn_cxx0x_compat
8148 && token->type == CPP_RSHIFT
8149 && !parser->greater_than_is_operator_p)
8151 if (warning_at (token->location, OPT_Wc__0x_compat,
8152 "%<>>%> operator is treated"
8153 " as two right angle brackets in C++11"))
8154 inform (token->location,
8155 "suggest parentheses around %<>>%> expression");
8158 new_prec = TOKEN_PRECEDENCE (token);
8160 /* Popping an entry off the stack means we completed a subexpression:
8161 - either we found a token which is not an operator (`>' where it is not
8162 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8163 will happen repeatedly;
8164 - or, we found an operator which has lower priority. This is the case
8165 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8166 parsing `3 * 4'. */
8167 if (new_prec <= current.prec)
8169 if (sp == stack)
8170 break;
8171 else
8172 goto pop;
8175 get_rhs:
8176 current.tree_type = binops_by_token[token->type].tree_type;
8177 current.loc = token->location;
8179 /* We used the operator token. */
8180 cp_lexer_consume_token (parser->lexer);
8182 /* For "false && x" or "true || x", x will never be executed;
8183 disable warnings while evaluating it. */
8184 if (current.tree_type == TRUTH_ANDIF_EXPR)
8185 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8186 else if (current.tree_type == TRUTH_ORIF_EXPR)
8187 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8189 /* Extract another operand. It may be the RHS of this expression
8190 or the LHS of a new, higher priority expression. */
8191 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8192 ? TRUTH_NOT_EXPR : ERROR_MARK);
8193 rhs = cp_parser_simple_cast_expression (parser);
8195 /* Get another operator token. Look up its precedence to avoid
8196 building a useless (immediately popped) stack entry for common
8197 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8198 token = cp_lexer_peek_token (parser->lexer);
8199 lookahead_prec = TOKEN_PRECEDENCE (token);
8200 if (lookahead_prec > new_prec)
8202 /* ... and prepare to parse the RHS of the new, higher priority
8203 expression. Since precedence levels on the stack are
8204 monotonically increasing, we do not have to care about
8205 stack overflows. */
8206 *sp = current;
8207 ++sp;
8208 current.lhs = rhs;
8209 current.lhs_type = rhs_type;
8210 current.prec = new_prec;
8211 new_prec = lookahead_prec;
8212 goto get_rhs;
8214 pop:
8215 lookahead_prec = new_prec;
8216 /* If the stack is not empty, we have parsed into LHS the right side
8217 (`4' in the example above) of an expression we had suspended.
8218 We can use the information on the stack to recover the LHS (`3')
8219 from the stack together with the tree code (`MULT_EXPR'), and
8220 the precedence of the higher level subexpression
8221 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8222 which will be used to actually build the additive expression. */
8223 rhs = current.lhs;
8224 rhs_type = current.lhs_type;
8225 --sp;
8226 current = *sp;
8229 /* Undo the disabling of warnings done above. */
8230 if (current.tree_type == TRUTH_ANDIF_EXPR)
8231 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8232 else if (current.tree_type == TRUTH_ORIF_EXPR)
8233 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8235 if (warn_logical_not_paren
8236 && current.lhs_type == TRUTH_NOT_EXPR)
8237 warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8239 overload = NULL;
8240 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8241 ERROR_MARK for everything that is not a binary expression.
8242 This makes warn_about_parentheses miss some warnings that
8243 involve unary operators. For unary expressions we should
8244 pass the correct tree_code unless the unary expression was
8245 surrounded by parentheses.
8247 if (no_toplevel_fold_p
8248 && lookahead_prec <= current.prec
8249 && sp == stack)
8250 current.lhs = build2 (current.tree_type,
8251 TREE_CODE_CLASS (current.tree_type)
8252 == tcc_comparison
8253 ? boolean_type_node : TREE_TYPE (current.lhs),
8254 current.lhs, rhs);
8255 else
8256 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8257 current.lhs, current.lhs_type,
8258 rhs, rhs_type, &overload,
8259 complain_flags (decltype_p));
8260 current.lhs_type = current.tree_type;
8261 if (EXPR_P (current.lhs))
8262 SET_EXPR_LOCATION (current.lhs, current.loc);
8264 /* If the binary operator required the use of an overloaded operator,
8265 then this expression cannot be an integral constant-expression.
8266 An overloaded operator can be used even if both operands are
8267 otherwise permissible in an integral constant-expression if at
8268 least one of the operands is of enumeration type. */
8270 if (overload
8271 && cp_parser_non_integral_constant_expression (parser,
8272 NIC_OVERLOADED))
8273 return error_mark_node;
8276 return current.lhs;
8279 static tree
8280 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8281 bool no_toplevel_fold_p,
8282 enum cp_parser_prec prec,
8283 cp_id_kind * pidk)
8285 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8286 /*decltype*/false, prec, pidk);
8289 /* Parse the `? expression : assignment-expression' part of a
8290 conditional-expression. The LOGICAL_OR_EXPR is the
8291 logical-or-expression that started the conditional-expression.
8292 Returns a representation of the entire conditional-expression.
8294 This routine is used by cp_parser_assignment_expression.
8296 ? expression : assignment-expression
8298 GNU Extensions:
8300 ? : assignment-expression */
8302 static tree
8303 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8305 tree expr;
8306 tree assignment_expr;
8307 struct cp_token *token;
8308 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8310 /* Consume the `?' token. */
8311 cp_lexer_consume_token (parser->lexer);
8312 token = cp_lexer_peek_token (parser->lexer);
8313 if (cp_parser_allow_gnu_extensions_p (parser)
8314 && token->type == CPP_COLON)
8316 pedwarn (token->location, OPT_Wpedantic,
8317 "ISO C++ does not allow ?: with omitted middle operand");
8318 /* Implicit true clause. */
8319 expr = NULL_TREE;
8320 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8321 warn_for_omitted_condop (token->location, logical_or_expr);
8323 else
8325 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8326 parser->colon_corrects_to_scope_p = false;
8327 /* Parse the expression. */
8328 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8329 expr = cp_parser_expression (parser);
8330 c_inhibit_evaluation_warnings +=
8331 ((logical_or_expr == truthvalue_true_node)
8332 - (logical_or_expr == truthvalue_false_node));
8333 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8336 /* The next token should be a `:'. */
8337 cp_parser_require (parser, CPP_COLON, RT_COLON);
8338 /* Parse the assignment-expression. */
8339 assignment_expr = cp_parser_assignment_expression (parser);
8340 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8342 /* Build the conditional-expression. */
8343 return build_x_conditional_expr (loc, logical_or_expr,
8344 expr,
8345 assignment_expr,
8346 tf_warning_or_error);
8349 /* Parse an assignment-expression.
8351 assignment-expression:
8352 conditional-expression
8353 logical-or-expression assignment-operator assignment_expression
8354 throw-expression
8356 CAST_P is true if this expression is the target of a cast.
8357 DECLTYPE_P is true if this expression is the operand of decltype.
8359 Returns a representation for the expression. */
8361 static tree
8362 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8363 bool cast_p, bool decltype_p)
8365 tree expr;
8367 /* If the next token is the `throw' keyword, then we're looking at
8368 a throw-expression. */
8369 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8370 expr = cp_parser_throw_expression (parser);
8371 /* Otherwise, it must be that we are looking at a
8372 logical-or-expression. */
8373 else
8375 /* Parse the binary expressions (logical-or-expression). */
8376 expr = cp_parser_binary_expression (parser, cast_p, false,
8377 decltype_p,
8378 PREC_NOT_OPERATOR, pidk);
8379 /* If the next token is a `?' then we're actually looking at a
8380 conditional-expression. */
8381 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8382 return cp_parser_question_colon_clause (parser, expr);
8383 else
8385 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8387 /* If it's an assignment-operator, we're using the second
8388 production. */
8389 enum tree_code assignment_operator
8390 = cp_parser_assignment_operator_opt (parser);
8391 if (assignment_operator != ERROR_MARK)
8393 bool non_constant_p;
8394 location_t saved_input_location;
8396 /* Parse the right-hand side of the assignment. */
8397 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8399 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8400 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8402 /* An assignment may not appear in a
8403 constant-expression. */
8404 if (cp_parser_non_integral_constant_expression (parser,
8405 NIC_ASSIGNMENT))
8406 return error_mark_node;
8407 /* Build the assignment expression. Its default
8408 location is the location of the '=' token. */
8409 saved_input_location = input_location;
8410 input_location = loc;
8411 expr = build_x_modify_expr (loc, expr,
8412 assignment_operator,
8413 rhs,
8414 complain_flags (decltype_p));
8415 input_location = saved_input_location;
8420 return expr;
8423 /* Parse an (optional) assignment-operator.
8425 assignment-operator: one of
8426 = *= /= %= += -= >>= <<= &= ^= |=
8428 GNU Extension:
8430 assignment-operator: one of
8431 <?= >?=
8433 If the next token is an assignment operator, the corresponding tree
8434 code is returned, and the token is consumed. For example, for
8435 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8436 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8437 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8438 operator, ERROR_MARK is returned. */
8440 static enum tree_code
8441 cp_parser_assignment_operator_opt (cp_parser* parser)
8443 enum tree_code op;
8444 cp_token *token;
8446 /* Peek at the next token. */
8447 token = cp_lexer_peek_token (parser->lexer);
8449 switch (token->type)
8451 case CPP_EQ:
8452 op = NOP_EXPR;
8453 break;
8455 case CPP_MULT_EQ:
8456 op = MULT_EXPR;
8457 break;
8459 case CPP_DIV_EQ:
8460 op = TRUNC_DIV_EXPR;
8461 break;
8463 case CPP_MOD_EQ:
8464 op = TRUNC_MOD_EXPR;
8465 break;
8467 case CPP_PLUS_EQ:
8468 op = PLUS_EXPR;
8469 break;
8471 case CPP_MINUS_EQ:
8472 op = MINUS_EXPR;
8473 break;
8475 case CPP_RSHIFT_EQ:
8476 op = RSHIFT_EXPR;
8477 break;
8479 case CPP_LSHIFT_EQ:
8480 op = LSHIFT_EXPR;
8481 break;
8483 case CPP_AND_EQ:
8484 op = BIT_AND_EXPR;
8485 break;
8487 case CPP_XOR_EQ:
8488 op = BIT_XOR_EXPR;
8489 break;
8491 case CPP_OR_EQ:
8492 op = BIT_IOR_EXPR;
8493 break;
8495 default:
8496 /* Nothing else is an assignment operator. */
8497 op = ERROR_MARK;
8500 /* If it was an assignment operator, consume it. */
8501 if (op != ERROR_MARK)
8502 cp_lexer_consume_token (parser->lexer);
8504 return op;
8507 /* Parse an expression.
8509 expression:
8510 assignment-expression
8511 expression , assignment-expression
8513 CAST_P is true if this expression is the target of a cast.
8514 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8515 except possibly parenthesized or on the RHS of a comma (N3276).
8517 Returns a representation of the expression. */
8519 static tree
8520 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8521 bool cast_p, bool decltype_p)
8523 tree expression = NULL_TREE;
8524 location_t loc = UNKNOWN_LOCATION;
8526 while (true)
8528 tree assignment_expression;
8530 /* Parse the next assignment-expression. */
8531 assignment_expression
8532 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8534 /* We don't create a temporary for a call that is the immediate operand
8535 of decltype or on the RHS of a comma. But when we see a comma, we
8536 need to create a temporary for a call on the LHS. */
8537 if (decltype_p && !processing_template_decl
8538 && TREE_CODE (assignment_expression) == CALL_EXPR
8539 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8540 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8541 assignment_expression
8542 = build_cplus_new (TREE_TYPE (assignment_expression),
8543 assignment_expression, tf_warning_or_error);
8545 /* If this is the first assignment-expression, we can just
8546 save it away. */
8547 if (!expression)
8548 expression = assignment_expression;
8549 else
8550 expression = build_x_compound_expr (loc, expression,
8551 assignment_expression,
8552 complain_flags (decltype_p));
8553 /* If the next token is not a comma, then we are done with the
8554 expression. */
8555 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8556 break;
8557 /* Consume the `,'. */
8558 loc = cp_lexer_peek_token (parser->lexer)->location;
8559 cp_lexer_consume_token (parser->lexer);
8560 /* A comma operator cannot appear in a constant-expression. */
8561 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8562 expression = error_mark_node;
8565 return expression;
8568 /* Parse a constant-expression.
8570 constant-expression:
8571 conditional-expression
8573 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8574 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8575 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8576 is false, NON_CONSTANT_P should be NULL. */
8578 static tree
8579 cp_parser_constant_expression (cp_parser* parser,
8580 bool allow_non_constant_p,
8581 bool *non_constant_p)
8583 bool saved_integral_constant_expression_p;
8584 bool saved_allow_non_integral_constant_expression_p;
8585 bool saved_non_integral_constant_expression_p;
8586 tree expression;
8588 /* It might seem that we could simply parse the
8589 conditional-expression, and then check to see if it were
8590 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8591 one that the compiler can figure out is constant, possibly after
8592 doing some simplifications or optimizations. The standard has a
8593 precise definition of constant-expression, and we must honor
8594 that, even though it is somewhat more restrictive.
8596 For example:
8598 int i[(2, 3)];
8600 is not a legal declaration, because `(2, 3)' is not a
8601 constant-expression. The `,' operator is forbidden in a
8602 constant-expression. However, GCC's constant-folding machinery
8603 will fold this operation to an INTEGER_CST for `3'. */
8605 /* Save the old settings. */
8606 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8607 saved_allow_non_integral_constant_expression_p
8608 = parser->allow_non_integral_constant_expression_p;
8609 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8610 /* We are now parsing a constant-expression. */
8611 parser->integral_constant_expression_p = true;
8612 parser->allow_non_integral_constant_expression_p
8613 = (allow_non_constant_p || cxx_dialect >= cxx11);
8614 parser->non_integral_constant_expression_p = false;
8615 /* Although the grammar says "conditional-expression", we parse an
8616 "assignment-expression", which also permits "throw-expression"
8617 and the use of assignment operators. In the case that
8618 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8619 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8620 actually essential that we look for an assignment-expression.
8621 For example, cp_parser_initializer_clauses uses this function to
8622 determine whether a particular assignment-expression is in fact
8623 constant. */
8624 expression = cp_parser_assignment_expression (parser);
8625 /* Restore the old settings. */
8626 parser->integral_constant_expression_p
8627 = saved_integral_constant_expression_p;
8628 parser->allow_non_integral_constant_expression_p
8629 = saved_allow_non_integral_constant_expression_p;
8630 if (cxx_dialect >= cxx11)
8632 /* Require an rvalue constant expression here; that's what our
8633 callers expect. Reference constant expressions are handled
8634 separately in e.g. cp_parser_template_argument. */
8635 bool is_const = potential_rvalue_constant_expression (expression);
8636 parser->non_integral_constant_expression_p = !is_const;
8637 if (!is_const && !allow_non_constant_p)
8638 require_potential_rvalue_constant_expression (expression);
8640 if (allow_non_constant_p)
8641 *non_constant_p = parser->non_integral_constant_expression_p;
8642 parser->non_integral_constant_expression_p
8643 = saved_non_integral_constant_expression_p;
8645 return expression;
8648 /* Parse __builtin_offsetof.
8650 offsetof-expression:
8651 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8653 offsetof-member-designator:
8654 id-expression
8655 | offsetof-member-designator "." id-expression
8656 | offsetof-member-designator "[" expression "]"
8657 | offsetof-member-designator "->" id-expression */
8659 static tree
8660 cp_parser_builtin_offsetof (cp_parser *parser)
8662 int save_ice_p, save_non_ice_p;
8663 tree type, expr;
8664 cp_id_kind dummy;
8665 cp_token *token;
8667 /* We're about to accept non-integral-constant things, but will
8668 definitely yield an integral constant expression. Save and
8669 restore these values around our local parsing. */
8670 save_ice_p = parser->integral_constant_expression_p;
8671 save_non_ice_p = parser->non_integral_constant_expression_p;
8673 /* Consume the "__builtin_offsetof" token. */
8674 cp_lexer_consume_token (parser->lexer);
8675 /* Consume the opening `('. */
8676 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8677 /* Parse the type-id. */
8678 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8679 type = cp_parser_type_id (parser);
8680 /* Look for the `,'. */
8681 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8682 token = cp_lexer_peek_token (parser->lexer);
8684 /* Build the (type *)null that begins the traditional offsetof macro. */
8685 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8686 tf_warning_or_error);
8688 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8689 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8690 true, &dummy, token->location);
8691 while (true)
8693 token = cp_lexer_peek_token (parser->lexer);
8694 switch (token->type)
8696 case CPP_OPEN_SQUARE:
8697 /* offsetof-member-designator "[" expression "]" */
8698 expr = cp_parser_postfix_open_square_expression (parser, expr,
8699 true, false);
8700 break;
8702 case CPP_DEREF:
8703 /* offsetof-member-designator "->" identifier */
8704 expr = grok_array_decl (token->location, expr,
8705 integer_zero_node, false);
8706 /* FALLTHRU */
8708 case CPP_DOT:
8709 /* offsetof-member-designator "." identifier */
8710 cp_lexer_consume_token (parser->lexer);
8711 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8712 expr, true, &dummy,
8713 token->location);
8714 break;
8716 case CPP_CLOSE_PAREN:
8717 /* Consume the ")" token. */
8718 cp_lexer_consume_token (parser->lexer);
8719 goto success;
8721 default:
8722 /* Error. We know the following require will fail, but
8723 that gives the proper error message. */
8724 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8725 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8726 expr = error_mark_node;
8727 goto failure;
8731 success:
8732 /* If we're processing a template, we can't finish the semantics yet.
8733 Otherwise we can fold the entire expression now. */
8734 if (processing_template_decl)
8736 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8737 SET_EXPR_LOCATION (expr, loc);
8739 else
8740 expr = finish_offsetof (expr, loc);
8742 failure:
8743 parser->integral_constant_expression_p = save_ice_p;
8744 parser->non_integral_constant_expression_p = save_non_ice_p;
8746 return expr;
8749 /* Parse a trait expression.
8751 Returns a representation of the expression, the underlying type
8752 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8754 static tree
8755 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8757 cp_trait_kind kind;
8758 tree type1, type2 = NULL_TREE;
8759 bool binary = false;
8760 bool variadic = false;
8762 switch (keyword)
8764 case RID_HAS_NOTHROW_ASSIGN:
8765 kind = CPTK_HAS_NOTHROW_ASSIGN;
8766 break;
8767 case RID_HAS_NOTHROW_CONSTRUCTOR:
8768 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8769 break;
8770 case RID_HAS_NOTHROW_COPY:
8771 kind = CPTK_HAS_NOTHROW_COPY;
8772 break;
8773 case RID_HAS_TRIVIAL_ASSIGN:
8774 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8775 break;
8776 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8777 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8778 break;
8779 case RID_HAS_TRIVIAL_COPY:
8780 kind = CPTK_HAS_TRIVIAL_COPY;
8781 break;
8782 case RID_HAS_TRIVIAL_DESTRUCTOR:
8783 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8784 break;
8785 case RID_HAS_VIRTUAL_DESTRUCTOR:
8786 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8787 break;
8788 case RID_IS_ABSTRACT:
8789 kind = CPTK_IS_ABSTRACT;
8790 break;
8791 case RID_IS_BASE_OF:
8792 kind = CPTK_IS_BASE_OF;
8793 binary = true;
8794 break;
8795 case RID_IS_CLASS:
8796 kind = CPTK_IS_CLASS;
8797 break;
8798 case RID_IS_EMPTY:
8799 kind = CPTK_IS_EMPTY;
8800 break;
8801 case RID_IS_ENUM:
8802 kind = CPTK_IS_ENUM;
8803 break;
8804 case RID_IS_FINAL:
8805 kind = CPTK_IS_FINAL;
8806 break;
8807 case RID_IS_LITERAL_TYPE:
8808 kind = CPTK_IS_LITERAL_TYPE;
8809 break;
8810 case RID_IS_POD:
8811 kind = CPTK_IS_POD;
8812 break;
8813 case RID_IS_POLYMORPHIC:
8814 kind = CPTK_IS_POLYMORPHIC;
8815 break;
8816 case RID_IS_STD_LAYOUT:
8817 kind = CPTK_IS_STD_LAYOUT;
8818 break;
8819 case RID_IS_TRIVIAL:
8820 kind = CPTK_IS_TRIVIAL;
8821 break;
8822 case RID_IS_TRIVIALLY_ASSIGNABLE:
8823 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8824 binary = true;
8825 break;
8826 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8827 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8828 variadic = true;
8829 break;
8830 case RID_IS_TRIVIALLY_COPYABLE:
8831 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8832 break;
8833 case RID_IS_UNION:
8834 kind = CPTK_IS_UNION;
8835 break;
8836 case RID_UNDERLYING_TYPE:
8837 kind = CPTK_UNDERLYING_TYPE;
8838 break;
8839 case RID_BASES:
8840 kind = CPTK_BASES;
8841 break;
8842 case RID_DIRECT_BASES:
8843 kind = CPTK_DIRECT_BASES;
8844 break;
8845 default:
8846 gcc_unreachable ();
8849 /* Consume the token. */
8850 cp_lexer_consume_token (parser->lexer);
8852 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8854 type1 = cp_parser_type_id (parser);
8856 if (type1 == error_mark_node)
8857 return error_mark_node;
8859 if (binary)
8861 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8863 type2 = cp_parser_type_id (parser);
8865 if (type2 == error_mark_node)
8866 return error_mark_node;
8868 else if (variadic)
8870 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8872 cp_lexer_consume_token (parser->lexer);
8873 tree elt = cp_parser_type_id (parser);
8874 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8876 cp_lexer_consume_token (parser->lexer);
8877 elt = make_pack_expansion (elt);
8879 if (elt == error_mark_node)
8880 return error_mark_node;
8881 type2 = tree_cons (NULL_TREE, elt, type2);
8885 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8887 /* Complete the trait expression, which may mean either processing
8888 the trait expr now or saving it for template instantiation. */
8889 switch(kind)
8891 case CPTK_UNDERLYING_TYPE:
8892 return finish_underlying_type (type1);
8893 case CPTK_BASES:
8894 return finish_bases (type1, false);
8895 case CPTK_DIRECT_BASES:
8896 return finish_bases (type1, true);
8897 default:
8898 return finish_trait_expr (kind, type1, type2);
8902 /* Lambdas that appear in variable initializer or default argument scope
8903 get that in their mangling, so we need to record it. We might as well
8904 use the count for function and namespace scopes as well. */
8905 static GTY(()) tree lambda_scope;
8906 static GTY(()) int lambda_count;
8907 typedef struct GTY(()) tree_int
8909 tree t;
8910 int i;
8911 } tree_int;
8912 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8914 static void
8915 start_lambda_scope (tree decl)
8917 tree_int ti;
8918 gcc_assert (decl);
8919 /* Once we're inside a function, we ignore other scopes and just push
8920 the function again so that popping works properly. */
8921 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8922 decl = current_function_decl;
8923 ti.t = lambda_scope;
8924 ti.i = lambda_count;
8925 vec_safe_push (lambda_scope_stack, ti);
8926 if (lambda_scope != decl)
8928 /* Don't reset the count if we're still in the same function. */
8929 lambda_scope = decl;
8930 lambda_count = 0;
8934 static void
8935 record_lambda_scope (tree lambda)
8937 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8938 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8941 static void
8942 finish_lambda_scope (void)
8944 tree_int *p = &lambda_scope_stack->last ();
8945 if (lambda_scope != p->t)
8947 lambda_scope = p->t;
8948 lambda_count = p->i;
8950 lambda_scope_stack->pop ();
8953 /* Parse a lambda expression.
8955 lambda-expression:
8956 lambda-introducer lambda-declarator [opt] compound-statement
8958 Returns a representation of the expression. */
8960 static tree
8961 cp_parser_lambda_expression (cp_parser* parser)
8963 tree lambda_expr = build_lambda_expr ();
8964 tree type;
8965 bool ok = true;
8966 cp_token *token = cp_lexer_peek_token (parser->lexer);
8967 cp_token_position start = 0;
8969 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8971 if (cp_unevaluated_operand)
8973 if (!token->error_reported)
8975 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8976 "lambda-expression in unevaluated context");
8977 token->error_reported = true;
8979 ok = false;
8981 else if (parser->in_template_argument_list_p)
8983 if (!token->error_reported)
8985 error_at (token->location, "lambda-expression in template-argument");
8986 token->error_reported = true;
8988 ok = false;
8991 /* We may be in the middle of deferred access check. Disable
8992 it now. */
8993 push_deferring_access_checks (dk_no_deferred);
8995 cp_parser_lambda_introducer (parser, lambda_expr);
8997 type = begin_lambda_type (lambda_expr);
8998 if (type == error_mark_node)
8999 return error_mark_node;
9001 record_lambda_scope (lambda_expr);
9003 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9004 determine_visibility (TYPE_NAME (type));
9006 /* Now that we've started the type, add the capture fields for any
9007 explicit captures. */
9008 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9011 /* Inside the class, surrounding template-parameter-lists do not apply. */
9012 unsigned int saved_num_template_parameter_lists
9013 = parser->num_template_parameter_lists;
9014 unsigned char in_statement = parser->in_statement;
9015 bool in_switch_statement_p = parser->in_switch_statement_p;
9016 bool fully_implicit_function_template_p
9017 = parser->fully_implicit_function_template_p;
9018 tree implicit_template_parms = parser->implicit_template_parms;
9019 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9020 bool auto_is_implicit_function_template_parm_p
9021 = parser->auto_is_implicit_function_template_parm_p;
9023 parser->num_template_parameter_lists = 0;
9024 parser->in_statement = 0;
9025 parser->in_switch_statement_p = false;
9026 parser->fully_implicit_function_template_p = false;
9027 parser->implicit_template_parms = 0;
9028 parser->implicit_template_scope = 0;
9029 parser->auto_is_implicit_function_template_parm_p = false;
9031 /* By virtue of defining a local class, a lambda expression has access to
9032 the private variables of enclosing classes. */
9034 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9036 if (ok)
9038 if (!cp_parser_error_occurred (parser)
9039 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9040 && cp_parser_start_tentative_firewall (parser))
9041 start = token;
9042 cp_parser_lambda_body (parser, lambda_expr);
9044 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9046 if (cp_parser_skip_to_closing_brace (parser))
9047 cp_lexer_consume_token (parser->lexer);
9050 /* The capture list was built up in reverse order; fix that now. */
9051 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9052 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9054 if (ok)
9055 maybe_add_lambda_conv_op (type);
9057 type = finish_struct (type, /*attributes=*/NULL_TREE);
9059 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9060 parser->in_statement = in_statement;
9061 parser->in_switch_statement_p = in_switch_statement_p;
9062 parser->fully_implicit_function_template_p
9063 = fully_implicit_function_template_p;
9064 parser->implicit_template_parms = implicit_template_parms;
9065 parser->implicit_template_scope = implicit_template_scope;
9066 parser->auto_is_implicit_function_template_parm_p
9067 = auto_is_implicit_function_template_parm_p;
9070 pop_deferring_access_checks ();
9072 /* This field is only used during parsing of the lambda. */
9073 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9075 /* This lambda shouldn't have any proxies left at this point. */
9076 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9077 /* And now that we're done, push proxies for an enclosing lambda. */
9078 insert_pending_capture_proxies ();
9080 if (ok)
9081 lambda_expr = build_lambda_object (lambda_expr);
9082 else
9083 lambda_expr = error_mark_node;
9085 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9087 return lambda_expr;
9090 /* Parse the beginning of a lambda expression.
9092 lambda-introducer:
9093 [ lambda-capture [opt] ]
9095 LAMBDA_EXPR is the current representation of the lambda expression. */
9097 static void
9098 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9100 /* Need commas after the first capture. */
9101 bool first = true;
9103 /* Eat the leading `['. */
9104 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9106 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9107 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9108 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9109 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9110 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9111 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9113 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9115 cp_lexer_consume_token (parser->lexer);
9116 first = false;
9119 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9121 cp_token* capture_token;
9122 tree capture_id;
9123 tree capture_init_expr;
9124 cp_id_kind idk = CP_ID_KIND_NONE;
9125 bool explicit_init_p = false;
9127 enum capture_kind_type
9129 BY_COPY,
9130 BY_REFERENCE
9132 enum capture_kind_type capture_kind = BY_COPY;
9134 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9136 error ("expected end of capture-list");
9137 return;
9140 if (first)
9141 first = false;
9142 else
9143 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9145 /* Possibly capture `this'. */
9146 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9148 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9149 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9150 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9151 "with by-copy capture default");
9152 cp_lexer_consume_token (parser->lexer);
9153 add_capture (lambda_expr,
9154 /*id=*/this_identifier,
9155 /*initializer=*/finish_this_expr(),
9156 /*by_reference_p=*/false,
9157 explicit_init_p);
9158 continue;
9161 /* Remember whether we want to capture as a reference or not. */
9162 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9164 capture_kind = BY_REFERENCE;
9165 cp_lexer_consume_token (parser->lexer);
9168 /* Get the identifier. */
9169 capture_token = cp_lexer_peek_token (parser->lexer);
9170 capture_id = cp_parser_identifier (parser);
9172 if (capture_id == error_mark_node)
9173 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9174 delimiters, but I modified this to stop on unnested ']' as well. It
9175 was already changed to stop on unnested '}', so the
9176 "closing_parenthesis" name is no more misleading with my change. */
9178 cp_parser_skip_to_closing_parenthesis (parser,
9179 /*recovering=*/true,
9180 /*or_comma=*/true,
9181 /*consume_paren=*/true);
9182 break;
9185 /* Find the initializer for this capture. */
9186 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9187 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9188 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9190 bool direct, non_constant;
9191 /* An explicit initializer exists. */
9192 if (cxx_dialect < cxx14)
9193 pedwarn (input_location, 0,
9194 "lambda capture initializers "
9195 "only available with -std=c++14 or -std=gnu++14");
9196 capture_init_expr = cp_parser_initializer (parser, &direct,
9197 &non_constant);
9198 explicit_init_p = true;
9199 if (capture_init_expr == NULL_TREE)
9201 error ("empty initializer for lambda init-capture");
9202 capture_init_expr = error_mark_node;
9205 else
9207 const char* error_msg;
9209 /* Turn the identifier into an id-expression. */
9210 capture_init_expr
9211 = cp_parser_lookup_name_simple (parser, capture_id,
9212 capture_token->location);
9214 if (capture_init_expr == error_mark_node)
9216 unqualified_name_lookup_error (capture_id);
9217 continue;
9219 else if (DECL_P (capture_init_expr)
9220 && (!VAR_P (capture_init_expr)
9221 && TREE_CODE (capture_init_expr) != PARM_DECL))
9223 error_at (capture_token->location,
9224 "capture of non-variable %qD ",
9225 capture_init_expr);
9226 inform (0, "%q+#D declared here", capture_init_expr);
9227 continue;
9229 if (VAR_P (capture_init_expr)
9230 && decl_storage_duration (capture_init_expr) != dk_auto)
9232 if (pedwarn (capture_token->location, 0, "capture of variable "
9233 "%qD with non-automatic storage duration",
9234 capture_init_expr))
9235 inform (0, "%q+#D declared here", capture_init_expr);
9236 continue;
9239 capture_init_expr
9240 = finish_id_expression
9241 (capture_id,
9242 capture_init_expr,
9243 parser->scope,
9244 &idk,
9245 /*integral_constant_expression_p=*/false,
9246 /*allow_non_integral_constant_expression_p=*/false,
9247 /*non_integral_constant_expression_p=*/NULL,
9248 /*template_p=*/false,
9249 /*done=*/true,
9250 /*address_p=*/false,
9251 /*template_arg_p=*/false,
9252 &error_msg,
9253 capture_token->location);
9255 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9257 cp_lexer_consume_token (parser->lexer);
9258 capture_init_expr = make_pack_expansion (capture_init_expr);
9260 else
9261 check_for_bare_parameter_packs (capture_init_expr);
9264 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9265 && !explicit_init_p)
9267 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9268 && capture_kind == BY_COPY)
9269 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9270 "of %qD redundant with by-copy capture default",
9271 capture_id);
9272 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9273 && capture_kind == BY_REFERENCE)
9274 pedwarn (capture_token->location, 0, "explicit by-reference "
9275 "capture of %qD redundant with by-reference capture "
9276 "default", capture_id);
9279 add_capture (lambda_expr,
9280 capture_id,
9281 capture_init_expr,
9282 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9283 explicit_init_p);
9286 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9289 /* Parse the (optional) middle of a lambda expression.
9291 lambda-declarator:
9292 < template-parameter-list [opt] >
9293 ( parameter-declaration-clause [opt] )
9294 attribute-specifier [opt]
9295 mutable [opt]
9296 exception-specification [opt]
9297 lambda-return-type-clause [opt]
9299 LAMBDA_EXPR is the current representation of the lambda expression. */
9301 static bool
9302 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9304 /* 5.1.1.4 of the standard says:
9305 If a lambda-expression does not include a lambda-declarator, it is as if
9306 the lambda-declarator were ().
9307 This means an empty parameter list, no attributes, and no exception
9308 specification. */
9309 tree param_list = void_list_node;
9310 tree attributes = NULL_TREE;
9311 tree exception_spec = NULL_TREE;
9312 tree template_param_list = NULL_TREE;
9314 /* The template-parameter-list is optional, but must begin with
9315 an opening angle if present. */
9316 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9318 if (cxx_dialect < cxx14)
9319 pedwarn (parser->lexer->next_token->location, 0,
9320 "lambda templates are only available with "
9321 "-std=c++14 or -std=gnu++14");
9323 cp_lexer_consume_token (parser->lexer);
9325 template_param_list = cp_parser_template_parameter_list (parser);
9327 cp_parser_skip_to_end_of_template_parameter_list (parser);
9329 /* We just processed one more parameter list. */
9330 ++parser->num_template_parameter_lists;
9333 /* The parameter-declaration-clause is optional (unless
9334 template-parameter-list was given), but must begin with an
9335 opening parenthesis if present. */
9336 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9338 cp_lexer_consume_token (parser->lexer);
9340 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9342 /* Parse parameters. */
9343 param_list = cp_parser_parameter_declaration_clause (parser);
9345 /* Default arguments shall not be specified in the
9346 parameter-declaration-clause of a lambda-declarator. */
9347 for (tree t = param_list; t; t = TREE_CHAIN (t))
9348 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9349 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9350 "default argument specified for lambda parameter");
9352 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9354 attributes = cp_parser_attributes_opt (parser);
9356 /* Parse optional `mutable' keyword. */
9357 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9359 cp_lexer_consume_token (parser->lexer);
9360 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9363 /* Parse optional exception specification. */
9364 exception_spec = cp_parser_exception_specification_opt (parser);
9366 /* Parse optional trailing return type. */
9367 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9369 cp_lexer_consume_token (parser->lexer);
9370 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9371 = cp_parser_trailing_type_id (parser);
9374 /* The function parameters must be in scope all the way until after the
9375 trailing-return-type in case of decltype. */
9376 pop_bindings_and_leave_scope ();
9378 else if (template_param_list != NULL_TREE) // generate diagnostic
9379 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9381 /* Create the function call operator.
9383 Messing with declarators like this is no uglier than building up the
9384 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9385 other code. */
9387 cp_decl_specifier_seq return_type_specs;
9388 cp_declarator* declarator;
9389 tree fco;
9390 int quals;
9391 void *p;
9393 clear_decl_specs (&return_type_specs);
9394 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9395 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9396 else
9397 /* Maybe we will deduce the return type later. */
9398 return_type_specs.type = make_auto ();
9400 p = obstack_alloc (&declarator_obstack, 0);
9402 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9403 sfk_none);
9405 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9406 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9407 declarator = make_call_declarator (declarator, param_list, quals,
9408 VIRT_SPEC_UNSPECIFIED,
9409 REF_QUAL_NONE,
9410 exception_spec,
9411 /*late_return_type=*/NULL_TREE);
9412 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9414 fco = grokmethod (&return_type_specs,
9415 declarator,
9416 attributes);
9417 if (fco != error_mark_node)
9419 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9420 DECL_ARTIFICIAL (fco) = 1;
9421 /* Give the object parameter a different name. */
9422 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9423 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9424 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9426 if (template_param_list)
9428 fco = finish_member_template_decl (fco);
9429 finish_template_decl (template_param_list);
9430 --parser->num_template_parameter_lists;
9432 else if (parser->fully_implicit_function_template_p)
9433 fco = finish_fully_implicit_template (parser, fco);
9435 finish_member_declaration (fco);
9437 obstack_free (&declarator_obstack, p);
9439 return (fco != error_mark_node);
9443 /* Parse the body of a lambda expression, which is simply
9445 compound-statement
9447 but which requires special handling.
9448 LAMBDA_EXPR is the current representation of the lambda expression. */
9450 static void
9451 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9453 bool nested = (current_function_decl != NULL_TREE);
9454 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9455 if (nested)
9456 push_function_context ();
9457 else
9458 /* Still increment function_depth so that we don't GC in the
9459 middle of an expression. */
9460 ++function_depth;
9461 /* Clear this in case we're in the middle of a default argument. */
9462 parser->local_variables_forbidden_p = false;
9464 /* Finish the function call operator
9465 - class_specifier
9466 + late_parsing_for_member
9467 + function_definition_after_declarator
9468 + ctor_initializer_opt_and_function_body */
9470 tree fco = lambda_function (lambda_expr);
9471 tree body;
9472 bool done = false;
9473 tree compound_stmt;
9474 tree cap;
9476 /* Let the front end know that we are going to be defining this
9477 function. */
9478 start_preparsed_function (fco,
9479 NULL_TREE,
9480 SF_PRE_PARSED | SF_INCLASS_INLINE);
9482 start_lambda_scope (fco);
9483 body = begin_function_body ();
9485 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9486 goto out;
9488 /* Push the proxies for any explicit captures. */
9489 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9490 cap = TREE_CHAIN (cap))
9491 build_capture_proxy (TREE_PURPOSE (cap));
9493 compound_stmt = begin_compound_stmt (0);
9495 /* 5.1.1.4 of the standard says:
9496 If a lambda-expression does not include a trailing-return-type, it
9497 is as if the trailing-return-type denotes the following type:
9498 * if the compound-statement is of the form
9499 { return attribute-specifier [opt] expression ; }
9500 the type of the returned expression after lvalue-to-rvalue
9501 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9502 (_conv.array_ 4.2), and function-to-pointer conversion
9503 (_conv.func_ 4.3);
9504 * otherwise, void. */
9506 /* In a lambda that has neither a lambda-return-type-clause
9507 nor a deducible form, errors should be reported for return statements
9508 in the body. Since we used void as the placeholder return type, parsing
9509 the body as usual will give such desired behavior. */
9510 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9511 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9512 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9514 tree expr = NULL_TREE;
9515 cp_id_kind idk = CP_ID_KIND_NONE;
9517 /* Parse tentatively in case there's more after the initial return
9518 statement. */
9519 cp_parser_parse_tentatively (parser);
9521 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9523 expr = cp_parser_expression (parser, &idk);
9525 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9526 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9528 if (cp_parser_parse_definitely (parser))
9530 if (!processing_template_decl)
9531 apply_deduced_return_type (fco, lambda_return_type (expr));
9533 /* Will get error here if type not deduced yet. */
9534 finish_return_stmt (expr);
9536 done = true;
9540 if (!done)
9542 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9543 cp_parser_label_declaration (parser);
9544 cp_parser_statement_seq_opt (parser, NULL_TREE);
9545 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9548 finish_compound_stmt (compound_stmt);
9550 out:
9551 finish_function_body (body);
9552 finish_lambda_scope ();
9554 /* Finish the function and generate code for it if necessary. */
9555 tree fn = finish_function (/*inline*/2);
9557 /* Only expand if the call op is not a template. */
9558 if (!DECL_TEMPLATE_INFO (fco))
9559 expand_or_defer_fn (fn);
9562 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9563 if (nested)
9564 pop_function_context();
9565 else
9566 --function_depth;
9569 /* Statements [gram.stmt.stmt] */
9571 /* Parse a statement.
9573 statement:
9574 labeled-statement
9575 expression-statement
9576 compound-statement
9577 selection-statement
9578 iteration-statement
9579 jump-statement
9580 declaration-statement
9581 try-block
9583 C++11:
9585 statement:
9586 labeled-statement
9587 attribute-specifier-seq (opt) expression-statement
9588 attribute-specifier-seq (opt) compound-statement
9589 attribute-specifier-seq (opt) selection-statement
9590 attribute-specifier-seq (opt) iteration-statement
9591 attribute-specifier-seq (opt) jump-statement
9592 declaration-statement
9593 attribute-specifier-seq (opt) try-block
9595 TM Extension:
9597 statement:
9598 atomic-statement
9600 IN_COMPOUND is true when the statement is nested inside a
9601 cp_parser_compound_statement; this matters for certain pragmas.
9603 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9604 is a (possibly labeled) if statement which is not enclosed in braces
9605 and has an else clause. This is used to implement -Wparentheses. */
9607 static void
9608 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9609 bool in_compound, bool *if_p)
9611 tree statement, std_attrs = NULL_TREE;
9612 cp_token *token;
9613 location_t statement_location, attrs_location;
9615 restart:
9616 if (if_p != NULL)
9617 *if_p = false;
9618 /* There is no statement yet. */
9619 statement = NULL_TREE;
9621 saved_token_sentinel saved_tokens (parser->lexer);
9622 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9623 if (c_dialect_objc ())
9624 /* In obj-c++, seeing '[[' might be the either the beginning of
9625 c++11 attributes, or a nested objc-message-expression. So
9626 let's parse the c++11 attributes tentatively. */
9627 cp_parser_parse_tentatively (parser);
9628 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9629 if (c_dialect_objc ())
9631 if (!cp_parser_parse_definitely (parser))
9632 std_attrs = NULL_TREE;
9635 /* Peek at the next token. */
9636 token = cp_lexer_peek_token (parser->lexer);
9637 /* Remember the location of the first token in the statement. */
9638 statement_location = token->location;
9639 /* If this is a keyword, then that will often determine what kind of
9640 statement we have. */
9641 if (token->type == CPP_KEYWORD)
9643 enum rid keyword = token->keyword;
9645 switch (keyword)
9647 case RID_CASE:
9648 case RID_DEFAULT:
9649 /* Looks like a labeled-statement with a case label.
9650 Parse the label, and then use tail recursion to parse
9651 the statement. */
9652 cp_parser_label_for_labeled_statement (parser, std_attrs);
9653 goto restart;
9655 case RID_IF:
9656 case RID_SWITCH:
9657 statement = cp_parser_selection_statement (parser, if_p);
9658 break;
9660 case RID_WHILE:
9661 case RID_DO:
9662 case RID_FOR:
9663 statement = cp_parser_iteration_statement (parser, false);
9664 break;
9666 case RID_CILK_FOR:
9667 if (!flag_cilkplus)
9669 error_at (cp_lexer_peek_token (parser->lexer)->location,
9670 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9671 cp_lexer_consume_token (parser->lexer);
9672 statement = error_mark_node;
9674 else
9675 statement = cp_parser_cilk_for (parser, integer_zero_node);
9676 break;
9678 case RID_BREAK:
9679 case RID_CONTINUE:
9680 case RID_RETURN:
9681 case RID_GOTO:
9682 statement = cp_parser_jump_statement (parser);
9683 break;
9685 case RID_CILK_SYNC:
9686 cp_lexer_consume_token (parser->lexer);
9687 if (flag_cilkplus)
9689 tree sync_expr = build_cilk_sync ();
9690 SET_EXPR_LOCATION (sync_expr,
9691 token->location);
9692 statement = finish_expr_stmt (sync_expr);
9694 else
9696 error_at (token->location, "-fcilkplus must be enabled to use"
9697 " %<_Cilk_sync%>");
9698 statement = error_mark_node;
9700 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9701 break;
9703 /* Objective-C++ exception-handling constructs. */
9704 case RID_AT_TRY:
9705 case RID_AT_CATCH:
9706 case RID_AT_FINALLY:
9707 case RID_AT_SYNCHRONIZED:
9708 case RID_AT_THROW:
9709 statement = cp_parser_objc_statement (parser);
9710 break;
9712 case RID_TRY:
9713 statement = cp_parser_try_block (parser);
9714 break;
9716 case RID_NAMESPACE:
9717 /* This must be a namespace alias definition. */
9718 cp_parser_declaration_statement (parser);
9719 return;
9721 case RID_TRANSACTION_ATOMIC:
9722 case RID_TRANSACTION_RELAXED:
9723 statement = cp_parser_transaction (parser, keyword);
9724 break;
9725 case RID_TRANSACTION_CANCEL:
9726 statement = cp_parser_transaction_cancel (parser);
9727 break;
9729 default:
9730 /* It might be a keyword like `int' that can start a
9731 declaration-statement. */
9732 break;
9735 else if (token->type == CPP_NAME)
9737 /* If the next token is a `:', then we are looking at a
9738 labeled-statement. */
9739 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9740 if (token->type == CPP_COLON)
9742 /* Looks like a labeled-statement with an ordinary label.
9743 Parse the label, and then use tail recursion to parse
9744 the statement. */
9746 cp_parser_label_for_labeled_statement (parser, std_attrs);
9747 goto restart;
9750 /* Anything that starts with a `{' must be a compound-statement. */
9751 else if (token->type == CPP_OPEN_BRACE)
9752 statement = cp_parser_compound_statement (parser, NULL, false, false);
9753 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9754 a statement all its own. */
9755 else if (token->type == CPP_PRAGMA)
9757 /* Only certain OpenMP pragmas are attached to statements, and thus
9758 are considered statements themselves. All others are not. In
9759 the context of a compound, accept the pragma as a "statement" and
9760 return so that we can check for a close brace. Otherwise we
9761 require a real statement and must go back and read one. */
9762 if (in_compound)
9763 cp_parser_pragma (parser, pragma_compound);
9764 else if (!cp_parser_pragma (parser, pragma_stmt))
9765 goto restart;
9766 return;
9768 else if (token->type == CPP_EOF)
9770 cp_parser_error (parser, "expected statement");
9771 return;
9774 /* Everything else must be a declaration-statement or an
9775 expression-statement. Try for the declaration-statement
9776 first, unless we are looking at a `;', in which case we know that
9777 we have an expression-statement. */
9778 if (!statement)
9780 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9782 if (std_attrs != NULL_TREE)
9784 /* Attributes should be parsed as part of the the
9785 declaration, so let's un-parse them. */
9786 saved_tokens.rollback();
9787 std_attrs = NULL_TREE;
9790 cp_parser_parse_tentatively (parser);
9791 /* Try to parse the declaration-statement. */
9792 cp_parser_declaration_statement (parser);
9793 /* If that worked, we're done. */
9794 if (cp_parser_parse_definitely (parser))
9795 return;
9797 /* Look for an expression-statement instead. */
9798 statement = cp_parser_expression_statement (parser, in_statement_expr);
9801 /* Set the line number for the statement. */
9802 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9803 SET_EXPR_LOCATION (statement, statement_location);
9805 /* Note that for now, we don't do anything with c++11 statements
9806 parsed at this level. */
9807 if (std_attrs != NULL_TREE)
9808 warning_at (attrs_location,
9809 OPT_Wattributes,
9810 "attributes at the beginning of statement are ignored");
9813 /* Parse the label for a labeled-statement, i.e.
9815 identifier :
9816 case constant-expression :
9817 default :
9819 GNU Extension:
9820 case constant-expression ... constant-expression : statement
9822 When a label is parsed without errors, the label is added to the
9823 parse tree by the finish_* functions, so this function doesn't
9824 have to return the label. */
9826 static void
9827 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9829 cp_token *token;
9830 tree label = NULL_TREE;
9831 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9833 /* The next token should be an identifier. */
9834 token = cp_lexer_peek_token (parser->lexer);
9835 if (token->type != CPP_NAME
9836 && token->type != CPP_KEYWORD)
9838 cp_parser_error (parser, "expected labeled-statement");
9839 return;
9842 parser->colon_corrects_to_scope_p = false;
9843 switch (token->keyword)
9845 case RID_CASE:
9847 tree expr, expr_hi;
9848 cp_token *ellipsis;
9850 /* Consume the `case' token. */
9851 cp_lexer_consume_token (parser->lexer);
9852 /* Parse the constant-expression. */
9853 expr = cp_parser_constant_expression (parser);
9854 if (check_for_bare_parameter_packs (expr))
9855 expr = error_mark_node;
9857 ellipsis = cp_lexer_peek_token (parser->lexer);
9858 if (ellipsis->type == CPP_ELLIPSIS)
9860 /* Consume the `...' token. */
9861 cp_lexer_consume_token (parser->lexer);
9862 expr_hi = cp_parser_constant_expression (parser);
9863 if (check_for_bare_parameter_packs (expr_hi))
9864 expr_hi = error_mark_node;
9866 /* We don't need to emit warnings here, as the common code
9867 will do this for us. */
9869 else
9870 expr_hi = NULL_TREE;
9872 if (parser->in_switch_statement_p)
9873 finish_case_label (token->location, expr, expr_hi);
9874 else
9875 error_at (token->location,
9876 "case label %qE not within a switch statement",
9877 expr);
9879 break;
9881 case RID_DEFAULT:
9882 /* Consume the `default' token. */
9883 cp_lexer_consume_token (parser->lexer);
9885 if (parser->in_switch_statement_p)
9886 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9887 else
9888 error_at (token->location, "case label not within a switch statement");
9889 break;
9891 default:
9892 /* Anything else must be an ordinary label. */
9893 label = finish_label_stmt (cp_parser_identifier (parser));
9894 break;
9897 /* Require the `:' token. */
9898 cp_parser_require (parser, CPP_COLON, RT_COLON);
9900 /* An ordinary label may optionally be followed by attributes.
9901 However, this is only permitted if the attributes are then
9902 followed by a semicolon. This is because, for backward
9903 compatibility, when parsing
9904 lab: __attribute__ ((unused)) int i;
9905 we want the attribute to attach to "i", not "lab". */
9906 if (label != NULL_TREE
9907 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9909 tree attrs;
9910 cp_parser_parse_tentatively (parser);
9911 attrs = cp_parser_gnu_attributes_opt (parser);
9912 if (attrs == NULL_TREE
9913 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9914 cp_parser_abort_tentative_parse (parser);
9915 else if (!cp_parser_parse_definitely (parser))
9917 else
9918 attributes = chainon (attributes, attrs);
9921 if (attributes != NULL_TREE)
9922 cplus_decl_attributes (&label, attributes, 0);
9924 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9927 /* Parse an expression-statement.
9929 expression-statement:
9930 expression [opt] ;
9932 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9933 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9934 indicates whether this expression-statement is part of an
9935 expression statement. */
9937 static tree
9938 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9940 tree statement = NULL_TREE;
9941 cp_token *token = cp_lexer_peek_token (parser->lexer);
9943 /* If the next token is a ';', then there is no expression
9944 statement. */
9945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9947 statement = cp_parser_expression (parser);
9948 if (statement == error_mark_node
9949 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9951 cp_parser_skip_to_end_of_block_or_statement (parser);
9952 return error_mark_node;
9956 /* Give a helpful message for "A<T>::type t;" and the like. */
9957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9958 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9960 if (TREE_CODE (statement) == SCOPE_REF)
9961 error_at (token->location, "need %<typename%> before %qE because "
9962 "%qT is a dependent scope",
9963 statement, TREE_OPERAND (statement, 0));
9964 else if (is_overloaded_fn (statement)
9965 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9967 /* A::A a; */
9968 tree fn = get_first_fn (statement);
9969 error_at (token->location,
9970 "%<%T::%D%> names the constructor, not the type",
9971 DECL_CONTEXT (fn), DECL_NAME (fn));
9975 /* Consume the final `;'. */
9976 cp_parser_consume_semicolon_at_end_of_statement (parser);
9978 if (in_statement_expr
9979 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9980 /* This is the final expression statement of a statement
9981 expression. */
9982 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9983 else if (statement)
9984 statement = finish_expr_stmt (statement);
9986 return statement;
9989 /* Parse a compound-statement.
9991 compound-statement:
9992 { statement-seq [opt] }
9994 GNU extension:
9996 compound-statement:
9997 { label-declaration-seq [opt] statement-seq [opt] }
9999 label-declaration-seq:
10000 label-declaration
10001 label-declaration-seq label-declaration
10003 Returns a tree representing the statement. */
10005 static tree
10006 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10007 bool in_try, bool function_body)
10009 tree compound_stmt;
10011 /* Consume the `{'. */
10012 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10013 return error_mark_node;
10014 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10015 && !function_body && cxx_dialect < cxx14)
10016 pedwarn (input_location, OPT_Wpedantic,
10017 "compound-statement in constexpr function");
10018 /* Begin the compound-statement. */
10019 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10020 /* If the next keyword is `__label__' we have a label declaration. */
10021 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10022 cp_parser_label_declaration (parser);
10023 /* Parse an (optional) statement-seq. */
10024 cp_parser_statement_seq_opt (parser, in_statement_expr);
10025 /* Finish the compound-statement. */
10026 finish_compound_stmt (compound_stmt);
10027 /* Consume the `}'. */
10028 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10030 return compound_stmt;
10033 /* Parse an (optional) statement-seq.
10035 statement-seq:
10036 statement
10037 statement-seq [opt] statement */
10039 static void
10040 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10042 /* Scan statements until there aren't any more. */
10043 while (true)
10045 cp_token *token = cp_lexer_peek_token (parser->lexer);
10047 /* If we are looking at a `}', then we have run out of
10048 statements; the same is true if we have reached the end
10049 of file, or have stumbled upon a stray '@end'. */
10050 if (token->type == CPP_CLOSE_BRACE
10051 || token->type == CPP_EOF
10052 || token->type == CPP_PRAGMA_EOL
10053 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10054 break;
10056 /* If we are in a compound statement and find 'else' then
10057 something went wrong. */
10058 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10060 if (parser->in_statement & IN_IF_STMT)
10061 break;
10062 else
10064 token = cp_lexer_consume_token (parser->lexer);
10065 error_at (token->location, "%<else%> without a previous %<if%>");
10069 /* Parse the statement. */
10070 cp_parser_statement (parser, in_statement_expr, true, NULL);
10074 /* Parse a selection-statement.
10076 selection-statement:
10077 if ( condition ) statement
10078 if ( condition ) statement else statement
10079 switch ( condition ) statement
10081 Returns the new IF_STMT or SWITCH_STMT.
10083 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10084 is a (possibly labeled) if statement which is not enclosed in
10085 braces and has an else clause. This is used to implement
10086 -Wparentheses. */
10088 static tree
10089 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10091 cp_token *token;
10092 enum rid keyword;
10094 if (if_p != NULL)
10095 *if_p = false;
10097 /* Peek at the next token. */
10098 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10100 /* See what kind of keyword it is. */
10101 keyword = token->keyword;
10102 switch (keyword)
10104 case RID_IF:
10105 case RID_SWITCH:
10107 tree statement;
10108 tree condition;
10110 /* Look for the `('. */
10111 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10113 cp_parser_skip_to_end_of_statement (parser);
10114 return error_mark_node;
10117 /* Begin the selection-statement. */
10118 if (keyword == RID_IF)
10119 statement = begin_if_stmt ();
10120 else
10121 statement = begin_switch_stmt ();
10123 /* Parse the condition. */
10124 condition = cp_parser_condition (parser);
10125 /* Look for the `)'. */
10126 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10127 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10128 /*consume_paren=*/true);
10130 if (keyword == RID_IF)
10132 bool nested_if;
10133 unsigned char in_statement;
10135 /* Add the condition. */
10136 finish_if_stmt_cond (condition, statement);
10138 /* Parse the then-clause. */
10139 in_statement = parser->in_statement;
10140 parser->in_statement |= IN_IF_STMT;
10141 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10143 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10144 add_stmt (build_empty_stmt (loc));
10145 cp_lexer_consume_token (parser->lexer);
10146 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10147 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10148 "empty body in an %<if%> statement");
10149 nested_if = false;
10151 else
10152 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10153 parser->in_statement = in_statement;
10155 finish_then_clause (statement);
10157 /* If the next token is `else', parse the else-clause. */
10158 if (cp_lexer_next_token_is_keyword (parser->lexer,
10159 RID_ELSE))
10161 /* Consume the `else' keyword. */
10162 cp_lexer_consume_token (parser->lexer);
10163 begin_else_clause (statement);
10164 /* Parse the else-clause. */
10165 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10167 location_t loc;
10168 loc = cp_lexer_peek_token (parser->lexer)->location;
10169 warning_at (loc,
10170 OPT_Wempty_body, "suggest braces around "
10171 "empty body in an %<else%> statement");
10172 add_stmt (build_empty_stmt (loc));
10173 cp_lexer_consume_token (parser->lexer);
10175 else
10176 cp_parser_implicitly_scoped_statement (parser, NULL);
10178 finish_else_clause (statement);
10180 /* If we are currently parsing a then-clause, then
10181 IF_P will not be NULL. We set it to true to
10182 indicate that this if statement has an else clause.
10183 This may trigger the Wparentheses warning below
10184 when we get back up to the parent if statement. */
10185 if (if_p != NULL)
10186 *if_p = true;
10188 else
10190 /* This if statement does not have an else clause. If
10191 NESTED_IF is true, then the then-clause is an if
10192 statement which does have an else clause. We warn
10193 about the potential ambiguity. */
10194 if (nested_if)
10195 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10196 "suggest explicit braces to avoid ambiguous"
10197 " %<else%>");
10200 /* Now we're all done with the if-statement. */
10201 finish_if_stmt (statement);
10203 else
10205 bool in_switch_statement_p;
10206 unsigned char in_statement;
10208 /* Add the condition. */
10209 finish_switch_cond (condition, statement);
10211 /* Parse the body of the switch-statement. */
10212 in_switch_statement_p = parser->in_switch_statement_p;
10213 in_statement = parser->in_statement;
10214 parser->in_switch_statement_p = true;
10215 parser->in_statement |= IN_SWITCH_STMT;
10216 cp_parser_implicitly_scoped_statement (parser, NULL);
10217 parser->in_switch_statement_p = in_switch_statement_p;
10218 parser->in_statement = in_statement;
10220 /* Now we're all done with the switch-statement. */
10221 finish_switch_stmt (statement);
10224 return statement;
10226 break;
10228 default:
10229 cp_parser_error (parser, "expected selection-statement");
10230 return error_mark_node;
10234 /* Parse a condition.
10236 condition:
10237 expression
10238 type-specifier-seq declarator = initializer-clause
10239 type-specifier-seq declarator braced-init-list
10241 GNU Extension:
10243 condition:
10244 type-specifier-seq declarator asm-specification [opt]
10245 attributes [opt] = assignment-expression
10247 Returns the expression that should be tested. */
10249 static tree
10250 cp_parser_condition (cp_parser* parser)
10252 cp_decl_specifier_seq type_specifiers;
10253 const char *saved_message;
10254 int declares_class_or_enum;
10256 /* Try the declaration first. */
10257 cp_parser_parse_tentatively (parser);
10258 /* New types are not allowed in the type-specifier-seq for a
10259 condition. */
10260 saved_message = parser->type_definition_forbidden_message;
10261 parser->type_definition_forbidden_message
10262 = G_("types may not be defined in conditions");
10263 /* Parse the type-specifier-seq. */
10264 cp_parser_decl_specifier_seq (parser,
10265 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10266 &type_specifiers,
10267 &declares_class_or_enum);
10268 /* Restore the saved message. */
10269 parser->type_definition_forbidden_message = saved_message;
10270 /* If all is well, we might be looking at a declaration. */
10271 if (!cp_parser_error_occurred (parser))
10273 tree decl;
10274 tree asm_specification;
10275 tree attributes;
10276 cp_declarator *declarator;
10277 tree initializer = NULL_TREE;
10279 /* Parse the declarator. */
10280 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10281 /*ctor_dtor_or_conv_p=*/NULL,
10282 /*parenthesized_p=*/NULL,
10283 /*member_p=*/false,
10284 /*friend_p=*/false);
10285 /* Parse the attributes. */
10286 attributes = cp_parser_attributes_opt (parser);
10287 /* Parse the asm-specification. */
10288 asm_specification = cp_parser_asm_specification_opt (parser);
10289 /* If the next token is not an `=' or '{', then we might still be
10290 looking at an expression. For example:
10292 if (A(a).x)
10294 looks like a decl-specifier-seq and a declarator -- but then
10295 there is no `=', so this is an expression. */
10296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10297 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10298 cp_parser_simulate_error (parser);
10300 /* If we did see an `=' or '{', then we are looking at a declaration
10301 for sure. */
10302 if (cp_parser_parse_definitely (parser))
10304 tree pushed_scope;
10305 bool non_constant_p;
10306 bool flags = LOOKUP_ONLYCONVERTING;
10308 /* Create the declaration. */
10309 decl = start_decl (declarator, &type_specifiers,
10310 /*initialized_p=*/true,
10311 attributes, /*prefix_attributes=*/NULL_TREE,
10312 &pushed_scope);
10314 /* Parse the initializer. */
10315 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10317 initializer = cp_parser_braced_list (parser, &non_constant_p);
10318 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10319 flags = 0;
10321 else
10323 /* Consume the `='. */
10324 cp_parser_require (parser, CPP_EQ, RT_EQ);
10325 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10327 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10328 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10330 /* Process the initializer. */
10331 cp_finish_decl (decl,
10332 initializer, !non_constant_p,
10333 asm_specification,
10334 flags);
10336 if (pushed_scope)
10337 pop_scope (pushed_scope);
10339 return convert_from_reference (decl);
10342 /* If we didn't even get past the declarator successfully, we are
10343 definitely not looking at a declaration. */
10344 else
10345 cp_parser_abort_tentative_parse (parser);
10347 /* Otherwise, we are looking at an expression. */
10348 return cp_parser_expression (parser);
10351 /* Parses a for-statement or range-for-statement until the closing ')',
10352 not included. */
10354 static tree
10355 cp_parser_for (cp_parser *parser, bool ivdep)
10357 tree init, scope, decl;
10358 bool is_range_for;
10360 /* Begin the for-statement. */
10361 scope = begin_for_scope (&init);
10363 /* Parse the initialization. */
10364 is_range_for = cp_parser_for_init_statement (parser, &decl);
10366 if (is_range_for)
10367 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10368 else
10369 return cp_parser_c_for (parser, scope, init, ivdep);
10372 static tree
10373 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10375 /* Normal for loop */
10376 tree condition = NULL_TREE;
10377 tree expression = NULL_TREE;
10378 tree stmt;
10380 stmt = begin_for_stmt (scope, init);
10381 /* The for-init-statement has already been parsed in
10382 cp_parser_for_init_statement, so no work is needed here. */
10383 finish_for_init_stmt (stmt);
10385 /* If there's a condition, process it. */
10386 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10387 condition = cp_parser_condition (parser);
10388 else if (ivdep)
10390 cp_parser_error (parser, "missing loop condition in loop with "
10391 "%<GCC ivdep%> pragma");
10392 condition = error_mark_node;
10394 finish_for_cond (condition, stmt, ivdep);
10395 /* Look for the `;'. */
10396 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10398 /* If there's an expression, process it. */
10399 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10400 expression = cp_parser_expression (parser);
10401 finish_for_expr (expression, stmt);
10403 return stmt;
10406 /* Tries to parse a range-based for-statement:
10408 range-based-for:
10409 decl-specifier-seq declarator : expression
10411 The decl-specifier-seq declarator and the `:' are already parsed by
10412 cp_parser_for_init_statement. If processing_template_decl it returns a
10413 newly created RANGE_FOR_STMT; if not, it is converted to a
10414 regular FOR_STMT. */
10416 static tree
10417 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10418 bool ivdep)
10420 tree stmt, range_expr;
10422 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10424 bool expr_non_constant_p;
10425 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10427 else
10428 range_expr = cp_parser_expression (parser);
10430 /* If in template, STMT is converted to a normal for-statement
10431 at instantiation. If not, it is done just ahead. */
10432 if (processing_template_decl)
10434 if (check_for_bare_parameter_packs (range_expr))
10435 range_expr = error_mark_node;
10436 stmt = begin_range_for_stmt (scope, init);
10437 if (ivdep)
10438 RANGE_FOR_IVDEP (stmt) = 1;
10439 finish_range_for_decl (stmt, range_decl, range_expr);
10440 if (!type_dependent_expression_p (range_expr)
10441 /* do_auto_deduction doesn't mess with template init-lists. */
10442 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10443 do_range_for_auto_deduction (range_decl, range_expr);
10445 else
10447 stmt = begin_for_stmt (scope, init);
10448 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10450 return stmt;
10453 /* Subroutine of cp_convert_range_for: given the initializer expression,
10454 builds up the range temporary. */
10456 static tree
10457 build_range_temp (tree range_expr)
10459 tree range_type, range_temp;
10461 /* Find out the type deduced by the declaration
10462 `auto &&__range = range_expr'. */
10463 range_type = cp_build_reference_type (make_auto (), true);
10464 range_type = do_auto_deduction (range_type, range_expr,
10465 type_uses_auto (range_type));
10467 /* Create the __range variable. */
10468 range_temp = build_decl (input_location, VAR_DECL,
10469 get_identifier ("__for_range"), range_type);
10470 TREE_USED (range_temp) = 1;
10471 DECL_ARTIFICIAL (range_temp) = 1;
10473 return range_temp;
10476 /* Used by cp_parser_range_for in template context: we aren't going to
10477 do a full conversion yet, but we still need to resolve auto in the
10478 type of the for-range-declaration if present. This is basically
10479 a shortcut version of cp_convert_range_for. */
10481 static void
10482 do_range_for_auto_deduction (tree decl, tree range_expr)
10484 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10485 if (auto_node)
10487 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10488 range_temp = convert_from_reference (build_range_temp (range_expr));
10489 iter_type = (cp_parser_perform_range_for_lookup
10490 (range_temp, &begin_dummy, &end_dummy));
10491 if (iter_type)
10493 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10494 iter_type);
10495 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10496 tf_warning_or_error);
10497 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10498 iter_decl, auto_node);
10503 /* Converts a range-based for-statement into a normal
10504 for-statement, as per the definition.
10506 for (RANGE_DECL : RANGE_EXPR)
10507 BLOCK
10509 should be equivalent to:
10512 auto &&__range = RANGE_EXPR;
10513 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10514 __begin != __end;
10515 ++__begin)
10517 RANGE_DECL = *__begin;
10518 BLOCK
10522 If RANGE_EXPR is an array:
10523 BEGIN_EXPR = __range
10524 END_EXPR = __range + ARRAY_SIZE(__range)
10525 Else if RANGE_EXPR has a member 'begin' or 'end':
10526 BEGIN_EXPR = __range.begin()
10527 END_EXPR = __range.end()
10528 Else:
10529 BEGIN_EXPR = begin(__range)
10530 END_EXPR = end(__range);
10532 If __range has a member 'begin' but not 'end', or vice versa, we must
10533 still use the second alternative (it will surely fail, however).
10534 When calling begin()/end() in the third alternative we must use
10535 argument dependent lookup, but always considering 'std' as an associated
10536 namespace. */
10538 tree
10539 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10540 bool ivdep)
10542 tree begin, end;
10543 tree iter_type, begin_expr, end_expr;
10544 tree condition, expression;
10546 if (range_decl == error_mark_node || range_expr == error_mark_node)
10547 /* If an error happened previously do nothing or else a lot of
10548 unhelpful errors would be issued. */
10549 begin_expr = end_expr = iter_type = error_mark_node;
10550 else
10552 tree range_temp;
10554 if (TREE_CODE (range_expr) == VAR_DECL
10555 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10556 /* Can't bind a reference to an array of runtime bound. */
10557 range_temp = range_expr;
10558 else
10560 range_temp = build_range_temp (range_expr);
10561 pushdecl (range_temp);
10562 cp_finish_decl (range_temp, range_expr,
10563 /*is_constant_init*/false, NULL_TREE,
10564 LOOKUP_ONLYCONVERTING);
10565 range_temp = convert_from_reference (range_temp);
10567 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10568 &begin_expr, &end_expr);
10571 /* The new for initialization statement. */
10572 begin = build_decl (input_location, VAR_DECL,
10573 get_identifier ("__for_begin"), iter_type);
10574 TREE_USED (begin) = 1;
10575 DECL_ARTIFICIAL (begin) = 1;
10576 pushdecl (begin);
10577 cp_finish_decl (begin, begin_expr,
10578 /*is_constant_init*/false, NULL_TREE,
10579 LOOKUP_ONLYCONVERTING);
10581 end = build_decl (input_location, VAR_DECL,
10582 get_identifier ("__for_end"), iter_type);
10583 TREE_USED (end) = 1;
10584 DECL_ARTIFICIAL (end) = 1;
10585 pushdecl (end);
10586 cp_finish_decl (end, end_expr,
10587 /*is_constant_init*/false, NULL_TREE,
10588 LOOKUP_ONLYCONVERTING);
10590 finish_for_init_stmt (statement);
10592 /* The new for condition. */
10593 condition = build_x_binary_op (input_location, NE_EXPR,
10594 begin, ERROR_MARK,
10595 end, ERROR_MARK,
10596 NULL, tf_warning_or_error);
10597 finish_for_cond (condition, statement, ivdep);
10599 /* The new increment expression. */
10600 expression = finish_unary_op_expr (input_location,
10601 PREINCREMENT_EXPR, begin,
10602 tf_warning_or_error);
10603 finish_for_expr (expression, statement);
10605 /* The declaration is initialized with *__begin inside the loop body. */
10606 cp_finish_decl (range_decl,
10607 build_x_indirect_ref (input_location, begin, RO_NULL,
10608 tf_warning_or_error),
10609 /*is_constant_init*/false, NULL_TREE,
10610 LOOKUP_ONLYCONVERTING);
10612 return statement;
10615 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10616 We need to solve both at the same time because the method used
10617 depends on the existence of members begin or end.
10618 Returns the type deduced for the iterator expression. */
10620 static tree
10621 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10623 if (error_operand_p (range))
10625 *begin = *end = error_mark_node;
10626 return error_mark_node;
10629 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10631 error ("range-based %<for%> expression of type %qT "
10632 "has incomplete type", TREE_TYPE (range));
10633 *begin = *end = error_mark_node;
10634 return error_mark_node;
10636 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10638 /* If RANGE is an array, we will use pointer arithmetic. */
10639 *begin = range;
10640 *end = build_binary_op (input_location, PLUS_EXPR,
10641 range,
10642 array_type_nelts_top (TREE_TYPE (range)),
10644 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10646 else
10648 /* If it is not an array, we must do a bit of magic. */
10649 tree id_begin, id_end;
10650 tree member_begin, member_end;
10652 *begin = *end = error_mark_node;
10654 id_begin = get_identifier ("begin");
10655 id_end = get_identifier ("end");
10656 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10657 /*protect=*/2, /*want_type=*/false,
10658 tf_warning_or_error);
10659 member_end = lookup_member (TREE_TYPE (range), id_end,
10660 /*protect=*/2, /*want_type=*/false,
10661 tf_warning_or_error);
10663 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10665 /* Use the member functions. */
10666 if (member_begin != NULL_TREE)
10667 *begin = cp_parser_range_for_member_function (range, id_begin);
10668 else
10669 error ("range-based %<for%> expression of type %qT has an "
10670 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10672 if (member_end != NULL_TREE)
10673 *end = cp_parser_range_for_member_function (range, id_end);
10674 else
10675 error ("range-based %<for%> expression of type %qT has a "
10676 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10678 else
10680 /* Use global functions with ADL. */
10681 vec<tree, va_gc> *vec;
10682 vec = make_tree_vector ();
10684 vec_safe_push (vec, range);
10686 member_begin = perform_koenig_lookup (id_begin, vec,
10687 tf_warning_or_error);
10688 *begin = finish_call_expr (member_begin, &vec, false, true,
10689 tf_warning_or_error);
10690 member_end = perform_koenig_lookup (id_end, vec,
10691 tf_warning_or_error);
10692 *end = finish_call_expr (member_end, &vec, false, true,
10693 tf_warning_or_error);
10695 release_tree_vector (vec);
10698 /* Last common checks. */
10699 if (*begin == error_mark_node || *end == error_mark_node)
10701 /* If one of the expressions is an error do no more checks. */
10702 *begin = *end = error_mark_node;
10703 return error_mark_node;
10705 else if (type_dependent_expression_p (*begin)
10706 || type_dependent_expression_p (*end))
10707 /* Can happen, when, eg, in a template context, Koenig lookup
10708 can't resolve begin/end (c++/58503). */
10709 return NULL_TREE;
10710 else
10712 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10713 /* The unqualified type of the __begin and __end temporaries should
10714 be the same, as required by the multiple auto declaration. */
10715 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10716 error ("inconsistent begin/end types in range-based %<for%> "
10717 "statement: %qT and %qT",
10718 TREE_TYPE (*begin), TREE_TYPE (*end));
10719 return iter_type;
10724 /* Helper function for cp_parser_perform_range_for_lookup.
10725 Builds a tree for RANGE.IDENTIFIER(). */
10727 static tree
10728 cp_parser_range_for_member_function (tree range, tree identifier)
10730 tree member, res;
10731 vec<tree, va_gc> *vec;
10733 member = finish_class_member_access_expr (range, identifier,
10734 false, tf_warning_or_error);
10735 if (member == error_mark_node)
10736 return error_mark_node;
10738 vec = make_tree_vector ();
10739 res = finish_call_expr (member, &vec,
10740 /*disallow_virtual=*/false,
10741 /*koenig_p=*/false,
10742 tf_warning_or_error);
10743 release_tree_vector (vec);
10744 return res;
10747 /* Parse an iteration-statement.
10749 iteration-statement:
10750 while ( condition ) statement
10751 do statement while ( expression ) ;
10752 for ( for-init-statement condition [opt] ; expression [opt] )
10753 statement
10755 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10757 static tree
10758 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10760 cp_token *token;
10761 enum rid keyword;
10762 tree statement;
10763 unsigned char in_statement;
10765 /* Peek at the next token. */
10766 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10767 if (!token)
10768 return error_mark_node;
10770 /* Remember whether or not we are already within an iteration
10771 statement. */
10772 in_statement = parser->in_statement;
10774 /* See what kind of keyword it is. */
10775 keyword = token->keyword;
10776 switch (keyword)
10778 case RID_WHILE:
10780 tree condition;
10782 /* Begin the while-statement. */
10783 statement = begin_while_stmt ();
10784 /* Look for the `('. */
10785 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10786 /* Parse the condition. */
10787 condition = cp_parser_condition (parser);
10788 finish_while_stmt_cond (condition, statement, ivdep);
10789 /* Look for the `)'. */
10790 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10791 /* Parse the dependent statement. */
10792 parser->in_statement = IN_ITERATION_STMT;
10793 cp_parser_already_scoped_statement (parser);
10794 parser->in_statement = in_statement;
10795 /* We're done with the while-statement. */
10796 finish_while_stmt (statement);
10798 break;
10800 case RID_DO:
10802 tree expression;
10804 /* Begin the do-statement. */
10805 statement = begin_do_stmt ();
10806 /* Parse the body of the do-statement. */
10807 parser->in_statement = IN_ITERATION_STMT;
10808 cp_parser_implicitly_scoped_statement (parser, NULL);
10809 parser->in_statement = in_statement;
10810 finish_do_body (statement);
10811 /* Look for the `while' keyword. */
10812 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10813 /* Look for the `('. */
10814 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10815 /* Parse the expression. */
10816 expression = cp_parser_expression (parser);
10817 /* We're done with the do-statement. */
10818 finish_do_stmt (expression, statement, ivdep);
10819 /* Look for the `)'. */
10820 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10821 /* Look for the `;'. */
10822 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10824 break;
10826 case RID_FOR:
10828 /* Look for the `('. */
10829 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10831 statement = cp_parser_for (parser, ivdep);
10833 /* Look for the `)'. */
10834 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10836 /* Parse the body of the for-statement. */
10837 parser->in_statement = IN_ITERATION_STMT;
10838 cp_parser_already_scoped_statement (parser);
10839 parser->in_statement = in_statement;
10841 /* We're done with the for-statement. */
10842 finish_for_stmt (statement);
10844 break;
10846 default:
10847 cp_parser_error (parser, "expected iteration-statement");
10848 statement = error_mark_node;
10849 break;
10852 return statement;
10855 /* Parse a for-init-statement or the declarator of a range-based-for.
10856 Returns true if a range-based-for declaration is seen.
10858 for-init-statement:
10859 expression-statement
10860 simple-declaration */
10862 static bool
10863 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10865 /* If the next token is a `;', then we have an empty
10866 expression-statement. Grammatically, this is also a
10867 simple-declaration, but an invalid one, because it does not
10868 declare anything. Therefore, if we did not handle this case
10869 specially, we would issue an error message about an invalid
10870 declaration. */
10871 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10873 bool is_range_for = false;
10874 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10876 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10877 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10879 /* N3994 -- for (id : init) ... */
10880 if (cxx_dialect < cxx1z)
10881 pedwarn (input_location, 0, "range-based for loop without a "
10882 "type-specifier only available with "
10883 "-std=c++1z or -std=gnu++1z");
10884 tree name = cp_parser_identifier (parser);
10885 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10886 *decl = build_decl (input_location, VAR_DECL, name, type);
10887 pushdecl (*decl);
10888 cp_lexer_consume_token (parser->lexer);
10889 return true;
10892 /* A colon is used in range-based for. */
10893 parser->colon_corrects_to_scope_p = false;
10895 /* We're going to speculatively look for a declaration, falling back
10896 to an expression, if necessary. */
10897 cp_parser_parse_tentatively (parser);
10898 /* Parse the declaration. */
10899 cp_parser_simple_declaration (parser,
10900 /*function_definition_allowed_p=*/false,
10901 decl);
10902 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10903 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10905 /* It is a range-for, consume the ':' */
10906 cp_lexer_consume_token (parser->lexer);
10907 is_range_for = true;
10908 if (cxx_dialect < cxx11)
10910 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10911 "range-based %<for%> loops only available with "
10912 "-std=c++11 or -std=gnu++11");
10913 *decl = error_mark_node;
10916 else
10917 /* The ';' is not consumed yet because we told
10918 cp_parser_simple_declaration not to. */
10919 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10921 if (cp_parser_parse_definitely (parser))
10922 return is_range_for;
10923 /* If the tentative parse failed, then we shall need to look for an
10924 expression-statement. */
10926 /* If we are here, it is an expression-statement. */
10927 cp_parser_expression_statement (parser, NULL_TREE);
10928 return false;
10931 /* Parse a jump-statement.
10933 jump-statement:
10934 break ;
10935 continue ;
10936 return expression [opt] ;
10937 return braced-init-list ;
10938 goto identifier ;
10940 GNU extension:
10942 jump-statement:
10943 goto * expression ;
10945 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10947 static tree
10948 cp_parser_jump_statement (cp_parser* parser)
10950 tree statement = error_mark_node;
10951 cp_token *token;
10952 enum rid keyword;
10953 unsigned char in_statement;
10955 /* Peek at the next token. */
10956 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10957 if (!token)
10958 return error_mark_node;
10960 /* See what kind of keyword it is. */
10961 keyword = token->keyword;
10962 switch (keyword)
10964 case RID_BREAK:
10965 in_statement = parser->in_statement & ~IN_IF_STMT;
10966 switch (in_statement)
10968 case 0:
10969 error_at (token->location, "break statement not within loop or switch");
10970 break;
10971 default:
10972 gcc_assert ((in_statement & IN_SWITCH_STMT)
10973 || in_statement == IN_ITERATION_STMT);
10974 statement = finish_break_stmt ();
10975 if (in_statement == IN_ITERATION_STMT)
10976 break_maybe_infinite_loop ();
10977 break;
10978 case IN_OMP_BLOCK:
10979 error_at (token->location, "invalid exit from OpenMP structured block");
10980 break;
10981 case IN_OMP_FOR:
10982 error_at (token->location, "break statement used with OpenMP for loop");
10983 break;
10984 case IN_CILK_SIMD_FOR:
10985 error_at (token->location, "break statement used with Cilk Plus for loop");
10986 break;
10988 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10989 break;
10991 case RID_CONTINUE:
10992 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10994 case 0:
10995 error_at (token->location, "continue statement not within a loop");
10996 break;
10997 case IN_CILK_SIMD_FOR:
10998 error_at (token->location,
10999 "continue statement within %<#pragma simd%> loop body");
11000 /* Fall through. */
11001 case IN_ITERATION_STMT:
11002 case IN_OMP_FOR:
11003 statement = finish_continue_stmt ();
11004 break;
11005 case IN_OMP_BLOCK:
11006 error_at (token->location, "invalid exit from OpenMP structured block");
11007 break;
11008 default:
11009 gcc_unreachable ();
11011 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11012 break;
11014 case RID_RETURN:
11016 tree expr;
11017 bool expr_non_constant_p;
11019 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11021 cp_lexer_set_source_position (parser->lexer);
11022 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11023 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11025 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11026 expr = cp_parser_expression (parser);
11027 else
11028 /* If the next token is a `;', then there is no
11029 expression. */
11030 expr = NULL_TREE;
11031 /* Build the return-statement. */
11032 statement = finish_return_stmt (expr);
11033 /* Look for the final `;'. */
11034 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11036 break;
11038 case RID_GOTO:
11039 if (parser->in_function_body
11040 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11042 error ("%<goto%> in %<constexpr%> function");
11043 cp_function_chain->invalid_constexpr = true;
11046 /* Create the goto-statement. */
11047 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11049 /* Issue a warning about this use of a GNU extension. */
11050 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11051 /* Consume the '*' token. */
11052 cp_lexer_consume_token (parser->lexer);
11053 /* Parse the dependent expression. */
11054 finish_goto_stmt (cp_parser_expression (parser));
11056 else
11057 finish_goto_stmt (cp_parser_identifier (parser));
11058 /* Look for the final `;'. */
11059 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11060 break;
11062 default:
11063 cp_parser_error (parser, "expected jump-statement");
11064 break;
11067 return statement;
11070 /* Parse a declaration-statement.
11072 declaration-statement:
11073 block-declaration */
11075 static void
11076 cp_parser_declaration_statement (cp_parser* parser)
11078 void *p;
11080 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11081 p = obstack_alloc (&declarator_obstack, 0);
11083 /* Parse the block-declaration. */
11084 cp_parser_block_declaration (parser, /*statement_p=*/true);
11086 /* Free any declarators allocated. */
11087 obstack_free (&declarator_obstack, p);
11090 /* Some dependent statements (like `if (cond) statement'), are
11091 implicitly in their own scope. In other words, if the statement is
11092 a single statement (as opposed to a compound-statement), it is
11093 none-the-less treated as if it were enclosed in braces. Any
11094 declarations appearing in the dependent statement are out of scope
11095 after control passes that point. This function parses a statement,
11096 but ensures that is in its own scope, even if it is not a
11097 compound-statement.
11099 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11100 is a (possibly labeled) if statement which is not enclosed in
11101 braces and has an else clause. This is used to implement
11102 -Wparentheses.
11104 Returns the new statement. */
11106 static tree
11107 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11109 tree statement;
11111 if (if_p != NULL)
11112 *if_p = false;
11114 /* Mark if () ; with a special NOP_EXPR. */
11115 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11117 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11118 cp_lexer_consume_token (parser->lexer);
11119 statement = add_stmt (build_empty_stmt (loc));
11121 /* if a compound is opened, we simply parse the statement directly. */
11122 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11123 statement = cp_parser_compound_statement (parser, NULL, false, false);
11124 /* If the token is not a `{', then we must take special action. */
11125 else
11127 /* Create a compound-statement. */
11128 statement = begin_compound_stmt (0);
11129 /* Parse the dependent-statement. */
11130 cp_parser_statement (parser, NULL_TREE, false, if_p);
11131 /* Finish the dummy compound-statement. */
11132 finish_compound_stmt (statement);
11135 /* Return the statement. */
11136 return statement;
11139 /* For some dependent statements (like `while (cond) statement'), we
11140 have already created a scope. Therefore, even if the dependent
11141 statement is a compound-statement, we do not want to create another
11142 scope. */
11144 static void
11145 cp_parser_already_scoped_statement (cp_parser* parser)
11147 /* If the token is a `{', then we must take special action. */
11148 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11149 cp_parser_statement (parser, NULL_TREE, false, NULL);
11150 else
11152 /* Avoid calling cp_parser_compound_statement, so that we
11153 don't create a new scope. Do everything else by hand. */
11154 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11155 /* If the next keyword is `__label__' we have a label declaration. */
11156 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11157 cp_parser_label_declaration (parser);
11158 /* Parse an (optional) statement-seq. */
11159 cp_parser_statement_seq_opt (parser, NULL_TREE);
11160 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11164 /* Declarations [gram.dcl.dcl] */
11166 /* Parse an optional declaration-sequence.
11168 declaration-seq:
11169 declaration
11170 declaration-seq declaration */
11172 static void
11173 cp_parser_declaration_seq_opt (cp_parser* parser)
11175 while (true)
11177 cp_token *token;
11179 token = cp_lexer_peek_token (parser->lexer);
11181 if (token->type == CPP_CLOSE_BRACE
11182 || token->type == CPP_EOF
11183 || token->type == CPP_PRAGMA_EOL)
11184 break;
11186 if (token->type == CPP_SEMICOLON)
11188 /* A declaration consisting of a single semicolon is
11189 invalid. Allow it unless we're being pedantic. */
11190 cp_lexer_consume_token (parser->lexer);
11191 if (!in_system_header_at (input_location))
11192 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11193 continue;
11196 /* If we're entering or exiting a region that's implicitly
11197 extern "C", modify the lang context appropriately. */
11198 if (!parser->implicit_extern_c && token->implicit_extern_c)
11200 push_lang_context (lang_name_c);
11201 parser->implicit_extern_c = true;
11203 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11205 pop_lang_context ();
11206 parser->implicit_extern_c = false;
11209 if (token->type == CPP_PRAGMA)
11211 /* A top-level declaration can consist solely of a #pragma.
11212 A nested declaration cannot, so this is done here and not
11213 in cp_parser_declaration. (A #pragma at block scope is
11214 handled in cp_parser_statement.) */
11215 cp_parser_pragma (parser, pragma_external);
11216 continue;
11219 /* Parse the declaration itself. */
11220 cp_parser_declaration (parser);
11224 /* Parse a declaration.
11226 declaration:
11227 block-declaration
11228 function-definition
11229 template-declaration
11230 explicit-instantiation
11231 explicit-specialization
11232 linkage-specification
11233 namespace-definition
11235 GNU extension:
11237 declaration:
11238 __extension__ declaration */
11240 static void
11241 cp_parser_declaration (cp_parser* parser)
11243 cp_token token1;
11244 cp_token token2;
11245 int saved_pedantic;
11246 void *p;
11247 tree attributes = NULL_TREE;
11249 /* Check for the `__extension__' keyword. */
11250 if (cp_parser_extension_opt (parser, &saved_pedantic))
11252 /* Parse the qualified declaration. */
11253 cp_parser_declaration (parser);
11254 /* Restore the PEDANTIC flag. */
11255 pedantic = saved_pedantic;
11257 return;
11260 /* Try to figure out what kind of declaration is present. */
11261 token1 = *cp_lexer_peek_token (parser->lexer);
11263 if (token1.type != CPP_EOF)
11264 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11265 else
11267 token2.type = CPP_EOF;
11268 token2.keyword = RID_MAX;
11271 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11272 p = obstack_alloc (&declarator_obstack, 0);
11274 /* If the next token is `extern' and the following token is a string
11275 literal, then we have a linkage specification. */
11276 if (token1.keyword == RID_EXTERN
11277 && cp_parser_is_pure_string_literal (&token2))
11278 cp_parser_linkage_specification (parser);
11279 /* If the next token is `template', then we have either a template
11280 declaration, an explicit instantiation, or an explicit
11281 specialization. */
11282 else if (token1.keyword == RID_TEMPLATE)
11284 /* `template <>' indicates a template specialization. */
11285 if (token2.type == CPP_LESS
11286 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11287 cp_parser_explicit_specialization (parser);
11288 /* `template <' indicates a template declaration. */
11289 else if (token2.type == CPP_LESS)
11290 cp_parser_template_declaration (parser, /*member_p=*/false);
11291 /* Anything else must be an explicit instantiation. */
11292 else
11293 cp_parser_explicit_instantiation (parser);
11295 /* If the next token is `export', then we have a template
11296 declaration. */
11297 else if (token1.keyword == RID_EXPORT)
11298 cp_parser_template_declaration (parser, /*member_p=*/false);
11299 /* If the next token is `extern', 'static' or 'inline' and the one
11300 after that is `template', we have a GNU extended explicit
11301 instantiation directive. */
11302 else if (cp_parser_allow_gnu_extensions_p (parser)
11303 && (token1.keyword == RID_EXTERN
11304 || token1.keyword == RID_STATIC
11305 || token1.keyword == RID_INLINE)
11306 && token2.keyword == RID_TEMPLATE)
11307 cp_parser_explicit_instantiation (parser);
11308 /* If the next token is `namespace', check for a named or unnamed
11309 namespace definition. */
11310 else if (token1.keyword == RID_NAMESPACE
11311 && (/* A named namespace definition. */
11312 (token2.type == CPP_NAME
11313 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11314 != CPP_EQ))
11315 /* An unnamed namespace definition. */
11316 || token2.type == CPP_OPEN_BRACE
11317 || token2.keyword == RID_ATTRIBUTE))
11318 cp_parser_namespace_definition (parser);
11319 /* An inline (associated) namespace definition. */
11320 else if (token1.keyword == RID_INLINE
11321 && token2.keyword == RID_NAMESPACE)
11322 cp_parser_namespace_definition (parser);
11323 /* Objective-C++ declaration/definition. */
11324 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11325 cp_parser_objc_declaration (parser, NULL_TREE);
11326 else if (c_dialect_objc ()
11327 && token1.keyword == RID_ATTRIBUTE
11328 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11329 cp_parser_objc_declaration (parser, attributes);
11330 /* We must have either a block declaration or a function
11331 definition. */
11332 else
11333 /* Try to parse a block-declaration, or a function-definition. */
11334 cp_parser_block_declaration (parser, /*statement_p=*/false);
11336 /* Free any declarators allocated. */
11337 obstack_free (&declarator_obstack, p);
11340 /* Parse a block-declaration.
11342 block-declaration:
11343 simple-declaration
11344 asm-definition
11345 namespace-alias-definition
11346 using-declaration
11347 using-directive
11349 GNU Extension:
11351 block-declaration:
11352 __extension__ block-declaration
11354 C++0x Extension:
11356 block-declaration:
11357 static_assert-declaration
11359 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11360 part of a declaration-statement. */
11362 static void
11363 cp_parser_block_declaration (cp_parser *parser,
11364 bool statement_p)
11366 cp_token *token1;
11367 int saved_pedantic;
11369 /* Check for the `__extension__' keyword. */
11370 if (cp_parser_extension_opt (parser, &saved_pedantic))
11372 /* Parse the qualified declaration. */
11373 cp_parser_block_declaration (parser, statement_p);
11374 /* Restore the PEDANTIC flag. */
11375 pedantic = saved_pedantic;
11377 return;
11380 /* Peek at the next token to figure out which kind of declaration is
11381 present. */
11382 token1 = cp_lexer_peek_token (parser->lexer);
11384 /* If the next keyword is `asm', we have an asm-definition. */
11385 if (token1->keyword == RID_ASM)
11387 if (statement_p)
11388 cp_parser_commit_to_tentative_parse (parser);
11389 cp_parser_asm_definition (parser);
11391 /* If the next keyword is `namespace', we have a
11392 namespace-alias-definition. */
11393 else if (token1->keyword == RID_NAMESPACE)
11394 cp_parser_namespace_alias_definition (parser);
11395 /* If the next keyword is `using', we have a
11396 using-declaration, a using-directive, or an alias-declaration. */
11397 else if (token1->keyword == RID_USING)
11399 cp_token *token2;
11401 if (statement_p)
11402 cp_parser_commit_to_tentative_parse (parser);
11403 /* If the token after `using' is `namespace', then we have a
11404 using-directive. */
11405 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11406 if (token2->keyword == RID_NAMESPACE)
11407 cp_parser_using_directive (parser);
11408 /* If the second token after 'using' is '=', then we have an
11409 alias-declaration. */
11410 else if (cxx_dialect >= cxx11
11411 && token2->type == CPP_NAME
11412 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11413 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11414 cp_parser_alias_declaration (parser);
11415 /* Otherwise, it's a using-declaration. */
11416 else
11417 cp_parser_using_declaration (parser,
11418 /*access_declaration_p=*/false);
11420 /* If the next keyword is `__label__' we have a misplaced label
11421 declaration. */
11422 else if (token1->keyword == RID_LABEL)
11424 cp_lexer_consume_token (parser->lexer);
11425 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11426 cp_parser_skip_to_end_of_statement (parser);
11427 /* If the next token is now a `;', consume it. */
11428 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11429 cp_lexer_consume_token (parser->lexer);
11431 /* If the next token is `static_assert' we have a static assertion. */
11432 else if (token1->keyword == RID_STATIC_ASSERT)
11433 cp_parser_static_assert (parser, /*member_p=*/false);
11434 /* Anything else must be a simple-declaration. */
11435 else
11436 cp_parser_simple_declaration (parser, !statement_p,
11437 /*maybe_range_for_decl*/NULL);
11440 /* Parse a simple-declaration.
11442 simple-declaration:
11443 decl-specifier-seq [opt] init-declarator-list [opt] ;
11445 init-declarator-list:
11446 init-declarator
11447 init-declarator-list , init-declarator
11449 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11450 function-definition as a simple-declaration.
11452 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11453 parsed declaration if it is an uninitialized single declarator not followed
11454 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11455 if present, will not be consumed. */
11457 static void
11458 cp_parser_simple_declaration (cp_parser* parser,
11459 bool function_definition_allowed_p,
11460 tree *maybe_range_for_decl)
11462 cp_decl_specifier_seq decl_specifiers;
11463 int declares_class_or_enum;
11464 bool saw_declarator;
11465 location_t comma_loc = UNKNOWN_LOCATION;
11466 location_t init_loc = UNKNOWN_LOCATION;
11468 if (maybe_range_for_decl)
11469 *maybe_range_for_decl = NULL_TREE;
11471 /* Defer access checks until we know what is being declared; the
11472 checks for names appearing in the decl-specifier-seq should be
11473 done as if we were in the scope of the thing being declared. */
11474 push_deferring_access_checks (dk_deferred);
11476 /* Parse the decl-specifier-seq. We have to keep track of whether
11477 or not the decl-specifier-seq declares a named class or
11478 enumeration type, since that is the only case in which the
11479 init-declarator-list is allowed to be empty.
11481 [dcl.dcl]
11483 In a simple-declaration, the optional init-declarator-list can be
11484 omitted only when declaring a class or enumeration, that is when
11485 the decl-specifier-seq contains either a class-specifier, an
11486 elaborated-type-specifier, or an enum-specifier. */
11487 cp_parser_decl_specifier_seq (parser,
11488 CP_PARSER_FLAGS_OPTIONAL,
11489 &decl_specifiers,
11490 &declares_class_or_enum);
11491 /* We no longer need to defer access checks. */
11492 stop_deferring_access_checks ();
11494 /* In a block scope, a valid declaration must always have a
11495 decl-specifier-seq. By not trying to parse declarators, we can
11496 resolve the declaration/expression ambiguity more quickly. */
11497 if (!function_definition_allowed_p
11498 && !decl_specifiers.any_specifiers_p)
11500 cp_parser_error (parser, "expected declaration");
11501 goto done;
11504 /* If the next two tokens are both identifiers, the code is
11505 erroneous. The usual cause of this situation is code like:
11507 T t;
11509 where "T" should name a type -- but does not. */
11510 if (!decl_specifiers.any_type_specifiers_p
11511 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11513 /* If parsing tentatively, we should commit; we really are
11514 looking at a declaration. */
11515 cp_parser_commit_to_tentative_parse (parser);
11516 /* Give up. */
11517 goto done;
11520 /* If we have seen at least one decl-specifier, and the next token
11521 is not a parenthesis, then we must be looking at a declaration.
11522 (After "int (" we might be looking at a functional cast.) */
11523 if (decl_specifiers.any_specifiers_p
11524 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11525 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11526 && !cp_parser_error_occurred (parser))
11527 cp_parser_commit_to_tentative_parse (parser);
11529 /* Keep going until we hit the `;' at the end of the simple
11530 declaration. */
11531 saw_declarator = false;
11532 while (cp_lexer_next_token_is_not (parser->lexer,
11533 CPP_SEMICOLON))
11535 cp_token *token;
11536 bool function_definition_p;
11537 tree decl;
11539 if (saw_declarator)
11541 /* If we are processing next declarator, comma is expected */
11542 token = cp_lexer_peek_token (parser->lexer);
11543 gcc_assert (token->type == CPP_COMMA);
11544 cp_lexer_consume_token (parser->lexer);
11545 if (maybe_range_for_decl)
11547 *maybe_range_for_decl = error_mark_node;
11548 if (comma_loc == UNKNOWN_LOCATION)
11549 comma_loc = token->location;
11552 else
11553 saw_declarator = true;
11555 /* Parse the init-declarator. */
11556 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11557 /*checks=*/NULL,
11558 function_definition_allowed_p,
11559 /*member_p=*/false,
11560 declares_class_or_enum,
11561 &function_definition_p,
11562 maybe_range_for_decl,
11563 &init_loc);
11564 /* If an error occurred while parsing tentatively, exit quickly.
11565 (That usually happens when in the body of a function; each
11566 statement is treated as a declaration-statement until proven
11567 otherwise.) */
11568 if (cp_parser_error_occurred (parser))
11569 goto done;
11570 /* Handle function definitions specially. */
11571 if (function_definition_p)
11573 /* If the next token is a `,', then we are probably
11574 processing something like:
11576 void f() {}, *p;
11578 which is erroneous. */
11579 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11581 cp_token *token = cp_lexer_peek_token (parser->lexer);
11582 error_at (token->location,
11583 "mixing"
11584 " declarations and function-definitions is forbidden");
11586 /* Otherwise, we're done with the list of declarators. */
11587 else
11589 pop_deferring_access_checks ();
11590 return;
11593 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11594 *maybe_range_for_decl = decl;
11595 /* The next token should be either a `,' or a `;'. */
11596 token = cp_lexer_peek_token (parser->lexer);
11597 /* If it's a `,', there are more declarators to come. */
11598 if (token->type == CPP_COMMA)
11599 /* will be consumed next time around */;
11600 /* If it's a `;', we are done. */
11601 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11602 break;
11603 /* Anything else is an error. */
11604 else
11606 /* If we have already issued an error message we don't need
11607 to issue another one. */
11608 if (decl != error_mark_node
11609 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11610 cp_parser_error (parser, "expected %<,%> or %<;%>");
11611 /* Skip tokens until we reach the end of the statement. */
11612 cp_parser_skip_to_end_of_statement (parser);
11613 /* If the next token is now a `;', consume it. */
11614 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11615 cp_lexer_consume_token (parser->lexer);
11616 goto done;
11618 /* After the first time around, a function-definition is not
11619 allowed -- even if it was OK at first. For example:
11621 int i, f() {}
11623 is not valid. */
11624 function_definition_allowed_p = false;
11627 /* Issue an error message if no declarators are present, and the
11628 decl-specifier-seq does not itself declare a class or
11629 enumeration: [dcl.dcl]/3. */
11630 if (!saw_declarator)
11632 if (cp_parser_declares_only_class_p (parser))
11634 if (!declares_class_or_enum
11635 && decl_specifiers.type
11636 && OVERLOAD_TYPE_P (decl_specifiers.type))
11637 /* Ensure an error is issued anyway when finish_decltype_type,
11638 called via cp_parser_decl_specifier_seq, returns a class or
11639 an enumeration (c++/51786). */
11640 decl_specifiers.type = NULL_TREE;
11641 shadow_tag (&decl_specifiers);
11643 /* Perform any deferred access checks. */
11644 perform_deferred_access_checks (tf_warning_or_error);
11647 /* Consume the `;'. */
11648 if (!maybe_range_for_decl)
11649 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11650 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11652 if (init_loc != UNKNOWN_LOCATION)
11653 error_at (init_loc, "initializer in range-based %<for%> loop");
11654 if (comma_loc != UNKNOWN_LOCATION)
11655 error_at (comma_loc,
11656 "multiple declarations in range-based %<for%> loop");
11659 done:
11660 pop_deferring_access_checks ();
11663 /* Parse a decl-specifier-seq.
11665 decl-specifier-seq:
11666 decl-specifier-seq [opt] decl-specifier
11667 decl-specifier attribute-specifier-seq [opt] (C++11)
11669 decl-specifier:
11670 storage-class-specifier
11671 type-specifier
11672 function-specifier
11673 friend
11674 typedef
11676 GNU Extension:
11678 decl-specifier:
11679 attributes
11681 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11683 The parser flags FLAGS is used to control type-specifier parsing.
11685 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11686 flags:
11688 1: one of the decl-specifiers is an elaborated-type-specifier
11689 (i.e., a type declaration)
11690 2: one of the decl-specifiers is an enum-specifier or a
11691 class-specifier (i.e., a type definition)
11695 static void
11696 cp_parser_decl_specifier_seq (cp_parser* parser,
11697 cp_parser_flags flags,
11698 cp_decl_specifier_seq *decl_specs,
11699 int* declares_class_or_enum)
11701 bool constructor_possible_p = !parser->in_declarator_p;
11702 bool found_decl_spec = false;
11703 cp_token *start_token = NULL;
11704 cp_decl_spec ds;
11706 /* Clear DECL_SPECS. */
11707 clear_decl_specs (decl_specs);
11709 /* Assume no class or enumeration type is declared. */
11710 *declares_class_or_enum = 0;
11712 /* Keep reading specifiers until there are no more to read. */
11713 while (true)
11715 bool constructor_p;
11716 cp_token *token;
11717 ds = ds_last;
11719 /* Peek at the next token. */
11720 token = cp_lexer_peek_token (parser->lexer);
11722 /* Save the first token of the decl spec list for error
11723 reporting. */
11724 if (!start_token)
11725 start_token = token;
11726 /* Handle attributes. */
11727 if (cp_next_tokens_can_be_attribute_p (parser))
11729 /* Parse the attributes. */
11730 tree attrs = cp_parser_attributes_opt (parser);
11732 /* In a sequence of declaration specifiers, c++11 attributes
11733 appertain to the type that precede them. In that case
11734 [dcl.spec]/1 says:
11736 The attribute-specifier-seq affects the type only for
11737 the declaration it appears in, not other declarations
11738 involving the same type.
11740 But for now let's force the user to position the
11741 attribute either at the beginning of the declaration or
11742 after the declarator-id, which would clearly mean that it
11743 applies to the declarator. */
11744 if (cxx11_attribute_p (attrs))
11746 if (!found_decl_spec)
11747 /* The c++11 attribute is at the beginning of the
11748 declaration. It appertains to the entity being
11749 declared. */;
11750 else
11752 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11754 /* This is an attribute following a
11755 class-specifier. */
11756 if (decl_specs->type_definition_p)
11757 warn_misplaced_attr_for_class_type (token->location,
11758 decl_specs->type);
11759 attrs = NULL_TREE;
11761 else
11763 decl_specs->std_attributes
11764 = chainon (decl_specs->std_attributes,
11765 attrs);
11766 if (decl_specs->locations[ds_std_attribute] == 0)
11767 decl_specs->locations[ds_std_attribute] = token->location;
11769 continue;
11773 decl_specs->attributes
11774 = chainon (decl_specs->attributes,
11775 attrs);
11776 if (decl_specs->locations[ds_attribute] == 0)
11777 decl_specs->locations[ds_attribute] = token->location;
11778 continue;
11780 /* Assume we will find a decl-specifier keyword. */
11781 found_decl_spec = true;
11782 /* If the next token is an appropriate keyword, we can simply
11783 add it to the list. */
11784 switch (token->keyword)
11786 /* decl-specifier:
11787 friend
11788 constexpr */
11789 case RID_FRIEND:
11790 if (!at_class_scope_p ())
11792 error_at (token->location, "%<friend%> used outside of class");
11793 cp_lexer_purge_token (parser->lexer);
11795 else
11797 ds = ds_friend;
11798 /* Consume the token. */
11799 cp_lexer_consume_token (parser->lexer);
11801 break;
11803 case RID_CONSTEXPR:
11804 ds = ds_constexpr;
11805 cp_lexer_consume_token (parser->lexer);
11806 break;
11808 /* function-specifier:
11809 inline
11810 virtual
11811 explicit */
11812 case RID_INLINE:
11813 case RID_VIRTUAL:
11814 case RID_EXPLICIT:
11815 cp_parser_function_specifier_opt (parser, decl_specs);
11816 break;
11818 /* decl-specifier:
11819 typedef */
11820 case RID_TYPEDEF:
11821 ds = ds_typedef;
11822 /* Consume the token. */
11823 cp_lexer_consume_token (parser->lexer);
11824 /* A constructor declarator cannot appear in a typedef. */
11825 constructor_possible_p = false;
11826 /* The "typedef" keyword can only occur in a declaration; we
11827 may as well commit at this point. */
11828 cp_parser_commit_to_tentative_parse (parser);
11830 if (decl_specs->storage_class != sc_none)
11831 decl_specs->conflicting_specifiers_p = true;
11832 break;
11834 /* storage-class-specifier:
11835 auto
11836 register
11837 static
11838 extern
11839 mutable
11841 GNU Extension:
11842 thread */
11843 case RID_AUTO:
11844 if (cxx_dialect == cxx98)
11846 /* Consume the token. */
11847 cp_lexer_consume_token (parser->lexer);
11849 /* Complain about `auto' as a storage specifier, if
11850 we're complaining about C++0x compatibility. */
11851 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11852 " changes meaning in C++11; please remove it");
11854 /* Set the storage class anyway. */
11855 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11856 token);
11858 else
11859 /* C++0x auto type-specifier. */
11860 found_decl_spec = false;
11861 break;
11863 case RID_REGISTER:
11864 case RID_STATIC:
11865 case RID_EXTERN:
11866 case RID_MUTABLE:
11867 /* Consume the token. */
11868 cp_lexer_consume_token (parser->lexer);
11869 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11870 token);
11871 break;
11872 case RID_THREAD:
11873 /* Consume the token. */
11874 ds = ds_thread;
11875 cp_lexer_consume_token (parser->lexer);
11876 break;
11878 default:
11879 /* We did not yet find a decl-specifier yet. */
11880 found_decl_spec = false;
11881 break;
11884 if (found_decl_spec
11885 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11886 && token->keyword != RID_CONSTEXPR)
11887 error ("decl-specifier invalid in condition");
11889 if (ds != ds_last)
11890 set_and_check_decl_spec_loc (decl_specs, ds, token);
11892 /* Constructors are a special case. The `S' in `S()' is not a
11893 decl-specifier; it is the beginning of the declarator. */
11894 constructor_p
11895 = (!found_decl_spec
11896 && constructor_possible_p
11897 && (cp_parser_constructor_declarator_p
11898 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11900 /* If we don't have a DECL_SPEC yet, then we must be looking at
11901 a type-specifier. */
11902 if (!found_decl_spec && !constructor_p)
11904 int decl_spec_declares_class_or_enum;
11905 bool is_cv_qualifier;
11906 tree type_spec;
11908 type_spec
11909 = cp_parser_type_specifier (parser, flags,
11910 decl_specs,
11911 /*is_declaration=*/true,
11912 &decl_spec_declares_class_or_enum,
11913 &is_cv_qualifier);
11914 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11916 /* If this type-specifier referenced a user-defined type
11917 (a typedef, class-name, etc.), then we can't allow any
11918 more such type-specifiers henceforth.
11920 [dcl.spec]
11922 The longest sequence of decl-specifiers that could
11923 possibly be a type name is taken as the
11924 decl-specifier-seq of a declaration. The sequence shall
11925 be self-consistent as described below.
11927 [dcl.type]
11929 As a general rule, at most one type-specifier is allowed
11930 in the complete decl-specifier-seq of a declaration. The
11931 only exceptions are the following:
11933 -- const or volatile can be combined with any other
11934 type-specifier.
11936 -- signed or unsigned can be combined with char, long,
11937 short, or int.
11939 -- ..
11941 Example:
11943 typedef char* Pc;
11944 void g (const int Pc);
11946 Here, Pc is *not* part of the decl-specifier seq; it's
11947 the declarator. Therefore, once we see a type-specifier
11948 (other than a cv-qualifier), we forbid any additional
11949 user-defined types. We *do* still allow things like `int
11950 int' to be considered a decl-specifier-seq, and issue the
11951 error message later. */
11952 if (type_spec && !is_cv_qualifier)
11953 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11954 /* A constructor declarator cannot follow a type-specifier. */
11955 if (type_spec)
11957 constructor_possible_p = false;
11958 found_decl_spec = true;
11959 if (!is_cv_qualifier)
11960 decl_specs->any_type_specifiers_p = true;
11964 /* If we still do not have a DECL_SPEC, then there are no more
11965 decl-specifiers. */
11966 if (!found_decl_spec)
11967 break;
11969 decl_specs->any_specifiers_p = true;
11970 /* After we see one decl-specifier, further decl-specifiers are
11971 always optional. */
11972 flags |= CP_PARSER_FLAGS_OPTIONAL;
11975 /* Don't allow a friend specifier with a class definition. */
11976 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11977 && (*declares_class_or_enum & 2))
11978 error_at (decl_specs->locations[ds_friend],
11979 "class definition may not be declared a friend");
11982 /* Parse an (optional) storage-class-specifier.
11984 storage-class-specifier:
11985 auto
11986 register
11987 static
11988 extern
11989 mutable
11991 GNU Extension:
11993 storage-class-specifier:
11994 thread
11996 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11998 static tree
11999 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12001 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12003 case RID_AUTO:
12004 if (cxx_dialect != cxx98)
12005 return NULL_TREE;
12006 /* Fall through for C++98. */
12008 case RID_REGISTER:
12009 case RID_STATIC:
12010 case RID_EXTERN:
12011 case RID_MUTABLE:
12012 case RID_THREAD:
12013 /* Consume the token. */
12014 return cp_lexer_consume_token (parser->lexer)->u.value;
12016 default:
12017 return NULL_TREE;
12021 /* Parse an (optional) function-specifier.
12023 function-specifier:
12024 inline
12025 virtual
12026 explicit
12028 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12029 Updates DECL_SPECS, if it is non-NULL. */
12031 static tree
12032 cp_parser_function_specifier_opt (cp_parser* parser,
12033 cp_decl_specifier_seq *decl_specs)
12035 cp_token *token = cp_lexer_peek_token (parser->lexer);
12036 switch (token->keyword)
12038 case RID_INLINE:
12039 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12040 break;
12042 case RID_VIRTUAL:
12043 /* 14.5.2.3 [temp.mem]
12045 A member function template shall not be virtual. */
12046 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12047 error_at (token->location, "templates may not be %<virtual%>");
12048 else
12049 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12050 break;
12052 case RID_EXPLICIT:
12053 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12054 break;
12056 default:
12057 return NULL_TREE;
12060 /* Consume the token. */
12061 return cp_lexer_consume_token (parser->lexer)->u.value;
12064 /* Parse a linkage-specification.
12066 linkage-specification:
12067 extern string-literal { declaration-seq [opt] }
12068 extern string-literal declaration */
12070 static void
12071 cp_parser_linkage_specification (cp_parser* parser)
12073 tree linkage;
12075 /* Look for the `extern' keyword. */
12076 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12078 /* Look for the string-literal. */
12079 linkage = cp_parser_string_literal (parser, false, false);
12081 /* Transform the literal into an identifier. If the literal is a
12082 wide-character string, or contains embedded NULs, then we can't
12083 handle it as the user wants. */
12084 if (strlen (TREE_STRING_POINTER (linkage))
12085 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12087 cp_parser_error (parser, "invalid linkage-specification");
12088 /* Assume C++ linkage. */
12089 linkage = lang_name_cplusplus;
12091 else
12092 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12094 /* We're now using the new linkage. */
12095 push_lang_context (linkage);
12097 /* If the next token is a `{', then we're using the first
12098 production. */
12099 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12101 cp_ensure_no_omp_declare_simd (parser);
12103 /* Consume the `{' token. */
12104 cp_lexer_consume_token (parser->lexer);
12105 /* Parse the declarations. */
12106 cp_parser_declaration_seq_opt (parser);
12107 /* Look for the closing `}'. */
12108 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12110 /* Otherwise, there's just one declaration. */
12111 else
12113 bool saved_in_unbraced_linkage_specification_p;
12115 saved_in_unbraced_linkage_specification_p
12116 = parser->in_unbraced_linkage_specification_p;
12117 parser->in_unbraced_linkage_specification_p = true;
12118 cp_parser_declaration (parser);
12119 parser->in_unbraced_linkage_specification_p
12120 = saved_in_unbraced_linkage_specification_p;
12123 /* We're done with the linkage-specification. */
12124 pop_lang_context ();
12127 /* Parse a static_assert-declaration.
12129 static_assert-declaration:
12130 static_assert ( constant-expression , string-literal ) ;
12132 If MEMBER_P, this static_assert is a class member. */
12134 static void
12135 cp_parser_static_assert(cp_parser *parser, bool member_p)
12137 tree condition;
12138 tree message;
12139 cp_token *token;
12140 location_t saved_loc;
12141 bool dummy;
12143 /* Peek at the `static_assert' token so we can keep track of exactly
12144 where the static assertion started. */
12145 token = cp_lexer_peek_token (parser->lexer);
12146 saved_loc = token->location;
12148 /* Look for the `static_assert' keyword. */
12149 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12150 RT_STATIC_ASSERT))
12151 return;
12153 /* We know we are in a static assertion; commit to any tentative
12154 parse. */
12155 if (cp_parser_parsing_tentatively (parser))
12156 cp_parser_commit_to_tentative_parse (parser);
12158 /* Parse the `(' starting the static assertion condition. */
12159 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12161 /* Parse the constant-expression. Allow a non-constant expression
12162 here in order to give better diagnostics in finish_static_assert. */
12163 condition =
12164 cp_parser_constant_expression (parser,
12165 /*allow_non_constant_p=*/true,
12166 /*non_constant_p=*/&dummy);
12168 /* Parse the separating `,'. */
12169 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12171 /* Parse the string-literal message. */
12172 message = cp_parser_string_literal (parser,
12173 /*translate=*/false,
12174 /*wide_ok=*/true);
12176 /* A `)' completes the static assertion. */
12177 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12178 cp_parser_skip_to_closing_parenthesis (parser,
12179 /*recovering=*/true,
12180 /*or_comma=*/false,
12181 /*consume_paren=*/true);
12183 /* A semicolon terminates the declaration. */
12184 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12186 /* Complete the static assertion, which may mean either processing
12187 the static assert now or saving it for template instantiation. */
12188 finish_static_assert (condition, message, saved_loc, member_p);
12191 /* Parse the expression in decltype ( expression ). */
12193 static tree
12194 cp_parser_decltype_expr (cp_parser *parser,
12195 bool &id_expression_or_member_access_p)
12197 cp_token *id_expr_start_token;
12198 tree expr;
12200 /* First, try parsing an id-expression. */
12201 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12202 cp_parser_parse_tentatively (parser);
12203 expr = cp_parser_id_expression (parser,
12204 /*template_keyword_p=*/false,
12205 /*check_dependency_p=*/true,
12206 /*template_p=*/NULL,
12207 /*declarator_p=*/false,
12208 /*optional_p=*/false);
12210 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12212 bool non_integral_constant_expression_p = false;
12213 tree id_expression = expr;
12214 cp_id_kind idk;
12215 const char *error_msg;
12217 if (identifier_p (expr))
12218 /* Lookup the name we got back from the id-expression. */
12219 expr = cp_parser_lookup_name_simple (parser, expr,
12220 id_expr_start_token->location);
12222 if (expr
12223 && expr != error_mark_node
12224 && TREE_CODE (expr) != TYPE_DECL
12225 && (TREE_CODE (expr) != BIT_NOT_EXPR
12226 || !TYPE_P (TREE_OPERAND (expr, 0)))
12227 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12229 /* Complete lookup of the id-expression. */
12230 expr = (finish_id_expression
12231 (id_expression, expr, parser->scope, &idk,
12232 /*integral_constant_expression_p=*/false,
12233 /*allow_non_integral_constant_expression_p=*/true,
12234 &non_integral_constant_expression_p,
12235 /*template_p=*/false,
12236 /*done=*/true,
12237 /*address_p=*/false,
12238 /*template_arg_p=*/false,
12239 &error_msg,
12240 id_expr_start_token->location));
12242 if (expr == error_mark_node)
12243 /* We found an id-expression, but it was something that we
12244 should not have found. This is an error, not something
12245 we can recover from, so note that we found an
12246 id-expression and we'll recover as gracefully as
12247 possible. */
12248 id_expression_or_member_access_p = true;
12251 if (expr
12252 && expr != error_mark_node
12253 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12254 /* We have an id-expression. */
12255 id_expression_or_member_access_p = true;
12258 if (!id_expression_or_member_access_p)
12260 /* Abort the id-expression parse. */
12261 cp_parser_abort_tentative_parse (parser);
12263 /* Parsing tentatively, again. */
12264 cp_parser_parse_tentatively (parser);
12266 /* Parse a class member access. */
12267 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12268 /*cast_p=*/false, /*decltype*/true,
12269 /*member_access_only_p=*/true, NULL);
12271 if (expr
12272 && expr != error_mark_node
12273 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12274 /* We have an id-expression. */
12275 id_expression_or_member_access_p = true;
12278 if (id_expression_or_member_access_p)
12279 /* We have parsed the complete id-expression or member access. */
12280 cp_parser_parse_definitely (parser);
12281 else
12283 /* Abort our attempt to parse an id-expression or member access
12284 expression. */
12285 cp_parser_abort_tentative_parse (parser);
12287 /* Parse a full expression. */
12288 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12289 /*decltype_p=*/true);
12292 return expr;
12295 /* Parse a `decltype' type. Returns the type.
12297 simple-type-specifier:
12298 decltype ( expression )
12299 C++14 proposal:
12300 decltype ( auto ) */
12302 static tree
12303 cp_parser_decltype (cp_parser *parser)
12305 tree expr;
12306 bool id_expression_or_member_access_p = false;
12307 const char *saved_message;
12308 bool saved_integral_constant_expression_p;
12309 bool saved_non_integral_constant_expression_p;
12310 bool saved_greater_than_is_operator_p;
12311 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12313 if (start_token->type == CPP_DECLTYPE)
12315 /* Already parsed. */
12316 cp_lexer_consume_token (parser->lexer);
12317 return start_token->u.value;
12320 /* Look for the `decltype' token. */
12321 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12322 return error_mark_node;
12324 /* Parse the opening `('. */
12325 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12326 return error_mark_node;
12328 /* decltype (auto) */
12329 if (cxx_dialect >= cxx14
12330 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12332 cp_lexer_consume_token (parser->lexer);
12333 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12334 return error_mark_node;
12335 expr = make_decltype_auto ();
12336 AUTO_IS_DECLTYPE (expr) = true;
12337 goto rewrite;
12340 /* Types cannot be defined in a `decltype' expression. Save away the
12341 old message. */
12342 saved_message = parser->type_definition_forbidden_message;
12344 /* And create the new one. */
12345 parser->type_definition_forbidden_message
12346 = G_("types may not be defined in %<decltype%> expressions");
12348 /* The restrictions on constant-expressions do not apply inside
12349 decltype expressions. */
12350 saved_integral_constant_expression_p
12351 = parser->integral_constant_expression_p;
12352 saved_non_integral_constant_expression_p
12353 = parser->non_integral_constant_expression_p;
12354 parser->integral_constant_expression_p = false;
12356 /* Within a parenthesized expression, a `>' token is always
12357 the greater-than operator. */
12358 saved_greater_than_is_operator_p
12359 = parser->greater_than_is_operator_p;
12360 parser->greater_than_is_operator_p = true;
12362 /* Do not actually evaluate the expression. */
12363 ++cp_unevaluated_operand;
12365 /* Do not warn about problems with the expression. */
12366 ++c_inhibit_evaluation_warnings;
12368 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12370 /* Go back to evaluating expressions. */
12371 --cp_unevaluated_operand;
12372 --c_inhibit_evaluation_warnings;
12374 /* The `>' token might be the end of a template-id or
12375 template-parameter-list now. */
12376 parser->greater_than_is_operator_p
12377 = saved_greater_than_is_operator_p;
12379 /* Restore the old message and the integral constant expression
12380 flags. */
12381 parser->type_definition_forbidden_message = saved_message;
12382 parser->integral_constant_expression_p
12383 = saved_integral_constant_expression_p;
12384 parser->non_integral_constant_expression_p
12385 = saved_non_integral_constant_expression_p;
12387 /* Parse to the closing `)'. */
12388 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12390 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12391 /*consume_paren=*/true);
12392 return error_mark_node;
12395 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12396 tf_warning_or_error);
12398 rewrite:
12399 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12400 it again. */
12401 start_token->type = CPP_DECLTYPE;
12402 start_token->u.value = expr;
12403 start_token->keyword = RID_MAX;
12404 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12406 return expr;
12409 /* Special member functions [gram.special] */
12411 /* Parse a conversion-function-id.
12413 conversion-function-id:
12414 operator conversion-type-id
12416 Returns an IDENTIFIER_NODE representing the operator. */
12418 static tree
12419 cp_parser_conversion_function_id (cp_parser* parser)
12421 tree type;
12422 tree saved_scope;
12423 tree saved_qualifying_scope;
12424 tree saved_object_scope;
12425 tree pushed_scope = NULL_TREE;
12427 /* Look for the `operator' token. */
12428 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12429 return error_mark_node;
12430 /* When we parse the conversion-type-id, the current scope will be
12431 reset. However, we need that information in able to look up the
12432 conversion function later, so we save it here. */
12433 saved_scope = parser->scope;
12434 saved_qualifying_scope = parser->qualifying_scope;
12435 saved_object_scope = parser->object_scope;
12436 /* We must enter the scope of the class so that the names of
12437 entities declared within the class are available in the
12438 conversion-type-id. For example, consider:
12440 struct S {
12441 typedef int I;
12442 operator I();
12445 S::operator I() { ... }
12447 In order to see that `I' is a type-name in the definition, we
12448 must be in the scope of `S'. */
12449 if (saved_scope)
12450 pushed_scope = push_scope (saved_scope);
12451 /* Parse the conversion-type-id. */
12452 type = cp_parser_conversion_type_id (parser);
12453 /* Leave the scope of the class, if any. */
12454 if (pushed_scope)
12455 pop_scope (pushed_scope);
12456 /* Restore the saved scope. */
12457 parser->scope = saved_scope;
12458 parser->qualifying_scope = saved_qualifying_scope;
12459 parser->object_scope = saved_object_scope;
12460 /* If the TYPE is invalid, indicate failure. */
12461 if (type == error_mark_node)
12462 return error_mark_node;
12463 return mangle_conv_op_name_for_type (type);
12466 /* Parse a conversion-type-id:
12468 conversion-type-id:
12469 type-specifier-seq conversion-declarator [opt]
12471 Returns the TYPE specified. */
12473 static tree
12474 cp_parser_conversion_type_id (cp_parser* parser)
12476 tree attributes;
12477 cp_decl_specifier_seq type_specifiers;
12478 cp_declarator *declarator;
12479 tree type_specified;
12480 const char *saved_message;
12482 /* Parse the attributes. */
12483 attributes = cp_parser_attributes_opt (parser);
12485 saved_message = parser->type_definition_forbidden_message;
12486 parser->type_definition_forbidden_message
12487 = G_("types may not be defined in a conversion-type-id");
12489 /* Parse the type-specifiers. */
12490 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12491 /*is_trailing_return=*/false,
12492 &type_specifiers);
12494 parser->type_definition_forbidden_message = saved_message;
12496 /* If that didn't work, stop. */
12497 if (type_specifiers.type == error_mark_node)
12498 return error_mark_node;
12499 /* Parse the conversion-declarator. */
12500 declarator = cp_parser_conversion_declarator_opt (parser);
12502 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12503 /*initialized=*/0, &attributes);
12504 if (attributes)
12505 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12507 /* Don't give this error when parsing tentatively. This happens to
12508 work because we always parse this definitively once. */
12509 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12510 && type_uses_auto (type_specified))
12512 if (cxx_dialect < cxx14)
12514 error ("invalid use of %<auto%> in conversion operator");
12515 return error_mark_node;
12517 else if (template_parm_scope_p ())
12518 warning (0, "use of %<auto%> in member template "
12519 "conversion operator can never be deduced");
12522 return type_specified;
12525 /* Parse an (optional) conversion-declarator.
12527 conversion-declarator:
12528 ptr-operator conversion-declarator [opt]
12532 static cp_declarator *
12533 cp_parser_conversion_declarator_opt (cp_parser* parser)
12535 enum tree_code code;
12536 tree class_type, std_attributes = NULL_TREE;
12537 cp_cv_quals cv_quals;
12539 /* We don't know if there's a ptr-operator next, or not. */
12540 cp_parser_parse_tentatively (parser);
12541 /* Try the ptr-operator. */
12542 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12543 &std_attributes);
12544 /* If it worked, look for more conversion-declarators. */
12545 if (cp_parser_parse_definitely (parser))
12547 cp_declarator *declarator;
12549 /* Parse another optional declarator. */
12550 declarator = cp_parser_conversion_declarator_opt (parser);
12552 declarator = cp_parser_make_indirect_declarator
12553 (code, class_type, cv_quals, declarator, std_attributes);
12555 return declarator;
12558 return NULL;
12561 /* Parse an (optional) ctor-initializer.
12563 ctor-initializer:
12564 : mem-initializer-list
12566 Returns TRUE iff the ctor-initializer was actually present. */
12568 static bool
12569 cp_parser_ctor_initializer_opt (cp_parser* parser)
12571 /* If the next token is not a `:', then there is no
12572 ctor-initializer. */
12573 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12575 /* Do default initialization of any bases and members. */
12576 if (DECL_CONSTRUCTOR_P (current_function_decl))
12577 finish_mem_initializers (NULL_TREE);
12579 return false;
12582 /* Consume the `:' token. */
12583 cp_lexer_consume_token (parser->lexer);
12584 /* And the mem-initializer-list. */
12585 cp_parser_mem_initializer_list (parser);
12587 return true;
12590 /* Parse a mem-initializer-list.
12592 mem-initializer-list:
12593 mem-initializer ... [opt]
12594 mem-initializer ... [opt] , mem-initializer-list */
12596 static void
12597 cp_parser_mem_initializer_list (cp_parser* parser)
12599 tree mem_initializer_list = NULL_TREE;
12600 tree target_ctor = error_mark_node;
12601 cp_token *token = cp_lexer_peek_token (parser->lexer);
12603 /* Let the semantic analysis code know that we are starting the
12604 mem-initializer-list. */
12605 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12606 error_at (token->location,
12607 "only constructors take member initializers");
12609 /* Loop through the list. */
12610 while (true)
12612 tree mem_initializer;
12614 token = cp_lexer_peek_token (parser->lexer);
12615 /* Parse the mem-initializer. */
12616 mem_initializer = cp_parser_mem_initializer (parser);
12617 /* If the next token is a `...', we're expanding member initializers. */
12618 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12620 /* Consume the `...'. */
12621 cp_lexer_consume_token (parser->lexer);
12623 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12624 can be expanded but members cannot. */
12625 if (mem_initializer != error_mark_node
12626 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12628 error_at (token->location,
12629 "cannot expand initializer for member %<%D%>",
12630 TREE_PURPOSE (mem_initializer));
12631 mem_initializer = error_mark_node;
12634 /* Construct the pack expansion type. */
12635 if (mem_initializer != error_mark_node)
12636 mem_initializer = make_pack_expansion (mem_initializer);
12638 if (target_ctor != error_mark_node
12639 && mem_initializer != error_mark_node)
12641 error ("mem-initializer for %qD follows constructor delegation",
12642 TREE_PURPOSE (mem_initializer));
12643 mem_initializer = error_mark_node;
12645 /* Look for a target constructor. */
12646 if (mem_initializer != error_mark_node
12647 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12648 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12650 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12651 if (mem_initializer_list)
12653 error ("constructor delegation follows mem-initializer for %qD",
12654 TREE_PURPOSE (mem_initializer_list));
12655 mem_initializer = error_mark_node;
12657 target_ctor = mem_initializer;
12659 /* Add it to the list, unless it was erroneous. */
12660 if (mem_initializer != error_mark_node)
12662 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12663 mem_initializer_list = mem_initializer;
12665 /* If the next token is not a `,', we're done. */
12666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12667 break;
12668 /* Consume the `,' token. */
12669 cp_lexer_consume_token (parser->lexer);
12672 /* Perform semantic analysis. */
12673 if (DECL_CONSTRUCTOR_P (current_function_decl))
12674 finish_mem_initializers (mem_initializer_list);
12677 /* Parse a mem-initializer.
12679 mem-initializer:
12680 mem-initializer-id ( expression-list [opt] )
12681 mem-initializer-id braced-init-list
12683 GNU extension:
12685 mem-initializer:
12686 ( expression-list [opt] )
12688 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12689 class) or FIELD_DECL (for a non-static data member) to initialize;
12690 the TREE_VALUE is the expression-list. An empty initialization
12691 list is represented by void_list_node. */
12693 static tree
12694 cp_parser_mem_initializer (cp_parser* parser)
12696 tree mem_initializer_id;
12697 tree expression_list;
12698 tree member;
12699 cp_token *token = cp_lexer_peek_token (parser->lexer);
12701 /* Find out what is being initialized. */
12702 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12704 permerror (token->location,
12705 "anachronistic old-style base class initializer");
12706 mem_initializer_id = NULL_TREE;
12708 else
12710 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12711 if (mem_initializer_id == error_mark_node)
12712 return mem_initializer_id;
12714 member = expand_member_init (mem_initializer_id);
12715 if (member && !DECL_P (member))
12716 in_base_initializer = 1;
12718 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12720 bool expr_non_constant_p;
12721 cp_lexer_set_source_position (parser->lexer);
12722 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12723 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12724 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12725 expression_list = build_tree_list (NULL_TREE, expression_list);
12727 else
12729 vec<tree, va_gc> *vec;
12730 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12731 /*cast_p=*/false,
12732 /*allow_expansion_p=*/true,
12733 /*non_constant_p=*/NULL);
12734 if (vec == NULL)
12735 return error_mark_node;
12736 expression_list = build_tree_list_vec (vec);
12737 release_tree_vector (vec);
12740 if (expression_list == error_mark_node)
12741 return error_mark_node;
12742 if (!expression_list)
12743 expression_list = void_type_node;
12745 in_base_initializer = 0;
12747 return member ? build_tree_list (member, expression_list) : error_mark_node;
12750 /* Parse a mem-initializer-id.
12752 mem-initializer-id:
12753 :: [opt] nested-name-specifier [opt] class-name
12754 identifier
12756 Returns a TYPE indicating the class to be initializer for the first
12757 production. Returns an IDENTIFIER_NODE indicating the data member
12758 to be initialized for the second production. */
12760 static tree
12761 cp_parser_mem_initializer_id (cp_parser* parser)
12763 bool global_scope_p;
12764 bool nested_name_specifier_p;
12765 bool template_p = false;
12766 tree id;
12768 cp_token *token = cp_lexer_peek_token (parser->lexer);
12770 /* `typename' is not allowed in this context ([temp.res]). */
12771 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12773 error_at (token->location,
12774 "keyword %<typename%> not allowed in this context (a qualified "
12775 "member initializer is implicitly a type)");
12776 cp_lexer_consume_token (parser->lexer);
12778 /* Look for the optional `::' operator. */
12779 global_scope_p
12780 = (cp_parser_global_scope_opt (parser,
12781 /*current_scope_valid_p=*/false)
12782 != NULL_TREE);
12783 /* Look for the optional nested-name-specifier. The simplest way to
12784 implement:
12786 [temp.res]
12788 The keyword `typename' is not permitted in a base-specifier or
12789 mem-initializer; in these contexts a qualified name that
12790 depends on a template-parameter is implicitly assumed to be a
12791 type name.
12793 is to assume that we have seen the `typename' keyword at this
12794 point. */
12795 nested_name_specifier_p
12796 = (cp_parser_nested_name_specifier_opt (parser,
12797 /*typename_keyword_p=*/true,
12798 /*check_dependency_p=*/true,
12799 /*type_p=*/true,
12800 /*is_declaration=*/true)
12801 != NULL_TREE);
12802 if (nested_name_specifier_p)
12803 template_p = cp_parser_optional_template_keyword (parser);
12804 /* If there is a `::' operator or a nested-name-specifier, then we
12805 are definitely looking for a class-name. */
12806 if (global_scope_p || nested_name_specifier_p)
12807 return cp_parser_class_name (parser,
12808 /*typename_keyword_p=*/true,
12809 /*template_keyword_p=*/template_p,
12810 typename_type,
12811 /*check_dependency_p=*/true,
12812 /*class_head_p=*/false,
12813 /*is_declaration=*/true);
12814 /* Otherwise, we could also be looking for an ordinary identifier. */
12815 cp_parser_parse_tentatively (parser);
12816 /* Try a class-name. */
12817 id = cp_parser_class_name (parser,
12818 /*typename_keyword_p=*/true,
12819 /*template_keyword_p=*/false,
12820 none_type,
12821 /*check_dependency_p=*/true,
12822 /*class_head_p=*/false,
12823 /*is_declaration=*/true);
12824 /* If we found one, we're done. */
12825 if (cp_parser_parse_definitely (parser))
12826 return id;
12827 /* Otherwise, look for an ordinary identifier. */
12828 return cp_parser_identifier (parser);
12831 /* Overloading [gram.over] */
12833 /* Parse an operator-function-id.
12835 operator-function-id:
12836 operator operator
12838 Returns an IDENTIFIER_NODE for the operator which is a
12839 human-readable spelling of the identifier, e.g., `operator +'. */
12841 static tree
12842 cp_parser_operator_function_id (cp_parser* parser)
12844 /* Look for the `operator' keyword. */
12845 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12846 return error_mark_node;
12847 /* And then the name of the operator itself. */
12848 return cp_parser_operator (parser);
12851 /* Return an identifier node for a user-defined literal operator.
12852 The suffix identifier is chained to the operator name identifier. */
12854 static tree
12855 cp_literal_operator_id (const char* name)
12857 tree identifier;
12858 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12859 + strlen (name) + 10);
12860 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12861 identifier = get_identifier (buffer);
12863 return identifier;
12866 /* Parse an operator.
12868 operator:
12869 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12870 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12871 || ++ -- , ->* -> () []
12873 GNU Extensions:
12875 operator:
12876 <? >? <?= >?=
12878 Returns an IDENTIFIER_NODE for the operator which is a
12879 human-readable spelling of the identifier, e.g., `operator +'. */
12881 static tree
12882 cp_parser_operator (cp_parser* parser)
12884 tree id = NULL_TREE;
12885 cp_token *token;
12886 bool utf8 = false;
12888 /* Peek at the next token. */
12889 token = cp_lexer_peek_token (parser->lexer);
12890 /* Figure out which operator we have. */
12891 switch (token->type)
12893 case CPP_KEYWORD:
12895 enum tree_code op;
12897 /* The keyword should be either `new' or `delete'. */
12898 if (token->keyword == RID_NEW)
12899 op = NEW_EXPR;
12900 else if (token->keyword == RID_DELETE)
12901 op = DELETE_EXPR;
12902 else
12903 break;
12905 /* Consume the `new' or `delete' token. */
12906 cp_lexer_consume_token (parser->lexer);
12908 /* Peek at the next token. */
12909 token = cp_lexer_peek_token (parser->lexer);
12910 /* If it's a `[' token then this is the array variant of the
12911 operator. */
12912 if (token->type == CPP_OPEN_SQUARE)
12914 /* Consume the `[' token. */
12915 cp_lexer_consume_token (parser->lexer);
12916 /* Look for the `]' token. */
12917 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12918 id = ansi_opname (op == NEW_EXPR
12919 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12921 /* Otherwise, we have the non-array variant. */
12922 else
12923 id = ansi_opname (op);
12925 return id;
12928 case CPP_PLUS:
12929 id = ansi_opname (PLUS_EXPR);
12930 break;
12932 case CPP_MINUS:
12933 id = ansi_opname (MINUS_EXPR);
12934 break;
12936 case CPP_MULT:
12937 id = ansi_opname (MULT_EXPR);
12938 break;
12940 case CPP_DIV:
12941 id = ansi_opname (TRUNC_DIV_EXPR);
12942 break;
12944 case CPP_MOD:
12945 id = ansi_opname (TRUNC_MOD_EXPR);
12946 break;
12948 case CPP_XOR:
12949 id = ansi_opname (BIT_XOR_EXPR);
12950 break;
12952 case CPP_AND:
12953 id = ansi_opname (BIT_AND_EXPR);
12954 break;
12956 case CPP_OR:
12957 id = ansi_opname (BIT_IOR_EXPR);
12958 break;
12960 case CPP_COMPL:
12961 id = ansi_opname (BIT_NOT_EXPR);
12962 break;
12964 case CPP_NOT:
12965 id = ansi_opname (TRUTH_NOT_EXPR);
12966 break;
12968 case CPP_EQ:
12969 id = ansi_assopname (NOP_EXPR);
12970 break;
12972 case CPP_LESS:
12973 id = ansi_opname (LT_EXPR);
12974 break;
12976 case CPP_GREATER:
12977 id = ansi_opname (GT_EXPR);
12978 break;
12980 case CPP_PLUS_EQ:
12981 id = ansi_assopname (PLUS_EXPR);
12982 break;
12984 case CPP_MINUS_EQ:
12985 id = ansi_assopname (MINUS_EXPR);
12986 break;
12988 case CPP_MULT_EQ:
12989 id = ansi_assopname (MULT_EXPR);
12990 break;
12992 case CPP_DIV_EQ:
12993 id = ansi_assopname (TRUNC_DIV_EXPR);
12994 break;
12996 case CPP_MOD_EQ:
12997 id = ansi_assopname (TRUNC_MOD_EXPR);
12998 break;
13000 case CPP_XOR_EQ:
13001 id = ansi_assopname (BIT_XOR_EXPR);
13002 break;
13004 case CPP_AND_EQ:
13005 id = ansi_assopname (BIT_AND_EXPR);
13006 break;
13008 case CPP_OR_EQ:
13009 id = ansi_assopname (BIT_IOR_EXPR);
13010 break;
13012 case CPP_LSHIFT:
13013 id = ansi_opname (LSHIFT_EXPR);
13014 break;
13016 case CPP_RSHIFT:
13017 id = ansi_opname (RSHIFT_EXPR);
13018 break;
13020 case CPP_LSHIFT_EQ:
13021 id = ansi_assopname (LSHIFT_EXPR);
13022 break;
13024 case CPP_RSHIFT_EQ:
13025 id = ansi_assopname (RSHIFT_EXPR);
13026 break;
13028 case CPP_EQ_EQ:
13029 id = ansi_opname (EQ_EXPR);
13030 break;
13032 case CPP_NOT_EQ:
13033 id = ansi_opname (NE_EXPR);
13034 break;
13036 case CPP_LESS_EQ:
13037 id = ansi_opname (LE_EXPR);
13038 break;
13040 case CPP_GREATER_EQ:
13041 id = ansi_opname (GE_EXPR);
13042 break;
13044 case CPP_AND_AND:
13045 id = ansi_opname (TRUTH_ANDIF_EXPR);
13046 break;
13048 case CPP_OR_OR:
13049 id = ansi_opname (TRUTH_ORIF_EXPR);
13050 break;
13052 case CPP_PLUS_PLUS:
13053 id = ansi_opname (POSTINCREMENT_EXPR);
13054 break;
13056 case CPP_MINUS_MINUS:
13057 id = ansi_opname (PREDECREMENT_EXPR);
13058 break;
13060 case CPP_COMMA:
13061 id = ansi_opname (COMPOUND_EXPR);
13062 break;
13064 case CPP_DEREF_STAR:
13065 id = ansi_opname (MEMBER_REF);
13066 break;
13068 case CPP_DEREF:
13069 id = ansi_opname (COMPONENT_REF);
13070 break;
13072 case CPP_OPEN_PAREN:
13073 /* Consume the `('. */
13074 cp_lexer_consume_token (parser->lexer);
13075 /* Look for the matching `)'. */
13076 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13077 return ansi_opname (CALL_EXPR);
13079 case CPP_OPEN_SQUARE:
13080 /* Consume the `['. */
13081 cp_lexer_consume_token (parser->lexer);
13082 /* Look for the matching `]'. */
13083 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13084 return ansi_opname (ARRAY_REF);
13086 case CPP_UTF8STRING:
13087 case CPP_UTF8STRING_USERDEF:
13088 utf8 = true;
13089 case CPP_STRING:
13090 case CPP_WSTRING:
13091 case CPP_STRING16:
13092 case CPP_STRING32:
13093 case CPP_STRING_USERDEF:
13094 case CPP_WSTRING_USERDEF:
13095 case CPP_STRING16_USERDEF:
13096 case CPP_STRING32_USERDEF:
13098 tree str, string_tree;
13099 int sz, len;
13101 if (cxx_dialect == cxx98)
13102 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13104 /* Consume the string. */
13105 str = cp_parser_string_literal (parser, /*translate=*/true,
13106 /*wide_ok=*/true, /*lookup_udlit=*/false);
13107 if (str == error_mark_node)
13108 return error_mark_node;
13109 else if (TREE_CODE (str) == USERDEF_LITERAL)
13111 string_tree = USERDEF_LITERAL_VALUE (str);
13112 id = USERDEF_LITERAL_SUFFIX_ID (str);
13114 else
13116 string_tree = str;
13117 /* Look for the suffix identifier. */
13118 token = cp_lexer_peek_token (parser->lexer);
13119 if (token->type == CPP_NAME)
13120 id = cp_parser_identifier (parser);
13121 else if (token->type == CPP_KEYWORD)
13123 error ("unexpected keyword;"
13124 " remove space between quotes and suffix identifier");
13125 return error_mark_node;
13127 else
13129 error ("expected suffix identifier");
13130 return error_mark_node;
13133 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13134 (TREE_TYPE (TREE_TYPE (string_tree))));
13135 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13136 if (len != 0)
13138 error ("expected empty string after %<operator%> keyword");
13139 return error_mark_node;
13141 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13142 != char_type_node)
13144 error ("invalid encoding prefix in literal operator");
13145 return error_mark_node;
13147 if (id != error_mark_node)
13149 const char *name = IDENTIFIER_POINTER (id);
13150 id = cp_literal_operator_id (name);
13152 return id;
13155 default:
13156 /* Anything else is an error. */
13157 break;
13160 /* If we have selected an identifier, we need to consume the
13161 operator token. */
13162 if (id)
13163 cp_lexer_consume_token (parser->lexer);
13164 /* Otherwise, no valid operator name was present. */
13165 else
13167 cp_parser_error (parser, "expected operator");
13168 id = error_mark_node;
13171 return id;
13174 /* Parse a template-declaration.
13176 template-declaration:
13177 export [opt] template < template-parameter-list > declaration
13179 If MEMBER_P is TRUE, this template-declaration occurs within a
13180 class-specifier.
13182 The grammar rule given by the standard isn't correct. What
13183 is really meant is:
13185 template-declaration:
13186 export [opt] template-parameter-list-seq
13187 decl-specifier-seq [opt] init-declarator [opt] ;
13188 export [opt] template-parameter-list-seq
13189 function-definition
13191 template-parameter-list-seq:
13192 template-parameter-list-seq [opt]
13193 template < template-parameter-list > */
13195 static void
13196 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13198 /* Check for `export'. */
13199 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13201 /* Consume the `export' token. */
13202 cp_lexer_consume_token (parser->lexer);
13203 /* Warn that we do not support `export'. */
13204 warning (0, "keyword %<export%> not implemented, and will be ignored");
13207 cp_parser_template_declaration_after_export (parser, member_p);
13210 /* Parse a template-parameter-list.
13212 template-parameter-list:
13213 template-parameter
13214 template-parameter-list , template-parameter
13216 Returns a TREE_LIST. Each node represents a template parameter.
13217 The nodes are connected via their TREE_CHAINs. */
13219 static tree
13220 cp_parser_template_parameter_list (cp_parser* parser)
13222 tree parameter_list = NULL_TREE;
13224 begin_template_parm_list ();
13226 /* The loop below parses the template parms. We first need to know
13227 the total number of template parms to be able to compute proper
13228 canonical types of each dependent type. So after the loop, when
13229 we know the total number of template parms,
13230 end_template_parm_list computes the proper canonical types and
13231 fixes up the dependent types accordingly. */
13232 while (true)
13234 tree parameter;
13235 bool is_non_type;
13236 bool is_parameter_pack;
13237 location_t parm_loc;
13239 /* Parse the template-parameter. */
13240 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13241 parameter = cp_parser_template_parameter (parser,
13242 &is_non_type,
13243 &is_parameter_pack);
13244 /* Add it to the list. */
13245 if (parameter != error_mark_node)
13246 parameter_list = process_template_parm (parameter_list,
13247 parm_loc,
13248 parameter,
13249 is_non_type,
13250 is_parameter_pack);
13251 else
13253 tree err_parm = build_tree_list (parameter, parameter);
13254 parameter_list = chainon (parameter_list, err_parm);
13257 /* If the next token is not a `,', we're done. */
13258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13259 break;
13260 /* Otherwise, consume the `,' token. */
13261 cp_lexer_consume_token (parser->lexer);
13264 return end_template_parm_list (parameter_list);
13267 /* Parse a template-parameter.
13269 template-parameter:
13270 type-parameter
13271 parameter-declaration
13273 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13274 the parameter. The TREE_PURPOSE is the default value, if any.
13275 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13276 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13277 set to true iff this parameter is a parameter pack. */
13279 static tree
13280 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13281 bool *is_parameter_pack)
13283 cp_token *token;
13284 cp_parameter_declarator *parameter_declarator;
13285 cp_declarator *id_declarator;
13286 tree parm;
13288 /* Assume it is a type parameter or a template parameter. */
13289 *is_non_type = false;
13290 /* Assume it not a parameter pack. */
13291 *is_parameter_pack = false;
13292 /* Peek at the next token. */
13293 token = cp_lexer_peek_token (parser->lexer);
13294 /* If it is `class' or `template', we have a type-parameter. */
13295 if (token->keyword == RID_TEMPLATE)
13296 return cp_parser_type_parameter (parser, is_parameter_pack);
13297 /* If it is `class' or `typename' we do not know yet whether it is a
13298 type parameter or a non-type parameter. Consider:
13300 template <typename T, typename T::X X> ...
13304 template <class C, class D*> ...
13306 Here, the first parameter is a type parameter, and the second is
13307 a non-type parameter. We can tell by looking at the token after
13308 the identifier -- if it is a `,', `=', or `>' then we have a type
13309 parameter. */
13310 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13312 /* Peek at the token after `class' or `typename'. */
13313 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13314 /* If it's an ellipsis, we have a template type parameter
13315 pack. */
13316 if (token->type == CPP_ELLIPSIS)
13317 return cp_parser_type_parameter (parser, is_parameter_pack);
13318 /* If it's an identifier, skip it. */
13319 if (token->type == CPP_NAME)
13320 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13321 /* Now, see if the token looks like the end of a template
13322 parameter. */
13323 if (token->type == CPP_COMMA
13324 || token->type == CPP_EQ
13325 || token->type == CPP_GREATER)
13326 return cp_parser_type_parameter (parser, is_parameter_pack);
13329 /* Otherwise, it is a non-type parameter.
13331 [temp.param]
13333 When parsing a default template-argument for a non-type
13334 template-parameter, the first non-nested `>' is taken as the end
13335 of the template parameter-list rather than a greater-than
13336 operator. */
13337 *is_non_type = true;
13338 parameter_declarator
13339 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13340 /*parenthesized_p=*/NULL);
13342 if (!parameter_declarator)
13343 return error_mark_node;
13345 /* If the parameter declaration is marked as a parameter pack, set
13346 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13347 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13348 grokdeclarator. */
13349 if (parameter_declarator->declarator
13350 && parameter_declarator->declarator->parameter_pack_p)
13352 *is_parameter_pack = true;
13353 parameter_declarator->declarator->parameter_pack_p = false;
13356 if (parameter_declarator->default_argument)
13358 /* Can happen in some cases of erroneous input (c++/34892). */
13359 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13360 /* Consume the `...' for better error recovery. */
13361 cp_lexer_consume_token (parser->lexer);
13363 /* If the next token is an ellipsis, and we don't already have it
13364 marked as a parameter pack, then we have a parameter pack (that
13365 has no declarator). */
13366 else if (!*is_parameter_pack
13367 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13368 && (declarator_can_be_parameter_pack
13369 (parameter_declarator->declarator)))
13371 /* Consume the `...'. */
13372 cp_lexer_consume_token (parser->lexer);
13373 maybe_warn_variadic_templates ();
13375 *is_parameter_pack = true;
13377 /* We might end up with a pack expansion as the type of the non-type
13378 template parameter, in which case this is a non-type template
13379 parameter pack. */
13380 else if (parameter_declarator->decl_specifiers.type
13381 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13383 *is_parameter_pack = true;
13384 parameter_declarator->decl_specifiers.type =
13385 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13388 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13390 /* Parameter packs cannot have default arguments. However, a
13391 user may try to do so, so we'll parse them and give an
13392 appropriate diagnostic here. */
13394 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13396 /* Find the name of the parameter pack. */
13397 id_declarator = parameter_declarator->declarator;
13398 while (id_declarator && id_declarator->kind != cdk_id)
13399 id_declarator = id_declarator->declarator;
13401 if (id_declarator && id_declarator->kind == cdk_id)
13402 error_at (start_token->location,
13403 "template parameter pack %qD cannot have a default argument",
13404 id_declarator->u.id.unqualified_name);
13405 else
13406 error_at (start_token->location,
13407 "template parameter pack cannot have a default argument");
13409 /* Parse the default argument, but throw away the result. */
13410 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13413 parm = grokdeclarator (parameter_declarator->declarator,
13414 &parameter_declarator->decl_specifiers,
13415 TPARM, /*initialized=*/0,
13416 /*attrlist=*/NULL);
13417 if (parm == error_mark_node)
13418 return error_mark_node;
13420 return build_tree_list (parameter_declarator->default_argument, parm);
13423 /* Parse a type-parameter.
13425 type-parameter:
13426 class identifier [opt]
13427 class identifier [opt] = type-id
13428 typename identifier [opt]
13429 typename identifier [opt] = type-id
13430 template < template-parameter-list > class identifier [opt]
13431 template < template-parameter-list > class identifier [opt]
13432 = id-expression
13434 GNU Extension (variadic templates):
13436 type-parameter:
13437 class ... identifier [opt]
13438 typename ... identifier [opt]
13440 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13441 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13442 the declaration of the parameter.
13444 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13446 static tree
13447 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13449 cp_token *token;
13450 tree parameter;
13452 /* Look for a keyword to tell us what kind of parameter this is. */
13453 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13454 if (!token)
13455 return error_mark_node;
13457 switch (token->keyword)
13459 case RID_CLASS:
13460 case RID_TYPENAME:
13462 tree identifier;
13463 tree default_argument;
13465 /* If the next token is an ellipsis, we have a template
13466 argument pack. */
13467 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13469 /* Consume the `...' token. */
13470 cp_lexer_consume_token (parser->lexer);
13471 maybe_warn_variadic_templates ();
13473 *is_parameter_pack = true;
13476 /* If the next token is an identifier, then it names the
13477 parameter. */
13478 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13479 identifier = cp_parser_identifier (parser);
13480 else
13481 identifier = NULL_TREE;
13483 /* Create the parameter. */
13484 parameter = finish_template_type_parm (class_type_node, identifier);
13486 /* If the next token is an `=', we have a default argument. */
13487 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13489 /* Consume the `=' token. */
13490 cp_lexer_consume_token (parser->lexer);
13491 /* Parse the default-argument. */
13492 push_deferring_access_checks (dk_no_deferred);
13493 default_argument = cp_parser_type_id (parser);
13495 /* Template parameter packs cannot have default
13496 arguments. */
13497 if (*is_parameter_pack)
13499 if (identifier)
13500 error_at (token->location,
13501 "template parameter pack %qD cannot have a "
13502 "default argument", identifier);
13503 else
13504 error_at (token->location,
13505 "template parameter packs cannot have "
13506 "default arguments");
13507 default_argument = NULL_TREE;
13509 else if (check_for_bare_parameter_packs (default_argument))
13510 default_argument = error_mark_node;
13511 pop_deferring_access_checks ();
13513 else
13514 default_argument = NULL_TREE;
13516 /* Create the combined representation of the parameter and the
13517 default argument. */
13518 parameter = build_tree_list (default_argument, parameter);
13520 break;
13522 case RID_TEMPLATE:
13524 tree identifier;
13525 tree default_argument;
13527 /* Look for the `<'. */
13528 cp_parser_require (parser, CPP_LESS, RT_LESS);
13529 /* Parse the template-parameter-list. */
13530 cp_parser_template_parameter_list (parser);
13531 /* Look for the `>'. */
13532 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13533 /* Look for the `class' or 'typename' keywords. */
13534 cp_parser_type_parameter_key (parser);
13535 /* If the next token is an ellipsis, we have a template
13536 argument pack. */
13537 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13539 /* Consume the `...' token. */
13540 cp_lexer_consume_token (parser->lexer);
13541 maybe_warn_variadic_templates ();
13543 *is_parameter_pack = true;
13545 /* If the next token is an `=', then there is a
13546 default-argument. If the next token is a `>', we are at
13547 the end of the parameter-list. If the next token is a `,',
13548 then we are at the end of this parameter. */
13549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13550 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13551 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13553 identifier = cp_parser_identifier (parser);
13554 /* Treat invalid names as if the parameter were nameless. */
13555 if (identifier == error_mark_node)
13556 identifier = NULL_TREE;
13558 else
13559 identifier = NULL_TREE;
13561 /* Create the template parameter. */
13562 parameter = finish_template_template_parm (class_type_node,
13563 identifier);
13565 /* If the next token is an `=', then there is a
13566 default-argument. */
13567 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13569 bool is_template;
13571 /* Consume the `='. */
13572 cp_lexer_consume_token (parser->lexer);
13573 /* Parse the id-expression. */
13574 push_deferring_access_checks (dk_no_deferred);
13575 /* save token before parsing the id-expression, for error
13576 reporting */
13577 token = cp_lexer_peek_token (parser->lexer);
13578 default_argument
13579 = cp_parser_id_expression (parser,
13580 /*template_keyword_p=*/false,
13581 /*check_dependency_p=*/true,
13582 /*template_p=*/&is_template,
13583 /*declarator_p=*/false,
13584 /*optional_p=*/false);
13585 if (TREE_CODE (default_argument) == TYPE_DECL)
13586 /* If the id-expression was a template-id that refers to
13587 a template-class, we already have the declaration here,
13588 so no further lookup is needed. */
13590 else
13591 /* Look up the name. */
13592 default_argument
13593 = cp_parser_lookup_name (parser, default_argument,
13594 none_type,
13595 /*is_template=*/is_template,
13596 /*is_namespace=*/false,
13597 /*check_dependency=*/true,
13598 /*ambiguous_decls=*/NULL,
13599 token->location);
13600 /* See if the default argument is valid. */
13601 default_argument
13602 = check_template_template_default_arg (default_argument);
13604 /* Template parameter packs cannot have default
13605 arguments. */
13606 if (*is_parameter_pack)
13608 if (identifier)
13609 error_at (token->location,
13610 "template parameter pack %qD cannot "
13611 "have a default argument",
13612 identifier);
13613 else
13614 error_at (token->location, "template parameter packs cannot "
13615 "have default arguments");
13616 default_argument = NULL_TREE;
13618 pop_deferring_access_checks ();
13620 else
13621 default_argument = NULL_TREE;
13623 /* Create the combined representation of the parameter and the
13624 default argument. */
13625 parameter = build_tree_list (default_argument, parameter);
13627 break;
13629 default:
13630 gcc_unreachable ();
13631 break;
13634 return parameter;
13637 /* Parse a template-id.
13639 template-id:
13640 template-name < template-argument-list [opt] >
13642 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13643 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13644 returned. Otherwise, if the template-name names a function, or set
13645 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13646 names a class, returns a TYPE_DECL for the specialization.
13648 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13649 uninstantiated templates. */
13651 static tree
13652 cp_parser_template_id (cp_parser *parser,
13653 bool template_keyword_p,
13654 bool check_dependency_p,
13655 enum tag_types tag_type,
13656 bool is_declaration)
13658 int i;
13659 tree templ;
13660 tree arguments;
13661 tree template_id;
13662 cp_token_position start_of_id = 0;
13663 deferred_access_check *chk;
13664 vec<deferred_access_check, va_gc> *access_check;
13665 cp_token *next_token = NULL, *next_token_2 = NULL;
13666 bool is_identifier;
13668 /* If the next token corresponds to a template-id, there is no need
13669 to reparse it. */
13670 next_token = cp_lexer_peek_token (parser->lexer);
13671 if (next_token->type == CPP_TEMPLATE_ID)
13673 struct tree_check *check_value;
13675 /* Get the stored value. */
13676 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13677 /* Perform any access checks that were deferred. */
13678 access_check = check_value->checks;
13679 if (access_check)
13681 FOR_EACH_VEC_ELT (*access_check, i, chk)
13682 perform_or_defer_access_check (chk->binfo,
13683 chk->decl,
13684 chk->diag_decl,
13685 tf_warning_or_error);
13687 /* Return the stored value. */
13688 return check_value->value;
13691 /* Avoid performing name lookup if there is no possibility of
13692 finding a template-id. */
13693 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13694 || (next_token->type == CPP_NAME
13695 && !cp_parser_nth_token_starts_template_argument_list_p
13696 (parser, 2)))
13698 cp_parser_error (parser, "expected template-id");
13699 return error_mark_node;
13702 /* Remember where the template-id starts. */
13703 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13704 start_of_id = cp_lexer_token_position (parser->lexer, false);
13706 push_deferring_access_checks (dk_deferred);
13708 /* Parse the template-name. */
13709 is_identifier = false;
13710 templ = cp_parser_template_name (parser, template_keyword_p,
13711 check_dependency_p,
13712 is_declaration,
13713 tag_type,
13714 &is_identifier);
13715 if (templ == error_mark_node || is_identifier)
13717 pop_deferring_access_checks ();
13718 return templ;
13721 /* If we find the sequence `[:' after a template-name, it's probably
13722 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13723 parse correctly the argument list. */
13724 next_token = cp_lexer_peek_token (parser->lexer);
13725 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13726 if (next_token->type == CPP_OPEN_SQUARE
13727 && next_token->flags & DIGRAPH
13728 && next_token_2->type == CPP_COLON
13729 && !(next_token_2->flags & PREV_WHITE))
13731 cp_parser_parse_tentatively (parser);
13732 /* Change `:' into `::'. */
13733 next_token_2->type = CPP_SCOPE;
13734 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13735 CPP_LESS. */
13736 cp_lexer_consume_token (parser->lexer);
13738 /* Parse the arguments. */
13739 arguments = cp_parser_enclosed_template_argument_list (parser);
13740 if (!cp_parser_parse_definitely (parser))
13742 /* If we couldn't parse an argument list, then we revert our changes
13743 and return simply an error. Maybe this is not a template-id
13744 after all. */
13745 next_token_2->type = CPP_COLON;
13746 cp_parser_error (parser, "expected %<<%>");
13747 pop_deferring_access_checks ();
13748 return error_mark_node;
13750 /* Otherwise, emit an error about the invalid digraph, but continue
13751 parsing because we got our argument list. */
13752 if (permerror (next_token->location,
13753 "%<<::%> cannot begin a template-argument list"))
13755 static bool hint = false;
13756 inform (next_token->location,
13757 "%<<:%> is an alternate spelling for %<[%>."
13758 " Insert whitespace between %<<%> and %<::%>");
13759 if (!hint && !flag_permissive)
13761 inform (next_token->location, "(if you use %<-fpermissive%> "
13762 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13763 "accept your code)");
13764 hint = true;
13768 else
13770 /* Look for the `<' that starts the template-argument-list. */
13771 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13773 pop_deferring_access_checks ();
13774 return error_mark_node;
13776 /* Parse the arguments. */
13777 arguments = cp_parser_enclosed_template_argument_list (parser);
13780 /* Build a representation of the specialization. */
13781 if (identifier_p (templ))
13782 template_id = build_min_nt_loc (next_token->location,
13783 TEMPLATE_ID_EXPR,
13784 templ, arguments);
13785 else if (DECL_TYPE_TEMPLATE_P (templ)
13786 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13788 bool entering_scope;
13789 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13790 template (rather than some instantiation thereof) only if
13791 is not nested within some other construct. For example, in
13792 "template <typename T> void f(T) { A<T>::", A<T> is just an
13793 instantiation of A. */
13794 entering_scope = (template_parm_scope_p ()
13795 && cp_lexer_next_token_is (parser->lexer,
13796 CPP_SCOPE));
13797 template_id
13798 = finish_template_type (templ, arguments, entering_scope);
13800 else if (variable_template_p (templ))
13802 template_id = lookup_template_variable (templ, arguments);
13804 else
13806 /* If it's not a class-template or a template-template, it should be
13807 a function-template. */
13808 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13809 || TREE_CODE (templ) == OVERLOAD
13810 || BASELINK_P (templ)));
13812 template_id = lookup_template_function (templ, arguments);
13815 /* If parsing tentatively, replace the sequence of tokens that makes
13816 up the template-id with a CPP_TEMPLATE_ID token. That way,
13817 should we re-parse the token stream, we will not have to repeat
13818 the effort required to do the parse, nor will we issue duplicate
13819 error messages about problems during instantiation of the
13820 template. */
13821 if (start_of_id
13822 /* Don't do this if we had a parse error in a declarator; re-parsing
13823 might succeed if a name changes meaning (60361). */
13824 && !(cp_parser_error_occurred (parser)
13825 && cp_parser_parsing_tentatively (parser)
13826 && parser->in_declarator_p))
13828 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13830 /* Reset the contents of the START_OF_ID token. */
13831 token->type = CPP_TEMPLATE_ID;
13832 /* Retrieve any deferred checks. Do not pop this access checks yet
13833 so the memory will not be reclaimed during token replacing below. */
13834 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13835 token->u.tree_check_value->value = template_id;
13836 token->u.tree_check_value->checks = get_deferred_access_checks ();
13837 token->keyword = RID_MAX;
13839 /* Purge all subsequent tokens. */
13840 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13842 /* ??? Can we actually assume that, if template_id ==
13843 error_mark_node, we will have issued a diagnostic to the
13844 user, as opposed to simply marking the tentative parse as
13845 failed? */
13846 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13847 error_at (token->location, "parse error in template argument list");
13850 pop_to_parent_deferring_access_checks ();
13851 return template_id;
13854 /* Parse a template-name.
13856 template-name:
13857 identifier
13859 The standard should actually say:
13861 template-name:
13862 identifier
13863 operator-function-id
13865 A defect report has been filed about this issue.
13867 A conversion-function-id cannot be a template name because they cannot
13868 be part of a template-id. In fact, looking at this code:
13870 a.operator K<int>()
13872 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13873 It is impossible to call a templated conversion-function-id with an
13874 explicit argument list, since the only allowed template parameter is
13875 the type to which it is converting.
13877 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13878 `template' keyword, in a construction like:
13880 T::template f<3>()
13882 In that case `f' is taken to be a template-name, even though there
13883 is no way of knowing for sure.
13885 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13886 name refers to a set of overloaded functions, at least one of which
13887 is a template, or an IDENTIFIER_NODE with the name of the template,
13888 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13889 names are looked up inside uninstantiated templates. */
13891 static tree
13892 cp_parser_template_name (cp_parser* parser,
13893 bool template_keyword_p,
13894 bool check_dependency_p,
13895 bool is_declaration,
13896 enum tag_types tag_type,
13897 bool *is_identifier)
13899 tree identifier;
13900 tree decl;
13901 tree fns;
13902 cp_token *token = cp_lexer_peek_token (parser->lexer);
13904 /* If the next token is `operator', then we have either an
13905 operator-function-id or a conversion-function-id. */
13906 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13908 /* We don't know whether we're looking at an
13909 operator-function-id or a conversion-function-id. */
13910 cp_parser_parse_tentatively (parser);
13911 /* Try an operator-function-id. */
13912 identifier = cp_parser_operator_function_id (parser);
13913 /* If that didn't work, try a conversion-function-id. */
13914 if (!cp_parser_parse_definitely (parser))
13916 cp_parser_error (parser, "expected template-name");
13917 return error_mark_node;
13920 /* Look for the identifier. */
13921 else
13922 identifier = cp_parser_identifier (parser);
13924 /* If we didn't find an identifier, we don't have a template-id. */
13925 if (identifier == error_mark_node)
13926 return error_mark_node;
13928 /* If the name immediately followed the `template' keyword, then it
13929 is a template-name. However, if the next token is not `<', then
13930 we do not treat it as a template-name, since it is not being used
13931 as part of a template-id. This enables us to handle constructs
13932 like:
13934 template <typename T> struct S { S(); };
13935 template <typename T> S<T>::S();
13937 correctly. We would treat `S' as a template -- if it were `S<T>'
13938 -- but we do not if there is no `<'. */
13940 if (processing_template_decl
13941 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13943 /* In a declaration, in a dependent context, we pretend that the
13944 "template" keyword was present in order to improve error
13945 recovery. For example, given:
13947 template <typename T> void f(T::X<int>);
13949 we want to treat "X<int>" as a template-id. */
13950 if (is_declaration
13951 && !template_keyword_p
13952 && parser->scope && TYPE_P (parser->scope)
13953 && check_dependency_p
13954 && dependent_scope_p (parser->scope)
13955 /* Do not do this for dtors (or ctors), since they never
13956 need the template keyword before their name. */
13957 && !constructor_name_p (identifier, parser->scope))
13959 cp_token_position start = 0;
13961 /* Explain what went wrong. */
13962 error_at (token->location, "non-template %qD used as template",
13963 identifier);
13964 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13965 parser->scope, identifier);
13966 /* If parsing tentatively, find the location of the "<" token. */
13967 if (cp_parser_simulate_error (parser))
13968 start = cp_lexer_token_position (parser->lexer, true);
13969 /* Parse the template arguments so that we can issue error
13970 messages about them. */
13971 cp_lexer_consume_token (parser->lexer);
13972 cp_parser_enclosed_template_argument_list (parser);
13973 /* Skip tokens until we find a good place from which to
13974 continue parsing. */
13975 cp_parser_skip_to_closing_parenthesis (parser,
13976 /*recovering=*/true,
13977 /*or_comma=*/true,
13978 /*consume_paren=*/false);
13979 /* If parsing tentatively, permanently remove the
13980 template argument list. That will prevent duplicate
13981 error messages from being issued about the missing
13982 "template" keyword. */
13983 if (start)
13984 cp_lexer_purge_tokens_after (parser->lexer, start);
13985 if (is_identifier)
13986 *is_identifier = true;
13987 return identifier;
13990 /* If the "template" keyword is present, then there is generally
13991 no point in doing name-lookup, so we just return IDENTIFIER.
13992 But, if the qualifying scope is non-dependent then we can
13993 (and must) do name-lookup normally. */
13994 if (template_keyword_p
13995 && (!parser->scope
13996 || (TYPE_P (parser->scope)
13997 && dependent_type_p (parser->scope))))
13998 return identifier;
14001 /* Look up the name. */
14002 decl = cp_parser_lookup_name (parser, identifier,
14003 tag_type,
14004 /*is_template=*/true,
14005 /*is_namespace=*/false,
14006 check_dependency_p,
14007 /*ambiguous_decls=*/NULL,
14008 token->location);
14010 /* If DECL is a template, then the name was a template-name. */
14011 if (TREE_CODE (decl) == TEMPLATE_DECL)
14013 if (TREE_DEPRECATED (decl)
14014 && deprecated_state != DEPRECATED_SUPPRESS)
14015 warn_deprecated_use (decl, NULL_TREE);
14017 else
14019 tree fn = NULL_TREE;
14021 /* The standard does not explicitly indicate whether a name that
14022 names a set of overloaded declarations, some of which are
14023 templates, is a template-name. However, such a name should
14024 be a template-name; otherwise, there is no way to form a
14025 template-id for the overloaded templates. */
14026 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14027 if (TREE_CODE (fns) == OVERLOAD)
14028 for (fn = fns; fn; fn = OVL_NEXT (fn))
14029 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14030 break;
14032 if (!fn)
14034 /* The name does not name a template. */
14035 cp_parser_error (parser, "expected template-name");
14036 return error_mark_node;
14040 /* If DECL is dependent, and refers to a function, then just return
14041 its name; we will look it up again during template instantiation. */
14042 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14044 tree scope = ovl_scope (decl);
14045 if (TYPE_P (scope) && dependent_type_p (scope))
14046 return identifier;
14049 return decl;
14052 /* Parse a template-argument-list.
14054 template-argument-list:
14055 template-argument ... [opt]
14056 template-argument-list , template-argument ... [opt]
14058 Returns a TREE_VEC containing the arguments. */
14060 static tree
14061 cp_parser_template_argument_list (cp_parser* parser)
14063 tree fixed_args[10];
14064 unsigned n_args = 0;
14065 unsigned alloced = 10;
14066 tree *arg_ary = fixed_args;
14067 tree vec;
14068 bool saved_in_template_argument_list_p;
14069 bool saved_ice_p;
14070 bool saved_non_ice_p;
14072 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14073 parser->in_template_argument_list_p = true;
14074 /* Even if the template-id appears in an integral
14075 constant-expression, the contents of the argument list do
14076 not. */
14077 saved_ice_p = parser->integral_constant_expression_p;
14078 parser->integral_constant_expression_p = false;
14079 saved_non_ice_p = parser->non_integral_constant_expression_p;
14080 parser->non_integral_constant_expression_p = false;
14082 /* Parse the arguments. */
14085 tree argument;
14087 if (n_args)
14088 /* Consume the comma. */
14089 cp_lexer_consume_token (parser->lexer);
14091 /* Parse the template-argument. */
14092 argument = cp_parser_template_argument (parser);
14094 /* If the next token is an ellipsis, we're expanding a template
14095 argument pack. */
14096 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14098 if (argument == error_mark_node)
14100 cp_token *token = cp_lexer_peek_token (parser->lexer);
14101 error_at (token->location,
14102 "expected parameter pack before %<...%>");
14104 /* Consume the `...' token. */
14105 cp_lexer_consume_token (parser->lexer);
14107 /* Make the argument into a TYPE_PACK_EXPANSION or
14108 EXPR_PACK_EXPANSION. */
14109 argument = make_pack_expansion (argument);
14112 if (n_args == alloced)
14114 alloced *= 2;
14116 if (arg_ary == fixed_args)
14118 arg_ary = XNEWVEC (tree, alloced);
14119 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14121 else
14122 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14124 arg_ary[n_args++] = argument;
14126 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14128 vec = make_tree_vec (n_args);
14130 while (n_args--)
14131 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14133 if (arg_ary != fixed_args)
14134 free (arg_ary);
14135 parser->non_integral_constant_expression_p = saved_non_ice_p;
14136 parser->integral_constant_expression_p = saved_ice_p;
14137 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14138 #ifdef ENABLE_CHECKING
14139 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14140 #endif
14141 return vec;
14144 /* Parse a template-argument.
14146 template-argument:
14147 assignment-expression
14148 type-id
14149 id-expression
14151 The representation is that of an assignment-expression, type-id, or
14152 id-expression -- except that the qualified id-expression is
14153 evaluated, so that the value returned is either a DECL or an
14154 OVERLOAD.
14156 Although the standard says "assignment-expression", it forbids
14157 throw-expressions or assignments in the template argument.
14158 Therefore, we use "conditional-expression" instead. */
14160 static tree
14161 cp_parser_template_argument (cp_parser* parser)
14163 tree argument;
14164 bool template_p;
14165 bool address_p;
14166 bool maybe_type_id = false;
14167 cp_token *token = NULL, *argument_start_token = NULL;
14168 location_t loc = 0;
14169 cp_id_kind idk;
14171 /* There's really no way to know what we're looking at, so we just
14172 try each alternative in order.
14174 [temp.arg]
14176 In a template-argument, an ambiguity between a type-id and an
14177 expression is resolved to a type-id, regardless of the form of
14178 the corresponding template-parameter.
14180 Therefore, we try a type-id first. */
14181 cp_parser_parse_tentatively (parser);
14182 argument = cp_parser_template_type_arg (parser);
14183 /* If there was no error parsing the type-id but the next token is a
14184 '>>', our behavior depends on which dialect of C++ we're
14185 parsing. In C++98, we probably found a typo for '> >'. But there
14186 are type-id which are also valid expressions. For instance:
14188 struct X { int operator >> (int); };
14189 template <int V> struct Foo {};
14190 Foo<X () >> 5> r;
14192 Here 'X()' is a valid type-id of a function type, but the user just
14193 wanted to write the expression "X() >> 5". Thus, we remember that we
14194 found a valid type-id, but we still try to parse the argument as an
14195 expression to see what happens.
14197 In C++0x, the '>>' will be considered two separate '>'
14198 tokens. */
14199 if (!cp_parser_error_occurred (parser)
14200 && cxx_dialect == cxx98
14201 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14203 maybe_type_id = true;
14204 cp_parser_abort_tentative_parse (parser);
14206 else
14208 /* If the next token isn't a `,' or a `>', then this argument wasn't
14209 really finished. This means that the argument is not a valid
14210 type-id. */
14211 if (!cp_parser_next_token_ends_template_argument_p (parser))
14212 cp_parser_error (parser, "expected template-argument");
14213 /* If that worked, we're done. */
14214 if (cp_parser_parse_definitely (parser))
14215 return argument;
14217 /* We're still not sure what the argument will be. */
14218 cp_parser_parse_tentatively (parser);
14219 /* Try a template. */
14220 argument_start_token = cp_lexer_peek_token (parser->lexer);
14221 argument = cp_parser_id_expression (parser,
14222 /*template_keyword_p=*/false,
14223 /*check_dependency_p=*/true,
14224 &template_p,
14225 /*declarator_p=*/false,
14226 /*optional_p=*/false);
14227 /* If the next token isn't a `,' or a `>', then this argument wasn't
14228 really finished. */
14229 if (!cp_parser_next_token_ends_template_argument_p (parser))
14230 cp_parser_error (parser, "expected template-argument");
14231 if (!cp_parser_error_occurred (parser))
14233 /* Figure out what is being referred to. If the id-expression
14234 was for a class template specialization, then we will have a
14235 TYPE_DECL at this point. There is no need to do name lookup
14236 at this point in that case. */
14237 if (TREE_CODE (argument) != TYPE_DECL)
14238 argument = cp_parser_lookup_name (parser, argument,
14239 none_type,
14240 /*is_template=*/template_p,
14241 /*is_namespace=*/false,
14242 /*check_dependency=*/true,
14243 /*ambiguous_decls=*/NULL,
14244 argument_start_token->location);
14245 if (TREE_CODE (argument) != TEMPLATE_DECL
14246 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14247 cp_parser_error (parser, "expected template-name");
14249 if (cp_parser_parse_definitely (parser))
14251 if (TREE_DEPRECATED (argument))
14252 warn_deprecated_use (argument, NULL_TREE);
14253 return argument;
14255 /* It must be a non-type argument. There permitted cases are given
14256 in [temp.arg.nontype]:
14258 -- an integral constant-expression of integral or enumeration
14259 type; or
14261 -- the name of a non-type template-parameter; or
14263 -- the name of an object or function with external linkage...
14265 -- the address of an object or function with external linkage...
14267 -- a pointer to member... */
14268 /* Look for a non-type template parameter. */
14269 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14271 cp_parser_parse_tentatively (parser);
14272 argument = cp_parser_primary_expression (parser,
14273 /*address_p=*/false,
14274 /*cast_p=*/false,
14275 /*template_arg_p=*/true,
14276 &idk);
14277 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14278 || !cp_parser_next_token_ends_template_argument_p (parser))
14279 cp_parser_simulate_error (parser);
14280 if (cp_parser_parse_definitely (parser))
14281 return argument;
14284 /* If the next token is "&", the argument must be the address of an
14285 object or function with external linkage. */
14286 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14287 if (address_p)
14289 loc = cp_lexer_peek_token (parser->lexer)->location;
14290 cp_lexer_consume_token (parser->lexer);
14292 /* See if we might have an id-expression. */
14293 token = cp_lexer_peek_token (parser->lexer);
14294 if (token->type == CPP_NAME
14295 || token->keyword == RID_OPERATOR
14296 || token->type == CPP_SCOPE
14297 || token->type == CPP_TEMPLATE_ID
14298 || token->type == CPP_NESTED_NAME_SPECIFIER)
14300 cp_parser_parse_tentatively (parser);
14301 argument = cp_parser_primary_expression (parser,
14302 address_p,
14303 /*cast_p=*/false,
14304 /*template_arg_p=*/true,
14305 &idk);
14306 if (cp_parser_error_occurred (parser)
14307 || !cp_parser_next_token_ends_template_argument_p (parser))
14308 cp_parser_abort_tentative_parse (parser);
14309 else
14311 tree probe;
14313 if (INDIRECT_REF_P (argument))
14315 /* Strip the dereference temporarily. */
14316 gcc_assert (REFERENCE_REF_P (argument));
14317 argument = TREE_OPERAND (argument, 0);
14320 /* If we're in a template, we represent a qualified-id referring
14321 to a static data member as a SCOPE_REF even if the scope isn't
14322 dependent so that we can check access control later. */
14323 probe = argument;
14324 if (TREE_CODE (probe) == SCOPE_REF)
14325 probe = TREE_OPERAND (probe, 1);
14326 if (VAR_P (probe))
14328 /* A variable without external linkage might still be a
14329 valid constant-expression, so no error is issued here
14330 if the external-linkage check fails. */
14331 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14332 cp_parser_simulate_error (parser);
14334 else if (is_overloaded_fn (argument))
14335 /* All overloaded functions are allowed; if the external
14336 linkage test does not pass, an error will be issued
14337 later. */
14339 else if (address_p
14340 && (TREE_CODE (argument) == OFFSET_REF
14341 || TREE_CODE (argument) == SCOPE_REF))
14342 /* A pointer-to-member. */
14344 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14346 else
14347 cp_parser_simulate_error (parser);
14349 if (cp_parser_parse_definitely (parser))
14351 if (address_p)
14352 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14353 tf_warning_or_error);
14354 else
14355 argument = convert_from_reference (argument);
14356 return argument;
14360 /* If the argument started with "&", there are no other valid
14361 alternatives at this point. */
14362 if (address_p)
14364 cp_parser_error (parser, "invalid non-type template argument");
14365 return error_mark_node;
14368 /* If the argument wasn't successfully parsed as a type-id followed
14369 by '>>', the argument can only be a constant expression now.
14370 Otherwise, we try parsing the constant-expression tentatively,
14371 because the argument could really be a type-id. */
14372 if (maybe_type_id)
14373 cp_parser_parse_tentatively (parser);
14374 argument = cp_parser_constant_expression (parser);
14376 if (!maybe_type_id)
14377 return argument;
14378 if (!cp_parser_next_token_ends_template_argument_p (parser))
14379 cp_parser_error (parser, "expected template-argument");
14380 if (cp_parser_parse_definitely (parser))
14381 return argument;
14382 /* We did our best to parse the argument as a non type-id, but that
14383 was the only alternative that matched (albeit with a '>' after
14384 it). We can assume it's just a typo from the user, and a
14385 diagnostic will then be issued. */
14386 return cp_parser_template_type_arg (parser);
14389 /* Parse an explicit-instantiation.
14391 explicit-instantiation:
14392 template declaration
14394 Although the standard says `declaration', what it really means is:
14396 explicit-instantiation:
14397 template decl-specifier-seq [opt] declarator [opt] ;
14399 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14400 supposed to be allowed. A defect report has been filed about this
14401 issue.
14403 GNU Extension:
14405 explicit-instantiation:
14406 storage-class-specifier template
14407 decl-specifier-seq [opt] declarator [opt] ;
14408 function-specifier template
14409 decl-specifier-seq [opt] declarator [opt] ; */
14411 static void
14412 cp_parser_explicit_instantiation (cp_parser* parser)
14414 int declares_class_or_enum;
14415 cp_decl_specifier_seq decl_specifiers;
14416 tree extension_specifier = NULL_TREE;
14418 timevar_push (TV_TEMPLATE_INST);
14420 /* Look for an (optional) storage-class-specifier or
14421 function-specifier. */
14422 if (cp_parser_allow_gnu_extensions_p (parser))
14424 extension_specifier
14425 = cp_parser_storage_class_specifier_opt (parser);
14426 if (!extension_specifier)
14427 extension_specifier
14428 = cp_parser_function_specifier_opt (parser,
14429 /*decl_specs=*/NULL);
14432 /* Look for the `template' keyword. */
14433 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14434 /* Let the front end know that we are processing an explicit
14435 instantiation. */
14436 begin_explicit_instantiation ();
14437 /* [temp.explicit] says that we are supposed to ignore access
14438 control while processing explicit instantiation directives. */
14439 push_deferring_access_checks (dk_no_check);
14440 /* Parse a decl-specifier-seq. */
14441 cp_parser_decl_specifier_seq (parser,
14442 CP_PARSER_FLAGS_OPTIONAL,
14443 &decl_specifiers,
14444 &declares_class_or_enum);
14445 /* If there was exactly one decl-specifier, and it declared a class,
14446 and there's no declarator, then we have an explicit type
14447 instantiation. */
14448 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14450 tree type;
14452 type = check_tag_decl (&decl_specifiers,
14453 /*explicit_type_instantiation_p=*/true);
14454 /* Turn access control back on for names used during
14455 template instantiation. */
14456 pop_deferring_access_checks ();
14457 if (type)
14458 do_type_instantiation (type, extension_specifier,
14459 /*complain=*/tf_error);
14461 else
14463 cp_declarator *declarator;
14464 tree decl;
14466 /* Parse the declarator. */
14467 declarator
14468 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14469 /*ctor_dtor_or_conv_p=*/NULL,
14470 /*parenthesized_p=*/NULL,
14471 /*member_p=*/false,
14472 /*friend_p=*/false);
14473 if (declares_class_or_enum & 2)
14474 cp_parser_check_for_definition_in_return_type (declarator,
14475 decl_specifiers.type,
14476 decl_specifiers.locations[ds_type_spec]);
14477 if (declarator != cp_error_declarator)
14479 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14480 permerror (decl_specifiers.locations[ds_inline],
14481 "explicit instantiation shall not use"
14482 " %<inline%> specifier");
14483 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14484 permerror (decl_specifiers.locations[ds_constexpr],
14485 "explicit instantiation shall not use"
14486 " %<constexpr%> specifier");
14488 decl = grokdeclarator (declarator, &decl_specifiers,
14489 NORMAL, 0, &decl_specifiers.attributes);
14490 /* Turn access control back on for names used during
14491 template instantiation. */
14492 pop_deferring_access_checks ();
14493 /* Do the explicit instantiation. */
14494 do_decl_instantiation (decl, extension_specifier);
14496 else
14498 pop_deferring_access_checks ();
14499 /* Skip the body of the explicit instantiation. */
14500 cp_parser_skip_to_end_of_statement (parser);
14503 /* We're done with the instantiation. */
14504 end_explicit_instantiation ();
14506 cp_parser_consume_semicolon_at_end_of_statement (parser);
14508 timevar_pop (TV_TEMPLATE_INST);
14511 /* Parse an explicit-specialization.
14513 explicit-specialization:
14514 template < > declaration
14516 Although the standard says `declaration', what it really means is:
14518 explicit-specialization:
14519 template <> decl-specifier [opt] init-declarator [opt] ;
14520 template <> function-definition
14521 template <> explicit-specialization
14522 template <> template-declaration */
14524 static void
14525 cp_parser_explicit_specialization (cp_parser* parser)
14527 bool need_lang_pop;
14528 cp_token *token = cp_lexer_peek_token (parser->lexer);
14530 /* Look for the `template' keyword. */
14531 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14532 /* Look for the `<'. */
14533 cp_parser_require (parser, CPP_LESS, RT_LESS);
14534 /* Look for the `>'. */
14535 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14536 /* We have processed another parameter list. */
14537 ++parser->num_template_parameter_lists;
14538 /* [temp]
14540 A template ... explicit specialization ... shall not have C
14541 linkage. */
14542 if (current_lang_name == lang_name_c)
14544 error_at (token->location, "template specialization with C linkage");
14545 /* Give it C++ linkage to avoid confusing other parts of the
14546 front end. */
14547 push_lang_context (lang_name_cplusplus);
14548 need_lang_pop = true;
14550 else
14551 need_lang_pop = false;
14552 /* Let the front end know that we are beginning a specialization. */
14553 if (!begin_specialization ())
14555 end_specialization ();
14556 return;
14559 /* If the next keyword is `template', we need to figure out whether
14560 or not we're looking a template-declaration. */
14561 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14563 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14564 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14565 cp_parser_template_declaration_after_export (parser,
14566 /*member_p=*/false);
14567 else
14568 cp_parser_explicit_specialization (parser);
14570 else
14571 /* Parse the dependent declaration. */
14572 cp_parser_single_declaration (parser,
14573 /*checks=*/NULL,
14574 /*member_p=*/false,
14575 /*explicit_specialization_p=*/true,
14576 /*friend_p=*/NULL);
14577 /* We're done with the specialization. */
14578 end_specialization ();
14579 /* For the erroneous case of a template with C linkage, we pushed an
14580 implicit C++ linkage scope; exit that scope now. */
14581 if (need_lang_pop)
14582 pop_lang_context ();
14583 /* We're done with this parameter list. */
14584 --parser->num_template_parameter_lists;
14587 /* Parse a type-specifier.
14589 type-specifier:
14590 simple-type-specifier
14591 class-specifier
14592 enum-specifier
14593 elaborated-type-specifier
14594 cv-qualifier
14596 GNU Extension:
14598 type-specifier:
14599 __complex__
14601 Returns a representation of the type-specifier. For a
14602 class-specifier, enum-specifier, or elaborated-type-specifier, a
14603 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14605 The parser flags FLAGS is used to control type-specifier parsing.
14607 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14608 in a decl-specifier-seq.
14610 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14611 class-specifier, enum-specifier, or elaborated-type-specifier, then
14612 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14613 if a type is declared; 2 if it is defined. Otherwise, it is set to
14614 zero.
14616 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14617 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14618 is set to FALSE. */
14620 static tree
14621 cp_parser_type_specifier (cp_parser* parser,
14622 cp_parser_flags flags,
14623 cp_decl_specifier_seq *decl_specs,
14624 bool is_declaration,
14625 int* declares_class_or_enum,
14626 bool* is_cv_qualifier)
14628 tree type_spec = NULL_TREE;
14629 cp_token *token;
14630 enum rid keyword;
14631 cp_decl_spec ds = ds_last;
14633 /* Assume this type-specifier does not declare a new type. */
14634 if (declares_class_or_enum)
14635 *declares_class_or_enum = 0;
14636 /* And that it does not specify a cv-qualifier. */
14637 if (is_cv_qualifier)
14638 *is_cv_qualifier = false;
14639 /* Peek at the next token. */
14640 token = cp_lexer_peek_token (parser->lexer);
14642 /* If we're looking at a keyword, we can use that to guide the
14643 production we choose. */
14644 keyword = token->keyword;
14645 switch (keyword)
14647 case RID_ENUM:
14648 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14649 goto elaborated_type_specifier;
14651 /* Look for the enum-specifier. */
14652 type_spec = cp_parser_enum_specifier (parser);
14653 /* If that worked, we're done. */
14654 if (type_spec)
14656 if (declares_class_or_enum)
14657 *declares_class_or_enum = 2;
14658 if (decl_specs)
14659 cp_parser_set_decl_spec_type (decl_specs,
14660 type_spec,
14661 token,
14662 /*type_definition_p=*/true);
14663 return type_spec;
14665 else
14666 goto elaborated_type_specifier;
14668 /* Any of these indicate either a class-specifier, or an
14669 elaborated-type-specifier. */
14670 case RID_CLASS:
14671 case RID_STRUCT:
14672 case RID_UNION:
14673 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14674 goto elaborated_type_specifier;
14676 /* Parse tentatively so that we can back up if we don't find a
14677 class-specifier. */
14678 cp_parser_parse_tentatively (parser);
14679 /* Look for the class-specifier. */
14680 type_spec = cp_parser_class_specifier (parser);
14681 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14682 /* If that worked, we're done. */
14683 if (cp_parser_parse_definitely (parser))
14685 if (declares_class_or_enum)
14686 *declares_class_or_enum = 2;
14687 if (decl_specs)
14688 cp_parser_set_decl_spec_type (decl_specs,
14689 type_spec,
14690 token,
14691 /*type_definition_p=*/true);
14692 return type_spec;
14695 /* Fall through. */
14696 elaborated_type_specifier:
14697 /* We're declaring (not defining) a class or enum. */
14698 if (declares_class_or_enum)
14699 *declares_class_or_enum = 1;
14701 /* Fall through. */
14702 case RID_TYPENAME:
14703 /* Look for an elaborated-type-specifier. */
14704 type_spec
14705 = (cp_parser_elaborated_type_specifier
14706 (parser,
14707 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14708 is_declaration));
14709 if (decl_specs)
14710 cp_parser_set_decl_spec_type (decl_specs,
14711 type_spec,
14712 token,
14713 /*type_definition_p=*/false);
14714 return type_spec;
14716 case RID_CONST:
14717 ds = ds_const;
14718 if (is_cv_qualifier)
14719 *is_cv_qualifier = true;
14720 break;
14722 case RID_VOLATILE:
14723 ds = ds_volatile;
14724 if (is_cv_qualifier)
14725 *is_cv_qualifier = true;
14726 break;
14728 case RID_RESTRICT:
14729 ds = ds_restrict;
14730 if (is_cv_qualifier)
14731 *is_cv_qualifier = true;
14732 break;
14734 case RID_COMPLEX:
14735 /* The `__complex__' keyword is a GNU extension. */
14736 ds = ds_complex;
14737 break;
14739 default:
14740 break;
14743 /* Handle simple keywords. */
14744 if (ds != ds_last)
14746 if (decl_specs)
14748 set_and_check_decl_spec_loc (decl_specs, ds, token);
14749 decl_specs->any_specifiers_p = true;
14751 return cp_lexer_consume_token (parser->lexer)->u.value;
14754 /* If we do not already have a type-specifier, assume we are looking
14755 at a simple-type-specifier. */
14756 type_spec = cp_parser_simple_type_specifier (parser,
14757 decl_specs,
14758 flags);
14760 /* If we didn't find a type-specifier, and a type-specifier was not
14761 optional in this context, issue an error message. */
14762 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14764 cp_parser_error (parser, "expected type specifier");
14765 return error_mark_node;
14768 return type_spec;
14771 /* Parse a simple-type-specifier.
14773 simple-type-specifier:
14774 :: [opt] nested-name-specifier [opt] type-name
14775 :: [opt] nested-name-specifier template template-id
14776 char
14777 wchar_t
14778 bool
14779 short
14781 long
14782 signed
14783 unsigned
14784 float
14785 double
14786 void
14788 C++0x Extension:
14790 simple-type-specifier:
14791 auto
14792 decltype ( expression )
14793 char16_t
14794 char32_t
14795 __underlying_type ( type-id )
14797 GNU Extension:
14799 simple-type-specifier:
14800 __int128
14801 __typeof__ unary-expression
14802 __typeof__ ( type-id )
14803 __typeof__ ( type-id ) { initializer-list , [opt] }
14805 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14806 appropriately updated. */
14808 static tree
14809 cp_parser_simple_type_specifier (cp_parser* parser,
14810 cp_decl_specifier_seq *decl_specs,
14811 cp_parser_flags flags)
14813 tree type = NULL_TREE;
14814 cp_token *token;
14815 int idx;
14817 /* Peek at the next token. */
14818 token = cp_lexer_peek_token (parser->lexer);
14820 /* If we're looking at a keyword, things are easy. */
14821 switch (token->keyword)
14823 case RID_CHAR:
14824 if (decl_specs)
14825 decl_specs->explicit_char_p = true;
14826 type = char_type_node;
14827 break;
14828 case RID_CHAR16:
14829 type = char16_type_node;
14830 break;
14831 case RID_CHAR32:
14832 type = char32_type_node;
14833 break;
14834 case RID_WCHAR:
14835 type = wchar_type_node;
14836 break;
14837 case RID_BOOL:
14838 type = boolean_type_node;
14839 break;
14840 case RID_SHORT:
14841 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14842 type = short_integer_type_node;
14843 break;
14844 case RID_INT:
14845 if (decl_specs)
14846 decl_specs->explicit_int_p = true;
14847 type = integer_type_node;
14848 break;
14849 case RID_INT_N_0:
14850 case RID_INT_N_1:
14851 case RID_INT_N_2:
14852 case RID_INT_N_3:
14853 idx = token->keyword - RID_INT_N_0;
14854 if (! int_n_enabled_p [idx])
14855 break;
14856 if (decl_specs)
14858 decl_specs->explicit_intN_p = true;
14859 decl_specs->int_n_idx = idx;
14861 type = int_n_trees [idx].signed_type;
14862 break;
14863 case RID_LONG:
14864 if (decl_specs)
14865 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14866 type = long_integer_type_node;
14867 break;
14868 case RID_SIGNED:
14869 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14870 type = integer_type_node;
14871 break;
14872 case RID_UNSIGNED:
14873 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14874 type = unsigned_type_node;
14875 break;
14876 case RID_FLOAT:
14877 type = float_type_node;
14878 break;
14879 case RID_DOUBLE:
14880 type = double_type_node;
14881 break;
14882 case RID_VOID:
14883 type = void_type_node;
14884 break;
14886 case RID_AUTO:
14887 maybe_warn_cpp0x (CPP0X_AUTO);
14888 if (parser->auto_is_implicit_function_template_parm_p)
14890 if (cxx_dialect >= cxx14)
14891 type = synthesize_implicit_template_parm (parser);
14892 else
14893 type = error_mark_node;
14895 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14897 if (cxx_dialect < cxx14)
14898 error_at (token->location,
14899 "use of %<auto%> in lambda parameter declaration "
14900 "only available with "
14901 "-std=c++14 or -std=gnu++14");
14903 else if (cxx_dialect < cxx14)
14904 error_at (token->location,
14905 "use of %<auto%> in parameter declaration "
14906 "only available with "
14907 "-std=c++14 or -std=gnu++14");
14908 else
14909 pedwarn (token->location, OPT_Wpedantic,
14910 "ISO C++ forbids use of %<auto%> in parameter "
14911 "declaration");
14913 else
14914 type = make_auto ();
14915 break;
14917 case RID_DECLTYPE:
14918 /* Since DR 743, decltype can either be a simple-type-specifier by
14919 itself or begin a nested-name-specifier. Parsing it will replace
14920 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14921 handling below decide what to do. */
14922 cp_parser_decltype (parser);
14923 cp_lexer_set_token_position (parser->lexer, token);
14924 break;
14926 case RID_TYPEOF:
14927 /* Consume the `typeof' token. */
14928 cp_lexer_consume_token (parser->lexer);
14929 /* Parse the operand to `typeof'. */
14930 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14931 /* If it is not already a TYPE, take its type. */
14932 if (!TYPE_P (type))
14933 type = finish_typeof (type);
14935 if (decl_specs)
14936 cp_parser_set_decl_spec_type (decl_specs, type,
14937 token,
14938 /*type_definition_p=*/false);
14940 return type;
14942 case RID_UNDERLYING_TYPE:
14943 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14944 if (decl_specs)
14945 cp_parser_set_decl_spec_type (decl_specs, type,
14946 token,
14947 /*type_definition_p=*/false);
14949 return type;
14951 case RID_BASES:
14952 case RID_DIRECT_BASES:
14953 type = cp_parser_trait_expr (parser, token->keyword);
14954 if (decl_specs)
14955 cp_parser_set_decl_spec_type (decl_specs, type,
14956 token,
14957 /*type_definition_p=*/false);
14958 return type;
14959 default:
14960 break;
14963 /* If token is an already-parsed decltype not followed by ::,
14964 it's a simple-type-specifier. */
14965 if (token->type == CPP_DECLTYPE
14966 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14968 type = token->u.value;
14969 if (decl_specs)
14971 cp_parser_set_decl_spec_type (decl_specs, type,
14972 token,
14973 /*type_definition_p=*/false);
14974 /* Remember that we are handling a decltype in order to
14975 implement the resolution of DR 1510 when the argument
14976 isn't instantiation dependent. */
14977 decl_specs->decltype_p = true;
14979 cp_lexer_consume_token (parser->lexer);
14980 return type;
14983 /* If the type-specifier was for a built-in type, we're done. */
14984 if (type)
14986 /* Record the type. */
14987 if (decl_specs
14988 && (token->keyword != RID_SIGNED
14989 && token->keyword != RID_UNSIGNED
14990 && token->keyword != RID_SHORT
14991 && token->keyword != RID_LONG))
14992 cp_parser_set_decl_spec_type (decl_specs,
14993 type,
14994 token,
14995 /*type_definition_p=*/false);
14996 if (decl_specs)
14997 decl_specs->any_specifiers_p = true;
14999 /* Consume the token. */
15000 cp_lexer_consume_token (parser->lexer);
15002 if (type == error_mark_node)
15003 return error_mark_node;
15005 /* There is no valid C++ program where a non-template type is
15006 followed by a "<". That usually indicates that the user thought
15007 that the type was a template. */
15008 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15009 token->location);
15011 return TYPE_NAME (type);
15014 /* The type-specifier must be a user-defined type. */
15015 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15017 bool qualified_p;
15018 bool global_p;
15020 /* Don't gobble tokens or issue error messages if this is an
15021 optional type-specifier. */
15022 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15023 cp_parser_parse_tentatively (parser);
15025 /* Look for the optional `::' operator. */
15026 global_p
15027 = (cp_parser_global_scope_opt (parser,
15028 /*current_scope_valid_p=*/false)
15029 != NULL_TREE);
15030 /* Look for the nested-name specifier. */
15031 qualified_p
15032 = (cp_parser_nested_name_specifier_opt (parser,
15033 /*typename_keyword_p=*/false,
15034 /*check_dependency_p=*/true,
15035 /*type_p=*/false,
15036 /*is_declaration=*/false)
15037 != NULL_TREE);
15038 token = cp_lexer_peek_token (parser->lexer);
15039 /* If we have seen a nested-name-specifier, and the next token
15040 is `template', then we are using the template-id production. */
15041 if (parser->scope
15042 && cp_parser_optional_template_keyword (parser))
15044 /* Look for the template-id. */
15045 type = cp_parser_template_id (parser,
15046 /*template_keyword_p=*/true,
15047 /*check_dependency_p=*/true,
15048 none_type,
15049 /*is_declaration=*/false);
15050 /* If the template-id did not name a type, we are out of
15051 luck. */
15052 if (TREE_CODE (type) != TYPE_DECL)
15054 cp_parser_error (parser, "expected template-id for type");
15055 type = NULL_TREE;
15058 /* Otherwise, look for a type-name. */
15059 else
15060 type = cp_parser_type_name (parser);
15061 /* Keep track of all name-lookups performed in class scopes. */
15062 if (type
15063 && !global_p
15064 && !qualified_p
15065 && TREE_CODE (type) == TYPE_DECL
15066 && identifier_p (DECL_NAME (type)))
15067 maybe_note_name_used_in_class (DECL_NAME (type), type);
15068 /* If it didn't work out, we don't have a TYPE. */
15069 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15070 && !cp_parser_parse_definitely (parser))
15071 type = NULL_TREE;
15072 if (type && decl_specs)
15073 cp_parser_set_decl_spec_type (decl_specs, type,
15074 token,
15075 /*type_definition_p=*/false);
15078 /* If we didn't get a type-name, issue an error message. */
15079 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15081 cp_parser_error (parser, "expected type-name");
15082 return error_mark_node;
15085 if (type && type != error_mark_node)
15087 /* See if TYPE is an Objective-C type, and if so, parse and
15088 accept any protocol references following it. Do this before
15089 the cp_parser_check_for_invalid_template_id() call, because
15090 Objective-C types can be followed by '<...>' which would
15091 enclose protocol names rather than template arguments, and so
15092 everything is fine. */
15093 if (c_dialect_objc () && !parser->scope
15094 && (objc_is_id (type) || objc_is_class_name (type)))
15096 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15097 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15099 /* Clobber the "unqualified" type previously entered into
15100 DECL_SPECS with the new, improved protocol-qualified version. */
15101 if (decl_specs)
15102 decl_specs->type = qual_type;
15104 return qual_type;
15107 /* There is no valid C++ program where a non-template type is
15108 followed by a "<". That usually indicates that the user
15109 thought that the type was a template. */
15110 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15111 none_type,
15112 token->location);
15115 return type;
15118 /* Parse a type-name.
15120 type-name:
15121 class-name
15122 enum-name
15123 typedef-name
15124 simple-template-id [in c++0x]
15126 enum-name:
15127 identifier
15129 typedef-name:
15130 identifier
15132 Returns a TYPE_DECL for the type. */
15134 static tree
15135 cp_parser_type_name (cp_parser* parser)
15137 tree type_decl;
15139 /* We can't know yet whether it is a class-name or not. */
15140 cp_parser_parse_tentatively (parser);
15141 /* Try a class-name. */
15142 type_decl = cp_parser_class_name (parser,
15143 /*typename_keyword_p=*/false,
15144 /*template_keyword_p=*/false,
15145 none_type,
15146 /*check_dependency_p=*/true,
15147 /*class_head_p=*/false,
15148 /*is_declaration=*/false);
15149 /* If it's not a class-name, keep looking. */
15150 if (!cp_parser_parse_definitely (parser))
15152 if (cxx_dialect < cxx11)
15153 /* It must be a typedef-name or an enum-name. */
15154 return cp_parser_nonclass_name (parser);
15156 cp_parser_parse_tentatively (parser);
15157 /* It is either a simple-template-id representing an
15158 instantiation of an alias template... */
15159 type_decl = cp_parser_template_id (parser,
15160 /*template_keyword_p=*/false,
15161 /*check_dependency_p=*/true,
15162 none_type,
15163 /*is_declaration=*/false);
15164 /* Note that this must be an instantiation of an alias template
15165 because [temp.names]/6 says:
15167 A template-id that names an alias template specialization
15168 is a type-name.
15170 Whereas [temp.names]/7 says:
15172 A simple-template-id that names a class template
15173 specialization is a class-name. */
15174 if (type_decl != NULL_TREE
15175 && TREE_CODE (type_decl) == TYPE_DECL
15176 && TYPE_DECL_ALIAS_P (type_decl))
15177 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15178 else
15179 cp_parser_simulate_error (parser);
15181 if (!cp_parser_parse_definitely (parser))
15182 /* ... Or a typedef-name or an enum-name. */
15183 return cp_parser_nonclass_name (parser);
15186 return type_decl;
15189 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15191 enum-name:
15192 identifier
15194 typedef-name:
15195 identifier
15197 Returns a TYPE_DECL for the type. */
15199 static tree
15200 cp_parser_nonclass_name (cp_parser* parser)
15202 tree type_decl;
15203 tree identifier;
15205 cp_token *token = cp_lexer_peek_token (parser->lexer);
15206 identifier = cp_parser_identifier (parser);
15207 if (identifier == error_mark_node)
15208 return error_mark_node;
15210 /* Look up the type-name. */
15211 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15213 type_decl = strip_using_decl (type_decl);
15215 if (TREE_CODE (type_decl) != TYPE_DECL
15216 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15218 /* See if this is an Objective-C type. */
15219 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15220 tree type = objc_get_protocol_qualified_type (identifier, protos);
15221 if (type)
15222 type_decl = TYPE_NAME (type);
15225 /* Issue an error if we did not find a type-name. */
15226 if (TREE_CODE (type_decl) != TYPE_DECL
15227 /* In Objective-C, we have the complication that class names are
15228 normally type names and start declarations (eg, the
15229 "NSObject" in "NSObject *object;"), but can be used in an
15230 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15231 is an expression. So, a classname followed by a dot is not a
15232 valid type-name. */
15233 || (objc_is_class_name (TREE_TYPE (type_decl))
15234 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15236 if (!cp_parser_simulate_error (parser))
15237 cp_parser_name_lookup_error (parser, identifier, type_decl,
15238 NLE_TYPE, token->location);
15239 return error_mark_node;
15241 /* Remember that the name was used in the definition of the
15242 current class so that we can check later to see if the
15243 meaning would have been different after the class was
15244 entirely defined. */
15245 else if (type_decl != error_mark_node
15246 && !parser->scope)
15247 maybe_note_name_used_in_class (identifier, type_decl);
15249 return type_decl;
15252 /* Parse an elaborated-type-specifier. Note that the grammar given
15253 here incorporates the resolution to DR68.
15255 elaborated-type-specifier:
15256 class-key :: [opt] nested-name-specifier [opt] identifier
15257 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15258 enum-key :: [opt] nested-name-specifier [opt] identifier
15259 typename :: [opt] nested-name-specifier identifier
15260 typename :: [opt] nested-name-specifier template [opt]
15261 template-id
15263 GNU extension:
15265 elaborated-type-specifier:
15266 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15267 class-key attributes :: [opt] nested-name-specifier [opt]
15268 template [opt] template-id
15269 enum attributes :: [opt] nested-name-specifier [opt] identifier
15271 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15272 declared `friend'. If IS_DECLARATION is TRUE, then this
15273 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15274 something is being declared.
15276 Returns the TYPE specified. */
15278 static tree
15279 cp_parser_elaborated_type_specifier (cp_parser* parser,
15280 bool is_friend,
15281 bool is_declaration)
15283 enum tag_types tag_type;
15284 tree identifier;
15285 tree type = NULL_TREE;
15286 tree attributes = NULL_TREE;
15287 tree globalscope;
15288 cp_token *token = NULL;
15290 /* See if we're looking at the `enum' keyword. */
15291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15293 /* Consume the `enum' token. */
15294 cp_lexer_consume_token (parser->lexer);
15295 /* Remember that it's an enumeration type. */
15296 tag_type = enum_type;
15297 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15298 enums) is used here. */
15299 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15300 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15302 pedwarn (input_location, 0, "elaborated-type-specifier "
15303 "for a scoped enum must not use the %<%D%> keyword",
15304 cp_lexer_peek_token (parser->lexer)->u.value);
15305 /* Consume the `struct' or `class' and parse it anyway. */
15306 cp_lexer_consume_token (parser->lexer);
15308 /* Parse the attributes. */
15309 attributes = cp_parser_attributes_opt (parser);
15311 /* Or, it might be `typename'. */
15312 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15313 RID_TYPENAME))
15315 /* Consume the `typename' token. */
15316 cp_lexer_consume_token (parser->lexer);
15317 /* Remember that it's a `typename' type. */
15318 tag_type = typename_type;
15320 /* Otherwise it must be a class-key. */
15321 else
15323 tag_type = cp_parser_class_key (parser);
15324 if (tag_type == none_type)
15325 return error_mark_node;
15326 /* Parse the attributes. */
15327 attributes = cp_parser_attributes_opt (parser);
15330 /* Look for the `::' operator. */
15331 globalscope = cp_parser_global_scope_opt (parser,
15332 /*current_scope_valid_p=*/false);
15333 /* Look for the nested-name-specifier. */
15334 if (tag_type == typename_type && !globalscope)
15336 if (!cp_parser_nested_name_specifier (parser,
15337 /*typename_keyword_p=*/true,
15338 /*check_dependency_p=*/true,
15339 /*type_p=*/true,
15340 is_declaration))
15341 return error_mark_node;
15343 else
15344 /* Even though `typename' is not present, the proposed resolution
15345 to Core Issue 180 says that in `class A<T>::B', `B' should be
15346 considered a type-name, even if `A<T>' is dependent. */
15347 cp_parser_nested_name_specifier_opt (parser,
15348 /*typename_keyword_p=*/true,
15349 /*check_dependency_p=*/true,
15350 /*type_p=*/true,
15351 is_declaration);
15352 /* For everything but enumeration types, consider a template-id.
15353 For an enumeration type, consider only a plain identifier. */
15354 if (tag_type != enum_type)
15356 bool template_p = false;
15357 tree decl;
15359 /* Allow the `template' keyword. */
15360 template_p = cp_parser_optional_template_keyword (parser);
15361 /* If we didn't see `template', we don't know if there's a
15362 template-id or not. */
15363 if (!template_p)
15364 cp_parser_parse_tentatively (parser);
15365 /* Parse the template-id. */
15366 token = cp_lexer_peek_token (parser->lexer);
15367 decl = cp_parser_template_id (parser, template_p,
15368 /*check_dependency_p=*/true,
15369 tag_type,
15370 is_declaration);
15371 /* If we didn't find a template-id, look for an ordinary
15372 identifier. */
15373 if (!template_p && !cp_parser_parse_definitely (parser))
15375 /* We can get here when cp_parser_template_id, called by
15376 cp_parser_class_name with tag_type == none_type, succeeds
15377 and caches a BASELINK. Then, when called again here,
15378 instead of failing and returning an error_mark_node
15379 returns it (see template/typename17.C in C++11).
15380 ??? Could we diagnose this earlier? */
15381 else if (tag_type == typename_type && BASELINK_P (decl))
15383 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15384 type = error_mark_node;
15386 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15387 in effect, then we must assume that, upon instantiation, the
15388 template will correspond to a class. */
15389 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15390 && tag_type == typename_type)
15391 type = make_typename_type (parser->scope, decl,
15392 typename_type,
15393 /*complain=*/tf_error);
15394 /* If the `typename' keyword is in effect and DECL is not a type
15395 decl, then type is non existent. */
15396 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15398 else if (TREE_CODE (decl) == TYPE_DECL)
15399 type = check_elaborated_type_specifier (tag_type, decl,
15400 /*allow_template_p=*/true);
15401 else if (decl == error_mark_node)
15402 type = error_mark_node;
15405 if (!type)
15407 token = cp_lexer_peek_token (parser->lexer);
15408 identifier = cp_parser_identifier (parser);
15410 if (identifier == error_mark_node)
15412 parser->scope = NULL_TREE;
15413 return error_mark_node;
15416 /* For a `typename', we needn't call xref_tag. */
15417 if (tag_type == typename_type
15418 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15419 return cp_parser_make_typename_type (parser, identifier,
15420 token->location);
15422 /* Template parameter lists apply only if we are not within a
15423 function parameter list. */
15424 bool template_parm_lists_apply
15425 = parser->num_template_parameter_lists;
15426 if (template_parm_lists_apply)
15427 for (cp_binding_level *s = current_binding_level;
15428 s && s->kind != sk_template_parms;
15429 s = s->level_chain)
15430 if (s->kind == sk_function_parms)
15431 template_parm_lists_apply = false;
15433 /* Look up a qualified name in the usual way. */
15434 if (parser->scope)
15436 tree decl;
15437 tree ambiguous_decls;
15439 decl = cp_parser_lookup_name (parser, identifier,
15440 tag_type,
15441 /*is_template=*/false,
15442 /*is_namespace=*/false,
15443 /*check_dependency=*/true,
15444 &ambiguous_decls,
15445 token->location);
15447 /* If the lookup was ambiguous, an error will already have been
15448 issued. */
15449 if (ambiguous_decls)
15450 return error_mark_node;
15452 /* If we are parsing friend declaration, DECL may be a
15453 TEMPLATE_DECL tree node here. However, we need to check
15454 whether this TEMPLATE_DECL results in valid code. Consider
15455 the following example:
15457 namespace N {
15458 template <class T> class C {};
15460 class X {
15461 template <class T> friend class N::C; // #1, valid code
15463 template <class T> class Y {
15464 friend class N::C; // #2, invalid code
15467 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15468 name lookup of `N::C'. We see that friend declaration must
15469 be template for the code to be valid. Note that
15470 processing_template_decl does not work here since it is
15471 always 1 for the above two cases. */
15473 decl = (cp_parser_maybe_treat_template_as_class
15474 (decl, /*tag_name_p=*/is_friend
15475 && template_parm_lists_apply));
15477 if (TREE_CODE (decl) != TYPE_DECL)
15479 cp_parser_diagnose_invalid_type_name (parser,
15480 identifier,
15481 token->location);
15482 return error_mark_node;
15485 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15487 bool allow_template = (template_parm_lists_apply
15488 || DECL_SELF_REFERENCE_P (decl));
15489 type = check_elaborated_type_specifier (tag_type, decl,
15490 allow_template);
15492 if (type == error_mark_node)
15493 return error_mark_node;
15496 /* Forward declarations of nested types, such as
15498 class C1::C2;
15499 class C1::C2::C3;
15501 are invalid unless all components preceding the final '::'
15502 are complete. If all enclosing types are complete, these
15503 declarations become merely pointless.
15505 Invalid forward declarations of nested types are errors
15506 caught elsewhere in parsing. Those that are pointless arrive
15507 here. */
15509 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15510 && !is_friend && !processing_explicit_instantiation)
15511 warning (0, "declaration %qD does not declare anything", decl);
15513 type = TREE_TYPE (decl);
15515 else
15517 /* An elaborated-type-specifier sometimes introduces a new type and
15518 sometimes names an existing type. Normally, the rule is that it
15519 introduces a new type only if there is not an existing type of
15520 the same name already in scope. For example, given:
15522 struct S {};
15523 void f() { struct S s; }
15525 the `struct S' in the body of `f' is the same `struct S' as in
15526 the global scope; the existing definition is used. However, if
15527 there were no global declaration, this would introduce a new
15528 local class named `S'.
15530 An exception to this rule applies to the following code:
15532 namespace N { struct S; }
15534 Here, the elaborated-type-specifier names a new type
15535 unconditionally; even if there is already an `S' in the
15536 containing scope this declaration names a new type.
15537 This exception only applies if the elaborated-type-specifier
15538 forms the complete declaration:
15540 [class.name]
15542 A declaration consisting solely of `class-key identifier ;' is
15543 either a redeclaration of the name in the current scope or a
15544 forward declaration of the identifier as a class name. It
15545 introduces the name into the current scope.
15547 We are in this situation precisely when the next token is a `;'.
15549 An exception to the exception is that a `friend' declaration does
15550 *not* name a new type; i.e., given:
15552 struct S { friend struct T; };
15554 `T' is not a new type in the scope of `S'.
15556 Also, `new struct S' or `sizeof (struct S)' never results in the
15557 definition of a new type; a new type can only be declared in a
15558 declaration context. */
15560 tag_scope ts;
15561 bool template_p;
15563 if (is_friend)
15564 /* Friends have special name lookup rules. */
15565 ts = ts_within_enclosing_non_class;
15566 else if (is_declaration
15567 && cp_lexer_next_token_is (parser->lexer,
15568 CPP_SEMICOLON))
15569 /* This is a `class-key identifier ;' */
15570 ts = ts_current;
15571 else
15572 ts = ts_global;
15574 template_p =
15575 (template_parm_lists_apply
15576 && (cp_parser_next_token_starts_class_definition_p (parser)
15577 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15578 /* An unqualified name was used to reference this type, so
15579 there were no qualifying templates. */
15580 if (template_parm_lists_apply
15581 && !cp_parser_check_template_parameters (parser,
15582 /*num_templates=*/0,
15583 token->location,
15584 /*declarator=*/NULL))
15585 return error_mark_node;
15586 type = xref_tag (tag_type, identifier, ts, template_p);
15590 if (type == error_mark_node)
15591 return error_mark_node;
15593 /* Allow attributes on forward declarations of classes. */
15594 if (attributes)
15596 if (TREE_CODE (type) == TYPENAME_TYPE)
15597 warning (OPT_Wattributes,
15598 "attributes ignored on uninstantiated type");
15599 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15600 && ! processing_explicit_instantiation)
15601 warning (OPT_Wattributes,
15602 "attributes ignored on template instantiation");
15603 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15604 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15605 else
15606 warning (OPT_Wattributes,
15607 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15610 if (tag_type != enum_type)
15612 /* Indicate whether this class was declared as a `class' or as a
15613 `struct'. */
15614 if (TREE_CODE (type) == RECORD_TYPE)
15615 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15616 cp_parser_check_class_key (tag_type, type);
15619 /* A "<" cannot follow an elaborated type specifier. If that
15620 happens, the user was probably trying to form a template-id. */
15621 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15622 token->location);
15624 return type;
15627 /* Parse an enum-specifier.
15629 enum-specifier:
15630 enum-head { enumerator-list [opt] }
15631 enum-head { enumerator-list , } [C++0x]
15633 enum-head:
15634 enum-key identifier [opt] enum-base [opt]
15635 enum-key nested-name-specifier identifier enum-base [opt]
15637 enum-key:
15638 enum
15639 enum class [C++0x]
15640 enum struct [C++0x]
15642 enum-base: [C++0x]
15643 : type-specifier-seq
15645 opaque-enum-specifier:
15646 enum-key identifier enum-base [opt] ;
15648 GNU Extensions:
15649 enum-key attributes[opt] identifier [opt] enum-base [opt]
15650 { enumerator-list [opt] }attributes[opt]
15651 enum-key attributes[opt] identifier [opt] enum-base [opt]
15652 { enumerator-list, }attributes[opt] [C++0x]
15654 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15655 if the token stream isn't an enum-specifier after all. */
15657 static tree
15658 cp_parser_enum_specifier (cp_parser* parser)
15660 tree identifier;
15661 tree type = NULL_TREE;
15662 tree prev_scope;
15663 tree nested_name_specifier = NULL_TREE;
15664 tree attributes;
15665 bool scoped_enum_p = false;
15666 bool has_underlying_type = false;
15667 bool nested_being_defined = false;
15668 bool new_value_list = false;
15669 bool is_new_type = false;
15670 bool is_anonymous = false;
15671 tree underlying_type = NULL_TREE;
15672 cp_token *type_start_token = NULL;
15673 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15675 parser->colon_corrects_to_scope_p = false;
15677 /* Parse tentatively so that we can back up if we don't find a
15678 enum-specifier. */
15679 cp_parser_parse_tentatively (parser);
15681 /* Caller guarantees that the current token is 'enum', an identifier
15682 possibly follows, and the token after that is an opening brace.
15683 If we don't have an identifier, fabricate an anonymous name for
15684 the enumeration being defined. */
15685 cp_lexer_consume_token (parser->lexer);
15687 /* Parse the "class" or "struct", which indicates a scoped
15688 enumeration type in C++0x. */
15689 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15690 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15692 if (cxx_dialect < cxx11)
15693 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15695 /* Consume the `struct' or `class' token. */
15696 cp_lexer_consume_token (parser->lexer);
15698 scoped_enum_p = true;
15701 attributes = cp_parser_attributes_opt (parser);
15703 /* Clear the qualification. */
15704 parser->scope = NULL_TREE;
15705 parser->qualifying_scope = NULL_TREE;
15706 parser->object_scope = NULL_TREE;
15708 /* Figure out in what scope the declaration is being placed. */
15709 prev_scope = current_scope ();
15711 type_start_token = cp_lexer_peek_token (parser->lexer);
15713 push_deferring_access_checks (dk_no_check);
15714 nested_name_specifier
15715 = cp_parser_nested_name_specifier_opt (parser,
15716 /*typename_keyword_p=*/true,
15717 /*check_dependency_p=*/false,
15718 /*type_p=*/false,
15719 /*is_declaration=*/false);
15721 if (nested_name_specifier)
15723 tree name;
15725 identifier = cp_parser_identifier (parser);
15726 name = cp_parser_lookup_name (parser, identifier,
15727 enum_type,
15728 /*is_template=*/false,
15729 /*is_namespace=*/false,
15730 /*check_dependency=*/true,
15731 /*ambiguous_decls=*/NULL,
15732 input_location);
15733 if (name && name != error_mark_node)
15735 type = TREE_TYPE (name);
15736 if (TREE_CODE (type) == TYPENAME_TYPE)
15738 /* Are template enums allowed in ISO? */
15739 if (template_parm_scope_p ())
15740 pedwarn (type_start_token->location, OPT_Wpedantic,
15741 "%qD is an enumeration template", name);
15742 /* ignore a typename reference, for it will be solved by name
15743 in start_enum. */
15744 type = NULL_TREE;
15747 else if (nested_name_specifier == error_mark_node)
15748 /* We already issued an error. */;
15749 else
15750 error_at (type_start_token->location,
15751 "%qD is not an enumerator-name", identifier);
15753 else
15755 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15756 identifier = cp_parser_identifier (parser);
15757 else
15759 identifier = make_anon_name ();
15760 is_anonymous = true;
15761 if (scoped_enum_p)
15762 error_at (type_start_token->location,
15763 "anonymous scoped enum is not allowed");
15766 pop_deferring_access_checks ();
15768 /* Check for the `:' that denotes a specified underlying type in C++0x.
15769 Note that a ':' could also indicate a bitfield width, however. */
15770 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15772 cp_decl_specifier_seq type_specifiers;
15774 /* Consume the `:'. */
15775 cp_lexer_consume_token (parser->lexer);
15777 /* Parse the type-specifier-seq. */
15778 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15779 /*is_trailing_return=*/false,
15780 &type_specifiers);
15782 /* At this point this is surely not elaborated type specifier. */
15783 if (!cp_parser_parse_definitely (parser))
15784 return NULL_TREE;
15786 if (cxx_dialect < cxx11)
15787 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15789 has_underlying_type = true;
15791 /* If that didn't work, stop. */
15792 if (type_specifiers.type != error_mark_node)
15794 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15795 /*initialized=*/0, NULL);
15796 if (underlying_type == error_mark_node
15797 || check_for_bare_parameter_packs (underlying_type))
15798 underlying_type = NULL_TREE;
15802 /* Look for the `{' but don't consume it yet. */
15803 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15805 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15807 cp_parser_error (parser, "expected %<{%>");
15808 if (has_underlying_type)
15810 type = NULL_TREE;
15811 goto out;
15814 /* An opaque-enum-specifier must have a ';' here. */
15815 if ((scoped_enum_p || underlying_type)
15816 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15818 cp_parser_error (parser, "expected %<;%> or %<{%>");
15819 if (has_underlying_type)
15821 type = NULL_TREE;
15822 goto out;
15827 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15828 return NULL_TREE;
15830 if (nested_name_specifier)
15832 if (CLASS_TYPE_P (nested_name_specifier))
15834 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15835 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15836 push_scope (nested_name_specifier);
15838 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15840 push_nested_namespace (nested_name_specifier);
15844 /* Issue an error message if type-definitions are forbidden here. */
15845 if (!cp_parser_check_type_definition (parser))
15846 type = error_mark_node;
15847 else
15848 /* Create the new type. We do this before consuming the opening
15849 brace so the enum will be recorded as being on the line of its
15850 tag (or the 'enum' keyword, if there is no tag). */
15851 type = start_enum (identifier, type, underlying_type,
15852 scoped_enum_p, &is_new_type);
15854 /* If the next token is not '{' it is an opaque-enum-specifier or an
15855 elaborated-type-specifier. */
15856 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15858 timevar_push (TV_PARSE_ENUM);
15859 if (nested_name_specifier
15860 && nested_name_specifier != error_mark_node)
15862 /* The following catches invalid code such as:
15863 enum class S<int>::E { A, B, C }; */
15864 if (!processing_specialization
15865 && CLASS_TYPE_P (nested_name_specifier)
15866 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15867 error_at (type_start_token->location, "cannot add an enumerator "
15868 "list to a template instantiation");
15870 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15872 error_at (type_start_token->location,
15873 "%<%T::%E%> has not been declared",
15874 TYPE_CONTEXT (nested_name_specifier),
15875 nested_name_specifier);
15876 type = error_mark_node;
15878 /* If that scope does not contain the scope in which the
15879 class was originally declared, the program is invalid. */
15880 else if (prev_scope && !is_ancestor (prev_scope,
15881 nested_name_specifier))
15883 if (at_namespace_scope_p ())
15884 error_at (type_start_token->location,
15885 "declaration of %qD in namespace %qD which does not "
15886 "enclose %qD",
15887 type, prev_scope, nested_name_specifier);
15888 else
15889 error_at (type_start_token->location,
15890 "declaration of %qD in %qD which does not "
15891 "enclose %qD",
15892 type, prev_scope, nested_name_specifier);
15893 type = error_mark_node;
15897 if (scoped_enum_p)
15898 begin_scope (sk_scoped_enum, type);
15900 /* Consume the opening brace. */
15901 cp_lexer_consume_token (parser->lexer);
15903 if (type == error_mark_node)
15904 ; /* Nothing to add */
15905 else if (OPAQUE_ENUM_P (type)
15906 || (cxx_dialect > cxx98 && processing_specialization))
15908 new_value_list = true;
15909 SET_OPAQUE_ENUM_P (type, false);
15910 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15912 else
15914 error_at (type_start_token->location,
15915 "multiple definition of %q#T", type);
15916 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15917 "previous definition here");
15918 type = error_mark_node;
15921 if (type == error_mark_node)
15922 cp_parser_skip_to_end_of_block_or_statement (parser);
15923 /* If the next token is not '}', then there are some enumerators. */
15924 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15926 if (is_anonymous && !scoped_enum_p)
15927 pedwarn (type_start_token->location, OPT_Wpedantic,
15928 "ISO C++ forbids empty anonymous enum");
15930 else
15931 cp_parser_enumerator_list (parser, type);
15933 /* Consume the final '}'. */
15934 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15936 if (scoped_enum_p)
15937 finish_scope ();
15938 timevar_pop (TV_PARSE_ENUM);
15940 else
15942 /* If a ';' follows, then it is an opaque-enum-specifier
15943 and additional restrictions apply. */
15944 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15946 if (is_anonymous)
15947 error_at (type_start_token->location,
15948 "opaque-enum-specifier without name");
15949 else if (nested_name_specifier)
15950 error_at (type_start_token->location,
15951 "opaque-enum-specifier must use a simple identifier");
15955 /* Look for trailing attributes to apply to this enumeration, and
15956 apply them if appropriate. */
15957 if (cp_parser_allow_gnu_extensions_p (parser))
15959 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15960 trailing_attr = chainon (trailing_attr, attributes);
15961 cplus_decl_attributes (&type,
15962 trailing_attr,
15963 (int) ATTR_FLAG_TYPE_IN_PLACE);
15966 /* Finish up the enumeration. */
15967 if (type != error_mark_node)
15969 if (new_value_list)
15970 finish_enum_value_list (type);
15971 if (is_new_type)
15972 finish_enum (type);
15975 if (nested_name_specifier)
15977 if (CLASS_TYPE_P (nested_name_specifier))
15979 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15980 pop_scope (nested_name_specifier);
15982 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15984 pop_nested_namespace (nested_name_specifier);
15987 out:
15988 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15989 return type;
15992 /* Parse an enumerator-list. The enumerators all have the indicated
15993 TYPE.
15995 enumerator-list:
15996 enumerator-definition
15997 enumerator-list , enumerator-definition */
15999 static void
16000 cp_parser_enumerator_list (cp_parser* parser, tree type)
16002 while (true)
16004 /* Parse an enumerator-definition. */
16005 cp_parser_enumerator_definition (parser, type);
16007 /* If the next token is not a ',', we've reached the end of
16008 the list. */
16009 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16010 break;
16011 /* Otherwise, consume the `,' and keep going. */
16012 cp_lexer_consume_token (parser->lexer);
16013 /* If the next token is a `}', there is a trailing comma. */
16014 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16016 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16017 pedwarn (input_location, OPT_Wpedantic,
16018 "comma at end of enumerator list");
16019 break;
16024 /* Parse an enumerator-definition. The enumerator has the indicated
16025 TYPE.
16027 enumerator-definition:
16028 enumerator
16029 enumerator = constant-expression
16031 enumerator:
16032 identifier */
16034 static void
16035 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16037 tree identifier;
16038 tree value;
16039 location_t loc;
16041 /* Save the input location because we are interested in the location
16042 of the identifier and not the location of the explicit value. */
16043 loc = cp_lexer_peek_token (parser->lexer)->location;
16045 /* Look for the identifier. */
16046 identifier = cp_parser_identifier (parser);
16047 if (identifier == error_mark_node)
16048 return;
16050 /* If the next token is an '=', then there is an explicit value. */
16051 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16053 /* Consume the `=' token. */
16054 cp_lexer_consume_token (parser->lexer);
16055 /* Parse the value. */
16056 value = cp_parser_constant_expression (parser);
16058 else
16059 value = NULL_TREE;
16061 /* If we are processing a template, make sure the initializer of the
16062 enumerator doesn't contain any bare template parameter pack. */
16063 if (check_for_bare_parameter_packs (value))
16064 value = error_mark_node;
16066 /* Create the enumerator. */
16067 build_enumerator (identifier, value, type, loc);
16070 /* Parse a namespace-name.
16072 namespace-name:
16073 original-namespace-name
16074 namespace-alias
16076 Returns the NAMESPACE_DECL for the namespace. */
16078 static tree
16079 cp_parser_namespace_name (cp_parser* parser)
16081 tree identifier;
16082 tree namespace_decl;
16084 cp_token *token = cp_lexer_peek_token (parser->lexer);
16086 /* Get the name of the namespace. */
16087 identifier = cp_parser_identifier (parser);
16088 if (identifier == error_mark_node)
16089 return error_mark_node;
16091 /* Look up the identifier in the currently active scope. Look only
16092 for namespaces, due to:
16094 [basic.lookup.udir]
16096 When looking up a namespace-name in a using-directive or alias
16097 definition, only namespace names are considered.
16099 And:
16101 [basic.lookup.qual]
16103 During the lookup of a name preceding the :: scope resolution
16104 operator, object, function, and enumerator names are ignored.
16106 (Note that cp_parser_qualifying_entity only calls this
16107 function if the token after the name is the scope resolution
16108 operator.) */
16109 namespace_decl = cp_parser_lookup_name (parser, identifier,
16110 none_type,
16111 /*is_template=*/false,
16112 /*is_namespace=*/true,
16113 /*check_dependency=*/true,
16114 /*ambiguous_decls=*/NULL,
16115 token->location);
16116 /* If it's not a namespace, issue an error. */
16117 if (namespace_decl == error_mark_node
16118 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16120 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16121 error_at (token->location, "%qD is not a namespace-name", identifier);
16122 cp_parser_error (parser, "expected namespace-name");
16123 namespace_decl = error_mark_node;
16126 return namespace_decl;
16129 /* Parse a namespace-definition.
16131 namespace-definition:
16132 named-namespace-definition
16133 unnamed-namespace-definition
16135 named-namespace-definition:
16136 original-namespace-definition
16137 extension-namespace-definition
16139 original-namespace-definition:
16140 namespace identifier { namespace-body }
16142 extension-namespace-definition:
16143 namespace original-namespace-name { namespace-body }
16145 unnamed-namespace-definition:
16146 namespace { namespace-body } */
16148 static void
16149 cp_parser_namespace_definition (cp_parser* parser)
16151 tree identifier, attribs;
16152 bool has_visibility;
16153 bool is_inline;
16155 cp_ensure_no_omp_declare_simd (parser);
16156 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16158 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16159 is_inline = true;
16160 cp_lexer_consume_token (parser->lexer);
16162 else
16163 is_inline = false;
16165 /* Look for the `namespace' keyword. */
16166 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16168 /* Get the name of the namespace. We do not attempt to distinguish
16169 between an original-namespace-definition and an
16170 extension-namespace-definition at this point. The semantic
16171 analysis routines are responsible for that. */
16172 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16173 identifier = cp_parser_identifier (parser);
16174 else
16175 identifier = NULL_TREE;
16177 /* Parse any specified attributes. */
16178 attribs = cp_parser_attributes_opt (parser);
16180 /* Look for the `{' to start the namespace. */
16181 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16182 /* Start the namespace. */
16183 push_namespace (identifier);
16185 /* "inline namespace" is equivalent to a stub namespace definition
16186 followed by a strong using directive. */
16187 if (is_inline)
16189 tree name_space = current_namespace;
16190 /* Set up namespace association. */
16191 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16192 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16193 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16194 /* Import the contents of the inline namespace. */
16195 pop_namespace ();
16196 do_using_directive (name_space);
16197 push_namespace (identifier);
16200 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16202 /* Parse the body of the namespace. */
16203 cp_parser_namespace_body (parser);
16205 if (has_visibility)
16206 pop_visibility (1);
16208 /* Finish the namespace. */
16209 pop_namespace ();
16210 /* Look for the final `}'. */
16211 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16214 /* Parse a namespace-body.
16216 namespace-body:
16217 declaration-seq [opt] */
16219 static void
16220 cp_parser_namespace_body (cp_parser* parser)
16222 cp_parser_declaration_seq_opt (parser);
16225 /* Parse a namespace-alias-definition.
16227 namespace-alias-definition:
16228 namespace identifier = qualified-namespace-specifier ; */
16230 static void
16231 cp_parser_namespace_alias_definition (cp_parser* parser)
16233 tree identifier;
16234 tree namespace_specifier;
16236 cp_token *token = cp_lexer_peek_token (parser->lexer);
16238 /* Look for the `namespace' keyword. */
16239 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16240 /* Look for the identifier. */
16241 identifier = cp_parser_identifier (parser);
16242 if (identifier == error_mark_node)
16243 return;
16244 /* Look for the `=' token. */
16245 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16246 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16248 error_at (token->location, "%<namespace%> definition is not allowed here");
16249 /* Skip the definition. */
16250 cp_lexer_consume_token (parser->lexer);
16251 if (cp_parser_skip_to_closing_brace (parser))
16252 cp_lexer_consume_token (parser->lexer);
16253 return;
16255 cp_parser_require (parser, CPP_EQ, RT_EQ);
16256 /* Look for the qualified-namespace-specifier. */
16257 namespace_specifier
16258 = cp_parser_qualified_namespace_specifier (parser);
16259 /* Look for the `;' token. */
16260 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16262 /* Register the alias in the symbol table. */
16263 do_namespace_alias (identifier, namespace_specifier);
16266 /* Parse a qualified-namespace-specifier.
16268 qualified-namespace-specifier:
16269 :: [opt] nested-name-specifier [opt] namespace-name
16271 Returns a NAMESPACE_DECL corresponding to the specified
16272 namespace. */
16274 static tree
16275 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16277 /* Look for the optional `::'. */
16278 cp_parser_global_scope_opt (parser,
16279 /*current_scope_valid_p=*/false);
16281 /* Look for the optional nested-name-specifier. */
16282 cp_parser_nested_name_specifier_opt (parser,
16283 /*typename_keyword_p=*/false,
16284 /*check_dependency_p=*/true,
16285 /*type_p=*/false,
16286 /*is_declaration=*/true);
16288 return cp_parser_namespace_name (parser);
16291 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16292 access declaration.
16294 using-declaration:
16295 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16296 using :: unqualified-id ;
16298 access-declaration:
16299 qualified-id ;
16303 static bool
16304 cp_parser_using_declaration (cp_parser* parser,
16305 bool access_declaration_p)
16307 cp_token *token;
16308 bool typename_p = false;
16309 bool global_scope_p;
16310 tree decl;
16311 tree identifier;
16312 tree qscope;
16313 int oldcount = errorcount;
16314 cp_token *diag_token = NULL;
16316 if (access_declaration_p)
16318 diag_token = cp_lexer_peek_token (parser->lexer);
16319 cp_parser_parse_tentatively (parser);
16321 else
16323 /* Look for the `using' keyword. */
16324 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16326 /* Peek at the next token. */
16327 token = cp_lexer_peek_token (parser->lexer);
16328 /* See if it's `typename'. */
16329 if (token->keyword == RID_TYPENAME)
16331 /* Remember that we've seen it. */
16332 typename_p = true;
16333 /* Consume the `typename' token. */
16334 cp_lexer_consume_token (parser->lexer);
16338 /* Look for the optional global scope qualification. */
16339 global_scope_p
16340 = (cp_parser_global_scope_opt (parser,
16341 /*current_scope_valid_p=*/false)
16342 != NULL_TREE);
16344 /* If we saw `typename', or didn't see `::', then there must be a
16345 nested-name-specifier present. */
16346 if (typename_p || !global_scope_p)
16348 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16349 /*check_dependency_p=*/true,
16350 /*type_p=*/false,
16351 /*is_declaration=*/true);
16352 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16354 cp_parser_skip_to_end_of_block_or_statement (parser);
16355 return false;
16358 /* Otherwise, we could be in either of the two productions. In that
16359 case, treat the nested-name-specifier as optional. */
16360 else
16361 qscope = cp_parser_nested_name_specifier_opt (parser,
16362 /*typename_keyword_p=*/false,
16363 /*check_dependency_p=*/true,
16364 /*type_p=*/false,
16365 /*is_declaration=*/true);
16366 if (!qscope)
16367 qscope = global_namespace;
16368 else if (UNSCOPED_ENUM_P (qscope))
16369 qscope = CP_TYPE_CONTEXT (qscope);
16371 if (access_declaration_p && cp_parser_error_occurred (parser))
16372 /* Something has already gone wrong; there's no need to parse
16373 further. Since an error has occurred, the return value of
16374 cp_parser_parse_definitely will be false, as required. */
16375 return cp_parser_parse_definitely (parser);
16377 token = cp_lexer_peek_token (parser->lexer);
16378 /* Parse the unqualified-id. */
16379 identifier = cp_parser_unqualified_id (parser,
16380 /*template_keyword_p=*/false,
16381 /*check_dependency_p=*/true,
16382 /*declarator_p=*/true,
16383 /*optional_p=*/false);
16385 if (access_declaration_p)
16387 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16388 cp_parser_simulate_error (parser);
16389 if (!cp_parser_parse_definitely (parser))
16390 return false;
16393 /* The function we call to handle a using-declaration is different
16394 depending on what scope we are in. */
16395 if (qscope == error_mark_node || identifier == error_mark_node)
16397 else if (!identifier_p (identifier)
16398 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16399 /* [namespace.udecl]
16401 A using declaration shall not name a template-id. */
16402 error_at (token->location,
16403 "a template-id may not appear in a using-declaration");
16404 else
16406 if (at_class_scope_p ())
16408 /* Create the USING_DECL. */
16409 decl = do_class_using_decl (parser->scope, identifier);
16411 if (decl && typename_p)
16412 USING_DECL_TYPENAME_P (decl) = 1;
16414 if (check_for_bare_parameter_packs (decl))
16416 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16417 return false;
16419 else
16420 /* Add it to the list of members in this class. */
16421 finish_member_declaration (decl);
16423 else
16425 decl = cp_parser_lookup_name_simple (parser,
16426 identifier,
16427 token->location);
16428 if (decl == error_mark_node)
16429 cp_parser_name_lookup_error (parser, identifier,
16430 decl, NLE_NULL,
16431 token->location);
16432 else if (check_for_bare_parameter_packs (decl))
16434 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16435 return false;
16437 else if (!at_namespace_scope_p ())
16438 do_local_using_decl (decl, qscope, identifier);
16439 else
16440 do_toplevel_using_decl (decl, qscope, identifier);
16444 /* Look for the final `;'. */
16445 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16447 if (access_declaration_p && errorcount == oldcount)
16448 warning_at (diag_token->location, OPT_Wdeprecated,
16449 "access declarations are deprecated "
16450 "in favour of using-declarations; "
16451 "suggestion: add the %<using%> keyword");
16453 return true;
16456 /* Parse an alias-declaration.
16458 alias-declaration:
16459 using identifier attribute-specifier-seq [opt] = type-id */
16461 static tree
16462 cp_parser_alias_declaration (cp_parser* parser)
16464 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16465 location_t id_location;
16466 cp_declarator *declarator;
16467 cp_decl_specifier_seq decl_specs;
16468 bool member_p;
16469 const char *saved_message = NULL;
16471 /* Look for the `using' keyword. */
16472 cp_token *using_token
16473 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16474 if (using_token == NULL)
16475 return error_mark_node;
16477 id_location = cp_lexer_peek_token (parser->lexer)->location;
16478 id = cp_parser_identifier (parser);
16479 if (id == error_mark_node)
16480 return error_mark_node;
16482 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16483 attributes = cp_parser_attributes_opt (parser);
16484 if (attributes == error_mark_node)
16485 return error_mark_node;
16487 cp_parser_require (parser, CPP_EQ, RT_EQ);
16489 if (cp_parser_error_occurred (parser))
16490 return error_mark_node;
16492 cp_parser_commit_to_tentative_parse (parser);
16494 /* Now we are going to parse the type-id of the declaration. */
16497 [dcl.type]/3 says:
16499 "A type-specifier-seq shall not define a class or enumeration
16500 unless it appears in the type-id of an alias-declaration (7.1.3) that
16501 is not the declaration of a template-declaration."
16503 In other words, if we currently are in an alias template, the
16504 type-id should not define a type.
16506 So let's set parser->type_definition_forbidden_message in that
16507 case; cp_parser_check_type_definition (called by
16508 cp_parser_class_specifier) will then emit an error if a type is
16509 defined in the type-id. */
16510 if (parser->num_template_parameter_lists)
16512 saved_message = parser->type_definition_forbidden_message;
16513 parser->type_definition_forbidden_message =
16514 G_("types may not be defined in alias template declarations");
16517 type = cp_parser_type_id (parser);
16519 /* Restore the error message if need be. */
16520 if (parser->num_template_parameter_lists)
16521 parser->type_definition_forbidden_message = saved_message;
16523 if (type == error_mark_node
16524 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16526 cp_parser_skip_to_end_of_block_or_statement (parser);
16527 return error_mark_node;
16530 /* A typedef-name can also be introduced by an alias-declaration. The
16531 identifier following the using keyword becomes a typedef-name. It has
16532 the same semantics as if it were introduced by the typedef
16533 specifier. In particular, it does not define a new type and it shall
16534 not appear in the type-id. */
16536 clear_decl_specs (&decl_specs);
16537 decl_specs.type = type;
16538 if (attributes != NULL_TREE)
16540 decl_specs.attributes = attributes;
16541 set_and_check_decl_spec_loc (&decl_specs,
16542 ds_attribute,
16543 attrs_token);
16545 set_and_check_decl_spec_loc (&decl_specs,
16546 ds_typedef,
16547 using_token);
16548 set_and_check_decl_spec_loc (&decl_specs,
16549 ds_alias,
16550 using_token);
16552 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16553 declarator->id_loc = id_location;
16555 member_p = at_class_scope_p ();
16556 if (member_p)
16557 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16558 NULL_TREE, attributes);
16559 else
16560 decl = start_decl (declarator, &decl_specs, 0,
16561 attributes, NULL_TREE, &pushed_scope);
16562 if (decl == error_mark_node)
16563 return decl;
16565 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16567 if (pushed_scope)
16568 pop_scope (pushed_scope);
16570 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16571 added into the symbol table; otherwise, return the TYPE_DECL. */
16572 if (DECL_LANG_SPECIFIC (decl)
16573 && DECL_TEMPLATE_INFO (decl)
16574 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16576 decl = DECL_TI_TEMPLATE (decl);
16577 if (member_p)
16578 check_member_template (decl);
16581 return decl;
16584 /* Parse a using-directive.
16586 using-directive:
16587 using namespace :: [opt] nested-name-specifier [opt]
16588 namespace-name ; */
16590 static void
16591 cp_parser_using_directive (cp_parser* parser)
16593 tree namespace_decl;
16594 tree attribs;
16596 /* Look for the `using' keyword. */
16597 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16598 /* And the `namespace' keyword. */
16599 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16600 /* Look for the optional `::' operator. */
16601 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16602 /* And the optional nested-name-specifier. */
16603 cp_parser_nested_name_specifier_opt (parser,
16604 /*typename_keyword_p=*/false,
16605 /*check_dependency_p=*/true,
16606 /*type_p=*/false,
16607 /*is_declaration=*/true);
16608 /* Get the namespace being used. */
16609 namespace_decl = cp_parser_namespace_name (parser);
16610 /* And any specified attributes. */
16611 attribs = cp_parser_attributes_opt (parser);
16612 /* Update the symbol table. */
16613 parse_using_directive (namespace_decl, attribs);
16614 /* Look for the final `;'. */
16615 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16618 /* Parse an asm-definition.
16620 asm-definition:
16621 asm ( string-literal ) ;
16623 GNU Extension:
16625 asm-definition:
16626 asm volatile [opt] ( string-literal ) ;
16627 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16628 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16629 : asm-operand-list [opt] ) ;
16630 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16631 : asm-operand-list [opt]
16632 : asm-clobber-list [opt] ) ;
16633 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16634 : asm-clobber-list [opt]
16635 : asm-goto-list ) ; */
16637 static void
16638 cp_parser_asm_definition (cp_parser* parser)
16640 tree string;
16641 tree outputs = NULL_TREE;
16642 tree inputs = NULL_TREE;
16643 tree clobbers = NULL_TREE;
16644 tree labels = NULL_TREE;
16645 tree asm_stmt;
16646 bool volatile_p = false;
16647 bool extended_p = false;
16648 bool invalid_inputs_p = false;
16649 bool invalid_outputs_p = false;
16650 bool goto_p = false;
16651 required_token missing = RT_NONE;
16653 /* Look for the `asm' keyword. */
16654 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16656 if (parser->in_function_body
16657 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16659 error ("%<asm%> in %<constexpr%> function");
16660 cp_function_chain->invalid_constexpr = true;
16663 /* See if the next token is `volatile'. */
16664 if (cp_parser_allow_gnu_extensions_p (parser)
16665 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16667 /* Remember that we saw the `volatile' keyword. */
16668 volatile_p = true;
16669 /* Consume the token. */
16670 cp_lexer_consume_token (parser->lexer);
16672 if (cp_parser_allow_gnu_extensions_p (parser)
16673 && parser->in_function_body
16674 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16676 /* Remember that we saw the `goto' keyword. */
16677 goto_p = true;
16678 /* Consume the token. */
16679 cp_lexer_consume_token (parser->lexer);
16681 /* Look for the opening `('. */
16682 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16683 return;
16684 /* Look for the string. */
16685 string = cp_parser_string_literal (parser, false, false);
16686 if (string == error_mark_node)
16688 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16689 /*consume_paren=*/true);
16690 return;
16693 /* If we're allowing GNU extensions, check for the extended assembly
16694 syntax. Unfortunately, the `:' tokens need not be separated by
16695 a space in C, and so, for compatibility, we tolerate that here
16696 too. Doing that means that we have to treat the `::' operator as
16697 two `:' tokens. */
16698 if (cp_parser_allow_gnu_extensions_p (parser)
16699 && parser->in_function_body
16700 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16701 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16703 bool inputs_p = false;
16704 bool clobbers_p = false;
16705 bool labels_p = false;
16707 /* The extended syntax was used. */
16708 extended_p = true;
16710 /* Look for outputs. */
16711 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16713 /* Consume the `:'. */
16714 cp_lexer_consume_token (parser->lexer);
16715 /* Parse the output-operands. */
16716 if (cp_lexer_next_token_is_not (parser->lexer,
16717 CPP_COLON)
16718 && cp_lexer_next_token_is_not (parser->lexer,
16719 CPP_SCOPE)
16720 && cp_lexer_next_token_is_not (parser->lexer,
16721 CPP_CLOSE_PAREN)
16722 && !goto_p)
16723 outputs = cp_parser_asm_operand_list (parser);
16725 if (outputs == error_mark_node)
16726 invalid_outputs_p = true;
16728 /* If the next token is `::', there are no outputs, and the
16729 next token is the beginning of the inputs. */
16730 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16731 /* The inputs are coming next. */
16732 inputs_p = true;
16734 /* Look for inputs. */
16735 if (inputs_p
16736 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16738 /* Consume the `:' or `::'. */
16739 cp_lexer_consume_token (parser->lexer);
16740 /* Parse the output-operands. */
16741 if (cp_lexer_next_token_is_not (parser->lexer,
16742 CPP_COLON)
16743 && cp_lexer_next_token_is_not (parser->lexer,
16744 CPP_SCOPE)
16745 && cp_lexer_next_token_is_not (parser->lexer,
16746 CPP_CLOSE_PAREN))
16747 inputs = cp_parser_asm_operand_list (parser);
16749 if (inputs == error_mark_node)
16750 invalid_inputs_p = true;
16752 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16753 /* The clobbers are coming next. */
16754 clobbers_p = true;
16756 /* Look for clobbers. */
16757 if (clobbers_p
16758 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16760 clobbers_p = true;
16761 /* Consume the `:' or `::'. */
16762 cp_lexer_consume_token (parser->lexer);
16763 /* Parse the clobbers. */
16764 if (cp_lexer_next_token_is_not (parser->lexer,
16765 CPP_COLON)
16766 && cp_lexer_next_token_is_not (parser->lexer,
16767 CPP_CLOSE_PAREN))
16768 clobbers = cp_parser_asm_clobber_list (parser);
16770 else if (goto_p
16771 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16772 /* The labels are coming next. */
16773 labels_p = true;
16775 /* Look for labels. */
16776 if (labels_p
16777 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16779 labels_p = true;
16780 /* Consume the `:' or `::'. */
16781 cp_lexer_consume_token (parser->lexer);
16782 /* Parse the labels. */
16783 labels = cp_parser_asm_label_list (parser);
16786 if (goto_p && !labels_p)
16787 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16789 else if (goto_p)
16790 missing = RT_COLON_SCOPE;
16792 /* Look for the closing `)'. */
16793 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16794 missing ? missing : RT_CLOSE_PAREN))
16795 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16796 /*consume_paren=*/true);
16797 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16799 if (!invalid_inputs_p && !invalid_outputs_p)
16801 /* Create the ASM_EXPR. */
16802 if (parser->in_function_body)
16804 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16805 inputs, clobbers, labels);
16806 /* If the extended syntax was not used, mark the ASM_EXPR. */
16807 if (!extended_p)
16809 tree temp = asm_stmt;
16810 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16811 temp = TREE_OPERAND (temp, 0);
16813 ASM_INPUT_P (temp) = 1;
16816 else
16817 symtab->finalize_toplevel_asm (string);
16821 /* Declarators [gram.dcl.decl] */
16823 /* Parse an init-declarator.
16825 init-declarator:
16826 declarator initializer [opt]
16828 GNU Extension:
16830 init-declarator:
16831 declarator asm-specification [opt] attributes [opt] initializer [opt]
16833 function-definition:
16834 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16835 function-body
16836 decl-specifier-seq [opt] declarator function-try-block
16838 GNU Extension:
16840 function-definition:
16841 __extension__ function-definition
16843 TM Extension:
16845 function-definition:
16846 decl-specifier-seq [opt] declarator function-transaction-block
16848 The DECL_SPECIFIERS apply to this declarator. Returns a
16849 representation of the entity declared. If MEMBER_P is TRUE, then
16850 this declarator appears in a class scope. The new DECL created by
16851 this declarator is returned.
16853 The CHECKS are access checks that should be performed once we know
16854 what entity is being declared (and, therefore, what classes have
16855 befriended it).
16857 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16858 for a function-definition here as well. If the declarator is a
16859 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16860 be TRUE upon return. By that point, the function-definition will
16861 have been completely parsed.
16863 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16864 is FALSE.
16866 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16867 parsed declaration if it is an uninitialized single declarator not followed
16868 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16869 if present, will not be consumed. If returned, this declarator will be
16870 created with SD_INITIALIZED but will not call cp_finish_decl.
16872 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16873 and there is an initializer, the pointed location_t is set to the
16874 location of the '=' or `(', or '{' in C++11 token introducing the
16875 initializer. */
16877 static tree
16878 cp_parser_init_declarator (cp_parser* parser,
16879 cp_decl_specifier_seq *decl_specifiers,
16880 vec<deferred_access_check, va_gc> *checks,
16881 bool function_definition_allowed_p,
16882 bool member_p,
16883 int declares_class_or_enum,
16884 bool* function_definition_p,
16885 tree* maybe_range_for_decl,
16886 location_t* init_loc)
16888 cp_token *token = NULL, *asm_spec_start_token = NULL,
16889 *attributes_start_token = NULL;
16890 cp_declarator *declarator;
16891 tree prefix_attributes;
16892 tree attributes = NULL;
16893 tree asm_specification;
16894 tree initializer;
16895 tree decl = NULL_TREE;
16896 tree scope;
16897 int is_initialized;
16898 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16899 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16900 "(...)". */
16901 enum cpp_ttype initialization_kind;
16902 bool is_direct_init = false;
16903 bool is_non_constant_init;
16904 int ctor_dtor_or_conv_p;
16905 bool friend_p = cp_parser_friend_p (decl_specifiers);
16906 tree pushed_scope = NULL_TREE;
16907 bool range_for_decl_p = false;
16908 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16909 location_t tmp_init_loc = UNKNOWN_LOCATION;
16911 /* Gather the attributes that were provided with the
16912 decl-specifiers. */
16913 prefix_attributes = decl_specifiers->attributes;
16915 /* Assume that this is not the declarator for a function
16916 definition. */
16917 if (function_definition_p)
16918 *function_definition_p = false;
16920 /* Default arguments are only permitted for function parameters. */
16921 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16922 parser->default_arg_ok_p = false;
16924 /* Defer access checks while parsing the declarator; we cannot know
16925 what names are accessible until we know what is being
16926 declared. */
16927 resume_deferring_access_checks ();
16929 /* Parse the declarator. */
16930 token = cp_lexer_peek_token (parser->lexer);
16931 declarator
16932 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16933 &ctor_dtor_or_conv_p,
16934 /*parenthesized_p=*/NULL,
16935 member_p, friend_p);
16936 /* Gather up the deferred checks. */
16937 stop_deferring_access_checks ();
16939 parser->default_arg_ok_p = saved_default_arg_ok_p;
16941 /* If the DECLARATOR was erroneous, there's no need to go
16942 further. */
16943 if (declarator == cp_error_declarator)
16944 return error_mark_node;
16946 /* Check that the number of template-parameter-lists is OK. */
16947 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16948 token->location))
16949 return error_mark_node;
16951 if (declares_class_or_enum & 2)
16952 cp_parser_check_for_definition_in_return_type (declarator,
16953 decl_specifiers->type,
16954 decl_specifiers->locations[ds_type_spec]);
16956 /* Figure out what scope the entity declared by the DECLARATOR is
16957 located in. `grokdeclarator' sometimes changes the scope, so
16958 we compute it now. */
16959 scope = get_scope_of_declarator (declarator);
16961 /* Perform any lookups in the declared type which were thought to be
16962 dependent, but are not in the scope of the declarator. */
16963 decl_specifiers->type
16964 = maybe_update_decl_type (decl_specifiers->type, scope);
16966 /* If we're allowing GNU extensions, look for an
16967 asm-specification. */
16968 if (cp_parser_allow_gnu_extensions_p (parser))
16970 /* Look for an asm-specification. */
16971 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16972 asm_specification = cp_parser_asm_specification_opt (parser);
16974 else
16975 asm_specification = NULL_TREE;
16977 /* Look for attributes. */
16978 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16979 attributes = cp_parser_attributes_opt (parser);
16981 /* Peek at the next token. */
16982 token = cp_lexer_peek_token (parser->lexer);
16984 bool bogus_implicit_tmpl = false;
16986 if (function_declarator_p (declarator))
16988 /* Check to see if the token indicates the start of a
16989 function-definition. */
16990 if (cp_parser_token_starts_function_definition_p (token))
16992 if (!function_definition_allowed_p)
16994 /* If a function-definition should not appear here, issue an
16995 error message. */
16996 cp_parser_error (parser,
16997 "a function-definition is not allowed here");
16998 return error_mark_node;
17001 location_t func_brace_location
17002 = cp_lexer_peek_token (parser->lexer)->location;
17004 /* Neither attributes nor an asm-specification are allowed
17005 on a function-definition. */
17006 if (asm_specification)
17007 error_at (asm_spec_start_token->location,
17008 "an asm-specification is not allowed "
17009 "on a function-definition");
17010 if (attributes)
17011 error_at (attributes_start_token->location,
17012 "attributes are not allowed "
17013 "on a function-definition");
17014 /* This is a function-definition. */
17015 *function_definition_p = true;
17017 /* Parse the function definition. */
17018 if (member_p)
17019 decl = cp_parser_save_member_function_body (parser,
17020 decl_specifiers,
17021 declarator,
17022 prefix_attributes);
17023 else
17024 decl =
17025 (cp_parser_function_definition_from_specifiers_and_declarator
17026 (parser, decl_specifiers, prefix_attributes, declarator));
17028 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17030 /* This is where the prologue starts... */
17031 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17032 = func_brace_location;
17035 return decl;
17038 else if (parser->fully_implicit_function_template_p)
17040 /* A non-template declaration involving a function parameter list
17041 containing an implicit template parameter will be made into a
17042 template. If the resulting declaration is not going to be an
17043 actual function then finish the template scope here to prevent it.
17044 An error message will be issued once we have a decl to talk about.
17046 FIXME probably we should do type deduction rather than create an
17047 implicit template, but the standard currently doesn't allow it. */
17048 bogus_implicit_tmpl = true;
17049 finish_fully_implicit_template (parser, NULL_TREE);
17052 /* [dcl.dcl]
17054 Only in function declarations for constructors, destructors, and
17055 type conversions can the decl-specifier-seq be omitted.
17057 We explicitly postpone this check past the point where we handle
17058 function-definitions because we tolerate function-definitions
17059 that are missing their return types in some modes. */
17060 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17062 cp_parser_error (parser,
17063 "expected constructor, destructor, or type conversion");
17064 return error_mark_node;
17067 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17068 if (token->type == CPP_EQ
17069 || token->type == CPP_OPEN_PAREN
17070 || token->type == CPP_OPEN_BRACE)
17072 is_initialized = SD_INITIALIZED;
17073 initialization_kind = token->type;
17074 if (maybe_range_for_decl)
17075 *maybe_range_for_decl = error_mark_node;
17076 tmp_init_loc = token->location;
17077 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17078 *init_loc = tmp_init_loc;
17080 if (token->type == CPP_EQ
17081 && function_declarator_p (declarator))
17083 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17084 if (t2->keyword == RID_DEFAULT)
17085 is_initialized = SD_DEFAULTED;
17086 else if (t2->keyword == RID_DELETE)
17087 is_initialized = SD_DELETED;
17090 else
17092 /* If the init-declarator isn't initialized and isn't followed by a
17093 `,' or `;', it's not a valid init-declarator. */
17094 if (token->type != CPP_COMMA
17095 && token->type != CPP_SEMICOLON)
17097 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17098 range_for_decl_p = true;
17099 else
17101 if (!maybe_range_for_decl)
17102 cp_parser_error (parser, "expected initializer");
17103 return error_mark_node;
17106 is_initialized = SD_UNINITIALIZED;
17107 initialization_kind = CPP_EOF;
17110 /* Because start_decl has side-effects, we should only call it if we
17111 know we're going ahead. By this point, we know that we cannot
17112 possibly be looking at any other construct. */
17113 cp_parser_commit_to_tentative_parse (parser);
17115 /* Enter the newly declared entry in the symbol table. If we're
17116 processing a declaration in a class-specifier, we wait until
17117 after processing the initializer. */
17118 if (!member_p)
17120 if (parser->in_unbraced_linkage_specification_p)
17121 decl_specifiers->storage_class = sc_extern;
17122 decl = start_decl (declarator, decl_specifiers,
17123 range_for_decl_p? SD_INITIALIZED : is_initialized,
17124 attributes, prefix_attributes, &pushed_scope);
17125 cp_finalize_omp_declare_simd (parser, decl);
17126 /* Adjust location of decl if declarator->id_loc is more appropriate:
17127 set, and decl wasn't merged with another decl, in which case its
17128 location would be different from input_location, and more accurate. */
17129 if (DECL_P (decl)
17130 && declarator->id_loc != UNKNOWN_LOCATION
17131 && DECL_SOURCE_LOCATION (decl) == input_location)
17132 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17134 else if (scope)
17135 /* Enter the SCOPE. That way unqualified names appearing in the
17136 initializer will be looked up in SCOPE. */
17137 pushed_scope = push_scope (scope);
17139 /* Perform deferred access control checks, now that we know in which
17140 SCOPE the declared entity resides. */
17141 if (!member_p && decl)
17143 tree saved_current_function_decl = NULL_TREE;
17145 /* If the entity being declared is a function, pretend that we
17146 are in its scope. If it is a `friend', it may have access to
17147 things that would not otherwise be accessible. */
17148 if (TREE_CODE (decl) == FUNCTION_DECL)
17150 saved_current_function_decl = current_function_decl;
17151 current_function_decl = decl;
17154 /* Perform access checks for template parameters. */
17155 cp_parser_perform_template_parameter_access_checks (checks);
17157 /* Perform the access control checks for the declarator and the
17158 decl-specifiers. */
17159 perform_deferred_access_checks (tf_warning_or_error);
17161 /* Restore the saved value. */
17162 if (TREE_CODE (decl) == FUNCTION_DECL)
17163 current_function_decl = saved_current_function_decl;
17166 /* Parse the initializer. */
17167 initializer = NULL_TREE;
17168 is_direct_init = false;
17169 is_non_constant_init = true;
17170 if (is_initialized)
17172 if (function_declarator_p (declarator))
17174 if (initialization_kind == CPP_EQ)
17175 initializer = cp_parser_pure_specifier (parser);
17176 else
17178 /* If the declaration was erroneous, we don't really
17179 know what the user intended, so just silently
17180 consume the initializer. */
17181 if (decl != error_mark_node)
17182 error_at (tmp_init_loc, "initializer provided for function");
17183 cp_parser_skip_to_closing_parenthesis (parser,
17184 /*recovering=*/true,
17185 /*or_comma=*/false,
17186 /*consume_paren=*/true);
17189 else
17191 /* We want to record the extra mangling scope for in-class
17192 initializers of class members and initializers of static data
17193 member templates. The former involves deferring
17194 parsing of the initializer until end of class as with default
17195 arguments. So right here we only handle the latter. */
17196 if (!member_p && processing_template_decl)
17197 start_lambda_scope (decl);
17198 initializer = cp_parser_initializer (parser,
17199 &is_direct_init,
17200 &is_non_constant_init);
17201 if (!member_p && processing_template_decl)
17202 finish_lambda_scope ();
17203 if (initializer == error_mark_node)
17204 cp_parser_skip_to_end_of_statement (parser);
17208 /* The old parser allows attributes to appear after a parenthesized
17209 initializer. Mark Mitchell proposed removing this functionality
17210 on the GCC mailing lists on 2002-08-13. This parser accepts the
17211 attributes -- but ignores them. */
17212 if (cp_parser_allow_gnu_extensions_p (parser)
17213 && initialization_kind == CPP_OPEN_PAREN)
17214 if (cp_parser_attributes_opt (parser))
17215 warning (OPT_Wattributes,
17216 "attributes after parenthesized initializer ignored");
17218 /* And now complain about a non-function implicit template. */
17219 if (bogus_implicit_tmpl)
17220 error_at (DECL_SOURCE_LOCATION (decl),
17221 "non-function %qD declared as implicit template", decl);
17223 /* For an in-class declaration, use `grokfield' to create the
17224 declaration. */
17225 if (member_p)
17227 if (pushed_scope)
17229 pop_scope (pushed_scope);
17230 pushed_scope = NULL_TREE;
17232 decl = grokfield (declarator, decl_specifiers,
17233 initializer, !is_non_constant_init,
17234 /*asmspec=*/NULL_TREE,
17235 chainon (attributes, prefix_attributes));
17236 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17237 cp_parser_save_default_args (parser, decl);
17238 cp_finalize_omp_declare_simd (parser, decl);
17241 /* Finish processing the declaration. But, skip member
17242 declarations. */
17243 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17245 cp_finish_decl (decl,
17246 initializer, !is_non_constant_init,
17247 asm_specification,
17248 /* If the initializer is in parentheses, then this is
17249 a direct-initialization, which means that an
17250 `explicit' constructor is OK. Otherwise, an
17251 `explicit' constructor cannot be used. */
17252 ((is_direct_init || !is_initialized)
17253 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17255 else if ((cxx_dialect != cxx98) && friend_p
17256 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17257 /* Core issue #226 (C++0x only): A default template-argument
17258 shall not be specified in a friend class template
17259 declaration. */
17260 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17261 /*is_partial=*/false, /*is_friend_decl=*/1);
17263 if (!friend_p && pushed_scope)
17264 pop_scope (pushed_scope);
17266 if (function_declarator_p (declarator)
17267 && parser->fully_implicit_function_template_p)
17269 if (member_p)
17270 decl = finish_fully_implicit_template (parser, decl);
17271 else
17272 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17275 return decl;
17278 /* Parse a declarator.
17280 declarator:
17281 direct-declarator
17282 ptr-operator declarator
17284 abstract-declarator:
17285 ptr-operator abstract-declarator [opt]
17286 direct-abstract-declarator
17288 GNU Extensions:
17290 declarator:
17291 attributes [opt] direct-declarator
17292 attributes [opt] ptr-operator declarator
17294 abstract-declarator:
17295 attributes [opt] ptr-operator abstract-declarator [opt]
17296 attributes [opt] direct-abstract-declarator
17298 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17299 detect constructor, destructor or conversion operators. It is set
17300 to -1 if the declarator is a name, and +1 if it is a
17301 function. Otherwise it is set to zero. Usually you just want to
17302 test for >0, but internally the negative value is used.
17304 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17305 a decl-specifier-seq unless it declares a constructor, destructor,
17306 or conversion. It might seem that we could check this condition in
17307 semantic analysis, rather than parsing, but that makes it difficult
17308 to handle something like `f()'. We want to notice that there are
17309 no decl-specifiers, and therefore realize that this is an
17310 expression, not a declaration.)
17312 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17313 the declarator is a direct-declarator of the form "(...)".
17315 MEMBER_P is true iff this declarator is a member-declarator.
17317 FRIEND_P is true iff this declarator is a friend. */
17319 static cp_declarator *
17320 cp_parser_declarator (cp_parser* parser,
17321 cp_parser_declarator_kind dcl_kind,
17322 int* ctor_dtor_or_conv_p,
17323 bool* parenthesized_p,
17324 bool member_p, bool friend_p)
17326 cp_declarator *declarator;
17327 enum tree_code code;
17328 cp_cv_quals cv_quals;
17329 tree class_type;
17330 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17332 /* Assume this is not a constructor, destructor, or type-conversion
17333 operator. */
17334 if (ctor_dtor_or_conv_p)
17335 *ctor_dtor_or_conv_p = 0;
17337 if (cp_parser_allow_gnu_extensions_p (parser))
17338 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17340 /* Check for the ptr-operator production. */
17341 cp_parser_parse_tentatively (parser);
17342 /* Parse the ptr-operator. */
17343 code = cp_parser_ptr_operator (parser,
17344 &class_type,
17345 &cv_quals,
17346 &std_attributes);
17348 /* If that worked, then we have a ptr-operator. */
17349 if (cp_parser_parse_definitely (parser))
17351 /* If a ptr-operator was found, then this declarator was not
17352 parenthesized. */
17353 if (parenthesized_p)
17354 *parenthesized_p = true;
17355 /* The dependent declarator is optional if we are parsing an
17356 abstract-declarator. */
17357 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17358 cp_parser_parse_tentatively (parser);
17360 /* Parse the dependent declarator. */
17361 declarator = cp_parser_declarator (parser, dcl_kind,
17362 /*ctor_dtor_or_conv_p=*/NULL,
17363 /*parenthesized_p=*/NULL,
17364 /*member_p=*/false,
17365 friend_p);
17367 /* If we are parsing an abstract-declarator, we must handle the
17368 case where the dependent declarator is absent. */
17369 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17370 && !cp_parser_parse_definitely (parser))
17371 declarator = NULL;
17373 declarator = cp_parser_make_indirect_declarator
17374 (code, class_type, cv_quals, declarator, std_attributes);
17376 /* Everything else is a direct-declarator. */
17377 else
17379 if (parenthesized_p)
17380 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17381 CPP_OPEN_PAREN);
17382 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17383 ctor_dtor_or_conv_p,
17384 member_p, friend_p);
17387 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17388 declarator->attributes = gnu_attributes;
17389 return declarator;
17392 /* Parse a direct-declarator or direct-abstract-declarator.
17394 direct-declarator:
17395 declarator-id
17396 direct-declarator ( parameter-declaration-clause )
17397 cv-qualifier-seq [opt]
17398 ref-qualifier [opt]
17399 exception-specification [opt]
17400 direct-declarator [ constant-expression [opt] ]
17401 ( declarator )
17403 direct-abstract-declarator:
17404 direct-abstract-declarator [opt]
17405 ( parameter-declaration-clause )
17406 cv-qualifier-seq [opt]
17407 ref-qualifier [opt]
17408 exception-specification [opt]
17409 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17410 ( abstract-declarator )
17412 Returns a representation of the declarator. DCL_KIND is
17413 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17414 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17415 we are parsing a direct-declarator. It is
17416 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17417 of ambiguity we prefer an abstract declarator, as per
17418 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17419 as for cp_parser_declarator. */
17421 static cp_declarator *
17422 cp_parser_direct_declarator (cp_parser* parser,
17423 cp_parser_declarator_kind dcl_kind,
17424 int* ctor_dtor_or_conv_p,
17425 bool member_p, bool friend_p)
17427 cp_token *token;
17428 cp_declarator *declarator = NULL;
17429 tree scope = NULL_TREE;
17430 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17431 bool saved_in_declarator_p = parser->in_declarator_p;
17432 bool first = true;
17433 tree pushed_scope = NULL_TREE;
17435 while (true)
17437 /* Peek at the next token. */
17438 token = cp_lexer_peek_token (parser->lexer);
17439 if (token->type == CPP_OPEN_PAREN)
17441 /* This is either a parameter-declaration-clause, or a
17442 parenthesized declarator. When we know we are parsing a
17443 named declarator, it must be a parenthesized declarator
17444 if FIRST is true. For instance, `(int)' is a
17445 parameter-declaration-clause, with an omitted
17446 direct-abstract-declarator. But `((*))', is a
17447 parenthesized abstract declarator. Finally, when T is a
17448 template parameter `(T)' is a
17449 parameter-declaration-clause, and not a parenthesized
17450 named declarator.
17452 We first try and parse a parameter-declaration-clause,
17453 and then try a nested declarator (if FIRST is true).
17455 It is not an error for it not to be a
17456 parameter-declaration-clause, even when FIRST is
17457 false. Consider,
17459 int i (int);
17460 int i (3);
17462 The first is the declaration of a function while the
17463 second is the definition of a variable, including its
17464 initializer.
17466 Having seen only the parenthesis, we cannot know which of
17467 these two alternatives should be selected. Even more
17468 complex are examples like:
17470 int i (int (a));
17471 int i (int (3));
17473 The former is a function-declaration; the latter is a
17474 variable initialization.
17476 Thus again, we try a parameter-declaration-clause, and if
17477 that fails, we back out and return. */
17479 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17481 tree params;
17482 bool is_declarator = false;
17484 /* In a member-declarator, the only valid interpretation
17485 of a parenthesis is the start of a
17486 parameter-declaration-clause. (It is invalid to
17487 initialize a static data member with a parenthesized
17488 initializer; only the "=" form of initialization is
17489 permitted.) */
17490 if (!member_p)
17491 cp_parser_parse_tentatively (parser);
17493 /* Consume the `('. */
17494 cp_lexer_consume_token (parser->lexer);
17495 if (first)
17497 /* If this is going to be an abstract declarator, we're
17498 in a declarator and we can't have default args. */
17499 parser->default_arg_ok_p = false;
17500 parser->in_declarator_p = true;
17503 begin_scope (sk_function_parms, NULL_TREE);
17505 /* Parse the parameter-declaration-clause. */
17506 params = cp_parser_parameter_declaration_clause (parser);
17508 /* Consume the `)'. */
17509 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17511 /* If all went well, parse the cv-qualifier-seq,
17512 ref-qualifier and the exception-specification. */
17513 if (member_p || cp_parser_parse_definitely (parser))
17515 cp_cv_quals cv_quals;
17516 cp_virt_specifiers virt_specifiers;
17517 cp_ref_qualifier ref_qual;
17518 tree exception_specification;
17519 tree late_return;
17520 tree attrs;
17521 bool memfn = (member_p || (pushed_scope
17522 && CLASS_TYPE_P (pushed_scope)));
17524 is_declarator = true;
17526 if (ctor_dtor_or_conv_p)
17527 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17528 first = false;
17530 /* Parse the cv-qualifier-seq. */
17531 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17532 /* Parse the ref-qualifier. */
17533 ref_qual = cp_parser_ref_qualifier_opt (parser);
17534 /* And the exception-specification. */
17535 exception_specification
17536 = cp_parser_exception_specification_opt (parser);
17538 attrs = cp_parser_std_attribute_spec_seq (parser);
17540 /* In here, we handle cases where attribute is used after
17541 the function declaration. For example:
17542 void func (int x) __attribute__((vector(..))); */
17543 if (flag_cilkplus
17544 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17546 cp_parser_parse_tentatively (parser);
17547 tree attr = cp_parser_gnu_attributes_opt (parser);
17548 if (cp_lexer_next_token_is_not (parser->lexer,
17549 CPP_SEMICOLON)
17550 && cp_lexer_next_token_is_not (parser->lexer,
17551 CPP_OPEN_BRACE))
17552 cp_parser_abort_tentative_parse (parser);
17553 else if (!cp_parser_parse_definitely (parser))
17555 else
17556 attrs = chainon (attr, attrs);
17558 late_return = (cp_parser_late_return_type_opt
17559 (parser, declarator,
17560 memfn ? cv_quals : -1));
17563 /* Parse the virt-specifier-seq. */
17564 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17566 /* Create the function-declarator. */
17567 declarator = make_call_declarator (declarator,
17568 params,
17569 cv_quals,
17570 virt_specifiers,
17571 ref_qual,
17572 exception_specification,
17573 late_return);
17574 declarator->std_attributes = attrs;
17575 /* Any subsequent parameter lists are to do with
17576 return type, so are not those of the declared
17577 function. */
17578 parser->default_arg_ok_p = false;
17581 /* Remove the function parms from scope. */
17582 pop_bindings_and_leave_scope ();
17584 if (is_declarator)
17585 /* Repeat the main loop. */
17586 continue;
17589 /* If this is the first, we can try a parenthesized
17590 declarator. */
17591 if (first)
17593 bool saved_in_type_id_in_expr_p;
17595 parser->default_arg_ok_p = saved_default_arg_ok_p;
17596 parser->in_declarator_p = saved_in_declarator_p;
17598 /* Consume the `('. */
17599 cp_lexer_consume_token (parser->lexer);
17600 /* Parse the nested declarator. */
17601 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17602 parser->in_type_id_in_expr_p = true;
17603 declarator
17604 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17605 /*parenthesized_p=*/NULL,
17606 member_p, friend_p);
17607 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17608 first = false;
17609 /* Expect a `)'. */
17610 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17611 declarator = cp_error_declarator;
17612 if (declarator == cp_error_declarator)
17613 break;
17615 goto handle_declarator;
17617 /* Otherwise, we must be done. */
17618 else
17619 break;
17621 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17622 && token->type == CPP_OPEN_SQUARE
17623 && !cp_next_tokens_can_be_attribute_p (parser))
17625 /* Parse an array-declarator. */
17626 tree bounds, attrs;
17628 if (ctor_dtor_or_conv_p)
17629 *ctor_dtor_or_conv_p = 0;
17631 first = false;
17632 parser->default_arg_ok_p = false;
17633 parser->in_declarator_p = true;
17634 /* Consume the `['. */
17635 cp_lexer_consume_token (parser->lexer);
17636 /* Peek at the next token. */
17637 token = cp_lexer_peek_token (parser->lexer);
17638 /* If the next token is `]', then there is no
17639 constant-expression. */
17640 if (token->type != CPP_CLOSE_SQUARE)
17642 bool non_constant_p;
17643 bounds
17644 = cp_parser_constant_expression (parser,
17645 /*allow_non_constant=*/true,
17646 &non_constant_p);
17647 if (!non_constant_p)
17648 /* OK */;
17649 else if (error_operand_p (bounds))
17650 /* Already gave an error. */;
17651 else if (!parser->in_function_body
17652 || current_binding_level->kind == sk_function_parms)
17654 /* Normally, the array bound must be an integral constant
17655 expression. However, as an extension, we allow VLAs
17656 in function scopes as long as they aren't part of a
17657 parameter declaration. */
17658 cp_parser_error (parser,
17659 "array bound is not an integer constant");
17660 bounds = error_mark_node;
17662 else if (processing_template_decl
17663 && !type_dependent_expression_p (bounds))
17665 /* Remember this wasn't a constant-expression. */
17666 bounds = build_nop (TREE_TYPE (bounds), bounds);
17667 TREE_SIDE_EFFECTS (bounds) = 1;
17670 else
17671 bounds = NULL_TREE;
17672 /* Look for the closing `]'. */
17673 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17675 declarator = cp_error_declarator;
17676 break;
17679 attrs = cp_parser_std_attribute_spec_seq (parser);
17680 declarator = make_array_declarator (declarator, bounds);
17681 declarator->std_attributes = attrs;
17683 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17686 tree qualifying_scope;
17687 tree unqualified_name;
17688 tree attrs;
17689 special_function_kind sfk;
17690 bool abstract_ok;
17691 bool pack_expansion_p = false;
17692 cp_token *declarator_id_start_token;
17694 /* Parse a declarator-id */
17695 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17696 if (abstract_ok)
17698 cp_parser_parse_tentatively (parser);
17700 /* If we see an ellipsis, we should be looking at a
17701 parameter pack. */
17702 if (token->type == CPP_ELLIPSIS)
17704 /* Consume the `...' */
17705 cp_lexer_consume_token (parser->lexer);
17707 pack_expansion_p = true;
17711 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17712 unqualified_name
17713 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17714 qualifying_scope = parser->scope;
17715 if (abstract_ok)
17717 bool okay = false;
17719 if (!unqualified_name && pack_expansion_p)
17721 /* Check whether an error occurred. */
17722 okay = !cp_parser_error_occurred (parser);
17724 /* We already consumed the ellipsis to mark a
17725 parameter pack, but we have no way to report it,
17726 so abort the tentative parse. We will be exiting
17727 immediately anyway. */
17728 cp_parser_abort_tentative_parse (parser);
17730 else
17731 okay = cp_parser_parse_definitely (parser);
17733 if (!okay)
17734 unqualified_name = error_mark_node;
17735 else if (unqualified_name
17736 && (qualifying_scope
17737 || (!identifier_p (unqualified_name))))
17739 cp_parser_error (parser, "expected unqualified-id");
17740 unqualified_name = error_mark_node;
17744 if (!unqualified_name)
17745 return NULL;
17746 if (unqualified_name == error_mark_node)
17748 declarator = cp_error_declarator;
17749 pack_expansion_p = false;
17750 declarator->parameter_pack_p = false;
17751 break;
17754 attrs = cp_parser_std_attribute_spec_seq (parser);
17756 if (qualifying_scope && at_namespace_scope_p ()
17757 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17759 /* In the declaration of a member of a template class
17760 outside of the class itself, the SCOPE will sometimes
17761 be a TYPENAME_TYPE. For example, given:
17763 template <typename T>
17764 int S<T>::R::i = 3;
17766 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17767 this context, we must resolve S<T>::R to an ordinary
17768 type, rather than a typename type.
17770 The reason we normally avoid resolving TYPENAME_TYPEs
17771 is that a specialization of `S' might render
17772 `S<T>::R' not a type. However, if `S' is
17773 specialized, then this `i' will not be used, so there
17774 is no harm in resolving the types here. */
17775 tree type;
17777 /* Resolve the TYPENAME_TYPE. */
17778 type = resolve_typename_type (qualifying_scope,
17779 /*only_current_p=*/false);
17780 /* If that failed, the declarator is invalid. */
17781 if (TREE_CODE (type) == TYPENAME_TYPE)
17783 if (typedef_variant_p (type))
17784 error_at (declarator_id_start_token->location,
17785 "cannot define member of dependent typedef "
17786 "%qT", type);
17787 else
17788 error_at (declarator_id_start_token->location,
17789 "%<%T::%E%> is not a type",
17790 TYPE_CONTEXT (qualifying_scope),
17791 TYPE_IDENTIFIER (qualifying_scope));
17793 qualifying_scope = type;
17796 sfk = sfk_none;
17798 if (unqualified_name)
17800 tree class_type;
17802 if (qualifying_scope
17803 && CLASS_TYPE_P (qualifying_scope))
17804 class_type = qualifying_scope;
17805 else
17806 class_type = current_class_type;
17808 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17810 tree name_type = TREE_TYPE (unqualified_name);
17811 if (class_type && same_type_p (name_type, class_type))
17813 if (qualifying_scope
17814 && CLASSTYPE_USE_TEMPLATE (name_type))
17816 error_at (declarator_id_start_token->location,
17817 "invalid use of constructor as a template");
17818 inform (declarator_id_start_token->location,
17819 "use %<%T::%D%> instead of %<%T::%D%> to "
17820 "name the constructor in a qualified name",
17821 class_type,
17822 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17823 class_type, name_type);
17824 declarator = cp_error_declarator;
17825 break;
17827 else
17828 unqualified_name = constructor_name (class_type);
17830 else
17832 /* We do not attempt to print the declarator
17833 here because we do not have enough
17834 information about its original syntactic
17835 form. */
17836 cp_parser_error (parser, "invalid declarator");
17837 declarator = cp_error_declarator;
17838 break;
17842 if (class_type)
17844 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17845 sfk = sfk_destructor;
17846 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17847 sfk = sfk_conversion;
17848 else if (/* There's no way to declare a constructor
17849 for an anonymous type, even if the type
17850 got a name for linkage purposes. */
17851 !TYPE_WAS_ANONYMOUS (class_type)
17852 /* Handle correctly (c++/19200):
17854 struct S {
17855 struct T{};
17856 friend void S(T);
17859 and also:
17861 namespace N {
17862 void S();
17865 struct S {
17866 friend void N::S();
17867 }; */
17868 && !(friend_p
17869 && class_type != qualifying_scope)
17870 && constructor_name_p (unqualified_name,
17871 class_type))
17873 unqualified_name = constructor_name (class_type);
17874 sfk = sfk_constructor;
17876 else if (is_overloaded_fn (unqualified_name)
17877 && DECL_CONSTRUCTOR_P (get_first_fn
17878 (unqualified_name)))
17879 sfk = sfk_constructor;
17881 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17882 *ctor_dtor_or_conv_p = -1;
17885 declarator = make_id_declarator (qualifying_scope,
17886 unqualified_name,
17887 sfk);
17888 declarator->std_attributes = attrs;
17889 declarator->id_loc = token->location;
17890 declarator->parameter_pack_p = pack_expansion_p;
17892 if (pack_expansion_p)
17893 maybe_warn_variadic_templates ();
17896 handle_declarator:;
17897 scope = get_scope_of_declarator (declarator);
17898 if (scope)
17900 /* Any names that appear after the declarator-id for a
17901 member are looked up in the containing scope. */
17902 if (at_function_scope_p ())
17904 /* But declarations with qualified-ids can't appear in a
17905 function. */
17906 cp_parser_error (parser, "qualified-id in declaration");
17907 declarator = cp_error_declarator;
17908 break;
17910 pushed_scope = push_scope (scope);
17912 parser->in_declarator_p = true;
17913 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17914 || (declarator && declarator->kind == cdk_id))
17915 /* Default args are only allowed on function
17916 declarations. */
17917 parser->default_arg_ok_p = saved_default_arg_ok_p;
17918 else
17919 parser->default_arg_ok_p = false;
17921 first = false;
17923 /* We're done. */
17924 else
17925 break;
17928 /* For an abstract declarator, we might wind up with nothing at this
17929 point. That's an error; the declarator is not optional. */
17930 if (!declarator)
17931 cp_parser_error (parser, "expected declarator");
17933 /* If we entered a scope, we must exit it now. */
17934 if (pushed_scope)
17935 pop_scope (pushed_scope);
17937 parser->default_arg_ok_p = saved_default_arg_ok_p;
17938 parser->in_declarator_p = saved_in_declarator_p;
17940 return declarator;
17943 /* Parse a ptr-operator.
17945 ptr-operator:
17946 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17947 * cv-qualifier-seq [opt]
17949 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17950 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17952 GNU Extension:
17954 ptr-operator:
17955 & cv-qualifier-seq [opt]
17957 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17958 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17959 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17960 filled in with the TYPE containing the member. *CV_QUALS is
17961 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17962 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17963 Note that the tree codes returned by this function have nothing
17964 to do with the types of trees that will be eventually be created
17965 to represent the pointer or reference type being parsed. They are
17966 just constants with suggestive names. */
17967 static enum tree_code
17968 cp_parser_ptr_operator (cp_parser* parser,
17969 tree* type,
17970 cp_cv_quals *cv_quals,
17971 tree *attributes)
17973 enum tree_code code = ERROR_MARK;
17974 cp_token *token;
17975 tree attrs = NULL_TREE;
17977 /* Assume that it's not a pointer-to-member. */
17978 *type = NULL_TREE;
17979 /* And that there are no cv-qualifiers. */
17980 *cv_quals = TYPE_UNQUALIFIED;
17982 /* Peek at the next token. */
17983 token = cp_lexer_peek_token (parser->lexer);
17985 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17986 if (token->type == CPP_MULT)
17987 code = INDIRECT_REF;
17988 else if (token->type == CPP_AND)
17989 code = ADDR_EXPR;
17990 else if ((cxx_dialect != cxx98) &&
17991 token->type == CPP_AND_AND) /* C++0x only */
17992 code = NON_LVALUE_EXPR;
17994 if (code != ERROR_MARK)
17996 /* Consume the `*', `&' or `&&'. */
17997 cp_lexer_consume_token (parser->lexer);
17999 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18000 `&', if we are allowing GNU extensions. (The only qualifier
18001 that can legally appear after `&' is `restrict', but that is
18002 enforced during semantic analysis. */
18003 if (code == INDIRECT_REF
18004 || cp_parser_allow_gnu_extensions_p (parser))
18005 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18007 attrs = cp_parser_std_attribute_spec_seq (parser);
18008 if (attributes != NULL)
18009 *attributes = attrs;
18011 else
18013 /* Try the pointer-to-member case. */
18014 cp_parser_parse_tentatively (parser);
18015 /* Look for the optional `::' operator. */
18016 cp_parser_global_scope_opt (parser,
18017 /*current_scope_valid_p=*/false);
18018 /* Look for the nested-name specifier. */
18019 token = cp_lexer_peek_token (parser->lexer);
18020 cp_parser_nested_name_specifier (parser,
18021 /*typename_keyword_p=*/false,
18022 /*check_dependency_p=*/true,
18023 /*type_p=*/false,
18024 /*is_declaration=*/false);
18025 /* If we found it, and the next token is a `*', then we are
18026 indeed looking at a pointer-to-member operator. */
18027 if (!cp_parser_error_occurred (parser)
18028 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18030 /* Indicate that the `*' operator was used. */
18031 code = INDIRECT_REF;
18033 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18034 error_at (token->location, "%qD is a namespace", parser->scope);
18035 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18036 error_at (token->location, "cannot form pointer to member of "
18037 "non-class %q#T", parser->scope);
18038 else
18040 /* The type of which the member is a member is given by the
18041 current SCOPE. */
18042 *type = parser->scope;
18043 /* The next name will not be qualified. */
18044 parser->scope = NULL_TREE;
18045 parser->qualifying_scope = NULL_TREE;
18046 parser->object_scope = NULL_TREE;
18047 /* Look for optional c++11 attributes. */
18048 attrs = cp_parser_std_attribute_spec_seq (parser);
18049 if (attributes != NULL)
18050 *attributes = attrs;
18051 /* Look for the optional cv-qualifier-seq. */
18052 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18055 /* If that didn't work we don't have a ptr-operator. */
18056 if (!cp_parser_parse_definitely (parser))
18057 cp_parser_error (parser, "expected ptr-operator");
18060 return code;
18063 /* Parse an (optional) cv-qualifier-seq.
18065 cv-qualifier-seq:
18066 cv-qualifier cv-qualifier-seq [opt]
18068 cv-qualifier:
18069 const
18070 volatile
18072 GNU Extension:
18074 cv-qualifier:
18075 __restrict__
18077 Returns a bitmask representing the cv-qualifiers. */
18079 static cp_cv_quals
18080 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18082 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18084 while (true)
18086 cp_token *token;
18087 cp_cv_quals cv_qualifier;
18089 /* Peek at the next token. */
18090 token = cp_lexer_peek_token (parser->lexer);
18091 /* See if it's a cv-qualifier. */
18092 switch (token->keyword)
18094 case RID_CONST:
18095 cv_qualifier = TYPE_QUAL_CONST;
18096 break;
18098 case RID_VOLATILE:
18099 cv_qualifier = TYPE_QUAL_VOLATILE;
18100 break;
18102 case RID_RESTRICT:
18103 cv_qualifier = TYPE_QUAL_RESTRICT;
18104 break;
18106 default:
18107 cv_qualifier = TYPE_UNQUALIFIED;
18108 break;
18111 if (!cv_qualifier)
18112 break;
18114 if (cv_quals & cv_qualifier)
18116 error_at (token->location, "duplicate cv-qualifier");
18117 cp_lexer_purge_token (parser->lexer);
18119 else
18121 cp_lexer_consume_token (parser->lexer);
18122 cv_quals |= cv_qualifier;
18126 return cv_quals;
18129 /* Parse an (optional) ref-qualifier
18131 ref-qualifier:
18135 Returns cp_ref_qualifier representing ref-qualifier. */
18137 static cp_ref_qualifier
18138 cp_parser_ref_qualifier_opt (cp_parser* parser)
18140 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18142 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18143 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18144 return ref_qual;
18146 while (true)
18148 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18149 cp_token *token = cp_lexer_peek_token (parser->lexer);
18151 switch (token->type)
18153 case CPP_AND:
18154 curr_ref_qual = REF_QUAL_LVALUE;
18155 break;
18157 case CPP_AND_AND:
18158 curr_ref_qual = REF_QUAL_RVALUE;
18159 break;
18161 default:
18162 curr_ref_qual = REF_QUAL_NONE;
18163 break;
18166 if (!curr_ref_qual)
18167 break;
18168 else if (ref_qual)
18170 error_at (token->location, "multiple ref-qualifiers");
18171 cp_lexer_purge_token (parser->lexer);
18173 else
18175 ref_qual = curr_ref_qual;
18176 cp_lexer_consume_token (parser->lexer);
18180 return ref_qual;
18183 /* Parse an (optional) virt-specifier-seq.
18185 virt-specifier-seq:
18186 virt-specifier virt-specifier-seq [opt]
18188 virt-specifier:
18189 override
18190 final
18192 Returns a bitmask representing the virt-specifiers. */
18194 static cp_virt_specifiers
18195 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18197 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18199 while (true)
18201 cp_token *token;
18202 cp_virt_specifiers virt_specifier;
18204 /* Peek at the next token. */
18205 token = cp_lexer_peek_token (parser->lexer);
18206 /* See if it's a virt-specifier-qualifier. */
18207 if (token->type != CPP_NAME)
18208 break;
18209 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18211 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18212 virt_specifier = VIRT_SPEC_OVERRIDE;
18214 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18216 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18217 virt_specifier = VIRT_SPEC_FINAL;
18219 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18221 virt_specifier = VIRT_SPEC_FINAL;
18223 else
18224 break;
18226 if (virt_specifiers & virt_specifier)
18228 error_at (token->location, "duplicate virt-specifier");
18229 cp_lexer_purge_token (parser->lexer);
18231 else
18233 cp_lexer_consume_token (parser->lexer);
18234 virt_specifiers |= virt_specifier;
18237 return virt_specifiers;
18240 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18241 is in scope even though it isn't real. */
18243 void
18244 inject_this_parameter (tree ctype, cp_cv_quals quals)
18246 tree this_parm;
18248 if (current_class_ptr)
18250 /* We don't clear this between NSDMIs. Is it already what we want? */
18251 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18252 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18253 && cp_type_quals (type) == quals)
18254 return;
18257 this_parm = build_this_parm (ctype, quals);
18258 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18259 current_class_ptr = NULL_TREE;
18260 current_class_ref
18261 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18262 current_class_ptr = this_parm;
18265 /* Return true iff our current scope is a non-static data member
18266 initializer. */
18268 bool
18269 parsing_nsdmi (void)
18271 /* We recognize NSDMI context by the context-less 'this' pointer set up
18272 by the function above. */
18273 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18274 return true;
18275 return false;
18278 /* Parse a late-specified return type, if any. This is not a separate
18279 non-terminal, but part of a function declarator, which looks like
18281 -> trailing-type-specifier-seq abstract-declarator(opt)
18283 Returns the type indicated by the type-id.
18285 In addition to this this parses any queued up omp declare simd
18286 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18288 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18289 function. */
18291 static tree
18292 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18293 cp_cv_quals quals)
18295 cp_token *token;
18296 tree type = NULL_TREE;
18297 bool declare_simd_p = (parser->omp_declare_simd
18298 && declarator
18299 && declarator->kind == cdk_id);
18301 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18302 && declarator && declarator->kind == cdk_id);
18304 /* Peek at the next token. */
18305 token = cp_lexer_peek_token (parser->lexer);
18306 /* A late-specified return type is indicated by an initial '->'. */
18307 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18308 return NULL_TREE;
18310 tree save_ccp = current_class_ptr;
18311 tree save_ccr = current_class_ref;
18312 if (quals >= 0)
18314 /* DR 1207: 'this' is in scope in the trailing return type. */
18315 inject_this_parameter (current_class_type, quals);
18318 if (token->type == CPP_DEREF)
18320 /* Consume the ->. */
18321 cp_lexer_consume_token (parser->lexer);
18323 type = cp_parser_trailing_type_id (parser);
18326 if (cilk_simd_fn_vector_p)
18327 declarator->std_attributes
18328 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18329 declarator->std_attributes);
18330 if (declare_simd_p)
18331 declarator->std_attributes
18332 = cp_parser_late_parsing_omp_declare_simd (parser,
18333 declarator->std_attributes);
18335 if (quals >= 0)
18337 current_class_ptr = save_ccp;
18338 current_class_ref = save_ccr;
18341 return type;
18344 /* Parse a declarator-id.
18346 declarator-id:
18347 id-expression
18348 :: [opt] nested-name-specifier [opt] type-name
18350 In the `id-expression' case, the value returned is as for
18351 cp_parser_id_expression if the id-expression was an unqualified-id.
18352 If the id-expression was a qualified-id, then a SCOPE_REF is
18353 returned. The first operand is the scope (either a NAMESPACE_DECL
18354 or TREE_TYPE), but the second is still just a representation of an
18355 unqualified-id. */
18357 static tree
18358 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18360 tree id;
18361 /* The expression must be an id-expression. Assume that qualified
18362 names are the names of types so that:
18364 template <class T>
18365 int S<T>::R::i = 3;
18367 will work; we must treat `S<T>::R' as the name of a type.
18368 Similarly, assume that qualified names are templates, where
18369 required, so that:
18371 template <class T>
18372 int S<T>::R<T>::i = 3;
18374 will work, too. */
18375 id = cp_parser_id_expression (parser,
18376 /*template_keyword_p=*/false,
18377 /*check_dependency_p=*/false,
18378 /*template_p=*/NULL,
18379 /*declarator_p=*/true,
18380 optional_p);
18381 if (id && BASELINK_P (id))
18382 id = BASELINK_FUNCTIONS (id);
18383 return id;
18386 /* Parse a type-id.
18388 type-id:
18389 type-specifier-seq abstract-declarator [opt]
18391 Returns the TYPE specified. */
18393 static tree
18394 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18395 bool is_trailing_return)
18397 cp_decl_specifier_seq type_specifier_seq;
18398 cp_declarator *abstract_declarator;
18400 /* Parse the type-specifier-seq. */
18401 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18402 is_trailing_return,
18403 &type_specifier_seq);
18404 if (type_specifier_seq.type == error_mark_node)
18405 return error_mark_node;
18407 /* There might or might not be an abstract declarator. */
18408 cp_parser_parse_tentatively (parser);
18409 /* Look for the declarator. */
18410 abstract_declarator
18411 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18412 /*parenthesized_p=*/NULL,
18413 /*member_p=*/false,
18414 /*friend_p=*/false);
18415 /* Check to see if there really was a declarator. */
18416 if (!cp_parser_parse_definitely (parser))
18417 abstract_declarator = NULL;
18419 if (type_specifier_seq.type
18420 /* None of the valid uses of 'auto' in C++14 involve the type-id
18421 nonterminal, but it is valid in a trailing-return-type. */
18422 && !(cxx_dialect >= cxx14 && is_trailing_return)
18423 && type_uses_auto (type_specifier_seq.type))
18425 /* A type-id with type 'auto' is only ok if the abstract declarator
18426 is a function declarator with a late-specified return type. */
18427 if (abstract_declarator
18428 && abstract_declarator->kind == cdk_function
18429 && abstract_declarator->u.function.late_return_type)
18430 /* OK */;
18431 else
18433 error ("invalid use of %<auto%>");
18434 return error_mark_node;
18438 return groktypename (&type_specifier_seq, abstract_declarator,
18439 is_template_arg);
18442 static tree cp_parser_type_id (cp_parser *parser)
18444 return cp_parser_type_id_1 (parser, false, false);
18447 static tree cp_parser_template_type_arg (cp_parser *parser)
18449 tree r;
18450 const char *saved_message = parser->type_definition_forbidden_message;
18451 parser->type_definition_forbidden_message
18452 = G_("types may not be defined in template arguments");
18453 r = cp_parser_type_id_1 (parser, true, false);
18454 parser->type_definition_forbidden_message = saved_message;
18455 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18457 error ("invalid use of %<auto%> in template argument");
18458 r = error_mark_node;
18460 return r;
18463 static tree cp_parser_trailing_type_id (cp_parser *parser)
18465 return cp_parser_type_id_1 (parser, false, true);
18468 /* Parse a type-specifier-seq.
18470 type-specifier-seq:
18471 type-specifier type-specifier-seq [opt]
18473 GNU extension:
18475 type-specifier-seq:
18476 attributes type-specifier-seq [opt]
18478 If IS_DECLARATION is true, we are at the start of a "condition" or
18479 exception-declaration, so we might be followed by a declarator-id.
18481 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18482 i.e. we've just seen "->".
18484 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18486 static void
18487 cp_parser_type_specifier_seq (cp_parser* parser,
18488 bool is_declaration,
18489 bool is_trailing_return,
18490 cp_decl_specifier_seq *type_specifier_seq)
18492 bool seen_type_specifier = false;
18493 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18494 cp_token *start_token = NULL;
18496 /* Clear the TYPE_SPECIFIER_SEQ. */
18497 clear_decl_specs (type_specifier_seq);
18499 /* In the context of a trailing return type, enum E { } is an
18500 elaborated-type-specifier followed by a function-body, not an
18501 enum-specifier. */
18502 if (is_trailing_return)
18503 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18505 /* Parse the type-specifiers and attributes. */
18506 while (true)
18508 tree type_specifier;
18509 bool is_cv_qualifier;
18511 /* Check for attributes first. */
18512 if (cp_next_tokens_can_be_attribute_p (parser))
18514 type_specifier_seq->attributes =
18515 chainon (type_specifier_seq->attributes,
18516 cp_parser_attributes_opt (parser));
18517 continue;
18520 /* record the token of the beginning of the type specifier seq,
18521 for error reporting purposes*/
18522 if (!start_token)
18523 start_token = cp_lexer_peek_token (parser->lexer);
18525 /* Look for the type-specifier. */
18526 type_specifier = cp_parser_type_specifier (parser,
18527 flags,
18528 type_specifier_seq,
18529 /*is_declaration=*/false,
18530 NULL,
18531 &is_cv_qualifier);
18532 if (!type_specifier)
18534 /* If the first type-specifier could not be found, this is not a
18535 type-specifier-seq at all. */
18536 if (!seen_type_specifier)
18538 /* Set in_declarator_p to avoid skipping to the semicolon. */
18539 int in_decl = parser->in_declarator_p;
18540 parser->in_declarator_p = true;
18542 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18543 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18544 cp_parser_error (parser, "expected type-specifier");
18546 parser->in_declarator_p = in_decl;
18548 type_specifier_seq->type = error_mark_node;
18549 return;
18551 /* If subsequent type-specifiers could not be found, the
18552 type-specifier-seq is complete. */
18553 break;
18556 seen_type_specifier = true;
18557 /* The standard says that a condition can be:
18559 type-specifier-seq declarator = assignment-expression
18561 However, given:
18563 struct S {};
18564 if (int S = ...)
18566 we should treat the "S" as a declarator, not as a
18567 type-specifier. The standard doesn't say that explicitly for
18568 type-specifier-seq, but it does say that for
18569 decl-specifier-seq in an ordinary declaration. Perhaps it
18570 would be clearer just to allow a decl-specifier-seq here, and
18571 then add a semantic restriction that if any decl-specifiers
18572 that are not type-specifiers appear, the program is invalid. */
18573 if (is_declaration && !is_cv_qualifier)
18574 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18578 /* Return whether the function currently being declared has an associated
18579 template parameter list. */
18581 static bool
18582 function_being_declared_is_template_p (cp_parser* parser)
18584 if (!current_template_parms || processing_template_parmlist)
18585 return false;
18587 if (parser->implicit_template_scope)
18588 return true;
18590 if (at_class_scope_p ()
18591 && TYPE_BEING_DEFINED (current_class_type))
18592 return parser->num_template_parameter_lists != 0;
18594 return ((int) parser->num_template_parameter_lists > template_class_depth
18595 (current_class_type));
18598 /* Parse a parameter-declaration-clause.
18600 parameter-declaration-clause:
18601 parameter-declaration-list [opt] ... [opt]
18602 parameter-declaration-list , ...
18604 Returns a representation for the parameter declarations. A return
18605 value of NULL indicates a parameter-declaration-clause consisting
18606 only of an ellipsis. */
18608 static tree
18609 cp_parser_parameter_declaration_clause (cp_parser* parser)
18611 tree parameters;
18612 cp_token *token;
18613 bool ellipsis_p;
18614 bool is_error;
18616 struct cleanup {
18617 cp_parser* parser;
18618 int auto_is_implicit_function_template_parm_p;
18619 ~cleanup() {
18620 parser->auto_is_implicit_function_template_parm_p
18621 = auto_is_implicit_function_template_parm_p;
18623 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18625 (void) cleanup;
18627 if (!processing_specialization
18628 && !processing_template_parmlist
18629 && !processing_explicit_instantiation)
18630 if (!current_function_decl
18631 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18632 parser->auto_is_implicit_function_template_parm_p = true;
18634 /* Peek at the next token. */
18635 token = cp_lexer_peek_token (parser->lexer);
18636 /* Check for trivial parameter-declaration-clauses. */
18637 if (token->type == CPP_ELLIPSIS)
18639 /* Consume the `...' token. */
18640 cp_lexer_consume_token (parser->lexer);
18641 return NULL_TREE;
18643 else if (token->type == CPP_CLOSE_PAREN)
18644 /* There are no parameters. */
18646 #ifndef NO_IMPLICIT_EXTERN_C
18647 if (in_system_header_at (input_location)
18648 && current_class_type == NULL
18649 && current_lang_name == lang_name_c)
18650 return NULL_TREE;
18651 else
18652 #endif
18653 return void_list_node;
18655 /* Check for `(void)', too, which is a special case. */
18656 else if (token->keyword == RID_VOID
18657 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18658 == CPP_CLOSE_PAREN))
18660 /* Consume the `void' token. */
18661 cp_lexer_consume_token (parser->lexer);
18662 /* There are no parameters. */
18663 return void_list_node;
18666 /* Parse the parameter-declaration-list. */
18667 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18668 /* If a parse error occurred while parsing the
18669 parameter-declaration-list, then the entire
18670 parameter-declaration-clause is erroneous. */
18671 if (is_error)
18672 return NULL;
18674 /* Peek at the next token. */
18675 token = cp_lexer_peek_token (parser->lexer);
18676 /* If it's a `,', the clause should terminate with an ellipsis. */
18677 if (token->type == CPP_COMMA)
18679 /* Consume the `,'. */
18680 cp_lexer_consume_token (parser->lexer);
18681 /* Expect an ellipsis. */
18682 ellipsis_p
18683 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18685 /* It might also be `...' if the optional trailing `,' was
18686 omitted. */
18687 else if (token->type == CPP_ELLIPSIS)
18689 /* Consume the `...' token. */
18690 cp_lexer_consume_token (parser->lexer);
18691 /* And remember that we saw it. */
18692 ellipsis_p = true;
18694 else
18695 ellipsis_p = false;
18697 /* Finish the parameter list. */
18698 if (!ellipsis_p)
18699 parameters = chainon (parameters, void_list_node);
18701 return parameters;
18704 /* Parse a parameter-declaration-list.
18706 parameter-declaration-list:
18707 parameter-declaration
18708 parameter-declaration-list , parameter-declaration
18710 Returns a representation of the parameter-declaration-list, as for
18711 cp_parser_parameter_declaration_clause. However, the
18712 `void_list_node' is never appended to the list. Upon return,
18713 *IS_ERROR will be true iff an error occurred. */
18715 static tree
18716 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18718 tree parameters = NULL_TREE;
18719 tree *tail = &parameters;
18720 bool saved_in_unbraced_linkage_specification_p;
18721 int index = 0;
18723 /* Assume all will go well. */
18724 *is_error = false;
18725 /* The special considerations that apply to a function within an
18726 unbraced linkage specifications do not apply to the parameters
18727 to the function. */
18728 saved_in_unbraced_linkage_specification_p
18729 = parser->in_unbraced_linkage_specification_p;
18730 parser->in_unbraced_linkage_specification_p = false;
18732 /* Look for more parameters. */
18733 while (true)
18735 cp_parameter_declarator *parameter;
18736 tree decl = error_mark_node;
18737 bool parenthesized_p = false;
18738 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18739 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18740 (current_template_parms)) : 0);
18742 /* Parse the parameter. */
18743 parameter
18744 = cp_parser_parameter_declaration (parser,
18745 /*template_parm_p=*/false,
18746 &parenthesized_p);
18748 /* We don't know yet if the enclosing context is deprecated, so wait
18749 and warn in grokparms if appropriate. */
18750 deprecated_state = DEPRECATED_SUPPRESS;
18752 if (parameter)
18754 /* If a function parameter pack was specified and an implicit template
18755 parameter was introduced during cp_parser_parameter_declaration,
18756 change any implicit parameters introduced into packs. */
18757 if (parser->implicit_template_parms
18758 && parameter->declarator
18759 && parameter->declarator->parameter_pack_p)
18761 int latest_template_parm_idx = TREE_VEC_LENGTH
18762 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18764 if (latest_template_parm_idx != template_parm_idx)
18765 parameter->decl_specifiers.type = convert_generic_types_to_packs
18766 (parameter->decl_specifiers.type,
18767 template_parm_idx, latest_template_parm_idx);
18770 decl = grokdeclarator (parameter->declarator,
18771 &parameter->decl_specifiers,
18772 PARM,
18773 parameter->default_argument != NULL_TREE,
18774 &parameter->decl_specifiers.attributes);
18777 deprecated_state = DEPRECATED_NORMAL;
18779 /* If a parse error occurred parsing the parameter declaration,
18780 then the entire parameter-declaration-list is erroneous. */
18781 if (decl == error_mark_node)
18783 *is_error = true;
18784 parameters = error_mark_node;
18785 break;
18788 if (parameter->decl_specifiers.attributes)
18789 cplus_decl_attributes (&decl,
18790 parameter->decl_specifiers.attributes,
18792 if (DECL_NAME (decl))
18793 decl = pushdecl (decl);
18795 if (decl != error_mark_node)
18797 retrofit_lang_decl (decl);
18798 DECL_PARM_INDEX (decl) = ++index;
18799 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18802 /* Add the new parameter to the list. */
18803 *tail = build_tree_list (parameter->default_argument, decl);
18804 tail = &TREE_CHAIN (*tail);
18806 /* Peek at the next token. */
18807 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18808 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18809 /* These are for Objective-C++ */
18810 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18811 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18812 /* The parameter-declaration-list is complete. */
18813 break;
18814 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18816 cp_token *token;
18818 /* Peek at the next token. */
18819 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18820 /* If it's an ellipsis, then the list is complete. */
18821 if (token->type == CPP_ELLIPSIS)
18822 break;
18823 /* Otherwise, there must be more parameters. Consume the
18824 `,'. */
18825 cp_lexer_consume_token (parser->lexer);
18826 /* When parsing something like:
18828 int i(float f, double d)
18830 we can tell after seeing the declaration for "f" that we
18831 are not looking at an initialization of a variable "i",
18832 but rather at the declaration of a function "i".
18834 Due to the fact that the parsing of template arguments
18835 (as specified to a template-id) requires backtracking we
18836 cannot use this technique when inside a template argument
18837 list. */
18838 if (!parser->in_template_argument_list_p
18839 && !parser->in_type_id_in_expr_p
18840 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18841 /* However, a parameter-declaration of the form
18842 "float(f)" (which is a valid declaration of a
18843 parameter "f") can also be interpreted as an
18844 expression (the conversion of "f" to "float"). */
18845 && !parenthesized_p)
18846 cp_parser_commit_to_tentative_parse (parser);
18848 else
18850 cp_parser_error (parser, "expected %<,%> or %<...%>");
18851 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18852 cp_parser_skip_to_closing_parenthesis (parser,
18853 /*recovering=*/true,
18854 /*or_comma=*/false,
18855 /*consume_paren=*/false);
18856 break;
18860 parser->in_unbraced_linkage_specification_p
18861 = saved_in_unbraced_linkage_specification_p;
18863 /* Reset implicit_template_scope if we are about to leave the function
18864 parameter list that introduced it. Note that for out-of-line member
18865 definitions, there will be one or more class scopes before we get to
18866 the template parameter scope. */
18868 if (cp_binding_level *its = parser->implicit_template_scope)
18869 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18871 while (maybe_its->kind == sk_class)
18872 maybe_its = maybe_its->level_chain;
18873 if (maybe_its == its)
18875 parser->implicit_template_parms = 0;
18876 parser->implicit_template_scope = 0;
18880 return parameters;
18883 /* Parse a parameter declaration.
18885 parameter-declaration:
18886 decl-specifier-seq ... [opt] declarator
18887 decl-specifier-seq declarator = assignment-expression
18888 decl-specifier-seq ... [opt] abstract-declarator [opt]
18889 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18891 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18892 declares a template parameter. (In that case, a non-nested `>'
18893 token encountered during the parsing of the assignment-expression
18894 is not interpreted as a greater-than operator.)
18896 Returns a representation of the parameter, or NULL if an error
18897 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18898 true iff the declarator is of the form "(p)". */
18900 static cp_parameter_declarator *
18901 cp_parser_parameter_declaration (cp_parser *parser,
18902 bool template_parm_p,
18903 bool *parenthesized_p)
18905 int declares_class_or_enum;
18906 cp_decl_specifier_seq decl_specifiers;
18907 cp_declarator *declarator;
18908 tree default_argument;
18909 cp_token *token = NULL, *declarator_token_start = NULL;
18910 const char *saved_message;
18912 /* In a template parameter, `>' is not an operator.
18914 [temp.param]
18916 When parsing a default template-argument for a non-type
18917 template-parameter, the first non-nested `>' is taken as the end
18918 of the template parameter-list rather than a greater-than
18919 operator. */
18921 /* Type definitions may not appear in parameter types. */
18922 saved_message = parser->type_definition_forbidden_message;
18923 parser->type_definition_forbidden_message
18924 = G_("types may not be defined in parameter types");
18926 /* Parse the declaration-specifiers. */
18927 cp_parser_decl_specifier_seq (parser,
18928 CP_PARSER_FLAGS_NONE,
18929 &decl_specifiers,
18930 &declares_class_or_enum);
18932 /* Complain about missing 'typename' or other invalid type names. */
18933 if (!decl_specifiers.any_type_specifiers_p
18934 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18935 decl_specifiers.type = error_mark_node;
18937 /* If an error occurred, there's no reason to attempt to parse the
18938 rest of the declaration. */
18939 if (cp_parser_error_occurred (parser))
18941 parser->type_definition_forbidden_message = saved_message;
18942 return NULL;
18945 /* Peek at the next token. */
18946 token = cp_lexer_peek_token (parser->lexer);
18948 /* If the next token is a `)', `,', `=', `>', or `...', then there
18949 is no declarator. However, when variadic templates are enabled,
18950 there may be a declarator following `...'. */
18951 if (token->type == CPP_CLOSE_PAREN
18952 || token->type == CPP_COMMA
18953 || token->type == CPP_EQ
18954 || token->type == CPP_GREATER)
18956 declarator = NULL;
18957 if (parenthesized_p)
18958 *parenthesized_p = false;
18960 /* Otherwise, there should be a declarator. */
18961 else
18963 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18964 parser->default_arg_ok_p = false;
18966 /* After seeing a decl-specifier-seq, if the next token is not a
18967 "(", there is no possibility that the code is a valid
18968 expression. Therefore, if parsing tentatively, we commit at
18969 this point. */
18970 if (!parser->in_template_argument_list_p
18971 /* In an expression context, having seen:
18973 (int((char ...
18975 we cannot be sure whether we are looking at a
18976 function-type (taking a "char" as a parameter) or a cast
18977 of some object of type "char" to "int". */
18978 && !parser->in_type_id_in_expr_p
18979 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18980 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18981 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18982 cp_parser_commit_to_tentative_parse (parser);
18983 /* Parse the declarator. */
18984 declarator_token_start = token;
18985 declarator = cp_parser_declarator (parser,
18986 CP_PARSER_DECLARATOR_EITHER,
18987 /*ctor_dtor_or_conv_p=*/NULL,
18988 parenthesized_p,
18989 /*member_p=*/false,
18990 /*friend_p=*/false);
18991 parser->default_arg_ok_p = saved_default_arg_ok_p;
18992 /* After the declarator, allow more attributes. */
18993 decl_specifiers.attributes
18994 = chainon (decl_specifiers.attributes,
18995 cp_parser_attributes_opt (parser));
18998 /* If the next token is an ellipsis, and we have not seen a
18999 declarator name, and the type of the declarator contains parameter
19000 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19001 a parameter pack expansion expression. Otherwise, leave the
19002 ellipsis for a C-style variadic function. */
19003 token = cp_lexer_peek_token (parser->lexer);
19004 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19006 tree type = decl_specifiers.type;
19008 if (type && DECL_P (type))
19009 type = TREE_TYPE (type);
19011 if (type
19012 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19013 && declarator_can_be_parameter_pack (declarator)
19014 && (!declarator || !declarator->parameter_pack_p)
19015 && uses_parameter_packs (type))
19017 /* Consume the `...'. */
19018 cp_lexer_consume_token (parser->lexer);
19019 maybe_warn_variadic_templates ();
19021 /* Build a pack expansion type */
19022 if (declarator)
19023 declarator->parameter_pack_p = true;
19024 else
19025 decl_specifiers.type = make_pack_expansion (type);
19029 /* The restriction on defining new types applies only to the type
19030 of the parameter, not to the default argument. */
19031 parser->type_definition_forbidden_message = saved_message;
19033 /* If the next token is `=', then process a default argument. */
19034 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19036 token = cp_lexer_peek_token (parser->lexer);
19037 /* If we are defining a class, then the tokens that make up the
19038 default argument must be saved and processed later. */
19039 if (!template_parm_p && at_class_scope_p ()
19040 && TYPE_BEING_DEFINED (current_class_type)
19041 && !LAMBDA_TYPE_P (current_class_type))
19042 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19043 /* Outside of a class definition, we can just parse the
19044 assignment-expression. */
19045 else
19046 default_argument
19047 = cp_parser_default_argument (parser, template_parm_p);
19049 if (!parser->default_arg_ok_p)
19051 if (flag_permissive)
19052 warning (0, "deprecated use of default argument for parameter of non-function");
19053 else
19055 error_at (token->location,
19056 "default arguments are only "
19057 "permitted for function parameters");
19058 default_argument = NULL_TREE;
19061 else if ((declarator && declarator->parameter_pack_p)
19062 || (decl_specifiers.type
19063 && PACK_EXPANSION_P (decl_specifiers.type)))
19065 /* Find the name of the parameter pack. */
19066 cp_declarator *id_declarator = declarator;
19067 while (id_declarator && id_declarator->kind != cdk_id)
19068 id_declarator = id_declarator->declarator;
19070 if (id_declarator && id_declarator->kind == cdk_id)
19071 error_at (declarator_token_start->location,
19072 template_parm_p
19073 ? G_("template parameter pack %qD "
19074 "cannot have a default argument")
19075 : G_("parameter pack %qD cannot have "
19076 "a default argument"),
19077 id_declarator->u.id.unqualified_name);
19078 else
19079 error_at (declarator_token_start->location,
19080 template_parm_p
19081 ? G_("template parameter pack cannot have "
19082 "a default argument")
19083 : G_("parameter pack cannot have a "
19084 "default argument"));
19086 default_argument = NULL_TREE;
19089 else
19090 default_argument = NULL_TREE;
19092 return make_parameter_declarator (&decl_specifiers,
19093 declarator,
19094 default_argument);
19097 /* Parse a default argument and return it.
19099 TEMPLATE_PARM_P is true if this is a default argument for a
19100 non-type template parameter. */
19101 static tree
19102 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19104 tree default_argument = NULL_TREE;
19105 bool saved_greater_than_is_operator_p;
19106 bool saved_local_variables_forbidden_p;
19107 bool non_constant_p, is_direct_init;
19109 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19110 set correctly. */
19111 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19112 parser->greater_than_is_operator_p = !template_parm_p;
19113 /* Local variable names (and the `this' keyword) may not
19114 appear in a default argument. */
19115 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19116 parser->local_variables_forbidden_p = true;
19117 /* Parse the assignment-expression. */
19118 if (template_parm_p)
19119 push_deferring_access_checks (dk_no_deferred);
19120 tree saved_class_ptr = NULL_TREE;
19121 tree saved_class_ref = NULL_TREE;
19122 /* The "this" pointer is not valid in a default argument. */
19123 if (cfun)
19125 saved_class_ptr = current_class_ptr;
19126 cp_function_chain->x_current_class_ptr = NULL_TREE;
19127 saved_class_ref = current_class_ref;
19128 cp_function_chain->x_current_class_ref = NULL_TREE;
19130 default_argument
19131 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19132 /* Restore the "this" pointer. */
19133 if (cfun)
19135 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19136 cp_function_chain->x_current_class_ref = saved_class_ref;
19138 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19139 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19140 if (template_parm_p)
19141 pop_deferring_access_checks ();
19142 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19143 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19145 return default_argument;
19148 /* Parse a function-body.
19150 function-body:
19151 compound_statement */
19153 static void
19154 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19156 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19159 /* Parse a ctor-initializer-opt followed by a function-body. Return
19160 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19161 is true we are parsing a function-try-block. */
19163 static bool
19164 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19165 bool in_function_try_block)
19167 tree body, list;
19168 bool ctor_initializer_p;
19169 const bool check_body_p =
19170 DECL_CONSTRUCTOR_P (current_function_decl)
19171 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19172 tree last = NULL;
19174 /* Begin the function body. */
19175 body = begin_function_body ();
19176 /* Parse the optional ctor-initializer. */
19177 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19179 /* If we're parsing a constexpr constructor definition, we need
19180 to check that the constructor body is indeed empty. However,
19181 before we get to cp_parser_function_body lot of junk has been
19182 generated, so we can't just check that we have an empty block.
19183 Rather we take a snapshot of the outermost block, and check whether
19184 cp_parser_function_body changed its state. */
19185 if (check_body_p)
19187 list = cur_stmt_list;
19188 if (STATEMENT_LIST_TAIL (list))
19189 last = STATEMENT_LIST_TAIL (list)->stmt;
19191 /* Parse the function-body. */
19192 cp_parser_function_body (parser, in_function_try_block);
19193 if (check_body_p)
19194 check_constexpr_ctor_body (last, list, /*complain=*/true);
19195 /* Finish the function body. */
19196 finish_function_body (body);
19198 return ctor_initializer_p;
19201 /* Parse an initializer.
19203 initializer:
19204 = initializer-clause
19205 ( expression-list )
19207 Returns an expression representing the initializer. If no
19208 initializer is present, NULL_TREE is returned.
19210 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19211 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19212 set to TRUE if there is no initializer present. If there is an
19213 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19214 is set to true; otherwise it is set to false. */
19216 static tree
19217 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19218 bool* non_constant_p)
19220 cp_token *token;
19221 tree init;
19223 /* Peek at the next token. */
19224 token = cp_lexer_peek_token (parser->lexer);
19226 /* Let our caller know whether or not this initializer was
19227 parenthesized. */
19228 *is_direct_init = (token->type != CPP_EQ);
19229 /* Assume that the initializer is constant. */
19230 *non_constant_p = false;
19232 if (token->type == CPP_EQ)
19234 /* Consume the `='. */
19235 cp_lexer_consume_token (parser->lexer);
19236 /* Parse the initializer-clause. */
19237 init = cp_parser_initializer_clause (parser, non_constant_p);
19239 else if (token->type == CPP_OPEN_PAREN)
19241 vec<tree, va_gc> *vec;
19242 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19243 /*cast_p=*/false,
19244 /*allow_expansion_p=*/true,
19245 non_constant_p);
19246 if (vec == NULL)
19247 return error_mark_node;
19248 init = build_tree_list_vec (vec);
19249 release_tree_vector (vec);
19251 else if (token->type == CPP_OPEN_BRACE)
19253 cp_lexer_set_source_position (parser->lexer);
19254 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19255 init = cp_parser_braced_list (parser, non_constant_p);
19256 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19258 else
19260 /* Anything else is an error. */
19261 cp_parser_error (parser, "expected initializer");
19262 init = error_mark_node;
19265 return init;
19268 /* Parse an initializer-clause.
19270 initializer-clause:
19271 assignment-expression
19272 braced-init-list
19274 Returns an expression representing the initializer.
19276 If the `assignment-expression' production is used the value
19277 returned is simply a representation for the expression.
19279 Otherwise, calls cp_parser_braced_list. */
19281 static tree
19282 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19284 tree initializer;
19286 /* Assume the expression is constant. */
19287 *non_constant_p = false;
19289 /* If it is not a `{', then we are looking at an
19290 assignment-expression. */
19291 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19293 initializer
19294 = cp_parser_constant_expression (parser,
19295 /*allow_non_constant_p=*/true,
19296 non_constant_p);
19298 else
19299 initializer = cp_parser_braced_list (parser, non_constant_p);
19301 return initializer;
19304 /* Parse a brace-enclosed initializer list.
19306 braced-init-list:
19307 { initializer-list , [opt] }
19310 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19311 the elements of the initializer-list (or NULL, if the last
19312 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19313 NULL_TREE. There is no way to detect whether or not the optional
19314 trailing `,' was provided. NON_CONSTANT_P is as for
19315 cp_parser_initializer. */
19317 static tree
19318 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19320 tree initializer;
19322 /* Consume the `{' token. */
19323 cp_lexer_consume_token (parser->lexer);
19324 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19325 initializer = make_node (CONSTRUCTOR);
19326 /* If it's not a `}', then there is a non-trivial initializer. */
19327 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19329 /* Parse the initializer list. */
19330 CONSTRUCTOR_ELTS (initializer)
19331 = cp_parser_initializer_list (parser, non_constant_p);
19332 /* A trailing `,' token is allowed. */
19333 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19334 cp_lexer_consume_token (parser->lexer);
19336 else
19337 *non_constant_p = false;
19338 /* Now, there should be a trailing `}'. */
19339 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19340 TREE_TYPE (initializer) = init_list_type_node;
19341 return initializer;
19344 /* Consume tokens up to, and including, the next non-nested closing `]'.
19345 Returns true iff we found a closing `]'. */
19347 static bool
19348 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19350 unsigned square_depth = 0;
19352 while (true)
19354 cp_token * token = cp_lexer_peek_token (parser->lexer);
19356 switch (token->type)
19358 case CPP_EOF:
19359 case CPP_PRAGMA_EOL:
19360 /* If we've run out of tokens, then there is no closing `]'. */
19361 return false;
19363 case CPP_OPEN_SQUARE:
19364 ++square_depth;
19365 break;
19367 case CPP_CLOSE_SQUARE:
19368 if (!square_depth--)
19370 cp_lexer_consume_token (parser->lexer);
19371 return true;
19373 break;
19375 default:
19376 break;
19379 /* Consume the token. */
19380 cp_lexer_consume_token (parser->lexer);
19384 /* Return true if we are looking at an array-designator, false otherwise. */
19386 static bool
19387 cp_parser_array_designator_p (cp_parser *parser)
19389 /* Consume the `['. */
19390 cp_lexer_consume_token (parser->lexer);
19392 cp_lexer_save_tokens (parser->lexer);
19394 /* Skip tokens until the next token is a closing square bracket.
19395 If we find the closing `]', and the next token is a `=', then
19396 we are looking at an array designator. */
19397 bool array_designator_p
19398 = (cp_parser_skip_to_closing_square_bracket (parser)
19399 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19401 /* Roll back the tokens we skipped. */
19402 cp_lexer_rollback_tokens (parser->lexer);
19404 return array_designator_p;
19407 /* Parse an initializer-list.
19409 initializer-list:
19410 initializer-clause ... [opt]
19411 initializer-list , initializer-clause ... [opt]
19413 GNU Extension:
19415 initializer-list:
19416 designation initializer-clause ...[opt]
19417 initializer-list , designation initializer-clause ...[opt]
19419 designation:
19420 . identifier =
19421 identifier :
19422 [ constant-expression ] =
19424 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19425 for the initializer. If the INDEX of the elt is non-NULL, it is the
19426 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19427 as for cp_parser_initializer. */
19429 static vec<constructor_elt, va_gc> *
19430 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19432 vec<constructor_elt, va_gc> *v = NULL;
19434 /* Assume all of the expressions are constant. */
19435 *non_constant_p = false;
19437 /* Parse the rest of the list. */
19438 while (true)
19440 cp_token *token;
19441 tree designator;
19442 tree initializer;
19443 bool clause_non_constant_p;
19445 /* If the next token is an identifier and the following one is a
19446 colon, we are looking at the GNU designated-initializer
19447 syntax. */
19448 if (cp_parser_allow_gnu_extensions_p (parser)
19449 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19450 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19452 /* Warn the user that they are using an extension. */
19453 pedwarn (input_location, OPT_Wpedantic,
19454 "ISO C++ does not allow designated initializers");
19455 /* Consume the identifier. */
19456 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19457 /* Consume the `:'. */
19458 cp_lexer_consume_token (parser->lexer);
19460 /* Also handle the C99 syntax, '. id ='. */
19461 else if (cp_parser_allow_gnu_extensions_p (parser)
19462 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19463 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19464 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19466 /* Warn the user that they are using an extension. */
19467 pedwarn (input_location, OPT_Wpedantic,
19468 "ISO C++ does not allow C99 designated initializers");
19469 /* Consume the `.'. */
19470 cp_lexer_consume_token (parser->lexer);
19471 /* Consume the identifier. */
19472 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19473 /* Consume the `='. */
19474 cp_lexer_consume_token (parser->lexer);
19476 /* Also handle C99 array designators, '[ const ] ='. */
19477 else if (cp_parser_allow_gnu_extensions_p (parser)
19478 && !c_dialect_objc ()
19479 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19481 /* In C++11, [ could start a lambda-introducer. */
19482 bool non_const = false;
19484 cp_parser_parse_tentatively (parser);
19486 if (!cp_parser_array_designator_p (parser))
19488 cp_parser_simulate_error (parser);
19489 designator = NULL_TREE;
19491 else
19493 designator = cp_parser_constant_expression (parser, true,
19494 &non_const);
19495 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19496 cp_parser_require (parser, CPP_EQ, RT_EQ);
19499 if (!cp_parser_parse_definitely (parser))
19500 designator = NULL_TREE;
19501 else if (non_const)
19502 require_potential_rvalue_constant_expression (designator);
19504 else
19505 designator = NULL_TREE;
19507 /* Parse the initializer. */
19508 initializer = cp_parser_initializer_clause (parser,
19509 &clause_non_constant_p);
19510 /* If any clause is non-constant, so is the entire initializer. */
19511 if (clause_non_constant_p)
19512 *non_constant_p = true;
19514 /* If we have an ellipsis, this is an initializer pack
19515 expansion. */
19516 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19518 /* Consume the `...'. */
19519 cp_lexer_consume_token (parser->lexer);
19521 /* Turn the initializer into an initializer expansion. */
19522 initializer = make_pack_expansion (initializer);
19525 /* Add it to the vector. */
19526 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19528 /* If the next token is not a comma, we have reached the end of
19529 the list. */
19530 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19531 break;
19533 /* Peek at the next token. */
19534 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19535 /* If the next token is a `}', then we're still done. An
19536 initializer-clause can have a trailing `,' after the
19537 initializer-list and before the closing `}'. */
19538 if (token->type == CPP_CLOSE_BRACE)
19539 break;
19541 /* Consume the `,' token. */
19542 cp_lexer_consume_token (parser->lexer);
19545 return v;
19548 /* Classes [gram.class] */
19550 /* Parse a class-name.
19552 class-name:
19553 identifier
19554 template-id
19556 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19557 to indicate that names looked up in dependent types should be
19558 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19559 keyword has been used to indicate that the name that appears next
19560 is a template. TAG_TYPE indicates the explicit tag given before
19561 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19562 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19563 is the class being defined in a class-head.
19565 Returns the TYPE_DECL representing the class. */
19567 static tree
19568 cp_parser_class_name (cp_parser *parser,
19569 bool typename_keyword_p,
19570 bool template_keyword_p,
19571 enum tag_types tag_type,
19572 bool check_dependency_p,
19573 bool class_head_p,
19574 bool is_declaration)
19576 tree decl;
19577 tree scope;
19578 bool typename_p;
19579 cp_token *token;
19580 tree identifier = NULL_TREE;
19582 /* All class-names start with an identifier. */
19583 token = cp_lexer_peek_token (parser->lexer);
19584 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19586 cp_parser_error (parser, "expected class-name");
19587 return error_mark_node;
19590 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19591 to a template-id, so we save it here. */
19592 scope = parser->scope;
19593 if (scope == error_mark_node)
19594 return error_mark_node;
19596 /* Any name names a type if we're following the `typename' keyword
19597 in a qualified name where the enclosing scope is type-dependent. */
19598 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19599 && dependent_type_p (scope));
19600 /* Handle the common case (an identifier, but not a template-id)
19601 efficiently. */
19602 if (token->type == CPP_NAME
19603 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19605 cp_token *identifier_token;
19606 bool ambiguous_p;
19608 /* Look for the identifier. */
19609 identifier_token = cp_lexer_peek_token (parser->lexer);
19610 ambiguous_p = identifier_token->error_reported;
19611 identifier = cp_parser_identifier (parser);
19612 /* If the next token isn't an identifier, we are certainly not
19613 looking at a class-name. */
19614 if (identifier == error_mark_node)
19615 decl = error_mark_node;
19616 /* If we know this is a type-name, there's no need to look it
19617 up. */
19618 else if (typename_p)
19619 decl = identifier;
19620 else
19622 tree ambiguous_decls;
19623 /* If we already know that this lookup is ambiguous, then
19624 we've already issued an error message; there's no reason
19625 to check again. */
19626 if (ambiguous_p)
19628 cp_parser_simulate_error (parser);
19629 return error_mark_node;
19631 /* If the next token is a `::', then the name must be a type
19632 name.
19634 [basic.lookup.qual]
19636 During the lookup for a name preceding the :: scope
19637 resolution operator, object, function, and enumerator
19638 names are ignored. */
19639 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19640 tag_type = typename_type;
19641 /* Look up the name. */
19642 decl = cp_parser_lookup_name (parser, identifier,
19643 tag_type,
19644 /*is_template=*/false,
19645 /*is_namespace=*/false,
19646 check_dependency_p,
19647 &ambiguous_decls,
19648 identifier_token->location);
19649 if (ambiguous_decls)
19651 if (cp_parser_parsing_tentatively (parser))
19652 cp_parser_simulate_error (parser);
19653 return error_mark_node;
19657 else
19659 /* Try a template-id. */
19660 decl = cp_parser_template_id (parser, template_keyword_p,
19661 check_dependency_p,
19662 tag_type,
19663 is_declaration);
19664 if (decl == error_mark_node)
19665 return error_mark_node;
19668 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19670 /* If this is a typename, create a TYPENAME_TYPE. */
19671 if (typename_p && decl != error_mark_node)
19673 decl = make_typename_type (scope, decl, typename_type,
19674 /*complain=*/tf_error);
19675 if (decl != error_mark_node)
19676 decl = TYPE_NAME (decl);
19679 decl = strip_using_decl (decl);
19681 /* Check to see that it is really the name of a class. */
19682 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19683 && identifier_p (TREE_OPERAND (decl, 0))
19684 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19685 /* Situations like this:
19687 template <typename T> struct A {
19688 typename T::template X<int>::I i;
19691 are problematic. Is `T::template X<int>' a class-name? The
19692 standard does not seem to be definitive, but there is no other
19693 valid interpretation of the following `::'. Therefore, those
19694 names are considered class-names. */
19696 decl = make_typename_type (scope, decl, tag_type, tf_error);
19697 if (decl != error_mark_node)
19698 decl = TYPE_NAME (decl);
19700 else if (TREE_CODE (decl) != TYPE_DECL
19701 || TREE_TYPE (decl) == error_mark_node
19702 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19703 /* In Objective-C 2.0, a classname followed by '.' starts a
19704 dot-syntax expression, and it's not a type-name. */
19705 || (c_dialect_objc ()
19706 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19707 && objc_is_class_name (decl)))
19708 decl = error_mark_node;
19710 if (decl == error_mark_node)
19711 cp_parser_error (parser, "expected class-name");
19712 else if (identifier && !parser->scope)
19713 maybe_note_name_used_in_class (identifier, decl);
19715 return decl;
19718 /* Parse a class-specifier.
19720 class-specifier:
19721 class-head { member-specification [opt] }
19723 Returns the TREE_TYPE representing the class. */
19725 static tree
19726 cp_parser_class_specifier_1 (cp_parser* parser)
19728 tree type;
19729 tree attributes = NULL_TREE;
19730 bool nested_name_specifier_p;
19731 unsigned saved_num_template_parameter_lists;
19732 bool saved_in_function_body;
19733 unsigned char in_statement;
19734 bool in_switch_statement_p;
19735 bool saved_in_unbraced_linkage_specification_p;
19736 tree old_scope = NULL_TREE;
19737 tree scope = NULL_TREE;
19738 cp_token *closing_brace;
19740 push_deferring_access_checks (dk_no_deferred);
19742 /* Parse the class-head. */
19743 type = cp_parser_class_head (parser,
19744 &nested_name_specifier_p);
19745 /* If the class-head was a semantic disaster, skip the entire body
19746 of the class. */
19747 if (!type)
19749 cp_parser_skip_to_end_of_block_or_statement (parser);
19750 pop_deferring_access_checks ();
19751 return error_mark_node;
19754 /* Look for the `{'. */
19755 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19757 pop_deferring_access_checks ();
19758 return error_mark_node;
19761 cp_ensure_no_omp_declare_simd (parser);
19763 /* Issue an error message if type-definitions are forbidden here. */
19764 cp_parser_check_type_definition (parser);
19765 /* Remember that we are defining one more class. */
19766 ++parser->num_classes_being_defined;
19767 /* Inside the class, surrounding template-parameter-lists do not
19768 apply. */
19769 saved_num_template_parameter_lists
19770 = parser->num_template_parameter_lists;
19771 parser->num_template_parameter_lists = 0;
19772 /* We are not in a function body. */
19773 saved_in_function_body = parser->in_function_body;
19774 parser->in_function_body = false;
19775 /* Or in a loop. */
19776 in_statement = parser->in_statement;
19777 parser->in_statement = 0;
19778 /* Or in a switch. */
19779 in_switch_statement_p = parser->in_switch_statement_p;
19780 parser->in_switch_statement_p = false;
19781 /* We are not immediately inside an extern "lang" block. */
19782 saved_in_unbraced_linkage_specification_p
19783 = parser->in_unbraced_linkage_specification_p;
19784 parser->in_unbraced_linkage_specification_p = false;
19786 /* Start the class. */
19787 if (nested_name_specifier_p)
19789 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19790 old_scope = push_inner_scope (scope);
19792 type = begin_class_definition (type);
19794 if (type == error_mark_node)
19795 /* If the type is erroneous, skip the entire body of the class. */
19796 cp_parser_skip_to_closing_brace (parser);
19797 else
19798 /* Parse the member-specification. */
19799 cp_parser_member_specification_opt (parser);
19801 /* Look for the trailing `}'. */
19802 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19803 /* Look for trailing attributes to apply to this class. */
19804 if (cp_parser_allow_gnu_extensions_p (parser))
19805 attributes = cp_parser_gnu_attributes_opt (parser);
19806 if (type != error_mark_node)
19807 type = finish_struct (type, attributes);
19808 if (nested_name_specifier_p)
19809 pop_inner_scope (old_scope, scope);
19811 /* We've finished a type definition. Check for the common syntax
19812 error of forgetting a semicolon after the definition. We need to
19813 be careful, as we can't just check for not-a-semicolon and be done
19814 with it; the user might have typed:
19816 class X { } c = ...;
19817 class X { } *p = ...;
19819 and so forth. Instead, enumerate all the possible tokens that
19820 might follow this production; if we don't see one of them, then
19821 complain and silently insert the semicolon. */
19823 cp_token *token = cp_lexer_peek_token (parser->lexer);
19824 bool want_semicolon = true;
19826 if (cp_next_tokens_can_be_std_attribute_p (parser))
19827 /* Don't try to parse c++11 attributes here. As per the
19828 grammar, that should be a task for
19829 cp_parser_decl_specifier_seq. */
19830 want_semicolon = false;
19832 switch (token->type)
19834 case CPP_NAME:
19835 case CPP_SEMICOLON:
19836 case CPP_MULT:
19837 case CPP_AND:
19838 case CPP_OPEN_PAREN:
19839 case CPP_CLOSE_PAREN:
19840 case CPP_COMMA:
19841 want_semicolon = false;
19842 break;
19844 /* While it's legal for type qualifiers and storage class
19845 specifiers to follow type definitions in the grammar, only
19846 compiler testsuites contain code like that. Assume that if
19847 we see such code, then what we're really seeing is a case
19848 like:
19850 class X { }
19851 const <type> var = ...;
19855 class Y { }
19856 static <type> func (...) ...
19858 i.e. the qualifier or specifier applies to the next
19859 declaration. To do so, however, we need to look ahead one
19860 more token to see if *that* token is a type specifier.
19862 This code could be improved to handle:
19864 class Z { }
19865 static const <type> var = ...; */
19866 case CPP_KEYWORD:
19867 if (keyword_is_decl_specifier (token->keyword))
19869 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19871 /* Handling user-defined types here would be nice, but very
19872 tricky. */
19873 want_semicolon
19874 = (lookahead->type == CPP_KEYWORD
19875 && keyword_begins_type_specifier (lookahead->keyword));
19877 break;
19878 default:
19879 break;
19882 /* If we don't have a type, then something is very wrong and we
19883 shouldn't try to do anything clever. Likewise for not seeing the
19884 closing brace. */
19885 if (closing_brace && TYPE_P (type) && want_semicolon)
19887 cp_token_position prev
19888 = cp_lexer_previous_token_position (parser->lexer);
19889 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19890 location_t loc = prev_token->location;
19892 if (CLASSTYPE_DECLARED_CLASS (type))
19893 error_at (loc, "expected %<;%> after class definition");
19894 else if (TREE_CODE (type) == RECORD_TYPE)
19895 error_at (loc, "expected %<;%> after struct definition");
19896 else if (TREE_CODE (type) == UNION_TYPE)
19897 error_at (loc, "expected %<;%> after union definition");
19898 else
19899 gcc_unreachable ();
19901 /* Unget one token and smash it to look as though we encountered
19902 a semicolon in the input stream. */
19903 cp_lexer_set_token_position (parser->lexer, prev);
19904 token = cp_lexer_peek_token (parser->lexer);
19905 token->type = CPP_SEMICOLON;
19906 token->keyword = RID_MAX;
19910 /* If this class is not itself within the scope of another class,
19911 then we need to parse the bodies of all of the queued function
19912 definitions. Note that the queued functions defined in a class
19913 are not always processed immediately following the
19914 class-specifier for that class. Consider:
19916 struct A {
19917 struct B { void f() { sizeof (A); } };
19920 If `f' were processed before the processing of `A' were
19921 completed, there would be no way to compute the size of `A'.
19922 Note that the nesting we are interested in here is lexical --
19923 not the semantic nesting given by TYPE_CONTEXT. In particular,
19924 for:
19926 struct A { struct B; };
19927 struct A::B { void f() { } };
19929 there is no need to delay the parsing of `A::B::f'. */
19930 if (--parser->num_classes_being_defined == 0)
19932 tree decl;
19933 tree class_type = NULL_TREE;
19934 tree pushed_scope = NULL_TREE;
19935 unsigned ix;
19936 cp_default_arg_entry *e;
19937 tree save_ccp, save_ccr;
19939 /* In a first pass, parse default arguments to the functions.
19940 Then, in a second pass, parse the bodies of the functions.
19941 This two-phased approach handles cases like:
19943 struct S {
19944 void f() { g(); }
19945 void g(int i = 3);
19949 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19951 decl = e->decl;
19952 /* If there are default arguments that have not yet been processed,
19953 take care of them now. */
19954 if (class_type != e->class_type)
19956 if (pushed_scope)
19957 pop_scope (pushed_scope);
19958 class_type = e->class_type;
19959 pushed_scope = push_scope (class_type);
19961 /* Make sure that any template parameters are in scope. */
19962 maybe_begin_member_template_processing (decl);
19963 /* Parse the default argument expressions. */
19964 cp_parser_late_parsing_default_args (parser, decl);
19965 /* Remove any template parameters from the symbol table. */
19966 maybe_end_member_template_processing ();
19968 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19969 /* Now parse any NSDMIs. */
19970 save_ccp = current_class_ptr;
19971 save_ccr = current_class_ref;
19972 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19974 if (class_type != DECL_CONTEXT (decl))
19976 if (pushed_scope)
19977 pop_scope (pushed_scope);
19978 class_type = DECL_CONTEXT (decl);
19979 pushed_scope = push_scope (class_type);
19981 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19982 cp_parser_late_parsing_nsdmi (parser, decl);
19984 vec_safe_truncate (unparsed_nsdmis, 0);
19985 current_class_ptr = save_ccp;
19986 current_class_ref = save_ccr;
19987 if (pushed_scope)
19988 pop_scope (pushed_scope);
19990 /* Now do some post-NSDMI bookkeeping. */
19991 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19992 after_nsdmi_defaulted_late_checks (class_type);
19993 vec_safe_truncate (unparsed_classes, 0);
19994 after_nsdmi_defaulted_late_checks (type);
19996 /* Now parse the body of the functions. */
19997 if (flag_openmp)
19999 /* OpenMP UDRs need to be parsed before all other functions. */
20000 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20001 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20002 cp_parser_late_parsing_for_member (parser, decl);
20003 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20004 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20005 cp_parser_late_parsing_for_member (parser, decl);
20007 else
20008 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20009 cp_parser_late_parsing_for_member (parser, decl);
20010 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20012 else
20013 vec_safe_push (unparsed_classes, type);
20015 /* Put back any saved access checks. */
20016 pop_deferring_access_checks ();
20018 /* Restore saved state. */
20019 parser->in_switch_statement_p = in_switch_statement_p;
20020 parser->in_statement = in_statement;
20021 parser->in_function_body = saved_in_function_body;
20022 parser->num_template_parameter_lists
20023 = saved_num_template_parameter_lists;
20024 parser->in_unbraced_linkage_specification_p
20025 = saved_in_unbraced_linkage_specification_p;
20027 return type;
20030 static tree
20031 cp_parser_class_specifier (cp_parser* parser)
20033 tree ret;
20034 timevar_push (TV_PARSE_STRUCT);
20035 ret = cp_parser_class_specifier_1 (parser);
20036 timevar_pop (TV_PARSE_STRUCT);
20037 return ret;
20040 /* Parse a class-head.
20042 class-head:
20043 class-key identifier [opt] base-clause [opt]
20044 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20045 class-key nested-name-specifier [opt] template-id
20046 base-clause [opt]
20048 class-virt-specifier:
20049 final
20051 GNU Extensions:
20052 class-key attributes identifier [opt] base-clause [opt]
20053 class-key attributes nested-name-specifier identifier base-clause [opt]
20054 class-key attributes nested-name-specifier [opt] template-id
20055 base-clause [opt]
20057 Upon return BASES is initialized to the list of base classes (or
20058 NULL, if there are none) in the same form returned by
20059 cp_parser_base_clause.
20061 Returns the TYPE of the indicated class. Sets
20062 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20063 involving a nested-name-specifier was used, and FALSE otherwise.
20065 Returns error_mark_node if this is not a class-head.
20067 Returns NULL_TREE if the class-head is syntactically valid, but
20068 semantically invalid in a way that means we should skip the entire
20069 body of the class. */
20071 static tree
20072 cp_parser_class_head (cp_parser* parser,
20073 bool* nested_name_specifier_p)
20075 tree nested_name_specifier;
20076 enum tag_types class_key;
20077 tree id = NULL_TREE;
20078 tree type = NULL_TREE;
20079 tree attributes;
20080 tree bases;
20081 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20082 bool template_id_p = false;
20083 bool qualified_p = false;
20084 bool invalid_nested_name_p = false;
20085 bool invalid_explicit_specialization_p = false;
20086 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20087 tree pushed_scope = NULL_TREE;
20088 unsigned num_templates;
20089 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20090 /* Assume no nested-name-specifier will be present. */
20091 *nested_name_specifier_p = false;
20092 /* Assume no template parameter lists will be used in defining the
20093 type. */
20094 num_templates = 0;
20095 parser->colon_corrects_to_scope_p = false;
20097 /* Look for the class-key. */
20098 class_key = cp_parser_class_key (parser);
20099 if (class_key == none_type)
20100 return error_mark_node;
20102 /* Parse the attributes. */
20103 attributes = cp_parser_attributes_opt (parser);
20105 /* If the next token is `::', that is invalid -- but sometimes
20106 people do try to write:
20108 struct ::S {};
20110 Handle this gracefully by accepting the extra qualifier, and then
20111 issuing an error about it later if this really is a
20112 class-head. If it turns out just to be an elaborated type
20113 specifier, remain silent. */
20114 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20115 qualified_p = true;
20117 push_deferring_access_checks (dk_no_check);
20119 /* Determine the name of the class. Begin by looking for an
20120 optional nested-name-specifier. */
20121 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20122 nested_name_specifier
20123 = cp_parser_nested_name_specifier_opt (parser,
20124 /*typename_keyword_p=*/false,
20125 /*check_dependency_p=*/false,
20126 /*type_p=*/true,
20127 /*is_declaration=*/false);
20128 /* If there was a nested-name-specifier, then there *must* be an
20129 identifier. */
20130 if (nested_name_specifier)
20132 type_start_token = cp_lexer_peek_token (parser->lexer);
20133 /* Although the grammar says `identifier', it really means
20134 `class-name' or `template-name'. You are only allowed to
20135 define a class that has already been declared with this
20136 syntax.
20138 The proposed resolution for Core Issue 180 says that wherever
20139 you see `class T::X' you should treat `X' as a type-name.
20141 It is OK to define an inaccessible class; for example:
20143 class A { class B; };
20144 class A::B {};
20146 We do not know if we will see a class-name, or a
20147 template-name. We look for a class-name first, in case the
20148 class-name is a template-id; if we looked for the
20149 template-name first we would stop after the template-name. */
20150 cp_parser_parse_tentatively (parser);
20151 type = cp_parser_class_name (parser,
20152 /*typename_keyword_p=*/false,
20153 /*template_keyword_p=*/false,
20154 class_type,
20155 /*check_dependency_p=*/false,
20156 /*class_head_p=*/true,
20157 /*is_declaration=*/false);
20158 /* If that didn't work, ignore the nested-name-specifier. */
20159 if (!cp_parser_parse_definitely (parser))
20161 invalid_nested_name_p = true;
20162 type_start_token = cp_lexer_peek_token (parser->lexer);
20163 id = cp_parser_identifier (parser);
20164 if (id == error_mark_node)
20165 id = NULL_TREE;
20167 /* If we could not find a corresponding TYPE, treat this
20168 declaration like an unqualified declaration. */
20169 if (type == error_mark_node)
20170 nested_name_specifier = NULL_TREE;
20171 /* Otherwise, count the number of templates used in TYPE and its
20172 containing scopes. */
20173 else
20175 tree scope;
20177 for (scope = TREE_TYPE (type);
20178 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20179 scope = get_containing_scope (scope))
20180 if (TYPE_P (scope)
20181 && CLASS_TYPE_P (scope)
20182 && CLASSTYPE_TEMPLATE_INFO (scope)
20183 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20184 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20185 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20186 ++num_templates;
20189 /* Otherwise, the identifier is optional. */
20190 else
20192 /* We don't know whether what comes next is a template-id,
20193 an identifier, or nothing at all. */
20194 cp_parser_parse_tentatively (parser);
20195 /* Check for a template-id. */
20196 type_start_token = cp_lexer_peek_token (parser->lexer);
20197 id = cp_parser_template_id (parser,
20198 /*template_keyword_p=*/false,
20199 /*check_dependency_p=*/true,
20200 class_key,
20201 /*is_declaration=*/true);
20202 /* If that didn't work, it could still be an identifier. */
20203 if (!cp_parser_parse_definitely (parser))
20205 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20207 type_start_token = cp_lexer_peek_token (parser->lexer);
20208 id = cp_parser_identifier (parser);
20210 else
20211 id = NULL_TREE;
20213 else
20215 template_id_p = true;
20216 ++num_templates;
20220 pop_deferring_access_checks ();
20222 if (id)
20224 cp_parser_check_for_invalid_template_id (parser, id,
20225 class_key,
20226 type_start_token->location);
20228 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20230 /* If it's not a `:' or a `{' then we can't really be looking at a
20231 class-head, since a class-head only appears as part of a
20232 class-specifier. We have to detect this situation before calling
20233 xref_tag, since that has irreversible side-effects. */
20234 if (!cp_parser_next_token_starts_class_definition_p (parser))
20236 cp_parser_error (parser, "expected %<{%> or %<:%>");
20237 type = error_mark_node;
20238 goto out;
20241 /* At this point, we're going ahead with the class-specifier, even
20242 if some other problem occurs. */
20243 cp_parser_commit_to_tentative_parse (parser);
20244 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20246 cp_parser_error (parser,
20247 "cannot specify %<override%> for a class");
20248 type = error_mark_node;
20249 goto out;
20251 /* Issue the error about the overly-qualified name now. */
20252 if (qualified_p)
20254 cp_parser_error (parser,
20255 "global qualification of class name is invalid");
20256 type = error_mark_node;
20257 goto out;
20259 else if (invalid_nested_name_p)
20261 cp_parser_error (parser,
20262 "qualified name does not name a class");
20263 type = error_mark_node;
20264 goto out;
20266 else if (nested_name_specifier)
20268 tree scope;
20270 /* Reject typedef-names in class heads. */
20271 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20273 error_at (type_start_token->location,
20274 "invalid class name in declaration of %qD",
20275 type);
20276 type = NULL_TREE;
20277 goto done;
20280 /* Figure out in what scope the declaration is being placed. */
20281 scope = current_scope ();
20282 /* If that scope does not contain the scope in which the
20283 class was originally declared, the program is invalid. */
20284 if (scope && !is_ancestor (scope, nested_name_specifier))
20286 if (at_namespace_scope_p ())
20287 error_at (type_start_token->location,
20288 "declaration of %qD in namespace %qD which does not "
20289 "enclose %qD",
20290 type, scope, nested_name_specifier);
20291 else
20292 error_at (type_start_token->location,
20293 "declaration of %qD in %qD which does not enclose %qD",
20294 type, scope, nested_name_specifier);
20295 type = NULL_TREE;
20296 goto done;
20298 /* [dcl.meaning]
20300 A declarator-id shall not be qualified except for the
20301 definition of a ... nested class outside of its class
20302 ... [or] the definition or explicit instantiation of a
20303 class member of a namespace outside of its namespace. */
20304 if (scope == nested_name_specifier)
20306 permerror (nested_name_specifier_token_start->location,
20307 "extra qualification not allowed");
20308 nested_name_specifier = NULL_TREE;
20309 num_templates = 0;
20312 /* An explicit-specialization must be preceded by "template <>". If
20313 it is not, try to recover gracefully. */
20314 if (at_namespace_scope_p ()
20315 && parser->num_template_parameter_lists == 0
20316 && template_id_p)
20318 error_at (type_start_token->location,
20319 "an explicit specialization must be preceded by %<template <>%>");
20320 invalid_explicit_specialization_p = true;
20321 /* Take the same action that would have been taken by
20322 cp_parser_explicit_specialization. */
20323 ++parser->num_template_parameter_lists;
20324 begin_specialization ();
20326 /* There must be no "return" statements between this point and the
20327 end of this function; set "type "to the correct return value and
20328 use "goto done;" to return. */
20329 /* Make sure that the right number of template parameters were
20330 present. */
20331 if (!cp_parser_check_template_parameters (parser, num_templates,
20332 type_start_token->location,
20333 /*declarator=*/NULL))
20335 /* If something went wrong, there is no point in even trying to
20336 process the class-definition. */
20337 type = NULL_TREE;
20338 goto done;
20341 /* Look up the type. */
20342 if (template_id_p)
20344 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20345 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20346 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20348 error_at (type_start_token->location,
20349 "function template %qD redeclared as a class template", id);
20350 type = error_mark_node;
20352 else
20354 type = TREE_TYPE (id);
20355 type = maybe_process_partial_specialization (type);
20357 if (nested_name_specifier)
20358 pushed_scope = push_scope (nested_name_specifier);
20360 else if (nested_name_specifier)
20362 tree class_type;
20364 /* Given:
20366 template <typename T> struct S { struct T };
20367 template <typename T> struct S<T>::T { };
20369 we will get a TYPENAME_TYPE when processing the definition of
20370 `S::T'. We need to resolve it to the actual type before we
20371 try to define it. */
20372 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20374 class_type = resolve_typename_type (TREE_TYPE (type),
20375 /*only_current_p=*/false);
20376 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20377 type = TYPE_NAME (class_type);
20378 else
20380 cp_parser_error (parser, "could not resolve typename type");
20381 type = error_mark_node;
20385 if (maybe_process_partial_specialization (TREE_TYPE (type))
20386 == error_mark_node)
20388 type = NULL_TREE;
20389 goto done;
20392 class_type = current_class_type;
20393 /* Enter the scope indicated by the nested-name-specifier. */
20394 pushed_scope = push_scope (nested_name_specifier);
20395 /* Get the canonical version of this type. */
20396 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20397 /* Call push_template_decl if it seems like we should be defining a
20398 template either from the template headers or the type we're
20399 defining, so that we diagnose both extra and missing headers. */
20400 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20401 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20402 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20404 type = push_template_decl (type);
20405 if (type == error_mark_node)
20407 type = NULL_TREE;
20408 goto done;
20412 type = TREE_TYPE (type);
20413 *nested_name_specifier_p = true;
20415 else /* The name is not a nested name. */
20417 /* If the class was unnamed, create a dummy name. */
20418 if (!id)
20419 id = make_anon_name ();
20420 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20421 parser->num_template_parameter_lists);
20424 /* Indicate whether this class was declared as a `class' or as a
20425 `struct'. */
20426 if (TREE_CODE (type) == RECORD_TYPE)
20427 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20428 cp_parser_check_class_key (class_key, type);
20430 /* If this type was already complete, and we see another definition,
20431 that's an error. */
20432 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20434 error_at (type_start_token->location, "redefinition of %q#T",
20435 type);
20436 error_at (type_start_token->location, "previous definition of %q+#T",
20437 type);
20438 type = NULL_TREE;
20439 goto done;
20441 else if (type == error_mark_node)
20442 type = NULL_TREE;
20444 if (type)
20446 /* Apply attributes now, before any use of the class as a template
20447 argument in its base list. */
20448 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20449 fixup_attribute_variants (type);
20452 /* We will have entered the scope containing the class; the names of
20453 base classes should be looked up in that context. For example:
20455 struct A { struct B {}; struct C; };
20456 struct A::C : B {};
20458 is valid. */
20460 /* Get the list of base-classes, if there is one. */
20461 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20463 /* PR59482: enter the class scope so that base-specifiers are looked
20464 up correctly. */
20465 if (type)
20466 pushclass (type);
20467 bases = cp_parser_base_clause (parser);
20468 /* PR59482: get out of the previously pushed class scope so that the
20469 subsequent pops pop the right thing. */
20470 if (type)
20471 popclass ();
20473 else
20474 bases = NULL_TREE;
20476 /* If we're really defining a class, process the base classes.
20477 If they're invalid, fail. */
20478 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20479 && !xref_basetypes (type, bases))
20480 type = NULL_TREE;
20482 done:
20483 /* Leave the scope given by the nested-name-specifier. We will
20484 enter the class scope itself while processing the members. */
20485 if (pushed_scope)
20486 pop_scope (pushed_scope);
20488 if (invalid_explicit_specialization_p)
20490 end_specialization ();
20491 --parser->num_template_parameter_lists;
20494 if (type)
20495 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20496 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20497 CLASSTYPE_FINAL (type) = 1;
20498 out:
20499 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20500 return type;
20503 /* Parse a class-key.
20505 class-key:
20506 class
20507 struct
20508 union
20510 Returns the kind of class-key specified, or none_type to indicate
20511 error. */
20513 static enum tag_types
20514 cp_parser_class_key (cp_parser* parser)
20516 cp_token *token;
20517 enum tag_types tag_type;
20519 /* Look for the class-key. */
20520 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20521 if (!token)
20522 return none_type;
20524 /* Check to see if the TOKEN is a class-key. */
20525 tag_type = cp_parser_token_is_class_key (token);
20526 if (!tag_type)
20527 cp_parser_error (parser, "expected class-key");
20528 return tag_type;
20531 /* Parse a type-parameter-key.
20533 type-parameter-key:
20534 class
20535 typename
20538 static void
20539 cp_parser_type_parameter_key (cp_parser* parser)
20541 /* Look for the type-parameter-key. */
20542 enum tag_types tag_type = none_type;
20543 cp_token *token = cp_lexer_peek_token (parser->lexer);
20544 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20546 cp_lexer_consume_token (parser->lexer);
20547 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20548 /* typename is not allowed in a template template parameter
20549 by the standard until C++1Z. */
20550 pedwarn (token->location, OPT_Wpedantic,
20551 "ISO C++ forbids typename key in template template parameter;"
20552 " use -std=c++1z or -std=gnu++1z");
20554 else
20555 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20557 return;
20560 /* Parse an (optional) member-specification.
20562 member-specification:
20563 member-declaration member-specification [opt]
20564 access-specifier : member-specification [opt] */
20566 static void
20567 cp_parser_member_specification_opt (cp_parser* parser)
20569 while (true)
20571 cp_token *token;
20572 enum rid keyword;
20574 /* Peek at the next token. */
20575 token = cp_lexer_peek_token (parser->lexer);
20576 /* If it's a `}', or EOF then we've seen all the members. */
20577 if (token->type == CPP_CLOSE_BRACE
20578 || token->type == CPP_EOF
20579 || token->type == CPP_PRAGMA_EOL)
20580 break;
20582 /* See if this token is a keyword. */
20583 keyword = token->keyword;
20584 switch (keyword)
20586 case RID_PUBLIC:
20587 case RID_PROTECTED:
20588 case RID_PRIVATE:
20589 /* Consume the access-specifier. */
20590 cp_lexer_consume_token (parser->lexer);
20591 /* Remember which access-specifier is active. */
20592 current_access_specifier = token->u.value;
20593 /* Look for the `:'. */
20594 cp_parser_require (parser, CPP_COLON, RT_COLON);
20595 break;
20597 default:
20598 /* Accept #pragmas at class scope. */
20599 if (token->type == CPP_PRAGMA)
20601 cp_parser_pragma (parser, pragma_member);
20602 break;
20605 /* Otherwise, the next construction must be a
20606 member-declaration. */
20607 cp_parser_member_declaration (parser);
20612 /* Parse a member-declaration.
20614 member-declaration:
20615 decl-specifier-seq [opt] member-declarator-list [opt] ;
20616 function-definition ; [opt]
20617 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20618 using-declaration
20619 template-declaration
20620 alias-declaration
20622 member-declarator-list:
20623 member-declarator
20624 member-declarator-list , member-declarator
20626 member-declarator:
20627 declarator pure-specifier [opt]
20628 declarator constant-initializer [opt]
20629 identifier [opt] : constant-expression
20631 GNU Extensions:
20633 member-declaration:
20634 __extension__ member-declaration
20636 member-declarator:
20637 declarator attributes [opt] pure-specifier [opt]
20638 declarator attributes [opt] constant-initializer [opt]
20639 identifier [opt] attributes [opt] : constant-expression
20641 C++0x Extensions:
20643 member-declaration:
20644 static_assert-declaration */
20646 static void
20647 cp_parser_member_declaration (cp_parser* parser)
20649 cp_decl_specifier_seq decl_specifiers;
20650 tree prefix_attributes;
20651 tree decl;
20652 int declares_class_or_enum;
20653 bool friend_p;
20654 cp_token *token = NULL;
20655 cp_token *decl_spec_token_start = NULL;
20656 cp_token *initializer_token_start = NULL;
20657 int saved_pedantic;
20658 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20660 /* Check for the `__extension__' keyword. */
20661 if (cp_parser_extension_opt (parser, &saved_pedantic))
20663 /* Recurse. */
20664 cp_parser_member_declaration (parser);
20665 /* Restore the old value of the PEDANTIC flag. */
20666 pedantic = saved_pedantic;
20668 return;
20671 /* Check for a template-declaration. */
20672 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20674 /* An explicit specialization here is an error condition, and we
20675 expect the specialization handler to detect and report this. */
20676 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20677 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20678 cp_parser_explicit_specialization (parser);
20679 else
20680 cp_parser_template_declaration (parser, /*member_p=*/true);
20682 return;
20685 /* Check for a using-declaration. */
20686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20688 if (cxx_dialect < cxx11)
20690 /* Parse the using-declaration. */
20691 cp_parser_using_declaration (parser,
20692 /*access_declaration_p=*/false);
20693 return;
20695 else
20697 tree decl;
20698 bool alias_decl_expected;
20699 cp_parser_parse_tentatively (parser);
20700 decl = cp_parser_alias_declaration (parser);
20701 /* Note that if we actually see the '=' token after the
20702 identifier, cp_parser_alias_declaration commits the
20703 tentative parse. In that case, we really expects an
20704 alias-declaration. Otherwise, we expect a using
20705 declaration. */
20706 alias_decl_expected =
20707 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20708 cp_parser_parse_definitely (parser);
20710 if (alias_decl_expected)
20711 finish_member_declaration (decl);
20712 else
20713 cp_parser_using_declaration (parser,
20714 /*access_declaration_p=*/false);
20715 return;
20719 /* Check for @defs. */
20720 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20722 tree ivar, member;
20723 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20724 ivar = ivar_chains;
20725 while (ivar)
20727 member = ivar;
20728 ivar = TREE_CHAIN (member);
20729 TREE_CHAIN (member) = NULL_TREE;
20730 finish_member_declaration (member);
20732 return;
20735 /* If the next token is `static_assert' we have a static assertion. */
20736 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20738 cp_parser_static_assert (parser, /*member_p=*/true);
20739 return;
20742 parser->colon_corrects_to_scope_p = false;
20744 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20745 goto out;
20747 /* Parse the decl-specifier-seq. */
20748 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20749 cp_parser_decl_specifier_seq (parser,
20750 CP_PARSER_FLAGS_OPTIONAL,
20751 &decl_specifiers,
20752 &declares_class_or_enum);
20753 /* Check for an invalid type-name. */
20754 if (!decl_specifiers.any_type_specifiers_p
20755 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20756 goto out;
20757 /* If there is no declarator, then the decl-specifier-seq should
20758 specify a type. */
20759 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20761 /* If there was no decl-specifier-seq, and the next token is a
20762 `;', then we have something like:
20764 struct S { ; };
20766 [class.mem]
20768 Each member-declaration shall declare at least one member
20769 name of the class. */
20770 if (!decl_specifiers.any_specifiers_p)
20772 cp_token *token = cp_lexer_peek_token (parser->lexer);
20773 if (!in_system_header_at (token->location))
20774 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20776 else
20778 tree type;
20780 /* See if this declaration is a friend. */
20781 friend_p = cp_parser_friend_p (&decl_specifiers);
20782 /* If there were decl-specifiers, check to see if there was
20783 a class-declaration. */
20784 type = check_tag_decl (&decl_specifiers,
20785 /*explicit_type_instantiation_p=*/false);
20786 /* Nested classes have already been added to the class, but
20787 a `friend' needs to be explicitly registered. */
20788 if (friend_p)
20790 /* If the `friend' keyword was present, the friend must
20791 be introduced with a class-key. */
20792 if (!declares_class_or_enum && cxx_dialect < cxx11)
20793 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20794 "in C++03 a class-key must be used "
20795 "when declaring a friend");
20796 /* In this case:
20798 template <typename T> struct A {
20799 friend struct A<T>::B;
20802 A<T>::B will be represented by a TYPENAME_TYPE, and
20803 therefore not recognized by check_tag_decl. */
20804 if (!type)
20806 type = decl_specifiers.type;
20807 if (type && TREE_CODE (type) == TYPE_DECL)
20808 type = TREE_TYPE (type);
20810 if (!type || !TYPE_P (type))
20811 error_at (decl_spec_token_start->location,
20812 "friend declaration does not name a class or "
20813 "function");
20814 else
20815 make_friend_class (current_class_type, type,
20816 /*complain=*/true);
20818 /* If there is no TYPE, an error message will already have
20819 been issued. */
20820 else if (!type || type == error_mark_node)
20822 /* An anonymous aggregate has to be handled specially; such
20823 a declaration really declares a data member (with a
20824 particular type), as opposed to a nested class. */
20825 else if (ANON_AGGR_TYPE_P (type))
20827 /* C++11 9.5/6. */
20828 if (decl_specifiers.storage_class != sc_none)
20829 error_at (decl_spec_token_start->location,
20830 "a storage class on an anonymous aggregate "
20831 "in class scope is not allowed");
20833 /* Remove constructors and such from TYPE, now that we
20834 know it is an anonymous aggregate. */
20835 fixup_anonymous_aggr (type);
20836 /* And make the corresponding data member. */
20837 decl = build_decl (decl_spec_token_start->location,
20838 FIELD_DECL, NULL_TREE, type);
20839 /* Add it to the class. */
20840 finish_member_declaration (decl);
20842 else
20843 cp_parser_check_access_in_redeclaration
20844 (TYPE_NAME (type),
20845 decl_spec_token_start->location);
20848 else
20850 bool assume_semicolon = false;
20852 /* Clear attributes from the decl_specifiers but keep them
20853 around as prefix attributes that apply them to the entity
20854 being declared. */
20855 prefix_attributes = decl_specifiers.attributes;
20856 decl_specifiers.attributes = NULL_TREE;
20858 /* See if these declarations will be friends. */
20859 friend_p = cp_parser_friend_p (&decl_specifiers);
20861 /* Keep going until we hit the `;' at the end of the
20862 declaration. */
20863 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20865 tree attributes = NULL_TREE;
20866 tree first_attribute;
20868 /* Peek at the next token. */
20869 token = cp_lexer_peek_token (parser->lexer);
20871 /* Check for a bitfield declaration. */
20872 if (token->type == CPP_COLON
20873 || (token->type == CPP_NAME
20874 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20875 == CPP_COLON))
20877 tree identifier;
20878 tree width;
20880 /* Get the name of the bitfield. Note that we cannot just
20881 check TOKEN here because it may have been invalidated by
20882 the call to cp_lexer_peek_nth_token above. */
20883 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20884 identifier = cp_parser_identifier (parser);
20885 else
20886 identifier = NULL_TREE;
20888 /* Consume the `:' token. */
20889 cp_lexer_consume_token (parser->lexer);
20890 /* Get the width of the bitfield. */
20891 width
20892 = cp_parser_constant_expression (parser);
20894 /* Look for attributes that apply to the bitfield. */
20895 attributes = cp_parser_attributes_opt (parser);
20896 /* Remember which attributes are prefix attributes and
20897 which are not. */
20898 first_attribute = attributes;
20899 /* Combine the attributes. */
20900 attributes = chainon (prefix_attributes, attributes);
20902 /* Create the bitfield declaration. */
20903 decl = grokbitfield (identifier
20904 ? make_id_declarator (NULL_TREE,
20905 identifier,
20906 sfk_none)
20907 : NULL,
20908 &decl_specifiers,
20909 width,
20910 attributes);
20912 else
20914 cp_declarator *declarator;
20915 tree initializer;
20916 tree asm_specification;
20917 int ctor_dtor_or_conv_p;
20919 /* Parse the declarator. */
20920 declarator
20921 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20922 &ctor_dtor_or_conv_p,
20923 /*parenthesized_p=*/NULL,
20924 /*member_p=*/true,
20925 friend_p);
20927 /* If something went wrong parsing the declarator, make sure
20928 that we at least consume some tokens. */
20929 if (declarator == cp_error_declarator)
20931 /* Skip to the end of the statement. */
20932 cp_parser_skip_to_end_of_statement (parser);
20933 /* If the next token is not a semicolon, that is
20934 probably because we just skipped over the body of
20935 a function. So, we consume a semicolon if
20936 present, but do not issue an error message if it
20937 is not present. */
20938 if (cp_lexer_next_token_is (parser->lexer,
20939 CPP_SEMICOLON))
20940 cp_lexer_consume_token (parser->lexer);
20941 goto out;
20944 if (declares_class_or_enum & 2)
20945 cp_parser_check_for_definition_in_return_type
20946 (declarator, decl_specifiers.type,
20947 decl_specifiers.locations[ds_type_spec]);
20949 /* Look for an asm-specification. */
20950 asm_specification = cp_parser_asm_specification_opt (parser);
20951 /* Look for attributes that apply to the declaration. */
20952 attributes = cp_parser_attributes_opt (parser);
20953 /* Remember which attributes are prefix attributes and
20954 which are not. */
20955 first_attribute = attributes;
20956 /* Combine the attributes. */
20957 attributes = chainon (prefix_attributes, attributes);
20959 /* If it's an `=', then we have a constant-initializer or a
20960 pure-specifier. It is not correct to parse the
20961 initializer before registering the member declaration
20962 since the member declaration should be in scope while
20963 its initializer is processed. However, the rest of the
20964 front end does not yet provide an interface that allows
20965 us to handle this correctly. */
20966 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20968 /* In [class.mem]:
20970 A pure-specifier shall be used only in the declaration of
20971 a virtual function.
20973 A member-declarator can contain a constant-initializer
20974 only if it declares a static member of integral or
20975 enumeration type.
20977 Therefore, if the DECLARATOR is for a function, we look
20978 for a pure-specifier; otherwise, we look for a
20979 constant-initializer. When we call `grokfield', it will
20980 perform more stringent semantics checks. */
20981 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20982 if (function_declarator_p (declarator)
20983 || (decl_specifiers.type
20984 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20985 && declarator->kind == cdk_id
20986 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20987 == FUNCTION_TYPE)))
20988 initializer = cp_parser_pure_specifier (parser);
20989 else if (decl_specifiers.storage_class != sc_static)
20990 initializer = cp_parser_save_nsdmi (parser);
20991 else if (cxx_dialect >= cxx11)
20993 bool nonconst;
20994 /* Don't require a constant rvalue in C++11, since we
20995 might want a reference constant. We'll enforce
20996 constancy later. */
20997 cp_lexer_consume_token (parser->lexer);
20998 /* Parse the initializer. */
20999 initializer = cp_parser_initializer_clause (parser,
21000 &nonconst);
21002 else
21003 /* Parse the initializer. */
21004 initializer = cp_parser_constant_initializer (parser);
21006 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21007 && !function_declarator_p (declarator))
21009 bool x;
21010 if (decl_specifiers.storage_class != sc_static)
21011 initializer = cp_parser_save_nsdmi (parser);
21012 else
21013 initializer = cp_parser_initializer (parser, &x, &x);
21015 /* Otherwise, there is no initializer. */
21016 else
21017 initializer = NULL_TREE;
21019 /* See if we are probably looking at a function
21020 definition. We are certainly not looking at a
21021 member-declarator. Calling `grokfield' has
21022 side-effects, so we must not do it unless we are sure
21023 that we are looking at a member-declarator. */
21024 if (cp_parser_token_starts_function_definition_p
21025 (cp_lexer_peek_token (parser->lexer)))
21027 /* The grammar does not allow a pure-specifier to be
21028 used when a member function is defined. (It is
21029 possible that this fact is an oversight in the
21030 standard, since a pure function may be defined
21031 outside of the class-specifier. */
21032 if (initializer && initializer_token_start)
21033 error_at (initializer_token_start->location,
21034 "pure-specifier on function-definition");
21035 decl = cp_parser_save_member_function_body (parser,
21036 &decl_specifiers,
21037 declarator,
21038 attributes);
21039 if (parser->fully_implicit_function_template_p)
21040 decl = finish_fully_implicit_template (parser, decl);
21041 /* If the member was not a friend, declare it here. */
21042 if (!friend_p)
21043 finish_member_declaration (decl);
21044 /* Peek at the next token. */
21045 token = cp_lexer_peek_token (parser->lexer);
21046 /* If the next token is a semicolon, consume it. */
21047 if (token->type == CPP_SEMICOLON)
21048 cp_lexer_consume_token (parser->lexer);
21049 goto out;
21051 else
21052 if (declarator->kind == cdk_function)
21053 declarator->id_loc = token->location;
21054 /* Create the declaration. */
21055 decl = grokfield (declarator, &decl_specifiers,
21056 initializer, /*init_const_expr_p=*/true,
21057 asm_specification, attributes);
21058 if (parser->fully_implicit_function_template_p)
21060 if (friend_p)
21061 finish_fully_implicit_template (parser, 0);
21062 else
21063 decl = finish_fully_implicit_template (parser, decl);
21067 cp_finalize_omp_declare_simd (parser, decl);
21069 /* Reset PREFIX_ATTRIBUTES. */
21070 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21071 attributes = TREE_CHAIN (attributes);
21072 if (attributes)
21073 TREE_CHAIN (attributes) = NULL_TREE;
21075 /* If there is any qualification still in effect, clear it
21076 now; we will be starting fresh with the next declarator. */
21077 parser->scope = NULL_TREE;
21078 parser->qualifying_scope = NULL_TREE;
21079 parser->object_scope = NULL_TREE;
21080 /* If it's a `,', then there are more declarators. */
21081 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21083 cp_lexer_consume_token (parser->lexer);
21084 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21086 cp_token *token = cp_lexer_previous_token (parser->lexer);
21087 error_at (token->location,
21088 "stray %<,%> at end of member declaration");
21091 /* If the next token isn't a `;', then we have a parse error. */
21092 else if (cp_lexer_next_token_is_not (parser->lexer,
21093 CPP_SEMICOLON))
21095 /* The next token might be a ways away from where the
21096 actual semicolon is missing. Find the previous token
21097 and use that for our error position. */
21098 cp_token *token = cp_lexer_previous_token (parser->lexer);
21099 error_at (token->location,
21100 "expected %<;%> at end of member declaration");
21102 /* Assume that the user meant to provide a semicolon. If
21103 we were to cp_parser_skip_to_end_of_statement, we might
21104 skip to a semicolon inside a member function definition
21105 and issue nonsensical error messages. */
21106 assume_semicolon = true;
21109 if (decl)
21111 /* Add DECL to the list of members. */
21112 if (!friend_p
21113 /* Explicitly include, eg, NSDMIs, for better error
21114 recovery (c++/58650). */
21115 || !DECL_DECLARES_FUNCTION_P (decl))
21116 finish_member_declaration (decl);
21118 if (TREE_CODE (decl) == FUNCTION_DECL)
21119 cp_parser_save_default_args (parser, decl);
21120 else if (TREE_CODE (decl) == FIELD_DECL
21121 && !DECL_C_BIT_FIELD (decl)
21122 && DECL_INITIAL (decl))
21123 /* Add DECL to the queue of NSDMI to be parsed later. */
21124 vec_safe_push (unparsed_nsdmis, decl);
21127 if (assume_semicolon)
21128 goto out;
21132 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21133 out:
21134 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21137 /* Parse a pure-specifier.
21139 pure-specifier:
21142 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21143 Otherwise, ERROR_MARK_NODE is returned. */
21145 static tree
21146 cp_parser_pure_specifier (cp_parser* parser)
21148 cp_token *token;
21150 /* Look for the `=' token. */
21151 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21152 return error_mark_node;
21153 /* Look for the `0' token. */
21154 token = cp_lexer_peek_token (parser->lexer);
21156 if (token->type == CPP_EOF
21157 || token->type == CPP_PRAGMA_EOL)
21158 return error_mark_node;
21160 cp_lexer_consume_token (parser->lexer);
21162 /* Accept = default or = delete in c++0x mode. */
21163 if (token->keyword == RID_DEFAULT
21164 || token->keyword == RID_DELETE)
21166 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21167 return token->u.value;
21170 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21171 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21173 cp_parser_error (parser,
21174 "invalid pure specifier (only %<= 0%> is allowed)");
21175 cp_parser_skip_to_end_of_statement (parser);
21176 return error_mark_node;
21178 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21180 error_at (token->location, "templates may not be %<virtual%>");
21181 return error_mark_node;
21184 return integer_zero_node;
21187 /* Parse a constant-initializer.
21189 constant-initializer:
21190 = constant-expression
21192 Returns a representation of the constant-expression. */
21194 static tree
21195 cp_parser_constant_initializer (cp_parser* parser)
21197 /* Look for the `=' token. */
21198 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21199 return error_mark_node;
21201 /* It is invalid to write:
21203 struct S { static const int i = { 7 }; };
21206 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21208 cp_parser_error (parser,
21209 "a brace-enclosed initializer is not allowed here");
21210 /* Consume the opening brace. */
21211 cp_lexer_consume_token (parser->lexer);
21212 /* Skip the initializer. */
21213 cp_parser_skip_to_closing_brace (parser);
21214 /* Look for the trailing `}'. */
21215 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21217 return error_mark_node;
21220 return cp_parser_constant_expression (parser);
21223 /* Derived classes [gram.class.derived] */
21225 /* Parse a base-clause.
21227 base-clause:
21228 : base-specifier-list
21230 base-specifier-list:
21231 base-specifier ... [opt]
21232 base-specifier-list , base-specifier ... [opt]
21234 Returns a TREE_LIST representing the base-classes, in the order in
21235 which they were declared. The representation of each node is as
21236 described by cp_parser_base_specifier.
21238 In the case that no bases are specified, this function will return
21239 NULL_TREE, not ERROR_MARK_NODE. */
21241 static tree
21242 cp_parser_base_clause (cp_parser* parser)
21244 tree bases = NULL_TREE;
21246 /* Look for the `:' that begins the list. */
21247 cp_parser_require (parser, CPP_COLON, RT_COLON);
21249 /* Scan the base-specifier-list. */
21250 while (true)
21252 cp_token *token;
21253 tree base;
21254 bool pack_expansion_p = false;
21256 /* Look for the base-specifier. */
21257 base = cp_parser_base_specifier (parser);
21258 /* Look for the (optional) ellipsis. */
21259 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21261 /* Consume the `...'. */
21262 cp_lexer_consume_token (parser->lexer);
21264 pack_expansion_p = true;
21267 /* Add BASE to the front of the list. */
21268 if (base && base != error_mark_node)
21270 if (pack_expansion_p)
21271 /* Make this a pack expansion type. */
21272 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21274 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21276 TREE_CHAIN (base) = bases;
21277 bases = base;
21280 /* Peek at the next token. */
21281 token = cp_lexer_peek_token (parser->lexer);
21282 /* If it's not a comma, then the list is complete. */
21283 if (token->type != CPP_COMMA)
21284 break;
21285 /* Consume the `,'. */
21286 cp_lexer_consume_token (parser->lexer);
21289 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21290 base class had a qualified name. However, the next name that
21291 appears is certainly not qualified. */
21292 parser->scope = NULL_TREE;
21293 parser->qualifying_scope = NULL_TREE;
21294 parser->object_scope = NULL_TREE;
21296 return nreverse (bases);
21299 /* Parse a base-specifier.
21301 base-specifier:
21302 :: [opt] nested-name-specifier [opt] class-name
21303 virtual access-specifier [opt] :: [opt] nested-name-specifier
21304 [opt] class-name
21305 access-specifier virtual [opt] :: [opt] nested-name-specifier
21306 [opt] class-name
21308 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21309 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21310 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21311 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21313 static tree
21314 cp_parser_base_specifier (cp_parser* parser)
21316 cp_token *token;
21317 bool done = false;
21318 bool virtual_p = false;
21319 bool duplicate_virtual_error_issued_p = false;
21320 bool duplicate_access_error_issued_p = false;
21321 bool class_scope_p, template_p;
21322 tree access = access_default_node;
21323 tree type;
21325 /* Process the optional `virtual' and `access-specifier'. */
21326 while (!done)
21328 /* Peek at the next token. */
21329 token = cp_lexer_peek_token (parser->lexer);
21330 /* Process `virtual'. */
21331 switch (token->keyword)
21333 case RID_VIRTUAL:
21334 /* If `virtual' appears more than once, issue an error. */
21335 if (virtual_p && !duplicate_virtual_error_issued_p)
21337 cp_parser_error (parser,
21338 "%<virtual%> specified more than once in base-specified");
21339 duplicate_virtual_error_issued_p = true;
21342 virtual_p = true;
21344 /* Consume the `virtual' token. */
21345 cp_lexer_consume_token (parser->lexer);
21347 break;
21349 case RID_PUBLIC:
21350 case RID_PROTECTED:
21351 case RID_PRIVATE:
21352 /* If more than one access specifier appears, issue an
21353 error. */
21354 if (access != access_default_node
21355 && !duplicate_access_error_issued_p)
21357 cp_parser_error (parser,
21358 "more than one access specifier in base-specified");
21359 duplicate_access_error_issued_p = true;
21362 access = ridpointers[(int) token->keyword];
21364 /* Consume the access-specifier. */
21365 cp_lexer_consume_token (parser->lexer);
21367 break;
21369 default:
21370 done = true;
21371 break;
21374 /* It is not uncommon to see programs mechanically, erroneously, use
21375 the 'typename' keyword to denote (dependent) qualified types
21376 as base classes. */
21377 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21379 token = cp_lexer_peek_token (parser->lexer);
21380 if (!processing_template_decl)
21381 error_at (token->location,
21382 "keyword %<typename%> not allowed outside of templates");
21383 else
21384 error_at (token->location,
21385 "keyword %<typename%> not allowed in this context "
21386 "(the base class is implicitly a type)");
21387 cp_lexer_consume_token (parser->lexer);
21390 /* Look for the optional `::' operator. */
21391 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21392 /* Look for the nested-name-specifier. The simplest way to
21393 implement:
21395 [temp.res]
21397 The keyword `typename' is not permitted in a base-specifier or
21398 mem-initializer; in these contexts a qualified name that
21399 depends on a template-parameter is implicitly assumed to be a
21400 type name.
21402 is to pretend that we have seen the `typename' keyword at this
21403 point. */
21404 cp_parser_nested_name_specifier_opt (parser,
21405 /*typename_keyword_p=*/true,
21406 /*check_dependency_p=*/true,
21407 typename_type,
21408 /*is_declaration=*/true);
21409 /* If the base class is given by a qualified name, assume that names
21410 we see are type names or templates, as appropriate. */
21411 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21412 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21414 if (!parser->scope
21415 && cp_lexer_next_token_is_decltype (parser->lexer))
21416 /* DR 950 allows decltype as a base-specifier. */
21417 type = cp_parser_decltype (parser);
21418 else
21420 /* Otherwise, look for the class-name. */
21421 type = cp_parser_class_name (parser,
21422 class_scope_p,
21423 template_p,
21424 typename_type,
21425 /*check_dependency_p=*/true,
21426 /*class_head_p=*/false,
21427 /*is_declaration=*/true);
21428 type = TREE_TYPE (type);
21431 if (type == error_mark_node)
21432 return error_mark_node;
21434 return finish_base_specifier (type, access, virtual_p);
21437 /* Exception handling [gram.exception] */
21439 /* Parse an (optional) noexcept-specification.
21441 noexcept-specification:
21442 noexcept ( constant-expression ) [opt]
21444 If no noexcept-specification is present, returns NULL_TREE.
21445 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21446 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21447 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21448 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21449 in which case a boolean condition is returned instead. */
21451 static tree
21452 cp_parser_noexcept_specification_opt (cp_parser* parser,
21453 bool require_constexpr,
21454 bool* consumed_expr,
21455 bool return_cond)
21457 cp_token *token;
21458 const char *saved_message;
21460 /* Peek at the next token. */
21461 token = cp_lexer_peek_token (parser->lexer);
21463 /* Is it a noexcept-specification? */
21464 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21466 tree expr;
21467 cp_lexer_consume_token (parser->lexer);
21469 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21471 cp_lexer_consume_token (parser->lexer);
21473 if (require_constexpr)
21475 /* Types may not be defined in an exception-specification. */
21476 saved_message = parser->type_definition_forbidden_message;
21477 parser->type_definition_forbidden_message
21478 = G_("types may not be defined in an exception-specification");
21480 expr = cp_parser_constant_expression (parser);
21482 /* Restore the saved message. */
21483 parser->type_definition_forbidden_message = saved_message;
21485 else
21487 expr = cp_parser_expression (parser);
21488 *consumed_expr = true;
21491 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21493 else
21495 expr = boolean_true_node;
21496 if (!require_constexpr)
21497 *consumed_expr = false;
21500 /* We cannot build a noexcept-spec right away because this will check
21501 that expr is a constexpr. */
21502 if (!return_cond)
21503 return build_noexcept_spec (expr, tf_warning_or_error);
21504 else
21505 return expr;
21507 else
21508 return NULL_TREE;
21511 /* Parse an (optional) exception-specification.
21513 exception-specification:
21514 throw ( type-id-list [opt] )
21516 Returns a TREE_LIST representing the exception-specification. The
21517 TREE_VALUE of each node is a type. */
21519 static tree
21520 cp_parser_exception_specification_opt (cp_parser* parser)
21522 cp_token *token;
21523 tree type_id_list;
21524 const char *saved_message;
21526 /* Peek at the next token. */
21527 token = cp_lexer_peek_token (parser->lexer);
21529 /* Is it a noexcept-specification? */
21530 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21531 false);
21532 if (type_id_list != NULL_TREE)
21533 return type_id_list;
21535 /* If it's not `throw', then there's no exception-specification. */
21536 if (!cp_parser_is_keyword (token, RID_THROW))
21537 return NULL_TREE;
21539 #if 0
21540 /* Enable this once a lot of code has transitioned to noexcept? */
21541 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21542 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21543 "deprecated in C++0x; use %<noexcept%> instead");
21544 #endif
21546 /* Consume the `throw'. */
21547 cp_lexer_consume_token (parser->lexer);
21549 /* Look for the `('. */
21550 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21552 /* Peek at the next token. */
21553 token = cp_lexer_peek_token (parser->lexer);
21554 /* If it's not a `)', then there is a type-id-list. */
21555 if (token->type != CPP_CLOSE_PAREN)
21557 /* Types may not be defined in an exception-specification. */
21558 saved_message = parser->type_definition_forbidden_message;
21559 parser->type_definition_forbidden_message
21560 = G_("types may not be defined in an exception-specification");
21561 /* Parse the type-id-list. */
21562 type_id_list = cp_parser_type_id_list (parser);
21563 /* Restore the saved message. */
21564 parser->type_definition_forbidden_message = saved_message;
21566 else
21567 type_id_list = empty_except_spec;
21569 /* Look for the `)'. */
21570 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21572 return type_id_list;
21575 /* Parse an (optional) type-id-list.
21577 type-id-list:
21578 type-id ... [opt]
21579 type-id-list , type-id ... [opt]
21581 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21582 in the order that the types were presented. */
21584 static tree
21585 cp_parser_type_id_list (cp_parser* parser)
21587 tree types = NULL_TREE;
21589 while (true)
21591 cp_token *token;
21592 tree type;
21594 /* Get the next type-id. */
21595 type = cp_parser_type_id (parser);
21596 /* Parse the optional ellipsis. */
21597 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21599 /* Consume the `...'. */
21600 cp_lexer_consume_token (parser->lexer);
21602 /* Turn the type into a pack expansion expression. */
21603 type = make_pack_expansion (type);
21605 /* Add it to the list. */
21606 types = add_exception_specifier (types, type, /*complain=*/1);
21607 /* Peek at the next token. */
21608 token = cp_lexer_peek_token (parser->lexer);
21609 /* If it is not a `,', we are done. */
21610 if (token->type != CPP_COMMA)
21611 break;
21612 /* Consume the `,'. */
21613 cp_lexer_consume_token (parser->lexer);
21616 return nreverse (types);
21619 /* Parse a try-block.
21621 try-block:
21622 try compound-statement handler-seq */
21624 static tree
21625 cp_parser_try_block (cp_parser* parser)
21627 tree try_block;
21629 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21630 if (parser->in_function_body
21631 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21632 error ("%<try%> in %<constexpr%> function");
21634 try_block = begin_try_block ();
21635 cp_parser_compound_statement (parser, NULL, true, false);
21636 finish_try_block (try_block);
21637 cp_parser_handler_seq (parser);
21638 finish_handler_sequence (try_block);
21640 return try_block;
21643 /* Parse a function-try-block.
21645 function-try-block:
21646 try ctor-initializer [opt] function-body handler-seq */
21648 static bool
21649 cp_parser_function_try_block (cp_parser* parser)
21651 tree compound_stmt;
21652 tree try_block;
21653 bool ctor_initializer_p;
21655 /* Look for the `try' keyword. */
21656 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21657 return false;
21658 /* Let the rest of the front end know where we are. */
21659 try_block = begin_function_try_block (&compound_stmt);
21660 /* Parse the function-body. */
21661 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21662 (parser, /*in_function_try_block=*/true);
21663 /* We're done with the `try' part. */
21664 finish_function_try_block (try_block);
21665 /* Parse the handlers. */
21666 cp_parser_handler_seq (parser);
21667 /* We're done with the handlers. */
21668 finish_function_handler_sequence (try_block, compound_stmt);
21670 return ctor_initializer_p;
21673 /* Parse a handler-seq.
21675 handler-seq:
21676 handler handler-seq [opt] */
21678 static void
21679 cp_parser_handler_seq (cp_parser* parser)
21681 while (true)
21683 cp_token *token;
21685 /* Parse the handler. */
21686 cp_parser_handler (parser);
21687 /* Peek at the next token. */
21688 token = cp_lexer_peek_token (parser->lexer);
21689 /* If it's not `catch' then there are no more handlers. */
21690 if (!cp_parser_is_keyword (token, RID_CATCH))
21691 break;
21695 /* Parse a handler.
21697 handler:
21698 catch ( exception-declaration ) compound-statement */
21700 static void
21701 cp_parser_handler (cp_parser* parser)
21703 tree handler;
21704 tree declaration;
21706 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21707 handler = begin_handler ();
21708 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21709 declaration = cp_parser_exception_declaration (parser);
21710 finish_handler_parms (declaration, handler);
21711 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21712 cp_parser_compound_statement (parser, NULL, false, false);
21713 finish_handler (handler);
21716 /* Parse an exception-declaration.
21718 exception-declaration:
21719 type-specifier-seq declarator
21720 type-specifier-seq abstract-declarator
21721 type-specifier-seq
21724 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21725 ellipsis variant is used. */
21727 static tree
21728 cp_parser_exception_declaration (cp_parser* parser)
21730 cp_decl_specifier_seq type_specifiers;
21731 cp_declarator *declarator;
21732 const char *saved_message;
21734 /* If it's an ellipsis, it's easy to handle. */
21735 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21737 /* Consume the `...' token. */
21738 cp_lexer_consume_token (parser->lexer);
21739 return NULL_TREE;
21742 /* Types may not be defined in exception-declarations. */
21743 saved_message = parser->type_definition_forbidden_message;
21744 parser->type_definition_forbidden_message
21745 = G_("types may not be defined in exception-declarations");
21747 /* Parse the type-specifier-seq. */
21748 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21749 /*is_trailing_return=*/false,
21750 &type_specifiers);
21751 /* If it's a `)', then there is no declarator. */
21752 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21753 declarator = NULL;
21754 else
21755 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21756 /*ctor_dtor_or_conv_p=*/NULL,
21757 /*parenthesized_p=*/NULL,
21758 /*member_p=*/false,
21759 /*friend_p=*/false);
21761 /* Restore the saved message. */
21762 parser->type_definition_forbidden_message = saved_message;
21764 if (!type_specifiers.any_specifiers_p)
21765 return error_mark_node;
21767 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21770 /* Parse a throw-expression.
21772 throw-expression:
21773 throw assignment-expression [opt]
21775 Returns a THROW_EXPR representing the throw-expression. */
21777 static tree
21778 cp_parser_throw_expression (cp_parser* parser)
21780 tree expression;
21781 cp_token* token;
21783 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21784 token = cp_lexer_peek_token (parser->lexer);
21785 /* Figure out whether or not there is an assignment-expression
21786 following the "throw" keyword. */
21787 if (token->type == CPP_COMMA
21788 || token->type == CPP_SEMICOLON
21789 || token->type == CPP_CLOSE_PAREN
21790 || token->type == CPP_CLOSE_SQUARE
21791 || token->type == CPP_CLOSE_BRACE
21792 || token->type == CPP_COLON)
21793 expression = NULL_TREE;
21794 else
21795 expression = cp_parser_assignment_expression (parser);
21797 return build_throw (expression);
21800 /* GNU Extensions */
21802 /* Parse an (optional) asm-specification.
21804 asm-specification:
21805 asm ( string-literal )
21807 If the asm-specification is present, returns a STRING_CST
21808 corresponding to the string-literal. Otherwise, returns
21809 NULL_TREE. */
21811 static tree
21812 cp_parser_asm_specification_opt (cp_parser* parser)
21814 cp_token *token;
21815 tree asm_specification;
21817 /* Peek at the next token. */
21818 token = cp_lexer_peek_token (parser->lexer);
21819 /* If the next token isn't the `asm' keyword, then there's no
21820 asm-specification. */
21821 if (!cp_parser_is_keyword (token, RID_ASM))
21822 return NULL_TREE;
21824 /* Consume the `asm' token. */
21825 cp_lexer_consume_token (parser->lexer);
21826 /* Look for the `('. */
21827 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21829 /* Look for the string-literal. */
21830 asm_specification = cp_parser_string_literal (parser, false, false);
21832 /* Look for the `)'. */
21833 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21835 return asm_specification;
21838 /* Parse an asm-operand-list.
21840 asm-operand-list:
21841 asm-operand
21842 asm-operand-list , asm-operand
21844 asm-operand:
21845 string-literal ( expression )
21846 [ string-literal ] string-literal ( expression )
21848 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21849 each node is the expression. The TREE_PURPOSE is itself a
21850 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21851 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21852 is a STRING_CST for the string literal before the parenthesis. Returns
21853 ERROR_MARK_NODE if any of the operands are invalid. */
21855 static tree
21856 cp_parser_asm_operand_list (cp_parser* parser)
21858 tree asm_operands = NULL_TREE;
21859 bool invalid_operands = false;
21861 while (true)
21863 tree string_literal;
21864 tree expression;
21865 tree name;
21867 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21869 /* Consume the `[' token. */
21870 cp_lexer_consume_token (parser->lexer);
21871 /* Read the operand name. */
21872 name = cp_parser_identifier (parser);
21873 if (name != error_mark_node)
21874 name = build_string (IDENTIFIER_LENGTH (name),
21875 IDENTIFIER_POINTER (name));
21876 /* Look for the closing `]'. */
21877 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21879 else
21880 name = NULL_TREE;
21881 /* Look for the string-literal. */
21882 string_literal = cp_parser_string_literal (parser, false, false);
21884 /* Look for the `('. */
21885 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21886 /* Parse the expression. */
21887 expression = cp_parser_expression (parser);
21888 /* Look for the `)'. */
21889 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21891 if (name == error_mark_node
21892 || string_literal == error_mark_node
21893 || expression == error_mark_node)
21894 invalid_operands = true;
21896 /* Add this operand to the list. */
21897 asm_operands = tree_cons (build_tree_list (name, string_literal),
21898 expression,
21899 asm_operands);
21900 /* If the next token is not a `,', there are no more
21901 operands. */
21902 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21903 break;
21904 /* Consume the `,'. */
21905 cp_lexer_consume_token (parser->lexer);
21908 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21911 /* Parse an asm-clobber-list.
21913 asm-clobber-list:
21914 string-literal
21915 asm-clobber-list , string-literal
21917 Returns a TREE_LIST, indicating the clobbers in the order that they
21918 appeared. The TREE_VALUE of each node is a STRING_CST. */
21920 static tree
21921 cp_parser_asm_clobber_list (cp_parser* parser)
21923 tree clobbers = NULL_TREE;
21925 while (true)
21927 tree string_literal;
21929 /* Look for the string literal. */
21930 string_literal = cp_parser_string_literal (parser, false, false);
21931 /* Add it to the list. */
21932 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21933 /* If the next token is not a `,', then the list is
21934 complete. */
21935 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21936 break;
21937 /* Consume the `,' token. */
21938 cp_lexer_consume_token (parser->lexer);
21941 return clobbers;
21944 /* Parse an asm-label-list.
21946 asm-label-list:
21947 identifier
21948 asm-label-list , identifier
21950 Returns a TREE_LIST, indicating the labels in the order that they
21951 appeared. The TREE_VALUE of each node is a label. */
21953 static tree
21954 cp_parser_asm_label_list (cp_parser* parser)
21956 tree labels = NULL_TREE;
21958 while (true)
21960 tree identifier, label, name;
21962 /* Look for the identifier. */
21963 identifier = cp_parser_identifier (parser);
21964 if (!error_operand_p (identifier))
21966 label = lookup_label (identifier);
21967 if (TREE_CODE (label) == LABEL_DECL)
21969 TREE_USED (label) = 1;
21970 check_goto (label);
21971 name = build_string (IDENTIFIER_LENGTH (identifier),
21972 IDENTIFIER_POINTER (identifier));
21973 labels = tree_cons (name, label, labels);
21976 /* If the next token is not a `,', then the list is
21977 complete. */
21978 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21979 break;
21980 /* Consume the `,' token. */
21981 cp_lexer_consume_token (parser->lexer);
21984 return nreverse (labels);
21987 /* Return TRUE iff the next tokens in the stream are possibly the
21988 beginning of a GNU extension attribute. */
21990 static bool
21991 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21993 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21996 /* Return TRUE iff the next tokens in the stream are possibly the
21997 beginning of a standard C++-11 attribute specifier. */
21999 static bool
22000 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22002 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22005 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22006 beginning of a standard C++-11 attribute specifier. */
22008 static bool
22009 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22011 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22013 return (cxx_dialect >= cxx11
22014 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22015 || (token->type == CPP_OPEN_SQUARE
22016 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22017 && token->type == CPP_OPEN_SQUARE)));
22020 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22021 beginning of a GNU extension attribute. */
22023 static bool
22024 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22026 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22028 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22031 /* Return true iff the next tokens can be the beginning of either a
22032 GNU attribute list, or a standard C++11 attribute sequence. */
22034 static bool
22035 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22037 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22038 || cp_next_tokens_can_be_std_attribute_p (parser));
22041 /* Return true iff the next Nth tokens can be the beginning of either
22042 a GNU attribute list, or a standard C++11 attribute sequence. */
22044 static bool
22045 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22047 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22048 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22051 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22052 of GNU attributes, or return NULL. */
22054 static tree
22055 cp_parser_attributes_opt (cp_parser *parser)
22057 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22058 return cp_parser_gnu_attributes_opt (parser);
22059 return cp_parser_std_attribute_spec_seq (parser);
22062 #define CILK_SIMD_FN_CLAUSE_MASK \
22063 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22064 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22065 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22066 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22067 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22069 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22070 vector [(<clauses>)] */
22072 static void
22073 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22075 bool first_p = parser->cilk_simd_fn_info == NULL;
22076 cp_token *token = v_token;
22077 if (first_p)
22079 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22080 parser->cilk_simd_fn_info->error_seen = false;
22081 parser->cilk_simd_fn_info->fndecl_seen = false;
22082 parser->cilk_simd_fn_info->tokens = vNULL;
22084 int paren_scope = 0;
22085 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22087 cp_lexer_consume_token (parser->lexer);
22088 v_token = cp_lexer_peek_token (parser->lexer);
22089 paren_scope++;
22091 while (paren_scope > 0)
22093 token = cp_lexer_peek_token (parser->lexer);
22094 if (token->type == CPP_OPEN_PAREN)
22095 paren_scope++;
22096 else if (token->type == CPP_CLOSE_PAREN)
22097 paren_scope--;
22098 /* Do not push the last ')' */
22099 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22100 cp_lexer_consume_token (parser->lexer);
22103 token->type = CPP_PRAGMA_EOL;
22104 parser->lexer->next_token = token;
22105 cp_lexer_consume_token (parser->lexer);
22107 struct cp_token_cache *cp
22108 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22109 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22112 /* Parse an (optional) series of attributes.
22114 attributes:
22115 attributes attribute
22117 attribute:
22118 __attribute__ (( attribute-list [opt] ))
22120 The return value is as for cp_parser_gnu_attribute_list. */
22122 static tree
22123 cp_parser_gnu_attributes_opt (cp_parser* parser)
22125 tree attributes = NULL_TREE;
22127 while (true)
22129 cp_token *token;
22130 tree attribute_list;
22131 bool ok = true;
22133 /* Peek at the next token. */
22134 token = cp_lexer_peek_token (parser->lexer);
22135 /* If it's not `__attribute__', then we're done. */
22136 if (token->keyword != RID_ATTRIBUTE)
22137 break;
22139 /* Consume the `__attribute__' keyword. */
22140 cp_lexer_consume_token (parser->lexer);
22141 /* Look for the two `(' tokens. */
22142 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22143 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22145 /* Peek at the next token. */
22146 token = cp_lexer_peek_token (parser->lexer);
22147 if (token->type != CPP_CLOSE_PAREN)
22148 /* Parse the attribute-list. */
22149 attribute_list = cp_parser_gnu_attribute_list (parser);
22150 else
22151 /* If the next token is a `)', then there is no attribute
22152 list. */
22153 attribute_list = NULL;
22155 /* Look for the two `)' tokens. */
22156 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22157 ok = false;
22158 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22159 ok = false;
22160 if (!ok)
22161 cp_parser_skip_to_end_of_statement (parser);
22163 /* Add these new attributes to the list. */
22164 attributes = chainon (attributes, attribute_list);
22167 return attributes;
22170 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22171 "__vector" or "__vector__." */
22173 static inline bool
22174 is_cilkplus_vector_p (tree name)
22176 if (flag_cilkplus && is_attribute_p ("vector", name))
22177 return true;
22178 return false;
22181 /* Parse a GNU attribute-list.
22183 attribute-list:
22184 attribute
22185 attribute-list , attribute
22187 attribute:
22188 identifier
22189 identifier ( identifier )
22190 identifier ( identifier , expression-list )
22191 identifier ( expression-list )
22193 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22194 to an attribute. The TREE_PURPOSE of each node is the identifier
22195 indicating which attribute is in use. The TREE_VALUE represents
22196 the arguments, if any. */
22198 static tree
22199 cp_parser_gnu_attribute_list (cp_parser* parser)
22201 tree attribute_list = NULL_TREE;
22202 bool save_translate_strings_p = parser->translate_strings_p;
22204 parser->translate_strings_p = false;
22205 while (true)
22207 cp_token *token;
22208 tree identifier;
22209 tree attribute;
22211 /* Look for the identifier. We also allow keywords here; for
22212 example `__attribute__ ((const))' is legal. */
22213 token = cp_lexer_peek_token (parser->lexer);
22214 if (token->type == CPP_NAME
22215 || token->type == CPP_KEYWORD)
22217 tree arguments = NULL_TREE;
22219 /* Consume the token, but save it since we need it for the
22220 SIMD enabled function parsing. */
22221 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22223 /* Save away the identifier that indicates which attribute
22224 this is. */
22225 identifier = (token->type == CPP_KEYWORD)
22226 /* For keywords, use the canonical spelling, not the
22227 parsed identifier. */
22228 ? ridpointers[(int) token->keyword]
22229 : id_token->u.value;
22231 attribute = build_tree_list (identifier, NULL_TREE);
22233 /* Peek at the next token. */
22234 token = cp_lexer_peek_token (parser->lexer);
22235 /* If it's an `(', then parse the attribute arguments. */
22236 if (token->type == CPP_OPEN_PAREN)
22238 vec<tree, va_gc> *vec;
22239 int attr_flag = (attribute_takes_identifier_p (identifier)
22240 ? id_attr : normal_attr);
22241 if (is_cilkplus_vector_p (identifier))
22243 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22244 continue;
22246 else
22247 vec = cp_parser_parenthesized_expression_list
22248 (parser, attr_flag, /*cast_p=*/false,
22249 /*allow_expansion_p=*/false,
22250 /*non_constant_p=*/NULL);
22251 if (vec == NULL)
22252 arguments = error_mark_node;
22253 else
22255 arguments = build_tree_list_vec (vec);
22256 release_tree_vector (vec);
22258 /* Save the arguments away. */
22259 TREE_VALUE (attribute) = arguments;
22261 else if (is_cilkplus_vector_p (identifier))
22263 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22264 continue;
22267 if (arguments != error_mark_node)
22269 /* Add this attribute to the list. */
22270 TREE_CHAIN (attribute) = attribute_list;
22271 attribute_list = attribute;
22274 token = cp_lexer_peek_token (parser->lexer);
22276 /* Now, look for more attributes. If the next token isn't a
22277 `,', we're done. */
22278 if (token->type != CPP_COMMA)
22279 break;
22281 /* Consume the comma and keep going. */
22282 cp_lexer_consume_token (parser->lexer);
22284 parser->translate_strings_p = save_translate_strings_p;
22286 /* We built up the list in reverse order. */
22287 return nreverse (attribute_list);
22290 /* Parse a standard C++11 attribute.
22292 The returned representation is a TREE_LIST which TREE_PURPOSE is
22293 the scoped name of the attribute, and the TREE_VALUE is its
22294 arguments list.
22296 Note that the scoped name of the attribute is itself a TREE_LIST
22297 which TREE_PURPOSE is the namespace of the attribute, and
22298 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22299 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22300 and which TREE_PURPOSE is directly the attribute name.
22302 Clients of the attribute code should use get_attribute_namespace
22303 and get_attribute_name to get the actual namespace and name of
22304 attributes, regardless of their being GNU or C++11 attributes.
22306 attribute:
22307 attribute-token attribute-argument-clause [opt]
22309 attribute-token:
22310 identifier
22311 attribute-scoped-token
22313 attribute-scoped-token:
22314 attribute-namespace :: identifier
22316 attribute-namespace:
22317 identifier
22319 attribute-argument-clause:
22320 ( balanced-token-seq )
22322 balanced-token-seq:
22323 balanced-token [opt]
22324 balanced-token-seq balanced-token
22326 balanced-token:
22327 ( balanced-token-seq )
22328 [ balanced-token-seq ]
22329 { balanced-token-seq }. */
22331 static tree
22332 cp_parser_std_attribute (cp_parser *parser)
22334 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22335 cp_token *token;
22337 /* First, parse name of the the attribute, a.k.a
22338 attribute-token. */
22340 token = cp_lexer_peek_token (parser->lexer);
22341 if (token->type == CPP_NAME)
22342 attr_id = token->u.value;
22343 else if (token->type == CPP_KEYWORD)
22344 attr_id = ridpointers[(int) token->keyword];
22345 else if (token->flags & NAMED_OP)
22346 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22348 if (attr_id == NULL_TREE)
22349 return NULL_TREE;
22351 cp_lexer_consume_token (parser->lexer);
22353 token = cp_lexer_peek_token (parser->lexer);
22354 if (token->type == CPP_SCOPE)
22356 /* We are seeing a scoped attribute token. */
22358 cp_lexer_consume_token (parser->lexer);
22359 attr_ns = attr_id;
22361 token = cp_lexer_consume_token (parser->lexer);
22362 if (token->type == CPP_NAME)
22363 attr_id = token->u.value;
22364 else if (token->type == CPP_KEYWORD)
22365 attr_id = ridpointers[(int) token->keyword];
22366 else
22368 error_at (token->location,
22369 "expected an identifier for the attribute name");
22370 return error_mark_node;
22372 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22373 NULL_TREE);
22374 token = cp_lexer_peek_token (parser->lexer);
22376 else
22378 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22379 NULL_TREE);
22380 /* C++11 noreturn attribute is equivalent to GNU's. */
22381 if (is_attribute_p ("noreturn", attr_id))
22382 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22383 /* C++14 deprecated attribute is equivalent to GNU's. */
22384 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22386 if (cxx_dialect == cxx11)
22387 pedwarn (token->location, OPT_Wpedantic,
22388 "%<deprecated%> is a C++14 feature;"
22389 " use %<gnu::deprecated%>");
22390 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22394 /* Now parse the optional argument clause of the attribute. */
22396 if (token->type != CPP_OPEN_PAREN)
22397 return attribute;
22400 vec<tree, va_gc> *vec;
22401 int attr_flag = normal_attr;
22403 if (attr_ns == get_identifier ("gnu")
22404 && attribute_takes_identifier_p (attr_id))
22405 /* A GNU attribute that takes an identifier in parameter. */
22406 attr_flag = id_attr;
22408 vec = cp_parser_parenthesized_expression_list
22409 (parser, attr_flag, /*cast_p=*/false,
22410 /*allow_expansion_p=*/true,
22411 /*non_constant_p=*/NULL);
22412 if (vec == NULL)
22413 arguments = error_mark_node;
22414 else
22416 arguments = build_tree_list_vec (vec);
22417 release_tree_vector (vec);
22420 if (arguments == error_mark_node)
22421 attribute = error_mark_node;
22422 else
22423 TREE_VALUE (attribute) = arguments;
22426 return attribute;
22429 /* Parse a list of standard C++-11 attributes.
22431 attribute-list:
22432 attribute [opt]
22433 attribute-list , attribute[opt]
22434 attribute ...
22435 attribute-list , attribute ...
22438 static tree
22439 cp_parser_std_attribute_list (cp_parser *parser)
22441 tree attributes = NULL_TREE, attribute = NULL_TREE;
22442 cp_token *token = NULL;
22444 while (true)
22446 attribute = cp_parser_std_attribute (parser);
22447 if (attribute == error_mark_node)
22448 break;
22449 if (attribute != NULL_TREE)
22451 TREE_CHAIN (attribute) = attributes;
22452 attributes = attribute;
22454 token = cp_lexer_peek_token (parser->lexer);
22455 if (token->type != CPP_COMMA)
22456 break;
22457 cp_lexer_consume_token (parser->lexer);
22459 attributes = nreverse (attributes);
22460 return attributes;
22463 /* Parse a standard C++-11 attribute specifier.
22465 attribute-specifier:
22466 [ [ attribute-list ] ]
22467 alignment-specifier
22469 alignment-specifier:
22470 alignas ( type-id ... [opt] )
22471 alignas ( alignment-expression ... [opt] ). */
22473 static tree
22474 cp_parser_std_attribute_spec (cp_parser *parser)
22476 tree attributes = NULL_TREE;
22477 cp_token *token = cp_lexer_peek_token (parser->lexer);
22479 if (token->type == CPP_OPEN_SQUARE
22480 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22482 cp_lexer_consume_token (parser->lexer);
22483 cp_lexer_consume_token (parser->lexer);
22485 attributes = cp_parser_std_attribute_list (parser);
22487 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22488 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22489 cp_parser_skip_to_end_of_statement (parser);
22490 else
22491 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22492 when we are sure that we have actually parsed them. */
22493 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22495 else
22497 tree alignas_expr;
22499 /* Look for an alignment-specifier. */
22501 token = cp_lexer_peek_token (parser->lexer);
22503 if (token->type != CPP_KEYWORD
22504 || token->keyword != RID_ALIGNAS)
22505 return NULL_TREE;
22507 cp_lexer_consume_token (parser->lexer);
22508 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22510 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22512 cp_parser_error (parser, "expected %<(%>");
22513 return error_mark_node;
22516 cp_parser_parse_tentatively (parser);
22517 alignas_expr = cp_parser_type_id (parser);
22519 if (!cp_parser_parse_definitely (parser))
22521 gcc_assert (alignas_expr == error_mark_node
22522 || alignas_expr == NULL_TREE);
22524 alignas_expr =
22525 cp_parser_assignment_expression (parser);
22526 if (alignas_expr == error_mark_node)
22527 cp_parser_skip_to_end_of_statement (parser);
22528 if (alignas_expr == NULL_TREE
22529 || alignas_expr == error_mark_node)
22530 return alignas_expr;
22533 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22535 cp_parser_error (parser, "expected %<)%>");
22536 return error_mark_node;
22539 alignas_expr = cxx_alignas_expr (alignas_expr);
22541 /* Build the C++-11 representation of an 'aligned'
22542 attribute. */
22543 attributes =
22544 build_tree_list (build_tree_list (get_identifier ("gnu"),
22545 get_identifier ("aligned")),
22546 build_tree_list (NULL_TREE, alignas_expr));
22549 return attributes;
22552 /* Parse a standard C++-11 attribute-specifier-seq.
22554 attribute-specifier-seq:
22555 attribute-specifier-seq [opt] attribute-specifier
22558 static tree
22559 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22561 tree attr_specs = NULL;
22563 while (true)
22565 tree attr_spec = cp_parser_std_attribute_spec (parser);
22566 if (attr_spec == NULL_TREE)
22567 break;
22568 if (attr_spec == error_mark_node)
22569 return error_mark_node;
22571 TREE_CHAIN (attr_spec) = attr_specs;
22572 attr_specs = attr_spec;
22575 attr_specs = nreverse (attr_specs);
22576 return attr_specs;
22579 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22580 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22581 current value of the PEDANTIC flag, regardless of whether or not
22582 the `__extension__' keyword is present. The caller is responsible
22583 for restoring the value of the PEDANTIC flag. */
22585 static bool
22586 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22588 /* Save the old value of the PEDANTIC flag. */
22589 *saved_pedantic = pedantic;
22591 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22593 /* Consume the `__extension__' token. */
22594 cp_lexer_consume_token (parser->lexer);
22595 /* We're not being pedantic while the `__extension__' keyword is
22596 in effect. */
22597 pedantic = 0;
22599 return true;
22602 return false;
22605 /* Parse a label declaration.
22607 label-declaration:
22608 __label__ label-declarator-seq ;
22610 label-declarator-seq:
22611 identifier , label-declarator-seq
22612 identifier */
22614 static void
22615 cp_parser_label_declaration (cp_parser* parser)
22617 /* Look for the `__label__' keyword. */
22618 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22620 while (true)
22622 tree identifier;
22624 /* Look for an identifier. */
22625 identifier = cp_parser_identifier (parser);
22626 /* If we failed, stop. */
22627 if (identifier == error_mark_node)
22628 break;
22629 /* Declare it as a label. */
22630 finish_label_decl (identifier);
22631 /* If the next token is a `;', stop. */
22632 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22633 break;
22634 /* Look for the `,' separating the label declarations. */
22635 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22638 /* Look for the final `;'. */
22639 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22642 /* Support Functions */
22644 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22645 NAME should have one of the representations used for an
22646 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22647 is returned. If PARSER->SCOPE is a dependent type, then a
22648 SCOPE_REF is returned.
22650 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22651 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22652 was formed. Abstractly, such entities should not be passed to this
22653 function, because they do not need to be looked up, but it is
22654 simpler to check for this special case here, rather than at the
22655 call-sites.
22657 In cases not explicitly covered above, this function returns a
22658 DECL, OVERLOAD, or baselink representing the result of the lookup.
22659 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22660 is returned.
22662 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22663 (e.g., "struct") that was used. In that case bindings that do not
22664 refer to types are ignored.
22666 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22667 ignored.
22669 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22670 are ignored.
22672 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22673 types.
22675 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22676 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22677 NULL_TREE otherwise. */
22679 static tree
22680 cp_parser_lookup_name (cp_parser *parser, tree name,
22681 enum tag_types tag_type,
22682 bool is_template,
22683 bool is_namespace,
22684 bool check_dependency,
22685 tree *ambiguous_decls,
22686 location_t name_location)
22688 tree decl;
22689 tree object_type = parser->context->object_type;
22691 /* Assume that the lookup will be unambiguous. */
22692 if (ambiguous_decls)
22693 *ambiguous_decls = NULL_TREE;
22695 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22696 no longer valid. Note that if we are parsing tentatively, and
22697 the parse fails, OBJECT_TYPE will be automatically restored. */
22698 parser->context->object_type = NULL_TREE;
22700 if (name == error_mark_node)
22701 return error_mark_node;
22703 /* A template-id has already been resolved; there is no lookup to
22704 do. */
22705 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22706 return name;
22707 if (BASELINK_P (name))
22709 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22710 == TEMPLATE_ID_EXPR);
22711 return name;
22714 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22715 it should already have been checked to make sure that the name
22716 used matches the type being destroyed. */
22717 if (TREE_CODE (name) == BIT_NOT_EXPR)
22719 tree type;
22721 /* Figure out to which type this destructor applies. */
22722 if (parser->scope)
22723 type = parser->scope;
22724 else if (object_type)
22725 type = object_type;
22726 else
22727 type = current_class_type;
22728 /* If that's not a class type, there is no destructor. */
22729 if (!type || !CLASS_TYPE_P (type))
22730 return error_mark_node;
22731 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22732 lazily_declare_fn (sfk_destructor, type);
22733 if (!CLASSTYPE_DESTRUCTORS (type))
22734 return error_mark_node;
22735 /* If it was a class type, return the destructor. */
22736 return CLASSTYPE_DESTRUCTORS (type);
22739 /* By this point, the NAME should be an ordinary identifier. If
22740 the id-expression was a qualified name, the qualifying scope is
22741 stored in PARSER->SCOPE at this point. */
22742 gcc_assert (identifier_p (name));
22744 /* Perform the lookup. */
22745 if (parser->scope)
22747 bool dependent_p;
22749 if (parser->scope == error_mark_node)
22750 return error_mark_node;
22752 /* If the SCOPE is dependent, the lookup must be deferred until
22753 the template is instantiated -- unless we are explicitly
22754 looking up names in uninstantiated templates. Even then, we
22755 cannot look up the name if the scope is not a class type; it
22756 might, for example, be a template type parameter. */
22757 dependent_p = (TYPE_P (parser->scope)
22758 && dependent_scope_p (parser->scope));
22759 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22760 && dependent_p)
22761 /* Defer lookup. */
22762 decl = error_mark_node;
22763 else
22765 tree pushed_scope = NULL_TREE;
22767 /* If PARSER->SCOPE is a dependent type, then it must be a
22768 class type, and we must not be checking dependencies;
22769 otherwise, we would have processed this lookup above. So
22770 that PARSER->SCOPE is not considered a dependent base by
22771 lookup_member, we must enter the scope here. */
22772 if (dependent_p)
22773 pushed_scope = push_scope (parser->scope);
22775 /* If the PARSER->SCOPE is a template specialization, it
22776 may be instantiated during name lookup. In that case,
22777 errors may be issued. Even if we rollback the current
22778 tentative parse, those errors are valid. */
22779 decl = lookup_qualified_name (parser->scope, name,
22780 tag_type != none_type,
22781 /*complain=*/true);
22783 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22784 lookup result and the nested-name-specifier nominates a class C:
22785 * if the name specified after the nested-name-specifier, when
22786 looked up in C, is the injected-class-name of C (Clause 9), or
22787 * if the name specified after the nested-name-specifier is the
22788 same as the identifier or the simple-template-id's template-
22789 name in the last component of the nested-name-specifier,
22790 the name is instead considered to name the constructor of
22791 class C. [ Note: for example, the constructor is not an
22792 acceptable lookup result in an elaborated-type-specifier so
22793 the constructor would not be used in place of the
22794 injected-class-name. --end note ] Such a constructor name
22795 shall be used only in the declarator-id of a declaration that
22796 names a constructor or in a using-declaration. */
22797 if (tag_type == none_type
22798 && DECL_SELF_REFERENCE_P (decl)
22799 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22800 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22801 tag_type != none_type,
22802 /*complain=*/true);
22804 /* If we have a single function from a using decl, pull it out. */
22805 if (TREE_CODE (decl) == OVERLOAD
22806 && !really_overloaded_fn (decl))
22807 decl = OVL_FUNCTION (decl);
22809 if (pushed_scope)
22810 pop_scope (pushed_scope);
22813 /* If the scope is a dependent type and either we deferred lookup or
22814 we did lookup but didn't find the name, rememeber the name. */
22815 if (decl == error_mark_node && TYPE_P (parser->scope)
22816 && dependent_type_p (parser->scope))
22818 if (tag_type)
22820 tree type;
22822 /* The resolution to Core Issue 180 says that `struct
22823 A::B' should be considered a type-name, even if `A'
22824 is dependent. */
22825 type = make_typename_type (parser->scope, name, tag_type,
22826 /*complain=*/tf_error);
22827 if (type != error_mark_node)
22828 decl = TYPE_NAME (type);
22830 else if (is_template
22831 && (cp_parser_next_token_ends_template_argument_p (parser)
22832 || cp_lexer_next_token_is (parser->lexer,
22833 CPP_CLOSE_PAREN)))
22834 decl = make_unbound_class_template (parser->scope,
22835 name, NULL_TREE,
22836 /*complain=*/tf_error);
22837 else
22838 decl = build_qualified_name (/*type=*/NULL_TREE,
22839 parser->scope, name,
22840 is_template);
22842 parser->qualifying_scope = parser->scope;
22843 parser->object_scope = NULL_TREE;
22845 else if (object_type)
22847 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22848 OBJECT_TYPE is not a class. */
22849 if (CLASS_TYPE_P (object_type))
22850 /* If the OBJECT_TYPE is a template specialization, it may
22851 be instantiated during name lookup. In that case, errors
22852 may be issued. Even if we rollback the current tentative
22853 parse, those errors are valid. */
22854 decl = lookup_member (object_type,
22855 name,
22856 /*protect=*/0,
22857 tag_type != none_type,
22858 tf_warning_or_error);
22859 else
22860 decl = NULL_TREE;
22862 if (!decl)
22863 /* Look it up in the enclosing context. */
22864 decl = lookup_name_real (name, tag_type != none_type,
22865 /*nonclass=*/0,
22866 /*block_p=*/true, is_namespace, 0);
22867 parser->object_scope = object_type;
22868 parser->qualifying_scope = NULL_TREE;
22870 else
22872 decl = lookup_name_real (name, tag_type != none_type,
22873 /*nonclass=*/0,
22874 /*block_p=*/true, is_namespace, 0);
22875 parser->qualifying_scope = NULL_TREE;
22876 parser->object_scope = NULL_TREE;
22879 /* If the lookup failed, let our caller know. */
22880 if (!decl || decl == error_mark_node)
22881 return error_mark_node;
22883 /* Pull out the template from an injected-class-name (or multiple). */
22884 if (is_template)
22885 decl = maybe_get_template_decl_from_type_decl (decl);
22887 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22888 if (TREE_CODE (decl) == TREE_LIST)
22890 if (ambiguous_decls)
22891 *ambiguous_decls = decl;
22892 /* The error message we have to print is too complicated for
22893 cp_parser_error, so we incorporate its actions directly. */
22894 if (!cp_parser_simulate_error (parser))
22896 error_at (name_location, "reference to %qD is ambiguous",
22897 name);
22898 print_candidates (decl);
22900 return error_mark_node;
22903 gcc_assert (DECL_P (decl)
22904 || TREE_CODE (decl) == OVERLOAD
22905 || TREE_CODE (decl) == SCOPE_REF
22906 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22907 || BASELINK_P (decl));
22909 /* If we have resolved the name of a member declaration, check to
22910 see if the declaration is accessible. When the name resolves to
22911 set of overloaded functions, accessibility is checked when
22912 overload resolution is done.
22914 During an explicit instantiation, access is not checked at all,
22915 as per [temp.explicit]. */
22916 if (DECL_P (decl))
22917 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22919 maybe_record_typedef_use (decl);
22921 return decl;
22924 /* Like cp_parser_lookup_name, but for use in the typical case where
22925 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22926 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22928 static tree
22929 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22931 return cp_parser_lookup_name (parser, name,
22932 none_type,
22933 /*is_template=*/false,
22934 /*is_namespace=*/false,
22935 /*check_dependency=*/true,
22936 /*ambiguous_decls=*/NULL,
22937 location);
22940 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22941 the current context, return the TYPE_DECL. If TAG_NAME_P is
22942 true, the DECL indicates the class being defined in a class-head,
22943 or declared in an elaborated-type-specifier.
22945 Otherwise, return DECL. */
22947 static tree
22948 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22950 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22951 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22953 struct A {
22954 template <typename T> struct B;
22957 template <typename T> struct A::B {};
22959 Similarly, in an elaborated-type-specifier:
22961 namespace N { struct X{}; }
22963 struct A {
22964 template <typename T> friend struct N::X;
22967 However, if the DECL refers to a class type, and we are in
22968 the scope of the class, then the name lookup automatically
22969 finds the TYPE_DECL created by build_self_reference rather
22970 than a TEMPLATE_DECL. For example, in:
22972 template <class T> struct S {
22973 S s;
22976 there is no need to handle such case. */
22978 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22979 return DECL_TEMPLATE_RESULT (decl);
22981 return decl;
22984 /* If too many, or too few, template-parameter lists apply to the
22985 declarator, issue an error message. Returns TRUE if all went well,
22986 and FALSE otherwise. */
22988 static bool
22989 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22990 cp_declarator *declarator,
22991 location_t declarator_location)
22993 switch (declarator->kind)
22995 case cdk_id:
22997 unsigned num_templates = 0;
22998 tree scope = declarator->u.id.qualifying_scope;
23000 if (scope)
23001 num_templates = num_template_headers_for_class (scope);
23002 else if (TREE_CODE (declarator->u.id.unqualified_name)
23003 == TEMPLATE_ID_EXPR)
23004 /* If the DECLARATOR has the form `X<y>' then it uses one
23005 additional level of template parameters. */
23006 ++num_templates;
23008 return cp_parser_check_template_parameters
23009 (parser, num_templates, declarator_location, declarator);
23012 case cdk_function:
23013 case cdk_array:
23014 case cdk_pointer:
23015 case cdk_reference:
23016 case cdk_ptrmem:
23017 return (cp_parser_check_declarator_template_parameters
23018 (parser, declarator->declarator, declarator_location));
23020 case cdk_error:
23021 return true;
23023 default:
23024 gcc_unreachable ();
23026 return false;
23029 /* NUM_TEMPLATES were used in the current declaration. If that is
23030 invalid, return FALSE and issue an error messages. Otherwise,
23031 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23032 declarator and we can print more accurate diagnostics. */
23034 static bool
23035 cp_parser_check_template_parameters (cp_parser* parser,
23036 unsigned num_templates,
23037 location_t location,
23038 cp_declarator *declarator)
23040 /* If there are the same number of template classes and parameter
23041 lists, that's OK. */
23042 if (parser->num_template_parameter_lists == num_templates)
23043 return true;
23044 /* If there are more, but only one more, then we are referring to a
23045 member template. That's OK too. */
23046 if (parser->num_template_parameter_lists == num_templates + 1)
23047 return true;
23048 /* If there are more template classes than parameter lists, we have
23049 something like:
23051 template <class T> void S<T>::R<T>::f (); */
23052 if (parser->num_template_parameter_lists < num_templates)
23054 if (declarator && !current_function_decl)
23055 error_at (location, "specializing member %<%T::%E%> "
23056 "requires %<template<>%> syntax",
23057 declarator->u.id.qualifying_scope,
23058 declarator->u.id.unqualified_name);
23059 else if (declarator)
23060 error_at (location, "invalid declaration of %<%T::%E%>",
23061 declarator->u.id.qualifying_scope,
23062 declarator->u.id.unqualified_name);
23063 else
23064 error_at (location, "too few template-parameter-lists");
23065 return false;
23067 /* Otherwise, there are too many template parameter lists. We have
23068 something like:
23070 template <class T> template <class U> void S::f(); */
23071 error_at (location, "too many template-parameter-lists");
23072 return false;
23075 /* Parse an optional `::' token indicating that the following name is
23076 from the global namespace. If so, PARSER->SCOPE is set to the
23077 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23078 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23079 Returns the new value of PARSER->SCOPE, if the `::' token is
23080 present, and NULL_TREE otherwise. */
23082 static tree
23083 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23085 cp_token *token;
23087 /* Peek at the next token. */
23088 token = cp_lexer_peek_token (parser->lexer);
23089 /* If we're looking at a `::' token then we're starting from the
23090 global namespace, not our current location. */
23091 if (token->type == CPP_SCOPE)
23093 /* Consume the `::' token. */
23094 cp_lexer_consume_token (parser->lexer);
23095 /* Set the SCOPE so that we know where to start the lookup. */
23096 parser->scope = global_namespace;
23097 parser->qualifying_scope = global_namespace;
23098 parser->object_scope = NULL_TREE;
23100 return parser->scope;
23102 else if (!current_scope_valid_p)
23104 parser->scope = NULL_TREE;
23105 parser->qualifying_scope = NULL_TREE;
23106 parser->object_scope = NULL_TREE;
23109 return NULL_TREE;
23112 /* Returns TRUE if the upcoming token sequence is the start of a
23113 constructor declarator. If FRIEND_P is true, the declarator is
23114 preceded by the `friend' specifier. */
23116 static bool
23117 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23119 bool constructor_p;
23120 bool outside_class_specifier_p;
23121 tree nested_name_specifier;
23122 cp_token *next_token;
23124 /* The common case is that this is not a constructor declarator, so
23125 try to avoid doing lots of work if at all possible. It's not
23126 valid declare a constructor at function scope. */
23127 if (parser->in_function_body)
23128 return false;
23129 /* And only certain tokens can begin a constructor declarator. */
23130 next_token = cp_lexer_peek_token (parser->lexer);
23131 if (next_token->type != CPP_NAME
23132 && next_token->type != CPP_SCOPE
23133 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23134 && next_token->type != CPP_TEMPLATE_ID)
23135 return false;
23137 /* Parse tentatively; we are going to roll back all of the tokens
23138 consumed here. */
23139 cp_parser_parse_tentatively (parser);
23140 /* Assume that we are looking at a constructor declarator. */
23141 constructor_p = true;
23143 /* Look for the optional `::' operator. */
23144 cp_parser_global_scope_opt (parser,
23145 /*current_scope_valid_p=*/false);
23146 /* Look for the nested-name-specifier. */
23147 nested_name_specifier
23148 = (cp_parser_nested_name_specifier_opt (parser,
23149 /*typename_keyword_p=*/false,
23150 /*check_dependency_p=*/false,
23151 /*type_p=*/false,
23152 /*is_declaration=*/false));
23154 outside_class_specifier_p = (!at_class_scope_p ()
23155 || !TYPE_BEING_DEFINED (current_class_type)
23156 || friend_p);
23158 /* Outside of a class-specifier, there must be a
23159 nested-name-specifier. */
23160 if (!nested_name_specifier && outside_class_specifier_p)
23161 constructor_p = false;
23162 else if (nested_name_specifier == error_mark_node)
23163 constructor_p = false;
23165 /* If we have a class scope, this is easy; DR 147 says that S::S always
23166 names the constructor, and no other qualified name could. */
23167 if (constructor_p && nested_name_specifier
23168 && CLASS_TYPE_P (nested_name_specifier))
23170 tree id = cp_parser_unqualified_id (parser,
23171 /*template_keyword_p=*/false,
23172 /*check_dependency_p=*/false,
23173 /*declarator_p=*/true,
23174 /*optional_p=*/false);
23175 if (is_overloaded_fn (id))
23176 id = DECL_NAME (get_first_fn (id));
23177 if (!constructor_name_p (id, nested_name_specifier))
23178 constructor_p = false;
23180 /* If we still think that this might be a constructor-declarator,
23181 look for a class-name. */
23182 else if (constructor_p)
23184 /* If we have:
23186 template <typename T> struct S {
23187 S();
23190 we must recognize that the nested `S' names a class. */
23191 tree type_decl;
23192 type_decl = cp_parser_class_name (parser,
23193 /*typename_keyword_p=*/false,
23194 /*template_keyword_p=*/false,
23195 none_type,
23196 /*check_dependency_p=*/false,
23197 /*class_head_p=*/false,
23198 /*is_declaration=*/false);
23199 /* If there was no class-name, then this is not a constructor.
23200 Otherwise, if we are in a class-specifier and we aren't
23201 handling a friend declaration, check that its type matches
23202 current_class_type (c++/38313). Note: error_mark_node
23203 is left alone for error recovery purposes. */
23204 constructor_p = (!cp_parser_error_occurred (parser)
23205 && (outside_class_specifier_p
23206 || type_decl == error_mark_node
23207 || same_type_p (current_class_type,
23208 TREE_TYPE (type_decl))));
23210 /* If we're still considering a constructor, we have to see a `(',
23211 to begin the parameter-declaration-clause, followed by either a
23212 `)', an `...', or a decl-specifier. We need to check for a
23213 type-specifier to avoid being fooled into thinking that:
23215 S (f) (int);
23217 is a constructor. (It is actually a function named `f' that
23218 takes one parameter (of type `int') and returns a value of type
23219 `S'. */
23220 if (constructor_p
23221 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23222 constructor_p = false;
23224 if (constructor_p
23225 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23226 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23227 /* A parameter declaration begins with a decl-specifier,
23228 which is either the "attribute" keyword, a storage class
23229 specifier, or (usually) a type-specifier. */
23230 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23232 tree type;
23233 tree pushed_scope = NULL_TREE;
23234 unsigned saved_num_template_parameter_lists;
23236 /* Names appearing in the type-specifier should be looked up
23237 in the scope of the class. */
23238 if (current_class_type)
23239 type = NULL_TREE;
23240 else
23242 type = TREE_TYPE (type_decl);
23243 if (TREE_CODE (type) == TYPENAME_TYPE)
23245 type = resolve_typename_type (type,
23246 /*only_current_p=*/false);
23247 if (TREE_CODE (type) == TYPENAME_TYPE)
23249 cp_parser_abort_tentative_parse (parser);
23250 return false;
23253 pushed_scope = push_scope (type);
23256 /* Inside the constructor parameter list, surrounding
23257 template-parameter-lists do not apply. */
23258 saved_num_template_parameter_lists
23259 = parser->num_template_parameter_lists;
23260 parser->num_template_parameter_lists = 0;
23262 /* Look for the type-specifier. */
23263 cp_parser_type_specifier (parser,
23264 CP_PARSER_FLAGS_NONE,
23265 /*decl_specs=*/NULL,
23266 /*is_declarator=*/true,
23267 /*declares_class_or_enum=*/NULL,
23268 /*is_cv_qualifier=*/NULL);
23270 parser->num_template_parameter_lists
23271 = saved_num_template_parameter_lists;
23273 /* Leave the scope of the class. */
23274 if (pushed_scope)
23275 pop_scope (pushed_scope);
23277 constructor_p = !cp_parser_error_occurred (parser);
23281 /* We did not really want to consume any tokens. */
23282 cp_parser_abort_tentative_parse (parser);
23284 return constructor_p;
23287 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23288 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23289 they must be performed once we are in the scope of the function.
23291 Returns the function defined. */
23293 static tree
23294 cp_parser_function_definition_from_specifiers_and_declarator
23295 (cp_parser* parser,
23296 cp_decl_specifier_seq *decl_specifiers,
23297 tree attributes,
23298 const cp_declarator *declarator)
23300 tree fn;
23301 bool success_p;
23303 /* Begin the function-definition. */
23304 success_p = start_function (decl_specifiers, declarator, attributes);
23306 /* The things we're about to see are not directly qualified by any
23307 template headers we've seen thus far. */
23308 reset_specialization ();
23310 /* If there were names looked up in the decl-specifier-seq that we
23311 did not check, check them now. We must wait until we are in the
23312 scope of the function to perform the checks, since the function
23313 might be a friend. */
23314 perform_deferred_access_checks (tf_warning_or_error);
23316 if (success_p)
23318 cp_finalize_omp_declare_simd (parser, current_function_decl);
23319 parser->omp_declare_simd = NULL;
23322 if (!success_p)
23324 /* Skip the entire function. */
23325 cp_parser_skip_to_end_of_block_or_statement (parser);
23326 fn = error_mark_node;
23328 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23330 /* Seen already, skip it. An error message has already been output. */
23331 cp_parser_skip_to_end_of_block_or_statement (parser);
23332 fn = current_function_decl;
23333 current_function_decl = NULL_TREE;
23334 /* If this is a function from a class, pop the nested class. */
23335 if (current_class_name)
23336 pop_nested_class ();
23338 else
23340 timevar_id_t tv;
23341 if (DECL_DECLARED_INLINE_P (current_function_decl))
23342 tv = TV_PARSE_INLINE;
23343 else
23344 tv = TV_PARSE_FUNC;
23345 timevar_push (tv);
23346 fn = cp_parser_function_definition_after_declarator (parser,
23347 /*inline_p=*/false);
23348 timevar_pop (tv);
23351 return fn;
23354 /* Parse the part of a function-definition that follows the
23355 declarator. INLINE_P is TRUE iff this function is an inline
23356 function defined within a class-specifier.
23358 Returns the function defined. */
23360 static tree
23361 cp_parser_function_definition_after_declarator (cp_parser* parser,
23362 bool inline_p)
23364 tree fn;
23365 bool ctor_initializer_p = false;
23366 bool saved_in_unbraced_linkage_specification_p;
23367 bool saved_in_function_body;
23368 unsigned saved_num_template_parameter_lists;
23369 cp_token *token;
23370 bool fully_implicit_function_template_p
23371 = parser->fully_implicit_function_template_p;
23372 parser->fully_implicit_function_template_p = false;
23373 tree implicit_template_parms
23374 = parser->implicit_template_parms;
23375 parser->implicit_template_parms = 0;
23376 cp_binding_level* implicit_template_scope
23377 = parser->implicit_template_scope;
23378 parser->implicit_template_scope = 0;
23380 saved_in_function_body = parser->in_function_body;
23381 parser->in_function_body = true;
23382 /* If the next token is `return', then the code may be trying to
23383 make use of the "named return value" extension that G++ used to
23384 support. */
23385 token = cp_lexer_peek_token (parser->lexer);
23386 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23388 /* Consume the `return' keyword. */
23389 cp_lexer_consume_token (parser->lexer);
23390 /* Look for the identifier that indicates what value is to be
23391 returned. */
23392 cp_parser_identifier (parser);
23393 /* Issue an error message. */
23394 error_at (token->location,
23395 "named return values are no longer supported");
23396 /* Skip tokens until we reach the start of the function body. */
23397 while (true)
23399 cp_token *token = cp_lexer_peek_token (parser->lexer);
23400 if (token->type == CPP_OPEN_BRACE
23401 || token->type == CPP_EOF
23402 || token->type == CPP_PRAGMA_EOL)
23403 break;
23404 cp_lexer_consume_token (parser->lexer);
23407 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23408 anything declared inside `f'. */
23409 saved_in_unbraced_linkage_specification_p
23410 = parser->in_unbraced_linkage_specification_p;
23411 parser->in_unbraced_linkage_specification_p = false;
23412 /* Inside the function, surrounding template-parameter-lists do not
23413 apply. */
23414 saved_num_template_parameter_lists
23415 = parser->num_template_parameter_lists;
23416 parser->num_template_parameter_lists = 0;
23418 start_lambda_scope (current_function_decl);
23420 /* If the next token is `try', `__transaction_atomic', or
23421 `__transaction_relaxed`, then we are looking at either function-try-block
23422 or function-transaction-block. Note that all of these include the
23423 function-body. */
23424 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23425 ctor_initializer_p = cp_parser_function_transaction (parser,
23426 RID_TRANSACTION_ATOMIC);
23427 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23428 RID_TRANSACTION_RELAXED))
23429 ctor_initializer_p = cp_parser_function_transaction (parser,
23430 RID_TRANSACTION_RELAXED);
23431 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23432 ctor_initializer_p = cp_parser_function_try_block (parser);
23433 else
23434 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23435 (parser, /*in_function_try_block=*/false);
23437 finish_lambda_scope ();
23439 /* Finish the function. */
23440 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23441 (inline_p ? 2 : 0));
23442 /* Generate code for it, if necessary. */
23443 expand_or_defer_fn (fn);
23444 /* Restore the saved values. */
23445 parser->in_unbraced_linkage_specification_p
23446 = saved_in_unbraced_linkage_specification_p;
23447 parser->num_template_parameter_lists
23448 = saved_num_template_parameter_lists;
23449 parser->in_function_body = saved_in_function_body;
23451 parser->fully_implicit_function_template_p
23452 = fully_implicit_function_template_p;
23453 parser->implicit_template_parms
23454 = implicit_template_parms;
23455 parser->implicit_template_scope
23456 = implicit_template_scope;
23458 if (parser->fully_implicit_function_template_p)
23459 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23461 return fn;
23464 /* Parse a template-declaration, assuming that the `export' (and
23465 `extern') keywords, if present, has already been scanned. MEMBER_P
23466 is as for cp_parser_template_declaration. */
23468 static void
23469 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23471 tree decl = NULL_TREE;
23472 vec<deferred_access_check, va_gc> *checks;
23473 tree parameter_list;
23474 bool friend_p = false;
23475 bool need_lang_pop;
23476 cp_token *token;
23478 /* Look for the `template' keyword. */
23479 token = cp_lexer_peek_token (parser->lexer);
23480 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23481 return;
23483 /* And the `<'. */
23484 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23485 return;
23486 if (at_class_scope_p () && current_function_decl)
23488 /* 14.5.2.2 [temp.mem]
23490 A local class shall not have member templates. */
23491 error_at (token->location,
23492 "invalid declaration of member template in local class");
23493 cp_parser_skip_to_end_of_block_or_statement (parser);
23494 return;
23496 /* [temp]
23498 A template ... shall not have C linkage. */
23499 if (current_lang_name == lang_name_c)
23501 error_at (token->location, "template with C linkage");
23502 /* Give it C++ linkage to avoid confusing other parts of the
23503 front end. */
23504 push_lang_context (lang_name_cplusplus);
23505 need_lang_pop = true;
23507 else
23508 need_lang_pop = false;
23510 /* We cannot perform access checks on the template parameter
23511 declarations until we know what is being declared, just as we
23512 cannot check the decl-specifier list. */
23513 push_deferring_access_checks (dk_deferred);
23515 /* If the next token is `>', then we have an invalid
23516 specialization. Rather than complain about an invalid template
23517 parameter, issue an error message here. */
23518 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23520 cp_parser_error (parser, "invalid explicit specialization");
23521 begin_specialization ();
23522 parameter_list = NULL_TREE;
23524 else
23526 /* Parse the template parameters. */
23527 parameter_list = cp_parser_template_parameter_list (parser);
23530 /* Get the deferred access checks from the parameter list. These
23531 will be checked once we know what is being declared, as for a
23532 member template the checks must be performed in the scope of the
23533 class containing the member. */
23534 checks = get_deferred_access_checks ();
23536 /* Look for the `>'. */
23537 cp_parser_skip_to_end_of_template_parameter_list (parser);
23538 /* We just processed one more parameter list. */
23539 ++parser->num_template_parameter_lists;
23540 /* If the next token is `template', there are more template
23541 parameters. */
23542 if (cp_lexer_next_token_is_keyword (parser->lexer,
23543 RID_TEMPLATE))
23544 cp_parser_template_declaration_after_export (parser, member_p);
23545 else if (cxx_dialect >= cxx11
23546 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23547 decl = cp_parser_alias_declaration (parser);
23548 else
23550 /* There are no access checks when parsing a template, as we do not
23551 know if a specialization will be a friend. */
23552 push_deferring_access_checks (dk_no_check);
23553 token = cp_lexer_peek_token (parser->lexer);
23554 decl = cp_parser_single_declaration (parser,
23555 checks,
23556 member_p,
23557 /*explicit_specialization_p=*/false,
23558 &friend_p);
23559 pop_deferring_access_checks ();
23561 /* If this is a member template declaration, let the front
23562 end know. */
23563 if (member_p && !friend_p && decl)
23565 if (TREE_CODE (decl) == TYPE_DECL)
23566 cp_parser_check_access_in_redeclaration (decl, token->location);
23568 decl = finish_member_template_decl (decl);
23570 else if (friend_p && decl
23571 && DECL_DECLARES_TYPE_P (decl))
23572 make_friend_class (current_class_type, TREE_TYPE (decl),
23573 /*complain=*/true);
23575 /* We are done with the current parameter list. */
23576 --parser->num_template_parameter_lists;
23578 pop_deferring_access_checks ();
23580 /* Finish up. */
23581 finish_template_decl (parameter_list);
23583 /* Check the template arguments for a literal operator template. */
23584 if (decl
23585 && DECL_DECLARES_FUNCTION_P (decl)
23586 && UDLIT_OPER_P (DECL_NAME (decl)))
23588 bool ok = true;
23589 if (parameter_list == NULL_TREE)
23590 ok = false;
23591 else
23593 int num_parms = TREE_VEC_LENGTH (parameter_list);
23594 if (num_parms == 1)
23596 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23597 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23598 if (TREE_TYPE (parm) != char_type_node
23599 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23600 ok = false;
23602 else if (num_parms == 2 && cxx_dialect >= cxx14)
23604 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23605 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23606 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23607 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23608 if (TREE_TYPE (parm) != TREE_TYPE (type)
23609 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23610 ok = false;
23612 else
23613 ok = false;
23615 if (!ok)
23617 if (cxx_dialect >= cxx14)
23618 error ("literal operator template %qD has invalid parameter list."
23619 " Expected non-type template argument pack <char...>"
23620 " or <typename CharT, CharT...>",
23621 decl);
23622 else
23623 error ("literal operator template %qD has invalid parameter list."
23624 " Expected non-type template argument pack <char...>",
23625 decl);
23628 /* Register member declarations. */
23629 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23630 finish_member_declaration (decl);
23631 /* For the erroneous case of a template with C linkage, we pushed an
23632 implicit C++ linkage scope; exit that scope now. */
23633 if (need_lang_pop)
23634 pop_lang_context ();
23635 /* If DECL is a function template, we must return to parse it later.
23636 (Even though there is no definition, there might be default
23637 arguments that need handling.) */
23638 if (member_p && decl
23639 && DECL_DECLARES_FUNCTION_P (decl))
23640 vec_safe_push (unparsed_funs_with_definitions, decl);
23643 /* Perform the deferred access checks from a template-parameter-list.
23644 CHECKS is a TREE_LIST of access checks, as returned by
23645 get_deferred_access_checks. */
23647 static void
23648 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23650 ++processing_template_parmlist;
23651 perform_access_checks (checks, tf_warning_or_error);
23652 --processing_template_parmlist;
23655 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23656 `function-definition' sequence that follows a template header.
23657 If MEMBER_P is true, this declaration appears in a class scope.
23659 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23660 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23662 static tree
23663 cp_parser_single_declaration (cp_parser* parser,
23664 vec<deferred_access_check, va_gc> *checks,
23665 bool member_p,
23666 bool explicit_specialization_p,
23667 bool* friend_p)
23669 int declares_class_or_enum;
23670 tree decl = NULL_TREE;
23671 cp_decl_specifier_seq decl_specifiers;
23672 bool function_definition_p = false;
23673 cp_token *decl_spec_token_start;
23675 /* This function is only used when processing a template
23676 declaration. */
23677 gcc_assert (innermost_scope_kind () == sk_template_parms
23678 || innermost_scope_kind () == sk_template_spec);
23680 /* Defer access checks until we know what is being declared. */
23681 push_deferring_access_checks (dk_deferred);
23683 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23684 alternative. */
23685 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23686 cp_parser_decl_specifier_seq (parser,
23687 CP_PARSER_FLAGS_OPTIONAL,
23688 &decl_specifiers,
23689 &declares_class_or_enum);
23690 if (friend_p)
23691 *friend_p = cp_parser_friend_p (&decl_specifiers);
23693 /* There are no template typedefs. */
23694 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23696 error_at (decl_spec_token_start->location,
23697 "template declaration of %<typedef%>");
23698 decl = error_mark_node;
23701 /* Gather up the access checks that occurred the
23702 decl-specifier-seq. */
23703 stop_deferring_access_checks ();
23705 /* Check for the declaration of a template class. */
23706 if (declares_class_or_enum)
23708 if (cp_parser_declares_only_class_p (parser))
23710 decl = shadow_tag (&decl_specifiers);
23712 /* In this case:
23714 struct C {
23715 friend template <typename T> struct A<T>::B;
23718 A<T>::B will be represented by a TYPENAME_TYPE, and
23719 therefore not recognized by shadow_tag. */
23720 if (friend_p && *friend_p
23721 && !decl
23722 && decl_specifiers.type
23723 && TYPE_P (decl_specifiers.type))
23724 decl = decl_specifiers.type;
23726 if (decl && decl != error_mark_node)
23727 decl = TYPE_NAME (decl);
23728 else
23729 decl = error_mark_node;
23731 /* Perform access checks for template parameters. */
23732 cp_parser_perform_template_parameter_access_checks (checks);
23736 /* Complain about missing 'typename' or other invalid type names. */
23737 if (!decl_specifiers.any_type_specifiers_p
23738 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23740 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23741 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23742 the rest of this declaration. */
23743 decl = error_mark_node;
23744 goto out;
23747 /* If it's not a template class, try for a template function. If
23748 the next token is a `;', then this declaration does not declare
23749 anything. But, if there were errors in the decl-specifiers, then
23750 the error might well have come from an attempted class-specifier.
23751 In that case, there's no need to warn about a missing declarator. */
23752 if (!decl
23753 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23754 || decl_specifiers.type != error_mark_node))
23756 decl = cp_parser_init_declarator (parser,
23757 &decl_specifiers,
23758 checks,
23759 /*function_definition_allowed_p=*/true,
23760 member_p,
23761 declares_class_or_enum,
23762 &function_definition_p,
23763 NULL, NULL);
23765 /* 7.1.1-1 [dcl.stc]
23767 A storage-class-specifier shall not be specified in an explicit
23768 specialization... */
23769 if (decl
23770 && explicit_specialization_p
23771 && decl_specifiers.storage_class != sc_none)
23773 error_at (decl_spec_token_start->location,
23774 "explicit template specialization cannot have a storage class");
23775 decl = error_mark_node;
23778 if (decl && VAR_P (decl))
23779 check_template_variable (decl);
23782 /* Look for a trailing `;' after the declaration. */
23783 if (!function_definition_p
23784 && (decl == error_mark_node
23785 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23786 cp_parser_skip_to_end_of_block_or_statement (parser);
23788 out:
23789 pop_deferring_access_checks ();
23791 /* Clear any current qualification; whatever comes next is the start
23792 of something new. */
23793 parser->scope = NULL_TREE;
23794 parser->qualifying_scope = NULL_TREE;
23795 parser->object_scope = NULL_TREE;
23797 return decl;
23800 /* Parse a cast-expression that is not the operand of a unary "&". */
23802 static tree
23803 cp_parser_simple_cast_expression (cp_parser *parser)
23805 return cp_parser_cast_expression (parser, /*address_p=*/false,
23806 /*cast_p=*/false, /*decltype*/false, NULL);
23809 /* Parse a functional cast to TYPE. Returns an expression
23810 representing the cast. */
23812 static tree
23813 cp_parser_functional_cast (cp_parser* parser, tree type)
23815 vec<tree, va_gc> *vec;
23816 tree expression_list;
23817 tree cast;
23818 bool nonconst_p;
23820 if (!type)
23821 type = error_mark_node;
23823 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23825 cp_lexer_set_source_position (parser->lexer);
23826 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23827 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23828 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23829 if (TREE_CODE (type) == TYPE_DECL)
23830 type = TREE_TYPE (type);
23831 return finish_compound_literal (type, expression_list,
23832 tf_warning_or_error);
23836 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23837 /*cast_p=*/true,
23838 /*allow_expansion_p=*/true,
23839 /*non_constant_p=*/NULL);
23840 if (vec == NULL)
23841 expression_list = error_mark_node;
23842 else
23844 expression_list = build_tree_list_vec (vec);
23845 release_tree_vector (vec);
23848 cast = build_functional_cast (type, expression_list,
23849 tf_warning_or_error);
23850 /* [expr.const]/1: In an integral constant expression "only type
23851 conversions to integral or enumeration type can be used". */
23852 if (TREE_CODE (type) == TYPE_DECL)
23853 type = TREE_TYPE (type);
23854 if (cast != error_mark_node
23855 && !cast_valid_in_integral_constant_expression_p (type)
23856 && cp_parser_non_integral_constant_expression (parser,
23857 NIC_CONSTRUCTOR))
23858 return error_mark_node;
23859 return cast;
23862 /* Save the tokens that make up the body of a member function defined
23863 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23864 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23865 specifiers applied to the declaration. Returns the FUNCTION_DECL
23866 for the member function. */
23868 static tree
23869 cp_parser_save_member_function_body (cp_parser* parser,
23870 cp_decl_specifier_seq *decl_specifiers,
23871 cp_declarator *declarator,
23872 tree attributes)
23874 cp_token *first;
23875 cp_token *last;
23876 tree fn;
23878 /* Create the FUNCTION_DECL. */
23879 fn = grokmethod (decl_specifiers, declarator, attributes);
23880 cp_finalize_omp_declare_simd (parser, fn);
23881 /* If something went badly wrong, bail out now. */
23882 if (fn == error_mark_node)
23884 /* If there's a function-body, skip it. */
23885 if (cp_parser_token_starts_function_definition_p
23886 (cp_lexer_peek_token (parser->lexer)))
23887 cp_parser_skip_to_end_of_block_or_statement (parser);
23888 return error_mark_node;
23891 /* Remember it, if there default args to post process. */
23892 cp_parser_save_default_args (parser, fn);
23894 /* Save away the tokens that make up the body of the
23895 function. */
23896 first = parser->lexer->next_token;
23897 /* Handle function try blocks. */
23898 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23899 cp_lexer_consume_token (parser->lexer);
23900 /* We can have braced-init-list mem-initializers before the fn body. */
23901 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23903 cp_lexer_consume_token (parser->lexer);
23904 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23906 /* cache_group will stop after an un-nested { } pair, too. */
23907 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23908 break;
23910 /* variadic mem-inits have ... after the ')'. */
23911 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23912 cp_lexer_consume_token (parser->lexer);
23915 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23916 /* Handle function try blocks. */
23917 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23918 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23919 last = parser->lexer->next_token;
23921 /* Save away the inline definition; we will process it when the
23922 class is complete. */
23923 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23924 DECL_PENDING_INLINE_P (fn) = 1;
23926 /* We need to know that this was defined in the class, so that
23927 friend templates are handled correctly. */
23928 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23930 /* Add FN to the queue of functions to be parsed later. */
23931 vec_safe_push (unparsed_funs_with_definitions, fn);
23933 return fn;
23936 /* Save the tokens that make up the in-class initializer for a non-static
23937 data member. Returns a DEFAULT_ARG. */
23939 static tree
23940 cp_parser_save_nsdmi (cp_parser* parser)
23942 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23945 /* Parse a template-argument-list, as well as the trailing ">" (but
23946 not the opening "<"). See cp_parser_template_argument_list for the
23947 return value. */
23949 static tree
23950 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23952 tree arguments;
23953 tree saved_scope;
23954 tree saved_qualifying_scope;
23955 tree saved_object_scope;
23956 bool saved_greater_than_is_operator_p;
23957 int saved_unevaluated_operand;
23958 int saved_inhibit_evaluation_warnings;
23960 /* [temp.names]
23962 When parsing a template-id, the first non-nested `>' is taken as
23963 the end of the template-argument-list rather than a greater-than
23964 operator. */
23965 saved_greater_than_is_operator_p
23966 = parser->greater_than_is_operator_p;
23967 parser->greater_than_is_operator_p = false;
23968 /* Parsing the argument list may modify SCOPE, so we save it
23969 here. */
23970 saved_scope = parser->scope;
23971 saved_qualifying_scope = parser->qualifying_scope;
23972 saved_object_scope = parser->object_scope;
23973 /* We need to evaluate the template arguments, even though this
23974 template-id may be nested within a "sizeof". */
23975 saved_unevaluated_operand = cp_unevaluated_operand;
23976 cp_unevaluated_operand = 0;
23977 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23978 c_inhibit_evaluation_warnings = 0;
23979 /* Parse the template-argument-list itself. */
23980 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23981 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23982 arguments = NULL_TREE;
23983 else
23984 arguments = cp_parser_template_argument_list (parser);
23985 /* Look for the `>' that ends the template-argument-list. If we find
23986 a '>>' instead, it's probably just a typo. */
23987 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23989 if (cxx_dialect != cxx98)
23991 /* In C++0x, a `>>' in a template argument list or cast
23992 expression is considered to be two separate `>'
23993 tokens. So, change the current token to a `>', but don't
23994 consume it: it will be consumed later when the outer
23995 template argument list (or cast expression) is parsed.
23996 Note that this replacement of `>' for `>>' is necessary
23997 even if we are parsing tentatively: in the tentative
23998 case, after calling
23999 cp_parser_enclosed_template_argument_list we will always
24000 throw away all of the template arguments and the first
24001 closing `>', either because the template argument list
24002 was erroneous or because we are replacing those tokens
24003 with a CPP_TEMPLATE_ID token. The second `>' (which will
24004 not have been thrown away) is needed either to close an
24005 outer template argument list or to complete a new-style
24006 cast. */
24007 cp_token *token = cp_lexer_peek_token (parser->lexer);
24008 token->type = CPP_GREATER;
24010 else if (!saved_greater_than_is_operator_p)
24012 /* If we're in a nested template argument list, the '>>' has
24013 to be a typo for '> >'. We emit the error message, but we
24014 continue parsing and we push a '>' as next token, so that
24015 the argument list will be parsed correctly. Note that the
24016 global source location is still on the token before the
24017 '>>', so we need to say explicitly where we want it. */
24018 cp_token *token = cp_lexer_peek_token (parser->lexer);
24019 error_at (token->location, "%<>>%> should be %<> >%> "
24020 "within a nested template argument list");
24022 token->type = CPP_GREATER;
24024 else
24026 /* If this is not a nested template argument list, the '>>'
24027 is a typo for '>'. Emit an error message and continue.
24028 Same deal about the token location, but here we can get it
24029 right by consuming the '>>' before issuing the diagnostic. */
24030 cp_token *token = cp_lexer_consume_token (parser->lexer);
24031 error_at (token->location,
24032 "spurious %<>>%>, use %<>%> to terminate "
24033 "a template argument list");
24036 else
24037 cp_parser_skip_to_end_of_template_parameter_list (parser);
24038 /* The `>' token might be a greater-than operator again now. */
24039 parser->greater_than_is_operator_p
24040 = saved_greater_than_is_operator_p;
24041 /* Restore the SAVED_SCOPE. */
24042 parser->scope = saved_scope;
24043 parser->qualifying_scope = saved_qualifying_scope;
24044 parser->object_scope = saved_object_scope;
24045 cp_unevaluated_operand = saved_unevaluated_operand;
24046 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24048 return arguments;
24051 /* MEMBER_FUNCTION is a member function, or a friend. If default
24052 arguments, or the body of the function have not yet been parsed,
24053 parse them now. */
24055 static void
24056 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24058 timevar_push (TV_PARSE_INMETH);
24059 /* If this member is a template, get the underlying
24060 FUNCTION_DECL. */
24061 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24062 member_function = DECL_TEMPLATE_RESULT (member_function);
24064 /* There should not be any class definitions in progress at this
24065 point; the bodies of members are only parsed outside of all class
24066 definitions. */
24067 gcc_assert (parser->num_classes_being_defined == 0);
24068 /* While we're parsing the member functions we might encounter more
24069 classes. We want to handle them right away, but we don't want
24070 them getting mixed up with functions that are currently in the
24071 queue. */
24072 push_unparsed_function_queues (parser);
24074 /* Make sure that any template parameters are in scope. */
24075 maybe_begin_member_template_processing (member_function);
24077 /* If the body of the function has not yet been parsed, parse it
24078 now. */
24079 if (DECL_PENDING_INLINE_P (member_function))
24081 tree function_scope;
24082 cp_token_cache *tokens;
24084 /* The function is no longer pending; we are processing it. */
24085 tokens = DECL_PENDING_INLINE_INFO (member_function);
24086 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24087 DECL_PENDING_INLINE_P (member_function) = 0;
24089 /* If this is a local class, enter the scope of the containing
24090 function. */
24091 function_scope = current_function_decl;
24092 if (function_scope)
24093 push_function_context ();
24095 /* Push the body of the function onto the lexer stack. */
24096 cp_parser_push_lexer_for_tokens (parser, tokens);
24098 /* Let the front end know that we going to be defining this
24099 function. */
24100 start_preparsed_function (member_function, NULL_TREE,
24101 SF_PRE_PARSED | SF_INCLASS_INLINE);
24103 /* Don't do access checking if it is a templated function. */
24104 if (processing_template_decl)
24105 push_deferring_access_checks (dk_no_check);
24107 /* #pragma omp declare reduction needs special parsing. */
24108 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24110 parser->lexer->in_pragma = true;
24111 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24112 finish_function (/*inline*/2);
24113 cp_check_omp_declare_reduction (member_function);
24115 else
24116 /* Now, parse the body of the function. */
24117 cp_parser_function_definition_after_declarator (parser,
24118 /*inline_p=*/true);
24120 if (processing_template_decl)
24121 pop_deferring_access_checks ();
24123 /* Leave the scope of the containing function. */
24124 if (function_scope)
24125 pop_function_context ();
24126 cp_parser_pop_lexer (parser);
24129 /* Remove any template parameters from the symbol table. */
24130 maybe_end_member_template_processing ();
24132 /* Restore the queue. */
24133 pop_unparsed_function_queues (parser);
24134 timevar_pop (TV_PARSE_INMETH);
24137 /* If DECL contains any default args, remember it on the unparsed
24138 functions queue. */
24140 static void
24141 cp_parser_save_default_args (cp_parser* parser, tree decl)
24143 tree probe;
24145 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24146 probe;
24147 probe = TREE_CHAIN (probe))
24148 if (TREE_PURPOSE (probe))
24150 cp_default_arg_entry entry = {current_class_type, decl};
24151 vec_safe_push (unparsed_funs_with_default_args, entry);
24152 break;
24156 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24157 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24158 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24159 from the parameter-type-list. */
24161 static tree
24162 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24163 tree default_arg, tree parmtype)
24165 cp_token_cache *tokens;
24166 tree parsed_arg;
24167 bool dummy;
24169 if (default_arg == error_mark_node)
24170 return error_mark_node;
24172 /* Push the saved tokens for the default argument onto the parser's
24173 lexer stack. */
24174 tokens = DEFARG_TOKENS (default_arg);
24175 cp_parser_push_lexer_for_tokens (parser, tokens);
24177 start_lambda_scope (decl);
24179 /* Parse the default argument. */
24180 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24181 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24182 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24184 finish_lambda_scope ();
24186 if (parsed_arg == error_mark_node)
24187 cp_parser_skip_to_end_of_statement (parser);
24189 if (!processing_template_decl)
24191 /* In a non-template class, check conversions now. In a template,
24192 we'll wait and instantiate these as needed. */
24193 if (TREE_CODE (decl) == PARM_DECL)
24194 parsed_arg = check_default_argument (parmtype, parsed_arg,
24195 tf_warning_or_error);
24196 else
24197 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24200 /* If the token stream has not been completely used up, then
24201 there was extra junk after the end of the default
24202 argument. */
24203 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24205 if (TREE_CODE (decl) == PARM_DECL)
24206 cp_parser_error (parser, "expected %<,%>");
24207 else
24208 cp_parser_error (parser, "expected %<;%>");
24211 /* Revert to the main lexer. */
24212 cp_parser_pop_lexer (parser);
24214 return parsed_arg;
24217 /* FIELD is a non-static data member with an initializer which we saved for
24218 later; parse it now. */
24220 static void
24221 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24223 tree def;
24225 maybe_begin_member_template_processing (field);
24227 push_unparsed_function_queues (parser);
24228 def = cp_parser_late_parse_one_default_arg (parser, field,
24229 DECL_INITIAL (field),
24230 NULL_TREE);
24231 pop_unparsed_function_queues (parser);
24233 maybe_end_member_template_processing ();
24235 DECL_INITIAL (field) = def;
24238 /* FN is a FUNCTION_DECL which may contains a parameter with an
24239 unparsed DEFAULT_ARG. Parse the default args now. This function
24240 assumes that the current scope is the scope in which the default
24241 argument should be processed. */
24243 static void
24244 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24246 bool saved_local_variables_forbidden_p;
24247 tree parm, parmdecl;
24249 /* While we're parsing the default args, we might (due to the
24250 statement expression extension) encounter more classes. We want
24251 to handle them right away, but we don't want them getting mixed
24252 up with default args that are currently in the queue. */
24253 push_unparsed_function_queues (parser);
24255 /* Local variable names (and the `this' keyword) may not appear
24256 in a default argument. */
24257 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24258 parser->local_variables_forbidden_p = true;
24260 push_defarg_context (fn);
24262 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24263 parmdecl = DECL_ARGUMENTS (fn);
24264 parm && parm != void_list_node;
24265 parm = TREE_CHAIN (parm),
24266 parmdecl = DECL_CHAIN (parmdecl))
24268 tree default_arg = TREE_PURPOSE (parm);
24269 tree parsed_arg;
24270 vec<tree, va_gc> *insts;
24271 tree copy;
24272 unsigned ix;
24274 if (!default_arg)
24275 continue;
24277 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24278 /* This can happen for a friend declaration for a function
24279 already declared with default arguments. */
24280 continue;
24282 parsed_arg
24283 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24284 default_arg,
24285 TREE_VALUE (parm));
24286 if (parsed_arg == error_mark_node)
24288 continue;
24291 TREE_PURPOSE (parm) = parsed_arg;
24293 /* Update any instantiations we've already created. */
24294 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24295 vec_safe_iterate (insts, ix, &copy); ix++)
24296 TREE_PURPOSE (copy) = parsed_arg;
24299 pop_defarg_context ();
24301 /* Make sure no default arg is missing. */
24302 check_default_args (fn);
24304 /* Restore the state of local_variables_forbidden_p. */
24305 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24307 /* Restore the queue. */
24308 pop_unparsed_function_queues (parser);
24311 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24313 sizeof ... ( identifier )
24315 where the 'sizeof' token has already been consumed. */
24317 static tree
24318 cp_parser_sizeof_pack (cp_parser *parser)
24320 /* Consume the `...'. */
24321 cp_lexer_consume_token (parser->lexer);
24322 maybe_warn_variadic_templates ();
24324 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24325 if (paren)
24326 cp_lexer_consume_token (parser->lexer);
24327 else
24328 permerror (cp_lexer_peek_token (parser->lexer)->location,
24329 "%<sizeof...%> argument must be surrounded by parentheses");
24331 cp_token *token = cp_lexer_peek_token (parser->lexer);
24332 tree name = cp_parser_identifier (parser);
24333 if (name == error_mark_node)
24334 return error_mark_node;
24335 /* The name is not qualified. */
24336 parser->scope = NULL_TREE;
24337 parser->qualifying_scope = NULL_TREE;
24338 parser->object_scope = NULL_TREE;
24339 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24340 if (expr == error_mark_node)
24341 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24342 token->location);
24343 if (TREE_CODE (expr) == TYPE_DECL)
24344 expr = TREE_TYPE (expr);
24345 else if (TREE_CODE (expr) == CONST_DECL)
24346 expr = DECL_INITIAL (expr);
24347 expr = make_pack_expansion (expr);
24349 if (paren)
24350 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24352 return expr;
24355 /* Parse the operand of `sizeof' (or a similar operator). Returns
24356 either a TYPE or an expression, depending on the form of the
24357 input. The KEYWORD indicates which kind of expression we have
24358 encountered. */
24360 static tree
24361 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24363 tree expr = NULL_TREE;
24364 const char *saved_message;
24365 char *tmp;
24366 bool saved_integral_constant_expression_p;
24367 bool saved_non_integral_constant_expression_p;
24369 /* If it's a `...', then we are computing the length of a parameter
24370 pack. */
24371 if (keyword == RID_SIZEOF
24372 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24373 return cp_parser_sizeof_pack (parser);
24375 /* Types cannot be defined in a `sizeof' expression. Save away the
24376 old message. */
24377 saved_message = parser->type_definition_forbidden_message;
24378 /* And create the new one. */
24379 tmp = concat ("types may not be defined in %<",
24380 IDENTIFIER_POINTER (ridpointers[keyword]),
24381 "%> expressions", NULL);
24382 parser->type_definition_forbidden_message = tmp;
24384 /* The restrictions on constant-expressions do not apply inside
24385 sizeof expressions. */
24386 saved_integral_constant_expression_p
24387 = parser->integral_constant_expression_p;
24388 saved_non_integral_constant_expression_p
24389 = parser->non_integral_constant_expression_p;
24390 parser->integral_constant_expression_p = false;
24392 /* Do not actually evaluate the expression. */
24393 ++cp_unevaluated_operand;
24394 ++c_inhibit_evaluation_warnings;
24395 /* If it's a `(', then we might be looking at the type-id
24396 construction. */
24397 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24399 tree type = NULL_TREE;
24401 /* We can't be sure yet whether we're looking at a type-id or an
24402 expression. */
24403 cp_parser_parse_tentatively (parser);
24404 /* Note: as a GNU Extension, compound literals are considered
24405 postfix-expressions as they are in C99, so they are valid
24406 arguments to sizeof. See comment in cp_parser_cast_expression
24407 for details. */
24408 if (cp_parser_compound_literal_p (parser))
24409 cp_parser_simulate_error (parser);
24410 else
24412 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24413 parser->in_type_id_in_expr_p = true;
24414 /* Look for the type-id. */
24415 type = cp_parser_type_id (parser);
24416 /* Look for the closing `)'. */
24417 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24418 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24421 /* If all went well, then we're done. */
24422 if (cp_parser_parse_definitely (parser))
24424 cp_decl_specifier_seq decl_specs;
24426 /* Build a trivial decl-specifier-seq. */
24427 clear_decl_specs (&decl_specs);
24428 decl_specs.type = type;
24430 /* Call grokdeclarator to figure out what type this is. */
24431 expr = grokdeclarator (NULL,
24432 &decl_specs,
24433 TYPENAME,
24434 /*initialized=*/0,
24435 /*attrlist=*/NULL);
24439 /* If the type-id production did not work out, then we must be
24440 looking at the unary-expression production. */
24441 if (!expr)
24442 expr = cp_parser_unary_expression (parser);
24444 /* Go back to evaluating expressions. */
24445 --cp_unevaluated_operand;
24446 --c_inhibit_evaluation_warnings;
24448 /* Free the message we created. */
24449 free (tmp);
24450 /* And restore the old one. */
24451 parser->type_definition_forbidden_message = saved_message;
24452 parser->integral_constant_expression_p
24453 = saved_integral_constant_expression_p;
24454 parser->non_integral_constant_expression_p
24455 = saved_non_integral_constant_expression_p;
24457 return expr;
24460 /* If the current declaration has no declarator, return true. */
24462 static bool
24463 cp_parser_declares_only_class_p (cp_parser *parser)
24465 /* If the next token is a `;' or a `,' then there is no
24466 declarator. */
24467 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24468 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24471 /* Update the DECL_SPECS to reflect the storage class indicated by
24472 KEYWORD. */
24474 static void
24475 cp_parser_set_storage_class (cp_parser *parser,
24476 cp_decl_specifier_seq *decl_specs,
24477 enum rid keyword,
24478 cp_token *token)
24480 cp_storage_class storage_class;
24482 if (parser->in_unbraced_linkage_specification_p)
24484 error_at (token->location, "invalid use of %qD in linkage specification",
24485 ridpointers[keyword]);
24486 return;
24488 else if (decl_specs->storage_class != sc_none)
24490 decl_specs->conflicting_specifiers_p = true;
24491 return;
24494 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24495 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24496 && decl_specs->gnu_thread_keyword_p)
24498 pedwarn (decl_specs->locations[ds_thread], 0,
24499 "%<__thread%> before %qD", ridpointers[keyword]);
24502 switch (keyword)
24504 case RID_AUTO:
24505 storage_class = sc_auto;
24506 break;
24507 case RID_REGISTER:
24508 storage_class = sc_register;
24509 break;
24510 case RID_STATIC:
24511 storage_class = sc_static;
24512 break;
24513 case RID_EXTERN:
24514 storage_class = sc_extern;
24515 break;
24516 case RID_MUTABLE:
24517 storage_class = sc_mutable;
24518 break;
24519 default:
24520 gcc_unreachable ();
24522 decl_specs->storage_class = storage_class;
24523 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24525 /* A storage class specifier cannot be applied alongside a typedef
24526 specifier. If there is a typedef specifier present then set
24527 conflicting_specifiers_p which will trigger an error later
24528 on in grokdeclarator. */
24529 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24530 decl_specs->conflicting_specifiers_p = true;
24533 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24534 is true, the type is a class or enum definition. */
24536 static void
24537 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24538 tree type_spec,
24539 cp_token *token,
24540 bool type_definition_p)
24542 decl_specs->any_specifiers_p = true;
24544 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24545 (with, for example, in "typedef int wchar_t;") we remember that
24546 this is what happened. In system headers, we ignore these
24547 declarations so that G++ can work with system headers that are not
24548 C++-safe. */
24549 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24550 && !type_definition_p
24551 && (type_spec == boolean_type_node
24552 || type_spec == char16_type_node
24553 || type_spec == char32_type_node
24554 || type_spec == wchar_type_node)
24555 && (decl_specs->type
24556 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24557 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24558 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24559 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24561 decl_specs->redefined_builtin_type = type_spec;
24562 set_and_check_decl_spec_loc (decl_specs,
24563 ds_redefined_builtin_type_spec,
24564 token);
24565 if (!decl_specs->type)
24567 decl_specs->type = type_spec;
24568 decl_specs->type_definition_p = false;
24569 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24572 else if (decl_specs->type)
24573 decl_specs->multiple_types_p = true;
24574 else
24576 decl_specs->type = type_spec;
24577 decl_specs->type_definition_p = type_definition_p;
24578 decl_specs->redefined_builtin_type = NULL_TREE;
24579 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24583 /* True iff TOKEN is the GNU keyword __thread. */
24585 static bool
24586 token_is__thread (cp_token *token)
24588 gcc_assert (token->keyword == RID_THREAD);
24589 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24592 /* Set the location for a declarator specifier and check if it is
24593 duplicated.
24595 DECL_SPECS is the sequence of declarator specifiers onto which to
24596 set the location.
24598 DS is the single declarator specifier to set which location is to
24599 be set onto the existing sequence of declarators.
24601 LOCATION is the location for the declarator specifier to
24602 consider. */
24604 static void
24605 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24606 cp_decl_spec ds, cp_token *token)
24608 gcc_assert (ds < ds_last);
24610 if (decl_specs == NULL)
24611 return;
24613 source_location location = token->location;
24615 if (decl_specs->locations[ds] == 0)
24617 decl_specs->locations[ds] = location;
24618 if (ds == ds_thread)
24619 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24621 else
24623 if (ds == ds_long)
24625 if (decl_specs->locations[ds_long_long] != 0)
24626 error_at (location,
24627 "%<long long long%> is too long for GCC");
24628 else
24630 decl_specs->locations[ds_long_long] = location;
24631 pedwarn_cxx98 (location,
24632 OPT_Wlong_long,
24633 "ISO C++ 1998 does not support %<long long%>");
24636 else if (ds == ds_thread)
24638 bool gnu = token_is__thread (token);
24639 if (gnu != decl_specs->gnu_thread_keyword_p)
24640 error_at (location,
24641 "both %<__thread%> and %<thread_local%> specified");
24642 else
24643 error_at (location, "duplicate %qD", token->u.value);
24645 else
24647 static const char *const decl_spec_names[] = {
24648 "signed",
24649 "unsigned",
24650 "short",
24651 "long",
24652 "const",
24653 "volatile",
24654 "restrict",
24655 "inline",
24656 "virtual",
24657 "explicit",
24658 "friend",
24659 "typedef",
24660 "using",
24661 "constexpr",
24662 "__complex"
24664 error_at (location,
24665 "duplicate %qs", decl_spec_names[ds]);
24670 /* Return true iff the declarator specifier DS is present in the
24671 sequence of declarator specifiers DECL_SPECS. */
24673 bool
24674 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24675 cp_decl_spec ds)
24677 gcc_assert (ds < ds_last);
24679 if (decl_specs == NULL)
24680 return false;
24682 return decl_specs->locations[ds] != 0;
24685 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24686 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24688 static bool
24689 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24691 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24694 /* Issue an error message indicating that TOKEN_DESC was expected.
24695 If KEYWORD is true, it indicated this function is called by
24696 cp_parser_require_keword and the required token can only be
24697 a indicated keyword. */
24699 static void
24700 cp_parser_required_error (cp_parser *parser,
24701 required_token token_desc,
24702 bool keyword)
24704 switch (token_desc)
24706 case RT_NEW:
24707 cp_parser_error (parser, "expected %<new%>");
24708 return;
24709 case RT_DELETE:
24710 cp_parser_error (parser, "expected %<delete%>");
24711 return;
24712 case RT_RETURN:
24713 cp_parser_error (parser, "expected %<return%>");
24714 return;
24715 case RT_WHILE:
24716 cp_parser_error (parser, "expected %<while%>");
24717 return;
24718 case RT_EXTERN:
24719 cp_parser_error (parser, "expected %<extern%>");
24720 return;
24721 case RT_STATIC_ASSERT:
24722 cp_parser_error (parser, "expected %<static_assert%>");
24723 return;
24724 case RT_DECLTYPE:
24725 cp_parser_error (parser, "expected %<decltype%>");
24726 return;
24727 case RT_OPERATOR:
24728 cp_parser_error (parser, "expected %<operator%>");
24729 return;
24730 case RT_CLASS:
24731 cp_parser_error (parser, "expected %<class%>");
24732 return;
24733 case RT_TEMPLATE:
24734 cp_parser_error (parser, "expected %<template%>");
24735 return;
24736 case RT_NAMESPACE:
24737 cp_parser_error (parser, "expected %<namespace%>");
24738 return;
24739 case RT_USING:
24740 cp_parser_error (parser, "expected %<using%>");
24741 return;
24742 case RT_ASM:
24743 cp_parser_error (parser, "expected %<asm%>");
24744 return;
24745 case RT_TRY:
24746 cp_parser_error (parser, "expected %<try%>");
24747 return;
24748 case RT_CATCH:
24749 cp_parser_error (parser, "expected %<catch%>");
24750 return;
24751 case RT_THROW:
24752 cp_parser_error (parser, "expected %<throw%>");
24753 return;
24754 case RT_LABEL:
24755 cp_parser_error (parser, "expected %<__label__%>");
24756 return;
24757 case RT_AT_TRY:
24758 cp_parser_error (parser, "expected %<@try%>");
24759 return;
24760 case RT_AT_SYNCHRONIZED:
24761 cp_parser_error (parser, "expected %<@synchronized%>");
24762 return;
24763 case RT_AT_THROW:
24764 cp_parser_error (parser, "expected %<@throw%>");
24765 return;
24766 case RT_TRANSACTION_ATOMIC:
24767 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24768 return;
24769 case RT_TRANSACTION_RELAXED:
24770 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24771 return;
24772 default:
24773 break;
24775 if (!keyword)
24777 switch (token_desc)
24779 case RT_SEMICOLON:
24780 cp_parser_error (parser, "expected %<;%>");
24781 return;
24782 case RT_OPEN_PAREN:
24783 cp_parser_error (parser, "expected %<(%>");
24784 return;
24785 case RT_CLOSE_BRACE:
24786 cp_parser_error (parser, "expected %<}%>");
24787 return;
24788 case RT_OPEN_BRACE:
24789 cp_parser_error (parser, "expected %<{%>");
24790 return;
24791 case RT_CLOSE_SQUARE:
24792 cp_parser_error (parser, "expected %<]%>");
24793 return;
24794 case RT_OPEN_SQUARE:
24795 cp_parser_error (parser, "expected %<[%>");
24796 return;
24797 case RT_COMMA:
24798 cp_parser_error (parser, "expected %<,%>");
24799 return;
24800 case RT_SCOPE:
24801 cp_parser_error (parser, "expected %<::%>");
24802 return;
24803 case RT_LESS:
24804 cp_parser_error (parser, "expected %<<%>");
24805 return;
24806 case RT_GREATER:
24807 cp_parser_error (parser, "expected %<>%>");
24808 return;
24809 case RT_EQ:
24810 cp_parser_error (parser, "expected %<=%>");
24811 return;
24812 case RT_ELLIPSIS:
24813 cp_parser_error (parser, "expected %<...%>");
24814 return;
24815 case RT_MULT:
24816 cp_parser_error (parser, "expected %<*%>");
24817 return;
24818 case RT_COMPL:
24819 cp_parser_error (parser, "expected %<~%>");
24820 return;
24821 case RT_COLON:
24822 cp_parser_error (parser, "expected %<:%>");
24823 return;
24824 case RT_COLON_SCOPE:
24825 cp_parser_error (parser, "expected %<:%> or %<::%>");
24826 return;
24827 case RT_CLOSE_PAREN:
24828 cp_parser_error (parser, "expected %<)%>");
24829 return;
24830 case RT_COMMA_CLOSE_PAREN:
24831 cp_parser_error (parser, "expected %<,%> or %<)%>");
24832 return;
24833 case RT_PRAGMA_EOL:
24834 cp_parser_error (parser, "expected end of line");
24835 return;
24836 case RT_NAME:
24837 cp_parser_error (parser, "expected identifier");
24838 return;
24839 case RT_SELECT:
24840 cp_parser_error (parser, "expected selection-statement");
24841 return;
24842 case RT_INTERATION:
24843 cp_parser_error (parser, "expected iteration-statement");
24844 return;
24845 case RT_JUMP:
24846 cp_parser_error (parser, "expected jump-statement");
24847 return;
24848 case RT_CLASS_KEY:
24849 cp_parser_error (parser, "expected class-key");
24850 return;
24851 case RT_CLASS_TYPENAME_TEMPLATE:
24852 cp_parser_error (parser,
24853 "expected %<class%>, %<typename%>, or %<template%>");
24854 return;
24855 default:
24856 gcc_unreachable ();
24859 else
24860 gcc_unreachable ();
24865 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24866 issue an error message indicating that TOKEN_DESC was expected.
24868 Returns the token consumed, if the token had the appropriate type.
24869 Otherwise, returns NULL. */
24871 static cp_token *
24872 cp_parser_require (cp_parser* parser,
24873 enum cpp_ttype type,
24874 required_token token_desc)
24876 if (cp_lexer_next_token_is (parser->lexer, type))
24877 return cp_lexer_consume_token (parser->lexer);
24878 else
24880 /* Output the MESSAGE -- unless we're parsing tentatively. */
24881 if (!cp_parser_simulate_error (parser))
24882 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24883 return NULL;
24887 /* An error message is produced if the next token is not '>'.
24888 All further tokens are skipped until the desired token is
24889 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24891 static void
24892 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24894 /* Current level of '< ... >'. */
24895 unsigned level = 0;
24896 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24897 unsigned nesting_depth = 0;
24899 /* Are we ready, yet? If not, issue error message. */
24900 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24901 return;
24903 /* Skip tokens until the desired token is found. */
24904 while (true)
24906 /* Peek at the next token. */
24907 switch (cp_lexer_peek_token (parser->lexer)->type)
24909 case CPP_LESS:
24910 if (!nesting_depth)
24911 ++level;
24912 break;
24914 case CPP_RSHIFT:
24915 if (cxx_dialect == cxx98)
24916 /* C++0x views the `>>' operator as two `>' tokens, but
24917 C++98 does not. */
24918 break;
24919 else if (!nesting_depth && level-- == 0)
24921 /* We've hit a `>>' where the first `>' closes the
24922 template argument list, and the second `>' is
24923 spurious. Just consume the `>>' and stop; we've
24924 already produced at least one error. */
24925 cp_lexer_consume_token (parser->lexer);
24926 return;
24928 /* Fall through for C++0x, so we handle the second `>' in
24929 the `>>'. */
24931 case CPP_GREATER:
24932 if (!nesting_depth && level-- == 0)
24934 /* We've reached the token we want, consume it and stop. */
24935 cp_lexer_consume_token (parser->lexer);
24936 return;
24938 break;
24940 case CPP_OPEN_PAREN:
24941 case CPP_OPEN_SQUARE:
24942 ++nesting_depth;
24943 break;
24945 case CPP_CLOSE_PAREN:
24946 case CPP_CLOSE_SQUARE:
24947 if (nesting_depth-- == 0)
24948 return;
24949 break;
24951 case CPP_EOF:
24952 case CPP_PRAGMA_EOL:
24953 case CPP_SEMICOLON:
24954 case CPP_OPEN_BRACE:
24955 case CPP_CLOSE_BRACE:
24956 /* The '>' was probably forgotten, don't look further. */
24957 return;
24959 default:
24960 break;
24963 /* Consume this token. */
24964 cp_lexer_consume_token (parser->lexer);
24968 /* If the next token is the indicated keyword, consume it. Otherwise,
24969 issue an error message indicating that TOKEN_DESC was expected.
24971 Returns the token consumed, if the token had the appropriate type.
24972 Otherwise, returns NULL. */
24974 static cp_token *
24975 cp_parser_require_keyword (cp_parser* parser,
24976 enum rid keyword,
24977 required_token token_desc)
24979 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24981 if (token && token->keyword != keyword)
24983 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24984 return NULL;
24987 return token;
24990 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24991 function-definition. */
24993 static bool
24994 cp_parser_token_starts_function_definition_p (cp_token* token)
24996 return (/* An ordinary function-body begins with an `{'. */
24997 token->type == CPP_OPEN_BRACE
24998 /* A ctor-initializer begins with a `:'. */
24999 || token->type == CPP_COLON
25000 /* A function-try-block begins with `try'. */
25001 || token->keyword == RID_TRY
25002 /* A function-transaction-block begins with `__transaction_atomic'
25003 or `__transaction_relaxed'. */
25004 || token->keyword == RID_TRANSACTION_ATOMIC
25005 || token->keyword == RID_TRANSACTION_RELAXED
25006 /* The named return value extension begins with `return'. */
25007 || token->keyword == RID_RETURN);
25010 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25011 definition. */
25013 static bool
25014 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25016 cp_token *token;
25018 token = cp_lexer_peek_token (parser->lexer);
25019 return (token->type == CPP_OPEN_BRACE
25020 || (token->type == CPP_COLON
25021 && !parser->colon_doesnt_start_class_def_p));
25024 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25025 C++0x) ending a template-argument. */
25027 static bool
25028 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25030 cp_token *token;
25032 token = cp_lexer_peek_token (parser->lexer);
25033 return (token->type == CPP_COMMA
25034 || token->type == CPP_GREATER
25035 || token->type == CPP_ELLIPSIS
25036 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25039 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25040 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25042 static bool
25043 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25044 size_t n)
25046 cp_token *token;
25048 token = cp_lexer_peek_nth_token (parser->lexer, n);
25049 if (token->type == CPP_LESS)
25050 return true;
25051 /* Check for the sequence `<::' in the original code. It would be lexed as
25052 `[:', where `[' is a digraph, and there is no whitespace before
25053 `:'. */
25054 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25056 cp_token *token2;
25057 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25058 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25059 return true;
25061 return false;
25064 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25065 or none_type otherwise. */
25067 static enum tag_types
25068 cp_parser_token_is_class_key (cp_token* token)
25070 switch (token->keyword)
25072 case RID_CLASS:
25073 return class_type;
25074 case RID_STRUCT:
25075 return record_type;
25076 case RID_UNION:
25077 return union_type;
25079 default:
25080 return none_type;
25084 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25085 or none_type otherwise or if the token is null. */
25087 static enum tag_types
25088 cp_parser_token_is_type_parameter_key (cp_token* token)
25090 if (!token)
25091 return none_type;
25093 switch (token->keyword)
25095 case RID_CLASS:
25096 return class_type;
25097 case RID_TYPENAME:
25098 return typename_type;
25100 default:
25101 return none_type;
25105 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25107 static void
25108 cp_parser_check_class_key (enum tag_types class_key, tree type)
25110 if (type == error_mark_node)
25111 return;
25112 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25114 if (permerror (input_location, "%qs tag used in naming %q#T",
25115 class_key == union_type ? "union"
25116 : class_key == record_type ? "struct" : "class",
25117 type))
25118 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25119 "%q#T was previously declared here", type);
25123 /* Issue an error message if DECL is redeclared with different
25124 access than its original declaration [class.access.spec/3].
25125 This applies to nested classes and nested class templates.
25126 [class.mem/1]. */
25128 static void
25129 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25131 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25132 return;
25134 if ((TREE_PRIVATE (decl)
25135 != (current_access_specifier == access_private_node))
25136 || (TREE_PROTECTED (decl)
25137 != (current_access_specifier == access_protected_node)))
25138 error_at (location, "%qD redeclared with different access", decl);
25141 /* Look for the `template' keyword, as a syntactic disambiguator.
25142 Return TRUE iff it is present, in which case it will be
25143 consumed. */
25145 static bool
25146 cp_parser_optional_template_keyword (cp_parser *parser)
25148 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25150 /* In C++98 the `template' keyword can only be used within templates;
25151 outside templates the parser can always figure out what is a
25152 template and what is not. In C++11, per the resolution of DR 468,
25153 `template' is allowed in cases where it is not strictly necessary. */
25154 if (!processing_template_decl
25155 && pedantic && cxx_dialect == cxx98)
25157 cp_token *token = cp_lexer_peek_token (parser->lexer);
25158 pedwarn (token->location, OPT_Wpedantic,
25159 "in C++98 %<template%> (as a disambiguator) is only "
25160 "allowed within templates");
25161 /* If this part of the token stream is rescanned, the same
25162 error message would be generated. So, we purge the token
25163 from the stream. */
25164 cp_lexer_purge_token (parser->lexer);
25165 return false;
25167 else
25169 /* Consume the `template' keyword. */
25170 cp_lexer_consume_token (parser->lexer);
25171 return true;
25174 return false;
25177 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25178 set PARSER->SCOPE, and perform other related actions. */
25180 static void
25181 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25183 int i;
25184 struct tree_check *check_value;
25185 deferred_access_check *chk;
25186 vec<deferred_access_check, va_gc> *checks;
25188 /* Get the stored value. */
25189 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25190 /* Perform any access checks that were deferred. */
25191 checks = check_value->checks;
25192 if (checks)
25194 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25195 perform_or_defer_access_check (chk->binfo,
25196 chk->decl,
25197 chk->diag_decl, tf_warning_or_error);
25199 /* Set the scope from the stored value. */
25200 parser->scope = check_value->value;
25201 parser->qualifying_scope = check_value->qualifying_scope;
25202 parser->object_scope = NULL_TREE;
25205 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25206 encounter the end of a block before what we were looking for. */
25208 static bool
25209 cp_parser_cache_group (cp_parser *parser,
25210 enum cpp_ttype end,
25211 unsigned depth)
25213 while (true)
25215 cp_token *token = cp_lexer_peek_token (parser->lexer);
25217 /* Abort a parenthesized expression if we encounter a semicolon. */
25218 if ((end == CPP_CLOSE_PAREN || depth == 0)
25219 && token->type == CPP_SEMICOLON)
25220 return true;
25221 /* If we've reached the end of the file, stop. */
25222 if (token->type == CPP_EOF
25223 || (end != CPP_PRAGMA_EOL
25224 && token->type == CPP_PRAGMA_EOL))
25225 return true;
25226 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25227 /* We've hit the end of an enclosing block, so there's been some
25228 kind of syntax error. */
25229 return true;
25231 /* Consume the token. */
25232 cp_lexer_consume_token (parser->lexer);
25233 /* See if it starts a new group. */
25234 if (token->type == CPP_OPEN_BRACE)
25236 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25237 /* In theory this should probably check end == '}', but
25238 cp_parser_save_member_function_body needs it to exit
25239 after either '}' or ')' when called with ')'. */
25240 if (depth == 0)
25241 return false;
25243 else if (token->type == CPP_OPEN_PAREN)
25245 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25246 if (depth == 0 && end == CPP_CLOSE_PAREN)
25247 return false;
25249 else if (token->type == CPP_PRAGMA)
25250 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25251 else if (token->type == end)
25252 return false;
25256 /* Like above, for caching a default argument or NSDMI. Both of these are
25257 terminated by a non-nested comma, but it can be unclear whether or not a
25258 comma is nested in a template argument list unless we do more parsing.
25259 In order to handle this ambiguity, when we encounter a ',' after a '<'
25260 we try to parse what follows as a parameter-declaration-list (in the
25261 case of a default argument) or a member-declarator (in the case of an
25262 NSDMI). If that succeeds, then we stop caching. */
25264 static tree
25265 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25267 unsigned depth = 0;
25268 int maybe_template_id = 0;
25269 cp_token *first_token;
25270 cp_token *token;
25271 tree default_argument;
25273 /* Add tokens until we have processed the entire default
25274 argument. We add the range [first_token, token). */
25275 first_token = cp_lexer_peek_token (parser->lexer);
25276 if (first_token->type == CPP_OPEN_BRACE)
25278 /* For list-initialization, this is straightforward. */
25279 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25280 token = cp_lexer_peek_token (parser->lexer);
25282 else while (true)
25284 bool done = false;
25286 /* Peek at the next token. */
25287 token = cp_lexer_peek_token (parser->lexer);
25288 /* What we do depends on what token we have. */
25289 switch (token->type)
25291 /* In valid code, a default argument must be
25292 immediately followed by a `,' `)', or `...'. */
25293 case CPP_COMMA:
25294 if (depth == 0 && maybe_template_id)
25296 /* If we've seen a '<', we might be in a
25297 template-argument-list. Until Core issue 325 is
25298 resolved, we don't know how this situation ought
25299 to be handled, so try to DTRT. We check whether
25300 what comes after the comma is a valid parameter
25301 declaration list. If it is, then the comma ends
25302 the default argument; otherwise the default
25303 argument continues. */
25304 bool error = false;
25306 /* Set ITALP so cp_parser_parameter_declaration_list
25307 doesn't decide to commit to this parse. */
25308 bool saved_italp = parser->in_template_argument_list_p;
25309 parser->in_template_argument_list_p = true;
25311 cp_parser_parse_tentatively (parser);
25312 cp_lexer_consume_token (parser->lexer);
25314 if (nsdmi)
25316 int ctor_dtor_or_conv_p;
25317 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25318 &ctor_dtor_or_conv_p,
25319 /*parenthesized_p=*/NULL,
25320 /*member_p=*/true,
25321 /*friend_p=*/false);
25323 else
25325 begin_scope (sk_function_parms, NULL_TREE);
25326 cp_parser_parameter_declaration_list (parser, &error);
25327 pop_bindings_and_leave_scope ();
25329 if (!cp_parser_error_occurred (parser) && !error)
25330 done = true;
25331 cp_parser_abort_tentative_parse (parser);
25333 parser->in_template_argument_list_p = saved_italp;
25334 break;
25336 case CPP_CLOSE_PAREN:
25337 case CPP_ELLIPSIS:
25338 /* If we run into a non-nested `;', `}', or `]',
25339 then the code is invalid -- but the default
25340 argument is certainly over. */
25341 case CPP_SEMICOLON:
25342 case CPP_CLOSE_BRACE:
25343 case CPP_CLOSE_SQUARE:
25344 if (depth == 0
25345 /* Handle correctly int n = sizeof ... ( p ); */
25346 && token->type != CPP_ELLIPSIS)
25347 done = true;
25348 /* Update DEPTH, if necessary. */
25349 else if (token->type == CPP_CLOSE_PAREN
25350 || token->type == CPP_CLOSE_BRACE
25351 || token->type == CPP_CLOSE_SQUARE)
25352 --depth;
25353 break;
25355 case CPP_OPEN_PAREN:
25356 case CPP_OPEN_SQUARE:
25357 case CPP_OPEN_BRACE:
25358 ++depth;
25359 break;
25361 case CPP_LESS:
25362 if (depth == 0)
25363 /* This might be the comparison operator, or it might
25364 start a template argument list. */
25365 ++maybe_template_id;
25366 break;
25368 case CPP_RSHIFT:
25369 if (cxx_dialect == cxx98)
25370 break;
25371 /* Fall through for C++0x, which treats the `>>'
25372 operator like two `>' tokens in certain
25373 cases. */
25375 case CPP_GREATER:
25376 if (depth == 0)
25378 /* This might be an operator, or it might close a
25379 template argument list. But if a previous '<'
25380 started a template argument list, this will have
25381 closed it, so we can't be in one anymore. */
25382 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25383 if (maybe_template_id < 0)
25384 maybe_template_id = 0;
25386 break;
25388 /* If we run out of tokens, issue an error message. */
25389 case CPP_EOF:
25390 case CPP_PRAGMA_EOL:
25391 error_at (token->location, "file ends in default argument");
25392 done = true;
25393 break;
25395 case CPP_NAME:
25396 case CPP_SCOPE:
25397 /* In these cases, we should look for template-ids.
25398 For example, if the default argument is
25399 `X<int, double>()', we need to do name lookup to
25400 figure out whether or not `X' is a template; if
25401 so, the `,' does not end the default argument.
25403 That is not yet done. */
25404 break;
25406 default:
25407 break;
25410 /* If we've reached the end, stop. */
25411 if (done)
25412 break;
25414 /* Add the token to the token block. */
25415 token = cp_lexer_consume_token (parser->lexer);
25418 /* Create a DEFAULT_ARG to represent the unparsed default
25419 argument. */
25420 default_argument = make_node (DEFAULT_ARG);
25421 DEFARG_TOKENS (default_argument)
25422 = cp_token_cache_new (first_token, token);
25423 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25425 return default_argument;
25428 /* Begin parsing tentatively. We always save tokens while parsing
25429 tentatively so that if the tentative parsing fails we can restore the
25430 tokens. */
25432 static void
25433 cp_parser_parse_tentatively (cp_parser* parser)
25435 /* Enter a new parsing context. */
25436 parser->context = cp_parser_context_new (parser->context);
25437 /* Begin saving tokens. */
25438 cp_lexer_save_tokens (parser->lexer);
25439 /* In order to avoid repetitive access control error messages,
25440 access checks are queued up until we are no longer parsing
25441 tentatively. */
25442 push_deferring_access_checks (dk_deferred);
25445 /* Commit to the currently active tentative parse. */
25447 static void
25448 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25450 cp_parser_context *context;
25451 cp_lexer *lexer;
25453 /* Mark all of the levels as committed. */
25454 lexer = parser->lexer;
25455 for (context = parser->context; context->next; context = context->next)
25457 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25458 break;
25459 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25460 while (!cp_lexer_saving_tokens (lexer))
25461 lexer = lexer->next;
25462 cp_lexer_commit_tokens (lexer);
25466 /* Commit to the topmost currently active tentative parse.
25468 Note that this function shouldn't be called when there are
25469 irreversible side-effects while in a tentative state. For
25470 example, we shouldn't create a permanent entry in the symbol
25471 table, or issue an error message that might not apply if the
25472 tentative parse is aborted. */
25474 static void
25475 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25477 cp_parser_context *context = parser->context;
25478 cp_lexer *lexer = parser->lexer;
25480 if (context)
25482 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25483 return;
25484 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25486 while (!cp_lexer_saving_tokens (lexer))
25487 lexer = lexer->next;
25488 cp_lexer_commit_tokens (lexer);
25492 /* Abort the currently active tentative parse. All consumed tokens
25493 will be rolled back, and no diagnostics will be issued. */
25495 static void
25496 cp_parser_abort_tentative_parse (cp_parser* parser)
25498 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25499 || errorcount > 0);
25500 cp_parser_simulate_error (parser);
25501 /* Now, pretend that we want to see if the construct was
25502 successfully parsed. */
25503 cp_parser_parse_definitely (parser);
25506 /* Stop parsing tentatively. If a parse error has occurred, restore the
25507 token stream. Otherwise, commit to the tokens we have consumed.
25508 Returns true if no error occurred; false otherwise. */
25510 static bool
25511 cp_parser_parse_definitely (cp_parser* parser)
25513 bool error_occurred;
25514 cp_parser_context *context;
25516 /* Remember whether or not an error occurred, since we are about to
25517 destroy that information. */
25518 error_occurred = cp_parser_error_occurred (parser);
25519 /* Remove the topmost context from the stack. */
25520 context = parser->context;
25521 parser->context = context->next;
25522 /* If no parse errors occurred, commit to the tentative parse. */
25523 if (!error_occurred)
25525 /* Commit to the tokens read tentatively, unless that was
25526 already done. */
25527 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25528 cp_lexer_commit_tokens (parser->lexer);
25530 pop_to_parent_deferring_access_checks ();
25532 /* Otherwise, if errors occurred, roll back our state so that things
25533 are just as they were before we began the tentative parse. */
25534 else
25536 cp_lexer_rollback_tokens (parser->lexer);
25537 pop_deferring_access_checks ();
25539 /* Add the context to the front of the free list. */
25540 context->next = cp_parser_context_free_list;
25541 cp_parser_context_free_list = context;
25543 return !error_occurred;
25546 /* Returns true if we are parsing tentatively and are not committed to
25547 this tentative parse. */
25549 static bool
25550 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25552 return (cp_parser_parsing_tentatively (parser)
25553 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25556 /* Returns nonzero iff an error has occurred during the most recent
25557 tentative parse. */
25559 static bool
25560 cp_parser_error_occurred (cp_parser* parser)
25562 return (cp_parser_parsing_tentatively (parser)
25563 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25566 /* Returns nonzero if GNU extensions are allowed. */
25568 static bool
25569 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25571 return parser->allow_gnu_extensions_p;
25574 /* Objective-C++ Productions */
25577 /* Parse an Objective-C expression, which feeds into a primary-expression
25578 above.
25580 objc-expression:
25581 objc-message-expression
25582 objc-string-literal
25583 objc-encode-expression
25584 objc-protocol-expression
25585 objc-selector-expression
25587 Returns a tree representation of the expression. */
25589 static tree
25590 cp_parser_objc_expression (cp_parser* parser)
25592 /* Try to figure out what kind of declaration is present. */
25593 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25595 switch (kwd->type)
25597 case CPP_OPEN_SQUARE:
25598 return cp_parser_objc_message_expression (parser);
25600 case CPP_OBJC_STRING:
25601 kwd = cp_lexer_consume_token (parser->lexer);
25602 return objc_build_string_object (kwd->u.value);
25604 case CPP_KEYWORD:
25605 switch (kwd->keyword)
25607 case RID_AT_ENCODE:
25608 return cp_parser_objc_encode_expression (parser);
25610 case RID_AT_PROTOCOL:
25611 return cp_parser_objc_protocol_expression (parser);
25613 case RID_AT_SELECTOR:
25614 return cp_parser_objc_selector_expression (parser);
25616 default:
25617 break;
25619 default:
25620 error_at (kwd->location,
25621 "misplaced %<@%D%> Objective-C++ construct",
25622 kwd->u.value);
25623 cp_parser_skip_to_end_of_block_or_statement (parser);
25626 return error_mark_node;
25629 /* Parse an Objective-C message expression.
25631 objc-message-expression:
25632 [ objc-message-receiver objc-message-args ]
25634 Returns a representation of an Objective-C message. */
25636 static tree
25637 cp_parser_objc_message_expression (cp_parser* parser)
25639 tree receiver, messageargs;
25641 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25642 receiver = cp_parser_objc_message_receiver (parser);
25643 messageargs = cp_parser_objc_message_args (parser);
25644 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25646 return objc_build_message_expr (receiver, messageargs);
25649 /* Parse an objc-message-receiver.
25651 objc-message-receiver:
25652 expression
25653 simple-type-specifier
25655 Returns a representation of the type or expression. */
25657 static tree
25658 cp_parser_objc_message_receiver (cp_parser* parser)
25660 tree rcv;
25662 /* An Objective-C message receiver may be either (1) a type
25663 or (2) an expression. */
25664 cp_parser_parse_tentatively (parser);
25665 rcv = cp_parser_expression (parser);
25667 /* If that worked out, fine. */
25668 if (cp_parser_parse_definitely (parser))
25669 return rcv;
25671 cp_parser_parse_tentatively (parser);
25672 rcv = cp_parser_simple_type_specifier (parser,
25673 /*decl_specs=*/NULL,
25674 CP_PARSER_FLAGS_NONE);
25676 if (cp_parser_parse_definitely (parser))
25677 return objc_get_class_reference (rcv);
25679 cp_parser_error (parser, "objective-c++ message receiver expected");
25680 return error_mark_node;
25683 /* Parse the arguments and selectors comprising an Objective-C message.
25685 objc-message-args:
25686 objc-selector
25687 objc-selector-args
25688 objc-selector-args , objc-comma-args
25690 objc-selector-args:
25691 objc-selector [opt] : assignment-expression
25692 objc-selector-args objc-selector [opt] : assignment-expression
25694 objc-comma-args:
25695 assignment-expression
25696 objc-comma-args , assignment-expression
25698 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25699 selector arguments and TREE_VALUE containing a list of comma
25700 arguments. */
25702 static tree
25703 cp_parser_objc_message_args (cp_parser* parser)
25705 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25706 bool maybe_unary_selector_p = true;
25707 cp_token *token = cp_lexer_peek_token (parser->lexer);
25709 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25711 tree selector = NULL_TREE, arg;
25713 if (token->type != CPP_COLON)
25714 selector = cp_parser_objc_selector (parser);
25716 /* Detect if we have a unary selector. */
25717 if (maybe_unary_selector_p
25718 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25719 return build_tree_list (selector, NULL_TREE);
25721 maybe_unary_selector_p = false;
25722 cp_parser_require (parser, CPP_COLON, RT_COLON);
25723 arg = cp_parser_assignment_expression (parser);
25725 sel_args
25726 = chainon (sel_args,
25727 build_tree_list (selector, arg));
25729 token = cp_lexer_peek_token (parser->lexer);
25732 /* Handle non-selector arguments, if any. */
25733 while (token->type == CPP_COMMA)
25735 tree arg;
25737 cp_lexer_consume_token (parser->lexer);
25738 arg = cp_parser_assignment_expression (parser);
25740 addl_args
25741 = chainon (addl_args,
25742 build_tree_list (NULL_TREE, arg));
25744 token = cp_lexer_peek_token (parser->lexer);
25747 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25749 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25750 return build_tree_list (error_mark_node, error_mark_node);
25753 return build_tree_list (sel_args, addl_args);
25756 /* Parse an Objective-C encode expression.
25758 objc-encode-expression:
25759 @encode objc-typename
25761 Returns an encoded representation of the type argument. */
25763 static tree
25764 cp_parser_objc_encode_expression (cp_parser* parser)
25766 tree type;
25767 cp_token *token;
25769 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25770 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25771 token = cp_lexer_peek_token (parser->lexer);
25772 type = complete_type (cp_parser_type_id (parser));
25773 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25775 if (!type)
25777 error_at (token->location,
25778 "%<@encode%> must specify a type as an argument");
25779 return error_mark_node;
25782 /* This happens if we find @encode(T) (where T is a template
25783 typename or something dependent on a template typename) when
25784 parsing a template. In that case, we can't compile it
25785 immediately, but we rather create an AT_ENCODE_EXPR which will
25786 need to be instantiated when the template is used.
25788 if (dependent_type_p (type))
25790 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25791 TREE_READONLY (value) = 1;
25792 return value;
25795 return objc_build_encode_expr (type);
25798 /* Parse an Objective-C @defs expression. */
25800 static tree
25801 cp_parser_objc_defs_expression (cp_parser *parser)
25803 tree name;
25805 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25806 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25807 name = cp_parser_identifier (parser);
25808 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25810 return objc_get_class_ivars (name);
25813 /* Parse an Objective-C protocol expression.
25815 objc-protocol-expression:
25816 @protocol ( identifier )
25818 Returns a representation of the protocol expression. */
25820 static tree
25821 cp_parser_objc_protocol_expression (cp_parser* parser)
25823 tree proto;
25825 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25826 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25827 proto = cp_parser_identifier (parser);
25828 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25830 return objc_build_protocol_expr (proto);
25833 /* Parse an Objective-C selector expression.
25835 objc-selector-expression:
25836 @selector ( objc-method-signature )
25838 objc-method-signature:
25839 objc-selector
25840 objc-selector-seq
25842 objc-selector-seq:
25843 objc-selector :
25844 objc-selector-seq objc-selector :
25846 Returns a representation of the method selector. */
25848 static tree
25849 cp_parser_objc_selector_expression (cp_parser* parser)
25851 tree sel_seq = NULL_TREE;
25852 bool maybe_unary_selector_p = true;
25853 cp_token *token;
25854 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25856 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25857 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25858 token = cp_lexer_peek_token (parser->lexer);
25860 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25861 || token->type == CPP_SCOPE)
25863 tree selector = NULL_TREE;
25865 if (token->type != CPP_COLON
25866 || token->type == CPP_SCOPE)
25867 selector = cp_parser_objc_selector (parser);
25869 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25870 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25872 /* Detect if we have a unary selector. */
25873 if (maybe_unary_selector_p)
25875 sel_seq = selector;
25876 goto finish_selector;
25878 else
25880 cp_parser_error (parser, "expected %<:%>");
25883 maybe_unary_selector_p = false;
25884 token = cp_lexer_consume_token (parser->lexer);
25886 if (token->type == CPP_SCOPE)
25888 sel_seq
25889 = chainon (sel_seq,
25890 build_tree_list (selector, NULL_TREE));
25891 sel_seq
25892 = chainon (sel_seq,
25893 build_tree_list (NULL_TREE, NULL_TREE));
25895 else
25896 sel_seq
25897 = chainon (sel_seq,
25898 build_tree_list (selector, NULL_TREE));
25900 token = cp_lexer_peek_token (parser->lexer);
25903 finish_selector:
25904 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25906 return objc_build_selector_expr (loc, sel_seq);
25909 /* Parse a list of identifiers.
25911 objc-identifier-list:
25912 identifier
25913 objc-identifier-list , identifier
25915 Returns a TREE_LIST of identifier nodes. */
25917 static tree
25918 cp_parser_objc_identifier_list (cp_parser* parser)
25920 tree identifier;
25921 tree list;
25922 cp_token *sep;
25924 identifier = cp_parser_identifier (parser);
25925 if (identifier == error_mark_node)
25926 return error_mark_node;
25928 list = build_tree_list (NULL_TREE, identifier);
25929 sep = cp_lexer_peek_token (parser->lexer);
25931 while (sep->type == CPP_COMMA)
25933 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25934 identifier = cp_parser_identifier (parser);
25935 if (identifier == error_mark_node)
25936 return list;
25938 list = chainon (list, build_tree_list (NULL_TREE,
25939 identifier));
25940 sep = cp_lexer_peek_token (parser->lexer);
25943 return list;
25946 /* Parse an Objective-C alias declaration.
25948 objc-alias-declaration:
25949 @compatibility_alias identifier identifier ;
25951 This function registers the alias mapping with the Objective-C front end.
25952 It returns nothing. */
25954 static void
25955 cp_parser_objc_alias_declaration (cp_parser* parser)
25957 tree alias, orig;
25959 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25960 alias = cp_parser_identifier (parser);
25961 orig = cp_parser_identifier (parser);
25962 objc_declare_alias (alias, orig);
25963 cp_parser_consume_semicolon_at_end_of_statement (parser);
25966 /* Parse an Objective-C class forward-declaration.
25968 objc-class-declaration:
25969 @class objc-identifier-list ;
25971 The function registers the forward declarations with the Objective-C
25972 front end. It returns nothing. */
25974 static void
25975 cp_parser_objc_class_declaration (cp_parser* parser)
25977 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25978 while (true)
25980 tree id;
25982 id = cp_parser_identifier (parser);
25983 if (id == error_mark_node)
25984 break;
25986 objc_declare_class (id);
25988 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25989 cp_lexer_consume_token (parser->lexer);
25990 else
25991 break;
25993 cp_parser_consume_semicolon_at_end_of_statement (parser);
25996 /* Parse a list of Objective-C protocol references.
25998 objc-protocol-refs-opt:
25999 objc-protocol-refs [opt]
26001 objc-protocol-refs:
26002 < objc-identifier-list >
26004 Returns a TREE_LIST of identifiers, if any. */
26006 static tree
26007 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26009 tree protorefs = NULL_TREE;
26011 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26013 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
26014 protorefs = cp_parser_objc_identifier_list (parser);
26015 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26018 return protorefs;
26021 /* Parse a Objective-C visibility specification. */
26023 static void
26024 cp_parser_objc_visibility_spec (cp_parser* parser)
26026 cp_token *vis = cp_lexer_peek_token (parser->lexer);
26028 switch (vis->keyword)
26030 case RID_AT_PRIVATE:
26031 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26032 break;
26033 case RID_AT_PROTECTED:
26034 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26035 break;
26036 case RID_AT_PUBLIC:
26037 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26038 break;
26039 case RID_AT_PACKAGE:
26040 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26041 break;
26042 default:
26043 return;
26046 /* Eat '@private'/'@protected'/'@public'. */
26047 cp_lexer_consume_token (parser->lexer);
26050 /* Parse an Objective-C method type. Return 'true' if it is a class
26051 (+) method, and 'false' if it is an instance (-) method. */
26053 static inline bool
26054 cp_parser_objc_method_type (cp_parser* parser)
26056 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26057 return true;
26058 else
26059 return false;
26062 /* Parse an Objective-C protocol qualifier. */
26064 static tree
26065 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26067 tree quals = NULL_TREE, node;
26068 cp_token *token = cp_lexer_peek_token (parser->lexer);
26070 node = token->u.value;
26072 while (node && identifier_p (node)
26073 && (node == ridpointers [(int) RID_IN]
26074 || node == ridpointers [(int) RID_OUT]
26075 || node == ridpointers [(int) RID_INOUT]
26076 || node == ridpointers [(int) RID_BYCOPY]
26077 || node == ridpointers [(int) RID_BYREF]
26078 || node == ridpointers [(int) RID_ONEWAY]))
26080 quals = tree_cons (NULL_TREE, node, quals);
26081 cp_lexer_consume_token (parser->lexer);
26082 token = cp_lexer_peek_token (parser->lexer);
26083 node = token->u.value;
26086 return quals;
26089 /* Parse an Objective-C typename. */
26091 static tree
26092 cp_parser_objc_typename (cp_parser* parser)
26094 tree type_name = NULL_TREE;
26096 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26098 tree proto_quals, cp_type = NULL_TREE;
26100 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26101 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26103 /* An ObjC type name may consist of just protocol qualifiers, in which
26104 case the type shall default to 'id'. */
26105 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26107 cp_type = cp_parser_type_id (parser);
26109 /* If the type could not be parsed, an error has already
26110 been produced. For error recovery, behave as if it had
26111 not been specified, which will use the default type
26112 'id'. */
26113 if (cp_type == error_mark_node)
26115 cp_type = NULL_TREE;
26116 /* We need to skip to the closing parenthesis as
26117 cp_parser_type_id() does not seem to do it for
26118 us. */
26119 cp_parser_skip_to_closing_parenthesis (parser,
26120 /*recovering=*/true,
26121 /*or_comma=*/false,
26122 /*consume_paren=*/false);
26126 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26127 type_name = build_tree_list (proto_quals, cp_type);
26130 return type_name;
26133 /* Check to see if TYPE refers to an Objective-C selector name. */
26135 static bool
26136 cp_parser_objc_selector_p (enum cpp_ttype type)
26138 return (type == CPP_NAME || type == CPP_KEYWORD
26139 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26140 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26141 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26142 || type == CPP_XOR || type == CPP_XOR_EQ);
26145 /* Parse an Objective-C selector. */
26147 static tree
26148 cp_parser_objc_selector (cp_parser* parser)
26150 cp_token *token = cp_lexer_consume_token (parser->lexer);
26152 if (!cp_parser_objc_selector_p (token->type))
26154 error_at (token->location, "invalid Objective-C++ selector name");
26155 return error_mark_node;
26158 /* C++ operator names are allowed to appear in ObjC selectors. */
26159 switch (token->type)
26161 case CPP_AND_AND: return get_identifier ("and");
26162 case CPP_AND_EQ: return get_identifier ("and_eq");
26163 case CPP_AND: return get_identifier ("bitand");
26164 case CPP_OR: return get_identifier ("bitor");
26165 case CPP_COMPL: return get_identifier ("compl");
26166 case CPP_NOT: return get_identifier ("not");
26167 case CPP_NOT_EQ: return get_identifier ("not_eq");
26168 case CPP_OR_OR: return get_identifier ("or");
26169 case CPP_OR_EQ: return get_identifier ("or_eq");
26170 case CPP_XOR: return get_identifier ("xor");
26171 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26172 default: return token->u.value;
26176 /* Parse an Objective-C params list. */
26178 static tree
26179 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26181 tree params = NULL_TREE;
26182 bool maybe_unary_selector_p = true;
26183 cp_token *token = cp_lexer_peek_token (parser->lexer);
26185 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26187 tree selector = NULL_TREE, type_name, identifier;
26188 tree parm_attr = NULL_TREE;
26190 if (token->keyword == RID_ATTRIBUTE)
26191 break;
26193 if (token->type != CPP_COLON)
26194 selector = cp_parser_objc_selector (parser);
26196 /* Detect if we have a unary selector. */
26197 if (maybe_unary_selector_p
26198 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26200 params = selector; /* Might be followed by attributes. */
26201 break;
26204 maybe_unary_selector_p = false;
26205 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26207 /* Something went quite wrong. There should be a colon
26208 here, but there is not. Stop parsing parameters. */
26209 break;
26211 type_name = cp_parser_objc_typename (parser);
26212 /* New ObjC allows attributes on parameters too. */
26213 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26214 parm_attr = cp_parser_attributes_opt (parser);
26215 identifier = cp_parser_identifier (parser);
26217 params
26218 = chainon (params,
26219 objc_build_keyword_decl (selector,
26220 type_name,
26221 identifier,
26222 parm_attr));
26224 token = cp_lexer_peek_token (parser->lexer);
26227 if (params == NULL_TREE)
26229 cp_parser_error (parser, "objective-c++ method declaration is expected");
26230 return error_mark_node;
26233 /* We allow tail attributes for the method. */
26234 if (token->keyword == RID_ATTRIBUTE)
26236 *attributes = cp_parser_attributes_opt (parser);
26237 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26238 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26239 return params;
26240 cp_parser_error (parser,
26241 "method attributes must be specified at the end");
26242 return error_mark_node;
26245 if (params == NULL_TREE)
26247 cp_parser_error (parser, "objective-c++ method declaration is expected");
26248 return error_mark_node;
26250 return params;
26253 /* Parse the non-keyword Objective-C params. */
26255 static tree
26256 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26257 tree* attributes)
26259 tree params = make_node (TREE_LIST);
26260 cp_token *token = cp_lexer_peek_token (parser->lexer);
26261 *ellipsisp = false; /* Initially, assume no ellipsis. */
26263 while (token->type == CPP_COMMA)
26265 cp_parameter_declarator *parmdecl;
26266 tree parm;
26268 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26269 token = cp_lexer_peek_token (parser->lexer);
26271 if (token->type == CPP_ELLIPSIS)
26273 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26274 *ellipsisp = true;
26275 token = cp_lexer_peek_token (parser->lexer);
26276 break;
26279 /* TODO: parse attributes for tail parameters. */
26280 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26281 parm = grokdeclarator (parmdecl->declarator,
26282 &parmdecl->decl_specifiers,
26283 PARM, /*initialized=*/0,
26284 /*attrlist=*/NULL);
26286 chainon (params, build_tree_list (NULL_TREE, parm));
26287 token = cp_lexer_peek_token (parser->lexer);
26290 /* We allow tail attributes for the method. */
26291 if (token->keyword == RID_ATTRIBUTE)
26293 if (*attributes == NULL_TREE)
26295 *attributes = cp_parser_attributes_opt (parser);
26296 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26297 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26298 return params;
26300 else
26301 /* We have an error, but parse the attributes, so that we can
26302 carry on. */
26303 *attributes = cp_parser_attributes_opt (parser);
26305 cp_parser_error (parser,
26306 "method attributes must be specified at the end");
26307 return error_mark_node;
26310 return params;
26313 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26315 static void
26316 cp_parser_objc_interstitial_code (cp_parser* parser)
26318 cp_token *token = cp_lexer_peek_token (parser->lexer);
26320 /* If the next token is `extern' and the following token is a string
26321 literal, then we have a linkage specification. */
26322 if (token->keyword == RID_EXTERN
26323 && cp_parser_is_pure_string_literal
26324 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26325 cp_parser_linkage_specification (parser);
26326 /* Handle #pragma, if any. */
26327 else if (token->type == CPP_PRAGMA)
26328 cp_parser_pragma (parser, pragma_objc_icode);
26329 /* Allow stray semicolons. */
26330 else if (token->type == CPP_SEMICOLON)
26331 cp_lexer_consume_token (parser->lexer);
26332 /* Mark methods as optional or required, when building protocols. */
26333 else if (token->keyword == RID_AT_OPTIONAL)
26335 cp_lexer_consume_token (parser->lexer);
26336 objc_set_method_opt (true);
26338 else if (token->keyword == RID_AT_REQUIRED)
26340 cp_lexer_consume_token (parser->lexer);
26341 objc_set_method_opt (false);
26343 else if (token->keyword == RID_NAMESPACE)
26344 cp_parser_namespace_definition (parser);
26345 /* Other stray characters must generate errors. */
26346 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26348 cp_lexer_consume_token (parser->lexer);
26349 error ("stray %qs between Objective-C++ methods",
26350 token->type == CPP_OPEN_BRACE ? "{" : "}");
26352 /* Finally, try to parse a block-declaration, or a function-definition. */
26353 else
26354 cp_parser_block_declaration (parser, /*statement_p=*/false);
26357 /* Parse a method signature. */
26359 static tree
26360 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26362 tree rettype, kwdparms, optparms;
26363 bool ellipsis = false;
26364 bool is_class_method;
26366 is_class_method = cp_parser_objc_method_type (parser);
26367 rettype = cp_parser_objc_typename (parser);
26368 *attributes = NULL_TREE;
26369 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26370 if (kwdparms == error_mark_node)
26371 return error_mark_node;
26372 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26373 if (optparms == error_mark_node)
26374 return error_mark_node;
26376 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26379 static bool
26380 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26382 tree tattr;
26383 cp_lexer_save_tokens (parser->lexer);
26384 tattr = cp_parser_attributes_opt (parser);
26385 gcc_assert (tattr) ;
26387 /* If the attributes are followed by a method introducer, this is not allowed.
26388 Dump the attributes and flag the situation. */
26389 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26390 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26391 return true;
26393 /* Otherwise, the attributes introduce some interstitial code, possibly so
26394 rewind to allow that check. */
26395 cp_lexer_rollback_tokens (parser->lexer);
26396 return false;
26399 /* Parse an Objective-C method prototype list. */
26401 static void
26402 cp_parser_objc_method_prototype_list (cp_parser* parser)
26404 cp_token *token = cp_lexer_peek_token (parser->lexer);
26406 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26408 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26410 tree attributes, sig;
26411 bool is_class_method;
26412 if (token->type == CPP_PLUS)
26413 is_class_method = true;
26414 else
26415 is_class_method = false;
26416 sig = cp_parser_objc_method_signature (parser, &attributes);
26417 if (sig == error_mark_node)
26419 cp_parser_skip_to_end_of_block_or_statement (parser);
26420 token = cp_lexer_peek_token (parser->lexer);
26421 continue;
26423 objc_add_method_declaration (is_class_method, sig, attributes);
26424 cp_parser_consume_semicolon_at_end_of_statement (parser);
26426 else if (token->keyword == RID_AT_PROPERTY)
26427 cp_parser_objc_at_property_declaration (parser);
26428 else if (token->keyword == RID_ATTRIBUTE
26429 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26430 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26431 OPT_Wattributes,
26432 "prefix attributes are ignored for methods");
26433 else
26434 /* Allow for interspersed non-ObjC++ code. */
26435 cp_parser_objc_interstitial_code (parser);
26437 token = cp_lexer_peek_token (parser->lexer);
26440 if (token->type != CPP_EOF)
26441 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26442 else
26443 cp_parser_error (parser, "expected %<@end%>");
26445 objc_finish_interface ();
26448 /* Parse an Objective-C method definition list. */
26450 static void
26451 cp_parser_objc_method_definition_list (cp_parser* parser)
26453 cp_token *token = cp_lexer_peek_token (parser->lexer);
26455 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26457 tree meth;
26459 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26461 cp_token *ptk;
26462 tree sig, attribute;
26463 bool is_class_method;
26464 if (token->type == CPP_PLUS)
26465 is_class_method = true;
26466 else
26467 is_class_method = false;
26468 push_deferring_access_checks (dk_deferred);
26469 sig = cp_parser_objc_method_signature (parser, &attribute);
26470 if (sig == error_mark_node)
26472 cp_parser_skip_to_end_of_block_or_statement (parser);
26473 token = cp_lexer_peek_token (parser->lexer);
26474 continue;
26476 objc_start_method_definition (is_class_method, sig, attribute,
26477 NULL_TREE);
26479 /* For historical reasons, we accept an optional semicolon. */
26480 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26481 cp_lexer_consume_token (parser->lexer);
26483 ptk = cp_lexer_peek_token (parser->lexer);
26484 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26485 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26487 perform_deferred_access_checks (tf_warning_or_error);
26488 stop_deferring_access_checks ();
26489 meth = cp_parser_function_definition_after_declarator (parser,
26490 false);
26491 pop_deferring_access_checks ();
26492 objc_finish_method_definition (meth);
26495 /* The following case will be removed once @synthesize is
26496 completely implemented. */
26497 else if (token->keyword == RID_AT_PROPERTY)
26498 cp_parser_objc_at_property_declaration (parser);
26499 else if (token->keyword == RID_AT_SYNTHESIZE)
26500 cp_parser_objc_at_synthesize_declaration (parser);
26501 else if (token->keyword == RID_AT_DYNAMIC)
26502 cp_parser_objc_at_dynamic_declaration (parser);
26503 else if (token->keyword == RID_ATTRIBUTE
26504 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26505 warning_at (token->location, OPT_Wattributes,
26506 "prefix attributes are ignored for methods");
26507 else
26508 /* Allow for interspersed non-ObjC++ code. */
26509 cp_parser_objc_interstitial_code (parser);
26511 token = cp_lexer_peek_token (parser->lexer);
26514 if (token->type != CPP_EOF)
26515 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26516 else
26517 cp_parser_error (parser, "expected %<@end%>");
26519 objc_finish_implementation ();
26522 /* Parse Objective-C ivars. */
26524 static void
26525 cp_parser_objc_class_ivars (cp_parser* parser)
26527 cp_token *token = cp_lexer_peek_token (parser->lexer);
26529 if (token->type != CPP_OPEN_BRACE)
26530 return; /* No ivars specified. */
26532 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26533 token = cp_lexer_peek_token (parser->lexer);
26535 while (token->type != CPP_CLOSE_BRACE
26536 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26538 cp_decl_specifier_seq declspecs;
26539 int decl_class_or_enum_p;
26540 tree prefix_attributes;
26542 cp_parser_objc_visibility_spec (parser);
26544 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26545 break;
26547 cp_parser_decl_specifier_seq (parser,
26548 CP_PARSER_FLAGS_OPTIONAL,
26549 &declspecs,
26550 &decl_class_or_enum_p);
26552 /* auto, register, static, extern, mutable. */
26553 if (declspecs.storage_class != sc_none)
26555 cp_parser_error (parser, "invalid type for instance variable");
26556 declspecs.storage_class = sc_none;
26559 /* thread_local. */
26560 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26562 cp_parser_error (parser, "invalid type for instance variable");
26563 declspecs.locations[ds_thread] = 0;
26566 /* typedef. */
26567 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26569 cp_parser_error (parser, "invalid type for instance variable");
26570 declspecs.locations[ds_typedef] = 0;
26573 prefix_attributes = declspecs.attributes;
26574 declspecs.attributes = NULL_TREE;
26576 /* Keep going until we hit the `;' at the end of the
26577 declaration. */
26578 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26580 tree width = NULL_TREE, attributes, first_attribute, decl;
26581 cp_declarator *declarator = NULL;
26582 int ctor_dtor_or_conv_p;
26584 /* Check for a (possibly unnamed) bitfield declaration. */
26585 token = cp_lexer_peek_token (parser->lexer);
26586 if (token->type == CPP_COLON)
26587 goto eat_colon;
26589 if (token->type == CPP_NAME
26590 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26591 == CPP_COLON))
26593 /* Get the name of the bitfield. */
26594 declarator = make_id_declarator (NULL_TREE,
26595 cp_parser_identifier (parser),
26596 sfk_none);
26598 eat_colon:
26599 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26600 /* Get the width of the bitfield. */
26601 width
26602 = cp_parser_constant_expression (parser);
26604 else
26606 /* Parse the declarator. */
26607 declarator
26608 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26609 &ctor_dtor_or_conv_p,
26610 /*parenthesized_p=*/NULL,
26611 /*member_p=*/false,
26612 /*friend_p=*/false);
26615 /* Look for attributes that apply to the ivar. */
26616 attributes = cp_parser_attributes_opt (parser);
26617 /* Remember which attributes are prefix attributes and
26618 which are not. */
26619 first_attribute = attributes;
26620 /* Combine the attributes. */
26621 attributes = chainon (prefix_attributes, attributes);
26623 if (width)
26624 /* Create the bitfield declaration. */
26625 decl = grokbitfield (declarator, &declspecs,
26626 width,
26627 attributes);
26628 else
26629 decl = grokfield (declarator, &declspecs,
26630 NULL_TREE, /*init_const_expr_p=*/false,
26631 NULL_TREE, attributes);
26633 /* Add the instance variable. */
26634 if (decl != error_mark_node && decl != NULL_TREE)
26635 objc_add_instance_variable (decl);
26637 /* Reset PREFIX_ATTRIBUTES. */
26638 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26639 attributes = TREE_CHAIN (attributes);
26640 if (attributes)
26641 TREE_CHAIN (attributes) = NULL_TREE;
26643 token = cp_lexer_peek_token (parser->lexer);
26645 if (token->type == CPP_COMMA)
26647 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26648 continue;
26650 break;
26653 cp_parser_consume_semicolon_at_end_of_statement (parser);
26654 token = cp_lexer_peek_token (parser->lexer);
26657 if (token->keyword == RID_AT_END)
26658 cp_parser_error (parser, "expected %<}%>");
26660 /* Do not consume the RID_AT_END, so it will be read again as terminating
26661 the @interface of @implementation. */
26662 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26663 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26665 /* For historical reasons, we accept an optional semicolon. */
26666 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26667 cp_lexer_consume_token (parser->lexer);
26670 /* Parse an Objective-C protocol declaration. */
26672 static void
26673 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26675 tree proto, protorefs;
26676 cp_token *tok;
26678 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26679 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26681 tok = cp_lexer_peek_token (parser->lexer);
26682 error_at (tok->location, "identifier expected after %<@protocol%>");
26683 cp_parser_consume_semicolon_at_end_of_statement (parser);
26684 return;
26687 /* See if we have a forward declaration or a definition. */
26688 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26690 /* Try a forward declaration first. */
26691 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26693 while (true)
26695 tree id;
26697 id = cp_parser_identifier (parser);
26698 if (id == error_mark_node)
26699 break;
26701 objc_declare_protocol (id, attributes);
26703 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26704 cp_lexer_consume_token (parser->lexer);
26705 else
26706 break;
26708 cp_parser_consume_semicolon_at_end_of_statement (parser);
26711 /* Ok, we got a full-fledged definition (or at least should). */
26712 else
26714 proto = cp_parser_identifier (parser);
26715 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26716 objc_start_protocol (proto, protorefs, attributes);
26717 cp_parser_objc_method_prototype_list (parser);
26721 /* Parse an Objective-C superclass or category. */
26723 static void
26724 cp_parser_objc_superclass_or_category (cp_parser *parser,
26725 bool iface_p,
26726 tree *super,
26727 tree *categ, bool *is_class_extension)
26729 cp_token *next = cp_lexer_peek_token (parser->lexer);
26731 *super = *categ = NULL_TREE;
26732 *is_class_extension = false;
26733 if (next->type == CPP_COLON)
26735 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26736 *super = cp_parser_identifier (parser);
26738 else if (next->type == CPP_OPEN_PAREN)
26740 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26742 /* If there is no category name, and this is an @interface, we
26743 have a class extension. */
26744 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26746 *categ = NULL_TREE;
26747 *is_class_extension = true;
26749 else
26750 *categ = cp_parser_identifier (parser);
26752 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26756 /* Parse an Objective-C class interface. */
26758 static void
26759 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26761 tree name, super, categ, protos;
26762 bool is_class_extension;
26764 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26765 name = cp_parser_identifier (parser);
26766 if (name == error_mark_node)
26768 /* It's hard to recover because even if valid @interface stuff
26769 is to follow, we can't compile it (or validate it) if we
26770 don't even know which class it refers to. Let's assume this
26771 was a stray '@interface' token in the stream and skip it.
26773 return;
26775 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26776 &is_class_extension);
26777 protos = cp_parser_objc_protocol_refs_opt (parser);
26779 /* We have either a class or a category on our hands. */
26780 if (categ || is_class_extension)
26781 objc_start_category_interface (name, categ, protos, attributes);
26782 else
26784 objc_start_class_interface (name, super, protos, attributes);
26785 /* Handle instance variable declarations, if any. */
26786 cp_parser_objc_class_ivars (parser);
26787 objc_continue_interface ();
26790 cp_parser_objc_method_prototype_list (parser);
26793 /* Parse an Objective-C class implementation. */
26795 static void
26796 cp_parser_objc_class_implementation (cp_parser* parser)
26798 tree name, super, categ;
26799 bool is_class_extension;
26801 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26802 name = cp_parser_identifier (parser);
26803 if (name == error_mark_node)
26805 /* It's hard to recover because even if valid @implementation
26806 stuff is to follow, we can't compile it (or validate it) if
26807 we don't even know which class it refers to. Let's assume
26808 this was a stray '@implementation' token in the stream and
26809 skip it.
26811 return;
26813 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26814 &is_class_extension);
26816 /* We have either a class or a category on our hands. */
26817 if (categ)
26818 objc_start_category_implementation (name, categ);
26819 else
26821 objc_start_class_implementation (name, super);
26822 /* Handle instance variable declarations, if any. */
26823 cp_parser_objc_class_ivars (parser);
26824 objc_continue_implementation ();
26827 cp_parser_objc_method_definition_list (parser);
26830 /* Consume the @end token and finish off the implementation. */
26832 static void
26833 cp_parser_objc_end_implementation (cp_parser* parser)
26835 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26836 objc_finish_implementation ();
26839 /* Parse an Objective-C declaration. */
26841 static void
26842 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26844 /* Try to figure out what kind of declaration is present. */
26845 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26847 if (attributes)
26848 switch (kwd->keyword)
26850 case RID_AT_ALIAS:
26851 case RID_AT_CLASS:
26852 case RID_AT_END:
26853 error_at (kwd->location, "attributes may not be specified before"
26854 " the %<@%D%> Objective-C++ keyword",
26855 kwd->u.value);
26856 attributes = NULL;
26857 break;
26858 case RID_AT_IMPLEMENTATION:
26859 warning_at (kwd->location, OPT_Wattributes,
26860 "prefix attributes are ignored before %<@%D%>",
26861 kwd->u.value);
26862 attributes = NULL;
26863 default:
26864 break;
26867 switch (kwd->keyword)
26869 case RID_AT_ALIAS:
26870 cp_parser_objc_alias_declaration (parser);
26871 break;
26872 case RID_AT_CLASS:
26873 cp_parser_objc_class_declaration (parser);
26874 break;
26875 case RID_AT_PROTOCOL:
26876 cp_parser_objc_protocol_declaration (parser, attributes);
26877 break;
26878 case RID_AT_INTERFACE:
26879 cp_parser_objc_class_interface (parser, attributes);
26880 break;
26881 case RID_AT_IMPLEMENTATION:
26882 cp_parser_objc_class_implementation (parser);
26883 break;
26884 case RID_AT_END:
26885 cp_parser_objc_end_implementation (parser);
26886 break;
26887 default:
26888 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26889 kwd->u.value);
26890 cp_parser_skip_to_end_of_block_or_statement (parser);
26894 /* Parse an Objective-C try-catch-finally statement.
26896 objc-try-catch-finally-stmt:
26897 @try compound-statement objc-catch-clause-seq [opt]
26898 objc-finally-clause [opt]
26900 objc-catch-clause-seq:
26901 objc-catch-clause objc-catch-clause-seq [opt]
26903 objc-catch-clause:
26904 @catch ( objc-exception-declaration ) compound-statement
26906 objc-finally-clause:
26907 @finally compound-statement
26909 objc-exception-declaration:
26910 parameter-declaration
26911 '...'
26913 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26915 Returns NULL_TREE.
26917 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26918 for C. Keep them in sync. */
26920 static tree
26921 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26923 location_t location;
26924 tree stmt;
26926 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26927 location = cp_lexer_peek_token (parser->lexer)->location;
26928 objc_maybe_warn_exceptions (location);
26929 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26930 node, lest it get absorbed into the surrounding block. */
26931 stmt = push_stmt_list ();
26932 cp_parser_compound_statement (parser, NULL, false, false);
26933 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26935 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26937 cp_parameter_declarator *parm;
26938 tree parameter_declaration = error_mark_node;
26939 bool seen_open_paren = false;
26941 cp_lexer_consume_token (parser->lexer);
26942 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26943 seen_open_paren = true;
26944 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26946 /* We have "@catch (...)" (where the '...' are literally
26947 what is in the code). Skip the '...'.
26948 parameter_declaration is set to NULL_TREE, and
26949 objc_being_catch_clauses() knows that that means
26950 '...'. */
26951 cp_lexer_consume_token (parser->lexer);
26952 parameter_declaration = NULL_TREE;
26954 else
26956 /* We have "@catch (NSException *exception)" or something
26957 like that. Parse the parameter declaration. */
26958 parm = cp_parser_parameter_declaration (parser, false, NULL);
26959 if (parm == NULL)
26960 parameter_declaration = error_mark_node;
26961 else
26962 parameter_declaration = grokdeclarator (parm->declarator,
26963 &parm->decl_specifiers,
26964 PARM, /*initialized=*/0,
26965 /*attrlist=*/NULL);
26967 if (seen_open_paren)
26968 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26969 else
26971 /* If there was no open parenthesis, we are recovering from
26972 an error, and we are trying to figure out what mistake
26973 the user has made. */
26975 /* If there is an immediate closing parenthesis, the user
26976 probably forgot the opening one (ie, they typed "@catch
26977 NSException *e)". Parse the closing parenthesis and keep
26978 going. */
26979 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26980 cp_lexer_consume_token (parser->lexer);
26982 /* If these is no immediate closing parenthesis, the user
26983 probably doesn't know that parenthesis are required at
26984 all (ie, they typed "@catch NSException *e"). So, just
26985 forget about the closing parenthesis and keep going. */
26987 objc_begin_catch_clause (parameter_declaration);
26988 cp_parser_compound_statement (parser, NULL, false, false);
26989 objc_finish_catch_clause ();
26991 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26993 cp_lexer_consume_token (parser->lexer);
26994 location = cp_lexer_peek_token (parser->lexer)->location;
26995 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26996 node, lest it get absorbed into the surrounding block. */
26997 stmt = push_stmt_list ();
26998 cp_parser_compound_statement (parser, NULL, false, false);
26999 objc_build_finally_clause (location, pop_stmt_list (stmt));
27002 return objc_finish_try_stmt ();
27005 /* Parse an Objective-C synchronized statement.
27007 objc-synchronized-stmt:
27008 @synchronized ( expression ) compound-statement
27010 Returns NULL_TREE. */
27012 static tree
27013 cp_parser_objc_synchronized_statement (cp_parser *parser)
27015 location_t location;
27016 tree lock, stmt;
27018 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27020 location = cp_lexer_peek_token (parser->lexer)->location;
27021 objc_maybe_warn_exceptions (location);
27022 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27023 lock = cp_parser_expression (parser);
27024 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27026 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27027 node, lest it get absorbed into the surrounding block. */
27028 stmt = push_stmt_list ();
27029 cp_parser_compound_statement (parser, NULL, false, false);
27031 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27034 /* Parse an Objective-C throw statement.
27036 objc-throw-stmt:
27037 @throw assignment-expression [opt] ;
27039 Returns a constructed '@throw' statement. */
27041 static tree
27042 cp_parser_objc_throw_statement (cp_parser *parser)
27044 tree expr = NULL_TREE;
27045 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27047 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27049 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27050 expr = cp_parser_expression (parser);
27052 cp_parser_consume_semicolon_at_end_of_statement (parser);
27054 return objc_build_throw_stmt (loc, expr);
27057 /* Parse an Objective-C statement. */
27059 static tree
27060 cp_parser_objc_statement (cp_parser * parser)
27062 /* Try to figure out what kind of declaration is present. */
27063 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27065 switch (kwd->keyword)
27067 case RID_AT_TRY:
27068 return cp_parser_objc_try_catch_finally_statement (parser);
27069 case RID_AT_SYNCHRONIZED:
27070 return cp_parser_objc_synchronized_statement (parser);
27071 case RID_AT_THROW:
27072 return cp_parser_objc_throw_statement (parser);
27073 default:
27074 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27075 kwd->u.value);
27076 cp_parser_skip_to_end_of_block_or_statement (parser);
27079 return error_mark_node;
27082 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27083 look ahead to see if an objc keyword follows the attributes. This
27084 is to detect the use of prefix attributes on ObjC @interface and
27085 @protocol. */
27087 static bool
27088 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27090 cp_lexer_save_tokens (parser->lexer);
27091 *attrib = cp_parser_attributes_opt (parser);
27092 gcc_assert (*attrib);
27093 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27095 cp_lexer_commit_tokens (parser->lexer);
27096 return true;
27098 cp_lexer_rollback_tokens (parser->lexer);
27099 return false;
27102 /* This routine is a minimal replacement for
27103 c_parser_struct_declaration () used when parsing the list of
27104 types/names or ObjC++ properties. For example, when parsing the
27105 code
27107 @property (readonly) int a, b, c;
27109 this function is responsible for parsing "int a, int b, int c" and
27110 returning the declarations as CHAIN of DECLs.
27112 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27113 similar parsing. */
27114 static tree
27115 cp_parser_objc_struct_declaration (cp_parser *parser)
27117 tree decls = NULL_TREE;
27118 cp_decl_specifier_seq declspecs;
27119 int decl_class_or_enum_p;
27120 tree prefix_attributes;
27122 cp_parser_decl_specifier_seq (parser,
27123 CP_PARSER_FLAGS_NONE,
27124 &declspecs,
27125 &decl_class_or_enum_p);
27127 if (declspecs.type == error_mark_node)
27128 return error_mark_node;
27130 /* auto, register, static, extern, mutable. */
27131 if (declspecs.storage_class != sc_none)
27133 cp_parser_error (parser, "invalid type for property");
27134 declspecs.storage_class = sc_none;
27137 /* thread_local. */
27138 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27140 cp_parser_error (parser, "invalid type for property");
27141 declspecs.locations[ds_thread] = 0;
27144 /* typedef. */
27145 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27147 cp_parser_error (parser, "invalid type for property");
27148 declspecs.locations[ds_typedef] = 0;
27151 prefix_attributes = declspecs.attributes;
27152 declspecs.attributes = NULL_TREE;
27154 /* Keep going until we hit the `;' at the end of the declaration. */
27155 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27157 tree attributes, first_attribute, decl;
27158 cp_declarator *declarator;
27159 cp_token *token;
27161 /* Parse the declarator. */
27162 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27163 NULL, NULL, false, false);
27165 /* Look for attributes that apply to the ivar. */
27166 attributes = cp_parser_attributes_opt (parser);
27167 /* Remember which attributes are prefix attributes and
27168 which are not. */
27169 first_attribute = attributes;
27170 /* Combine the attributes. */
27171 attributes = chainon (prefix_attributes, attributes);
27173 decl = grokfield (declarator, &declspecs,
27174 NULL_TREE, /*init_const_expr_p=*/false,
27175 NULL_TREE, attributes);
27177 if (decl == error_mark_node || decl == NULL_TREE)
27178 return error_mark_node;
27180 /* Reset PREFIX_ATTRIBUTES. */
27181 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27182 attributes = TREE_CHAIN (attributes);
27183 if (attributes)
27184 TREE_CHAIN (attributes) = NULL_TREE;
27186 DECL_CHAIN (decl) = decls;
27187 decls = decl;
27189 token = cp_lexer_peek_token (parser->lexer);
27190 if (token->type == CPP_COMMA)
27192 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27193 continue;
27195 else
27196 break;
27198 return decls;
27201 /* Parse an Objective-C @property declaration. The syntax is:
27203 objc-property-declaration:
27204 '@property' objc-property-attributes[opt] struct-declaration ;
27206 objc-property-attributes:
27207 '(' objc-property-attribute-list ')'
27209 objc-property-attribute-list:
27210 objc-property-attribute
27211 objc-property-attribute-list, objc-property-attribute
27213 objc-property-attribute
27214 'getter' = identifier
27215 'setter' = identifier
27216 'readonly'
27217 'readwrite'
27218 'assign'
27219 'retain'
27220 'copy'
27221 'nonatomic'
27223 For example:
27224 @property NSString *name;
27225 @property (readonly) id object;
27226 @property (retain, nonatomic, getter=getTheName) id name;
27227 @property int a, b, c;
27229 PS: This function is identical to
27230 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27231 static void
27232 cp_parser_objc_at_property_declaration (cp_parser *parser)
27234 /* The following variables hold the attributes of the properties as
27235 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27236 seen. When we see an attribute, we set them to 'true' (if they
27237 are boolean properties) or to the identifier (if they have an
27238 argument, ie, for getter and setter). Note that here we only
27239 parse the list of attributes, check the syntax and accumulate the
27240 attributes that we find. objc_add_property_declaration() will
27241 then process the information. */
27242 bool property_assign = false;
27243 bool property_copy = false;
27244 tree property_getter_ident = NULL_TREE;
27245 bool property_nonatomic = false;
27246 bool property_readonly = false;
27247 bool property_readwrite = false;
27248 bool property_retain = false;
27249 tree property_setter_ident = NULL_TREE;
27251 /* 'properties' is the list of properties that we read. Usually a
27252 single one, but maybe more (eg, in "@property int a, b, c;" there
27253 are three). */
27254 tree properties;
27255 location_t loc;
27257 loc = cp_lexer_peek_token (parser->lexer)->location;
27259 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27261 /* Parse the optional attribute list... */
27262 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27264 /* Eat the '('. */
27265 cp_lexer_consume_token (parser->lexer);
27267 while (true)
27269 bool syntax_error = false;
27270 cp_token *token = cp_lexer_peek_token (parser->lexer);
27271 enum rid keyword;
27273 if (token->type != CPP_NAME)
27275 cp_parser_error (parser, "expected identifier");
27276 break;
27278 keyword = C_RID_CODE (token->u.value);
27279 cp_lexer_consume_token (parser->lexer);
27280 switch (keyword)
27282 case RID_ASSIGN: property_assign = true; break;
27283 case RID_COPY: property_copy = true; break;
27284 case RID_NONATOMIC: property_nonatomic = true; break;
27285 case RID_READONLY: property_readonly = true; break;
27286 case RID_READWRITE: property_readwrite = true; break;
27287 case RID_RETAIN: property_retain = true; break;
27289 case RID_GETTER:
27290 case RID_SETTER:
27291 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27293 if (keyword == RID_GETTER)
27294 cp_parser_error (parser,
27295 "missing %<=%> (after %<getter%> attribute)");
27296 else
27297 cp_parser_error (parser,
27298 "missing %<=%> (after %<setter%> attribute)");
27299 syntax_error = true;
27300 break;
27302 cp_lexer_consume_token (parser->lexer); /* eat the = */
27303 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27305 cp_parser_error (parser, "expected identifier");
27306 syntax_error = true;
27307 break;
27309 if (keyword == RID_SETTER)
27311 if (property_setter_ident != NULL_TREE)
27313 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27314 cp_lexer_consume_token (parser->lexer);
27316 else
27317 property_setter_ident = cp_parser_objc_selector (parser);
27318 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27319 cp_parser_error (parser, "setter name must terminate with %<:%>");
27320 else
27321 cp_lexer_consume_token (parser->lexer);
27323 else
27325 if (property_getter_ident != NULL_TREE)
27327 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27328 cp_lexer_consume_token (parser->lexer);
27330 else
27331 property_getter_ident = cp_parser_objc_selector (parser);
27333 break;
27334 default:
27335 cp_parser_error (parser, "unknown property attribute");
27336 syntax_error = true;
27337 break;
27340 if (syntax_error)
27341 break;
27343 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27344 cp_lexer_consume_token (parser->lexer);
27345 else
27346 break;
27349 /* FIXME: "@property (setter, assign);" will generate a spurious
27350 "error: expected ‘)’ before ‘,’ token". This is because
27351 cp_parser_require, unlike the C counterpart, will produce an
27352 error even if we are in error recovery. */
27353 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27355 cp_parser_skip_to_closing_parenthesis (parser,
27356 /*recovering=*/true,
27357 /*or_comma=*/false,
27358 /*consume_paren=*/true);
27362 /* ... and the property declaration(s). */
27363 properties = cp_parser_objc_struct_declaration (parser);
27365 if (properties == error_mark_node)
27367 cp_parser_skip_to_end_of_statement (parser);
27368 /* If the next token is now a `;', consume it. */
27369 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27370 cp_lexer_consume_token (parser->lexer);
27371 return;
27374 if (properties == NULL_TREE)
27375 cp_parser_error (parser, "expected identifier");
27376 else
27378 /* Comma-separated properties are chained together in
27379 reverse order; add them one by one. */
27380 properties = nreverse (properties);
27382 for (; properties; properties = TREE_CHAIN (properties))
27383 objc_add_property_declaration (loc, copy_node (properties),
27384 property_readonly, property_readwrite,
27385 property_assign, property_retain,
27386 property_copy, property_nonatomic,
27387 property_getter_ident, property_setter_ident);
27390 cp_parser_consume_semicolon_at_end_of_statement (parser);
27393 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27395 objc-synthesize-declaration:
27396 @synthesize objc-synthesize-identifier-list ;
27398 objc-synthesize-identifier-list:
27399 objc-synthesize-identifier
27400 objc-synthesize-identifier-list, objc-synthesize-identifier
27402 objc-synthesize-identifier
27403 identifier
27404 identifier = identifier
27406 For example:
27407 @synthesize MyProperty;
27408 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27410 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27411 for C. Keep them in sync.
27413 static void
27414 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27416 tree list = NULL_TREE;
27417 location_t loc;
27418 loc = cp_lexer_peek_token (parser->lexer)->location;
27420 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27421 while (true)
27423 tree property, ivar;
27424 property = cp_parser_identifier (parser);
27425 if (property == error_mark_node)
27427 cp_parser_consume_semicolon_at_end_of_statement (parser);
27428 return;
27430 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27432 cp_lexer_consume_token (parser->lexer);
27433 ivar = cp_parser_identifier (parser);
27434 if (ivar == error_mark_node)
27436 cp_parser_consume_semicolon_at_end_of_statement (parser);
27437 return;
27440 else
27441 ivar = NULL_TREE;
27442 list = chainon (list, build_tree_list (ivar, property));
27443 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27444 cp_lexer_consume_token (parser->lexer);
27445 else
27446 break;
27448 cp_parser_consume_semicolon_at_end_of_statement (parser);
27449 objc_add_synthesize_declaration (loc, list);
27452 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27454 objc-dynamic-declaration:
27455 @dynamic identifier-list ;
27457 For example:
27458 @dynamic MyProperty;
27459 @dynamic MyProperty, AnotherProperty;
27461 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27462 for C. Keep them in sync.
27464 static void
27465 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27467 tree list = NULL_TREE;
27468 location_t loc;
27469 loc = cp_lexer_peek_token (parser->lexer)->location;
27471 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27472 while (true)
27474 tree property;
27475 property = cp_parser_identifier (parser);
27476 if (property == error_mark_node)
27478 cp_parser_consume_semicolon_at_end_of_statement (parser);
27479 return;
27481 list = chainon (list, build_tree_list (NULL, property));
27482 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27483 cp_lexer_consume_token (parser->lexer);
27484 else
27485 break;
27487 cp_parser_consume_semicolon_at_end_of_statement (parser);
27488 objc_add_dynamic_declaration (loc, list);
27492 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27494 /* Returns name of the next clause.
27495 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27496 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27497 returned and the token is consumed. */
27499 static pragma_omp_clause
27500 cp_parser_omp_clause_name (cp_parser *parser)
27502 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27504 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27505 result = PRAGMA_OMP_CLAUSE_IF;
27506 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27507 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27508 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27509 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27510 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27511 result = PRAGMA_OMP_CLAUSE_FOR;
27512 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27514 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27515 const char *p = IDENTIFIER_POINTER (id);
27517 switch (p[0])
27519 case 'a':
27520 if (!strcmp ("aligned", p))
27521 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27522 break;
27523 case 'c':
27524 if (!strcmp ("collapse", p))
27525 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27526 else if (!strcmp ("copyin", p))
27527 result = PRAGMA_OMP_CLAUSE_COPYIN;
27528 else if (!strcmp ("copyprivate", p))
27529 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27530 break;
27531 case 'd':
27532 if (!strcmp ("depend", p))
27533 result = PRAGMA_OMP_CLAUSE_DEPEND;
27534 else if (!strcmp ("device", p))
27535 result = PRAGMA_OMP_CLAUSE_DEVICE;
27536 else if (!strcmp ("dist_schedule", p))
27537 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27538 break;
27539 case 'f':
27540 if (!strcmp ("final", p))
27541 result = PRAGMA_OMP_CLAUSE_FINAL;
27542 else if (!strcmp ("firstprivate", p))
27543 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27544 else if (!strcmp ("from", p))
27545 result = PRAGMA_OMP_CLAUSE_FROM;
27546 break;
27547 case 'i':
27548 if (!strcmp ("inbranch", p))
27549 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27550 break;
27551 case 'l':
27552 if (!strcmp ("lastprivate", p))
27553 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27554 else if (!strcmp ("linear", p))
27555 result = PRAGMA_OMP_CLAUSE_LINEAR;
27556 break;
27557 case 'm':
27558 if (!strcmp ("map", p))
27559 result = PRAGMA_OMP_CLAUSE_MAP;
27560 else if (!strcmp ("mergeable", p))
27561 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27562 else if (flag_cilkplus && !strcmp ("mask", p))
27563 result = PRAGMA_CILK_CLAUSE_MASK;
27564 break;
27565 case 'n':
27566 if (!strcmp ("notinbranch", p))
27567 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27568 else if (!strcmp ("nowait", p))
27569 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27570 else if (flag_cilkplus && !strcmp ("nomask", p))
27571 result = PRAGMA_CILK_CLAUSE_NOMASK;
27572 else if (!strcmp ("num_teams", p))
27573 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27574 else if (!strcmp ("num_threads", p))
27575 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27576 break;
27577 case 'o':
27578 if (!strcmp ("ordered", p))
27579 result = PRAGMA_OMP_CLAUSE_ORDERED;
27580 break;
27581 case 'p':
27582 if (!strcmp ("parallel", p))
27583 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27584 else if (!strcmp ("proc_bind", p))
27585 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27586 break;
27587 case 'r':
27588 if (!strcmp ("reduction", p))
27589 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27590 break;
27591 case 's':
27592 if (!strcmp ("safelen", p))
27593 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27594 else if (!strcmp ("schedule", p))
27595 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27596 else if (!strcmp ("sections", p))
27597 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27598 else if (!strcmp ("shared", p))
27599 result = PRAGMA_OMP_CLAUSE_SHARED;
27600 else if (!strcmp ("simdlen", p))
27601 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27602 break;
27603 case 't':
27604 if (!strcmp ("taskgroup", p))
27605 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27606 else if (!strcmp ("thread_limit", p))
27607 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27608 else if (!strcmp ("to", p))
27609 result = PRAGMA_OMP_CLAUSE_TO;
27610 break;
27611 case 'u':
27612 if (!strcmp ("uniform", p))
27613 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27614 else if (!strcmp ("untied", p))
27615 result = PRAGMA_OMP_CLAUSE_UNTIED;
27616 break;
27617 case 'v':
27618 if (flag_cilkplus && !strcmp ("vectorlength", p))
27619 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27620 break;
27624 if (result != PRAGMA_OMP_CLAUSE_NONE)
27625 cp_lexer_consume_token (parser->lexer);
27627 return result;
27630 /* Validate that a clause of the given type does not already exist. */
27632 static void
27633 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27634 const char *name, location_t location)
27636 tree c;
27638 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27639 if (OMP_CLAUSE_CODE (c) == code)
27641 error_at (location, "too many %qs clauses", name);
27642 break;
27646 /* OpenMP 2.5:
27647 variable-list:
27648 identifier
27649 variable-list , identifier
27651 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27652 colon). An opening parenthesis will have been consumed by the caller.
27654 If KIND is nonzero, create the appropriate node and install the decl
27655 in OMP_CLAUSE_DECL and add the node to the head of the list.
27657 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27658 return the list created.
27660 COLON can be NULL if only closing parenthesis should end the list,
27661 or pointer to bool which will receive false if the list is terminated
27662 by closing parenthesis or true if the list is terminated by colon. */
27664 static tree
27665 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27666 tree list, bool *colon)
27668 cp_token *token;
27669 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27670 if (colon)
27672 parser->colon_corrects_to_scope_p = false;
27673 *colon = false;
27675 while (1)
27677 tree name, decl;
27679 token = cp_lexer_peek_token (parser->lexer);
27680 name = cp_parser_id_expression (parser, /*template_p=*/false,
27681 /*check_dependency_p=*/true,
27682 /*template_p=*/NULL,
27683 /*declarator_p=*/false,
27684 /*optional_p=*/false);
27685 if (name == error_mark_node)
27686 goto skip_comma;
27688 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27689 if (decl == error_mark_node)
27690 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27691 token->location);
27692 else if (kind != 0)
27694 switch (kind)
27696 case OMP_CLAUSE_MAP:
27697 case OMP_CLAUSE_FROM:
27698 case OMP_CLAUSE_TO:
27699 case OMP_CLAUSE_DEPEND:
27700 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27702 tree low_bound = NULL_TREE, length = NULL_TREE;
27704 parser->colon_corrects_to_scope_p = false;
27705 cp_lexer_consume_token (parser->lexer);
27706 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27707 low_bound = cp_parser_expression (parser);
27708 if (!colon)
27709 parser->colon_corrects_to_scope_p
27710 = saved_colon_corrects_to_scope_p;
27711 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27712 length = integer_one_node;
27713 else
27715 /* Look for `:'. */
27716 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27717 goto skip_comma;
27718 if (!cp_lexer_next_token_is (parser->lexer,
27719 CPP_CLOSE_SQUARE))
27720 length = cp_parser_expression (parser);
27722 /* Look for the closing `]'. */
27723 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27724 RT_CLOSE_SQUARE))
27725 goto skip_comma;
27726 decl = tree_cons (low_bound, length, decl);
27728 break;
27729 default:
27730 break;
27733 tree u = build_omp_clause (token->location, kind);
27734 OMP_CLAUSE_DECL (u) = decl;
27735 OMP_CLAUSE_CHAIN (u) = list;
27736 list = u;
27738 else
27739 list = tree_cons (decl, NULL_TREE, list);
27741 get_comma:
27742 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27743 break;
27744 cp_lexer_consume_token (parser->lexer);
27747 if (colon)
27748 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27750 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27752 *colon = true;
27753 cp_parser_require (parser, CPP_COLON, RT_COLON);
27754 return list;
27757 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27759 int ending;
27761 /* Try to resync to an unnested comma. Copied from
27762 cp_parser_parenthesized_expression_list. */
27763 skip_comma:
27764 if (colon)
27765 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27766 ending = cp_parser_skip_to_closing_parenthesis (parser,
27767 /*recovering=*/true,
27768 /*or_comma=*/true,
27769 /*consume_paren=*/true);
27770 if (ending < 0)
27771 goto get_comma;
27774 return list;
27777 /* Similarly, but expect leading and trailing parenthesis. This is a very
27778 common case for omp clauses. */
27780 static tree
27781 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27783 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27784 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27785 return list;
27788 /* OpenMP 3.0:
27789 collapse ( constant-expression ) */
27791 static tree
27792 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27794 tree c, num;
27795 location_t loc;
27796 HOST_WIDE_INT n;
27798 loc = cp_lexer_peek_token (parser->lexer)->location;
27799 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27800 return list;
27802 num = cp_parser_constant_expression (parser);
27804 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27805 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27806 /*or_comma=*/false,
27807 /*consume_paren=*/true);
27809 if (num == error_mark_node)
27810 return list;
27811 num = fold_non_dependent_expr (num);
27812 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27813 || !tree_fits_shwi_p (num)
27814 || (n = tree_to_shwi (num)) <= 0
27815 || (int) n != n)
27817 error_at (loc, "collapse argument needs positive constant integer expression");
27818 return list;
27821 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27822 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27823 OMP_CLAUSE_CHAIN (c) = list;
27824 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27826 return c;
27829 /* OpenMP 2.5:
27830 default ( shared | none ) */
27832 static tree
27833 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27835 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27836 tree c;
27838 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27839 return list;
27840 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27842 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27843 const char *p = IDENTIFIER_POINTER (id);
27845 switch (p[0])
27847 case 'n':
27848 if (strcmp ("none", p) != 0)
27849 goto invalid_kind;
27850 kind = OMP_CLAUSE_DEFAULT_NONE;
27851 break;
27853 case 's':
27854 if (strcmp ("shared", p) != 0)
27855 goto invalid_kind;
27856 kind = OMP_CLAUSE_DEFAULT_SHARED;
27857 break;
27859 default:
27860 goto invalid_kind;
27863 cp_lexer_consume_token (parser->lexer);
27865 else
27867 invalid_kind:
27868 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27871 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27872 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27873 /*or_comma=*/false,
27874 /*consume_paren=*/true);
27876 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27877 return list;
27879 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27880 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27881 OMP_CLAUSE_CHAIN (c) = list;
27882 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27884 return c;
27887 /* OpenMP 3.1:
27888 final ( expression ) */
27890 static tree
27891 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27893 tree t, c;
27895 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27896 return list;
27898 t = cp_parser_condition (parser);
27900 if (t == error_mark_node
27901 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27902 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27903 /*or_comma=*/false,
27904 /*consume_paren=*/true);
27906 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27908 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27909 OMP_CLAUSE_FINAL_EXPR (c) = t;
27910 OMP_CLAUSE_CHAIN (c) = list;
27912 return c;
27915 /* OpenMP 2.5:
27916 if ( expression ) */
27918 static tree
27919 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27921 tree t, c;
27923 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27924 return list;
27926 t = cp_parser_condition (parser);
27928 if (t == error_mark_node
27929 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27930 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27931 /*or_comma=*/false,
27932 /*consume_paren=*/true);
27934 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27936 c = build_omp_clause (location, OMP_CLAUSE_IF);
27937 OMP_CLAUSE_IF_EXPR (c) = t;
27938 OMP_CLAUSE_CHAIN (c) = list;
27940 return c;
27943 /* OpenMP 3.1:
27944 mergeable */
27946 static tree
27947 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27948 tree list, location_t location)
27950 tree c;
27952 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27953 location);
27955 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27956 OMP_CLAUSE_CHAIN (c) = list;
27957 return c;
27960 /* OpenMP 2.5:
27961 nowait */
27963 static tree
27964 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27965 tree list, location_t location)
27967 tree c;
27969 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27971 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27972 OMP_CLAUSE_CHAIN (c) = list;
27973 return c;
27976 /* OpenMP 2.5:
27977 num_threads ( expression ) */
27979 static tree
27980 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27981 location_t location)
27983 tree t, c;
27985 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27986 return list;
27988 t = cp_parser_expression (parser);
27990 if (t == error_mark_node
27991 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27992 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27993 /*or_comma=*/false,
27994 /*consume_paren=*/true);
27996 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27997 "num_threads", location);
27999 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28000 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28001 OMP_CLAUSE_CHAIN (c) = list;
28003 return c;
28006 /* OpenMP 2.5:
28007 ordered */
28009 static tree
28010 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28011 tree list, location_t location)
28013 tree c;
28015 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28016 "ordered", location);
28018 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28019 OMP_CLAUSE_CHAIN (c) = list;
28020 return c;
28023 /* OpenMP 2.5:
28024 reduction ( reduction-operator : variable-list )
28026 reduction-operator:
28027 One of: + * - & ^ | && ||
28029 OpenMP 3.1:
28031 reduction-operator:
28032 One of: + * - & ^ | && || min max
28034 OpenMP 4.0:
28036 reduction-operator:
28037 One of: + * - & ^ | && ||
28038 id-expression */
28040 static tree
28041 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28043 enum tree_code code = ERROR_MARK;
28044 tree nlist, c, id = NULL_TREE;
28046 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28047 return list;
28049 switch (cp_lexer_peek_token (parser->lexer)->type)
28051 case CPP_PLUS: code = PLUS_EXPR; break;
28052 case CPP_MULT: code = MULT_EXPR; break;
28053 case CPP_MINUS: code = MINUS_EXPR; break;
28054 case CPP_AND: code = BIT_AND_EXPR; break;
28055 case CPP_XOR: code = BIT_XOR_EXPR; break;
28056 case CPP_OR: code = BIT_IOR_EXPR; break;
28057 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28058 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28059 default: break;
28062 if (code != ERROR_MARK)
28063 cp_lexer_consume_token (parser->lexer);
28064 else
28066 bool saved_colon_corrects_to_scope_p;
28067 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28068 parser->colon_corrects_to_scope_p = false;
28069 id = cp_parser_id_expression (parser, /*template_p=*/false,
28070 /*check_dependency_p=*/true,
28071 /*template_p=*/NULL,
28072 /*declarator_p=*/false,
28073 /*optional_p=*/false);
28074 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28075 if (identifier_p (id))
28077 const char *p = IDENTIFIER_POINTER (id);
28079 if (strcmp (p, "min") == 0)
28080 code = MIN_EXPR;
28081 else if (strcmp (p, "max") == 0)
28082 code = MAX_EXPR;
28083 else if (id == ansi_opname (PLUS_EXPR))
28084 code = PLUS_EXPR;
28085 else if (id == ansi_opname (MULT_EXPR))
28086 code = MULT_EXPR;
28087 else if (id == ansi_opname (MINUS_EXPR))
28088 code = MINUS_EXPR;
28089 else if (id == ansi_opname (BIT_AND_EXPR))
28090 code = BIT_AND_EXPR;
28091 else if (id == ansi_opname (BIT_IOR_EXPR))
28092 code = BIT_IOR_EXPR;
28093 else if (id == ansi_opname (BIT_XOR_EXPR))
28094 code = BIT_XOR_EXPR;
28095 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28096 code = TRUTH_ANDIF_EXPR;
28097 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28098 code = TRUTH_ORIF_EXPR;
28099 id = omp_reduction_id (code, id, NULL_TREE);
28100 tree scope = parser->scope;
28101 if (scope)
28102 id = build_qualified_name (NULL_TREE, scope, id, false);
28103 parser->scope = NULL_TREE;
28104 parser->qualifying_scope = NULL_TREE;
28105 parser->object_scope = NULL_TREE;
28107 else
28109 error ("invalid reduction-identifier");
28110 resync_fail:
28111 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28112 /*or_comma=*/false,
28113 /*consume_paren=*/true);
28114 return list;
28118 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28119 goto resync_fail;
28121 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28122 NULL);
28123 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28125 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28126 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28129 return nlist;
28132 /* OpenMP 2.5:
28133 schedule ( schedule-kind )
28134 schedule ( schedule-kind , expression )
28136 schedule-kind:
28137 static | dynamic | guided | runtime | auto */
28139 static tree
28140 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28142 tree c, t;
28144 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28145 return list;
28147 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28149 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28151 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28152 const char *p = IDENTIFIER_POINTER (id);
28154 switch (p[0])
28156 case 'd':
28157 if (strcmp ("dynamic", p) != 0)
28158 goto invalid_kind;
28159 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28160 break;
28162 case 'g':
28163 if (strcmp ("guided", p) != 0)
28164 goto invalid_kind;
28165 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28166 break;
28168 case 'r':
28169 if (strcmp ("runtime", p) != 0)
28170 goto invalid_kind;
28171 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28172 break;
28174 default:
28175 goto invalid_kind;
28178 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28179 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28180 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28181 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28182 else
28183 goto invalid_kind;
28184 cp_lexer_consume_token (parser->lexer);
28186 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28188 cp_token *token;
28189 cp_lexer_consume_token (parser->lexer);
28191 token = cp_lexer_peek_token (parser->lexer);
28192 t = cp_parser_assignment_expression (parser);
28194 if (t == error_mark_node)
28195 goto resync_fail;
28196 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28197 error_at (token->location, "schedule %<runtime%> does not take "
28198 "a %<chunk_size%> parameter");
28199 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28200 error_at (token->location, "schedule %<auto%> does not take "
28201 "a %<chunk_size%> parameter");
28202 else
28203 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28205 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28206 goto resync_fail;
28208 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28209 goto resync_fail;
28211 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28212 OMP_CLAUSE_CHAIN (c) = list;
28213 return c;
28215 invalid_kind:
28216 cp_parser_error (parser, "invalid schedule kind");
28217 resync_fail:
28218 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28219 /*or_comma=*/false,
28220 /*consume_paren=*/true);
28221 return list;
28224 /* OpenMP 3.0:
28225 untied */
28227 static tree
28228 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28229 tree list, location_t location)
28231 tree c;
28233 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28235 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28236 OMP_CLAUSE_CHAIN (c) = list;
28237 return c;
28240 /* OpenMP 4.0:
28241 inbranch
28242 notinbranch */
28244 static tree
28245 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28246 tree list, location_t location)
28248 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28249 tree c = build_omp_clause (location, code);
28250 OMP_CLAUSE_CHAIN (c) = list;
28251 return c;
28254 /* OpenMP 4.0:
28255 parallel
28257 sections
28258 taskgroup */
28260 static tree
28261 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28262 enum omp_clause_code code,
28263 tree list, location_t location)
28265 tree c = build_omp_clause (location, code);
28266 OMP_CLAUSE_CHAIN (c) = list;
28267 return c;
28270 /* OpenMP 4.0:
28271 num_teams ( expression ) */
28273 static tree
28274 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28275 location_t location)
28277 tree t, c;
28279 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28280 return list;
28282 t = cp_parser_expression (parser);
28284 if (t == error_mark_node
28285 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28286 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28287 /*or_comma=*/false,
28288 /*consume_paren=*/true);
28290 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28291 "num_teams", location);
28293 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28294 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28295 OMP_CLAUSE_CHAIN (c) = list;
28297 return c;
28300 /* OpenMP 4.0:
28301 thread_limit ( expression ) */
28303 static tree
28304 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28305 location_t location)
28307 tree t, c;
28309 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28310 return list;
28312 t = cp_parser_expression (parser);
28314 if (t == error_mark_node
28315 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28316 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28317 /*or_comma=*/false,
28318 /*consume_paren=*/true);
28320 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28321 "thread_limit", location);
28323 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28324 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28325 OMP_CLAUSE_CHAIN (c) = list;
28327 return c;
28330 /* OpenMP 4.0:
28331 aligned ( variable-list )
28332 aligned ( variable-list : constant-expression ) */
28334 static tree
28335 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28337 tree nlist, c, alignment = NULL_TREE;
28338 bool colon;
28340 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28341 return list;
28343 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28344 &colon);
28346 if (colon)
28348 alignment = cp_parser_constant_expression (parser);
28350 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28351 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28352 /*or_comma=*/false,
28353 /*consume_paren=*/true);
28355 if (alignment == error_mark_node)
28356 alignment = NULL_TREE;
28359 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28360 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28362 return nlist;
28365 /* OpenMP 4.0:
28366 linear ( variable-list )
28367 linear ( variable-list : expression ) */
28369 static tree
28370 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28371 bool is_cilk_simd_fn)
28373 tree nlist, c, step = integer_one_node;
28374 bool colon;
28376 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28377 return list;
28379 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28380 &colon);
28382 if (colon)
28384 step = cp_parser_expression (parser);
28386 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28388 sorry ("using parameters for %<linear%> step is not supported yet");
28389 step = integer_one_node;
28391 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28392 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28393 /*or_comma=*/false,
28394 /*consume_paren=*/true);
28396 if (step == error_mark_node)
28397 return list;
28400 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28401 OMP_CLAUSE_LINEAR_STEP (c) = step;
28403 return nlist;
28406 /* OpenMP 4.0:
28407 safelen ( constant-expression ) */
28409 static tree
28410 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28411 location_t location)
28413 tree t, c;
28415 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28416 return list;
28418 t = cp_parser_constant_expression (parser);
28420 if (t == error_mark_node
28421 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28422 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28423 /*or_comma=*/false,
28424 /*consume_paren=*/true);
28426 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28428 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28429 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28430 OMP_CLAUSE_CHAIN (c) = list;
28432 return c;
28435 /* OpenMP 4.0:
28436 simdlen ( constant-expression ) */
28438 static tree
28439 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28440 location_t location)
28442 tree t, c;
28444 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28445 return list;
28447 t = cp_parser_constant_expression (parser);
28449 if (t == error_mark_node
28450 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28451 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28452 /*or_comma=*/false,
28453 /*consume_paren=*/true);
28455 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28457 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28458 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28459 OMP_CLAUSE_CHAIN (c) = list;
28461 return c;
28464 /* OpenMP 4.0:
28465 depend ( depend-kind : variable-list )
28467 depend-kind:
28468 in | out | inout */
28470 static tree
28471 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28473 tree nlist, c;
28474 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28476 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28477 return list;
28479 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28481 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28482 const char *p = IDENTIFIER_POINTER (id);
28484 if (strcmp ("in", p) == 0)
28485 kind = OMP_CLAUSE_DEPEND_IN;
28486 else if (strcmp ("inout", p) == 0)
28487 kind = OMP_CLAUSE_DEPEND_INOUT;
28488 else if (strcmp ("out", p) == 0)
28489 kind = OMP_CLAUSE_DEPEND_OUT;
28490 else
28491 goto invalid_kind;
28493 else
28494 goto invalid_kind;
28496 cp_lexer_consume_token (parser->lexer);
28497 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28498 goto resync_fail;
28500 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28501 NULL);
28503 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28504 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28506 return nlist;
28508 invalid_kind:
28509 cp_parser_error (parser, "invalid depend kind");
28510 resync_fail:
28511 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28512 /*or_comma=*/false,
28513 /*consume_paren=*/true);
28514 return list;
28517 /* OpenMP 4.0:
28518 map ( map-kind : variable-list )
28519 map ( variable-list )
28521 map-kind:
28522 alloc | to | from | tofrom */
28524 static tree
28525 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28527 tree nlist, c;
28528 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28530 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28531 return list;
28533 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28534 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28536 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28537 const char *p = IDENTIFIER_POINTER (id);
28539 if (strcmp ("alloc", p) == 0)
28540 kind = OMP_CLAUSE_MAP_ALLOC;
28541 else if (strcmp ("to", p) == 0)
28542 kind = OMP_CLAUSE_MAP_TO;
28543 else if (strcmp ("from", p) == 0)
28544 kind = OMP_CLAUSE_MAP_FROM;
28545 else if (strcmp ("tofrom", p) == 0)
28546 kind = OMP_CLAUSE_MAP_TOFROM;
28547 else
28549 cp_parser_error (parser, "invalid map kind");
28550 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28551 /*or_comma=*/false,
28552 /*consume_paren=*/true);
28553 return list;
28555 cp_lexer_consume_token (parser->lexer);
28556 cp_lexer_consume_token (parser->lexer);
28559 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28560 NULL);
28562 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28563 OMP_CLAUSE_MAP_KIND (c) = kind;
28565 return nlist;
28568 /* OpenMP 4.0:
28569 device ( expression ) */
28571 static tree
28572 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28573 location_t location)
28575 tree t, c;
28577 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28578 return list;
28580 t = cp_parser_expression (parser);
28582 if (t == error_mark_node
28583 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28584 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28585 /*or_comma=*/false,
28586 /*consume_paren=*/true);
28588 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28589 "device", location);
28591 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28592 OMP_CLAUSE_DEVICE_ID (c) = t;
28593 OMP_CLAUSE_CHAIN (c) = list;
28595 return c;
28598 /* OpenMP 4.0:
28599 dist_schedule ( static )
28600 dist_schedule ( static , expression ) */
28602 static tree
28603 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28604 location_t location)
28606 tree c, t;
28608 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28609 return list;
28611 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28613 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28614 goto invalid_kind;
28615 cp_lexer_consume_token (parser->lexer);
28617 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28619 cp_lexer_consume_token (parser->lexer);
28621 t = cp_parser_assignment_expression (parser);
28623 if (t == error_mark_node)
28624 goto resync_fail;
28625 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28627 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28628 goto resync_fail;
28630 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28631 goto resync_fail;
28633 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28634 location);
28635 OMP_CLAUSE_CHAIN (c) = list;
28636 return c;
28638 invalid_kind:
28639 cp_parser_error (parser, "invalid dist_schedule kind");
28640 resync_fail:
28641 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28642 /*or_comma=*/false,
28643 /*consume_paren=*/true);
28644 return list;
28647 /* OpenMP 4.0:
28648 proc_bind ( proc-bind-kind )
28650 proc-bind-kind:
28651 master | close | spread */
28653 static tree
28654 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28655 location_t location)
28657 tree c;
28658 enum omp_clause_proc_bind_kind kind;
28660 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28661 return list;
28663 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28665 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28666 const char *p = IDENTIFIER_POINTER (id);
28668 if (strcmp ("master", p) == 0)
28669 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28670 else if (strcmp ("close", p) == 0)
28671 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28672 else if (strcmp ("spread", p) == 0)
28673 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28674 else
28675 goto invalid_kind;
28677 else
28678 goto invalid_kind;
28680 cp_lexer_consume_token (parser->lexer);
28681 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28682 goto resync_fail;
28684 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28685 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28686 location);
28687 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28688 OMP_CLAUSE_CHAIN (c) = list;
28689 return c;
28691 invalid_kind:
28692 cp_parser_error (parser, "invalid depend kind");
28693 resync_fail:
28694 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28695 /*or_comma=*/false,
28696 /*consume_paren=*/true);
28697 return list;
28700 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28701 is a bitmask in MASK. Return the list of clauses found; the result
28702 of clause default goes in *pdefault. */
28704 static tree
28705 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28706 const char *where, cp_token *pragma_tok,
28707 bool finish_p = true)
28709 tree clauses = NULL;
28710 bool first = true;
28711 cp_token *token = NULL;
28712 bool cilk_simd_fn = false;
28714 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28716 pragma_omp_clause c_kind;
28717 const char *c_name;
28718 tree prev = clauses;
28720 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28721 cp_lexer_consume_token (parser->lexer);
28723 token = cp_lexer_peek_token (parser->lexer);
28724 c_kind = cp_parser_omp_clause_name (parser);
28726 switch (c_kind)
28728 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28729 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28730 token->location);
28731 c_name = "collapse";
28732 break;
28733 case PRAGMA_OMP_CLAUSE_COPYIN:
28734 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28735 c_name = "copyin";
28736 break;
28737 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28738 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28739 clauses);
28740 c_name = "copyprivate";
28741 break;
28742 case PRAGMA_OMP_CLAUSE_DEFAULT:
28743 clauses = cp_parser_omp_clause_default (parser, clauses,
28744 token->location);
28745 c_name = "default";
28746 break;
28747 case PRAGMA_OMP_CLAUSE_FINAL:
28748 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28749 c_name = "final";
28750 break;
28751 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28752 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28753 clauses);
28754 c_name = "firstprivate";
28755 break;
28756 case PRAGMA_OMP_CLAUSE_IF:
28757 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28758 c_name = "if";
28759 break;
28760 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28761 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28762 clauses);
28763 c_name = "lastprivate";
28764 break;
28765 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28766 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28767 token->location);
28768 c_name = "mergeable";
28769 break;
28770 case PRAGMA_OMP_CLAUSE_NOWAIT:
28771 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28772 c_name = "nowait";
28773 break;
28774 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28775 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28776 token->location);
28777 c_name = "num_threads";
28778 break;
28779 case PRAGMA_OMP_CLAUSE_ORDERED:
28780 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28781 token->location);
28782 c_name = "ordered";
28783 break;
28784 case PRAGMA_OMP_CLAUSE_PRIVATE:
28785 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28786 clauses);
28787 c_name = "private";
28788 break;
28789 case PRAGMA_OMP_CLAUSE_REDUCTION:
28790 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28791 c_name = "reduction";
28792 break;
28793 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28794 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28795 token->location);
28796 c_name = "schedule";
28797 break;
28798 case PRAGMA_OMP_CLAUSE_SHARED:
28799 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28800 clauses);
28801 c_name = "shared";
28802 break;
28803 case PRAGMA_OMP_CLAUSE_UNTIED:
28804 clauses = cp_parser_omp_clause_untied (parser, clauses,
28805 token->location);
28806 c_name = "untied";
28807 break;
28808 case PRAGMA_OMP_CLAUSE_INBRANCH:
28809 case PRAGMA_CILK_CLAUSE_MASK:
28810 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28811 clauses, token->location);
28812 c_name = "inbranch";
28813 break;
28814 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28815 case PRAGMA_CILK_CLAUSE_NOMASK:
28816 clauses = cp_parser_omp_clause_branch (parser,
28817 OMP_CLAUSE_NOTINBRANCH,
28818 clauses, token->location);
28819 c_name = "notinbranch";
28820 break;
28821 case PRAGMA_OMP_CLAUSE_PARALLEL:
28822 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28823 clauses, token->location);
28824 c_name = "parallel";
28825 if (!first)
28827 clause_not_first:
28828 error_at (token->location, "%qs must be the first clause of %qs",
28829 c_name, where);
28830 clauses = prev;
28832 break;
28833 case PRAGMA_OMP_CLAUSE_FOR:
28834 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28835 clauses, token->location);
28836 c_name = "for";
28837 if (!first)
28838 goto clause_not_first;
28839 break;
28840 case PRAGMA_OMP_CLAUSE_SECTIONS:
28841 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28842 clauses, token->location);
28843 c_name = "sections";
28844 if (!first)
28845 goto clause_not_first;
28846 break;
28847 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28848 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28849 clauses, token->location);
28850 c_name = "taskgroup";
28851 if (!first)
28852 goto clause_not_first;
28853 break;
28854 case PRAGMA_OMP_CLAUSE_TO:
28855 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28856 clauses);
28857 c_name = "to";
28858 break;
28859 case PRAGMA_OMP_CLAUSE_FROM:
28860 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28861 clauses);
28862 c_name = "from";
28863 break;
28864 case PRAGMA_OMP_CLAUSE_UNIFORM:
28865 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28866 clauses);
28867 c_name = "uniform";
28868 break;
28869 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28870 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28871 token->location);
28872 c_name = "num_teams";
28873 break;
28874 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28875 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28876 token->location);
28877 c_name = "thread_limit";
28878 break;
28879 case PRAGMA_OMP_CLAUSE_ALIGNED:
28880 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28881 c_name = "aligned";
28882 break;
28883 case PRAGMA_OMP_CLAUSE_LINEAR:
28884 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28885 cilk_simd_fn = true;
28886 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28887 c_name = "linear";
28888 break;
28889 case PRAGMA_OMP_CLAUSE_DEPEND:
28890 clauses = cp_parser_omp_clause_depend (parser, clauses);
28891 c_name = "depend";
28892 break;
28893 case PRAGMA_OMP_CLAUSE_MAP:
28894 clauses = cp_parser_omp_clause_map (parser, clauses);
28895 c_name = "map";
28896 break;
28897 case PRAGMA_OMP_CLAUSE_DEVICE:
28898 clauses = cp_parser_omp_clause_device (parser, clauses,
28899 token->location);
28900 c_name = "device";
28901 break;
28902 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28903 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28904 token->location);
28905 c_name = "dist_schedule";
28906 break;
28907 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28908 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28909 token->location);
28910 c_name = "proc_bind";
28911 break;
28912 case PRAGMA_OMP_CLAUSE_SAFELEN:
28913 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28914 token->location);
28915 c_name = "safelen";
28916 break;
28917 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28918 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28919 token->location);
28920 c_name = "simdlen";
28921 break;
28922 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28923 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28924 c_name = "simdlen";
28925 break;
28926 default:
28927 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28928 goto saw_error;
28931 first = false;
28933 if (((mask >> c_kind) & 1) == 0)
28935 /* Remove the invalid clause(s) from the list to avoid
28936 confusing the rest of the compiler. */
28937 clauses = prev;
28938 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28941 saw_error:
28942 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28943 no reason to skip to the end. */
28944 if (!(flag_cilkplus && pragma_tok == NULL))
28945 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28946 if (finish_p)
28947 return finish_omp_clauses (clauses);
28948 return clauses;
28951 /* OpenMP 2.5:
28952 structured-block:
28953 statement
28955 In practice, we're also interested in adding the statement to an
28956 outer node. So it is convenient if we work around the fact that
28957 cp_parser_statement calls add_stmt. */
28959 static unsigned
28960 cp_parser_begin_omp_structured_block (cp_parser *parser)
28962 unsigned save = parser->in_statement;
28964 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28965 This preserves the "not within loop or switch" style error messages
28966 for nonsense cases like
28967 void foo() {
28968 #pragma omp single
28969 break;
28972 if (parser->in_statement)
28973 parser->in_statement = IN_OMP_BLOCK;
28975 return save;
28978 static void
28979 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28981 parser->in_statement = save;
28984 static tree
28985 cp_parser_omp_structured_block (cp_parser *parser)
28987 tree stmt = begin_omp_structured_block ();
28988 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28990 cp_parser_statement (parser, NULL_TREE, false, NULL);
28992 cp_parser_end_omp_structured_block (parser, save);
28993 return finish_omp_structured_block (stmt);
28996 /* OpenMP 2.5:
28997 # pragma omp atomic new-line
28998 expression-stmt
29000 expression-stmt:
29001 x binop= expr | x++ | ++x | x-- | --x
29002 binop:
29003 +, *, -, /, &, ^, |, <<, >>
29005 where x is an lvalue expression with scalar type.
29007 OpenMP 3.1:
29008 # pragma omp atomic new-line
29009 update-stmt
29011 # pragma omp atomic read new-line
29012 read-stmt
29014 # pragma omp atomic write new-line
29015 write-stmt
29017 # pragma omp atomic update new-line
29018 update-stmt
29020 # pragma omp atomic capture new-line
29021 capture-stmt
29023 # pragma omp atomic capture new-line
29024 capture-block
29026 read-stmt:
29027 v = x
29028 write-stmt:
29029 x = expr
29030 update-stmt:
29031 expression-stmt | x = x binop expr
29032 capture-stmt:
29033 v = expression-stmt
29034 capture-block:
29035 { v = x; update-stmt; } | { update-stmt; v = x; }
29037 OpenMP 4.0:
29038 update-stmt:
29039 expression-stmt | x = x binop expr | x = expr binop x
29040 capture-stmt:
29041 v = update-stmt
29042 capture-block:
29043 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29045 where x and v are lvalue expressions with scalar type. */
29047 static void
29048 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29050 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29051 tree rhs1 = NULL_TREE, orig_lhs;
29052 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29053 bool structured_block = false;
29054 bool seq_cst = false;
29056 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29058 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29059 const char *p = IDENTIFIER_POINTER (id);
29061 if (!strcmp (p, "seq_cst"))
29063 seq_cst = true;
29064 cp_lexer_consume_token (parser->lexer);
29065 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29066 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29067 cp_lexer_consume_token (parser->lexer);
29070 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29072 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29073 const char *p = IDENTIFIER_POINTER (id);
29075 if (!strcmp (p, "read"))
29076 code = OMP_ATOMIC_READ;
29077 else if (!strcmp (p, "write"))
29078 code = NOP_EXPR;
29079 else if (!strcmp (p, "update"))
29080 code = OMP_ATOMIC;
29081 else if (!strcmp (p, "capture"))
29082 code = OMP_ATOMIC_CAPTURE_NEW;
29083 else
29084 p = NULL;
29085 if (p)
29086 cp_lexer_consume_token (parser->lexer);
29088 if (!seq_cst)
29090 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29091 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29092 cp_lexer_consume_token (parser->lexer);
29094 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29096 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29097 const char *p = IDENTIFIER_POINTER (id);
29099 if (!strcmp (p, "seq_cst"))
29101 seq_cst = true;
29102 cp_lexer_consume_token (parser->lexer);
29106 cp_parser_require_pragma_eol (parser, pragma_tok);
29108 switch (code)
29110 case OMP_ATOMIC_READ:
29111 case NOP_EXPR: /* atomic write */
29112 v = cp_parser_unary_expression (parser);
29113 if (v == error_mark_node)
29114 goto saw_error;
29115 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29116 goto saw_error;
29117 if (code == NOP_EXPR)
29118 lhs = cp_parser_expression (parser);
29119 else
29120 lhs = cp_parser_unary_expression (parser);
29121 if (lhs == error_mark_node)
29122 goto saw_error;
29123 if (code == NOP_EXPR)
29125 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29126 opcode. */
29127 code = OMP_ATOMIC;
29128 rhs = lhs;
29129 lhs = v;
29130 v = NULL_TREE;
29132 goto done;
29133 case OMP_ATOMIC_CAPTURE_NEW:
29134 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29136 cp_lexer_consume_token (parser->lexer);
29137 structured_block = true;
29139 else
29141 v = cp_parser_unary_expression (parser);
29142 if (v == error_mark_node)
29143 goto saw_error;
29144 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29145 goto saw_error;
29147 default:
29148 break;
29151 restart:
29152 lhs = cp_parser_unary_expression (parser);
29153 orig_lhs = lhs;
29154 switch (TREE_CODE (lhs))
29156 case ERROR_MARK:
29157 goto saw_error;
29159 case POSTINCREMENT_EXPR:
29160 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29161 code = OMP_ATOMIC_CAPTURE_OLD;
29162 /* FALLTHROUGH */
29163 case PREINCREMENT_EXPR:
29164 lhs = TREE_OPERAND (lhs, 0);
29165 opcode = PLUS_EXPR;
29166 rhs = integer_one_node;
29167 break;
29169 case POSTDECREMENT_EXPR:
29170 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29171 code = OMP_ATOMIC_CAPTURE_OLD;
29172 /* FALLTHROUGH */
29173 case PREDECREMENT_EXPR:
29174 lhs = TREE_OPERAND (lhs, 0);
29175 opcode = MINUS_EXPR;
29176 rhs = integer_one_node;
29177 break;
29179 case COMPOUND_EXPR:
29180 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29181 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29182 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29183 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29184 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29185 (TREE_OPERAND (lhs, 1), 0), 0)))
29186 == BOOLEAN_TYPE)
29187 /* Undo effects of boolean_increment for post {in,de}crement. */
29188 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29189 /* FALLTHRU */
29190 case MODIFY_EXPR:
29191 if (TREE_CODE (lhs) == MODIFY_EXPR
29192 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29194 /* Undo effects of boolean_increment. */
29195 if (integer_onep (TREE_OPERAND (lhs, 1)))
29197 /* This is pre or post increment. */
29198 rhs = TREE_OPERAND (lhs, 1);
29199 lhs = TREE_OPERAND (lhs, 0);
29200 opcode = NOP_EXPR;
29201 if (code == OMP_ATOMIC_CAPTURE_NEW
29202 && !structured_block
29203 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29204 code = OMP_ATOMIC_CAPTURE_OLD;
29205 break;
29208 /* FALLTHRU */
29209 default:
29210 switch (cp_lexer_peek_token (parser->lexer)->type)
29212 case CPP_MULT_EQ:
29213 opcode = MULT_EXPR;
29214 break;
29215 case CPP_DIV_EQ:
29216 opcode = TRUNC_DIV_EXPR;
29217 break;
29218 case CPP_PLUS_EQ:
29219 opcode = PLUS_EXPR;
29220 break;
29221 case CPP_MINUS_EQ:
29222 opcode = MINUS_EXPR;
29223 break;
29224 case CPP_LSHIFT_EQ:
29225 opcode = LSHIFT_EXPR;
29226 break;
29227 case CPP_RSHIFT_EQ:
29228 opcode = RSHIFT_EXPR;
29229 break;
29230 case CPP_AND_EQ:
29231 opcode = BIT_AND_EXPR;
29232 break;
29233 case CPP_OR_EQ:
29234 opcode = BIT_IOR_EXPR;
29235 break;
29236 case CPP_XOR_EQ:
29237 opcode = BIT_XOR_EXPR;
29238 break;
29239 case CPP_EQ:
29240 enum cp_parser_prec oprec;
29241 cp_token *token;
29242 cp_lexer_consume_token (parser->lexer);
29243 cp_parser_parse_tentatively (parser);
29244 rhs1 = cp_parser_simple_cast_expression (parser);
29245 if (rhs1 == error_mark_node)
29247 cp_parser_abort_tentative_parse (parser);
29248 cp_parser_simple_cast_expression (parser);
29249 goto saw_error;
29251 token = cp_lexer_peek_token (parser->lexer);
29252 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29254 cp_parser_abort_tentative_parse (parser);
29255 cp_parser_parse_tentatively (parser);
29256 rhs = cp_parser_binary_expression (parser, false, true,
29257 PREC_NOT_OPERATOR, NULL);
29258 if (rhs == error_mark_node)
29260 cp_parser_abort_tentative_parse (parser);
29261 cp_parser_binary_expression (parser, false, true,
29262 PREC_NOT_OPERATOR, NULL);
29263 goto saw_error;
29265 switch (TREE_CODE (rhs))
29267 case MULT_EXPR:
29268 case TRUNC_DIV_EXPR:
29269 case PLUS_EXPR:
29270 case MINUS_EXPR:
29271 case LSHIFT_EXPR:
29272 case RSHIFT_EXPR:
29273 case BIT_AND_EXPR:
29274 case BIT_IOR_EXPR:
29275 case BIT_XOR_EXPR:
29276 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29278 if (cp_parser_parse_definitely (parser))
29280 opcode = TREE_CODE (rhs);
29281 rhs1 = TREE_OPERAND (rhs, 0);
29282 rhs = TREE_OPERAND (rhs, 1);
29283 goto stmt_done;
29285 else
29286 goto saw_error;
29288 break;
29289 default:
29290 break;
29292 cp_parser_abort_tentative_parse (parser);
29293 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29295 rhs = cp_parser_expression (parser);
29296 if (rhs == error_mark_node)
29297 goto saw_error;
29298 opcode = NOP_EXPR;
29299 rhs1 = NULL_TREE;
29300 goto stmt_done;
29302 cp_parser_error (parser,
29303 "invalid form of %<#pragma omp atomic%>");
29304 goto saw_error;
29306 if (!cp_parser_parse_definitely (parser))
29307 goto saw_error;
29308 switch (token->type)
29310 case CPP_SEMICOLON:
29311 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29313 code = OMP_ATOMIC_CAPTURE_OLD;
29314 v = lhs;
29315 lhs = NULL_TREE;
29316 lhs1 = rhs1;
29317 rhs1 = NULL_TREE;
29318 cp_lexer_consume_token (parser->lexer);
29319 goto restart;
29321 else if (structured_block)
29323 opcode = NOP_EXPR;
29324 rhs = rhs1;
29325 rhs1 = NULL_TREE;
29326 goto stmt_done;
29328 cp_parser_error (parser,
29329 "invalid form of %<#pragma omp atomic%>");
29330 goto saw_error;
29331 case CPP_MULT:
29332 opcode = MULT_EXPR;
29333 break;
29334 case CPP_DIV:
29335 opcode = TRUNC_DIV_EXPR;
29336 break;
29337 case CPP_PLUS:
29338 opcode = PLUS_EXPR;
29339 break;
29340 case CPP_MINUS:
29341 opcode = MINUS_EXPR;
29342 break;
29343 case CPP_LSHIFT:
29344 opcode = LSHIFT_EXPR;
29345 break;
29346 case CPP_RSHIFT:
29347 opcode = RSHIFT_EXPR;
29348 break;
29349 case CPP_AND:
29350 opcode = BIT_AND_EXPR;
29351 break;
29352 case CPP_OR:
29353 opcode = BIT_IOR_EXPR;
29354 break;
29355 case CPP_XOR:
29356 opcode = BIT_XOR_EXPR;
29357 break;
29358 default:
29359 cp_parser_error (parser,
29360 "invalid operator for %<#pragma omp atomic%>");
29361 goto saw_error;
29363 oprec = TOKEN_PRECEDENCE (token);
29364 gcc_assert (oprec != PREC_NOT_OPERATOR);
29365 if (commutative_tree_code (opcode))
29366 oprec = (enum cp_parser_prec) (oprec - 1);
29367 cp_lexer_consume_token (parser->lexer);
29368 rhs = cp_parser_binary_expression (parser, false, false,
29369 oprec, NULL);
29370 if (rhs == error_mark_node)
29371 goto saw_error;
29372 goto stmt_done;
29373 /* FALLTHROUGH */
29374 default:
29375 cp_parser_error (parser,
29376 "invalid operator for %<#pragma omp atomic%>");
29377 goto saw_error;
29379 cp_lexer_consume_token (parser->lexer);
29381 rhs = cp_parser_expression (parser);
29382 if (rhs == error_mark_node)
29383 goto saw_error;
29384 break;
29386 stmt_done:
29387 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29389 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29390 goto saw_error;
29391 v = cp_parser_unary_expression (parser);
29392 if (v == error_mark_node)
29393 goto saw_error;
29394 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29395 goto saw_error;
29396 lhs1 = cp_parser_unary_expression (parser);
29397 if (lhs1 == error_mark_node)
29398 goto saw_error;
29400 if (structured_block)
29402 cp_parser_consume_semicolon_at_end_of_statement (parser);
29403 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29405 done:
29406 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29407 if (!structured_block)
29408 cp_parser_consume_semicolon_at_end_of_statement (parser);
29409 return;
29411 saw_error:
29412 cp_parser_skip_to_end_of_block_or_statement (parser);
29413 if (structured_block)
29415 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29416 cp_lexer_consume_token (parser->lexer);
29417 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29419 cp_parser_skip_to_end_of_block_or_statement (parser);
29420 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29421 cp_lexer_consume_token (parser->lexer);
29427 /* OpenMP 2.5:
29428 # pragma omp barrier new-line */
29430 static void
29431 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29433 cp_parser_require_pragma_eol (parser, pragma_tok);
29434 finish_omp_barrier ();
29437 /* OpenMP 2.5:
29438 # pragma omp critical [(name)] new-line
29439 structured-block */
29441 static tree
29442 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29444 tree stmt, name = NULL;
29446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29448 cp_lexer_consume_token (parser->lexer);
29450 name = cp_parser_identifier (parser);
29452 if (name == error_mark_node
29453 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29454 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29455 /*or_comma=*/false,
29456 /*consume_paren=*/true);
29457 if (name == error_mark_node)
29458 name = NULL;
29460 cp_parser_require_pragma_eol (parser, pragma_tok);
29462 stmt = cp_parser_omp_structured_block (parser);
29463 return c_finish_omp_critical (input_location, stmt, name);
29466 /* OpenMP 2.5:
29467 # pragma omp flush flush-vars[opt] new-line
29469 flush-vars:
29470 ( variable-list ) */
29472 static void
29473 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29475 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29476 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29477 cp_parser_require_pragma_eol (parser, pragma_tok);
29479 finish_omp_flush ();
29482 /* Helper function, to parse omp for increment expression. */
29484 static tree
29485 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29487 tree cond = cp_parser_binary_expression (parser, false, true,
29488 PREC_NOT_OPERATOR, NULL);
29489 if (cond == error_mark_node
29490 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29492 cp_parser_skip_to_end_of_statement (parser);
29493 return error_mark_node;
29496 switch (TREE_CODE (cond))
29498 case GT_EXPR:
29499 case GE_EXPR:
29500 case LT_EXPR:
29501 case LE_EXPR:
29502 break;
29503 case NE_EXPR:
29504 if (code == CILK_SIMD || code == CILK_FOR)
29505 break;
29506 /* Fall through: OpenMP disallows NE_EXPR. */
29507 default:
29508 return error_mark_node;
29511 /* If decl is an iterator, preserve LHS and RHS of the relational
29512 expr until finish_omp_for. */
29513 if (decl
29514 && (type_dependent_expression_p (decl)
29515 || CLASS_TYPE_P (TREE_TYPE (decl))))
29516 return cond;
29518 return build_x_binary_op (input_location, TREE_CODE (cond),
29519 TREE_OPERAND (cond, 0), ERROR_MARK,
29520 TREE_OPERAND (cond, 1), ERROR_MARK,
29521 /*overload=*/NULL, tf_warning_or_error);
29524 /* Helper function, to parse omp for increment expression. */
29526 static tree
29527 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29529 cp_token *token = cp_lexer_peek_token (parser->lexer);
29530 enum tree_code op;
29531 tree lhs, rhs;
29532 cp_id_kind idk;
29533 bool decl_first;
29535 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29537 op = (token->type == CPP_PLUS_PLUS
29538 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29539 cp_lexer_consume_token (parser->lexer);
29540 lhs = cp_parser_simple_cast_expression (parser);
29541 if (lhs != decl)
29542 return error_mark_node;
29543 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29546 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29547 if (lhs != decl)
29548 return error_mark_node;
29550 token = cp_lexer_peek_token (parser->lexer);
29551 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29553 op = (token->type == CPP_PLUS_PLUS
29554 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29555 cp_lexer_consume_token (parser->lexer);
29556 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29559 op = cp_parser_assignment_operator_opt (parser);
29560 if (op == ERROR_MARK)
29561 return error_mark_node;
29563 if (op != NOP_EXPR)
29565 rhs = cp_parser_assignment_expression (parser);
29566 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29567 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29570 lhs = cp_parser_binary_expression (parser, false, false,
29571 PREC_ADDITIVE_EXPRESSION, NULL);
29572 token = cp_lexer_peek_token (parser->lexer);
29573 decl_first = lhs == decl;
29574 if (decl_first)
29575 lhs = NULL_TREE;
29576 if (token->type != CPP_PLUS
29577 && token->type != CPP_MINUS)
29578 return error_mark_node;
29582 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29583 cp_lexer_consume_token (parser->lexer);
29584 rhs = cp_parser_binary_expression (parser, false, false,
29585 PREC_ADDITIVE_EXPRESSION, NULL);
29586 token = cp_lexer_peek_token (parser->lexer);
29587 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29589 if (lhs == NULL_TREE)
29591 if (op == PLUS_EXPR)
29592 lhs = rhs;
29593 else
29594 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29595 tf_warning_or_error);
29597 else
29598 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29599 ERROR_MARK, NULL, tf_warning_or_error);
29602 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29604 if (!decl_first)
29606 if (rhs != decl || op == MINUS_EXPR)
29607 return error_mark_node;
29608 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29610 else
29611 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29613 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29616 /* Parse the initialization statement of either an OpenMP for loop or
29617 a Cilk Plus for loop.
29619 Return true if the resulting construct should have an
29620 OMP_CLAUSE_PRIVATE added to it. */
29622 static bool
29623 cp_parser_omp_for_loop_init (cp_parser *parser,
29624 enum tree_code code,
29625 tree &this_pre_body,
29626 vec<tree, va_gc> *for_block,
29627 tree &init,
29628 tree &decl,
29629 tree &real_decl)
29631 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29632 return false;
29634 bool add_private_clause = false;
29636 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29638 init-expr:
29639 var = lb
29640 integer-type var = lb
29641 random-access-iterator-type var = lb
29642 pointer-type var = lb
29644 cp_decl_specifier_seq type_specifiers;
29646 /* First, try to parse as an initialized declaration. See
29647 cp_parser_condition, from whence the bulk of this is copied. */
29649 cp_parser_parse_tentatively (parser);
29650 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29651 /*is_trailing_return=*/false,
29652 &type_specifiers);
29653 if (cp_parser_parse_definitely (parser))
29655 /* If parsing a type specifier seq succeeded, then this
29656 MUST be a initialized declaration. */
29657 tree asm_specification, attributes;
29658 cp_declarator *declarator;
29660 declarator = cp_parser_declarator (parser,
29661 CP_PARSER_DECLARATOR_NAMED,
29662 /*ctor_dtor_or_conv_p=*/NULL,
29663 /*parenthesized_p=*/NULL,
29664 /*member_p=*/false,
29665 /*friend_p=*/false);
29666 attributes = cp_parser_attributes_opt (parser);
29667 asm_specification = cp_parser_asm_specification_opt (parser);
29669 if (declarator == cp_error_declarator)
29670 cp_parser_skip_to_end_of_statement (parser);
29672 else
29674 tree pushed_scope, auto_node;
29676 decl = start_decl (declarator, &type_specifiers,
29677 SD_INITIALIZED, attributes,
29678 /*prefix_attributes=*/NULL_TREE,
29679 &pushed_scope);
29681 auto_node = type_uses_auto (TREE_TYPE (decl));
29682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29684 if (cp_lexer_next_token_is (parser->lexer,
29685 CPP_OPEN_PAREN))
29687 if (code != CILK_SIMD && code != CILK_FOR)
29688 error ("parenthesized initialization is not allowed in "
29689 "OpenMP %<for%> loop");
29690 else
29691 error ("parenthesized initialization is "
29692 "not allowed in for-loop");
29694 else
29695 /* Trigger an error. */
29696 cp_parser_require (parser, CPP_EQ, RT_EQ);
29698 init = error_mark_node;
29699 cp_parser_skip_to_end_of_statement (parser);
29701 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29702 || type_dependent_expression_p (decl)
29703 || auto_node)
29705 bool is_direct_init, is_non_constant_init;
29707 init = cp_parser_initializer (parser,
29708 &is_direct_init,
29709 &is_non_constant_init);
29711 if (auto_node)
29713 TREE_TYPE (decl)
29714 = do_auto_deduction (TREE_TYPE (decl), init,
29715 auto_node);
29717 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29718 && !type_dependent_expression_p (decl))
29719 goto non_class;
29722 cp_finish_decl (decl, init, !is_non_constant_init,
29723 asm_specification,
29724 LOOKUP_ONLYCONVERTING);
29725 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29727 vec_safe_push (for_block, this_pre_body);
29728 init = NULL_TREE;
29730 else
29731 init = pop_stmt_list (this_pre_body);
29732 this_pre_body = NULL_TREE;
29734 else
29736 /* Consume '='. */
29737 cp_lexer_consume_token (parser->lexer);
29738 init = cp_parser_assignment_expression (parser);
29740 non_class:
29741 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29742 init = error_mark_node;
29743 else
29744 cp_finish_decl (decl, NULL_TREE,
29745 /*init_const_expr_p=*/false,
29746 asm_specification,
29747 LOOKUP_ONLYCONVERTING);
29750 if (pushed_scope)
29751 pop_scope (pushed_scope);
29754 else
29756 cp_id_kind idk;
29757 /* If parsing a type specifier sequence failed, then
29758 this MUST be a simple expression. */
29759 if (code == CILK_FOR)
29760 error ("%<_Cilk_for%> allows expression instead of declaration only "
29761 "in C, not in C++");
29762 cp_parser_parse_tentatively (parser);
29763 decl = cp_parser_primary_expression (parser, false, false,
29764 false, &idk);
29765 if (!cp_parser_error_occurred (parser)
29766 && decl
29767 && DECL_P (decl)
29768 && CLASS_TYPE_P (TREE_TYPE (decl)))
29770 tree rhs;
29772 cp_parser_parse_definitely (parser);
29773 cp_parser_require (parser, CPP_EQ, RT_EQ);
29774 rhs = cp_parser_assignment_expression (parser);
29775 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29776 decl, NOP_EXPR,
29777 rhs,
29778 tf_warning_or_error));
29779 add_private_clause = true;
29781 else
29783 decl = NULL;
29784 cp_parser_abort_tentative_parse (parser);
29785 init = cp_parser_expression (parser);
29786 if (init)
29788 if (TREE_CODE (init) == MODIFY_EXPR
29789 || TREE_CODE (init) == MODOP_EXPR)
29790 real_decl = TREE_OPERAND (init, 0);
29794 return add_private_clause;
29797 /* Parse the restricted form of the for statement allowed by OpenMP. */
29799 static tree
29800 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29801 tree *cclauses)
29803 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29804 tree real_decl, initv, condv, incrv, declv;
29805 tree this_pre_body, cl;
29806 location_t loc_first;
29807 bool collapse_err = false;
29808 int i, collapse = 1, nbraces = 0;
29809 vec<tree, va_gc> *for_block = make_tree_vector ();
29811 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29812 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29813 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29815 gcc_assert (collapse >= 1);
29817 declv = make_tree_vec (collapse);
29818 initv = make_tree_vec (collapse);
29819 condv = make_tree_vec (collapse);
29820 incrv = make_tree_vec (collapse);
29822 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29824 for (i = 0; i < collapse; i++)
29826 int bracecount = 0;
29827 bool add_private_clause = false;
29828 location_t loc;
29830 if (code != CILK_FOR
29831 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29833 cp_parser_error (parser, "for statement expected");
29834 return NULL;
29836 if (code == CILK_FOR
29837 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
29839 cp_parser_error (parser, "_Cilk_for statement expected");
29840 return NULL;
29842 loc = cp_lexer_consume_token (parser->lexer)->location;
29844 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29845 return NULL;
29847 init = decl = real_decl = NULL;
29848 this_pre_body = push_stmt_list ();
29850 add_private_clause
29851 |= cp_parser_omp_for_loop_init (parser, code,
29852 this_pre_body, for_block,
29853 init, decl, real_decl);
29855 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29856 if (this_pre_body)
29858 this_pre_body = pop_stmt_list (this_pre_body);
29859 if (pre_body)
29861 tree t = pre_body;
29862 pre_body = push_stmt_list ();
29863 add_stmt (t);
29864 add_stmt (this_pre_body);
29865 pre_body = pop_stmt_list (pre_body);
29867 else
29868 pre_body = this_pre_body;
29871 if (decl)
29872 real_decl = decl;
29873 if (cclauses != NULL
29874 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29875 && real_decl != NULL_TREE)
29877 tree *c;
29878 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29879 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29880 && OMP_CLAUSE_DECL (*c) == real_decl)
29882 error_at (loc, "iteration variable %qD"
29883 " should not be firstprivate", real_decl);
29884 *c = OMP_CLAUSE_CHAIN (*c);
29886 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29887 && OMP_CLAUSE_DECL (*c) == real_decl)
29889 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29890 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29891 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29892 OMP_CLAUSE_DECL (l) = real_decl;
29893 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29894 if (code == OMP_SIMD)
29896 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29897 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
29899 else
29901 OMP_CLAUSE_CHAIN (l) = clauses;
29902 clauses = l;
29904 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29905 CP_OMP_CLAUSE_INFO (*c) = NULL;
29906 add_private_clause = false;
29908 else
29910 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29911 && OMP_CLAUSE_DECL (*c) == real_decl)
29912 add_private_clause = false;
29913 c = &OMP_CLAUSE_CHAIN (*c);
29917 if (add_private_clause)
29919 tree c;
29920 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29922 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29923 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29924 && OMP_CLAUSE_DECL (c) == decl)
29925 break;
29926 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29927 && OMP_CLAUSE_DECL (c) == decl)
29928 error_at (loc, "iteration variable %qD "
29929 "should not be firstprivate",
29930 decl);
29931 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29932 && OMP_CLAUSE_DECL (c) == decl)
29933 error_at (loc, "iteration variable %qD should not be reduction",
29934 decl);
29936 if (c == NULL)
29938 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29939 OMP_CLAUSE_DECL (c) = decl;
29940 c = finish_omp_clauses (c);
29941 if (c)
29943 OMP_CLAUSE_CHAIN (c) = clauses;
29944 clauses = c;
29949 cond = NULL;
29950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29951 cond = cp_parser_omp_for_cond (parser, decl, code);
29952 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29954 incr = NULL;
29955 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29957 /* If decl is an iterator, preserve the operator on decl
29958 until finish_omp_for. */
29959 if (real_decl
29960 && ((processing_template_decl
29961 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29962 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29963 incr = cp_parser_omp_for_incr (parser, real_decl);
29964 else
29965 incr = cp_parser_expression (parser);
29966 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29967 SET_EXPR_LOCATION (incr, input_location);
29970 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29971 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29972 /*or_comma=*/false,
29973 /*consume_paren=*/true);
29975 TREE_VEC_ELT (declv, i) = decl;
29976 TREE_VEC_ELT (initv, i) = init;
29977 TREE_VEC_ELT (condv, i) = cond;
29978 TREE_VEC_ELT (incrv, i) = incr;
29980 if (i == collapse - 1)
29981 break;
29983 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29984 in between the collapsed for loops to be still considered perfectly
29985 nested. Hopefully the final version clarifies this.
29986 For now handle (multiple) {'s and empty statements. */
29987 cp_parser_parse_tentatively (parser);
29990 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29991 break;
29992 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29994 cp_lexer_consume_token (parser->lexer);
29995 bracecount++;
29997 else if (bracecount
29998 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29999 cp_lexer_consume_token (parser->lexer);
30000 else
30002 loc = cp_lexer_peek_token (parser->lexer)->location;
30003 error_at (loc, "not enough collapsed for loops");
30004 collapse_err = true;
30005 cp_parser_abort_tentative_parse (parser);
30006 declv = NULL_TREE;
30007 break;
30010 while (1);
30012 if (declv)
30014 cp_parser_parse_definitely (parser);
30015 nbraces += bracecount;
30019 /* Note that we saved the original contents of this flag when we entered
30020 the structured block, and so we don't need to re-save it here. */
30021 if (code == CILK_SIMD || code == CILK_FOR)
30022 parser->in_statement = IN_CILK_SIMD_FOR;
30023 else
30024 parser->in_statement = IN_OMP_FOR;
30026 /* Note that the grammar doesn't call for a structured block here,
30027 though the loop as a whole is a structured block. */
30028 body = push_stmt_list ();
30029 cp_parser_statement (parser, NULL_TREE, false, NULL);
30030 body = pop_stmt_list (body);
30032 if (declv == NULL_TREE)
30033 ret = NULL_TREE;
30034 else
30035 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30036 pre_body, clauses);
30038 while (nbraces)
30040 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30042 cp_lexer_consume_token (parser->lexer);
30043 nbraces--;
30045 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30046 cp_lexer_consume_token (parser->lexer);
30047 else
30049 if (!collapse_err)
30051 error_at (cp_lexer_peek_token (parser->lexer)->location,
30052 "collapsed loops not perfectly nested");
30054 collapse_err = true;
30055 cp_parser_statement_seq_opt (parser, NULL);
30056 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30057 break;
30061 while (!for_block->is_empty ())
30062 add_stmt (pop_stmt_list (for_block->pop ()));
30063 release_tree_vector (for_block);
30065 return ret;
30068 /* Helper function for OpenMP parsing, split clauses and call
30069 finish_omp_clauses on each of the set of clauses afterwards. */
30071 static void
30072 cp_omp_split_clauses (location_t loc, enum tree_code code,
30073 omp_clause_mask mask, tree clauses, tree *cclauses)
30075 int i;
30076 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30077 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30078 if (cclauses[i])
30079 cclauses[i] = finish_omp_clauses (cclauses[i]);
30082 /* OpenMP 4.0:
30083 #pragma omp simd simd-clause[optseq] new-line
30084 for-loop */
30086 #define OMP_SIMD_CLAUSE_MASK \
30087 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30095 static tree
30096 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30097 char *p_name, omp_clause_mask mask, tree *cclauses)
30099 tree clauses, sb, ret;
30100 unsigned int save;
30101 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30103 strcat (p_name, " simd");
30104 mask |= OMP_SIMD_CLAUSE_MASK;
30105 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30107 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30108 cclauses == NULL);
30109 if (cclauses)
30111 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30112 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30115 sb = begin_omp_structured_block ();
30116 save = cp_parser_begin_omp_structured_block (parser);
30118 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30120 cp_parser_end_omp_structured_block (parser, save);
30121 add_stmt (finish_omp_structured_block (sb));
30123 return ret;
30126 /* OpenMP 2.5:
30127 #pragma omp for for-clause[optseq] new-line
30128 for-loop
30130 OpenMP 4.0:
30131 #pragma omp for simd for-simd-clause[optseq] new-line
30132 for-loop */
30134 #define OMP_FOR_CLAUSE_MASK \
30135 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30144 static tree
30145 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30146 char *p_name, omp_clause_mask mask, tree *cclauses)
30148 tree clauses, sb, ret;
30149 unsigned int save;
30150 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30152 strcat (p_name, " for");
30153 mask |= OMP_FOR_CLAUSE_MASK;
30154 if (cclauses)
30155 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30157 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30159 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30160 const char *p = IDENTIFIER_POINTER (id);
30162 if (strcmp (p, "simd") == 0)
30164 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30165 if (cclauses == NULL)
30166 cclauses = cclauses_buf;
30168 cp_lexer_consume_token (parser->lexer);
30169 if (!flag_openmp) /* flag_openmp_simd */
30170 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30171 cclauses);
30172 sb = begin_omp_structured_block ();
30173 save = cp_parser_begin_omp_structured_block (parser);
30174 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30175 cclauses);
30176 cp_parser_end_omp_structured_block (parser, save);
30177 tree body = finish_omp_structured_block (sb);
30178 if (ret == NULL)
30179 return ret;
30180 ret = make_node (OMP_FOR);
30181 TREE_TYPE (ret) = void_type_node;
30182 OMP_FOR_BODY (ret) = body;
30183 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30184 SET_EXPR_LOCATION (ret, loc);
30185 add_stmt (ret);
30186 return ret;
30189 if (!flag_openmp) /* flag_openmp_simd */
30191 cp_parser_require_pragma_eol (parser, pragma_tok);
30192 return NULL_TREE;
30195 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30196 cclauses == NULL);
30197 if (cclauses)
30199 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30200 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30203 sb = begin_omp_structured_block ();
30204 save = cp_parser_begin_omp_structured_block (parser);
30206 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30208 cp_parser_end_omp_structured_block (parser, save);
30209 add_stmt (finish_omp_structured_block (sb));
30211 return ret;
30214 /* OpenMP 2.5:
30215 # pragma omp master new-line
30216 structured-block */
30218 static tree
30219 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30221 cp_parser_require_pragma_eol (parser, pragma_tok);
30222 return c_finish_omp_master (input_location,
30223 cp_parser_omp_structured_block (parser));
30226 /* OpenMP 2.5:
30227 # pragma omp ordered new-line
30228 structured-block */
30230 static tree
30231 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30233 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30234 cp_parser_require_pragma_eol (parser, pragma_tok);
30235 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30238 /* OpenMP 2.5:
30240 section-scope:
30241 { section-sequence }
30243 section-sequence:
30244 section-directive[opt] structured-block
30245 section-sequence section-directive structured-block */
30247 static tree
30248 cp_parser_omp_sections_scope (cp_parser *parser)
30250 tree stmt, substmt;
30251 bool error_suppress = false;
30252 cp_token *tok;
30254 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30255 return NULL_TREE;
30257 stmt = push_stmt_list ();
30259 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30261 substmt = cp_parser_omp_structured_block (parser);
30262 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30263 add_stmt (substmt);
30266 while (1)
30268 tok = cp_lexer_peek_token (parser->lexer);
30269 if (tok->type == CPP_CLOSE_BRACE)
30270 break;
30271 if (tok->type == CPP_EOF)
30272 break;
30274 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30276 cp_lexer_consume_token (parser->lexer);
30277 cp_parser_require_pragma_eol (parser, tok);
30278 error_suppress = false;
30280 else if (!error_suppress)
30282 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30283 error_suppress = true;
30286 substmt = cp_parser_omp_structured_block (parser);
30287 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30288 add_stmt (substmt);
30290 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30292 substmt = pop_stmt_list (stmt);
30294 stmt = make_node (OMP_SECTIONS);
30295 TREE_TYPE (stmt) = void_type_node;
30296 OMP_SECTIONS_BODY (stmt) = substmt;
30298 add_stmt (stmt);
30299 return stmt;
30302 /* OpenMP 2.5:
30303 # pragma omp sections sections-clause[optseq] newline
30304 sections-scope */
30306 #define OMP_SECTIONS_CLAUSE_MASK \
30307 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30313 static tree
30314 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30315 char *p_name, omp_clause_mask mask, tree *cclauses)
30317 tree clauses, ret;
30318 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30320 strcat (p_name, " sections");
30321 mask |= OMP_SECTIONS_CLAUSE_MASK;
30322 if (cclauses)
30323 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30325 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30326 cclauses == NULL);
30327 if (cclauses)
30329 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30330 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30333 ret = cp_parser_omp_sections_scope (parser);
30334 if (ret)
30335 OMP_SECTIONS_CLAUSES (ret) = clauses;
30337 return ret;
30340 /* OpenMP 2.5:
30341 # pragma omp parallel parallel-clause[optseq] new-line
30342 structured-block
30343 # pragma omp parallel for parallel-for-clause[optseq] new-line
30344 structured-block
30345 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30346 structured-block
30348 OpenMP 4.0:
30349 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30350 structured-block */
30352 #define OMP_PARALLEL_CLAUSE_MASK \
30353 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30363 static tree
30364 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30365 char *p_name, omp_clause_mask mask, tree *cclauses)
30367 tree stmt, clauses, block;
30368 unsigned int save;
30369 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30371 strcat (p_name, " parallel");
30372 mask |= OMP_PARALLEL_CLAUSE_MASK;
30374 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30376 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30377 if (cclauses == NULL)
30378 cclauses = cclauses_buf;
30380 cp_lexer_consume_token (parser->lexer);
30381 if (!flag_openmp) /* flag_openmp_simd */
30382 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30383 block = begin_omp_parallel ();
30384 save = cp_parser_begin_omp_structured_block (parser);
30385 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30386 cp_parser_end_omp_structured_block (parser, save);
30387 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30388 block);
30389 if (ret == NULL_TREE)
30390 return ret;
30391 OMP_PARALLEL_COMBINED (stmt) = 1;
30392 return stmt;
30394 else if (cclauses)
30396 error_at (loc, "expected %<for%> after %qs", p_name);
30397 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30398 return NULL_TREE;
30400 else if (!flag_openmp) /* flag_openmp_simd */
30402 cp_parser_require_pragma_eol (parser, pragma_tok);
30403 return NULL_TREE;
30405 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30407 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30408 const char *p = IDENTIFIER_POINTER (id);
30409 if (strcmp (p, "sections") == 0)
30411 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30412 cclauses = cclauses_buf;
30414 cp_lexer_consume_token (parser->lexer);
30415 block = begin_omp_parallel ();
30416 save = cp_parser_begin_omp_structured_block (parser);
30417 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30418 cp_parser_end_omp_structured_block (parser, save);
30419 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30420 block);
30421 OMP_PARALLEL_COMBINED (stmt) = 1;
30422 return stmt;
30426 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30428 block = begin_omp_parallel ();
30429 save = cp_parser_begin_omp_structured_block (parser);
30430 cp_parser_statement (parser, NULL_TREE, false, NULL);
30431 cp_parser_end_omp_structured_block (parser, save);
30432 stmt = finish_omp_parallel (clauses, block);
30433 return stmt;
30436 /* OpenMP 2.5:
30437 # pragma omp single single-clause[optseq] new-line
30438 structured-block */
30440 #define OMP_SINGLE_CLAUSE_MASK \
30441 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30446 static tree
30447 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30449 tree stmt = make_node (OMP_SINGLE);
30450 TREE_TYPE (stmt) = void_type_node;
30452 OMP_SINGLE_CLAUSES (stmt)
30453 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30454 "#pragma omp single", pragma_tok);
30455 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30457 return add_stmt (stmt);
30460 /* OpenMP 3.0:
30461 # pragma omp task task-clause[optseq] new-line
30462 structured-block */
30464 #define OMP_TASK_CLAUSE_MASK \
30465 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30475 static tree
30476 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30478 tree clauses, block;
30479 unsigned int save;
30481 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30482 "#pragma omp task", pragma_tok);
30483 block = begin_omp_task ();
30484 save = cp_parser_begin_omp_structured_block (parser);
30485 cp_parser_statement (parser, NULL_TREE, false, NULL);
30486 cp_parser_end_omp_structured_block (parser, save);
30487 return finish_omp_task (clauses, block);
30490 /* OpenMP 3.0:
30491 # pragma omp taskwait new-line */
30493 static void
30494 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30496 cp_parser_require_pragma_eol (parser, pragma_tok);
30497 finish_omp_taskwait ();
30500 /* OpenMP 3.1:
30501 # pragma omp taskyield new-line */
30503 static void
30504 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30506 cp_parser_require_pragma_eol (parser, pragma_tok);
30507 finish_omp_taskyield ();
30510 /* OpenMP 4.0:
30511 # pragma omp taskgroup new-line
30512 structured-block */
30514 static tree
30515 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30517 cp_parser_require_pragma_eol (parser, pragma_tok);
30518 return c_finish_omp_taskgroup (input_location,
30519 cp_parser_omp_structured_block (parser));
30523 /* OpenMP 2.5:
30524 # pragma omp threadprivate (variable-list) */
30526 static void
30527 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30529 tree vars;
30531 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30532 cp_parser_require_pragma_eol (parser, pragma_tok);
30534 finish_omp_threadprivate (vars);
30537 /* OpenMP 4.0:
30538 # pragma omp cancel cancel-clause[optseq] new-line */
30540 #define OMP_CANCEL_CLAUSE_MASK \
30541 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30547 static void
30548 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30550 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30551 "#pragma omp cancel", pragma_tok);
30552 finish_omp_cancel (clauses);
30555 /* OpenMP 4.0:
30556 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30558 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30559 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30564 static void
30565 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30567 tree clauses;
30568 bool point_seen = false;
30570 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30572 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30573 const char *p = IDENTIFIER_POINTER (id);
30575 if (strcmp (p, "point") == 0)
30577 cp_lexer_consume_token (parser->lexer);
30578 point_seen = true;
30581 if (!point_seen)
30583 cp_parser_error (parser, "expected %<point%>");
30584 cp_parser_require_pragma_eol (parser, pragma_tok);
30585 return;
30588 clauses = cp_parser_omp_all_clauses (parser,
30589 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30590 "#pragma omp cancellation point",
30591 pragma_tok);
30592 finish_omp_cancellation_point (clauses);
30595 /* OpenMP 4.0:
30596 #pragma omp distribute distribute-clause[optseq] new-line
30597 for-loop */
30599 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30600 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30605 static tree
30606 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30607 char *p_name, omp_clause_mask mask, tree *cclauses)
30609 tree clauses, sb, ret;
30610 unsigned int save;
30611 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30613 strcat (p_name, " distribute");
30614 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30616 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30618 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30619 const char *p = IDENTIFIER_POINTER (id);
30620 bool simd = false;
30621 bool parallel = false;
30623 if (strcmp (p, "simd") == 0)
30624 simd = true;
30625 else
30626 parallel = strcmp (p, "parallel") == 0;
30627 if (parallel || simd)
30629 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30630 if (cclauses == NULL)
30631 cclauses = cclauses_buf;
30632 cp_lexer_consume_token (parser->lexer);
30633 if (!flag_openmp) /* flag_openmp_simd */
30635 if (simd)
30636 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30637 cclauses);
30638 else
30639 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30640 cclauses);
30642 sb = begin_omp_structured_block ();
30643 save = cp_parser_begin_omp_structured_block (parser);
30644 if (simd)
30645 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30646 cclauses);
30647 else
30648 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30649 cclauses);
30650 cp_parser_end_omp_structured_block (parser, save);
30651 tree body = finish_omp_structured_block (sb);
30652 if (ret == NULL)
30653 return ret;
30654 ret = make_node (OMP_DISTRIBUTE);
30655 TREE_TYPE (ret) = void_type_node;
30656 OMP_FOR_BODY (ret) = body;
30657 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30658 SET_EXPR_LOCATION (ret, loc);
30659 add_stmt (ret);
30660 return ret;
30663 if (!flag_openmp) /* flag_openmp_simd */
30665 cp_parser_require_pragma_eol (parser, pragma_tok);
30666 return NULL_TREE;
30669 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30670 cclauses == NULL);
30671 if (cclauses)
30673 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30674 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30677 sb = begin_omp_structured_block ();
30678 save = cp_parser_begin_omp_structured_block (parser);
30680 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30682 cp_parser_end_omp_structured_block (parser, save);
30683 add_stmt (finish_omp_structured_block (sb));
30685 return ret;
30688 /* OpenMP 4.0:
30689 # pragma omp teams teams-clause[optseq] new-line
30690 structured-block */
30692 #define OMP_TEAMS_CLAUSE_MASK \
30693 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30701 static tree
30702 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30703 char *p_name, omp_clause_mask mask, tree *cclauses)
30705 tree clauses, sb, ret;
30706 unsigned int save;
30707 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30709 strcat (p_name, " teams");
30710 mask |= OMP_TEAMS_CLAUSE_MASK;
30712 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30714 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30715 const char *p = IDENTIFIER_POINTER (id);
30716 if (strcmp (p, "distribute") == 0)
30718 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30719 if (cclauses == NULL)
30720 cclauses = cclauses_buf;
30722 cp_lexer_consume_token (parser->lexer);
30723 if (!flag_openmp) /* flag_openmp_simd */
30724 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30725 cclauses);
30726 sb = begin_omp_structured_block ();
30727 save = cp_parser_begin_omp_structured_block (parser);
30728 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30729 cclauses);
30730 cp_parser_end_omp_structured_block (parser, save);
30731 tree body = finish_omp_structured_block (sb);
30732 if (ret == NULL)
30733 return ret;
30734 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30735 ret = make_node (OMP_TEAMS);
30736 TREE_TYPE (ret) = void_type_node;
30737 OMP_TEAMS_CLAUSES (ret) = clauses;
30738 OMP_TEAMS_BODY (ret) = body;
30739 return add_stmt (ret);
30742 if (!flag_openmp) /* flag_openmp_simd */
30744 cp_parser_require_pragma_eol (parser, pragma_tok);
30745 return NULL_TREE;
30748 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30749 cclauses == NULL);
30750 if (cclauses)
30752 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30753 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30756 tree stmt = make_node (OMP_TEAMS);
30757 TREE_TYPE (stmt) = void_type_node;
30758 OMP_TEAMS_CLAUSES (stmt) = clauses;
30759 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30761 return add_stmt (stmt);
30764 /* OpenMP 4.0:
30765 # pragma omp target data target-data-clause[optseq] new-line
30766 structured-block */
30768 #define OMP_TARGET_DATA_CLAUSE_MASK \
30769 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30773 static tree
30774 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30776 tree stmt = make_node (OMP_TARGET_DATA);
30777 TREE_TYPE (stmt) = void_type_node;
30779 OMP_TARGET_DATA_CLAUSES (stmt)
30780 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30781 "#pragma omp target data", pragma_tok);
30782 keep_next_level (true);
30783 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30785 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30786 return add_stmt (stmt);
30789 /* OpenMP 4.0:
30790 # pragma omp target update target-update-clause[optseq] new-line */
30792 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30793 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30798 static bool
30799 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30800 enum pragma_context context)
30802 if (context == pragma_stmt)
30804 error_at (pragma_tok->location,
30805 "%<#pragma omp target update%> may only be "
30806 "used in compound statements");
30807 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30808 return false;
30811 tree clauses
30812 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30813 "#pragma omp target update", pragma_tok);
30814 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30815 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30817 error_at (pragma_tok->location,
30818 "%<#pragma omp target update must contain at least one "
30819 "%<from%> or %<to%> clauses");
30820 return false;
30823 tree stmt = make_node (OMP_TARGET_UPDATE);
30824 TREE_TYPE (stmt) = void_type_node;
30825 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30826 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30827 add_stmt (stmt);
30828 return false;
30831 /* OpenMP 4.0:
30832 # pragma omp target target-clause[optseq] new-line
30833 structured-block */
30835 #define OMP_TARGET_CLAUSE_MASK \
30836 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30840 static bool
30841 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30842 enum pragma_context context)
30844 if (context != pragma_stmt && context != pragma_compound)
30846 cp_parser_error (parser, "expected declaration specifiers");
30847 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30848 return false;
30851 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30853 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30854 const char *p = IDENTIFIER_POINTER (id);
30856 if (strcmp (p, "teams") == 0)
30858 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30859 char p_name[sizeof ("#pragma omp target teams distribute "
30860 "parallel for simd")];
30862 cp_lexer_consume_token (parser->lexer);
30863 strcpy (p_name, "#pragma omp target");
30864 if (!flag_openmp) /* flag_openmp_simd */
30866 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
30867 OMP_TARGET_CLAUSE_MASK,
30868 cclauses);
30869 return stmt != NULL_TREE;
30871 keep_next_level (true);
30872 tree sb = begin_omp_structured_block ();
30873 unsigned save = cp_parser_begin_omp_structured_block (parser);
30874 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30875 OMP_TARGET_CLAUSE_MASK, cclauses);
30876 cp_parser_end_omp_structured_block (parser, save);
30877 tree body = finish_omp_structured_block (sb);
30878 if (ret == NULL_TREE)
30879 return false;
30880 tree stmt = make_node (OMP_TARGET);
30881 TREE_TYPE (stmt) = void_type_node;
30882 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30883 OMP_TARGET_BODY (stmt) = body;
30884 add_stmt (stmt);
30885 return true;
30887 else if (!flag_openmp) /* flag_openmp_simd */
30889 cp_parser_require_pragma_eol (parser, pragma_tok);
30890 return false;
30892 else if (strcmp (p, "data") == 0)
30894 cp_lexer_consume_token (parser->lexer);
30895 cp_parser_omp_target_data (parser, pragma_tok);
30896 return true;
30898 else if (strcmp (p, "update") == 0)
30900 cp_lexer_consume_token (parser->lexer);
30901 return cp_parser_omp_target_update (parser, pragma_tok, context);
30905 tree stmt = make_node (OMP_TARGET);
30906 TREE_TYPE (stmt) = void_type_node;
30908 OMP_TARGET_CLAUSES (stmt)
30909 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30910 "#pragma omp target", pragma_tok);
30911 keep_next_level (true);
30912 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30914 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30915 add_stmt (stmt);
30916 return true;
30919 /* OpenMP 4.0:
30920 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30922 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30923 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30930 static void
30931 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30932 enum pragma_context context)
30934 bool first_p = parser->omp_declare_simd == NULL;
30935 cp_omp_declare_simd_data data;
30936 if (first_p)
30938 data.error_seen = false;
30939 data.fndecl_seen = false;
30940 data.tokens = vNULL;
30941 parser->omp_declare_simd = &data;
30943 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30944 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30945 cp_lexer_consume_token (parser->lexer);
30946 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30947 parser->omp_declare_simd->error_seen = true;
30948 cp_parser_require_pragma_eol (parser, pragma_tok);
30949 struct cp_token_cache *cp
30950 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30951 parser->omp_declare_simd->tokens.safe_push (cp);
30952 if (first_p)
30954 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30955 cp_parser_pragma (parser, context);
30956 switch (context)
30958 case pragma_external:
30959 cp_parser_declaration (parser);
30960 break;
30961 case pragma_member:
30962 cp_parser_member_declaration (parser);
30963 break;
30964 case pragma_objc_icode:
30965 cp_parser_block_declaration (parser, /*statement_p=*/false);
30966 break;
30967 default:
30968 cp_parser_declaration_statement (parser);
30969 break;
30971 if (parser->omp_declare_simd
30972 && !parser->omp_declare_simd->error_seen
30973 && !parser->omp_declare_simd->fndecl_seen)
30974 error_at (pragma_tok->location,
30975 "%<#pragma omp declare simd%> not immediately followed by "
30976 "function declaration or definition");
30977 data.tokens.release ();
30978 parser->omp_declare_simd = NULL;
30982 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30983 This function is modelled similar to the late parsing of omp declare
30984 simd. */
30986 static tree
30987 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30989 struct cp_token_cache *ce;
30990 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30991 int ii = 0;
30993 if (parser->omp_declare_simd != NULL)
30995 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30996 " marked as a Cilk Plus SIMD-enabled function");
30997 XDELETE (parser->cilk_simd_fn_info);
30998 parser->cilk_simd_fn_info = NULL;
30999 return attrs;
31001 if (!info->error_seen && info->fndecl_seen)
31003 error ("vector attribute not immediately followed by a single function"
31004 " declaration or definition");
31005 info->error_seen = true;
31007 if (info->error_seen)
31008 return attrs;
31010 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31012 tree c, cl;
31014 cp_parser_push_lexer_for_tokens (parser, ce);
31015 parser->lexer->in_pragma = true;
31016 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31017 "SIMD-enabled functions attribute",
31018 NULL);
31019 cp_parser_pop_lexer (parser);
31020 if (cl)
31021 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31023 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31024 TREE_CHAIN (c) = attrs;
31025 attrs = c;
31027 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31028 TREE_CHAIN (c) = attrs;
31029 if (processing_template_decl)
31030 ATTR_IS_DEPENDENT (c) = 1;
31031 attrs = c;
31033 info->fndecl_seen = true;
31034 XDELETE (parser->cilk_simd_fn_info);
31035 parser->cilk_simd_fn_info = NULL;
31036 return attrs;
31039 /* Finalize #pragma omp declare simd clauses after direct declarator has
31040 been parsed, and put that into "omp declare simd" attribute. */
31042 static tree
31043 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31045 struct cp_token_cache *ce;
31046 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31047 int i;
31049 if (!data->error_seen && data->fndecl_seen)
31051 error ("%<#pragma omp declare simd%> not immediately followed by "
31052 "a single function declaration or definition");
31053 data->error_seen = true;
31054 return attrs;
31056 if (data->error_seen)
31057 return attrs;
31059 FOR_EACH_VEC_ELT (data->tokens, i, ce)
31061 tree c, cl;
31063 cp_parser_push_lexer_for_tokens (parser, ce);
31064 parser->lexer->in_pragma = true;
31065 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31066 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31067 cp_lexer_consume_token (parser->lexer);
31068 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31069 "#pragma omp declare simd", pragma_tok);
31070 cp_parser_pop_lexer (parser);
31071 if (cl)
31072 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31073 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31074 TREE_CHAIN (c) = attrs;
31075 if (processing_template_decl)
31076 ATTR_IS_DEPENDENT (c) = 1;
31077 attrs = c;
31080 data->fndecl_seen = true;
31081 return attrs;
31085 /* OpenMP 4.0:
31086 # pragma omp declare target new-line
31087 declarations and definitions
31088 # pragma omp end declare target new-line */
31090 static void
31091 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31093 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31094 scope_chain->omp_declare_target_attribute++;
31097 static void
31098 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
31100 const char *p = "";
31101 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31103 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31104 p = IDENTIFIER_POINTER (id);
31106 if (strcmp (p, "declare") == 0)
31108 cp_lexer_consume_token (parser->lexer);
31109 p = "";
31110 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31112 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31113 p = IDENTIFIER_POINTER (id);
31115 if (strcmp (p, "target") == 0)
31116 cp_lexer_consume_token (parser->lexer);
31117 else
31119 cp_parser_error (parser, "expected %<target%>");
31120 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31121 return;
31124 else
31126 cp_parser_error (parser, "expected %<declare%>");
31127 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31128 return;
31130 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31131 if (!scope_chain->omp_declare_target_attribute)
31132 error_at (pragma_tok->location,
31133 "%<#pragma omp end declare target%> without corresponding "
31134 "%<#pragma omp declare target%>");
31135 else
31136 scope_chain->omp_declare_target_attribute--;
31139 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31140 expression and optional initializer clause of
31141 #pragma omp declare reduction. We store the expression(s) as
31142 either 3, 6 or 7 special statements inside of the artificial function's
31143 body. The first two statements are DECL_EXPRs for the artificial
31144 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
31145 expression that uses those variables.
31146 If there was any INITIALIZER clause, this is followed by further statements,
31147 the fourth and fifth statements are DECL_EXPRs for the artificial
31148 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
31149 constructor variant (first token after open paren is not omp_priv),
31150 then the sixth statement is a statement with the function call expression
31151 that uses the OMP_PRIV and optionally OMP_ORIG variable.
31152 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
31153 to initialize the OMP_PRIV artificial variable and there is seventh
31154 statement, a DECL_EXPR of the OMP_PRIV statement again. */
31156 static bool
31157 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
31159 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
31160 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
31161 type = TREE_TYPE (type);
31162 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
31163 DECL_ARTIFICIAL (omp_out) = 1;
31164 pushdecl (omp_out);
31165 add_decl_expr (omp_out);
31166 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
31167 DECL_ARTIFICIAL (omp_in) = 1;
31168 pushdecl (omp_in);
31169 add_decl_expr (omp_in);
31170 tree combiner;
31171 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
31173 keep_next_level (true);
31174 tree block = begin_omp_structured_block ();
31175 combiner = cp_parser_expression (parser);
31176 finish_expr_stmt (combiner);
31177 block = finish_omp_structured_block (block);
31178 add_stmt (block);
31180 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31181 return false;
31183 const char *p = "";
31184 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31186 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31187 p = IDENTIFIER_POINTER (id);
31190 if (strcmp (p, "initializer") == 0)
31192 cp_lexer_consume_token (parser->lexer);
31193 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31194 return false;
31196 p = "";
31197 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31199 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31200 p = IDENTIFIER_POINTER (id);
31203 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
31204 DECL_ARTIFICIAL (omp_priv) = 1;
31205 pushdecl (omp_priv);
31206 add_decl_expr (omp_priv);
31207 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
31208 DECL_ARTIFICIAL (omp_orig) = 1;
31209 pushdecl (omp_orig);
31210 add_decl_expr (omp_orig);
31212 keep_next_level (true);
31213 block = begin_omp_structured_block ();
31215 bool ctor = false;
31216 if (strcmp (p, "omp_priv") == 0)
31218 bool is_direct_init, is_non_constant_init;
31219 ctor = true;
31220 cp_lexer_consume_token (parser->lexer);
31221 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
31222 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
31223 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31224 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
31225 == CPP_CLOSE_PAREN
31226 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
31227 == CPP_CLOSE_PAREN))
31229 finish_omp_structured_block (block);
31230 error ("invalid initializer clause");
31231 return false;
31233 initializer = cp_parser_initializer (parser, &is_direct_init,
31234 &is_non_constant_init);
31235 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
31236 NULL_TREE, LOOKUP_ONLYCONVERTING);
31238 else
31240 cp_parser_parse_tentatively (parser);
31241 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
31242 /*check_dependency_p=*/true,
31243 /*template_p=*/NULL,
31244 /*declarator_p=*/false,
31245 /*optional_p=*/false);
31246 vec<tree, va_gc> *args;
31247 if (fn_name == error_mark_node
31248 || cp_parser_error_occurred (parser)
31249 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31250 || ((args = cp_parser_parenthesized_expression_list
31251 (parser, non_attr, /*cast_p=*/false,
31252 /*allow_expansion_p=*/true,
31253 /*non_constant_p=*/NULL)),
31254 cp_parser_error_occurred (parser)))
31256 finish_omp_structured_block (block);
31257 cp_parser_abort_tentative_parse (parser);
31258 cp_parser_error (parser, "expected id-expression (arguments)");
31259 return false;
31261 unsigned int i;
31262 tree arg;
31263 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
31264 if (arg == omp_priv
31265 || (TREE_CODE (arg) == ADDR_EXPR
31266 && TREE_OPERAND (arg, 0) == omp_priv))
31267 break;
31268 cp_parser_abort_tentative_parse (parser);
31269 if (arg == NULL_TREE)
31270 error ("one of the initializer call arguments should be %<omp_priv%>"
31271 " or %<&omp_priv%>");
31272 initializer = cp_parser_postfix_expression (parser, false, false, false,
31273 false, NULL);
31274 finish_expr_stmt (initializer);
31277 block = finish_omp_structured_block (block);
31278 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
31279 add_stmt (block);
31281 if (ctor)
31282 add_decl_expr (omp_orig);
31284 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31285 return false;
31288 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31289 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31291 return true;
31294 /* OpenMP 4.0
31295 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31296 initializer-clause[opt] new-line
31298 initializer-clause:
31299 initializer (omp_priv initializer)
31300 initializer (function-name (argument-list)) */
31302 static void
31303 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31304 enum pragma_context)
31306 auto_vec<tree> types;
31307 enum tree_code reduc_code = ERROR_MARK;
31308 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31309 unsigned int i;
31310 cp_token *first_token;
31311 cp_token_cache *cp;
31312 int errs;
31313 void *p;
31315 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31316 p = obstack_alloc (&declarator_obstack, 0);
31318 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31319 goto fail;
31321 switch (cp_lexer_peek_token (parser->lexer)->type)
31323 case CPP_PLUS:
31324 reduc_code = PLUS_EXPR;
31325 break;
31326 case CPP_MULT:
31327 reduc_code = MULT_EXPR;
31328 break;
31329 case CPP_MINUS:
31330 reduc_code = MINUS_EXPR;
31331 break;
31332 case CPP_AND:
31333 reduc_code = BIT_AND_EXPR;
31334 break;
31335 case CPP_XOR:
31336 reduc_code = BIT_XOR_EXPR;
31337 break;
31338 case CPP_OR:
31339 reduc_code = BIT_IOR_EXPR;
31340 break;
31341 case CPP_AND_AND:
31342 reduc_code = TRUTH_ANDIF_EXPR;
31343 break;
31344 case CPP_OR_OR:
31345 reduc_code = TRUTH_ORIF_EXPR;
31346 break;
31347 case CPP_NAME:
31348 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31349 break;
31350 default:
31351 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31352 "%<|%>, %<&&%>, %<||%> or identifier");
31353 goto fail;
31356 if (reduc_code != ERROR_MARK)
31357 cp_lexer_consume_token (parser->lexer);
31359 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31360 if (reduc_id == error_mark_node)
31361 goto fail;
31363 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31364 goto fail;
31366 /* Types may not be defined in declare reduction type list. */
31367 const char *saved_message;
31368 saved_message = parser->type_definition_forbidden_message;
31369 parser->type_definition_forbidden_message
31370 = G_("types may not be defined in declare reduction type list");
31371 bool saved_colon_corrects_to_scope_p;
31372 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31373 parser->colon_corrects_to_scope_p = false;
31374 bool saved_colon_doesnt_start_class_def_p;
31375 saved_colon_doesnt_start_class_def_p
31376 = parser->colon_doesnt_start_class_def_p;
31377 parser->colon_doesnt_start_class_def_p = true;
31379 while (true)
31381 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31382 type = cp_parser_type_id (parser);
31383 if (type == error_mark_node)
31385 else if (ARITHMETIC_TYPE_P (type)
31386 && (orig_reduc_id == NULL_TREE
31387 || (TREE_CODE (type) != COMPLEX_TYPE
31388 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31389 "min") == 0
31390 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31391 "max") == 0))))
31392 error_at (loc, "predeclared arithmetic type %qT in "
31393 "%<#pragma omp declare reduction%>", type);
31394 else if (TREE_CODE (type) == FUNCTION_TYPE
31395 || TREE_CODE (type) == METHOD_TYPE
31396 || TREE_CODE (type) == ARRAY_TYPE)
31397 error_at (loc, "function or array type %qT in "
31398 "%<#pragma omp declare reduction%>", type);
31399 else if (TREE_CODE (type) == REFERENCE_TYPE)
31400 error_at (loc, "reference type %qT in "
31401 "%<#pragma omp declare reduction%>", type);
31402 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31403 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31404 "%<#pragma omp declare reduction%>", type);
31405 else
31406 types.safe_push (type);
31408 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31409 cp_lexer_consume_token (parser->lexer);
31410 else
31411 break;
31414 /* Restore the saved message. */
31415 parser->type_definition_forbidden_message = saved_message;
31416 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31417 parser->colon_doesnt_start_class_def_p
31418 = saved_colon_doesnt_start_class_def_p;
31420 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31421 || types.is_empty ())
31423 fail:
31424 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31425 goto done;
31428 first_token = cp_lexer_peek_token (parser->lexer);
31429 cp = NULL;
31430 errs = errorcount;
31431 FOR_EACH_VEC_ELT (types, i, type)
31433 tree fntype
31434 = build_function_type_list (void_type_node,
31435 cp_build_reference_type (type, false),
31436 NULL_TREE);
31437 tree this_reduc_id = reduc_id;
31438 if (!dependent_type_p (type))
31439 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31440 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31441 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31442 DECL_ARTIFICIAL (fndecl) = 1;
31443 DECL_EXTERNAL (fndecl) = 1;
31444 DECL_DECLARED_INLINE_P (fndecl) = 1;
31445 DECL_IGNORED_P (fndecl) = 1;
31446 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31447 DECL_ATTRIBUTES (fndecl)
31448 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31449 DECL_ATTRIBUTES (fndecl));
31450 if (processing_template_decl)
31451 fndecl = push_template_decl (fndecl);
31452 bool block_scope = false;
31453 tree block = NULL_TREE;
31454 if (current_function_decl)
31456 block_scope = true;
31457 DECL_CONTEXT (fndecl) = global_namespace;
31458 if (!processing_template_decl)
31459 pushdecl (fndecl);
31461 else if (current_class_type)
31463 if (cp == NULL)
31465 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31466 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31467 cp_lexer_consume_token (parser->lexer);
31468 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31469 goto fail;
31470 cp = cp_token_cache_new (first_token,
31471 cp_lexer_peek_nth_token (parser->lexer,
31472 2));
31474 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31475 finish_member_declaration (fndecl);
31476 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31477 DECL_PENDING_INLINE_P (fndecl) = 1;
31478 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31479 continue;
31481 else
31483 DECL_CONTEXT (fndecl) = current_namespace;
31484 pushdecl (fndecl);
31486 if (!block_scope)
31487 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31488 else
31489 block = begin_omp_structured_block ();
31490 if (cp)
31492 cp_parser_push_lexer_for_tokens (parser, cp);
31493 parser->lexer->in_pragma = true;
31495 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31497 if (!block_scope)
31498 finish_function (0);
31499 else
31500 DECL_CONTEXT (fndecl) = current_function_decl;
31501 if (cp)
31502 cp_parser_pop_lexer (parser);
31503 goto fail;
31505 if (cp)
31506 cp_parser_pop_lexer (parser);
31507 if (!block_scope)
31508 finish_function (0);
31509 else
31511 DECL_CONTEXT (fndecl) = current_function_decl;
31512 block = finish_omp_structured_block (block);
31513 if (TREE_CODE (block) == BIND_EXPR)
31514 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31515 else if (TREE_CODE (block) == STATEMENT_LIST)
31516 DECL_SAVED_TREE (fndecl) = block;
31517 if (processing_template_decl)
31518 add_decl_expr (fndecl);
31520 cp_check_omp_declare_reduction (fndecl);
31521 if (cp == NULL && types.length () > 1)
31522 cp = cp_token_cache_new (first_token,
31523 cp_lexer_peek_nth_token (parser->lexer, 2));
31524 if (errs != errorcount)
31525 break;
31528 cp_parser_require_pragma_eol (parser, pragma_tok);
31530 done:
31531 /* Free any declarators allocated. */
31532 obstack_free (&declarator_obstack, p);
31535 /* OpenMP 4.0
31536 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31537 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31538 initializer-clause[opt] new-line
31539 #pragma omp declare target new-line */
31541 static void
31542 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31543 enum pragma_context context)
31545 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31547 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31548 const char *p = IDENTIFIER_POINTER (id);
31550 if (strcmp (p, "simd") == 0)
31552 cp_lexer_consume_token (parser->lexer);
31553 cp_parser_omp_declare_simd (parser, pragma_tok,
31554 context);
31555 return;
31557 cp_ensure_no_omp_declare_simd (parser);
31558 if (strcmp (p, "reduction") == 0)
31560 cp_lexer_consume_token (parser->lexer);
31561 cp_parser_omp_declare_reduction (parser, pragma_tok,
31562 context);
31563 return;
31565 if (!flag_openmp) /* flag_openmp_simd */
31567 cp_parser_require_pragma_eol (parser, pragma_tok);
31568 return;
31570 if (strcmp (p, "target") == 0)
31572 cp_lexer_consume_token (parser->lexer);
31573 cp_parser_omp_declare_target (parser, pragma_tok);
31574 return;
31577 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31578 "or %<target%>");
31579 cp_parser_require_pragma_eol (parser, pragma_tok);
31582 /* Main entry point to OpenMP statement pragmas. */
31584 static void
31585 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31587 tree stmt;
31588 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31589 omp_clause_mask mask (0);
31591 switch (pragma_tok->pragma_kind)
31593 case PRAGMA_OMP_ATOMIC:
31594 cp_parser_omp_atomic (parser, pragma_tok);
31595 return;
31596 case PRAGMA_OMP_CRITICAL:
31597 stmt = cp_parser_omp_critical (parser, pragma_tok);
31598 break;
31599 case PRAGMA_OMP_DISTRIBUTE:
31600 strcpy (p_name, "#pragma omp");
31601 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31602 break;
31603 case PRAGMA_OMP_FOR:
31604 strcpy (p_name, "#pragma omp");
31605 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31606 break;
31607 case PRAGMA_OMP_MASTER:
31608 stmt = cp_parser_omp_master (parser, pragma_tok);
31609 break;
31610 case PRAGMA_OMP_ORDERED:
31611 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31612 break;
31613 case PRAGMA_OMP_PARALLEL:
31614 strcpy (p_name, "#pragma omp");
31615 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31616 break;
31617 case PRAGMA_OMP_SECTIONS:
31618 strcpy (p_name, "#pragma omp");
31619 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31620 break;
31621 case PRAGMA_OMP_SIMD:
31622 strcpy (p_name, "#pragma omp");
31623 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31624 break;
31625 case PRAGMA_OMP_SINGLE:
31626 stmt = cp_parser_omp_single (parser, pragma_tok);
31627 break;
31628 case PRAGMA_OMP_TASK:
31629 stmt = cp_parser_omp_task (parser, pragma_tok);
31630 break;
31631 case PRAGMA_OMP_TASKGROUP:
31632 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31633 break;
31634 case PRAGMA_OMP_TEAMS:
31635 strcpy (p_name, "#pragma omp");
31636 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31637 break;
31638 default:
31639 gcc_unreachable ();
31642 if (stmt)
31643 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31646 /* Transactional Memory parsing routines. */
31648 /* Parse a transaction attribute.
31650 txn-attribute:
31651 attribute
31652 [ [ identifier ] ]
31654 ??? Simplify this when C++0x bracket attributes are
31655 implemented properly. */
31657 static tree
31658 cp_parser_txn_attribute_opt (cp_parser *parser)
31660 cp_token *token;
31661 tree attr_name, attr = NULL;
31663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31664 return cp_parser_attributes_opt (parser);
31666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31667 return NULL_TREE;
31668 cp_lexer_consume_token (parser->lexer);
31669 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31670 goto error1;
31672 token = cp_lexer_peek_token (parser->lexer);
31673 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31675 token = cp_lexer_consume_token (parser->lexer);
31677 attr_name = (token->type == CPP_KEYWORD
31678 /* For keywords, use the canonical spelling,
31679 not the parsed identifier. */
31680 ? ridpointers[(int) token->keyword]
31681 : token->u.value);
31682 attr = build_tree_list (attr_name, NULL_TREE);
31684 else
31685 cp_parser_error (parser, "expected identifier");
31687 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31688 error1:
31689 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31690 return attr;
31693 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31695 transaction-statement:
31696 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31697 compound-statement
31698 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31701 static tree
31702 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31704 unsigned char old_in = parser->in_transaction;
31705 unsigned char this_in = 1, new_in;
31706 cp_token *token;
31707 tree stmt, attrs, noex;
31709 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31710 || keyword == RID_TRANSACTION_RELAXED);
31711 token = cp_parser_require_keyword (parser, keyword,
31712 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31713 : RT_TRANSACTION_RELAXED));
31714 gcc_assert (token != NULL);
31716 if (keyword == RID_TRANSACTION_RELAXED)
31717 this_in |= TM_STMT_ATTR_RELAXED;
31718 else
31720 attrs = cp_parser_txn_attribute_opt (parser);
31721 if (attrs)
31722 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31725 /* Parse a noexcept specification. */
31726 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31728 /* Keep track if we're in the lexical scope of an outer transaction. */
31729 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31731 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31733 parser->in_transaction = new_in;
31734 cp_parser_compound_statement (parser, NULL, false, false);
31735 parser->in_transaction = old_in;
31737 finish_transaction_stmt (stmt, NULL, this_in, noex);
31739 return stmt;
31742 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31744 transaction-expression:
31745 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31746 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31749 static tree
31750 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31752 unsigned char old_in = parser->in_transaction;
31753 unsigned char this_in = 1;
31754 cp_token *token;
31755 tree expr, noex;
31756 bool noex_expr;
31758 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31759 || keyword == RID_TRANSACTION_RELAXED);
31761 if (!flag_tm)
31762 error (keyword == RID_TRANSACTION_RELAXED
31763 ? G_("%<__transaction_relaxed%> without transactional memory "
31764 "support enabled")
31765 : G_("%<__transaction_atomic%> without transactional memory "
31766 "support enabled"));
31768 token = cp_parser_require_keyword (parser, keyword,
31769 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31770 : RT_TRANSACTION_RELAXED));
31771 gcc_assert (token != NULL);
31773 if (keyword == RID_TRANSACTION_RELAXED)
31774 this_in |= TM_STMT_ATTR_RELAXED;
31776 /* Set this early. This might mean that we allow transaction_cancel in
31777 an expression that we find out later actually has to be a constexpr.
31778 However, we expect that cxx_constant_value will be able to deal with
31779 this; also, if the noexcept has no constexpr, then what we parse next
31780 really is a transaction's body. */
31781 parser->in_transaction = this_in;
31783 /* Parse a noexcept specification. */
31784 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31785 true);
31787 if (!noex || !noex_expr
31788 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31790 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31792 expr = cp_parser_expression (parser);
31793 expr = finish_parenthesized_expr (expr);
31795 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31797 else
31799 /* The only expression that is available got parsed for the noexcept
31800 already. noexcept is true then. */
31801 expr = noex;
31802 noex = boolean_true_node;
31805 expr = build_transaction_expr (token->location, expr, this_in, noex);
31806 parser->in_transaction = old_in;
31808 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31809 return error_mark_node;
31811 return (flag_tm ? expr : error_mark_node);
31814 /* Parse a function-transaction-block.
31816 function-transaction-block:
31817 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31818 function-body
31819 __transaction_atomic txn-attribute[opt] function-try-block
31820 __transaction_relaxed ctor-initializer[opt] function-body
31821 __transaction_relaxed function-try-block
31824 static bool
31825 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31827 unsigned char old_in = parser->in_transaction;
31828 unsigned char new_in = 1;
31829 tree compound_stmt, stmt, attrs;
31830 bool ctor_initializer_p;
31831 cp_token *token;
31833 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31834 || keyword == RID_TRANSACTION_RELAXED);
31835 token = cp_parser_require_keyword (parser, keyword,
31836 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31837 : RT_TRANSACTION_RELAXED));
31838 gcc_assert (token != NULL);
31840 if (keyword == RID_TRANSACTION_RELAXED)
31841 new_in |= TM_STMT_ATTR_RELAXED;
31842 else
31844 attrs = cp_parser_txn_attribute_opt (parser);
31845 if (attrs)
31846 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31849 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31851 parser->in_transaction = new_in;
31853 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31854 ctor_initializer_p = cp_parser_function_try_block (parser);
31855 else
31856 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31857 (parser, /*in_function_try_block=*/false);
31859 parser->in_transaction = old_in;
31861 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31863 return ctor_initializer_p;
31866 /* Parse a __transaction_cancel statement.
31868 cancel-statement:
31869 __transaction_cancel txn-attribute[opt] ;
31870 __transaction_cancel txn-attribute[opt] throw-expression ;
31872 ??? Cancel and throw is not yet implemented. */
31874 static tree
31875 cp_parser_transaction_cancel (cp_parser *parser)
31877 cp_token *token;
31878 bool is_outer = false;
31879 tree stmt, attrs;
31881 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31882 RT_TRANSACTION_CANCEL);
31883 gcc_assert (token != NULL);
31885 attrs = cp_parser_txn_attribute_opt (parser);
31886 if (attrs)
31887 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31889 /* ??? Parse cancel-and-throw here. */
31891 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31893 if (!flag_tm)
31895 error_at (token->location, "%<__transaction_cancel%> without "
31896 "transactional memory support enabled");
31897 return error_mark_node;
31899 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31901 error_at (token->location, "%<__transaction_cancel%> within a "
31902 "%<__transaction_relaxed%>");
31903 return error_mark_node;
31905 else if (is_outer)
31907 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31908 && !is_tm_may_cancel_outer (current_function_decl))
31910 error_at (token->location, "outer %<__transaction_cancel%> not "
31911 "within outer %<__transaction_atomic%>");
31912 error_at (token->location,
31913 " or a %<transaction_may_cancel_outer%> function");
31914 return error_mark_node;
31917 else if (parser->in_transaction == 0)
31919 error_at (token->location, "%<__transaction_cancel%> not within "
31920 "%<__transaction_atomic%>");
31921 return error_mark_node;
31924 stmt = build_tm_abort_call (token->location, is_outer);
31925 add_stmt (stmt);
31927 return stmt;
31930 /* The parser. */
31932 static GTY (()) cp_parser *the_parser;
31935 /* Special handling for the first token or line in the file. The first
31936 thing in the file might be #pragma GCC pch_preprocess, which loads a
31937 PCH file, which is a GC collection point. So we need to handle this
31938 first pragma without benefit of an existing lexer structure.
31940 Always returns one token to the caller in *FIRST_TOKEN. This is
31941 either the true first token of the file, or the first token after
31942 the initial pragma. */
31944 static void
31945 cp_parser_initial_pragma (cp_token *first_token)
31947 tree name = NULL;
31949 cp_lexer_get_preprocessor_token (NULL, first_token);
31950 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31951 return;
31953 cp_lexer_get_preprocessor_token (NULL, first_token);
31954 if (first_token->type == CPP_STRING)
31956 name = first_token->u.value;
31958 cp_lexer_get_preprocessor_token (NULL, first_token);
31959 if (first_token->type != CPP_PRAGMA_EOL)
31960 error_at (first_token->location,
31961 "junk at end of %<#pragma GCC pch_preprocess%>");
31963 else
31964 error_at (first_token->location, "expected string literal");
31966 /* Skip to the end of the pragma. */
31967 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31968 cp_lexer_get_preprocessor_token (NULL, first_token);
31970 /* Now actually load the PCH file. */
31971 if (name)
31972 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31974 /* Read one more token to return to our caller. We have to do this
31975 after reading the PCH file in, since its pointers have to be
31976 live. */
31977 cp_lexer_get_preprocessor_token (NULL, first_token);
31980 /* Parses the grainsize pragma for the _Cilk_for statement.
31981 Syntax:
31982 #pragma cilk grainsize = <VALUE>. */
31984 static void
31985 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
31987 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
31989 tree exp = cp_parser_binary_expression (parser, false, false,
31990 PREC_NOT_OPERATOR, NULL);
31991 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31992 if (!exp || exp == error_mark_node)
31994 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
31995 return;
31998 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
31999 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32000 cp_parser_cilk_for (parser, exp);
32001 else
32002 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
32003 "%<#pragma cilk grainsize%> is not followed by "
32004 "%<_Cilk_for%>");
32005 return;
32007 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32010 /* Normal parsing of a pragma token. Here we can (and must) use the
32011 regular lexer. */
32013 static bool
32014 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32016 cp_token *pragma_tok;
32017 unsigned int id;
32019 pragma_tok = cp_lexer_consume_token (parser->lexer);
32020 gcc_assert (pragma_tok->type == CPP_PRAGMA);
32021 parser->lexer->in_pragma = true;
32023 id = pragma_tok->pragma_kind;
32024 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
32025 cp_ensure_no_omp_declare_simd (parser);
32026 switch (id)
32028 case PRAGMA_GCC_PCH_PREPROCESS:
32029 error_at (pragma_tok->location,
32030 "%<#pragma GCC pch_preprocess%> must be first");
32031 break;
32033 case PRAGMA_OMP_BARRIER:
32034 switch (context)
32036 case pragma_compound:
32037 cp_parser_omp_barrier (parser, pragma_tok);
32038 return false;
32039 case pragma_stmt:
32040 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32041 "used in compound statements");
32042 break;
32043 default:
32044 goto bad_stmt;
32046 break;
32048 case PRAGMA_OMP_FLUSH:
32049 switch (context)
32051 case pragma_compound:
32052 cp_parser_omp_flush (parser, pragma_tok);
32053 return false;
32054 case pragma_stmt:
32055 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32056 "used in compound statements");
32057 break;
32058 default:
32059 goto bad_stmt;
32061 break;
32063 case PRAGMA_OMP_TASKWAIT:
32064 switch (context)
32066 case pragma_compound:
32067 cp_parser_omp_taskwait (parser, pragma_tok);
32068 return false;
32069 case pragma_stmt:
32070 error_at (pragma_tok->location,
32071 "%<#pragma omp taskwait%> may only be "
32072 "used in compound statements");
32073 break;
32074 default:
32075 goto bad_stmt;
32077 break;
32079 case PRAGMA_OMP_TASKYIELD:
32080 switch (context)
32082 case pragma_compound:
32083 cp_parser_omp_taskyield (parser, pragma_tok);
32084 return false;
32085 case pragma_stmt:
32086 error_at (pragma_tok->location,
32087 "%<#pragma omp taskyield%> may only be "
32088 "used in compound statements");
32089 break;
32090 default:
32091 goto bad_stmt;
32093 break;
32095 case PRAGMA_OMP_CANCEL:
32096 switch (context)
32098 case pragma_compound:
32099 cp_parser_omp_cancel (parser, pragma_tok);
32100 return false;
32101 case pragma_stmt:
32102 error_at (pragma_tok->location,
32103 "%<#pragma omp cancel%> may only be "
32104 "used in compound statements");
32105 break;
32106 default:
32107 goto bad_stmt;
32109 break;
32111 case PRAGMA_OMP_CANCELLATION_POINT:
32112 switch (context)
32114 case pragma_compound:
32115 cp_parser_omp_cancellation_point (parser, pragma_tok);
32116 return false;
32117 case pragma_stmt:
32118 error_at (pragma_tok->location,
32119 "%<#pragma omp cancellation point%> may only be "
32120 "used in compound statements");
32121 break;
32122 default:
32123 goto bad_stmt;
32125 break;
32127 case PRAGMA_OMP_THREADPRIVATE:
32128 cp_parser_omp_threadprivate (parser, pragma_tok);
32129 return false;
32131 case PRAGMA_OMP_DECLARE_REDUCTION:
32132 cp_parser_omp_declare (parser, pragma_tok, context);
32133 return false;
32135 case PRAGMA_OMP_ATOMIC:
32136 case PRAGMA_OMP_CRITICAL:
32137 case PRAGMA_OMP_DISTRIBUTE:
32138 case PRAGMA_OMP_FOR:
32139 case PRAGMA_OMP_MASTER:
32140 case PRAGMA_OMP_ORDERED:
32141 case PRAGMA_OMP_PARALLEL:
32142 case PRAGMA_OMP_SECTIONS:
32143 case PRAGMA_OMP_SIMD:
32144 case PRAGMA_OMP_SINGLE:
32145 case PRAGMA_OMP_TASK:
32146 case PRAGMA_OMP_TASKGROUP:
32147 case PRAGMA_OMP_TEAMS:
32148 if (context != pragma_stmt && context != pragma_compound)
32149 goto bad_stmt;
32150 cp_parser_omp_construct (parser, pragma_tok);
32151 return true;
32153 case PRAGMA_OMP_TARGET:
32154 return cp_parser_omp_target (parser, pragma_tok, context);
32156 case PRAGMA_OMP_END_DECLARE_TARGET:
32157 cp_parser_omp_end_declare_target (parser, pragma_tok);
32158 return false;
32160 case PRAGMA_OMP_SECTION:
32161 error_at (pragma_tok->location,
32162 "%<#pragma omp section%> may only be used in "
32163 "%<#pragma omp sections%> construct");
32164 break;
32166 case PRAGMA_IVDEP:
32168 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32169 cp_token *tok;
32170 tok = cp_lexer_peek_token (the_parser->lexer);
32171 if (tok->type != CPP_KEYWORD
32172 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
32173 && tok->keyword != RID_DO))
32175 cp_parser_error (parser, "for, while or do statement expected");
32176 return false;
32178 cp_parser_iteration_statement (parser, true);
32179 return true;
32182 case PRAGMA_CILK_SIMD:
32183 if (context == pragma_external)
32185 error_at (pragma_tok->location,
32186 "%<#pragma simd%> must be inside a function");
32187 break;
32189 cp_parser_cilk_simd (parser, pragma_tok);
32190 return true;
32192 case PRAGMA_CILK_GRAINSIZE:
32193 if (context == pragma_external)
32195 error_at (pragma_tok->location,
32196 "%<#pragma cilk grainsize%> must be inside a function");
32197 break;
32200 /* Ignore the pragma if Cilk Plus is not enabled. */
32201 if (flag_cilkplus)
32203 cp_parser_cilk_grainsize (parser, pragma_tok);
32204 return true;
32206 else
32208 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
32209 "%<#pragma cilk grainsize%>");
32210 break;
32213 default:
32214 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
32215 c_invoke_pragma_handler (id);
32216 break;
32218 bad_stmt:
32219 cp_parser_error (parser, "expected declaration specifiers");
32220 break;
32223 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32224 return false;
32227 /* The interface the pragma parsers have to the lexer. */
32229 enum cpp_ttype
32230 pragma_lex (tree *value)
32232 cp_token *tok;
32233 enum cpp_ttype ret;
32235 tok = cp_lexer_peek_token (the_parser->lexer);
32237 ret = tok->type;
32238 *value = tok->u.value;
32240 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
32241 ret = CPP_EOF;
32242 else if (ret == CPP_STRING)
32243 *value = cp_parser_string_literal (the_parser, false, false);
32244 else
32246 cp_lexer_consume_token (the_parser->lexer);
32247 if (ret == CPP_KEYWORD)
32248 ret = CPP_NAME;
32251 return ret;
32255 /* External interface. */
32257 /* Parse one entire translation unit. */
32259 void
32260 c_parse_file (void)
32262 static bool already_called = false;
32264 if (already_called)
32265 fatal_error ("inter-module optimizations not implemented for C++");
32266 already_called = true;
32268 the_parser = cp_parser_new ();
32269 push_deferring_access_checks (flag_access_control
32270 ? dk_no_deferred : dk_no_check);
32271 cp_parser_translation_unit (the_parser);
32272 the_parser = NULL;
32275 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
32276 vectorlength clause:
32277 Syntax:
32278 vectorlength ( constant-expression ) */
32280 static tree
32281 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
32282 bool is_simd_fn)
32284 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32285 tree expr;
32286 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
32287 safelen clause. Thus, vectorlength is represented as OMP 4.0
32288 safelen. For SIMD-enabled function it is represented by OMP 4.0
32289 simdlen. */
32290 if (!is_simd_fn)
32291 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
32292 loc);
32293 else
32294 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
32295 loc);
32297 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32298 return error_mark_node;
32300 expr = cp_parser_constant_expression (parser);
32301 expr = maybe_constant_value (expr);
32303 /* If expr == error_mark_node, then don't emit any errors nor
32304 create a clause. if any of the above functions returns
32305 error mark node then they would have emitted an error message. */
32306 if (expr == error_mark_node)
32308 else if (!TREE_TYPE (expr)
32309 || !TREE_CONSTANT (expr)
32310 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
32311 error_at (loc, "vectorlength must be an integer constant");
32312 else if (TREE_CONSTANT (expr)
32313 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
32314 error_at (loc, "vectorlength must be a power of 2");
32315 else
32317 tree c;
32318 if (!is_simd_fn)
32320 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
32321 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
32322 OMP_CLAUSE_CHAIN (c) = clauses;
32323 clauses = c;
32325 else
32327 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
32328 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
32329 OMP_CLAUSE_CHAIN (c) = clauses;
32330 clauses = c;
32334 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32335 return error_mark_node;
32336 return clauses;
32339 /* Handles the Cilk Plus #pragma simd linear clause.
32340 Syntax:
32341 linear ( simd-linear-variable-list )
32343 simd-linear-variable-list:
32344 simd-linear-variable
32345 simd-linear-variable-list , simd-linear-variable
32347 simd-linear-variable:
32348 id-expression
32349 id-expression : simd-linear-step
32351 simd-linear-step:
32352 conditional-expression */
32354 static tree
32355 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
32357 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32359 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32360 return clauses;
32361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32363 cp_parser_error (parser, "expected identifier");
32364 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32365 return error_mark_node;
32368 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32369 parser->colon_corrects_to_scope_p = false;
32370 while (1)
32372 cp_token *token = cp_lexer_peek_token (parser->lexer);
32373 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32375 cp_parser_error (parser, "expected variable-name");
32376 clauses = error_mark_node;
32377 break;
32380 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32381 false, false);
32382 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32383 token->location);
32384 if (decl == error_mark_node)
32386 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32387 token->location);
32388 clauses = error_mark_node;
32390 else
32392 tree e = NULL_TREE;
32393 tree step_size = integer_one_node;
32395 /* If present, parse the linear step. Otherwise, assume the default
32396 value of 1. */
32397 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32399 cp_lexer_consume_token (parser->lexer);
32401 e = cp_parser_assignment_expression (parser);
32402 e = maybe_constant_value (e);
32404 if (e == error_mark_node)
32406 /* If an error has occurred, then the whole pragma is
32407 considered ill-formed. Thus, no reason to keep
32408 parsing. */
32409 clauses = error_mark_node;
32410 break;
32412 else if (type_dependent_expression_p (e)
32413 || value_dependent_expression_p (e)
32414 || (TREE_TYPE (e)
32415 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32416 && (TREE_CONSTANT (e)
32417 || DECL_P (e))))
32418 step_size = e;
32419 else
32420 cp_parser_error (parser,
32421 "step size must be an integer constant "
32422 "expression or an integer variable");
32425 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32426 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32427 OMP_CLAUSE_DECL (l) = decl;
32428 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32429 OMP_CLAUSE_CHAIN (l) = clauses;
32430 clauses = l;
32432 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32433 cp_lexer_consume_token (parser->lexer);
32434 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32435 break;
32436 else
32438 error_at (cp_lexer_peek_token (parser->lexer)->location,
32439 "expected %<,%> or %<)%> after %qE", decl);
32440 clauses = error_mark_node;
32441 break;
32444 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32445 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32446 return clauses;
32449 /* Returns the name of the next clause. If the clause is not
32450 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32451 token is not consumed. Otherwise, the appropriate enum from the
32452 pragma_simd_clause is returned and the token is consumed. */
32454 static pragma_omp_clause
32455 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32457 pragma_omp_clause clause_type;
32458 cp_token *token = cp_lexer_peek_token (parser->lexer);
32460 if (token->keyword == RID_PRIVATE)
32461 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32462 else if (!token->u.value || token->type != CPP_NAME)
32463 return PRAGMA_CILK_CLAUSE_NONE;
32464 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32465 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32466 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32467 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32468 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32469 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32470 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32471 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32472 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32473 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32474 else
32475 return PRAGMA_CILK_CLAUSE_NONE;
32477 cp_lexer_consume_token (parser->lexer);
32478 return clause_type;
32481 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32483 static tree
32484 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32486 tree clauses = NULL_TREE;
32488 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32489 && clauses != error_mark_node)
32491 pragma_omp_clause c_kind;
32492 c_kind = cp_parser_cilk_simd_clause_name (parser);
32493 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32494 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
32495 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32496 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32497 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32498 /* Use the OpenMP 4.0 equivalent function. */
32499 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32500 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32501 /* Use the OpenMP 4.0 equivalent function. */
32502 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32503 clauses);
32504 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32505 /* Use the OMP 4.0 equivalent function. */
32506 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32507 clauses);
32508 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32509 /* Use the OMP 4.0 equivalent function. */
32510 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32511 else
32513 clauses = error_mark_node;
32514 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32515 break;
32519 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32521 if (clauses == error_mark_node)
32522 return error_mark_node;
32523 else
32524 return c_finish_cilk_clauses (clauses);
32527 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32529 static void
32530 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32532 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32534 if (clauses == error_mark_node)
32535 return;
32537 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32539 error_at (cp_lexer_peek_token (parser->lexer)->location,
32540 "for statement expected");
32541 return;
32544 tree sb = begin_omp_structured_block ();
32545 int save = cp_parser_begin_omp_structured_block (parser);
32546 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32547 if (ret)
32548 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32549 cp_parser_end_omp_structured_block (parser, save);
32550 add_stmt (finish_omp_structured_block (sb));
32553 /* Main entry-point for parsing Cilk Plus _Cilk_for
32554 loops. The return value is error_mark_node
32555 when errors happen and CILK_FOR tree on success. */
32557 static tree
32558 cp_parser_cilk_for (cp_parser *parser, tree grain)
32560 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
32561 gcc_unreachable ();
32563 tree sb = begin_omp_structured_block ();
32564 int save = cp_parser_begin_omp_structured_block (parser);
32566 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
32567 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
32568 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
32569 clauses = finish_omp_clauses (clauses);
32571 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
32572 if (ret)
32573 cpp_validate_cilk_plus_loop (ret);
32574 else
32575 ret = error_mark_node;
32577 cp_parser_end_omp_structured_block (parser, save);
32578 add_stmt (finish_omp_structured_block (sb));
32579 return ret;
32582 /* Create an identifier for a generic parameter type (a synthesized
32583 template parameter implied by `auto' or a concept identifier). */
32585 static GTY(()) int generic_parm_count;
32586 static tree
32587 make_generic_type_name ()
32589 char buf[32];
32590 sprintf (buf, "auto:%d", ++generic_parm_count);
32591 return get_identifier (buf);
32594 /* Predicate that behaves as is_auto_or_concept but matches the parent
32595 node of the generic type rather than the generic type itself. This
32596 allows for type transformation in add_implicit_template_parms. */
32598 static inline bool
32599 tree_type_is_auto_or_concept (const_tree t)
32601 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32604 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32605 (creating a new template parameter list if necessary). Returns the newly
32606 created template type parm. */
32608 tree
32609 synthesize_implicit_template_parm (cp_parser *parser)
32611 gcc_assert (current_binding_level->kind == sk_function_parms);
32613 /* We are either continuing a function template that already contains implicit
32614 template parameters, creating a new fully-implicit function template, or
32615 extending an existing explicit function template with implicit template
32616 parameters. */
32618 cp_binding_level *const entry_scope = current_binding_level;
32620 bool become_template = false;
32621 cp_binding_level *parent_scope = 0;
32623 if (parser->implicit_template_scope)
32625 gcc_assert (parser->implicit_template_parms);
32627 current_binding_level = parser->implicit_template_scope;
32629 else
32631 /* Roll back to the existing template parameter scope (in the case of
32632 extending an explicit function template) or introduce a new template
32633 parameter scope ahead of the function parameter scope (or class scope
32634 in the case of out-of-line member definitions). The function scope is
32635 added back after template parameter synthesis below. */
32637 cp_binding_level *scope = entry_scope;
32639 while (scope->kind == sk_function_parms)
32641 parent_scope = scope;
32642 scope = scope->level_chain;
32644 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32646 /* If not defining a class, then any class scope is a scope level in
32647 an out-of-line member definition. In this case simply wind back
32648 beyond the first such scope to inject the template parameter list.
32649 Otherwise wind back to the class being defined. The latter can
32650 occur in class member friend declarations such as:
32652 class A {
32653 void foo (auto);
32655 class B {
32656 friend void A::foo (auto);
32659 The template parameter list synthesized for the friend declaration
32660 must be injected in the scope of 'B'. This can also occur in
32661 erroneous cases such as:
32663 struct A {
32664 struct B {
32665 void foo (auto);
32667 void B::foo (auto) {}
32670 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32671 but, nevertheless, the template parameter list synthesized for the
32672 declarator should be injected into the scope of 'A' as if the
32673 ill-formed template was specified explicitly. */
32675 while (scope->kind == sk_class && !scope->defining_class_p)
32677 parent_scope = scope;
32678 scope = scope->level_chain;
32682 current_binding_level = scope;
32684 if (scope->kind != sk_template_parms
32685 || !function_being_declared_is_template_p (parser))
32687 /* Introduce a new template parameter list for implicit template
32688 parameters. */
32690 become_template = true;
32692 parser->implicit_template_scope
32693 = begin_scope (sk_template_parms, NULL);
32695 ++processing_template_decl;
32697 parser->fully_implicit_function_template_p = true;
32698 ++parser->num_template_parameter_lists;
32700 else
32702 /* Synthesize implicit template parameters at the end of the explicit
32703 template parameter list. */
32705 gcc_assert (current_template_parms);
32707 parser->implicit_template_scope = scope;
32709 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32710 parser->implicit_template_parms
32711 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32715 /* Synthesize a new template parameter and track the current template
32716 parameter chain with implicit_template_parms. */
32718 tree synth_id = make_generic_type_name ();
32719 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32720 synth_id);
32721 tree new_parm
32722 = process_template_parm (parser->implicit_template_parms,
32723 input_location,
32724 build_tree_list (NULL_TREE, synth_tmpl_parm),
32725 /*non_type=*/false,
32726 /*param_pack=*/false);
32729 if (parser->implicit_template_parms)
32730 parser->implicit_template_parms
32731 = TREE_CHAIN (parser->implicit_template_parms);
32732 else
32733 parser->implicit_template_parms = new_parm;
32735 tree new_type = TREE_TYPE (getdecls ());
32737 /* If creating a fully implicit function template, start the new implicit
32738 template parameter list with this synthesized type, otherwise grow the
32739 current template parameter list. */
32741 if (become_template)
32743 parent_scope->level_chain = current_binding_level;
32745 tree new_parms = make_tree_vec (1);
32746 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32747 current_template_parms = tree_cons (size_int (processing_template_decl),
32748 new_parms, current_template_parms);
32750 else
32752 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32753 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32754 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32755 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32758 current_binding_level = entry_scope;
32760 return new_type;
32763 /* Finish the declaration of a fully implicit function template. Such a
32764 template has no explicit template parameter list so has not been through the
32765 normal template head and tail processing. synthesize_implicit_template_parm
32766 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32767 provided if the declaration is a class member such that its template
32768 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32769 form is returned. Otherwise NULL_TREE is returned. */
32771 tree
32772 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32774 gcc_assert (parser->fully_implicit_function_template_p);
32776 if (member_decl_opt && member_decl_opt != error_mark_node
32777 && DECL_VIRTUAL_P (member_decl_opt))
32779 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32780 "implicit templates may not be %<virtual%>");
32781 DECL_VIRTUAL_P (member_decl_opt) = false;
32784 if (member_decl_opt)
32785 member_decl_opt = finish_member_template_decl (member_decl_opt);
32786 end_template_decl ();
32788 parser->fully_implicit_function_template_p = false;
32789 --parser->num_template_parameter_lists;
32791 return member_decl_opt;
32794 #include "gt-cp-parser.h"