/cp
[official-gcc.git] / gcc / cp / parser.c
blobe57a5bd680281800c2e920e87723fd66641c37f8
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "print-tree.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "cp-tree.h"
33 #include "intl.h"
34 #include "c-family/c-pragma.h"
35 #include "decl.h"
36 #include "flags.h"
37 #include "diagnostic-core.h"
38 #include "target.h"
39 #include "hash-map.h"
40 #include "is-a.h"
41 #include "plugin-api.h"
42 #include "vec.h"
43 #include "hashtab.h"
44 #include "hash-set.h"
45 #include "machmode.h"
46 #include "hard-reg-set.h"
47 #include "input.h"
48 #include "function.h"
49 #include "ipa-ref.h"
50 #include "cgraph.h"
51 #include "c-family/c-common.h"
52 #include "c-family/c-objc.h"
53 #include "plugin.h"
54 #include "tree-pretty-print.h"
55 #include "parser.h"
56 #include "type-utils.h"
57 #include "omp-low.h"
60 /* The lexer. */
62 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
63 and c-lex.c) and the C++ parser. */
65 static cp_token eof_token =
67 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
70 /* The various kinds of non integral constant we encounter. */
71 typedef enum non_integral_constant {
72 NIC_NONE,
73 /* floating-point literal */
74 NIC_FLOAT,
75 /* %<this%> */
76 NIC_THIS,
77 /* %<__FUNCTION__%> */
78 NIC_FUNC_NAME,
79 /* %<__PRETTY_FUNCTION__%> */
80 NIC_PRETTY_FUNC,
81 /* %<__func__%> */
82 NIC_C99_FUNC,
83 /* "%<va_arg%> */
84 NIC_VA_ARG,
85 /* a cast */
86 NIC_CAST,
87 /* %<typeid%> operator */
88 NIC_TYPEID,
89 /* non-constant compound literals */
90 NIC_NCC,
91 /* a function call */
92 NIC_FUNC_CALL,
93 /* an increment */
94 NIC_INC,
95 /* an decrement */
96 NIC_DEC,
97 /* an array reference */
98 NIC_ARRAY_REF,
99 /* %<->%> */
100 NIC_ARROW,
101 /* %<.%> */
102 NIC_POINT,
103 /* the address of a label */
104 NIC_ADDR_LABEL,
105 /* %<*%> */
106 NIC_STAR,
107 /* %<&%> */
108 NIC_ADDR,
109 /* %<++%> */
110 NIC_PREINCREMENT,
111 /* %<--%> */
112 NIC_PREDECREMENT,
113 /* %<new%> */
114 NIC_NEW,
115 /* %<delete%> */
116 NIC_DEL,
117 /* calls to overloaded operators */
118 NIC_OVERLOADED,
119 /* an assignment */
120 NIC_ASSIGNMENT,
121 /* a comma operator */
122 NIC_COMMA,
123 /* a call to a constructor */
124 NIC_CONSTRUCTOR,
125 /* a transaction expression */
126 NIC_TRANSACTION
127 } non_integral_constant;
129 /* The various kinds of errors about name-lookup failing. */
130 typedef enum name_lookup_error {
131 /* NULL */
132 NLE_NULL,
133 /* is not a type */
134 NLE_TYPE,
135 /* is not a class or namespace */
136 NLE_CXX98,
137 /* is not a class, namespace, or enumeration */
138 NLE_NOT_CXX98
139 } name_lookup_error;
141 /* The various kinds of required token */
142 typedef enum required_token {
143 RT_NONE,
144 RT_SEMICOLON, /* ';' */
145 RT_OPEN_PAREN, /* '(' */
146 RT_CLOSE_BRACE, /* '}' */
147 RT_OPEN_BRACE, /* '{' */
148 RT_CLOSE_SQUARE, /* ']' */
149 RT_OPEN_SQUARE, /* '[' */
150 RT_COMMA, /* ',' */
151 RT_SCOPE, /* '::' */
152 RT_LESS, /* '<' */
153 RT_GREATER, /* '>' */
154 RT_EQ, /* '=' */
155 RT_ELLIPSIS, /* '...' */
156 RT_MULT, /* '*' */
157 RT_COMPL, /* '~' */
158 RT_COLON, /* ':' */
159 RT_COLON_SCOPE, /* ':' or '::' */
160 RT_CLOSE_PAREN, /* ')' */
161 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
162 RT_PRAGMA_EOL, /* end of line */
163 RT_NAME, /* identifier */
165 /* The type is CPP_KEYWORD */
166 RT_NEW, /* new */
167 RT_DELETE, /* delete */
168 RT_RETURN, /* return */
169 RT_WHILE, /* while */
170 RT_EXTERN, /* extern */
171 RT_STATIC_ASSERT, /* static_assert */
172 RT_DECLTYPE, /* decltype */
173 RT_OPERATOR, /* operator */
174 RT_CLASS, /* class */
175 RT_TEMPLATE, /* template */
176 RT_NAMESPACE, /* namespace */
177 RT_USING, /* using */
178 RT_ASM, /* asm */
179 RT_TRY, /* try */
180 RT_CATCH, /* catch */
181 RT_THROW, /* throw */
182 RT_LABEL, /* __label__ */
183 RT_AT_TRY, /* @try */
184 RT_AT_SYNCHRONIZED, /* @synchronized */
185 RT_AT_THROW, /* @throw */
187 RT_SELECT, /* selection-statement */
188 RT_INTERATION, /* iteration-statement */
189 RT_JUMP, /* jump-statement */
190 RT_CLASS_KEY, /* class-key */
191 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
192 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
193 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
194 RT_TRANSACTION_CANCEL /* __transaction_cancel */
195 } required_token;
197 /* Prototypes. */
199 static cp_lexer *cp_lexer_new_main
200 (void);
201 static cp_lexer *cp_lexer_new_from_tokens
202 (cp_token_cache *tokens);
203 static void cp_lexer_destroy
204 (cp_lexer *);
205 static int cp_lexer_saving_tokens
206 (const cp_lexer *);
207 static cp_token *cp_lexer_token_at
208 (cp_lexer *, cp_token_position);
209 static void cp_lexer_get_preprocessor_token
210 (cp_lexer *, cp_token *);
211 static inline cp_token *cp_lexer_peek_token
212 (cp_lexer *);
213 static cp_token *cp_lexer_peek_nth_token
214 (cp_lexer *, size_t);
215 static inline bool cp_lexer_next_token_is
216 (cp_lexer *, enum cpp_ttype);
217 static bool cp_lexer_next_token_is_not
218 (cp_lexer *, enum cpp_ttype);
219 static bool cp_lexer_next_token_is_keyword
220 (cp_lexer *, enum rid);
221 static cp_token *cp_lexer_consume_token
222 (cp_lexer *);
223 static void cp_lexer_purge_token
224 (cp_lexer *);
225 static void cp_lexer_purge_tokens_after
226 (cp_lexer *, cp_token_position);
227 static void cp_lexer_save_tokens
228 (cp_lexer *);
229 static void cp_lexer_commit_tokens
230 (cp_lexer *);
231 static void cp_lexer_rollback_tokens
232 (cp_lexer *);
233 static void cp_lexer_print_token
234 (FILE *, cp_token *);
235 static inline bool cp_lexer_debugging_p
236 (cp_lexer *);
237 static void cp_lexer_start_debugging
238 (cp_lexer *) ATTRIBUTE_UNUSED;
239 static void cp_lexer_stop_debugging
240 (cp_lexer *) ATTRIBUTE_UNUSED;
242 static cp_token_cache *cp_token_cache_new
243 (cp_token *, cp_token *);
245 static void cp_parser_initial_pragma
246 (cp_token *);
248 static tree cp_literal_operator_id
249 (const char *);
251 static void cp_parser_cilk_simd
252 (cp_parser *, cp_token *);
253 static tree cp_parser_cilk_for
254 (cp_parser *, tree);
255 static bool cp_parser_omp_declare_reduction_exprs
256 (tree, cp_parser *);
257 static tree cp_parser_cilk_simd_vectorlength
258 (cp_parser *, tree, bool);
260 /* Manifest constants. */
261 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
262 #define CP_SAVED_TOKEN_STACK 5
264 /* Variables. */
266 /* The stream to which debugging output should be written. */
267 static FILE *cp_lexer_debug_stream;
269 /* Nonzero if we are parsing an unevaluated operand: an operand to
270 sizeof, typeof, or alignof. */
271 int cp_unevaluated_operand;
273 /* Dump up to NUM tokens in BUFFER to FILE starting with token
274 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
275 first token in BUFFER. If NUM is 0, dump all the tokens. If
276 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
277 highlighted by surrounding it in [[ ]]. */
279 static void
280 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
281 cp_token *start_token, unsigned num,
282 cp_token *curr_token)
284 unsigned i, nprinted;
285 cp_token *token;
286 bool do_print;
288 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
290 if (buffer == NULL)
291 return;
293 if (num == 0)
294 num = buffer->length ();
296 if (start_token == NULL)
297 start_token = buffer->address ();
299 if (start_token > buffer->address ())
301 cp_lexer_print_token (file, &(*buffer)[0]);
302 fprintf (file, " ... ");
305 do_print = false;
306 nprinted = 0;
307 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
309 if (token == start_token)
310 do_print = true;
312 if (!do_print)
313 continue;
315 nprinted++;
316 if (token == curr_token)
317 fprintf (file, "[[");
319 cp_lexer_print_token (file, token);
321 if (token == curr_token)
322 fprintf (file, "]]");
324 switch (token->type)
326 case CPP_SEMICOLON:
327 case CPP_OPEN_BRACE:
328 case CPP_CLOSE_BRACE:
329 case CPP_EOF:
330 fputc ('\n', file);
331 break;
333 default:
334 fputc (' ', file);
338 if (i == num && i < buffer->length ())
340 fprintf (file, " ... ");
341 cp_lexer_print_token (file, &buffer->last ());
344 fprintf (file, "\n");
348 /* Dump all tokens in BUFFER to stderr. */
350 void
351 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
353 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
356 DEBUG_FUNCTION void
357 debug (vec<cp_token, va_gc> &ref)
359 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
362 DEBUG_FUNCTION void
363 debug (vec<cp_token, va_gc> *ptr)
365 if (ptr)
366 debug (*ptr);
367 else
368 fprintf (stderr, "<nil>\n");
372 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
373 description for T. */
375 static void
376 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
378 if (t)
380 fprintf (file, "%s: ", desc);
381 print_node_brief (file, "", t, 0);
386 /* Dump parser context C to FILE. */
388 static void
389 cp_debug_print_context (FILE *file, cp_parser_context *c)
391 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
392 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
393 print_node_brief (file, "", c->object_type, 0);
394 fprintf (file, "}\n");
398 /* Print the stack of parsing contexts to FILE starting with FIRST. */
400 static void
401 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
403 unsigned i;
404 cp_parser_context *c;
406 fprintf (file, "Parsing context stack:\n");
407 for (i = 0, c = first; c; c = c->next, i++)
409 fprintf (file, "\t#%u: ", i);
410 cp_debug_print_context (file, c);
415 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
417 static void
418 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
420 if (flag)
421 fprintf (file, "%s: true\n", desc);
425 /* Print an unparsed function entry UF to FILE. */
427 static void
428 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
430 unsigned i;
431 cp_default_arg_entry *default_arg_fn;
432 tree fn;
434 fprintf (file, "\tFunctions with default args:\n");
435 for (i = 0;
436 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
437 i++)
439 fprintf (file, "\t\tClass type: ");
440 print_node_brief (file, "", default_arg_fn->class_type, 0);
441 fprintf (file, "\t\tDeclaration: ");
442 print_node_brief (file, "", default_arg_fn->decl, 0);
443 fprintf (file, "\n");
446 fprintf (file, "\n\tFunctions with definitions that require "
447 "post-processing\n\t\t");
448 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
450 print_node_brief (file, "", fn, 0);
451 fprintf (file, " ");
453 fprintf (file, "\n");
455 fprintf (file, "\n\tNon-static data members with initializers that require "
456 "post-processing\n\t\t");
457 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
459 print_node_brief (file, "", fn, 0);
460 fprintf (file, " ");
462 fprintf (file, "\n");
466 /* Print the stack of unparsed member functions S to FILE. */
468 static void
469 cp_debug_print_unparsed_queues (FILE *file,
470 vec<cp_unparsed_functions_entry, va_gc> *s)
472 unsigned i;
473 cp_unparsed_functions_entry *uf;
475 fprintf (file, "Unparsed functions\n");
476 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
478 fprintf (file, "#%u:\n", i);
479 cp_debug_print_unparsed_function (file, uf);
484 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
485 the given PARSER. If FILE is NULL, the output is printed on stderr. */
487 static void
488 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
490 cp_token *next_token, *first_token, *start_token;
492 if (file == NULL)
493 file = stderr;
495 next_token = parser->lexer->next_token;
496 first_token = parser->lexer->buffer->address ();
497 start_token = (next_token > first_token + window_size / 2)
498 ? next_token - window_size / 2
499 : first_token;
500 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
501 next_token);
505 /* Dump debugging information for the given PARSER. If FILE is NULL,
506 the output is printed on stderr. */
508 void
509 cp_debug_parser (FILE *file, cp_parser *parser)
511 const size_t window_size = 20;
512 cp_token *token;
513 expanded_location eloc;
515 if (file == NULL)
516 file = stderr;
518 fprintf (file, "Parser state\n\n");
519 fprintf (file, "Number of tokens: %u\n",
520 vec_safe_length (parser->lexer->buffer));
521 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
522 cp_debug_print_tree_if_set (file, "Object scope",
523 parser->object_scope);
524 cp_debug_print_tree_if_set (file, "Qualifying scope",
525 parser->qualifying_scope);
526 cp_debug_print_context_stack (file, parser->context);
527 cp_debug_print_flag (file, "Allow GNU extensions",
528 parser->allow_gnu_extensions_p);
529 cp_debug_print_flag (file, "'>' token is greater-than",
530 parser->greater_than_is_operator_p);
531 cp_debug_print_flag (file, "Default args allowed in current "
532 "parameter list", parser->default_arg_ok_p);
533 cp_debug_print_flag (file, "Parsing integral constant-expression",
534 parser->integral_constant_expression_p);
535 cp_debug_print_flag (file, "Allow non-constant expression in current "
536 "constant-expression",
537 parser->allow_non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Seen non-constant expression",
539 parser->non_integral_constant_expression_p);
540 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
541 "current context",
542 parser->local_variables_forbidden_p);
543 cp_debug_print_flag (file, "In unbraced linkage specification",
544 parser->in_unbraced_linkage_specification_p);
545 cp_debug_print_flag (file, "Parsing a declarator",
546 parser->in_declarator_p);
547 cp_debug_print_flag (file, "In template argument list",
548 parser->in_template_argument_list_p);
549 cp_debug_print_flag (file, "Parsing an iteration statement",
550 parser->in_statement & IN_ITERATION_STMT);
551 cp_debug_print_flag (file, "Parsing a switch statement",
552 parser->in_statement & IN_SWITCH_STMT);
553 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
554 parser->in_statement & IN_OMP_BLOCK);
555 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
556 parser->in_statement & IN_CILK_SIMD_FOR);
557 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
558 parser->in_statement & IN_OMP_FOR);
559 cp_debug_print_flag (file, "Parsing an if statement",
560 parser->in_statement & IN_IF_STMT);
561 cp_debug_print_flag (file, "Parsing a type-id in an expression "
562 "context", parser->in_type_id_in_expr_p);
563 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
564 parser->implicit_extern_c);
565 cp_debug_print_flag (file, "String expressions should be translated "
566 "to execution character set",
567 parser->translate_strings_p);
568 cp_debug_print_flag (file, "Parsing function body outside of a "
569 "local class", parser->in_function_body);
570 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
571 parser->colon_corrects_to_scope_p);
572 cp_debug_print_flag (file, "Colon doesn't start a class definition",
573 parser->colon_doesnt_start_class_def_p);
574 if (parser->type_definition_forbidden_message)
575 fprintf (file, "Error message for forbidden type definitions: %s\n",
576 parser->type_definition_forbidden_message);
577 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
578 fprintf (file, "Number of class definitions in progress: %u\n",
579 parser->num_classes_being_defined);
580 fprintf (file, "Number of template parameter lists for the current "
581 "declaration: %u\n", parser->num_template_parameter_lists);
582 cp_debug_parser_tokens (file, parser, window_size);
583 token = parser->lexer->next_token;
584 fprintf (file, "Next token to parse:\n");
585 fprintf (file, "\tToken: ");
586 cp_lexer_print_token (file, token);
587 eloc = expand_location (token->location);
588 fprintf (file, "\n\tFile: %s\n", eloc.file);
589 fprintf (file, "\tLine: %d\n", eloc.line);
590 fprintf (file, "\tColumn: %d\n", eloc.column);
593 DEBUG_FUNCTION void
594 debug (cp_parser &ref)
596 cp_debug_parser (stderr, &ref);
599 DEBUG_FUNCTION void
600 debug (cp_parser *ptr)
602 if (ptr)
603 debug (*ptr);
604 else
605 fprintf (stderr, "<nil>\n");
608 /* Allocate memory for a new lexer object and return it. */
610 static cp_lexer *
611 cp_lexer_alloc (void)
613 cp_lexer *lexer;
615 c_common_no_more_pch ();
617 /* Allocate the memory. */
618 lexer = ggc_cleared_alloc<cp_lexer> ();
620 /* Initially we are not debugging. */
621 lexer->debugging_p = false;
623 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
625 /* Create the buffer. */
626 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
628 return lexer;
632 /* Create a new main C++ lexer, the lexer that gets tokens from the
633 preprocessor. */
635 static cp_lexer *
636 cp_lexer_new_main (void)
638 cp_lexer *lexer;
639 cp_token token;
641 /* It's possible that parsing the first pragma will load a PCH file,
642 which is a GC collection point. So we have to do that before
643 allocating any memory. */
644 cp_parser_initial_pragma (&token);
646 lexer = cp_lexer_alloc ();
648 /* Put the first token in the buffer. */
649 lexer->buffer->quick_push (token);
651 /* Get the remaining tokens from the preprocessor. */
652 while (token.type != CPP_EOF)
654 cp_lexer_get_preprocessor_token (lexer, &token);
655 vec_safe_push (lexer->buffer, token);
658 lexer->last_token = lexer->buffer->address ()
659 + lexer->buffer->length ()
660 - 1;
661 lexer->next_token = lexer->buffer->length ()
662 ? lexer->buffer->address ()
663 : &eof_token;
665 /* Subsequent preprocessor diagnostics should use compiler
666 diagnostic functions to get the compiler source location. */
667 done_lexing = true;
669 gcc_assert (!lexer->next_token->purged_p);
670 return lexer;
673 /* Create a new lexer whose token stream is primed with the tokens in
674 CACHE. When these tokens are exhausted, no new tokens will be read. */
676 static cp_lexer *
677 cp_lexer_new_from_tokens (cp_token_cache *cache)
679 cp_token *first = cache->first;
680 cp_token *last = cache->last;
681 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
683 /* We do not own the buffer. */
684 lexer->buffer = NULL;
685 lexer->next_token = first == last ? &eof_token : first;
686 lexer->last_token = last;
688 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
690 /* Initially we are not debugging. */
691 lexer->debugging_p = false;
693 gcc_assert (!lexer->next_token->purged_p);
694 return lexer;
697 /* Frees all resources associated with LEXER. */
699 static void
700 cp_lexer_destroy (cp_lexer *lexer)
702 vec_free (lexer->buffer);
703 lexer->saved_tokens.release ();
704 ggc_free (lexer);
707 /* Returns nonzero if debugging information should be output. */
709 static inline bool
710 cp_lexer_debugging_p (cp_lexer *lexer)
712 return lexer->debugging_p;
716 static inline cp_token_position
717 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
719 gcc_assert (!previous_p || lexer->next_token != &eof_token);
721 return lexer->next_token - previous_p;
724 static inline cp_token *
725 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
727 return pos;
730 static inline void
731 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
733 lexer->next_token = cp_lexer_token_at (lexer, pos);
736 static inline cp_token_position
737 cp_lexer_previous_token_position (cp_lexer *lexer)
739 if (lexer->next_token == &eof_token)
740 return lexer->last_token - 1;
741 else
742 return cp_lexer_token_position (lexer, true);
745 static inline cp_token *
746 cp_lexer_previous_token (cp_lexer *lexer)
748 cp_token_position tp = cp_lexer_previous_token_position (lexer);
750 return cp_lexer_token_at (lexer, tp);
753 /* nonzero if we are presently saving tokens. */
755 static inline int
756 cp_lexer_saving_tokens (const cp_lexer* lexer)
758 return lexer->saved_tokens.length () != 0;
761 /* Store the next token from the preprocessor in *TOKEN. Return true
762 if we reach EOF. If LEXER is NULL, assume we are handling an
763 initial #pragma pch_preprocess, and thus want the lexer to return
764 processed strings. */
766 static void
767 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
769 static int is_extern_c = 0;
771 /* Get a new token from the preprocessor. */
772 token->type
773 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
774 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
775 token->keyword = RID_MAX;
776 token->pragma_kind = PRAGMA_NONE;
777 token->purged_p = false;
778 token->error_reported = false;
780 /* On some systems, some header files are surrounded by an
781 implicit extern "C" block. Set a flag in the token if it
782 comes from such a header. */
783 is_extern_c += pending_lang_change;
784 pending_lang_change = 0;
785 token->implicit_extern_c = is_extern_c > 0;
787 /* Check to see if this token is a keyword. */
788 if (token->type == CPP_NAME)
790 if (C_IS_RESERVED_WORD (token->u.value))
792 /* Mark this token as a keyword. */
793 token->type = CPP_KEYWORD;
794 /* Record which keyword. */
795 token->keyword = C_RID_CODE (token->u.value);
797 else
799 if (warn_cxx0x_compat
800 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
801 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
803 /* Warn about the C++0x keyword (but still treat it as
804 an identifier). */
805 warning (OPT_Wc__0x_compat,
806 "identifier %qE is a keyword in C++11",
807 token->u.value);
809 /* Clear out the C_RID_CODE so we don't warn about this
810 particular identifier-turned-keyword again. */
811 C_SET_RID_CODE (token->u.value, RID_MAX);
814 token->keyword = RID_MAX;
817 else if (token->type == CPP_AT_NAME)
819 /* This only happens in Objective-C++; it must be a keyword. */
820 token->type = CPP_KEYWORD;
821 switch (C_RID_CODE (token->u.value))
823 /* Replace 'class' with '@class', 'private' with '@private',
824 etc. This prevents confusion with the C++ keyword
825 'class', and makes the tokens consistent with other
826 Objective-C 'AT' keywords. For example '@class' is
827 reported as RID_AT_CLASS which is consistent with
828 '@synchronized', which is reported as
829 RID_AT_SYNCHRONIZED.
831 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
832 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
833 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
834 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
835 case RID_THROW: token->keyword = RID_AT_THROW; break;
836 case RID_TRY: token->keyword = RID_AT_TRY; break;
837 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
838 default: token->keyword = C_RID_CODE (token->u.value);
841 else if (token->type == CPP_PRAGMA)
843 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
844 token->pragma_kind = ((enum pragma_kind)
845 TREE_INT_CST_LOW (token->u.value));
846 token->u.value = NULL_TREE;
850 /* Update the globals input_location and the input file stack from TOKEN. */
851 static inline void
852 cp_lexer_set_source_position_from_token (cp_token *token)
854 if (token->type != CPP_EOF)
856 input_location = token->location;
860 /* Update the globals input_location and the input file stack from LEXER. */
861 static inline void
862 cp_lexer_set_source_position (cp_lexer *lexer)
864 cp_token *token = cp_lexer_peek_token (lexer);
865 cp_lexer_set_source_position_from_token (token);
868 /* Return a pointer to the next token in the token stream, but do not
869 consume it. */
871 static inline cp_token *
872 cp_lexer_peek_token (cp_lexer *lexer)
874 if (cp_lexer_debugging_p (lexer))
876 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
877 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
878 putc ('\n', cp_lexer_debug_stream);
880 return lexer->next_token;
883 /* Return true if the next token has the indicated TYPE. */
885 static inline bool
886 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
888 return cp_lexer_peek_token (lexer)->type == type;
891 /* Return true if the next token does not have the indicated TYPE. */
893 static inline bool
894 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
896 return !cp_lexer_next_token_is (lexer, type);
899 /* Return true if the next token is the indicated KEYWORD. */
901 static inline bool
902 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
904 return cp_lexer_peek_token (lexer)->keyword == keyword;
907 static inline bool
908 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
910 return cp_lexer_peek_nth_token (lexer, n)->type == type;
913 static inline bool
914 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
916 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
919 /* Return true if the next token is not the indicated KEYWORD. */
921 static inline bool
922 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
924 return cp_lexer_peek_token (lexer)->keyword != keyword;
927 /* Return true if the next token is a keyword for a decl-specifier. */
929 static bool
930 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
932 cp_token *token;
934 token = cp_lexer_peek_token (lexer);
935 switch (token->keyword)
937 /* auto specifier: storage-class-specifier in C++,
938 simple-type-specifier in C++0x. */
939 case RID_AUTO:
940 /* Storage classes. */
941 case RID_REGISTER:
942 case RID_STATIC:
943 case RID_EXTERN:
944 case RID_MUTABLE:
945 case RID_THREAD:
946 /* Elaborated type specifiers. */
947 case RID_ENUM:
948 case RID_CLASS:
949 case RID_STRUCT:
950 case RID_UNION:
951 case RID_TYPENAME:
952 /* Simple type specifiers. */
953 case RID_CHAR:
954 case RID_CHAR16:
955 case RID_CHAR32:
956 case RID_WCHAR:
957 case RID_BOOL:
958 case RID_SHORT:
959 case RID_INT:
960 case RID_LONG:
961 case RID_SIGNED:
962 case RID_UNSIGNED:
963 case RID_FLOAT:
964 case RID_DOUBLE:
965 case RID_VOID:
966 /* GNU extensions. */
967 case RID_ATTRIBUTE:
968 case RID_TYPEOF:
969 /* C++0x extensions. */
970 case RID_DECLTYPE:
971 case RID_UNDERLYING_TYPE:
972 return true;
974 default:
975 if (token->keyword >= RID_FIRST_INT_N
976 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
977 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
978 return true;
979 return false;
983 /* Returns TRUE iff the token T begins a decltype type. */
985 static bool
986 token_is_decltype (cp_token *t)
988 return (t->keyword == RID_DECLTYPE
989 || t->type == CPP_DECLTYPE);
992 /* Returns TRUE iff the next token begins a decltype type. */
994 static bool
995 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
997 cp_token *t = cp_lexer_peek_token (lexer);
998 return token_is_decltype (t);
1001 /* Return a pointer to the Nth token in the token stream. If N is 1,
1002 then this is precisely equivalent to cp_lexer_peek_token (except
1003 that it is not inline). One would like to disallow that case, but
1004 there is one case (cp_parser_nth_token_starts_template_id) where
1005 the caller passes a variable for N and it might be 1. */
1007 static cp_token *
1008 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1010 cp_token *token;
1012 /* N is 1-based, not zero-based. */
1013 gcc_assert (n > 0);
1015 if (cp_lexer_debugging_p (lexer))
1016 fprintf (cp_lexer_debug_stream,
1017 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1019 --n;
1020 token = lexer->next_token;
1021 gcc_assert (!n || token != &eof_token);
1022 while (n != 0)
1024 ++token;
1025 if (token == lexer->last_token)
1027 token = &eof_token;
1028 break;
1031 if (!token->purged_p)
1032 --n;
1035 if (cp_lexer_debugging_p (lexer))
1037 cp_lexer_print_token (cp_lexer_debug_stream, token);
1038 putc ('\n', cp_lexer_debug_stream);
1041 return token;
1044 /* Return the next token, and advance the lexer's next_token pointer
1045 to point to the next non-purged token. */
1047 static cp_token *
1048 cp_lexer_consume_token (cp_lexer* lexer)
1050 cp_token *token = lexer->next_token;
1052 gcc_assert (token != &eof_token);
1053 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1057 lexer->next_token++;
1058 if (lexer->next_token == lexer->last_token)
1060 lexer->next_token = &eof_token;
1061 break;
1065 while (lexer->next_token->purged_p);
1067 cp_lexer_set_source_position_from_token (token);
1069 /* Provide debugging output. */
1070 if (cp_lexer_debugging_p (lexer))
1072 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1073 cp_lexer_print_token (cp_lexer_debug_stream, token);
1074 putc ('\n', cp_lexer_debug_stream);
1077 return token;
1080 /* Permanently remove the next token from the token stream, and
1081 advance the next_token pointer to refer to the next non-purged
1082 token. */
1084 static void
1085 cp_lexer_purge_token (cp_lexer *lexer)
1087 cp_token *tok = lexer->next_token;
1089 gcc_assert (tok != &eof_token);
1090 tok->purged_p = true;
1091 tok->location = UNKNOWN_LOCATION;
1092 tok->u.value = NULL_TREE;
1093 tok->keyword = RID_MAX;
1097 tok++;
1098 if (tok == lexer->last_token)
1100 tok = &eof_token;
1101 break;
1104 while (tok->purged_p);
1105 lexer->next_token = tok;
1108 /* Permanently remove all tokens after TOK, up to, but not
1109 including, the token that will be returned next by
1110 cp_lexer_peek_token. */
1112 static void
1113 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1115 cp_token *peek = lexer->next_token;
1117 if (peek == &eof_token)
1118 peek = lexer->last_token;
1120 gcc_assert (tok < peek);
1122 for ( tok += 1; tok != peek; tok += 1)
1124 tok->purged_p = true;
1125 tok->location = UNKNOWN_LOCATION;
1126 tok->u.value = NULL_TREE;
1127 tok->keyword = RID_MAX;
1131 /* Begin saving tokens. All tokens consumed after this point will be
1132 preserved. */
1134 static void
1135 cp_lexer_save_tokens (cp_lexer* lexer)
1137 /* Provide debugging output. */
1138 if (cp_lexer_debugging_p (lexer))
1139 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1141 lexer->saved_tokens.safe_push (lexer->next_token);
1144 /* Commit to the portion of the token stream most recently saved. */
1146 static void
1147 cp_lexer_commit_tokens (cp_lexer* lexer)
1149 /* Provide debugging output. */
1150 if (cp_lexer_debugging_p (lexer))
1151 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1153 lexer->saved_tokens.pop ();
1156 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1157 to the token stream. Stop saving tokens. */
1159 static void
1160 cp_lexer_rollback_tokens (cp_lexer* lexer)
1162 /* Provide debugging output. */
1163 if (cp_lexer_debugging_p (lexer))
1164 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1166 lexer->next_token = lexer->saved_tokens.pop ();
1169 /* RAII wrapper around the above functions, with sanity checking. Creating
1170 a variable saves tokens, which are committed when the variable is
1171 destroyed unless they are explicitly rolled back by calling the rollback
1172 member function. */
1174 struct saved_token_sentinel
1176 cp_lexer *lexer;
1177 unsigned len;
1178 bool commit;
1179 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1181 len = lexer->saved_tokens.length ();
1182 cp_lexer_save_tokens (lexer);
1184 void rollback ()
1186 cp_lexer_rollback_tokens (lexer);
1187 commit = false;
1189 ~saved_token_sentinel()
1191 if (commit)
1192 cp_lexer_commit_tokens (lexer);
1193 gcc_assert (lexer->saved_tokens.length () == len);
1197 /* Print a representation of the TOKEN on the STREAM. */
1199 static void
1200 cp_lexer_print_token (FILE * stream, cp_token *token)
1202 /* We don't use cpp_type2name here because the parser defines
1203 a few tokens of its own. */
1204 static const char *const token_names[] = {
1205 /* cpplib-defined token types */
1206 #define OP(e, s) #e,
1207 #define TK(e, s) #e,
1208 TTYPE_TABLE
1209 #undef OP
1210 #undef TK
1211 /* C++ parser token types - see "Manifest constants", above. */
1212 "KEYWORD",
1213 "TEMPLATE_ID",
1214 "NESTED_NAME_SPECIFIER",
1217 /* For some tokens, print the associated data. */
1218 switch (token->type)
1220 case CPP_KEYWORD:
1221 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1222 For example, `struct' is mapped to an INTEGER_CST. */
1223 if (!identifier_p (token->u.value))
1224 break;
1225 /* else fall through */
1226 case CPP_NAME:
1227 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1228 break;
1230 case CPP_STRING:
1231 case CPP_STRING16:
1232 case CPP_STRING32:
1233 case CPP_WSTRING:
1234 case CPP_UTF8STRING:
1235 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1236 break;
1238 case CPP_NUMBER:
1239 print_generic_expr (stream, token->u.value, 0);
1240 break;
1242 default:
1243 /* If we have a name for the token, print it out. Otherwise, we
1244 simply give the numeric code. */
1245 if (token->type < ARRAY_SIZE(token_names))
1246 fputs (token_names[token->type], stream);
1247 else
1248 fprintf (stream, "[%d]", token->type);
1249 break;
1253 DEBUG_FUNCTION void
1254 debug (cp_token &ref)
1256 cp_lexer_print_token (stderr, &ref);
1257 fprintf (stderr, "\n");
1260 DEBUG_FUNCTION void
1261 debug (cp_token *ptr)
1263 if (ptr)
1264 debug (*ptr);
1265 else
1266 fprintf (stderr, "<nil>\n");
1270 /* Start emitting debugging information. */
1272 static void
1273 cp_lexer_start_debugging (cp_lexer* lexer)
1275 lexer->debugging_p = true;
1276 cp_lexer_debug_stream = stderr;
1279 /* Stop emitting debugging information. */
1281 static void
1282 cp_lexer_stop_debugging (cp_lexer* lexer)
1284 lexer->debugging_p = false;
1285 cp_lexer_debug_stream = NULL;
1288 /* Create a new cp_token_cache, representing a range of tokens. */
1290 static cp_token_cache *
1291 cp_token_cache_new (cp_token *first, cp_token *last)
1293 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1294 cache->first = first;
1295 cache->last = last;
1296 return cache;
1299 /* Diagnose if #pragma omp declare simd isn't followed immediately
1300 by function declaration or definition. */
1302 static inline void
1303 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1305 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1307 error ("%<#pragma omp declare simd%> not immediately followed by "
1308 "function declaration or definition");
1309 parser->omp_declare_simd = NULL;
1313 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1314 and put that into "omp declare simd" attribute. */
1316 static inline void
1317 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1319 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1321 if (fndecl == error_mark_node)
1323 parser->omp_declare_simd = NULL;
1324 return;
1326 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1328 cp_ensure_no_omp_declare_simd (parser);
1329 return;
1334 /* Decl-specifiers. */
1336 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1338 static void
1339 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1341 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1344 /* Declarators. */
1346 /* Nothing other than the parser should be creating declarators;
1347 declarators are a semi-syntactic representation of C++ entities.
1348 Other parts of the front end that need to create entities (like
1349 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1351 static cp_declarator *make_call_declarator
1352 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1353 static cp_declarator *make_array_declarator
1354 (cp_declarator *, tree);
1355 static cp_declarator *make_pointer_declarator
1356 (cp_cv_quals, cp_declarator *, tree);
1357 static cp_declarator *make_reference_declarator
1358 (cp_cv_quals, cp_declarator *, bool, tree);
1359 static cp_parameter_declarator *make_parameter_declarator
1360 (cp_decl_specifier_seq *, cp_declarator *, tree);
1361 static cp_declarator *make_ptrmem_declarator
1362 (cp_cv_quals, tree, cp_declarator *, tree);
1364 /* An erroneous declarator. */
1365 static cp_declarator *cp_error_declarator;
1367 /* The obstack on which declarators and related data structures are
1368 allocated. */
1369 static struct obstack declarator_obstack;
1371 /* Alloc BYTES from the declarator memory pool. */
1373 static inline void *
1374 alloc_declarator (size_t bytes)
1376 return obstack_alloc (&declarator_obstack, bytes);
1379 /* Allocate a declarator of the indicated KIND. Clear fields that are
1380 common to all declarators. */
1382 static cp_declarator *
1383 make_declarator (cp_declarator_kind kind)
1385 cp_declarator *declarator;
1387 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1388 declarator->kind = kind;
1389 declarator->attributes = NULL_TREE;
1390 declarator->std_attributes = NULL_TREE;
1391 declarator->declarator = NULL;
1392 declarator->parameter_pack_p = false;
1393 declarator->id_loc = UNKNOWN_LOCATION;
1395 return declarator;
1398 /* Make a declarator for a generalized identifier. If
1399 QUALIFYING_SCOPE is non-NULL, the identifier is
1400 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1401 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1402 is, if any. */
1404 static cp_declarator *
1405 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1406 special_function_kind sfk)
1408 cp_declarator *declarator;
1410 /* It is valid to write:
1412 class C { void f(); };
1413 typedef C D;
1414 void D::f();
1416 The standard is not clear about whether `typedef const C D' is
1417 legal; as of 2002-09-15 the committee is considering that
1418 question. EDG 3.0 allows that syntax. Therefore, we do as
1419 well. */
1420 if (qualifying_scope && TYPE_P (qualifying_scope))
1421 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1423 gcc_assert (identifier_p (unqualified_name)
1424 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1425 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1427 declarator = make_declarator (cdk_id);
1428 declarator->u.id.qualifying_scope = qualifying_scope;
1429 declarator->u.id.unqualified_name = unqualified_name;
1430 declarator->u.id.sfk = sfk;
1432 return declarator;
1435 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1436 of modifiers such as const or volatile to apply to the pointer
1437 type, represented as identifiers. ATTRIBUTES represent the attributes that
1438 appertain to the pointer or reference. */
1440 cp_declarator *
1441 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1442 tree attributes)
1444 cp_declarator *declarator;
1446 declarator = make_declarator (cdk_pointer);
1447 declarator->declarator = target;
1448 declarator->u.pointer.qualifiers = cv_qualifiers;
1449 declarator->u.pointer.class_type = NULL_TREE;
1450 if (target)
1452 declarator->id_loc = target->id_loc;
1453 declarator->parameter_pack_p = target->parameter_pack_p;
1454 target->parameter_pack_p = false;
1456 else
1457 declarator->parameter_pack_p = false;
1459 declarator->std_attributes = attributes;
1461 return declarator;
1464 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1465 represent the attributes that appertain to the pointer or
1466 reference. */
1468 cp_declarator *
1469 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1470 bool rvalue_ref, tree attributes)
1472 cp_declarator *declarator;
1474 declarator = make_declarator (cdk_reference);
1475 declarator->declarator = target;
1476 declarator->u.reference.qualifiers = cv_qualifiers;
1477 declarator->u.reference.rvalue_ref = rvalue_ref;
1478 if (target)
1480 declarator->id_loc = target->id_loc;
1481 declarator->parameter_pack_p = target->parameter_pack_p;
1482 target->parameter_pack_p = false;
1484 else
1485 declarator->parameter_pack_p = false;
1487 declarator->std_attributes = attributes;
1489 return declarator;
1492 /* Like make_pointer_declarator -- but for a pointer to a non-static
1493 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1494 appertain to the pointer or reference. */
1496 cp_declarator *
1497 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1498 cp_declarator *pointee,
1499 tree attributes)
1501 cp_declarator *declarator;
1503 declarator = make_declarator (cdk_ptrmem);
1504 declarator->declarator = pointee;
1505 declarator->u.pointer.qualifiers = cv_qualifiers;
1506 declarator->u.pointer.class_type = class_type;
1508 if (pointee)
1510 declarator->parameter_pack_p = pointee->parameter_pack_p;
1511 pointee->parameter_pack_p = false;
1513 else
1514 declarator->parameter_pack_p = false;
1516 declarator->std_attributes = attributes;
1518 return declarator;
1521 /* Make a declarator for the function given by TARGET, with the
1522 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1523 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1524 indicates what exceptions can be thrown. */
1526 cp_declarator *
1527 make_call_declarator (cp_declarator *target,
1528 tree parms,
1529 cp_cv_quals cv_qualifiers,
1530 cp_virt_specifiers virt_specifiers,
1531 cp_ref_qualifier ref_qualifier,
1532 tree exception_specification,
1533 tree late_return_type)
1535 cp_declarator *declarator;
1537 declarator = make_declarator (cdk_function);
1538 declarator->declarator = target;
1539 declarator->u.function.parameters = parms;
1540 declarator->u.function.qualifiers = cv_qualifiers;
1541 declarator->u.function.virt_specifiers = virt_specifiers;
1542 declarator->u.function.ref_qualifier = ref_qualifier;
1543 declarator->u.function.exception_specification = exception_specification;
1544 declarator->u.function.late_return_type = late_return_type;
1545 if (target)
1547 declarator->id_loc = target->id_loc;
1548 declarator->parameter_pack_p = target->parameter_pack_p;
1549 target->parameter_pack_p = false;
1551 else
1552 declarator->parameter_pack_p = false;
1554 return declarator;
1557 /* Make a declarator for an array of BOUNDS elements, each of which is
1558 defined by ELEMENT. */
1560 cp_declarator *
1561 make_array_declarator (cp_declarator *element, tree bounds)
1563 cp_declarator *declarator;
1565 declarator = make_declarator (cdk_array);
1566 declarator->declarator = element;
1567 declarator->u.array.bounds = bounds;
1568 if (element)
1570 declarator->id_loc = element->id_loc;
1571 declarator->parameter_pack_p = element->parameter_pack_p;
1572 element->parameter_pack_p = false;
1574 else
1575 declarator->parameter_pack_p = false;
1577 return declarator;
1580 /* Determine whether the declarator we've seen so far can be a
1581 parameter pack, when followed by an ellipsis. */
1582 static bool
1583 declarator_can_be_parameter_pack (cp_declarator *declarator)
1585 /* Search for a declarator name, or any other declarator that goes
1586 after the point where the ellipsis could appear in a parameter
1587 pack. If we find any of these, then this declarator can not be
1588 made into a parameter pack. */
1589 bool found = false;
1590 while (declarator && !found)
1592 switch ((int)declarator->kind)
1594 case cdk_id:
1595 case cdk_array:
1596 found = true;
1597 break;
1599 case cdk_error:
1600 return true;
1602 default:
1603 declarator = declarator->declarator;
1604 break;
1608 return !found;
1611 cp_parameter_declarator *no_parameters;
1613 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1614 DECLARATOR and DEFAULT_ARGUMENT. */
1616 cp_parameter_declarator *
1617 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1618 cp_declarator *declarator,
1619 tree default_argument)
1621 cp_parameter_declarator *parameter;
1623 parameter = ((cp_parameter_declarator *)
1624 alloc_declarator (sizeof (cp_parameter_declarator)));
1625 parameter->next = NULL;
1626 if (decl_specifiers)
1627 parameter->decl_specifiers = *decl_specifiers;
1628 else
1629 clear_decl_specs (&parameter->decl_specifiers);
1630 parameter->declarator = declarator;
1631 parameter->default_argument = default_argument;
1632 parameter->ellipsis_p = false;
1634 return parameter;
1637 /* Returns true iff DECLARATOR is a declaration for a function. */
1639 static bool
1640 function_declarator_p (const cp_declarator *declarator)
1642 while (declarator)
1644 if (declarator->kind == cdk_function
1645 && declarator->declarator->kind == cdk_id)
1646 return true;
1647 if (declarator->kind == cdk_id
1648 || declarator->kind == cdk_error)
1649 return false;
1650 declarator = declarator->declarator;
1652 return false;
1655 /* The parser. */
1657 /* Overview
1658 --------
1660 A cp_parser parses the token stream as specified by the C++
1661 grammar. Its job is purely parsing, not semantic analysis. For
1662 example, the parser breaks the token stream into declarators,
1663 expressions, statements, and other similar syntactic constructs.
1664 It does not check that the types of the expressions on either side
1665 of an assignment-statement are compatible, or that a function is
1666 not declared with a parameter of type `void'.
1668 The parser invokes routines elsewhere in the compiler to perform
1669 semantic analysis and to build up the abstract syntax tree for the
1670 code processed.
1672 The parser (and the template instantiation code, which is, in a
1673 way, a close relative of parsing) are the only parts of the
1674 compiler that should be calling push_scope and pop_scope, or
1675 related functions. The parser (and template instantiation code)
1676 keeps track of what scope is presently active; everything else
1677 should simply honor that. (The code that generates static
1678 initializers may also need to set the scope, in order to check
1679 access control correctly when emitting the initializers.)
1681 Methodology
1682 -----------
1684 The parser is of the standard recursive-descent variety. Upcoming
1685 tokens in the token stream are examined in order to determine which
1686 production to use when parsing a non-terminal. Some C++ constructs
1687 require arbitrary look ahead to disambiguate. For example, it is
1688 impossible, in the general case, to tell whether a statement is an
1689 expression or declaration without scanning the entire statement.
1690 Therefore, the parser is capable of "parsing tentatively." When the
1691 parser is not sure what construct comes next, it enters this mode.
1692 Then, while we attempt to parse the construct, the parser queues up
1693 error messages, rather than issuing them immediately, and saves the
1694 tokens it consumes. If the construct is parsed successfully, the
1695 parser "commits", i.e., it issues any queued error messages and
1696 the tokens that were being preserved are permanently discarded.
1697 If, however, the construct is not parsed successfully, the parser
1698 rolls back its state completely so that it can resume parsing using
1699 a different alternative.
1701 Future Improvements
1702 -------------------
1704 The performance of the parser could probably be improved substantially.
1705 We could often eliminate the need to parse tentatively by looking ahead
1706 a little bit. In some places, this approach might not entirely eliminate
1707 the need to parse tentatively, but it might still speed up the average
1708 case. */
1710 /* Flags that are passed to some parsing functions. These values can
1711 be bitwise-ored together. */
1713 enum
1715 /* No flags. */
1716 CP_PARSER_FLAGS_NONE = 0x0,
1717 /* The construct is optional. If it is not present, then no error
1718 should be issued. */
1719 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1720 /* When parsing a type-specifier, treat user-defined type-names
1721 as non-type identifiers. */
1722 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1723 /* When parsing a type-specifier, do not try to parse a class-specifier
1724 or enum-specifier. */
1725 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1726 /* When parsing a decl-specifier-seq, only allow type-specifier or
1727 constexpr. */
1728 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1731 /* This type is used for parameters and variables which hold
1732 combinations of the above flags. */
1733 typedef int cp_parser_flags;
1735 /* The different kinds of declarators we want to parse. */
1737 typedef enum cp_parser_declarator_kind
1739 /* We want an abstract declarator. */
1740 CP_PARSER_DECLARATOR_ABSTRACT,
1741 /* We want a named declarator. */
1742 CP_PARSER_DECLARATOR_NAMED,
1743 /* We don't mind, but the name must be an unqualified-id. */
1744 CP_PARSER_DECLARATOR_EITHER
1745 } cp_parser_declarator_kind;
1747 /* The precedence values used to parse binary expressions. The minimum value
1748 of PREC must be 1, because zero is reserved to quickly discriminate
1749 binary operators from other tokens. */
1751 enum cp_parser_prec
1753 PREC_NOT_OPERATOR,
1754 PREC_LOGICAL_OR_EXPRESSION,
1755 PREC_LOGICAL_AND_EXPRESSION,
1756 PREC_INCLUSIVE_OR_EXPRESSION,
1757 PREC_EXCLUSIVE_OR_EXPRESSION,
1758 PREC_AND_EXPRESSION,
1759 PREC_EQUALITY_EXPRESSION,
1760 PREC_RELATIONAL_EXPRESSION,
1761 PREC_SHIFT_EXPRESSION,
1762 PREC_ADDITIVE_EXPRESSION,
1763 PREC_MULTIPLICATIVE_EXPRESSION,
1764 PREC_PM_EXPRESSION,
1765 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1768 /* A mapping from a token type to a corresponding tree node type, with a
1769 precedence value. */
1771 typedef struct cp_parser_binary_operations_map_node
1773 /* The token type. */
1774 enum cpp_ttype token_type;
1775 /* The corresponding tree code. */
1776 enum tree_code tree_type;
1777 /* The precedence of this operator. */
1778 enum cp_parser_prec prec;
1779 } cp_parser_binary_operations_map_node;
1781 typedef struct cp_parser_expression_stack_entry
1783 /* Left hand side of the binary operation we are currently
1784 parsing. */
1785 tree lhs;
1786 /* Original tree code for left hand side, if it was a binary
1787 expression itself (used for -Wparentheses). */
1788 enum tree_code lhs_type;
1789 /* Tree code for the binary operation we are parsing. */
1790 enum tree_code tree_type;
1791 /* Precedence of the binary operation we are parsing. */
1792 enum cp_parser_prec prec;
1793 /* Location of the binary operation we are parsing. */
1794 location_t loc;
1795 } cp_parser_expression_stack_entry;
1797 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1798 entries because precedence levels on the stack are monotonically
1799 increasing. */
1800 typedef struct cp_parser_expression_stack_entry
1801 cp_parser_expression_stack[NUM_PREC_VALUES];
1803 /* Prototypes. */
1805 /* Constructors and destructors. */
1807 static cp_parser_context *cp_parser_context_new
1808 (cp_parser_context *);
1810 /* Class variables. */
1812 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1814 /* The operator-precedence table used by cp_parser_binary_expression.
1815 Transformed into an associative array (binops_by_token) by
1816 cp_parser_new. */
1818 static const cp_parser_binary_operations_map_node binops[] = {
1819 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1820 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1822 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1823 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1824 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1826 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1827 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1829 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1830 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1832 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1833 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1834 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1835 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1837 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1838 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1840 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1842 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1844 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1846 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1848 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1851 /* The same as binops, but initialized by cp_parser_new so that
1852 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1853 for speed. */
1854 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1856 /* Constructors and destructors. */
1858 /* Construct a new context. The context below this one on the stack
1859 is given by NEXT. */
1861 static cp_parser_context *
1862 cp_parser_context_new (cp_parser_context* next)
1864 cp_parser_context *context;
1866 /* Allocate the storage. */
1867 if (cp_parser_context_free_list != NULL)
1869 /* Pull the first entry from the free list. */
1870 context = cp_parser_context_free_list;
1871 cp_parser_context_free_list = context->next;
1872 memset (context, 0, sizeof (*context));
1874 else
1875 context = ggc_cleared_alloc<cp_parser_context> ();
1877 /* No errors have occurred yet in this context. */
1878 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1879 /* If this is not the bottommost context, copy information that we
1880 need from the previous context. */
1881 if (next)
1883 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1884 expression, then we are parsing one in this context, too. */
1885 context->object_type = next->object_type;
1886 /* Thread the stack. */
1887 context->next = next;
1890 return context;
1893 /* Managing the unparsed function queues. */
1895 #define unparsed_funs_with_default_args \
1896 parser->unparsed_queues->last ().funs_with_default_args
1897 #define unparsed_funs_with_definitions \
1898 parser->unparsed_queues->last ().funs_with_definitions
1899 #define unparsed_nsdmis \
1900 parser->unparsed_queues->last ().nsdmis
1901 #define unparsed_classes \
1902 parser->unparsed_queues->last ().classes
1904 static void
1905 push_unparsed_function_queues (cp_parser *parser)
1907 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1908 vec_safe_push (parser->unparsed_queues, e);
1911 static void
1912 pop_unparsed_function_queues (cp_parser *parser)
1914 release_tree_vector (unparsed_funs_with_definitions);
1915 parser->unparsed_queues->pop ();
1918 /* Prototypes. */
1920 /* Constructors and destructors. */
1922 static cp_parser *cp_parser_new
1923 (void);
1925 /* Routines to parse various constructs.
1927 Those that return `tree' will return the error_mark_node (rather
1928 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1929 Sometimes, they will return an ordinary node if error-recovery was
1930 attempted, even though a parse error occurred. So, to check
1931 whether or not a parse error occurred, you should always use
1932 cp_parser_error_occurred. If the construct is optional (indicated
1933 either by an `_opt' in the name of the function that does the
1934 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1935 the construct is not present. */
1937 /* Lexical conventions [gram.lex] */
1939 static tree cp_parser_identifier
1940 (cp_parser *);
1941 static tree cp_parser_string_literal
1942 (cp_parser *, bool, bool, bool);
1943 static tree cp_parser_userdef_char_literal
1944 (cp_parser *);
1945 static tree cp_parser_userdef_string_literal
1946 (tree);
1947 static tree cp_parser_userdef_numeric_literal
1948 (cp_parser *);
1950 /* Basic concepts [gram.basic] */
1952 static bool cp_parser_translation_unit
1953 (cp_parser *);
1955 /* Expressions [gram.expr] */
1957 static tree cp_parser_primary_expression
1958 (cp_parser *, bool, bool, bool, cp_id_kind *);
1959 static tree cp_parser_id_expression
1960 (cp_parser *, bool, bool, bool *, bool, bool);
1961 static tree cp_parser_unqualified_id
1962 (cp_parser *, bool, bool, bool, bool);
1963 static tree cp_parser_nested_name_specifier_opt
1964 (cp_parser *, bool, bool, bool, bool);
1965 static tree cp_parser_nested_name_specifier
1966 (cp_parser *, bool, bool, bool, bool);
1967 static tree cp_parser_qualifying_entity
1968 (cp_parser *, bool, bool, bool, bool, bool);
1969 static tree cp_parser_postfix_expression
1970 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1971 static tree cp_parser_postfix_open_square_expression
1972 (cp_parser *, tree, bool, bool);
1973 static tree cp_parser_postfix_dot_deref_expression
1974 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1975 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1976 (cp_parser *, int, bool, bool, bool *, bool = false);
1977 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1978 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1979 static void cp_parser_pseudo_destructor_name
1980 (cp_parser *, tree, tree *, tree *);
1981 static tree cp_parser_unary_expression
1982 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1983 static enum tree_code cp_parser_unary_operator
1984 (cp_token *);
1985 static tree cp_parser_new_expression
1986 (cp_parser *);
1987 static vec<tree, va_gc> *cp_parser_new_placement
1988 (cp_parser *);
1989 static tree cp_parser_new_type_id
1990 (cp_parser *, tree *);
1991 static cp_declarator *cp_parser_new_declarator_opt
1992 (cp_parser *);
1993 static cp_declarator *cp_parser_direct_new_declarator
1994 (cp_parser *);
1995 static vec<tree, va_gc> *cp_parser_new_initializer
1996 (cp_parser *);
1997 static tree cp_parser_delete_expression
1998 (cp_parser *);
1999 static tree cp_parser_cast_expression
2000 (cp_parser *, bool, bool, bool, cp_id_kind *);
2001 static tree cp_parser_binary_expression
2002 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2003 static tree cp_parser_question_colon_clause
2004 (cp_parser *, tree);
2005 static tree cp_parser_assignment_expression
2006 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2007 static enum tree_code cp_parser_assignment_operator_opt
2008 (cp_parser *);
2009 static tree cp_parser_expression
2010 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2011 static tree cp_parser_constant_expression
2012 (cp_parser *, bool = false, bool * = NULL);
2013 static tree cp_parser_builtin_offsetof
2014 (cp_parser *);
2015 static tree cp_parser_lambda_expression
2016 (cp_parser *);
2017 static void cp_parser_lambda_introducer
2018 (cp_parser *, tree);
2019 static bool cp_parser_lambda_declarator_opt
2020 (cp_parser *, tree);
2021 static void cp_parser_lambda_body
2022 (cp_parser *, tree);
2024 /* Statements [gram.stmt.stmt] */
2026 static void cp_parser_statement
2027 (cp_parser *, tree, bool, bool *);
2028 static void cp_parser_label_for_labeled_statement
2029 (cp_parser *, tree);
2030 static tree cp_parser_expression_statement
2031 (cp_parser *, tree);
2032 static tree cp_parser_compound_statement
2033 (cp_parser *, tree, bool, bool);
2034 static void cp_parser_statement_seq_opt
2035 (cp_parser *, tree);
2036 static tree cp_parser_selection_statement
2037 (cp_parser *, bool *);
2038 static tree cp_parser_condition
2039 (cp_parser *);
2040 static tree cp_parser_iteration_statement
2041 (cp_parser *, bool);
2042 static bool cp_parser_for_init_statement
2043 (cp_parser *, tree *decl);
2044 static tree cp_parser_for
2045 (cp_parser *, bool);
2046 static tree cp_parser_c_for
2047 (cp_parser *, tree, tree, bool);
2048 static tree cp_parser_range_for
2049 (cp_parser *, tree, tree, tree, bool);
2050 static void do_range_for_auto_deduction
2051 (tree, tree);
2052 static tree cp_parser_perform_range_for_lookup
2053 (tree, tree *, tree *);
2054 static tree cp_parser_range_for_member_function
2055 (tree, tree);
2056 static tree cp_parser_jump_statement
2057 (cp_parser *);
2058 static void cp_parser_declaration_statement
2059 (cp_parser *);
2061 static tree cp_parser_implicitly_scoped_statement
2062 (cp_parser *, bool *);
2063 static void cp_parser_already_scoped_statement
2064 (cp_parser *);
2066 /* Declarations [gram.dcl.dcl] */
2068 static void cp_parser_declaration_seq_opt
2069 (cp_parser *);
2070 static void cp_parser_declaration
2071 (cp_parser *);
2072 static void cp_parser_block_declaration
2073 (cp_parser *, bool);
2074 static void cp_parser_simple_declaration
2075 (cp_parser *, bool, tree *);
2076 static void cp_parser_decl_specifier_seq
2077 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2078 static tree cp_parser_storage_class_specifier_opt
2079 (cp_parser *);
2080 static tree cp_parser_function_specifier_opt
2081 (cp_parser *, cp_decl_specifier_seq *);
2082 static tree cp_parser_type_specifier
2083 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2084 int *, bool *);
2085 static tree cp_parser_simple_type_specifier
2086 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2087 static tree cp_parser_type_name
2088 (cp_parser *);
2089 static tree cp_parser_nonclass_name
2090 (cp_parser* parser);
2091 static tree cp_parser_elaborated_type_specifier
2092 (cp_parser *, bool, bool);
2093 static tree cp_parser_enum_specifier
2094 (cp_parser *);
2095 static void cp_parser_enumerator_list
2096 (cp_parser *, tree);
2097 static void cp_parser_enumerator_definition
2098 (cp_parser *, tree);
2099 static tree cp_parser_namespace_name
2100 (cp_parser *);
2101 static void cp_parser_namespace_definition
2102 (cp_parser *);
2103 static void cp_parser_namespace_body
2104 (cp_parser *);
2105 static tree cp_parser_qualified_namespace_specifier
2106 (cp_parser *);
2107 static void cp_parser_namespace_alias_definition
2108 (cp_parser *);
2109 static bool cp_parser_using_declaration
2110 (cp_parser *, bool);
2111 static void cp_parser_using_directive
2112 (cp_parser *);
2113 static tree cp_parser_alias_declaration
2114 (cp_parser *);
2115 static void cp_parser_asm_definition
2116 (cp_parser *);
2117 static void cp_parser_linkage_specification
2118 (cp_parser *);
2119 static void cp_parser_static_assert
2120 (cp_parser *, bool);
2121 static tree cp_parser_decltype
2122 (cp_parser *);
2124 /* Declarators [gram.dcl.decl] */
2126 static tree cp_parser_init_declarator
2127 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2128 bool, bool, int, bool *, tree *, location_t *);
2129 static cp_declarator *cp_parser_declarator
2130 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2131 static cp_declarator *cp_parser_direct_declarator
2132 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2133 static enum tree_code cp_parser_ptr_operator
2134 (cp_parser *, tree *, cp_cv_quals *, tree *);
2135 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2136 (cp_parser *);
2137 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2138 (cp_parser *);
2139 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2140 (cp_parser *);
2141 static tree cp_parser_late_return_type_opt
2142 (cp_parser *, cp_declarator *, cp_cv_quals);
2143 static tree cp_parser_declarator_id
2144 (cp_parser *, bool);
2145 static tree cp_parser_type_id
2146 (cp_parser *);
2147 static tree cp_parser_template_type_arg
2148 (cp_parser *);
2149 static tree cp_parser_trailing_type_id (cp_parser *);
2150 static tree cp_parser_type_id_1
2151 (cp_parser *, bool, bool);
2152 static void cp_parser_type_specifier_seq
2153 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2154 static tree cp_parser_parameter_declaration_clause
2155 (cp_parser *);
2156 static tree cp_parser_parameter_declaration_list
2157 (cp_parser *, bool *);
2158 static cp_parameter_declarator *cp_parser_parameter_declaration
2159 (cp_parser *, bool, bool *);
2160 static tree cp_parser_default_argument
2161 (cp_parser *, bool);
2162 static void cp_parser_function_body
2163 (cp_parser *, bool);
2164 static tree cp_parser_initializer
2165 (cp_parser *, bool *, bool *);
2166 static tree cp_parser_initializer_clause
2167 (cp_parser *, bool *);
2168 static tree cp_parser_braced_list
2169 (cp_parser*, bool*);
2170 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2171 (cp_parser *, bool *);
2173 static bool cp_parser_ctor_initializer_opt_and_function_body
2174 (cp_parser *, bool);
2176 static tree cp_parser_late_parsing_omp_declare_simd
2177 (cp_parser *, tree);
2179 static tree cp_parser_late_parsing_cilk_simd_fn_info
2180 (cp_parser *, tree);
2182 static tree synthesize_implicit_template_parm
2183 (cp_parser *);
2184 static tree finish_fully_implicit_template
2185 (cp_parser *, tree);
2187 /* Classes [gram.class] */
2189 static tree cp_parser_class_name
2190 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2191 static tree cp_parser_class_specifier
2192 (cp_parser *);
2193 static tree cp_parser_class_head
2194 (cp_parser *, bool *);
2195 static enum tag_types cp_parser_class_key
2196 (cp_parser *);
2197 static void cp_parser_type_parameter_key
2198 (cp_parser* parser);
2199 static void cp_parser_member_specification_opt
2200 (cp_parser *);
2201 static void cp_parser_member_declaration
2202 (cp_parser *);
2203 static tree cp_parser_pure_specifier
2204 (cp_parser *);
2205 static tree cp_parser_constant_initializer
2206 (cp_parser *);
2208 /* Derived classes [gram.class.derived] */
2210 static tree cp_parser_base_clause
2211 (cp_parser *);
2212 static tree cp_parser_base_specifier
2213 (cp_parser *);
2215 /* Special member functions [gram.special] */
2217 static tree cp_parser_conversion_function_id
2218 (cp_parser *);
2219 static tree cp_parser_conversion_type_id
2220 (cp_parser *);
2221 static cp_declarator *cp_parser_conversion_declarator_opt
2222 (cp_parser *);
2223 static bool cp_parser_ctor_initializer_opt
2224 (cp_parser *);
2225 static void cp_parser_mem_initializer_list
2226 (cp_parser *);
2227 static tree cp_parser_mem_initializer
2228 (cp_parser *);
2229 static tree cp_parser_mem_initializer_id
2230 (cp_parser *);
2232 /* Overloading [gram.over] */
2234 static tree cp_parser_operator_function_id
2235 (cp_parser *);
2236 static tree cp_parser_operator
2237 (cp_parser *);
2239 /* Templates [gram.temp] */
2241 static void cp_parser_template_declaration
2242 (cp_parser *, bool);
2243 static tree cp_parser_template_parameter_list
2244 (cp_parser *);
2245 static tree cp_parser_template_parameter
2246 (cp_parser *, bool *, bool *);
2247 static tree cp_parser_type_parameter
2248 (cp_parser *, bool *);
2249 static tree cp_parser_template_id
2250 (cp_parser *, bool, bool, enum tag_types, bool);
2251 static tree cp_parser_template_name
2252 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2253 static tree cp_parser_template_argument_list
2254 (cp_parser *);
2255 static tree cp_parser_template_argument
2256 (cp_parser *);
2257 static void cp_parser_explicit_instantiation
2258 (cp_parser *);
2259 static void cp_parser_explicit_specialization
2260 (cp_parser *);
2262 /* Exception handling [gram.exception] */
2264 static tree cp_parser_try_block
2265 (cp_parser *);
2266 static bool cp_parser_function_try_block
2267 (cp_parser *);
2268 static void cp_parser_handler_seq
2269 (cp_parser *);
2270 static void cp_parser_handler
2271 (cp_parser *);
2272 static tree cp_parser_exception_declaration
2273 (cp_parser *);
2274 static tree cp_parser_throw_expression
2275 (cp_parser *);
2276 static tree cp_parser_exception_specification_opt
2277 (cp_parser *);
2278 static tree cp_parser_type_id_list
2279 (cp_parser *);
2281 /* GNU Extensions */
2283 static tree cp_parser_asm_specification_opt
2284 (cp_parser *);
2285 static tree cp_parser_asm_operand_list
2286 (cp_parser *);
2287 static tree cp_parser_asm_clobber_list
2288 (cp_parser *);
2289 static tree cp_parser_asm_label_list
2290 (cp_parser *);
2291 static bool cp_next_tokens_can_be_attribute_p
2292 (cp_parser *);
2293 static bool cp_next_tokens_can_be_gnu_attribute_p
2294 (cp_parser *);
2295 static bool cp_next_tokens_can_be_std_attribute_p
2296 (cp_parser *);
2297 static bool cp_nth_tokens_can_be_std_attribute_p
2298 (cp_parser *, size_t);
2299 static bool cp_nth_tokens_can_be_gnu_attribute_p
2300 (cp_parser *, size_t);
2301 static bool cp_nth_tokens_can_be_attribute_p
2302 (cp_parser *, size_t);
2303 static tree cp_parser_attributes_opt
2304 (cp_parser *);
2305 static tree cp_parser_gnu_attributes_opt
2306 (cp_parser *);
2307 static tree cp_parser_gnu_attribute_list
2308 (cp_parser *);
2309 static tree cp_parser_std_attribute
2310 (cp_parser *);
2311 static tree cp_parser_std_attribute_spec
2312 (cp_parser *);
2313 static tree cp_parser_std_attribute_spec_seq
2314 (cp_parser *);
2315 static bool cp_parser_extension_opt
2316 (cp_parser *, int *);
2317 static void cp_parser_label_declaration
2318 (cp_parser *);
2320 /* Transactional Memory Extensions */
2322 static tree cp_parser_transaction
2323 (cp_parser *, enum rid);
2324 static tree cp_parser_transaction_expression
2325 (cp_parser *, enum rid);
2326 static bool cp_parser_function_transaction
2327 (cp_parser *, enum rid);
2328 static tree cp_parser_transaction_cancel
2329 (cp_parser *);
2331 enum pragma_context {
2332 pragma_external,
2333 pragma_member,
2334 pragma_objc_icode,
2335 pragma_stmt,
2336 pragma_compound
2338 static bool cp_parser_pragma
2339 (cp_parser *, enum pragma_context);
2341 /* Objective-C++ Productions */
2343 static tree cp_parser_objc_message_receiver
2344 (cp_parser *);
2345 static tree cp_parser_objc_message_args
2346 (cp_parser *);
2347 static tree cp_parser_objc_message_expression
2348 (cp_parser *);
2349 static tree cp_parser_objc_encode_expression
2350 (cp_parser *);
2351 static tree cp_parser_objc_defs_expression
2352 (cp_parser *);
2353 static tree cp_parser_objc_protocol_expression
2354 (cp_parser *);
2355 static tree cp_parser_objc_selector_expression
2356 (cp_parser *);
2357 static tree cp_parser_objc_expression
2358 (cp_parser *);
2359 static bool cp_parser_objc_selector_p
2360 (enum cpp_ttype);
2361 static tree cp_parser_objc_selector
2362 (cp_parser *);
2363 static tree cp_parser_objc_protocol_refs_opt
2364 (cp_parser *);
2365 static void cp_parser_objc_declaration
2366 (cp_parser *, tree);
2367 static tree cp_parser_objc_statement
2368 (cp_parser *);
2369 static bool cp_parser_objc_valid_prefix_attributes
2370 (cp_parser *, tree *);
2371 static void cp_parser_objc_at_property_declaration
2372 (cp_parser *) ;
2373 static void cp_parser_objc_at_synthesize_declaration
2374 (cp_parser *) ;
2375 static void cp_parser_objc_at_dynamic_declaration
2376 (cp_parser *) ;
2377 static tree cp_parser_objc_struct_declaration
2378 (cp_parser *) ;
2380 /* Utility Routines */
2382 static tree cp_parser_lookup_name
2383 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2384 static tree cp_parser_lookup_name_simple
2385 (cp_parser *, tree, location_t);
2386 static tree cp_parser_maybe_treat_template_as_class
2387 (tree, bool);
2388 static bool cp_parser_check_declarator_template_parameters
2389 (cp_parser *, cp_declarator *, location_t);
2390 static bool cp_parser_check_template_parameters
2391 (cp_parser *, unsigned, location_t, cp_declarator *);
2392 static tree cp_parser_simple_cast_expression
2393 (cp_parser *);
2394 static tree cp_parser_global_scope_opt
2395 (cp_parser *, bool);
2396 static bool cp_parser_constructor_declarator_p
2397 (cp_parser *, bool);
2398 static tree cp_parser_function_definition_from_specifiers_and_declarator
2399 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2400 static tree cp_parser_function_definition_after_declarator
2401 (cp_parser *, bool);
2402 static void cp_parser_template_declaration_after_export
2403 (cp_parser *, bool);
2404 static void cp_parser_perform_template_parameter_access_checks
2405 (vec<deferred_access_check, va_gc> *);
2406 static tree cp_parser_single_declaration
2407 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2408 static tree cp_parser_functional_cast
2409 (cp_parser *, tree);
2410 static tree cp_parser_save_member_function_body
2411 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2412 static tree cp_parser_save_nsdmi
2413 (cp_parser *);
2414 static tree cp_parser_enclosed_template_argument_list
2415 (cp_parser *);
2416 static void cp_parser_save_default_args
2417 (cp_parser *, tree);
2418 static void cp_parser_late_parsing_for_member
2419 (cp_parser *, tree);
2420 static tree cp_parser_late_parse_one_default_arg
2421 (cp_parser *, tree, tree, tree);
2422 static void cp_parser_late_parsing_nsdmi
2423 (cp_parser *, tree);
2424 static void cp_parser_late_parsing_default_args
2425 (cp_parser *, tree);
2426 static tree cp_parser_sizeof_operand
2427 (cp_parser *, enum rid);
2428 static tree cp_parser_trait_expr
2429 (cp_parser *, enum rid);
2430 static bool cp_parser_declares_only_class_p
2431 (cp_parser *);
2432 static void cp_parser_set_storage_class
2433 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2434 static void cp_parser_set_decl_spec_type
2435 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2436 static void set_and_check_decl_spec_loc
2437 (cp_decl_specifier_seq *decl_specs,
2438 cp_decl_spec ds, cp_token *);
2439 static bool cp_parser_friend_p
2440 (const cp_decl_specifier_seq *);
2441 static void cp_parser_required_error
2442 (cp_parser *, required_token, bool);
2443 static cp_token *cp_parser_require
2444 (cp_parser *, enum cpp_ttype, required_token);
2445 static cp_token *cp_parser_require_keyword
2446 (cp_parser *, enum rid, required_token);
2447 static bool cp_parser_token_starts_function_definition_p
2448 (cp_token *);
2449 static bool cp_parser_next_token_starts_class_definition_p
2450 (cp_parser *);
2451 static bool cp_parser_next_token_ends_template_argument_p
2452 (cp_parser *);
2453 static bool cp_parser_nth_token_starts_template_argument_list_p
2454 (cp_parser *, size_t);
2455 static enum tag_types cp_parser_token_is_class_key
2456 (cp_token *);
2457 static enum tag_types cp_parser_token_is_type_parameter_key
2458 (cp_token *);
2459 static void cp_parser_check_class_key
2460 (enum tag_types, tree type);
2461 static void cp_parser_check_access_in_redeclaration
2462 (tree type, location_t location);
2463 static bool cp_parser_optional_template_keyword
2464 (cp_parser *);
2465 static void cp_parser_pre_parsed_nested_name_specifier
2466 (cp_parser *);
2467 static bool cp_parser_cache_group
2468 (cp_parser *, enum cpp_ttype, unsigned);
2469 static tree cp_parser_cache_defarg
2470 (cp_parser *parser, bool nsdmi);
2471 static void cp_parser_parse_tentatively
2472 (cp_parser *);
2473 static void cp_parser_commit_to_tentative_parse
2474 (cp_parser *);
2475 static void cp_parser_commit_to_topmost_tentative_parse
2476 (cp_parser *);
2477 static void cp_parser_abort_tentative_parse
2478 (cp_parser *);
2479 static bool cp_parser_parse_definitely
2480 (cp_parser *);
2481 static inline bool cp_parser_parsing_tentatively
2482 (cp_parser *);
2483 static bool cp_parser_uncommitted_to_tentative_parse_p
2484 (cp_parser *);
2485 static void cp_parser_error
2486 (cp_parser *, const char *);
2487 static void cp_parser_name_lookup_error
2488 (cp_parser *, tree, tree, name_lookup_error, location_t);
2489 static bool cp_parser_simulate_error
2490 (cp_parser *);
2491 static bool cp_parser_check_type_definition
2492 (cp_parser *);
2493 static void cp_parser_check_for_definition_in_return_type
2494 (cp_declarator *, tree, location_t type_location);
2495 static void cp_parser_check_for_invalid_template_id
2496 (cp_parser *, tree, enum tag_types, location_t location);
2497 static bool cp_parser_non_integral_constant_expression
2498 (cp_parser *, non_integral_constant);
2499 static void cp_parser_diagnose_invalid_type_name
2500 (cp_parser *, tree, location_t);
2501 static bool cp_parser_parse_and_diagnose_invalid_type_name
2502 (cp_parser *);
2503 static int cp_parser_skip_to_closing_parenthesis
2504 (cp_parser *, bool, bool, bool);
2505 static void cp_parser_skip_to_end_of_statement
2506 (cp_parser *);
2507 static void cp_parser_consume_semicolon_at_end_of_statement
2508 (cp_parser *);
2509 static void cp_parser_skip_to_end_of_block_or_statement
2510 (cp_parser *);
2511 static bool cp_parser_skip_to_closing_brace
2512 (cp_parser *);
2513 static void cp_parser_skip_to_end_of_template_parameter_list
2514 (cp_parser *);
2515 static void cp_parser_skip_to_pragma_eol
2516 (cp_parser*, cp_token *);
2517 static bool cp_parser_error_occurred
2518 (cp_parser *);
2519 static bool cp_parser_allow_gnu_extensions_p
2520 (cp_parser *);
2521 static bool cp_parser_is_pure_string_literal
2522 (cp_token *);
2523 static bool cp_parser_is_string_literal
2524 (cp_token *);
2525 static bool cp_parser_is_keyword
2526 (cp_token *, enum rid);
2527 static tree cp_parser_make_typename_type
2528 (cp_parser *, tree, location_t location);
2529 static cp_declarator * cp_parser_make_indirect_declarator
2530 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2531 static bool cp_parser_compound_literal_p
2532 (cp_parser *);
2533 static bool cp_parser_array_designator_p
2534 (cp_parser *);
2535 static bool cp_parser_skip_to_closing_square_bracket
2536 (cp_parser *);
2538 /* Returns nonzero if we are parsing tentatively. */
2540 static inline bool
2541 cp_parser_parsing_tentatively (cp_parser* parser)
2543 return parser->context->next != NULL;
2546 /* Returns nonzero if TOKEN is a string literal. */
2548 static bool
2549 cp_parser_is_pure_string_literal (cp_token* token)
2551 return (token->type == CPP_STRING ||
2552 token->type == CPP_STRING16 ||
2553 token->type == CPP_STRING32 ||
2554 token->type == CPP_WSTRING ||
2555 token->type == CPP_UTF8STRING);
2558 /* Returns nonzero if TOKEN is a string literal
2559 of a user-defined string literal. */
2561 static bool
2562 cp_parser_is_string_literal (cp_token* token)
2564 return (cp_parser_is_pure_string_literal (token) ||
2565 token->type == CPP_STRING_USERDEF ||
2566 token->type == CPP_STRING16_USERDEF ||
2567 token->type == CPP_STRING32_USERDEF ||
2568 token->type == CPP_WSTRING_USERDEF ||
2569 token->type == CPP_UTF8STRING_USERDEF);
2572 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2574 static bool
2575 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2577 return token->keyword == keyword;
2580 /* If not parsing tentatively, issue a diagnostic of the form
2581 FILE:LINE: MESSAGE before TOKEN
2582 where TOKEN is the next token in the input stream. MESSAGE
2583 (specified by the caller) is usually of the form "expected
2584 OTHER-TOKEN". */
2586 static void
2587 cp_parser_error (cp_parser* parser, const char* gmsgid)
2589 if (!cp_parser_simulate_error (parser))
2591 cp_token *token = cp_lexer_peek_token (parser->lexer);
2592 /* This diagnostic makes more sense if it is tagged to the line
2593 of the token we just peeked at. */
2594 cp_lexer_set_source_position_from_token (token);
2596 if (token->type == CPP_PRAGMA)
2598 error_at (token->location,
2599 "%<#pragma%> is not allowed here");
2600 cp_parser_skip_to_pragma_eol (parser, token);
2601 return;
2604 c_parse_error (gmsgid,
2605 /* Because c_parser_error does not understand
2606 CPP_KEYWORD, keywords are treated like
2607 identifiers. */
2608 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2609 token->u.value, token->flags);
2613 /* Issue an error about name-lookup failing. NAME is the
2614 IDENTIFIER_NODE DECL is the result of
2615 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2616 the thing that we hoped to find. */
2618 static void
2619 cp_parser_name_lookup_error (cp_parser* parser,
2620 tree name,
2621 tree decl,
2622 name_lookup_error desired,
2623 location_t location)
2625 /* If name lookup completely failed, tell the user that NAME was not
2626 declared. */
2627 if (decl == error_mark_node)
2629 if (parser->scope && parser->scope != global_namespace)
2630 error_at (location, "%<%E::%E%> has not been declared",
2631 parser->scope, name);
2632 else if (parser->scope == global_namespace)
2633 error_at (location, "%<::%E%> has not been declared", name);
2634 else if (parser->object_scope
2635 && !CLASS_TYPE_P (parser->object_scope))
2636 error_at (location, "request for member %qE in non-class type %qT",
2637 name, parser->object_scope);
2638 else if (parser->object_scope)
2639 error_at (location, "%<%T::%E%> has not been declared",
2640 parser->object_scope, name);
2641 else
2642 error_at (location, "%qE has not been declared", name);
2644 else if (parser->scope && parser->scope != global_namespace)
2646 switch (desired)
2648 case NLE_TYPE:
2649 error_at (location, "%<%E::%E%> is not a type",
2650 parser->scope, name);
2651 break;
2652 case NLE_CXX98:
2653 error_at (location, "%<%E::%E%> is not a class or namespace",
2654 parser->scope, name);
2655 break;
2656 case NLE_NOT_CXX98:
2657 error_at (location,
2658 "%<%E::%E%> is not a class, namespace, or enumeration",
2659 parser->scope, name);
2660 break;
2661 default:
2662 gcc_unreachable ();
2666 else if (parser->scope == global_namespace)
2668 switch (desired)
2670 case NLE_TYPE:
2671 error_at (location, "%<::%E%> is not a type", name);
2672 break;
2673 case NLE_CXX98:
2674 error_at (location, "%<::%E%> is not a class or namespace", name);
2675 break;
2676 case NLE_NOT_CXX98:
2677 error_at (location,
2678 "%<::%E%> is not a class, namespace, or enumeration",
2679 name);
2680 break;
2681 default:
2682 gcc_unreachable ();
2685 else
2687 switch (desired)
2689 case NLE_TYPE:
2690 error_at (location, "%qE is not a type", name);
2691 break;
2692 case NLE_CXX98:
2693 error_at (location, "%qE is not a class or namespace", name);
2694 break;
2695 case NLE_NOT_CXX98:
2696 error_at (location,
2697 "%qE is not a class, namespace, or enumeration", name);
2698 break;
2699 default:
2700 gcc_unreachable ();
2705 /* If we are parsing tentatively, remember that an error has occurred
2706 during this tentative parse. Returns true if the error was
2707 simulated; false if a message should be issued by the caller. */
2709 static bool
2710 cp_parser_simulate_error (cp_parser* parser)
2712 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2714 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2715 return true;
2717 return false;
2720 /* This function is called when a type is defined. If type
2721 definitions are forbidden at this point, an error message is
2722 issued. */
2724 static bool
2725 cp_parser_check_type_definition (cp_parser* parser)
2727 /* If types are forbidden here, issue a message. */
2728 if (parser->type_definition_forbidden_message)
2730 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2731 in the message need to be interpreted. */
2732 error (parser->type_definition_forbidden_message);
2733 return false;
2735 return true;
2738 /* This function is called when the DECLARATOR is processed. The TYPE
2739 was a type defined in the decl-specifiers. If it is invalid to
2740 define a type in the decl-specifiers for DECLARATOR, an error is
2741 issued. TYPE_LOCATION is the location of TYPE and is used
2742 for error reporting. */
2744 static void
2745 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2746 tree type, location_t type_location)
2748 /* [dcl.fct] forbids type definitions in return types.
2749 Unfortunately, it's not easy to know whether or not we are
2750 processing a return type until after the fact. */
2751 while (declarator
2752 && (declarator->kind == cdk_pointer
2753 || declarator->kind == cdk_reference
2754 || declarator->kind == cdk_ptrmem))
2755 declarator = declarator->declarator;
2756 if (declarator
2757 && declarator->kind == cdk_function)
2759 error_at (type_location,
2760 "new types may not be defined in a return type");
2761 inform (type_location,
2762 "(perhaps a semicolon is missing after the definition of %qT)",
2763 type);
2767 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2768 "<" in any valid C++ program. If the next token is indeed "<",
2769 issue a message warning the user about what appears to be an
2770 invalid attempt to form a template-id. LOCATION is the location
2771 of the type-specifier (TYPE) */
2773 static void
2774 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2775 tree type,
2776 enum tag_types tag_type,
2777 location_t location)
2779 cp_token_position start = 0;
2781 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2783 if (TYPE_P (type))
2784 error_at (location, "%qT is not a template", type);
2785 else if (identifier_p (type))
2787 if (tag_type != none_type)
2788 error_at (location, "%qE is not a class template", type);
2789 else
2790 error_at (location, "%qE is not a template", type);
2792 else
2793 error_at (location, "invalid template-id");
2794 /* Remember the location of the invalid "<". */
2795 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2796 start = cp_lexer_token_position (parser->lexer, true);
2797 /* Consume the "<". */
2798 cp_lexer_consume_token (parser->lexer);
2799 /* Parse the template arguments. */
2800 cp_parser_enclosed_template_argument_list (parser);
2801 /* Permanently remove the invalid template arguments so that
2802 this error message is not issued again. */
2803 if (start)
2804 cp_lexer_purge_tokens_after (parser->lexer, start);
2808 /* If parsing an integral constant-expression, issue an error message
2809 about the fact that THING appeared and return true. Otherwise,
2810 return false. In either case, set
2811 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2813 static bool
2814 cp_parser_non_integral_constant_expression (cp_parser *parser,
2815 non_integral_constant thing)
2817 parser->non_integral_constant_expression_p = true;
2818 if (parser->integral_constant_expression_p)
2820 if (!parser->allow_non_integral_constant_expression_p)
2822 const char *msg = NULL;
2823 switch (thing)
2825 case NIC_FLOAT:
2826 error ("floating-point literal "
2827 "cannot appear in a constant-expression");
2828 return true;
2829 case NIC_CAST:
2830 error ("a cast to a type other than an integral or "
2831 "enumeration type cannot appear in a "
2832 "constant-expression");
2833 return true;
2834 case NIC_TYPEID:
2835 error ("%<typeid%> operator "
2836 "cannot appear in a constant-expression");
2837 return true;
2838 case NIC_NCC:
2839 error ("non-constant compound literals "
2840 "cannot appear in a constant-expression");
2841 return true;
2842 case NIC_FUNC_CALL:
2843 error ("a function call "
2844 "cannot appear in a constant-expression");
2845 return true;
2846 case NIC_INC:
2847 error ("an increment "
2848 "cannot appear in a constant-expression");
2849 return true;
2850 case NIC_DEC:
2851 error ("an decrement "
2852 "cannot appear in a constant-expression");
2853 return true;
2854 case NIC_ARRAY_REF:
2855 error ("an array reference "
2856 "cannot appear in a constant-expression");
2857 return true;
2858 case NIC_ADDR_LABEL:
2859 error ("the address of a label "
2860 "cannot appear in a constant-expression");
2861 return true;
2862 case NIC_OVERLOADED:
2863 error ("calls to overloaded operators "
2864 "cannot appear in a constant-expression");
2865 return true;
2866 case NIC_ASSIGNMENT:
2867 error ("an assignment cannot appear in a constant-expression");
2868 return true;
2869 case NIC_COMMA:
2870 error ("a comma operator "
2871 "cannot appear in a constant-expression");
2872 return true;
2873 case NIC_CONSTRUCTOR:
2874 error ("a call to a constructor "
2875 "cannot appear in a constant-expression");
2876 return true;
2877 case NIC_TRANSACTION:
2878 error ("a transaction expression "
2879 "cannot appear in a constant-expression");
2880 return true;
2881 case NIC_THIS:
2882 msg = "this";
2883 break;
2884 case NIC_FUNC_NAME:
2885 msg = "__FUNCTION__";
2886 break;
2887 case NIC_PRETTY_FUNC:
2888 msg = "__PRETTY_FUNCTION__";
2889 break;
2890 case NIC_C99_FUNC:
2891 msg = "__func__";
2892 break;
2893 case NIC_VA_ARG:
2894 msg = "va_arg";
2895 break;
2896 case NIC_ARROW:
2897 msg = "->";
2898 break;
2899 case NIC_POINT:
2900 msg = ".";
2901 break;
2902 case NIC_STAR:
2903 msg = "*";
2904 break;
2905 case NIC_ADDR:
2906 msg = "&";
2907 break;
2908 case NIC_PREINCREMENT:
2909 msg = "++";
2910 break;
2911 case NIC_PREDECREMENT:
2912 msg = "--";
2913 break;
2914 case NIC_NEW:
2915 msg = "new";
2916 break;
2917 case NIC_DEL:
2918 msg = "delete";
2919 break;
2920 default:
2921 gcc_unreachable ();
2923 if (msg)
2924 error ("%qs cannot appear in a constant-expression", msg);
2925 return true;
2928 return false;
2931 /* Emit a diagnostic for an invalid type name. This function commits
2932 to the current active tentative parse, if any. (Otherwise, the
2933 problematic construct might be encountered again later, resulting
2934 in duplicate error messages.) LOCATION is the location of ID. */
2936 static void
2937 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2938 location_t location)
2940 tree decl, ambiguous_decls;
2941 cp_parser_commit_to_tentative_parse (parser);
2942 /* Try to lookup the identifier. */
2943 decl = cp_parser_lookup_name (parser, id, none_type,
2944 /*is_template=*/false,
2945 /*is_namespace=*/false,
2946 /*check_dependency=*/true,
2947 &ambiguous_decls, location);
2948 if (ambiguous_decls)
2949 /* If the lookup was ambiguous, an error will already have
2950 been issued. */
2951 return;
2952 /* If the lookup found a template-name, it means that the user forgot
2953 to specify an argument list. Emit a useful error message. */
2954 if (TREE_CODE (decl) == TEMPLATE_DECL)
2955 error_at (location,
2956 "invalid use of template-name %qE without an argument list",
2957 decl);
2958 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2959 error_at (location, "invalid use of destructor %qD as a type", id);
2960 else if (TREE_CODE (decl) == TYPE_DECL)
2961 /* Something like 'unsigned A a;' */
2962 error_at (location, "invalid combination of multiple type-specifiers");
2963 else if (!parser->scope)
2965 /* Issue an error message. */
2966 error_at (location, "%qE does not name a type", id);
2967 /* If we're in a template class, it's possible that the user was
2968 referring to a type from a base class. For example:
2970 template <typename T> struct A { typedef T X; };
2971 template <typename T> struct B : public A<T> { X x; };
2973 The user should have said "typename A<T>::X". */
2974 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2975 inform (location, "C++11 %<constexpr%> only available with "
2976 "-std=c++11 or -std=gnu++11");
2977 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2978 inform (location, "C++11 %<noexcept%> only available with "
2979 "-std=c++11 or -std=gnu++11");
2980 else if (cxx_dialect < cxx11
2981 && TREE_CODE (id) == IDENTIFIER_NODE
2982 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2983 inform (location, "C++11 %<thread_local%> only available with "
2984 "-std=c++11 or -std=gnu++11");
2985 else if (processing_template_decl && current_class_type
2986 && TYPE_BINFO (current_class_type))
2988 tree b;
2990 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2992 b = TREE_CHAIN (b))
2994 tree base_type = BINFO_TYPE (b);
2995 if (CLASS_TYPE_P (base_type)
2996 && dependent_type_p (base_type))
2998 tree field;
2999 /* Go from a particular instantiation of the
3000 template (which will have an empty TYPE_FIELDs),
3001 to the main version. */
3002 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3003 for (field = TYPE_FIELDS (base_type);
3004 field;
3005 field = DECL_CHAIN (field))
3006 if (TREE_CODE (field) == TYPE_DECL
3007 && DECL_NAME (field) == id)
3009 inform (location,
3010 "(perhaps %<typename %T::%E%> was intended)",
3011 BINFO_TYPE (b), id);
3012 break;
3014 if (field)
3015 break;
3020 /* Here we diagnose qualified-ids where the scope is actually correct,
3021 but the identifier does not resolve to a valid type name. */
3022 else if (parser->scope != error_mark_node)
3024 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3026 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3027 error_at (location_of (id),
3028 "%qE in namespace %qE does not name a template type",
3029 id, parser->scope);
3030 else
3031 error_at (location_of (id),
3032 "%qE in namespace %qE does not name a type",
3033 id, parser->scope);
3035 else if (CLASS_TYPE_P (parser->scope)
3036 && constructor_name_p (id, parser->scope))
3038 /* A<T>::A<T>() */
3039 error_at (location, "%<%T::%E%> names the constructor, not"
3040 " the type", parser->scope, id);
3041 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3042 error_at (location, "and %qT has no template constructors",
3043 parser->scope);
3045 else if (TYPE_P (parser->scope)
3046 && dependent_scope_p (parser->scope))
3047 error_at (location, "need %<typename%> before %<%T::%E%> because "
3048 "%qT is a dependent scope",
3049 parser->scope, id, parser->scope);
3050 else if (TYPE_P (parser->scope))
3052 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3053 error_at (location_of (id),
3054 "%qE in %q#T does not name a template type",
3055 id, parser->scope);
3056 else
3057 error_at (location_of (id),
3058 "%qE in %q#T does not name a type",
3059 id, parser->scope);
3061 else
3062 gcc_unreachable ();
3066 /* Check for a common situation where a type-name should be present,
3067 but is not, and issue a sensible error message. Returns true if an
3068 invalid type-name was detected.
3070 The situation handled by this function are variable declarations of the
3071 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3072 Usually, `ID' should name a type, but if we got here it means that it
3073 does not. We try to emit the best possible error message depending on
3074 how exactly the id-expression looks like. */
3076 static bool
3077 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3079 tree id;
3080 cp_token *token = cp_lexer_peek_token (parser->lexer);
3082 /* Avoid duplicate error about ambiguous lookup. */
3083 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3085 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3086 if (next->type == CPP_NAME && next->error_reported)
3087 goto out;
3090 cp_parser_parse_tentatively (parser);
3091 id = cp_parser_id_expression (parser,
3092 /*template_keyword_p=*/false,
3093 /*check_dependency_p=*/true,
3094 /*template_p=*/NULL,
3095 /*declarator_p=*/true,
3096 /*optional_p=*/false);
3097 /* If the next token is a (, this is a function with no explicit return
3098 type, i.e. constructor, destructor or conversion op. */
3099 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3100 || TREE_CODE (id) == TYPE_DECL)
3102 cp_parser_abort_tentative_parse (parser);
3103 return false;
3105 if (!cp_parser_parse_definitely (parser))
3106 return false;
3108 /* Emit a diagnostic for the invalid type. */
3109 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3110 out:
3111 /* If we aren't in the middle of a declarator (i.e. in a
3112 parameter-declaration-clause), skip to the end of the declaration;
3113 there's no point in trying to process it. */
3114 if (!parser->in_declarator_p)
3115 cp_parser_skip_to_end_of_block_or_statement (parser);
3116 return true;
3119 /* Consume tokens up to, and including, the next non-nested closing `)'.
3120 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3121 are doing error recovery. Returns -1 if OR_COMMA is true and we
3122 found an unnested comma. */
3124 static int
3125 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3126 bool recovering,
3127 bool or_comma,
3128 bool consume_paren)
3130 unsigned paren_depth = 0;
3131 unsigned brace_depth = 0;
3132 unsigned square_depth = 0;
3134 if (recovering && !or_comma
3135 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3136 return 0;
3138 while (true)
3140 cp_token * token = cp_lexer_peek_token (parser->lexer);
3142 switch (token->type)
3144 case CPP_EOF:
3145 case CPP_PRAGMA_EOL:
3146 /* If we've run out of tokens, then there is no closing `)'. */
3147 return 0;
3149 /* This is good for lambda expression capture-lists. */
3150 case CPP_OPEN_SQUARE:
3151 ++square_depth;
3152 break;
3153 case CPP_CLOSE_SQUARE:
3154 if (!square_depth--)
3155 return 0;
3156 break;
3158 case CPP_SEMICOLON:
3159 /* This matches the processing in skip_to_end_of_statement. */
3160 if (!brace_depth)
3161 return 0;
3162 break;
3164 case CPP_OPEN_BRACE:
3165 ++brace_depth;
3166 break;
3167 case CPP_CLOSE_BRACE:
3168 if (!brace_depth--)
3169 return 0;
3170 break;
3172 case CPP_COMMA:
3173 if (recovering && or_comma && !brace_depth && !paren_depth
3174 && !square_depth)
3175 return -1;
3176 break;
3178 case CPP_OPEN_PAREN:
3179 if (!brace_depth)
3180 ++paren_depth;
3181 break;
3183 case CPP_CLOSE_PAREN:
3184 if (!brace_depth && !paren_depth--)
3186 if (consume_paren)
3187 cp_lexer_consume_token (parser->lexer);
3188 return 1;
3190 break;
3192 default:
3193 break;
3196 /* Consume the token. */
3197 cp_lexer_consume_token (parser->lexer);
3201 /* Consume tokens until we reach the end of the current statement.
3202 Normally, that will be just before consuming a `;'. However, if a
3203 non-nested `}' comes first, then we stop before consuming that. */
3205 static void
3206 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3208 unsigned nesting_depth = 0;
3210 /* Unwind generic function template scope if necessary. */
3211 if (parser->fully_implicit_function_template_p)
3212 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3214 while (true)
3216 cp_token *token = cp_lexer_peek_token (parser->lexer);
3218 switch (token->type)
3220 case CPP_EOF:
3221 case CPP_PRAGMA_EOL:
3222 /* If we've run out of tokens, stop. */
3223 return;
3225 case CPP_SEMICOLON:
3226 /* If the next token is a `;', we have reached the end of the
3227 statement. */
3228 if (!nesting_depth)
3229 return;
3230 break;
3232 case CPP_CLOSE_BRACE:
3233 /* If this is a non-nested '}', stop before consuming it.
3234 That way, when confronted with something like:
3236 { 3 + }
3238 we stop before consuming the closing '}', even though we
3239 have not yet reached a `;'. */
3240 if (nesting_depth == 0)
3241 return;
3243 /* If it is the closing '}' for a block that we have
3244 scanned, stop -- but only after consuming the token.
3245 That way given:
3247 void f g () { ... }
3248 typedef int I;
3250 we will stop after the body of the erroneously declared
3251 function, but before consuming the following `typedef'
3252 declaration. */
3253 if (--nesting_depth == 0)
3255 cp_lexer_consume_token (parser->lexer);
3256 return;
3259 case CPP_OPEN_BRACE:
3260 ++nesting_depth;
3261 break;
3263 default:
3264 break;
3267 /* Consume the token. */
3268 cp_lexer_consume_token (parser->lexer);
3272 /* This function is called at the end of a statement or declaration.
3273 If the next token is a semicolon, it is consumed; otherwise, error
3274 recovery is attempted. */
3276 static void
3277 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3279 /* Look for the trailing `;'. */
3280 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3282 /* If there is additional (erroneous) input, skip to the end of
3283 the statement. */
3284 cp_parser_skip_to_end_of_statement (parser);
3285 /* If the next token is now a `;', consume it. */
3286 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3287 cp_lexer_consume_token (parser->lexer);
3291 /* Skip tokens until we have consumed an entire block, or until we
3292 have consumed a non-nested `;'. */
3294 static void
3295 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3297 int nesting_depth = 0;
3299 /* Unwind generic function template scope if necessary. */
3300 if (parser->fully_implicit_function_template_p)
3301 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3303 while (nesting_depth >= 0)
3305 cp_token *token = cp_lexer_peek_token (parser->lexer);
3307 switch (token->type)
3309 case CPP_EOF:
3310 case CPP_PRAGMA_EOL:
3311 /* If we've run out of tokens, stop. */
3312 return;
3314 case CPP_SEMICOLON:
3315 /* Stop if this is an unnested ';'. */
3316 if (!nesting_depth)
3317 nesting_depth = -1;
3318 break;
3320 case CPP_CLOSE_BRACE:
3321 /* Stop if this is an unnested '}', or closes the outermost
3322 nesting level. */
3323 nesting_depth--;
3324 if (nesting_depth < 0)
3325 return;
3326 if (!nesting_depth)
3327 nesting_depth = -1;
3328 break;
3330 case CPP_OPEN_BRACE:
3331 /* Nest. */
3332 nesting_depth++;
3333 break;
3335 default:
3336 break;
3339 /* Consume the token. */
3340 cp_lexer_consume_token (parser->lexer);
3344 /* Skip tokens until a non-nested closing curly brace is the next
3345 token, or there are no more tokens. Return true in the first case,
3346 false otherwise. */
3348 static bool
3349 cp_parser_skip_to_closing_brace (cp_parser *parser)
3351 unsigned nesting_depth = 0;
3353 while (true)
3355 cp_token *token = cp_lexer_peek_token (parser->lexer);
3357 switch (token->type)
3359 case CPP_EOF:
3360 case CPP_PRAGMA_EOL:
3361 /* If we've run out of tokens, stop. */
3362 return false;
3364 case CPP_CLOSE_BRACE:
3365 /* If the next token is a non-nested `}', then we have reached
3366 the end of the current block. */
3367 if (nesting_depth-- == 0)
3368 return true;
3369 break;
3371 case CPP_OPEN_BRACE:
3372 /* If it the next token is a `{', then we are entering a new
3373 block. Consume the entire block. */
3374 ++nesting_depth;
3375 break;
3377 default:
3378 break;
3381 /* Consume the token. */
3382 cp_lexer_consume_token (parser->lexer);
3386 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3387 parameter is the PRAGMA token, allowing us to purge the entire pragma
3388 sequence. */
3390 static void
3391 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3393 cp_token *token;
3395 parser->lexer->in_pragma = false;
3398 token = cp_lexer_consume_token (parser->lexer);
3399 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3401 /* Ensure that the pragma is not parsed again. */
3402 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3405 /* Require pragma end of line, resyncing with it as necessary. The
3406 arguments are as for cp_parser_skip_to_pragma_eol. */
3408 static void
3409 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3411 parser->lexer->in_pragma = false;
3412 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3413 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3416 /* This is a simple wrapper around make_typename_type. When the id is
3417 an unresolved identifier node, we can provide a superior diagnostic
3418 using cp_parser_diagnose_invalid_type_name. */
3420 static tree
3421 cp_parser_make_typename_type (cp_parser *parser, tree id,
3422 location_t id_location)
3424 tree result;
3425 if (identifier_p (id))
3427 result = make_typename_type (parser->scope, id, typename_type,
3428 /*complain=*/tf_none);
3429 if (result == error_mark_node)
3430 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3431 return result;
3433 return make_typename_type (parser->scope, id, typename_type, tf_error);
3436 /* This is a wrapper around the
3437 make_{pointer,ptrmem,reference}_declarator functions that decides
3438 which one to call based on the CODE and CLASS_TYPE arguments. The
3439 CODE argument should be one of the values returned by
3440 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3441 appertain to the pointer or reference. */
3443 static cp_declarator *
3444 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3445 cp_cv_quals cv_qualifiers,
3446 cp_declarator *target,
3447 tree attributes)
3449 if (code == ERROR_MARK)
3450 return cp_error_declarator;
3452 if (code == INDIRECT_REF)
3453 if (class_type == NULL_TREE)
3454 return make_pointer_declarator (cv_qualifiers, target, attributes);
3455 else
3456 return make_ptrmem_declarator (cv_qualifiers, class_type,
3457 target, attributes);
3458 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3459 return make_reference_declarator (cv_qualifiers, target,
3460 false, attributes);
3461 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3462 return make_reference_declarator (cv_qualifiers, target,
3463 true, attributes);
3464 gcc_unreachable ();
3467 /* Create a new C++ parser. */
3469 static cp_parser *
3470 cp_parser_new (void)
3472 cp_parser *parser;
3473 cp_lexer *lexer;
3474 unsigned i;
3476 /* cp_lexer_new_main is called before doing GC allocation because
3477 cp_lexer_new_main might load a PCH file. */
3478 lexer = cp_lexer_new_main ();
3480 /* Initialize the binops_by_token so that we can get the tree
3481 directly from the token. */
3482 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3483 binops_by_token[binops[i].token_type] = binops[i];
3485 parser = ggc_cleared_alloc<cp_parser> ();
3486 parser->lexer = lexer;
3487 parser->context = cp_parser_context_new (NULL);
3489 /* For now, we always accept GNU extensions. */
3490 parser->allow_gnu_extensions_p = 1;
3492 /* The `>' token is a greater-than operator, not the end of a
3493 template-id. */
3494 parser->greater_than_is_operator_p = true;
3496 parser->default_arg_ok_p = true;
3498 /* We are not parsing a constant-expression. */
3499 parser->integral_constant_expression_p = false;
3500 parser->allow_non_integral_constant_expression_p = false;
3501 parser->non_integral_constant_expression_p = false;
3503 /* Local variable names are not forbidden. */
3504 parser->local_variables_forbidden_p = false;
3506 /* We are not processing an `extern "C"' declaration. */
3507 parser->in_unbraced_linkage_specification_p = false;
3509 /* We are not processing a declarator. */
3510 parser->in_declarator_p = false;
3512 /* We are not processing a template-argument-list. */
3513 parser->in_template_argument_list_p = false;
3515 /* We are not in an iteration statement. */
3516 parser->in_statement = 0;
3518 /* We are not in a switch statement. */
3519 parser->in_switch_statement_p = false;
3521 /* We are not parsing a type-id inside an expression. */
3522 parser->in_type_id_in_expr_p = false;
3524 /* Declarations aren't implicitly extern "C". */
3525 parser->implicit_extern_c = false;
3527 /* String literals should be translated to the execution character set. */
3528 parser->translate_strings_p = true;
3530 /* We are not parsing a function body. */
3531 parser->in_function_body = false;
3533 /* We can correct until told otherwise. */
3534 parser->colon_corrects_to_scope_p = true;
3536 /* The unparsed function queue is empty. */
3537 push_unparsed_function_queues (parser);
3539 /* There are no classes being defined. */
3540 parser->num_classes_being_defined = 0;
3542 /* No template parameters apply. */
3543 parser->num_template_parameter_lists = 0;
3545 /* Not declaring an implicit function template. */
3546 parser->auto_is_implicit_function_template_parm_p = false;
3547 parser->fully_implicit_function_template_p = false;
3548 parser->implicit_template_parms = 0;
3549 parser->implicit_template_scope = 0;
3551 return parser;
3554 /* Create a cp_lexer structure which will emit the tokens in CACHE
3555 and push it onto the parser's lexer stack. This is used for delayed
3556 parsing of in-class method bodies and default arguments, and should
3557 not be confused with tentative parsing. */
3558 static void
3559 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3561 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3562 lexer->next = parser->lexer;
3563 parser->lexer = lexer;
3565 /* Move the current source position to that of the first token in the
3566 new lexer. */
3567 cp_lexer_set_source_position_from_token (lexer->next_token);
3570 /* Pop the top lexer off the parser stack. This is never used for the
3571 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3572 static void
3573 cp_parser_pop_lexer (cp_parser *parser)
3575 cp_lexer *lexer = parser->lexer;
3576 parser->lexer = lexer->next;
3577 cp_lexer_destroy (lexer);
3579 /* Put the current source position back where it was before this
3580 lexer was pushed. */
3581 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3584 /* Lexical conventions [gram.lex] */
3586 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3587 identifier. */
3589 static tree
3590 cp_parser_identifier (cp_parser* parser)
3592 cp_token *token;
3594 /* Look for the identifier. */
3595 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3596 /* Return the value. */
3597 return token ? token->u.value : error_mark_node;
3600 /* Parse a sequence of adjacent string constants. Returns a
3601 TREE_STRING representing the combined, nul-terminated string
3602 constant. If TRANSLATE is true, translate the string to the
3603 execution character set. If WIDE_OK is true, a wide string is
3604 invalid here.
3606 C++98 [lex.string] says that if a narrow string literal token is
3607 adjacent to a wide string literal token, the behavior is undefined.
3608 However, C99 6.4.5p4 says that this results in a wide string literal.
3609 We follow C99 here, for consistency with the C front end.
3611 This code is largely lifted from lex_string() in c-lex.c.
3613 FUTURE: ObjC++ will need to handle @-strings here. */
3614 static tree
3615 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3616 bool lookup_udlit = true)
3618 tree value;
3619 size_t count;
3620 struct obstack str_ob;
3621 cpp_string str, istr, *strs;
3622 cp_token *tok;
3623 enum cpp_ttype type, curr_type;
3624 int have_suffix_p = 0;
3625 tree string_tree;
3626 tree suffix_id = NULL_TREE;
3627 bool curr_tok_is_userdef_p = false;
3629 tok = cp_lexer_peek_token (parser->lexer);
3630 if (!cp_parser_is_string_literal (tok))
3632 cp_parser_error (parser, "expected string-literal");
3633 return error_mark_node;
3636 if (cpp_userdef_string_p (tok->type))
3638 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3639 curr_type = cpp_userdef_string_remove_type (tok->type);
3640 curr_tok_is_userdef_p = true;
3642 else
3644 string_tree = tok->u.value;
3645 curr_type = tok->type;
3647 type = curr_type;
3649 /* Try to avoid the overhead of creating and destroying an obstack
3650 for the common case of just one string. */
3651 if (!cp_parser_is_string_literal
3652 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3654 cp_lexer_consume_token (parser->lexer);
3656 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3657 str.len = TREE_STRING_LENGTH (string_tree);
3658 count = 1;
3660 if (curr_tok_is_userdef_p)
3662 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3663 have_suffix_p = 1;
3664 curr_type = cpp_userdef_string_remove_type (tok->type);
3666 else
3667 curr_type = tok->type;
3669 strs = &str;
3671 else
3673 gcc_obstack_init (&str_ob);
3674 count = 0;
3678 cp_lexer_consume_token (parser->lexer);
3679 count++;
3680 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3681 str.len = TREE_STRING_LENGTH (string_tree);
3683 if (curr_tok_is_userdef_p)
3685 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3686 if (have_suffix_p == 0)
3688 suffix_id = curr_suffix_id;
3689 have_suffix_p = 1;
3691 else if (have_suffix_p == 1
3692 && curr_suffix_id != suffix_id)
3694 error ("inconsistent user-defined literal suffixes"
3695 " %qD and %qD in string literal",
3696 suffix_id, curr_suffix_id);
3697 have_suffix_p = -1;
3699 curr_type = cpp_userdef_string_remove_type (tok->type);
3701 else
3702 curr_type = tok->type;
3704 if (type != curr_type)
3706 if (type == CPP_STRING)
3707 type = curr_type;
3708 else if (curr_type != CPP_STRING)
3709 error_at (tok->location,
3710 "unsupported non-standard concatenation "
3711 "of string literals");
3714 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3716 tok = cp_lexer_peek_token (parser->lexer);
3717 if (cpp_userdef_string_p (tok->type))
3719 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3720 curr_type = cpp_userdef_string_remove_type (tok->type);
3721 curr_tok_is_userdef_p = true;
3723 else
3725 string_tree = tok->u.value;
3726 curr_type = tok->type;
3727 curr_tok_is_userdef_p = false;
3730 while (cp_parser_is_string_literal (tok));
3732 strs = (cpp_string *) obstack_finish (&str_ob);
3735 if (type != CPP_STRING && !wide_ok)
3737 cp_parser_error (parser, "a wide string is invalid in this context");
3738 type = CPP_STRING;
3741 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3742 (parse_in, strs, count, &istr, type))
3744 value = build_string (istr.len, (const char *)istr.text);
3745 free (CONST_CAST (unsigned char *, istr.text));
3747 switch (type)
3749 default:
3750 case CPP_STRING:
3751 case CPP_UTF8STRING:
3752 TREE_TYPE (value) = char_array_type_node;
3753 break;
3754 case CPP_STRING16:
3755 TREE_TYPE (value) = char16_array_type_node;
3756 break;
3757 case CPP_STRING32:
3758 TREE_TYPE (value) = char32_array_type_node;
3759 break;
3760 case CPP_WSTRING:
3761 TREE_TYPE (value) = wchar_array_type_node;
3762 break;
3765 value = fix_string_type (value);
3767 if (have_suffix_p)
3769 tree literal = build_userdef_literal (suffix_id, value,
3770 OT_NONE, NULL_TREE);
3771 if (lookup_udlit)
3772 value = cp_parser_userdef_string_literal (literal);
3773 else
3774 value = literal;
3777 else
3778 /* cpp_interpret_string has issued an error. */
3779 value = error_mark_node;
3781 if (count > 1)
3782 obstack_free (&str_ob, 0);
3784 return value;
3787 /* Look up a literal operator with the name and the exact arguments. */
3789 static tree
3790 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3792 tree decl, fns;
3793 decl = lookup_name (name);
3794 if (!decl || !is_overloaded_fn (decl))
3795 return error_mark_node;
3797 for (fns = decl; fns; fns = OVL_NEXT (fns))
3799 unsigned int ix;
3800 bool found = true;
3801 tree fn = OVL_CURRENT (fns);
3802 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3803 if (parmtypes != NULL_TREE)
3805 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3806 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3808 tree tparm = TREE_VALUE (parmtypes);
3809 tree targ = TREE_TYPE ((*args)[ix]);
3810 bool ptr = TYPE_PTR_P (tparm);
3811 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3812 if ((ptr || arr || !same_type_p (tparm, targ))
3813 && (!ptr || !arr
3814 || !same_type_p (TREE_TYPE (tparm),
3815 TREE_TYPE (targ))))
3816 found = false;
3818 if (found
3819 && ix == vec_safe_length (args)
3820 /* May be this should be sufficient_parms_p instead,
3821 depending on how exactly should user-defined literals
3822 work in presence of default arguments on the literal
3823 operator parameters. */
3824 && parmtypes == void_list_node)
3825 return fn;
3829 return error_mark_node;
3832 /* Parse a user-defined char constant. Returns a call to a user-defined
3833 literal operator taking the character as an argument. */
3835 static tree
3836 cp_parser_userdef_char_literal (cp_parser *parser)
3838 cp_token *token = cp_lexer_consume_token (parser->lexer);
3839 tree literal = token->u.value;
3840 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3841 tree value = USERDEF_LITERAL_VALUE (literal);
3842 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3843 tree decl, result;
3845 /* Build up a call to the user-defined operator */
3846 /* Lookup the name we got back from the id-expression. */
3847 vec<tree, va_gc> *args = make_tree_vector ();
3848 vec_safe_push (args, value);
3849 decl = lookup_literal_operator (name, args);
3850 if (!decl || decl == error_mark_node)
3852 error ("unable to find character literal operator %qD with %qT argument",
3853 name, TREE_TYPE (value));
3854 release_tree_vector (args);
3855 return error_mark_node;
3857 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3858 release_tree_vector (args);
3859 if (result != error_mark_node)
3860 return result;
3862 error ("unable to find character literal operator %qD with %qT argument",
3863 name, TREE_TYPE (value));
3864 return error_mark_node;
3867 /* A subroutine of cp_parser_userdef_numeric_literal to
3868 create a char... template parameter pack from a string node. */
3870 static tree
3871 make_char_string_pack (tree value)
3873 tree charvec;
3874 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3875 const char *str = TREE_STRING_POINTER (value);
3876 int i, len = TREE_STRING_LENGTH (value) - 1;
3877 tree argvec = make_tree_vec (1);
3879 /* Fill in CHARVEC with all of the parameters. */
3880 charvec = make_tree_vec (len);
3881 for (i = 0; i < len; ++i)
3882 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3884 /* Build the argument packs. */
3885 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3886 TREE_TYPE (argpack) = char_type_node;
3888 TREE_VEC_ELT (argvec, 0) = argpack;
3890 return argvec;
3893 /* A subroutine of cp_parser_userdef_numeric_literal to
3894 create a char... template parameter pack from a string node. */
3896 static tree
3897 make_string_pack (tree value)
3899 tree charvec;
3900 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3901 const unsigned char *str
3902 = (const unsigned char *) TREE_STRING_POINTER (value);
3903 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3904 int len = TREE_STRING_LENGTH (value) / sz - 1;
3905 tree argvec = make_tree_vec (2);
3907 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3908 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3910 /* First template parm is character type. */
3911 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3913 /* Fill in CHARVEC with all of the parameters. */
3914 charvec = make_tree_vec (len);
3915 for (int i = 0; i < len; ++i)
3916 TREE_VEC_ELT (charvec, i)
3917 = double_int_to_tree (str_char_type_node,
3918 double_int::from_buffer (str + i * sz, sz));
3920 /* Build the argument packs. */
3921 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3922 TREE_TYPE (argpack) = str_char_type_node;
3924 TREE_VEC_ELT (argvec, 1) = argpack;
3926 return argvec;
3929 /* Parse a user-defined numeric constant. returns a call to a user-defined
3930 literal operator. */
3932 static tree
3933 cp_parser_userdef_numeric_literal (cp_parser *parser)
3935 cp_token *token = cp_lexer_consume_token (parser->lexer);
3936 tree literal = token->u.value;
3937 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3938 tree value = USERDEF_LITERAL_VALUE (literal);
3939 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3940 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3941 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3942 tree decl, result;
3943 vec<tree, va_gc> *args;
3945 /* Look for a literal operator taking the exact type of numeric argument
3946 as the literal value. */
3947 args = make_tree_vector ();
3948 vec_safe_push (args, value);
3949 decl = lookup_literal_operator (name, args);
3950 if (decl && decl != error_mark_node)
3952 result = finish_call_expr (decl, &args, false, true, tf_none);
3953 if (result != error_mark_node)
3955 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3956 warning_at (token->location, OPT_Woverflow,
3957 "integer literal exceeds range of %qT type",
3958 long_long_unsigned_type_node);
3959 else
3961 if (overflow > 0)
3962 warning_at (token->location, OPT_Woverflow,
3963 "floating literal exceeds range of %qT type",
3964 long_double_type_node);
3965 else if (overflow < 0)
3966 warning_at (token->location, OPT_Woverflow,
3967 "floating literal truncated to zero");
3969 release_tree_vector (args);
3970 return result;
3973 release_tree_vector (args);
3975 /* If the numeric argument didn't work, look for a raw literal
3976 operator taking a const char* argument consisting of the number
3977 in string format. */
3978 args = make_tree_vector ();
3979 vec_safe_push (args, num_string);
3980 decl = lookup_literal_operator (name, args);
3981 if (decl && decl != error_mark_node)
3983 result = finish_call_expr (decl, &args, false, true, tf_none);
3984 if (result != error_mark_node)
3986 release_tree_vector (args);
3987 return result;
3990 release_tree_vector (args);
3992 /* If the raw literal didn't work, look for a non-type template
3993 function with parameter pack char.... Call the function with
3994 template parameter characters representing the number. */
3995 args = make_tree_vector ();
3996 decl = lookup_literal_operator (name, args);
3997 if (decl && decl != error_mark_node)
3999 tree tmpl_args = make_char_string_pack (num_string);
4000 decl = lookup_template_function (decl, tmpl_args);
4001 result = finish_call_expr (decl, &args, false, true, tf_none);
4002 if (result != error_mark_node)
4004 release_tree_vector (args);
4005 return result;
4008 release_tree_vector (args);
4010 error ("unable to find numeric literal operator %qD", name);
4011 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4012 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4013 "to enable more built-in suffixes");
4014 return error_mark_node;
4017 /* Parse a user-defined string constant. Returns a call to a user-defined
4018 literal operator taking a character pointer and the length of the string
4019 as arguments. */
4021 static tree
4022 cp_parser_userdef_string_literal (tree literal)
4024 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4025 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4026 tree value = USERDEF_LITERAL_VALUE (literal);
4027 int len = TREE_STRING_LENGTH (value)
4028 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4029 tree decl, result;
4030 vec<tree, va_gc> *args;
4032 /* Look for a template function with typename parameter CharT
4033 and parameter pack CharT... Call the function with
4034 template parameter characters representing the string. */
4035 args = make_tree_vector ();
4036 decl = lookup_literal_operator (name, args);
4037 if (decl && decl != error_mark_node)
4039 tree tmpl_args = make_string_pack (value);
4040 decl = lookup_template_function (decl, tmpl_args);
4041 result = finish_call_expr (decl, &args, false, true, tf_none);
4042 if (result != error_mark_node)
4044 release_tree_vector (args);
4045 return result;
4048 release_tree_vector (args);
4050 /* Build up a call to the user-defined operator */
4051 /* Lookup the name we got back from the id-expression. */
4052 args = make_tree_vector ();
4053 vec_safe_push (args, value);
4054 vec_safe_push (args, build_int_cst (size_type_node, len));
4055 decl = lookup_name (name);
4056 if (!decl || decl == error_mark_node)
4058 error ("unable to find string literal operator %qD", name);
4059 release_tree_vector (args);
4060 return error_mark_node;
4062 result = finish_call_expr (decl, &args, false, true, tf_none);
4063 release_tree_vector (args);
4064 if (result != error_mark_node)
4065 return result;
4067 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4068 name, TREE_TYPE (value), size_type_node);
4069 return error_mark_node;
4073 /* Basic concepts [gram.basic] */
4075 /* Parse a translation-unit.
4077 translation-unit:
4078 declaration-seq [opt]
4080 Returns TRUE if all went well. */
4082 static bool
4083 cp_parser_translation_unit (cp_parser* parser)
4085 /* The address of the first non-permanent object on the declarator
4086 obstack. */
4087 static void *declarator_obstack_base;
4089 bool success;
4091 /* Create the declarator obstack, if necessary. */
4092 if (!cp_error_declarator)
4094 gcc_obstack_init (&declarator_obstack);
4095 /* Create the error declarator. */
4096 cp_error_declarator = make_declarator (cdk_error);
4097 /* Create the empty parameter list. */
4098 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4099 /* Remember where the base of the declarator obstack lies. */
4100 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4103 cp_parser_declaration_seq_opt (parser);
4105 /* If there are no tokens left then all went well. */
4106 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4108 /* Get rid of the token array; we don't need it any more. */
4109 cp_lexer_destroy (parser->lexer);
4110 parser->lexer = NULL;
4112 /* This file might have been a context that's implicitly extern
4113 "C". If so, pop the lang context. (Only relevant for PCH.) */
4114 if (parser->implicit_extern_c)
4116 pop_lang_context ();
4117 parser->implicit_extern_c = false;
4120 /* Finish up. */
4121 finish_translation_unit ();
4123 success = true;
4125 else
4127 cp_parser_error (parser, "expected declaration");
4128 success = false;
4131 /* Make sure the declarator obstack was fully cleaned up. */
4132 gcc_assert (obstack_next_free (&declarator_obstack)
4133 == declarator_obstack_base);
4135 /* All went well. */
4136 return success;
4139 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4140 decltype context. */
4142 static inline tsubst_flags_t
4143 complain_flags (bool decltype_p)
4145 tsubst_flags_t complain = tf_warning_or_error;
4146 if (decltype_p)
4147 complain |= tf_decltype;
4148 return complain;
4151 /* We're about to parse a collection of statements. If we're currently
4152 parsing tentatively, set up a firewall so that any nested
4153 cp_parser_commit_to_tentative_parse won't affect the current context. */
4155 static cp_token_position
4156 cp_parser_start_tentative_firewall (cp_parser *parser)
4158 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4159 return 0;
4161 cp_parser_parse_tentatively (parser);
4162 cp_parser_commit_to_topmost_tentative_parse (parser);
4163 return cp_lexer_token_position (parser->lexer, false);
4166 /* We've finished parsing the collection of statements. Wrap up the
4167 firewall and replace the relevant tokens with the parsed form. */
4169 static void
4170 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4171 tree expr)
4173 if (!start)
4174 return;
4176 /* Finish the firewall level. */
4177 cp_parser_parse_definitely (parser);
4178 /* And remember the result of the parse for when we try again. */
4179 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4180 token->type = CPP_PREPARSED_EXPR;
4181 token->u.value = expr;
4182 token->keyword = RID_MAX;
4183 cp_lexer_purge_tokens_after (parser->lexer, start);
4186 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4187 enclosing parentheses. */
4189 static tree
4190 cp_parser_statement_expr (cp_parser *parser)
4192 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4194 /* Consume the '('. */
4195 cp_lexer_consume_token (parser->lexer);
4196 /* Start the statement-expression. */
4197 tree expr = begin_stmt_expr ();
4198 /* Parse the compound-statement. */
4199 cp_parser_compound_statement (parser, expr, false, false);
4200 /* Finish up. */
4201 expr = finish_stmt_expr (expr, false);
4202 /* Consume the ')'. */
4203 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4204 cp_parser_skip_to_end_of_statement (parser);
4206 cp_parser_end_tentative_firewall (parser, start, expr);
4207 return expr;
4210 /* Expressions [gram.expr] */
4212 /* Parse a primary-expression.
4214 primary-expression:
4215 literal
4216 this
4217 ( expression )
4218 id-expression
4219 lambda-expression (C++11)
4221 GNU Extensions:
4223 primary-expression:
4224 ( compound-statement )
4225 __builtin_va_arg ( assignment-expression , type-id )
4226 __builtin_offsetof ( type-id , offsetof-expression )
4228 C++ Extensions:
4229 __has_nothrow_assign ( type-id )
4230 __has_nothrow_constructor ( type-id )
4231 __has_nothrow_copy ( type-id )
4232 __has_trivial_assign ( type-id )
4233 __has_trivial_constructor ( type-id )
4234 __has_trivial_copy ( type-id )
4235 __has_trivial_destructor ( type-id )
4236 __has_virtual_destructor ( type-id )
4237 __is_abstract ( type-id )
4238 __is_base_of ( type-id , type-id )
4239 __is_class ( type-id )
4240 __is_empty ( type-id )
4241 __is_enum ( type-id )
4242 __is_final ( type-id )
4243 __is_literal_type ( type-id )
4244 __is_pod ( type-id )
4245 __is_polymorphic ( type-id )
4246 __is_std_layout ( type-id )
4247 __is_trivial ( type-id )
4248 __is_union ( type-id )
4250 Objective-C++ Extension:
4252 primary-expression:
4253 objc-expression
4255 literal:
4256 __null
4258 ADDRESS_P is true iff this expression was immediately preceded by
4259 "&" and therefore might denote a pointer-to-member. CAST_P is true
4260 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4261 true iff this expression is a template argument.
4263 Returns a representation of the expression. Upon return, *IDK
4264 indicates what kind of id-expression (if any) was present. */
4266 static tree
4267 cp_parser_primary_expression (cp_parser *parser,
4268 bool address_p,
4269 bool cast_p,
4270 bool template_arg_p,
4271 bool decltype_p,
4272 cp_id_kind *idk)
4274 cp_token *token = NULL;
4276 /* Assume the primary expression is not an id-expression. */
4277 *idk = CP_ID_KIND_NONE;
4279 /* Peek at the next token. */
4280 token = cp_lexer_peek_token (parser->lexer);
4281 switch ((int) token->type)
4283 /* literal:
4284 integer-literal
4285 character-literal
4286 floating-literal
4287 string-literal
4288 boolean-literal
4289 pointer-literal
4290 user-defined-literal */
4291 case CPP_CHAR:
4292 case CPP_CHAR16:
4293 case CPP_CHAR32:
4294 case CPP_WCHAR:
4295 case CPP_NUMBER:
4296 case CPP_PREPARSED_EXPR:
4297 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4298 return cp_parser_userdef_numeric_literal (parser);
4299 token = cp_lexer_consume_token (parser->lexer);
4300 if (TREE_CODE (token->u.value) == FIXED_CST)
4302 error_at (token->location,
4303 "fixed-point types not supported in C++");
4304 return error_mark_node;
4306 /* Floating-point literals are only allowed in an integral
4307 constant expression if they are cast to an integral or
4308 enumeration type. */
4309 if (TREE_CODE (token->u.value) == REAL_CST
4310 && parser->integral_constant_expression_p
4311 && pedantic)
4313 /* CAST_P will be set even in invalid code like "int(2.7 +
4314 ...)". Therefore, we have to check that the next token
4315 is sure to end the cast. */
4316 if (cast_p)
4318 cp_token *next_token;
4320 next_token = cp_lexer_peek_token (parser->lexer);
4321 if (/* The comma at the end of an
4322 enumerator-definition. */
4323 next_token->type != CPP_COMMA
4324 /* The curly brace at the end of an enum-specifier. */
4325 && next_token->type != CPP_CLOSE_BRACE
4326 /* The end of a statement. */
4327 && next_token->type != CPP_SEMICOLON
4328 /* The end of the cast-expression. */
4329 && next_token->type != CPP_CLOSE_PAREN
4330 /* The end of an array bound. */
4331 && next_token->type != CPP_CLOSE_SQUARE
4332 /* The closing ">" in a template-argument-list. */
4333 && (next_token->type != CPP_GREATER
4334 || parser->greater_than_is_operator_p)
4335 /* C++0x only: A ">>" treated like two ">" tokens,
4336 in a template-argument-list. */
4337 && (next_token->type != CPP_RSHIFT
4338 || (cxx_dialect == cxx98)
4339 || parser->greater_than_is_operator_p))
4340 cast_p = false;
4343 /* If we are within a cast, then the constraint that the
4344 cast is to an integral or enumeration type will be
4345 checked at that point. If we are not within a cast, then
4346 this code is invalid. */
4347 if (!cast_p)
4348 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4350 return token->u.value;
4352 case CPP_CHAR_USERDEF:
4353 case CPP_CHAR16_USERDEF:
4354 case CPP_CHAR32_USERDEF:
4355 case CPP_WCHAR_USERDEF:
4356 return cp_parser_userdef_char_literal (parser);
4358 case CPP_STRING:
4359 case CPP_STRING16:
4360 case CPP_STRING32:
4361 case CPP_WSTRING:
4362 case CPP_UTF8STRING:
4363 case CPP_STRING_USERDEF:
4364 case CPP_STRING16_USERDEF:
4365 case CPP_STRING32_USERDEF:
4366 case CPP_WSTRING_USERDEF:
4367 case CPP_UTF8STRING_USERDEF:
4368 /* ??? Should wide strings be allowed when parser->translate_strings_p
4369 is false (i.e. in attributes)? If not, we can kill the third
4370 argument to cp_parser_string_literal. */
4371 return cp_parser_string_literal (parser,
4372 parser->translate_strings_p,
4373 true);
4375 case CPP_OPEN_PAREN:
4376 /* If we see `( { ' then we are looking at the beginning of
4377 a GNU statement-expression. */
4378 if (cp_parser_allow_gnu_extensions_p (parser)
4379 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4381 /* Statement-expressions are not allowed by the standard. */
4382 pedwarn (token->location, OPT_Wpedantic,
4383 "ISO C++ forbids braced-groups within expressions");
4385 /* And they're not allowed outside of a function-body; you
4386 cannot, for example, write:
4388 int i = ({ int j = 3; j + 1; });
4390 at class or namespace scope. */
4391 if (!parser->in_function_body
4392 || parser->in_template_argument_list_p)
4394 error_at (token->location,
4395 "statement-expressions are not allowed outside "
4396 "functions nor in template-argument lists");
4397 cp_parser_skip_to_end_of_block_or_statement (parser);
4398 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4399 cp_lexer_consume_token (parser->lexer);
4400 return error_mark_node;
4402 else
4403 return cp_parser_statement_expr (parser);
4405 /* Otherwise it's a normal parenthesized expression. */
4407 tree expr;
4408 bool saved_greater_than_is_operator_p;
4410 /* Consume the `('. */
4411 cp_lexer_consume_token (parser->lexer);
4412 /* Within a parenthesized expression, a `>' token is always
4413 the greater-than operator. */
4414 saved_greater_than_is_operator_p
4415 = parser->greater_than_is_operator_p;
4416 parser->greater_than_is_operator_p = true;
4418 /* Parse the parenthesized expression. */
4419 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4420 /* Let the front end know that this expression was
4421 enclosed in parentheses. This matters in case, for
4422 example, the expression is of the form `A::B', since
4423 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4424 not. */
4425 expr = finish_parenthesized_expr (expr);
4426 /* DR 705: Wrapping an unqualified name in parentheses
4427 suppresses arg-dependent lookup. We want to pass back
4428 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4429 (c++/37862), but none of the others. */
4430 if (*idk != CP_ID_KIND_QUALIFIED)
4431 *idk = CP_ID_KIND_NONE;
4433 /* The `>' token might be the end of a template-id or
4434 template-parameter-list now. */
4435 parser->greater_than_is_operator_p
4436 = saved_greater_than_is_operator_p;
4437 /* Consume the `)'. */
4438 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4439 cp_parser_skip_to_end_of_statement (parser);
4441 return expr;
4444 case CPP_OPEN_SQUARE:
4445 if (c_dialect_objc ())
4446 /* We have an Objective-C++ message. */
4447 return cp_parser_objc_expression (parser);
4449 tree lam = cp_parser_lambda_expression (parser);
4450 /* Don't warn about a failed tentative parse. */
4451 if (cp_parser_error_occurred (parser))
4452 return error_mark_node;
4453 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4454 return lam;
4457 case CPP_OBJC_STRING:
4458 if (c_dialect_objc ())
4459 /* We have an Objective-C++ string literal. */
4460 return cp_parser_objc_expression (parser);
4461 cp_parser_error (parser, "expected primary-expression");
4462 return error_mark_node;
4464 case CPP_KEYWORD:
4465 switch (token->keyword)
4467 /* These two are the boolean literals. */
4468 case RID_TRUE:
4469 cp_lexer_consume_token (parser->lexer);
4470 return boolean_true_node;
4471 case RID_FALSE:
4472 cp_lexer_consume_token (parser->lexer);
4473 return boolean_false_node;
4475 /* The `__null' literal. */
4476 case RID_NULL:
4477 cp_lexer_consume_token (parser->lexer);
4478 return null_node;
4480 /* The `nullptr' literal. */
4481 case RID_NULLPTR:
4482 cp_lexer_consume_token (parser->lexer);
4483 return nullptr_node;
4485 /* Recognize the `this' keyword. */
4486 case RID_THIS:
4487 cp_lexer_consume_token (parser->lexer);
4488 if (parser->local_variables_forbidden_p)
4490 error_at (token->location,
4491 "%<this%> may not be used in this context");
4492 return error_mark_node;
4494 /* Pointers cannot appear in constant-expressions. */
4495 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4496 return error_mark_node;
4497 return finish_this_expr ();
4499 /* The `operator' keyword can be the beginning of an
4500 id-expression. */
4501 case RID_OPERATOR:
4502 goto id_expression;
4504 case RID_FUNCTION_NAME:
4505 case RID_PRETTY_FUNCTION_NAME:
4506 case RID_C99_FUNCTION_NAME:
4508 non_integral_constant name;
4510 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4511 __func__ are the names of variables -- but they are
4512 treated specially. Therefore, they are handled here,
4513 rather than relying on the generic id-expression logic
4514 below. Grammatically, these names are id-expressions.
4516 Consume the token. */
4517 token = cp_lexer_consume_token (parser->lexer);
4519 switch (token->keyword)
4521 case RID_FUNCTION_NAME:
4522 name = NIC_FUNC_NAME;
4523 break;
4524 case RID_PRETTY_FUNCTION_NAME:
4525 name = NIC_PRETTY_FUNC;
4526 break;
4527 case RID_C99_FUNCTION_NAME:
4528 name = NIC_C99_FUNC;
4529 break;
4530 default:
4531 gcc_unreachable ();
4534 if (cp_parser_non_integral_constant_expression (parser, name))
4535 return error_mark_node;
4537 /* Look up the name. */
4538 return finish_fname (token->u.value);
4541 case RID_VA_ARG:
4543 tree expression;
4544 tree type;
4545 source_location type_location;
4547 /* The `__builtin_va_arg' construct is used to handle
4548 `va_arg'. Consume the `__builtin_va_arg' token. */
4549 cp_lexer_consume_token (parser->lexer);
4550 /* Look for the opening `('. */
4551 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4552 /* Now, parse the assignment-expression. */
4553 expression = cp_parser_assignment_expression (parser);
4554 /* Look for the `,'. */
4555 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4556 type_location = cp_lexer_peek_token (parser->lexer)->location;
4557 /* Parse the type-id. */
4558 type = cp_parser_type_id (parser);
4559 /* Look for the closing `)'. */
4560 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4561 /* Using `va_arg' in a constant-expression is not
4562 allowed. */
4563 if (cp_parser_non_integral_constant_expression (parser,
4564 NIC_VA_ARG))
4565 return error_mark_node;
4566 return build_x_va_arg (type_location, expression, type);
4569 case RID_OFFSETOF:
4570 return cp_parser_builtin_offsetof (parser);
4572 case RID_HAS_NOTHROW_ASSIGN:
4573 case RID_HAS_NOTHROW_CONSTRUCTOR:
4574 case RID_HAS_NOTHROW_COPY:
4575 case RID_HAS_TRIVIAL_ASSIGN:
4576 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4577 case RID_HAS_TRIVIAL_COPY:
4578 case RID_HAS_TRIVIAL_DESTRUCTOR:
4579 case RID_HAS_VIRTUAL_DESTRUCTOR:
4580 case RID_IS_ABSTRACT:
4581 case RID_IS_BASE_OF:
4582 case RID_IS_CLASS:
4583 case RID_IS_EMPTY:
4584 case RID_IS_ENUM:
4585 case RID_IS_FINAL:
4586 case RID_IS_LITERAL_TYPE:
4587 case RID_IS_POD:
4588 case RID_IS_POLYMORPHIC:
4589 case RID_IS_STD_LAYOUT:
4590 case RID_IS_TRIVIAL:
4591 case RID_IS_TRIVIALLY_ASSIGNABLE:
4592 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4593 case RID_IS_TRIVIALLY_COPYABLE:
4594 case RID_IS_UNION:
4595 return cp_parser_trait_expr (parser, token->keyword);
4597 /* Objective-C++ expressions. */
4598 case RID_AT_ENCODE:
4599 case RID_AT_PROTOCOL:
4600 case RID_AT_SELECTOR:
4601 return cp_parser_objc_expression (parser);
4603 case RID_TEMPLATE:
4604 if (parser->in_function_body
4605 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4606 == CPP_LESS))
4608 error_at (token->location,
4609 "a template declaration cannot appear at block scope");
4610 cp_parser_skip_to_end_of_block_or_statement (parser);
4611 return error_mark_node;
4613 default:
4614 cp_parser_error (parser, "expected primary-expression");
4615 return error_mark_node;
4618 /* An id-expression can start with either an identifier, a
4619 `::' as the beginning of a qualified-id, or the "operator"
4620 keyword. */
4621 case CPP_NAME:
4622 case CPP_SCOPE:
4623 case CPP_TEMPLATE_ID:
4624 case CPP_NESTED_NAME_SPECIFIER:
4626 tree id_expression;
4627 tree decl;
4628 const char *error_msg;
4629 bool template_p;
4630 bool done;
4631 cp_token *id_expr_token;
4633 id_expression:
4634 /* Parse the id-expression. */
4635 id_expression
4636 = cp_parser_id_expression (parser,
4637 /*template_keyword_p=*/false,
4638 /*check_dependency_p=*/true,
4639 &template_p,
4640 /*declarator_p=*/false,
4641 /*optional_p=*/false);
4642 if (id_expression == error_mark_node)
4643 return error_mark_node;
4644 id_expr_token = token;
4645 token = cp_lexer_peek_token (parser->lexer);
4646 done = (token->type != CPP_OPEN_SQUARE
4647 && token->type != CPP_OPEN_PAREN
4648 && token->type != CPP_DOT
4649 && token->type != CPP_DEREF
4650 && token->type != CPP_PLUS_PLUS
4651 && token->type != CPP_MINUS_MINUS);
4652 /* If we have a template-id, then no further lookup is
4653 required. If the template-id was for a template-class, we
4654 will sometimes have a TYPE_DECL at this point. */
4655 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4656 || TREE_CODE (id_expression) == TYPE_DECL)
4657 decl = id_expression;
4658 /* Look up the name. */
4659 else
4661 tree ambiguous_decls;
4663 /* If we already know that this lookup is ambiguous, then
4664 we've already issued an error message; there's no reason
4665 to check again. */
4666 if (id_expr_token->type == CPP_NAME
4667 && id_expr_token->error_reported)
4669 cp_parser_simulate_error (parser);
4670 return error_mark_node;
4673 decl = cp_parser_lookup_name (parser, id_expression,
4674 none_type,
4675 template_p,
4676 /*is_namespace=*/false,
4677 /*check_dependency=*/true,
4678 &ambiguous_decls,
4679 id_expr_token->location);
4680 /* If the lookup was ambiguous, an error will already have
4681 been issued. */
4682 if (ambiguous_decls)
4683 return error_mark_node;
4685 /* In Objective-C++, we may have an Objective-C 2.0
4686 dot-syntax for classes here. */
4687 if (c_dialect_objc ()
4688 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4689 && TREE_CODE (decl) == TYPE_DECL
4690 && objc_is_class_name (decl))
4692 tree component;
4693 cp_lexer_consume_token (parser->lexer);
4694 component = cp_parser_identifier (parser);
4695 if (component == error_mark_node)
4696 return error_mark_node;
4698 return objc_build_class_component_ref (id_expression, component);
4701 /* In Objective-C++, an instance variable (ivar) may be preferred
4702 to whatever cp_parser_lookup_name() found. */
4703 decl = objc_lookup_ivar (decl, id_expression);
4705 /* If name lookup gives us a SCOPE_REF, then the
4706 qualifying scope was dependent. */
4707 if (TREE_CODE (decl) == SCOPE_REF)
4709 /* At this point, we do not know if DECL is a valid
4710 integral constant expression. We assume that it is
4711 in fact such an expression, so that code like:
4713 template <int N> struct A {
4714 int a[B<N>::i];
4717 is accepted. At template-instantiation time, we
4718 will check that B<N>::i is actually a constant. */
4719 return decl;
4721 /* Check to see if DECL is a local variable in a context
4722 where that is forbidden. */
4723 if (parser->local_variables_forbidden_p
4724 && local_variable_p (decl))
4726 /* It might be that we only found DECL because we are
4727 trying to be generous with pre-ISO scoping rules.
4728 For example, consider:
4730 int i;
4731 void g() {
4732 for (int i = 0; i < 10; ++i) {}
4733 extern void f(int j = i);
4736 Here, name look up will originally find the out
4737 of scope `i'. We need to issue a warning message,
4738 but then use the global `i'. */
4739 decl = check_for_out_of_scope_variable (decl);
4740 if (local_variable_p (decl))
4742 error_at (id_expr_token->location,
4743 "local variable %qD may not appear in this context",
4744 decl);
4745 return error_mark_node;
4750 decl = (finish_id_expression
4751 (id_expression, decl, parser->scope,
4752 idk,
4753 parser->integral_constant_expression_p,
4754 parser->allow_non_integral_constant_expression_p,
4755 &parser->non_integral_constant_expression_p,
4756 template_p, done, address_p,
4757 template_arg_p,
4758 &error_msg,
4759 id_expr_token->location));
4760 if (error_msg)
4761 cp_parser_error (parser, error_msg);
4762 return decl;
4765 /* Anything else is an error. */
4766 default:
4767 cp_parser_error (parser, "expected primary-expression");
4768 return error_mark_node;
4772 static inline tree
4773 cp_parser_primary_expression (cp_parser *parser,
4774 bool address_p,
4775 bool cast_p,
4776 bool template_arg_p,
4777 cp_id_kind *idk)
4779 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4780 /*decltype*/false, idk);
4783 /* Parse an id-expression.
4785 id-expression:
4786 unqualified-id
4787 qualified-id
4789 qualified-id:
4790 :: [opt] nested-name-specifier template [opt] unqualified-id
4791 :: identifier
4792 :: operator-function-id
4793 :: template-id
4795 Return a representation of the unqualified portion of the
4796 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4797 a `::' or nested-name-specifier.
4799 Often, if the id-expression was a qualified-id, the caller will
4800 want to make a SCOPE_REF to represent the qualified-id. This
4801 function does not do this in order to avoid wastefully creating
4802 SCOPE_REFs when they are not required.
4804 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4805 `template' keyword.
4807 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4808 uninstantiated templates.
4810 If *TEMPLATE_P is non-NULL, it is set to true iff the
4811 `template' keyword is used to explicitly indicate that the entity
4812 named is a template.
4814 If DECLARATOR_P is true, the id-expression is appearing as part of
4815 a declarator, rather than as part of an expression. */
4817 static tree
4818 cp_parser_id_expression (cp_parser *parser,
4819 bool template_keyword_p,
4820 bool check_dependency_p,
4821 bool *template_p,
4822 bool declarator_p,
4823 bool optional_p)
4825 bool global_scope_p;
4826 bool nested_name_specifier_p;
4828 /* Assume the `template' keyword was not used. */
4829 if (template_p)
4830 *template_p = template_keyword_p;
4832 /* Look for the optional `::' operator. */
4833 global_scope_p
4834 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4835 != NULL_TREE);
4836 /* Look for the optional nested-name-specifier. */
4837 nested_name_specifier_p
4838 = (cp_parser_nested_name_specifier_opt (parser,
4839 /*typename_keyword_p=*/false,
4840 check_dependency_p,
4841 /*type_p=*/false,
4842 declarator_p)
4843 != NULL_TREE);
4844 /* If there is a nested-name-specifier, then we are looking at
4845 the first qualified-id production. */
4846 if (nested_name_specifier_p)
4848 tree saved_scope;
4849 tree saved_object_scope;
4850 tree saved_qualifying_scope;
4851 tree unqualified_id;
4852 bool is_template;
4854 /* See if the next token is the `template' keyword. */
4855 if (!template_p)
4856 template_p = &is_template;
4857 *template_p = cp_parser_optional_template_keyword (parser);
4858 /* Name lookup we do during the processing of the
4859 unqualified-id might obliterate SCOPE. */
4860 saved_scope = parser->scope;
4861 saved_object_scope = parser->object_scope;
4862 saved_qualifying_scope = parser->qualifying_scope;
4863 /* Process the final unqualified-id. */
4864 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4865 check_dependency_p,
4866 declarator_p,
4867 /*optional_p=*/false);
4868 /* Restore the SAVED_SCOPE for our caller. */
4869 parser->scope = saved_scope;
4870 parser->object_scope = saved_object_scope;
4871 parser->qualifying_scope = saved_qualifying_scope;
4873 return unqualified_id;
4875 /* Otherwise, if we are in global scope, then we are looking at one
4876 of the other qualified-id productions. */
4877 else if (global_scope_p)
4879 cp_token *token;
4880 tree id;
4882 /* Peek at the next token. */
4883 token = cp_lexer_peek_token (parser->lexer);
4885 /* If it's an identifier, and the next token is not a "<", then
4886 we can avoid the template-id case. This is an optimization
4887 for this common case. */
4888 if (token->type == CPP_NAME
4889 && !cp_parser_nth_token_starts_template_argument_list_p
4890 (parser, 2))
4891 return cp_parser_identifier (parser);
4893 cp_parser_parse_tentatively (parser);
4894 /* Try a template-id. */
4895 id = cp_parser_template_id (parser,
4896 /*template_keyword_p=*/false,
4897 /*check_dependency_p=*/true,
4898 none_type,
4899 declarator_p);
4900 /* If that worked, we're done. */
4901 if (cp_parser_parse_definitely (parser))
4902 return id;
4904 /* Peek at the next token. (Changes in the token buffer may
4905 have invalidated the pointer obtained above.) */
4906 token = cp_lexer_peek_token (parser->lexer);
4908 switch (token->type)
4910 case CPP_NAME:
4911 return cp_parser_identifier (parser);
4913 case CPP_KEYWORD:
4914 if (token->keyword == RID_OPERATOR)
4915 return cp_parser_operator_function_id (parser);
4916 /* Fall through. */
4918 default:
4919 cp_parser_error (parser, "expected id-expression");
4920 return error_mark_node;
4923 else
4924 return cp_parser_unqualified_id (parser, template_keyword_p,
4925 /*check_dependency_p=*/true,
4926 declarator_p,
4927 optional_p);
4930 /* Parse an unqualified-id.
4932 unqualified-id:
4933 identifier
4934 operator-function-id
4935 conversion-function-id
4936 ~ class-name
4937 template-id
4939 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4940 keyword, in a construct like `A::template ...'.
4942 Returns a representation of unqualified-id. For the `identifier'
4943 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4944 production a BIT_NOT_EXPR is returned; the operand of the
4945 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4946 other productions, see the documentation accompanying the
4947 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4948 names are looked up in uninstantiated templates. If DECLARATOR_P
4949 is true, the unqualified-id is appearing as part of a declarator,
4950 rather than as part of an expression. */
4952 static tree
4953 cp_parser_unqualified_id (cp_parser* parser,
4954 bool template_keyword_p,
4955 bool check_dependency_p,
4956 bool declarator_p,
4957 bool optional_p)
4959 cp_token *token;
4961 /* Peek at the next token. */
4962 token = cp_lexer_peek_token (parser->lexer);
4964 switch ((int) token->type)
4966 case CPP_NAME:
4968 tree id;
4970 /* We don't know yet whether or not this will be a
4971 template-id. */
4972 cp_parser_parse_tentatively (parser);
4973 /* Try a template-id. */
4974 id = cp_parser_template_id (parser, template_keyword_p,
4975 check_dependency_p,
4976 none_type,
4977 declarator_p);
4978 /* If it worked, we're done. */
4979 if (cp_parser_parse_definitely (parser))
4980 return id;
4981 /* Otherwise, it's an ordinary identifier. */
4982 return cp_parser_identifier (parser);
4985 case CPP_TEMPLATE_ID:
4986 return cp_parser_template_id (parser, template_keyword_p,
4987 check_dependency_p,
4988 none_type,
4989 declarator_p);
4991 case CPP_COMPL:
4993 tree type_decl;
4994 tree qualifying_scope;
4995 tree object_scope;
4996 tree scope;
4997 bool done;
4999 /* Consume the `~' token. */
5000 cp_lexer_consume_token (parser->lexer);
5001 /* Parse the class-name. The standard, as written, seems to
5002 say that:
5004 template <typename T> struct S { ~S (); };
5005 template <typename T> S<T>::~S() {}
5007 is invalid, since `~' must be followed by a class-name, but
5008 `S<T>' is dependent, and so not known to be a class.
5009 That's not right; we need to look in uninstantiated
5010 templates. A further complication arises from:
5012 template <typename T> void f(T t) {
5013 t.T::~T();
5016 Here, it is not possible to look up `T' in the scope of `T'
5017 itself. We must look in both the current scope, and the
5018 scope of the containing complete expression.
5020 Yet another issue is:
5022 struct S {
5023 int S;
5024 ~S();
5027 S::~S() {}
5029 The standard does not seem to say that the `S' in `~S'
5030 should refer to the type `S' and not the data member
5031 `S::S'. */
5033 /* DR 244 says that we look up the name after the "~" in the
5034 same scope as we looked up the qualifying name. That idea
5035 isn't fully worked out; it's more complicated than that. */
5036 scope = parser->scope;
5037 object_scope = parser->object_scope;
5038 qualifying_scope = parser->qualifying_scope;
5040 /* Check for invalid scopes. */
5041 if (scope == error_mark_node)
5043 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5044 cp_lexer_consume_token (parser->lexer);
5045 return error_mark_node;
5047 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5049 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5050 error_at (token->location,
5051 "scope %qT before %<~%> is not a class-name",
5052 scope);
5053 cp_parser_simulate_error (parser);
5054 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5055 cp_lexer_consume_token (parser->lexer);
5056 return error_mark_node;
5058 gcc_assert (!scope || TYPE_P (scope));
5060 /* If the name is of the form "X::~X" it's OK even if X is a
5061 typedef. */
5062 token = cp_lexer_peek_token (parser->lexer);
5063 if (scope
5064 && token->type == CPP_NAME
5065 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5066 != CPP_LESS)
5067 && (token->u.value == TYPE_IDENTIFIER (scope)
5068 || (CLASS_TYPE_P (scope)
5069 && constructor_name_p (token->u.value, scope))))
5071 cp_lexer_consume_token (parser->lexer);
5072 return build_nt (BIT_NOT_EXPR, scope);
5075 /* ~auto means the destructor of whatever the object is. */
5076 if (cp_parser_is_keyword (token, RID_AUTO))
5078 if (cxx_dialect < cxx14)
5079 pedwarn (input_location, 0,
5080 "%<~auto%> only available with "
5081 "-std=c++14 or -std=gnu++14");
5082 cp_lexer_consume_token (parser->lexer);
5083 return build_nt (BIT_NOT_EXPR, make_auto ());
5086 /* If there was an explicit qualification (S::~T), first look
5087 in the scope given by the qualification (i.e., S).
5089 Note: in the calls to cp_parser_class_name below we pass
5090 typename_type so that lookup finds the injected-class-name
5091 rather than the constructor. */
5092 done = false;
5093 type_decl = NULL_TREE;
5094 if (scope)
5096 cp_parser_parse_tentatively (parser);
5097 type_decl = cp_parser_class_name (parser,
5098 /*typename_keyword_p=*/false,
5099 /*template_keyword_p=*/false,
5100 typename_type,
5101 /*check_dependency=*/false,
5102 /*class_head_p=*/false,
5103 declarator_p);
5104 if (cp_parser_parse_definitely (parser))
5105 done = true;
5107 /* In "N::S::~S", look in "N" as well. */
5108 if (!done && scope && qualifying_scope)
5110 cp_parser_parse_tentatively (parser);
5111 parser->scope = qualifying_scope;
5112 parser->object_scope = NULL_TREE;
5113 parser->qualifying_scope = NULL_TREE;
5114 type_decl
5115 = cp_parser_class_name (parser,
5116 /*typename_keyword_p=*/false,
5117 /*template_keyword_p=*/false,
5118 typename_type,
5119 /*check_dependency=*/false,
5120 /*class_head_p=*/false,
5121 declarator_p);
5122 if (cp_parser_parse_definitely (parser))
5123 done = true;
5125 /* In "p->S::~T", look in the scope given by "*p" as well. */
5126 else if (!done && object_scope)
5128 cp_parser_parse_tentatively (parser);
5129 parser->scope = object_scope;
5130 parser->object_scope = NULL_TREE;
5131 parser->qualifying_scope = NULL_TREE;
5132 type_decl
5133 = cp_parser_class_name (parser,
5134 /*typename_keyword_p=*/false,
5135 /*template_keyword_p=*/false,
5136 typename_type,
5137 /*check_dependency=*/false,
5138 /*class_head_p=*/false,
5139 declarator_p);
5140 if (cp_parser_parse_definitely (parser))
5141 done = true;
5143 /* Look in the surrounding context. */
5144 if (!done)
5146 parser->scope = NULL_TREE;
5147 parser->object_scope = NULL_TREE;
5148 parser->qualifying_scope = NULL_TREE;
5149 if (processing_template_decl)
5150 cp_parser_parse_tentatively (parser);
5151 type_decl
5152 = cp_parser_class_name (parser,
5153 /*typename_keyword_p=*/false,
5154 /*template_keyword_p=*/false,
5155 typename_type,
5156 /*check_dependency=*/false,
5157 /*class_head_p=*/false,
5158 declarator_p);
5159 if (processing_template_decl
5160 && ! cp_parser_parse_definitely (parser))
5162 /* We couldn't find a type with this name, so just accept
5163 it and check for a match at instantiation time. */
5164 type_decl = cp_parser_identifier (parser);
5165 if (type_decl != error_mark_node)
5166 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5167 return type_decl;
5170 /* If an error occurred, assume that the name of the
5171 destructor is the same as the name of the qualifying
5172 class. That allows us to keep parsing after running
5173 into ill-formed destructor names. */
5174 if (type_decl == error_mark_node && scope)
5175 return build_nt (BIT_NOT_EXPR, scope);
5176 else if (type_decl == error_mark_node)
5177 return error_mark_node;
5179 /* Check that destructor name and scope match. */
5180 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5182 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5183 error_at (token->location,
5184 "declaration of %<~%T%> as member of %qT",
5185 type_decl, scope);
5186 cp_parser_simulate_error (parser);
5187 return error_mark_node;
5190 /* [class.dtor]
5192 A typedef-name that names a class shall not be used as the
5193 identifier in the declarator for a destructor declaration. */
5194 if (declarator_p
5195 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5196 && !DECL_SELF_REFERENCE_P (type_decl)
5197 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5198 error_at (token->location,
5199 "typedef-name %qD used as destructor declarator",
5200 type_decl);
5202 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5205 case CPP_KEYWORD:
5206 if (token->keyword == RID_OPERATOR)
5208 tree id;
5210 /* This could be a template-id, so we try that first. */
5211 cp_parser_parse_tentatively (parser);
5212 /* Try a template-id. */
5213 id = cp_parser_template_id (parser, template_keyword_p,
5214 /*check_dependency_p=*/true,
5215 none_type,
5216 declarator_p);
5217 /* If that worked, we're done. */
5218 if (cp_parser_parse_definitely (parser))
5219 return id;
5220 /* We still don't know whether we're looking at an
5221 operator-function-id or a conversion-function-id. */
5222 cp_parser_parse_tentatively (parser);
5223 /* Try an operator-function-id. */
5224 id = cp_parser_operator_function_id (parser);
5225 /* If that didn't work, try a conversion-function-id. */
5226 if (!cp_parser_parse_definitely (parser))
5227 id = cp_parser_conversion_function_id (parser);
5228 else if (UDLIT_OPER_P (id))
5230 /* 17.6.3.3.5 */
5231 const char *name = UDLIT_OP_SUFFIX (id);
5232 if (name[0] != '_' && !in_system_header_at (input_location)
5233 && declarator_p)
5234 warning (0, "literal operator suffixes not preceded by %<_%>"
5235 " are reserved for future standardization");
5238 return id;
5240 /* Fall through. */
5242 default:
5243 if (optional_p)
5244 return NULL_TREE;
5245 cp_parser_error (parser, "expected unqualified-id");
5246 return error_mark_node;
5250 /* Parse an (optional) nested-name-specifier.
5252 nested-name-specifier: [C++98]
5253 class-or-namespace-name :: nested-name-specifier [opt]
5254 class-or-namespace-name :: template nested-name-specifier [opt]
5256 nested-name-specifier: [C++0x]
5257 type-name ::
5258 namespace-name ::
5259 nested-name-specifier identifier ::
5260 nested-name-specifier template [opt] simple-template-id ::
5262 PARSER->SCOPE should be set appropriately before this function is
5263 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5264 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5265 in name lookups.
5267 Sets PARSER->SCOPE to the class (TYPE) or namespace
5268 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5269 it unchanged if there is no nested-name-specifier. Returns the new
5270 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5272 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5273 part of a declaration and/or decl-specifier. */
5275 static tree
5276 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5277 bool typename_keyword_p,
5278 bool check_dependency_p,
5279 bool type_p,
5280 bool is_declaration)
5282 bool success = false;
5283 cp_token_position start = 0;
5284 cp_token *token;
5286 /* Remember where the nested-name-specifier starts. */
5287 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5289 start = cp_lexer_token_position (parser->lexer, false);
5290 push_deferring_access_checks (dk_deferred);
5293 while (true)
5295 tree new_scope;
5296 tree old_scope;
5297 tree saved_qualifying_scope;
5298 bool template_keyword_p;
5300 /* Spot cases that cannot be the beginning of a
5301 nested-name-specifier. */
5302 token = cp_lexer_peek_token (parser->lexer);
5304 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5305 the already parsed nested-name-specifier. */
5306 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5308 /* Grab the nested-name-specifier and continue the loop. */
5309 cp_parser_pre_parsed_nested_name_specifier (parser);
5310 /* If we originally encountered this nested-name-specifier
5311 with IS_DECLARATION set to false, we will not have
5312 resolved TYPENAME_TYPEs, so we must do so here. */
5313 if (is_declaration
5314 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5316 new_scope = resolve_typename_type (parser->scope,
5317 /*only_current_p=*/false);
5318 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5319 parser->scope = new_scope;
5321 success = true;
5322 continue;
5325 /* Spot cases that cannot be the beginning of a
5326 nested-name-specifier. On the second and subsequent times
5327 through the loop, we look for the `template' keyword. */
5328 if (success && token->keyword == RID_TEMPLATE)
5330 /* A template-id can start a nested-name-specifier. */
5331 else if (token->type == CPP_TEMPLATE_ID)
5333 /* DR 743: decltype can be used in a nested-name-specifier. */
5334 else if (token_is_decltype (token))
5336 else
5338 /* If the next token is not an identifier, then it is
5339 definitely not a type-name or namespace-name. */
5340 if (token->type != CPP_NAME)
5341 break;
5342 /* If the following token is neither a `<' (to begin a
5343 template-id), nor a `::', then we are not looking at a
5344 nested-name-specifier. */
5345 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5347 if (token->type == CPP_COLON
5348 && parser->colon_corrects_to_scope_p
5349 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5351 error_at (token->location,
5352 "found %<:%> in nested-name-specifier, expected %<::%>");
5353 token->type = CPP_SCOPE;
5356 if (token->type != CPP_SCOPE
5357 && !cp_parser_nth_token_starts_template_argument_list_p
5358 (parser, 2))
5359 break;
5362 /* The nested-name-specifier is optional, so we parse
5363 tentatively. */
5364 cp_parser_parse_tentatively (parser);
5366 /* Look for the optional `template' keyword, if this isn't the
5367 first time through the loop. */
5368 if (success)
5369 template_keyword_p = cp_parser_optional_template_keyword (parser);
5370 else
5371 template_keyword_p = false;
5373 /* Save the old scope since the name lookup we are about to do
5374 might destroy it. */
5375 old_scope = parser->scope;
5376 saved_qualifying_scope = parser->qualifying_scope;
5377 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5378 look up names in "X<T>::I" in order to determine that "Y" is
5379 a template. So, if we have a typename at this point, we make
5380 an effort to look through it. */
5381 if (is_declaration
5382 && !typename_keyword_p
5383 && parser->scope
5384 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5385 parser->scope = resolve_typename_type (parser->scope,
5386 /*only_current_p=*/false);
5387 /* Parse the qualifying entity. */
5388 new_scope
5389 = cp_parser_qualifying_entity (parser,
5390 typename_keyword_p,
5391 template_keyword_p,
5392 check_dependency_p,
5393 type_p,
5394 is_declaration);
5395 /* Look for the `::' token. */
5396 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5398 /* If we found what we wanted, we keep going; otherwise, we're
5399 done. */
5400 if (!cp_parser_parse_definitely (parser))
5402 bool error_p = false;
5404 /* Restore the OLD_SCOPE since it was valid before the
5405 failed attempt at finding the last
5406 class-or-namespace-name. */
5407 parser->scope = old_scope;
5408 parser->qualifying_scope = saved_qualifying_scope;
5410 /* If the next token is a decltype, and the one after that is a
5411 `::', then the decltype has failed to resolve to a class or
5412 enumeration type. Give this error even when parsing
5413 tentatively since it can't possibly be valid--and we're going
5414 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5415 won't get another chance.*/
5416 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5417 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5418 == CPP_SCOPE))
5420 token = cp_lexer_consume_token (parser->lexer);
5421 error_at (token->location, "decltype evaluates to %qT, "
5422 "which is not a class or enumeration type",
5423 token->u.value);
5424 parser->scope = error_mark_node;
5425 error_p = true;
5426 /* As below. */
5427 success = true;
5428 cp_lexer_consume_token (parser->lexer);
5431 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5432 break;
5433 /* If the next token is an identifier, and the one after
5434 that is a `::', then any valid interpretation would have
5435 found a class-or-namespace-name. */
5436 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5437 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5438 == CPP_SCOPE)
5439 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5440 != CPP_COMPL))
5442 token = cp_lexer_consume_token (parser->lexer);
5443 if (!error_p)
5445 if (!token->error_reported)
5447 tree decl;
5448 tree ambiguous_decls;
5450 decl = cp_parser_lookup_name (parser, token->u.value,
5451 none_type,
5452 /*is_template=*/false,
5453 /*is_namespace=*/false,
5454 /*check_dependency=*/true,
5455 &ambiguous_decls,
5456 token->location);
5457 if (TREE_CODE (decl) == TEMPLATE_DECL)
5458 error_at (token->location,
5459 "%qD used without template parameters",
5460 decl);
5461 else if (ambiguous_decls)
5463 // cp_parser_lookup_name has the same diagnostic,
5464 // thus make sure to emit it at most once.
5465 if (cp_parser_uncommitted_to_tentative_parse_p
5466 (parser))
5468 error_at (token->location,
5469 "reference to %qD is ambiguous",
5470 token->u.value);
5471 print_candidates (ambiguous_decls);
5473 decl = error_mark_node;
5475 else
5477 if (cxx_dialect != cxx98)
5478 cp_parser_name_lookup_error
5479 (parser, token->u.value, decl, NLE_NOT_CXX98,
5480 token->location);
5481 else
5482 cp_parser_name_lookup_error
5483 (parser, token->u.value, decl, NLE_CXX98,
5484 token->location);
5487 parser->scope = error_mark_node;
5488 error_p = true;
5489 /* Treat this as a successful nested-name-specifier
5490 due to:
5492 [basic.lookup.qual]
5494 If the name found is not a class-name (clause
5495 _class_) or namespace-name (_namespace.def_), the
5496 program is ill-formed. */
5497 success = true;
5499 cp_lexer_consume_token (parser->lexer);
5501 break;
5503 /* We've found one valid nested-name-specifier. */
5504 success = true;
5505 /* Name lookup always gives us a DECL. */
5506 if (TREE_CODE (new_scope) == TYPE_DECL)
5507 new_scope = TREE_TYPE (new_scope);
5508 /* Uses of "template" must be followed by actual templates. */
5509 if (template_keyword_p
5510 && !(CLASS_TYPE_P (new_scope)
5511 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5512 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5513 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5514 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5515 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5516 == TEMPLATE_ID_EXPR)))
5517 permerror (input_location, TYPE_P (new_scope)
5518 ? G_("%qT is not a template")
5519 : G_("%qD is not a template"),
5520 new_scope);
5521 /* If it is a class scope, try to complete it; we are about to
5522 be looking up names inside the class. */
5523 if (TYPE_P (new_scope)
5524 /* Since checking types for dependency can be expensive,
5525 avoid doing it if the type is already complete. */
5526 && !COMPLETE_TYPE_P (new_scope)
5527 /* Do not try to complete dependent types. */
5528 && !dependent_type_p (new_scope))
5530 new_scope = complete_type (new_scope);
5531 /* If it is a typedef to current class, use the current
5532 class instead, as the typedef won't have any names inside
5533 it yet. */
5534 if (!COMPLETE_TYPE_P (new_scope)
5535 && currently_open_class (new_scope))
5536 new_scope = TYPE_MAIN_VARIANT (new_scope);
5538 /* Make sure we look in the right scope the next time through
5539 the loop. */
5540 parser->scope = new_scope;
5543 /* If parsing tentatively, replace the sequence of tokens that makes
5544 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5545 token. That way, should we re-parse the token stream, we will
5546 not have to repeat the effort required to do the parse, nor will
5547 we issue duplicate error messages. */
5548 if (success && start)
5550 cp_token *token;
5552 token = cp_lexer_token_at (parser->lexer, start);
5553 /* Reset the contents of the START token. */
5554 token->type = CPP_NESTED_NAME_SPECIFIER;
5555 /* Retrieve any deferred checks. Do not pop this access checks yet
5556 so the memory will not be reclaimed during token replacing below. */
5557 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5558 token->u.tree_check_value->value = parser->scope;
5559 token->u.tree_check_value->checks = get_deferred_access_checks ();
5560 token->u.tree_check_value->qualifying_scope =
5561 parser->qualifying_scope;
5562 token->keyword = RID_MAX;
5564 /* Purge all subsequent tokens. */
5565 cp_lexer_purge_tokens_after (parser->lexer, start);
5568 if (start)
5569 pop_to_parent_deferring_access_checks ();
5571 return success ? parser->scope : NULL_TREE;
5574 /* Parse a nested-name-specifier. See
5575 cp_parser_nested_name_specifier_opt for details. This function
5576 behaves identically, except that it will an issue an error if no
5577 nested-name-specifier is present. */
5579 static tree
5580 cp_parser_nested_name_specifier (cp_parser *parser,
5581 bool typename_keyword_p,
5582 bool check_dependency_p,
5583 bool type_p,
5584 bool is_declaration)
5586 tree scope;
5588 /* Look for the nested-name-specifier. */
5589 scope = cp_parser_nested_name_specifier_opt (parser,
5590 typename_keyword_p,
5591 check_dependency_p,
5592 type_p,
5593 is_declaration);
5594 /* If it was not present, issue an error message. */
5595 if (!scope)
5597 cp_parser_error (parser, "expected nested-name-specifier");
5598 parser->scope = NULL_TREE;
5601 return scope;
5604 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5605 this is either a class-name or a namespace-name (which corresponds
5606 to the class-or-namespace-name production in the grammar). For
5607 C++0x, it can also be a type-name that refers to an enumeration
5608 type or a simple-template-id.
5610 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5611 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5612 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5613 TYPE_P is TRUE iff the next name should be taken as a class-name,
5614 even the same name is declared to be another entity in the same
5615 scope.
5617 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5618 specified by the class-or-namespace-name. If neither is found the
5619 ERROR_MARK_NODE is returned. */
5621 static tree
5622 cp_parser_qualifying_entity (cp_parser *parser,
5623 bool typename_keyword_p,
5624 bool template_keyword_p,
5625 bool check_dependency_p,
5626 bool type_p,
5627 bool is_declaration)
5629 tree saved_scope;
5630 tree saved_qualifying_scope;
5631 tree saved_object_scope;
5632 tree scope;
5633 bool only_class_p;
5634 bool successful_parse_p;
5636 /* DR 743: decltype can appear in a nested-name-specifier. */
5637 if (cp_lexer_next_token_is_decltype (parser->lexer))
5639 scope = cp_parser_decltype (parser);
5640 if (TREE_CODE (scope) != ENUMERAL_TYPE
5641 && !MAYBE_CLASS_TYPE_P (scope))
5643 cp_parser_simulate_error (parser);
5644 return error_mark_node;
5646 if (TYPE_NAME (scope))
5647 scope = TYPE_NAME (scope);
5648 return scope;
5651 /* Before we try to parse the class-name, we must save away the
5652 current PARSER->SCOPE since cp_parser_class_name will destroy
5653 it. */
5654 saved_scope = parser->scope;
5655 saved_qualifying_scope = parser->qualifying_scope;
5656 saved_object_scope = parser->object_scope;
5657 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5658 there is no need to look for a namespace-name. */
5659 only_class_p = template_keyword_p
5660 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5661 if (!only_class_p)
5662 cp_parser_parse_tentatively (parser);
5663 scope = cp_parser_class_name (parser,
5664 typename_keyword_p,
5665 template_keyword_p,
5666 type_p ? class_type : none_type,
5667 check_dependency_p,
5668 /*class_head_p=*/false,
5669 is_declaration);
5670 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5671 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5672 if (!only_class_p
5673 && cxx_dialect != cxx98
5674 && !successful_parse_p)
5676 /* Restore the saved scope. */
5677 parser->scope = saved_scope;
5678 parser->qualifying_scope = saved_qualifying_scope;
5679 parser->object_scope = saved_object_scope;
5681 /* Parse tentatively. */
5682 cp_parser_parse_tentatively (parser);
5684 /* Parse a type-name */
5685 scope = cp_parser_type_name (parser);
5687 /* "If the name found does not designate a namespace or a class,
5688 enumeration, or dependent type, the program is ill-formed."
5690 We cover classes and dependent types above and namespaces below,
5691 so this code is only looking for enums. */
5692 if (!scope || TREE_CODE (scope) != TYPE_DECL
5693 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5694 cp_parser_simulate_error (parser);
5696 successful_parse_p = cp_parser_parse_definitely (parser);
5698 /* If that didn't work, try for a namespace-name. */
5699 if (!only_class_p && !successful_parse_p)
5701 /* Restore the saved scope. */
5702 parser->scope = saved_scope;
5703 parser->qualifying_scope = saved_qualifying_scope;
5704 parser->object_scope = saved_object_scope;
5705 /* If we are not looking at an identifier followed by the scope
5706 resolution operator, then this is not part of a
5707 nested-name-specifier. (Note that this function is only used
5708 to parse the components of a nested-name-specifier.) */
5709 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5710 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5711 return error_mark_node;
5712 scope = cp_parser_namespace_name (parser);
5715 return scope;
5718 /* Return true if we are looking at a compound-literal, false otherwise. */
5720 static bool
5721 cp_parser_compound_literal_p (cp_parser *parser)
5723 /* Consume the `('. */
5724 cp_lexer_consume_token (parser->lexer);
5726 cp_lexer_save_tokens (parser->lexer);
5728 /* Skip tokens until the next token is a closing parenthesis.
5729 If we find the closing `)', and the next token is a `{', then
5730 we are looking at a compound-literal. */
5731 bool compound_literal_p
5732 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5733 /*consume_paren=*/true)
5734 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5736 /* Roll back the tokens we skipped. */
5737 cp_lexer_rollback_tokens (parser->lexer);
5739 return compound_literal_p;
5742 /* Parse a postfix-expression.
5744 postfix-expression:
5745 primary-expression
5746 postfix-expression [ expression ]
5747 postfix-expression ( expression-list [opt] )
5748 simple-type-specifier ( expression-list [opt] )
5749 typename :: [opt] nested-name-specifier identifier
5750 ( expression-list [opt] )
5751 typename :: [opt] nested-name-specifier template [opt] template-id
5752 ( expression-list [opt] )
5753 postfix-expression . template [opt] id-expression
5754 postfix-expression -> template [opt] id-expression
5755 postfix-expression . pseudo-destructor-name
5756 postfix-expression -> pseudo-destructor-name
5757 postfix-expression ++
5758 postfix-expression --
5759 dynamic_cast < type-id > ( expression )
5760 static_cast < type-id > ( expression )
5761 reinterpret_cast < type-id > ( expression )
5762 const_cast < type-id > ( expression )
5763 typeid ( expression )
5764 typeid ( type-id )
5766 GNU Extension:
5768 postfix-expression:
5769 ( type-id ) { initializer-list , [opt] }
5771 This extension is a GNU version of the C99 compound-literal
5772 construct. (The C99 grammar uses `type-name' instead of `type-id',
5773 but they are essentially the same concept.)
5775 If ADDRESS_P is true, the postfix expression is the operand of the
5776 `&' operator. CAST_P is true if this expression is the target of a
5777 cast.
5779 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5780 class member access expressions [expr.ref].
5782 Returns a representation of the expression. */
5784 static tree
5785 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5786 bool member_access_only_p, bool decltype_p,
5787 cp_id_kind * pidk_return)
5789 cp_token *token;
5790 location_t loc;
5791 enum rid keyword;
5792 cp_id_kind idk = CP_ID_KIND_NONE;
5793 tree postfix_expression = NULL_TREE;
5794 bool is_member_access = false;
5795 int saved_in_statement = -1;
5797 /* Peek at the next token. */
5798 token = cp_lexer_peek_token (parser->lexer);
5799 loc = token->location;
5800 /* Some of the productions are determined by keywords. */
5801 keyword = token->keyword;
5802 switch (keyword)
5804 case RID_DYNCAST:
5805 case RID_STATCAST:
5806 case RID_REINTCAST:
5807 case RID_CONSTCAST:
5809 tree type;
5810 tree expression;
5811 const char *saved_message;
5812 bool saved_in_type_id_in_expr_p;
5814 /* All of these can be handled in the same way from the point
5815 of view of parsing. Begin by consuming the token
5816 identifying the cast. */
5817 cp_lexer_consume_token (parser->lexer);
5819 /* New types cannot be defined in the cast. */
5820 saved_message = parser->type_definition_forbidden_message;
5821 parser->type_definition_forbidden_message
5822 = G_("types may not be defined in casts");
5824 /* Look for the opening `<'. */
5825 cp_parser_require (parser, CPP_LESS, RT_LESS);
5826 /* Parse the type to which we are casting. */
5827 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5828 parser->in_type_id_in_expr_p = true;
5829 type = cp_parser_type_id (parser);
5830 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5831 /* Look for the closing `>'. */
5832 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5833 /* Restore the old message. */
5834 parser->type_definition_forbidden_message = saved_message;
5836 bool saved_greater_than_is_operator_p
5837 = parser->greater_than_is_operator_p;
5838 parser->greater_than_is_operator_p = true;
5840 /* And the expression which is being cast. */
5841 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5842 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5843 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5845 parser->greater_than_is_operator_p
5846 = saved_greater_than_is_operator_p;
5848 /* Only type conversions to integral or enumeration types
5849 can be used in constant-expressions. */
5850 if (!cast_valid_in_integral_constant_expression_p (type)
5851 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5852 return error_mark_node;
5854 switch (keyword)
5856 case RID_DYNCAST:
5857 postfix_expression
5858 = build_dynamic_cast (type, expression, tf_warning_or_error);
5859 break;
5860 case RID_STATCAST:
5861 postfix_expression
5862 = build_static_cast (type, expression, tf_warning_or_error);
5863 break;
5864 case RID_REINTCAST:
5865 postfix_expression
5866 = build_reinterpret_cast (type, expression,
5867 tf_warning_or_error);
5868 break;
5869 case RID_CONSTCAST:
5870 postfix_expression
5871 = build_const_cast (type, expression, tf_warning_or_error);
5872 break;
5873 default:
5874 gcc_unreachable ();
5877 break;
5879 case RID_TYPEID:
5881 tree type;
5882 const char *saved_message;
5883 bool saved_in_type_id_in_expr_p;
5885 /* Consume the `typeid' token. */
5886 cp_lexer_consume_token (parser->lexer);
5887 /* Look for the `(' token. */
5888 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5889 /* Types cannot be defined in a `typeid' expression. */
5890 saved_message = parser->type_definition_forbidden_message;
5891 parser->type_definition_forbidden_message
5892 = G_("types may not be defined in a %<typeid%> expression");
5893 /* We can't be sure yet whether we're looking at a type-id or an
5894 expression. */
5895 cp_parser_parse_tentatively (parser);
5896 /* Try a type-id first. */
5897 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5898 parser->in_type_id_in_expr_p = true;
5899 type = cp_parser_type_id (parser);
5900 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5901 /* Look for the `)' token. Otherwise, we can't be sure that
5902 we're not looking at an expression: consider `typeid (int
5903 (3))', for example. */
5904 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5905 /* If all went well, simply lookup the type-id. */
5906 if (cp_parser_parse_definitely (parser))
5907 postfix_expression = get_typeid (type, tf_warning_or_error);
5908 /* Otherwise, fall back to the expression variant. */
5909 else
5911 tree expression;
5913 /* Look for an expression. */
5914 expression = cp_parser_expression (parser, & idk);
5915 /* Compute its typeid. */
5916 postfix_expression = build_typeid (expression, tf_warning_or_error);
5917 /* Look for the `)' token. */
5918 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5920 /* Restore the saved message. */
5921 parser->type_definition_forbidden_message = saved_message;
5922 /* `typeid' may not appear in an integral constant expression. */
5923 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5924 return error_mark_node;
5926 break;
5928 case RID_TYPENAME:
5930 tree type;
5931 /* The syntax permitted here is the same permitted for an
5932 elaborated-type-specifier. */
5933 type = cp_parser_elaborated_type_specifier (parser,
5934 /*is_friend=*/false,
5935 /*is_declaration=*/false);
5936 postfix_expression = cp_parser_functional_cast (parser, type);
5938 break;
5940 case RID_CILK_SPAWN:
5942 cp_lexer_consume_token (parser->lexer);
5943 token = cp_lexer_peek_token (parser->lexer);
5944 if (token->type == CPP_SEMICOLON)
5946 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5947 "an expression");
5948 postfix_expression = error_mark_node;
5949 break;
5951 else if (!current_function_decl)
5953 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5954 "inside a function");
5955 postfix_expression = error_mark_node;
5956 break;
5958 else
5960 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5961 saved_in_statement = parser->in_statement;
5962 parser->in_statement |= IN_CILK_SPAWN;
5964 cfun->calls_cilk_spawn = 1;
5965 postfix_expression =
5966 cp_parser_postfix_expression (parser, false, false,
5967 false, false, &idk);
5968 if (!flag_cilkplus)
5970 error_at (token->location, "-fcilkplus must be enabled to use"
5971 " %<_Cilk_spawn%>");
5972 cfun->calls_cilk_spawn = 0;
5974 else if (saved_in_statement & IN_CILK_SPAWN)
5976 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5977 "are not permitted");
5978 postfix_expression = error_mark_node;
5979 cfun->calls_cilk_spawn = 0;
5981 else
5983 postfix_expression = build_cilk_spawn (token->location,
5984 postfix_expression);
5985 if (postfix_expression != error_mark_node)
5986 SET_EXPR_LOCATION (postfix_expression, input_location);
5987 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5989 break;
5992 case RID_BUILTIN_SHUFFLE:
5994 vec<tree, va_gc> *vec;
5995 unsigned int i;
5996 tree p;
5998 cp_lexer_consume_token (parser->lexer);
5999 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6000 /*cast_p=*/false, /*allow_expansion_p=*/true,
6001 /*non_constant_p=*/NULL);
6002 if (vec == NULL)
6003 return error_mark_node;
6005 FOR_EACH_VEC_ELT (*vec, i, p)
6006 mark_exp_read (p);
6008 if (vec->length () == 2)
6009 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6010 tf_warning_or_error);
6011 else if (vec->length () == 3)
6012 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6013 tf_warning_or_error);
6014 else
6016 error_at (loc, "wrong number of arguments to "
6017 "%<__builtin_shuffle%>");
6018 return error_mark_node;
6020 break;
6023 default:
6025 tree type;
6027 /* If the next thing is a simple-type-specifier, we may be
6028 looking at a functional cast. We could also be looking at
6029 an id-expression. So, we try the functional cast, and if
6030 that doesn't work we fall back to the primary-expression. */
6031 cp_parser_parse_tentatively (parser);
6032 /* Look for the simple-type-specifier. */
6033 type = cp_parser_simple_type_specifier (parser,
6034 /*decl_specs=*/NULL,
6035 CP_PARSER_FLAGS_NONE);
6036 /* Parse the cast itself. */
6037 if (!cp_parser_error_occurred (parser))
6038 postfix_expression
6039 = cp_parser_functional_cast (parser, type);
6040 /* If that worked, we're done. */
6041 if (cp_parser_parse_definitely (parser))
6042 break;
6044 /* If the functional-cast didn't work out, try a
6045 compound-literal. */
6046 if (cp_parser_allow_gnu_extensions_p (parser)
6047 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6049 tree initializer = NULL_TREE;
6051 cp_parser_parse_tentatively (parser);
6053 /* Avoid calling cp_parser_type_id pointlessly, see comment
6054 in cp_parser_cast_expression about c++/29234. */
6055 if (!cp_parser_compound_literal_p (parser))
6056 cp_parser_simulate_error (parser);
6057 else
6059 /* Parse the type. */
6060 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6061 parser->in_type_id_in_expr_p = true;
6062 type = cp_parser_type_id (parser);
6063 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6064 /* Look for the `)'. */
6065 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6068 /* If things aren't going well, there's no need to
6069 keep going. */
6070 if (!cp_parser_error_occurred (parser))
6072 bool non_constant_p;
6073 /* Parse the brace-enclosed initializer list. */
6074 initializer = cp_parser_braced_list (parser,
6075 &non_constant_p);
6077 /* If that worked, we're definitely looking at a
6078 compound-literal expression. */
6079 if (cp_parser_parse_definitely (parser))
6081 /* Warn the user that a compound literal is not
6082 allowed in standard C++. */
6083 pedwarn (input_location, OPT_Wpedantic,
6084 "ISO C++ forbids compound-literals");
6085 /* For simplicity, we disallow compound literals in
6086 constant-expressions. We could
6087 allow compound literals of integer type, whose
6088 initializer was a constant, in constant
6089 expressions. Permitting that usage, as a further
6090 extension, would not change the meaning of any
6091 currently accepted programs. (Of course, as
6092 compound literals are not part of ISO C++, the
6093 standard has nothing to say.) */
6094 if (cp_parser_non_integral_constant_expression (parser,
6095 NIC_NCC))
6097 postfix_expression = error_mark_node;
6098 break;
6100 /* Form the representation of the compound-literal. */
6101 postfix_expression
6102 = finish_compound_literal (type, initializer,
6103 tf_warning_or_error);
6104 break;
6108 /* It must be a primary-expression. */
6109 postfix_expression
6110 = cp_parser_primary_expression (parser, address_p, cast_p,
6111 /*template_arg_p=*/false,
6112 decltype_p,
6113 &idk);
6115 break;
6118 /* Note that we don't need to worry about calling build_cplus_new on a
6119 class-valued CALL_EXPR in decltype when it isn't the end of the
6120 postfix-expression; unary_complex_lvalue will take care of that for
6121 all these cases. */
6123 /* Keep looping until the postfix-expression is complete. */
6124 while (true)
6126 if (idk == CP_ID_KIND_UNQUALIFIED
6127 && identifier_p (postfix_expression)
6128 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6129 /* It is not a Koenig lookup function call. */
6130 postfix_expression
6131 = unqualified_name_lookup_error (postfix_expression);
6133 /* Peek at the next token. */
6134 token = cp_lexer_peek_token (parser->lexer);
6136 switch (token->type)
6138 case CPP_OPEN_SQUARE:
6139 if (cp_next_tokens_can_be_std_attribute_p (parser))
6141 cp_parser_error (parser,
6142 "two consecutive %<[%> shall "
6143 "only introduce an attribute");
6144 return error_mark_node;
6146 postfix_expression
6147 = cp_parser_postfix_open_square_expression (parser,
6148 postfix_expression,
6149 false,
6150 decltype_p);
6151 idk = CP_ID_KIND_NONE;
6152 is_member_access = false;
6153 break;
6155 case CPP_OPEN_PAREN:
6156 /* postfix-expression ( expression-list [opt] ) */
6158 bool koenig_p;
6159 bool is_builtin_constant_p;
6160 bool saved_integral_constant_expression_p = false;
6161 bool saved_non_integral_constant_expression_p = false;
6162 tsubst_flags_t complain = complain_flags (decltype_p);
6163 vec<tree, va_gc> *args;
6165 is_member_access = false;
6167 is_builtin_constant_p
6168 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6169 if (is_builtin_constant_p)
6171 /* The whole point of __builtin_constant_p is to allow
6172 non-constant expressions to appear as arguments. */
6173 saved_integral_constant_expression_p
6174 = parser->integral_constant_expression_p;
6175 saved_non_integral_constant_expression_p
6176 = parser->non_integral_constant_expression_p;
6177 parser->integral_constant_expression_p = false;
6179 args = (cp_parser_parenthesized_expression_list
6180 (parser, non_attr,
6181 /*cast_p=*/false, /*allow_expansion_p=*/true,
6182 /*non_constant_p=*/NULL,
6183 /*want_literal_zero_p=*/warn_memset_transposed_args));
6184 if (is_builtin_constant_p)
6186 parser->integral_constant_expression_p
6187 = saved_integral_constant_expression_p;
6188 parser->non_integral_constant_expression_p
6189 = saved_non_integral_constant_expression_p;
6192 if (args == NULL)
6194 postfix_expression = error_mark_node;
6195 break;
6198 /* Function calls are not permitted in
6199 constant-expressions. */
6200 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6201 && cp_parser_non_integral_constant_expression (parser,
6202 NIC_FUNC_CALL))
6204 postfix_expression = error_mark_node;
6205 release_tree_vector (args);
6206 break;
6209 koenig_p = false;
6210 if (idk == CP_ID_KIND_UNQUALIFIED
6211 || idk == CP_ID_KIND_TEMPLATE_ID)
6213 if (identifier_p (postfix_expression))
6215 if (!args->is_empty ())
6217 koenig_p = true;
6218 if (!any_type_dependent_arguments_p (args))
6219 postfix_expression
6220 = perform_koenig_lookup (postfix_expression, args,
6221 complain);
6223 else
6224 postfix_expression
6225 = unqualified_fn_lookup_error (postfix_expression);
6227 /* We do not perform argument-dependent lookup if
6228 normal lookup finds a non-function, in accordance
6229 with the expected resolution of DR 218. */
6230 else if (!args->is_empty ()
6231 && is_overloaded_fn (postfix_expression))
6233 tree fn = get_first_fn (postfix_expression);
6234 fn = STRIP_TEMPLATE (fn);
6236 /* Do not do argument dependent lookup if regular
6237 lookup finds a member function or a block-scope
6238 function declaration. [basic.lookup.argdep]/3 */
6239 if (!DECL_FUNCTION_MEMBER_P (fn)
6240 && !DECL_LOCAL_FUNCTION_P (fn))
6242 koenig_p = true;
6243 if (!any_type_dependent_arguments_p (args))
6244 postfix_expression
6245 = perform_koenig_lookup (postfix_expression, args,
6246 complain);
6251 if (warn_memset_transposed_args)
6253 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6254 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6255 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6256 && vec_safe_length (args) == 3
6257 && integer_zerop ((*args)[2])
6258 && LITERAL_ZERO_P ((*args)[2])
6259 && !(integer_zerop ((*args)[1])
6260 && LITERAL_ZERO_P ((*args)[1])))
6261 warning (OPT_Wmemset_transposed_args,
6262 "%<memset%> used with constant zero length "
6263 "parameter; this could be due to transposed "
6264 "parameters");
6266 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6267 to avoid leaking those into folder and middle-end. */
6268 unsigned int i;
6269 tree arg;
6270 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6271 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6272 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6275 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6277 tree instance = TREE_OPERAND (postfix_expression, 0);
6278 tree fn = TREE_OPERAND (postfix_expression, 1);
6280 if (processing_template_decl
6281 && (type_dependent_expression_p (instance)
6282 || (!BASELINK_P (fn)
6283 && TREE_CODE (fn) != FIELD_DECL)
6284 || type_dependent_expression_p (fn)
6285 || any_type_dependent_arguments_p (args)))
6287 postfix_expression
6288 = build_nt_call_vec (postfix_expression, args);
6289 release_tree_vector (args);
6290 break;
6293 if (BASELINK_P (fn))
6295 postfix_expression
6296 = (build_new_method_call
6297 (instance, fn, &args, NULL_TREE,
6298 (idk == CP_ID_KIND_QUALIFIED
6299 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6300 : LOOKUP_NORMAL),
6301 /*fn_p=*/NULL,
6302 complain));
6304 else
6305 postfix_expression
6306 = finish_call_expr (postfix_expression, &args,
6307 /*disallow_virtual=*/false,
6308 /*koenig_p=*/false,
6309 complain);
6311 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6312 || TREE_CODE (postfix_expression) == MEMBER_REF
6313 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6314 postfix_expression = (build_offset_ref_call_from_tree
6315 (postfix_expression, &args,
6316 complain));
6317 else if (idk == CP_ID_KIND_QUALIFIED)
6318 /* A call to a static class member, or a namespace-scope
6319 function. */
6320 postfix_expression
6321 = finish_call_expr (postfix_expression, &args,
6322 /*disallow_virtual=*/true,
6323 koenig_p,
6324 complain);
6325 else
6326 /* All other function calls. */
6327 postfix_expression
6328 = finish_call_expr (postfix_expression, &args,
6329 /*disallow_virtual=*/false,
6330 koenig_p,
6331 complain);
6333 protected_set_expr_location (postfix_expression, token->location);
6335 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6336 idk = CP_ID_KIND_NONE;
6338 release_tree_vector (args);
6340 break;
6342 case CPP_DOT:
6343 case CPP_DEREF:
6344 /* postfix-expression . template [opt] id-expression
6345 postfix-expression . pseudo-destructor-name
6346 postfix-expression -> template [opt] id-expression
6347 postfix-expression -> pseudo-destructor-name */
6349 /* Consume the `.' or `->' operator. */
6350 cp_lexer_consume_token (parser->lexer);
6352 postfix_expression
6353 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6354 postfix_expression,
6355 false, &idk, loc);
6357 is_member_access = true;
6358 break;
6360 case CPP_PLUS_PLUS:
6361 /* postfix-expression ++ */
6362 /* Consume the `++' token. */
6363 cp_lexer_consume_token (parser->lexer);
6364 /* Generate a representation for the complete expression. */
6365 postfix_expression
6366 = finish_increment_expr (postfix_expression,
6367 POSTINCREMENT_EXPR);
6368 /* Increments may not appear in constant-expressions. */
6369 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6370 postfix_expression = error_mark_node;
6371 idk = CP_ID_KIND_NONE;
6372 is_member_access = false;
6373 break;
6375 case CPP_MINUS_MINUS:
6376 /* postfix-expression -- */
6377 /* Consume the `--' token. */
6378 cp_lexer_consume_token (parser->lexer);
6379 /* Generate a representation for the complete expression. */
6380 postfix_expression
6381 = finish_increment_expr (postfix_expression,
6382 POSTDECREMENT_EXPR);
6383 /* Decrements may not appear in constant-expressions. */
6384 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6385 postfix_expression = error_mark_node;
6386 idk = CP_ID_KIND_NONE;
6387 is_member_access = false;
6388 break;
6390 default:
6391 if (pidk_return != NULL)
6392 * pidk_return = idk;
6393 if (member_access_only_p)
6394 return is_member_access? postfix_expression : error_mark_node;
6395 else
6396 return postfix_expression;
6400 /* We should never get here. */
6401 gcc_unreachable ();
6402 return error_mark_node;
6405 /* This function parses Cilk Plus array notations. If a normal array expr. is
6406 parsed then the array index is passed back to the caller through *INIT_INDEX
6407 and the function returns a NULL_TREE. If array notation expr. is parsed,
6408 then *INIT_INDEX is ignored by the caller and the function returns
6409 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6410 error_mark_node. */
6412 static tree
6413 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6414 tree array_value)
6416 cp_token *token = NULL;
6417 tree length_index, stride = NULL_TREE, value_tree, array_type;
6418 if (!array_value || array_value == error_mark_node)
6420 cp_parser_skip_to_end_of_statement (parser);
6421 return error_mark_node;
6424 array_type = TREE_TYPE (array_value);
6426 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6427 parser->colon_corrects_to_scope_p = false;
6428 token = cp_lexer_peek_token (parser->lexer);
6430 if (!token)
6432 cp_parser_error (parser, "expected %<:%> or numeral");
6433 return error_mark_node;
6435 else if (token->type == CPP_COLON)
6437 /* Consume the ':'. */
6438 cp_lexer_consume_token (parser->lexer);
6440 /* If we are here, then we have a case like this A[:]. */
6441 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6443 cp_parser_error (parser, "expected %<]%>");
6444 cp_parser_skip_to_end_of_statement (parser);
6445 return error_mark_node;
6447 *init_index = NULL_TREE;
6448 stride = NULL_TREE;
6449 length_index = NULL_TREE;
6451 else
6453 /* If we are here, then there are three valid possibilities:
6454 1. ARRAY [ EXP ]
6455 2. ARRAY [ EXP : EXP ]
6456 3. ARRAY [ EXP : EXP : EXP ] */
6458 *init_index = cp_parser_expression (parser);
6459 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6461 /* This indicates that we have a normal array expression. */
6462 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6463 return NULL_TREE;
6466 /* Consume the ':'. */
6467 cp_lexer_consume_token (parser->lexer);
6468 length_index = cp_parser_expression (parser);
6469 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6471 cp_lexer_consume_token (parser->lexer);
6472 stride = cp_parser_expression (parser);
6475 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6477 if (*init_index == error_mark_node || length_index == error_mark_node
6478 || stride == error_mark_node || array_type == error_mark_node)
6480 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6481 cp_lexer_consume_token (parser->lexer);
6482 return error_mark_node;
6484 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6486 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6487 length_index, stride, array_type);
6488 return value_tree;
6491 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6492 by cp_parser_builtin_offsetof. We're looking for
6494 postfix-expression [ expression ]
6495 postfix-expression [ braced-init-list ] (C++11)
6497 FOR_OFFSETOF is set if we're being called in that context, which
6498 changes how we deal with integer constant expressions. */
6500 static tree
6501 cp_parser_postfix_open_square_expression (cp_parser *parser,
6502 tree postfix_expression,
6503 bool for_offsetof,
6504 bool decltype_p)
6506 tree index = NULL_TREE;
6507 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6508 bool saved_greater_than_is_operator_p;
6510 /* Consume the `[' token. */
6511 cp_lexer_consume_token (parser->lexer);
6513 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6514 parser->greater_than_is_operator_p = true;
6516 /* Parse the index expression. */
6517 /* ??? For offsetof, there is a question of what to allow here. If
6518 offsetof is not being used in an integral constant expression context,
6519 then we *could* get the right answer by computing the value at runtime.
6520 If we are in an integral constant expression context, then we might
6521 could accept any constant expression; hard to say without analysis.
6522 Rather than open the barn door too wide right away, allow only integer
6523 constant expressions here. */
6524 if (for_offsetof)
6525 index = cp_parser_constant_expression (parser);
6526 else
6528 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6530 bool expr_nonconst_p;
6531 cp_lexer_set_source_position (parser->lexer);
6532 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6533 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6534 if (flag_cilkplus
6535 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6537 error_at (cp_lexer_peek_token (parser->lexer)->location,
6538 "braced list index is not allowed with array "
6539 "notation");
6540 cp_parser_skip_to_end_of_statement (parser);
6541 return error_mark_node;
6544 else if (flag_cilkplus)
6546 /* Here are have these two options:
6547 ARRAY[EXP : EXP] - Array notation expr with default
6548 stride of 1.
6549 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6550 stride. */
6551 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6552 postfix_expression);
6553 if (an_exp)
6554 return an_exp;
6556 else
6557 index = cp_parser_expression (parser);
6560 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6562 /* Look for the closing `]'. */
6563 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6565 /* Build the ARRAY_REF. */
6566 postfix_expression = grok_array_decl (loc, postfix_expression,
6567 index, decltype_p);
6569 /* When not doing offsetof, array references are not permitted in
6570 constant-expressions. */
6571 if (!for_offsetof
6572 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6573 postfix_expression = error_mark_node;
6575 return postfix_expression;
6578 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6579 by cp_parser_builtin_offsetof. We're looking for
6581 postfix-expression . template [opt] id-expression
6582 postfix-expression . pseudo-destructor-name
6583 postfix-expression -> template [opt] id-expression
6584 postfix-expression -> pseudo-destructor-name
6586 FOR_OFFSETOF is set if we're being called in that context. That sorta
6587 limits what of the above we'll actually accept, but nevermind.
6588 TOKEN_TYPE is the "." or "->" token, which will already have been
6589 removed from the stream. */
6591 static tree
6592 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6593 enum cpp_ttype token_type,
6594 tree postfix_expression,
6595 bool for_offsetof, cp_id_kind *idk,
6596 location_t location)
6598 tree name;
6599 bool dependent_p;
6600 bool pseudo_destructor_p;
6601 tree scope = NULL_TREE;
6603 /* If this is a `->' operator, dereference the pointer. */
6604 if (token_type == CPP_DEREF)
6605 postfix_expression = build_x_arrow (location, postfix_expression,
6606 tf_warning_or_error);
6607 /* Check to see whether or not the expression is type-dependent. */
6608 dependent_p = type_dependent_expression_p (postfix_expression);
6609 /* The identifier following the `->' or `.' is not qualified. */
6610 parser->scope = NULL_TREE;
6611 parser->qualifying_scope = NULL_TREE;
6612 parser->object_scope = NULL_TREE;
6613 *idk = CP_ID_KIND_NONE;
6615 /* Enter the scope corresponding to the type of the object
6616 given by the POSTFIX_EXPRESSION. */
6617 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6619 scope = TREE_TYPE (postfix_expression);
6620 /* According to the standard, no expression should ever have
6621 reference type. Unfortunately, we do not currently match
6622 the standard in this respect in that our internal representation
6623 of an expression may have reference type even when the standard
6624 says it does not. Therefore, we have to manually obtain the
6625 underlying type here. */
6626 scope = non_reference (scope);
6627 /* The type of the POSTFIX_EXPRESSION must be complete. */
6628 if (scope == unknown_type_node)
6630 error_at (location, "%qE does not have class type",
6631 postfix_expression);
6632 scope = NULL_TREE;
6634 /* Unlike the object expression in other contexts, *this is not
6635 required to be of complete type for purposes of class member
6636 access (5.2.5) outside the member function body. */
6637 else if (postfix_expression != current_class_ref
6638 && !(processing_template_decl && scope == current_class_type))
6639 scope = complete_type_or_else (scope, NULL_TREE);
6640 /* Let the name lookup machinery know that we are processing a
6641 class member access expression. */
6642 parser->context->object_type = scope;
6643 /* If something went wrong, we want to be able to discern that case,
6644 as opposed to the case where there was no SCOPE due to the type
6645 of expression being dependent. */
6646 if (!scope)
6647 scope = error_mark_node;
6648 /* If the SCOPE was erroneous, make the various semantic analysis
6649 functions exit quickly -- and without issuing additional error
6650 messages. */
6651 if (scope == error_mark_node)
6652 postfix_expression = error_mark_node;
6655 /* Assume this expression is not a pseudo-destructor access. */
6656 pseudo_destructor_p = false;
6658 /* If the SCOPE is a scalar type, then, if this is a valid program,
6659 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6660 is type dependent, it can be pseudo-destructor-name or something else.
6661 Try to parse it as pseudo-destructor-name first. */
6662 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6664 tree s;
6665 tree type;
6667 cp_parser_parse_tentatively (parser);
6668 /* Parse the pseudo-destructor-name. */
6669 s = NULL_TREE;
6670 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6671 &s, &type);
6672 if (dependent_p
6673 && (cp_parser_error_occurred (parser)
6674 || !SCALAR_TYPE_P (type)))
6675 cp_parser_abort_tentative_parse (parser);
6676 else if (cp_parser_parse_definitely (parser))
6678 pseudo_destructor_p = true;
6679 postfix_expression
6680 = finish_pseudo_destructor_expr (postfix_expression,
6681 s, type, location);
6685 if (!pseudo_destructor_p)
6687 /* If the SCOPE is not a scalar type, we are looking at an
6688 ordinary class member access expression, rather than a
6689 pseudo-destructor-name. */
6690 bool template_p;
6691 cp_token *token = cp_lexer_peek_token (parser->lexer);
6692 /* Parse the id-expression. */
6693 name = (cp_parser_id_expression
6694 (parser,
6695 cp_parser_optional_template_keyword (parser),
6696 /*check_dependency_p=*/true,
6697 &template_p,
6698 /*declarator_p=*/false,
6699 /*optional_p=*/false));
6700 /* In general, build a SCOPE_REF if the member name is qualified.
6701 However, if the name was not dependent and has already been
6702 resolved; there is no need to build the SCOPE_REF. For example;
6704 struct X { void f(); };
6705 template <typename T> void f(T* t) { t->X::f(); }
6707 Even though "t" is dependent, "X::f" is not and has been resolved
6708 to a BASELINK; there is no need to include scope information. */
6710 /* But we do need to remember that there was an explicit scope for
6711 virtual function calls. */
6712 if (parser->scope)
6713 *idk = CP_ID_KIND_QUALIFIED;
6715 /* If the name is a template-id that names a type, we will get a
6716 TYPE_DECL here. That is invalid code. */
6717 if (TREE_CODE (name) == TYPE_DECL)
6719 error_at (token->location, "invalid use of %qD", name);
6720 postfix_expression = error_mark_node;
6722 else
6724 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6726 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6728 error_at (token->location, "%<%D::%D%> is not a class member",
6729 parser->scope, name);
6730 postfix_expression = error_mark_node;
6732 else
6733 name = build_qualified_name (/*type=*/NULL_TREE,
6734 parser->scope,
6735 name,
6736 template_p);
6737 parser->scope = NULL_TREE;
6738 parser->qualifying_scope = NULL_TREE;
6739 parser->object_scope = NULL_TREE;
6741 if (parser->scope && name && BASELINK_P (name))
6742 adjust_result_of_qualified_name_lookup
6743 (name, parser->scope, scope);
6744 postfix_expression
6745 = finish_class_member_access_expr (postfix_expression, name,
6746 template_p,
6747 tf_warning_or_error);
6751 /* We no longer need to look up names in the scope of the object on
6752 the left-hand side of the `.' or `->' operator. */
6753 parser->context->object_type = NULL_TREE;
6755 /* Outside of offsetof, these operators may not appear in
6756 constant-expressions. */
6757 if (!for_offsetof
6758 && (cp_parser_non_integral_constant_expression
6759 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6760 postfix_expression = error_mark_node;
6762 return postfix_expression;
6765 /* Cache of LITERAL_ZERO_P constants. */
6767 static GTY(()) tree literal_zeros[itk_none];
6769 /* Parse a parenthesized expression-list.
6771 expression-list:
6772 assignment-expression
6773 expression-list, assignment-expression
6775 attribute-list:
6776 expression-list
6777 identifier
6778 identifier, expression-list
6780 CAST_P is true if this expression is the target of a cast.
6782 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6783 argument pack.
6785 Returns a vector of trees. Each element is a representation of an
6786 assignment-expression. NULL is returned if the ( and or ) are
6787 missing. An empty, but allocated, vector is returned on no
6788 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6789 if we are parsing an attribute list for an attribute that wants a
6790 plain identifier argument, normal_attr for an attribute that wants
6791 an expression, or non_attr if we aren't parsing an attribute list. If
6792 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6793 not all of the expressions in the list were constant.
6794 WANT_LITERAL_ZERO_P is true if the caller is interested in
6795 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6796 immediately, this can be removed. */
6798 static vec<tree, va_gc> *
6799 cp_parser_parenthesized_expression_list (cp_parser* parser,
6800 int is_attribute_list,
6801 bool cast_p,
6802 bool allow_expansion_p,
6803 bool *non_constant_p,
6804 bool want_literal_zero_p)
6806 vec<tree, va_gc> *expression_list;
6807 bool fold_expr_p = is_attribute_list != non_attr;
6808 tree identifier = NULL_TREE;
6809 bool saved_greater_than_is_operator_p;
6811 /* Assume all the expressions will be constant. */
6812 if (non_constant_p)
6813 *non_constant_p = false;
6815 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6816 return NULL;
6818 expression_list = make_tree_vector ();
6820 /* Within a parenthesized expression, a `>' token is always
6821 the greater-than operator. */
6822 saved_greater_than_is_operator_p
6823 = parser->greater_than_is_operator_p;
6824 parser->greater_than_is_operator_p = true;
6826 /* Consume expressions until there are no more. */
6827 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6828 while (true)
6830 tree expr;
6832 /* At the beginning of attribute lists, check to see if the
6833 next token is an identifier. */
6834 if (is_attribute_list == id_attr
6835 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6837 cp_token *token;
6839 /* Consume the identifier. */
6840 token = cp_lexer_consume_token (parser->lexer);
6841 /* Save the identifier. */
6842 identifier = token->u.value;
6844 else
6846 bool expr_non_constant_p;
6848 /* Parse the next assignment-expression. */
6849 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6851 /* A braced-init-list. */
6852 cp_lexer_set_source_position (parser->lexer);
6853 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6854 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6855 if (non_constant_p && expr_non_constant_p)
6856 *non_constant_p = true;
6858 else if (non_constant_p)
6860 expr = (cp_parser_constant_expression
6861 (parser, /*allow_non_constant_p=*/true,
6862 &expr_non_constant_p));
6863 if (expr_non_constant_p)
6864 *non_constant_p = true;
6866 else
6868 expr = NULL_TREE;
6869 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6870 switch (tok->type)
6872 case CPP_NUMBER:
6873 case CPP_CHAR:
6874 case CPP_WCHAR:
6875 case CPP_CHAR16:
6876 case CPP_CHAR32:
6877 /* If a parameter is literal zero alone, remember it
6878 for -Wmemset-transposed-args warning. */
6879 if (integer_zerop (tok->u.value)
6880 && !TREE_OVERFLOW (tok->u.value)
6881 && want_literal_zero_p
6882 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6883 == CPP_COMMA
6884 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6885 == CPP_CLOSE_PAREN))
6887 unsigned int i;
6888 for (i = 0; i < itk_none; ++i)
6889 if (TREE_TYPE (tok->u.value) == integer_types[i])
6890 break;
6891 if (i < itk_none && literal_zeros[i])
6892 expr = literal_zeros[i];
6893 else
6895 expr = copy_node (tok->u.value);
6896 LITERAL_ZERO_P (expr) = 1;
6897 if (i < itk_none)
6898 literal_zeros[i] = expr;
6900 /* Consume the 0 token (or '\0', 0LL etc.). */
6901 cp_lexer_consume_token (parser->lexer);
6903 break;
6904 default:
6905 break;
6907 if (expr == NULL_TREE)
6908 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6909 cast_p);
6912 if (fold_expr_p)
6913 expr = instantiate_non_dependent_expr (expr);
6915 /* If we have an ellipsis, then this is an expression
6916 expansion. */
6917 if (allow_expansion_p
6918 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6920 /* Consume the `...'. */
6921 cp_lexer_consume_token (parser->lexer);
6923 /* Build the argument pack. */
6924 expr = make_pack_expansion (expr);
6927 /* Add it to the list. We add error_mark_node
6928 expressions to the list, so that we can still tell if
6929 the correct form for a parenthesized expression-list
6930 is found. That gives better errors. */
6931 vec_safe_push (expression_list, expr);
6933 if (expr == error_mark_node)
6934 goto skip_comma;
6937 /* After the first item, attribute lists look the same as
6938 expression lists. */
6939 is_attribute_list = non_attr;
6941 get_comma:;
6942 /* If the next token isn't a `,', then we are done. */
6943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6944 break;
6946 /* Otherwise, consume the `,' and keep going. */
6947 cp_lexer_consume_token (parser->lexer);
6950 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6952 int ending;
6954 skip_comma:;
6955 /* We try and resync to an unnested comma, as that will give the
6956 user better diagnostics. */
6957 ending = cp_parser_skip_to_closing_parenthesis (parser,
6958 /*recovering=*/true,
6959 /*or_comma=*/true,
6960 /*consume_paren=*/true);
6961 if (ending < 0)
6962 goto get_comma;
6963 if (!ending)
6965 parser->greater_than_is_operator_p
6966 = saved_greater_than_is_operator_p;
6967 return NULL;
6971 parser->greater_than_is_operator_p
6972 = saved_greater_than_is_operator_p;
6974 if (identifier)
6975 vec_safe_insert (expression_list, 0, identifier);
6977 return expression_list;
6980 /* Parse a pseudo-destructor-name.
6982 pseudo-destructor-name:
6983 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6984 :: [opt] nested-name-specifier template template-id :: ~ type-name
6985 :: [opt] nested-name-specifier [opt] ~ type-name
6987 If either of the first two productions is used, sets *SCOPE to the
6988 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6989 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6990 or ERROR_MARK_NODE if the parse fails. */
6992 static void
6993 cp_parser_pseudo_destructor_name (cp_parser* parser,
6994 tree object,
6995 tree* scope,
6996 tree* type)
6998 bool nested_name_specifier_p;
7000 /* Handle ~auto. */
7001 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7002 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7003 && !type_dependent_expression_p (object))
7005 if (cxx_dialect < cxx14)
7006 pedwarn (input_location, 0,
7007 "%<~auto%> only available with "
7008 "-std=c++14 or -std=gnu++14");
7009 cp_lexer_consume_token (parser->lexer);
7010 cp_lexer_consume_token (parser->lexer);
7011 *scope = NULL_TREE;
7012 *type = TREE_TYPE (object);
7013 return;
7016 /* Assume that things will not work out. */
7017 *type = error_mark_node;
7019 /* Look for the optional `::' operator. */
7020 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7021 /* Look for the optional nested-name-specifier. */
7022 nested_name_specifier_p
7023 = (cp_parser_nested_name_specifier_opt (parser,
7024 /*typename_keyword_p=*/false,
7025 /*check_dependency_p=*/true,
7026 /*type_p=*/false,
7027 /*is_declaration=*/false)
7028 != NULL_TREE);
7029 /* Now, if we saw a nested-name-specifier, we might be doing the
7030 second production. */
7031 if (nested_name_specifier_p
7032 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7034 /* Consume the `template' keyword. */
7035 cp_lexer_consume_token (parser->lexer);
7036 /* Parse the template-id. */
7037 cp_parser_template_id (parser,
7038 /*template_keyword_p=*/true,
7039 /*check_dependency_p=*/false,
7040 class_type,
7041 /*is_declaration=*/true);
7042 /* Look for the `::' token. */
7043 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7045 /* If the next token is not a `~', then there might be some
7046 additional qualification. */
7047 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7049 /* At this point, we're looking for "type-name :: ~". The type-name
7050 must not be a class-name, since this is a pseudo-destructor. So,
7051 it must be either an enum-name, or a typedef-name -- both of which
7052 are just identifiers. So, we peek ahead to check that the "::"
7053 and "~" tokens are present; if they are not, then we can avoid
7054 calling type_name. */
7055 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7056 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7057 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7059 cp_parser_error (parser, "non-scalar type");
7060 return;
7063 /* Look for the type-name. */
7064 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7065 if (*scope == error_mark_node)
7066 return;
7068 /* Look for the `::' token. */
7069 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7071 else
7072 *scope = NULL_TREE;
7074 /* Look for the `~'. */
7075 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7077 /* Once we see the ~, this has to be a pseudo-destructor. */
7078 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7079 cp_parser_commit_to_topmost_tentative_parse (parser);
7081 /* Look for the type-name again. We are not responsible for
7082 checking that it matches the first type-name. */
7083 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7086 /* Parse a unary-expression.
7088 unary-expression:
7089 postfix-expression
7090 ++ cast-expression
7091 -- cast-expression
7092 unary-operator cast-expression
7093 sizeof unary-expression
7094 sizeof ( type-id )
7095 alignof ( type-id ) [C++0x]
7096 new-expression
7097 delete-expression
7099 GNU Extensions:
7101 unary-expression:
7102 __extension__ cast-expression
7103 __alignof__ unary-expression
7104 __alignof__ ( type-id )
7105 alignof unary-expression [C++0x]
7106 __real__ cast-expression
7107 __imag__ cast-expression
7108 && identifier
7109 sizeof ( type-id ) { initializer-list , [opt] }
7110 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7111 __alignof__ ( type-id ) { initializer-list , [opt] }
7113 ADDRESS_P is true iff the unary-expression is appearing as the
7114 operand of the `&' operator. CAST_P is true if this expression is
7115 the target of a cast.
7117 Returns a representation of the expression. */
7119 static tree
7120 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7121 bool address_p, bool cast_p, bool decltype_p)
7123 cp_token *token;
7124 enum tree_code unary_operator;
7126 /* Peek at the next token. */
7127 token = cp_lexer_peek_token (parser->lexer);
7128 /* Some keywords give away the kind of expression. */
7129 if (token->type == CPP_KEYWORD)
7131 enum rid keyword = token->keyword;
7133 switch (keyword)
7135 case RID_ALIGNOF:
7136 case RID_SIZEOF:
7138 tree operand, ret;
7139 enum tree_code op;
7140 location_t first_loc;
7142 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7143 /* Consume the token. */
7144 cp_lexer_consume_token (parser->lexer);
7145 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7146 /* Parse the operand. */
7147 operand = cp_parser_sizeof_operand (parser, keyword);
7149 if (TYPE_P (operand))
7150 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7151 else
7153 /* ISO C++ defines alignof only with types, not with
7154 expressions. So pedwarn if alignof is used with a non-
7155 type expression. However, __alignof__ is ok. */
7156 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7157 pedwarn (token->location, OPT_Wpedantic,
7158 "ISO C++ does not allow %<alignof%> "
7159 "with a non-type");
7161 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7163 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7164 SIZEOF_EXPR with the original operand. */
7165 if (op == SIZEOF_EXPR && ret != error_mark_node)
7167 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7169 if (!processing_template_decl && TYPE_P (operand))
7171 ret = build_min (SIZEOF_EXPR, size_type_node,
7172 build1 (NOP_EXPR, operand,
7173 error_mark_node));
7174 SIZEOF_EXPR_TYPE_P (ret) = 1;
7176 else
7177 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7178 TREE_SIDE_EFFECTS (ret) = 0;
7179 TREE_READONLY (ret) = 1;
7181 SET_EXPR_LOCATION (ret, first_loc);
7183 return ret;
7186 case RID_NEW:
7187 return cp_parser_new_expression (parser);
7189 case RID_DELETE:
7190 return cp_parser_delete_expression (parser);
7192 case RID_EXTENSION:
7194 /* The saved value of the PEDANTIC flag. */
7195 int saved_pedantic;
7196 tree expr;
7198 /* Save away the PEDANTIC flag. */
7199 cp_parser_extension_opt (parser, &saved_pedantic);
7200 /* Parse the cast-expression. */
7201 expr = cp_parser_simple_cast_expression (parser);
7202 /* Restore the PEDANTIC flag. */
7203 pedantic = saved_pedantic;
7205 return expr;
7208 case RID_REALPART:
7209 case RID_IMAGPART:
7211 tree expression;
7213 /* Consume the `__real__' or `__imag__' token. */
7214 cp_lexer_consume_token (parser->lexer);
7215 /* Parse the cast-expression. */
7216 expression = cp_parser_simple_cast_expression (parser);
7217 /* Create the complete representation. */
7218 return build_x_unary_op (token->location,
7219 (keyword == RID_REALPART
7220 ? REALPART_EXPR : IMAGPART_EXPR),
7221 expression,
7222 tf_warning_or_error);
7224 break;
7226 case RID_TRANSACTION_ATOMIC:
7227 case RID_TRANSACTION_RELAXED:
7228 return cp_parser_transaction_expression (parser, keyword);
7230 case RID_NOEXCEPT:
7232 tree expr;
7233 const char *saved_message;
7234 bool saved_integral_constant_expression_p;
7235 bool saved_non_integral_constant_expression_p;
7236 bool saved_greater_than_is_operator_p;
7238 cp_lexer_consume_token (parser->lexer);
7239 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7241 saved_message = parser->type_definition_forbidden_message;
7242 parser->type_definition_forbidden_message
7243 = G_("types may not be defined in %<noexcept%> expressions");
7245 saved_integral_constant_expression_p
7246 = parser->integral_constant_expression_p;
7247 saved_non_integral_constant_expression_p
7248 = parser->non_integral_constant_expression_p;
7249 parser->integral_constant_expression_p = false;
7251 saved_greater_than_is_operator_p
7252 = parser->greater_than_is_operator_p;
7253 parser->greater_than_is_operator_p = true;
7255 ++cp_unevaluated_operand;
7256 ++c_inhibit_evaluation_warnings;
7257 ++cp_noexcept_operand;
7258 expr = cp_parser_expression (parser);
7259 --cp_noexcept_operand;
7260 --c_inhibit_evaluation_warnings;
7261 --cp_unevaluated_operand;
7263 parser->greater_than_is_operator_p
7264 = saved_greater_than_is_operator_p;
7266 parser->integral_constant_expression_p
7267 = saved_integral_constant_expression_p;
7268 parser->non_integral_constant_expression_p
7269 = saved_non_integral_constant_expression_p;
7271 parser->type_definition_forbidden_message = saved_message;
7273 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7274 return finish_noexcept_expr (expr, tf_warning_or_error);
7277 default:
7278 break;
7282 /* Look for the `:: new' and `:: delete', which also signal the
7283 beginning of a new-expression, or delete-expression,
7284 respectively. If the next token is `::', then it might be one of
7285 these. */
7286 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7288 enum rid keyword;
7290 /* See if the token after the `::' is one of the keywords in
7291 which we're interested. */
7292 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7293 /* If it's `new', we have a new-expression. */
7294 if (keyword == RID_NEW)
7295 return cp_parser_new_expression (parser);
7296 /* Similarly, for `delete'. */
7297 else if (keyword == RID_DELETE)
7298 return cp_parser_delete_expression (parser);
7301 /* Look for a unary operator. */
7302 unary_operator = cp_parser_unary_operator (token);
7303 /* The `++' and `--' operators can be handled similarly, even though
7304 they are not technically unary-operators in the grammar. */
7305 if (unary_operator == ERROR_MARK)
7307 if (token->type == CPP_PLUS_PLUS)
7308 unary_operator = PREINCREMENT_EXPR;
7309 else if (token->type == CPP_MINUS_MINUS)
7310 unary_operator = PREDECREMENT_EXPR;
7311 /* Handle the GNU address-of-label extension. */
7312 else if (cp_parser_allow_gnu_extensions_p (parser)
7313 && token->type == CPP_AND_AND)
7315 tree identifier;
7316 tree expression;
7317 location_t loc = token->location;
7319 /* Consume the '&&' token. */
7320 cp_lexer_consume_token (parser->lexer);
7321 /* Look for the identifier. */
7322 identifier = cp_parser_identifier (parser);
7323 /* Create an expression representing the address. */
7324 expression = finish_label_address_expr (identifier, loc);
7325 if (cp_parser_non_integral_constant_expression (parser,
7326 NIC_ADDR_LABEL))
7327 expression = error_mark_node;
7328 return expression;
7331 if (unary_operator != ERROR_MARK)
7333 tree cast_expression;
7334 tree expression = error_mark_node;
7335 non_integral_constant non_constant_p = NIC_NONE;
7336 location_t loc = token->location;
7337 tsubst_flags_t complain = complain_flags (decltype_p);
7339 /* Consume the operator token. */
7340 token = cp_lexer_consume_token (parser->lexer);
7341 /* Parse the cast-expression. */
7342 cast_expression
7343 = cp_parser_cast_expression (parser,
7344 unary_operator == ADDR_EXPR,
7345 /*cast_p=*/false,
7346 /*decltype*/false,
7347 pidk);
7348 /* Now, build an appropriate representation. */
7349 switch (unary_operator)
7351 case INDIRECT_REF:
7352 non_constant_p = NIC_STAR;
7353 expression = build_x_indirect_ref (loc, cast_expression,
7354 RO_UNARY_STAR,
7355 complain);
7356 break;
7358 case ADDR_EXPR:
7359 non_constant_p = NIC_ADDR;
7360 /* Fall through. */
7361 case BIT_NOT_EXPR:
7362 expression = build_x_unary_op (loc, unary_operator,
7363 cast_expression,
7364 complain);
7365 break;
7367 case PREINCREMENT_EXPR:
7368 case PREDECREMENT_EXPR:
7369 non_constant_p = unary_operator == PREINCREMENT_EXPR
7370 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7371 /* Fall through. */
7372 case UNARY_PLUS_EXPR:
7373 case NEGATE_EXPR:
7374 case TRUTH_NOT_EXPR:
7375 expression = finish_unary_op_expr (loc, unary_operator,
7376 cast_expression, complain);
7377 break;
7379 default:
7380 gcc_unreachable ();
7383 if (non_constant_p != NIC_NONE
7384 && cp_parser_non_integral_constant_expression (parser,
7385 non_constant_p))
7386 expression = error_mark_node;
7388 return expression;
7391 return cp_parser_postfix_expression (parser, address_p, cast_p,
7392 /*member_access_only_p=*/false,
7393 decltype_p,
7394 pidk);
7397 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7398 unary-operator, the corresponding tree code is returned. */
7400 static enum tree_code
7401 cp_parser_unary_operator (cp_token* token)
7403 switch (token->type)
7405 case CPP_MULT:
7406 return INDIRECT_REF;
7408 case CPP_AND:
7409 return ADDR_EXPR;
7411 case CPP_PLUS:
7412 return UNARY_PLUS_EXPR;
7414 case CPP_MINUS:
7415 return NEGATE_EXPR;
7417 case CPP_NOT:
7418 return TRUTH_NOT_EXPR;
7420 case CPP_COMPL:
7421 return BIT_NOT_EXPR;
7423 default:
7424 return ERROR_MARK;
7428 /* Parse a new-expression.
7430 new-expression:
7431 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7432 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7434 Returns a representation of the expression. */
7436 static tree
7437 cp_parser_new_expression (cp_parser* parser)
7439 bool global_scope_p;
7440 vec<tree, va_gc> *placement;
7441 tree type;
7442 vec<tree, va_gc> *initializer;
7443 tree nelts = NULL_TREE;
7444 tree ret;
7446 /* Look for the optional `::' operator. */
7447 global_scope_p
7448 = (cp_parser_global_scope_opt (parser,
7449 /*current_scope_valid_p=*/false)
7450 != NULL_TREE);
7451 /* Look for the `new' operator. */
7452 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7453 /* There's no easy way to tell a new-placement from the
7454 `( type-id )' construct. */
7455 cp_parser_parse_tentatively (parser);
7456 /* Look for a new-placement. */
7457 placement = cp_parser_new_placement (parser);
7458 /* If that didn't work out, there's no new-placement. */
7459 if (!cp_parser_parse_definitely (parser))
7461 if (placement != NULL)
7462 release_tree_vector (placement);
7463 placement = NULL;
7466 /* If the next token is a `(', then we have a parenthesized
7467 type-id. */
7468 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7470 cp_token *token;
7471 const char *saved_message = parser->type_definition_forbidden_message;
7473 /* Consume the `('. */
7474 cp_lexer_consume_token (parser->lexer);
7476 /* Parse the type-id. */
7477 parser->type_definition_forbidden_message
7478 = G_("types may not be defined in a new-expression");
7479 type = cp_parser_type_id (parser);
7480 parser->type_definition_forbidden_message = saved_message;
7482 /* Look for the closing `)'. */
7483 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7484 token = cp_lexer_peek_token (parser->lexer);
7485 /* There should not be a direct-new-declarator in this production,
7486 but GCC used to allowed this, so we check and emit a sensible error
7487 message for this case. */
7488 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7490 error_at (token->location,
7491 "array bound forbidden after parenthesized type-id");
7492 inform (token->location,
7493 "try removing the parentheses around the type-id");
7494 cp_parser_direct_new_declarator (parser);
7497 /* Otherwise, there must be a new-type-id. */
7498 else
7499 type = cp_parser_new_type_id (parser, &nelts);
7501 /* If the next token is a `(' or '{', then we have a new-initializer. */
7502 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7503 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7504 initializer = cp_parser_new_initializer (parser);
7505 else
7506 initializer = NULL;
7508 /* A new-expression may not appear in an integral constant
7509 expression. */
7510 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7511 ret = error_mark_node;
7512 else
7514 /* Create a representation of the new-expression. */
7515 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7516 tf_warning_or_error);
7519 if (placement != NULL)
7520 release_tree_vector (placement);
7521 if (initializer != NULL)
7522 release_tree_vector (initializer);
7524 return ret;
7527 /* Parse a new-placement.
7529 new-placement:
7530 ( expression-list )
7532 Returns the same representation as for an expression-list. */
7534 static vec<tree, va_gc> *
7535 cp_parser_new_placement (cp_parser* parser)
7537 vec<tree, va_gc> *expression_list;
7539 /* Parse the expression-list. */
7540 expression_list = (cp_parser_parenthesized_expression_list
7541 (parser, non_attr, /*cast_p=*/false,
7542 /*allow_expansion_p=*/true,
7543 /*non_constant_p=*/NULL));
7545 return expression_list;
7548 /* Parse a new-type-id.
7550 new-type-id:
7551 type-specifier-seq new-declarator [opt]
7553 Returns the TYPE allocated. If the new-type-id indicates an array
7554 type, *NELTS is set to the number of elements in the last array
7555 bound; the TYPE will not include the last array bound. */
7557 static tree
7558 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7560 cp_decl_specifier_seq type_specifier_seq;
7561 cp_declarator *new_declarator;
7562 cp_declarator *declarator;
7563 cp_declarator *outer_declarator;
7564 const char *saved_message;
7566 /* The type-specifier sequence must not contain type definitions.
7567 (It cannot contain declarations of new types either, but if they
7568 are not definitions we will catch that because they are not
7569 complete.) */
7570 saved_message = parser->type_definition_forbidden_message;
7571 parser->type_definition_forbidden_message
7572 = G_("types may not be defined in a new-type-id");
7573 /* Parse the type-specifier-seq. */
7574 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7575 /*is_trailing_return=*/false,
7576 &type_specifier_seq);
7577 /* Restore the old message. */
7578 parser->type_definition_forbidden_message = saved_message;
7580 if (type_specifier_seq.type == error_mark_node)
7581 return error_mark_node;
7583 /* Parse the new-declarator. */
7584 new_declarator = cp_parser_new_declarator_opt (parser);
7586 /* Determine the number of elements in the last array dimension, if
7587 any. */
7588 *nelts = NULL_TREE;
7589 /* Skip down to the last array dimension. */
7590 declarator = new_declarator;
7591 outer_declarator = NULL;
7592 while (declarator && (declarator->kind == cdk_pointer
7593 || declarator->kind == cdk_ptrmem))
7595 outer_declarator = declarator;
7596 declarator = declarator->declarator;
7598 while (declarator
7599 && declarator->kind == cdk_array
7600 && declarator->declarator
7601 && declarator->declarator->kind == cdk_array)
7603 outer_declarator = declarator;
7604 declarator = declarator->declarator;
7607 if (declarator && declarator->kind == cdk_array)
7609 *nelts = declarator->u.array.bounds;
7610 if (*nelts == error_mark_node)
7611 *nelts = integer_one_node;
7613 if (outer_declarator)
7614 outer_declarator->declarator = declarator->declarator;
7615 else
7616 new_declarator = NULL;
7619 return groktypename (&type_specifier_seq, new_declarator, false);
7622 /* Parse an (optional) new-declarator.
7624 new-declarator:
7625 ptr-operator new-declarator [opt]
7626 direct-new-declarator
7628 Returns the declarator. */
7630 static cp_declarator *
7631 cp_parser_new_declarator_opt (cp_parser* parser)
7633 enum tree_code code;
7634 tree type, std_attributes = NULL_TREE;
7635 cp_cv_quals cv_quals;
7637 /* We don't know if there's a ptr-operator next, or not. */
7638 cp_parser_parse_tentatively (parser);
7639 /* Look for a ptr-operator. */
7640 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7641 /* If that worked, look for more new-declarators. */
7642 if (cp_parser_parse_definitely (parser))
7644 cp_declarator *declarator;
7646 /* Parse another optional declarator. */
7647 declarator = cp_parser_new_declarator_opt (parser);
7649 declarator = cp_parser_make_indirect_declarator
7650 (code, type, cv_quals, declarator, std_attributes);
7652 return declarator;
7655 /* If the next token is a `[', there is a direct-new-declarator. */
7656 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7657 return cp_parser_direct_new_declarator (parser);
7659 return NULL;
7662 /* Parse a direct-new-declarator.
7664 direct-new-declarator:
7665 [ expression ]
7666 direct-new-declarator [constant-expression]
7670 static cp_declarator *
7671 cp_parser_direct_new_declarator (cp_parser* parser)
7673 cp_declarator *declarator = NULL;
7675 while (true)
7677 tree expression;
7678 cp_token *token;
7680 /* Look for the opening `['. */
7681 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7683 token = cp_lexer_peek_token (parser->lexer);
7684 expression = cp_parser_expression (parser);
7685 /* The standard requires that the expression have integral
7686 type. DR 74 adds enumeration types. We believe that the
7687 real intent is that these expressions be handled like the
7688 expression in a `switch' condition, which also allows
7689 classes with a single conversion to integral or
7690 enumeration type. */
7691 if (!processing_template_decl)
7693 expression
7694 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7695 expression,
7696 /*complain=*/true);
7697 if (!expression)
7699 error_at (token->location,
7700 "expression in new-declarator must have integral "
7701 "or enumeration type");
7702 expression = error_mark_node;
7706 /* Look for the closing `]'. */
7707 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7709 /* Add this bound to the declarator. */
7710 declarator = make_array_declarator (declarator, expression);
7712 /* If the next token is not a `[', then there are no more
7713 bounds. */
7714 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7715 break;
7718 return declarator;
7721 /* Parse a new-initializer.
7723 new-initializer:
7724 ( expression-list [opt] )
7725 braced-init-list
7727 Returns a representation of the expression-list. */
7729 static vec<tree, va_gc> *
7730 cp_parser_new_initializer (cp_parser* parser)
7732 vec<tree, va_gc> *expression_list;
7734 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7736 tree t;
7737 bool expr_non_constant_p;
7738 cp_lexer_set_source_position (parser->lexer);
7739 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7740 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7741 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7742 expression_list = make_tree_vector_single (t);
7744 else
7745 expression_list = (cp_parser_parenthesized_expression_list
7746 (parser, non_attr, /*cast_p=*/false,
7747 /*allow_expansion_p=*/true,
7748 /*non_constant_p=*/NULL));
7750 return expression_list;
7753 /* Parse a delete-expression.
7755 delete-expression:
7756 :: [opt] delete cast-expression
7757 :: [opt] delete [ ] cast-expression
7759 Returns a representation of the expression. */
7761 static tree
7762 cp_parser_delete_expression (cp_parser* parser)
7764 bool global_scope_p;
7765 bool array_p;
7766 tree expression;
7768 /* Look for the optional `::' operator. */
7769 global_scope_p
7770 = (cp_parser_global_scope_opt (parser,
7771 /*current_scope_valid_p=*/false)
7772 != NULL_TREE);
7773 /* Look for the `delete' keyword. */
7774 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7775 /* See if the array syntax is in use. */
7776 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7778 /* Consume the `[' token. */
7779 cp_lexer_consume_token (parser->lexer);
7780 /* Look for the `]' token. */
7781 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7782 /* Remember that this is the `[]' construct. */
7783 array_p = true;
7785 else
7786 array_p = false;
7788 /* Parse the cast-expression. */
7789 expression = cp_parser_simple_cast_expression (parser);
7791 /* A delete-expression may not appear in an integral constant
7792 expression. */
7793 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7794 return error_mark_node;
7796 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7797 tf_warning_or_error);
7800 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7801 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7802 0 otherwise. */
7804 static int
7805 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7807 cp_token *token = cp_lexer_peek_token (parser->lexer);
7808 switch (token->type)
7810 case CPP_COMMA:
7811 case CPP_SEMICOLON:
7812 case CPP_QUERY:
7813 case CPP_COLON:
7814 case CPP_CLOSE_SQUARE:
7815 case CPP_CLOSE_PAREN:
7816 case CPP_CLOSE_BRACE:
7817 case CPP_OPEN_BRACE:
7818 case CPP_DOT:
7819 case CPP_DOT_STAR:
7820 case CPP_DEREF:
7821 case CPP_DEREF_STAR:
7822 case CPP_DIV:
7823 case CPP_MOD:
7824 case CPP_LSHIFT:
7825 case CPP_RSHIFT:
7826 case CPP_LESS:
7827 case CPP_GREATER:
7828 case CPP_LESS_EQ:
7829 case CPP_GREATER_EQ:
7830 case CPP_EQ_EQ:
7831 case CPP_NOT_EQ:
7832 case CPP_EQ:
7833 case CPP_MULT_EQ:
7834 case CPP_DIV_EQ:
7835 case CPP_MOD_EQ:
7836 case CPP_PLUS_EQ:
7837 case CPP_MINUS_EQ:
7838 case CPP_RSHIFT_EQ:
7839 case CPP_LSHIFT_EQ:
7840 case CPP_AND_EQ:
7841 case CPP_XOR_EQ:
7842 case CPP_OR_EQ:
7843 case CPP_XOR:
7844 case CPP_OR:
7845 case CPP_OR_OR:
7846 case CPP_EOF:
7847 case CPP_ELLIPSIS:
7848 return 0;
7850 case CPP_OPEN_PAREN:
7851 /* In ((type ()) () the last () isn't a valid cast-expression,
7852 so the whole must be parsed as postfix-expression. */
7853 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7854 != CPP_CLOSE_PAREN;
7856 case CPP_OPEN_SQUARE:
7857 /* '[' may start a primary-expression in obj-c++ and in C++11,
7858 as a lambda-expression, eg, '(void)[]{}'. */
7859 if (cxx_dialect >= cxx11)
7860 return -1;
7861 return c_dialect_objc ();
7863 case CPP_PLUS_PLUS:
7864 case CPP_MINUS_MINUS:
7865 /* '++' and '--' may or may not start a cast-expression:
7867 struct T { void operator++(int); };
7868 void f() { (T())++; }
7872 int a;
7873 (int)++a; */
7874 return -1;
7876 default:
7877 return 1;
7881 /* Parse a cast-expression.
7883 cast-expression:
7884 unary-expression
7885 ( type-id ) cast-expression
7887 ADDRESS_P is true iff the unary-expression is appearing as the
7888 operand of the `&' operator. CAST_P is true if this expression is
7889 the target of a cast.
7891 Returns a representation of the expression. */
7893 static tree
7894 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7895 bool decltype_p, cp_id_kind * pidk)
7897 /* If it's a `(', then we might be looking at a cast. */
7898 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7900 tree type = NULL_TREE;
7901 tree expr = NULL_TREE;
7902 int cast_expression = 0;
7903 const char *saved_message;
7905 /* There's no way to know yet whether or not this is a cast.
7906 For example, `(int (3))' is a unary-expression, while `(int)
7907 3' is a cast. So, we resort to parsing tentatively. */
7908 cp_parser_parse_tentatively (parser);
7909 /* Types may not be defined in a cast. */
7910 saved_message = parser->type_definition_forbidden_message;
7911 parser->type_definition_forbidden_message
7912 = G_("types may not be defined in casts");
7913 /* Consume the `('. */
7914 cp_lexer_consume_token (parser->lexer);
7915 /* A very tricky bit is that `(struct S) { 3 }' is a
7916 compound-literal (which we permit in C++ as an extension).
7917 But, that construct is not a cast-expression -- it is a
7918 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7919 is legal; if the compound-literal were a cast-expression,
7920 you'd need an extra set of parentheses.) But, if we parse
7921 the type-id, and it happens to be a class-specifier, then we
7922 will commit to the parse at that point, because we cannot
7923 undo the action that is done when creating a new class. So,
7924 then we cannot back up and do a postfix-expression.
7926 Another tricky case is the following (c++/29234):
7928 struct S { void operator () (); };
7930 void foo ()
7932 ( S()() );
7935 As a type-id we parse the parenthesized S()() as a function
7936 returning a function, groktypename complains and we cannot
7937 back up in this case either.
7939 Therefore, we scan ahead to the closing `)', and check to see
7940 if the tokens after the `)' can start a cast-expression. Otherwise
7941 we are dealing with an unary-expression, a postfix-expression
7942 or something else.
7944 Yet another tricky case, in C++11, is the following (c++/54891):
7946 (void)[]{};
7948 The issue is that usually, besides the case of lambda-expressions,
7949 the parenthesized type-id cannot be followed by '[', and, eg, we
7950 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7951 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7952 we don't commit, we try a cast-expression, then an unary-expression.
7954 Save tokens so that we can put them back. */
7955 cp_lexer_save_tokens (parser->lexer);
7957 /* We may be looking at a cast-expression. */
7958 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7959 /*consume_paren=*/true))
7960 cast_expression
7961 = cp_parser_tokens_start_cast_expression (parser);
7963 /* Roll back the tokens we skipped. */
7964 cp_lexer_rollback_tokens (parser->lexer);
7965 /* If we aren't looking at a cast-expression, simulate an error so
7966 that the call to cp_parser_error_occurred below returns true. */
7967 if (!cast_expression)
7968 cp_parser_simulate_error (parser);
7969 else
7971 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7972 parser->in_type_id_in_expr_p = true;
7973 /* Look for the type-id. */
7974 type = cp_parser_type_id (parser);
7975 /* Look for the closing `)'. */
7976 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7977 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7980 /* Restore the saved message. */
7981 parser->type_definition_forbidden_message = saved_message;
7983 /* At this point this can only be either a cast or a
7984 parenthesized ctor such as `(T ())' that looks like a cast to
7985 function returning T. */
7986 if (!cp_parser_error_occurred (parser))
7988 /* Only commit if the cast-expression doesn't start with
7989 '++', '--', or '[' in C++11. */
7990 if (cast_expression > 0)
7991 cp_parser_commit_to_topmost_tentative_parse (parser);
7993 expr = cp_parser_cast_expression (parser,
7994 /*address_p=*/false,
7995 /*cast_p=*/true,
7996 /*decltype_p=*/false,
7997 pidk);
7999 if (cp_parser_parse_definitely (parser))
8001 /* Warn about old-style casts, if so requested. */
8002 if (warn_old_style_cast
8003 && !in_system_header_at (input_location)
8004 && !VOID_TYPE_P (type)
8005 && current_lang_name != lang_name_c)
8006 warning (OPT_Wold_style_cast, "use of old-style cast");
8008 /* Only type conversions to integral or enumeration types
8009 can be used in constant-expressions. */
8010 if (!cast_valid_in_integral_constant_expression_p (type)
8011 && cp_parser_non_integral_constant_expression (parser,
8012 NIC_CAST))
8013 return error_mark_node;
8015 /* Perform the cast. */
8016 expr = build_c_cast (input_location, type, expr);
8017 return expr;
8020 else
8021 cp_parser_abort_tentative_parse (parser);
8024 /* If we get here, then it's not a cast, so it must be a
8025 unary-expression. */
8026 return cp_parser_unary_expression (parser, pidk, address_p,
8027 cast_p, decltype_p);
8030 /* Parse a binary expression of the general form:
8032 pm-expression:
8033 cast-expression
8034 pm-expression .* cast-expression
8035 pm-expression ->* cast-expression
8037 multiplicative-expression:
8038 pm-expression
8039 multiplicative-expression * pm-expression
8040 multiplicative-expression / pm-expression
8041 multiplicative-expression % pm-expression
8043 additive-expression:
8044 multiplicative-expression
8045 additive-expression + multiplicative-expression
8046 additive-expression - multiplicative-expression
8048 shift-expression:
8049 additive-expression
8050 shift-expression << additive-expression
8051 shift-expression >> additive-expression
8053 relational-expression:
8054 shift-expression
8055 relational-expression < shift-expression
8056 relational-expression > shift-expression
8057 relational-expression <= shift-expression
8058 relational-expression >= shift-expression
8060 GNU Extension:
8062 relational-expression:
8063 relational-expression <? shift-expression
8064 relational-expression >? shift-expression
8066 equality-expression:
8067 relational-expression
8068 equality-expression == relational-expression
8069 equality-expression != relational-expression
8071 and-expression:
8072 equality-expression
8073 and-expression & equality-expression
8075 exclusive-or-expression:
8076 and-expression
8077 exclusive-or-expression ^ and-expression
8079 inclusive-or-expression:
8080 exclusive-or-expression
8081 inclusive-or-expression | exclusive-or-expression
8083 logical-and-expression:
8084 inclusive-or-expression
8085 logical-and-expression && inclusive-or-expression
8087 logical-or-expression:
8088 logical-and-expression
8089 logical-or-expression || logical-and-expression
8091 All these are implemented with a single function like:
8093 binary-expression:
8094 simple-cast-expression
8095 binary-expression <token> binary-expression
8097 CAST_P is true if this expression is the target of a cast.
8099 The binops_by_token map is used to get the tree codes for each <token> type.
8100 binary-expressions are associated according to a precedence table. */
8102 #define TOKEN_PRECEDENCE(token) \
8103 (((token->type == CPP_GREATER \
8104 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8105 && !parser->greater_than_is_operator_p) \
8106 ? PREC_NOT_OPERATOR \
8107 : binops_by_token[token->type].prec)
8109 static tree
8110 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8111 bool no_toplevel_fold_p,
8112 bool decltype_p,
8113 enum cp_parser_prec prec,
8114 cp_id_kind * pidk)
8116 cp_parser_expression_stack stack;
8117 cp_parser_expression_stack_entry *sp = &stack[0];
8118 cp_parser_expression_stack_entry current;
8119 tree rhs;
8120 cp_token *token;
8121 enum tree_code rhs_type;
8122 enum cp_parser_prec new_prec, lookahead_prec;
8123 tree overload;
8125 /* Parse the first expression. */
8126 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8127 ? TRUTH_NOT_EXPR : ERROR_MARK);
8128 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8129 cast_p, decltype_p, pidk);
8130 current.prec = prec;
8132 if (cp_parser_error_occurred (parser))
8133 return error_mark_node;
8135 for (;;)
8137 /* Get an operator token. */
8138 token = cp_lexer_peek_token (parser->lexer);
8140 if (warn_cxx0x_compat
8141 && token->type == CPP_RSHIFT
8142 && !parser->greater_than_is_operator_p)
8144 if (warning_at (token->location, OPT_Wc__0x_compat,
8145 "%<>>%> operator is treated"
8146 " as two right angle brackets in C++11"))
8147 inform (token->location,
8148 "suggest parentheses around %<>>%> expression");
8151 new_prec = TOKEN_PRECEDENCE (token);
8153 /* Popping an entry off the stack means we completed a subexpression:
8154 - either we found a token which is not an operator (`>' where it is not
8155 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8156 will happen repeatedly;
8157 - or, we found an operator which has lower priority. This is the case
8158 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8159 parsing `3 * 4'. */
8160 if (new_prec <= current.prec)
8162 if (sp == stack)
8163 break;
8164 else
8165 goto pop;
8168 get_rhs:
8169 current.tree_type = binops_by_token[token->type].tree_type;
8170 current.loc = token->location;
8172 /* We used the operator token. */
8173 cp_lexer_consume_token (parser->lexer);
8175 /* For "false && x" or "true || x", x will never be executed;
8176 disable warnings while evaluating it. */
8177 if (current.tree_type == TRUTH_ANDIF_EXPR)
8178 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8179 else if (current.tree_type == TRUTH_ORIF_EXPR)
8180 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8182 /* Extract another operand. It may be the RHS of this expression
8183 or the LHS of a new, higher priority expression. */
8184 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8185 ? TRUTH_NOT_EXPR : ERROR_MARK);
8186 rhs = cp_parser_simple_cast_expression (parser);
8188 /* Get another operator token. Look up its precedence to avoid
8189 building a useless (immediately popped) stack entry for common
8190 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8191 token = cp_lexer_peek_token (parser->lexer);
8192 lookahead_prec = TOKEN_PRECEDENCE (token);
8193 if (lookahead_prec > new_prec)
8195 /* ... and prepare to parse the RHS of the new, higher priority
8196 expression. Since precedence levels on the stack are
8197 monotonically increasing, we do not have to care about
8198 stack overflows. */
8199 *sp = current;
8200 ++sp;
8201 current.lhs = rhs;
8202 current.lhs_type = rhs_type;
8203 current.prec = new_prec;
8204 new_prec = lookahead_prec;
8205 goto get_rhs;
8207 pop:
8208 lookahead_prec = new_prec;
8209 /* If the stack is not empty, we have parsed into LHS the right side
8210 (`4' in the example above) of an expression we had suspended.
8211 We can use the information on the stack to recover the LHS (`3')
8212 from the stack together with the tree code (`MULT_EXPR'), and
8213 the precedence of the higher level subexpression
8214 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8215 which will be used to actually build the additive expression. */
8216 rhs = current.lhs;
8217 rhs_type = current.lhs_type;
8218 --sp;
8219 current = *sp;
8222 /* Undo the disabling of warnings done above. */
8223 if (current.tree_type == TRUTH_ANDIF_EXPR)
8224 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8225 else if (current.tree_type == TRUTH_ORIF_EXPR)
8226 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8228 if (warn_logical_not_paren
8229 && current.lhs_type == TRUTH_NOT_EXPR)
8230 warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8232 overload = NULL;
8233 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8234 ERROR_MARK for everything that is not a binary expression.
8235 This makes warn_about_parentheses miss some warnings that
8236 involve unary operators. For unary expressions we should
8237 pass the correct tree_code unless the unary expression was
8238 surrounded by parentheses.
8240 if (no_toplevel_fold_p
8241 && lookahead_prec <= current.prec
8242 && sp == stack)
8243 current.lhs = build2 (current.tree_type,
8244 TREE_CODE_CLASS (current.tree_type)
8245 == tcc_comparison
8246 ? boolean_type_node : TREE_TYPE (current.lhs),
8247 current.lhs, rhs);
8248 else
8249 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8250 current.lhs, current.lhs_type,
8251 rhs, rhs_type, &overload,
8252 complain_flags (decltype_p));
8253 current.lhs_type = current.tree_type;
8254 if (EXPR_P (current.lhs))
8255 SET_EXPR_LOCATION (current.lhs, current.loc);
8257 /* If the binary operator required the use of an overloaded operator,
8258 then this expression cannot be an integral constant-expression.
8259 An overloaded operator can be used even if both operands are
8260 otherwise permissible in an integral constant-expression if at
8261 least one of the operands is of enumeration type. */
8263 if (overload
8264 && cp_parser_non_integral_constant_expression (parser,
8265 NIC_OVERLOADED))
8266 return error_mark_node;
8269 return current.lhs;
8272 static tree
8273 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8274 bool no_toplevel_fold_p,
8275 enum cp_parser_prec prec,
8276 cp_id_kind * pidk)
8278 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8279 /*decltype*/false, prec, pidk);
8282 /* Parse the `? expression : assignment-expression' part of a
8283 conditional-expression. The LOGICAL_OR_EXPR is the
8284 logical-or-expression that started the conditional-expression.
8285 Returns a representation of the entire conditional-expression.
8287 This routine is used by cp_parser_assignment_expression.
8289 ? expression : assignment-expression
8291 GNU Extensions:
8293 ? : assignment-expression */
8295 static tree
8296 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8298 tree expr;
8299 tree assignment_expr;
8300 struct cp_token *token;
8301 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8303 /* Consume the `?' token. */
8304 cp_lexer_consume_token (parser->lexer);
8305 token = cp_lexer_peek_token (parser->lexer);
8306 if (cp_parser_allow_gnu_extensions_p (parser)
8307 && token->type == CPP_COLON)
8309 pedwarn (token->location, OPT_Wpedantic,
8310 "ISO C++ does not allow ?: with omitted middle operand");
8311 /* Implicit true clause. */
8312 expr = NULL_TREE;
8313 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8314 warn_for_omitted_condop (token->location, logical_or_expr);
8316 else
8318 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8319 parser->colon_corrects_to_scope_p = false;
8320 /* Parse the expression. */
8321 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8322 expr = cp_parser_expression (parser);
8323 c_inhibit_evaluation_warnings +=
8324 ((logical_or_expr == truthvalue_true_node)
8325 - (logical_or_expr == truthvalue_false_node));
8326 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8329 /* The next token should be a `:'. */
8330 cp_parser_require (parser, CPP_COLON, RT_COLON);
8331 /* Parse the assignment-expression. */
8332 assignment_expr = cp_parser_assignment_expression (parser);
8333 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8335 /* Build the conditional-expression. */
8336 return build_x_conditional_expr (loc, logical_or_expr,
8337 expr,
8338 assignment_expr,
8339 tf_warning_or_error);
8342 /* Parse an assignment-expression.
8344 assignment-expression:
8345 conditional-expression
8346 logical-or-expression assignment-operator assignment_expression
8347 throw-expression
8349 CAST_P is true if this expression is the target of a cast.
8350 DECLTYPE_P is true if this expression is the operand of decltype.
8352 Returns a representation for the expression. */
8354 static tree
8355 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8356 bool cast_p, bool decltype_p)
8358 tree expr;
8360 /* If the next token is the `throw' keyword, then we're looking at
8361 a throw-expression. */
8362 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8363 expr = cp_parser_throw_expression (parser);
8364 /* Otherwise, it must be that we are looking at a
8365 logical-or-expression. */
8366 else
8368 /* Parse the binary expressions (logical-or-expression). */
8369 expr = cp_parser_binary_expression (parser, cast_p, false,
8370 decltype_p,
8371 PREC_NOT_OPERATOR, pidk);
8372 /* If the next token is a `?' then we're actually looking at a
8373 conditional-expression. */
8374 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8375 return cp_parser_question_colon_clause (parser, expr);
8376 else
8378 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8380 /* If it's an assignment-operator, we're using the second
8381 production. */
8382 enum tree_code assignment_operator
8383 = cp_parser_assignment_operator_opt (parser);
8384 if (assignment_operator != ERROR_MARK)
8386 bool non_constant_p;
8387 location_t saved_input_location;
8389 /* Parse the right-hand side of the assignment. */
8390 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8392 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8393 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8395 /* An assignment may not appear in a
8396 constant-expression. */
8397 if (cp_parser_non_integral_constant_expression (parser,
8398 NIC_ASSIGNMENT))
8399 return error_mark_node;
8400 /* Build the assignment expression. Its default
8401 location is the location of the '=' token. */
8402 saved_input_location = input_location;
8403 input_location = loc;
8404 expr = build_x_modify_expr (loc, expr,
8405 assignment_operator,
8406 rhs,
8407 complain_flags (decltype_p));
8408 input_location = saved_input_location;
8413 return expr;
8416 /* Parse an (optional) assignment-operator.
8418 assignment-operator: one of
8419 = *= /= %= += -= >>= <<= &= ^= |=
8421 GNU Extension:
8423 assignment-operator: one of
8424 <?= >?=
8426 If the next token is an assignment operator, the corresponding tree
8427 code is returned, and the token is consumed. For example, for
8428 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8429 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8430 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8431 operator, ERROR_MARK is returned. */
8433 static enum tree_code
8434 cp_parser_assignment_operator_opt (cp_parser* parser)
8436 enum tree_code op;
8437 cp_token *token;
8439 /* Peek at the next token. */
8440 token = cp_lexer_peek_token (parser->lexer);
8442 switch (token->type)
8444 case CPP_EQ:
8445 op = NOP_EXPR;
8446 break;
8448 case CPP_MULT_EQ:
8449 op = MULT_EXPR;
8450 break;
8452 case CPP_DIV_EQ:
8453 op = TRUNC_DIV_EXPR;
8454 break;
8456 case CPP_MOD_EQ:
8457 op = TRUNC_MOD_EXPR;
8458 break;
8460 case CPP_PLUS_EQ:
8461 op = PLUS_EXPR;
8462 break;
8464 case CPP_MINUS_EQ:
8465 op = MINUS_EXPR;
8466 break;
8468 case CPP_RSHIFT_EQ:
8469 op = RSHIFT_EXPR;
8470 break;
8472 case CPP_LSHIFT_EQ:
8473 op = LSHIFT_EXPR;
8474 break;
8476 case CPP_AND_EQ:
8477 op = BIT_AND_EXPR;
8478 break;
8480 case CPP_XOR_EQ:
8481 op = BIT_XOR_EXPR;
8482 break;
8484 case CPP_OR_EQ:
8485 op = BIT_IOR_EXPR;
8486 break;
8488 default:
8489 /* Nothing else is an assignment operator. */
8490 op = ERROR_MARK;
8493 /* If it was an assignment operator, consume it. */
8494 if (op != ERROR_MARK)
8495 cp_lexer_consume_token (parser->lexer);
8497 return op;
8500 /* Parse an expression.
8502 expression:
8503 assignment-expression
8504 expression , assignment-expression
8506 CAST_P is true if this expression is the target of a cast.
8507 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8508 except possibly parenthesized or on the RHS of a comma (N3276).
8510 Returns a representation of the expression. */
8512 static tree
8513 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8514 bool cast_p, bool decltype_p)
8516 tree expression = NULL_TREE;
8517 location_t loc = UNKNOWN_LOCATION;
8519 while (true)
8521 tree assignment_expression;
8523 /* Parse the next assignment-expression. */
8524 assignment_expression
8525 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8527 /* We don't create a temporary for a call that is the immediate operand
8528 of decltype or on the RHS of a comma. But when we see a comma, we
8529 need to create a temporary for a call on the LHS. */
8530 if (decltype_p && !processing_template_decl
8531 && TREE_CODE (assignment_expression) == CALL_EXPR
8532 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8533 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8534 assignment_expression
8535 = build_cplus_new (TREE_TYPE (assignment_expression),
8536 assignment_expression, tf_warning_or_error);
8538 /* If this is the first assignment-expression, we can just
8539 save it away. */
8540 if (!expression)
8541 expression = assignment_expression;
8542 else
8543 expression = build_x_compound_expr (loc, expression,
8544 assignment_expression,
8545 complain_flags (decltype_p));
8546 /* If the next token is not a comma, then we are done with the
8547 expression. */
8548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8549 break;
8550 /* Consume the `,'. */
8551 loc = cp_lexer_peek_token (parser->lexer)->location;
8552 cp_lexer_consume_token (parser->lexer);
8553 /* A comma operator cannot appear in a constant-expression. */
8554 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8555 expression = error_mark_node;
8558 return expression;
8561 /* Parse a constant-expression.
8563 constant-expression:
8564 conditional-expression
8566 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8567 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8568 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8569 is false, NON_CONSTANT_P should be NULL. */
8571 static tree
8572 cp_parser_constant_expression (cp_parser* parser,
8573 bool allow_non_constant_p,
8574 bool *non_constant_p)
8576 bool saved_integral_constant_expression_p;
8577 bool saved_allow_non_integral_constant_expression_p;
8578 bool saved_non_integral_constant_expression_p;
8579 tree expression;
8581 /* It might seem that we could simply parse the
8582 conditional-expression, and then check to see if it were
8583 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8584 one that the compiler can figure out is constant, possibly after
8585 doing some simplifications or optimizations. The standard has a
8586 precise definition of constant-expression, and we must honor
8587 that, even though it is somewhat more restrictive.
8589 For example:
8591 int i[(2, 3)];
8593 is not a legal declaration, because `(2, 3)' is not a
8594 constant-expression. The `,' operator is forbidden in a
8595 constant-expression. However, GCC's constant-folding machinery
8596 will fold this operation to an INTEGER_CST for `3'. */
8598 /* Save the old settings. */
8599 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8600 saved_allow_non_integral_constant_expression_p
8601 = parser->allow_non_integral_constant_expression_p;
8602 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8603 /* We are now parsing a constant-expression. */
8604 parser->integral_constant_expression_p = true;
8605 parser->allow_non_integral_constant_expression_p
8606 = (allow_non_constant_p || cxx_dialect >= cxx11);
8607 parser->non_integral_constant_expression_p = false;
8608 /* Although the grammar says "conditional-expression", we parse an
8609 "assignment-expression", which also permits "throw-expression"
8610 and the use of assignment operators. In the case that
8611 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8612 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8613 actually essential that we look for an assignment-expression.
8614 For example, cp_parser_initializer_clauses uses this function to
8615 determine whether a particular assignment-expression is in fact
8616 constant. */
8617 expression = cp_parser_assignment_expression (parser);
8618 /* Restore the old settings. */
8619 parser->integral_constant_expression_p
8620 = saved_integral_constant_expression_p;
8621 parser->allow_non_integral_constant_expression_p
8622 = saved_allow_non_integral_constant_expression_p;
8623 if (cxx_dialect >= cxx11)
8625 /* Require an rvalue constant expression here; that's what our
8626 callers expect. Reference constant expressions are handled
8627 separately in e.g. cp_parser_template_argument. */
8628 bool is_const = potential_rvalue_constant_expression (expression);
8629 parser->non_integral_constant_expression_p = !is_const;
8630 if (!is_const && !allow_non_constant_p)
8631 require_potential_rvalue_constant_expression (expression);
8633 if (allow_non_constant_p)
8634 *non_constant_p = parser->non_integral_constant_expression_p;
8635 parser->non_integral_constant_expression_p
8636 = saved_non_integral_constant_expression_p;
8638 return expression;
8641 /* Parse __builtin_offsetof.
8643 offsetof-expression:
8644 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8646 offsetof-member-designator:
8647 id-expression
8648 | offsetof-member-designator "." id-expression
8649 | offsetof-member-designator "[" expression "]"
8650 | offsetof-member-designator "->" id-expression */
8652 static tree
8653 cp_parser_builtin_offsetof (cp_parser *parser)
8655 int save_ice_p, save_non_ice_p;
8656 tree type, expr;
8657 cp_id_kind dummy;
8658 cp_token *token;
8660 /* We're about to accept non-integral-constant things, but will
8661 definitely yield an integral constant expression. Save and
8662 restore these values around our local parsing. */
8663 save_ice_p = parser->integral_constant_expression_p;
8664 save_non_ice_p = parser->non_integral_constant_expression_p;
8666 /* Consume the "__builtin_offsetof" token. */
8667 cp_lexer_consume_token (parser->lexer);
8668 /* Consume the opening `('. */
8669 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8670 /* Parse the type-id. */
8671 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8672 type = cp_parser_type_id (parser);
8673 /* Look for the `,'. */
8674 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8675 token = cp_lexer_peek_token (parser->lexer);
8677 /* Build the (type *)null that begins the traditional offsetof macro. */
8678 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8679 tf_warning_or_error);
8681 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8682 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8683 true, &dummy, token->location);
8684 while (true)
8686 token = cp_lexer_peek_token (parser->lexer);
8687 switch (token->type)
8689 case CPP_OPEN_SQUARE:
8690 /* offsetof-member-designator "[" expression "]" */
8691 expr = cp_parser_postfix_open_square_expression (parser, expr,
8692 true, false);
8693 break;
8695 case CPP_DEREF:
8696 /* offsetof-member-designator "->" identifier */
8697 expr = grok_array_decl (token->location, expr,
8698 integer_zero_node, false);
8699 /* FALLTHRU */
8701 case CPP_DOT:
8702 /* offsetof-member-designator "." identifier */
8703 cp_lexer_consume_token (parser->lexer);
8704 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8705 expr, true, &dummy,
8706 token->location);
8707 break;
8709 case CPP_CLOSE_PAREN:
8710 /* Consume the ")" token. */
8711 cp_lexer_consume_token (parser->lexer);
8712 goto success;
8714 default:
8715 /* Error. We know the following require will fail, but
8716 that gives the proper error message. */
8717 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8718 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8719 expr = error_mark_node;
8720 goto failure;
8724 success:
8725 /* If we're processing a template, we can't finish the semantics yet.
8726 Otherwise we can fold the entire expression now. */
8727 if (processing_template_decl)
8729 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8730 SET_EXPR_LOCATION (expr, loc);
8732 else
8733 expr = finish_offsetof (expr, loc);
8735 failure:
8736 parser->integral_constant_expression_p = save_ice_p;
8737 parser->non_integral_constant_expression_p = save_non_ice_p;
8739 return expr;
8742 /* Parse a trait expression.
8744 Returns a representation of the expression, the underlying type
8745 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8747 static tree
8748 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8750 cp_trait_kind kind;
8751 tree type1, type2 = NULL_TREE;
8752 bool binary = false;
8753 bool variadic = false;
8755 switch (keyword)
8757 case RID_HAS_NOTHROW_ASSIGN:
8758 kind = CPTK_HAS_NOTHROW_ASSIGN;
8759 break;
8760 case RID_HAS_NOTHROW_CONSTRUCTOR:
8761 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8762 break;
8763 case RID_HAS_NOTHROW_COPY:
8764 kind = CPTK_HAS_NOTHROW_COPY;
8765 break;
8766 case RID_HAS_TRIVIAL_ASSIGN:
8767 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8768 break;
8769 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8770 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8771 break;
8772 case RID_HAS_TRIVIAL_COPY:
8773 kind = CPTK_HAS_TRIVIAL_COPY;
8774 break;
8775 case RID_HAS_TRIVIAL_DESTRUCTOR:
8776 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8777 break;
8778 case RID_HAS_VIRTUAL_DESTRUCTOR:
8779 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8780 break;
8781 case RID_IS_ABSTRACT:
8782 kind = CPTK_IS_ABSTRACT;
8783 break;
8784 case RID_IS_BASE_OF:
8785 kind = CPTK_IS_BASE_OF;
8786 binary = true;
8787 break;
8788 case RID_IS_CLASS:
8789 kind = CPTK_IS_CLASS;
8790 break;
8791 case RID_IS_EMPTY:
8792 kind = CPTK_IS_EMPTY;
8793 break;
8794 case RID_IS_ENUM:
8795 kind = CPTK_IS_ENUM;
8796 break;
8797 case RID_IS_FINAL:
8798 kind = CPTK_IS_FINAL;
8799 break;
8800 case RID_IS_LITERAL_TYPE:
8801 kind = CPTK_IS_LITERAL_TYPE;
8802 break;
8803 case RID_IS_POD:
8804 kind = CPTK_IS_POD;
8805 break;
8806 case RID_IS_POLYMORPHIC:
8807 kind = CPTK_IS_POLYMORPHIC;
8808 break;
8809 case RID_IS_STD_LAYOUT:
8810 kind = CPTK_IS_STD_LAYOUT;
8811 break;
8812 case RID_IS_TRIVIAL:
8813 kind = CPTK_IS_TRIVIAL;
8814 break;
8815 case RID_IS_TRIVIALLY_ASSIGNABLE:
8816 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8817 binary = true;
8818 break;
8819 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8820 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8821 variadic = true;
8822 break;
8823 case RID_IS_TRIVIALLY_COPYABLE:
8824 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8825 break;
8826 case RID_IS_UNION:
8827 kind = CPTK_IS_UNION;
8828 break;
8829 case RID_UNDERLYING_TYPE:
8830 kind = CPTK_UNDERLYING_TYPE;
8831 break;
8832 case RID_BASES:
8833 kind = CPTK_BASES;
8834 break;
8835 case RID_DIRECT_BASES:
8836 kind = CPTK_DIRECT_BASES;
8837 break;
8838 default:
8839 gcc_unreachable ();
8842 /* Consume the token. */
8843 cp_lexer_consume_token (parser->lexer);
8845 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8847 type1 = cp_parser_type_id (parser);
8849 if (type1 == error_mark_node)
8850 return error_mark_node;
8852 if (binary)
8854 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8856 type2 = cp_parser_type_id (parser);
8858 if (type2 == error_mark_node)
8859 return error_mark_node;
8861 else if (variadic)
8863 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8865 cp_lexer_consume_token (parser->lexer);
8866 tree elt = cp_parser_type_id (parser);
8867 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8869 cp_lexer_consume_token (parser->lexer);
8870 elt = make_pack_expansion (elt);
8872 if (elt == error_mark_node)
8873 return error_mark_node;
8874 type2 = tree_cons (NULL_TREE, elt, type2);
8878 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8880 /* Complete the trait expression, which may mean either processing
8881 the trait expr now or saving it for template instantiation. */
8882 switch(kind)
8884 case CPTK_UNDERLYING_TYPE:
8885 return finish_underlying_type (type1);
8886 case CPTK_BASES:
8887 return finish_bases (type1, false);
8888 case CPTK_DIRECT_BASES:
8889 return finish_bases (type1, true);
8890 default:
8891 return finish_trait_expr (kind, type1, type2);
8895 /* Lambdas that appear in variable initializer or default argument scope
8896 get that in their mangling, so we need to record it. We might as well
8897 use the count for function and namespace scopes as well. */
8898 static GTY(()) tree lambda_scope;
8899 static GTY(()) int lambda_count;
8900 typedef struct GTY(()) tree_int
8902 tree t;
8903 int i;
8904 } tree_int;
8905 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8907 static void
8908 start_lambda_scope (tree decl)
8910 tree_int ti;
8911 gcc_assert (decl);
8912 /* Once we're inside a function, we ignore other scopes and just push
8913 the function again so that popping works properly. */
8914 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8915 decl = current_function_decl;
8916 ti.t = lambda_scope;
8917 ti.i = lambda_count;
8918 vec_safe_push (lambda_scope_stack, ti);
8919 if (lambda_scope != decl)
8921 /* Don't reset the count if we're still in the same function. */
8922 lambda_scope = decl;
8923 lambda_count = 0;
8927 static void
8928 record_lambda_scope (tree lambda)
8930 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8931 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8934 static void
8935 finish_lambda_scope (void)
8937 tree_int *p = &lambda_scope_stack->last ();
8938 if (lambda_scope != p->t)
8940 lambda_scope = p->t;
8941 lambda_count = p->i;
8943 lambda_scope_stack->pop ();
8946 /* Parse a lambda expression.
8948 lambda-expression:
8949 lambda-introducer lambda-declarator [opt] compound-statement
8951 Returns a representation of the expression. */
8953 static tree
8954 cp_parser_lambda_expression (cp_parser* parser)
8956 tree lambda_expr = build_lambda_expr ();
8957 tree type;
8958 bool ok = true;
8959 cp_token *token = cp_lexer_peek_token (parser->lexer);
8960 cp_token_position start = 0;
8962 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8964 if (cp_unevaluated_operand)
8966 if (!token->error_reported)
8968 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8969 "lambda-expression in unevaluated context");
8970 token->error_reported = true;
8972 ok = false;
8974 else if (parser->in_template_argument_list_p)
8976 if (!token->error_reported)
8978 error_at (token->location, "lambda-expression in template-argument");
8979 token->error_reported = true;
8981 ok = false;
8984 /* We may be in the middle of deferred access check. Disable
8985 it now. */
8986 push_deferring_access_checks (dk_no_deferred);
8988 cp_parser_lambda_introducer (parser, lambda_expr);
8990 type = begin_lambda_type (lambda_expr);
8991 if (type == error_mark_node)
8992 return error_mark_node;
8994 record_lambda_scope (lambda_expr);
8996 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8997 determine_visibility (TYPE_NAME (type));
8999 /* Now that we've started the type, add the capture fields for any
9000 explicit captures. */
9001 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9004 /* Inside the class, surrounding template-parameter-lists do not apply. */
9005 unsigned int saved_num_template_parameter_lists
9006 = parser->num_template_parameter_lists;
9007 unsigned char in_statement = parser->in_statement;
9008 bool in_switch_statement_p = parser->in_switch_statement_p;
9009 bool fully_implicit_function_template_p
9010 = parser->fully_implicit_function_template_p;
9011 tree implicit_template_parms = parser->implicit_template_parms;
9012 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9013 bool auto_is_implicit_function_template_parm_p
9014 = parser->auto_is_implicit_function_template_parm_p;
9016 parser->num_template_parameter_lists = 0;
9017 parser->in_statement = 0;
9018 parser->in_switch_statement_p = false;
9019 parser->fully_implicit_function_template_p = false;
9020 parser->implicit_template_parms = 0;
9021 parser->implicit_template_scope = 0;
9022 parser->auto_is_implicit_function_template_parm_p = false;
9024 /* By virtue of defining a local class, a lambda expression has access to
9025 the private variables of enclosing classes. */
9027 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9029 if (ok)
9031 if (!cp_parser_error_occurred (parser)
9032 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9033 && cp_parser_start_tentative_firewall (parser))
9034 start = token;
9035 cp_parser_lambda_body (parser, lambda_expr);
9037 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9039 if (cp_parser_skip_to_closing_brace (parser))
9040 cp_lexer_consume_token (parser->lexer);
9043 /* The capture list was built up in reverse order; fix that now. */
9044 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9045 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9047 if (ok)
9048 maybe_add_lambda_conv_op (type);
9050 type = finish_struct (type, /*attributes=*/NULL_TREE);
9052 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9053 parser->in_statement = in_statement;
9054 parser->in_switch_statement_p = in_switch_statement_p;
9055 parser->fully_implicit_function_template_p
9056 = fully_implicit_function_template_p;
9057 parser->implicit_template_parms = implicit_template_parms;
9058 parser->implicit_template_scope = implicit_template_scope;
9059 parser->auto_is_implicit_function_template_parm_p
9060 = auto_is_implicit_function_template_parm_p;
9063 pop_deferring_access_checks ();
9065 /* This field is only used during parsing of the lambda. */
9066 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9068 /* This lambda shouldn't have any proxies left at this point. */
9069 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9070 /* And now that we're done, push proxies for an enclosing lambda. */
9071 insert_pending_capture_proxies ();
9073 if (ok)
9074 lambda_expr = build_lambda_object (lambda_expr);
9075 else
9076 lambda_expr = error_mark_node;
9078 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9080 return lambda_expr;
9083 /* Parse the beginning of a lambda expression.
9085 lambda-introducer:
9086 [ lambda-capture [opt] ]
9088 LAMBDA_EXPR is the current representation of the lambda expression. */
9090 static void
9091 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9093 /* Need commas after the first capture. */
9094 bool first = true;
9096 /* Eat the leading `['. */
9097 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9099 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9100 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9101 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9102 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9103 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9104 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9106 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9108 cp_lexer_consume_token (parser->lexer);
9109 first = false;
9112 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9114 cp_token* capture_token;
9115 tree capture_id;
9116 tree capture_init_expr;
9117 cp_id_kind idk = CP_ID_KIND_NONE;
9118 bool explicit_init_p = false;
9120 enum capture_kind_type
9122 BY_COPY,
9123 BY_REFERENCE
9125 enum capture_kind_type capture_kind = BY_COPY;
9127 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9129 error ("expected end of capture-list");
9130 return;
9133 if (first)
9134 first = false;
9135 else
9136 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9138 /* Possibly capture `this'. */
9139 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9141 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9142 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9143 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9144 "with by-copy capture default");
9145 cp_lexer_consume_token (parser->lexer);
9146 add_capture (lambda_expr,
9147 /*id=*/this_identifier,
9148 /*initializer=*/finish_this_expr(),
9149 /*by_reference_p=*/false,
9150 explicit_init_p);
9151 continue;
9154 /* Remember whether we want to capture as a reference or not. */
9155 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9157 capture_kind = BY_REFERENCE;
9158 cp_lexer_consume_token (parser->lexer);
9161 /* Get the identifier. */
9162 capture_token = cp_lexer_peek_token (parser->lexer);
9163 capture_id = cp_parser_identifier (parser);
9165 if (capture_id == error_mark_node)
9166 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9167 delimiters, but I modified this to stop on unnested ']' as well. It
9168 was already changed to stop on unnested '}', so the
9169 "closing_parenthesis" name is no more misleading with my change. */
9171 cp_parser_skip_to_closing_parenthesis (parser,
9172 /*recovering=*/true,
9173 /*or_comma=*/true,
9174 /*consume_paren=*/true);
9175 break;
9178 /* Find the initializer for this capture. */
9179 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9180 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9181 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9183 bool direct, non_constant;
9184 /* An explicit initializer exists. */
9185 if (cxx_dialect < cxx14)
9186 pedwarn (input_location, 0,
9187 "lambda capture initializers "
9188 "only available with -std=c++14 or -std=gnu++14");
9189 capture_init_expr = cp_parser_initializer (parser, &direct,
9190 &non_constant);
9191 explicit_init_p = true;
9192 if (capture_init_expr == NULL_TREE)
9194 error ("empty initializer for lambda init-capture");
9195 capture_init_expr = error_mark_node;
9198 else
9200 const char* error_msg;
9202 /* Turn the identifier into an id-expression. */
9203 capture_init_expr
9204 = cp_parser_lookup_name_simple (parser, capture_id,
9205 capture_token->location);
9207 if (capture_init_expr == error_mark_node)
9209 unqualified_name_lookup_error (capture_id);
9210 continue;
9212 else if (DECL_P (capture_init_expr)
9213 && (!VAR_P (capture_init_expr)
9214 && TREE_CODE (capture_init_expr) != PARM_DECL))
9216 error_at (capture_token->location,
9217 "capture of non-variable %qD ",
9218 capture_init_expr);
9219 inform (0, "%q+#D declared here", capture_init_expr);
9220 continue;
9222 if (VAR_P (capture_init_expr)
9223 && decl_storage_duration (capture_init_expr) != dk_auto)
9225 if (pedwarn (capture_token->location, 0, "capture of variable "
9226 "%qD with non-automatic storage duration",
9227 capture_init_expr))
9228 inform (0, "%q+#D declared here", capture_init_expr);
9229 continue;
9232 capture_init_expr
9233 = finish_id_expression
9234 (capture_id,
9235 capture_init_expr,
9236 parser->scope,
9237 &idk,
9238 /*integral_constant_expression_p=*/false,
9239 /*allow_non_integral_constant_expression_p=*/false,
9240 /*non_integral_constant_expression_p=*/NULL,
9241 /*template_p=*/false,
9242 /*done=*/true,
9243 /*address_p=*/false,
9244 /*template_arg_p=*/false,
9245 &error_msg,
9246 capture_token->location);
9248 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9250 cp_lexer_consume_token (parser->lexer);
9251 capture_init_expr = make_pack_expansion (capture_init_expr);
9253 else
9254 check_for_bare_parameter_packs (capture_init_expr);
9257 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9258 && !explicit_init_p)
9260 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9261 && capture_kind == BY_COPY)
9262 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9263 "of %qD redundant with by-copy capture default",
9264 capture_id);
9265 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9266 && capture_kind == BY_REFERENCE)
9267 pedwarn (capture_token->location, 0, "explicit by-reference "
9268 "capture of %qD redundant with by-reference capture "
9269 "default", capture_id);
9272 add_capture (lambda_expr,
9273 capture_id,
9274 capture_init_expr,
9275 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9276 explicit_init_p);
9279 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9282 /* Parse the (optional) middle of a lambda expression.
9284 lambda-declarator:
9285 < template-parameter-list [opt] >
9286 ( parameter-declaration-clause [opt] )
9287 attribute-specifier [opt]
9288 mutable [opt]
9289 exception-specification [opt]
9290 lambda-return-type-clause [opt]
9292 LAMBDA_EXPR is the current representation of the lambda expression. */
9294 static bool
9295 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9297 /* 5.1.1.4 of the standard says:
9298 If a lambda-expression does not include a lambda-declarator, it is as if
9299 the lambda-declarator were ().
9300 This means an empty parameter list, no attributes, and no exception
9301 specification. */
9302 tree param_list = void_list_node;
9303 tree attributes = NULL_TREE;
9304 tree exception_spec = NULL_TREE;
9305 tree template_param_list = NULL_TREE;
9307 /* The template-parameter-list is optional, but must begin with
9308 an opening angle if present. */
9309 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9311 if (cxx_dialect < cxx14)
9312 pedwarn (parser->lexer->next_token->location, 0,
9313 "lambda templates are only available with "
9314 "-std=c++14 or -std=gnu++14");
9316 cp_lexer_consume_token (parser->lexer);
9318 template_param_list = cp_parser_template_parameter_list (parser);
9320 cp_parser_skip_to_end_of_template_parameter_list (parser);
9322 /* We just processed one more parameter list. */
9323 ++parser->num_template_parameter_lists;
9326 /* The parameter-declaration-clause is optional (unless
9327 template-parameter-list was given), but must begin with an
9328 opening parenthesis if present. */
9329 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9331 cp_lexer_consume_token (parser->lexer);
9333 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9335 /* Parse parameters. */
9336 param_list = cp_parser_parameter_declaration_clause (parser);
9338 /* Default arguments shall not be specified in the
9339 parameter-declaration-clause of a lambda-declarator. */
9340 for (tree t = param_list; t; t = TREE_CHAIN (t))
9341 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9342 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9343 "default argument specified for lambda parameter");
9345 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9347 attributes = cp_parser_attributes_opt (parser);
9349 /* Parse optional `mutable' keyword. */
9350 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9352 cp_lexer_consume_token (parser->lexer);
9353 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9356 /* Parse optional exception specification. */
9357 exception_spec = cp_parser_exception_specification_opt (parser);
9359 /* Parse optional trailing return type. */
9360 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9362 cp_lexer_consume_token (parser->lexer);
9363 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9364 = cp_parser_trailing_type_id (parser);
9367 /* The function parameters must be in scope all the way until after the
9368 trailing-return-type in case of decltype. */
9369 pop_bindings_and_leave_scope ();
9371 else if (template_param_list != NULL_TREE) // generate diagnostic
9372 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9374 /* Create the function call operator.
9376 Messing with declarators like this is no uglier than building up the
9377 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9378 other code. */
9380 cp_decl_specifier_seq return_type_specs;
9381 cp_declarator* declarator;
9382 tree fco;
9383 int quals;
9384 void *p;
9386 clear_decl_specs (&return_type_specs);
9387 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9388 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9389 else
9390 /* Maybe we will deduce the return type later. */
9391 return_type_specs.type = make_auto ();
9393 p = obstack_alloc (&declarator_obstack, 0);
9395 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9396 sfk_none);
9398 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9399 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9400 declarator = make_call_declarator (declarator, param_list, quals,
9401 VIRT_SPEC_UNSPECIFIED,
9402 REF_QUAL_NONE,
9403 exception_spec,
9404 /*late_return_type=*/NULL_TREE);
9405 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9407 fco = grokmethod (&return_type_specs,
9408 declarator,
9409 attributes);
9410 if (fco != error_mark_node)
9412 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9413 DECL_ARTIFICIAL (fco) = 1;
9414 /* Give the object parameter a different name. */
9415 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9416 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9417 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9419 if (template_param_list)
9421 fco = finish_member_template_decl (fco);
9422 finish_template_decl (template_param_list);
9423 --parser->num_template_parameter_lists;
9425 else if (parser->fully_implicit_function_template_p)
9426 fco = finish_fully_implicit_template (parser, fco);
9428 finish_member_declaration (fco);
9430 obstack_free (&declarator_obstack, p);
9432 return (fco != error_mark_node);
9436 /* Parse the body of a lambda expression, which is simply
9438 compound-statement
9440 but which requires special handling.
9441 LAMBDA_EXPR is the current representation of the lambda expression. */
9443 static void
9444 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9446 bool nested = (current_function_decl != NULL_TREE);
9447 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9448 if (nested)
9449 push_function_context ();
9450 else
9451 /* Still increment function_depth so that we don't GC in the
9452 middle of an expression. */
9453 ++function_depth;
9454 /* Clear this in case we're in the middle of a default argument. */
9455 parser->local_variables_forbidden_p = false;
9457 /* Finish the function call operator
9458 - class_specifier
9459 + late_parsing_for_member
9460 + function_definition_after_declarator
9461 + ctor_initializer_opt_and_function_body */
9463 tree fco = lambda_function (lambda_expr);
9464 tree body;
9465 bool done = false;
9466 tree compound_stmt;
9467 tree cap;
9469 /* Let the front end know that we are going to be defining this
9470 function. */
9471 start_preparsed_function (fco,
9472 NULL_TREE,
9473 SF_PRE_PARSED | SF_INCLASS_INLINE);
9475 start_lambda_scope (fco);
9476 body = begin_function_body ();
9478 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9479 goto out;
9481 /* Push the proxies for any explicit captures. */
9482 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9483 cap = TREE_CHAIN (cap))
9484 build_capture_proxy (TREE_PURPOSE (cap));
9486 compound_stmt = begin_compound_stmt (0);
9488 /* 5.1.1.4 of the standard says:
9489 If a lambda-expression does not include a trailing-return-type, it
9490 is as if the trailing-return-type denotes the following type:
9491 * if the compound-statement is of the form
9492 { return attribute-specifier [opt] expression ; }
9493 the type of the returned expression after lvalue-to-rvalue
9494 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9495 (_conv.array_ 4.2), and function-to-pointer conversion
9496 (_conv.func_ 4.3);
9497 * otherwise, void. */
9499 /* In a lambda that has neither a lambda-return-type-clause
9500 nor a deducible form, errors should be reported for return statements
9501 in the body. Since we used void as the placeholder return type, parsing
9502 the body as usual will give such desired behavior. */
9503 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9504 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9505 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9507 tree expr = NULL_TREE;
9508 cp_id_kind idk = CP_ID_KIND_NONE;
9510 /* Parse tentatively in case there's more after the initial return
9511 statement. */
9512 cp_parser_parse_tentatively (parser);
9514 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9516 expr = cp_parser_expression (parser, &idk);
9518 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9519 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9521 if (cp_parser_parse_definitely (parser))
9523 if (!processing_template_decl)
9524 apply_deduced_return_type (fco, lambda_return_type (expr));
9526 /* Will get error here if type not deduced yet. */
9527 finish_return_stmt (expr);
9529 done = true;
9533 if (!done)
9535 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9536 cp_parser_label_declaration (parser);
9537 cp_parser_statement_seq_opt (parser, NULL_TREE);
9538 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9541 finish_compound_stmt (compound_stmt);
9543 out:
9544 finish_function_body (body);
9545 finish_lambda_scope ();
9547 /* Finish the function and generate code for it if necessary. */
9548 tree fn = finish_function (/*inline*/2);
9550 /* Only expand if the call op is not a template. */
9551 if (!DECL_TEMPLATE_INFO (fco))
9552 expand_or_defer_fn (fn);
9555 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9556 if (nested)
9557 pop_function_context();
9558 else
9559 --function_depth;
9562 /* Statements [gram.stmt.stmt] */
9564 /* Parse a statement.
9566 statement:
9567 labeled-statement
9568 expression-statement
9569 compound-statement
9570 selection-statement
9571 iteration-statement
9572 jump-statement
9573 declaration-statement
9574 try-block
9576 C++11:
9578 statement:
9579 labeled-statement
9580 attribute-specifier-seq (opt) expression-statement
9581 attribute-specifier-seq (opt) compound-statement
9582 attribute-specifier-seq (opt) selection-statement
9583 attribute-specifier-seq (opt) iteration-statement
9584 attribute-specifier-seq (opt) jump-statement
9585 declaration-statement
9586 attribute-specifier-seq (opt) try-block
9588 TM Extension:
9590 statement:
9591 atomic-statement
9593 IN_COMPOUND is true when the statement is nested inside a
9594 cp_parser_compound_statement; this matters for certain pragmas.
9596 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9597 is a (possibly labeled) if statement which is not enclosed in braces
9598 and has an else clause. This is used to implement -Wparentheses. */
9600 static void
9601 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9602 bool in_compound, bool *if_p)
9604 tree statement, std_attrs = NULL_TREE;
9605 cp_token *token;
9606 location_t statement_location, attrs_location;
9608 restart:
9609 if (if_p != NULL)
9610 *if_p = false;
9611 /* There is no statement yet. */
9612 statement = NULL_TREE;
9614 saved_token_sentinel saved_tokens (parser->lexer);
9615 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9616 if (c_dialect_objc ())
9617 /* In obj-c++, seeing '[[' might be the either the beginning of
9618 c++11 attributes, or a nested objc-message-expression. So
9619 let's parse the c++11 attributes tentatively. */
9620 cp_parser_parse_tentatively (parser);
9621 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9622 if (c_dialect_objc ())
9624 if (!cp_parser_parse_definitely (parser))
9625 std_attrs = NULL_TREE;
9628 /* Peek at the next token. */
9629 token = cp_lexer_peek_token (parser->lexer);
9630 /* Remember the location of the first token in the statement. */
9631 statement_location = token->location;
9632 /* If this is a keyword, then that will often determine what kind of
9633 statement we have. */
9634 if (token->type == CPP_KEYWORD)
9636 enum rid keyword = token->keyword;
9638 switch (keyword)
9640 case RID_CASE:
9641 case RID_DEFAULT:
9642 /* Looks like a labeled-statement with a case label.
9643 Parse the label, and then use tail recursion to parse
9644 the statement. */
9645 cp_parser_label_for_labeled_statement (parser, std_attrs);
9646 goto restart;
9648 case RID_IF:
9649 case RID_SWITCH:
9650 statement = cp_parser_selection_statement (parser, if_p);
9651 break;
9653 case RID_WHILE:
9654 case RID_DO:
9655 case RID_FOR:
9656 statement = cp_parser_iteration_statement (parser, false);
9657 break;
9659 case RID_CILK_FOR:
9660 if (!flag_cilkplus)
9662 error_at (cp_lexer_peek_token (parser->lexer)->location,
9663 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9664 cp_lexer_consume_token (parser->lexer);
9665 statement = error_mark_node;
9667 else
9668 statement = cp_parser_cilk_for (parser, integer_zero_node);
9669 break;
9671 case RID_BREAK:
9672 case RID_CONTINUE:
9673 case RID_RETURN:
9674 case RID_GOTO:
9675 statement = cp_parser_jump_statement (parser);
9676 break;
9678 case RID_CILK_SYNC:
9679 cp_lexer_consume_token (parser->lexer);
9680 if (flag_cilkplus)
9682 tree sync_expr = build_cilk_sync ();
9683 SET_EXPR_LOCATION (sync_expr,
9684 token->location);
9685 statement = finish_expr_stmt (sync_expr);
9687 else
9689 error_at (token->location, "-fcilkplus must be enabled to use"
9690 " %<_Cilk_sync%>");
9691 statement = error_mark_node;
9693 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9694 break;
9696 /* Objective-C++ exception-handling constructs. */
9697 case RID_AT_TRY:
9698 case RID_AT_CATCH:
9699 case RID_AT_FINALLY:
9700 case RID_AT_SYNCHRONIZED:
9701 case RID_AT_THROW:
9702 statement = cp_parser_objc_statement (parser);
9703 break;
9705 case RID_TRY:
9706 statement = cp_parser_try_block (parser);
9707 break;
9709 case RID_NAMESPACE:
9710 /* This must be a namespace alias definition. */
9711 cp_parser_declaration_statement (parser);
9712 return;
9714 case RID_TRANSACTION_ATOMIC:
9715 case RID_TRANSACTION_RELAXED:
9716 statement = cp_parser_transaction (parser, keyword);
9717 break;
9718 case RID_TRANSACTION_CANCEL:
9719 statement = cp_parser_transaction_cancel (parser);
9720 break;
9722 default:
9723 /* It might be a keyword like `int' that can start a
9724 declaration-statement. */
9725 break;
9728 else if (token->type == CPP_NAME)
9730 /* If the next token is a `:', then we are looking at a
9731 labeled-statement. */
9732 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9733 if (token->type == CPP_COLON)
9735 /* Looks like a labeled-statement with an ordinary label.
9736 Parse the label, and then use tail recursion to parse
9737 the statement. */
9739 cp_parser_label_for_labeled_statement (parser, std_attrs);
9740 goto restart;
9743 /* Anything that starts with a `{' must be a compound-statement. */
9744 else if (token->type == CPP_OPEN_BRACE)
9745 statement = cp_parser_compound_statement (parser, NULL, false, false);
9746 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9747 a statement all its own. */
9748 else if (token->type == CPP_PRAGMA)
9750 /* Only certain OpenMP pragmas are attached to statements, and thus
9751 are considered statements themselves. All others are not. In
9752 the context of a compound, accept the pragma as a "statement" and
9753 return so that we can check for a close brace. Otherwise we
9754 require a real statement and must go back and read one. */
9755 if (in_compound)
9756 cp_parser_pragma (parser, pragma_compound);
9757 else if (!cp_parser_pragma (parser, pragma_stmt))
9758 goto restart;
9759 return;
9761 else if (token->type == CPP_EOF)
9763 cp_parser_error (parser, "expected statement");
9764 return;
9767 /* Everything else must be a declaration-statement or an
9768 expression-statement. Try for the declaration-statement
9769 first, unless we are looking at a `;', in which case we know that
9770 we have an expression-statement. */
9771 if (!statement)
9773 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9775 if (std_attrs != NULL_TREE)
9777 /* Attributes should be parsed as part of the the
9778 declaration, so let's un-parse them. */
9779 saved_tokens.rollback();
9780 std_attrs = NULL_TREE;
9783 cp_parser_parse_tentatively (parser);
9784 /* Try to parse the declaration-statement. */
9785 cp_parser_declaration_statement (parser);
9786 /* If that worked, we're done. */
9787 if (cp_parser_parse_definitely (parser))
9788 return;
9790 /* Look for an expression-statement instead. */
9791 statement = cp_parser_expression_statement (parser, in_statement_expr);
9794 /* Set the line number for the statement. */
9795 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9796 SET_EXPR_LOCATION (statement, statement_location);
9798 /* Note that for now, we don't do anything with c++11 statements
9799 parsed at this level. */
9800 if (std_attrs != NULL_TREE)
9801 warning_at (attrs_location,
9802 OPT_Wattributes,
9803 "attributes at the beginning of statement are ignored");
9806 /* Parse the label for a labeled-statement, i.e.
9808 identifier :
9809 case constant-expression :
9810 default :
9812 GNU Extension:
9813 case constant-expression ... constant-expression : statement
9815 When a label is parsed without errors, the label is added to the
9816 parse tree by the finish_* functions, so this function doesn't
9817 have to return the label. */
9819 static void
9820 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9822 cp_token *token;
9823 tree label = NULL_TREE;
9824 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9826 /* The next token should be an identifier. */
9827 token = cp_lexer_peek_token (parser->lexer);
9828 if (token->type != CPP_NAME
9829 && token->type != CPP_KEYWORD)
9831 cp_parser_error (parser, "expected labeled-statement");
9832 return;
9835 parser->colon_corrects_to_scope_p = false;
9836 switch (token->keyword)
9838 case RID_CASE:
9840 tree expr, expr_hi;
9841 cp_token *ellipsis;
9843 /* Consume the `case' token. */
9844 cp_lexer_consume_token (parser->lexer);
9845 /* Parse the constant-expression. */
9846 expr = cp_parser_constant_expression (parser);
9847 if (check_for_bare_parameter_packs (expr))
9848 expr = error_mark_node;
9850 ellipsis = cp_lexer_peek_token (parser->lexer);
9851 if (ellipsis->type == CPP_ELLIPSIS)
9853 /* Consume the `...' token. */
9854 cp_lexer_consume_token (parser->lexer);
9855 expr_hi = cp_parser_constant_expression (parser);
9856 if (check_for_bare_parameter_packs (expr_hi))
9857 expr_hi = error_mark_node;
9859 /* We don't need to emit warnings here, as the common code
9860 will do this for us. */
9862 else
9863 expr_hi = NULL_TREE;
9865 if (parser->in_switch_statement_p)
9866 finish_case_label (token->location, expr, expr_hi);
9867 else
9868 error_at (token->location,
9869 "case label %qE not within a switch statement",
9870 expr);
9872 break;
9874 case RID_DEFAULT:
9875 /* Consume the `default' token. */
9876 cp_lexer_consume_token (parser->lexer);
9878 if (parser->in_switch_statement_p)
9879 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9880 else
9881 error_at (token->location, "case label not within a switch statement");
9882 break;
9884 default:
9885 /* Anything else must be an ordinary label. */
9886 label = finish_label_stmt (cp_parser_identifier (parser));
9887 break;
9890 /* Require the `:' token. */
9891 cp_parser_require (parser, CPP_COLON, RT_COLON);
9893 /* An ordinary label may optionally be followed by attributes.
9894 However, this is only permitted if the attributes are then
9895 followed by a semicolon. This is because, for backward
9896 compatibility, when parsing
9897 lab: __attribute__ ((unused)) int i;
9898 we want the attribute to attach to "i", not "lab". */
9899 if (label != NULL_TREE
9900 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9902 tree attrs;
9903 cp_parser_parse_tentatively (parser);
9904 attrs = cp_parser_gnu_attributes_opt (parser);
9905 if (attrs == NULL_TREE
9906 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9907 cp_parser_abort_tentative_parse (parser);
9908 else if (!cp_parser_parse_definitely (parser))
9910 else
9911 attributes = chainon (attributes, attrs);
9914 if (attributes != NULL_TREE)
9915 cplus_decl_attributes (&label, attributes, 0);
9917 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9920 /* Parse an expression-statement.
9922 expression-statement:
9923 expression [opt] ;
9925 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9926 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9927 indicates whether this expression-statement is part of an
9928 expression statement. */
9930 static tree
9931 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9933 tree statement = NULL_TREE;
9934 cp_token *token = cp_lexer_peek_token (parser->lexer);
9936 /* If the next token is a ';', then there is no expression
9937 statement. */
9938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9940 statement = cp_parser_expression (parser);
9941 if (statement == error_mark_node
9942 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9944 cp_parser_skip_to_end_of_block_or_statement (parser);
9945 return error_mark_node;
9949 /* Give a helpful message for "A<T>::type t;" and the like. */
9950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9951 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9953 if (TREE_CODE (statement) == SCOPE_REF)
9954 error_at (token->location, "need %<typename%> before %qE because "
9955 "%qT is a dependent scope",
9956 statement, TREE_OPERAND (statement, 0));
9957 else if (is_overloaded_fn (statement)
9958 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9960 /* A::A a; */
9961 tree fn = get_first_fn (statement);
9962 error_at (token->location,
9963 "%<%T::%D%> names the constructor, not the type",
9964 DECL_CONTEXT (fn), DECL_NAME (fn));
9968 /* Consume the final `;'. */
9969 cp_parser_consume_semicolon_at_end_of_statement (parser);
9971 if (in_statement_expr
9972 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9973 /* This is the final expression statement of a statement
9974 expression. */
9975 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9976 else if (statement)
9977 statement = finish_expr_stmt (statement);
9979 return statement;
9982 /* Parse a compound-statement.
9984 compound-statement:
9985 { statement-seq [opt] }
9987 GNU extension:
9989 compound-statement:
9990 { label-declaration-seq [opt] statement-seq [opt] }
9992 label-declaration-seq:
9993 label-declaration
9994 label-declaration-seq label-declaration
9996 Returns a tree representing the statement. */
9998 static tree
9999 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10000 bool in_try, bool function_body)
10002 tree compound_stmt;
10004 /* Consume the `{'. */
10005 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10006 return error_mark_node;
10007 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10008 && !function_body && cxx_dialect < cxx14)
10009 pedwarn (input_location, OPT_Wpedantic,
10010 "compound-statement in constexpr function");
10011 /* Begin the compound-statement. */
10012 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10013 /* If the next keyword is `__label__' we have a label declaration. */
10014 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10015 cp_parser_label_declaration (parser);
10016 /* Parse an (optional) statement-seq. */
10017 cp_parser_statement_seq_opt (parser, in_statement_expr);
10018 /* Finish the compound-statement. */
10019 finish_compound_stmt (compound_stmt);
10020 /* Consume the `}'. */
10021 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10023 return compound_stmt;
10026 /* Parse an (optional) statement-seq.
10028 statement-seq:
10029 statement
10030 statement-seq [opt] statement */
10032 static void
10033 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10035 /* Scan statements until there aren't any more. */
10036 while (true)
10038 cp_token *token = cp_lexer_peek_token (parser->lexer);
10040 /* If we are looking at a `}', then we have run out of
10041 statements; the same is true if we have reached the end
10042 of file, or have stumbled upon a stray '@end'. */
10043 if (token->type == CPP_CLOSE_BRACE
10044 || token->type == CPP_EOF
10045 || token->type == CPP_PRAGMA_EOL
10046 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10047 break;
10049 /* If we are in a compound statement and find 'else' then
10050 something went wrong. */
10051 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10053 if (parser->in_statement & IN_IF_STMT)
10054 break;
10055 else
10057 token = cp_lexer_consume_token (parser->lexer);
10058 error_at (token->location, "%<else%> without a previous %<if%>");
10062 /* Parse the statement. */
10063 cp_parser_statement (parser, in_statement_expr, true, NULL);
10067 /* Parse a selection-statement.
10069 selection-statement:
10070 if ( condition ) statement
10071 if ( condition ) statement else statement
10072 switch ( condition ) statement
10074 Returns the new IF_STMT or SWITCH_STMT.
10076 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10077 is a (possibly labeled) if statement which is not enclosed in
10078 braces and has an else clause. This is used to implement
10079 -Wparentheses. */
10081 static tree
10082 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10084 cp_token *token;
10085 enum rid keyword;
10087 if (if_p != NULL)
10088 *if_p = false;
10090 /* Peek at the next token. */
10091 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10093 /* See what kind of keyword it is. */
10094 keyword = token->keyword;
10095 switch (keyword)
10097 case RID_IF:
10098 case RID_SWITCH:
10100 tree statement;
10101 tree condition;
10103 /* Look for the `('. */
10104 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10106 cp_parser_skip_to_end_of_statement (parser);
10107 return error_mark_node;
10110 /* Begin the selection-statement. */
10111 if (keyword == RID_IF)
10112 statement = begin_if_stmt ();
10113 else
10114 statement = begin_switch_stmt ();
10116 /* Parse the condition. */
10117 condition = cp_parser_condition (parser);
10118 /* Look for the `)'. */
10119 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10120 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10121 /*consume_paren=*/true);
10123 if (keyword == RID_IF)
10125 bool nested_if;
10126 unsigned char in_statement;
10128 /* Add the condition. */
10129 finish_if_stmt_cond (condition, statement);
10131 /* Parse the then-clause. */
10132 in_statement = parser->in_statement;
10133 parser->in_statement |= IN_IF_STMT;
10134 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10136 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10137 add_stmt (build_empty_stmt (loc));
10138 cp_lexer_consume_token (parser->lexer);
10139 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10140 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10141 "empty body in an %<if%> statement");
10142 nested_if = false;
10144 else
10145 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10146 parser->in_statement = in_statement;
10148 finish_then_clause (statement);
10150 /* If the next token is `else', parse the else-clause. */
10151 if (cp_lexer_next_token_is_keyword (parser->lexer,
10152 RID_ELSE))
10154 /* Consume the `else' keyword. */
10155 cp_lexer_consume_token (parser->lexer);
10156 begin_else_clause (statement);
10157 /* Parse the else-clause. */
10158 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10160 location_t loc;
10161 loc = cp_lexer_peek_token (parser->lexer)->location;
10162 warning_at (loc,
10163 OPT_Wempty_body, "suggest braces around "
10164 "empty body in an %<else%> statement");
10165 add_stmt (build_empty_stmt (loc));
10166 cp_lexer_consume_token (parser->lexer);
10168 else
10169 cp_parser_implicitly_scoped_statement (parser, NULL);
10171 finish_else_clause (statement);
10173 /* If we are currently parsing a then-clause, then
10174 IF_P will not be NULL. We set it to true to
10175 indicate that this if statement has an else clause.
10176 This may trigger the Wparentheses warning below
10177 when we get back up to the parent if statement. */
10178 if (if_p != NULL)
10179 *if_p = true;
10181 else
10183 /* This if statement does not have an else clause. If
10184 NESTED_IF is true, then the then-clause is an if
10185 statement which does have an else clause. We warn
10186 about the potential ambiguity. */
10187 if (nested_if)
10188 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10189 "suggest explicit braces to avoid ambiguous"
10190 " %<else%>");
10193 /* Now we're all done with the if-statement. */
10194 finish_if_stmt (statement);
10196 else
10198 bool in_switch_statement_p;
10199 unsigned char in_statement;
10201 /* Add the condition. */
10202 finish_switch_cond (condition, statement);
10204 /* Parse the body of the switch-statement. */
10205 in_switch_statement_p = parser->in_switch_statement_p;
10206 in_statement = parser->in_statement;
10207 parser->in_switch_statement_p = true;
10208 parser->in_statement |= IN_SWITCH_STMT;
10209 cp_parser_implicitly_scoped_statement (parser, NULL);
10210 parser->in_switch_statement_p = in_switch_statement_p;
10211 parser->in_statement = in_statement;
10213 /* Now we're all done with the switch-statement. */
10214 finish_switch_stmt (statement);
10217 return statement;
10219 break;
10221 default:
10222 cp_parser_error (parser, "expected selection-statement");
10223 return error_mark_node;
10227 /* Parse a condition.
10229 condition:
10230 expression
10231 type-specifier-seq declarator = initializer-clause
10232 type-specifier-seq declarator braced-init-list
10234 GNU Extension:
10236 condition:
10237 type-specifier-seq declarator asm-specification [opt]
10238 attributes [opt] = assignment-expression
10240 Returns the expression that should be tested. */
10242 static tree
10243 cp_parser_condition (cp_parser* parser)
10245 cp_decl_specifier_seq type_specifiers;
10246 const char *saved_message;
10247 int declares_class_or_enum;
10249 /* Try the declaration first. */
10250 cp_parser_parse_tentatively (parser);
10251 /* New types are not allowed in the type-specifier-seq for a
10252 condition. */
10253 saved_message = parser->type_definition_forbidden_message;
10254 parser->type_definition_forbidden_message
10255 = G_("types may not be defined in conditions");
10256 /* Parse the type-specifier-seq. */
10257 cp_parser_decl_specifier_seq (parser,
10258 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10259 &type_specifiers,
10260 &declares_class_or_enum);
10261 /* Restore the saved message. */
10262 parser->type_definition_forbidden_message = saved_message;
10263 /* If all is well, we might be looking at a declaration. */
10264 if (!cp_parser_error_occurred (parser))
10266 tree decl;
10267 tree asm_specification;
10268 tree attributes;
10269 cp_declarator *declarator;
10270 tree initializer = NULL_TREE;
10272 /* Parse the declarator. */
10273 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10274 /*ctor_dtor_or_conv_p=*/NULL,
10275 /*parenthesized_p=*/NULL,
10276 /*member_p=*/false,
10277 /*friend_p=*/false);
10278 /* Parse the attributes. */
10279 attributes = cp_parser_attributes_opt (parser);
10280 /* Parse the asm-specification. */
10281 asm_specification = cp_parser_asm_specification_opt (parser);
10282 /* If the next token is not an `=' or '{', then we might still be
10283 looking at an expression. For example:
10285 if (A(a).x)
10287 looks like a decl-specifier-seq and a declarator -- but then
10288 there is no `=', so this is an expression. */
10289 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10290 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10291 cp_parser_simulate_error (parser);
10293 /* If we did see an `=' or '{', then we are looking at a declaration
10294 for sure. */
10295 if (cp_parser_parse_definitely (parser))
10297 tree pushed_scope;
10298 bool non_constant_p;
10299 bool flags = LOOKUP_ONLYCONVERTING;
10301 /* Create the declaration. */
10302 decl = start_decl (declarator, &type_specifiers,
10303 /*initialized_p=*/true,
10304 attributes, /*prefix_attributes=*/NULL_TREE,
10305 &pushed_scope);
10307 /* Parse the initializer. */
10308 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10310 initializer = cp_parser_braced_list (parser, &non_constant_p);
10311 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10312 flags = 0;
10314 else
10316 /* Consume the `='. */
10317 cp_parser_require (parser, CPP_EQ, RT_EQ);
10318 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10320 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10321 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10323 /* Process the initializer. */
10324 cp_finish_decl (decl,
10325 initializer, !non_constant_p,
10326 asm_specification,
10327 flags);
10329 if (pushed_scope)
10330 pop_scope (pushed_scope);
10332 return convert_from_reference (decl);
10335 /* If we didn't even get past the declarator successfully, we are
10336 definitely not looking at a declaration. */
10337 else
10338 cp_parser_abort_tentative_parse (parser);
10340 /* Otherwise, we are looking at an expression. */
10341 return cp_parser_expression (parser);
10344 /* Parses a for-statement or range-for-statement until the closing ')',
10345 not included. */
10347 static tree
10348 cp_parser_for (cp_parser *parser, bool ivdep)
10350 tree init, scope, decl;
10351 bool is_range_for;
10353 /* Begin the for-statement. */
10354 scope = begin_for_scope (&init);
10356 /* Parse the initialization. */
10357 is_range_for = cp_parser_for_init_statement (parser, &decl);
10359 if (is_range_for)
10360 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10361 else
10362 return cp_parser_c_for (parser, scope, init, ivdep);
10365 static tree
10366 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10368 /* Normal for loop */
10369 tree condition = NULL_TREE;
10370 tree expression = NULL_TREE;
10371 tree stmt;
10373 stmt = begin_for_stmt (scope, init);
10374 /* The for-init-statement has already been parsed in
10375 cp_parser_for_init_statement, so no work is needed here. */
10376 finish_for_init_stmt (stmt);
10378 /* If there's a condition, process it. */
10379 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10380 condition = cp_parser_condition (parser);
10381 else if (ivdep)
10383 cp_parser_error (parser, "missing loop condition in loop with "
10384 "%<GCC ivdep%> pragma");
10385 condition = error_mark_node;
10387 finish_for_cond (condition, stmt, ivdep);
10388 /* Look for the `;'. */
10389 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10391 /* If there's an expression, process it. */
10392 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10393 expression = cp_parser_expression (parser);
10394 finish_for_expr (expression, stmt);
10396 return stmt;
10399 /* Tries to parse a range-based for-statement:
10401 range-based-for:
10402 decl-specifier-seq declarator : expression
10404 The decl-specifier-seq declarator and the `:' are already parsed by
10405 cp_parser_for_init_statement. If processing_template_decl it returns a
10406 newly created RANGE_FOR_STMT; if not, it is converted to a
10407 regular FOR_STMT. */
10409 static tree
10410 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10411 bool ivdep)
10413 tree stmt, range_expr;
10415 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10417 bool expr_non_constant_p;
10418 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10420 else
10421 range_expr = cp_parser_expression (parser);
10423 /* If in template, STMT is converted to a normal for-statement
10424 at instantiation. If not, it is done just ahead. */
10425 if (processing_template_decl)
10427 if (check_for_bare_parameter_packs (range_expr))
10428 range_expr = error_mark_node;
10429 stmt = begin_range_for_stmt (scope, init);
10430 if (ivdep)
10431 RANGE_FOR_IVDEP (stmt) = 1;
10432 finish_range_for_decl (stmt, range_decl, range_expr);
10433 if (!type_dependent_expression_p (range_expr)
10434 /* do_auto_deduction doesn't mess with template init-lists. */
10435 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10436 do_range_for_auto_deduction (range_decl, range_expr);
10438 else
10440 stmt = begin_for_stmt (scope, init);
10441 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10443 return stmt;
10446 /* Subroutine of cp_convert_range_for: given the initializer expression,
10447 builds up the range temporary. */
10449 static tree
10450 build_range_temp (tree range_expr)
10452 tree range_type, range_temp;
10454 /* Find out the type deduced by the declaration
10455 `auto &&__range = range_expr'. */
10456 range_type = cp_build_reference_type (make_auto (), true);
10457 range_type = do_auto_deduction (range_type, range_expr,
10458 type_uses_auto (range_type));
10460 /* Create the __range variable. */
10461 range_temp = build_decl (input_location, VAR_DECL,
10462 get_identifier ("__for_range"), range_type);
10463 TREE_USED (range_temp) = 1;
10464 DECL_ARTIFICIAL (range_temp) = 1;
10466 return range_temp;
10469 /* Used by cp_parser_range_for in template context: we aren't going to
10470 do a full conversion yet, but we still need to resolve auto in the
10471 type of the for-range-declaration if present. This is basically
10472 a shortcut version of cp_convert_range_for. */
10474 static void
10475 do_range_for_auto_deduction (tree decl, tree range_expr)
10477 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10478 if (auto_node)
10480 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10481 range_temp = convert_from_reference (build_range_temp (range_expr));
10482 iter_type = (cp_parser_perform_range_for_lookup
10483 (range_temp, &begin_dummy, &end_dummy));
10484 if (iter_type)
10486 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10487 iter_type);
10488 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10489 tf_warning_or_error);
10490 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10491 iter_decl, auto_node);
10496 /* Converts a range-based for-statement into a normal
10497 for-statement, as per the definition.
10499 for (RANGE_DECL : RANGE_EXPR)
10500 BLOCK
10502 should be equivalent to:
10505 auto &&__range = RANGE_EXPR;
10506 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10507 __begin != __end;
10508 ++__begin)
10510 RANGE_DECL = *__begin;
10511 BLOCK
10515 If RANGE_EXPR is an array:
10516 BEGIN_EXPR = __range
10517 END_EXPR = __range + ARRAY_SIZE(__range)
10518 Else if RANGE_EXPR has a member 'begin' or 'end':
10519 BEGIN_EXPR = __range.begin()
10520 END_EXPR = __range.end()
10521 Else:
10522 BEGIN_EXPR = begin(__range)
10523 END_EXPR = end(__range);
10525 If __range has a member 'begin' but not 'end', or vice versa, we must
10526 still use the second alternative (it will surely fail, however).
10527 When calling begin()/end() in the third alternative we must use
10528 argument dependent lookup, but always considering 'std' as an associated
10529 namespace. */
10531 tree
10532 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10533 bool ivdep)
10535 tree begin, end;
10536 tree iter_type, begin_expr, end_expr;
10537 tree condition, expression;
10539 if (range_decl == error_mark_node || range_expr == error_mark_node)
10540 /* If an error happened previously do nothing or else a lot of
10541 unhelpful errors would be issued. */
10542 begin_expr = end_expr = iter_type = error_mark_node;
10543 else
10545 tree range_temp;
10547 if (TREE_CODE (range_expr) == VAR_DECL
10548 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10549 /* Can't bind a reference to an array of runtime bound. */
10550 range_temp = range_expr;
10551 else
10553 range_temp = build_range_temp (range_expr);
10554 pushdecl (range_temp);
10555 cp_finish_decl (range_temp, range_expr,
10556 /*is_constant_init*/false, NULL_TREE,
10557 LOOKUP_ONLYCONVERTING);
10558 range_temp = convert_from_reference (range_temp);
10560 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10561 &begin_expr, &end_expr);
10564 /* The new for initialization statement. */
10565 begin = build_decl (input_location, VAR_DECL,
10566 get_identifier ("__for_begin"), iter_type);
10567 TREE_USED (begin) = 1;
10568 DECL_ARTIFICIAL (begin) = 1;
10569 pushdecl (begin);
10570 cp_finish_decl (begin, begin_expr,
10571 /*is_constant_init*/false, NULL_TREE,
10572 LOOKUP_ONLYCONVERTING);
10574 end = build_decl (input_location, VAR_DECL,
10575 get_identifier ("__for_end"), iter_type);
10576 TREE_USED (end) = 1;
10577 DECL_ARTIFICIAL (end) = 1;
10578 pushdecl (end);
10579 cp_finish_decl (end, end_expr,
10580 /*is_constant_init*/false, NULL_TREE,
10581 LOOKUP_ONLYCONVERTING);
10583 finish_for_init_stmt (statement);
10585 /* The new for condition. */
10586 condition = build_x_binary_op (input_location, NE_EXPR,
10587 begin, ERROR_MARK,
10588 end, ERROR_MARK,
10589 NULL, tf_warning_or_error);
10590 finish_for_cond (condition, statement, ivdep);
10592 /* The new increment expression. */
10593 expression = finish_unary_op_expr (input_location,
10594 PREINCREMENT_EXPR, begin,
10595 tf_warning_or_error);
10596 finish_for_expr (expression, statement);
10598 /* The declaration is initialized with *__begin inside the loop body. */
10599 cp_finish_decl (range_decl,
10600 build_x_indirect_ref (input_location, begin, RO_NULL,
10601 tf_warning_or_error),
10602 /*is_constant_init*/false, NULL_TREE,
10603 LOOKUP_ONLYCONVERTING);
10605 return statement;
10608 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10609 We need to solve both at the same time because the method used
10610 depends on the existence of members begin or end.
10611 Returns the type deduced for the iterator expression. */
10613 static tree
10614 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10616 if (error_operand_p (range))
10618 *begin = *end = error_mark_node;
10619 return error_mark_node;
10622 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10624 error ("range-based %<for%> expression of type %qT "
10625 "has incomplete type", TREE_TYPE (range));
10626 *begin = *end = error_mark_node;
10627 return error_mark_node;
10629 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10631 /* If RANGE is an array, we will use pointer arithmetic. */
10632 *begin = range;
10633 *end = build_binary_op (input_location, PLUS_EXPR,
10634 range,
10635 array_type_nelts_top (TREE_TYPE (range)),
10637 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10639 else
10641 /* If it is not an array, we must do a bit of magic. */
10642 tree id_begin, id_end;
10643 tree member_begin, member_end;
10645 *begin = *end = error_mark_node;
10647 id_begin = get_identifier ("begin");
10648 id_end = get_identifier ("end");
10649 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10650 /*protect=*/2, /*want_type=*/false,
10651 tf_warning_or_error);
10652 member_end = lookup_member (TREE_TYPE (range), id_end,
10653 /*protect=*/2, /*want_type=*/false,
10654 tf_warning_or_error);
10656 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10658 /* Use the member functions. */
10659 if (member_begin != NULL_TREE)
10660 *begin = cp_parser_range_for_member_function (range, id_begin);
10661 else
10662 error ("range-based %<for%> expression of type %qT has an "
10663 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10665 if (member_end != NULL_TREE)
10666 *end = cp_parser_range_for_member_function (range, id_end);
10667 else
10668 error ("range-based %<for%> expression of type %qT has a "
10669 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10671 else
10673 /* Use global functions with ADL. */
10674 vec<tree, va_gc> *vec;
10675 vec = make_tree_vector ();
10677 vec_safe_push (vec, range);
10679 member_begin = perform_koenig_lookup (id_begin, vec,
10680 tf_warning_or_error);
10681 *begin = finish_call_expr (member_begin, &vec, false, true,
10682 tf_warning_or_error);
10683 member_end = perform_koenig_lookup (id_end, vec,
10684 tf_warning_or_error);
10685 *end = finish_call_expr (member_end, &vec, false, true,
10686 tf_warning_or_error);
10688 release_tree_vector (vec);
10691 /* Last common checks. */
10692 if (*begin == error_mark_node || *end == error_mark_node)
10694 /* If one of the expressions is an error do no more checks. */
10695 *begin = *end = error_mark_node;
10696 return error_mark_node;
10698 else if (type_dependent_expression_p (*begin)
10699 || type_dependent_expression_p (*end))
10700 /* Can happen, when, eg, in a template context, Koenig lookup
10701 can't resolve begin/end (c++/58503). */
10702 return NULL_TREE;
10703 else
10705 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10706 /* The unqualified type of the __begin and __end temporaries should
10707 be the same, as required by the multiple auto declaration. */
10708 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10709 error ("inconsistent begin/end types in range-based %<for%> "
10710 "statement: %qT and %qT",
10711 TREE_TYPE (*begin), TREE_TYPE (*end));
10712 return iter_type;
10717 /* Helper function for cp_parser_perform_range_for_lookup.
10718 Builds a tree for RANGE.IDENTIFIER(). */
10720 static tree
10721 cp_parser_range_for_member_function (tree range, tree identifier)
10723 tree member, res;
10724 vec<tree, va_gc> *vec;
10726 member = finish_class_member_access_expr (range, identifier,
10727 false, tf_warning_or_error);
10728 if (member == error_mark_node)
10729 return error_mark_node;
10731 vec = make_tree_vector ();
10732 res = finish_call_expr (member, &vec,
10733 /*disallow_virtual=*/false,
10734 /*koenig_p=*/false,
10735 tf_warning_or_error);
10736 release_tree_vector (vec);
10737 return res;
10740 /* Parse an iteration-statement.
10742 iteration-statement:
10743 while ( condition ) statement
10744 do statement while ( expression ) ;
10745 for ( for-init-statement condition [opt] ; expression [opt] )
10746 statement
10748 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10750 static tree
10751 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10753 cp_token *token;
10754 enum rid keyword;
10755 tree statement;
10756 unsigned char in_statement;
10758 /* Peek at the next token. */
10759 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10760 if (!token)
10761 return error_mark_node;
10763 /* Remember whether or not we are already within an iteration
10764 statement. */
10765 in_statement = parser->in_statement;
10767 /* See what kind of keyword it is. */
10768 keyword = token->keyword;
10769 switch (keyword)
10771 case RID_WHILE:
10773 tree condition;
10775 /* Begin the while-statement. */
10776 statement = begin_while_stmt ();
10777 /* Look for the `('. */
10778 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10779 /* Parse the condition. */
10780 condition = cp_parser_condition (parser);
10781 finish_while_stmt_cond (condition, statement, ivdep);
10782 /* Look for the `)'. */
10783 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10784 /* Parse the dependent statement. */
10785 parser->in_statement = IN_ITERATION_STMT;
10786 cp_parser_already_scoped_statement (parser);
10787 parser->in_statement = in_statement;
10788 /* We're done with the while-statement. */
10789 finish_while_stmt (statement);
10791 break;
10793 case RID_DO:
10795 tree expression;
10797 /* Begin the do-statement. */
10798 statement = begin_do_stmt ();
10799 /* Parse the body of the do-statement. */
10800 parser->in_statement = IN_ITERATION_STMT;
10801 cp_parser_implicitly_scoped_statement (parser, NULL);
10802 parser->in_statement = in_statement;
10803 finish_do_body (statement);
10804 /* Look for the `while' keyword. */
10805 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10806 /* Look for the `('. */
10807 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10808 /* Parse the expression. */
10809 expression = cp_parser_expression (parser);
10810 /* We're done with the do-statement. */
10811 finish_do_stmt (expression, statement, ivdep);
10812 /* Look for the `)'. */
10813 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10814 /* Look for the `;'. */
10815 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10817 break;
10819 case RID_FOR:
10821 /* Look for the `('. */
10822 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10824 statement = cp_parser_for (parser, ivdep);
10826 /* Look for the `)'. */
10827 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10829 /* Parse the body of the for-statement. */
10830 parser->in_statement = IN_ITERATION_STMT;
10831 cp_parser_already_scoped_statement (parser);
10832 parser->in_statement = in_statement;
10834 /* We're done with the for-statement. */
10835 finish_for_stmt (statement);
10837 break;
10839 default:
10840 cp_parser_error (parser, "expected iteration-statement");
10841 statement = error_mark_node;
10842 break;
10845 return statement;
10848 /* Parse a for-init-statement or the declarator of a range-based-for.
10849 Returns true if a range-based-for declaration is seen.
10851 for-init-statement:
10852 expression-statement
10853 simple-declaration */
10855 static bool
10856 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10858 /* If the next token is a `;', then we have an empty
10859 expression-statement. Grammatically, this is also a
10860 simple-declaration, but an invalid one, because it does not
10861 declare anything. Therefore, if we did not handle this case
10862 specially, we would issue an error message about an invalid
10863 declaration. */
10864 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10866 bool is_range_for = false;
10867 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10869 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10870 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10872 /* N3994 -- for (id : init) ... */
10873 if (cxx_dialect < cxx1z)
10874 pedwarn (input_location, 0, "range-based for loop without a "
10875 "type-specifier only available with "
10876 "-std=c++1z or -std=gnu++1z");
10877 tree name = cp_parser_identifier (parser);
10878 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10879 *decl = build_decl (input_location, VAR_DECL, name, type);
10880 pushdecl (*decl);
10881 cp_lexer_consume_token (parser->lexer);
10882 return true;
10885 /* A colon is used in range-based for. */
10886 parser->colon_corrects_to_scope_p = false;
10888 /* We're going to speculatively look for a declaration, falling back
10889 to an expression, if necessary. */
10890 cp_parser_parse_tentatively (parser);
10891 /* Parse the declaration. */
10892 cp_parser_simple_declaration (parser,
10893 /*function_definition_allowed_p=*/false,
10894 decl);
10895 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10896 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10898 /* It is a range-for, consume the ':' */
10899 cp_lexer_consume_token (parser->lexer);
10900 is_range_for = true;
10901 if (cxx_dialect < cxx11)
10903 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10904 "range-based %<for%> loops only available with "
10905 "-std=c++11 or -std=gnu++11");
10906 *decl = error_mark_node;
10909 else
10910 /* The ';' is not consumed yet because we told
10911 cp_parser_simple_declaration not to. */
10912 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10914 if (cp_parser_parse_definitely (parser))
10915 return is_range_for;
10916 /* If the tentative parse failed, then we shall need to look for an
10917 expression-statement. */
10919 /* If we are here, it is an expression-statement. */
10920 cp_parser_expression_statement (parser, NULL_TREE);
10921 return false;
10924 /* Parse a jump-statement.
10926 jump-statement:
10927 break ;
10928 continue ;
10929 return expression [opt] ;
10930 return braced-init-list ;
10931 goto identifier ;
10933 GNU extension:
10935 jump-statement:
10936 goto * expression ;
10938 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10940 static tree
10941 cp_parser_jump_statement (cp_parser* parser)
10943 tree statement = error_mark_node;
10944 cp_token *token;
10945 enum rid keyword;
10946 unsigned char in_statement;
10948 /* Peek at the next token. */
10949 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10950 if (!token)
10951 return error_mark_node;
10953 /* See what kind of keyword it is. */
10954 keyword = token->keyword;
10955 switch (keyword)
10957 case RID_BREAK:
10958 in_statement = parser->in_statement & ~IN_IF_STMT;
10959 switch (in_statement)
10961 case 0:
10962 error_at (token->location, "break statement not within loop or switch");
10963 break;
10964 default:
10965 gcc_assert ((in_statement & IN_SWITCH_STMT)
10966 || in_statement == IN_ITERATION_STMT);
10967 statement = finish_break_stmt ();
10968 if (in_statement == IN_ITERATION_STMT)
10969 break_maybe_infinite_loop ();
10970 break;
10971 case IN_OMP_BLOCK:
10972 error_at (token->location, "invalid exit from OpenMP structured block");
10973 break;
10974 case IN_OMP_FOR:
10975 error_at (token->location, "break statement used with OpenMP for loop");
10976 break;
10977 case IN_CILK_SIMD_FOR:
10978 error_at (token->location, "break statement used with Cilk Plus for loop");
10979 break;
10981 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10982 break;
10984 case RID_CONTINUE:
10985 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10987 case 0:
10988 error_at (token->location, "continue statement not within a loop");
10989 break;
10990 case IN_CILK_SIMD_FOR:
10991 error_at (token->location,
10992 "continue statement within %<#pragma simd%> loop body");
10993 /* Fall through. */
10994 case IN_ITERATION_STMT:
10995 case IN_OMP_FOR:
10996 statement = finish_continue_stmt ();
10997 break;
10998 case IN_OMP_BLOCK:
10999 error_at (token->location, "invalid exit from OpenMP structured block");
11000 break;
11001 default:
11002 gcc_unreachable ();
11004 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11005 break;
11007 case RID_RETURN:
11009 tree expr;
11010 bool expr_non_constant_p;
11012 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11014 cp_lexer_set_source_position (parser->lexer);
11015 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11016 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11018 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11019 expr = cp_parser_expression (parser);
11020 else
11021 /* If the next token is a `;', then there is no
11022 expression. */
11023 expr = NULL_TREE;
11024 /* Build the return-statement. */
11025 statement = finish_return_stmt (expr);
11026 /* Look for the final `;'. */
11027 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11029 break;
11031 case RID_GOTO:
11032 if (parser->in_function_body
11033 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11035 error ("%<goto%> in %<constexpr%> function");
11036 cp_function_chain->invalid_constexpr = true;
11039 /* Create the goto-statement. */
11040 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11042 /* Issue a warning about this use of a GNU extension. */
11043 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11044 /* Consume the '*' token. */
11045 cp_lexer_consume_token (parser->lexer);
11046 /* Parse the dependent expression. */
11047 finish_goto_stmt (cp_parser_expression (parser));
11049 else
11050 finish_goto_stmt (cp_parser_identifier (parser));
11051 /* Look for the final `;'. */
11052 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11053 break;
11055 default:
11056 cp_parser_error (parser, "expected jump-statement");
11057 break;
11060 return statement;
11063 /* Parse a declaration-statement.
11065 declaration-statement:
11066 block-declaration */
11068 static void
11069 cp_parser_declaration_statement (cp_parser* parser)
11071 void *p;
11073 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11074 p = obstack_alloc (&declarator_obstack, 0);
11076 /* Parse the block-declaration. */
11077 cp_parser_block_declaration (parser, /*statement_p=*/true);
11079 /* Free any declarators allocated. */
11080 obstack_free (&declarator_obstack, p);
11083 /* Some dependent statements (like `if (cond) statement'), are
11084 implicitly in their own scope. In other words, if the statement is
11085 a single statement (as opposed to a compound-statement), it is
11086 none-the-less treated as if it were enclosed in braces. Any
11087 declarations appearing in the dependent statement are out of scope
11088 after control passes that point. This function parses a statement,
11089 but ensures that is in its own scope, even if it is not a
11090 compound-statement.
11092 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11093 is a (possibly labeled) if statement which is not enclosed in
11094 braces and has an else clause. This is used to implement
11095 -Wparentheses.
11097 Returns the new statement. */
11099 static tree
11100 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11102 tree statement;
11104 if (if_p != NULL)
11105 *if_p = false;
11107 /* Mark if () ; with a special NOP_EXPR. */
11108 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11110 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11111 cp_lexer_consume_token (parser->lexer);
11112 statement = add_stmt (build_empty_stmt (loc));
11114 /* if a compound is opened, we simply parse the statement directly. */
11115 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11116 statement = cp_parser_compound_statement (parser, NULL, false, false);
11117 /* If the token is not a `{', then we must take special action. */
11118 else
11120 /* Create a compound-statement. */
11121 statement = begin_compound_stmt (0);
11122 /* Parse the dependent-statement. */
11123 cp_parser_statement (parser, NULL_TREE, false, if_p);
11124 /* Finish the dummy compound-statement. */
11125 finish_compound_stmt (statement);
11128 /* Return the statement. */
11129 return statement;
11132 /* For some dependent statements (like `while (cond) statement'), we
11133 have already created a scope. Therefore, even if the dependent
11134 statement is a compound-statement, we do not want to create another
11135 scope. */
11137 static void
11138 cp_parser_already_scoped_statement (cp_parser* parser)
11140 /* If the token is a `{', then we must take special action. */
11141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11142 cp_parser_statement (parser, NULL_TREE, false, NULL);
11143 else
11145 /* Avoid calling cp_parser_compound_statement, so that we
11146 don't create a new scope. Do everything else by hand. */
11147 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11148 /* If the next keyword is `__label__' we have a label declaration. */
11149 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11150 cp_parser_label_declaration (parser);
11151 /* Parse an (optional) statement-seq. */
11152 cp_parser_statement_seq_opt (parser, NULL_TREE);
11153 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11157 /* Declarations [gram.dcl.dcl] */
11159 /* Parse an optional declaration-sequence.
11161 declaration-seq:
11162 declaration
11163 declaration-seq declaration */
11165 static void
11166 cp_parser_declaration_seq_opt (cp_parser* parser)
11168 while (true)
11170 cp_token *token;
11172 token = cp_lexer_peek_token (parser->lexer);
11174 if (token->type == CPP_CLOSE_BRACE
11175 || token->type == CPP_EOF
11176 || token->type == CPP_PRAGMA_EOL)
11177 break;
11179 if (token->type == CPP_SEMICOLON)
11181 /* A declaration consisting of a single semicolon is
11182 invalid. Allow it unless we're being pedantic. */
11183 cp_lexer_consume_token (parser->lexer);
11184 if (!in_system_header_at (input_location))
11185 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11186 continue;
11189 /* If we're entering or exiting a region that's implicitly
11190 extern "C", modify the lang context appropriately. */
11191 if (!parser->implicit_extern_c && token->implicit_extern_c)
11193 push_lang_context (lang_name_c);
11194 parser->implicit_extern_c = true;
11196 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11198 pop_lang_context ();
11199 parser->implicit_extern_c = false;
11202 if (token->type == CPP_PRAGMA)
11204 /* A top-level declaration can consist solely of a #pragma.
11205 A nested declaration cannot, so this is done here and not
11206 in cp_parser_declaration. (A #pragma at block scope is
11207 handled in cp_parser_statement.) */
11208 cp_parser_pragma (parser, pragma_external);
11209 continue;
11212 /* Parse the declaration itself. */
11213 cp_parser_declaration (parser);
11217 /* Parse a declaration.
11219 declaration:
11220 block-declaration
11221 function-definition
11222 template-declaration
11223 explicit-instantiation
11224 explicit-specialization
11225 linkage-specification
11226 namespace-definition
11228 GNU extension:
11230 declaration:
11231 __extension__ declaration */
11233 static void
11234 cp_parser_declaration (cp_parser* parser)
11236 cp_token token1;
11237 cp_token token2;
11238 int saved_pedantic;
11239 void *p;
11240 tree attributes = NULL_TREE;
11242 /* Check for the `__extension__' keyword. */
11243 if (cp_parser_extension_opt (parser, &saved_pedantic))
11245 /* Parse the qualified declaration. */
11246 cp_parser_declaration (parser);
11247 /* Restore the PEDANTIC flag. */
11248 pedantic = saved_pedantic;
11250 return;
11253 /* Try to figure out what kind of declaration is present. */
11254 token1 = *cp_lexer_peek_token (parser->lexer);
11256 if (token1.type != CPP_EOF)
11257 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11258 else
11260 token2.type = CPP_EOF;
11261 token2.keyword = RID_MAX;
11264 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11265 p = obstack_alloc (&declarator_obstack, 0);
11267 /* If the next token is `extern' and the following token is a string
11268 literal, then we have a linkage specification. */
11269 if (token1.keyword == RID_EXTERN
11270 && cp_parser_is_pure_string_literal (&token2))
11271 cp_parser_linkage_specification (parser);
11272 /* If the next token is `template', then we have either a template
11273 declaration, an explicit instantiation, or an explicit
11274 specialization. */
11275 else if (token1.keyword == RID_TEMPLATE)
11277 /* `template <>' indicates a template specialization. */
11278 if (token2.type == CPP_LESS
11279 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11280 cp_parser_explicit_specialization (parser);
11281 /* `template <' indicates a template declaration. */
11282 else if (token2.type == CPP_LESS)
11283 cp_parser_template_declaration (parser, /*member_p=*/false);
11284 /* Anything else must be an explicit instantiation. */
11285 else
11286 cp_parser_explicit_instantiation (parser);
11288 /* If the next token is `export', then we have a template
11289 declaration. */
11290 else if (token1.keyword == RID_EXPORT)
11291 cp_parser_template_declaration (parser, /*member_p=*/false);
11292 /* If the next token is `extern', 'static' or 'inline' and the one
11293 after that is `template', we have a GNU extended explicit
11294 instantiation directive. */
11295 else if (cp_parser_allow_gnu_extensions_p (parser)
11296 && (token1.keyword == RID_EXTERN
11297 || token1.keyword == RID_STATIC
11298 || token1.keyword == RID_INLINE)
11299 && token2.keyword == RID_TEMPLATE)
11300 cp_parser_explicit_instantiation (parser);
11301 /* If the next token is `namespace', check for a named or unnamed
11302 namespace definition. */
11303 else if (token1.keyword == RID_NAMESPACE
11304 && (/* A named namespace definition. */
11305 (token2.type == CPP_NAME
11306 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11307 != CPP_EQ))
11308 /* An unnamed namespace definition. */
11309 || token2.type == CPP_OPEN_BRACE
11310 || token2.keyword == RID_ATTRIBUTE))
11311 cp_parser_namespace_definition (parser);
11312 /* An inline (associated) namespace definition. */
11313 else if (token1.keyword == RID_INLINE
11314 && token2.keyword == RID_NAMESPACE)
11315 cp_parser_namespace_definition (parser);
11316 /* Objective-C++ declaration/definition. */
11317 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11318 cp_parser_objc_declaration (parser, NULL_TREE);
11319 else if (c_dialect_objc ()
11320 && token1.keyword == RID_ATTRIBUTE
11321 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11322 cp_parser_objc_declaration (parser, attributes);
11323 /* We must have either a block declaration or a function
11324 definition. */
11325 else
11326 /* Try to parse a block-declaration, or a function-definition. */
11327 cp_parser_block_declaration (parser, /*statement_p=*/false);
11329 /* Free any declarators allocated. */
11330 obstack_free (&declarator_obstack, p);
11333 /* Parse a block-declaration.
11335 block-declaration:
11336 simple-declaration
11337 asm-definition
11338 namespace-alias-definition
11339 using-declaration
11340 using-directive
11342 GNU Extension:
11344 block-declaration:
11345 __extension__ block-declaration
11347 C++0x Extension:
11349 block-declaration:
11350 static_assert-declaration
11352 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11353 part of a declaration-statement. */
11355 static void
11356 cp_parser_block_declaration (cp_parser *parser,
11357 bool statement_p)
11359 cp_token *token1;
11360 int saved_pedantic;
11362 /* Check for the `__extension__' keyword. */
11363 if (cp_parser_extension_opt (parser, &saved_pedantic))
11365 /* Parse the qualified declaration. */
11366 cp_parser_block_declaration (parser, statement_p);
11367 /* Restore the PEDANTIC flag. */
11368 pedantic = saved_pedantic;
11370 return;
11373 /* Peek at the next token to figure out which kind of declaration is
11374 present. */
11375 token1 = cp_lexer_peek_token (parser->lexer);
11377 /* If the next keyword is `asm', we have an asm-definition. */
11378 if (token1->keyword == RID_ASM)
11380 if (statement_p)
11381 cp_parser_commit_to_tentative_parse (parser);
11382 cp_parser_asm_definition (parser);
11384 /* If the next keyword is `namespace', we have a
11385 namespace-alias-definition. */
11386 else if (token1->keyword == RID_NAMESPACE)
11387 cp_parser_namespace_alias_definition (parser);
11388 /* If the next keyword is `using', we have a
11389 using-declaration, a using-directive, or an alias-declaration. */
11390 else if (token1->keyword == RID_USING)
11392 cp_token *token2;
11394 if (statement_p)
11395 cp_parser_commit_to_tentative_parse (parser);
11396 /* If the token after `using' is `namespace', then we have a
11397 using-directive. */
11398 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11399 if (token2->keyword == RID_NAMESPACE)
11400 cp_parser_using_directive (parser);
11401 /* If the second token after 'using' is '=', then we have an
11402 alias-declaration. */
11403 else if (cxx_dialect >= cxx11
11404 && token2->type == CPP_NAME
11405 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11406 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11407 cp_parser_alias_declaration (parser);
11408 /* Otherwise, it's a using-declaration. */
11409 else
11410 cp_parser_using_declaration (parser,
11411 /*access_declaration_p=*/false);
11413 /* If the next keyword is `__label__' we have a misplaced label
11414 declaration. */
11415 else if (token1->keyword == RID_LABEL)
11417 cp_lexer_consume_token (parser->lexer);
11418 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11419 cp_parser_skip_to_end_of_statement (parser);
11420 /* If the next token is now a `;', consume it. */
11421 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11422 cp_lexer_consume_token (parser->lexer);
11424 /* If the next token is `static_assert' we have a static assertion. */
11425 else if (token1->keyword == RID_STATIC_ASSERT)
11426 cp_parser_static_assert (parser, /*member_p=*/false);
11427 /* Anything else must be a simple-declaration. */
11428 else
11429 cp_parser_simple_declaration (parser, !statement_p,
11430 /*maybe_range_for_decl*/NULL);
11433 /* Parse a simple-declaration.
11435 simple-declaration:
11436 decl-specifier-seq [opt] init-declarator-list [opt] ;
11438 init-declarator-list:
11439 init-declarator
11440 init-declarator-list , init-declarator
11442 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11443 function-definition as a simple-declaration.
11445 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11446 parsed declaration if it is an uninitialized single declarator not followed
11447 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11448 if present, will not be consumed. */
11450 static void
11451 cp_parser_simple_declaration (cp_parser* parser,
11452 bool function_definition_allowed_p,
11453 tree *maybe_range_for_decl)
11455 cp_decl_specifier_seq decl_specifiers;
11456 int declares_class_or_enum;
11457 bool saw_declarator;
11458 location_t comma_loc = UNKNOWN_LOCATION;
11459 location_t init_loc = UNKNOWN_LOCATION;
11461 if (maybe_range_for_decl)
11462 *maybe_range_for_decl = NULL_TREE;
11464 /* Defer access checks until we know what is being declared; the
11465 checks for names appearing in the decl-specifier-seq should be
11466 done as if we were in the scope of the thing being declared. */
11467 push_deferring_access_checks (dk_deferred);
11469 /* Parse the decl-specifier-seq. We have to keep track of whether
11470 or not the decl-specifier-seq declares a named class or
11471 enumeration type, since that is the only case in which the
11472 init-declarator-list is allowed to be empty.
11474 [dcl.dcl]
11476 In a simple-declaration, the optional init-declarator-list can be
11477 omitted only when declaring a class or enumeration, that is when
11478 the decl-specifier-seq contains either a class-specifier, an
11479 elaborated-type-specifier, or an enum-specifier. */
11480 cp_parser_decl_specifier_seq (parser,
11481 CP_PARSER_FLAGS_OPTIONAL,
11482 &decl_specifiers,
11483 &declares_class_or_enum);
11484 /* We no longer need to defer access checks. */
11485 stop_deferring_access_checks ();
11487 /* In a block scope, a valid declaration must always have a
11488 decl-specifier-seq. By not trying to parse declarators, we can
11489 resolve the declaration/expression ambiguity more quickly. */
11490 if (!function_definition_allowed_p
11491 && !decl_specifiers.any_specifiers_p)
11493 cp_parser_error (parser, "expected declaration");
11494 goto done;
11497 /* If the next two tokens are both identifiers, the code is
11498 erroneous. The usual cause of this situation is code like:
11500 T t;
11502 where "T" should name a type -- but does not. */
11503 if (!decl_specifiers.any_type_specifiers_p
11504 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11506 /* If parsing tentatively, we should commit; we really are
11507 looking at a declaration. */
11508 cp_parser_commit_to_tentative_parse (parser);
11509 /* Give up. */
11510 goto done;
11513 /* If we have seen at least one decl-specifier, and the next token
11514 is not a parenthesis, then we must be looking at a declaration.
11515 (After "int (" we might be looking at a functional cast.) */
11516 if (decl_specifiers.any_specifiers_p
11517 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11518 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11519 && !cp_parser_error_occurred (parser))
11520 cp_parser_commit_to_tentative_parse (parser);
11522 /* Keep going until we hit the `;' at the end of the simple
11523 declaration. */
11524 saw_declarator = false;
11525 while (cp_lexer_next_token_is_not (parser->lexer,
11526 CPP_SEMICOLON))
11528 cp_token *token;
11529 bool function_definition_p;
11530 tree decl;
11532 if (saw_declarator)
11534 /* If we are processing next declarator, comma is expected */
11535 token = cp_lexer_peek_token (parser->lexer);
11536 gcc_assert (token->type == CPP_COMMA);
11537 cp_lexer_consume_token (parser->lexer);
11538 if (maybe_range_for_decl)
11540 *maybe_range_for_decl = error_mark_node;
11541 if (comma_loc == UNKNOWN_LOCATION)
11542 comma_loc = token->location;
11545 else
11546 saw_declarator = true;
11548 /* Parse the init-declarator. */
11549 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11550 /*checks=*/NULL,
11551 function_definition_allowed_p,
11552 /*member_p=*/false,
11553 declares_class_or_enum,
11554 &function_definition_p,
11555 maybe_range_for_decl,
11556 &init_loc);
11557 /* If an error occurred while parsing tentatively, exit quickly.
11558 (That usually happens when in the body of a function; each
11559 statement is treated as a declaration-statement until proven
11560 otherwise.) */
11561 if (cp_parser_error_occurred (parser))
11562 goto done;
11563 /* Handle function definitions specially. */
11564 if (function_definition_p)
11566 /* If the next token is a `,', then we are probably
11567 processing something like:
11569 void f() {}, *p;
11571 which is erroneous. */
11572 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11574 cp_token *token = cp_lexer_peek_token (parser->lexer);
11575 error_at (token->location,
11576 "mixing"
11577 " declarations and function-definitions is forbidden");
11579 /* Otherwise, we're done with the list of declarators. */
11580 else
11582 pop_deferring_access_checks ();
11583 return;
11586 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11587 *maybe_range_for_decl = decl;
11588 /* The next token should be either a `,' or a `;'. */
11589 token = cp_lexer_peek_token (parser->lexer);
11590 /* If it's a `,', there are more declarators to come. */
11591 if (token->type == CPP_COMMA)
11592 /* will be consumed next time around */;
11593 /* If it's a `;', we are done. */
11594 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11595 break;
11596 /* Anything else is an error. */
11597 else
11599 /* If we have already issued an error message we don't need
11600 to issue another one. */
11601 if (decl != error_mark_node
11602 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11603 cp_parser_error (parser, "expected %<,%> or %<;%>");
11604 /* Skip tokens until we reach the end of the statement. */
11605 cp_parser_skip_to_end_of_statement (parser);
11606 /* If the next token is now a `;', consume it. */
11607 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11608 cp_lexer_consume_token (parser->lexer);
11609 goto done;
11611 /* After the first time around, a function-definition is not
11612 allowed -- even if it was OK at first. For example:
11614 int i, f() {}
11616 is not valid. */
11617 function_definition_allowed_p = false;
11620 /* Issue an error message if no declarators are present, and the
11621 decl-specifier-seq does not itself declare a class or
11622 enumeration: [dcl.dcl]/3. */
11623 if (!saw_declarator)
11625 if (cp_parser_declares_only_class_p (parser))
11627 if (!declares_class_or_enum
11628 && decl_specifiers.type
11629 && OVERLOAD_TYPE_P (decl_specifiers.type))
11630 /* Ensure an error is issued anyway when finish_decltype_type,
11631 called via cp_parser_decl_specifier_seq, returns a class or
11632 an enumeration (c++/51786). */
11633 decl_specifiers.type = NULL_TREE;
11634 shadow_tag (&decl_specifiers);
11636 /* Perform any deferred access checks. */
11637 perform_deferred_access_checks (tf_warning_or_error);
11640 /* Consume the `;'. */
11641 if (!maybe_range_for_decl)
11642 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11643 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11645 if (init_loc != UNKNOWN_LOCATION)
11646 error_at (init_loc, "initializer in range-based %<for%> loop");
11647 if (comma_loc != UNKNOWN_LOCATION)
11648 error_at (comma_loc,
11649 "multiple declarations in range-based %<for%> loop");
11652 done:
11653 pop_deferring_access_checks ();
11656 /* Parse a decl-specifier-seq.
11658 decl-specifier-seq:
11659 decl-specifier-seq [opt] decl-specifier
11660 decl-specifier attribute-specifier-seq [opt] (C++11)
11662 decl-specifier:
11663 storage-class-specifier
11664 type-specifier
11665 function-specifier
11666 friend
11667 typedef
11669 GNU Extension:
11671 decl-specifier:
11672 attributes
11674 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11676 The parser flags FLAGS is used to control type-specifier parsing.
11678 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11679 flags:
11681 1: one of the decl-specifiers is an elaborated-type-specifier
11682 (i.e., a type declaration)
11683 2: one of the decl-specifiers is an enum-specifier or a
11684 class-specifier (i.e., a type definition)
11688 static void
11689 cp_parser_decl_specifier_seq (cp_parser* parser,
11690 cp_parser_flags flags,
11691 cp_decl_specifier_seq *decl_specs,
11692 int* declares_class_or_enum)
11694 bool constructor_possible_p = !parser->in_declarator_p;
11695 bool found_decl_spec = false;
11696 cp_token *start_token = NULL;
11697 cp_decl_spec ds;
11699 /* Clear DECL_SPECS. */
11700 clear_decl_specs (decl_specs);
11702 /* Assume no class or enumeration type is declared. */
11703 *declares_class_or_enum = 0;
11705 /* Keep reading specifiers until there are no more to read. */
11706 while (true)
11708 bool constructor_p;
11709 cp_token *token;
11710 ds = ds_last;
11712 /* Peek at the next token. */
11713 token = cp_lexer_peek_token (parser->lexer);
11715 /* Save the first token of the decl spec list for error
11716 reporting. */
11717 if (!start_token)
11718 start_token = token;
11719 /* Handle attributes. */
11720 if (cp_next_tokens_can_be_attribute_p (parser))
11722 /* Parse the attributes. */
11723 tree attrs = cp_parser_attributes_opt (parser);
11725 /* In a sequence of declaration specifiers, c++11 attributes
11726 appertain to the type that precede them. In that case
11727 [dcl.spec]/1 says:
11729 The attribute-specifier-seq affects the type only for
11730 the declaration it appears in, not other declarations
11731 involving the same type.
11733 But for now let's force the user to position the
11734 attribute either at the beginning of the declaration or
11735 after the declarator-id, which would clearly mean that it
11736 applies to the declarator. */
11737 if (cxx11_attribute_p (attrs))
11739 if (!found_decl_spec)
11740 /* The c++11 attribute is at the beginning of the
11741 declaration. It appertains to the entity being
11742 declared. */;
11743 else
11745 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11747 /* This is an attribute following a
11748 class-specifier. */
11749 if (decl_specs->type_definition_p)
11750 warn_misplaced_attr_for_class_type (token->location,
11751 decl_specs->type);
11752 attrs = NULL_TREE;
11754 else
11756 decl_specs->std_attributes
11757 = chainon (decl_specs->std_attributes,
11758 attrs);
11759 if (decl_specs->locations[ds_std_attribute] == 0)
11760 decl_specs->locations[ds_std_attribute] = token->location;
11762 continue;
11766 decl_specs->attributes
11767 = chainon (decl_specs->attributes,
11768 attrs);
11769 if (decl_specs->locations[ds_attribute] == 0)
11770 decl_specs->locations[ds_attribute] = token->location;
11771 continue;
11773 /* Assume we will find a decl-specifier keyword. */
11774 found_decl_spec = true;
11775 /* If the next token is an appropriate keyword, we can simply
11776 add it to the list. */
11777 switch (token->keyword)
11779 /* decl-specifier:
11780 friend
11781 constexpr */
11782 case RID_FRIEND:
11783 if (!at_class_scope_p ())
11785 error_at (token->location, "%<friend%> used outside of class");
11786 cp_lexer_purge_token (parser->lexer);
11788 else
11790 ds = ds_friend;
11791 /* Consume the token. */
11792 cp_lexer_consume_token (parser->lexer);
11794 break;
11796 case RID_CONSTEXPR:
11797 ds = ds_constexpr;
11798 cp_lexer_consume_token (parser->lexer);
11799 break;
11801 /* function-specifier:
11802 inline
11803 virtual
11804 explicit */
11805 case RID_INLINE:
11806 case RID_VIRTUAL:
11807 case RID_EXPLICIT:
11808 cp_parser_function_specifier_opt (parser, decl_specs);
11809 break;
11811 /* decl-specifier:
11812 typedef */
11813 case RID_TYPEDEF:
11814 ds = ds_typedef;
11815 /* Consume the token. */
11816 cp_lexer_consume_token (parser->lexer);
11817 /* A constructor declarator cannot appear in a typedef. */
11818 constructor_possible_p = false;
11819 /* The "typedef" keyword can only occur in a declaration; we
11820 may as well commit at this point. */
11821 cp_parser_commit_to_tentative_parse (parser);
11823 if (decl_specs->storage_class != sc_none)
11824 decl_specs->conflicting_specifiers_p = true;
11825 break;
11827 /* storage-class-specifier:
11828 auto
11829 register
11830 static
11831 extern
11832 mutable
11834 GNU Extension:
11835 thread */
11836 case RID_AUTO:
11837 if (cxx_dialect == cxx98)
11839 /* Consume the token. */
11840 cp_lexer_consume_token (parser->lexer);
11842 /* Complain about `auto' as a storage specifier, if
11843 we're complaining about C++0x compatibility. */
11844 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11845 " changes meaning in C++11; please remove it");
11847 /* Set the storage class anyway. */
11848 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11849 token);
11851 else
11852 /* C++0x auto type-specifier. */
11853 found_decl_spec = false;
11854 break;
11856 case RID_REGISTER:
11857 case RID_STATIC:
11858 case RID_EXTERN:
11859 case RID_MUTABLE:
11860 /* Consume the token. */
11861 cp_lexer_consume_token (parser->lexer);
11862 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11863 token);
11864 break;
11865 case RID_THREAD:
11866 /* Consume the token. */
11867 ds = ds_thread;
11868 cp_lexer_consume_token (parser->lexer);
11869 break;
11871 default:
11872 /* We did not yet find a decl-specifier yet. */
11873 found_decl_spec = false;
11874 break;
11877 if (found_decl_spec
11878 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11879 && token->keyword != RID_CONSTEXPR)
11880 error ("decl-specifier invalid in condition");
11882 if (ds != ds_last)
11883 set_and_check_decl_spec_loc (decl_specs, ds, token);
11885 /* Constructors are a special case. The `S' in `S()' is not a
11886 decl-specifier; it is the beginning of the declarator. */
11887 constructor_p
11888 = (!found_decl_spec
11889 && constructor_possible_p
11890 && (cp_parser_constructor_declarator_p
11891 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11893 /* If we don't have a DECL_SPEC yet, then we must be looking at
11894 a type-specifier. */
11895 if (!found_decl_spec && !constructor_p)
11897 int decl_spec_declares_class_or_enum;
11898 bool is_cv_qualifier;
11899 tree type_spec;
11901 type_spec
11902 = cp_parser_type_specifier (parser, flags,
11903 decl_specs,
11904 /*is_declaration=*/true,
11905 &decl_spec_declares_class_or_enum,
11906 &is_cv_qualifier);
11907 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11909 /* If this type-specifier referenced a user-defined type
11910 (a typedef, class-name, etc.), then we can't allow any
11911 more such type-specifiers henceforth.
11913 [dcl.spec]
11915 The longest sequence of decl-specifiers that could
11916 possibly be a type name is taken as the
11917 decl-specifier-seq of a declaration. The sequence shall
11918 be self-consistent as described below.
11920 [dcl.type]
11922 As a general rule, at most one type-specifier is allowed
11923 in the complete decl-specifier-seq of a declaration. The
11924 only exceptions are the following:
11926 -- const or volatile can be combined with any other
11927 type-specifier.
11929 -- signed or unsigned can be combined with char, long,
11930 short, or int.
11932 -- ..
11934 Example:
11936 typedef char* Pc;
11937 void g (const int Pc);
11939 Here, Pc is *not* part of the decl-specifier seq; it's
11940 the declarator. Therefore, once we see a type-specifier
11941 (other than a cv-qualifier), we forbid any additional
11942 user-defined types. We *do* still allow things like `int
11943 int' to be considered a decl-specifier-seq, and issue the
11944 error message later. */
11945 if (type_spec && !is_cv_qualifier)
11946 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11947 /* A constructor declarator cannot follow a type-specifier. */
11948 if (type_spec)
11950 constructor_possible_p = false;
11951 found_decl_spec = true;
11952 if (!is_cv_qualifier)
11953 decl_specs->any_type_specifiers_p = true;
11957 /* If we still do not have a DECL_SPEC, then there are no more
11958 decl-specifiers. */
11959 if (!found_decl_spec)
11960 break;
11962 decl_specs->any_specifiers_p = true;
11963 /* After we see one decl-specifier, further decl-specifiers are
11964 always optional. */
11965 flags |= CP_PARSER_FLAGS_OPTIONAL;
11968 /* Don't allow a friend specifier with a class definition. */
11969 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11970 && (*declares_class_or_enum & 2))
11971 error_at (decl_specs->locations[ds_friend],
11972 "class definition may not be declared a friend");
11975 /* Parse an (optional) storage-class-specifier.
11977 storage-class-specifier:
11978 auto
11979 register
11980 static
11981 extern
11982 mutable
11984 GNU Extension:
11986 storage-class-specifier:
11987 thread
11989 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11991 static tree
11992 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11994 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11996 case RID_AUTO:
11997 if (cxx_dialect != cxx98)
11998 return NULL_TREE;
11999 /* Fall through for C++98. */
12001 case RID_REGISTER:
12002 case RID_STATIC:
12003 case RID_EXTERN:
12004 case RID_MUTABLE:
12005 case RID_THREAD:
12006 /* Consume the token. */
12007 return cp_lexer_consume_token (parser->lexer)->u.value;
12009 default:
12010 return NULL_TREE;
12014 /* Parse an (optional) function-specifier.
12016 function-specifier:
12017 inline
12018 virtual
12019 explicit
12021 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12022 Updates DECL_SPECS, if it is non-NULL. */
12024 static tree
12025 cp_parser_function_specifier_opt (cp_parser* parser,
12026 cp_decl_specifier_seq *decl_specs)
12028 cp_token *token = cp_lexer_peek_token (parser->lexer);
12029 switch (token->keyword)
12031 case RID_INLINE:
12032 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12033 break;
12035 case RID_VIRTUAL:
12036 /* 14.5.2.3 [temp.mem]
12038 A member function template shall not be virtual. */
12039 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12040 error_at (token->location, "templates may not be %<virtual%>");
12041 else
12042 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12043 break;
12045 case RID_EXPLICIT:
12046 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12047 break;
12049 default:
12050 return NULL_TREE;
12053 /* Consume the token. */
12054 return cp_lexer_consume_token (parser->lexer)->u.value;
12057 /* Parse a linkage-specification.
12059 linkage-specification:
12060 extern string-literal { declaration-seq [opt] }
12061 extern string-literal declaration */
12063 static void
12064 cp_parser_linkage_specification (cp_parser* parser)
12066 tree linkage;
12068 /* Look for the `extern' keyword. */
12069 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12071 /* Look for the string-literal. */
12072 linkage = cp_parser_string_literal (parser, false, false);
12074 /* Transform the literal into an identifier. If the literal is a
12075 wide-character string, or contains embedded NULs, then we can't
12076 handle it as the user wants. */
12077 if (strlen (TREE_STRING_POINTER (linkage))
12078 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12080 cp_parser_error (parser, "invalid linkage-specification");
12081 /* Assume C++ linkage. */
12082 linkage = lang_name_cplusplus;
12084 else
12085 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12087 /* We're now using the new linkage. */
12088 push_lang_context (linkage);
12090 /* If the next token is a `{', then we're using the first
12091 production. */
12092 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12094 cp_ensure_no_omp_declare_simd (parser);
12096 /* Consume the `{' token. */
12097 cp_lexer_consume_token (parser->lexer);
12098 /* Parse the declarations. */
12099 cp_parser_declaration_seq_opt (parser);
12100 /* Look for the closing `}'. */
12101 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12103 /* Otherwise, there's just one declaration. */
12104 else
12106 bool saved_in_unbraced_linkage_specification_p;
12108 saved_in_unbraced_linkage_specification_p
12109 = parser->in_unbraced_linkage_specification_p;
12110 parser->in_unbraced_linkage_specification_p = true;
12111 cp_parser_declaration (parser);
12112 parser->in_unbraced_linkage_specification_p
12113 = saved_in_unbraced_linkage_specification_p;
12116 /* We're done with the linkage-specification. */
12117 pop_lang_context ();
12120 /* Parse a static_assert-declaration.
12122 static_assert-declaration:
12123 static_assert ( constant-expression , string-literal ) ;
12125 If MEMBER_P, this static_assert is a class member. */
12127 static void
12128 cp_parser_static_assert(cp_parser *parser, bool member_p)
12130 tree condition;
12131 tree message;
12132 cp_token *token;
12133 location_t saved_loc;
12134 bool dummy;
12136 /* Peek at the `static_assert' token so we can keep track of exactly
12137 where the static assertion started. */
12138 token = cp_lexer_peek_token (parser->lexer);
12139 saved_loc = token->location;
12141 /* Look for the `static_assert' keyword. */
12142 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12143 RT_STATIC_ASSERT))
12144 return;
12146 /* We know we are in a static assertion; commit to any tentative
12147 parse. */
12148 if (cp_parser_parsing_tentatively (parser))
12149 cp_parser_commit_to_tentative_parse (parser);
12151 /* Parse the `(' starting the static assertion condition. */
12152 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12154 /* Parse the constant-expression. Allow a non-constant expression
12155 here in order to give better diagnostics in finish_static_assert. */
12156 condition =
12157 cp_parser_constant_expression (parser,
12158 /*allow_non_constant_p=*/true,
12159 /*non_constant_p=*/&dummy);
12161 /* Parse the separating `,'. */
12162 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12164 /* Parse the string-literal message. */
12165 message = cp_parser_string_literal (parser,
12166 /*translate=*/false,
12167 /*wide_ok=*/true);
12169 /* A `)' completes the static assertion. */
12170 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12171 cp_parser_skip_to_closing_parenthesis (parser,
12172 /*recovering=*/true,
12173 /*or_comma=*/false,
12174 /*consume_paren=*/true);
12176 /* A semicolon terminates the declaration. */
12177 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12179 /* Complete the static assertion, which may mean either processing
12180 the static assert now or saving it for template instantiation. */
12181 finish_static_assert (condition, message, saved_loc, member_p);
12184 /* Parse the expression in decltype ( expression ). */
12186 static tree
12187 cp_parser_decltype_expr (cp_parser *parser,
12188 bool &id_expression_or_member_access_p)
12190 cp_token *id_expr_start_token;
12191 tree expr;
12193 /* First, try parsing an id-expression. */
12194 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12195 cp_parser_parse_tentatively (parser);
12196 expr = cp_parser_id_expression (parser,
12197 /*template_keyword_p=*/false,
12198 /*check_dependency_p=*/true,
12199 /*template_p=*/NULL,
12200 /*declarator_p=*/false,
12201 /*optional_p=*/false);
12203 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12205 bool non_integral_constant_expression_p = false;
12206 tree id_expression = expr;
12207 cp_id_kind idk;
12208 const char *error_msg;
12210 if (identifier_p (expr))
12211 /* Lookup the name we got back from the id-expression. */
12212 expr = cp_parser_lookup_name_simple (parser, expr,
12213 id_expr_start_token->location);
12215 if (expr
12216 && expr != error_mark_node
12217 && TREE_CODE (expr) != TYPE_DECL
12218 && (TREE_CODE (expr) != BIT_NOT_EXPR
12219 || !TYPE_P (TREE_OPERAND (expr, 0)))
12220 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12222 /* Complete lookup of the id-expression. */
12223 expr = (finish_id_expression
12224 (id_expression, expr, parser->scope, &idk,
12225 /*integral_constant_expression_p=*/false,
12226 /*allow_non_integral_constant_expression_p=*/true,
12227 &non_integral_constant_expression_p,
12228 /*template_p=*/false,
12229 /*done=*/true,
12230 /*address_p=*/false,
12231 /*template_arg_p=*/false,
12232 &error_msg,
12233 id_expr_start_token->location));
12235 if (expr == error_mark_node)
12236 /* We found an id-expression, but it was something that we
12237 should not have found. This is an error, not something
12238 we can recover from, so note that we found an
12239 id-expression and we'll recover as gracefully as
12240 possible. */
12241 id_expression_or_member_access_p = true;
12244 if (expr
12245 && expr != error_mark_node
12246 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12247 /* We have an id-expression. */
12248 id_expression_or_member_access_p = true;
12251 if (!id_expression_or_member_access_p)
12253 /* Abort the id-expression parse. */
12254 cp_parser_abort_tentative_parse (parser);
12256 /* Parsing tentatively, again. */
12257 cp_parser_parse_tentatively (parser);
12259 /* Parse a class member access. */
12260 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12261 /*cast_p=*/false, /*decltype*/true,
12262 /*member_access_only_p=*/true, NULL);
12264 if (expr
12265 && expr != error_mark_node
12266 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12267 /* We have an id-expression. */
12268 id_expression_or_member_access_p = true;
12271 if (id_expression_or_member_access_p)
12272 /* We have parsed the complete id-expression or member access. */
12273 cp_parser_parse_definitely (parser);
12274 else
12276 /* Abort our attempt to parse an id-expression or member access
12277 expression. */
12278 cp_parser_abort_tentative_parse (parser);
12280 /* Parse a full expression. */
12281 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12282 /*decltype_p=*/true);
12285 return expr;
12288 /* Parse a `decltype' type. Returns the type.
12290 simple-type-specifier:
12291 decltype ( expression )
12292 C++14 proposal:
12293 decltype ( auto ) */
12295 static tree
12296 cp_parser_decltype (cp_parser *parser)
12298 tree expr;
12299 bool id_expression_or_member_access_p = false;
12300 const char *saved_message;
12301 bool saved_integral_constant_expression_p;
12302 bool saved_non_integral_constant_expression_p;
12303 bool saved_greater_than_is_operator_p;
12304 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12306 if (start_token->type == CPP_DECLTYPE)
12308 /* Already parsed. */
12309 cp_lexer_consume_token (parser->lexer);
12310 return start_token->u.value;
12313 /* Look for the `decltype' token. */
12314 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12315 return error_mark_node;
12317 /* Parse the opening `('. */
12318 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12319 return error_mark_node;
12321 /* decltype (auto) */
12322 if (cxx_dialect >= cxx14
12323 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12325 cp_lexer_consume_token (parser->lexer);
12326 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12327 return error_mark_node;
12328 expr = make_decltype_auto ();
12329 AUTO_IS_DECLTYPE (expr) = true;
12330 goto rewrite;
12333 /* Types cannot be defined in a `decltype' expression. Save away the
12334 old message. */
12335 saved_message = parser->type_definition_forbidden_message;
12337 /* And create the new one. */
12338 parser->type_definition_forbidden_message
12339 = G_("types may not be defined in %<decltype%> expressions");
12341 /* The restrictions on constant-expressions do not apply inside
12342 decltype expressions. */
12343 saved_integral_constant_expression_p
12344 = parser->integral_constant_expression_p;
12345 saved_non_integral_constant_expression_p
12346 = parser->non_integral_constant_expression_p;
12347 parser->integral_constant_expression_p = false;
12349 /* Within a parenthesized expression, a `>' token is always
12350 the greater-than operator. */
12351 saved_greater_than_is_operator_p
12352 = parser->greater_than_is_operator_p;
12353 parser->greater_than_is_operator_p = true;
12355 /* Do not actually evaluate the expression. */
12356 ++cp_unevaluated_operand;
12358 /* Do not warn about problems with the expression. */
12359 ++c_inhibit_evaluation_warnings;
12361 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12363 /* Go back to evaluating expressions. */
12364 --cp_unevaluated_operand;
12365 --c_inhibit_evaluation_warnings;
12367 /* The `>' token might be the end of a template-id or
12368 template-parameter-list now. */
12369 parser->greater_than_is_operator_p
12370 = saved_greater_than_is_operator_p;
12372 /* Restore the old message and the integral constant expression
12373 flags. */
12374 parser->type_definition_forbidden_message = saved_message;
12375 parser->integral_constant_expression_p
12376 = saved_integral_constant_expression_p;
12377 parser->non_integral_constant_expression_p
12378 = saved_non_integral_constant_expression_p;
12380 /* Parse to the closing `)'. */
12381 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12383 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12384 /*consume_paren=*/true);
12385 return error_mark_node;
12388 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12389 tf_warning_or_error);
12391 rewrite:
12392 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12393 it again. */
12394 start_token->type = CPP_DECLTYPE;
12395 start_token->u.value = expr;
12396 start_token->keyword = RID_MAX;
12397 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12399 return expr;
12402 /* Special member functions [gram.special] */
12404 /* Parse a conversion-function-id.
12406 conversion-function-id:
12407 operator conversion-type-id
12409 Returns an IDENTIFIER_NODE representing the operator. */
12411 static tree
12412 cp_parser_conversion_function_id (cp_parser* parser)
12414 tree type;
12415 tree saved_scope;
12416 tree saved_qualifying_scope;
12417 tree saved_object_scope;
12418 tree pushed_scope = NULL_TREE;
12420 /* Look for the `operator' token. */
12421 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12422 return error_mark_node;
12423 /* When we parse the conversion-type-id, the current scope will be
12424 reset. However, we need that information in able to look up the
12425 conversion function later, so we save it here. */
12426 saved_scope = parser->scope;
12427 saved_qualifying_scope = parser->qualifying_scope;
12428 saved_object_scope = parser->object_scope;
12429 /* We must enter the scope of the class so that the names of
12430 entities declared within the class are available in the
12431 conversion-type-id. For example, consider:
12433 struct S {
12434 typedef int I;
12435 operator I();
12438 S::operator I() { ... }
12440 In order to see that `I' is a type-name in the definition, we
12441 must be in the scope of `S'. */
12442 if (saved_scope)
12443 pushed_scope = push_scope (saved_scope);
12444 /* Parse the conversion-type-id. */
12445 type = cp_parser_conversion_type_id (parser);
12446 /* Leave the scope of the class, if any. */
12447 if (pushed_scope)
12448 pop_scope (pushed_scope);
12449 /* Restore the saved scope. */
12450 parser->scope = saved_scope;
12451 parser->qualifying_scope = saved_qualifying_scope;
12452 parser->object_scope = saved_object_scope;
12453 /* If the TYPE is invalid, indicate failure. */
12454 if (type == error_mark_node)
12455 return error_mark_node;
12456 return mangle_conv_op_name_for_type (type);
12459 /* Parse a conversion-type-id:
12461 conversion-type-id:
12462 type-specifier-seq conversion-declarator [opt]
12464 Returns the TYPE specified. */
12466 static tree
12467 cp_parser_conversion_type_id (cp_parser* parser)
12469 tree attributes;
12470 cp_decl_specifier_seq type_specifiers;
12471 cp_declarator *declarator;
12472 tree type_specified;
12473 const char *saved_message;
12475 /* Parse the attributes. */
12476 attributes = cp_parser_attributes_opt (parser);
12478 saved_message = parser->type_definition_forbidden_message;
12479 parser->type_definition_forbidden_message
12480 = G_("types may not be defined in a conversion-type-id");
12482 /* Parse the type-specifiers. */
12483 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12484 /*is_trailing_return=*/false,
12485 &type_specifiers);
12487 parser->type_definition_forbidden_message = saved_message;
12489 /* If that didn't work, stop. */
12490 if (type_specifiers.type == error_mark_node)
12491 return error_mark_node;
12492 /* Parse the conversion-declarator. */
12493 declarator = cp_parser_conversion_declarator_opt (parser);
12495 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12496 /*initialized=*/0, &attributes);
12497 if (attributes)
12498 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12500 /* Don't give this error when parsing tentatively. This happens to
12501 work because we always parse this definitively once. */
12502 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12503 && type_uses_auto (type_specified))
12505 if (cxx_dialect < cxx14)
12507 error ("invalid use of %<auto%> in conversion operator");
12508 return error_mark_node;
12510 else if (template_parm_scope_p ())
12511 warning (0, "use of %<auto%> in member template "
12512 "conversion operator can never be deduced");
12515 return type_specified;
12518 /* Parse an (optional) conversion-declarator.
12520 conversion-declarator:
12521 ptr-operator conversion-declarator [opt]
12525 static cp_declarator *
12526 cp_parser_conversion_declarator_opt (cp_parser* parser)
12528 enum tree_code code;
12529 tree class_type, std_attributes = NULL_TREE;
12530 cp_cv_quals cv_quals;
12532 /* We don't know if there's a ptr-operator next, or not. */
12533 cp_parser_parse_tentatively (parser);
12534 /* Try the ptr-operator. */
12535 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12536 &std_attributes);
12537 /* If it worked, look for more conversion-declarators. */
12538 if (cp_parser_parse_definitely (parser))
12540 cp_declarator *declarator;
12542 /* Parse another optional declarator. */
12543 declarator = cp_parser_conversion_declarator_opt (parser);
12545 declarator = cp_parser_make_indirect_declarator
12546 (code, class_type, cv_quals, declarator, std_attributes);
12548 return declarator;
12551 return NULL;
12554 /* Parse an (optional) ctor-initializer.
12556 ctor-initializer:
12557 : mem-initializer-list
12559 Returns TRUE iff the ctor-initializer was actually present. */
12561 static bool
12562 cp_parser_ctor_initializer_opt (cp_parser* parser)
12564 /* If the next token is not a `:', then there is no
12565 ctor-initializer. */
12566 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12568 /* Do default initialization of any bases and members. */
12569 if (DECL_CONSTRUCTOR_P (current_function_decl))
12570 finish_mem_initializers (NULL_TREE);
12572 return false;
12575 /* Consume the `:' token. */
12576 cp_lexer_consume_token (parser->lexer);
12577 /* And the mem-initializer-list. */
12578 cp_parser_mem_initializer_list (parser);
12580 return true;
12583 /* Parse a mem-initializer-list.
12585 mem-initializer-list:
12586 mem-initializer ... [opt]
12587 mem-initializer ... [opt] , mem-initializer-list */
12589 static void
12590 cp_parser_mem_initializer_list (cp_parser* parser)
12592 tree mem_initializer_list = NULL_TREE;
12593 tree target_ctor = error_mark_node;
12594 cp_token *token = cp_lexer_peek_token (parser->lexer);
12596 /* Let the semantic analysis code know that we are starting the
12597 mem-initializer-list. */
12598 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12599 error_at (token->location,
12600 "only constructors take member initializers");
12602 /* Loop through the list. */
12603 while (true)
12605 tree mem_initializer;
12607 token = cp_lexer_peek_token (parser->lexer);
12608 /* Parse the mem-initializer. */
12609 mem_initializer = cp_parser_mem_initializer (parser);
12610 /* If the next token is a `...', we're expanding member initializers. */
12611 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12613 /* Consume the `...'. */
12614 cp_lexer_consume_token (parser->lexer);
12616 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12617 can be expanded but members cannot. */
12618 if (mem_initializer != error_mark_node
12619 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12621 error_at (token->location,
12622 "cannot expand initializer for member %<%D%>",
12623 TREE_PURPOSE (mem_initializer));
12624 mem_initializer = error_mark_node;
12627 /* Construct the pack expansion type. */
12628 if (mem_initializer != error_mark_node)
12629 mem_initializer = make_pack_expansion (mem_initializer);
12631 if (target_ctor != error_mark_node
12632 && mem_initializer != error_mark_node)
12634 error ("mem-initializer for %qD follows constructor delegation",
12635 TREE_PURPOSE (mem_initializer));
12636 mem_initializer = error_mark_node;
12638 /* Look for a target constructor. */
12639 if (mem_initializer != error_mark_node
12640 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12641 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12643 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12644 if (mem_initializer_list)
12646 error ("constructor delegation follows mem-initializer for %qD",
12647 TREE_PURPOSE (mem_initializer_list));
12648 mem_initializer = error_mark_node;
12650 target_ctor = mem_initializer;
12652 /* Add it to the list, unless it was erroneous. */
12653 if (mem_initializer != error_mark_node)
12655 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12656 mem_initializer_list = mem_initializer;
12658 /* If the next token is not a `,', we're done. */
12659 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12660 break;
12661 /* Consume the `,' token. */
12662 cp_lexer_consume_token (parser->lexer);
12665 /* Perform semantic analysis. */
12666 if (DECL_CONSTRUCTOR_P (current_function_decl))
12667 finish_mem_initializers (mem_initializer_list);
12670 /* Parse a mem-initializer.
12672 mem-initializer:
12673 mem-initializer-id ( expression-list [opt] )
12674 mem-initializer-id braced-init-list
12676 GNU extension:
12678 mem-initializer:
12679 ( expression-list [opt] )
12681 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12682 class) or FIELD_DECL (for a non-static data member) to initialize;
12683 the TREE_VALUE is the expression-list. An empty initialization
12684 list is represented by void_list_node. */
12686 static tree
12687 cp_parser_mem_initializer (cp_parser* parser)
12689 tree mem_initializer_id;
12690 tree expression_list;
12691 tree member;
12692 cp_token *token = cp_lexer_peek_token (parser->lexer);
12694 /* Find out what is being initialized. */
12695 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12697 permerror (token->location,
12698 "anachronistic old-style base class initializer");
12699 mem_initializer_id = NULL_TREE;
12701 else
12703 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12704 if (mem_initializer_id == error_mark_node)
12705 return mem_initializer_id;
12707 member = expand_member_init (mem_initializer_id);
12708 if (member && !DECL_P (member))
12709 in_base_initializer = 1;
12711 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12713 bool expr_non_constant_p;
12714 cp_lexer_set_source_position (parser->lexer);
12715 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12716 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12717 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12718 expression_list = build_tree_list (NULL_TREE, expression_list);
12720 else
12722 vec<tree, va_gc> *vec;
12723 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12724 /*cast_p=*/false,
12725 /*allow_expansion_p=*/true,
12726 /*non_constant_p=*/NULL);
12727 if (vec == NULL)
12728 return error_mark_node;
12729 expression_list = build_tree_list_vec (vec);
12730 release_tree_vector (vec);
12733 if (expression_list == error_mark_node)
12734 return error_mark_node;
12735 if (!expression_list)
12736 expression_list = void_type_node;
12738 in_base_initializer = 0;
12740 return member ? build_tree_list (member, expression_list) : error_mark_node;
12743 /* Parse a mem-initializer-id.
12745 mem-initializer-id:
12746 :: [opt] nested-name-specifier [opt] class-name
12747 identifier
12749 Returns a TYPE indicating the class to be initializer for the first
12750 production. Returns an IDENTIFIER_NODE indicating the data member
12751 to be initialized for the second production. */
12753 static tree
12754 cp_parser_mem_initializer_id (cp_parser* parser)
12756 bool global_scope_p;
12757 bool nested_name_specifier_p;
12758 bool template_p = false;
12759 tree id;
12761 cp_token *token = cp_lexer_peek_token (parser->lexer);
12763 /* `typename' is not allowed in this context ([temp.res]). */
12764 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12766 error_at (token->location,
12767 "keyword %<typename%> not allowed in this context (a qualified "
12768 "member initializer is implicitly a type)");
12769 cp_lexer_consume_token (parser->lexer);
12771 /* Look for the optional `::' operator. */
12772 global_scope_p
12773 = (cp_parser_global_scope_opt (parser,
12774 /*current_scope_valid_p=*/false)
12775 != NULL_TREE);
12776 /* Look for the optional nested-name-specifier. The simplest way to
12777 implement:
12779 [temp.res]
12781 The keyword `typename' is not permitted in a base-specifier or
12782 mem-initializer; in these contexts a qualified name that
12783 depends on a template-parameter is implicitly assumed to be a
12784 type name.
12786 is to assume that we have seen the `typename' keyword at this
12787 point. */
12788 nested_name_specifier_p
12789 = (cp_parser_nested_name_specifier_opt (parser,
12790 /*typename_keyword_p=*/true,
12791 /*check_dependency_p=*/true,
12792 /*type_p=*/true,
12793 /*is_declaration=*/true)
12794 != NULL_TREE);
12795 if (nested_name_specifier_p)
12796 template_p = cp_parser_optional_template_keyword (parser);
12797 /* If there is a `::' operator or a nested-name-specifier, then we
12798 are definitely looking for a class-name. */
12799 if (global_scope_p || nested_name_specifier_p)
12800 return cp_parser_class_name (parser,
12801 /*typename_keyword_p=*/true,
12802 /*template_keyword_p=*/template_p,
12803 typename_type,
12804 /*check_dependency_p=*/true,
12805 /*class_head_p=*/false,
12806 /*is_declaration=*/true);
12807 /* Otherwise, we could also be looking for an ordinary identifier. */
12808 cp_parser_parse_tentatively (parser);
12809 /* Try a class-name. */
12810 id = cp_parser_class_name (parser,
12811 /*typename_keyword_p=*/true,
12812 /*template_keyword_p=*/false,
12813 none_type,
12814 /*check_dependency_p=*/true,
12815 /*class_head_p=*/false,
12816 /*is_declaration=*/true);
12817 /* If we found one, we're done. */
12818 if (cp_parser_parse_definitely (parser))
12819 return id;
12820 /* Otherwise, look for an ordinary identifier. */
12821 return cp_parser_identifier (parser);
12824 /* Overloading [gram.over] */
12826 /* Parse an operator-function-id.
12828 operator-function-id:
12829 operator operator
12831 Returns an IDENTIFIER_NODE for the operator which is a
12832 human-readable spelling of the identifier, e.g., `operator +'. */
12834 static tree
12835 cp_parser_operator_function_id (cp_parser* parser)
12837 /* Look for the `operator' keyword. */
12838 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12839 return error_mark_node;
12840 /* And then the name of the operator itself. */
12841 return cp_parser_operator (parser);
12844 /* Return an identifier node for a user-defined literal operator.
12845 The suffix identifier is chained to the operator name identifier. */
12847 static tree
12848 cp_literal_operator_id (const char* name)
12850 tree identifier;
12851 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12852 + strlen (name) + 10);
12853 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12854 identifier = get_identifier (buffer);
12856 return identifier;
12859 /* Parse an operator.
12861 operator:
12862 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12863 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12864 || ++ -- , ->* -> () []
12866 GNU Extensions:
12868 operator:
12869 <? >? <?= >?=
12871 Returns an IDENTIFIER_NODE for the operator which is a
12872 human-readable spelling of the identifier, e.g., `operator +'. */
12874 static tree
12875 cp_parser_operator (cp_parser* parser)
12877 tree id = NULL_TREE;
12878 cp_token *token;
12879 bool utf8 = false;
12881 /* Peek at the next token. */
12882 token = cp_lexer_peek_token (parser->lexer);
12883 /* Figure out which operator we have. */
12884 switch (token->type)
12886 case CPP_KEYWORD:
12888 enum tree_code op;
12890 /* The keyword should be either `new' or `delete'. */
12891 if (token->keyword == RID_NEW)
12892 op = NEW_EXPR;
12893 else if (token->keyword == RID_DELETE)
12894 op = DELETE_EXPR;
12895 else
12896 break;
12898 /* Consume the `new' or `delete' token. */
12899 cp_lexer_consume_token (parser->lexer);
12901 /* Peek at the next token. */
12902 token = cp_lexer_peek_token (parser->lexer);
12903 /* If it's a `[' token then this is the array variant of the
12904 operator. */
12905 if (token->type == CPP_OPEN_SQUARE)
12907 /* Consume the `[' token. */
12908 cp_lexer_consume_token (parser->lexer);
12909 /* Look for the `]' token. */
12910 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12911 id = ansi_opname (op == NEW_EXPR
12912 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12914 /* Otherwise, we have the non-array variant. */
12915 else
12916 id = ansi_opname (op);
12918 return id;
12921 case CPP_PLUS:
12922 id = ansi_opname (PLUS_EXPR);
12923 break;
12925 case CPP_MINUS:
12926 id = ansi_opname (MINUS_EXPR);
12927 break;
12929 case CPP_MULT:
12930 id = ansi_opname (MULT_EXPR);
12931 break;
12933 case CPP_DIV:
12934 id = ansi_opname (TRUNC_DIV_EXPR);
12935 break;
12937 case CPP_MOD:
12938 id = ansi_opname (TRUNC_MOD_EXPR);
12939 break;
12941 case CPP_XOR:
12942 id = ansi_opname (BIT_XOR_EXPR);
12943 break;
12945 case CPP_AND:
12946 id = ansi_opname (BIT_AND_EXPR);
12947 break;
12949 case CPP_OR:
12950 id = ansi_opname (BIT_IOR_EXPR);
12951 break;
12953 case CPP_COMPL:
12954 id = ansi_opname (BIT_NOT_EXPR);
12955 break;
12957 case CPP_NOT:
12958 id = ansi_opname (TRUTH_NOT_EXPR);
12959 break;
12961 case CPP_EQ:
12962 id = ansi_assopname (NOP_EXPR);
12963 break;
12965 case CPP_LESS:
12966 id = ansi_opname (LT_EXPR);
12967 break;
12969 case CPP_GREATER:
12970 id = ansi_opname (GT_EXPR);
12971 break;
12973 case CPP_PLUS_EQ:
12974 id = ansi_assopname (PLUS_EXPR);
12975 break;
12977 case CPP_MINUS_EQ:
12978 id = ansi_assopname (MINUS_EXPR);
12979 break;
12981 case CPP_MULT_EQ:
12982 id = ansi_assopname (MULT_EXPR);
12983 break;
12985 case CPP_DIV_EQ:
12986 id = ansi_assopname (TRUNC_DIV_EXPR);
12987 break;
12989 case CPP_MOD_EQ:
12990 id = ansi_assopname (TRUNC_MOD_EXPR);
12991 break;
12993 case CPP_XOR_EQ:
12994 id = ansi_assopname (BIT_XOR_EXPR);
12995 break;
12997 case CPP_AND_EQ:
12998 id = ansi_assopname (BIT_AND_EXPR);
12999 break;
13001 case CPP_OR_EQ:
13002 id = ansi_assopname (BIT_IOR_EXPR);
13003 break;
13005 case CPP_LSHIFT:
13006 id = ansi_opname (LSHIFT_EXPR);
13007 break;
13009 case CPP_RSHIFT:
13010 id = ansi_opname (RSHIFT_EXPR);
13011 break;
13013 case CPP_LSHIFT_EQ:
13014 id = ansi_assopname (LSHIFT_EXPR);
13015 break;
13017 case CPP_RSHIFT_EQ:
13018 id = ansi_assopname (RSHIFT_EXPR);
13019 break;
13021 case CPP_EQ_EQ:
13022 id = ansi_opname (EQ_EXPR);
13023 break;
13025 case CPP_NOT_EQ:
13026 id = ansi_opname (NE_EXPR);
13027 break;
13029 case CPP_LESS_EQ:
13030 id = ansi_opname (LE_EXPR);
13031 break;
13033 case CPP_GREATER_EQ:
13034 id = ansi_opname (GE_EXPR);
13035 break;
13037 case CPP_AND_AND:
13038 id = ansi_opname (TRUTH_ANDIF_EXPR);
13039 break;
13041 case CPP_OR_OR:
13042 id = ansi_opname (TRUTH_ORIF_EXPR);
13043 break;
13045 case CPP_PLUS_PLUS:
13046 id = ansi_opname (POSTINCREMENT_EXPR);
13047 break;
13049 case CPP_MINUS_MINUS:
13050 id = ansi_opname (PREDECREMENT_EXPR);
13051 break;
13053 case CPP_COMMA:
13054 id = ansi_opname (COMPOUND_EXPR);
13055 break;
13057 case CPP_DEREF_STAR:
13058 id = ansi_opname (MEMBER_REF);
13059 break;
13061 case CPP_DEREF:
13062 id = ansi_opname (COMPONENT_REF);
13063 break;
13065 case CPP_OPEN_PAREN:
13066 /* Consume the `('. */
13067 cp_lexer_consume_token (parser->lexer);
13068 /* Look for the matching `)'. */
13069 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13070 return ansi_opname (CALL_EXPR);
13072 case CPP_OPEN_SQUARE:
13073 /* Consume the `['. */
13074 cp_lexer_consume_token (parser->lexer);
13075 /* Look for the matching `]'. */
13076 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13077 return ansi_opname (ARRAY_REF);
13079 case CPP_UTF8STRING:
13080 case CPP_UTF8STRING_USERDEF:
13081 utf8 = true;
13082 case CPP_STRING:
13083 case CPP_WSTRING:
13084 case CPP_STRING16:
13085 case CPP_STRING32:
13086 case CPP_STRING_USERDEF:
13087 case CPP_WSTRING_USERDEF:
13088 case CPP_STRING16_USERDEF:
13089 case CPP_STRING32_USERDEF:
13091 tree str, string_tree;
13092 int sz, len;
13094 if (cxx_dialect == cxx98)
13095 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13097 /* Consume the string. */
13098 str = cp_parser_string_literal (parser, /*translate=*/true,
13099 /*wide_ok=*/true, /*lookup_udlit=*/false);
13100 if (str == error_mark_node)
13101 return error_mark_node;
13102 else if (TREE_CODE (str) == USERDEF_LITERAL)
13104 string_tree = USERDEF_LITERAL_VALUE (str);
13105 id = USERDEF_LITERAL_SUFFIX_ID (str);
13107 else
13109 string_tree = str;
13110 /* Look for the suffix identifier. */
13111 token = cp_lexer_peek_token (parser->lexer);
13112 if (token->type == CPP_NAME)
13113 id = cp_parser_identifier (parser);
13114 else if (token->type == CPP_KEYWORD)
13116 error ("unexpected keyword;"
13117 " remove space between quotes and suffix identifier");
13118 return error_mark_node;
13120 else
13122 error ("expected suffix identifier");
13123 return error_mark_node;
13126 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13127 (TREE_TYPE (TREE_TYPE (string_tree))));
13128 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13129 if (len != 0)
13131 error ("expected empty string after %<operator%> keyword");
13132 return error_mark_node;
13134 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13135 != char_type_node)
13137 error ("invalid encoding prefix in literal operator");
13138 return error_mark_node;
13140 if (id != error_mark_node)
13142 const char *name = IDENTIFIER_POINTER (id);
13143 id = cp_literal_operator_id (name);
13145 return id;
13148 default:
13149 /* Anything else is an error. */
13150 break;
13153 /* If we have selected an identifier, we need to consume the
13154 operator token. */
13155 if (id)
13156 cp_lexer_consume_token (parser->lexer);
13157 /* Otherwise, no valid operator name was present. */
13158 else
13160 cp_parser_error (parser, "expected operator");
13161 id = error_mark_node;
13164 return id;
13167 /* Parse a template-declaration.
13169 template-declaration:
13170 export [opt] template < template-parameter-list > declaration
13172 If MEMBER_P is TRUE, this template-declaration occurs within a
13173 class-specifier.
13175 The grammar rule given by the standard isn't correct. What
13176 is really meant is:
13178 template-declaration:
13179 export [opt] template-parameter-list-seq
13180 decl-specifier-seq [opt] init-declarator [opt] ;
13181 export [opt] template-parameter-list-seq
13182 function-definition
13184 template-parameter-list-seq:
13185 template-parameter-list-seq [opt]
13186 template < template-parameter-list > */
13188 static void
13189 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13191 /* Check for `export'. */
13192 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13194 /* Consume the `export' token. */
13195 cp_lexer_consume_token (parser->lexer);
13196 /* Warn that we do not support `export'. */
13197 warning (0, "keyword %<export%> not implemented, and will be ignored");
13200 cp_parser_template_declaration_after_export (parser, member_p);
13203 /* Parse a template-parameter-list.
13205 template-parameter-list:
13206 template-parameter
13207 template-parameter-list , template-parameter
13209 Returns a TREE_LIST. Each node represents a template parameter.
13210 The nodes are connected via their TREE_CHAINs. */
13212 static tree
13213 cp_parser_template_parameter_list (cp_parser* parser)
13215 tree parameter_list = NULL_TREE;
13217 begin_template_parm_list ();
13219 /* The loop below parses the template parms. We first need to know
13220 the total number of template parms to be able to compute proper
13221 canonical types of each dependent type. So after the loop, when
13222 we know the total number of template parms,
13223 end_template_parm_list computes the proper canonical types and
13224 fixes up the dependent types accordingly. */
13225 while (true)
13227 tree parameter;
13228 bool is_non_type;
13229 bool is_parameter_pack;
13230 location_t parm_loc;
13232 /* Parse the template-parameter. */
13233 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13234 parameter = cp_parser_template_parameter (parser,
13235 &is_non_type,
13236 &is_parameter_pack);
13237 /* Add it to the list. */
13238 if (parameter != error_mark_node)
13239 parameter_list = process_template_parm (parameter_list,
13240 parm_loc,
13241 parameter,
13242 is_non_type,
13243 is_parameter_pack);
13244 else
13246 tree err_parm = build_tree_list (parameter, parameter);
13247 parameter_list = chainon (parameter_list, err_parm);
13250 /* If the next token is not a `,', we're done. */
13251 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13252 break;
13253 /* Otherwise, consume the `,' token. */
13254 cp_lexer_consume_token (parser->lexer);
13257 return end_template_parm_list (parameter_list);
13260 /* Parse a template-parameter.
13262 template-parameter:
13263 type-parameter
13264 parameter-declaration
13266 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13267 the parameter. The TREE_PURPOSE is the default value, if any.
13268 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13269 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13270 set to true iff this parameter is a parameter pack. */
13272 static tree
13273 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13274 bool *is_parameter_pack)
13276 cp_token *token;
13277 cp_parameter_declarator *parameter_declarator;
13278 cp_declarator *id_declarator;
13279 tree parm;
13281 /* Assume it is a type parameter or a template parameter. */
13282 *is_non_type = false;
13283 /* Assume it not a parameter pack. */
13284 *is_parameter_pack = false;
13285 /* Peek at the next token. */
13286 token = cp_lexer_peek_token (parser->lexer);
13287 /* If it is `class' or `template', we have a type-parameter. */
13288 if (token->keyword == RID_TEMPLATE)
13289 return cp_parser_type_parameter (parser, is_parameter_pack);
13290 /* If it is `class' or `typename' we do not know yet whether it is a
13291 type parameter or a non-type parameter. Consider:
13293 template <typename T, typename T::X X> ...
13297 template <class C, class D*> ...
13299 Here, the first parameter is a type parameter, and the second is
13300 a non-type parameter. We can tell by looking at the token after
13301 the identifier -- if it is a `,', `=', or `>' then we have a type
13302 parameter. */
13303 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13305 /* Peek at the token after `class' or `typename'. */
13306 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13307 /* If it's an ellipsis, we have a template type parameter
13308 pack. */
13309 if (token->type == CPP_ELLIPSIS)
13310 return cp_parser_type_parameter (parser, is_parameter_pack);
13311 /* If it's an identifier, skip it. */
13312 if (token->type == CPP_NAME)
13313 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13314 /* Now, see if the token looks like the end of a template
13315 parameter. */
13316 if (token->type == CPP_COMMA
13317 || token->type == CPP_EQ
13318 || token->type == CPP_GREATER)
13319 return cp_parser_type_parameter (parser, is_parameter_pack);
13322 /* Otherwise, it is a non-type parameter.
13324 [temp.param]
13326 When parsing a default template-argument for a non-type
13327 template-parameter, the first non-nested `>' is taken as the end
13328 of the template parameter-list rather than a greater-than
13329 operator. */
13330 *is_non_type = true;
13331 parameter_declarator
13332 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13333 /*parenthesized_p=*/NULL);
13335 if (!parameter_declarator)
13336 return error_mark_node;
13338 /* If the parameter declaration is marked as a parameter pack, set
13339 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13340 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13341 grokdeclarator. */
13342 if (parameter_declarator->declarator
13343 && parameter_declarator->declarator->parameter_pack_p)
13345 *is_parameter_pack = true;
13346 parameter_declarator->declarator->parameter_pack_p = false;
13349 if (parameter_declarator->default_argument)
13351 /* Can happen in some cases of erroneous input (c++/34892). */
13352 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13353 /* Consume the `...' for better error recovery. */
13354 cp_lexer_consume_token (parser->lexer);
13356 /* If the next token is an ellipsis, and we don't already have it
13357 marked as a parameter pack, then we have a parameter pack (that
13358 has no declarator). */
13359 else if (!*is_parameter_pack
13360 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13361 && (declarator_can_be_parameter_pack
13362 (parameter_declarator->declarator)))
13364 /* Consume the `...'. */
13365 cp_lexer_consume_token (parser->lexer);
13366 maybe_warn_variadic_templates ();
13368 *is_parameter_pack = true;
13370 /* We might end up with a pack expansion as the type of the non-type
13371 template parameter, in which case this is a non-type template
13372 parameter pack. */
13373 else if (parameter_declarator->decl_specifiers.type
13374 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13376 *is_parameter_pack = true;
13377 parameter_declarator->decl_specifiers.type =
13378 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13381 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13383 /* Parameter packs cannot have default arguments. However, a
13384 user may try to do so, so we'll parse them and give an
13385 appropriate diagnostic here. */
13387 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13389 /* Find the name of the parameter pack. */
13390 id_declarator = parameter_declarator->declarator;
13391 while (id_declarator && id_declarator->kind != cdk_id)
13392 id_declarator = id_declarator->declarator;
13394 if (id_declarator && id_declarator->kind == cdk_id)
13395 error_at (start_token->location,
13396 "template parameter pack %qD cannot have a default argument",
13397 id_declarator->u.id.unqualified_name);
13398 else
13399 error_at (start_token->location,
13400 "template parameter pack cannot have a default argument");
13402 /* Parse the default argument, but throw away the result. */
13403 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13406 parm = grokdeclarator (parameter_declarator->declarator,
13407 &parameter_declarator->decl_specifiers,
13408 TPARM, /*initialized=*/0,
13409 /*attrlist=*/NULL);
13410 if (parm == error_mark_node)
13411 return error_mark_node;
13413 return build_tree_list (parameter_declarator->default_argument, parm);
13416 /* Parse a type-parameter.
13418 type-parameter:
13419 class identifier [opt]
13420 class identifier [opt] = type-id
13421 typename identifier [opt]
13422 typename identifier [opt] = type-id
13423 template < template-parameter-list > class identifier [opt]
13424 template < template-parameter-list > class identifier [opt]
13425 = id-expression
13427 GNU Extension (variadic templates):
13429 type-parameter:
13430 class ... identifier [opt]
13431 typename ... identifier [opt]
13433 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13434 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13435 the declaration of the parameter.
13437 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13439 static tree
13440 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13442 cp_token *token;
13443 tree parameter;
13445 /* Look for a keyword to tell us what kind of parameter this is. */
13446 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13447 if (!token)
13448 return error_mark_node;
13450 switch (token->keyword)
13452 case RID_CLASS:
13453 case RID_TYPENAME:
13455 tree identifier;
13456 tree default_argument;
13458 /* If the next token is an ellipsis, we have a template
13459 argument pack. */
13460 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13462 /* Consume the `...' token. */
13463 cp_lexer_consume_token (parser->lexer);
13464 maybe_warn_variadic_templates ();
13466 *is_parameter_pack = true;
13469 /* If the next token is an identifier, then it names the
13470 parameter. */
13471 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13472 identifier = cp_parser_identifier (parser);
13473 else
13474 identifier = NULL_TREE;
13476 /* Create the parameter. */
13477 parameter = finish_template_type_parm (class_type_node, identifier);
13479 /* If the next token is an `=', we have a default argument. */
13480 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13482 /* Consume the `=' token. */
13483 cp_lexer_consume_token (parser->lexer);
13484 /* Parse the default-argument. */
13485 push_deferring_access_checks (dk_no_deferred);
13486 default_argument = cp_parser_type_id (parser);
13488 /* Template parameter packs cannot have default
13489 arguments. */
13490 if (*is_parameter_pack)
13492 if (identifier)
13493 error_at (token->location,
13494 "template parameter pack %qD cannot have a "
13495 "default argument", identifier);
13496 else
13497 error_at (token->location,
13498 "template parameter packs cannot have "
13499 "default arguments");
13500 default_argument = NULL_TREE;
13502 pop_deferring_access_checks ();
13504 else
13505 default_argument = NULL_TREE;
13507 /* Create the combined representation of the parameter and the
13508 default argument. */
13509 parameter = build_tree_list (default_argument, parameter);
13511 break;
13513 case RID_TEMPLATE:
13515 tree identifier;
13516 tree default_argument;
13518 /* Look for the `<'. */
13519 cp_parser_require (parser, CPP_LESS, RT_LESS);
13520 /* Parse the template-parameter-list. */
13521 cp_parser_template_parameter_list (parser);
13522 /* Look for the `>'. */
13523 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13524 /* Look for the `class' or 'typename' keywords. */
13525 cp_parser_type_parameter_key (parser);
13526 /* If the next token is an ellipsis, we have a template
13527 argument pack. */
13528 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13530 /* Consume the `...' token. */
13531 cp_lexer_consume_token (parser->lexer);
13532 maybe_warn_variadic_templates ();
13534 *is_parameter_pack = true;
13536 /* If the next token is an `=', then there is a
13537 default-argument. If the next token is a `>', we are at
13538 the end of the parameter-list. If the next token is a `,',
13539 then we are at the end of this parameter. */
13540 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13541 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13542 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13544 identifier = cp_parser_identifier (parser);
13545 /* Treat invalid names as if the parameter were nameless. */
13546 if (identifier == error_mark_node)
13547 identifier = NULL_TREE;
13549 else
13550 identifier = NULL_TREE;
13552 /* Create the template parameter. */
13553 parameter = finish_template_template_parm (class_type_node,
13554 identifier);
13556 /* If the next token is an `=', then there is a
13557 default-argument. */
13558 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13560 bool is_template;
13562 /* Consume the `='. */
13563 cp_lexer_consume_token (parser->lexer);
13564 /* Parse the id-expression. */
13565 push_deferring_access_checks (dk_no_deferred);
13566 /* save token before parsing the id-expression, for error
13567 reporting */
13568 token = cp_lexer_peek_token (parser->lexer);
13569 default_argument
13570 = cp_parser_id_expression (parser,
13571 /*template_keyword_p=*/false,
13572 /*check_dependency_p=*/true,
13573 /*template_p=*/&is_template,
13574 /*declarator_p=*/false,
13575 /*optional_p=*/false);
13576 if (TREE_CODE (default_argument) == TYPE_DECL)
13577 /* If the id-expression was a template-id that refers to
13578 a template-class, we already have the declaration here,
13579 so no further lookup is needed. */
13581 else
13582 /* Look up the name. */
13583 default_argument
13584 = cp_parser_lookup_name (parser, default_argument,
13585 none_type,
13586 /*is_template=*/is_template,
13587 /*is_namespace=*/false,
13588 /*check_dependency=*/true,
13589 /*ambiguous_decls=*/NULL,
13590 token->location);
13591 /* See if the default argument is valid. */
13592 default_argument
13593 = check_template_template_default_arg (default_argument);
13595 /* Template parameter packs cannot have default
13596 arguments. */
13597 if (*is_parameter_pack)
13599 if (identifier)
13600 error_at (token->location,
13601 "template parameter pack %qD cannot "
13602 "have a default argument",
13603 identifier);
13604 else
13605 error_at (token->location, "template parameter packs cannot "
13606 "have default arguments");
13607 default_argument = NULL_TREE;
13609 pop_deferring_access_checks ();
13611 else
13612 default_argument = NULL_TREE;
13614 /* Create the combined representation of the parameter and the
13615 default argument. */
13616 parameter = build_tree_list (default_argument, parameter);
13618 break;
13620 default:
13621 gcc_unreachable ();
13622 break;
13625 return parameter;
13628 /* Parse a template-id.
13630 template-id:
13631 template-name < template-argument-list [opt] >
13633 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13634 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13635 returned. Otherwise, if the template-name names a function, or set
13636 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13637 names a class, returns a TYPE_DECL for the specialization.
13639 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13640 uninstantiated templates. */
13642 static tree
13643 cp_parser_template_id (cp_parser *parser,
13644 bool template_keyword_p,
13645 bool check_dependency_p,
13646 enum tag_types tag_type,
13647 bool is_declaration)
13649 int i;
13650 tree templ;
13651 tree arguments;
13652 tree template_id;
13653 cp_token_position start_of_id = 0;
13654 deferred_access_check *chk;
13655 vec<deferred_access_check, va_gc> *access_check;
13656 cp_token *next_token = NULL, *next_token_2 = NULL;
13657 bool is_identifier;
13659 /* If the next token corresponds to a template-id, there is no need
13660 to reparse it. */
13661 next_token = cp_lexer_peek_token (parser->lexer);
13662 if (next_token->type == CPP_TEMPLATE_ID)
13664 struct tree_check *check_value;
13666 /* Get the stored value. */
13667 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13668 /* Perform any access checks that were deferred. */
13669 access_check = check_value->checks;
13670 if (access_check)
13672 FOR_EACH_VEC_ELT (*access_check, i, chk)
13673 perform_or_defer_access_check (chk->binfo,
13674 chk->decl,
13675 chk->diag_decl,
13676 tf_warning_or_error);
13678 /* Return the stored value. */
13679 return check_value->value;
13682 /* Avoid performing name lookup if there is no possibility of
13683 finding a template-id. */
13684 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13685 || (next_token->type == CPP_NAME
13686 && !cp_parser_nth_token_starts_template_argument_list_p
13687 (parser, 2)))
13689 cp_parser_error (parser, "expected template-id");
13690 return error_mark_node;
13693 /* Remember where the template-id starts. */
13694 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13695 start_of_id = cp_lexer_token_position (parser->lexer, false);
13697 push_deferring_access_checks (dk_deferred);
13699 /* Parse the template-name. */
13700 is_identifier = false;
13701 templ = cp_parser_template_name (parser, template_keyword_p,
13702 check_dependency_p,
13703 is_declaration,
13704 tag_type,
13705 &is_identifier);
13706 if (templ == error_mark_node || is_identifier)
13708 pop_deferring_access_checks ();
13709 return templ;
13712 /* If we find the sequence `[:' after a template-name, it's probably
13713 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13714 parse correctly the argument list. */
13715 next_token = cp_lexer_peek_token (parser->lexer);
13716 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13717 if (next_token->type == CPP_OPEN_SQUARE
13718 && next_token->flags & DIGRAPH
13719 && next_token_2->type == CPP_COLON
13720 && !(next_token_2->flags & PREV_WHITE))
13722 cp_parser_parse_tentatively (parser);
13723 /* Change `:' into `::'. */
13724 next_token_2->type = CPP_SCOPE;
13725 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13726 CPP_LESS. */
13727 cp_lexer_consume_token (parser->lexer);
13729 /* Parse the arguments. */
13730 arguments = cp_parser_enclosed_template_argument_list (parser);
13731 if (!cp_parser_parse_definitely (parser))
13733 /* If we couldn't parse an argument list, then we revert our changes
13734 and return simply an error. Maybe this is not a template-id
13735 after all. */
13736 next_token_2->type = CPP_COLON;
13737 cp_parser_error (parser, "expected %<<%>");
13738 pop_deferring_access_checks ();
13739 return error_mark_node;
13741 /* Otherwise, emit an error about the invalid digraph, but continue
13742 parsing because we got our argument list. */
13743 if (permerror (next_token->location,
13744 "%<<::%> cannot begin a template-argument list"))
13746 static bool hint = false;
13747 inform (next_token->location,
13748 "%<<:%> is an alternate spelling for %<[%>."
13749 " Insert whitespace between %<<%> and %<::%>");
13750 if (!hint && !flag_permissive)
13752 inform (next_token->location, "(if you use %<-fpermissive%> "
13753 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13754 "accept your code)");
13755 hint = true;
13759 else
13761 /* Look for the `<' that starts the template-argument-list. */
13762 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13764 pop_deferring_access_checks ();
13765 return error_mark_node;
13767 /* Parse the arguments. */
13768 arguments = cp_parser_enclosed_template_argument_list (parser);
13771 /* Build a representation of the specialization. */
13772 if (identifier_p (templ))
13773 template_id = build_min_nt_loc (next_token->location,
13774 TEMPLATE_ID_EXPR,
13775 templ, arguments);
13776 else if (DECL_TYPE_TEMPLATE_P (templ)
13777 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13779 bool entering_scope;
13780 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13781 template (rather than some instantiation thereof) only if
13782 is not nested within some other construct. For example, in
13783 "template <typename T> void f(T) { A<T>::", A<T> is just an
13784 instantiation of A. */
13785 entering_scope = (template_parm_scope_p ()
13786 && cp_lexer_next_token_is (parser->lexer,
13787 CPP_SCOPE));
13788 template_id
13789 = finish_template_type (templ, arguments, entering_scope);
13791 else if (variable_template_p (templ))
13793 template_id = lookup_template_variable (templ, arguments);
13795 else
13797 /* If it's not a class-template or a template-template, it should be
13798 a function-template. */
13799 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13800 || TREE_CODE (templ) == OVERLOAD
13801 || BASELINK_P (templ)));
13803 template_id = lookup_template_function (templ, arguments);
13806 /* If parsing tentatively, replace the sequence of tokens that makes
13807 up the template-id with a CPP_TEMPLATE_ID token. That way,
13808 should we re-parse the token stream, we will not have to repeat
13809 the effort required to do the parse, nor will we issue duplicate
13810 error messages about problems during instantiation of the
13811 template. */
13812 if (start_of_id
13813 /* Don't do this if we had a parse error in a declarator; re-parsing
13814 might succeed if a name changes meaning (60361). */
13815 && !(cp_parser_error_occurred (parser)
13816 && cp_parser_parsing_tentatively (parser)
13817 && parser->in_declarator_p))
13819 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13821 /* Reset the contents of the START_OF_ID token. */
13822 token->type = CPP_TEMPLATE_ID;
13823 /* Retrieve any deferred checks. Do not pop this access checks yet
13824 so the memory will not be reclaimed during token replacing below. */
13825 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13826 token->u.tree_check_value->value = template_id;
13827 token->u.tree_check_value->checks = get_deferred_access_checks ();
13828 token->keyword = RID_MAX;
13830 /* Purge all subsequent tokens. */
13831 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13833 /* ??? Can we actually assume that, if template_id ==
13834 error_mark_node, we will have issued a diagnostic to the
13835 user, as opposed to simply marking the tentative parse as
13836 failed? */
13837 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13838 error_at (token->location, "parse error in template argument list");
13841 pop_to_parent_deferring_access_checks ();
13842 return template_id;
13845 /* Parse a template-name.
13847 template-name:
13848 identifier
13850 The standard should actually say:
13852 template-name:
13853 identifier
13854 operator-function-id
13856 A defect report has been filed about this issue.
13858 A conversion-function-id cannot be a template name because they cannot
13859 be part of a template-id. In fact, looking at this code:
13861 a.operator K<int>()
13863 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13864 It is impossible to call a templated conversion-function-id with an
13865 explicit argument list, since the only allowed template parameter is
13866 the type to which it is converting.
13868 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13869 `template' keyword, in a construction like:
13871 T::template f<3>()
13873 In that case `f' is taken to be a template-name, even though there
13874 is no way of knowing for sure.
13876 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13877 name refers to a set of overloaded functions, at least one of which
13878 is a template, or an IDENTIFIER_NODE with the name of the template,
13879 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13880 names are looked up inside uninstantiated templates. */
13882 static tree
13883 cp_parser_template_name (cp_parser* parser,
13884 bool template_keyword_p,
13885 bool check_dependency_p,
13886 bool is_declaration,
13887 enum tag_types tag_type,
13888 bool *is_identifier)
13890 tree identifier;
13891 tree decl;
13892 tree fns;
13893 cp_token *token = cp_lexer_peek_token (parser->lexer);
13895 /* If the next token is `operator', then we have either an
13896 operator-function-id or a conversion-function-id. */
13897 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13899 /* We don't know whether we're looking at an
13900 operator-function-id or a conversion-function-id. */
13901 cp_parser_parse_tentatively (parser);
13902 /* Try an operator-function-id. */
13903 identifier = cp_parser_operator_function_id (parser);
13904 /* If that didn't work, try a conversion-function-id. */
13905 if (!cp_parser_parse_definitely (parser))
13907 cp_parser_error (parser, "expected template-name");
13908 return error_mark_node;
13911 /* Look for the identifier. */
13912 else
13913 identifier = cp_parser_identifier (parser);
13915 /* If we didn't find an identifier, we don't have a template-id. */
13916 if (identifier == error_mark_node)
13917 return error_mark_node;
13919 /* If the name immediately followed the `template' keyword, then it
13920 is a template-name. However, if the next token is not `<', then
13921 we do not treat it as a template-name, since it is not being used
13922 as part of a template-id. This enables us to handle constructs
13923 like:
13925 template <typename T> struct S { S(); };
13926 template <typename T> S<T>::S();
13928 correctly. We would treat `S' as a template -- if it were `S<T>'
13929 -- but we do not if there is no `<'. */
13931 if (processing_template_decl
13932 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13934 /* In a declaration, in a dependent context, we pretend that the
13935 "template" keyword was present in order to improve error
13936 recovery. For example, given:
13938 template <typename T> void f(T::X<int>);
13940 we want to treat "X<int>" as a template-id. */
13941 if (is_declaration
13942 && !template_keyword_p
13943 && parser->scope && TYPE_P (parser->scope)
13944 && check_dependency_p
13945 && dependent_scope_p (parser->scope)
13946 /* Do not do this for dtors (or ctors), since they never
13947 need the template keyword before their name. */
13948 && !constructor_name_p (identifier, parser->scope))
13950 cp_token_position start = 0;
13952 /* Explain what went wrong. */
13953 error_at (token->location, "non-template %qD used as template",
13954 identifier);
13955 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13956 parser->scope, identifier);
13957 /* If parsing tentatively, find the location of the "<" token. */
13958 if (cp_parser_simulate_error (parser))
13959 start = cp_lexer_token_position (parser->lexer, true);
13960 /* Parse the template arguments so that we can issue error
13961 messages about them. */
13962 cp_lexer_consume_token (parser->lexer);
13963 cp_parser_enclosed_template_argument_list (parser);
13964 /* Skip tokens until we find a good place from which to
13965 continue parsing. */
13966 cp_parser_skip_to_closing_parenthesis (parser,
13967 /*recovering=*/true,
13968 /*or_comma=*/true,
13969 /*consume_paren=*/false);
13970 /* If parsing tentatively, permanently remove the
13971 template argument list. That will prevent duplicate
13972 error messages from being issued about the missing
13973 "template" keyword. */
13974 if (start)
13975 cp_lexer_purge_tokens_after (parser->lexer, start);
13976 if (is_identifier)
13977 *is_identifier = true;
13978 return identifier;
13981 /* If the "template" keyword is present, then there is generally
13982 no point in doing name-lookup, so we just return IDENTIFIER.
13983 But, if the qualifying scope is non-dependent then we can
13984 (and must) do name-lookup normally. */
13985 if (template_keyword_p
13986 && (!parser->scope
13987 || (TYPE_P (parser->scope)
13988 && dependent_type_p (parser->scope))))
13989 return identifier;
13992 /* Look up the name. */
13993 decl = cp_parser_lookup_name (parser, identifier,
13994 tag_type,
13995 /*is_template=*/true,
13996 /*is_namespace=*/false,
13997 check_dependency_p,
13998 /*ambiguous_decls=*/NULL,
13999 token->location);
14001 /* If DECL is a template, then the name was a template-name. */
14002 if (TREE_CODE (decl) == TEMPLATE_DECL)
14004 if (TREE_DEPRECATED (decl)
14005 && deprecated_state != DEPRECATED_SUPPRESS)
14006 warn_deprecated_use (decl, NULL_TREE);
14008 else
14010 tree fn = NULL_TREE;
14012 /* The standard does not explicitly indicate whether a name that
14013 names a set of overloaded declarations, some of which are
14014 templates, is a template-name. However, such a name should
14015 be a template-name; otherwise, there is no way to form a
14016 template-id for the overloaded templates. */
14017 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14018 if (TREE_CODE (fns) == OVERLOAD)
14019 for (fn = fns; fn; fn = OVL_NEXT (fn))
14020 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14021 break;
14023 if (!fn)
14025 /* The name does not name a template. */
14026 cp_parser_error (parser, "expected template-name");
14027 return error_mark_node;
14031 /* If DECL is dependent, and refers to a function, then just return
14032 its name; we will look it up again during template instantiation. */
14033 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14035 tree scope = ovl_scope (decl);
14036 if (TYPE_P (scope) && dependent_type_p (scope))
14037 return identifier;
14040 return decl;
14043 /* Parse a template-argument-list.
14045 template-argument-list:
14046 template-argument ... [opt]
14047 template-argument-list , template-argument ... [opt]
14049 Returns a TREE_VEC containing the arguments. */
14051 static tree
14052 cp_parser_template_argument_list (cp_parser* parser)
14054 tree fixed_args[10];
14055 unsigned n_args = 0;
14056 unsigned alloced = 10;
14057 tree *arg_ary = fixed_args;
14058 tree vec;
14059 bool saved_in_template_argument_list_p;
14060 bool saved_ice_p;
14061 bool saved_non_ice_p;
14063 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14064 parser->in_template_argument_list_p = true;
14065 /* Even if the template-id appears in an integral
14066 constant-expression, the contents of the argument list do
14067 not. */
14068 saved_ice_p = parser->integral_constant_expression_p;
14069 parser->integral_constant_expression_p = false;
14070 saved_non_ice_p = parser->non_integral_constant_expression_p;
14071 parser->non_integral_constant_expression_p = false;
14073 /* Parse the arguments. */
14076 tree argument;
14078 if (n_args)
14079 /* Consume the comma. */
14080 cp_lexer_consume_token (parser->lexer);
14082 /* Parse the template-argument. */
14083 argument = cp_parser_template_argument (parser);
14085 /* If the next token is an ellipsis, we're expanding a template
14086 argument pack. */
14087 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14089 if (argument == error_mark_node)
14091 cp_token *token = cp_lexer_peek_token (parser->lexer);
14092 error_at (token->location,
14093 "expected parameter pack before %<...%>");
14095 /* Consume the `...' token. */
14096 cp_lexer_consume_token (parser->lexer);
14098 /* Make the argument into a TYPE_PACK_EXPANSION or
14099 EXPR_PACK_EXPANSION. */
14100 argument = make_pack_expansion (argument);
14103 if (n_args == alloced)
14105 alloced *= 2;
14107 if (arg_ary == fixed_args)
14109 arg_ary = XNEWVEC (tree, alloced);
14110 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14112 else
14113 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14115 arg_ary[n_args++] = argument;
14117 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14119 vec = make_tree_vec (n_args);
14121 while (n_args--)
14122 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14124 if (arg_ary != fixed_args)
14125 free (arg_ary);
14126 parser->non_integral_constant_expression_p = saved_non_ice_p;
14127 parser->integral_constant_expression_p = saved_ice_p;
14128 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14129 #ifdef ENABLE_CHECKING
14130 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14131 #endif
14132 return vec;
14135 /* Parse a template-argument.
14137 template-argument:
14138 assignment-expression
14139 type-id
14140 id-expression
14142 The representation is that of an assignment-expression, type-id, or
14143 id-expression -- except that the qualified id-expression is
14144 evaluated, so that the value returned is either a DECL or an
14145 OVERLOAD.
14147 Although the standard says "assignment-expression", it forbids
14148 throw-expressions or assignments in the template argument.
14149 Therefore, we use "conditional-expression" instead. */
14151 static tree
14152 cp_parser_template_argument (cp_parser* parser)
14154 tree argument;
14155 bool template_p;
14156 bool address_p;
14157 bool maybe_type_id = false;
14158 cp_token *token = NULL, *argument_start_token = NULL;
14159 location_t loc = 0;
14160 cp_id_kind idk;
14162 /* There's really no way to know what we're looking at, so we just
14163 try each alternative in order.
14165 [temp.arg]
14167 In a template-argument, an ambiguity between a type-id and an
14168 expression is resolved to a type-id, regardless of the form of
14169 the corresponding template-parameter.
14171 Therefore, we try a type-id first. */
14172 cp_parser_parse_tentatively (parser);
14173 argument = cp_parser_template_type_arg (parser);
14174 /* If there was no error parsing the type-id but the next token is a
14175 '>>', our behavior depends on which dialect of C++ we're
14176 parsing. In C++98, we probably found a typo for '> >'. But there
14177 are type-id which are also valid expressions. For instance:
14179 struct X { int operator >> (int); };
14180 template <int V> struct Foo {};
14181 Foo<X () >> 5> r;
14183 Here 'X()' is a valid type-id of a function type, but the user just
14184 wanted to write the expression "X() >> 5". Thus, we remember that we
14185 found a valid type-id, but we still try to parse the argument as an
14186 expression to see what happens.
14188 In C++0x, the '>>' will be considered two separate '>'
14189 tokens. */
14190 if (!cp_parser_error_occurred (parser)
14191 && cxx_dialect == cxx98
14192 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14194 maybe_type_id = true;
14195 cp_parser_abort_tentative_parse (parser);
14197 else
14199 /* If the next token isn't a `,' or a `>', then this argument wasn't
14200 really finished. This means that the argument is not a valid
14201 type-id. */
14202 if (!cp_parser_next_token_ends_template_argument_p (parser))
14203 cp_parser_error (parser, "expected template-argument");
14204 /* If that worked, we're done. */
14205 if (cp_parser_parse_definitely (parser))
14206 return argument;
14208 /* We're still not sure what the argument will be. */
14209 cp_parser_parse_tentatively (parser);
14210 /* Try a template. */
14211 argument_start_token = cp_lexer_peek_token (parser->lexer);
14212 argument = cp_parser_id_expression (parser,
14213 /*template_keyword_p=*/false,
14214 /*check_dependency_p=*/true,
14215 &template_p,
14216 /*declarator_p=*/false,
14217 /*optional_p=*/false);
14218 /* If the next token isn't a `,' or a `>', then this argument wasn't
14219 really finished. */
14220 if (!cp_parser_next_token_ends_template_argument_p (parser))
14221 cp_parser_error (parser, "expected template-argument");
14222 if (!cp_parser_error_occurred (parser))
14224 /* Figure out what is being referred to. If the id-expression
14225 was for a class template specialization, then we will have a
14226 TYPE_DECL at this point. There is no need to do name lookup
14227 at this point in that case. */
14228 if (TREE_CODE (argument) != TYPE_DECL)
14229 argument = cp_parser_lookup_name (parser, argument,
14230 none_type,
14231 /*is_template=*/template_p,
14232 /*is_namespace=*/false,
14233 /*check_dependency=*/true,
14234 /*ambiguous_decls=*/NULL,
14235 argument_start_token->location);
14236 if (TREE_CODE (argument) != TEMPLATE_DECL
14237 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14238 cp_parser_error (parser, "expected template-name");
14240 if (cp_parser_parse_definitely (parser))
14242 if (TREE_DEPRECATED (argument))
14243 warn_deprecated_use (argument, NULL_TREE);
14244 return argument;
14246 /* It must be a non-type argument. There permitted cases are given
14247 in [temp.arg.nontype]:
14249 -- an integral constant-expression of integral or enumeration
14250 type; or
14252 -- the name of a non-type template-parameter; or
14254 -- the name of an object or function with external linkage...
14256 -- the address of an object or function with external linkage...
14258 -- a pointer to member... */
14259 /* Look for a non-type template parameter. */
14260 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14262 cp_parser_parse_tentatively (parser);
14263 argument = cp_parser_primary_expression (parser,
14264 /*address_p=*/false,
14265 /*cast_p=*/false,
14266 /*template_arg_p=*/true,
14267 &idk);
14268 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14269 || !cp_parser_next_token_ends_template_argument_p (parser))
14270 cp_parser_simulate_error (parser);
14271 if (cp_parser_parse_definitely (parser))
14272 return argument;
14275 /* If the next token is "&", the argument must be the address of an
14276 object or function with external linkage. */
14277 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14278 if (address_p)
14280 loc = cp_lexer_peek_token (parser->lexer)->location;
14281 cp_lexer_consume_token (parser->lexer);
14283 /* See if we might have an id-expression. */
14284 token = cp_lexer_peek_token (parser->lexer);
14285 if (token->type == CPP_NAME
14286 || token->keyword == RID_OPERATOR
14287 || token->type == CPP_SCOPE
14288 || token->type == CPP_TEMPLATE_ID
14289 || token->type == CPP_NESTED_NAME_SPECIFIER)
14291 cp_parser_parse_tentatively (parser);
14292 argument = cp_parser_primary_expression (parser,
14293 address_p,
14294 /*cast_p=*/false,
14295 /*template_arg_p=*/true,
14296 &idk);
14297 if (cp_parser_error_occurred (parser)
14298 || !cp_parser_next_token_ends_template_argument_p (parser))
14299 cp_parser_abort_tentative_parse (parser);
14300 else
14302 tree probe;
14304 if (INDIRECT_REF_P (argument))
14306 /* Strip the dereference temporarily. */
14307 gcc_assert (REFERENCE_REF_P (argument));
14308 argument = TREE_OPERAND (argument, 0);
14311 /* If we're in a template, we represent a qualified-id referring
14312 to a static data member as a SCOPE_REF even if the scope isn't
14313 dependent so that we can check access control later. */
14314 probe = argument;
14315 if (TREE_CODE (probe) == SCOPE_REF)
14316 probe = TREE_OPERAND (probe, 1);
14317 if (VAR_P (probe))
14319 /* A variable without external linkage might still be a
14320 valid constant-expression, so no error is issued here
14321 if the external-linkage check fails. */
14322 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14323 cp_parser_simulate_error (parser);
14325 else if (is_overloaded_fn (argument))
14326 /* All overloaded functions are allowed; if the external
14327 linkage test does not pass, an error will be issued
14328 later. */
14330 else if (address_p
14331 && (TREE_CODE (argument) == OFFSET_REF
14332 || TREE_CODE (argument) == SCOPE_REF))
14333 /* A pointer-to-member. */
14335 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14337 else
14338 cp_parser_simulate_error (parser);
14340 if (cp_parser_parse_definitely (parser))
14342 if (address_p)
14343 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14344 tf_warning_or_error);
14345 else
14346 argument = convert_from_reference (argument);
14347 return argument;
14351 /* If the argument started with "&", there are no other valid
14352 alternatives at this point. */
14353 if (address_p)
14355 cp_parser_error (parser, "invalid non-type template argument");
14356 return error_mark_node;
14359 /* If the argument wasn't successfully parsed as a type-id followed
14360 by '>>', the argument can only be a constant expression now.
14361 Otherwise, we try parsing the constant-expression tentatively,
14362 because the argument could really be a type-id. */
14363 if (maybe_type_id)
14364 cp_parser_parse_tentatively (parser);
14365 argument = cp_parser_constant_expression (parser);
14367 if (!maybe_type_id)
14368 return argument;
14369 if (!cp_parser_next_token_ends_template_argument_p (parser))
14370 cp_parser_error (parser, "expected template-argument");
14371 if (cp_parser_parse_definitely (parser))
14372 return argument;
14373 /* We did our best to parse the argument as a non type-id, but that
14374 was the only alternative that matched (albeit with a '>' after
14375 it). We can assume it's just a typo from the user, and a
14376 diagnostic will then be issued. */
14377 return cp_parser_template_type_arg (parser);
14380 /* Parse an explicit-instantiation.
14382 explicit-instantiation:
14383 template declaration
14385 Although the standard says `declaration', what it really means is:
14387 explicit-instantiation:
14388 template decl-specifier-seq [opt] declarator [opt] ;
14390 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14391 supposed to be allowed. A defect report has been filed about this
14392 issue.
14394 GNU Extension:
14396 explicit-instantiation:
14397 storage-class-specifier template
14398 decl-specifier-seq [opt] declarator [opt] ;
14399 function-specifier template
14400 decl-specifier-seq [opt] declarator [opt] ; */
14402 static void
14403 cp_parser_explicit_instantiation (cp_parser* parser)
14405 int declares_class_or_enum;
14406 cp_decl_specifier_seq decl_specifiers;
14407 tree extension_specifier = NULL_TREE;
14409 timevar_push (TV_TEMPLATE_INST);
14411 /* Look for an (optional) storage-class-specifier or
14412 function-specifier. */
14413 if (cp_parser_allow_gnu_extensions_p (parser))
14415 extension_specifier
14416 = cp_parser_storage_class_specifier_opt (parser);
14417 if (!extension_specifier)
14418 extension_specifier
14419 = cp_parser_function_specifier_opt (parser,
14420 /*decl_specs=*/NULL);
14423 /* Look for the `template' keyword. */
14424 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14425 /* Let the front end know that we are processing an explicit
14426 instantiation. */
14427 begin_explicit_instantiation ();
14428 /* [temp.explicit] says that we are supposed to ignore access
14429 control while processing explicit instantiation directives. */
14430 push_deferring_access_checks (dk_no_check);
14431 /* Parse a decl-specifier-seq. */
14432 cp_parser_decl_specifier_seq (parser,
14433 CP_PARSER_FLAGS_OPTIONAL,
14434 &decl_specifiers,
14435 &declares_class_or_enum);
14436 /* If there was exactly one decl-specifier, and it declared a class,
14437 and there's no declarator, then we have an explicit type
14438 instantiation. */
14439 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14441 tree type;
14443 type = check_tag_decl (&decl_specifiers,
14444 /*explicit_type_instantiation_p=*/true);
14445 /* Turn access control back on for names used during
14446 template instantiation. */
14447 pop_deferring_access_checks ();
14448 if (type)
14449 do_type_instantiation (type, extension_specifier,
14450 /*complain=*/tf_error);
14452 else
14454 cp_declarator *declarator;
14455 tree decl;
14457 /* Parse the declarator. */
14458 declarator
14459 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14460 /*ctor_dtor_or_conv_p=*/NULL,
14461 /*parenthesized_p=*/NULL,
14462 /*member_p=*/false,
14463 /*friend_p=*/false);
14464 if (declares_class_or_enum & 2)
14465 cp_parser_check_for_definition_in_return_type (declarator,
14466 decl_specifiers.type,
14467 decl_specifiers.locations[ds_type_spec]);
14468 if (declarator != cp_error_declarator)
14470 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14471 permerror (decl_specifiers.locations[ds_inline],
14472 "explicit instantiation shall not use"
14473 " %<inline%> specifier");
14474 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14475 permerror (decl_specifiers.locations[ds_constexpr],
14476 "explicit instantiation shall not use"
14477 " %<constexpr%> specifier");
14479 decl = grokdeclarator (declarator, &decl_specifiers,
14480 NORMAL, 0, &decl_specifiers.attributes);
14481 /* Turn access control back on for names used during
14482 template instantiation. */
14483 pop_deferring_access_checks ();
14484 /* Do the explicit instantiation. */
14485 do_decl_instantiation (decl, extension_specifier);
14487 else
14489 pop_deferring_access_checks ();
14490 /* Skip the body of the explicit instantiation. */
14491 cp_parser_skip_to_end_of_statement (parser);
14494 /* We're done with the instantiation. */
14495 end_explicit_instantiation ();
14497 cp_parser_consume_semicolon_at_end_of_statement (parser);
14499 timevar_pop (TV_TEMPLATE_INST);
14502 /* Parse an explicit-specialization.
14504 explicit-specialization:
14505 template < > declaration
14507 Although the standard says `declaration', what it really means is:
14509 explicit-specialization:
14510 template <> decl-specifier [opt] init-declarator [opt] ;
14511 template <> function-definition
14512 template <> explicit-specialization
14513 template <> template-declaration */
14515 static void
14516 cp_parser_explicit_specialization (cp_parser* parser)
14518 bool need_lang_pop;
14519 cp_token *token = cp_lexer_peek_token (parser->lexer);
14521 /* Look for the `template' keyword. */
14522 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14523 /* Look for the `<'. */
14524 cp_parser_require (parser, CPP_LESS, RT_LESS);
14525 /* Look for the `>'. */
14526 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14527 /* We have processed another parameter list. */
14528 ++parser->num_template_parameter_lists;
14529 /* [temp]
14531 A template ... explicit specialization ... shall not have C
14532 linkage. */
14533 if (current_lang_name == lang_name_c)
14535 error_at (token->location, "template specialization with C linkage");
14536 /* Give it C++ linkage to avoid confusing other parts of the
14537 front end. */
14538 push_lang_context (lang_name_cplusplus);
14539 need_lang_pop = true;
14541 else
14542 need_lang_pop = false;
14543 /* Let the front end know that we are beginning a specialization. */
14544 if (!begin_specialization ())
14546 end_specialization ();
14547 return;
14550 /* If the next keyword is `template', we need to figure out whether
14551 or not we're looking a template-declaration. */
14552 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14554 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14555 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14556 cp_parser_template_declaration_after_export (parser,
14557 /*member_p=*/false);
14558 else
14559 cp_parser_explicit_specialization (parser);
14561 else
14562 /* Parse the dependent declaration. */
14563 cp_parser_single_declaration (parser,
14564 /*checks=*/NULL,
14565 /*member_p=*/false,
14566 /*explicit_specialization_p=*/true,
14567 /*friend_p=*/NULL);
14568 /* We're done with the specialization. */
14569 end_specialization ();
14570 /* For the erroneous case of a template with C linkage, we pushed an
14571 implicit C++ linkage scope; exit that scope now. */
14572 if (need_lang_pop)
14573 pop_lang_context ();
14574 /* We're done with this parameter list. */
14575 --parser->num_template_parameter_lists;
14578 /* Parse a type-specifier.
14580 type-specifier:
14581 simple-type-specifier
14582 class-specifier
14583 enum-specifier
14584 elaborated-type-specifier
14585 cv-qualifier
14587 GNU Extension:
14589 type-specifier:
14590 __complex__
14592 Returns a representation of the type-specifier. For a
14593 class-specifier, enum-specifier, or elaborated-type-specifier, a
14594 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14596 The parser flags FLAGS is used to control type-specifier parsing.
14598 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14599 in a decl-specifier-seq.
14601 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14602 class-specifier, enum-specifier, or elaborated-type-specifier, then
14603 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14604 if a type is declared; 2 if it is defined. Otherwise, it is set to
14605 zero.
14607 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14608 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14609 is set to FALSE. */
14611 static tree
14612 cp_parser_type_specifier (cp_parser* parser,
14613 cp_parser_flags flags,
14614 cp_decl_specifier_seq *decl_specs,
14615 bool is_declaration,
14616 int* declares_class_or_enum,
14617 bool* is_cv_qualifier)
14619 tree type_spec = NULL_TREE;
14620 cp_token *token;
14621 enum rid keyword;
14622 cp_decl_spec ds = ds_last;
14624 /* Assume this type-specifier does not declare a new type. */
14625 if (declares_class_or_enum)
14626 *declares_class_or_enum = 0;
14627 /* And that it does not specify a cv-qualifier. */
14628 if (is_cv_qualifier)
14629 *is_cv_qualifier = false;
14630 /* Peek at the next token. */
14631 token = cp_lexer_peek_token (parser->lexer);
14633 /* If we're looking at a keyword, we can use that to guide the
14634 production we choose. */
14635 keyword = token->keyword;
14636 switch (keyword)
14638 case RID_ENUM:
14639 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14640 goto elaborated_type_specifier;
14642 /* Look for the enum-specifier. */
14643 type_spec = cp_parser_enum_specifier (parser);
14644 /* If that worked, we're done. */
14645 if (type_spec)
14647 if (declares_class_or_enum)
14648 *declares_class_or_enum = 2;
14649 if (decl_specs)
14650 cp_parser_set_decl_spec_type (decl_specs,
14651 type_spec,
14652 token,
14653 /*type_definition_p=*/true);
14654 return type_spec;
14656 else
14657 goto elaborated_type_specifier;
14659 /* Any of these indicate either a class-specifier, or an
14660 elaborated-type-specifier. */
14661 case RID_CLASS:
14662 case RID_STRUCT:
14663 case RID_UNION:
14664 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14665 goto elaborated_type_specifier;
14667 /* Parse tentatively so that we can back up if we don't find a
14668 class-specifier. */
14669 cp_parser_parse_tentatively (parser);
14670 /* Look for the class-specifier. */
14671 type_spec = cp_parser_class_specifier (parser);
14672 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14673 /* If that worked, we're done. */
14674 if (cp_parser_parse_definitely (parser))
14676 if (declares_class_or_enum)
14677 *declares_class_or_enum = 2;
14678 if (decl_specs)
14679 cp_parser_set_decl_spec_type (decl_specs,
14680 type_spec,
14681 token,
14682 /*type_definition_p=*/true);
14683 return type_spec;
14686 /* Fall through. */
14687 elaborated_type_specifier:
14688 /* We're declaring (not defining) a class or enum. */
14689 if (declares_class_or_enum)
14690 *declares_class_or_enum = 1;
14692 /* Fall through. */
14693 case RID_TYPENAME:
14694 /* Look for an elaborated-type-specifier. */
14695 type_spec
14696 = (cp_parser_elaborated_type_specifier
14697 (parser,
14698 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14699 is_declaration));
14700 if (decl_specs)
14701 cp_parser_set_decl_spec_type (decl_specs,
14702 type_spec,
14703 token,
14704 /*type_definition_p=*/false);
14705 return type_spec;
14707 case RID_CONST:
14708 ds = ds_const;
14709 if (is_cv_qualifier)
14710 *is_cv_qualifier = true;
14711 break;
14713 case RID_VOLATILE:
14714 ds = ds_volatile;
14715 if (is_cv_qualifier)
14716 *is_cv_qualifier = true;
14717 break;
14719 case RID_RESTRICT:
14720 ds = ds_restrict;
14721 if (is_cv_qualifier)
14722 *is_cv_qualifier = true;
14723 break;
14725 case RID_COMPLEX:
14726 /* The `__complex__' keyword is a GNU extension. */
14727 ds = ds_complex;
14728 break;
14730 default:
14731 break;
14734 /* Handle simple keywords. */
14735 if (ds != ds_last)
14737 if (decl_specs)
14739 set_and_check_decl_spec_loc (decl_specs, ds, token);
14740 decl_specs->any_specifiers_p = true;
14742 return cp_lexer_consume_token (parser->lexer)->u.value;
14745 /* If we do not already have a type-specifier, assume we are looking
14746 at a simple-type-specifier. */
14747 type_spec = cp_parser_simple_type_specifier (parser,
14748 decl_specs,
14749 flags);
14751 /* If we didn't find a type-specifier, and a type-specifier was not
14752 optional in this context, issue an error message. */
14753 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14755 cp_parser_error (parser, "expected type specifier");
14756 return error_mark_node;
14759 return type_spec;
14762 /* Parse a simple-type-specifier.
14764 simple-type-specifier:
14765 :: [opt] nested-name-specifier [opt] type-name
14766 :: [opt] nested-name-specifier template template-id
14767 char
14768 wchar_t
14769 bool
14770 short
14772 long
14773 signed
14774 unsigned
14775 float
14776 double
14777 void
14779 C++0x Extension:
14781 simple-type-specifier:
14782 auto
14783 decltype ( expression )
14784 char16_t
14785 char32_t
14786 __underlying_type ( type-id )
14788 GNU Extension:
14790 simple-type-specifier:
14791 __int128
14792 __typeof__ unary-expression
14793 __typeof__ ( type-id )
14794 __typeof__ ( type-id ) { initializer-list , [opt] }
14796 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14797 appropriately updated. */
14799 static tree
14800 cp_parser_simple_type_specifier (cp_parser* parser,
14801 cp_decl_specifier_seq *decl_specs,
14802 cp_parser_flags flags)
14804 tree type = NULL_TREE;
14805 cp_token *token;
14806 int idx;
14808 /* Peek at the next token. */
14809 token = cp_lexer_peek_token (parser->lexer);
14811 /* If we're looking at a keyword, things are easy. */
14812 switch (token->keyword)
14814 case RID_CHAR:
14815 if (decl_specs)
14816 decl_specs->explicit_char_p = true;
14817 type = char_type_node;
14818 break;
14819 case RID_CHAR16:
14820 type = char16_type_node;
14821 break;
14822 case RID_CHAR32:
14823 type = char32_type_node;
14824 break;
14825 case RID_WCHAR:
14826 type = wchar_type_node;
14827 break;
14828 case RID_BOOL:
14829 type = boolean_type_node;
14830 break;
14831 case RID_SHORT:
14832 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14833 type = short_integer_type_node;
14834 break;
14835 case RID_INT:
14836 if (decl_specs)
14837 decl_specs->explicit_int_p = true;
14838 type = integer_type_node;
14839 break;
14840 case RID_INT_N_0:
14841 case RID_INT_N_1:
14842 case RID_INT_N_2:
14843 case RID_INT_N_3:
14844 idx = token->keyword - RID_INT_N_0;
14845 if (! int_n_enabled_p [idx])
14846 break;
14847 if (decl_specs)
14849 decl_specs->explicit_intN_p = true;
14850 decl_specs->int_n_idx = idx;
14852 type = int_n_trees [idx].signed_type;
14853 break;
14854 case RID_LONG:
14855 if (decl_specs)
14856 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14857 type = long_integer_type_node;
14858 break;
14859 case RID_SIGNED:
14860 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14861 type = integer_type_node;
14862 break;
14863 case RID_UNSIGNED:
14864 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14865 type = unsigned_type_node;
14866 break;
14867 case RID_FLOAT:
14868 type = float_type_node;
14869 break;
14870 case RID_DOUBLE:
14871 type = double_type_node;
14872 break;
14873 case RID_VOID:
14874 type = void_type_node;
14875 break;
14877 case RID_AUTO:
14878 maybe_warn_cpp0x (CPP0X_AUTO);
14879 if (parser->auto_is_implicit_function_template_parm_p)
14881 if (cxx_dialect >= cxx14)
14882 type = synthesize_implicit_template_parm (parser);
14883 else
14884 type = error_mark_node;
14886 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14888 if (cxx_dialect < cxx14)
14889 error_at (token->location,
14890 "use of %<auto%> in lambda parameter declaration "
14891 "only available with "
14892 "-std=c++14 or -std=gnu++14");
14894 else if (cxx_dialect < cxx14)
14895 error_at (token->location,
14896 "use of %<auto%> in parameter declaration "
14897 "only available with "
14898 "-std=c++14 or -std=gnu++14");
14899 else
14900 pedwarn (token->location, OPT_Wpedantic,
14901 "ISO C++ forbids use of %<auto%> in parameter "
14902 "declaration");
14904 else
14905 type = make_auto ();
14906 break;
14908 case RID_DECLTYPE:
14909 /* Since DR 743, decltype can either be a simple-type-specifier by
14910 itself or begin a nested-name-specifier. Parsing it will replace
14911 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14912 handling below decide what to do. */
14913 cp_parser_decltype (parser);
14914 cp_lexer_set_token_position (parser->lexer, token);
14915 break;
14917 case RID_TYPEOF:
14918 /* Consume the `typeof' token. */
14919 cp_lexer_consume_token (parser->lexer);
14920 /* Parse the operand to `typeof'. */
14921 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14922 /* If it is not already a TYPE, take its type. */
14923 if (!TYPE_P (type))
14924 type = finish_typeof (type);
14926 if (decl_specs)
14927 cp_parser_set_decl_spec_type (decl_specs, type,
14928 token,
14929 /*type_definition_p=*/false);
14931 return type;
14933 case RID_UNDERLYING_TYPE:
14934 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14935 if (decl_specs)
14936 cp_parser_set_decl_spec_type (decl_specs, type,
14937 token,
14938 /*type_definition_p=*/false);
14940 return type;
14942 case RID_BASES:
14943 case RID_DIRECT_BASES:
14944 type = cp_parser_trait_expr (parser, token->keyword);
14945 if (decl_specs)
14946 cp_parser_set_decl_spec_type (decl_specs, type,
14947 token,
14948 /*type_definition_p=*/false);
14949 return type;
14950 default:
14951 break;
14954 /* If token is an already-parsed decltype not followed by ::,
14955 it's a simple-type-specifier. */
14956 if (token->type == CPP_DECLTYPE
14957 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14959 type = token->u.value;
14960 if (decl_specs)
14962 cp_parser_set_decl_spec_type (decl_specs, type,
14963 token,
14964 /*type_definition_p=*/false);
14965 /* Remember that we are handling a decltype in order to
14966 implement the resolution of DR 1510 when the argument
14967 isn't instantiation dependent. */
14968 decl_specs->decltype_p = true;
14970 cp_lexer_consume_token (parser->lexer);
14971 return type;
14974 /* If the type-specifier was for a built-in type, we're done. */
14975 if (type)
14977 /* Record the type. */
14978 if (decl_specs
14979 && (token->keyword != RID_SIGNED
14980 && token->keyword != RID_UNSIGNED
14981 && token->keyword != RID_SHORT
14982 && token->keyword != RID_LONG))
14983 cp_parser_set_decl_spec_type (decl_specs,
14984 type,
14985 token,
14986 /*type_definition_p=*/false);
14987 if (decl_specs)
14988 decl_specs->any_specifiers_p = true;
14990 /* Consume the token. */
14991 cp_lexer_consume_token (parser->lexer);
14993 if (type == error_mark_node)
14994 return error_mark_node;
14996 /* There is no valid C++ program where a non-template type is
14997 followed by a "<". That usually indicates that the user thought
14998 that the type was a template. */
14999 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15000 token->location);
15002 return TYPE_NAME (type);
15005 /* The type-specifier must be a user-defined type. */
15006 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15008 bool qualified_p;
15009 bool global_p;
15011 /* Don't gobble tokens or issue error messages if this is an
15012 optional type-specifier. */
15013 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15014 cp_parser_parse_tentatively (parser);
15016 /* Look for the optional `::' operator. */
15017 global_p
15018 = (cp_parser_global_scope_opt (parser,
15019 /*current_scope_valid_p=*/false)
15020 != NULL_TREE);
15021 /* Look for the nested-name specifier. */
15022 qualified_p
15023 = (cp_parser_nested_name_specifier_opt (parser,
15024 /*typename_keyword_p=*/false,
15025 /*check_dependency_p=*/true,
15026 /*type_p=*/false,
15027 /*is_declaration=*/false)
15028 != NULL_TREE);
15029 token = cp_lexer_peek_token (parser->lexer);
15030 /* If we have seen a nested-name-specifier, and the next token
15031 is `template', then we are using the template-id production. */
15032 if (parser->scope
15033 && cp_parser_optional_template_keyword (parser))
15035 /* Look for the template-id. */
15036 type = cp_parser_template_id (parser,
15037 /*template_keyword_p=*/true,
15038 /*check_dependency_p=*/true,
15039 none_type,
15040 /*is_declaration=*/false);
15041 /* If the template-id did not name a type, we are out of
15042 luck. */
15043 if (TREE_CODE (type) != TYPE_DECL)
15045 cp_parser_error (parser, "expected template-id for type");
15046 type = NULL_TREE;
15049 /* Otherwise, look for a type-name. */
15050 else
15051 type = cp_parser_type_name (parser);
15052 /* Keep track of all name-lookups performed in class scopes. */
15053 if (type
15054 && !global_p
15055 && !qualified_p
15056 && TREE_CODE (type) == TYPE_DECL
15057 && identifier_p (DECL_NAME (type)))
15058 maybe_note_name_used_in_class (DECL_NAME (type), type);
15059 /* If it didn't work out, we don't have a TYPE. */
15060 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15061 && !cp_parser_parse_definitely (parser))
15062 type = NULL_TREE;
15063 if (type && decl_specs)
15064 cp_parser_set_decl_spec_type (decl_specs, type,
15065 token,
15066 /*type_definition_p=*/false);
15069 /* If we didn't get a type-name, issue an error message. */
15070 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15072 cp_parser_error (parser, "expected type-name");
15073 return error_mark_node;
15076 if (type && type != error_mark_node)
15078 /* See if TYPE is an Objective-C type, and if so, parse and
15079 accept any protocol references following it. Do this before
15080 the cp_parser_check_for_invalid_template_id() call, because
15081 Objective-C types can be followed by '<...>' which would
15082 enclose protocol names rather than template arguments, and so
15083 everything is fine. */
15084 if (c_dialect_objc () && !parser->scope
15085 && (objc_is_id (type) || objc_is_class_name (type)))
15087 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15088 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15090 /* Clobber the "unqualified" type previously entered into
15091 DECL_SPECS with the new, improved protocol-qualified version. */
15092 if (decl_specs)
15093 decl_specs->type = qual_type;
15095 return qual_type;
15098 /* There is no valid C++ program where a non-template type is
15099 followed by a "<". That usually indicates that the user
15100 thought that the type was a template. */
15101 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15102 none_type,
15103 token->location);
15106 return type;
15109 /* Parse a type-name.
15111 type-name:
15112 class-name
15113 enum-name
15114 typedef-name
15115 simple-template-id [in c++0x]
15117 enum-name:
15118 identifier
15120 typedef-name:
15121 identifier
15123 Returns a TYPE_DECL for the type. */
15125 static tree
15126 cp_parser_type_name (cp_parser* parser)
15128 tree type_decl;
15130 /* We can't know yet whether it is a class-name or not. */
15131 cp_parser_parse_tentatively (parser);
15132 /* Try a class-name. */
15133 type_decl = cp_parser_class_name (parser,
15134 /*typename_keyword_p=*/false,
15135 /*template_keyword_p=*/false,
15136 none_type,
15137 /*check_dependency_p=*/true,
15138 /*class_head_p=*/false,
15139 /*is_declaration=*/false);
15140 /* If it's not a class-name, keep looking. */
15141 if (!cp_parser_parse_definitely (parser))
15143 if (cxx_dialect < cxx11)
15144 /* It must be a typedef-name or an enum-name. */
15145 return cp_parser_nonclass_name (parser);
15147 cp_parser_parse_tentatively (parser);
15148 /* It is either a simple-template-id representing an
15149 instantiation of an alias template... */
15150 type_decl = cp_parser_template_id (parser,
15151 /*template_keyword_p=*/false,
15152 /*check_dependency_p=*/true,
15153 none_type,
15154 /*is_declaration=*/false);
15155 /* Note that this must be an instantiation of an alias template
15156 because [temp.names]/6 says:
15158 A template-id that names an alias template specialization
15159 is a type-name.
15161 Whereas [temp.names]/7 says:
15163 A simple-template-id that names a class template
15164 specialization is a class-name. */
15165 if (type_decl != NULL_TREE
15166 && TREE_CODE (type_decl) == TYPE_DECL
15167 && TYPE_DECL_ALIAS_P (type_decl))
15168 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15169 else
15170 cp_parser_simulate_error (parser);
15172 if (!cp_parser_parse_definitely (parser))
15173 /* ... Or a typedef-name or an enum-name. */
15174 return cp_parser_nonclass_name (parser);
15177 return type_decl;
15180 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15182 enum-name:
15183 identifier
15185 typedef-name:
15186 identifier
15188 Returns a TYPE_DECL for the type. */
15190 static tree
15191 cp_parser_nonclass_name (cp_parser* parser)
15193 tree type_decl;
15194 tree identifier;
15196 cp_token *token = cp_lexer_peek_token (parser->lexer);
15197 identifier = cp_parser_identifier (parser);
15198 if (identifier == error_mark_node)
15199 return error_mark_node;
15201 /* Look up the type-name. */
15202 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15204 type_decl = strip_using_decl (type_decl);
15206 if (TREE_CODE (type_decl) != TYPE_DECL
15207 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15209 /* See if this is an Objective-C type. */
15210 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15211 tree type = objc_get_protocol_qualified_type (identifier, protos);
15212 if (type)
15213 type_decl = TYPE_NAME (type);
15216 /* Issue an error if we did not find a type-name. */
15217 if (TREE_CODE (type_decl) != TYPE_DECL
15218 /* In Objective-C, we have the complication that class names are
15219 normally type names and start declarations (eg, the
15220 "NSObject" in "NSObject *object;"), but can be used in an
15221 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15222 is an expression. So, a classname followed by a dot is not a
15223 valid type-name. */
15224 || (objc_is_class_name (TREE_TYPE (type_decl))
15225 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15227 if (!cp_parser_simulate_error (parser))
15228 cp_parser_name_lookup_error (parser, identifier, type_decl,
15229 NLE_TYPE, token->location);
15230 return error_mark_node;
15232 /* Remember that the name was used in the definition of the
15233 current class so that we can check later to see if the
15234 meaning would have been different after the class was
15235 entirely defined. */
15236 else if (type_decl != error_mark_node
15237 && !parser->scope)
15238 maybe_note_name_used_in_class (identifier, type_decl);
15240 return type_decl;
15243 /* Parse an elaborated-type-specifier. Note that the grammar given
15244 here incorporates the resolution to DR68.
15246 elaborated-type-specifier:
15247 class-key :: [opt] nested-name-specifier [opt] identifier
15248 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15249 enum-key :: [opt] nested-name-specifier [opt] identifier
15250 typename :: [opt] nested-name-specifier identifier
15251 typename :: [opt] nested-name-specifier template [opt]
15252 template-id
15254 GNU extension:
15256 elaborated-type-specifier:
15257 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15258 class-key attributes :: [opt] nested-name-specifier [opt]
15259 template [opt] template-id
15260 enum attributes :: [opt] nested-name-specifier [opt] identifier
15262 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15263 declared `friend'. If IS_DECLARATION is TRUE, then this
15264 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15265 something is being declared.
15267 Returns the TYPE specified. */
15269 static tree
15270 cp_parser_elaborated_type_specifier (cp_parser* parser,
15271 bool is_friend,
15272 bool is_declaration)
15274 enum tag_types tag_type;
15275 tree identifier;
15276 tree type = NULL_TREE;
15277 tree attributes = NULL_TREE;
15278 tree globalscope;
15279 cp_token *token = NULL;
15281 /* See if we're looking at the `enum' keyword. */
15282 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15284 /* Consume the `enum' token. */
15285 cp_lexer_consume_token (parser->lexer);
15286 /* Remember that it's an enumeration type. */
15287 tag_type = enum_type;
15288 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15289 enums) is used here. */
15290 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15291 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15293 pedwarn (input_location, 0, "elaborated-type-specifier "
15294 "for a scoped enum must not use the %<%D%> keyword",
15295 cp_lexer_peek_token (parser->lexer)->u.value);
15296 /* Consume the `struct' or `class' and parse it anyway. */
15297 cp_lexer_consume_token (parser->lexer);
15299 /* Parse the attributes. */
15300 attributes = cp_parser_attributes_opt (parser);
15302 /* Or, it might be `typename'. */
15303 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15304 RID_TYPENAME))
15306 /* Consume the `typename' token. */
15307 cp_lexer_consume_token (parser->lexer);
15308 /* Remember that it's a `typename' type. */
15309 tag_type = typename_type;
15311 /* Otherwise it must be a class-key. */
15312 else
15314 tag_type = cp_parser_class_key (parser);
15315 if (tag_type == none_type)
15316 return error_mark_node;
15317 /* Parse the attributes. */
15318 attributes = cp_parser_attributes_opt (parser);
15321 /* Look for the `::' operator. */
15322 globalscope = cp_parser_global_scope_opt (parser,
15323 /*current_scope_valid_p=*/false);
15324 /* Look for the nested-name-specifier. */
15325 if (tag_type == typename_type && !globalscope)
15327 if (!cp_parser_nested_name_specifier (parser,
15328 /*typename_keyword_p=*/true,
15329 /*check_dependency_p=*/true,
15330 /*type_p=*/true,
15331 is_declaration))
15332 return error_mark_node;
15334 else
15335 /* Even though `typename' is not present, the proposed resolution
15336 to Core Issue 180 says that in `class A<T>::B', `B' should be
15337 considered a type-name, even if `A<T>' is dependent. */
15338 cp_parser_nested_name_specifier_opt (parser,
15339 /*typename_keyword_p=*/true,
15340 /*check_dependency_p=*/true,
15341 /*type_p=*/true,
15342 is_declaration);
15343 /* For everything but enumeration types, consider a template-id.
15344 For an enumeration type, consider only a plain identifier. */
15345 if (tag_type != enum_type)
15347 bool template_p = false;
15348 tree decl;
15350 /* Allow the `template' keyword. */
15351 template_p = cp_parser_optional_template_keyword (parser);
15352 /* If we didn't see `template', we don't know if there's a
15353 template-id or not. */
15354 if (!template_p)
15355 cp_parser_parse_tentatively (parser);
15356 /* Parse the template-id. */
15357 token = cp_lexer_peek_token (parser->lexer);
15358 decl = cp_parser_template_id (parser, template_p,
15359 /*check_dependency_p=*/true,
15360 tag_type,
15361 is_declaration);
15362 /* If we didn't find a template-id, look for an ordinary
15363 identifier. */
15364 if (!template_p && !cp_parser_parse_definitely (parser))
15366 /* We can get here when cp_parser_template_id, called by
15367 cp_parser_class_name with tag_type == none_type, succeeds
15368 and caches a BASELINK. Then, when called again here,
15369 instead of failing and returning an error_mark_node
15370 returns it (see template/typename17.C in C++11).
15371 ??? Could we diagnose this earlier? */
15372 else if (tag_type == typename_type && BASELINK_P (decl))
15374 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15375 type = error_mark_node;
15377 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15378 in effect, then we must assume that, upon instantiation, the
15379 template will correspond to a class. */
15380 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15381 && tag_type == typename_type)
15382 type = make_typename_type (parser->scope, decl,
15383 typename_type,
15384 /*complain=*/tf_error);
15385 /* If the `typename' keyword is in effect and DECL is not a type
15386 decl, then type is non existent. */
15387 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15389 else if (TREE_CODE (decl) == TYPE_DECL)
15390 type = check_elaborated_type_specifier (tag_type, decl,
15391 /*allow_template_p=*/true);
15392 else if (decl == error_mark_node)
15393 type = error_mark_node;
15396 if (!type)
15398 token = cp_lexer_peek_token (parser->lexer);
15399 identifier = cp_parser_identifier (parser);
15401 if (identifier == error_mark_node)
15403 parser->scope = NULL_TREE;
15404 return error_mark_node;
15407 /* For a `typename', we needn't call xref_tag. */
15408 if (tag_type == typename_type
15409 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15410 return cp_parser_make_typename_type (parser, identifier,
15411 token->location);
15413 /* Template parameter lists apply only if we are not within a
15414 function parameter list. */
15415 bool template_parm_lists_apply
15416 = parser->num_template_parameter_lists;
15417 if (template_parm_lists_apply)
15418 for (cp_binding_level *s = current_binding_level;
15419 s && s->kind != sk_template_parms;
15420 s = s->level_chain)
15421 if (s->kind == sk_function_parms)
15422 template_parm_lists_apply = false;
15424 /* Look up a qualified name in the usual way. */
15425 if (parser->scope)
15427 tree decl;
15428 tree ambiguous_decls;
15430 decl = cp_parser_lookup_name (parser, identifier,
15431 tag_type,
15432 /*is_template=*/false,
15433 /*is_namespace=*/false,
15434 /*check_dependency=*/true,
15435 &ambiguous_decls,
15436 token->location);
15438 /* If the lookup was ambiguous, an error will already have been
15439 issued. */
15440 if (ambiguous_decls)
15441 return error_mark_node;
15443 /* If we are parsing friend declaration, DECL may be a
15444 TEMPLATE_DECL tree node here. However, we need to check
15445 whether this TEMPLATE_DECL results in valid code. Consider
15446 the following example:
15448 namespace N {
15449 template <class T> class C {};
15451 class X {
15452 template <class T> friend class N::C; // #1, valid code
15454 template <class T> class Y {
15455 friend class N::C; // #2, invalid code
15458 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15459 name lookup of `N::C'. We see that friend declaration must
15460 be template for the code to be valid. Note that
15461 processing_template_decl does not work here since it is
15462 always 1 for the above two cases. */
15464 decl = (cp_parser_maybe_treat_template_as_class
15465 (decl, /*tag_name_p=*/is_friend
15466 && template_parm_lists_apply));
15468 if (TREE_CODE (decl) != TYPE_DECL)
15470 cp_parser_diagnose_invalid_type_name (parser,
15471 identifier,
15472 token->location);
15473 return error_mark_node;
15476 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15478 bool allow_template = (template_parm_lists_apply
15479 || DECL_SELF_REFERENCE_P (decl));
15480 type = check_elaborated_type_specifier (tag_type, decl,
15481 allow_template);
15483 if (type == error_mark_node)
15484 return error_mark_node;
15487 /* Forward declarations of nested types, such as
15489 class C1::C2;
15490 class C1::C2::C3;
15492 are invalid unless all components preceding the final '::'
15493 are complete. If all enclosing types are complete, these
15494 declarations become merely pointless.
15496 Invalid forward declarations of nested types are errors
15497 caught elsewhere in parsing. Those that are pointless arrive
15498 here. */
15500 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15501 && !is_friend && !processing_explicit_instantiation)
15502 warning (0, "declaration %qD does not declare anything", decl);
15504 type = TREE_TYPE (decl);
15506 else
15508 /* An elaborated-type-specifier sometimes introduces a new type and
15509 sometimes names an existing type. Normally, the rule is that it
15510 introduces a new type only if there is not an existing type of
15511 the same name already in scope. For example, given:
15513 struct S {};
15514 void f() { struct S s; }
15516 the `struct S' in the body of `f' is the same `struct S' as in
15517 the global scope; the existing definition is used. However, if
15518 there were no global declaration, this would introduce a new
15519 local class named `S'.
15521 An exception to this rule applies to the following code:
15523 namespace N { struct S; }
15525 Here, the elaborated-type-specifier names a new type
15526 unconditionally; even if there is already an `S' in the
15527 containing scope this declaration names a new type.
15528 This exception only applies if the elaborated-type-specifier
15529 forms the complete declaration:
15531 [class.name]
15533 A declaration consisting solely of `class-key identifier ;' is
15534 either a redeclaration of the name in the current scope or a
15535 forward declaration of the identifier as a class name. It
15536 introduces the name into the current scope.
15538 We are in this situation precisely when the next token is a `;'.
15540 An exception to the exception is that a `friend' declaration does
15541 *not* name a new type; i.e., given:
15543 struct S { friend struct T; };
15545 `T' is not a new type in the scope of `S'.
15547 Also, `new struct S' or `sizeof (struct S)' never results in the
15548 definition of a new type; a new type can only be declared in a
15549 declaration context. */
15551 tag_scope ts;
15552 bool template_p;
15554 if (is_friend)
15555 /* Friends have special name lookup rules. */
15556 ts = ts_within_enclosing_non_class;
15557 else if (is_declaration
15558 && cp_lexer_next_token_is (parser->lexer,
15559 CPP_SEMICOLON))
15560 /* This is a `class-key identifier ;' */
15561 ts = ts_current;
15562 else
15563 ts = ts_global;
15565 template_p =
15566 (template_parm_lists_apply
15567 && (cp_parser_next_token_starts_class_definition_p (parser)
15568 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15569 /* An unqualified name was used to reference this type, so
15570 there were no qualifying templates. */
15571 if (template_parm_lists_apply
15572 && !cp_parser_check_template_parameters (parser,
15573 /*num_templates=*/0,
15574 token->location,
15575 /*declarator=*/NULL))
15576 return error_mark_node;
15577 type = xref_tag (tag_type, identifier, ts, template_p);
15581 if (type == error_mark_node)
15582 return error_mark_node;
15584 /* Allow attributes on forward declarations of classes. */
15585 if (attributes)
15587 if (TREE_CODE (type) == TYPENAME_TYPE)
15588 warning (OPT_Wattributes,
15589 "attributes ignored on uninstantiated type");
15590 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15591 && ! processing_explicit_instantiation)
15592 warning (OPT_Wattributes,
15593 "attributes ignored on template instantiation");
15594 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15595 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15596 else
15597 warning (OPT_Wattributes,
15598 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15601 if (tag_type != enum_type)
15603 /* Indicate whether this class was declared as a `class' or as a
15604 `struct'. */
15605 if (TREE_CODE (type) == RECORD_TYPE)
15606 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15607 cp_parser_check_class_key (tag_type, type);
15610 /* A "<" cannot follow an elaborated type specifier. If that
15611 happens, the user was probably trying to form a template-id. */
15612 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15613 token->location);
15615 return type;
15618 /* Parse an enum-specifier.
15620 enum-specifier:
15621 enum-head { enumerator-list [opt] }
15622 enum-head { enumerator-list , } [C++0x]
15624 enum-head:
15625 enum-key identifier [opt] enum-base [opt]
15626 enum-key nested-name-specifier identifier enum-base [opt]
15628 enum-key:
15629 enum
15630 enum class [C++0x]
15631 enum struct [C++0x]
15633 enum-base: [C++0x]
15634 : type-specifier-seq
15636 opaque-enum-specifier:
15637 enum-key identifier enum-base [opt] ;
15639 GNU Extensions:
15640 enum-key attributes[opt] identifier [opt] enum-base [opt]
15641 { enumerator-list [opt] }attributes[opt]
15642 enum-key attributes[opt] identifier [opt] enum-base [opt]
15643 { enumerator-list, }attributes[opt] [C++0x]
15645 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15646 if the token stream isn't an enum-specifier after all. */
15648 static tree
15649 cp_parser_enum_specifier (cp_parser* parser)
15651 tree identifier;
15652 tree type = NULL_TREE;
15653 tree prev_scope;
15654 tree nested_name_specifier = NULL_TREE;
15655 tree attributes;
15656 bool scoped_enum_p = false;
15657 bool has_underlying_type = false;
15658 bool nested_being_defined = false;
15659 bool new_value_list = false;
15660 bool is_new_type = false;
15661 bool is_anonymous = false;
15662 tree underlying_type = NULL_TREE;
15663 cp_token *type_start_token = NULL;
15664 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15666 parser->colon_corrects_to_scope_p = false;
15668 /* Parse tentatively so that we can back up if we don't find a
15669 enum-specifier. */
15670 cp_parser_parse_tentatively (parser);
15672 /* Caller guarantees that the current token is 'enum', an identifier
15673 possibly follows, and the token after that is an opening brace.
15674 If we don't have an identifier, fabricate an anonymous name for
15675 the enumeration being defined. */
15676 cp_lexer_consume_token (parser->lexer);
15678 /* Parse the "class" or "struct", which indicates a scoped
15679 enumeration type in C++0x. */
15680 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15681 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15683 if (cxx_dialect < cxx11)
15684 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15686 /* Consume the `struct' or `class' token. */
15687 cp_lexer_consume_token (parser->lexer);
15689 scoped_enum_p = true;
15692 attributes = cp_parser_attributes_opt (parser);
15694 /* Clear the qualification. */
15695 parser->scope = NULL_TREE;
15696 parser->qualifying_scope = NULL_TREE;
15697 parser->object_scope = NULL_TREE;
15699 /* Figure out in what scope the declaration is being placed. */
15700 prev_scope = current_scope ();
15702 type_start_token = cp_lexer_peek_token (parser->lexer);
15704 push_deferring_access_checks (dk_no_check);
15705 nested_name_specifier
15706 = cp_parser_nested_name_specifier_opt (parser,
15707 /*typename_keyword_p=*/true,
15708 /*check_dependency_p=*/false,
15709 /*type_p=*/false,
15710 /*is_declaration=*/false);
15712 if (nested_name_specifier)
15714 tree name;
15716 identifier = cp_parser_identifier (parser);
15717 name = cp_parser_lookup_name (parser, identifier,
15718 enum_type,
15719 /*is_template=*/false,
15720 /*is_namespace=*/false,
15721 /*check_dependency=*/true,
15722 /*ambiguous_decls=*/NULL,
15723 input_location);
15724 if (name && name != error_mark_node)
15726 type = TREE_TYPE (name);
15727 if (TREE_CODE (type) == TYPENAME_TYPE)
15729 /* Are template enums allowed in ISO? */
15730 if (template_parm_scope_p ())
15731 pedwarn (type_start_token->location, OPT_Wpedantic,
15732 "%qD is an enumeration template", name);
15733 /* ignore a typename reference, for it will be solved by name
15734 in start_enum. */
15735 type = NULL_TREE;
15738 else if (nested_name_specifier == error_mark_node)
15739 /* We already issued an error. */;
15740 else
15741 error_at (type_start_token->location,
15742 "%qD is not an enumerator-name", identifier);
15744 else
15746 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15747 identifier = cp_parser_identifier (parser);
15748 else
15750 identifier = make_anon_name ();
15751 is_anonymous = true;
15752 if (scoped_enum_p)
15753 error_at (type_start_token->location,
15754 "anonymous scoped enum is not allowed");
15757 pop_deferring_access_checks ();
15759 /* Check for the `:' that denotes a specified underlying type in C++0x.
15760 Note that a ':' could also indicate a bitfield width, however. */
15761 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15763 cp_decl_specifier_seq type_specifiers;
15765 /* Consume the `:'. */
15766 cp_lexer_consume_token (parser->lexer);
15768 /* Parse the type-specifier-seq. */
15769 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15770 /*is_trailing_return=*/false,
15771 &type_specifiers);
15773 /* At this point this is surely not elaborated type specifier. */
15774 if (!cp_parser_parse_definitely (parser))
15775 return NULL_TREE;
15777 if (cxx_dialect < cxx11)
15778 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15780 has_underlying_type = true;
15782 /* If that didn't work, stop. */
15783 if (type_specifiers.type != error_mark_node)
15785 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15786 /*initialized=*/0, NULL);
15787 if (underlying_type == error_mark_node
15788 || check_for_bare_parameter_packs (underlying_type))
15789 underlying_type = NULL_TREE;
15793 /* Look for the `{' but don't consume it yet. */
15794 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15796 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15798 cp_parser_error (parser, "expected %<{%>");
15799 if (has_underlying_type)
15801 type = NULL_TREE;
15802 goto out;
15805 /* An opaque-enum-specifier must have a ';' here. */
15806 if ((scoped_enum_p || underlying_type)
15807 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15809 cp_parser_error (parser, "expected %<;%> or %<{%>");
15810 if (has_underlying_type)
15812 type = NULL_TREE;
15813 goto out;
15818 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15819 return NULL_TREE;
15821 if (nested_name_specifier)
15823 if (CLASS_TYPE_P (nested_name_specifier))
15825 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15826 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15827 push_scope (nested_name_specifier);
15829 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15831 push_nested_namespace (nested_name_specifier);
15835 /* Issue an error message if type-definitions are forbidden here. */
15836 if (!cp_parser_check_type_definition (parser))
15837 type = error_mark_node;
15838 else
15839 /* Create the new type. We do this before consuming the opening
15840 brace so the enum will be recorded as being on the line of its
15841 tag (or the 'enum' keyword, if there is no tag). */
15842 type = start_enum (identifier, type, underlying_type,
15843 scoped_enum_p, &is_new_type);
15845 /* If the next token is not '{' it is an opaque-enum-specifier or an
15846 elaborated-type-specifier. */
15847 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15849 timevar_push (TV_PARSE_ENUM);
15850 if (nested_name_specifier
15851 && nested_name_specifier != error_mark_node)
15853 /* The following catches invalid code such as:
15854 enum class S<int>::E { A, B, C }; */
15855 if (!processing_specialization
15856 && CLASS_TYPE_P (nested_name_specifier)
15857 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15858 error_at (type_start_token->location, "cannot add an enumerator "
15859 "list to a template instantiation");
15861 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15863 error_at (type_start_token->location,
15864 "%<%T::%E%> has not been declared",
15865 TYPE_CONTEXT (nested_name_specifier),
15866 nested_name_specifier);
15867 type = error_mark_node;
15869 /* If that scope does not contain the scope in which the
15870 class was originally declared, the program is invalid. */
15871 else if (prev_scope && !is_ancestor (prev_scope,
15872 nested_name_specifier))
15874 if (at_namespace_scope_p ())
15875 error_at (type_start_token->location,
15876 "declaration of %qD in namespace %qD which does not "
15877 "enclose %qD",
15878 type, prev_scope, nested_name_specifier);
15879 else
15880 error_at (type_start_token->location,
15881 "declaration of %qD in %qD which does not "
15882 "enclose %qD",
15883 type, prev_scope, nested_name_specifier);
15884 type = error_mark_node;
15888 if (scoped_enum_p)
15889 begin_scope (sk_scoped_enum, type);
15891 /* Consume the opening brace. */
15892 cp_lexer_consume_token (parser->lexer);
15894 if (type == error_mark_node)
15895 ; /* Nothing to add */
15896 else if (OPAQUE_ENUM_P (type)
15897 || (cxx_dialect > cxx98 && processing_specialization))
15899 new_value_list = true;
15900 SET_OPAQUE_ENUM_P (type, false);
15901 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15903 else
15905 error_at (type_start_token->location,
15906 "multiple definition of %q#T", type);
15907 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15908 "previous definition here");
15909 type = error_mark_node;
15912 if (type == error_mark_node)
15913 cp_parser_skip_to_end_of_block_or_statement (parser);
15914 /* If the next token is not '}', then there are some enumerators. */
15915 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15917 if (is_anonymous && !scoped_enum_p)
15918 pedwarn (type_start_token->location, OPT_Wpedantic,
15919 "ISO C++ forbids empty anonymous enum");
15921 else
15922 cp_parser_enumerator_list (parser, type);
15924 /* Consume the final '}'. */
15925 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15927 if (scoped_enum_p)
15928 finish_scope ();
15929 timevar_pop (TV_PARSE_ENUM);
15931 else
15933 /* If a ';' follows, then it is an opaque-enum-specifier
15934 and additional restrictions apply. */
15935 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15937 if (is_anonymous)
15938 error_at (type_start_token->location,
15939 "opaque-enum-specifier without name");
15940 else if (nested_name_specifier)
15941 error_at (type_start_token->location,
15942 "opaque-enum-specifier must use a simple identifier");
15946 /* Look for trailing attributes to apply to this enumeration, and
15947 apply them if appropriate. */
15948 if (cp_parser_allow_gnu_extensions_p (parser))
15950 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15951 trailing_attr = chainon (trailing_attr, attributes);
15952 cplus_decl_attributes (&type,
15953 trailing_attr,
15954 (int) ATTR_FLAG_TYPE_IN_PLACE);
15957 /* Finish up the enumeration. */
15958 if (type != error_mark_node)
15960 if (new_value_list)
15961 finish_enum_value_list (type);
15962 if (is_new_type)
15963 finish_enum (type);
15966 if (nested_name_specifier)
15968 if (CLASS_TYPE_P (nested_name_specifier))
15970 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15971 pop_scope (nested_name_specifier);
15973 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15975 pop_nested_namespace (nested_name_specifier);
15978 out:
15979 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15980 return type;
15983 /* Parse an enumerator-list. The enumerators all have the indicated
15984 TYPE.
15986 enumerator-list:
15987 enumerator-definition
15988 enumerator-list , enumerator-definition */
15990 static void
15991 cp_parser_enumerator_list (cp_parser* parser, tree type)
15993 while (true)
15995 /* Parse an enumerator-definition. */
15996 cp_parser_enumerator_definition (parser, type);
15998 /* If the next token is not a ',', we've reached the end of
15999 the list. */
16000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16001 break;
16002 /* Otherwise, consume the `,' and keep going. */
16003 cp_lexer_consume_token (parser->lexer);
16004 /* If the next token is a `}', there is a trailing comma. */
16005 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16007 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16008 pedwarn (input_location, OPT_Wpedantic,
16009 "comma at end of enumerator list");
16010 break;
16015 /* Parse an enumerator-definition. The enumerator has the indicated
16016 TYPE.
16018 enumerator-definition:
16019 enumerator
16020 enumerator = constant-expression
16022 enumerator:
16023 identifier */
16025 static void
16026 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16028 tree identifier;
16029 tree value;
16030 location_t loc;
16032 /* Save the input location because we are interested in the location
16033 of the identifier and not the location of the explicit value. */
16034 loc = cp_lexer_peek_token (parser->lexer)->location;
16036 /* Look for the identifier. */
16037 identifier = cp_parser_identifier (parser);
16038 if (identifier == error_mark_node)
16039 return;
16041 /* If the next token is an '=', then there is an explicit value. */
16042 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16044 /* Consume the `=' token. */
16045 cp_lexer_consume_token (parser->lexer);
16046 /* Parse the value. */
16047 value = cp_parser_constant_expression (parser);
16049 else
16050 value = NULL_TREE;
16052 /* If we are processing a template, make sure the initializer of the
16053 enumerator doesn't contain any bare template parameter pack. */
16054 if (check_for_bare_parameter_packs (value))
16055 value = error_mark_node;
16057 /* Create the enumerator. */
16058 build_enumerator (identifier, value, type, loc);
16061 /* Parse a namespace-name.
16063 namespace-name:
16064 original-namespace-name
16065 namespace-alias
16067 Returns the NAMESPACE_DECL for the namespace. */
16069 static tree
16070 cp_parser_namespace_name (cp_parser* parser)
16072 tree identifier;
16073 tree namespace_decl;
16075 cp_token *token = cp_lexer_peek_token (parser->lexer);
16077 /* Get the name of the namespace. */
16078 identifier = cp_parser_identifier (parser);
16079 if (identifier == error_mark_node)
16080 return error_mark_node;
16082 /* Look up the identifier in the currently active scope. Look only
16083 for namespaces, due to:
16085 [basic.lookup.udir]
16087 When looking up a namespace-name in a using-directive or alias
16088 definition, only namespace names are considered.
16090 And:
16092 [basic.lookup.qual]
16094 During the lookup of a name preceding the :: scope resolution
16095 operator, object, function, and enumerator names are ignored.
16097 (Note that cp_parser_qualifying_entity only calls this
16098 function if the token after the name is the scope resolution
16099 operator.) */
16100 namespace_decl = cp_parser_lookup_name (parser, identifier,
16101 none_type,
16102 /*is_template=*/false,
16103 /*is_namespace=*/true,
16104 /*check_dependency=*/true,
16105 /*ambiguous_decls=*/NULL,
16106 token->location);
16107 /* If it's not a namespace, issue an error. */
16108 if (namespace_decl == error_mark_node
16109 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16111 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16112 error_at (token->location, "%qD is not a namespace-name", identifier);
16113 cp_parser_error (parser, "expected namespace-name");
16114 namespace_decl = error_mark_node;
16117 return namespace_decl;
16120 /* Parse a namespace-definition.
16122 namespace-definition:
16123 named-namespace-definition
16124 unnamed-namespace-definition
16126 named-namespace-definition:
16127 original-namespace-definition
16128 extension-namespace-definition
16130 original-namespace-definition:
16131 namespace identifier { namespace-body }
16133 extension-namespace-definition:
16134 namespace original-namespace-name { namespace-body }
16136 unnamed-namespace-definition:
16137 namespace { namespace-body } */
16139 static void
16140 cp_parser_namespace_definition (cp_parser* parser)
16142 tree identifier, attribs;
16143 bool has_visibility;
16144 bool is_inline;
16146 cp_ensure_no_omp_declare_simd (parser);
16147 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16149 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16150 is_inline = true;
16151 cp_lexer_consume_token (parser->lexer);
16153 else
16154 is_inline = false;
16156 /* Look for the `namespace' keyword. */
16157 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16159 /* Get the name of the namespace. We do not attempt to distinguish
16160 between an original-namespace-definition and an
16161 extension-namespace-definition at this point. The semantic
16162 analysis routines are responsible for that. */
16163 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16164 identifier = cp_parser_identifier (parser);
16165 else
16166 identifier = NULL_TREE;
16168 /* Parse any specified attributes. */
16169 attribs = cp_parser_attributes_opt (parser);
16171 /* Look for the `{' to start the namespace. */
16172 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16173 /* Start the namespace. */
16174 push_namespace (identifier);
16176 /* "inline namespace" is equivalent to a stub namespace definition
16177 followed by a strong using directive. */
16178 if (is_inline)
16180 tree name_space = current_namespace;
16181 /* Set up namespace association. */
16182 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16183 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16184 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16185 /* Import the contents of the inline namespace. */
16186 pop_namespace ();
16187 do_using_directive (name_space);
16188 push_namespace (identifier);
16191 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16193 /* Parse the body of the namespace. */
16194 cp_parser_namespace_body (parser);
16196 if (has_visibility)
16197 pop_visibility (1);
16199 /* Finish the namespace. */
16200 pop_namespace ();
16201 /* Look for the final `}'. */
16202 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16205 /* Parse a namespace-body.
16207 namespace-body:
16208 declaration-seq [opt] */
16210 static void
16211 cp_parser_namespace_body (cp_parser* parser)
16213 cp_parser_declaration_seq_opt (parser);
16216 /* Parse a namespace-alias-definition.
16218 namespace-alias-definition:
16219 namespace identifier = qualified-namespace-specifier ; */
16221 static void
16222 cp_parser_namespace_alias_definition (cp_parser* parser)
16224 tree identifier;
16225 tree namespace_specifier;
16227 cp_token *token = cp_lexer_peek_token (parser->lexer);
16229 /* Look for the `namespace' keyword. */
16230 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16231 /* Look for the identifier. */
16232 identifier = cp_parser_identifier (parser);
16233 if (identifier == error_mark_node)
16234 return;
16235 /* Look for the `=' token. */
16236 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16237 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16239 error_at (token->location, "%<namespace%> definition is not allowed here");
16240 /* Skip the definition. */
16241 cp_lexer_consume_token (parser->lexer);
16242 if (cp_parser_skip_to_closing_brace (parser))
16243 cp_lexer_consume_token (parser->lexer);
16244 return;
16246 cp_parser_require (parser, CPP_EQ, RT_EQ);
16247 /* Look for the qualified-namespace-specifier. */
16248 namespace_specifier
16249 = cp_parser_qualified_namespace_specifier (parser);
16250 /* Look for the `;' token. */
16251 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16253 /* Register the alias in the symbol table. */
16254 do_namespace_alias (identifier, namespace_specifier);
16257 /* Parse a qualified-namespace-specifier.
16259 qualified-namespace-specifier:
16260 :: [opt] nested-name-specifier [opt] namespace-name
16262 Returns a NAMESPACE_DECL corresponding to the specified
16263 namespace. */
16265 static tree
16266 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16268 /* Look for the optional `::'. */
16269 cp_parser_global_scope_opt (parser,
16270 /*current_scope_valid_p=*/false);
16272 /* Look for the optional nested-name-specifier. */
16273 cp_parser_nested_name_specifier_opt (parser,
16274 /*typename_keyword_p=*/false,
16275 /*check_dependency_p=*/true,
16276 /*type_p=*/false,
16277 /*is_declaration=*/true);
16279 return cp_parser_namespace_name (parser);
16282 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16283 access declaration.
16285 using-declaration:
16286 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16287 using :: unqualified-id ;
16289 access-declaration:
16290 qualified-id ;
16294 static bool
16295 cp_parser_using_declaration (cp_parser* parser,
16296 bool access_declaration_p)
16298 cp_token *token;
16299 bool typename_p = false;
16300 bool global_scope_p;
16301 tree decl;
16302 tree identifier;
16303 tree qscope;
16304 int oldcount = errorcount;
16305 cp_token *diag_token = NULL;
16307 if (access_declaration_p)
16309 diag_token = cp_lexer_peek_token (parser->lexer);
16310 cp_parser_parse_tentatively (parser);
16312 else
16314 /* Look for the `using' keyword. */
16315 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16317 /* Peek at the next token. */
16318 token = cp_lexer_peek_token (parser->lexer);
16319 /* See if it's `typename'. */
16320 if (token->keyword == RID_TYPENAME)
16322 /* Remember that we've seen it. */
16323 typename_p = true;
16324 /* Consume the `typename' token. */
16325 cp_lexer_consume_token (parser->lexer);
16329 /* Look for the optional global scope qualification. */
16330 global_scope_p
16331 = (cp_parser_global_scope_opt (parser,
16332 /*current_scope_valid_p=*/false)
16333 != NULL_TREE);
16335 /* If we saw `typename', or didn't see `::', then there must be a
16336 nested-name-specifier present. */
16337 if (typename_p || !global_scope_p)
16339 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16340 /*check_dependency_p=*/true,
16341 /*type_p=*/false,
16342 /*is_declaration=*/true);
16343 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16345 cp_parser_skip_to_end_of_block_or_statement (parser);
16346 return false;
16349 /* Otherwise, we could be in either of the two productions. In that
16350 case, treat the nested-name-specifier as optional. */
16351 else
16352 qscope = cp_parser_nested_name_specifier_opt (parser,
16353 /*typename_keyword_p=*/false,
16354 /*check_dependency_p=*/true,
16355 /*type_p=*/false,
16356 /*is_declaration=*/true);
16357 if (!qscope)
16358 qscope = global_namespace;
16359 else if (UNSCOPED_ENUM_P (qscope))
16360 qscope = CP_TYPE_CONTEXT (qscope);
16362 if (access_declaration_p && cp_parser_error_occurred (parser))
16363 /* Something has already gone wrong; there's no need to parse
16364 further. Since an error has occurred, the return value of
16365 cp_parser_parse_definitely will be false, as required. */
16366 return cp_parser_parse_definitely (parser);
16368 token = cp_lexer_peek_token (parser->lexer);
16369 /* Parse the unqualified-id. */
16370 identifier = cp_parser_unqualified_id (parser,
16371 /*template_keyword_p=*/false,
16372 /*check_dependency_p=*/true,
16373 /*declarator_p=*/true,
16374 /*optional_p=*/false);
16376 if (access_declaration_p)
16378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16379 cp_parser_simulate_error (parser);
16380 if (!cp_parser_parse_definitely (parser))
16381 return false;
16384 /* The function we call to handle a using-declaration is different
16385 depending on what scope we are in. */
16386 if (qscope == error_mark_node || identifier == error_mark_node)
16388 else if (!identifier_p (identifier)
16389 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16390 /* [namespace.udecl]
16392 A using declaration shall not name a template-id. */
16393 error_at (token->location,
16394 "a template-id may not appear in a using-declaration");
16395 else
16397 if (at_class_scope_p ())
16399 /* Create the USING_DECL. */
16400 decl = do_class_using_decl (parser->scope, identifier);
16402 if (decl && typename_p)
16403 USING_DECL_TYPENAME_P (decl) = 1;
16405 if (check_for_bare_parameter_packs (decl))
16407 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16408 return false;
16410 else
16411 /* Add it to the list of members in this class. */
16412 finish_member_declaration (decl);
16414 else
16416 decl = cp_parser_lookup_name_simple (parser,
16417 identifier,
16418 token->location);
16419 if (decl == error_mark_node)
16420 cp_parser_name_lookup_error (parser, identifier,
16421 decl, NLE_NULL,
16422 token->location);
16423 else if (check_for_bare_parameter_packs (decl))
16425 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16426 return false;
16428 else if (!at_namespace_scope_p ())
16429 do_local_using_decl (decl, qscope, identifier);
16430 else
16431 do_toplevel_using_decl (decl, qscope, identifier);
16435 /* Look for the final `;'. */
16436 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16438 if (access_declaration_p && errorcount == oldcount)
16439 warning_at (diag_token->location, OPT_Wdeprecated,
16440 "access declarations are deprecated "
16441 "in favour of using-declarations; "
16442 "suggestion: add the %<using%> keyword");
16444 return true;
16447 /* Parse an alias-declaration.
16449 alias-declaration:
16450 using identifier attribute-specifier-seq [opt] = type-id */
16452 static tree
16453 cp_parser_alias_declaration (cp_parser* parser)
16455 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16456 location_t id_location;
16457 cp_declarator *declarator;
16458 cp_decl_specifier_seq decl_specs;
16459 bool member_p;
16460 const char *saved_message = NULL;
16462 /* Look for the `using' keyword. */
16463 cp_token *using_token
16464 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16465 if (using_token == NULL)
16466 return error_mark_node;
16468 id_location = cp_lexer_peek_token (parser->lexer)->location;
16469 id = cp_parser_identifier (parser);
16470 if (id == error_mark_node)
16471 return error_mark_node;
16473 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16474 attributes = cp_parser_attributes_opt (parser);
16475 if (attributes == error_mark_node)
16476 return error_mark_node;
16478 cp_parser_require (parser, CPP_EQ, RT_EQ);
16480 if (cp_parser_error_occurred (parser))
16481 return error_mark_node;
16483 cp_parser_commit_to_tentative_parse (parser);
16485 /* Now we are going to parse the type-id of the declaration. */
16488 [dcl.type]/3 says:
16490 "A type-specifier-seq shall not define a class or enumeration
16491 unless it appears in the type-id of an alias-declaration (7.1.3) that
16492 is not the declaration of a template-declaration."
16494 In other words, if we currently are in an alias template, the
16495 type-id should not define a type.
16497 So let's set parser->type_definition_forbidden_message in that
16498 case; cp_parser_check_type_definition (called by
16499 cp_parser_class_specifier) will then emit an error if a type is
16500 defined in the type-id. */
16501 if (parser->num_template_parameter_lists)
16503 saved_message = parser->type_definition_forbidden_message;
16504 parser->type_definition_forbidden_message =
16505 G_("types may not be defined in alias template declarations");
16508 type = cp_parser_type_id (parser);
16510 /* Restore the error message if need be. */
16511 if (parser->num_template_parameter_lists)
16512 parser->type_definition_forbidden_message = saved_message;
16514 if (type == error_mark_node
16515 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16517 cp_parser_skip_to_end_of_block_or_statement (parser);
16518 return error_mark_node;
16521 /* A typedef-name can also be introduced by an alias-declaration. The
16522 identifier following the using keyword becomes a typedef-name. It has
16523 the same semantics as if it were introduced by the typedef
16524 specifier. In particular, it does not define a new type and it shall
16525 not appear in the type-id. */
16527 clear_decl_specs (&decl_specs);
16528 decl_specs.type = type;
16529 if (attributes != NULL_TREE)
16531 decl_specs.attributes = attributes;
16532 set_and_check_decl_spec_loc (&decl_specs,
16533 ds_attribute,
16534 attrs_token);
16536 set_and_check_decl_spec_loc (&decl_specs,
16537 ds_typedef,
16538 using_token);
16539 set_and_check_decl_spec_loc (&decl_specs,
16540 ds_alias,
16541 using_token);
16543 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16544 declarator->id_loc = id_location;
16546 member_p = at_class_scope_p ();
16547 if (member_p)
16548 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16549 NULL_TREE, attributes);
16550 else
16551 decl = start_decl (declarator, &decl_specs, 0,
16552 attributes, NULL_TREE, &pushed_scope);
16553 if (decl == error_mark_node)
16554 return decl;
16556 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16558 if (pushed_scope)
16559 pop_scope (pushed_scope);
16561 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16562 added into the symbol table; otherwise, return the TYPE_DECL. */
16563 if (DECL_LANG_SPECIFIC (decl)
16564 && DECL_TEMPLATE_INFO (decl)
16565 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16567 decl = DECL_TI_TEMPLATE (decl);
16568 if (member_p)
16569 check_member_template (decl);
16572 return decl;
16575 /* Parse a using-directive.
16577 using-directive:
16578 using namespace :: [opt] nested-name-specifier [opt]
16579 namespace-name ; */
16581 static void
16582 cp_parser_using_directive (cp_parser* parser)
16584 tree namespace_decl;
16585 tree attribs;
16587 /* Look for the `using' keyword. */
16588 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16589 /* And the `namespace' keyword. */
16590 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16591 /* Look for the optional `::' operator. */
16592 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16593 /* And the optional nested-name-specifier. */
16594 cp_parser_nested_name_specifier_opt (parser,
16595 /*typename_keyword_p=*/false,
16596 /*check_dependency_p=*/true,
16597 /*type_p=*/false,
16598 /*is_declaration=*/true);
16599 /* Get the namespace being used. */
16600 namespace_decl = cp_parser_namespace_name (parser);
16601 /* And any specified attributes. */
16602 attribs = cp_parser_attributes_opt (parser);
16603 /* Update the symbol table. */
16604 parse_using_directive (namespace_decl, attribs);
16605 /* Look for the final `;'. */
16606 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16609 /* Parse an asm-definition.
16611 asm-definition:
16612 asm ( string-literal ) ;
16614 GNU Extension:
16616 asm-definition:
16617 asm volatile [opt] ( string-literal ) ;
16618 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16619 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16620 : asm-operand-list [opt] ) ;
16621 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16622 : asm-operand-list [opt]
16623 : asm-clobber-list [opt] ) ;
16624 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16625 : asm-clobber-list [opt]
16626 : asm-goto-list ) ; */
16628 static void
16629 cp_parser_asm_definition (cp_parser* parser)
16631 tree string;
16632 tree outputs = NULL_TREE;
16633 tree inputs = NULL_TREE;
16634 tree clobbers = NULL_TREE;
16635 tree labels = NULL_TREE;
16636 tree asm_stmt;
16637 bool volatile_p = false;
16638 bool extended_p = false;
16639 bool invalid_inputs_p = false;
16640 bool invalid_outputs_p = false;
16641 bool goto_p = false;
16642 required_token missing = RT_NONE;
16644 /* Look for the `asm' keyword. */
16645 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16647 if (parser->in_function_body
16648 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16650 error ("%<asm%> in %<constexpr%> function");
16651 cp_function_chain->invalid_constexpr = true;
16654 /* See if the next token is `volatile'. */
16655 if (cp_parser_allow_gnu_extensions_p (parser)
16656 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16658 /* Remember that we saw the `volatile' keyword. */
16659 volatile_p = true;
16660 /* Consume the token. */
16661 cp_lexer_consume_token (parser->lexer);
16663 if (cp_parser_allow_gnu_extensions_p (parser)
16664 && parser->in_function_body
16665 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16667 /* Remember that we saw the `goto' keyword. */
16668 goto_p = true;
16669 /* Consume the token. */
16670 cp_lexer_consume_token (parser->lexer);
16672 /* Look for the opening `('. */
16673 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16674 return;
16675 /* Look for the string. */
16676 string = cp_parser_string_literal (parser, false, false);
16677 if (string == error_mark_node)
16679 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16680 /*consume_paren=*/true);
16681 return;
16684 /* If we're allowing GNU extensions, check for the extended assembly
16685 syntax. Unfortunately, the `:' tokens need not be separated by
16686 a space in C, and so, for compatibility, we tolerate that here
16687 too. Doing that means that we have to treat the `::' operator as
16688 two `:' tokens. */
16689 if (cp_parser_allow_gnu_extensions_p (parser)
16690 && parser->in_function_body
16691 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16692 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16694 bool inputs_p = false;
16695 bool clobbers_p = false;
16696 bool labels_p = false;
16698 /* The extended syntax was used. */
16699 extended_p = true;
16701 /* Look for outputs. */
16702 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16704 /* Consume the `:'. */
16705 cp_lexer_consume_token (parser->lexer);
16706 /* Parse the output-operands. */
16707 if (cp_lexer_next_token_is_not (parser->lexer,
16708 CPP_COLON)
16709 && cp_lexer_next_token_is_not (parser->lexer,
16710 CPP_SCOPE)
16711 && cp_lexer_next_token_is_not (parser->lexer,
16712 CPP_CLOSE_PAREN)
16713 && !goto_p)
16714 outputs = cp_parser_asm_operand_list (parser);
16716 if (outputs == error_mark_node)
16717 invalid_outputs_p = true;
16719 /* If the next token is `::', there are no outputs, and the
16720 next token is the beginning of the inputs. */
16721 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16722 /* The inputs are coming next. */
16723 inputs_p = true;
16725 /* Look for inputs. */
16726 if (inputs_p
16727 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16729 /* Consume the `:' or `::'. */
16730 cp_lexer_consume_token (parser->lexer);
16731 /* Parse the output-operands. */
16732 if (cp_lexer_next_token_is_not (parser->lexer,
16733 CPP_COLON)
16734 && cp_lexer_next_token_is_not (parser->lexer,
16735 CPP_SCOPE)
16736 && cp_lexer_next_token_is_not (parser->lexer,
16737 CPP_CLOSE_PAREN))
16738 inputs = cp_parser_asm_operand_list (parser);
16740 if (inputs == error_mark_node)
16741 invalid_inputs_p = true;
16743 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16744 /* The clobbers are coming next. */
16745 clobbers_p = true;
16747 /* Look for clobbers. */
16748 if (clobbers_p
16749 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16751 clobbers_p = true;
16752 /* Consume the `:' or `::'. */
16753 cp_lexer_consume_token (parser->lexer);
16754 /* Parse the clobbers. */
16755 if (cp_lexer_next_token_is_not (parser->lexer,
16756 CPP_COLON)
16757 && cp_lexer_next_token_is_not (parser->lexer,
16758 CPP_CLOSE_PAREN))
16759 clobbers = cp_parser_asm_clobber_list (parser);
16761 else if (goto_p
16762 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16763 /* The labels are coming next. */
16764 labels_p = true;
16766 /* Look for labels. */
16767 if (labels_p
16768 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16770 labels_p = true;
16771 /* Consume the `:' or `::'. */
16772 cp_lexer_consume_token (parser->lexer);
16773 /* Parse the labels. */
16774 labels = cp_parser_asm_label_list (parser);
16777 if (goto_p && !labels_p)
16778 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16780 else if (goto_p)
16781 missing = RT_COLON_SCOPE;
16783 /* Look for the closing `)'. */
16784 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16785 missing ? missing : RT_CLOSE_PAREN))
16786 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16787 /*consume_paren=*/true);
16788 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16790 if (!invalid_inputs_p && !invalid_outputs_p)
16792 /* Create the ASM_EXPR. */
16793 if (parser->in_function_body)
16795 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16796 inputs, clobbers, labels);
16797 /* If the extended syntax was not used, mark the ASM_EXPR. */
16798 if (!extended_p)
16800 tree temp = asm_stmt;
16801 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16802 temp = TREE_OPERAND (temp, 0);
16804 ASM_INPUT_P (temp) = 1;
16807 else
16808 symtab->finalize_toplevel_asm (string);
16812 /* Declarators [gram.dcl.decl] */
16814 /* Parse an init-declarator.
16816 init-declarator:
16817 declarator initializer [opt]
16819 GNU Extension:
16821 init-declarator:
16822 declarator asm-specification [opt] attributes [opt] initializer [opt]
16824 function-definition:
16825 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16826 function-body
16827 decl-specifier-seq [opt] declarator function-try-block
16829 GNU Extension:
16831 function-definition:
16832 __extension__ function-definition
16834 TM Extension:
16836 function-definition:
16837 decl-specifier-seq [opt] declarator function-transaction-block
16839 The DECL_SPECIFIERS apply to this declarator. Returns a
16840 representation of the entity declared. If MEMBER_P is TRUE, then
16841 this declarator appears in a class scope. The new DECL created by
16842 this declarator is returned.
16844 The CHECKS are access checks that should be performed once we know
16845 what entity is being declared (and, therefore, what classes have
16846 befriended it).
16848 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16849 for a function-definition here as well. If the declarator is a
16850 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16851 be TRUE upon return. By that point, the function-definition will
16852 have been completely parsed.
16854 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16855 is FALSE.
16857 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16858 parsed declaration if it is an uninitialized single declarator not followed
16859 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16860 if present, will not be consumed. If returned, this declarator will be
16861 created with SD_INITIALIZED but will not call cp_finish_decl.
16863 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16864 and there is an initializer, the pointed location_t is set to the
16865 location of the '=' or `(', or '{' in C++11 token introducing the
16866 initializer. */
16868 static tree
16869 cp_parser_init_declarator (cp_parser* parser,
16870 cp_decl_specifier_seq *decl_specifiers,
16871 vec<deferred_access_check, va_gc> *checks,
16872 bool function_definition_allowed_p,
16873 bool member_p,
16874 int declares_class_or_enum,
16875 bool* function_definition_p,
16876 tree* maybe_range_for_decl,
16877 location_t* init_loc)
16879 cp_token *token = NULL, *asm_spec_start_token = NULL,
16880 *attributes_start_token = NULL;
16881 cp_declarator *declarator;
16882 tree prefix_attributes;
16883 tree attributes = NULL;
16884 tree asm_specification;
16885 tree initializer;
16886 tree decl = NULL_TREE;
16887 tree scope;
16888 int is_initialized;
16889 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16890 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16891 "(...)". */
16892 enum cpp_ttype initialization_kind;
16893 bool is_direct_init = false;
16894 bool is_non_constant_init;
16895 int ctor_dtor_or_conv_p;
16896 bool friend_p = cp_parser_friend_p (decl_specifiers);
16897 tree pushed_scope = NULL_TREE;
16898 bool range_for_decl_p = false;
16899 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16900 location_t tmp_init_loc = UNKNOWN_LOCATION;
16902 /* Gather the attributes that were provided with the
16903 decl-specifiers. */
16904 prefix_attributes = decl_specifiers->attributes;
16906 /* Assume that this is not the declarator for a function
16907 definition. */
16908 if (function_definition_p)
16909 *function_definition_p = false;
16911 /* Default arguments are only permitted for function parameters. */
16912 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16913 parser->default_arg_ok_p = false;
16915 /* Defer access checks while parsing the declarator; we cannot know
16916 what names are accessible until we know what is being
16917 declared. */
16918 resume_deferring_access_checks ();
16920 /* Parse the declarator. */
16921 token = cp_lexer_peek_token (parser->lexer);
16922 declarator
16923 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16924 &ctor_dtor_or_conv_p,
16925 /*parenthesized_p=*/NULL,
16926 member_p, friend_p);
16927 /* Gather up the deferred checks. */
16928 stop_deferring_access_checks ();
16930 parser->default_arg_ok_p = saved_default_arg_ok_p;
16932 /* If the DECLARATOR was erroneous, there's no need to go
16933 further. */
16934 if (declarator == cp_error_declarator)
16935 return error_mark_node;
16937 /* Check that the number of template-parameter-lists is OK. */
16938 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16939 token->location))
16940 return error_mark_node;
16942 if (declares_class_or_enum & 2)
16943 cp_parser_check_for_definition_in_return_type (declarator,
16944 decl_specifiers->type,
16945 decl_specifiers->locations[ds_type_spec]);
16947 /* Figure out what scope the entity declared by the DECLARATOR is
16948 located in. `grokdeclarator' sometimes changes the scope, so
16949 we compute it now. */
16950 scope = get_scope_of_declarator (declarator);
16952 /* Perform any lookups in the declared type which were thought to be
16953 dependent, but are not in the scope of the declarator. */
16954 decl_specifiers->type
16955 = maybe_update_decl_type (decl_specifiers->type, scope);
16957 /* If we're allowing GNU extensions, look for an
16958 asm-specification. */
16959 if (cp_parser_allow_gnu_extensions_p (parser))
16961 /* Look for an asm-specification. */
16962 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16963 asm_specification = cp_parser_asm_specification_opt (parser);
16965 else
16966 asm_specification = NULL_TREE;
16968 /* Look for attributes. */
16969 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16970 attributes = cp_parser_attributes_opt (parser);
16972 /* Peek at the next token. */
16973 token = cp_lexer_peek_token (parser->lexer);
16975 bool bogus_implicit_tmpl = false;
16977 if (function_declarator_p (declarator))
16979 /* Check to see if the token indicates the start of a
16980 function-definition. */
16981 if (cp_parser_token_starts_function_definition_p (token))
16983 if (!function_definition_allowed_p)
16985 /* If a function-definition should not appear here, issue an
16986 error message. */
16987 cp_parser_error (parser,
16988 "a function-definition is not allowed here");
16989 return error_mark_node;
16992 location_t func_brace_location
16993 = cp_lexer_peek_token (parser->lexer)->location;
16995 /* Neither attributes nor an asm-specification are allowed
16996 on a function-definition. */
16997 if (asm_specification)
16998 error_at (asm_spec_start_token->location,
16999 "an asm-specification is not allowed "
17000 "on a function-definition");
17001 if (attributes)
17002 error_at (attributes_start_token->location,
17003 "attributes are not allowed "
17004 "on a function-definition");
17005 /* This is a function-definition. */
17006 *function_definition_p = true;
17008 /* Parse the function definition. */
17009 if (member_p)
17010 decl = cp_parser_save_member_function_body (parser,
17011 decl_specifiers,
17012 declarator,
17013 prefix_attributes);
17014 else
17015 decl =
17016 (cp_parser_function_definition_from_specifiers_and_declarator
17017 (parser, decl_specifiers, prefix_attributes, declarator));
17019 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17021 /* This is where the prologue starts... */
17022 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17023 = func_brace_location;
17026 return decl;
17029 else if (parser->fully_implicit_function_template_p)
17031 /* A non-template declaration involving a function parameter list
17032 containing an implicit template parameter will be made into a
17033 template. If the resulting declaration is not going to be an
17034 actual function then finish the template scope here to prevent it.
17035 An error message will be issued once we have a decl to talk about.
17037 FIXME probably we should do type deduction rather than create an
17038 implicit template, but the standard currently doesn't allow it. */
17039 bogus_implicit_tmpl = true;
17040 finish_fully_implicit_template (parser, NULL_TREE);
17043 /* [dcl.dcl]
17045 Only in function declarations for constructors, destructors, and
17046 type conversions can the decl-specifier-seq be omitted.
17048 We explicitly postpone this check past the point where we handle
17049 function-definitions because we tolerate function-definitions
17050 that are missing their return types in some modes. */
17051 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17053 cp_parser_error (parser,
17054 "expected constructor, destructor, or type conversion");
17055 return error_mark_node;
17058 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17059 if (token->type == CPP_EQ
17060 || token->type == CPP_OPEN_PAREN
17061 || token->type == CPP_OPEN_BRACE)
17063 is_initialized = SD_INITIALIZED;
17064 initialization_kind = token->type;
17065 if (maybe_range_for_decl)
17066 *maybe_range_for_decl = error_mark_node;
17067 tmp_init_loc = token->location;
17068 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17069 *init_loc = tmp_init_loc;
17071 if (token->type == CPP_EQ
17072 && function_declarator_p (declarator))
17074 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17075 if (t2->keyword == RID_DEFAULT)
17076 is_initialized = SD_DEFAULTED;
17077 else if (t2->keyword == RID_DELETE)
17078 is_initialized = SD_DELETED;
17081 else
17083 /* If the init-declarator isn't initialized and isn't followed by a
17084 `,' or `;', it's not a valid init-declarator. */
17085 if (token->type != CPP_COMMA
17086 && token->type != CPP_SEMICOLON)
17088 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17089 range_for_decl_p = true;
17090 else
17092 if (!maybe_range_for_decl)
17093 cp_parser_error (parser, "expected initializer");
17094 return error_mark_node;
17097 is_initialized = SD_UNINITIALIZED;
17098 initialization_kind = CPP_EOF;
17101 /* Because start_decl has side-effects, we should only call it if we
17102 know we're going ahead. By this point, we know that we cannot
17103 possibly be looking at any other construct. */
17104 cp_parser_commit_to_tentative_parse (parser);
17106 /* Enter the newly declared entry in the symbol table. If we're
17107 processing a declaration in a class-specifier, we wait until
17108 after processing the initializer. */
17109 if (!member_p)
17111 if (parser->in_unbraced_linkage_specification_p)
17112 decl_specifiers->storage_class = sc_extern;
17113 decl = start_decl (declarator, decl_specifiers,
17114 range_for_decl_p? SD_INITIALIZED : is_initialized,
17115 attributes, prefix_attributes, &pushed_scope);
17116 cp_finalize_omp_declare_simd (parser, decl);
17117 /* Adjust location of decl if declarator->id_loc is more appropriate:
17118 set, and decl wasn't merged with another decl, in which case its
17119 location would be different from input_location, and more accurate. */
17120 if (DECL_P (decl)
17121 && declarator->id_loc != UNKNOWN_LOCATION
17122 && DECL_SOURCE_LOCATION (decl) == input_location)
17123 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17125 else if (scope)
17126 /* Enter the SCOPE. That way unqualified names appearing in the
17127 initializer will be looked up in SCOPE. */
17128 pushed_scope = push_scope (scope);
17130 /* Perform deferred access control checks, now that we know in which
17131 SCOPE the declared entity resides. */
17132 if (!member_p && decl)
17134 tree saved_current_function_decl = NULL_TREE;
17136 /* If the entity being declared is a function, pretend that we
17137 are in its scope. If it is a `friend', it may have access to
17138 things that would not otherwise be accessible. */
17139 if (TREE_CODE (decl) == FUNCTION_DECL)
17141 saved_current_function_decl = current_function_decl;
17142 current_function_decl = decl;
17145 /* Perform access checks for template parameters. */
17146 cp_parser_perform_template_parameter_access_checks (checks);
17148 /* Perform the access control checks for the declarator and the
17149 decl-specifiers. */
17150 perform_deferred_access_checks (tf_warning_or_error);
17152 /* Restore the saved value. */
17153 if (TREE_CODE (decl) == FUNCTION_DECL)
17154 current_function_decl = saved_current_function_decl;
17157 /* Parse the initializer. */
17158 initializer = NULL_TREE;
17159 is_direct_init = false;
17160 is_non_constant_init = true;
17161 if (is_initialized)
17163 if (function_declarator_p (declarator))
17165 if (initialization_kind == CPP_EQ)
17166 initializer = cp_parser_pure_specifier (parser);
17167 else
17169 /* If the declaration was erroneous, we don't really
17170 know what the user intended, so just silently
17171 consume the initializer. */
17172 if (decl != error_mark_node)
17173 error_at (tmp_init_loc, "initializer provided for function");
17174 cp_parser_skip_to_closing_parenthesis (parser,
17175 /*recovering=*/true,
17176 /*or_comma=*/false,
17177 /*consume_paren=*/true);
17180 else
17182 /* We want to record the extra mangling scope for in-class
17183 initializers of class members and initializers of static data
17184 member templates. The former involves deferring
17185 parsing of the initializer until end of class as with default
17186 arguments. So right here we only handle the latter. */
17187 if (!member_p && processing_template_decl)
17188 start_lambda_scope (decl);
17189 initializer = cp_parser_initializer (parser,
17190 &is_direct_init,
17191 &is_non_constant_init);
17192 if (!member_p && processing_template_decl)
17193 finish_lambda_scope ();
17194 if (initializer == error_mark_node)
17195 cp_parser_skip_to_end_of_statement (parser);
17199 /* The old parser allows attributes to appear after a parenthesized
17200 initializer. Mark Mitchell proposed removing this functionality
17201 on the GCC mailing lists on 2002-08-13. This parser accepts the
17202 attributes -- but ignores them. */
17203 if (cp_parser_allow_gnu_extensions_p (parser)
17204 && initialization_kind == CPP_OPEN_PAREN)
17205 if (cp_parser_attributes_opt (parser))
17206 warning (OPT_Wattributes,
17207 "attributes after parenthesized initializer ignored");
17209 /* And now complain about a non-function implicit template. */
17210 if (bogus_implicit_tmpl)
17211 error_at (DECL_SOURCE_LOCATION (decl),
17212 "non-function %qD declared as implicit template", decl);
17214 /* For an in-class declaration, use `grokfield' to create the
17215 declaration. */
17216 if (member_p)
17218 if (pushed_scope)
17220 pop_scope (pushed_scope);
17221 pushed_scope = NULL_TREE;
17223 decl = grokfield (declarator, decl_specifiers,
17224 initializer, !is_non_constant_init,
17225 /*asmspec=*/NULL_TREE,
17226 chainon (attributes, prefix_attributes));
17227 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17228 cp_parser_save_default_args (parser, decl);
17229 cp_finalize_omp_declare_simd (parser, decl);
17232 /* Finish processing the declaration. But, skip member
17233 declarations. */
17234 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17236 cp_finish_decl (decl,
17237 initializer, !is_non_constant_init,
17238 asm_specification,
17239 /* If the initializer is in parentheses, then this is
17240 a direct-initialization, which means that an
17241 `explicit' constructor is OK. Otherwise, an
17242 `explicit' constructor cannot be used. */
17243 ((is_direct_init || !is_initialized)
17244 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17246 else if ((cxx_dialect != cxx98) && friend_p
17247 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17248 /* Core issue #226 (C++0x only): A default template-argument
17249 shall not be specified in a friend class template
17250 declaration. */
17251 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17252 /*is_partial=*/false, /*is_friend_decl=*/1);
17254 if (!friend_p && pushed_scope)
17255 pop_scope (pushed_scope);
17257 if (function_declarator_p (declarator)
17258 && parser->fully_implicit_function_template_p)
17260 if (member_p)
17261 decl = finish_fully_implicit_template (parser, decl);
17262 else
17263 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17266 return decl;
17269 /* Parse a declarator.
17271 declarator:
17272 direct-declarator
17273 ptr-operator declarator
17275 abstract-declarator:
17276 ptr-operator abstract-declarator [opt]
17277 direct-abstract-declarator
17279 GNU Extensions:
17281 declarator:
17282 attributes [opt] direct-declarator
17283 attributes [opt] ptr-operator declarator
17285 abstract-declarator:
17286 attributes [opt] ptr-operator abstract-declarator [opt]
17287 attributes [opt] direct-abstract-declarator
17289 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17290 detect constructor, destructor or conversion operators. It is set
17291 to -1 if the declarator is a name, and +1 if it is a
17292 function. Otherwise it is set to zero. Usually you just want to
17293 test for >0, but internally the negative value is used.
17295 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17296 a decl-specifier-seq unless it declares a constructor, destructor,
17297 or conversion. It might seem that we could check this condition in
17298 semantic analysis, rather than parsing, but that makes it difficult
17299 to handle something like `f()'. We want to notice that there are
17300 no decl-specifiers, and therefore realize that this is an
17301 expression, not a declaration.)
17303 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17304 the declarator is a direct-declarator of the form "(...)".
17306 MEMBER_P is true iff this declarator is a member-declarator.
17308 FRIEND_P is true iff this declarator is a friend. */
17310 static cp_declarator *
17311 cp_parser_declarator (cp_parser* parser,
17312 cp_parser_declarator_kind dcl_kind,
17313 int* ctor_dtor_or_conv_p,
17314 bool* parenthesized_p,
17315 bool member_p, bool friend_p)
17317 cp_declarator *declarator;
17318 enum tree_code code;
17319 cp_cv_quals cv_quals;
17320 tree class_type;
17321 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17323 /* Assume this is not a constructor, destructor, or type-conversion
17324 operator. */
17325 if (ctor_dtor_or_conv_p)
17326 *ctor_dtor_or_conv_p = 0;
17328 if (cp_parser_allow_gnu_extensions_p (parser))
17329 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17331 /* Check for the ptr-operator production. */
17332 cp_parser_parse_tentatively (parser);
17333 /* Parse the ptr-operator. */
17334 code = cp_parser_ptr_operator (parser,
17335 &class_type,
17336 &cv_quals,
17337 &std_attributes);
17339 /* If that worked, then we have a ptr-operator. */
17340 if (cp_parser_parse_definitely (parser))
17342 /* If a ptr-operator was found, then this declarator was not
17343 parenthesized. */
17344 if (parenthesized_p)
17345 *parenthesized_p = true;
17346 /* The dependent declarator is optional if we are parsing an
17347 abstract-declarator. */
17348 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17349 cp_parser_parse_tentatively (parser);
17351 /* Parse the dependent declarator. */
17352 declarator = cp_parser_declarator (parser, dcl_kind,
17353 /*ctor_dtor_or_conv_p=*/NULL,
17354 /*parenthesized_p=*/NULL,
17355 /*member_p=*/false,
17356 friend_p);
17358 /* If we are parsing an abstract-declarator, we must handle the
17359 case where the dependent declarator is absent. */
17360 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17361 && !cp_parser_parse_definitely (parser))
17362 declarator = NULL;
17364 declarator = cp_parser_make_indirect_declarator
17365 (code, class_type, cv_quals, declarator, std_attributes);
17367 /* Everything else is a direct-declarator. */
17368 else
17370 if (parenthesized_p)
17371 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17372 CPP_OPEN_PAREN);
17373 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17374 ctor_dtor_or_conv_p,
17375 member_p, friend_p);
17378 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17379 declarator->attributes = gnu_attributes;
17380 return declarator;
17383 /* Parse a direct-declarator or direct-abstract-declarator.
17385 direct-declarator:
17386 declarator-id
17387 direct-declarator ( parameter-declaration-clause )
17388 cv-qualifier-seq [opt]
17389 ref-qualifier [opt]
17390 exception-specification [opt]
17391 direct-declarator [ constant-expression [opt] ]
17392 ( declarator )
17394 direct-abstract-declarator:
17395 direct-abstract-declarator [opt]
17396 ( parameter-declaration-clause )
17397 cv-qualifier-seq [opt]
17398 ref-qualifier [opt]
17399 exception-specification [opt]
17400 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17401 ( abstract-declarator )
17403 Returns a representation of the declarator. DCL_KIND is
17404 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17405 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17406 we are parsing a direct-declarator. It is
17407 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17408 of ambiguity we prefer an abstract declarator, as per
17409 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17410 as for cp_parser_declarator. */
17412 static cp_declarator *
17413 cp_parser_direct_declarator (cp_parser* parser,
17414 cp_parser_declarator_kind dcl_kind,
17415 int* ctor_dtor_or_conv_p,
17416 bool member_p, bool friend_p)
17418 cp_token *token;
17419 cp_declarator *declarator = NULL;
17420 tree scope = NULL_TREE;
17421 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17422 bool saved_in_declarator_p = parser->in_declarator_p;
17423 bool first = true;
17424 tree pushed_scope = NULL_TREE;
17426 while (true)
17428 /* Peek at the next token. */
17429 token = cp_lexer_peek_token (parser->lexer);
17430 if (token->type == CPP_OPEN_PAREN)
17432 /* This is either a parameter-declaration-clause, or a
17433 parenthesized declarator. When we know we are parsing a
17434 named declarator, it must be a parenthesized declarator
17435 if FIRST is true. For instance, `(int)' is a
17436 parameter-declaration-clause, with an omitted
17437 direct-abstract-declarator. But `((*))', is a
17438 parenthesized abstract declarator. Finally, when T is a
17439 template parameter `(T)' is a
17440 parameter-declaration-clause, and not a parenthesized
17441 named declarator.
17443 We first try and parse a parameter-declaration-clause,
17444 and then try a nested declarator (if FIRST is true).
17446 It is not an error for it not to be a
17447 parameter-declaration-clause, even when FIRST is
17448 false. Consider,
17450 int i (int);
17451 int i (3);
17453 The first is the declaration of a function while the
17454 second is the definition of a variable, including its
17455 initializer.
17457 Having seen only the parenthesis, we cannot know which of
17458 these two alternatives should be selected. Even more
17459 complex are examples like:
17461 int i (int (a));
17462 int i (int (3));
17464 The former is a function-declaration; the latter is a
17465 variable initialization.
17467 Thus again, we try a parameter-declaration-clause, and if
17468 that fails, we back out and return. */
17470 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17472 tree params;
17473 bool is_declarator = false;
17475 /* In a member-declarator, the only valid interpretation
17476 of a parenthesis is the start of a
17477 parameter-declaration-clause. (It is invalid to
17478 initialize a static data member with a parenthesized
17479 initializer; only the "=" form of initialization is
17480 permitted.) */
17481 if (!member_p)
17482 cp_parser_parse_tentatively (parser);
17484 /* Consume the `('. */
17485 cp_lexer_consume_token (parser->lexer);
17486 if (first)
17488 /* If this is going to be an abstract declarator, we're
17489 in a declarator and we can't have default args. */
17490 parser->default_arg_ok_p = false;
17491 parser->in_declarator_p = true;
17494 begin_scope (sk_function_parms, NULL_TREE);
17496 /* Parse the parameter-declaration-clause. */
17497 params = cp_parser_parameter_declaration_clause (parser);
17499 /* Consume the `)'. */
17500 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17502 /* If all went well, parse the cv-qualifier-seq,
17503 ref-qualifier and the exception-specification. */
17504 if (member_p || cp_parser_parse_definitely (parser))
17506 cp_cv_quals cv_quals;
17507 cp_virt_specifiers virt_specifiers;
17508 cp_ref_qualifier ref_qual;
17509 tree exception_specification;
17510 tree late_return;
17511 tree attrs;
17512 bool memfn = (member_p || (pushed_scope
17513 && CLASS_TYPE_P (pushed_scope)));
17515 is_declarator = true;
17517 if (ctor_dtor_or_conv_p)
17518 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17519 first = false;
17521 /* Parse the cv-qualifier-seq. */
17522 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17523 /* Parse the ref-qualifier. */
17524 ref_qual = cp_parser_ref_qualifier_opt (parser);
17525 /* And the exception-specification. */
17526 exception_specification
17527 = cp_parser_exception_specification_opt (parser);
17529 attrs = cp_parser_std_attribute_spec_seq (parser);
17531 /* In here, we handle cases where attribute is used after
17532 the function declaration. For example:
17533 void func (int x) __attribute__((vector(..))); */
17534 if (flag_cilkplus
17535 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17537 cp_parser_parse_tentatively (parser);
17538 tree attr = cp_parser_gnu_attributes_opt (parser);
17539 if (cp_lexer_next_token_is_not (parser->lexer,
17540 CPP_SEMICOLON)
17541 && cp_lexer_next_token_is_not (parser->lexer,
17542 CPP_OPEN_BRACE))
17543 cp_parser_abort_tentative_parse (parser);
17544 else if (!cp_parser_parse_definitely (parser))
17546 else
17547 attrs = chainon (attr, attrs);
17549 late_return = (cp_parser_late_return_type_opt
17550 (parser, declarator,
17551 memfn ? cv_quals : -1));
17554 /* Parse the virt-specifier-seq. */
17555 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17557 /* Create the function-declarator. */
17558 declarator = make_call_declarator (declarator,
17559 params,
17560 cv_quals,
17561 virt_specifiers,
17562 ref_qual,
17563 exception_specification,
17564 late_return);
17565 declarator->std_attributes = attrs;
17566 /* Any subsequent parameter lists are to do with
17567 return type, so are not those of the declared
17568 function. */
17569 parser->default_arg_ok_p = false;
17572 /* Remove the function parms from scope. */
17573 pop_bindings_and_leave_scope ();
17575 if (is_declarator)
17576 /* Repeat the main loop. */
17577 continue;
17580 /* If this is the first, we can try a parenthesized
17581 declarator. */
17582 if (first)
17584 bool saved_in_type_id_in_expr_p;
17586 parser->default_arg_ok_p = saved_default_arg_ok_p;
17587 parser->in_declarator_p = saved_in_declarator_p;
17589 /* Consume the `('. */
17590 cp_lexer_consume_token (parser->lexer);
17591 /* Parse the nested declarator. */
17592 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17593 parser->in_type_id_in_expr_p = true;
17594 declarator
17595 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17596 /*parenthesized_p=*/NULL,
17597 member_p, friend_p);
17598 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17599 first = false;
17600 /* Expect a `)'. */
17601 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17602 declarator = cp_error_declarator;
17603 if (declarator == cp_error_declarator)
17604 break;
17606 goto handle_declarator;
17608 /* Otherwise, we must be done. */
17609 else
17610 break;
17612 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17613 && token->type == CPP_OPEN_SQUARE
17614 && !cp_next_tokens_can_be_attribute_p (parser))
17616 /* Parse an array-declarator. */
17617 tree bounds, attrs;
17619 if (ctor_dtor_or_conv_p)
17620 *ctor_dtor_or_conv_p = 0;
17622 first = false;
17623 parser->default_arg_ok_p = false;
17624 parser->in_declarator_p = true;
17625 /* Consume the `['. */
17626 cp_lexer_consume_token (parser->lexer);
17627 /* Peek at the next token. */
17628 token = cp_lexer_peek_token (parser->lexer);
17629 /* If the next token is `]', then there is no
17630 constant-expression. */
17631 if (token->type != CPP_CLOSE_SQUARE)
17633 bool non_constant_p;
17634 bounds
17635 = cp_parser_constant_expression (parser,
17636 /*allow_non_constant=*/true,
17637 &non_constant_p);
17638 if (!non_constant_p)
17639 /* OK */;
17640 else if (error_operand_p (bounds))
17641 /* Already gave an error. */;
17642 else if (!parser->in_function_body
17643 || current_binding_level->kind == sk_function_parms)
17645 /* Normally, the array bound must be an integral constant
17646 expression. However, as an extension, we allow VLAs
17647 in function scopes as long as they aren't part of a
17648 parameter declaration. */
17649 cp_parser_error (parser,
17650 "array bound is not an integer constant");
17651 bounds = error_mark_node;
17653 else if (processing_template_decl
17654 && !type_dependent_expression_p (bounds))
17656 /* Remember this wasn't a constant-expression. */
17657 bounds = build_nop (TREE_TYPE (bounds), bounds);
17658 TREE_SIDE_EFFECTS (bounds) = 1;
17661 else
17662 bounds = NULL_TREE;
17663 /* Look for the closing `]'. */
17664 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17666 declarator = cp_error_declarator;
17667 break;
17670 attrs = cp_parser_std_attribute_spec_seq (parser);
17671 declarator = make_array_declarator (declarator, bounds);
17672 declarator->std_attributes = attrs;
17674 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17677 tree qualifying_scope;
17678 tree unqualified_name;
17679 tree attrs;
17680 special_function_kind sfk;
17681 bool abstract_ok;
17682 bool pack_expansion_p = false;
17683 cp_token *declarator_id_start_token;
17685 /* Parse a declarator-id */
17686 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17687 if (abstract_ok)
17689 cp_parser_parse_tentatively (parser);
17691 /* If we see an ellipsis, we should be looking at a
17692 parameter pack. */
17693 if (token->type == CPP_ELLIPSIS)
17695 /* Consume the `...' */
17696 cp_lexer_consume_token (parser->lexer);
17698 pack_expansion_p = true;
17702 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17703 unqualified_name
17704 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17705 qualifying_scope = parser->scope;
17706 if (abstract_ok)
17708 bool okay = false;
17710 if (!unqualified_name && pack_expansion_p)
17712 /* Check whether an error occurred. */
17713 okay = !cp_parser_error_occurred (parser);
17715 /* We already consumed the ellipsis to mark a
17716 parameter pack, but we have no way to report it,
17717 so abort the tentative parse. We will be exiting
17718 immediately anyway. */
17719 cp_parser_abort_tentative_parse (parser);
17721 else
17722 okay = cp_parser_parse_definitely (parser);
17724 if (!okay)
17725 unqualified_name = error_mark_node;
17726 else if (unqualified_name
17727 && (qualifying_scope
17728 || (!identifier_p (unqualified_name))))
17730 cp_parser_error (parser, "expected unqualified-id");
17731 unqualified_name = error_mark_node;
17735 if (!unqualified_name)
17736 return NULL;
17737 if (unqualified_name == error_mark_node)
17739 declarator = cp_error_declarator;
17740 pack_expansion_p = false;
17741 declarator->parameter_pack_p = false;
17742 break;
17745 attrs = cp_parser_std_attribute_spec_seq (parser);
17747 if (qualifying_scope && at_namespace_scope_p ()
17748 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17750 /* In the declaration of a member of a template class
17751 outside of the class itself, the SCOPE will sometimes
17752 be a TYPENAME_TYPE. For example, given:
17754 template <typename T>
17755 int S<T>::R::i = 3;
17757 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17758 this context, we must resolve S<T>::R to an ordinary
17759 type, rather than a typename type.
17761 The reason we normally avoid resolving TYPENAME_TYPEs
17762 is that a specialization of `S' might render
17763 `S<T>::R' not a type. However, if `S' is
17764 specialized, then this `i' will not be used, so there
17765 is no harm in resolving the types here. */
17766 tree type;
17768 /* Resolve the TYPENAME_TYPE. */
17769 type = resolve_typename_type (qualifying_scope,
17770 /*only_current_p=*/false);
17771 /* If that failed, the declarator is invalid. */
17772 if (TREE_CODE (type) == TYPENAME_TYPE)
17774 if (typedef_variant_p (type))
17775 error_at (declarator_id_start_token->location,
17776 "cannot define member of dependent typedef "
17777 "%qT", type);
17778 else
17779 error_at (declarator_id_start_token->location,
17780 "%<%T::%E%> is not a type",
17781 TYPE_CONTEXT (qualifying_scope),
17782 TYPE_IDENTIFIER (qualifying_scope));
17784 qualifying_scope = type;
17787 sfk = sfk_none;
17789 if (unqualified_name)
17791 tree class_type;
17793 if (qualifying_scope
17794 && CLASS_TYPE_P (qualifying_scope))
17795 class_type = qualifying_scope;
17796 else
17797 class_type = current_class_type;
17799 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17801 tree name_type = TREE_TYPE (unqualified_name);
17802 if (class_type && same_type_p (name_type, class_type))
17804 if (qualifying_scope
17805 && CLASSTYPE_USE_TEMPLATE (name_type))
17807 error_at (declarator_id_start_token->location,
17808 "invalid use of constructor as a template");
17809 inform (declarator_id_start_token->location,
17810 "use %<%T::%D%> instead of %<%T::%D%> to "
17811 "name the constructor in a qualified name",
17812 class_type,
17813 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17814 class_type, name_type);
17815 declarator = cp_error_declarator;
17816 break;
17818 else
17819 unqualified_name = constructor_name (class_type);
17821 else
17823 /* We do not attempt to print the declarator
17824 here because we do not have enough
17825 information about its original syntactic
17826 form. */
17827 cp_parser_error (parser, "invalid declarator");
17828 declarator = cp_error_declarator;
17829 break;
17833 if (class_type)
17835 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17836 sfk = sfk_destructor;
17837 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17838 sfk = sfk_conversion;
17839 else if (/* There's no way to declare a constructor
17840 for an anonymous type, even if the type
17841 got a name for linkage purposes. */
17842 !TYPE_WAS_ANONYMOUS (class_type)
17843 /* Handle correctly (c++/19200):
17845 struct S {
17846 struct T{};
17847 friend void S(T);
17850 and also:
17852 namespace N {
17853 void S();
17856 struct S {
17857 friend void N::S();
17858 }; */
17859 && !(friend_p
17860 && class_type != qualifying_scope)
17861 && constructor_name_p (unqualified_name,
17862 class_type))
17864 unqualified_name = constructor_name (class_type);
17865 sfk = sfk_constructor;
17867 else if (is_overloaded_fn (unqualified_name)
17868 && DECL_CONSTRUCTOR_P (get_first_fn
17869 (unqualified_name)))
17870 sfk = sfk_constructor;
17872 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17873 *ctor_dtor_or_conv_p = -1;
17876 declarator = make_id_declarator (qualifying_scope,
17877 unqualified_name,
17878 sfk);
17879 declarator->std_attributes = attrs;
17880 declarator->id_loc = token->location;
17881 declarator->parameter_pack_p = pack_expansion_p;
17883 if (pack_expansion_p)
17884 maybe_warn_variadic_templates ();
17887 handle_declarator:;
17888 scope = get_scope_of_declarator (declarator);
17889 if (scope)
17891 /* Any names that appear after the declarator-id for a
17892 member are looked up in the containing scope. */
17893 if (at_function_scope_p ())
17895 /* But declarations with qualified-ids can't appear in a
17896 function. */
17897 cp_parser_error (parser, "qualified-id in declaration");
17898 declarator = cp_error_declarator;
17899 break;
17901 pushed_scope = push_scope (scope);
17903 parser->in_declarator_p = true;
17904 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17905 || (declarator && declarator->kind == cdk_id))
17906 /* Default args are only allowed on function
17907 declarations. */
17908 parser->default_arg_ok_p = saved_default_arg_ok_p;
17909 else
17910 parser->default_arg_ok_p = false;
17912 first = false;
17914 /* We're done. */
17915 else
17916 break;
17919 /* For an abstract declarator, we might wind up with nothing at this
17920 point. That's an error; the declarator is not optional. */
17921 if (!declarator)
17922 cp_parser_error (parser, "expected declarator");
17924 /* If we entered a scope, we must exit it now. */
17925 if (pushed_scope)
17926 pop_scope (pushed_scope);
17928 parser->default_arg_ok_p = saved_default_arg_ok_p;
17929 parser->in_declarator_p = saved_in_declarator_p;
17931 return declarator;
17934 /* Parse a ptr-operator.
17936 ptr-operator:
17937 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17938 * cv-qualifier-seq [opt]
17940 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17941 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17943 GNU Extension:
17945 ptr-operator:
17946 & cv-qualifier-seq [opt]
17948 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17949 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17950 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17951 filled in with the TYPE containing the member. *CV_QUALS is
17952 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17953 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17954 Note that the tree codes returned by this function have nothing
17955 to do with the types of trees that will be eventually be created
17956 to represent the pointer or reference type being parsed. They are
17957 just constants with suggestive names. */
17958 static enum tree_code
17959 cp_parser_ptr_operator (cp_parser* parser,
17960 tree* type,
17961 cp_cv_quals *cv_quals,
17962 tree *attributes)
17964 enum tree_code code = ERROR_MARK;
17965 cp_token *token;
17966 tree attrs = NULL_TREE;
17968 /* Assume that it's not a pointer-to-member. */
17969 *type = NULL_TREE;
17970 /* And that there are no cv-qualifiers. */
17971 *cv_quals = TYPE_UNQUALIFIED;
17973 /* Peek at the next token. */
17974 token = cp_lexer_peek_token (parser->lexer);
17976 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17977 if (token->type == CPP_MULT)
17978 code = INDIRECT_REF;
17979 else if (token->type == CPP_AND)
17980 code = ADDR_EXPR;
17981 else if ((cxx_dialect != cxx98) &&
17982 token->type == CPP_AND_AND) /* C++0x only */
17983 code = NON_LVALUE_EXPR;
17985 if (code != ERROR_MARK)
17987 /* Consume the `*', `&' or `&&'. */
17988 cp_lexer_consume_token (parser->lexer);
17990 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17991 `&', if we are allowing GNU extensions. (The only qualifier
17992 that can legally appear after `&' is `restrict', but that is
17993 enforced during semantic analysis. */
17994 if (code == INDIRECT_REF
17995 || cp_parser_allow_gnu_extensions_p (parser))
17996 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17998 attrs = cp_parser_std_attribute_spec_seq (parser);
17999 if (attributes != NULL)
18000 *attributes = attrs;
18002 else
18004 /* Try the pointer-to-member case. */
18005 cp_parser_parse_tentatively (parser);
18006 /* Look for the optional `::' operator. */
18007 cp_parser_global_scope_opt (parser,
18008 /*current_scope_valid_p=*/false);
18009 /* Look for the nested-name specifier. */
18010 token = cp_lexer_peek_token (parser->lexer);
18011 cp_parser_nested_name_specifier (parser,
18012 /*typename_keyword_p=*/false,
18013 /*check_dependency_p=*/true,
18014 /*type_p=*/false,
18015 /*is_declaration=*/false);
18016 /* If we found it, and the next token is a `*', then we are
18017 indeed looking at a pointer-to-member operator. */
18018 if (!cp_parser_error_occurred (parser)
18019 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18021 /* Indicate that the `*' operator was used. */
18022 code = INDIRECT_REF;
18024 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18025 error_at (token->location, "%qD is a namespace", parser->scope);
18026 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18027 error_at (token->location, "cannot form pointer to member of "
18028 "non-class %q#T", parser->scope);
18029 else
18031 /* The type of which the member is a member is given by the
18032 current SCOPE. */
18033 *type = parser->scope;
18034 /* The next name will not be qualified. */
18035 parser->scope = NULL_TREE;
18036 parser->qualifying_scope = NULL_TREE;
18037 parser->object_scope = NULL_TREE;
18038 /* Look for optional c++11 attributes. */
18039 attrs = cp_parser_std_attribute_spec_seq (parser);
18040 if (attributes != NULL)
18041 *attributes = attrs;
18042 /* Look for the optional cv-qualifier-seq. */
18043 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18046 /* If that didn't work we don't have a ptr-operator. */
18047 if (!cp_parser_parse_definitely (parser))
18048 cp_parser_error (parser, "expected ptr-operator");
18051 return code;
18054 /* Parse an (optional) cv-qualifier-seq.
18056 cv-qualifier-seq:
18057 cv-qualifier cv-qualifier-seq [opt]
18059 cv-qualifier:
18060 const
18061 volatile
18063 GNU Extension:
18065 cv-qualifier:
18066 __restrict__
18068 Returns a bitmask representing the cv-qualifiers. */
18070 static cp_cv_quals
18071 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18073 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18075 while (true)
18077 cp_token *token;
18078 cp_cv_quals cv_qualifier;
18080 /* Peek at the next token. */
18081 token = cp_lexer_peek_token (parser->lexer);
18082 /* See if it's a cv-qualifier. */
18083 switch (token->keyword)
18085 case RID_CONST:
18086 cv_qualifier = TYPE_QUAL_CONST;
18087 break;
18089 case RID_VOLATILE:
18090 cv_qualifier = TYPE_QUAL_VOLATILE;
18091 break;
18093 case RID_RESTRICT:
18094 cv_qualifier = TYPE_QUAL_RESTRICT;
18095 break;
18097 default:
18098 cv_qualifier = TYPE_UNQUALIFIED;
18099 break;
18102 if (!cv_qualifier)
18103 break;
18105 if (cv_quals & cv_qualifier)
18107 error_at (token->location, "duplicate cv-qualifier");
18108 cp_lexer_purge_token (parser->lexer);
18110 else
18112 cp_lexer_consume_token (parser->lexer);
18113 cv_quals |= cv_qualifier;
18117 return cv_quals;
18120 /* Parse an (optional) ref-qualifier
18122 ref-qualifier:
18126 Returns cp_ref_qualifier representing ref-qualifier. */
18128 static cp_ref_qualifier
18129 cp_parser_ref_qualifier_opt (cp_parser* parser)
18131 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18133 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18134 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18135 return ref_qual;
18137 while (true)
18139 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18140 cp_token *token = cp_lexer_peek_token (parser->lexer);
18142 switch (token->type)
18144 case CPP_AND:
18145 curr_ref_qual = REF_QUAL_LVALUE;
18146 break;
18148 case CPP_AND_AND:
18149 curr_ref_qual = REF_QUAL_RVALUE;
18150 break;
18152 default:
18153 curr_ref_qual = REF_QUAL_NONE;
18154 break;
18157 if (!curr_ref_qual)
18158 break;
18159 else if (ref_qual)
18161 error_at (token->location, "multiple ref-qualifiers");
18162 cp_lexer_purge_token (parser->lexer);
18164 else
18166 ref_qual = curr_ref_qual;
18167 cp_lexer_consume_token (parser->lexer);
18171 return ref_qual;
18174 /* Parse an (optional) virt-specifier-seq.
18176 virt-specifier-seq:
18177 virt-specifier virt-specifier-seq [opt]
18179 virt-specifier:
18180 override
18181 final
18183 Returns a bitmask representing the virt-specifiers. */
18185 static cp_virt_specifiers
18186 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18188 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18190 while (true)
18192 cp_token *token;
18193 cp_virt_specifiers virt_specifier;
18195 /* Peek at the next token. */
18196 token = cp_lexer_peek_token (parser->lexer);
18197 /* See if it's a virt-specifier-qualifier. */
18198 if (token->type != CPP_NAME)
18199 break;
18200 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18202 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18203 virt_specifier = VIRT_SPEC_OVERRIDE;
18205 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18207 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18208 virt_specifier = VIRT_SPEC_FINAL;
18210 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18212 virt_specifier = VIRT_SPEC_FINAL;
18214 else
18215 break;
18217 if (virt_specifiers & virt_specifier)
18219 error_at (token->location, "duplicate virt-specifier");
18220 cp_lexer_purge_token (parser->lexer);
18222 else
18224 cp_lexer_consume_token (parser->lexer);
18225 virt_specifiers |= virt_specifier;
18228 return virt_specifiers;
18231 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18232 is in scope even though it isn't real. */
18234 void
18235 inject_this_parameter (tree ctype, cp_cv_quals quals)
18237 tree this_parm;
18239 if (current_class_ptr)
18241 /* We don't clear this between NSDMIs. Is it already what we want? */
18242 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18243 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18244 && cp_type_quals (type) == quals)
18245 return;
18248 this_parm = build_this_parm (ctype, quals);
18249 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18250 current_class_ptr = NULL_TREE;
18251 current_class_ref
18252 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18253 current_class_ptr = this_parm;
18256 /* Return true iff our current scope is a non-static data member
18257 initializer. */
18259 bool
18260 parsing_nsdmi (void)
18262 /* We recognize NSDMI context by the context-less 'this' pointer set up
18263 by the function above. */
18264 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18265 return true;
18266 return false;
18269 /* Parse a late-specified return type, if any. This is not a separate
18270 non-terminal, but part of a function declarator, which looks like
18272 -> trailing-type-specifier-seq abstract-declarator(opt)
18274 Returns the type indicated by the type-id.
18276 In addition to this this parses any queued up omp declare simd
18277 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18279 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18280 function. */
18282 static tree
18283 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18284 cp_cv_quals quals)
18286 cp_token *token;
18287 tree type = NULL_TREE;
18288 bool declare_simd_p = (parser->omp_declare_simd
18289 && declarator
18290 && declarator->kind == cdk_id);
18292 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18293 && declarator && declarator->kind == cdk_id);
18295 /* Peek at the next token. */
18296 token = cp_lexer_peek_token (parser->lexer);
18297 /* A late-specified return type is indicated by an initial '->'. */
18298 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18299 return NULL_TREE;
18301 tree save_ccp = current_class_ptr;
18302 tree save_ccr = current_class_ref;
18303 if (quals >= 0)
18305 /* DR 1207: 'this' is in scope in the trailing return type. */
18306 inject_this_parameter (current_class_type, quals);
18309 if (token->type == CPP_DEREF)
18311 /* Consume the ->. */
18312 cp_lexer_consume_token (parser->lexer);
18314 type = cp_parser_trailing_type_id (parser);
18317 if (cilk_simd_fn_vector_p)
18318 declarator->std_attributes
18319 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18320 declarator->std_attributes);
18321 if (declare_simd_p)
18322 declarator->std_attributes
18323 = cp_parser_late_parsing_omp_declare_simd (parser,
18324 declarator->std_attributes);
18326 if (quals >= 0)
18328 current_class_ptr = save_ccp;
18329 current_class_ref = save_ccr;
18332 return type;
18335 /* Parse a declarator-id.
18337 declarator-id:
18338 id-expression
18339 :: [opt] nested-name-specifier [opt] type-name
18341 In the `id-expression' case, the value returned is as for
18342 cp_parser_id_expression if the id-expression was an unqualified-id.
18343 If the id-expression was a qualified-id, then a SCOPE_REF is
18344 returned. The first operand is the scope (either a NAMESPACE_DECL
18345 or TREE_TYPE), but the second is still just a representation of an
18346 unqualified-id. */
18348 static tree
18349 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18351 tree id;
18352 /* The expression must be an id-expression. Assume that qualified
18353 names are the names of types so that:
18355 template <class T>
18356 int S<T>::R::i = 3;
18358 will work; we must treat `S<T>::R' as the name of a type.
18359 Similarly, assume that qualified names are templates, where
18360 required, so that:
18362 template <class T>
18363 int S<T>::R<T>::i = 3;
18365 will work, too. */
18366 id = cp_parser_id_expression (parser,
18367 /*template_keyword_p=*/false,
18368 /*check_dependency_p=*/false,
18369 /*template_p=*/NULL,
18370 /*declarator_p=*/true,
18371 optional_p);
18372 if (id && BASELINK_P (id))
18373 id = BASELINK_FUNCTIONS (id);
18374 return id;
18377 /* Parse a type-id.
18379 type-id:
18380 type-specifier-seq abstract-declarator [opt]
18382 Returns the TYPE specified. */
18384 static tree
18385 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18386 bool is_trailing_return)
18388 cp_decl_specifier_seq type_specifier_seq;
18389 cp_declarator *abstract_declarator;
18391 /* Parse the type-specifier-seq. */
18392 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18393 is_trailing_return,
18394 &type_specifier_seq);
18395 if (type_specifier_seq.type == error_mark_node)
18396 return error_mark_node;
18398 /* There might or might not be an abstract declarator. */
18399 cp_parser_parse_tentatively (parser);
18400 /* Look for the declarator. */
18401 abstract_declarator
18402 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18403 /*parenthesized_p=*/NULL,
18404 /*member_p=*/false,
18405 /*friend_p=*/false);
18406 /* Check to see if there really was a declarator. */
18407 if (!cp_parser_parse_definitely (parser))
18408 abstract_declarator = NULL;
18410 if (type_specifier_seq.type
18411 /* None of the valid uses of 'auto' in C++14 involve the type-id
18412 nonterminal, but it is valid in a trailing-return-type. */
18413 && !(cxx_dialect >= cxx14 && is_trailing_return)
18414 && type_uses_auto (type_specifier_seq.type))
18416 /* A type-id with type 'auto' is only ok if the abstract declarator
18417 is a function declarator with a late-specified return type. */
18418 if (abstract_declarator
18419 && abstract_declarator->kind == cdk_function
18420 && abstract_declarator->u.function.late_return_type)
18421 /* OK */;
18422 else
18424 error ("invalid use of %<auto%>");
18425 return error_mark_node;
18429 return groktypename (&type_specifier_seq, abstract_declarator,
18430 is_template_arg);
18433 static tree cp_parser_type_id (cp_parser *parser)
18435 return cp_parser_type_id_1 (parser, false, false);
18438 static tree cp_parser_template_type_arg (cp_parser *parser)
18440 tree r;
18441 const char *saved_message = parser->type_definition_forbidden_message;
18442 parser->type_definition_forbidden_message
18443 = G_("types may not be defined in template arguments");
18444 r = cp_parser_type_id_1 (parser, true, false);
18445 parser->type_definition_forbidden_message = saved_message;
18446 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18448 error ("invalid use of %<auto%> in template argument");
18449 r = error_mark_node;
18451 return r;
18454 static tree cp_parser_trailing_type_id (cp_parser *parser)
18456 return cp_parser_type_id_1 (parser, false, true);
18459 /* Parse a type-specifier-seq.
18461 type-specifier-seq:
18462 type-specifier type-specifier-seq [opt]
18464 GNU extension:
18466 type-specifier-seq:
18467 attributes type-specifier-seq [opt]
18469 If IS_DECLARATION is true, we are at the start of a "condition" or
18470 exception-declaration, so we might be followed by a declarator-id.
18472 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18473 i.e. we've just seen "->".
18475 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18477 static void
18478 cp_parser_type_specifier_seq (cp_parser* parser,
18479 bool is_declaration,
18480 bool is_trailing_return,
18481 cp_decl_specifier_seq *type_specifier_seq)
18483 bool seen_type_specifier = false;
18484 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18485 cp_token *start_token = NULL;
18487 /* Clear the TYPE_SPECIFIER_SEQ. */
18488 clear_decl_specs (type_specifier_seq);
18490 /* In the context of a trailing return type, enum E { } is an
18491 elaborated-type-specifier followed by a function-body, not an
18492 enum-specifier. */
18493 if (is_trailing_return)
18494 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18496 /* Parse the type-specifiers and attributes. */
18497 while (true)
18499 tree type_specifier;
18500 bool is_cv_qualifier;
18502 /* Check for attributes first. */
18503 if (cp_next_tokens_can_be_attribute_p (parser))
18505 type_specifier_seq->attributes =
18506 chainon (type_specifier_seq->attributes,
18507 cp_parser_attributes_opt (parser));
18508 continue;
18511 /* record the token of the beginning of the type specifier seq,
18512 for error reporting purposes*/
18513 if (!start_token)
18514 start_token = cp_lexer_peek_token (parser->lexer);
18516 /* Look for the type-specifier. */
18517 type_specifier = cp_parser_type_specifier (parser,
18518 flags,
18519 type_specifier_seq,
18520 /*is_declaration=*/false,
18521 NULL,
18522 &is_cv_qualifier);
18523 if (!type_specifier)
18525 /* If the first type-specifier could not be found, this is not a
18526 type-specifier-seq at all. */
18527 if (!seen_type_specifier)
18529 /* Set in_declarator_p to avoid skipping to the semicolon. */
18530 int in_decl = parser->in_declarator_p;
18531 parser->in_declarator_p = true;
18533 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18534 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18535 cp_parser_error (parser, "expected type-specifier");
18537 parser->in_declarator_p = in_decl;
18539 type_specifier_seq->type = error_mark_node;
18540 return;
18542 /* If subsequent type-specifiers could not be found, the
18543 type-specifier-seq is complete. */
18544 break;
18547 seen_type_specifier = true;
18548 /* The standard says that a condition can be:
18550 type-specifier-seq declarator = assignment-expression
18552 However, given:
18554 struct S {};
18555 if (int S = ...)
18557 we should treat the "S" as a declarator, not as a
18558 type-specifier. The standard doesn't say that explicitly for
18559 type-specifier-seq, but it does say that for
18560 decl-specifier-seq in an ordinary declaration. Perhaps it
18561 would be clearer just to allow a decl-specifier-seq here, and
18562 then add a semantic restriction that if any decl-specifiers
18563 that are not type-specifiers appear, the program is invalid. */
18564 if (is_declaration && !is_cv_qualifier)
18565 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18569 /* Return whether the function currently being declared has an associated
18570 template parameter list. */
18572 static bool
18573 function_being_declared_is_template_p (cp_parser* parser)
18575 if (!current_template_parms || processing_template_parmlist)
18576 return false;
18578 if (parser->implicit_template_scope)
18579 return true;
18581 if (at_class_scope_p ()
18582 && TYPE_BEING_DEFINED (current_class_type))
18583 return parser->num_template_parameter_lists != 0;
18585 return ((int) parser->num_template_parameter_lists > template_class_depth
18586 (current_class_type));
18589 /* Parse a parameter-declaration-clause.
18591 parameter-declaration-clause:
18592 parameter-declaration-list [opt] ... [opt]
18593 parameter-declaration-list , ...
18595 Returns a representation for the parameter declarations. A return
18596 value of NULL indicates a parameter-declaration-clause consisting
18597 only of an ellipsis. */
18599 static tree
18600 cp_parser_parameter_declaration_clause (cp_parser* parser)
18602 tree parameters;
18603 cp_token *token;
18604 bool ellipsis_p;
18605 bool is_error;
18607 struct cleanup {
18608 cp_parser* parser;
18609 int auto_is_implicit_function_template_parm_p;
18610 ~cleanup() {
18611 parser->auto_is_implicit_function_template_parm_p
18612 = auto_is_implicit_function_template_parm_p;
18614 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18616 (void) cleanup;
18618 if (!processing_specialization
18619 && !processing_template_parmlist
18620 && !processing_explicit_instantiation)
18621 if (!current_function_decl
18622 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18623 parser->auto_is_implicit_function_template_parm_p = true;
18625 /* Peek at the next token. */
18626 token = cp_lexer_peek_token (parser->lexer);
18627 /* Check for trivial parameter-declaration-clauses. */
18628 if (token->type == CPP_ELLIPSIS)
18630 /* Consume the `...' token. */
18631 cp_lexer_consume_token (parser->lexer);
18632 return NULL_TREE;
18634 else if (token->type == CPP_CLOSE_PAREN)
18635 /* There are no parameters. */
18637 #ifndef NO_IMPLICIT_EXTERN_C
18638 if (in_system_header_at (input_location)
18639 && current_class_type == NULL
18640 && current_lang_name == lang_name_c)
18641 return NULL_TREE;
18642 else
18643 #endif
18644 return void_list_node;
18646 /* Check for `(void)', too, which is a special case. */
18647 else if (token->keyword == RID_VOID
18648 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18649 == CPP_CLOSE_PAREN))
18651 /* Consume the `void' token. */
18652 cp_lexer_consume_token (parser->lexer);
18653 /* There are no parameters. */
18654 return void_list_node;
18657 /* Parse the parameter-declaration-list. */
18658 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18659 /* If a parse error occurred while parsing the
18660 parameter-declaration-list, then the entire
18661 parameter-declaration-clause is erroneous. */
18662 if (is_error)
18663 return NULL;
18665 /* Peek at the next token. */
18666 token = cp_lexer_peek_token (parser->lexer);
18667 /* If it's a `,', the clause should terminate with an ellipsis. */
18668 if (token->type == CPP_COMMA)
18670 /* Consume the `,'. */
18671 cp_lexer_consume_token (parser->lexer);
18672 /* Expect an ellipsis. */
18673 ellipsis_p
18674 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18676 /* It might also be `...' if the optional trailing `,' was
18677 omitted. */
18678 else if (token->type == CPP_ELLIPSIS)
18680 /* Consume the `...' token. */
18681 cp_lexer_consume_token (parser->lexer);
18682 /* And remember that we saw it. */
18683 ellipsis_p = true;
18685 else
18686 ellipsis_p = false;
18688 /* Finish the parameter list. */
18689 if (!ellipsis_p)
18690 parameters = chainon (parameters, void_list_node);
18692 return parameters;
18695 /* Parse a parameter-declaration-list.
18697 parameter-declaration-list:
18698 parameter-declaration
18699 parameter-declaration-list , parameter-declaration
18701 Returns a representation of the parameter-declaration-list, as for
18702 cp_parser_parameter_declaration_clause. However, the
18703 `void_list_node' is never appended to the list. Upon return,
18704 *IS_ERROR will be true iff an error occurred. */
18706 static tree
18707 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18709 tree parameters = NULL_TREE;
18710 tree *tail = &parameters;
18711 bool saved_in_unbraced_linkage_specification_p;
18712 int index = 0;
18714 /* Assume all will go well. */
18715 *is_error = false;
18716 /* The special considerations that apply to a function within an
18717 unbraced linkage specifications do not apply to the parameters
18718 to the function. */
18719 saved_in_unbraced_linkage_specification_p
18720 = parser->in_unbraced_linkage_specification_p;
18721 parser->in_unbraced_linkage_specification_p = false;
18723 /* Look for more parameters. */
18724 while (true)
18726 cp_parameter_declarator *parameter;
18727 tree decl = error_mark_node;
18728 bool parenthesized_p = false;
18729 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18730 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18731 (current_template_parms)) : 0);
18733 /* Parse the parameter. */
18734 parameter
18735 = cp_parser_parameter_declaration (parser,
18736 /*template_parm_p=*/false,
18737 &parenthesized_p);
18739 /* We don't know yet if the enclosing context is deprecated, so wait
18740 and warn in grokparms if appropriate. */
18741 deprecated_state = DEPRECATED_SUPPRESS;
18743 if (parameter)
18745 /* If a function parameter pack was specified and an implicit template
18746 parameter was introduced during cp_parser_parameter_declaration,
18747 change any implicit parameters introduced into packs. */
18748 if (parser->implicit_template_parms
18749 && parameter->declarator
18750 && parameter->declarator->parameter_pack_p)
18752 int latest_template_parm_idx = TREE_VEC_LENGTH
18753 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18755 if (latest_template_parm_idx != template_parm_idx)
18756 parameter->decl_specifiers.type = convert_generic_types_to_packs
18757 (parameter->decl_specifiers.type,
18758 template_parm_idx, latest_template_parm_idx);
18761 decl = grokdeclarator (parameter->declarator,
18762 &parameter->decl_specifiers,
18763 PARM,
18764 parameter->default_argument != NULL_TREE,
18765 &parameter->decl_specifiers.attributes);
18768 deprecated_state = DEPRECATED_NORMAL;
18770 /* If a parse error occurred parsing the parameter declaration,
18771 then the entire parameter-declaration-list is erroneous. */
18772 if (decl == error_mark_node)
18774 *is_error = true;
18775 parameters = error_mark_node;
18776 break;
18779 if (parameter->decl_specifiers.attributes)
18780 cplus_decl_attributes (&decl,
18781 parameter->decl_specifiers.attributes,
18783 if (DECL_NAME (decl))
18784 decl = pushdecl (decl);
18786 if (decl != error_mark_node)
18788 retrofit_lang_decl (decl);
18789 DECL_PARM_INDEX (decl) = ++index;
18790 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18793 /* Add the new parameter to the list. */
18794 *tail = build_tree_list (parameter->default_argument, decl);
18795 tail = &TREE_CHAIN (*tail);
18797 /* Peek at the next token. */
18798 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18799 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18800 /* These are for Objective-C++ */
18801 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18802 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18803 /* The parameter-declaration-list is complete. */
18804 break;
18805 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18807 cp_token *token;
18809 /* Peek at the next token. */
18810 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18811 /* If it's an ellipsis, then the list is complete. */
18812 if (token->type == CPP_ELLIPSIS)
18813 break;
18814 /* Otherwise, there must be more parameters. Consume the
18815 `,'. */
18816 cp_lexer_consume_token (parser->lexer);
18817 /* When parsing something like:
18819 int i(float f, double d)
18821 we can tell after seeing the declaration for "f" that we
18822 are not looking at an initialization of a variable "i",
18823 but rather at the declaration of a function "i".
18825 Due to the fact that the parsing of template arguments
18826 (as specified to a template-id) requires backtracking we
18827 cannot use this technique when inside a template argument
18828 list. */
18829 if (!parser->in_template_argument_list_p
18830 && !parser->in_type_id_in_expr_p
18831 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18832 /* However, a parameter-declaration of the form
18833 "float(f)" (which is a valid declaration of a
18834 parameter "f") can also be interpreted as an
18835 expression (the conversion of "f" to "float"). */
18836 && !parenthesized_p)
18837 cp_parser_commit_to_tentative_parse (parser);
18839 else
18841 cp_parser_error (parser, "expected %<,%> or %<...%>");
18842 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18843 cp_parser_skip_to_closing_parenthesis (parser,
18844 /*recovering=*/true,
18845 /*or_comma=*/false,
18846 /*consume_paren=*/false);
18847 break;
18851 parser->in_unbraced_linkage_specification_p
18852 = saved_in_unbraced_linkage_specification_p;
18854 /* Reset implicit_template_scope if we are about to leave the function
18855 parameter list that introduced it. Note that for out-of-line member
18856 definitions, there will be one or more class scopes before we get to
18857 the template parameter scope. */
18859 if (cp_binding_level *its = parser->implicit_template_scope)
18860 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18862 while (maybe_its->kind == sk_class)
18863 maybe_its = maybe_its->level_chain;
18864 if (maybe_its == its)
18866 parser->implicit_template_parms = 0;
18867 parser->implicit_template_scope = 0;
18871 return parameters;
18874 /* Parse a parameter declaration.
18876 parameter-declaration:
18877 decl-specifier-seq ... [opt] declarator
18878 decl-specifier-seq declarator = assignment-expression
18879 decl-specifier-seq ... [opt] abstract-declarator [opt]
18880 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18882 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18883 declares a template parameter. (In that case, a non-nested `>'
18884 token encountered during the parsing of the assignment-expression
18885 is not interpreted as a greater-than operator.)
18887 Returns a representation of the parameter, or NULL if an error
18888 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18889 true iff the declarator is of the form "(p)". */
18891 static cp_parameter_declarator *
18892 cp_parser_parameter_declaration (cp_parser *parser,
18893 bool template_parm_p,
18894 bool *parenthesized_p)
18896 int declares_class_or_enum;
18897 cp_decl_specifier_seq decl_specifiers;
18898 cp_declarator *declarator;
18899 tree default_argument;
18900 cp_token *token = NULL, *declarator_token_start = NULL;
18901 const char *saved_message;
18903 /* In a template parameter, `>' is not an operator.
18905 [temp.param]
18907 When parsing a default template-argument for a non-type
18908 template-parameter, the first non-nested `>' is taken as the end
18909 of the template parameter-list rather than a greater-than
18910 operator. */
18912 /* Type definitions may not appear in parameter types. */
18913 saved_message = parser->type_definition_forbidden_message;
18914 parser->type_definition_forbidden_message
18915 = G_("types may not be defined in parameter types");
18917 /* Parse the declaration-specifiers. */
18918 cp_parser_decl_specifier_seq (parser,
18919 CP_PARSER_FLAGS_NONE,
18920 &decl_specifiers,
18921 &declares_class_or_enum);
18923 /* Complain about missing 'typename' or other invalid type names. */
18924 if (!decl_specifiers.any_type_specifiers_p
18925 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18926 decl_specifiers.type = error_mark_node;
18928 /* If an error occurred, there's no reason to attempt to parse the
18929 rest of the declaration. */
18930 if (cp_parser_error_occurred (parser))
18932 parser->type_definition_forbidden_message = saved_message;
18933 return NULL;
18936 /* Peek at the next token. */
18937 token = cp_lexer_peek_token (parser->lexer);
18939 /* If the next token is a `)', `,', `=', `>', or `...', then there
18940 is no declarator. However, when variadic templates are enabled,
18941 there may be a declarator following `...'. */
18942 if (token->type == CPP_CLOSE_PAREN
18943 || token->type == CPP_COMMA
18944 || token->type == CPP_EQ
18945 || token->type == CPP_GREATER)
18947 declarator = NULL;
18948 if (parenthesized_p)
18949 *parenthesized_p = false;
18951 /* Otherwise, there should be a declarator. */
18952 else
18954 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18955 parser->default_arg_ok_p = false;
18957 /* After seeing a decl-specifier-seq, if the next token is not a
18958 "(", there is no possibility that the code is a valid
18959 expression. Therefore, if parsing tentatively, we commit at
18960 this point. */
18961 if (!parser->in_template_argument_list_p
18962 /* In an expression context, having seen:
18964 (int((char ...
18966 we cannot be sure whether we are looking at a
18967 function-type (taking a "char" as a parameter) or a cast
18968 of some object of type "char" to "int". */
18969 && !parser->in_type_id_in_expr_p
18970 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18971 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18972 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18973 cp_parser_commit_to_tentative_parse (parser);
18974 /* Parse the declarator. */
18975 declarator_token_start = token;
18976 declarator = cp_parser_declarator (parser,
18977 CP_PARSER_DECLARATOR_EITHER,
18978 /*ctor_dtor_or_conv_p=*/NULL,
18979 parenthesized_p,
18980 /*member_p=*/false,
18981 /*friend_p=*/false);
18982 parser->default_arg_ok_p = saved_default_arg_ok_p;
18983 /* After the declarator, allow more attributes. */
18984 decl_specifiers.attributes
18985 = chainon (decl_specifiers.attributes,
18986 cp_parser_attributes_opt (parser));
18989 /* If the next token is an ellipsis, and we have not seen a
18990 declarator name, and the type of the declarator contains parameter
18991 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18992 a parameter pack expansion expression. Otherwise, leave the
18993 ellipsis for a C-style variadic function. */
18994 token = cp_lexer_peek_token (parser->lexer);
18995 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18997 tree type = decl_specifiers.type;
18999 if (type && DECL_P (type))
19000 type = TREE_TYPE (type);
19002 if (type
19003 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19004 && declarator_can_be_parameter_pack (declarator)
19005 && (!declarator || !declarator->parameter_pack_p)
19006 && uses_parameter_packs (type))
19008 /* Consume the `...'. */
19009 cp_lexer_consume_token (parser->lexer);
19010 maybe_warn_variadic_templates ();
19012 /* Build a pack expansion type */
19013 if (declarator)
19014 declarator->parameter_pack_p = true;
19015 else
19016 decl_specifiers.type = make_pack_expansion (type);
19020 /* The restriction on defining new types applies only to the type
19021 of the parameter, not to the default argument. */
19022 parser->type_definition_forbidden_message = saved_message;
19024 /* If the next token is `=', then process a default argument. */
19025 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19027 token = cp_lexer_peek_token (parser->lexer);
19028 /* If we are defining a class, then the tokens that make up the
19029 default argument must be saved and processed later. */
19030 if (!template_parm_p && at_class_scope_p ()
19031 && TYPE_BEING_DEFINED (current_class_type)
19032 && !LAMBDA_TYPE_P (current_class_type))
19033 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19034 /* Outside of a class definition, we can just parse the
19035 assignment-expression. */
19036 else
19037 default_argument
19038 = cp_parser_default_argument (parser, template_parm_p);
19040 if (!parser->default_arg_ok_p)
19042 if (flag_permissive)
19043 warning (0, "deprecated use of default argument for parameter of non-function");
19044 else
19046 error_at (token->location,
19047 "default arguments are only "
19048 "permitted for function parameters");
19049 default_argument = NULL_TREE;
19052 else if ((declarator && declarator->parameter_pack_p)
19053 || (decl_specifiers.type
19054 && PACK_EXPANSION_P (decl_specifiers.type)))
19056 /* Find the name of the parameter pack. */
19057 cp_declarator *id_declarator = declarator;
19058 while (id_declarator && id_declarator->kind != cdk_id)
19059 id_declarator = id_declarator->declarator;
19061 if (id_declarator && id_declarator->kind == cdk_id)
19062 error_at (declarator_token_start->location,
19063 template_parm_p
19064 ? G_("template parameter pack %qD "
19065 "cannot have a default argument")
19066 : G_("parameter pack %qD cannot have "
19067 "a default argument"),
19068 id_declarator->u.id.unqualified_name);
19069 else
19070 error_at (declarator_token_start->location,
19071 template_parm_p
19072 ? G_("template parameter pack cannot have "
19073 "a default argument")
19074 : G_("parameter pack cannot have a "
19075 "default argument"));
19077 default_argument = NULL_TREE;
19080 else
19081 default_argument = NULL_TREE;
19083 return make_parameter_declarator (&decl_specifiers,
19084 declarator,
19085 default_argument);
19088 /* Parse a default argument and return it.
19090 TEMPLATE_PARM_P is true if this is a default argument for a
19091 non-type template parameter. */
19092 static tree
19093 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19095 tree default_argument = NULL_TREE;
19096 bool saved_greater_than_is_operator_p;
19097 bool saved_local_variables_forbidden_p;
19098 bool non_constant_p, is_direct_init;
19100 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19101 set correctly. */
19102 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19103 parser->greater_than_is_operator_p = !template_parm_p;
19104 /* Local variable names (and the `this' keyword) may not
19105 appear in a default argument. */
19106 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19107 parser->local_variables_forbidden_p = true;
19108 /* Parse the assignment-expression. */
19109 if (template_parm_p)
19110 push_deferring_access_checks (dk_no_deferred);
19111 tree saved_class_ptr = NULL_TREE;
19112 tree saved_class_ref = NULL_TREE;
19113 /* The "this" pointer is not valid in a default argument. */
19114 if (cfun)
19116 saved_class_ptr = current_class_ptr;
19117 cp_function_chain->x_current_class_ptr = NULL_TREE;
19118 saved_class_ref = current_class_ref;
19119 cp_function_chain->x_current_class_ref = NULL_TREE;
19121 default_argument
19122 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19123 /* Restore the "this" pointer. */
19124 if (cfun)
19126 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19127 cp_function_chain->x_current_class_ref = saved_class_ref;
19129 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19130 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19131 if (template_parm_p)
19132 pop_deferring_access_checks ();
19133 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19134 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19136 return default_argument;
19139 /* Parse a function-body.
19141 function-body:
19142 compound_statement */
19144 static void
19145 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19147 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19150 /* Parse a ctor-initializer-opt followed by a function-body. Return
19151 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19152 is true we are parsing a function-try-block. */
19154 static bool
19155 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19156 bool in_function_try_block)
19158 tree body, list;
19159 bool ctor_initializer_p;
19160 const bool check_body_p =
19161 DECL_CONSTRUCTOR_P (current_function_decl)
19162 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19163 tree last = NULL;
19165 /* Begin the function body. */
19166 body = begin_function_body ();
19167 /* Parse the optional ctor-initializer. */
19168 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19170 /* If we're parsing a constexpr constructor definition, we need
19171 to check that the constructor body is indeed empty. However,
19172 before we get to cp_parser_function_body lot of junk has been
19173 generated, so we can't just check that we have an empty block.
19174 Rather we take a snapshot of the outermost block, and check whether
19175 cp_parser_function_body changed its state. */
19176 if (check_body_p)
19178 list = cur_stmt_list;
19179 if (STATEMENT_LIST_TAIL (list))
19180 last = STATEMENT_LIST_TAIL (list)->stmt;
19182 /* Parse the function-body. */
19183 cp_parser_function_body (parser, in_function_try_block);
19184 if (check_body_p)
19185 check_constexpr_ctor_body (last, list, /*complain=*/true);
19186 /* Finish the function body. */
19187 finish_function_body (body);
19189 return ctor_initializer_p;
19192 /* Parse an initializer.
19194 initializer:
19195 = initializer-clause
19196 ( expression-list )
19198 Returns an expression representing the initializer. If no
19199 initializer is present, NULL_TREE is returned.
19201 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19202 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19203 set to TRUE if there is no initializer present. If there is an
19204 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19205 is set to true; otherwise it is set to false. */
19207 static tree
19208 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19209 bool* non_constant_p)
19211 cp_token *token;
19212 tree init;
19214 /* Peek at the next token. */
19215 token = cp_lexer_peek_token (parser->lexer);
19217 /* Let our caller know whether or not this initializer was
19218 parenthesized. */
19219 *is_direct_init = (token->type != CPP_EQ);
19220 /* Assume that the initializer is constant. */
19221 *non_constant_p = false;
19223 if (token->type == CPP_EQ)
19225 /* Consume the `='. */
19226 cp_lexer_consume_token (parser->lexer);
19227 /* Parse the initializer-clause. */
19228 init = cp_parser_initializer_clause (parser, non_constant_p);
19230 else if (token->type == CPP_OPEN_PAREN)
19232 vec<tree, va_gc> *vec;
19233 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19234 /*cast_p=*/false,
19235 /*allow_expansion_p=*/true,
19236 non_constant_p);
19237 if (vec == NULL)
19238 return error_mark_node;
19239 init = build_tree_list_vec (vec);
19240 release_tree_vector (vec);
19242 else if (token->type == CPP_OPEN_BRACE)
19244 cp_lexer_set_source_position (parser->lexer);
19245 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19246 init = cp_parser_braced_list (parser, non_constant_p);
19247 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19249 else
19251 /* Anything else is an error. */
19252 cp_parser_error (parser, "expected initializer");
19253 init = error_mark_node;
19256 return init;
19259 /* Parse an initializer-clause.
19261 initializer-clause:
19262 assignment-expression
19263 braced-init-list
19265 Returns an expression representing the initializer.
19267 If the `assignment-expression' production is used the value
19268 returned is simply a representation for the expression.
19270 Otherwise, calls cp_parser_braced_list. */
19272 static tree
19273 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19275 tree initializer;
19277 /* Assume the expression is constant. */
19278 *non_constant_p = false;
19280 /* If it is not a `{', then we are looking at an
19281 assignment-expression. */
19282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19284 initializer
19285 = cp_parser_constant_expression (parser,
19286 /*allow_non_constant_p=*/true,
19287 non_constant_p);
19289 else
19290 initializer = cp_parser_braced_list (parser, non_constant_p);
19292 return initializer;
19295 /* Parse a brace-enclosed initializer list.
19297 braced-init-list:
19298 { initializer-list , [opt] }
19301 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19302 the elements of the initializer-list (or NULL, if the last
19303 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19304 NULL_TREE. There is no way to detect whether or not the optional
19305 trailing `,' was provided. NON_CONSTANT_P is as for
19306 cp_parser_initializer. */
19308 static tree
19309 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19311 tree initializer;
19313 /* Consume the `{' token. */
19314 cp_lexer_consume_token (parser->lexer);
19315 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19316 initializer = make_node (CONSTRUCTOR);
19317 /* If it's not a `}', then there is a non-trivial initializer. */
19318 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19320 /* Parse the initializer list. */
19321 CONSTRUCTOR_ELTS (initializer)
19322 = cp_parser_initializer_list (parser, non_constant_p);
19323 /* A trailing `,' token is allowed. */
19324 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19325 cp_lexer_consume_token (parser->lexer);
19327 else
19328 *non_constant_p = false;
19329 /* Now, there should be a trailing `}'. */
19330 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19331 TREE_TYPE (initializer) = init_list_type_node;
19332 return initializer;
19335 /* Consume tokens up to, and including, the next non-nested closing `]'.
19336 Returns true iff we found a closing `]'. */
19338 static bool
19339 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19341 unsigned square_depth = 0;
19343 while (true)
19345 cp_token * token = cp_lexer_peek_token (parser->lexer);
19347 switch (token->type)
19349 case CPP_EOF:
19350 case CPP_PRAGMA_EOL:
19351 /* If we've run out of tokens, then there is no closing `]'. */
19352 return false;
19354 case CPP_OPEN_SQUARE:
19355 ++square_depth;
19356 break;
19358 case CPP_CLOSE_SQUARE:
19359 if (!square_depth--)
19361 cp_lexer_consume_token (parser->lexer);
19362 return true;
19364 break;
19366 default:
19367 break;
19370 /* Consume the token. */
19371 cp_lexer_consume_token (parser->lexer);
19375 /* Return true if we are looking at an array-designator, false otherwise. */
19377 static bool
19378 cp_parser_array_designator_p (cp_parser *parser)
19380 /* Consume the `['. */
19381 cp_lexer_consume_token (parser->lexer);
19383 cp_lexer_save_tokens (parser->lexer);
19385 /* Skip tokens until the next token is a closing square bracket.
19386 If we find the closing `]', and the next token is a `=', then
19387 we are looking at an array designator. */
19388 bool array_designator_p
19389 = (cp_parser_skip_to_closing_square_bracket (parser)
19390 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19392 /* Roll back the tokens we skipped. */
19393 cp_lexer_rollback_tokens (parser->lexer);
19395 return array_designator_p;
19398 /* Parse an initializer-list.
19400 initializer-list:
19401 initializer-clause ... [opt]
19402 initializer-list , initializer-clause ... [opt]
19404 GNU Extension:
19406 initializer-list:
19407 designation initializer-clause ...[opt]
19408 initializer-list , designation initializer-clause ...[opt]
19410 designation:
19411 . identifier =
19412 identifier :
19413 [ constant-expression ] =
19415 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19416 for the initializer. If the INDEX of the elt is non-NULL, it is the
19417 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19418 as for cp_parser_initializer. */
19420 static vec<constructor_elt, va_gc> *
19421 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19423 vec<constructor_elt, va_gc> *v = NULL;
19425 /* Assume all of the expressions are constant. */
19426 *non_constant_p = false;
19428 /* Parse the rest of the list. */
19429 while (true)
19431 cp_token *token;
19432 tree designator;
19433 tree initializer;
19434 bool clause_non_constant_p;
19436 /* If the next token is an identifier and the following one is a
19437 colon, we are looking at the GNU designated-initializer
19438 syntax. */
19439 if (cp_parser_allow_gnu_extensions_p (parser)
19440 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19441 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19443 /* Warn the user that they are using an extension. */
19444 pedwarn (input_location, OPT_Wpedantic,
19445 "ISO C++ does not allow designated initializers");
19446 /* Consume the identifier. */
19447 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19448 /* Consume the `:'. */
19449 cp_lexer_consume_token (parser->lexer);
19451 /* Also handle the C99 syntax, '. id ='. */
19452 else if (cp_parser_allow_gnu_extensions_p (parser)
19453 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19454 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19455 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19457 /* Warn the user that they are using an extension. */
19458 pedwarn (input_location, OPT_Wpedantic,
19459 "ISO C++ does not allow C99 designated initializers");
19460 /* Consume the `.'. */
19461 cp_lexer_consume_token (parser->lexer);
19462 /* Consume the identifier. */
19463 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19464 /* Consume the `='. */
19465 cp_lexer_consume_token (parser->lexer);
19467 /* Also handle C99 array designators, '[ const ] ='. */
19468 else if (cp_parser_allow_gnu_extensions_p (parser)
19469 && !c_dialect_objc ()
19470 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19472 /* In C++11, [ could start a lambda-introducer. */
19473 bool non_const = false;
19475 cp_parser_parse_tentatively (parser);
19477 if (!cp_parser_array_designator_p (parser))
19479 cp_parser_simulate_error (parser);
19480 designator = NULL_TREE;
19482 else
19484 designator = cp_parser_constant_expression (parser, true,
19485 &non_const);
19486 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19487 cp_parser_require (parser, CPP_EQ, RT_EQ);
19490 if (!cp_parser_parse_definitely (parser))
19491 designator = NULL_TREE;
19492 else if (non_const)
19493 require_potential_rvalue_constant_expression (designator);
19495 else
19496 designator = NULL_TREE;
19498 /* Parse the initializer. */
19499 initializer = cp_parser_initializer_clause (parser,
19500 &clause_non_constant_p);
19501 /* If any clause is non-constant, so is the entire initializer. */
19502 if (clause_non_constant_p)
19503 *non_constant_p = true;
19505 /* If we have an ellipsis, this is an initializer pack
19506 expansion. */
19507 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19509 /* Consume the `...'. */
19510 cp_lexer_consume_token (parser->lexer);
19512 /* Turn the initializer into an initializer expansion. */
19513 initializer = make_pack_expansion (initializer);
19516 /* Add it to the vector. */
19517 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19519 /* If the next token is not a comma, we have reached the end of
19520 the list. */
19521 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19522 break;
19524 /* Peek at the next token. */
19525 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19526 /* If the next token is a `}', then we're still done. An
19527 initializer-clause can have a trailing `,' after the
19528 initializer-list and before the closing `}'. */
19529 if (token->type == CPP_CLOSE_BRACE)
19530 break;
19532 /* Consume the `,' token. */
19533 cp_lexer_consume_token (parser->lexer);
19536 return v;
19539 /* Classes [gram.class] */
19541 /* Parse a class-name.
19543 class-name:
19544 identifier
19545 template-id
19547 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19548 to indicate that names looked up in dependent types should be
19549 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19550 keyword has been used to indicate that the name that appears next
19551 is a template. TAG_TYPE indicates the explicit tag given before
19552 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19553 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19554 is the class being defined in a class-head.
19556 Returns the TYPE_DECL representing the class. */
19558 static tree
19559 cp_parser_class_name (cp_parser *parser,
19560 bool typename_keyword_p,
19561 bool template_keyword_p,
19562 enum tag_types tag_type,
19563 bool check_dependency_p,
19564 bool class_head_p,
19565 bool is_declaration)
19567 tree decl;
19568 tree scope;
19569 bool typename_p;
19570 cp_token *token;
19571 tree identifier = NULL_TREE;
19573 /* All class-names start with an identifier. */
19574 token = cp_lexer_peek_token (parser->lexer);
19575 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19577 cp_parser_error (parser, "expected class-name");
19578 return error_mark_node;
19581 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19582 to a template-id, so we save it here. */
19583 scope = parser->scope;
19584 if (scope == error_mark_node)
19585 return error_mark_node;
19587 /* Any name names a type if we're following the `typename' keyword
19588 in a qualified name where the enclosing scope is type-dependent. */
19589 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19590 && dependent_type_p (scope));
19591 /* Handle the common case (an identifier, but not a template-id)
19592 efficiently. */
19593 if (token->type == CPP_NAME
19594 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19596 cp_token *identifier_token;
19597 bool ambiguous_p;
19599 /* Look for the identifier. */
19600 identifier_token = cp_lexer_peek_token (parser->lexer);
19601 ambiguous_p = identifier_token->error_reported;
19602 identifier = cp_parser_identifier (parser);
19603 /* If the next token isn't an identifier, we are certainly not
19604 looking at a class-name. */
19605 if (identifier == error_mark_node)
19606 decl = error_mark_node;
19607 /* If we know this is a type-name, there's no need to look it
19608 up. */
19609 else if (typename_p)
19610 decl = identifier;
19611 else
19613 tree ambiguous_decls;
19614 /* If we already know that this lookup is ambiguous, then
19615 we've already issued an error message; there's no reason
19616 to check again. */
19617 if (ambiguous_p)
19619 cp_parser_simulate_error (parser);
19620 return error_mark_node;
19622 /* If the next token is a `::', then the name must be a type
19623 name.
19625 [basic.lookup.qual]
19627 During the lookup for a name preceding the :: scope
19628 resolution operator, object, function, and enumerator
19629 names are ignored. */
19630 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19631 tag_type = typename_type;
19632 /* Look up the name. */
19633 decl = cp_parser_lookup_name (parser, identifier,
19634 tag_type,
19635 /*is_template=*/false,
19636 /*is_namespace=*/false,
19637 check_dependency_p,
19638 &ambiguous_decls,
19639 identifier_token->location);
19640 if (ambiguous_decls)
19642 if (cp_parser_parsing_tentatively (parser))
19643 cp_parser_simulate_error (parser);
19644 return error_mark_node;
19648 else
19650 /* Try a template-id. */
19651 decl = cp_parser_template_id (parser, template_keyword_p,
19652 check_dependency_p,
19653 tag_type,
19654 is_declaration);
19655 if (decl == error_mark_node)
19656 return error_mark_node;
19659 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19661 /* If this is a typename, create a TYPENAME_TYPE. */
19662 if (typename_p && decl != error_mark_node)
19664 decl = make_typename_type (scope, decl, typename_type,
19665 /*complain=*/tf_error);
19666 if (decl != error_mark_node)
19667 decl = TYPE_NAME (decl);
19670 decl = strip_using_decl (decl);
19672 /* Check to see that it is really the name of a class. */
19673 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19674 && identifier_p (TREE_OPERAND (decl, 0))
19675 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19676 /* Situations like this:
19678 template <typename T> struct A {
19679 typename T::template X<int>::I i;
19682 are problematic. Is `T::template X<int>' a class-name? The
19683 standard does not seem to be definitive, but there is no other
19684 valid interpretation of the following `::'. Therefore, those
19685 names are considered class-names. */
19687 decl = make_typename_type (scope, decl, tag_type, tf_error);
19688 if (decl != error_mark_node)
19689 decl = TYPE_NAME (decl);
19691 else if (TREE_CODE (decl) != TYPE_DECL
19692 || TREE_TYPE (decl) == error_mark_node
19693 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19694 /* In Objective-C 2.0, a classname followed by '.' starts a
19695 dot-syntax expression, and it's not a type-name. */
19696 || (c_dialect_objc ()
19697 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19698 && objc_is_class_name (decl)))
19699 decl = error_mark_node;
19701 if (decl == error_mark_node)
19702 cp_parser_error (parser, "expected class-name");
19703 else if (identifier && !parser->scope)
19704 maybe_note_name_used_in_class (identifier, decl);
19706 return decl;
19709 /* Parse a class-specifier.
19711 class-specifier:
19712 class-head { member-specification [opt] }
19714 Returns the TREE_TYPE representing the class. */
19716 static tree
19717 cp_parser_class_specifier_1 (cp_parser* parser)
19719 tree type;
19720 tree attributes = NULL_TREE;
19721 bool nested_name_specifier_p;
19722 unsigned saved_num_template_parameter_lists;
19723 bool saved_in_function_body;
19724 unsigned char in_statement;
19725 bool in_switch_statement_p;
19726 bool saved_in_unbraced_linkage_specification_p;
19727 tree old_scope = NULL_TREE;
19728 tree scope = NULL_TREE;
19729 cp_token *closing_brace;
19731 push_deferring_access_checks (dk_no_deferred);
19733 /* Parse the class-head. */
19734 type = cp_parser_class_head (parser,
19735 &nested_name_specifier_p);
19736 /* If the class-head was a semantic disaster, skip the entire body
19737 of the class. */
19738 if (!type)
19740 cp_parser_skip_to_end_of_block_or_statement (parser);
19741 pop_deferring_access_checks ();
19742 return error_mark_node;
19745 /* Look for the `{'. */
19746 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19748 pop_deferring_access_checks ();
19749 return error_mark_node;
19752 cp_ensure_no_omp_declare_simd (parser);
19754 /* Issue an error message if type-definitions are forbidden here. */
19755 cp_parser_check_type_definition (parser);
19756 /* Remember that we are defining one more class. */
19757 ++parser->num_classes_being_defined;
19758 /* Inside the class, surrounding template-parameter-lists do not
19759 apply. */
19760 saved_num_template_parameter_lists
19761 = parser->num_template_parameter_lists;
19762 parser->num_template_parameter_lists = 0;
19763 /* We are not in a function body. */
19764 saved_in_function_body = parser->in_function_body;
19765 parser->in_function_body = false;
19766 /* Or in a loop. */
19767 in_statement = parser->in_statement;
19768 parser->in_statement = 0;
19769 /* Or in a switch. */
19770 in_switch_statement_p = parser->in_switch_statement_p;
19771 parser->in_switch_statement_p = false;
19772 /* We are not immediately inside an extern "lang" block. */
19773 saved_in_unbraced_linkage_specification_p
19774 = parser->in_unbraced_linkage_specification_p;
19775 parser->in_unbraced_linkage_specification_p = false;
19777 /* Start the class. */
19778 if (nested_name_specifier_p)
19780 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19781 old_scope = push_inner_scope (scope);
19783 type = begin_class_definition (type);
19785 if (type == error_mark_node)
19786 /* If the type is erroneous, skip the entire body of the class. */
19787 cp_parser_skip_to_closing_brace (parser);
19788 else
19789 /* Parse the member-specification. */
19790 cp_parser_member_specification_opt (parser);
19792 /* Look for the trailing `}'. */
19793 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19794 /* Look for trailing attributes to apply to this class. */
19795 if (cp_parser_allow_gnu_extensions_p (parser))
19796 attributes = cp_parser_gnu_attributes_opt (parser);
19797 if (type != error_mark_node)
19798 type = finish_struct (type, attributes);
19799 if (nested_name_specifier_p)
19800 pop_inner_scope (old_scope, scope);
19802 /* We've finished a type definition. Check for the common syntax
19803 error of forgetting a semicolon after the definition. We need to
19804 be careful, as we can't just check for not-a-semicolon and be done
19805 with it; the user might have typed:
19807 class X { } c = ...;
19808 class X { } *p = ...;
19810 and so forth. Instead, enumerate all the possible tokens that
19811 might follow this production; if we don't see one of them, then
19812 complain and silently insert the semicolon. */
19814 cp_token *token = cp_lexer_peek_token (parser->lexer);
19815 bool want_semicolon = true;
19817 if (cp_next_tokens_can_be_std_attribute_p (parser))
19818 /* Don't try to parse c++11 attributes here. As per the
19819 grammar, that should be a task for
19820 cp_parser_decl_specifier_seq. */
19821 want_semicolon = false;
19823 switch (token->type)
19825 case CPP_NAME:
19826 case CPP_SEMICOLON:
19827 case CPP_MULT:
19828 case CPP_AND:
19829 case CPP_OPEN_PAREN:
19830 case CPP_CLOSE_PAREN:
19831 case CPP_COMMA:
19832 want_semicolon = false;
19833 break;
19835 /* While it's legal for type qualifiers and storage class
19836 specifiers to follow type definitions in the grammar, only
19837 compiler testsuites contain code like that. Assume that if
19838 we see such code, then what we're really seeing is a case
19839 like:
19841 class X { }
19842 const <type> var = ...;
19846 class Y { }
19847 static <type> func (...) ...
19849 i.e. the qualifier or specifier applies to the next
19850 declaration. To do so, however, we need to look ahead one
19851 more token to see if *that* token is a type specifier.
19853 This code could be improved to handle:
19855 class Z { }
19856 static const <type> var = ...; */
19857 case CPP_KEYWORD:
19858 if (keyword_is_decl_specifier (token->keyword))
19860 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19862 /* Handling user-defined types here would be nice, but very
19863 tricky. */
19864 want_semicolon
19865 = (lookahead->type == CPP_KEYWORD
19866 && keyword_begins_type_specifier (lookahead->keyword));
19868 break;
19869 default:
19870 break;
19873 /* If we don't have a type, then something is very wrong and we
19874 shouldn't try to do anything clever. Likewise for not seeing the
19875 closing brace. */
19876 if (closing_brace && TYPE_P (type) && want_semicolon)
19878 cp_token_position prev
19879 = cp_lexer_previous_token_position (parser->lexer);
19880 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19881 location_t loc = prev_token->location;
19883 if (CLASSTYPE_DECLARED_CLASS (type))
19884 error_at (loc, "expected %<;%> after class definition");
19885 else if (TREE_CODE (type) == RECORD_TYPE)
19886 error_at (loc, "expected %<;%> after struct definition");
19887 else if (TREE_CODE (type) == UNION_TYPE)
19888 error_at (loc, "expected %<;%> after union definition");
19889 else
19890 gcc_unreachable ();
19892 /* Unget one token and smash it to look as though we encountered
19893 a semicolon in the input stream. */
19894 cp_lexer_set_token_position (parser->lexer, prev);
19895 token = cp_lexer_peek_token (parser->lexer);
19896 token->type = CPP_SEMICOLON;
19897 token->keyword = RID_MAX;
19901 /* If this class is not itself within the scope of another class,
19902 then we need to parse the bodies of all of the queued function
19903 definitions. Note that the queued functions defined in a class
19904 are not always processed immediately following the
19905 class-specifier for that class. Consider:
19907 struct A {
19908 struct B { void f() { sizeof (A); } };
19911 If `f' were processed before the processing of `A' were
19912 completed, there would be no way to compute the size of `A'.
19913 Note that the nesting we are interested in here is lexical --
19914 not the semantic nesting given by TYPE_CONTEXT. In particular,
19915 for:
19917 struct A { struct B; };
19918 struct A::B { void f() { } };
19920 there is no need to delay the parsing of `A::B::f'. */
19921 if (--parser->num_classes_being_defined == 0)
19923 tree decl;
19924 tree class_type = NULL_TREE;
19925 tree pushed_scope = NULL_TREE;
19926 unsigned ix;
19927 cp_default_arg_entry *e;
19928 tree save_ccp, save_ccr;
19930 /* In a first pass, parse default arguments to the functions.
19931 Then, in a second pass, parse the bodies of the functions.
19932 This two-phased approach handles cases like:
19934 struct S {
19935 void f() { g(); }
19936 void g(int i = 3);
19940 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19942 decl = e->decl;
19943 /* If there are default arguments that have not yet been processed,
19944 take care of them now. */
19945 if (class_type != e->class_type)
19947 if (pushed_scope)
19948 pop_scope (pushed_scope);
19949 class_type = e->class_type;
19950 pushed_scope = push_scope (class_type);
19952 /* Make sure that any template parameters are in scope. */
19953 maybe_begin_member_template_processing (decl);
19954 /* Parse the default argument expressions. */
19955 cp_parser_late_parsing_default_args (parser, decl);
19956 /* Remove any template parameters from the symbol table. */
19957 maybe_end_member_template_processing ();
19959 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19960 /* Now parse any NSDMIs. */
19961 save_ccp = current_class_ptr;
19962 save_ccr = current_class_ref;
19963 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19965 if (class_type != DECL_CONTEXT (decl))
19967 if (pushed_scope)
19968 pop_scope (pushed_scope);
19969 class_type = DECL_CONTEXT (decl);
19970 pushed_scope = push_scope (class_type);
19972 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19973 cp_parser_late_parsing_nsdmi (parser, decl);
19975 vec_safe_truncate (unparsed_nsdmis, 0);
19976 current_class_ptr = save_ccp;
19977 current_class_ref = save_ccr;
19978 if (pushed_scope)
19979 pop_scope (pushed_scope);
19981 /* Now do some post-NSDMI bookkeeping. */
19982 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19983 after_nsdmi_defaulted_late_checks (class_type);
19984 vec_safe_truncate (unparsed_classes, 0);
19985 after_nsdmi_defaulted_late_checks (type);
19987 /* Now parse the body of the functions. */
19988 if (flag_openmp)
19990 /* OpenMP UDRs need to be parsed before all other functions. */
19991 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19992 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19993 cp_parser_late_parsing_for_member (parser, decl);
19994 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19995 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19996 cp_parser_late_parsing_for_member (parser, decl);
19998 else
19999 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20000 cp_parser_late_parsing_for_member (parser, decl);
20001 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20003 else
20004 vec_safe_push (unparsed_classes, type);
20006 /* Put back any saved access checks. */
20007 pop_deferring_access_checks ();
20009 /* Restore saved state. */
20010 parser->in_switch_statement_p = in_switch_statement_p;
20011 parser->in_statement = in_statement;
20012 parser->in_function_body = saved_in_function_body;
20013 parser->num_template_parameter_lists
20014 = saved_num_template_parameter_lists;
20015 parser->in_unbraced_linkage_specification_p
20016 = saved_in_unbraced_linkage_specification_p;
20018 return type;
20021 static tree
20022 cp_parser_class_specifier (cp_parser* parser)
20024 tree ret;
20025 timevar_push (TV_PARSE_STRUCT);
20026 ret = cp_parser_class_specifier_1 (parser);
20027 timevar_pop (TV_PARSE_STRUCT);
20028 return ret;
20031 /* Parse a class-head.
20033 class-head:
20034 class-key identifier [opt] base-clause [opt]
20035 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20036 class-key nested-name-specifier [opt] template-id
20037 base-clause [opt]
20039 class-virt-specifier:
20040 final
20042 GNU Extensions:
20043 class-key attributes identifier [opt] base-clause [opt]
20044 class-key attributes nested-name-specifier identifier base-clause [opt]
20045 class-key attributes nested-name-specifier [opt] template-id
20046 base-clause [opt]
20048 Upon return BASES is initialized to the list of base classes (or
20049 NULL, if there are none) in the same form returned by
20050 cp_parser_base_clause.
20052 Returns the TYPE of the indicated class. Sets
20053 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20054 involving a nested-name-specifier was used, and FALSE otherwise.
20056 Returns error_mark_node if this is not a class-head.
20058 Returns NULL_TREE if the class-head is syntactically valid, but
20059 semantically invalid in a way that means we should skip the entire
20060 body of the class. */
20062 static tree
20063 cp_parser_class_head (cp_parser* parser,
20064 bool* nested_name_specifier_p)
20066 tree nested_name_specifier;
20067 enum tag_types class_key;
20068 tree id = NULL_TREE;
20069 tree type = NULL_TREE;
20070 tree attributes;
20071 tree bases;
20072 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20073 bool template_id_p = false;
20074 bool qualified_p = false;
20075 bool invalid_nested_name_p = false;
20076 bool invalid_explicit_specialization_p = false;
20077 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20078 tree pushed_scope = NULL_TREE;
20079 unsigned num_templates;
20080 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20081 /* Assume no nested-name-specifier will be present. */
20082 *nested_name_specifier_p = false;
20083 /* Assume no template parameter lists will be used in defining the
20084 type. */
20085 num_templates = 0;
20086 parser->colon_corrects_to_scope_p = false;
20088 /* Look for the class-key. */
20089 class_key = cp_parser_class_key (parser);
20090 if (class_key == none_type)
20091 return error_mark_node;
20093 /* Parse the attributes. */
20094 attributes = cp_parser_attributes_opt (parser);
20096 /* If the next token is `::', that is invalid -- but sometimes
20097 people do try to write:
20099 struct ::S {};
20101 Handle this gracefully by accepting the extra qualifier, and then
20102 issuing an error about it later if this really is a
20103 class-head. If it turns out just to be an elaborated type
20104 specifier, remain silent. */
20105 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20106 qualified_p = true;
20108 push_deferring_access_checks (dk_no_check);
20110 /* Determine the name of the class. Begin by looking for an
20111 optional nested-name-specifier. */
20112 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20113 nested_name_specifier
20114 = cp_parser_nested_name_specifier_opt (parser,
20115 /*typename_keyword_p=*/false,
20116 /*check_dependency_p=*/false,
20117 /*type_p=*/true,
20118 /*is_declaration=*/false);
20119 /* If there was a nested-name-specifier, then there *must* be an
20120 identifier. */
20121 if (nested_name_specifier)
20123 type_start_token = cp_lexer_peek_token (parser->lexer);
20124 /* Although the grammar says `identifier', it really means
20125 `class-name' or `template-name'. You are only allowed to
20126 define a class that has already been declared with this
20127 syntax.
20129 The proposed resolution for Core Issue 180 says that wherever
20130 you see `class T::X' you should treat `X' as a type-name.
20132 It is OK to define an inaccessible class; for example:
20134 class A { class B; };
20135 class A::B {};
20137 We do not know if we will see a class-name, or a
20138 template-name. We look for a class-name first, in case the
20139 class-name is a template-id; if we looked for the
20140 template-name first we would stop after the template-name. */
20141 cp_parser_parse_tentatively (parser);
20142 type = cp_parser_class_name (parser,
20143 /*typename_keyword_p=*/false,
20144 /*template_keyword_p=*/false,
20145 class_type,
20146 /*check_dependency_p=*/false,
20147 /*class_head_p=*/true,
20148 /*is_declaration=*/false);
20149 /* If that didn't work, ignore the nested-name-specifier. */
20150 if (!cp_parser_parse_definitely (parser))
20152 invalid_nested_name_p = true;
20153 type_start_token = cp_lexer_peek_token (parser->lexer);
20154 id = cp_parser_identifier (parser);
20155 if (id == error_mark_node)
20156 id = NULL_TREE;
20158 /* If we could not find a corresponding TYPE, treat this
20159 declaration like an unqualified declaration. */
20160 if (type == error_mark_node)
20161 nested_name_specifier = NULL_TREE;
20162 /* Otherwise, count the number of templates used in TYPE and its
20163 containing scopes. */
20164 else
20166 tree scope;
20168 for (scope = TREE_TYPE (type);
20169 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20170 scope = get_containing_scope (scope))
20171 if (TYPE_P (scope)
20172 && CLASS_TYPE_P (scope)
20173 && CLASSTYPE_TEMPLATE_INFO (scope)
20174 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20175 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20176 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20177 ++num_templates;
20180 /* Otherwise, the identifier is optional. */
20181 else
20183 /* We don't know whether what comes next is a template-id,
20184 an identifier, or nothing at all. */
20185 cp_parser_parse_tentatively (parser);
20186 /* Check for a template-id. */
20187 type_start_token = cp_lexer_peek_token (parser->lexer);
20188 id = cp_parser_template_id (parser,
20189 /*template_keyword_p=*/false,
20190 /*check_dependency_p=*/true,
20191 class_key,
20192 /*is_declaration=*/true);
20193 /* If that didn't work, it could still be an identifier. */
20194 if (!cp_parser_parse_definitely (parser))
20196 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20198 type_start_token = cp_lexer_peek_token (parser->lexer);
20199 id = cp_parser_identifier (parser);
20201 else
20202 id = NULL_TREE;
20204 else
20206 template_id_p = true;
20207 ++num_templates;
20211 pop_deferring_access_checks ();
20213 if (id)
20215 cp_parser_check_for_invalid_template_id (parser, id,
20216 class_key,
20217 type_start_token->location);
20219 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20221 /* If it's not a `:' or a `{' then we can't really be looking at a
20222 class-head, since a class-head only appears as part of a
20223 class-specifier. We have to detect this situation before calling
20224 xref_tag, since that has irreversible side-effects. */
20225 if (!cp_parser_next_token_starts_class_definition_p (parser))
20227 cp_parser_error (parser, "expected %<{%> or %<:%>");
20228 type = error_mark_node;
20229 goto out;
20232 /* At this point, we're going ahead with the class-specifier, even
20233 if some other problem occurs. */
20234 cp_parser_commit_to_tentative_parse (parser);
20235 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20237 cp_parser_error (parser,
20238 "cannot specify %<override%> for a class");
20239 type = error_mark_node;
20240 goto out;
20242 /* Issue the error about the overly-qualified name now. */
20243 if (qualified_p)
20245 cp_parser_error (parser,
20246 "global qualification of class name is invalid");
20247 type = error_mark_node;
20248 goto out;
20250 else if (invalid_nested_name_p)
20252 cp_parser_error (parser,
20253 "qualified name does not name a class");
20254 type = error_mark_node;
20255 goto out;
20257 else if (nested_name_specifier)
20259 tree scope;
20261 /* Reject typedef-names in class heads. */
20262 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20264 error_at (type_start_token->location,
20265 "invalid class name in declaration of %qD",
20266 type);
20267 type = NULL_TREE;
20268 goto done;
20271 /* Figure out in what scope the declaration is being placed. */
20272 scope = current_scope ();
20273 /* If that scope does not contain the scope in which the
20274 class was originally declared, the program is invalid. */
20275 if (scope && !is_ancestor (scope, nested_name_specifier))
20277 if (at_namespace_scope_p ())
20278 error_at (type_start_token->location,
20279 "declaration of %qD in namespace %qD which does not "
20280 "enclose %qD",
20281 type, scope, nested_name_specifier);
20282 else
20283 error_at (type_start_token->location,
20284 "declaration of %qD in %qD which does not enclose %qD",
20285 type, scope, nested_name_specifier);
20286 type = NULL_TREE;
20287 goto done;
20289 /* [dcl.meaning]
20291 A declarator-id shall not be qualified except for the
20292 definition of a ... nested class outside of its class
20293 ... [or] the definition or explicit instantiation of a
20294 class member of a namespace outside of its namespace. */
20295 if (scope == nested_name_specifier)
20297 permerror (nested_name_specifier_token_start->location,
20298 "extra qualification not allowed");
20299 nested_name_specifier = NULL_TREE;
20300 num_templates = 0;
20303 /* An explicit-specialization must be preceded by "template <>". If
20304 it is not, try to recover gracefully. */
20305 if (at_namespace_scope_p ()
20306 && parser->num_template_parameter_lists == 0
20307 && template_id_p)
20309 error_at (type_start_token->location,
20310 "an explicit specialization must be preceded by %<template <>%>");
20311 invalid_explicit_specialization_p = true;
20312 /* Take the same action that would have been taken by
20313 cp_parser_explicit_specialization. */
20314 ++parser->num_template_parameter_lists;
20315 begin_specialization ();
20317 /* There must be no "return" statements between this point and the
20318 end of this function; set "type "to the correct return value and
20319 use "goto done;" to return. */
20320 /* Make sure that the right number of template parameters were
20321 present. */
20322 if (!cp_parser_check_template_parameters (parser, num_templates,
20323 type_start_token->location,
20324 /*declarator=*/NULL))
20326 /* If something went wrong, there is no point in even trying to
20327 process the class-definition. */
20328 type = NULL_TREE;
20329 goto done;
20332 /* Look up the type. */
20333 if (template_id_p)
20335 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20336 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20337 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20339 error_at (type_start_token->location,
20340 "function template %qD redeclared as a class template", id);
20341 type = error_mark_node;
20343 else
20345 type = TREE_TYPE (id);
20346 type = maybe_process_partial_specialization (type);
20348 if (nested_name_specifier)
20349 pushed_scope = push_scope (nested_name_specifier);
20351 else if (nested_name_specifier)
20353 tree class_type;
20355 /* Given:
20357 template <typename T> struct S { struct T };
20358 template <typename T> struct S<T>::T { };
20360 we will get a TYPENAME_TYPE when processing the definition of
20361 `S::T'. We need to resolve it to the actual type before we
20362 try to define it. */
20363 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20365 class_type = resolve_typename_type (TREE_TYPE (type),
20366 /*only_current_p=*/false);
20367 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20368 type = TYPE_NAME (class_type);
20369 else
20371 cp_parser_error (parser, "could not resolve typename type");
20372 type = error_mark_node;
20376 if (maybe_process_partial_specialization (TREE_TYPE (type))
20377 == error_mark_node)
20379 type = NULL_TREE;
20380 goto done;
20383 class_type = current_class_type;
20384 /* Enter the scope indicated by the nested-name-specifier. */
20385 pushed_scope = push_scope (nested_name_specifier);
20386 /* Get the canonical version of this type. */
20387 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20388 /* Call push_template_decl if it seems like we should be defining a
20389 template either from the template headers or the type we're
20390 defining, so that we diagnose both extra and missing headers. */
20391 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20392 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20393 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20395 type = push_template_decl (type);
20396 if (type == error_mark_node)
20398 type = NULL_TREE;
20399 goto done;
20403 type = TREE_TYPE (type);
20404 *nested_name_specifier_p = true;
20406 else /* The name is not a nested name. */
20408 /* If the class was unnamed, create a dummy name. */
20409 if (!id)
20410 id = make_anon_name ();
20411 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20412 parser->num_template_parameter_lists);
20415 /* Indicate whether this class was declared as a `class' or as a
20416 `struct'. */
20417 if (TREE_CODE (type) == RECORD_TYPE)
20418 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20419 cp_parser_check_class_key (class_key, type);
20421 /* If this type was already complete, and we see another definition,
20422 that's an error. */
20423 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20425 error_at (type_start_token->location, "redefinition of %q#T",
20426 type);
20427 error_at (type_start_token->location, "previous definition of %q+#T",
20428 type);
20429 type = NULL_TREE;
20430 goto done;
20432 else if (type == error_mark_node)
20433 type = NULL_TREE;
20435 if (type)
20437 /* Apply attributes now, before any use of the class as a template
20438 argument in its base list. */
20439 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20440 fixup_attribute_variants (type);
20443 /* We will have entered the scope containing the class; the names of
20444 base classes should be looked up in that context. For example:
20446 struct A { struct B {}; struct C; };
20447 struct A::C : B {};
20449 is valid. */
20451 /* Get the list of base-classes, if there is one. */
20452 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20454 /* PR59482: enter the class scope so that base-specifiers are looked
20455 up correctly. */
20456 if (type)
20457 pushclass (type);
20458 bases = cp_parser_base_clause (parser);
20459 /* PR59482: get out of the previously pushed class scope so that the
20460 subsequent pops pop the right thing. */
20461 if (type)
20462 popclass ();
20464 else
20465 bases = NULL_TREE;
20467 /* If we're really defining a class, process the base classes.
20468 If they're invalid, fail. */
20469 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20470 && !xref_basetypes (type, bases))
20471 type = NULL_TREE;
20473 done:
20474 /* Leave the scope given by the nested-name-specifier. We will
20475 enter the class scope itself while processing the members. */
20476 if (pushed_scope)
20477 pop_scope (pushed_scope);
20479 if (invalid_explicit_specialization_p)
20481 end_specialization ();
20482 --parser->num_template_parameter_lists;
20485 if (type)
20486 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20487 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20488 CLASSTYPE_FINAL (type) = 1;
20489 out:
20490 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20491 return type;
20494 /* Parse a class-key.
20496 class-key:
20497 class
20498 struct
20499 union
20501 Returns the kind of class-key specified, or none_type to indicate
20502 error. */
20504 static enum tag_types
20505 cp_parser_class_key (cp_parser* parser)
20507 cp_token *token;
20508 enum tag_types tag_type;
20510 /* Look for the class-key. */
20511 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20512 if (!token)
20513 return none_type;
20515 /* Check to see if the TOKEN is a class-key. */
20516 tag_type = cp_parser_token_is_class_key (token);
20517 if (!tag_type)
20518 cp_parser_error (parser, "expected class-key");
20519 return tag_type;
20522 /* Parse a type-parameter-key.
20524 type-parameter-key:
20525 class
20526 typename
20529 static void
20530 cp_parser_type_parameter_key (cp_parser* parser)
20532 /* Look for the type-parameter-key. */
20533 enum tag_types tag_type = none_type;
20534 cp_token *token = cp_lexer_peek_token (parser->lexer);
20535 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20537 cp_lexer_consume_token (parser->lexer);
20538 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20539 /* typename is not allowed in a template template parameter
20540 by the standard until C++1Z. */
20541 pedwarn (token->location, OPT_Wpedantic,
20542 "ISO C++ forbids typename key in template template parameter;"
20543 " use -std=c++1z or -std=gnu++1z");
20545 else
20546 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20548 return;
20551 /* Parse an (optional) member-specification.
20553 member-specification:
20554 member-declaration member-specification [opt]
20555 access-specifier : member-specification [opt] */
20557 static void
20558 cp_parser_member_specification_opt (cp_parser* parser)
20560 while (true)
20562 cp_token *token;
20563 enum rid keyword;
20565 /* Peek at the next token. */
20566 token = cp_lexer_peek_token (parser->lexer);
20567 /* If it's a `}', or EOF then we've seen all the members. */
20568 if (token->type == CPP_CLOSE_BRACE
20569 || token->type == CPP_EOF
20570 || token->type == CPP_PRAGMA_EOL)
20571 break;
20573 /* See if this token is a keyword. */
20574 keyword = token->keyword;
20575 switch (keyword)
20577 case RID_PUBLIC:
20578 case RID_PROTECTED:
20579 case RID_PRIVATE:
20580 /* Consume the access-specifier. */
20581 cp_lexer_consume_token (parser->lexer);
20582 /* Remember which access-specifier is active. */
20583 current_access_specifier = token->u.value;
20584 /* Look for the `:'. */
20585 cp_parser_require (parser, CPP_COLON, RT_COLON);
20586 break;
20588 default:
20589 /* Accept #pragmas at class scope. */
20590 if (token->type == CPP_PRAGMA)
20592 cp_parser_pragma (parser, pragma_member);
20593 break;
20596 /* Otherwise, the next construction must be a
20597 member-declaration. */
20598 cp_parser_member_declaration (parser);
20603 /* Parse a member-declaration.
20605 member-declaration:
20606 decl-specifier-seq [opt] member-declarator-list [opt] ;
20607 function-definition ; [opt]
20608 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20609 using-declaration
20610 template-declaration
20611 alias-declaration
20613 member-declarator-list:
20614 member-declarator
20615 member-declarator-list , member-declarator
20617 member-declarator:
20618 declarator pure-specifier [opt]
20619 declarator constant-initializer [opt]
20620 identifier [opt] : constant-expression
20622 GNU Extensions:
20624 member-declaration:
20625 __extension__ member-declaration
20627 member-declarator:
20628 declarator attributes [opt] pure-specifier [opt]
20629 declarator attributes [opt] constant-initializer [opt]
20630 identifier [opt] attributes [opt] : constant-expression
20632 C++0x Extensions:
20634 member-declaration:
20635 static_assert-declaration */
20637 static void
20638 cp_parser_member_declaration (cp_parser* parser)
20640 cp_decl_specifier_seq decl_specifiers;
20641 tree prefix_attributes;
20642 tree decl;
20643 int declares_class_or_enum;
20644 bool friend_p;
20645 cp_token *token = NULL;
20646 cp_token *decl_spec_token_start = NULL;
20647 cp_token *initializer_token_start = NULL;
20648 int saved_pedantic;
20649 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20651 /* Check for the `__extension__' keyword. */
20652 if (cp_parser_extension_opt (parser, &saved_pedantic))
20654 /* Recurse. */
20655 cp_parser_member_declaration (parser);
20656 /* Restore the old value of the PEDANTIC flag. */
20657 pedantic = saved_pedantic;
20659 return;
20662 /* Check for a template-declaration. */
20663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20665 /* An explicit specialization here is an error condition, and we
20666 expect the specialization handler to detect and report this. */
20667 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20668 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20669 cp_parser_explicit_specialization (parser);
20670 else
20671 cp_parser_template_declaration (parser, /*member_p=*/true);
20673 return;
20676 /* Check for a using-declaration. */
20677 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20679 if (cxx_dialect < cxx11)
20681 /* Parse the using-declaration. */
20682 cp_parser_using_declaration (parser,
20683 /*access_declaration_p=*/false);
20684 return;
20686 else
20688 tree decl;
20689 bool alias_decl_expected;
20690 cp_parser_parse_tentatively (parser);
20691 decl = cp_parser_alias_declaration (parser);
20692 /* Note that if we actually see the '=' token after the
20693 identifier, cp_parser_alias_declaration commits the
20694 tentative parse. In that case, we really expects an
20695 alias-declaration. Otherwise, we expect a using
20696 declaration. */
20697 alias_decl_expected =
20698 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20699 cp_parser_parse_definitely (parser);
20701 if (alias_decl_expected)
20702 finish_member_declaration (decl);
20703 else
20704 cp_parser_using_declaration (parser,
20705 /*access_declaration_p=*/false);
20706 return;
20710 /* Check for @defs. */
20711 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20713 tree ivar, member;
20714 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20715 ivar = ivar_chains;
20716 while (ivar)
20718 member = ivar;
20719 ivar = TREE_CHAIN (member);
20720 TREE_CHAIN (member) = NULL_TREE;
20721 finish_member_declaration (member);
20723 return;
20726 /* If the next token is `static_assert' we have a static assertion. */
20727 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20729 cp_parser_static_assert (parser, /*member_p=*/true);
20730 return;
20733 parser->colon_corrects_to_scope_p = false;
20735 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20736 goto out;
20738 /* Parse the decl-specifier-seq. */
20739 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20740 cp_parser_decl_specifier_seq (parser,
20741 CP_PARSER_FLAGS_OPTIONAL,
20742 &decl_specifiers,
20743 &declares_class_or_enum);
20744 /* Check for an invalid type-name. */
20745 if (!decl_specifiers.any_type_specifiers_p
20746 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20747 goto out;
20748 /* If there is no declarator, then the decl-specifier-seq should
20749 specify a type. */
20750 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20752 /* If there was no decl-specifier-seq, and the next token is a
20753 `;', then we have something like:
20755 struct S { ; };
20757 [class.mem]
20759 Each member-declaration shall declare at least one member
20760 name of the class. */
20761 if (!decl_specifiers.any_specifiers_p)
20763 cp_token *token = cp_lexer_peek_token (parser->lexer);
20764 if (!in_system_header_at (token->location))
20765 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20767 else
20769 tree type;
20771 /* See if this declaration is a friend. */
20772 friend_p = cp_parser_friend_p (&decl_specifiers);
20773 /* If there were decl-specifiers, check to see if there was
20774 a class-declaration. */
20775 type = check_tag_decl (&decl_specifiers,
20776 /*explicit_type_instantiation_p=*/false);
20777 /* Nested classes have already been added to the class, but
20778 a `friend' needs to be explicitly registered. */
20779 if (friend_p)
20781 /* If the `friend' keyword was present, the friend must
20782 be introduced with a class-key. */
20783 if (!declares_class_or_enum && cxx_dialect < cxx11)
20784 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20785 "in C++03 a class-key must be used "
20786 "when declaring a friend");
20787 /* In this case:
20789 template <typename T> struct A {
20790 friend struct A<T>::B;
20793 A<T>::B will be represented by a TYPENAME_TYPE, and
20794 therefore not recognized by check_tag_decl. */
20795 if (!type)
20797 type = decl_specifiers.type;
20798 if (type && TREE_CODE (type) == TYPE_DECL)
20799 type = TREE_TYPE (type);
20801 if (!type || !TYPE_P (type))
20802 error_at (decl_spec_token_start->location,
20803 "friend declaration does not name a class or "
20804 "function");
20805 else
20806 make_friend_class (current_class_type, type,
20807 /*complain=*/true);
20809 /* If there is no TYPE, an error message will already have
20810 been issued. */
20811 else if (!type || type == error_mark_node)
20813 /* An anonymous aggregate has to be handled specially; such
20814 a declaration really declares a data member (with a
20815 particular type), as opposed to a nested class. */
20816 else if (ANON_AGGR_TYPE_P (type))
20818 /* C++11 9.5/6. */
20819 if (decl_specifiers.storage_class != sc_none)
20820 error_at (decl_spec_token_start->location,
20821 "a storage class on an anonymous aggregate "
20822 "in class scope is not allowed");
20824 /* Remove constructors and such from TYPE, now that we
20825 know it is an anonymous aggregate. */
20826 fixup_anonymous_aggr (type);
20827 /* And make the corresponding data member. */
20828 decl = build_decl (decl_spec_token_start->location,
20829 FIELD_DECL, NULL_TREE, type);
20830 /* Add it to the class. */
20831 finish_member_declaration (decl);
20833 else
20834 cp_parser_check_access_in_redeclaration
20835 (TYPE_NAME (type),
20836 decl_spec_token_start->location);
20839 else
20841 bool assume_semicolon = false;
20843 /* Clear attributes from the decl_specifiers but keep them
20844 around as prefix attributes that apply them to the entity
20845 being declared. */
20846 prefix_attributes = decl_specifiers.attributes;
20847 decl_specifiers.attributes = NULL_TREE;
20849 /* See if these declarations will be friends. */
20850 friend_p = cp_parser_friend_p (&decl_specifiers);
20852 /* Keep going until we hit the `;' at the end of the
20853 declaration. */
20854 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20856 tree attributes = NULL_TREE;
20857 tree first_attribute;
20859 /* Peek at the next token. */
20860 token = cp_lexer_peek_token (parser->lexer);
20862 /* Check for a bitfield declaration. */
20863 if (token->type == CPP_COLON
20864 || (token->type == CPP_NAME
20865 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20866 == CPP_COLON))
20868 tree identifier;
20869 tree width;
20871 /* Get the name of the bitfield. Note that we cannot just
20872 check TOKEN here because it may have been invalidated by
20873 the call to cp_lexer_peek_nth_token above. */
20874 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20875 identifier = cp_parser_identifier (parser);
20876 else
20877 identifier = NULL_TREE;
20879 /* Consume the `:' token. */
20880 cp_lexer_consume_token (parser->lexer);
20881 /* Get the width of the bitfield. */
20882 width
20883 = cp_parser_constant_expression (parser);
20885 /* Look for attributes that apply to the bitfield. */
20886 attributes = cp_parser_attributes_opt (parser);
20887 /* Remember which attributes are prefix attributes and
20888 which are not. */
20889 first_attribute = attributes;
20890 /* Combine the attributes. */
20891 attributes = chainon (prefix_attributes, attributes);
20893 /* Create the bitfield declaration. */
20894 decl = grokbitfield (identifier
20895 ? make_id_declarator (NULL_TREE,
20896 identifier,
20897 sfk_none)
20898 : NULL,
20899 &decl_specifiers,
20900 width,
20901 attributes);
20903 else
20905 cp_declarator *declarator;
20906 tree initializer;
20907 tree asm_specification;
20908 int ctor_dtor_or_conv_p;
20910 /* Parse the declarator. */
20911 declarator
20912 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20913 &ctor_dtor_or_conv_p,
20914 /*parenthesized_p=*/NULL,
20915 /*member_p=*/true,
20916 friend_p);
20918 /* If something went wrong parsing the declarator, make sure
20919 that we at least consume some tokens. */
20920 if (declarator == cp_error_declarator)
20922 /* Skip to the end of the statement. */
20923 cp_parser_skip_to_end_of_statement (parser);
20924 /* If the next token is not a semicolon, that is
20925 probably because we just skipped over the body of
20926 a function. So, we consume a semicolon if
20927 present, but do not issue an error message if it
20928 is not present. */
20929 if (cp_lexer_next_token_is (parser->lexer,
20930 CPP_SEMICOLON))
20931 cp_lexer_consume_token (parser->lexer);
20932 goto out;
20935 if (declares_class_or_enum & 2)
20936 cp_parser_check_for_definition_in_return_type
20937 (declarator, decl_specifiers.type,
20938 decl_specifiers.locations[ds_type_spec]);
20940 /* Look for an asm-specification. */
20941 asm_specification = cp_parser_asm_specification_opt (parser);
20942 /* Look for attributes that apply to the declaration. */
20943 attributes = cp_parser_attributes_opt (parser);
20944 /* Remember which attributes are prefix attributes and
20945 which are not. */
20946 first_attribute = attributes;
20947 /* Combine the attributes. */
20948 attributes = chainon (prefix_attributes, attributes);
20950 /* If it's an `=', then we have a constant-initializer or a
20951 pure-specifier. It is not correct to parse the
20952 initializer before registering the member declaration
20953 since the member declaration should be in scope while
20954 its initializer is processed. However, the rest of the
20955 front end does not yet provide an interface that allows
20956 us to handle this correctly. */
20957 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20959 /* In [class.mem]:
20961 A pure-specifier shall be used only in the declaration of
20962 a virtual function.
20964 A member-declarator can contain a constant-initializer
20965 only if it declares a static member of integral or
20966 enumeration type.
20968 Therefore, if the DECLARATOR is for a function, we look
20969 for a pure-specifier; otherwise, we look for a
20970 constant-initializer. When we call `grokfield', it will
20971 perform more stringent semantics checks. */
20972 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20973 if (function_declarator_p (declarator)
20974 || (decl_specifiers.type
20975 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20976 && declarator->kind == cdk_id
20977 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20978 == FUNCTION_TYPE)))
20979 initializer = cp_parser_pure_specifier (parser);
20980 else if (decl_specifiers.storage_class != sc_static)
20981 initializer = cp_parser_save_nsdmi (parser);
20982 else if (cxx_dialect >= cxx11)
20984 bool nonconst;
20985 /* Don't require a constant rvalue in C++11, since we
20986 might want a reference constant. We'll enforce
20987 constancy later. */
20988 cp_lexer_consume_token (parser->lexer);
20989 /* Parse the initializer. */
20990 initializer = cp_parser_initializer_clause (parser,
20991 &nonconst);
20993 else
20994 /* Parse the initializer. */
20995 initializer = cp_parser_constant_initializer (parser);
20997 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20998 && !function_declarator_p (declarator))
21000 bool x;
21001 if (decl_specifiers.storage_class != sc_static)
21002 initializer = cp_parser_save_nsdmi (parser);
21003 else
21004 initializer = cp_parser_initializer (parser, &x, &x);
21006 /* Otherwise, there is no initializer. */
21007 else
21008 initializer = NULL_TREE;
21010 /* See if we are probably looking at a function
21011 definition. We are certainly not looking at a
21012 member-declarator. Calling `grokfield' has
21013 side-effects, so we must not do it unless we are sure
21014 that we are looking at a member-declarator. */
21015 if (cp_parser_token_starts_function_definition_p
21016 (cp_lexer_peek_token (parser->lexer)))
21018 /* The grammar does not allow a pure-specifier to be
21019 used when a member function is defined. (It is
21020 possible that this fact is an oversight in the
21021 standard, since a pure function may be defined
21022 outside of the class-specifier. */
21023 if (initializer && initializer_token_start)
21024 error_at (initializer_token_start->location,
21025 "pure-specifier on function-definition");
21026 decl = cp_parser_save_member_function_body (parser,
21027 &decl_specifiers,
21028 declarator,
21029 attributes);
21030 if (parser->fully_implicit_function_template_p)
21031 decl = finish_fully_implicit_template (parser, decl);
21032 /* If the member was not a friend, declare it here. */
21033 if (!friend_p)
21034 finish_member_declaration (decl);
21035 /* Peek at the next token. */
21036 token = cp_lexer_peek_token (parser->lexer);
21037 /* If the next token is a semicolon, consume it. */
21038 if (token->type == CPP_SEMICOLON)
21039 cp_lexer_consume_token (parser->lexer);
21040 goto out;
21042 else
21043 if (declarator->kind == cdk_function)
21044 declarator->id_loc = token->location;
21045 /* Create the declaration. */
21046 decl = grokfield (declarator, &decl_specifiers,
21047 initializer, /*init_const_expr_p=*/true,
21048 asm_specification, attributes);
21049 if (parser->fully_implicit_function_template_p)
21051 if (friend_p)
21052 finish_fully_implicit_template (parser, 0);
21053 else
21054 decl = finish_fully_implicit_template (parser, decl);
21058 cp_finalize_omp_declare_simd (parser, decl);
21060 /* Reset PREFIX_ATTRIBUTES. */
21061 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21062 attributes = TREE_CHAIN (attributes);
21063 if (attributes)
21064 TREE_CHAIN (attributes) = NULL_TREE;
21066 /* If there is any qualification still in effect, clear it
21067 now; we will be starting fresh with the next declarator. */
21068 parser->scope = NULL_TREE;
21069 parser->qualifying_scope = NULL_TREE;
21070 parser->object_scope = NULL_TREE;
21071 /* If it's a `,', then there are more declarators. */
21072 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21074 cp_lexer_consume_token (parser->lexer);
21075 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21077 cp_token *token = cp_lexer_previous_token (parser->lexer);
21078 error_at (token->location,
21079 "stray %<,%> at end of member declaration");
21082 /* If the next token isn't a `;', then we have a parse error. */
21083 else if (cp_lexer_next_token_is_not (parser->lexer,
21084 CPP_SEMICOLON))
21086 /* The next token might be a ways away from where the
21087 actual semicolon is missing. Find the previous token
21088 and use that for our error position. */
21089 cp_token *token = cp_lexer_previous_token (parser->lexer);
21090 error_at (token->location,
21091 "expected %<;%> at end of member declaration");
21093 /* Assume that the user meant to provide a semicolon. If
21094 we were to cp_parser_skip_to_end_of_statement, we might
21095 skip to a semicolon inside a member function definition
21096 and issue nonsensical error messages. */
21097 assume_semicolon = true;
21100 if (decl)
21102 /* Add DECL to the list of members. */
21103 if (!friend_p
21104 /* Explicitly include, eg, NSDMIs, for better error
21105 recovery (c++/58650). */
21106 || !DECL_DECLARES_FUNCTION_P (decl))
21107 finish_member_declaration (decl);
21109 if (TREE_CODE (decl) == FUNCTION_DECL)
21110 cp_parser_save_default_args (parser, decl);
21111 else if (TREE_CODE (decl) == FIELD_DECL
21112 && !DECL_C_BIT_FIELD (decl)
21113 && DECL_INITIAL (decl))
21114 /* Add DECL to the queue of NSDMI to be parsed later. */
21115 vec_safe_push (unparsed_nsdmis, decl);
21118 if (assume_semicolon)
21119 goto out;
21123 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21124 out:
21125 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21128 /* Parse a pure-specifier.
21130 pure-specifier:
21133 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21134 Otherwise, ERROR_MARK_NODE is returned. */
21136 static tree
21137 cp_parser_pure_specifier (cp_parser* parser)
21139 cp_token *token;
21141 /* Look for the `=' token. */
21142 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21143 return error_mark_node;
21144 /* Look for the `0' token. */
21145 token = cp_lexer_peek_token (parser->lexer);
21147 if (token->type == CPP_EOF
21148 || token->type == CPP_PRAGMA_EOL)
21149 return error_mark_node;
21151 cp_lexer_consume_token (parser->lexer);
21153 /* Accept = default or = delete in c++0x mode. */
21154 if (token->keyword == RID_DEFAULT
21155 || token->keyword == RID_DELETE)
21157 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21158 return token->u.value;
21161 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21162 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21164 cp_parser_error (parser,
21165 "invalid pure specifier (only %<= 0%> is allowed)");
21166 cp_parser_skip_to_end_of_statement (parser);
21167 return error_mark_node;
21169 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21171 error_at (token->location, "templates may not be %<virtual%>");
21172 return error_mark_node;
21175 return integer_zero_node;
21178 /* Parse a constant-initializer.
21180 constant-initializer:
21181 = constant-expression
21183 Returns a representation of the constant-expression. */
21185 static tree
21186 cp_parser_constant_initializer (cp_parser* parser)
21188 /* Look for the `=' token. */
21189 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21190 return error_mark_node;
21192 /* It is invalid to write:
21194 struct S { static const int i = { 7 }; };
21197 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21199 cp_parser_error (parser,
21200 "a brace-enclosed initializer is not allowed here");
21201 /* Consume the opening brace. */
21202 cp_lexer_consume_token (parser->lexer);
21203 /* Skip the initializer. */
21204 cp_parser_skip_to_closing_brace (parser);
21205 /* Look for the trailing `}'. */
21206 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21208 return error_mark_node;
21211 return cp_parser_constant_expression (parser);
21214 /* Derived classes [gram.class.derived] */
21216 /* Parse a base-clause.
21218 base-clause:
21219 : base-specifier-list
21221 base-specifier-list:
21222 base-specifier ... [opt]
21223 base-specifier-list , base-specifier ... [opt]
21225 Returns a TREE_LIST representing the base-classes, in the order in
21226 which they were declared. The representation of each node is as
21227 described by cp_parser_base_specifier.
21229 In the case that no bases are specified, this function will return
21230 NULL_TREE, not ERROR_MARK_NODE. */
21232 static tree
21233 cp_parser_base_clause (cp_parser* parser)
21235 tree bases = NULL_TREE;
21237 /* Look for the `:' that begins the list. */
21238 cp_parser_require (parser, CPP_COLON, RT_COLON);
21240 /* Scan the base-specifier-list. */
21241 while (true)
21243 cp_token *token;
21244 tree base;
21245 bool pack_expansion_p = false;
21247 /* Look for the base-specifier. */
21248 base = cp_parser_base_specifier (parser);
21249 /* Look for the (optional) ellipsis. */
21250 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21252 /* Consume the `...'. */
21253 cp_lexer_consume_token (parser->lexer);
21255 pack_expansion_p = true;
21258 /* Add BASE to the front of the list. */
21259 if (base && base != error_mark_node)
21261 if (pack_expansion_p)
21262 /* Make this a pack expansion type. */
21263 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21265 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21267 TREE_CHAIN (base) = bases;
21268 bases = base;
21271 /* Peek at the next token. */
21272 token = cp_lexer_peek_token (parser->lexer);
21273 /* If it's not a comma, then the list is complete. */
21274 if (token->type != CPP_COMMA)
21275 break;
21276 /* Consume the `,'. */
21277 cp_lexer_consume_token (parser->lexer);
21280 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21281 base class had a qualified name. However, the next name that
21282 appears is certainly not qualified. */
21283 parser->scope = NULL_TREE;
21284 parser->qualifying_scope = NULL_TREE;
21285 parser->object_scope = NULL_TREE;
21287 return nreverse (bases);
21290 /* Parse a base-specifier.
21292 base-specifier:
21293 :: [opt] nested-name-specifier [opt] class-name
21294 virtual access-specifier [opt] :: [opt] nested-name-specifier
21295 [opt] class-name
21296 access-specifier virtual [opt] :: [opt] nested-name-specifier
21297 [opt] class-name
21299 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21300 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21301 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21302 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21304 static tree
21305 cp_parser_base_specifier (cp_parser* parser)
21307 cp_token *token;
21308 bool done = false;
21309 bool virtual_p = false;
21310 bool duplicate_virtual_error_issued_p = false;
21311 bool duplicate_access_error_issued_p = false;
21312 bool class_scope_p, template_p;
21313 tree access = access_default_node;
21314 tree type;
21316 /* Process the optional `virtual' and `access-specifier'. */
21317 while (!done)
21319 /* Peek at the next token. */
21320 token = cp_lexer_peek_token (parser->lexer);
21321 /* Process `virtual'. */
21322 switch (token->keyword)
21324 case RID_VIRTUAL:
21325 /* If `virtual' appears more than once, issue an error. */
21326 if (virtual_p && !duplicate_virtual_error_issued_p)
21328 cp_parser_error (parser,
21329 "%<virtual%> specified more than once in base-specified");
21330 duplicate_virtual_error_issued_p = true;
21333 virtual_p = true;
21335 /* Consume the `virtual' token. */
21336 cp_lexer_consume_token (parser->lexer);
21338 break;
21340 case RID_PUBLIC:
21341 case RID_PROTECTED:
21342 case RID_PRIVATE:
21343 /* If more than one access specifier appears, issue an
21344 error. */
21345 if (access != access_default_node
21346 && !duplicate_access_error_issued_p)
21348 cp_parser_error (parser,
21349 "more than one access specifier in base-specified");
21350 duplicate_access_error_issued_p = true;
21353 access = ridpointers[(int) token->keyword];
21355 /* Consume the access-specifier. */
21356 cp_lexer_consume_token (parser->lexer);
21358 break;
21360 default:
21361 done = true;
21362 break;
21365 /* It is not uncommon to see programs mechanically, erroneously, use
21366 the 'typename' keyword to denote (dependent) qualified types
21367 as base classes. */
21368 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21370 token = cp_lexer_peek_token (parser->lexer);
21371 if (!processing_template_decl)
21372 error_at (token->location,
21373 "keyword %<typename%> not allowed outside of templates");
21374 else
21375 error_at (token->location,
21376 "keyword %<typename%> not allowed in this context "
21377 "(the base class is implicitly a type)");
21378 cp_lexer_consume_token (parser->lexer);
21381 /* Look for the optional `::' operator. */
21382 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21383 /* Look for the nested-name-specifier. The simplest way to
21384 implement:
21386 [temp.res]
21388 The keyword `typename' is not permitted in a base-specifier or
21389 mem-initializer; in these contexts a qualified name that
21390 depends on a template-parameter is implicitly assumed to be a
21391 type name.
21393 is to pretend that we have seen the `typename' keyword at this
21394 point. */
21395 cp_parser_nested_name_specifier_opt (parser,
21396 /*typename_keyword_p=*/true,
21397 /*check_dependency_p=*/true,
21398 typename_type,
21399 /*is_declaration=*/true);
21400 /* If the base class is given by a qualified name, assume that names
21401 we see are type names or templates, as appropriate. */
21402 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21403 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21405 if (!parser->scope
21406 && cp_lexer_next_token_is_decltype (parser->lexer))
21407 /* DR 950 allows decltype as a base-specifier. */
21408 type = cp_parser_decltype (parser);
21409 else
21411 /* Otherwise, look for the class-name. */
21412 type = cp_parser_class_name (parser,
21413 class_scope_p,
21414 template_p,
21415 typename_type,
21416 /*check_dependency_p=*/true,
21417 /*class_head_p=*/false,
21418 /*is_declaration=*/true);
21419 type = TREE_TYPE (type);
21422 if (type == error_mark_node)
21423 return error_mark_node;
21425 return finish_base_specifier (type, access, virtual_p);
21428 /* Exception handling [gram.exception] */
21430 /* Parse an (optional) noexcept-specification.
21432 noexcept-specification:
21433 noexcept ( constant-expression ) [opt]
21435 If no noexcept-specification is present, returns NULL_TREE.
21436 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21437 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21438 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21439 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21440 in which case a boolean condition is returned instead. */
21442 static tree
21443 cp_parser_noexcept_specification_opt (cp_parser* parser,
21444 bool require_constexpr,
21445 bool* consumed_expr,
21446 bool return_cond)
21448 cp_token *token;
21449 const char *saved_message;
21451 /* Peek at the next token. */
21452 token = cp_lexer_peek_token (parser->lexer);
21454 /* Is it a noexcept-specification? */
21455 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21457 tree expr;
21458 cp_lexer_consume_token (parser->lexer);
21460 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21462 cp_lexer_consume_token (parser->lexer);
21464 if (require_constexpr)
21466 /* Types may not be defined in an exception-specification. */
21467 saved_message = parser->type_definition_forbidden_message;
21468 parser->type_definition_forbidden_message
21469 = G_("types may not be defined in an exception-specification");
21471 expr = cp_parser_constant_expression (parser);
21473 /* Restore the saved message. */
21474 parser->type_definition_forbidden_message = saved_message;
21476 else
21478 expr = cp_parser_expression (parser);
21479 *consumed_expr = true;
21482 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21484 else
21486 expr = boolean_true_node;
21487 if (!require_constexpr)
21488 *consumed_expr = false;
21491 /* We cannot build a noexcept-spec right away because this will check
21492 that expr is a constexpr. */
21493 if (!return_cond)
21494 return build_noexcept_spec (expr, tf_warning_or_error);
21495 else
21496 return expr;
21498 else
21499 return NULL_TREE;
21502 /* Parse an (optional) exception-specification.
21504 exception-specification:
21505 throw ( type-id-list [opt] )
21507 Returns a TREE_LIST representing the exception-specification. The
21508 TREE_VALUE of each node is a type. */
21510 static tree
21511 cp_parser_exception_specification_opt (cp_parser* parser)
21513 cp_token *token;
21514 tree type_id_list;
21515 const char *saved_message;
21517 /* Peek at the next token. */
21518 token = cp_lexer_peek_token (parser->lexer);
21520 /* Is it a noexcept-specification? */
21521 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21522 false);
21523 if (type_id_list != NULL_TREE)
21524 return type_id_list;
21526 /* If it's not `throw', then there's no exception-specification. */
21527 if (!cp_parser_is_keyword (token, RID_THROW))
21528 return NULL_TREE;
21530 #if 0
21531 /* Enable this once a lot of code has transitioned to noexcept? */
21532 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21533 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21534 "deprecated in C++0x; use %<noexcept%> instead");
21535 #endif
21537 /* Consume the `throw'. */
21538 cp_lexer_consume_token (parser->lexer);
21540 /* Look for the `('. */
21541 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21543 /* Peek at the next token. */
21544 token = cp_lexer_peek_token (parser->lexer);
21545 /* If it's not a `)', then there is a type-id-list. */
21546 if (token->type != CPP_CLOSE_PAREN)
21548 /* Types may not be defined in an exception-specification. */
21549 saved_message = parser->type_definition_forbidden_message;
21550 parser->type_definition_forbidden_message
21551 = G_("types may not be defined in an exception-specification");
21552 /* Parse the type-id-list. */
21553 type_id_list = cp_parser_type_id_list (parser);
21554 /* Restore the saved message. */
21555 parser->type_definition_forbidden_message = saved_message;
21557 else
21558 type_id_list = empty_except_spec;
21560 /* Look for the `)'. */
21561 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21563 return type_id_list;
21566 /* Parse an (optional) type-id-list.
21568 type-id-list:
21569 type-id ... [opt]
21570 type-id-list , type-id ... [opt]
21572 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21573 in the order that the types were presented. */
21575 static tree
21576 cp_parser_type_id_list (cp_parser* parser)
21578 tree types = NULL_TREE;
21580 while (true)
21582 cp_token *token;
21583 tree type;
21585 /* Get the next type-id. */
21586 type = cp_parser_type_id (parser);
21587 /* Parse the optional ellipsis. */
21588 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21590 /* Consume the `...'. */
21591 cp_lexer_consume_token (parser->lexer);
21593 /* Turn the type into a pack expansion expression. */
21594 type = make_pack_expansion (type);
21596 /* Add it to the list. */
21597 types = add_exception_specifier (types, type, /*complain=*/1);
21598 /* Peek at the next token. */
21599 token = cp_lexer_peek_token (parser->lexer);
21600 /* If it is not a `,', we are done. */
21601 if (token->type != CPP_COMMA)
21602 break;
21603 /* Consume the `,'. */
21604 cp_lexer_consume_token (parser->lexer);
21607 return nreverse (types);
21610 /* Parse a try-block.
21612 try-block:
21613 try compound-statement handler-seq */
21615 static tree
21616 cp_parser_try_block (cp_parser* parser)
21618 tree try_block;
21620 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21621 if (parser->in_function_body
21622 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21623 error ("%<try%> in %<constexpr%> function");
21625 try_block = begin_try_block ();
21626 cp_parser_compound_statement (parser, NULL, true, false);
21627 finish_try_block (try_block);
21628 cp_parser_handler_seq (parser);
21629 finish_handler_sequence (try_block);
21631 return try_block;
21634 /* Parse a function-try-block.
21636 function-try-block:
21637 try ctor-initializer [opt] function-body handler-seq */
21639 static bool
21640 cp_parser_function_try_block (cp_parser* parser)
21642 tree compound_stmt;
21643 tree try_block;
21644 bool ctor_initializer_p;
21646 /* Look for the `try' keyword. */
21647 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21648 return false;
21649 /* Let the rest of the front end know where we are. */
21650 try_block = begin_function_try_block (&compound_stmt);
21651 /* Parse the function-body. */
21652 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21653 (parser, /*in_function_try_block=*/true);
21654 /* We're done with the `try' part. */
21655 finish_function_try_block (try_block);
21656 /* Parse the handlers. */
21657 cp_parser_handler_seq (parser);
21658 /* We're done with the handlers. */
21659 finish_function_handler_sequence (try_block, compound_stmt);
21661 return ctor_initializer_p;
21664 /* Parse a handler-seq.
21666 handler-seq:
21667 handler handler-seq [opt] */
21669 static void
21670 cp_parser_handler_seq (cp_parser* parser)
21672 while (true)
21674 cp_token *token;
21676 /* Parse the handler. */
21677 cp_parser_handler (parser);
21678 /* Peek at the next token. */
21679 token = cp_lexer_peek_token (parser->lexer);
21680 /* If it's not `catch' then there are no more handlers. */
21681 if (!cp_parser_is_keyword (token, RID_CATCH))
21682 break;
21686 /* Parse a handler.
21688 handler:
21689 catch ( exception-declaration ) compound-statement */
21691 static void
21692 cp_parser_handler (cp_parser* parser)
21694 tree handler;
21695 tree declaration;
21697 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21698 handler = begin_handler ();
21699 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21700 declaration = cp_parser_exception_declaration (parser);
21701 finish_handler_parms (declaration, handler);
21702 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21703 cp_parser_compound_statement (parser, NULL, false, false);
21704 finish_handler (handler);
21707 /* Parse an exception-declaration.
21709 exception-declaration:
21710 type-specifier-seq declarator
21711 type-specifier-seq abstract-declarator
21712 type-specifier-seq
21715 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21716 ellipsis variant is used. */
21718 static tree
21719 cp_parser_exception_declaration (cp_parser* parser)
21721 cp_decl_specifier_seq type_specifiers;
21722 cp_declarator *declarator;
21723 const char *saved_message;
21725 /* If it's an ellipsis, it's easy to handle. */
21726 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21728 /* Consume the `...' token. */
21729 cp_lexer_consume_token (parser->lexer);
21730 return NULL_TREE;
21733 /* Types may not be defined in exception-declarations. */
21734 saved_message = parser->type_definition_forbidden_message;
21735 parser->type_definition_forbidden_message
21736 = G_("types may not be defined in exception-declarations");
21738 /* Parse the type-specifier-seq. */
21739 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21740 /*is_trailing_return=*/false,
21741 &type_specifiers);
21742 /* If it's a `)', then there is no declarator. */
21743 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21744 declarator = NULL;
21745 else
21746 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21747 /*ctor_dtor_or_conv_p=*/NULL,
21748 /*parenthesized_p=*/NULL,
21749 /*member_p=*/false,
21750 /*friend_p=*/false);
21752 /* Restore the saved message. */
21753 parser->type_definition_forbidden_message = saved_message;
21755 if (!type_specifiers.any_specifiers_p)
21756 return error_mark_node;
21758 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21761 /* Parse a throw-expression.
21763 throw-expression:
21764 throw assignment-expression [opt]
21766 Returns a THROW_EXPR representing the throw-expression. */
21768 static tree
21769 cp_parser_throw_expression (cp_parser* parser)
21771 tree expression;
21772 cp_token* token;
21774 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21775 token = cp_lexer_peek_token (parser->lexer);
21776 /* Figure out whether or not there is an assignment-expression
21777 following the "throw" keyword. */
21778 if (token->type == CPP_COMMA
21779 || token->type == CPP_SEMICOLON
21780 || token->type == CPP_CLOSE_PAREN
21781 || token->type == CPP_CLOSE_SQUARE
21782 || token->type == CPP_CLOSE_BRACE
21783 || token->type == CPP_COLON)
21784 expression = NULL_TREE;
21785 else
21786 expression = cp_parser_assignment_expression (parser);
21788 return build_throw (expression);
21791 /* GNU Extensions */
21793 /* Parse an (optional) asm-specification.
21795 asm-specification:
21796 asm ( string-literal )
21798 If the asm-specification is present, returns a STRING_CST
21799 corresponding to the string-literal. Otherwise, returns
21800 NULL_TREE. */
21802 static tree
21803 cp_parser_asm_specification_opt (cp_parser* parser)
21805 cp_token *token;
21806 tree asm_specification;
21808 /* Peek at the next token. */
21809 token = cp_lexer_peek_token (parser->lexer);
21810 /* If the next token isn't the `asm' keyword, then there's no
21811 asm-specification. */
21812 if (!cp_parser_is_keyword (token, RID_ASM))
21813 return NULL_TREE;
21815 /* Consume the `asm' token. */
21816 cp_lexer_consume_token (parser->lexer);
21817 /* Look for the `('. */
21818 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21820 /* Look for the string-literal. */
21821 asm_specification = cp_parser_string_literal (parser, false, false);
21823 /* Look for the `)'. */
21824 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21826 return asm_specification;
21829 /* Parse an asm-operand-list.
21831 asm-operand-list:
21832 asm-operand
21833 asm-operand-list , asm-operand
21835 asm-operand:
21836 string-literal ( expression )
21837 [ string-literal ] string-literal ( expression )
21839 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21840 each node is the expression. The TREE_PURPOSE is itself a
21841 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21842 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21843 is a STRING_CST for the string literal before the parenthesis. Returns
21844 ERROR_MARK_NODE if any of the operands are invalid. */
21846 static tree
21847 cp_parser_asm_operand_list (cp_parser* parser)
21849 tree asm_operands = NULL_TREE;
21850 bool invalid_operands = false;
21852 while (true)
21854 tree string_literal;
21855 tree expression;
21856 tree name;
21858 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21860 /* Consume the `[' token. */
21861 cp_lexer_consume_token (parser->lexer);
21862 /* Read the operand name. */
21863 name = cp_parser_identifier (parser);
21864 if (name != error_mark_node)
21865 name = build_string (IDENTIFIER_LENGTH (name),
21866 IDENTIFIER_POINTER (name));
21867 /* Look for the closing `]'. */
21868 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21870 else
21871 name = NULL_TREE;
21872 /* Look for the string-literal. */
21873 string_literal = cp_parser_string_literal (parser, false, false);
21875 /* Look for the `('. */
21876 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21877 /* Parse the expression. */
21878 expression = cp_parser_expression (parser);
21879 /* Look for the `)'. */
21880 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21882 if (name == error_mark_node
21883 || string_literal == error_mark_node
21884 || expression == error_mark_node)
21885 invalid_operands = true;
21887 /* Add this operand to the list. */
21888 asm_operands = tree_cons (build_tree_list (name, string_literal),
21889 expression,
21890 asm_operands);
21891 /* If the next token is not a `,', there are no more
21892 operands. */
21893 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21894 break;
21895 /* Consume the `,'. */
21896 cp_lexer_consume_token (parser->lexer);
21899 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21902 /* Parse an asm-clobber-list.
21904 asm-clobber-list:
21905 string-literal
21906 asm-clobber-list , string-literal
21908 Returns a TREE_LIST, indicating the clobbers in the order that they
21909 appeared. The TREE_VALUE of each node is a STRING_CST. */
21911 static tree
21912 cp_parser_asm_clobber_list (cp_parser* parser)
21914 tree clobbers = NULL_TREE;
21916 while (true)
21918 tree string_literal;
21920 /* Look for the string literal. */
21921 string_literal = cp_parser_string_literal (parser, false, false);
21922 /* Add it to the list. */
21923 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21924 /* If the next token is not a `,', then the list is
21925 complete. */
21926 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21927 break;
21928 /* Consume the `,' token. */
21929 cp_lexer_consume_token (parser->lexer);
21932 return clobbers;
21935 /* Parse an asm-label-list.
21937 asm-label-list:
21938 identifier
21939 asm-label-list , identifier
21941 Returns a TREE_LIST, indicating the labels in the order that they
21942 appeared. The TREE_VALUE of each node is a label. */
21944 static tree
21945 cp_parser_asm_label_list (cp_parser* parser)
21947 tree labels = NULL_TREE;
21949 while (true)
21951 tree identifier, label, name;
21953 /* Look for the identifier. */
21954 identifier = cp_parser_identifier (parser);
21955 if (!error_operand_p (identifier))
21957 label = lookup_label (identifier);
21958 if (TREE_CODE (label) == LABEL_DECL)
21960 TREE_USED (label) = 1;
21961 check_goto (label);
21962 name = build_string (IDENTIFIER_LENGTH (identifier),
21963 IDENTIFIER_POINTER (identifier));
21964 labels = tree_cons (name, label, labels);
21967 /* If the next token is not a `,', then the list is
21968 complete. */
21969 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21970 break;
21971 /* Consume the `,' token. */
21972 cp_lexer_consume_token (parser->lexer);
21975 return nreverse (labels);
21978 /* Return TRUE iff the next tokens in the stream are possibly the
21979 beginning of a GNU extension attribute. */
21981 static bool
21982 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21984 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21987 /* Return TRUE iff the next tokens in the stream are possibly the
21988 beginning of a standard C++-11 attribute specifier. */
21990 static bool
21991 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21993 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21996 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21997 beginning of a standard C++-11 attribute specifier. */
21999 static bool
22000 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22002 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22004 return (cxx_dialect >= cxx11
22005 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22006 || (token->type == CPP_OPEN_SQUARE
22007 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22008 && token->type == CPP_OPEN_SQUARE)));
22011 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22012 beginning of a GNU extension attribute. */
22014 static bool
22015 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22017 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22019 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22022 /* Return true iff the next tokens can be the beginning of either a
22023 GNU attribute list, or a standard C++11 attribute sequence. */
22025 static bool
22026 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22028 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22029 || cp_next_tokens_can_be_std_attribute_p (parser));
22032 /* Return true iff the next Nth tokens can be the beginning of either
22033 a GNU attribute list, or a standard C++11 attribute sequence. */
22035 static bool
22036 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22038 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22039 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22042 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22043 of GNU attributes, or return NULL. */
22045 static tree
22046 cp_parser_attributes_opt (cp_parser *parser)
22048 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22049 return cp_parser_gnu_attributes_opt (parser);
22050 return cp_parser_std_attribute_spec_seq (parser);
22053 #define CILK_SIMD_FN_CLAUSE_MASK \
22054 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22055 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22056 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22057 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22058 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22060 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22061 vector [(<clauses>)] */
22063 static void
22064 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22066 bool first_p = parser->cilk_simd_fn_info == NULL;
22067 cp_token *token = v_token;
22068 if (first_p)
22070 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22071 parser->cilk_simd_fn_info->error_seen = false;
22072 parser->cilk_simd_fn_info->fndecl_seen = false;
22073 parser->cilk_simd_fn_info->tokens = vNULL;
22075 int paren_scope = 0;
22076 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22078 cp_lexer_consume_token (parser->lexer);
22079 v_token = cp_lexer_peek_token (parser->lexer);
22080 paren_scope++;
22082 while (paren_scope > 0)
22084 token = cp_lexer_peek_token (parser->lexer);
22085 if (token->type == CPP_OPEN_PAREN)
22086 paren_scope++;
22087 else if (token->type == CPP_CLOSE_PAREN)
22088 paren_scope--;
22089 /* Do not push the last ')' */
22090 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22091 cp_lexer_consume_token (parser->lexer);
22094 token->type = CPP_PRAGMA_EOL;
22095 parser->lexer->next_token = token;
22096 cp_lexer_consume_token (parser->lexer);
22098 struct cp_token_cache *cp
22099 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22100 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22103 /* Parse an (optional) series of attributes.
22105 attributes:
22106 attributes attribute
22108 attribute:
22109 __attribute__ (( attribute-list [opt] ))
22111 The return value is as for cp_parser_gnu_attribute_list. */
22113 static tree
22114 cp_parser_gnu_attributes_opt (cp_parser* parser)
22116 tree attributes = NULL_TREE;
22118 while (true)
22120 cp_token *token;
22121 tree attribute_list;
22122 bool ok = true;
22124 /* Peek at the next token. */
22125 token = cp_lexer_peek_token (parser->lexer);
22126 /* If it's not `__attribute__', then we're done. */
22127 if (token->keyword != RID_ATTRIBUTE)
22128 break;
22130 /* Consume the `__attribute__' keyword. */
22131 cp_lexer_consume_token (parser->lexer);
22132 /* Look for the two `(' tokens. */
22133 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22134 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22136 /* Peek at the next token. */
22137 token = cp_lexer_peek_token (parser->lexer);
22138 if (token->type != CPP_CLOSE_PAREN)
22139 /* Parse the attribute-list. */
22140 attribute_list = cp_parser_gnu_attribute_list (parser);
22141 else
22142 /* If the next token is a `)', then there is no attribute
22143 list. */
22144 attribute_list = NULL;
22146 /* Look for the two `)' tokens. */
22147 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22148 ok = false;
22149 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22150 ok = false;
22151 if (!ok)
22152 cp_parser_skip_to_end_of_statement (parser);
22154 /* Add these new attributes to the list. */
22155 attributes = chainon (attributes, attribute_list);
22158 return attributes;
22161 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22162 "__vector" or "__vector__." */
22164 static inline bool
22165 is_cilkplus_vector_p (tree name)
22167 if (flag_cilkplus && is_attribute_p ("vector", name))
22168 return true;
22169 return false;
22172 /* Parse a GNU attribute-list.
22174 attribute-list:
22175 attribute
22176 attribute-list , attribute
22178 attribute:
22179 identifier
22180 identifier ( identifier )
22181 identifier ( identifier , expression-list )
22182 identifier ( expression-list )
22184 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22185 to an attribute. The TREE_PURPOSE of each node is the identifier
22186 indicating which attribute is in use. The TREE_VALUE represents
22187 the arguments, if any. */
22189 static tree
22190 cp_parser_gnu_attribute_list (cp_parser* parser)
22192 tree attribute_list = NULL_TREE;
22193 bool save_translate_strings_p = parser->translate_strings_p;
22195 parser->translate_strings_p = false;
22196 while (true)
22198 cp_token *token;
22199 tree identifier;
22200 tree attribute;
22202 /* Look for the identifier. We also allow keywords here; for
22203 example `__attribute__ ((const))' is legal. */
22204 token = cp_lexer_peek_token (parser->lexer);
22205 if (token->type == CPP_NAME
22206 || token->type == CPP_KEYWORD)
22208 tree arguments = NULL_TREE;
22210 /* Consume the token, but save it since we need it for the
22211 SIMD enabled function parsing. */
22212 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22214 /* Save away the identifier that indicates which attribute
22215 this is. */
22216 identifier = (token->type == CPP_KEYWORD)
22217 /* For keywords, use the canonical spelling, not the
22218 parsed identifier. */
22219 ? ridpointers[(int) token->keyword]
22220 : id_token->u.value;
22222 attribute = build_tree_list (identifier, NULL_TREE);
22224 /* Peek at the next token. */
22225 token = cp_lexer_peek_token (parser->lexer);
22226 /* If it's an `(', then parse the attribute arguments. */
22227 if (token->type == CPP_OPEN_PAREN)
22229 vec<tree, va_gc> *vec;
22230 int attr_flag = (attribute_takes_identifier_p (identifier)
22231 ? id_attr : normal_attr);
22232 if (is_cilkplus_vector_p (identifier))
22234 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22235 continue;
22237 else
22238 vec = cp_parser_parenthesized_expression_list
22239 (parser, attr_flag, /*cast_p=*/false,
22240 /*allow_expansion_p=*/false,
22241 /*non_constant_p=*/NULL);
22242 if (vec == NULL)
22243 arguments = error_mark_node;
22244 else
22246 arguments = build_tree_list_vec (vec);
22247 release_tree_vector (vec);
22249 /* Save the arguments away. */
22250 TREE_VALUE (attribute) = arguments;
22252 else if (is_cilkplus_vector_p (identifier))
22254 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22255 continue;
22258 if (arguments != error_mark_node)
22260 /* Add this attribute to the list. */
22261 TREE_CHAIN (attribute) = attribute_list;
22262 attribute_list = attribute;
22265 token = cp_lexer_peek_token (parser->lexer);
22267 /* Now, look for more attributes. If the next token isn't a
22268 `,', we're done. */
22269 if (token->type != CPP_COMMA)
22270 break;
22272 /* Consume the comma and keep going. */
22273 cp_lexer_consume_token (parser->lexer);
22275 parser->translate_strings_p = save_translate_strings_p;
22277 /* We built up the list in reverse order. */
22278 return nreverse (attribute_list);
22281 /* Parse a standard C++11 attribute.
22283 The returned representation is a TREE_LIST which TREE_PURPOSE is
22284 the scoped name of the attribute, and the TREE_VALUE is its
22285 arguments list.
22287 Note that the scoped name of the attribute is itself a TREE_LIST
22288 which TREE_PURPOSE is the namespace of the attribute, and
22289 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22290 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22291 and which TREE_PURPOSE is directly the attribute name.
22293 Clients of the attribute code should use get_attribute_namespace
22294 and get_attribute_name to get the actual namespace and name of
22295 attributes, regardless of their being GNU or C++11 attributes.
22297 attribute:
22298 attribute-token attribute-argument-clause [opt]
22300 attribute-token:
22301 identifier
22302 attribute-scoped-token
22304 attribute-scoped-token:
22305 attribute-namespace :: identifier
22307 attribute-namespace:
22308 identifier
22310 attribute-argument-clause:
22311 ( balanced-token-seq )
22313 balanced-token-seq:
22314 balanced-token [opt]
22315 balanced-token-seq balanced-token
22317 balanced-token:
22318 ( balanced-token-seq )
22319 [ balanced-token-seq ]
22320 { balanced-token-seq }. */
22322 static tree
22323 cp_parser_std_attribute (cp_parser *parser)
22325 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22326 cp_token *token;
22328 /* First, parse name of the the attribute, a.k.a
22329 attribute-token. */
22331 token = cp_lexer_peek_token (parser->lexer);
22332 if (token->type == CPP_NAME)
22333 attr_id = token->u.value;
22334 else if (token->type == CPP_KEYWORD)
22335 attr_id = ridpointers[(int) token->keyword];
22336 else if (token->flags & NAMED_OP)
22337 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22339 if (attr_id == NULL_TREE)
22340 return NULL_TREE;
22342 cp_lexer_consume_token (parser->lexer);
22344 token = cp_lexer_peek_token (parser->lexer);
22345 if (token->type == CPP_SCOPE)
22347 /* We are seeing a scoped attribute token. */
22349 cp_lexer_consume_token (parser->lexer);
22350 attr_ns = attr_id;
22352 token = cp_lexer_consume_token (parser->lexer);
22353 if (token->type == CPP_NAME)
22354 attr_id = token->u.value;
22355 else if (token->type == CPP_KEYWORD)
22356 attr_id = ridpointers[(int) token->keyword];
22357 else
22359 error_at (token->location,
22360 "expected an identifier for the attribute name");
22361 return error_mark_node;
22363 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22364 NULL_TREE);
22365 token = cp_lexer_peek_token (parser->lexer);
22367 else
22369 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22370 NULL_TREE);
22371 /* C++11 noreturn attribute is equivalent to GNU's. */
22372 if (is_attribute_p ("noreturn", attr_id))
22373 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22374 /* C++14 deprecated attribute is equivalent to GNU's. */
22375 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22377 if (cxx_dialect == cxx11)
22378 pedwarn (token->location, OPT_Wpedantic,
22379 "%<deprecated%> is a C++14 feature;"
22380 " use %<gnu::deprecated%>");
22381 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22385 /* Now parse the optional argument clause of the attribute. */
22387 if (token->type != CPP_OPEN_PAREN)
22388 return attribute;
22391 vec<tree, va_gc> *vec;
22392 int attr_flag = normal_attr;
22394 if (attr_ns == get_identifier ("gnu")
22395 && attribute_takes_identifier_p (attr_id))
22396 /* A GNU attribute that takes an identifier in parameter. */
22397 attr_flag = id_attr;
22399 vec = cp_parser_parenthesized_expression_list
22400 (parser, attr_flag, /*cast_p=*/false,
22401 /*allow_expansion_p=*/true,
22402 /*non_constant_p=*/NULL);
22403 if (vec == NULL)
22404 arguments = error_mark_node;
22405 else
22407 arguments = build_tree_list_vec (vec);
22408 release_tree_vector (vec);
22411 if (arguments == error_mark_node)
22412 attribute = error_mark_node;
22413 else
22414 TREE_VALUE (attribute) = arguments;
22417 return attribute;
22420 /* Parse a list of standard C++-11 attributes.
22422 attribute-list:
22423 attribute [opt]
22424 attribute-list , attribute[opt]
22425 attribute ...
22426 attribute-list , attribute ...
22429 static tree
22430 cp_parser_std_attribute_list (cp_parser *parser)
22432 tree attributes = NULL_TREE, attribute = NULL_TREE;
22433 cp_token *token = NULL;
22435 while (true)
22437 attribute = cp_parser_std_attribute (parser);
22438 if (attribute == error_mark_node)
22439 break;
22440 if (attribute != NULL_TREE)
22442 TREE_CHAIN (attribute) = attributes;
22443 attributes = attribute;
22445 token = cp_lexer_peek_token (parser->lexer);
22446 if (token->type != CPP_COMMA)
22447 break;
22448 cp_lexer_consume_token (parser->lexer);
22450 attributes = nreverse (attributes);
22451 return attributes;
22454 /* Parse a standard C++-11 attribute specifier.
22456 attribute-specifier:
22457 [ [ attribute-list ] ]
22458 alignment-specifier
22460 alignment-specifier:
22461 alignas ( type-id ... [opt] )
22462 alignas ( alignment-expression ... [opt] ). */
22464 static tree
22465 cp_parser_std_attribute_spec (cp_parser *parser)
22467 tree attributes = NULL_TREE;
22468 cp_token *token = cp_lexer_peek_token (parser->lexer);
22470 if (token->type == CPP_OPEN_SQUARE
22471 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22473 cp_lexer_consume_token (parser->lexer);
22474 cp_lexer_consume_token (parser->lexer);
22476 attributes = cp_parser_std_attribute_list (parser);
22478 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22479 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22480 cp_parser_skip_to_end_of_statement (parser);
22481 else
22482 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22483 when we are sure that we have actually parsed them. */
22484 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22486 else
22488 tree alignas_expr;
22490 /* Look for an alignment-specifier. */
22492 token = cp_lexer_peek_token (parser->lexer);
22494 if (token->type != CPP_KEYWORD
22495 || token->keyword != RID_ALIGNAS)
22496 return NULL_TREE;
22498 cp_lexer_consume_token (parser->lexer);
22499 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22501 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22503 cp_parser_error (parser, "expected %<(%>");
22504 return error_mark_node;
22507 cp_parser_parse_tentatively (parser);
22508 alignas_expr = cp_parser_type_id (parser);
22510 if (!cp_parser_parse_definitely (parser))
22512 gcc_assert (alignas_expr == error_mark_node
22513 || alignas_expr == NULL_TREE);
22515 alignas_expr =
22516 cp_parser_assignment_expression (parser);
22517 if (alignas_expr == error_mark_node)
22518 cp_parser_skip_to_end_of_statement (parser);
22519 if (alignas_expr == NULL_TREE
22520 || alignas_expr == error_mark_node)
22521 return alignas_expr;
22524 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22526 cp_parser_error (parser, "expected %<)%>");
22527 return error_mark_node;
22530 alignas_expr = cxx_alignas_expr (alignas_expr);
22532 /* Build the C++-11 representation of an 'aligned'
22533 attribute. */
22534 attributes =
22535 build_tree_list (build_tree_list (get_identifier ("gnu"),
22536 get_identifier ("aligned")),
22537 build_tree_list (NULL_TREE, alignas_expr));
22540 return attributes;
22543 /* Parse a standard C++-11 attribute-specifier-seq.
22545 attribute-specifier-seq:
22546 attribute-specifier-seq [opt] attribute-specifier
22549 static tree
22550 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22552 tree attr_specs = NULL;
22554 while (true)
22556 tree attr_spec = cp_parser_std_attribute_spec (parser);
22557 if (attr_spec == NULL_TREE)
22558 break;
22559 if (attr_spec == error_mark_node)
22560 return error_mark_node;
22562 TREE_CHAIN (attr_spec) = attr_specs;
22563 attr_specs = attr_spec;
22566 attr_specs = nreverse (attr_specs);
22567 return attr_specs;
22570 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22571 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22572 current value of the PEDANTIC flag, regardless of whether or not
22573 the `__extension__' keyword is present. The caller is responsible
22574 for restoring the value of the PEDANTIC flag. */
22576 static bool
22577 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22579 /* Save the old value of the PEDANTIC flag. */
22580 *saved_pedantic = pedantic;
22582 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22584 /* Consume the `__extension__' token. */
22585 cp_lexer_consume_token (parser->lexer);
22586 /* We're not being pedantic while the `__extension__' keyword is
22587 in effect. */
22588 pedantic = 0;
22590 return true;
22593 return false;
22596 /* Parse a label declaration.
22598 label-declaration:
22599 __label__ label-declarator-seq ;
22601 label-declarator-seq:
22602 identifier , label-declarator-seq
22603 identifier */
22605 static void
22606 cp_parser_label_declaration (cp_parser* parser)
22608 /* Look for the `__label__' keyword. */
22609 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22611 while (true)
22613 tree identifier;
22615 /* Look for an identifier. */
22616 identifier = cp_parser_identifier (parser);
22617 /* If we failed, stop. */
22618 if (identifier == error_mark_node)
22619 break;
22620 /* Declare it as a label. */
22621 finish_label_decl (identifier);
22622 /* If the next token is a `;', stop. */
22623 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22624 break;
22625 /* Look for the `,' separating the label declarations. */
22626 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22629 /* Look for the final `;'. */
22630 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22633 /* Support Functions */
22635 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22636 NAME should have one of the representations used for an
22637 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22638 is returned. If PARSER->SCOPE is a dependent type, then a
22639 SCOPE_REF is returned.
22641 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22642 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22643 was formed. Abstractly, such entities should not be passed to this
22644 function, because they do not need to be looked up, but it is
22645 simpler to check for this special case here, rather than at the
22646 call-sites.
22648 In cases not explicitly covered above, this function returns a
22649 DECL, OVERLOAD, or baselink representing the result of the lookup.
22650 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22651 is returned.
22653 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22654 (e.g., "struct") that was used. In that case bindings that do not
22655 refer to types are ignored.
22657 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22658 ignored.
22660 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22661 are ignored.
22663 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22664 types.
22666 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22667 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22668 NULL_TREE otherwise. */
22670 static tree
22671 cp_parser_lookup_name (cp_parser *parser, tree name,
22672 enum tag_types tag_type,
22673 bool is_template,
22674 bool is_namespace,
22675 bool check_dependency,
22676 tree *ambiguous_decls,
22677 location_t name_location)
22679 tree decl;
22680 tree object_type = parser->context->object_type;
22682 /* Assume that the lookup will be unambiguous. */
22683 if (ambiguous_decls)
22684 *ambiguous_decls = NULL_TREE;
22686 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22687 no longer valid. Note that if we are parsing tentatively, and
22688 the parse fails, OBJECT_TYPE will be automatically restored. */
22689 parser->context->object_type = NULL_TREE;
22691 if (name == error_mark_node)
22692 return error_mark_node;
22694 /* A template-id has already been resolved; there is no lookup to
22695 do. */
22696 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22697 return name;
22698 if (BASELINK_P (name))
22700 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22701 == TEMPLATE_ID_EXPR);
22702 return name;
22705 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22706 it should already have been checked to make sure that the name
22707 used matches the type being destroyed. */
22708 if (TREE_CODE (name) == BIT_NOT_EXPR)
22710 tree type;
22712 /* Figure out to which type this destructor applies. */
22713 if (parser->scope)
22714 type = parser->scope;
22715 else if (object_type)
22716 type = object_type;
22717 else
22718 type = current_class_type;
22719 /* If that's not a class type, there is no destructor. */
22720 if (!type || !CLASS_TYPE_P (type))
22721 return error_mark_node;
22722 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22723 lazily_declare_fn (sfk_destructor, type);
22724 if (!CLASSTYPE_DESTRUCTORS (type))
22725 return error_mark_node;
22726 /* If it was a class type, return the destructor. */
22727 return CLASSTYPE_DESTRUCTORS (type);
22730 /* By this point, the NAME should be an ordinary identifier. If
22731 the id-expression was a qualified name, the qualifying scope is
22732 stored in PARSER->SCOPE at this point. */
22733 gcc_assert (identifier_p (name));
22735 /* Perform the lookup. */
22736 if (parser->scope)
22738 bool dependent_p;
22740 if (parser->scope == error_mark_node)
22741 return error_mark_node;
22743 /* If the SCOPE is dependent, the lookup must be deferred until
22744 the template is instantiated -- unless we are explicitly
22745 looking up names in uninstantiated templates. Even then, we
22746 cannot look up the name if the scope is not a class type; it
22747 might, for example, be a template type parameter. */
22748 dependent_p = (TYPE_P (parser->scope)
22749 && dependent_scope_p (parser->scope));
22750 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22751 && dependent_p)
22752 /* Defer lookup. */
22753 decl = error_mark_node;
22754 else
22756 tree pushed_scope = NULL_TREE;
22758 /* If PARSER->SCOPE is a dependent type, then it must be a
22759 class type, and we must not be checking dependencies;
22760 otherwise, we would have processed this lookup above. So
22761 that PARSER->SCOPE is not considered a dependent base by
22762 lookup_member, we must enter the scope here. */
22763 if (dependent_p)
22764 pushed_scope = push_scope (parser->scope);
22766 /* If the PARSER->SCOPE is a template specialization, it
22767 may be instantiated during name lookup. In that case,
22768 errors may be issued. Even if we rollback the current
22769 tentative parse, those errors are valid. */
22770 decl = lookup_qualified_name (parser->scope, name,
22771 tag_type != none_type,
22772 /*complain=*/true);
22774 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22775 lookup result and the nested-name-specifier nominates a class C:
22776 * if the name specified after the nested-name-specifier, when
22777 looked up in C, is the injected-class-name of C (Clause 9), or
22778 * if the name specified after the nested-name-specifier is the
22779 same as the identifier or the simple-template-id's template-
22780 name in the last component of the nested-name-specifier,
22781 the name is instead considered to name the constructor of
22782 class C. [ Note: for example, the constructor is not an
22783 acceptable lookup result in an elaborated-type-specifier so
22784 the constructor would not be used in place of the
22785 injected-class-name. --end note ] Such a constructor name
22786 shall be used only in the declarator-id of a declaration that
22787 names a constructor or in a using-declaration. */
22788 if (tag_type == none_type
22789 && DECL_SELF_REFERENCE_P (decl)
22790 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22791 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22792 tag_type != none_type,
22793 /*complain=*/true);
22795 /* If we have a single function from a using decl, pull it out. */
22796 if (TREE_CODE (decl) == OVERLOAD
22797 && !really_overloaded_fn (decl))
22798 decl = OVL_FUNCTION (decl);
22800 if (pushed_scope)
22801 pop_scope (pushed_scope);
22804 /* If the scope is a dependent type and either we deferred lookup or
22805 we did lookup but didn't find the name, rememeber the name. */
22806 if (decl == error_mark_node && TYPE_P (parser->scope)
22807 && dependent_type_p (parser->scope))
22809 if (tag_type)
22811 tree type;
22813 /* The resolution to Core Issue 180 says that `struct
22814 A::B' should be considered a type-name, even if `A'
22815 is dependent. */
22816 type = make_typename_type (parser->scope, name, tag_type,
22817 /*complain=*/tf_error);
22818 if (type != error_mark_node)
22819 decl = TYPE_NAME (type);
22821 else if (is_template
22822 && (cp_parser_next_token_ends_template_argument_p (parser)
22823 || cp_lexer_next_token_is (parser->lexer,
22824 CPP_CLOSE_PAREN)))
22825 decl = make_unbound_class_template (parser->scope,
22826 name, NULL_TREE,
22827 /*complain=*/tf_error);
22828 else
22829 decl = build_qualified_name (/*type=*/NULL_TREE,
22830 parser->scope, name,
22831 is_template);
22833 parser->qualifying_scope = parser->scope;
22834 parser->object_scope = NULL_TREE;
22836 else if (object_type)
22838 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22839 OBJECT_TYPE is not a class. */
22840 if (CLASS_TYPE_P (object_type))
22841 /* If the OBJECT_TYPE is a template specialization, it may
22842 be instantiated during name lookup. In that case, errors
22843 may be issued. Even if we rollback the current tentative
22844 parse, those errors are valid. */
22845 decl = lookup_member (object_type,
22846 name,
22847 /*protect=*/0,
22848 tag_type != none_type,
22849 tf_warning_or_error);
22850 else
22851 decl = NULL_TREE;
22853 if (!decl)
22854 /* Look it up in the enclosing context. */
22855 decl = lookup_name_real (name, tag_type != none_type,
22856 /*nonclass=*/0,
22857 /*block_p=*/true, is_namespace, 0);
22858 parser->object_scope = object_type;
22859 parser->qualifying_scope = NULL_TREE;
22861 else
22863 decl = lookup_name_real (name, tag_type != none_type,
22864 /*nonclass=*/0,
22865 /*block_p=*/true, is_namespace, 0);
22866 parser->qualifying_scope = NULL_TREE;
22867 parser->object_scope = NULL_TREE;
22870 /* If the lookup failed, let our caller know. */
22871 if (!decl || decl == error_mark_node)
22872 return error_mark_node;
22874 /* Pull out the template from an injected-class-name (or multiple). */
22875 if (is_template)
22876 decl = maybe_get_template_decl_from_type_decl (decl);
22878 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22879 if (TREE_CODE (decl) == TREE_LIST)
22881 if (ambiguous_decls)
22882 *ambiguous_decls = decl;
22883 /* The error message we have to print is too complicated for
22884 cp_parser_error, so we incorporate its actions directly. */
22885 if (!cp_parser_simulate_error (parser))
22887 error_at (name_location, "reference to %qD is ambiguous",
22888 name);
22889 print_candidates (decl);
22891 return error_mark_node;
22894 gcc_assert (DECL_P (decl)
22895 || TREE_CODE (decl) == OVERLOAD
22896 || TREE_CODE (decl) == SCOPE_REF
22897 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22898 || BASELINK_P (decl));
22900 /* If we have resolved the name of a member declaration, check to
22901 see if the declaration is accessible. When the name resolves to
22902 set of overloaded functions, accessibility is checked when
22903 overload resolution is done.
22905 During an explicit instantiation, access is not checked at all,
22906 as per [temp.explicit]. */
22907 if (DECL_P (decl))
22908 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22910 maybe_record_typedef_use (decl);
22912 return decl;
22915 /* Like cp_parser_lookup_name, but for use in the typical case where
22916 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22917 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22919 static tree
22920 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22922 return cp_parser_lookup_name (parser, name,
22923 none_type,
22924 /*is_template=*/false,
22925 /*is_namespace=*/false,
22926 /*check_dependency=*/true,
22927 /*ambiguous_decls=*/NULL,
22928 location);
22931 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22932 the current context, return the TYPE_DECL. If TAG_NAME_P is
22933 true, the DECL indicates the class being defined in a class-head,
22934 or declared in an elaborated-type-specifier.
22936 Otherwise, return DECL. */
22938 static tree
22939 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22941 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22942 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22944 struct A {
22945 template <typename T> struct B;
22948 template <typename T> struct A::B {};
22950 Similarly, in an elaborated-type-specifier:
22952 namespace N { struct X{}; }
22954 struct A {
22955 template <typename T> friend struct N::X;
22958 However, if the DECL refers to a class type, and we are in
22959 the scope of the class, then the name lookup automatically
22960 finds the TYPE_DECL created by build_self_reference rather
22961 than a TEMPLATE_DECL. For example, in:
22963 template <class T> struct S {
22964 S s;
22967 there is no need to handle such case. */
22969 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22970 return DECL_TEMPLATE_RESULT (decl);
22972 return decl;
22975 /* If too many, or too few, template-parameter lists apply to the
22976 declarator, issue an error message. Returns TRUE if all went well,
22977 and FALSE otherwise. */
22979 static bool
22980 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22981 cp_declarator *declarator,
22982 location_t declarator_location)
22984 switch (declarator->kind)
22986 case cdk_id:
22988 unsigned num_templates = 0;
22989 tree scope = declarator->u.id.qualifying_scope;
22991 if (scope)
22992 num_templates = num_template_headers_for_class (scope);
22993 else if (TREE_CODE (declarator->u.id.unqualified_name)
22994 == TEMPLATE_ID_EXPR)
22995 /* If the DECLARATOR has the form `X<y>' then it uses one
22996 additional level of template parameters. */
22997 ++num_templates;
22999 return cp_parser_check_template_parameters
23000 (parser, num_templates, declarator_location, declarator);
23003 case cdk_function:
23004 case cdk_array:
23005 case cdk_pointer:
23006 case cdk_reference:
23007 case cdk_ptrmem:
23008 return (cp_parser_check_declarator_template_parameters
23009 (parser, declarator->declarator, declarator_location));
23011 case cdk_error:
23012 return true;
23014 default:
23015 gcc_unreachable ();
23017 return false;
23020 /* NUM_TEMPLATES were used in the current declaration. If that is
23021 invalid, return FALSE and issue an error messages. Otherwise,
23022 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23023 declarator and we can print more accurate diagnostics. */
23025 static bool
23026 cp_parser_check_template_parameters (cp_parser* parser,
23027 unsigned num_templates,
23028 location_t location,
23029 cp_declarator *declarator)
23031 /* If there are the same number of template classes and parameter
23032 lists, that's OK. */
23033 if (parser->num_template_parameter_lists == num_templates)
23034 return true;
23035 /* If there are more, but only one more, then we are referring to a
23036 member template. That's OK too. */
23037 if (parser->num_template_parameter_lists == num_templates + 1)
23038 return true;
23039 /* If there are more template classes than parameter lists, we have
23040 something like:
23042 template <class T> void S<T>::R<T>::f (); */
23043 if (parser->num_template_parameter_lists < num_templates)
23045 if (declarator && !current_function_decl)
23046 error_at (location, "specializing member %<%T::%E%> "
23047 "requires %<template<>%> syntax",
23048 declarator->u.id.qualifying_scope,
23049 declarator->u.id.unqualified_name);
23050 else if (declarator)
23051 error_at (location, "invalid declaration of %<%T::%E%>",
23052 declarator->u.id.qualifying_scope,
23053 declarator->u.id.unqualified_name);
23054 else
23055 error_at (location, "too few template-parameter-lists");
23056 return false;
23058 /* Otherwise, there are too many template parameter lists. We have
23059 something like:
23061 template <class T> template <class U> void S::f(); */
23062 error_at (location, "too many template-parameter-lists");
23063 return false;
23066 /* Parse an optional `::' token indicating that the following name is
23067 from the global namespace. If so, PARSER->SCOPE is set to the
23068 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23069 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23070 Returns the new value of PARSER->SCOPE, if the `::' token is
23071 present, and NULL_TREE otherwise. */
23073 static tree
23074 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23076 cp_token *token;
23078 /* Peek at the next token. */
23079 token = cp_lexer_peek_token (parser->lexer);
23080 /* If we're looking at a `::' token then we're starting from the
23081 global namespace, not our current location. */
23082 if (token->type == CPP_SCOPE)
23084 /* Consume the `::' token. */
23085 cp_lexer_consume_token (parser->lexer);
23086 /* Set the SCOPE so that we know where to start the lookup. */
23087 parser->scope = global_namespace;
23088 parser->qualifying_scope = global_namespace;
23089 parser->object_scope = NULL_TREE;
23091 return parser->scope;
23093 else if (!current_scope_valid_p)
23095 parser->scope = NULL_TREE;
23096 parser->qualifying_scope = NULL_TREE;
23097 parser->object_scope = NULL_TREE;
23100 return NULL_TREE;
23103 /* Returns TRUE if the upcoming token sequence is the start of a
23104 constructor declarator. If FRIEND_P is true, the declarator is
23105 preceded by the `friend' specifier. */
23107 static bool
23108 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23110 bool constructor_p;
23111 bool outside_class_specifier_p;
23112 tree nested_name_specifier;
23113 cp_token *next_token;
23115 /* The common case is that this is not a constructor declarator, so
23116 try to avoid doing lots of work if at all possible. It's not
23117 valid declare a constructor at function scope. */
23118 if (parser->in_function_body)
23119 return false;
23120 /* And only certain tokens can begin a constructor declarator. */
23121 next_token = cp_lexer_peek_token (parser->lexer);
23122 if (next_token->type != CPP_NAME
23123 && next_token->type != CPP_SCOPE
23124 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23125 && next_token->type != CPP_TEMPLATE_ID)
23126 return false;
23128 /* Parse tentatively; we are going to roll back all of the tokens
23129 consumed here. */
23130 cp_parser_parse_tentatively (parser);
23131 /* Assume that we are looking at a constructor declarator. */
23132 constructor_p = true;
23134 /* Look for the optional `::' operator. */
23135 cp_parser_global_scope_opt (parser,
23136 /*current_scope_valid_p=*/false);
23137 /* Look for the nested-name-specifier. */
23138 nested_name_specifier
23139 = (cp_parser_nested_name_specifier_opt (parser,
23140 /*typename_keyword_p=*/false,
23141 /*check_dependency_p=*/false,
23142 /*type_p=*/false,
23143 /*is_declaration=*/false));
23145 outside_class_specifier_p = (!at_class_scope_p ()
23146 || !TYPE_BEING_DEFINED (current_class_type)
23147 || friend_p);
23149 /* Outside of a class-specifier, there must be a
23150 nested-name-specifier. */
23151 if (!nested_name_specifier && outside_class_specifier_p)
23152 constructor_p = false;
23153 else if (nested_name_specifier == error_mark_node)
23154 constructor_p = false;
23156 /* If we have a class scope, this is easy; DR 147 says that S::S always
23157 names the constructor, and no other qualified name could. */
23158 if (constructor_p && nested_name_specifier
23159 && CLASS_TYPE_P (nested_name_specifier))
23161 tree id = cp_parser_unqualified_id (parser,
23162 /*template_keyword_p=*/false,
23163 /*check_dependency_p=*/false,
23164 /*declarator_p=*/true,
23165 /*optional_p=*/false);
23166 if (is_overloaded_fn (id))
23167 id = DECL_NAME (get_first_fn (id));
23168 if (!constructor_name_p (id, nested_name_specifier))
23169 constructor_p = false;
23171 /* If we still think that this might be a constructor-declarator,
23172 look for a class-name. */
23173 else if (constructor_p)
23175 /* If we have:
23177 template <typename T> struct S {
23178 S();
23181 we must recognize that the nested `S' names a class. */
23182 tree type_decl;
23183 type_decl = cp_parser_class_name (parser,
23184 /*typename_keyword_p=*/false,
23185 /*template_keyword_p=*/false,
23186 none_type,
23187 /*check_dependency_p=*/false,
23188 /*class_head_p=*/false,
23189 /*is_declaration=*/false);
23190 /* If there was no class-name, then this is not a constructor.
23191 Otherwise, if we are in a class-specifier and we aren't
23192 handling a friend declaration, check that its type matches
23193 current_class_type (c++/38313). Note: error_mark_node
23194 is left alone for error recovery purposes. */
23195 constructor_p = (!cp_parser_error_occurred (parser)
23196 && (outside_class_specifier_p
23197 || type_decl == error_mark_node
23198 || same_type_p (current_class_type,
23199 TREE_TYPE (type_decl))));
23201 /* If we're still considering a constructor, we have to see a `(',
23202 to begin the parameter-declaration-clause, followed by either a
23203 `)', an `...', or a decl-specifier. We need to check for a
23204 type-specifier to avoid being fooled into thinking that:
23206 S (f) (int);
23208 is a constructor. (It is actually a function named `f' that
23209 takes one parameter (of type `int') and returns a value of type
23210 `S'. */
23211 if (constructor_p
23212 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23213 constructor_p = false;
23215 if (constructor_p
23216 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23217 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23218 /* A parameter declaration begins with a decl-specifier,
23219 which is either the "attribute" keyword, a storage class
23220 specifier, or (usually) a type-specifier. */
23221 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23223 tree type;
23224 tree pushed_scope = NULL_TREE;
23225 unsigned saved_num_template_parameter_lists;
23227 /* Names appearing in the type-specifier should be looked up
23228 in the scope of the class. */
23229 if (current_class_type)
23230 type = NULL_TREE;
23231 else
23233 type = TREE_TYPE (type_decl);
23234 if (TREE_CODE (type) == TYPENAME_TYPE)
23236 type = resolve_typename_type (type,
23237 /*only_current_p=*/false);
23238 if (TREE_CODE (type) == TYPENAME_TYPE)
23240 cp_parser_abort_tentative_parse (parser);
23241 return false;
23244 pushed_scope = push_scope (type);
23247 /* Inside the constructor parameter list, surrounding
23248 template-parameter-lists do not apply. */
23249 saved_num_template_parameter_lists
23250 = parser->num_template_parameter_lists;
23251 parser->num_template_parameter_lists = 0;
23253 /* Look for the type-specifier. */
23254 cp_parser_type_specifier (parser,
23255 CP_PARSER_FLAGS_NONE,
23256 /*decl_specs=*/NULL,
23257 /*is_declarator=*/true,
23258 /*declares_class_or_enum=*/NULL,
23259 /*is_cv_qualifier=*/NULL);
23261 parser->num_template_parameter_lists
23262 = saved_num_template_parameter_lists;
23264 /* Leave the scope of the class. */
23265 if (pushed_scope)
23266 pop_scope (pushed_scope);
23268 constructor_p = !cp_parser_error_occurred (parser);
23272 /* We did not really want to consume any tokens. */
23273 cp_parser_abort_tentative_parse (parser);
23275 return constructor_p;
23278 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23279 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23280 they must be performed once we are in the scope of the function.
23282 Returns the function defined. */
23284 static tree
23285 cp_parser_function_definition_from_specifiers_and_declarator
23286 (cp_parser* parser,
23287 cp_decl_specifier_seq *decl_specifiers,
23288 tree attributes,
23289 const cp_declarator *declarator)
23291 tree fn;
23292 bool success_p;
23294 /* Begin the function-definition. */
23295 success_p = start_function (decl_specifiers, declarator, attributes);
23297 /* The things we're about to see are not directly qualified by any
23298 template headers we've seen thus far. */
23299 reset_specialization ();
23301 /* If there were names looked up in the decl-specifier-seq that we
23302 did not check, check them now. We must wait until we are in the
23303 scope of the function to perform the checks, since the function
23304 might be a friend. */
23305 perform_deferred_access_checks (tf_warning_or_error);
23307 if (success_p)
23309 cp_finalize_omp_declare_simd (parser, current_function_decl);
23310 parser->omp_declare_simd = NULL;
23313 if (!success_p)
23315 /* Skip the entire function. */
23316 cp_parser_skip_to_end_of_block_or_statement (parser);
23317 fn = error_mark_node;
23319 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23321 /* Seen already, skip it. An error message has already been output. */
23322 cp_parser_skip_to_end_of_block_or_statement (parser);
23323 fn = current_function_decl;
23324 current_function_decl = NULL_TREE;
23325 /* If this is a function from a class, pop the nested class. */
23326 if (current_class_name)
23327 pop_nested_class ();
23329 else
23331 timevar_id_t tv;
23332 if (DECL_DECLARED_INLINE_P (current_function_decl))
23333 tv = TV_PARSE_INLINE;
23334 else
23335 tv = TV_PARSE_FUNC;
23336 timevar_push (tv);
23337 fn = cp_parser_function_definition_after_declarator (parser,
23338 /*inline_p=*/false);
23339 timevar_pop (tv);
23342 return fn;
23345 /* Parse the part of a function-definition that follows the
23346 declarator. INLINE_P is TRUE iff this function is an inline
23347 function defined within a class-specifier.
23349 Returns the function defined. */
23351 static tree
23352 cp_parser_function_definition_after_declarator (cp_parser* parser,
23353 bool inline_p)
23355 tree fn;
23356 bool ctor_initializer_p = false;
23357 bool saved_in_unbraced_linkage_specification_p;
23358 bool saved_in_function_body;
23359 unsigned saved_num_template_parameter_lists;
23360 cp_token *token;
23361 bool fully_implicit_function_template_p
23362 = parser->fully_implicit_function_template_p;
23363 parser->fully_implicit_function_template_p = false;
23364 tree implicit_template_parms
23365 = parser->implicit_template_parms;
23366 parser->implicit_template_parms = 0;
23367 cp_binding_level* implicit_template_scope
23368 = parser->implicit_template_scope;
23369 parser->implicit_template_scope = 0;
23371 saved_in_function_body = parser->in_function_body;
23372 parser->in_function_body = true;
23373 /* If the next token is `return', then the code may be trying to
23374 make use of the "named return value" extension that G++ used to
23375 support. */
23376 token = cp_lexer_peek_token (parser->lexer);
23377 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23379 /* Consume the `return' keyword. */
23380 cp_lexer_consume_token (parser->lexer);
23381 /* Look for the identifier that indicates what value is to be
23382 returned. */
23383 cp_parser_identifier (parser);
23384 /* Issue an error message. */
23385 error_at (token->location,
23386 "named return values are no longer supported");
23387 /* Skip tokens until we reach the start of the function body. */
23388 while (true)
23390 cp_token *token = cp_lexer_peek_token (parser->lexer);
23391 if (token->type == CPP_OPEN_BRACE
23392 || token->type == CPP_EOF
23393 || token->type == CPP_PRAGMA_EOL)
23394 break;
23395 cp_lexer_consume_token (parser->lexer);
23398 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23399 anything declared inside `f'. */
23400 saved_in_unbraced_linkage_specification_p
23401 = parser->in_unbraced_linkage_specification_p;
23402 parser->in_unbraced_linkage_specification_p = false;
23403 /* Inside the function, surrounding template-parameter-lists do not
23404 apply. */
23405 saved_num_template_parameter_lists
23406 = parser->num_template_parameter_lists;
23407 parser->num_template_parameter_lists = 0;
23409 start_lambda_scope (current_function_decl);
23411 /* If the next token is `try', `__transaction_atomic', or
23412 `__transaction_relaxed`, then we are looking at either function-try-block
23413 or function-transaction-block. Note that all of these include the
23414 function-body. */
23415 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23416 ctor_initializer_p = cp_parser_function_transaction (parser,
23417 RID_TRANSACTION_ATOMIC);
23418 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23419 RID_TRANSACTION_RELAXED))
23420 ctor_initializer_p = cp_parser_function_transaction (parser,
23421 RID_TRANSACTION_RELAXED);
23422 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23423 ctor_initializer_p = cp_parser_function_try_block (parser);
23424 else
23425 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23426 (parser, /*in_function_try_block=*/false);
23428 finish_lambda_scope ();
23430 /* Finish the function. */
23431 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23432 (inline_p ? 2 : 0));
23433 /* Generate code for it, if necessary. */
23434 expand_or_defer_fn (fn);
23435 /* Restore the saved values. */
23436 parser->in_unbraced_linkage_specification_p
23437 = saved_in_unbraced_linkage_specification_p;
23438 parser->num_template_parameter_lists
23439 = saved_num_template_parameter_lists;
23440 parser->in_function_body = saved_in_function_body;
23442 parser->fully_implicit_function_template_p
23443 = fully_implicit_function_template_p;
23444 parser->implicit_template_parms
23445 = implicit_template_parms;
23446 parser->implicit_template_scope
23447 = implicit_template_scope;
23449 if (parser->fully_implicit_function_template_p)
23450 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23452 return fn;
23455 /* Parse a template-declaration, assuming that the `export' (and
23456 `extern') keywords, if present, has already been scanned. MEMBER_P
23457 is as for cp_parser_template_declaration. */
23459 static void
23460 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23462 tree decl = NULL_TREE;
23463 vec<deferred_access_check, va_gc> *checks;
23464 tree parameter_list;
23465 bool friend_p = false;
23466 bool need_lang_pop;
23467 cp_token *token;
23469 /* Look for the `template' keyword. */
23470 token = cp_lexer_peek_token (parser->lexer);
23471 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23472 return;
23474 /* And the `<'. */
23475 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23476 return;
23477 if (at_class_scope_p () && current_function_decl)
23479 /* 14.5.2.2 [temp.mem]
23481 A local class shall not have member templates. */
23482 error_at (token->location,
23483 "invalid declaration of member template in local class");
23484 cp_parser_skip_to_end_of_block_or_statement (parser);
23485 return;
23487 /* [temp]
23489 A template ... shall not have C linkage. */
23490 if (current_lang_name == lang_name_c)
23492 error_at (token->location, "template with C linkage");
23493 /* Give it C++ linkage to avoid confusing other parts of the
23494 front end. */
23495 push_lang_context (lang_name_cplusplus);
23496 need_lang_pop = true;
23498 else
23499 need_lang_pop = false;
23501 /* We cannot perform access checks on the template parameter
23502 declarations until we know what is being declared, just as we
23503 cannot check the decl-specifier list. */
23504 push_deferring_access_checks (dk_deferred);
23506 /* If the next token is `>', then we have an invalid
23507 specialization. Rather than complain about an invalid template
23508 parameter, issue an error message here. */
23509 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23511 cp_parser_error (parser, "invalid explicit specialization");
23512 begin_specialization ();
23513 parameter_list = NULL_TREE;
23515 else
23517 /* Parse the template parameters. */
23518 parameter_list = cp_parser_template_parameter_list (parser);
23521 /* Get the deferred access checks from the parameter list. These
23522 will be checked once we know what is being declared, as for a
23523 member template the checks must be performed in the scope of the
23524 class containing the member. */
23525 checks = get_deferred_access_checks ();
23527 /* Look for the `>'. */
23528 cp_parser_skip_to_end_of_template_parameter_list (parser);
23529 /* We just processed one more parameter list. */
23530 ++parser->num_template_parameter_lists;
23531 /* If the next token is `template', there are more template
23532 parameters. */
23533 if (cp_lexer_next_token_is_keyword (parser->lexer,
23534 RID_TEMPLATE))
23535 cp_parser_template_declaration_after_export (parser, member_p);
23536 else if (cxx_dialect >= cxx11
23537 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23538 decl = cp_parser_alias_declaration (parser);
23539 else
23541 /* There are no access checks when parsing a template, as we do not
23542 know if a specialization will be a friend. */
23543 push_deferring_access_checks (dk_no_check);
23544 token = cp_lexer_peek_token (parser->lexer);
23545 decl = cp_parser_single_declaration (parser,
23546 checks,
23547 member_p,
23548 /*explicit_specialization_p=*/false,
23549 &friend_p);
23550 pop_deferring_access_checks ();
23552 /* If this is a member template declaration, let the front
23553 end know. */
23554 if (member_p && !friend_p && decl)
23556 if (TREE_CODE (decl) == TYPE_DECL)
23557 cp_parser_check_access_in_redeclaration (decl, token->location);
23559 decl = finish_member_template_decl (decl);
23561 else if (friend_p && decl
23562 && DECL_DECLARES_TYPE_P (decl))
23563 make_friend_class (current_class_type, TREE_TYPE (decl),
23564 /*complain=*/true);
23566 /* We are done with the current parameter list. */
23567 --parser->num_template_parameter_lists;
23569 pop_deferring_access_checks ();
23571 /* Finish up. */
23572 finish_template_decl (parameter_list);
23574 /* Check the template arguments for a literal operator template. */
23575 if (decl
23576 && DECL_DECLARES_FUNCTION_P (decl)
23577 && UDLIT_OPER_P (DECL_NAME (decl)))
23579 bool ok = true;
23580 if (parameter_list == NULL_TREE)
23581 ok = false;
23582 else
23584 int num_parms = TREE_VEC_LENGTH (parameter_list);
23585 if (num_parms == 1)
23587 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23588 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23589 if (TREE_TYPE (parm) != char_type_node
23590 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23591 ok = false;
23593 else if (num_parms == 2 && cxx_dialect >= cxx14)
23595 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23596 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23597 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23598 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23599 if (TREE_TYPE (parm) != TREE_TYPE (type)
23600 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23601 ok = false;
23603 else
23604 ok = false;
23606 if (!ok)
23608 if (cxx_dialect >= cxx14)
23609 error ("literal operator template %qD has invalid parameter list."
23610 " Expected non-type template argument pack <char...>"
23611 " or <typename CharT, CharT...>",
23612 decl);
23613 else
23614 error ("literal operator template %qD has invalid parameter list."
23615 " Expected non-type template argument pack <char...>",
23616 decl);
23619 /* Register member declarations. */
23620 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23621 finish_member_declaration (decl);
23622 /* For the erroneous case of a template with C linkage, we pushed an
23623 implicit C++ linkage scope; exit that scope now. */
23624 if (need_lang_pop)
23625 pop_lang_context ();
23626 /* If DECL is a function template, we must return to parse it later.
23627 (Even though there is no definition, there might be default
23628 arguments that need handling.) */
23629 if (member_p && decl
23630 && DECL_DECLARES_FUNCTION_P (decl))
23631 vec_safe_push (unparsed_funs_with_definitions, decl);
23634 /* Perform the deferred access checks from a template-parameter-list.
23635 CHECKS is a TREE_LIST of access checks, as returned by
23636 get_deferred_access_checks. */
23638 static void
23639 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23641 ++processing_template_parmlist;
23642 perform_access_checks (checks, tf_warning_or_error);
23643 --processing_template_parmlist;
23646 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23647 `function-definition' sequence that follows a template header.
23648 If MEMBER_P is true, this declaration appears in a class scope.
23650 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23651 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23653 static tree
23654 cp_parser_single_declaration (cp_parser* parser,
23655 vec<deferred_access_check, va_gc> *checks,
23656 bool member_p,
23657 bool explicit_specialization_p,
23658 bool* friend_p)
23660 int declares_class_or_enum;
23661 tree decl = NULL_TREE;
23662 cp_decl_specifier_seq decl_specifiers;
23663 bool function_definition_p = false;
23664 cp_token *decl_spec_token_start;
23666 /* This function is only used when processing a template
23667 declaration. */
23668 gcc_assert (innermost_scope_kind () == sk_template_parms
23669 || innermost_scope_kind () == sk_template_spec);
23671 /* Defer access checks until we know what is being declared. */
23672 push_deferring_access_checks (dk_deferred);
23674 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23675 alternative. */
23676 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23677 cp_parser_decl_specifier_seq (parser,
23678 CP_PARSER_FLAGS_OPTIONAL,
23679 &decl_specifiers,
23680 &declares_class_or_enum);
23681 if (friend_p)
23682 *friend_p = cp_parser_friend_p (&decl_specifiers);
23684 /* There are no template typedefs. */
23685 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23687 error_at (decl_spec_token_start->location,
23688 "template declaration of %<typedef%>");
23689 decl = error_mark_node;
23692 /* Gather up the access checks that occurred the
23693 decl-specifier-seq. */
23694 stop_deferring_access_checks ();
23696 /* Check for the declaration of a template class. */
23697 if (declares_class_or_enum)
23699 if (cp_parser_declares_only_class_p (parser))
23701 decl = shadow_tag (&decl_specifiers);
23703 /* In this case:
23705 struct C {
23706 friend template <typename T> struct A<T>::B;
23709 A<T>::B will be represented by a TYPENAME_TYPE, and
23710 therefore not recognized by shadow_tag. */
23711 if (friend_p && *friend_p
23712 && !decl
23713 && decl_specifiers.type
23714 && TYPE_P (decl_specifiers.type))
23715 decl = decl_specifiers.type;
23717 if (decl && decl != error_mark_node)
23718 decl = TYPE_NAME (decl);
23719 else
23720 decl = error_mark_node;
23722 /* Perform access checks for template parameters. */
23723 cp_parser_perform_template_parameter_access_checks (checks);
23727 /* Complain about missing 'typename' or other invalid type names. */
23728 if (!decl_specifiers.any_type_specifiers_p
23729 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23731 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23732 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23733 the rest of this declaration. */
23734 decl = error_mark_node;
23735 goto out;
23738 /* If it's not a template class, try for a template function. If
23739 the next token is a `;', then this declaration does not declare
23740 anything. But, if there were errors in the decl-specifiers, then
23741 the error might well have come from an attempted class-specifier.
23742 In that case, there's no need to warn about a missing declarator. */
23743 if (!decl
23744 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23745 || decl_specifiers.type != error_mark_node))
23747 decl = cp_parser_init_declarator (parser,
23748 &decl_specifiers,
23749 checks,
23750 /*function_definition_allowed_p=*/true,
23751 member_p,
23752 declares_class_or_enum,
23753 &function_definition_p,
23754 NULL, NULL);
23756 /* 7.1.1-1 [dcl.stc]
23758 A storage-class-specifier shall not be specified in an explicit
23759 specialization... */
23760 if (decl
23761 && explicit_specialization_p
23762 && decl_specifiers.storage_class != sc_none)
23764 error_at (decl_spec_token_start->location,
23765 "explicit template specialization cannot have a storage class");
23766 decl = error_mark_node;
23769 if (decl && VAR_P (decl))
23770 check_template_variable (decl);
23773 /* Look for a trailing `;' after the declaration. */
23774 if (!function_definition_p
23775 && (decl == error_mark_node
23776 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23777 cp_parser_skip_to_end_of_block_or_statement (parser);
23779 out:
23780 pop_deferring_access_checks ();
23782 /* Clear any current qualification; whatever comes next is the start
23783 of something new. */
23784 parser->scope = NULL_TREE;
23785 parser->qualifying_scope = NULL_TREE;
23786 parser->object_scope = NULL_TREE;
23788 return decl;
23791 /* Parse a cast-expression that is not the operand of a unary "&". */
23793 static tree
23794 cp_parser_simple_cast_expression (cp_parser *parser)
23796 return cp_parser_cast_expression (parser, /*address_p=*/false,
23797 /*cast_p=*/false, /*decltype*/false, NULL);
23800 /* Parse a functional cast to TYPE. Returns an expression
23801 representing the cast. */
23803 static tree
23804 cp_parser_functional_cast (cp_parser* parser, tree type)
23806 vec<tree, va_gc> *vec;
23807 tree expression_list;
23808 tree cast;
23809 bool nonconst_p;
23811 if (!type)
23812 type = error_mark_node;
23814 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23816 cp_lexer_set_source_position (parser->lexer);
23817 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23818 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23819 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23820 if (TREE_CODE (type) == TYPE_DECL)
23821 type = TREE_TYPE (type);
23822 return finish_compound_literal (type, expression_list,
23823 tf_warning_or_error);
23827 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23828 /*cast_p=*/true,
23829 /*allow_expansion_p=*/true,
23830 /*non_constant_p=*/NULL);
23831 if (vec == NULL)
23832 expression_list = error_mark_node;
23833 else
23835 expression_list = build_tree_list_vec (vec);
23836 release_tree_vector (vec);
23839 cast = build_functional_cast (type, expression_list,
23840 tf_warning_or_error);
23841 /* [expr.const]/1: In an integral constant expression "only type
23842 conversions to integral or enumeration type can be used". */
23843 if (TREE_CODE (type) == TYPE_DECL)
23844 type = TREE_TYPE (type);
23845 if (cast != error_mark_node
23846 && !cast_valid_in_integral_constant_expression_p (type)
23847 && cp_parser_non_integral_constant_expression (parser,
23848 NIC_CONSTRUCTOR))
23849 return error_mark_node;
23850 return cast;
23853 /* Save the tokens that make up the body of a member function defined
23854 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23855 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23856 specifiers applied to the declaration. Returns the FUNCTION_DECL
23857 for the member function. */
23859 static tree
23860 cp_parser_save_member_function_body (cp_parser* parser,
23861 cp_decl_specifier_seq *decl_specifiers,
23862 cp_declarator *declarator,
23863 tree attributes)
23865 cp_token *first;
23866 cp_token *last;
23867 tree fn;
23869 /* Create the FUNCTION_DECL. */
23870 fn = grokmethod (decl_specifiers, declarator, attributes);
23871 cp_finalize_omp_declare_simd (parser, fn);
23872 /* If something went badly wrong, bail out now. */
23873 if (fn == error_mark_node)
23875 /* If there's a function-body, skip it. */
23876 if (cp_parser_token_starts_function_definition_p
23877 (cp_lexer_peek_token (parser->lexer)))
23878 cp_parser_skip_to_end_of_block_or_statement (parser);
23879 return error_mark_node;
23882 /* Remember it, if there default args to post process. */
23883 cp_parser_save_default_args (parser, fn);
23885 /* Save away the tokens that make up the body of the
23886 function. */
23887 first = parser->lexer->next_token;
23888 /* Handle function try blocks. */
23889 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23890 cp_lexer_consume_token (parser->lexer);
23891 /* We can have braced-init-list mem-initializers before the fn body. */
23892 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23894 cp_lexer_consume_token (parser->lexer);
23895 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23897 /* cache_group will stop after an un-nested { } pair, too. */
23898 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23899 break;
23901 /* variadic mem-inits have ... after the ')'. */
23902 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23903 cp_lexer_consume_token (parser->lexer);
23906 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23907 /* Handle function try blocks. */
23908 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23909 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23910 last = parser->lexer->next_token;
23912 /* Save away the inline definition; we will process it when the
23913 class is complete. */
23914 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23915 DECL_PENDING_INLINE_P (fn) = 1;
23917 /* We need to know that this was defined in the class, so that
23918 friend templates are handled correctly. */
23919 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23921 /* Add FN to the queue of functions to be parsed later. */
23922 vec_safe_push (unparsed_funs_with_definitions, fn);
23924 return fn;
23927 /* Save the tokens that make up the in-class initializer for a non-static
23928 data member. Returns a DEFAULT_ARG. */
23930 static tree
23931 cp_parser_save_nsdmi (cp_parser* parser)
23933 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23936 /* Parse a template-argument-list, as well as the trailing ">" (but
23937 not the opening "<"). See cp_parser_template_argument_list for the
23938 return value. */
23940 static tree
23941 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23943 tree arguments;
23944 tree saved_scope;
23945 tree saved_qualifying_scope;
23946 tree saved_object_scope;
23947 bool saved_greater_than_is_operator_p;
23948 int saved_unevaluated_operand;
23949 int saved_inhibit_evaluation_warnings;
23951 /* [temp.names]
23953 When parsing a template-id, the first non-nested `>' is taken as
23954 the end of the template-argument-list rather than a greater-than
23955 operator. */
23956 saved_greater_than_is_operator_p
23957 = parser->greater_than_is_operator_p;
23958 parser->greater_than_is_operator_p = false;
23959 /* Parsing the argument list may modify SCOPE, so we save it
23960 here. */
23961 saved_scope = parser->scope;
23962 saved_qualifying_scope = parser->qualifying_scope;
23963 saved_object_scope = parser->object_scope;
23964 /* We need to evaluate the template arguments, even though this
23965 template-id may be nested within a "sizeof". */
23966 saved_unevaluated_operand = cp_unevaluated_operand;
23967 cp_unevaluated_operand = 0;
23968 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23969 c_inhibit_evaluation_warnings = 0;
23970 /* Parse the template-argument-list itself. */
23971 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23972 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23973 arguments = NULL_TREE;
23974 else
23975 arguments = cp_parser_template_argument_list (parser);
23976 /* Look for the `>' that ends the template-argument-list. If we find
23977 a '>>' instead, it's probably just a typo. */
23978 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23980 if (cxx_dialect != cxx98)
23982 /* In C++0x, a `>>' in a template argument list or cast
23983 expression is considered to be two separate `>'
23984 tokens. So, change the current token to a `>', but don't
23985 consume it: it will be consumed later when the outer
23986 template argument list (or cast expression) is parsed.
23987 Note that this replacement of `>' for `>>' is necessary
23988 even if we are parsing tentatively: in the tentative
23989 case, after calling
23990 cp_parser_enclosed_template_argument_list we will always
23991 throw away all of the template arguments and the first
23992 closing `>', either because the template argument list
23993 was erroneous or because we are replacing those tokens
23994 with a CPP_TEMPLATE_ID token. The second `>' (which will
23995 not have been thrown away) is needed either to close an
23996 outer template argument list or to complete a new-style
23997 cast. */
23998 cp_token *token = cp_lexer_peek_token (parser->lexer);
23999 token->type = CPP_GREATER;
24001 else if (!saved_greater_than_is_operator_p)
24003 /* If we're in a nested template argument list, the '>>' has
24004 to be a typo for '> >'. We emit the error message, but we
24005 continue parsing and we push a '>' as next token, so that
24006 the argument list will be parsed correctly. Note that the
24007 global source location is still on the token before the
24008 '>>', so we need to say explicitly where we want it. */
24009 cp_token *token = cp_lexer_peek_token (parser->lexer);
24010 error_at (token->location, "%<>>%> should be %<> >%> "
24011 "within a nested template argument list");
24013 token->type = CPP_GREATER;
24015 else
24017 /* If this is not a nested template argument list, the '>>'
24018 is a typo for '>'. Emit an error message and continue.
24019 Same deal about the token location, but here we can get it
24020 right by consuming the '>>' before issuing the diagnostic. */
24021 cp_token *token = cp_lexer_consume_token (parser->lexer);
24022 error_at (token->location,
24023 "spurious %<>>%>, use %<>%> to terminate "
24024 "a template argument list");
24027 else
24028 cp_parser_skip_to_end_of_template_parameter_list (parser);
24029 /* The `>' token might be a greater-than operator again now. */
24030 parser->greater_than_is_operator_p
24031 = saved_greater_than_is_operator_p;
24032 /* Restore the SAVED_SCOPE. */
24033 parser->scope = saved_scope;
24034 parser->qualifying_scope = saved_qualifying_scope;
24035 parser->object_scope = saved_object_scope;
24036 cp_unevaluated_operand = saved_unevaluated_operand;
24037 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24039 return arguments;
24042 /* MEMBER_FUNCTION is a member function, or a friend. If default
24043 arguments, or the body of the function have not yet been parsed,
24044 parse them now. */
24046 static void
24047 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24049 timevar_push (TV_PARSE_INMETH);
24050 /* If this member is a template, get the underlying
24051 FUNCTION_DECL. */
24052 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24053 member_function = DECL_TEMPLATE_RESULT (member_function);
24055 /* There should not be any class definitions in progress at this
24056 point; the bodies of members are only parsed outside of all class
24057 definitions. */
24058 gcc_assert (parser->num_classes_being_defined == 0);
24059 /* While we're parsing the member functions we might encounter more
24060 classes. We want to handle them right away, but we don't want
24061 them getting mixed up with functions that are currently in the
24062 queue. */
24063 push_unparsed_function_queues (parser);
24065 /* Make sure that any template parameters are in scope. */
24066 maybe_begin_member_template_processing (member_function);
24068 /* If the body of the function has not yet been parsed, parse it
24069 now. */
24070 if (DECL_PENDING_INLINE_P (member_function))
24072 tree function_scope;
24073 cp_token_cache *tokens;
24075 /* The function is no longer pending; we are processing it. */
24076 tokens = DECL_PENDING_INLINE_INFO (member_function);
24077 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24078 DECL_PENDING_INLINE_P (member_function) = 0;
24080 /* If this is a local class, enter the scope of the containing
24081 function. */
24082 function_scope = current_function_decl;
24083 if (function_scope)
24084 push_function_context ();
24086 /* Push the body of the function onto the lexer stack. */
24087 cp_parser_push_lexer_for_tokens (parser, tokens);
24089 /* Let the front end know that we going to be defining this
24090 function. */
24091 start_preparsed_function (member_function, NULL_TREE,
24092 SF_PRE_PARSED | SF_INCLASS_INLINE);
24094 /* Don't do access checking if it is a templated function. */
24095 if (processing_template_decl)
24096 push_deferring_access_checks (dk_no_check);
24098 /* #pragma omp declare reduction needs special parsing. */
24099 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24101 parser->lexer->in_pragma = true;
24102 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24103 finish_function (/*inline*/2);
24104 cp_check_omp_declare_reduction (member_function);
24106 else
24107 /* Now, parse the body of the function. */
24108 cp_parser_function_definition_after_declarator (parser,
24109 /*inline_p=*/true);
24111 if (processing_template_decl)
24112 pop_deferring_access_checks ();
24114 /* Leave the scope of the containing function. */
24115 if (function_scope)
24116 pop_function_context ();
24117 cp_parser_pop_lexer (parser);
24120 /* Remove any template parameters from the symbol table. */
24121 maybe_end_member_template_processing ();
24123 /* Restore the queue. */
24124 pop_unparsed_function_queues (parser);
24125 timevar_pop (TV_PARSE_INMETH);
24128 /* If DECL contains any default args, remember it on the unparsed
24129 functions queue. */
24131 static void
24132 cp_parser_save_default_args (cp_parser* parser, tree decl)
24134 tree probe;
24136 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24137 probe;
24138 probe = TREE_CHAIN (probe))
24139 if (TREE_PURPOSE (probe))
24141 cp_default_arg_entry entry = {current_class_type, decl};
24142 vec_safe_push (unparsed_funs_with_default_args, entry);
24143 break;
24147 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24148 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24149 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24150 from the parameter-type-list. */
24152 static tree
24153 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24154 tree default_arg, tree parmtype)
24156 cp_token_cache *tokens;
24157 tree parsed_arg;
24158 bool dummy;
24160 if (default_arg == error_mark_node)
24161 return error_mark_node;
24163 /* Push the saved tokens for the default argument onto the parser's
24164 lexer stack. */
24165 tokens = DEFARG_TOKENS (default_arg);
24166 cp_parser_push_lexer_for_tokens (parser, tokens);
24168 start_lambda_scope (decl);
24170 /* Parse the default argument. */
24171 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24172 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24173 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24175 finish_lambda_scope ();
24177 if (parsed_arg == error_mark_node)
24178 cp_parser_skip_to_end_of_statement (parser);
24180 if (!processing_template_decl)
24182 /* In a non-template class, check conversions now. In a template,
24183 we'll wait and instantiate these as needed. */
24184 if (TREE_CODE (decl) == PARM_DECL)
24185 parsed_arg = check_default_argument (parmtype, parsed_arg,
24186 tf_warning_or_error);
24187 else
24188 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24191 /* If the token stream has not been completely used up, then
24192 there was extra junk after the end of the default
24193 argument. */
24194 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24196 if (TREE_CODE (decl) == PARM_DECL)
24197 cp_parser_error (parser, "expected %<,%>");
24198 else
24199 cp_parser_error (parser, "expected %<;%>");
24202 /* Revert to the main lexer. */
24203 cp_parser_pop_lexer (parser);
24205 return parsed_arg;
24208 /* FIELD is a non-static data member with an initializer which we saved for
24209 later; parse it now. */
24211 static void
24212 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24214 tree def;
24216 maybe_begin_member_template_processing (field);
24218 push_unparsed_function_queues (parser);
24219 def = cp_parser_late_parse_one_default_arg (parser, field,
24220 DECL_INITIAL (field),
24221 NULL_TREE);
24222 pop_unparsed_function_queues (parser);
24224 maybe_end_member_template_processing ();
24226 DECL_INITIAL (field) = def;
24229 /* FN is a FUNCTION_DECL which may contains a parameter with an
24230 unparsed DEFAULT_ARG. Parse the default args now. This function
24231 assumes that the current scope is the scope in which the default
24232 argument should be processed. */
24234 static void
24235 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24237 bool saved_local_variables_forbidden_p;
24238 tree parm, parmdecl;
24240 /* While we're parsing the default args, we might (due to the
24241 statement expression extension) encounter more classes. We want
24242 to handle them right away, but we don't want them getting mixed
24243 up with default args that are currently in the queue. */
24244 push_unparsed_function_queues (parser);
24246 /* Local variable names (and the `this' keyword) may not appear
24247 in a default argument. */
24248 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24249 parser->local_variables_forbidden_p = true;
24251 push_defarg_context (fn);
24253 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24254 parmdecl = DECL_ARGUMENTS (fn);
24255 parm && parm != void_list_node;
24256 parm = TREE_CHAIN (parm),
24257 parmdecl = DECL_CHAIN (parmdecl))
24259 tree default_arg = TREE_PURPOSE (parm);
24260 tree parsed_arg;
24261 vec<tree, va_gc> *insts;
24262 tree copy;
24263 unsigned ix;
24265 if (!default_arg)
24266 continue;
24268 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24269 /* This can happen for a friend declaration for a function
24270 already declared with default arguments. */
24271 continue;
24273 parsed_arg
24274 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24275 default_arg,
24276 TREE_VALUE (parm));
24277 if (parsed_arg == error_mark_node)
24279 continue;
24282 TREE_PURPOSE (parm) = parsed_arg;
24284 /* Update any instantiations we've already created. */
24285 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24286 vec_safe_iterate (insts, ix, &copy); ix++)
24287 TREE_PURPOSE (copy) = parsed_arg;
24290 pop_defarg_context ();
24292 /* Make sure no default arg is missing. */
24293 check_default_args (fn);
24295 /* Restore the state of local_variables_forbidden_p. */
24296 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24298 /* Restore the queue. */
24299 pop_unparsed_function_queues (parser);
24302 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24304 sizeof ... ( identifier )
24306 where the 'sizeof' token has already been consumed. */
24308 static tree
24309 cp_parser_sizeof_pack (cp_parser *parser)
24311 /* Consume the `...'. */
24312 cp_lexer_consume_token (parser->lexer);
24313 maybe_warn_variadic_templates ();
24315 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24316 if (paren)
24317 cp_lexer_consume_token (parser->lexer);
24318 else
24319 permerror (cp_lexer_peek_token (parser->lexer)->location,
24320 "%<sizeof...%> argument must be surrounded by parentheses");
24322 cp_token *token = cp_lexer_peek_token (parser->lexer);
24323 tree name = cp_parser_identifier (parser);
24324 if (name == error_mark_node)
24325 return error_mark_node;
24326 /* The name is not qualified. */
24327 parser->scope = NULL_TREE;
24328 parser->qualifying_scope = NULL_TREE;
24329 parser->object_scope = NULL_TREE;
24330 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24331 if (expr == error_mark_node)
24332 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24333 token->location);
24334 if (TREE_CODE (expr) == TYPE_DECL)
24335 expr = TREE_TYPE (expr);
24336 else if (TREE_CODE (expr) == CONST_DECL)
24337 expr = DECL_INITIAL (expr);
24338 expr = make_pack_expansion (expr);
24340 if (paren)
24341 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24343 return expr;
24346 /* Parse the operand of `sizeof' (or a similar operator). Returns
24347 either a TYPE or an expression, depending on the form of the
24348 input. The KEYWORD indicates which kind of expression we have
24349 encountered. */
24351 static tree
24352 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24354 tree expr = NULL_TREE;
24355 const char *saved_message;
24356 char *tmp;
24357 bool saved_integral_constant_expression_p;
24358 bool saved_non_integral_constant_expression_p;
24360 /* If it's a `...', then we are computing the length of a parameter
24361 pack. */
24362 if (keyword == RID_SIZEOF
24363 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24364 return cp_parser_sizeof_pack (parser);
24366 /* Types cannot be defined in a `sizeof' expression. Save away the
24367 old message. */
24368 saved_message = parser->type_definition_forbidden_message;
24369 /* And create the new one. */
24370 tmp = concat ("types may not be defined in %<",
24371 IDENTIFIER_POINTER (ridpointers[keyword]),
24372 "%> expressions", NULL);
24373 parser->type_definition_forbidden_message = tmp;
24375 /* The restrictions on constant-expressions do not apply inside
24376 sizeof expressions. */
24377 saved_integral_constant_expression_p
24378 = parser->integral_constant_expression_p;
24379 saved_non_integral_constant_expression_p
24380 = parser->non_integral_constant_expression_p;
24381 parser->integral_constant_expression_p = false;
24383 /* Do not actually evaluate the expression. */
24384 ++cp_unevaluated_operand;
24385 ++c_inhibit_evaluation_warnings;
24386 /* If it's a `(', then we might be looking at the type-id
24387 construction. */
24388 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24390 tree type = NULL_TREE;
24392 /* We can't be sure yet whether we're looking at a type-id or an
24393 expression. */
24394 cp_parser_parse_tentatively (parser);
24395 /* Note: as a GNU Extension, compound literals are considered
24396 postfix-expressions as they are in C99, so they are valid
24397 arguments to sizeof. See comment in cp_parser_cast_expression
24398 for details. */
24399 if (cp_parser_compound_literal_p (parser))
24400 cp_parser_simulate_error (parser);
24401 else
24403 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24404 parser->in_type_id_in_expr_p = true;
24405 /* Look for the type-id. */
24406 type = cp_parser_type_id (parser);
24407 /* Look for the closing `)'. */
24408 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24409 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24412 /* If all went well, then we're done. */
24413 if (cp_parser_parse_definitely (parser))
24415 cp_decl_specifier_seq decl_specs;
24417 /* Build a trivial decl-specifier-seq. */
24418 clear_decl_specs (&decl_specs);
24419 decl_specs.type = type;
24421 /* Call grokdeclarator to figure out what type this is. */
24422 expr = grokdeclarator (NULL,
24423 &decl_specs,
24424 TYPENAME,
24425 /*initialized=*/0,
24426 /*attrlist=*/NULL);
24430 /* If the type-id production did not work out, then we must be
24431 looking at the unary-expression production. */
24432 if (!expr)
24433 expr = cp_parser_unary_expression (parser);
24435 /* Go back to evaluating expressions. */
24436 --cp_unevaluated_operand;
24437 --c_inhibit_evaluation_warnings;
24439 /* Free the message we created. */
24440 free (tmp);
24441 /* And restore the old one. */
24442 parser->type_definition_forbidden_message = saved_message;
24443 parser->integral_constant_expression_p
24444 = saved_integral_constant_expression_p;
24445 parser->non_integral_constant_expression_p
24446 = saved_non_integral_constant_expression_p;
24448 return expr;
24451 /* If the current declaration has no declarator, return true. */
24453 static bool
24454 cp_parser_declares_only_class_p (cp_parser *parser)
24456 /* If the next token is a `;' or a `,' then there is no
24457 declarator. */
24458 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24459 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24462 /* Update the DECL_SPECS to reflect the storage class indicated by
24463 KEYWORD. */
24465 static void
24466 cp_parser_set_storage_class (cp_parser *parser,
24467 cp_decl_specifier_seq *decl_specs,
24468 enum rid keyword,
24469 cp_token *token)
24471 cp_storage_class storage_class;
24473 if (parser->in_unbraced_linkage_specification_p)
24475 error_at (token->location, "invalid use of %qD in linkage specification",
24476 ridpointers[keyword]);
24477 return;
24479 else if (decl_specs->storage_class != sc_none)
24481 decl_specs->conflicting_specifiers_p = true;
24482 return;
24485 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24486 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24487 && decl_specs->gnu_thread_keyword_p)
24489 pedwarn (decl_specs->locations[ds_thread], 0,
24490 "%<__thread%> before %qD", ridpointers[keyword]);
24493 switch (keyword)
24495 case RID_AUTO:
24496 storage_class = sc_auto;
24497 break;
24498 case RID_REGISTER:
24499 storage_class = sc_register;
24500 break;
24501 case RID_STATIC:
24502 storage_class = sc_static;
24503 break;
24504 case RID_EXTERN:
24505 storage_class = sc_extern;
24506 break;
24507 case RID_MUTABLE:
24508 storage_class = sc_mutable;
24509 break;
24510 default:
24511 gcc_unreachable ();
24513 decl_specs->storage_class = storage_class;
24514 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24516 /* A storage class specifier cannot be applied alongside a typedef
24517 specifier. If there is a typedef specifier present then set
24518 conflicting_specifiers_p which will trigger an error later
24519 on in grokdeclarator. */
24520 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24521 decl_specs->conflicting_specifiers_p = true;
24524 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24525 is true, the type is a class or enum definition. */
24527 static void
24528 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24529 tree type_spec,
24530 cp_token *token,
24531 bool type_definition_p)
24533 decl_specs->any_specifiers_p = true;
24535 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24536 (with, for example, in "typedef int wchar_t;") we remember that
24537 this is what happened. In system headers, we ignore these
24538 declarations so that G++ can work with system headers that are not
24539 C++-safe. */
24540 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24541 && !type_definition_p
24542 && (type_spec == boolean_type_node
24543 || type_spec == char16_type_node
24544 || type_spec == char32_type_node
24545 || type_spec == wchar_type_node)
24546 && (decl_specs->type
24547 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24548 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24549 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24550 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24552 decl_specs->redefined_builtin_type = type_spec;
24553 set_and_check_decl_spec_loc (decl_specs,
24554 ds_redefined_builtin_type_spec,
24555 token);
24556 if (!decl_specs->type)
24558 decl_specs->type = type_spec;
24559 decl_specs->type_definition_p = false;
24560 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24563 else if (decl_specs->type)
24564 decl_specs->multiple_types_p = true;
24565 else
24567 decl_specs->type = type_spec;
24568 decl_specs->type_definition_p = type_definition_p;
24569 decl_specs->redefined_builtin_type = NULL_TREE;
24570 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24574 /* True iff TOKEN is the GNU keyword __thread. */
24576 static bool
24577 token_is__thread (cp_token *token)
24579 gcc_assert (token->keyword == RID_THREAD);
24580 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24583 /* Set the location for a declarator specifier and check if it is
24584 duplicated.
24586 DECL_SPECS is the sequence of declarator specifiers onto which to
24587 set the location.
24589 DS is the single declarator specifier to set which location is to
24590 be set onto the existing sequence of declarators.
24592 LOCATION is the location for the declarator specifier to
24593 consider. */
24595 static void
24596 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24597 cp_decl_spec ds, cp_token *token)
24599 gcc_assert (ds < ds_last);
24601 if (decl_specs == NULL)
24602 return;
24604 source_location location = token->location;
24606 if (decl_specs->locations[ds] == 0)
24608 decl_specs->locations[ds] = location;
24609 if (ds == ds_thread)
24610 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24612 else
24614 if (ds == ds_long)
24616 if (decl_specs->locations[ds_long_long] != 0)
24617 error_at (location,
24618 "%<long long long%> is too long for GCC");
24619 else
24621 decl_specs->locations[ds_long_long] = location;
24622 pedwarn_cxx98 (location,
24623 OPT_Wlong_long,
24624 "ISO C++ 1998 does not support %<long long%>");
24627 else if (ds == ds_thread)
24629 bool gnu = token_is__thread (token);
24630 if (gnu != decl_specs->gnu_thread_keyword_p)
24631 error_at (location,
24632 "both %<__thread%> and %<thread_local%> specified");
24633 else
24634 error_at (location, "duplicate %qD", token->u.value);
24636 else
24638 static const char *const decl_spec_names[] = {
24639 "signed",
24640 "unsigned",
24641 "short",
24642 "long",
24643 "const",
24644 "volatile",
24645 "restrict",
24646 "inline",
24647 "virtual",
24648 "explicit",
24649 "friend",
24650 "typedef",
24651 "using",
24652 "constexpr",
24653 "__complex"
24655 error_at (location,
24656 "duplicate %qs", decl_spec_names[ds]);
24661 /* Return true iff the declarator specifier DS is present in the
24662 sequence of declarator specifiers DECL_SPECS. */
24664 bool
24665 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24666 cp_decl_spec ds)
24668 gcc_assert (ds < ds_last);
24670 if (decl_specs == NULL)
24671 return false;
24673 return decl_specs->locations[ds] != 0;
24676 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24677 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24679 static bool
24680 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24682 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24685 /* Issue an error message indicating that TOKEN_DESC was expected.
24686 If KEYWORD is true, it indicated this function is called by
24687 cp_parser_require_keword and the required token can only be
24688 a indicated keyword. */
24690 static void
24691 cp_parser_required_error (cp_parser *parser,
24692 required_token token_desc,
24693 bool keyword)
24695 switch (token_desc)
24697 case RT_NEW:
24698 cp_parser_error (parser, "expected %<new%>");
24699 return;
24700 case RT_DELETE:
24701 cp_parser_error (parser, "expected %<delete%>");
24702 return;
24703 case RT_RETURN:
24704 cp_parser_error (parser, "expected %<return%>");
24705 return;
24706 case RT_WHILE:
24707 cp_parser_error (parser, "expected %<while%>");
24708 return;
24709 case RT_EXTERN:
24710 cp_parser_error (parser, "expected %<extern%>");
24711 return;
24712 case RT_STATIC_ASSERT:
24713 cp_parser_error (parser, "expected %<static_assert%>");
24714 return;
24715 case RT_DECLTYPE:
24716 cp_parser_error (parser, "expected %<decltype%>");
24717 return;
24718 case RT_OPERATOR:
24719 cp_parser_error (parser, "expected %<operator%>");
24720 return;
24721 case RT_CLASS:
24722 cp_parser_error (parser, "expected %<class%>");
24723 return;
24724 case RT_TEMPLATE:
24725 cp_parser_error (parser, "expected %<template%>");
24726 return;
24727 case RT_NAMESPACE:
24728 cp_parser_error (parser, "expected %<namespace%>");
24729 return;
24730 case RT_USING:
24731 cp_parser_error (parser, "expected %<using%>");
24732 return;
24733 case RT_ASM:
24734 cp_parser_error (parser, "expected %<asm%>");
24735 return;
24736 case RT_TRY:
24737 cp_parser_error (parser, "expected %<try%>");
24738 return;
24739 case RT_CATCH:
24740 cp_parser_error (parser, "expected %<catch%>");
24741 return;
24742 case RT_THROW:
24743 cp_parser_error (parser, "expected %<throw%>");
24744 return;
24745 case RT_LABEL:
24746 cp_parser_error (parser, "expected %<__label__%>");
24747 return;
24748 case RT_AT_TRY:
24749 cp_parser_error (parser, "expected %<@try%>");
24750 return;
24751 case RT_AT_SYNCHRONIZED:
24752 cp_parser_error (parser, "expected %<@synchronized%>");
24753 return;
24754 case RT_AT_THROW:
24755 cp_parser_error (parser, "expected %<@throw%>");
24756 return;
24757 case RT_TRANSACTION_ATOMIC:
24758 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24759 return;
24760 case RT_TRANSACTION_RELAXED:
24761 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24762 return;
24763 default:
24764 break;
24766 if (!keyword)
24768 switch (token_desc)
24770 case RT_SEMICOLON:
24771 cp_parser_error (parser, "expected %<;%>");
24772 return;
24773 case RT_OPEN_PAREN:
24774 cp_parser_error (parser, "expected %<(%>");
24775 return;
24776 case RT_CLOSE_BRACE:
24777 cp_parser_error (parser, "expected %<}%>");
24778 return;
24779 case RT_OPEN_BRACE:
24780 cp_parser_error (parser, "expected %<{%>");
24781 return;
24782 case RT_CLOSE_SQUARE:
24783 cp_parser_error (parser, "expected %<]%>");
24784 return;
24785 case RT_OPEN_SQUARE:
24786 cp_parser_error (parser, "expected %<[%>");
24787 return;
24788 case RT_COMMA:
24789 cp_parser_error (parser, "expected %<,%>");
24790 return;
24791 case RT_SCOPE:
24792 cp_parser_error (parser, "expected %<::%>");
24793 return;
24794 case RT_LESS:
24795 cp_parser_error (parser, "expected %<<%>");
24796 return;
24797 case RT_GREATER:
24798 cp_parser_error (parser, "expected %<>%>");
24799 return;
24800 case RT_EQ:
24801 cp_parser_error (parser, "expected %<=%>");
24802 return;
24803 case RT_ELLIPSIS:
24804 cp_parser_error (parser, "expected %<...%>");
24805 return;
24806 case RT_MULT:
24807 cp_parser_error (parser, "expected %<*%>");
24808 return;
24809 case RT_COMPL:
24810 cp_parser_error (parser, "expected %<~%>");
24811 return;
24812 case RT_COLON:
24813 cp_parser_error (parser, "expected %<:%>");
24814 return;
24815 case RT_COLON_SCOPE:
24816 cp_parser_error (parser, "expected %<:%> or %<::%>");
24817 return;
24818 case RT_CLOSE_PAREN:
24819 cp_parser_error (parser, "expected %<)%>");
24820 return;
24821 case RT_COMMA_CLOSE_PAREN:
24822 cp_parser_error (parser, "expected %<,%> or %<)%>");
24823 return;
24824 case RT_PRAGMA_EOL:
24825 cp_parser_error (parser, "expected end of line");
24826 return;
24827 case RT_NAME:
24828 cp_parser_error (parser, "expected identifier");
24829 return;
24830 case RT_SELECT:
24831 cp_parser_error (parser, "expected selection-statement");
24832 return;
24833 case RT_INTERATION:
24834 cp_parser_error (parser, "expected iteration-statement");
24835 return;
24836 case RT_JUMP:
24837 cp_parser_error (parser, "expected jump-statement");
24838 return;
24839 case RT_CLASS_KEY:
24840 cp_parser_error (parser, "expected class-key");
24841 return;
24842 case RT_CLASS_TYPENAME_TEMPLATE:
24843 cp_parser_error (parser,
24844 "expected %<class%>, %<typename%>, or %<template%>");
24845 return;
24846 default:
24847 gcc_unreachable ();
24850 else
24851 gcc_unreachable ();
24856 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24857 issue an error message indicating that TOKEN_DESC was expected.
24859 Returns the token consumed, if the token had the appropriate type.
24860 Otherwise, returns NULL. */
24862 static cp_token *
24863 cp_parser_require (cp_parser* parser,
24864 enum cpp_ttype type,
24865 required_token token_desc)
24867 if (cp_lexer_next_token_is (parser->lexer, type))
24868 return cp_lexer_consume_token (parser->lexer);
24869 else
24871 /* Output the MESSAGE -- unless we're parsing tentatively. */
24872 if (!cp_parser_simulate_error (parser))
24873 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24874 return NULL;
24878 /* An error message is produced if the next token is not '>'.
24879 All further tokens are skipped until the desired token is
24880 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24882 static void
24883 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24885 /* Current level of '< ... >'. */
24886 unsigned level = 0;
24887 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24888 unsigned nesting_depth = 0;
24890 /* Are we ready, yet? If not, issue error message. */
24891 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24892 return;
24894 /* Skip tokens until the desired token is found. */
24895 while (true)
24897 /* Peek at the next token. */
24898 switch (cp_lexer_peek_token (parser->lexer)->type)
24900 case CPP_LESS:
24901 if (!nesting_depth)
24902 ++level;
24903 break;
24905 case CPP_RSHIFT:
24906 if (cxx_dialect == cxx98)
24907 /* C++0x views the `>>' operator as two `>' tokens, but
24908 C++98 does not. */
24909 break;
24910 else if (!nesting_depth && level-- == 0)
24912 /* We've hit a `>>' where the first `>' closes the
24913 template argument list, and the second `>' is
24914 spurious. Just consume the `>>' and stop; we've
24915 already produced at least one error. */
24916 cp_lexer_consume_token (parser->lexer);
24917 return;
24919 /* Fall through for C++0x, so we handle the second `>' in
24920 the `>>'. */
24922 case CPP_GREATER:
24923 if (!nesting_depth && level-- == 0)
24925 /* We've reached the token we want, consume it and stop. */
24926 cp_lexer_consume_token (parser->lexer);
24927 return;
24929 break;
24931 case CPP_OPEN_PAREN:
24932 case CPP_OPEN_SQUARE:
24933 ++nesting_depth;
24934 break;
24936 case CPP_CLOSE_PAREN:
24937 case CPP_CLOSE_SQUARE:
24938 if (nesting_depth-- == 0)
24939 return;
24940 break;
24942 case CPP_EOF:
24943 case CPP_PRAGMA_EOL:
24944 case CPP_SEMICOLON:
24945 case CPP_OPEN_BRACE:
24946 case CPP_CLOSE_BRACE:
24947 /* The '>' was probably forgotten, don't look further. */
24948 return;
24950 default:
24951 break;
24954 /* Consume this token. */
24955 cp_lexer_consume_token (parser->lexer);
24959 /* If the next token is the indicated keyword, consume it. Otherwise,
24960 issue an error message indicating that TOKEN_DESC was expected.
24962 Returns the token consumed, if the token had the appropriate type.
24963 Otherwise, returns NULL. */
24965 static cp_token *
24966 cp_parser_require_keyword (cp_parser* parser,
24967 enum rid keyword,
24968 required_token token_desc)
24970 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24972 if (token && token->keyword != keyword)
24974 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24975 return NULL;
24978 return token;
24981 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24982 function-definition. */
24984 static bool
24985 cp_parser_token_starts_function_definition_p (cp_token* token)
24987 return (/* An ordinary function-body begins with an `{'. */
24988 token->type == CPP_OPEN_BRACE
24989 /* A ctor-initializer begins with a `:'. */
24990 || token->type == CPP_COLON
24991 /* A function-try-block begins with `try'. */
24992 || token->keyword == RID_TRY
24993 /* A function-transaction-block begins with `__transaction_atomic'
24994 or `__transaction_relaxed'. */
24995 || token->keyword == RID_TRANSACTION_ATOMIC
24996 || token->keyword == RID_TRANSACTION_RELAXED
24997 /* The named return value extension begins with `return'. */
24998 || token->keyword == RID_RETURN);
25001 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25002 definition. */
25004 static bool
25005 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25007 cp_token *token;
25009 token = cp_lexer_peek_token (parser->lexer);
25010 return (token->type == CPP_OPEN_BRACE
25011 || (token->type == CPP_COLON
25012 && !parser->colon_doesnt_start_class_def_p));
25015 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25016 C++0x) ending a template-argument. */
25018 static bool
25019 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25021 cp_token *token;
25023 token = cp_lexer_peek_token (parser->lexer);
25024 return (token->type == CPP_COMMA
25025 || token->type == CPP_GREATER
25026 || token->type == CPP_ELLIPSIS
25027 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25030 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25031 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25033 static bool
25034 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25035 size_t n)
25037 cp_token *token;
25039 token = cp_lexer_peek_nth_token (parser->lexer, n);
25040 if (token->type == CPP_LESS)
25041 return true;
25042 /* Check for the sequence `<::' in the original code. It would be lexed as
25043 `[:', where `[' is a digraph, and there is no whitespace before
25044 `:'. */
25045 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25047 cp_token *token2;
25048 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25049 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25050 return true;
25052 return false;
25055 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25056 or none_type otherwise. */
25058 static enum tag_types
25059 cp_parser_token_is_class_key (cp_token* token)
25061 switch (token->keyword)
25063 case RID_CLASS:
25064 return class_type;
25065 case RID_STRUCT:
25066 return record_type;
25067 case RID_UNION:
25068 return union_type;
25070 default:
25071 return none_type;
25075 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25076 or none_type otherwise or if the token is null. */
25078 static enum tag_types
25079 cp_parser_token_is_type_parameter_key (cp_token* token)
25081 if (!token)
25082 return none_type;
25084 switch (token->keyword)
25086 case RID_CLASS:
25087 return class_type;
25088 case RID_TYPENAME:
25089 return typename_type;
25091 default:
25092 return none_type;
25096 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25098 static void
25099 cp_parser_check_class_key (enum tag_types class_key, tree type)
25101 if (type == error_mark_node)
25102 return;
25103 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25105 if (permerror (input_location, "%qs tag used in naming %q#T",
25106 class_key == union_type ? "union"
25107 : class_key == record_type ? "struct" : "class",
25108 type))
25109 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25110 "%q#T was previously declared here", type);
25114 /* Issue an error message if DECL is redeclared with different
25115 access than its original declaration [class.access.spec/3].
25116 This applies to nested classes and nested class templates.
25117 [class.mem/1]. */
25119 static void
25120 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25122 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25123 return;
25125 if ((TREE_PRIVATE (decl)
25126 != (current_access_specifier == access_private_node))
25127 || (TREE_PROTECTED (decl)
25128 != (current_access_specifier == access_protected_node)))
25129 error_at (location, "%qD redeclared with different access", decl);
25132 /* Look for the `template' keyword, as a syntactic disambiguator.
25133 Return TRUE iff it is present, in which case it will be
25134 consumed. */
25136 static bool
25137 cp_parser_optional_template_keyword (cp_parser *parser)
25139 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25141 /* In C++98 the `template' keyword can only be used within templates;
25142 outside templates the parser can always figure out what is a
25143 template and what is not. In C++11, per the resolution of DR 468,
25144 `template' is allowed in cases where it is not strictly necessary. */
25145 if (!processing_template_decl
25146 && pedantic && cxx_dialect == cxx98)
25148 cp_token *token = cp_lexer_peek_token (parser->lexer);
25149 pedwarn (token->location, OPT_Wpedantic,
25150 "in C++98 %<template%> (as a disambiguator) is only "
25151 "allowed within templates");
25152 /* If this part of the token stream is rescanned, the same
25153 error message would be generated. So, we purge the token
25154 from the stream. */
25155 cp_lexer_purge_token (parser->lexer);
25156 return false;
25158 else
25160 /* Consume the `template' keyword. */
25161 cp_lexer_consume_token (parser->lexer);
25162 return true;
25165 return false;
25168 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25169 set PARSER->SCOPE, and perform other related actions. */
25171 static void
25172 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25174 int i;
25175 struct tree_check *check_value;
25176 deferred_access_check *chk;
25177 vec<deferred_access_check, va_gc> *checks;
25179 /* Get the stored value. */
25180 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25181 /* Perform any access checks that were deferred. */
25182 checks = check_value->checks;
25183 if (checks)
25185 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25186 perform_or_defer_access_check (chk->binfo,
25187 chk->decl,
25188 chk->diag_decl, tf_warning_or_error);
25190 /* Set the scope from the stored value. */
25191 parser->scope = check_value->value;
25192 parser->qualifying_scope = check_value->qualifying_scope;
25193 parser->object_scope = NULL_TREE;
25196 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25197 encounter the end of a block before what we were looking for. */
25199 static bool
25200 cp_parser_cache_group (cp_parser *parser,
25201 enum cpp_ttype end,
25202 unsigned depth)
25204 while (true)
25206 cp_token *token = cp_lexer_peek_token (parser->lexer);
25208 /* Abort a parenthesized expression if we encounter a semicolon. */
25209 if ((end == CPP_CLOSE_PAREN || depth == 0)
25210 && token->type == CPP_SEMICOLON)
25211 return true;
25212 /* If we've reached the end of the file, stop. */
25213 if (token->type == CPP_EOF
25214 || (end != CPP_PRAGMA_EOL
25215 && token->type == CPP_PRAGMA_EOL))
25216 return true;
25217 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25218 /* We've hit the end of an enclosing block, so there's been some
25219 kind of syntax error. */
25220 return true;
25222 /* Consume the token. */
25223 cp_lexer_consume_token (parser->lexer);
25224 /* See if it starts a new group. */
25225 if (token->type == CPP_OPEN_BRACE)
25227 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25228 /* In theory this should probably check end == '}', but
25229 cp_parser_save_member_function_body needs it to exit
25230 after either '}' or ')' when called with ')'. */
25231 if (depth == 0)
25232 return false;
25234 else if (token->type == CPP_OPEN_PAREN)
25236 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25237 if (depth == 0 && end == CPP_CLOSE_PAREN)
25238 return false;
25240 else if (token->type == CPP_PRAGMA)
25241 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25242 else if (token->type == end)
25243 return false;
25247 /* Like above, for caching a default argument or NSDMI. Both of these are
25248 terminated by a non-nested comma, but it can be unclear whether or not a
25249 comma is nested in a template argument list unless we do more parsing.
25250 In order to handle this ambiguity, when we encounter a ',' after a '<'
25251 we try to parse what follows as a parameter-declaration-list (in the
25252 case of a default argument) or a member-declarator (in the case of an
25253 NSDMI). If that succeeds, then we stop caching. */
25255 static tree
25256 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25258 unsigned depth = 0;
25259 int maybe_template_id = 0;
25260 cp_token *first_token;
25261 cp_token *token;
25262 tree default_argument;
25264 /* Add tokens until we have processed the entire default
25265 argument. We add the range [first_token, token). */
25266 first_token = cp_lexer_peek_token (parser->lexer);
25267 if (first_token->type == CPP_OPEN_BRACE)
25269 /* For list-initialization, this is straightforward. */
25270 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25271 token = cp_lexer_peek_token (parser->lexer);
25273 else while (true)
25275 bool done = false;
25277 /* Peek at the next token. */
25278 token = cp_lexer_peek_token (parser->lexer);
25279 /* What we do depends on what token we have. */
25280 switch (token->type)
25282 /* In valid code, a default argument must be
25283 immediately followed by a `,' `)', or `...'. */
25284 case CPP_COMMA:
25285 if (depth == 0 && maybe_template_id)
25287 /* If we've seen a '<', we might be in a
25288 template-argument-list. Until Core issue 325 is
25289 resolved, we don't know how this situation ought
25290 to be handled, so try to DTRT. We check whether
25291 what comes after the comma is a valid parameter
25292 declaration list. If it is, then the comma ends
25293 the default argument; otherwise the default
25294 argument continues. */
25295 bool error = false;
25297 /* Set ITALP so cp_parser_parameter_declaration_list
25298 doesn't decide to commit to this parse. */
25299 bool saved_italp = parser->in_template_argument_list_p;
25300 parser->in_template_argument_list_p = true;
25302 cp_parser_parse_tentatively (parser);
25303 cp_lexer_consume_token (parser->lexer);
25305 if (nsdmi)
25307 int ctor_dtor_or_conv_p;
25308 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25309 &ctor_dtor_or_conv_p,
25310 /*parenthesized_p=*/NULL,
25311 /*member_p=*/true,
25312 /*friend_p=*/false);
25314 else
25316 begin_scope (sk_function_parms, NULL_TREE);
25317 cp_parser_parameter_declaration_list (parser, &error);
25318 pop_bindings_and_leave_scope ();
25320 if (!cp_parser_error_occurred (parser) && !error)
25321 done = true;
25322 cp_parser_abort_tentative_parse (parser);
25324 parser->in_template_argument_list_p = saved_italp;
25325 break;
25327 case CPP_CLOSE_PAREN:
25328 case CPP_ELLIPSIS:
25329 /* If we run into a non-nested `;', `}', or `]',
25330 then the code is invalid -- but the default
25331 argument is certainly over. */
25332 case CPP_SEMICOLON:
25333 case CPP_CLOSE_BRACE:
25334 case CPP_CLOSE_SQUARE:
25335 if (depth == 0
25336 /* Handle correctly int n = sizeof ... ( p ); */
25337 && token->type != CPP_ELLIPSIS)
25338 done = true;
25339 /* Update DEPTH, if necessary. */
25340 else if (token->type == CPP_CLOSE_PAREN
25341 || token->type == CPP_CLOSE_BRACE
25342 || token->type == CPP_CLOSE_SQUARE)
25343 --depth;
25344 break;
25346 case CPP_OPEN_PAREN:
25347 case CPP_OPEN_SQUARE:
25348 case CPP_OPEN_BRACE:
25349 ++depth;
25350 break;
25352 case CPP_LESS:
25353 if (depth == 0)
25354 /* This might be the comparison operator, or it might
25355 start a template argument list. */
25356 ++maybe_template_id;
25357 break;
25359 case CPP_RSHIFT:
25360 if (cxx_dialect == cxx98)
25361 break;
25362 /* Fall through for C++0x, which treats the `>>'
25363 operator like two `>' tokens in certain
25364 cases. */
25366 case CPP_GREATER:
25367 if (depth == 0)
25369 /* This might be an operator, or it might close a
25370 template argument list. But if a previous '<'
25371 started a template argument list, this will have
25372 closed it, so we can't be in one anymore. */
25373 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25374 if (maybe_template_id < 0)
25375 maybe_template_id = 0;
25377 break;
25379 /* If we run out of tokens, issue an error message. */
25380 case CPP_EOF:
25381 case CPP_PRAGMA_EOL:
25382 error_at (token->location, "file ends in default argument");
25383 done = true;
25384 break;
25386 case CPP_NAME:
25387 case CPP_SCOPE:
25388 /* In these cases, we should look for template-ids.
25389 For example, if the default argument is
25390 `X<int, double>()', we need to do name lookup to
25391 figure out whether or not `X' is a template; if
25392 so, the `,' does not end the default argument.
25394 That is not yet done. */
25395 break;
25397 default:
25398 break;
25401 /* If we've reached the end, stop. */
25402 if (done)
25403 break;
25405 /* Add the token to the token block. */
25406 token = cp_lexer_consume_token (parser->lexer);
25409 /* Create a DEFAULT_ARG to represent the unparsed default
25410 argument. */
25411 default_argument = make_node (DEFAULT_ARG);
25412 DEFARG_TOKENS (default_argument)
25413 = cp_token_cache_new (first_token, token);
25414 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25416 return default_argument;
25419 /* Begin parsing tentatively. We always save tokens while parsing
25420 tentatively so that if the tentative parsing fails we can restore the
25421 tokens. */
25423 static void
25424 cp_parser_parse_tentatively (cp_parser* parser)
25426 /* Enter a new parsing context. */
25427 parser->context = cp_parser_context_new (parser->context);
25428 /* Begin saving tokens. */
25429 cp_lexer_save_tokens (parser->lexer);
25430 /* In order to avoid repetitive access control error messages,
25431 access checks are queued up until we are no longer parsing
25432 tentatively. */
25433 push_deferring_access_checks (dk_deferred);
25436 /* Commit to the currently active tentative parse. */
25438 static void
25439 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25441 cp_parser_context *context;
25442 cp_lexer *lexer;
25444 /* Mark all of the levels as committed. */
25445 lexer = parser->lexer;
25446 for (context = parser->context; context->next; context = context->next)
25448 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25449 break;
25450 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25451 while (!cp_lexer_saving_tokens (lexer))
25452 lexer = lexer->next;
25453 cp_lexer_commit_tokens (lexer);
25457 /* Commit to the topmost currently active tentative parse.
25459 Note that this function shouldn't be called when there are
25460 irreversible side-effects while in a tentative state. For
25461 example, we shouldn't create a permanent entry in the symbol
25462 table, or issue an error message that might not apply if the
25463 tentative parse is aborted. */
25465 static void
25466 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25468 cp_parser_context *context = parser->context;
25469 cp_lexer *lexer = parser->lexer;
25471 if (context)
25473 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25474 return;
25475 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25477 while (!cp_lexer_saving_tokens (lexer))
25478 lexer = lexer->next;
25479 cp_lexer_commit_tokens (lexer);
25483 /* Abort the currently active tentative parse. All consumed tokens
25484 will be rolled back, and no diagnostics will be issued. */
25486 static void
25487 cp_parser_abort_tentative_parse (cp_parser* parser)
25489 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25490 || errorcount > 0);
25491 cp_parser_simulate_error (parser);
25492 /* Now, pretend that we want to see if the construct was
25493 successfully parsed. */
25494 cp_parser_parse_definitely (parser);
25497 /* Stop parsing tentatively. If a parse error has occurred, restore the
25498 token stream. Otherwise, commit to the tokens we have consumed.
25499 Returns true if no error occurred; false otherwise. */
25501 static bool
25502 cp_parser_parse_definitely (cp_parser* parser)
25504 bool error_occurred;
25505 cp_parser_context *context;
25507 /* Remember whether or not an error occurred, since we are about to
25508 destroy that information. */
25509 error_occurred = cp_parser_error_occurred (parser);
25510 /* Remove the topmost context from the stack. */
25511 context = parser->context;
25512 parser->context = context->next;
25513 /* If no parse errors occurred, commit to the tentative parse. */
25514 if (!error_occurred)
25516 /* Commit to the tokens read tentatively, unless that was
25517 already done. */
25518 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25519 cp_lexer_commit_tokens (parser->lexer);
25521 pop_to_parent_deferring_access_checks ();
25523 /* Otherwise, if errors occurred, roll back our state so that things
25524 are just as they were before we began the tentative parse. */
25525 else
25527 cp_lexer_rollback_tokens (parser->lexer);
25528 pop_deferring_access_checks ();
25530 /* Add the context to the front of the free list. */
25531 context->next = cp_parser_context_free_list;
25532 cp_parser_context_free_list = context;
25534 return !error_occurred;
25537 /* Returns true if we are parsing tentatively and are not committed to
25538 this tentative parse. */
25540 static bool
25541 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25543 return (cp_parser_parsing_tentatively (parser)
25544 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25547 /* Returns nonzero iff an error has occurred during the most recent
25548 tentative parse. */
25550 static bool
25551 cp_parser_error_occurred (cp_parser* parser)
25553 return (cp_parser_parsing_tentatively (parser)
25554 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25557 /* Returns nonzero if GNU extensions are allowed. */
25559 static bool
25560 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25562 return parser->allow_gnu_extensions_p;
25565 /* Objective-C++ Productions */
25568 /* Parse an Objective-C expression, which feeds into a primary-expression
25569 above.
25571 objc-expression:
25572 objc-message-expression
25573 objc-string-literal
25574 objc-encode-expression
25575 objc-protocol-expression
25576 objc-selector-expression
25578 Returns a tree representation of the expression. */
25580 static tree
25581 cp_parser_objc_expression (cp_parser* parser)
25583 /* Try to figure out what kind of declaration is present. */
25584 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25586 switch (kwd->type)
25588 case CPP_OPEN_SQUARE:
25589 return cp_parser_objc_message_expression (parser);
25591 case CPP_OBJC_STRING:
25592 kwd = cp_lexer_consume_token (parser->lexer);
25593 return objc_build_string_object (kwd->u.value);
25595 case CPP_KEYWORD:
25596 switch (kwd->keyword)
25598 case RID_AT_ENCODE:
25599 return cp_parser_objc_encode_expression (parser);
25601 case RID_AT_PROTOCOL:
25602 return cp_parser_objc_protocol_expression (parser);
25604 case RID_AT_SELECTOR:
25605 return cp_parser_objc_selector_expression (parser);
25607 default:
25608 break;
25610 default:
25611 error_at (kwd->location,
25612 "misplaced %<@%D%> Objective-C++ construct",
25613 kwd->u.value);
25614 cp_parser_skip_to_end_of_block_or_statement (parser);
25617 return error_mark_node;
25620 /* Parse an Objective-C message expression.
25622 objc-message-expression:
25623 [ objc-message-receiver objc-message-args ]
25625 Returns a representation of an Objective-C message. */
25627 static tree
25628 cp_parser_objc_message_expression (cp_parser* parser)
25630 tree receiver, messageargs;
25632 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25633 receiver = cp_parser_objc_message_receiver (parser);
25634 messageargs = cp_parser_objc_message_args (parser);
25635 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25637 return objc_build_message_expr (receiver, messageargs);
25640 /* Parse an objc-message-receiver.
25642 objc-message-receiver:
25643 expression
25644 simple-type-specifier
25646 Returns a representation of the type or expression. */
25648 static tree
25649 cp_parser_objc_message_receiver (cp_parser* parser)
25651 tree rcv;
25653 /* An Objective-C message receiver may be either (1) a type
25654 or (2) an expression. */
25655 cp_parser_parse_tentatively (parser);
25656 rcv = cp_parser_expression (parser);
25658 if (cp_parser_parse_definitely (parser))
25659 return rcv;
25661 rcv = cp_parser_simple_type_specifier (parser,
25662 /*decl_specs=*/NULL,
25663 CP_PARSER_FLAGS_NONE);
25665 return objc_get_class_reference (rcv);
25668 /* Parse the arguments and selectors comprising an Objective-C message.
25670 objc-message-args:
25671 objc-selector
25672 objc-selector-args
25673 objc-selector-args , objc-comma-args
25675 objc-selector-args:
25676 objc-selector [opt] : assignment-expression
25677 objc-selector-args objc-selector [opt] : assignment-expression
25679 objc-comma-args:
25680 assignment-expression
25681 objc-comma-args , assignment-expression
25683 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25684 selector arguments and TREE_VALUE containing a list of comma
25685 arguments. */
25687 static tree
25688 cp_parser_objc_message_args (cp_parser* parser)
25690 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25691 bool maybe_unary_selector_p = true;
25692 cp_token *token = cp_lexer_peek_token (parser->lexer);
25694 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25696 tree selector = NULL_TREE, arg;
25698 if (token->type != CPP_COLON)
25699 selector = cp_parser_objc_selector (parser);
25701 /* Detect if we have a unary selector. */
25702 if (maybe_unary_selector_p
25703 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25704 return build_tree_list (selector, NULL_TREE);
25706 maybe_unary_selector_p = false;
25707 cp_parser_require (parser, CPP_COLON, RT_COLON);
25708 arg = cp_parser_assignment_expression (parser);
25710 sel_args
25711 = chainon (sel_args,
25712 build_tree_list (selector, arg));
25714 token = cp_lexer_peek_token (parser->lexer);
25717 /* Handle non-selector arguments, if any. */
25718 while (token->type == CPP_COMMA)
25720 tree arg;
25722 cp_lexer_consume_token (parser->lexer);
25723 arg = cp_parser_assignment_expression (parser);
25725 addl_args
25726 = chainon (addl_args,
25727 build_tree_list (NULL_TREE, arg));
25729 token = cp_lexer_peek_token (parser->lexer);
25732 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25734 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25735 return build_tree_list (error_mark_node, error_mark_node);
25738 return build_tree_list (sel_args, addl_args);
25741 /* Parse an Objective-C encode expression.
25743 objc-encode-expression:
25744 @encode objc-typename
25746 Returns an encoded representation of the type argument. */
25748 static tree
25749 cp_parser_objc_encode_expression (cp_parser* parser)
25751 tree type;
25752 cp_token *token;
25754 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25755 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25756 token = cp_lexer_peek_token (parser->lexer);
25757 type = complete_type (cp_parser_type_id (parser));
25758 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25760 if (!type)
25762 error_at (token->location,
25763 "%<@encode%> must specify a type as an argument");
25764 return error_mark_node;
25767 /* This happens if we find @encode(T) (where T is a template
25768 typename or something dependent on a template typename) when
25769 parsing a template. In that case, we can't compile it
25770 immediately, but we rather create an AT_ENCODE_EXPR which will
25771 need to be instantiated when the template is used.
25773 if (dependent_type_p (type))
25775 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25776 TREE_READONLY (value) = 1;
25777 return value;
25780 return objc_build_encode_expr (type);
25783 /* Parse an Objective-C @defs expression. */
25785 static tree
25786 cp_parser_objc_defs_expression (cp_parser *parser)
25788 tree name;
25790 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25791 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25792 name = cp_parser_identifier (parser);
25793 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25795 return objc_get_class_ivars (name);
25798 /* Parse an Objective-C protocol expression.
25800 objc-protocol-expression:
25801 @protocol ( identifier )
25803 Returns a representation of the protocol expression. */
25805 static tree
25806 cp_parser_objc_protocol_expression (cp_parser* parser)
25808 tree proto;
25810 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25811 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25812 proto = cp_parser_identifier (parser);
25813 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25815 return objc_build_protocol_expr (proto);
25818 /* Parse an Objective-C selector expression.
25820 objc-selector-expression:
25821 @selector ( objc-method-signature )
25823 objc-method-signature:
25824 objc-selector
25825 objc-selector-seq
25827 objc-selector-seq:
25828 objc-selector :
25829 objc-selector-seq objc-selector :
25831 Returns a representation of the method selector. */
25833 static tree
25834 cp_parser_objc_selector_expression (cp_parser* parser)
25836 tree sel_seq = NULL_TREE;
25837 bool maybe_unary_selector_p = true;
25838 cp_token *token;
25839 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25841 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25842 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25843 token = cp_lexer_peek_token (parser->lexer);
25845 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25846 || token->type == CPP_SCOPE)
25848 tree selector = NULL_TREE;
25850 if (token->type != CPP_COLON
25851 || token->type == CPP_SCOPE)
25852 selector = cp_parser_objc_selector (parser);
25854 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25855 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25857 /* Detect if we have a unary selector. */
25858 if (maybe_unary_selector_p)
25860 sel_seq = selector;
25861 goto finish_selector;
25863 else
25865 cp_parser_error (parser, "expected %<:%>");
25868 maybe_unary_selector_p = false;
25869 token = cp_lexer_consume_token (parser->lexer);
25871 if (token->type == CPP_SCOPE)
25873 sel_seq
25874 = chainon (sel_seq,
25875 build_tree_list (selector, NULL_TREE));
25876 sel_seq
25877 = chainon (sel_seq,
25878 build_tree_list (NULL_TREE, NULL_TREE));
25880 else
25881 sel_seq
25882 = chainon (sel_seq,
25883 build_tree_list (selector, NULL_TREE));
25885 token = cp_lexer_peek_token (parser->lexer);
25888 finish_selector:
25889 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25891 return objc_build_selector_expr (loc, sel_seq);
25894 /* Parse a list of identifiers.
25896 objc-identifier-list:
25897 identifier
25898 objc-identifier-list , identifier
25900 Returns a TREE_LIST of identifier nodes. */
25902 static tree
25903 cp_parser_objc_identifier_list (cp_parser* parser)
25905 tree identifier;
25906 tree list;
25907 cp_token *sep;
25909 identifier = cp_parser_identifier (parser);
25910 if (identifier == error_mark_node)
25911 return error_mark_node;
25913 list = build_tree_list (NULL_TREE, identifier);
25914 sep = cp_lexer_peek_token (parser->lexer);
25916 while (sep->type == CPP_COMMA)
25918 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25919 identifier = cp_parser_identifier (parser);
25920 if (identifier == error_mark_node)
25921 return list;
25923 list = chainon (list, build_tree_list (NULL_TREE,
25924 identifier));
25925 sep = cp_lexer_peek_token (parser->lexer);
25928 return list;
25931 /* Parse an Objective-C alias declaration.
25933 objc-alias-declaration:
25934 @compatibility_alias identifier identifier ;
25936 This function registers the alias mapping with the Objective-C front end.
25937 It returns nothing. */
25939 static void
25940 cp_parser_objc_alias_declaration (cp_parser* parser)
25942 tree alias, orig;
25944 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25945 alias = cp_parser_identifier (parser);
25946 orig = cp_parser_identifier (parser);
25947 objc_declare_alias (alias, orig);
25948 cp_parser_consume_semicolon_at_end_of_statement (parser);
25951 /* Parse an Objective-C class forward-declaration.
25953 objc-class-declaration:
25954 @class objc-identifier-list ;
25956 The function registers the forward declarations with the Objective-C
25957 front end. It returns nothing. */
25959 static void
25960 cp_parser_objc_class_declaration (cp_parser* parser)
25962 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25963 while (true)
25965 tree id;
25967 id = cp_parser_identifier (parser);
25968 if (id == error_mark_node)
25969 break;
25971 objc_declare_class (id);
25973 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25974 cp_lexer_consume_token (parser->lexer);
25975 else
25976 break;
25978 cp_parser_consume_semicolon_at_end_of_statement (parser);
25981 /* Parse a list of Objective-C protocol references.
25983 objc-protocol-refs-opt:
25984 objc-protocol-refs [opt]
25986 objc-protocol-refs:
25987 < objc-identifier-list >
25989 Returns a TREE_LIST of identifiers, if any. */
25991 static tree
25992 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25994 tree protorefs = NULL_TREE;
25996 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25998 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25999 protorefs = cp_parser_objc_identifier_list (parser);
26000 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26003 return protorefs;
26006 /* Parse a Objective-C visibility specification. */
26008 static void
26009 cp_parser_objc_visibility_spec (cp_parser* parser)
26011 cp_token *vis = cp_lexer_peek_token (parser->lexer);
26013 switch (vis->keyword)
26015 case RID_AT_PRIVATE:
26016 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26017 break;
26018 case RID_AT_PROTECTED:
26019 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26020 break;
26021 case RID_AT_PUBLIC:
26022 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26023 break;
26024 case RID_AT_PACKAGE:
26025 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26026 break;
26027 default:
26028 return;
26031 /* Eat '@private'/'@protected'/'@public'. */
26032 cp_lexer_consume_token (parser->lexer);
26035 /* Parse an Objective-C method type. Return 'true' if it is a class
26036 (+) method, and 'false' if it is an instance (-) method. */
26038 static inline bool
26039 cp_parser_objc_method_type (cp_parser* parser)
26041 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26042 return true;
26043 else
26044 return false;
26047 /* Parse an Objective-C protocol qualifier. */
26049 static tree
26050 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26052 tree quals = NULL_TREE, node;
26053 cp_token *token = cp_lexer_peek_token (parser->lexer);
26055 node = token->u.value;
26057 while (node && identifier_p (node)
26058 && (node == ridpointers [(int) RID_IN]
26059 || node == ridpointers [(int) RID_OUT]
26060 || node == ridpointers [(int) RID_INOUT]
26061 || node == ridpointers [(int) RID_BYCOPY]
26062 || node == ridpointers [(int) RID_BYREF]
26063 || node == ridpointers [(int) RID_ONEWAY]))
26065 quals = tree_cons (NULL_TREE, node, quals);
26066 cp_lexer_consume_token (parser->lexer);
26067 token = cp_lexer_peek_token (parser->lexer);
26068 node = token->u.value;
26071 return quals;
26074 /* Parse an Objective-C typename. */
26076 static tree
26077 cp_parser_objc_typename (cp_parser* parser)
26079 tree type_name = NULL_TREE;
26081 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26083 tree proto_quals, cp_type = NULL_TREE;
26085 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26086 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26088 /* An ObjC type name may consist of just protocol qualifiers, in which
26089 case the type shall default to 'id'. */
26090 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26092 cp_type = cp_parser_type_id (parser);
26094 /* If the type could not be parsed, an error has already
26095 been produced. For error recovery, behave as if it had
26096 not been specified, which will use the default type
26097 'id'. */
26098 if (cp_type == error_mark_node)
26100 cp_type = NULL_TREE;
26101 /* We need to skip to the closing parenthesis as
26102 cp_parser_type_id() does not seem to do it for
26103 us. */
26104 cp_parser_skip_to_closing_parenthesis (parser,
26105 /*recovering=*/true,
26106 /*or_comma=*/false,
26107 /*consume_paren=*/false);
26111 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26112 type_name = build_tree_list (proto_quals, cp_type);
26115 return type_name;
26118 /* Check to see if TYPE refers to an Objective-C selector name. */
26120 static bool
26121 cp_parser_objc_selector_p (enum cpp_ttype type)
26123 return (type == CPP_NAME || type == CPP_KEYWORD
26124 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26125 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26126 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26127 || type == CPP_XOR || type == CPP_XOR_EQ);
26130 /* Parse an Objective-C selector. */
26132 static tree
26133 cp_parser_objc_selector (cp_parser* parser)
26135 cp_token *token = cp_lexer_consume_token (parser->lexer);
26137 if (!cp_parser_objc_selector_p (token->type))
26139 error_at (token->location, "invalid Objective-C++ selector name");
26140 return error_mark_node;
26143 /* C++ operator names are allowed to appear in ObjC selectors. */
26144 switch (token->type)
26146 case CPP_AND_AND: return get_identifier ("and");
26147 case CPP_AND_EQ: return get_identifier ("and_eq");
26148 case CPP_AND: return get_identifier ("bitand");
26149 case CPP_OR: return get_identifier ("bitor");
26150 case CPP_COMPL: return get_identifier ("compl");
26151 case CPP_NOT: return get_identifier ("not");
26152 case CPP_NOT_EQ: return get_identifier ("not_eq");
26153 case CPP_OR_OR: return get_identifier ("or");
26154 case CPP_OR_EQ: return get_identifier ("or_eq");
26155 case CPP_XOR: return get_identifier ("xor");
26156 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26157 default: return token->u.value;
26161 /* Parse an Objective-C params list. */
26163 static tree
26164 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26166 tree params = NULL_TREE;
26167 bool maybe_unary_selector_p = true;
26168 cp_token *token = cp_lexer_peek_token (parser->lexer);
26170 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26172 tree selector = NULL_TREE, type_name, identifier;
26173 tree parm_attr = NULL_TREE;
26175 if (token->keyword == RID_ATTRIBUTE)
26176 break;
26178 if (token->type != CPP_COLON)
26179 selector = cp_parser_objc_selector (parser);
26181 /* Detect if we have a unary selector. */
26182 if (maybe_unary_selector_p
26183 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26185 params = selector; /* Might be followed by attributes. */
26186 break;
26189 maybe_unary_selector_p = false;
26190 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26192 /* Something went quite wrong. There should be a colon
26193 here, but there is not. Stop parsing parameters. */
26194 break;
26196 type_name = cp_parser_objc_typename (parser);
26197 /* New ObjC allows attributes on parameters too. */
26198 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26199 parm_attr = cp_parser_attributes_opt (parser);
26200 identifier = cp_parser_identifier (parser);
26202 params
26203 = chainon (params,
26204 objc_build_keyword_decl (selector,
26205 type_name,
26206 identifier,
26207 parm_attr));
26209 token = cp_lexer_peek_token (parser->lexer);
26212 if (params == NULL_TREE)
26214 cp_parser_error (parser, "objective-c++ method declaration is expected");
26215 return error_mark_node;
26218 /* We allow tail attributes for the method. */
26219 if (token->keyword == RID_ATTRIBUTE)
26221 *attributes = cp_parser_attributes_opt (parser);
26222 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26223 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26224 return params;
26225 cp_parser_error (parser,
26226 "method attributes must be specified at the end");
26227 return error_mark_node;
26230 if (params == NULL_TREE)
26232 cp_parser_error (parser, "objective-c++ method declaration is expected");
26233 return error_mark_node;
26235 return params;
26238 /* Parse the non-keyword Objective-C params. */
26240 static tree
26241 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26242 tree* attributes)
26244 tree params = make_node (TREE_LIST);
26245 cp_token *token = cp_lexer_peek_token (parser->lexer);
26246 *ellipsisp = false; /* Initially, assume no ellipsis. */
26248 while (token->type == CPP_COMMA)
26250 cp_parameter_declarator *parmdecl;
26251 tree parm;
26253 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26254 token = cp_lexer_peek_token (parser->lexer);
26256 if (token->type == CPP_ELLIPSIS)
26258 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26259 *ellipsisp = true;
26260 token = cp_lexer_peek_token (parser->lexer);
26261 break;
26264 /* TODO: parse attributes for tail parameters. */
26265 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26266 parm = grokdeclarator (parmdecl->declarator,
26267 &parmdecl->decl_specifiers,
26268 PARM, /*initialized=*/0,
26269 /*attrlist=*/NULL);
26271 chainon (params, build_tree_list (NULL_TREE, parm));
26272 token = cp_lexer_peek_token (parser->lexer);
26275 /* We allow tail attributes for the method. */
26276 if (token->keyword == RID_ATTRIBUTE)
26278 if (*attributes == NULL_TREE)
26280 *attributes = cp_parser_attributes_opt (parser);
26281 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26282 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26283 return params;
26285 else
26286 /* We have an error, but parse the attributes, so that we can
26287 carry on. */
26288 *attributes = cp_parser_attributes_opt (parser);
26290 cp_parser_error (parser,
26291 "method attributes must be specified at the end");
26292 return error_mark_node;
26295 return params;
26298 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26300 static void
26301 cp_parser_objc_interstitial_code (cp_parser* parser)
26303 cp_token *token = cp_lexer_peek_token (parser->lexer);
26305 /* If the next token is `extern' and the following token is a string
26306 literal, then we have a linkage specification. */
26307 if (token->keyword == RID_EXTERN
26308 && cp_parser_is_pure_string_literal
26309 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26310 cp_parser_linkage_specification (parser);
26311 /* Handle #pragma, if any. */
26312 else if (token->type == CPP_PRAGMA)
26313 cp_parser_pragma (parser, pragma_objc_icode);
26314 /* Allow stray semicolons. */
26315 else if (token->type == CPP_SEMICOLON)
26316 cp_lexer_consume_token (parser->lexer);
26317 /* Mark methods as optional or required, when building protocols. */
26318 else if (token->keyword == RID_AT_OPTIONAL)
26320 cp_lexer_consume_token (parser->lexer);
26321 objc_set_method_opt (true);
26323 else if (token->keyword == RID_AT_REQUIRED)
26325 cp_lexer_consume_token (parser->lexer);
26326 objc_set_method_opt (false);
26328 else if (token->keyword == RID_NAMESPACE)
26329 cp_parser_namespace_definition (parser);
26330 /* Other stray characters must generate errors. */
26331 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26333 cp_lexer_consume_token (parser->lexer);
26334 error ("stray %qs between Objective-C++ methods",
26335 token->type == CPP_OPEN_BRACE ? "{" : "}");
26337 /* Finally, try to parse a block-declaration, or a function-definition. */
26338 else
26339 cp_parser_block_declaration (parser, /*statement_p=*/false);
26342 /* Parse a method signature. */
26344 static tree
26345 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26347 tree rettype, kwdparms, optparms;
26348 bool ellipsis = false;
26349 bool is_class_method;
26351 is_class_method = cp_parser_objc_method_type (parser);
26352 rettype = cp_parser_objc_typename (parser);
26353 *attributes = NULL_TREE;
26354 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26355 if (kwdparms == error_mark_node)
26356 return error_mark_node;
26357 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26358 if (optparms == error_mark_node)
26359 return error_mark_node;
26361 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26364 static bool
26365 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26367 tree tattr;
26368 cp_lexer_save_tokens (parser->lexer);
26369 tattr = cp_parser_attributes_opt (parser);
26370 gcc_assert (tattr) ;
26372 /* If the attributes are followed by a method introducer, this is not allowed.
26373 Dump the attributes and flag the situation. */
26374 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26375 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26376 return true;
26378 /* Otherwise, the attributes introduce some interstitial code, possibly so
26379 rewind to allow that check. */
26380 cp_lexer_rollback_tokens (parser->lexer);
26381 return false;
26384 /* Parse an Objective-C method prototype list. */
26386 static void
26387 cp_parser_objc_method_prototype_list (cp_parser* parser)
26389 cp_token *token = cp_lexer_peek_token (parser->lexer);
26391 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26393 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26395 tree attributes, sig;
26396 bool is_class_method;
26397 if (token->type == CPP_PLUS)
26398 is_class_method = true;
26399 else
26400 is_class_method = false;
26401 sig = cp_parser_objc_method_signature (parser, &attributes);
26402 if (sig == error_mark_node)
26404 cp_parser_skip_to_end_of_block_or_statement (parser);
26405 token = cp_lexer_peek_token (parser->lexer);
26406 continue;
26408 objc_add_method_declaration (is_class_method, sig, attributes);
26409 cp_parser_consume_semicolon_at_end_of_statement (parser);
26411 else if (token->keyword == RID_AT_PROPERTY)
26412 cp_parser_objc_at_property_declaration (parser);
26413 else if (token->keyword == RID_ATTRIBUTE
26414 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26415 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26416 OPT_Wattributes,
26417 "prefix attributes are ignored for methods");
26418 else
26419 /* Allow for interspersed non-ObjC++ code. */
26420 cp_parser_objc_interstitial_code (parser);
26422 token = cp_lexer_peek_token (parser->lexer);
26425 if (token->type != CPP_EOF)
26426 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26427 else
26428 cp_parser_error (parser, "expected %<@end%>");
26430 objc_finish_interface ();
26433 /* Parse an Objective-C method definition list. */
26435 static void
26436 cp_parser_objc_method_definition_list (cp_parser* parser)
26438 cp_token *token = cp_lexer_peek_token (parser->lexer);
26440 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26442 tree meth;
26444 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26446 cp_token *ptk;
26447 tree sig, attribute;
26448 bool is_class_method;
26449 if (token->type == CPP_PLUS)
26450 is_class_method = true;
26451 else
26452 is_class_method = false;
26453 push_deferring_access_checks (dk_deferred);
26454 sig = cp_parser_objc_method_signature (parser, &attribute);
26455 if (sig == error_mark_node)
26457 cp_parser_skip_to_end_of_block_or_statement (parser);
26458 token = cp_lexer_peek_token (parser->lexer);
26459 continue;
26461 objc_start_method_definition (is_class_method, sig, attribute,
26462 NULL_TREE);
26464 /* For historical reasons, we accept an optional semicolon. */
26465 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26466 cp_lexer_consume_token (parser->lexer);
26468 ptk = cp_lexer_peek_token (parser->lexer);
26469 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26470 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26472 perform_deferred_access_checks (tf_warning_or_error);
26473 stop_deferring_access_checks ();
26474 meth = cp_parser_function_definition_after_declarator (parser,
26475 false);
26476 pop_deferring_access_checks ();
26477 objc_finish_method_definition (meth);
26480 /* The following case will be removed once @synthesize is
26481 completely implemented. */
26482 else if (token->keyword == RID_AT_PROPERTY)
26483 cp_parser_objc_at_property_declaration (parser);
26484 else if (token->keyword == RID_AT_SYNTHESIZE)
26485 cp_parser_objc_at_synthesize_declaration (parser);
26486 else if (token->keyword == RID_AT_DYNAMIC)
26487 cp_parser_objc_at_dynamic_declaration (parser);
26488 else if (token->keyword == RID_ATTRIBUTE
26489 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26490 warning_at (token->location, OPT_Wattributes,
26491 "prefix attributes are ignored for methods");
26492 else
26493 /* Allow for interspersed non-ObjC++ code. */
26494 cp_parser_objc_interstitial_code (parser);
26496 token = cp_lexer_peek_token (parser->lexer);
26499 if (token->type != CPP_EOF)
26500 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26501 else
26502 cp_parser_error (parser, "expected %<@end%>");
26504 objc_finish_implementation ();
26507 /* Parse Objective-C ivars. */
26509 static void
26510 cp_parser_objc_class_ivars (cp_parser* parser)
26512 cp_token *token = cp_lexer_peek_token (parser->lexer);
26514 if (token->type != CPP_OPEN_BRACE)
26515 return; /* No ivars specified. */
26517 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26518 token = cp_lexer_peek_token (parser->lexer);
26520 while (token->type != CPP_CLOSE_BRACE
26521 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26523 cp_decl_specifier_seq declspecs;
26524 int decl_class_or_enum_p;
26525 tree prefix_attributes;
26527 cp_parser_objc_visibility_spec (parser);
26529 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26530 break;
26532 cp_parser_decl_specifier_seq (parser,
26533 CP_PARSER_FLAGS_OPTIONAL,
26534 &declspecs,
26535 &decl_class_or_enum_p);
26537 /* auto, register, static, extern, mutable. */
26538 if (declspecs.storage_class != sc_none)
26540 cp_parser_error (parser, "invalid type for instance variable");
26541 declspecs.storage_class = sc_none;
26544 /* thread_local. */
26545 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26547 cp_parser_error (parser, "invalid type for instance variable");
26548 declspecs.locations[ds_thread] = 0;
26551 /* typedef. */
26552 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26554 cp_parser_error (parser, "invalid type for instance variable");
26555 declspecs.locations[ds_typedef] = 0;
26558 prefix_attributes = declspecs.attributes;
26559 declspecs.attributes = NULL_TREE;
26561 /* Keep going until we hit the `;' at the end of the
26562 declaration. */
26563 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26565 tree width = NULL_TREE, attributes, first_attribute, decl;
26566 cp_declarator *declarator = NULL;
26567 int ctor_dtor_or_conv_p;
26569 /* Check for a (possibly unnamed) bitfield declaration. */
26570 token = cp_lexer_peek_token (parser->lexer);
26571 if (token->type == CPP_COLON)
26572 goto eat_colon;
26574 if (token->type == CPP_NAME
26575 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26576 == CPP_COLON))
26578 /* Get the name of the bitfield. */
26579 declarator = make_id_declarator (NULL_TREE,
26580 cp_parser_identifier (parser),
26581 sfk_none);
26583 eat_colon:
26584 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26585 /* Get the width of the bitfield. */
26586 width
26587 = cp_parser_constant_expression (parser);
26589 else
26591 /* Parse the declarator. */
26592 declarator
26593 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26594 &ctor_dtor_or_conv_p,
26595 /*parenthesized_p=*/NULL,
26596 /*member_p=*/false,
26597 /*friend_p=*/false);
26600 /* Look for attributes that apply to the ivar. */
26601 attributes = cp_parser_attributes_opt (parser);
26602 /* Remember which attributes are prefix attributes and
26603 which are not. */
26604 first_attribute = attributes;
26605 /* Combine the attributes. */
26606 attributes = chainon (prefix_attributes, attributes);
26608 if (width)
26609 /* Create the bitfield declaration. */
26610 decl = grokbitfield (declarator, &declspecs,
26611 width,
26612 attributes);
26613 else
26614 decl = grokfield (declarator, &declspecs,
26615 NULL_TREE, /*init_const_expr_p=*/false,
26616 NULL_TREE, attributes);
26618 /* Add the instance variable. */
26619 if (decl != error_mark_node && decl != NULL_TREE)
26620 objc_add_instance_variable (decl);
26622 /* Reset PREFIX_ATTRIBUTES. */
26623 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26624 attributes = TREE_CHAIN (attributes);
26625 if (attributes)
26626 TREE_CHAIN (attributes) = NULL_TREE;
26628 token = cp_lexer_peek_token (parser->lexer);
26630 if (token->type == CPP_COMMA)
26632 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26633 continue;
26635 break;
26638 cp_parser_consume_semicolon_at_end_of_statement (parser);
26639 token = cp_lexer_peek_token (parser->lexer);
26642 if (token->keyword == RID_AT_END)
26643 cp_parser_error (parser, "expected %<}%>");
26645 /* Do not consume the RID_AT_END, so it will be read again as terminating
26646 the @interface of @implementation. */
26647 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26648 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26650 /* For historical reasons, we accept an optional semicolon. */
26651 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26652 cp_lexer_consume_token (parser->lexer);
26655 /* Parse an Objective-C protocol declaration. */
26657 static void
26658 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26660 tree proto, protorefs;
26661 cp_token *tok;
26663 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26666 tok = cp_lexer_peek_token (parser->lexer);
26667 error_at (tok->location, "identifier expected after %<@protocol%>");
26668 cp_parser_consume_semicolon_at_end_of_statement (parser);
26669 return;
26672 /* See if we have a forward declaration or a definition. */
26673 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26675 /* Try a forward declaration first. */
26676 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26678 while (true)
26680 tree id;
26682 id = cp_parser_identifier (parser);
26683 if (id == error_mark_node)
26684 break;
26686 objc_declare_protocol (id, attributes);
26688 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26689 cp_lexer_consume_token (parser->lexer);
26690 else
26691 break;
26693 cp_parser_consume_semicolon_at_end_of_statement (parser);
26696 /* Ok, we got a full-fledged definition (or at least should). */
26697 else
26699 proto = cp_parser_identifier (parser);
26700 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26701 objc_start_protocol (proto, protorefs, attributes);
26702 cp_parser_objc_method_prototype_list (parser);
26706 /* Parse an Objective-C superclass or category. */
26708 static void
26709 cp_parser_objc_superclass_or_category (cp_parser *parser,
26710 bool iface_p,
26711 tree *super,
26712 tree *categ, bool *is_class_extension)
26714 cp_token *next = cp_lexer_peek_token (parser->lexer);
26716 *super = *categ = NULL_TREE;
26717 *is_class_extension = false;
26718 if (next->type == CPP_COLON)
26720 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26721 *super = cp_parser_identifier (parser);
26723 else if (next->type == CPP_OPEN_PAREN)
26725 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26727 /* If there is no category name, and this is an @interface, we
26728 have a class extension. */
26729 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26731 *categ = NULL_TREE;
26732 *is_class_extension = true;
26734 else
26735 *categ = cp_parser_identifier (parser);
26737 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26741 /* Parse an Objective-C class interface. */
26743 static void
26744 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26746 tree name, super, categ, protos;
26747 bool is_class_extension;
26749 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26750 name = cp_parser_identifier (parser);
26751 if (name == error_mark_node)
26753 /* It's hard to recover because even if valid @interface stuff
26754 is to follow, we can't compile it (or validate it) if we
26755 don't even know which class it refers to. Let's assume this
26756 was a stray '@interface' token in the stream and skip it.
26758 return;
26760 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26761 &is_class_extension);
26762 protos = cp_parser_objc_protocol_refs_opt (parser);
26764 /* We have either a class or a category on our hands. */
26765 if (categ || is_class_extension)
26766 objc_start_category_interface (name, categ, protos, attributes);
26767 else
26769 objc_start_class_interface (name, super, protos, attributes);
26770 /* Handle instance variable declarations, if any. */
26771 cp_parser_objc_class_ivars (parser);
26772 objc_continue_interface ();
26775 cp_parser_objc_method_prototype_list (parser);
26778 /* Parse an Objective-C class implementation. */
26780 static void
26781 cp_parser_objc_class_implementation (cp_parser* parser)
26783 tree name, super, categ;
26784 bool is_class_extension;
26786 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26787 name = cp_parser_identifier (parser);
26788 if (name == error_mark_node)
26790 /* It's hard to recover because even if valid @implementation
26791 stuff is to follow, we can't compile it (or validate it) if
26792 we don't even know which class it refers to. Let's assume
26793 this was a stray '@implementation' token in the stream and
26794 skip it.
26796 return;
26798 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26799 &is_class_extension);
26801 /* We have either a class or a category on our hands. */
26802 if (categ)
26803 objc_start_category_implementation (name, categ);
26804 else
26806 objc_start_class_implementation (name, super);
26807 /* Handle instance variable declarations, if any. */
26808 cp_parser_objc_class_ivars (parser);
26809 objc_continue_implementation ();
26812 cp_parser_objc_method_definition_list (parser);
26815 /* Consume the @end token and finish off the implementation. */
26817 static void
26818 cp_parser_objc_end_implementation (cp_parser* parser)
26820 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26821 objc_finish_implementation ();
26824 /* Parse an Objective-C declaration. */
26826 static void
26827 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26829 /* Try to figure out what kind of declaration is present. */
26830 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26832 if (attributes)
26833 switch (kwd->keyword)
26835 case RID_AT_ALIAS:
26836 case RID_AT_CLASS:
26837 case RID_AT_END:
26838 error_at (kwd->location, "attributes may not be specified before"
26839 " the %<@%D%> Objective-C++ keyword",
26840 kwd->u.value);
26841 attributes = NULL;
26842 break;
26843 case RID_AT_IMPLEMENTATION:
26844 warning_at (kwd->location, OPT_Wattributes,
26845 "prefix attributes are ignored before %<@%D%>",
26846 kwd->u.value);
26847 attributes = NULL;
26848 default:
26849 break;
26852 switch (kwd->keyword)
26854 case RID_AT_ALIAS:
26855 cp_parser_objc_alias_declaration (parser);
26856 break;
26857 case RID_AT_CLASS:
26858 cp_parser_objc_class_declaration (parser);
26859 break;
26860 case RID_AT_PROTOCOL:
26861 cp_parser_objc_protocol_declaration (parser, attributes);
26862 break;
26863 case RID_AT_INTERFACE:
26864 cp_parser_objc_class_interface (parser, attributes);
26865 break;
26866 case RID_AT_IMPLEMENTATION:
26867 cp_parser_objc_class_implementation (parser);
26868 break;
26869 case RID_AT_END:
26870 cp_parser_objc_end_implementation (parser);
26871 break;
26872 default:
26873 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26874 kwd->u.value);
26875 cp_parser_skip_to_end_of_block_or_statement (parser);
26879 /* Parse an Objective-C try-catch-finally statement.
26881 objc-try-catch-finally-stmt:
26882 @try compound-statement objc-catch-clause-seq [opt]
26883 objc-finally-clause [opt]
26885 objc-catch-clause-seq:
26886 objc-catch-clause objc-catch-clause-seq [opt]
26888 objc-catch-clause:
26889 @catch ( objc-exception-declaration ) compound-statement
26891 objc-finally-clause:
26892 @finally compound-statement
26894 objc-exception-declaration:
26895 parameter-declaration
26896 '...'
26898 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26900 Returns NULL_TREE.
26902 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26903 for C. Keep them in sync. */
26905 static tree
26906 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26908 location_t location;
26909 tree stmt;
26911 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26912 location = cp_lexer_peek_token (parser->lexer)->location;
26913 objc_maybe_warn_exceptions (location);
26914 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26915 node, lest it get absorbed into the surrounding block. */
26916 stmt = push_stmt_list ();
26917 cp_parser_compound_statement (parser, NULL, false, false);
26918 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26920 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26922 cp_parameter_declarator *parm;
26923 tree parameter_declaration = error_mark_node;
26924 bool seen_open_paren = false;
26926 cp_lexer_consume_token (parser->lexer);
26927 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26928 seen_open_paren = true;
26929 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26931 /* We have "@catch (...)" (where the '...' are literally
26932 what is in the code). Skip the '...'.
26933 parameter_declaration is set to NULL_TREE, and
26934 objc_being_catch_clauses() knows that that means
26935 '...'. */
26936 cp_lexer_consume_token (parser->lexer);
26937 parameter_declaration = NULL_TREE;
26939 else
26941 /* We have "@catch (NSException *exception)" or something
26942 like that. Parse the parameter declaration. */
26943 parm = cp_parser_parameter_declaration (parser, false, NULL);
26944 if (parm == NULL)
26945 parameter_declaration = error_mark_node;
26946 else
26947 parameter_declaration = grokdeclarator (parm->declarator,
26948 &parm->decl_specifiers,
26949 PARM, /*initialized=*/0,
26950 /*attrlist=*/NULL);
26952 if (seen_open_paren)
26953 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26954 else
26956 /* If there was no open parenthesis, we are recovering from
26957 an error, and we are trying to figure out what mistake
26958 the user has made. */
26960 /* If there is an immediate closing parenthesis, the user
26961 probably forgot the opening one (ie, they typed "@catch
26962 NSException *e)". Parse the closing parenthesis and keep
26963 going. */
26964 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26965 cp_lexer_consume_token (parser->lexer);
26967 /* If these is no immediate closing parenthesis, the user
26968 probably doesn't know that parenthesis are required at
26969 all (ie, they typed "@catch NSException *e"). So, just
26970 forget about the closing parenthesis and keep going. */
26972 objc_begin_catch_clause (parameter_declaration);
26973 cp_parser_compound_statement (parser, NULL, false, false);
26974 objc_finish_catch_clause ();
26976 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26978 cp_lexer_consume_token (parser->lexer);
26979 location = cp_lexer_peek_token (parser->lexer)->location;
26980 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26981 node, lest it get absorbed into the surrounding block. */
26982 stmt = push_stmt_list ();
26983 cp_parser_compound_statement (parser, NULL, false, false);
26984 objc_build_finally_clause (location, pop_stmt_list (stmt));
26987 return objc_finish_try_stmt ();
26990 /* Parse an Objective-C synchronized statement.
26992 objc-synchronized-stmt:
26993 @synchronized ( expression ) compound-statement
26995 Returns NULL_TREE. */
26997 static tree
26998 cp_parser_objc_synchronized_statement (cp_parser *parser)
27000 location_t location;
27001 tree lock, stmt;
27003 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27005 location = cp_lexer_peek_token (parser->lexer)->location;
27006 objc_maybe_warn_exceptions (location);
27007 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27008 lock = cp_parser_expression (parser);
27009 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27011 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27012 node, lest it get absorbed into the surrounding block. */
27013 stmt = push_stmt_list ();
27014 cp_parser_compound_statement (parser, NULL, false, false);
27016 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27019 /* Parse an Objective-C throw statement.
27021 objc-throw-stmt:
27022 @throw assignment-expression [opt] ;
27024 Returns a constructed '@throw' statement. */
27026 static tree
27027 cp_parser_objc_throw_statement (cp_parser *parser)
27029 tree expr = NULL_TREE;
27030 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27032 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27034 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27035 expr = cp_parser_expression (parser);
27037 cp_parser_consume_semicolon_at_end_of_statement (parser);
27039 return objc_build_throw_stmt (loc, expr);
27042 /* Parse an Objective-C statement. */
27044 static tree
27045 cp_parser_objc_statement (cp_parser * parser)
27047 /* Try to figure out what kind of declaration is present. */
27048 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27050 switch (kwd->keyword)
27052 case RID_AT_TRY:
27053 return cp_parser_objc_try_catch_finally_statement (parser);
27054 case RID_AT_SYNCHRONIZED:
27055 return cp_parser_objc_synchronized_statement (parser);
27056 case RID_AT_THROW:
27057 return cp_parser_objc_throw_statement (parser);
27058 default:
27059 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27060 kwd->u.value);
27061 cp_parser_skip_to_end_of_block_or_statement (parser);
27064 return error_mark_node;
27067 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27068 look ahead to see if an objc keyword follows the attributes. This
27069 is to detect the use of prefix attributes on ObjC @interface and
27070 @protocol. */
27072 static bool
27073 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27075 cp_lexer_save_tokens (parser->lexer);
27076 *attrib = cp_parser_attributes_opt (parser);
27077 gcc_assert (*attrib);
27078 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27080 cp_lexer_commit_tokens (parser->lexer);
27081 return true;
27083 cp_lexer_rollback_tokens (parser->lexer);
27084 return false;
27087 /* This routine is a minimal replacement for
27088 c_parser_struct_declaration () used when parsing the list of
27089 types/names or ObjC++ properties. For example, when parsing the
27090 code
27092 @property (readonly) int a, b, c;
27094 this function is responsible for parsing "int a, int b, int c" and
27095 returning the declarations as CHAIN of DECLs.
27097 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27098 similar parsing. */
27099 static tree
27100 cp_parser_objc_struct_declaration (cp_parser *parser)
27102 tree decls = NULL_TREE;
27103 cp_decl_specifier_seq declspecs;
27104 int decl_class_or_enum_p;
27105 tree prefix_attributes;
27107 cp_parser_decl_specifier_seq (parser,
27108 CP_PARSER_FLAGS_NONE,
27109 &declspecs,
27110 &decl_class_or_enum_p);
27112 if (declspecs.type == error_mark_node)
27113 return error_mark_node;
27115 /* auto, register, static, extern, mutable. */
27116 if (declspecs.storage_class != sc_none)
27118 cp_parser_error (parser, "invalid type for property");
27119 declspecs.storage_class = sc_none;
27122 /* thread_local. */
27123 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27125 cp_parser_error (parser, "invalid type for property");
27126 declspecs.locations[ds_thread] = 0;
27129 /* typedef. */
27130 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27132 cp_parser_error (parser, "invalid type for property");
27133 declspecs.locations[ds_typedef] = 0;
27136 prefix_attributes = declspecs.attributes;
27137 declspecs.attributes = NULL_TREE;
27139 /* Keep going until we hit the `;' at the end of the declaration. */
27140 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27142 tree attributes, first_attribute, decl;
27143 cp_declarator *declarator;
27144 cp_token *token;
27146 /* Parse the declarator. */
27147 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27148 NULL, NULL, false, false);
27150 /* Look for attributes that apply to the ivar. */
27151 attributes = cp_parser_attributes_opt (parser);
27152 /* Remember which attributes are prefix attributes and
27153 which are not. */
27154 first_attribute = attributes;
27155 /* Combine the attributes. */
27156 attributes = chainon (prefix_attributes, attributes);
27158 decl = grokfield (declarator, &declspecs,
27159 NULL_TREE, /*init_const_expr_p=*/false,
27160 NULL_TREE, attributes);
27162 if (decl == error_mark_node || decl == NULL_TREE)
27163 return error_mark_node;
27165 /* Reset PREFIX_ATTRIBUTES. */
27166 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27167 attributes = TREE_CHAIN (attributes);
27168 if (attributes)
27169 TREE_CHAIN (attributes) = NULL_TREE;
27171 DECL_CHAIN (decl) = decls;
27172 decls = decl;
27174 token = cp_lexer_peek_token (parser->lexer);
27175 if (token->type == CPP_COMMA)
27177 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27178 continue;
27180 else
27181 break;
27183 return decls;
27186 /* Parse an Objective-C @property declaration. The syntax is:
27188 objc-property-declaration:
27189 '@property' objc-property-attributes[opt] struct-declaration ;
27191 objc-property-attributes:
27192 '(' objc-property-attribute-list ')'
27194 objc-property-attribute-list:
27195 objc-property-attribute
27196 objc-property-attribute-list, objc-property-attribute
27198 objc-property-attribute
27199 'getter' = identifier
27200 'setter' = identifier
27201 'readonly'
27202 'readwrite'
27203 'assign'
27204 'retain'
27205 'copy'
27206 'nonatomic'
27208 For example:
27209 @property NSString *name;
27210 @property (readonly) id object;
27211 @property (retain, nonatomic, getter=getTheName) id name;
27212 @property int a, b, c;
27214 PS: This function is identical to
27215 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27216 static void
27217 cp_parser_objc_at_property_declaration (cp_parser *parser)
27219 /* The following variables hold the attributes of the properties as
27220 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27221 seen. When we see an attribute, we set them to 'true' (if they
27222 are boolean properties) or to the identifier (if they have an
27223 argument, ie, for getter and setter). Note that here we only
27224 parse the list of attributes, check the syntax and accumulate the
27225 attributes that we find. objc_add_property_declaration() will
27226 then process the information. */
27227 bool property_assign = false;
27228 bool property_copy = false;
27229 tree property_getter_ident = NULL_TREE;
27230 bool property_nonatomic = false;
27231 bool property_readonly = false;
27232 bool property_readwrite = false;
27233 bool property_retain = false;
27234 tree property_setter_ident = NULL_TREE;
27236 /* 'properties' is the list of properties that we read. Usually a
27237 single one, but maybe more (eg, in "@property int a, b, c;" there
27238 are three). */
27239 tree properties;
27240 location_t loc;
27242 loc = cp_lexer_peek_token (parser->lexer)->location;
27244 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27246 /* Parse the optional attribute list... */
27247 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27249 /* Eat the '('. */
27250 cp_lexer_consume_token (parser->lexer);
27252 while (true)
27254 bool syntax_error = false;
27255 cp_token *token = cp_lexer_peek_token (parser->lexer);
27256 enum rid keyword;
27258 if (token->type != CPP_NAME)
27260 cp_parser_error (parser, "expected identifier");
27261 break;
27263 keyword = C_RID_CODE (token->u.value);
27264 cp_lexer_consume_token (parser->lexer);
27265 switch (keyword)
27267 case RID_ASSIGN: property_assign = true; break;
27268 case RID_COPY: property_copy = true; break;
27269 case RID_NONATOMIC: property_nonatomic = true; break;
27270 case RID_READONLY: property_readonly = true; break;
27271 case RID_READWRITE: property_readwrite = true; break;
27272 case RID_RETAIN: property_retain = true; break;
27274 case RID_GETTER:
27275 case RID_SETTER:
27276 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27278 if (keyword == RID_GETTER)
27279 cp_parser_error (parser,
27280 "missing %<=%> (after %<getter%> attribute)");
27281 else
27282 cp_parser_error (parser,
27283 "missing %<=%> (after %<setter%> attribute)");
27284 syntax_error = true;
27285 break;
27287 cp_lexer_consume_token (parser->lexer); /* eat the = */
27288 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27290 cp_parser_error (parser, "expected identifier");
27291 syntax_error = true;
27292 break;
27294 if (keyword == RID_SETTER)
27296 if (property_setter_ident != NULL_TREE)
27298 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27299 cp_lexer_consume_token (parser->lexer);
27301 else
27302 property_setter_ident = cp_parser_objc_selector (parser);
27303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27304 cp_parser_error (parser, "setter name must terminate with %<:%>");
27305 else
27306 cp_lexer_consume_token (parser->lexer);
27308 else
27310 if (property_getter_ident != NULL_TREE)
27312 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27313 cp_lexer_consume_token (parser->lexer);
27315 else
27316 property_getter_ident = cp_parser_objc_selector (parser);
27318 break;
27319 default:
27320 cp_parser_error (parser, "unknown property attribute");
27321 syntax_error = true;
27322 break;
27325 if (syntax_error)
27326 break;
27328 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27329 cp_lexer_consume_token (parser->lexer);
27330 else
27331 break;
27334 /* FIXME: "@property (setter, assign);" will generate a spurious
27335 "error: expected ‘)’ before ‘,’ token". This is because
27336 cp_parser_require, unlike the C counterpart, will produce an
27337 error even if we are in error recovery. */
27338 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27340 cp_parser_skip_to_closing_parenthesis (parser,
27341 /*recovering=*/true,
27342 /*or_comma=*/false,
27343 /*consume_paren=*/true);
27347 /* ... and the property declaration(s). */
27348 properties = cp_parser_objc_struct_declaration (parser);
27350 if (properties == error_mark_node)
27352 cp_parser_skip_to_end_of_statement (parser);
27353 /* If the next token is now a `;', consume it. */
27354 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27355 cp_lexer_consume_token (parser->lexer);
27356 return;
27359 if (properties == NULL_TREE)
27360 cp_parser_error (parser, "expected identifier");
27361 else
27363 /* Comma-separated properties are chained together in
27364 reverse order; add them one by one. */
27365 properties = nreverse (properties);
27367 for (; properties; properties = TREE_CHAIN (properties))
27368 objc_add_property_declaration (loc, copy_node (properties),
27369 property_readonly, property_readwrite,
27370 property_assign, property_retain,
27371 property_copy, property_nonatomic,
27372 property_getter_ident, property_setter_ident);
27375 cp_parser_consume_semicolon_at_end_of_statement (parser);
27378 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27380 objc-synthesize-declaration:
27381 @synthesize objc-synthesize-identifier-list ;
27383 objc-synthesize-identifier-list:
27384 objc-synthesize-identifier
27385 objc-synthesize-identifier-list, objc-synthesize-identifier
27387 objc-synthesize-identifier
27388 identifier
27389 identifier = identifier
27391 For example:
27392 @synthesize MyProperty;
27393 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27395 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27396 for C. Keep them in sync.
27398 static void
27399 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27401 tree list = NULL_TREE;
27402 location_t loc;
27403 loc = cp_lexer_peek_token (parser->lexer)->location;
27405 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27406 while (true)
27408 tree property, ivar;
27409 property = cp_parser_identifier (parser);
27410 if (property == error_mark_node)
27412 cp_parser_consume_semicolon_at_end_of_statement (parser);
27413 return;
27415 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27417 cp_lexer_consume_token (parser->lexer);
27418 ivar = cp_parser_identifier (parser);
27419 if (ivar == error_mark_node)
27421 cp_parser_consume_semicolon_at_end_of_statement (parser);
27422 return;
27425 else
27426 ivar = NULL_TREE;
27427 list = chainon (list, build_tree_list (ivar, property));
27428 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27429 cp_lexer_consume_token (parser->lexer);
27430 else
27431 break;
27433 cp_parser_consume_semicolon_at_end_of_statement (parser);
27434 objc_add_synthesize_declaration (loc, list);
27437 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27439 objc-dynamic-declaration:
27440 @dynamic identifier-list ;
27442 For example:
27443 @dynamic MyProperty;
27444 @dynamic MyProperty, AnotherProperty;
27446 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27447 for C. Keep them in sync.
27449 static void
27450 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27452 tree list = NULL_TREE;
27453 location_t loc;
27454 loc = cp_lexer_peek_token (parser->lexer)->location;
27456 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27457 while (true)
27459 tree property;
27460 property = cp_parser_identifier (parser);
27461 if (property == error_mark_node)
27463 cp_parser_consume_semicolon_at_end_of_statement (parser);
27464 return;
27466 list = chainon (list, build_tree_list (NULL, property));
27467 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27468 cp_lexer_consume_token (parser->lexer);
27469 else
27470 break;
27472 cp_parser_consume_semicolon_at_end_of_statement (parser);
27473 objc_add_dynamic_declaration (loc, list);
27477 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27479 /* Returns name of the next clause.
27480 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27481 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27482 returned and the token is consumed. */
27484 static pragma_omp_clause
27485 cp_parser_omp_clause_name (cp_parser *parser)
27487 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27489 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27490 result = PRAGMA_OMP_CLAUSE_IF;
27491 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27492 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27493 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27494 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27495 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27496 result = PRAGMA_OMP_CLAUSE_FOR;
27497 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27499 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27500 const char *p = IDENTIFIER_POINTER (id);
27502 switch (p[0])
27504 case 'a':
27505 if (!strcmp ("aligned", p))
27506 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27507 break;
27508 case 'c':
27509 if (!strcmp ("collapse", p))
27510 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27511 else if (!strcmp ("copyin", p))
27512 result = PRAGMA_OMP_CLAUSE_COPYIN;
27513 else if (!strcmp ("copyprivate", p))
27514 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27515 break;
27516 case 'd':
27517 if (!strcmp ("depend", p))
27518 result = PRAGMA_OMP_CLAUSE_DEPEND;
27519 else if (!strcmp ("device", p))
27520 result = PRAGMA_OMP_CLAUSE_DEVICE;
27521 else if (!strcmp ("dist_schedule", p))
27522 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27523 break;
27524 case 'f':
27525 if (!strcmp ("final", p))
27526 result = PRAGMA_OMP_CLAUSE_FINAL;
27527 else if (!strcmp ("firstprivate", p))
27528 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27529 else if (!strcmp ("from", p))
27530 result = PRAGMA_OMP_CLAUSE_FROM;
27531 break;
27532 case 'i':
27533 if (!strcmp ("inbranch", p))
27534 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27535 break;
27536 case 'l':
27537 if (!strcmp ("lastprivate", p))
27538 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27539 else if (!strcmp ("linear", p))
27540 result = PRAGMA_OMP_CLAUSE_LINEAR;
27541 break;
27542 case 'm':
27543 if (!strcmp ("map", p))
27544 result = PRAGMA_OMP_CLAUSE_MAP;
27545 else if (!strcmp ("mergeable", p))
27546 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27547 else if (flag_cilkplus && !strcmp ("mask", p))
27548 result = PRAGMA_CILK_CLAUSE_MASK;
27549 break;
27550 case 'n':
27551 if (!strcmp ("notinbranch", p))
27552 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27553 else if (!strcmp ("nowait", p))
27554 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27555 else if (flag_cilkplus && !strcmp ("nomask", p))
27556 result = PRAGMA_CILK_CLAUSE_NOMASK;
27557 else if (!strcmp ("num_teams", p))
27558 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27559 else if (!strcmp ("num_threads", p))
27560 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27561 break;
27562 case 'o':
27563 if (!strcmp ("ordered", p))
27564 result = PRAGMA_OMP_CLAUSE_ORDERED;
27565 break;
27566 case 'p':
27567 if (!strcmp ("parallel", p))
27568 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27569 else if (!strcmp ("proc_bind", p))
27570 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27571 break;
27572 case 'r':
27573 if (!strcmp ("reduction", p))
27574 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27575 break;
27576 case 's':
27577 if (!strcmp ("safelen", p))
27578 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27579 else if (!strcmp ("schedule", p))
27580 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27581 else if (!strcmp ("sections", p))
27582 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27583 else if (!strcmp ("shared", p))
27584 result = PRAGMA_OMP_CLAUSE_SHARED;
27585 else if (!strcmp ("simdlen", p))
27586 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27587 break;
27588 case 't':
27589 if (!strcmp ("taskgroup", p))
27590 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27591 else if (!strcmp ("thread_limit", p))
27592 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27593 else if (!strcmp ("to", p))
27594 result = PRAGMA_OMP_CLAUSE_TO;
27595 break;
27596 case 'u':
27597 if (!strcmp ("uniform", p))
27598 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27599 else if (!strcmp ("untied", p))
27600 result = PRAGMA_OMP_CLAUSE_UNTIED;
27601 break;
27602 case 'v':
27603 if (flag_cilkplus && !strcmp ("vectorlength", p))
27604 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27605 break;
27609 if (result != PRAGMA_OMP_CLAUSE_NONE)
27610 cp_lexer_consume_token (parser->lexer);
27612 return result;
27615 /* Validate that a clause of the given type does not already exist. */
27617 static void
27618 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27619 const char *name, location_t location)
27621 tree c;
27623 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27624 if (OMP_CLAUSE_CODE (c) == code)
27626 error_at (location, "too many %qs clauses", name);
27627 break;
27631 /* OpenMP 2.5:
27632 variable-list:
27633 identifier
27634 variable-list , identifier
27636 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27637 colon). An opening parenthesis will have been consumed by the caller.
27639 If KIND is nonzero, create the appropriate node and install the decl
27640 in OMP_CLAUSE_DECL and add the node to the head of the list.
27642 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27643 return the list created.
27645 COLON can be NULL if only closing parenthesis should end the list,
27646 or pointer to bool which will receive false if the list is terminated
27647 by closing parenthesis or true if the list is terminated by colon. */
27649 static tree
27650 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27651 tree list, bool *colon)
27653 cp_token *token;
27654 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27655 if (colon)
27657 parser->colon_corrects_to_scope_p = false;
27658 *colon = false;
27660 while (1)
27662 tree name, decl;
27664 token = cp_lexer_peek_token (parser->lexer);
27665 name = cp_parser_id_expression (parser, /*template_p=*/false,
27666 /*check_dependency_p=*/true,
27667 /*template_p=*/NULL,
27668 /*declarator_p=*/false,
27669 /*optional_p=*/false);
27670 if (name == error_mark_node)
27671 goto skip_comma;
27673 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27674 if (decl == error_mark_node)
27675 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27676 token->location);
27677 else if (kind != 0)
27679 switch (kind)
27681 case OMP_CLAUSE_MAP:
27682 case OMP_CLAUSE_FROM:
27683 case OMP_CLAUSE_TO:
27684 case OMP_CLAUSE_DEPEND:
27685 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27687 tree low_bound = NULL_TREE, length = NULL_TREE;
27689 parser->colon_corrects_to_scope_p = false;
27690 cp_lexer_consume_token (parser->lexer);
27691 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27692 low_bound = cp_parser_expression (parser);
27693 if (!colon)
27694 parser->colon_corrects_to_scope_p
27695 = saved_colon_corrects_to_scope_p;
27696 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27697 length = integer_one_node;
27698 else
27700 /* Look for `:'. */
27701 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27702 goto skip_comma;
27703 if (!cp_lexer_next_token_is (parser->lexer,
27704 CPP_CLOSE_SQUARE))
27705 length = cp_parser_expression (parser);
27707 /* Look for the closing `]'. */
27708 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27709 RT_CLOSE_SQUARE))
27710 goto skip_comma;
27711 decl = tree_cons (low_bound, length, decl);
27713 break;
27714 default:
27715 break;
27718 tree u = build_omp_clause (token->location, kind);
27719 OMP_CLAUSE_DECL (u) = decl;
27720 OMP_CLAUSE_CHAIN (u) = list;
27721 list = u;
27723 else
27724 list = tree_cons (decl, NULL_TREE, list);
27726 get_comma:
27727 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27728 break;
27729 cp_lexer_consume_token (parser->lexer);
27732 if (colon)
27733 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27735 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27737 *colon = true;
27738 cp_parser_require (parser, CPP_COLON, RT_COLON);
27739 return list;
27742 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27744 int ending;
27746 /* Try to resync to an unnested comma. Copied from
27747 cp_parser_parenthesized_expression_list. */
27748 skip_comma:
27749 if (colon)
27750 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27751 ending = cp_parser_skip_to_closing_parenthesis (parser,
27752 /*recovering=*/true,
27753 /*or_comma=*/true,
27754 /*consume_paren=*/true);
27755 if (ending < 0)
27756 goto get_comma;
27759 return list;
27762 /* Similarly, but expect leading and trailing parenthesis. This is a very
27763 common case for omp clauses. */
27765 static tree
27766 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27768 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27769 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27770 return list;
27773 /* OpenMP 3.0:
27774 collapse ( constant-expression ) */
27776 static tree
27777 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27779 tree c, num;
27780 location_t loc;
27781 HOST_WIDE_INT n;
27783 loc = cp_lexer_peek_token (parser->lexer)->location;
27784 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27785 return list;
27787 num = cp_parser_constant_expression (parser);
27789 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27790 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27791 /*or_comma=*/false,
27792 /*consume_paren=*/true);
27794 if (num == error_mark_node)
27795 return list;
27796 num = fold_non_dependent_expr (num);
27797 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27798 || !tree_fits_shwi_p (num)
27799 || (n = tree_to_shwi (num)) <= 0
27800 || (int) n != n)
27802 error_at (loc, "collapse argument needs positive constant integer expression");
27803 return list;
27806 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27807 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27808 OMP_CLAUSE_CHAIN (c) = list;
27809 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27811 return c;
27814 /* OpenMP 2.5:
27815 default ( shared | none ) */
27817 static tree
27818 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27820 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27821 tree c;
27823 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27824 return list;
27825 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27827 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27828 const char *p = IDENTIFIER_POINTER (id);
27830 switch (p[0])
27832 case 'n':
27833 if (strcmp ("none", p) != 0)
27834 goto invalid_kind;
27835 kind = OMP_CLAUSE_DEFAULT_NONE;
27836 break;
27838 case 's':
27839 if (strcmp ("shared", p) != 0)
27840 goto invalid_kind;
27841 kind = OMP_CLAUSE_DEFAULT_SHARED;
27842 break;
27844 default:
27845 goto invalid_kind;
27848 cp_lexer_consume_token (parser->lexer);
27850 else
27852 invalid_kind:
27853 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27856 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27857 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27858 /*or_comma=*/false,
27859 /*consume_paren=*/true);
27861 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27862 return list;
27864 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27865 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27866 OMP_CLAUSE_CHAIN (c) = list;
27867 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27869 return c;
27872 /* OpenMP 3.1:
27873 final ( expression ) */
27875 static tree
27876 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27878 tree t, c;
27880 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27881 return list;
27883 t = cp_parser_condition (parser);
27885 if (t == error_mark_node
27886 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27887 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27888 /*or_comma=*/false,
27889 /*consume_paren=*/true);
27891 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27893 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27894 OMP_CLAUSE_FINAL_EXPR (c) = t;
27895 OMP_CLAUSE_CHAIN (c) = list;
27897 return c;
27900 /* OpenMP 2.5:
27901 if ( expression ) */
27903 static tree
27904 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27906 tree t, c;
27908 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27909 return list;
27911 t = cp_parser_condition (parser);
27913 if (t == error_mark_node
27914 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27915 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27916 /*or_comma=*/false,
27917 /*consume_paren=*/true);
27919 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27921 c = build_omp_clause (location, OMP_CLAUSE_IF);
27922 OMP_CLAUSE_IF_EXPR (c) = t;
27923 OMP_CLAUSE_CHAIN (c) = list;
27925 return c;
27928 /* OpenMP 3.1:
27929 mergeable */
27931 static tree
27932 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27933 tree list, location_t location)
27935 tree c;
27937 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27938 location);
27940 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27941 OMP_CLAUSE_CHAIN (c) = list;
27942 return c;
27945 /* OpenMP 2.5:
27946 nowait */
27948 static tree
27949 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27950 tree list, location_t location)
27952 tree c;
27954 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27956 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27957 OMP_CLAUSE_CHAIN (c) = list;
27958 return c;
27961 /* OpenMP 2.5:
27962 num_threads ( expression ) */
27964 static tree
27965 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27966 location_t location)
27968 tree t, c;
27970 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27971 return list;
27973 t = cp_parser_expression (parser);
27975 if (t == error_mark_node
27976 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27977 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27978 /*or_comma=*/false,
27979 /*consume_paren=*/true);
27981 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27982 "num_threads", location);
27984 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27985 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27986 OMP_CLAUSE_CHAIN (c) = list;
27988 return c;
27991 /* OpenMP 2.5:
27992 ordered */
27994 static tree
27995 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27996 tree list, location_t location)
27998 tree c;
28000 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28001 "ordered", location);
28003 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28004 OMP_CLAUSE_CHAIN (c) = list;
28005 return c;
28008 /* OpenMP 2.5:
28009 reduction ( reduction-operator : variable-list )
28011 reduction-operator:
28012 One of: + * - & ^ | && ||
28014 OpenMP 3.1:
28016 reduction-operator:
28017 One of: + * - & ^ | && || min max
28019 OpenMP 4.0:
28021 reduction-operator:
28022 One of: + * - & ^ | && ||
28023 id-expression */
28025 static tree
28026 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28028 enum tree_code code = ERROR_MARK;
28029 tree nlist, c, id = NULL_TREE;
28031 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28032 return list;
28034 switch (cp_lexer_peek_token (parser->lexer)->type)
28036 case CPP_PLUS: code = PLUS_EXPR; break;
28037 case CPP_MULT: code = MULT_EXPR; break;
28038 case CPP_MINUS: code = MINUS_EXPR; break;
28039 case CPP_AND: code = BIT_AND_EXPR; break;
28040 case CPP_XOR: code = BIT_XOR_EXPR; break;
28041 case CPP_OR: code = BIT_IOR_EXPR; break;
28042 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28043 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28044 default: break;
28047 if (code != ERROR_MARK)
28048 cp_lexer_consume_token (parser->lexer);
28049 else
28051 bool saved_colon_corrects_to_scope_p;
28052 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28053 parser->colon_corrects_to_scope_p = false;
28054 id = cp_parser_id_expression (parser, /*template_p=*/false,
28055 /*check_dependency_p=*/true,
28056 /*template_p=*/NULL,
28057 /*declarator_p=*/false,
28058 /*optional_p=*/false);
28059 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28060 if (identifier_p (id))
28062 const char *p = IDENTIFIER_POINTER (id);
28064 if (strcmp (p, "min") == 0)
28065 code = MIN_EXPR;
28066 else if (strcmp (p, "max") == 0)
28067 code = MAX_EXPR;
28068 else if (id == ansi_opname (PLUS_EXPR))
28069 code = PLUS_EXPR;
28070 else if (id == ansi_opname (MULT_EXPR))
28071 code = MULT_EXPR;
28072 else if (id == ansi_opname (MINUS_EXPR))
28073 code = MINUS_EXPR;
28074 else if (id == ansi_opname (BIT_AND_EXPR))
28075 code = BIT_AND_EXPR;
28076 else if (id == ansi_opname (BIT_IOR_EXPR))
28077 code = BIT_IOR_EXPR;
28078 else if (id == ansi_opname (BIT_XOR_EXPR))
28079 code = BIT_XOR_EXPR;
28080 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28081 code = TRUTH_ANDIF_EXPR;
28082 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28083 code = TRUTH_ORIF_EXPR;
28084 id = omp_reduction_id (code, id, NULL_TREE);
28085 tree scope = parser->scope;
28086 if (scope)
28087 id = build_qualified_name (NULL_TREE, scope, id, false);
28088 parser->scope = NULL_TREE;
28089 parser->qualifying_scope = NULL_TREE;
28090 parser->object_scope = NULL_TREE;
28092 else
28094 error ("invalid reduction-identifier");
28095 resync_fail:
28096 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28097 /*or_comma=*/false,
28098 /*consume_paren=*/true);
28099 return list;
28103 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28104 goto resync_fail;
28106 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28107 NULL);
28108 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28110 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28111 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28114 return nlist;
28117 /* OpenMP 2.5:
28118 schedule ( schedule-kind )
28119 schedule ( schedule-kind , expression )
28121 schedule-kind:
28122 static | dynamic | guided | runtime | auto */
28124 static tree
28125 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28127 tree c, t;
28129 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28130 return list;
28132 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28134 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28136 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28137 const char *p = IDENTIFIER_POINTER (id);
28139 switch (p[0])
28141 case 'd':
28142 if (strcmp ("dynamic", p) != 0)
28143 goto invalid_kind;
28144 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28145 break;
28147 case 'g':
28148 if (strcmp ("guided", p) != 0)
28149 goto invalid_kind;
28150 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28151 break;
28153 case 'r':
28154 if (strcmp ("runtime", p) != 0)
28155 goto invalid_kind;
28156 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28157 break;
28159 default:
28160 goto invalid_kind;
28163 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28164 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28165 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28166 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28167 else
28168 goto invalid_kind;
28169 cp_lexer_consume_token (parser->lexer);
28171 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28173 cp_token *token;
28174 cp_lexer_consume_token (parser->lexer);
28176 token = cp_lexer_peek_token (parser->lexer);
28177 t = cp_parser_assignment_expression (parser);
28179 if (t == error_mark_node)
28180 goto resync_fail;
28181 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28182 error_at (token->location, "schedule %<runtime%> does not take "
28183 "a %<chunk_size%> parameter");
28184 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28185 error_at (token->location, "schedule %<auto%> does not take "
28186 "a %<chunk_size%> parameter");
28187 else
28188 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28190 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28191 goto resync_fail;
28193 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28194 goto resync_fail;
28196 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28197 OMP_CLAUSE_CHAIN (c) = list;
28198 return c;
28200 invalid_kind:
28201 cp_parser_error (parser, "invalid schedule kind");
28202 resync_fail:
28203 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28204 /*or_comma=*/false,
28205 /*consume_paren=*/true);
28206 return list;
28209 /* OpenMP 3.0:
28210 untied */
28212 static tree
28213 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28214 tree list, location_t location)
28216 tree c;
28218 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28220 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28221 OMP_CLAUSE_CHAIN (c) = list;
28222 return c;
28225 /* OpenMP 4.0:
28226 inbranch
28227 notinbranch */
28229 static tree
28230 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28231 tree list, location_t location)
28233 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28234 tree c = build_omp_clause (location, code);
28235 OMP_CLAUSE_CHAIN (c) = list;
28236 return c;
28239 /* OpenMP 4.0:
28240 parallel
28242 sections
28243 taskgroup */
28245 static tree
28246 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28247 enum omp_clause_code code,
28248 tree list, location_t location)
28250 tree c = build_omp_clause (location, code);
28251 OMP_CLAUSE_CHAIN (c) = list;
28252 return c;
28255 /* OpenMP 4.0:
28256 num_teams ( expression ) */
28258 static tree
28259 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28260 location_t location)
28262 tree t, c;
28264 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28265 return list;
28267 t = cp_parser_expression (parser);
28269 if (t == error_mark_node
28270 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28271 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28272 /*or_comma=*/false,
28273 /*consume_paren=*/true);
28275 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28276 "num_teams", location);
28278 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28279 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28280 OMP_CLAUSE_CHAIN (c) = list;
28282 return c;
28285 /* OpenMP 4.0:
28286 thread_limit ( expression ) */
28288 static tree
28289 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28290 location_t location)
28292 tree t, c;
28294 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28295 return list;
28297 t = cp_parser_expression (parser);
28299 if (t == error_mark_node
28300 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28301 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28302 /*or_comma=*/false,
28303 /*consume_paren=*/true);
28305 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28306 "thread_limit", location);
28308 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28309 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28310 OMP_CLAUSE_CHAIN (c) = list;
28312 return c;
28315 /* OpenMP 4.0:
28316 aligned ( variable-list )
28317 aligned ( variable-list : constant-expression ) */
28319 static tree
28320 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28322 tree nlist, c, alignment = NULL_TREE;
28323 bool colon;
28325 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28326 return list;
28328 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28329 &colon);
28331 if (colon)
28333 alignment = cp_parser_constant_expression (parser);
28335 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28336 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28337 /*or_comma=*/false,
28338 /*consume_paren=*/true);
28340 if (alignment == error_mark_node)
28341 alignment = NULL_TREE;
28344 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28345 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28347 return nlist;
28350 /* OpenMP 4.0:
28351 linear ( variable-list )
28352 linear ( variable-list : expression ) */
28354 static tree
28355 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28356 bool is_cilk_simd_fn)
28358 tree nlist, c, step = integer_one_node;
28359 bool colon;
28361 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28362 return list;
28364 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28365 &colon);
28367 if (colon)
28369 step = cp_parser_expression (parser);
28371 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28373 sorry ("using parameters for %<linear%> step is not supported yet");
28374 step = integer_one_node;
28376 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28377 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28378 /*or_comma=*/false,
28379 /*consume_paren=*/true);
28381 if (step == error_mark_node)
28382 return list;
28385 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28386 OMP_CLAUSE_LINEAR_STEP (c) = step;
28388 return nlist;
28391 /* OpenMP 4.0:
28392 safelen ( constant-expression ) */
28394 static tree
28395 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28396 location_t location)
28398 tree t, c;
28400 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28401 return list;
28403 t = cp_parser_constant_expression (parser);
28405 if (t == error_mark_node
28406 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28407 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28408 /*or_comma=*/false,
28409 /*consume_paren=*/true);
28411 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28413 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28414 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28415 OMP_CLAUSE_CHAIN (c) = list;
28417 return c;
28420 /* OpenMP 4.0:
28421 simdlen ( constant-expression ) */
28423 static tree
28424 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28425 location_t location)
28427 tree t, c;
28429 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28430 return list;
28432 t = cp_parser_constant_expression (parser);
28434 if (t == error_mark_node
28435 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28436 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28437 /*or_comma=*/false,
28438 /*consume_paren=*/true);
28440 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28442 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28443 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28444 OMP_CLAUSE_CHAIN (c) = list;
28446 return c;
28449 /* OpenMP 4.0:
28450 depend ( depend-kind : variable-list )
28452 depend-kind:
28453 in | out | inout */
28455 static tree
28456 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28458 tree nlist, c;
28459 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28461 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28462 return list;
28464 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28466 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28467 const char *p = IDENTIFIER_POINTER (id);
28469 if (strcmp ("in", p) == 0)
28470 kind = OMP_CLAUSE_DEPEND_IN;
28471 else if (strcmp ("inout", p) == 0)
28472 kind = OMP_CLAUSE_DEPEND_INOUT;
28473 else if (strcmp ("out", p) == 0)
28474 kind = OMP_CLAUSE_DEPEND_OUT;
28475 else
28476 goto invalid_kind;
28478 else
28479 goto invalid_kind;
28481 cp_lexer_consume_token (parser->lexer);
28482 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28483 goto resync_fail;
28485 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28486 NULL);
28488 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28489 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28491 return nlist;
28493 invalid_kind:
28494 cp_parser_error (parser, "invalid depend kind");
28495 resync_fail:
28496 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28497 /*or_comma=*/false,
28498 /*consume_paren=*/true);
28499 return list;
28502 /* OpenMP 4.0:
28503 map ( map-kind : variable-list )
28504 map ( variable-list )
28506 map-kind:
28507 alloc | to | from | tofrom */
28509 static tree
28510 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28512 tree nlist, c;
28513 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28515 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28516 return list;
28518 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28519 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28521 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28522 const char *p = IDENTIFIER_POINTER (id);
28524 if (strcmp ("alloc", p) == 0)
28525 kind = OMP_CLAUSE_MAP_ALLOC;
28526 else if (strcmp ("to", p) == 0)
28527 kind = OMP_CLAUSE_MAP_TO;
28528 else if (strcmp ("from", p) == 0)
28529 kind = OMP_CLAUSE_MAP_FROM;
28530 else if (strcmp ("tofrom", p) == 0)
28531 kind = OMP_CLAUSE_MAP_TOFROM;
28532 else
28534 cp_parser_error (parser, "invalid map kind");
28535 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28536 /*or_comma=*/false,
28537 /*consume_paren=*/true);
28538 return list;
28540 cp_lexer_consume_token (parser->lexer);
28541 cp_lexer_consume_token (parser->lexer);
28544 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28545 NULL);
28547 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28548 OMP_CLAUSE_MAP_KIND (c) = kind;
28550 return nlist;
28553 /* OpenMP 4.0:
28554 device ( expression ) */
28556 static tree
28557 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28558 location_t location)
28560 tree t, c;
28562 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28563 return list;
28565 t = cp_parser_expression (parser);
28567 if (t == error_mark_node
28568 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28569 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28570 /*or_comma=*/false,
28571 /*consume_paren=*/true);
28573 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28574 "device", location);
28576 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28577 OMP_CLAUSE_DEVICE_ID (c) = t;
28578 OMP_CLAUSE_CHAIN (c) = list;
28580 return c;
28583 /* OpenMP 4.0:
28584 dist_schedule ( static )
28585 dist_schedule ( static , expression ) */
28587 static tree
28588 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28589 location_t location)
28591 tree c, t;
28593 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28594 return list;
28596 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28598 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28599 goto invalid_kind;
28600 cp_lexer_consume_token (parser->lexer);
28602 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28604 cp_lexer_consume_token (parser->lexer);
28606 t = cp_parser_assignment_expression (parser);
28608 if (t == error_mark_node)
28609 goto resync_fail;
28610 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28612 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28613 goto resync_fail;
28615 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28616 goto resync_fail;
28618 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28619 location);
28620 OMP_CLAUSE_CHAIN (c) = list;
28621 return c;
28623 invalid_kind:
28624 cp_parser_error (parser, "invalid dist_schedule kind");
28625 resync_fail:
28626 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28627 /*or_comma=*/false,
28628 /*consume_paren=*/true);
28629 return list;
28632 /* OpenMP 4.0:
28633 proc_bind ( proc-bind-kind )
28635 proc-bind-kind:
28636 master | close | spread */
28638 static tree
28639 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28640 location_t location)
28642 tree c;
28643 enum omp_clause_proc_bind_kind kind;
28645 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28646 return list;
28648 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28650 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28651 const char *p = IDENTIFIER_POINTER (id);
28653 if (strcmp ("master", p) == 0)
28654 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28655 else if (strcmp ("close", p) == 0)
28656 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28657 else if (strcmp ("spread", p) == 0)
28658 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28659 else
28660 goto invalid_kind;
28662 else
28663 goto invalid_kind;
28665 cp_lexer_consume_token (parser->lexer);
28666 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28667 goto resync_fail;
28669 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28670 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28671 location);
28672 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28673 OMP_CLAUSE_CHAIN (c) = list;
28674 return c;
28676 invalid_kind:
28677 cp_parser_error (parser, "invalid depend kind");
28678 resync_fail:
28679 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28680 /*or_comma=*/false,
28681 /*consume_paren=*/true);
28682 return list;
28685 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28686 is a bitmask in MASK. Return the list of clauses found; the result
28687 of clause default goes in *pdefault. */
28689 static tree
28690 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28691 const char *where, cp_token *pragma_tok,
28692 bool finish_p = true)
28694 tree clauses = NULL;
28695 bool first = true;
28696 cp_token *token = NULL;
28697 bool cilk_simd_fn = false;
28699 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28701 pragma_omp_clause c_kind;
28702 const char *c_name;
28703 tree prev = clauses;
28705 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28706 cp_lexer_consume_token (parser->lexer);
28708 token = cp_lexer_peek_token (parser->lexer);
28709 c_kind = cp_parser_omp_clause_name (parser);
28711 switch (c_kind)
28713 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28714 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28715 token->location);
28716 c_name = "collapse";
28717 break;
28718 case PRAGMA_OMP_CLAUSE_COPYIN:
28719 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28720 c_name = "copyin";
28721 break;
28722 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28723 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28724 clauses);
28725 c_name = "copyprivate";
28726 break;
28727 case PRAGMA_OMP_CLAUSE_DEFAULT:
28728 clauses = cp_parser_omp_clause_default (parser, clauses,
28729 token->location);
28730 c_name = "default";
28731 break;
28732 case PRAGMA_OMP_CLAUSE_FINAL:
28733 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28734 c_name = "final";
28735 break;
28736 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28737 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28738 clauses);
28739 c_name = "firstprivate";
28740 break;
28741 case PRAGMA_OMP_CLAUSE_IF:
28742 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28743 c_name = "if";
28744 break;
28745 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28746 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28747 clauses);
28748 c_name = "lastprivate";
28749 break;
28750 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28751 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28752 token->location);
28753 c_name = "mergeable";
28754 break;
28755 case PRAGMA_OMP_CLAUSE_NOWAIT:
28756 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28757 c_name = "nowait";
28758 break;
28759 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28760 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28761 token->location);
28762 c_name = "num_threads";
28763 break;
28764 case PRAGMA_OMP_CLAUSE_ORDERED:
28765 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28766 token->location);
28767 c_name = "ordered";
28768 break;
28769 case PRAGMA_OMP_CLAUSE_PRIVATE:
28770 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28771 clauses);
28772 c_name = "private";
28773 break;
28774 case PRAGMA_OMP_CLAUSE_REDUCTION:
28775 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28776 c_name = "reduction";
28777 break;
28778 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28779 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28780 token->location);
28781 c_name = "schedule";
28782 break;
28783 case PRAGMA_OMP_CLAUSE_SHARED:
28784 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28785 clauses);
28786 c_name = "shared";
28787 break;
28788 case PRAGMA_OMP_CLAUSE_UNTIED:
28789 clauses = cp_parser_omp_clause_untied (parser, clauses,
28790 token->location);
28791 c_name = "untied";
28792 break;
28793 case PRAGMA_OMP_CLAUSE_INBRANCH:
28794 case PRAGMA_CILK_CLAUSE_MASK:
28795 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28796 clauses, token->location);
28797 c_name = "inbranch";
28798 break;
28799 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28800 case PRAGMA_CILK_CLAUSE_NOMASK:
28801 clauses = cp_parser_omp_clause_branch (parser,
28802 OMP_CLAUSE_NOTINBRANCH,
28803 clauses, token->location);
28804 c_name = "notinbranch";
28805 break;
28806 case PRAGMA_OMP_CLAUSE_PARALLEL:
28807 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28808 clauses, token->location);
28809 c_name = "parallel";
28810 if (!first)
28812 clause_not_first:
28813 error_at (token->location, "%qs must be the first clause of %qs",
28814 c_name, where);
28815 clauses = prev;
28817 break;
28818 case PRAGMA_OMP_CLAUSE_FOR:
28819 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28820 clauses, token->location);
28821 c_name = "for";
28822 if (!first)
28823 goto clause_not_first;
28824 break;
28825 case PRAGMA_OMP_CLAUSE_SECTIONS:
28826 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28827 clauses, token->location);
28828 c_name = "sections";
28829 if (!first)
28830 goto clause_not_first;
28831 break;
28832 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28833 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28834 clauses, token->location);
28835 c_name = "taskgroup";
28836 if (!first)
28837 goto clause_not_first;
28838 break;
28839 case PRAGMA_OMP_CLAUSE_TO:
28840 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28841 clauses);
28842 c_name = "to";
28843 break;
28844 case PRAGMA_OMP_CLAUSE_FROM:
28845 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28846 clauses);
28847 c_name = "from";
28848 break;
28849 case PRAGMA_OMP_CLAUSE_UNIFORM:
28850 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28851 clauses);
28852 c_name = "uniform";
28853 break;
28854 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28855 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28856 token->location);
28857 c_name = "num_teams";
28858 break;
28859 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28860 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28861 token->location);
28862 c_name = "thread_limit";
28863 break;
28864 case PRAGMA_OMP_CLAUSE_ALIGNED:
28865 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28866 c_name = "aligned";
28867 break;
28868 case PRAGMA_OMP_CLAUSE_LINEAR:
28869 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28870 cilk_simd_fn = true;
28871 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28872 c_name = "linear";
28873 break;
28874 case PRAGMA_OMP_CLAUSE_DEPEND:
28875 clauses = cp_parser_omp_clause_depend (parser, clauses);
28876 c_name = "depend";
28877 break;
28878 case PRAGMA_OMP_CLAUSE_MAP:
28879 clauses = cp_parser_omp_clause_map (parser, clauses);
28880 c_name = "map";
28881 break;
28882 case PRAGMA_OMP_CLAUSE_DEVICE:
28883 clauses = cp_parser_omp_clause_device (parser, clauses,
28884 token->location);
28885 c_name = "device";
28886 break;
28887 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28888 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28889 token->location);
28890 c_name = "dist_schedule";
28891 break;
28892 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28893 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28894 token->location);
28895 c_name = "proc_bind";
28896 break;
28897 case PRAGMA_OMP_CLAUSE_SAFELEN:
28898 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28899 token->location);
28900 c_name = "safelen";
28901 break;
28902 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28903 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28904 token->location);
28905 c_name = "simdlen";
28906 break;
28907 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28908 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28909 c_name = "simdlen";
28910 break;
28911 default:
28912 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28913 goto saw_error;
28916 first = false;
28918 if (((mask >> c_kind) & 1) == 0)
28920 /* Remove the invalid clause(s) from the list to avoid
28921 confusing the rest of the compiler. */
28922 clauses = prev;
28923 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28926 saw_error:
28927 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28928 no reason to skip to the end. */
28929 if (!(flag_cilkplus && pragma_tok == NULL))
28930 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28931 if (finish_p)
28932 return finish_omp_clauses (clauses);
28933 return clauses;
28936 /* OpenMP 2.5:
28937 structured-block:
28938 statement
28940 In practice, we're also interested in adding the statement to an
28941 outer node. So it is convenient if we work around the fact that
28942 cp_parser_statement calls add_stmt. */
28944 static unsigned
28945 cp_parser_begin_omp_structured_block (cp_parser *parser)
28947 unsigned save = parser->in_statement;
28949 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28950 This preserves the "not within loop or switch" style error messages
28951 for nonsense cases like
28952 void foo() {
28953 #pragma omp single
28954 break;
28957 if (parser->in_statement)
28958 parser->in_statement = IN_OMP_BLOCK;
28960 return save;
28963 static void
28964 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28966 parser->in_statement = save;
28969 static tree
28970 cp_parser_omp_structured_block (cp_parser *parser)
28972 tree stmt = begin_omp_structured_block ();
28973 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28975 cp_parser_statement (parser, NULL_TREE, false, NULL);
28977 cp_parser_end_omp_structured_block (parser, save);
28978 return finish_omp_structured_block (stmt);
28981 /* OpenMP 2.5:
28982 # pragma omp atomic new-line
28983 expression-stmt
28985 expression-stmt:
28986 x binop= expr | x++ | ++x | x-- | --x
28987 binop:
28988 +, *, -, /, &, ^, |, <<, >>
28990 where x is an lvalue expression with scalar type.
28992 OpenMP 3.1:
28993 # pragma omp atomic new-line
28994 update-stmt
28996 # pragma omp atomic read new-line
28997 read-stmt
28999 # pragma omp atomic write new-line
29000 write-stmt
29002 # pragma omp atomic update new-line
29003 update-stmt
29005 # pragma omp atomic capture new-line
29006 capture-stmt
29008 # pragma omp atomic capture new-line
29009 capture-block
29011 read-stmt:
29012 v = x
29013 write-stmt:
29014 x = expr
29015 update-stmt:
29016 expression-stmt | x = x binop expr
29017 capture-stmt:
29018 v = expression-stmt
29019 capture-block:
29020 { v = x; update-stmt; } | { update-stmt; v = x; }
29022 OpenMP 4.0:
29023 update-stmt:
29024 expression-stmt | x = x binop expr | x = expr binop x
29025 capture-stmt:
29026 v = update-stmt
29027 capture-block:
29028 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29030 where x and v are lvalue expressions with scalar type. */
29032 static void
29033 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29035 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29036 tree rhs1 = NULL_TREE, orig_lhs;
29037 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29038 bool structured_block = false;
29039 bool seq_cst = false;
29041 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29043 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29044 const char *p = IDENTIFIER_POINTER (id);
29046 if (!strcmp (p, "seq_cst"))
29048 seq_cst = true;
29049 cp_lexer_consume_token (parser->lexer);
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);
29055 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29057 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29058 const char *p = IDENTIFIER_POINTER (id);
29060 if (!strcmp (p, "read"))
29061 code = OMP_ATOMIC_READ;
29062 else if (!strcmp (p, "write"))
29063 code = NOP_EXPR;
29064 else if (!strcmp (p, "update"))
29065 code = OMP_ATOMIC;
29066 else if (!strcmp (p, "capture"))
29067 code = OMP_ATOMIC_CAPTURE_NEW;
29068 else
29069 p = NULL;
29070 if (p)
29071 cp_lexer_consume_token (parser->lexer);
29073 if (!seq_cst)
29075 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29076 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29077 cp_lexer_consume_token (parser->lexer);
29079 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29081 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29082 const char *p = IDENTIFIER_POINTER (id);
29084 if (!strcmp (p, "seq_cst"))
29086 seq_cst = true;
29087 cp_lexer_consume_token (parser->lexer);
29091 cp_parser_require_pragma_eol (parser, pragma_tok);
29093 switch (code)
29095 case OMP_ATOMIC_READ:
29096 case NOP_EXPR: /* atomic write */
29097 v = cp_parser_unary_expression (parser);
29098 if (v == error_mark_node)
29099 goto saw_error;
29100 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29101 goto saw_error;
29102 if (code == NOP_EXPR)
29103 lhs = cp_parser_expression (parser);
29104 else
29105 lhs = cp_parser_unary_expression (parser);
29106 if (lhs == error_mark_node)
29107 goto saw_error;
29108 if (code == NOP_EXPR)
29110 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29111 opcode. */
29112 code = OMP_ATOMIC;
29113 rhs = lhs;
29114 lhs = v;
29115 v = NULL_TREE;
29117 goto done;
29118 case OMP_ATOMIC_CAPTURE_NEW:
29119 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29121 cp_lexer_consume_token (parser->lexer);
29122 structured_block = true;
29124 else
29126 v = cp_parser_unary_expression (parser);
29127 if (v == error_mark_node)
29128 goto saw_error;
29129 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29130 goto saw_error;
29132 default:
29133 break;
29136 restart:
29137 lhs = cp_parser_unary_expression (parser);
29138 orig_lhs = lhs;
29139 switch (TREE_CODE (lhs))
29141 case ERROR_MARK:
29142 goto saw_error;
29144 case POSTINCREMENT_EXPR:
29145 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29146 code = OMP_ATOMIC_CAPTURE_OLD;
29147 /* FALLTHROUGH */
29148 case PREINCREMENT_EXPR:
29149 lhs = TREE_OPERAND (lhs, 0);
29150 opcode = PLUS_EXPR;
29151 rhs = integer_one_node;
29152 break;
29154 case POSTDECREMENT_EXPR:
29155 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29156 code = OMP_ATOMIC_CAPTURE_OLD;
29157 /* FALLTHROUGH */
29158 case PREDECREMENT_EXPR:
29159 lhs = TREE_OPERAND (lhs, 0);
29160 opcode = MINUS_EXPR;
29161 rhs = integer_one_node;
29162 break;
29164 case COMPOUND_EXPR:
29165 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29166 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29167 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29168 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29169 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29170 (TREE_OPERAND (lhs, 1), 0), 0)))
29171 == BOOLEAN_TYPE)
29172 /* Undo effects of boolean_increment for post {in,de}crement. */
29173 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29174 /* FALLTHRU */
29175 case MODIFY_EXPR:
29176 if (TREE_CODE (lhs) == MODIFY_EXPR
29177 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29179 /* Undo effects of boolean_increment. */
29180 if (integer_onep (TREE_OPERAND (lhs, 1)))
29182 /* This is pre or post increment. */
29183 rhs = TREE_OPERAND (lhs, 1);
29184 lhs = TREE_OPERAND (lhs, 0);
29185 opcode = NOP_EXPR;
29186 if (code == OMP_ATOMIC_CAPTURE_NEW
29187 && !structured_block
29188 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29189 code = OMP_ATOMIC_CAPTURE_OLD;
29190 break;
29193 /* FALLTHRU */
29194 default:
29195 switch (cp_lexer_peek_token (parser->lexer)->type)
29197 case CPP_MULT_EQ:
29198 opcode = MULT_EXPR;
29199 break;
29200 case CPP_DIV_EQ:
29201 opcode = TRUNC_DIV_EXPR;
29202 break;
29203 case CPP_PLUS_EQ:
29204 opcode = PLUS_EXPR;
29205 break;
29206 case CPP_MINUS_EQ:
29207 opcode = MINUS_EXPR;
29208 break;
29209 case CPP_LSHIFT_EQ:
29210 opcode = LSHIFT_EXPR;
29211 break;
29212 case CPP_RSHIFT_EQ:
29213 opcode = RSHIFT_EXPR;
29214 break;
29215 case CPP_AND_EQ:
29216 opcode = BIT_AND_EXPR;
29217 break;
29218 case CPP_OR_EQ:
29219 opcode = BIT_IOR_EXPR;
29220 break;
29221 case CPP_XOR_EQ:
29222 opcode = BIT_XOR_EXPR;
29223 break;
29224 case CPP_EQ:
29225 enum cp_parser_prec oprec;
29226 cp_token *token;
29227 cp_lexer_consume_token (parser->lexer);
29228 cp_parser_parse_tentatively (parser);
29229 rhs1 = cp_parser_simple_cast_expression (parser);
29230 if (rhs1 == error_mark_node)
29232 cp_parser_abort_tentative_parse (parser);
29233 cp_parser_simple_cast_expression (parser);
29234 goto saw_error;
29236 token = cp_lexer_peek_token (parser->lexer);
29237 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29239 cp_parser_abort_tentative_parse (parser);
29240 cp_parser_parse_tentatively (parser);
29241 rhs = cp_parser_binary_expression (parser, false, true,
29242 PREC_NOT_OPERATOR, NULL);
29243 if (rhs == error_mark_node)
29245 cp_parser_abort_tentative_parse (parser);
29246 cp_parser_binary_expression (parser, false, true,
29247 PREC_NOT_OPERATOR, NULL);
29248 goto saw_error;
29250 switch (TREE_CODE (rhs))
29252 case MULT_EXPR:
29253 case TRUNC_DIV_EXPR:
29254 case PLUS_EXPR:
29255 case MINUS_EXPR:
29256 case LSHIFT_EXPR:
29257 case RSHIFT_EXPR:
29258 case BIT_AND_EXPR:
29259 case BIT_IOR_EXPR:
29260 case BIT_XOR_EXPR:
29261 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29263 if (cp_parser_parse_definitely (parser))
29265 opcode = TREE_CODE (rhs);
29266 rhs1 = TREE_OPERAND (rhs, 0);
29267 rhs = TREE_OPERAND (rhs, 1);
29268 goto stmt_done;
29270 else
29271 goto saw_error;
29273 break;
29274 default:
29275 break;
29277 cp_parser_abort_tentative_parse (parser);
29278 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29280 rhs = cp_parser_expression (parser);
29281 if (rhs == error_mark_node)
29282 goto saw_error;
29283 opcode = NOP_EXPR;
29284 rhs1 = NULL_TREE;
29285 goto stmt_done;
29287 cp_parser_error (parser,
29288 "invalid form of %<#pragma omp atomic%>");
29289 goto saw_error;
29291 if (!cp_parser_parse_definitely (parser))
29292 goto saw_error;
29293 switch (token->type)
29295 case CPP_SEMICOLON:
29296 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29298 code = OMP_ATOMIC_CAPTURE_OLD;
29299 v = lhs;
29300 lhs = NULL_TREE;
29301 lhs1 = rhs1;
29302 rhs1 = NULL_TREE;
29303 cp_lexer_consume_token (parser->lexer);
29304 goto restart;
29306 else if (structured_block)
29308 opcode = NOP_EXPR;
29309 rhs = rhs1;
29310 rhs1 = NULL_TREE;
29311 goto stmt_done;
29313 cp_parser_error (parser,
29314 "invalid form of %<#pragma omp atomic%>");
29315 goto saw_error;
29316 case CPP_MULT:
29317 opcode = MULT_EXPR;
29318 break;
29319 case CPP_DIV:
29320 opcode = TRUNC_DIV_EXPR;
29321 break;
29322 case CPP_PLUS:
29323 opcode = PLUS_EXPR;
29324 break;
29325 case CPP_MINUS:
29326 opcode = MINUS_EXPR;
29327 break;
29328 case CPP_LSHIFT:
29329 opcode = LSHIFT_EXPR;
29330 break;
29331 case CPP_RSHIFT:
29332 opcode = RSHIFT_EXPR;
29333 break;
29334 case CPP_AND:
29335 opcode = BIT_AND_EXPR;
29336 break;
29337 case CPP_OR:
29338 opcode = BIT_IOR_EXPR;
29339 break;
29340 case CPP_XOR:
29341 opcode = BIT_XOR_EXPR;
29342 break;
29343 default:
29344 cp_parser_error (parser,
29345 "invalid operator for %<#pragma omp atomic%>");
29346 goto saw_error;
29348 oprec = TOKEN_PRECEDENCE (token);
29349 gcc_assert (oprec != PREC_NOT_OPERATOR);
29350 if (commutative_tree_code (opcode))
29351 oprec = (enum cp_parser_prec) (oprec - 1);
29352 cp_lexer_consume_token (parser->lexer);
29353 rhs = cp_parser_binary_expression (parser, false, false,
29354 oprec, NULL);
29355 if (rhs == error_mark_node)
29356 goto saw_error;
29357 goto stmt_done;
29358 /* FALLTHROUGH */
29359 default:
29360 cp_parser_error (parser,
29361 "invalid operator for %<#pragma omp atomic%>");
29362 goto saw_error;
29364 cp_lexer_consume_token (parser->lexer);
29366 rhs = cp_parser_expression (parser);
29367 if (rhs == error_mark_node)
29368 goto saw_error;
29369 break;
29371 stmt_done:
29372 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29374 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29375 goto saw_error;
29376 v = cp_parser_unary_expression (parser);
29377 if (v == error_mark_node)
29378 goto saw_error;
29379 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29380 goto saw_error;
29381 lhs1 = cp_parser_unary_expression (parser);
29382 if (lhs1 == error_mark_node)
29383 goto saw_error;
29385 if (structured_block)
29387 cp_parser_consume_semicolon_at_end_of_statement (parser);
29388 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29390 done:
29391 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29392 if (!structured_block)
29393 cp_parser_consume_semicolon_at_end_of_statement (parser);
29394 return;
29396 saw_error:
29397 cp_parser_skip_to_end_of_block_or_statement (parser);
29398 if (structured_block)
29400 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29401 cp_lexer_consume_token (parser->lexer);
29402 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29404 cp_parser_skip_to_end_of_block_or_statement (parser);
29405 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29406 cp_lexer_consume_token (parser->lexer);
29412 /* OpenMP 2.5:
29413 # pragma omp barrier new-line */
29415 static void
29416 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29418 cp_parser_require_pragma_eol (parser, pragma_tok);
29419 finish_omp_barrier ();
29422 /* OpenMP 2.5:
29423 # pragma omp critical [(name)] new-line
29424 structured-block */
29426 static tree
29427 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29429 tree stmt, name = NULL;
29431 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29433 cp_lexer_consume_token (parser->lexer);
29435 name = cp_parser_identifier (parser);
29437 if (name == error_mark_node
29438 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29439 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29440 /*or_comma=*/false,
29441 /*consume_paren=*/true);
29442 if (name == error_mark_node)
29443 name = NULL;
29445 cp_parser_require_pragma_eol (parser, pragma_tok);
29447 stmt = cp_parser_omp_structured_block (parser);
29448 return c_finish_omp_critical (input_location, stmt, name);
29451 /* OpenMP 2.5:
29452 # pragma omp flush flush-vars[opt] new-line
29454 flush-vars:
29455 ( variable-list ) */
29457 static void
29458 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29461 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29462 cp_parser_require_pragma_eol (parser, pragma_tok);
29464 finish_omp_flush ();
29467 /* Helper function, to parse omp for increment expression. */
29469 static tree
29470 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29472 tree cond = cp_parser_binary_expression (parser, false, true,
29473 PREC_NOT_OPERATOR, NULL);
29474 if (cond == error_mark_node
29475 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29477 cp_parser_skip_to_end_of_statement (parser);
29478 return error_mark_node;
29481 switch (TREE_CODE (cond))
29483 case GT_EXPR:
29484 case GE_EXPR:
29485 case LT_EXPR:
29486 case LE_EXPR:
29487 break;
29488 case NE_EXPR:
29489 if (code == CILK_SIMD || code == CILK_FOR)
29490 break;
29491 /* Fall through: OpenMP disallows NE_EXPR. */
29492 default:
29493 return error_mark_node;
29496 /* If decl is an iterator, preserve LHS and RHS of the relational
29497 expr until finish_omp_for. */
29498 if (decl
29499 && (type_dependent_expression_p (decl)
29500 || CLASS_TYPE_P (TREE_TYPE (decl))))
29501 return cond;
29503 return build_x_binary_op (input_location, TREE_CODE (cond),
29504 TREE_OPERAND (cond, 0), ERROR_MARK,
29505 TREE_OPERAND (cond, 1), ERROR_MARK,
29506 /*overload=*/NULL, tf_warning_or_error);
29509 /* Helper function, to parse omp for increment expression. */
29511 static tree
29512 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29514 cp_token *token = cp_lexer_peek_token (parser->lexer);
29515 enum tree_code op;
29516 tree lhs, rhs;
29517 cp_id_kind idk;
29518 bool decl_first;
29520 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29522 op = (token->type == CPP_PLUS_PLUS
29523 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29524 cp_lexer_consume_token (parser->lexer);
29525 lhs = cp_parser_simple_cast_expression (parser);
29526 if (lhs != decl)
29527 return error_mark_node;
29528 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29531 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29532 if (lhs != decl)
29533 return error_mark_node;
29535 token = cp_lexer_peek_token (parser->lexer);
29536 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29538 op = (token->type == CPP_PLUS_PLUS
29539 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29540 cp_lexer_consume_token (parser->lexer);
29541 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29544 op = cp_parser_assignment_operator_opt (parser);
29545 if (op == ERROR_MARK)
29546 return error_mark_node;
29548 if (op != NOP_EXPR)
29550 rhs = cp_parser_assignment_expression (parser);
29551 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29552 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29555 lhs = cp_parser_binary_expression (parser, false, false,
29556 PREC_ADDITIVE_EXPRESSION, NULL);
29557 token = cp_lexer_peek_token (parser->lexer);
29558 decl_first = lhs == decl;
29559 if (decl_first)
29560 lhs = NULL_TREE;
29561 if (token->type != CPP_PLUS
29562 && token->type != CPP_MINUS)
29563 return error_mark_node;
29567 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29568 cp_lexer_consume_token (parser->lexer);
29569 rhs = cp_parser_binary_expression (parser, false, false,
29570 PREC_ADDITIVE_EXPRESSION, NULL);
29571 token = cp_lexer_peek_token (parser->lexer);
29572 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29574 if (lhs == NULL_TREE)
29576 if (op == PLUS_EXPR)
29577 lhs = rhs;
29578 else
29579 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29580 tf_warning_or_error);
29582 else
29583 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29584 ERROR_MARK, NULL, tf_warning_or_error);
29587 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29589 if (!decl_first)
29591 if (rhs != decl || op == MINUS_EXPR)
29592 return error_mark_node;
29593 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29595 else
29596 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29598 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29601 /* Parse the initialization statement of either an OpenMP for loop or
29602 a Cilk Plus for loop.
29604 Return true if the resulting construct should have an
29605 OMP_CLAUSE_PRIVATE added to it. */
29607 static bool
29608 cp_parser_omp_for_loop_init (cp_parser *parser,
29609 enum tree_code code,
29610 tree &this_pre_body,
29611 vec<tree, va_gc> *for_block,
29612 tree &init,
29613 tree &decl,
29614 tree &real_decl)
29616 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29617 return false;
29619 bool add_private_clause = false;
29621 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29623 init-expr:
29624 var = lb
29625 integer-type var = lb
29626 random-access-iterator-type var = lb
29627 pointer-type var = lb
29629 cp_decl_specifier_seq type_specifiers;
29631 /* First, try to parse as an initialized declaration. See
29632 cp_parser_condition, from whence the bulk of this is copied. */
29634 cp_parser_parse_tentatively (parser);
29635 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29636 /*is_trailing_return=*/false,
29637 &type_specifiers);
29638 if (cp_parser_parse_definitely (parser))
29640 /* If parsing a type specifier seq succeeded, then this
29641 MUST be a initialized declaration. */
29642 tree asm_specification, attributes;
29643 cp_declarator *declarator;
29645 declarator = cp_parser_declarator (parser,
29646 CP_PARSER_DECLARATOR_NAMED,
29647 /*ctor_dtor_or_conv_p=*/NULL,
29648 /*parenthesized_p=*/NULL,
29649 /*member_p=*/false,
29650 /*friend_p=*/false);
29651 attributes = cp_parser_attributes_opt (parser);
29652 asm_specification = cp_parser_asm_specification_opt (parser);
29654 if (declarator == cp_error_declarator)
29655 cp_parser_skip_to_end_of_statement (parser);
29657 else
29659 tree pushed_scope, auto_node;
29661 decl = start_decl (declarator, &type_specifiers,
29662 SD_INITIALIZED, attributes,
29663 /*prefix_attributes=*/NULL_TREE,
29664 &pushed_scope);
29666 auto_node = type_uses_auto (TREE_TYPE (decl));
29667 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29669 if (cp_lexer_next_token_is (parser->lexer,
29670 CPP_OPEN_PAREN))
29672 if (code != CILK_SIMD && code != CILK_FOR)
29673 error ("parenthesized initialization is not allowed in "
29674 "OpenMP %<for%> loop");
29675 else
29676 error ("parenthesized initialization is "
29677 "not allowed in for-loop");
29679 else
29680 /* Trigger an error. */
29681 cp_parser_require (parser, CPP_EQ, RT_EQ);
29683 init = error_mark_node;
29684 cp_parser_skip_to_end_of_statement (parser);
29686 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29687 || type_dependent_expression_p (decl)
29688 || auto_node)
29690 bool is_direct_init, is_non_constant_init;
29692 init = cp_parser_initializer (parser,
29693 &is_direct_init,
29694 &is_non_constant_init);
29696 if (auto_node)
29698 TREE_TYPE (decl)
29699 = do_auto_deduction (TREE_TYPE (decl), init,
29700 auto_node);
29702 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29703 && !type_dependent_expression_p (decl))
29704 goto non_class;
29707 cp_finish_decl (decl, init, !is_non_constant_init,
29708 asm_specification,
29709 LOOKUP_ONLYCONVERTING);
29710 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29712 vec_safe_push (for_block, this_pre_body);
29713 init = NULL_TREE;
29715 else
29716 init = pop_stmt_list (this_pre_body);
29717 this_pre_body = NULL_TREE;
29719 else
29721 /* Consume '='. */
29722 cp_lexer_consume_token (parser->lexer);
29723 init = cp_parser_assignment_expression (parser);
29725 non_class:
29726 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29727 init = error_mark_node;
29728 else
29729 cp_finish_decl (decl, NULL_TREE,
29730 /*init_const_expr_p=*/false,
29731 asm_specification,
29732 LOOKUP_ONLYCONVERTING);
29735 if (pushed_scope)
29736 pop_scope (pushed_scope);
29739 else
29741 cp_id_kind idk;
29742 /* If parsing a type specifier sequence failed, then
29743 this MUST be a simple expression. */
29744 if (code == CILK_FOR)
29745 error ("%<_Cilk_for%> allows expression instead of declaration only "
29746 "in C, not in C++");
29747 cp_parser_parse_tentatively (parser);
29748 decl = cp_parser_primary_expression (parser, false, false,
29749 false, &idk);
29750 if (!cp_parser_error_occurred (parser)
29751 && decl
29752 && DECL_P (decl)
29753 && CLASS_TYPE_P (TREE_TYPE (decl)))
29755 tree rhs;
29757 cp_parser_parse_definitely (parser);
29758 cp_parser_require (parser, CPP_EQ, RT_EQ);
29759 rhs = cp_parser_assignment_expression (parser);
29760 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29761 decl, NOP_EXPR,
29762 rhs,
29763 tf_warning_or_error));
29764 add_private_clause = true;
29766 else
29768 decl = NULL;
29769 cp_parser_abort_tentative_parse (parser);
29770 init = cp_parser_expression (parser);
29771 if (init)
29773 if (TREE_CODE (init) == MODIFY_EXPR
29774 || TREE_CODE (init) == MODOP_EXPR)
29775 real_decl = TREE_OPERAND (init, 0);
29779 return add_private_clause;
29782 /* Parse the restricted form of the for statement allowed by OpenMP. */
29784 static tree
29785 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29786 tree *cclauses)
29788 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29789 tree real_decl, initv, condv, incrv, declv;
29790 tree this_pre_body, cl;
29791 location_t loc_first;
29792 bool collapse_err = false;
29793 int i, collapse = 1, nbraces = 0;
29794 vec<tree, va_gc> *for_block = make_tree_vector ();
29796 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29797 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29798 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29800 gcc_assert (collapse >= 1);
29802 declv = make_tree_vec (collapse);
29803 initv = make_tree_vec (collapse);
29804 condv = make_tree_vec (collapse);
29805 incrv = make_tree_vec (collapse);
29807 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29809 for (i = 0; i < collapse; i++)
29811 int bracecount = 0;
29812 bool add_private_clause = false;
29813 location_t loc;
29815 if (code != CILK_FOR
29816 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29818 cp_parser_error (parser, "for statement expected");
29819 return NULL;
29821 if (code == CILK_FOR
29822 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
29824 cp_parser_error (parser, "_Cilk_for statement expected");
29825 return NULL;
29827 loc = cp_lexer_consume_token (parser->lexer)->location;
29829 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29830 return NULL;
29832 init = decl = real_decl = NULL;
29833 this_pre_body = push_stmt_list ();
29835 add_private_clause
29836 |= cp_parser_omp_for_loop_init (parser, code,
29837 this_pre_body, for_block,
29838 init, decl, real_decl);
29840 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29841 if (this_pre_body)
29843 this_pre_body = pop_stmt_list (this_pre_body);
29844 if (pre_body)
29846 tree t = pre_body;
29847 pre_body = push_stmt_list ();
29848 add_stmt (t);
29849 add_stmt (this_pre_body);
29850 pre_body = pop_stmt_list (pre_body);
29852 else
29853 pre_body = this_pre_body;
29856 if (decl)
29857 real_decl = decl;
29858 if (cclauses != NULL
29859 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29860 && real_decl != NULL_TREE)
29862 tree *c;
29863 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29864 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29865 && OMP_CLAUSE_DECL (*c) == real_decl)
29867 error_at (loc, "iteration variable %qD"
29868 " should not be firstprivate", real_decl);
29869 *c = OMP_CLAUSE_CHAIN (*c);
29871 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29872 && OMP_CLAUSE_DECL (*c) == real_decl)
29874 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29875 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29876 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29877 OMP_CLAUSE_DECL (l) = real_decl;
29878 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29879 if (code == OMP_SIMD)
29881 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29882 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
29884 else
29886 OMP_CLAUSE_CHAIN (l) = clauses;
29887 clauses = l;
29889 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29890 CP_OMP_CLAUSE_INFO (*c) = NULL;
29891 add_private_clause = false;
29893 else
29895 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29896 && OMP_CLAUSE_DECL (*c) == real_decl)
29897 add_private_clause = false;
29898 c = &OMP_CLAUSE_CHAIN (*c);
29902 if (add_private_clause)
29904 tree c;
29905 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29907 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29908 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29909 && OMP_CLAUSE_DECL (c) == decl)
29910 break;
29911 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29912 && OMP_CLAUSE_DECL (c) == decl)
29913 error_at (loc, "iteration variable %qD "
29914 "should not be firstprivate",
29915 decl);
29916 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29917 && OMP_CLAUSE_DECL (c) == decl)
29918 error_at (loc, "iteration variable %qD should not be reduction",
29919 decl);
29921 if (c == NULL)
29923 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29924 OMP_CLAUSE_DECL (c) = decl;
29925 c = finish_omp_clauses (c);
29926 if (c)
29928 OMP_CLAUSE_CHAIN (c) = clauses;
29929 clauses = c;
29934 cond = NULL;
29935 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29936 cond = cp_parser_omp_for_cond (parser, decl, code);
29937 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29939 incr = NULL;
29940 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29942 /* If decl is an iterator, preserve the operator on decl
29943 until finish_omp_for. */
29944 if (real_decl
29945 && ((processing_template_decl
29946 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29947 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29948 incr = cp_parser_omp_for_incr (parser, real_decl);
29949 else
29950 incr = cp_parser_expression (parser);
29951 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29952 SET_EXPR_LOCATION (incr, input_location);
29955 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29956 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29957 /*or_comma=*/false,
29958 /*consume_paren=*/true);
29960 TREE_VEC_ELT (declv, i) = decl;
29961 TREE_VEC_ELT (initv, i) = init;
29962 TREE_VEC_ELT (condv, i) = cond;
29963 TREE_VEC_ELT (incrv, i) = incr;
29965 if (i == collapse - 1)
29966 break;
29968 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29969 in between the collapsed for loops to be still considered perfectly
29970 nested. Hopefully the final version clarifies this.
29971 For now handle (multiple) {'s and empty statements. */
29972 cp_parser_parse_tentatively (parser);
29975 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29976 break;
29977 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29979 cp_lexer_consume_token (parser->lexer);
29980 bracecount++;
29982 else if (bracecount
29983 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29984 cp_lexer_consume_token (parser->lexer);
29985 else
29987 loc = cp_lexer_peek_token (parser->lexer)->location;
29988 error_at (loc, "not enough collapsed for loops");
29989 collapse_err = true;
29990 cp_parser_abort_tentative_parse (parser);
29991 declv = NULL_TREE;
29992 break;
29995 while (1);
29997 if (declv)
29999 cp_parser_parse_definitely (parser);
30000 nbraces += bracecount;
30004 /* Note that we saved the original contents of this flag when we entered
30005 the structured block, and so we don't need to re-save it here. */
30006 if (code == CILK_SIMD || code == CILK_FOR)
30007 parser->in_statement = IN_CILK_SIMD_FOR;
30008 else
30009 parser->in_statement = IN_OMP_FOR;
30011 /* Note that the grammar doesn't call for a structured block here,
30012 though the loop as a whole is a structured block. */
30013 body = push_stmt_list ();
30014 cp_parser_statement (parser, NULL_TREE, false, NULL);
30015 body = pop_stmt_list (body);
30017 if (declv == NULL_TREE)
30018 ret = NULL_TREE;
30019 else
30020 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30021 pre_body, clauses);
30023 while (nbraces)
30025 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30027 cp_lexer_consume_token (parser->lexer);
30028 nbraces--;
30030 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30031 cp_lexer_consume_token (parser->lexer);
30032 else
30034 if (!collapse_err)
30036 error_at (cp_lexer_peek_token (parser->lexer)->location,
30037 "collapsed loops not perfectly nested");
30039 collapse_err = true;
30040 cp_parser_statement_seq_opt (parser, NULL);
30041 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30042 break;
30046 while (!for_block->is_empty ())
30047 add_stmt (pop_stmt_list (for_block->pop ()));
30048 release_tree_vector (for_block);
30050 return ret;
30053 /* Helper function for OpenMP parsing, split clauses and call
30054 finish_omp_clauses on each of the set of clauses afterwards. */
30056 static void
30057 cp_omp_split_clauses (location_t loc, enum tree_code code,
30058 omp_clause_mask mask, tree clauses, tree *cclauses)
30060 int i;
30061 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30062 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30063 if (cclauses[i])
30064 cclauses[i] = finish_omp_clauses (cclauses[i]);
30067 /* OpenMP 4.0:
30068 #pragma omp simd simd-clause[optseq] new-line
30069 for-loop */
30071 #define OMP_SIMD_CLAUSE_MASK \
30072 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30080 static tree
30081 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30082 char *p_name, omp_clause_mask mask, tree *cclauses)
30084 tree clauses, sb, ret;
30085 unsigned int save;
30086 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30088 strcat (p_name, " simd");
30089 mask |= OMP_SIMD_CLAUSE_MASK;
30090 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30092 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30093 cclauses == NULL);
30094 if (cclauses)
30096 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30097 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30100 sb = begin_omp_structured_block ();
30101 save = cp_parser_begin_omp_structured_block (parser);
30103 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30105 cp_parser_end_omp_structured_block (parser, save);
30106 add_stmt (finish_omp_structured_block (sb));
30108 return ret;
30111 /* OpenMP 2.5:
30112 #pragma omp for for-clause[optseq] new-line
30113 for-loop
30115 OpenMP 4.0:
30116 #pragma omp for simd for-simd-clause[optseq] new-line
30117 for-loop */
30119 #define OMP_FOR_CLAUSE_MASK \
30120 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30129 static tree
30130 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30131 char *p_name, omp_clause_mask mask, tree *cclauses)
30133 tree clauses, sb, ret;
30134 unsigned int save;
30135 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30137 strcat (p_name, " for");
30138 mask |= OMP_FOR_CLAUSE_MASK;
30139 if (cclauses)
30140 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30142 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30144 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30145 const char *p = IDENTIFIER_POINTER (id);
30147 if (strcmp (p, "simd") == 0)
30149 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30150 if (cclauses == NULL)
30151 cclauses = cclauses_buf;
30153 cp_lexer_consume_token (parser->lexer);
30154 if (!flag_openmp) /* flag_openmp_simd */
30155 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30156 cclauses);
30157 sb = begin_omp_structured_block ();
30158 save = cp_parser_begin_omp_structured_block (parser);
30159 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30160 cclauses);
30161 cp_parser_end_omp_structured_block (parser, save);
30162 tree body = finish_omp_structured_block (sb);
30163 if (ret == NULL)
30164 return ret;
30165 ret = make_node (OMP_FOR);
30166 TREE_TYPE (ret) = void_type_node;
30167 OMP_FOR_BODY (ret) = body;
30168 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30169 SET_EXPR_LOCATION (ret, loc);
30170 add_stmt (ret);
30171 return ret;
30174 if (!flag_openmp) /* flag_openmp_simd */
30176 cp_parser_require_pragma_eol (parser, pragma_tok);
30177 return NULL_TREE;
30180 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30181 cclauses == NULL);
30182 if (cclauses)
30184 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30185 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30188 sb = begin_omp_structured_block ();
30189 save = cp_parser_begin_omp_structured_block (parser);
30191 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30193 cp_parser_end_omp_structured_block (parser, save);
30194 add_stmt (finish_omp_structured_block (sb));
30196 return ret;
30199 /* OpenMP 2.5:
30200 # pragma omp master new-line
30201 structured-block */
30203 static tree
30204 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30206 cp_parser_require_pragma_eol (parser, pragma_tok);
30207 return c_finish_omp_master (input_location,
30208 cp_parser_omp_structured_block (parser));
30211 /* OpenMP 2.5:
30212 # pragma omp ordered new-line
30213 structured-block */
30215 static tree
30216 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30218 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30219 cp_parser_require_pragma_eol (parser, pragma_tok);
30220 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30223 /* OpenMP 2.5:
30225 section-scope:
30226 { section-sequence }
30228 section-sequence:
30229 section-directive[opt] structured-block
30230 section-sequence section-directive structured-block */
30232 static tree
30233 cp_parser_omp_sections_scope (cp_parser *parser)
30235 tree stmt, substmt;
30236 bool error_suppress = false;
30237 cp_token *tok;
30239 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30240 return NULL_TREE;
30242 stmt = push_stmt_list ();
30244 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30246 substmt = cp_parser_omp_structured_block (parser);
30247 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30248 add_stmt (substmt);
30251 while (1)
30253 tok = cp_lexer_peek_token (parser->lexer);
30254 if (tok->type == CPP_CLOSE_BRACE)
30255 break;
30256 if (tok->type == CPP_EOF)
30257 break;
30259 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30261 cp_lexer_consume_token (parser->lexer);
30262 cp_parser_require_pragma_eol (parser, tok);
30263 error_suppress = false;
30265 else if (!error_suppress)
30267 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30268 error_suppress = true;
30271 substmt = cp_parser_omp_structured_block (parser);
30272 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30273 add_stmt (substmt);
30275 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30277 substmt = pop_stmt_list (stmt);
30279 stmt = make_node (OMP_SECTIONS);
30280 TREE_TYPE (stmt) = void_type_node;
30281 OMP_SECTIONS_BODY (stmt) = substmt;
30283 add_stmt (stmt);
30284 return stmt;
30287 /* OpenMP 2.5:
30288 # pragma omp sections sections-clause[optseq] newline
30289 sections-scope */
30291 #define OMP_SECTIONS_CLAUSE_MASK \
30292 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30298 static tree
30299 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30300 char *p_name, omp_clause_mask mask, tree *cclauses)
30302 tree clauses, ret;
30303 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30305 strcat (p_name, " sections");
30306 mask |= OMP_SECTIONS_CLAUSE_MASK;
30307 if (cclauses)
30308 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30310 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30311 cclauses == NULL);
30312 if (cclauses)
30314 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30315 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30318 ret = cp_parser_omp_sections_scope (parser);
30319 if (ret)
30320 OMP_SECTIONS_CLAUSES (ret) = clauses;
30322 return ret;
30325 /* OpenMP 2.5:
30326 # pragma omp parallel parallel-clause[optseq] new-line
30327 structured-block
30328 # pragma omp parallel for parallel-for-clause[optseq] new-line
30329 structured-block
30330 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30331 structured-block
30333 OpenMP 4.0:
30334 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30335 structured-block */
30337 #define OMP_PARALLEL_CLAUSE_MASK \
30338 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30348 static tree
30349 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30350 char *p_name, omp_clause_mask mask, tree *cclauses)
30352 tree stmt, clauses, block;
30353 unsigned int save;
30354 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30356 strcat (p_name, " parallel");
30357 mask |= OMP_PARALLEL_CLAUSE_MASK;
30359 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30361 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30362 if (cclauses == NULL)
30363 cclauses = cclauses_buf;
30365 cp_lexer_consume_token (parser->lexer);
30366 if (!flag_openmp) /* flag_openmp_simd */
30367 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30368 block = begin_omp_parallel ();
30369 save = cp_parser_begin_omp_structured_block (parser);
30370 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30371 cp_parser_end_omp_structured_block (parser, save);
30372 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30373 block);
30374 if (ret == NULL_TREE)
30375 return ret;
30376 OMP_PARALLEL_COMBINED (stmt) = 1;
30377 return stmt;
30379 else if (cclauses)
30381 error_at (loc, "expected %<for%> after %qs", p_name);
30382 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30383 return NULL_TREE;
30385 else if (!flag_openmp) /* flag_openmp_simd */
30387 cp_parser_require_pragma_eol (parser, pragma_tok);
30388 return NULL_TREE;
30390 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30392 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30393 const char *p = IDENTIFIER_POINTER (id);
30394 if (strcmp (p, "sections") == 0)
30396 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30397 cclauses = cclauses_buf;
30399 cp_lexer_consume_token (parser->lexer);
30400 block = begin_omp_parallel ();
30401 save = cp_parser_begin_omp_structured_block (parser);
30402 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30403 cp_parser_end_omp_structured_block (parser, save);
30404 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30405 block);
30406 OMP_PARALLEL_COMBINED (stmt) = 1;
30407 return stmt;
30411 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30413 block = begin_omp_parallel ();
30414 save = cp_parser_begin_omp_structured_block (parser);
30415 cp_parser_statement (parser, NULL_TREE, false, NULL);
30416 cp_parser_end_omp_structured_block (parser, save);
30417 stmt = finish_omp_parallel (clauses, block);
30418 return stmt;
30421 /* OpenMP 2.5:
30422 # pragma omp single single-clause[optseq] new-line
30423 structured-block */
30425 #define OMP_SINGLE_CLAUSE_MASK \
30426 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30431 static tree
30432 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30434 tree stmt = make_node (OMP_SINGLE);
30435 TREE_TYPE (stmt) = void_type_node;
30437 OMP_SINGLE_CLAUSES (stmt)
30438 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30439 "#pragma omp single", pragma_tok);
30440 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30442 return add_stmt (stmt);
30445 /* OpenMP 3.0:
30446 # pragma omp task task-clause[optseq] new-line
30447 structured-block */
30449 #define OMP_TASK_CLAUSE_MASK \
30450 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30460 static tree
30461 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30463 tree clauses, block;
30464 unsigned int save;
30466 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30467 "#pragma omp task", pragma_tok);
30468 block = begin_omp_task ();
30469 save = cp_parser_begin_omp_structured_block (parser);
30470 cp_parser_statement (parser, NULL_TREE, false, NULL);
30471 cp_parser_end_omp_structured_block (parser, save);
30472 return finish_omp_task (clauses, block);
30475 /* OpenMP 3.0:
30476 # pragma omp taskwait new-line */
30478 static void
30479 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30481 cp_parser_require_pragma_eol (parser, pragma_tok);
30482 finish_omp_taskwait ();
30485 /* OpenMP 3.1:
30486 # pragma omp taskyield new-line */
30488 static void
30489 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30491 cp_parser_require_pragma_eol (parser, pragma_tok);
30492 finish_omp_taskyield ();
30495 /* OpenMP 4.0:
30496 # pragma omp taskgroup new-line
30497 structured-block */
30499 static tree
30500 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30502 cp_parser_require_pragma_eol (parser, pragma_tok);
30503 return c_finish_omp_taskgroup (input_location,
30504 cp_parser_omp_structured_block (parser));
30508 /* OpenMP 2.5:
30509 # pragma omp threadprivate (variable-list) */
30511 static void
30512 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30514 tree vars;
30516 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30517 cp_parser_require_pragma_eol (parser, pragma_tok);
30519 finish_omp_threadprivate (vars);
30522 /* OpenMP 4.0:
30523 # pragma omp cancel cancel-clause[optseq] new-line */
30525 #define OMP_CANCEL_CLAUSE_MASK \
30526 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30532 static void
30533 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30535 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30536 "#pragma omp cancel", pragma_tok);
30537 finish_omp_cancel (clauses);
30540 /* OpenMP 4.0:
30541 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30543 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30544 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30549 static void
30550 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30552 tree clauses;
30553 bool point_seen = false;
30555 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30557 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30558 const char *p = IDENTIFIER_POINTER (id);
30560 if (strcmp (p, "point") == 0)
30562 cp_lexer_consume_token (parser->lexer);
30563 point_seen = true;
30566 if (!point_seen)
30568 cp_parser_error (parser, "expected %<point%>");
30569 cp_parser_require_pragma_eol (parser, pragma_tok);
30570 return;
30573 clauses = cp_parser_omp_all_clauses (parser,
30574 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30575 "#pragma omp cancellation point",
30576 pragma_tok);
30577 finish_omp_cancellation_point (clauses);
30580 /* OpenMP 4.0:
30581 #pragma omp distribute distribute-clause[optseq] new-line
30582 for-loop */
30584 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30585 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30590 static tree
30591 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30592 char *p_name, omp_clause_mask mask, tree *cclauses)
30594 tree clauses, sb, ret;
30595 unsigned int save;
30596 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30598 strcat (p_name, " distribute");
30599 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30601 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30603 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30604 const char *p = IDENTIFIER_POINTER (id);
30605 bool simd = false;
30606 bool parallel = false;
30608 if (strcmp (p, "simd") == 0)
30609 simd = true;
30610 else
30611 parallel = strcmp (p, "parallel") == 0;
30612 if (parallel || simd)
30614 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30615 if (cclauses == NULL)
30616 cclauses = cclauses_buf;
30617 cp_lexer_consume_token (parser->lexer);
30618 if (!flag_openmp) /* flag_openmp_simd */
30620 if (simd)
30621 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30622 cclauses);
30623 else
30624 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30625 cclauses);
30627 sb = begin_omp_structured_block ();
30628 save = cp_parser_begin_omp_structured_block (parser);
30629 if (simd)
30630 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30631 cclauses);
30632 else
30633 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30634 cclauses);
30635 cp_parser_end_omp_structured_block (parser, save);
30636 tree body = finish_omp_structured_block (sb);
30637 if (ret == NULL)
30638 return ret;
30639 ret = make_node (OMP_DISTRIBUTE);
30640 TREE_TYPE (ret) = void_type_node;
30641 OMP_FOR_BODY (ret) = body;
30642 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30643 SET_EXPR_LOCATION (ret, loc);
30644 add_stmt (ret);
30645 return ret;
30648 if (!flag_openmp) /* flag_openmp_simd */
30650 cp_parser_require_pragma_eol (parser, pragma_tok);
30651 return NULL_TREE;
30654 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30655 cclauses == NULL);
30656 if (cclauses)
30658 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30659 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30662 sb = begin_omp_structured_block ();
30663 save = cp_parser_begin_omp_structured_block (parser);
30665 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30667 cp_parser_end_omp_structured_block (parser, save);
30668 add_stmt (finish_omp_structured_block (sb));
30670 return ret;
30673 /* OpenMP 4.0:
30674 # pragma omp teams teams-clause[optseq] new-line
30675 structured-block */
30677 #define OMP_TEAMS_CLAUSE_MASK \
30678 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30686 static tree
30687 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30688 char *p_name, omp_clause_mask mask, tree *cclauses)
30690 tree clauses, sb, ret;
30691 unsigned int save;
30692 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30694 strcat (p_name, " teams");
30695 mask |= OMP_TEAMS_CLAUSE_MASK;
30697 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30699 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30700 const char *p = IDENTIFIER_POINTER (id);
30701 if (strcmp (p, "distribute") == 0)
30703 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30704 if (cclauses == NULL)
30705 cclauses = cclauses_buf;
30707 cp_lexer_consume_token (parser->lexer);
30708 if (!flag_openmp) /* flag_openmp_simd */
30709 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30710 cclauses);
30711 sb = begin_omp_structured_block ();
30712 save = cp_parser_begin_omp_structured_block (parser);
30713 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30714 cclauses);
30715 cp_parser_end_omp_structured_block (parser, save);
30716 tree body = finish_omp_structured_block (sb);
30717 if (ret == NULL)
30718 return ret;
30719 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30720 ret = make_node (OMP_TEAMS);
30721 TREE_TYPE (ret) = void_type_node;
30722 OMP_TEAMS_CLAUSES (ret) = clauses;
30723 OMP_TEAMS_BODY (ret) = body;
30724 return add_stmt (ret);
30727 if (!flag_openmp) /* flag_openmp_simd */
30729 cp_parser_require_pragma_eol (parser, pragma_tok);
30730 return NULL_TREE;
30733 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30734 cclauses == NULL);
30735 if (cclauses)
30737 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30738 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30741 tree stmt = make_node (OMP_TEAMS);
30742 TREE_TYPE (stmt) = void_type_node;
30743 OMP_TEAMS_CLAUSES (stmt) = clauses;
30744 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30746 return add_stmt (stmt);
30749 /* OpenMP 4.0:
30750 # pragma omp target data target-data-clause[optseq] new-line
30751 structured-block */
30753 #define OMP_TARGET_DATA_CLAUSE_MASK \
30754 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30758 static tree
30759 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30761 tree stmt = make_node (OMP_TARGET_DATA);
30762 TREE_TYPE (stmt) = void_type_node;
30764 OMP_TARGET_DATA_CLAUSES (stmt)
30765 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30766 "#pragma omp target data", pragma_tok);
30767 keep_next_level (true);
30768 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30770 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30771 return add_stmt (stmt);
30774 /* OpenMP 4.0:
30775 # pragma omp target update target-update-clause[optseq] new-line */
30777 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30778 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30783 static bool
30784 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30785 enum pragma_context context)
30787 if (context == pragma_stmt)
30789 error_at (pragma_tok->location,
30790 "%<#pragma omp target update%> may only be "
30791 "used in compound statements");
30792 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30793 return false;
30796 tree clauses
30797 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30798 "#pragma omp target update", pragma_tok);
30799 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30800 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30802 error_at (pragma_tok->location,
30803 "%<#pragma omp target update must contain at least one "
30804 "%<from%> or %<to%> clauses");
30805 return false;
30808 tree stmt = make_node (OMP_TARGET_UPDATE);
30809 TREE_TYPE (stmt) = void_type_node;
30810 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30811 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30812 add_stmt (stmt);
30813 return false;
30816 /* OpenMP 4.0:
30817 # pragma omp target target-clause[optseq] new-line
30818 structured-block */
30820 #define OMP_TARGET_CLAUSE_MASK \
30821 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30825 static bool
30826 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30827 enum pragma_context context)
30829 if (context != pragma_stmt && context != pragma_compound)
30831 cp_parser_error (parser, "expected declaration specifiers");
30832 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30833 return false;
30836 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30838 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30839 const char *p = IDENTIFIER_POINTER (id);
30841 if (strcmp (p, "teams") == 0)
30843 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30844 char p_name[sizeof ("#pragma omp target teams distribute "
30845 "parallel for simd")];
30847 cp_lexer_consume_token (parser->lexer);
30848 strcpy (p_name, "#pragma omp target");
30849 if (!flag_openmp) /* flag_openmp_simd */
30851 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
30852 OMP_TARGET_CLAUSE_MASK,
30853 cclauses);
30854 return stmt != NULL_TREE;
30856 keep_next_level (true);
30857 tree sb = begin_omp_structured_block ();
30858 unsigned save = cp_parser_begin_omp_structured_block (parser);
30859 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30860 OMP_TARGET_CLAUSE_MASK, cclauses);
30861 cp_parser_end_omp_structured_block (parser, save);
30862 tree body = finish_omp_structured_block (sb);
30863 if (ret == NULL_TREE)
30864 return false;
30865 tree stmt = make_node (OMP_TARGET);
30866 TREE_TYPE (stmt) = void_type_node;
30867 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30868 OMP_TARGET_BODY (stmt) = body;
30869 add_stmt (stmt);
30870 return true;
30872 else if (!flag_openmp) /* flag_openmp_simd */
30874 cp_parser_require_pragma_eol (parser, pragma_tok);
30875 return false;
30877 else if (strcmp (p, "data") == 0)
30879 cp_lexer_consume_token (parser->lexer);
30880 cp_parser_omp_target_data (parser, pragma_tok);
30881 return true;
30883 else if (strcmp (p, "update") == 0)
30885 cp_lexer_consume_token (parser->lexer);
30886 return cp_parser_omp_target_update (parser, pragma_tok, context);
30890 tree stmt = make_node (OMP_TARGET);
30891 TREE_TYPE (stmt) = void_type_node;
30893 OMP_TARGET_CLAUSES (stmt)
30894 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30895 "#pragma omp target", pragma_tok);
30896 keep_next_level (true);
30897 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30899 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30900 add_stmt (stmt);
30901 return true;
30904 /* OpenMP 4.0:
30905 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30907 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30908 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30915 static void
30916 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30917 enum pragma_context context)
30919 bool first_p = parser->omp_declare_simd == NULL;
30920 cp_omp_declare_simd_data data;
30921 if (first_p)
30923 data.error_seen = false;
30924 data.fndecl_seen = false;
30925 data.tokens = vNULL;
30926 parser->omp_declare_simd = &data;
30928 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30929 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30930 cp_lexer_consume_token (parser->lexer);
30931 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30932 parser->omp_declare_simd->error_seen = true;
30933 cp_parser_require_pragma_eol (parser, pragma_tok);
30934 struct cp_token_cache *cp
30935 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30936 parser->omp_declare_simd->tokens.safe_push (cp);
30937 if (first_p)
30939 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30940 cp_parser_pragma (parser, context);
30941 switch (context)
30943 case pragma_external:
30944 cp_parser_declaration (parser);
30945 break;
30946 case pragma_member:
30947 cp_parser_member_declaration (parser);
30948 break;
30949 case pragma_objc_icode:
30950 cp_parser_block_declaration (parser, /*statement_p=*/false);
30951 break;
30952 default:
30953 cp_parser_declaration_statement (parser);
30954 break;
30956 if (parser->omp_declare_simd
30957 && !parser->omp_declare_simd->error_seen
30958 && !parser->omp_declare_simd->fndecl_seen)
30959 error_at (pragma_tok->location,
30960 "%<#pragma omp declare simd%> not immediately followed by "
30961 "function declaration or definition");
30962 data.tokens.release ();
30963 parser->omp_declare_simd = NULL;
30967 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30968 This function is modelled similar to the late parsing of omp declare
30969 simd. */
30971 static tree
30972 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30974 struct cp_token_cache *ce;
30975 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30976 int ii = 0;
30978 if (parser->omp_declare_simd != NULL)
30980 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30981 " marked as a Cilk Plus SIMD-enabled function");
30982 XDELETE (parser->cilk_simd_fn_info);
30983 parser->cilk_simd_fn_info = NULL;
30984 return attrs;
30986 if (!info->error_seen && info->fndecl_seen)
30988 error ("vector attribute not immediately followed by a single function"
30989 " declaration or definition");
30990 info->error_seen = true;
30992 if (info->error_seen)
30993 return attrs;
30995 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30997 tree c, cl;
30999 cp_parser_push_lexer_for_tokens (parser, ce);
31000 parser->lexer->in_pragma = true;
31001 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31002 "SIMD-enabled functions attribute",
31003 NULL);
31004 cp_parser_pop_lexer (parser);
31005 if (cl)
31006 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31008 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31009 TREE_CHAIN (c) = attrs;
31010 attrs = c;
31012 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31013 TREE_CHAIN (c) = attrs;
31014 if (processing_template_decl)
31015 ATTR_IS_DEPENDENT (c) = 1;
31016 attrs = c;
31018 info->fndecl_seen = true;
31019 XDELETE (parser->cilk_simd_fn_info);
31020 parser->cilk_simd_fn_info = NULL;
31021 return attrs;
31024 /* Finalize #pragma omp declare simd clauses after direct declarator has
31025 been parsed, and put that into "omp declare simd" attribute. */
31027 static tree
31028 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31030 struct cp_token_cache *ce;
31031 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31032 int i;
31034 if (!data->error_seen && data->fndecl_seen)
31036 error ("%<#pragma omp declare simd%> not immediately followed by "
31037 "a single function declaration or definition");
31038 data->error_seen = true;
31039 return attrs;
31041 if (data->error_seen)
31042 return attrs;
31044 FOR_EACH_VEC_ELT (data->tokens, i, ce)
31046 tree c, cl;
31048 cp_parser_push_lexer_for_tokens (parser, ce);
31049 parser->lexer->in_pragma = true;
31050 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31051 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31052 cp_lexer_consume_token (parser->lexer);
31053 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31054 "#pragma omp declare simd", pragma_tok);
31055 cp_parser_pop_lexer (parser);
31056 if (cl)
31057 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31058 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31059 TREE_CHAIN (c) = attrs;
31060 if (processing_template_decl)
31061 ATTR_IS_DEPENDENT (c) = 1;
31062 attrs = c;
31065 data->fndecl_seen = true;
31066 return attrs;
31070 /* OpenMP 4.0:
31071 # pragma omp declare target new-line
31072 declarations and definitions
31073 # pragma omp end declare target new-line */
31075 static void
31076 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31078 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31079 scope_chain->omp_declare_target_attribute++;
31082 static void
31083 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
31085 const char *p = "";
31086 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31088 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31089 p = IDENTIFIER_POINTER (id);
31091 if (strcmp (p, "declare") == 0)
31093 cp_lexer_consume_token (parser->lexer);
31094 p = "";
31095 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31097 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31098 p = IDENTIFIER_POINTER (id);
31100 if (strcmp (p, "target") == 0)
31101 cp_lexer_consume_token (parser->lexer);
31102 else
31104 cp_parser_error (parser, "expected %<target%>");
31105 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31106 return;
31109 else
31111 cp_parser_error (parser, "expected %<declare%>");
31112 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31113 return;
31115 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31116 if (!scope_chain->omp_declare_target_attribute)
31117 error_at (pragma_tok->location,
31118 "%<#pragma omp end declare target%> without corresponding "
31119 "%<#pragma omp declare target%>");
31120 else
31121 scope_chain->omp_declare_target_attribute--;
31124 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31125 expression and optional initializer clause of
31126 #pragma omp declare reduction. We store the expression(s) as
31127 either 3, 6 or 7 special statements inside of the artificial function's
31128 body. The first two statements are DECL_EXPRs for the artificial
31129 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
31130 expression that uses those variables.
31131 If there was any INITIALIZER clause, this is followed by further statements,
31132 the fourth and fifth statements are DECL_EXPRs for the artificial
31133 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
31134 constructor variant (first token after open paren is not omp_priv),
31135 then the sixth statement is a statement with the function call expression
31136 that uses the OMP_PRIV and optionally OMP_ORIG variable.
31137 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
31138 to initialize the OMP_PRIV artificial variable and there is seventh
31139 statement, a DECL_EXPR of the OMP_PRIV statement again. */
31141 static bool
31142 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
31144 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
31145 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
31146 type = TREE_TYPE (type);
31147 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
31148 DECL_ARTIFICIAL (omp_out) = 1;
31149 pushdecl (omp_out);
31150 add_decl_expr (omp_out);
31151 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
31152 DECL_ARTIFICIAL (omp_in) = 1;
31153 pushdecl (omp_in);
31154 add_decl_expr (omp_in);
31155 tree combiner;
31156 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
31158 keep_next_level (true);
31159 tree block = begin_omp_structured_block ();
31160 combiner = cp_parser_expression (parser);
31161 finish_expr_stmt (combiner);
31162 block = finish_omp_structured_block (block);
31163 add_stmt (block);
31165 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31166 return false;
31168 const char *p = "";
31169 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31171 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31172 p = IDENTIFIER_POINTER (id);
31175 if (strcmp (p, "initializer") == 0)
31177 cp_lexer_consume_token (parser->lexer);
31178 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31179 return false;
31181 p = "";
31182 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31184 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31185 p = IDENTIFIER_POINTER (id);
31188 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
31189 DECL_ARTIFICIAL (omp_priv) = 1;
31190 pushdecl (omp_priv);
31191 add_decl_expr (omp_priv);
31192 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
31193 DECL_ARTIFICIAL (omp_orig) = 1;
31194 pushdecl (omp_orig);
31195 add_decl_expr (omp_orig);
31197 keep_next_level (true);
31198 block = begin_omp_structured_block ();
31200 bool ctor = false;
31201 if (strcmp (p, "omp_priv") == 0)
31203 bool is_direct_init, is_non_constant_init;
31204 ctor = true;
31205 cp_lexer_consume_token (parser->lexer);
31206 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
31207 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
31208 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31209 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
31210 == CPP_CLOSE_PAREN
31211 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
31212 == CPP_CLOSE_PAREN))
31214 finish_omp_structured_block (block);
31215 error ("invalid initializer clause");
31216 return false;
31218 initializer = cp_parser_initializer (parser, &is_direct_init,
31219 &is_non_constant_init);
31220 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
31221 NULL_TREE, LOOKUP_ONLYCONVERTING);
31223 else
31225 cp_parser_parse_tentatively (parser);
31226 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
31227 /*check_dependency_p=*/true,
31228 /*template_p=*/NULL,
31229 /*declarator_p=*/false,
31230 /*optional_p=*/false);
31231 vec<tree, va_gc> *args;
31232 if (fn_name == error_mark_node
31233 || cp_parser_error_occurred (parser)
31234 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31235 || ((args = cp_parser_parenthesized_expression_list
31236 (parser, non_attr, /*cast_p=*/false,
31237 /*allow_expansion_p=*/true,
31238 /*non_constant_p=*/NULL)),
31239 cp_parser_error_occurred (parser)))
31241 finish_omp_structured_block (block);
31242 cp_parser_abort_tentative_parse (parser);
31243 cp_parser_error (parser, "expected id-expression (arguments)");
31244 return false;
31246 unsigned int i;
31247 tree arg;
31248 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
31249 if (arg == omp_priv
31250 || (TREE_CODE (arg) == ADDR_EXPR
31251 && TREE_OPERAND (arg, 0) == omp_priv))
31252 break;
31253 cp_parser_abort_tentative_parse (parser);
31254 if (arg == NULL_TREE)
31255 error ("one of the initializer call arguments should be %<omp_priv%>"
31256 " or %<&omp_priv%>");
31257 initializer = cp_parser_postfix_expression (parser, false, false, false,
31258 false, NULL);
31259 finish_expr_stmt (initializer);
31262 block = finish_omp_structured_block (block);
31263 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
31264 add_stmt (block);
31266 if (ctor)
31267 add_decl_expr (omp_orig);
31269 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31270 return false;
31273 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31274 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31276 return true;
31279 /* OpenMP 4.0
31280 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31281 initializer-clause[opt] new-line
31283 initializer-clause:
31284 initializer (omp_priv initializer)
31285 initializer (function-name (argument-list)) */
31287 static void
31288 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31289 enum pragma_context)
31291 auto_vec<tree> types;
31292 enum tree_code reduc_code = ERROR_MARK;
31293 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31294 unsigned int i;
31295 cp_token *first_token;
31296 cp_token_cache *cp;
31297 int errs;
31298 void *p;
31300 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31301 p = obstack_alloc (&declarator_obstack, 0);
31303 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31304 goto fail;
31306 switch (cp_lexer_peek_token (parser->lexer)->type)
31308 case CPP_PLUS:
31309 reduc_code = PLUS_EXPR;
31310 break;
31311 case CPP_MULT:
31312 reduc_code = MULT_EXPR;
31313 break;
31314 case CPP_MINUS:
31315 reduc_code = MINUS_EXPR;
31316 break;
31317 case CPP_AND:
31318 reduc_code = BIT_AND_EXPR;
31319 break;
31320 case CPP_XOR:
31321 reduc_code = BIT_XOR_EXPR;
31322 break;
31323 case CPP_OR:
31324 reduc_code = BIT_IOR_EXPR;
31325 break;
31326 case CPP_AND_AND:
31327 reduc_code = TRUTH_ANDIF_EXPR;
31328 break;
31329 case CPP_OR_OR:
31330 reduc_code = TRUTH_ORIF_EXPR;
31331 break;
31332 case CPP_NAME:
31333 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31334 break;
31335 default:
31336 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31337 "%<|%>, %<&&%>, %<||%> or identifier");
31338 goto fail;
31341 if (reduc_code != ERROR_MARK)
31342 cp_lexer_consume_token (parser->lexer);
31344 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31345 if (reduc_id == error_mark_node)
31346 goto fail;
31348 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31349 goto fail;
31351 /* Types may not be defined in declare reduction type list. */
31352 const char *saved_message;
31353 saved_message = parser->type_definition_forbidden_message;
31354 parser->type_definition_forbidden_message
31355 = G_("types may not be defined in declare reduction type list");
31356 bool saved_colon_corrects_to_scope_p;
31357 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31358 parser->colon_corrects_to_scope_p = false;
31359 bool saved_colon_doesnt_start_class_def_p;
31360 saved_colon_doesnt_start_class_def_p
31361 = parser->colon_doesnt_start_class_def_p;
31362 parser->colon_doesnt_start_class_def_p = true;
31364 while (true)
31366 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31367 type = cp_parser_type_id (parser);
31368 if (type == error_mark_node)
31370 else if (ARITHMETIC_TYPE_P (type)
31371 && (orig_reduc_id == NULL_TREE
31372 || (TREE_CODE (type) != COMPLEX_TYPE
31373 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31374 "min") == 0
31375 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31376 "max") == 0))))
31377 error_at (loc, "predeclared arithmetic type %qT in "
31378 "%<#pragma omp declare reduction%>", type);
31379 else if (TREE_CODE (type) == FUNCTION_TYPE
31380 || TREE_CODE (type) == METHOD_TYPE
31381 || TREE_CODE (type) == ARRAY_TYPE)
31382 error_at (loc, "function or array type %qT in "
31383 "%<#pragma omp declare reduction%>", type);
31384 else if (TREE_CODE (type) == REFERENCE_TYPE)
31385 error_at (loc, "reference type %qT in "
31386 "%<#pragma omp declare reduction%>", type);
31387 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31388 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31389 "%<#pragma omp declare reduction%>", type);
31390 else
31391 types.safe_push (type);
31393 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31394 cp_lexer_consume_token (parser->lexer);
31395 else
31396 break;
31399 /* Restore the saved message. */
31400 parser->type_definition_forbidden_message = saved_message;
31401 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31402 parser->colon_doesnt_start_class_def_p
31403 = saved_colon_doesnt_start_class_def_p;
31405 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31406 || types.is_empty ())
31408 fail:
31409 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31410 goto done;
31413 first_token = cp_lexer_peek_token (parser->lexer);
31414 cp = NULL;
31415 errs = errorcount;
31416 FOR_EACH_VEC_ELT (types, i, type)
31418 tree fntype
31419 = build_function_type_list (void_type_node,
31420 cp_build_reference_type (type, false),
31421 NULL_TREE);
31422 tree this_reduc_id = reduc_id;
31423 if (!dependent_type_p (type))
31424 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31425 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31426 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31427 DECL_ARTIFICIAL (fndecl) = 1;
31428 DECL_EXTERNAL (fndecl) = 1;
31429 DECL_DECLARED_INLINE_P (fndecl) = 1;
31430 DECL_IGNORED_P (fndecl) = 1;
31431 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31432 DECL_ATTRIBUTES (fndecl)
31433 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31434 DECL_ATTRIBUTES (fndecl));
31435 if (processing_template_decl)
31436 fndecl = push_template_decl (fndecl);
31437 bool block_scope = false;
31438 tree block = NULL_TREE;
31439 if (current_function_decl)
31441 block_scope = true;
31442 DECL_CONTEXT (fndecl) = global_namespace;
31443 if (!processing_template_decl)
31444 pushdecl (fndecl);
31446 else if (current_class_type)
31448 if (cp == NULL)
31450 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31451 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31452 cp_lexer_consume_token (parser->lexer);
31453 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31454 goto fail;
31455 cp = cp_token_cache_new (first_token,
31456 cp_lexer_peek_nth_token (parser->lexer,
31457 2));
31459 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31460 finish_member_declaration (fndecl);
31461 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31462 DECL_PENDING_INLINE_P (fndecl) = 1;
31463 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31464 continue;
31466 else
31468 DECL_CONTEXT (fndecl) = current_namespace;
31469 pushdecl (fndecl);
31471 if (!block_scope)
31472 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31473 else
31474 block = begin_omp_structured_block ();
31475 if (cp)
31477 cp_parser_push_lexer_for_tokens (parser, cp);
31478 parser->lexer->in_pragma = true;
31480 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31482 if (!block_scope)
31483 finish_function (0);
31484 else
31485 DECL_CONTEXT (fndecl) = current_function_decl;
31486 if (cp)
31487 cp_parser_pop_lexer (parser);
31488 goto fail;
31490 if (cp)
31491 cp_parser_pop_lexer (parser);
31492 if (!block_scope)
31493 finish_function (0);
31494 else
31496 DECL_CONTEXT (fndecl) = current_function_decl;
31497 block = finish_omp_structured_block (block);
31498 if (TREE_CODE (block) == BIND_EXPR)
31499 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31500 else if (TREE_CODE (block) == STATEMENT_LIST)
31501 DECL_SAVED_TREE (fndecl) = block;
31502 if (processing_template_decl)
31503 add_decl_expr (fndecl);
31505 cp_check_omp_declare_reduction (fndecl);
31506 if (cp == NULL && types.length () > 1)
31507 cp = cp_token_cache_new (first_token,
31508 cp_lexer_peek_nth_token (parser->lexer, 2));
31509 if (errs != errorcount)
31510 break;
31513 cp_parser_require_pragma_eol (parser, pragma_tok);
31515 done:
31516 /* Free any declarators allocated. */
31517 obstack_free (&declarator_obstack, p);
31520 /* OpenMP 4.0
31521 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31522 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31523 initializer-clause[opt] new-line
31524 #pragma omp declare target new-line */
31526 static void
31527 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31528 enum pragma_context context)
31530 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31532 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31533 const char *p = IDENTIFIER_POINTER (id);
31535 if (strcmp (p, "simd") == 0)
31537 cp_lexer_consume_token (parser->lexer);
31538 cp_parser_omp_declare_simd (parser, pragma_tok,
31539 context);
31540 return;
31542 cp_ensure_no_omp_declare_simd (parser);
31543 if (strcmp (p, "reduction") == 0)
31545 cp_lexer_consume_token (parser->lexer);
31546 cp_parser_omp_declare_reduction (parser, pragma_tok,
31547 context);
31548 return;
31550 if (!flag_openmp) /* flag_openmp_simd */
31552 cp_parser_require_pragma_eol (parser, pragma_tok);
31553 return;
31555 if (strcmp (p, "target") == 0)
31557 cp_lexer_consume_token (parser->lexer);
31558 cp_parser_omp_declare_target (parser, pragma_tok);
31559 return;
31562 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31563 "or %<target%>");
31564 cp_parser_require_pragma_eol (parser, pragma_tok);
31567 /* Main entry point to OpenMP statement pragmas. */
31569 static void
31570 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31572 tree stmt;
31573 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31574 omp_clause_mask mask (0);
31576 switch (pragma_tok->pragma_kind)
31578 case PRAGMA_OMP_ATOMIC:
31579 cp_parser_omp_atomic (parser, pragma_tok);
31580 return;
31581 case PRAGMA_OMP_CRITICAL:
31582 stmt = cp_parser_omp_critical (parser, pragma_tok);
31583 break;
31584 case PRAGMA_OMP_DISTRIBUTE:
31585 strcpy (p_name, "#pragma omp");
31586 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31587 break;
31588 case PRAGMA_OMP_FOR:
31589 strcpy (p_name, "#pragma omp");
31590 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31591 break;
31592 case PRAGMA_OMP_MASTER:
31593 stmt = cp_parser_omp_master (parser, pragma_tok);
31594 break;
31595 case PRAGMA_OMP_ORDERED:
31596 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31597 break;
31598 case PRAGMA_OMP_PARALLEL:
31599 strcpy (p_name, "#pragma omp");
31600 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31601 break;
31602 case PRAGMA_OMP_SECTIONS:
31603 strcpy (p_name, "#pragma omp");
31604 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31605 break;
31606 case PRAGMA_OMP_SIMD:
31607 strcpy (p_name, "#pragma omp");
31608 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31609 break;
31610 case PRAGMA_OMP_SINGLE:
31611 stmt = cp_parser_omp_single (parser, pragma_tok);
31612 break;
31613 case PRAGMA_OMP_TASK:
31614 stmt = cp_parser_omp_task (parser, pragma_tok);
31615 break;
31616 case PRAGMA_OMP_TASKGROUP:
31617 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31618 break;
31619 case PRAGMA_OMP_TEAMS:
31620 strcpy (p_name, "#pragma omp");
31621 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31622 break;
31623 default:
31624 gcc_unreachable ();
31627 if (stmt)
31628 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31631 /* Transactional Memory parsing routines. */
31633 /* Parse a transaction attribute.
31635 txn-attribute:
31636 attribute
31637 [ [ identifier ] ]
31639 ??? Simplify this when C++0x bracket attributes are
31640 implemented properly. */
31642 static tree
31643 cp_parser_txn_attribute_opt (cp_parser *parser)
31645 cp_token *token;
31646 tree attr_name, attr = NULL;
31648 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31649 return cp_parser_attributes_opt (parser);
31651 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31652 return NULL_TREE;
31653 cp_lexer_consume_token (parser->lexer);
31654 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31655 goto error1;
31657 token = cp_lexer_peek_token (parser->lexer);
31658 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31660 token = cp_lexer_consume_token (parser->lexer);
31662 attr_name = (token->type == CPP_KEYWORD
31663 /* For keywords, use the canonical spelling,
31664 not the parsed identifier. */
31665 ? ridpointers[(int) token->keyword]
31666 : token->u.value);
31667 attr = build_tree_list (attr_name, NULL_TREE);
31669 else
31670 cp_parser_error (parser, "expected identifier");
31672 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31673 error1:
31674 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31675 return attr;
31678 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31680 transaction-statement:
31681 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31682 compound-statement
31683 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31686 static tree
31687 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31689 unsigned char old_in = parser->in_transaction;
31690 unsigned char this_in = 1, new_in;
31691 cp_token *token;
31692 tree stmt, attrs, noex;
31694 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31695 || keyword == RID_TRANSACTION_RELAXED);
31696 token = cp_parser_require_keyword (parser, keyword,
31697 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31698 : RT_TRANSACTION_RELAXED));
31699 gcc_assert (token != NULL);
31701 if (keyword == RID_TRANSACTION_RELAXED)
31702 this_in |= TM_STMT_ATTR_RELAXED;
31703 else
31705 attrs = cp_parser_txn_attribute_opt (parser);
31706 if (attrs)
31707 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31710 /* Parse a noexcept specification. */
31711 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31713 /* Keep track if we're in the lexical scope of an outer transaction. */
31714 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31716 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31718 parser->in_transaction = new_in;
31719 cp_parser_compound_statement (parser, NULL, false, false);
31720 parser->in_transaction = old_in;
31722 finish_transaction_stmt (stmt, NULL, this_in, noex);
31724 return stmt;
31727 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31729 transaction-expression:
31730 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31731 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31734 static tree
31735 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31737 unsigned char old_in = parser->in_transaction;
31738 unsigned char this_in = 1;
31739 cp_token *token;
31740 tree expr, noex;
31741 bool noex_expr;
31743 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31744 || keyword == RID_TRANSACTION_RELAXED);
31746 if (!flag_tm)
31747 error (keyword == RID_TRANSACTION_RELAXED
31748 ? G_("%<__transaction_relaxed%> without transactional memory "
31749 "support enabled")
31750 : G_("%<__transaction_atomic%> without transactional memory "
31751 "support enabled"));
31753 token = cp_parser_require_keyword (parser, keyword,
31754 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31755 : RT_TRANSACTION_RELAXED));
31756 gcc_assert (token != NULL);
31758 if (keyword == RID_TRANSACTION_RELAXED)
31759 this_in |= TM_STMT_ATTR_RELAXED;
31761 /* Set this early. This might mean that we allow transaction_cancel in
31762 an expression that we find out later actually has to be a constexpr.
31763 However, we expect that cxx_constant_value will be able to deal with
31764 this; also, if the noexcept has no constexpr, then what we parse next
31765 really is a transaction's body. */
31766 parser->in_transaction = this_in;
31768 /* Parse a noexcept specification. */
31769 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31770 true);
31772 if (!noex || !noex_expr
31773 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31775 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31777 expr = cp_parser_expression (parser);
31778 expr = finish_parenthesized_expr (expr);
31780 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31782 else
31784 /* The only expression that is available got parsed for the noexcept
31785 already. noexcept is true then. */
31786 expr = noex;
31787 noex = boolean_true_node;
31790 expr = build_transaction_expr (token->location, expr, this_in, noex);
31791 parser->in_transaction = old_in;
31793 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31794 return error_mark_node;
31796 return (flag_tm ? expr : error_mark_node);
31799 /* Parse a function-transaction-block.
31801 function-transaction-block:
31802 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31803 function-body
31804 __transaction_atomic txn-attribute[opt] function-try-block
31805 __transaction_relaxed ctor-initializer[opt] function-body
31806 __transaction_relaxed function-try-block
31809 static bool
31810 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31812 unsigned char old_in = parser->in_transaction;
31813 unsigned char new_in = 1;
31814 tree compound_stmt, stmt, attrs;
31815 bool ctor_initializer_p;
31816 cp_token *token;
31818 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31819 || keyword == RID_TRANSACTION_RELAXED);
31820 token = cp_parser_require_keyword (parser, keyword,
31821 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31822 : RT_TRANSACTION_RELAXED));
31823 gcc_assert (token != NULL);
31825 if (keyword == RID_TRANSACTION_RELAXED)
31826 new_in |= TM_STMT_ATTR_RELAXED;
31827 else
31829 attrs = cp_parser_txn_attribute_opt (parser);
31830 if (attrs)
31831 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31834 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31836 parser->in_transaction = new_in;
31838 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31839 ctor_initializer_p = cp_parser_function_try_block (parser);
31840 else
31841 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31842 (parser, /*in_function_try_block=*/false);
31844 parser->in_transaction = old_in;
31846 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31848 return ctor_initializer_p;
31851 /* Parse a __transaction_cancel statement.
31853 cancel-statement:
31854 __transaction_cancel txn-attribute[opt] ;
31855 __transaction_cancel txn-attribute[opt] throw-expression ;
31857 ??? Cancel and throw is not yet implemented. */
31859 static tree
31860 cp_parser_transaction_cancel (cp_parser *parser)
31862 cp_token *token;
31863 bool is_outer = false;
31864 tree stmt, attrs;
31866 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31867 RT_TRANSACTION_CANCEL);
31868 gcc_assert (token != NULL);
31870 attrs = cp_parser_txn_attribute_opt (parser);
31871 if (attrs)
31872 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31874 /* ??? Parse cancel-and-throw here. */
31876 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31878 if (!flag_tm)
31880 error_at (token->location, "%<__transaction_cancel%> without "
31881 "transactional memory support enabled");
31882 return error_mark_node;
31884 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31886 error_at (token->location, "%<__transaction_cancel%> within a "
31887 "%<__transaction_relaxed%>");
31888 return error_mark_node;
31890 else if (is_outer)
31892 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31893 && !is_tm_may_cancel_outer (current_function_decl))
31895 error_at (token->location, "outer %<__transaction_cancel%> not "
31896 "within outer %<__transaction_atomic%>");
31897 error_at (token->location,
31898 " or a %<transaction_may_cancel_outer%> function");
31899 return error_mark_node;
31902 else if (parser->in_transaction == 0)
31904 error_at (token->location, "%<__transaction_cancel%> not within "
31905 "%<__transaction_atomic%>");
31906 return error_mark_node;
31909 stmt = build_tm_abort_call (token->location, is_outer);
31910 add_stmt (stmt);
31912 return stmt;
31915 /* The parser. */
31917 static GTY (()) cp_parser *the_parser;
31920 /* Special handling for the first token or line in the file. The first
31921 thing in the file might be #pragma GCC pch_preprocess, which loads a
31922 PCH file, which is a GC collection point. So we need to handle this
31923 first pragma without benefit of an existing lexer structure.
31925 Always returns one token to the caller in *FIRST_TOKEN. This is
31926 either the true first token of the file, or the first token after
31927 the initial pragma. */
31929 static void
31930 cp_parser_initial_pragma (cp_token *first_token)
31932 tree name = NULL;
31934 cp_lexer_get_preprocessor_token (NULL, first_token);
31935 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31936 return;
31938 cp_lexer_get_preprocessor_token (NULL, first_token);
31939 if (first_token->type == CPP_STRING)
31941 name = first_token->u.value;
31943 cp_lexer_get_preprocessor_token (NULL, first_token);
31944 if (first_token->type != CPP_PRAGMA_EOL)
31945 error_at (first_token->location,
31946 "junk at end of %<#pragma GCC pch_preprocess%>");
31948 else
31949 error_at (first_token->location, "expected string literal");
31951 /* Skip to the end of the pragma. */
31952 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31953 cp_lexer_get_preprocessor_token (NULL, first_token);
31955 /* Now actually load the PCH file. */
31956 if (name)
31957 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31959 /* Read one more token to return to our caller. We have to do this
31960 after reading the PCH file in, since its pointers have to be
31961 live. */
31962 cp_lexer_get_preprocessor_token (NULL, first_token);
31965 /* Parses the grainsize pragma for the _Cilk_for statement.
31966 Syntax:
31967 #pragma cilk grainsize = <VALUE>. */
31969 static void
31970 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
31972 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
31974 tree exp = cp_parser_binary_expression (parser, false, false,
31975 PREC_NOT_OPERATOR, NULL);
31976 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31977 if (!exp || exp == error_mark_node)
31979 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
31980 return;
31983 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
31984 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
31985 cp_parser_cilk_for (parser, exp);
31986 else
31987 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
31988 "%<#pragma cilk grainsize%> is not followed by "
31989 "%<_Cilk_for%>");
31990 return;
31992 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31995 /* Normal parsing of a pragma token. Here we can (and must) use the
31996 regular lexer. */
31998 static bool
31999 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32001 cp_token *pragma_tok;
32002 unsigned int id;
32004 pragma_tok = cp_lexer_consume_token (parser->lexer);
32005 gcc_assert (pragma_tok->type == CPP_PRAGMA);
32006 parser->lexer->in_pragma = true;
32008 id = pragma_tok->pragma_kind;
32009 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
32010 cp_ensure_no_omp_declare_simd (parser);
32011 switch (id)
32013 case PRAGMA_GCC_PCH_PREPROCESS:
32014 error_at (pragma_tok->location,
32015 "%<#pragma GCC pch_preprocess%> must be first");
32016 break;
32018 case PRAGMA_OMP_BARRIER:
32019 switch (context)
32021 case pragma_compound:
32022 cp_parser_omp_barrier (parser, pragma_tok);
32023 return false;
32024 case pragma_stmt:
32025 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32026 "used in compound statements");
32027 break;
32028 default:
32029 goto bad_stmt;
32031 break;
32033 case PRAGMA_OMP_FLUSH:
32034 switch (context)
32036 case pragma_compound:
32037 cp_parser_omp_flush (parser, pragma_tok);
32038 return false;
32039 case pragma_stmt:
32040 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32041 "used in compound statements");
32042 break;
32043 default:
32044 goto bad_stmt;
32046 break;
32048 case PRAGMA_OMP_TASKWAIT:
32049 switch (context)
32051 case pragma_compound:
32052 cp_parser_omp_taskwait (parser, pragma_tok);
32053 return false;
32054 case pragma_stmt:
32055 error_at (pragma_tok->location,
32056 "%<#pragma omp taskwait%> may only be "
32057 "used in compound statements");
32058 break;
32059 default:
32060 goto bad_stmt;
32062 break;
32064 case PRAGMA_OMP_TASKYIELD:
32065 switch (context)
32067 case pragma_compound:
32068 cp_parser_omp_taskyield (parser, pragma_tok);
32069 return false;
32070 case pragma_stmt:
32071 error_at (pragma_tok->location,
32072 "%<#pragma omp taskyield%> may only be "
32073 "used in compound statements");
32074 break;
32075 default:
32076 goto bad_stmt;
32078 break;
32080 case PRAGMA_OMP_CANCEL:
32081 switch (context)
32083 case pragma_compound:
32084 cp_parser_omp_cancel (parser, pragma_tok);
32085 return false;
32086 case pragma_stmt:
32087 error_at (pragma_tok->location,
32088 "%<#pragma omp cancel%> may only be "
32089 "used in compound statements");
32090 break;
32091 default:
32092 goto bad_stmt;
32094 break;
32096 case PRAGMA_OMP_CANCELLATION_POINT:
32097 switch (context)
32099 case pragma_compound:
32100 cp_parser_omp_cancellation_point (parser, pragma_tok);
32101 return false;
32102 case pragma_stmt:
32103 error_at (pragma_tok->location,
32104 "%<#pragma omp cancellation point%> may only be "
32105 "used in compound statements");
32106 break;
32107 default:
32108 goto bad_stmt;
32110 break;
32112 case PRAGMA_OMP_THREADPRIVATE:
32113 cp_parser_omp_threadprivate (parser, pragma_tok);
32114 return false;
32116 case PRAGMA_OMP_DECLARE_REDUCTION:
32117 cp_parser_omp_declare (parser, pragma_tok, context);
32118 return false;
32120 case PRAGMA_OMP_ATOMIC:
32121 case PRAGMA_OMP_CRITICAL:
32122 case PRAGMA_OMP_DISTRIBUTE:
32123 case PRAGMA_OMP_FOR:
32124 case PRAGMA_OMP_MASTER:
32125 case PRAGMA_OMP_ORDERED:
32126 case PRAGMA_OMP_PARALLEL:
32127 case PRAGMA_OMP_SECTIONS:
32128 case PRAGMA_OMP_SIMD:
32129 case PRAGMA_OMP_SINGLE:
32130 case PRAGMA_OMP_TASK:
32131 case PRAGMA_OMP_TASKGROUP:
32132 case PRAGMA_OMP_TEAMS:
32133 if (context != pragma_stmt && context != pragma_compound)
32134 goto bad_stmt;
32135 cp_parser_omp_construct (parser, pragma_tok);
32136 return true;
32138 case PRAGMA_OMP_TARGET:
32139 return cp_parser_omp_target (parser, pragma_tok, context);
32141 case PRAGMA_OMP_END_DECLARE_TARGET:
32142 cp_parser_omp_end_declare_target (parser, pragma_tok);
32143 return false;
32145 case PRAGMA_OMP_SECTION:
32146 error_at (pragma_tok->location,
32147 "%<#pragma omp section%> may only be used in "
32148 "%<#pragma omp sections%> construct");
32149 break;
32151 case PRAGMA_IVDEP:
32153 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32154 cp_token *tok;
32155 tok = cp_lexer_peek_token (the_parser->lexer);
32156 if (tok->type != CPP_KEYWORD
32157 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
32158 && tok->keyword != RID_DO))
32160 cp_parser_error (parser, "for, while or do statement expected");
32161 return false;
32163 cp_parser_iteration_statement (parser, true);
32164 return true;
32167 case PRAGMA_CILK_SIMD:
32168 if (context == pragma_external)
32170 error_at (pragma_tok->location,
32171 "%<#pragma simd%> must be inside a function");
32172 break;
32174 cp_parser_cilk_simd (parser, pragma_tok);
32175 return true;
32177 case PRAGMA_CILK_GRAINSIZE:
32178 if (context == pragma_external)
32180 error_at (pragma_tok->location,
32181 "%<#pragma cilk grainsize%> must be inside a function");
32182 break;
32185 /* Ignore the pragma if Cilk Plus is not enabled. */
32186 if (flag_cilkplus)
32188 cp_parser_cilk_grainsize (parser, pragma_tok);
32189 return true;
32191 else
32193 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
32194 "%<#pragma cilk grainsize%>");
32195 break;
32198 default:
32199 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
32200 c_invoke_pragma_handler (id);
32201 break;
32203 bad_stmt:
32204 cp_parser_error (parser, "expected declaration specifiers");
32205 break;
32208 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32209 return false;
32212 /* The interface the pragma parsers have to the lexer. */
32214 enum cpp_ttype
32215 pragma_lex (tree *value)
32217 cp_token *tok;
32218 enum cpp_ttype ret;
32220 tok = cp_lexer_peek_token (the_parser->lexer);
32222 ret = tok->type;
32223 *value = tok->u.value;
32225 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
32226 ret = CPP_EOF;
32227 else if (ret == CPP_STRING)
32228 *value = cp_parser_string_literal (the_parser, false, false);
32229 else
32231 cp_lexer_consume_token (the_parser->lexer);
32232 if (ret == CPP_KEYWORD)
32233 ret = CPP_NAME;
32236 return ret;
32240 /* External interface. */
32242 /* Parse one entire translation unit. */
32244 void
32245 c_parse_file (void)
32247 static bool already_called = false;
32249 if (already_called)
32250 fatal_error ("inter-module optimizations not implemented for C++");
32251 already_called = true;
32253 the_parser = cp_parser_new ();
32254 push_deferring_access_checks (flag_access_control
32255 ? dk_no_deferred : dk_no_check);
32256 cp_parser_translation_unit (the_parser);
32257 the_parser = NULL;
32260 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
32261 vectorlength clause:
32262 Syntax:
32263 vectorlength ( constant-expression ) */
32265 static tree
32266 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
32267 bool is_simd_fn)
32269 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32270 tree expr;
32271 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
32272 safelen clause. Thus, vectorlength is represented as OMP 4.0
32273 safelen. For SIMD-enabled function it is represented by OMP 4.0
32274 simdlen. */
32275 if (!is_simd_fn)
32276 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
32277 loc);
32278 else
32279 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
32280 loc);
32282 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32283 return error_mark_node;
32285 expr = cp_parser_constant_expression (parser);
32286 expr = maybe_constant_value (expr);
32288 /* If expr == error_mark_node, then don't emit any errors nor
32289 create a clause. if any of the above functions returns
32290 error mark node then they would have emitted an error message. */
32291 if (expr == error_mark_node)
32293 else if (!TREE_TYPE (expr)
32294 || !TREE_CONSTANT (expr)
32295 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
32296 error_at (loc, "vectorlength must be an integer constant");
32297 else if (TREE_CONSTANT (expr)
32298 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
32299 error_at (loc, "vectorlength must be a power of 2");
32300 else
32302 tree c;
32303 if (!is_simd_fn)
32305 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
32306 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
32307 OMP_CLAUSE_CHAIN (c) = clauses;
32308 clauses = c;
32310 else
32312 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
32313 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
32314 OMP_CLAUSE_CHAIN (c) = clauses;
32315 clauses = c;
32319 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32320 return error_mark_node;
32321 return clauses;
32324 /* Handles the Cilk Plus #pragma simd linear clause.
32325 Syntax:
32326 linear ( simd-linear-variable-list )
32328 simd-linear-variable-list:
32329 simd-linear-variable
32330 simd-linear-variable-list , simd-linear-variable
32332 simd-linear-variable:
32333 id-expression
32334 id-expression : simd-linear-step
32336 simd-linear-step:
32337 conditional-expression */
32339 static tree
32340 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
32342 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32344 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32345 return clauses;
32346 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32348 cp_parser_error (parser, "expected identifier");
32349 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32350 return error_mark_node;
32353 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32354 parser->colon_corrects_to_scope_p = false;
32355 while (1)
32357 cp_token *token = cp_lexer_peek_token (parser->lexer);
32358 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32360 cp_parser_error (parser, "expected variable-name");
32361 clauses = error_mark_node;
32362 break;
32365 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32366 false, false);
32367 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32368 token->location);
32369 if (decl == error_mark_node)
32371 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32372 token->location);
32373 clauses = error_mark_node;
32375 else
32377 tree e = NULL_TREE;
32378 tree step_size = integer_one_node;
32380 /* If present, parse the linear step. Otherwise, assume the default
32381 value of 1. */
32382 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32384 cp_lexer_consume_token (parser->lexer);
32386 e = cp_parser_assignment_expression (parser);
32387 e = maybe_constant_value (e);
32389 if (e == error_mark_node)
32391 /* If an error has occurred, then the whole pragma is
32392 considered ill-formed. Thus, no reason to keep
32393 parsing. */
32394 clauses = error_mark_node;
32395 break;
32397 else if (type_dependent_expression_p (e)
32398 || value_dependent_expression_p (e)
32399 || (TREE_TYPE (e)
32400 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32401 && (TREE_CONSTANT (e)
32402 || DECL_P (e))))
32403 step_size = e;
32404 else
32405 cp_parser_error (parser,
32406 "step size must be an integer constant "
32407 "expression or an integer variable");
32410 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32411 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32412 OMP_CLAUSE_DECL (l) = decl;
32413 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32414 OMP_CLAUSE_CHAIN (l) = clauses;
32415 clauses = l;
32417 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32418 cp_lexer_consume_token (parser->lexer);
32419 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32420 break;
32421 else
32423 error_at (cp_lexer_peek_token (parser->lexer)->location,
32424 "expected %<,%> or %<)%> after %qE", decl);
32425 clauses = error_mark_node;
32426 break;
32429 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32430 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32431 return clauses;
32434 /* Returns the name of the next clause. If the clause is not
32435 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32436 token is not consumed. Otherwise, the appropriate enum from the
32437 pragma_simd_clause is returned and the token is consumed. */
32439 static pragma_omp_clause
32440 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32442 pragma_omp_clause clause_type;
32443 cp_token *token = cp_lexer_peek_token (parser->lexer);
32445 if (token->keyword == RID_PRIVATE)
32446 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32447 else if (!token->u.value || token->type != CPP_NAME)
32448 return PRAGMA_CILK_CLAUSE_NONE;
32449 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32450 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32451 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32452 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32453 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32454 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32455 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32456 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32457 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32458 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32459 else
32460 return PRAGMA_CILK_CLAUSE_NONE;
32462 cp_lexer_consume_token (parser->lexer);
32463 return clause_type;
32466 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32468 static tree
32469 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32471 tree clauses = NULL_TREE;
32473 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32474 && clauses != error_mark_node)
32476 pragma_omp_clause c_kind;
32477 c_kind = cp_parser_cilk_simd_clause_name (parser);
32478 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32479 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
32480 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32481 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32482 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32483 /* Use the OpenMP 4.0 equivalent function. */
32484 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32485 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32486 /* Use the OpenMP 4.0 equivalent function. */
32487 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32488 clauses);
32489 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32490 /* Use the OMP 4.0 equivalent function. */
32491 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32492 clauses);
32493 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32494 /* Use the OMP 4.0 equivalent function. */
32495 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32496 else
32498 clauses = error_mark_node;
32499 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32500 break;
32504 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32506 if (clauses == error_mark_node)
32507 return error_mark_node;
32508 else
32509 return c_finish_cilk_clauses (clauses);
32512 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32514 static void
32515 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32517 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32519 if (clauses == error_mark_node)
32520 return;
32522 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32524 error_at (cp_lexer_peek_token (parser->lexer)->location,
32525 "for statement expected");
32526 return;
32529 tree sb = begin_omp_structured_block ();
32530 int save = cp_parser_begin_omp_structured_block (parser);
32531 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32532 if (ret)
32533 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32534 cp_parser_end_omp_structured_block (parser, save);
32535 add_stmt (finish_omp_structured_block (sb));
32538 /* Main entry-point for parsing Cilk Plus _Cilk_for
32539 loops. The return value is error_mark_node
32540 when errors happen and CILK_FOR tree on success. */
32542 static tree
32543 cp_parser_cilk_for (cp_parser *parser, tree grain)
32545 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
32546 gcc_unreachable ();
32548 tree sb = begin_omp_structured_block ();
32549 int save = cp_parser_begin_omp_structured_block (parser);
32551 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
32552 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
32553 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
32554 clauses = finish_omp_clauses (clauses);
32556 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
32557 if (ret)
32558 cpp_validate_cilk_plus_loop (ret);
32559 else
32560 ret = error_mark_node;
32562 cp_parser_end_omp_structured_block (parser, save);
32563 add_stmt (finish_omp_structured_block (sb));
32564 return ret;
32567 /* Create an identifier for a generic parameter type (a synthesized
32568 template parameter implied by `auto' or a concept identifier). */
32570 static GTY(()) int generic_parm_count;
32571 static tree
32572 make_generic_type_name ()
32574 char buf[32];
32575 sprintf (buf, "auto:%d", ++generic_parm_count);
32576 return get_identifier (buf);
32579 /* Predicate that behaves as is_auto_or_concept but matches the parent
32580 node of the generic type rather than the generic type itself. This
32581 allows for type transformation in add_implicit_template_parms. */
32583 static inline bool
32584 tree_type_is_auto_or_concept (const_tree t)
32586 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32589 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32590 (creating a new template parameter list if necessary). Returns the newly
32591 created template type parm. */
32593 tree
32594 synthesize_implicit_template_parm (cp_parser *parser)
32596 gcc_assert (current_binding_level->kind == sk_function_parms);
32598 /* We are either continuing a function template that already contains implicit
32599 template parameters, creating a new fully-implicit function template, or
32600 extending an existing explicit function template with implicit template
32601 parameters. */
32603 cp_binding_level *const entry_scope = current_binding_level;
32605 bool become_template = false;
32606 cp_binding_level *parent_scope = 0;
32608 if (parser->implicit_template_scope)
32610 gcc_assert (parser->implicit_template_parms);
32612 current_binding_level = parser->implicit_template_scope;
32614 else
32616 /* Roll back to the existing template parameter scope (in the case of
32617 extending an explicit function template) or introduce a new template
32618 parameter scope ahead of the function parameter scope (or class scope
32619 in the case of out-of-line member definitions). The function scope is
32620 added back after template parameter synthesis below. */
32622 cp_binding_level *scope = entry_scope;
32624 while (scope->kind == sk_function_parms)
32626 parent_scope = scope;
32627 scope = scope->level_chain;
32629 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32631 /* If not defining a class, then any class scope is a scope level in
32632 an out-of-line member definition. In this case simply wind back
32633 beyond the first such scope to inject the template parameter list.
32634 Otherwise wind back to the class being defined. The latter can
32635 occur in class member friend declarations such as:
32637 class A {
32638 void foo (auto);
32640 class B {
32641 friend void A::foo (auto);
32644 The template parameter list synthesized for the friend declaration
32645 must be injected in the scope of 'B'. This can also occur in
32646 erroneous cases such as:
32648 struct A {
32649 struct B {
32650 void foo (auto);
32652 void B::foo (auto) {}
32655 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32656 but, nevertheless, the template parameter list synthesized for the
32657 declarator should be injected into the scope of 'A' as if the
32658 ill-formed template was specified explicitly. */
32660 while (scope->kind == sk_class && !scope->defining_class_p)
32662 parent_scope = scope;
32663 scope = scope->level_chain;
32667 current_binding_level = scope;
32669 if (scope->kind != sk_template_parms
32670 || !function_being_declared_is_template_p (parser))
32672 /* Introduce a new template parameter list for implicit template
32673 parameters. */
32675 become_template = true;
32677 parser->implicit_template_scope
32678 = begin_scope (sk_template_parms, NULL);
32680 ++processing_template_decl;
32682 parser->fully_implicit_function_template_p = true;
32683 ++parser->num_template_parameter_lists;
32685 else
32687 /* Synthesize implicit template parameters at the end of the explicit
32688 template parameter list. */
32690 gcc_assert (current_template_parms);
32692 parser->implicit_template_scope = scope;
32694 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32695 parser->implicit_template_parms
32696 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32700 /* Synthesize a new template parameter and track the current template
32701 parameter chain with implicit_template_parms. */
32703 tree synth_id = make_generic_type_name ();
32704 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32705 synth_id);
32706 tree new_parm
32707 = process_template_parm (parser->implicit_template_parms,
32708 input_location,
32709 build_tree_list (NULL_TREE, synth_tmpl_parm),
32710 /*non_type=*/false,
32711 /*param_pack=*/false);
32714 if (parser->implicit_template_parms)
32715 parser->implicit_template_parms
32716 = TREE_CHAIN (parser->implicit_template_parms);
32717 else
32718 parser->implicit_template_parms = new_parm;
32720 tree new_type = TREE_TYPE (getdecls ());
32722 /* If creating a fully implicit function template, start the new implicit
32723 template parameter list with this synthesized type, otherwise grow the
32724 current template parameter list. */
32726 if (become_template)
32728 parent_scope->level_chain = current_binding_level;
32730 tree new_parms = make_tree_vec (1);
32731 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32732 current_template_parms = tree_cons (size_int (processing_template_decl),
32733 new_parms, current_template_parms);
32735 else
32737 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32738 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32739 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32740 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32743 current_binding_level = entry_scope;
32745 return new_type;
32748 /* Finish the declaration of a fully implicit function template. Such a
32749 template has no explicit template parameter list so has not been through the
32750 normal template head and tail processing. synthesize_implicit_template_parm
32751 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32752 provided if the declaration is a class member such that its template
32753 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32754 form is returned. Otherwise NULL_TREE is returned. */
32756 tree
32757 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32759 gcc_assert (parser->fully_implicit_function_template_p);
32761 if (member_decl_opt && member_decl_opt != error_mark_node
32762 && DECL_VIRTUAL_P (member_decl_opt))
32764 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32765 "implicit templates may not be %<virtual%>");
32766 DECL_VIRTUAL_P (member_decl_opt) = false;
32769 if (member_decl_opt)
32770 member_decl_opt = finish_member_template_decl (member_decl_opt);
32771 end_template_decl ();
32773 parser->fully_implicit_function_template_p = false;
32774 --parser->num_template_parameter_lists;
32776 return member_decl_opt;
32779 #include "gt-cp-parser.h"