PR preprocessor/63831
[official-gcc.git] / gcc / cp / parser.c
blob8ff16ed770c409b00dd561a06394d0b88d3f92c2
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "print-tree.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "cp-tree.h"
33 #include "intl.h"
34 #include "c-family/c-pragma.h"
35 #include "decl.h"
36 #include "flags.h"
37 #include "diagnostic-core.h"
38 #include "target.h"
39 #include "hash-map.h"
40 #include "is-a.h"
41 #include "plugin-api.h"
42 #include "vec.h"
43 #include "hashtab.h"
44 #include "hash-set.h"
45 #include "machmode.h"
46 #include "hard-reg-set.h"
47 #include "input.h"
48 #include "function.h"
49 #include "ipa-ref.h"
50 #include "cgraph.h"
51 #include "c-family/c-common.h"
52 #include "c-family/c-objc.h"
53 #include "plugin.h"
54 #include "tree-pretty-print.h"
55 #include "parser.h"
56 #include "type-utils.h"
57 #include "omp-low.h"
60 /* The lexer. */
62 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
63 and c-lex.c) and the C++ parser. */
65 static cp_token eof_token =
67 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
70 /* The various kinds of non integral constant we encounter. */
71 typedef enum non_integral_constant {
72 NIC_NONE,
73 /* floating-point literal */
74 NIC_FLOAT,
75 /* %<this%> */
76 NIC_THIS,
77 /* %<__FUNCTION__%> */
78 NIC_FUNC_NAME,
79 /* %<__PRETTY_FUNCTION__%> */
80 NIC_PRETTY_FUNC,
81 /* %<__func__%> */
82 NIC_C99_FUNC,
83 /* "%<va_arg%> */
84 NIC_VA_ARG,
85 /* a cast */
86 NIC_CAST,
87 /* %<typeid%> operator */
88 NIC_TYPEID,
89 /* non-constant compound literals */
90 NIC_NCC,
91 /* a function call */
92 NIC_FUNC_CALL,
93 /* an increment */
94 NIC_INC,
95 /* an decrement */
96 NIC_DEC,
97 /* an array reference */
98 NIC_ARRAY_REF,
99 /* %<->%> */
100 NIC_ARROW,
101 /* %<.%> */
102 NIC_POINT,
103 /* the address of a label */
104 NIC_ADDR_LABEL,
105 /* %<*%> */
106 NIC_STAR,
107 /* %<&%> */
108 NIC_ADDR,
109 /* %<++%> */
110 NIC_PREINCREMENT,
111 /* %<--%> */
112 NIC_PREDECREMENT,
113 /* %<new%> */
114 NIC_NEW,
115 /* %<delete%> */
116 NIC_DEL,
117 /* calls to overloaded operators */
118 NIC_OVERLOADED,
119 /* an assignment */
120 NIC_ASSIGNMENT,
121 /* a comma operator */
122 NIC_COMMA,
123 /* a call to a constructor */
124 NIC_CONSTRUCTOR,
125 /* a transaction expression */
126 NIC_TRANSACTION
127 } non_integral_constant;
129 /* The various kinds of errors about name-lookup failing. */
130 typedef enum name_lookup_error {
131 /* NULL */
132 NLE_NULL,
133 /* is not a type */
134 NLE_TYPE,
135 /* is not a class or namespace */
136 NLE_CXX98,
137 /* is not a class, namespace, or enumeration */
138 NLE_NOT_CXX98
139 } name_lookup_error;
141 /* The various kinds of required token */
142 typedef enum required_token {
143 RT_NONE,
144 RT_SEMICOLON, /* ';' */
145 RT_OPEN_PAREN, /* '(' */
146 RT_CLOSE_BRACE, /* '}' */
147 RT_OPEN_BRACE, /* '{' */
148 RT_CLOSE_SQUARE, /* ']' */
149 RT_OPEN_SQUARE, /* '[' */
150 RT_COMMA, /* ',' */
151 RT_SCOPE, /* '::' */
152 RT_LESS, /* '<' */
153 RT_GREATER, /* '>' */
154 RT_EQ, /* '=' */
155 RT_ELLIPSIS, /* '...' */
156 RT_MULT, /* '*' */
157 RT_COMPL, /* '~' */
158 RT_COLON, /* ':' */
159 RT_COLON_SCOPE, /* ':' or '::' */
160 RT_CLOSE_PAREN, /* ')' */
161 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
162 RT_PRAGMA_EOL, /* end of line */
163 RT_NAME, /* identifier */
165 /* The type is CPP_KEYWORD */
166 RT_NEW, /* new */
167 RT_DELETE, /* delete */
168 RT_RETURN, /* return */
169 RT_WHILE, /* while */
170 RT_EXTERN, /* extern */
171 RT_STATIC_ASSERT, /* static_assert */
172 RT_DECLTYPE, /* decltype */
173 RT_OPERATOR, /* operator */
174 RT_CLASS, /* class */
175 RT_TEMPLATE, /* template */
176 RT_NAMESPACE, /* namespace */
177 RT_USING, /* using */
178 RT_ASM, /* asm */
179 RT_TRY, /* try */
180 RT_CATCH, /* catch */
181 RT_THROW, /* throw */
182 RT_LABEL, /* __label__ */
183 RT_AT_TRY, /* @try */
184 RT_AT_SYNCHRONIZED, /* @synchronized */
185 RT_AT_THROW, /* @throw */
187 RT_SELECT, /* selection-statement */
188 RT_INTERATION, /* iteration-statement */
189 RT_JUMP, /* jump-statement */
190 RT_CLASS_KEY, /* class-key */
191 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
192 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
193 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
194 RT_TRANSACTION_CANCEL /* __transaction_cancel */
195 } required_token;
197 /* Prototypes. */
199 static cp_lexer *cp_lexer_new_main
200 (void);
201 static cp_lexer *cp_lexer_new_from_tokens
202 (cp_token_cache *tokens);
203 static void cp_lexer_destroy
204 (cp_lexer *);
205 static int cp_lexer_saving_tokens
206 (const cp_lexer *);
207 static cp_token *cp_lexer_token_at
208 (cp_lexer *, cp_token_position);
209 static void cp_lexer_get_preprocessor_token
210 (cp_lexer *, cp_token *);
211 static inline cp_token *cp_lexer_peek_token
212 (cp_lexer *);
213 static cp_token *cp_lexer_peek_nth_token
214 (cp_lexer *, size_t);
215 static inline bool cp_lexer_next_token_is
216 (cp_lexer *, enum cpp_ttype);
217 static bool cp_lexer_next_token_is_not
218 (cp_lexer *, enum cpp_ttype);
219 static bool cp_lexer_next_token_is_keyword
220 (cp_lexer *, enum rid);
221 static cp_token *cp_lexer_consume_token
222 (cp_lexer *);
223 static void cp_lexer_purge_token
224 (cp_lexer *);
225 static void cp_lexer_purge_tokens_after
226 (cp_lexer *, cp_token_position);
227 static void cp_lexer_save_tokens
228 (cp_lexer *);
229 static void cp_lexer_commit_tokens
230 (cp_lexer *);
231 static void cp_lexer_rollback_tokens
232 (cp_lexer *);
233 static void cp_lexer_print_token
234 (FILE *, cp_token *);
235 static inline bool cp_lexer_debugging_p
236 (cp_lexer *);
237 static void cp_lexer_start_debugging
238 (cp_lexer *) ATTRIBUTE_UNUSED;
239 static void cp_lexer_stop_debugging
240 (cp_lexer *) ATTRIBUTE_UNUSED;
242 static cp_token_cache *cp_token_cache_new
243 (cp_token *, cp_token *);
245 static void cp_parser_initial_pragma
246 (cp_token *);
248 static tree cp_literal_operator_id
249 (const char *);
251 static void cp_parser_cilk_simd
252 (cp_parser *, cp_token *);
253 static tree cp_parser_cilk_for
254 (cp_parser *, tree);
255 static bool cp_parser_omp_declare_reduction_exprs
256 (tree, cp_parser *);
257 static tree cp_parser_cilk_simd_vectorlength
258 (cp_parser *, tree, bool);
260 /* Manifest constants. */
261 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
262 #define CP_SAVED_TOKEN_STACK 5
264 /* Variables. */
266 /* The stream to which debugging output should be written. */
267 static FILE *cp_lexer_debug_stream;
269 /* Nonzero if we are parsing an unevaluated operand: an operand to
270 sizeof, typeof, or alignof. */
271 int cp_unevaluated_operand;
273 /* Dump up to NUM tokens in BUFFER to FILE starting with token
274 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
275 first token in BUFFER. If NUM is 0, dump all the tokens. If
276 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
277 highlighted by surrounding it in [[ ]]. */
279 static void
280 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
281 cp_token *start_token, unsigned num,
282 cp_token *curr_token)
284 unsigned i, nprinted;
285 cp_token *token;
286 bool do_print;
288 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
290 if (buffer == NULL)
291 return;
293 if (num == 0)
294 num = buffer->length ();
296 if (start_token == NULL)
297 start_token = buffer->address ();
299 if (start_token > buffer->address ())
301 cp_lexer_print_token (file, &(*buffer)[0]);
302 fprintf (file, " ... ");
305 do_print = false;
306 nprinted = 0;
307 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
309 if (token == start_token)
310 do_print = true;
312 if (!do_print)
313 continue;
315 nprinted++;
316 if (token == curr_token)
317 fprintf (file, "[[");
319 cp_lexer_print_token (file, token);
321 if (token == curr_token)
322 fprintf (file, "]]");
324 switch (token->type)
326 case CPP_SEMICOLON:
327 case CPP_OPEN_BRACE:
328 case CPP_CLOSE_BRACE:
329 case CPP_EOF:
330 fputc ('\n', file);
331 break;
333 default:
334 fputc (' ', file);
338 if (i == num && i < buffer->length ())
340 fprintf (file, " ... ");
341 cp_lexer_print_token (file, &buffer->last ());
344 fprintf (file, "\n");
348 /* Dump all tokens in BUFFER to stderr. */
350 void
351 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
353 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
356 DEBUG_FUNCTION void
357 debug (vec<cp_token, va_gc> &ref)
359 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
362 DEBUG_FUNCTION void
363 debug (vec<cp_token, va_gc> *ptr)
365 if (ptr)
366 debug (*ptr);
367 else
368 fprintf (stderr, "<nil>\n");
372 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
373 description for T. */
375 static void
376 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
378 if (t)
380 fprintf (file, "%s: ", desc);
381 print_node_brief (file, "", t, 0);
386 /* Dump parser context C to FILE. */
388 static void
389 cp_debug_print_context (FILE *file, cp_parser_context *c)
391 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
392 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
393 print_node_brief (file, "", c->object_type, 0);
394 fprintf (file, "}\n");
398 /* Print the stack of parsing contexts to FILE starting with FIRST. */
400 static void
401 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
403 unsigned i;
404 cp_parser_context *c;
406 fprintf (file, "Parsing context stack:\n");
407 for (i = 0, c = first; c; c = c->next, i++)
409 fprintf (file, "\t#%u: ", i);
410 cp_debug_print_context (file, c);
415 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
417 static void
418 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
420 if (flag)
421 fprintf (file, "%s: true\n", desc);
425 /* Print an unparsed function entry UF to FILE. */
427 static void
428 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
430 unsigned i;
431 cp_default_arg_entry *default_arg_fn;
432 tree fn;
434 fprintf (file, "\tFunctions with default args:\n");
435 for (i = 0;
436 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
437 i++)
439 fprintf (file, "\t\tClass type: ");
440 print_node_brief (file, "", default_arg_fn->class_type, 0);
441 fprintf (file, "\t\tDeclaration: ");
442 print_node_brief (file, "", default_arg_fn->decl, 0);
443 fprintf (file, "\n");
446 fprintf (file, "\n\tFunctions with definitions that require "
447 "post-processing\n\t\t");
448 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
450 print_node_brief (file, "", fn, 0);
451 fprintf (file, " ");
453 fprintf (file, "\n");
455 fprintf (file, "\n\tNon-static data members with initializers that require "
456 "post-processing\n\t\t");
457 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
459 print_node_brief (file, "", fn, 0);
460 fprintf (file, " ");
462 fprintf (file, "\n");
466 /* Print the stack of unparsed member functions S to FILE. */
468 static void
469 cp_debug_print_unparsed_queues (FILE *file,
470 vec<cp_unparsed_functions_entry, va_gc> *s)
472 unsigned i;
473 cp_unparsed_functions_entry *uf;
475 fprintf (file, "Unparsed functions\n");
476 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
478 fprintf (file, "#%u:\n", i);
479 cp_debug_print_unparsed_function (file, uf);
484 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
485 the given PARSER. If FILE is NULL, the output is printed on stderr. */
487 static void
488 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
490 cp_token *next_token, *first_token, *start_token;
492 if (file == NULL)
493 file = stderr;
495 next_token = parser->lexer->next_token;
496 first_token = parser->lexer->buffer->address ();
497 start_token = (next_token > first_token + window_size / 2)
498 ? next_token - window_size / 2
499 : first_token;
500 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
501 next_token);
505 /* Dump debugging information for the given PARSER. If FILE is NULL,
506 the output is printed on stderr. */
508 void
509 cp_debug_parser (FILE *file, cp_parser *parser)
511 const size_t window_size = 20;
512 cp_token *token;
513 expanded_location eloc;
515 if (file == NULL)
516 file = stderr;
518 fprintf (file, "Parser state\n\n");
519 fprintf (file, "Number of tokens: %u\n",
520 vec_safe_length (parser->lexer->buffer));
521 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
522 cp_debug_print_tree_if_set (file, "Object scope",
523 parser->object_scope);
524 cp_debug_print_tree_if_set (file, "Qualifying scope",
525 parser->qualifying_scope);
526 cp_debug_print_context_stack (file, parser->context);
527 cp_debug_print_flag (file, "Allow GNU extensions",
528 parser->allow_gnu_extensions_p);
529 cp_debug_print_flag (file, "'>' token is greater-than",
530 parser->greater_than_is_operator_p);
531 cp_debug_print_flag (file, "Default args allowed in current "
532 "parameter list", parser->default_arg_ok_p);
533 cp_debug_print_flag (file, "Parsing integral constant-expression",
534 parser->integral_constant_expression_p);
535 cp_debug_print_flag (file, "Allow non-constant expression in current "
536 "constant-expression",
537 parser->allow_non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Seen non-constant expression",
539 parser->non_integral_constant_expression_p);
540 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
541 "current context",
542 parser->local_variables_forbidden_p);
543 cp_debug_print_flag (file, "In unbraced linkage specification",
544 parser->in_unbraced_linkage_specification_p);
545 cp_debug_print_flag (file, "Parsing a declarator",
546 parser->in_declarator_p);
547 cp_debug_print_flag (file, "In template argument list",
548 parser->in_template_argument_list_p);
549 cp_debug_print_flag (file, "Parsing an iteration statement",
550 parser->in_statement & IN_ITERATION_STMT);
551 cp_debug_print_flag (file, "Parsing a switch statement",
552 parser->in_statement & IN_SWITCH_STMT);
553 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
554 parser->in_statement & IN_OMP_BLOCK);
555 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
556 parser->in_statement & IN_CILK_SIMD_FOR);
557 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
558 parser->in_statement & IN_OMP_FOR);
559 cp_debug_print_flag (file, "Parsing an if statement",
560 parser->in_statement & IN_IF_STMT);
561 cp_debug_print_flag (file, "Parsing a type-id in an expression "
562 "context", parser->in_type_id_in_expr_p);
563 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
564 parser->implicit_extern_c);
565 cp_debug_print_flag (file, "String expressions should be translated "
566 "to execution character set",
567 parser->translate_strings_p);
568 cp_debug_print_flag (file, "Parsing function body outside of a "
569 "local class", parser->in_function_body);
570 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
571 parser->colon_corrects_to_scope_p);
572 cp_debug_print_flag (file, "Colon doesn't start a class definition",
573 parser->colon_doesnt_start_class_def_p);
574 if (parser->type_definition_forbidden_message)
575 fprintf (file, "Error message for forbidden type definitions: %s\n",
576 parser->type_definition_forbidden_message);
577 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
578 fprintf (file, "Number of class definitions in progress: %u\n",
579 parser->num_classes_being_defined);
580 fprintf (file, "Number of template parameter lists for the current "
581 "declaration: %u\n", parser->num_template_parameter_lists);
582 cp_debug_parser_tokens (file, parser, window_size);
583 token = parser->lexer->next_token;
584 fprintf (file, "Next token to parse:\n");
585 fprintf (file, "\tToken: ");
586 cp_lexer_print_token (file, token);
587 eloc = expand_location (token->location);
588 fprintf (file, "\n\tFile: %s\n", eloc.file);
589 fprintf (file, "\tLine: %d\n", eloc.line);
590 fprintf (file, "\tColumn: %d\n", eloc.column);
593 DEBUG_FUNCTION void
594 debug (cp_parser &ref)
596 cp_debug_parser (stderr, &ref);
599 DEBUG_FUNCTION void
600 debug (cp_parser *ptr)
602 if (ptr)
603 debug (*ptr);
604 else
605 fprintf (stderr, "<nil>\n");
608 /* Allocate memory for a new lexer object and return it. */
610 static cp_lexer *
611 cp_lexer_alloc (void)
613 cp_lexer *lexer;
615 c_common_no_more_pch ();
617 /* Allocate the memory. */
618 lexer = ggc_cleared_alloc<cp_lexer> ();
620 /* Initially we are not debugging. */
621 lexer->debugging_p = false;
623 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
625 /* Create the buffer. */
626 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
628 return lexer;
632 /* Create a new main C++ lexer, the lexer that gets tokens from the
633 preprocessor. */
635 static cp_lexer *
636 cp_lexer_new_main (void)
638 cp_lexer *lexer;
639 cp_token token;
641 /* It's possible that parsing the first pragma will load a PCH file,
642 which is a GC collection point. So we have to do that before
643 allocating any memory. */
644 cp_parser_initial_pragma (&token);
646 lexer = cp_lexer_alloc ();
648 /* Put the first token in the buffer. */
649 lexer->buffer->quick_push (token);
651 /* Get the remaining tokens from the preprocessor. */
652 while (token.type != CPP_EOF)
654 cp_lexer_get_preprocessor_token (lexer, &token);
655 vec_safe_push (lexer->buffer, token);
658 lexer->last_token = lexer->buffer->address ()
659 + lexer->buffer->length ()
660 - 1;
661 lexer->next_token = lexer->buffer->length ()
662 ? lexer->buffer->address ()
663 : &eof_token;
665 /* Subsequent preprocessor diagnostics should use compiler
666 diagnostic functions to get the compiler source location. */
667 done_lexing = true;
669 gcc_assert (!lexer->next_token->purged_p);
670 return lexer;
673 /* Create a new lexer whose token stream is primed with the tokens in
674 CACHE. When these tokens are exhausted, no new tokens will be read. */
676 static cp_lexer *
677 cp_lexer_new_from_tokens (cp_token_cache *cache)
679 cp_token *first = cache->first;
680 cp_token *last = cache->last;
681 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
683 /* We do not own the buffer. */
684 lexer->buffer = NULL;
685 lexer->next_token = first == last ? &eof_token : first;
686 lexer->last_token = last;
688 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
690 /* Initially we are not debugging. */
691 lexer->debugging_p = false;
693 gcc_assert (!lexer->next_token->purged_p);
694 return lexer;
697 /* Frees all resources associated with LEXER. */
699 static void
700 cp_lexer_destroy (cp_lexer *lexer)
702 vec_free (lexer->buffer);
703 lexer->saved_tokens.release ();
704 ggc_free (lexer);
707 /* Returns nonzero if debugging information should be output. */
709 static inline bool
710 cp_lexer_debugging_p (cp_lexer *lexer)
712 return lexer->debugging_p;
716 static inline cp_token_position
717 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
719 gcc_assert (!previous_p || lexer->next_token != &eof_token);
721 return lexer->next_token - previous_p;
724 static inline cp_token *
725 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
727 return pos;
730 static inline void
731 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
733 lexer->next_token = cp_lexer_token_at (lexer, pos);
736 static inline cp_token_position
737 cp_lexer_previous_token_position (cp_lexer *lexer)
739 if (lexer->next_token == &eof_token)
740 return lexer->last_token - 1;
741 else
742 return cp_lexer_token_position (lexer, true);
745 static inline cp_token *
746 cp_lexer_previous_token (cp_lexer *lexer)
748 cp_token_position tp = cp_lexer_previous_token_position (lexer);
750 return cp_lexer_token_at (lexer, tp);
753 /* nonzero if we are presently saving tokens. */
755 static inline int
756 cp_lexer_saving_tokens (const cp_lexer* lexer)
758 return lexer->saved_tokens.length () != 0;
761 /* Store the next token from the preprocessor in *TOKEN. Return true
762 if we reach EOF. If LEXER is NULL, assume we are handling an
763 initial #pragma pch_preprocess, and thus want the lexer to return
764 processed strings. */
766 static void
767 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
769 static int is_extern_c = 0;
771 /* Get a new token from the preprocessor. */
772 token->type
773 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
774 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
775 token->keyword = RID_MAX;
776 token->pragma_kind = PRAGMA_NONE;
777 token->purged_p = false;
778 token->error_reported = false;
780 /* On some systems, some header files are surrounded by an
781 implicit extern "C" block. Set a flag in the token if it
782 comes from such a header. */
783 is_extern_c += pending_lang_change;
784 pending_lang_change = 0;
785 token->implicit_extern_c = is_extern_c > 0;
787 /* Check to see if this token is a keyword. */
788 if (token->type == CPP_NAME)
790 if (C_IS_RESERVED_WORD (token->u.value))
792 /* Mark this token as a keyword. */
793 token->type = CPP_KEYWORD;
794 /* Record which keyword. */
795 token->keyword = C_RID_CODE (token->u.value);
797 else
799 if (warn_cxx0x_compat
800 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
801 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
803 /* Warn about the C++0x keyword (but still treat it as
804 an identifier). */
805 warning (OPT_Wc__0x_compat,
806 "identifier %qE is a keyword in C++11",
807 token->u.value);
809 /* Clear out the C_RID_CODE so we don't warn about this
810 particular identifier-turned-keyword again. */
811 C_SET_RID_CODE (token->u.value, RID_MAX);
814 token->keyword = RID_MAX;
817 else if (token->type == CPP_AT_NAME)
819 /* This only happens in Objective-C++; it must be a keyword. */
820 token->type = CPP_KEYWORD;
821 switch (C_RID_CODE (token->u.value))
823 /* Replace 'class' with '@class', 'private' with '@private',
824 etc. This prevents confusion with the C++ keyword
825 'class', and makes the tokens consistent with other
826 Objective-C 'AT' keywords. For example '@class' is
827 reported as RID_AT_CLASS which is consistent with
828 '@synchronized', which is reported as
829 RID_AT_SYNCHRONIZED.
831 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
832 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
833 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
834 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
835 case RID_THROW: token->keyword = RID_AT_THROW; break;
836 case RID_TRY: token->keyword = RID_AT_TRY; break;
837 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
838 default: token->keyword = C_RID_CODE (token->u.value);
841 else if (token->type == CPP_PRAGMA)
843 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
844 token->pragma_kind = ((enum pragma_kind)
845 TREE_INT_CST_LOW (token->u.value));
846 token->u.value = NULL_TREE;
850 /* Update the globals input_location and the input file stack from TOKEN. */
851 static inline void
852 cp_lexer_set_source_position_from_token (cp_token *token)
854 if (token->type != CPP_EOF)
856 input_location = token->location;
860 /* Update the globals input_location and the input file stack from LEXER. */
861 static inline void
862 cp_lexer_set_source_position (cp_lexer *lexer)
864 cp_token *token = cp_lexer_peek_token (lexer);
865 cp_lexer_set_source_position_from_token (token);
868 /* Return a pointer to the next token in the token stream, but do not
869 consume it. */
871 static inline cp_token *
872 cp_lexer_peek_token (cp_lexer *lexer)
874 if (cp_lexer_debugging_p (lexer))
876 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
877 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
878 putc ('\n', cp_lexer_debug_stream);
880 return lexer->next_token;
883 /* Return true if the next token has the indicated TYPE. */
885 static inline bool
886 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
888 return cp_lexer_peek_token (lexer)->type == type;
891 /* Return true if the next token does not have the indicated TYPE. */
893 static inline bool
894 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
896 return !cp_lexer_next_token_is (lexer, type);
899 /* Return true if the next token is the indicated KEYWORD. */
901 static inline bool
902 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
904 return cp_lexer_peek_token (lexer)->keyword == keyword;
907 static inline bool
908 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
910 return cp_lexer_peek_nth_token (lexer, n)->type == type;
913 static inline bool
914 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
916 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
919 /* Return true if the next token is not the indicated KEYWORD. */
921 static inline bool
922 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
924 return cp_lexer_peek_token (lexer)->keyword != keyword;
927 /* Return true if the next token is a keyword for a decl-specifier. */
929 static bool
930 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
932 cp_token *token;
934 token = cp_lexer_peek_token (lexer);
935 switch (token->keyword)
937 /* auto specifier: storage-class-specifier in C++,
938 simple-type-specifier in C++0x. */
939 case RID_AUTO:
940 /* Storage classes. */
941 case RID_REGISTER:
942 case RID_STATIC:
943 case RID_EXTERN:
944 case RID_MUTABLE:
945 case RID_THREAD:
946 /* Elaborated type specifiers. */
947 case RID_ENUM:
948 case RID_CLASS:
949 case RID_STRUCT:
950 case RID_UNION:
951 case RID_TYPENAME:
952 /* Simple type specifiers. */
953 case RID_CHAR:
954 case RID_CHAR16:
955 case RID_CHAR32:
956 case RID_WCHAR:
957 case RID_BOOL:
958 case RID_SHORT:
959 case RID_INT:
960 case RID_LONG:
961 case RID_SIGNED:
962 case RID_UNSIGNED:
963 case RID_FLOAT:
964 case RID_DOUBLE:
965 case RID_VOID:
966 /* GNU extensions. */
967 case RID_ATTRIBUTE:
968 case RID_TYPEOF:
969 /* C++0x extensions. */
970 case RID_DECLTYPE:
971 case RID_UNDERLYING_TYPE:
972 return true;
974 default:
975 if (token->keyword >= RID_FIRST_INT_N
976 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
977 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
978 return true;
979 return false;
983 /* Returns TRUE iff the token T begins a decltype type. */
985 static bool
986 token_is_decltype (cp_token *t)
988 return (t->keyword == RID_DECLTYPE
989 || t->type == CPP_DECLTYPE);
992 /* Returns TRUE iff the next token begins a decltype type. */
994 static bool
995 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
997 cp_token *t = cp_lexer_peek_token (lexer);
998 return token_is_decltype (t);
1001 /* Return a pointer to the Nth token in the token stream. If N is 1,
1002 then this is precisely equivalent to cp_lexer_peek_token (except
1003 that it is not inline). One would like to disallow that case, but
1004 there is one case (cp_parser_nth_token_starts_template_id) where
1005 the caller passes a variable for N and it might be 1. */
1007 static cp_token *
1008 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1010 cp_token *token;
1012 /* N is 1-based, not zero-based. */
1013 gcc_assert (n > 0);
1015 if (cp_lexer_debugging_p (lexer))
1016 fprintf (cp_lexer_debug_stream,
1017 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1019 --n;
1020 token = lexer->next_token;
1021 gcc_assert (!n || token != &eof_token);
1022 while (n != 0)
1024 ++token;
1025 if (token == lexer->last_token)
1027 token = &eof_token;
1028 break;
1031 if (!token->purged_p)
1032 --n;
1035 if (cp_lexer_debugging_p (lexer))
1037 cp_lexer_print_token (cp_lexer_debug_stream, token);
1038 putc ('\n', cp_lexer_debug_stream);
1041 return token;
1044 /* Return the next token, and advance the lexer's next_token pointer
1045 to point to the next non-purged token. */
1047 static cp_token *
1048 cp_lexer_consume_token (cp_lexer* lexer)
1050 cp_token *token = lexer->next_token;
1052 gcc_assert (token != &eof_token);
1053 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1057 lexer->next_token++;
1058 if (lexer->next_token == lexer->last_token)
1060 lexer->next_token = &eof_token;
1061 break;
1065 while (lexer->next_token->purged_p);
1067 cp_lexer_set_source_position_from_token (token);
1069 /* Provide debugging output. */
1070 if (cp_lexer_debugging_p (lexer))
1072 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1073 cp_lexer_print_token (cp_lexer_debug_stream, token);
1074 putc ('\n', cp_lexer_debug_stream);
1077 return token;
1080 /* Permanently remove the next token from the token stream, and
1081 advance the next_token pointer to refer to the next non-purged
1082 token. */
1084 static void
1085 cp_lexer_purge_token (cp_lexer *lexer)
1087 cp_token *tok = lexer->next_token;
1089 gcc_assert (tok != &eof_token);
1090 tok->purged_p = true;
1091 tok->location = UNKNOWN_LOCATION;
1092 tok->u.value = NULL_TREE;
1093 tok->keyword = RID_MAX;
1097 tok++;
1098 if (tok == lexer->last_token)
1100 tok = &eof_token;
1101 break;
1104 while (tok->purged_p);
1105 lexer->next_token = tok;
1108 /* Permanently remove all tokens after TOK, up to, but not
1109 including, the token that will be returned next by
1110 cp_lexer_peek_token. */
1112 static void
1113 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1115 cp_token *peek = lexer->next_token;
1117 if (peek == &eof_token)
1118 peek = lexer->last_token;
1120 gcc_assert (tok < peek);
1122 for ( tok += 1; tok != peek; tok += 1)
1124 tok->purged_p = true;
1125 tok->location = UNKNOWN_LOCATION;
1126 tok->u.value = NULL_TREE;
1127 tok->keyword = RID_MAX;
1131 /* Begin saving tokens. All tokens consumed after this point will be
1132 preserved. */
1134 static void
1135 cp_lexer_save_tokens (cp_lexer* lexer)
1137 /* Provide debugging output. */
1138 if (cp_lexer_debugging_p (lexer))
1139 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1141 lexer->saved_tokens.safe_push (lexer->next_token);
1144 /* Commit to the portion of the token stream most recently saved. */
1146 static void
1147 cp_lexer_commit_tokens (cp_lexer* lexer)
1149 /* Provide debugging output. */
1150 if (cp_lexer_debugging_p (lexer))
1151 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1153 lexer->saved_tokens.pop ();
1156 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1157 to the token stream. Stop saving tokens. */
1159 static void
1160 cp_lexer_rollback_tokens (cp_lexer* lexer)
1162 /* Provide debugging output. */
1163 if (cp_lexer_debugging_p (lexer))
1164 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1166 lexer->next_token = lexer->saved_tokens.pop ();
1169 /* RAII wrapper around the above functions, with sanity checking. Creating
1170 a variable saves tokens, which are committed when the variable is
1171 destroyed unless they are explicitly rolled back by calling the rollback
1172 member function. */
1174 struct saved_token_sentinel
1176 cp_lexer *lexer;
1177 unsigned len;
1178 bool commit;
1179 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1181 len = lexer->saved_tokens.length ();
1182 cp_lexer_save_tokens (lexer);
1184 void rollback ()
1186 cp_lexer_rollback_tokens (lexer);
1187 commit = false;
1189 ~saved_token_sentinel()
1191 if (commit)
1192 cp_lexer_commit_tokens (lexer);
1193 gcc_assert (lexer->saved_tokens.length () == len);
1197 /* Print a representation of the TOKEN on the STREAM. */
1199 static void
1200 cp_lexer_print_token (FILE * stream, cp_token *token)
1202 /* We don't use cpp_type2name here because the parser defines
1203 a few tokens of its own. */
1204 static const char *const token_names[] = {
1205 /* cpplib-defined token types */
1206 #define OP(e, s) #e,
1207 #define TK(e, s) #e,
1208 TTYPE_TABLE
1209 #undef OP
1210 #undef TK
1211 /* C++ parser token types - see "Manifest constants", above. */
1212 "KEYWORD",
1213 "TEMPLATE_ID",
1214 "NESTED_NAME_SPECIFIER",
1217 /* For some tokens, print the associated data. */
1218 switch (token->type)
1220 case CPP_KEYWORD:
1221 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1222 For example, `struct' is mapped to an INTEGER_CST. */
1223 if (!identifier_p (token->u.value))
1224 break;
1225 /* else fall through */
1226 case CPP_NAME:
1227 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1228 break;
1230 case CPP_STRING:
1231 case CPP_STRING16:
1232 case CPP_STRING32:
1233 case CPP_WSTRING:
1234 case CPP_UTF8STRING:
1235 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1236 break;
1238 case CPP_NUMBER:
1239 print_generic_expr (stream, token->u.value, 0);
1240 break;
1242 default:
1243 /* If we have a name for the token, print it out. Otherwise, we
1244 simply give the numeric code. */
1245 if (token->type < ARRAY_SIZE(token_names))
1246 fputs (token_names[token->type], stream);
1247 else
1248 fprintf (stream, "[%d]", token->type);
1249 break;
1253 DEBUG_FUNCTION void
1254 debug (cp_token &ref)
1256 cp_lexer_print_token (stderr, &ref);
1257 fprintf (stderr, "\n");
1260 DEBUG_FUNCTION void
1261 debug (cp_token *ptr)
1263 if (ptr)
1264 debug (*ptr);
1265 else
1266 fprintf (stderr, "<nil>\n");
1270 /* Start emitting debugging information. */
1272 static void
1273 cp_lexer_start_debugging (cp_lexer* lexer)
1275 lexer->debugging_p = true;
1276 cp_lexer_debug_stream = stderr;
1279 /* Stop emitting debugging information. */
1281 static void
1282 cp_lexer_stop_debugging (cp_lexer* lexer)
1284 lexer->debugging_p = false;
1285 cp_lexer_debug_stream = NULL;
1288 /* Create a new cp_token_cache, representing a range of tokens. */
1290 static cp_token_cache *
1291 cp_token_cache_new (cp_token *first, cp_token *last)
1293 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1294 cache->first = first;
1295 cache->last = last;
1296 return cache;
1299 /* Diagnose if #pragma omp declare simd isn't followed immediately
1300 by function declaration or definition. */
1302 static inline void
1303 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1305 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1307 error ("%<#pragma omp declare simd%> not immediately followed by "
1308 "function declaration or definition");
1309 parser->omp_declare_simd = NULL;
1313 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1314 and put that into "omp declare simd" attribute. */
1316 static inline void
1317 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1319 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1321 if (fndecl == error_mark_node)
1323 parser->omp_declare_simd = NULL;
1324 return;
1326 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1328 cp_ensure_no_omp_declare_simd (parser);
1329 return;
1334 /* Decl-specifiers. */
1336 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1338 static void
1339 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1341 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1344 /* Declarators. */
1346 /* Nothing other than the parser should be creating declarators;
1347 declarators are a semi-syntactic representation of C++ entities.
1348 Other parts of the front end that need to create entities (like
1349 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1351 static cp_declarator *make_call_declarator
1352 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1353 static cp_declarator *make_array_declarator
1354 (cp_declarator *, tree);
1355 static cp_declarator *make_pointer_declarator
1356 (cp_cv_quals, cp_declarator *, tree);
1357 static cp_declarator *make_reference_declarator
1358 (cp_cv_quals, cp_declarator *, bool, tree);
1359 static cp_parameter_declarator *make_parameter_declarator
1360 (cp_decl_specifier_seq *, cp_declarator *, tree);
1361 static cp_declarator *make_ptrmem_declarator
1362 (cp_cv_quals, tree, cp_declarator *, tree);
1364 /* An erroneous declarator. */
1365 static cp_declarator *cp_error_declarator;
1367 /* The obstack on which declarators and related data structures are
1368 allocated. */
1369 static struct obstack declarator_obstack;
1371 /* Alloc BYTES from the declarator memory pool. */
1373 static inline void *
1374 alloc_declarator (size_t bytes)
1376 return obstack_alloc (&declarator_obstack, bytes);
1379 /* Allocate a declarator of the indicated KIND. Clear fields that are
1380 common to all declarators. */
1382 static cp_declarator *
1383 make_declarator (cp_declarator_kind kind)
1385 cp_declarator *declarator;
1387 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1388 declarator->kind = kind;
1389 declarator->attributes = NULL_TREE;
1390 declarator->std_attributes = NULL_TREE;
1391 declarator->declarator = NULL;
1392 declarator->parameter_pack_p = false;
1393 declarator->id_loc = UNKNOWN_LOCATION;
1395 return declarator;
1398 /* Make a declarator for a generalized identifier. If
1399 QUALIFYING_SCOPE is non-NULL, the identifier is
1400 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1401 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1402 is, if any. */
1404 static cp_declarator *
1405 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1406 special_function_kind sfk)
1408 cp_declarator *declarator;
1410 /* It is valid to write:
1412 class C { void f(); };
1413 typedef C D;
1414 void D::f();
1416 The standard is not clear about whether `typedef const C D' is
1417 legal; as of 2002-09-15 the committee is considering that
1418 question. EDG 3.0 allows that syntax. Therefore, we do as
1419 well. */
1420 if (qualifying_scope && TYPE_P (qualifying_scope))
1421 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1423 gcc_assert (identifier_p (unqualified_name)
1424 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1425 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1427 declarator = make_declarator (cdk_id);
1428 declarator->u.id.qualifying_scope = qualifying_scope;
1429 declarator->u.id.unqualified_name = unqualified_name;
1430 declarator->u.id.sfk = sfk;
1432 return declarator;
1435 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1436 of modifiers such as const or volatile to apply to the pointer
1437 type, represented as identifiers. ATTRIBUTES represent the attributes that
1438 appertain to the pointer or reference. */
1440 cp_declarator *
1441 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1442 tree attributes)
1444 cp_declarator *declarator;
1446 declarator = make_declarator (cdk_pointer);
1447 declarator->declarator = target;
1448 declarator->u.pointer.qualifiers = cv_qualifiers;
1449 declarator->u.pointer.class_type = NULL_TREE;
1450 if (target)
1452 declarator->id_loc = target->id_loc;
1453 declarator->parameter_pack_p = target->parameter_pack_p;
1454 target->parameter_pack_p = false;
1456 else
1457 declarator->parameter_pack_p = false;
1459 declarator->std_attributes = attributes;
1461 return declarator;
1464 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1465 represent the attributes that appertain to the pointer or
1466 reference. */
1468 cp_declarator *
1469 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1470 bool rvalue_ref, tree attributes)
1472 cp_declarator *declarator;
1474 declarator = make_declarator (cdk_reference);
1475 declarator->declarator = target;
1476 declarator->u.reference.qualifiers = cv_qualifiers;
1477 declarator->u.reference.rvalue_ref = rvalue_ref;
1478 if (target)
1480 declarator->id_loc = target->id_loc;
1481 declarator->parameter_pack_p = target->parameter_pack_p;
1482 target->parameter_pack_p = false;
1484 else
1485 declarator->parameter_pack_p = false;
1487 declarator->std_attributes = attributes;
1489 return declarator;
1492 /* Like make_pointer_declarator -- but for a pointer to a non-static
1493 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1494 appertain to the pointer or reference. */
1496 cp_declarator *
1497 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1498 cp_declarator *pointee,
1499 tree attributes)
1501 cp_declarator *declarator;
1503 declarator = make_declarator (cdk_ptrmem);
1504 declarator->declarator = pointee;
1505 declarator->u.pointer.qualifiers = cv_qualifiers;
1506 declarator->u.pointer.class_type = class_type;
1508 if (pointee)
1510 declarator->parameter_pack_p = pointee->parameter_pack_p;
1511 pointee->parameter_pack_p = false;
1513 else
1514 declarator->parameter_pack_p = false;
1516 declarator->std_attributes = attributes;
1518 return declarator;
1521 /* Make a declarator for the function given by TARGET, with the
1522 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1523 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1524 indicates what exceptions can be thrown. */
1526 cp_declarator *
1527 make_call_declarator (cp_declarator *target,
1528 tree parms,
1529 cp_cv_quals cv_qualifiers,
1530 cp_virt_specifiers virt_specifiers,
1531 cp_ref_qualifier ref_qualifier,
1532 tree exception_specification,
1533 tree late_return_type)
1535 cp_declarator *declarator;
1537 declarator = make_declarator (cdk_function);
1538 declarator->declarator = target;
1539 declarator->u.function.parameters = parms;
1540 declarator->u.function.qualifiers = cv_qualifiers;
1541 declarator->u.function.virt_specifiers = virt_specifiers;
1542 declarator->u.function.ref_qualifier = ref_qualifier;
1543 declarator->u.function.exception_specification = exception_specification;
1544 declarator->u.function.late_return_type = late_return_type;
1545 if (target)
1547 declarator->id_loc = target->id_loc;
1548 declarator->parameter_pack_p = target->parameter_pack_p;
1549 target->parameter_pack_p = false;
1551 else
1552 declarator->parameter_pack_p = false;
1554 return declarator;
1557 /* Make a declarator for an array of BOUNDS elements, each of which is
1558 defined by ELEMENT. */
1560 cp_declarator *
1561 make_array_declarator (cp_declarator *element, tree bounds)
1563 cp_declarator *declarator;
1565 declarator = make_declarator (cdk_array);
1566 declarator->declarator = element;
1567 declarator->u.array.bounds = bounds;
1568 if (element)
1570 declarator->id_loc = element->id_loc;
1571 declarator->parameter_pack_p = element->parameter_pack_p;
1572 element->parameter_pack_p = false;
1574 else
1575 declarator->parameter_pack_p = false;
1577 return declarator;
1580 /* Determine whether the declarator we've seen so far can be a
1581 parameter pack, when followed by an ellipsis. */
1582 static bool
1583 declarator_can_be_parameter_pack (cp_declarator *declarator)
1585 /* Search for a declarator name, or any other declarator that goes
1586 after the point where the ellipsis could appear in a parameter
1587 pack. If we find any of these, then this declarator can not be
1588 made into a parameter pack. */
1589 bool found = false;
1590 while (declarator && !found)
1592 switch ((int)declarator->kind)
1594 case cdk_id:
1595 case cdk_array:
1596 found = true;
1597 break;
1599 case cdk_error:
1600 return true;
1602 default:
1603 declarator = declarator->declarator;
1604 break;
1608 return !found;
1611 cp_parameter_declarator *no_parameters;
1613 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1614 DECLARATOR and DEFAULT_ARGUMENT. */
1616 cp_parameter_declarator *
1617 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1618 cp_declarator *declarator,
1619 tree default_argument)
1621 cp_parameter_declarator *parameter;
1623 parameter = ((cp_parameter_declarator *)
1624 alloc_declarator (sizeof (cp_parameter_declarator)));
1625 parameter->next = NULL;
1626 if (decl_specifiers)
1627 parameter->decl_specifiers = *decl_specifiers;
1628 else
1629 clear_decl_specs (&parameter->decl_specifiers);
1630 parameter->declarator = declarator;
1631 parameter->default_argument = default_argument;
1632 parameter->ellipsis_p = false;
1634 return parameter;
1637 /* Returns true iff DECLARATOR is a declaration for a function. */
1639 static bool
1640 function_declarator_p (const cp_declarator *declarator)
1642 while (declarator)
1644 if (declarator->kind == cdk_function
1645 && declarator->declarator->kind == cdk_id)
1646 return true;
1647 if (declarator->kind == cdk_id
1648 || declarator->kind == cdk_error)
1649 return false;
1650 declarator = declarator->declarator;
1652 return false;
1655 /* The parser. */
1657 /* Overview
1658 --------
1660 A cp_parser parses the token stream as specified by the C++
1661 grammar. Its job is purely parsing, not semantic analysis. For
1662 example, the parser breaks the token stream into declarators,
1663 expressions, statements, and other similar syntactic constructs.
1664 It does not check that the types of the expressions on either side
1665 of an assignment-statement are compatible, or that a function is
1666 not declared with a parameter of type `void'.
1668 The parser invokes routines elsewhere in the compiler to perform
1669 semantic analysis and to build up the abstract syntax tree for the
1670 code processed.
1672 The parser (and the template instantiation code, which is, in a
1673 way, a close relative of parsing) are the only parts of the
1674 compiler that should be calling push_scope and pop_scope, or
1675 related functions. The parser (and template instantiation code)
1676 keeps track of what scope is presently active; everything else
1677 should simply honor that. (The code that generates static
1678 initializers may also need to set the scope, in order to check
1679 access control correctly when emitting the initializers.)
1681 Methodology
1682 -----------
1684 The parser is of the standard recursive-descent variety. Upcoming
1685 tokens in the token stream are examined in order to determine which
1686 production to use when parsing a non-terminal. Some C++ constructs
1687 require arbitrary look ahead to disambiguate. For example, it is
1688 impossible, in the general case, to tell whether a statement is an
1689 expression or declaration without scanning the entire statement.
1690 Therefore, the parser is capable of "parsing tentatively." When the
1691 parser is not sure what construct comes next, it enters this mode.
1692 Then, while we attempt to parse the construct, the parser queues up
1693 error messages, rather than issuing them immediately, and saves the
1694 tokens it consumes. If the construct is parsed successfully, the
1695 parser "commits", i.e., it issues any queued error messages and
1696 the tokens that were being preserved are permanently discarded.
1697 If, however, the construct is not parsed successfully, the parser
1698 rolls back its state completely so that it can resume parsing using
1699 a different alternative.
1701 Future Improvements
1702 -------------------
1704 The performance of the parser could probably be improved substantially.
1705 We could often eliminate the need to parse tentatively by looking ahead
1706 a little bit. In some places, this approach might not entirely eliminate
1707 the need to parse tentatively, but it might still speed up the average
1708 case. */
1710 /* Flags that are passed to some parsing functions. These values can
1711 be bitwise-ored together. */
1713 enum
1715 /* No flags. */
1716 CP_PARSER_FLAGS_NONE = 0x0,
1717 /* The construct is optional. If it is not present, then no error
1718 should be issued. */
1719 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1720 /* When parsing a type-specifier, treat user-defined type-names
1721 as non-type identifiers. */
1722 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1723 /* When parsing a type-specifier, do not try to parse a class-specifier
1724 or enum-specifier. */
1725 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1726 /* When parsing a decl-specifier-seq, only allow type-specifier or
1727 constexpr. */
1728 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1731 /* This type is used for parameters and variables which hold
1732 combinations of the above flags. */
1733 typedef int cp_parser_flags;
1735 /* The different kinds of declarators we want to parse. */
1737 typedef enum cp_parser_declarator_kind
1739 /* We want an abstract declarator. */
1740 CP_PARSER_DECLARATOR_ABSTRACT,
1741 /* We want a named declarator. */
1742 CP_PARSER_DECLARATOR_NAMED,
1743 /* We don't mind, but the name must be an unqualified-id. */
1744 CP_PARSER_DECLARATOR_EITHER
1745 } cp_parser_declarator_kind;
1747 /* The precedence values used to parse binary expressions. The minimum value
1748 of PREC must be 1, because zero is reserved to quickly discriminate
1749 binary operators from other tokens. */
1751 enum cp_parser_prec
1753 PREC_NOT_OPERATOR,
1754 PREC_LOGICAL_OR_EXPRESSION,
1755 PREC_LOGICAL_AND_EXPRESSION,
1756 PREC_INCLUSIVE_OR_EXPRESSION,
1757 PREC_EXCLUSIVE_OR_EXPRESSION,
1758 PREC_AND_EXPRESSION,
1759 PREC_EQUALITY_EXPRESSION,
1760 PREC_RELATIONAL_EXPRESSION,
1761 PREC_SHIFT_EXPRESSION,
1762 PREC_ADDITIVE_EXPRESSION,
1763 PREC_MULTIPLICATIVE_EXPRESSION,
1764 PREC_PM_EXPRESSION,
1765 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1768 /* A mapping from a token type to a corresponding tree node type, with a
1769 precedence value. */
1771 typedef struct cp_parser_binary_operations_map_node
1773 /* The token type. */
1774 enum cpp_ttype token_type;
1775 /* The corresponding tree code. */
1776 enum tree_code tree_type;
1777 /* The precedence of this operator. */
1778 enum cp_parser_prec prec;
1779 } cp_parser_binary_operations_map_node;
1781 typedef struct cp_parser_expression_stack_entry
1783 /* Left hand side of the binary operation we are currently
1784 parsing. */
1785 tree lhs;
1786 /* Original tree code for left hand side, if it was a binary
1787 expression itself (used for -Wparentheses). */
1788 enum tree_code lhs_type;
1789 /* Tree code for the binary operation we are parsing. */
1790 enum tree_code tree_type;
1791 /* Precedence of the binary operation we are parsing. */
1792 enum cp_parser_prec prec;
1793 /* Location of the binary operation we are parsing. */
1794 location_t loc;
1795 } cp_parser_expression_stack_entry;
1797 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1798 entries because precedence levels on the stack are monotonically
1799 increasing. */
1800 typedef struct cp_parser_expression_stack_entry
1801 cp_parser_expression_stack[NUM_PREC_VALUES];
1803 /* Prototypes. */
1805 /* Constructors and destructors. */
1807 static cp_parser_context *cp_parser_context_new
1808 (cp_parser_context *);
1810 /* Class variables. */
1812 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1814 /* The operator-precedence table used by cp_parser_binary_expression.
1815 Transformed into an associative array (binops_by_token) by
1816 cp_parser_new. */
1818 static const cp_parser_binary_operations_map_node binops[] = {
1819 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1820 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1822 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1823 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1824 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1826 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1827 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1829 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1830 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1832 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1833 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1834 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1835 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1837 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1838 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1840 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1842 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1844 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1846 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1848 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1851 /* The same as binops, but initialized by cp_parser_new so that
1852 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1853 for speed. */
1854 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1856 /* Constructors and destructors. */
1858 /* Construct a new context. The context below this one on the stack
1859 is given by NEXT. */
1861 static cp_parser_context *
1862 cp_parser_context_new (cp_parser_context* next)
1864 cp_parser_context *context;
1866 /* Allocate the storage. */
1867 if (cp_parser_context_free_list != NULL)
1869 /* Pull the first entry from the free list. */
1870 context = cp_parser_context_free_list;
1871 cp_parser_context_free_list = context->next;
1872 memset (context, 0, sizeof (*context));
1874 else
1875 context = ggc_cleared_alloc<cp_parser_context> ();
1877 /* No errors have occurred yet in this context. */
1878 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1879 /* If this is not the bottommost context, copy information that we
1880 need from the previous context. */
1881 if (next)
1883 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1884 expression, then we are parsing one in this context, too. */
1885 context->object_type = next->object_type;
1886 /* Thread the stack. */
1887 context->next = next;
1890 return context;
1893 /* Managing the unparsed function queues. */
1895 #define unparsed_funs_with_default_args \
1896 parser->unparsed_queues->last ().funs_with_default_args
1897 #define unparsed_funs_with_definitions \
1898 parser->unparsed_queues->last ().funs_with_definitions
1899 #define unparsed_nsdmis \
1900 parser->unparsed_queues->last ().nsdmis
1901 #define unparsed_classes \
1902 parser->unparsed_queues->last ().classes
1904 static void
1905 push_unparsed_function_queues (cp_parser *parser)
1907 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1908 vec_safe_push (parser->unparsed_queues, e);
1911 static void
1912 pop_unparsed_function_queues (cp_parser *parser)
1914 release_tree_vector (unparsed_funs_with_definitions);
1915 parser->unparsed_queues->pop ();
1918 /* Prototypes. */
1920 /* Constructors and destructors. */
1922 static cp_parser *cp_parser_new
1923 (void);
1925 /* Routines to parse various constructs.
1927 Those that return `tree' will return the error_mark_node (rather
1928 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1929 Sometimes, they will return an ordinary node if error-recovery was
1930 attempted, even though a parse error occurred. So, to check
1931 whether or not a parse error occurred, you should always use
1932 cp_parser_error_occurred. If the construct is optional (indicated
1933 either by an `_opt' in the name of the function that does the
1934 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1935 the construct is not present. */
1937 /* Lexical conventions [gram.lex] */
1939 static tree cp_parser_identifier
1940 (cp_parser *);
1941 static tree cp_parser_string_literal
1942 (cp_parser *, bool, bool, bool);
1943 static tree cp_parser_userdef_char_literal
1944 (cp_parser *);
1945 static tree cp_parser_userdef_string_literal
1946 (tree);
1947 static tree cp_parser_userdef_numeric_literal
1948 (cp_parser *);
1950 /* Basic concepts [gram.basic] */
1952 static bool cp_parser_translation_unit
1953 (cp_parser *);
1955 /* Expressions [gram.expr] */
1957 static tree cp_parser_primary_expression
1958 (cp_parser *, bool, bool, bool, cp_id_kind *);
1959 static tree cp_parser_id_expression
1960 (cp_parser *, bool, bool, bool *, bool, bool);
1961 static tree cp_parser_unqualified_id
1962 (cp_parser *, bool, bool, bool, bool);
1963 static tree cp_parser_nested_name_specifier_opt
1964 (cp_parser *, bool, bool, bool, bool);
1965 static tree cp_parser_nested_name_specifier
1966 (cp_parser *, bool, bool, bool, bool);
1967 static tree cp_parser_qualifying_entity
1968 (cp_parser *, bool, bool, bool, bool, bool);
1969 static tree cp_parser_postfix_expression
1970 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1971 static tree cp_parser_postfix_open_square_expression
1972 (cp_parser *, tree, bool, bool);
1973 static tree cp_parser_postfix_dot_deref_expression
1974 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1975 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1976 (cp_parser *, int, bool, bool, bool *, bool = false);
1977 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1978 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1979 static void cp_parser_pseudo_destructor_name
1980 (cp_parser *, tree, tree *, tree *);
1981 static tree cp_parser_unary_expression
1982 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1983 static enum tree_code cp_parser_unary_operator
1984 (cp_token *);
1985 static tree cp_parser_new_expression
1986 (cp_parser *);
1987 static vec<tree, va_gc> *cp_parser_new_placement
1988 (cp_parser *);
1989 static tree cp_parser_new_type_id
1990 (cp_parser *, tree *);
1991 static cp_declarator *cp_parser_new_declarator_opt
1992 (cp_parser *);
1993 static cp_declarator *cp_parser_direct_new_declarator
1994 (cp_parser *);
1995 static vec<tree, va_gc> *cp_parser_new_initializer
1996 (cp_parser *);
1997 static tree cp_parser_delete_expression
1998 (cp_parser *);
1999 static tree cp_parser_cast_expression
2000 (cp_parser *, bool, bool, bool, cp_id_kind *);
2001 static tree cp_parser_binary_expression
2002 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2003 static tree cp_parser_question_colon_clause
2004 (cp_parser *, tree);
2005 static tree cp_parser_assignment_expression
2006 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2007 static enum tree_code cp_parser_assignment_operator_opt
2008 (cp_parser *);
2009 static tree cp_parser_expression
2010 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2011 static tree cp_parser_constant_expression
2012 (cp_parser *, bool = false, bool * = NULL);
2013 static tree cp_parser_builtin_offsetof
2014 (cp_parser *);
2015 static tree cp_parser_lambda_expression
2016 (cp_parser *);
2017 static void cp_parser_lambda_introducer
2018 (cp_parser *, tree);
2019 static bool cp_parser_lambda_declarator_opt
2020 (cp_parser *, tree);
2021 static void cp_parser_lambda_body
2022 (cp_parser *, tree);
2024 /* Statements [gram.stmt.stmt] */
2026 static void cp_parser_statement
2027 (cp_parser *, tree, bool, bool *);
2028 static void cp_parser_label_for_labeled_statement
2029 (cp_parser *, tree);
2030 static tree cp_parser_expression_statement
2031 (cp_parser *, tree);
2032 static tree cp_parser_compound_statement
2033 (cp_parser *, tree, bool, bool);
2034 static void cp_parser_statement_seq_opt
2035 (cp_parser *, tree);
2036 static tree cp_parser_selection_statement
2037 (cp_parser *, bool *);
2038 static tree cp_parser_condition
2039 (cp_parser *);
2040 static tree cp_parser_iteration_statement
2041 (cp_parser *, bool);
2042 static bool cp_parser_for_init_statement
2043 (cp_parser *, tree *decl);
2044 static tree cp_parser_for
2045 (cp_parser *, bool);
2046 static tree cp_parser_c_for
2047 (cp_parser *, tree, tree, bool);
2048 static tree cp_parser_range_for
2049 (cp_parser *, tree, tree, tree, bool);
2050 static void do_range_for_auto_deduction
2051 (tree, tree);
2052 static tree cp_parser_perform_range_for_lookup
2053 (tree, tree *, tree *);
2054 static tree cp_parser_range_for_member_function
2055 (tree, tree);
2056 static tree cp_parser_jump_statement
2057 (cp_parser *);
2058 static void cp_parser_declaration_statement
2059 (cp_parser *);
2061 static tree cp_parser_implicitly_scoped_statement
2062 (cp_parser *, bool *);
2063 static void cp_parser_already_scoped_statement
2064 (cp_parser *);
2066 /* Declarations [gram.dcl.dcl] */
2068 static void cp_parser_declaration_seq_opt
2069 (cp_parser *);
2070 static void cp_parser_declaration
2071 (cp_parser *);
2072 static void cp_parser_block_declaration
2073 (cp_parser *, bool);
2074 static void cp_parser_simple_declaration
2075 (cp_parser *, bool, tree *);
2076 static void cp_parser_decl_specifier_seq
2077 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2078 static tree cp_parser_storage_class_specifier_opt
2079 (cp_parser *);
2080 static tree cp_parser_function_specifier_opt
2081 (cp_parser *, cp_decl_specifier_seq *);
2082 static tree cp_parser_type_specifier
2083 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2084 int *, bool *);
2085 static tree cp_parser_simple_type_specifier
2086 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2087 static tree cp_parser_type_name
2088 (cp_parser *);
2089 static tree cp_parser_nonclass_name
2090 (cp_parser* parser);
2091 static tree cp_parser_elaborated_type_specifier
2092 (cp_parser *, bool, bool);
2093 static tree cp_parser_enum_specifier
2094 (cp_parser *);
2095 static void cp_parser_enumerator_list
2096 (cp_parser *, tree);
2097 static void cp_parser_enumerator_definition
2098 (cp_parser *, tree);
2099 static tree cp_parser_namespace_name
2100 (cp_parser *);
2101 static void cp_parser_namespace_definition
2102 (cp_parser *);
2103 static void cp_parser_namespace_body
2104 (cp_parser *);
2105 static tree cp_parser_qualified_namespace_specifier
2106 (cp_parser *);
2107 static void cp_parser_namespace_alias_definition
2108 (cp_parser *);
2109 static bool cp_parser_using_declaration
2110 (cp_parser *, bool);
2111 static void cp_parser_using_directive
2112 (cp_parser *);
2113 static tree cp_parser_alias_declaration
2114 (cp_parser *);
2115 static void cp_parser_asm_definition
2116 (cp_parser *);
2117 static void cp_parser_linkage_specification
2118 (cp_parser *);
2119 static void cp_parser_static_assert
2120 (cp_parser *, bool);
2121 static tree cp_parser_decltype
2122 (cp_parser *);
2124 /* Declarators [gram.dcl.decl] */
2126 static tree cp_parser_init_declarator
2127 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2128 static cp_declarator *cp_parser_declarator
2129 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2130 static cp_declarator *cp_parser_direct_declarator
2131 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2132 static enum tree_code cp_parser_ptr_operator
2133 (cp_parser *, tree *, cp_cv_quals *, tree *);
2134 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2135 (cp_parser *);
2136 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2137 (cp_parser *);
2138 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2139 (cp_parser *);
2140 static tree cp_parser_late_return_type_opt
2141 (cp_parser *, cp_declarator *, cp_cv_quals);
2142 static tree cp_parser_declarator_id
2143 (cp_parser *, bool);
2144 static tree cp_parser_type_id
2145 (cp_parser *);
2146 static tree cp_parser_template_type_arg
2147 (cp_parser *);
2148 static tree cp_parser_trailing_type_id (cp_parser *);
2149 static tree cp_parser_type_id_1
2150 (cp_parser *, bool, bool);
2151 static void cp_parser_type_specifier_seq
2152 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2153 static tree cp_parser_parameter_declaration_clause
2154 (cp_parser *);
2155 static tree cp_parser_parameter_declaration_list
2156 (cp_parser *, bool *);
2157 static cp_parameter_declarator *cp_parser_parameter_declaration
2158 (cp_parser *, bool, bool *);
2159 static tree cp_parser_default_argument
2160 (cp_parser *, bool);
2161 static void cp_parser_function_body
2162 (cp_parser *, bool);
2163 static tree cp_parser_initializer
2164 (cp_parser *, bool *, bool *);
2165 static tree cp_parser_initializer_clause
2166 (cp_parser *, bool *);
2167 static tree cp_parser_braced_list
2168 (cp_parser*, bool*);
2169 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2170 (cp_parser *, bool *);
2172 static bool cp_parser_ctor_initializer_opt_and_function_body
2173 (cp_parser *, bool);
2175 static tree cp_parser_late_parsing_omp_declare_simd
2176 (cp_parser *, tree);
2178 static tree cp_parser_late_parsing_cilk_simd_fn_info
2179 (cp_parser *, tree);
2181 static tree synthesize_implicit_template_parm
2182 (cp_parser *);
2183 static tree finish_fully_implicit_template
2184 (cp_parser *, tree);
2186 /* Classes [gram.class] */
2188 static tree cp_parser_class_name
2189 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2190 static tree cp_parser_class_specifier
2191 (cp_parser *);
2192 static tree cp_parser_class_head
2193 (cp_parser *, bool *);
2194 static enum tag_types cp_parser_class_key
2195 (cp_parser *);
2196 static void cp_parser_type_parameter_key
2197 (cp_parser* parser);
2198 static void cp_parser_member_specification_opt
2199 (cp_parser *);
2200 static void cp_parser_member_declaration
2201 (cp_parser *);
2202 static tree cp_parser_pure_specifier
2203 (cp_parser *);
2204 static tree cp_parser_constant_initializer
2205 (cp_parser *);
2207 /* Derived classes [gram.class.derived] */
2209 static tree cp_parser_base_clause
2210 (cp_parser *);
2211 static tree cp_parser_base_specifier
2212 (cp_parser *);
2214 /* Special member functions [gram.special] */
2216 static tree cp_parser_conversion_function_id
2217 (cp_parser *);
2218 static tree cp_parser_conversion_type_id
2219 (cp_parser *);
2220 static cp_declarator *cp_parser_conversion_declarator_opt
2221 (cp_parser *);
2222 static bool cp_parser_ctor_initializer_opt
2223 (cp_parser *);
2224 static void cp_parser_mem_initializer_list
2225 (cp_parser *);
2226 static tree cp_parser_mem_initializer
2227 (cp_parser *);
2228 static tree cp_parser_mem_initializer_id
2229 (cp_parser *);
2231 /* Overloading [gram.over] */
2233 static tree cp_parser_operator_function_id
2234 (cp_parser *);
2235 static tree cp_parser_operator
2236 (cp_parser *);
2238 /* Templates [gram.temp] */
2240 static void cp_parser_template_declaration
2241 (cp_parser *, bool);
2242 static tree cp_parser_template_parameter_list
2243 (cp_parser *);
2244 static tree cp_parser_template_parameter
2245 (cp_parser *, bool *, bool *);
2246 static tree cp_parser_type_parameter
2247 (cp_parser *, bool *);
2248 static tree cp_parser_template_id
2249 (cp_parser *, bool, bool, enum tag_types, bool);
2250 static tree cp_parser_template_name
2251 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2252 static tree cp_parser_template_argument_list
2253 (cp_parser *);
2254 static tree cp_parser_template_argument
2255 (cp_parser *);
2256 static void cp_parser_explicit_instantiation
2257 (cp_parser *);
2258 static void cp_parser_explicit_specialization
2259 (cp_parser *);
2261 /* Exception handling [gram.exception] */
2263 static tree cp_parser_try_block
2264 (cp_parser *);
2265 static bool cp_parser_function_try_block
2266 (cp_parser *);
2267 static void cp_parser_handler_seq
2268 (cp_parser *);
2269 static void cp_parser_handler
2270 (cp_parser *);
2271 static tree cp_parser_exception_declaration
2272 (cp_parser *);
2273 static tree cp_parser_throw_expression
2274 (cp_parser *);
2275 static tree cp_parser_exception_specification_opt
2276 (cp_parser *);
2277 static tree cp_parser_type_id_list
2278 (cp_parser *);
2280 /* GNU Extensions */
2282 static tree cp_parser_asm_specification_opt
2283 (cp_parser *);
2284 static tree cp_parser_asm_operand_list
2285 (cp_parser *);
2286 static tree cp_parser_asm_clobber_list
2287 (cp_parser *);
2288 static tree cp_parser_asm_label_list
2289 (cp_parser *);
2290 static bool cp_next_tokens_can_be_attribute_p
2291 (cp_parser *);
2292 static bool cp_next_tokens_can_be_gnu_attribute_p
2293 (cp_parser *);
2294 static bool cp_next_tokens_can_be_std_attribute_p
2295 (cp_parser *);
2296 static bool cp_nth_tokens_can_be_std_attribute_p
2297 (cp_parser *, size_t);
2298 static bool cp_nth_tokens_can_be_gnu_attribute_p
2299 (cp_parser *, size_t);
2300 static bool cp_nth_tokens_can_be_attribute_p
2301 (cp_parser *, size_t);
2302 static tree cp_parser_attributes_opt
2303 (cp_parser *);
2304 static tree cp_parser_gnu_attributes_opt
2305 (cp_parser *);
2306 static tree cp_parser_gnu_attribute_list
2307 (cp_parser *);
2308 static tree cp_parser_std_attribute
2309 (cp_parser *);
2310 static tree cp_parser_std_attribute_spec
2311 (cp_parser *);
2312 static tree cp_parser_std_attribute_spec_seq
2313 (cp_parser *);
2314 static bool cp_parser_extension_opt
2315 (cp_parser *, int *);
2316 static void cp_parser_label_declaration
2317 (cp_parser *);
2319 /* Transactional Memory Extensions */
2321 static tree cp_parser_transaction
2322 (cp_parser *, enum rid);
2323 static tree cp_parser_transaction_expression
2324 (cp_parser *, enum rid);
2325 static bool cp_parser_function_transaction
2326 (cp_parser *, enum rid);
2327 static tree cp_parser_transaction_cancel
2328 (cp_parser *);
2330 enum pragma_context {
2331 pragma_external,
2332 pragma_member,
2333 pragma_objc_icode,
2334 pragma_stmt,
2335 pragma_compound
2337 static bool cp_parser_pragma
2338 (cp_parser *, enum pragma_context);
2340 /* Objective-C++ Productions */
2342 static tree cp_parser_objc_message_receiver
2343 (cp_parser *);
2344 static tree cp_parser_objc_message_args
2345 (cp_parser *);
2346 static tree cp_parser_objc_message_expression
2347 (cp_parser *);
2348 static tree cp_parser_objc_encode_expression
2349 (cp_parser *);
2350 static tree cp_parser_objc_defs_expression
2351 (cp_parser *);
2352 static tree cp_parser_objc_protocol_expression
2353 (cp_parser *);
2354 static tree cp_parser_objc_selector_expression
2355 (cp_parser *);
2356 static tree cp_parser_objc_expression
2357 (cp_parser *);
2358 static bool cp_parser_objc_selector_p
2359 (enum cpp_ttype);
2360 static tree cp_parser_objc_selector
2361 (cp_parser *);
2362 static tree cp_parser_objc_protocol_refs_opt
2363 (cp_parser *);
2364 static void cp_parser_objc_declaration
2365 (cp_parser *, tree);
2366 static tree cp_parser_objc_statement
2367 (cp_parser *);
2368 static bool cp_parser_objc_valid_prefix_attributes
2369 (cp_parser *, tree *);
2370 static void cp_parser_objc_at_property_declaration
2371 (cp_parser *) ;
2372 static void cp_parser_objc_at_synthesize_declaration
2373 (cp_parser *) ;
2374 static void cp_parser_objc_at_dynamic_declaration
2375 (cp_parser *) ;
2376 static tree cp_parser_objc_struct_declaration
2377 (cp_parser *) ;
2379 /* Utility Routines */
2381 static tree cp_parser_lookup_name
2382 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2383 static tree cp_parser_lookup_name_simple
2384 (cp_parser *, tree, location_t);
2385 static tree cp_parser_maybe_treat_template_as_class
2386 (tree, bool);
2387 static bool cp_parser_check_declarator_template_parameters
2388 (cp_parser *, cp_declarator *, location_t);
2389 static bool cp_parser_check_template_parameters
2390 (cp_parser *, unsigned, location_t, cp_declarator *);
2391 static tree cp_parser_simple_cast_expression
2392 (cp_parser *);
2393 static tree cp_parser_global_scope_opt
2394 (cp_parser *, bool);
2395 static bool cp_parser_constructor_declarator_p
2396 (cp_parser *, bool);
2397 static tree cp_parser_function_definition_from_specifiers_and_declarator
2398 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2399 static tree cp_parser_function_definition_after_declarator
2400 (cp_parser *, bool);
2401 static void cp_parser_template_declaration_after_export
2402 (cp_parser *, bool);
2403 static void cp_parser_perform_template_parameter_access_checks
2404 (vec<deferred_access_check, va_gc> *);
2405 static tree cp_parser_single_declaration
2406 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2407 static tree cp_parser_functional_cast
2408 (cp_parser *, tree);
2409 static tree cp_parser_save_member_function_body
2410 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2411 static tree cp_parser_save_nsdmi
2412 (cp_parser *);
2413 static tree cp_parser_enclosed_template_argument_list
2414 (cp_parser *);
2415 static void cp_parser_save_default_args
2416 (cp_parser *, tree);
2417 static void cp_parser_late_parsing_for_member
2418 (cp_parser *, tree);
2419 static tree cp_parser_late_parse_one_default_arg
2420 (cp_parser *, tree, tree, tree);
2421 static void cp_parser_late_parsing_nsdmi
2422 (cp_parser *, tree);
2423 static void cp_parser_late_parsing_default_args
2424 (cp_parser *, tree);
2425 static tree cp_parser_sizeof_operand
2426 (cp_parser *, enum rid);
2427 static tree cp_parser_trait_expr
2428 (cp_parser *, enum rid);
2429 static bool cp_parser_declares_only_class_p
2430 (cp_parser *);
2431 static void cp_parser_set_storage_class
2432 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2433 static void cp_parser_set_decl_spec_type
2434 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2435 static void set_and_check_decl_spec_loc
2436 (cp_decl_specifier_seq *decl_specs,
2437 cp_decl_spec ds, cp_token *);
2438 static bool cp_parser_friend_p
2439 (const cp_decl_specifier_seq *);
2440 static void cp_parser_required_error
2441 (cp_parser *, required_token, bool);
2442 static cp_token *cp_parser_require
2443 (cp_parser *, enum cpp_ttype, required_token);
2444 static cp_token *cp_parser_require_keyword
2445 (cp_parser *, enum rid, required_token);
2446 static bool cp_parser_token_starts_function_definition_p
2447 (cp_token *);
2448 static bool cp_parser_next_token_starts_class_definition_p
2449 (cp_parser *);
2450 static bool cp_parser_next_token_ends_template_argument_p
2451 (cp_parser *);
2452 static bool cp_parser_nth_token_starts_template_argument_list_p
2453 (cp_parser *, size_t);
2454 static enum tag_types cp_parser_token_is_class_key
2455 (cp_token *);
2456 static enum tag_types cp_parser_token_is_type_parameter_key
2457 (cp_token *);
2458 static void cp_parser_check_class_key
2459 (enum tag_types, tree type);
2460 static void cp_parser_check_access_in_redeclaration
2461 (tree type, location_t location);
2462 static bool cp_parser_optional_template_keyword
2463 (cp_parser *);
2464 static void cp_parser_pre_parsed_nested_name_specifier
2465 (cp_parser *);
2466 static bool cp_parser_cache_group
2467 (cp_parser *, enum cpp_ttype, unsigned);
2468 static tree cp_parser_cache_defarg
2469 (cp_parser *parser, bool nsdmi);
2470 static void cp_parser_parse_tentatively
2471 (cp_parser *);
2472 static void cp_parser_commit_to_tentative_parse
2473 (cp_parser *);
2474 static void cp_parser_commit_to_topmost_tentative_parse
2475 (cp_parser *);
2476 static void cp_parser_abort_tentative_parse
2477 (cp_parser *);
2478 static bool cp_parser_parse_definitely
2479 (cp_parser *);
2480 static inline bool cp_parser_parsing_tentatively
2481 (cp_parser *);
2482 static bool cp_parser_uncommitted_to_tentative_parse_p
2483 (cp_parser *);
2484 static void cp_parser_error
2485 (cp_parser *, const char *);
2486 static void cp_parser_name_lookup_error
2487 (cp_parser *, tree, tree, name_lookup_error, location_t);
2488 static bool cp_parser_simulate_error
2489 (cp_parser *);
2490 static bool cp_parser_check_type_definition
2491 (cp_parser *);
2492 static void cp_parser_check_for_definition_in_return_type
2493 (cp_declarator *, tree, location_t type_location);
2494 static void cp_parser_check_for_invalid_template_id
2495 (cp_parser *, tree, enum tag_types, location_t location);
2496 static bool cp_parser_non_integral_constant_expression
2497 (cp_parser *, non_integral_constant);
2498 static void cp_parser_diagnose_invalid_type_name
2499 (cp_parser *, tree, location_t);
2500 static bool cp_parser_parse_and_diagnose_invalid_type_name
2501 (cp_parser *);
2502 static int cp_parser_skip_to_closing_parenthesis
2503 (cp_parser *, bool, bool, bool);
2504 static void cp_parser_skip_to_end_of_statement
2505 (cp_parser *);
2506 static void cp_parser_consume_semicolon_at_end_of_statement
2507 (cp_parser *);
2508 static void cp_parser_skip_to_end_of_block_or_statement
2509 (cp_parser *);
2510 static bool cp_parser_skip_to_closing_brace
2511 (cp_parser *);
2512 static void cp_parser_skip_to_end_of_template_parameter_list
2513 (cp_parser *);
2514 static void cp_parser_skip_to_pragma_eol
2515 (cp_parser*, cp_token *);
2516 static bool cp_parser_error_occurred
2517 (cp_parser *);
2518 static bool cp_parser_allow_gnu_extensions_p
2519 (cp_parser *);
2520 static bool cp_parser_is_pure_string_literal
2521 (cp_token *);
2522 static bool cp_parser_is_string_literal
2523 (cp_token *);
2524 static bool cp_parser_is_keyword
2525 (cp_token *, enum rid);
2526 static tree cp_parser_make_typename_type
2527 (cp_parser *, tree, location_t location);
2528 static cp_declarator * cp_parser_make_indirect_declarator
2529 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2530 static bool cp_parser_compound_literal_p
2531 (cp_parser *);
2532 static bool cp_parser_array_designator_p
2533 (cp_parser *);
2534 static bool cp_parser_skip_to_closing_square_bracket
2535 (cp_parser *);
2537 /* Returns nonzero if we are parsing tentatively. */
2539 static inline bool
2540 cp_parser_parsing_tentatively (cp_parser* parser)
2542 return parser->context->next != NULL;
2545 /* Returns nonzero if TOKEN is a string literal. */
2547 static bool
2548 cp_parser_is_pure_string_literal (cp_token* token)
2550 return (token->type == CPP_STRING ||
2551 token->type == CPP_STRING16 ||
2552 token->type == CPP_STRING32 ||
2553 token->type == CPP_WSTRING ||
2554 token->type == CPP_UTF8STRING);
2557 /* Returns nonzero if TOKEN is a string literal
2558 of a user-defined string literal. */
2560 static bool
2561 cp_parser_is_string_literal (cp_token* token)
2563 return (cp_parser_is_pure_string_literal (token) ||
2564 token->type == CPP_STRING_USERDEF ||
2565 token->type == CPP_STRING16_USERDEF ||
2566 token->type == CPP_STRING32_USERDEF ||
2567 token->type == CPP_WSTRING_USERDEF ||
2568 token->type == CPP_UTF8STRING_USERDEF);
2571 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2573 static bool
2574 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2576 return token->keyword == keyword;
2579 /* If not parsing tentatively, issue a diagnostic of the form
2580 FILE:LINE: MESSAGE before TOKEN
2581 where TOKEN is the next token in the input stream. MESSAGE
2582 (specified by the caller) is usually of the form "expected
2583 OTHER-TOKEN". */
2585 static void
2586 cp_parser_error (cp_parser* parser, const char* gmsgid)
2588 if (!cp_parser_simulate_error (parser))
2590 cp_token *token = cp_lexer_peek_token (parser->lexer);
2591 /* This diagnostic makes more sense if it is tagged to the line
2592 of the token we just peeked at. */
2593 cp_lexer_set_source_position_from_token (token);
2595 if (token->type == CPP_PRAGMA)
2597 error_at (token->location,
2598 "%<#pragma%> is not allowed here");
2599 cp_parser_skip_to_pragma_eol (parser, token);
2600 return;
2603 c_parse_error (gmsgid,
2604 /* Because c_parser_error does not understand
2605 CPP_KEYWORD, keywords are treated like
2606 identifiers. */
2607 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2608 token->u.value, token->flags);
2612 /* Issue an error about name-lookup failing. NAME is the
2613 IDENTIFIER_NODE DECL is the result of
2614 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2615 the thing that we hoped to find. */
2617 static void
2618 cp_parser_name_lookup_error (cp_parser* parser,
2619 tree name,
2620 tree decl,
2621 name_lookup_error desired,
2622 location_t location)
2624 /* If name lookup completely failed, tell the user that NAME was not
2625 declared. */
2626 if (decl == error_mark_node)
2628 if (parser->scope && parser->scope != global_namespace)
2629 error_at (location, "%<%E::%E%> has not been declared",
2630 parser->scope, name);
2631 else if (parser->scope == global_namespace)
2632 error_at (location, "%<::%E%> has not been declared", name);
2633 else if (parser->object_scope
2634 && !CLASS_TYPE_P (parser->object_scope))
2635 error_at (location, "request for member %qE in non-class type %qT",
2636 name, parser->object_scope);
2637 else if (parser->object_scope)
2638 error_at (location, "%<%T::%E%> has not been declared",
2639 parser->object_scope, name);
2640 else
2641 error_at (location, "%qE has not been declared", name);
2643 else if (parser->scope && parser->scope != global_namespace)
2645 switch (desired)
2647 case NLE_TYPE:
2648 error_at (location, "%<%E::%E%> is not a type",
2649 parser->scope, name);
2650 break;
2651 case NLE_CXX98:
2652 error_at (location, "%<%E::%E%> is not a class or namespace",
2653 parser->scope, name);
2654 break;
2655 case NLE_NOT_CXX98:
2656 error_at (location,
2657 "%<%E::%E%> is not a class, namespace, or enumeration",
2658 parser->scope, name);
2659 break;
2660 default:
2661 gcc_unreachable ();
2665 else if (parser->scope == global_namespace)
2667 switch (desired)
2669 case NLE_TYPE:
2670 error_at (location, "%<::%E%> is not a type", name);
2671 break;
2672 case NLE_CXX98:
2673 error_at (location, "%<::%E%> is not a class or namespace", name);
2674 break;
2675 case NLE_NOT_CXX98:
2676 error_at (location,
2677 "%<::%E%> is not a class, namespace, or enumeration",
2678 name);
2679 break;
2680 default:
2681 gcc_unreachable ();
2684 else
2686 switch (desired)
2688 case NLE_TYPE:
2689 error_at (location, "%qE is not a type", name);
2690 break;
2691 case NLE_CXX98:
2692 error_at (location, "%qE is not a class or namespace", name);
2693 break;
2694 case NLE_NOT_CXX98:
2695 error_at (location,
2696 "%qE is not a class, namespace, or enumeration", name);
2697 break;
2698 default:
2699 gcc_unreachable ();
2704 /* If we are parsing tentatively, remember that an error has occurred
2705 during this tentative parse. Returns true if the error was
2706 simulated; false if a message should be issued by the caller. */
2708 static bool
2709 cp_parser_simulate_error (cp_parser* parser)
2711 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2713 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2714 return true;
2716 return false;
2719 /* This function is called when a type is defined. If type
2720 definitions are forbidden at this point, an error message is
2721 issued. */
2723 static bool
2724 cp_parser_check_type_definition (cp_parser* parser)
2726 /* If types are forbidden here, issue a message. */
2727 if (parser->type_definition_forbidden_message)
2729 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2730 in the message need to be interpreted. */
2731 error (parser->type_definition_forbidden_message);
2732 return false;
2734 return true;
2737 /* This function is called when the DECLARATOR is processed. The TYPE
2738 was a type defined in the decl-specifiers. If it is invalid to
2739 define a type in the decl-specifiers for DECLARATOR, an error is
2740 issued. TYPE_LOCATION is the location of TYPE and is used
2741 for error reporting. */
2743 static void
2744 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2745 tree type, location_t type_location)
2747 /* [dcl.fct] forbids type definitions in return types.
2748 Unfortunately, it's not easy to know whether or not we are
2749 processing a return type until after the fact. */
2750 while (declarator
2751 && (declarator->kind == cdk_pointer
2752 || declarator->kind == cdk_reference
2753 || declarator->kind == cdk_ptrmem))
2754 declarator = declarator->declarator;
2755 if (declarator
2756 && declarator->kind == cdk_function)
2758 error_at (type_location,
2759 "new types may not be defined in a return type");
2760 inform (type_location,
2761 "(perhaps a semicolon is missing after the definition of %qT)",
2762 type);
2766 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2767 "<" in any valid C++ program. If the next token is indeed "<",
2768 issue a message warning the user about what appears to be an
2769 invalid attempt to form a template-id. LOCATION is the location
2770 of the type-specifier (TYPE) */
2772 static void
2773 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2774 tree type,
2775 enum tag_types tag_type,
2776 location_t location)
2778 cp_token_position start = 0;
2780 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2782 if (TYPE_P (type))
2783 error_at (location, "%qT is not a template", type);
2784 else if (identifier_p (type))
2786 if (tag_type != none_type)
2787 error_at (location, "%qE is not a class template", type);
2788 else
2789 error_at (location, "%qE is not a template", type);
2791 else
2792 error_at (location, "invalid template-id");
2793 /* Remember the location of the invalid "<". */
2794 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2795 start = cp_lexer_token_position (parser->lexer, true);
2796 /* Consume the "<". */
2797 cp_lexer_consume_token (parser->lexer);
2798 /* Parse the template arguments. */
2799 cp_parser_enclosed_template_argument_list (parser);
2800 /* Permanently remove the invalid template arguments so that
2801 this error message is not issued again. */
2802 if (start)
2803 cp_lexer_purge_tokens_after (parser->lexer, start);
2807 /* If parsing an integral constant-expression, issue an error message
2808 about the fact that THING appeared and return true. Otherwise,
2809 return false. In either case, set
2810 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2812 static bool
2813 cp_parser_non_integral_constant_expression (cp_parser *parser,
2814 non_integral_constant thing)
2816 parser->non_integral_constant_expression_p = true;
2817 if (parser->integral_constant_expression_p)
2819 if (!parser->allow_non_integral_constant_expression_p)
2821 const char *msg = NULL;
2822 switch (thing)
2824 case NIC_FLOAT:
2825 error ("floating-point literal "
2826 "cannot appear in a constant-expression");
2827 return true;
2828 case NIC_CAST:
2829 error ("a cast to a type other than an integral or "
2830 "enumeration type cannot appear in a "
2831 "constant-expression");
2832 return true;
2833 case NIC_TYPEID:
2834 error ("%<typeid%> operator "
2835 "cannot appear in a constant-expression");
2836 return true;
2837 case NIC_NCC:
2838 error ("non-constant compound literals "
2839 "cannot appear in a constant-expression");
2840 return true;
2841 case NIC_FUNC_CALL:
2842 error ("a function call "
2843 "cannot appear in a constant-expression");
2844 return true;
2845 case NIC_INC:
2846 error ("an increment "
2847 "cannot appear in a constant-expression");
2848 return true;
2849 case NIC_DEC:
2850 error ("an decrement "
2851 "cannot appear in a constant-expression");
2852 return true;
2853 case NIC_ARRAY_REF:
2854 error ("an array reference "
2855 "cannot appear in a constant-expression");
2856 return true;
2857 case NIC_ADDR_LABEL:
2858 error ("the address of a label "
2859 "cannot appear in a constant-expression");
2860 return true;
2861 case NIC_OVERLOADED:
2862 error ("calls to overloaded operators "
2863 "cannot appear in a constant-expression");
2864 return true;
2865 case NIC_ASSIGNMENT:
2866 error ("an assignment cannot appear in a constant-expression");
2867 return true;
2868 case NIC_COMMA:
2869 error ("a comma operator "
2870 "cannot appear in a constant-expression");
2871 return true;
2872 case NIC_CONSTRUCTOR:
2873 error ("a call to a constructor "
2874 "cannot appear in a constant-expression");
2875 return true;
2876 case NIC_TRANSACTION:
2877 error ("a transaction expression "
2878 "cannot appear in a constant-expression");
2879 return true;
2880 case NIC_THIS:
2881 msg = "this";
2882 break;
2883 case NIC_FUNC_NAME:
2884 msg = "__FUNCTION__";
2885 break;
2886 case NIC_PRETTY_FUNC:
2887 msg = "__PRETTY_FUNCTION__";
2888 break;
2889 case NIC_C99_FUNC:
2890 msg = "__func__";
2891 break;
2892 case NIC_VA_ARG:
2893 msg = "va_arg";
2894 break;
2895 case NIC_ARROW:
2896 msg = "->";
2897 break;
2898 case NIC_POINT:
2899 msg = ".";
2900 break;
2901 case NIC_STAR:
2902 msg = "*";
2903 break;
2904 case NIC_ADDR:
2905 msg = "&";
2906 break;
2907 case NIC_PREINCREMENT:
2908 msg = "++";
2909 break;
2910 case NIC_PREDECREMENT:
2911 msg = "--";
2912 break;
2913 case NIC_NEW:
2914 msg = "new";
2915 break;
2916 case NIC_DEL:
2917 msg = "delete";
2918 break;
2919 default:
2920 gcc_unreachable ();
2922 if (msg)
2923 error ("%qs cannot appear in a constant-expression", msg);
2924 return true;
2927 return false;
2930 /* Emit a diagnostic for an invalid type name. This function commits
2931 to the current active tentative parse, if any. (Otherwise, the
2932 problematic construct might be encountered again later, resulting
2933 in duplicate error messages.) LOCATION is the location of ID. */
2935 static void
2936 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2937 location_t location)
2939 tree decl, ambiguous_decls;
2940 cp_parser_commit_to_tentative_parse (parser);
2941 /* Try to lookup the identifier. */
2942 decl = cp_parser_lookup_name (parser, id, none_type,
2943 /*is_template=*/false,
2944 /*is_namespace=*/false,
2945 /*check_dependency=*/true,
2946 &ambiguous_decls, location);
2947 if (ambiguous_decls)
2948 /* If the lookup was ambiguous, an error will already have
2949 been issued. */
2950 return;
2951 /* If the lookup found a template-name, it means that the user forgot
2952 to specify an argument list. Emit a useful error message. */
2953 if (TREE_CODE (decl) == TEMPLATE_DECL)
2954 error_at (location,
2955 "invalid use of template-name %qE without an argument list",
2956 decl);
2957 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2958 error_at (location, "invalid use of destructor %qD as a type", id);
2959 else if (TREE_CODE (decl) == TYPE_DECL)
2960 /* Something like 'unsigned A a;' */
2961 error_at (location, "invalid combination of multiple type-specifiers");
2962 else if (!parser->scope)
2964 /* Issue an error message. */
2965 error_at (location, "%qE does not name a type", id);
2966 /* If we're in a template class, it's possible that the user was
2967 referring to a type from a base class. For example:
2969 template <typename T> struct A { typedef T X; };
2970 template <typename T> struct B : public A<T> { X x; };
2972 The user should have said "typename A<T>::X". */
2973 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2974 inform (location, "C++11 %<constexpr%> only available with "
2975 "-std=c++11 or -std=gnu++11");
2976 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2977 inform (location, "C++11 %<noexcept%> only available with "
2978 "-std=c++11 or -std=gnu++11");
2979 else if (cxx_dialect < cxx11
2980 && TREE_CODE (id) == IDENTIFIER_NODE
2981 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2982 inform (location, "C++11 %<thread_local%> only available with "
2983 "-std=c++11 or -std=gnu++11");
2984 else if (processing_template_decl && current_class_type
2985 && TYPE_BINFO (current_class_type))
2987 tree b;
2989 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2991 b = TREE_CHAIN (b))
2993 tree base_type = BINFO_TYPE (b);
2994 if (CLASS_TYPE_P (base_type)
2995 && dependent_type_p (base_type))
2997 tree field;
2998 /* Go from a particular instantiation of the
2999 template (which will have an empty TYPE_FIELDs),
3000 to the main version. */
3001 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3002 for (field = TYPE_FIELDS (base_type);
3003 field;
3004 field = DECL_CHAIN (field))
3005 if (TREE_CODE (field) == TYPE_DECL
3006 && DECL_NAME (field) == id)
3008 inform (location,
3009 "(perhaps %<typename %T::%E%> was intended)",
3010 BINFO_TYPE (b), id);
3011 break;
3013 if (field)
3014 break;
3019 /* Here we diagnose qualified-ids where the scope is actually correct,
3020 but the identifier does not resolve to a valid type name. */
3021 else if (parser->scope != error_mark_node)
3023 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3025 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3026 error_at (location_of (id),
3027 "%qE in namespace %qE does not name a template type",
3028 id, parser->scope);
3029 else
3030 error_at (location_of (id),
3031 "%qE in namespace %qE does not name a type",
3032 id, parser->scope);
3034 else if (CLASS_TYPE_P (parser->scope)
3035 && constructor_name_p (id, parser->scope))
3037 /* A<T>::A<T>() */
3038 error_at (location, "%<%T::%E%> names the constructor, not"
3039 " the type", parser->scope, id);
3040 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3041 error_at (location, "and %qT has no template constructors",
3042 parser->scope);
3044 else if (TYPE_P (parser->scope)
3045 && dependent_scope_p (parser->scope))
3046 error_at (location, "need %<typename%> before %<%T::%E%> because "
3047 "%qT is a dependent scope",
3048 parser->scope, id, parser->scope);
3049 else if (TYPE_P (parser->scope))
3051 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3052 error_at (location_of (id),
3053 "%qE in %q#T does not name a template type",
3054 id, parser->scope);
3055 else
3056 error_at (location_of (id),
3057 "%qE in %q#T does not name a type",
3058 id, parser->scope);
3060 else
3061 gcc_unreachable ();
3065 /* Check for a common situation where a type-name should be present,
3066 but is not, and issue a sensible error message. Returns true if an
3067 invalid type-name was detected.
3069 The situation handled by this function are variable declarations of the
3070 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3071 Usually, `ID' should name a type, but if we got here it means that it
3072 does not. We try to emit the best possible error message depending on
3073 how exactly the id-expression looks like. */
3075 static bool
3076 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3078 tree id;
3079 cp_token *token = cp_lexer_peek_token (parser->lexer);
3081 /* Avoid duplicate error about ambiguous lookup. */
3082 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3084 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3085 if (next->type == CPP_NAME && next->error_reported)
3086 goto out;
3089 cp_parser_parse_tentatively (parser);
3090 id = cp_parser_id_expression (parser,
3091 /*template_keyword_p=*/false,
3092 /*check_dependency_p=*/true,
3093 /*template_p=*/NULL,
3094 /*declarator_p=*/true,
3095 /*optional_p=*/false);
3096 /* If the next token is a (, this is a function with no explicit return
3097 type, i.e. constructor, destructor or conversion op. */
3098 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3099 || TREE_CODE (id) == TYPE_DECL)
3101 cp_parser_abort_tentative_parse (parser);
3102 return false;
3104 if (!cp_parser_parse_definitely (parser))
3105 return false;
3107 /* Emit a diagnostic for the invalid type. */
3108 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3109 out:
3110 /* If we aren't in the middle of a declarator (i.e. in a
3111 parameter-declaration-clause), skip to the end of the declaration;
3112 there's no point in trying to process it. */
3113 if (!parser->in_declarator_p)
3114 cp_parser_skip_to_end_of_block_or_statement (parser);
3115 return true;
3118 /* Consume tokens up to, and including, the next non-nested closing `)'.
3119 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3120 are doing error recovery. Returns -1 if OR_COMMA is true and we
3121 found an unnested comma. */
3123 static int
3124 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3125 bool recovering,
3126 bool or_comma,
3127 bool consume_paren)
3129 unsigned paren_depth = 0;
3130 unsigned brace_depth = 0;
3131 unsigned square_depth = 0;
3133 if (recovering && !or_comma
3134 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3135 return 0;
3137 while (true)
3139 cp_token * token = cp_lexer_peek_token (parser->lexer);
3141 switch (token->type)
3143 case CPP_EOF:
3144 case CPP_PRAGMA_EOL:
3145 /* If we've run out of tokens, then there is no closing `)'. */
3146 return 0;
3148 /* This is good for lambda expression capture-lists. */
3149 case CPP_OPEN_SQUARE:
3150 ++square_depth;
3151 break;
3152 case CPP_CLOSE_SQUARE:
3153 if (!square_depth--)
3154 return 0;
3155 break;
3157 case CPP_SEMICOLON:
3158 /* This matches the processing in skip_to_end_of_statement. */
3159 if (!brace_depth)
3160 return 0;
3161 break;
3163 case CPP_OPEN_BRACE:
3164 ++brace_depth;
3165 break;
3166 case CPP_CLOSE_BRACE:
3167 if (!brace_depth--)
3168 return 0;
3169 break;
3171 case CPP_COMMA:
3172 if (recovering && or_comma && !brace_depth && !paren_depth
3173 && !square_depth)
3174 return -1;
3175 break;
3177 case CPP_OPEN_PAREN:
3178 if (!brace_depth)
3179 ++paren_depth;
3180 break;
3182 case CPP_CLOSE_PAREN:
3183 if (!brace_depth && !paren_depth--)
3185 if (consume_paren)
3186 cp_lexer_consume_token (parser->lexer);
3187 return 1;
3189 break;
3191 default:
3192 break;
3195 /* Consume the token. */
3196 cp_lexer_consume_token (parser->lexer);
3200 /* Consume tokens until we reach the end of the current statement.
3201 Normally, that will be just before consuming a `;'. However, if a
3202 non-nested `}' comes first, then we stop before consuming that. */
3204 static void
3205 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3207 unsigned nesting_depth = 0;
3209 /* Unwind generic function template scope if necessary. */
3210 if (parser->fully_implicit_function_template_p)
3211 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3213 while (true)
3215 cp_token *token = cp_lexer_peek_token (parser->lexer);
3217 switch (token->type)
3219 case CPP_EOF:
3220 case CPP_PRAGMA_EOL:
3221 /* If we've run out of tokens, stop. */
3222 return;
3224 case CPP_SEMICOLON:
3225 /* If the next token is a `;', we have reached the end of the
3226 statement. */
3227 if (!nesting_depth)
3228 return;
3229 break;
3231 case CPP_CLOSE_BRACE:
3232 /* If this is a non-nested '}', stop before consuming it.
3233 That way, when confronted with something like:
3235 { 3 + }
3237 we stop before consuming the closing '}', even though we
3238 have not yet reached a `;'. */
3239 if (nesting_depth == 0)
3240 return;
3242 /* If it is the closing '}' for a block that we have
3243 scanned, stop -- but only after consuming the token.
3244 That way given:
3246 void f g () { ... }
3247 typedef int I;
3249 we will stop after the body of the erroneously declared
3250 function, but before consuming the following `typedef'
3251 declaration. */
3252 if (--nesting_depth == 0)
3254 cp_lexer_consume_token (parser->lexer);
3255 return;
3258 case CPP_OPEN_BRACE:
3259 ++nesting_depth;
3260 break;
3262 default:
3263 break;
3266 /* Consume the token. */
3267 cp_lexer_consume_token (parser->lexer);
3271 /* This function is called at the end of a statement or declaration.
3272 If the next token is a semicolon, it is consumed; otherwise, error
3273 recovery is attempted. */
3275 static void
3276 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3278 /* Look for the trailing `;'. */
3279 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3281 /* If there is additional (erroneous) input, skip to the end of
3282 the statement. */
3283 cp_parser_skip_to_end_of_statement (parser);
3284 /* If the next token is now a `;', consume it. */
3285 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3286 cp_lexer_consume_token (parser->lexer);
3290 /* Skip tokens until we have consumed an entire block, or until we
3291 have consumed a non-nested `;'. */
3293 static void
3294 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3296 int nesting_depth = 0;
3298 /* Unwind generic function template scope if necessary. */
3299 if (parser->fully_implicit_function_template_p)
3300 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3302 while (nesting_depth >= 0)
3304 cp_token *token = cp_lexer_peek_token (parser->lexer);
3306 switch (token->type)
3308 case CPP_EOF:
3309 case CPP_PRAGMA_EOL:
3310 /* If we've run out of tokens, stop. */
3311 return;
3313 case CPP_SEMICOLON:
3314 /* Stop if this is an unnested ';'. */
3315 if (!nesting_depth)
3316 nesting_depth = -1;
3317 break;
3319 case CPP_CLOSE_BRACE:
3320 /* Stop if this is an unnested '}', or closes the outermost
3321 nesting level. */
3322 nesting_depth--;
3323 if (nesting_depth < 0)
3324 return;
3325 if (!nesting_depth)
3326 nesting_depth = -1;
3327 break;
3329 case CPP_OPEN_BRACE:
3330 /* Nest. */
3331 nesting_depth++;
3332 break;
3334 default:
3335 break;
3338 /* Consume the token. */
3339 cp_lexer_consume_token (parser->lexer);
3343 /* Skip tokens until a non-nested closing curly brace is the next
3344 token, or there are no more tokens. Return true in the first case,
3345 false otherwise. */
3347 static bool
3348 cp_parser_skip_to_closing_brace (cp_parser *parser)
3350 unsigned nesting_depth = 0;
3352 while (true)
3354 cp_token *token = cp_lexer_peek_token (parser->lexer);
3356 switch (token->type)
3358 case CPP_EOF:
3359 case CPP_PRAGMA_EOL:
3360 /* If we've run out of tokens, stop. */
3361 return false;
3363 case CPP_CLOSE_BRACE:
3364 /* If the next token is a non-nested `}', then we have reached
3365 the end of the current block. */
3366 if (nesting_depth-- == 0)
3367 return true;
3368 break;
3370 case CPP_OPEN_BRACE:
3371 /* If it the next token is a `{', then we are entering a new
3372 block. Consume the entire block. */
3373 ++nesting_depth;
3374 break;
3376 default:
3377 break;
3380 /* Consume the token. */
3381 cp_lexer_consume_token (parser->lexer);
3385 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3386 parameter is the PRAGMA token, allowing us to purge the entire pragma
3387 sequence. */
3389 static void
3390 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3392 cp_token *token;
3394 parser->lexer->in_pragma = false;
3397 token = cp_lexer_consume_token (parser->lexer);
3398 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3400 /* Ensure that the pragma is not parsed again. */
3401 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3404 /* Require pragma end of line, resyncing with it as necessary. The
3405 arguments are as for cp_parser_skip_to_pragma_eol. */
3407 static void
3408 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3410 parser->lexer->in_pragma = false;
3411 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3412 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3415 /* This is a simple wrapper around make_typename_type. When the id is
3416 an unresolved identifier node, we can provide a superior diagnostic
3417 using cp_parser_diagnose_invalid_type_name. */
3419 static tree
3420 cp_parser_make_typename_type (cp_parser *parser, tree id,
3421 location_t id_location)
3423 tree result;
3424 if (identifier_p (id))
3426 result = make_typename_type (parser->scope, id, typename_type,
3427 /*complain=*/tf_none);
3428 if (result == error_mark_node)
3429 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3430 return result;
3432 return make_typename_type (parser->scope, id, typename_type, tf_error);
3435 /* This is a wrapper around the
3436 make_{pointer,ptrmem,reference}_declarator functions that decides
3437 which one to call based on the CODE and CLASS_TYPE arguments. The
3438 CODE argument should be one of the values returned by
3439 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3440 appertain to the pointer or reference. */
3442 static cp_declarator *
3443 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3444 cp_cv_quals cv_qualifiers,
3445 cp_declarator *target,
3446 tree attributes)
3448 if (code == ERROR_MARK)
3449 return cp_error_declarator;
3451 if (code == INDIRECT_REF)
3452 if (class_type == NULL_TREE)
3453 return make_pointer_declarator (cv_qualifiers, target, attributes);
3454 else
3455 return make_ptrmem_declarator (cv_qualifiers, class_type,
3456 target, attributes);
3457 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3458 return make_reference_declarator (cv_qualifiers, target,
3459 false, attributes);
3460 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3461 return make_reference_declarator (cv_qualifiers, target,
3462 true, attributes);
3463 gcc_unreachable ();
3466 /* Create a new C++ parser. */
3468 static cp_parser *
3469 cp_parser_new (void)
3471 cp_parser *parser;
3472 cp_lexer *lexer;
3473 unsigned i;
3475 /* cp_lexer_new_main is called before doing GC allocation because
3476 cp_lexer_new_main might load a PCH file. */
3477 lexer = cp_lexer_new_main ();
3479 /* Initialize the binops_by_token so that we can get the tree
3480 directly from the token. */
3481 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3482 binops_by_token[binops[i].token_type] = binops[i];
3484 parser = ggc_cleared_alloc<cp_parser> ();
3485 parser->lexer = lexer;
3486 parser->context = cp_parser_context_new (NULL);
3488 /* For now, we always accept GNU extensions. */
3489 parser->allow_gnu_extensions_p = 1;
3491 /* The `>' token is a greater-than operator, not the end of a
3492 template-id. */
3493 parser->greater_than_is_operator_p = true;
3495 parser->default_arg_ok_p = true;
3497 /* We are not parsing a constant-expression. */
3498 parser->integral_constant_expression_p = false;
3499 parser->allow_non_integral_constant_expression_p = false;
3500 parser->non_integral_constant_expression_p = false;
3502 /* Local variable names are not forbidden. */
3503 parser->local_variables_forbidden_p = false;
3505 /* We are not processing an `extern "C"' declaration. */
3506 parser->in_unbraced_linkage_specification_p = false;
3508 /* We are not processing a declarator. */
3509 parser->in_declarator_p = false;
3511 /* We are not processing a template-argument-list. */
3512 parser->in_template_argument_list_p = false;
3514 /* We are not in an iteration statement. */
3515 parser->in_statement = 0;
3517 /* We are not in a switch statement. */
3518 parser->in_switch_statement_p = false;
3520 /* We are not parsing a type-id inside an expression. */
3521 parser->in_type_id_in_expr_p = false;
3523 /* Declarations aren't implicitly extern "C". */
3524 parser->implicit_extern_c = false;
3526 /* String literals should be translated to the execution character set. */
3527 parser->translate_strings_p = true;
3529 /* We are not parsing a function body. */
3530 parser->in_function_body = false;
3532 /* We can correct until told otherwise. */
3533 parser->colon_corrects_to_scope_p = true;
3535 /* The unparsed function queue is empty. */
3536 push_unparsed_function_queues (parser);
3538 /* There are no classes being defined. */
3539 parser->num_classes_being_defined = 0;
3541 /* No template parameters apply. */
3542 parser->num_template_parameter_lists = 0;
3544 /* Not declaring an implicit function template. */
3545 parser->auto_is_implicit_function_template_parm_p = false;
3546 parser->fully_implicit_function_template_p = false;
3547 parser->implicit_template_parms = 0;
3548 parser->implicit_template_scope = 0;
3550 return parser;
3553 /* Create a cp_lexer structure which will emit the tokens in CACHE
3554 and push it onto the parser's lexer stack. This is used for delayed
3555 parsing of in-class method bodies and default arguments, and should
3556 not be confused with tentative parsing. */
3557 static void
3558 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3560 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3561 lexer->next = parser->lexer;
3562 parser->lexer = lexer;
3564 /* Move the current source position to that of the first token in the
3565 new lexer. */
3566 cp_lexer_set_source_position_from_token (lexer->next_token);
3569 /* Pop the top lexer off the parser stack. This is never used for the
3570 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3571 static void
3572 cp_parser_pop_lexer (cp_parser *parser)
3574 cp_lexer *lexer = parser->lexer;
3575 parser->lexer = lexer->next;
3576 cp_lexer_destroy (lexer);
3578 /* Put the current source position back where it was before this
3579 lexer was pushed. */
3580 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3583 /* Lexical conventions [gram.lex] */
3585 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3586 identifier. */
3588 static tree
3589 cp_parser_identifier (cp_parser* parser)
3591 cp_token *token;
3593 /* Look for the identifier. */
3594 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3595 /* Return the value. */
3596 return token ? token->u.value : error_mark_node;
3599 /* Parse a sequence of adjacent string constants. Returns a
3600 TREE_STRING representing the combined, nul-terminated string
3601 constant. If TRANSLATE is true, translate the string to the
3602 execution character set. If WIDE_OK is true, a wide string is
3603 invalid here.
3605 C++98 [lex.string] says that if a narrow string literal token is
3606 adjacent to a wide string literal token, the behavior is undefined.
3607 However, C99 6.4.5p4 says that this results in a wide string literal.
3608 We follow C99 here, for consistency with the C front end.
3610 This code is largely lifted from lex_string() in c-lex.c.
3612 FUTURE: ObjC++ will need to handle @-strings here. */
3613 static tree
3614 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3615 bool lookup_udlit = true)
3617 tree value;
3618 size_t count;
3619 struct obstack str_ob;
3620 cpp_string str, istr, *strs;
3621 cp_token *tok;
3622 enum cpp_ttype type, curr_type;
3623 int have_suffix_p = 0;
3624 tree string_tree;
3625 tree suffix_id = NULL_TREE;
3626 bool curr_tok_is_userdef_p = false;
3628 tok = cp_lexer_peek_token (parser->lexer);
3629 if (!cp_parser_is_string_literal (tok))
3631 cp_parser_error (parser, "expected string-literal");
3632 return error_mark_node;
3635 if (cpp_userdef_string_p (tok->type))
3637 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3638 curr_type = cpp_userdef_string_remove_type (tok->type);
3639 curr_tok_is_userdef_p = true;
3641 else
3643 string_tree = tok->u.value;
3644 curr_type = tok->type;
3646 type = curr_type;
3648 /* Try to avoid the overhead of creating and destroying an obstack
3649 for the common case of just one string. */
3650 if (!cp_parser_is_string_literal
3651 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3653 cp_lexer_consume_token (parser->lexer);
3655 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3656 str.len = TREE_STRING_LENGTH (string_tree);
3657 count = 1;
3659 if (curr_tok_is_userdef_p)
3661 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3662 have_suffix_p = 1;
3663 curr_type = cpp_userdef_string_remove_type (tok->type);
3665 else
3666 curr_type = tok->type;
3668 strs = &str;
3670 else
3672 gcc_obstack_init (&str_ob);
3673 count = 0;
3677 cp_lexer_consume_token (parser->lexer);
3678 count++;
3679 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3680 str.len = TREE_STRING_LENGTH (string_tree);
3682 if (curr_tok_is_userdef_p)
3684 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3685 if (have_suffix_p == 0)
3687 suffix_id = curr_suffix_id;
3688 have_suffix_p = 1;
3690 else if (have_suffix_p == 1
3691 && curr_suffix_id != suffix_id)
3693 error ("inconsistent user-defined literal suffixes"
3694 " %qD and %qD in string literal",
3695 suffix_id, curr_suffix_id);
3696 have_suffix_p = -1;
3698 curr_type = cpp_userdef_string_remove_type (tok->type);
3700 else
3701 curr_type = tok->type;
3703 if (type != curr_type)
3705 if (type == CPP_STRING)
3706 type = curr_type;
3707 else if (curr_type != CPP_STRING)
3708 error_at (tok->location,
3709 "unsupported non-standard concatenation "
3710 "of string literals");
3713 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3715 tok = cp_lexer_peek_token (parser->lexer);
3716 if (cpp_userdef_string_p (tok->type))
3718 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3719 curr_type = cpp_userdef_string_remove_type (tok->type);
3720 curr_tok_is_userdef_p = true;
3722 else
3724 string_tree = tok->u.value;
3725 curr_type = tok->type;
3726 curr_tok_is_userdef_p = false;
3729 while (cp_parser_is_string_literal (tok));
3731 strs = (cpp_string *) obstack_finish (&str_ob);
3734 if (type != CPP_STRING && !wide_ok)
3736 cp_parser_error (parser, "a wide string is invalid in this context");
3737 type = CPP_STRING;
3740 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3741 (parse_in, strs, count, &istr, type))
3743 value = build_string (istr.len, (const char *)istr.text);
3744 free (CONST_CAST (unsigned char *, istr.text));
3746 switch (type)
3748 default:
3749 case CPP_STRING:
3750 case CPP_UTF8STRING:
3751 TREE_TYPE (value) = char_array_type_node;
3752 break;
3753 case CPP_STRING16:
3754 TREE_TYPE (value) = char16_array_type_node;
3755 break;
3756 case CPP_STRING32:
3757 TREE_TYPE (value) = char32_array_type_node;
3758 break;
3759 case CPP_WSTRING:
3760 TREE_TYPE (value) = wchar_array_type_node;
3761 break;
3764 value = fix_string_type (value);
3766 if (have_suffix_p)
3768 tree literal = build_userdef_literal (suffix_id, value,
3769 OT_NONE, NULL_TREE);
3770 if (lookup_udlit)
3771 value = cp_parser_userdef_string_literal (literal);
3772 else
3773 value = literal;
3776 else
3777 /* cpp_interpret_string has issued an error. */
3778 value = error_mark_node;
3780 if (count > 1)
3781 obstack_free (&str_ob, 0);
3783 return value;
3786 /* Look up a literal operator with the name and the exact arguments. */
3788 static tree
3789 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3791 tree decl, fns;
3792 decl = lookup_name (name);
3793 if (!decl || !is_overloaded_fn (decl))
3794 return error_mark_node;
3796 for (fns = decl; fns; fns = OVL_NEXT (fns))
3798 unsigned int ix;
3799 bool found = true;
3800 tree fn = OVL_CURRENT (fns);
3801 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3802 if (parmtypes != NULL_TREE)
3804 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3805 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3807 tree tparm = TREE_VALUE (parmtypes);
3808 tree targ = TREE_TYPE ((*args)[ix]);
3809 bool ptr = TYPE_PTR_P (tparm);
3810 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3811 if ((ptr || arr || !same_type_p (tparm, targ))
3812 && (!ptr || !arr
3813 || !same_type_p (TREE_TYPE (tparm),
3814 TREE_TYPE (targ))))
3815 found = false;
3817 if (found
3818 && ix == vec_safe_length (args)
3819 /* May be this should be sufficient_parms_p instead,
3820 depending on how exactly should user-defined literals
3821 work in presence of default arguments on the literal
3822 operator parameters. */
3823 && parmtypes == void_list_node)
3824 return fn;
3828 return error_mark_node;
3831 /* Parse a user-defined char constant. Returns a call to a user-defined
3832 literal operator taking the character as an argument. */
3834 static tree
3835 cp_parser_userdef_char_literal (cp_parser *parser)
3837 cp_token *token = cp_lexer_consume_token (parser->lexer);
3838 tree literal = token->u.value;
3839 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3840 tree value = USERDEF_LITERAL_VALUE (literal);
3841 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3842 tree decl, result;
3844 /* Build up a call to the user-defined operator */
3845 /* Lookup the name we got back from the id-expression. */
3846 vec<tree, va_gc> *args = make_tree_vector ();
3847 vec_safe_push (args, value);
3848 decl = lookup_literal_operator (name, args);
3849 if (!decl || decl == error_mark_node)
3851 error ("unable to find character literal operator %qD with %qT argument",
3852 name, TREE_TYPE (value));
3853 release_tree_vector (args);
3854 return error_mark_node;
3856 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3857 release_tree_vector (args);
3858 if (result != error_mark_node)
3859 return result;
3861 error ("unable to find character literal operator %qD with %qT argument",
3862 name, TREE_TYPE (value));
3863 return error_mark_node;
3866 /* A subroutine of cp_parser_userdef_numeric_literal to
3867 create a char... template parameter pack from a string node. */
3869 static tree
3870 make_char_string_pack (tree value)
3872 tree charvec;
3873 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3874 const char *str = TREE_STRING_POINTER (value);
3875 int i, len = TREE_STRING_LENGTH (value) - 1;
3876 tree argvec = make_tree_vec (1);
3878 /* Fill in CHARVEC with all of the parameters. */
3879 charvec = make_tree_vec (len);
3880 for (i = 0; i < len; ++i)
3881 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3883 /* Build the argument packs. */
3884 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3885 TREE_TYPE (argpack) = char_type_node;
3887 TREE_VEC_ELT (argvec, 0) = argpack;
3889 return argvec;
3892 /* A subroutine of cp_parser_userdef_numeric_literal to
3893 create a char... template parameter pack from a string node. */
3895 static tree
3896 make_string_pack (tree value)
3898 tree charvec;
3899 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3900 const unsigned char *str
3901 = (const unsigned char *) TREE_STRING_POINTER (value);
3902 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3903 int len = TREE_STRING_LENGTH (value) / sz - 1;
3904 tree argvec = make_tree_vec (2);
3906 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3907 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3909 /* First template parm is character type. */
3910 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3912 /* Fill in CHARVEC with all of the parameters. */
3913 charvec = make_tree_vec (len);
3914 for (int i = 0; i < len; ++i)
3915 TREE_VEC_ELT (charvec, i)
3916 = double_int_to_tree (str_char_type_node,
3917 double_int::from_buffer (str + i * sz, sz));
3919 /* Build the argument packs. */
3920 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3921 TREE_TYPE (argpack) = str_char_type_node;
3923 TREE_VEC_ELT (argvec, 1) = argpack;
3925 return argvec;
3928 /* Parse a user-defined numeric constant. returns a call to a user-defined
3929 literal operator. */
3931 static tree
3932 cp_parser_userdef_numeric_literal (cp_parser *parser)
3934 cp_token *token = cp_lexer_consume_token (parser->lexer);
3935 tree literal = token->u.value;
3936 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3937 tree value = USERDEF_LITERAL_VALUE (literal);
3938 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3939 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3940 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3941 tree decl, result;
3942 vec<tree, va_gc> *args;
3944 /* Look for a literal operator taking the exact type of numeric argument
3945 as the literal value. */
3946 args = make_tree_vector ();
3947 vec_safe_push (args, value);
3948 decl = lookup_literal_operator (name, args);
3949 if (decl && decl != error_mark_node)
3951 result = finish_call_expr (decl, &args, false, true, tf_none);
3952 if (result != error_mark_node)
3954 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3955 warning_at (token->location, OPT_Woverflow,
3956 "integer literal exceeds range of %qT type",
3957 long_long_unsigned_type_node);
3958 else
3960 if (overflow > 0)
3961 warning_at (token->location, OPT_Woverflow,
3962 "floating literal exceeds range of %qT type",
3963 long_double_type_node);
3964 else if (overflow < 0)
3965 warning_at (token->location, OPT_Woverflow,
3966 "floating literal truncated to zero");
3968 release_tree_vector (args);
3969 return result;
3972 release_tree_vector (args);
3974 /* If the numeric argument didn't work, look for a raw literal
3975 operator taking a const char* argument consisting of the number
3976 in string format. */
3977 args = make_tree_vector ();
3978 vec_safe_push (args, num_string);
3979 decl = lookup_literal_operator (name, args);
3980 if (decl && decl != error_mark_node)
3982 result = finish_call_expr (decl, &args, false, true, tf_none);
3983 if (result != error_mark_node)
3985 release_tree_vector (args);
3986 return result;
3989 release_tree_vector (args);
3991 /* If the raw literal didn't work, look for a non-type template
3992 function with parameter pack char.... Call the function with
3993 template parameter characters representing the number. */
3994 args = make_tree_vector ();
3995 decl = lookup_literal_operator (name, args);
3996 if (decl && decl != error_mark_node)
3998 tree tmpl_args = make_char_string_pack (num_string);
3999 decl = lookup_template_function (decl, tmpl_args);
4000 result = finish_call_expr (decl, &args, false, true, tf_none);
4001 if (result != error_mark_node)
4003 release_tree_vector (args);
4004 return result;
4007 release_tree_vector (args);
4009 error ("unable to find numeric literal operator %qD", name);
4010 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4011 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4012 "to enable more built-in suffixes");
4013 return error_mark_node;
4016 /* Parse a user-defined string constant. Returns a call to a user-defined
4017 literal operator taking a character pointer and the length of the string
4018 as arguments. */
4020 static tree
4021 cp_parser_userdef_string_literal (tree literal)
4023 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4024 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4025 tree value = USERDEF_LITERAL_VALUE (literal);
4026 int len = TREE_STRING_LENGTH (value)
4027 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4028 tree decl, result;
4029 vec<tree, va_gc> *args;
4031 /* Look for a template function with typename parameter CharT
4032 and parameter pack CharT... Call the function with
4033 template parameter characters representing the string. */
4034 args = make_tree_vector ();
4035 decl = lookup_literal_operator (name, args);
4036 if (decl && decl != error_mark_node)
4038 tree tmpl_args = make_string_pack (value);
4039 decl = lookup_template_function (decl, tmpl_args);
4040 result = finish_call_expr (decl, &args, false, true, tf_none);
4041 if (result != error_mark_node)
4043 release_tree_vector (args);
4044 return result;
4047 release_tree_vector (args);
4049 /* Build up a call to the user-defined operator */
4050 /* Lookup the name we got back from the id-expression. */
4051 args = make_tree_vector ();
4052 vec_safe_push (args, value);
4053 vec_safe_push (args, build_int_cst (size_type_node, len));
4054 decl = lookup_name (name);
4055 if (!decl || decl == error_mark_node)
4057 error ("unable to find string literal operator %qD", name);
4058 release_tree_vector (args);
4059 return error_mark_node;
4061 result = finish_call_expr (decl, &args, false, true, tf_none);
4062 release_tree_vector (args);
4063 if (result != error_mark_node)
4064 return result;
4066 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4067 name, TREE_TYPE (value), size_type_node);
4068 return error_mark_node;
4072 /* Basic concepts [gram.basic] */
4074 /* Parse a translation-unit.
4076 translation-unit:
4077 declaration-seq [opt]
4079 Returns TRUE if all went well. */
4081 static bool
4082 cp_parser_translation_unit (cp_parser* parser)
4084 /* The address of the first non-permanent object on the declarator
4085 obstack. */
4086 static void *declarator_obstack_base;
4088 bool success;
4090 /* Create the declarator obstack, if necessary. */
4091 if (!cp_error_declarator)
4093 gcc_obstack_init (&declarator_obstack);
4094 /* Create the error declarator. */
4095 cp_error_declarator = make_declarator (cdk_error);
4096 /* Create the empty parameter list. */
4097 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4098 /* Remember where the base of the declarator obstack lies. */
4099 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4102 cp_parser_declaration_seq_opt (parser);
4104 /* If there are no tokens left then all went well. */
4105 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4107 /* Get rid of the token array; we don't need it any more. */
4108 cp_lexer_destroy (parser->lexer);
4109 parser->lexer = NULL;
4111 /* This file might have been a context that's implicitly extern
4112 "C". If so, pop the lang context. (Only relevant for PCH.) */
4113 if (parser->implicit_extern_c)
4115 pop_lang_context ();
4116 parser->implicit_extern_c = false;
4119 /* Finish up. */
4120 finish_translation_unit ();
4122 success = true;
4124 else
4126 cp_parser_error (parser, "expected declaration");
4127 success = false;
4130 /* Make sure the declarator obstack was fully cleaned up. */
4131 gcc_assert (obstack_next_free (&declarator_obstack)
4132 == declarator_obstack_base);
4134 /* All went well. */
4135 return success;
4138 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4139 decltype context. */
4141 static inline tsubst_flags_t
4142 complain_flags (bool decltype_p)
4144 tsubst_flags_t complain = tf_warning_or_error;
4145 if (decltype_p)
4146 complain |= tf_decltype;
4147 return complain;
4150 /* We're about to parse a collection of statements. If we're currently
4151 parsing tentatively, set up a firewall so that any nested
4152 cp_parser_commit_to_tentative_parse won't affect the current context. */
4154 static cp_token_position
4155 cp_parser_start_tentative_firewall (cp_parser *parser)
4157 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4158 return 0;
4160 cp_parser_parse_tentatively (parser);
4161 cp_parser_commit_to_topmost_tentative_parse (parser);
4162 return cp_lexer_token_position (parser->lexer, false);
4165 /* We've finished parsing the collection of statements. Wrap up the
4166 firewall and replace the relevant tokens with the parsed form. */
4168 static void
4169 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4170 tree expr)
4172 if (!start)
4173 return;
4175 /* Finish the firewall level. */
4176 cp_parser_parse_definitely (parser);
4177 /* And remember the result of the parse for when we try again. */
4178 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4179 token->type = CPP_PREPARSED_EXPR;
4180 token->u.value = expr;
4181 token->keyword = RID_MAX;
4182 cp_lexer_purge_tokens_after (parser->lexer, start);
4185 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4186 enclosing parentheses. */
4188 static tree
4189 cp_parser_statement_expr (cp_parser *parser)
4191 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4193 /* Consume the '('. */
4194 cp_lexer_consume_token (parser->lexer);
4195 /* Start the statement-expression. */
4196 tree expr = begin_stmt_expr ();
4197 /* Parse the compound-statement. */
4198 cp_parser_compound_statement (parser, expr, false, false);
4199 /* Finish up. */
4200 expr = finish_stmt_expr (expr, false);
4201 /* Consume the ')'. */
4202 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4203 cp_parser_skip_to_end_of_statement (parser);
4205 cp_parser_end_tentative_firewall (parser, start, expr);
4206 return expr;
4209 /* Expressions [gram.expr] */
4211 /* Parse a primary-expression.
4213 primary-expression:
4214 literal
4215 this
4216 ( expression )
4217 id-expression
4218 lambda-expression (C++11)
4220 GNU Extensions:
4222 primary-expression:
4223 ( compound-statement )
4224 __builtin_va_arg ( assignment-expression , type-id )
4225 __builtin_offsetof ( type-id , offsetof-expression )
4227 C++ Extensions:
4228 __has_nothrow_assign ( type-id )
4229 __has_nothrow_constructor ( type-id )
4230 __has_nothrow_copy ( type-id )
4231 __has_trivial_assign ( type-id )
4232 __has_trivial_constructor ( type-id )
4233 __has_trivial_copy ( type-id )
4234 __has_trivial_destructor ( type-id )
4235 __has_virtual_destructor ( type-id )
4236 __is_abstract ( type-id )
4237 __is_base_of ( type-id , type-id )
4238 __is_class ( type-id )
4239 __is_empty ( type-id )
4240 __is_enum ( type-id )
4241 __is_final ( type-id )
4242 __is_literal_type ( type-id )
4243 __is_pod ( type-id )
4244 __is_polymorphic ( type-id )
4245 __is_std_layout ( type-id )
4246 __is_trivial ( type-id )
4247 __is_union ( type-id )
4249 Objective-C++ Extension:
4251 primary-expression:
4252 objc-expression
4254 literal:
4255 __null
4257 ADDRESS_P is true iff this expression was immediately preceded by
4258 "&" and therefore might denote a pointer-to-member. CAST_P is true
4259 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4260 true iff this expression is a template argument.
4262 Returns a representation of the expression. Upon return, *IDK
4263 indicates what kind of id-expression (if any) was present. */
4265 static tree
4266 cp_parser_primary_expression (cp_parser *parser,
4267 bool address_p,
4268 bool cast_p,
4269 bool template_arg_p,
4270 bool decltype_p,
4271 cp_id_kind *idk)
4273 cp_token *token = NULL;
4275 /* Assume the primary expression is not an id-expression. */
4276 *idk = CP_ID_KIND_NONE;
4278 /* Peek at the next token. */
4279 token = cp_lexer_peek_token (parser->lexer);
4280 switch ((int) token->type)
4282 /* literal:
4283 integer-literal
4284 character-literal
4285 floating-literal
4286 string-literal
4287 boolean-literal
4288 pointer-literal
4289 user-defined-literal */
4290 case CPP_CHAR:
4291 case CPP_CHAR16:
4292 case CPP_CHAR32:
4293 case CPP_WCHAR:
4294 case CPP_NUMBER:
4295 case CPP_PREPARSED_EXPR:
4296 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4297 return cp_parser_userdef_numeric_literal (parser);
4298 token = cp_lexer_consume_token (parser->lexer);
4299 if (TREE_CODE (token->u.value) == FIXED_CST)
4301 error_at (token->location,
4302 "fixed-point types not supported in C++");
4303 return error_mark_node;
4305 /* Floating-point literals are only allowed in an integral
4306 constant expression if they are cast to an integral or
4307 enumeration type. */
4308 if (TREE_CODE (token->u.value) == REAL_CST
4309 && parser->integral_constant_expression_p
4310 && pedantic)
4312 /* CAST_P will be set even in invalid code like "int(2.7 +
4313 ...)". Therefore, we have to check that the next token
4314 is sure to end the cast. */
4315 if (cast_p)
4317 cp_token *next_token;
4319 next_token = cp_lexer_peek_token (parser->lexer);
4320 if (/* The comma at the end of an
4321 enumerator-definition. */
4322 next_token->type != CPP_COMMA
4323 /* The curly brace at the end of an enum-specifier. */
4324 && next_token->type != CPP_CLOSE_BRACE
4325 /* The end of a statement. */
4326 && next_token->type != CPP_SEMICOLON
4327 /* The end of the cast-expression. */
4328 && next_token->type != CPP_CLOSE_PAREN
4329 /* The end of an array bound. */
4330 && next_token->type != CPP_CLOSE_SQUARE
4331 /* The closing ">" in a template-argument-list. */
4332 && (next_token->type != CPP_GREATER
4333 || parser->greater_than_is_operator_p)
4334 /* C++0x only: A ">>" treated like two ">" tokens,
4335 in a template-argument-list. */
4336 && (next_token->type != CPP_RSHIFT
4337 || (cxx_dialect == cxx98)
4338 || parser->greater_than_is_operator_p))
4339 cast_p = false;
4342 /* If we are within a cast, then the constraint that the
4343 cast is to an integral or enumeration type will be
4344 checked at that point. If we are not within a cast, then
4345 this code is invalid. */
4346 if (!cast_p)
4347 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4349 return token->u.value;
4351 case CPP_CHAR_USERDEF:
4352 case CPP_CHAR16_USERDEF:
4353 case CPP_CHAR32_USERDEF:
4354 case CPP_WCHAR_USERDEF:
4355 return cp_parser_userdef_char_literal (parser);
4357 case CPP_STRING:
4358 case CPP_STRING16:
4359 case CPP_STRING32:
4360 case CPP_WSTRING:
4361 case CPP_UTF8STRING:
4362 case CPP_STRING_USERDEF:
4363 case CPP_STRING16_USERDEF:
4364 case CPP_STRING32_USERDEF:
4365 case CPP_WSTRING_USERDEF:
4366 case CPP_UTF8STRING_USERDEF:
4367 /* ??? Should wide strings be allowed when parser->translate_strings_p
4368 is false (i.e. in attributes)? If not, we can kill the third
4369 argument to cp_parser_string_literal. */
4370 return cp_parser_string_literal (parser,
4371 parser->translate_strings_p,
4372 true);
4374 case CPP_OPEN_PAREN:
4375 /* If we see `( { ' then we are looking at the beginning of
4376 a GNU statement-expression. */
4377 if (cp_parser_allow_gnu_extensions_p (parser)
4378 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4380 /* Statement-expressions are not allowed by the standard. */
4381 pedwarn (token->location, OPT_Wpedantic,
4382 "ISO C++ forbids braced-groups within expressions");
4384 /* And they're not allowed outside of a function-body; you
4385 cannot, for example, write:
4387 int i = ({ int j = 3; j + 1; });
4389 at class or namespace scope. */
4390 if (!parser->in_function_body
4391 || parser->in_template_argument_list_p)
4393 error_at (token->location,
4394 "statement-expressions are not allowed outside "
4395 "functions nor in template-argument lists");
4396 cp_parser_skip_to_end_of_block_or_statement (parser);
4397 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4398 cp_lexer_consume_token (parser->lexer);
4399 return error_mark_node;
4401 else
4402 return cp_parser_statement_expr (parser);
4404 /* Otherwise it's a normal parenthesized expression. */
4406 tree expr;
4407 bool saved_greater_than_is_operator_p;
4409 /* Consume the `('. */
4410 cp_lexer_consume_token (parser->lexer);
4411 /* Within a parenthesized expression, a `>' token is always
4412 the greater-than operator. */
4413 saved_greater_than_is_operator_p
4414 = parser->greater_than_is_operator_p;
4415 parser->greater_than_is_operator_p = true;
4417 /* Parse the parenthesized expression. */
4418 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4419 /* Let the front end know that this expression was
4420 enclosed in parentheses. This matters in case, for
4421 example, the expression is of the form `A::B', since
4422 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4423 not. */
4424 expr = finish_parenthesized_expr (expr);
4425 /* DR 705: Wrapping an unqualified name in parentheses
4426 suppresses arg-dependent lookup. We want to pass back
4427 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4428 (c++/37862), but none of the others. */
4429 if (*idk != CP_ID_KIND_QUALIFIED)
4430 *idk = CP_ID_KIND_NONE;
4432 /* The `>' token might be the end of a template-id or
4433 template-parameter-list now. */
4434 parser->greater_than_is_operator_p
4435 = saved_greater_than_is_operator_p;
4436 /* Consume the `)'. */
4437 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4438 cp_parser_skip_to_end_of_statement (parser);
4440 return expr;
4443 case CPP_OPEN_SQUARE:
4444 if (c_dialect_objc ())
4445 /* We have an Objective-C++ message. */
4446 return cp_parser_objc_expression (parser);
4448 tree lam = cp_parser_lambda_expression (parser);
4449 /* Don't warn about a failed tentative parse. */
4450 if (cp_parser_error_occurred (parser))
4451 return error_mark_node;
4452 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4453 return lam;
4456 case CPP_OBJC_STRING:
4457 if (c_dialect_objc ())
4458 /* We have an Objective-C++ string literal. */
4459 return cp_parser_objc_expression (parser);
4460 cp_parser_error (parser, "expected primary-expression");
4461 return error_mark_node;
4463 case CPP_KEYWORD:
4464 switch (token->keyword)
4466 /* These two are the boolean literals. */
4467 case RID_TRUE:
4468 cp_lexer_consume_token (parser->lexer);
4469 return boolean_true_node;
4470 case RID_FALSE:
4471 cp_lexer_consume_token (parser->lexer);
4472 return boolean_false_node;
4474 /* The `__null' literal. */
4475 case RID_NULL:
4476 cp_lexer_consume_token (parser->lexer);
4477 return null_node;
4479 /* The `nullptr' literal. */
4480 case RID_NULLPTR:
4481 cp_lexer_consume_token (parser->lexer);
4482 return nullptr_node;
4484 /* Recognize the `this' keyword. */
4485 case RID_THIS:
4486 cp_lexer_consume_token (parser->lexer);
4487 if (parser->local_variables_forbidden_p)
4489 error_at (token->location,
4490 "%<this%> may not be used in this context");
4491 return error_mark_node;
4493 /* Pointers cannot appear in constant-expressions. */
4494 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4495 return error_mark_node;
4496 return finish_this_expr ();
4498 /* The `operator' keyword can be the beginning of an
4499 id-expression. */
4500 case RID_OPERATOR:
4501 goto id_expression;
4503 case RID_FUNCTION_NAME:
4504 case RID_PRETTY_FUNCTION_NAME:
4505 case RID_C99_FUNCTION_NAME:
4507 non_integral_constant name;
4509 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4510 __func__ are the names of variables -- but they are
4511 treated specially. Therefore, they are handled here,
4512 rather than relying on the generic id-expression logic
4513 below. Grammatically, these names are id-expressions.
4515 Consume the token. */
4516 token = cp_lexer_consume_token (parser->lexer);
4518 switch (token->keyword)
4520 case RID_FUNCTION_NAME:
4521 name = NIC_FUNC_NAME;
4522 break;
4523 case RID_PRETTY_FUNCTION_NAME:
4524 name = NIC_PRETTY_FUNC;
4525 break;
4526 case RID_C99_FUNCTION_NAME:
4527 name = NIC_C99_FUNC;
4528 break;
4529 default:
4530 gcc_unreachable ();
4533 if (cp_parser_non_integral_constant_expression (parser, name))
4534 return error_mark_node;
4536 /* Look up the name. */
4537 return finish_fname (token->u.value);
4540 case RID_VA_ARG:
4542 tree expression;
4543 tree type;
4544 source_location type_location;
4546 /* The `__builtin_va_arg' construct is used to handle
4547 `va_arg'. Consume the `__builtin_va_arg' token. */
4548 cp_lexer_consume_token (parser->lexer);
4549 /* Look for the opening `('. */
4550 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4551 /* Now, parse the assignment-expression. */
4552 expression = cp_parser_assignment_expression (parser);
4553 /* Look for the `,'. */
4554 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4555 type_location = cp_lexer_peek_token (parser->lexer)->location;
4556 /* Parse the type-id. */
4557 type = cp_parser_type_id (parser);
4558 /* Look for the closing `)'. */
4559 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4560 /* Using `va_arg' in a constant-expression is not
4561 allowed. */
4562 if (cp_parser_non_integral_constant_expression (parser,
4563 NIC_VA_ARG))
4564 return error_mark_node;
4565 return build_x_va_arg (type_location, expression, type);
4568 case RID_OFFSETOF:
4569 return cp_parser_builtin_offsetof (parser);
4571 case RID_HAS_NOTHROW_ASSIGN:
4572 case RID_HAS_NOTHROW_CONSTRUCTOR:
4573 case RID_HAS_NOTHROW_COPY:
4574 case RID_HAS_TRIVIAL_ASSIGN:
4575 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4576 case RID_HAS_TRIVIAL_COPY:
4577 case RID_HAS_TRIVIAL_DESTRUCTOR:
4578 case RID_HAS_VIRTUAL_DESTRUCTOR:
4579 case RID_IS_ABSTRACT:
4580 case RID_IS_BASE_OF:
4581 case RID_IS_CLASS:
4582 case RID_IS_EMPTY:
4583 case RID_IS_ENUM:
4584 case RID_IS_FINAL:
4585 case RID_IS_LITERAL_TYPE:
4586 case RID_IS_POD:
4587 case RID_IS_POLYMORPHIC:
4588 case RID_IS_STD_LAYOUT:
4589 case RID_IS_TRIVIAL:
4590 case RID_IS_TRIVIALLY_ASSIGNABLE:
4591 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4592 case RID_IS_TRIVIALLY_COPYABLE:
4593 case RID_IS_UNION:
4594 return cp_parser_trait_expr (parser, token->keyword);
4596 /* Objective-C++ expressions. */
4597 case RID_AT_ENCODE:
4598 case RID_AT_PROTOCOL:
4599 case RID_AT_SELECTOR:
4600 return cp_parser_objc_expression (parser);
4602 case RID_TEMPLATE:
4603 if (parser->in_function_body
4604 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4605 == CPP_LESS))
4607 error_at (token->location,
4608 "a template declaration cannot appear at block scope");
4609 cp_parser_skip_to_end_of_block_or_statement (parser);
4610 return error_mark_node;
4612 default:
4613 cp_parser_error (parser, "expected primary-expression");
4614 return error_mark_node;
4617 /* An id-expression can start with either an identifier, a
4618 `::' as the beginning of a qualified-id, or the "operator"
4619 keyword. */
4620 case CPP_NAME:
4621 case CPP_SCOPE:
4622 case CPP_TEMPLATE_ID:
4623 case CPP_NESTED_NAME_SPECIFIER:
4625 tree id_expression;
4626 tree decl;
4627 const char *error_msg;
4628 bool template_p;
4629 bool done;
4630 cp_token *id_expr_token;
4632 id_expression:
4633 /* Parse the id-expression. */
4634 id_expression
4635 = cp_parser_id_expression (parser,
4636 /*template_keyword_p=*/false,
4637 /*check_dependency_p=*/true,
4638 &template_p,
4639 /*declarator_p=*/false,
4640 /*optional_p=*/false);
4641 if (id_expression == error_mark_node)
4642 return error_mark_node;
4643 id_expr_token = token;
4644 token = cp_lexer_peek_token (parser->lexer);
4645 done = (token->type != CPP_OPEN_SQUARE
4646 && token->type != CPP_OPEN_PAREN
4647 && token->type != CPP_DOT
4648 && token->type != CPP_DEREF
4649 && token->type != CPP_PLUS_PLUS
4650 && token->type != CPP_MINUS_MINUS);
4651 /* If we have a template-id, then no further lookup is
4652 required. If the template-id was for a template-class, we
4653 will sometimes have a TYPE_DECL at this point. */
4654 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4655 || TREE_CODE (id_expression) == TYPE_DECL)
4656 decl = id_expression;
4657 /* Look up the name. */
4658 else
4660 tree ambiguous_decls;
4662 /* If we already know that this lookup is ambiguous, then
4663 we've already issued an error message; there's no reason
4664 to check again. */
4665 if (id_expr_token->type == CPP_NAME
4666 && id_expr_token->error_reported)
4668 cp_parser_simulate_error (parser);
4669 return error_mark_node;
4672 decl = cp_parser_lookup_name (parser, id_expression,
4673 none_type,
4674 template_p,
4675 /*is_namespace=*/false,
4676 /*check_dependency=*/true,
4677 &ambiguous_decls,
4678 id_expr_token->location);
4679 /* If the lookup was ambiguous, an error will already have
4680 been issued. */
4681 if (ambiguous_decls)
4682 return error_mark_node;
4684 /* In Objective-C++, we may have an Objective-C 2.0
4685 dot-syntax for classes here. */
4686 if (c_dialect_objc ()
4687 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4688 && TREE_CODE (decl) == TYPE_DECL
4689 && objc_is_class_name (decl))
4691 tree component;
4692 cp_lexer_consume_token (parser->lexer);
4693 component = cp_parser_identifier (parser);
4694 if (component == error_mark_node)
4695 return error_mark_node;
4697 return objc_build_class_component_ref (id_expression, component);
4700 /* In Objective-C++, an instance variable (ivar) may be preferred
4701 to whatever cp_parser_lookup_name() found. */
4702 decl = objc_lookup_ivar (decl, id_expression);
4704 /* If name lookup gives us a SCOPE_REF, then the
4705 qualifying scope was dependent. */
4706 if (TREE_CODE (decl) == SCOPE_REF)
4708 /* At this point, we do not know if DECL is a valid
4709 integral constant expression. We assume that it is
4710 in fact such an expression, so that code like:
4712 template <int N> struct A {
4713 int a[B<N>::i];
4716 is accepted. At template-instantiation time, we
4717 will check that B<N>::i is actually a constant. */
4718 return decl;
4720 /* Check to see if DECL is a local variable in a context
4721 where that is forbidden. */
4722 if (parser->local_variables_forbidden_p
4723 && local_variable_p (decl))
4725 /* It might be that we only found DECL because we are
4726 trying to be generous with pre-ISO scoping rules.
4727 For example, consider:
4729 int i;
4730 void g() {
4731 for (int i = 0; i < 10; ++i) {}
4732 extern void f(int j = i);
4735 Here, name look up will originally find the out
4736 of scope `i'. We need to issue a warning message,
4737 but then use the global `i'. */
4738 decl = check_for_out_of_scope_variable (decl);
4739 if (local_variable_p (decl))
4741 error_at (id_expr_token->location,
4742 "local variable %qD may not appear in this context",
4743 decl);
4744 return error_mark_node;
4749 decl = (finish_id_expression
4750 (id_expression, decl, parser->scope,
4751 idk,
4752 parser->integral_constant_expression_p,
4753 parser->allow_non_integral_constant_expression_p,
4754 &parser->non_integral_constant_expression_p,
4755 template_p, done, address_p,
4756 template_arg_p,
4757 &error_msg,
4758 id_expr_token->location));
4759 if (error_msg)
4760 cp_parser_error (parser, error_msg);
4761 return decl;
4764 /* Anything else is an error. */
4765 default:
4766 cp_parser_error (parser, "expected primary-expression");
4767 return error_mark_node;
4771 static inline tree
4772 cp_parser_primary_expression (cp_parser *parser,
4773 bool address_p,
4774 bool cast_p,
4775 bool template_arg_p,
4776 cp_id_kind *idk)
4778 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4779 /*decltype*/false, idk);
4782 /* Parse an id-expression.
4784 id-expression:
4785 unqualified-id
4786 qualified-id
4788 qualified-id:
4789 :: [opt] nested-name-specifier template [opt] unqualified-id
4790 :: identifier
4791 :: operator-function-id
4792 :: template-id
4794 Return a representation of the unqualified portion of the
4795 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4796 a `::' or nested-name-specifier.
4798 Often, if the id-expression was a qualified-id, the caller will
4799 want to make a SCOPE_REF to represent the qualified-id. This
4800 function does not do this in order to avoid wastefully creating
4801 SCOPE_REFs when they are not required.
4803 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4804 `template' keyword.
4806 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4807 uninstantiated templates.
4809 If *TEMPLATE_P is non-NULL, it is set to true iff the
4810 `template' keyword is used to explicitly indicate that the entity
4811 named is a template.
4813 If DECLARATOR_P is true, the id-expression is appearing as part of
4814 a declarator, rather than as part of an expression. */
4816 static tree
4817 cp_parser_id_expression (cp_parser *parser,
4818 bool template_keyword_p,
4819 bool check_dependency_p,
4820 bool *template_p,
4821 bool declarator_p,
4822 bool optional_p)
4824 bool global_scope_p;
4825 bool nested_name_specifier_p;
4827 /* Assume the `template' keyword was not used. */
4828 if (template_p)
4829 *template_p = template_keyword_p;
4831 /* Look for the optional `::' operator. */
4832 global_scope_p
4833 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4834 != NULL_TREE);
4835 /* Look for the optional nested-name-specifier. */
4836 nested_name_specifier_p
4837 = (cp_parser_nested_name_specifier_opt (parser,
4838 /*typename_keyword_p=*/false,
4839 check_dependency_p,
4840 /*type_p=*/false,
4841 declarator_p)
4842 != NULL_TREE);
4843 /* If there is a nested-name-specifier, then we are looking at
4844 the first qualified-id production. */
4845 if (nested_name_specifier_p)
4847 tree saved_scope;
4848 tree saved_object_scope;
4849 tree saved_qualifying_scope;
4850 tree unqualified_id;
4851 bool is_template;
4853 /* See if the next token is the `template' keyword. */
4854 if (!template_p)
4855 template_p = &is_template;
4856 *template_p = cp_parser_optional_template_keyword (parser);
4857 /* Name lookup we do during the processing of the
4858 unqualified-id might obliterate SCOPE. */
4859 saved_scope = parser->scope;
4860 saved_object_scope = parser->object_scope;
4861 saved_qualifying_scope = parser->qualifying_scope;
4862 /* Process the final unqualified-id. */
4863 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4864 check_dependency_p,
4865 declarator_p,
4866 /*optional_p=*/false);
4867 /* Restore the SAVED_SCOPE for our caller. */
4868 parser->scope = saved_scope;
4869 parser->object_scope = saved_object_scope;
4870 parser->qualifying_scope = saved_qualifying_scope;
4872 return unqualified_id;
4874 /* Otherwise, if we are in global scope, then we are looking at one
4875 of the other qualified-id productions. */
4876 else if (global_scope_p)
4878 cp_token *token;
4879 tree id;
4881 /* Peek at the next token. */
4882 token = cp_lexer_peek_token (parser->lexer);
4884 /* If it's an identifier, and the next token is not a "<", then
4885 we can avoid the template-id case. This is an optimization
4886 for this common case. */
4887 if (token->type == CPP_NAME
4888 && !cp_parser_nth_token_starts_template_argument_list_p
4889 (parser, 2))
4890 return cp_parser_identifier (parser);
4892 cp_parser_parse_tentatively (parser);
4893 /* Try a template-id. */
4894 id = cp_parser_template_id (parser,
4895 /*template_keyword_p=*/false,
4896 /*check_dependency_p=*/true,
4897 none_type,
4898 declarator_p);
4899 /* If that worked, we're done. */
4900 if (cp_parser_parse_definitely (parser))
4901 return id;
4903 /* Peek at the next token. (Changes in the token buffer may
4904 have invalidated the pointer obtained above.) */
4905 token = cp_lexer_peek_token (parser->lexer);
4907 switch (token->type)
4909 case CPP_NAME:
4910 return cp_parser_identifier (parser);
4912 case CPP_KEYWORD:
4913 if (token->keyword == RID_OPERATOR)
4914 return cp_parser_operator_function_id (parser);
4915 /* Fall through. */
4917 default:
4918 cp_parser_error (parser, "expected id-expression");
4919 return error_mark_node;
4922 else
4923 return cp_parser_unqualified_id (parser, template_keyword_p,
4924 /*check_dependency_p=*/true,
4925 declarator_p,
4926 optional_p);
4929 /* Parse an unqualified-id.
4931 unqualified-id:
4932 identifier
4933 operator-function-id
4934 conversion-function-id
4935 ~ class-name
4936 template-id
4938 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4939 keyword, in a construct like `A::template ...'.
4941 Returns a representation of unqualified-id. For the `identifier'
4942 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4943 production a BIT_NOT_EXPR is returned; the operand of the
4944 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4945 other productions, see the documentation accompanying the
4946 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4947 names are looked up in uninstantiated templates. If DECLARATOR_P
4948 is true, the unqualified-id is appearing as part of a declarator,
4949 rather than as part of an expression. */
4951 static tree
4952 cp_parser_unqualified_id (cp_parser* parser,
4953 bool template_keyword_p,
4954 bool check_dependency_p,
4955 bool declarator_p,
4956 bool optional_p)
4958 cp_token *token;
4960 /* Peek at the next token. */
4961 token = cp_lexer_peek_token (parser->lexer);
4963 switch ((int) token->type)
4965 case CPP_NAME:
4967 tree id;
4969 /* We don't know yet whether or not this will be a
4970 template-id. */
4971 cp_parser_parse_tentatively (parser);
4972 /* Try a template-id. */
4973 id = cp_parser_template_id (parser, template_keyword_p,
4974 check_dependency_p,
4975 none_type,
4976 declarator_p);
4977 /* If it worked, we're done. */
4978 if (cp_parser_parse_definitely (parser))
4979 return id;
4980 /* Otherwise, it's an ordinary identifier. */
4981 return cp_parser_identifier (parser);
4984 case CPP_TEMPLATE_ID:
4985 return cp_parser_template_id (parser, template_keyword_p,
4986 check_dependency_p,
4987 none_type,
4988 declarator_p);
4990 case CPP_COMPL:
4992 tree type_decl;
4993 tree qualifying_scope;
4994 tree object_scope;
4995 tree scope;
4996 bool done;
4998 /* Consume the `~' token. */
4999 cp_lexer_consume_token (parser->lexer);
5000 /* Parse the class-name. The standard, as written, seems to
5001 say that:
5003 template <typename T> struct S { ~S (); };
5004 template <typename T> S<T>::~S() {}
5006 is invalid, since `~' must be followed by a class-name, but
5007 `S<T>' is dependent, and so not known to be a class.
5008 That's not right; we need to look in uninstantiated
5009 templates. A further complication arises from:
5011 template <typename T> void f(T t) {
5012 t.T::~T();
5015 Here, it is not possible to look up `T' in the scope of `T'
5016 itself. We must look in both the current scope, and the
5017 scope of the containing complete expression.
5019 Yet another issue is:
5021 struct S {
5022 int S;
5023 ~S();
5026 S::~S() {}
5028 The standard does not seem to say that the `S' in `~S'
5029 should refer to the type `S' and not the data member
5030 `S::S'. */
5032 /* DR 244 says that we look up the name after the "~" in the
5033 same scope as we looked up the qualifying name. That idea
5034 isn't fully worked out; it's more complicated than that. */
5035 scope = parser->scope;
5036 object_scope = parser->object_scope;
5037 qualifying_scope = parser->qualifying_scope;
5039 /* Check for invalid scopes. */
5040 if (scope == error_mark_node)
5042 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5043 cp_lexer_consume_token (parser->lexer);
5044 return error_mark_node;
5046 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5048 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5049 error_at (token->location,
5050 "scope %qT before %<~%> is not a class-name",
5051 scope);
5052 cp_parser_simulate_error (parser);
5053 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5054 cp_lexer_consume_token (parser->lexer);
5055 return error_mark_node;
5057 gcc_assert (!scope || TYPE_P (scope));
5059 /* If the name is of the form "X::~X" it's OK even if X is a
5060 typedef. */
5061 token = cp_lexer_peek_token (parser->lexer);
5062 if (scope
5063 && token->type == CPP_NAME
5064 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5065 != CPP_LESS)
5066 && (token->u.value == TYPE_IDENTIFIER (scope)
5067 || (CLASS_TYPE_P (scope)
5068 && constructor_name_p (token->u.value, scope))))
5070 cp_lexer_consume_token (parser->lexer);
5071 return build_nt (BIT_NOT_EXPR, scope);
5074 /* ~auto means the destructor of whatever the object is. */
5075 if (cp_parser_is_keyword (token, RID_AUTO))
5077 if (cxx_dialect < cxx14)
5078 pedwarn (input_location, 0,
5079 "%<~auto%> only available with "
5080 "-std=c++14 or -std=gnu++14");
5081 cp_lexer_consume_token (parser->lexer);
5082 return build_nt (BIT_NOT_EXPR, make_auto ());
5085 /* If there was an explicit qualification (S::~T), first look
5086 in the scope given by the qualification (i.e., S).
5088 Note: in the calls to cp_parser_class_name below we pass
5089 typename_type so that lookup finds the injected-class-name
5090 rather than the constructor. */
5091 done = false;
5092 type_decl = NULL_TREE;
5093 if (scope)
5095 cp_parser_parse_tentatively (parser);
5096 type_decl = cp_parser_class_name (parser,
5097 /*typename_keyword_p=*/false,
5098 /*template_keyword_p=*/false,
5099 typename_type,
5100 /*check_dependency=*/false,
5101 /*class_head_p=*/false,
5102 declarator_p);
5103 if (cp_parser_parse_definitely (parser))
5104 done = true;
5106 /* In "N::S::~S", look in "N" as well. */
5107 if (!done && scope && qualifying_scope)
5109 cp_parser_parse_tentatively (parser);
5110 parser->scope = qualifying_scope;
5111 parser->object_scope = NULL_TREE;
5112 parser->qualifying_scope = NULL_TREE;
5113 type_decl
5114 = cp_parser_class_name (parser,
5115 /*typename_keyword_p=*/false,
5116 /*template_keyword_p=*/false,
5117 typename_type,
5118 /*check_dependency=*/false,
5119 /*class_head_p=*/false,
5120 declarator_p);
5121 if (cp_parser_parse_definitely (parser))
5122 done = true;
5124 /* In "p->S::~T", look in the scope given by "*p" as well. */
5125 else if (!done && object_scope)
5127 cp_parser_parse_tentatively (parser);
5128 parser->scope = object_scope;
5129 parser->object_scope = NULL_TREE;
5130 parser->qualifying_scope = NULL_TREE;
5131 type_decl
5132 = cp_parser_class_name (parser,
5133 /*typename_keyword_p=*/false,
5134 /*template_keyword_p=*/false,
5135 typename_type,
5136 /*check_dependency=*/false,
5137 /*class_head_p=*/false,
5138 declarator_p);
5139 if (cp_parser_parse_definitely (parser))
5140 done = true;
5142 /* Look in the surrounding context. */
5143 if (!done)
5145 parser->scope = NULL_TREE;
5146 parser->object_scope = NULL_TREE;
5147 parser->qualifying_scope = NULL_TREE;
5148 if (processing_template_decl)
5149 cp_parser_parse_tentatively (parser);
5150 type_decl
5151 = cp_parser_class_name (parser,
5152 /*typename_keyword_p=*/false,
5153 /*template_keyword_p=*/false,
5154 typename_type,
5155 /*check_dependency=*/false,
5156 /*class_head_p=*/false,
5157 declarator_p);
5158 if (processing_template_decl
5159 && ! cp_parser_parse_definitely (parser))
5161 /* We couldn't find a type with this name, so just accept
5162 it and check for a match at instantiation time. */
5163 type_decl = cp_parser_identifier (parser);
5164 if (type_decl != error_mark_node)
5165 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5166 return type_decl;
5169 /* If an error occurred, assume that the name of the
5170 destructor is the same as the name of the qualifying
5171 class. That allows us to keep parsing after running
5172 into ill-formed destructor names. */
5173 if (type_decl == error_mark_node && scope)
5174 return build_nt (BIT_NOT_EXPR, scope);
5175 else if (type_decl == error_mark_node)
5176 return error_mark_node;
5178 /* Check that destructor name and scope match. */
5179 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5181 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5182 error_at (token->location,
5183 "declaration of %<~%T%> as member of %qT",
5184 type_decl, scope);
5185 cp_parser_simulate_error (parser);
5186 return error_mark_node;
5189 /* [class.dtor]
5191 A typedef-name that names a class shall not be used as the
5192 identifier in the declarator for a destructor declaration. */
5193 if (declarator_p
5194 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5195 && !DECL_SELF_REFERENCE_P (type_decl)
5196 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5197 error_at (token->location,
5198 "typedef-name %qD used as destructor declarator",
5199 type_decl);
5201 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5204 case CPP_KEYWORD:
5205 if (token->keyword == RID_OPERATOR)
5207 tree id;
5209 /* This could be a template-id, so we try that first. */
5210 cp_parser_parse_tentatively (parser);
5211 /* Try a template-id. */
5212 id = cp_parser_template_id (parser, template_keyword_p,
5213 /*check_dependency_p=*/true,
5214 none_type,
5215 declarator_p);
5216 /* If that worked, we're done. */
5217 if (cp_parser_parse_definitely (parser))
5218 return id;
5219 /* We still don't know whether we're looking at an
5220 operator-function-id or a conversion-function-id. */
5221 cp_parser_parse_tentatively (parser);
5222 /* Try an operator-function-id. */
5223 id = cp_parser_operator_function_id (parser);
5224 /* If that didn't work, try a conversion-function-id. */
5225 if (!cp_parser_parse_definitely (parser))
5226 id = cp_parser_conversion_function_id (parser);
5227 else if (UDLIT_OPER_P (id))
5229 /* 17.6.3.3.5 */
5230 const char *name = UDLIT_OP_SUFFIX (id);
5231 if (name[0] != '_' && !in_system_header_at (input_location)
5232 && declarator_p)
5233 warning (0, "literal operator suffixes not preceded by %<_%>"
5234 " are reserved for future standardization");
5237 return id;
5239 /* Fall through. */
5241 default:
5242 if (optional_p)
5243 return NULL_TREE;
5244 cp_parser_error (parser, "expected unqualified-id");
5245 return error_mark_node;
5249 /* Parse an (optional) nested-name-specifier.
5251 nested-name-specifier: [C++98]
5252 class-or-namespace-name :: nested-name-specifier [opt]
5253 class-or-namespace-name :: template nested-name-specifier [opt]
5255 nested-name-specifier: [C++0x]
5256 type-name ::
5257 namespace-name ::
5258 nested-name-specifier identifier ::
5259 nested-name-specifier template [opt] simple-template-id ::
5261 PARSER->SCOPE should be set appropriately before this function is
5262 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5263 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5264 in name lookups.
5266 Sets PARSER->SCOPE to the class (TYPE) or namespace
5267 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5268 it unchanged if there is no nested-name-specifier. Returns the new
5269 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5271 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5272 part of a declaration and/or decl-specifier. */
5274 static tree
5275 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5276 bool typename_keyword_p,
5277 bool check_dependency_p,
5278 bool type_p,
5279 bool is_declaration)
5281 bool success = false;
5282 cp_token_position start = 0;
5283 cp_token *token;
5285 /* Remember where the nested-name-specifier starts. */
5286 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5288 start = cp_lexer_token_position (parser->lexer, false);
5289 push_deferring_access_checks (dk_deferred);
5292 while (true)
5294 tree new_scope;
5295 tree old_scope;
5296 tree saved_qualifying_scope;
5297 bool template_keyword_p;
5299 /* Spot cases that cannot be the beginning of a
5300 nested-name-specifier. */
5301 token = cp_lexer_peek_token (parser->lexer);
5303 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5304 the already parsed nested-name-specifier. */
5305 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5307 /* Grab the nested-name-specifier and continue the loop. */
5308 cp_parser_pre_parsed_nested_name_specifier (parser);
5309 /* If we originally encountered this nested-name-specifier
5310 with IS_DECLARATION set to false, we will not have
5311 resolved TYPENAME_TYPEs, so we must do so here. */
5312 if (is_declaration
5313 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5315 new_scope = resolve_typename_type (parser->scope,
5316 /*only_current_p=*/false);
5317 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5318 parser->scope = new_scope;
5320 success = true;
5321 continue;
5324 /* Spot cases that cannot be the beginning of a
5325 nested-name-specifier. On the second and subsequent times
5326 through the loop, we look for the `template' keyword. */
5327 if (success && token->keyword == RID_TEMPLATE)
5329 /* A template-id can start a nested-name-specifier. */
5330 else if (token->type == CPP_TEMPLATE_ID)
5332 /* DR 743: decltype can be used in a nested-name-specifier. */
5333 else if (token_is_decltype (token))
5335 else
5337 /* If the next token is not an identifier, then it is
5338 definitely not a type-name or namespace-name. */
5339 if (token->type != CPP_NAME)
5340 break;
5341 /* If the following token is neither a `<' (to begin a
5342 template-id), nor a `::', then we are not looking at a
5343 nested-name-specifier. */
5344 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5346 if (token->type == CPP_COLON
5347 && parser->colon_corrects_to_scope_p
5348 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5350 error_at (token->location,
5351 "found %<:%> in nested-name-specifier, expected %<::%>");
5352 token->type = CPP_SCOPE;
5355 if (token->type != CPP_SCOPE
5356 && !cp_parser_nth_token_starts_template_argument_list_p
5357 (parser, 2))
5358 break;
5361 /* The nested-name-specifier is optional, so we parse
5362 tentatively. */
5363 cp_parser_parse_tentatively (parser);
5365 /* Look for the optional `template' keyword, if this isn't the
5366 first time through the loop. */
5367 if (success)
5368 template_keyword_p = cp_parser_optional_template_keyword (parser);
5369 else
5370 template_keyword_p = false;
5372 /* Save the old scope since the name lookup we are about to do
5373 might destroy it. */
5374 old_scope = parser->scope;
5375 saved_qualifying_scope = parser->qualifying_scope;
5376 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5377 look up names in "X<T>::I" in order to determine that "Y" is
5378 a template. So, if we have a typename at this point, we make
5379 an effort to look through it. */
5380 if (is_declaration
5381 && !typename_keyword_p
5382 && parser->scope
5383 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5384 parser->scope = resolve_typename_type (parser->scope,
5385 /*only_current_p=*/false);
5386 /* Parse the qualifying entity. */
5387 new_scope
5388 = cp_parser_qualifying_entity (parser,
5389 typename_keyword_p,
5390 template_keyword_p,
5391 check_dependency_p,
5392 type_p,
5393 is_declaration);
5394 /* Look for the `::' token. */
5395 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5397 /* If we found what we wanted, we keep going; otherwise, we're
5398 done. */
5399 if (!cp_parser_parse_definitely (parser))
5401 bool error_p = false;
5403 /* Restore the OLD_SCOPE since it was valid before the
5404 failed attempt at finding the last
5405 class-or-namespace-name. */
5406 parser->scope = old_scope;
5407 parser->qualifying_scope = saved_qualifying_scope;
5409 /* If the next token is a decltype, and the one after that is a
5410 `::', then the decltype has failed to resolve to a class or
5411 enumeration type. Give this error even when parsing
5412 tentatively since it can't possibly be valid--and we're going
5413 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5414 won't get another chance.*/
5415 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5416 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5417 == CPP_SCOPE))
5419 token = cp_lexer_consume_token (parser->lexer);
5420 error_at (token->location, "decltype evaluates to %qT, "
5421 "which is not a class or enumeration type",
5422 token->u.value);
5423 parser->scope = error_mark_node;
5424 error_p = true;
5425 /* As below. */
5426 success = true;
5427 cp_lexer_consume_token (parser->lexer);
5430 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5431 break;
5432 /* If the next token is an identifier, and the one after
5433 that is a `::', then any valid interpretation would have
5434 found a class-or-namespace-name. */
5435 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5436 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5437 == CPP_SCOPE)
5438 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5439 != CPP_COMPL))
5441 token = cp_lexer_consume_token (parser->lexer);
5442 if (!error_p)
5444 if (!token->error_reported)
5446 tree decl;
5447 tree ambiguous_decls;
5449 decl = cp_parser_lookup_name (parser, token->u.value,
5450 none_type,
5451 /*is_template=*/false,
5452 /*is_namespace=*/false,
5453 /*check_dependency=*/true,
5454 &ambiguous_decls,
5455 token->location);
5456 if (TREE_CODE (decl) == TEMPLATE_DECL)
5457 error_at (token->location,
5458 "%qD used without template parameters",
5459 decl);
5460 else if (ambiguous_decls)
5462 // cp_parser_lookup_name has the same diagnostic,
5463 // thus make sure to emit it at most once.
5464 if (cp_parser_uncommitted_to_tentative_parse_p
5465 (parser))
5467 error_at (token->location,
5468 "reference to %qD is ambiguous",
5469 token->u.value);
5470 print_candidates (ambiguous_decls);
5472 decl = error_mark_node;
5474 else
5476 if (cxx_dialect != cxx98)
5477 cp_parser_name_lookup_error
5478 (parser, token->u.value, decl, NLE_NOT_CXX98,
5479 token->location);
5480 else
5481 cp_parser_name_lookup_error
5482 (parser, token->u.value, decl, NLE_CXX98,
5483 token->location);
5486 parser->scope = error_mark_node;
5487 error_p = true;
5488 /* Treat this as a successful nested-name-specifier
5489 due to:
5491 [basic.lookup.qual]
5493 If the name found is not a class-name (clause
5494 _class_) or namespace-name (_namespace.def_), the
5495 program is ill-formed. */
5496 success = true;
5498 cp_lexer_consume_token (parser->lexer);
5500 break;
5502 /* We've found one valid nested-name-specifier. */
5503 success = true;
5504 /* Name lookup always gives us a DECL. */
5505 if (TREE_CODE (new_scope) == TYPE_DECL)
5506 new_scope = TREE_TYPE (new_scope);
5507 /* Uses of "template" must be followed by actual templates. */
5508 if (template_keyword_p
5509 && !(CLASS_TYPE_P (new_scope)
5510 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5511 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5512 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5513 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5514 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5515 == TEMPLATE_ID_EXPR)))
5516 permerror (input_location, TYPE_P (new_scope)
5517 ? G_("%qT is not a template")
5518 : G_("%qD is not a template"),
5519 new_scope);
5520 /* If it is a class scope, try to complete it; we are about to
5521 be looking up names inside the class. */
5522 if (TYPE_P (new_scope)
5523 /* Since checking types for dependency can be expensive,
5524 avoid doing it if the type is already complete. */
5525 && !COMPLETE_TYPE_P (new_scope)
5526 /* Do not try to complete dependent types. */
5527 && !dependent_type_p (new_scope))
5529 new_scope = complete_type (new_scope);
5530 /* If it is a typedef to current class, use the current
5531 class instead, as the typedef won't have any names inside
5532 it yet. */
5533 if (!COMPLETE_TYPE_P (new_scope)
5534 && currently_open_class (new_scope))
5535 new_scope = TYPE_MAIN_VARIANT (new_scope);
5537 /* Make sure we look in the right scope the next time through
5538 the loop. */
5539 parser->scope = new_scope;
5542 /* If parsing tentatively, replace the sequence of tokens that makes
5543 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5544 token. That way, should we re-parse the token stream, we will
5545 not have to repeat the effort required to do the parse, nor will
5546 we issue duplicate error messages. */
5547 if (success && start)
5549 cp_token *token;
5551 token = cp_lexer_token_at (parser->lexer, start);
5552 /* Reset the contents of the START token. */
5553 token->type = CPP_NESTED_NAME_SPECIFIER;
5554 /* Retrieve any deferred checks. Do not pop this access checks yet
5555 so the memory will not be reclaimed during token replacing below. */
5556 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5557 token->u.tree_check_value->value = parser->scope;
5558 token->u.tree_check_value->checks = get_deferred_access_checks ();
5559 token->u.tree_check_value->qualifying_scope =
5560 parser->qualifying_scope;
5561 token->keyword = RID_MAX;
5563 /* Purge all subsequent tokens. */
5564 cp_lexer_purge_tokens_after (parser->lexer, start);
5567 if (start)
5568 pop_to_parent_deferring_access_checks ();
5570 return success ? parser->scope : NULL_TREE;
5573 /* Parse a nested-name-specifier. See
5574 cp_parser_nested_name_specifier_opt for details. This function
5575 behaves identically, except that it will an issue an error if no
5576 nested-name-specifier is present. */
5578 static tree
5579 cp_parser_nested_name_specifier (cp_parser *parser,
5580 bool typename_keyword_p,
5581 bool check_dependency_p,
5582 bool type_p,
5583 bool is_declaration)
5585 tree scope;
5587 /* Look for the nested-name-specifier. */
5588 scope = cp_parser_nested_name_specifier_opt (parser,
5589 typename_keyword_p,
5590 check_dependency_p,
5591 type_p,
5592 is_declaration);
5593 /* If it was not present, issue an error message. */
5594 if (!scope)
5596 cp_parser_error (parser, "expected nested-name-specifier");
5597 parser->scope = NULL_TREE;
5600 return scope;
5603 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5604 this is either a class-name or a namespace-name (which corresponds
5605 to the class-or-namespace-name production in the grammar). For
5606 C++0x, it can also be a type-name that refers to an enumeration
5607 type or a simple-template-id.
5609 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5610 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5611 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5612 TYPE_P is TRUE iff the next name should be taken as a class-name,
5613 even the same name is declared to be another entity in the same
5614 scope.
5616 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5617 specified by the class-or-namespace-name. If neither is found the
5618 ERROR_MARK_NODE is returned. */
5620 static tree
5621 cp_parser_qualifying_entity (cp_parser *parser,
5622 bool typename_keyword_p,
5623 bool template_keyword_p,
5624 bool check_dependency_p,
5625 bool type_p,
5626 bool is_declaration)
5628 tree saved_scope;
5629 tree saved_qualifying_scope;
5630 tree saved_object_scope;
5631 tree scope;
5632 bool only_class_p;
5633 bool successful_parse_p;
5635 /* DR 743: decltype can appear in a nested-name-specifier. */
5636 if (cp_lexer_next_token_is_decltype (parser->lexer))
5638 scope = cp_parser_decltype (parser);
5639 if (TREE_CODE (scope) != ENUMERAL_TYPE
5640 && !MAYBE_CLASS_TYPE_P (scope))
5642 cp_parser_simulate_error (parser);
5643 return error_mark_node;
5645 if (TYPE_NAME (scope))
5646 scope = TYPE_NAME (scope);
5647 return scope;
5650 /* Before we try to parse the class-name, we must save away the
5651 current PARSER->SCOPE since cp_parser_class_name will destroy
5652 it. */
5653 saved_scope = parser->scope;
5654 saved_qualifying_scope = parser->qualifying_scope;
5655 saved_object_scope = parser->object_scope;
5656 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5657 there is no need to look for a namespace-name. */
5658 only_class_p = template_keyword_p
5659 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5660 if (!only_class_p)
5661 cp_parser_parse_tentatively (parser);
5662 scope = cp_parser_class_name (parser,
5663 typename_keyword_p,
5664 template_keyword_p,
5665 type_p ? class_type : none_type,
5666 check_dependency_p,
5667 /*class_head_p=*/false,
5668 is_declaration);
5669 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5670 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5671 if (!only_class_p
5672 && cxx_dialect != cxx98
5673 && !successful_parse_p)
5675 /* Restore the saved scope. */
5676 parser->scope = saved_scope;
5677 parser->qualifying_scope = saved_qualifying_scope;
5678 parser->object_scope = saved_object_scope;
5680 /* Parse tentatively. */
5681 cp_parser_parse_tentatively (parser);
5683 /* Parse a type-name */
5684 scope = cp_parser_type_name (parser);
5686 /* "If the name found does not designate a namespace or a class,
5687 enumeration, or dependent type, the program is ill-formed."
5689 We cover classes and dependent types above and namespaces below,
5690 so this code is only looking for enums. */
5691 if (!scope || TREE_CODE (scope) != TYPE_DECL
5692 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5693 cp_parser_simulate_error (parser);
5695 successful_parse_p = cp_parser_parse_definitely (parser);
5697 /* If that didn't work, try for a namespace-name. */
5698 if (!only_class_p && !successful_parse_p)
5700 /* Restore the saved scope. */
5701 parser->scope = saved_scope;
5702 parser->qualifying_scope = saved_qualifying_scope;
5703 parser->object_scope = saved_object_scope;
5704 /* If we are not looking at an identifier followed by the scope
5705 resolution operator, then this is not part of a
5706 nested-name-specifier. (Note that this function is only used
5707 to parse the components of a nested-name-specifier.) */
5708 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5709 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5710 return error_mark_node;
5711 scope = cp_parser_namespace_name (parser);
5714 return scope;
5717 /* Return true if we are looking at a compound-literal, false otherwise. */
5719 static bool
5720 cp_parser_compound_literal_p (cp_parser *parser)
5722 /* Consume the `('. */
5723 cp_lexer_consume_token (parser->lexer);
5725 cp_lexer_save_tokens (parser->lexer);
5727 /* Skip tokens until the next token is a closing parenthesis.
5728 If we find the closing `)', and the next token is a `{', then
5729 we are looking at a compound-literal. */
5730 bool compound_literal_p
5731 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5732 /*consume_paren=*/true)
5733 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5735 /* Roll back the tokens we skipped. */
5736 cp_lexer_rollback_tokens (parser->lexer);
5738 return compound_literal_p;
5741 /* Parse a postfix-expression.
5743 postfix-expression:
5744 primary-expression
5745 postfix-expression [ expression ]
5746 postfix-expression ( expression-list [opt] )
5747 simple-type-specifier ( expression-list [opt] )
5748 typename :: [opt] nested-name-specifier identifier
5749 ( expression-list [opt] )
5750 typename :: [opt] nested-name-specifier template [opt] template-id
5751 ( expression-list [opt] )
5752 postfix-expression . template [opt] id-expression
5753 postfix-expression -> template [opt] id-expression
5754 postfix-expression . pseudo-destructor-name
5755 postfix-expression -> pseudo-destructor-name
5756 postfix-expression ++
5757 postfix-expression --
5758 dynamic_cast < type-id > ( expression )
5759 static_cast < type-id > ( expression )
5760 reinterpret_cast < type-id > ( expression )
5761 const_cast < type-id > ( expression )
5762 typeid ( expression )
5763 typeid ( type-id )
5765 GNU Extension:
5767 postfix-expression:
5768 ( type-id ) { initializer-list , [opt] }
5770 This extension is a GNU version of the C99 compound-literal
5771 construct. (The C99 grammar uses `type-name' instead of `type-id',
5772 but they are essentially the same concept.)
5774 If ADDRESS_P is true, the postfix expression is the operand of the
5775 `&' operator. CAST_P is true if this expression is the target of a
5776 cast.
5778 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5779 class member access expressions [expr.ref].
5781 Returns a representation of the expression. */
5783 static tree
5784 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5785 bool member_access_only_p, bool decltype_p,
5786 cp_id_kind * pidk_return)
5788 cp_token *token;
5789 location_t loc;
5790 enum rid keyword;
5791 cp_id_kind idk = CP_ID_KIND_NONE;
5792 tree postfix_expression = NULL_TREE;
5793 bool is_member_access = false;
5794 int saved_in_statement = -1;
5796 /* Peek at the next token. */
5797 token = cp_lexer_peek_token (parser->lexer);
5798 loc = token->location;
5799 /* Some of the productions are determined by keywords. */
5800 keyword = token->keyword;
5801 switch (keyword)
5803 case RID_DYNCAST:
5804 case RID_STATCAST:
5805 case RID_REINTCAST:
5806 case RID_CONSTCAST:
5808 tree type;
5809 tree expression;
5810 const char *saved_message;
5811 bool saved_in_type_id_in_expr_p;
5813 /* All of these can be handled in the same way from the point
5814 of view of parsing. Begin by consuming the token
5815 identifying the cast. */
5816 cp_lexer_consume_token (parser->lexer);
5818 /* New types cannot be defined in the cast. */
5819 saved_message = parser->type_definition_forbidden_message;
5820 parser->type_definition_forbidden_message
5821 = G_("types may not be defined in casts");
5823 /* Look for the opening `<'. */
5824 cp_parser_require (parser, CPP_LESS, RT_LESS);
5825 /* Parse the type to which we are casting. */
5826 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5827 parser->in_type_id_in_expr_p = true;
5828 type = cp_parser_type_id (parser);
5829 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5830 /* Look for the closing `>'. */
5831 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5832 /* Restore the old message. */
5833 parser->type_definition_forbidden_message = saved_message;
5835 bool saved_greater_than_is_operator_p
5836 = parser->greater_than_is_operator_p;
5837 parser->greater_than_is_operator_p = true;
5839 /* And the expression which is being cast. */
5840 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5841 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5842 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5844 parser->greater_than_is_operator_p
5845 = saved_greater_than_is_operator_p;
5847 /* Only type conversions to integral or enumeration types
5848 can be used in constant-expressions. */
5849 if (!cast_valid_in_integral_constant_expression_p (type)
5850 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5851 return error_mark_node;
5853 switch (keyword)
5855 case RID_DYNCAST:
5856 postfix_expression
5857 = build_dynamic_cast (type, expression, tf_warning_or_error);
5858 break;
5859 case RID_STATCAST:
5860 postfix_expression
5861 = build_static_cast (type, expression, tf_warning_or_error);
5862 break;
5863 case RID_REINTCAST:
5864 postfix_expression
5865 = build_reinterpret_cast (type, expression,
5866 tf_warning_or_error);
5867 break;
5868 case RID_CONSTCAST:
5869 postfix_expression
5870 = build_const_cast (type, expression, tf_warning_or_error);
5871 break;
5872 default:
5873 gcc_unreachable ();
5876 break;
5878 case RID_TYPEID:
5880 tree type;
5881 const char *saved_message;
5882 bool saved_in_type_id_in_expr_p;
5884 /* Consume the `typeid' token. */
5885 cp_lexer_consume_token (parser->lexer);
5886 /* Look for the `(' token. */
5887 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5888 /* Types cannot be defined in a `typeid' expression. */
5889 saved_message = parser->type_definition_forbidden_message;
5890 parser->type_definition_forbidden_message
5891 = G_("types may not be defined in a %<typeid%> expression");
5892 /* We can't be sure yet whether we're looking at a type-id or an
5893 expression. */
5894 cp_parser_parse_tentatively (parser);
5895 /* Try a type-id first. */
5896 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5897 parser->in_type_id_in_expr_p = true;
5898 type = cp_parser_type_id (parser);
5899 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5900 /* Look for the `)' token. Otherwise, we can't be sure that
5901 we're not looking at an expression: consider `typeid (int
5902 (3))', for example. */
5903 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5904 /* If all went well, simply lookup the type-id. */
5905 if (cp_parser_parse_definitely (parser))
5906 postfix_expression = get_typeid (type, tf_warning_or_error);
5907 /* Otherwise, fall back to the expression variant. */
5908 else
5910 tree expression;
5912 /* Look for an expression. */
5913 expression = cp_parser_expression (parser, & idk);
5914 /* Compute its typeid. */
5915 postfix_expression = build_typeid (expression, tf_warning_or_error);
5916 /* Look for the `)' token. */
5917 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5919 /* Restore the saved message. */
5920 parser->type_definition_forbidden_message = saved_message;
5921 /* `typeid' may not appear in an integral constant expression. */
5922 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5923 return error_mark_node;
5925 break;
5927 case RID_TYPENAME:
5929 tree type;
5930 /* The syntax permitted here is the same permitted for an
5931 elaborated-type-specifier. */
5932 type = cp_parser_elaborated_type_specifier (parser,
5933 /*is_friend=*/false,
5934 /*is_declaration=*/false);
5935 postfix_expression = cp_parser_functional_cast (parser, type);
5937 break;
5939 case RID_CILK_SPAWN:
5941 cp_lexer_consume_token (parser->lexer);
5942 token = cp_lexer_peek_token (parser->lexer);
5943 if (token->type == CPP_SEMICOLON)
5945 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5946 "an expression");
5947 postfix_expression = error_mark_node;
5948 break;
5950 else if (!current_function_decl)
5952 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5953 "inside a function");
5954 postfix_expression = error_mark_node;
5955 break;
5957 else
5959 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5960 saved_in_statement = parser->in_statement;
5961 parser->in_statement |= IN_CILK_SPAWN;
5963 cfun->calls_cilk_spawn = 1;
5964 postfix_expression =
5965 cp_parser_postfix_expression (parser, false, false,
5966 false, false, &idk);
5967 if (!flag_cilkplus)
5969 error_at (token->location, "-fcilkplus must be enabled to use"
5970 " %<_Cilk_spawn%>");
5971 cfun->calls_cilk_spawn = 0;
5973 else if (saved_in_statement & IN_CILK_SPAWN)
5975 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5976 "are not permitted");
5977 postfix_expression = error_mark_node;
5978 cfun->calls_cilk_spawn = 0;
5980 else
5982 postfix_expression = build_cilk_spawn (token->location,
5983 postfix_expression);
5984 if (postfix_expression != error_mark_node)
5985 SET_EXPR_LOCATION (postfix_expression, input_location);
5986 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5988 break;
5991 case RID_BUILTIN_SHUFFLE:
5993 vec<tree, va_gc> *vec;
5994 unsigned int i;
5995 tree p;
5997 cp_lexer_consume_token (parser->lexer);
5998 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5999 /*cast_p=*/false, /*allow_expansion_p=*/true,
6000 /*non_constant_p=*/NULL);
6001 if (vec == NULL)
6002 return error_mark_node;
6004 FOR_EACH_VEC_ELT (*vec, i, p)
6005 mark_exp_read (p);
6007 if (vec->length () == 2)
6008 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6009 tf_warning_or_error);
6010 else if (vec->length () == 3)
6011 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6012 tf_warning_or_error);
6013 else
6015 error_at (loc, "wrong number of arguments to "
6016 "%<__builtin_shuffle%>");
6017 return error_mark_node;
6019 break;
6022 default:
6024 tree type;
6026 /* If the next thing is a simple-type-specifier, we may be
6027 looking at a functional cast. We could also be looking at
6028 an id-expression. So, we try the functional cast, and if
6029 that doesn't work we fall back to the primary-expression. */
6030 cp_parser_parse_tentatively (parser);
6031 /* Look for the simple-type-specifier. */
6032 type = cp_parser_simple_type_specifier (parser,
6033 /*decl_specs=*/NULL,
6034 CP_PARSER_FLAGS_NONE);
6035 /* Parse the cast itself. */
6036 if (!cp_parser_error_occurred (parser))
6037 postfix_expression
6038 = cp_parser_functional_cast (parser, type);
6039 /* If that worked, we're done. */
6040 if (cp_parser_parse_definitely (parser))
6041 break;
6043 /* If the functional-cast didn't work out, try a
6044 compound-literal. */
6045 if (cp_parser_allow_gnu_extensions_p (parser)
6046 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6048 tree initializer = NULL_TREE;
6050 cp_parser_parse_tentatively (parser);
6052 /* Avoid calling cp_parser_type_id pointlessly, see comment
6053 in cp_parser_cast_expression about c++/29234. */
6054 if (!cp_parser_compound_literal_p (parser))
6055 cp_parser_simulate_error (parser);
6056 else
6058 /* Parse the type. */
6059 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6060 parser->in_type_id_in_expr_p = true;
6061 type = cp_parser_type_id (parser);
6062 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6063 /* Look for the `)'. */
6064 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6067 /* If things aren't going well, there's no need to
6068 keep going. */
6069 if (!cp_parser_error_occurred (parser))
6071 bool non_constant_p;
6072 /* Parse the brace-enclosed initializer list. */
6073 initializer = cp_parser_braced_list (parser,
6074 &non_constant_p);
6076 /* If that worked, we're definitely looking at a
6077 compound-literal expression. */
6078 if (cp_parser_parse_definitely (parser))
6080 /* Warn the user that a compound literal is not
6081 allowed in standard C++. */
6082 pedwarn (input_location, OPT_Wpedantic,
6083 "ISO C++ forbids compound-literals");
6084 /* For simplicity, we disallow compound literals in
6085 constant-expressions. We could
6086 allow compound literals of integer type, whose
6087 initializer was a constant, in constant
6088 expressions. Permitting that usage, as a further
6089 extension, would not change the meaning of any
6090 currently accepted programs. (Of course, as
6091 compound literals are not part of ISO C++, the
6092 standard has nothing to say.) */
6093 if (cp_parser_non_integral_constant_expression (parser,
6094 NIC_NCC))
6096 postfix_expression = error_mark_node;
6097 break;
6099 /* Form the representation of the compound-literal. */
6100 postfix_expression
6101 = finish_compound_literal (type, initializer,
6102 tf_warning_or_error);
6103 break;
6107 /* It must be a primary-expression. */
6108 postfix_expression
6109 = cp_parser_primary_expression (parser, address_p, cast_p,
6110 /*template_arg_p=*/false,
6111 decltype_p,
6112 &idk);
6114 break;
6117 /* Note that we don't need to worry about calling build_cplus_new on a
6118 class-valued CALL_EXPR in decltype when it isn't the end of the
6119 postfix-expression; unary_complex_lvalue will take care of that for
6120 all these cases. */
6122 /* Keep looping until the postfix-expression is complete. */
6123 while (true)
6125 if (idk == CP_ID_KIND_UNQUALIFIED
6126 && identifier_p (postfix_expression)
6127 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6128 /* It is not a Koenig lookup function call. */
6129 postfix_expression
6130 = unqualified_name_lookup_error (postfix_expression);
6132 /* Peek at the next token. */
6133 token = cp_lexer_peek_token (parser->lexer);
6135 switch (token->type)
6137 case CPP_OPEN_SQUARE:
6138 if (cp_next_tokens_can_be_std_attribute_p (parser))
6140 cp_parser_error (parser,
6141 "two consecutive %<[%> shall "
6142 "only introduce an attribute");
6143 return error_mark_node;
6145 postfix_expression
6146 = cp_parser_postfix_open_square_expression (parser,
6147 postfix_expression,
6148 false,
6149 decltype_p);
6150 idk = CP_ID_KIND_NONE;
6151 is_member_access = false;
6152 break;
6154 case CPP_OPEN_PAREN:
6155 /* postfix-expression ( expression-list [opt] ) */
6157 bool koenig_p;
6158 bool is_builtin_constant_p;
6159 bool saved_integral_constant_expression_p = false;
6160 bool saved_non_integral_constant_expression_p = false;
6161 tsubst_flags_t complain = complain_flags (decltype_p);
6162 vec<tree, va_gc> *args;
6164 is_member_access = false;
6166 is_builtin_constant_p
6167 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6168 if (is_builtin_constant_p)
6170 /* The whole point of __builtin_constant_p is to allow
6171 non-constant expressions to appear as arguments. */
6172 saved_integral_constant_expression_p
6173 = parser->integral_constant_expression_p;
6174 saved_non_integral_constant_expression_p
6175 = parser->non_integral_constant_expression_p;
6176 parser->integral_constant_expression_p = false;
6178 args = (cp_parser_parenthesized_expression_list
6179 (parser, non_attr,
6180 /*cast_p=*/false, /*allow_expansion_p=*/true,
6181 /*non_constant_p=*/NULL,
6182 /*want_literal_zero_p=*/warn_memset_transposed_args));
6183 if (is_builtin_constant_p)
6185 parser->integral_constant_expression_p
6186 = saved_integral_constant_expression_p;
6187 parser->non_integral_constant_expression_p
6188 = saved_non_integral_constant_expression_p;
6191 if (args == NULL)
6193 postfix_expression = error_mark_node;
6194 break;
6197 /* Function calls are not permitted in
6198 constant-expressions. */
6199 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6200 && cp_parser_non_integral_constant_expression (parser,
6201 NIC_FUNC_CALL))
6203 postfix_expression = error_mark_node;
6204 release_tree_vector (args);
6205 break;
6208 koenig_p = false;
6209 if (idk == CP_ID_KIND_UNQUALIFIED
6210 || idk == CP_ID_KIND_TEMPLATE_ID)
6212 if (identifier_p (postfix_expression))
6214 if (!args->is_empty ())
6216 koenig_p = true;
6217 if (!any_type_dependent_arguments_p (args))
6218 postfix_expression
6219 = perform_koenig_lookup (postfix_expression, args,
6220 complain);
6222 else
6223 postfix_expression
6224 = unqualified_fn_lookup_error (postfix_expression);
6226 /* We do not perform argument-dependent lookup if
6227 normal lookup finds a non-function, in accordance
6228 with the expected resolution of DR 218. */
6229 else if (!args->is_empty ()
6230 && is_overloaded_fn (postfix_expression))
6232 tree fn = get_first_fn (postfix_expression);
6233 fn = STRIP_TEMPLATE (fn);
6235 /* Do not do argument dependent lookup if regular
6236 lookup finds a member function or a block-scope
6237 function declaration. [basic.lookup.argdep]/3 */
6238 if (!DECL_FUNCTION_MEMBER_P (fn)
6239 && !DECL_LOCAL_FUNCTION_P (fn))
6241 koenig_p = true;
6242 if (!any_type_dependent_arguments_p (args))
6243 postfix_expression
6244 = perform_koenig_lookup (postfix_expression, args,
6245 complain);
6250 if (warn_memset_transposed_args)
6252 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6253 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6254 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6255 && vec_safe_length (args) == 3
6256 && integer_zerop ((*args)[2])
6257 && LITERAL_ZERO_P ((*args)[2])
6258 && !(integer_zerop ((*args)[1])
6259 && LITERAL_ZERO_P ((*args)[1])))
6260 warning (OPT_Wmemset_transposed_args,
6261 "%<memset%> used with constant zero length "
6262 "parameter; this could be due to transposed "
6263 "parameters");
6265 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6266 to avoid leaking those into folder and middle-end. */
6267 unsigned int i;
6268 tree arg;
6269 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6270 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6271 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6274 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6276 tree instance = TREE_OPERAND (postfix_expression, 0);
6277 tree fn = TREE_OPERAND (postfix_expression, 1);
6279 if (processing_template_decl
6280 && (type_dependent_expression_p (instance)
6281 || (!BASELINK_P (fn)
6282 && TREE_CODE (fn) != FIELD_DECL)
6283 || type_dependent_expression_p (fn)
6284 || any_type_dependent_arguments_p (args)))
6286 postfix_expression
6287 = build_nt_call_vec (postfix_expression, args);
6288 release_tree_vector (args);
6289 break;
6292 if (BASELINK_P (fn))
6294 postfix_expression
6295 = (build_new_method_call
6296 (instance, fn, &args, NULL_TREE,
6297 (idk == CP_ID_KIND_QUALIFIED
6298 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6299 : LOOKUP_NORMAL),
6300 /*fn_p=*/NULL,
6301 complain));
6303 else
6304 postfix_expression
6305 = finish_call_expr (postfix_expression, &args,
6306 /*disallow_virtual=*/false,
6307 /*koenig_p=*/false,
6308 complain);
6310 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6311 || TREE_CODE (postfix_expression) == MEMBER_REF
6312 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6313 postfix_expression = (build_offset_ref_call_from_tree
6314 (postfix_expression, &args,
6315 complain));
6316 else if (idk == CP_ID_KIND_QUALIFIED)
6317 /* A call to a static class member, or a namespace-scope
6318 function. */
6319 postfix_expression
6320 = finish_call_expr (postfix_expression, &args,
6321 /*disallow_virtual=*/true,
6322 koenig_p,
6323 complain);
6324 else
6325 /* All other function calls. */
6326 postfix_expression
6327 = finish_call_expr (postfix_expression, &args,
6328 /*disallow_virtual=*/false,
6329 koenig_p,
6330 complain);
6332 protected_set_expr_location (postfix_expression, token->location);
6334 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6335 idk = CP_ID_KIND_NONE;
6337 release_tree_vector (args);
6339 break;
6341 case CPP_DOT:
6342 case CPP_DEREF:
6343 /* postfix-expression . template [opt] id-expression
6344 postfix-expression . pseudo-destructor-name
6345 postfix-expression -> template [opt] id-expression
6346 postfix-expression -> pseudo-destructor-name */
6348 /* Consume the `.' or `->' operator. */
6349 cp_lexer_consume_token (parser->lexer);
6351 postfix_expression
6352 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6353 postfix_expression,
6354 false, &idk, loc);
6356 is_member_access = true;
6357 break;
6359 case CPP_PLUS_PLUS:
6360 /* postfix-expression ++ */
6361 /* Consume the `++' token. */
6362 cp_lexer_consume_token (parser->lexer);
6363 /* Generate a representation for the complete expression. */
6364 postfix_expression
6365 = finish_increment_expr (postfix_expression,
6366 POSTINCREMENT_EXPR);
6367 /* Increments may not appear in constant-expressions. */
6368 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6369 postfix_expression = error_mark_node;
6370 idk = CP_ID_KIND_NONE;
6371 is_member_access = false;
6372 break;
6374 case CPP_MINUS_MINUS:
6375 /* postfix-expression -- */
6376 /* Consume the `--' token. */
6377 cp_lexer_consume_token (parser->lexer);
6378 /* Generate a representation for the complete expression. */
6379 postfix_expression
6380 = finish_increment_expr (postfix_expression,
6381 POSTDECREMENT_EXPR);
6382 /* Decrements may not appear in constant-expressions. */
6383 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6384 postfix_expression = error_mark_node;
6385 idk = CP_ID_KIND_NONE;
6386 is_member_access = false;
6387 break;
6389 default:
6390 if (pidk_return != NULL)
6391 * pidk_return = idk;
6392 if (member_access_only_p)
6393 return is_member_access? postfix_expression : error_mark_node;
6394 else
6395 return postfix_expression;
6399 /* We should never get here. */
6400 gcc_unreachable ();
6401 return error_mark_node;
6404 /* This function parses Cilk Plus array notations. If a normal array expr. is
6405 parsed then the array index is passed back to the caller through *INIT_INDEX
6406 and the function returns a NULL_TREE. If array notation expr. is parsed,
6407 then *INIT_INDEX is ignored by the caller and the function returns
6408 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6409 error_mark_node. */
6411 static tree
6412 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6413 tree array_value)
6415 cp_token *token = NULL;
6416 tree length_index, stride = NULL_TREE, value_tree, array_type;
6417 if (!array_value || array_value == error_mark_node)
6419 cp_parser_skip_to_end_of_statement (parser);
6420 return error_mark_node;
6423 array_type = TREE_TYPE (array_value);
6425 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6426 parser->colon_corrects_to_scope_p = false;
6427 token = cp_lexer_peek_token (parser->lexer);
6429 if (!token)
6431 cp_parser_error (parser, "expected %<:%> or numeral");
6432 return error_mark_node;
6434 else if (token->type == CPP_COLON)
6436 /* Consume the ':'. */
6437 cp_lexer_consume_token (parser->lexer);
6439 /* If we are here, then we have a case like this A[:]. */
6440 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6442 cp_parser_error (parser, "expected %<]%>");
6443 cp_parser_skip_to_end_of_statement (parser);
6444 return error_mark_node;
6446 *init_index = NULL_TREE;
6447 stride = NULL_TREE;
6448 length_index = NULL_TREE;
6450 else
6452 /* If we are here, then there are three valid possibilities:
6453 1. ARRAY [ EXP ]
6454 2. ARRAY [ EXP : EXP ]
6455 3. ARRAY [ EXP : EXP : EXP ] */
6457 *init_index = cp_parser_expression (parser);
6458 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6460 /* This indicates that we have a normal array expression. */
6461 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6462 return NULL_TREE;
6465 /* Consume the ':'. */
6466 cp_lexer_consume_token (parser->lexer);
6467 length_index = cp_parser_expression (parser);
6468 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6470 cp_lexer_consume_token (parser->lexer);
6471 stride = cp_parser_expression (parser);
6474 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6476 if (*init_index == error_mark_node || length_index == error_mark_node
6477 || stride == error_mark_node || array_type == error_mark_node)
6479 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6480 cp_lexer_consume_token (parser->lexer);
6481 return error_mark_node;
6483 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6485 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6486 length_index, stride, array_type);
6487 return value_tree;
6490 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6491 by cp_parser_builtin_offsetof. We're looking for
6493 postfix-expression [ expression ]
6494 postfix-expression [ braced-init-list ] (C++11)
6496 FOR_OFFSETOF is set if we're being called in that context, which
6497 changes how we deal with integer constant expressions. */
6499 static tree
6500 cp_parser_postfix_open_square_expression (cp_parser *parser,
6501 tree postfix_expression,
6502 bool for_offsetof,
6503 bool decltype_p)
6505 tree index = NULL_TREE;
6506 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6507 bool saved_greater_than_is_operator_p;
6509 /* Consume the `[' token. */
6510 cp_lexer_consume_token (parser->lexer);
6512 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6513 parser->greater_than_is_operator_p = true;
6515 /* Parse the index expression. */
6516 /* ??? For offsetof, there is a question of what to allow here. If
6517 offsetof is not being used in an integral constant expression context,
6518 then we *could* get the right answer by computing the value at runtime.
6519 If we are in an integral constant expression context, then we might
6520 could accept any constant expression; hard to say without analysis.
6521 Rather than open the barn door too wide right away, allow only integer
6522 constant expressions here. */
6523 if (for_offsetof)
6524 index = cp_parser_constant_expression (parser);
6525 else
6527 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6529 bool expr_nonconst_p;
6530 cp_lexer_set_source_position (parser->lexer);
6531 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6532 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6533 if (flag_cilkplus
6534 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6536 error_at (cp_lexer_peek_token (parser->lexer)->location,
6537 "braced list index is not allowed with array "
6538 "notation");
6539 cp_parser_skip_to_end_of_statement (parser);
6540 return error_mark_node;
6543 else if (flag_cilkplus)
6545 /* Here are have these two options:
6546 ARRAY[EXP : EXP] - Array notation expr with default
6547 stride of 1.
6548 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6549 stride. */
6550 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6551 postfix_expression);
6552 if (an_exp)
6553 return an_exp;
6555 else
6556 index = cp_parser_expression (parser);
6559 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6561 /* Look for the closing `]'. */
6562 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6564 /* Build the ARRAY_REF. */
6565 postfix_expression = grok_array_decl (loc, postfix_expression,
6566 index, decltype_p);
6568 /* When not doing offsetof, array references are not permitted in
6569 constant-expressions. */
6570 if (!for_offsetof
6571 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6572 postfix_expression = error_mark_node;
6574 return postfix_expression;
6577 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6578 by cp_parser_builtin_offsetof. We're looking for
6580 postfix-expression . template [opt] id-expression
6581 postfix-expression . pseudo-destructor-name
6582 postfix-expression -> template [opt] id-expression
6583 postfix-expression -> pseudo-destructor-name
6585 FOR_OFFSETOF is set if we're being called in that context. That sorta
6586 limits what of the above we'll actually accept, but nevermind.
6587 TOKEN_TYPE is the "." or "->" token, which will already have been
6588 removed from the stream. */
6590 static tree
6591 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6592 enum cpp_ttype token_type,
6593 tree postfix_expression,
6594 bool for_offsetof, cp_id_kind *idk,
6595 location_t location)
6597 tree name;
6598 bool dependent_p;
6599 bool pseudo_destructor_p;
6600 tree scope = NULL_TREE;
6602 /* If this is a `->' operator, dereference the pointer. */
6603 if (token_type == CPP_DEREF)
6604 postfix_expression = build_x_arrow (location, postfix_expression,
6605 tf_warning_or_error);
6606 /* Check to see whether or not the expression is type-dependent. */
6607 dependent_p = type_dependent_expression_p (postfix_expression);
6608 /* The identifier following the `->' or `.' is not qualified. */
6609 parser->scope = NULL_TREE;
6610 parser->qualifying_scope = NULL_TREE;
6611 parser->object_scope = NULL_TREE;
6612 *idk = CP_ID_KIND_NONE;
6614 /* Enter the scope corresponding to the type of the object
6615 given by the POSTFIX_EXPRESSION. */
6616 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6618 scope = TREE_TYPE (postfix_expression);
6619 /* According to the standard, no expression should ever have
6620 reference type. Unfortunately, we do not currently match
6621 the standard in this respect in that our internal representation
6622 of an expression may have reference type even when the standard
6623 says it does not. Therefore, we have to manually obtain the
6624 underlying type here. */
6625 scope = non_reference (scope);
6626 /* The type of the POSTFIX_EXPRESSION must be complete. */
6627 if (scope == unknown_type_node)
6629 error_at (location, "%qE does not have class type",
6630 postfix_expression);
6631 scope = NULL_TREE;
6633 /* Unlike the object expression in other contexts, *this is not
6634 required to be of complete type for purposes of class member
6635 access (5.2.5) outside the member function body. */
6636 else if (postfix_expression != current_class_ref
6637 && !(processing_template_decl && scope == current_class_type))
6638 scope = complete_type_or_else (scope, NULL_TREE);
6639 /* Let the name lookup machinery know that we are processing a
6640 class member access expression. */
6641 parser->context->object_type = scope;
6642 /* If something went wrong, we want to be able to discern that case,
6643 as opposed to the case where there was no SCOPE due to the type
6644 of expression being dependent. */
6645 if (!scope)
6646 scope = error_mark_node;
6647 /* If the SCOPE was erroneous, make the various semantic analysis
6648 functions exit quickly -- and without issuing additional error
6649 messages. */
6650 if (scope == error_mark_node)
6651 postfix_expression = error_mark_node;
6654 /* Assume this expression is not a pseudo-destructor access. */
6655 pseudo_destructor_p = false;
6657 /* If the SCOPE is a scalar type, then, if this is a valid program,
6658 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6659 is type dependent, it can be pseudo-destructor-name or something else.
6660 Try to parse it as pseudo-destructor-name first. */
6661 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6663 tree s;
6664 tree type;
6666 cp_parser_parse_tentatively (parser);
6667 /* Parse the pseudo-destructor-name. */
6668 s = NULL_TREE;
6669 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6670 &s, &type);
6671 if (dependent_p
6672 && (cp_parser_error_occurred (parser)
6673 || !SCALAR_TYPE_P (type)))
6674 cp_parser_abort_tentative_parse (parser);
6675 else if (cp_parser_parse_definitely (parser))
6677 pseudo_destructor_p = true;
6678 postfix_expression
6679 = finish_pseudo_destructor_expr (postfix_expression,
6680 s, type, location);
6684 if (!pseudo_destructor_p)
6686 /* If the SCOPE is not a scalar type, we are looking at an
6687 ordinary class member access expression, rather than a
6688 pseudo-destructor-name. */
6689 bool template_p;
6690 cp_token *token = cp_lexer_peek_token (parser->lexer);
6691 /* Parse the id-expression. */
6692 name = (cp_parser_id_expression
6693 (parser,
6694 cp_parser_optional_template_keyword (parser),
6695 /*check_dependency_p=*/true,
6696 &template_p,
6697 /*declarator_p=*/false,
6698 /*optional_p=*/false));
6699 /* In general, build a SCOPE_REF if the member name is qualified.
6700 However, if the name was not dependent and has already been
6701 resolved; there is no need to build the SCOPE_REF. For example;
6703 struct X { void f(); };
6704 template <typename T> void f(T* t) { t->X::f(); }
6706 Even though "t" is dependent, "X::f" is not and has been resolved
6707 to a BASELINK; there is no need to include scope information. */
6709 /* But we do need to remember that there was an explicit scope for
6710 virtual function calls. */
6711 if (parser->scope)
6712 *idk = CP_ID_KIND_QUALIFIED;
6714 /* If the name is a template-id that names a type, we will get a
6715 TYPE_DECL here. That is invalid code. */
6716 if (TREE_CODE (name) == TYPE_DECL)
6718 error_at (token->location, "invalid use of %qD", name);
6719 postfix_expression = error_mark_node;
6721 else
6723 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6725 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6727 error_at (token->location, "%<%D::%D%> is not a class member",
6728 parser->scope, name);
6729 postfix_expression = error_mark_node;
6731 else
6732 name = build_qualified_name (/*type=*/NULL_TREE,
6733 parser->scope,
6734 name,
6735 template_p);
6736 parser->scope = NULL_TREE;
6737 parser->qualifying_scope = NULL_TREE;
6738 parser->object_scope = NULL_TREE;
6740 if (parser->scope && name && BASELINK_P (name))
6741 adjust_result_of_qualified_name_lookup
6742 (name, parser->scope, scope);
6743 postfix_expression
6744 = finish_class_member_access_expr (postfix_expression, name,
6745 template_p,
6746 tf_warning_or_error);
6750 /* We no longer need to look up names in the scope of the object on
6751 the left-hand side of the `.' or `->' operator. */
6752 parser->context->object_type = NULL_TREE;
6754 /* Outside of offsetof, these operators may not appear in
6755 constant-expressions. */
6756 if (!for_offsetof
6757 && (cp_parser_non_integral_constant_expression
6758 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6759 postfix_expression = error_mark_node;
6761 return postfix_expression;
6764 /* Cache of LITERAL_ZERO_P constants. */
6766 static GTY(()) tree literal_zeros[itk_none];
6768 /* Parse a parenthesized expression-list.
6770 expression-list:
6771 assignment-expression
6772 expression-list, assignment-expression
6774 attribute-list:
6775 expression-list
6776 identifier
6777 identifier, expression-list
6779 CAST_P is true if this expression is the target of a cast.
6781 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6782 argument pack.
6784 Returns a vector of trees. Each element is a representation of an
6785 assignment-expression. NULL is returned if the ( and or ) are
6786 missing. An empty, but allocated, vector is returned on no
6787 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6788 if we are parsing an attribute list for an attribute that wants a
6789 plain identifier argument, normal_attr for an attribute that wants
6790 an expression, or non_attr if we aren't parsing an attribute list. If
6791 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6792 not all of the expressions in the list were constant.
6793 WANT_LITERAL_ZERO_P is true if the caller is interested in
6794 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6795 immediately, this can be removed. */
6797 static vec<tree, va_gc> *
6798 cp_parser_parenthesized_expression_list (cp_parser* parser,
6799 int is_attribute_list,
6800 bool cast_p,
6801 bool allow_expansion_p,
6802 bool *non_constant_p,
6803 bool want_literal_zero_p)
6805 vec<tree, va_gc> *expression_list;
6806 bool fold_expr_p = is_attribute_list != non_attr;
6807 tree identifier = NULL_TREE;
6808 bool saved_greater_than_is_operator_p;
6810 /* Assume all the expressions will be constant. */
6811 if (non_constant_p)
6812 *non_constant_p = false;
6814 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6815 return NULL;
6817 expression_list = make_tree_vector ();
6819 /* Within a parenthesized expression, a `>' token is always
6820 the greater-than operator. */
6821 saved_greater_than_is_operator_p
6822 = parser->greater_than_is_operator_p;
6823 parser->greater_than_is_operator_p = true;
6825 /* Consume expressions until there are no more. */
6826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6827 while (true)
6829 tree expr;
6831 /* At the beginning of attribute lists, check to see if the
6832 next token is an identifier. */
6833 if (is_attribute_list == id_attr
6834 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6836 cp_token *token;
6838 /* Consume the identifier. */
6839 token = cp_lexer_consume_token (parser->lexer);
6840 /* Save the identifier. */
6841 identifier = token->u.value;
6843 else
6845 bool expr_non_constant_p;
6847 /* Parse the next assignment-expression. */
6848 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6850 /* A braced-init-list. */
6851 cp_lexer_set_source_position (parser->lexer);
6852 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6853 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6854 if (non_constant_p && expr_non_constant_p)
6855 *non_constant_p = true;
6857 else if (non_constant_p)
6859 expr = (cp_parser_constant_expression
6860 (parser, /*allow_non_constant_p=*/true,
6861 &expr_non_constant_p));
6862 if (expr_non_constant_p)
6863 *non_constant_p = true;
6865 else
6867 expr = NULL_TREE;
6868 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6869 switch (tok->type)
6871 case CPP_NUMBER:
6872 case CPP_CHAR:
6873 case CPP_WCHAR:
6874 case CPP_CHAR16:
6875 case CPP_CHAR32:
6876 /* If a parameter is literal zero alone, remember it
6877 for -Wmemset-transposed-args warning. */
6878 if (integer_zerop (tok->u.value)
6879 && !TREE_OVERFLOW (tok->u.value)
6880 && want_literal_zero_p
6881 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6882 == CPP_COMMA
6883 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6884 == CPP_CLOSE_PAREN))
6886 unsigned int i;
6887 for (i = 0; i < itk_none; ++i)
6888 if (TREE_TYPE (tok->u.value) == integer_types[i])
6889 break;
6890 if (i < itk_none && literal_zeros[i])
6891 expr = literal_zeros[i];
6892 else
6894 expr = copy_node (tok->u.value);
6895 LITERAL_ZERO_P (expr) = 1;
6896 if (i < itk_none)
6897 literal_zeros[i] = expr;
6899 /* Consume the 0 token (or '\0', 0LL etc.). */
6900 cp_lexer_consume_token (parser->lexer);
6902 break;
6903 default:
6904 break;
6906 if (expr == NULL_TREE)
6907 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6908 cast_p);
6911 if (fold_expr_p)
6912 expr = instantiate_non_dependent_expr (expr);
6914 /* If we have an ellipsis, then this is an expression
6915 expansion. */
6916 if (allow_expansion_p
6917 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6919 /* Consume the `...'. */
6920 cp_lexer_consume_token (parser->lexer);
6922 /* Build the argument pack. */
6923 expr = make_pack_expansion (expr);
6926 /* Add it to the list. We add error_mark_node
6927 expressions to the list, so that we can still tell if
6928 the correct form for a parenthesized expression-list
6929 is found. That gives better errors. */
6930 vec_safe_push (expression_list, expr);
6932 if (expr == error_mark_node)
6933 goto skip_comma;
6936 /* After the first item, attribute lists look the same as
6937 expression lists. */
6938 is_attribute_list = non_attr;
6940 get_comma:;
6941 /* If the next token isn't a `,', then we are done. */
6942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6943 break;
6945 /* Otherwise, consume the `,' and keep going. */
6946 cp_lexer_consume_token (parser->lexer);
6949 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6951 int ending;
6953 skip_comma:;
6954 /* We try and resync to an unnested comma, as that will give the
6955 user better diagnostics. */
6956 ending = cp_parser_skip_to_closing_parenthesis (parser,
6957 /*recovering=*/true,
6958 /*or_comma=*/true,
6959 /*consume_paren=*/true);
6960 if (ending < 0)
6961 goto get_comma;
6962 if (!ending)
6964 parser->greater_than_is_operator_p
6965 = saved_greater_than_is_operator_p;
6966 return NULL;
6970 parser->greater_than_is_operator_p
6971 = saved_greater_than_is_operator_p;
6973 if (identifier)
6974 vec_safe_insert (expression_list, 0, identifier);
6976 return expression_list;
6979 /* Parse a pseudo-destructor-name.
6981 pseudo-destructor-name:
6982 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6983 :: [opt] nested-name-specifier template template-id :: ~ type-name
6984 :: [opt] nested-name-specifier [opt] ~ type-name
6986 If either of the first two productions is used, sets *SCOPE to the
6987 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6988 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6989 or ERROR_MARK_NODE if the parse fails. */
6991 static void
6992 cp_parser_pseudo_destructor_name (cp_parser* parser,
6993 tree object,
6994 tree* scope,
6995 tree* type)
6997 bool nested_name_specifier_p;
6999 /* Handle ~auto. */
7000 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7001 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7002 && !type_dependent_expression_p (object))
7004 if (cxx_dialect < cxx14)
7005 pedwarn (input_location, 0,
7006 "%<~auto%> only available with "
7007 "-std=c++14 or -std=gnu++14");
7008 cp_lexer_consume_token (parser->lexer);
7009 cp_lexer_consume_token (parser->lexer);
7010 *scope = NULL_TREE;
7011 *type = TREE_TYPE (object);
7012 return;
7015 /* Assume that things will not work out. */
7016 *type = error_mark_node;
7018 /* Look for the optional `::' operator. */
7019 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7020 /* Look for the optional nested-name-specifier. */
7021 nested_name_specifier_p
7022 = (cp_parser_nested_name_specifier_opt (parser,
7023 /*typename_keyword_p=*/false,
7024 /*check_dependency_p=*/true,
7025 /*type_p=*/false,
7026 /*is_declaration=*/false)
7027 != NULL_TREE);
7028 /* Now, if we saw a nested-name-specifier, we might be doing the
7029 second production. */
7030 if (nested_name_specifier_p
7031 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7033 /* Consume the `template' keyword. */
7034 cp_lexer_consume_token (parser->lexer);
7035 /* Parse the template-id. */
7036 cp_parser_template_id (parser,
7037 /*template_keyword_p=*/true,
7038 /*check_dependency_p=*/false,
7039 class_type,
7040 /*is_declaration=*/true);
7041 /* Look for the `::' token. */
7042 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7044 /* If the next token is not a `~', then there might be some
7045 additional qualification. */
7046 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7048 /* At this point, we're looking for "type-name :: ~". The type-name
7049 must not be a class-name, since this is a pseudo-destructor. So,
7050 it must be either an enum-name, or a typedef-name -- both of which
7051 are just identifiers. So, we peek ahead to check that the "::"
7052 and "~" tokens are present; if they are not, then we can avoid
7053 calling type_name. */
7054 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7055 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7056 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7058 cp_parser_error (parser, "non-scalar type");
7059 return;
7062 /* Look for the type-name. */
7063 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7064 if (*scope == error_mark_node)
7065 return;
7067 /* Look for the `::' token. */
7068 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7070 else
7071 *scope = NULL_TREE;
7073 /* Look for the `~'. */
7074 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7076 /* Once we see the ~, this has to be a pseudo-destructor. */
7077 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7078 cp_parser_commit_to_topmost_tentative_parse (parser);
7080 /* Look for the type-name again. We are not responsible for
7081 checking that it matches the first type-name. */
7082 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7085 /* Parse a unary-expression.
7087 unary-expression:
7088 postfix-expression
7089 ++ cast-expression
7090 -- cast-expression
7091 unary-operator cast-expression
7092 sizeof unary-expression
7093 sizeof ( type-id )
7094 alignof ( type-id ) [C++0x]
7095 new-expression
7096 delete-expression
7098 GNU Extensions:
7100 unary-expression:
7101 __extension__ cast-expression
7102 __alignof__ unary-expression
7103 __alignof__ ( type-id )
7104 alignof unary-expression [C++0x]
7105 __real__ cast-expression
7106 __imag__ cast-expression
7107 && identifier
7108 sizeof ( type-id ) { initializer-list , [opt] }
7109 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7110 __alignof__ ( type-id ) { initializer-list , [opt] }
7112 ADDRESS_P is true iff the unary-expression is appearing as the
7113 operand of the `&' operator. CAST_P is true if this expression is
7114 the target of a cast.
7116 Returns a representation of the expression. */
7118 static tree
7119 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7120 bool address_p, bool cast_p, bool decltype_p)
7122 cp_token *token;
7123 enum tree_code unary_operator;
7125 /* Peek at the next token. */
7126 token = cp_lexer_peek_token (parser->lexer);
7127 /* Some keywords give away the kind of expression. */
7128 if (token->type == CPP_KEYWORD)
7130 enum rid keyword = token->keyword;
7132 switch (keyword)
7134 case RID_ALIGNOF:
7135 case RID_SIZEOF:
7137 tree operand, ret;
7138 enum tree_code op;
7139 location_t first_loc;
7141 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7142 /* Consume the token. */
7143 cp_lexer_consume_token (parser->lexer);
7144 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7145 /* Parse the operand. */
7146 operand = cp_parser_sizeof_operand (parser, keyword);
7148 if (TYPE_P (operand))
7149 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7150 else
7152 /* ISO C++ defines alignof only with types, not with
7153 expressions. So pedwarn if alignof is used with a non-
7154 type expression. However, __alignof__ is ok. */
7155 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7156 pedwarn (token->location, OPT_Wpedantic,
7157 "ISO C++ does not allow %<alignof%> "
7158 "with a non-type");
7160 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7162 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7163 SIZEOF_EXPR with the original operand. */
7164 if (op == SIZEOF_EXPR && ret != error_mark_node)
7166 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7168 if (!processing_template_decl && TYPE_P (operand))
7170 ret = build_min (SIZEOF_EXPR, size_type_node,
7171 build1 (NOP_EXPR, operand,
7172 error_mark_node));
7173 SIZEOF_EXPR_TYPE_P (ret) = 1;
7175 else
7176 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7177 TREE_SIDE_EFFECTS (ret) = 0;
7178 TREE_READONLY (ret) = 1;
7180 SET_EXPR_LOCATION (ret, first_loc);
7182 return ret;
7185 case RID_NEW:
7186 return cp_parser_new_expression (parser);
7188 case RID_DELETE:
7189 return cp_parser_delete_expression (parser);
7191 case RID_EXTENSION:
7193 /* The saved value of the PEDANTIC flag. */
7194 int saved_pedantic;
7195 tree expr;
7197 /* Save away the PEDANTIC flag. */
7198 cp_parser_extension_opt (parser, &saved_pedantic);
7199 /* Parse the cast-expression. */
7200 expr = cp_parser_simple_cast_expression (parser);
7201 /* Restore the PEDANTIC flag. */
7202 pedantic = saved_pedantic;
7204 return expr;
7207 case RID_REALPART:
7208 case RID_IMAGPART:
7210 tree expression;
7212 /* Consume the `__real__' or `__imag__' token. */
7213 cp_lexer_consume_token (parser->lexer);
7214 /* Parse the cast-expression. */
7215 expression = cp_parser_simple_cast_expression (parser);
7216 /* Create the complete representation. */
7217 return build_x_unary_op (token->location,
7218 (keyword == RID_REALPART
7219 ? REALPART_EXPR : IMAGPART_EXPR),
7220 expression,
7221 tf_warning_or_error);
7223 break;
7225 case RID_TRANSACTION_ATOMIC:
7226 case RID_TRANSACTION_RELAXED:
7227 return cp_parser_transaction_expression (parser, keyword);
7229 case RID_NOEXCEPT:
7231 tree expr;
7232 const char *saved_message;
7233 bool saved_integral_constant_expression_p;
7234 bool saved_non_integral_constant_expression_p;
7235 bool saved_greater_than_is_operator_p;
7237 cp_lexer_consume_token (parser->lexer);
7238 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7240 saved_message = parser->type_definition_forbidden_message;
7241 parser->type_definition_forbidden_message
7242 = G_("types may not be defined in %<noexcept%> expressions");
7244 saved_integral_constant_expression_p
7245 = parser->integral_constant_expression_p;
7246 saved_non_integral_constant_expression_p
7247 = parser->non_integral_constant_expression_p;
7248 parser->integral_constant_expression_p = false;
7250 saved_greater_than_is_operator_p
7251 = parser->greater_than_is_operator_p;
7252 parser->greater_than_is_operator_p = true;
7254 ++cp_unevaluated_operand;
7255 ++c_inhibit_evaluation_warnings;
7256 ++cp_noexcept_operand;
7257 expr = cp_parser_expression (parser);
7258 --cp_noexcept_operand;
7259 --c_inhibit_evaluation_warnings;
7260 --cp_unevaluated_operand;
7262 parser->greater_than_is_operator_p
7263 = saved_greater_than_is_operator_p;
7265 parser->integral_constant_expression_p
7266 = saved_integral_constant_expression_p;
7267 parser->non_integral_constant_expression_p
7268 = saved_non_integral_constant_expression_p;
7270 parser->type_definition_forbidden_message = saved_message;
7272 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7273 return finish_noexcept_expr (expr, tf_warning_or_error);
7276 default:
7277 break;
7281 /* Look for the `:: new' and `:: delete', which also signal the
7282 beginning of a new-expression, or delete-expression,
7283 respectively. If the next token is `::', then it might be one of
7284 these. */
7285 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7287 enum rid keyword;
7289 /* See if the token after the `::' is one of the keywords in
7290 which we're interested. */
7291 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7292 /* If it's `new', we have a new-expression. */
7293 if (keyword == RID_NEW)
7294 return cp_parser_new_expression (parser);
7295 /* Similarly, for `delete'. */
7296 else if (keyword == RID_DELETE)
7297 return cp_parser_delete_expression (parser);
7300 /* Look for a unary operator. */
7301 unary_operator = cp_parser_unary_operator (token);
7302 /* The `++' and `--' operators can be handled similarly, even though
7303 they are not technically unary-operators in the grammar. */
7304 if (unary_operator == ERROR_MARK)
7306 if (token->type == CPP_PLUS_PLUS)
7307 unary_operator = PREINCREMENT_EXPR;
7308 else if (token->type == CPP_MINUS_MINUS)
7309 unary_operator = PREDECREMENT_EXPR;
7310 /* Handle the GNU address-of-label extension. */
7311 else if (cp_parser_allow_gnu_extensions_p (parser)
7312 && token->type == CPP_AND_AND)
7314 tree identifier;
7315 tree expression;
7316 location_t loc = token->location;
7318 /* Consume the '&&' token. */
7319 cp_lexer_consume_token (parser->lexer);
7320 /* Look for the identifier. */
7321 identifier = cp_parser_identifier (parser);
7322 /* Create an expression representing the address. */
7323 expression = finish_label_address_expr (identifier, loc);
7324 if (cp_parser_non_integral_constant_expression (parser,
7325 NIC_ADDR_LABEL))
7326 expression = error_mark_node;
7327 return expression;
7330 if (unary_operator != ERROR_MARK)
7332 tree cast_expression;
7333 tree expression = error_mark_node;
7334 non_integral_constant non_constant_p = NIC_NONE;
7335 location_t loc = token->location;
7336 tsubst_flags_t complain = complain_flags (decltype_p);
7338 /* Consume the operator token. */
7339 token = cp_lexer_consume_token (parser->lexer);
7340 /* Parse the cast-expression. */
7341 cast_expression
7342 = cp_parser_cast_expression (parser,
7343 unary_operator == ADDR_EXPR,
7344 /*cast_p=*/false,
7345 /*decltype*/false,
7346 pidk);
7347 /* Now, build an appropriate representation. */
7348 switch (unary_operator)
7350 case INDIRECT_REF:
7351 non_constant_p = NIC_STAR;
7352 expression = build_x_indirect_ref (loc, cast_expression,
7353 RO_UNARY_STAR,
7354 complain);
7355 break;
7357 case ADDR_EXPR:
7358 non_constant_p = NIC_ADDR;
7359 /* Fall through. */
7360 case BIT_NOT_EXPR:
7361 expression = build_x_unary_op (loc, unary_operator,
7362 cast_expression,
7363 complain);
7364 break;
7366 case PREINCREMENT_EXPR:
7367 case PREDECREMENT_EXPR:
7368 non_constant_p = unary_operator == PREINCREMENT_EXPR
7369 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7370 /* Fall through. */
7371 case UNARY_PLUS_EXPR:
7372 case NEGATE_EXPR:
7373 case TRUTH_NOT_EXPR:
7374 expression = finish_unary_op_expr (loc, unary_operator,
7375 cast_expression, complain);
7376 break;
7378 default:
7379 gcc_unreachable ();
7382 if (non_constant_p != NIC_NONE
7383 && cp_parser_non_integral_constant_expression (parser,
7384 non_constant_p))
7385 expression = error_mark_node;
7387 return expression;
7390 return cp_parser_postfix_expression (parser, address_p, cast_p,
7391 /*member_access_only_p=*/false,
7392 decltype_p,
7393 pidk);
7396 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7397 unary-operator, the corresponding tree code is returned. */
7399 static enum tree_code
7400 cp_parser_unary_operator (cp_token* token)
7402 switch (token->type)
7404 case CPP_MULT:
7405 return INDIRECT_REF;
7407 case CPP_AND:
7408 return ADDR_EXPR;
7410 case CPP_PLUS:
7411 return UNARY_PLUS_EXPR;
7413 case CPP_MINUS:
7414 return NEGATE_EXPR;
7416 case CPP_NOT:
7417 return TRUTH_NOT_EXPR;
7419 case CPP_COMPL:
7420 return BIT_NOT_EXPR;
7422 default:
7423 return ERROR_MARK;
7427 /* Parse a new-expression.
7429 new-expression:
7430 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7431 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7433 Returns a representation of the expression. */
7435 static tree
7436 cp_parser_new_expression (cp_parser* parser)
7438 bool global_scope_p;
7439 vec<tree, va_gc> *placement;
7440 tree type;
7441 vec<tree, va_gc> *initializer;
7442 tree nelts = NULL_TREE;
7443 tree ret;
7445 /* Look for the optional `::' operator. */
7446 global_scope_p
7447 = (cp_parser_global_scope_opt (parser,
7448 /*current_scope_valid_p=*/false)
7449 != NULL_TREE);
7450 /* Look for the `new' operator. */
7451 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7452 /* There's no easy way to tell a new-placement from the
7453 `( type-id )' construct. */
7454 cp_parser_parse_tentatively (parser);
7455 /* Look for a new-placement. */
7456 placement = cp_parser_new_placement (parser);
7457 /* If that didn't work out, there's no new-placement. */
7458 if (!cp_parser_parse_definitely (parser))
7460 if (placement != NULL)
7461 release_tree_vector (placement);
7462 placement = NULL;
7465 /* If the next token is a `(', then we have a parenthesized
7466 type-id. */
7467 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7469 cp_token *token;
7470 const char *saved_message = parser->type_definition_forbidden_message;
7472 /* Consume the `('. */
7473 cp_lexer_consume_token (parser->lexer);
7475 /* Parse the type-id. */
7476 parser->type_definition_forbidden_message
7477 = G_("types may not be defined in a new-expression");
7478 type = cp_parser_type_id (parser);
7479 parser->type_definition_forbidden_message = saved_message;
7481 /* Look for the closing `)'. */
7482 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7483 token = cp_lexer_peek_token (parser->lexer);
7484 /* There should not be a direct-new-declarator in this production,
7485 but GCC used to allowed this, so we check and emit a sensible error
7486 message for this case. */
7487 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7489 error_at (token->location,
7490 "array bound forbidden after parenthesized type-id");
7491 inform (token->location,
7492 "try removing the parentheses around the type-id");
7493 cp_parser_direct_new_declarator (parser);
7496 /* Otherwise, there must be a new-type-id. */
7497 else
7498 type = cp_parser_new_type_id (parser, &nelts);
7500 /* If the next token is a `(' or '{', then we have a new-initializer. */
7501 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7502 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7503 initializer = cp_parser_new_initializer (parser);
7504 else
7505 initializer = NULL;
7507 /* A new-expression may not appear in an integral constant
7508 expression. */
7509 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7510 ret = error_mark_node;
7511 else
7513 /* Create a representation of the new-expression. */
7514 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7515 tf_warning_or_error);
7518 if (placement != NULL)
7519 release_tree_vector (placement);
7520 if (initializer != NULL)
7521 release_tree_vector (initializer);
7523 return ret;
7526 /* Parse a new-placement.
7528 new-placement:
7529 ( expression-list )
7531 Returns the same representation as for an expression-list. */
7533 static vec<tree, va_gc> *
7534 cp_parser_new_placement (cp_parser* parser)
7536 vec<tree, va_gc> *expression_list;
7538 /* Parse the expression-list. */
7539 expression_list = (cp_parser_parenthesized_expression_list
7540 (parser, non_attr, /*cast_p=*/false,
7541 /*allow_expansion_p=*/true,
7542 /*non_constant_p=*/NULL));
7544 return expression_list;
7547 /* Parse a new-type-id.
7549 new-type-id:
7550 type-specifier-seq new-declarator [opt]
7552 Returns the TYPE allocated. If the new-type-id indicates an array
7553 type, *NELTS is set to the number of elements in the last array
7554 bound; the TYPE will not include the last array bound. */
7556 static tree
7557 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7559 cp_decl_specifier_seq type_specifier_seq;
7560 cp_declarator *new_declarator;
7561 cp_declarator *declarator;
7562 cp_declarator *outer_declarator;
7563 const char *saved_message;
7565 /* The type-specifier sequence must not contain type definitions.
7566 (It cannot contain declarations of new types either, but if they
7567 are not definitions we will catch that because they are not
7568 complete.) */
7569 saved_message = parser->type_definition_forbidden_message;
7570 parser->type_definition_forbidden_message
7571 = G_("types may not be defined in a new-type-id");
7572 /* Parse the type-specifier-seq. */
7573 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7574 /*is_trailing_return=*/false,
7575 &type_specifier_seq);
7576 /* Restore the old message. */
7577 parser->type_definition_forbidden_message = saved_message;
7579 if (type_specifier_seq.type == error_mark_node)
7580 return error_mark_node;
7582 /* Parse the new-declarator. */
7583 new_declarator = cp_parser_new_declarator_opt (parser);
7585 /* Determine the number of elements in the last array dimension, if
7586 any. */
7587 *nelts = NULL_TREE;
7588 /* Skip down to the last array dimension. */
7589 declarator = new_declarator;
7590 outer_declarator = NULL;
7591 while (declarator && (declarator->kind == cdk_pointer
7592 || declarator->kind == cdk_ptrmem))
7594 outer_declarator = declarator;
7595 declarator = declarator->declarator;
7597 while (declarator
7598 && declarator->kind == cdk_array
7599 && declarator->declarator
7600 && declarator->declarator->kind == cdk_array)
7602 outer_declarator = declarator;
7603 declarator = declarator->declarator;
7606 if (declarator && declarator->kind == cdk_array)
7608 *nelts = declarator->u.array.bounds;
7609 if (*nelts == error_mark_node)
7610 *nelts = integer_one_node;
7612 if (outer_declarator)
7613 outer_declarator->declarator = declarator->declarator;
7614 else
7615 new_declarator = NULL;
7618 return groktypename (&type_specifier_seq, new_declarator, false);
7621 /* Parse an (optional) new-declarator.
7623 new-declarator:
7624 ptr-operator new-declarator [opt]
7625 direct-new-declarator
7627 Returns the declarator. */
7629 static cp_declarator *
7630 cp_parser_new_declarator_opt (cp_parser* parser)
7632 enum tree_code code;
7633 tree type, std_attributes = NULL_TREE;
7634 cp_cv_quals cv_quals;
7636 /* We don't know if there's a ptr-operator next, or not. */
7637 cp_parser_parse_tentatively (parser);
7638 /* Look for a ptr-operator. */
7639 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7640 /* If that worked, look for more new-declarators. */
7641 if (cp_parser_parse_definitely (parser))
7643 cp_declarator *declarator;
7645 /* Parse another optional declarator. */
7646 declarator = cp_parser_new_declarator_opt (parser);
7648 declarator = cp_parser_make_indirect_declarator
7649 (code, type, cv_quals, declarator, std_attributes);
7651 return declarator;
7654 /* If the next token is a `[', there is a direct-new-declarator. */
7655 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7656 return cp_parser_direct_new_declarator (parser);
7658 return NULL;
7661 /* Parse a direct-new-declarator.
7663 direct-new-declarator:
7664 [ expression ]
7665 direct-new-declarator [constant-expression]
7669 static cp_declarator *
7670 cp_parser_direct_new_declarator (cp_parser* parser)
7672 cp_declarator *declarator = NULL;
7674 while (true)
7676 tree expression;
7677 cp_token *token;
7679 /* Look for the opening `['. */
7680 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7682 token = cp_lexer_peek_token (parser->lexer);
7683 expression = cp_parser_expression (parser);
7684 /* The standard requires that the expression have integral
7685 type. DR 74 adds enumeration types. We believe that the
7686 real intent is that these expressions be handled like the
7687 expression in a `switch' condition, which also allows
7688 classes with a single conversion to integral or
7689 enumeration type. */
7690 if (!processing_template_decl)
7692 expression
7693 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7694 expression,
7695 /*complain=*/true);
7696 if (!expression)
7698 error_at (token->location,
7699 "expression in new-declarator must have integral "
7700 "or enumeration type");
7701 expression = error_mark_node;
7705 /* Look for the closing `]'. */
7706 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7708 /* Add this bound to the declarator. */
7709 declarator = make_array_declarator (declarator, expression);
7711 /* If the next token is not a `[', then there are no more
7712 bounds. */
7713 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7714 break;
7717 return declarator;
7720 /* Parse a new-initializer.
7722 new-initializer:
7723 ( expression-list [opt] )
7724 braced-init-list
7726 Returns a representation of the expression-list. */
7728 static vec<tree, va_gc> *
7729 cp_parser_new_initializer (cp_parser* parser)
7731 vec<tree, va_gc> *expression_list;
7733 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7735 tree t;
7736 bool expr_non_constant_p;
7737 cp_lexer_set_source_position (parser->lexer);
7738 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7739 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7740 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7741 expression_list = make_tree_vector_single (t);
7743 else
7744 expression_list = (cp_parser_parenthesized_expression_list
7745 (parser, non_attr, /*cast_p=*/false,
7746 /*allow_expansion_p=*/true,
7747 /*non_constant_p=*/NULL));
7749 return expression_list;
7752 /* Parse a delete-expression.
7754 delete-expression:
7755 :: [opt] delete cast-expression
7756 :: [opt] delete [ ] cast-expression
7758 Returns a representation of the expression. */
7760 static tree
7761 cp_parser_delete_expression (cp_parser* parser)
7763 bool global_scope_p;
7764 bool array_p;
7765 tree expression;
7767 /* Look for the optional `::' operator. */
7768 global_scope_p
7769 = (cp_parser_global_scope_opt (parser,
7770 /*current_scope_valid_p=*/false)
7771 != NULL_TREE);
7772 /* Look for the `delete' keyword. */
7773 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7774 /* See if the array syntax is in use. */
7775 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7777 /* Consume the `[' token. */
7778 cp_lexer_consume_token (parser->lexer);
7779 /* Look for the `]' token. */
7780 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7781 /* Remember that this is the `[]' construct. */
7782 array_p = true;
7784 else
7785 array_p = false;
7787 /* Parse the cast-expression. */
7788 expression = cp_parser_simple_cast_expression (parser);
7790 /* A delete-expression may not appear in an integral constant
7791 expression. */
7792 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7793 return error_mark_node;
7795 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7796 tf_warning_or_error);
7799 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7800 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7801 0 otherwise. */
7803 static int
7804 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7806 cp_token *token = cp_lexer_peek_token (parser->lexer);
7807 switch (token->type)
7809 case CPP_COMMA:
7810 case CPP_SEMICOLON:
7811 case CPP_QUERY:
7812 case CPP_COLON:
7813 case CPP_CLOSE_SQUARE:
7814 case CPP_CLOSE_PAREN:
7815 case CPP_CLOSE_BRACE:
7816 case CPP_OPEN_BRACE:
7817 case CPP_DOT:
7818 case CPP_DOT_STAR:
7819 case CPP_DEREF:
7820 case CPP_DEREF_STAR:
7821 case CPP_DIV:
7822 case CPP_MOD:
7823 case CPP_LSHIFT:
7824 case CPP_RSHIFT:
7825 case CPP_LESS:
7826 case CPP_GREATER:
7827 case CPP_LESS_EQ:
7828 case CPP_GREATER_EQ:
7829 case CPP_EQ_EQ:
7830 case CPP_NOT_EQ:
7831 case CPP_EQ:
7832 case CPP_MULT_EQ:
7833 case CPP_DIV_EQ:
7834 case CPP_MOD_EQ:
7835 case CPP_PLUS_EQ:
7836 case CPP_MINUS_EQ:
7837 case CPP_RSHIFT_EQ:
7838 case CPP_LSHIFT_EQ:
7839 case CPP_AND_EQ:
7840 case CPP_XOR_EQ:
7841 case CPP_OR_EQ:
7842 case CPP_XOR:
7843 case CPP_OR:
7844 case CPP_OR_OR:
7845 case CPP_EOF:
7846 case CPP_ELLIPSIS:
7847 return 0;
7849 case CPP_OPEN_PAREN:
7850 /* In ((type ()) () the last () isn't a valid cast-expression,
7851 so the whole must be parsed as postfix-expression. */
7852 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7853 != CPP_CLOSE_PAREN;
7855 case CPP_OPEN_SQUARE:
7856 /* '[' may start a primary-expression in obj-c++ and in C++11,
7857 as a lambda-expression, eg, '(void)[]{}'. */
7858 if (cxx_dialect >= cxx11)
7859 return -1;
7860 return c_dialect_objc ();
7862 case CPP_PLUS_PLUS:
7863 case CPP_MINUS_MINUS:
7864 /* '++' and '--' may or may not start a cast-expression:
7866 struct T { void operator++(int); };
7867 void f() { (T())++; }
7871 int a;
7872 (int)++a; */
7873 return -1;
7875 default:
7876 return 1;
7880 /* Parse a cast-expression.
7882 cast-expression:
7883 unary-expression
7884 ( type-id ) cast-expression
7886 ADDRESS_P is true iff the unary-expression is appearing as the
7887 operand of the `&' operator. CAST_P is true if this expression is
7888 the target of a cast.
7890 Returns a representation of the expression. */
7892 static tree
7893 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7894 bool decltype_p, cp_id_kind * pidk)
7896 /* If it's a `(', then we might be looking at a cast. */
7897 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7899 tree type = NULL_TREE;
7900 tree expr = NULL_TREE;
7901 int cast_expression = 0;
7902 const char *saved_message;
7904 /* There's no way to know yet whether or not this is a cast.
7905 For example, `(int (3))' is a unary-expression, while `(int)
7906 3' is a cast. So, we resort to parsing tentatively. */
7907 cp_parser_parse_tentatively (parser);
7908 /* Types may not be defined in a cast. */
7909 saved_message = parser->type_definition_forbidden_message;
7910 parser->type_definition_forbidden_message
7911 = G_("types may not be defined in casts");
7912 /* Consume the `('. */
7913 cp_lexer_consume_token (parser->lexer);
7914 /* A very tricky bit is that `(struct S) { 3 }' is a
7915 compound-literal (which we permit in C++ as an extension).
7916 But, that construct is not a cast-expression -- it is a
7917 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7918 is legal; if the compound-literal were a cast-expression,
7919 you'd need an extra set of parentheses.) But, if we parse
7920 the type-id, and it happens to be a class-specifier, then we
7921 will commit to the parse at that point, because we cannot
7922 undo the action that is done when creating a new class. So,
7923 then we cannot back up and do a postfix-expression.
7925 Another tricky case is the following (c++/29234):
7927 struct S { void operator () (); };
7929 void foo ()
7931 ( S()() );
7934 As a type-id we parse the parenthesized S()() as a function
7935 returning a function, groktypename complains and we cannot
7936 back up in this case either.
7938 Therefore, we scan ahead to the closing `)', and check to see
7939 if the tokens after the `)' can start a cast-expression. Otherwise
7940 we are dealing with an unary-expression, a postfix-expression
7941 or something else.
7943 Yet another tricky case, in C++11, is the following (c++/54891):
7945 (void)[]{};
7947 The issue is that usually, besides the case of lambda-expressions,
7948 the parenthesized type-id cannot be followed by '[', and, eg, we
7949 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7950 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7951 we don't commit, we try a cast-expression, then an unary-expression.
7953 Save tokens so that we can put them back. */
7954 cp_lexer_save_tokens (parser->lexer);
7956 /* We may be looking at a cast-expression. */
7957 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7958 /*consume_paren=*/true))
7959 cast_expression
7960 = cp_parser_tokens_start_cast_expression (parser);
7962 /* Roll back the tokens we skipped. */
7963 cp_lexer_rollback_tokens (parser->lexer);
7964 /* If we aren't looking at a cast-expression, simulate an error so
7965 that the call to cp_parser_error_occurred below returns true. */
7966 if (!cast_expression)
7967 cp_parser_simulate_error (parser);
7968 else
7970 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7971 parser->in_type_id_in_expr_p = true;
7972 /* Look for the type-id. */
7973 type = cp_parser_type_id (parser);
7974 /* Look for the closing `)'. */
7975 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7976 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7979 /* Restore the saved message. */
7980 parser->type_definition_forbidden_message = saved_message;
7982 /* At this point this can only be either a cast or a
7983 parenthesized ctor such as `(T ())' that looks like a cast to
7984 function returning T. */
7985 if (!cp_parser_error_occurred (parser))
7987 /* Only commit if the cast-expression doesn't start with
7988 '++', '--', or '[' in C++11. */
7989 if (cast_expression > 0)
7990 cp_parser_commit_to_topmost_tentative_parse (parser);
7992 expr = cp_parser_cast_expression (parser,
7993 /*address_p=*/false,
7994 /*cast_p=*/true,
7995 /*decltype_p=*/false,
7996 pidk);
7998 if (cp_parser_parse_definitely (parser))
8000 /* Warn about old-style casts, if so requested. */
8001 if (warn_old_style_cast
8002 && !in_system_header_at (input_location)
8003 && !VOID_TYPE_P (type)
8004 && current_lang_name != lang_name_c)
8005 warning (OPT_Wold_style_cast, "use of old-style cast");
8007 /* Only type conversions to integral or enumeration types
8008 can be used in constant-expressions. */
8009 if (!cast_valid_in_integral_constant_expression_p (type)
8010 && cp_parser_non_integral_constant_expression (parser,
8011 NIC_CAST))
8012 return error_mark_node;
8014 /* Perform the cast. */
8015 expr = build_c_cast (input_location, type, expr);
8016 return expr;
8019 else
8020 cp_parser_abort_tentative_parse (parser);
8023 /* If we get here, then it's not a cast, so it must be a
8024 unary-expression. */
8025 return cp_parser_unary_expression (parser, pidk, address_p,
8026 cast_p, decltype_p);
8029 /* Parse a binary expression of the general form:
8031 pm-expression:
8032 cast-expression
8033 pm-expression .* cast-expression
8034 pm-expression ->* cast-expression
8036 multiplicative-expression:
8037 pm-expression
8038 multiplicative-expression * pm-expression
8039 multiplicative-expression / pm-expression
8040 multiplicative-expression % pm-expression
8042 additive-expression:
8043 multiplicative-expression
8044 additive-expression + multiplicative-expression
8045 additive-expression - multiplicative-expression
8047 shift-expression:
8048 additive-expression
8049 shift-expression << additive-expression
8050 shift-expression >> additive-expression
8052 relational-expression:
8053 shift-expression
8054 relational-expression < shift-expression
8055 relational-expression > shift-expression
8056 relational-expression <= shift-expression
8057 relational-expression >= shift-expression
8059 GNU Extension:
8061 relational-expression:
8062 relational-expression <? shift-expression
8063 relational-expression >? shift-expression
8065 equality-expression:
8066 relational-expression
8067 equality-expression == relational-expression
8068 equality-expression != relational-expression
8070 and-expression:
8071 equality-expression
8072 and-expression & equality-expression
8074 exclusive-or-expression:
8075 and-expression
8076 exclusive-or-expression ^ and-expression
8078 inclusive-or-expression:
8079 exclusive-or-expression
8080 inclusive-or-expression | exclusive-or-expression
8082 logical-and-expression:
8083 inclusive-or-expression
8084 logical-and-expression && inclusive-or-expression
8086 logical-or-expression:
8087 logical-and-expression
8088 logical-or-expression || logical-and-expression
8090 All these are implemented with a single function like:
8092 binary-expression:
8093 simple-cast-expression
8094 binary-expression <token> binary-expression
8096 CAST_P is true if this expression is the target of a cast.
8098 The binops_by_token map is used to get the tree codes for each <token> type.
8099 binary-expressions are associated according to a precedence table. */
8101 #define TOKEN_PRECEDENCE(token) \
8102 (((token->type == CPP_GREATER \
8103 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8104 && !parser->greater_than_is_operator_p) \
8105 ? PREC_NOT_OPERATOR \
8106 : binops_by_token[token->type].prec)
8108 static tree
8109 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8110 bool no_toplevel_fold_p,
8111 bool decltype_p,
8112 enum cp_parser_prec prec,
8113 cp_id_kind * pidk)
8115 cp_parser_expression_stack stack;
8116 cp_parser_expression_stack_entry *sp = &stack[0];
8117 cp_parser_expression_stack_entry current;
8118 tree rhs;
8119 cp_token *token;
8120 enum tree_code rhs_type;
8121 enum cp_parser_prec new_prec, lookahead_prec;
8122 tree overload;
8124 /* Parse the first expression. */
8125 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8126 ? TRUTH_NOT_EXPR : ERROR_MARK);
8127 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8128 cast_p, decltype_p, pidk);
8129 current.prec = prec;
8131 if (cp_parser_error_occurred (parser))
8132 return error_mark_node;
8134 for (;;)
8136 /* Get an operator token. */
8137 token = cp_lexer_peek_token (parser->lexer);
8139 if (warn_cxx0x_compat
8140 && token->type == CPP_RSHIFT
8141 && !parser->greater_than_is_operator_p)
8143 if (warning_at (token->location, OPT_Wc__0x_compat,
8144 "%<>>%> operator is treated"
8145 " as two right angle brackets in C++11"))
8146 inform (token->location,
8147 "suggest parentheses around %<>>%> expression");
8150 new_prec = TOKEN_PRECEDENCE (token);
8152 /* Popping an entry off the stack means we completed a subexpression:
8153 - either we found a token which is not an operator (`>' where it is not
8154 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8155 will happen repeatedly;
8156 - or, we found an operator which has lower priority. This is the case
8157 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8158 parsing `3 * 4'. */
8159 if (new_prec <= current.prec)
8161 if (sp == stack)
8162 break;
8163 else
8164 goto pop;
8167 get_rhs:
8168 current.tree_type = binops_by_token[token->type].tree_type;
8169 current.loc = token->location;
8171 /* We used the operator token. */
8172 cp_lexer_consume_token (parser->lexer);
8174 /* For "false && x" or "true || x", x will never be executed;
8175 disable warnings while evaluating it. */
8176 if (current.tree_type == TRUTH_ANDIF_EXPR)
8177 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8178 else if (current.tree_type == TRUTH_ORIF_EXPR)
8179 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8181 /* Extract another operand. It may be the RHS of this expression
8182 or the LHS of a new, higher priority expression. */
8183 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8184 ? TRUTH_NOT_EXPR : ERROR_MARK);
8185 rhs = cp_parser_simple_cast_expression (parser);
8187 /* Get another operator token. Look up its precedence to avoid
8188 building a useless (immediately popped) stack entry for common
8189 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8190 token = cp_lexer_peek_token (parser->lexer);
8191 lookahead_prec = TOKEN_PRECEDENCE (token);
8192 if (lookahead_prec > new_prec)
8194 /* ... and prepare to parse the RHS of the new, higher priority
8195 expression. Since precedence levels on the stack are
8196 monotonically increasing, we do not have to care about
8197 stack overflows. */
8198 *sp = current;
8199 ++sp;
8200 current.lhs = rhs;
8201 current.lhs_type = rhs_type;
8202 current.prec = new_prec;
8203 new_prec = lookahead_prec;
8204 goto get_rhs;
8206 pop:
8207 lookahead_prec = new_prec;
8208 /* If the stack is not empty, we have parsed into LHS the right side
8209 (`4' in the example above) of an expression we had suspended.
8210 We can use the information on the stack to recover the LHS (`3')
8211 from the stack together with the tree code (`MULT_EXPR'), and
8212 the precedence of the higher level subexpression
8213 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8214 which will be used to actually build the additive expression. */
8215 rhs = current.lhs;
8216 rhs_type = current.lhs_type;
8217 --sp;
8218 current = *sp;
8221 /* Undo the disabling of warnings done above. */
8222 if (current.tree_type == TRUTH_ANDIF_EXPR)
8223 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8224 else if (current.tree_type == TRUTH_ORIF_EXPR)
8225 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8227 if (warn_logical_not_paren
8228 && current.lhs_type == TRUTH_NOT_EXPR)
8229 warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8231 overload = NULL;
8232 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8233 ERROR_MARK for everything that is not a binary expression.
8234 This makes warn_about_parentheses miss some warnings that
8235 involve unary operators. For unary expressions we should
8236 pass the correct tree_code unless the unary expression was
8237 surrounded by parentheses.
8239 if (no_toplevel_fold_p
8240 && lookahead_prec <= current.prec
8241 && sp == stack)
8242 current.lhs = build2 (current.tree_type,
8243 TREE_CODE_CLASS (current.tree_type)
8244 == tcc_comparison
8245 ? boolean_type_node : TREE_TYPE (current.lhs),
8246 current.lhs, rhs);
8247 else
8248 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8249 current.lhs, current.lhs_type,
8250 rhs, rhs_type, &overload,
8251 complain_flags (decltype_p));
8252 current.lhs_type = current.tree_type;
8253 if (EXPR_P (current.lhs))
8254 SET_EXPR_LOCATION (current.lhs, current.loc);
8256 /* If the binary operator required the use of an overloaded operator,
8257 then this expression cannot be an integral constant-expression.
8258 An overloaded operator can be used even if both operands are
8259 otherwise permissible in an integral constant-expression if at
8260 least one of the operands is of enumeration type. */
8262 if (overload
8263 && cp_parser_non_integral_constant_expression (parser,
8264 NIC_OVERLOADED))
8265 return error_mark_node;
8268 return current.lhs;
8271 static tree
8272 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8273 bool no_toplevel_fold_p,
8274 enum cp_parser_prec prec,
8275 cp_id_kind * pidk)
8277 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8278 /*decltype*/false, prec, pidk);
8281 /* Parse the `? expression : assignment-expression' part of a
8282 conditional-expression. The LOGICAL_OR_EXPR is the
8283 logical-or-expression that started the conditional-expression.
8284 Returns a representation of the entire conditional-expression.
8286 This routine is used by cp_parser_assignment_expression.
8288 ? expression : assignment-expression
8290 GNU Extensions:
8292 ? : assignment-expression */
8294 static tree
8295 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8297 tree expr;
8298 tree assignment_expr;
8299 struct cp_token *token;
8300 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8302 /* Consume the `?' token. */
8303 cp_lexer_consume_token (parser->lexer);
8304 token = cp_lexer_peek_token (parser->lexer);
8305 if (cp_parser_allow_gnu_extensions_p (parser)
8306 && token->type == CPP_COLON)
8308 pedwarn (token->location, OPT_Wpedantic,
8309 "ISO C++ does not allow ?: with omitted middle operand");
8310 /* Implicit true clause. */
8311 expr = NULL_TREE;
8312 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8313 warn_for_omitted_condop (token->location, logical_or_expr);
8315 else
8317 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8318 parser->colon_corrects_to_scope_p = false;
8319 /* Parse the expression. */
8320 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8321 expr = cp_parser_expression (parser);
8322 c_inhibit_evaluation_warnings +=
8323 ((logical_or_expr == truthvalue_true_node)
8324 - (logical_or_expr == truthvalue_false_node));
8325 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8328 /* The next token should be a `:'. */
8329 cp_parser_require (parser, CPP_COLON, RT_COLON);
8330 /* Parse the assignment-expression. */
8331 assignment_expr = cp_parser_assignment_expression (parser);
8332 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8334 /* Build the conditional-expression. */
8335 return build_x_conditional_expr (loc, logical_or_expr,
8336 expr,
8337 assignment_expr,
8338 tf_warning_or_error);
8341 /* Parse an assignment-expression.
8343 assignment-expression:
8344 conditional-expression
8345 logical-or-expression assignment-operator assignment_expression
8346 throw-expression
8348 CAST_P is true if this expression is the target of a cast.
8349 DECLTYPE_P is true if this expression is the operand of decltype.
8351 Returns a representation for the expression. */
8353 static tree
8354 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8355 bool cast_p, bool decltype_p)
8357 tree expr;
8359 /* If the next token is the `throw' keyword, then we're looking at
8360 a throw-expression. */
8361 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8362 expr = cp_parser_throw_expression (parser);
8363 /* Otherwise, it must be that we are looking at a
8364 logical-or-expression. */
8365 else
8367 /* Parse the binary expressions (logical-or-expression). */
8368 expr = cp_parser_binary_expression (parser, cast_p, false,
8369 decltype_p,
8370 PREC_NOT_OPERATOR, pidk);
8371 /* If the next token is a `?' then we're actually looking at a
8372 conditional-expression. */
8373 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8374 return cp_parser_question_colon_clause (parser, expr);
8375 else
8377 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8379 /* If it's an assignment-operator, we're using the second
8380 production. */
8381 enum tree_code assignment_operator
8382 = cp_parser_assignment_operator_opt (parser);
8383 if (assignment_operator != ERROR_MARK)
8385 bool non_constant_p;
8386 location_t saved_input_location;
8388 /* Parse the right-hand side of the assignment. */
8389 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8391 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8392 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8394 /* An assignment may not appear in a
8395 constant-expression. */
8396 if (cp_parser_non_integral_constant_expression (parser,
8397 NIC_ASSIGNMENT))
8398 return error_mark_node;
8399 /* Build the assignment expression. Its default
8400 location is the location of the '=' token. */
8401 saved_input_location = input_location;
8402 input_location = loc;
8403 expr = build_x_modify_expr (loc, expr,
8404 assignment_operator,
8405 rhs,
8406 complain_flags (decltype_p));
8407 input_location = saved_input_location;
8412 return expr;
8415 /* Parse an (optional) assignment-operator.
8417 assignment-operator: one of
8418 = *= /= %= += -= >>= <<= &= ^= |=
8420 GNU Extension:
8422 assignment-operator: one of
8423 <?= >?=
8425 If the next token is an assignment operator, the corresponding tree
8426 code is returned, and the token is consumed. For example, for
8427 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8428 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8429 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8430 operator, ERROR_MARK is returned. */
8432 static enum tree_code
8433 cp_parser_assignment_operator_opt (cp_parser* parser)
8435 enum tree_code op;
8436 cp_token *token;
8438 /* Peek at the next token. */
8439 token = cp_lexer_peek_token (parser->lexer);
8441 switch (token->type)
8443 case CPP_EQ:
8444 op = NOP_EXPR;
8445 break;
8447 case CPP_MULT_EQ:
8448 op = MULT_EXPR;
8449 break;
8451 case CPP_DIV_EQ:
8452 op = TRUNC_DIV_EXPR;
8453 break;
8455 case CPP_MOD_EQ:
8456 op = TRUNC_MOD_EXPR;
8457 break;
8459 case CPP_PLUS_EQ:
8460 op = PLUS_EXPR;
8461 break;
8463 case CPP_MINUS_EQ:
8464 op = MINUS_EXPR;
8465 break;
8467 case CPP_RSHIFT_EQ:
8468 op = RSHIFT_EXPR;
8469 break;
8471 case CPP_LSHIFT_EQ:
8472 op = LSHIFT_EXPR;
8473 break;
8475 case CPP_AND_EQ:
8476 op = BIT_AND_EXPR;
8477 break;
8479 case CPP_XOR_EQ:
8480 op = BIT_XOR_EXPR;
8481 break;
8483 case CPP_OR_EQ:
8484 op = BIT_IOR_EXPR;
8485 break;
8487 default:
8488 /* Nothing else is an assignment operator. */
8489 op = ERROR_MARK;
8492 /* If it was an assignment operator, consume it. */
8493 if (op != ERROR_MARK)
8494 cp_lexer_consume_token (parser->lexer);
8496 return op;
8499 /* Parse an expression.
8501 expression:
8502 assignment-expression
8503 expression , assignment-expression
8505 CAST_P is true if this expression is the target of a cast.
8506 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8507 except possibly parenthesized or on the RHS of a comma (N3276).
8509 Returns a representation of the expression. */
8511 static tree
8512 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8513 bool cast_p, bool decltype_p)
8515 tree expression = NULL_TREE;
8516 location_t loc = UNKNOWN_LOCATION;
8518 while (true)
8520 tree assignment_expression;
8522 /* Parse the next assignment-expression. */
8523 assignment_expression
8524 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8526 /* We don't create a temporary for a call that is the immediate operand
8527 of decltype or on the RHS of a comma. But when we see a comma, we
8528 need to create a temporary for a call on the LHS. */
8529 if (decltype_p && !processing_template_decl
8530 && TREE_CODE (assignment_expression) == CALL_EXPR
8531 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8532 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8533 assignment_expression
8534 = build_cplus_new (TREE_TYPE (assignment_expression),
8535 assignment_expression, tf_warning_or_error);
8537 /* If this is the first assignment-expression, we can just
8538 save it away. */
8539 if (!expression)
8540 expression = assignment_expression;
8541 else
8542 expression = build_x_compound_expr (loc, expression,
8543 assignment_expression,
8544 complain_flags (decltype_p));
8545 /* If the next token is not a comma, then we are done with the
8546 expression. */
8547 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8548 break;
8549 /* Consume the `,'. */
8550 loc = cp_lexer_peek_token (parser->lexer)->location;
8551 cp_lexer_consume_token (parser->lexer);
8552 /* A comma operator cannot appear in a constant-expression. */
8553 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8554 expression = error_mark_node;
8557 return expression;
8560 /* Parse a constant-expression.
8562 constant-expression:
8563 conditional-expression
8565 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8566 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8567 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8568 is false, NON_CONSTANT_P should be NULL. */
8570 static tree
8571 cp_parser_constant_expression (cp_parser* parser,
8572 bool allow_non_constant_p,
8573 bool *non_constant_p)
8575 bool saved_integral_constant_expression_p;
8576 bool saved_allow_non_integral_constant_expression_p;
8577 bool saved_non_integral_constant_expression_p;
8578 tree expression;
8580 /* It might seem that we could simply parse the
8581 conditional-expression, and then check to see if it were
8582 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8583 one that the compiler can figure out is constant, possibly after
8584 doing some simplifications or optimizations. The standard has a
8585 precise definition of constant-expression, and we must honor
8586 that, even though it is somewhat more restrictive.
8588 For example:
8590 int i[(2, 3)];
8592 is not a legal declaration, because `(2, 3)' is not a
8593 constant-expression. The `,' operator is forbidden in a
8594 constant-expression. However, GCC's constant-folding machinery
8595 will fold this operation to an INTEGER_CST for `3'. */
8597 /* Save the old settings. */
8598 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8599 saved_allow_non_integral_constant_expression_p
8600 = parser->allow_non_integral_constant_expression_p;
8601 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8602 /* We are now parsing a constant-expression. */
8603 parser->integral_constant_expression_p = true;
8604 parser->allow_non_integral_constant_expression_p
8605 = (allow_non_constant_p || cxx_dialect >= cxx11);
8606 parser->non_integral_constant_expression_p = false;
8607 /* Although the grammar says "conditional-expression", we parse an
8608 "assignment-expression", which also permits "throw-expression"
8609 and the use of assignment operators. In the case that
8610 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8611 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8612 actually essential that we look for an assignment-expression.
8613 For example, cp_parser_initializer_clauses uses this function to
8614 determine whether a particular assignment-expression is in fact
8615 constant. */
8616 expression = cp_parser_assignment_expression (parser);
8617 /* Restore the old settings. */
8618 parser->integral_constant_expression_p
8619 = saved_integral_constant_expression_p;
8620 parser->allow_non_integral_constant_expression_p
8621 = saved_allow_non_integral_constant_expression_p;
8622 if (cxx_dialect >= cxx11)
8624 /* Require an rvalue constant expression here; that's what our
8625 callers expect. Reference constant expressions are handled
8626 separately in e.g. cp_parser_template_argument. */
8627 bool is_const = potential_rvalue_constant_expression (expression);
8628 parser->non_integral_constant_expression_p = !is_const;
8629 if (!is_const && !allow_non_constant_p)
8630 require_potential_rvalue_constant_expression (expression);
8632 if (allow_non_constant_p)
8633 *non_constant_p = parser->non_integral_constant_expression_p;
8634 parser->non_integral_constant_expression_p
8635 = saved_non_integral_constant_expression_p;
8637 return expression;
8640 /* Parse __builtin_offsetof.
8642 offsetof-expression:
8643 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8645 offsetof-member-designator:
8646 id-expression
8647 | offsetof-member-designator "." id-expression
8648 | offsetof-member-designator "[" expression "]"
8649 | offsetof-member-designator "->" id-expression */
8651 static tree
8652 cp_parser_builtin_offsetof (cp_parser *parser)
8654 int save_ice_p, save_non_ice_p;
8655 tree type, expr;
8656 cp_id_kind dummy;
8657 cp_token *token;
8659 /* We're about to accept non-integral-constant things, but will
8660 definitely yield an integral constant expression. Save and
8661 restore these values around our local parsing. */
8662 save_ice_p = parser->integral_constant_expression_p;
8663 save_non_ice_p = parser->non_integral_constant_expression_p;
8665 /* Consume the "__builtin_offsetof" token. */
8666 cp_lexer_consume_token (parser->lexer);
8667 /* Consume the opening `('. */
8668 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8669 /* Parse the type-id. */
8670 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8671 type = cp_parser_type_id (parser);
8672 /* Look for the `,'. */
8673 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8674 token = cp_lexer_peek_token (parser->lexer);
8676 /* Build the (type *)null that begins the traditional offsetof macro. */
8677 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8678 tf_warning_or_error);
8680 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8681 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8682 true, &dummy, token->location);
8683 while (true)
8685 token = cp_lexer_peek_token (parser->lexer);
8686 switch (token->type)
8688 case CPP_OPEN_SQUARE:
8689 /* offsetof-member-designator "[" expression "]" */
8690 expr = cp_parser_postfix_open_square_expression (parser, expr,
8691 true, false);
8692 break;
8694 case CPP_DEREF:
8695 /* offsetof-member-designator "->" identifier */
8696 expr = grok_array_decl (token->location, expr,
8697 integer_zero_node, false);
8698 /* FALLTHRU */
8700 case CPP_DOT:
8701 /* offsetof-member-designator "." identifier */
8702 cp_lexer_consume_token (parser->lexer);
8703 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8704 expr, true, &dummy,
8705 token->location);
8706 break;
8708 case CPP_CLOSE_PAREN:
8709 /* Consume the ")" token. */
8710 cp_lexer_consume_token (parser->lexer);
8711 goto success;
8713 default:
8714 /* Error. We know the following require will fail, but
8715 that gives the proper error message. */
8716 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8717 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8718 expr = error_mark_node;
8719 goto failure;
8723 success:
8724 /* If we're processing a template, we can't finish the semantics yet.
8725 Otherwise we can fold the entire expression now. */
8726 if (processing_template_decl)
8728 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8729 SET_EXPR_LOCATION (expr, loc);
8731 else
8732 expr = finish_offsetof (expr, loc);
8734 failure:
8735 parser->integral_constant_expression_p = save_ice_p;
8736 parser->non_integral_constant_expression_p = save_non_ice_p;
8738 return expr;
8741 /* Parse a trait expression.
8743 Returns a representation of the expression, the underlying type
8744 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8746 static tree
8747 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8749 cp_trait_kind kind;
8750 tree type1, type2 = NULL_TREE;
8751 bool binary = false;
8752 bool variadic = false;
8754 switch (keyword)
8756 case RID_HAS_NOTHROW_ASSIGN:
8757 kind = CPTK_HAS_NOTHROW_ASSIGN;
8758 break;
8759 case RID_HAS_NOTHROW_CONSTRUCTOR:
8760 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8761 break;
8762 case RID_HAS_NOTHROW_COPY:
8763 kind = CPTK_HAS_NOTHROW_COPY;
8764 break;
8765 case RID_HAS_TRIVIAL_ASSIGN:
8766 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8767 break;
8768 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8769 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8770 break;
8771 case RID_HAS_TRIVIAL_COPY:
8772 kind = CPTK_HAS_TRIVIAL_COPY;
8773 break;
8774 case RID_HAS_TRIVIAL_DESTRUCTOR:
8775 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8776 break;
8777 case RID_HAS_VIRTUAL_DESTRUCTOR:
8778 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8779 break;
8780 case RID_IS_ABSTRACT:
8781 kind = CPTK_IS_ABSTRACT;
8782 break;
8783 case RID_IS_BASE_OF:
8784 kind = CPTK_IS_BASE_OF;
8785 binary = true;
8786 break;
8787 case RID_IS_CLASS:
8788 kind = CPTK_IS_CLASS;
8789 break;
8790 case RID_IS_EMPTY:
8791 kind = CPTK_IS_EMPTY;
8792 break;
8793 case RID_IS_ENUM:
8794 kind = CPTK_IS_ENUM;
8795 break;
8796 case RID_IS_FINAL:
8797 kind = CPTK_IS_FINAL;
8798 break;
8799 case RID_IS_LITERAL_TYPE:
8800 kind = CPTK_IS_LITERAL_TYPE;
8801 break;
8802 case RID_IS_POD:
8803 kind = CPTK_IS_POD;
8804 break;
8805 case RID_IS_POLYMORPHIC:
8806 kind = CPTK_IS_POLYMORPHIC;
8807 break;
8808 case RID_IS_STD_LAYOUT:
8809 kind = CPTK_IS_STD_LAYOUT;
8810 break;
8811 case RID_IS_TRIVIAL:
8812 kind = CPTK_IS_TRIVIAL;
8813 break;
8814 case RID_IS_TRIVIALLY_ASSIGNABLE:
8815 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8816 binary = true;
8817 break;
8818 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8819 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8820 variadic = true;
8821 break;
8822 case RID_IS_TRIVIALLY_COPYABLE:
8823 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8824 break;
8825 case RID_IS_UNION:
8826 kind = CPTK_IS_UNION;
8827 break;
8828 case RID_UNDERLYING_TYPE:
8829 kind = CPTK_UNDERLYING_TYPE;
8830 break;
8831 case RID_BASES:
8832 kind = CPTK_BASES;
8833 break;
8834 case RID_DIRECT_BASES:
8835 kind = CPTK_DIRECT_BASES;
8836 break;
8837 default:
8838 gcc_unreachable ();
8841 /* Consume the token. */
8842 cp_lexer_consume_token (parser->lexer);
8844 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8846 type1 = cp_parser_type_id (parser);
8848 if (type1 == error_mark_node)
8849 return error_mark_node;
8851 if (binary)
8853 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8855 type2 = cp_parser_type_id (parser);
8857 if (type2 == error_mark_node)
8858 return error_mark_node;
8860 else if (variadic)
8862 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8864 cp_lexer_consume_token (parser->lexer);
8865 tree elt = cp_parser_type_id (parser);
8866 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8868 cp_lexer_consume_token (parser->lexer);
8869 elt = make_pack_expansion (elt);
8871 if (elt == error_mark_node)
8872 return error_mark_node;
8873 type2 = tree_cons (NULL_TREE, elt, type2);
8877 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8879 /* Complete the trait expression, which may mean either processing
8880 the trait expr now or saving it for template instantiation. */
8881 switch(kind)
8883 case CPTK_UNDERLYING_TYPE:
8884 return finish_underlying_type (type1);
8885 case CPTK_BASES:
8886 return finish_bases (type1, false);
8887 case CPTK_DIRECT_BASES:
8888 return finish_bases (type1, true);
8889 default:
8890 return finish_trait_expr (kind, type1, type2);
8894 /* Lambdas that appear in variable initializer or default argument scope
8895 get that in their mangling, so we need to record it. We might as well
8896 use the count for function and namespace scopes as well. */
8897 static GTY(()) tree lambda_scope;
8898 static GTY(()) int lambda_count;
8899 typedef struct GTY(()) tree_int
8901 tree t;
8902 int i;
8903 } tree_int;
8904 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8906 static void
8907 start_lambda_scope (tree decl)
8909 tree_int ti;
8910 gcc_assert (decl);
8911 /* Once we're inside a function, we ignore other scopes and just push
8912 the function again so that popping works properly. */
8913 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8914 decl = current_function_decl;
8915 ti.t = lambda_scope;
8916 ti.i = lambda_count;
8917 vec_safe_push (lambda_scope_stack, ti);
8918 if (lambda_scope != decl)
8920 /* Don't reset the count if we're still in the same function. */
8921 lambda_scope = decl;
8922 lambda_count = 0;
8926 static void
8927 record_lambda_scope (tree lambda)
8929 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8930 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8933 static void
8934 finish_lambda_scope (void)
8936 tree_int *p = &lambda_scope_stack->last ();
8937 if (lambda_scope != p->t)
8939 lambda_scope = p->t;
8940 lambda_count = p->i;
8942 lambda_scope_stack->pop ();
8945 /* Parse a lambda expression.
8947 lambda-expression:
8948 lambda-introducer lambda-declarator [opt] compound-statement
8950 Returns a representation of the expression. */
8952 static tree
8953 cp_parser_lambda_expression (cp_parser* parser)
8955 tree lambda_expr = build_lambda_expr ();
8956 tree type;
8957 bool ok = true;
8958 cp_token *token = cp_lexer_peek_token (parser->lexer);
8959 cp_token_position start = 0;
8961 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8963 if (cp_unevaluated_operand)
8965 if (!token->error_reported)
8967 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8968 "lambda-expression in unevaluated context");
8969 token->error_reported = true;
8971 ok = false;
8973 else if (parser->in_template_argument_list_p)
8975 if (!token->error_reported)
8977 error_at (token->location, "lambda-expression in template-argument");
8978 token->error_reported = true;
8980 ok = false;
8983 /* We may be in the middle of deferred access check. Disable
8984 it now. */
8985 push_deferring_access_checks (dk_no_deferred);
8987 cp_parser_lambda_introducer (parser, lambda_expr);
8989 type = begin_lambda_type (lambda_expr);
8990 if (type == error_mark_node)
8991 return error_mark_node;
8993 record_lambda_scope (lambda_expr);
8995 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8996 determine_visibility (TYPE_NAME (type));
8998 /* Now that we've started the type, add the capture fields for any
8999 explicit captures. */
9000 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9003 /* Inside the class, surrounding template-parameter-lists do not apply. */
9004 unsigned int saved_num_template_parameter_lists
9005 = parser->num_template_parameter_lists;
9006 unsigned char in_statement = parser->in_statement;
9007 bool in_switch_statement_p = parser->in_switch_statement_p;
9008 bool fully_implicit_function_template_p
9009 = parser->fully_implicit_function_template_p;
9010 tree implicit_template_parms = parser->implicit_template_parms;
9011 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9012 bool auto_is_implicit_function_template_parm_p
9013 = parser->auto_is_implicit_function_template_parm_p;
9015 parser->num_template_parameter_lists = 0;
9016 parser->in_statement = 0;
9017 parser->in_switch_statement_p = false;
9018 parser->fully_implicit_function_template_p = false;
9019 parser->implicit_template_parms = 0;
9020 parser->implicit_template_scope = 0;
9021 parser->auto_is_implicit_function_template_parm_p = false;
9023 /* By virtue of defining a local class, a lambda expression has access to
9024 the private variables of enclosing classes. */
9026 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9028 if (ok)
9030 if (!cp_parser_error_occurred (parser)
9031 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9032 && cp_parser_start_tentative_firewall (parser))
9033 start = token;
9034 cp_parser_lambda_body (parser, lambda_expr);
9036 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9038 if (cp_parser_skip_to_closing_brace (parser))
9039 cp_lexer_consume_token (parser->lexer);
9042 /* The capture list was built up in reverse order; fix that now. */
9043 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9044 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9046 if (ok)
9047 maybe_add_lambda_conv_op (type);
9049 type = finish_struct (type, /*attributes=*/NULL_TREE);
9051 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9052 parser->in_statement = in_statement;
9053 parser->in_switch_statement_p = in_switch_statement_p;
9054 parser->fully_implicit_function_template_p
9055 = fully_implicit_function_template_p;
9056 parser->implicit_template_parms = implicit_template_parms;
9057 parser->implicit_template_scope = implicit_template_scope;
9058 parser->auto_is_implicit_function_template_parm_p
9059 = auto_is_implicit_function_template_parm_p;
9062 pop_deferring_access_checks ();
9064 /* This field is only used during parsing of the lambda. */
9065 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9067 /* This lambda shouldn't have any proxies left at this point. */
9068 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9069 /* And now that we're done, push proxies for an enclosing lambda. */
9070 insert_pending_capture_proxies ();
9072 if (ok)
9073 lambda_expr = build_lambda_object (lambda_expr);
9074 else
9075 lambda_expr = error_mark_node;
9077 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9079 return lambda_expr;
9082 /* Parse the beginning of a lambda expression.
9084 lambda-introducer:
9085 [ lambda-capture [opt] ]
9087 LAMBDA_EXPR is the current representation of the lambda expression. */
9089 static void
9090 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9092 /* Need commas after the first capture. */
9093 bool first = true;
9095 /* Eat the leading `['. */
9096 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9098 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9099 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9100 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9101 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9102 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9103 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9105 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9107 cp_lexer_consume_token (parser->lexer);
9108 first = false;
9111 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9113 cp_token* capture_token;
9114 tree capture_id;
9115 tree capture_init_expr;
9116 cp_id_kind idk = CP_ID_KIND_NONE;
9117 bool explicit_init_p = false;
9119 enum capture_kind_type
9121 BY_COPY,
9122 BY_REFERENCE
9124 enum capture_kind_type capture_kind = BY_COPY;
9126 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9128 error ("expected end of capture-list");
9129 return;
9132 if (first)
9133 first = false;
9134 else
9135 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9137 /* Possibly capture `this'. */
9138 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9140 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9141 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9142 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9143 "with by-copy capture default");
9144 cp_lexer_consume_token (parser->lexer);
9145 add_capture (lambda_expr,
9146 /*id=*/this_identifier,
9147 /*initializer=*/finish_this_expr(),
9148 /*by_reference_p=*/false,
9149 explicit_init_p);
9150 continue;
9153 /* Remember whether we want to capture as a reference or not. */
9154 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9156 capture_kind = BY_REFERENCE;
9157 cp_lexer_consume_token (parser->lexer);
9160 /* Get the identifier. */
9161 capture_token = cp_lexer_peek_token (parser->lexer);
9162 capture_id = cp_parser_identifier (parser);
9164 if (capture_id == error_mark_node)
9165 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9166 delimiters, but I modified this to stop on unnested ']' as well. It
9167 was already changed to stop on unnested '}', so the
9168 "closing_parenthesis" name is no more misleading with my change. */
9170 cp_parser_skip_to_closing_parenthesis (parser,
9171 /*recovering=*/true,
9172 /*or_comma=*/true,
9173 /*consume_paren=*/true);
9174 break;
9177 /* Find the initializer for this capture. */
9178 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9179 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9180 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9182 bool direct, non_constant;
9183 /* An explicit initializer exists. */
9184 if (cxx_dialect < cxx14)
9185 pedwarn (input_location, 0,
9186 "lambda capture initializers "
9187 "only available with -std=c++14 or -std=gnu++14");
9188 capture_init_expr = cp_parser_initializer (parser, &direct,
9189 &non_constant);
9190 explicit_init_p = true;
9191 if (capture_init_expr == NULL_TREE)
9193 error ("empty initializer for lambda init-capture");
9194 capture_init_expr = error_mark_node;
9197 else
9199 const char* error_msg;
9201 /* Turn the identifier into an id-expression. */
9202 capture_init_expr
9203 = cp_parser_lookup_name_simple (parser, capture_id,
9204 capture_token->location);
9206 if (capture_init_expr == error_mark_node)
9208 unqualified_name_lookup_error (capture_id);
9209 continue;
9211 else if (DECL_P (capture_init_expr)
9212 && (!VAR_P (capture_init_expr)
9213 && TREE_CODE (capture_init_expr) != PARM_DECL))
9215 error_at (capture_token->location,
9216 "capture of non-variable %qD ",
9217 capture_init_expr);
9218 inform (0, "%q+#D declared here", capture_init_expr);
9219 continue;
9221 if (VAR_P (capture_init_expr)
9222 && decl_storage_duration (capture_init_expr) != dk_auto)
9224 if (pedwarn (capture_token->location, 0, "capture of variable "
9225 "%qD with non-automatic storage duration",
9226 capture_init_expr))
9227 inform (0, "%q+#D declared here", capture_init_expr);
9228 continue;
9231 capture_init_expr
9232 = finish_id_expression
9233 (capture_id,
9234 capture_init_expr,
9235 parser->scope,
9236 &idk,
9237 /*integral_constant_expression_p=*/false,
9238 /*allow_non_integral_constant_expression_p=*/false,
9239 /*non_integral_constant_expression_p=*/NULL,
9240 /*template_p=*/false,
9241 /*done=*/true,
9242 /*address_p=*/false,
9243 /*template_arg_p=*/false,
9244 &error_msg,
9245 capture_token->location);
9247 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9249 cp_lexer_consume_token (parser->lexer);
9250 capture_init_expr = make_pack_expansion (capture_init_expr);
9252 else
9253 check_for_bare_parameter_packs (capture_init_expr);
9256 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9257 && !explicit_init_p)
9259 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9260 && capture_kind == BY_COPY)
9261 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9262 "of %qD redundant with by-copy capture default",
9263 capture_id);
9264 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9265 && capture_kind == BY_REFERENCE)
9266 pedwarn (capture_token->location, 0, "explicit by-reference "
9267 "capture of %qD redundant with by-reference capture "
9268 "default", capture_id);
9271 add_capture (lambda_expr,
9272 capture_id,
9273 capture_init_expr,
9274 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9275 explicit_init_p);
9278 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9281 /* Parse the (optional) middle of a lambda expression.
9283 lambda-declarator:
9284 < template-parameter-list [opt] >
9285 ( parameter-declaration-clause [opt] )
9286 attribute-specifier [opt]
9287 mutable [opt]
9288 exception-specification [opt]
9289 lambda-return-type-clause [opt]
9291 LAMBDA_EXPR is the current representation of the lambda expression. */
9293 static bool
9294 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9296 /* 5.1.1.4 of the standard says:
9297 If a lambda-expression does not include a lambda-declarator, it is as if
9298 the lambda-declarator were ().
9299 This means an empty parameter list, no attributes, and no exception
9300 specification. */
9301 tree param_list = void_list_node;
9302 tree attributes = NULL_TREE;
9303 tree exception_spec = NULL_TREE;
9304 tree template_param_list = NULL_TREE;
9306 /* The template-parameter-list is optional, but must begin with
9307 an opening angle if present. */
9308 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9310 if (cxx_dialect < cxx14)
9311 pedwarn (parser->lexer->next_token->location, 0,
9312 "lambda templates are only available with "
9313 "-std=c++14 or -std=gnu++14");
9315 cp_lexer_consume_token (parser->lexer);
9317 template_param_list = cp_parser_template_parameter_list (parser);
9319 cp_parser_skip_to_end_of_template_parameter_list (parser);
9321 /* We just processed one more parameter list. */
9322 ++parser->num_template_parameter_lists;
9325 /* The parameter-declaration-clause is optional (unless
9326 template-parameter-list was given), but must begin with an
9327 opening parenthesis if present. */
9328 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9330 cp_lexer_consume_token (parser->lexer);
9332 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9334 /* Parse parameters. */
9335 param_list = cp_parser_parameter_declaration_clause (parser);
9337 /* Default arguments shall not be specified in the
9338 parameter-declaration-clause of a lambda-declarator. */
9339 for (tree t = param_list; t; t = TREE_CHAIN (t))
9340 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9341 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9342 "default argument specified for lambda parameter");
9344 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9346 attributes = cp_parser_attributes_opt (parser);
9348 /* Parse optional `mutable' keyword. */
9349 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9351 cp_lexer_consume_token (parser->lexer);
9352 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9355 /* Parse optional exception specification. */
9356 exception_spec = cp_parser_exception_specification_opt (parser);
9358 /* Parse optional trailing return type. */
9359 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9361 cp_lexer_consume_token (parser->lexer);
9362 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9363 = cp_parser_trailing_type_id (parser);
9366 /* The function parameters must be in scope all the way until after the
9367 trailing-return-type in case of decltype. */
9368 pop_bindings_and_leave_scope ();
9370 else if (template_param_list != NULL_TREE) // generate diagnostic
9371 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9373 /* Create the function call operator.
9375 Messing with declarators like this is no uglier than building up the
9376 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9377 other code. */
9379 cp_decl_specifier_seq return_type_specs;
9380 cp_declarator* declarator;
9381 tree fco;
9382 int quals;
9383 void *p;
9385 clear_decl_specs (&return_type_specs);
9386 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9387 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9388 else
9389 /* Maybe we will deduce the return type later. */
9390 return_type_specs.type = make_auto ();
9392 p = obstack_alloc (&declarator_obstack, 0);
9394 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9395 sfk_none);
9397 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9398 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9399 declarator = make_call_declarator (declarator, param_list, quals,
9400 VIRT_SPEC_UNSPECIFIED,
9401 REF_QUAL_NONE,
9402 exception_spec,
9403 /*late_return_type=*/NULL_TREE);
9404 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9406 fco = grokmethod (&return_type_specs,
9407 declarator,
9408 attributes);
9409 if (fco != error_mark_node)
9411 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9412 DECL_ARTIFICIAL (fco) = 1;
9413 /* Give the object parameter a different name. */
9414 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9415 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9416 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9418 if (template_param_list)
9420 fco = finish_member_template_decl (fco);
9421 finish_template_decl (template_param_list);
9422 --parser->num_template_parameter_lists;
9424 else if (parser->fully_implicit_function_template_p)
9425 fco = finish_fully_implicit_template (parser, fco);
9427 finish_member_declaration (fco);
9429 obstack_free (&declarator_obstack, p);
9431 return (fco != error_mark_node);
9435 /* Parse the body of a lambda expression, which is simply
9437 compound-statement
9439 but which requires special handling.
9440 LAMBDA_EXPR is the current representation of the lambda expression. */
9442 static void
9443 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9445 bool nested = (current_function_decl != NULL_TREE);
9446 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9447 if (nested)
9448 push_function_context ();
9449 else
9450 /* Still increment function_depth so that we don't GC in the
9451 middle of an expression. */
9452 ++function_depth;
9453 /* Clear this in case we're in the middle of a default argument. */
9454 parser->local_variables_forbidden_p = false;
9456 /* Finish the function call operator
9457 - class_specifier
9458 + late_parsing_for_member
9459 + function_definition_after_declarator
9460 + ctor_initializer_opt_and_function_body */
9462 tree fco = lambda_function (lambda_expr);
9463 tree body;
9464 bool done = false;
9465 tree compound_stmt;
9466 tree cap;
9468 /* Let the front end know that we are going to be defining this
9469 function. */
9470 start_preparsed_function (fco,
9471 NULL_TREE,
9472 SF_PRE_PARSED | SF_INCLASS_INLINE);
9474 start_lambda_scope (fco);
9475 body = begin_function_body ();
9477 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9478 goto out;
9480 /* Push the proxies for any explicit captures. */
9481 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9482 cap = TREE_CHAIN (cap))
9483 build_capture_proxy (TREE_PURPOSE (cap));
9485 compound_stmt = begin_compound_stmt (0);
9487 /* 5.1.1.4 of the standard says:
9488 If a lambda-expression does not include a trailing-return-type, it
9489 is as if the trailing-return-type denotes the following type:
9490 * if the compound-statement is of the form
9491 { return attribute-specifier [opt] expression ; }
9492 the type of the returned expression after lvalue-to-rvalue
9493 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9494 (_conv.array_ 4.2), and function-to-pointer conversion
9495 (_conv.func_ 4.3);
9496 * otherwise, void. */
9498 /* In a lambda that has neither a lambda-return-type-clause
9499 nor a deducible form, errors should be reported for return statements
9500 in the body. Since we used void as the placeholder return type, parsing
9501 the body as usual will give such desired behavior. */
9502 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9503 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9504 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9506 tree expr = NULL_TREE;
9507 cp_id_kind idk = CP_ID_KIND_NONE;
9509 /* Parse tentatively in case there's more after the initial return
9510 statement. */
9511 cp_parser_parse_tentatively (parser);
9513 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9515 expr = cp_parser_expression (parser, &idk);
9517 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9518 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9520 if (cp_parser_parse_definitely (parser))
9522 if (!processing_template_decl)
9523 apply_deduced_return_type (fco, lambda_return_type (expr));
9525 /* Will get error here if type not deduced yet. */
9526 finish_return_stmt (expr);
9528 done = true;
9532 if (!done)
9534 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9535 cp_parser_label_declaration (parser);
9536 cp_parser_statement_seq_opt (parser, NULL_TREE);
9537 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9540 finish_compound_stmt (compound_stmt);
9542 out:
9543 finish_function_body (body);
9544 finish_lambda_scope ();
9546 /* Finish the function and generate code for it if necessary. */
9547 tree fn = finish_function (/*inline*/2);
9549 /* Only expand if the call op is not a template. */
9550 if (!DECL_TEMPLATE_INFO (fco))
9551 expand_or_defer_fn (fn);
9554 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9555 if (nested)
9556 pop_function_context();
9557 else
9558 --function_depth;
9561 /* Statements [gram.stmt.stmt] */
9563 /* Parse a statement.
9565 statement:
9566 labeled-statement
9567 expression-statement
9568 compound-statement
9569 selection-statement
9570 iteration-statement
9571 jump-statement
9572 declaration-statement
9573 try-block
9575 C++11:
9577 statement:
9578 labeled-statement
9579 attribute-specifier-seq (opt) expression-statement
9580 attribute-specifier-seq (opt) compound-statement
9581 attribute-specifier-seq (opt) selection-statement
9582 attribute-specifier-seq (opt) iteration-statement
9583 attribute-specifier-seq (opt) jump-statement
9584 declaration-statement
9585 attribute-specifier-seq (opt) try-block
9587 TM Extension:
9589 statement:
9590 atomic-statement
9592 IN_COMPOUND is true when the statement is nested inside a
9593 cp_parser_compound_statement; this matters for certain pragmas.
9595 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9596 is a (possibly labeled) if statement which is not enclosed in braces
9597 and has an else clause. This is used to implement -Wparentheses. */
9599 static void
9600 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9601 bool in_compound, bool *if_p)
9603 tree statement, std_attrs = NULL_TREE;
9604 cp_token *token;
9605 location_t statement_location, attrs_location;
9607 restart:
9608 if (if_p != NULL)
9609 *if_p = false;
9610 /* There is no statement yet. */
9611 statement = NULL_TREE;
9613 saved_token_sentinel saved_tokens (parser->lexer);
9614 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9615 if (c_dialect_objc ())
9616 /* In obj-c++, seeing '[[' might be the either the beginning of
9617 c++11 attributes, or a nested objc-message-expression. So
9618 let's parse the c++11 attributes tentatively. */
9619 cp_parser_parse_tentatively (parser);
9620 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9621 if (c_dialect_objc ())
9623 if (!cp_parser_parse_definitely (parser))
9624 std_attrs = NULL_TREE;
9627 /* Peek at the next token. */
9628 token = cp_lexer_peek_token (parser->lexer);
9629 /* Remember the location of the first token in the statement. */
9630 statement_location = token->location;
9631 /* If this is a keyword, then that will often determine what kind of
9632 statement we have. */
9633 if (token->type == CPP_KEYWORD)
9635 enum rid keyword = token->keyword;
9637 switch (keyword)
9639 case RID_CASE:
9640 case RID_DEFAULT:
9641 /* Looks like a labeled-statement with a case label.
9642 Parse the label, and then use tail recursion to parse
9643 the statement. */
9644 cp_parser_label_for_labeled_statement (parser, std_attrs);
9645 goto restart;
9647 case RID_IF:
9648 case RID_SWITCH:
9649 statement = cp_parser_selection_statement (parser, if_p);
9650 break;
9652 case RID_WHILE:
9653 case RID_DO:
9654 case RID_FOR:
9655 statement = cp_parser_iteration_statement (parser, false);
9656 break;
9658 case RID_CILK_FOR:
9659 if (!flag_cilkplus)
9661 error_at (cp_lexer_peek_token (parser->lexer)->location,
9662 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9663 cp_lexer_consume_token (parser->lexer);
9664 statement = error_mark_node;
9666 else
9667 statement = cp_parser_cilk_for (parser, integer_zero_node);
9668 break;
9670 case RID_BREAK:
9671 case RID_CONTINUE:
9672 case RID_RETURN:
9673 case RID_GOTO:
9674 statement = cp_parser_jump_statement (parser);
9675 break;
9677 case RID_CILK_SYNC:
9678 cp_lexer_consume_token (parser->lexer);
9679 if (flag_cilkplus)
9681 tree sync_expr = build_cilk_sync ();
9682 SET_EXPR_LOCATION (sync_expr,
9683 token->location);
9684 statement = finish_expr_stmt (sync_expr);
9686 else
9688 error_at (token->location, "-fcilkplus must be enabled to use"
9689 " %<_Cilk_sync%>");
9690 statement = error_mark_node;
9692 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9693 break;
9695 /* Objective-C++ exception-handling constructs. */
9696 case RID_AT_TRY:
9697 case RID_AT_CATCH:
9698 case RID_AT_FINALLY:
9699 case RID_AT_SYNCHRONIZED:
9700 case RID_AT_THROW:
9701 statement = cp_parser_objc_statement (parser);
9702 break;
9704 case RID_TRY:
9705 statement = cp_parser_try_block (parser);
9706 break;
9708 case RID_NAMESPACE:
9709 /* This must be a namespace alias definition. */
9710 cp_parser_declaration_statement (parser);
9711 return;
9713 case RID_TRANSACTION_ATOMIC:
9714 case RID_TRANSACTION_RELAXED:
9715 statement = cp_parser_transaction (parser, keyword);
9716 break;
9717 case RID_TRANSACTION_CANCEL:
9718 statement = cp_parser_transaction_cancel (parser);
9719 break;
9721 default:
9722 /* It might be a keyword like `int' that can start a
9723 declaration-statement. */
9724 break;
9727 else if (token->type == CPP_NAME)
9729 /* If the next token is a `:', then we are looking at a
9730 labeled-statement. */
9731 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9732 if (token->type == CPP_COLON)
9734 /* Looks like a labeled-statement with an ordinary label.
9735 Parse the label, and then use tail recursion to parse
9736 the statement. */
9738 cp_parser_label_for_labeled_statement (parser, std_attrs);
9739 goto restart;
9742 /* Anything that starts with a `{' must be a compound-statement. */
9743 else if (token->type == CPP_OPEN_BRACE)
9744 statement = cp_parser_compound_statement (parser, NULL, false, false);
9745 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9746 a statement all its own. */
9747 else if (token->type == CPP_PRAGMA)
9749 /* Only certain OpenMP pragmas are attached to statements, and thus
9750 are considered statements themselves. All others are not. In
9751 the context of a compound, accept the pragma as a "statement" and
9752 return so that we can check for a close brace. Otherwise we
9753 require a real statement and must go back and read one. */
9754 if (in_compound)
9755 cp_parser_pragma (parser, pragma_compound);
9756 else if (!cp_parser_pragma (parser, pragma_stmt))
9757 goto restart;
9758 return;
9760 else if (token->type == CPP_EOF)
9762 cp_parser_error (parser, "expected statement");
9763 return;
9766 /* Everything else must be a declaration-statement or an
9767 expression-statement. Try for the declaration-statement
9768 first, unless we are looking at a `;', in which case we know that
9769 we have an expression-statement. */
9770 if (!statement)
9772 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9774 if (std_attrs != NULL_TREE)
9776 /* Attributes should be parsed as part of the the
9777 declaration, so let's un-parse them. */
9778 saved_tokens.rollback();
9779 std_attrs = NULL_TREE;
9782 cp_parser_parse_tentatively (parser);
9783 /* Try to parse the declaration-statement. */
9784 cp_parser_declaration_statement (parser);
9785 /* If that worked, we're done. */
9786 if (cp_parser_parse_definitely (parser))
9787 return;
9789 /* Look for an expression-statement instead. */
9790 statement = cp_parser_expression_statement (parser, in_statement_expr);
9793 /* Set the line number for the statement. */
9794 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9795 SET_EXPR_LOCATION (statement, statement_location);
9797 /* Note that for now, we don't do anything with c++11 statements
9798 parsed at this level. */
9799 if (std_attrs != NULL_TREE)
9800 warning_at (attrs_location,
9801 OPT_Wattributes,
9802 "attributes at the beginning of statement are ignored");
9805 /* Parse the label for a labeled-statement, i.e.
9807 identifier :
9808 case constant-expression :
9809 default :
9811 GNU Extension:
9812 case constant-expression ... constant-expression : statement
9814 When a label is parsed without errors, the label is added to the
9815 parse tree by the finish_* functions, so this function doesn't
9816 have to return the label. */
9818 static void
9819 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9821 cp_token *token;
9822 tree label = NULL_TREE;
9823 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9825 /* The next token should be an identifier. */
9826 token = cp_lexer_peek_token (parser->lexer);
9827 if (token->type != CPP_NAME
9828 && token->type != CPP_KEYWORD)
9830 cp_parser_error (parser, "expected labeled-statement");
9831 return;
9834 parser->colon_corrects_to_scope_p = false;
9835 switch (token->keyword)
9837 case RID_CASE:
9839 tree expr, expr_hi;
9840 cp_token *ellipsis;
9842 /* Consume the `case' token. */
9843 cp_lexer_consume_token (parser->lexer);
9844 /* Parse the constant-expression. */
9845 expr = cp_parser_constant_expression (parser);
9846 if (check_for_bare_parameter_packs (expr))
9847 expr = error_mark_node;
9849 ellipsis = cp_lexer_peek_token (parser->lexer);
9850 if (ellipsis->type == CPP_ELLIPSIS)
9852 /* Consume the `...' token. */
9853 cp_lexer_consume_token (parser->lexer);
9854 expr_hi = cp_parser_constant_expression (parser);
9855 if (check_for_bare_parameter_packs (expr_hi))
9856 expr_hi = error_mark_node;
9858 /* We don't need to emit warnings here, as the common code
9859 will do this for us. */
9861 else
9862 expr_hi = NULL_TREE;
9864 if (parser->in_switch_statement_p)
9865 finish_case_label (token->location, expr, expr_hi);
9866 else
9867 error_at (token->location,
9868 "case label %qE not within a switch statement",
9869 expr);
9871 break;
9873 case RID_DEFAULT:
9874 /* Consume the `default' token. */
9875 cp_lexer_consume_token (parser->lexer);
9877 if (parser->in_switch_statement_p)
9878 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9879 else
9880 error_at (token->location, "case label not within a switch statement");
9881 break;
9883 default:
9884 /* Anything else must be an ordinary label. */
9885 label = finish_label_stmt (cp_parser_identifier (parser));
9886 break;
9889 /* Require the `:' token. */
9890 cp_parser_require (parser, CPP_COLON, RT_COLON);
9892 /* An ordinary label may optionally be followed by attributes.
9893 However, this is only permitted if the attributes are then
9894 followed by a semicolon. This is because, for backward
9895 compatibility, when parsing
9896 lab: __attribute__ ((unused)) int i;
9897 we want the attribute to attach to "i", not "lab". */
9898 if (label != NULL_TREE
9899 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9901 tree attrs;
9902 cp_parser_parse_tentatively (parser);
9903 attrs = cp_parser_gnu_attributes_opt (parser);
9904 if (attrs == NULL_TREE
9905 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9906 cp_parser_abort_tentative_parse (parser);
9907 else if (!cp_parser_parse_definitely (parser))
9909 else
9910 attributes = chainon (attributes, attrs);
9913 if (attributes != NULL_TREE)
9914 cplus_decl_attributes (&label, attributes, 0);
9916 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9919 /* Parse an expression-statement.
9921 expression-statement:
9922 expression [opt] ;
9924 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9925 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9926 indicates whether this expression-statement is part of an
9927 expression statement. */
9929 static tree
9930 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9932 tree statement = NULL_TREE;
9933 cp_token *token = cp_lexer_peek_token (parser->lexer);
9935 /* If the next token is a ';', then there is no expression
9936 statement. */
9937 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9939 statement = cp_parser_expression (parser);
9940 if (statement == error_mark_node
9941 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9943 cp_parser_skip_to_end_of_block_or_statement (parser);
9944 return error_mark_node;
9948 /* Give a helpful message for "A<T>::type t;" and the like. */
9949 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9950 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9952 if (TREE_CODE (statement) == SCOPE_REF)
9953 error_at (token->location, "need %<typename%> before %qE because "
9954 "%qT is a dependent scope",
9955 statement, TREE_OPERAND (statement, 0));
9956 else if (is_overloaded_fn (statement)
9957 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9959 /* A::A a; */
9960 tree fn = get_first_fn (statement);
9961 error_at (token->location,
9962 "%<%T::%D%> names the constructor, not the type",
9963 DECL_CONTEXT (fn), DECL_NAME (fn));
9967 /* Consume the final `;'. */
9968 cp_parser_consume_semicolon_at_end_of_statement (parser);
9970 if (in_statement_expr
9971 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9972 /* This is the final expression statement of a statement
9973 expression. */
9974 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9975 else if (statement)
9976 statement = finish_expr_stmt (statement);
9978 return statement;
9981 /* Parse a compound-statement.
9983 compound-statement:
9984 { statement-seq [opt] }
9986 GNU extension:
9988 compound-statement:
9989 { label-declaration-seq [opt] statement-seq [opt] }
9991 label-declaration-seq:
9992 label-declaration
9993 label-declaration-seq label-declaration
9995 Returns a tree representing the statement. */
9997 static tree
9998 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9999 bool in_try, bool function_body)
10001 tree compound_stmt;
10003 /* Consume the `{'. */
10004 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10005 return error_mark_node;
10006 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10007 && !function_body && cxx_dialect < cxx14)
10008 pedwarn (input_location, OPT_Wpedantic,
10009 "compound-statement in constexpr function");
10010 /* Begin the compound-statement. */
10011 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10012 /* If the next keyword is `__label__' we have a label declaration. */
10013 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10014 cp_parser_label_declaration (parser);
10015 /* Parse an (optional) statement-seq. */
10016 cp_parser_statement_seq_opt (parser, in_statement_expr);
10017 /* Finish the compound-statement. */
10018 finish_compound_stmt (compound_stmt);
10019 /* Consume the `}'. */
10020 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10022 return compound_stmt;
10025 /* Parse an (optional) statement-seq.
10027 statement-seq:
10028 statement
10029 statement-seq [opt] statement */
10031 static void
10032 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10034 /* Scan statements until there aren't any more. */
10035 while (true)
10037 cp_token *token = cp_lexer_peek_token (parser->lexer);
10039 /* If we are looking at a `}', then we have run out of
10040 statements; the same is true if we have reached the end
10041 of file, or have stumbled upon a stray '@end'. */
10042 if (token->type == CPP_CLOSE_BRACE
10043 || token->type == CPP_EOF
10044 || token->type == CPP_PRAGMA_EOL
10045 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10046 break;
10048 /* If we are in a compound statement and find 'else' then
10049 something went wrong. */
10050 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10052 if (parser->in_statement & IN_IF_STMT)
10053 break;
10054 else
10056 token = cp_lexer_consume_token (parser->lexer);
10057 error_at (token->location, "%<else%> without a previous %<if%>");
10061 /* Parse the statement. */
10062 cp_parser_statement (parser, in_statement_expr, true, NULL);
10066 /* Parse a selection-statement.
10068 selection-statement:
10069 if ( condition ) statement
10070 if ( condition ) statement else statement
10071 switch ( condition ) statement
10073 Returns the new IF_STMT or SWITCH_STMT.
10075 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10076 is a (possibly labeled) if statement which is not enclosed in
10077 braces and has an else clause. This is used to implement
10078 -Wparentheses. */
10080 static tree
10081 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10083 cp_token *token;
10084 enum rid keyword;
10086 if (if_p != NULL)
10087 *if_p = false;
10089 /* Peek at the next token. */
10090 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10092 /* See what kind of keyword it is. */
10093 keyword = token->keyword;
10094 switch (keyword)
10096 case RID_IF:
10097 case RID_SWITCH:
10099 tree statement;
10100 tree condition;
10102 /* Look for the `('. */
10103 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10105 cp_parser_skip_to_end_of_statement (parser);
10106 return error_mark_node;
10109 /* Begin the selection-statement. */
10110 if (keyword == RID_IF)
10111 statement = begin_if_stmt ();
10112 else
10113 statement = begin_switch_stmt ();
10115 /* Parse the condition. */
10116 condition = cp_parser_condition (parser);
10117 /* Look for the `)'. */
10118 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10119 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10120 /*consume_paren=*/true);
10122 if (keyword == RID_IF)
10124 bool nested_if;
10125 unsigned char in_statement;
10127 /* Add the condition. */
10128 finish_if_stmt_cond (condition, statement);
10130 /* Parse the then-clause. */
10131 in_statement = parser->in_statement;
10132 parser->in_statement |= IN_IF_STMT;
10133 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10135 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10136 add_stmt (build_empty_stmt (loc));
10137 cp_lexer_consume_token (parser->lexer);
10138 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10139 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10140 "empty body in an %<if%> statement");
10141 nested_if = false;
10143 else
10144 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10145 parser->in_statement = in_statement;
10147 finish_then_clause (statement);
10149 /* If the next token is `else', parse the else-clause. */
10150 if (cp_lexer_next_token_is_keyword (parser->lexer,
10151 RID_ELSE))
10153 /* Consume the `else' keyword. */
10154 cp_lexer_consume_token (parser->lexer);
10155 begin_else_clause (statement);
10156 /* Parse the else-clause. */
10157 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10159 location_t loc;
10160 loc = cp_lexer_peek_token (parser->lexer)->location;
10161 warning_at (loc,
10162 OPT_Wempty_body, "suggest braces around "
10163 "empty body in an %<else%> statement");
10164 add_stmt (build_empty_stmt (loc));
10165 cp_lexer_consume_token (parser->lexer);
10167 else
10168 cp_parser_implicitly_scoped_statement (parser, NULL);
10170 finish_else_clause (statement);
10172 /* If we are currently parsing a then-clause, then
10173 IF_P will not be NULL. We set it to true to
10174 indicate that this if statement has an else clause.
10175 This may trigger the Wparentheses warning below
10176 when we get back up to the parent if statement. */
10177 if (if_p != NULL)
10178 *if_p = true;
10180 else
10182 /* This if statement does not have an else clause. If
10183 NESTED_IF is true, then the then-clause is an if
10184 statement which does have an else clause. We warn
10185 about the potential ambiguity. */
10186 if (nested_if)
10187 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10188 "suggest explicit braces to avoid ambiguous"
10189 " %<else%>");
10192 /* Now we're all done with the if-statement. */
10193 finish_if_stmt (statement);
10195 else
10197 bool in_switch_statement_p;
10198 unsigned char in_statement;
10200 /* Add the condition. */
10201 finish_switch_cond (condition, statement);
10203 /* Parse the body of the switch-statement. */
10204 in_switch_statement_p = parser->in_switch_statement_p;
10205 in_statement = parser->in_statement;
10206 parser->in_switch_statement_p = true;
10207 parser->in_statement |= IN_SWITCH_STMT;
10208 cp_parser_implicitly_scoped_statement (parser, NULL);
10209 parser->in_switch_statement_p = in_switch_statement_p;
10210 parser->in_statement = in_statement;
10212 /* Now we're all done with the switch-statement. */
10213 finish_switch_stmt (statement);
10216 return statement;
10218 break;
10220 default:
10221 cp_parser_error (parser, "expected selection-statement");
10222 return error_mark_node;
10226 /* Parse a condition.
10228 condition:
10229 expression
10230 type-specifier-seq declarator = initializer-clause
10231 type-specifier-seq declarator braced-init-list
10233 GNU Extension:
10235 condition:
10236 type-specifier-seq declarator asm-specification [opt]
10237 attributes [opt] = assignment-expression
10239 Returns the expression that should be tested. */
10241 static tree
10242 cp_parser_condition (cp_parser* parser)
10244 cp_decl_specifier_seq type_specifiers;
10245 const char *saved_message;
10246 int declares_class_or_enum;
10248 /* Try the declaration first. */
10249 cp_parser_parse_tentatively (parser);
10250 /* New types are not allowed in the type-specifier-seq for a
10251 condition. */
10252 saved_message = parser->type_definition_forbidden_message;
10253 parser->type_definition_forbidden_message
10254 = G_("types may not be defined in conditions");
10255 /* Parse the type-specifier-seq. */
10256 cp_parser_decl_specifier_seq (parser,
10257 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10258 &type_specifiers,
10259 &declares_class_or_enum);
10260 /* Restore the saved message. */
10261 parser->type_definition_forbidden_message = saved_message;
10262 /* If all is well, we might be looking at a declaration. */
10263 if (!cp_parser_error_occurred (parser))
10265 tree decl;
10266 tree asm_specification;
10267 tree attributes;
10268 cp_declarator *declarator;
10269 tree initializer = NULL_TREE;
10271 /* Parse the declarator. */
10272 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10273 /*ctor_dtor_or_conv_p=*/NULL,
10274 /*parenthesized_p=*/NULL,
10275 /*member_p=*/false,
10276 /*friend_p=*/false);
10277 /* Parse the attributes. */
10278 attributes = cp_parser_attributes_opt (parser);
10279 /* Parse the asm-specification. */
10280 asm_specification = cp_parser_asm_specification_opt (parser);
10281 /* If the next token is not an `=' or '{', then we might still be
10282 looking at an expression. For example:
10284 if (A(a).x)
10286 looks like a decl-specifier-seq and a declarator -- but then
10287 there is no `=', so this is an expression. */
10288 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10289 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10290 cp_parser_simulate_error (parser);
10292 /* If we did see an `=' or '{', then we are looking at a declaration
10293 for sure. */
10294 if (cp_parser_parse_definitely (parser))
10296 tree pushed_scope;
10297 bool non_constant_p;
10298 bool flags = LOOKUP_ONLYCONVERTING;
10300 /* Create the declaration. */
10301 decl = start_decl (declarator, &type_specifiers,
10302 /*initialized_p=*/true,
10303 attributes, /*prefix_attributes=*/NULL_TREE,
10304 &pushed_scope);
10306 /* Parse the initializer. */
10307 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10309 initializer = cp_parser_braced_list (parser, &non_constant_p);
10310 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10311 flags = 0;
10313 else
10315 /* Consume the `='. */
10316 cp_parser_require (parser, CPP_EQ, RT_EQ);
10317 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10319 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10320 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10322 /* Process the initializer. */
10323 cp_finish_decl (decl,
10324 initializer, !non_constant_p,
10325 asm_specification,
10326 flags);
10328 if (pushed_scope)
10329 pop_scope (pushed_scope);
10331 return convert_from_reference (decl);
10334 /* If we didn't even get past the declarator successfully, we are
10335 definitely not looking at a declaration. */
10336 else
10337 cp_parser_abort_tentative_parse (parser);
10339 /* Otherwise, we are looking at an expression. */
10340 return cp_parser_expression (parser);
10343 /* Parses a for-statement or range-for-statement until the closing ')',
10344 not included. */
10346 static tree
10347 cp_parser_for (cp_parser *parser, bool ivdep)
10349 tree init, scope, decl;
10350 bool is_range_for;
10352 /* Begin the for-statement. */
10353 scope = begin_for_scope (&init);
10355 /* Parse the initialization. */
10356 is_range_for = cp_parser_for_init_statement (parser, &decl);
10358 if (is_range_for)
10359 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10360 else
10361 return cp_parser_c_for (parser, scope, init, ivdep);
10364 static tree
10365 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10367 /* Normal for loop */
10368 tree condition = NULL_TREE;
10369 tree expression = NULL_TREE;
10370 tree stmt;
10372 stmt = begin_for_stmt (scope, init);
10373 /* The for-init-statement has already been parsed in
10374 cp_parser_for_init_statement, so no work is needed here. */
10375 finish_for_init_stmt (stmt);
10377 /* If there's a condition, process it. */
10378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10379 condition = cp_parser_condition (parser);
10380 else if (ivdep)
10382 cp_parser_error (parser, "missing loop condition in loop with "
10383 "%<GCC ivdep%> pragma");
10384 condition = error_mark_node;
10386 finish_for_cond (condition, stmt, ivdep);
10387 /* Look for the `;'. */
10388 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10390 /* If there's an expression, process it. */
10391 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10392 expression = cp_parser_expression (parser);
10393 finish_for_expr (expression, stmt);
10395 return stmt;
10398 /* Tries to parse a range-based for-statement:
10400 range-based-for:
10401 decl-specifier-seq declarator : expression
10403 The decl-specifier-seq declarator and the `:' are already parsed by
10404 cp_parser_for_init_statement. If processing_template_decl it returns a
10405 newly created RANGE_FOR_STMT; if not, it is converted to a
10406 regular FOR_STMT. */
10408 static tree
10409 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10410 bool ivdep)
10412 tree stmt, range_expr;
10414 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10416 bool expr_non_constant_p;
10417 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10419 else
10420 range_expr = cp_parser_expression (parser);
10422 /* If in template, STMT is converted to a normal for-statement
10423 at instantiation. If not, it is done just ahead. */
10424 if (processing_template_decl)
10426 if (check_for_bare_parameter_packs (range_expr))
10427 range_expr = error_mark_node;
10428 stmt = begin_range_for_stmt (scope, init);
10429 if (ivdep)
10430 RANGE_FOR_IVDEP (stmt) = 1;
10431 finish_range_for_decl (stmt, range_decl, range_expr);
10432 if (!type_dependent_expression_p (range_expr)
10433 /* do_auto_deduction doesn't mess with template init-lists. */
10434 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10435 do_range_for_auto_deduction (range_decl, range_expr);
10437 else
10439 stmt = begin_for_stmt (scope, init);
10440 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10442 return stmt;
10445 /* Subroutine of cp_convert_range_for: given the initializer expression,
10446 builds up the range temporary. */
10448 static tree
10449 build_range_temp (tree range_expr)
10451 tree range_type, range_temp;
10453 /* Find out the type deduced by the declaration
10454 `auto &&__range = range_expr'. */
10455 range_type = cp_build_reference_type (make_auto (), true);
10456 range_type = do_auto_deduction (range_type, range_expr,
10457 type_uses_auto (range_type));
10459 /* Create the __range variable. */
10460 range_temp = build_decl (input_location, VAR_DECL,
10461 get_identifier ("__for_range"), range_type);
10462 TREE_USED (range_temp) = 1;
10463 DECL_ARTIFICIAL (range_temp) = 1;
10465 return range_temp;
10468 /* Used by cp_parser_range_for in template context: we aren't going to
10469 do a full conversion yet, but we still need to resolve auto in the
10470 type of the for-range-declaration if present. This is basically
10471 a shortcut version of cp_convert_range_for. */
10473 static void
10474 do_range_for_auto_deduction (tree decl, tree range_expr)
10476 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10477 if (auto_node)
10479 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10480 range_temp = convert_from_reference (build_range_temp (range_expr));
10481 iter_type = (cp_parser_perform_range_for_lookup
10482 (range_temp, &begin_dummy, &end_dummy));
10483 if (iter_type)
10485 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10486 iter_type);
10487 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10488 tf_warning_or_error);
10489 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10490 iter_decl, auto_node);
10495 /* Converts a range-based for-statement into a normal
10496 for-statement, as per the definition.
10498 for (RANGE_DECL : RANGE_EXPR)
10499 BLOCK
10501 should be equivalent to:
10504 auto &&__range = RANGE_EXPR;
10505 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10506 __begin != __end;
10507 ++__begin)
10509 RANGE_DECL = *__begin;
10510 BLOCK
10514 If RANGE_EXPR is an array:
10515 BEGIN_EXPR = __range
10516 END_EXPR = __range + ARRAY_SIZE(__range)
10517 Else if RANGE_EXPR has a member 'begin' or 'end':
10518 BEGIN_EXPR = __range.begin()
10519 END_EXPR = __range.end()
10520 Else:
10521 BEGIN_EXPR = begin(__range)
10522 END_EXPR = end(__range);
10524 If __range has a member 'begin' but not 'end', or vice versa, we must
10525 still use the second alternative (it will surely fail, however).
10526 When calling begin()/end() in the third alternative we must use
10527 argument dependent lookup, but always considering 'std' as an associated
10528 namespace. */
10530 tree
10531 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10532 bool ivdep)
10534 tree begin, end;
10535 tree iter_type, begin_expr, end_expr;
10536 tree condition, expression;
10538 if (range_decl == error_mark_node || range_expr == error_mark_node)
10539 /* If an error happened previously do nothing or else a lot of
10540 unhelpful errors would be issued. */
10541 begin_expr = end_expr = iter_type = error_mark_node;
10542 else
10544 tree range_temp;
10546 if (TREE_CODE (range_expr) == VAR_DECL
10547 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10548 /* Can't bind a reference to an array of runtime bound. */
10549 range_temp = range_expr;
10550 else
10552 range_temp = build_range_temp (range_expr);
10553 pushdecl (range_temp);
10554 cp_finish_decl (range_temp, range_expr,
10555 /*is_constant_init*/false, NULL_TREE,
10556 LOOKUP_ONLYCONVERTING);
10557 range_temp = convert_from_reference (range_temp);
10559 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10560 &begin_expr, &end_expr);
10563 /* The new for initialization statement. */
10564 begin = build_decl (input_location, VAR_DECL,
10565 get_identifier ("__for_begin"), iter_type);
10566 TREE_USED (begin) = 1;
10567 DECL_ARTIFICIAL (begin) = 1;
10568 pushdecl (begin);
10569 cp_finish_decl (begin, begin_expr,
10570 /*is_constant_init*/false, NULL_TREE,
10571 LOOKUP_ONLYCONVERTING);
10573 end = build_decl (input_location, VAR_DECL,
10574 get_identifier ("__for_end"), iter_type);
10575 TREE_USED (end) = 1;
10576 DECL_ARTIFICIAL (end) = 1;
10577 pushdecl (end);
10578 cp_finish_decl (end, end_expr,
10579 /*is_constant_init*/false, NULL_TREE,
10580 LOOKUP_ONLYCONVERTING);
10582 finish_for_init_stmt (statement);
10584 /* The new for condition. */
10585 condition = build_x_binary_op (input_location, NE_EXPR,
10586 begin, ERROR_MARK,
10587 end, ERROR_MARK,
10588 NULL, tf_warning_or_error);
10589 finish_for_cond (condition, statement, ivdep);
10591 /* The new increment expression. */
10592 expression = finish_unary_op_expr (input_location,
10593 PREINCREMENT_EXPR, begin,
10594 tf_warning_or_error);
10595 finish_for_expr (expression, statement);
10597 /* The declaration is initialized with *__begin inside the loop body. */
10598 cp_finish_decl (range_decl,
10599 build_x_indirect_ref (input_location, begin, RO_NULL,
10600 tf_warning_or_error),
10601 /*is_constant_init*/false, NULL_TREE,
10602 LOOKUP_ONLYCONVERTING);
10604 return statement;
10607 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10608 We need to solve both at the same time because the method used
10609 depends on the existence of members begin or end.
10610 Returns the type deduced for the iterator expression. */
10612 static tree
10613 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10615 if (error_operand_p (range))
10617 *begin = *end = error_mark_node;
10618 return error_mark_node;
10621 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10623 error ("range-based %<for%> expression of type %qT "
10624 "has incomplete type", TREE_TYPE (range));
10625 *begin = *end = error_mark_node;
10626 return error_mark_node;
10628 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10630 /* If RANGE is an array, we will use pointer arithmetic. */
10631 *begin = range;
10632 *end = build_binary_op (input_location, PLUS_EXPR,
10633 range,
10634 array_type_nelts_top (TREE_TYPE (range)),
10636 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10638 else
10640 /* If it is not an array, we must do a bit of magic. */
10641 tree id_begin, id_end;
10642 tree member_begin, member_end;
10644 *begin = *end = error_mark_node;
10646 id_begin = get_identifier ("begin");
10647 id_end = get_identifier ("end");
10648 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10649 /*protect=*/2, /*want_type=*/false,
10650 tf_warning_or_error);
10651 member_end = lookup_member (TREE_TYPE (range), id_end,
10652 /*protect=*/2, /*want_type=*/false,
10653 tf_warning_or_error);
10655 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10657 /* Use the member functions. */
10658 if (member_begin != NULL_TREE)
10659 *begin = cp_parser_range_for_member_function (range, id_begin);
10660 else
10661 error ("range-based %<for%> expression of type %qT has an "
10662 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10664 if (member_end != NULL_TREE)
10665 *end = cp_parser_range_for_member_function (range, id_end);
10666 else
10667 error ("range-based %<for%> expression of type %qT has a "
10668 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10670 else
10672 /* Use global functions with ADL. */
10673 vec<tree, va_gc> *vec;
10674 vec = make_tree_vector ();
10676 vec_safe_push (vec, range);
10678 member_begin = perform_koenig_lookup (id_begin, vec,
10679 tf_warning_or_error);
10680 *begin = finish_call_expr (member_begin, &vec, false, true,
10681 tf_warning_or_error);
10682 member_end = perform_koenig_lookup (id_end, vec,
10683 tf_warning_or_error);
10684 *end = finish_call_expr (member_end, &vec, false, true,
10685 tf_warning_or_error);
10687 release_tree_vector (vec);
10690 /* Last common checks. */
10691 if (*begin == error_mark_node || *end == error_mark_node)
10693 /* If one of the expressions is an error do no more checks. */
10694 *begin = *end = error_mark_node;
10695 return error_mark_node;
10697 else if (type_dependent_expression_p (*begin)
10698 || type_dependent_expression_p (*end))
10699 /* Can happen, when, eg, in a template context, Koenig lookup
10700 can't resolve begin/end (c++/58503). */
10701 return NULL_TREE;
10702 else
10704 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10705 /* The unqualified type of the __begin and __end temporaries should
10706 be the same, as required by the multiple auto declaration. */
10707 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10708 error ("inconsistent begin/end types in range-based %<for%> "
10709 "statement: %qT and %qT",
10710 TREE_TYPE (*begin), TREE_TYPE (*end));
10711 return iter_type;
10716 /* Helper function for cp_parser_perform_range_for_lookup.
10717 Builds a tree for RANGE.IDENTIFIER(). */
10719 static tree
10720 cp_parser_range_for_member_function (tree range, tree identifier)
10722 tree member, res;
10723 vec<tree, va_gc> *vec;
10725 member = finish_class_member_access_expr (range, identifier,
10726 false, tf_warning_or_error);
10727 if (member == error_mark_node)
10728 return error_mark_node;
10730 vec = make_tree_vector ();
10731 res = finish_call_expr (member, &vec,
10732 /*disallow_virtual=*/false,
10733 /*koenig_p=*/false,
10734 tf_warning_or_error);
10735 release_tree_vector (vec);
10736 return res;
10739 /* Parse an iteration-statement.
10741 iteration-statement:
10742 while ( condition ) statement
10743 do statement while ( expression ) ;
10744 for ( for-init-statement condition [opt] ; expression [opt] )
10745 statement
10747 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10749 static tree
10750 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10752 cp_token *token;
10753 enum rid keyword;
10754 tree statement;
10755 unsigned char in_statement;
10757 /* Peek at the next token. */
10758 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10759 if (!token)
10760 return error_mark_node;
10762 /* Remember whether or not we are already within an iteration
10763 statement. */
10764 in_statement = parser->in_statement;
10766 /* See what kind of keyword it is. */
10767 keyword = token->keyword;
10768 switch (keyword)
10770 case RID_WHILE:
10772 tree condition;
10774 /* Begin the while-statement. */
10775 statement = begin_while_stmt ();
10776 /* Look for the `('. */
10777 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10778 /* Parse the condition. */
10779 condition = cp_parser_condition (parser);
10780 finish_while_stmt_cond (condition, statement, ivdep);
10781 /* Look for the `)'. */
10782 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10783 /* Parse the dependent statement. */
10784 parser->in_statement = IN_ITERATION_STMT;
10785 cp_parser_already_scoped_statement (parser);
10786 parser->in_statement = in_statement;
10787 /* We're done with the while-statement. */
10788 finish_while_stmt (statement);
10790 break;
10792 case RID_DO:
10794 tree expression;
10796 /* Begin the do-statement. */
10797 statement = begin_do_stmt ();
10798 /* Parse the body of the do-statement. */
10799 parser->in_statement = IN_ITERATION_STMT;
10800 cp_parser_implicitly_scoped_statement (parser, NULL);
10801 parser->in_statement = in_statement;
10802 finish_do_body (statement);
10803 /* Look for the `while' keyword. */
10804 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10805 /* Look for the `('. */
10806 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10807 /* Parse the expression. */
10808 expression = cp_parser_expression (parser);
10809 /* We're done with the do-statement. */
10810 finish_do_stmt (expression, statement, ivdep);
10811 /* Look for the `)'. */
10812 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10813 /* Look for the `;'. */
10814 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10816 break;
10818 case RID_FOR:
10820 /* Look for the `('. */
10821 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10823 statement = cp_parser_for (parser, ivdep);
10825 /* Look for the `)'. */
10826 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10828 /* Parse the body of the for-statement. */
10829 parser->in_statement = IN_ITERATION_STMT;
10830 cp_parser_already_scoped_statement (parser);
10831 parser->in_statement = in_statement;
10833 /* We're done with the for-statement. */
10834 finish_for_stmt (statement);
10836 break;
10838 default:
10839 cp_parser_error (parser, "expected iteration-statement");
10840 statement = error_mark_node;
10841 break;
10844 return statement;
10847 /* Parse a for-init-statement or the declarator of a range-based-for.
10848 Returns true if a range-based-for declaration is seen.
10850 for-init-statement:
10851 expression-statement
10852 simple-declaration */
10854 static bool
10855 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10857 /* If the next token is a `;', then we have an empty
10858 expression-statement. Grammatically, this is also a
10859 simple-declaration, but an invalid one, because it does not
10860 declare anything. Therefore, if we did not handle this case
10861 specially, we would issue an error message about an invalid
10862 declaration. */
10863 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10865 bool is_range_for = false;
10866 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10868 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10869 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10871 /* N3994 -- for (id : init) ... */
10872 if (cxx_dialect < cxx1z)
10873 pedwarn (input_location, 0, "range-based for loop without a "
10874 "type-specifier only available with "
10875 "-std=c++1z or -std=gnu++1z");
10876 tree name = cp_parser_identifier (parser);
10877 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10878 *decl = build_decl (input_location, VAR_DECL, name, type);
10879 pushdecl (*decl);
10880 cp_lexer_consume_token (parser->lexer);
10881 return true;
10884 /* A colon is used in range-based for. */
10885 parser->colon_corrects_to_scope_p = false;
10887 /* We're going to speculatively look for a declaration, falling back
10888 to an expression, if necessary. */
10889 cp_parser_parse_tentatively (parser);
10890 /* Parse the declaration. */
10891 cp_parser_simple_declaration (parser,
10892 /*function_definition_allowed_p=*/false,
10893 decl);
10894 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10895 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10897 /* It is a range-for, consume the ':' */
10898 cp_lexer_consume_token (parser->lexer);
10899 is_range_for = true;
10900 if (cxx_dialect < cxx11)
10902 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10903 "range-based %<for%> loops only available with "
10904 "-std=c++11 or -std=gnu++11");
10905 *decl = error_mark_node;
10908 else
10909 /* The ';' is not consumed yet because we told
10910 cp_parser_simple_declaration not to. */
10911 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10913 if (cp_parser_parse_definitely (parser))
10914 return is_range_for;
10915 /* If the tentative parse failed, then we shall need to look for an
10916 expression-statement. */
10918 /* If we are here, it is an expression-statement. */
10919 cp_parser_expression_statement (parser, NULL_TREE);
10920 return false;
10923 /* Parse a jump-statement.
10925 jump-statement:
10926 break ;
10927 continue ;
10928 return expression [opt] ;
10929 return braced-init-list ;
10930 goto identifier ;
10932 GNU extension:
10934 jump-statement:
10935 goto * expression ;
10937 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10939 static tree
10940 cp_parser_jump_statement (cp_parser* parser)
10942 tree statement = error_mark_node;
10943 cp_token *token;
10944 enum rid keyword;
10945 unsigned char in_statement;
10947 /* Peek at the next token. */
10948 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10949 if (!token)
10950 return error_mark_node;
10952 /* See what kind of keyword it is. */
10953 keyword = token->keyword;
10954 switch (keyword)
10956 case RID_BREAK:
10957 in_statement = parser->in_statement & ~IN_IF_STMT;
10958 switch (in_statement)
10960 case 0:
10961 error_at (token->location, "break statement not within loop or switch");
10962 break;
10963 default:
10964 gcc_assert ((in_statement & IN_SWITCH_STMT)
10965 || in_statement == IN_ITERATION_STMT);
10966 statement = finish_break_stmt ();
10967 if (in_statement == IN_ITERATION_STMT)
10968 break_maybe_infinite_loop ();
10969 break;
10970 case IN_OMP_BLOCK:
10971 error_at (token->location, "invalid exit from OpenMP structured block");
10972 break;
10973 case IN_OMP_FOR:
10974 error_at (token->location, "break statement used with OpenMP for loop");
10975 break;
10976 case IN_CILK_SIMD_FOR:
10977 error_at (token->location, "break statement used with Cilk Plus for loop");
10978 break;
10980 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10981 break;
10983 case RID_CONTINUE:
10984 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10986 case 0:
10987 error_at (token->location, "continue statement not within a loop");
10988 break;
10989 case IN_CILK_SIMD_FOR:
10990 error_at (token->location,
10991 "continue statement within %<#pragma simd%> loop body");
10992 /* Fall through. */
10993 case IN_ITERATION_STMT:
10994 case IN_OMP_FOR:
10995 statement = finish_continue_stmt ();
10996 break;
10997 case IN_OMP_BLOCK:
10998 error_at (token->location, "invalid exit from OpenMP structured block");
10999 break;
11000 default:
11001 gcc_unreachable ();
11003 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11004 break;
11006 case RID_RETURN:
11008 tree expr;
11009 bool expr_non_constant_p;
11011 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11013 cp_lexer_set_source_position (parser->lexer);
11014 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11015 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11017 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11018 expr = cp_parser_expression (parser);
11019 else
11020 /* If the next token is a `;', then there is no
11021 expression. */
11022 expr = NULL_TREE;
11023 /* Build the return-statement. */
11024 statement = finish_return_stmt (expr);
11025 /* Look for the final `;'. */
11026 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11028 break;
11030 case RID_GOTO:
11031 if (parser->in_function_body
11032 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11034 error ("%<goto%> in %<constexpr%> function");
11035 cp_function_chain->invalid_constexpr = true;
11038 /* Create the goto-statement. */
11039 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11041 /* Issue a warning about this use of a GNU extension. */
11042 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11043 /* Consume the '*' token. */
11044 cp_lexer_consume_token (parser->lexer);
11045 /* Parse the dependent expression. */
11046 finish_goto_stmt (cp_parser_expression (parser));
11048 else
11049 finish_goto_stmt (cp_parser_identifier (parser));
11050 /* Look for the final `;'. */
11051 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11052 break;
11054 default:
11055 cp_parser_error (parser, "expected jump-statement");
11056 break;
11059 return statement;
11062 /* Parse a declaration-statement.
11064 declaration-statement:
11065 block-declaration */
11067 static void
11068 cp_parser_declaration_statement (cp_parser* parser)
11070 void *p;
11072 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11073 p = obstack_alloc (&declarator_obstack, 0);
11075 /* Parse the block-declaration. */
11076 cp_parser_block_declaration (parser, /*statement_p=*/true);
11078 /* Free any declarators allocated. */
11079 obstack_free (&declarator_obstack, p);
11082 /* Some dependent statements (like `if (cond) statement'), are
11083 implicitly in their own scope. In other words, if the statement is
11084 a single statement (as opposed to a compound-statement), it is
11085 none-the-less treated as if it were enclosed in braces. Any
11086 declarations appearing in the dependent statement are out of scope
11087 after control passes that point. This function parses a statement,
11088 but ensures that is in its own scope, even if it is not a
11089 compound-statement.
11091 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11092 is a (possibly labeled) if statement which is not enclosed in
11093 braces and has an else clause. This is used to implement
11094 -Wparentheses.
11096 Returns the new statement. */
11098 static tree
11099 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11101 tree statement;
11103 if (if_p != NULL)
11104 *if_p = false;
11106 /* Mark if () ; with a special NOP_EXPR. */
11107 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11109 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11110 cp_lexer_consume_token (parser->lexer);
11111 statement = add_stmt (build_empty_stmt (loc));
11113 /* if a compound is opened, we simply parse the statement directly. */
11114 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11115 statement = cp_parser_compound_statement (parser, NULL, false, false);
11116 /* If the token is not a `{', then we must take special action. */
11117 else
11119 /* Create a compound-statement. */
11120 statement = begin_compound_stmt (0);
11121 /* Parse the dependent-statement. */
11122 cp_parser_statement (parser, NULL_TREE, false, if_p);
11123 /* Finish the dummy compound-statement. */
11124 finish_compound_stmt (statement);
11127 /* Return the statement. */
11128 return statement;
11131 /* For some dependent statements (like `while (cond) statement'), we
11132 have already created a scope. Therefore, even if the dependent
11133 statement is a compound-statement, we do not want to create another
11134 scope. */
11136 static void
11137 cp_parser_already_scoped_statement (cp_parser* parser)
11139 /* If the token is a `{', then we must take special action. */
11140 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11141 cp_parser_statement (parser, NULL_TREE, false, NULL);
11142 else
11144 /* Avoid calling cp_parser_compound_statement, so that we
11145 don't create a new scope. Do everything else by hand. */
11146 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11147 /* If the next keyword is `__label__' we have a label declaration. */
11148 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11149 cp_parser_label_declaration (parser);
11150 /* Parse an (optional) statement-seq. */
11151 cp_parser_statement_seq_opt (parser, NULL_TREE);
11152 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11156 /* Declarations [gram.dcl.dcl] */
11158 /* Parse an optional declaration-sequence.
11160 declaration-seq:
11161 declaration
11162 declaration-seq declaration */
11164 static void
11165 cp_parser_declaration_seq_opt (cp_parser* parser)
11167 while (true)
11169 cp_token *token;
11171 token = cp_lexer_peek_token (parser->lexer);
11173 if (token->type == CPP_CLOSE_BRACE
11174 || token->type == CPP_EOF
11175 || token->type == CPP_PRAGMA_EOL)
11176 break;
11178 if (token->type == CPP_SEMICOLON)
11180 /* A declaration consisting of a single semicolon is
11181 invalid. Allow it unless we're being pedantic. */
11182 cp_lexer_consume_token (parser->lexer);
11183 if (!in_system_header_at (input_location))
11184 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11185 continue;
11188 /* If we're entering or exiting a region that's implicitly
11189 extern "C", modify the lang context appropriately. */
11190 if (!parser->implicit_extern_c && token->implicit_extern_c)
11192 push_lang_context (lang_name_c);
11193 parser->implicit_extern_c = true;
11195 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11197 pop_lang_context ();
11198 parser->implicit_extern_c = false;
11201 if (token->type == CPP_PRAGMA)
11203 /* A top-level declaration can consist solely of a #pragma.
11204 A nested declaration cannot, so this is done here and not
11205 in cp_parser_declaration. (A #pragma at block scope is
11206 handled in cp_parser_statement.) */
11207 cp_parser_pragma (parser, pragma_external);
11208 continue;
11211 /* Parse the declaration itself. */
11212 cp_parser_declaration (parser);
11216 /* Parse a declaration.
11218 declaration:
11219 block-declaration
11220 function-definition
11221 template-declaration
11222 explicit-instantiation
11223 explicit-specialization
11224 linkage-specification
11225 namespace-definition
11227 GNU extension:
11229 declaration:
11230 __extension__ declaration */
11232 static void
11233 cp_parser_declaration (cp_parser* parser)
11235 cp_token token1;
11236 cp_token token2;
11237 int saved_pedantic;
11238 void *p;
11239 tree attributes = NULL_TREE;
11241 /* Check for the `__extension__' keyword. */
11242 if (cp_parser_extension_opt (parser, &saved_pedantic))
11244 /* Parse the qualified declaration. */
11245 cp_parser_declaration (parser);
11246 /* Restore the PEDANTIC flag. */
11247 pedantic = saved_pedantic;
11249 return;
11252 /* Try to figure out what kind of declaration is present. */
11253 token1 = *cp_lexer_peek_token (parser->lexer);
11255 if (token1.type != CPP_EOF)
11256 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11257 else
11259 token2.type = CPP_EOF;
11260 token2.keyword = RID_MAX;
11263 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11264 p = obstack_alloc (&declarator_obstack, 0);
11266 /* If the next token is `extern' and the following token is a string
11267 literal, then we have a linkage specification. */
11268 if (token1.keyword == RID_EXTERN
11269 && cp_parser_is_pure_string_literal (&token2))
11270 cp_parser_linkage_specification (parser);
11271 /* If the next token is `template', then we have either a template
11272 declaration, an explicit instantiation, or an explicit
11273 specialization. */
11274 else if (token1.keyword == RID_TEMPLATE)
11276 /* `template <>' indicates a template specialization. */
11277 if (token2.type == CPP_LESS
11278 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11279 cp_parser_explicit_specialization (parser);
11280 /* `template <' indicates a template declaration. */
11281 else if (token2.type == CPP_LESS)
11282 cp_parser_template_declaration (parser, /*member_p=*/false);
11283 /* Anything else must be an explicit instantiation. */
11284 else
11285 cp_parser_explicit_instantiation (parser);
11287 /* If the next token is `export', then we have a template
11288 declaration. */
11289 else if (token1.keyword == RID_EXPORT)
11290 cp_parser_template_declaration (parser, /*member_p=*/false);
11291 /* If the next token is `extern', 'static' or 'inline' and the one
11292 after that is `template', we have a GNU extended explicit
11293 instantiation directive. */
11294 else if (cp_parser_allow_gnu_extensions_p (parser)
11295 && (token1.keyword == RID_EXTERN
11296 || token1.keyword == RID_STATIC
11297 || token1.keyword == RID_INLINE)
11298 && token2.keyword == RID_TEMPLATE)
11299 cp_parser_explicit_instantiation (parser);
11300 /* If the next token is `namespace', check for a named or unnamed
11301 namespace definition. */
11302 else if (token1.keyword == RID_NAMESPACE
11303 && (/* A named namespace definition. */
11304 (token2.type == CPP_NAME
11305 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11306 != CPP_EQ))
11307 /* An unnamed namespace definition. */
11308 || token2.type == CPP_OPEN_BRACE
11309 || token2.keyword == RID_ATTRIBUTE))
11310 cp_parser_namespace_definition (parser);
11311 /* An inline (associated) namespace definition. */
11312 else if (token1.keyword == RID_INLINE
11313 && token2.keyword == RID_NAMESPACE)
11314 cp_parser_namespace_definition (parser);
11315 /* Objective-C++ declaration/definition. */
11316 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11317 cp_parser_objc_declaration (parser, NULL_TREE);
11318 else if (c_dialect_objc ()
11319 && token1.keyword == RID_ATTRIBUTE
11320 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11321 cp_parser_objc_declaration (parser, attributes);
11322 /* We must have either a block declaration or a function
11323 definition. */
11324 else
11325 /* Try to parse a block-declaration, or a function-definition. */
11326 cp_parser_block_declaration (parser, /*statement_p=*/false);
11328 /* Free any declarators allocated. */
11329 obstack_free (&declarator_obstack, p);
11332 /* Parse a block-declaration.
11334 block-declaration:
11335 simple-declaration
11336 asm-definition
11337 namespace-alias-definition
11338 using-declaration
11339 using-directive
11341 GNU Extension:
11343 block-declaration:
11344 __extension__ block-declaration
11346 C++0x Extension:
11348 block-declaration:
11349 static_assert-declaration
11351 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11352 part of a declaration-statement. */
11354 static void
11355 cp_parser_block_declaration (cp_parser *parser,
11356 bool statement_p)
11358 cp_token *token1;
11359 int saved_pedantic;
11361 /* Check for the `__extension__' keyword. */
11362 if (cp_parser_extension_opt (parser, &saved_pedantic))
11364 /* Parse the qualified declaration. */
11365 cp_parser_block_declaration (parser, statement_p);
11366 /* Restore the PEDANTIC flag. */
11367 pedantic = saved_pedantic;
11369 return;
11372 /* Peek at the next token to figure out which kind of declaration is
11373 present. */
11374 token1 = cp_lexer_peek_token (parser->lexer);
11376 /* If the next keyword is `asm', we have an asm-definition. */
11377 if (token1->keyword == RID_ASM)
11379 if (statement_p)
11380 cp_parser_commit_to_tentative_parse (parser);
11381 cp_parser_asm_definition (parser);
11383 /* If the next keyword is `namespace', we have a
11384 namespace-alias-definition. */
11385 else if (token1->keyword == RID_NAMESPACE)
11386 cp_parser_namespace_alias_definition (parser);
11387 /* If the next keyword is `using', we have a
11388 using-declaration, a using-directive, or an alias-declaration. */
11389 else if (token1->keyword == RID_USING)
11391 cp_token *token2;
11393 if (statement_p)
11394 cp_parser_commit_to_tentative_parse (parser);
11395 /* If the token after `using' is `namespace', then we have a
11396 using-directive. */
11397 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11398 if (token2->keyword == RID_NAMESPACE)
11399 cp_parser_using_directive (parser);
11400 /* If the second token after 'using' is '=', then we have an
11401 alias-declaration. */
11402 else if (cxx_dialect >= cxx11
11403 && token2->type == CPP_NAME
11404 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11405 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11406 cp_parser_alias_declaration (parser);
11407 /* Otherwise, it's a using-declaration. */
11408 else
11409 cp_parser_using_declaration (parser,
11410 /*access_declaration_p=*/false);
11412 /* If the next keyword is `__label__' we have a misplaced label
11413 declaration. */
11414 else if (token1->keyword == RID_LABEL)
11416 cp_lexer_consume_token (parser->lexer);
11417 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11418 cp_parser_skip_to_end_of_statement (parser);
11419 /* If the next token is now a `;', consume it. */
11420 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11421 cp_lexer_consume_token (parser->lexer);
11423 /* If the next token is `static_assert' we have a static assertion. */
11424 else if (token1->keyword == RID_STATIC_ASSERT)
11425 cp_parser_static_assert (parser, /*member_p=*/false);
11426 /* Anything else must be a simple-declaration. */
11427 else
11428 cp_parser_simple_declaration (parser, !statement_p,
11429 /*maybe_range_for_decl*/NULL);
11432 /* Parse a simple-declaration.
11434 simple-declaration:
11435 decl-specifier-seq [opt] init-declarator-list [opt] ;
11437 init-declarator-list:
11438 init-declarator
11439 init-declarator-list , init-declarator
11441 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11442 function-definition as a simple-declaration.
11444 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11445 parsed declaration if it is an uninitialized single declarator not followed
11446 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11447 if present, will not be consumed. */
11449 static void
11450 cp_parser_simple_declaration (cp_parser* parser,
11451 bool function_definition_allowed_p,
11452 tree *maybe_range_for_decl)
11454 cp_decl_specifier_seq decl_specifiers;
11455 int declares_class_or_enum;
11456 bool saw_declarator;
11458 if (maybe_range_for_decl)
11459 *maybe_range_for_decl = NULL_TREE;
11461 /* Defer access checks until we know what is being declared; the
11462 checks for names appearing in the decl-specifier-seq should be
11463 done as if we were in the scope of the thing being declared. */
11464 push_deferring_access_checks (dk_deferred);
11466 /* Parse the decl-specifier-seq. We have to keep track of whether
11467 or not the decl-specifier-seq declares a named class or
11468 enumeration type, since that is the only case in which the
11469 init-declarator-list is allowed to be empty.
11471 [dcl.dcl]
11473 In a simple-declaration, the optional init-declarator-list can be
11474 omitted only when declaring a class or enumeration, that is when
11475 the decl-specifier-seq contains either a class-specifier, an
11476 elaborated-type-specifier, or an enum-specifier. */
11477 cp_parser_decl_specifier_seq (parser,
11478 CP_PARSER_FLAGS_OPTIONAL,
11479 &decl_specifiers,
11480 &declares_class_or_enum);
11481 /* We no longer need to defer access checks. */
11482 stop_deferring_access_checks ();
11484 /* In a block scope, a valid declaration must always have a
11485 decl-specifier-seq. By not trying to parse declarators, we can
11486 resolve the declaration/expression ambiguity more quickly. */
11487 if (!function_definition_allowed_p
11488 && !decl_specifiers.any_specifiers_p)
11490 cp_parser_error (parser, "expected declaration");
11491 goto done;
11494 /* If the next two tokens are both identifiers, the code is
11495 erroneous. The usual cause of this situation is code like:
11497 T t;
11499 where "T" should name a type -- but does not. */
11500 if (!decl_specifiers.any_type_specifiers_p
11501 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11503 /* If parsing tentatively, we should commit; we really are
11504 looking at a declaration. */
11505 cp_parser_commit_to_tentative_parse (parser);
11506 /* Give up. */
11507 goto done;
11510 /* If we have seen at least one decl-specifier, and the next token
11511 is not a parenthesis, then we must be looking at a declaration.
11512 (After "int (" we might be looking at a functional cast.) */
11513 if (decl_specifiers.any_specifiers_p
11514 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11515 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11516 && !cp_parser_error_occurred (parser))
11517 cp_parser_commit_to_tentative_parse (parser);
11519 /* Keep going until we hit the `;' at the end of the simple
11520 declaration. */
11521 saw_declarator = false;
11522 while (cp_lexer_next_token_is_not (parser->lexer,
11523 CPP_SEMICOLON))
11525 cp_token *token;
11526 bool function_definition_p;
11527 tree decl;
11529 if (saw_declarator)
11531 /* If we are processing next declarator, coma is expected */
11532 token = cp_lexer_peek_token (parser->lexer);
11533 gcc_assert (token->type == CPP_COMMA);
11534 cp_lexer_consume_token (parser->lexer);
11535 if (maybe_range_for_decl)
11536 *maybe_range_for_decl = error_mark_node;
11538 else
11539 saw_declarator = true;
11541 /* Parse the init-declarator. */
11542 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11543 /*checks=*/NULL,
11544 function_definition_allowed_p,
11545 /*member_p=*/false,
11546 declares_class_or_enum,
11547 &function_definition_p,
11548 maybe_range_for_decl);
11549 /* If an error occurred while parsing tentatively, exit quickly.
11550 (That usually happens when in the body of a function; each
11551 statement is treated as a declaration-statement until proven
11552 otherwise.) */
11553 if (cp_parser_error_occurred (parser))
11554 goto done;
11555 /* Handle function definitions specially. */
11556 if (function_definition_p)
11558 /* If the next token is a `,', then we are probably
11559 processing something like:
11561 void f() {}, *p;
11563 which is erroneous. */
11564 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11566 cp_token *token = cp_lexer_peek_token (parser->lexer);
11567 error_at (token->location,
11568 "mixing"
11569 " declarations and function-definitions is forbidden");
11571 /* Otherwise, we're done with the list of declarators. */
11572 else
11574 pop_deferring_access_checks ();
11575 return;
11578 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11579 *maybe_range_for_decl = decl;
11580 /* The next token should be either a `,' or a `;'. */
11581 token = cp_lexer_peek_token (parser->lexer);
11582 /* If it's a `,', there are more declarators to come. */
11583 if (token->type == CPP_COMMA)
11584 /* will be consumed next time around */;
11585 /* If it's a `;', we are done. */
11586 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11587 break;
11588 /* Anything else is an error. */
11589 else
11591 /* If we have already issued an error message we don't need
11592 to issue another one. */
11593 if (decl != error_mark_node
11594 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11595 cp_parser_error (parser, "expected %<,%> or %<;%>");
11596 /* Skip tokens until we reach the end of the statement. */
11597 cp_parser_skip_to_end_of_statement (parser);
11598 /* If the next token is now a `;', consume it. */
11599 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11600 cp_lexer_consume_token (parser->lexer);
11601 goto done;
11603 /* After the first time around, a function-definition is not
11604 allowed -- even if it was OK at first. For example:
11606 int i, f() {}
11608 is not valid. */
11609 function_definition_allowed_p = false;
11612 /* Issue an error message if no declarators are present, and the
11613 decl-specifier-seq does not itself declare a class or
11614 enumeration: [dcl.dcl]/3. */
11615 if (!saw_declarator)
11617 if (cp_parser_declares_only_class_p (parser))
11619 if (!declares_class_or_enum
11620 && decl_specifiers.type
11621 && OVERLOAD_TYPE_P (decl_specifiers.type))
11622 /* Ensure an error is issued anyway when finish_decltype_type,
11623 called via cp_parser_decl_specifier_seq, returns a class or
11624 an enumeration (c++/51786). */
11625 decl_specifiers.type = NULL_TREE;
11626 shadow_tag (&decl_specifiers);
11628 /* Perform any deferred access checks. */
11629 perform_deferred_access_checks (tf_warning_or_error);
11632 /* Consume the `;'. */
11633 if (!maybe_range_for_decl)
11634 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11636 done:
11637 pop_deferring_access_checks ();
11640 /* Parse a decl-specifier-seq.
11642 decl-specifier-seq:
11643 decl-specifier-seq [opt] decl-specifier
11644 decl-specifier attribute-specifier-seq [opt] (C++11)
11646 decl-specifier:
11647 storage-class-specifier
11648 type-specifier
11649 function-specifier
11650 friend
11651 typedef
11653 GNU Extension:
11655 decl-specifier:
11656 attributes
11658 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11660 The parser flags FLAGS is used to control type-specifier parsing.
11662 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11663 flags:
11665 1: one of the decl-specifiers is an elaborated-type-specifier
11666 (i.e., a type declaration)
11667 2: one of the decl-specifiers is an enum-specifier or a
11668 class-specifier (i.e., a type definition)
11672 static void
11673 cp_parser_decl_specifier_seq (cp_parser* parser,
11674 cp_parser_flags flags,
11675 cp_decl_specifier_seq *decl_specs,
11676 int* declares_class_or_enum)
11678 bool constructor_possible_p = !parser->in_declarator_p;
11679 bool found_decl_spec = false;
11680 cp_token *start_token = NULL;
11681 cp_decl_spec ds;
11683 /* Clear DECL_SPECS. */
11684 clear_decl_specs (decl_specs);
11686 /* Assume no class or enumeration type is declared. */
11687 *declares_class_or_enum = 0;
11689 /* Keep reading specifiers until there are no more to read. */
11690 while (true)
11692 bool constructor_p;
11693 cp_token *token;
11694 ds = ds_last;
11696 /* Peek at the next token. */
11697 token = cp_lexer_peek_token (parser->lexer);
11699 /* Save the first token of the decl spec list for error
11700 reporting. */
11701 if (!start_token)
11702 start_token = token;
11703 /* Handle attributes. */
11704 if (cp_next_tokens_can_be_attribute_p (parser))
11706 /* Parse the attributes. */
11707 tree attrs = cp_parser_attributes_opt (parser);
11709 /* In a sequence of declaration specifiers, c++11 attributes
11710 appertain to the type that precede them. In that case
11711 [dcl.spec]/1 says:
11713 The attribute-specifier-seq affects the type only for
11714 the declaration it appears in, not other declarations
11715 involving the same type.
11717 But for now let's force the user to position the
11718 attribute either at the beginning of the declaration or
11719 after the declarator-id, which would clearly mean that it
11720 applies to the declarator. */
11721 if (cxx11_attribute_p (attrs))
11723 if (!found_decl_spec)
11724 /* The c++11 attribute is at the beginning of the
11725 declaration. It appertains to the entity being
11726 declared. */;
11727 else
11729 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11731 /* This is an attribute following a
11732 class-specifier. */
11733 if (decl_specs->type_definition_p)
11734 warn_misplaced_attr_for_class_type (token->location,
11735 decl_specs->type);
11736 attrs = NULL_TREE;
11738 else
11740 decl_specs->std_attributes
11741 = chainon (decl_specs->std_attributes,
11742 attrs);
11743 if (decl_specs->locations[ds_std_attribute] == 0)
11744 decl_specs->locations[ds_std_attribute] = token->location;
11746 continue;
11750 decl_specs->attributes
11751 = chainon (decl_specs->attributes,
11752 attrs);
11753 if (decl_specs->locations[ds_attribute] == 0)
11754 decl_specs->locations[ds_attribute] = token->location;
11755 continue;
11757 /* Assume we will find a decl-specifier keyword. */
11758 found_decl_spec = true;
11759 /* If the next token is an appropriate keyword, we can simply
11760 add it to the list. */
11761 switch (token->keyword)
11763 /* decl-specifier:
11764 friend
11765 constexpr */
11766 case RID_FRIEND:
11767 if (!at_class_scope_p ())
11769 error_at (token->location, "%<friend%> used outside of class");
11770 cp_lexer_purge_token (parser->lexer);
11772 else
11774 ds = ds_friend;
11775 /* Consume the token. */
11776 cp_lexer_consume_token (parser->lexer);
11778 break;
11780 case RID_CONSTEXPR:
11781 ds = ds_constexpr;
11782 cp_lexer_consume_token (parser->lexer);
11783 break;
11785 /* function-specifier:
11786 inline
11787 virtual
11788 explicit */
11789 case RID_INLINE:
11790 case RID_VIRTUAL:
11791 case RID_EXPLICIT:
11792 cp_parser_function_specifier_opt (parser, decl_specs);
11793 break;
11795 /* decl-specifier:
11796 typedef */
11797 case RID_TYPEDEF:
11798 ds = ds_typedef;
11799 /* Consume the token. */
11800 cp_lexer_consume_token (parser->lexer);
11801 /* A constructor declarator cannot appear in a typedef. */
11802 constructor_possible_p = false;
11803 /* The "typedef" keyword can only occur in a declaration; we
11804 may as well commit at this point. */
11805 cp_parser_commit_to_tentative_parse (parser);
11807 if (decl_specs->storage_class != sc_none)
11808 decl_specs->conflicting_specifiers_p = true;
11809 break;
11811 /* storage-class-specifier:
11812 auto
11813 register
11814 static
11815 extern
11816 mutable
11818 GNU Extension:
11819 thread */
11820 case RID_AUTO:
11821 if (cxx_dialect == cxx98)
11823 /* Consume the token. */
11824 cp_lexer_consume_token (parser->lexer);
11826 /* Complain about `auto' as a storage specifier, if
11827 we're complaining about C++0x compatibility. */
11828 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11829 " changes meaning in C++11; please remove it");
11831 /* Set the storage class anyway. */
11832 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11833 token);
11835 else
11836 /* C++0x auto type-specifier. */
11837 found_decl_spec = false;
11838 break;
11840 case RID_REGISTER:
11841 case RID_STATIC:
11842 case RID_EXTERN:
11843 case RID_MUTABLE:
11844 /* Consume the token. */
11845 cp_lexer_consume_token (parser->lexer);
11846 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11847 token);
11848 break;
11849 case RID_THREAD:
11850 /* Consume the token. */
11851 ds = ds_thread;
11852 cp_lexer_consume_token (parser->lexer);
11853 break;
11855 default:
11856 /* We did not yet find a decl-specifier yet. */
11857 found_decl_spec = false;
11858 break;
11861 if (found_decl_spec
11862 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11863 && token->keyword != RID_CONSTEXPR)
11864 error ("decl-specifier invalid in condition");
11866 if (ds != ds_last)
11867 set_and_check_decl_spec_loc (decl_specs, ds, token);
11869 /* Constructors are a special case. The `S' in `S()' is not a
11870 decl-specifier; it is the beginning of the declarator. */
11871 constructor_p
11872 = (!found_decl_spec
11873 && constructor_possible_p
11874 && (cp_parser_constructor_declarator_p
11875 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11877 /* If we don't have a DECL_SPEC yet, then we must be looking at
11878 a type-specifier. */
11879 if (!found_decl_spec && !constructor_p)
11881 int decl_spec_declares_class_or_enum;
11882 bool is_cv_qualifier;
11883 tree type_spec;
11885 type_spec
11886 = cp_parser_type_specifier (parser, flags,
11887 decl_specs,
11888 /*is_declaration=*/true,
11889 &decl_spec_declares_class_or_enum,
11890 &is_cv_qualifier);
11891 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11893 /* If this type-specifier referenced a user-defined type
11894 (a typedef, class-name, etc.), then we can't allow any
11895 more such type-specifiers henceforth.
11897 [dcl.spec]
11899 The longest sequence of decl-specifiers that could
11900 possibly be a type name is taken as the
11901 decl-specifier-seq of a declaration. The sequence shall
11902 be self-consistent as described below.
11904 [dcl.type]
11906 As a general rule, at most one type-specifier is allowed
11907 in the complete decl-specifier-seq of a declaration. The
11908 only exceptions are the following:
11910 -- const or volatile can be combined with any other
11911 type-specifier.
11913 -- signed or unsigned can be combined with char, long,
11914 short, or int.
11916 -- ..
11918 Example:
11920 typedef char* Pc;
11921 void g (const int Pc);
11923 Here, Pc is *not* part of the decl-specifier seq; it's
11924 the declarator. Therefore, once we see a type-specifier
11925 (other than a cv-qualifier), we forbid any additional
11926 user-defined types. We *do* still allow things like `int
11927 int' to be considered a decl-specifier-seq, and issue the
11928 error message later. */
11929 if (type_spec && !is_cv_qualifier)
11930 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11931 /* A constructor declarator cannot follow a type-specifier. */
11932 if (type_spec)
11934 constructor_possible_p = false;
11935 found_decl_spec = true;
11936 if (!is_cv_qualifier)
11937 decl_specs->any_type_specifiers_p = true;
11941 /* If we still do not have a DECL_SPEC, then there are no more
11942 decl-specifiers. */
11943 if (!found_decl_spec)
11944 break;
11946 decl_specs->any_specifiers_p = true;
11947 /* After we see one decl-specifier, further decl-specifiers are
11948 always optional. */
11949 flags |= CP_PARSER_FLAGS_OPTIONAL;
11952 /* Don't allow a friend specifier with a class definition. */
11953 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11954 && (*declares_class_or_enum & 2))
11955 error_at (decl_specs->locations[ds_friend],
11956 "class definition may not be declared a friend");
11959 /* Parse an (optional) storage-class-specifier.
11961 storage-class-specifier:
11962 auto
11963 register
11964 static
11965 extern
11966 mutable
11968 GNU Extension:
11970 storage-class-specifier:
11971 thread
11973 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11975 static tree
11976 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11978 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11980 case RID_AUTO:
11981 if (cxx_dialect != cxx98)
11982 return NULL_TREE;
11983 /* Fall through for C++98. */
11985 case RID_REGISTER:
11986 case RID_STATIC:
11987 case RID_EXTERN:
11988 case RID_MUTABLE:
11989 case RID_THREAD:
11990 /* Consume the token. */
11991 return cp_lexer_consume_token (parser->lexer)->u.value;
11993 default:
11994 return NULL_TREE;
11998 /* Parse an (optional) function-specifier.
12000 function-specifier:
12001 inline
12002 virtual
12003 explicit
12005 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12006 Updates DECL_SPECS, if it is non-NULL. */
12008 static tree
12009 cp_parser_function_specifier_opt (cp_parser* parser,
12010 cp_decl_specifier_seq *decl_specs)
12012 cp_token *token = cp_lexer_peek_token (parser->lexer);
12013 switch (token->keyword)
12015 case RID_INLINE:
12016 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12017 break;
12019 case RID_VIRTUAL:
12020 /* 14.5.2.3 [temp.mem]
12022 A member function template shall not be virtual. */
12023 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12024 error_at (token->location, "templates may not be %<virtual%>");
12025 else
12026 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12027 break;
12029 case RID_EXPLICIT:
12030 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12031 break;
12033 default:
12034 return NULL_TREE;
12037 /* Consume the token. */
12038 return cp_lexer_consume_token (parser->lexer)->u.value;
12041 /* Parse a linkage-specification.
12043 linkage-specification:
12044 extern string-literal { declaration-seq [opt] }
12045 extern string-literal declaration */
12047 static void
12048 cp_parser_linkage_specification (cp_parser* parser)
12050 tree linkage;
12052 /* Look for the `extern' keyword. */
12053 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12055 /* Look for the string-literal. */
12056 linkage = cp_parser_string_literal (parser, false, false);
12058 /* Transform the literal into an identifier. If the literal is a
12059 wide-character string, or contains embedded NULs, then we can't
12060 handle it as the user wants. */
12061 if (strlen (TREE_STRING_POINTER (linkage))
12062 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12064 cp_parser_error (parser, "invalid linkage-specification");
12065 /* Assume C++ linkage. */
12066 linkage = lang_name_cplusplus;
12068 else
12069 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12071 /* We're now using the new linkage. */
12072 push_lang_context (linkage);
12074 /* If the next token is a `{', then we're using the first
12075 production. */
12076 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12078 cp_ensure_no_omp_declare_simd (parser);
12080 /* Consume the `{' token. */
12081 cp_lexer_consume_token (parser->lexer);
12082 /* Parse the declarations. */
12083 cp_parser_declaration_seq_opt (parser);
12084 /* Look for the closing `}'. */
12085 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12087 /* Otherwise, there's just one declaration. */
12088 else
12090 bool saved_in_unbraced_linkage_specification_p;
12092 saved_in_unbraced_linkage_specification_p
12093 = parser->in_unbraced_linkage_specification_p;
12094 parser->in_unbraced_linkage_specification_p = true;
12095 cp_parser_declaration (parser);
12096 parser->in_unbraced_linkage_specification_p
12097 = saved_in_unbraced_linkage_specification_p;
12100 /* We're done with the linkage-specification. */
12101 pop_lang_context ();
12104 /* Parse a static_assert-declaration.
12106 static_assert-declaration:
12107 static_assert ( constant-expression , string-literal ) ;
12109 If MEMBER_P, this static_assert is a class member. */
12111 static void
12112 cp_parser_static_assert(cp_parser *parser, bool member_p)
12114 tree condition;
12115 tree message;
12116 cp_token *token;
12117 location_t saved_loc;
12118 bool dummy;
12120 /* Peek at the `static_assert' token so we can keep track of exactly
12121 where the static assertion started. */
12122 token = cp_lexer_peek_token (parser->lexer);
12123 saved_loc = token->location;
12125 /* Look for the `static_assert' keyword. */
12126 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12127 RT_STATIC_ASSERT))
12128 return;
12130 /* We know we are in a static assertion; commit to any tentative
12131 parse. */
12132 if (cp_parser_parsing_tentatively (parser))
12133 cp_parser_commit_to_tentative_parse (parser);
12135 /* Parse the `(' starting the static assertion condition. */
12136 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12138 /* Parse the constant-expression. Allow a non-constant expression
12139 here in order to give better diagnostics in finish_static_assert. */
12140 condition =
12141 cp_parser_constant_expression (parser,
12142 /*allow_non_constant_p=*/true,
12143 /*non_constant_p=*/&dummy);
12145 /* Parse the separating `,'. */
12146 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12148 /* Parse the string-literal message. */
12149 message = cp_parser_string_literal (parser,
12150 /*translate=*/false,
12151 /*wide_ok=*/true);
12153 /* A `)' completes the static assertion. */
12154 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12155 cp_parser_skip_to_closing_parenthesis (parser,
12156 /*recovering=*/true,
12157 /*or_comma=*/false,
12158 /*consume_paren=*/true);
12160 /* A semicolon terminates the declaration. */
12161 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12163 /* Complete the static assertion, which may mean either processing
12164 the static assert now or saving it for template instantiation. */
12165 finish_static_assert (condition, message, saved_loc, member_p);
12168 /* Parse the expression in decltype ( expression ). */
12170 static tree
12171 cp_parser_decltype_expr (cp_parser *parser,
12172 bool &id_expression_or_member_access_p)
12174 cp_token *id_expr_start_token;
12175 tree expr;
12177 /* First, try parsing an id-expression. */
12178 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12179 cp_parser_parse_tentatively (parser);
12180 expr = cp_parser_id_expression (parser,
12181 /*template_keyword_p=*/false,
12182 /*check_dependency_p=*/true,
12183 /*template_p=*/NULL,
12184 /*declarator_p=*/false,
12185 /*optional_p=*/false);
12187 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12189 bool non_integral_constant_expression_p = false;
12190 tree id_expression = expr;
12191 cp_id_kind idk;
12192 const char *error_msg;
12194 if (identifier_p (expr))
12195 /* Lookup the name we got back from the id-expression. */
12196 expr = cp_parser_lookup_name_simple (parser, expr,
12197 id_expr_start_token->location);
12199 if (expr
12200 && expr != error_mark_node
12201 && TREE_CODE (expr) != TYPE_DECL
12202 && (TREE_CODE (expr) != BIT_NOT_EXPR
12203 || !TYPE_P (TREE_OPERAND (expr, 0)))
12204 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12206 /* Complete lookup of the id-expression. */
12207 expr = (finish_id_expression
12208 (id_expression, expr, parser->scope, &idk,
12209 /*integral_constant_expression_p=*/false,
12210 /*allow_non_integral_constant_expression_p=*/true,
12211 &non_integral_constant_expression_p,
12212 /*template_p=*/false,
12213 /*done=*/true,
12214 /*address_p=*/false,
12215 /*template_arg_p=*/false,
12216 &error_msg,
12217 id_expr_start_token->location));
12219 if (expr == error_mark_node)
12220 /* We found an id-expression, but it was something that we
12221 should not have found. This is an error, not something
12222 we can recover from, so note that we found an
12223 id-expression and we'll recover as gracefully as
12224 possible. */
12225 id_expression_or_member_access_p = true;
12228 if (expr
12229 && expr != error_mark_node
12230 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12231 /* We have an id-expression. */
12232 id_expression_or_member_access_p = true;
12235 if (!id_expression_or_member_access_p)
12237 /* Abort the id-expression parse. */
12238 cp_parser_abort_tentative_parse (parser);
12240 /* Parsing tentatively, again. */
12241 cp_parser_parse_tentatively (parser);
12243 /* Parse a class member access. */
12244 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12245 /*cast_p=*/false, /*decltype*/true,
12246 /*member_access_only_p=*/true, NULL);
12248 if (expr
12249 && expr != error_mark_node
12250 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12251 /* We have an id-expression. */
12252 id_expression_or_member_access_p = true;
12255 if (id_expression_or_member_access_p)
12256 /* We have parsed the complete id-expression or member access. */
12257 cp_parser_parse_definitely (parser);
12258 else
12260 /* Abort our attempt to parse an id-expression or member access
12261 expression. */
12262 cp_parser_abort_tentative_parse (parser);
12264 /* Parse a full expression. */
12265 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12266 /*decltype_p=*/true);
12269 return expr;
12272 /* Parse a `decltype' type. Returns the type.
12274 simple-type-specifier:
12275 decltype ( expression )
12276 C++14 proposal:
12277 decltype ( auto ) */
12279 static tree
12280 cp_parser_decltype (cp_parser *parser)
12282 tree expr;
12283 bool id_expression_or_member_access_p = false;
12284 const char *saved_message;
12285 bool saved_integral_constant_expression_p;
12286 bool saved_non_integral_constant_expression_p;
12287 bool saved_greater_than_is_operator_p;
12288 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12290 if (start_token->type == CPP_DECLTYPE)
12292 /* Already parsed. */
12293 cp_lexer_consume_token (parser->lexer);
12294 return start_token->u.value;
12297 /* Look for the `decltype' token. */
12298 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12299 return error_mark_node;
12301 /* Parse the opening `('. */
12302 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12303 return error_mark_node;
12305 /* decltype (auto) */
12306 if (cxx_dialect >= cxx14
12307 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12309 cp_lexer_consume_token (parser->lexer);
12310 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12311 return error_mark_node;
12312 expr = make_decltype_auto ();
12313 AUTO_IS_DECLTYPE (expr) = true;
12314 goto rewrite;
12317 /* Types cannot be defined in a `decltype' expression. Save away the
12318 old message. */
12319 saved_message = parser->type_definition_forbidden_message;
12321 /* And create the new one. */
12322 parser->type_definition_forbidden_message
12323 = G_("types may not be defined in %<decltype%> expressions");
12325 /* The restrictions on constant-expressions do not apply inside
12326 decltype expressions. */
12327 saved_integral_constant_expression_p
12328 = parser->integral_constant_expression_p;
12329 saved_non_integral_constant_expression_p
12330 = parser->non_integral_constant_expression_p;
12331 parser->integral_constant_expression_p = false;
12333 /* Within a parenthesized expression, a `>' token is always
12334 the greater-than operator. */
12335 saved_greater_than_is_operator_p
12336 = parser->greater_than_is_operator_p;
12337 parser->greater_than_is_operator_p = true;
12339 /* Do not actually evaluate the expression. */
12340 ++cp_unevaluated_operand;
12342 /* Do not warn about problems with the expression. */
12343 ++c_inhibit_evaluation_warnings;
12345 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12347 /* Go back to evaluating expressions. */
12348 --cp_unevaluated_operand;
12349 --c_inhibit_evaluation_warnings;
12351 /* The `>' token might be the end of a template-id or
12352 template-parameter-list now. */
12353 parser->greater_than_is_operator_p
12354 = saved_greater_than_is_operator_p;
12356 /* Restore the old message and the integral constant expression
12357 flags. */
12358 parser->type_definition_forbidden_message = saved_message;
12359 parser->integral_constant_expression_p
12360 = saved_integral_constant_expression_p;
12361 parser->non_integral_constant_expression_p
12362 = saved_non_integral_constant_expression_p;
12364 /* Parse to the closing `)'. */
12365 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12367 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12368 /*consume_paren=*/true);
12369 return error_mark_node;
12372 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12373 tf_warning_or_error);
12375 rewrite:
12376 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12377 it again. */
12378 start_token->type = CPP_DECLTYPE;
12379 start_token->u.value = expr;
12380 start_token->keyword = RID_MAX;
12381 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12383 return expr;
12386 /* Special member functions [gram.special] */
12388 /* Parse a conversion-function-id.
12390 conversion-function-id:
12391 operator conversion-type-id
12393 Returns an IDENTIFIER_NODE representing the operator. */
12395 static tree
12396 cp_parser_conversion_function_id (cp_parser* parser)
12398 tree type;
12399 tree saved_scope;
12400 tree saved_qualifying_scope;
12401 tree saved_object_scope;
12402 tree pushed_scope = NULL_TREE;
12404 /* Look for the `operator' token. */
12405 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12406 return error_mark_node;
12407 /* When we parse the conversion-type-id, the current scope will be
12408 reset. However, we need that information in able to look up the
12409 conversion function later, so we save it here. */
12410 saved_scope = parser->scope;
12411 saved_qualifying_scope = parser->qualifying_scope;
12412 saved_object_scope = parser->object_scope;
12413 /* We must enter the scope of the class so that the names of
12414 entities declared within the class are available in the
12415 conversion-type-id. For example, consider:
12417 struct S {
12418 typedef int I;
12419 operator I();
12422 S::operator I() { ... }
12424 In order to see that `I' is a type-name in the definition, we
12425 must be in the scope of `S'. */
12426 if (saved_scope)
12427 pushed_scope = push_scope (saved_scope);
12428 /* Parse the conversion-type-id. */
12429 type = cp_parser_conversion_type_id (parser);
12430 /* Leave the scope of the class, if any. */
12431 if (pushed_scope)
12432 pop_scope (pushed_scope);
12433 /* Restore the saved scope. */
12434 parser->scope = saved_scope;
12435 parser->qualifying_scope = saved_qualifying_scope;
12436 parser->object_scope = saved_object_scope;
12437 /* If the TYPE is invalid, indicate failure. */
12438 if (type == error_mark_node)
12439 return error_mark_node;
12440 return mangle_conv_op_name_for_type (type);
12443 /* Parse a conversion-type-id:
12445 conversion-type-id:
12446 type-specifier-seq conversion-declarator [opt]
12448 Returns the TYPE specified. */
12450 static tree
12451 cp_parser_conversion_type_id (cp_parser* parser)
12453 tree attributes;
12454 cp_decl_specifier_seq type_specifiers;
12455 cp_declarator *declarator;
12456 tree type_specified;
12457 const char *saved_message;
12459 /* Parse the attributes. */
12460 attributes = cp_parser_attributes_opt (parser);
12462 saved_message = parser->type_definition_forbidden_message;
12463 parser->type_definition_forbidden_message
12464 = G_("types may not be defined in a conversion-type-id");
12466 /* Parse the type-specifiers. */
12467 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12468 /*is_trailing_return=*/false,
12469 &type_specifiers);
12471 parser->type_definition_forbidden_message = saved_message;
12473 /* If that didn't work, stop. */
12474 if (type_specifiers.type == error_mark_node)
12475 return error_mark_node;
12476 /* Parse the conversion-declarator. */
12477 declarator = cp_parser_conversion_declarator_opt (parser);
12479 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12480 /*initialized=*/0, &attributes);
12481 if (attributes)
12482 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12484 /* Don't give this error when parsing tentatively. This happens to
12485 work because we always parse this definitively once. */
12486 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12487 && type_uses_auto (type_specified))
12489 if (cxx_dialect < cxx14)
12491 error ("invalid use of %<auto%> in conversion operator");
12492 return error_mark_node;
12494 else if (template_parm_scope_p ())
12495 warning (0, "use of %<auto%> in member template "
12496 "conversion operator can never be deduced");
12499 return type_specified;
12502 /* Parse an (optional) conversion-declarator.
12504 conversion-declarator:
12505 ptr-operator conversion-declarator [opt]
12509 static cp_declarator *
12510 cp_parser_conversion_declarator_opt (cp_parser* parser)
12512 enum tree_code code;
12513 tree class_type, std_attributes = NULL_TREE;
12514 cp_cv_quals cv_quals;
12516 /* We don't know if there's a ptr-operator next, or not. */
12517 cp_parser_parse_tentatively (parser);
12518 /* Try the ptr-operator. */
12519 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12520 &std_attributes);
12521 /* If it worked, look for more conversion-declarators. */
12522 if (cp_parser_parse_definitely (parser))
12524 cp_declarator *declarator;
12526 /* Parse another optional declarator. */
12527 declarator = cp_parser_conversion_declarator_opt (parser);
12529 declarator = cp_parser_make_indirect_declarator
12530 (code, class_type, cv_quals, declarator, std_attributes);
12532 return declarator;
12535 return NULL;
12538 /* Parse an (optional) ctor-initializer.
12540 ctor-initializer:
12541 : mem-initializer-list
12543 Returns TRUE iff the ctor-initializer was actually present. */
12545 static bool
12546 cp_parser_ctor_initializer_opt (cp_parser* parser)
12548 /* If the next token is not a `:', then there is no
12549 ctor-initializer. */
12550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12552 /* Do default initialization of any bases and members. */
12553 if (DECL_CONSTRUCTOR_P (current_function_decl))
12554 finish_mem_initializers (NULL_TREE);
12556 return false;
12559 /* Consume the `:' token. */
12560 cp_lexer_consume_token (parser->lexer);
12561 /* And the mem-initializer-list. */
12562 cp_parser_mem_initializer_list (parser);
12564 return true;
12567 /* Parse a mem-initializer-list.
12569 mem-initializer-list:
12570 mem-initializer ... [opt]
12571 mem-initializer ... [opt] , mem-initializer-list */
12573 static void
12574 cp_parser_mem_initializer_list (cp_parser* parser)
12576 tree mem_initializer_list = NULL_TREE;
12577 tree target_ctor = error_mark_node;
12578 cp_token *token = cp_lexer_peek_token (parser->lexer);
12580 /* Let the semantic analysis code know that we are starting the
12581 mem-initializer-list. */
12582 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12583 error_at (token->location,
12584 "only constructors take member initializers");
12586 /* Loop through the list. */
12587 while (true)
12589 tree mem_initializer;
12591 token = cp_lexer_peek_token (parser->lexer);
12592 /* Parse the mem-initializer. */
12593 mem_initializer = cp_parser_mem_initializer (parser);
12594 /* If the next token is a `...', we're expanding member initializers. */
12595 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12597 /* Consume the `...'. */
12598 cp_lexer_consume_token (parser->lexer);
12600 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12601 can be expanded but members cannot. */
12602 if (mem_initializer != error_mark_node
12603 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12605 error_at (token->location,
12606 "cannot expand initializer for member %<%D%>",
12607 TREE_PURPOSE (mem_initializer));
12608 mem_initializer = error_mark_node;
12611 /* Construct the pack expansion type. */
12612 if (mem_initializer != error_mark_node)
12613 mem_initializer = make_pack_expansion (mem_initializer);
12615 if (target_ctor != error_mark_node
12616 && mem_initializer != error_mark_node)
12618 error ("mem-initializer for %qD follows constructor delegation",
12619 TREE_PURPOSE (mem_initializer));
12620 mem_initializer = error_mark_node;
12622 /* Look for a target constructor. */
12623 if (mem_initializer != error_mark_node
12624 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12625 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12627 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12628 if (mem_initializer_list)
12630 error ("constructor delegation follows mem-initializer for %qD",
12631 TREE_PURPOSE (mem_initializer_list));
12632 mem_initializer = error_mark_node;
12634 target_ctor = mem_initializer;
12636 /* Add it to the list, unless it was erroneous. */
12637 if (mem_initializer != error_mark_node)
12639 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12640 mem_initializer_list = mem_initializer;
12642 /* If the next token is not a `,', we're done. */
12643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12644 break;
12645 /* Consume the `,' token. */
12646 cp_lexer_consume_token (parser->lexer);
12649 /* Perform semantic analysis. */
12650 if (DECL_CONSTRUCTOR_P (current_function_decl))
12651 finish_mem_initializers (mem_initializer_list);
12654 /* Parse a mem-initializer.
12656 mem-initializer:
12657 mem-initializer-id ( expression-list [opt] )
12658 mem-initializer-id braced-init-list
12660 GNU extension:
12662 mem-initializer:
12663 ( expression-list [opt] )
12665 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12666 class) or FIELD_DECL (for a non-static data member) to initialize;
12667 the TREE_VALUE is the expression-list. An empty initialization
12668 list is represented by void_list_node. */
12670 static tree
12671 cp_parser_mem_initializer (cp_parser* parser)
12673 tree mem_initializer_id;
12674 tree expression_list;
12675 tree member;
12676 cp_token *token = cp_lexer_peek_token (parser->lexer);
12678 /* Find out what is being initialized. */
12679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12681 permerror (token->location,
12682 "anachronistic old-style base class initializer");
12683 mem_initializer_id = NULL_TREE;
12685 else
12687 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12688 if (mem_initializer_id == error_mark_node)
12689 return mem_initializer_id;
12691 member = expand_member_init (mem_initializer_id);
12692 if (member && !DECL_P (member))
12693 in_base_initializer = 1;
12695 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12697 bool expr_non_constant_p;
12698 cp_lexer_set_source_position (parser->lexer);
12699 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12700 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12701 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12702 expression_list = build_tree_list (NULL_TREE, expression_list);
12704 else
12706 vec<tree, va_gc> *vec;
12707 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12708 /*cast_p=*/false,
12709 /*allow_expansion_p=*/true,
12710 /*non_constant_p=*/NULL);
12711 if (vec == NULL)
12712 return error_mark_node;
12713 expression_list = build_tree_list_vec (vec);
12714 release_tree_vector (vec);
12717 if (expression_list == error_mark_node)
12718 return error_mark_node;
12719 if (!expression_list)
12720 expression_list = void_type_node;
12722 in_base_initializer = 0;
12724 return member ? build_tree_list (member, expression_list) : error_mark_node;
12727 /* Parse a mem-initializer-id.
12729 mem-initializer-id:
12730 :: [opt] nested-name-specifier [opt] class-name
12731 identifier
12733 Returns a TYPE indicating the class to be initializer for the first
12734 production. Returns an IDENTIFIER_NODE indicating the data member
12735 to be initialized for the second production. */
12737 static tree
12738 cp_parser_mem_initializer_id (cp_parser* parser)
12740 bool global_scope_p;
12741 bool nested_name_specifier_p;
12742 bool template_p = false;
12743 tree id;
12745 cp_token *token = cp_lexer_peek_token (parser->lexer);
12747 /* `typename' is not allowed in this context ([temp.res]). */
12748 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12750 error_at (token->location,
12751 "keyword %<typename%> not allowed in this context (a qualified "
12752 "member initializer is implicitly a type)");
12753 cp_lexer_consume_token (parser->lexer);
12755 /* Look for the optional `::' operator. */
12756 global_scope_p
12757 = (cp_parser_global_scope_opt (parser,
12758 /*current_scope_valid_p=*/false)
12759 != NULL_TREE);
12760 /* Look for the optional nested-name-specifier. The simplest way to
12761 implement:
12763 [temp.res]
12765 The keyword `typename' is not permitted in a base-specifier or
12766 mem-initializer; in these contexts a qualified name that
12767 depends on a template-parameter is implicitly assumed to be a
12768 type name.
12770 is to assume that we have seen the `typename' keyword at this
12771 point. */
12772 nested_name_specifier_p
12773 = (cp_parser_nested_name_specifier_opt (parser,
12774 /*typename_keyword_p=*/true,
12775 /*check_dependency_p=*/true,
12776 /*type_p=*/true,
12777 /*is_declaration=*/true)
12778 != NULL_TREE);
12779 if (nested_name_specifier_p)
12780 template_p = cp_parser_optional_template_keyword (parser);
12781 /* If there is a `::' operator or a nested-name-specifier, then we
12782 are definitely looking for a class-name. */
12783 if (global_scope_p || nested_name_specifier_p)
12784 return cp_parser_class_name (parser,
12785 /*typename_keyword_p=*/true,
12786 /*template_keyword_p=*/template_p,
12787 typename_type,
12788 /*check_dependency_p=*/true,
12789 /*class_head_p=*/false,
12790 /*is_declaration=*/true);
12791 /* Otherwise, we could also be looking for an ordinary identifier. */
12792 cp_parser_parse_tentatively (parser);
12793 /* Try a class-name. */
12794 id = cp_parser_class_name (parser,
12795 /*typename_keyword_p=*/true,
12796 /*template_keyword_p=*/false,
12797 none_type,
12798 /*check_dependency_p=*/true,
12799 /*class_head_p=*/false,
12800 /*is_declaration=*/true);
12801 /* If we found one, we're done. */
12802 if (cp_parser_parse_definitely (parser))
12803 return id;
12804 /* Otherwise, look for an ordinary identifier. */
12805 return cp_parser_identifier (parser);
12808 /* Overloading [gram.over] */
12810 /* Parse an operator-function-id.
12812 operator-function-id:
12813 operator operator
12815 Returns an IDENTIFIER_NODE for the operator which is a
12816 human-readable spelling of the identifier, e.g., `operator +'. */
12818 static tree
12819 cp_parser_operator_function_id (cp_parser* parser)
12821 /* Look for the `operator' keyword. */
12822 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12823 return error_mark_node;
12824 /* And then the name of the operator itself. */
12825 return cp_parser_operator (parser);
12828 /* Return an identifier node for a user-defined literal operator.
12829 The suffix identifier is chained to the operator name identifier. */
12831 static tree
12832 cp_literal_operator_id (const char* name)
12834 tree identifier;
12835 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12836 + strlen (name) + 10);
12837 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12838 identifier = get_identifier (buffer);
12840 return identifier;
12843 /* Parse an operator.
12845 operator:
12846 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12847 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12848 || ++ -- , ->* -> () []
12850 GNU Extensions:
12852 operator:
12853 <? >? <?= >?=
12855 Returns an IDENTIFIER_NODE for the operator which is a
12856 human-readable spelling of the identifier, e.g., `operator +'. */
12858 static tree
12859 cp_parser_operator (cp_parser* parser)
12861 tree id = NULL_TREE;
12862 cp_token *token;
12863 bool utf8 = false;
12865 /* Peek at the next token. */
12866 token = cp_lexer_peek_token (parser->lexer);
12867 /* Figure out which operator we have. */
12868 switch (token->type)
12870 case CPP_KEYWORD:
12872 enum tree_code op;
12874 /* The keyword should be either `new' or `delete'. */
12875 if (token->keyword == RID_NEW)
12876 op = NEW_EXPR;
12877 else if (token->keyword == RID_DELETE)
12878 op = DELETE_EXPR;
12879 else
12880 break;
12882 /* Consume the `new' or `delete' token. */
12883 cp_lexer_consume_token (parser->lexer);
12885 /* Peek at the next token. */
12886 token = cp_lexer_peek_token (parser->lexer);
12887 /* If it's a `[' token then this is the array variant of the
12888 operator. */
12889 if (token->type == CPP_OPEN_SQUARE)
12891 /* Consume the `[' token. */
12892 cp_lexer_consume_token (parser->lexer);
12893 /* Look for the `]' token. */
12894 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12895 id = ansi_opname (op == NEW_EXPR
12896 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12898 /* Otherwise, we have the non-array variant. */
12899 else
12900 id = ansi_opname (op);
12902 return id;
12905 case CPP_PLUS:
12906 id = ansi_opname (PLUS_EXPR);
12907 break;
12909 case CPP_MINUS:
12910 id = ansi_opname (MINUS_EXPR);
12911 break;
12913 case CPP_MULT:
12914 id = ansi_opname (MULT_EXPR);
12915 break;
12917 case CPP_DIV:
12918 id = ansi_opname (TRUNC_DIV_EXPR);
12919 break;
12921 case CPP_MOD:
12922 id = ansi_opname (TRUNC_MOD_EXPR);
12923 break;
12925 case CPP_XOR:
12926 id = ansi_opname (BIT_XOR_EXPR);
12927 break;
12929 case CPP_AND:
12930 id = ansi_opname (BIT_AND_EXPR);
12931 break;
12933 case CPP_OR:
12934 id = ansi_opname (BIT_IOR_EXPR);
12935 break;
12937 case CPP_COMPL:
12938 id = ansi_opname (BIT_NOT_EXPR);
12939 break;
12941 case CPP_NOT:
12942 id = ansi_opname (TRUTH_NOT_EXPR);
12943 break;
12945 case CPP_EQ:
12946 id = ansi_assopname (NOP_EXPR);
12947 break;
12949 case CPP_LESS:
12950 id = ansi_opname (LT_EXPR);
12951 break;
12953 case CPP_GREATER:
12954 id = ansi_opname (GT_EXPR);
12955 break;
12957 case CPP_PLUS_EQ:
12958 id = ansi_assopname (PLUS_EXPR);
12959 break;
12961 case CPP_MINUS_EQ:
12962 id = ansi_assopname (MINUS_EXPR);
12963 break;
12965 case CPP_MULT_EQ:
12966 id = ansi_assopname (MULT_EXPR);
12967 break;
12969 case CPP_DIV_EQ:
12970 id = ansi_assopname (TRUNC_DIV_EXPR);
12971 break;
12973 case CPP_MOD_EQ:
12974 id = ansi_assopname (TRUNC_MOD_EXPR);
12975 break;
12977 case CPP_XOR_EQ:
12978 id = ansi_assopname (BIT_XOR_EXPR);
12979 break;
12981 case CPP_AND_EQ:
12982 id = ansi_assopname (BIT_AND_EXPR);
12983 break;
12985 case CPP_OR_EQ:
12986 id = ansi_assopname (BIT_IOR_EXPR);
12987 break;
12989 case CPP_LSHIFT:
12990 id = ansi_opname (LSHIFT_EXPR);
12991 break;
12993 case CPP_RSHIFT:
12994 id = ansi_opname (RSHIFT_EXPR);
12995 break;
12997 case CPP_LSHIFT_EQ:
12998 id = ansi_assopname (LSHIFT_EXPR);
12999 break;
13001 case CPP_RSHIFT_EQ:
13002 id = ansi_assopname (RSHIFT_EXPR);
13003 break;
13005 case CPP_EQ_EQ:
13006 id = ansi_opname (EQ_EXPR);
13007 break;
13009 case CPP_NOT_EQ:
13010 id = ansi_opname (NE_EXPR);
13011 break;
13013 case CPP_LESS_EQ:
13014 id = ansi_opname (LE_EXPR);
13015 break;
13017 case CPP_GREATER_EQ:
13018 id = ansi_opname (GE_EXPR);
13019 break;
13021 case CPP_AND_AND:
13022 id = ansi_opname (TRUTH_ANDIF_EXPR);
13023 break;
13025 case CPP_OR_OR:
13026 id = ansi_opname (TRUTH_ORIF_EXPR);
13027 break;
13029 case CPP_PLUS_PLUS:
13030 id = ansi_opname (POSTINCREMENT_EXPR);
13031 break;
13033 case CPP_MINUS_MINUS:
13034 id = ansi_opname (PREDECREMENT_EXPR);
13035 break;
13037 case CPP_COMMA:
13038 id = ansi_opname (COMPOUND_EXPR);
13039 break;
13041 case CPP_DEREF_STAR:
13042 id = ansi_opname (MEMBER_REF);
13043 break;
13045 case CPP_DEREF:
13046 id = ansi_opname (COMPONENT_REF);
13047 break;
13049 case CPP_OPEN_PAREN:
13050 /* Consume the `('. */
13051 cp_lexer_consume_token (parser->lexer);
13052 /* Look for the matching `)'. */
13053 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13054 return ansi_opname (CALL_EXPR);
13056 case CPP_OPEN_SQUARE:
13057 /* Consume the `['. */
13058 cp_lexer_consume_token (parser->lexer);
13059 /* Look for the matching `]'. */
13060 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13061 return ansi_opname (ARRAY_REF);
13063 case CPP_UTF8STRING:
13064 case CPP_UTF8STRING_USERDEF:
13065 utf8 = true;
13066 case CPP_STRING:
13067 case CPP_WSTRING:
13068 case CPP_STRING16:
13069 case CPP_STRING32:
13070 case CPP_STRING_USERDEF:
13071 case CPP_WSTRING_USERDEF:
13072 case CPP_STRING16_USERDEF:
13073 case CPP_STRING32_USERDEF:
13075 tree str, string_tree;
13076 int sz, len;
13078 if (cxx_dialect == cxx98)
13079 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13081 /* Consume the string. */
13082 str = cp_parser_string_literal (parser, /*translate=*/true,
13083 /*wide_ok=*/true, /*lookup_udlit=*/false);
13084 if (str == error_mark_node)
13085 return error_mark_node;
13086 else if (TREE_CODE (str) == USERDEF_LITERAL)
13088 string_tree = USERDEF_LITERAL_VALUE (str);
13089 id = USERDEF_LITERAL_SUFFIX_ID (str);
13091 else
13093 string_tree = str;
13094 /* Look for the suffix identifier. */
13095 token = cp_lexer_peek_token (parser->lexer);
13096 if (token->type == CPP_NAME)
13097 id = cp_parser_identifier (parser);
13098 else if (token->type == CPP_KEYWORD)
13100 error ("unexpected keyword;"
13101 " remove space between quotes and suffix identifier");
13102 return error_mark_node;
13104 else
13106 error ("expected suffix identifier");
13107 return error_mark_node;
13110 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13111 (TREE_TYPE (TREE_TYPE (string_tree))));
13112 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13113 if (len != 0)
13115 error ("expected empty string after %<operator%> keyword");
13116 return error_mark_node;
13118 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13119 != char_type_node)
13121 error ("invalid encoding prefix in literal operator");
13122 return error_mark_node;
13124 if (id != error_mark_node)
13126 const char *name = IDENTIFIER_POINTER (id);
13127 id = cp_literal_operator_id (name);
13129 return id;
13132 default:
13133 /* Anything else is an error. */
13134 break;
13137 /* If we have selected an identifier, we need to consume the
13138 operator token. */
13139 if (id)
13140 cp_lexer_consume_token (parser->lexer);
13141 /* Otherwise, no valid operator name was present. */
13142 else
13144 cp_parser_error (parser, "expected operator");
13145 id = error_mark_node;
13148 return id;
13151 /* Parse a template-declaration.
13153 template-declaration:
13154 export [opt] template < template-parameter-list > declaration
13156 If MEMBER_P is TRUE, this template-declaration occurs within a
13157 class-specifier.
13159 The grammar rule given by the standard isn't correct. What
13160 is really meant is:
13162 template-declaration:
13163 export [opt] template-parameter-list-seq
13164 decl-specifier-seq [opt] init-declarator [opt] ;
13165 export [opt] template-parameter-list-seq
13166 function-definition
13168 template-parameter-list-seq:
13169 template-parameter-list-seq [opt]
13170 template < template-parameter-list > */
13172 static void
13173 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13175 /* Check for `export'. */
13176 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13178 /* Consume the `export' token. */
13179 cp_lexer_consume_token (parser->lexer);
13180 /* Warn that we do not support `export'. */
13181 warning (0, "keyword %<export%> not implemented, and will be ignored");
13184 cp_parser_template_declaration_after_export (parser, member_p);
13187 /* Parse a template-parameter-list.
13189 template-parameter-list:
13190 template-parameter
13191 template-parameter-list , template-parameter
13193 Returns a TREE_LIST. Each node represents a template parameter.
13194 The nodes are connected via their TREE_CHAINs. */
13196 static tree
13197 cp_parser_template_parameter_list (cp_parser* parser)
13199 tree parameter_list = NULL_TREE;
13201 begin_template_parm_list ();
13203 /* The loop below parses the template parms. We first need to know
13204 the total number of template parms to be able to compute proper
13205 canonical types of each dependent type. So after the loop, when
13206 we know the total number of template parms,
13207 end_template_parm_list computes the proper canonical types and
13208 fixes up the dependent types accordingly. */
13209 while (true)
13211 tree parameter;
13212 bool is_non_type;
13213 bool is_parameter_pack;
13214 location_t parm_loc;
13216 /* Parse the template-parameter. */
13217 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13218 parameter = cp_parser_template_parameter (parser,
13219 &is_non_type,
13220 &is_parameter_pack);
13221 /* Add it to the list. */
13222 if (parameter != error_mark_node)
13223 parameter_list = process_template_parm (parameter_list,
13224 parm_loc,
13225 parameter,
13226 is_non_type,
13227 is_parameter_pack);
13228 else
13230 tree err_parm = build_tree_list (parameter, parameter);
13231 parameter_list = chainon (parameter_list, err_parm);
13234 /* If the next token is not a `,', we're done. */
13235 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13236 break;
13237 /* Otherwise, consume the `,' token. */
13238 cp_lexer_consume_token (parser->lexer);
13241 return end_template_parm_list (parameter_list);
13244 /* Parse a template-parameter.
13246 template-parameter:
13247 type-parameter
13248 parameter-declaration
13250 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13251 the parameter. The TREE_PURPOSE is the default value, if any.
13252 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13253 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13254 set to true iff this parameter is a parameter pack. */
13256 static tree
13257 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13258 bool *is_parameter_pack)
13260 cp_token *token;
13261 cp_parameter_declarator *parameter_declarator;
13262 cp_declarator *id_declarator;
13263 tree parm;
13265 /* Assume it is a type parameter or a template parameter. */
13266 *is_non_type = false;
13267 /* Assume it not a parameter pack. */
13268 *is_parameter_pack = false;
13269 /* Peek at the next token. */
13270 token = cp_lexer_peek_token (parser->lexer);
13271 /* If it is `class' or `template', we have a type-parameter. */
13272 if (token->keyword == RID_TEMPLATE)
13273 return cp_parser_type_parameter (parser, is_parameter_pack);
13274 /* If it is `class' or `typename' we do not know yet whether it is a
13275 type parameter or a non-type parameter. Consider:
13277 template <typename T, typename T::X X> ...
13281 template <class C, class D*> ...
13283 Here, the first parameter is a type parameter, and the second is
13284 a non-type parameter. We can tell by looking at the token after
13285 the identifier -- if it is a `,', `=', or `>' then we have a type
13286 parameter. */
13287 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13289 /* Peek at the token after `class' or `typename'. */
13290 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13291 /* If it's an ellipsis, we have a template type parameter
13292 pack. */
13293 if (token->type == CPP_ELLIPSIS)
13294 return cp_parser_type_parameter (parser, is_parameter_pack);
13295 /* If it's an identifier, skip it. */
13296 if (token->type == CPP_NAME)
13297 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13298 /* Now, see if the token looks like the end of a template
13299 parameter. */
13300 if (token->type == CPP_COMMA
13301 || token->type == CPP_EQ
13302 || token->type == CPP_GREATER)
13303 return cp_parser_type_parameter (parser, is_parameter_pack);
13306 /* Otherwise, it is a non-type parameter.
13308 [temp.param]
13310 When parsing a default template-argument for a non-type
13311 template-parameter, the first non-nested `>' is taken as the end
13312 of the template parameter-list rather than a greater-than
13313 operator. */
13314 *is_non_type = true;
13315 parameter_declarator
13316 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13317 /*parenthesized_p=*/NULL);
13319 if (!parameter_declarator)
13320 return error_mark_node;
13322 /* If the parameter declaration is marked as a parameter pack, set
13323 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13324 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13325 grokdeclarator. */
13326 if (parameter_declarator->declarator
13327 && parameter_declarator->declarator->parameter_pack_p)
13329 *is_parameter_pack = true;
13330 parameter_declarator->declarator->parameter_pack_p = false;
13333 if (parameter_declarator->default_argument)
13335 /* Can happen in some cases of erroneous input (c++/34892). */
13336 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13337 /* Consume the `...' for better error recovery. */
13338 cp_lexer_consume_token (parser->lexer);
13340 /* If the next token is an ellipsis, and we don't already have it
13341 marked as a parameter pack, then we have a parameter pack (that
13342 has no declarator). */
13343 else if (!*is_parameter_pack
13344 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13345 && (declarator_can_be_parameter_pack
13346 (parameter_declarator->declarator)))
13348 /* Consume the `...'. */
13349 cp_lexer_consume_token (parser->lexer);
13350 maybe_warn_variadic_templates ();
13352 *is_parameter_pack = true;
13354 /* We might end up with a pack expansion as the type of the non-type
13355 template parameter, in which case this is a non-type template
13356 parameter pack. */
13357 else if (parameter_declarator->decl_specifiers.type
13358 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13360 *is_parameter_pack = true;
13361 parameter_declarator->decl_specifiers.type =
13362 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13365 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13367 /* Parameter packs cannot have default arguments. However, a
13368 user may try to do so, so we'll parse them and give an
13369 appropriate diagnostic here. */
13371 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13373 /* Find the name of the parameter pack. */
13374 id_declarator = parameter_declarator->declarator;
13375 while (id_declarator && id_declarator->kind != cdk_id)
13376 id_declarator = id_declarator->declarator;
13378 if (id_declarator && id_declarator->kind == cdk_id)
13379 error_at (start_token->location,
13380 "template parameter pack %qD cannot have a default argument",
13381 id_declarator->u.id.unqualified_name);
13382 else
13383 error_at (start_token->location,
13384 "template parameter pack cannot have a default argument");
13386 /* Parse the default argument, but throw away the result. */
13387 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13390 parm = grokdeclarator (parameter_declarator->declarator,
13391 &parameter_declarator->decl_specifiers,
13392 TPARM, /*initialized=*/0,
13393 /*attrlist=*/NULL);
13394 if (parm == error_mark_node)
13395 return error_mark_node;
13397 return build_tree_list (parameter_declarator->default_argument, parm);
13400 /* Parse a type-parameter.
13402 type-parameter:
13403 class identifier [opt]
13404 class identifier [opt] = type-id
13405 typename identifier [opt]
13406 typename identifier [opt] = type-id
13407 template < template-parameter-list > class identifier [opt]
13408 template < template-parameter-list > class identifier [opt]
13409 = id-expression
13411 GNU Extension (variadic templates):
13413 type-parameter:
13414 class ... identifier [opt]
13415 typename ... identifier [opt]
13417 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13418 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13419 the declaration of the parameter.
13421 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13423 static tree
13424 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13426 cp_token *token;
13427 tree parameter;
13429 /* Look for a keyword to tell us what kind of parameter this is. */
13430 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13431 if (!token)
13432 return error_mark_node;
13434 switch (token->keyword)
13436 case RID_CLASS:
13437 case RID_TYPENAME:
13439 tree identifier;
13440 tree default_argument;
13442 /* If the next token is an ellipsis, we have a template
13443 argument pack. */
13444 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13446 /* Consume the `...' token. */
13447 cp_lexer_consume_token (parser->lexer);
13448 maybe_warn_variadic_templates ();
13450 *is_parameter_pack = true;
13453 /* If the next token is an identifier, then it names the
13454 parameter. */
13455 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13456 identifier = cp_parser_identifier (parser);
13457 else
13458 identifier = NULL_TREE;
13460 /* Create the parameter. */
13461 parameter = finish_template_type_parm (class_type_node, identifier);
13463 /* If the next token is an `=', we have a default argument. */
13464 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13466 /* Consume the `=' token. */
13467 cp_lexer_consume_token (parser->lexer);
13468 /* Parse the default-argument. */
13469 push_deferring_access_checks (dk_no_deferred);
13470 default_argument = cp_parser_type_id (parser);
13472 /* Template parameter packs cannot have default
13473 arguments. */
13474 if (*is_parameter_pack)
13476 if (identifier)
13477 error_at (token->location,
13478 "template parameter pack %qD cannot have a "
13479 "default argument", identifier);
13480 else
13481 error_at (token->location,
13482 "template parameter packs cannot have "
13483 "default arguments");
13484 default_argument = NULL_TREE;
13486 pop_deferring_access_checks ();
13488 else
13489 default_argument = NULL_TREE;
13491 /* Create the combined representation of the parameter and the
13492 default argument. */
13493 parameter = build_tree_list (default_argument, parameter);
13495 break;
13497 case RID_TEMPLATE:
13499 tree identifier;
13500 tree default_argument;
13502 /* Look for the `<'. */
13503 cp_parser_require (parser, CPP_LESS, RT_LESS);
13504 /* Parse the template-parameter-list. */
13505 cp_parser_template_parameter_list (parser);
13506 /* Look for the `>'. */
13507 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13508 /* Look for the `class' or 'typename' keywords. */
13509 cp_parser_type_parameter_key (parser);
13510 /* If the next token is an ellipsis, we have a template
13511 argument pack. */
13512 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13514 /* Consume the `...' token. */
13515 cp_lexer_consume_token (parser->lexer);
13516 maybe_warn_variadic_templates ();
13518 *is_parameter_pack = true;
13520 /* If the next token is an `=', then there is a
13521 default-argument. If the next token is a `>', we are at
13522 the end of the parameter-list. If the next token is a `,',
13523 then we are at the end of this parameter. */
13524 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13525 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13526 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13528 identifier = cp_parser_identifier (parser);
13529 /* Treat invalid names as if the parameter were nameless. */
13530 if (identifier == error_mark_node)
13531 identifier = NULL_TREE;
13533 else
13534 identifier = NULL_TREE;
13536 /* Create the template parameter. */
13537 parameter = finish_template_template_parm (class_type_node,
13538 identifier);
13540 /* If the next token is an `=', then there is a
13541 default-argument. */
13542 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13544 bool is_template;
13546 /* Consume the `='. */
13547 cp_lexer_consume_token (parser->lexer);
13548 /* Parse the id-expression. */
13549 push_deferring_access_checks (dk_no_deferred);
13550 /* save token before parsing the id-expression, for error
13551 reporting */
13552 token = cp_lexer_peek_token (parser->lexer);
13553 default_argument
13554 = cp_parser_id_expression (parser,
13555 /*template_keyword_p=*/false,
13556 /*check_dependency_p=*/true,
13557 /*template_p=*/&is_template,
13558 /*declarator_p=*/false,
13559 /*optional_p=*/false);
13560 if (TREE_CODE (default_argument) == TYPE_DECL)
13561 /* If the id-expression was a template-id that refers to
13562 a template-class, we already have the declaration here,
13563 so no further lookup is needed. */
13565 else
13566 /* Look up the name. */
13567 default_argument
13568 = cp_parser_lookup_name (parser, default_argument,
13569 none_type,
13570 /*is_template=*/is_template,
13571 /*is_namespace=*/false,
13572 /*check_dependency=*/true,
13573 /*ambiguous_decls=*/NULL,
13574 token->location);
13575 /* See if the default argument is valid. */
13576 default_argument
13577 = check_template_template_default_arg (default_argument);
13579 /* Template parameter packs cannot have default
13580 arguments. */
13581 if (*is_parameter_pack)
13583 if (identifier)
13584 error_at (token->location,
13585 "template parameter pack %qD cannot "
13586 "have a default argument",
13587 identifier);
13588 else
13589 error_at (token->location, "template parameter packs cannot "
13590 "have default arguments");
13591 default_argument = NULL_TREE;
13593 pop_deferring_access_checks ();
13595 else
13596 default_argument = NULL_TREE;
13598 /* Create the combined representation of the parameter and the
13599 default argument. */
13600 parameter = build_tree_list (default_argument, parameter);
13602 break;
13604 default:
13605 gcc_unreachable ();
13606 break;
13609 return parameter;
13612 /* Parse a template-id.
13614 template-id:
13615 template-name < template-argument-list [opt] >
13617 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13618 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13619 returned. Otherwise, if the template-name names a function, or set
13620 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13621 names a class, returns a TYPE_DECL for the specialization.
13623 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13624 uninstantiated templates. */
13626 static tree
13627 cp_parser_template_id (cp_parser *parser,
13628 bool template_keyword_p,
13629 bool check_dependency_p,
13630 enum tag_types tag_type,
13631 bool is_declaration)
13633 int i;
13634 tree templ;
13635 tree arguments;
13636 tree template_id;
13637 cp_token_position start_of_id = 0;
13638 deferred_access_check *chk;
13639 vec<deferred_access_check, va_gc> *access_check;
13640 cp_token *next_token = NULL, *next_token_2 = NULL;
13641 bool is_identifier;
13643 /* If the next token corresponds to a template-id, there is no need
13644 to reparse it. */
13645 next_token = cp_lexer_peek_token (parser->lexer);
13646 if (next_token->type == CPP_TEMPLATE_ID)
13648 struct tree_check *check_value;
13650 /* Get the stored value. */
13651 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13652 /* Perform any access checks that were deferred. */
13653 access_check = check_value->checks;
13654 if (access_check)
13656 FOR_EACH_VEC_ELT (*access_check, i, chk)
13657 perform_or_defer_access_check (chk->binfo,
13658 chk->decl,
13659 chk->diag_decl,
13660 tf_warning_or_error);
13662 /* Return the stored value. */
13663 return check_value->value;
13666 /* Avoid performing name lookup if there is no possibility of
13667 finding a template-id. */
13668 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13669 || (next_token->type == CPP_NAME
13670 && !cp_parser_nth_token_starts_template_argument_list_p
13671 (parser, 2)))
13673 cp_parser_error (parser, "expected template-id");
13674 return error_mark_node;
13677 /* Remember where the template-id starts. */
13678 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13679 start_of_id = cp_lexer_token_position (parser->lexer, false);
13681 push_deferring_access_checks (dk_deferred);
13683 /* Parse the template-name. */
13684 is_identifier = false;
13685 templ = cp_parser_template_name (parser, template_keyword_p,
13686 check_dependency_p,
13687 is_declaration,
13688 tag_type,
13689 &is_identifier);
13690 if (templ == error_mark_node || is_identifier)
13692 pop_deferring_access_checks ();
13693 return templ;
13696 /* If we find the sequence `[:' after a template-name, it's probably
13697 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13698 parse correctly the argument list. */
13699 next_token = cp_lexer_peek_token (parser->lexer);
13700 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13701 if (next_token->type == CPP_OPEN_SQUARE
13702 && next_token->flags & DIGRAPH
13703 && next_token_2->type == CPP_COLON
13704 && !(next_token_2->flags & PREV_WHITE))
13706 cp_parser_parse_tentatively (parser);
13707 /* Change `:' into `::'. */
13708 next_token_2->type = CPP_SCOPE;
13709 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13710 CPP_LESS. */
13711 cp_lexer_consume_token (parser->lexer);
13713 /* Parse the arguments. */
13714 arguments = cp_parser_enclosed_template_argument_list (parser);
13715 if (!cp_parser_parse_definitely (parser))
13717 /* If we couldn't parse an argument list, then we revert our changes
13718 and return simply an error. Maybe this is not a template-id
13719 after all. */
13720 next_token_2->type = CPP_COLON;
13721 cp_parser_error (parser, "expected %<<%>");
13722 pop_deferring_access_checks ();
13723 return error_mark_node;
13725 /* Otherwise, emit an error about the invalid digraph, but continue
13726 parsing because we got our argument list. */
13727 if (permerror (next_token->location,
13728 "%<<::%> cannot begin a template-argument list"))
13730 static bool hint = false;
13731 inform (next_token->location,
13732 "%<<:%> is an alternate spelling for %<[%>."
13733 " Insert whitespace between %<<%> and %<::%>");
13734 if (!hint && !flag_permissive)
13736 inform (next_token->location, "(if you use %<-fpermissive%> "
13737 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13738 "accept your code)");
13739 hint = true;
13743 else
13745 /* Look for the `<' that starts the template-argument-list. */
13746 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13748 pop_deferring_access_checks ();
13749 return error_mark_node;
13751 /* Parse the arguments. */
13752 arguments = cp_parser_enclosed_template_argument_list (parser);
13755 /* Build a representation of the specialization. */
13756 if (identifier_p (templ))
13757 template_id = build_min_nt_loc (next_token->location,
13758 TEMPLATE_ID_EXPR,
13759 templ, arguments);
13760 else if (DECL_TYPE_TEMPLATE_P (templ)
13761 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13763 bool entering_scope;
13764 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13765 template (rather than some instantiation thereof) only if
13766 is not nested within some other construct. For example, in
13767 "template <typename T> void f(T) { A<T>::", A<T> is just an
13768 instantiation of A. */
13769 entering_scope = (template_parm_scope_p ()
13770 && cp_lexer_next_token_is (parser->lexer,
13771 CPP_SCOPE));
13772 template_id
13773 = finish_template_type (templ, arguments, entering_scope);
13775 else if (variable_template_p (templ))
13777 template_id = lookup_template_variable (templ, arguments);
13779 else
13781 /* If it's not a class-template or a template-template, it should be
13782 a function-template. */
13783 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13784 || TREE_CODE (templ) == OVERLOAD
13785 || BASELINK_P (templ)));
13787 template_id = lookup_template_function (templ, arguments);
13790 /* If parsing tentatively, replace the sequence of tokens that makes
13791 up the template-id with a CPP_TEMPLATE_ID token. That way,
13792 should we re-parse the token stream, we will not have to repeat
13793 the effort required to do the parse, nor will we issue duplicate
13794 error messages about problems during instantiation of the
13795 template. */
13796 if (start_of_id
13797 /* Don't do this if we had a parse error in a declarator; re-parsing
13798 might succeed if a name changes meaning (60361). */
13799 && !(cp_parser_error_occurred (parser)
13800 && cp_parser_parsing_tentatively (parser)
13801 && parser->in_declarator_p))
13803 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13805 /* Reset the contents of the START_OF_ID token. */
13806 token->type = CPP_TEMPLATE_ID;
13807 /* Retrieve any deferred checks. Do not pop this access checks yet
13808 so the memory will not be reclaimed during token replacing below. */
13809 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13810 token->u.tree_check_value->value = template_id;
13811 token->u.tree_check_value->checks = get_deferred_access_checks ();
13812 token->keyword = RID_MAX;
13814 /* Purge all subsequent tokens. */
13815 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13817 /* ??? Can we actually assume that, if template_id ==
13818 error_mark_node, we will have issued a diagnostic to the
13819 user, as opposed to simply marking the tentative parse as
13820 failed? */
13821 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13822 error_at (token->location, "parse error in template argument list");
13825 pop_to_parent_deferring_access_checks ();
13826 return template_id;
13829 /* Parse a template-name.
13831 template-name:
13832 identifier
13834 The standard should actually say:
13836 template-name:
13837 identifier
13838 operator-function-id
13840 A defect report has been filed about this issue.
13842 A conversion-function-id cannot be a template name because they cannot
13843 be part of a template-id. In fact, looking at this code:
13845 a.operator K<int>()
13847 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13848 It is impossible to call a templated conversion-function-id with an
13849 explicit argument list, since the only allowed template parameter is
13850 the type to which it is converting.
13852 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13853 `template' keyword, in a construction like:
13855 T::template f<3>()
13857 In that case `f' is taken to be a template-name, even though there
13858 is no way of knowing for sure.
13860 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13861 name refers to a set of overloaded functions, at least one of which
13862 is a template, or an IDENTIFIER_NODE with the name of the template,
13863 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13864 names are looked up inside uninstantiated templates. */
13866 static tree
13867 cp_parser_template_name (cp_parser* parser,
13868 bool template_keyword_p,
13869 bool check_dependency_p,
13870 bool is_declaration,
13871 enum tag_types tag_type,
13872 bool *is_identifier)
13874 tree identifier;
13875 tree decl;
13876 tree fns;
13877 cp_token *token = cp_lexer_peek_token (parser->lexer);
13879 /* If the next token is `operator', then we have either an
13880 operator-function-id or a conversion-function-id. */
13881 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13883 /* We don't know whether we're looking at an
13884 operator-function-id or a conversion-function-id. */
13885 cp_parser_parse_tentatively (parser);
13886 /* Try an operator-function-id. */
13887 identifier = cp_parser_operator_function_id (parser);
13888 /* If that didn't work, try a conversion-function-id. */
13889 if (!cp_parser_parse_definitely (parser))
13891 cp_parser_error (parser, "expected template-name");
13892 return error_mark_node;
13895 /* Look for the identifier. */
13896 else
13897 identifier = cp_parser_identifier (parser);
13899 /* If we didn't find an identifier, we don't have a template-id. */
13900 if (identifier == error_mark_node)
13901 return error_mark_node;
13903 /* If the name immediately followed the `template' keyword, then it
13904 is a template-name. However, if the next token is not `<', then
13905 we do not treat it as a template-name, since it is not being used
13906 as part of a template-id. This enables us to handle constructs
13907 like:
13909 template <typename T> struct S { S(); };
13910 template <typename T> S<T>::S();
13912 correctly. We would treat `S' as a template -- if it were `S<T>'
13913 -- but we do not if there is no `<'. */
13915 if (processing_template_decl
13916 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13918 /* In a declaration, in a dependent context, we pretend that the
13919 "template" keyword was present in order to improve error
13920 recovery. For example, given:
13922 template <typename T> void f(T::X<int>);
13924 we want to treat "X<int>" as a template-id. */
13925 if (is_declaration
13926 && !template_keyword_p
13927 && parser->scope && TYPE_P (parser->scope)
13928 && check_dependency_p
13929 && dependent_scope_p (parser->scope)
13930 /* Do not do this for dtors (or ctors), since they never
13931 need the template keyword before their name. */
13932 && !constructor_name_p (identifier, parser->scope))
13934 cp_token_position start = 0;
13936 /* Explain what went wrong. */
13937 error_at (token->location, "non-template %qD used as template",
13938 identifier);
13939 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13940 parser->scope, identifier);
13941 /* If parsing tentatively, find the location of the "<" token. */
13942 if (cp_parser_simulate_error (parser))
13943 start = cp_lexer_token_position (parser->lexer, true);
13944 /* Parse the template arguments so that we can issue error
13945 messages about them. */
13946 cp_lexer_consume_token (parser->lexer);
13947 cp_parser_enclosed_template_argument_list (parser);
13948 /* Skip tokens until we find a good place from which to
13949 continue parsing. */
13950 cp_parser_skip_to_closing_parenthesis (parser,
13951 /*recovering=*/true,
13952 /*or_comma=*/true,
13953 /*consume_paren=*/false);
13954 /* If parsing tentatively, permanently remove the
13955 template argument list. That will prevent duplicate
13956 error messages from being issued about the missing
13957 "template" keyword. */
13958 if (start)
13959 cp_lexer_purge_tokens_after (parser->lexer, start);
13960 if (is_identifier)
13961 *is_identifier = true;
13962 return identifier;
13965 /* If the "template" keyword is present, then there is generally
13966 no point in doing name-lookup, so we just return IDENTIFIER.
13967 But, if the qualifying scope is non-dependent then we can
13968 (and must) do name-lookup normally. */
13969 if (template_keyword_p
13970 && (!parser->scope
13971 || (TYPE_P (parser->scope)
13972 && dependent_type_p (parser->scope))))
13973 return identifier;
13976 /* Look up the name. */
13977 decl = cp_parser_lookup_name (parser, identifier,
13978 tag_type,
13979 /*is_template=*/true,
13980 /*is_namespace=*/false,
13981 check_dependency_p,
13982 /*ambiguous_decls=*/NULL,
13983 token->location);
13985 /* If DECL is a template, then the name was a template-name. */
13986 if (TREE_CODE (decl) == TEMPLATE_DECL)
13988 if (TREE_DEPRECATED (decl)
13989 && deprecated_state != DEPRECATED_SUPPRESS)
13990 warn_deprecated_use (decl, NULL_TREE);
13992 else
13994 tree fn = NULL_TREE;
13996 /* The standard does not explicitly indicate whether a name that
13997 names a set of overloaded declarations, some of which are
13998 templates, is a template-name. However, such a name should
13999 be a template-name; otherwise, there is no way to form a
14000 template-id for the overloaded templates. */
14001 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14002 if (TREE_CODE (fns) == OVERLOAD)
14003 for (fn = fns; fn; fn = OVL_NEXT (fn))
14004 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14005 break;
14007 if (!fn)
14009 /* The name does not name a template. */
14010 cp_parser_error (parser, "expected template-name");
14011 return error_mark_node;
14015 /* If DECL is dependent, and refers to a function, then just return
14016 its name; we will look it up again during template instantiation. */
14017 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14019 tree scope = ovl_scope (decl);
14020 if (TYPE_P (scope) && dependent_type_p (scope))
14021 return identifier;
14024 return decl;
14027 /* Parse a template-argument-list.
14029 template-argument-list:
14030 template-argument ... [opt]
14031 template-argument-list , template-argument ... [opt]
14033 Returns a TREE_VEC containing the arguments. */
14035 static tree
14036 cp_parser_template_argument_list (cp_parser* parser)
14038 tree fixed_args[10];
14039 unsigned n_args = 0;
14040 unsigned alloced = 10;
14041 tree *arg_ary = fixed_args;
14042 tree vec;
14043 bool saved_in_template_argument_list_p;
14044 bool saved_ice_p;
14045 bool saved_non_ice_p;
14047 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14048 parser->in_template_argument_list_p = true;
14049 /* Even if the template-id appears in an integral
14050 constant-expression, the contents of the argument list do
14051 not. */
14052 saved_ice_p = parser->integral_constant_expression_p;
14053 parser->integral_constant_expression_p = false;
14054 saved_non_ice_p = parser->non_integral_constant_expression_p;
14055 parser->non_integral_constant_expression_p = false;
14057 /* Parse the arguments. */
14060 tree argument;
14062 if (n_args)
14063 /* Consume the comma. */
14064 cp_lexer_consume_token (parser->lexer);
14066 /* Parse the template-argument. */
14067 argument = cp_parser_template_argument (parser);
14069 /* If the next token is an ellipsis, we're expanding a template
14070 argument pack. */
14071 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14073 if (argument == error_mark_node)
14075 cp_token *token = cp_lexer_peek_token (parser->lexer);
14076 error_at (token->location,
14077 "expected parameter pack before %<...%>");
14079 /* Consume the `...' token. */
14080 cp_lexer_consume_token (parser->lexer);
14082 /* Make the argument into a TYPE_PACK_EXPANSION or
14083 EXPR_PACK_EXPANSION. */
14084 argument = make_pack_expansion (argument);
14087 if (n_args == alloced)
14089 alloced *= 2;
14091 if (arg_ary == fixed_args)
14093 arg_ary = XNEWVEC (tree, alloced);
14094 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14096 else
14097 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14099 arg_ary[n_args++] = argument;
14101 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14103 vec = make_tree_vec (n_args);
14105 while (n_args--)
14106 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14108 if (arg_ary != fixed_args)
14109 free (arg_ary);
14110 parser->non_integral_constant_expression_p = saved_non_ice_p;
14111 parser->integral_constant_expression_p = saved_ice_p;
14112 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14113 #ifdef ENABLE_CHECKING
14114 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14115 #endif
14116 return vec;
14119 /* Parse a template-argument.
14121 template-argument:
14122 assignment-expression
14123 type-id
14124 id-expression
14126 The representation is that of an assignment-expression, type-id, or
14127 id-expression -- except that the qualified id-expression is
14128 evaluated, so that the value returned is either a DECL or an
14129 OVERLOAD.
14131 Although the standard says "assignment-expression", it forbids
14132 throw-expressions or assignments in the template argument.
14133 Therefore, we use "conditional-expression" instead. */
14135 static tree
14136 cp_parser_template_argument (cp_parser* parser)
14138 tree argument;
14139 bool template_p;
14140 bool address_p;
14141 bool maybe_type_id = false;
14142 cp_token *token = NULL, *argument_start_token = NULL;
14143 location_t loc = 0;
14144 cp_id_kind idk;
14146 /* There's really no way to know what we're looking at, so we just
14147 try each alternative in order.
14149 [temp.arg]
14151 In a template-argument, an ambiguity between a type-id and an
14152 expression is resolved to a type-id, regardless of the form of
14153 the corresponding template-parameter.
14155 Therefore, we try a type-id first. */
14156 cp_parser_parse_tentatively (parser);
14157 argument = cp_parser_template_type_arg (parser);
14158 /* If there was no error parsing the type-id but the next token is a
14159 '>>', our behavior depends on which dialect of C++ we're
14160 parsing. In C++98, we probably found a typo for '> >'. But there
14161 are type-id which are also valid expressions. For instance:
14163 struct X { int operator >> (int); };
14164 template <int V> struct Foo {};
14165 Foo<X () >> 5> r;
14167 Here 'X()' is a valid type-id of a function type, but the user just
14168 wanted to write the expression "X() >> 5". Thus, we remember that we
14169 found a valid type-id, but we still try to parse the argument as an
14170 expression to see what happens.
14172 In C++0x, the '>>' will be considered two separate '>'
14173 tokens. */
14174 if (!cp_parser_error_occurred (parser)
14175 && cxx_dialect == cxx98
14176 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14178 maybe_type_id = true;
14179 cp_parser_abort_tentative_parse (parser);
14181 else
14183 /* If the next token isn't a `,' or a `>', then this argument wasn't
14184 really finished. This means that the argument is not a valid
14185 type-id. */
14186 if (!cp_parser_next_token_ends_template_argument_p (parser))
14187 cp_parser_error (parser, "expected template-argument");
14188 /* If that worked, we're done. */
14189 if (cp_parser_parse_definitely (parser))
14190 return argument;
14192 /* We're still not sure what the argument will be. */
14193 cp_parser_parse_tentatively (parser);
14194 /* Try a template. */
14195 argument_start_token = cp_lexer_peek_token (parser->lexer);
14196 argument = cp_parser_id_expression (parser,
14197 /*template_keyword_p=*/false,
14198 /*check_dependency_p=*/true,
14199 &template_p,
14200 /*declarator_p=*/false,
14201 /*optional_p=*/false);
14202 /* If the next token isn't a `,' or a `>', then this argument wasn't
14203 really finished. */
14204 if (!cp_parser_next_token_ends_template_argument_p (parser))
14205 cp_parser_error (parser, "expected template-argument");
14206 if (!cp_parser_error_occurred (parser))
14208 /* Figure out what is being referred to. If the id-expression
14209 was for a class template specialization, then we will have a
14210 TYPE_DECL at this point. There is no need to do name lookup
14211 at this point in that case. */
14212 if (TREE_CODE (argument) != TYPE_DECL)
14213 argument = cp_parser_lookup_name (parser, argument,
14214 none_type,
14215 /*is_template=*/template_p,
14216 /*is_namespace=*/false,
14217 /*check_dependency=*/true,
14218 /*ambiguous_decls=*/NULL,
14219 argument_start_token->location);
14220 if (TREE_CODE (argument) != TEMPLATE_DECL
14221 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14222 cp_parser_error (parser, "expected template-name");
14224 if (cp_parser_parse_definitely (parser))
14226 if (TREE_DEPRECATED (argument))
14227 warn_deprecated_use (argument, NULL_TREE);
14228 return argument;
14230 /* It must be a non-type argument. There permitted cases are given
14231 in [temp.arg.nontype]:
14233 -- an integral constant-expression of integral or enumeration
14234 type; or
14236 -- the name of a non-type template-parameter; or
14238 -- the name of an object or function with external linkage...
14240 -- the address of an object or function with external linkage...
14242 -- a pointer to member... */
14243 /* Look for a non-type template parameter. */
14244 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14246 cp_parser_parse_tentatively (parser);
14247 argument = cp_parser_primary_expression (parser,
14248 /*address_p=*/false,
14249 /*cast_p=*/false,
14250 /*template_arg_p=*/true,
14251 &idk);
14252 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14253 || !cp_parser_next_token_ends_template_argument_p (parser))
14254 cp_parser_simulate_error (parser);
14255 if (cp_parser_parse_definitely (parser))
14256 return argument;
14259 /* If the next token is "&", the argument must be the address of an
14260 object or function with external linkage. */
14261 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14262 if (address_p)
14264 loc = cp_lexer_peek_token (parser->lexer)->location;
14265 cp_lexer_consume_token (parser->lexer);
14267 /* See if we might have an id-expression. */
14268 token = cp_lexer_peek_token (parser->lexer);
14269 if (token->type == CPP_NAME
14270 || token->keyword == RID_OPERATOR
14271 || token->type == CPP_SCOPE
14272 || token->type == CPP_TEMPLATE_ID
14273 || token->type == CPP_NESTED_NAME_SPECIFIER)
14275 cp_parser_parse_tentatively (parser);
14276 argument = cp_parser_primary_expression (parser,
14277 address_p,
14278 /*cast_p=*/false,
14279 /*template_arg_p=*/true,
14280 &idk);
14281 if (cp_parser_error_occurred (parser)
14282 || !cp_parser_next_token_ends_template_argument_p (parser))
14283 cp_parser_abort_tentative_parse (parser);
14284 else
14286 tree probe;
14288 if (INDIRECT_REF_P (argument))
14290 /* Strip the dereference temporarily. */
14291 gcc_assert (REFERENCE_REF_P (argument));
14292 argument = TREE_OPERAND (argument, 0);
14295 /* If we're in a template, we represent a qualified-id referring
14296 to a static data member as a SCOPE_REF even if the scope isn't
14297 dependent so that we can check access control later. */
14298 probe = argument;
14299 if (TREE_CODE (probe) == SCOPE_REF)
14300 probe = TREE_OPERAND (probe, 1);
14301 if (VAR_P (probe))
14303 /* A variable without external linkage might still be a
14304 valid constant-expression, so no error is issued here
14305 if the external-linkage check fails. */
14306 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14307 cp_parser_simulate_error (parser);
14309 else if (is_overloaded_fn (argument))
14310 /* All overloaded functions are allowed; if the external
14311 linkage test does not pass, an error will be issued
14312 later. */
14314 else if (address_p
14315 && (TREE_CODE (argument) == OFFSET_REF
14316 || TREE_CODE (argument) == SCOPE_REF))
14317 /* A pointer-to-member. */
14319 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14321 else
14322 cp_parser_simulate_error (parser);
14324 if (cp_parser_parse_definitely (parser))
14326 if (address_p)
14327 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14328 tf_warning_or_error);
14329 else
14330 argument = convert_from_reference (argument);
14331 return argument;
14335 /* If the argument started with "&", there are no other valid
14336 alternatives at this point. */
14337 if (address_p)
14339 cp_parser_error (parser, "invalid non-type template argument");
14340 return error_mark_node;
14343 /* If the argument wasn't successfully parsed as a type-id followed
14344 by '>>', the argument can only be a constant expression now.
14345 Otherwise, we try parsing the constant-expression tentatively,
14346 because the argument could really be a type-id. */
14347 if (maybe_type_id)
14348 cp_parser_parse_tentatively (parser);
14349 argument = cp_parser_constant_expression (parser);
14351 if (!maybe_type_id)
14352 return argument;
14353 if (!cp_parser_next_token_ends_template_argument_p (parser))
14354 cp_parser_error (parser, "expected template-argument");
14355 if (cp_parser_parse_definitely (parser))
14356 return argument;
14357 /* We did our best to parse the argument as a non type-id, but that
14358 was the only alternative that matched (albeit with a '>' after
14359 it). We can assume it's just a typo from the user, and a
14360 diagnostic will then be issued. */
14361 return cp_parser_template_type_arg (parser);
14364 /* Parse an explicit-instantiation.
14366 explicit-instantiation:
14367 template declaration
14369 Although the standard says `declaration', what it really means is:
14371 explicit-instantiation:
14372 template decl-specifier-seq [opt] declarator [opt] ;
14374 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14375 supposed to be allowed. A defect report has been filed about this
14376 issue.
14378 GNU Extension:
14380 explicit-instantiation:
14381 storage-class-specifier template
14382 decl-specifier-seq [opt] declarator [opt] ;
14383 function-specifier template
14384 decl-specifier-seq [opt] declarator [opt] ; */
14386 static void
14387 cp_parser_explicit_instantiation (cp_parser* parser)
14389 int declares_class_or_enum;
14390 cp_decl_specifier_seq decl_specifiers;
14391 tree extension_specifier = NULL_TREE;
14393 timevar_push (TV_TEMPLATE_INST);
14395 /* Look for an (optional) storage-class-specifier or
14396 function-specifier. */
14397 if (cp_parser_allow_gnu_extensions_p (parser))
14399 extension_specifier
14400 = cp_parser_storage_class_specifier_opt (parser);
14401 if (!extension_specifier)
14402 extension_specifier
14403 = cp_parser_function_specifier_opt (parser,
14404 /*decl_specs=*/NULL);
14407 /* Look for the `template' keyword. */
14408 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14409 /* Let the front end know that we are processing an explicit
14410 instantiation. */
14411 begin_explicit_instantiation ();
14412 /* [temp.explicit] says that we are supposed to ignore access
14413 control while processing explicit instantiation directives. */
14414 push_deferring_access_checks (dk_no_check);
14415 /* Parse a decl-specifier-seq. */
14416 cp_parser_decl_specifier_seq (parser,
14417 CP_PARSER_FLAGS_OPTIONAL,
14418 &decl_specifiers,
14419 &declares_class_or_enum);
14420 /* If there was exactly one decl-specifier, and it declared a class,
14421 and there's no declarator, then we have an explicit type
14422 instantiation. */
14423 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14425 tree type;
14427 type = check_tag_decl (&decl_specifiers,
14428 /*explicit_type_instantiation_p=*/true);
14429 /* Turn access control back on for names used during
14430 template instantiation. */
14431 pop_deferring_access_checks ();
14432 if (type)
14433 do_type_instantiation (type, extension_specifier,
14434 /*complain=*/tf_error);
14436 else
14438 cp_declarator *declarator;
14439 tree decl;
14441 /* Parse the declarator. */
14442 declarator
14443 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14444 /*ctor_dtor_or_conv_p=*/NULL,
14445 /*parenthesized_p=*/NULL,
14446 /*member_p=*/false,
14447 /*friend_p=*/false);
14448 if (declares_class_or_enum & 2)
14449 cp_parser_check_for_definition_in_return_type (declarator,
14450 decl_specifiers.type,
14451 decl_specifiers.locations[ds_type_spec]);
14452 if (declarator != cp_error_declarator)
14454 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14455 permerror (decl_specifiers.locations[ds_inline],
14456 "explicit instantiation shall not use"
14457 " %<inline%> specifier");
14458 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14459 permerror (decl_specifiers.locations[ds_constexpr],
14460 "explicit instantiation shall not use"
14461 " %<constexpr%> specifier");
14463 decl = grokdeclarator (declarator, &decl_specifiers,
14464 NORMAL, 0, &decl_specifiers.attributes);
14465 /* Turn access control back on for names used during
14466 template instantiation. */
14467 pop_deferring_access_checks ();
14468 /* Do the explicit instantiation. */
14469 do_decl_instantiation (decl, extension_specifier);
14471 else
14473 pop_deferring_access_checks ();
14474 /* Skip the body of the explicit instantiation. */
14475 cp_parser_skip_to_end_of_statement (parser);
14478 /* We're done with the instantiation. */
14479 end_explicit_instantiation ();
14481 cp_parser_consume_semicolon_at_end_of_statement (parser);
14483 timevar_pop (TV_TEMPLATE_INST);
14486 /* Parse an explicit-specialization.
14488 explicit-specialization:
14489 template < > declaration
14491 Although the standard says `declaration', what it really means is:
14493 explicit-specialization:
14494 template <> decl-specifier [opt] init-declarator [opt] ;
14495 template <> function-definition
14496 template <> explicit-specialization
14497 template <> template-declaration */
14499 static void
14500 cp_parser_explicit_specialization (cp_parser* parser)
14502 bool need_lang_pop;
14503 cp_token *token = cp_lexer_peek_token (parser->lexer);
14505 /* Look for the `template' keyword. */
14506 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14507 /* Look for the `<'. */
14508 cp_parser_require (parser, CPP_LESS, RT_LESS);
14509 /* Look for the `>'. */
14510 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14511 /* We have processed another parameter list. */
14512 ++parser->num_template_parameter_lists;
14513 /* [temp]
14515 A template ... explicit specialization ... shall not have C
14516 linkage. */
14517 if (current_lang_name == lang_name_c)
14519 error_at (token->location, "template specialization with C linkage");
14520 /* Give it C++ linkage to avoid confusing other parts of the
14521 front end. */
14522 push_lang_context (lang_name_cplusplus);
14523 need_lang_pop = true;
14525 else
14526 need_lang_pop = false;
14527 /* Let the front end know that we are beginning a specialization. */
14528 if (!begin_specialization ())
14530 end_specialization ();
14531 return;
14534 /* If the next keyword is `template', we need to figure out whether
14535 or not we're looking a template-declaration. */
14536 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14538 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14539 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14540 cp_parser_template_declaration_after_export (parser,
14541 /*member_p=*/false);
14542 else
14543 cp_parser_explicit_specialization (parser);
14545 else
14546 /* Parse the dependent declaration. */
14547 cp_parser_single_declaration (parser,
14548 /*checks=*/NULL,
14549 /*member_p=*/false,
14550 /*explicit_specialization_p=*/true,
14551 /*friend_p=*/NULL);
14552 /* We're done with the specialization. */
14553 end_specialization ();
14554 /* For the erroneous case of a template with C linkage, we pushed an
14555 implicit C++ linkage scope; exit that scope now. */
14556 if (need_lang_pop)
14557 pop_lang_context ();
14558 /* We're done with this parameter list. */
14559 --parser->num_template_parameter_lists;
14562 /* Parse a type-specifier.
14564 type-specifier:
14565 simple-type-specifier
14566 class-specifier
14567 enum-specifier
14568 elaborated-type-specifier
14569 cv-qualifier
14571 GNU Extension:
14573 type-specifier:
14574 __complex__
14576 Returns a representation of the type-specifier. For a
14577 class-specifier, enum-specifier, or elaborated-type-specifier, a
14578 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14580 The parser flags FLAGS is used to control type-specifier parsing.
14582 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14583 in a decl-specifier-seq.
14585 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14586 class-specifier, enum-specifier, or elaborated-type-specifier, then
14587 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14588 if a type is declared; 2 if it is defined. Otherwise, it is set to
14589 zero.
14591 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14592 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14593 is set to FALSE. */
14595 static tree
14596 cp_parser_type_specifier (cp_parser* parser,
14597 cp_parser_flags flags,
14598 cp_decl_specifier_seq *decl_specs,
14599 bool is_declaration,
14600 int* declares_class_or_enum,
14601 bool* is_cv_qualifier)
14603 tree type_spec = NULL_TREE;
14604 cp_token *token;
14605 enum rid keyword;
14606 cp_decl_spec ds = ds_last;
14608 /* Assume this type-specifier does not declare a new type. */
14609 if (declares_class_or_enum)
14610 *declares_class_or_enum = 0;
14611 /* And that it does not specify a cv-qualifier. */
14612 if (is_cv_qualifier)
14613 *is_cv_qualifier = false;
14614 /* Peek at the next token. */
14615 token = cp_lexer_peek_token (parser->lexer);
14617 /* If we're looking at a keyword, we can use that to guide the
14618 production we choose. */
14619 keyword = token->keyword;
14620 switch (keyword)
14622 case RID_ENUM:
14623 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14624 goto elaborated_type_specifier;
14626 /* Look for the enum-specifier. */
14627 type_spec = cp_parser_enum_specifier (parser);
14628 /* If that worked, we're done. */
14629 if (type_spec)
14631 if (declares_class_or_enum)
14632 *declares_class_or_enum = 2;
14633 if (decl_specs)
14634 cp_parser_set_decl_spec_type (decl_specs,
14635 type_spec,
14636 token,
14637 /*type_definition_p=*/true);
14638 return type_spec;
14640 else
14641 goto elaborated_type_specifier;
14643 /* Any of these indicate either a class-specifier, or an
14644 elaborated-type-specifier. */
14645 case RID_CLASS:
14646 case RID_STRUCT:
14647 case RID_UNION:
14648 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14649 goto elaborated_type_specifier;
14651 /* Parse tentatively so that we can back up if we don't find a
14652 class-specifier. */
14653 cp_parser_parse_tentatively (parser);
14654 /* Look for the class-specifier. */
14655 type_spec = cp_parser_class_specifier (parser);
14656 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14657 /* If that worked, we're done. */
14658 if (cp_parser_parse_definitely (parser))
14660 if (declares_class_or_enum)
14661 *declares_class_or_enum = 2;
14662 if (decl_specs)
14663 cp_parser_set_decl_spec_type (decl_specs,
14664 type_spec,
14665 token,
14666 /*type_definition_p=*/true);
14667 return type_spec;
14670 /* Fall through. */
14671 elaborated_type_specifier:
14672 /* We're declaring (not defining) a class or enum. */
14673 if (declares_class_or_enum)
14674 *declares_class_or_enum = 1;
14676 /* Fall through. */
14677 case RID_TYPENAME:
14678 /* Look for an elaborated-type-specifier. */
14679 type_spec
14680 = (cp_parser_elaborated_type_specifier
14681 (parser,
14682 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14683 is_declaration));
14684 if (decl_specs)
14685 cp_parser_set_decl_spec_type (decl_specs,
14686 type_spec,
14687 token,
14688 /*type_definition_p=*/false);
14689 return type_spec;
14691 case RID_CONST:
14692 ds = ds_const;
14693 if (is_cv_qualifier)
14694 *is_cv_qualifier = true;
14695 break;
14697 case RID_VOLATILE:
14698 ds = ds_volatile;
14699 if (is_cv_qualifier)
14700 *is_cv_qualifier = true;
14701 break;
14703 case RID_RESTRICT:
14704 ds = ds_restrict;
14705 if (is_cv_qualifier)
14706 *is_cv_qualifier = true;
14707 break;
14709 case RID_COMPLEX:
14710 /* The `__complex__' keyword is a GNU extension. */
14711 ds = ds_complex;
14712 break;
14714 default:
14715 break;
14718 /* Handle simple keywords. */
14719 if (ds != ds_last)
14721 if (decl_specs)
14723 set_and_check_decl_spec_loc (decl_specs, ds, token);
14724 decl_specs->any_specifiers_p = true;
14726 return cp_lexer_consume_token (parser->lexer)->u.value;
14729 /* If we do not already have a type-specifier, assume we are looking
14730 at a simple-type-specifier. */
14731 type_spec = cp_parser_simple_type_specifier (parser,
14732 decl_specs,
14733 flags);
14735 /* If we didn't find a type-specifier, and a type-specifier was not
14736 optional in this context, issue an error message. */
14737 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14739 cp_parser_error (parser, "expected type specifier");
14740 return error_mark_node;
14743 return type_spec;
14746 /* Parse a simple-type-specifier.
14748 simple-type-specifier:
14749 :: [opt] nested-name-specifier [opt] type-name
14750 :: [opt] nested-name-specifier template template-id
14751 char
14752 wchar_t
14753 bool
14754 short
14756 long
14757 signed
14758 unsigned
14759 float
14760 double
14761 void
14763 C++0x Extension:
14765 simple-type-specifier:
14766 auto
14767 decltype ( expression )
14768 char16_t
14769 char32_t
14770 __underlying_type ( type-id )
14772 GNU Extension:
14774 simple-type-specifier:
14775 __int128
14776 __typeof__ unary-expression
14777 __typeof__ ( type-id )
14778 __typeof__ ( type-id ) { initializer-list , [opt] }
14780 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14781 appropriately updated. */
14783 static tree
14784 cp_parser_simple_type_specifier (cp_parser* parser,
14785 cp_decl_specifier_seq *decl_specs,
14786 cp_parser_flags flags)
14788 tree type = NULL_TREE;
14789 cp_token *token;
14790 int idx;
14792 /* Peek at the next token. */
14793 token = cp_lexer_peek_token (parser->lexer);
14795 /* If we're looking at a keyword, things are easy. */
14796 switch (token->keyword)
14798 case RID_CHAR:
14799 if (decl_specs)
14800 decl_specs->explicit_char_p = true;
14801 type = char_type_node;
14802 break;
14803 case RID_CHAR16:
14804 type = char16_type_node;
14805 break;
14806 case RID_CHAR32:
14807 type = char32_type_node;
14808 break;
14809 case RID_WCHAR:
14810 type = wchar_type_node;
14811 break;
14812 case RID_BOOL:
14813 type = boolean_type_node;
14814 break;
14815 case RID_SHORT:
14816 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14817 type = short_integer_type_node;
14818 break;
14819 case RID_INT:
14820 if (decl_specs)
14821 decl_specs->explicit_int_p = true;
14822 type = integer_type_node;
14823 break;
14824 case RID_INT_N_0:
14825 case RID_INT_N_1:
14826 case RID_INT_N_2:
14827 case RID_INT_N_3:
14828 idx = token->keyword - RID_INT_N_0;
14829 if (! int_n_enabled_p [idx])
14830 break;
14831 if (decl_specs)
14833 decl_specs->explicit_intN_p = true;
14834 decl_specs->int_n_idx = idx;
14836 type = int_n_trees [idx].signed_type;
14837 break;
14838 case RID_LONG:
14839 if (decl_specs)
14840 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14841 type = long_integer_type_node;
14842 break;
14843 case RID_SIGNED:
14844 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14845 type = integer_type_node;
14846 break;
14847 case RID_UNSIGNED:
14848 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14849 type = unsigned_type_node;
14850 break;
14851 case RID_FLOAT:
14852 type = float_type_node;
14853 break;
14854 case RID_DOUBLE:
14855 type = double_type_node;
14856 break;
14857 case RID_VOID:
14858 type = void_type_node;
14859 break;
14861 case RID_AUTO:
14862 maybe_warn_cpp0x (CPP0X_AUTO);
14863 if (parser->auto_is_implicit_function_template_parm_p)
14865 if (cxx_dialect >= cxx14)
14866 type = synthesize_implicit_template_parm (parser);
14867 else
14868 type = error_mark_node;
14870 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14872 if (cxx_dialect < cxx14)
14873 error_at (token->location,
14874 "use of %<auto%> in lambda parameter declaration "
14875 "only available with "
14876 "-std=c++14 or -std=gnu++14");
14878 else if (cxx_dialect < cxx14)
14879 error_at (token->location,
14880 "use of %<auto%> in parameter declaration "
14881 "only available with "
14882 "-std=c++14 or -std=gnu++14");
14883 else
14884 pedwarn (token->location, OPT_Wpedantic,
14885 "ISO C++ forbids use of %<auto%> in parameter "
14886 "declaration");
14888 else
14889 type = make_auto ();
14890 break;
14892 case RID_DECLTYPE:
14893 /* Since DR 743, decltype can either be a simple-type-specifier by
14894 itself or begin a nested-name-specifier. Parsing it will replace
14895 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14896 handling below decide what to do. */
14897 cp_parser_decltype (parser);
14898 cp_lexer_set_token_position (parser->lexer, token);
14899 break;
14901 case RID_TYPEOF:
14902 /* Consume the `typeof' token. */
14903 cp_lexer_consume_token (parser->lexer);
14904 /* Parse the operand to `typeof'. */
14905 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14906 /* If it is not already a TYPE, take its type. */
14907 if (!TYPE_P (type))
14908 type = finish_typeof (type);
14910 if (decl_specs)
14911 cp_parser_set_decl_spec_type (decl_specs, type,
14912 token,
14913 /*type_definition_p=*/false);
14915 return type;
14917 case RID_UNDERLYING_TYPE:
14918 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14919 if (decl_specs)
14920 cp_parser_set_decl_spec_type (decl_specs, type,
14921 token,
14922 /*type_definition_p=*/false);
14924 return type;
14926 case RID_BASES:
14927 case RID_DIRECT_BASES:
14928 type = cp_parser_trait_expr (parser, token->keyword);
14929 if (decl_specs)
14930 cp_parser_set_decl_spec_type (decl_specs, type,
14931 token,
14932 /*type_definition_p=*/false);
14933 return type;
14934 default:
14935 break;
14938 /* If token is an already-parsed decltype not followed by ::,
14939 it's a simple-type-specifier. */
14940 if (token->type == CPP_DECLTYPE
14941 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14943 type = token->u.value;
14944 if (decl_specs)
14946 cp_parser_set_decl_spec_type (decl_specs, type,
14947 token,
14948 /*type_definition_p=*/false);
14949 /* Remember that we are handling a decltype in order to
14950 implement the resolution of DR 1510 when the argument
14951 isn't instantiation dependent. */
14952 decl_specs->decltype_p = true;
14954 cp_lexer_consume_token (parser->lexer);
14955 return type;
14958 /* If the type-specifier was for a built-in type, we're done. */
14959 if (type)
14961 /* Record the type. */
14962 if (decl_specs
14963 && (token->keyword != RID_SIGNED
14964 && token->keyword != RID_UNSIGNED
14965 && token->keyword != RID_SHORT
14966 && token->keyword != RID_LONG))
14967 cp_parser_set_decl_spec_type (decl_specs,
14968 type,
14969 token,
14970 /*type_definition_p=*/false);
14971 if (decl_specs)
14972 decl_specs->any_specifiers_p = true;
14974 /* Consume the token. */
14975 cp_lexer_consume_token (parser->lexer);
14977 if (type == error_mark_node)
14978 return error_mark_node;
14980 /* There is no valid C++ program where a non-template type is
14981 followed by a "<". That usually indicates that the user thought
14982 that the type was a template. */
14983 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14984 token->location);
14986 return TYPE_NAME (type);
14989 /* The type-specifier must be a user-defined type. */
14990 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14992 bool qualified_p;
14993 bool global_p;
14995 /* Don't gobble tokens or issue error messages if this is an
14996 optional type-specifier. */
14997 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14998 cp_parser_parse_tentatively (parser);
15000 /* Look for the optional `::' operator. */
15001 global_p
15002 = (cp_parser_global_scope_opt (parser,
15003 /*current_scope_valid_p=*/false)
15004 != NULL_TREE);
15005 /* Look for the nested-name specifier. */
15006 qualified_p
15007 = (cp_parser_nested_name_specifier_opt (parser,
15008 /*typename_keyword_p=*/false,
15009 /*check_dependency_p=*/true,
15010 /*type_p=*/false,
15011 /*is_declaration=*/false)
15012 != NULL_TREE);
15013 token = cp_lexer_peek_token (parser->lexer);
15014 /* If we have seen a nested-name-specifier, and the next token
15015 is `template', then we are using the template-id production. */
15016 if (parser->scope
15017 && cp_parser_optional_template_keyword (parser))
15019 /* Look for the template-id. */
15020 type = cp_parser_template_id (parser,
15021 /*template_keyword_p=*/true,
15022 /*check_dependency_p=*/true,
15023 none_type,
15024 /*is_declaration=*/false);
15025 /* If the template-id did not name a type, we are out of
15026 luck. */
15027 if (TREE_CODE (type) != TYPE_DECL)
15029 cp_parser_error (parser, "expected template-id for type");
15030 type = NULL_TREE;
15033 /* Otherwise, look for a type-name. */
15034 else
15035 type = cp_parser_type_name (parser);
15036 /* Keep track of all name-lookups performed in class scopes. */
15037 if (type
15038 && !global_p
15039 && !qualified_p
15040 && TREE_CODE (type) == TYPE_DECL
15041 && identifier_p (DECL_NAME (type)))
15042 maybe_note_name_used_in_class (DECL_NAME (type), type);
15043 /* If it didn't work out, we don't have a TYPE. */
15044 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15045 && !cp_parser_parse_definitely (parser))
15046 type = NULL_TREE;
15047 if (type && decl_specs)
15048 cp_parser_set_decl_spec_type (decl_specs, type,
15049 token,
15050 /*type_definition_p=*/false);
15053 /* If we didn't get a type-name, issue an error message. */
15054 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15056 cp_parser_error (parser, "expected type-name");
15057 return error_mark_node;
15060 if (type && type != error_mark_node)
15062 /* See if TYPE is an Objective-C type, and if so, parse and
15063 accept any protocol references following it. Do this before
15064 the cp_parser_check_for_invalid_template_id() call, because
15065 Objective-C types can be followed by '<...>' which would
15066 enclose protocol names rather than template arguments, and so
15067 everything is fine. */
15068 if (c_dialect_objc () && !parser->scope
15069 && (objc_is_id (type) || objc_is_class_name (type)))
15071 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15072 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15074 /* Clobber the "unqualified" type previously entered into
15075 DECL_SPECS with the new, improved protocol-qualified version. */
15076 if (decl_specs)
15077 decl_specs->type = qual_type;
15079 return qual_type;
15082 /* There is no valid C++ program where a non-template type is
15083 followed by a "<". That usually indicates that the user
15084 thought that the type was a template. */
15085 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15086 none_type,
15087 token->location);
15090 return type;
15093 /* Parse a type-name.
15095 type-name:
15096 class-name
15097 enum-name
15098 typedef-name
15099 simple-template-id [in c++0x]
15101 enum-name:
15102 identifier
15104 typedef-name:
15105 identifier
15107 Returns a TYPE_DECL for the type. */
15109 static tree
15110 cp_parser_type_name (cp_parser* parser)
15112 tree type_decl;
15114 /* We can't know yet whether it is a class-name or not. */
15115 cp_parser_parse_tentatively (parser);
15116 /* Try a class-name. */
15117 type_decl = cp_parser_class_name (parser,
15118 /*typename_keyword_p=*/false,
15119 /*template_keyword_p=*/false,
15120 none_type,
15121 /*check_dependency_p=*/true,
15122 /*class_head_p=*/false,
15123 /*is_declaration=*/false);
15124 /* If it's not a class-name, keep looking. */
15125 if (!cp_parser_parse_definitely (parser))
15127 if (cxx_dialect < cxx11)
15128 /* It must be a typedef-name or an enum-name. */
15129 return cp_parser_nonclass_name (parser);
15131 cp_parser_parse_tentatively (parser);
15132 /* It is either a simple-template-id representing an
15133 instantiation of an alias template... */
15134 type_decl = cp_parser_template_id (parser,
15135 /*template_keyword_p=*/false,
15136 /*check_dependency_p=*/true,
15137 none_type,
15138 /*is_declaration=*/false);
15139 /* Note that this must be an instantiation of an alias template
15140 because [temp.names]/6 says:
15142 A template-id that names an alias template specialization
15143 is a type-name.
15145 Whereas [temp.names]/7 says:
15147 A simple-template-id that names a class template
15148 specialization is a class-name. */
15149 if (type_decl != NULL_TREE
15150 && TREE_CODE (type_decl) == TYPE_DECL
15151 && TYPE_DECL_ALIAS_P (type_decl))
15152 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15153 else
15154 cp_parser_simulate_error (parser);
15156 if (!cp_parser_parse_definitely (parser))
15157 /* ... Or a typedef-name or an enum-name. */
15158 return cp_parser_nonclass_name (parser);
15161 return type_decl;
15164 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15166 enum-name:
15167 identifier
15169 typedef-name:
15170 identifier
15172 Returns a TYPE_DECL for the type. */
15174 static tree
15175 cp_parser_nonclass_name (cp_parser* parser)
15177 tree type_decl;
15178 tree identifier;
15180 cp_token *token = cp_lexer_peek_token (parser->lexer);
15181 identifier = cp_parser_identifier (parser);
15182 if (identifier == error_mark_node)
15183 return error_mark_node;
15185 /* Look up the type-name. */
15186 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15188 type_decl = strip_using_decl (type_decl);
15190 if (TREE_CODE (type_decl) != TYPE_DECL
15191 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15193 /* See if this is an Objective-C type. */
15194 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15195 tree type = objc_get_protocol_qualified_type (identifier, protos);
15196 if (type)
15197 type_decl = TYPE_NAME (type);
15200 /* Issue an error if we did not find a type-name. */
15201 if (TREE_CODE (type_decl) != TYPE_DECL
15202 /* In Objective-C, we have the complication that class names are
15203 normally type names and start declarations (eg, the
15204 "NSObject" in "NSObject *object;"), but can be used in an
15205 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15206 is an expression. So, a classname followed by a dot is not a
15207 valid type-name. */
15208 || (objc_is_class_name (TREE_TYPE (type_decl))
15209 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15211 if (!cp_parser_simulate_error (parser))
15212 cp_parser_name_lookup_error (parser, identifier, type_decl,
15213 NLE_TYPE, token->location);
15214 return error_mark_node;
15216 /* Remember that the name was used in the definition of the
15217 current class so that we can check later to see if the
15218 meaning would have been different after the class was
15219 entirely defined. */
15220 else if (type_decl != error_mark_node
15221 && !parser->scope)
15222 maybe_note_name_used_in_class (identifier, type_decl);
15224 return type_decl;
15227 /* Parse an elaborated-type-specifier. Note that the grammar given
15228 here incorporates the resolution to DR68.
15230 elaborated-type-specifier:
15231 class-key :: [opt] nested-name-specifier [opt] identifier
15232 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15233 enum-key :: [opt] nested-name-specifier [opt] identifier
15234 typename :: [opt] nested-name-specifier identifier
15235 typename :: [opt] nested-name-specifier template [opt]
15236 template-id
15238 GNU extension:
15240 elaborated-type-specifier:
15241 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15242 class-key attributes :: [opt] nested-name-specifier [opt]
15243 template [opt] template-id
15244 enum attributes :: [opt] nested-name-specifier [opt] identifier
15246 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15247 declared `friend'. If IS_DECLARATION is TRUE, then this
15248 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15249 something is being declared.
15251 Returns the TYPE specified. */
15253 static tree
15254 cp_parser_elaborated_type_specifier (cp_parser* parser,
15255 bool is_friend,
15256 bool is_declaration)
15258 enum tag_types tag_type;
15259 tree identifier;
15260 tree type = NULL_TREE;
15261 tree attributes = NULL_TREE;
15262 tree globalscope;
15263 cp_token *token = NULL;
15265 /* See if we're looking at the `enum' keyword. */
15266 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15268 /* Consume the `enum' token. */
15269 cp_lexer_consume_token (parser->lexer);
15270 /* Remember that it's an enumeration type. */
15271 tag_type = enum_type;
15272 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15273 enums) is used here. */
15274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15275 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15277 pedwarn (input_location, 0, "elaborated-type-specifier "
15278 "for a scoped enum must not use the %<%D%> keyword",
15279 cp_lexer_peek_token (parser->lexer)->u.value);
15280 /* Consume the `struct' or `class' and parse it anyway. */
15281 cp_lexer_consume_token (parser->lexer);
15283 /* Parse the attributes. */
15284 attributes = cp_parser_attributes_opt (parser);
15286 /* Or, it might be `typename'. */
15287 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15288 RID_TYPENAME))
15290 /* Consume the `typename' token. */
15291 cp_lexer_consume_token (parser->lexer);
15292 /* Remember that it's a `typename' type. */
15293 tag_type = typename_type;
15295 /* Otherwise it must be a class-key. */
15296 else
15298 tag_type = cp_parser_class_key (parser);
15299 if (tag_type == none_type)
15300 return error_mark_node;
15301 /* Parse the attributes. */
15302 attributes = cp_parser_attributes_opt (parser);
15305 /* Look for the `::' operator. */
15306 globalscope = cp_parser_global_scope_opt (parser,
15307 /*current_scope_valid_p=*/false);
15308 /* Look for the nested-name-specifier. */
15309 if (tag_type == typename_type && !globalscope)
15311 if (!cp_parser_nested_name_specifier (parser,
15312 /*typename_keyword_p=*/true,
15313 /*check_dependency_p=*/true,
15314 /*type_p=*/true,
15315 is_declaration))
15316 return error_mark_node;
15318 else
15319 /* Even though `typename' is not present, the proposed resolution
15320 to Core Issue 180 says that in `class A<T>::B', `B' should be
15321 considered a type-name, even if `A<T>' is dependent. */
15322 cp_parser_nested_name_specifier_opt (parser,
15323 /*typename_keyword_p=*/true,
15324 /*check_dependency_p=*/true,
15325 /*type_p=*/true,
15326 is_declaration);
15327 /* For everything but enumeration types, consider a template-id.
15328 For an enumeration type, consider only a plain identifier. */
15329 if (tag_type != enum_type)
15331 bool template_p = false;
15332 tree decl;
15334 /* Allow the `template' keyword. */
15335 template_p = cp_parser_optional_template_keyword (parser);
15336 /* If we didn't see `template', we don't know if there's a
15337 template-id or not. */
15338 if (!template_p)
15339 cp_parser_parse_tentatively (parser);
15340 /* Parse the template-id. */
15341 token = cp_lexer_peek_token (parser->lexer);
15342 decl = cp_parser_template_id (parser, template_p,
15343 /*check_dependency_p=*/true,
15344 tag_type,
15345 is_declaration);
15346 /* If we didn't find a template-id, look for an ordinary
15347 identifier. */
15348 if (!template_p && !cp_parser_parse_definitely (parser))
15350 /* We can get here when cp_parser_template_id, called by
15351 cp_parser_class_name with tag_type == none_type, succeeds
15352 and caches a BASELINK. Then, when called again here,
15353 instead of failing and returning an error_mark_node
15354 returns it (see template/typename17.C in C++11).
15355 ??? Could we diagnose this earlier? */
15356 else if (tag_type == typename_type && BASELINK_P (decl))
15358 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15359 type = error_mark_node;
15361 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15362 in effect, then we must assume that, upon instantiation, the
15363 template will correspond to a class. */
15364 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15365 && tag_type == typename_type)
15366 type = make_typename_type (parser->scope, decl,
15367 typename_type,
15368 /*complain=*/tf_error);
15369 /* If the `typename' keyword is in effect and DECL is not a type
15370 decl, then type is non existent. */
15371 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15373 else if (TREE_CODE (decl) == TYPE_DECL)
15374 type = check_elaborated_type_specifier (tag_type, decl,
15375 /*allow_template_p=*/true);
15376 else if (decl == error_mark_node)
15377 type = error_mark_node;
15380 if (!type)
15382 token = cp_lexer_peek_token (parser->lexer);
15383 identifier = cp_parser_identifier (parser);
15385 if (identifier == error_mark_node)
15387 parser->scope = NULL_TREE;
15388 return error_mark_node;
15391 /* For a `typename', we needn't call xref_tag. */
15392 if (tag_type == typename_type
15393 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15394 return cp_parser_make_typename_type (parser, identifier,
15395 token->location);
15397 /* Template parameter lists apply only if we are not within a
15398 function parameter list. */
15399 bool template_parm_lists_apply
15400 = parser->num_template_parameter_lists;
15401 if (template_parm_lists_apply)
15402 for (cp_binding_level *s = current_binding_level;
15403 s && s->kind != sk_template_parms;
15404 s = s->level_chain)
15405 if (s->kind == sk_function_parms)
15406 template_parm_lists_apply = false;
15408 /* Look up a qualified name in the usual way. */
15409 if (parser->scope)
15411 tree decl;
15412 tree ambiguous_decls;
15414 decl = cp_parser_lookup_name (parser, identifier,
15415 tag_type,
15416 /*is_template=*/false,
15417 /*is_namespace=*/false,
15418 /*check_dependency=*/true,
15419 &ambiguous_decls,
15420 token->location);
15422 /* If the lookup was ambiguous, an error will already have been
15423 issued. */
15424 if (ambiguous_decls)
15425 return error_mark_node;
15427 /* If we are parsing friend declaration, DECL may be a
15428 TEMPLATE_DECL tree node here. However, we need to check
15429 whether this TEMPLATE_DECL results in valid code. Consider
15430 the following example:
15432 namespace N {
15433 template <class T> class C {};
15435 class X {
15436 template <class T> friend class N::C; // #1, valid code
15438 template <class T> class Y {
15439 friend class N::C; // #2, invalid code
15442 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15443 name lookup of `N::C'. We see that friend declaration must
15444 be template for the code to be valid. Note that
15445 processing_template_decl does not work here since it is
15446 always 1 for the above two cases. */
15448 decl = (cp_parser_maybe_treat_template_as_class
15449 (decl, /*tag_name_p=*/is_friend
15450 && template_parm_lists_apply));
15452 if (TREE_CODE (decl) != TYPE_DECL)
15454 cp_parser_diagnose_invalid_type_name (parser,
15455 identifier,
15456 token->location);
15457 return error_mark_node;
15460 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15462 bool allow_template = (template_parm_lists_apply
15463 || DECL_SELF_REFERENCE_P (decl));
15464 type = check_elaborated_type_specifier (tag_type, decl,
15465 allow_template);
15467 if (type == error_mark_node)
15468 return error_mark_node;
15471 /* Forward declarations of nested types, such as
15473 class C1::C2;
15474 class C1::C2::C3;
15476 are invalid unless all components preceding the final '::'
15477 are complete. If all enclosing types are complete, these
15478 declarations become merely pointless.
15480 Invalid forward declarations of nested types are errors
15481 caught elsewhere in parsing. Those that are pointless arrive
15482 here. */
15484 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15485 && !is_friend && !processing_explicit_instantiation)
15486 warning (0, "declaration %qD does not declare anything", decl);
15488 type = TREE_TYPE (decl);
15490 else
15492 /* An elaborated-type-specifier sometimes introduces a new type and
15493 sometimes names an existing type. Normally, the rule is that it
15494 introduces a new type only if there is not an existing type of
15495 the same name already in scope. For example, given:
15497 struct S {};
15498 void f() { struct S s; }
15500 the `struct S' in the body of `f' is the same `struct S' as in
15501 the global scope; the existing definition is used. However, if
15502 there were no global declaration, this would introduce a new
15503 local class named `S'.
15505 An exception to this rule applies to the following code:
15507 namespace N { struct S; }
15509 Here, the elaborated-type-specifier names a new type
15510 unconditionally; even if there is already an `S' in the
15511 containing scope this declaration names a new type.
15512 This exception only applies if the elaborated-type-specifier
15513 forms the complete declaration:
15515 [class.name]
15517 A declaration consisting solely of `class-key identifier ;' is
15518 either a redeclaration of the name in the current scope or a
15519 forward declaration of the identifier as a class name. It
15520 introduces the name into the current scope.
15522 We are in this situation precisely when the next token is a `;'.
15524 An exception to the exception is that a `friend' declaration does
15525 *not* name a new type; i.e., given:
15527 struct S { friend struct T; };
15529 `T' is not a new type in the scope of `S'.
15531 Also, `new struct S' or `sizeof (struct S)' never results in the
15532 definition of a new type; a new type can only be declared in a
15533 declaration context. */
15535 tag_scope ts;
15536 bool template_p;
15538 if (is_friend)
15539 /* Friends have special name lookup rules. */
15540 ts = ts_within_enclosing_non_class;
15541 else if (is_declaration
15542 && cp_lexer_next_token_is (parser->lexer,
15543 CPP_SEMICOLON))
15544 /* This is a `class-key identifier ;' */
15545 ts = ts_current;
15546 else
15547 ts = ts_global;
15549 template_p =
15550 (template_parm_lists_apply
15551 && (cp_parser_next_token_starts_class_definition_p (parser)
15552 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15553 /* An unqualified name was used to reference this type, so
15554 there were no qualifying templates. */
15555 if (template_parm_lists_apply
15556 && !cp_parser_check_template_parameters (parser,
15557 /*num_templates=*/0,
15558 token->location,
15559 /*declarator=*/NULL))
15560 return error_mark_node;
15561 type = xref_tag (tag_type, identifier, ts, template_p);
15565 if (type == error_mark_node)
15566 return error_mark_node;
15568 /* Allow attributes on forward declarations of classes. */
15569 if (attributes)
15571 if (TREE_CODE (type) == TYPENAME_TYPE)
15572 warning (OPT_Wattributes,
15573 "attributes ignored on uninstantiated type");
15574 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15575 && ! processing_explicit_instantiation)
15576 warning (OPT_Wattributes,
15577 "attributes ignored on template instantiation");
15578 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15579 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15580 else
15581 warning (OPT_Wattributes,
15582 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15585 if (tag_type != enum_type)
15587 /* Indicate whether this class was declared as a `class' or as a
15588 `struct'. */
15589 if (TREE_CODE (type) == RECORD_TYPE)
15590 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15591 cp_parser_check_class_key (tag_type, type);
15594 /* A "<" cannot follow an elaborated type specifier. If that
15595 happens, the user was probably trying to form a template-id. */
15596 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15597 token->location);
15599 return type;
15602 /* Parse an enum-specifier.
15604 enum-specifier:
15605 enum-head { enumerator-list [opt] }
15606 enum-head { enumerator-list , } [C++0x]
15608 enum-head:
15609 enum-key identifier [opt] enum-base [opt]
15610 enum-key nested-name-specifier identifier enum-base [opt]
15612 enum-key:
15613 enum
15614 enum class [C++0x]
15615 enum struct [C++0x]
15617 enum-base: [C++0x]
15618 : type-specifier-seq
15620 opaque-enum-specifier:
15621 enum-key identifier enum-base [opt] ;
15623 GNU Extensions:
15624 enum-key attributes[opt] identifier [opt] enum-base [opt]
15625 { enumerator-list [opt] }attributes[opt]
15626 enum-key attributes[opt] identifier [opt] enum-base [opt]
15627 { enumerator-list, }attributes[opt] [C++0x]
15629 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15630 if the token stream isn't an enum-specifier after all. */
15632 static tree
15633 cp_parser_enum_specifier (cp_parser* parser)
15635 tree identifier;
15636 tree type = NULL_TREE;
15637 tree prev_scope;
15638 tree nested_name_specifier = NULL_TREE;
15639 tree attributes;
15640 bool scoped_enum_p = false;
15641 bool has_underlying_type = false;
15642 bool nested_being_defined = false;
15643 bool new_value_list = false;
15644 bool is_new_type = false;
15645 bool is_anonymous = false;
15646 tree underlying_type = NULL_TREE;
15647 cp_token *type_start_token = NULL;
15648 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15650 parser->colon_corrects_to_scope_p = false;
15652 /* Parse tentatively so that we can back up if we don't find a
15653 enum-specifier. */
15654 cp_parser_parse_tentatively (parser);
15656 /* Caller guarantees that the current token is 'enum', an identifier
15657 possibly follows, and the token after that is an opening brace.
15658 If we don't have an identifier, fabricate an anonymous name for
15659 the enumeration being defined. */
15660 cp_lexer_consume_token (parser->lexer);
15662 /* Parse the "class" or "struct", which indicates a scoped
15663 enumeration type in C++0x. */
15664 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15665 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15667 if (cxx_dialect < cxx11)
15668 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15670 /* Consume the `struct' or `class' token. */
15671 cp_lexer_consume_token (parser->lexer);
15673 scoped_enum_p = true;
15676 attributes = cp_parser_attributes_opt (parser);
15678 /* Clear the qualification. */
15679 parser->scope = NULL_TREE;
15680 parser->qualifying_scope = NULL_TREE;
15681 parser->object_scope = NULL_TREE;
15683 /* Figure out in what scope the declaration is being placed. */
15684 prev_scope = current_scope ();
15686 type_start_token = cp_lexer_peek_token (parser->lexer);
15688 push_deferring_access_checks (dk_no_check);
15689 nested_name_specifier
15690 = cp_parser_nested_name_specifier_opt (parser,
15691 /*typename_keyword_p=*/true,
15692 /*check_dependency_p=*/false,
15693 /*type_p=*/false,
15694 /*is_declaration=*/false);
15696 if (nested_name_specifier)
15698 tree name;
15700 identifier = cp_parser_identifier (parser);
15701 name = cp_parser_lookup_name (parser, identifier,
15702 enum_type,
15703 /*is_template=*/false,
15704 /*is_namespace=*/false,
15705 /*check_dependency=*/true,
15706 /*ambiguous_decls=*/NULL,
15707 input_location);
15708 if (name && name != error_mark_node)
15710 type = TREE_TYPE (name);
15711 if (TREE_CODE (type) == TYPENAME_TYPE)
15713 /* Are template enums allowed in ISO? */
15714 if (template_parm_scope_p ())
15715 pedwarn (type_start_token->location, OPT_Wpedantic,
15716 "%qD is an enumeration template", name);
15717 /* ignore a typename reference, for it will be solved by name
15718 in start_enum. */
15719 type = NULL_TREE;
15722 else if (nested_name_specifier == error_mark_node)
15723 /* We already issued an error. */;
15724 else
15725 error_at (type_start_token->location,
15726 "%qD is not an enumerator-name", identifier);
15728 else
15730 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15731 identifier = cp_parser_identifier (parser);
15732 else
15734 identifier = make_anon_name ();
15735 is_anonymous = true;
15736 if (scoped_enum_p)
15737 error_at (type_start_token->location,
15738 "anonymous scoped enum is not allowed");
15741 pop_deferring_access_checks ();
15743 /* Check for the `:' that denotes a specified underlying type in C++0x.
15744 Note that a ':' could also indicate a bitfield width, however. */
15745 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15747 cp_decl_specifier_seq type_specifiers;
15749 /* Consume the `:'. */
15750 cp_lexer_consume_token (parser->lexer);
15752 /* Parse the type-specifier-seq. */
15753 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15754 /*is_trailing_return=*/false,
15755 &type_specifiers);
15757 /* At this point this is surely not elaborated type specifier. */
15758 if (!cp_parser_parse_definitely (parser))
15759 return NULL_TREE;
15761 if (cxx_dialect < cxx11)
15762 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15764 has_underlying_type = true;
15766 /* If that didn't work, stop. */
15767 if (type_specifiers.type != error_mark_node)
15769 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15770 /*initialized=*/0, NULL);
15771 if (underlying_type == error_mark_node
15772 || check_for_bare_parameter_packs (underlying_type))
15773 underlying_type = NULL_TREE;
15777 /* Look for the `{' but don't consume it yet. */
15778 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15780 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15782 cp_parser_error (parser, "expected %<{%>");
15783 if (has_underlying_type)
15785 type = NULL_TREE;
15786 goto out;
15789 /* An opaque-enum-specifier must have a ';' here. */
15790 if ((scoped_enum_p || underlying_type)
15791 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15793 cp_parser_error (parser, "expected %<;%> or %<{%>");
15794 if (has_underlying_type)
15796 type = NULL_TREE;
15797 goto out;
15802 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15803 return NULL_TREE;
15805 if (nested_name_specifier)
15807 if (CLASS_TYPE_P (nested_name_specifier))
15809 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15810 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15811 push_scope (nested_name_specifier);
15813 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15815 push_nested_namespace (nested_name_specifier);
15819 /* Issue an error message if type-definitions are forbidden here. */
15820 if (!cp_parser_check_type_definition (parser))
15821 type = error_mark_node;
15822 else
15823 /* Create the new type. We do this before consuming the opening
15824 brace so the enum will be recorded as being on the line of its
15825 tag (or the 'enum' keyword, if there is no tag). */
15826 type = start_enum (identifier, type, underlying_type,
15827 scoped_enum_p, &is_new_type);
15829 /* If the next token is not '{' it is an opaque-enum-specifier or an
15830 elaborated-type-specifier. */
15831 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15833 timevar_push (TV_PARSE_ENUM);
15834 if (nested_name_specifier
15835 && nested_name_specifier != error_mark_node)
15837 /* The following catches invalid code such as:
15838 enum class S<int>::E { A, B, C }; */
15839 if (!processing_specialization
15840 && CLASS_TYPE_P (nested_name_specifier)
15841 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15842 error_at (type_start_token->location, "cannot add an enumerator "
15843 "list to a template instantiation");
15845 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15847 error_at (type_start_token->location,
15848 "%<%T::%E%> has not been declared",
15849 TYPE_CONTEXT (nested_name_specifier),
15850 nested_name_specifier);
15851 type = error_mark_node;
15853 /* If that scope does not contain the scope in which the
15854 class was originally declared, the program is invalid. */
15855 else if (prev_scope && !is_ancestor (prev_scope,
15856 nested_name_specifier))
15858 if (at_namespace_scope_p ())
15859 error_at (type_start_token->location,
15860 "declaration of %qD in namespace %qD which does not "
15861 "enclose %qD",
15862 type, prev_scope, nested_name_specifier);
15863 else
15864 error_at (type_start_token->location,
15865 "declaration of %qD in %qD which does not "
15866 "enclose %qD",
15867 type, prev_scope, nested_name_specifier);
15868 type = error_mark_node;
15872 if (scoped_enum_p)
15873 begin_scope (sk_scoped_enum, type);
15875 /* Consume the opening brace. */
15876 cp_lexer_consume_token (parser->lexer);
15878 if (type == error_mark_node)
15879 ; /* Nothing to add */
15880 else if (OPAQUE_ENUM_P (type)
15881 || (cxx_dialect > cxx98 && processing_specialization))
15883 new_value_list = true;
15884 SET_OPAQUE_ENUM_P (type, false);
15885 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15887 else
15889 error_at (type_start_token->location,
15890 "multiple definition of %q#T", type);
15891 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15892 "previous definition here");
15893 type = error_mark_node;
15896 if (type == error_mark_node)
15897 cp_parser_skip_to_end_of_block_or_statement (parser);
15898 /* If the next token is not '}', then there are some enumerators. */
15899 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15901 if (is_anonymous && !scoped_enum_p)
15902 pedwarn (type_start_token->location, OPT_Wpedantic,
15903 "ISO C++ forbids empty anonymous enum");
15905 else
15906 cp_parser_enumerator_list (parser, type);
15908 /* Consume the final '}'. */
15909 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15911 if (scoped_enum_p)
15912 finish_scope ();
15913 timevar_pop (TV_PARSE_ENUM);
15915 else
15917 /* If a ';' follows, then it is an opaque-enum-specifier
15918 and additional restrictions apply. */
15919 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15921 if (is_anonymous)
15922 error_at (type_start_token->location,
15923 "opaque-enum-specifier without name");
15924 else if (nested_name_specifier)
15925 error_at (type_start_token->location,
15926 "opaque-enum-specifier must use a simple identifier");
15930 /* Look for trailing attributes to apply to this enumeration, and
15931 apply them if appropriate. */
15932 if (cp_parser_allow_gnu_extensions_p (parser))
15934 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15935 trailing_attr = chainon (trailing_attr, attributes);
15936 cplus_decl_attributes (&type,
15937 trailing_attr,
15938 (int) ATTR_FLAG_TYPE_IN_PLACE);
15941 /* Finish up the enumeration. */
15942 if (type != error_mark_node)
15944 if (new_value_list)
15945 finish_enum_value_list (type);
15946 if (is_new_type)
15947 finish_enum (type);
15950 if (nested_name_specifier)
15952 if (CLASS_TYPE_P (nested_name_specifier))
15954 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15955 pop_scope (nested_name_specifier);
15957 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15959 pop_nested_namespace (nested_name_specifier);
15962 out:
15963 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15964 return type;
15967 /* Parse an enumerator-list. The enumerators all have the indicated
15968 TYPE.
15970 enumerator-list:
15971 enumerator-definition
15972 enumerator-list , enumerator-definition */
15974 static void
15975 cp_parser_enumerator_list (cp_parser* parser, tree type)
15977 while (true)
15979 /* Parse an enumerator-definition. */
15980 cp_parser_enumerator_definition (parser, type);
15982 /* If the next token is not a ',', we've reached the end of
15983 the list. */
15984 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15985 break;
15986 /* Otherwise, consume the `,' and keep going. */
15987 cp_lexer_consume_token (parser->lexer);
15988 /* If the next token is a `}', there is a trailing comma. */
15989 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15991 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15992 pedwarn (input_location, OPT_Wpedantic,
15993 "comma at end of enumerator list");
15994 break;
15999 /* Parse an enumerator-definition. The enumerator has the indicated
16000 TYPE.
16002 enumerator-definition:
16003 enumerator
16004 enumerator = constant-expression
16006 enumerator:
16007 identifier */
16009 static void
16010 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16012 tree identifier;
16013 tree value;
16014 location_t loc;
16016 /* Save the input location because we are interested in the location
16017 of the identifier and not the location of the explicit value. */
16018 loc = cp_lexer_peek_token (parser->lexer)->location;
16020 /* Look for the identifier. */
16021 identifier = cp_parser_identifier (parser);
16022 if (identifier == error_mark_node)
16023 return;
16025 /* If the next token is an '=', then there is an explicit value. */
16026 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16028 /* Consume the `=' token. */
16029 cp_lexer_consume_token (parser->lexer);
16030 /* Parse the value. */
16031 value = cp_parser_constant_expression (parser);
16033 else
16034 value = NULL_TREE;
16036 /* If we are processing a template, make sure the initializer of the
16037 enumerator doesn't contain any bare template parameter pack. */
16038 if (check_for_bare_parameter_packs (value))
16039 value = error_mark_node;
16041 /* Create the enumerator. */
16042 build_enumerator (identifier, value, type, loc);
16045 /* Parse a namespace-name.
16047 namespace-name:
16048 original-namespace-name
16049 namespace-alias
16051 Returns the NAMESPACE_DECL for the namespace. */
16053 static tree
16054 cp_parser_namespace_name (cp_parser* parser)
16056 tree identifier;
16057 tree namespace_decl;
16059 cp_token *token = cp_lexer_peek_token (parser->lexer);
16061 /* Get the name of the namespace. */
16062 identifier = cp_parser_identifier (parser);
16063 if (identifier == error_mark_node)
16064 return error_mark_node;
16066 /* Look up the identifier in the currently active scope. Look only
16067 for namespaces, due to:
16069 [basic.lookup.udir]
16071 When looking up a namespace-name in a using-directive or alias
16072 definition, only namespace names are considered.
16074 And:
16076 [basic.lookup.qual]
16078 During the lookup of a name preceding the :: scope resolution
16079 operator, object, function, and enumerator names are ignored.
16081 (Note that cp_parser_qualifying_entity only calls this
16082 function if the token after the name is the scope resolution
16083 operator.) */
16084 namespace_decl = cp_parser_lookup_name (parser, identifier,
16085 none_type,
16086 /*is_template=*/false,
16087 /*is_namespace=*/true,
16088 /*check_dependency=*/true,
16089 /*ambiguous_decls=*/NULL,
16090 token->location);
16091 /* If it's not a namespace, issue an error. */
16092 if (namespace_decl == error_mark_node
16093 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16095 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16096 error_at (token->location, "%qD is not a namespace-name", identifier);
16097 cp_parser_error (parser, "expected namespace-name");
16098 namespace_decl = error_mark_node;
16101 return namespace_decl;
16104 /* Parse a namespace-definition.
16106 namespace-definition:
16107 named-namespace-definition
16108 unnamed-namespace-definition
16110 named-namespace-definition:
16111 original-namespace-definition
16112 extension-namespace-definition
16114 original-namespace-definition:
16115 namespace identifier { namespace-body }
16117 extension-namespace-definition:
16118 namespace original-namespace-name { namespace-body }
16120 unnamed-namespace-definition:
16121 namespace { namespace-body } */
16123 static void
16124 cp_parser_namespace_definition (cp_parser* parser)
16126 tree identifier, attribs;
16127 bool has_visibility;
16128 bool is_inline;
16130 cp_ensure_no_omp_declare_simd (parser);
16131 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16133 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16134 is_inline = true;
16135 cp_lexer_consume_token (parser->lexer);
16137 else
16138 is_inline = false;
16140 /* Look for the `namespace' keyword. */
16141 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16143 /* Get the name of the namespace. We do not attempt to distinguish
16144 between an original-namespace-definition and an
16145 extension-namespace-definition at this point. The semantic
16146 analysis routines are responsible for that. */
16147 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16148 identifier = cp_parser_identifier (parser);
16149 else
16150 identifier = NULL_TREE;
16152 /* Parse any specified attributes. */
16153 attribs = cp_parser_attributes_opt (parser);
16155 /* Look for the `{' to start the namespace. */
16156 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16157 /* Start the namespace. */
16158 push_namespace (identifier);
16160 /* "inline namespace" is equivalent to a stub namespace definition
16161 followed by a strong using directive. */
16162 if (is_inline)
16164 tree name_space = current_namespace;
16165 /* Set up namespace association. */
16166 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16167 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16168 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16169 /* Import the contents of the inline namespace. */
16170 pop_namespace ();
16171 do_using_directive (name_space);
16172 push_namespace (identifier);
16175 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16177 /* Parse the body of the namespace. */
16178 cp_parser_namespace_body (parser);
16180 if (has_visibility)
16181 pop_visibility (1);
16183 /* Finish the namespace. */
16184 pop_namespace ();
16185 /* Look for the final `}'. */
16186 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16189 /* Parse a namespace-body.
16191 namespace-body:
16192 declaration-seq [opt] */
16194 static void
16195 cp_parser_namespace_body (cp_parser* parser)
16197 cp_parser_declaration_seq_opt (parser);
16200 /* Parse a namespace-alias-definition.
16202 namespace-alias-definition:
16203 namespace identifier = qualified-namespace-specifier ; */
16205 static void
16206 cp_parser_namespace_alias_definition (cp_parser* parser)
16208 tree identifier;
16209 tree namespace_specifier;
16211 cp_token *token = cp_lexer_peek_token (parser->lexer);
16213 /* Look for the `namespace' keyword. */
16214 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16215 /* Look for the identifier. */
16216 identifier = cp_parser_identifier (parser);
16217 if (identifier == error_mark_node)
16218 return;
16219 /* Look for the `=' token. */
16220 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16221 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16223 error_at (token->location, "%<namespace%> definition is not allowed here");
16224 /* Skip the definition. */
16225 cp_lexer_consume_token (parser->lexer);
16226 if (cp_parser_skip_to_closing_brace (parser))
16227 cp_lexer_consume_token (parser->lexer);
16228 return;
16230 cp_parser_require (parser, CPP_EQ, RT_EQ);
16231 /* Look for the qualified-namespace-specifier. */
16232 namespace_specifier
16233 = cp_parser_qualified_namespace_specifier (parser);
16234 /* Look for the `;' token. */
16235 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16237 /* Register the alias in the symbol table. */
16238 do_namespace_alias (identifier, namespace_specifier);
16241 /* Parse a qualified-namespace-specifier.
16243 qualified-namespace-specifier:
16244 :: [opt] nested-name-specifier [opt] namespace-name
16246 Returns a NAMESPACE_DECL corresponding to the specified
16247 namespace. */
16249 static tree
16250 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16252 /* Look for the optional `::'. */
16253 cp_parser_global_scope_opt (parser,
16254 /*current_scope_valid_p=*/false);
16256 /* Look for the optional nested-name-specifier. */
16257 cp_parser_nested_name_specifier_opt (parser,
16258 /*typename_keyword_p=*/false,
16259 /*check_dependency_p=*/true,
16260 /*type_p=*/false,
16261 /*is_declaration=*/true);
16263 return cp_parser_namespace_name (parser);
16266 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16267 access declaration.
16269 using-declaration:
16270 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16271 using :: unqualified-id ;
16273 access-declaration:
16274 qualified-id ;
16278 static bool
16279 cp_parser_using_declaration (cp_parser* parser,
16280 bool access_declaration_p)
16282 cp_token *token;
16283 bool typename_p = false;
16284 bool global_scope_p;
16285 tree decl;
16286 tree identifier;
16287 tree qscope;
16288 int oldcount = errorcount;
16289 cp_token *diag_token = NULL;
16291 if (access_declaration_p)
16293 diag_token = cp_lexer_peek_token (parser->lexer);
16294 cp_parser_parse_tentatively (parser);
16296 else
16298 /* Look for the `using' keyword. */
16299 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16301 /* Peek at the next token. */
16302 token = cp_lexer_peek_token (parser->lexer);
16303 /* See if it's `typename'. */
16304 if (token->keyword == RID_TYPENAME)
16306 /* Remember that we've seen it. */
16307 typename_p = true;
16308 /* Consume the `typename' token. */
16309 cp_lexer_consume_token (parser->lexer);
16313 /* Look for the optional global scope qualification. */
16314 global_scope_p
16315 = (cp_parser_global_scope_opt (parser,
16316 /*current_scope_valid_p=*/false)
16317 != NULL_TREE);
16319 /* If we saw `typename', or didn't see `::', then there must be a
16320 nested-name-specifier present. */
16321 if (typename_p || !global_scope_p)
16323 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16324 /*check_dependency_p=*/true,
16325 /*type_p=*/false,
16326 /*is_declaration=*/true);
16327 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16329 cp_parser_skip_to_end_of_block_or_statement (parser);
16330 return false;
16333 /* Otherwise, we could be in either of the two productions. In that
16334 case, treat the nested-name-specifier as optional. */
16335 else
16336 qscope = cp_parser_nested_name_specifier_opt (parser,
16337 /*typename_keyword_p=*/false,
16338 /*check_dependency_p=*/true,
16339 /*type_p=*/false,
16340 /*is_declaration=*/true);
16341 if (!qscope)
16342 qscope = global_namespace;
16343 else if (UNSCOPED_ENUM_P (qscope))
16344 qscope = CP_TYPE_CONTEXT (qscope);
16346 if (access_declaration_p && cp_parser_error_occurred (parser))
16347 /* Something has already gone wrong; there's no need to parse
16348 further. Since an error has occurred, the return value of
16349 cp_parser_parse_definitely will be false, as required. */
16350 return cp_parser_parse_definitely (parser);
16352 token = cp_lexer_peek_token (parser->lexer);
16353 /* Parse the unqualified-id. */
16354 identifier = cp_parser_unqualified_id (parser,
16355 /*template_keyword_p=*/false,
16356 /*check_dependency_p=*/true,
16357 /*declarator_p=*/true,
16358 /*optional_p=*/false);
16360 if (access_declaration_p)
16362 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16363 cp_parser_simulate_error (parser);
16364 if (!cp_parser_parse_definitely (parser))
16365 return false;
16368 /* The function we call to handle a using-declaration is different
16369 depending on what scope we are in. */
16370 if (qscope == error_mark_node || identifier == error_mark_node)
16372 else if (!identifier_p (identifier)
16373 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16374 /* [namespace.udecl]
16376 A using declaration shall not name a template-id. */
16377 error_at (token->location,
16378 "a template-id may not appear in a using-declaration");
16379 else
16381 if (at_class_scope_p ())
16383 /* Create the USING_DECL. */
16384 decl = do_class_using_decl (parser->scope, identifier);
16386 if (decl && typename_p)
16387 USING_DECL_TYPENAME_P (decl) = 1;
16389 if (check_for_bare_parameter_packs (decl))
16391 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16392 return false;
16394 else
16395 /* Add it to the list of members in this class. */
16396 finish_member_declaration (decl);
16398 else
16400 decl = cp_parser_lookup_name_simple (parser,
16401 identifier,
16402 token->location);
16403 if (decl == error_mark_node)
16404 cp_parser_name_lookup_error (parser, identifier,
16405 decl, NLE_NULL,
16406 token->location);
16407 else if (check_for_bare_parameter_packs (decl))
16409 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16410 return false;
16412 else if (!at_namespace_scope_p ())
16413 do_local_using_decl (decl, qscope, identifier);
16414 else
16415 do_toplevel_using_decl (decl, qscope, identifier);
16419 /* Look for the final `;'. */
16420 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16422 if (access_declaration_p && errorcount == oldcount)
16423 warning_at (diag_token->location, OPT_Wdeprecated,
16424 "access declarations are deprecated "
16425 "in favour of using-declarations; "
16426 "suggestion: add the %<using%> keyword");
16428 return true;
16431 /* Parse an alias-declaration.
16433 alias-declaration:
16434 using identifier attribute-specifier-seq [opt] = type-id */
16436 static tree
16437 cp_parser_alias_declaration (cp_parser* parser)
16439 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16440 location_t id_location;
16441 cp_declarator *declarator;
16442 cp_decl_specifier_seq decl_specs;
16443 bool member_p;
16444 const char *saved_message = NULL;
16446 /* Look for the `using' keyword. */
16447 cp_token *using_token
16448 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16449 if (using_token == NULL)
16450 return error_mark_node;
16452 id_location = cp_lexer_peek_token (parser->lexer)->location;
16453 id = cp_parser_identifier (parser);
16454 if (id == error_mark_node)
16455 return error_mark_node;
16457 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16458 attributes = cp_parser_attributes_opt (parser);
16459 if (attributes == error_mark_node)
16460 return error_mark_node;
16462 cp_parser_require (parser, CPP_EQ, RT_EQ);
16464 if (cp_parser_error_occurred (parser))
16465 return error_mark_node;
16467 cp_parser_commit_to_tentative_parse (parser);
16469 /* Now we are going to parse the type-id of the declaration. */
16472 [dcl.type]/3 says:
16474 "A type-specifier-seq shall not define a class or enumeration
16475 unless it appears in the type-id of an alias-declaration (7.1.3) that
16476 is not the declaration of a template-declaration."
16478 In other words, if we currently are in an alias template, the
16479 type-id should not define a type.
16481 So let's set parser->type_definition_forbidden_message in that
16482 case; cp_parser_check_type_definition (called by
16483 cp_parser_class_specifier) will then emit an error if a type is
16484 defined in the type-id. */
16485 if (parser->num_template_parameter_lists)
16487 saved_message = parser->type_definition_forbidden_message;
16488 parser->type_definition_forbidden_message =
16489 G_("types may not be defined in alias template declarations");
16492 type = cp_parser_type_id (parser);
16494 /* Restore the error message if need be. */
16495 if (parser->num_template_parameter_lists)
16496 parser->type_definition_forbidden_message = saved_message;
16498 if (type == error_mark_node
16499 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16501 cp_parser_skip_to_end_of_block_or_statement (parser);
16502 return error_mark_node;
16505 /* A typedef-name can also be introduced by an alias-declaration. The
16506 identifier following the using keyword becomes a typedef-name. It has
16507 the same semantics as if it were introduced by the typedef
16508 specifier. In particular, it does not define a new type and it shall
16509 not appear in the type-id. */
16511 clear_decl_specs (&decl_specs);
16512 decl_specs.type = type;
16513 if (attributes != NULL_TREE)
16515 decl_specs.attributes = attributes;
16516 set_and_check_decl_spec_loc (&decl_specs,
16517 ds_attribute,
16518 attrs_token);
16520 set_and_check_decl_spec_loc (&decl_specs,
16521 ds_typedef,
16522 using_token);
16523 set_and_check_decl_spec_loc (&decl_specs,
16524 ds_alias,
16525 using_token);
16527 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16528 declarator->id_loc = id_location;
16530 member_p = at_class_scope_p ();
16531 if (member_p)
16532 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16533 NULL_TREE, attributes);
16534 else
16535 decl = start_decl (declarator, &decl_specs, 0,
16536 attributes, NULL_TREE, &pushed_scope);
16537 if (decl == error_mark_node)
16538 return decl;
16540 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16542 if (pushed_scope)
16543 pop_scope (pushed_scope);
16545 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16546 added into the symbol table; otherwise, return the TYPE_DECL. */
16547 if (DECL_LANG_SPECIFIC (decl)
16548 && DECL_TEMPLATE_INFO (decl)
16549 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16551 decl = DECL_TI_TEMPLATE (decl);
16552 if (member_p)
16553 check_member_template (decl);
16556 return decl;
16559 /* Parse a using-directive.
16561 using-directive:
16562 using namespace :: [opt] nested-name-specifier [opt]
16563 namespace-name ; */
16565 static void
16566 cp_parser_using_directive (cp_parser* parser)
16568 tree namespace_decl;
16569 tree attribs;
16571 /* Look for the `using' keyword. */
16572 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16573 /* And the `namespace' keyword. */
16574 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16575 /* Look for the optional `::' operator. */
16576 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16577 /* And the optional nested-name-specifier. */
16578 cp_parser_nested_name_specifier_opt (parser,
16579 /*typename_keyword_p=*/false,
16580 /*check_dependency_p=*/true,
16581 /*type_p=*/false,
16582 /*is_declaration=*/true);
16583 /* Get the namespace being used. */
16584 namespace_decl = cp_parser_namespace_name (parser);
16585 /* And any specified attributes. */
16586 attribs = cp_parser_attributes_opt (parser);
16587 /* Update the symbol table. */
16588 parse_using_directive (namespace_decl, attribs);
16589 /* Look for the final `;'. */
16590 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16593 /* Parse an asm-definition.
16595 asm-definition:
16596 asm ( string-literal ) ;
16598 GNU Extension:
16600 asm-definition:
16601 asm volatile [opt] ( string-literal ) ;
16602 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16603 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16604 : asm-operand-list [opt] ) ;
16605 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16606 : asm-operand-list [opt]
16607 : asm-clobber-list [opt] ) ;
16608 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16609 : asm-clobber-list [opt]
16610 : asm-goto-list ) ; */
16612 static void
16613 cp_parser_asm_definition (cp_parser* parser)
16615 tree string;
16616 tree outputs = NULL_TREE;
16617 tree inputs = NULL_TREE;
16618 tree clobbers = NULL_TREE;
16619 tree labels = NULL_TREE;
16620 tree asm_stmt;
16621 bool volatile_p = false;
16622 bool extended_p = false;
16623 bool invalid_inputs_p = false;
16624 bool invalid_outputs_p = false;
16625 bool goto_p = false;
16626 required_token missing = RT_NONE;
16628 /* Look for the `asm' keyword. */
16629 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16631 if (parser->in_function_body
16632 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16634 error ("%<asm%> in %<constexpr%> function");
16635 cp_function_chain->invalid_constexpr = true;
16638 /* See if the next token is `volatile'. */
16639 if (cp_parser_allow_gnu_extensions_p (parser)
16640 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16642 /* Remember that we saw the `volatile' keyword. */
16643 volatile_p = true;
16644 /* Consume the token. */
16645 cp_lexer_consume_token (parser->lexer);
16647 if (cp_parser_allow_gnu_extensions_p (parser)
16648 && parser->in_function_body
16649 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16651 /* Remember that we saw the `goto' keyword. */
16652 goto_p = true;
16653 /* Consume the token. */
16654 cp_lexer_consume_token (parser->lexer);
16656 /* Look for the opening `('. */
16657 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16658 return;
16659 /* Look for the string. */
16660 string = cp_parser_string_literal (parser, false, false);
16661 if (string == error_mark_node)
16663 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16664 /*consume_paren=*/true);
16665 return;
16668 /* If we're allowing GNU extensions, check for the extended assembly
16669 syntax. Unfortunately, the `:' tokens need not be separated by
16670 a space in C, and so, for compatibility, we tolerate that here
16671 too. Doing that means that we have to treat the `::' operator as
16672 two `:' tokens. */
16673 if (cp_parser_allow_gnu_extensions_p (parser)
16674 && parser->in_function_body
16675 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16676 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16678 bool inputs_p = false;
16679 bool clobbers_p = false;
16680 bool labels_p = false;
16682 /* The extended syntax was used. */
16683 extended_p = true;
16685 /* Look for outputs. */
16686 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16688 /* Consume the `:'. */
16689 cp_lexer_consume_token (parser->lexer);
16690 /* Parse the output-operands. */
16691 if (cp_lexer_next_token_is_not (parser->lexer,
16692 CPP_COLON)
16693 && cp_lexer_next_token_is_not (parser->lexer,
16694 CPP_SCOPE)
16695 && cp_lexer_next_token_is_not (parser->lexer,
16696 CPP_CLOSE_PAREN)
16697 && !goto_p)
16698 outputs = cp_parser_asm_operand_list (parser);
16700 if (outputs == error_mark_node)
16701 invalid_outputs_p = true;
16703 /* If the next token is `::', there are no outputs, and the
16704 next token is the beginning of the inputs. */
16705 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16706 /* The inputs are coming next. */
16707 inputs_p = true;
16709 /* Look for inputs. */
16710 if (inputs_p
16711 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16713 /* Consume the `:' or `::'. */
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 inputs = cp_parser_asm_operand_list (parser);
16724 if (inputs == error_mark_node)
16725 invalid_inputs_p = true;
16727 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16728 /* The clobbers are coming next. */
16729 clobbers_p = true;
16731 /* Look for clobbers. */
16732 if (clobbers_p
16733 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16735 clobbers_p = true;
16736 /* Consume the `:' or `::'. */
16737 cp_lexer_consume_token (parser->lexer);
16738 /* Parse the clobbers. */
16739 if (cp_lexer_next_token_is_not (parser->lexer,
16740 CPP_COLON)
16741 && cp_lexer_next_token_is_not (parser->lexer,
16742 CPP_CLOSE_PAREN))
16743 clobbers = cp_parser_asm_clobber_list (parser);
16745 else if (goto_p
16746 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16747 /* The labels are coming next. */
16748 labels_p = true;
16750 /* Look for labels. */
16751 if (labels_p
16752 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16754 labels_p = true;
16755 /* Consume the `:' or `::'. */
16756 cp_lexer_consume_token (parser->lexer);
16757 /* Parse the labels. */
16758 labels = cp_parser_asm_label_list (parser);
16761 if (goto_p && !labels_p)
16762 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16764 else if (goto_p)
16765 missing = RT_COLON_SCOPE;
16767 /* Look for the closing `)'. */
16768 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16769 missing ? missing : RT_CLOSE_PAREN))
16770 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16771 /*consume_paren=*/true);
16772 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16774 if (!invalid_inputs_p && !invalid_outputs_p)
16776 /* Create the ASM_EXPR. */
16777 if (parser->in_function_body)
16779 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16780 inputs, clobbers, labels);
16781 /* If the extended syntax was not used, mark the ASM_EXPR. */
16782 if (!extended_p)
16784 tree temp = asm_stmt;
16785 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16786 temp = TREE_OPERAND (temp, 0);
16788 ASM_INPUT_P (temp) = 1;
16791 else
16792 symtab->finalize_toplevel_asm (string);
16796 /* Declarators [gram.dcl.decl] */
16798 /* Parse an init-declarator.
16800 init-declarator:
16801 declarator initializer [opt]
16803 GNU Extension:
16805 init-declarator:
16806 declarator asm-specification [opt] attributes [opt] initializer [opt]
16808 function-definition:
16809 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16810 function-body
16811 decl-specifier-seq [opt] declarator function-try-block
16813 GNU Extension:
16815 function-definition:
16816 __extension__ function-definition
16818 TM Extension:
16820 function-definition:
16821 decl-specifier-seq [opt] declarator function-transaction-block
16823 The DECL_SPECIFIERS apply to this declarator. Returns a
16824 representation of the entity declared. If MEMBER_P is TRUE, then
16825 this declarator appears in a class scope. The new DECL created by
16826 this declarator is returned.
16828 The CHECKS are access checks that should be performed once we know
16829 what entity is being declared (and, therefore, what classes have
16830 befriended it).
16832 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16833 for a function-definition here as well. If the declarator is a
16834 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16835 be TRUE upon return. By that point, the function-definition will
16836 have been completely parsed.
16838 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16839 is FALSE.
16841 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16842 parsed declaration if it is an uninitialized single declarator not followed
16843 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16844 if present, will not be consumed. If returned, this declarator will be
16845 created with SD_INITIALIZED but will not call cp_finish_decl. */
16847 static tree
16848 cp_parser_init_declarator (cp_parser* parser,
16849 cp_decl_specifier_seq *decl_specifiers,
16850 vec<deferred_access_check, va_gc> *checks,
16851 bool function_definition_allowed_p,
16852 bool member_p,
16853 int declares_class_or_enum,
16854 bool* function_definition_p,
16855 tree* maybe_range_for_decl)
16857 cp_token *token = NULL, *asm_spec_start_token = NULL,
16858 *attributes_start_token = NULL;
16859 cp_declarator *declarator;
16860 tree prefix_attributes;
16861 tree attributes = NULL;
16862 tree asm_specification;
16863 tree initializer;
16864 tree decl = NULL_TREE;
16865 tree scope;
16866 int is_initialized;
16867 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16868 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16869 "(...)". */
16870 enum cpp_ttype initialization_kind;
16871 bool is_direct_init = false;
16872 bool is_non_constant_init;
16873 int ctor_dtor_or_conv_p;
16874 bool friend_p = cp_parser_friend_p (decl_specifiers);
16875 tree pushed_scope = NULL_TREE;
16876 bool range_for_decl_p = false;
16877 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16879 /* Gather the attributes that were provided with the
16880 decl-specifiers. */
16881 prefix_attributes = decl_specifiers->attributes;
16883 /* Assume that this is not the declarator for a function
16884 definition. */
16885 if (function_definition_p)
16886 *function_definition_p = false;
16888 /* Default arguments are only permitted for function parameters. */
16889 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16890 parser->default_arg_ok_p = false;
16892 /* Defer access checks while parsing the declarator; we cannot know
16893 what names are accessible until we know what is being
16894 declared. */
16895 resume_deferring_access_checks ();
16897 /* Parse the declarator. */
16898 token = cp_lexer_peek_token (parser->lexer);
16899 declarator
16900 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16901 &ctor_dtor_or_conv_p,
16902 /*parenthesized_p=*/NULL,
16903 member_p, friend_p);
16904 /* Gather up the deferred checks. */
16905 stop_deferring_access_checks ();
16907 parser->default_arg_ok_p = saved_default_arg_ok_p;
16909 /* If the DECLARATOR was erroneous, there's no need to go
16910 further. */
16911 if (declarator == cp_error_declarator)
16912 return error_mark_node;
16914 /* Check that the number of template-parameter-lists is OK. */
16915 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16916 token->location))
16917 return error_mark_node;
16919 if (declares_class_or_enum & 2)
16920 cp_parser_check_for_definition_in_return_type (declarator,
16921 decl_specifiers->type,
16922 decl_specifiers->locations[ds_type_spec]);
16924 /* Figure out what scope the entity declared by the DECLARATOR is
16925 located in. `grokdeclarator' sometimes changes the scope, so
16926 we compute it now. */
16927 scope = get_scope_of_declarator (declarator);
16929 /* Perform any lookups in the declared type which were thought to be
16930 dependent, but are not in the scope of the declarator. */
16931 decl_specifiers->type
16932 = maybe_update_decl_type (decl_specifiers->type, scope);
16934 /* If we're allowing GNU extensions, look for an
16935 asm-specification. */
16936 if (cp_parser_allow_gnu_extensions_p (parser))
16938 /* Look for an asm-specification. */
16939 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16940 asm_specification = cp_parser_asm_specification_opt (parser);
16942 else
16943 asm_specification = NULL_TREE;
16945 /* Look for attributes. */
16946 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16947 attributes = cp_parser_attributes_opt (parser);
16949 /* Peek at the next token. */
16950 token = cp_lexer_peek_token (parser->lexer);
16952 bool bogus_implicit_tmpl = false;
16954 if (function_declarator_p (declarator))
16956 /* Check to see if the token indicates the start of a
16957 function-definition. */
16958 if (cp_parser_token_starts_function_definition_p (token))
16960 if (!function_definition_allowed_p)
16962 /* If a function-definition should not appear here, issue an
16963 error message. */
16964 cp_parser_error (parser,
16965 "a function-definition is not allowed here");
16966 return error_mark_node;
16969 location_t func_brace_location
16970 = cp_lexer_peek_token (parser->lexer)->location;
16972 /* Neither attributes nor an asm-specification are allowed
16973 on a function-definition. */
16974 if (asm_specification)
16975 error_at (asm_spec_start_token->location,
16976 "an asm-specification is not allowed "
16977 "on a function-definition");
16978 if (attributes)
16979 error_at (attributes_start_token->location,
16980 "attributes are not allowed "
16981 "on a function-definition");
16982 /* This is a function-definition. */
16983 *function_definition_p = true;
16985 /* Parse the function definition. */
16986 if (member_p)
16987 decl = cp_parser_save_member_function_body (parser,
16988 decl_specifiers,
16989 declarator,
16990 prefix_attributes);
16991 else
16992 decl =
16993 (cp_parser_function_definition_from_specifiers_and_declarator
16994 (parser, decl_specifiers, prefix_attributes, declarator));
16996 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16998 /* This is where the prologue starts... */
16999 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17000 = func_brace_location;
17003 return decl;
17006 else if (parser->fully_implicit_function_template_p)
17008 /* A non-template declaration involving a function parameter list
17009 containing an implicit template parameter will be made into a
17010 template. If the resulting declaration is not going to be an
17011 actual function then finish the template scope here to prevent it.
17012 An error message will be issued once we have a decl to talk about.
17014 FIXME probably we should do type deduction rather than create an
17015 implicit template, but the standard currently doesn't allow it. */
17016 bogus_implicit_tmpl = true;
17017 finish_fully_implicit_template (parser, NULL_TREE);
17020 /* [dcl.dcl]
17022 Only in function declarations for constructors, destructors, and
17023 type conversions can the decl-specifier-seq be omitted.
17025 We explicitly postpone this check past the point where we handle
17026 function-definitions because we tolerate function-definitions
17027 that are missing their return types in some modes. */
17028 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17030 cp_parser_error (parser,
17031 "expected constructor, destructor, or type conversion");
17032 return error_mark_node;
17035 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17036 if (token->type == CPP_EQ
17037 || token->type == CPP_OPEN_PAREN
17038 || token->type == CPP_OPEN_BRACE)
17040 is_initialized = SD_INITIALIZED;
17041 initialization_kind = token->type;
17042 if (maybe_range_for_decl)
17043 *maybe_range_for_decl = error_mark_node;
17045 if (token->type == CPP_EQ
17046 && function_declarator_p (declarator))
17048 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17049 if (t2->keyword == RID_DEFAULT)
17050 is_initialized = SD_DEFAULTED;
17051 else if (t2->keyword == RID_DELETE)
17052 is_initialized = SD_DELETED;
17055 else
17057 /* If the init-declarator isn't initialized and isn't followed by a
17058 `,' or `;', it's not a valid init-declarator. */
17059 if (token->type != CPP_COMMA
17060 && token->type != CPP_SEMICOLON)
17062 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17063 range_for_decl_p = true;
17064 else
17066 cp_parser_error (parser, "expected initializer");
17067 return error_mark_node;
17070 is_initialized = SD_UNINITIALIZED;
17071 initialization_kind = CPP_EOF;
17074 /* Because start_decl has side-effects, we should only call it if we
17075 know we're going ahead. By this point, we know that we cannot
17076 possibly be looking at any other construct. */
17077 cp_parser_commit_to_tentative_parse (parser);
17079 /* Enter the newly declared entry in the symbol table. If we're
17080 processing a declaration in a class-specifier, we wait until
17081 after processing the initializer. */
17082 if (!member_p)
17084 if (parser->in_unbraced_linkage_specification_p)
17085 decl_specifiers->storage_class = sc_extern;
17086 decl = start_decl (declarator, decl_specifiers,
17087 range_for_decl_p? SD_INITIALIZED : is_initialized,
17088 attributes, prefix_attributes, &pushed_scope);
17089 cp_finalize_omp_declare_simd (parser, decl);
17090 /* Adjust location of decl if declarator->id_loc is more appropriate:
17091 set, and decl wasn't merged with another decl, in which case its
17092 location would be different from input_location, and more accurate. */
17093 if (DECL_P (decl)
17094 && declarator->id_loc != UNKNOWN_LOCATION
17095 && DECL_SOURCE_LOCATION (decl) == input_location)
17096 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17098 else if (scope)
17099 /* Enter the SCOPE. That way unqualified names appearing in the
17100 initializer will be looked up in SCOPE. */
17101 pushed_scope = push_scope (scope);
17103 /* Perform deferred access control checks, now that we know in which
17104 SCOPE the declared entity resides. */
17105 if (!member_p && decl)
17107 tree saved_current_function_decl = NULL_TREE;
17109 /* If the entity being declared is a function, pretend that we
17110 are in its scope. If it is a `friend', it may have access to
17111 things that would not otherwise be accessible. */
17112 if (TREE_CODE (decl) == FUNCTION_DECL)
17114 saved_current_function_decl = current_function_decl;
17115 current_function_decl = decl;
17118 /* Perform access checks for template parameters. */
17119 cp_parser_perform_template_parameter_access_checks (checks);
17121 /* Perform the access control checks for the declarator and the
17122 decl-specifiers. */
17123 perform_deferred_access_checks (tf_warning_or_error);
17125 /* Restore the saved value. */
17126 if (TREE_CODE (decl) == FUNCTION_DECL)
17127 current_function_decl = saved_current_function_decl;
17130 /* Parse the initializer. */
17131 initializer = NULL_TREE;
17132 is_direct_init = false;
17133 is_non_constant_init = true;
17134 if (is_initialized)
17136 if (function_declarator_p (declarator))
17138 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
17139 if (initialization_kind == CPP_EQ)
17140 initializer = cp_parser_pure_specifier (parser);
17141 else
17143 /* If the declaration was erroneous, we don't really
17144 know what the user intended, so just silently
17145 consume the initializer. */
17146 if (decl != error_mark_node)
17147 error_at (initializer_start_token->location,
17148 "initializer provided for function");
17149 cp_parser_skip_to_closing_parenthesis (parser,
17150 /*recovering=*/true,
17151 /*or_comma=*/false,
17152 /*consume_paren=*/true);
17155 else
17157 /* We want to record the extra mangling scope for in-class
17158 initializers of class members and initializers of static data
17159 member templates. The former involves deferring
17160 parsing of the initializer until end of class as with default
17161 arguments. So right here we only handle the latter. */
17162 if (!member_p && processing_template_decl)
17163 start_lambda_scope (decl);
17164 initializer = cp_parser_initializer (parser,
17165 &is_direct_init,
17166 &is_non_constant_init);
17167 if (!member_p && processing_template_decl)
17168 finish_lambda_scope ();
17169 if (initializer == error_mark_node)
17170 cp_parser_skip_to_end_of_statement (parser);
17174 /* The old parser allows attributes to appear after a parenthesized
17175 initializer. Mark Mitchell proposed removing this functionality
17176 on the GCC mailing lists on 2002-08-13. This parser accepts the
17177 attributes -- but ignores them. */
17178 if (cp_parser_allow_gnu_extensions_p (parser)
17179 && initialization_kind == CPP_OPEN_PAREN)
17180 if (cp_parser_attributes_opt (parser))
17181 warning (OPT_Wattributes,
17182 "attributes after parenthesized initializer ignored");
17184 /* And now complain about a non-function implicit template. */
17185 if (bogus_implicit_tmpl)
17186 error_at (DECL_SOURCE_LOCATION (decl),
17187 "non-function %qD declared as implicit template", decl);
17189 /* For an in-class declaration, use `grokfield' to create the
17190 declaration. */
17191 if (member_p)
17193 if (pushed_scope)
17195 pop_scope (pushed_scope);
17196 pushed_scope = NULL_TREE;
17198 decl = grokfield (declarator, decl_specifiers,
17199 initializer, !is_non_constant_init,
17200 /*asmspec=*/NULL_TREE,
17201 chainon (attributes, prefix_attributes));
17202 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17203 cp_parser_save_default_args (parser, decl);
17204 cp_finalize_omp_declare_simd (parser, decl);
17207 /* Finish processing the declaration. But, skip member
17208 declarations. */
17209 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17211 cp_finish_decl (decl,
17212 initializer, !is_non_constant_init,
17213 asm_specification,
17214 /* If the initializer is in parentheses, then this is
17215 a direct-initialization, which means that an
17216 `explicit' constructor is OK. Otherwise, an
17217 `explicit' constructor cannot be used. */
17218 ((is_direct_init || !is_initialized)
17219 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17221 else if ((cxx_dialect != cxx98) && friend_p
17222 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17223 /* Core issue #226 (C++0x only): A default template-argument
17224 shall not be specified in a friend class template
17225 declaration. */
17226 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17227 /*is_partial=*/false, /*is_friend_decl=*/1);
17229 if (!friend_p && pushed_scope)
17230 pop_scope (pushed_scope);
17232 if (function_declarator_p (declarator)
17233 && parser->fully_implicit_function_template_p)
17235 if (member_p)
17236 decl = finish_fully_implicit_template (parser, decl);
17237 else
17238 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17241 return decl;
17244 /* Parse a declarator.
17246 declarator:
17247 direct-declarator
17248 ptr-operator declarator
17250 abstract-declarator:
17251 ptr-operator abstract-declarator [opt]
17252 direct-abstract-declarator
17254 GNU Extensions:
17256 declarator:
17257 attributes [opt] direct-declarator
17258 attributes [opt] ptr-operator declarator
17260 abstract-declarator:
17261 attributes [opt] ptr-operator abstract-declarator [opt]
17262 attributes [opt] direct-abstract-declarator
17264 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17265 detect constructor, destructor or conversion operators. It is set
17266 to -1 if the declarator is a name, and +1 if it is a
17267 function. Otherwise it is set to zero. Usually you just want to
17268 test for >0, but internally the negative value is used.
17270 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17271 a decl-specifier-seq unless it declares a constructor, destructor,
17272 or conversion. It might seem that we could check this condition in
17273 semantic analysis, rather than parsing, but that makes it difficult
17274 to handle something like `f()'. We want to notice that there are
17275 no decl-specifiers, and therefore realize that this is an
17276 expression, not a declaration.)
17278 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17279 the declarator is a direct-declarator of the form "(...)".
17281 MEMBER_P is true iff this declarator is a member-declarator.
17283 FRIEND_P is true iff this declarator is a friend. */
17285 static cp_declarator *
17286 cp_parser_declarator (cp_parser* parser,
17287 cp_parser_declarator_kind dcl_kind,
17288 int* ctor_dtor_or_conv_p,
17289 bool* parenthesized_p,
17290 bool member_p, bool friend_p)
17292 cp_declarator *declarator;
17293 enum tree_code code;
17294 cp_cv_quals cv_quals;
17295 tree class_type;
17296 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17298 /* Assume this is not a constructor, destructor, or type-conversion
17299 operator. */
17300 if (ctor_dtor_or_conv_p)
17301 *ctor_dtor_or_conv_p = 0;
17303 if (cp_parser_allow_gnu_extensions_p (parser))
17304 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17306 /* Check for the ptr-operator production. */
17307 cp_parser_parse_tentatively (parser);
17308 /* Parse the ptr-operator. */
17309 code = cp_parser_ptr_operator (parser,
17310 &class_type,
17311 &cv_quals,
17312 &std_attributes);
17314 /* If that worked, then we have a ptr-operator. */
17315 if (cp_parser_parse_definitely (parser))
17317 /* If a ptr-operator was found, then this declarator was not
17318 parenthesized. */
17319 if (parenthesized_p)
17320 *parenthesized_p = true;
17321 /* The dependent declarator is optional if we are parsing an
17322 abstract-declarator. */
17323 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17324 cp_parser_parse_tentatively (parser);
17326 /* Parse the dependent declarator. */
17327 declarator = cp_parser_declarator (parser, dcl_kind,
17328 /*ctor_dtor_or_conv_p=*/NULL,
17329 /*parenthesized_p=*/NULL,
17330 /*member_p=*/false,
17331 friend_p);
17333 /* If we are parsing an abstract-declarator, we must handle the
17334 case where the dependent declarator is absent. */
17335 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17336 && !cp_parser_parse_definitely (parser))
17337 declarator = NULL;
17339 declarator = cp_parser_make_indirect_declarator
17340 (code, class_type, cv_quals, declarator, std_attributes);
17342 /* Everything else is a direct-declarator. */
17343 else
17345 if (parenthesized_p)
17346 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17347 CPP_OPEN_PAREN);
17348 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17349 ctor_dtor_or_conv_p,
17350 member_p, friend_p);
17353 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17354 declarator->attributes = gnu_attributes;
17355 return declarator;
17358 /* Parse a direct-declarator or direct-abstract-declarator.
17360 direct-declarator:
17361 declarator-id
17362 direct-declarator ( parameter-declaration-clause )
17363 cv-qualifier-seq [opt]
17364 ref-qualifier [opt]
17365 exception-specification [opt]
17366 direct-declarator [ constant-expression [opt] ]
17367 ( declarator )
17369 direct-abstract-declarator:
17370 direct-abstract-declarator [opt]
17371 ( parameter-declaration-clause )
17372 cv-qualifier-seq [opt]
17373 ref-qualifier [opt]
17374 exception-specification [opt]
17375 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17376 ( abstract-declarator )
17378 Returns a representation of the declarator. DCL_KIND is
17379 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17380 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17381 we are parsing a direct-declarator. It is
17382 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17383 of ambiguity we prefer an abstract declarator, as per
17384 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17385 as for cp_parser_declarator. */
17387 static cp_declarator *
17388 cp_parser_direct_declarator (cp_parser* parser,
17389 cp_parser_declarator_kind dcl_kind,
17390 int* ctor_dtor_or_conv_p,
17391 bool member_p, bool friend_p)
17393 cp_token *token;
17394 cp_declarator *declarator = NULL;
17395 tree scope = NULL_TREE;
17396 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17397 bool saved_in_declarator_p = parser->in_declarator_p;
17398 bool first = true;
17399 tree pushed_scope = NULL_TREE;
17401 while (true)
17403 /* Peek at the next token. */
17404 token = cp_lexer_peek_token (parser->lexer);
17405 if (token->type == CPP_OPEN_PAREN)
17407 /* This is either a parameter-declaration-clause, or a
17408 parenthesized declarator. When we know we are parsing a
17409 named declarator, it must be a parenthesized declarator
17410 if FIRST is true. For instance, `(int)' is a
17411 parameter-declaration-clause, with an omitted
17412 direct-abstract-declarator. But `((*))', is a
17413 parenthesized abstract declarator. Finally, when T is a
17414 template parameter `(T)' is a
17415 parameter-declaration-clause, and not a parenthesized
17416 named declarator.
17418 We first try and parse a parameter-declaration-clause,
17419 and then try a nested declarator (if FIRST is true).
17421 It is not an error for it not to be a
17422 parameter-declaration-clause, even when FIRST is
17423 false. Consider,
17425 int i (int);
17426 int i (3);
17428 The first is the declaration of a function while the
17429 second is the definition of a variable, including its
17430 initializer.
17432 Having seen only the parenthesis, we cannot know which of
17433 these two alternatives should be selected. Even more
17434 complex are examples like:
17436 int i (int (a));
17437 int i (int (3));
17439 The former is a function-declaration; the latter is a
17440 variable initialization.
17442 Thus again, we try a parameter-declaration-clause, and if
17443 that fails, we back out and return. */
17445 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17447 tree params;
17448 bool is_declarator = false;
17450 /* In a member-declarator, the only valid interpretation
17451 of a parenthesis is the start of a
17452 parameter-declaration-clause. (It is invalid to
17453 initialize a static data member with a parenthesized
17454 initializer; only the "=" form of initialization is
17455 permitted.) */
17456 if (!member_p)
17457 cp_parser_parse_tentatively (parser);
17459 /* Consume the `('. */
17460 cp_lexer_consume_token (parser->lexer);
17461 if (first)
17463 /* If this is going to be an abstract declarator, we're
17464 in a declarator and we can't have default args. */
17465 parser->default_arg_ok_p = false;
17466 parser->in_declarator_p = true;
17469 begin_scope (sk_function_parms, NULL_TREE);
17471 /* Parse the parameter-declaration-clause. */
17472 params = cp_parser_parameter_declaration_clause (parser);
17474 /* Consume the `)'. */
17475 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17477 /* If all went well, parse the cv-qualifier-seq,
17478 ref-qualifier and the exception-specification. */
17479 if (member_p || cp_parser_parse_definitely (parser))
17481 cp_cv_quals cv_quals;
17482 cp_virt_specifiers virt_specifiers;
17483 cp_ref_qualifier ref_qual;
17484 tree exception_specification;
17485 tree late_return;
17486 tree attrs;
17487 bool memfn = (member_p || (pushed_scope
17488 && CLASS_TYPE_P (pushed_scope)));
17490 is_declarator = true;
17492 if (ctor_dtor_or_conv_p)
17493 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17494 first = false;
17496 /* Parse the cv-qualifier-seq. */
17497 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17498 /* Parse the ref-qualifier. */
17499 ref_qual = cp_parser_ref_qualifier_opt (parser);
17500 /* And the exception-specification. */
17501 exception_specification
17502 = cp_parser_exception_specification_opt (parser);
17504 attrs = cp_parser_std_attribute_spec_seq (parser);
17506 /* In here, we handle cases where attribute is used after
17507 the function declaration. For example:
17508 void func (int x) __attribute__((vector(..))); */
17509 if (flag_cilkplus
17510 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17512 cp_parser_parse_tentatively (parser);
17513 tree attr = cp_parser_gnu_attributes_opt (parser);
17514 if (cp_lexer_next_token_is_not (parser->lexer,
17515 CPP_SEMICOLON)
17516 && cp_lexer_next_token_is_not (parser->lexer,
17517 CPP_OPEN_BRACE))
17518 cp_parser_abort_tentative_parse (parser);
17519 else if (!cp_parser_parse_definitely (parser))
17521 else
17522 attrs = chainon (attr, attrs);
17524 late_return = (cp_parser_late_return_type_opt
17525 (parser, declarator,
17526 memfn ? cv_quals : -1));
17529 /* Parse the virt-specifier-seq. */
17530 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17532 /* Create the function-declarator. */
17533 declarator = make_call_declarator (declarator,
17534 params,
17535 cv_quals,
17536 virt_specifiers,
17537 ref_qual,
17538 exception_specification,
17539 late_return);
17540 declarator->std_attributes = attrs;
17541 /* Any subsequent parameter lists are to do with
17542 return type, so are not those of the declared
17543 function. */
17544 parser->default_arg_ok_p = false;
17547 /* Remove the function parms from scope. */
17548 pop_bindings_and_leave_scope ();
17550 if (is_declarator)
17551 /* Repeat the main loop. */
17552 continue;
17555 /* If this is the first, we can try a parenthesized
17556 declarator. */
17557 if (first)
17559 bool saved_in_type_id_in_expr_p;
17561 parser->default_arg_ok_p = saved_default_arg_ok_p;
17562 parser->in_declarator_p = saved_in_declarator_p;
17564 /* Consume the `('. */
17565 cp_lexer_consume_token (parser->lexer);
17566 /* Parse the nested declarator. */
17567 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17568 parser->in_type_id_in_expr_p = true;
17569 declarator
17570 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17571 /*parenthesized_p=*/NULL,
17572 member_p, friend_p);
17573 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17574 first = false;
17575 /* Expect a `)'. */
17576 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17577 declarator = cp_error_declarator;
17578 if (declarator == cp_error_declarator)
17579 break;
17581 goto handle_declarator;
17583 /* Otherwise, we must be done. */
17584 else
17585 break;
17587 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17588 && token->type == CPP_OPEN_SQUARE
17589 && !cp_next_tokens_can_be_attribute_p (parser))
17591 /* Parse an array-declarator. */
17592 tree bounds, attrs;
17594 if (ctor_dtor_or_conv_p)
17595 *ctor_dtor_or_conv_p = 0;
17597 first = false;
17598 parser->default_arg_ok_p = false;
17599 parser->in_declarator_p = true;
17600 /* Consume the `['. */
17601 cp_lexer_consume_token (parser->lexer);
17602 /* Peek at the next token. */
17603 token = cp_lexer_peek_token (parser->lexer);
17604 /* If the next token is `]', then there is no
17605 constant-expression. */
17606 if (token->type != CPP_CLOSE_SQUARE)
17608 bool non_constant_p;
17609 bounds
17610 = cp_parser_constant_expression (parser,
17611 /*allow_non_constant=*/true,
17612 &non_constant_p);
17613 if (!non_constant_p)
17614 /* OK */;
17615 else if (error_operand_p (bounds))
17616 /* Already gave an error. */;
17617 else if (!parser->in_function_body
17618 || current_binding_level->kind == sk_function_parms)
17620 /* Normally, the array bound must be an integral constant
17621 expression. However, as an extension, we allow VLAs
17622 in function scopes as long as they aren't part of a
17623 parameter declaration. */
17624 cp_parser_error (parser,
17625 "array bound is not an integer constant");
17626 bounds = error_mark_node;
17628 else if (processing_template_decl
17629 && !type_dependent_expression_p (bounds))
17631 /* Remember this wasn't a constant-expression. */
17632 bounds = build_nop (TREE_TYPE (bounds), bounds);
17633 TREE_SIDE_EFFECTS (bounds) = 1;
17636 else
17637 bounds = NULL_TREE;
17638 /* Look for the closing `]'. */
17639 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17641 declarator = cp_error_declarator;
17642 break;
17645 attrs = cp_parser_std_attribute_spec_seq (parser);
17646 declarator = make_array_declarator (declarator, bounds);
17647 declarator->std_attributes = attrs;
17649 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17652 tree qualifying_scope;
17653 tree unqualified_name;
17654 tree attrs;
17655 special_function_kind sfk;
17656 bool abstract_ok;
17657 bool pack_expansion_p = false;
17658 cp_token *declarator_id_start_token;
17660 /* Parse a declarator-id */
17661 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17662 if (abstract_ok)
17664 cp_parser_parse_tentatively (parser);
17666 /* If we see an ellipsis, we should be looking at a
17667 parameter pack. */
17668 if (token->type == CPP_ELLIPSIS)
17670 /* Consume the `...' */
17671 cp_lexer_consume_token (parser->lexer);
17673 pack_expansion_p = true;
17677 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17678 unqualified_name
17679 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17680 qualifying_scope = parser->scope;
17681 if (abstract_ok)
17683 bool okay = false;
17685 if (!unqualified_name && pack_expansion_p)
17687 /* Check whether an error occurred. */
17688 okay = !cp_parser_error_occurred (parser);
17690 /* We already consumed the ellipsis to mark a
17691 parameter pack, but we have no way to report it,
17692 so abort the tentative parse. We will be exiting
17693 immediately anyway. */
17694 cp_parser_abort_tentative_parse (parser);
17696 else
17697 okay = cp_parser_parse_definitely (parser);
17699 if (!okay)
17700 unqualified_name = error_mark_node;
17701 else if (unqualified_name
17702 && (qualifying_scope
17703 || (!identifier_p (unqualified_name))))
17705 cp_parser_error (parser, "expected unqualified-id");
17706 unqualified_name = error_mark_node;
17710 if (!unqualified_name)
17711 return NULL;
17712 if (unqualified_name == error_mark_node)
17714 declarator = cp_error_declarator;
17715 pack_expansion_p = false;
17716 declarator->parameter_pack_p = false;
17717 break;
17720 attrs = cp_parser_std_attribute_spec_seq (parser);
17722 if (qualifying_scope && at_namespace_scope_p ()
17723 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17725 /* In the declaration of a member of a template class
17726 outside of the class itself, the SCOPE will sometimes
17727 be a TYPENAME_TYPE. For example, given:
17729 template <typename T>
17730 int S<T>::R::i = 3;
17732 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17733 this context, we must resolve S<T>::R to an ordinary
17734 type, rather than a typename type.
17736 The reason we normally avoid resolving TYPENAME_TYPEs
17737 is that a specialization of `S' might render
17738 `S<T>::R' not a type. However, if `S' is
17739 specialized, then this `i' will not be used, so there
17740 is no harm in resolving the types here. */
17741 tree type;
17743 /* Resolve the TYPENAME_TYPE. */
17744 type = resolve_typename_type (qualifying_scope,
17745 /*only_current_p=*/false);
17746 /* If that failed, the declarator is invalid. */
17747 if (TREE_CODE (type) == TYPENAME_TYPE)
17749 if (typedef_variant_p (type))
17750 error_at (declarator_id_start_token->location,
17751 "cannot define member of dependent typedef "
17752 "%qT", type);
17753 else
17754 error_at (declarator_id_start_token->location,
17755 "%<%T::%E%> is not a type",
17756 TYPE_CONTEXT (qualifying_scope),
17757 TYPE_IDENTIFIER (qualifying_scope));
17759 qualifying_scope = type;
17762 sfk = sfk_none;
17764 if (unqualified_name)
17766 tree class_type;
17768 if (qualifying_scope
17769 && CLASS_TYPE_P (qualifying_scope))
17770 class_type = qualifying_scope;
17771 else
17772 class_type = current_class_type;
17774 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17776 tree name_type = TREE_TYPE (unqualified_name);
17777 if (class_type && same_type_p (name_type, class_type))
17779 if (qualifying_scope
17780 && CLASSTYPE_USE_TEMPLATE (name_type))
17782 error_at (declarator_id_start_token->location,
17783 "invalid use of constructor as a template");
17784 inform (declarator_id_start_token->location,
17785 "use %<%T::%D%> instead of %<%T::%D%> to "
17786 "name the constructor in a qualified name",
17787 class_type,
17788 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17789 class_type, name_type);
17790 declarator = cp_error_declarator;
17791 break;
17793 else
17794 unqualified_name = constructor_name (class_type);
17796 else
17798 /* We do not attempt to print the declarator
17799 here because we do not have enough
17800 information about its original syntactic
17801 form. */
17802 cp_parser_error (parser, "invalid declarator");
17803 declarator = cp_error_declarator;
17804 break;
17808 if (class_type)
17810 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17811 sfk = sfk_destructor;
17812 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17813 sfk = sfk_conversion;
17814 else if (/* There's no way to declare a constructor
17815 for an anonymous type, even if the type
17816 got a name for linkage purposes. */
17817 !TYPE_WAS_ANONYMOUS (class_type)
17818 /* Handle correctly (c++/19200):
17820 struct S {
17821 struct T{};
17822 friend void S(T);
17825 and also:
17827 namespace N {
17828 void S();
17831 struct S {
17832 friend void N::S();
17833 }; */
17834 && !(friend_p
17835 && class_type != qualifying_scope)
17836 && constructor_name_p (unqualified_name,
17837 class_type))
17839 unqualified_name = constructor_name (class_type);
17840 sfk = sfk_constructor;
17842 else if (is_overloaded_fn (unqualified_name)
17843 && DECL_CONSTRUCTOR_P (get_first_fn
17844 (unqualified_name)))
17845 sfk = sfk_constructor;
17847 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17848 *ctor_dtor_or_conv_p = -1;
17851 declarator = make_id_declarator (qualifying_scope,
17852 unqualified_name,
17853 sfk);
17854 declarator->std_attributes = attrs;
17855 declarator->id_loc = token->location;
17856 declarator->parameter_pack_p = pack_expansion_p;
17858 if (pack_expansion_p)
17859 maybe_warn_variadic_templates ();
17862 handle_declarator:;
17863 scope = get_scope_of_declarator (declarator);
17864 if (scope)
17866 /* Any names that appear after the declarator-id for a
17867 member are looked up in the containing scope. */
17868 if (at_function_scope_p ())
17870 /* But declarations with qualified-ids can't appear in a
17871 function. */
17872 cp_parser_error (parser, "qualified-id in declaration");
17873 declarator = cp_error_declarator;
17874 break;
17876 pushed_scope = push_scope (scope);
17878 parser->in_declarator_p = true;
17879 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17880 || (declarator && declarator->kind == cdk_id))
17881 /* Default args are only allowed on function
17882 declarations. */
17883 parser->default_arg_ok_p = saved_default_arg_ok_p;
17884 else
17885 parser->default_arg_ok_p = false;
17887 first = false;
17889 /* We're done. */
17890 else
17891 break;
17894 /* For an abstract declarator, we might wind up with nothing at this
17895 point. That's an error; the declarator is not optional. */
17896 if (!declarator)
17897 cp_parser_error (parser, "expected declarator");
17899 /* If we entered a scope, we must exit it now. */
17900 if (pushed_scope)
17901 pop_scope (pushed_scope);
17903 parser->default_arg_ok_p = saved_default_arg_ok_p;
17904 parser->in_declarator_p = saved_in_declarator_p;
17906 return declarator;
17909 /* Parse a ptr-operator.
17911 ptr-operator:
17912 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17913 * cv-qualifier-seq [opt]
17915 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17916 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17918 GNU Extension:
17920 ptr-operator:
17921 & cv-qualifier-seq [opt]
17923 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17924 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17925 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17926 filled in with the TYPE containing the member. *CV_QUALS is
17927 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17928 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17929 Note that the tree codes returned by this function have nothing
17930 to do with the types of trees that will be eventually be created
17931 to represent the pointer or reference type being parsed. They are
17932 just constants with suggestive names. */
17933 static enum tree_code
17934 cp_parser_ptr_operator (cp_parser* parser,
17935 tree* type,
17936 cp_cv_quals *cv_quals,
17937 tree *attributes)
17939 enum tree_code code = ERROR_MARK;
17940 cp_token *token;
17941 tree attrs = NULL_TREE;
17943 /* Assume that it's not a pointer-to-member. */
17944 *type = NULL_TREE;
17945 /* And that there are no cv-qualifiers. */
17946 *cv_quals = TYPE_UNQUALIFIED;
17948 /* Peek at the next token. */
17949 token = cp_lexer_peek_token (parser->lexer);
17951 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17952 if (token->type == CPP_MULT)
17953 code = INDIRECT_REF;
17954 else if (token->type == CPP_AND)
17955 code = ADDR_EXPR;
17956 else if ((cxx_dialect != cxx98) &&
17957 token->type == CPP_AND_AND) /* C++0x only */
17958 code = NON_LVALUE_EXPR;
17960 if (code != ERROR_MARK)
17962 /* Consume the `*', `&' or `&&'. */
17963 cp_lexer_consume_token (parser->lexer);
17965 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17966 `&', if we are allowing GNU extensions. (The only qualifier
17967 that can legally appear after `&' is `restrict', but that is
17968 enforced during semantic analysis. */
17969 if (code == INDIRECT_REF
17970 || cp_parser_allow_gnu_extensions_p (parser))
17971 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17973 attrs = cp_parser_std_attribute_spec_seq (parser);
17974 if (attributes != NULL)
17975 *attributes = attrs;
17977 else
17979 /* Try the pointer-to-member case. */
17980 cp_parser_parse_tentatively (parser);
17981 /* Look for the optional `::' operator. */
17982 cp_parser_global_scope_opt (parser,
17983 /*current_scope_valid_p=*/false);
17984 /* Look for the nested-name specifier. */
17985 token = cp_lexer_peek_token (parser->lexer);
17986 cp_parser_nested_name_specifier (parser,
17987 /*typename_keyword_p=*/false,
17988 /*check_dependency_p=*/true,
17989 /*type_p=*/false,
17990 /*is_declaration=*/false);
17991 /* If we found it, and the next token is a `*', then we are
17992 indeed looking at a pointer-to-member operator. */
17993 if (!cp_parser_error_occurred (parser)
17994 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17996 /* Indicate that the `*' operator was used. */
17997 code = INDIRECT_REF;
17999 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18000 error_at (token->location, "%qD is a namespace", parser->scope);
18001 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18002 error_at (token->location, "cannot form pointer to member of "
18003 "non-class %q#T", parser->scope);
18004 else
18006 /* The type of which the member is a member is given by the
18007 current SCOPE. */
18008 *type = parser->scope;
18009 /* The next name will not be qualified. */
18010 parser->scope = NULL_TREE;
18011 parser->qualifying_scope = NULL_TREE;
18012 parser->object_scope = NULL_TREE;
18013 /* Look for optional c++11 attributes. */
18014 attrs = cp_parser_std_attribute_spec_seq (parser);
18015 if (attributes != NULL)
18016 *attributes = attrs;
18017 /* Look for the optional cv-qualifier-seq. */
18018 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18021 /* If that didn't work we don't have a ptr-operator. */
18022 if (!cp_parser_parse_definitely (parser))
18023 cp_parser_error (parser, "expected ptr-operator");
18026 return code;
18029 /* Parse an (optional) cv-qualifier-seq.
18031 cv-qualifier-seq:
18032 cv-qualifier cv-qualifier-seq [opt]
18034 cv-qualifier:
18035 const
18036 volatile
18038 GNU Extension:
18040 cv-qualifier:
18041 __restrict__
18043 Returns a bitmask representing the cv-qualifiers. */
18045 static cp_cv_quals
18046 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18048 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18050 while (true)
18052 cp_token *token;
18053 cp_cv_quals cv_qualifier;
18055 /* Peek at the next token. */
18056 token = cp_lexer_peek_token (parser->lexer);
18057 /* See if it's a cv-qualifier. */
18058 switch (token->keyword)
18060 case RID_CONST:
18061 cv_qualifier = TYPE_QUAL_CONST;
18062 break;
18064 case RID_VOLATILE:
18065 cv_qualifier = TYPE_QUAL_VOLATILE;
18066 break;
18068 case RID_RESTRICT:
18069 cv_qualifier = TYPE_QUAL_RESTRICT;
18070 break;
18072 default:
18073 cv_qualifier = TYPE_UNQUALIFIED;
18074 break;
18077 if (!cv_qualifier)
18078 break;
18080 if (cv_quals & cv_qualifier)
18082 error_at (token->location, "duplicate cv-qualifier");
18083 cp_lexer_purge_token (parser->lexer);
18085 else
18087 cp_lexer_consume_token (parser->lexer);
18088 cv_quals |= cv_qualifier;
18092 return cv_quals;
18095 /* Parse an (optional) ref-qualifier
18097 ref-qualifier:
18101 Returns cp_ref_qualifier representing ref-qualifier. */
18103 static cp_ref_qualifier
18104 cp_parser_ref_qualifier_opt (cp_parser* parser)
18106 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18108 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18109 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18110 return ref_qual;
18112 while (true)
18114 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18115 cp_token *token = cp_lexer_peek_token (parser->lexer);
18117 switch (token->type)
18119 case CPP_AND:
18120 curr_ref_qual = REF_QUAL_LVALUE;
18121 break;
18123 case CPP_AND_AND:
18124 curr_ref_qual = REF_QUAL_RVALUE;
18125 break;
18127 default:
18128 curr_ref_qual = REF_QUAL_NONE;
18129 break;
18132 if (!curr_ref_qual)
18133 break;
18134 else if (ref_qual)
18136 error_at (token->location, "multiple ref-qualifiers");
18137 cp_lexer_purge_token (parser->lexer);
18139 else
18141 ref_qual = curr_ref_qual;
18142 cp_lexer_consume_token (parser->lexer);
18146 return ref_qual;
18149 /* Parse an (optional) virt-specifier-seq.
18151 virt-specifier-seq:
18152 virt-specifier virt-specifier-seq [opt]
18154 virt-specifier:
18155 override
18156 final
18158 Returns a bitmask representing the virt-specifiers. */
18160 static cp_virt_specifiers
18161 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18163 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18165 while (true)
18167 cp_token *token;
18168 cp_virt_specifiers virt_specifier;
18170 /* Peek at the next token. */
18171 token = cp_lexer_peek_token (parser->lexer);
18172 /* See if it's a virt-specifier-qualifier. */
18173 if (token->type != CPP_NAME)
18174 break;
18175 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18177 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18178 virt_specifier = VIRT_SPEC_OVERRIDE;
18180 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18182 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18183 virt_specifier = VIRT_SPEC_FINAL;
18185 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18187 virt_specifier = VIRT_SPEC_FINAL;
18189 else
18190 break;
18192 if (virt_specifiers & virt_specifier)
18194 error_at (token->location, "duplicate virt-specifier");
18195 cp_lexer_purge_token (parser->lexer);
18197 else
18199 cp_lexer_consume_token (parser->lexer);
18200 virt_specifiers |= virt_specifier;
18203 return virt_specifiers;
18206 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18207 is in scope even though it isn't real. */
18209 void
18210 inject_this_parameter (tree ctype, cp_cv_quals quals)
18212 tree this_parm;
18214 if (current_class_ptr)
18216 /* We don't clear this between NSDMIs. Is it already what we want? */
18217 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18218 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18219 && cp_type_quals (type) == quals)
18220 return;
18223 this_parm = build_this_parm (ctype, quals);
18224 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18225 current_class_ptr = NULL_TREE;
18226 current_class_ref
18227 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18228 current_class_ptr = this_parm;
18231 /* Return true iff our current scope is a non-static data member
18232 initializer. */
18234 bool
18235 parsing_nsdmi (void)
18237 /* We recognize NSDMI context by the context-less 'this' pointer set up
18238 by the function above. */
18239 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18240 return true;
18241 return false;
18244 /* Parse a late-specified return type, if any. This is not a separate
18245 non-terminal, but part of a function declarator, which looks like
18247 -> trailing-type-specifier-seq abstract-declarator(opt)
18249 Returns the type indicated by the type-id.
18251 In addition to this this parses any queued up omp declare simd
18252 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18254 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18255 function. */
18257 static tree
18258 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18259 cp_cv_quals quals)
18261 cp_token *token;
18262 tree type = NULL_TREE;
18263 bool declare_simd_p = (parser->omp_declare_simd
18264 && declarator
18265 && declarator->kind == cdk_id);
18267 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18268 && declarator && declarator->kind == cdk_id);
18270 /* Peek at the next token. */
18271 token = cp_lexer_peek_token (parser->lexer);
18272 /* A late-specified return type is indicated by an initial '->'. */
18273 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18274 return NULL_TREE;
18276 tree save_ccp = current_class_ptr;
18277 tree save_ccr = current_class_ref;
18278 if (quals >= 0)
18280 /* DR 1207: 'this' is in scope in the trailing return type. */
18281 inject_this_parameter (current_class_type, quals);
18284 if (token->type == CPP_DEREF)
18286 /* Consume the ->. */
18287 cp_lexer_consume_token (parser->lexer);
18289 type = cp_parser_trailing_type_id (parser);
18292 if (cilk_simd_fn_vector_p)
18293 declarator->std_attributes
18294 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18295 declarator->std_attributes);
18296 if (declare_simd_p)
18297 declarator->std_attributes
18298 = cp_parser_late_parsing_omp_declare_simd (parser,
18299 declarator->std_attributes);
18301 if (quals >= 0)
18303 current_class_ptr = save_ccp;
18304 current_class_ref = save_ccr;
18307 return type;
18310 /* Parse a declarator-id.
18312 declarator-id:
18313 id-expression
18314 :: [opt] nested-name-specifier [opt] type-name
18316 In the `id-expression' case, the value returned is as for
18317 cp_parser_id_expression if the id-expression was an unqualified-id.
18318 If the id-expression was a qualified-id, then a SCOPE_REF is
18319 returned. The first operand is the scope (either a NAMESPACE_DECL
18320 or TREE_TYPE), but the second is still just a representation of an
18321 unqualified-id. */
18323 static tree
18324 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18326 tree id;
18327 /* The expression must be an id-expression. Assume that qualified
18328 names are the names of types so that:
18330 template <class T>
18331 int S<T>::R::i = 3;
18333 will work; we must treat `S<T>::R' as the name of a type.
18334 Similarly, assume that qualified names are templates, where
18335 required, so that:
18337 template <class T>
18338 int S<T>::R<T>::i = 3;
18340 will work, too. */
18341 id = cp_parser_id_expression (parser,
18342 /*template_keyword_p=*/false,
18343 /*check_dependency_p=*/false,
18344 /*template_p=*/NULL,
18345 /*declarator_p=*/true,
18346 optional_p);
18347 if (id && BASELINK_P (id))
18348 id = BASELINK_FUNCTIONS (id);
18349 return id;
18352 /* Parse a type-id.
18354 type-id:
18355 type-specifier-seq abstract-declarator [opt]
18357 Returns the TYPE specified. */
18359 static tree
18360 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18361 bool is_trailing_return)
18363 cp_decl_specifier_seq type_specifier_seq;
18364 cp_declarator *abstract_declarator;
18366 /* Parse the type-specifier-seq. */
18367 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18368 is_trailing_return,
18369 &type_specifier_seq);
18370 if (type_specifier_seq.type == error_mark_node)
18371 return error_mark_node;
18373 /* There might or might not be an abstract declarator. */
18374 cp_parser_parse_tentatively (parser);
18375 /* Look for the declarator. */
18376 abstract_declarator
18377 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18378 /*parenthesized_p=*/NULL,
18379 /*member_p=*/false,
18380 /*friend_p=*/false);
18381 /* Check to see if there really was a declarator. */
18382 if (!cp_parser_parse_definitely (parser))
18383 abstract_declarator = NULL;
18385 if (type_specifier_seq.type
18386 /* None of the valid uses of 'auto' in C++14 involve the type-id
18387 nonterminal, but it is valid in a trailing-return-type. */
18388 && !(cxx_dialect >= cxx14 && is_trailing_return)
18389 && type_uses_auto (type_specifier_seq.type))
18391 /* A type-id with type 'auto' is only ok if the abstract declarator
18392 is a function declarator with a late-specified return type. */
18393 if (abstract_declarator
18394 && abstract_declarator->kind == cdk_function
18395 && abstract_declarator->u.function.late_return_type)
18396 /* OK */;
18397 else
18399 error ("invalid use of %<auto%>");
18400 return error_mark_node;
18404 return groktypename (&type_specifier_seq, abstract_declarator,
18405 is_template_arg);
18408 static tree cp_parser_type_id (cp_parser *parser)
18410 return cp_parser_type_id_1 (parser, false, false);
18413 static tree cp_parser_template_type_arg (cp_parser *parser)
18415 tree r;
18416 const char *saved_message = parser->type_definition_forbidden_message;
18417 parser->type_definition_forbidden_message
18418 = G_("types may not be defined in template arguments");
18419 r = cp_parser_type_id_1 (parser, true, false);
18420 parser->type_definition_forbidden_message = saved_message;
18421 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18423 error ("invalid use of %<auto%> in template argument");
18424 r = error_mark_node;
18426 return r;
18429 static tree cp_parser_trailing_type_id (cp_parser *parser)
18431 return cp_parser_type_id_1 (parser, false, true);
18434 /* Parse a type-specifier-seq.
18436 type-specifier-seq:
18437 type-specifier type-specifier-seq [opt]
18439 GNU extension:
18441 type-specifier-seq:
18442 attributes type-specifier-seq [opt]
18444 If IS_DECLARATION is true, we are at the start of a "condition" or
18445 exception-declaration, so we might be followed by a declarator-id.
18447 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18448 i.e. we've just seen "->".
18450 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18452 static void
18453 cp_parser_type_specifier_seq (cp_parser* parser,
18454 bool is_declaration,
18455 bool is_trailing_return,
18456 cp_decl_specifier_seq *type_specifier_seq)
18458 bool seen_type_specifier = false;
18459 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18460 cp_token *start_token = NULL;
18462 /* Clear the TYPE_SPECIFIER_SEQ. */
18463 clear_decl_specs (type_specifier_seq);
18465 /* In the context of a trailing return type, enum E { } is an
18466 elaborated-type-specifier followed by a function-body, not an
18467 enum-specifier. */
18468 if (is_trailing_return)
18469 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18471 /* Parse the type-specifiers and attributes. */
18472 while (true)
18474 tree type_specifier;
18475 bool is_cv_qualifier;
18477 /* Check for attributes first. */
18478 if (cp_next_tokens_can_be_attribute_p (parser))
18480 type_specifier_seq->attributes =
18481 chainon (type_specifier_seq->attributes,
18482 cp_parser_attributes_opt (parser));
18483 continue;
18486 /* record the token of the beginning of the type specifier seq,
18487 for error reporting purposes*/
18488 if (!start_token)
18489 start_token = cp_lexer_peek_token (parser->lexer);
18491 /* Look for the type-specifier. */
18492 type_specifier = cp_parser_type_specifier (parser,
18493 flags,
18494 type_specifier_seq,
18495 /*is_declaration=*/false,
18496 NULL,
18497 &is_cv_qualifier);
18498 if (!type_specifier)
18500 /* If the first type-specifier could not be found, this is not a
18501 type-specifier-seq at all. */
18502 if (!seen_type_specifier)
18504 /* Set in_declarator_p to avoid skipping to the semicolon. */
18505 int in_decl = parser->in_declarator_p;
18506 parser->in_declarator_p = true;
18508 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18509 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18510 cp_parser_error (parser, "expected type-specifier");
18512 parser->in_declarator_p = in_decl;
18514 type_specifier_seq->type = error_mark_node;
18515 return;
18517 /* If subsequent type-specifiers could not be found, the
18518 type-specifier-seq is complete. */
18519 break;
18522 seen_type_specifier = true;
18523 /* The standard says that a condition can be:
18525 type-specifier-seq declarator = assignment-expression
18527 However, given:
18529 struct S {};
18530 if (int S = ...)
18532 we should treat the "S" as a declarator, not as a
18533 type-specifier. The standard doesn't say that explicitly for
18534 type-specifier-seq, but it does say that for
18535 decl-specifier-seq in an ordinary declaration. Perhaps it
18536 would be clearer just to allow a decl-specifier-seq here, and
18537 then add a semantic restriction that if any decl-specifiers
18538 that are not type-specifiers appear, the program is invalid. */
18539 if (is_declaration && !is_cv_qualifier)
18540 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18544 /* Return whether the function currently being declared has an associated
18545 template parameter list. */
18547 static bool
18548 function_being_declared_is_template_p (cp_parser* parser)
18550 if (!current_template_parms || processing_template_parmlist)
18551 return false;
18553 if (parser->implicit_template_scope)
18554 return true;
18556 if (at_class_scope_p ()
18557 && TYPE_BEING_DEFINED (current_class_type))
18558 return parser->num_template_parameter_lists != 0;
18560 return ((int) parser->num_template_parameter_lists > template_class_depth
18561 (current_class_type));
18564 /* Parse a parameter-declaration-clause.
18566 parameter-declaration-clause:
18567 parameter-declaration-list [opt] ... [opt]
18568 parameter-declaration-list , ...
18570 Returns a representation for the parameter declarations. A return
18571 value of NULL indicates a parameter-declaration-clause consisting
18572 only of an ellipsis. */
18574 static tree
18575 cp_parser_parameter_declaration_clause (cp_parser* parser)
18577 tree parameters;
18578 cp_token *token;
18579 bool ellipsis_p;
18580 bool is_error;
18582 struct cleanup {
18583 cp_parser* parser;
18584 int auto_is_implicit_function_template_parm_p;
18585 ~cleanup() {
18586 parser->auto_is_implicit_function_template_parm_p
18587 = auto_is_implicit_function_template_parm_p;
18589 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18591 (void) cleanup;
18593 if (!processing_specialization
18594 && !processing_template_parmlist
18595 && !processing_explicit_instantiation)
18596 if (!current_function_decl
18597 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18598 parser->auto_is_implicit_function_template_parm_p = true;
18600 /* Peek at the next token. */
18601 token = cp_lexer_peek_token (parser->lexer);
18602 /* Check for trivial parameter-declaration-clauses. */
18603 if (token->type == CPP_ELLIPSIS)
18605 /* Consume the `...' token. */
18606 cp_lexer_consume_token (parser->lexer);
18607 return NULL_TREE;
18609 else if (token->type == CPP_CLOSE_PAREN)
18610 /* There are no parameters. */
18612 #ifndef NO_IMPLICIT_EXTERN_C
18613 if (in_system_header_at (input_location)
18614 && current_class_type == NULL
18615 && current_lang_name == lang_name_c)
18616 return NULL_TREE;
18617 else
18618 #endif
18619 return void_list_node;
18621 /* Check for `(void)', too, which is a special case. */
18622 else if (token->keyword == RID_VOID
18623 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18624 == CPP_CLOSE_PAREN))
18626 /* Consume the `void' token. */
18627 cp_lexer_consume_token (parser->lexer);
18628 /* There are no parameters. */
18629 return void_list_node;
18632 /* Parse the parameter-declaration-list. */
18633 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18634 /* If a parse error occurred while parsing the
18635 parameter-declaration-list, then the entire
18636 parameter-declaration-clause is erroneous. */
18637 if (is_error)
18638 return NULL;
18640 /* Peek at the next token. */
18641 token = cp_lexer_peek_token (parser->lexer);
18642 /* If it's a `,', the clause should terminate with an ellipsis. */
18643 if (token->type == CPP_COMMA)
18645 /* Consume the `,'. */
18646 cp_lexer_consume_token (parser->lexer);
18647 /* Expect an ellipsis. */
18648 ellipsis_p
18649 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18651 /* It might also be `...' if the optional trailing `,' was
18652 omitted. */
18653 else if (token->type == CPP_ELLIPSIS)
18655 /* Consume the `...' token. */
18656 cp_lexer_consume_token (parser->lexer);
18657 /* And remember that we saw it. */
18658 ellipsis_p = true;
18660 else
18661 ellipsis_p = false;
18663 /* Finish the parameter list. */
18664 if (!ellipsis_p)
18665 parameters = chainon (parameters, void_list_node);
18667 return parameters;
18670 /* Parse a parameter-declaration-list.
18672 parameter-declaration-list:
18673 parameter-declaration
18674 parameter-declaration-list , parameter-declaration
18676 Returns a representation of the parameter-declaration-list, as for
18677 cp_parser_parameter_declaration_clause. However, the
18678 `void_list_node' is never appended to the list. Upon return,
18679 *IS_ERROR will be true iff an error occurred. */
18681 static tree
18682 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18684 tree parameters = NULL_TREE;
18685 tree *tail = &parameters;
18686 bool saved_in_unbraced_linkage_specification_p;
18687 int index = 0;
18689 /* Assume all will go well. */
18690 *is_error = false;
18691 /* The special considerations that apply to a function within an
18692 unbraced linkage specifications do not apply to the parameters
18693 to the function. */
18694 saved_in_unbraced_linkage_specification_p
18695 = parser->in_unbraced_linkage_specification_p;
18696 parser->in_unbraced_linkage_specification_p = false;
18698 /* Look for more parameters. */
18699 while (true)
18701 cp_parameter_declarator *parameter;
18702 tree decl = error_mark_node;
18703 bool parenthesized_p = false;
18704 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18705 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18706 (current_template_parms)) : 0);
18708 /* Parse the parameter. */
18709 parameter
18710 = cp_parser_parameter_declaration (parser,
18711 /*template_parm_p=*/false,
18712 &parenthesized_p);
18714 /* We don't know yet if the enclosing context is deprecated, so wait
18715 and warn in grokparms if appropriate. */
18716 deprecated_state = DEPRECATED_SUPPRESS;
18718 if (parameter)
18720 /* If a function parameter pack was specified and an implicit template
18721 parameter was introduced during cp_parser_parameter_declaration,
18722 change any implicit parameters introduced into packs. */
18723 if (parser->implicit_template_parms
18724 && parameter->declarator
18725 && parameter->declarator->parameter_pack_p)
18727 int latest_template_parm_idx = TREE_VEC_LENGTH
18728 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18730 if (latest_template_parm_idx != template_parm_idx)
18731 parameter->decl_specifiers.type = convert_generic_types_to_packs
18732 (parameter->decl_specifiers.type,
18733 template_parm_idx, latest_template_parm_idx);
18736 decl = grokdeclarator (parameter->declarator,
18737 &parameter->decl_specifiers,
18738 PARM,
18739 parameter->default_argument != NULL_TREE,
18740 &parameter->decl_specifiers.attributes);
18743 deprecated_state = DEPRECATED_NORMAL;
18745 /* If a parse error occurred parsing the parameter declaration,
18746 then the entire parameter-declaration-list is erroneous. */
18747 if (decl == error_mark_node)
18749 *is_error = true;
18750 parameters = error_mark_node;
18751 break;
18754 if (parameter->decl_specifiers.attributes)
18755 cplus_decl_attributes (&decl,
18756 parameter->decl_specifiers.attributes,
18758 if (DECL_NAME (decl))
18759 decl = pushdecl (decl);
18761 if (decl != error_mark_node)
18763 retrofit_lang_decl (decl);
18764 DECL_PARM_INDEX (decl) = ++index;
18765 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18768 /* Add the new parameter to the list. */
18769 *tail = build_tree_list (parameter->default_argument, decl);
18770 tail = &TREE_CHAIN (*tail);
18772 /* Peek at the next token. */
18773 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18774 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18775 /* These are for Objective-C++ */
18776 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18777 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18778 /* The parameter-declaration-list is complete. */
18779 break;
18780 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18782 cp_token *token;
18784 /* Peek at the next token. */
18785 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18786 /* If it's an ellipsis, then the list is complete. */
18787 if (token->type == CPP_ELLIPSIS)
18788 break;
18789 /* Otherwise, there must be more parameters. Consume the
18790 `,'. */
18791 cp_lexer_consume_token (parser->lexer);
18792 /* When parsing something like:
18794 int i(float f, double d)
18796 we can tell after seeing the declaration for "f" that we
18797 are not looking at an initialization of a variable "i",
18798 but rather at the declaration of a function "i".
18800 Due to the fact that the parsing of template arguments
18801 (as specified to a template-id) requires backtracking we
18802 cannot use this technique when inside a template argument
18803 list. */
18804 if (!parser->in_template_argument_list_p
18805 && !parser->in_type_id_in_expr_p
18806 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18807 /* However, a parameter-declaration of the form
18808 "float(f)" (which is a valid declaration of a
18809 parameter "f") can also be interpreted as an
18810 expression (the conversion of "f" to "float"). */
18811 && !parenthesized_p)
18812 cp_parser_commit_to_tentative_parse (parser);
18814 else
18816 cp_parser_error (parser, "expected %<,%> or %<...%>");
18817 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18818 cp_parser_skip_to_closing_parenthesis (parser,
18819 /*recovering=*/true,
18820 /*or_comma=*/false,
18821 /*consume_paren=*/false);
18822 break;
18826 parser->in_unbraced_linkage_specification_p
18827 = saved_in_unbraced_linkage_specification_p;
18829 /* Reset implicit_template_scope if we are about to leave the function
18830 parameter list that introduced it. Note that for out-of-line member
18831 definitions, there will be one or more class scopes before we get to
18832 the template parameter scope. */
18834 if (cp_binding_level *its = parser->implicit_template_scope)
18835 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18837 while (maybe_its->kind == sk_class)
18838 maybe_its = maybe_its->level_chain;
18839 if (maybe_its == its)
18841 parser->implicit_template_parms = 0;
18842 parser->implicit_template_scope = 0;
18846 return parameters;
18849 /* Parse a parameter declaration.
18851 parameter-declaration:
18852 decl-specifier-seq ... [opt] declarator
18853 decl-specifier-seq declarator = assignment-expression
18854 decl-specifier-seq ... [opt] abstract-declarator [opt]
18855 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18857 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18858 declares a template parameter. (In that case, a non-nested `>'
18859 token encountered during the parsing of the assignment-expression
18860 is not interpreted as a greater-than operator.)
18862 Returns a representation of the parameter, or NULL if an error
18863 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18864 true iff the declarator is of the form "(p)". */
18866 static cp_parameter_declarator *
18867 cp_parser_parameter_declaration (cp_parser *parser,
18868 bool template_parm_p,
18869 bool *parenthesized_p)
18871 int declares_class_or_enum;
18872 cp_decl_specifier_seq decl_specifiers;
18873 cp_declarator *declarator;
18874 tree default_argument;
18875 cp_token *token = NULL, *declarator_token_start = NULL;
18876 const char *saved_message;
18878 /* In a template parameter, `>' is not an operator.
18880 [temp.param]
18882 When parsing a default template-argument for a non-type
18883 template-parameter, the first non-nested `>' is taken as the end
18884 of the template parameter-list rather than a greater-than
18885 operator. */
18887 /* Type definitions may not appear in parameter types. */
18888 saved_message = parser->type_definition_forbidden_message;
18889 parser->type_definition_forbidden_message
18890 = G_("types may not be defined in parameter types");
18892 /* Parse the declaration-specifiers. */
18893 cp_parser_decl_specifier_seq (parser,
18894 CP_PARSER_FLAGS_NONE,
18895 &decl_specifiers,
18896 &declares_class_or_enum);
18898 /* Complain about missing 'typename' or other invalid type names. */
18899 if (!decl_specifiers.any_type_specifiers_p
18900 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18901 decl_specifiers.type = error_mark_node;
18903 /* If an error occurred, there's no reason to attempt to parse the
18904 rest of the declaration. */
18905 if (cp_parser_error_occurred (parser))
18907 parser->type_definition_forbidden_message = saved_message;
18908 return NULL;
18911 /* Peek at the next token. */
18912 token = cp_lexer_peek_token (parser->lexer);
18914 /* If the next token is a `)', `,', `=', `>', or `...', then there
18915 is no declarator. However, when variadic templates are enabled,
18916 there may be a declarator following `...'. */
18917 if (token->type == CPP_CLOSE_PAREN
18918 || token->type == CPP_COMMA
18919 || token->type == CPP_EQ
18920 || token->type == CPP_GREATER)
18922 declarator = NULL;
18923 if (parenthesized_p)
18924 *parenthesized_p = false;
18926 /* Otherwise, there should be a declarator. */
18927 else
18929 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18930 parser->default_arg_ok_p = false;
18932 /* After seeing a decl-specifier-seq, if the next token is not a
18933 "(", there is no possibility that the code is a valid
18934 expression. Therefore, if parsing tentatively, we commit at
18935 this point. */
18936 if (!parser->in_template_argument_list_p
18937 /* In an expression context, having seen:
18939 (int((char ...
18941 we cannot be sure whether we are looking at a
18942 function-type (taking a "char" as a parameter) or a cast
18943 of some object of type "char" to "int". */
18944 && !parser->in_type_id_in_expr_p
18945 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18946 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18947 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18948 cp_parser_commit_to_tentative_parse (parser);
18949 /* Parse the declarator. */
18950 declarator_token_start = token;
18951 declarator = cp_parser_declarator (parser,
18952 CP_PARSER_DECLARATOR_EITHER,
18953 /*ctor_dtor_or_conv_p=*/NULL,
18954 parenthesized_p,
18955 /*member_p=*/false,
18956 /*friend_p=*/false);
18957 parser->default_arg_ok_p = saved_default_arg_ok_p;
18958 /* After the declarator, allow more attributes. */
18959 decl_specifiers.attributes
18960 = chainon (decl_specifiers.attributes,
18961 cp_parser_attributes_opt (parser));
18964 /* If the next token is an ellipsis, and we have not seen a
18965 declarator name, and the type of the declarator contains parameter
18966 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18967 a parameter pack expansion expression. Otherwise, leave the
18968 ellipsis for a C-style variadic function. */
18969 token = cp_lexer_peek_token (parser->lexer);
18970 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18972 tree type = decl_specifiers.type;
18974 if (type && DECL_P (type))
18975 type = TREE_TYPE (type);
18977 if (type
18978 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18979 && declarator_can_be_parameter_pack (declarator)
18980 && (!declarator || !declarator->parameter_pack_p)
18981 && uses_parameter_packs (type))
18983 /* Consume the `...'. */
18984 cp_lexer_consume_token (parser->lexer);
18985 maybe_warn_variadic_templates ();
18987 /* Build a pack expansion type */
18988 if (declarator)
18989 declarator->parameter_pack_p = true;
18990 else
18991 decl_specifiers.type = make_pack_expansion (type);
18995 /* The restriction on defining new types applies only to the type
18996 of the parameter, not to the default argument. */
18997 parser->type_definition_forbidden_message = saved_message;
18999 /* If the next token is `=', then process a default argument. */
19000 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19002 token = cp_lexer_peek_token (parser->lexer);
19003 /* If we are defining a class, then the tokens that make up the
19004 default argument must be saved and processed later. */
19005 if (!template_parm_p && at_class_scope_p ()
19006 && TYPE_BEING_DEFINED (current_class_type)
19007 && !LAMBDA_TYPE_P (current_class_type))
19008 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19009 /* Outside of a class definition, we can just parse the
19010 assignment-expression. */
19011 else
19012 default_argument
19013 = cp_parser_default_argument (parser, template_parm_p);
19015 if (!parser->default_arg_ok_p)
19017 if (flag_permissive)
19018 warning (0, "deprecated use of default argument for parameter of non-function");
19019 else
19021 error_at (token->location,
19022 "default arguments are only "
19023 "permitted for function parameters");
19024 default_argument = NULL_TREE;
19027 else if ((declarator && declarator->parameter_pack_p)
19028 || (decl_specifiers.type
19029 && PACK_EXPANSION_P (decl_specifiers.type)))
19031 /* Find the name of the parameter pack. */
19032 cp_declarator *id_declarator = declarator;
19033 while (id_declarator && id_declarator->kind != cdk_id)
19034 id_declarator = id_declarator->declarator;
19036 if (id_declarator && id_declarator->kind == cdk_id)
19037 error_at (declarator_token_start->location,
19038 template_parm_p
19039 ? G_("template parameter pack %qD "
19040 "cannot have a default argument")
19041 : G_("parameter pack %qD cannot have "
19042 "a default argument"),
19043 id_declarator->u.id.unqualified_name);
19044 else
19045 error_at (declarator_token_start->location,
19046 template_parm_p
19047 ? G_("template parameter pack cannot have "
19048 "a default argument")
19049 : G_("parameter pack cannot have a "
19050 "default argument"));
19052 default_argument = NULL_TREE;
19055 else
19056 default_argument = NULL_TREE;
19058 return make_parameter_declarator (&decl_specifiers,
19059 declarator,
19060 default_argument);
19063 /* Parse a default argument and return it.
19065 TEMPLATE_PARM_P is true if this is a default argument for a
19066 non-type template parameter. */
19067 static tree
19068 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19070 tree default_argument = NULL_TREE;
19071 bool saved_greater_than_is_operator_p;
19072 bool saved_local_variables_forbidden_p;
19073 bool non_constant_p, is_direct_init;
19075 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19076 set correctly. */
19077 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19078 parser->greater_than_is_operator_p = !template_parm_p;
19079 /* Local variable names (and the `this' keyword) may not
19080 appear in a default argument. */
19081 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19082 parser->local_variables_forbidden_p = true;
19083 /* Parse the assignment-expression. */
19084 if (template_parm_p)
19085 push_deferring_access_checks (dk_no_deferred);
19086 tree saved_class_ptr = NULL_TREE;
19087 tree saved_class_ref = NULL_TREE;
19088 /* The "this" pointer is not valid in a default argument. */
19089 if (cfun)
19091 saved_class_ptr = current_class_ptr;
19092 cp_function_chain->x_current_class_ptr = NULL_TREE;
19093 saved_class_ref = current_class_ref;
19094 cp_function_chain->x_current_class_ref = NULL_TREE;
19096 default_argument
19097 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19098 /* Restore the "this" pointer. */
19099 if (cfun)
19101 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19102 cp_function_chain->x_current_class_ref = saved_class_ref;
19104 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19105 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19106 if (template_parm_p)
19107 pop_deferring_access_checks ();
19108 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19109 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19111 return default_argument;
19114 /* Parse a function-body.
19116 function-body:
19117 compound_statement */
19119 static void
19120 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19122 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19125 /* Parse a ctor-initializer-opt followed by a function-body. Return
19126 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19127 is true we are parsing a function-try-block. */
19129 static bool
19130 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19131 bool in_function_try_block)
19133 tree body, list;
19134 bool ctor_initializer_p;
19135 const bool check_body_p =
19136 DECL_CONSTRUCTOR_P (current_function_decl)
19137 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19138 tree last = NULL;
19140 /* Begin the function body. */
19141 body = begin_function_body ();
19142 /* Parse the optional ctor-initializer. */
19143 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19145 /* If we're parsing a constexpr constructor definition, we need
19146 to check that the constructor body is indeed empty. However,
19147 before we get to cp_parser_function_body lot of junk has been
19148 generated, so we can't just check that we have an empty block.
19149 Rather we take a snapshot of the outermost block, and check whether
19150 cp_parser_function_body changed its state. */
19151 if (check_body_p)
19153 list = cur_stmt_list;
19154 if (STATEMENT_LIST_TAIL (list))
19155 last = STATEMENT_LIST_TAIL (list)->stmt;
19157 /* Parse the function-body. */
19158 cp_parser_function_body (parser, in_function_try_block);
19159 if (check_body_p)
19160 check_constexpr_ctor_body (last, list, /*complain=*/true);
19161 /* Finish the function body. */
19162 finish_function_body (body);
19164 return ctor_initializer_p;
19167 /* Parse an initializer.
19169 initializer:
19170 = initializer-clause
19171 ( expression-list )
19173 Returns an expression representing the initializer. If no
19174 initializer is present, NULL_TREE is returned.
19176 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19177 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19178 set to TRUE if there is no initializer present. If there is an
19179 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19180 is set to true; otherwise it is set to false. */
19182 static tree
19183 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19184 bool* non_constant_p)
19186 cp_token *token;
19187 tree init;
19189 /* Peek at the next token. */
19190 token = cp_lexer_peek_token (parser->lexer);
19192 /* Let our caller know whether or not this initializer was
19193 parenthesized. */
19194 *is_direct_init = (token->type != CPP_EQ);
19195 /* Assume that the initializer is constant. */
19196 *non_constant_p = false;
19198 if (token->type == CPP_EQ)
19200 /* Consume the `='. */
19201 cp_lexer_consume_token (parser->lexer);
19202 /* Parse the initializer-clause. */
19203 init = cp_parser_initializer_clause (parser, non_constant_p);
19205 else if (token->type == CPP_OPEN_PAREN)
19207 vec<tree, va_gc> *vec;
19208 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19209 /*cast_p=*/false,
19210 /*allow_expansion_p=*/true,
19211 non_constant_p);
19212 if (vec == NULL)
19213 return error_mark_node;
19214 init = build_tree_list_vec (vec);
19215 release_tree_vector (vec);
19217 else if (token->type == CPP_OPEN_BRACE)
19219 cp_lexer_set_source_position (parser->lexer);
19220 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19221 init = cp_parser_braced_list (parser, non_constant_p);
19222 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19224 else
19226 /* Anything else is an error. */
19227 cp_parser_error (parser, "expected initializer");
19228 init = error_mark_node;
19231 return init;
19234 /* Parse an initializer-clause.
19236 initializer-clause:
19237 assignment-expression
19238 braced-init-list
19240 Returns an expression representing the initializer.
19242 If the `assignment-expression' production is used the value
19243 returned is simply a representation for the expression.
19245 Otherwise, calls cp_parser_braced_list. */
19247 static tree
19248 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19250 tree initializer;
19252 /* Assume the expression is constant. */
19253 *non_constant_p = false;
19255 /* If it is not a `{', then we are looking at an
19256 assignment-expression. */
19257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19259 initializer
19260 = cp_parser_constant_expression (parser,
19261 /*allow_non_constant_p=*/true,
19262 non_constant_p);
19264 else
19265 initializer = cp_parser_braced_list (parser, non_constant_p);
19267 return initializer;
19270 /* Parse a brace-enclosed initializer list.
19272 braced-init-list:
19273 { initializer-list , [opt] }
19276 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19277 the elements of the initializer-list (or NULL, if the last
19278 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19279 NULL_TREE. There is no way to detect whether or not the optional
19280 trailing `,' was provided. NON_CONSTANT_P is as for
19281 cp_parser_initializer. */
19283 static tree
19284 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19286 tree initializer;
19288 /* Consume the `{' token. */
19289 cp_lexer_consume_token (parser->lexer);
19290 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19291 initializer = make_node (CONSTRUCTOR);
19292 /* If it's not a `}', then there is a non-trivial initializer. */
19293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19295 /* Parse the initializer list. */
19296 CONSTRUCTOR_ELTS (initializer)
19297 = cp_parser_initializer_list (parser, non_constant_p);
19298 /* A trailing `,' token is allowed. */
19299 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19300 cp_lexer_consume_token (parser->lexer);
19302 else
19303 *non_constant_p = false;
19304 /* Now, there should be a trailing `}'. */
19305 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19306 TREE_TYPE (initializer) = init_list_type_node;
19307 return initializer;
19310 /* Consume tokens up to, and including, the next non-nested closing `]'.
19311 Returns true iff we found a closing `]'. */
19313 static bool
19314 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19316 unsigned square_depth = 0;
19318 while (true)
19320 cp_token * token = cp_lexer_peek_token (parser->lexer);
19322 switch (token->type)
19324 case CPP_EOF:
19325 case CPP_PRAGMA_EOL:
19326 /* If we've run out of tokens, then there is no closing `]'. */
19327 return false;
19329 case CPP_OPEN_SQUARE:
19330 ++square_depth;
19331 break;
19333 case CPP_CLOSE_SQUARE:
19334 if (!square_depth--)
19336 cp_lexer_consume_token (parser->lexer);
19337 return true;
19339 break;
19341 default:
19342 break;
19345 /* Consume the token. */
19346 cp_lexer_consume_token (parser->lexer);
19350 /* Return true if we are looking at an array-designator, false otherwise. */
19352 static bool
19353 cp_parser_array_designator_p (cp_parser *parser)
19355 /* Consume the `['. */
19356 cp_lexer_consume_token (parser->lexer);
19358 cp_lexer_save_tokens (parser->lexer);
19360 /* Skip tokens until the next token is a closing square bracket.
19361 If we find the closing `]', and the next token is a `=', then
19362 we are looking at an array designator. */
19363 bool array_designator_p
19364 = (cp_parser_skip_to_closing_square_bracket (parser)
19365 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19367 /* Roll back the tokens we skipped. */
19368 cp_lexer_rollback_tokens (parser->lexer);
19370 return array_designator_p;
19373 /* Parse an initializer-list.
19375 initializer-list:
19376 initializer-clause ... [opt]
19377 initializer-list , initializer-clause ... [opt]
19379 GNU Extension:
19381 initializer-list:
19382 designation initializer-clause ...[opt]
19383 initializer-list , designation initializer-clause ...[opt]
19385 designation:
19386 . identifier =
19387 identifier :
19388 [ constant-expression ] =
19390 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19391 for the initializer. If the INDEX of the elt is non-NULL, it is the
19392 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19393 as for cp_parser_initializer. */
19395 static vec<constructor_elt, va_gc> *
19396 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19398 vec<constructor_elt, va_gc> *v = NULL;
19400 /* Assume all of the expressions are constant. */
19401 *non_constant_p = false;
19403 /* Parse the rest of the list. */
19404 while (true)
19406 cp_token *token;
19407 tree designator;
19408 tree initializer;
19409 bool clause_non_constant_p;
19411 /* If the next token is an identifier and the following one is a
19412 colon, we are looking at the GNU designated-initializer
19413 syntax. */
19414 if (cp_parser_allow_gnu_extensions_p (parser)
19415 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19416 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19418 /* Warn the user that they are using an extension. */
19419 pedwarn (input_location, OPT_Wpedantic,
19420 "ISO C++ does not allow designated initializers");
19421 /* Consume the identifier. */
19422 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19423 /* Consume the `:'. */
19424 cp_lexer_consume_token (parser->lexer);
19426 /* Also handle the C99 syntax, '. id ='. */
19427 else if (cp_parser_allow_gnu_extensions_p (parser)
19428 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19429 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19430 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19432 /* Warn the user that they are using an extension. */
19433 pedwarn (input_location, OPT_Wpedantic,
19434 "ISO C++ does not allow C99 designated initializers");
19435 /* Consume the `.'. */
19436 cp_lexer_consume_token (parser->lexer);
19437 /* Consume the identifier. */
19438 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19439 /* Consume the `='. */
19440 cp_lexer_consume_token (parser->lexer);
19442 /* Also handle C99 array designators, '[ const ] ='. */
19443 else if (cp_parser_allow_gnu_extensions_p (parser)
19444 && !c_dialect_objc ()
19445 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19447 /* In C++11, [ could start a lambda-introducer. */
19448 bool non_const = false;
19450 cp_parser_parse_tentatively (parser);
19452 if (!cp_parser_array_designator_p (parser))
19454 cp_parser_simulate_error (parser);
19455 designator = NULL_TREE;
19457 else
19459 designator = cp_parser_constant_expression (parser, true,
19460 &non_const);
19461 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19462 cp_parser_require (parser, CPP_EQ, RT_EQ);
19465 if (!cp_parser_parse_definitely (parser))
19466 designator = NULL_TREE;
19467 else if (non_const)
19468 require_potential_rvalue_constant_expression (designator);
19470 else
19471 designator = NULL_TREE;
19473 /* Parse the initializer. */
19474 initializer = cp_parser_initializer_clause (parser,
19475 &clause_non_constant_p);
19476 /* If any clause is non-constant, so is the entire initializer. */
19477 if (clause_non_constant_p)
19478 *non_constant_p = true;
19480 /* If we have an ellipsis, this is an initializer pack
19481 expansion. */
19482 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19484 /* Consume the `...'. */
19485 cp_lexer_consume_token (parser->lexer);
19487 /* Turn the initializer into an initializer expansion. */
19488 initializer = make_pack_expansion (initializer);
19491 /* Add it to the vector. */
19492 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19494 /* If the next token is not a comma, we have reached the end of
19495 the list. */
19496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19497 break;
19499 /* Peek at the next token. */
19500 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19501 /* If the next token is a `}', then we're still done. An
19502 initializer-clause can have a trailing `,' after the
19503 initializer-list and before the closing `}'. */
19504 if (token->type == CPP_CLOSE_BRACE)
19505 break;
19507 /* Consume the `,' token. */
19508 cp_lexer_consume_token (parser->lexer);
19511 return v;
19514 /* Classes [gram.class] */
19516 /* Parse a class-name.
19518 class-name:
19519 identifier
19520 template-id
19522 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19523 to indicate that names looked up in dependent types should be
19524 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19525 keyword has been used to indicate that the name that appears next
19526 is a template. TAG_TYPE indicates the explicit tag given before
19527 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19528 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19529 is the class being defined in a class-head.
19531 Returns the TYPE_DECL representing the class. */
19533 static tree
19534 cp_parser_class_name (cp_parser *parser,
19535 bool typename_keyword_p,
19536 bool template_keyword_p,
19537 enum tag_types tag_type,
19538 bool check_dependency_p,
19539 bool class_head_p,
19540 bool is_declaration)
19542 tree decl;
19543 tree scope;
19544 bool typename_p;
19545 cp_token *token;
19546 tree identifier = NULL_TREE;
19548 /* All class-names start with an identifier. */
19549 token = cp_lexer_peek_token (parser->lexer);
19550 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19552 cp_parser_error (parser, "expected class-name");
19553 return error_mark_node;
19556 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19557 to a template-id, so we save it here. */
19558 scope = parser->scope;
19559 if (scope == error_mark_node)
19560 return error_mark_node;
19562 /* Any name names a type if we're following the `typename' keyword
19563 in a qualified name where the enclosing scope is type-dependent. */
19564 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19565 && dependent_type_p (scope));
19566 /* Handle the common case (an identifier, but not a template-id)
19567 efficiently. */
19568 if (token->type == CPP_NAME
19569 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19571 cp_token *identifier_token;
19572 bool ambiguous_p;
19574 /* Look for the identifier. */
19575 identifier_token = cp_lexer_peek_token (parser->lexer);
19576 ambiguous_p = identifier_token->error_reported;
19577 identifier = cp_parser_identifier (parser);
19578 /* If the next token isn't an identifier, we are certainly not
19579 looking at a class-name. */
19580 if (identifier == error_mark_node)
19581 decl = error_mark_node;
19582 /* If we know this is a type-name, there's no need to look it
19583 up. */
19584 else if (typename_p)
19585 decl = identifier;
19586 else
19588 tree ambiguous_decls;
19589 /* If we already know that this lookup is ambiguous, then
19590 we've already issued an error message; there's no reason
19591 to check again. */
19592 if (ambiguous_p)
19594 cp_parser_simulate_error (parser);
19595 return error_mark_node;
19597 /* If the next token is a `::', then the name must be a type
19598 name.
19600 [basic.lookup.qual]
19602 During the lookup for a name preceding the :: scope
19603 resolution operator, object, function, and enumerator
19604 names are ignored. */
19605 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19606 tag_type = typename_type;
19607 /* Look up the name. */
19608 decl = cp_parser_lookup_name (parser, identifier,
19609 tag_type,
19610 /*is_template=*/false,
19611 /*is_namespace=*/false,
19612 check_dependency_p,
19613 &ambiguous_decls,
19614 identifier_token->location);
19615 if (ambiguous_decls)
19617 if (cp_parser_parsing_tentatively (parser))
19618 cp_parser_simulate_error (parser);
19619 return error_mark_node;
19623 else
19625 /* Try a template-id. */
19626 decl = cp_parser_template_id (parser, template_keyword_p,
19627 check_dependency_p,
19628 tag_type,
19629 is_declaration);
19630 if (decl == error_mark_node)
19631 return error_mark_node;
19634 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19636 /* If this is a typename, create a TYPENAME_TYPE. */
19637 if (typename_p && decl != error_mark_node)
19639 decl = make_typename_type (scope, decl, typename_type,
19640 /*complain=*/tf_error);
19641 if (decl != error_mark_node)
19642 decl = TYPE_NAME (decl);
19645 decl = strip_using_decl (decl);
19647 /* Check to see that it is really the name of a class. */
19648 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19649 && identifier_p (TREE_OPERAND (decl, 0))
19650 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19651 /* Situations like this:
19653 template <typename T> struct A {
19654 typename T::template X<int>::I i;
19657 are problematic. Is `T::template X<int>' a class-name? The
19658 standard does not seem to be definitive, but there is no other
19659 valid interpretation of the following `::'. Therefore, those
19660 names are considered class-names. */
19662 decl = make_typename_type (scope, decl, tag_type, tf_error);
19663 if (decl != error_mark_node)
19664 decl = TYPE_NAME (decl);
19666 else if (TREE_CODE (decl) != TYPE_DECL
19667 || TREE_TYPE (decl) == error_mark_node
19668 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19669 /* In Objective-C 2.0, a classname followed by '.' starts a
19670 dot-syntax expression, and it's not a type-name. */
19671 || (c_dialect_objc ()
19672 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19673 && objc_is_class_name (decl)))
19674 decl = error_mark_node;
19676 if (decl == error_mark_node)
19677 cp_parser_error (parser, "expected class-name");
19678 else if (identifier && !parser->scope)
19679 maybe_note_name_used_in_class (identifier, decl);
19681 return decl;
19684 /* Parse a class-specifier.
19686 class-specifier:
19687 class-head { member-specification [opt] }
19689 Returns the TREE_TYPE representing the class. */
19691 static tree
19692 cp_parser_class_specifier_1 (cp_parser* parser)
19694 tree type;
19695 tree attributes = NULL_TREE;
19696 bool nested_name_specifier_p;
19697 unsigned saved_num_template_parameter_lists;
19698 bool saved_in_function_body;
19699 unsigned char in_statement;
19700 bool in_switch_statement_p;
19701 bool saved_in_unbraced_linkage_specification_p;
19702 tree old_scope = NULL_TREE;
19703 tree scope = NULL_TREE;
19704 cp_token *closing_brace;
19706 push_deferring_access_checks (dk_no_deferred);
19708 /* Parse the class-head. */
19709 type = cp_parser_class_head (parser,
19710 &nested_name_specifier_p);
19711 /* If the class-head was a semantic disaster, skip the entire body
19712 of the class. */
19713 if (!type)
19715 cp_parser_skip_to_end_of_block_or_statement (parser);
19716 pop_deferring_access_checks ();
19717 return error_mark_node;
19720 /* Look for the `{'. */
19721 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19723 pop_deferring_access_checks ();
19724 return error_mark_node;
19727 cp_ensure_no_omp_declare_simd (parser);
19729 /* Issue an error message if type-definitions are forbidden here. */
19730 cp_parser_check_type_definition (parser);
19731 /* Remember that we are defining one more class. */
19732 ++parser->num_classes_being_defined;
19733 /* Inside the class, surrounding template-parameter-lists do not
19734 apply. */
19735 saved_num_template_parameter_lists
19736 = parser->num_template_parameter_lists;
19737 parser->num_template_parameter_lists = 0;
19738 /* We are not in a function body. */
19739 saved_in_function_body = parser->in_function_body;
19740 parser->in_function_body = false;
19741 /* Or in a loop. */
19742 in_statement = parser->in_statement;
19743 parser->in_statement = 0;
19744 /* Or in a switch. */
19745 in_switch_statement_p = parser->in_switch_statement_p;
19746 parser->in_switch_statement_p = false;
19747 /* We are not immediately inside an extern "lang" block. */
19748 saved_in_unbraced_linkage_specification_p
19749 = parser->in_unbraced_linkage_specification_p;
19750 parser->in_unbraced_linkage_specification_p = false;
19752 /* Start the class. */
19753 if (nested_name_specifier_p)
19755 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19756 old_scope = push_inner_scope (scope);
19758 type = begin_class_definition (type);
19760 if (type == error_mark_node)
19761 /* If the type is erroneous, skip the entire body of the class. */
19762 cp_parser_skip_to_closing_brace (parser);
19763 else
19764 /* Parse the member-specification. */
19765 cp_parser_member_specification_opt (parser);
19767 /* Look for the trailing `}'. */
19768 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19769 /* Look for trailing attributes to apply to this class. */
19770 if (cp_parser_allow_gnu_extensions_p (parser))
19771 attributes = cp_parser_gnu_attributes_opt (parser);
19772 if (type != error_mark_node)
19773 type = finish_struct (type, attributes);
19774 if (nested_name_specifier_p)
19775 pop_inner_scope (old_scope, scope);
19777 /* We've finished a type definition. Check for the common syntax
19778 error of forgetting a semicolon after the definition. We need to
19779 be careful, as we can't just check for not-a-semicolon and be done
19780 with it; the user might have typed:
19782 class X { } c = ...;
19783 class X { } *p = ...;
19785 and so forth. Instead, enumerate all the possible tokens that
19786 might follow this production; if we don't see one of them, then
19787 complain and silently insert the semicolon. */
19789 cp_token *token = cp_lexer_peek_token (parser->lexer);
19790 bool want_semicolon = true;
19792 if (cp_next_tokens_can_be_std_attribute_p (parser))
19793 /* Don't try to parse c++11 attributes here. As per the
19794 grammar, that should be a task for
19795 cp_parser_decl_specifier_seq. */
19796 want_semicolon = false;
19798 switch (token->type)
19800 case CPP_NAME:
19801 case CPP_SEMICOLON:
19802 case CPP_MULT:
19803 case CPP_AND:
19804 case CPP_OPEN_PAREN:
19805 case CPP_CLOSE_PAREN:
19806 case CPP_COMMA:
19807 want_semicolon = false;
19808 break;
19810 /* While it's legal for type qualifiers and storage class
19811 specifiers to follow type definitions in the grammar, only
19812 compiler testsuites contain code like that. Assume that if
19813 we see such code, then what we're really seeing is a case
19814 like:
19816 class X { }
19817 const <type> var = ...;
19821 class Y { }
19822 static <type> func (...) ...
19824 i.e. the qualifier or specifier applies to the next
19825 declaration. To do so, however, we need to look ahead one
19826 more token to see if *that* token is a type specifier.
19828 This code could be improved to handle:
19830 class Z { }
19831 static const <type> var = ...; */
19832 case CPP_KEYWORD:
19833 if (keyword_is_decl_specifier (token->keyword))
19835 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19837 /* Handling user-defined types here would be nice, but very
19838 tricky. */
19839 want_semicolon
19840 = (lookahead->type == CPP_KEYWORD
19841 && keyword_begins_type_specifier (lookahead->keyword));
19843 break;
19844 default:
19845 break;
19848 /* If we don't have a type, then something is very wrong and we
19849 shouldn't try to do anything clever. Likewise for not seeing the
19850 closing brace. */
19851 if (closing_brace && TYPE_P (type) && want_semicolon)
19853 cp_token_position prev
19854 = cp_lexer_previous_token_position (parser->lexer);
19855 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19856 location_t loc = prev_token->location;
19858 if (CLASSTYPE_DECLARED_CLASS (type))
19859 error_at (loc, "expected %<;%> after class definition");
19860 else if (TREE_CODE (type) == RECORD_TYPE)
19861 error_at (loc, "expected %<;%> after struct definition");
19862 else if (TREE_CODE (type) == UNION_TYPE)
19863 error_at (loc, "expected %<;%> after union definition");
19864 else
19865 gcc_unreachable ();
19867 /* Unget one token and smash it to look as though we encountered
19868 a semicolon in the input stream. */
19869 cp_lexer_set_token_position (parser->lexer, prev);
19870 token = cp_lexer_peek_token (parser->lexer);
19871 token->type = CPP_SEMICOLON;
19872 token->keyword = RID_MAX;
19876 /* If this class is not itself within the scope of another class,
19877 then we need to parse the bodies of all of the queued function
19878 definitions. Note that the queued functions defined in a class
19879 are not always processed immediately following the
19880 class-specifier for that class. Consider:
19882 struct A {
19883 struct B { void f() { sizeof (A); } };
19886 If `f' were processed before the processing of `A' were
19887 completed, there would be no way to compute the size of `A'.
19888 Note that the nesting we are interested in here is lexical --
19889 not the semantic nesting given by TYPE_CONTEXT. In particular,
19890 for:
19892 struct A { struct B; };
19893 struct A::B { void f() { } };
19895 there is no need to delay the parsing of `A::B::f'. */
19896 if (--parser->num_classes_being_defined == 0)
19898 tree decl;
19899 tree class_type = NULL_TREE;
19900 tree pushed_scope = NULL_TREE;
19901 unsigned ix;
19902 cp_default_arg_entry *e;
19903 tree save_ccp, save_ccr;
19905 /* In a first pass, parse default arguments to the functions.
19906 Then, in a second pass, parse the bodies of the functions.
19907 This two-phased approach handles cases like:
19909 struct S {
19910 void f() { g(); }
19911 void g(int i = 3);
19915 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19917 decl = e->decl;
19918 /* If there are default arguments that have not yet been processed,
19919 take care of them now. */
19920 if (class_type != e->class_type)
19922 if (pushed_scope)
19923 pop_scope (pushed_scope);
19924 class_type = e->class_type;
19925 pushed_scope = push_scope (class_type);
19927 /* Make sure that any template parameters are in scope. */
19928 maybe_begin_member_template_processing (decl);
19929 /* Parse the default argument expressions. */
19930 cp_parser_late_parsing_default_args (parser, decl);
19931 /* Remove any template parameters from the symbol table. */
19932 maybe_end_member_template_processing ();
19934 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19935 /* Now parse any NSDMIs. */
19936 save_ccp = current_class_ptr;
19937 save_ccr = current_class_ref;
19938 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19940 if (class_type != DECL_CONTEXT (decl))
19942 if (pushed_scope)
19943 pop_scope (pushed_scope);
19944 class_type = DECL_CONTEXT (decl);
19945 pushed_scope = push_scope (class_type);
19947 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19948 cp_parser_late_parsing_nsdmi (parser, decl);
19950 vec_safe_truncate (unparsed_nsdmis, 0);
19951 current_class_ptr = save_ccp;
19952 current_class_ref = save_ccr;
19953 if (pushed_scope)
19954 pop_scope (pushed_scope);
19956 /* Now do some post-NSDMI bookkeeping. */
19957 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19958 after_nsdmi_defaulted_late_checks (class_type);
19959 vec_safe_truncate (unparsed_classes, 0);
19960 after_nsdmi_defaulted_late_checks (type);
19962 /* Now parse the body of the functions. */
19963 if (flag_openmp)
19965 /* OpenMP UDRs need to be parsed before all other functions. */
19966 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19967 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19968 cp_parser_late_parsing_for_member (parser, decl);
19969 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19970 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19971 cp_parser_late_parsing_for_member (parser, decl);
19973 else
19974 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19975 cp_parser_late_parsing_for_member (parser, decl);
19976 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19978 else
19979 vec_safe_push (unparsed_classes, type);
19981 /* Put back any saved access checks. */
19982 pop_deferring_access_checks ();
19984 /* Restore saved state. */
19985 parser->in_switch_statement_p = in_switch_statement_p;
19986 parser->in_statement = in_statement;
19987 parser->in_function_body = saved_in_function_body;
19988 parser->num_template_parameter_lists
19989 = saved_num_template_parameter_lists;
19990 parser->in_unbraced_linkage_specification_p
19991 = saved_in_unbraced_linkage_specification_p;
19993 return type;
19996 static tree
19997 cp_parser_class_specifier (cp_parser* parser)
19999 tree ret;
20000 timevar_push (TV_PARSE_STRUCT);
20001 ret = cp_parser_class_specifier_1 (parser);
20002 timevar_pop (TV_PARSE_STRUCT);
20003 return ret;
20006 /* Parse a class-head.
20008 class-head:
20009 class-key identifier [opt] base-clause [opt]
20010 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20011 class-key nested-name-specifier [opt] template-id
20012 base-clause [opt]
20014 class-virt-specifier:
20015 final
20017 GNU Extensions:
20018 class-key attributes identifier [opt] base-clause [opt]
20019 class-key attributes nested-name-specifier identifier base-clause [opt]
20020 class-key attributes nested-name-specifier [opt] template-id
20021 base-clause [opt]
20023 Upon return BASES is initialized to the list of base classes (or
20024 NULL, if there are none) in the same form returned by
20025 cp_parser_base_clause.
20027 Returns the TYPE of the indicated class. Sets
20028 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20029 involving a nested-name-specifier was used, and FALSE otherwise.
20031 Returns error_mark_node if this is not a class-head.
20033 Returns NULL_TREE if the class-head is syntactically valid, but
20034 semantically invalid in a way that means we should skip the entire
20035 body of the class. */
20037 static tree
20038 cp_parser_class_head (cp_parser* parser,
20039 bool* nested_name_specifier_p)
20041 tree nested_name_specifier;
20042 enum tag_types class_key;
20043 tree id = NULL_TREE;
20044 tree type = NULL_TREE;
20045 tree attributes;
20046 tree bases;
20047 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20048 bool template_id_p = false;
20049 bool qualified_p = false;
20050 bool invalid_nested_name_p = false;
20051 bool invalid_explicit_specialization_p = false;
20052 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20053 tree pushed_scope = NULL_TREE;
20054 unsigned num_templates;
20055 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20056 /* Assume no nested-name-specifier will be present. */
20057 *nested_name_specifier_p = false;
20058 /* Assume no template parameter lists will be used in defining the
20059 type. */
20060 num_templates = 0;
20061 parser->colon_corrects_to_scope_p = false;
20063 /* Look for the class-key. */
20064 class_key = cp_parser_class_key (parser);
20065 if (class_key == none_type)
20066 return error_mark_node;
20068 /* Parse the attributes. */
20069 attributes = cp_parser_attributes_opt (parser);
20071 /* If the next token is `::', that is invalid -- but sometimes
20072 people do try to write:
20074 struct ::S {};
20076 Handle this gracefully by accepting the extra qualifier, and then
20077 issuing an error about it later if this really is a
20078 class-head. If it turns out just to be an elaborated type
20079 specifier, remain silent. */
20080 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20081 qualified_p = true;
20083 push_deferring_access_checks (dk_no_check);
20085 /* Determine the name of the class. Begin by looking for an
20086 optional nested-name-specifier. */
20087 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20088 nested_name_specifier
20089 = cp_parser_nested_name_specifier_opt (parser,
20090 /*typename_keyword_p=*/false,
20091 /*check_dependency_p=*/false,
20092 /*type_p=*/true,
20093 /*is_declaration=*/false);
20094 /* If there was a nested-name-specifier, then there *must* be an
20095 identifier. */
20096 if (nested_name_specifier)
20098 type_start_token = cp_lexer_peek_token (parser->lexer);
20099 /* Although the grammar says `identifier', it really means
20100 `class-name' or `template-name'. You are only allowed to
20101 define a class that has already been declared with this
20102 syntax.
20104 The proposed resolution for Core Issue 180 says that wherever
20105 you see `class T::X' you should treat `X' as a type-name.
20107 It is OK to define an inaccessible class; for example:
20109 class A { class B; };
20110 class A::B {};
20112 We do not know if we will see a class-name, or a
20113 template-name. We look for a class-name first, in case the
20114 class-name is a template-id; if we looked for the
20115 template-name first we would stop after the template-name. */
20116 cp_parser_parse_tentatively (parser);
20117 type = cp_parser_class_name (parser,
20118 /*typename_keyword_p=*/false,
20119 /*template_keyword_p=*/false,
20120 class_type,
20121 /*check_dependency_p=*/false,
20122 /*class_head_p=*/true,
20123 /*is_declaration=*/false);
20124 /* If that didn't work, ignore the nested-name-specifier. */
20125 if (!cp_parser_parse_definitely (parser))
20127 invalid_nested_name_p = true;
20128 type_start_token = cp_lexer_peek_token (parser->lexer);
20129 id = cp_parser_identifier (parser);
20130 if (id == error_mark_node)
20131 id = NULL_TREE;
20133 /* If we could not find a corresponding TYPE, treat this
20134 declaration like an unqualified declaration. */
20135 if (type == error_mark_node)
20136 nested_name_specifier = NULL_TREE;
20137 /* Otherwise, count the number of templates used in TYPE and its
20138 containing scopes. */
20139 else
20141 tree scope;
20143 for (scope = TREE_TYPE (type);
20144 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20145 scope = get_containing_scope (scope))
20146 if (TYPE_P (scope)
20147 && CLASS_TYPE_P (scope)
20148 && CLASSTYPE_TEMPLATE_INFO (scope)
20149 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20150 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20151 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20152 ++num_templates;
20155 /* Otherwise, the identifier is optional. */
20156 else
20158 /* We don't know whether what comes next is a template-id,
20159 an identifier, or nothing at all. */
20160 cp_parser_parse_tentatively (parser);
20161 /* Check for a template-id. */
20162 type_start_token = cp_lexer_peek_token (parser->lexer);
20163 id = cp_parser_template_id (parser,
20164 /*template_keyword_p=*/false,
20165 /*check_dependency_p=*/true,
20166 class_key,
20167 /*is_declaration=*/true);
20168 /* If that didn't work, it could still be an identifier. */
20169 if (!cp_parser_parse_definitely (parser))
20171 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20173 type_start_token = cp_lexer_peek_token (parser->lexer);
20174 id = cp_parser_identifier (parser);
20176 else
20177 id = NULL_TREE;
20179 else
20181 template_id_p = true;
20182 ++num_templates;
20186 pop_deferring_access_checks ();
20188 if (id)
20190 cp_parser_check_for_invalid_template_id (parser, id,
20191 class_key,
20192 type_start_token->location);
20194 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20196 /* If it's not a `:' or a `{' then we can't really be looking at a
20197 class-head, since a class-head only appears as part of a
20198 class-specifier. We have to detect this situation before calling
20199 xref_tag, since that has irreversible side-effects. */
20200 if (!cp_parser_next_token_starts_class_definition_p (parser))
20202 cp_parser_error (parser, "expected %<{%> or %<:%>");
20203 type = error_mark_node;
20204 goto out;
20207 /* At this point, we're going ahead with the class-specifier, even
20208 if some other problem occurs. */
20209 cp_parser_commit_to_tentative_parse (parser);
20210 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20212 cp_parser_error (parser,
20213 "cannot specify %<override%> for a class");
20214 type = error_mark_node;
20215 goto out;
20217 /* Issue the error about the overly-qualified name now. */
20218 if (qualified_p)
20220 cp_parser_error (parser,
20221 "global qualification of class name is invalid");
20222 type = error_mark_node;
20223 goto out;
20225 else if (invalid_nested_name_p)
20227 cp_parser_error (parser,
20228 "qualified name does not name a class");
20229 type = error_mark_node;
20230 goto out;
20232 else if (nested_name_specifier)
20234 tree scope;
20236 /* Reject typedef-names in class heads. */
20237 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20239 error_at (type_start_token->location,
20240 "invalid class name in declaration of %qD",
20241 type);
20242 type = NULL_TREE;
20243 goto done;
20246 /* Figure out in what scope the declaration is being placed. */
20247 scope = current_scope ();
20248 /* If that scope does not contain the scope in which the
20249 class was originally declared, the program is invalid. */
20250 if (scope && !is_ancestor (scope, nested_name_specifier))
20252 if (at_namespace_scope_p ())
20253 error_at (type_start_token->location,
20254 "declaration of %qD in namespace %qD which does not "
20255 "enclose %qD",
20256 type, scope, nested_name_specifier);
20257 else
20258 error_at (type_start_token->location,
20259 "declaration of %qD in %qD which does not enclose %qD",
20260 type, scope, nested_name_specifier);
20261 type = NULL_TREE;
20262 goto done;
20264 /* [dcl.meaning]
20266 A declarator-id shall not be qualified except for the
20267 definition of a ... nested class outside of its class
20268 ... [or] the definition or explicit instantiation of a
20269 class member of a namespace outside of its namespace. */
20270 if (scope == nested_name_specifier)
20272 permerror (nested_name_specifier_token_start->location,
20273 "extra qualification not allowed");
20274 nested_name_specifier = NULL_TREE;
20275 num_templates = 0;
20278 /* An explicit-specialization must be preceded by "template <>". If
20279 it is not, try to recover gracefully. */
20280 if (at_namespace_scope_p ()
20281 && parser->num_template_parameter_lists == 0
20282 && template_id_p)
20284 error_at (type_start_token->location,
20285 "an explicit specialization must be preceded by %<template <>%>");
20286 invalid_explicit_specialization_p = true;
20287 /* Take the same action that would have been taken by
20288 cp_parser_explicit_specialization. */
20289 ++parser->num_template_parameter_lists;
20290 begin_specialization ();
20292 /* There must be no "return" statements between this point and the
20293 end of this function; set "type "to the correct return value and
20294 use "goto done;" to return. */
20295 /* Make sure that the right number of template parameters were
20296 present. */
20297 if (!cp_parser_check_template_parameters (parser, num_templates,
20298 type_start_token->location,
20299 /*declarator=*/NULL))
20301 /* If something went wrong, there is no point in even trying to
20302 process the class-definition. */
20303 type = NULL_TREE;
20304 goto done;
20307 /* Look up the type. */
20308 if (template_id_p)
20310 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20311 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20312 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20314 error_at (type_start_token->location,
20315 "function template %qD redeclared as a class template", id);
20316 type = error_mark_node;
20318 else
20320 type = TREE_TYPE (id);
20321 type = maybe_process_partial_specialization (type);
20323 if (nested_name_specifier)
20324 pushed_scope = push_scope (nested_name_specifier);
20326 else if (nested_name_specifier)
20328 tree class_type;
20330 /* Given:
20332 template <typename T> struct S { struct T };
20333 template <typename T> struct S<T>::T { };
20335 we will get a TYPENAME_TYPE when processing the definition of
20336 `S::T'. We need to resolve it to the actual type before we
20337 try to define it. */
20338 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20340 class_type = resolve_typename_type (TREE_TYPE (type),
20341 /*only_current_p=*/false);
20342 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20343 type = TYPE_NAME (class_type);
20344 else
20346 cp_parser_error (parser, "could not resolve typename type");
20347 type = error_mark_node;
20351 if (maybe_process_partial_specialization (TREE_TYPE (type))
20352 == error_mark_node)
20354 type = NULL_TREE;
20355 goto done;
20358 class_type = current_class_type;
20359 /* Enter the scope indicated by the nested-name-specifier. */
20360 pushed_scope = push_scope (nested_name_specifier);
20361 /* Get the canonical version of this type. */
20362 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20363 /* Call push_template_decl if it seems like we should be defining a
20364 template either from the template headers or the type we're
20365 defining, so that we diagnose both extra and missing headers. */
20366 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20367 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20368 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20370 type = push_template_decl (type);
20371 if (type == error_mark_node)
20373 type = NULL_TREE;
20374 goto done;
20378 type = TREE_TYPE (type);
20379 *nested_name_specifier_p = true;
20381 else /* The name is not a nested name. */
20383 /* If the class was unnamed, create a dummy name. */
20384 if (!id)
20385 id = make_anon_name ();
20386 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20387 parser->num_template_parameter_lists);
20390 /* Indicate whether this class was declared as a `class' or as a
20391 `struct'. */
20392 if (TREE_CODE (type) == RECORD_TYPE)
20393 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20394 cp_parser_check_class_key (class_key, type);
20396 /* If this type was already complete, and we see another definition,
20397 that's an error. */
20398 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20400 error_at (type_start_token->location, "redefinition of %q#T",
20401 type);
20402 error_at (type_start_token->location, "previous definition of %q+#T",
20403 type);
20404 type = NULL_TREE;
20405 goto done;
20407 else if (type == error_mark_node)
20408 type = NULL_TREE;
20410 if (type)
20412 /* Apply attributes now, before any use of the class as a template
20413 argument in its base list. */
20414 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20415 fixup_attribute_variants (type);
20418 /* We will have entered the scope containing the class; the names of
20419 base classes should be looked up in that context. For example:
20421 struct A { struct B {}; struct C; };
20422 struct A::C : B {};
20424 is valid. */
20426 /* Get the list of base-classes, if there is one. */
20427 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20429 /* PR59482: enter the class scope so that base-specifiers are looked
20430 up correctly. */
20431 if (type)
20432 pushclass (type);
20433 bases = cp_parser_base_clause (parser);
20434 /* PR59482: get out of the previously pushed class scope so that the
20435 subsequent pops pop the right thing. */
20436 if (type)
20437 popclass ();
20439 else
20440 bases = NULL_TREE;
20442 /* If we're really defining a class, process the base classes.
20443 If they're invalid, fail. */
20444 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20445 && !xref_basetypes (type, bases))
20446 type = NULL_TREE;
20448 done:
20449 /* Leave the scope given by the nested-name-specifier. We will
20450 enter the class scope itself while processing the members. */
20451 if (pushed_scope)
20452 pop_scope (pushed_scope);
20454 if (invalid_explicit_specialization_p)
20456 end_specialization ();
20457 --parser->num_template_parameter_lists;
20460 if (type)
20461 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20462 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20463 CLASSTYPE_FINAL (type) = 1;
20464 out:
20465 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20466 return type;
20469 /* Parse a class-key.
20471 class-key:
20472 class
20473 struct
20474 union
20476 Returns the kind of class-key specified, or none_type to indicate
20477 error. */
20479 static enum tag_types
20480 cp_parser_class_key (cp_parser* parser)
20482 cp_token *token;
20483 enum tag_types tag_type;
20485 /* Look for the class-key. */
20486 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20487 if (!token)
20488 return none_type;
20490 /* Check to see if the TOKEN is a class-key. */
20491 tag_type = cp_parser_token_is_class_key (token);
20492 if (!tag_type)
20493 cp_parser_error (parser, "expected class-key");
20494 return tag_type;
20497 /* Parse a type-parameter-key.
20499 type-parameter-key:
20500 class
20501 typename
20504 static void
20505 cp_parser_type_parameter_key (cp_parser* parser)
20507 /* Look for the type-parameter-key. */
20508 enum tag_types tag_type = none_type;
20509 cp_token *token = cp_lexer_peek_token (parser->lexer);
20510 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20512 cp_lexer_consume_token (parser->lexer);
20513 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20514 /* typename is not allowed in a template template parameter
20515 by the standard until C++1Z. */
20516 pedwarn (token->location, OPT_Wpedantic,
20517 "ISO C++ forbids typename key in template template parameter;"
20518 " use -std=c++1z or -std=gnu++1z");
20520 else
20521 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20523 return;
20526 /* Parse an (optional) member-specification.
20528 member-specification:
20529 member-declaration member-specification [opt]
20530 access-specifier : member-specification [opt] */
20532 static void
20533 cp_parser_member_specification_opt (cp_parser* parser)
20535 while (true)
20537 cp_token *token;
20538 enum rid keyword;
20540 /* Peek at the next token. */
20541 token = cp_lexer_peek_token (parser->lexer);
20542 /* If it's a `}', or EOF then we've seen all the members. */
20543 if (token->type == CPP_CLOSE_BRACE
20544 || token->type == CPP_EOF
20545 || token->type == CPP_PRAGMA_EOL)
20546 break;
20548 /* See if this token is a keyword. */
20549 keyword = token->keyword;
20550 switch (keyword)
20552 case RID_PUBLIC:
20553 case RID_PROTECTED:
20554 case RID_PRIVATE:
20555 /* Consume the access-specifier. */
20556 cp_lexer_consume_token (parser->lexer);
20557 /* Remember which access-specifier is active. */
20558 current_access_specifier = token->u.value;
20559 /* Look for the `:'. */
20560 cp_parser_require (parser, CPP_COLON, RT_COLON);
20561 break;
20563 default:
20564 /* Accept #pragmas at class scope. */
20565 if (token->type == CPP_PRAGMA)
20567 cp_parser_pragma (parser, pragma_member);
20568 break;
20571 /* Otherwise, the next construction must be a
20572 member-declaration. */
20573 cp_parser_member_declaration (parser);
20578 /* Parse a member-declaration.
20580 member-declaration:
20581 decl-specifier-seq [opt] member-declarator-list [opt] ;
20582 function-definition ; [opt]
20583 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20584 using-declaration
20585 template-declaration
20586 alias-declaration
20588 member-declarator-list:
20589 member-declarator
20590 member-declarator-list , member-declarator
20592 member-declarator:
20593 declarator pure-specifier [opt]
20594 declarator constant-initializer [opt]
20595 identifier [opt] : constant-expression
20597 GNU Extensions:
20599 member-declaration:
20600 __extension__ member-declaration
20602 member-declarator:
20603 declarator attributes [opt] pure-specifier [opt]
20604 declarator attributes [opt] constant-initializer [opt]
20605 identifier [opt] attributes [opt] : constant-expression
20607 C++0x Extensions:
20609 member-declaration:
20610 static_assert-declaration */
20612 static void
20613 cp_parser_member_declaration (cp_parser* parser)
20615 cp_decl_specifier_seq decl_specifiers;
20616 tree prefix_attributes;
20617 tree decl;
20618 int declares_class_or_enum;
20619 bool friend_p;
20620 cp_token *token = NULL;
20621 cp_token *decl_spec_token_start = NULL;
20622 cp_token *initializer_token_start = NULL;
20623 int saved_pedantic;
20624 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20626 /* Check for the `__extension__' keyword. */
20627 if (cp_parser_extension_opt (parser, &saved_pedantic))
20629 /* Recurse. */
20630 cp_parser_member_declaration (parser);
20631 /* Restore the old value of the PEDANTIC flag. */
20632 pedantic = saved_pedantic;
20634 return;
20637 /* Check for a template-declaration. */
20638 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20640 /* An explicit specialization here is an error condition, and we
20641 expect the specialization handler to detect and report this. */
20642 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20643 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20644 cp_parser_explicit_specialization (parser);
20645 else
20646 cp_parser_template_declaration (parser, /*member_p=*/true);
20648 return;
20651 /* Check for a using-declaration. */
20652 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20654 if (cxx_dialect < cxx11)
20656 /* Parse the using-declaration. */
20657 cp_parser_using_declaration (parser,
20658 /*access_declaration_p=*/false);
20659 return;
20661 else
20663 tree decl;
20664 bool alias_decl_expected;
20665 cp_parser_parse_tentatively (parser);
20666 decl = cp_parser_alias_declaration (parser);
20667 /* Note that if we actually see the '=' token after the
20668 identifier, cp_parser_alias_declaration commits the
20669 tentative parse. In that case, we really expects an
20670 alias-declaration. Otherwise, we expect a using
20671 declaration. */
20672 alias_decl_expected =
20673 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20674 cp_parser_parse_definitely (parser);
20676 if (alias_decl_expected)
20677 finish_member_declaration (decl);
20678 else
20679 cp_parser_using_declaration (parser,
20680 /*access_declaration_p=*/false);
20681 return;
20685 /* Check for @defs. */
20686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20688 tree ivar, member;
20689 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20690 ivar = ivar_chains;
20691 while (ivar)
20693 member = ivar;
20694 ivar = TREE_CHAIN (member);
20695 TREE_CHAIN (member) = NULL_TREE;
20696 finish_member_declaration (member);
20698 return;
20701 /* If the next token is `static_assert' we have a static assertion. */
20702 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20704 cp_parser_static_assert (parser, /*member_p=*/true);
20705 return;
20708 parser->colon_corrects_to_scope_p = false;
20710 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20711 goto out;
20713 /* Parse the decl-specifier-seq. */
20714 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20715 cp_parser_decl_specifier_seq (parser,
20716 CP_PARSER_FLAGS_OPTIONAL,
20717 &decl_specifiers,
20718 &declares_class_or_enum);
20719 /* Check for an invalid type-name. */
20720 if (!decl_specifiers.any_type_specifiers_p
20721 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20722 goto out;
20723 /* If there is no declarator, then the decl-specifier-seq should
20724 specify a type. */
20725 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20727 /* If there was no decl-specifier-seq, and the next token is a
20728 `;', then we have something like:
20730 struct S { ; };
20732 [class.mem]
20734 Each member-declaration shall declare at least one member
20735 name of the class. */
20736 if (!decl_specifiers.any_specifiers_p)
20738 cp_token *token = cp_lexer_peek_token (parser->lexer);
20739 if (!in_system_header_at (token->location))
20740 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20742 else
20744 tree type;
20746 /* See if this declaration is a friend. */
20747 friend_p = cp_parser_friend_p (&decl_specifiers);
20748 /* If there were decl-specifiers, check to see if there was
20749 a class-declaration. */
20750 type = check_tag_decl (&decl_specifiers,
20751 /*explicit_type_instantiation_p=*/false);
20752 /* Nested classes have already been added to the class, but
20753 a `friend' needs to be explicitly registered. */
20754 if (friend_p)
20756 /* If the `friend' keyword was present, the friend must
20757 be introduced with a class-key. */
20758 if (!declares_class_or_enum && cxx_dialect < cxx11)
20759 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20760 "in C++03 a class-key must be used "
20761 "when declaring a friend");
20762 /* In this case:
20764 template <typename T> struct A {
20765 friend struct A<T>::B;
20768 A<T>::B will be represented by a TYPENAME_TYPE, and
20769 therefore not recognized by check_tag_decl. */
20770 if (!type)
20772 type = decl_specifiers.type;
20773 if (type && TREE_CODE (type) == TYPE_DECL)
20774 type = TREE_TYPE (type);
20776 if (!type || !TYPE_P (type))
20777 error_at (decl_spec_token_start->location,
20778 "friend declaration does not name a class or "
20779 "function");
20780 else
20781 make_friend_class (current_class_type, type,
20782 /*complain=*/true);
20784 /* If there is no TYPE, an error message will already have
20785 been issued. */
20786 else if (!type || type == error_mark_node)
20788 /* An anonymous aggregate has to be handled specially; such
20789 a declaration really declares a data member (with a
20790 particular type), as opposed to a nested class. */
20791 else if (ANON_AGGR_TYPE_P (type))
20793 /* C++11 9.5/6. */
20794 if (decl_specifiers.storage_class != sc_none)
20795 error_at (decl_spec_token_start->location,
20796 "a storage class on an anonymous aggregate "
20797 "in class scope is not allowed");
20799 /* Remove constructors and such from TYPE, now that we
20800 know it is an anonymous aggregate. */
20801 fixup_anonymous_aggr (type);
20802 /* And make the corresponding data member. */
20803 decl = build_decl (decl_spec_token_start->location,
20804 FIELD_DECL, NULL_TREE, type);
20805 /* Add it to the class. */
20806 finish_member_declaration (decl);
20808 else
20809 cp_parser_check_access_in_redeclaration
20810 (TYPE_NAME (type),
20811 decl_spec_token_start->location);
20814 else
20816 bool assume_semicolon = false;
20818 /* Clear attributes from the decl_specifiers but keep them
20819 around as prefix attributes that apply them to the entity
20820 being declared. */
20821 prefix_attributes = decl_specifiers.attributes;
20822 decl_specifiers.attributes = NULL_TREE;
20824 /* See if these declarations will be friends. */
20825 friend_p = cp_parser_friend_p (&decl_specifiers);
20827 /* Keep going until we hit the `;' at the end of the
20828 declaration. */
20829 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20831 tree attributes = NULL_TREE;
20832 tree first_attribute;
20834 /* Peek at the next token. */
20835 token = cp_lexer_peek_token (parser->lexer);
20837 /* Check for a bitfield declaration. */
20838 if (token->type == CPP_COLON
20839 || (token->type == CPP_NAME
20840 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20841 == CPP_COLON))
20843 tree identifier;
20844 tree width;
20846 /* Get the name of the bitfield. Note that we cannot just
20847 check TOKEN here because it may have been invalidated by
20848 the call to cp_lexer_peek_nth_token above. */
20849 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20850 identifier = cp_parser_identifier (parser);
20851 else
20852 identifier = NULL_TREE;
20854 /* Consume the `:' token. */
20855 cp_lexer_consume_token (parser->lexer);
20856 /* Get the width of the bitfield. */
20857 width
20858 = cp_parser_constant_expression (parser);
20860 /* Look for attributes that apply to the bitfield. */
20861 attributes = cp_parser_attributes_opt (parser);
20862 /* Remember which attributes are prefix attributes and
20863 which are not. */
20864 first_attribute = attributes;
20865 /* Combine the attributes. */
20866 attributes = chainon (prefix_attributes, attributes);
20868 /* Create the bitfield declaration. */
20869 decl = grokbitfield (identifier
20870 ? make_id_declarator (NULL_TREE,
20871 identifier,
20872 sfk_none)
20873 : NULL,
20874 &decl_specifiers,
20875 width,
20876 attributes);
20878 else
20880 cp_declarator *declarator;
20881 tree initializer;
20882 tree asm_specification;
20883 int ctor_dtor_or_conv_p;
20885 /* Parse the declarator. */
20886 declarator
20887 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20888 &ctor_dtor_or_conv_p,
20889 /*parenthesized_p=*/NULL,
20890 /*member_p=*/true,
20891 friend_p);
20893 /* If something went wrong parsing the declarator, make sure
20894 that we at least consume some tokens. */
20895 if (declarator == cp_error_declarator)
20897 /* Skip to the end of the statement. */
20898 cp_parser_skip_to_end_of_statement (parser);
20899 /* If the next token is not a semicolon, that is
20900 probably because we just skipped over the body of
20901 a function. So, we consume a semicolon if
20902 present, but do not issue an error message if it
20903 is not present. */
20904 if (cp_lexer_next_token_is (parser->lexer,
20905 CPP_SEMICOLON))
20906 cp_lexer_consume_token (parser->lexer);
20907 goto out;
20910 if (declares_class_or_enum & 2)
20911 cp_parser_check_for_definition_in_return_type
20912 (declarator, decl_specifiers.type,
20913 decl_specifiers.locations[ds_type_spec]);
20915 /* Look for an asm-specification. */
20916 asm_specification = cp_parser_asm_specification_opt (parser);
20917 /* Look for attributes that apply to the declaration. */
20918 attributes = cp_parser_attributes_opt (parser);
20919 /* Remember which attributes are prefix attributes and
20920 which are not. */
20921 first_attribute = attributes;
20922 /* Combine the attributes. */
20923 attributes = chainon (prefix_attributes, attributes);
20925 /* If it's an `=', then we have a constant-initializer or a
20926 pure-specifier. It is not correct to parse the
20927 initializer before registering the member declaration
20928 since the member declaration should be in scope while
20929 its initializer is processed. However, the rest of the
20930 front end does not yet provide an interface that allows
20931 us to handle this correctly. */
20932 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20934 /* In [class.mem]:
20936 A pure-specifier shall be used only in the declaration of
20937 a virtual function.
20939 A member-declarator can contain a constant-initializer
20940 only if it declares a static member of integral or
20941 enumeration type.
20943 Therefore, if the DECLARATOR is for a function, we look
20944 for a pure-specifier; otherwise, we look for a
20945 constant-initializer. When we call `grokfield', it will
20946 perform more stringent semantics checks. */
20947 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20948 if (function_declarator_p (declarator)
20949 || (decl_specifiers.type
20950 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20951 && declarator->kind == cdk_id
20952 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20953 == FUNCTION_TYPE)))
20954 initializer = cp_parser_pure_specifier (parser);
20955 else if (decl_specifiers.storage_class != sc_static)
20956 initializer = cp_parser_save_nsdmi (parser);
20957 else if (cxx_dialect >= cxx11)
20959 bool nonconst;
20960 /* Don't require a constant rvalue in C++11, since we
20961 might want a reference constant. We'll enforce
20962 constancy later. */
20963 cp_lexer_consume_token (parser->lexer);
20964 /* Parse the initializer. */
20965 initializer = cp_parser_initializer_clause (parser,
20966 &nonconst);
20968 else
20969 /* Parse the initializer. */
20970 initializer = cp_parser_constant_initializer (parser);
20972 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20973 && !function_declarator_p (declarator))
20975 bool x;
20976 if (decl_specifiers.storage_class != sc_static)
20977 initializer = cp_parser_save_nsdmi (parser);
20978 else
20979 initializer = cp_parser_initializer (parser, &x, &x);
20981 /* Otherwise, there is no initializer. */
20982 else
20983 initializer = NULL_TREE;
20985 /* See if we are probably looking at a function
20986 definition. We are certainly not looking at a
20987 member-declarator. Calling `grokfield' has
20988 side-effects, so we must not do it unless we are sure
20989 that we are looking at a member-declarator. */
20990 if (cp_parser_token_starts_function_definition_p
20991 (cp_lexer_peek_token (parser->lexer)))
20993 /* The grammar does not allow a pure-specifier to be
20994 used when a member function is defined. (It is
20995 possible that this fact is an oversight in the
20996 standard, since a pure function may be defined
20997 outside of the class-specifier. */
20998 if (initializer && initializer_token_start)
20999 error_at (initializer_token_start->location,
21000 "pure-specifier on function-definition");
21001 decl = cp_parser_save_member_function_body (parser,
21002 &decl_specifiers,
21003 declarator,
21004 attributes);
21005 if (parser->fully_implicit_function_template_p)
21006 decl = finish_fully_implicit_template (parser, decl);
21007 /* If the member was not a friend, declare it here. */
21008 if (!friend_p)
21009 finish_member_declaration (decl);
21010 /* Peek at the next token. */
21011 token = cp_lexer_peek_token (parser->lexer);
21012 /* If the next token is a semicolon, consume it. */
21013 if (token->type == CPP_SEMICOLON)
21014 cp_lexer_consume_token (parser->lexer);
21015 goto out;
21017 else
21018 if (declarator->kind == cdk_function)
21019 declarator->id_loc = token->location;
21020 /* Create the declaration. */
21021 decl = grokfield (declarator, &decl_specifiers,
21022 initializer, /*init_const_expr_p=*/true,
21023 asm_specification, attributes);
21024 if (parser->fully_implicit_function_template_p)
21026 if (friend_p)
21027 finish_fully_implicit_template (parser, 0);
21028 else
21029 decl = finish_fully_implicit_template (parser, decl);
21033 cp_finalize_omp_declare_simd (parser, decl);
21035 /* Reset PREFIX_ATTRIBUTES. */
21036 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21037 attributes = TREE_CHAIN (attributes);
21038 if (attributes)
21039 TREE_CHAIN (attributes) = NULL_TREE;
21041 /* If there is any qualification still in effect, clear it
21042 now; we will be starting fresh with the next declarator. */
21043 parser->scope = NULL_TREE;
21044 parser->qualifying_scope = NULL_TREE;
21045 parser->object_scope = NULL_TREE;
21046 /* If it's a `,', then there are more declarators. */
21047 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21049 cp_lexer_consume_token (parser->lexer);
21050 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21052 cp_token *token = cp_lexer_previous_token (parser->lexer);
21053 error_at (token->location,
21054 "stray %<,%> at end of member declaration");
21057 /* If the next token isn't a `;', then we have a parse error. */
21058 else if (cp_lexer_next_token_is_not (parser->lexer,
21059 CPP_SEMICOLON))
21061 /* The next token might be a ways away from where the
21062 actual semicolon is missing. Find the previous token
21063 and use that for our error position. */
21064 cp_token *token = cp_lexer_previous_token (parser->lexer);
21065 error_at (token->location,
21066 "expected %<;%> at end of member declaration");
21068 /* Assume that the user meant to provide a semicolon. If
21069 we were to cp_parser_skip_to_end_of_statement, we might
21070 skip to a semicolon inside a member function definition
21071 and issue nonsensical error messages. */
21072 assume_semicolon = true;
21075 if (decl)
21077 /* Add DECL to the list of members. */
21078 if (!friend_p
21079 /* Explicitly include, eg, NSDMIs, for better error
21080 recovery (c++/58650). */
21081 || !DECL_DECLARES_FUNCTION_P (decl))
21082 finish_member_declaration (decl);
21084 if (TREE_CODE (decl) == FUNCTION_DECL)
21085 cp_parser_save_default_args (parser, decl);
21086 else if (TREE_CODE (decl) == FIELD_DECL
21087 && !DECL_C_BIT_FIELD (decl)
21088 && DECL_INITIAL (decl))
21089 /* Add DECL to the queue of NSDMI to be parsed later. */
21090 vec_safe_push (unparsed_nsdmis, decl);
21093 if (assume_semicolon)
21094 goto out;
21098 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21099 out:
21100 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21103 /* Parse a pure-specifier.
21105 pure-specifier:
21108 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21109 Otherwise, ERROR_MARK_NODE is returned. */
21111 static tree
21112 cp_parser_pure_specifier (cp_parser* parser)
21114 cp_token *token;
21116 /* Look for the `=' token. */
21117 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21118 return error_mark_node;
21119 /* Look for the `0' token. */
21120 token = cp_lexer_peek_token (parser->lexer);
21122 if (token->type == CPP_EOF
21123 || token->type == CPP_PRAGMA_EOL)
21124 return error_mark_node;
21126 cp_lexer_consume_token (parser->lexer);
21128 /* Accept = default or = delete in c++0x mode. */
21129 if (token->keyword == RID_DEFAULT
21130 || token->keyword == RID_DELETE)
21132 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21133 return token->u.value;
21136 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21137 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21139 cp_parser_error (parser,
21140 "invalid pure specifier (only %<= 0%> is allowed)");
21141 cp_parser_skip_to_end_of_statement (parser);
21142 return error_mark_node;
21144 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21146 error_at (token->location, "templates may not be %<virtual%>");
21147 return error_mark_node;
21150 return integer_zero_node;
21153 /* Parse a constant-initializer.
21155 constant-initializer:
21156 = constant-expression
21158 Returns a representation of the constant-expression. */
21160 static tree
21161 cp_parser_constant_initializer (cp_parser* parser)
21163 /* Look for the `=' token. */
21164 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21165 return error_mark_node;
21167 /* It is invalid to write:
21169 struct S { static const int i = { 7 }; };
21172 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21174 cp_parser_error (parser,
21175 "a brace-enclosed initializer is not allowed here");
21176 /* Consume the opening brace. */
21177 cp_lexer_consume_token (parser->lexer);
21178 /* Skip the initializer. */
21179 cp_parser_skip_to_closing_brace (parser);
21180 /* Look for the trailing `}'. */
21181 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21183 return error_mark_node;
21186 return cp_parser_constant_expression (parser);
21189 /* Derived classes [gram.class.derived] */
21191 /* Parse a base-clause.
21193 base-clause:
21194 : base-specifier-list
21196 base-specifier-list:
21197 base-specifier ... [opt]
21198 base-specifier-list , base-specifier ... [opt]
21200 Returns a TREE_LIST representing the base-classes, in the order in
21201 which they were declared. The representation of each node is as
21202 described by cp_parser_base_specifier.
21204 In the case that no bases are specified, this function will return
21205 NULL_TREE, not ERROR_MARK_NODE. */
21207 static tree
21208 cp_parser_base_clause (cp_parser* parser)
21210 tree bases = NULL_TREE;
21212 /* Look for the `:' that begins the list. */
21213 cp_parser_require (parser, CPP_COLON, RT_COLON);
21215 /* Scan the base-specifier-list. */
21216 while (true)
21218 cp_token *token;
21219 tree base;
21220 bool pack_expansion_p = false;
21222 /* Look for the base-specifier. */
21223 base = cp_parser_base_specifier (parser);
21224 /* Look for the (optional) ellipsis. */
21225 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21227 /* Consume the `...'. */
21228 cp_lexer_consume_token (parser->lexer);
21230 pack_expansion_p = true;
21233 /* Add BASE to the front of the list. */
21234 if (base && base != error_mark_node)
21236 if (pack_expansion_p)
21237 /* Make this a pack expansion type. */
21238 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21240 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21242 TREE_CHAIN (base) = bases;
21243 bases = base;
21246 /* Peek at the next token. */
21247 token = cp_lexer_peek_token (parser->lexer);
21248 /* If it's not a comma, then the list is complete. */
21249 if (token->type != CPP_COMMA)
21250 break;
21251 /* Consume the `,'. */
21252 cp_lexer_consume_token (parser->lexer);
21255 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21256 base class had a qualified name. However, the next name that
21257 appears is certainly not qualified. */
21258 parser->scope = NULL_TREE;
21259 parser->qualifying_scope = NULL_TREE;
21260 parser->object_scope = NULL_TREE;
21262 return nreverse (bases);
21265 /* Parse a base-specifier.
21267 base-specifier:
21268 :: [opt] nested-name-specifier [opt] class-name
21269 virtual access-specifier [opt] :: [opt] nested-name-specifier
21270 [opt] class-name
21271 access-specifier virtual [opt] :: [opt] nested-name-specifier
21272 [opt] class-name
21274 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21275 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21276 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21277 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21279 static tree
21280 cp_parser_base_specifier (cp_parser* parser)
21282 cp_token *token;
21283 bool done = false;
21284 bool virtual_p = false;
21285 bool duplicate_virtual_error_issued_p = false;
21286 bool duplicate_access_error_issued_p = false;
21287 bool class_scope_p, template_p;
21288 tree access = access_default_node;
21289 tree type;
21291 /* Process the optional `virtual' and `access-specifier'. */
21292 while (!done)
21294 /* Peek at the next token. */
21295 token = cp_lexer_peek_token (parser->lexer);
21296 /* Process `virtual'. */
21297 switch (token->keyword)
21299 case RID_VIRTUAL:
21300 /* If `virtual' appears more than once, issue an error. */
21301 if (virtual_p && !duplicate_virtual_error_issued_p)
21303 cp_parser_error (parser,
21304 "%<virtual%> specified more than once in base-specified");
21305 duplicate_virtual_error_issued_p = true;
21308 virtual_p = true;
21310 /* Consume the `virtual' token. */
21311 cp_lexer_consume_token (parser->lexer);
21313 break;
21315 case RID_PUBLIC:
21316 case RID_PROTECTED:
21317 case RID_PRIVATE:
21318 /* If more than one access specifier appears, issue an
21319 error. */
21320 if (access != access_default_node
21321 && !duplicate_access_error_issued_p)
21323 cp_parser_error (parser,
21324 "more than one access specifier in base-specified");
21325 duplicate_access_error_issued_p = true;
21328 access = ridpointers[(int) token->keyword];
21330 /* Consume the access-specifier. */
21331 cp_lexer_consume_token (parser->lexer);
21333 break;
21335 default:
21336 done = true;
21337 break;
21340 /* It is not uncommon to see programs mechanically, erroneously, use
21341 the 'typename' keyword to denote (dependent) qualified types
21342 as base classes. */
21343 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21345 token = cp_lexer_peek_token (parser->lexer);
21346 if (!processing_template_decl)
21347 error_at (token->location,
21348 "keyword %<typename%> not allowed outside of templates");
21349 else
21350 error_at (token->location,
21351 "keyword %<typename%> not allowed in this context "
21352 "(the base class is implicitly a type)");
21353 cp_lexer_consume_token (parser->lexer);
21356 /* Look for the optional `::' operator. */
21357 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21358 /* Look for the nested-name-specifier. The simplest way to
21359 implement:
21361 [temp.res]
21363 The keyword `typename' is not permitted in a base-specifier or
21364 mem-initializer; in these contexts a qualified name that
21365 depends on a template-parameter is implicitly assumed to be a
21366 type name.
21368 is to pretend that we have seen the `typename' keyword at this
21369 point. */
21370 cp_parser_nested_name_specifier_opt (parser,
21371 /*typename_keyword_p=*/true,
21372 /*check_dependency_p=*/true,
21373 typename_type,
21374 /*is_declaration=*/true);
21375 /* If the base class is given by a qualified name, assume that names
21376 we see are type names or templates, as appropriate. */
21377 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21378 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21380 if (!parser->scope
21381 && cp_lexer_next_token_is_decltype (parser->lexer))
21382 /* DR 950 allows decltype as a base-specifier. */
21383 type = cp_parser_decltype (parser);
21384 else
21386 /* Otherwise, look for the class-name. */
21387 type = cp_parser_class_name (parser,
21388 class_scope_p,
21389 template_p,
21390 typename_type,
21391 /*check_dependency_p=*/true,
21392 /*class_head_p=*/false,
21393 /*is_declaration=*/true);
21394 type = TREE_TYPE (type);
21397 if (type == error_mark_node)
21398 return error_mark_node;
21400 return finish_base_specifier (type, access, virtual_p);
21403 /* Exception handling [gram.exception] */
21405 /* Parse an (optional) noexcept-specification.
21407 noexcept-specification:
21408 noexcept ( constant-expression ) [opt]
21410 If no noexcept-specification is present, returns NULL_TREE.
21411 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21412 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21413 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21414 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21415 in which case a boolean condition is returned instead. */
21417 static tree
21418 cp_parser_noexcept_specification_opt (cp_parser* parser,
21419 bool require_constexpr,
21420 bool* consumed_expr,
21421 bool return_cond)
21423 cp_token *token;
21424 const char *saved_message;
21426 /* Peek at the next token. */
21427 token = cp_lexer_peek_token (parser->lexer);
21429 /* Is it a noexcept-specification? */
21430 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21432 tree expr;
21433 cp_lexer_consume_token (parser->lexer);
21435 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21437 cp_lexer_consume_token (parser->lexer);
21439 if (require_constexpr)
21441 /* Types may not be defined in an exception-specification. */
21442 saved_message = parser->type_definition_forbidden_message;
21443 parser->type_definition_forbidden_message
21444 = G_("types may not be defined in an exception-specification");
21446 expr = cp_parser_constant_expression (parser);
21448 /* Restore the saved message. */
21449 parser->type_definition_forbidden_message = saved_message;
21451 else
21453 expr = cp_parser_expression (parser);
21454 *consumed_expr = true;
21457 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21459 else
21461 expr = boolean_true_node;
21462 if (!require_constexpr)
21463 *consumed_expr = false;
21466 /* We cannot build a noexcept-spec right away because this will check
21467 that expr is a constexpr. */
21468 if (!return_cond)
21469 return build_noexcept_spec (expr, tf_warning_or_error);
21470 else
21471 return expr;
21473 else
21474 return NULL_TREE;
21477 /* Parse an (optional) exception-specification.
21479 exception-specification:
21480 throw ( type-id-list [opt] )
21482 Returns a TREE_LIST representing the exception-specification. The
21483 TREE_VALUE of each node is a type. */
21485 static tree
21486 cp_parser_exception_specification_opt (cp_parser* parser)
21488 cp_token *token;
21489 tree type_id_list;
21490 const char *saved_message;
21492 /* Peek at the next token. */
21493 token = cp_lexer_peek_token (parser->lexer);
21495 /* Is it a noexcept-specification? */
21496 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21497 false);
21498 if (type_id_list != NULL_TREE)
21499 return type_id_list;
21501 /* If it's not `throw', then there's no exception-specification. */
21502 if (!cp_parser_is_keyword (token, RID_THROW))
21503 return NULL_TREE;
21505 #if 0
21506 /* Enable this once a lot of code has transitioned to noexcept? */
21507 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21508 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21509 "deprecated in C++0x; use %<noexcept%> instead");
21510 #endif
21512 /* Consume the `throw'. */
21513 cp_lexer_consume_token (parser->lexer);
21515 /* Look for the `('. */
21516 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21518 /* Peek at the next token. */
21519 token = cp_lexer_peek_token (parser->lexer);
21520 /* If it's not a `)', then there is a type-id-list. */
21521 if (token->type != CPP_CLOSE_PAREN)
21523 /* Types may not be defined in an exception-specification. */
21524 saved_message = parser->type_definition_forbidden_message;
21525 parser->type_definition_forbidden_message
21526 = G_("types may not be defined in an exception-specification");
21527 /* Parse the type-id-list. */
21528 type_id_list = cp_parser_type_id_list (parser);
21529 /* Restore the saved message. */
21530 parser->type_definition_forbidden_message = saved_message;
21532 else
21533 type_id_list = empty_except_spec;
21535 /* Look for the `)'. */
21536 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21538 return type_id_list;
21541 /* Parse an (optional) type-id-list.
21543 type-id-list:
21544 type-id ... [opt]
21545 type-id-list , type-id ... [opt]
21547 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21548 in the order that the types were presented. */
21550 static tree
21551 cp_parser_type_id_list (cp_parser* parser)
21553 tree types = NULL_TREE;
21555 while (true)
21557 cp_token *token;
21558 tree type;
21560 /* Get the next type-id. */
21561 type = cp_parser_type_id (parser);
21562 /* Parse the optional ellipsis. */
21563 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21565 /* Consume the `...'. */
21566 cp_lexer_consume_token (parser->lexer);
21568 /* Turn the type into a pack expansion expression. */
21569 type = make_pack_expansion (type);
21571 /* Add it to the list. */
21572 types = add_exception_specifier (types, type, /*complain=*/1);
21573 /* Peek at the next token. */
21574 token = cp_lexer_peek_token (parser->lexer);
21575 /* If it is not a `,', we are done. */
21576 if (token->type != CPP_COMMA)
21577 break;
21578 /* Consume the `,'. */
21579 cp_lexer_consume_token (parser->lexer);
21582 return nreverse (types);
21585 /* Parse a try-block.
21587 try-block:
21588 try compound-statement handler-seq */
21590 static tree
21591 cp_parser_try_block (cp_parser* parser)
21593 tree try_block;
21595 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21596 if (parser->in_function_body
21597 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21598 error ("%<try%> in %<constexpr%> function");
21600 try_block = begin_try_block ();
21601 cp_parser_compound_statement (parser, NULL, true, false);
21602 finish_try_block (try_block);
21603 cp_parser_handler_seq (parser);
21604 finish_handler_sequence (try_block);
21606 return try_block;
21609 /* Parse a function-try-block.
21611 function-try-block:
21612 try ctor-initializer [opt] function-body handler-seq */
21614 static bool
21615 cp_parser_function_try_block (cp_parser* parser)
21617 tree compound_stmt;
21618 tree try_block;
21619 bool ctor_initializer_p;
21621 /* Look for the `try' keyword. */
21622 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21623 return false;
21624 /* Let the rest of the front end know where we are. */
21625 try_block = begin_function_try_block (&compound_stmt);
21626 /* Parse the function-body. */
21627 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21628 (parser, /*in_function_try_block=*/true);
21629 /* We're done with the `try' part. */
21630 finish_function_try_block (try_block);
21631 /* Parse the handlers. */
21632 cp_parser_handler_seq (parser);
21633 /* We're done with the handlers. */
21634 finish_function_handler_sequence (try_block, compound_stmt);
21636 return ctor_initializer_p;
21639 /* Parse a handler-seq.
21641 handler-seq:
21642 handler handler-seq [opt] */
21644 static void
21645 cp_parser_handler_seq (cp_parser* parser)
21647 while (true)
21649 cp_token *token;
21651 /* Parse the handler. */
21652 cp_parser_handler (parser);
21653 /* Peek at the next token. */
21654 token = cp_lexer_peek_token (parser->lexer);
21655 /* If it's not `catch' then there are no more handlers. */
21656 if (!cp_parser_is_keyword (token, RID_CATCH))
21657 break;
21661 /* Parse a handler.
21663 handler:
21664 catch ( exception-declaration ) compound-statement */
21666 static void
21667 cp_parser_handler (cp_parser* parser)
21669 tree handler;
21670 tree declaration;
21672 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21673 handler = begin_handler ();
21674 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21675 declaration = cp_parser_exception_declaration (parser);
21676 finish_handler_parms (declaration, handler);
21677 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21678 cp_parser_compound_statement (parser, NULL, false, false);
21679 finish_handler (handler);
21682 /* Parse an exception-declaration.
21684 exception-declaration:
21685 type-specifier-seq declarator
21686 type-specifier-seq abstract-declarator
21687 type-specifier-seq
21690 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21691 ellipsis variant is used. */
21693 static tree
21694 cp_parser_exception_declaration (cp_parser* parser)
21696 cp_decl_specifier_seq type_specifiers;
21697 cp_declarator *declarator;
21698 const char *saved_message;
21700 /* If it's an ellipsis, it's easy to handle. */
21701 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21703 /* Consume the `...' token. */
21704 cp_lexer_consume_token (parser->lexer);
21705 return NULL_TREE;
21708 /* Types may not be defined in exception-declarations. */
21709 saved_message = parser->type_definition_forbidden_message;
21710 parser->type_definition_forbidden_message
21711 = G_("types may not be defined in exception-declarations");
21713 /* Parse the type-specifier-seq. */
21714 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21715 /*is_trailing_return=*/false,
21716 &type_specifiers);
21717 /* If it's a `)', then there is no declarator. */
21718 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21719 declarator = NULL;
21720 else
21721 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21722 /*ctor_dtor_or_conv_p=*/NULL,
21723 /*parenthesized_p=*/NULL,
21724 /*member_p=*/false,
21725 /*friend_p=*/false);
21727 /* Restore the saved message. */
21728 parser->type_definition_forbidden_message = saved_message;
21730 if (!type_specifiers.any_specifiers_p)
21731 return error_mark_node;
21733 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21736 /* Parse a throw-expression.
21738 throw-expression:
21739 throw assignment-expression [opt]
21741 Returns a THROW_EXPR representing the throw-expression. */
21743 static tree
21744 cp_parser_throw_expression (cp_parser* parser)
21746 tree expression;
21747 cp_token* token;
21749 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21750 token = cp_lexer_peek_token (parser->lexer);
21751 /* Figure out whether or not there is an assignment-expression
21752 following the "throw" keyword. */
21753 if (token->type == CPP_COMMA
21754 || token->type == CPP_SEMICOLON
21755 || token->type == CPP_CLOSE_PAREN
21756 || token->type == CPP_CLOSE_SQUARE
21757 || token->type == CPP_CLOSE_BRACE
21758 || token->type == CPP_COLON)
21759 expression = NULL_TREE;
21760 else
21761 expression = cp_parser_assignment_expression (parser);
21763 return build_throw (expression);
21766 /* GNU Extensions */
21768 /* Parse an (optional) asm-specification.
21770 asm-specification:
21771 asm ( string-literal )
21773 If the asm-specification is present, returns a STRING_CST
21774 corresponding to the string-literal. Otherwise, returns
21775 NULL_TREE. */
21777 static tree
21778 cp_parser_asm_specification_opt (cp_parser* parser)
21780 cp_token *token;
21781 tree asm_specification;
21783 /* Peek at the next token. */
21784 token = cp_lexer_peek_token (parser->lexer);
21785 /* If the next token isn't the `asm' keyword, then there's no
21786 asm-specification. */
21787 if (!cp_parser_is_keyword (token, RID_ASM))
21788 return NULL_TREE;
21790 /* Consume the `asm' token. */
21791 cp_lexer_consume_token (parser->lexer);
21792 /* Look for the `('. */
21793 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21795 /* Look for the string-literal. */
21796 asm_specification = cp_parser_string_literal (parser, false, false);
21798 /* Look for the `)'. */
21799 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21801 return asm_specification;
21804 /* Parse an asm-operand-list.
21806 asm-operand-list:
21807 asm-operand
21808 asm-operand-list , asm-operand
21810 asm-operand:
21811 string-literal ( expression )
21812 [ string-literal ] string-literal ( expression )
21814 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21815 each node is the expression. The TREE_PURPOSE is itself a
21816 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21817 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21818 is a STRING_CST for the string literal before the parenthesis. Returns
21819 ERROR_MARK_NODE if any of the operands are invalid. */
21821 static tree
21822 cp_parser_asm_operand_list (cp_parser* parser)
21824 tree asm_operands = NULL_TREE;
21825 bool invalid_operands = false;
21827 while (true)
21829 tree string_literal;
21830 tree expression;
21831 tree name;
21833 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21835 /* Consume the `[' token. */
21836 cp_lexer_consume_token (parser->lexer);
21837 /* Read the operand name. */
21838 name = cp_parser_identifier (parser);
21839 if (name != error_mark_node)
21840 name = build_string (IDENTIFIER_LENGTH (name),
21841 IDENTIFIER_POINTER (name));
21842 /* Look for the closing `]'. */
21843 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21845 else
21846 name = NULL_TREE;
21847 /* Look for the string-literal. */
21848 string_literal = cp_parser_string_literal (parser, false, false);
21850 /* Look for the `('. */
21851 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21852 /* Parse the expression. */
21853 expression = cp_parser_expression (parser);
21854 /* Look for the `)'. */
21855 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21857 if (name == error_mark_node
21858 || string_literal == error_mark_node
21859 || expression == error_mark_node)
21860 invalid_operands = true;
21862 /* Add this operand to the list. */
21863 asm_operands = tree_cons (build_tree_list (name, string_literal),
21864 expression,
21865 asm_operands);
21866 /* If the next token is not a `,', there are no more
21867 operands. */
21868 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21869 break;
21870 /* Consume the `,'. */
21871 cp_lexer_consume_token (parser->lexer);
21874 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21877 /* Parse an asm-clobber-list.
21879 asm-clobber-list:
21880 string-literal
21881 asm-clobber-list , string-literal
21883 Returns a TREE_LIST, indicating the clobbers in the order that they
21884 appeared. The TREE_VALUE of each node is a STRING_CST. */
21886 static tree
21887 cp_parser_asm_clobber_list (cp_parser* parser)
21889 tree clobbers = NULL_TREE;
21891 while (true)
21893 tree string_literal;
21895 /* Look for the string literal. */
21896 string_literal = cp_parser_string_literal (parser, false, false);
21897 /* Add it to the list. */
21898 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21899 /* If the next token is not a `,', then the list is
21900 complete. */
21901 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21902 break;
21903 /* Consume the `,' token. */
21904 cp_lexer_consume_token (parser->lexer);
21907 return clobbers;
21910 /* Parse an asm-label-list.
21912 asm-label-list:
21913 identifier
21914 asm-label-list , identifier
21916 Returns a TREE_LIST, indicating the labels in the order that they
21917 appeared. The TREE_VALUE of each node is a label. */
21919 static tree
21920 cp_parser_asm_label_list (cp_parser* parser)
21922 tree labels = NULL_TREE;
21924 while (true)
21926 tree identifier, label, name;
21928 /* Look for the identifier. */
21929 identifier = cp_parser_identifier (parser);
21930 if (!error_operand_p (identifier))
21932 label = lookup_label (identifier);
21933 if (TREE_CODE (label) == LABEL_DECL)
21935 TREE_USED (label) = 1;
21936 check_goto (label);
21937 name = build_string (IDENTIFIER_LENGTH (identifier),
21938 IDENTIFIER_POINTER (identifier));
21939 labels = tree_cons (name, label, labels);
21942 /* If the next token is not a `,', then the list is
21943 complete. */
21944 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21945 break;
21946 /* Consume the `,' token. */
21947 cp_lexer_consume_token (parser->lexer);
21950 return nreverse (labels);
21953 /* Return TRUE iff the next tokens in the stream are possibly the
21954 beginning of a GNU extension attribute. */
21956 static bool
21957 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21959 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21962 /* Return TRUE iff the next tokens in the stream are possibly the
21963 beginning of a standard C++-11 attribute specifier. */
21965 static bool
21966 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21968 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21971 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21972 beginning of a standard C++-11 attribute specifier. */
21974 static bool
21975 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21977 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21979 return (cxx_dialect >= cxx11
21980 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21981 || (token->type == CPP_OPEN_SQUARE
21982 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21983 && token->type == CPP_OPEN_SQUARE)));
21986 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21987 beginning of a GNU extension attribute. */
21989 static bool
21990 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21992 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21994 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21997 /* Return true iff the next tokens can be the beginning of either a
21998 GNU attribute list, or a standard C++11 attribute sequence. */
22000 static bool
22001 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22003 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22004 || cp_next_tokens_can_be_std_attribute_p (parser));
22007 /* Return true iff the next Nth tokens can be the beginning of either
22008 a GNU attribute list, or a standard C++11 attribute sequence. */
22010 static bool
22011 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22013 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22014 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22017 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22018 of GNU attributes, or return NULL. */
22020 static tree
22021 cp_parser_attributes_opt (cp_parser *parser)
22023 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22024 return cp_parser_gnu_attributes_opt (parser);
22025 return cp_parser_std_attribute_spec_seq (parser);
22028 #define CILK_SIMD_FN_CLAUSE_MASK \
22029 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22030 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22031 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22032 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22033 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22035 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22036 vector [(<clauses>)] */
22038 static void
22039 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22041 bool first_p = parser->cilk_simd_fn_info == NULL;
22042 cp_token *token = v_token;
22043 if (first_p)
22045 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22046 parser->cilk_simd_fn_info->error_seen = false;
22047 parser->cilk_simd_fn_info->fndecl_seen = false;
22048 parser->cilk_simd_fn_info->tokens = vNULL;
22050 int paren_scope = 0;
22051 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22053 cp_lexer_consume_token (parser->lexer);
22054 v_token = cp_lexer_peek_token (parser->lexer);
22055 paren_scope++;
22057 while (paren_scope > 0)
22059 token = cp_lexer_peek_token (parser->lexer);
22060 if (token->type == CPP_OPEN_PAREN)
22061 paren_scope++;
22062 else if (token->type == CPP_CLOSE_PAREN)
22063 paren_scope--;
22064 /* Do not push the last ')' */
22065 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22066 cp_lexer_consume_token (parser->lexer);
22069 token->type = CPP_PRAGMA_EOL;
22070 parser->lexer->next_token = token;
22071 cp_lexer_consume_token (parser->lexer);
22073 struct cp_token_cache *cp
22074 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22075 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22078 /* Parse an (optional) series of attributes.
22080 attributes:
22081 attributes attribute
22083 attribute:
22084 __attribute__ (( attribute-list [opt] ))
22086 The return value is as for cp_parser_gnu_attribute_list. */
22088 static tree
22089 cp_parser_gnu_attributes_opt (cp_parser* parser)
22091 tree attributes = NULL_TREE;
22093 while (true)
22095 cp_token *token;
22096 tree attribute_list;
22097 bool ok = true;
22099 /* Peek at the next token. */
22100 token = cp_lexer_peek_token (parser->lexer);
22101 /* If it's not `__attribute__', then we're done. */
22102 if (token->keyword != RID_ATTRIBUTE)
22103 break;
22105 /* Consume the `__attribute__' keyword. */
22106 cp_lexer_consume_token (parser->lexer);
22107 /* Look for the two `(' tokens. */
22108 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22109 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22111 /* Peek at the next token. */
22112 token = cp_lexer_peek_token (parser->lexer);
22113 if (token->type != CPP_CLOSE_PAREN)
22114 /* Parse the attribute-list. */
22115 attribute_list = cp_parser_gnu_attribute_list (parser);
22116 else
22117 /* If the next token is a `)', then there is no attribute
22118 list. */
22119 attribute_list = NULL;
22121 /* Look for the two `)' tokens. */
22122 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22123 ok = false;
22124 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22125 ok = false;
22126 if (!ok)
22127 cp_parser_skip_to_end_of_statement (parser);
22129 /* Add these new attributes to the list. */
22130 attributes = chainon (attributes, attribute_list);
22133 return attributes;
22136 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22137 "__vector" or "__vector__." */
22139 static inline bool
22140 is_cilkplus_vector_p (tree name)
22142 if (flag_cilkplus && is_attribute_p ("vector", name))
22143 return true;
22144 return false;
22147 /* Parse a GNU attribute-list.
22149 attribute-list:
22150 attribute
22151 attribute-list , attribute
22153 attribute:
22154 identifier
22155 identifier ( identifier )
22156 identifier ( identifier , expression-list )
22157 identifier ( expression-list )
22159 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22160 to an attribute. The TREE_PURPOSE of each node is the identifier
22161 indicating which attribute is in use. The TREE_VALUE represents
22162 the arguments, if any. */
22164 static tree
22165 cp_parser_gnu_attribute_list (cp_parser* parser)
22167 tree attribute_list = NULL_TREE;
22168 bool save_translate_strings_p = parser->translate_strings_p;
22170 parser->translate_strings_p = false;
22171 while (true)
22173 cp_token *token;
22174 tree identifier;
22175 tree attribute;
22177 /* Look for the identifier. We also allow keywords here; for
22178 example `__attribute__ ((const))' is legal. */
22179 token = cp_lexer_peek_token (parser->lexer);
22180 if (token->type == CPP_NAME
22181 || token->type == CPP_KEYWORD)
22183 tree arguments = NULL_TREE;
22185 /* Consume the token, but save it since we need it for the
22186 SIMD enabled function parsing. */
22187 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22189 /* Save away the identifier that indicates which attribute
22190 this is. */
22191 identifier = (token->type == CPP_KEYWORD)
22192 /* For keywords, use the canonical spelling, not the
22193 parsed identifier. */
22194 ? ridpointers[(int) token->keyword]
22195 : id_token->u.value;
22197 attribute = build_tree_list (identifier, NULL_TREE);
22199 /* Peek at the next token. */
22200 token = cp_lexer_peek_token (parser->lexer);
22201 /* If it's an `(', then parse the attribute arguments. */
22202 if (token->type == CPP_OPEN_PAREN)
22204 vec<tree, va_gc> *vec;
22205 int attr_flag = (attribute_takes_identifier_p (identifier)
22206 ? id_attr : normal_attr);
22207 if (is_cilkplus_vector_p (identifier))
22209 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22210 continue;
22212 else
22213 vec = cp_parser_parenthesized_expression_list
22214 (parser, attr_flag, /*cast_p=*/false,
22215 /*allow_expansion_p=*/false,
22216 /*non_constant_p=*/NULL);
22217 if (vec == NULL)
22218 arguments = error_mark_node;
22219 else
22221 arguments = build_tree_list_vec (vec);
22222 release_tree_vector (vec);
22224 /* Save the arguments away. */
22225 TREE_VALUE (attribute) = arguments;
22227 else if (is_cilkplus_vector_p (identifier))
22229 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22230 continue;
22233 if (arguments != error_mark_node)
22235 /* Add this attribute to the list. */
22236 TREE_CHAIN (attribute) = attribute_list;
22237 attribute_list = attribute;
22240 token = cp_lexer_peek_token (parser->lexer);
22242 /* Now, look for more attributes. If the next token isn't a
22243 `,', we're done. */
22244 if (token->type != CPP_COMMA)
22245 break;
22247 /* Consume the comma and keep going. */
22248 cp_lexer_consume_token (parser->lexer);
22250 parser->translate_strings_p = save_translate_strings_p;
22252 /* We built up the list in reverse order. */
22253 return nreverse (attribute_list);
22256 /* Parse a standard C++11 attribute.
22258 The returned representation is a TREE_LIST which TREE_PURPOSE is
22259 the scoped name of the attribute, and the TREE_VALUE is its
22260 arguments list.
22262 Note that the scoped name of the attribute is itself a TREE_LIST
22263 which TREE_PURPOSE is the namespace of the attribute, and
22264 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22265 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22266 and which TREE_PURPOSE is directly the attribute name.
22268 Clients of the attribute code should use get_attribute_namespace
22269 and get_attribute_name to get the actual namespace and name of
22270 attributes, regardless of their being GNU or C++11 attributes.
22272 attribute:
22273 attribute-token attribute-argument-clause [opt]
22275 attribute-token:
22276 identifier
22277 attribute-scoped-token
22279 attribute-scoped-token:
22280 attribute-namespace :: identifier
22282 attribute-namespace:
22283 identifier
22285 attribute-argument-clause:
22286 ( balanced-token-seq )
22288 balanced-token-seq:
22289 balanced-token [opt]
22290 balanced-token-seq balanced-token
22292 balanced-token:
22293 ( balanced-token-seq )
22294 [ balanced-token-seq ]
22295 { balanced-token-seq }. */
22297 static tree
22298 cp_parser_std_attribute (cp_parser *parser)
22300 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22301 cp_token *token;
22303 /* First, parse name of the the attribute, a.k.a
22304 attribute-token. */
22306 token = cp_lexer_peek_token (parser->lexer);
22307 if (token->type == CPP_NAME)
22308 attr_id = token->u.value;
22309 else if (token->type == CPP_KEYWORD)
22310 attr_id = ridpointers[(int) token->keyword];
22311 else if (token->flags & NAMED_OP)
22312 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22314 if (attr_id == NULL_TREE)
22315 return NULL_TREE;
22317 cp_lexer_consume_token (parser->lexer);
22319 token = cp_lexer_peek_token (parser->lexer);
22320 if (token->type == CPP_SCOPE)
22322 /* We are seeing a scoped attribute token. */
22324 cp_lexer_consume_token (parser->lexer);
22325 attr_ns = attr_id;
22327 token = cp_lexer_consume_token (parser->lexer);
22328 if (token->type == CPP_NAME)
22329 attr_id = token->u.value;
22330 else if (token->type == CPP_KEYWORD)
22331 attr_id = ridpointers[(int) token->keyword];
22332 else
22334 error_at (token->location,
22335 "expected an identifier for the attribute name");
22336 return error_mark_node;
22338 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22339 NULL_TREE);
22340 token = cp_lexer_peek_token (parser->lexer);
22342 else
22344 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22345 NULL_TREE);
22346 /* C++11 noreturn attribute is equivalent to GNU's. */
22347 if (is_attribute_p ("noreturn", attr_id))
22348 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22349 /* C++14 deprecated attribute is equivalent to GNU's. */
22350 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22352 if (cxx_dialect == cxx11)
22353 pedwarn (token->location, OPT_Wpedantic,
22354 "%<deprecated%> is a C++14 feature;"
22355 " use %<gnu::deprecated%>");
22356 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22360 /* Now parse the optional argument clause of the attribute. */
22362 if (token->type != CPP_OPEN_PAREN)
22363 return attribute;
22366 vec<tree, va_gc> *vec;
22367 int attr_flag = normal_attr;
22369 if (attr_ns == get_identifier ("gnu")
22370 && attribute_takes_identifier_p (attr_id))
22371 /* A GNU attribute that takes an identifier in parameter. */
22372 attr_flag = id_attr;
22374 vec = cp_parser_parenthesized_expression_list
22375 (parser, attr_flag, /*cast_p=*/false,
22376 /*allow_expansion_p=*/true,
22377 /*non_constant_p=*/NULL);
22378 if (vec == NULL)
22379 arguments = error_mark_node;
22380 else
22382 arguments = build_tree_list_vec (vec);
22383 release_tree_vector (vec);
22386 if (arguments == error_mark_node)
22387 attribute = error_mark_node;
22388 else
22389 TREE_VALUE (attribute) = arguments;
22392 return attribute;
22395 /* Parse a list of standard C++-11 attributes.
22397 attribute-list:
22398 attribute [opt]
22399 attribute-list , attribute[opt]
22400 attribute ...
22401 attribute-list , attribute ...
22404 static tree
22405 cp_parser_std_attribute_list (cp_parser *parser)
22407 tree attributes = NULL_TREE, attribute = NULL_TREE;
22408 cp_token *token = NULL;
22410 while (true)
22412 attribute = cp_parser_std_attribute (parser);
22413 if (attribute == error_mark_node)
22414 break;
22415 if (attribute != NULL_TREE)
22417 TREE_CHAIN (attribute) = attributes;
22418 attributes = attribute;
22420 token = cp_lexer_peek_token (parser->lexer);
22421 if (token->type != CPP_COMMA)
22422 break;
22423 cp_lexer_consume_token (parser->lexer);
22425 attributes = nreverse (attributes);
22426 return attributes;
22429 /* Parse a standard C++-11 attribute specifier.
22431 attribute-specifier:
22432 [ [ attribute-list ] ]
22433 alignment-specifier
22435 alignment-specifier:
22436 alignas ( type-id ... [opt] )
22437 alignas ( alignment-expression ... [opt] ). */
22439 static tree
22440 cp_parser_std_attribute_spec (cp_parser *parser)
22442 tree attributes = NULL_TREE;
22443 cp_token *token = cp_lexer_peek_token (parser->lexer);
22445 if (token->type == CPP_OPEN_SQUARE
22446 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22448 cp_lexer_consume_token (parser->lexer);
22449 cp_lexer_consume_token (parser->lexer);
22451 attributes = cp_parser_std_attribute_list (parser);
22453 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22454 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22455 cp_parser_skip_to_end_of_statement (parser);
22456 else
22457 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22458 when we are sure that we have actually parsed them. */
22459 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22461 else
22463 tree alignas_expr;
22465 /* Look for an alignment-specifier. */
22467 token = cp_lexer_peek_token (parser->lexer);
22469 if (token->type != CPP_KEYWORD
22470 || token->keyword != RID_ALIGNAS)
22471 return NULL_TREE;
22473 cp_lexer_consume_token (parser->lexer);
22474 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22476 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22478 cp_parser_error (parser, "expected %<(%>");
22479 return error_mark_node;
22482 cp_parser_parse_tentatively (parser);
22483 alignas_expr = cp_parser_type_id (parser);
22485 if (!cp_parser_parse_definitely (parser))
22487 gcc_assert (alignas_expr == error_mark_node
22488 || alignas_expr == NULL_TREE);
22490 alignas_expr =
22491 cp_parser_assignment_expression (parser);
22492 if (alignas_expr == error_mark_node)
22493 cp_parser_skip_to_end_of_statement (parser);
22494 if (alignas_expr == NULL_TREE
22495 || alignas_expr == error_mark_node)
22496 return alignas_expr;
22499 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22501 cp_parser_error (parser, "expected %<)%>");
22502 return error_mark_node;
22505 alignas_expr = cxx_alignas_expr (alignas_expr);
22507 /* Build the C++-11 representation of an 'aligned'
22508 attribute. */
22509 attributes =
22510 build_tree_list (build_tree_list (get_identifier ("gnu"),
22511 get_identifier ("aligned")),
22512 build_tree_list (NULL_TREE, alignas_expr));
22515 return attributes;
22518 /* Parse a standard C++-11 attribute-specifier-seq.
22520 attribute-specifier-seq:
22521 attribute-specifier-seq [opt] attribute-specifier
22524 static tree
22525 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22527 tree attr_specs = NULL;
22529 while (true)
22531 tree attr_spec = cp_parser_std_attribute_spec (parser);
22532 if (attr_spec == NULL_TREE)
22533 break;
22534 if (attr_spec == error_mark_node)
22535 return error_mark_node;
22537 TREE_CHAIN (attr_spec) = attr_specs;
22538 attr_specs = attr_spec;
22541 attr_specs = nreverse (attr_specs);
22542 return attr_specs;
22545 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22546 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22547 current value of the PEDANTIC flag, regardless of whether or not
22548 the `__extension__' keyword is present. The caller is responsible
22549 for restoring the value of the PEDANTIC flag. */
22551 static bool
22552 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22554 /* Save the old value of the PEDANTIC flag. */
22555 *saved_pedantic = pedantic;
22557 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22559 /* Consume the `__extension__' token. */
22560 cp_lexer_consume_token (parser->lexer);
22561 /* We're not being pedantic while the `__extension__' keyword is
22562 in effect. */
22563 pedantic = 0;
22565 return true;
22568 return false;
22571 /* Parse a label declaration.
22573 label-declaration:
22574 __label__ label-declarator-seq ;
22576 label-declarator-seq:
22577 identifier , label-declarator-seq
22578 identifier */
22580 static void
22581 cp_parser_label_declaration (cp_parser* parser)
22583 /* Look for the `__label__' keyword. */
22584 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22586 while (true)
22588 tree identifier;
22590 /* Look for an identifier. */
22591 identifier = cp_parser_identifier (parser);
22592 /* If we failed, stop. */
22593 if (identifier == error_mark_node)
22594 break;
22595 /* Declare it as a label. */
22596 finish_label_decl (identifier);
22597 /* If the next token is a `;', stop. */
22598 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22599 break;
22600 /* Look for the `,' separating the label declarations. */
22601 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22604 /* Look for the final `;'. */
22605 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22608 /* Support Functions */
22610 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22611 NAME should have one of the representations used for an
22612 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22613 is returned. If PARSER->SCOPE is a dependent type, then a
22614 SCOPE_REF is returned.
22616 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22617 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22618 was formed. Abstractly, such entities should not be passed to this
22619 function, because they do not need to be looked up, but it is
22620 simpler to check for this special case here, rather than at the
22621 call-sites.
22623 In cases not explicitly covered above, this function returns a
22624 DECL, OVERLOAD, or baselink representing the result of the lookup.
22625 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22626 is returned.
22628 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22629 (e.g., "struct") that was used. In that case bindings that do not
22630 refer to types are ignored.
22632 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22633 ignored.
22635 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22636 are ignored.
22638 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22639 types.
22641 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22642 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22643 NULL_TREE otherwise. */
22645 static tree
22646 cp_parser_lookup_name (cp_parser *parser, tree name,
22647 enum tag_types tag_type,
22648 bool is_template,
22649 bool is_namespace,
22650 bool check_dependency,
22651 tree *ambiguous_decls,
22652 location_t name_location)
22654 tree decl;
22655 tree object_type = parser->context->object_type;
22657 /* Assume that the lookup will be unambiguous. */
22658 if (ambiguous_decls)
22659 *ambiguous_decls = NULL_TREE;
22661 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22662 no longer valid. Note that if we are parsing tentatively, and
22663 the parse fails, OBJECT_TYPE will be automatically restored. */
22664 parser->context->object_type = NULL_TREE;
22666 if (name == error_mark_node)
22667 return error_mark_node;
22669 /* A template-id has already been resolved; there is no lookup to
22670 do. */
22671 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22672 return name;
22673 if (BASELINK_P (name))
22675 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22676 == TEMPLATE_ID_EXPR);
22677 return name;
22680 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22681 it should already have been checked to make sure that the name
22682 used matches the type being destroyed. */
22683 if (TREE_CODE (name) == BIT_NOT_EXPR)
22685 tree type;
22687 /* Figure out to which type this destructor applies. */
22688 if (parser->scope)
22689 type = parser->scope;
22690 else if (object_type)
22691 type = object_type;
22692 else
22693 type = current_class_type;
22694 /* If that's not a class type, there is no destructor. */
22695 if (!type || !CLASS_TYPE_P (type))
22696 return error_mark_node;
22697 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22698 lazily_declare_fn (sfk_destructor, type);
22699 if (!CLASSTYPE_DESTRUCTORS (type))
22700 return error_mark_node;
22701 /* If it was a class type, return the destructor. */
22702 return CLASSTYPE_DESTRUCTORS (type);
22705 /* By this point, the NAME should be an ordinary identifier. If
22706 the id-expression was a qualified name, the qualifying scope is
22707 stored in PARSER->SCOPE at this point. */
22708 gcc_assert (identifier_p (name));
22710 /* Perform the lookup. */
22711 if (parser->scope)
22713 bool dependent_p;
22715 if (parser->scope == error_mark_node)
22716 return error_mark_node;
22718 /* If the SCOPE is dependent, the lookup must be deferred until
22719 the template is instantiated -- unless we are explicitly
22720 looking up names in uninstantiated templates. Even then, we
22721 cannot look up the name if the scope is not a class type; it
22722 might, for example, be a template type parameter. */
22723 dependent_p = (TYPE_P (parser->scope)
22724 && dependent_scope_p (parser->scope));
22725 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22726 && dependent_p)
22727 /* Defer lookup. */
22728 decl = error_mark_node;
22729 else
22731 tree pushed_scope = NULL_TREE;
22733 /* If PARSER->SCOPE is a dependent type, then it must be a
22734 class type, and we must not be checking dependencies;
22735 otherwise, we would have processed this lookup above. So
22736 that PARSER->SCOPE is not considered a dependent base by
22737 lookup_member, we must enter the scope here. */
22738 if (dependent_p)
22739 pushed_scope = push_scope (parser->scope);
22741 /* If the PARSER->SCOPE is a template specialization, it
22742 may be instantiated during name lookup. In that case,
22743 errors may be issued. Even if we rollback the current
22744 tentative parse, those errors are valid. */
22745 decl = lookup_qualified_name (parser->scope, name,
22746 tag_type != none_type,
22747 /*complain=*/true);
22749 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22750 lookup result and the nested-name-specifier nominates a class C:
22751 * if the name specified after the nested-name-specifier, when
22752 looked up in C, is the injected-class-name of C (Clause 9), or
22753 * if the name specified after the nested-name-specifier is the
22754 same as the identifier or the simple-template-id's template-
22755 name in the last component of the nested-name-specifier,
22756 the name is instead considered to name the constructor of
22757 class C. [ Note: for example, the constructor is not an
22758 acceptable lookup result in an elaborated-type-specifier so
22759 the constructor would not be used in place of the
22760 injected-class-name. --end note ] Such a constructor name
22761 shall be used only in the declarator-id of a declaration that
22762 names a constructor or in a using-declaration. */
22763 if (tag_type == none_type
22764 && DECL_SELF_REFERENCE_P (decl)
22765 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22766 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22767 tag_type != none_type,
22768 /*complain=*/true);
22770 /* If we have a single function from a using decl, pull it out. */
22771 if (TREE_CODE (decl) == OVERLOAD
22772 && !really_overloaded_fn (decl))
22773 decl = OVL_FUNCTION (decl);
22775 if (pushed_scope)
22776 pop_scope (pushed_scope);
22779 /* If the scope is a dependent type and either we deferred lookup or
22780 we did lookup but didn't find the name, rememeber the name. */
22781 if (decl == error_mark_node && TYPE_P (parser->scope)
22782 && dependent_type_p (parser->scope))
22784 if (tag_type)
22786 tree type;
22788 /* The resolution to Core Issue 180 says that `struct
22789 A::B' should be considered a type-name, even if `A'
22790 is dependent. */
22791 type = make_typename_type (parser->scope, name, tag_type,
22792 /*complain=*/tf_error);
22793 if (type != error_mark_node)
22794 decl = TYPE_NAME (type);
22796 else if (is_template
22797 && (cp_parser_next_token_ends_template_argument_p (parser)
22798 || cp_lexer_next_token_is (parser->lexer,
22799 CPP_CLOSE_PAREN)))
22800 decl = make_unbound_class_template (parser->scope,
22801 name, NULL_TREE,
22802 /*complain=*/tf_error);
22803 else
22804 decl = build_qualified_name (/*type=*/NULL_TREE,
22805 parser->scope, name,
22806 is_template);
22808 parser->qualifying_scope = parser->scope;
22809 parser->object_scope = NULL_TREE;
22811 else if (object_type)
22813 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22814 OBJECT_TYPE is not a class. */
22815 if (CLASS_TYPE_P (object_type))
22816 /* If the OBJECT_TYPE is a template specialization, it may
22817 be instantiated during name lookup. In that case, errors
22818 may be issued. Even if we rollback the current tentative
22819 parse, those errors are valid. */
22820 decl = lookup_member (object_type,
22821 name,
22822 /*protect=*/0,
22823 tag_type != none_type,
22824 tf_warning_or_error);
22825 else
22826 decl = NULL_TREE;
22828 if (!decl)
22829 /* Look it up in the enclosing context. */
22830 decl = lookup_name_real (name, tag_type != none_type,
22831 /*nonclass=*/0,
22832 /*block_p=*/true, is_namespace, 0);
22833 parser->object_scope = object_type;
22834 parser->qualifying_scope = NULL_TREE;
22836 else
22838 decl = lookup_name_real (name, tag_type != none_type,
22839 /*nonclass=*/0,
22840 /*block_p=*/true, is_namespace, 0);
22841 parser->qualifying_scope = NULL_TREE;
22842 parser->object_scope = NULL_TREE;
22845 /* If the lookup failed, let our caller know. */
22846 if (!decl || decl == error_mark_node)
22847 return error_mark_node;
22849 /* Pull out the template from an injected-class-name (or multiple). */
22850 if (is_template)
22851 decl = maybe_get_template_decl_from_type_decl (decl);
22853 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22854 if (TREE_CODE (decl) == TREE_LIST)
22856 if (ambiguous_decls)
22857 *ambiguous_decls = decl;
22858 /* The error message we have to print is too complicated for
22859 cp_parser_error, so we incorporate its actions directly. */
22860 if (!cp_parser_simulate_error (parser))
22862 error_at (name_location, "reference to %qD is ambiguous",
22863 name);
22864 print_candidates (decl);
22866 return error_mark_node;
22869 gcc_assert (DECL_P (decl)
22870 || TREE_CODE (decl) == OVERLOAD
22871 || TREE_CODE (decl) == SCOPE_REF
22872 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22873 || BASELINK_P (decl));
22875 /* If we have resolved the name of a member declaration, check to
22876 see if the declaration is accessible. When the name resolves to
22877 set of overloaded functions, accessibility is checked when
22878 overload resolution is done.
22880 During an explicit instantiation, access is not checked at all,
22881 as per [temp.explicit]. */
22882 if (DECL_P (decl))
22883 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22885 maybe_record_typedef_use (decl);
22887 return decl;
22890 /* Like cp_parser_lookup_name, but for use in the typical case where
22891 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22892 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22894 static tree
22895 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22897 return cp_parser_lookup_name (parser, name,
22898 none_type,
22899 /*is_template=*/false,
22900 /*is_namespace=*/false,
22901 /*check_dependency=*/true,
22902 /*ambiguous_decls=*/NULL,
22903 location);
22906 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22907 the current context, return the TYPE_DECL. If TAG_NAME_P is
22908 true, the DECL indicates the class being defined in a class-head,
22909 or declared in an elaborated-type-specifier.
22911 Otherwise, return DECL. */
22913 static tree
22914 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22916 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22917 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22919 struct A {
22920 template <typename T> struct B;
22923 template <typename T> struct A::B {};
22925 Similarly, in an elaborated-type-specifier:
22927 namespace N { struct X{}; }
22929 struct A {
22930 template <typename T> friend struct N::X;
22933 However, if the DECL refers to a class type, and we are in
22934 the scope of the class, then the name lookup automatically
22935 finds the TYPE_DECL created by build_self_reference rather
22936 than a TEMPLATE_DECL. For example, in:
22938 template <class T> struct S {
22939 S s;
22942 there is no need to handle such case. */
22944 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22945 return DECL_TEMPLATE_RESULT (decl);
22947 return decl;
22950 /* If too many, or too few, template-parameter lists apply to the
22951 declarator, issue an error message. Returns TRUE if all went well,
22952 and FALSE otherwise. */
22954 static bool
22955 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22956 cp_declarator *declarator,
22957 location_t declarator_location)
22959 switch (declarator->kind)
22961 case cdk_id:
22963 unsigned num_templates = 0;
22964 tree scope = declarator->u.id.qualifying_scope;
22966 if (scope)
22967 num_templates = num_template_headers_for_class (scope);
22968 else if (TREE_CODE (declarator->u.id.unqualified_name)
22969 == TEMPLATE_ID_EXPR)
22970 /* If the DECLARATOR has the form `X<y>' then it uses one
22971 additional level of template parameters. */
22972 ++num_templates;
22974 return cp_parser_check_template_parameters
22975 (parser, num_templates, declarator_location, declarator);
22978 case cdk_function:
22979 case cdk_array:
22980 case cdk_pointer:
22981 case cdk_reference:
22982 case cdk_ptrmem:
22983 return (cp_parser_check_declarator_template_parameters
22984 (parser, declarator->declarator, declarator_location));
22986 case cdk_error:
22987 return true;
22989 default:
22990 gcc_unreachable ();
22992 return false;
22995 /* NUM_TEMPLATES were used in the current declaration. If that is
22996 invalid, return FALSE and issue an error messages. Otherwise,
22997 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22998 declarator and we can print more accurate diagnostics. */
23000 static bool
23001 cp_parser_check_template_parameters (cp_parser* parser,
23002 unsigned num_templates,
23003 location_t location,
23004 cp_declarator *declarator)
23006 /* If there are the same number of template classes and parameter
23007 lists, that's OK. */
23008 if (parser->num_template_parameter_lists == num_templates)
23009 return true;
23010 /* If there are more, but only one more, then we are referring to a
23011 member template. That's OK too. */
23012 if (parser->num_template_parameter_lists == num_templates + 1)
23013 return true;
23014 /* If there are more template classes than parameter lists, we have
23015 something like:
23017 template <class T> void S<T>::R<T>::f (); */
23018 if (parser->num_template_parameter_lists < num_templates)
23020 if (declarator && !current_function_decl)
23021 error_at (location, "specializing member %<%T::%E%> "
23022 "requires %<template<>%> syntax",
23023 declarator->u.id.qualifying_scope,
23024 declarator->u.id.unqualified_name);
23025 else if (declarator)
23026 error_at (location, "invalid declaration of %<%T::%E%>",
23027 declarator->u.id.qualifying_scope,
23028 declarator->u.id.unqualified_name);
23029 else
23030 error_at (location, "too few template-parameter-lists");
23031 return false;
23033 /* Otherwise, there are too many template parameter lists. We have
23034 something like:
23036 template <class T> template <class U> void S::f(); */
23037 error_at (location, "too many template-parameter-lists");
23038 return false;
23041 /* Parse an optional `::' token indicating that the following name is
23042 from the global namespace. If so, PARSER->SCOPE is set to the
23043 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23044 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23045 Returns the new value of PARSER->SCOPE, if the `::' token is
23046 present, and NULL_TREE otherwise. */
23048 static tree
23049 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23051 cp_token *token;
23053 /* Peek at the next token. */
23054 token = cp_lexer_peek_token (parser->lexer);
23055 /* If we're looking at a `::' token then we're starting from the
23056 global namespace, not our current location. */
23057 if (token->type == CPP_SCOPE)
23059 /* Consume the `::' token. */
23060 cp_lexer_consume_token (parser->lexer);
23061 /* Set the SCOPE so that we know where to start the lookup. */
23062 parser->scope = global_namespace;
23063 parser->qualifying_scope = global_namespace;
23064 parser->object_scope = NULL_TREE;
23066 return parser->scope;
23068 else if (!current_scope_valid_p)
23070 parser->scope = NULL_TREE;
23071 parser->qualifying_scope = NULL_TREE;
23072 parser->object_scope = NULL_TREE;
23075 return NULL_TREE;
23078 /* Returns TRUE if the upcoming token sequence is the start of a
23079 constructor declarator. If FRIEND_P is true, the declarator is
23080 preceded by the `friend' specifier. */
23082 static bool
23083 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23085 bool constructor_p;
23086 bool outside_class_specifier_p;
23087 tree nested_name_specifier;
23088 cp_token *next_token;
23090 /* The common case is that this is not a constructor declarator, so
23091 try to avoid doing lots of work if at all possible. It's not
23092 valid declare a constructor at function scope. */
23093 if (parser->in_function_body)
23094 return false;
23095 /* And only certain tokens can begin a constructor declarator. */
23096 next_token = cp_lexer_peek_token (parser->lexer);
23097 if (next_token->type != CPP_NAME
23098 && next_token->type != CPP_SCOPE
23099 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23100 && next_token->type != CPP_TEMPLATE_ID)
23101 return false;
23103 /* Parse tentatively; we are going to roll back all of the tokens
23104 consumed here. */
23105 cp_parser_parse_tentatively (parser);
23106 /* Assume that we are looking at a constructor declarator. */
23107 constructor_p = true;
23109 /* Look for the optional `::' operator. */
23110 cp_parser_global_scope_opt (parser,
23111 /*current_scope_valid_p=*/false);
23112 /* Look for the nested-name-specifier. */
23113 nested_name_specifier
23114 = (cp_parser_nested_name_specifier_opt (parser,
23115 /*typename_keyword_p=*/false,
23116 /*check_dependency_p=*/false,
23117 /*type_p=*/false,
23118 /*is_declaration=*/false));
23120 outside_class_specifier_p = (!at_class_scope_p ()
23121 || !TYPE_BEING_DEFINED (current_class_type)
23122 || friend_p);
23124 /* Outside of a class-specifier, there must be a
23125 nested-name-specifier. */
23126 if (!nested_name_specifier && outside_class_specifier_p)
23127 constructor_p = false;
23128 else if (nested_name_specifier == error_mark_node)
23129 constructor_p = false;
23131 /* If we have a class scope, this is easy; DR 147 says that S::S always
23132 names the constructor, and no other qualified name could. */
23133 if (constructor_p && nested_name_specifier
23134 && CLASS_TYPE_P (nested_name_specifier))
23136 tree id = cp_parser_unqualified_id (parser,
23137 /*template_keyword_p=*/false,
23138 /*check_dependency_p=*/false,
23139 /*declarator_p=*/true,
23140 /*optional_p=*/false);
23141 if (is_overloaded_fn (id))
23142 id = DECL_NAME (get_first_fn (id));
23143 if (!constructor_name_p (id, nested_name_specifier))
23144 constructor_p = false;
23146 /* If we still think that this might be a constructor-declarator,
23147 look for a class-name. */
23148 else if (constructor_p)
23150 /* If we have:
23152 template <typename T> struct S {
23153 S();
23156 we must recognize that the nested `S' names a class. */
23157 tree type_decl;
23158 type_decl = cp_parser_class_name (parser,
23159 /*typename_keyword_p=*/false,
23160 /*template_keyword_p=*/false,
23161 none_type,
23162 /*check_dependency_p=*/false,
23163 /*class_head_p=*/false,
23164 /*is_declaration=*/false);
23165 /* If there was no class-name, then this is not a constructor.
23166 Otherwise, if we are in a class-specifier and we aren't
23167 handling a friend declaration, check that its type matches
23168 current_class_type (c++/38313). Note: error_mark_node
23169 is left alone for error recovery purposes. */
23170 constructor_p = (!cp_parser_error_occurred (parser)
23171 && (outside_class_specifier_p
23172 || type_decl == error_mark_node
23173 || same_type_p (current_class_type,
23174 TREE_TYPE (type_decl))));
23176 /* If we're still considering a constructor, we have to see a `(',
23177 to begin the parameter-declaration-clause, followed by either a
23178 `)', an `...', or a decl-specifier. We need to check for a
23179 type-specifier to avoid being fooled into thinking that:
23181 S (f) (int);
23183 is a constructor. (It is actually a function named `f' that
23184 takes one parameter (of type `int') and returns a value of type
23185 `S'. */
23186 if (constructor_p
23187 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23188 constructor_p = false;
23190 if (constructor_p
23191 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23192 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23193 /* A parameter declaration begins with a decl-specifier,
23194 which is either the "attribute" keyword, a storage class
23195 specifier, or (usually) a type-specifier. */
23196 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23198 tree type;
23199 tree pushed_scope = NULL_TREE;
23200 unsigned saved_num_template_parameter_lists;
23202 /* Names appearing in the type-specifier should be looked up
23203 in the scope of the class. */
23204 if (current_class_type)
23205 type = NULL_TREE;
23206 else
23208 type = TREE_TYPE (type_decl);
23209 if (TREE_CODE (type) == TYPENAME_TYPE)
23211 type = resolve_typename_type (type,
23212 /*only_current_p=*/false);
23213 if (TREE_CODE (type) == TYPENAME_TYPE)
23215 cp_parser_abort_tentative_parse (parser);
23216 return false;
23219 pushed_scope = push_scope (type);
23222 /* Inside the constructor parameter list, surrounding
23223 template-parameter-lists do not apply. */
23224 saved_num_template_parameter_lists
23225 = parser->num_template_parameter_lists;
23226 parser->num_template_parameter_lists = 0;
23228 /* Look for the type-specifier. */
23229 cp_parser_type_specifier (parser,
23230 CP_PARSER_FLAGS_NONE,
23231 /*decl_specs=*/NULL,
23232 /*is_declarator=*/true,
23233 /*declares_class_or_enum=*/NULL,
23234 /*is_cv_qualifier=*/NULL);
23236 parser->num_template_parameter_lists
23237 = saved_num_template_parameter_lists;
23239 /* Leave the scope of the class. */
23240 if (pushed_scope)
23241 pop_scope (pushed_scope);
23243 constructor_p = !cp_parser_error_occurred (parser);
23247 /* We did not really want to consume any tokens. */
23248 cp_parser_abort_tentative_parse (parser);
23250 return constructor_p;
23253 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23254 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23255 they must be performed once we are in the scope of the function.
23257 Returns the function defined. */
23259 static tree
23260 cp_parser_function_definition_from_specifiers_and_declarator
23261 (cp_parser* parser,
23262 cp_decl_specifier_seq *decl_specifiers,
23263 tree attributes,
23264 const cp_declarator *declarator)
23266 tree fn;
23267 bool success_p;
23269 /* Begin the function-definition. */
23270 success_p = start_function (decl_specifiers, declarator, attributes);
23272 /* The things we're about to see are not directly qualified by any
23273 template headers we've seen thus far. */
23274 reset_specialization ();
23276 /* If there were names looked up in the decl-specifier-seq that we
23277 did not check, check them now. We must wait until we are in the
23278 scope of the function to perform the checks, since the function
23279 might be a friend. */
23280 perform_deferred_access_checks (tf_warning_or_error);
23282 if (success_p)
23284 cp_finalize_omp_declare_simd (parser, current_function_decl);
23285 parser->omp_declare_simd = NULL;
23288 if (!success_p)
23290 /* Skip the entire function. */
23291 cp_parser_skip_to_end_of_block_or_statement (parser);
23292 fn = error_mark_node;
23294 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23296 /* Seen already, skip it. An error message has already been output. */
23297 cp_parser_skip_to_end_of_block_or_statement (parser);
23298 fn = current_function_decl;
23299 current_function_decl = NULL_TREE;
23300 /* If this is a function from a class, pop the nested class. */
23301 if (current_class_name)
23302 pop_nested_class ();
23304 else
23306 timevar_id_t tv;
23307 if (DECL_DECLARED_INLINE_P (current_function_decl))
23308 tv = TV_PARSE_INLINE;
23309 else
23310 tv = TV_PARSE_FUNC;
23311 timevar_push (tv);
23312 fn = cp_parser_function_definition_after_declarator (parser,
23313 /*inline_p=*/false);
23314 timevar_pop (tv);
23317 return fn;
23320 /* Parse the part of a function-definition that follows the
23321 declarator. INLINE_P is TRUE iff this function is an inline
23322 function defined within a class-specifier.
23324 Returns the function defined. */
23326 static tree
23327 cp_parser_function_definition_after_declarator (cp_parser* parser,
23328 bool inline_p)
23330 tree fn;
23331 bool ctor_initializer_p = false;
23332 bool saved_in_unbraced_linkage_specification_p;
23333 bool saved_in_function_body;
23334 unsigned saved_num_template_parameter_lists;
23335 cp_token *token;
23336 bool fully_implicit_function_template_p
23337 = parser->fully_implicit_function_template_p;
23338 parser->fully_implicit_function_template_p = false;
23339 tree implicit_template_parms
23340 = parser->implicit_template_parms;
23341 parser->implicit_template_parms = 0;
23342 cp_binding_level* implicit_template_scope
23343 = parser->implicit_template_scope;
23344 parser->implicit_template_scope = 0;
23346 saved_in_function_body = parser->in_function_body;
23347 parser->in_function_body = true;
23348 /* If the next token is `return', then the code may be trying to
23349 make use of the "named return value" extension that G++ used to
23350 support. */
23351 token = cp_lexer_peek_token (parser->lexer);
23352 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23354 /* Consume the `return' keyword. */
23355 cp_lexer_consume_token (parser->lexer);
23356 /* Look for the identifier that indicates what value is to be
23357 returned. */
23358 cp_parser_identifier (parser);
23359 /* Issue an error message. */
23360 error_at (token->location,
23361 "named return values are no longer supported");
23362 /* Skip tokens until we reach the start of the function body. */
23363 while (true)
23365 cp_token *token = cp_lexer_peek_token (parser->lexer);
23366 if (token->type == CPP_OPEN_BRACE
23367 || token->type == CPP_EOF
23368 || token->type == CPP_PRAGMA_EOL)
23369 break;
23370 cp_lexer_consume_token (parser->lexer);
23373 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23374 anything declared inside `f'. */
23375 saved_in_unbraced_linkage_specification_p
23376 = parser->in_unbraced_linkage_specification_p;
23377 parser->in_unbraced_linkage_specification_p = false;
23378 /* Inside the function, surrounding template-parameter-lists do not
23379 apply. */
23380 saved_num_template_parameter_lists
23381 = parser->num_template_parameter_lists;
23382 parser->num_template_parameter_lists = 0;
23384 start_lambda_scope (current_function_decl);
23386 /* If the next token is `try', `__transaction_atomic', or
23387 `__transaction_relaxed`, then we are looking at either function-try-block
23388 or function-transaction-block. Note that all of these include the
23389 function-body. */
23390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23391 ctor_initializer_p = cp_parser_function_transaction (parser,
23392 RID_TRANSACTION_ATOMIC);
23393 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23394 RID_TRANSACTION_RELAXED))
23395 ctor_initializer_p = cp_parser_function_transaction (parser,
23396 RID_TRANSACTION_RELAXED);
23397 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23398 ctor_initializer_p = cp_parser_function_try_block (parser);
23399 else
23400 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23401 (parser, /*in_function_try_block=*/false);
23403 finish_lambda_scope ();
23405 /* Finish the function. */
23406 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23407 (inline_p ? 2 : 0));
23408 /* Generate code for it, if necessary. */
23409 expand_or_defer_fn (fn);
23410 /* Restore the saved values. */
23411 parser->in_unbraced_linkage_specification_p
23412 = saved_in_unbraced_linkage_specification_p;
23413 parser->num_template_parameter_lists
23414 = saved_num_template_parameter_lists;
23415 parser->in_function_body = saved_in_function_body;
23417 parser->fully_implicit_function_template_p
23418 = fully_implicit_function_template_p;
23419 parser->implicit_template_parms
23420 = implicit_template_parms;
23421 parser->implicit_template_scope
23422 = implicit_template_scope;
23424 if (parser->fully_implicit_function_template_p)
23425 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23427 return fn;
23430 /* Parse a template-declaration, assuming that the `export' (and
23431 `extern') keywords, if present, has already been scanned. MEMBER_P
23432 is as for cp_parser_template_declaration. */
23434 static void
23435 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23437 tree decl = NULL_TREE;
23438 vec<deferred_access_check, va_gc> *checks;
23439 tree parameter_list;
23440 bool friend_p = false;
23441 bool need_lang_pop;
23442 cp_token *token;
23444 /* Look for the `template' keyword. */
23445 token = cp_lexer_peek_token (parser->lexer);
23446 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23447 return;
23449 /* And the `<'. */
23450 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23451 return;
23452 if (at_class_scope_p () && current_function_decl)
23454 /* 14.5.2.2 [temp.mem]
23456 A local class shall not have member templates. */
23457 error_at (token->location,
23458 "invalid declaration of member template in local class");
23459 cp_parser_skip_to_end_of_block_or_statement (parser);
23460 return;
23462 /* [temp]
23464 A template ... shall not have C linkage. */
23465 if (current_lang_name == lang_name_c)
23467 error_at (token->location, "template with C linkage");
23468 /* Give it C++ linkage to avoid confusing other parts of the
23469 front end. */
23470 push_lang_context (lang_name_cplusplus);
23471 need_lang_pop = true;
23473 else
23474 need_lang_pop = false;
23476 /* We cannot perform access checks on the template parameter
23477 declarations until we know what is being declared, just as we
23478 cannot check the decl-specifier list. */
23479 push_deferring_access_checks (dk_deferred);
23481 /* If the next token is `>', then we have an invalid
23482 specialization. Rather than complain about an invalid template
23483 parameter, issue an error message here. */
23484 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23486 cp_parser_error (parser, "invalid explicit specialization");
23487 begin_specialization ();
23488 parameter_list = NULL_TREE;
23490 else
23492 /* Parse the template parameters. */
23493 parameter_list = cp_parser_template_parameter_list (parser);
23496 /* Get the deferred access checks from the parameter list. These
23497 will be checked once we know what is being declared, as for a
23498 member template the checks must be performed in the scope of the
23499 class containing the member. */
23500 checks = get_deferred_access_checks ();
23502 /* Look for the `>'. */
23503 cp_parser_skip_to_end_of_template_parameter_list (parser);
23504 /* We just processed one more parameter list. */
23505 ++parser->num_template_parameter_lists;
23506 /* If the next token is `template', there are more template
23507 parameters. */
23508 if (cp_lexer_next_token_is_keyword (parser->lexer,
23509 RID_TEMPLATE))
23510 cp_parser_template_declaration_after_export (parser, member_p);
23511 else if (cxx_dialect >= cxx11
23512 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23513 decl = cp_parser_alias_declaration (parser);
23514 else
23516 /* There are no access checks when parsing a template, as we do not
23517 know if a specialization will be a friend. */
23518 push_deferring_access_checks (dk_no_check);
23519 token = cp_lexer_peek_token (parser->lexer);
23520 decl = cp_parser_single_declaration (parser,
23521 checks,
23522 member_p,
23523 /*explicit_specialization_p=*/false,
23524 &friend_p);
23525 pop_deferring_access_checks ();
23527 /* If this is a member template declaration, let the front
23528 end know. */
23529 if (member_p && !friend_p && decl)
23531 if (TREE_CODE (decl) == TYPE_DECL)
23532 cp_parser_check_access_in_redeclaration (decl, token->location);
23534 decl = finish_member_template_decl (decl);
23536 else if (friend_p && decl
23537 && DECL_DECLARES_TYPE_P (decl))
23538 make_friend_class (current_class_type, TREE_TYPE (decl),
23539 /*complain=*/true);
23541 /* We are done with the current parameter list. */
23542 --parser->num_template_parameter_lists;
23544 pop_deferring_access_checks ();
23546 /* Finish up. */
23547 finish_template_decl (parameter_list);
23549 /* Check the template arguments for a literal operator template. */
23550 if (decl
23551 && DECL_DECLARES_FUNCTION_P (decl)
23552 && UDLIT_OPER_P (DECL_NAME (decl)))
23554 bool ok = true;
23555 if (parameter_list == NULL_TREE)
23556 ok = false;
23557 else
23559 int num_parms = TREE_VEC_LENGTH (parameter_list);
23560 if (num_parms == 1)
23562 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23563 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23564 if (TREE_TYPE (parm) != char_type_node
23565 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23566 ok = false;
23568 else if (num_parms == 2 && cxx_dialect >= cxx14)
23570 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23571 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23572 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23573 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23574 if (TREE_TYPE (parm) != TREE_TYPE (type)
23575 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23576 ok = false;
23578 else
23579 ok = false;
23581 if (!ok)
23583 if (cxx_dialect >= cxx14)
23584 error ("literal operator template %qD has invalid parameter list."
23585 " Expected non-type template argument pack <char...>"
23586 " or <typename CharT, CharT...>",
23587 decl);
23588 else
23589 error ("literal operator template %qD has invalid parameter list."
23590 " Expected non-type template argument pack <char...>",
23591 decl);
23594 /* Register member declarations. */
23595 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23596 finish_member_declaration (decl);
23597 /* For the erroneous case of a template with C linkage, we pushed an
23598 implicit C++ linkage scope; exit that scope now. */
23599 if (need_lang_pop)
23600 pop_lang_context ();
23601 /* If DECL is a function template, we must return to parse it later.
23602 (Even though there is no definition, there might be default
23603 arguments that need handling.) */
23604 if (member_p && decl
23605 && DECL_DECLARES_FUNCTION_P (decl))
23606 vec_safe_push (unparsed_funs_with_definitions, decl);
23609 /* Perform the deferred access checks from a template-parameter-list.
23610 CHECKS is a TREE_LIST of access checks, as returned by
23611 get_deferred_access_checks. */
23613 static void
23614 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23616 ++processing_template_parmlist;
23617 perform_access_checks (checks, tf_warning_or_error);
23618 --processing_template_parmlist;
23621 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23622 `function-definition' sequence that follows a template header.
23623 If MEMBER_P is true, this declaration appears in a class scope.
23625 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23626 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23628 static tree
23629 cp_parser_single_declaration (cp_parser* parser,
23630 vec<deferred_access_check, va_gc> *checks,
23631 bool member_p,
23632 bool explicit_specialization_p,
23633 bool* friend_p)
23635 int declares_class_or_enum;
23636 tree decl = NULL_TREE;
23637 cp_decl_specifier_seq decl_specifiers;
23638 bool function_definition_p = false;
23639 cp_token *decl_spec_token_start;
23641 /* This function is only used when processing a template
23642 declaration. */
23643 gcc_assert (innermost_scope_kind () == sk_template_parms
23644 || innermost_scope_kind () == sk_template_spec);
23646 /* Defer access checks until we know what is being declared. */
23647 push_deferring_access_checks (dk_deferred);
23649 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23650 alternative. */
23651 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23652 cp_parser_decl_specifier_seq (parser,
23653 CP_PARSER_FLAGS_OPTIONAL,
23654 &decl_specifiers,
23655 &declares_class_or_enum);
23656 if (friend_p)
23657 *friend_p = cp_parser_friend_p (&decl_specifiers);
23659 /* There are no template typedefs. */
23660 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23662 error_at (decl_spec_token_start->location,
23663 "template declaration of %<typedef%>");
23664 decl = error_mark_node;
23667 /* Gather up the access checks that occurred the
23668 decl-specifier-seq. */
23669 stop_deferring_access_checks ();
23671 /* Check for the declaration of a template class. */
23672 if (declares_class_or_enum)
23674 if (cp_parser_declares_only_class_p (parser))
23676 decl = shadow_tag (&decl_specifiers);
23678 /* In this case:
23680 struct C {
23681 friend template <typename T> struct A<T>::B;
23684 A<T>::B will be represented by a TYPENAME_TYPE, and
23685 therefore not recognized by shadow_tag. */
23686 if (friend_p && *friend_p
23687 && !decl
23688 && decl_specifiers.type
23689 && TYPE_P (decl_specifiers.type))
23690 decl = decl_specifiers.type;
23692 if (decl && decl != error_mark_node)
23693 decl = TYPE_NAME (decl);
23694 else
23695 decl = error_mark_node;
23697 /* Perform access checks for template parameters. */
23698 cp_parser_perform_template_parameter_access_checks (checks);
23702 /* Complain about missing 'typename' or other invalid type names. */
23703 if (!decl_specifiers.any_type_specifiers_p
23704 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23706 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23707 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23708 the rest of this declaration. */
23709 decl = error_mark_node;
23710 goto out;
23713 /* If it's not a template class, try for a template function. If
23714 the next token is a `;', then this declaration does not declare
23715 anything. But, if there were errors in the decl-specifiers, then
23716 the error might well have come from an attempted class-specifier.
23717 In that case, there's no need to warn about a missing declarator. */
23718 if (!decl
23719 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23720 || decl_specifiers.type != error_mark_node))
23722 decl = cp_parser_init_declarator (parser,
23723 &decl_specifiers,
23724 checks,
23725 /*function_definition_allowed_p=*/true,
23726 member_p,
23727 declares_class_or_enum,
23728 &function_definition_p,
23729 NULL);
23731 /* 7.1.1-1 [dcl.stc]
23733 A storage-class-specifier shall not be specified in an explicit
23734 specialization... */
23735 if (decl
23736 && explicit_specialization_p
23737 && decl_specifiers.storage_class != sc_none)
23739 error_at (decl_spec_token_start->location,
23740 "explicit template specialization cannot have a storage class");
23741 decl = error_mark_node;
23744 if (decl && VAR_P (decl))
23745 check_template_variable (decl);
23748 /* Look for a trailing `;' after the declaration. */
23749 if (!function_definition_p
23750 && (decl == error_mark_node
23751 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23752 cp_parser_skip_to_end_of_block_or_statement (parser);
23754 out:
23755 pop_deferring_access_checks ();
23757 /* Clear any current qualification; whatever comes next is the start
23758 of something new. */
23759 parser->scope = NULL_TREE;
23760 parser->qualifying_scope = NULL_TREE;
23761 parser->object_scope = NULL_TREE;
23763 return decl;
23766 /* Parse a cast-expression that is not the operand of a unary "&". */
23768 static tree
23769 cp_parser_simple_cast_expression (cp_parser *parser)
23771 return cp_parser_cast_expression (parser, /*address_p=*/false,
23772 /*cast_p=*/false, /*decltype*/false, NULL);
23775 /* Parse a functional cast to TYPE. Returns an expression
23776 representing the cast. */
23778 static tree
23779 cp_parser_functional_cast (cp_parser* parser, tree type)
23781 vec<tree, va_gc> *vec;
23782 tree expression_list;
23783 tree cast;
23784 bool nonconst_p;
23786 if (!type)
23787 type = error_mark_node;
23789 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23791 cp_lexer_set_source_position (parser->lexer);
23792 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23793 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23794 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23795 if (TREE_CODE (type) == TYPE_DECL)
23796 type = TREE_TYPE (type);
23797 return finish_compound_literal (type, expression_list,
23798 tf_warning_or_error);
23802 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23803 /*cast_p=*/true,
23804 /*allow_expansion_p=*/true,
23805 /*non_constant_p=*/NULL);
23806 if (vec == NULL)
23807 expression_list = error_mark_node;
23808 else
23810 expression_list = build_tree_list_vec (vec);
23811 release_tree_vector (vec);
23814 cast = build_functional_cast (type, expression_list,
23815 tf_warning_or_error);
23816 /* [expr.const]/1: In an integral constant expression "only type
23817 conversions to integral or enumeration type can be used". */
23818 if (TREE_CODE (type) == TYPE_DECL)
23819 type = TREE_TYPE (type);
23820 if (cast != error_mark_node
23821 && !cast_valid_in_integral_constant_expression_p (type)
23822 && cp_parser_non_integral_constant_expression (parser,
23823 NIC_CONSTRUCTOR))
23824 return error_mark_node;
23825 return cast;
23828 /* Save the tokens that make up the body of a member function defined
23829 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23830 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23831 specifiers applied to the declaration. Returns the FUNCTION_DECL
23832 for the member function. */
23834 static tree
23835 cp_parser_save_member_function_body (cp_parser* parser,
23836 cp_decl_specifier_seq *decl_specifiers,
23837 cp_declarator *declarator,
23838 tree attributes)
23840 cp_token *first;
23841 cp_token *last;
23842 tree fn;
23844 /* Create the FUNCTION_DECL. */
23845 fn = grokmethod (decl_specifiers, declarator, attributes);
23846 cp_finalize_omp_declare_simd (parser, fn);
23847 /* If something went badly wrong, bail out now. */
23848 if (fn == error_mark_node)
23850 /* If there's a function-body, skip it. */
23851 if (cp_parser_token_starts_function_definition_p
23852 (cp_lexer_peek_token (parser->lexer)))
23853 cp_parser_skip_to_end_of_block_or_statement (parser);
23854 return error_mark_node;
23857 /* Remember it, if there default args to post process. */
23858 cp_parser_save_default_args (parser, fn);
23860 /* Save away the tokens that make up the body of the
23861 function. */
23862 first = parser->lexer->next_token;
23863 /* Handle function try blocks. */
23864 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23865 cp_lexer_consume_token (parser->lexer);
23866 /* We can have braced-init-list mem-initializers before the fn body. */
23867 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23869 cp_lexer_consume_token (parser->lexer);
23870 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23872 /* cache_group will stop after an un-nested { } pair, too. */
23873 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23874 break;
23876 /* variadic mem-inits have ... after the ')'. */
23877 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23878 cp_lexer_consume_token (parser->lexer);
23881 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23882 /* Handle function try blocks. */
23883 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23884 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23885 last = parser->lexer->next_token;
23887 /* Save away the inline definition; we will process it when the
23888 class is complete. */
23889 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23890 DECL_PENDING_INLINE_P (fn) = 1;
23892 /* We need to know that this was defined in the class, so that
23893 friend templates are handled correctly. */
23894 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23896 /* Add FN to the queue of functions to be parsed later. */
23897 vec_safe_push (unparsed_funs_with_definitions, fn);
23899 return fn;
23902 /* Save the tokens that make up the in-class initializer for a non-static
23903 data member. Returns a DEFAULT_ARG. */
23905 static tree
23906 cp_parser_save_nsdmi (cp_parser* parser)
23908 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23911 /* Parse a template-argument-list, as well as the trailing ">" (but
23912 not the opening "<"). See cp_parser_template_argument_list for the
23913 return value. */
23915 static tree
23916 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23918 tree arguments;
23919 tree saved_scope;
23920 tree saved_qualifying_scope;
23921 tree saved_object_scope;
23922 bool saved_greater_than_is_operator_p;
23923 int saved_unevaluated_operand;
23924 int saved_inhibit_evaluation_warnings;
23926 /* [temp.names]
23928 When parsing a template-id, the first non-nested `>' is taken as
23929 the end of the template-argument-list rather than a greater-than
23930 operator. */
23931 saved_greater_than_is_operator_p
23932 = parser->greater_than_is_operator_p;
23933 parser->greater_than_is_operator_p = false;
23934 /* Parsing the argument list may modify SCOPE, so we save it
23935 here. */
23936 saved_scope = parser->scope;
23937 saved_qualifying_scope = parser->qualifying_scope;
23938 saved_object_scope = parser->object_scope;
23939 /* We need to evaluate the template arguments, even though this
23940 template-id may be nested within a "sizeof". */
23941 saved_unevaluated_operand = cp_unevaluated_operand;
23942 cp_unevaluated_operand = 0;
23943 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23944 c_inhibit_evaluation_warnings = 0;
23945 /* Parse the template-argument-list itself. */
23946 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23947 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23948 arguments = NULL_TREE;
23949 else
23950 arguments = cp_parser_template_argument_list (parser);
23951 /* Look for the `>' that ends the template-argument-list. If we find
23952 a '>>' instead, it's probably just a typo. */
23953 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23955 if (cxx_dialect != cxx98)
23957 /* In C++0x, a `>>' in a template argument list or cast
23958 expression is considered to be two separate `>'
23959 tokens. So, change the current token to a `>', but don't
23960 consume it: it will be consumed later when the outer
23961 template argument list (or cast expression) is parsed.
23962 Note that this replacement of `>' for `>>' is necessary
23963 even if we are parsing tentatively: in the tentative
23964 case, after calling
23965 cp_parser_enclosed_template_argument_list we will always
23966 throw away all of the template arguments and the first
23967 closing `>', either because the template argument list
23968 was erroneous or because we are replacing those tokens
23969 with a CPP_TEMPLATE_ID token. The second `>' (which will
23970 not have been thrown away) is needed either to close an
23971 outer template argument list or to complete a new-style
23972 cast. */
23973 cp_token *token = cp_lexer_peek_token (parser->lexer);
23974 token->type = CPP_GREATER;
23976 else if (!saved_greater_than_is_operator_p)
23978 /* If we're in a nested template argument list, the '>>' has
23979 to be a typo for '> >'. We emit the error message, but we
23980 continue parsing and we push a '>' as next token, so that
23981 the argument list will be parsed correctly. Note that the
23982 global source location is still on the token before the
23983 '>>', so we need to say explicitly where we want it. */
23984 cp_token *token = cp_lexer_peek_token (parser->lexer);
23985 error_at (token->location, "%<>>%> should be %<> >%> "
23986 "within a nested template argument list");
23988 token->type = CPP_GREATER;
23990 else
23992 /* If this is not a nested template argument list, the '>>'
23993 is a typo for '>'. Emit an error message and continue.
23994 Same deal about the token location, but here we can get it
23995 right by consuming the '>>' before issuing the diagnostic. */
23996 cp_token *token = cp_lexer_consume_token (parser->lexer);
23997 error_at (token->location,
23998 "spurious %<>>%>, use %<>%> to terminate "
23999 "a template argument list");
24002 else
24003 cp_parser_skip_to_end_of_template_parameter_list (parser);
24004 /* The `>' token might be a greater-than operator again now. */
24005 parser->greater_than_is_operator_p
24006 = saved_greater_than_is_operator_p;
24007 /* Restore the SAVED_SCOPE. */
24008 parser->scope = saved_scope;
24009 parser->qualifying_scope = saved_qualifying_scope;
24010 parser->object_scope = saved_object_scope;
24011 cp_unevaluated_operand = saved_unevaluated_operand;
24012 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24014 return arguments;
24017 /* MEMBER_FUNCTION is a member function, or a friend. If default
24018 arguments, or the body of the function have not yet been parsed,
24019 parse them now. */
24021 static void
24022 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24024 timevar_push (TV_PARSE_INMETH);
24025 /* If this member is a template, get the underlying
24026 FUNCTION_DECL. */
24027 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24028 member_function = DECL_TEMPLATE_RESULT (member_function);
24030 /* There should not be any class definitions in progress at this
24031 point; the bodies of members are only parsed outside of all class
24032 definitions. */
24033 gcc_assert (parser->num_classes_being_defined == 0);
24034 /* While we're parsing the member functions we might encounter more
24035 classes. We want to handle them right away, but we don't want
24036 them getting mixed up with functions that are currently in the
24037 queue. */
24038 push_unparsed_function_queues (parser);
24040 /* Make sure that any template parameters are in scope. */
24041 maybe_begin_member_template_processing (member_function);
24043 /* If the body of the function has not yet been parsed, parse it
24044 now. */
24045 if (DECL_PENDING_INLINE_P (member_function))
24047 tree function_scope;
24048 cp_token_cache *tokens;
24050 /* The function is no longer pending; we are processing it. */
24051 tokens = DECL_PENDING_INLINE_INFO (member_function);
24052 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24053 DECL_PENDING_INLINE_P (member_function) = 0;
24055 /* If this is a local class, enter the scope of the containing
24056 function. */
24057 function_scope = current_function_decl;
24058 if (function_scope)
24059 push_function_context ();
24061 /* Push the body of the function onto the lexer stack. */
24062 cp_parser_push_lexer_for_tokens (parser, tokens);
24064 /* Let the front end know that we going to be defining this
24065 function. */
24066 start_preparsed_function (member_function, NULL_TREE,
24067 SF_PRE_PARSED | SF_INCLASS_INLINE);
24069 /* Don't do access checking if it is a templated function. */
24070 if (processing_template_decl)
24071 push_deferring_access_checks (dk_no_check);
24073 /* #pragma omp declare reduction needs special parsing. */
24074 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24076 parser->lexer->in_pragma = true;
24077 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24078 finish_function (/*inline*/2);
24079 cp_check_omp_declare_reduction (member_function);
24081 else
24082 /* Now, parse the body of the function. */
24083 cp_parser_function_definition_after_declarator (parser,
24084 /*inline_p=*/true);
24086 if (processing_template_decl)
24087 pop_deferring_access_checks ();
24089 /* Leave the scope of the containing function. */
24090 if (function_scope)
24091 pop_function_context ();
24092 cp_parser_pop_lexer (parser);
24095 /* Remove any template parameters from the symbol table. */
24096 maybe_end_member_template_processing ();
24098 /* Restore the queue. */
24099 pop_unparsed_function_queues (parser);
24100 timevar_pop (TV_PARSE_INMETH);
24103 /* If DECL contains any default args, remember it on the unparsed
24104 functions queue. */
24106 static void
24107 cp_parser_save_default_args (cp_parser* parser, tree decl)
24109 tree probe;
24111 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24112 probe;
24113 probe = TREE_CHAIN (probe))
24114 if (TREE_PURPOSE (probe))
24116 cp_default_arg_entry entry = {current_class_type, decl};
24117 vec_safe_push (unparsed_funs_with_default_args, entry);
24118 break;
24122 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24123 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24124 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24125 from the parameter-type-list. */
24127 static tree
24128 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24129 tree default_arg, tree parmtype)
24131 cp_token_cache *tokens;
24132 tree parsed_arg;
24133 bool dummy;
24135 if (default_arg == error_mark_node)
24136 return error_mark_node;
24138 /* Push the saved tokens for the default argument onto the parser's
24139 lexer stack. */
24140 tokens = DEFARG_TOKENS (default_arg);
24141 cp_parser_push_lexer_for_tokens (parser, tokens);
24143 start_lambda_scope (decl);
24145 /* Parse the default argument. */
24146 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24147 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24148 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24150 finish_lambda_scope ();
24152 if (parsed_arg == error_mark_node)
24153 cp_parser_skip_to_end_of_statement (parser);
24155 if (!processing_template_decl)
24157 /* In a non-template class, check conversions now. In a template,
24158 we'll wait and instantiate these as needed. */
24159 if (TREE_CODE (decl) == PARM_DECL)
24160 parsed_arg = check_default_argument (parmtype, parsed_arg,
24161 tf_warning_or_error);
24162 else
24163 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24166 /* If the token stream has not been completely used up, then
24167 there was extra junk after the end of the default
24168 argument. */
24169 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24171 if (TREE_CODE (decl) == PARM_DECL)
24172 cp_parser_error (parser, "expected %<,%>");
24173 else
24174 cp_parser_error (parser, "expected %<;%>");
24177 /* Revert to the main lexer. */
24178 cp_parser_pop_lexer (parser);
24180 return parsed_arg;
24183 /* FIELD is a non-static data member with an initializer which we saved for
24184 later; parse it now. */
24186 static void
24187 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24189 tree def;
24191 maybe_begin_member_template_processing (field);
24193 push_unparsed_function_queues (parser);
24194 def = cp_parser_late_parse_one_default_arg (parser, field,
24195 DECL_INITIAL (field),
24196 NULL_TREE);
24197 pop_unparsed_function_queues (parser);
24199 maybe_end_member_template_processing ();
24201 DECL_INITIAL (field) = def;
24204 /* FN is a FUNCTION_DECL which may contains a parameter with an
24205 unparsed DEFAULT_ARG. Parse the default args now. This function
24206 assumes that the current scope is the scope in which the default
24207 argument should be processed. */
24209 static void
24210 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24212 bool saved_local_variables_forbidden_p;
24213 tree parm, parmdecl;
24215 /* While we're parsing the default args, we might (due to the
24216 statement expression extension) encounter more classes. We want
24217 to handle them right away, but we don't want them getting mixed
24218 up with default args that are currently in the queue. */
24219 push_unparsed_function_queues (parser);
24221 /* Local variable names (and the `this' keyword) may not appear
24222 in a default argument. */
24223 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24224 parser->local_variables_forbidden_p = true;
24226 push_defarg_context (fn);
24228 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24229 parmdecl = DECL_ARGUMENTS (fn);
24230 parm && parm != void_list_node;
24231 parm = TREE_CHAIN (parm),
24232 parmdecl = DECL_CHAIN (parmdecl))
24234 tree default_arg = TREE_PURPOSE (parm);
24235 tree parsed_arg;
24236 vec<tree, va_gc> *insts;
24237 tree copy;
24238 unsigned ix;
24240 if (!default_arg)
24241 continue;
24243 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24244 /* This can happen for a friend declaration for a function
24245 already declared with default arguments. */
24246 continue;
24248 parsed_arg
24249 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24250 default_arg,
24251 TREE_VALUE (parm));
24252 if (parsed_arg == error_mark_node)
24254 continue;
24257 TREE_PURPOSE (parm) = parsed_arg;
24259 /* Update any instantiations we've already created. */
24260 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24261 vec_safe_iterate (insts, ix, &copy); ix++)
24262 TREE_PURPOSE (copy) = parsed_arg;
24265 pop_defarg_context ();
24267 /* Make sure no default arg is missing. */
24268 check_default_args (fn);
24270 /* Restore the state of local_variables_forbidden_p. */
24271 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24273 /* Restore the queue. */
24274 pop_unparsed_function_queues (parser);
24277 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24279 sizeof ... ( identifier )
24281 where the 'sizeof' token has already been consumed. */
24283 static tree
24284 cp_parser_sizeof_pack (cp_parser *parser)
24286 /* Consume the `...'. */
24287 cp_lexer_consume_token (parser->lexer);
24288 maybe_warn_variadic_templates ();
24290 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24291 if (paren)
24292 cp_lexer_consume_token (parser->lexer);
24293 else
24294 permerror (cp_lexer_peek_token (parser->lexer)->location,
24295 "%<sizeof...%> argument must be surrounded by parentheses");
24297 cp_token *token = cp_lexer_peek_token (parser->lexer);
24298 tree name = cp_parser_identifier (parser);
24299 if (name == error_mark_node)
24300 return error_mark_node;
24301 /* The name is not qualified. */
24302 parser->scope = NULL_TREE;
24303 parser->qualifying_scope = NULL_TREE;
24304 parser->object_scope = NULL_TREE;
24305 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24306 if (expr == error_mark_node)
24307 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24308 token->location);
24309 if (TREE_CODE (expr) == TYPE_DECL)
24310 expr = TREE_TYPE (expr);
24311 else if (TREE_CODE (expr) == CONST_DECL)
24312 expr = DECL_INITIAL (expr);
24313 expr = make_pack_expansion (expr);
24315 if (paren)
24316 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24318 return expr;
24321 /* Parse the operand of `sizeof' (or a similar operator). Returns
24322 either a TYPE or an expression, depending on the form of the
24323 input. The KEYWORD indicates which kind of expression we have
24324 encountered. */
24326 static tree
24327 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24329 tree expr = NULL_TREE;
24330 const char *saved_message;
24331 char *tmp;
24332 bool saved_integral_constant_expression_p;
24333 bool saved_non_integral_constant_expression_p;
24335 /* If it's a `...', then we are computing the length of a parameter
24336 pack. */
24337 if (keyword == RID_SIZEOF
24338 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24339 return cp_parser_sizeof_pack (parser);
24341 /* Types cannot be defined in a `sizeof' expression. Save away the
24342 old message. */
24343 saved_message = parser->type_definition_forbidden_message;
24344 /* And create the new one. */
24345 tmp = concat ("types may not be defined in %<",
24346 IDENTIFIER_POINTER (ridpointers[keyword]),
24347 "%> expressions", NULL);
24348 parser->type_definition_forbidden_message = tmp;
24350 /* The restrictions on constant-expressions do not apply inside
24351 sizeof expressions. */
24352 saved_integral_constant_expression_p
24353 = parser->integral_constant_expression_p;
24354 saved_non_integral_constant_expression_p
24355 = parser->non_integral_constant_expression_p;
24356 parser->integral_constant_expression_p = false;
24358 /* Do not actually evaluate the expression. */
24359 ++cp_unevaluated_operand;
24360 ++c_inhibit_evaluation_warnings;
24361 /* If it's a `(', then we might be looking at the type-id
24362 construction. */
24363 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24365 tree type = NULL_TREE;
24367 /* We can't be sure yet whether we're looking at a type-id or an
24368 expression. */
24369 cp_parser_parse_tentatively (parser);
24370 /* Note: as a GNU Extension, compound literals are considered
24371 postfix-expressions as they are in C99, so they are valid
24372 arguments to sizeof. See comment in cp_parser_cast_expression
24373 for details. */
24374 if (cp_parser_compound_literal_p (parser))
24375 cp_parser_simulate_error (parser);
24376 else
24378 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24379 parser->in_type_id_in_expr_p = true;
24380 /* Look for the type-id. */
24381 type = cp_parser_type_id (parser);
24382 /* Look for the closing `)'. */
24383 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24384 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24387 /* If all went well, then we're done. */
24388 if (cp_parser_parse_definitely (parser))
24390 cp_decl_specifier_seq decl_specs;
24392 /* Build a trivial decl-specifier-seq. */
24393 clear_decl_specs (&decl_specs);
24394 decl_specs.type = type;
24396 /* Call grokdeclarator to figure out what type this is. */
24397 expr = grokdeclarator (NULL,
24398 &decl_specs,
24399 TYPENAME,
24400 /*initialized=*/0,
24401 /*attrlist=*/NULL);
24405 /* If the type-id production did not work out, then we must be
24406 looking at the unary-expression production. */
24407 if (!expr)
24408 expr = cp_parser_unary_expression (parser);
24410 /* Go back to evaluating expressions. */
24411 --cp_unevaluated_operand;
24412 --c_inhibit_evaluation_warnings;
24414 /* Free the message we created. */
24415 free (tmp);
24416 /* And restore the old one. */
24417 parser->type_definition_forbidden_message = saved_message;
24418 parser->integral_constant_expression_p
24419 = saved_integral_constant_expression_p;
24420 parser->non_integral_constant_expression_p
24421 = saved_non_integral_constant_expression_p;
24423 return expr;
24426 /* If the current declaration has no declarator, return true. */
24428 static bool
24429 cp_parser_declares_only_class_p (cp_parser *parser)
24431 /* If the next token is a `;' or a `,' then there is no
24432 declarator. */
24433 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24434 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24437 /* Update the DECL_SPECS to reflect the storage class indicated by
24438 KEYWORD. */
24440 static void
24441 cp_parser_set_storage_class (cp_parser *parser,
24442 cp_decl_specifier_seq *decl_specs,
24443 enum rid keyword,
24444 cp_token *token)
24446 cp_storage_class storage_class;
24448 if (parser->in_unbraced_linkage_specification_p)
24450 error_at (token->location, "invalid use of %qD in linkage specification",
24451 ridpointers[keyword]);
24452 return;
24454 else if (decl_specs->storage_class != sc_none)
24456 decl_specs->conflicting_specifiers_p = true;
24457 return;
24460 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24461 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24462 && decl_specs->gnu_thread_keyword_p)
24464 pedwarn (decl_specs->locations[ds_thread], 0,
24465 "%<__thread%> before %qD", ridpointers[keyword]);
24468 switch (keyword)
24470 case RID_AUTO:
24471 storage_class = sc_auto;
24472 break;
24473 case RID_REGISTER:
24474 storage_class = sc_register;
24475 break;
24476 case RID_STATIC:
24477 storage_class = sc_static;
24478 break;
24479 case RID_EXTERN:
24480 storage_class = sc_extern;
24481 break;
24482 case RID_MUTABLE:
24483 storage_class = sc_mutable;
24484 break;
24485 default:
24486 gcc_unreachable ();
24488 decl_specs->storage_class = storage_class;
24489 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24491 /* A storage class specifier cannot be applied alongside a typedef
24492 specifier. If there is a typedef specifier present then set
24493 conflicting_specifiers_p which will trigger an error later
24494 on in grokdeclarator. */
24495 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24496 decl_specs->conflicting_specifiers_p = true;
24499 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24500 is true, the type is a class or enum definition. */
24502 static void
24503 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24504 tree type_spec,
24505 cp_token *token,
24506 bool type_definition_p)
24508 decl_specs->any_specifiers_p = true;
24510 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24511 (with, for example, in "typedef int wchar_t;") we remember that
24512 this is what happened. In system headers, we ignore these
24513 declarations so that G++ can work with system headers that are not
24514 C++-safe. */
24515 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24516 && !type_definition_p
24517 && (type_spec == boolean_type_node
24518 || type_spec == char16_type_node
24519 || type_spec == char32_type_node
24520 || type_spec == wchar_type_node)
24521 && (decl_specs->type
24522 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24523 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24524 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24525 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24527 decl_specs->redefined_builtin_type = type_spec;
24528 set_and_check_decl_spec_loc (decl_specs,
24529 ds_redefined_builtin_type_spec,
24530 token);
24531 if (!decl_specs->type)
24533 decl_specs->type = type_spec;
24534 decl_specs->type_definition_p = false;
24535 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24538 else if (decl_specs->type)
24539 decl_specs->multiple_types_p = true;
24540 else
24542 decl_specs->type = type_spec;
24543 decl_specs->type_definition_p = type_definition_p;
24544 decl_specs->redefined_builtin_type = NULL_TREE;
24545 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24549 /* True iff TOKEN is the GNU keyword __thread. */
24551 static bool
24552 token_is__thread (cp_token *token)
24554 gcc_assert (token->keyword == RID_THREAD);
24555 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24558 /* Set the location for a declarator specifier and check if it is
24559 duplicated.
24561 DECL_SPECS is the sequence of declarator specifiers onto which to
24562 set the location.
24564 DS is the single declarator specifier to set which location is to
24565 be set onto the existing sequence of declarators.
24567 LOCATION is the location for the declarator specifier to
24568 consider. */
24570 static void
24571 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24572 cp_decl_spec ds, cp_token *token)
24574 gcc_assert (ds < ds_last);
24576 if (decl_specs == NULL)
24577 return;
24579 source_location location = token->location;
24581 if (decl_specs->locations[ds] == 0)
24583 decl_specs->locations[ds] = location;
24584 if (ds == ds_thread)
24585 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24587 else
24589 if (ds == ds_long)
24591 if (decl_specs->locations[ds_long_long] != 0)
24592 error_at (location,
24593 "%<long long long%> is too long for GCC");
24594 else
24596 decl_specs->locations[ds_long_long] = location;
24597 pedwarn_cxx98 (location,
24598 OPT_Wlong_long,
24599 "ISO C++ 1998 does not support %<long long%>");
24602 else if (ds == ds_thread)
24604 bool gnu = token_is__thread (token);
24605 if (gnu != decl_specs->gnu_thread_keyword_p)
24606 error_at (location,
24607 "both %<__thread%> and %<thread_local%> specified");
24608 else
24609 error_at (location, "duplicate %qD", token->u.value);
24611 else
24613 static const char *const decl_spec_names[] = {
24614 "signed",
24615 "unsigned",
24616 "short",
24617 "long",
24618 "const",
24619 "volatile",
24620 "restrict",
24621 "inline",
24622 "virtual",
24623 "explicit",
24624 "friend",
24625 "typedef",
24626 "using",
24627 "constexpr",
24628 "__complex"
24630 error_at (location,
24631 "duplicate %qs", decl_spec_names[ds]);
24636 /* Return true iff the declarator specifier DS is present in the
24637 sequence of declarator specifiers DECL_SPECS. */
24639 bool
24640 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24641 cp_decl_spec ds)
24643 gcc_assert (ds < ds_last);
24645 if (decl_specs == NULL)
24646 return false;
24648 return decl_specs->locations[ds] != 0;
24651 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24652 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24654 static bool
24655 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24657 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24660 /* Issue an error message indicating that TOKEN_DESC was expected.
24661 If KEYWORD is true, it indicated this function is called by
24662 cp_parser_require_keword and the required token can only be
24663 a indicated keyword. */
24665 static void
24666 cp_parser_required_error (cp_parser *parser,
24667 required_token token_desc,
24668 bool keyword)
24670 switch (token_desc)
24672 case RT_NEW:
24673 cp_parser_error (parser, "expected %<new%>");
24674 return;
24675 case RT_DELETE:
24676 cp_parser_error (parser, "expected %<delete%>");
24677 return;
24678 case RT_RETURN:
24679 cp_parser_error (parser, "expected %<return%>");
24680 return;
24681 case RT_WHILE:
24682 cp_parser_error (parser, "expected %<while%>");
24683 return;
24684 case RT_EXTERN:
24685 cp_parser_error (parser, "expected %<extern%>");
24686 return;
24687 case RT_STATIC_ASSERT:
24688 cp_parser_error (parser, "expected %<static_assert%>");
24689 return;
24690 case RT_DECLTYPE:
24691 cp_parser_error (parser, "expected %<decltype%>");
24692 return;
24693 case RT_OPERATOR:
24694 cp_parser_error (parser, "expected %<operator%>");
24695 return;
24696 case RT_CLASS:
24697 cp_parser_error (parser, "expected %<class%>");
24698 return;
24699 case RT_TEMPLATE:
24700 cp_parser_error (parser, "expected %<template%>");
24701 return;
24702 case RT_NAMESPACE:
24703 cp_parser_error (parser, "expected %<namespace%>");
24704 return;
24705 case RT_USING:
24706 cp_parser_error (parser, "expected %<using%>");
24707 return;
24708 case RT_ASM:
24709 cp_parser_error (parser, "expected %<asm%>");
24710 return;
24711 case RT_TRY:
24712 cp_parser_error (parser, "expected %<try%>");
24713 return;
24714 case RT_CATCH:
24715 cp_parser_error (parser, "expected %<catch%>");
24716 return;
24717 case RT_THROW:
24718 cp_parser_error (parser, "expected %<throw%>");
24719 return;
24720 case RT_LABEL:
24721 cp_parser_error (parser, "expected %<__label__%>");
24722 return;
24723 case RT_AT_TRY:
24724 cp_parser_error (parser, "expected %<@try%>");
24725 return;
24726 case RT_AT_SYNCHRONIZED:
24727 cp_parser_error (parser, "expected %<@synchronized%>");
24728 return;
24729 case RT_AT_THROW:
24730 cp_parser_error (parser, "expected %<@throw%>");
24731 return;
24732 case RT_TRANSACTION_ATOMIC:
24733 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24734 return;
24735 case RT_TRANSACTION_RELAXED:
24736 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24737 return;
24738 default:
24739 break;
24741 if (!keyword)
24743 switch (token_desc)
24745 case RT_SEMICOLON:
24746 cp_parser_error (parser, "expected %<;%>");
24747 return;
24748 case RT_OPEN_PAREN:
24749 cp_parser_error (parser, "expected %<(%>");
24750 return;
24751 case RT_CLOSE_BRACE:
24752 cp_parser_error (parser, "expected %<}%>");
24753 return;
24754 case RT_OPEN_BRACE:
24755 cp_parser_error (parser, "expected %<{%>");
24756 return;
24757 case RT_CLOSE_SQUARE:
24758 cp_parser_error (parser, "expected %<]%>");
24759 return;
24760 case RT_OPEN_SQUARE:
24761 cp_parser_error (parser, "expected %<[%>");
24762 return;
24763 case RT_COMMA:
24764 cp_parser_error (parser, "expected %<,%>");
24765 return;
24766 case RT_SCOPE:
24767 cp_parser_error (parser, "expected %<::%>");
24768 return;
24769 case RT_LESS:
24770 cp_parser_error (parser, "expected %<<%>");
24771 return;
24772 case RT_GREATER:
24773 cp_parser_error (parser, "expected %<>%>");
24774 return;
24775 case RT_EQ:
24776 cp_parser_error (parser, "expected %<=%>");
24777 return;
24778 case RT_ELLIPSIS:
24779 cp_parser_error (parser, "expected %<...%>");
24780 return;
24781 case RT_MULT:
24782 cp_parser_error (parser, "expected %<*%>");
24783 return;
24784 case RT_COMPL:
24785 cp_parser_error (parser, "expected %<~%>");
24786 return;
24787 case RT_COLON:
24788 cp_parser_error (parser, "expected %<:%>");
24789 return;
24790 case RT_COLON_SCOPE:
24791 cp_parser_error (parser, "expected %<:%> or %<::%>");
24792 return;
24793 case RT_CLOSE_PAREN:
24794 cp_parser_error (parser, "expected %<)%>");
24795 return;
24796 case RT_COMMA_CLOSE_PAREN:
24797 cp_parser_error (parser, "expected %<,%> or %<)%>");
24798 return;
24799 case RT_PRAGMA_EOL:
24800 cp_parser_error (parser, "expected end of line");
24801 return;
24802 case RT_NAME:
24803 cp_parser_error (parser, "expected identifier");
24804 return;
24805 case RT_SELECT:
24806 cp_parser_error (parser, "expected selection-statement");
24807 return;
24808 case RT_INTERATION:
24809 cp_parser_error (parser, "expected iteration-statement");
24810 return;
24811 case RT_JUMP:
24812 cp_parser_error (parser, "expected jump-statement");
24813 return;
24814 case RT_CLASS_KEY:
24815 cp_parser_error (parser, "expected class-key");
24816 return;
24817 case RT_CLASS_TYPENAME_TEMPLATE:
24818 cp_parser_error (parser,
24819 "expected %<class%>, %<typename%>, or %<template%>");
24820 return;
24821 default:
24822 gcc_unreachable ();
24825 else
24826 gcc_unreachable ();
24831 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24832 issue an error message indicating that TOKEN_DESC was expected.
24834 Returns the token consumed, if the token had the appropriate type.
24835 Otherwise, returns NULL. */
24837 static cp_token *
24838 cp_parser_require (cp_parser* parser,
24839 enum cpp_ttype type,
24840 required_token token_desc)
24842 if (cp_lexer_next_token_is (parser->lexer, type))
24843 return cp_lexer_consume_token (parser->lexer);
24844 else
24846 /* Output the MESSAGE -- unless we're parsing tentatively. */
24847 if (!cp_parser_simulate_error (parser))
24848 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24849 return NULL;
24853 /* An error message is produced if the next token is not '>'.
24854 All further tokens are skipped until the desired token is
24855 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24857 static void
24858 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24860 /* Current level of '< ... >'. */
24861 unsigned level = 0;
24862 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24863 unsigned nesting_depth = 0;
24865 /* Are we ready, yet? If not, issue error message. */
24866 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24867 return;
24869 /* Skip tokens until the desired token is found. */
24870 while (true)
24872 /* Peek at the next token. */
24873 switch (cp_lexer_peek_token (parser->lexer)->type)
24875 case CPP_LESS:
24876 if (!nesting_depth)
24877 ++level;
24878 break;
24880 case CPP_RSHIFT:
24881 if (cxx_dialect == cxx98)
24882 /* C++0x views the `>>' operator as two `>' tokens, but
24883 C++98 does not. */
24884 break;
24885 else if (!nesting_depth && level-- == 0)
24887 /* We've hit a `>>' where the first `>' closes the
24888 template argument list, and the second `>' is
24889 spurious. Just consume the `>>' and stop; we've
24890 already produced at least one error. */
24891 cp_lexer_consume_token (parser->lexer);
24892 return;
24894 /* Fall through for C++0x, so we handle the second `>' in
24895 the `>>'. */
24897 case CPP_GREATER:
24898 if (!nesting_depth && level-- == 0)
24900 /* We've reached the token we want, consume it and stop. */
24901 cp_lexer_consume_token (parser->lexer);
24902 return;
24904 break;
24906 case CPP_OPEN_PAREN:
24907 case CPP_OPEN_SQUARE:
24908 ++nesting_depth;
24909 break;
24911 case CPP_CLOSE_PAREN:
24912 case CPP_CLOSE_SQUARE:
24913 if (nesting_depth-- == 0)
24914 return;
24915 break;
24917 case CPP_EOF:
24918 case CPP_PRAGMA_EOL:
24919 case CPP_SEMICOLON:
24920 case CPP_OPEN_BRACE:
24921 case CPP_CLOSE_BRACE:
24922 /* The '>' was probably forgotten, don't look further. */
24923 return;
24925 default:
24926 break;
24929 /* Consume this token. */
24930 cp_lexer_consume_token (parser->lexer);
24934 /* If the next token is the indicated keyword, consume it. Otherwise,
24935 issue an error message indicating that TOKEN_DESC was expected.
24937 Returns the token consumed, if the token had the appropriate type.
24938 Otherwise, returns NULL. */
24940 static cp_token *
24941 cp_parser_require_keyword (cp_parser* parser,
24942 enum rid keyword,
24943 required_token token_desc)
24945 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24947 if (token && token->keyword != keyword)
24949 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24950 return NULL;
24953 return token;
24956 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24957 function-definition. */
24959 static bool
24960 cp_parser_token_starts_function_definition_p (cp_token* token)
24962 return (/* An ordinary function-body begins with an `{'. */
24963 token->type == CPP_OPEN_BRACE
24964 /* A ctor-initializer begins with a `:'. */
24965 || token->type == CPP_COLON
24966 /* A function-try-block begins with `try'. */
24967 || token->keyword == RID_TRY
24968 /* A function-transaction-block begins with `__transaction_atomic'
24969 or `__transaction_relaxed'. */
24970 || token->keyword == RID_TRANSACTION_ATOMIC
24971 || token->keyword == RID_TRANSACTION_RELAXED
24972 /* The named return value extension begins with `return'. */
24973 || token->keyword == RID_RETURN);
24976 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24977 definition. */
24979 static bool
24980 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24982 cp_token *token;
24984 token = cp_lexer_peek_token (parser->lexer);
24985 return (token->type == CPP_OPEN_BRACE
24986 || (token->type == CPP_COLON
24987 && !parser->colon_doesnt_start_class_def_p));
24990 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24991 C++0x) ending a template-argument. */
24993 static bool
24994 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24996 cp_token *token;
24998 token = cp_lexer_peek_token (parser->lexer);
24999 return (token->type == CPP_COMMA
25000 || token->type == CPP_GREATER
25001 || token->type == CPP_ELLIPSIS
25002 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25005 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25006 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25008 static bool
25009 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25010 size_t n)
25012 cp_token *token;
25014 token = cp_lexer_peek_nth_token (parser->lexer, n);
25015 if (token->type == CPP_LESS)
25016 return true;
25017 /* Check for the sequence `<::' in the original code. It would be lexed as
25018 `[:', where `[' is a digraph, and there is no whitespace before
25019 `:'. */
25020 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25022 cp_token *token2;
25023 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25024 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25025 return true;
25027 return false;
25030 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25031 or none_type otherwise. */
25033 static enum tag_types
25034 cp_parser_token_is_class_key (cp_token* token)
25036 switch (token->keyword)
25038 case RID_CLASS:
25039 return class_type;
25040 case RID_STRUCT:
25041 return record_type;
25042 case RID_UNION:
25043 return union_type;
25045 default:
25046 return none_type;
25050 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25051 or none_type otherwise or if the token is null. */
25053 static enum tag_types
25054 cp_parser_token_is_type_parameter_key (cp_token* token)
25056 if (!token)
25057 return none_type;
25059 switch (token->keyword)
25061 case RID_CLASS:
25062 return class_type;
25063 case RID_TYPENAME:
25064 return typename_type;
25066 default:
25067 return none_type;
25071 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25073 static void
25074 cp_parser_check_class_key (enum tag_types class_key, tree type)
25076 if (type == error_mark_node)
25077 return;
25078 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25080 if (permerror (input_location, "%qs tag used in naming %q#T",
25081 class_key == union_type ? "union"
25082 : class_key == record_type ? "struct" : "class",
25083 type))
25084 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25085 "%q#T was previously declared here", type);
25089 /* Issue an error message if DECL is redeclared with different
25090 access than its original declaration [class.access.spec/3].
25091 This applies to nested classes and nested class templates.
25092 [class.mem/1]. */
25094 static void
25095 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25097 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25098 return;
25100 if ((TREE_PRIVATE (decl)
25101 != (current_access_specifier == access_private_node))
25102 || (TREE_PROTECTED (decl)
25103 != (current_access_specifier == access_protected_node)))
25104 error_at (location, "%qD redeclared with different access", decl);
25107 /* Look for the `template' keyword, as a syntactic disambiguator.
25108 Return TRUE iff it is present, in which case it will be
25109 consumed. */
25111 static bool
25112 cp_parser_optional_template_keyword (cp_parser *parser)
25114 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25116 /* In C++98 the `template' keyword can only be used within templates;
25117 outside templates the parser can always figure out what is a
25118 template and what is not. In C++11, per the resolution of DR 468,
25119 `template' is allowed in cases where it is not strictly necessary. */
25120 if (!processing_template_decl
25121 && pedantic && cxx_dialect == cxx98)
25123 cp_token *token = cp_lexer_peek_token (parser->lexer);
25124 pedwarn (token->location, OPT_Wpedantic,
25125 "in C++98 %<template%> (as a disambiguator) is only "
25126 "allowed within templates");
25127 /* If this part of the token stream is rescanned, the same
25128 error message would be generated. So, we purge the token
25129 from the stream. */
25130 cp_lexer_purge_token (parser->lexer);
25131 return false;
25133 else
25135 /* Consume the `template' keyword. */
25136 cp_lexer_consume_token (parser->lexer);
25137 return true;
25140 return false;
25143 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25144 set PARSER->SCOPE, and perform other related actions. */
25146 static void
25147 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25149 int i;
25150 struct tree_check *check_value;
25151 deferred_access_check *chk;
25152 vec<deferred_access_check, va_gc> *checks;
25154 /* Get the stored value. */
25155 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25156 /* Perform any access checks that were deferred. */
25157 checks = check_value->checks;
25158 if (checks)
25160 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25161 perform_or_defer_access_check (chk->binfo,
25162 chk->decl,
25163 chk->diag_decl, tf_warning_or_error);
25165 /* Set the scope from the stored value. */
25166 parser->scope = check_value->value;
25167 parser->qualifying_scope = check_value->qualifying_scope;
25168 parser->object_scope = NULL_TREE;
25171 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25172 encounter the end of a block before what we were looking for. */
25174 static bool
25175 cp_parser_cache_group (cp_parser *parser,
25176 enum cpp_ttype end,
25177 unsigned depth)
25179 while (true)
25181 cp_token *token = cp_lexer_peek_token (parser->lexer);
25183 /* Abort a parenthesized expression if we encounter a semicolon. */
25184 if ((end == CPP_CLOSE_PAREN || depth == 0)
25185 && token->type == CPP_SEMICOLON)
25186 return true;
25187 /* If we've reached the end of the file, stop. */
25188 if (token->type == CPP_EOF
25189 || (end != CPP_PRAGMA_EOL
25190 && token->type == CPP_PRAGMA_EOL))
25191 return true;
25192 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25193 /* We've hit the end of an enclosing block, so there's been some
25194 kind of syntax error. */
25195 return true;
25197 /* Consume the token. */
25198 cp_lexer_consume_token (parser->lexer);
25199 /* See if it starts a new group. */
25200 if (token->type == CPP_OPEN_BRACE)
25202 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25203 /* In theory this should probably check end == '}', but
25204 cp_parser_save_member_function_body needs it to exit
25205 after either '}' or ')' when called with ')'. */
25206 if (depth == 0)
25207 return false;
25209 else if (token->type == CPP_OPEN_PAREN)
25211 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25212 if (depth == 0 && end == CPP_CLOSE_PAREN)
25213 return false;
25215 else if (token->type == CPP_PRAGMA)
25216 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25217 else if (token->type == end)
25218 return false;
25222 /* Like above, for caching a default argument or NSDMI. Both of these are
25223 terminated by a non-nested comma, but it can be unclear whether or not a
25224 comma is nested in a template argument list unless we do more parsing.
25225 In order to handle this ambiguity, when we encounter a ',' after a '<'
25226 we try to parse what follows as a parameter-declaration-list (in the
25227 case of a default argument) or a member-declarator (in the case of an
25228 NSDMI). If that succeeds, then we stop caching. */
25230 static tree
25231 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25233 unsigned depth = 0;
25234 int maybe_template_id = 0;
25235 cp_token *first_token;
25236 cp_token *token;
25237 tree default_argument;
25239 /* Add tokens until we have processed the entire default
25240 argument. We add the range [first_token, token). */
25241 first_token = cp_lexer_peek_token (parser->lexer);
25242 if (first_token->type == CPP_OPEN_BRACE)
25244 /* For list-initialization, this is straightforward. */
25245 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25246 token = cp_lexer_peek_token (parser->lexer);
25248 else while (true)
25250 bool done = false;
25252 /* Peek at the next token. */
25253 token = cp_lexer_peek_token (parser->lexer);
25254 /* What we do depends on what token we have. */
25255 switch (token->type)
25257 /* In valid code, a default argument must be
25258 immediately followed by a `,' `)', or `...'. */
25259 case CPP_COMMA:
25260 if (depth == 0 && maybe_template_id)
25262 /* If we've seen a '<', we might be in a
25263 template-argument-list. Until Core issue 325 is
25264 resolved, we don't know how this situation ought
25265 to be handled, so try to DTRT. We check whether
25266 what comes after the comma is a valid parameter
25267 declaration list. If it is, then the comma ends
25268 the default argument; otherwise the default
25269 argument continues. */
25270 bool error = false;
25272 /* Set ITALP so cp_parser_parameter_declaration_list
25273 doesn't decide to commit to this parse. */
25274 bool saved_italp = parser->in_template_argument_list_p;
25275 parser->in_template_argument_list_p = true;
25277 cp_parser_parse_tentatively (parser);
25278 cp_lexer_consume_token (parser->lexer);
25280 if (nsdmi)
25282 int ctor_dtor_or_conv_p;
25283 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25284 &ctor_dtor_or_conv_p,
25285 /*parenthesized_p=*/NULL,
25286 /*member_p=*/true,
25287 /*friend_p=*/false);
25289 else
25291 begin_scope (sk_function_parms, NULL_TREE);
25292 cp_parser_parameter_declaration_list (parser, &error);
25293 pop_bindings_and_leave_scope ();
25295 if (!cp_parser_error_occurred (parser) && !error)
25296 done = true;
25297 cp_parser_abort_tentative_parse (parser);
25299 parser->in_template_argument_list_p = saved_italp;
25300 break;
25302 case CPP_CLOSE_PAREN:
25303 case CPP_ELLIPSIS:
25304 /* If we run into a non-nested `;', `}', or `]',
25305 then the code is invalid -- but the default
25306 argument is certainly over. */
25307 case CPP_SEMICOLON:
25308 case CPP_CLOSE_BRACE:
25309 case CPP_CLOSE_SQUARE:
25310 if (depth == 0
25311 /* Handle correctly int n = sizeof ... ( p ); */
25312 && token->type != CPP_ELLIPSIS)
25313 done = true;
25314 /* Update DEPTH, if necessary. */
25315 else if (token->type == CPP_CLOSE_PAREN
25316 || token->type == CPP_CLOSE_BRACE
25317 || token->type == CPP_CLOSE_SQUARE)
25318 --depth;
25319 break;
25321 case CPP_OPEN_PAREN:
25322 case CPP_OPEN_SQUARE:
25323 case CPP_OPEN_BRACE:
25324 ++depth;
25325 break;
25327 case CPP_LESS:
25328 if (depth == 0)
25329 /* This might be the comparison operator, or it might
25330 start a template argument list. */
25331 ++maybe_template_id;
25332 break;
25334 case CPP_RSHIFT:
25335 if (cxx_dialect == cxx98)
25336 break;
25337 /* Fall through for C++0x, which treats the `>>'
25338 operator like two `>' tokens in certain
25339 cases. */
25341 case CPP_GREATER:
25342 if (depth == 0)
25344 /* This might be an operator, or it might close a
25345 template argument list. But if a previous '<'
25346 started a template argument list, this will have
25347 closed it, so we can't be in one anymore. */
25348 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25349 if (maybe_template_id < 0)
25350 maybe_template_id = 0;
25352 break;
25354 /* If we run out of tokens, issue an error message. */
25355 case CPP_EOF:
25356 case CPP_PRAGMA_EOL:
25357 error_at (token->location, "file ends in default argument");
25358 done = true;
25359 break;
25361 case CPP_NAME:
25362 case CPP_SCOPE:
25363 /* In these cases, we should look for template-ids.
25364 For example, if the default argument is
25365 `X<int, double>()', we need to do name lookup to
25366 figure out whether or not `X' is a template; if
25367 so, the `,' does not end the default argument.
25369 That is not yet done. */
25370 break;
25372 default:
25373 break;
25376 /* If we've reached the end, stop. */
25377 if (done)
25378 break;
25380 /* Add the token to the token block. */
25381 token = cp_lexer_consume_token (parser->lexer);
25384 /* Create a DEFAULT_ARG to represent the unparsed default
25385 argument. */
25386 default_argument = make_node (DEFAULT_ARG);
25387 DEFARG_TOKENS (default_argument)
25388 = cp_token_cache_new (first_token, token);
25389 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25391 return default_argument;
25394 /* Begin parsing tentatively. We always save tokens while parsing
25395 tentatively so that if the tentative parsing fails we can restore the
25396 tokens. */
25398 static void
25399 cp_parser_parse_tentatively (cp_parser* parser)
25401 /* Enter a new parsing context. */
25402 parser->context = cp_parser_context_new (parser->context);
25403 /* Begin saving tokens. */
25404 cp_lexer_save_tokens (parser->lexer);
25405 /* In order to avoid repetitive access control error messages,
25406 access checks are queued up until we are no longer parsing
25407 tentatively. */
25408 push_deferring_access_checks (dk_deferred);
25411 /* Commit to the currently active tentative parse. */
25413 static void
25414 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25416 cp_parser_context *context;
25417 cp_lexer *lexer;
25419 /* Mark all of the levels as committed. */
25420 lexer = parser->lexer;
25421 for (context = parser->context; context->next; context = context->next)
25423 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25424 break;
25425 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25426 while (!cp_lexer_saving_tokens (lexer))
25427 lexer = lexer->next;
25428 cp_lexer_commit_tokens (lexer);
25432 /* Commit to the topmost currently active tentative parse.
25434 Note that this function shouldn't be called when there are
25435 irreversible side-effects while in a tentative state. For
25436 example, we shouldn't create a permanent entry in the symbol
25437 table, or issue an error message that might not apply if the
25438 tentative parse is aborted. */
25440 static void
25441 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25443 cp_parser_context *context = parser->context;
25444 cp_lexer *lexer = parser->lexer;
25446 if (context)
25448 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25449 return;
25450 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25452 while (!cp_lexer_saving_tokens (lexer))
25453 lexer = lexer->next;
25454 cp_lexer_commit_tokens (lexer);
25458 /* Abort the currently active tentative parse. All consumed tokens
25459 will be rolled back, and no diagnostics will be issued. */
25461 static void
25462 cp_parser_abort_tentative_parse (cp_parser* parser)
25464 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25465 || errorcount > 0);
25466 cp_parser_simulate_error (parser);
25467 /* Now, pretend that we want to see if the construct was
25468 successfully parsed. */
25469 cp_parser_parse_definitely (parser);
25472 /* Stop parsing tentatively. If a parse error has occurred, restore the
25473 token stream. Otherwise, commit to the tokens we have consumed.
25474 Returns true if no error occurred; false otherwise. */
25476 static bool
25477 cp_parser_parse_definitely (cp_parser* parser)
25479 bool error_occurred;
25480 cp_parser_context *context;
25482 /* Remember whether or not an error occurred, since we are about to
25483 destroy that information. */
25484 error_occurred = cp_parser_error_occurred (parser);
25485 /* Remove the topmost context from the stack. */
25486 context = parser->context;
25487 parser->context = context->next;
25488 /* If no parse errors occurred, commit to the tentative parse. */
25489 if (!error_occurred)
25491 /* Commit to the tokens read tentatively, unless that was
25492 already done. */
25493 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25494 cp_lexer_commit_tokens (parser->lexer);
25496 pop_to_parent_deferring_access_checks ();
25498 /* Otherwise, if errors occurred, roll back our state so that things
25499 are just as they were before we began the tentative parse. */
25500 else
25502 cp_lexer_rollback_tokens (parser->lexer);
25503 pop_deferring_access_checks ();
25505 /* Add the context to the front of the free list. */
25506 context->next = cp_parser_context_free_list;
25507 cp_parser_context_free_list = context;
25509 return !error_occurred;
25512 /* Returns true if we are parsing tentatively and are not committed to
25513 this tentative parse. */
25515 static bool
25516 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25518 return (cp_parser_parsing_tentatively (parser)
25519 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25522 /* Returns nonzero iff an error has occurred during the most recent
25523 tentative parse. */
25525 static bool
25526 cp_parser_error_occurred (cp_parser* parser)
25528 return (cp_parser_parsing_tentatively (parser)
25529 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25532 /* Returns nonzero if GNU extensions are allowed. */
25534 static bool
25535 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25537 return parser->allow_gnu_extensions_p;
25540 /* Objective-C++ Productions */
25543 /* Parse an Objective-C expression, which feeds into a primary-expression
25544 above.
25546 objc-expression:
25547 objc-message-expression
25548 objc-string-literal
25549 objc-encode-expression
25550 objc-protocol-expression
25551 objc-selector-expression
25553 Returns a tree representation of the expression. */
25555 static tree
25556 cp_parser_objc_expression (cp_parser* parser)
25558 /* Try to figure out what kind of declaration is present. */
25559 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25561 switch (kwd->type)
25563 case CPP_OPEN_SQUARE:
25564 return cp_parser_objc_message_expression (parser);
25566 case CPP_OBJC_STRING:
25567 kwd = cp_lexer_consume_token (parser->lexer);
25568 return objc_build_string_object (kwd->u.value);
25570 case CPP_KEYWORD:
25571 switch (kwd->keyword)
25573 case RID_AT_ENCODE:
25574 return cp_parser_objc_encode_expression (parser);
25576 case RID_AT_PROTOCOL:
25577 return cp_parser_objc_protocol_expression (parser);
25579 case RID_AT_SELECTOR:
25580 return cp_parser_objc_selector_expression (parser);
25582 default:
25583 break;
25585 default:
25586 error_at (kwd->location,
25587 "misplaced %<@%D%> Objective-C++ construct",
25588 kwd->u.value);
25589 cp_parser_skip_to_end_of_block_or_statement (parser);
25592 return error_mark_node;
25595 /* Parse an Objective-C message expression.
25597 objc-message-expression:
25598 [ objc-message-receiver objc-message-args ]
25600 Returns a representation of an Objective-C message. */
25602 static tree
25603 cp_parser_objc_message_expression (cp_parser* parser)
25605 tree receiver, messageargs;
25607 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25608 receiver = cp_parser_objc_message_receiver (parser);
25609 messageargs = cp_parser_objc_message_args (parser);
25610 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25612 return objc_build_message_expr (receiver, messageargs);
25615 /* Parse an objc-message-receiver.
25617 objc-message-receiver:
25618 expression
25619 simple-type-specifier
25621 Returns a representation of the type or expression. */
25623 static tree
25624 cp_parser_objc_message_receiver (cp_parser* parser)
25626 tree rcv;
25628 /* An Objective-C message receiver may be either (1) a type
25629 or (2) an expression. */
25630 cp_parser_parse_tentatively (parser);
25631 rcv = cp_parser_expression (parser);
25633 if (cp_parser_parse_definitely (parser))
25634 return rcv;
25636 rcv = cp_parser_simple_type_specifier (parser,
25637 /*decl_specs=*/NULL,
25638 CP_PARSER_FLAGS_NONE);
25640 return objc_get_class_reference (rcv);
25643 /* Parse the arguments and selectors comprising an Objective-C message.
25645 objc-message-args:
25646 objc-selector
25647 objc-selector-args
25648 objc-selector-args , objc-comma-args
25650 objc-selector-args:
25651 objc-selector [opt] : assignment-expression
25652 objc-selector-args objc-selector [opt] : assignment-expression
25654 objc-comma-args:
25655 assignment-expression
25656 objc-comma-args , assignment-expression
25658 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25659 selector arguments and TREE_VALUE containing a list of comma
25660 arguments. */
25662 static tree
25663 cp_parser_objc_message_args (cp_parser* parser)
25665 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25666 bool maybe_unary_selector_p = true;
25667 cp_token *token = cp_lexer_peek_token (parser->lexer);
25669 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25671 tree selector = NULL_TREE, arg;
25673 if (token->type != CPP_COLON)
25674 selector = cp_parser_objc_selector (parser);
25676 /* Detect if we have a unary selector. */
25677 if (maybe_unary_selector_p
25678 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25679 return build_tree_list (selector, NULL_TREE);
25681 maybe_unary_selector_p = false;
25682 cp_parser_require (parser, CPP_COLON, RT_COLON);
25683 arg = cp_parser_assignment_expression (parser);
25685 sel_args
25686 = chainon (sel_args,
25687 build_tree_list (selector, arg));
25689 token = cp_lexer_peek_token (parser->lexer);
25692 /* Handle non-selector arguments, if any. */
25693 while (token->type == CPP_COMMA)
25695 tree arg;
25697 cp_lexer_consume_token (parser->lexer);
25698 arg = cp_parser_assignment_expression (parser);
25700 addl_args
25701 = chainon (addl_args,
25702 build_tree_list (NULL_TREE, arg));
25704 token = cp_lexer_peek_token (parser->lexer);
25707 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25709 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25710 return build_tree_list (error_mark_node, error_mark_node);
25713 return build_tree_list (sel_args, addl_args);
25716 /* Parse an Objective-C encode expression.
25718 objc-encode-expression:
25719 @encode objc-typename
25721 Returns an encoded representation of the type argument. */
25723 static tree
25724 cp_parser_objc_encode_expression (cp_parser* parser)
25726 tree type;
25727 cp_token *token;
25729 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25730 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25731 token = cp_lexer_peek_token (parser->lexer);
25732 type = complete_type (cp_parser_type_id (parser));
25733 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25735 if (!type)
25737 error_at (token->location,
25738 "%<@encode%> must specify a type as an argument");
25739 return error_mark_node;
25742 /* This happens if we find @encode(T) (where T is a template
25743 typename or something dependent on a template typename) when
25744 parsing a template. In that case, we can't compile it
25745 immediately, but we rather create an AT_ENCODE_EXPR which will
25746 need to be instantiated when the template is used.
25748 if (dependent_type_p (type))
25750 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25751 TREE_READONLY (value) = 1;
25752 return value;
25755 return objc_build_encode_expr (type);
25758 /* Parse an Objective-C @defs expression. */
25760 static tree
25761 cp_parser_objc_defs_expression (cp_parser *parser)
25763 tree name;
25765 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25766 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25767 name = cp_parser_identifier (parser);
25768 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25770 return objc_get_class_ivars (name);
25773 /* Parse an Objective-C protocol expression.
25775 objc-protocol-expression:
25776 @protocol ( identifier )
25778 Returns a representation of the protocol expression. */
25780 static tree
25781 cp_parser_objc_protocol_expression (cp_parser* parser)
25783 tree proto;
25785 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25786 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25787 proto = cp_parser_identifier (parser);
25788 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25790 return objc_build_protocol_expr (proto);
25793 /* Parse an Objective-C selector expression.
25795 objc-selector-expression:
25796 @selector ( objc-method-signature )
25798 objc-method-signature:
25799 objc-selector
25800 objc-selector-seq
25802 objc-selector-seq:
25803 objc-selector :
25804 objc-selector-seq objc-selector :
25806 Returns a representation of the method selector. */
25808 static tree
25809 cp_parser_objc_selector_expression (cp_parser* parser)
25811 tree sel_seq = NULL_TREE;
25812 bool maybe_unary_selector_p = true;
25813 cp_token *token;
25814 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25816 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25817 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25818 token = cp_lexer_peek_token (parser->lexer);
25820 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25821 || token->type == CPP_SCOPE)
25823 tree selector = NULL_TREE;
25825 if (token->type != CPP_COLON
25826 || token->type == CPP_SCOPE)
25827 selector = cp_parser_objc_selector (parser);
25829 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25830 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25832 /* Detect if we have a unary selector. */
25833 if (maybe_unary_selector_p)
25835 sel_seq = selector;
25836 goto finish_selector;
25838 else
25840 cp_parser_error (parser, "expected %<:%>");
25843 maybe_unary_selector_p = false;
25844 token = cp_lexer_consume_token (parser->lexer);
25846 if (token->type == CPP_SCOPE)
25848 sel_seq
25849 = chainon (sel_seq,
25850 build_tree_list (selector, NULL_TREE));
25851 sel_seq
25852 = chainon (sel_seq,
25853 build_tree_list (NULL_TREE, NULL_TREE));
25855 else
25856 sel_seq
25857 = chainon (sel_seq,
25858 build_tree_list (selector, NULL_TREE));
25860 token = cp_lexer_peek_token (parser->lexer);
25863 finish_selector:
25864 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25866 return objc_build_selector_expr (loc, sel_seq);
25869 /* Parse a list of identifiers.
25871 objc-identifier-list:
25872 identifier
25873 objc-identifier-list , identifier
25875 Returns a TREE_LIST of identifier nodes. */
25877 static tree
25878 cp_parser_objc_identifier_list (cp_parser* parser)
25880 tree identifier;
25881 tree list;
25882 cp_token *sep;
25884 identifier = cp_parser_identifier (parser);
25885 if (identifier == error_mark_node)
25886 return error_mark_node;
25888 list = build_tree_list (NULL_TREE, identifier);
25889 sep = cp_lexer_peek_token (parser->lexer);
25891 while (sep->type == CPP_COMMA)
25893 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25894 identifier = cp_parser_identifier (parser);
25895 if (identifier == error_mark_node)
25896 return list;
25898 list = chainon (list, build_tree_list (NULL_TREE,
25899 identifier));
25900 sep = cp_lexer_peek_token (parser->lexer);
25903 return list;
25906 /* Parse an Objective-C alias declaration.
25908 objc-alias-declaration:
25909 @compatibility_alias identifier identifier ;
25911 This function registers the alias mapping with the Objective-C front end.
25912 It returns nothing. */
25914 static void
25915 cp_parser_objc_alias_declaration (cp_parser* parser)
25917 tree alias, orig;
25919 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25920 alias = cp_parser_identifier (parser);
25921 orig = cp_parser_identifier (parser);
25922 objc_declare_alias (alias, orig);
25923 cp_parser_consume_semicolon_at_end_of_statement (parser);
25926 /* Parse an Objective-C class forward-declaration.
25928 objc-class-declaration:
25929 @class objc-identifier-list ;
25931 The function registers the forward declarations with the Objective-C
25932 front end. It returns nothing. */
25934 static void
25935 cp_parser_objc_class_declaration (cp_parser* parser)
25937 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25938 while (true)
25940 tree id;
25942 id = cp_parser_identifier (parser);
25943 if (id == error_mark_node)
25944 break;
25946 objc_declare_class (id);
25948 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25949 cp_lexer_consume_token (parser->lexer);
25950 else
25951 break;
25953 cp_parser_consume_semicolon_at_end_of_statement (parser);
25956 /* Parse a list of Objective-C protocol references.
25958 objc-protocol-refs-opt:
25959 objc-protocol-refs [opt]
25961 objc-protocol-refs:
25962 < objc-identifier-list >
25964 Returns a TREE_LIST of identifiers, if any. */
25966 static tree
25967 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25969 tree protorefs = NULL_TREE;
25971 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25973 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25974 protorefs = cp_parser_objc_identifier_list (parser);
25975 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25978 return protorefs;
25981 /* Parse a Objective-C visibility specification. */
25983 static void
25984 cp_parser_objc_visibility_spec (cp_parser* parser)
25986 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25988 switch (vis->keyword)
25990 case RID_AT_PRIVATE:
25991 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25992 break;
25993 case RID_AT_PROTECTED:
25994 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25995 break;
25996 case RID_AT_PUBLIC:
25997 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25998 break;
25999 case RID_AT_PACKAGE:
26000 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26001 break;
26002 default:
26003 return;
26006 /* Eat '@private'/'@protected'/'@public'. */
26007 cp_lexer_consume_token (parser->lexer);
26010 /* Parse an Objective-C method type. Return 'true' if it is a class
26011 (+) method, and 'false' if it is an instance (-) method. */
26013 static inline bool
26014 cp_parser_objc_method_type (cp_parser* parser)
26016 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26017 return true;
26018 else
26019 return false;
26022 /* Parse an Objective-C protocol qualifier. */
26024 static tree
26025 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26027 tree quals = NULL_TREE, node;
26028 cp_token *token = cp_lexer_peek_token (parser->lexer);
26030 node = token->u.value;
26032 while (node && identifier_p (node)
26033 && (node == ridpointers [(int) RID_IN]
26034 || node == ridpointers [(int) RID_OUT]
26035 || node == ridpointers [(int) RID_INOUT]
26036 || node == ridpointers [(int) RID_BYCOPY]
26037 || node == ridpointers [(int) RID_BYREF]
26038 || node == ridpointers [(int) RID_ONEWAY]))
26040 quals = tree_cons (NULL_TREE, node, quals);
26041 cp_lexer_consume_token (parser->lexer);
26042 token = cp_lexer_peek_token (parser->lexer);
26043 node = token->u.value;
26046 return quals;
26049 /* Parse an Objective-C typename. */
26051 static tree
26052 cp_parser_objc_typename (cp_parser* parser)
26054 tree type_name = NULL_TREE;
26056 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26058 tree proto_quals, cp_type = NULL_TREE;
26060 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26061 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26063 /* An ObjC type name may consist of just protocol qualifiers, in which
26064 case the type shall default to 'id'. */
26065 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26067 cp_type = cp_parser_type_id (parser);
26069 /* If the type could not be parsed, an error has already
26070 been produced. For error recovery, behave as if it had
26071 not been specified, which will use the default type
26072 'id'. */
26073 if (cp_type == error_mark_node)
26075 cp_type = NULL_TREE;
26076 /* We need to skip to the closing parenthesis as
26077 cp_parser_type_id() does not seem to do it for
26078 us. */
26079 cp_parser_skip_to_closing_parenthesis (parser,
26080 /*recovering=*/true,
26081 /*or_comma=*/false,
26082 /*consume_paren=*/false);
26086 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26087 type_name = build_tree_list (proto_quals, cp_type);
26090 return type_name;
26093 /* Check to see if TYPE refers to an Objective-C selector name. */
26095 static bool
26096 cp_parser_objc_selector_p (enum cpp_ttype type)
26098 return (type == CPP_NAME || type == CPP_KEYWORD
26099 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26100 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26101 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26102 || type == CPP_XOR || type == CPP_XOR_EQ);
26105 /* Parse an Objective-C selector. */
26107 static tree
26108 cp_parser_objc_selector (cp_parser* parser)
26110 cp_token *token = cp_lexer_consume_token (parser->lexer);
26112 if (!cp_parser_objc_selector_p (token->type))
26114 error_at (token->location, "invalid Objective-C++ selector name");
26115 return error_mark_node;
26118 /* C++ operator names are allowed to appear in ObjC selectors. */
26119 switch (token->type)
26121 case CPP_AND_AND: return get_identifier ("and");
26122 case CPP_AND_EQ: return get_identifier ("and_eq");
26123 case CPP_AND: return get_identifier ("bitand");
26124 case CPP_OR: return get_identifier ("bitor");
26125 case CPP_COMPL: return get_identifier ("compl");
26126 case CPP_NOT: return get_identifier ("not");
26127 case CPP_NOT_EQ: return get_identifier ("not_eq");
26128 case CPP_OR_OR: return get_identifier ("or");
26129 case CPP_OR_EQ: return get_identifier ("or_eq");
26130 case CPP_XOR: return get_identifier ("xor");
26131 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26132 default: return token->u.value;
26136 /* Parse an Objective-C params list. */
26138 static tree
26139 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26141 tree params = NULL_TREE;
26142 bool maybe_unary_selector_p = true;
26143 cp_token *token = cp_lexer_peek_token (parser->lexer);
26145 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26147 tree selector = NULL_TREE, type_name, identifier;
26148 tree parm_attr = NULL_TREE;
26150 if (token->keyword == RID_ATTRIBUTE)
26151 break;
26153 if (token->type != CPP_COLON)
26154 selector = cp_parser_objc_selector (parser);
26156 /* Detect if we have a unary selector. */
26157 if (maybe_unary_selector_p
26158 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26160 params = selector; /* Might be followed by attributes. */
26161 break;
26164 maybe_unary_selector_p = false;
26165 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26167 /* Something went quite wrong. There should be a colon
26168 here, but there is not. Stop parsing parameters. */
26169 break;
26171 type_name = cp_parser_objc_typename (parser);
26172 /* New ObjC allows attributes on parameters too. */
26173 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26174 parm_attr = cp_parser_attributes_opt (parser);
26175 identifier = cp_parser_identifier (parser);
26177 params
26178 = chainon (params,
26179 objc_build_keyword_decl (selector,
26180 type_name,
26181 identifier,
26182 parm_attr));
26184 token = cp_lexer_peek_token (parser->lexer);
26187 if (params == NULL_TREE)
26189 cp_parser_error (parser, "objective-c++ method declaration is expected");
26190 return error_mark_node;
26193 /* We allow tail attributes for the method. */
26194 if (token->keyword == RID_ATTRIBUTE)
26196 *attributes = cp_parser_attributes_opt (parser);
26197 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26198 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26199 return params;
26200 cp_parser_error (parser,
26201 "method attributes must be specified at the end");
26202 return error_mark_node;
26205 if (params == NULL_TREE)
26207 cp_parser_error (parser, "objective-c++ method declaration is expected");
26208 return error_mark_node;
26210 return params;
26213 /* Parse the non-keyword Objective-C params. */
26215 static tree
26216 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26217 tree* attributes)
26219 tree params = make_node (TREE_LIST);
26220 cp_token *token = cp_lexer_peek_token (parser->lexer);
26221 *ellipsisp = false; /* Initially, assume no ellipsis. */
26223 while (token->type == CPP_COMMA)
26225 cp_parameter_declarator *parmdecl;
26226 tree parm;
26228 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26229 token = cp_lexer_peek_token (parser->lexer);
26231 if (token->type == CPP_ELLIPSIS)
26233 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26234 *ellipsisp = true;
26235 token = cp_lexer_peek_token (parser->lexer);
26236 break;
26239 /* TODO: parse attributes for tail parameters. */
26240 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26241 parm = grokdeclarator (parmdecl->declarator,
26242 &parmdecl->decl_specifiers,
26243 PARM, /*initialized=*/0,
26244 /*attrlist=*/NULL);
26246 chainon (params, build_tree_list (NULL_TREE, parm));
26247 token = cp_lexer_peek_token (parser->lexer);
26250 /* We allow tail attributes for the method. */
26251 if (token->keyword == RID_ATTRIBUTE)
26253 if (*attributes == NULL_TREE)
26255 *attributes = cp_parser_attributes_opt (parser);
26256 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26257 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26258 return params;
26260 else
26261 /* We have an error, but parse the attributes, so that we can
26262 carry on. */
26263 *attributes = cp_parser_attributes_opt (parser);
26265 cp_parser_error (parser,
26266 "method attributes must be specified at the end");
26267 return error_mark_node;
26270 return params;
26273 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26275 static void
26276 cp_parser_objc_interstitial_code (cp_parser* parser)
26278 cp_token *token = cp_lexer_peek_token (parser->lexer);
26280 /* If the next token is `extern' and the following token is a string
26281 literal, then we have a linkage specification. */
26282 if (token->keyword == RID_EXTERN
26283 && cp_parser_is_pure_string_literal
26284 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26285 cp_parser_linkage_specification (parser);
26286 /* Handle #pragma, if any. */
26287 else if (token->type == CPP_PRAGMA)
26288 cp_parser_pragma (parser, pragma_objc_icode);
26289 /* Allow stray semicolons. */
26290 else if (token->type == CPP_SEMICOLON)
26291 cp_lexer_consume_token (parser->lexer);
26292 /* Mark methods as optional or required, when building protocols. */
26293 else if (token->keyword == RID_AT_OPTIONAL)
26295 cp_lexer_consume_token (parser->lexer);
26296 objc_set_method_opt (true);
26298 else if (token->keyword == RID_AT_REQUIRED)
26300 cp_lexer_consume_token (parser->lexer);
26301 objc_set_method_opt (false);
26303 else if (token->keyword == RID_NAMESPACE)
26304 cp_parser_namespace_definition (parser);
26305 /* Other stray characters must generate errors. */
26306 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26308 cp_lexer_consume_token (parser->lexer);
26309 error ("stray %qs between Objective-C++ methods",
26310 token->type == CPP_OPEN_BRACE ? "{" : "}");
26312 /* Finally, try to parse a block-declaration, or a function-definition. */
26313 else
26314 cp_parser_block_declaration (parser, /*statement_p=*/false);
26317 /* Parse a method signature. */
26319 static tree
26320 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26322 tree rettype, kwdparms, optparms;
26323 bool ellipsis = false;
26324 bool is_class_method;
26326 is_class_method = cp_parser_objc_method_type (parser);
26327 rettype = cp_parser_objc_typename (parser);
26328 *attributes = NULL_TREE;
26329 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26330 if (kwdparms == error_mark_node)
26331 return error_mark_node;
26332 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26333 if (optparms == error_mark_node)
26334 return error_mark_node;
26336 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26339 static bool
26340 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26342 tree tattr;
26343 cp_lexer_save_tokens (parser->lexer);
26344 tattr = cp_parser_attributes_opt (parser);
26345 gcc_assert (tattr) ;
26347 /* If the attributes are followed by a method introducer, this is not allowed.
26348 Dump the attributes and flag the situation. */
26349 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26350 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26351 return true;
26353 /* Otherwise, the attributes introduce some interstitial code, possibly so
26354 rewind to allow that check. */
26355 cp_lexer_rollback_tokens (parser->lexer);
26356 return false;
26359 /* Parse an Objective-C method prototype list. */
26361 static void
26362 cp_parser_objc_method_prototype_list (cp_parser* parser)
26364 cp_token *token = cp_lexer_peek_token (parser->lexer);
26366 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26368 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26370 tree attributes, sig;
26371 bool is_class_method;
26372 if (token->type == CPP_PLUS)
26373 is_class_method = true;
26374 else
26375 is_class_method = false;
26376 sig = cp_parser_objc_method_signature (parser, &attributes);
26377 if (sig == error_mark_node)
26379 cp_parser_skip_to_end_of_block_or_statement (parser);
26380 token = cp_lexer_peek_token (parser->lexer);
26381 continue;
26383 objc_add_method_declaration (is_class_method, sig, attributes);
26384 cp_parser_consume_semicolon_at_end_of_statement (parser);
26386 else if (token->keyword == RID_AT_PROPERTY)
26387 cp_parser_objc_at_property_declaration (parser);
26388 else if (token->keyword == RID_ATTRIBUTE
26389 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26390 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26391 OPT_Wattributes,
26392 "prefix attributes are ignored for methods");
26393 else
26394 /* Allow for interspersed non-ObjC++ code. */
26395 cp_parser_objc_interstitial_code (parser);
26397 token = cp_lexer_peek_token (parser->lexer);
26400 if (token->type != CPP_EOF)
26401 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26402 else
26403 cp_parser_error (parser, "expected %<@end%>");
26405 objc_finish_interface ();
26408 /* Parse an Objective-C method definition list. */
26410 static void
26411 cp_parser_objc_method_definition_list (cp_parser* parser)
26413 cp_token *token = cp_lexer_peek_token (parser->lexer);
26415 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26417 tree meth;
26419 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26421 cp_token *ptk;
26422 tree sig, attribute;
26423 bool is_class_method;
26424 if (token->type == CPP_PLUS)
26425 is_class_method = true;
26426 else
26427 is_class_method = false;
26428 push_deferring_access_checks (dk_deferred);
26429 sig = cp_parser_objc_method_signature (parser, &attribute);
26430 if (sig == error_mark_node)
26432 cp_parser_skip_to_end_of_block_or_statement (parser);
26433 token = cp_lexer_peek_token (parser->lexer);
26434 continue;
26436 objc_start_method_definition (is_class_method, sig, attribute,
26437 NULL_TREE);
26439 /* For historical reasons, we accept an optional semicolon. */
26440 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26441 cp_lexer_consume_token (parser->lexer);
26443 ptk = cp_lexer_peek_token (parser->lexer);
26444 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26445 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26447 perform_deferred_access_checks (tf_warning_or_error);
26448 stop_deferring_access_checks ();
26449 meth = cp_parser_function_definition_after_declarator (parser,
26450 false);
26451 pop_deferring_access_checks ();
26452 objc_finish_method_definition (meth);
26455 /* The following case will be removed once @synthesize is
26456 completely implemented. */
26457 else if (token->keyword == RID_AT_PROPERTY)
26458 cp_parser_objc_at_property_declaration (parser);
26459 else if (token->keyword == RID_AT_SYNTHESIZE)
26460 cp_parser_objc_at_synthesize_declaration (parser);
26461 else if (token->keyword == RID_AT_DYNAMIC)
26462 cp_parser_objc_at_dynamic_declaration (parser);
26463 else if (token->keyword == RID_ATTRIBUTE
26464 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26465 warning_at (token->location, OPT_Wattributes,
26466 "prefix attributes are ignored for methods");
26467 else
26468 /* Allow for interspersed non-ObjC++ code. */
26469 cp_parser_objc_interstitial_code (parser);
26471 token = cp_lexer_peek_token (parser->lexer);
26474 if (token->type != CPP_EOF)
26475 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26476 else
26477 cp_parser_error (parser, "expected %<@end%>");
26479 objc_finish_implementation ();
26482 /* Parse Objective-C ivars. */
26484 static void
26485 cp_parser_objc_class_ivars (cp_parser* parser)
26487 cp_token *token = cp_lexer_peek_token (parser->lexer);
26489 if (token->type != CPP_OPEN_BRACE)
26490 return; /* No ivars specified. */
26492 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26493 token = cp_lexer_peek_token (parser->lexer);
26495 while (token->type != CPP_CLOSE_BRACE
26496 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26498 cp_decl_specifier_seq declspecs;
26499 int decl_class_or_enum_p;
26500 tree prefix_attributes;
26502 cp_parser_objc_visibility_spec (parser);
26504 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26505 break;
26507 cp_parser_decl_specifier_seq (parser,
26508 CP_PARSER_FLAGS_OPTIONAL,
26509 &declspecs,
26510 &decl_class_or_enum_p);
26512 /* auto, register, static, extern, mutable. */
26513 if (declspecs.storage_class != sc_none)
26515 cp_parser_error (parser, "invalid type for instance variable");
26516 declspecs.storage_class = sc_none;
26519 /* thread_local. */
26520 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26522 cp_parser_error (parser, "invalid type for instance variable");
26523 declspecs.locations[ds_thread] = 0;
26526 /* typedef. */
26527 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26529 cp_parser_error (parser, "invalid type for instance variable");
26530 declspecs.locations[ds_typedef] = 0;
26533 prefix_attributes = declspecs.attributes;
26534 declspecs.attributes = NULL_TREE;
26536 /* Keep going until we hit the `;' at the end of the
26537 declaration. */
26538 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26540 tree width = NULL_TREE, attributes, first_attribute, decl;
26541 cp_declarator *declarator = NULL;
26542 int ctor_dtor_or_conv_p;
26544 /* Check for a (possibly unnamed) bitfield declaration. */
26545 token = cp_lexer_peek_token (parser->lexer);
26546 if (token->type == CPP_COLON)
26547 goto eat_colon;
26549 if (token->type == CPP_NAME
26550 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26551 == CPP_COLON))
26553 /* Get the name of the bitfield. */
26554 declarator = make_id_declarator (NULL_TREE,
26555 cp_parser_identifier (parser),
26556 sfk_none);
26558 eat_colon:
26559 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26560 /* Get the width of the bitfield. */
26561 width
26562 = cp_parser_constant_expression (parser);
26564 else
26566 /* Parse the declarator. */
26567 declarator
26568 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26569 &ctor_dtor_or_conv_p,
26570 /*parenthesized_p=*/NULL,
26571 /*member_p=*/false,
26572 /*friend_p=*/false);
26575 /* Look for attributes that apply to the ivar. */
26576 attributes = cp_parser_attributes_opt (parser);
26577 /* Remember which attributes are prefix attributes and
26578 which are not. */
26579 first_attribute = attributes;
26580 /* Combine the attributes. */
26581 attributes = chainon (prefix_attributes, attributes);
26583 if (width)
26584 /* Create the bitfield declaration. */
26585 decl = grokbitfield (declarator, &declspecs,
26586 width,
26587 attributes);
26588 else
26589 decl = grokfield (declarator, &declspecs,
26590 NULL_TREE, /*init_const_expr_p=*/false,
26591 NULL_TREE, attributes);
26593 /* Add the instance variable. */
26594 if (decl != error_mark_node && decl != NULL_TREE)
26595 objc_add_instance_variable (decl);
26597 /* Reset PREFIX_ATTRIBUTES. */
26598 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26599 attributes = TREE_CHAIN (attributes);
26600 if (attributes)
26601 TREE_CHAIN (attributes) = NULL_TREE;
26603 token = cp_lexer_peek_token (parser->lexer);
26605 if (token->type == CPP_COMMA)
26607 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26608 continue;
26610 break;
26613 cp_parser_consume_semicolon_at_end_of_statement (parser);
26614 token = cp_lexer_peek_token (parser->lexer);
26617 if (token->keyword == RID_AT_END)
26618 cp_parser_error (parser, "expected %<}%>");
26620 /* Do not consume the RID_AT_END, so it will be read again as terminating
26621 the @interface of @implementation. */
26622 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26623 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26625 /* For historical reasons, we accept an optional semicolon. */
26626 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26627 cp_lexer_consume_token (parser->lexer);
26630 /* Parse an Objective-C protocol declaration. */
26632 static void
26633 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26635 tree proto, protorefs;
26636 cp_token *tok;
26638 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26639 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26641 tok = cp_lexer_peek_token (parser->lexer);
26642 error_at (tok->location, "identifier expected after %<@protocol%>");
26643 cp_parser_consume_semicolon_at_end_of_statement (parser);
26644 return;
26647 /* See if we have a forward declaration or a definition. */
26648 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26650 /* Try a forward declaration first. */
26651 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26653 while (true)
26655 tree id;
26657 id = cp_parser_identifier (parser);
26658 if (id == error_mark_node)
26659 break;
26661 objc_declare_protocol (id, attributes);
26663 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26664 cp_lexer_consume_token (parser->lexer);
26665 else
26666 break;
26668 cp_parser_consume_semicolon_at_end_of_statement (parser);
26671 /* Ok, we got a full-fledged definition (or at least should). */
26672 else
26674 proto = cp_parser_identifier (parser);
26675 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26676 objc_start_protocol (proto, protorefs, attributes);
26677 cp_parser_objc_method_prototype_list (parser);
26681 /* Parse an Objective-C superclass or category. */
26683 static void
26684 cp_parser_objc_superclass_or_category (cp_parser *parser,
26685 bool iface_p,
26686 tree *super,
26687 tree *categ, bool *is_class_extension)
26689 cp_token *next = cp_lexer_peek_token (parser->lexer);
26691 *super = *categ = NULL_TREE;
26692 *is_class_extension = false;
26693 if (next->type == CPP_COLON)
26695 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26696 *super = cp_parser_identifier (parser);
26698 else if (next->type == CPP_OPEN_PAREN)
26700 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26702 /* If there is no category name, and this is an @interface, we
26703 have a class extension. */
26704 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26706 *categ = NULL_TREE;
26707 *is_class_extension = true;
26709 else
26710 *categ = cp_parser_identifier (parser);
26712 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26716 /* Parse an Objective-C class interface. */
26718 static void
26719 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26721 tree name, super, categ, protos;
26722 bool is_class_extension;
26724 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26725 name = cp_parser_identifier (parser);
26726 if (name == error_mark_node)
26728 /* It's hard to recover because even if valid @interface stuff
26729 is to follow, we can't compile it (or validate it) if we
26730 don't even know which class it refers to. Let's assume this
26731 was a stray '@interface' token in the stream and skip it.
26733 return;
26735 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26736 &is_class_extension);
26737 protos = cp_parser_objc_protocol_refs_opt (parser);
26739 /* We have either a class or a category on our hands. */
26740 if (categ || is_class_extension)
26741 objc_start_category_interface (name, categ, protos, attributes);
26742 else
26744 objc_start_class_interface (name, super, protos, attributes);
26745 /* Handle instance variable declarations, if any. */
26746 cp_parser_objc_class_ivars (parser);
26747 objc_continue_interface ();
26750 cp_parser_objc_method_prototype_list (parser);
26753 /* Parse an Objective-C class implementation. */
26755 static void
26756 cp_parser_objc_class_implementation (cp_parser* parser)
26758 tree name, super, categ;
26759 bool is_class_extension;
26761 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26762 name = cp_parser_identifier (parser);
26763 if (name == error_mark_node)
26765 /* It's hard to recover because even if valid @implementation
26766 stuff is to follow, we can't compile it (or validate it) if
26767 we don't even know which class it refers to. Let's assume
26768 this was a stray '@implementation' token in the stream and
26769 skip it.
26771 return;
26773 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26774 &is_class_extension);
26776 /* We have either a class or a category on our hands. */
26777 if (categ)
26778 objc_start_category_implementation (name, categ);
26779 else
26781 objc_start_class_implementation (name, super);
26782 /* Handle instance variable declarations, if any. */
26783 cp_parser_objc_class_ivars (parser);
26784 objc_continue_implementation ();
26787 cp_parser_objc_method_definition_list (parser);
26790 /* Consume the @end token and finish off the implementation. */
26792 static void
26793 cp_parser_objc_end_implementation (cp_parser* parser)
26795 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26796 objc_finish_implementation ();
26799 /* Parse an Objective-C declaration. */
26801 static void
26802 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26804 /* Try to figure out what kind of declaration is present. */
26805 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26807 if (attributes)
26808 switch (kwd->keyword)
26810 case RID_AT_ALIAS:
26811 case RID_AT_CLASS:
26812 case RID_AT_END:
26813 error_at (kwd->location, "attributes may not be specified before"
26814 " the %<@%D%> Objective-C++ keyword",
26815 kwd->u.value);
26816 attributes = NULL;
26817 break;
26818 case RID_AT_IMPLEMENTATION:
26819 warning_at (kwd->location, OPT_Wattributes,
26820 "prefix attributes are ignored before %<@%D%>",
26821 kwd->u.value);
26822 attributes = NULL;
26823 default:
26824 break;
26827 switch (kwd->keyword)
26829 case RID_AT_ALIAS:
26830 cp_parser_objc_alias_declaration (parser);
26831 break;
26832 case RID_AT_CLASS:
26833 cp_parser_objc_class_declaration (parser);
26834 break;
26835 case RID_AT_PROTOCOL:
26836 cp_parser_objc_protocol_declaration (parser, attributes);
26837 break;
26838 case RID_AT_INTERFACE:
26839 cp_parser_objc_class_interface (parser, attributes);
26840 break;
26841 case RID_AT_IMPLEMENTATION:
26842 cp_parser_objc_class_implementation (parser);
26843 break;
26844 case RID_AT_END:
26845 cp_parser_objc_end_implementation (parser);
26846 break;
26847 default:
26848 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26849 kwd->u.value);
26850 cp_parser_skip_to_end_of_block_or_statement (parser);
26854 /* Parse an Objective-C try-catch-finally statement.
26856 objc-try-catch-finally-stmt:
26857 @try compound-statement objc-catch-clause-seq [opt]
26858 objc-finally-clause [opt]
26860 objc-catch-clause-seq:
26861 objc-catch-clause objc-catch-clause-seq [opt]
26863 objc-catch-clause:
26864 @catch ( objc-exception-declaration ) compound-statement
26866 objc-finally-clause:
26867 @finally compound-statement
26869 objc-exception-declaration:
26870 parameter-declaration
26871 '...'
26873 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26875 Returns NULL_TREE.
26877 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26878 for C. Keep them in sync. */
26880 static tree
26881 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26883 location_t location;
26884 tree stmt;
26886 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26887 location = cp_lexer_peek_token (parser->lexer)->location;
26888 objc_maybe_warn_exceptions (location);
26889 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26890 node, lest it get absorbed into the surrounding block. */
26891 stmt = push_stmt_list ();
26892 cp_parser_compound_statement (parser, NULL, false, false);
26893 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26895 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26897 cp_parameter_declarator *parm;
26898 tree parameter_declaration = error_mark_node;
26899 bool seen_open_paren = false;
26901 cp_lexer_consume_token (parser->lexer);
26902 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26903 seen_open_paren = true;
26904 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26906 /* We have "@catch (...)" (where the '...' are literally
26907 what is in the code). Skip the '...'.
26908 parameter_declaration is set to NULL_TREE, and
26909 objc_being_catch_clauses() knows that that means
26910 '...'. */
26911 cp_lexer_consume_token (parser->lexer);
26912 parameter_declaration = NULL_TREE;
26914 else
26916 /* We have "@catch (NSException *exception)" or something
26917 like that. Parse the parameter declaration. */
26918 parm = cp_parser_parameter_declaration (parser, false, NULL);
26919 if (parm == NULL)
26920 parameter_declaration = error_mark_node;
26921 else
26922 parameter_declaration = grokdeclarator (parm->declarator,
26923 &parm->decl_specifiers,
26924 PARM, /*initialized=*/0,
26925 /*attrlist=*/NULL);
26927 if (seen_open_paren)
26928 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26929 else
26931 /* If there was no open parenthesis, we are recovering from
26932 an error, and we are trying to figure out what mistake
26933 the user has made. */
26935 /* If there is an immediate closing parenthesis, the user
26936 probably forgot the opening one (ie, they typed "@catch
26937 NSException *e)". Parse the closing parenthesis and keep
26938 going. */
26939 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26940 cp_lexer_consume_token (parser->lexer);
26942 /* If these is no immediate closing parenthesis, the user
26943 probably doesn't know that parenthesis are required at
26944 all (ie, they typed "@catch NSException *e"). So, just
26945 forget about the closing parenthesis and keep going. */
26947 objc_begin_catch_clause (parameter_declaration);
26948 cp_parser_compound_statement (parser, NULL, false, false);
26949 objc_finish_catch_clause ();
26951 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26953 cp_lexer_consume_token (parser->lexer);
26954 location = cp_lexer_peek_token (parser->lexer)->location;
26955 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26956 node, lest it get absorbed into the surrounding block. */
26957 stmt = push_stmt_list ();
26958 cp_parser_compound_statement (parser, NULL, false, false);
26959 objc_build_finally_clause (location, pop_stmt_list (stmt));
26962 return objc_finish_try_stmt ();
26965 /* Parse an Objective-C synchronized statement.
26967 objc-synchronized-stmt:
26968 @synchronized ( expression ) compound-statement
26970 Returns NULL_TREE. */
26972 static tree
26973 cp_parser_objc_synchronized_statement (cp_parser *parser)
26975 location_t location;
26976 tree lock, stmt;
26978 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26980 location = cp_lexer_peek_token (parser->lexer)->location;
26981 objc_maybe_warn_exceptions (location);
26982 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26983 lock = cp_parser_expression (parser);
26984 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26986 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26987 node, lest it get absorbed into the surrounding block. */
26988 stmt = push_stmt_list ();
26989 cp_parser_compound_statement (parser, NULL, false, false);
26991 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26994 /* Parse an Objective-C throw statement.
26996 objc-throw-stmt:
26997 @throw assignment-expression [opt] ;
26999 Returns a constructed '@throw' statement. */
27001 static tree
27002 cp_parser_objc_throw_statement (cp_parser *parser)
27004 tree expr = NULL_TREE;
27005 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27007 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27009 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27010 expr = cp_parser_expression (parser);
27012 cp_parser_consume_semicolon_at_end_of_statement (parser);
27014 return objc_build_throw_stmt (loc, expr);
27017 /* Parse an Objective-C statement. */
27019 static tree
27020 cp_parser_objc_statement (cp_parser * parser)
27022 /* Try to figure out what kind of declaration is present. */
27023 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27025 switch (kwd->keyword)
27027 case RID_AT_TRY:
27028 return cp_parser_objc_try_catch_finally_statement (parser);
27029 case RID_AT_SYNCHRONIZED:
27030 return cp_parser_objc_synchronized_statement (parser);
27031 case RID_AT_THROW:
27032 return cp_parser_objc_throw_statement (parser);
27033 default:
27034 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27035 kwd->u.value);
27036 cp_parser_skip_to_end_of_block_or_statement (parser);
27039 return error_mark_node;
27042 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27043 look ahead to see if an objc keyword follows the attributes. This
27044 is to detect the use of prefix attributes on ObjC @interface and
27045 @protocol. */
27047 static bool
27048 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27050 cp_lexer_save_tokens (parser->lexer);
27051 *attrib = cp_parser_attributes_opt (parser);
27052 gcc_assert (*attrib);
27053 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27055 cp_lexer_commit_tokens (parser->lexer);
27056 return true;
27058 cp_lexer_rollback_tokens (parser->lexer);
27059 return false;
27062 /* This routine is a minimal replacement for
27063 c_parser_struct_declaration () used when parsing the list of
27064 types/names or ObjC++ properties. For example, when parsing the
27065 code
27067 @property (readonly) int a, b, c;
27069 this function is responsible for parsing "int a, int b, int c" and
27070 returning the declarations as CHAIN of DECLs.
27072 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27073 similar parsing. */
27074 static tree
27075 cp_parser_objc_struct_declaration (cp_parser *parser)
27077 tree decls = NULL_TREE;
27078 cp_decl_specifier_seq declspecs;
27079 int decl_class_or_enum_p;
27080 tree prefix_attributes;
27082 cp_parser_decl_specifier_seq (parser,
27083 CP_PARSER_FLAGS_NONE,
27084 &declspecs,
27085 &decl_class_or_enum_p);
27087 if (declspecs.type == error_mark_node)
27088 return error_mark_node;
27090 /* auto, register, static, extern, mutable. */
27091 if (declspecs.storage_class != sc_none)
27093 cp_parser_error (parser, "invalid type for property");
27094 declspecs.storage_class = sc_none;
27097 /* thread_local. */
27098 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27100 cp_parser_error (parser, "invalid type for property");
27101 declspecs.locations[ds_thread] = 0;
27104 /* typedef. */
27105 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27107 cp_parser_error (parser, "invalid type for property");
27108 declspecs.locations[ds_typedef] = 0;
27111 prefix_attributes = declspecs.attributes;
27112 declspecs.attributes = NULL_TREE;
27114 /* Keep going until we hit the `;' at the end of the declaration. */
27115 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27117 tree attributes, first_attribute, decl;
27118 cp_declarator *declarator;
27119 cp_token *token;
27121 /* Parse the declarator. */
27122 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27123 NULL, NULL, false, false);
27125 /* Look for attributes that apply to the ivar. */
27126 attributes = cp_parser_attributes_opt (parser);
27127 /* Remember which attributes are prefix attributes and
27128 which are not. */
27129 first_attribute = attributes;
27130 /* Combine the attributes. */
27131 attributes = chainon (prefix_attributes, attributes);
27133 decl = grokfield (declarator, &declspecs,
27134 NULL_TREE, /*init_const_expr_p=*/false,
27135 NULL_TREE, attributes);
27137 if (decl == error_mark_node || decl == NULL_TREE)
27138 return error_mark_node;
27140 /* Reset PREFIX_ATTRIBUTES. */
27141 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27142 attributes = TREE_CHAIN (attributes);
27143 if (attributes)
27144 TREE_CHAIN (attributes) = NULL_TREE;
27146 DECL_CHAIN (decl) = decls;
27147 decls = decl;
27149 token = cp_lexer_peek_token (parser->lexer);
27150 if (token->type == CPP_COMMA)
27152 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27153 continue;
27155 else
27156 break;
27158 return decls;
27161 /* Parse an Objective-C @property declaration. The syntax is:
27163 objc-property-declaration:
27164 '@property' objc-property-attributes[opt] struct-declaration ;
27166 objc-property-attributes:
27167 '(' objc-property-attribute-list ')'
27169 objc-property-attribute-list:
27170 objc-property-attribute
27171 objc-property-attribute-list, objc-property-attribute
27173 objc-property-attribute
27174 'getter' = identifier
27175 'setter' = identifier
27176 'readonly'
27177 'readwrite'
27178 'assign'
27179 'retain'
27180 'copy'
27181 'nonatomic'
27183 For example:
27184 @property NSString *name;
27185 @property (readonly) id object;
27186 @property (retain, nonatomic, getter=getTheName) id name;
27187 @property int a, b, c;
27189 PS: This function is identical to
27190 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27191 static void
27192 cp_parser_objc_at_property_declaration (cp_parser *parser)
27194 /* The following variables hold the attributes of the properties as
27195 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27196 seen. When we see an attribute, we set them to 'true' (if they
27197 are boolean properties) or to the identifier (if they have an
27198 argument, ie, for getter and setter). Note that here we only
27199 parse the list of attributes, check the syntax and accumulate the
27200 attributes that we find. objc_add_property_declaration() will
27201 then process the information. */
27202 bool property_assign = false;
27203 bool property_copy = false;
27204 tree property_getter_ident = NULL_TREE;
27205 bool property_nonatomic = false;
27206 bool property_readonly = false;
27207 bool property_readwrite = false;
27208 bool property_retain = false;
27209 tree property_setter_ident = NULL_TREE;
27211 /* 'properties' is the list of properties that we read. Usually a
27212 single one, but maybe more (eg, in "@property int a, b, c;" there
27213 are three). */
27214 tree properties;
27215 location_t loc;
27217 loc = cp_lexer_peek_token (parser->lexer)->location;
27219 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27221 /* Parse the optional attribute list... */
27222 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27224 /* Eat the '('. */
27225 cp_lexer_consume_token (parser->lexer);
27227 while (true)
27229 bool syntax_error = false;
27230 cp_token *token = cp_lexer_peek_token (parser->lexer);
27231 enum rid keyword;
27233 if (token->type != CPP_NAME)
27235 cp_parser_error (parser, "expected identifier");
27236 break;
27238 keyword = C_RID_CODE (token->u.value);
27239 cp_lexer_consume_token (parser->lexer);
27240 switch (keyword)
27242 case RID_ASSIGN: property_assign = true; break;
27243 case RID_COPY: property_copy = true; break;
27244 case RID_NONATOMIC: property_nonatomic = true; break;
27245 case RID_READONLY: property_readonly = true; break;
27246 case RID_READWRITE: property_readwrite = true; break;
27247 case RID_RETAIN: property_retain = true; break;
27249 case RID_GETTER:
27250 case RID_SETTER:
27251 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27253 if (keyword == RID_GETTER)
27254 cp_parser_error (parser,
27255 "missing %<=%> (after %<getter%> attribute)");
27256 else
27257 cp_parser_error (parser,
27258 "missing %<=%> (after %<setter%> attribute)");
27259 syntax_error = true;
27260 break;
27262 cp_lexer_consume_token (parser->lexer); /* eat the = */
27263 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27265 cp_parser_error (parser, "expected identifier");
27266 syntax_error = true;
27267 break;
27269 if (keyword == RID_SETTER)
27271 if (property_setter_ident != NULL_TREE)
27273 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27274 cp_lexer_consume_token (parser->lexer);
27276 else
27277 property_setter_ident = cp_parser_objc_selector (parser);
27278 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27279 cp_parser_error (parser, "setter name must terminate with %<:%>");
27280 else
27281 cp_lexer_consume_token (parser->lexer);
27283 else
27285 if (property_getter_ident != NULL_TREE)
27287 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27288 cp_lexer_consume_token (parser->lexer);
27290 else
27291 property_getter_ident = cp_parser_objc_selector (parser);
27293 break;
27294 default:
27295 cp_parser_error (parser, "unknown property attribute");
27296 syntax_error = true;
27297 break;
27300 if (syntax_error)
27301 break;
27303 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27304 cp_lexer_consume_token (parser->lexer);
27305 else
27306 break;
27309 /* FIXME: "@property (setter, assign);" will generate a spurious
27310 "error: expected ‘)’ before ‘,’ token". This is because
27311 cp_parser_require, unlike the C counterpart, will produce an
27312 error even if we are in error recovery. */
27313 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27315 cp_parser_skip_to_closing_parenthesis (parser,
27316 /*recovering=*/true,
27317 /*or_comma=*/false,
27318 /*consume_paren=*/true);
27322 /* ... and the property declaration(s). */
27323 properties = cp_parser_objc_struct_declaration (parser);
27325 if (properties == error_mark_node)
27327 cp_parser_skip_to_end_of_statement (parser);
27328 /* If the next token is now a `;', consume it. */
27329 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27330 cp_lexer_consume_token (parser->lexer);
27331 return;
27334 if (properties == NULL_TREE)
27335 cp_parser_error (parser, "expected identifier");
27336 else
27338 /* Comma-separated properties are chained together in
27339 reverse order; add them one by one. */
27340 properties = nreverse (properties);
27342 for (; properties; properties = TREE_CHAIN (properties))
27343 objc_add_property_declaration (loc, copy_node (properties),
27344 property_readonly, property_readwrite,
27345 property_assign, property_retain,
27346 property_copy, property_nonatomic,
27347 property_getter_ident, property_setter_ident);
27350 cp_parser_consume_semicolon_at_end_of_statement (parser);
27353 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27355 objc-synthesize-declaration:
27356 @synthesize objc-synthesize-identifier-list ;
27358 objc-synthesize-identifier-list:
27359 objc-synthesize-identifier
27360 objc-synthesize-identifier-list, objc-synthesize-identifier
27362 objc-synthesize-identifier
27363 identifier
27364 identifier = identifier
27366 For example:
27367 @synthesize MyProperty;
27368 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27370 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27371 for C. Keep them in sync.
27373 static void
27374 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27376 tree list = NULL_TREE;
27377 location_t loc;
27378 loc = cp_lexer_peek_token (parser->lexer)->location;
27380 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27381 while (true)
27383 tree property, ivar;
27384 property = cp_parser_identifier (parser);
27385 if (property == error_mark_node)
27387 cp_parser_consume_semicolon_at_end_of_statement (parser);
27388 return;
27390 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27392 cp_lexer_consume_token (parser->lexer);
27393 ivar = cp_parser_identifier (parser);
27394 if (ivar == error_mark_node)
27396 cp_parser_consume_semicolon_at_end_of_statement (parser);
27397 return;
27400 else
27401 ivar = NULL_TREE;
27402 list = chainon (list, build_tree_list (ivar, property));
27403 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27404 cp_lexer_consume_token (parser->lexer);
27405 else
27406 break;
27408 cp_parser_consume_semicolon_at_end_of_statement (parser);
27409 objc_add_synthesize_declaration (loc, list);
27412 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27414 objc-dynamic-declaration:
27415 @dynamic identifier-list ;
27417 For example:
27418 @dynamic MyProperty;
27419 @dynamic MyProperty, AnotherProperty;
27421 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27422 for C. Keep them in sync.
27424 static void
27425 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27427 tree list = NULL_TREE;
27428 location_t loc;
27429 loc = cp_lexer_peek_token (parser->lexer)->location;
27431 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27432 while (true)
27434 tree property;
27435 property = cp_parser_identifier (parser);
27436 if (property == error_mark_node)
27438 cp_parser_consume_semicolon_at_end_of_statement (parser);
27439 return;
27441 list = chainon (list, build_tree_list (NULL, property));
27442 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27443 cp_lexer_consume_token (parser->lexer);
27444 else
27445 break;
27447 cp_parser_consume_semicolon_at_end_of_statement (parser);
27448 objc_add_dynamic_declaration (loc, list);
27452 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27454 /* Returns name of the next clause.
27455 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27456 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27457 returned and the token is consumed. */
27459 static pragma_omp_clause
27460 cp_parser_omp_clause_name (cp_parser *parser)
27462 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27464 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27465 result = PRAGMA_OMP_CLAUSE_IF;
27466 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27467 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27468 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27469 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27470 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27471 result = PRAGMA_OMP_CLAUSE_FOR;
27472 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27474 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27475 const char *p = IDENTIFIER_POINTER (id);
27477 switch (p[0])
27479 case 'a':
27480 if (!strcmp ("aligned", p))
27481 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27482 break;
27483 case 'c':
27484 if (!strcmp ("collapse", p))
27485 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27486 else if (!strcmp ("copyin", p))
27487 result = PRAGMA_OMP_CLAUSE_COPYIN;
27488 else if (!strcmp ("copyprivate", p))
27489 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27490 break;
27491 case 'd':
27492 if (!strcmp ("depend", p))
27493 result = PRAGMA_OMP_CLAUSE_DEPEND;
27494 else if (!strcmp ("device", p))
27495 result = PRAGMA_OMP_CLAUSE_DEVICE;
27496 else if (!strcmp ("dist_schedule", p))
27497 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27498 break;
27499 case 'f':
27500 if (!strcmp ("final", p))
27501 result = PRAGMA_OMP_CLAUSE_FINAL;
27502 else if (!strcmp ("firstprivate", p))
27503 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27504 else if (!strcmp ("from", p))
27505 result = PRAGMA_OMP_CLAUSE_FROM;
27506 break;
27507 case 'i':
27508 if (!strcmp ("inbranch", p))
27509 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27510 break;
27511 case 'l':
27512 if (!strcmp ("lastprivate", p))
27513 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27514 else if (!strcmp ("linear", p))
27515 result = PRAGMA_OMP_CLAUSE_LINEAR;
27516 break;
27517 case 'm':
27518 if (!strcmp ("map", p))
27519 result = PRAGMA_OMP_CLAUSE_MAP;
27520 else if (!strcmp ("mergeable", p))
27521 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27522 else if (flag_cilkplus && !strcmp ("mask", p))
27523 result = PRAGMA_CILK_CLAUSE_MASK;
27524 break;
27525 case 'n':
27526 if (!strcmp ("notinbranch", p))
27527 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27528 else if (!strcmp ("nowait", p))
27529 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27530 else if (flag_cilkplus && !strcmp ("nomask", p))
27531 result = PRAGMA_CILK_CLAUSE_NOMASK;
27532 else if (!strcmp ("num_teams", p))
27533 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27534 else if (!strcmp ("num_threads", p))
27535 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27536 break;
27537 case 'o':
27538 if (!strcmp ("ordered", p))
27539 result = PRAGMA_OMP_CLAUSE_ORDERED;
27540 break;
27541 case 'p':
27542 if (!strcmp ("parallel", p))
27543 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27544 else if (!strcmp ("proc_bind", p))
27545 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27546 break;
27547 case 'r':
27548 if (!strcmp ("reduction", p))
27549 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27550 break;
27551 case 's':
27552 if (!strcmp ("safelen", p))
27553 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27554 else if (!strcmp ("schedule", p))
27555 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27556 else if (!strcmp ("sections", p))
27557 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27558 else if (!strcmp ("shared", p))
27559 result = PRAGMA_OMP_CLAUSE_SHARED;
27560 else if (!strcmp ("simdlen", p))
27561 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27562 break;
27563 case 't':
27564 if (!strcmp ("taskgroup", p))
27565 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27566 else if (!strcmp ("thread_limit", p))
27567 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27568 else if (!strcmp ("to", p))
27569 result = PRAGMA_OMP_CLAUSE_TO;
27570 break;
27571 case 'u':
27572 if (!strcmp ("uniform", p))
27573 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27574 else if (!strcmp ("untied", p))
27575 result = PRAGMA_OMP_CLAUSE_UNTIED;
27576 break;
27577 case 'v':
27578 if (flag_cilkplus && !strcmp ("vectorlength", p))
27579 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27580 break;
27584 if (result != PRAGMA_OMP_CLAUSE_NONE)
27585 cp_lexer_consume_token (parser->lexer);
27587 return result;
27590 /* Validate that a clause of the given type does not already exist. */
27592 static void
27593 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27594 const char *name, location_t location)
27596 tree c;
27598 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27599 if (OMP_CLAUSE_CODE (c) == code)
27601 error_at (location, "too many %qs clauses", name);
27602 break;
27606 /* OpenMP 2.5:
27607 variable-list:
27608 identifier
27609 variable-list , identifier
27611 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27612 colon). An opening parenthesis will have been consumed by the caller.
27614 If KIND is nonzero, create the appropriate node and install the decl
27615 in OMP_CLAUSE_DECL and add the node to the head of the list.
27617 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27618 return the list created.
27620 COLON can be NULL if only closing parenthesis should end the list,
27621 or pointer to bool which will receive false if the list is terminated
27622 by closing parenthesis or true if the list is terminated by colon. */
27624 static tree
27625 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27626 tree list, bool *colon)
27628 cp_token *token;
27629 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27630 if (colon)
27632 parser->colon_corrects_to_scope_p = false;
27633 *colon = false;
27635 while (1)
27637 tree name, decl;
27639 token = cp_lexer_peek_token (parser->lexer);
27640 name = cp_parser_id_expression (parser, /*template_p=*/false,
27641 /*check_dependency_p=*/true,
27642 /*template_p=*/NULL,
27643 /*declarator_p=*/false,
27644 /*optional_p=*/false);
27645 if (name == error_mark_node)
27646 goto skip_comma;
27648 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27649 if (decl == error_mark_node)
27650 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27651 token->location);
27652 else if (kind != 0)
27654 switch (kind)
27656 case OMP_CLAUSE_MAP:
27657 case OMP_CLAUSE_FROM:
27658 case OMP_CLAUSE_TO:
27659 case OMP_CLAUSE_DEPEND:
27660 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27662 tree low_bound = NULL_TREE, length = NULL_TREE;
27664 parser->colon_corrects_to_scope_p = false;
27665 cp_lexer_consume_token (parser->lexer);
27666 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27667 low_bound = cp_parser_expression (parser);
27668 if (!colon)
27669 parser->colon_corrects_to_scope_p
27670 = saved_colon_corrects_to_scope_p;
27671 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27672 length = integer_one_node;
27673 else
27675 /* Look for `:'. */
27676 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27677 goto skip_comma;
27678 if (!cp_lexer_next_token_is (parser->lexer,
27679 CPP_CLOSE_SQUARE))
27680 length = cp_parser_expression (parser);
27682 /* Look for the closing `]'. */
27683 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27684 RT_CLOSE_SQUARE))
27685 goto skip_comma;
27686 decl = tree_cons (low_bound, length, decl);
27688 break;
27689 default:
27690 break;
27693 tree u = build_omp_clause (token->location, kind);
27694 OMP_CLAUSE_DECL (u) = decl;
27695 OMP_CLAUSE_CHAIN (u) = list;
27696 list = u;
27698 else
27699 list = tree_cons (decl, NULL_TREE, list);
27701 get_comma:
27702 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27703 break;
27704 cp_lexer_consume_token (parser->lexer);
27707 if (colon)
27708 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27710 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27712 *colon = true;
27713 cp_parser_require (parser, CPP_COLON, RT_COLON);
27714 return list;
27717 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27719 int ending;
27721 /* Try to resync to an unnested comma. Copied from
27722 cp_parser_parenthesized_expression_list. */
27723 skip_comma:
27724 if (colon)
27725 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27726 ending = cp_parser_skip_to_closing_parenthesis (parser,
27727 /*recovering=*/true,
27728 /*or_comma=*/true,
27729 /*consume_paren=*/true);
27730 if (ending < 0)
27731 goto get_comma;
27734 return list;
27737 /* Similarly, but expect leading and trailing parenthesis. This is a very
27738 common case for omp clauses. */
27740 static tree
27741 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27743 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27744 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27745 return list;
27748 /* OpenMP 3.0:
27749 collapse ( constant-expression ) */
27751 static tree
27752 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27754 tree c, num;
27755 location_t loc;
27756 HOST_WIDE_INT n;
27758 loc = cp_lexer_peek_token (parser->lexer)->location;
27759 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27760 return list;
27762 num = cp_parser_constant_expression (parser);
27764 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27765 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27766 /*or_comma=*/false,
27767 /*consume_paren=*/true);
27769 if (num == error_mark_node)
27770 return list;
27771 num = fold_non_dependent_expr (num);
27772 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27773 || !tree_fits_shwi_p (num)
27774 || (n = tree_to_shwi (num)) <= 0
27775 || (int) n != n)
27777 error_at (loc, "collapse argument needs positive constant integer expression");
27778 return list;
27781 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27782 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27783 OMP_CLAUSE_CHAIN (c) = list;
27784 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27786 return c;
27789 /* OpenMP 2.5:
27790 default ( shared | none ) */
27792 static tree
27793 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27795 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27796 tree c;
27798 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27799 return list;
27800 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27802 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27803 const char *p = IDENTIFIER_POINTER (id);
27805 switch (p[0])
27807 case 'n':
27808 if (strcmp ("none", p) != 0)
27809 goto invalid_kind;
27810 kind = OMP_CLAUSE_DEFAULT_NONE;
27811 break;
27813 case 's':
27814 if (strcmp ("shared", p) != 0)
27815 goto invalid_kind;
27816 kind = OMP_CLAUSE_DEFAULT_SHARED;
27817 break;
27819 default:
27820 goto invalid_kind;
27823 cp_lexer_consume_token (parser->lexer);
27825 else
27827 invalid_kind:
27828 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27831 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27832 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27833 /*or_comma=*/false,
27834 /*consume_paren=*/true);
27836 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27837 return list;
27839 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27840 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27841 OMP_CLAUSE_CHAIN (c) = list;
27842 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27844 return c;
27847 /* OpenMP 3.1:
27848 final ( expression ) */
27850 static tree
27851 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27853 tree t, c;
27855 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27856 return list;
27858 t = cp_parser_condition (parser);
27860 if (t == error_mark_node
27861 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27862 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27863 /*or_comma=*/false,
27864 /*consume_paren=*/true);
27866 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27868 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27869 OMP_CLAUSE_FINAL_EXPR (c) = t;
27870 OMP_CLAUSE_CHAIN (c) = list;
27872 return c;
27875 /* OpenMP 2.5:
27876 if ( expression ) */
27878 static tree
27879 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27881 tree t, c;
27883 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27884 return list;
27886 t = cp_parser_condition (parser);
27888 if (t == error_mark_node
27889 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27890 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27891 /*or_comma=*/false,
27892 /*consume_paren=*/true);
27894 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27896 c = build_omp_clause (location, OMP_CLAUSE_IF);
27897 OMP_CLAUSE_IF_EXPR (c) = t;
27898 OMP_CLAUSE_CHAIN (c) = list;
27900 return c;
27903 /* OpenMP 3.1:
27904 mergeable */
27906 static tree
27907 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27908 tree list, location_t location)
27910 tree c;
27912 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27913 location);
27915 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27916 OMP_CLAUSE_CHAIN (c) = list;
27917 return c;
27920 /* OpenMP 2.5:
27921 nowait */
27923 static tree
27924 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27925 tree list, location_t location)
27927 tree c;
27929 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27931 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27932 OMP_CLAUSE_CHAIN (c) = list;
27933 return c;
27936 /* OpenMP 2.5:
27937 num_threads ( expression ) */
27939 static tree
27940 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27941 location_t location)
27943 tree t, c;
27945 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27946 return list;
27948 t = cp_parser_expression (parser);
27950 if (t == error_mark_node
27951 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27952 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27953 /*or_comma=*/false,
27954 /*consume_paren=*/true);
27956 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27957 "num_threads", location);
27959 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27960 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27961 OMP_CLAUSE_CHAIN (c) = list;
27963 return c;
27966 /* OpenMP 2.5:
27967 ordered */
27969 static tree
27970 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27971 tree list, location_t location)
27973 tree c;
27975 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27976 "ordered", location);
27978 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27979 OMP_CLAUSE_CHAIN (c) = list;
27980 return c;
27983 /* OpenMP 2.5:
27984 reduction ( reduction-operator : variable-list )
27986 reduction-operator:
27987 One of: + * - & ^ | && ||
27989 OpenMP 3.1:
27991 reduction-operator:
27992 One of: + * - & ^ | && || min max
27994 OpenMP 4.0:
27996 reduction-operator:
27997 One of: + * - & ^ | && ||
27998 id-expression */
28000 static tree
28001 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28003 enum tree_code code = ERROR_MARK;
28004 tree nlist, c, id = NULL_TREE;
28006 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28007 return list;
28009 switch (cp_lexer_peek_token (parser->lexer)->type)
28011 case CPP_PLUS: code = PLUS_EXPR; break;
28012 case CPP_MULT: code = MULT_EXPR; break;
28013 case CPP_MINUS: code = MINUS_EXPR; break;
28014 case CPP_AND: code = BIT_AND_EXPR; break;
28015 case CPP_XOR: code = BIT_XOR_EXPR; break;
28016 case CPP_OR: code = BIT_IOR_EXPR; break;
28017 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28018 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28019 default: break;
28022 if (code != ERROR_MARK)
28023 cp_lexer_consume_token (parser->lexer);
28024 else
28026 bool saved_colon_corrects_to_scope_p;
28027 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28028 parser->colon_corrects_to_scope_p = false;
28029 id = cp_parser_id_expression (parser, /*template_p=*/false,
28030 /*check_dependency_p=*/true,
28031 /*template_p=*/NULL,
28032 /*declarator_p=*/false,
28033 /*optional_p=*/false);
28034 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28035 if (identifier_p (id))
28037 const char *p = IDENTIFIER_POINTER (id);
28039 if (strcmp (p, "min") == 0)
28040 code = MIN_EXPR;
28041 else if (strcmp (p, "max") == 0)
28042 code = MAX_EXPR;
28043 else if (id == ansi_opname (PLUS_EXPR))
28044 code = PLUS_EXPR;
28045 else if (id == ansi_opname (MULT_EXPR))
28046 code = MULT_EXPR;
28047 else if (id == ansi_opname (MINUS_EXPR))
28048 code = MINUS_EXPR;
28049 else if (id == ansi_opname (BIT_AND_EXPR))
28050 code = BIT_AND_EXPR;
28051 else if (id == ansi_opname (BIT_IOR_EXPR))
28052 code = BIT_IOR_EXPR;
28053 else if (id == ansi_opname (BIT_XOR_EXPR))
28054 code = BIT_XOR_EXPR;
28055 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28056 code = TRUTH_ANDIF_EXPR;
28057 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28058 code = TRUTH_ORIF_EXPR;
28059 id = omp_reduction_id (code, id, NULL_TREE);
28060 tree scope = parser->scope;
28061 if (scope)
28062 id = build_qualified_name (NULL_TREE, scope, id, false);
28063 parser->scope = NULL_TREE;
28064 parser->qualifying_scope = NULL_TREE;
28065 parser->object_scope = NULL_TREE;
28067 else
28069 error ("invalid reduction-identifier");
28070 resync_fail:
28071 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28072 /*or_comma=*/false,
28073 /*consume_paren=*/true);
28074 return list;
28078 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28079 goto resync_fail;
28081 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28082 NULL);
28083 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28085 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28086 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28089 return nlist;
28092 /* OpenMP 2.5:
28093 schedule ( schedule-kind )
28094 schedule ( schedule-kind , expression )
28096 schedule-kind:
28097 static | dynamic | guided | runtime | auto */
28099 static tree
28100 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28102 tree c, t;
28104 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28105 return list;
28107 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28109 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28111 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28112 const char *p = IDENTIFIER_POINTER (id);
28114 switch (p[0])
28116 case 'd':
28117 if (strcmp ("dynamic", p) != 0)
28118 goto invalid_kind;
28119 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28120 break;
28122 case 'g':
28123 if (strcmp ("guided", p) != 0)
28124 goto invalid_kind;
28125 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28126 break;
28128 case 'r':
28129 if (strcmp ("runtime", p) != 0)
28130 goto invalid_kind;
28131 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28132 break;
28134 default:
28135 goto invalid_kind;
28138 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28139 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28140 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28141 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28142 else
28143 goto invalid_kind;
28144 cp_lexer_consume_token (parser->lexer);
28146 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28148 cp_token *token;
28149 cp_lexer_consume_token (parser->lexer);
28151 token = cp_lexer_peek_token (parser->lexer);
28152 t = cp_parser_assignment_expression (parser);
28154 if (t == error_mark_node)
28155 goto resync_fail;
28156 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28157 error_at (token->location, "schedule %<runtime%> does not take "
28158 "a %<chunk_size%> parameter");
28159 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28160 error_at (token->location, "schedule %<auto%> does not take "
28161 "a %<chunk_size%> parameter");
28162 else
28163 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28165 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28166 goto resync_fail;
28168 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28169 goto resync_fail;
28171 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28172 OMP_CLAUSE_CHAIN (c) = list;
28173 return c;
28175 invalid_kind:
28176 cp_parser_error (parser, "invalid schedule kind");
28177 resync_fail:
28178 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28179 /*or_comma=*/false,
28180 /*consume_paren=*/true);
28181 return list;
28184 /* OpenMP 3.0:
28185 untied */
28187 static tree
28188 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28189 tree list, location_t location)
28191 tree c;
28193 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28195 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28196 OMP_CLAUSE_CHAIN (c) = list;
28197 return c;
28200 /* OpenMP 4.0:
28201 inbranch
28202 notinbranch */
28204 static tree
28205 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28206 tree list, location_t location)
28208 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28209 tree c = build_omp_clause (location, code);
28210 OMP_CLAUSE_CHAIN (c) = list;
28211 return c;
28214 /* OpenMP 4.0:
28215 parallel
28217 sections
28218 taskgroup */
28220 static tree
28221 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28222 enum omp_clause_code code,
28223 tree list, location_t location)
28225 tree c = build_omp_clause (location, code);
28226 OMP_CLAUSE_CHAIN (c) = list;
28227 return c;
28230 /* OpenMP 4.0:
28231 num_teams ( expression ) */
28233 static tree
28234 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28235 location_t location)
28237 tree t, c;
28239 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28240 return list;
28242 t = cp_parser_expression (parser);
28244 if (t == error_mark_node
28245 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28246 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28247 /*or_comma=*/false,
28248 /*consume_paren=*/true);
28250 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28251 "num_teams", location);
28253 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28254 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28255 OMP_CLAUSE_CHAIN (c) = list;
28257 return c;
28260 /* OpenMP 4.0:
28261 thread_limit ( expression ) */
28263 static tree
28264 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28265 location_t location)
28267 tree t, c;
28269 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28270 return list;
28272 t = cp_parser_expression (parser);
28274 if (t == error_mark_node
28275 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28276 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28277 /*or_comma=*/false,
28278 /*consume_paren=*/true);
28280 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28281 "thread_limit", location);
28283 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28284 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28285 OMP_CLAUSE_CHAIN (c) = list;
28287 return c;
28290 /* OpenMP 4.0:
28291 aligned ( variable-list )
28292 aligned ( variable-list : constant-expression ) */
28294 static tree
28295 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28297 tree nlist, c, alignment = NULL_TREE;
28298 bool colon;
28300 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28301 return list;
28303 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28304 &colon);
28306 if (colon)
28308 alignment = cp_parser_constant_expression (parser);
28310 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28311 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28312 /*or_comma=*/false,
28313 /*consume_paren=*/true);
28315 if (alignment == error_mark_node)
28316 alignment = NULL_TREE;
28319 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28320 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28322 return nlist;
28325 /* OpenMP 4.0:
28326 linear ( variable-list )
28327 linear ( variable-list : expression ) */
28329 static tree
28330 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28331 bool is_cilk_simd_fn)
28333 tree nlist, c, step = integer_one_node;
28334 bool colon;
28336 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28337 return list;
28339 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28340 &colon);
28342 if (colon)
28344 step = cp_parser_expression (parser);
28346 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28348 sorry ("using parameters for %<linear%> step is not supported yet");
28349 step = integer_one_node;
28351 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28352 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28353 /*or_comma=*/false,
28354 /*consume_paren=*/true);
28356 if (step == error_mark_node)
28357 return list;
28360 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28361 OMP_CLAUSE_LINEAR_STEP (c) = step;
28363 return nlist;
28366 /* OpenMP 4.0:
28367 safelen ( constant-expression ) */
28369 static tree
28370 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28371 location_t location)
28373 tree t, c;
28375 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28376 return list;
28378 t = cp_parser_constant_expression (parser);
28380 if (t == error_mark_node
28381 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28382 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28383 /*or_comma=*/false,
28384 /*consume_paren=*/true);
28386 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28388 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28389 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28390 OMP_CLAUSE_CHAIN (c) = list;
28392 return c;
28395 /* OpenMP 4.0:
28396 simdlen ( constant-expression ) */
28398 static tree
28399 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28400 location_t location)
28402 tree t, c;
28404 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28405 return list;
28407 t = cp_parser_constant_expression (parser);
28409 if (t == error_mark_node
28410 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28411 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28412 /*or_comma=*/false,
28413 /*consume_paren=*/true);
28415 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28417 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28418 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28419 OMP_CLAUSE_CHAIN (c) = list;
28421 return c;
28424 /* OpenMP 4.0:
28425 depend ( depend-kind : variable-list )
28427 depend-kind:
28428 in | out | inout */
28430 static tree
28431 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28433 tree nlist, c;
28434 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28436 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28437 return list;
28439 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28441 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28442 const char *p = IDENTIFIER_POINTER (id);
28444 if (strcmp ("in", p) == 0)
28445 kind = OMP_CLAUSE_DEPEND_IN;
28446 else if (strcmp ("inout", p) == 0)
28447 kind = OMP_CLAUSE_DEPEND_INOUT;
28448 else if (strcmp ("out", p) == 0)
28449 kind = OMP_CLAUSE_DEPEND_OUT;
28450 else
28451 goto invalid_kind;
28453 else
28454 goto invalid_kind;
28456 cp_lexer_consume_token (parser->lexer);
28457 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28458 goto resync_fail;
28460 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28461 NULL);
28463 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28464 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28466 return nlist;
28468 invalid_kind:
28469 cp_parser_error (parser, "invalid depend kind");
28470 resync_fail:
28471 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28472 /*or_comma=*/false,
28473 /*consume_paren=*/true);
28474 return list;
28477 /* OpenMP 4.0:
28478 map ( map-kind : variable-list )
28479 map ( variable-list )
28481 map-kind:
28482 alloc | to | from | tofrom */
28484 static tree
28485 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28487 tree nlist, c;
28488 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28490 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28491 return list;
28493 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28494 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28496 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28497 const char *p = IDENTIFIER_POINTER (id);
28499 if (strcmp ("alloc", p) == 0)
28500 kind = OMP_CLAUSE_MAP_ALLOC;
28501 else if (strcmp ("to", p) == 0)
28502 kind = OMP_CLAUSE_MAP_TO;
28503 else if (strcmp ("from", p) == 0)
28504 kind = OMP_CLAUSE_MAP_FROM;
28505 else if (strcmp ("tofrom", p) == 0)
28506 kind = OMP_CLAUSE_MAP_TOFROM;
28507 else
28509 cp_parser_error (parser, "invalid map kind");
28510 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28511 /*or_comma=*/false,
28512 /*consume_paren=*/true);
28513 return list;
28515 cp_lexer_consume_token (parser->lexer);
28516 cp_lexer_consume_token (parser->lexer);
28519 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28520 NULL);
28522 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28523 OMP_CLAUSE_MAP_KIND (c) = kind;
28525 return nlist;
28528 /* OpenMP 4.0:
28529 device ( expression ) */
28531 static tree
28532 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28533 location_t location)
28535 tree t, c;
28537 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28538 return list;
28540 t = cp_parser_expression (parser);
28542 if (t == error_mark_node
28543 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28544 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28545 /*or_comma=*/false,
28546 /*consume_paren=*/true);
28548 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28549 "device", location);
28551 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28552 OMP_CLAUSE_DEVICE_ID (c) = t;
28553 OMP_CLAUSE_CHAIN (c) = list;
28555 return c;
28558 /* OpenMP 4.0:
28559 dist_schedule ( static )
28560 dist_schedule ( static , expression ) */
28562 static tree
28563 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28564 location_t location)
28566 tree c, t;
28568 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28569 return list;
28571 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28573 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28574 goto invalid_kind;
28575 cp_lexer_consume_token (parser->lexer);
28577 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28579 cp_lexer_consume_token (parser->lexer);
28581 t = cp_parser_assignment_expression (parser);
28583 if (t == error_mark_node)
28584 goto resync_fail;
28585 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28587 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28588 goto resync_fail;
28590 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28591 goto resync_fail;
28593 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28594 location);
28595 OMP_CLAUSE_CHAIN (c) = list;
28596 return c;
28598 invalid_kind:
28599 cp_parser_error (parser, "invalid dist_schedule kind");
28600 resync_fail:
28601 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28602 /*or_comma=*/false,
28603 /*consume_paren=*/true);
28604 return list;
28607 /* OpenMP 4.0:
28608 proc_bind ( proc-bind-kind )
28610 proc-bind-kind:
28611 master | close | spread */
28613 static tree
28614 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28615 location_t location)
28617 tree c;
28618 enum omp_clause_proc_bind_kind kind;
28620 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28621 return list;
28623 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28625 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28626 const char *p = IDENTIFIER_POINTER (id);
28628 if (strcmp ("master", p) == 0)
28629 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28630 else if (strcmp ("close", p) == 0)
28631 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28632 else if (strcmp ("spread", p) == 0)
28633 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28634 else
28635 goto invalid_kind;
28637 else
28638 goto invalid_kind;
28640 cp_lexer_consume_token (parser->lexer);
28641 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28642 goto resync_fail;
28644 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28645 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28646 location);
28647 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28648 OMP_CLAUSE_CHAIN (c) = list;
28649 return c;
28651 invalid_kind:
28652 cp_parser_error (parser, "invalid depend kind");
28653 resync_fail:
28654 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28655 /*or_comma=*/false,
28656 /*consume_paren=*/true);
28657 return list;
28660 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28661 is a bitmask in MASK. Return the list of clauses found; the result
28662 of clause default goes in *pdefault. */
28664 static tree
28665 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28666 const char *where, cp_token *pragma_tok,
28667 bool finish_p = true)
28669 tree clauses = NULL;
28670 bool first = true;
28671 cp_token *token = NULL;
28672 bool cilk_simd_fn = false;
28674 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28676 pragma_omp_clause c_kind;
28677 const char *c_name;
28678 tree prev = clauses;
28680 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28681 cp_lexer_consume_token (parser->lexer);
28683 token = cp_lexer_peek_token (parser->lexer);
28684 c_kind = cp_parser_omp_clause_name (parser);
28686 switch (c_kind)
28688 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28689 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28690 token->location);
28691 c_name = "collapse";
28692 break;
28693 case PRAGMA_OMP_CLAUSE_COPYIN:
28694 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28695 c_name = "copyin";
28696 break;
28697 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28698 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28699 clauses);
28700 c_name = "copyprivate";
28701 break;
28702 case PRAGMA_OMP_CLAUSE_DEFAULT:
28703 clauses = cp_parser_omp_clause_default (parser, clauses,
28704 token->location);
28705 c_name = "default";
28706 break;
28707 case PRAGMA_OMP_CLAUSE_FINAL:
28708 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28709 c_name = "final";
28710 break;
28711 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28712 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28713 clauses);
28714 c_name = "firstprivate";
28715 break;
28716 case PRAGMA_OMP_CLAUSE_IF:
28717 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28718 c_name = "if";
28719 break;
28720 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28721 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28722 clauses);
28723 c_name = "lastprivate";
28724 break;
28725 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28726 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28727 token->location);
28728 c_name = "mergeable";
28729 break;
28730 case PRAGMA_OMP_CLAUSE_NOWAIT:
28731 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28732 c_name = "nowait";
28733 break;
28734 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28735 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28736 token->location);
28737 c_name = "num_threads";
28738 break;
28739 case PRAGMA_OMP_CLAUSE_ORDERED:
28740 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28741 token->location);
28742 c_name = "ordered";
28743 break;
28744 case PRAGMA_OMP_CLAUSE_PRIVATE:
28745 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28746 clauses);
28747 c_name = "private";
28748 break;
28749 case PRAGMA_OMP_CLAUSE_REDUCTION:
28750 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28751 c_name = "reduction";
28752 break;
28753 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28754 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28755 token->location);
28756 c_name = "schedule";
28757 break;
28758 case PRAGMA_OMP_CLAUSE_SHARED:
28759 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28760 clauses);
28761 c_name = "shared";
28762 break;
28763 case PRAGMA_OMP_CLAUSE_UNTIED:
28764 clauses = cp_parser_omp_clause_untied (parser, clauses,
28765 token->location);
28766 c_name = "untied";
28767 break;
28768 case PRAGMA_OMP_CLAUSE_INBRANCH:
28769 case PRAGMA_CILK_CLAUSE_MASK:
28770 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28771 clauses, token->location);
28772 c_name = "inbranch";
28773 break;
28774 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28775 case PRAGMA_CILK_CLAUSE_NOMASK:
28776 clauses = cp_parser_omp_clause_branch (parser,
28777 OMP_CLAUSE_NOTINBRANCH,
28778 clauses, token->location);
28779 c_name = "notinbranch";
28780 break;
28781 case PRAGMA_OMP_CLAUSE_PARALLEL:
28782 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28783 clauses, token->location);
28784 c_name = "parallel";
28785 if (!first)
28787 clause_not_first:
28788 error_at (token->location, "%qs must be the first clause of %qs",
28789 c_name, where);
28790 clauses = prev;
28792 break;
28793 case PRAGMA_OMP_CLAUSE_FOR:
28794 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28795 clauses, token->location);
28796 c_name = "for";
28797 if (!first)
28798 goto clause_not_first;
28799 break;
28800 case PRAGMA_OMP_CLAUSE_SECTIONS:
28801 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28802 clauses, token->location);
28803 c_name = "sections";
28804 if (!first)
28805 goto clause_not_first;
28806 break;
28807 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28808 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28809 clauses, token->location);
28810 c_name = "taskgroup";
28811 if (!first)
28812 goto clause_not_first;
28813 break;
28814 case PRAGMA_OMP_CLAUSE_TO:
28815 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28816 clauses);
28817 c_name = "to";
28818 break;
28819 case PRAGMA_OMP_CLAUSE_FROM:
28820 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28821 clauses);
28822 c_name = "from";
28823 break;
28824 case PRAGMA_OMP_CLAUSE_UNIFORM:
28825 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28826 clauses);
28827 c_name = "uniform";
28828 break;
28829 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28830 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28831 token->location);
28832 c_name = "num_teams";
28833 break;
28834 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28835 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28836 token->location);
28837 c_name = "thread_limit";
28838 break;
28839 case PRAGMA_OMP_CLAUSE_ALIGNED:
28840 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28841 c_name = "aligned";
28842 break;
28843 case PRAGMA_OMP_CLAUSE_LINEAR:
28844 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28845 cilk_simd_fn = true;
28846 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28847 c_name = "linear";
28848 break;
28849 case PRAGMA_OMP_CLAUSE_DEPEND:
28850 clauses = cp_parser_omp_clause_depend (parser, clauses);
28851 c_name = "depend";
28852 break;
28853 case PRAGMA_OMP_CLAUSE_MAP:
28854 clauses = cp_parser_omp_clause_map (parser, clauses);
28855 c_name = "map";
28856 break;
28857 case PRAGMA_OMP_CLAUSE_DEVICE:
28858 clauses = cp_parser_omp_clause_device (parser, clauses,
28859 token->location);
28860 c_name = "device";
28861 break;
28862 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28863 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28864 token->location);
28865 c_name = "dist_schedule";
28866 break;
28867 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28868 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28869 token->location);
28870 c_name = "proc_bind";
28871 break;
28872 case PRAGMA_OMP_CLAUSE_SAFELEN:
28873 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28874 token->location);
28875 c_name = "safelen";
28876 break;
28877 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28878 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28879 token->location);
28880 c_name = "simdlen";
28881 break;
28882 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28883 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28884 c_name = "simdlen";
28885 break;
28886 default:
28887 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28888 goto saw_error;
28891 first = false;
28893 if (((mask >> c_kind) & 1) == 0)
28895 /* Remove the invalid clause(s) from the list to avoid
28896 confusing the rest of the compiler. */
28897 clauses = prev;
28898 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28901 saw_error:
28902 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28903 no reason to skip to the end. */
28904 if (!(flag_cilkplus && pragma_tok == NULL))
28905 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28906 if (finish_p)
28907 return finish_omp_clauses (clauses);
28908 return clauses;
28911 /* OpenMP 2.5:
28912 structured-block:
28913 statement
28915 In practice, we're also interested in adding the statement to an
28916 outer node. So it is convenient if we work around the fact that
28917 cp_parser_statement calls add_stmt. */
28919 static unsigned
28920 cp_parser_begin_omp_structured_block (cp_parser *parser)
28922 unsigned save = parser->in_statement;
28924 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28925 This preserves the "not within loop or switch" style error messages
28926 for nonsense cases like
28927 void foo() {
28928 #pragma omp single
28929 break;
28932 if (parser->in_statement)
28933 parser->in_statement = IN_OMP_BLOCK;
28935 return save;
28938 static void
28939 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28941 parser->in_statement = save;
28944 static tree
28945 cp_parser_omp_structured_block (cp_parser *parser)
28947 tree stmt = begin_omp_structured_block ();
28948 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28950 cp_parser_statement (parser, NULL_TREE, false, NULL);
28952 cp_parser_end_omp_structured_block (parser, save);
28953 return finish_omp_structured_block (stmt);
28956 /* OpenMP 2.5:
28957 # pragma omp atomic new-line
28958 expression-stmt
28960 expression-stmt:
28961 x binop= expr | x++ | ++x | x-- | --x
28962 binop:
28963 +, *, -, /, &, ^, |, <<, >>
28965 where x is an lvalue expression with scalar type.
28967 OpenMP 3.1:
28968 # pragma omp atomic new-line
28969 update-stmt
28971 # pragma omp atomic read new-line
28972 read-stmt
28974 # pragma omp atomic write new-line
28975 write-stmt
28977 # pragma omp atomic update new-line
28978 update-stmt
28980 # pragma omp atomic capture new-line
28981 capture-stmt
28983 # pragma omp atomic capture new-line
28984 capture-block
28986 read-stmt:
28987 v = x
28988 write-stmt:
28989 x = expr
28990 update-stmt:
28991 expression-stmt | x = x binop expr
28992 capture-stmt:
28993 v = expression-stmt
28994 capture-block:
28995 { v = x; update-stmt; } | { update-stmt; v = x; }
28997 OpenMP 4.0:
28998 update-stmt:
28999 expression-stmt | x = x binop expr | x = expr binop x
29000 capture-stmt:
29001 v = update-stmt
29002 capture-block:
29003 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29005 where x and v are lvalue expressions with scalar type. */
29007 static void
29008 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29010 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29011 tree rhs1 = NULL_TREE, orig_lhs;
29012 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29013 bool structured_block = false;
29014 bool seq_cst = false;
29016 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29018 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29019 const char *p = IDENTIFIER_POINTER (id);
29021 if (!strcmp (p, "seq_cst"))
29023 seq_cst = true;
29024 cp_lexer_consume_token (parser->lexer);
29025 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29026 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29027 cp_lexer_consume_token (parser->lexer);
29030 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29032 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29033 const char *p = IDENTIFIER_POINTER (id);
29035 if (!strcmp (p, "read"))
29036 code = OMP_ATOMIC_READ;
29037 else if (!strcmp (p, "write"))
29038 code = NOP_EXPR;
29039 else if (!strcmp (p, "update"))
29040 code = OMP_ATOMIC;
29041 else if (!strcmp (p, "capture"))
29042 code = OMP_ATOMIC_CAPTURE_NEW;
29043 else
29044 p = NULL;
29045 if (p)
29046 cp_lexer_consume_token (parser->lexer);
29048 if (!seq_cst)
29050 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29051 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29052 cp_lexer_consume_token (parser->lexer);
29054 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29056 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29057 const char *p = IDENTIFIER_POINTER (id);
29059 if (!strcmp (p, "seq_cst"))
29061 seq_cst = true;
29062 cp_lexer_consume_token (parser->lexer);
29066 cp_parser_require_pragma_eol (parser, pragma_tok);
29068 switch (code)
29070 case OMP_ATOMIC_READ:
29071 case NOP_EXPR: /* atomic write */
29072 v = cp_parser_unary_expression (parser);
29073 if (v == error_mark_node)
29074 goto saw_error;
29075 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29076 goto saw_error;
29077 if (code == NOP_EXPR)
29078 lhs = cp_parser_expression (parser);
29079 else
29080 lhs = cp_parser_unary_expression (parser);
29081 if (lhs == error_mark_node)
29082 goto saw_error;
29083 if (code == NOP_EXPR)
29085 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29086 opcode. */
29087 code = OMP_ATOMIC;
29088 rhs = lhs;
29089 lhs = v;
29090 v = NULL_TREE;
29092 goto done;
29093 case OMP_ATOMIC_CAPTURE_NEW:
29094 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29096 cp_lexer_consume_token (parser->lexer);
29097 structured_block = true;
29099 else
29101 v = cp_parser_unary_expression (parser);
29102 if (v == error_mark_node)
29103 goto saw_error;
29104 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29105 goto saw_error;
29107 default:
29108 break;
29111 restart:
29112 lhs = cp_parser_unary_expression (parser);
29113 orig_lhs = lhs;
29114 switch (TREE_CODE (lhs))
29116 case ERROR_MARK:
29117 goto saw_error;
29119 case POSTINCREMENT_EXPR:
29120 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29121 code = OMP_ATOMIC_CAPTURE_OLD;
29122 /* FALLTHROUGH */
29123 case PREINCREMENT_EXPR:
29124 lhs = TREE_OPERAND (lhs, 0);
29125 opcode = PLUS_EXPR;
29126 rhs = integer_one_node;
29127 break;
29129 case POSTDECREMENT_EXPR:
29130 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29131 code = OMP_ATOMIC_CAPTURE_OLD;
29132 /* FALLTHROUGH */
29133 case PREDECREMENT_EXPR:
29134 lhs = TREE_OPERAND (lhs, 0);
29135 opcode = MINUS_EXPR;
29136 rhs = integer_one_node;
29137 break;
29139 case COMPOUND_EXPR:
29140 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29141 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29142 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29143 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29144 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29145 (TREE_OPERAND (lhs, 1), 0), 0)))
29146 == BOOLEAN_TYPE)
29147 /* Undo effects of boolean_increment for post {in,de}crement. */
29148 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29149 /* FALLTHRU */
29150 case MODIFY_EXPR:
29151 if (TREE_CODE (lhs) == MODIFY_EXPR
29152 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29154 /* Undo effects of boolean_increment. */
29155 if (integer_onep (TREE_OPERAND (lhs, 1)))
29157 /* This is pre or post increment. */
29158 rhs = TREE_OPERAND (lhs, 1);
29159 lhs = TREE_OPERAND (lhs, 0);
29160 opcode = NOP_EXPR;
29161 if (code == OMP_ATOMIC_CAPTURE_NEW
29162 && !structured_block
29163 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29164 code = OMP_ATOMIC_CAPTURE_OLD;
29165 break;
29168 /* FALLTHRU */
29169 default:
29170 switch (cp_lexer_peek_token (parser->lexer)->type)
29172 case CPP_MULT_EQ:
29173 opcode = MULT_EXPR;
29174 break;
29175 case CPP_DIV_EQ:
29176 opcode = TRUNC_DIV_EXPR;
29177 break;
29178 case CPP_PLUS_EQ:
29179 opcode = PLUS_EXPR;
29180 break;
29181 case CPP_MINUS_EQ:
29182 opcode = MINUS_EXPR;
29183 break;
29184 case CPP_LSHIFT_EQ:
29185 opcode = LSHIFT_EXPR;
29186 break;
29187 case CPP_RSHIFT_EQ:
29188 opcode = RSHIFT_EXPR;
29189 break;
29190 case CPP_AND_EQ:
29191 opcode = BIT_AND_EXPR;
29192 break;
29193 case CPP_OR_EQ:
29194 opcode = BIT_IOR_EXPR;
29195 break;
29196 case CPP_XOR_EQ:
29197 opcode = BIT_XOR_EXPR;
29198 break;
29199 case CPP_EQ:
29200 enum cp_parser_prec oprec;
29201 cp_token *token;
29202 cp_lexer_consume_token (parser->lexer);
29203 cp_parser_parse_tentatively (parser);
29204 rhs1 = cp_parser_simple_cast_expression (parser);
29205 if (rhs1 == error_mark_node)
29207 cp_parser_abort_tentative_parse (parser);
29208 cp_parser_simple_cast_expression (parser);
29209 goto saw_error;
29211 token = cp_lexer_peek_token (parser->lexer);
29212 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29214 cp_parser_abort_tentative_parse (parser);
29215 cp_parser_parse_tentatively (parser);
29216 rhs = cp_parser_binary_expression (parser, false, true,
29217 PREC_NOT_OPERATOR, NULL);
29218 if (rhs == error_mark_node)
29220 cp_parser_abort_tentative_parse (parser);
29221 cp_parser_binary_expression (parser, false, true,
29222 PREC_NOT_OPERATOR, NULL);
29223 goto saw_error;
29225 switch (TREE_CODE (rhs))
29227 case MULT_EXPR:
29228 case TRUNC_DIV_EXPR:
29229 case PLUS_EXPR:
29230 case MINUS_EXPR:
29231 case LSHIFT_EXPR:
29232 case RSHIFT_EXPR:
29233 case BIT_AND_EXPR:
29234 case BIT_IOR_EXPR:
29235 case BIT_XOR_EXPR:
29236 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29238 if (cp_parser_parse_definitely (parser))
29240 opcode = TREE_CODE (rhs);
29241 rhs1 = TREE_OPERAND (rhs, 0);
29242 rhs = TREE_OPERAND (rhs, 1);
29243 goto stmt_done;
29245 else
29246 goto saw_error;
29248 break;
29249 default:
29250 break;
29252 cp_parser_abort_tentative_parse (parser);
29253 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29255 rhs = cp_parser_expression (parser);
29256 if (rhs == error_mark_node)
29257 goto saw_error;
29258 opcode = NOP_EXPR;
29259 rhs1 = NULL_TREE;
29260 goto stmt_done;
29262 cp_parser_error (parser,
29263 "invalid form of %<#pragma omp atomic%>");
29264 goto saw_error;
29266 if (!cp_parser_parse_definitely (parser))
29267 goto saw_error;
29268 switch (token->type)
29270 case CPP_SEMICOLON:
29271 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29273 code = OMP_ATOMIC_CAPTURE_OLD;
29274 v = lhs;
29275 lhs = NULL_TREE;
29276 lhs1 = rhs1;
29277 rhs1 = NULL_TREE;
29278 cp_lexer_consume_token (parser->lexer);
29279 goto restart;
29281 else if (structured_block)
29283 opcode = NOP_EXPR;
29284 rhs = rhs1;
29285 rhs1 = NULL_TREE;
29286 goto stmt_done;
29288 cp_parser_error (parser,
29289 "invalid form of %<#pragma omp atomic%>");
29290 goto saw_error;
29291 case CPP_MULT:
29292 opcode = MULT_EXPR;
29293 break;
29294 case CPP_DIV:
29295 opcode = TRUNC_DIV_EXPR;
29296 break;
29297 case CPP_PLUS:
29298 opcode = PLUS_EXPR;
29299 break;
29300 case CPP_MINUS:
29301 opcode = MINUS_EXPR;
29302 break;
29303 case CPP_LSHIFT:
29304 opcode = LSHIFT_EXPR;
29305 break;
29306 case CPP_RSHIFT:
29307 opcode = RSHIFT_EXPR;
29308 break;
29309 case CPP_AND:
29310 opcode = BIT_AND_EXPR;
29311 break;
29312 case CPP_OR:
29313 opcode = BIT_IOR_EXPR;
29314 break;
29315 case CPP_XOR:
29316 opcode = BIT_XOR_EXPR;
29317 break;
29318 default:
29319 cp_parser_error (parser,
29320 "invalid operator for %<#pragma omp atomic%>");
29321 goto saw_error;
29323 oprec = TOKEN_PRECEDENCE (token);
29324 gcc_assert (oprec != PREC_NOT_OPERATOR);
29325 if (commutative_tree_code (opcode))
29326 oprec = (enum cp_parser_prec) (oprec - 1);
29327 cp_lexer_consume_token (parser->lexer);
29328 rhs = cp_parser_binary_expression (parser, false, false,
29329 oprec, NULL);
29330 if (rhs == error_mark_node)
29331 goto saw_error;
29332 goto stmt_done;
29333 /* FALLTHROUGH */
29334 default:
29335 cp_parser_error (parser,
29336 "invalid operator for %<#pragma omp atomic%>");
29337 goto saw_error;
29339 cp_lexer_consume_token (parser->lexer);
29341 rhs = cp_parser_expression (parser);
29342 if (rhs == error_mark_node)
29343 goto saw_error;
29344 break;
29346 stmt_done:
29347 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29349 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29350 goto saw_error;
29351 v = cp_parser_unary_expression (parser);
29352 if (v == error_mark_node)
29353 goto saw_error;
29354 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29355 goto saw_error;
29356 lhs1 = cp_parser_unary_expression (parser);
29357 if (lhs1 == error_mark_node)
29358 goto saw_error;
29360 if (structured_block)
29362 cp_parser_consume_semicolon_at_end_of_statement (parser);
29363 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29365 done:
29366 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29367 if (!structured_block)
29368 cp_parser_consume_semicolon_at_end_of_statement (parser);
29369 return;
29371 saw_error:
29372 cp_parser_skip_to_end_of_block_or_statement (parser);
29373 if (structured_block)
29375 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29376 cp_lexer_consume_token (parser->lexer);
29377 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29379 cp_parser_skip_to_end_of_block_or_statement (parser);
29380 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29381 cp_lexer_consume_token (parser->lexer);
29387 /* OpenMP 2.5:
29388 # pragma omp barrier new-line */
29390 static void
29391 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29393 cp_parser_require_pragma_eol (parser, pragma_tok);
29394 finish_omp_barrier ();
29397 /* OpenMP 2.5:
29398 # pragma omp critical [(name)] new-line
29399 structured-block */
29401 static tree
29402 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29404 tree stmt, name = NULL;
29406 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29408 cp_lexer_consume_token (parser->lexer);
29410 name = cp_parser_identifier (parser);
29412 if (name == error_mark_node
29413 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29414 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29415 /*or_comma=*/false,
29416 /*consume_paren=*/true);
29417 if (name == error_mark_node)
29418 name = NULL;
29420 cp_parser_require_pragma_eol (parser, pragma_tok);
29422 stmt = cp_parser_omp_structured_block (parser);
29423 return c_finish_omp_critical (input_location, stmt, name);
29426 /* OpenMP 2.5:
29427 # pragma omp flush flush-vars[opt] new-line
29429 flush-vars:
29430 ( variable-list ) */
29432 static void
29433 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29435 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29436 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29437 cp_parser_require_pragma_eol (parser, pragma_tok);
29439 finish_omp_flush ();
29442 /* Helper function, to parse omp for increment expression. */
29444 static tree
29445 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29447 tree cond = cp_parser_binary_expression (parser, false, true,
29448 PREC_NOT_OPERATOR, NULL);
29449 if (cond == error_mark_node
29450 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29452 cp_parser_skip_to_end_of_statement (parser);
29453 return error_mark_node;
29456 switch (TREE_CODE (cond))
29458 case GT_EXPR:
29459 case GE_EXPR:
29460 case LT_EXPR:
29461 case LE_EXPR:
29462 break;
29463 case NE_EXPR:
29464 if (code == CILK_SIMD || code == CILK_FOR)
29465 break;
29466 /* Fall through: OpenMP disallows NE_EXPR. */
29467 default:
29468 return error_mark_node;
29471 /* If decl is an iterator, preserve LHS and RHS of the relational
29472 expr until finish_omp_for. */
29473 if (decl
29474 && (type_dependent_expression_p (decl)
29475 || CLASS_TYPE_P (TREE_TYPE (decl))))
29476 return cond;
29478 return build_x_binary_op (input_location, TREE_CODE (cond),
29479 TREE_OPERAND (cond, 0), ERROR_MARK,
29480 TREE_OPERAND (cond, 1), ERROR_MARK,
29481 /*overload=*/NULL, tf_warning_or_error);
29484 /* Helper function, to parse omp for increment expression. */
29486 static tree
29487 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29489 cp_token *token = cp_lexer_peek_token (parser->lexer);
29490 enum tree_code op;
29491 tree lhs, rhs;
29492 cp_id_kind idk;
29493 bool decl_first;
29495 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29497 op = (token->type == CPP_PLUS_PLUS
29498 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29499 cp_lexer_consume_token (parser->lexer);
29500 lhs = cp_parser_simple_cast_expression (parser);
29501 if (lhs != decl)
29502 return error_mark_node;
29503 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29506 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29507 if (lhs != decl)
29508 return error_mark_node;
29510 token = cp_lexer_peek_token (parser->lexer);
29511 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29513 op = (token->type == CPP_PLUS_PLUS
29514 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29515 cp_lexer_consume_token (parser->lexer);
29516 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29519 op = cp_parser_assignment_operator_opt (parser);
29520 if (op == ERROR_MARK)
29521 return error_mark_node;
29523 if (op != NOP_EXPR)
29525 rhs = cp_parser_assignment_expression (parser);
29526 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29527 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29530 lhs = cp_parser_binary_expression (parser, false, false,
29531 PREC_ADDITIVE_EXPRESSION, NULL);
29532 token = cp_lexer_peek_token (parser->lexer);
29533 decl_first = lhs == decl;
29534 if (decl_first)
29535 lhs = NULL_TREE;
29536 if (token->type != CPP_PLUS
29537 && token->type != CPP_MINUS)
29538 return error_mark_node;
29542 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29543 cp_lexer_consume_token (parser->lexer);
29544 rhs = cp_parser_binary_expression (parser, false, false,
29545 PREC_ADDITIVE_EXPRESSION, NULL);
29546 token = cp_lexer_peek_token (parser->lexer);
29547 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29549 if (lhs == NULL_TREE)
29551 if (op == PLUS_EXPR)
29552 lhs = rhs;
29553 else
29554 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29555 tf_warning_or_error);
29557 else
29558 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29559 ERROR_MARK, NULL, tf_warning_or_error);
29562 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29564 if (!decl_first)
29566 if (rhs != decl || op == MINUS_EXPR)
29567 return error_mark_node;
29568 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29570 else
29571 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29573 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29576 /* Parse the initialization statement of either an OpenMP for loop or
29577 a Cilk Plus for loop.
29579 Return true if the resulting construct should have an
29580 OMP_CLAUSE_PRIVATE added to it. */
29582 static bool
29583 cp_parser_omp_for_loop_init (cp_parser *parser,
29584 enum tree_code code,
29585 tree &this_pre_body,
29586 vec<tree, va_gc> *for_block,
29587 tree &init,
29588 tree &decl,
29589 tree &real_decl)
29591 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29592 return false;
29594 bool add_private_clause = false;
29596 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29598 init-expr:
29599 var = lb
29600 integer-type var = lb
29601 random-access-iterator-type var = lb
29602 pointer-type var = lb
29604 cp_decl_specifier_seq type_specifiers;
29606 /* First, try to parse as an initialized declaration. See
29607 cp_parser_condition, from whence the bulk of this is copied. */
29609 cp_parser_parse_tentatively (parser);
29610 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29611 /*is_trailing_return=*/false,
29612 &type_specifiers);
29613 if (cp_parser_parse_definitely (parser))
29615 /* If parsing a type specifier seq succeeded, then this
29616 MUST be a initialized declaration. */
29617 tree asm_specification, attributes;
29618 cp_declarator *declarator;
29620 declarator = cp_parser_declarator (parser,
29621 CP_PARSER_DECLARATOR_NAMED,
29622 /*ctor_dtor_or_conv_p=*/NULL,
29623 /*parenthesized_p=*/NULL,
29624 /*member_p=*/false,
29625 /*friend_p=*/false);
29626 attributes = cp_parser_attributes_opt (parser);
29627 asm_specification = cp_parser_asm_specification_opt (parser);
29629 if (declarator == cp_error_declarator)
29630 cp_parser_skip_to_end_of_statement (parser);
29632 else
29634 tree pushed_scope, auto_node;
29636 decl = start_decl (declarator, &type_specifiers,
29637 SD_INITIALIZED, attributes,
29638 /*prefix_attributes=*/NULL_TREE,
29639 &pushed_scope);
29641 auto_node = type_uses_auto (TREE_TYPE (decl));
29642 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29644 if (cp_lexer_next_token_is (parser->lexer,
29645 CPP_OPEN_PAREN))
29647 if (code != CILK_SIMD && code != CILK_FOR)
29648 error ("parenthesized initialization is not allowed in "
29649 "OpenMP %<for%> loop");
29650 else
29651 error ("parenthesized initialization is "
29652 "not allowed in for-loop");
29654 else
29655 /* Trigger an error. */
29656 cp_parser_require (parser, CPP_EQ, RT_EQ);
29658 init = error_mark_node;
29659 cp_parser_skip_to_end_of_statement (parser);
29661 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29662 || type_dependent_expression_p (decl)
29663 || auto_node)
29665 bool is_direct_init, is_non_constant_init;
29667 init = cp_parser_initializer (parser,
29668 &is_direct_init,
29669 &is_non_constant_init);
29671 if (auto_node)
29673 TREE_TYPE (decl)
29674 = do_auto_deduction (TREE_TYPE (decl), init,
29675 auto_node);
29677 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29678 && !type_dependent_expression_p (decl))
29679 goto non_class;
29682 cp_finish_decl (decl, init, !is_non_constant_init,
29683 asm_specification,
29684 LOOKUP_ONLYCONVERTING);
29685 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29687 vec_safe_push (for_block, this_pre_body);
29688 init = NULL_TREE;
29690 else
29691 init = pop_stmt_list (this_pre_body);
29692 this_pre_body = NULL_TREE;
29694 else
29696 /* Consume '='. */
29697 cp_lexer_consume_token (parser->lexer);
29698 init = cp_parser_assignment_expression (parser);
29700 non_class:
29701 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29702 init = error_mark_node;
29703 else
29704 cp_finish_decl (decl, NULL_TREE,
29705 /*init_const_expr_p=*/false,
29706 asm_specification,
29707 LOOKUP_ONLYCONVERTING);
29710 if (pushed_scope)
29711 pop_scope (pushed_scope);
29714 else
29716 cp_id_kind idk;
29717 /* If parsing a type specifier sequence failed, then
29718 this MUST be a simple expression. */
29719 if (code == CILK_FOR)
29720 error ("%<_Cilk_for%> allows expression instead of declaration only "
29721 "in C, not in C++");
29722 cp_parser_parse_tentatively (parser);
29723 decl = cp_parser_primary_expression (parser, false, false,
29724 false, &idk);
29725 if (!cp_parser_error_occurred (parser)
29726 && decl
29727 && DECL_P (decl)
29728 && CLASS_TYPE_P (TREE_TYPE (decl)))
29730 tree rhs;
29732 cp_parser_parse_definitely (parser);
29733 cp_parser_require (parser, CPP_EQ, RT_EQ);
29734 rhs = cp_parser_assignment_expression (parser);
29735 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29736 decl, NOP_EXPR,
29737 rhs,
29738 tf_warning_or_error));
29739 add_private_clause = true;
29741 else
29743 decl = NULL;
29744 cp_parser_abort_tentative_parse (parser);
29745 init = cp_parser_expression (parser);
29746 if (init)
29748 if (TREE_CODE (init) == MODIFY_EXPR
29749 || TREE_CODE (init) == MODOP_EXPR)
29750 real_decl = TREE_OPERAND (init, 0);
29754 return add_private_clause;
29757 /* Parse the restricted form of the for statement allowed by OpenMP. */
29759 static tree
29760 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29761 tree *cclauses)
29763 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29764 tree real_decl, initv, condv, incrv, declv;
29765 tree this_pre_body, cl;
29766 location_t loc_first;
29767 bool collapse_err = false;
29768 int i, collapse = 1, nbraces = 0;
29769 vec<tree, va_gc> *for_block = make_tree_vector ();
29771 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29772 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29773 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29775 gcc_assert (collapse >= 1);
29777 declv = make_tree_vec (collapse);
29778 initv = make_tree_vec (collapse);
29779 condv = make_tree_vec (collapse);
29780 incrv = make_tree_vec (collapse);
29782 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29784 for (i = 0; i < collapse; i++)
29786 int bracecount = 0;
29787 bool add_private_clause = false;
29788 location_t loc;
29790 if (code != CILK_FOR
29791 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29793 cp_parser_error (parser, "for statement expected");
29794 return NULL;
29796 if (code == CILK_FOR
29797 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
29799 cp_parser_error (parser, "_Cilk_for statement expected");
29800 return NULL;
29802 loc = cp_lexer_consume_token (parser->lexer)->location;
29804 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29805 return NULL;
29807 init = decl = real_decl = NULL;
29808 this_pre_body = push_stmt_list ();
29810 add_private_clause
29811 |= cp_parser_omp_for_loop_init (parser, code,
29812 this_pre_body, for_block,
29813 init, decl, real_decl);
29815 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29816 if (this_pre_body)
29818 this_pre_body = pop_stmt_list (this_pre_body);
29819 if (pre_body)
29821 tree t = pre_body;
29822 pre_body = push_stmt_list ();
29823 add_stmt (t);
29824 add_stmt (this_pre_body);
29825 pre_body = pop_stmt_list (pre_body);
29827 else
29828 pre_body = this_pre_body;
29831 if (decl)
29832 real_decl = decl;
29833 if (cclauses != NULL
29834 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29835 && real_decl != NULL_TREE)
29837 tree *c;
29838 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29839 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29840 && OMP_CLAUSE_DECL (*c) == real_decl)
29842 error_at (loc, "iteration variable %qD"
29843 " should not be firstprivate", real_decl);
29844 *c = OMP_CLAUSE_CHAIN (*c);
29846 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29847 && OMP_CLAUSE_DECL (*c) == real_decl)
29849 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29850 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29851 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29852 OMP_CLAUSE_DECL (l) = real_decl;
29853 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29854 if (code == OMP_SIMD)
29856 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29857 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
29859 else
29861 OMP_CLAUSE_CHAIN (l) = clauses;
29862 clauses = l;
29864 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29865 CP_OMP_CLAUSE_INFO (*c) = NULL;
29866 add_private_clause = false;
29868 else
29870 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29871 && OMP_CLAUSE_DECL (*c) == real_decl)
29872 add_private_clause = false;
29873 c = &OMP_CLAUSE_CHAIN (*c);
29877 if (add_private_clause)
29879 tree c;
29880 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29882 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29883 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29884 && OMP_CLAUSE_DECL (c) == decl)
29885 break;
29886 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29887 && OMP_CLAUSE_DECL (c) == decl)
29888 error_at (loc, "iteration variable %qD "
29889 "should not be firstprivate",
29890 decl);
29891 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29892 && OMP_CLAUSE_DECL (c) == decl)
29893 error_at (loc, "iteration variable %qD should not be reduction",
29894 decl);
29896 if (c == NULL)
29898 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29899 OMP_CLAUSE_DECL (c) = decl;
29900 c = finish_omp_clauses (c);
29901 if (c)
29903 OMP_CLAUSE_CHAIN (c) = clauses;
29904 clauses = c;
29909 cond = NULL;
29910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29911 cond = cp_parser_omp_for_cond (parser, decl, code);
29912 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29914 incr = NULL;
29915 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29917 /* If decl is an iterator, preserve the operator on decl
29918 until finish_omp_for. */
29919 if (real_decl
29920 && ((processing_template_decl
29921 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29922 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29923 incr = cp_parser_omp_for_incr (parser, real_decl);
29924 else
29925 incr = cp_parser_expression (parser);
29926 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29927 SET_EXPR_LOCATION (incr, input_location);
29930 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29931 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29932 /*or_comma=*/false,
29933 /*consume_paren=*/true);
29935 TREE_VEC_ELT (declv, i) = decl;
29936 TREE_VEC_ELT (initv, i) = init;
29937 TREE_VEC_ELT (condv, i) = cond;
29938 TREE_VEC_ELT (incrv, i) = incr;
29940 if (i == collapse - 1)
29941 break;
29943 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29944 in between the collapsed for loops to be still considered perfectly
29945 nested. Hopefully the final version clarifies this.
29946 For now handle (multiple) {'s and empty statements. */
29947 cp_parser_parse_tentatively (parser);
29950 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29951 break;
29952 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29954 cp_lexer_consume_token (parser->lexer);
29955 bracecount++;
29957 else if (bracecount
29958 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29959 cp_lexer_consume_token (parser->lexer);
29960 else
29962 loc = cp_lexer_peek_token (parser->lexer)->location;
29963 error_at (loc, "not enough collapsed for loops");
29964 collapse_err = true;
29965 cp_parser_abort_tentative_parse (parser);
29966 declv = NULL_TREE;
29967 break;
29970 while (1);
29972 if (declv)
29974 cp_parser_parse_definitely (parser);
29975 nbraces += bracecount;
29979 /* Note that we saved the original contents of this flag when we entered
29980 the structured block, and so we don't need to re-save it here. */
29981 if (code == CILK_SIMD || code == CILK_FOR)
29982 parser->in_statement = IN_CILK_SIMD_FOR;
29983 else
29984 parser->in_statement = IN_OMP_FOR;
29986 /* Note that the grammar doesn't call for a structured block here,
29987 though the loop as a whole is a structured block. */
29988 body = push_stmt_list ();
29989 cp_parser_statement (parser, NULL_TREE, false, NULL);
29990 body = pop_stmt_list (body);
29992 if (declv == NULL_TREE)
29993 ret = NULL_TREE;
29994 else
29995 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29996 pre_body, clauses);
29998 while (nbraces)
30000 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30002 cp_lexer_consume_token (parser->lexer);
30003 nbraces--;
30005 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30006 cp_lexer_consume_token (parser->lexer);
30007 else
30009 if (!collapse_err)
30011 error_at (cp_lexer_peek_token (parser->lexer)->location,
30012 "collapsed loops not perfectly nested");
30014 collapse_err = true;
30015 cp_parser_statement_seq_opt (parser, NULL);
30016 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30017 break;
30021 while (!for_block->is_empty ())
30022 add_stmt (pop_stmt_list (for_block->pop ()));
30023 release_tree_vector (for_block);
30025 return ret;
30028 /* Helper function for OpenMP parsing, split clauses and call
30029 finish_omp_clauses on each of the set of clauses afterwards. */
30031 static void
30032 cp_omp_split_clauses (location_t loc, enum tree_code code,
30033 omp_clause_mask mask, tree clauses, tree *cclauses)
30035 int i;
30036 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30037 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30038 if (cclauses[i])
30039 cclauses[i] = finish_omp_clauses (cclauses[i]);
30042 /* OpenMP 4.0:
30043 #pragma omp simd simd-clause[optseq] new-line
30044 for-loop */
30046 #define OMP_SIMD_CLAUSE_MASK \
30047 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30055 static tree
30056 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30057 char *p_name, omp_clause_mask mask, tree *cclauses)
30059 tree clauses, sb, ret;
30060 unsigned int save;
30061 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30063 strcat (p_name, " simd");
30064 mask |= OMP_SIMD_CLAUSE_MASK;
30065 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30067 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30068 cclauses == NULL);
30069 if (cclauses)
30071 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30072 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30075 sb = begin_omp_structured_block ();
30076 save = cp_parser_begin_omp_structured_block (parser);
30078 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30080 cp_parser_end_omp_structured_block (parser, save);
30081 add_stmt (finish_omp_structured_block (sb));
30083 return ret;
30086 /* OpenMP 2.5:
30087 #pragma omp for for-clause[optseq] new-line
30088 for-loop
30090 OpenMP 4.0:
30091 #pragma omp for simd for-simd-clause[optseq] new-line
30092 for-loop */
30094 #define OMP_FOR_CLAUSE_MASK \
30095 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30104 static tree
30105 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30106 char *p_name, omp_clause_mask mask, tree *cclauses)
30108 tree clauses, sb, ret;
30109 unsigned int save;
30110 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30112 strcat (p_name, " for");
30113 mask |= OMP_FOR_CLAUSE_MASK;
30114 if (cclauses)
30115 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30117 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30119 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30120 const char *p = IDENTIFIER_POINTER (id);
30122 if (strcmp (p, "simd") == 0)
30124 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30125 if (cclauses == NULL)
30126 cclauses = cclauses_buf;
30128 cp_lexer_consume_token (parser->lexer);
30129 if (!flag_openmp) /* flag_openmp_simd */
30130 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30131 cclauses);
30132 sb = begin_omp_structured_block ();
30133 save = cp_parser_begin_omp_structured_block (parser);
30134 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30135 cclauses);
30136 cp_parser_end_omp_structured_block (parser, save);
30137 tree body = finish_omp_structured_block (sb);
30138 if (ret == NULL)
30139 return ret;
30140 ret = make_node (OMP_FOR);
30141 TREE_TYPE (ret) = void_type_node;
30142 OMP_FOR_BODY (ret) = body;
30143 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30144 SET_EXPR_LOCATION (ret, loc);
30145 add_stmt (ret);
30146 return ret;
30149 if (!flag_openmp) /* flag_openmp_simd */
30151 cp_parser_require_pragma_eol (parser, pragma_tok);
30152 return NULL_TREE;
30155 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30156 cclauses == NULL);
30157 if (cclauses)
30159 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30160 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30163 sb = begin_omp_structured_block ();
30164 save = cp_parser_begin_omp_structured_block (parser);
30166 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30168 cp_parser_end_omp_structured_block (parser, save);
30169 add_stmt (finish_omp_structured_block (sb));
30171 return ret;
30174 /* OpenMP 2.5:
30175 # pragma omp master new-line
30176 structured-block */
30178 static tree
30179 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30181 cp_parser_require_pragma_eol (parser, pragma_tok);
30182 return c_finish_omp_master (input_location,
30183 cp_parser_omp_structured_block (parser));
30186 /* OpenMP 2.5:
30187 # pragma omp ordered new-line
30188 structured-block */
30190 static tree
30191 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30193 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30194 cp_parser_require_pragma_eol (parser, pragma_tok);
30195 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30198 /* OpenMP 2.5:
30200 section-scope:
30201 { section-sequence }
30203 section-sequence:
30204 section-directive[opt] structured-block
30205 section-sequence section-directive structured-block */
30207 static tree
30208 cp_parser_omp_sections_scope (cp_parser *parser)
30210 tree stmt, substmt;
30211 bool error_suppress = false;
30212 cp_token *tok;
30214 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30215 return NULL_TREE;
30217 stmt = push_stmt_list ();
30219 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30221 substmt = cp_parser_omp_structured_block (parser);
30222 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30223 add_stmt (substmt);
30226 while (1)
30228 tok = cp_lexer_peek_token (parser->lexer);
30229 if (tok->type == CPP_CLOSE_BRACE)
30230 break;
30231 if (tok->type == CPP_EOF)
30232 break;
30234 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30236 cp_lexer_consume_token (parser->lexer);
30237 cp_parser_require_pragma_eol (parser, tok);
30238 error_suppress = false;
30240 else if (!error_suppress)
30242 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30243 error_suppress = true;
30246 substmt = cp_parser_omp_structured_block (parser);
30247 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30248 add_stmt (substmt);
30250 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30252 substmt = pop_stmt_list (stmt);
30254 stmt = make_node (OMP_SECTIONS);
30255 TREE_TYPE (stmt) = void_type_node;
30256 OMP_SECTIONS_BODY (stmt) = substmt;
30258 add_stmt (stmt);
30259 return stmt;
30262 /* OpenMP 2.5:
30263 # pragma omp sections sections-clause[optseq] newline
30264 sections-scope */
30266 #define OMP_SECTIONS_CLAUSE_MASK \
30267 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30273 static tree
30274 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30275 char *p_name, omp_clause_mask mask, tree *cclauses)
30277 tree clauses, ret;
30278 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30280 strcat (p_name, " sections");
30281 mask |= OMP_SECTIONS_CLAUSE_MASK;
30282 if (cclauses)
30283 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30285 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30286 cclauses == NULL);
30287 if (cclauses)
30289 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30290 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30293 ret = cp_parser_omp_sections_scope (parser);
30294 if (ret)
30295 OMP_SECTIONS_CLAUSES (ret) = clauses;
30297 return ret;
30300 /* OpenMP 2.5:
30301 # pragma omp parallel parallel-clause[optseq] new-line
30302 structured-block
30303 # pragma omp parallel for parallel-for-clause[optseq] new-line
30304 structured-block
30305 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30306 structured-block
30308 OpenMP 4.0:
30309 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30310 structured-block */
30312 #define OMP_PARALLEL_CLAUSE_MASK \
30313 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30323 static tree
30324 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30325 char *p_name, omp_clause_mask mask, tree *cclauses)
30327 tree stmt, clauses, block;
30328 unsigned int save;
30329 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30331 strcat (p_name, " parallel");
30332 mask |= OMP_PARALLEL_CLAUSE_MASK;
30334 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30336 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30337 if (cclauses == NULL)
30338 cclauses = cclauses_buf;
30340 cp_lexer_consume_token (parser->lexer);
30341 if (!flag_openmp) /* flag_openmp_simd */
30342 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30343 block = begin_omp_parallel ();
30344 save = cp_parser_begin_omp_structured_block (parser);
30345 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30346 cp_parser_end_omp_structured_block (parser, save);
30347 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30348 block);
30349 if (ret == NULL_TREE)
30350 return ret;
30351 OMP_PARALLEL_COMBINED (stmt) = 1;
30352 return stmt;
30354 else if (cclauses)
30356 error_at (loc, "expected %<for%> after %qs", p_name);
30357 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30358 return NULL_TREE;
30360 else if (!flag_openmp) /* flag_openmp_simd */
30362 cp_parser_require_pragma_eol (parser, pragma_tok);
30363 return NULL_TREE;
30365 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30367 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30368 const char *p = IDENTIFIER_POINTER (id);
30369 if (strcmp (p, "sections") == 0)
30371 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30372 cclauses = cclauses_buf;
30374 cp_lexer_consume_token (parser->lexer);
30375 block = begin_omp_parallel ();
30376 save = cp_parser_begin_omp_structured_block (parser);
30377 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30378 cp_parser_end_omp_structured_block (parser, save);
30379 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30380 block);
30381 OMP_PARALLEL_COMBINED (stmt) = 1;
30382 return stmt;
30386 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30388 block = begin_omp_parallel ();
30389 save = cp_parser_begin_omp_structured_block (parser);
30390 cp_parser_statement (parser, NULL_TREE, false, NULL);
30391 cp_parser_end_omp_structured_block (parser, save);
30392 stmt = finish_omp_parallel (clauses, block);
30393 return stmt;
30396 /* OpenMP 2.5:
30397 # pragma omp single single-clause[optseq] new-line
30398 structured-block */
30400 #define OMP_SINGLE_CLAUSE_MASK \
30401 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30406 static tree
30407 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30409 tree stmt = make_node (OMP_SINGLE);
30410 TREE_TYPE (stmt) = void_type_node;
30412 OMP_SINGLE_CLAUSES (stmt)
30413 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30414 "#pragma omp single", pragma_tok);
30415 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30417 return add_stmt (stmt);
30420 /* OpenMP 3.0:
30421 # pragma omp task task-clause[optseq] new-line
30422 structured-block */
30424 #define OMP_TASK_CLAUSE_MASK \
30425 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30435 static tree
30436 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30438 tree clauses, block;
30439 unsigned int save;
30441 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30442 "#pragma omp task", pragma_tok);
30443 block = begin_omp_task ();
30444 save = cp_parser_begin_omp_structured_block (parser);
30445 cp_parser_statement (parser, NULL_TREE, false, NULL);
30446 cp_parser_end_omp_structured_block (parser, save);
30447 return finish_omp_task (clauses, block);
30450 /* OpenMP 3.0:
30451 # pragma omp taskwait new-line */
30453 static void
30454 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30456 cp_parser_require_pragma_eol (parser, pragma_tok);
30457 finish_omp_taskwait ();
30460 /* OpenMP 3.1:
30461 # pragma omp taskyield new-line */
30463 static void
30464 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30466 cp_parser_require_pragma_eol (parser, pragma_tok);
30467 finish_omp_taskyield ();
30470 /* OpenMP 4.0:
30471 # pragma omp taskgroup new-line
30472 structured-block */
30474 static tree
30475 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30477 cp_parser_require_pragma_eol (parser, pragma_tok);
30478 return c_finish_omp_taskgroup (input_location,
30479 cp_parser_omp_structured_block (parser));
30483 /* OpenMP 2.5:
30484 # pragma omp threadprivate (variable-list) */
30486 static void
30487 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30489 tree vars;
30491 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30492 cp_parser_require_pragma_eol (parser, pragma_tok);
30494 finish_omp_threadprivate (vars);
30497 /* OpenMP 4.0:
30498 # pragma omp cancel cancel-clause[optseq] new-line */
30500 #define OMP_CANCEL_CLAUSE_MASK \
30501 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30507 static void
30508 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30510 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30511 "#pragma omp cancel", pragma_tok);
30512 finish_omp_cancel (clauses);
30515 /* OpenMP 4.0:
30516 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30518 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30519 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30524 static void
30525 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30527 tree clauses;
30528 bool point_seen = false;
30530 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30532 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30533 const char *p = IDENTIFIER_POINTER (id);
30535 if (strcmp (p, "point") == 0)
30537 cp_lexer_consume_token (parser->lexer);
30538 point_seen = true;
30541 if (!point_seen)
30543 cp_parser_error (parser, "expected %<point%>");
30544 cp_parser_require_pragma_eol (parser, pragma_tok);
30545 return;
30548 clauses = cp_parser_omp_all_clauses (parser,
30549 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30550 "#pragma omp cancellation point",
30551 pragma_tok);
30552 finish_omp_cancellation_point (clauses);
30555 /* OpenMP 4.0:
30556 #pragma omp distribute distribute-clause[optseq] new-line
30557 for-loop */
30559 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30560 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30565 static tree
30566 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30567 char *p_name, omp_clause_mask mask, tree *cclauses)
30569 tree clauses, sb, ret;
30570 unsigned int save;
30571 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30573 strcat (p_name, " distribute");
30574 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30576 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30578 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30579 const char *p = IDENTIFIER_POINTER (id);
30580 bool simd = false;
30581 bool parallel = false;
30583 if (strcmp (p, "simd") == 0)
30584 simd = true;
30585 else
30586 parallel = strcmp (p, "parallel") == 0;
30587 if (parallel || simd)
30589 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30590 if (cclauses == NULL)
30591 cclauses = cclauses_buf;
30592 cp_lexer_consume_token (parser->lexer);
30593 if (!flag_openmp) /* flag_openmp_simd */
30595 if (simd)
30596 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30597 cclauses);
30598 else
30599 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30600 cclauses);
30602 sb = begin_omp_structured_block ();
30603 save = cp_parser_begin_omp_structured_block (parser);
30604 if (simd)
30605 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30606 cclauses);
30607 else
30608 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30609 cclauses);
30610 cp_parser_end_omp_structured_block (parser, save);
30611 tree body = finish_omp_structured_block (sb);
30612 if (ret == NULL)
30613 return ret;
30614 ret = make_node (OMP_DISTRIBUTE);
30615 TREE_TYPE (ret) = void_type_node;
30616 OMP_FOR_BODY (ret) = body;
30617 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30618 SET_EXPR_LOCATION (ret, loc);
30619 add_stmt (ret);
30620 return ret;
30623 if (!flag_openmp) /* flag_openmp_simd */
30625 cp_parser_require_pragma_eol (parser, pragma_tok);
30626 return NULL_TREE;
30629 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30630 cclauses == NULL);
30631 if (cclauses)
30633 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30634 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30637 sb = begin_omp_structured_block ();
30638 save = cp_parser_begin_omp_structured_block (parser);
30640 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30642 cp_parser_end_omp_structured_block (parser, save);
30643 add_stmt (finish_omp_structured_block (sb));
30645 return ret;
30648 /* OpenMP 4.0:
30649 # pragma omp teams teams-clause[optseq] new-line
30650 structured-block */
30652 #define OMP_TEAMS_CLAUSE_MASK \
30653 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30661 static tree
30662 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30663 char *p_name, omp_clause_mask mask, tree *cclauses)
30665 tree clauses, sb, ret;
30666 unsigned int save;
30667 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30669 strcat (p_name, " teams");
30670 mask |= OMP_TEAMS_CLAUSE_MASK;
30672 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30674 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30675 const char *p = IDENTIFIER_POINTER (id);
30676 if (strcmp (p, "distribute") == 0)
30678 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30679 if (cclauses == NULL)
30680 cclauses = cclauses_buf;
30682 cp_lexer_consume_token (parser->lexer);
30683 if (!flag_openmp) /* flag_openmp_simd */
30684 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30685 cclauses);
30686 sb = begin_omp_structured_block ();
30687 save = cp_parser_begin_omp_structured_block (parser);
30688 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30689 cclauses);
30690 cp_parser_end_omp_structured_block (parser, save);
30691 tree body = finish_omp_structured_block (sb);
30692 if (ret == NULL)
30693 return ret;
30694 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30695 ret = make_node (OMP_TEAMS);
30696 TREE_TYPE (ret) = void_type_node;
30697 OMP_TEAMS_CLAUSES (ret) = clauses;
30698 OMP_TEAMS_BODY (ret) = body;
30699 return add_stmt (ret);
30702 if (!flag_openmp) /* flag_openmp_simd */
30704 cp_parser_require_pragma_eol (parser, pragma_tok);
30705 return NULL_TREE;
30708 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30709 cclauses == NULL);
30710 if (cclauses)
30712 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30713 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30716 tree stmt = make_node (OMP_TEAMS);
30717 TREE_TYPE (stmt) = void_type_node;
30718 OMP_TEAMS_CLAUSES (stmt) = clauses;
30719 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30721 return add_stmt (stmt);
30724 /* OpenMP 4.0:
30725 # pragma omp target data target-data-clause[optseq] new-line
30726 structured-block */
30728 #define OMP_TARGET_DATA_CLAUSE_MASK \
30729 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30733 static tree
30734 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30736 tree stmt = make_node (OMP_TARGET_DATA);
30737 TREE_TYPE (stmt) = void_type_node;
30739 OMP_TARGET_DATA_CLAUSES (stmt)
30740 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30741 "#pragma omp target data", pragma_tok);
30742 keep_next_level (true);
30743 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30745 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30746 return add_stmt (stmt);
30749 /* OpenMP 4.0:
30750 # pragma omp target update target-update-clause[optseq] new-line */
30752 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30753 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30758 static bool
30759 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30760 enum pragma_context context)
30762 if (context == pragma_stmt)
30764 error_at (pragma_tok->location,
30765 "%<#pragma omp target update%> may only be "
30766 "used in compound statements");
30767 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30768 return false;
30771 tree clauses
30772 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30773 "#pragma omp target update", pragma_tok);
30774 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30775 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30777 error_at (pragma_tok->location,
30778 "%<#pragma omp target update must contain at least one "
30779 "%<from%> or %<to%> clauses");
30780 return false;
30783 tree stmt = make_node (OMP_TARGET_UPDATE);
30784 TREE_TYPE (stmt) = void_type_node;
30785 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30786 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30787 add_stmt (stmt);
30788 return false;
30791 /* OpenMP 4.0:
30792 # pragma omp target target-clause[optseq] new-line
30793 structured-block */
30795 #define OMP_TARGET_CLAUSE_MASK \
30796 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30800 static bool
30801 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30802 enum pragma_context context)
30804 if (context != pragma_stmt && context != pragma_compound)
30806 cp_parser_error (parser, "expected declaration specifiers");
30807 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30808 return false;
30811 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30813 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30814 const char *p = IDENTIFIER_POINTER (id);
30816 if (strcmp (p, "teams") == 0)
30818 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30819 char p_name[sizeof ("#pragma omp target teams distribute "
30820 "parallel for simd")];
30822 cp_lexer_consume_token (parser->lexer);
30823 strcpy (p_name, "#pragma omp target");
30824 if (!flag_openmp) /* flag_openmp_simd */
30826 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
30827 OMP_TARGET_CLAUSE_MASK,
30828 cclauses);
30829 return stmt != NULL_TREE;
30831 keep_next_level (true);
30832 tree sb = begin_omp_structured_block ();
30833 unsigned save = cp_parser_begin_omp_structured_block (parser);
30834 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30835 OMP_TARGET_CLAUSE_MASK, cclauses);
30836 cp_parser_end_omp_structured_block (parser, save);
30837 tree body = finish_omp_structured_block (sb);
30838 if (ret == NULL_TREE)
30839 return false;
30840 tree stmt = make_node (OMP_TARGET);
30841 TREE_TYPE (stmt) = void_type_node;
30842 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30843 OMP_TARGET_BODY (stmt) = body;
30844 add_stmt (stmt);
30845 return true;
30847 else if (!flag_openmp) /* flag_openmp_simd */
30849 cp_parser_require_pragma_eol (parser, pragma_tok);
30850 return false;
30852 else if (strcmp (p, "data") == 0)
30854 cp_lexer_consume_token (parser->lexer);
30855 cp_parser_omp_target_data (parser, pragma_tok);
30856 return true;
30858 else if (strcmp (p, "update") == 0)
30860 cp_lexer_consume_token (parser->lexer);
30861 return cp_parser_omp_target_update (parser, pragma_tok, context);
30865 tree stmt = make_node (OMP_TARGET);
30866 TREE_TYPE (stmt) = void_type_node;
30868 OMP_TARGET_CLAUSES (stmt)
30869 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30870 "#pragma omp target", pragma_tok);
30871 keep_next_level (true);
30872 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30874 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30875 add_stmt (stmt);
30876 return true;
30879 /* OpenMP 4.0:
30880 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30882 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30883 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30890 static void
30891 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30892 enum pragma_context context)
30894 bool first_p = parser->omp_declare_simd == NULL;
30895 cp_omp_declare_simd_data data;
30896 if (first_p)
30898 data.error_seen = false;
30899 data.fndecl_seen = false;
30900 data.tokens = vNULL;
30901 parser->omp_declare_simd = &data;
30903 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30904 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30905 cp_lexer_consume_token (parser->lexer);
30906 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30907 parser->omp_declare_simd->error_seen = true;
30908 cp_parser_require_pragma_eol (parser, pragma_tok);
30909 struct cp_token_cache *cp
30910 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30911 parser->omp_declare_simd->tokens.safe_push (cp);
30912 if (first_p)
30914 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30915 cp_parser_pragma (parser, context);
30916 switch (context)
30918 case pragma_external:
30919 cp_parser_declaration (parser);
30920 break;
30921 case pragma_member:
30922 cp_parser_member_declaration (parser);
30923 break;
30924 case pragma_objc_icode:
30925 cp_parser_block_declaration (parser, /*statement_p=*/false);
30926 break;
30927 default:
30928 cp_parser_declaration_statement (parser);
30929 break;
30931 if (parser->omp_declare_simd
30932 && !parser->omp_declare_simd->error_seen
30933 && !parser->omp_declare_simd->fndecl_seen)
30934 error_at (pragma_tok->location,
30935 "%<#pragma omp declare simd%> not immediately followed by "
30936 "function declaration or definition");
30937 data.tokens.release ();
30938 parser->omp_declare_simd = NULL;
30942 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30943 This function is modelled similar to the late parsing of omp declare
30944 simd. */
30946 static tree
30947 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30949 struct cp_token_cache *ce;
30950 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30951 int ii = 0;
30953 if (parser->omp_declare_simd != NULL)
30955 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30956 " marked as a Cilk Plus SIMD-enabled function");
30957 XDELETE (parser->cilk_simd_fn_info);
30958 parser->cilk_simd_fn_info = NULL;
30959 return attrs;
30961 if (!info->error_seen && info->fndecl_seen)
30963 error ("vector attribute not immediately followed by a single function"
30964 " declaration or definition");
30965 info->error_seen = true;
30967 if (info->error_seen)
30968 return attrs;
30970 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30972 tree c, cl;
30974 cp_parser_push_lexer_for_tokens (parser, ce);
30975 parser->lexer->in_pragma = true;
30976 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
30977 "SIMD-enabled functions attribute",
30978 NULL);
30979 cp_parser_pop_lexer (parser);
30980 if (cl)
30981 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30983 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
30984 TREE_CHAIN (c) = attrs;
30985 attrs = c;
30987 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30988 TREE_CHAIN (c) = attrs;
30989 if (processing_template_decl)
30990 ATTR_IS_DEPENDENT (c) = 1;
30991 attrs = c;
30993 info->fndecl_seen = true;
30994 XDELETE (parser->cilk_simd_fn_info);
30995 parser->cilk_simd_fn_info = NULL;
30996 return attrs;
30999 /* Finalize #pragma omp declare simd clauses after direct declarator has
31000 been parsed, and put that into "omp declare simd" attribute. */
31002 static tree
31003 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31005 struct cp_token_cache *ce;
31006 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31007 int i;
31009 if (!data->error_seen && data->fndecl_seen)
31011 error ("%<#pragma omp declare simd%> not immediately followed by "
31012 "a single function declaration or definition");
31013 data->error_seen = true;
31014 return attrs;
31016 if (data->error_seen)
31017 return attrs;
31019 FOR_EACH_VEC_ELT (data->tokens, i, ce)
31021 tree c, cl;
31023 cp_parser_push_lexer_for_tokens (parser, ce);
31024 parser->lexer->in_pragma = true;
31025 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31026 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31027 cp_lexer_consume_token (parser->lexer);
31028 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31029 "#pragma omp declare simd", pragma_tok);
31030 cp_parser_pop_lexer (parser);
31031 if (cl)
31032 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31033 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31034 TREE_CHAIN (c) = attrs;
31035 if (processing_template_decl)
31036 ATTR_IS_DEPENDENT (c) = 1;
31037 attrs = c;
31040 data->fndecl_seen = true;
31041 return attrs;
31045 /* OpenMP 4.0:
31046 # pragma omp declare target new-line
31047 declarations and definitions
31048 # pragma omp end declare target new-line */
31050 static void
31051 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31053 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31054 scope_chain->omp_declare_target_attribute++;
31057 static void
31058 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
31060 const char *p = "";
31061 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31063 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31064 p = IDENTIFIER_POINTER (id);
31066 if (strcmp (p, "declare") == 0)
31068 cp_lexer_consume_token (parser->lexer);
31069 p = "";
31070 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31072 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31073 p = IDENTIFIER_POINTER (id);
31075 if (strcmp (p, "target") == 0)
31076 cp_lexer_consume_token (parser->lexer);
31077 else
31079 cp_parser_error (parser, "expected %<target%>");
31080 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31081 return;
31084 else
31086 cp_parser_error (parser, "expected %<declare%>");
31087 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31088 return;
31090 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31091 if (!scope_chain->omp_declare_target_attribute)
31092 error_at (pragma_tok->location,
31093 "%<#pragma omp end declare target%> without corresponding "
31094 "%<#pragma omp declare target%>");
31095 else
31096 scope_chain->omp_declare_target_attribute--;
31099 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31100 expression and optional initializer clause of
31101 #pragma omp declare reduction. We store the expression(s) as
31102 either 3, 6 or 7 special statements inside of the artificial function's
31103 body. The first two statements are DECL_EXPRs for the artificial
31104 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
31105 expression that uses those variables.
31106 If there was any INITIALIZER clause, this is followed by further statements,
31107 the fourth and fifth statements are DECL_EXPRs for the artificial
31108 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
31109 constructor variant (first token after open paren is not omp_priv),
31110 then the sixth statement is a statement with the function call expression
31111 that uses the OMP_PRIV and optionally OMP_ORIG variable.
31112 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
31113 to initialize the OMP_PRIV artificial variable and there is seventh
31114 statement, a DECL_EXPR of the OMP_PRIV statement again. */
31116 static bool
31117 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
31119 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
31120 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
31121 type = TREE_TYPE (type);
31122 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
31123 DECL_ARTIFICIAL (omp_out) = 1;
31124 pushdecl (omp_out);
31125 add_decl_expr (omp_out);
31126 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
31127 DECL_ARTIFICIAL (omp_in) = 1;
31128 pushdecl (omp_in);
31129 add_decl_expr (omp_in);
31130 tree combiner;
31131 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
31133 keep_next_level (true);
31134 tree block = begin_omp_structured_block ();
31135 combiner = cp_parser_expression (parser);
31136 finish_expr_stmt (combiner);
31137 block = finish_omp_structured_block (block);
31138 add_stmt (block);
31140 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31141 return false;
31143 const char *p = "";
31144 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31146 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31147 p = IDENTIFIER_POINTER (id);
31150 if (strcmp (p, "initializer") == 0)
31152 cp_lexer_consume_token (parser->lexer);
31153 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31154 return false;
31156 p = "";
31157 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31159 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31160 p = IDENTIFIER_POINTER (id);
31163 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
31164 DECL_ARTIFICIAL (omp_priv) = 1;
31165 pushdecl (omp_priv);
31166 add_decl_expr (omp_priv);
31167 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
31168 DECL_ARTIFICIAL (omp_orig) = 1;
31169 pushdecl (omp_orig);
31170 add_decl_expr (omp_orig);
31172 keep_next_level (true);
31173 block = begin_omp_structured_block ();
31175 bool ctor = false;
31176 if (strcmp (p, "omp_priv") == 0)
31178 bool is_direct_init, is_non_constant_init;
31179 ctor = true;
31180 cp_lexer_consume_token (parser->lexer);
31181 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
31182 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
31183 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31184 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
31185 == CPP_CLOSE_PAREN
31186 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
31187 == CPP_CLOSE_PAREN))
31189 finish_omp_structured_block (block);
31190 error ("invalid initializer clause");
31191 return false;
31193 initializer = cp_parser_initializer (parser, &is_direct_init,
31194 &is_non_constant_init);
31195 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
31196 NULL_TREE, LOOKUP_ONLYCONVERTING);
31198 else
31200 cp_parser_parse_tentatively (parser);
31201 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
31202 /*check_dependency_p=*/true,
31203 /*template_p=*/NULL,
31204 /*declarator_p=*/false,
31205 /*optional_p=*/false);
31206 vec<tree, va_gc> *args;
31207 if (fn_name == error_mark_node
31208 || cp_parser_error_occurred (parser)
31209 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31210 || ((args = cp_parser_parenthesized_expression_list
31211 (parser, non_attr, /*cast_p=*/false,
31212 /*allow_expansion_p=*/true,
31213 /*non_constant_p=*/NULL)),
31214 cp_parser_error_occurred (parser)))
31216 finish_omp_structured_block (block);
31217 cp_parser_abort_tentative_parse (parser);
31218 cp_parser_error (parser, "expected id-expression (arguments)");
31219 return false;
31221 unsigned int i;
31222 tree arg;
31223 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
31224 if (arg == omp_priv
31225 || (TREE_CODE (arg) == ADDR_EXPR
31226 && TREE_OPERAND (arg, 0) == omp_priv))
31227 break;
31228 cp_parser_abort_tentative_parse (parser);
31229 if (arg == NULL_TREE)
31230 error ("one of the initializer call arguments should be %<omp_priv%>"
31231 " or %<&omp_priv%>");
31232 initializer = cp_parser_postfix_expression (parser, false, false, false,
31233 false, NULL);
31234 finish_expr_stmt (initializer);
31237 block = finish_omp_structured_block (block);
31238 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
31239 add_stmt (block);
31241 if (ctor)
31242 add_decl_expr (omp_orig);
31244 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31245 return false;
31248 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31249 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31251 return true;
31254 /* OpenMP 4.0
31255 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31256 initializer-clause[opt] new-line
31258 initializer-clause:
31259 initializer (omp_priv initializer)
31260 initializer (function-name (argument-list)) */
31262 static void
31263 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31264 enum pragma_context)
31266 auto_vec<tree> types;
31267 enum tree_code reduc_code = ERROR_MARK;
31268 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31269 unsigned int i;
31270 cp_token *first_token;
31271 cp_token_cache *cp;
31272 int errs;
31273 void *p;
31275 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31276 p = obstack_alloc (&declarator_obstack, 0);
31278 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31279 goto fail;
31281 switch (cp_lexer_peek_token (parser->lexer)->type)
31283 case CPP_PLUS:
31284 reduc_code = PLUS_EXPR;
31285 break;
31286 case CPP_MULT:
31287 reduc_code = MULT_EXPR;
31288 break;
31289 case CPP_MINUS:
31290 reduc_code = MINUS_EXPR;
31291 break;
31292 case CPP_AND:
31293 reduc_code = BIT_AND_EXPR;
31294 break;
31295 case CPP_XOR:
31296 reduc_code = BIT_XOR_EXPR;
31297 break;
31298 case CPP_OR:
31299 reduc_code = BIT_IOR_EXPR;
31300 break;
31301 case CPP_AND_AND:
31302 reduc_code = TRUTH_ANDIF_EXPR;
31303 break;
31304 case CPP_OR_OR:
31305 reduc_code = TRUTH_ORIF_EXPR;
31306 break;
31307 case CPP_NAME:
31308 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31309 break;
31310 default:
31311 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31312 "%<|%>, %<&&%>, %<||%> or identifier");
31313 goto fail;
31316 if (reduc_code != ERROR_MARK)
31317 cp_lexer_consume_token (parser->lexer);
31319 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31320 if (reduc_id == error_mark_node)
31321 goto fail;
31323 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31324 goto fail;
31326 /* Types may not be defined in declare reduction type list. */
31327 const char *saved_message;
31328 saved_message = parser->type_definition_forbidden_message;
31329 parser->type_definition_forbidden_message
31330 = G_("types may not be defined in declare reduction type list");
31331 bool saved_colon_corrects_to_scope_p;
31332 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31333 parser->colon_corrects_to_scope_p = false;
31334 bool saved_colon_doesnt_start_class_def_p;
31335 saved_colon_doesnt_start_class_def_p
31336 = parser->colon_doesnt_start_class_def_p;
31337 parser->colon_doesnt_start_class_def_p = true;
31339 while (true)
31341 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31342 type = cp_parser_type_id (parser);
31343 if (type == error_mark_node)
31345 else if (ARITHMETIC_TYPE_P (type)
31346 && (orig_reduc_id == NULL_TREE
31347 || (TREE_CODE (type) != COMPLEX_TYPE
31348 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31349 "min") == 0
31350 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31351 "max") == 0))))
31352 error_at (loc, "predeclared arithmetic type %qT in "
31353 "%<#pragma omp declare reduction%>", type);
31354 else if (TREE_CODE (type) == FUNCTION_TYPE
31355 || TREE_CODE (type) == METHOD_TYPE
31356 || TREE_CODE (type) == ARRAY_TYPE)
31357 error_at (loc, "function or array type %qT in "
31358 "%<#pragma omp declare reduction%>", type);
31359 else if (TREE_CODE (type) == REFERENCE_TYPE)
31360 error_at (loc, "reference type %qT in "
31361 "%<#pragma omp declare reduction%>", type);
31362 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31363 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31364 "%<#pragma omp declare reduction%>", type);
31365 else
31366 types.safe_push (type);
31368 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31369 cp_lexer_consume_token (parser->lexer);
31370 else
31371 break;
31374 /* Restore the saved message. */
31375 parser->type_definition_forbidden_message = saved_message;
31376 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31377 parser->colon_doesnt_start_class_def_p
31378 = saved_colon_doesnt_start_class_def_p;
31380 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31381 || types.is_empty ())
31383 fail:
31384 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31385 goto done;
31388 first_token = cp_lexer_peek_token (parser->lexer);
31389 cp = NULL;
31390 errs = errorcount;
31391 FOR_EACH_VEC_ELT (types, i, type)
31393 tree fntype
31394 = build_function_type_list (void_type_node,
31395 cp_build_reference_type (type, false),
31396 NULL_TREE);
31397 tree this_reduc_id = reduc_id;
31398 if (!dependent_type_p (type))
31399 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31400 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31401 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31402 DECL_ARTIFICIAL (fndecl) = 1;
31403 DECL_EXTERNAL (fndecl) = 1;
31404 DECL_DECLARED_INLINE_P (fndecl) = 1;
31405 DECL_IGNORED_P (fndecl) = 1;
31406 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31407 DECL_ATTRIBUTES (fndecl)
31408 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31409 DECL_ATTRIBUTES (fndecl));
31410 if (processing_template_decl)
31411 fndecl = push_template_decl (fndecl);
31412 bool block_scope = false;
31413 tree block = NULL_TREE;
31414 if (current_function_decl)
31416 block_scope = true;
31417 DECL_CONTEXT (fndecl) = global_namespace;
31418 if (!processing_template_decl)
31419 pushdecl (fndecl);
31421 else if (current_class_type)
31423 if (cp == NULL)
31425 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31426 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31427 cp_lexer_consume_token (parser->lexer);
31428 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31429 goto fail;
31430 cp = cp_token_cache_new (first_token,
31431 cp_lexer_peek_nth_token (parser->lexer,
31432 2));
31434 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31435 finish_member_declaration (fndecl);
31436 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31437 DECL_PENDING_INLINE_P (fndecl) = 1;
31438 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31439 continue;
31441 else
31443 DECL_CONTEXT (fndecl) = current_namespace;
31444 pushdecl (fndecl);
31446 if (!block_scope)
31447 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31448 else
31449 block = begin_omp_structured_block ();
31450 if (cp)
31452 cp_parser_push_lexer_for_tokens (parser, cp);
31453 parser->lexer->in_pragma = true;
31455 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31457 if (!block_scope)
31458 finish_function (0);
31459 else
31460 DECL_CONTEXT (fndecl) = current_function_decl;
31461 if (cp)
31462 cp_parser_pop_lexer (parser);
31463 goto fail;
31465 if (cp)
31466 cp_parser_pop_lexer (parser);
31467 if (!block_scope)
31468 finish_function (0);
31469 else
31471 DECL_CONTEXT (fndecl) = current_function_decl;
31472 block = finish_omp_structured_block (block);
31473 if (TREE_CODE (block) == BIND_EXPR)
31474 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31475 else if (TREE_CODE (block) == STATEMENT_LIST)
31476 DECL_SAVED_TREE (fndecl) = block;
31477 if (processing_template_decl)
31478 add_decl_expr (fndecl);
31480 cp_check_omp_declare_reduction (fndecl);
31481 if (cp == NULL && types.length () > 1)
31482 cp = cp_token_cache_new (first_token,
31483 cp_lexer_peek_nth_token (parser->lexer, 2));
31484 if (errs != errorcount)
31485 break;
31488 cp_parser_require_pragma_eol (parser, pragma_tok);
31490 done:
31491 /* Free any declarators allocated. */
31492 obstack_free (&declarator_obstack, p);
31495 /* OpenMP 4.0
31496 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31497 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31498 initializer-clause[opt] new-line
31499 #pragma omp declare target new-line */
31501 static void
31502 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31503 enum pragma_context context)
31505 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31507 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31508 const char *p = IDENTIFIER_POINTER (id);
31510 if (strcmp (p, "simd") == 0)
31512 cp_lexer_consume_token (parser->lexer);
31513 cp_parser_omp_declare_simd (parser, pragma_tok,
31514 context);
31515 return;
31517 cp_ensure_no_omp_declare_simd (parser);
31518 if (strcmp (p, "reduction") == 0)
31520 cp_lexer_consume_token (parser->lexer);
31521 cp_parser_omp_declare_reduction (parser, pragma_tok,
31522 context);
31523 return;
31525 if (!flag_openmp) /* flag_openmp_simd */
31527 cp_parser_require_pragma_eol (parser, pragma_tok);
31528 return;
31530 if (strcmp (p, "target") == 0)
31532 cp_lexer_consume_token (parser->lexer);
31533 cp_parser_omp_declare_target (parser, pragma_tok);
31534 return;
31537 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31538 "or %<target%>");
31539 cp_parser_require_pragma_eol (parser, pragma_tok);
31542 /* Main entry point to OpenMP statement pragmas. */
31544 static void
31545 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31547 tree stmt;
31548 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31549 omp_clause_mask mask (0);
31551 switch (pragma_tok->pragma_kind)
31553 case PRAGMA_OMP_ATOMIC:
31554 cp_parser_omp_atomic (parser, pragma_tok);
31555 return;
31556 case PRAGMA_OMP_CRITICAL:
31557 stmt = cp_parser_omp_critical (parser, pragma_tok);
31558 break;
31559 case PRAGMA_OMP_DISTRIBUTE:
31560 strcpy (p_name, "#pragma omp");
31561 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31562 break;
31563 case PRAGMA_OMP_FOR:
31564 strcpy (p_name, "#pragma omp");
31565 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31566 break;
31567 case PRAGMA_OMP_MASTER:
31568 stmt = cp_parser_omp_master (parser, pragma_tok);
31569 break;
31570 case PRAGMA_OMP_ORDERED:
31571 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31572 break;
31573 case PRAGMA_OMP_PARALLEL:
31574 strcpy (p_name, "#pragma omp");
31575 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31576 break;
31577 case PRAGMA_OMP_SECTIONS:
31578 strcpy (p_name, "#pragma omp");
31579 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31580 break;
31581 case PRAGMA_OMP_SIMD:
31582 strcpy (p_name, "#pragma omp");
31583 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31584 break;
31585 case PRAGMA_OMP_SINGLE:
31586 stmt = cp_parser_omp_single (parser, pragma_tok);
31587 break;
31588 case PRAGMA_OMP_TASK:
31589 stmt = cp_parser_omp_task (parser, pragma_tok);
31590 break;
31591 case PRAGMA_OMP_TASKGROUP:
31592 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31593 break;
31594 case PRAGMA_OMP_TEAMS:
31595 strcpy (p_name, "#pragma omp");
31596 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31597 break;
31598 default:
31599 gcc_unreachable ();
31602 if (stmt)
31603 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31606 /* Transactional Memory parsing routines. */
31608 /* Parse a transaction attribute.
31610 txn-attribute:
31611 attribute
31612 [ [ identifier ] ]
31614 ??? Simplify this when C++0x bracket attributes are
31615 implemented properly. */
31617 static tree
31618 cp_parser_txn_attribute_opt (cp_parser *parser)
31620 cp_token *token;
31621 tree attr_name, attr = NULL;
31623 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31624 return cp_parser_attributes_opt (parser);
31626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31627 return NULL_TREE;
31628 cp_lexer_consume_token (parser->lexer);
31629 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31630 goto error1;
31632 token = cp_lexer_peek_token (parser->lexer);
31633 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31635 token = cp_lexer_consume_token (parser->lexer);
31637 attr_name = (token->type == CPP_KEYWORD
31638 /* For keywords, use the canonical spelling,
31639 not the parsed identifier. */
31640 ? ridpointers[(int) token->keyword]
31641 : token->u.value);
31642 attr = build_tree_list (attr_name, NULL_TREE);
31644 else
31645 cp_parser_error (parser, "expected identifier");
31647 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31648 error1:
31649 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31650 return attr;
31653 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31655 transaction-statement:
31656 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31657 compound-statement
31658 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31661 static tree
31662 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31664 unsigned char old_in = parser->in_transaction;
31665 unsigned char this_in = 1, new_in;
31666 cp_token *token;
31667 tree stmt, attrs, noex;
31669 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31670 || keyword == RID_TRANSACTION_RELAXED);
31671 token = cp_parser_require_keyword (parser, keyword,
31672 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31673 : RT_TRANSACTION_RELAXED));
31674 gcc_assert (token != NULL);
31676 if (keyword == RID_TRANSACTION_RELAXED)
31677 this_in |= TM_STMT_ATTR_RELAXED;
31678 else
31680 attrs = cp_parser_txn_attribute_opt (parser);
31681 if (attrs)
31682 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31685 /* Parse a noexcept specification. */
31686 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31688 /* Keep track if we're in the lexical scope of an outer transaction. */
31689 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31691 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31693 parser->in_transaction = new_in;
31694 cp_parser_compound_statement (parser, NULL, false, false);
31695 parser->in_transaction = old_in;
31697 finish_transaction_stmt (stmt, NULL, this_in, noex);
31699 return stmt;
31702 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31704 transaction-expression:
31705 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31706 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31709 static tree
31710 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31712 unsigned char old_in = parser->in_transaction;
31713 unsigned char this_in = 1;
31714 cp_token *token;
31715 tree expr, noex;
31716 bool noex_expr;
31718 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31719 || keyword == RID_TRANSACTION_RELAXED);
31721 if (!flag_tm)
31722 error (keyword == RID_TRANSACTION_RELAXED
31723 ? G_("%<__transaction_relaxed%> without transactional memory "
31724 "support enabled")
31725 : G_("%<__transaction_atomic%> without transactional memory "
31726 "support enabled"));
31728 token = cp_parser_require_keyword (parser, keyword,
31729 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31730 : RT_TRANSACTION_RELAXED));
31731 gcc_assert (token != NULL);
31733 if (keyword == RID_TRANSACTION_RELAXED)
31734 this_in |= TM_STMT_ATTR_RELAXED;
31736 /* Set this early. This might mean that we allow transaction_cancel in
31737 an expression that we find out later actually has to be a constexpr.
31738 However, we expect that cxx_constant_value will be able to deal with
31739 this; also, if the noexcept has no constexpr, then what we parse next
31740 really is a transaction's body. */
31741 parser->in_transaction = this_in;
31743 /* Parse a noexcept specification. */
31744 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31745 true);
31747 if (!noex || !noex_expr
31748 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31750 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31752 expr = cp_parser_expression (parser);
31753 expr = finish_parenthesized_expr (expr);
31755 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31757 else
31759 /* The only expression that is available got parsed for the noexcept
31760 already. noexcept is true then. */
31761 expr = noex;
31762 noex = boolean_true_node;
31765 expr = build_transaction_expr (token->location, expr, this_in, noex);
31766 parser->in_transaction = old_in;
31768 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31769 return error_mark_node;
31771 return (flag_tm ? expr : error_mark_node);
31774 /* Parse a function-transaction-block.
31776 function-transaction-block:
31777 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31778 function-body
31779 __transaction_atomic txn-attribute[opt] function-try-block
31780 __transaction_relaxed ctor-initializer[opt] function-body
31781 __transaction_relaxed function-try-block
31784 static bool
31785 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31787 unsigned char old_in = parser->in_transaction;
31788 unsigned char new_in = 1;
31789 tree compound_stmt, stmt, attrs;
31790 bool ctor_initializer_p;
31791 cp_token *token;
31793 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31794 || keyword == RID_TRANSACTION_RELAXED);
31795 token = cp_parser_require_keyword (parser, keyword,
31796 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31797 : RT_TRANSACTION_RELAXED));
31798 gcc_assert (token != NULL);
31800 if (keyword == RID_TRANSACTION_RELAXED)
31801 new_in |= TM_STMT_ATTR_RELAXED;
31802 else
31804 attrs = cp_parser_txn_attribute_opt (parser);
31805 if (attrs)
31806 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31809 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31811 parser->in_transaction = new_in;
31813 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31814 ctor_initializer_p = cp_parser_function_try_block (parser);
31815 else
31816 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31817 (parser, /*in_function_try_block=*/false);
31819 parser->in_transaction = old_in;
31821 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31823 return ctor_initializer_p;
31826 /* Parse a __transaction_cancel statement.
31828 cancel-statement:
31829 __transaction_cancel txn-attribute[opt] ;
31830 __transaction_cancel txn-attribute[opt] throw-expression ;
31832 ??? Cancel and throw is not yet implemented. */
31834 static tree
31835 cp_parser_transaction_cancel (cp_parser *parser)
31837 cp_token *token;
31838 bool is_outer = false;
31839 tree stmt, attrs;
31841 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31842 RT_TRANSACTION_CANCEL);
31843 gcc_assert (token != NULL);
31845 attrs = cp_parser_txn_attribute_opt (parser);
31846 if (attrs)
31847 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31849 /* ??? Parse cancel-and-throw here. */
31851 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31853 if (!flag_tm)
31855 error_at (token->location, "%<__transaction_cancel%> without "
31856 "transactional memory support enabled");
31857 return error_mark_node;
31859 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31861 error_at (token->location, "%<__transaction_cancel%> within a "
31862 "%<__transaction_relaxed%>");
31863 return error_mark_node;
31865 else if (is_outer)
31867 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31868 && !is_tm_may_cancel_outer (current_function_decl))
31870 error_at (token->location, "outer %<__transaction_cancel%> not "
31871 "within outer %<__transaction_atomic%>");
31872 error_at (token->location,
31873 " or a %<transaction_may_cancel_outer%> function");
31874 return error_mark_node;
31877 else if (parser->in_transaction == 0)
31879 error_at (token->location, "%<__transaction_cancel%> not within "
31880 "%<__transaction_atomic%>");
31881 return error_mark_node;
31884 stmt = build_tm_abort_call (token->location, is_outer);
31885 add_stmt (stmt);
31887 return stmt;
31890 /* The parser. */
31892 static GTY (()) cp_parser *the_parser;
31895 /* Special handling for the first token or line in the file. The first
31896 thing in the file might be #pragma GCC pch_preprocess, which loads a
31897 PCH file, which is a GC collection point. So we need to handle this
31898 first pragma without benefit of an existing lexer structure.
31900 Always returns one token to the caller in *FIRST_TOKEN. This is
31901 either the true first token of the file, or the first token after
31902 the initial pragma. */
31904 static void
31905 cp_parser_initial_pragma (cp_token *first_token)
31907 tree name = NULL;
31909 cp_lexer_get_preprocessor_token (NULL, first_token);
31910 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31911 return;
31913 cp_lexer_get_preprocessor_token (NULL, first_token);
31914 if (first_token->type == CPP_STRING)
31916 name = first_token->u.value;
31918 cp_lexer_get_preprocessor_token (NULL, first_token);
31919 if (first_token->type != CPP_PRAGMA_EOL)
31920 error_at (first_token->location,
31921 "junk at end of %<#pragma GCC pch_preprocess%>");
31923 else
31924 error_at (first_token->location, "expected string literal");
31926 /* Skip to the end of the pragma. */
31927 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31928 cp_lexer_get_preprocessor_token (NULL, first_token);
31930 /* Now actually load the PCH file. */
31931 if (name)
31932 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31934 /* Read one more token to return to our caller. We have to do this
31935 after reading the PCH file in, since its pointers have to be
31936 live. */
31937 cp_lexer_get_preprocessor_token (NULL, first_token);
31940 /* Parses the grainsize pragma for the _Cilk_for statement.
31941 Syntax:
31942 #pragma cilk grainsize = <VALUE>. */
31944 static void
31945 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
31947 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
31949 tree exp = cp_parser_binary_expression (parser, false, false,
31950 PREC_NOT_OPERATOR, NULL);
31951 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31952 if (!exp || exp == error_mark_node)
31954 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
31955 return;
31958 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
31959 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
31960 cp_parser_cilk_for (parser, exp);
31961 else
31962 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
31963 "%<#pragma cilk grainsize%> is not followed by "
31964 "%<_Cilk_for%>");
31965 return;
31967 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31970 /* Normal parsing of a pragma token. Here we can (and must) use the
31971 regular lexer. */
31973 static bool
31974 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31976 cp_token *pragma_tok;
31977 unsigned int id;
31979 pragma_tok = cp_lexer_consume_token (parser->lexer);
31980 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31981 parser->lexer->in_pragma = true;
31983 id = pragma_tok->pragma_kind;
31984 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31985 cp_ensure_no_omp_declare_simd (parser);
31986 switch (id)
31988 case PRAGMA_GCC_PCH_PREPROCESS:
31989 error_at (pragma_tok->location,
31990 "%<#pragma GCC pch_preprocess%> must be first");
31991 break;
31993 case PRAGMA_OMP_BARRIER:
31994 switch (context)
31996 case pragma_compound:
31997 cp_parser_omp_barrier (parser, pragma_tok);
31998 return false;
31999 case pragma_stmt:
32000 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32001 "used in compound statements");
32002 break;
32003 default:
32004 goto bad_stmt;
32006 break;
32008 case PRAGMA_OMP_FLUSH:
32009 switch (context)
32011 case pragma_compound:
32012 cp_parser_omp_flush (parser, pragma_tok);
32013 return false;
32014 case pragma_stmt:
32015 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32016 "used in compound statements");
32017 break;
32018 default:
32019 goto bad_stmt;
32021 break;
32023 case PRAGMA_OMP_TASKWAIT:
32024 switch (context)
32026 case pragma_compound:
32027 cp_parser_omp_taskwait (parser, pragma_tok);
32028 return false;
32029 case pragma_stmt:
32030 error_at (pragma_tok->location,
32031 "%<#pragma omp taskwait%> may only be "
32032 "used in compound statements");
32033 break;
32034 default:
32035 goto bad_stmt;
32037 break;
32039 case PRAGMA_OMP_TASKYIELD:
32040 switch (context)
32042 case pragma_compound:
32043 cp_parser_omp_taskyield (parser, pragma_tok);
32044 return false;
32045 case pragma_stmt:
32046 error_at (pragma_tok->location,
32047 "%<#pragma omp taskyield%> may only be "
32048 "used in compound statements");
32049 break;
32050 default:
32051 goto bad_stmt;
32053 break;
32055 case PRAGMA_OMP_CANCEL:
32056 switch (context)
32058 case pragma_compound:
32059 cp_parser_omp_cancel (parser, pragma_tok);
32060 return false;
32061 case pragma_stmt:
32062 error_at (pragma_tok->location,
32063 "%<#pragma omp cancel%> may only be "
32064 "used in compound statements");
32065 break;
32066 default:
32067 goto bad_stmt;
32069 break;
32071 case PRAGMA_OMP_CANCELLATION_POINT:
32072 switch (context)
32074 case pragma_compound:
32075 cp_parser_omp_cancellation_point (parser, pragma_tok);
32076 return false;
32077 case pragma_stmt:
32078 error_at (pragma_tok->location,
32079 "%<#pragma omp cancellation point%> may only be "
32080 "used in compound statements");
32081 break;
32082 default:
32083 goto bad_stmt;
32085 break;
32087 case PRAGMA_OMP_THREADPRIVATE:
32088 cp_parser_omp_threadprivate (parser, pragma_tok);
32089 return false;
32091 case PRAGMA_OMP_DECLARE_REDUCTION:
32092 cp_parser_omp_declare (parser, pragma_tok, context);
32093 return false;
32095 case PRAGMA_OMP_ATOMIC:
32096 case PRAGMA_OMP_CRITICAL:
32097 case PRAGMA_OMP_DISTRIBUTE:
32098 case PRAGMA_OMP_FOR:
32099 case PRAGMA_OMP_MASTER:
32100 case PRAGMA_OMP_ORDERED:
32101 case PRAGMA_OMP_PARALLEL:
32102 case PRAGMA_OMP_SECTIONS:
32103 case PRAGMA_OMP_SIMD:
32104 case PRAGMA_OMP_SINGLE:
32105 case PRAGMA_OMP_TASK:
32106 case PRAGMA_OMP_TASKGROUP:
32107 case PRAGMA_OMP_TEAMS:
32108 if (context != pragma_stmt && context != pragma_compound)
32109 goto bad_stmt;
32110 cp_parser_omp_construct (parser, pragma_tok);
32111 return true;
32113 case PRAGMA_OMP_TARGET:
32114 return cp_parser_omp_target (parser, pragma_tok, context);
32116 case PRAGMA_OMP_END_DECLARE_TARGET:
32117 cp_parser_omp_end_declare_target (parser, pragma_tok);
32118 return false;
32120 case PRAGMA_OMP_SECTION:
32121 error_at (pragma_tok->location,
32122 "%<#pragma omp section%> may only be used in "
32123 "%<#pragma omp sections%> construct");
32124 break;
32126 case PRAGMA_IVDEP:
32128 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32129 cp_token *tok;
32130 tok = cp_lexer_peek_token (the_parser->lexer);
32131 if (tok->type != CPP_KEYWORD
32132 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
32133 && tok->keyword != RID_DO))
32135 cp_parser_error (parser, "for, while or do statement expected");
32136 return false;
32138 cp_parser_iteration_statement (parser, true);
32139 return true;
32142 case PRAGMA_CILK_SIMD:
32143 if (context == pragma_external)
32145 error_at (pragma_tok->location,
32146 "%<#pragma simd%> must be inside a function");
32147 break;
32149 cp_parser_cilk_simd (parser, pragma_tok);
32150 return true;
32152 case PRAGMA_CILK_GRAINSIZE:
32153 if (context == pragma_external)
32155 error_at (pragma_tok->location,
32156 "%<#pragma cilk grainsize%> must be inside a function");
32157 break;
32160 /* Ignore the pragma if Cilk Plus is not enabled. */
32161 if (flag_cilkplus)
32163 cp_parser_cilk_grainsize (parser, pragma_tok);
32164 return true;
32166 else
32168 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
32169 "%<#pragma cilk grainsize%>");
32170 break;
32173 default:
32174 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
32175 c_invoke_pragma_handler (id);
32176 break;
32178 bad_stmt:
32179 cp_parser_error (parser, "expected declaration specifiers");
32180 break;
32183 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32184 return false;
32187 /* The interface the pragma parsers have to the lexer. */
32189 enum cpp_ttype
32190 pragma_lex (tree *value)
32192 cp_token *tok;
32193 enum cpp_ttype ret;
32195 tok = cp_lexer_peek_token (the_parser->lexer);
32197 ret = tok->type;
32198 *value = tok->u.value;
32200 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
32201 ret = CPP_EOF;
32202 else if (ret == CPP_STRING)
32203 *value = cp_parser_string_literal (the_parser, false, false);
32204 else
32206 cp_lexer_consume_token (the_parser->lexer);
32207 if (ret == CPP_KEYWORD)
32208 ret = CPP_NAME;
32211 return ret;
32215 /* External interface. */
32217 /* Parse one entire translation unit. */
32219 void
32220 c_parse_file (void)
32222 static bool already_called = false;
32224 if (already_called)
32225 fatal_error ("inter-module optimizations not implemented for C++");
32226 already_called = true;
32228 the_parser = cp_parser_new ();
32229 push_deferring_access_checks (flag_access_control
32230 ? dk_no_deferred : dk_no_check);
32231 cp_parser_translation_unit (the_parser);
32232 the_parser = NULL;
32235 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
32236 vectorlength clause:
32237 Syntax:
32238 vectorlength ( constant-expression ) */
32240 static tree
32241 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
32242 bool is_simd_fn)
32244 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32245 tree expr;
32246 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
32247 safelen clause. Thus, vectorlength is represented as OMP 4.0
32248 safelen. For SIMD-enabled function it is represented by OMP 4.0
32249 simdlen. */
32250 if (!is_simd_fn)
32251 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
32252 loc);
32253 else
32254 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
32255 loc);
32257 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32258 return error_mark_node;
32260 expr = cp_parser_constant_expression (parser);
32261 expr = maybe_constant_value (expr);
32263 /* If expr == error_mark_node, then don't emit any errors nor
32264 create a clause. if any of the above functions returns
32265 error mark node then they would have emitted an error message. */
32266 if (expr == error_mark_node)
32268 else if (!TREE_TYPE (expr)
32269 || !TREE_CONSTANT (expr)
32270 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
32271 error_at (loc, "vectorlength must be an integer constant");
32272 else if (TREE_CONSTANT (expr)
32273 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
32274 error_at (loc, "vectorlength must be a power of 2");
32275 else
32277 tree c;
32278 if (!is_simd_fn)
32280 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
32281 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
32282 OMP_CLAUSE_CHAIN (c) = clauses;
32283 clauses = c;
32285 else
32287 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
32288 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
32289 OMP_CLAUSE_CHAIN (c) = clauses;
32290 clauses = c;
32294 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32295 return error_mark_node;
32296 return clauses;
32299 /* Handles the Cilk Plus #pragma simd linear clause.
32300 Syntax:
32301 linear ( simd-linear-variable-list )
32303 simd-linear-variable-list:
32304 simd-linear-variable
32305 simd-linear-variable-list , simd-linear-variable
32307 simd-linear-variable:
32308 id-expression
32309 id-expression : simd-linear-step
32311 simd-linear-step:
32312 conditional-expression */
32314 static tree
32315 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
32317 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32319 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32320 return clauses;
32321 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32323 cp_parser_error (parser, "expected identifier");
32324 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32325 return error_mark_node;
32328 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32329 parser->colon_corrects_to_scope_p = false;
32330 while (1)
32332 cp_token *token = cp_lexer_peek_token (parser->lexer);
32333 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32335 cp_parser_error (parser, "expected variable-name");
32336 clauses = error_mark_node;
32337 break;
32340 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32341 false, false);
32342 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32343 token->location);
32344 if (decl == error_mark_node)
32346 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32347 token->location);
32348 clauses = error_mark_node;
32350 else
32352 tree e = NULL_TREE;
32353 tree step_size = integer_one_node;
32355 /* If present, parse the linear step. Otherwise, assume the default
32356 value of 1. */
32357 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32359 cp_lexer_consume_token (parser->lexer);
32361 e = cp_parser_assignment_expression (parser);
32362 e = maybe_constant_value (e);
32364 if (e == error_mark_node)
32366 /* If an error has occurred, then the whole pragma is
32367 considered ill-formed. Thus, no reason to keep
32368 parsing. */
32369 clauses = error_mark_node;
32370 break;
32372 else if (type_dependent_expression_p (e)
32373 || value_dependent_expression_p (e)
32374 || (TREE_TYPE (e)
32375 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32376 && (TREE_CONSTANT (e)
32377 || DECL_P (e))))
32378 step_size = e;
32379 else
32380 cp_parser_error (parser,
32381 "step size must be an integer constant "
32382 "expression or an integer variable");
32385 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32386 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32387 OMP_CLAUSE_DECL (l) = decl;
32388 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32389 OMP_CLAUSE_CHAIN (l) = clauses;
32390 clauses = l;
32392 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32393 cp_lexer_consume_token (parser->lexer);
32394 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32395 break;
32396 else
32398 error_at (cp_lexer_peek_token (parser->lexer)->location,
32399 "expected %<,%> or %<)%> after %qE", decl);
32400 clauses = error_mark_node;
32401 break;
32404 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32405 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32406 return clauses;
32409 /* Returns the name of the next clause. If the clause is not
32410 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32411 token is not consumed. Otherwise, the appropriate enum from the
32412 pragma_simd_clause is returned and the token is consumed. */
32414 static pragma_omp_clause
32415 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32417 pragma_omp_clause clause_type;
32418 cp_token *token = cp_lexer_peek_token (parser->lexer);
32420 if (token->keyword == RID_PRIVATE)
32421 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32422 else if (!token->u.value || token->type != CPP_NAME)
32423 return PRAGMA_CILK_CLAUSE_NONE;
32424 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32425 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32426 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32427 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32428 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32429 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32430 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32431 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32432 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32433 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32434 else
32435 return PRAGMA_CILK_CLAUSE_NONE;
32437 cp_lexer_consume_token (parser->lexer);
32438 return clause_type;
32441 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32443 static tree
32444 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32446 tree clauses = NULL_TREE;
32448 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32449 && clauses != error_mark_node)
32451 pragma_omp_clause c_kind;
32452 c_kind = cp_parser_cilk_simd_clause_name (parser);
32453 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32454 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
32455 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32456 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32457 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32458 /* Use the OpenMP 4.0 equivalent function. */
32459 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32460 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32461 /* Use the OpenMP 4.0 equivalent function. */
32462 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32463 clauses);
32464 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32465 /* Use the OMP 4.0 equivalent function. */
32466 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32467 clauses);
32468 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32469 /* Use the OMP 4.0 equivalent function. */
32470 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32471 else
32473 clauses = error_mark_node;
32474 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32475 break;
32479 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32481 if (clauses == error_mark_node)
32482 return error_mark_node;
32483 else
32484 return c_finish_cilk_clauses (clauses);
32487 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32489 static void
32490 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32492 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32494 if (clauses == error_mark_node)
32495 return;
32497 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32499 error_at (cp_lexer_peek_token (parser->lexer)->location,
32500 "for statement expected");
32501 return;
32504 tree sb = begin_omp_structured_block ();
32505 int save = cp_parser_begin_omp_structured_block (parser);
32506 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32507 if (ret)
32508 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32509 cp_parser_end_omp_structured_block (parser, save);
32510 add_stmt (finish_omp_structured_block (sb));
32513 /* Main entry-point for parsing Cilk Plus _Cilk_for
32514 loops. The return value is error_mark_node
32515 when errors happen and CILK_FOR tree on success. */
32517 static tree
32518 cp_parser_cilk_for (cp_parser *parser, tree grain)
32520 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
32521 gcc_unreachable ();
32523 tree sb = begin_omp_structured_block ();
32524 int save = cp_parser_begin_omp_structured_block (parser);
32526 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
32527 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
32528 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
32529 clauses = finish_omp_clauses (clauses);
32531 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
32532 if (ret)
32533 cpp_validate_cilk_plus_loop (ret);
32534 else
32535 ret = error_mark_node;
32537 cp_parser_end_omp_structured_block (parser, save);
32538 add_stmt (finish_omp_structured_block (sb));
32539 return ret;
32542 /* Create an identifier for a generic parameter type (a synthesized
32543 template parameter implied by `auto' or a concept identifier). */
32545 static GTY(()) int generic_parm_count;
32546 static tree
32547 make_generic_type_name ()
32549 char buf[32];
32550 sprintf (buf, "auto:%d", ++generic_parm_count);
32551 return get_identifier (buf);
32554 /* Predicate that behaves as is_auto_or_concept but matches the parent
32555 node of the generic type rather than the generic type itself. This
32556 allows for type transformation in add_implicit_template_parms. */
32558 static inline bool
32559 tree_type_is_auto_or_concept (const_tree t)
32561 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32564 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32565 (creating a new template parameter list if necessary). Returns the newly
32566 created template type parm. */
32568 tree
32569 synthesize_implicit_template_parm (cp_parser *parser)
32571 gcc_assert (current_binding_level->kind == sk_function_parms);
32573 /* We are either continuing a function template that already contains implicit
32574 template parameters, creating a new fully-implicit function template, or
32575 extending an existing explicit function template with implicit template
32576 parameters. */
32578 cp_binding_level *const entry_scope = current_binding_level;
32580 bool become_template = false;
32581 cp_binding_level *parent_scope = 0;
32583 if (parser->implicit_template_scope)
32585 gcc_assert (parser->implicit_template_parms);
32587 current_binding_level = parser->implicit_template_scope;
32589 else
32591 /* Roll back to the existing template parameter scope (in the case of
32592 extending an explicit function template) or introduce a new template
32593 parameter scope ahead of the function parameter scope (or class scope
32594 in the case of out-of-line member definitions). The function scope is
32595 added back after template parameter synthesis below. */
32597 cp_binding_level *scope = entry_scope;
32599 while (scope->kind == sk_function_parms)
32601 parent_scope = scope;
32602 scope = scope->level_chain;
32604 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32606 /* If not defining a class, then any class scope is a scope level in
32607 an out-of-line member definition. In this case simply wind back
32608 beyond the first such scope to inject the template parameter list.
32609 Otherwise wind back to the class being defined. The latter can
32610 occur in class member friend declarations such as:
32612 class A {
32613 void foo (auto);
32615 class B {
32616 friend void A::foo (auto);
32619 The template parameter list synthesized for the friend declaration
32620 must be injected in the scope of 'B'. This can also occur in
32621 erroneous cases such as:
32623 struct A {
32624 struct B {
32625 void foo (auto);
32627 void B::foo (auto) {}
32630 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32631 but, nevertheless, the template parameter list synthesized for the
32632 declarator should be injected into the scope of 'A' as if the
32633 ill-formed template was specified explicitly. */
32635 while (scope->kind == sk_class && !scope->defining_class_p)
32637 parent_scope = scope;
32638 scope = scope->level_chain;
32642 current_binding_level = scope;
32644 if (scope->kind != sk_template_parms
32645 || !function_being_declared_is_template_p (parser))
32647 /* Introduce a new template parameter list for implicit template
32648 parameters. */
32650 become_template = true;
32652 parser->implicit_template_scope
32653 = begin_scope (sk_template_parms, NULL);
32655 ++processing_template_decl;
32657 parser->fully_implicit_function_template_p = true;
32658 ++parser->num_template_parameter_lists;
32660 else
32662 /* Synthesize implicit template parameters at the end of the explicit
32663 template parameter list. */
32665 gcc_assert (current_template_parms);
32667 parser->implicit_template_scope = scope;
32669 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32670 parser->implicit_template_parms
32671 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32675 /* Synthesize a new template parameter and track the current template
32676 parameter chain with implicit_template_parms. */
32678 tree synth_id = make_generic_type_name ();
32679 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32680 synth_id);
32681 tree new_parm
32682 = process_template_parm (parser->implicit_template_parms,
32683 input_location,
32684 build_tree_list (NULL_TREE, synth_tmpl_parm),
32685 /*non_type=*/false,
32686 /*param_pack=*/false);
32689 if (parser->implicit_template_parms)
32690 parser->implicit_template_parms
32691 = TREE_CHAIN (parser->implicit_template_parms);
32692 else
32693 parser->implicit_template_parms = new_parm;
32695 tree new_type = TREE_TYPE (getdecls ());
32697 /* If creating a fully implicit function template, start the new implicit
32698 template parameter list with this synthesized type, otherwise grow the
32699 current template parameter list. */
32701 if (become_template)
32703 parent_scope->level_chain = current_binding_level;
32705 tree new_parms = make_tree_vec (1);
32706 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32707 current_template_parms = tree_cons (size_int (processing_template_decl),
32708 new_parms, current_template_parms);
32710 else
32712 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32713 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32714 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32715 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32718 current_binding_level = entry_scope;
32720 return new_type;
32723 /* Finish the declaration of a fully implicit function template. Such a
32724 template has no explicit template parameter list so has not been through the
32725 normal template head and tail processing. synthesize_implicit_template_parm
32726 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32727 provided if the declaration is a class member such that its template
32728 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32729 form is returned. Otherwise NULL_TREE is returned. */
32731 tree
32732 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32734 gcc_assert (parser->fully_implicit_function_template_p);
32736 if (member_decl_opt && member_decl_opt != error_mark_node
32737 && DECL_VIRTUAL_P (member_decl_opt))
32739 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32740 "implicit templates may not be %<virtual%>");
32741 DECL_VIRTUAL_P (member_decl_opt) = false;
32744 if (member_decl_opt)
32745 member_decl_opt = finish_member_template_decl (member_decl_opt);
32746 end_template_decl ();
32748 parser->fully_implicit_function_template_p = false;
32749 --parser->num_template_parameter_lists;
32751 return member_decl_opt;
32754 #include "gt-cp-parser.h"